]>
Commit | Line | Data |
---|---|---|
6f2750fe | 1 | # Copyright (C) 1999-2016 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 | |
32866df7 | 5 | # the Free Software Foundation; either version 3 of the License, or |
252b5132 | 6 | # (at your option) any later version. |
65951855 | 7 | # |
252b5132 RH |
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. | |
65951855 | 12 | # |
252b5132 RH |
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 | # bug-dejagnu@prep.ai.mit.edu | |
19 | ||
113675b7 | 20 | # Written by Nick Clifton <nickc@cygnus.com> |
252b5132 RH |
21 | # Based on scripts written by Ian Lance Taylor <ian@cygnus.com> |
22 | # and Ken Raeburn <raeburn@cygnus.com>. | |
23 | ||
09c11c86 NC |
24 | # Exclude non-ELF targets. |
25 | if ![is_elf_format] { | |
26 | verbose "$READELF is only intended for ELF targets" 2 | |
27 | return | |
28 | } | |
29 | ||
252b5132 RH |
30 | # First some helpful procedures, then the tests themselves |
31 | ||
32 | # Return the contents of the filename given | |
33 | proc file_contents { filename } { | |
34 | set file [open $filename r] | |
35 | set contents [read $file] | |
36 | close $file | |
37 | return $contents | |
38 | } | |
39 | ||
9921923c HPN |
40 | # Find out the size by reading the output of the EI_CLASS field. |
41 | # Similar to the test for readelf -h, but we're just looking for the | |
42 | # EI_CLASS line here. | |
43 | proc readelf_find_size { binary_file } { | |
44 | global READELF | |
45 | global READELFFLAGS | |
46 | global readelf_size | |
47 | ||
48 | set readelf_size "" | |
49 | set testname "finding out ELF size with readelf -h" | |
7f6a71ff JM |
50 | set got [remote_exec host "$READELF $READELFFLAGS -h $binary_file" "" "/dev/null" "readelf.out"] |
51 | if [is_remote host] then { | |
52 | remote_upload host "readelf.out" | |
53 | } | |
9921923c | 54 | |
7f6a71ff | 55 | if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then { |
9921923c HPN |
56 | send_log $got |
57 | fail $testname | |
58 | return | |
59 | } | |
60 | ||
61 | if { ! [regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \ | |
62 | [file_contents readelf.out] nil readelf_size] } { | |
63 | verbose -log "EI_CLASS field not found in output" | |
64 | verbose -log "output is \n[file_contents readelf.out]" | |
65 | fail $testname | |
66 | return | |
67 | } else { | |
68 | verbose -log "ELF size is $readelf_size" | |
69 | } | |
70 | ||
71 | pass $testname | |
72 | } | |
73 | ||
252b5132 RH |
74 | # Run an individual readelf test. |
75 | # Basically readelf is run on the binary_file with the given options. | |
76 | # Readelf's output is captured and then compared against the contents | |
9921923c | 77 | # of the regexp_file-readelf_size if it exists, else regexp_file. |
252b5132 RH |
78 | |
79 | proc readelf_test { options binary_file regexp_file xfails } { | |
80 | ||
81 | global READELF | |
82 | global READELFFLAGS | |
9921923c | 83 | global readelf_size |
252b5132 RH |
84 | global srcdir |
85 | global subdir | |
65951855 | 86 | |
9921923c | 87 | send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n" |
7f6a71ff | 88 | set got [remote_exec host "$READELF $READELFFLAGS $options $binary_file" "" "/dev/null" "readelf.out"] |
252b5132 | 89 | |
9a1c9383 NC |
90 | foreach xfail $xfails { |
91 | setup_xfail $xfail | |
252b5132 | 92 | } |
3f7de0e7 | 93 | |
7f6a71ff | 94 | if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then { |
3f7de0e7 | 95 | fail "readelf $options (reason: unexpected output)" |
252b5132 | 96 | send_log $got |
3f7de0e7 | 97 | send_log "\n" |
252b5132 RH |
98 | return |
99 | } | |
100 | ||
ade0b24f NC |
101 | set target_machine "" |
102 | if [istarget "mips*-*-*"] then { | |
96037eb0 | 103 | if { [istarget "mips*-*-*linux*"] |
aeffff67 | 104 | || [istarget "mips*-sde-elf*"] |
d4a43794 | 105 | || [istarget "mips*-mti-elf*"] |
a9d58c06 | 106 | || [istarget "mips*-img-elf*"] |
aeffff67 | 107 | || [istarget "mips*-*freebsd*"] } then { |
eed3fa3b L |
108 | set target_machine tmips |
109 | } else { | |
110 | set target_machine mips | |
111 | } | |
ade0b24f NC |
112 | } |
113 | ||
114 | if { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$readelf_size-$target_machine] } then { | |
115 | set regexp_file $regexp_file-$readelf_size-$target_machine | |
116 | } elseif { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$target_machine] } then { | |
117 | set regexp_file $regexp_file-$target_machine | |
118 | } elseif { [file exists $srcdir/$subdir/$regexp_file-$readelf_size] } then { | |
9921923c HPN |
119 | set regexp_file $regexp_file-$readelf_size |
120 | } | |
121 | ||
252b5132 RH |
122 | if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then { |
123 | fail "readelf $options" | |
124 | verbose "output is \n[file_contents readelf.out]" 2 | |
125 | return | |
126 | } | |
127 | ||
128 | pass "readelf $options" | |
129 | } | |
130 | ||
cbb356d9 NC |
131 | # Simple proc to skip certain expected warning messages. |
132 | ||
133 | proc prune_readelf_wi_warnings { text } { | |
134 | regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text | |
135 | return $text | |
136 | } | |
137 | ||
138 | # Testing the "readelf -wi" option is difficult because there | |
139 | # is no guaranteed order to the output, and because some ports | |
140 | # will use indirect string references, whilst others will use | |
141 | # direct references. So instead of having an expected output | |
142 | # file, like the other readelf tests, we grep for strings that | |
143 | # really ought to be there. | |
144 | ||
145 | proc readelf_wi_test {} { | |
146 | global READELF | |
147 | global READELFFLAGS | |
148 | global srcdir | |
149 | global subdir | |
65951855 | 150 | |
cbb356d9 NC |
151 | # Compile the second test file. |
152 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { | |
153 | verbose "Unable to compile test file." | |
154 | untested "readelf -wi" | |
155 | return | |
156 | } | |
157 | ||
158 | # Download it. | |
8d263650 | 159 | set tempfile [remote_download host tmpdir/testprog.o] |
cbb356d9 NC |
160 | |
161 | # Run "readelf -wi" on it. | |
7f6a71ff | 162 | set got [remote_exec host "$READELF $READELFFLAGS -wi $tempfile" "" "/dev/null" "readelf.out"] |
cbb356d9 NC |
163 | |
164 | # Upload the results. | |
8d263650 | 165 | set output [remote_upload host readelf.out] |
cbb356d9 | 166 | |
8d263650 | 167 | file_on_host delete $tempfile |
65951855 | 168 | |
cbb356d9 | 169 | # Strip any superflous warnings. |
7f6a71ff | 170 | set got [prune_readelf_wi_warnings [lindex $got 1]] |
cbb356d9 NC |
171 | |
172 | if ![string match "" $got] then { | |
b279a55c | 173 | fail "readelf $READELFFLAGS -wi (reason: unexpected output)" |
cbb356d9 NC |
174 | send_log $got |
175 | send_log "\n" | |
176 | return | |
177 | } | |
178 | ||
179 | if ![file size $output] then { | |
180 | # If the output file is empty, then this target does not | |
181 | # generate dwarf2 output. This is not a failure. | |
182 | verbose "No output from 'readelf -wi'" | |
183 | untested "readelf -wi" | |
184 | return | |
185 | } | |
65951855 | 186 | |
cbb356d9 NC |
187 | # Search for strings that should be in the output. |
188 | set sought { | |
189 | ".*DW_TAG_compile_unit.*" | |
190 | ".*DW_TAG_subprogram.*" | |
191 | ".*DW_TAG_base_type.*" | |
192 | ".*DW_AT_producer.*(GNU C|indirect string).*" | |
193 | ".*DW_AT_language.*ANSI C.*" | |
194 | ".*DW_AT_name.*(testprog.c|indirect string).*" | |
195 | ".*DW_AT_name.*fn.*" | |
196 | ".*DW_AT_name.*(main|indirect string).*" | |
aaa222e7 | 197 | ".*\(DW_OP_addr: 0\).*" |
cbb356d9 | 198 | } |
65951855 | 199 | |
13761a11 NC |
200 | # The MSP430 in LARGE mode does not generate a DW_OP_addr. |
201 | setup_xfail msp430*-*-* | |
202 | ||
65951855 | 203 | foreach looked_for $sought { |
cbb356d9 NC |
204 | set lines [grep $output $looked_for] |
205 | if ![llength $lines] then { | |
206 | fail "readelf -wi: missing: $looked_for" | |
207 | send_log readelf.out | |
208 | return | |
209 | } | |
210 | } | |
211 | ||
8d263650 | 212 | file_on_host delete $output |
65951855 | 213 | |
cbb356d9 NC |
214 | # All done. |
215 | pass "readelf -wi" | |
216 | } | |
252b5132 | 217 | |
1b315056 CS |
218 | # This tests "readelf -wa", but on a file with a compressed |
219 | # .debug_abbrev section. | |
220 | ||
221 | proc readelf_compressed_wa_test {} { | |
222 | global READELF | |
223 | global READELFFLAGS | |
224 | global srcdir | |
225 | global subdir | |
65951855 | 226 | |
1b315056 CS |
227 | # Compile the compressed-debug-section test file. |
228 | if { [target_compile $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o object debug] != "" } { | |
229 | verbose "Unable to compile test file." | |
230 | untested "readelf -wa (compressed)" | |
231 | return | |
232 | } | |
233 | ||
234 | # Download it. | |
235 | set tempfile [remote_download host tmpdir/dw2-compressed.o] | |
236 | ||
237 | # Run "readelf -wa" on it. | |
238 | set got [remote_exec host "$READELF $READELFFLAGS -wa $tempfile" "" "/dev/null" "readelf.out"] | |
239 | ||
240 | # Upload the results. | |
241 | set output [remote_upload host readelf.out] | |
242 | ||
243 | file_on_host delete $tempfile | |
65951855 | 244 | |
1b315056 CS |
245 | if { [string compare [file_contents readelf.out] [file_contents $srcdir/$subdir/readelf.wa]] != 0 } then { |
246 | fail "readelf -wa (compressed)" | |
247 | verbose "output is \n[file_contents readelf.out]" 2 | |
248 | verbose "expected is \n[file_contents $srcdir/$subdir/readelf.wa]" 2 | |
249 | return | |
250 | } | |
251 | ||
252 | pass "readelf -wa (compressed)" | |
253 | } | |
252b5132 | 254 | |
09c11c86 NC |
255 | # Test readelf's dumping abilities. |
256 | ||
257 | proc readelf_dump_test {} { | |
258 | global READELF | |
259 | global READELFFLAGS | |
260 | global srcdir | |
261 | global subdir | |
65951855 | 262 | |
09c11c86 NC |
263 | # Assemble the dump test file. |
264 | if {![binutils_assemble $srcdir/$subdir/dumptest.s tmpdir/dumptest.o]} then { | |
265 | unresolved "readelf -p: failed to assemble dump test file" | |
266 | return | |
267 | } | |
268 | # Download it. | |
269 | set tempfile [remote_download host tmpdir/dumptest.o] | |
270 | ||
271 | # Run "readelf -p.data" on it. | |
96037eb0 NC |
272 | set sect_names [get_standard_section_names] |
273 | if { $sect_names != "" } { | |
274 | set got [remote_exec host "$READELF $READELFFLAGS -p[lindex $sect_names 1] $tempfile" "" "/dev/null" "readelf.out"] | |
275 | } else { | |
276 | set got [remote_exec host "$READELF $READELFFLAGS -p.data $tempfile" "" "/dev/null" "readelf.out"] | |
277 | } | |
09c11c86 NC |
278 | set got [lindex $got 1] |
279 | ||
280 | # Upload the results. | |
281 | set output [remote_upload host readelf.out] | |
282 | ||
283 | # Check for something going wrong. | |
284 | if ![string match "" $got] then { | |
285 | fail "readelf -p: unexpected output" | |
286 | send_log $got | |
287 | send_log "\n" | |
288 | return | |
289 | } | |
290 | ||
291 | # Search for strings that should be in the output. | |
292 | set sought { | |
293 | ".*test_string.*" | |
294 | } | |
65951855 RM |
295 | |
296 | foreach looked_for $sought { | |
09c11c86 NC |
297 | set lines [grep $output $looked_for] |
298 | if ![llength $lines] then { | |
299 | fail "readelf -p: missing: $looked_for" | |
300 | send_log readelf.out | |
301 | return | |
302 | } | |
303 | } | |
304 | ||
65951855 | 305 | file_on_host delete $tempfile |
09c11c86 NC |
306 | file_on_host delete $output |
307 | ||
308 | # All done. | |
309 | pass "readelf -p" | |
310 | ||
311 | # XXX FIXME: Add test of readelf -x here | |
252b5132 RH |
312 | } |
313 | ||
314 | if ![is_remote host] { | |
315 | if {[which $READELF] == 0} then { | |
316 | perror "$READELF does not exist" | |
317 | return | |
318 | } | |
319 | } | |
320 | ||
321 | send_user "Version [binutil_version $READELF]" | |
322 | ||
89084430 | 323 | # Assemble the test file. |
252b5132 | 324 | if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then { |
09c11c86 | 325 | perror "could not assemble test file" |
252b5132 RH |
326 | unresolved "readelf - failed to assemble" |
327 | return | |
328 | } | |
329 | ||
330 | if ![is_remote host] { | |
8d263650 | 331 | set tempfile tmpdir/bintest.o |
252b5132 RH |
332 | } else { |
333 | set tempfile [remote_download host tmpdir/bintest.o] | |
334 | } | |
335 | ||
9921923c HPN |
336 | # First, determine the size, so specific output matchers can be used. |
337 | readelf_find_size $tempfile | |
338 | ||
339 | # Run the tests. | |
252b5132 | 340 | readelf_test -h $tempfile readelf.h {} |
ade0b24f | 341 | readelf_test -S $tempfile readelf.s {} |
96037eb0 | 342 | setup_xfail "mips-*-*irix*" |
ade0b24f | 343 | readelf_test -s $tempfile readelf.ss {} |
252b5132 RH |
344 | readelf_test -r $tempfile readelf.r {} |
345 | ||
cbb356d9 | 346 | readelf_wi_test |
1b315056 | 347 | readelf_compressed_wa_test |
09c11c86 NC |
348 | |
349 | readelf_dump_test | |
8b971f9f NC |
350 | |
351 | # PR 13482 - Check for off-by-one errors when dumping .note sections. | |
352 | if {![binutils_assemble $srcdir/$subdir/version.s tmpdir/version.o]} then { | |
353 | perror "could not assemble version note test file" | |
354 | unresolved "readelf - failed to assemble" | |
0e602686 | 355 | fail "readelf -n" |
8b971f9f | 356 | } else { |
0e602686 NC |
357 | |
358 | if ![is_remote host] { | |
359 | set tempfile tmpdir/version.o | |
360 | } else { | |
361 | set tempfile [remote_download host tmpdir/version.o] | |
362 | } | |
363 | ||
364 | readelf_test -n $tempfile readelf.n {} | |
8b971f9f NC |
365 | } |
366 | ||
d1c4b12b NC |
367 | |
368 | # PR 18374 - Check that relocations against the .debug_loc section | |
369 | # do not prevent readelf from displaying all the location lists. | |
370 | if {![binutils_assemble $srcdir/$subdir/pr18374.s tmpdir/pr18374.o]} then { | |
371 | perror "could not assemble PR18374 test file" | |
372 | unresolved "readelf - failed to assemble" | |
0e602686 NC |
373 | fail "readelf --debug-loc" |
374 | } else { | |
375 | ||
376 | if ![is_remote host] { | |
377 | set tempfile tmpdir/pr18374.o | |
378 | } else { | |
379 | set tempfile [remote_download host tmpdir/pr18374.o] | |
380 | } | |
381 | ||
382 | readelf_test --debug-dump=loc $tempfile readelf.pr18374 {} | |
d1c4b12b NC |
383 | } |
384 | ||
0e602686 NC |
385 | |
386 | # Check that decompressed dumps work. | |
387 | if {![binutils_assemble $srcdir/$subdir/z.s tmpdir/z.o]} then { | |
388 | perror "could not assemble decompress dump test file" | |
389 | unresolved "readelf - failed to assemble" | |
390 | fail "readelf -z" | |
d1c4b12b | 391 | } else { |
d1c4b12b | 392 | |
0e602686 NC |
393 | if ![is_remote host] { |
394 | set tempfile tmpdir/z.o | |
395 | } else { | |
396 | set tempfile [remote_download host tmpdir/z.o] | |
397 | } | |
398 | ||
399 | readelf_test {--decompress --hex-dump .debug_loc} $tempfile readelf.z {} | |
400 | } |