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.
23 set testfile1 code_elim1
24 set testfile2 code_elim2
25 set srcfile1 ${testfile1}.c
26 set srcfile2 ${testfile2}.c
27 set binfile1 ${objdir}/${subdir}/${testfile1}
28 set binfile2 ${objdir}/${subdir}/${testfile2}
30 lappend opts "additional_flags=-ffunction-sections"
31 lappend opts "additional_flags=-fdata-sections"
32 lappend opts "additional_flags=-Wl,-gc-sections"
33 lappend opts "additional_flags=-Wl,-e,main"
35 remote_exec build "rm -f ${binfile1}"
36 remote_exec build "rm -f ${binfile2}"
38 if { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${binfile1}" executable $opts] != "" } {
39 untested code_elim.exp
43 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $opts] != "" } {
44 untested code_elim.exp
48 proc get_var_address { var } {
54 # $6 = (int (*)()) 0x24 <function_bar>
56 gdb_test_multiple "print &${var}" "get address of ${var}" {
57 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" {
58 pass "get address of ${var}"
59 if { $expect_out(1,string) == "0" } {
62 return $expect_out(1,string)
69 proc not_null_var_address { var } {
71 # Same as get_var_address, expect that it reports a failure if a null
72 # address is returned by gdb.
74 set address [get_var_address $var]
75 regexp "0x\[0-9a-fA-F\]+" $address address
76 if { "$address" == "0x0" } {
77 fail "$var has null address"
81 proc test_eliminated_var { var } {
84 # Match output 'No symbol "${var}" in current context'
86 gdb_test_multiple "print &${var}" "test eliminated var ${var}" {
87 -re "No symbol \"${var}\" in current context\\.\[\r\n\]+${gdb_prompt} $" {
88 pass "test eliminated var ${var}"
90 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" {
91 fail "test eliminated var ${var}"
96 # Check that the code and data eliminated in binfile1 are not included
97 # into partial symtab... and that non-eliminated symbols are still there.
102 gdb_test "add-symbol-file ${binfile1} 0x100000" \
103 "Reading symbols from .*${testfile1}\\.\\.\\.done\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
104 "add-symbol-file ${testfile1} 0x100000" \
105 "add symbol table from file \".*${testfile1}\" at\[ \t\r\n\]+\.text_addr = 0x100000\[\r\n\]+\\(y or n\\) " \
108 test_eliminated_var my_global_symbol
109 test_eliminated_var my_static_symbol
110 test_eliminated_var my_global_func
111 not_null_var_address main
113 # Same thing for symtabs
117 set saved_gdbflags $GDBFLAGS
118 set GDBFLAGS "$GDBFLAGS --readnow $binfile1"
120 set GDBFLAGS $saved_gdbflags
122 test_eliminated_var my_global_symbol
123 test_eliminated_var my_static_symbol
124 test_eliminated_var my_global_func
125 not_null_var_address main
127 # binfile2 contains the symbols that have been eliminated in binfile1. Check
128 # the eliminated symbols does not hide these valid ones.
133 gdb_test "add-symbol-file ${binfile1} 0x100000" \
134 "Reading symbols from .*${testfile1}\\.\\.\\.done\\." \
135 "add-symbol-file ${testfile1} 0x100000" \
136 "add symbol table from file \".*${testfile1}\" at\[ \t\r\n\]+\.text_addr = 0x100000\[\r\n\]+\\(y or n\\) " \
139 gdb_test "add-symbol-file ${binfile2} 0x200000" \
140 "Reading symbols from .*${testfile2}\\.\\.\\.done\\." \
141 "add-symbol-file ${testfile2} 0x200000" \
142 "add symbol table from file \".*${testfile2}\" at\[ \t\r\n\]+\.text_addr = 0x200000\[\r\n\]+\\(y or n\\) " \
145 not_null_var_address my_global_symbol
146 not_null_var_address my_static_symbol
147 not_null_var_address my_global_func
148 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 not_null_var_address my_global_symbol
168 not_null_var_address my_static_symbol
169 not_null_var_address my_global_func
170 not_null_var_address main