]>
Commit | Line | Data |
---|---|---|
aef6203b | 1 | # Copyright 1999, 2000, 2001, 2003, 2004 Free Software Foundation, Inc. |
252b5132 RH |
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 2 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, write to the Free Software | |
b43b5d5f | 15 | # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
252b5132 RH |
16 | |
17 | # Please email any bugs, comments, and/or additions to this file to: | |
18 | # [email protected] | |
19 | ||
113675b7 | 20 | # Written by Nick Clifton <[email protected]> |
252b5132 RH |
21 | # Based on scripts written by Ian Lance Taylor <[email protected]> |
22 | # and Ken Raeburn <[email protected]>. | |
23 | ||
24 | # First some helpful procedures, then the tests themselves | |
25 | ||
26 | # Return the contents of the filename given | |
27 | proc file_contents { filename } { | |
28 | set file [open $filename r] | |
29 | set contents [read $file] | |
30 | close $file | |
31 | return $contents | |
32 | } | |
33 | ||
34 | # regexp_diff, based on simple_diff taken from ld test suite | |
35 | # compares two files line-by-line | |
36 | # file1 contains strings, file2 contains regexps and #-comments | |
37 | # blank lines are ignored in either file | |
38 | # returns non-zero if differences exist | |
39 | # | |
40 | proc regexp_diff { file_1 file_2 } { | |
41 | ||
42 | set eof -1 | |
43 | set end_1 0 | |
44 | set end_2 0 | |
45 | set differences 0 | |
46 | set diff_pass 0 | |
47 | ||
48 | if [file exists $file_1] then { | |
49 | set file_a [open $file_1 r] | |
50 | } else { | |
51 | warning "$file_1 doesn't exist" | |
52 | return 1 | |
53 | } | |
54 | ||
55 | if [file exists $file_2] then { | |
56 | set file_b [open $file_2 r] | |
57 | } else { | |
58 | fail "$file_2 doesn't exist" | |
59 | close $file_a | |
60 | return 1 | |
61 | } | |
62 | ||
63 | verbose " Regexp-diff'ing: $file_1 $file_2" 2 | |
64 | ||
65 | while { 1 } { | |
66 | set line_a "" | |
67 | set line_b "" | |
68 | while { [string length $line_a] == 0 } { | |
69 | if { [gets $file_a line_a] == $eof } { | |
70 | set end_1 1 | |
71 | break | |
72 | } | |
73 | } | |
74 | while { [string length $line_b] == 0 || [string match "#*" $line_b] } { | |
75 | if [ string match "#pass" $line_b ] { | |
76 | set end_2 1 | |
77 | set diff_pass 1 | |
78 | break | |
ade0b24f NC |
79 | } elseif [ string match "#..." $line_b ] { |
80 | if { [gets $file_b line_b] == $eof } { | |
81 | set end_2 1 | |
82 | break | |
83 | } | |
84 | verbose "looking for \"^$line_b$\"" 3 | |
85 | while { ![regexp "^$line_b$" "$line_a"] } { | |
86 | verbose "skipping \"$line_a\"" 3 | |
87 | if { [gets $file_a line_a] == $eof } { | |
88 | set end_1 1 | |
89 | break | |
90 | } | |
91 | } | |
92 | break | |
252b5132 RH |
93 | } |
94 | if { [gets $file_b line_b] == $eof } { | |
95 | set end_2 1 | |
96 | break | |
97 | } | |
98 | } | |
99 | ||
100 | if { $diff_pass } { | |
101 | break | |
102 | } elseif { $end_1 && $end_2 } { | |
103 | break | |
104 | } elseif { $end_1 } { | |
105 | send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n" | |
106 | verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3 | |
107 | set differences 1 | |
108 | break | |
109 | } elseif { $end_2 } { | |
110 | send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" | |
111 | verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3 | |
112 | set differences 1 | |
113 | break | |
114 | } else { | |
115 | verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3 | |
116 | if ![regexp "^$line_b$" "$line_a"] { | |
117 | send_log "regexp_diff match failure\n" | |
118 | send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n" | |
119 | set differences 1 | |
252b5132 RH |
120 | } |
121 | } | |
122 | } | |
123 | ||
124 | if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } { | |
125 | send_log "$file_1 and $file_2 are different lengths\n" | |
126 | verbose "$file_1 and $file_2 are different lengths" 3 | |
127 | set differences 1 | |
128 | } | |
129 | ||
130 | close $file_a | |
131 | close $file_b | |
132 | ||
133 | return $differences | |
134 | } | |
135 | ||
9921923c HPN |
136 | # Find out the size by reading the output of the EI_CLASS field. |
137 | # Similar to the test for readelf -h, but we're just looking for the | |
138 | # EI_CLASS line here. | |
139 | proc readelf_find_size { binary_file } { | |
140 | global READELF | |
141 | global READELFFLAGS | |
142 | global readelf_size | |
143 | ||
144 | set readelf_size "" | |
145 | set testname "finding out ELF size with readelf -h" | |
146 | catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got | |
147 | ||
148 | if ![string match "" $got] then { | |
149 | send_log $got | |
150 | fail $testname | |
151 | return | |
152 | } | |
153 | ||
154 | if { ! [regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \ | |
155 | [file_contents readelf.out] nil readelf_size] } { | |
156 | verbose -log "EI_CLASS field not found in output" | |
157 | verbose -log "output is \n[file_contents readelf.out]" | |
158 | fail $testname | |
159 | return | |
160 | } else { | |
161 | verbose -log "ELF size is $readelf_size" | |
162 | } | |
163 | ||
164 | pass $testname | |
165 | } | |
166 | ||
252b5132 RH |
167 | # Run an individual readelf test. |
168 | # Basically readelf is run on the binary_file with the given options. | |
169 | # Readelf's output is captured and then compared against the contents | |
9921923c | 170 | # of the regexp_file-readelf_size if it exists, else regexp_file. |
252b5132 RH |
171 | |
172 | proc readelf_test { options binary_file regexp_file xfails } { | |
173 | ||
174 | global READELF | |
175 | global READELFFLAGS | |
9921923c | 176 | global readelf_size |
252b5132 RH |
177 | global srcdir |
178 | global subdir | |
179 | ||
9921923c | 180 | send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n" |
252b5132 RH |
181 | catch "exec $READELF $READELFFLAGS $options $binary_file > readelf.out" got |
182 | ||
9a1c9383 NC |
183 | foreach xfail $xfails { |
184 | setup_xfail $xfail | |
252b5132 | 185 | } |
3f7de0e7 | 186 | |
252b5132 | 187 | if ![string match "" $got] then { |
3f7de0e7 | 188 | fail "readelf $options (reason: unexpected output)" |
252b5132 | 189 | send_log $got |
3f7de0e7 | 190 | send_log "\n" |
252b5132 RH |
191 | return |
192 | } | |
193 | ||
ade0b24f NC |
194 | set target_machine "" |
195 | if [istarget "mips*-*-*"] then { | |
dda688fc | 196 | if { [istarget "mips*-*-*linux*"] } then { |
eed3fa3b L |
197 | set target_machine tmips |
198 | } else { | |
199 | set target_machine mips | |
200 | } | |
ade0b24f NC |
201 | } |
202 | ||
203 | if { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$readelf_size-$target_machine] } then { | |
204 | set regexp_file $regexp_file-$readelf_size-$target_machine | |
205 | } elseif { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$target_machine] } then { | |
206 | set regexp_file $regexp_file-$target_machine | |
207 | } elseif { [file exists $srcdir/$subdir/$regexp_file-$readelf_size] } then { | |
9921923c HPN |
208 | set regexp_file $regexp_file-$readelf_size |
209 | } | |
210 | ||
252b5132 RH |
211 | if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then { |
212 | fail "readelf $options" | |
213 | verbose "output is \n[file_contents readelf.out]" 2 | |
214 | return | |
215 | } | |
216 | ||
217 | pass "readelf $options" | |
218 | } | |
219 | ||
cbb356d9 NC |
220 | # Simple proc to skip certain expected warning messages. |
221 | ||
222 | proc prune_readelf_wi_warnings { text } { | |
223 | regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text | |
224 | return $text | |
225 | } | |
226 | ||
227 | # Testing the "readelf -wi" option is difficult because there | |
228 | # is no guaranteed order to the output, and because some ports | |
229 | # will use indirect string references, whilst others will use | |
230 | # direct references. So instead of having an expected output | |
231 | # file, like the other readelf tests, we grep for strings that | |
232 | # really ought to be there. | |
233 | ||
234 | proc readelf_wi_test {} { | |
235 | global READELF | |
236 | global READELFFLAGS | |
237 | global srcdir | |
238 | global subdir | |
239 | ||
240 | # Compile the second test file. | |
241 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { | |
242 | verbose "Unable to compile test file." | |
243 | untested "readelf -wi" | |
244 | return | |
245 | } | |
246 | ||
247 | # Download it. | |
8d263650 | 248 | set tempfile [remote_download host tmpdir/testprog.o] |
cbb356d9 NC |
249 | |
250 | # Run "readelf -wi" on it. | |
251 | send_log "exec $READELF $READELFFLAGS -wi $tempfile > readelf.out\n" | |
252 | catch "exec $READELF $READELFFLAGS -wi $tempfile > readelf.out" got | |
253 | ||
254 | # Upload the results. | |
8d263650 | 255 | set output [remote_upload host readelf.out] |
cbb356d9 | 256 | |
8d263650 | 257 | file_on_host delete $tempfile |
cbb356d9 NC |
258 | |
259 | # Strip any superflous warnings. | |
260 | set got [prune_readelf_wi_warnings $got] | |
261 | ||
262 | if ![string match "" $got] then { | |
263 | fail "readelf $options (reason: unexpected output)" | |
264 | send_log $got | |
265 | send_log "\n" | |
266 | return | |
267 | } | |
268 | ||
269 | if ![file size $output] then { | |
270 | # If the output file is empty, then this target does not | |
271 | # generate dwarf2 output. This is not a failure. | |
272 | verbose "No output from 'readelf -wi'" | |
273 | untested "readelf -wi" | |
274 | return | |
275 | } | |
276 | ||
277 | # Search for strings that should be in the output. | |
278 | set sought { | |
279 | ".*DW_TAG_compile_unit.*" | |
280 | ".*DW_TAG_subprogram.*" | |
281 | ".*DW_TAG_base_type.*" | |
282 | ".*DW_AT_producer.*(GNU C|indirect string).*" | |
283 | ".*DW_AT_language.*ANSI C.*" | |
284 | ".*DW_AT_name.*(testprog.c|indirect string).*" | |
285 | ".*DW_AT_name.*fn.*" | |
286 | ".*DW_AT_name.*(main|indirect string).*" | |
aaa222e7 | 287 | ".*\(DW_OP_addr: 0\).*" |
cbb356d9 NC |
288 | } |
289 | ||
290 | foreach looked_for $sought { | |
291 | set lines [grep $output $looked_for] | |
292 | if ![llength $lines] then { | |
293 | fail "readelf -wi: missing: $looked_for" | |
294 | send_log readelf.out | |
295 | return | |
296 | } | |
297 | } | |
298 | ||
8d263650 | 299 | file_on_host delete $output |
cbb356d9 NC |
300 | |
301 | # All done. | |
302 | pass "readelf -wi" | |
303 | } | |
252b5132 RH |
304 | |
305 | ||
9ce701e2 L |
306 | # Exclude non-ELF targets. |
307 | if ![is_elf_format] { | |
9921923c | 308 | verbose "$READELF is only intended for ELF targets" 2 |
252b5132 RH |
309 | return |
310 | } | |
311 | ||
312 | if ![is_remote host] { | |
313 | if {[which $READELF] == 0} then { | |
314 | perror "$READELF does not exist" | |
315 | return | |
316 | } | |
317 | } | |
318 | ||
319 | send_user "Version [binutil_version $READELF]" | |
320 | ||
89084430 | 321 | # Assemble the test file. |
252b5132 RH |
322 | if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then { |
323 | perror "unresolved 1" | |
324 | unresolved "readelf - failed to assemble" | |
325 | return | |
326 | } | |
327 | ||
328 | if ![is_remote host] { | |
8d263650 | 329 | set tempfile tmpdir/bintest.o |
252b5132 RH |
330 | } else { |
331 | set tempfile [remote_download host tmpdir/bintest.o] | |
332 | } | |
333 | ||
9921923c HPN |
334 | # First, determine the size, so specific output matchers can be used. |
335 | readelf_find_size $tempfile | |
336 | ||
337 | # Run the tests. | |
252b5132 | 338 | readelf_test -h $tempfile readelf.h {} |
ade0b24f NC |
339 | readelf_test -S $tempfile readelf.s {} |
340 | readelf_test -s $tempfile readelf.ss {} | |
252b5132 RH |
341 | readelf_test -r $tempfile readelf.r {} |
342 | ||
cbb356d9 | 343 | readelf_wi_test |