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 remote_exec build "rm -f ${binfile1}"
32 remote_exec build "rm -f ${binfile2}"
34 if { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${binfile1}" executable $opts] != "" } {
35 untested "failed to compile"
39 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $opts] != "" } {
40 untested "failed to compile"
44 proc not_null_var_address { var } {
46 # Same as get_var_address, expect that it reports a failure if a null
47 # address is returned by gdb.
49 set address [get_var_address $var]
50 regexp "0x\[0-9a-fA-F\]+" $address address
51 if { "$address" == "0x0" } {
52 fail "$var has null address"
56 proc test_eliminated_var { var } {
59 # Match output 'No symbol "${var}" in current context'
61 gdb_test_multiple "print &${var}" "test eliminated var ${var}" {
62 -re "No symbol \"${var}\" in current context\\.\[\r\n\]+${gdb_prompt} $" {
63 pass "test eliminated var ${var}"
65 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" {
66 fail "test eliminated var ${var}"
71 # Check that the code and data eliminated in binfile1 are not included
72 # into partial symtab... and that non-eliminated symbols are still there.
77 gdb_test "symbol-file ${binfile1}" \
78 "Reading symbols from .*${testfile1}\\.\\.\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
79 "symbol-file ${testfile1}"
81 with_test_prefix "single psymtabs" {
82 test_eliminated_var my_global_symbol
83 test_eliminated_var my_static_symbol
84 test_eliminated_var my_global_func
85 not_null_var_address main
88 # Same thing for symtabs
92 set saved_gdbflags $GDBFLAGS
93 set GDBFLAGS "$GDBFLAGS --readnow $binfile1"
95 set GDBFLAGS $saved_gdbflags
97 with_test_prefix "single symtabs" {
98 test_eliminated_var my_global_symbol
99 test_eliminated_var my_static_symbol
100 test_eliminated_var my_global_func
101 not_null_var_address main
104 # binfile2 contains the symbols that have been eliminated in binfile1. Check
105 # the eliminated symbols does not hide these valid ones.
110 with_test_prefix "order1" {
111 gdb_test "add-symbol-file ${binfile1} 0x100000 -s .bss 0x120000" \
112 "Reading symbols from .*${testfile1}\\.\\.\\." \
113 "add-symbol-file ${testfile1} 0x100000" \
114 "add symbol table from file \".*${testfile1}\" at.*\\(y or n\\) " \
117 gdb_test "add-symbol-file ${binfile2} 0x200000 -s .data 0x210000 -s .bss 0x220000" \
118 "Reading symbols from .*${testfile2}\\.\\.\\." \
119 "add-symbol-file ${testfile2} 0x200000" \
120 "add symbol table from file \".*${testfile2}\" at.*\\(y or n\\) " \
123 not_null_var_address my_global_symbol
124 not_null_var_address my_static_symbol
125 not_null_var_address my_global_func
126 not_null_var_address main
129 # Same thing, but loading binfile2 before binfile1.
134 with_test_prefix "order2" {
135 gdb_test "add-symbol-file ${binfile2} 0x200000 -s .data 0x210000 -s .bss 0x220000" \
136 "Reading symbols from .*${testfile2}\\.\\.\\." \
137 "add-symbol-file ${testfile2} 0x200000" \
138 "add symbol table from file \".*${testfile2}\" at.*\\(y or n\\) " \
141 gdb_test "add-symbol-file ${binfile1} 0x100000 -s .bss 0x120000" \
142 "Reading symbols from .*${testfile1}\\.\\.\\." \
143 "add-symbol-file ${testfile1} 0x100000" \
144 "add symbol table from file \".*${testfile1}\" at.*\\(y or n\\) " \
147 not_null_var_address my_global_symbol
148 not_null_var_address my_static_symbol
149 not_null_var_address my_global_func
150 not_null_var_address main