Index: CMakeLists.txt =================================================================== --- CMakeLists.txt +++ CMakeLists.txt @@ -216,6 +216,9 @@ endif() endif() +option(CLANG_ENABLE_DEFAULT_PIE + "Use -fPIE and -pie as default option" OFF) + include(CheckIncludeFile) check_include_file(sys/resource.h CLANG_HAVE_RLIMITS) Index: include/clang/Config/config.h.cmake =================================================================== --- include/clang/Config/config.h.cmake +++ include/clang/Config/config.h.cmake @@ -35,6 +35,9 @@ /* Default architecture for OpenMP offloading to Nvidia GPUs. */ #define CLANG_OPENMP_NVPTX_DEFAULT_ARCH "${CLANG_OPENMP_NVPTX_DEFAULT_ARCH}" +/* Use -fPIE and -pie as default option */ +#cmakedefine01 CLANG_ENABLE_DEFAULT_PIE + /* Multilib suffix for libdir. */ #define CLANG_LIBDIR_SUFFIX "${CLANG_LIBDIR_SUFFIX}" Index: include/clang/Driver/ToolChain.h =================================================================== --- include/clang/Driver/ToolChain.h +++ include/clang/Driver/ToolChain.h @@ -13,6 +13,7 @@ #include "clang/Basic/LLVM.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/Sanitizers.h" +#include "clang/Config/config.h" #include "clang/Driver/Action.h" #include "clang/Driver/Multilib.h" #include "clang/Driver/Types.h" @@ -410,7 +411,11 @@ virtual bool isPICDefault() const = 0; /// Test whether this toolchain defaults to PIE. +#if CLANG_ENABLE_DEFAULT_PIE + bool isPIEDefault() const { return true; }; +#else virtual bool isPIEDefault() const = 0; +#endif /// Test whether this toolchaind defaults to non-executable stacks. virtual bool isNoExecStackDefault() const; Index: lib/Driver/Driver.cpp =================================================================== --- lib/Driver/Driver.cpp +++ lib/Driver/Driver.cpp @@ -44,7 +44,6 @@ #include "ToolChains/WebAssembly.h" #include "ToolChains/XCore.h" #include "clang/Basic/Version.h" -#include "clang/Config/config.h" #include "clang/Driver/Action.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/DriverDiagnostic.h" Index: lib/Driver/ToolChain.cpp =================================================================== --- lib/Driver/ToolChain.cpp +++ lib/Driver/ToolChain.cpp @@ -12,7 +12,6 @@ #include "ToolChains/Clang.h" #include "clang/Basic/ObjCRuntime.h" #include "clang/Basic/Sanitizers.h" -#include "clang/Config/config.h" #include "clang/Driver/Action.h" #include "clang/Driver/Driver.h" #include "clang/Driver/DriverDiagnostic.h" Index: lib/Driver/ToolChains/BareMetal.h =================================================================== --- lib/Driver/ToolChains/BareMetal.h +++ lib/Driver/ToolChains/BareMetal.h @@ -33,7 +33,9 @@ bool useIntegratedAs() const override { return true; } bool isCrossCompiling() const override { return true; } bool isPICDefault() const override { return false; } +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override { return false; } bool SupportsProfiling() const override { return false; } Index: lib/Driver/ToolChains/CloudABI.h =================================================================== --- lib/Driver/ToolChains/CloudABI.h +++ lib/Driver/ToolChains/CloudABI.h @@ -55,7 +55,9 @@ void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override; +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override; +#endif SanitizerMask getSupportedSanitizers() const override; SanitizerMask getDefaultSanitizers() const override; Index: lib/Driver/ToolChains/CloudABI.cpp =================================================================== --- lib/Driver/ToolChains/CloudABI.cpp +++ lib/Driver/ToolChains/CloudABI.cpp @@ -123,6 +123,7 @@ return new tools::cloudabi::Linker(*this); } +#if !CLANG_ENABLE_DEFAULT_PIE bool CloudABI::isPIEDefault() const { // Only enable PIE on architectures that support PC-relative // addressing. PC-relative addressing is required, as the process @@ -135,6 +136,7 @@ return false; } } +#endif SanitizerMask CloudABI::getSupportedSanitizers() const { SanitizerMask Res = ToolChain::getSupportedSanitizers(); Index: lib/Driver/ToolChains/CommonArgs.cpp =================================================================== --- lib/Driver/ToolChains/CommonArgs.cpp +++ lib/Driver/ToolChains/CommonArgs.cpp @@ -20,7 +20,6 @@ #include "clang/Basic/LangOptions.h" #include "clang/Basic/ObjCRuntime.h" #include "clang/Basic/Version.h" -#include "clang/Config/config.h" #include "clang/Driver/Action.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" Index: lib/Driver/ToolChains/CrossWindows.h =================================================================== --- lib/Driver/ToolChains/CrossWindows.h +++ lib/Driver/ToolChains/CrossWindows.h @@ -57,7 +57,9 @@ bool IsIntegratedAssemblerDefault() const override { return true; } bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefault() const override; +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override; +#endif bool isPICDefaultForced() const override; unsigned int GetDefaultStackProtectorLevel(bool KernelOrKext) const override { Index: lib/Driver/ToolChains/CrossWindows.cpp =================================================================== --- lib/Driver/ToolChains/CrossWindows.cpp +++ lib/Driver/ToolChains/CrossWindows.cpp @@ -220,9 +220,11 @@ return getArch() == llvm::Triple::x86_64; } +#if !CLANG_ENABLE_DEFAULT_PIE bool CrossWindowsToolChain::isPIEDefault() const { return getArch() == llvm::Triple::x86_64; } +#endif bool CrossWindowsToolChain::isPICDefaultForced() const { return getArch() == llvm::Triple::x86_64; Index: lib/Driver/ToolChains/Cuda.h =================================================================== --- lib/Driver/ToolChains/Cuda.h +++ lib/Driver/ToolChains/Cuda.h @@ -154,7 +154,9 @@ bool useIntegratedAs() const override { return false; } bool isCrossCompiling() const override { return true; } bool isPICDefault() const override { return false; } +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override { return false; } bool SupportsProfiling() const override { return false; } bool supportsDebugInfoOption(const llvm::opt::Arg *A) const override; Index: lib/Driver/ToolChains/Cuda.cpp =================================================================== --- lib/Driver/ToolChains/Cuda.cpp +++ lib/Driver/ToolChains/Cuda.cpp @@ -10,7 +10,6 @@ #include "CommonArgs.h" #include "InputInfo.h" #include "clang/Basic/Cuda.h" -#include "clang/Config/config.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Distro.h" #include "clang/Driver/Driver.h" Index: lib/Driver/ToolChains/Darwin.h =================================================================== --- lib/Driver/ToolChains/Darwin.h +++ lib/Driver/ToolChains/Darwin.h @@ -241,7 +241,9 @@ } bool isPICDefault() const override; - bool isPIEDefault() const override; +#if !CLANG_ENABLE_DEFAULT_PIE + bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override; bool SupportsProfiling() const override; Index: lib/Driver/ToolChains/Darwin.cpp =================================================================== --- lib/Driver/ToolChains/Darwin.cpp +++ lib/Driver/ToolChains/Darwin.cpp @@ -2231,8 +2231,6 @@ bool MachO::isPICDefault() const { return true; } -bool MachO::isPIEDefault() const { return false; } - bool MachO::isPICDefaultForced() const { return (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::aarch64); Index: lib/Driver/ToolChains/FreeBSD.h =================================================================== --- lib/Driver/ToolChains/FreeBSD.h +++ lib/Driver/ToolChains/FreeBSD.h @@ -67,7 +67,9 @@ llvm::ExceptionHandling GetExceptionModel( const llvm::opt::ArgList &Args) const override; +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override; +#endif SanitizerMask getSupportedSanitizers() const override; unsigned GetDefaultDwarfVersion() const override { return 2; } // Until dtrace (via CTF) and LLDB can deal with distributed debug info, Index: lib/Driver/ToolChains/FreeBSD.cpp =================================================================== --- lib/Driver/ToolChains/FreeBSD.cpp +++ lib/Driver/ToolChains/FreeBSD.cpp @@ -402,7 +402,9 @@ bool FreeBSD::HasNativeLLVMSupport() const { return true; } +#if !CLANG_ENABLE_DEFAULT_PIE bool FreeBSD::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); } +#endif SanitizerMask FreeBSD::getSupportedSanitizers() const { const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; Index: lib/Driver/ToolChains/Fuchsia.h =================================================================== --- lib/Driver/ToolChains/Fuchsia.h +++ lib/Driver/ToolChains/Fuchsia.h @@ -53,7 +53,9 @@ return true; } bool isPICDefault() const override { return false; } +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override { return true; } +#endif bool isPICDefaultForced() const override { return false; } llvm::DebuggerKind getDefaultDebuggerTuning() const override { return llvm::DebuggerKind::GDB; Index: lib/Driver/ToolChains/Fuchsia.cpp =================================================================== --- lib/Driver/ToolChains/Fuchsia.cpp +++ lib/Driver/ToolChains/Fuchsia.cpp @@ -8,7 +8,6 @@ #include "Fuchsia.h" #include "CommonArgs.h" -#include "clang/Config/config.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/DriverDiagnostic.h" Index: lib/Driver/ToolChains/Gnu.h =================================================================== --- lib/Driver/ToolChains/Gnu.h +++ lib/Driver/ToolChains/Gnu.h @@ -288,7 +288,9 @@ bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefault() const override; - bool isPIEDefault() const override; +#if !CLANG_ENABLE_DEFAULT_PIE + bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override; bool IsIntegratedAssemblerDefault() const override; llvm::opt::DerivedArgList * Index: lib/Driver/ToolChains/Gnu.cpp =================================================================== --- lib/Driver/ToolChains/Gnu.cpp +++ lib/Driver/ToolChains/Gnu.cpp @@ -15,7 +15,6 @@ #include "Arch/SystemZ.h" #include "CommonArgs.h" #include "Linux.h" -#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/DriverDiagnostic.h" @@ -2503,8 +2502,6 @@ } } -bool Generic_GCC::isPIEDefault() const { return false; } - bool Generic_GCC::isPICDefaultForced() const { return getArch() == llvm::Triple::x86_64 && getTriple().isOSWindows(); } Index: lib/Driver/ToolChains/HIP.h =================================================================== --- lib/Driver/ToolChains/HIP.h +++ lib/Driver/ToolChains/HIP.h @@ -90,7 +90,9 @@ bool useIntegratedAs() const override { return true; } bool isCrossCompiling() const override { return true; } bool isPICDefault() const override { return false; } +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override { return false; } bool SupportsProfiling() const override { return false; } bool IsMathErrnoDefault() const override { return false; } Index: lib/Driver/ToolChains/Haiku.h =================================================================== --- lib/Driver/ToolChains/Haiku.h +++ lib/Driver/ToolChains/Haiku.h @@ -22,9 +22,11 @@ Haiku(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args); +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override { return getTriple().getArch() == llvm::Triple::x86_64; } +#endif void addLibCxxIncludePaths( const llvm::opt::ArgList &DriverArgs, Index: lib/Driver/ToolChains/Hurd.cpp =================================================================== --- lib/Driver/ToolChains/Hurd.cpp +++ lib/Driver/ToolChains/Hurd.cpp @@ -8,7 +8,6 @@ #include "Hurd.h" #include "CommonArgs.h" -#include "clang/Config/config.h" #include "clang/Driver/Driver.h" #include "clang/Driver/Options.h" #include "llvm/Support/Path.h" Index: lib/Driver/ToolChains/Linux.h =================================================================== --- lib/Driver/ToolChains/Linux.h +++ lib/Driver/ToolChains/Linux.h @@ -37,7 +37,9 @@ void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override; CXXStdlibType GetDefaultCXXStdlibType() const override; +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override; +#endif bool isNoExecStackDefault() const override; bool IsMathErrnoDefault() const override; SanitizerMask getSupportedSanitizers() const override; Index: lib/Driver/ToolChains/Linux.cpp =================================================================== --- lib/Driver/ToolChains/Linux.cpp +++ lib/Driver/ToolChains/Linux.cpp @@ -12,7 +12,6 @@ #include "Arch/PPC.h" #include "Arch/RISCV.h" #include "CommonArgs.h" -#include "clang/Config/config.h" #include "clang/Driver/Distro.h" #include "clang/Driver/Driver.h" #include "clang/Driver/Options.h" @@ -973,10 +972,12 @@ } } +#if !CLANG_ENABLE_DEFAULT_PIE bool Linux::isPIEDefault() const { return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) || getTriple().isMusl() || getSanitizerArgs().requiresPIE(); } +#endif bool Linux::isNoExecStackDefault() const { return getTriple().isAndroid(); Index: lib/Driver/ToolChains/MSP430.h =================================================================== --- lib/Driver/ToolChains/MSP430.h +++ lib/Driver/ToolChains/MSP430.h @@ -37,7 +37,9 @@ Action::OffloadKind) const override; bool isPICDefault() const override { return false; } +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override { return true; } protected: Index: lib/Driver/ToolChains/MSVC.h =================================================================== --- lib/Driver/ToolChains/MSVC.h +++ lib/Driver/ToolChains/MSVC.h @@ -75,7 +75,9 @@ bool IsIntegratedAssemblerDefault() const override; bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefault() const override; - bool isPIEDefault() const override; +#if !CLANG_ENABLE_DEFAULT_PIE + bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override; /// Set CodeView as the default debug info format. Users can use -gcodeview Index: lib/Driver/ToolChains/MSVC.cpp =================================================================== --- lib/Driver/ToolChains/MSVC.cpp +++ lib/Driver/ToolChains/MSVC.cpp @@ -736,10 +736,6 @@ return getArch() == llvm::Triple::x86_64; } -bool MSVCToolChain::isPIEDefault() const { - return false; -} - bool MSVCToolChain::isPICDefaultForced() const { return getArch() == llvm::Triple::x86_64; } Index: lib/Driver/ToolChains/MinGW.h =================================================================== --- lib/Driver/ToolChains/MinGW.h +++ lib/Driver/ToolChains/MinGW.h @@ -63,7 +63,9 @@ bool IsIntegratedAssemblerDefault() const override; bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefault() const override; - bool isPIEDefault() const override; +#if !CLANG_ENABLE_DEFAULT_PIE + bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override; SanitizerMask getSupportedSanitizers() const override; Index: lib/Driver/ToolChains/MinGW.cpp =================================================================== --- lib/Driver/ToolChains/MinGW.cpp +++ lib/Driver/ToolChains/MinGW.cpp @@ -9,7 +9,6 @@ #include "MinGW.h" #include "InputInfo.h" #include "CommonArgs.h" -#include "clang/Config/config.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/DriverDiagnostic.h" @@ -443,8 +442,6 @@ return getArch() == llvm::Triple::x86_64; } -bool toolchains::MinGW::isPIEDefault() const { return false; } - bool toolchains::MinGW::isPICDefaultForced() const { return getArch() == llvm::Triple::x86_64; } Index: lib/Driver/ToolChains/OpenBSD.h =================================================================== --- lib/Driver/ToolChains/OpenBSD.h +++ lib/Driver/ToolChains/OpenBSD.h @@ -56,7 +56,9 @@ bool IsMathErrnoDefault() const override { return false; } bool IsObjCNonFragileABIDefault() const override { return true; } +#if !CLANG_ENABLE_DEFAULT_PIE bool isPIEDefault() const override { return true; } +#endif RuntimeLibType GetDefaultRuntimeLibType() const override { return ToolChain::RLT_CompilerRT; Index: lib/Driver/ToolChains/Solaris.cpp =================================================================== --- lib/Driver/ToolChains/Solaris.cpp +++ lib/Driver/ToolChains/Solaris.cpp @@ -8,7 +8,6 @@ #include "Solaris.h" #include "CommonArgs.h" -#include "clang/Config/config.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/DriverDiagnostic.h" Index: lib/Driver/ToolChains/TCE.h =================================================================== --- lib/Driver/ToolChains/TCE.h +++ lib/Driver/ToolChains/TCE.h @@ -27,7 +27,9 @@ bool IsMathErrnoDefault() const override; bool isPICDefault() const override; - bool isPIEDefault() const override; +#if !CLANG_ENABLE_DEFAULT_PIE + bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override; }; Index: lib/Driver/ToolChains/TCE.cpp =================================================================== --- lib/Driver/ToolChains/TCE.cpp +++ lib/Driver/ToolChains/TCE.cpp @@ -34,8 +34,6 @@ bool TCEToolChain::isPICDefault() const { return false; } -bool TCEToolChain::isPIEDefault() const { return false; } - bool TCEToolChain::isPICDefaultForced() const { return false; } TCELEToolChain::TCELEToolChain(const Driver &D, const llvm::Triple& Triple, Index: lib/Driver/ToolChains/WebAssembly.h =================================================================== --- lib/Driver/ToolChains/WebAssembly.h +++ lib/Driver/ToolChains/WebAssembly.h @@ -45,7 +45,9 @@ bool IsObjCNonFragileABIDefault() const override; bool UseObjCMixedDispatch() const override; bool isPICDefault() const override; - bool isPIEDefault() const override; +#if !CLANG_ENABLE_DEFAULT_PIE + bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override; bool IsIntegratedAssemblerDefault() const override; bool hasBlocksRuntime() const override; Index: lib/Driver/ToolChains/WebAssembly.cpp =================================================================== --- lib/Driver/ToolChains/WebAssembly.cpp +++ lib/Driver/ToolChains/WebAssembly.cpp @@ -127,8 +127,6 @@ bool WebAssembly::isPICDefault() const { return false; } -bool WebAssembly::isPIEDefault() const { return false; } - bool WebAssembly::isPICDefaultForced() const { return false; } bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; } Index: lib/Driver/ToolChains/XCore.h =================================================================== --- lib/Driver/ToolChains/XCore.h +++ lib/Driver/ToolChains/XCore.h @@ -58,7 +58,9 @@ public: bool isPICDefault() const override; - bool isPIEDefault() const override; +#if !CLANG_ENABLE_DEFAULT_PIE + bool isPIEDefault() const override { return false; } +#endif bool isPICDefaultForced() const override; bool SupportsProfiling() const override; bool hasBlocksRuntime() const override; Index: lib/Driver/ToolChains/XCore.cpp =================================================================== --- lib/Driver/ToolChains/XCore.cpp +++ lib/Driver/ToolChains/XCore.cpp @@ -100,8 +100,6 @@ bool XCoreToolChain::isPICDefault() const { return false; } -bool XCoreToolChain::isPIEDefault() const { return false; } - bool XCoreToolChain::isPICDefaultForced() const { return false; } bool XCoreToolChain::SupportsProfiling() const { return false; } Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -25,7 +25,6 @@ #include "clang/Basic/Version.h" #include "clang/Basic/Visibility.h" #include "clang/Basic/XRayInstr.h" -#include "clang/Config/config.h" #include "clang/Driver/Driver.h" #include "clang/Driver/DriverDiagnostic.h" #include "clang/Driver/Options.h"