diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt --- a/clang/CMakeLists.txt +++ b/clang/CMakeLists.txt @@ -227,6 +227,11 @@ set(CLANG_SPAWN_CC1 OFF CACHE BOOL "Whether clang should use a new process for the CC1 invocation") +option(CLANG_DEFAULT_PIE "Default to -fPIE and -pie (Linux only)" OFF) +if(CLANG_DEFAULT_PIE) + set(CLANG_DEFAULT_PIE 1) +endif() + # TODO: verify the values against LangStandards.def? set(CLANG_DEFAULT_STD_C "" CACHE STRING "Default standard to use for C/ObjC code (IDENT from LangStandards.def, empty for platform default)") diff --git a/clang/include/clang/Config/config.h.cmake b/clang/include/clang/Config/config.h.cmake --- a/clang/include/clang/Config/config.h.cmake +++ b/clang/include/clang/Config/config.h.cmake @@ -8,6 +8,9 @@ /* Bug report URL. */ #define BUG_REPORT_URL "${BUG_REPORT_URL}" +/* Default to -fPIE and -pie. */ +#cmakedefine01 CLANG_DEFAULT_PIE + /* Default linker to use. */ #define CLANG_DEFAULT_LINKER "${CLANG_DEFAULT_LINKER}" diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp --- a/clang/lib/Driver/ToolChains/Linux.cpp +++ b/clang/lib/Driver/ToolChains/Linux.cpp @@ -663,6 +663,8 @@ } bool Linux::isPIEDefault() const { + if (CLANG_DEFAULT_PIE) + return true; return getTriple().isAndroid() || getTriple().isMusl() || getSanitizerArgs().requiresPIE(); } diff --git a/clang/test/Driver/default-pie.c b/clang/test/Driver/default-pie.c new file mode 100644 --- /dev/null +++ b/clang/test/Driver/default-pie.c @@ -0,0 +1,7 @@ +// REQUIRES: default-pie +/// Test -DCLANG_DEFAULT_PIE=on. + +// RUN: %clang %s -target aarch64-linux-gnu -### 2>&1 | FileCheck %s --check-prefix=PIE2 + +// PIE2: "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" +// PIE2: "-pie" diff --git a/clang/test/Driver/linux-ld.c b/clang/test/Driver/linux-ld.c --- a/clang/test/Driver/linux-ld.c +++ b/clang/test/Driver/linux-ld.c @@ -1,4 +1,4 @@ -// UNSUPPORTED: system-windows +// UNSUPPORTED: system-windows, default-pie // General tests that ld invocations on Linux targets sane. Note that we use // sysroot to make these tests independent of the host system. // diff --git a/clang/test/lit.cfg.py b/clang/test/lit.cfg.py --- a/clang/test/lit.cfg.py +++ b/clang/test/lit.cfg.py @@ -121,6 +121,9 @@ if config.has_plugins and config.llvm_plugin_ext: config.available_features.add('plugins') +if config.clang_default_pie == '1': + config.available_features.add('default-pie') + # Set available features we allow tests to conditionalize on. # if config.clang_default_cxx_stdlib != '': diff --git a/clang/test/lit.site.cfg.py.in b/clang/test/lit.site.cfg.py.in --- a/clang/test/lit.site.cfg.py.in +++ b/clang/test/lit.site.cfg.py.in @@ -22,6 +22,7 @@ config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@" config.have_zlib = @LLVM_ENABLE_ZLIB@ config.clang_arcmt = @CLANG_ENABLE_ARCMT@ +config.clang_default_pie = "@CLANG_DEFAULT_PIE@" config.clang_default_cxx_stdlib = "@CLANG_DEFAULT_CXX_STDLIB@" config.clang_staticanalyzer = @CLANG_ENABLE_STATIC_ANALYZER@ config.clang_staticanalyzer_z3 = "@LLVM_WITH_Z3@"