Skip to content

Commit

Permalink
[Driver] Default Android toolchains to noexecstack.
Browse files Browse the repository at this point in the history
Android does not support executable stacks.

Reviewers: srhines, pirama

Reviewed By: pirama

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D53343

llvm-svn: 357197
  • Loading branch information
DanAlbert committed Mar 28, 2019
1 parent c25c9b4 commit 2715b28
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 3 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/ToolChain.h
Expand Up @@ -412,6 +412,9 @@ class ToolChain {
/// Test whether this toolchain defaults to PIE.
virtual bool isPIEDefault() const = 0;

/// Test whether this toolchaind defaults to non-executable stacks.
virtual bool isNoExecStackDefault() const;

/// Tests whether this toolchain forces its default for PIC, PIE or
/// non-PIC. If this returns true, any PIC related flags should be ignored
/// and instead the results of \c isPICDefault() and \c isPIEDefault() are
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Driver/ToolChain.cpp
Expand Up @@ -112,6 +112,10 @@ bool ToolChain::useRelaxRelocations() const {
return ENABLE_X86_RELAX_RELOCATIONS;
}

bool ToolChain::isNoExecStackDefault() const {
return false;
}

const SanitizerArgs& ToolChain::getSanitizerArgs() const {
if (!SanitizerArguments.get())
SanitizerArguments.reset(new SanitizerArgs(*this, Args));
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Driver/ToolChains/Clang.cpp
Expand Up @@ -2053,6 +2053,7 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
bool TakeNextArg = false;

bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
bool UseNoExecStack = C.getDefaultToolChain().isNoExecStackDefault();
const char *MipsTargetFeature = nullptr;
for (const Arg *A :
Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
Expand Down Expand Up @@ -2134,7 +2135,7 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
} else if (Value == "--fatal-warnings") {
CmdArgs.push_back("-massembler-fatal-warnings");
} else if (Value == "--noexecstack") {
CmdArgs.push_back("-mnoexecstack");
UseNoExecStack = true;
} else if (Value.startswith("-compress-debug-sections") ||
Value.startswith("--compress-debug-sections") ||
Value == "-nocompress-debug-sections" ||
Expand Down Expand Up @@ -2197,6 +2198,8 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
}
if (UseRelaxRelocations)
CmdArgs.push_back("--mrelax-relocations");
if (UseNoExecStack)
CmdArgs.push_back("-mnoexecstack");
if (MipsTargetFeature != nullptr) {
CmdArgs.push_back("-target-feature");
CmdArgs.push_back(MipsTargetFeature);
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Driver/ToolChains/Gnu.cpp
Expand Up @@ -360,6 +360,11 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("--no-dynamic-linker");
}

if (ToolChain.isNoExecStackDefault()) {
CmdArgs.push_back("-z");
CmdArgs.push_back("noexecstack");
}

if (Args.hasArg(options::OPT_rdynamic))
CmdArgs.push_back("-export-dynamic");

Expand Down Expand Up @@ -609,6 +614,10 @@ void tools::gnutools::Assembler::ConstructJob(Compilation &C,
}
}

if (getToolChain().isNoExecStackDefault()) {
CmdArgs.push_back("--noexecstack");
}

switch (getToolChain().getArch()) {
default:
break;
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Driver/ToolChains/Linux.cpp
Expand Up @@ -971,6 +971,10 @@ bool Linux::isPIEDefault() const {
getTriple().isMusl() || getSanitizerArgs().requiresPIE();
}

bool Linux::isNoExecStackDefault() const {
return getTriple().isAndroid();
}

bool Linux::IsMathErrnoDefault() const {
if (getTriple().isAndroid())
return false;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Driver/ToolChains/Linux.h
Expand Up @@ -38,6 +38,7 @@ class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
llvm::opt::ArgStringList &CC1Args) const override;
CXXStdlibType GetDefaultCXXStdlibType() const override;
bool isPIEDefault() const override;
bool isNoExecStackDefault() const override;
bool IsMathErrnoDefault() const override;
SanitizerMask getSupportedSanitizers() const override;
void addProfileRTLibs(const llvm::opt::ArgList &Args,
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Driver/integrated-as.c
Expand Up @@ -13,3 +13,8 @@
// NOFIAS-NOT: cc1as
// NOFIAS: -cc1
// NOFIAS: -no-integrated-as

// RUN: %clang -target arm-linux-androideabi -### \
// RUN: -integrated-as -c %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-ARM-ANDROID %s
// CHECK-ARM-ANDROID: "-mnoexecstack"
4 changes: 2 additions & 2 deletions clang/test/Driver/linux-as.c
Expand Up @@ -108,12 +108,12 @@
// RUN: %clang -target arm-linux-androideabi -### \
// RUN: -no-integrated-as -c %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-ARM-ANDROID %s
// CHECK-ARM-ANDROID: as{{(.exe)?}}" "-EL" "-mfloat-abi=soft"
// CHECK-ARM-ANDROID: as{{(.exe)?}}" "--noexecstack" "-EL" "-mfloat-abi=soft"
//
// RUN: %clang -target arm-linux-androideabi -march=armv7-a -### \
// RUN: -no-integrated-as -c %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-ARM-ANDROID-SOFTFP %s
// CHECK-ARM-ANDROID-SOFTFP: as{{(.exe)?}}" "-EL" "-mfloat-abi=softfp" "-march=armv7-a"
// CHECK-ARM-ANDROID-SOFTFP: as{{(.exe)?}}" "--noexecstack" "-EL" "-mfloat-abi=softfp" "-march=armv7-a"
//
// RUN: %clang -target arm-linux-eabi -mhard-float -### \
// RUN: -no-integrated-as -c %s 2>&1 \
Expand Down
9 changes: 9 additions & 0 deletions clang/test/Driver/linux-ld.c
Expand Up @@ -997,6 +997,15 @@
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-HASH-STYLE-M %s
// CHECK-ANDROID-HASH-STYLE-M: "{{.*}}ld{{(.exe)?}}"
// CHECK-ANDROID-HASH-STYLE-M: "--hash-style=gnu"

// RUN: %clang %s -### -o %t.o 2>&1 \
// RUN: --target=armv7-linux-android21 \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-NOEXECSTACK %s
// CHECK-ANDROID-NOEXECSTACK: "{{.*}}ld{{(.exe)?}}"
// CHECK-ANDROID-NOEXECSTACK: "-z" "noexecstack"
// CHECK-ANDROID-NOEXECSTACK-NOT: "-z" "execstack"
// CHECK-ANDROID-NOEXECSTACK-NOT: "-z,execstack"
// CHECK-ANDROID-NOEXECSTACK-NOT: "-zexecstack"
//
// RUN: %clang %s -### -o %t.o 2>&1 --target=mips64-linux-gnuabin32 \
// RUN: | FileCheck --check-prefix=CHECK-MIPS64EL-GNUABIN32 %s
Expand Down

0 comments on commit 2715b28

Please sign in to comment.