Skip to content

Commit 9a4b6b7

Browse files
committed
Follow up comments on pr146610
1 parent 9d994d1 commit 9a4b6b7

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

lld/COFF/Config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ struct Configuration {
307307
bool warnDebugInfoUnusable = true;
308308
bool warnLongSectionNames = true;
309309
bool warnStdcallFixup = true;
310-
bool warnExportedDllMain = true;
310+
bool warnImportedDllMain = true;
311311
bool incremental = true;
312312
bool integrityCheck = false;
313313
bool killAt = false;

lld/COFF/Driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,8 +1643,8 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
16431643
config->warnLocallyDefinedImported = false;
16441644
else if (s == "longsections")
16451645
config->warnLongSectionNames = false;
1646-
else if (s == "exporteddllmain")
1647-
config->warnExportedDllMain = false;
1646+
else if (s == "importeddllmain")
1647+
config->warnImportedDllMain = false;
16481648
// Other warning numbers are ignored.
16491649
}
16501650
}

lld/COFF/InputFiles.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ static bool fixupDllMain(COFFLinkerContext &ctx, llvm::object::Archive *file,
128128
file->getFileName() +
129129
": could not get the buffer for a child buffer of the archive");
130130
if (identify_magic(mb.getBuffer()) == file_magic::coff_import_library) {
131-
if (ctx.config.warnExportedDllMain) {
131+
if (ctx.config.warnImportedDllMain) {
132132
// We won't place DllMain symbols in the symbol table if they are
133133
// coming from a import library. This message can be ignored with the flag
134-
// '/ignore:exporteddllmain'
134+
// '/ignore:importeddllmain'
135135
Warn(ctx)
136136
<< file->getFileName()
137-
<< ": skipping exported DllMain symbol [exporteddllmain]\nNOTE: this "
137+
<< ": skipping imported DllMain symbol [importeddllmain]\nNOTE: this "
138138
"might be a mistake when the DLL/library was produced.";
139139
}
140140
skipDllMain = true;
@@ -207,10 +207,10 @@ void ArchiveFile::parse() {
207207
// Read the symbol table to construct Lazy objects.
208208
bool skipDllMain = false;
209209
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") {
210+
// If an import library provides the DllMain symbol, skip importing it, as
211+
// we should be using our own DllMain, not another DLL's DllMain.
212+
if (sym.getName() == "__imp_DllMain" || sym.getName() == "DllMain" ||
213+
sym.getName() == "_DllMain") {
214214
if (fixupDllMain(ctx, file.get(), sym, skipDllMain))
215215
continue;
216216
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
REQUIRES: x86
2+
RUN: split-file %s %t.dir && cd %t.dir
3+
4+
RUN: llvm-mc -filetype=obj -triple=i386-windows a.s -o a.obj
5+
6+
RUN: llvm-mc -filetype=obj -triple=i386-windows b1.s -o b1.obj
7+
RUN: llvm-mc -filetype=obj -triple=i386-windows b2.s -o b2.obj
8+
9+
### This is the line where our problem occurs. Here, we export the DllMain symbol which shouldn't happen normally.
10+
RUN: lld-link b1.obj b2.obj -out:b.dll -dll -implib:b.lib -entry:DllMain -export:bar -export:DllMain -safeseh:no
11+
12+
RUN: llvm-mc -filetype=obj -triple=i386-windows c.s -o c.obj
13+
RUN: lld-link -lib c.obj -out:c.lib
14+
15+
### Later, if b.lib is provided before other libs/objs that export DllMain statically, we previously were using the dllimported DllMain from b.lib, which is wrong.
16+
RUN: lld-link a.obj b.lib c.lib -dll -out:out.dll -entry:DllMain -safeseh:no 2>&1 | FileCheck -check-prefix=WARN %s
17+
RUN: lld-link a.obj b.lib c.lib -dll -out:out.dll -entry:DllMain -ignore:importeddllmain -safeseh:no 2>&1 | FileCheck -check-prefix=IGNORED --allow-empty %s
18+
RUN: llvm-objdump --private-headers -d out.dll | FileCheck -check-prefix=DISASM %s
19+
20+
WARN: lld-link: warning: b.lib: skipping imported DllMain symbol [importeddllmain]
21+
IGNORED-NOT: lld-link: warning: b.lib: skipping imported DllMain symbol [importeddllmain]
22+
23+
DISASM: The Import Tables:
24+
DISASM: DLL Name: b.dll
25+
DISASM-NOT: DllMain
26+
DISASM: bar
27+
DISASM: Disassembly of section .text:
28+
DISASM-EMPTY:
29+
DISASM-NEXT: b0 01 movb $0x1, %al
30+
DISASM-NEXT: c3 retl
31+
32+
#--- a.s
33+
.text
34+
.globl _foo
35+
_foo:
36+
call *__imp__bar
37+
ret
38+
39+
#--- b1.s
40+
.text
41+
.globl _bar
42+
_bar:
43+
ret
44+
45+
#--- b2.s
46+
.intel_syntax noprefix
47+
.text
48+
.globl _DllMain
49+
_DllMain:
50+
xor al, al
51+
ret
52+
53+
#--- c.s
54+
.intel_syntax noprefix
55+
.text
56+
.globl _DllMain
57+
_DllMain:
58+
mov al, 1
59+
ret

lld/test/COFF/exported-dllmain.test renamed to lld/test/COFF/imported-dllmain.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ RUN: lld-link -lib c.obj -out:c.lib
1414

1515
### Later, if b.lib is provided before other libs/objs that export DllMain statically, we previously were using the dllimported DllMain from b.lib, which is wrong.
1616
RUN: lld-link a.obj b.lib c.lib -dll -out:out.dll -entry:DllMain 2>&1 | FileCheck -check-prefix=WARN %s
17-
RUN: lld-link a.obj b.lib c.lib -dll -out:out.dll -entry:DllMain -ignore:exporteddllmain 2>&1 | FileCheck -check-prefix=IGNORED --allow-empty %s
17+
RUN: lld-link a.obj b.lib c.lib -dll -out:out.dll -entry:DllMain -ignore:importeddllmain 2>&1 | FileCheck -check-prefix=IGNORED --allow-empty %s
1818
RUN: llvm-objdump --private-headers -d out.dll | FileCheck -check-prefix=DISASM %s
1919

20-
WARN: lld-link: warning: b.lib: skipping exported DllMain symbol [exporteddllmain]
21-
IGNORED-NOT: lld-link: warning: b.lib: skipping exported DllMain symbol [exporteddllmain]
20+
WARN: lld-link: warning: b.lib: skipping imported DllMain symbol [importeddllmain]
21+
IGNORED-NOT: lld-link: warning: b.lib: skipping imported DllMain symbol [importeddllmain]
2222

2323
DISASM: The Import Tables:
2424
DISASM: DLL Name: b.dll

0 commit comments

Comments
 (0)