[llvm-branch-commits] [clang] ae623d1 - [Driver, Gnu] Simplify -static -static-pie -shared -pie handling and suppress -shared -rdynamic warning
Fangrui Song via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Nov 17 03:47:29 PST 2023
Author: Fangrui Song
Date: 2023-11-16T13:48:04-08:00
New Revision: ae623d16d50c9f12de7ae7ac1aa11c9d6857e081
URL: https://github.com/llvm/llvm-project/commit/ae623d16d50c9f12de7ae7ac1aa11c9d6857e081
DIFF: https://github.com/llvm/llvm-project/commit/ae623d16d50c9f12de7ae7ac1aa11c9d6857e081.diff
LOG: [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).
Added:
Modified:
clang/lib/Driver/ToolChains/Gnu.cpp
clang/test/Driver/dynamic-linker.c
clang/test/Driver/linux-ld.c
clang/test/Driver/ohos.c
Removed:
################################################################################
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 76986481686adc6..ba95ce9c5a28153 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 555e46aba5f069b..978907e0adee697 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 1e3666539de8c6b..7adb078f755dca2 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 9b98c709815c5e2..dfb7981525fd2a9 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
More information about the llvm-branch-commits
mailing list