@@ -117,8 +117,6 @@ static coff_symbol_generic *cloneSymbol(COFFSymbolRef sym) {
117117// Skip importing DllMain thunks from import libraries.
118118static bool fixupDllMain (COFFLinkerContext &ctx, llvm::object::Archive *file,
119119 const Archive::Symbol &sym, bool &skipDllMain) {
120- if (skipDllMain)
121- return true ;
122120 const Archive::Child &c =
123121 CHECK (sym.getMember (), file->getFileName () +
124122 " : could not get the member for symbol " +
@@ -128,13 +126,13 @@ static bool fixupDllMain(COFFLinkerContext &ctx, llvm::object::Archive *file,
128126 file->getFileName () +
129127 " : could not get the buffer for a child buffer of the archive" );
130128 if (identify_magic (mb.getBuffer ()) == file_magic::coff_import_library) {
131- if (ctx.config .warnExportedDllMain ) {
129+ if (ctx.config .warnImportedDllMain ) {
132130 // We won't place DllMain symbols in the symbol table if they are
133131 // coming from a import library. This message can be ignored with the flag
134- // '/ignore:exporteddllmain '
132+ // '/ignore:importeddllmain '
135133 Warn (ctx)
136134 << file->getFileName ()
137- << " : skipping exported DllMain symbol [exporteddllmain ]\n NOTE: this "
135+ << " : skipping imported DllMain symbol [importeddllmain ]\n NOTE: this "
138136 " might be a mistake when the DLL/library was produced." ;
139137 }
140138 skipDllMain = true ;
@@ -204,14 +202,24 @@ void ArchiveFile::parse() {
204202 }
205203 }
206204
207- // Read the symbol table to construct Lazy objects.
208205 bool skipDllMain = false ;
206+ StringRef mangledDllMain, impMangledDllMain;
207+
208+ // The calls below will fail if we haven't set the machine type yet. Instead
209+ // of failing, it is preferable to skip this "imported DllMain" check if we
210+ // don't know the machine type at this point.
211+ if (!file->isEmpty () && ctx.config .machine != IMAGE_FILE_MACHINE_UNKNOWN) {
212+ mangledDllMain = archiveSymtab->mangle (" DllMain" );
213+ impMangledDllMain = uniqueSaver ().save (" __imp_" + mangledDllMain);
214+ }
215+
216+ // Read the symbol table to construct Lazy objects.
209217 for (const Archive::Symbol &sym : file->symbols ()) {
210- // If the DllMain symbol was exported by mistake , skip importing it
211- // otherwise we might end up with a import thunk in the final binary which
212- // is wrong.
213- if (sym. getName () == " __imp_DllMain " || sym.getName () == " DllMain " ) {
214- if (fixupDllMain (ctx, file.get (), sym, skipDllMain))
218+ // If an import library provides the DllMain symbol , skip importing it, as
219+ // we should be using our own DllMain, not another DLL's DllMain.
220+ if (!mangledDllMain. empty () && (sym. getName () == mangledDllMain ||
221+ sym.getName () == impMangledDllMain) ) {
222+ if (skipDllMain || fixupDllMain (ctx, file.get (), sym, skipDllMain))
215223 continue ;
216224 }
217225 archiveSymtab->addLazyArchive (this , sym);
0 commit comments