[PATCH 17/27] gdb.base/shreloc.exp: Test absolute symbols portably

Pedro Alves <[email protected]> Thu, 23 Jul 2026 14:01:08 +0100
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
gdb.base/shreloc.exp checks that an absolute symbol -- one whose value
is a plain constant, not tied to any section -- is not adjusted when
the library it lives in is relocated at load time.  It does this by
looking at _minor_os_version__, an absolute symbol that GNU ld's PE
emulation injects into every image.  That symbol only exists with GNU
ld, so the check is gated on cygwin/mingw and does not run anywhere
else.

Generalize the test by doing what the existing comment already
suggests: include an absolute symbol via the assembler instead.

This drops the last istarget check from the test, runs the
absolute-symbol check on every target rather than just cygwin/mingw,
and also covers windows-msvc, where _minor_os_version__ does not exist
(lld-link does not emit it).

While at it, convert the check to gdb_assert.

Change-Id: Ife2cc8f59d9e121661c125e08be21b221db860c9
---
 gdb/testsuite/gdb.base/shreloc.exp | 20 ++++++------------
 gdb/testsuite/gdb.base/shreloc.h   | 33 ++++++++++++++++++++++++++++++
 gdb/testsuite/gdb.base/shreloc1.c  |  2 ++
 gdb/testsuite/gdb.base/shreloc2.c  |  2 ++
 4 files changed, 43 insertions(+), 14 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/shreloc.h

diff --git a/gdb/testsuite/gdb.base/shreloc.exp b/gdb/testsuite/gdb.base/shreloc.exp
index 718b1ca81ad..6c884f23fad 100644
--- a/gdb/testsuite/gdb.base/shreloc.exp
+++ b/gdb/testsuite/gdb.base/shreloc.exp
@@ -224,17 +224,9 @@ if {[send_gdb_discard "maint print msymbols ${msymfile}"]} {
     }
 }
 
-if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } {
-    #
-    # We know the names of some absolute symbols included in the
-    # portable-executable (DLL) format. Check that they didn't get
-    # relocated.
-    #
-    # A better approach would be include absolute symbols via the assembler.
-    #
-    if {[check_same "_minor_os_version__" "${msymfile}"]} {
-	pass "absolute symbols not relocated"
-    } else {
-	fail "absolute symbols not relocated"
-    }
-}
+# Both libraries define 'shreloc_abs_sym' as an absolute symbol with
+# the same value (see shreloc.h).  An absolute symbol is not tied to a
+# section, so it must not be adjusted when the library is relocated.
+# Check that its value is reported the same for both libraries.
+gdb_assert {[check_same "shreloc_abs_sym" "${msymfile}"]} \
+    "absolute symbols not relocated"
diff --git a/gdb/testsuite/gdb.base/shreloc.h b/gdb/testsuite/gdb.base/shreloc.h
new file mode 100644
index 00000000000..cc283a85ce3
--- /dev/null
+++ b/gdb/testsuite/gdb.base/shreloc.h
@@ -0,0 +1,33 @@
+/* Copyright 2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_TESTSUITE_GDB_BASE_SHRELOC_H
+#define GDB_TESTSUITE_GDB_BASE_SHRELOC_H
+
+/* An absolute symbol.  Its value is a plain constant, not tied to any
+   section, so it must not be adjusted when the library is relocated
+   at load time.  Included by both libraries so the test can confirm
+   that GDB reports the same value for both.
+
+   The name is emitted literally without the target's user-label
+   prefix (e.g. a leading '_' on some formats).  The test greps for
+   the exact name, so it must be identical on every target.  This is
+   OK because the symbol is never referenced from C.  */
+asm (".globl shreloc_abs_sym\n\t"
+     "shreloc_abs_sym = 0x4000");
+
+#endif /* GDB_TESTSUITE_GDB_BASE_SHRELOC_H */
diff --git a/gdb/testsuite/gdb.base/shreloc1.c b/gdb/testsuite/gdb.base/shreloc1.c
index 6a228f85853..848ffaab4d4 100644
--- a/gdb/testsuite/gdb.base/shreloc1.c
+++ b/gdb/testsuite/gdb.base/shreloc1.c
@@ -1,3 +1,5 @@
+#include "shreloc.h"
+
 #if defined(_WIN32) || defined(__CYGWIN__)
 # define ATTRIBUTES __attribute((__dllexport__))
 #else
diff --git a/gdb/testsuite/gdb.base/shreloc2.c b/gdb/testsuite/gdb.base/shreloc2.c
index 1a9b48bd13d..521c4094c80 100644
--- a/gdb/testsuite/gdb.base/shreloc2.c
+++ b/gdb/testsuite/gdb.base/shreloc2.c
@@ -1,3 +1,5 @@
+#include "shreloc.h"
+
 #if defined(_WIN32) || defined(__CYGWIN__)
 # define ATTRIBUTES __attribute((__dllexport__))
 #else
-- 
2.54.0