From 81802aa576c3f66aac5a21acc35adda4b1ee3a0d Mon Sep 17 00:00:00 2001 From: Tarun Prabhu Date: Thu, 23 Oct 2025 14:41:02 -0600 Subject: [PATCH 1/5] [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 8784c9d7d206d..ed5ff4d7ec5b9 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, NegFlag, BothFlags<[], [ClangOption, CC1Option, FlangOption, FC1Option]>>; -def pie : Flag<["-"], "pie">, Group; def static_pie : Flag<["-"], "static-pie">, Group; def read__only__relocs : Separate<["-"], "read_only_relocs">; def remap : Flag<["-"], "remap">; @@ -6508,6 +6506,8 @@ def fpic : Flag<["-"], "fpic">, Group; def fno_pic : Flag<["-"], "fno-pic">, Group; def fpie : Flag<["-"], "fpie">, Group; def fno_pie : Flag<["-"], "fno-pie">, Group; +def pie : Flag<["-"], "pie">, 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 312a7eed4bdd18ca5409f71fd166bcc33b5cf9a5 Mon Sep 17 00:00:00 2001 From: Tarun Prabhu Date: Sun, 26 Oct 2025 12:09:18 -0600 Subject: [PATCH 2/5] 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 ed5ff4d7ec5b9..cb5cb888c6da7 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; def fpie : Flag<["-"], "fpie">, Group; def fno_pie : Flag<["-"], "fno-pie">, Group; def pie : Flag<["-"], "pie">, Group; -def no_pie : Flag<["-"], "no-pie">; +def no_pie : Flag<["-"], "no-pie">, 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 94722a305a1f7f77bd5e2c6bb2fa1e5c300c768b Mon Sep 17 00:00:00 2001 From: Tarun Prabhu Date: Mon, 27 Oct 2025 09:15:42 -0600 Subject: [PATCH 3/5] 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 3f79259614798c8d810dd09b818b3dfe42d52adc Mon Sep 17 00:00:00 2001 From: Tarun Prabhu Date: Mon, 27 Oct 2025 11:27:45 -0600 Subject: [PATCH 4/5] 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!" From 283cf814c5bc739438a2d3b57f86913f31e84b71 Mon Sep 17 00:00:00 2001 From: Tarun Prabhu Date: Tue, 28 Oct 2025 12:54:02 -0600 Subject: [PATCH 5/5] Also check for an unused argument warning on certain platforms --- flang/test/Driver/linker-options.f90 | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/flang/test/Driver/linker-options.f90 b/flang/test/Driver/linker-options.f90 index 82201e3d4afea..07f967b4bac5d 100644 --- a/flang/test/Driver/linker-options.f90 +++ b/flang/test/Driver/linker-options.f90 @@ -76,28 +76,29 @@ ! ! 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: | FileCheck %s --check-prefixes=NO-PIE ! RUN: %flang -target powerpc64-unknown-aix -pie -### %s 2>&1 \ -! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: | FileCheck %s --check-prefixes=NO-PIE,UNUSED ! RUN: %flang -target powerpc64-unknown-aix -no-pie -### %s 2>&1 \ -! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: | FileCheck %s --check-prefixes=NO-PIE,UNUSED ! ! 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: | FileCheck %s --check-prefixes=NO-PIE ! RUN: %flang -target x86_64-pc-windows-gnu -pie -### %s 2>&1 \ -! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: | FileCheck %s --check-prefixes=NO-PIE,UNUSED ! RUN: %flang -target i686-pc-windows-gnu -no-pie -### %s 2>&1 \ -! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: | FileCheck %s --check-prefixes=NO-PIE,UNUSED ! RUN: %flang -target aarch64-windows-msvc -### %s 2>&1 \ -! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: | FileCheck %s --check-prefixes=NO-PIE ! RUN: %flang -target aarch64-windows-msvc -pie -### %s 2>&1 \ -! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: | FileCheck %s --check-prefixes=NO-PIE,UNUSED ! RUN: %flang -target aarch64-windows-msvc -no-pie -### %s 2>&1 \ -! RUN: | FileCheck %s --check-prefix=NO-PIE +! RUN: | FileCheck %s --check-prefixes=NO-PIE,UNUSED ! ! PIE: "-pie" ! NO-PIE-NOT: "-pie" +! UNUSED: warning: argument unused during compilation: '{{(-no)?}}-pie' ! ------------------------------------------------------------------------------ program hello