[ELF] Add --export-dynamic-symbol-list

This is available in GNU ld 2.35 and can be seen as a shortcut for multiple
--export-dynamic-symbol, or a --dynamic-list variant without the symbolic intention.

In the long term, this option probably should be preferred over --dynamic-list.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D107317
This commit is contained in:
Fangrui Song 2021-08-03 09:01:03 -07:00
parent ccf1038a92
commit 44361e5b90
5 changed files with 50 additions and 20 deletions

View File

@ -1379,7 +1379,8 @@ static void readConfigs(opt::InputArgList &args) {
// symbols not specified by --dynamic-list are non-preemptible.
config->symbolic =
config->bsymbolic == BsymbolicKind::All || args.hasArg(OPT_dynamic_list);
for (auto *arg : args.filtered(OPT_dynamic_list))
for (auto *arg :
args.filtered(OPT_dynamic_list, OPT_export_dynamic_symbol_list))
if (Optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
readDynamicList(*buffer);

View File

@ -156,10 +156,8 @@ def discard_none: F<"discard-none">,
defm dynamic_linker: Eq<"dynamic-linker", "Which dynamic linker to use">;
defm dynamic_list : Eq<"dynamic-list",
"Read a list of dynamic symbols. (executable) Put matched non-local defined"
"symbols to the dynamic symbol table. (shared object) References to matched"
"non-local STV_DEFAULT symbols shouldn't be bound to definitions within the "
"shared object. Implies -Bsymbolic but does not set DF_SYMBOLIC">,
"Similar to --export-dynamic-symbol-list. When creating a shared object, "
"this additionally implies -Bsymbolic but does not set DF_SYMBOLIC">,
MetaVarName<"<file>">;
def eb: F<"EB">, HelpText<"Select the big-endian format in OUTPUT_FORMAT">;
@ -209,6 +207,10 @@ defm export_dynamic_symbol : EEq<"export-dynamic-symbol",
"Does not imply -Bsymbolic.">,
MetaVarName<"glob">;
defm export_dynamic_symbol_list : EEq<"export-dynamic-symbol-list",
"Read a list of dynamic symbol patterns. Apply --export-dynamic-symbol on each pattern">,
MetaVarName<"file">;
defm fatal_warnings: B<"fatal-warnings",
"Treat warnings as errors",
"Do not treat warnings as errors (default)">;

View File

@ -24,8 +24,8 @@ Non-comprehensive list of changes in this release
ELF Improvements
----------------
* ``-Bsymbolic-non-weak-functions`` has been added as a ``STB_GLOBAL`` subset of ``-Bsymbolic-functions``.
(`D102570 <https://reviews.llvm.org/D102570>`_)
* ``--export-dynamic-symbol-list`` has been added.
(`D107317 <https://reviews.llvm.org/D107317>`_)
Breaking changes
----------------

View File

@ -167,10 +167,9 @@ Specify the dynamic linker to be used for a dynamically linked executable.
This is recorded in an ELF segment of type
.Dv PT_INTERP .
.It Fl -dynamic-list Ns = Ns Ar file
Read a list of dynamic symbols from
.Ar file .
(executable) Put matched non-local defined symbols to the dynamic symbol table.
(shared object) References to matched non-local STV_DEFAULT symbols shouldn't be bound to definitions within the shared object. Implies
Similar to
.Cm --export-dynamic-symbol-list .
When creating a shared object, implies
.Cm -Bsymbolic
but does not set DF_SYMBOLIC
.It Fl -EB
@ -225,6 +224,12 @@ Put symbols in the dynamic symbol table.
.Cm -Bsymbolic-functions
or
.Cm --dynamic-list
.It Fl -export-dynamic-symbol-list Ns = Ns Ar file
Read a list of dynamic symbol patterns from
.Ar file .
Apply
.Cm --export-dynamic-symbol
on each pattern.
.It Fl -fatal-warnings
Treat warnings as errors.
.It Fl -filter Ns = Ns Ar value , Fl F Ar value

View File

@ -3,7 +3,11 @@
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
## For an executable, --export-dynamic-symbol exports a symbol if it is non-local and defined.
# RUN: ld.lld -pie --export-dynamic-symbol foo %t.o -o %t
# RUN: ld.lld -pie --export-dynamic-symbol foo --export-dynamic-symbol qux %t.o -o %t
# RUN: llvm-nm -D -p %t | FileCheck %s
# RUN: echo '{ foo; };' > %t1.list
# RUN: echo '{ foo; qux; };' > %t2.list
# RUN: ld.lld -pie --export-dynamic-symbol-list=%t2.list %t.o -o %t
# RUN: llvm-nm -D -p %t | FileCheck %s
## --export-dynamic exports all non-local defined symbols.
@ -14,6 +18,7 @@
# CHECK-NOT: .
# START: T _start
# CHECK: T foo
# CHECK: T qux
# CHECK-NOT: .
## --export-dynamic-symbol does not imply -u: %t1.a(%t1.o) is not fetched.
@ -23,36 +28,53 @@
# RUN: ld.lld --export-dynamic-symbol bar %t1.a %t.o -o %t.nofetch
# RUN: llvm-nm %t.nofetch | FileCheck /dev/null --implicit-check-not=bar
## For -shared, if no option expresses a symbolic intention, --export-dynamic-symbol is a no-op.
# RUN: ld.lld -shared --export-dynamic-symbol foo %t.o -o %t.noop
# RUN: llvm-objdump -d %t.noop | FileCheck --check-prefix=PLT2 %s
# RUN: ld.lld -shared --export-dynamic-symbol-list %t2.list %t.o -o %t.noop
# RUN: llvm-objdump -d %t.noop | FileCheck --check-prefix=PLT2 %s
## --export-dynamic-symbol can make a symbol preemptible even if it would be otherwise
## non-preemptible (due to -Bsymbolic, -Bsymbolic-functions or --dynamic-list).
# RUN: ld.lld -shared -Bsymbolic --export-dynamic-symbol nomatch %t.o -o %t.nopreempt
# RUN: llvm-objdump -d %t.nopreempt | FileCheck --check-prefix=NOPLT %s
# RUN: ld.lld -shared -Bsymbolic --export-dynamic-symbol foo %t.o -o %t.preempt
# RUN: llvm-objdump -d %t.preempt | FileCheck --check-prefix=PLT %s
# RUN: llvm-objdump -d %t.preempt | FileCheck --check-prefix=PLT1 %s
# RUN: ld.lld -shared -Bsymbolic --export-dynamic-symbol-list %t1.list %t.o -o %t.preempt
# RUN: llvm-objdump -d %t.preempt | FileCheck --check-prefix=PLT1 %s
## 'nomatch' does not match any symbol. Don't warn.
# RUN: ld.lld --fatal-warnings -shared -Bsymbolic-functions --export-dynamic-symbol nomatch %t.o -o %t.nopreempt2
# RUN: llvm-objdump -d %t.nopreempt2 | FileCheck --check-prefix=NOPLT %s
# RUN: ld.lld -shared -Bsymbolic-functions --export-dynamic-symbol foo %t.o -o %t.preempt2
# RUN: llvm-objdump -d %t.preempt2 | FileCheck --check-prefix=PLT %s
# RUN: llvm-objdump -d %t.preempt2 | FileCheck --check-prefix=PLT1 %s
# RUN: echo '{};' > %t.list
# RUN: ld.lld -shared --dynamic-list %t.list --export-dynamic-symbol foo %t.o -o %t.preempt3
# RUN: llvm-objdump -d %t.preempt3 | FileCheck --check-prefix=PLT %s
# RUN: llvm-objdump -d %t.preempt3 | FileCheck --check-prefix=PLT1 %s
## The option value is a glob.
# RUN: ld.lld -shared -Bsymbolic --export-dynamic-symbol 'f*' %t.o -o - | \
# RUN: llvm-objdump -d - | FileCheck --check-prefix=PLT %s
# RUN: llvm-objdump -d - | FileCheck --check-prefix=PLT1 %s
# RUN: ld.lld -shared -Bsymbolic --export-dynamic-symbol '[f]o[o]' %t.o -o - | \
# RUN: llvm-objdump -d - | FileCheck --check-prefix=PLT %s
# RUN: llvm-objdump -d - | FileCheck --check-prefix=PLT1 %s
# RUN: ld.lld -shared -Bsymbolic --export-dynamic-symbol 'f?o' %t.o -o - | \
# RUN: llvm-objdump -d - | FileCheck --check-prefix=PLT %s
# RUN: llvm-objdump -d - | FileCheck --check-prefix=PLT1 %s
# PLT1: <foo@plt>
# PLT1: <qux>
# PLT2: <foo@plt>
# PLT2: <qux@plt>
# PLT: <foo@plt>
# NOPLT-NOT: <foo@plt>
# NOPLT-NOT: <qux@plt>
.global _start, foo
.global _start, foo, qux
.type foo, @function
.type qux, @function
_start:
call foo
call qux
foo:
qux: