Index: clang/lib/Driver/SanitizerArgs.cpp =================================================================== --- clang/lib/Driver/SanitizerArgs.cpp +++ clang/lib/Driver/SanitizerArgs.cpp @@ -614,7 +614,7 @@ AsanSharedRuntime = Args.hasArg(options::OPT_shared_libasan) || TC.getTriple().isAndroid() || TC.getTriple().isOSFuchsia(); - 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(); Index: clang/lib/Driver/ToolChains/Gnu.cpp =================================================================== --- clang/lib/Driver/ToolChains/Gnu.cpp +++ clang/lib/Driver/ToolChains/Gnu.cpp @@ -282,6 +282,17 @@ } } +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 @@ 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); Index: clang/lib/Driver/ToolChains/Linux.cpp =================================================================== --- clang/lib/Driver/ToolChains/Linux.cpp +++ clang/lib/Driver/ToolChains/Linux.cpp @@ -248,7 +248,7 @@ 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 @@ } } -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; Index: clang/test/Driver/android-pie.c =================================================================== --- /dev/null +++ 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 Index: clang/test/Driver/fsanitize.c =================================================================== --- clang/test/Driver/fsanitize.c +++ 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" Index: clang/test/Driver/linux-ld.c =================================================================== --- clang/test/Driver/linux-ld.c +++ 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 Index: clang/test/Driver/pic.c =================================================================== --- clang/test/Driver/pic.c +++ 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 \ Index: clang/test/Driver/sanitizer-ld.c =================================================================== --- clang/test/Driver/sanitizer-ld.c +++ clang/test/Driver/sanitizer-ld.c @@ -127,7 +127,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" @@ -139,7 +139,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"