[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

(cherry picked from commit d1da6469f9ea9b078276ee2e098241f0440468be)
This commit is contained in:
Martin Storsjö 2022-07-18 23:43:02 +03:00 committed by Tobias Hieta
parent 89863763f9
commit 871a78eeae
9 changed files with 44 additions and 0 deletions

View file

@ -1312,6 +1312,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))

View file

@ -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())

View file

@ -28,6 +28,7 @@ public:
AutoExporter();
void addWholeArchive(StringRef path);
void addExcludedSymbol(StringRef symbol);
llvm::StringSet<> excludeSymbols;
llvm::StringSet<> excludeSymbolPrefixes;

View file

@ -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", "">;

View file

@ -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)) {

View file

@ -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",

View file

@ -87,6 +87,8 @@ MinGW Improvements
* The ``--disable-reloc-section`` option is now supported.
(`D127478 <https://reviews.llvm.org/D127478>`_)
* The ``--exclude-symbols`` option is now supported.
(`D130118 <https://reviews.llvm.org/D130118>`_)
MachO Improvements
------------------

View file

@ -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

View file

@ -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