1 # Copyright 2002-2003, 2005, 2007-2012 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 ${objdir}/${subdir}/${testfile1}
24 set binfile2 ${objdir}/${subdir}/${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 code_elim.exp
39 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $opts] != "" } {
40 untested code_elim.exp
44 proc get_var_address { var } {
50 # $6 = (int (*)()) 0x24 <function_bar>
52 gdb_test_multiple "print &${var}" "get address of ${var}" {
53 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" {
54 pass "get address of ${var}"
55 if { $expect_out(1,string) == "0" } {
58 return $expect_out(1,string)
65 proc not_null_var_address { var } {
67 # Same as get_var_address, expect that it reports a failure if a null
68 # address is returned by gdb.
70 set address [get_var_address $var]
71 regexp "0x\[0-9a-fA-F\]+" $address address
72 if { "$address" == "0x0" } {
73 fail "$var has null address"
77 proc test_eliminated_var { var } {
80 # Match output 'No symbol "${var}" in current context'
82 gdb_test_multiple "print &${var}" "test eliminated var ${var}" {
83 -re "No symbol \"${var}\" in current context\\.\[\r\n\]+${gdb_prompt} $" {
84 pass "test eliminated var ${var}"
86 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" {
87 fail "test eliminated var ${var}"
92 # Check that the code and data eliminated in binfile1 are not included
93 # into partial symtab... and that non-eliminated symbols are still there.
98 gdb_test "symbol-file ${binfile1}" \
99 "Reading symbols from .*${testfile1}\\.\\.\\.done\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
100 "symbol-file ${testfile1}"
102 with_test_prefix "single psymtabs" {
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 # Same thing for symtabs
113 set saved_gdbflags $GDBFLAGS
114 set GDBFLAGS "$GDBFLAGS --readnow $binfile1"
116 set GDBFLAGS $saved_gdbflags
118 with_test_prefix "single symtabs" {
119 test_eliminated_var my_global_symbol
120 test_eliminated_var my_static_symbol
121 test_eliminated_var my_global_func
122 not_null_var_address main
125 # binfile2 contains the symbols that have been eliminated in binfile1. Check
126 # the eliminated symbols does not hide these valid ones.
131 gdb_test "add-symbol-file ${binfile1} 0x100000" \
132 "Reading symbols from .*${testfile1}\\.\\.\\.done\\." \
133 "add-symbol-file ${testfile1} 0x100000" \
134 "add symbol table from file \".*${testfile1}\" at\[ \t\r\n\]+\.text_addr = 0x100000\[\r\n\]+\\(y or n\\) " \
137 gdb_test "add-symbol-file ${binfile2} 0x200000" \
138 "Reading symbols from .*${testfile2}\\.\\.\\.done\\." \
139 "add-symbol-file ${testfile2} 0x200000" \
140 "add symbol table from file \".*${testfile2}\" at\[ \t\r\n\]+\.text_addr = 0x200000\[\r\n\]+\\(y or n\\) " \
143 with_test_prefix "order1" {
144 not_null_var_address my_global_symbol
145 not_null_var_address my_static_symbol
146 not_null_var_address my_global_func
147 not_null_var_address main
150 # Same thing, but loading binfile2 before binfile1.
155 gdb_test "add-symbol-file ${binfile2} 0x200000" \
156 "Reading symbols from .*${testfile2}\\.\\.\\.done\\." \
157 "add-symbol-file ${testfile2} 0x200000" \
158 "add symbol table from file \".*${testfile2}\" at\[ \t\r\n\]+\.text_addr = 0x200000\[\r\n\]+\\(y or n\\) " \
161 gdb_test "add-symbol-file ${binfile1} 0x100000" \
162 "Reading symbols from .*${testfile1}\\.\\.\\.done\\." \
163 "add-symbol-file ${testfile1} 0x100000" \
164 "add symbol table from file \".*${testfile1}\" at\[ \t\r\n\]+\.text_addr = 0x100000\[\r\n\]+\\(y or n\\) " \
167 with_test_prefix "order2" {
168 not_null_var_address my_global_symbol
169 not_null_var_address my_static_symbol
170 not_null_var_address my_global_func
171 not_null_var_address main