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:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-23 11:41:08 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 05:46:57 -0500
commit82d6883bed1c18dfb4d409314e7805262aded79e (patch)
tree1cee8445872cf1a2175c02bb64aadbb1424f5c68 /runtime/src/pe_loader.cpp
parent21625bca8c675c62f9dbffc318ce6a59d57faf31 (diff)
pe_loader: trace delay imports bound to an unimplemented thunk; note delay-load and thread-attach order
Delay imports are bound eagerly (deliberate: --dry-run shows the whole import surface). A delay-loaded DLL or function that nothing serves becomes an unimplemented thunk returning 0 at its first call, where Windows' delay-load helper raises 0xC06D007E / 0xC06D007F, which a title can catch for a fallback. The binding is unchanged; --trace now writes a delay_import_unimplemented event (module, import) for each such import at load, so the first boot names them before any image code runs. docs/runtime_imports.md gets two notes on where the loader differs from Windows: the eager delay binding above, and that DLL_THREAD_ATTACH reaches TLS callbacks in TLS slot order, which puts the executable before the DLLs it imports (Windows' order is undocumented). runtime/README.md lists the new trace event and now says the loader applies DIR64 and HIGHLOW relocations and rejects HIGH, LOW and HIGHADJ (old upstream 761). startup_trace_test gets a synthetic DLL with one ordinary and two delay imports (one missing, one bound to kernel32): exactly one delay_import_unimplemented event, naming the missing delay import. Findings by cynbun in the old upstream coordination issue (old upstream 7231). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp13
1 files changed, 10 insertions, 3 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index c29a288a9..ebb1531bd 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -1796,7 +1796,10 @@ void Loader::bind_imports(Module& m) {
1796 const uint64_t headers_end = nt->OptionalHeader.SizeOfHeaders; 1796 const uint64_t headers_end = nt->OptionalHeader.SizeOfHeaders;
1797 1797
1798 // Thunk tables have no length field: they end at a zero entry, so each step is checked against the image. 1798 // Thunk tables have no length field: they end at a zero entry, so each step is checked against the image.
1799 auto bind_thunks = [&](const std::string& dll, uint64_t lookup_rva, uint64_t iat_rva) { 1799 // `delayed`: a delay import that binds to an unimplemented thunk is traced. Windows' delay-load helper would raise
1800 // 0xC06D007E (module) or 0xC06D007F (procedure) at its first call, which a title can catch for a fallback; the
1801 // thunk returns 0 instead, so the trace names each such import before any image code runs.
1802 auto bind_thunks = [&](const std::string& dll, uint64_t lookup_rva, uint64_t iat_rva, bool delayed) {
1800 for (;; lookup_rva += sizeof(IMAGE_THUNK_DATA64), iat_rva += sizeof(IMAGE_THUNK_DATA64)) { 1803 for (;; lookup_rva += sizeof(IMAGE_THUNK_DATA64), iat_rva += sizeof(IMAGE_THUNK_DATA64)) {
1801 require_range(m.name, lookup_rva, sizeof(IMAGE_THUNK_DATA64), m.size, "an import lookup table"); 1804 require_range(m.name, lookup_rva, sizeof(IMAGE_THUNK_DATA64), m.size, "an import lookup table");
1802 require_range(m.name, iat_rva, sizeof(IMAGE_THUNK_DATA64), m.size, "an import address table"); 1805 require_range(m.name, iat_rva, sizeof(IMAGE_THUNK_DATA64), m.size, "an import address table");
@@ -1814,6 +1817,10 @@ void Loader::bind_imports(Module& m) {
1814 throw std::runtime_error(m.name + ": an import address table lands in the headers"); 1817 throw std::runtime_error(m.name + ": an import address table lands in the headers");
1815 at<IMAGE_THUNK_DATA64>(m.base, static_cast<uint32_t>(iat_rva))->u1.Function = 1818 at<IMAGE_THUNK_DATA64>(m.base, static_cast<uint32_t>(iat_rva))->u1.Function =
1816 reinterpret_cast<uint64_t>(resolve(m, dll, name, ord, by_ord)); 1819 reinterpret_cast<uint64_t>(resolve(m, dll, name, ord, by_ord));
1820 if (delayed && startup_trace::enabled() && m.imports.back().binding == Binding::Unimplemented)
1821 startup_trace::event("delay_import_unimplemented",
1822 "\"module\":" + startup_trace::quote(m.name) + ",\"import\":" +
1823 startup_trace::quote(dll + "!" + m.imports.back().name));
1817 } 1824 }
1818 }; 1825 };
1819 1826
@@ -1826,7 +1833,7 @@ void Loader::bind_imports(Module& m) {
1826 if (!d->Name) break; 1833 if (!d->Name) break;
1827 std::string dll = read_string(m.name, m.base, m.size, d->Name, "an imported DLL name"); 1834 std::string dll = read_string(m.name, m.base, m.size, d->Name, "an imported DLL name");
1828 uint32_t lookup = d->OriginalFirstThunk ? d->OriginalFirstThunk : d->FirstThunk; 1835 uint32_t lookup = d->OriginalFirstThunk ? d->OriginalFirstThunk : d->FirstThunk;
1829 bind_thunks(dll, lookup, d->FirstThunk); 1836 bind_thunks(dll, lookup, d->FirstThunk, false);
1830 } 1837 }
1831 } 1838 }
1832 // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. 1839 // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run.
@@ -1849,7 +1856,7 @@ void Loader::bind_imports(Module& m) {
1849 // thunk addresses as name RVAs. 1856 // thunk addresses as name RVAs.
1850 if (!d->ImportNameTableRVA) 1857 if (!d->ImportNameTableRVA)
1851 throw std::runtime_error(m.name + ": a delay-import descriptor has no name table"); 1858 throw std::runtime_error(m.name + ": a delay-import descriptor has no name table");
1852 bind_thunks(dll, d->ImportNameTableRVA, d->ImportAddressTableRVA); 1859 bind_thunks(dll, d->ImportNameTableRVA, d->ImportAddressTableRVA, true);
1853 } 1860 }
1854 } 1861 }
1855} 1862}