[lld] [LLD][COFF] Follow up comments on pr146610 (PR #147152)

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 17 22:58:29 PDT 2025


================
@@ -204,14 +202,24 @@ void ArchiveFile::parse() {
     }
   }
 
-  // Read the symbol table to construct Lazy objects.
   bool skipDllMain = false;
+  StringRef mangledDllMain, impMangledDllMain;
+
+  // The calls below will fail if we haven't set the machine type yet. Instead
+  // of failing, it is preferable to skip this "imported DllMain" check if we
+  // don't know the machine type at this point.
+  if (!file->isEmpty() && ctx.config.machine != IMAGE_FILE_MACHINE_UNKNOWN) {
+    mangledDllMain = archiveSymtab->mangle("DllMain");
+    impMangledDllMain = uniqueSaver().save("__imp_" + mangledDllMain);
+  }
+
+  // Read the symbol table to construct Lazy objects.
   for (const Archive::Symbol &sym : file->symbols()) {
-    // If the DllMain symbol was exported by mistake, skip importing it
-    // otherwise we might end up with a import thunk in the final binary which
-    // is wrong.
-    if (sym.getName() == "__imp_DllMain" || sym.getName() == "DllMain") {
-      if (fixupDllMain(ctx, file.get(), sym, skipDllMain))
+    // If an import library provides the DllMain symbol, skip importing it, as
+    // we should be using our own DllMain, not another DLL's DllMain.
+    if (!mangledDllMain.empty() && (sym.getName() == mangledDllMain ||
+                                    sym.getName() == impMangledDllMain)) {
+      if (skipDllMain || fixupDllMain(ctx, file.get(), sym, skipDllMain))
----------------
mstorsjo wrote:

Unrelated to this change in itself - why do we need to check if `skipDllMain` already was set? As far as I can see - the effect is to skip any (even a regular object) `DllMain` if we already saw an import object `DllMain` in the same archive. But if the objects would happen to be iterated over in a different order, that wouldn’t happen. Is this a functional aspect of this feature?

https://github.com/llvm/llvm-project/pull/147152


More information about the llvm-commits mailing list