[Driver] Pass -X to ld for riscv*-{elf,freebsd,linux}
authorFangrui Song <i@maskray.me>
Fri, 17 Jun 2022 06:33:48 +0000 (23:33 -0700)
committerFangrui Song <i@maskray.me>
Fri, 17 Jun 2022 06:33:48 +0000 (23:33 -0700)
commitc324c938becdbb963b34895cc12ffccb261bf167
tree67b2305c4c681153f2ca5bdbd3fca8d75b540c33
parent0d2dde20be14e42a26280fd80b78f09edf7befe6
[Driver] Pass -X to ld for riscv*-{elf,freebsd,linux}

GNU ld has a hack that defaults to -X (--discard-locals) in the emulation file
`riscvelf.em`. The recommended way, as gcc/config/arm does, is to let the
compiler driver pass -X to ld.
(The motivation is likely to discard a plethora of `.L` symbols due to linker
relaxation.)

lld default to --discard-none. To make clang+lld match GNU ld's behavior, pass
-X to ld.

Note: GNU ld has a special rule to treat ld -r -s as ld -r -S -x. With -X, driver `-r -Wl,-s`
will behave as ld `-r -S -X`. This removes fewer symbols than `-r -S -x` but is safe.

Differential Revision: https://reviews.llvm.org/D127826
clang/lib/Driver/ToolChains/FreeBSD.cpp
clang/lib/Driver/ToolChains/Gnu.cpp
clang/lib/Driver/ToolChains/RISCVToolchain.cpp
clang/test/Driver/riscv32-toolchain.c
clang/test/Driver/riscv64-toolchain.c