Skip to content

Commit

Permalink
[Driver] Add CLANG_DEFAULT_PIE_ON_LINUX to emulate GCC --enable-defau…
Browse files Browse the repository at this point in the history
…lt-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
  • Loading branch information
MaskRay committed Dec 14, 2021
1 parent f0ca8d2 commit 1042de9
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 4 deletions.
5 changes: 5 additions & 0 deletions clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
5 changes: 4 additions & 1 deletion clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Config/config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Driver/ToolChains/Linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions clang/test/Driver/fsanitize.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions clang/test/Driver/linux-default-pie.c
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion clang/test/Driver/linux-ld.c
Original file line number Diff line number Diff line change
@@ -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.
//
Expand Down
3 changes: 3 additions & 0 deletions clang/test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 != '':
Expand Down
1 change: 1 addition & 0 deletions clang/test/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -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@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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=",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""

Expand Down

0 comments on commit 1042de9

Please sign in to comment.