Skip to content

Commit

Permalink
Enable -pie and --enable-new-dtags by default on Android.
Browse files Browse the repository at this point in the history
Summary:
Also enable -no-pie on Gnu toolchain (previously available on Darwin only).

Non-PIE executables won't even start on recent Android, and DT_RPATH is ignored by the loader.

Reviewers: srhines, danalbert

Subscribers: cfe-commits

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

llvm-svn: 316606
  • Loading branch information
eugenis committed Oct 25, 2017
1 parent 818cf5b commit 117627c
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 37 deletions.
2 changes: 1 addition & 1 deletion clang/lib/Driver/SanitizerArgs.cpp
Expand Up @@ -622,7 +622,7 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
ImplicitCfiRuntime = TC.getTriple().isAndroid();

if (AllAddedKinds & Address) {
NeedPIE |= TC.getTriple().isAndroid() || TC.getTriple().isOSFuchsia();
NeedPIE |= TC.getTriple().isOSFuchsia();
if (Arg *A =
Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
StringRef S = A->getValue();
Expand Down
15 changes: 12 additions & 3 deletions clang/lib/Driver/ToolChains/Gnu.cpp
Expand Up @@ -282,6 +282,17 @@ static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) {
}
}

static bool getPIE(const ArgList &Args, const toolchains::Linux &ToolChain) {
if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_static))
return false;

Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie,
options::OPT_nopie);
if (!A)
return ToolChain.isPIEDefault();
return A->getOption().matches(options::OPT_pie);
}

void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output,
const InputInfoList &Inputs,
Expand All @@ -296,9 +307,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
const llvm::Triple::ArchType Arch = ToolChain.getArch();
const bool isAndroid = ToolChain.getTriple().isAndroid();
const bool IsIAMCU = ToolChain.getTriple().isOSIAMCU();
const bool IsPIE =
!Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_static) &&
(Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
const bool IsPIE = getPIE(Args, ToolChain);
const bool HasCRTBeginEndFiles =
ToolChain.getTriple().hasEnvironment() ||
(ToolChain.getTriple().getVendor() != llvm::Triple::MipsTechnologies);
Expand Down
7 changes: 5 additions & 2 deletions clang/lib/Driver/ToolChains/Linux.cpp
Expand Up @@ -248,7 +248,7 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
ExtraOpts.push_back("--build-id");
#endif

if (Distro.IsOpenSUSE())
if (IsAndroid || Distro.IsOpenSUSE())
ExtraOpts.push_back("--enable-new-dtags");

// The selection of paths to try here is designed to match the patterns which
Expand Down Expand Up @@ -810,7 +810,10 @@ void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
}
}

bool Linux::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); }
bool Linux::isPIEDefault() const {
return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) ||
getSanitizerArgs().requiresPIE();
}

