]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/code_elim.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / code_elim.exp
CommitLineData
213516ef 1# Copyright 2002-2023 Free Software Foundation, Inc.
a04ba985
JG
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>. */
15
16# code_elim.exp -- tests that GDB can handle executables where some data/code
17# has been eliminated by the linker.
18
a04ba985
JG
19set testfile1 code_elim1
20set testfile2 code_elim2
21set srcfile1 ${testfile1}.c
22set srcfile2 ${testfile2}.c
f76495c8
TT
23set binfile1 [standard_output_file ${testfile1}]
24set binfile2 [standard_output_file ${testfile2}]
a04ba985
JG
25set opts [list debug]
26lappend opts "additional_flags=-ffunction-sections"
27lappend opts "additional_flags=-fdata-sections"
21f507ef
TV
28lappend opts "ldflags=-Wl,-gc-sections"
29lappend opts "ldflags=-Wl,-e,main"
a04ba985 30
109be305
JW
31# Place variables in .data instead of .sdata.
32if {[istarget "riscv*-*-*"]} {
33 lappend opts "additional_flags=-msmall-data-limit=0"
34}
35
a04ba985
JG
36remote_exec build "rm -f ${binfile1}"
37remote_exec build "rm -f ${binfile2}"
38
39if { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${binfile1}" executable $opts] != "" } {
5b362f04 40 untested "failed to compile"
a04ba985
JG
41 return -1
42}
43
44if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $opts] != "" } {
5b362f04 45 untested "failed to compile"
a04ba985
JG
46 return -1
47}
48
a04ba985
JG
49proc not_null_var_address { var } {
50
51 # Same as get_var_address, expect that it reports a failure if a null
52 # address is returned by gdb.
53
54 set address [get_var_address $var]
55 regexp "0x\[0-9a-fA-F\]+" $address address
56 if { "$address" == "0x0" } {
57 fail "$var has null address"
58 }
59}
60
61proc test_eliminated_var { var } {
62 global gdb_prompt hex
63
64 # Match output 'No symbol "${var}" in current context'
65
66 gdb_test_multiple "print &${var}" "test eliminated var ${var}" {
67 -re "No symbol \"${var}\" in current context\\.\[\r\n\]+${gdb_prompt} $" {
68 pass "test eliminated var ${var}"
69 }
70 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" {
71 fail "test eliminated var ${var}"
72 }
73 }
74}
75
76# Check that the code and data eliminated in binfile1 are not included
77# into partial symtab... and that non-eliminated symbols are still there.
78
79gdb_exit
80gdb_start
81
d0d86158 82gdb_test "symbol-file ${binfile1}" \
3453e7e4 83 "Reading symbols from .*${testfile1}\\.\\.\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
d0d86158 84 "symbol-file ${testfile1}"
a04ba985 85
d0d86158
JK
86with_test_prefix "single psymtabs" {
87 test_eliminated_var my_global_symbol
88 test_eliminated_var my_static_symbol
89 test_eliminated_var my_global_func
90 not_null_var_address main
91}
a04ba985
JG
92
93# Same thing for symtabs
94
95gdb_exit
96global GDBFLAGS
97set saved_gdbflags $GDBFLAGS
98set GDBFLAGS "$GDBFLAGS --readnow $binfile1"
99gdb_start
100set GDBFLAGS $saved_gdbflags
101
d0d86158
JK
102with_test_prefix "single symtabs" {
103 test_eliminated_var my_global_symbol
104 test_eliminated_var my_static_symbol
105 test_eliminated_var my_global_func
106 not_null_var_address main
107}
a04ba985
JG
108
109# binfile2 contains the symbols that have been eliminated in binfile1. Check
110# the eliminated symbols does not hide these valid ones.
111
112gdb_exit
113gdb_start
114
d0d86158 115with_test_prefix "order1" {
952cf74c 116 gdb_test "add-symbol-file ${binfile1} 0x100000 -s .bss 0x120000" \
3453e7e4 117 "Reading symbols from .*${testfile1}\\.\\.\\." \
952cf74c
JK
118 "add-symbol-file ${testfile1} 0x100000" \
119 "add symbol table from file \".*${testfile1}\" at.*\\(y or n\\) " \
120 "y"
121
122 gdb_test "add-symbol-file ${binfile2} 0x200000 -s .data 0x210000 -s .bss 0x220000" \
3453e7e4 123 "Reading symbols from .*${testfile2}\\.\\.\\." \
952cf74c
JK
124 "add-symbol-file ${testfile2} 0x200000" \
125 "add symbol table from file \".*${testfile2}\" at.*\\(y or n\\) " \
126 "y"
127
d0d86158
JK
128 not_null_var_address my_global_symbol
129 not_null_var_address my_static_symbol
130 not_null_var_address my_global_func
131 not_null_var_address main
132}
a04ba985
JG
133
134# Same thing, but loading binfile2 before binfile1.
135
136gdb_exit
137gdb_start
138
d0d86158 139with_test_prefix "order2" {
952cf74c 140 gdb_test "add-symbol-file ${binfile2} 0x200000 -s .data 0x210000 -s .bss 0x220000" \
3453e7e4 141 "Reading symbols from .*${testfile2}\\.\\.\\." \
952cf74c
JK
142 "add-symbol-file ${testfile2} 0x200000" \
143 "add symbol table from file \".*${testfile2}\" at.*\\(y or n\\) " \
144 "y"
145
146 gdb_test "add-symbol-file ${binfile1} 0x100000 -s .bss 0x120000" \
3453e7e4 147 "Reading symbols from .*${testfile1}\\.\\.\\." \
952cf74c
JK
148 "add-symbol-file ${testfile1} 0x100000" \
149 "add symbol table from file \".*${testfile1}\" at.*\\(y or n\\) " \
150 "y"
151
d0d86158
JK
152 not_null_var_address my_global_symbol
153 not_null_var_address my_static_symbol
154 not_null_var_address my_global_func
155 not_null_var_address main
156}