1# Copyright 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011
2# Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17# code_elim.exp -- tests that GDB can handle executables where some data/code
18#                  has been eliminated by the linker.
19
20if $tracelevel then {
21    strace $tracelevel
22}
23
24set testfile1 code_elim1
25set testfile2 code_elim2
26set srcfile1 ${testfile1}.c
27set srcfile2 ${testfile2}.c
28set binfile1 ${objdir}/${subdir}/${testfile1}
29set binfile2 ${objdir}/${subdir}/${testfile2}
30set opts [list debug]
31lappend opts "additional_flags=-ffunction-sections"
32lappend opts "additional_flags=-fdata-sections"
33lappend opts "additional_flags=-Wl,-gc-sections"
34lappend opts "additional_flags=-Wl,-e,main"
35
36remote_exec build "rm -f ${binfile1}"
37remote_exec build "rm -f ${binfile2}"
38
39if { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${binfile1}" executable $opts] != "" } {
40     untested code_elim.exp
41     return -1
42}
43
44if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $opts] != "" } {
45     untested code_elim.exp
46     return -1
47}
48
49proc get_var_address { var } {
50    global gdb_prompt hex
51
52    # Match output like:
53    # $1 = (int *) 0x0
54    # $5 = (int (*)()) 0
55    # $6 = (int (*)()) 0x24 <function_bar>
56
57    gdb_test_multiple "print &${var}" "get address of ${var}" {
58	-re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" {
59	    pass "get address of ${var}"
60	    if { $expect_out(1,string) == "0" } {
61		return "0x0"
62	    } else {
63		return $expect_out(1,string)
64	    }
65	}
66    }
67    return ""
68}
69
70proc not_null_var_address { var } {
71
72    # Same as get_var_address, expect that it reports a failure if a null
73    # address is returned by gdb.
74
75    set address [get_var_address $var]
76    regexp "0x\[0-9a-fA-F\]+" $address address
77    if { "$address" == "0x0" } {
78	fail "$var has null address"
79    }
80}
81
82proc test_eliminated_var { var } {
83    global gdb_prompt hex
84
85    # Match output 'No symbol "${var}" in current context'
86
87    gdb_test_multiple "print &${var}" "test eliminated var ${var}" {
88	-re "No symbol \"${var}\" in current context\\.\[\r\n\]+${gdb_prompt} $" {
89	    pass "test eliminated var ${var}"
90	}
91	-re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" {
92	    fail "test eliminated var ${var}"
93	}
94    }
95}
96
97# Check that the code and data eliminated in binfile1 are not included
98# into partial symtab... and that non-eliminated symbols are still there.
99
100gdb_exit
101gdb_start
102
103gdb_test "add-symbol-file ${binfile1} 0x100000" \
104	"Reading symbols from .*${testfile1}\\.\\.\\.done\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
105	"add-symbol-file ${testfile1} 0x100000" \
106	"add symbol table from file \".*${testfile1}\" at\[ \t\r\n\]+\.text_addr = 0x100000\[\r\n\]+\\(y or n\\) " \
107	"y"
108
109test_eliminated_var my_global_symbol
110test_eliminated_var my_static_symbol
111test_eliminated_var my_global_func
112not_null_var_address main
113
114# Same thing for symtabs
115
116gdb_exit
117global GDBFLAGS
118set saved_gdbflags $GDBFLAGS
119set GDBFLAGS "$GDBFLAGS --readnow $binfile1"
120gdb_start
121set GDBFLAGS $saved_gdbflags
122
123test_eliminated_var my_global_symbol
124test_eliminated_var my_static_symbol
125test_eliminated_var my_global_func
126not_null_var_address main
127
128# binfile2 contains the symbols that have been eliminated in binfile1. Check
129# the eliminated symbols does not hide these valid ones.
130
131gdb_exit
132gdb_start
133
134gdb_test "add-symbol-file ${binfile1} 0x100000" \
135	"Reading symbols from .*${testfile1}\\.\\.\\.done\\." \
136	"add-symbol-file ${testfile1} 0x100000" \
137	"add symbol table from file \".*${testfile1}\" at\[ \t\r\n\]+\.text_addr = 0x100000\[\r\n\]+\\(y or n\\) " \
138	"y"
139
140gdb_test "add-symbol-file ${binfile2} 0x200000" \
141	"Reading symbols from .*${testfile2}\\.\\.\\.done\\." \
142	"add-symbol-file ${testfile2} 0x200000" \
143	"add symbol table from file \".*${testfile2}\" at\[ \t\r\n\]+\.text_addr = 0x200000\[\r\n\]+\\(y or n\\) " \
144	"y"
145
146not_null_var_address my_global_symbol
147not_null_var_address my_static_symbol
148not_null_var_address my_global_func
149not_null_var_address main
150
151# Same thing, but loading binfile2 before binfile1.
152
153gdb_exit
154gdb_start
155
156gdb_test "add-symbol-file ${binfile2} 0x200000" \
157	"Reading symbols from .*${testfile2}\\.\\.\\.done\\." \
158	"add-symbol-file ${testfile2} 0x200000" \
159	"add symbol table from file \".*${testfile2}\" at\[ \t\r\n\]+\.text_addr = 0x200000\[\r\n\]+\\(y or n\\) " \
160	"y"
161
162gdb_test "add-symbol-file ${binfile1} 0x100000" \
163	"Reading symbols from .*${testfile1}\\.\\.\\.done\\." \
164	"add-symbol-file ${testfile1} 0x100000" \
165	"add symbol table from file \".*${testfile1}\" at\[ \t\r\n\]+\.text_addr = 0x100000\[\r\n\]+\\(y or n\\) " \
166	"y"
167
168not_null_var_address my_global_symbol
169not_null_var_address my_static_symbol
170not_null_var_address my_global_func
171not_null_var_address main
172
173
174