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 <[email protected]> Date: Thu, 23 Oct 2025 14:41:02 -0600 Subject: [PATCH 1/4] [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 <[email protected]> Date: Sun, 26 Oct 2025 12:09:18 -0600 Subject: [PATCH 2/4] 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 <[email protected]> Date: Mon, 27 Oct 2025 09:15:42 -0600 Subject: [PATCH 3/4] 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 >From 5c0ff19192b30a0086056d5fffc62def9cb80434 Mon Sep 17 00:00:00 2001 From: Tarun Prabhu <[email protected]> Date: Mon, 27 Oct 2025 11:27:45 -0600 Subject: [PATCH 4/4] Add more tests for the defaults and expected behavior on a range of platforms --- flang/test/Driver/linker-options.f90 | 92 ++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 6 deletions(-) diff --git a/flang/test/Driver/linker-options.f90 b/flang/test/Driver/linker-options.f90 index a3784f9f912e2..82201e3d4afea 100644 --- a/flang/test/Driver/linker-options.f90 +++ b/flang/test/Driver/linker-options.f90 @@ -10,15 +10,95 @@ ! Make sure that `-L' is "visible" to Flang's driver ! RUN: %flang -L/ -### %s +! ------------------------------------------------------------------------------ ! 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 -! 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 +! the correct option is added to the link line. +! +! Last match "wins" +! RUN: %flang -target x86_64-pc-linux-gnu -pie -no-pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target x86_64-pc-linux-gnu -no-pie -pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=PIE +! RUN: %flang -target x86_64-pc-linux-gnu -pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=PIE +! RUN: %flang -target x86_64-pc-linux-gnu -no-pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! +! Ensure that "-pie" is passed to the linker. +! RUN: %flang -target i386-unknown-freebsd -pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=PIE +! RUN: %flang -target aarch64-pc-linux-gnu -pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=PIE +! +! On Musl Linux, PIE is enabled by default, but can be disabled. +! RUN: %flang -target x86_64-linux-musl -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=PIE +! RUN: %flang -target i686-linux-musl -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=PIE +! RUN: %flang -target armv6-linux-musleabihf %s -### 2>&1 \ +! RUN: | FileCheck %s --check-prefix=PIE +! RUN: %flang -target armv7-linux-musleabihf %s -### 2>&1 \ +! RUN: | FileCheck %s --check-prefix=PIE +! RUN: %flang --target=x86_64-linux-musl -no-pie -### 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! +! On OpenBSD, -pie is not passed to the linker, but can be forced. +! RUN: %flang -target amd64-pc-openbsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target i386-pc-openbsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target aarch64-unknown-openbsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target arm-unknown-openbsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target powerpc-unknown-openbsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target sparc64-unknown-openbsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target i386-pc-openbsd -pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=PIE +! +! On FreeBSD, -pie is not passed to the linker, but can be forced. +! RUN: %flang -target amd64-pc-freebsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target i386-pc-freebsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target aarch64-unknown-freebsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target arm-unknown-freebsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target powerpc-unknown-freebsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target sparc64-unknown-freebsd -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target i386-pc-freebsd -pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=PIE +! +! On AIX, -pie is never passed to the linker. +! RUN: %flang -target powerpc64-unknown-aix -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target powerpc64-unknown-aix -pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target powerpc64-unknown-aix -no-pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! +! On MinGW and Windows, -pie may be specified, but it is ignored. +! RUN: %flang -target aarch64-pc-windows-gnu -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target x86_64-pc-windows-gnu -pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target i686-pc-windows-gnu -no-pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target aarch64-windows-msvc -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target aarch64-windows-msvc -pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: %flang -target aarch64-windows-msvc -no-pie -### %s 2>&1 \ +! RUN: | FileCheck %s --check-prefix=NO-PIE +! ! PIE: "-pie" ! NO-PIE-NOT: "-pie" +! ------------------------------------------------------------------------------ program hello write(*,*), "Hello world!" _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
