Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -827,6 +827,12 @@ } void LinkerDriver::createFiles(opt::InputArgList &Args) { + std::vector> State = { + {&Config->AsNeeded, Config->AsNeeded}, + {&Config->Static, Config->Static}, + {&InBinary, InBinary}, + {&InWholeArchive, InWholeArchive}}; + for (auto *Arg : Args) { switch (Arg->getOption().getUnaliasedOption().getID()) { case OPT_library: @@ -866,6 +872,14 @@ case OPT_end_lib: InLib = false; break; + case OPT_pop_state: + for (std::pair &P : State) + *P.first = P.second; + break; + case OPT_push_state: + for (std::pair &P : State) + P.second = *P.first; + break; } } Index: ELF/Options.td =================================================================== --- ELF/Options.td +++ ELF/Options.td @@ -222,12 +222,18 @@ def pie: F<"pie">, HelpText<"Create a position independent executable">; +def pop_state: Flag<["--"], "pop-state">, + HelpText<"Restore position dependent options">; + def print_gc_sections: F<"print-gc-sections">, HelpText<"List removed unused sections">; def print_map: F<"print-map">, HelpText<"Print a link map to the standard output">; +def push_state: Flag<["--"], "push-state">, + HelpText<"Preserve position dependent options">; + defm reproduce: Eq<"reproduce">, HelpText<"Dump linker invocation and input files for debugging">; Index: test/ELF/as-needed.s =================================================================== --- test/ELF/as-needed.s +++ test/ELF/as-needed.s @@ -18,6 +18,9 @@ // RUN: ld.lld --as-needed %t.o %t2.so --no-as-needed %t3.so %t4.so -o %t2 // RUN: llvm-readobj -dynamic-table %t2 | FileCheck %s +// RUN: ld.lld --push-state --as-needed %t.o %t2.so --pop-state %t3.so %t4.so -o %t2 +// RUN: llvm-readobj -dynamic-table %t2 | FileCheck %s + /// GROUP directive is the same as --as-needed. // RUN: echo "GROUP(\"%t2.so\" \"%t3.so\" \"%t4.so\")" > %t.script Index: test/ELF/format-binary.test =================================================================== --- test/ELF/format-binary.test +++ test/ELF/format-binary.test @@ -7,6 +7,9 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o # RUN: ld.lld %t.o -b binary %t.binary -b default %t.o -shared -o %t.out +## Check --push-state/--pop-state works with -b. +# RUN: ld.lld %t.o --push-state -b binary %t.binary --pop-state %t.o -shared -o %t.out + # RUN: not ld.lld -b foo > %t.log 2>&1 # RUN: FileCheck -check-prefix=ERR %s < %t.log # ERR: error: unknown -format value: foo (supported formats: elf, default, binary) Index: test/ELF/libsearch.s =================================================================== --- test/ELF/libsearch.s +++ test/ELF/libsearch.s @@ -69,6 +69,12 @@ // RUN: | FileCheck --check-prefix=NOLIB2 %s // NOLIB2: unable to find library -lls2 +// Check --pop-state/--push-state work for -Bstatic/-Bdynamic. +// RUN: ld.lld -Bstatic --push-state -Bdynamic -o %t3 %t.o -L%t.dir --pop-state -lls +// RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=STATIC %s +// RUN: not ld.lld -o %t3 %t.o -L%t.dir -Bstatic -lls2 2>&1 \ +// RUN: | FileCheck --check-prefix=NOLIB2 %s + // -Bdynamic should restore default behaviour // RUN: ld.lld -o %t3 %t.o -L%t.dir -Bstatic -Bdynamic -lls // RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=DYNAMIC %s Index: test/ELF/whole-archive.s =================================================================== --- test/ELF/whole-archive.s +++ test/ELF/whole-archive.s @@ -24,6 +24,10 @@ // RUN: ld.lld -o %t3 %t.o --whole-archive --no-whole-archive %t.a // RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=NOTADDED %s +// Check --push-state/--pop-state works for --whole-archive. +// RUN: ld.lld -o %t3 %t.o --push-state --whole-archive --pop-state %t.a +// RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=NOTADDED %s + // --whole-archive and --no-whole-archive should affect only archives which follow them // RUN: ld.lld -o %t3 %t.o %t.a --whole-archive --no-whole-archive // RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=NOTADDED %s