[clang] [flang] [flang][Driver] Enable -pie and -no-pie in flang's driver (PR #164890)

Tarun Prabhu via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 27 08:18:12 PDT 2025


https://github.com/tarunprabhu updated https://github.com/llvm/llvm-project/pull/164890

>From 18c52f9466e457ee87b7d548b914efa1aba9a3b5 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Thu, 23 Oct 2025 14:41:02 -0600
Subject: [PATCH 1/3] [flang][Driver] Enable -pie and -no-pie in flang's driver

Passing -pie to flang will pass the flag on to the linker. Passing -no-pie will
ensure that -pie is *not* passed to the linker. This behavior is consistent with
both clang and gfortran.

Fixes #159970
---
 clang/include/clang/Driver/Options.td |  4 ++--
 flang/test/Driver/misc-flags.f90      | 10 ++++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index ef1c8758705f4..9c215564ae5ae 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5999,7 +5999,6 @@ def nofixprebinding : Flag<["-"], "nofixprebinding">;
 def nolibc : Flag<["-"], "nolibc">;
 def nomultidefs : Flag<["-"], "nomultidefs">;
 def nopie : Flag<["-"], "nopie">, Visibility<[ClangOption, FlangOption]>, Flags<[TargetSpecific]>; // OpenBSD
-def no_pie : Flag<["-"], "no-pie">, Visibility<[ClangOption, FlangOption]>;
 def noprebind : Flag<["-"], "noprebind">;
 def noprofilelib : Flag<["-"], "noprofilelib">;
 def noseglinkedit : Flag<["-"], "noseglinkedit">;
@@ -6113,7 +6112,6 @@ defm pthread : BoolOption<"", "pthread",
   PosFlag<SetTrue, [], [ClangOption], "Support POSIX threads in generated code">,
   NegFlag<SetFalse>,
   BothFlags<[], [ClangOption, CC1Option, FlangOption, FC1Option]>>;
-def pie : Flag<["-"], "pie">, Group<Link_Group>;
 def static_pie : Flag<["-"], "static-pie">, Group<Link_Group>;
 def read__only__relocs : Separate<["-"], "read_only_relocs">;
 def remap : Flag<["-"], "remap">;
@@ -6508,6 +6506,8 @@ def fpic : Flag<["-"], "fpic">, Group<f_Group>;
 def fno_pic : Flag<["-"], "fno-pic">, Group<f_Group>;
 def fpie : Flag<["-"], "fpie">, Group<f_Group>;
 def fno_pie : Flag<["-"], "fno-pie">, Group<f_Group>;
+def pie : Flag<["-"], "pie">, Group<Link_Group>;
+def no_pie : Flag<["-"], "no-pie">;
 
 } // let Vis = [Default, FlangOption]
 
diff --git a/flang/test/Driver/misc-flags.f90 b/flang/test/Driver/misc-flags.f90
index 61d763c5b64dd..e594c9cb50517 100644
--- a/flang/test/Driver/misc-flags.f90
+++ b/flang/test/Driver/misc-flags.f90
@@ -10,6 +10,16 @@
 ! Make sure that `-L' is "visible" to Flang's driver
 ! RUN: %flang -L/ -### %s
 
+! Check that '-pie' is "visible" to Flang's driver and is passed on to the
+! linker.
+! RUN: %flang -pie -### %s 2>&1 | FileCheck %s --check-prefix=PIE
+! PIE: "-pie"
+
+! Check that '-no-pie' is "visible" to Flang's driver and that "-pie" is *not*
+! passed to the linker.
+! RUN: %flang -no-pie -### %s 2>&1 | FileCheck %s --check-prefix=NO-PIE
+! NO-PIE-NOT: "-pie"
+
 program hello
   write(*,*), "Hello world!"
 end program hello

>From e59cbbbf5f48598c14eb3f0cbb3fe20e49a8e003 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun.prabhu at gmail.com>
Date: Sun, 26 Oct 2025 12:09:18 -0600
Subject: [PATCH 2/3] Address reviewer comments

---
 clang/include/clang/Driver/Options.td                    | 2 +-
 flang/test/Driver/{misc-flags.f90 => linker-options.f90} | 0
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename flang/test/Driver/{misc-flags.f90 => linker-options.f90} (100%)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 9c215564ae5ae..a3f3c903e4682 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6507,7 +6507,7 @@ def fno_pic : Flag<["-"], "fno-pic">, Group<f_Group>;
 def fpie : Flag<["-"], "fpie">, Group<f_Group>;
 def fno_pie : Flag<["-"], "fno-pie">, Group<f_Group>;
 def pie : Flag<["-"], "pie">, Group<Link_Group>;
-def no_pie : Flag<["-"], "no-pie">;
+def no_pie : Flag<["-"], "no-pie">, Group<Link_Group>;
 
 } // let Vis = [Default, FlangOption]
 
diff --git a/flang/test/Driver/misc-flags.f90 b/flang/test/Driver/linker-options.f90
similarity index 100%
rename from flang/test/Driver/misc-flags.f90
rename to flang/test/Driver/linker-options.f90

>From feca43d7eb1478d063b10ec04e8b3138cdea6c3d Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Mon, 27 Oct 2025 09:15:42 -0600
Subject: [PATCH 3/3] Add tests where -no-pie and -pie are both present. Also
 add tests for the default

---
 flang/test/Driver/linker-options.f90 | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/flang/test/Driver/linker-options.f90 b/flang/test/Driver/linker-options.f90
index e594c9cb50517..a3784f9f912e2 100644
--- a/flang/test/Driver/linker-options.f90
+++ b/flang/test/Driver/linker-options.f90
@@ -10,14 +10,14 @@
 ! Make sure that `-L' is "visible" to Flang's driver
 ! RUN: %flang -L/ -### %s
 
-! Check that '-pie' is "visible" to Flang's driver and is passed on to the
-! linker.
+! Check that '-pie' and '-no-pie' are "visible" to Flang's driver. Check that
+! the correct option is added to the link line. The default is '-pie'.
+! RUN: %flang -### %s 2>&1 | FileCheck %s --check-prefix=PIE
 ! RUN: %flang -pie -### %s 2>&1 | FileCheck %s --check-prefix=PIE
-! PIE: "-pie"
-
-! Check that '-no-pie' is "visible" to Flang's driver and that "-pie" is *not*
-! passed to the linker.
 ! RUN: %flang -no-pie -### %s 2>&1 | FileCheck %s --check-prefix=NO-PIE
+! RUN: %flang -pie -no-pie -### %s 2>&1 | FileCheck %s --check-prefix=NO-PIE
+! RUN: %flang -no-pie -pie -### %s 2>&1 | FileCheck %s --check-prefix=PIE
+! PIE: "-pie"
 ! NO-PIE-NOT: "-pie"
 
 program hello



More information about the cfe-commits mailing list