upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp28
1 files changed, 19 insertions, 9 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index f2c62ec91..f894fcb43 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -454,15 +454,7 @@ void redirect_pc_to_file_header_at(const void* resolved) {
454 454
455namespace { 455namespace {
456bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t& rva) { 456bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t& rva) {
457 for (const Module* m : static_cast<Loader*>(ctx)->modules()) { 457 return static_cast<Loader*>(ctx)->find_address(address, module, rva);
458 const auto base = reinterpret_cast<uint64_t>(m->base);
459 if (m->base && address >= base && address - base < m->size) {
460 module = m->name;
461 rva = static_cast<uint32_t>(address - base);
462 return true;
463 }
464 }
465 return false;
466} 458}
467 459
468void* bundled_find_loaded(void* ctx, const std::string& dll_lower) { 460void* bundled_find_loaded(void* ctx, const std::string& dll_lower) {
@@ -637,6 +629,18 @@ void* Loader::resolve_module_export(void* module_handle, const std::string& sym,
637 return nullptr; 629 return nullptr;
638} 630}
639 631
632bool Loader::find_address(uint64_t address, std::string& module, uint32_t& rva) const {
633 std::lock_guard lock(ranges_mutex_);
634 for (const MappedRange& r : ranges_) {
635 if (address >= r.base && address - r.base < r.size) {
636 module = r.name;
637 rva = static_cast<uint32_t>(address - r.base);
638 return true;
639 }
640 }
641 return false;
642}
643
640bool Loader::owns_module_handle(void* module_handle) const { 644bool Loader::owns_module_handle(void* module_handle) const {
641 if (!module_handle) return false; 645 if (!module_handle) return false;
642 std::lock_guard lock(mutex_); 646 std::lock_guard lock(mutex_);
@@ -765,6 +769,12 @@ Module& Loader::map_image(const std::filesystem::path& path) {
765 m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); 769 m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
766 if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); 770 if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed");
767 } 771 }
772 {
773 // From here an address inside the image names it. The memory stays until ~Loader, which unregisters the
774 // lookup first.
775 std::lock_guard lock(ranges_mutex_);
776 ranges_.push_back({reinterpret_cast<uint64_t>(m->base), m->size, m->name});
777 }
768 778
769 std::memcpy(m->base, file.data(), oh.SizeOfHeaders); 779 std::memcpy(m->base, file.data(), oh.SizeOfHeaders);
770 auto* sec = IMAGE_FIRST_SECTION(nt); 780 auto* sec = IMAGE_FIRST_SECTION(nt);