浏览代码

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316606 91177308-0d34-0410-b5e6-96231b3b80d8
Evgeniy Stepanov 7 年之前
父节点
当前提交
516d04abee

+ 1 - 1
lib/Driver/SanitizerArgs.cpp

@@ -622,7 +622,7 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
   ImplicitCfiRuntime = TC.getTriple().isAndroid();
   ImplicitCfiRuntime = TC.getTriple().isAndroid();
 
 
   if (AllAddedKinds & Address) {
   if (AllAddedKinds & Address) {
-    NeedPIE |= TC.getTriple().isAndroid() || TC.getTriple().isOSFuchsia();
+    NeedPIE |= TC.getTriple().isOSFuchsia();
     if (Arg *A =
     if (Arg *A =
             Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
             Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
         StringRef S = A->getValue();
         StringRef S = A->getValue();

+ 12 - 3
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,
 void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
                                            const InputInfo &Output,
                                            const InputInfo &Output,
                                            const InputInfoList &Inputs,
                                            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 llvm::Triple::ArchType Arch = ToolChain.getArch();
   const bool isAndroid = ToolChain.getTriple().isAndroid();
   const bool isAndroid = ToolChain.getTriple().isAndroid();
   const bool IsIAMCU = ToolChain.getTriple().isOSIAMCU();
   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 =
   const bool HasCRTBeginEndFiles =
       ToolChain.getTriple().hasEnvironment() ||
       ToolChain.getTriple().hasEnvironment() ||
       (ToolChain.getTriple().getVendor() != llvm::Triple::MipsTechnologies);
       (ToolChain.getTriple().getVendor() != llvm::Triple::MipsTechnologies);

+ 5 - 2
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");
   ExtraOpts.push_back("--build-id");
 #endif
 #endif
 
 
-  if (Distro.IsOpenSUSE())
+  if (IsAndroid || Distro.IsOpenSUSE())
     ExtraOpts.push_back("--enable-new-dtags");
     ExtraOpts.push_back("--enable-new-dtags");
 
 
   // The selection of paths to try here is designed to match the patterns which
   // 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 {
 SanitizerMask Linux::getSupportedSanitizers() const {
   const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
   const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;

+ 66 - 0
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

+ 7 - 1
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-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 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 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 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
 // 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: "-mrelocation-model" "static"
 // CHECK-NO-PIE-NOT: "-pie"
 // 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: "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie"
 // CHECK-PIE: "-pie"
 // CHECK-PIE: "-pie"
 
 

+ 1 - 25
test/Driver/linux-ld.c

@@ -1133,6 +1133,7 @@
 // RUN:     --sysroot=%S/Inputs/basic_android_tree/sysroot \
 // RUN:     --sysroot=%S/Inputs/basic_android_tree/sysroot \
 // RUN:   | FileCheck --check-prefix=CHECK-ANDROID %s
 // RUN:   | FileCheck --check-prefix=CHECK-ANDROID %s
 // CHECK-ANDROID: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]"
 // CHECK-ANDROID: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]"
+// CHECK-ANDROID: "--enable-new-dtags"
 // CHECK-ANDROID: "{{.*}}{{/|\\\\}}crtbegin_dynamic.o"
 // CHECK-ANDROID: "{{.*}}{{/|\\\\}}crtbegin_dynamic.o"
 // CHECK-ANDROID: "-L[[SYSROOT]]/usr/lib"
 // CHECK-ANDROID: "-L[[SYSROOT]]/usr/lib"
 // CHECK-ANDROID-NOT: "gcc_s"
 // CHECK-ANDROID-NOT: "gcc_s"
@@ -1307,31 +1308,6 @@
 // CHECK-ANDROID-PIE: "{{.*}}{{/|\\\\}}crtend_android.o"
 // CHECK-ANDROID-PIE: "{{.*}}{{/|\\\\}}crtend_android.o"
 // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
 // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
 // RUN:     --target=arm-linux-androideabi \
 // 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:     --gcc-toolchain="" \
 // RUN:     --sysroot=%S/Inputs/basic_android_tree/sysroot \
 // RUN:     --sysroot=%S/Inputs/basic_android_tree/sysroot \
 // RUN:   | FileCheck --check-prefix=CHECK-ANDROID-32 %s
 // RUN:   | FileCheck --check-prefix=CHECK-ANDROID-32 %s

+ 40 - 3
test/Driver/pic.c

@@ -251,17 +251,54 @@
 // RUN: %clang %s -target i386-pc-openbsd -no-pie -### 2>&1 \
 // RUN: %clang %s -target i386-pc-openbsd -no-pie -### 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=CHECK-NOPIE-LD
 // 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: %clang -c %s -target i686-linux-android -### 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
 // 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: %clang -c %s -target arm-linux-androideabi -### 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=CHECK-PIC1
 // 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: %clang -c %s -target mipsel-linux-android -### 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=CHECK-PIC1
 // 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:   | 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: %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
 // On Windows-X64 PIC is enabled by default
 // RUN: %clang -c %s -target x86_64-pc-windows-msvc18.0.0 -### 2>&1 \
 // RUN: %clang -c %s -target x86_64-pc-windows-msvc18.0.0 -### 2>&1 \

+ 2 - 2
test/Driver/sanitizer-ld.c

@@ -140,7 +140,7 @@
 //
 //
 // CHECK-ASAN-ANDROID: "{{(.*[^.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
 // CHECK-ASAN-ANDROID: "{{(.*[^.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
 // CHECK-ASAN-ANDROID-NOT: "-lc"
 // CHECK-ASAN-ANDROID-NOT: "-lc"
-// CHECK-ASAN-ANDROID: "-pie"
+// CHECK-ASAN-ANDROID-NOT: "-pie"
 // CHECK-ASAN-ANDROID-NOT: "-lpthread"
 // CHECK-ASAN-ANDROID-NOT: "-lpthread"
 // CHECK-ASAN-ANDROID: libclang_rt.asan-arm-android.so"
 // CHECK-ASAN-ANDROID: libclang_rt.asan-arm-android.so"
 // CHECK-ASAN-ANDROID-NOT: "-lpthread"
 // CHECK-ASAN-ANDROID-NOT: "-lpthread"
@@ -185,7 +185,7 @@
 //
 //
 // CHECK-ASAN-ANDROID-X86: "{{(.*[^.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
 // CHECK-ASAN-ANDROID-X86: "{{(.*[^.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
 // CHECK-ASAN-ANDROID-X86-NOT: "-lc"
 // 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-NOT: "-lpthread"
 // CHECK-ASAN-ANDROID-X86: libclang_rt.asan-i686-android.so"
 // CHECK-ASAN-ANDROID-X86: libclang_rt.asan-i686-android.so"
 // CHECK-ASAN-ANDROID-X86-NOT: "-lpthread"
 // CHECK-ASAN-ANDROID-X86-NOT: "-lpthread"