From 117627c9a13ba68849599520d7013511aa7986a7 Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Wed, 25 Oct 2017 20:39:22 +0000 Subject: [PATCH] Enable -pie and --enable-new-dtags by default on Android. 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 --- clang/lib/Driver/SanitizerArgs.cpp | 2 +- clang/lib/Driver/ToolChains/Gnu.cpp | 15 ++++++-- clang/lib/Driver/ToolChains/Linux.cpp | 7 ++-- clang/test/Driver/android-pie.c | 66 +++++++++++++++++++++++++++++++++++ clang/test/Driver/fsanitize.c | 8 ++++- clang/test/Driver/linux-ld.c | 26 +------------- clang/test/Driver/pic.c | 43 +++++++++++++++++++++-- clang/test/Driver/sanitizer-ld.c | 4 +-- 8 files changed, 134 insertions(+), 37 deletions(-) create mode 100644 clang/test/Driver/android-pie.c diff --git a/clang/lib/Driver/SanitizerArgs.cpp b/clang/lib/Driver/SanitizerArgs.cpp index 5eeaeb3..0379896 100644 --- a/clang/lib/Driver/SanitizerArgs.cpp +++ b/clang/lib/Driver/SanitizerArgs.cpp @@ -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(); diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index 757ebda..08d4aa5 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -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, @@ -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); diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp index 1adf9f7..d99f980 100644 --- a/clang/lib/Driver/ToolChains/Linux.cpp +++ b/clang/lib/Driver/ToolChains/Linux.cpp @@ -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 @@ -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; diff --git a/clang/test/Driver/android-pie.c b/clang/test/Driver/android-pie.c new file mode 100644 index 0000000..2569c55 --- /dev/null +++ b/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 diff --git a/clang/test/Driver/fsanitize.c b/clang/test/Driver/fsanitize.c index 0f37070..d6f0f19 100644 --- a/clang/test/Driver/fsanitize.c +++ b/clang/test/Driver/fsanitize.c @@ -202,7 +202,9 @@ // 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 @@ -210,6 +212,10 @@ // 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" diff --git a/clang/test/Driver/linux-ld.c b/clang/test/Driver/linux-ld.c index a47afe3..662aabb 100644 --- a/clang/test/Driver/linux-ld.c +++ b/clang/test/Driver/linux-ld.c @@ -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" @@ -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 diff --git a/clang/test/Driver/pic.c b/clang/test/Driver/pic.c index 6b01c58..f2618ee 100644 --- a/clang/test/Driver/pic.c +++ b/clang/test/Driver/pic.c @@ -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 \ diff --git a/clang/test/Driver/sanitizer-ld.c b/clang/test/Driver/sanitizer-ld.c index 68eab30..f03714c 100644 --- a/clang/test/Driver/sanitizer-ld.c +++ b/clang/test/Driver/sanitizer-ld.c @@ -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" @@ -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" -- 2.7.4