Skip to content

Commit cac82e2

Browse files
authored
[Driver] Make ELF -nopie specific to OpenBSD (llvm#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 use -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
1 parent ec1086f commit cac82e2

File tree

6 files changed

+10
-19
lines changed

6 files changed

+10
-19
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5158,7 +5158,7 @@ def nofixprebinding : Flag<["-"], "nofixprebinding">;
51585158
def nolibc : Flag<["-"], "nolibc">;
51595159
def nomultidefs : Flag<["-"], "nomultidefs">;
51605160
def nopie : Flag<["-"], "nopie">, Visibility<[ClangOption, FlangOption]>;
5161-
def no_pie : Flag<["-"], "no-pie">, Visibility<[ClangOption, FlangOption]>, Alias<nopie>;
5161+
def no_pie : Flag<["-"], "no-pie">, Visibility<[ClangOption, FlangOption]>;
51625162
def noprebind : Flag<["-"], "noprebind">;
51635163
def noprofilelib : Flag<["-"], "noprofilelib">;
51645164
def noseglinkedit : Flag<["-"], "noseglinkedit">;

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,7 @@ static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) {
294294

295295
static bool getStaticPIE(const ArgList &Args, const ToolChain &TC) {
296296
bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
297-
// -no-pie is an alias for -nopie. So, handling -nopie takes care of
298-
// -no-pie as well.
299-
if (HasStaticPIE && Args.hasArg(options::OPT_nopie)) {
297+
if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
300298
const Driver &D = TC.getDriver();
301299
const llvm::opt::OptTable &Opts = D.getOpts();
302300
StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
@@ -443,10 +441,8 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
443441
if (Args.hasArg(options::OPT_rdynamic))
444442
CmdArgs.push_back("-export-dynamic");
445443
if (!IsShared) {
446-
Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie,
447-
options::OPT_nopie);
448-
IsPIE = A ? A->getOption().matches(options::OPT_pie)
449-
: ToolChain.isPIEDefault(Args);
444+
IsPIE = Args.hasFlag(options::OPT_pie, options::OPT_no_pie,
445+
ToolChain.isPIEDefault(Args));
450446
if (IsPIE)
451447
CmdArgs.push_back("-pie");
452448
CmdArgs.push_back("-dynamic-linker");

clang/lib/Driver/ToolChains/OpenBSD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
116116
const bool Shared = Args.hasArg(options::OPT_shared);
117117
const bool Profiling = Args.hasArg(options::OPT_pg);
118118
const bool Pie = Args.hasArg(options::OPT_pie);
119-
const bool Nopie = Args.hasArg(options::OPT_nopie);
119+
const bool Nopie = Args.hasArg(options::OPT_no_pie, options::OPT_nopie);
120120
const bool Relocatable = Args.hasArg(options::OPT_r);
121121
ArgStringList CmdArgs;
122122

clang/lib/Driver/ToolChains/Solaris.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,8 @@ static bool getPIE(const ArgList &Args, const ToolChain &TC) {
4949
Args.hasArg(options::OPT_r))
5050
return false;
5151

52-
Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie,
53-
options::OPT_nopie);
54-
if (!A)
55-
return TC.isPIEDefault(Args);
56-
return A->getOption().matches(options::OPT_pie);
52+
return Args.hasFlag(options::OPT_pie, options::OPT_no_pie,
53+
TC.isPIEDefault(Args));
5754
}
5855

5956
// FIXME: Need to handle CLANG_DEFAULT_LINKER here?

clang/test/Driver/android-pie.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636

3737
// RUN: %clang %s -### -o %t.o 2>&1 -no-pie --target=arm-linux-androideabi24 \
3838
// RUN: | FileCheck --check-prefix=NO-PIE %s
39-
// RUN: %clang %s -### -o %t.o 2>&1 -nopie --target=arm-linux-androideabi24 \
40-
// RUN: | FileCheck --check-prefix=NO-PIE %s
4139
// RUN: %clang %s -### -o %t.o 2>&1 -pie -no-pie --target=arm-linux-androideabi24 \
4240
// RUN: | FileCheck --check-prefix=NO-PIE %s
4341

clang/test/Driver/pic.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@
166166
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
167167
// RUN: %clang -c %s -target armv7-linux-musleabihf -### 2>&1 \
168168
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
169-
// RUN: %clang %s -target x86_64-linux-musl -nopie -### 2>&1 \
169+
// RUN: %clang %s --target=x86_64-linux-musl -no-pie -### 2>&1 \
170170
// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIE
171-
// RUN: %clang %s -target x86_64-linux-musl -pie -nopie -### 2>&1 \
171+
// RUN: %clang %s --target=x86_64-linux-musl -pie -no-pie -### 2>&1 \
172172
// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIE
173-
// RUN: %clang %s -target x86_64-linux-musl -nopie -pie -### 2>&1 \
173+
// RUN: %clang %s --target=x86_64-linux-musl -no-pie -pie -### 2>&1 \
174174
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
175175
//
176176
// Darwin is a beautiful and unique snowflake when it comes to these flags.

0 commit comments

Comments
 (0)