From da499ab9113015ba91c2218895abc51f0aa4baf3 Mon Sep 17 00:00:00 2001 From: IAmNotSiob <273832158+IAmNotSiob@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:07:23 +0100 Subject: Loader: initialise dependencies before their importers map_image pushes a module into order_ before bind_imports maps the modules it imports, so order_ lists an importer ahead of its DLLs, and initialise() ran TLS callbacks and DllMain in that order. Keep a separate init_order_ filled after a module's imports are loaded and initialise from it. modules() stays mapping order. pe_loader_test checks tiny_dll comes before tiny_exe. --- runtime/src/pe_loader.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index f26527989..f5ada18f2 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -67,14 +67,16 @@ Module& Loader::load(const std::filesystem::path& image) { std::string key = lower(image.filename().string()); if (auto it = by_name_.find(key); it != by_name_.end()) return *it->second; Module& m = map_image(image); - bind_imports(m); + bind_imports(m); // loads every bundled module m imports first, so they land in init_order_ before m protect_sections(m); + init_order_.push_back(&m); return m; } void Loader::initialise() { - // order_ is dependency order: a module is pushed after the modules it imports. - for (auto* m : order_) initialise(*m); + // Not order_: map_image pushes a module there before bind_imports maps its dependencies, so order_ lists an + // importer ahead of the DLLs it imports. init_order_ is filled once a module's imports are all loaded. + for (auto* m : init_order_) initialise(*m); } void Loader::initialise(Module& m) { -- cgit v1.2.3