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:
authormariuslozo-boop <254631011+mariuslozo-boop@users.noreply.github.com>2026-11-23 06:37:38 +0100
committermariuslozo-boop <254631011+mariuslozo-boop@users.noreply.github.com>2026-11-23 06:37:38 +0100
commit28ea6b971ba229e8caeb7bf4a50f263224b448fb (patch)
tree83612b5f78671113bdf91c82d8a4df8e94288f44 /runtime/src/pe_loader.cpp
parent9366e54f9744bbcd7df428c602ce1e775a9b9001 (diff)
parent98fa503f8a19c3ed65df1f4f2f0e4571aa42c4f7 (diff)
Merge remote-tracking branch 'origin/main' into rt/exe-tls-slot0
# Conflicts: # runtime/src/main.cpp
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 88ca6c431..a4c56cb5d 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -822,15 +822,7 @@ bool reserve_tls_index0(std::string* why) {
822 822
823namespace { 823namespace {
824bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t& rva) { 824bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t& rva) {
825 for (const Module* m : static_cast<Loader*>(ctx)->modules()) { 825 return static_cast<Loader*>(ctx)->find_address(address, module, rva);
826 const auto base = reinterpret_cast<uint64_t>(m->base);
827 if (m->base && address >= base && address - base < m->size) {
828 module = m->name;
829 rva = static_cast<uint32_t>(address - base);
830 return true;
831 }
832 }
833 return false;
834} 826}
835 827
836void* bundled_find_loaded(void* ctx, const std::string& dll_lower) { 828void* bundled_find_loaded(void* ctx, const std::string& dll_lower) {
@@ -1015,6 +1007,18 @@ void* Loader::resolve_module_export(void* module_handle, const std::string& sym,
1015 return nullptr; 1007 return nullptr;
1016} 1008}
1017 1009
1010bool Loader::find_address(uint64_t address, std::string& module, uint32_t& rva) const {
1011 std::shared_lock lock(ranges_mutex_);
1012 for (const MappedRange& r : ranges_) {
1013 if (address >= r.base && address - r.base < r.size) {
1014 module = r.name;
1015 rva = static_cast<uint32_t>(address - r.base);
1016 return true;
1017 }
1018 }
1019 return false;
1020}
1021
1018bool Loader::owns_module_handle(void* module_handle) const { 1022bool Loader::owns_module_handle(void* module_handle) const {
1019 if (!module_handle) return false; 1023 if (!module_handle) return false;
1020 std::lock_guard lock(mutex_); 1024 std::lock_guard lock(mutex_);
@@ -1143,6 +1147,12 @@ Module& Loader::map_image(const std::filesystem::path& path) {
1143 m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); 1147 m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
1144 if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); 1148 if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed");
1145 } 1149 }
1150 {
1151 // From here an address inside the image names it. The memory stays until ~Loader, which unregisters the
1152 // lookup first.
1153 std::unique_lock lock(ranges_mutex_);
1154 ranges_.push_back({reinterpret_cast<uint64_t>(m->base), m->size, m->name});
1155 }
1146 1156
1147 std::memcpy(m->base, file.data(), oh.SizeOfHeaders); 1157 std::memcpy(m->base, file.data(), oh.SizeOfHeaders);
1148 auto* sec = IMAGE_FIRST_SECTION(nt); 1158 auto* sec = IMAGE_FIRST_SECTION(nt);