[PATCH 06/27] gdb/testsuite: Don't pass -fPIC on PE/COFF targets

Pedro Alves <[email protected]> Thu, 23 Jul 2026 14:00:57 +0100
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
'-fPIC' is not needed for any PE/COFF-based target.  We were passing
'-fPIC' on mingw and cygwin, and it works there because GCC and Clang
simply ignore that option for those targets (to ease portability), but
Clang for x86_64-pc-windows-msvc isn't as forgiving:

  error: unsupported option '-fPIC' for target 'x86_64-pc-windows-msvc'

Originally, gdb.exp wasn't passing -fPIC on cygwin/mingw, but it
started doing that with commit 2f4132646d8 ("[gdb/testsuite] Don't
pass -fPIC to gdb_compile_shlib").  Before that commit,
gdb_compile_shlib added -fpic on the platforms that need it, but added
nothing in some cases (cygwin, mingw and pe, and the unknown-compiler
fallback), and the individual testcases papered over that by passing
additional_flags=-fPIC unconditionally themselves.  That commit moved
the -fPIC from the testcase calls to gdb_compile_shlib, adding it in
exactly those previously-empty cases, which is how cygwin and mingw
ended up being passed -fPIC even though they don't need it.

Change-Id: I6883bc14381883eac3ee825a65ef67ed26bcadce
---
 gdb/testsuite/lib/gdb.exp | 47 ++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 25 deletions(-)

diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index db22d2bc320..25f01ed9727 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -7226,35 +7226,32 @@ proc gdb_compile_shlib_1 {sources dest options} {
 	set info_options "c"
     }
 
-    switch -glob [test_compiler_info "" ${info_options}] {
-	"xlc-*" {
-	    lappend obj_options "additional_flags=-qpic"
-	}
-	"clang-*" {
-	    if { [istarget "*-*-cygwin*"]
-		 || [istarget "*-*-mingw*"] } {
-		lappend obj_options "additional_flags=-fPIC"
-	    } else {
-		lappend obj_options "additional_flags=-fpic"
+    if { [is_pecoff_target] } {
+	# Nothing needed, PE targets always compile all code as
+	# position-independent.
+    } else {
+	switch -glob [test_compiler_info "" ${info_options}] {
+	    "xlc-*" {
+		lappend obj_options "additional_flags=-qpic"
 	    }
-	}
-	"gcc-*" {
-	    if { [istarget "powerpc*-*-aix*"]
-		   || [istarget "rs6000*-*-aix*"]
-		   || [istarget "*-*-cygwin*"]
-		   || [istarget "*-*-mingw*"]
-		   || [istarget "*-*-pe*"] } {
-		lappend obj_options "additional_flags=-fPIC"
-	    } else {
+	    "clang-*" {
 		lappend obj_options "additional_flags=-fpic"
 	    }
-	}
-	"icc-*" {
+	    "gcc-*" {
+		if { [istarget "powerpc*-*-aix*"]
+		     || [istarget "rs6000*-*-aix*"] } {
+		    lappend obj_options "additional_flags=-fPIC"
+		} else {
+		    lappend obj_options "additional_flags=-fpic"
+		}
+	    }
+	    "icc-*" {
 		lappend obj_options "additional_flags=-fpic"
-	}
-	default {
-	    # don't know what the compiler is...
-	    lappend obj_options "additional_flags=-fPIC"
+	    }
+	    default {
+		# don't know what the compiler is...
+		lappend obj_options "additional_flags=-fPIC"
+	    }
 	}
     }
 
-- 
2.54.0