diff options
author | Fangrui Song <i@maskray.me> | 2023-11-16 13:48:04 -0800 |
---|---|---|
committer | Fangrui Song <i@maskray.me> | 2023-11-16 13:48:04 -0800 |
commit | ae623d16d50c9f12de7ae7ac1aa11c9d6857e081 (patch) | |
tree | 3e2eed81fa73632c2f820c1e150b54b7e040752a /clang | |
parent | ac3779e92ef9405fd2c602a08e8031f7b8aeedd8 (diff) | |
download | llvm-ae623d16d50c9f12de7ae7ac1aa11c9d6857e081.zip llvm-ae623d16d50c9f12de7ae7ac1aa11c9d6857e081.tar.gz llvm-ae623d16d50c9f12de7ae7ac1aa11c9d6857e081.tar.bz2 |
[Driver,Gnu] Simplify -static -static-pie -shared -pie handling and suppress -shared -rdynamic warning
These options select different link modes (note: -shared -static can be
used together for musl and mingw). It makes sense to place them
together, which enables some simplification. The relevant ld options
are now consistently placed after -m, similar to GCC.
While here, suppress -Wunused-command-line-argument warning when -shared
-rdynamic are used together (introduced by commit
291f4a00232b5742940d67e2ecf9168631251317). It can be argued either way
whether the warning is justified (in ELF linkers --export-dynamic
functionality is subsumed by -shared), but it is not useful (users can
do -Wl,--export-dynamic, bypassing the driver diagnostic).
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/Driver/ToolChains/Gnu.cpp | 55 | ||||
-rw-r--r-- | clang/test/Driver/dynamic-linker.c | 2 | ||||
-rw-r--r-- | clang/test/Driver/linux-ld.c | 18 | ||||
-rw-r--r-- | clang/test/Driver/ohos.c | 2 |
4 files changed, 27 insertions, 50 deletions
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index 7698648..ba95ce9 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -292,18 +292,6 @@ static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) { } } -static bool getPIE(const ArgList &Args, const ToolChain &TC) { - if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_static) || - Args.hasArg(options::OPT_r) || Args.hasArg(options::OPT_static_pie)) - return false; - - Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie, - options::OPT_nopie); - if (!A) - return TC.isPIEDefault(Args); - return A->getOption().matches(options::OPT_pie); -} - static bool getStaticPIE(const ArgList &Args, const ToolChain &TC) { bool HasStaticPIE = Args.hasArg(options::OPT_static_pie); // -no-pie is an alias for -nopie. So, handling -nopie takes care of @@ -386,7 +374,6 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, const bool isAndroid = ToolChain.getTriple().isAndroid(); const bool IsIAMCU = ToolChain.getTriple().isOSIAMCU(); const bool IsVE = ToolChain.getTriple().isVE(); - const bool IsPIE = getPIE(Args, ToolChain); const bool IsStaticPIE = getStaticPIE(Args, ToolChain); const bool IsStatic = getStatic(Args); const bool HasCRTBeginEndFiles = @@ -406,17 +393,6 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, if (!D.SysRoot.empty()) CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); - if (IsPIE) - CmdArgs.push_back("-pie"); - - if (IsStaticPIE) { - CmdArgs.push_back("-static"); - CmdArgs.push_back("-pie"); - CmdArgs.push_back("--no-dynamic-linker"); - CmdArgs.push_back("-z"); - CmdArgs.push_back("text"); - } - if (Args.hasArg(options::OPT_s)) CmdArgs.push_back("-s"); @@ -451,19 +427,32 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, if (Triple.isRISCV()) CmdArgs.push_back("-X"); - if (Args.hasArg(options::OPT_shared)) + const bool IsShared = Args.hasArg(options::OPT_shared); + if (IsShared) CmdArgs.push_back("-shared"); - - if (IsStatic) { + bool IsPIE = false; + if (IsStaticPIE) { + CmdArgs.push_back("-static"); + CmdArgs.push_back("-pie"); + CmdArgs.push_back("--no-dynamic-linker"); + CmdArgs.push_back("-z"); + CmdArgs.push_back("text"); + } else if (IsStatic) { CmdArgs.push_back("-static"); - } else if (!Args.hasArg(options::OPT_r) && - !Args.hasArg(options::OPT_shared) && !IsStaticPIE) { + } else if (!Args.hasArg(options::OPT_r)) { if (Args.hasArg(options::OPT_rdynamic)) CmdArgs.push_back("-export-dynamic"); - - CmdArgs.push_back("-dynamic-linker"); - CmdArgs.push_back(Args.MakeArgString(Twine(D.DyldPrefix) + - ToolChain.getDynamicLinker(Args))); + if (!IsShared) { + Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie, + options::OPT_nopie); + IsPIE = A ? A->getOption().matches(options::OPT_pie) + : ToolChain.isPIEDefault(Args); + if (IsPIE) + CmdArgs.push_back("-pie"); + CmdArgs.push_back("-dynamic-linker"); + CmdArgs.push_back(Args.MakeArgString(Twine(D.DyldPrefix) + + ToolChain.getDynamicLinker(Args))); + } } CmdArgs.push_back("-o"); diff --git a/clang/test/Driver/dynamic-linker.c b/clang/test/Driver/dynamic-linker.c index 555e46ab..978907e 100644 --- a/clang/test/Driver/dynamic-linker.c +++ b/clang/test/Driver/dynamic-linker.c @@ -11,7 +11,7 @@ // RUN: %clang -target x86_64-unknown-linux-gnu -### -shared /dev/null -o /dev/null 2>&1 | FileCheck -check-prefix CHECK-SHARED %s -// RUN: %clang -target armv7-unknown-linux-gnueabi -### -shared -rdynamic /dev/null -o /dev/null 2>&1 | FileCheck -check-prefix CHECK-SHARED %s +// RUN: %clang --target=armv7-unknown-linux-gnueabi -### -Werror -shared -rdynamic /dev/null -o /dev/null 2>&1 | FileCheck -check-prefix CHECK-SHARED %s // RUN: %clang -target i386-unknown-linux-gnu -### -shared -rdynamic /dev/null -o /dev/null 2>&1 | FileCheck -check-prefix CHECK-SHARED %s // RUN: %clang -target mips64-unknown-linux-gnu -### -shared -rdynamic /dev/null -o /dev/null 2>&1 | FileCheck -check-prefix CHECK-SHARED %s // RUN: %clang -target powerpc64-unknown-linux-gnu -### -shared -rdynamic /dev/null -o /dev/null 2>&1 | FileCheck -check-prefix CHECK-SHARED %s diff --git a/clang/test/Driver/linux-ld.c b/clang/test/Driver/linux-ld.c index 1e36665..7adb078 100644 --- a/clang/test/Driver/linux-ld.c +++ b/clang/test/Driver/linux-ld.c @@ -182,12 +182,8 @@ // RUN: --sysroot=%S/Inputs/basic_linux_tree \ // RUN: | FileCheck --check-prefix=CHECK-CLANG-LD-STATIC-PIE %s // CHECK-CLANG-LD-STATIC-PIE: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]" -// CHECK-CLANG-LD-STATIC-PIE: "-static" -// CHECK-CLANG-LD-STATIC-PIE: "-pie" -// CHECK-CLANG-LD-STATIC-PIE: "--no-dynamic-linker" -// CHECK-CLANG-LD-STATIC-PIE: "-z" -// CHECK-CLANG-LD-STATIC-PIE: "text" // CHECK-CLANG-LD-STATIC-PIE: "-m" "elf_x86_64" +// CHECK-CLANG-LD-STATIC-PIE-SAME: "-static" "-pie" "--no-dynamic-linker" "-z" "text" // CHECK-CLANG-LD-STATIC-PIE: "{{.*}}rcrt1.o" // CHECK-CLANG-LD-STATIC-PIE: "--start-group" "-lgcc" "-lgcc_eh" "-lc" "--end-group" // @@ -197,12 +193,8 @@ // RUN: --sysroot=%S/Inputs/basic_linux_tree \ // RUN: | FileCheck --check-prefix=CHECK-CLANG-LD-STATIC-PIE-PIE %s // CHECK-CLANG-LD-STATIC-PIE-PIE: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]" -// CHECK-CLANG-LD-STATIC-PIE-PIE: "-static" -// CHECK-CLANG-LD-STATIC-PIE-PIE: "-pie" -// CHECK-CLANG-LD-STATIC-PIE-PIE: "--no-dynamic-linker" -// CHECK-CLANG-LD-STATIC-PIE-PIE: "-z" -// CHECK-CLANG-LD-STATIC-PIE-PIE: "text" // CHECK-CLANG-LD-STATIC-PIE-PIE: "-m" "elf_x86_64" +// CHECK-CLANG-LD-STATIC-PIE-PIE-SAME: "-static" "-pie" "--no-dynamic-linker" "-z" "text" // CHECK-CLANG-LD-STATIC-PIE-PIE: "{{.*}}rcrt1.o" // CHECK-CLANG-LD-STATIC-PIE-PIE: "--start-group" "-lgcc" "-lgcc_eh" "-lc" "--end-group" // @@ -212,12 +204,8 @@ // RUN: --sysroot=%S/Inputs/basic_linux_tree \ // RUN: | FileCheck --check-prefix=CHECK-CLANG-LD-STATIC-PIE-STATIC %s // CHECK-CLANG-LD-STATIC-PIE-STATIC: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]" -// CHECK-CLANG-LD-STATIC-PIE-STATIC: "-static" -// CHECK-CLANG-LD-STATIC-PIE-STATIC: "-pie" -// CHECK-CLANG-LD-STATIC-PIE-STATIC: "--no-dynamic-linker" -// CHECK-CLANG-LD-STATIC-PIE-STATIC: "-z" -// CHECK-CLANG-LD-STATIC-PIE-STATIC: "text" // CHECK-CLANG-LD-STATIC-PIE-STATIC: "-m" "elf_x86_64" +// CHECK-CLANG-LD-STATIC-PIE-STATIC-SAME: "-static" "-pie" "--no-dynamic-linker" "-z" "text" // CHECK-CLANG-LD-STATIC-PIE-STATIC: "{{.*}}rcrt1.o" // CHECK-CLANG-LD-STATIC-PIE-STATIC: "--start-group" "-lgcc" "-lgcc_eh" "-lc" "--end-group" // diff --git a/clang/test/Driver/ohos.c b/clang/test/Driver/ohos.c index 9b98c70..dfb7981 100644 --- a/clang/test/Driver/ohos.c +++ b/clang/test/Driver/ohos.c @@ -17,9 +17,9 @@ // CHECK-NOT: "-fno-common" // CHECK: {{.*}}ld.lld{{.*}}" "--sysroot=[[SYSROOT]]" // CHECK-NOT: "--sysroot=[[SYSROOT]]" -// CHECK: "-pie" // CHECK-NOT: "--build-id" // CHECK: "--hash-style=both" +// CHECK: "-pie" // CHECK: "-dynamic-linker" "/lib/ld-musl-arm.so.1" // CHECK: Scrt1.o // CHECK: crti.o |