[clang] [Driver] Make ELF -nopie specific to OpenBSD (PR #72578)
Fangrui Song via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 16 14:23:25 PST 2023
https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/72578
-no-pie[1]/-nopie is rarely used and among the rare uses almost
everwhere uses -no-pie, since GCC does not recognize -nopie.
However, OpenBSD seems to prefer -nopie. Therefore, make -nopie specific
to OpenBSD to prevent newer ToolChains (Solaris, SerenityOS) from cargo
culting and copying -nopie.
[1]: https://reviews.llvm.org/D35462
>From 2135d365c1820a23826d16293cc59a65b0ae847e Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 16 Nov 2023 14:09:09 -0800
Subject: [PATCH] [Driver] Make ELF -nopie specific to OpenBSD
-no-pie[1]/-nopie is rarely used and among the rare uses almost
everwhere uses -no-pie, since GCC does not recognize -nopie.
However, OpenBSD seems to prefer -nopie. Therefore, make -nopie specific
to OpenBSD to prevent newer ToolChains (Solaris, SerenityOS) from cargo
culting and copying -nopie.
[1]: https://reviews.llvm.org/D35462
---
clang/include/clang/Driver/Options.td | 2 +-
clang/lib/Driver/ToolChains/Gnu.cpp | 10 +++-------
clang/lib/Driver/ToolChains/OpenBSD.cpp | 2 +-
clang/lib/Driver/ToolChains/Solaris.cpp | 7 ++-----
clang/test/Driver/android-pie.c | 2 --
clang/test/Driver/pic.c | 6 +++---
6 files changed, 10 insertions(+), 19 deletions(-)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 811550416110b3d..654bf8be78d6259 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5137,7 +5137,7 @@ def nofixprebinding : Flag<["-"], "nofixprebinding">;
def nolibc : Flag<["-"], "nolibc">;
def nomultidefs : Flag<["-"], "nomultidefs">;
def nopie : Flag<["-"], "nopie">, Visibility<[ClangOption, FlangOption]>;
-def no_pie : Flag<["-"], "no-pie">, Visibility<[ClangOption, FlangOption]>, Alias<nopie>;
+def no_pie : Flag<["-"], "no-pie">, Visibility<[ClangOption, FlangOption]>;
def noprebind : Flag<["-"], "noprebind">;
def noprofilelib : Flag<["-"], "noprofilelib">;
def noseglinkedit : Flag<["-"], "noseglinkedit">;
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index ba95ce9c5a28153..2b1e8f02cf66388 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -294,9 +294,7 @@ static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) {
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
- // -no-pie as well.
- if (HasStaticPIE && Args.hasArg(options::OPT_nopie)) {
+ if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
const Driver &D = TC.getDriver();
const llvm::opt::OptTable &Opts = D.getOpts();
StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
@@ -443,10 +441,8 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
if (Args.hasArg(options::OPT_rdynamic))
CmdArgs.push_back("-export-dynamic");
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);
+ IsPIE = Args.hasFlag(options::OPT_pie, options::OPT_no_pie,
+ ToolChain.isPIEDefault(Args));
if (IsPIE)
CmdArgs.push_back("-pie");
CmdArgs.push_back("-dynamic-linker");
diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index 5d06cd8ab0bad16..e883838e2dc9317 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -117,7 +117,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
bool Shared = Args.hasArg(options::OPT_shared);
bool Profiling = Args.hasArg(options::OPT_pg);
bool Pie = Args.hasArg(options::OPT_pie);
- bool Nopie = Args.hasArg(options::OPT_nopie);
+ bool Nopie = Args.hasArg(options::OPT_no_pie, options::OPT_nopie);
const bool Relocatable = Args.hasArg(options::OPT_r);
// Silence warning for "clang -g foo.o -o foo"
diff --git a/clang/lib/Driver/ToolChains/Solaris.cpp b/clang/lib/Driver/ToolChains/Solaris.cpp
index 96757f07a54974f..485730da7df1f8c 100644
--- a/clang/lib/Driver/ToolChains/Solaris.cpp
+++ b/clang/lib/Driver/ToolChains/Solaris.cpp
@@ -49,11 +49,8 @@ static bool getPIE(const ArgList &Args, const ToolChain &TC) {
Args.hasArg(options::OPT_r))
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);
+ return Args.hasFlag(options::OPT_pie, options::OPT_no_pie,
+ TC.isPIEDefault(Args));
}
// FIXME: Need to handle CLANG_DEFAULT_LINKER here?
diff --git a/clang/test/Driver/android-pie.c b/clang/test/Driver/android-pie.c
index 8620e185654586c..01720edf98fb170 100644
--- a/clang/test/Driver/android-pie.c
+++ b/clang/test/Driver/android-pie.c
@@ -36,8 +36,6 @@
// RUN: %clang %s -### -o %t.o 2>&1 -no-pie --target=arm-linux-androideabi24 \
// RUN: | FileCheck --check-prefix=NO-PIE %s
-// RUN: %clang %s -### -o %t.o 2>&1 -nopie --target=arm-linux-androideabi24 \
-// RUN: | FileCheck --check-prefix=NO-PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 -pie -no-pie --target=arm-linux-androideabi24 \
// RUN: | FileCheck --check-prefix=NO-PIE %s
diff --git a/clang/test/Driver/pic.c b/clang/test/Driver/pic.c
index 86b2a3b041d5f92..aeddead6dbf8f4c 100644
--- a/clang/test/Driver/pic.c
+++ b/clang/test/Driver/pic.c
@@ -166,11 +166,11 @@
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
// RUN: %clang -c %s -target armv7-linux-musleabihf -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
-// RUN: %clang %s -target x86_64-linux-musl -nopie -### 2>&1 \
+// RUN: %clang %s --target=x86_64-linux-musl -no-pie -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIE
-// RUN: %clang %s -target x86_64-linux-musl -pie -nopie -### 2>&1 \
+// RUN: %clang %s --target=x86_64-linux-musl -pie -no-pie -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIE
-// RUN: %clang %s -target x86_64-linux-musl -nopie -pie -### 2>&1 \
+// RUN: %clang %s --target=x86_64-linux-musl -no-pie -pie -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
//
// Darwin is a beautiful and unique snowflake when it comes to these flags.
More information about the cfe-commits
mailing list