[PATCH] D113372: [Driver] Add CLANG_DEFAULT_PIE to emulate GCC --enable-default-pie

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 7 14:38:28 PST 2021


MaskRay created this revision.
MaskRay added reviewers: foutrelis, manojgupta, sylvestre.ledru, thesamesam, tstellar.
Herald added a subscriber: mgorny.
MaskRay requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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. This has been adopted by most popular 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.

This patch add CLANG_DEFAULT_PIE 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.
10+ tests would fail when -DCLANG_DEFAULT_PIE=on is specified.
The lit feature `default-pie` can be handy to exclude default PIE sensitive tests.
Many tests are not too difficult to be made agnostic of the default. They can be
fixed separately.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113372

Files:
  clang/CMakeLists.txt
  clang/include/clang/Config/config.h.cmake
  clang/lib/Driver/ToolChains/Linux.cpp
  clang/test/Driver/linux-ld.c
  clang/test/lit.cfg.py
  clang/test/lit.site.cfg.py.in


Index: clang/test/lit.site.cfg.py.in
===================================================================
--- clang/test/lit.site.cfg.py.in
+++ 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@"
Index: clang/test/lit.cfg.py
===================================================================
--- clang/test/lit.cfg.py
+++ 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 != '':
Index: clang/test/Driver/linux-ld.c
===================================================================
--- clang/test/Driver/linux-ld.c
+++ 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.
 //
Index: clang/lib/Driver/ToolChains/Linux.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Linux.cpp
+++ 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();
 }
Index: clang/include/clang/Config/config.h.cmake
===================================================================
--- clang/include/clang/Config/config.h.cmake
+++ 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}"
 
Index: clang/CMakeLists.txt
===================================================================
--- clang/CMakeLists.txt
+++ 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)")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113372.385374.patch
Type: text/x-patch
Size: 2898 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211107/e14ba3eb/attachment.bin>


More information about the cfe-commits mailing list