Skip to content

Commit 9c29291

Browse files
mstorsjotru
authored andcommitted
[LLD] [COFF] Fix export directives in object files from -includeoptional
When an object file contains an export directive, we normally do some amount of deferred processing of them at the end of the linking process. The -includeoptional option was handled after this, and any object files (defining new exports) weren't handled. Move the handling of the -includeoptional into the same late loop which does the fixups for e.g. export directives. Ideally, this would also be done for object files that are pulled in by the wrap options, and for mingw autoimports, but those changes require more modifications, to make them safe for potentially being executed multiple times. This fixes #57243. Differential Revision: https://reviews.llvm.org/D132361 (cherry picked from commit af39e6f)
1 parent e6f03d1 commit 9c29291

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

lld/COFF/Driver.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -2228,15 +2228,14 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
22282228
// Windows specific -- if __load_config_used can be resolved, resolve it.
22292229
if (ctx.symtab.findUnderscore("_load_config_used"))
22302230
addUndefined(mangle("_load_config_used"));
2231-
} while (run());
22322231

2233-
if (args.hasArg(OPT_include_optional)) {
2234-
// Handle /includeoptional
2235-
for (auto *arg : args.filtered(OPT_include_optional))
2236-
if (isa_and_nonnull<LazyArchive>(ctx.symtab.find(arg->getValue())))
2237-
addUndefined(arg->getValue());
2238-
while (run());
2239-
}
2232+
if (args.hasArg(OPT_include_optional)) {
2233+
// Handle /includeoptional
2234+
for (auto *arg : args.filtered(OPT_include_optional))
2235+
if (isa_and_nonnull<LazyArchive>(ctx.symtab.find(arg->getValue())))
2236+
addUndefined(arg->getValue());
2237+
}
2238+
} while (run());
22402239

22412240
// Create wrapped symbols for -wrap option.
22422241
std::vector<WrappedSymbol> wrapped = addWrappedSymbols(ctx, args);
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// REQUIRES: x86
2+
// RUN: split-file %s %t.dir
3+
4+
// RUN: llvm-mc -filetype=obj -triple=x86_64-win32-gnu %t.dir/main.s -o %t.main.o
5+
// RUN: llvm-mc -filetype=obj -triple=x86_64-win32-gnu %t.dir/lib1.s -o %t.lib1.o
6+
// RUN: llvm-mc -filetype=obj -triple=x86_64-win32-gnu %t.dir/lib2.s -o %t.lib2.o
7+
8+
// RUN: rm -f %t.lib.a
9+
// RUN: llvm-ar cru %t.lib.a %t.lib1.o %t.lib2.o
10+
// RUN: lld-link -dll -out:%t-1.dll -entry:entry %t.main.o %t.lib.a
11+
// RUN: lld-link -dll -out:%t-2.dll -entry:entry %t.main.o %t.lib.a -includeoptional:libfunc
12+
13+
// RUN: llvm-readobj --coff-exports %t-1.dll | FileCheck --implicit-check-not=Name: %s --check-prefix=CHECK-DEFAULT
14+
// RUN: llvm-readobj --coff-exports %t-2.dll | FileCheck --implicit-check-not=Name: %s --check-prefix=CHECK-INCLUDEOPTIONAL
15+
16+
// CHECK-DEFAULT: Name:
17+
// CHECK-DEFAULT: Name: myfunc
18+
19+
// CHECK-INCLUDEOPTIONAL: Name:
20+
// CHECK-INCLUDEOPTIONAL: Name: libfunc
21+
// CHECK-INCLUDEOPTIONAL: Name: myfunc
22+
// CHECK-INCLUDEOPTIONAL: Name: otherlibfunc
23+
24+
#--- main.s
25+
.global entry
26+
entry:
27+
ret
28+
29+
.global myfunc
30+
myfunc:
31+
ret
32+
33+
.section .drectve
34+
.ascii "-export:myfunc "
35+
36+
#--- lib1.s
37+
.global libfunc
38+
libfunc:
39+
call otherlibfunc
40+
ret
41+
42+
.section .drectve
43+
.ascii "-export:libfunc "
44+
45+
#--- lib2.s
46+
.global otherlibfunc
47+
otherlibfunc:
48+
ret
49+
50+
.section .drectve
51+
.ascii "-export:otherlibfunc "

0 commit comments

Comments
 (0)