Skip to content

Commit

Permalink
[LLD] [MinGW] Implement the --exclude-symbols option
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mstorsjo committed Aug 11, 2022
1 parent 1640679 commit d1da646
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,13 @@ void LinkerDriver::maybeExportMinGWSymbols(const opt::InputArgList &args) {
if (Optional<StringRef> path = doFindFile(arg->getValue()))
exporter.addWholeArchive(*path);

for (auto *arg : args.filtered(OPT_exclude_symbols)) {
SmallVector<StringRef, 2> vec;
StringRef(arg->getValue()).split(vec, ',');
for (StringRef sym : vec)
exporter.addExcludedSymbol(mangle(sym));
}

ctx.symtab.forEachSymbol([&](Symbol *s) {
auto *def = dyn_cast<Defined>(s);
if (!exporter.shouldExport(ctx, def))
Expand Down
4 changes: 4 additions & 0 deletions lld/COFF/MinGW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ void AutoExporter::addWholeArchive(StringRef path) {
excludeLibs.erase(libName);
}

void AutoExporter::addExcludedSymbol(StringRef symbol) {
excludeSymbols.insert(symbol);
}

bool AutoExporter::shouldExport(const COFFLinkerContext &ctx,
Defined *sym) const {
if (!sym || !sym->getChunk())
Expand Down
1 change: 1 addition & 0 deletions lld/COFF/MinGW.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AutoExporter {
AutoExporter();

void addWholeArchive(StringRef path);
void addExcludedSymbol(StringRef symbol);

llvm::StringSet<> excludeSymbols;
llvm::StringSet<> excludeSymbolPrefixes;
Expand Down
2 changes: 2 additions & 0 deletions lld/COFF/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def diasdkdir : P<"diasdkdir", "Set the location of the DIA SDK">;
def entry : P<"entry", "Name of entry point symbol">;
def errorlimit : P<"errorlimit",
"Maximum number of errors to emit before stopping (0 = no limit)">;
def exclude_symbols : P<"exclude-symbols", "Exclude symbols from automatic export">,
MetaVarName<"<symbol[,symbol,...]>">;
def export : P<"export", "Export a function">;
// No help text because /failifmismatch is not intended to be used by the user.
def failifmismatch : P<"failifmismatch", "">;
Expand Down
2 changes: 2 additions & 0 deletions lld/MinGW/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ bool mingw::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
add("-delayload:" + StringRef(a->getValue()));
for (auto *a : args.filtered(OPT_wrap))
add("-wrap:" + StringRef(a->getValue()));
for (auto *a : args.filtered(OPT_exclude_symbols))
add("-exclude-symbols:" + StringRef(a->getValue()));

std::vector<StringRef> searchPaths;
for (auto *a : args.filtered(OPT_L)) {
Expand Down
2 changes: 2 additions & 0 deletions lld/MinGW/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def enable_stdcall_fixup: F<"enable-stdcall-fixup">,
defm entry: Eq<"entry", "Name of entry point symbol">, MetaVarName<"<entry>">;
def exclude_all_symbols: F<"exclude-all-symbols">,
HelpText<"Don't automatically export any symbols">;
defm exclude_symbols: Eq<"exclude-symbols",
"Exclude symbols from automatic export">, MetaVarName<"<symbol[,symbol,...]>">;
def export_all_symbols: F<"export-all-symbols">,
HelpText<"Export all symbols even if a def file or dllexport attributes are used">;
defm fatal_warnings: B<"fatal-warnings",
Expand Down
3 changes: 2 additions & 1 deletion lld/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ COFF Improvements
MinGW Improvements
------------------

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

MachO Improvements
------------------
Expand Down
20 changes: 20 additions & 0 deletions lld/test/COFF/exclude-symbols.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// REQUIRES: x86
// RUN: llvm-mc -filetype=obj -triple=i686-win32-gnu %s -o %t.o

// RUN: lld-link -lldmingw -dll -out:%t.dll %t.o -noentry -exclude-symbols:sym2,unknownsym -exclude-symbols:unknownsym,sym3
// RUN: llvm-readobj --coff-exports %t.dll | FileCheck --implicit-check-not=Name: %s

// CHECK: Name:
// CHECK: Name: sym1

.global _sym1
_sym1:
ret

.global _sym2
_sym2:
ret

.global _sym3
_sym3:
ret
4 changes: 4 additions & 0 deletions lld/test/MinGW/driver.test
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ RUN: ld.lld -### -m i386pep foo.o -wrap foo1 --wrap foo2 2>&1 | FileCheck -check
RUN: ld.lld -### -m i386pep foo.o -wrap=foo1 --wrap=foo2 2>&1 | FileCheck -check-prefix WRAP %s
WRAP: -wrap:foo1 -wrap:foo2

RUN: ld.lld -### -m i386pep foo.o -exclude-symbols sym1,sym2 --exclude-symbols sym3 2>&1 | FileCheck -check-prefix EXCLUDE_SYMBOLS %s
RUN: ld.lld -### -m i386pep foo.o -exclude-symbols=sym1,sym2 --exclude-symbols=sym3 2>&1 | FileCheck -check-prefix EXCLUDE_SYMBOLS %s
EXCLUDE_SYMBOLS: -exclude-symbols:sym1,sym2 -exclude-symbols:sym3

RUN: ld.lld -### -m i386pep foo.o 2>&1 | FileCheck -check-prefix DEMANGLE %s
RUN: ld.lld -### -m i386pep foo.o -demangle 2>&1 | FileCheck -check-prefix DEMANGLE %s
RUN: ld.lld -### -m i386pep foo.o --demangle 2>&1 | FileCheck -check-prefix DEMANGLE %s
Expand Down

0 comments on commit d1da646

Please sign in to comment.