diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -216,6 +216,7 @@ bool emitLLVM; bool emitRelocs; bool enableNewDtags; + llvm::SmallVector excludeInputs; bool executeOnly; bool exportDynamic; bool fixCortexA53Errata843419; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -230,6 +230,12 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) { using namespace sys::fs; + for (const llvm::GlobPattern &pat : config->excludeInputs) + if (pat.match(path)) { + log("excluding input " + Twine(path)); + return; + } + std::optional buffer = readFile(path); if (!buffer) return; @@ -1563,6 +1569,14 @@ } else { error(Twine("cannot find version script ") + arg->getValue()); } + + for (opt::Arg *arg : args.filtered(OPT_exclude_inputs)) { + StringRef pattern(arg->getValue()); + if (Expected pat = GlobPattern::create(pattern)) + config->excludeInputs.push_back(std::move(*pat)); + else + error(arg->getSpelling() + ": " + toString(pat.takeError())); + } } // Some Config members do not directly correspond to any particular diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -184,6 +184,9 @@ defm error_handling_script: EEq<"error-handling-script", "Specify an error handling script">; +defm exclude_inputs: EEq<"exclude-inputs", "Do not link inputs matching given pattern">, + MetaVarName<"">; + defm exclude_libs: Eq<"exclude-libs", "Exclude static libraries from automatic export">; defm execute_only: BB<"execute-only", diff --git a/lld/docs/ld.lld.1 b/lld/docs/ld.lld.1 --- a/lld/docs/ld.lld.1 +++ b/lld/docs/ld.lld.1 @@ -218,6 +218,8 @@ .It Fl -execute-only Mark executable sections unreadable. This option is currently only supported on AArch64. +.It Fl -exclude-inputs Ns = Ns Ar pattern +Do not link inputs matching given pattern. .It Fl -exclude-libs Ns = Ns Ar value Exclude static libraries from automatic export. .It Fl -export-dynamic , Fl E diff --git a/lld/test/ELF/exclude-inputs.s b/lld/test/ELF/exclude-inputs.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/exclude-inputs.s @@ -0,0 +1,26 @@ +// REQUIRES: x86 + +// RUN: rm -rf %t && mkdir %t && cd %t +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o foo.o +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux --defsym BAR=1 %s -o bar.o +// RUN: llvm-ar rcs bar.a bar.o + +// RUN: ld.lld --exclude-inputs='*foo*' --whole-archive foo.o bar.a -o exe +// RUN: llvm-readelf -s exe | FileCheck --check-prefix=OBJECT %s +// OBJECT: bar +// OBJECT-NOT: foo + +// RUN: ld.lld --exclude-inputs='*bar*' --whole-archive foo.o bar.a -o exe +// RUN: llvm-readelf -s exe | FileCheck --check-prefix=ARCHIVE %s +// ARCHIVE: foo +// ARCHIVE-NOT: bar + +.ifdef BAR +.globl bar +bar: + nop +.else +.globl foo +foo: + nop +.endif