[v4,2/2] `add-symbol-file`: add test for mixing -o and -s

Message ID 20260926064742.29327-3-dragorn421@gmail.com
State New
Headers
Series Fix `add-symbol-file -o ... -s ...` address mapping bug |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Dragorn421 Sept. 26, 2026, 6:47 a.m. UTC
  The test makes use of a built .plf (partially linked file) file,
which is needed to exhibit the previously fixed bug, as unlinked
.o object files only have 0 as address for their sections.
---
 gdb/testsuite/gdb.base/relocate_linked.c   | 28 +++++++++
 gdb/testsuite/gdb.base/relocate_linked.exp | 71 ++++++++++++++++++++++
 2 files changed, 99 insertions(+)
 create mode 100644 gdb/testsuite/gdb.base/relocate_linked.c
 create mode 100644 gdb/testsuite/gdb.base/relocate_linked.exp
  

Patch

diff --git a/gdb/testsuite/gdb.base/relocate_linked.c b/gdb/testsuite/gdb.base/relocate_linked.c
new file mode 100644
index 00000000000..ac48ea28dc0
--- /dev/null
+++ b/gdb/testsuite/gdb.base/relocate_linked.c
@@ -0,0 +1,28 @@ 
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2026 Free Software Foundation, Inc.
+
+   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/>.  */
+
+int
+text ()
+{
+  return 0;
+}
+
+int data = 1;
+
+const int rodata = 2;
+
+int bss;
diff --git a/gdb/testsuite/gdb.base/relocate_linked.exp b/gdb/testsuite/gdb.base/relocate_linked.exp
new file mode 100644
index 00000000000..6759ef04746
--- /dev/null
+++ b/gdb/testsuite/gdb.base/relocate_linked.exp
@@ -0,0 +1,71 @@ 
+# Copyright 2010-2026 Free Software Foundation, Inc.
+
+# 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/>.  */
+
+# relocate_linked.exp -- Expect script to test loading symbols from linked
+#		  relocatable object files (also known as partially linked files).
+
+standard_testfile .c
+
+# plf stands for "partially linked file"
+remote_exec build "rm -f ${binfile}.o ${binfile}.plf"
+if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}.o" object {debug}] != "" } {
+    untested "failed to compile"
+    return
+}
+
+set board [target_info name]
+if {[board_info $board exists mathlib]} {
+    set saved_mathlib [board_info $board mathlib]
+    set_board_info mathlib ""
+    set err [gdb_compile "${binfile}.o" "${binfile}.plf" executable \
+		 {additional_flags=-nostdlib ldflags=-Wl,-r}]
+    set_board_info mathlib $saved_mathlib
+} else {
+    set_board_info mathlib ""
+    set err [gdb_compile "${binfile}.o" "${binfile}.plf" executable \
+		 {additional_flags=-nostdlib ldflags=-Wl,-r}]
+    unset_board_info mathlib
+}
+if { $err != "" } {
+    untested "failed to partially link"
+    return
+}
+
+# Check that the -s address is respected when -o is used.
+clean_restart
+set offset 0xff000000
+set offset_re 0x0*ff000000
+set data 0x12340
+set data_re 0x0*12340
+set saw_data_section_with_correct_address 0
+gdb_test_multiple "add-symbol-file $binfile.plf -o $offset -s .data $data" \
+    "add-symbol-file with offset and explicit data section address" {
+    -re "add symbol table from file .*${testfile}\\.plf.*at\[ \\t\\r\\n\]+\\.data_addr = ${data_re}\[\\r\\n\]+with other sections offset by ${offset_re}\[\\r\\n\]+\\(y or n\\) " {
+	send_gdb "y\n"
+	exp_continue
+    }
+    -re "Reading symbols from .*\\.\\.\\.\\r\\n$gdb_prompt " {
+	send_gdb "maint info sections -all-objects\n"
+	exp_continue
+    }
+    -re "$data_re->$hex .*: \\.data" {
+	set saw_data_section_with_correct_address 1
+	exp_continue
+    }
+    -re -wrap "" {
+	gdb_assert { $saw_data_section_with_correct_address } \
+	    "explicit .data section address is preserved"
+    }
+}