Skip to content

Conversation

@tarunprabhu
Copy link
Contributor

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

@llvmbot llvmbot added clang Clang issues not falling into any other category flang:driver flang Flang issues not falling into any other category labels Oct 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 23, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-flang-driver

Author: Tarun Prabhu (tarunprabhu)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/164890.diff

2 Files Affected:

  • (modified) clang/include/clang/Driver/Options.td (+2-2)
  • (modified) flang/test/Driver/misc-flags.f90 (+10)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 0c9584f1b479f..40ca2f9717d55 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

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">;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Group<Link_Group>

@tarunprabhu
Copy link
Contributor Author

Per @MaskRay's comment, I have changed the name of flang/test/Driver/misc-flags.f90 to flang/test/Driver/linker-options.f90. There is another test in flang/test/Driver named linker-flags.f90. Looking at the contents of that file, I think renaming it link-libs.f90 would be good. However, since that is unrelated, it is not done in this PR.

Copy link
Contributor

@kiranchandramohan kiranchandramohan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG.

Copy link
Contributor

@kiranchandramohan kiranchandramohan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG

@tarunprabhu
Copy link
Contributor Author

Following investigation of the failure of pre-merge CI, I found that the tests were likely to fail on certain platforms. A number of tests have been added that check the behavior of -pie on various platforms.

@DavidTruby, could you take a look at the Windows tests and see if they look reasonable? They pass for me, but I want to be certain that I have not made any bad assumptions.

@DanielCChen, @kkwli, could you do the same for AIX?

@DanielCChen
Copy link
Contributor

Following investigation of the failure of pre-merge CI, I found that the tests were likely to fail on certain platforms. A number of tests have been added that check the behavior of -pie on various platforms.

@DavidTruby, could you take a look at the Windows tests and see if they look reasonable? They pass for me, but I want to be certain that I have not made any bad assumptions.

@DanielCChen, @kkwli, could you do the same for AIX?

I got error when applying the patch as

> git apply ~/temp/164890.patch
error: patch failed: flang/test/Driver/linker-options.f90:10
error: flang/test/Driver/linker-options.f90: patch does not apply
error: patch failed: flang/test/Driver/linker-options.f90:10
error: flang/test/Driver/linker-options.f90: patch does not apply

Even though flang/test/Driver/misc-flags.f90 is deleted, it still showed up in the patch.

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!"

Copy link
Contributor

@DanielCChen DanielCChen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test passed on Both AIX and LoP.
Thanks.

@tarunprabhu
Copy link
Contributor Author

The test passed on Both AIX and LoP. Thanks.

Thanks for checking Daniel. And apologies for not responding to your first question about the patch not applying. I got tangled up with other things.

Copy link
Member

@DavidTruby DavidTruby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

tarunprabhu and others added 4 commits October 28, 2025 12:39
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 llvm#159970
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category flang:driver flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[flang] allow to pass -pie to flang driver

6 participants