From b405407a489902c0acfcf936bfda9821a1deb170 Mon Sep 17 00:00:00 2001 From: Andrzej Warzynski Date: Wed, 22 Jun 2022 10:29:57 +0000 Subject: [PATCH] [clang][flang] Disable defaulting to `-fpie` for LLVM Flang In, https://reviews.llvm.org/D120305, CLANG_DEFAULT_PIE_ON_LINUX was set to `On` by default. However, neither `-fpie` nor `-fpic` are currently supported in LLVM Flang. Hence, in this patch the behaviour controlled with CLANG_DEFAULT_PIE_ON_LINUX is refined not to apply to Flang. Another way to look at this is that CLANG_DEFAULT_PIE_ON_LINUX is currently affecting both Clang and Flang. IIUC, the intention for this CMake variable has always been to only affect Clang. This patch makes sure that that's the case. Without this change, you might see errors like this on X86_64: ``` /usr/bin/ld: main.o: relocation R_X86_64_32 against `.bss' can not be used when making a PIE object; recompile with -fPIC ``` I've not experienced any issues on AArch64. That's probably because on AArch64 some object files happen to be position independent without needing -fpie or -fpic. Differential Revision: https://reviews.llvm.org/D128333 --- clang/lib/Driver/ToolChains/Linux.cpp | 7 +++++-- flang/docs/ReleaseNotes.md | 7 +++++++ flang/test/Driver/pic-flags.f90 | 24 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 flang/test/Driver/pic-flags.f90 diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp index 4309b10..ceb1a98 100644 --- a/clang/lib/Driver/ToolChains/Linux.cpp +++ b/clang/lib/Driver/ToolChains/Linux.cpp @@ -699,8 +699,11 @@ void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, } bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const { - return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() || - getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE(); + // TODO: Remove the special treatment for Flang once its frontend driver can + // generate position independent code. + return !getDriver().IsFlangMode() && + (CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() || + getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE()); } bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const { diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md index b41a2aa..38d5c3f 100644 --- a/flang/docs/ReleaseNotes.md +++ b/flang/docs/ReleaseNotes.md @@ -28,6 +28,13 @@ page](https://llvm.org/releases/). ## Non-comprehensive list of changes in this release * The bash wrapper script, `flang`, is renamed as `flang-to-external-fc`. +* In contrast to Clang, Flang will not default to using `-fpie` when linking + executables. This is only a temporary solution and the goal is to align with + Clang in the near future. First, however, the frontend driver needs to be + extended so that it can generate position independent code (that requires + adding support for e.g. `-fpic` and `-mrelocation-model` in `flang-new + -fc1`). Once that is available, support for the `-fpie` can officially be + added and the default behaviour updated. ## New Compiler Flags * Refined how `-f{no-}color-diagnostics` is treated to better align with Clang. diff --git a/flang/test/Driver/pic-flags.f90 b/flang/test/Driver/pic-flags.f90 new file mode 100644 index 0000000..4e6b979 --- /dev/null +++ b/flang/test/Driver/pic-flags.f90 @@ -0,0 +1,24 @@ +! Verify that in contrast to Clang, Flang does not default to generating position independent executables/code + +!------------- +! RUN COMMANDS +!------------- +! RUN: %flang -### %s --target=aarch64-linux-gnu 2>&1 | FileCheck %s --check-prefix=CHECK-NOPIE +! RUN: %flang -### %s --target=aarch64-linux-gnu -fno-pie 2>&1 | FileCheck %s --check-prefix=CHECK-NOPIE + +! RUN: %flang -### %s --target=aarch64-linux-gnu -fpie 2>&1 | FileCheck %s --check-prefix=CHECK-PIE + +!---------------- +! EXPECTED OUTPUT +!---------------- +! CHECK-NOPIE: "-fc1" +! CHECk-NOPIE-NOT: "-fpic" +! CHECK-NOPIE: "{{.*}}ld" +! CHECK-NOPIE-NOT: "-pie" + +! CHECK-PIE: "-fc1" +!! TODO Once Flang supports `-fpie`, it //should// use -fpic when invoking `flang -fc1`. Update the following line once `-fpie` is +! available. +! CHECk-PIE-NOT: "-fpic" +! CHECK-PIE: "{{.*}}ld" +! CHECK-PIE-NOT: "-pie" -- 2.7.4