7369a9c113dc2e80fcc6324e29cac274f2fd0e44
[external/binutils.git] / gdb / testsuite / gdb.base / code_elim.exp
1 # Copyright 2002-2003, 2005, 2007-2012 Free Software Foundation, Inc.
2
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.
7 #
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.
12 #
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/>.  */
15
16 # code_elim.exp -- tests that GDB can handle executables where some data/code
17 #                  has been eliminated by the linker.
18
19 if $tracelevel then {
20     strace $tracelevel
21 }
22
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}
29 set opts [list debug]
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"
34
35 remote_exec build "rm -f ${binfile1}"
36 remote_exec build "rm -f ${binfile2}"
37
38 if { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${binfile1}" executable $opts] != "" } {
39      untested code_elim.exp
40      return -1
41 }
42
43 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $opts] != "" } {
44      untested code_elim.exp
45      return -1
46 }
47
48 proc get_var_address { var } {
49     global gdb_prompt hex
50
51     # Match output like:
52     # $1 = (int *) 0x0
53     # $5 = (int (*)()) 0
54     # $6 = (int (*)()) 0x24 <function_bar>
55
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" } {
60                 return "0x0"
61             } else {
62                 return $expect_out(1,string)
63             }
64         }
65     }
66     return ""
67 }
68
69 proc not_null_var_address { var } {
70
71     # Same as get_var_address, expect that it reports a failure if a null
72     # address is returned by gdb.
73
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"
78     }
79 }
80
81 proc test_eliminated_var { var } {
82     global gdb_prompt hex
83
84     # Match output 'No symbol "${var}" in current context'
85
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}"
89         }
90         -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" {
91             fail "test eliminated var ${var}"
92         }
93     }
94 }
95
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.
98
99 gdb_exit
100 gdb_start
101
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\\) " \
106         "y"
107
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
112
113 # Same thing for symtabs
114
115 gdb_exit
116 global GDBFLAGS
117 set saved_gdbflags $GDBFLAGS
118 set GDBFLAGS "$GDBFLAGS --readnow $binfile1"
119 gdb_start
120 set GDBFLAGS $saved_gdbflags
121
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
126
127 # binfile2 contains the symbols that have been eliminated in binfile1. Check
128 # the eliminated symbols does not hide these valid ones.
129
130 gdb_exit
131 gdb_start
132
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\\) " \
137         "y"
138
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\\) " \
143         "y"
144
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
149
150 # Same thing, but loading binfile2 before binfile1.
151
152 gdb_exit
153 gdb_start
154
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\\) " \
159         "y"
160
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\\) " \
165         "y"
166
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
171
172