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

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 18 07:34:09 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))
----------------
aganea wrote:

It is simply to avoid calling twice `fixupDllMain()` and avoid displaying the message twice for each LIB. This is simply because we iterate on the `__imp_` symbols as well:
```
> dumpbin /all C:\src\hvn\havengine\third_party\toolchains\winsdk\10.0.22621.0\Lib\10.0.22621.0\um\x64\xinput.lib | findstr DllMain
      9D2 DllMain
      9D2 __imp_DllMain
        4 DllMain
        4 __imp_DllMain
  Symbol name  : DllMain
             1    DllMain
```

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


More information about the llvm-commits mailing list