SanitizerMask Linux::getSupportedSanitizers() const {
const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
Expand Down
66 changes: 66 additions & 0 deletions clang/test/Driver/android-pie.c
@@ -0,0 +1,66 @@
// NO-PIE-NOT: "-pie"
// PIE: "-pie"

// RUN: %clang %s -### -o %t.o 2>&1 --target=arm-linux-androideabi \
// RUN: | FileCheck --check-prefix=NO-PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=arm-linux-android \
// RUN: | FileCheck --check-prefix=NO-PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=arm-linux-android14 \
// RUN: | FileCheck --check-prefix=NO-PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=arm-linux-android16 \
// RUN: | FileCheck --check-prefix=PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=arm-linux-android24 \
// RUN: | FileCheck --check-prefix=PIE %s

// RUN: %clang %s -### -o %t.o 2>&1 --target=mipsel-linux-android \
// RUN: | FileCheck --check-prefix=NO-PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=mipsel-linux-android14 \
// RUN: | FileCheck --check-prefix=NO-PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=mipsel-linux-android16 \
// RUN: | FileCheck --check-prefix=PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=mipsel-linux-android24 \
// RUN: | FileCheck --check-prefix=PIE %s

// RUN: %clang %s -### -o %t.o 2>&1 --target=i686-linux-android \
// RUN: | FileCheck --check-prefix=NO-PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=i686-linux-android14 \
// RUN: | FileCheck --check-prefix=NO-PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=i686-linux-android16 \
// RUN: | FileCheck --check-prefix=PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=i686-linux-android24 \
// RUN: | FileCheck --check-prefix=PIE %s

// RUN: %clang %s -### -o %t.o 2>&1 --target=aarch64-linux-android \
// RUN: | FileCheck --check-prefix=PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=aarch64-linux-android24 \
// RUN: | FileCheck --check-prefix=PIE %s

// RUN: %clang %s -### -o %t.o 2>&1 --target=arm64-linux-android \
// RUN: | FileCheck --check-prefix=PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=arm64-linux-android24 \
// RUN: | FileCheck --check-prefix=PIE %s

// RUN: %clang %s -### -o %t.o 2>&1 --target=mips64el-linux-android \
// RUN: | FileCheck --check-prefix=PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=mips64el-linux-android24 \
// RUN: | FileCheck --check-prefix=PIE %s

// RUN: %clang %s -### -o %t.o 2>&1 --target=x86_64-linux-android \
// RUN: | FileCheck --check-prefix=PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 --target=x86_64-linux-android24 \
// RUN: | FileCheck --check-prefix=PIE %s

// Override toolchain default setting.
// RUN: %clang %s -### -o %t.o 2>&1 -pie --target=arm-linux-androideabi \
// RUN: | FileCheck --check-prefix=PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 -pie --target=arm-linux-androideabi14 \
// RUN: | FileCheck --check-prefix=PIE %s
// RUN: %clang %s -### -o %t.o 2>&1 -no-pie -pie --target=arm-linux-androideabi24 \
// RUN: | FileCheck --check-prefix=PIE %s

// 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
8 changes: 7 additions & 1 deletion clang/test/Driver/fsanitize.c
Expand Up @@ -202,14 +202,20 @@
// RUN: %clang -target x86_64-linux-gnu -fsanitize=memory %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-PIE
// RUN: %clang -target x86_64-unknown-freebsd -fsanitize=memory %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-PIE
// RUN: %clang -target aarch64-linux-gnu -fsanitize=memory %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-PIE
// RUN: %clang -target arm-linux-androideabi -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-PIE
// RUN: %clang -target arm-linux-androideabi -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-PIC-NO-PIE
// RUN: %clang -target arm-linux-androideabi24 -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-PIE
// RUN: %clang -target aarch64-linux-android -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-PIE
// RUN: %clang -target x86_64-linux-gnu -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-PIE
// RUN: %clang -target i386-linux-gnu -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-PIE

// CHECK-NO-PIE-NOT: "-pie"
// CHECK-NO-PIE: "-mrelocation-model" "static"
// CHECK-NO-PIE-NOT: "-pie"

// CHECK-PIC-NO-PIE-NOT: "-pie"
// CHECK-PIC-NO-PIE: "-mrelocation-model" "pic"
// CHECK-PIC-NO-PIE-NOT: "-pie"

// CHECK-PIE: "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie"
// CHECK-PIE: "-pie"

Expand Down
26 changes: 1 addition & 25 deletions clang/test/Driver/linux-ld.c
Expand Up @@ -1133,6 +1133,7 @@
// RUN: --sysroot=%S/Inputs/basic_android_tree/sysroot \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID %s
// CHECK-ANDROID: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]"
// CHECK-ANDROID: "--enable-new-dtags"
// CHECK-ANDROID: "{{.*}}{{/|\\\\}}crtbegin_dynamic.o"
// CHECK-ANDROID: "-L[[SYSROOT]]/usr/lib"
// CHECK-ANDROID-NOT: "gcc_s"
Expand Down Expand Up @@ -1307,31 +1308,6 @@
// CHECK-ANDROID-PIE: "{{.*}}{{/|\\\\}}crtend_android.o"
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: --target=arm-linux-androideabi \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-NO-DEFAULT-PIE %s
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: --target=arm-linux-android \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-NO-DEFAULT-PIE %s
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: --target=aarch64-linux-android \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-NO-DEFAULT-PIE %s
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: --target=arm64-linux-android \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-NO-DEFAULT-PIE %s
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: --target=mipsel-linux-android \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-NO-DEFAULT-PIE %s
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: --target=mips64el-linux-android \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-NO-DEFAULT-PIE %s
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: --target=i686-linux-android \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-NO-DEFAULT-PIE %s
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: --target=x86_64-linux-android \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-NO-DEFAULT-PIE %s
// CHECK-ANDROID-NO-DEFAULT-PIE-NOT: -pie
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: --target=arm-linux-androideabi \
// RUN: --gcc-toolchain="" \
// RUN: --sysroot=%S/Inputs/basic_android_tree/sysroot \
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-32 %s
Expand Down
43 changes: 40 additions & 3 deletions clang/test/Driver/pic.c
Expand Up @@ -251,17 +251,54 @@
// RUN: %clang %s -target i386-pc-openbsd -no-pie -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-NOPIE-LD
//
// On Android PIC is enabled by default
// On Android PIC is enabled by default, and PIE is enabled by default starting
// with API16.
// RUN: %clang -c %s -target i686-linux-android -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
// RUN: %clang -c %s -target i686-linux-android14 -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
// RUN: %clang -c %s -target i686-linux-android16 -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
// RUN: %clang -c %s -target i686-linux-android24 -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
//
// RUN: %clang -c %s -target arm-linux-androideabi -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
// RUN: %clang -c %s -target arm-linux-androideabi14 -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
// RUN: %clang -c %s -target arm-linux-androideabi16 -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
// RUN: %clang -c %s -target arm-linux-androideabi24 -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
//
// RUN: %clang -c %s -target mipsel-linux-android -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
// RUN: %clang -c %s -target aarch64-linux-android -### 2>&1 \
// RUN: %clang -c %s -target mipsel-linux-android14 -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
// RUN: %clang -c %s -target mipsel-linux-android16 -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
// RUN: %clang -c %s -target mipsel-linux-android24 -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
//
// 64-bit Android targets are always PIE.
// RUN: %clang -c %s -target aarch64-linux-android -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
// RUN: %clang -c %s -target aarch64-linux-android24 -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
// RUN: %clang -c %s -target arm64-linux-android -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
//
// Default value of PIE can be overwritten, even on 64-bit targets.
// RUN: %clang -c %s -target arm-linux-androideabi -fPIE -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
// RUN: %clang -c %s -target i686-linux-android14 -fPIE -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
// RUN: %clang -c %s -target i686-linux-android16 -fno-PIE -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
// RUN: %clang -c %s -target aarch64-linux-android -fno-PIE -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
// RUN: %clang -c %s -target aarch64-linux-android24 -fno-PIE -### 2>&1 \
// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
//
// On Windows-X64 PIC is enabled by default
// RUN: %clang -c %s -target x86_64-pc-windows-msvc18.0.0 -### 2>&1 \
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Driver/sanitizer-ld.c
Expand Up @@ -140,7 +140,7 @@
//
// CHECK-ASAN-ANDROID: "{{(.*[^.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
// CHECK-ASAN-ANDROID-NOT: "-lc"
// CHECK-ASAN-ANDROID: "-pie"
// CHECK-ASAN-ANDROID-NOT: "-pie"
// CHECK-ASAN-ANDROID-NOT: "-lpthread"
// CHECK-ASAN-ANDROID: libclang_rt.asan-arm-android.so"
// CHECK-ASAN-ANDROID-NOT: "-lpthread"
Expand Down Expand Up @@ -185,7 +185,7 @@
//
// CHECK-ASAN-ANDROID-X86: "{{(.*[^.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
// CHECK-ASAN-ANDROID-X86-NOT: "-lc"
// CHECK-ASAN-ANDROID-X86: "-pie"
// CHECK-ASAN-ANDROID-X86-NOT: "-pie"
// CHECK-ASAN-ANDROID-X86-NOT: "-lpthread"
// CHECK-ASAN-ANDROID-X86: libclang_rt.asan-i686-android.so"
// CHECK-ASAN-ANDROID-X86-NOT: "-lpthread"
Expand Down

0 comments on commit 117627c

Please sign in to comment.