Skip to content

Commit d1da646

Browse files
committed
[LLD] [MinGW] Implement the --exclude-symbols option
This adds support for the existing GNU ld command line option, which allows excluding individual symbols from autoexport (when linking a DLL and no symbols are marked explicitly as dllexported). Differential Revision: https://reviews.llvm.org/D130118
1 parent 1640679 commit d1da646

File tree

9 files changed

+44
-1
lines changed

9 files changed

+44
-1
lines changed

lld/COFF/Driver.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,13 @@ void LinkerDriver::maybeExportMinGWSymbols(const opt::InputArgList &args) {
13151315
if (Optional<StringRef> path = doFindFile(arg->getValue()))
13161316
exporter.addWholeArchive(*path);
13171317

1318+
for (auto *arg : args.filtered(OPT_exclude_symbols)) {
1319+
SmallVector<StringRef, 2> vec;
1320+
StringRef(arg->getValue()).split(vec, ',');
1321+
for (StringRef sym : vec)
1322+
exporter.addExcludedSymbol(mangle(sym));
1323+
}
1324+
13181325
ctx.symtab.forEachSymbol([&](Symbol *s) {
13191326
auto *def = dyn_cast<Defined>(s);
13201327
if (!exporter.shouldExport(ctx, def))

lld/COFF/MinGW.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ void AutoExporter::addWholeArchive(StringRef path) {
122122
excludeLibs.erase(libName);
123123
}
124124

125+
void AutoExporter::addExcludedSymbol(StringRef symbol) {
126+
excludeSymbols.insert(symbol);
127+
}
128+
125129
bool AutoExporter::shouldExport(const COFFLinkerContext &ctx,
126130
Defined *sym) const {
127131
if (!sym || !sym->getChunk())

lld/COFF/MinGW.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class AutoExporter {
2727
AutoExporter();
2828

2929
void addWholeArchive(StringRef path);
30+
void addExcludedSymbol(StringRef symbol);
3031

3132
llvm::StringSet<> excludeSymbols;
3233
llvm::StringSet<> excludeSymbolPrefixes;

lld/COFF/Options.td

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def diasdkdir : P<"diasdkdir", "Set the location of the DIA SDK">;
4545
def entry : P<"entry", "Name of entry point symbol">;
4646
def errorlimit : P<"errorlimit",
4747
"Maximum number of errors to emit before stopping (0 = no limit)">;
48+
def exclude_symbols : P<"exclude-symbols", "Exclude symbols from automatic export">,
49+
MetaVarName<"<symbol[,symbol,...]>">;
4850
def export : P<"export", "Export a function">;
4951
// No help text because /failifmismatch is not intended to be used by the user.
5052
def failifmismatch : P<"failifmismatch", "">;

lld/MinGW/Driver.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ bool mingw::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
398398
add("-delayload:" + StringRef(a->getValue()));
399399
for (auto *a : args.filtered(OPT_wrap))
400400
add("-wrap:" + StringRef(a->getValue()));
401+
for (auto *a : args.filtered(OPT_exclude_symbols))
402+
add("-exclude-symbols:" + StringRef(a->getValue()));
401403

402404
std::vector<StringRef> searchPaths;
403405
for (auto *a : args.filtered(OPT_L)) {

lld/MinGW/Options.td

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def enable_stdcall_fixup: F<"enable-stdcall-fixup">,
6262
defm entry: Eq<"entry", "Name of entry point symbol">, MetaVarName<"<entry>">;
6363
def exclude_all_symbols: F<"exclude-all-symbols">,
6464
HelpText<"Don't automatically export any symbols">;
65+
defm exclude_symbols: Eq<"exclude-symbols",
66+
"Exclude symbols from automatic export">, MetaVarName<"<symbol[,symbol,...]>">;
6567
def export_all_symbols: F<"export-all-symbols">,
6668
HelpText<"Export all symbols even if a def file or dllexport attributes are used">;
6769
defm fatal_warnings: B<"fatal-warnings",

lld/docs/ReleaseNotes.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ COFF Improvements
3939
MinGW Improvements
4040
------------------
4141

42-
* ...
42+
* The ``--exclude-symbols`` option is now supported.
43+
(`D130118 <https://reviews.llvm.org/D130118>`_)
4344

4445
MachO Improvements
4546
------------------

lld/test/COFF/exclude-symbols.s

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// REQUIRES: x86
2+
// RUN: llvm-mc -filetype=obj -triple=i686-win32-gnu %s -o %t.o
3+
4+
// RUN: lld-link -lldmingw -dll -out:%t.dll %t.o -noentry -exclude-symbols:sym2,unknownsym -exclude-symbols:unknownsym,sym3
5+
// RUN: llvm-readobj --coff-exports %t.dll | FileCheck --implicit-check-not=Name: %s
6+
7+
// CHECK: Name:
8+
// CHECK: Name: sym1
9+
10+
.global _sym1
11+
_sym1:
12+
ret
13+
14+
.global _sym2
15+
_sym2:
16+
ret
17+
18+
.global _sym3
19+
_sym3:
20+
ret

lld/test/MinGW/driver.test

+4
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ RUN: ld.lld -### -m i386pep foo.o -wrap foo1 --wrap foo2 2>&1 | FileCheck -check
321321
RUN: ld.lld -### -m i386pep foo.o -wrap=foo1 --wrap=foo2 2>&1 | FileCheck -check-prefix WRAP %s
322322
WRAP: -wrap:foo1 -wrap:foo2
323323

324+
RUN: ld.lld -### -m i386pep foo.o -exclude-symbols sym1,sym2 --exclude-symbols sym3 2>&1 | FileCheck -check-prefix EXCLUDE_SYMBOLS %s
325+
RUN: ld.lld -### -m i386pep foo.o -exclude-symbols=sym1,sym2 --exclude-symbols=sym3 2>&1 | FileCheck -check-prefix EXCLUDE_SYMBOLS %s
326+
EXCLUDE_SYMBOLS: -exclude-symbols:sym1,sym2 -exclude-symbols:sym3
327+
324328
RUN: ld.lld -### -m i386pep foo.o 2>&1 | FileCheck -check-prefix DEMANGLE %s
325329
RUN: ld.lld -### -m i386pep foo.o -demangle 2>&1 | FileCheck -check-prefix DEMANGLE %s
326330
RUN: ld.lld -### -m i386pep foo.o --demangle 2>&1 | FileCheck -check-prefix DEMANGLE %s

0 commit comments

Comments
 (0)