[PATCH 15/27] gdb.base/shreloc.exp: Use gdb_compile text_segment to set image base

Pedro Alves <[email protected]> Thu, 23 Jul 2026 14:01:06 +0100
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
The gdb.base/shreloc.exp testcase wants its two shared libraries to
have the same preferred image base, so that when GDB mishandles a
relocated library its symbols collapse onto the other's and the
check_different tests catch it.  On cygwin/mingw, to guarantee this,
it passes -Wl,--image-base directly.

Express the intent through gdb_compile's text_segment option instead
of open-coding the linker flag, and gate it on is_pecoff_target so it
is done on all PE targets, including windows-msvc.

This is mainly to avoid istarget special-casing, as in practice
lld-link already gives every DLL the same fixed default base, there is
no equivalent of --enable-auto-image-base there.

The second, cygwin/mingw-only block that checks the
"_minor_os_version__" absolute symbol is left as is.  That symbol is
specific to GNU ld's PE output, so the check does not apply to
windows-msvc.  That will change in a follow patch, though.

Change-Id: I002ed69603804c1ef6f18c0aa7b563e6bd80941b
---
 gdb/testsuite/gdb.base/shreloc.exp | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/gdb/testsuite/gdb.base/shreloc.exp b/gdb/testsuite/gdb.base/shreloc.exp
index e0efd61bcdf..718b1ca81ad 100644
--- a/gdb/testsuite/gdb.base/shreloc.exp
+++ b/gdb/testsuite/gdb.base/shreloc.exp
@@ -38,8 +38,15 @@ set lib2_sl [standard_output_file shreloc2.sl]
 set lib_opts {debug shlib}
 set exec_opts [list debug shlib=$lib1_sl shlib=$lib2_sl]
 
-if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } {
-    lappend lib_opts "ldflags=-Wl,--image-base,0x04000000"
+if { [is_pecoff_target] } {
+    # Force both shared libraries to the same image base so that they
+    # overlap and one of them gets relocated at load time, which is
+    # what this test exercises.  Without this, they would not
+    # necessarily overlap: some MinGW ld distributions default to
+    # --enable-auto-image-base, which derives each DLL's base from a
+    # hash of its file name, giving the two libraries different bases.
+    # Pinning the base defeats that.
+    lappend lib_opts "text_segment=0x04000000"
 }
 
 if {[test_compiler_info "xlc-*"]} {
-- 
2.54.0