Skip to content

Commit

Permalink
[Driver] Make ELF -nopie specific to OpenBSD (#72578)
Browse files Browse the repository at this point in the history
-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
  • Loading branch information
MaskRay committed Nov 21, 2023
1 parent ec1086f commit cac82e2
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 19 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -5158,7 +5158,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">;
Expand Down
10 changes: 3 additions & 7 deletions clang/lib/Driver/ToolChains/Gnu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/OpenBSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
const bool Shared = Args.hasArg(options::OPT_shared);
const bool Profiling = Args.hasArg(options::OPT_pg);
const bool Pie = Args.hasArg(options::OPT_pie);
const bool Nopie = Args.hasArg(options::OPT_nopie);
const bool Nopie = Args.hasArg(options::OPT_no_pie, options::OPT_nopie);
const bool Relocatable = Args.hasArg(options::OPT_r);
ArgStringList CmdArgs;

Expand Down
7 changes: 2 additions & 5 deletions clang/lib/Driver/ToolChains/Solaris.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 0 additions & 2 deletions clang/test/Driver/android-pie.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions clang/test/Driver/pic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit cac82e2

Please sign in to comment.