[PATCH] D128333: [clang][flang] Disable defaulting to `-fpie` for LLVM Flang

Andrzej Warzynski via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 22 04:12:44 PDT 2022

awarzynski created this revision.
awarzynski added reviewers: rovka, MaskRay, schweitz, Leporacanthicus.
Herald added subscribers: jsji, StephenFan, pengfei, kristof.beyls.
Herald added a reviewer: sscalpone.
Herald added projects: Flang, All.
awarzynski requested review of this revision.
Herald added subscribers: cfe-commits, jdoerfert.
Herald added a project: clang.

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.

  rG LLVM Github Monorepo



Index: flang/test/Driver/no-pie.f90
--- /dev/null
+++ flang/test/Driver/no-pie.f90
@@ -0,0 +1,12 @@
+! Verify that in contrast to Clang, Flang does not default to generating position independent executables/code
+! RUN: %flang -### -flang-experimental-exec %S/Inputs/hello.f90 2>&1 | FileCheck %s
+! CHECK-NOT: fpie
+! CHECK-NOT: fpic
Index: clang/lib/Driver/ToolChains/Linux.cpp
--- clang/lib/Driver/ToolChains/Linux.cpp
+++ clang/lib/Driver/ToolChains/Linux.cpp
@@ -699,8 +699,9 @@
 bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const {
-  return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() ||
-         getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE();
+  return !getDriver().IsFlangMode() &&
+         (CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() ||
+          getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE());
 bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const {

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128333.438967.patch
Type: text/x-patch
Size: 1199 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220622/835e431b/attachment.bin>

More information about the cfe-commits mailing list