1 # Copyright 2002-2018 Free Software Foundation, Inc.
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.
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.
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/>. */
16 # code_elim.exp -- tests that GDB can handle executables where some data/code
17 # has been eliminated by the linker.
19 set testfile1 code_elim1
20 set testfile2 code_elim2
21 set srcfile1 ${testfile1}.c
22 set srcfile2 ${testfile2}.c
23 set binfile1 [standard_output_file ${testfile1}]
24 set binfile2 [standard_output_file ${testfile2}]
26 lappend opts "additional_flags=-ffunction-sections"
27 lappend opts "additional_flags=-fdata-sections"
28 lappend opts "additional_flags=-Wl,-gc-sections"
29 lappend opts "additional_flags=-Wl,-e,main"
31 # Place variables in .data instead of .sdata.
32 if {[istarget "riscv*-*-*"]} {
33 lappend opts "additional_flags=-msmall-data-limit=0"
36 remote_exec build "rm -f ${binfile1}"
37 remote_exec build "rm -f ${binfile2}"
39 if { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${binfile1}" executable $opts] != "" } {
40 untested "failed to compile"
44 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $opts] != "" } {
45 untested "failed to compile"
49 proc not_null_var_address { var } {
51 # Same as get_var_address, expect that it reports a failure if a null
52 # address is returned by gdb.
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"
61 proc test_eliminated_var { var } {
64 # Match output 'No symbol "${var}" in current context'
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}"
70 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" {
71 fail "test eliminated var ${var}"
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.
82 gdb_test "symbol-file ${binfile1}" \
83 "Reading symbols from .*${testfile1}\\.\\.\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
84 "symbol-file ${testfile1}"
86 with_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
93 # Same thing for symtabs
97 set saved_gdbflags $GDBFLAGS
98 set GDBFLAGS "$GDBFLAGS --readnow $binfile1"
100 set GDBFLAGS $saved_gdbflags
102 with_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
109 # binfile2 contains the symbols that have been eliminated in binfile1. Check
110 # the eliminated symbols does not hide these valid ones.
115 with_test_prefix "order1" {
116 gdb_test "add-symbol-file ${binfile1} 0x100000 -s .bss 0x120000" \
117 "Reading symbols from .*${testfile1}\\.\\.\\." \
118 "add-symbol-file ${testfile1} 0x100000" \
119 "add symbol table from file \".*${testfile1}\" at.*\\(y or n\\) " \
122 gdb_test "add-symbol-file ${binfile2} 0x200000 -s .data 0x210000 -s .bss 0x220000" \
123 "Reading symbols from .*${testfile2}\\.\\.\\." \
124 "add-symbol-file ${testfile2} 0x200000" \
125 "add symbol table from file \".*${testfile2}\" at.*\\(y or n\\) " \
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
134 # Same thing, but loading binfile2 before binfile1.
139 with_test_prefix "order2" {
140 gdb_test "add-symbol-file ${binfile2} 0x200000 -s .data 0x210000 -s .bss 0x220000" \
141 "Reading symbols from .*${testfile2}\\.\\.\\." \
142 "add-symbol-file ${testfile2} 0x200000" \
143 "add symbol table from file \".*${testfile2}\" at.*\\(y or n\\) " \
146 gdb_test "add-symbol-file ${binfile1} 0x100000 -s .bss 0x120000" \
147 "Reading symbols from .*${testfile1}\\.\\.\\." \
148 "add-symbol-file ${testfile1} 0x100000" \
149 "add symbol table from file \".*${testfile1}\" at.*\\(y or n\\) " \
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