diff options
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index c29a288a9..253a773b5 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -1425,10 +1425,7 @@ Module& Loader::map_image(const std::filesystem::path& path, const std::string& | |||
| 1425 | if (!m->base) | 1425 | if (!m->base) |
| 1426 | m->base = static_cast<uint8_t*>(VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, | 1426 | m->base = static_cast<uint8_t*>(VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, |
| 1427 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 1427 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| 1428 | if (!m->base) { | 1428 | if (!m->base) { // whether the image may move is decided at the relocation step below |
| 1429 | const auto relocs = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); | ||
| 1430 | if (!relocs.Size) | ||
| 1431 | throw std::runtime_error(m->name + ": preferred base is taken and the image has no relocations"); | ||
| 1432 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 1429 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| 1433 | if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); | 1430 | if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); |
| 1434 | } | 1431 | } |
| @@ -1453,10 +1450,19 @@ Module& Loader::map_image(const std::filesystem::path& path, const std::string& | |||
| 1453 | } | 1450 | } |
| 1454 | 1451 | ||
| 1455 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); | 1452 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); |
| 1456 | if (delta) { | 1453 | // Windows' loader (LdrRelocateImage) refuses to move an image only when IMAGE_FILE_RELOCS_STRIPPED is set; with the |
| 1457 | m->relocated = true; | 1454 | // flag clear and no relocation directory it moves it and fixes up nothing, so the image is trusted to hold no |
| 1458 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); | 1455 | // absolute addresses. That move is logged so it shows in traces. base_reservation::needed still keeps the main |
| 1459 | if (!dir.Size) throw std::runtime_error(m->name + ": the image has no relocations and isn't at its base"); | 1456 | // image's range free when it has no relocation directory, so a main image is moved this way only where the |
| 1457 | // range could not be had. | ||
| 1458 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); | ||
| 1459 | if (delta) m->relocated = true; | ||
| 1460 | if (delta && (nt->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)) | ||
| 1461 | throw std::runtime_error(m->name + ": preferred base is taken and the image's relocations are stripped"); | ||
| 1462 | if (delta && !dir.Size) | ||
| 1463 | std::fprintf(stderr, "[rt] loader: %s: preferred base %#llx is taken, moved to %p with no relocations\n", | ||
| 1464 | m->name.c_str(), static_cast<unsigned long long>(oh.ImageBase), static_cast<void*>(m->base)); | ||
| 1465 | if (delta && dir.Size) { | ||
| 1460 | require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); | 1466 | require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); |
| 1461 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); | 1467 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); |
| 1462 | auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size; | 1468 | auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size; |