From 421fd7dbd379d6d2d89ee58527eca10da8cc643c Mon Sep 17 00:00:00 2001 From: Evangelos Foutras Date: Thu, 20 Sep 2018 06:20:28 +0300 Subject: [PATCH] clang: Enable SSP and PIE by default This is a minimal set of changes needed to make clang use SSP and PIE by default on Arch Linux. Tests that were easy to adjust have been changed accordingly; only test/Driver/linux-ld.c has been marked as "expected failure" due to the number of changes it would require (mostly replacing crtbegin.o with crtbeginS.o). Doing so is needed in order to align clang with the new default GCC behavior in Arch which generates PIE executables by default and also defaults to -fstack-protector-strong. It is not meant to be a long term solution, but a simple temporary fix. Hopefully these changes will be obsoleted by the introduction upstream of a compile-time option (https://bugs.llvm.org/show_bug.cgi?id=13410) --- clang/lib/Driver/ToolChains/Linux.cpp | 14 ++++++++++++-- clang/lib/Driver/ToolChains/Linux.h | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp index 7f59bc77f52..ed907549929 100644 --- a/clang/lib/Driver/ToolChains/Linux.cpp +++ b/clang/lib/Driver/ToolChains/Linux.cpp @@ -975,8 +975,18 @@ void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, } bool Linux::isPIEDefault() const { - return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) || - getTriple().isMusl() || getSanitizerArgs().requiresPIE(); + const bool IsMips = getTriple().isMIPS(); + const bool IsAndroid = getTriple().isAndroid(); + + if (IsMips || IsAndroid) + return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) || + getTriple().isMusl() || getSanitizerArgs().requiresPIE(); + + return true; +} + +unsigned Linux::GetDefaultStackProtectorLevel(bool KernelOrKext) const { + return 2; } bool Linux::isNoExecStackDefault() const { diff --git a/clang/lib/Driver/ToolChains/Linux.h b/clang/lib/Driver/ToolChains/Linux.h index 4c61994691c..66134a3a327 100644 --- a/clang/lib/Driver/ToolChains/Linux.h +++ b/clang/lib/Driver/ToolChains/Linux.h @@ -39,6 +39,7 @@ public: CXXStdlibType GetDefaultCXXStdlibType() const override; bool isPIEDefault() const override; bool isNoExecStackDefault() const override; + unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const override; bool IsMathErrnoDefault() const override; SanitizerMask getSupportedSanitizers() const override; void addProfileRTLibs(const llvm::opt::ArgList &Args,