[llvm] 1042de9 - [Driver] Add CLANG_DEFAULT_PIE_ON_LINUX to emulate GCC --enable-default-pie
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 14 10:09:04 PST 2021
Author: Fangrui Song
Date: 2021-12-14T10:09:00-08:00
New Revision: 1042de90589c74c6998c127d1ee1b33ff3efb8cc
URL: https://github.com/llvm/llvm-project/commit/1042de90589c74c6998c127d1ee1b33ff3efb8cc
DIFF: https://github.com/llvm/llvm-project/commit/1042de90589c74c6998c127d1ee1b33ff3efb8cc.diff
LOG: [Driver] Add CLANG_DEFAULT_PIE_ON_LINUX to emulate GCC --enable-default-pie
In 2015-05, GCC added the configure option `--enable-default-pie`. When enabled,
* in the absence of -fno-pic/-fpie/-fpic (and their upper-case variants), -fPIE is the default.
* in the absence of -no-pie/-pie/-shared/-static/-static-pie, -pie is the default.
This has been adopted by all(?) major distros.
I think default PIE is the majority in the Linux world, but
--disable-default-pie users is not that uncommon because GCC upstream hasn't
switched the default yet (https://gcc.gnu.org/PR103398).
This patch add CLANG_DEFAULT_PIE_ON_LINUX which allows distros to use default PIE.
The option is justified as its adoption can be very high among Linux distros
to make Clang default match GCC, and is likely a future-new-default, at which
point we will remove CLANG_DEFAULT_PIE_ON_LINUX.
The lit feature `default-pie-on-linux` can be handy to exclude default PIE sensitive tests.
Reviewed By: foutrelis, sylvestre.ledru, thesamesam
Differential Revision: https://reviews.llvm.org/D113372
Added:
clang/test/Driver/linux-default-pie.c
Modified:
clang/CMakeLists.txt
clang/docs/ReleaseNotes.rst
clang/include/clang/Config/config.h.cmake
clang/lib/Driver/ToolChains/Linux.cpp
clang/test/Driver/fsanitize.c
clang/test/Driver/linux-ld.c
clang/test/lit.cfg.py
clang/test/lit.site.cfg.py.in
llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn
utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
Removed:
################################################################################
diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index 6f26e94f31602..00243d8b13b97 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -227,6 +227,11 @@ set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL
set(CLANG_SPAWN_CC1 OFF CACHE BOOL
"Whether clang should use a new process for the CC1 invocation")
+option(CLANG_DEFAULT_PIE_ON_LINUX "Default to -fPIE and -pie on Linux" OFF)
+if(CLANG_DEFAULT_PIE_ON_LINUX)
+ set(CLANG_DEFAULT_PIE_ON_LINUX 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/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6b0ce5bfd595a..07953a8fb0fdc 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -249,7 +249,10 @@ Internal API Changes
Build System Changes
--------------------
-- ...
+- Linux distros can specify ``-DCLANG_DEFAULT_PIE_ON_LINUX=On`` to use ``-fPIE`` and
+ ``-pie`` by default. This matches GCC installations on many Linux distros
+ (configured with ``--enable-default-pie``).
+ (`D113372 <https://reviews.llvm.org/D113372>`_)
AST Matchers
------------
diff --git a/clang/include/clang/Config/config.h.cmake b/clang/include/clang/Config/config.h.cmake
index a240862c52895..53386ef94129b 100644
--- 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 on Linux. */
+#cmakedefine01 CLANG_DEFAULT_PIE_ON_LINUX
+
/* 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
index 7494c2129211e..e413640abad35 100644
--- a/clang/lib/Driver/ToolChains/Linux.cpp
+++ b/clang/lib/Driver/ToolChains/Linux.cpp
@@ -663,8 +663,8 @@ void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
}
bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const {
- return getTriple().isAndroid() || getTriple().isMusl() ||
- getSanitizerArgs(Args).requiresPIE();
+ return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() ||
+ getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE();
}
bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const {
diff --git a/clang/test/Driver/fsanitize.c b/clang/test/Driver/fsanitize.c
index a6b5776c9e706..7bcda5c33dc9e 100644
--- a/clang/test/Driver/fsanitize.c
+++ b/clang/test/Driver/fsanitize.c
@@ -1,3 +1,4 @@
+// UNSUPPORTED: default-pie-on-linux
// RUN: %clang -target x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-TRAP
// RUN: %clang -target x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-trap=signed-integer-overflow %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-TRAP2
// RUN: %clang -target x86_64-linux-gnu -fsanitize=undefined -fsanitize-undefined-trap-on-error %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-TRAP
diff --git a/clang/test/Driver/linux-default-pie.c b/clang/test/Driver/linux-default-pie.c
new file mode 100644
index 0000000000000..6772a6cd3fd0c
--- /dev/null
+++ b/clang/test/Driver/linux-default-pie.c
@@ -0,0 +1,7 @@
+// REQUIRES: default-pie-on-linux
+/// Test -DCLANG_DEFAULT_PIE_ON_LINUX=on.
+
+// RUN: %clang -### --target=aarch64-linux-gnu %s 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
index 31905e1a3d00b..b70b712c2eb77 100644
--- 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-on-linux
// 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
index 5a9a1abf4531a..514ddf997c7ce 100644
--- a/clang/test/lit.cfg.py
+++ b/clang/test/lit.cfg.py
@@ -121,6 +121,9 @@ def have_host_jit_support():
if config.has_plugins and config.llvm_plugin_ext:
config.available_features.add('plugins')
+if config.clang_default_pie_on_linux == '1':
+ config.available_features.add('default-pie-on-linux')
+
# 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
index a99550c3f909a..807c1d3472173 100644
--- a/clang/test/lit.site.cfg.py.in
+++ b/clang/test/lit.site.cfg.py.in
@@ -22,6 +22,7 @@ config.host_cxx = "@CMAKE_CXX_COMPILER@"
config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@"
config.have_zlib = @LLVM_ENABLE_ZLIB@
config.clang_arcmt = @CLANG_ENABLE_ARCMT@
+config.clang_default_pie_on_linux = "@CLANG_DEFAULT_PIE_ON_LINUX@"
config.clang_default_cxx_stdlib = "@CLANG_DEFAULT_CXX_STDLIB@"
config.clang_staticanalyzer = @CLANG_ENABLE_STATIC_ANALYZER@
config.clang_staticanalyzer_z3 = "@LLVM_WITH_Z3@"
diff --git a/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn b/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn
index aa3e40af1d9dc..f635c0abe04d4 100644
--- a/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn
+++ b/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn
@@ -9,6 +9,7 @@ write_cmake_config("Config") {
output = "$target_gen_dir/config.h"
values = [
"BUG_REPORT_URL=https://bugs.llvm.org/",
+ "CLANG_DEFAULT_PIE_ON_LINUX=0",
"CLANG_DEFAULT_LINKER=",
"CLANG_DEFAULT_STD_C=",
"CLANG_DEFAULT_STD_CXX=",
diff --git a/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h b/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
index 4d5cd0520a0c4..ee4c6277d2ae9 100644
--- a/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
+++ b/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
@@ -22,6 +22,9 @@
/* Bug report URL. */
#define BUG_REPORT_URL "https://bugs.llvm.org/"
+/* Default to -fPIE and -pie on Linux. */
+#define CLANG_DEFAULT_PIE_ON_LINUX 0
+
/* Default linker to use. */
#define CLANG_DEFAULT_LINKER ""
More information about the llvm-commits
mailing list