]>
Commit | Line | Data |
---|---|---|
b3adc24a | 1 | # Copyright (C) 1994-2020 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. |
21c40443 | 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. | |
21c40443 | 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 | ||
20 | # Written by Ian Lance Taylor <ian@cygnus.com> | |
21 | ||
22 | if ![is_remote host] { | |
23 | if {[which $OBJCOPY] == 0} then { | |
24 | perror "$OBJCOPY does not exist" | |
25 | return | |
26 | } | |
27 | } | |
28 | ||
29 | send_user "Version [binutil_version $OBJCOPY]" | |
30 | ||
252b5132 | 31 | if ![is_remote host] { |
8d263650 BE |
32 | set tempfile tmpdir/bintest.o |
33 | set copyfile tmpdir/copy | |
252b5132 RH |
34 | } else { |
35 | set tempfile [remote_download host tmpdir/bintest.o] | |
36 | set copyfile copy | |
37 | } | |
38 | ||
39 | # Test that objcopy does not modify a file when copying it. | |
1fafefd5 | 40 | # "object" or "executable" values for type are supported. |
252b5132 | 41 | |
1fafefd5 | 42 | proc objcopy_test {testname srcfile type asflags ldflags} { |
64bb95af L |
43 | global OBJCOPY |
44 | global OBJCOPYFLAGS | |
45 | global srcdir | |
46 | global subdir | |
47 | global tempfile | |
48 | global copyfile | |
1fafefd5 JL |
49 | set t_tempfile $tempfile |
50 | set t_copyfile ${copyfile}.o | |
252b5132 | 51 | |
1fafefd5 JL |
52 | if { $type != "object" && $type != "executable" } { |
53 | error "objcopy_test accepts only \"object\" or \"executable\" values for type" | |
54 | } | |
55 | ||
56 | if {![binutils_assemble_flags $srcdir/$subdir/${srcfile} $t_tempfile "$asflags"]} then { | |
57 | unresolved "objcopy $type ($testname)" | |
58 | remote_file host delete $t_tempfile | |
64bb95af | 59 | return |
252b5132 | 60 | } |
252b5132 | 61 | |
1fafefd5 JL |
62 | if { $type == "executable" } { |
63 | global LD | |
64 | # Check that LD exists | |
65 | if {[which $LD] == 0} then { | |
66 | untested "objcopy $type ($testname)" | |
67 | return | |
68 | } | |
69 | # Use tempfile and copyfile without the .o extension for executable files | |
70 | set t_tempfile [string range $tempfile 0 end-2] | |
71 | set t_copyfile $copyfile | |
72 | set got [binutils_run $LD "$tempfile -o $t_tempfile $ldflags"] | |
73 | if { ![string equal "" $got] } then { | |
74 | unresolved "objcopy $type ($testname)" | |
75 | return | |
76 | } | |
77 | } | |
78 | ||
79 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $t_tempfile $t_copyfile"] | |
64bb95af | 80 | |
21c40443 | 81 | if ![string equal "" $got] then { |
1fafefd5 | 82 | fail "objcopy $type ($testname)" |
252b5132 | 83 | } else { |
1fafefd5 JL |
84 | send_log "cmp $t_tempfile $t_copyfile\n" |
85 | verbose "cmp $t_tempfile $t_copyfile" | |
64bb95af | 86 | if [is_remote host] { |
1fafefd5 JL |
87 | set src1 tmpdir/bintest |
88 | set src2 tmpdir/copy | |
89 | remote_upload host $t_tempfile $src1 | |
90 | remote_upload host $t_copyfile $src2 | |
64bb95af | 91 | } else { |
1fafefd5 JL |
92 | set src1 $t_tempfile |
93 | set src2 $t_copyfile | |
64bb95af L |
94 | } |
95 | set status [remote_exec build cmp "${src1} ${src2}"] | |
96 | set exec_output [lindex $status 1] | |
97 | set exec_output [prune_warnings $exec_output] | |
98 | ||
99 | # On some systems the result of objcopy will not be identical. | |
100 | # Usually this is just because gas isn't using bfd to write the | |
101 | # files in the first place, and may order things a little | |
102 | # differently. Those systems should use setup_xfail here. | |
103 | ||
64bb95af | 104 | setup_xfail "hppa*-*-*" |
64bb95af | 105 | setup_xfail "m8*-*" |
b0a0c50a | 106 | setup_xfail "sh-*-coff*" |
d3def5d7 | 107 | setup_xfail "tic54x-*-*" |
64bb95af L |
108 | |
109 | clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*" | |
110 | clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*" | |
64bb95af | 111 | |
21c40443 | 112 | if [string equal "" $exec_output] then { |
1fafefd5 | 113 | pass "objcopy $type ($testname)" |
64bb95af L |
114 | } else { |
115 | send_log "$exec_output\n" | |
116 | verbose "$exec_output" 1 | |
252b5132 | 117 | |
64bb95af L |
118 | # On OSF/1, this succeeds with gas and fails with /bin/as. |
119 | setup_xfail "alpha*-*-osf*" | |
252b5132 | 120 | |
1fafefd5 | 121 | fail "objcopy $type ($testname)" |
64bb95af | 122 | } |
252b5132 RH |
123 | } |
124 | } | |
125 | ||
1fafefd5 | 126 | objcopy_test "simple copy" bintest.s object "" "" |
64bb95af | 127 | |
37d0d091 JH |
128 | # Test verilog data width |
129 | proc objcopy_test_verilog {testname} { | |
130 | global OBJCOPY | |
131 | global OBJCOPYFLAGS | |
132 | global srcdir | |
133 | global subdir | |
134 | global copyfile | |
135 | set binfile tmpdir/verilogtest.o | |
136 | set verilog tmpdir/verilog | |
137 | ||
138 | set got [binutils_assemble $srcdir/$subdir/verilogtest.s $binfile] | |
139 | if {![binutils_assemble $srcdir/$subdir/verilogtest.s $binfile]} then { | |
140 | unresolved "objcopy ($testname)" | |
141 | return | |
142 | } | |
143 | ||
144 | set got [binutils_run $OBJCOPY "-O verilog $binfile $verilog"] | |
145 | if ![string equal "" $got] then { | |
146 | fail "objcopy ($testname)" | |
147 | } | |
148 | ||
149 | set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width 0 $binfile $verilog-0.hex"] | |
150 | if ![regexp "verilog data width must be at least 1 byte" $got] then { | |
151 | fail "objcopy ($testname 0) {$got}" | |
152 | } else { | |
153 | pass "objcopy ($testname 0)" | |
154 | } | |
155 | ||
156 | foreach width {1 2 4 8} { | |
157 | set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width $width $binfile $verilog-$width.hex"] | |
158 | if ![string equal "" $got] then { | |
159 | fail "objcopy ($testname $width)" | |
160 | } | |
161 | send_log "regexp_diff $verilog-$width.hex $srcdir/$subdir/verilog-$width.hex\n" | |
162 | if {! [regexp_diff "$verilog-$width.hex" "$srcdir/$subdir/verilog-$width.hex"]} { | |
163 | pass "objcopy ($testname $width)" | |
164 | } else { | |
165 | fail "objcopy ($testname $width)" | |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | objcopy_test_verilog "verilog data width" | |
171 | ||
96037eb0 NC |
172 | if { [file exists $tempfile] } { |
173 | # Test reversing bytes in a section. | |
9e48b4c6 | 174 | |
96037eb0 NC |
175 | set reversed ${tempfile}-reversed |
176 | set sect_names [get_standard_section_names] | |
177 | if { $sect_names != "" } { | |
178 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"] | |
179 | } else { | |
180 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"] | |
9e48b4c6 NC |
181 | } |
182 | ||
96037eb0 NC |
183 | if ![string equal "" $got] then { |
184 | fail "objcopy --reverse-bytes" | |
185 | } else { | |
186 | if [is_remote host] { | |
187 | remote_upload host ${reversed} tmpdir/copy-reversed.o | |
188 | set reversed tmpdir/copy-reversed.o | |
189 | } | |
9e48b4c6 | 190 | |
96037eb0 NC |
191 | set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"] |
192 | set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"] | |
9e48b4c6 | 193 | |
96037eb0 NC |
194 | set want "^ \[0-9\]+ (\[0-9\]+)" |
195 | set found_orig [regexp -lineanchor $want $origdata -> origdata] | |
196 | set found_rev [regexp -lineanchor $want $revdata -> revdata] | |
8941d5e8 | 197 | |
96037eb0 NC |
198 | if {$found_orig == 0 || $found_rev == 0} then { |
199 | # som doesn't have a .data section | |
200 | setup_xfail "hppa*-*-hpux*" | |
201 | clear_xfail "hppa*64*-*-hpux*" | |
9e48b4c6 | 202 | |
96037eb0 NC |
203 | fail "objcopy --reverse-bytes" |
204 | } else { | |
205 | scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4 | |
206 | scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1 | |
207 | ||
208 | if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then { | |
209 | pass "objcopy --reverse-bytes" | |
210 | } else { | |
211 | fail "objcopy --reverse-bytes" | |
212 | } | |
213 | } | |
9e48b4c6 NC |
214 | } |
215 | } | |
216 | ||
b7dd81f7 NC |
217 | # Test interleaved copy of multiple byte width |
218 | ||
219 | set sequence_file sequence_file | |
220 | set file [open ${sequence_file} w] | |
221 | puts ${file} "12345678" | |
222 | close ${file} | |
223 | ||
224 | if [is_remote host] { | |
225 | remote_upload host ${sequence_file} tmpdir/sequence_file | |
226 | set sequence_file tmpdir/sequence_file | |
227 | } | |
228 | ||
229 | set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"] | |
230 | ||
21c40443 | 231 | if ![string equal "" $got] then { |
b7dd81f7 NC |
232 | fail "objcopy -i --interleave-width" |
233 | } else { | |
234 | if [is_remote host] { | |
235 | remote_upload host ${copyfile} tmpdir/interleave_output | |
236 | set interleave_output tmpdir/interleave_output | |
237 | } else { | |
238 | set interleave_output ${copyfile} | |
239 | } | |
240 | ||
241 | set file [open ${interleave_output} r] | |
242 | gets $file line | |
243 | send_log "$line\n" | |
244 | verbose $line | |
245 | ||
21c40443 | 246 | if ![string equal "1256" $line] then { |
b7dd81f7 NC |
247 | fail "objcopy -i --interleave-width" |
248 | } | |
249 | pass "objcopy -i --interleave-width" | |
250 | ||
251 | close $file | |
252 | } | |
253 | ||
252b5132 RH |
254 | # Test generating S records. |
255 | ||
96037eb0 NC |
256 | if { [file exists $tempfile] } { |
257 | # We make the srec filename 8.3 compatible. Note that the header string | |
258 | # matched against depends on the name of the file. Ugh. | |
252b5132 | 259 | |
252b5132 | 260 | if [is_remote host] { |
96037eb0 NC |
261 | set srecfile copy.sre |
262 | set header_string S00B0000636F70792E737265C1 | |
263 | } else { | |
264 | set srecfile ${copyfile}.srec | |
265 | set header_string S0130000746D706469722F636F70792E7372656397 | |
252b5132 | 266 | } |
252b5132 | 267 | |
96037eb0 NC |
268 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"] |
269 | ||
270 | if ![string equal "" $got] then { | |
252b5132 RH |
271 | fail "objcopy -O srec" |
272 | } else { | |
96037eb0 NC |
273 | if [is_remote host] { |
274 | remote_upload host ${srecfile} tmpdir/copy.srec | |
275 | set srecfile tmpdir/copy.srec | |
252b5132 | 276 | } |
96037eb0 NC |
277 | set file [open ${srecfile} r] |
278 | ||
279 | # The first S record is fixed by the file name we are using. | |
280 | gets $file line | |
252b5132 RH |
281 | send_log "$line\n" |
282 | verbose $line | |
96037eb0 NC |
283 | if ![regexp "$header_string.*" $line] { |
284 | send_log "bad header\n" | |
252b5132 RH |
285 | fail "objcopy -O srec" |
286 | } else { | |
96037eb0 NC |
287 | while {[gets $file line] != -1 \ |
288 | && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} { | |
252b5132 RH |
289 | send_log "$line\n" |
290 | verbose $line | |
96037eb0 NC |
291 | set line "**EOF**" |
292 | } | |
293 | send_log "$line\n" | |
294 | verbose $line | |
295 | if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then { | |
296 | send_log "bad trailer\n" | |
252b5132 RH |
297 | fail "objcopy -O srec" |
298 | } else { | |
96037eb0 NC |
299 | if {[gets $file line] != -1} then { |
300 | send_log "garbage at end\n" | |
301 | send_log "$line\n" | |
302 | verbose $line | |
252b5132 RH |
303 | fail "objcopy -O srec" |
304 | } else { | |
96037eb0 NC |
305 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"] |
306 | if ![regexp "file format srec" $got] then { | |
307 | send_log "objdump failed\n" | |
308 | fail "objcopy -O srec" | |
309 | } else { | |
310 | pass "objcopy -O srec" | |
311 | } | |
252b5132 RH |
312 | } |
313 | } | |
314 | } | |
252b5132 | 315 | |
96037eb0 NC |
316 | close $file |
317 | } | |
252b5132 RH |
318 | } |
319 | ||
320 | # Test setting and adjusting the start address. We only test this | |
321 | # while generating S records, because we may not be able to set the | |
322 | # start address for other object file formats, and the S record case | |
323 | # is the only useful one anyhow. | |
324 | ||
325 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"] | |
326 | if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then { | |
327 | perror "objdump can not recognize bintest.o" | |
328 | set origstart "" | |
329 | } else { | |
627fe3fb | 330 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"] |
21c40443 | 331 | if ![string equal "" $got] then { |
252b5132 RH |
332 | fail "objcopy --set-start" |
333 | } else { | |
334 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"] | |
335 | if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then { | |
336 | fail "objcopy --set-start" | |
337 | } else { | |
338 | if {$srecstart != 0x7654} then { | |
339 | send_log "$srecstart != 0x7654\n" | |
340 | fail "objcopy --set-start" | |
341 | } else { | |
342 | pass "objcopy --set-start" | |
343 | } | |
344 | } | |
345 | } | |
346 | ||
627fe3fb | 347 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"] |
21c40443 | 348 | if ![string equal "" $got] then { |
252b5132 RH |
349 | fail "objcopy --adjust-start" |
350 | } else { | |
351 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"] | |
352 | if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then { | |
353 | fail "objcopy --adjust-start" | |
354 | } else { | |
355 | if {$srecstart != $origstart + 0x123} then { | |
356 | send_log "$srecstart != $origstart + 0x123\n" | |
357 | fail "objcopy --adjust-start" | |
358 | } else { | |
359 | pass "objcopy --adjust-start" | |
360 | } | |
361 | } | |
362 | } | |
363 | } | |
364 | ||
365 | # Test adjusting the overall VMA, and adjusting the VMA of a | |
366 | # particular section. We again only test this when generating S | |
367 | # records. | |
368 | ||
369 | set low "" | |
370 | set lowname "" | |
371 | ||
372 | set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"] | |
373 | ||
374 | set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)" | |
375 | ||
376 | set got $headers | |
377 | while {[regexp $headers_regexp $got all name size vma rest]} { | |
378 | set vma 0x$vma | |
379 | set size 0x$size | |
380 | if {$size != 0} { | |
381 | if {$low == "" || $vma < $low} { | |
382 | set low $vma | |
383 | set lowname $name | |
384 | } | |
385 | } | |
386 | set got $rest | |
387 | } | |
388 | ||
389 | if {$low == "" || $origstart == ""} then { | |
390 | perror "objdump can not recognize bintest.o" | |
391 | } else { | |
627fe3fb | 392 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"] |
21c40443 | 393 | if ![string equal "" $got] then { |
252b5132 RH |
394 | fail "objcopy --adjust-vma" |
395 | } else { | |
396 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"] | |
397 | set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)" | |
398 | if ![regexp $want $got all start vma] then { | |
399 | fail "objcopy --adjust-vma" | |
400 | } else { | |
401 | set vma 0x$vma | |
402 | if {$vma != $low + 0x123} then { | |
403 | send_log "$vma != $low + 0x123\n" | |
404 | fail "objcopy --adjust-vma" | |
405 | } else { | |
406 | if {$start != $origstart + 0x123} then { | |
407 | send_log "$start != $origstart + 0x123\n" | |
408 | fail "objcopy --adjust-vma" | |
409 | } else { | |
410 | pass "objcopy --adjust-vma" | |
411 | } | |
412 | } | |
413 | } | |
414 | } | |
415 | ||
416 | set arg "" | |
417 | set got $headers | |
418 | while {[regexp $headers_regexp $got all name size vma rest]} { | |
419 | set vma 0x$vma | |
420 | if {$vma == $low} then { | |
421 | set arg "$arg --adjust-section-vma $name+4" | |
422 | } | |
423 | set got $rest | |
424 | } | |
425 | ||
627fe3fb | 426 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"] |
21c40443 | 427 | if ![string equal "" $got] then { |
252b5132 RH |
428 | fail "objcopy --adjust-section-vma +" |
429 | } else { | |
430 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"] | |
431 | set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)" | |
432 | if ![regexp $want $got all vma] then { | |
433 | fail "objcopy --adjust-section-vma +" | |
434 | } else { | |
435 | set vma 0x$vma | |
436 | if {$vma != $low + 4} then { | |
437 | send_log "$vma != $low + 4\n" | |
438 | fail "objcopy --adjust-section-vma +" | |
439 | } else { | |
440 | pass "objcopy --adjust-section-vma +" | |
441 | } | |
442 | } | |
443 | } | |
444 | ||
445 | regsub -all "\\+4" $arg "=[expr $low + 4]" argeq | |
627fe3fb | 446 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"] |
21c40443 | 447 | if ![string equal "" $got] then { |
252b5132 RH |
448 | fail "objcopy --adjust-section-vma =" |
449 | } else { | |
450 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"] | |
451 | set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)" | |
452 | if ![regexp $want $got all vma] then { | |
453 | fail "objcopy --adjust-section-vma =" | |
454 | } else { | |
455 | set vma 0x$vma | |
456 | if {$vma != $low + 4} then { | |
457 | send_log "$vma != $low + 4\n" | |
458 | fail "objcopy --adjust-section-vma =" | |
459 | } else { | |
460 | pass "objcopy --adjust-section-vma =" | |
461 | } | |
462 | } | |
463 | } | |
464 | } | |
465 | ||
466 | # Test stripping an object. | |
467 | ||
468 | proc strip_test { } { | |
1983fbac | 469 | global AR |
252b5132 RH |
470 | global CC |
471 | global STRIP | |
472 | global STRIPFLAGS | |
473 | global NM | |
474 | global NMFLAGS | |
475 | global srcdir | |
476 | global subdir | |
28902555 | 477 | global READELF |
252b5132 RH |
478 | |
479 | set test "strip" | |
480 | ||
481 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { | |
482 | untested $test | |
483 | return | |
484 | } | |
485 | ||
28902555 BS |
486 | set osabi_fail "false" |
487 | if [is_elf_format] { | |
488 | verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in" | |
489 | set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"] | |
490 | if { [lindex $exec_output 0] != 0 } then { | |
21c40443 | 491 | unresolved "$test preserving OS/ABI" |
28902555 BS |
492 | set osabi_fail "true" |
493 | } else { | |
494 | verbose -log "grep OS/ABI tmpdir/osabi.in" | |
495 | catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in | |
496 | } | |
497 | } | |
498 | ||
252b5132 | 499 | if [is_remote host] { |
1983fbac | 500 | set archive libstrip.a |
8d263650 | 501 | set objfile [remote_download host tmpdir/testprog.o] |
1983fbac L |
502 | remote_file host delete $archive |
503 | } else { | |
504 | set archive tmpdir/libstrip.a | |
505 | set objfile tmpdir/testprog.o | |
506 | } | |
507 | ||
508 | remote_file build delete tmpdir/libstrip.a | |
509 | ||
510 | set exec_output [binutils_run $AR "rc $archive ${objfile}"] | |
7dd36a6f | 511 | set exec_output [prune_warnings $exec_output] |
21c40443 | 512 | if ![string equal "" $exec_output] { |
1983fbac | 513 | fail $test |
21c40443 | 514 | unresolved "$test preserving OS/ABI" |
1983fbac L |
515 | return |
516 | } | |
517 | ||
8648f88f | 518 | set exec_output [binutils_run $STRIP "-g $archive"] |
7dd36a6f | 519 | set exec_output [prune_warnings $exec_output] |
21c40443 | 520 | if ![string equal "" $exec_output] { |
8648f88f | 521 | fail $test |
21c40443 | 522 | unresolved "$test preserving OS/ABI" |
8648f88f L |
523 | return |
524 | } | |
525 | ||
1983fbac | 526 | set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"] |
7dd36a6f | 527 | set exec_output [prune_warnings $exec_output] |
21c40443 | 528 | if ![string equal "" $exec_output] { |
1983fbac | 529 | fail $test |
21c40443 | 530 | unresolved "$test preserving OS/ABI" |
1983fbac L |
531 | return |
532 | } | |
533 | ||
28902555 BS |
534 | if { $osabi_fail != "true" && [is_elf_format] } { |
535 | verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out" | |
536 | set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"] | |
537 | if { [lindex $exec_output 0] != 0 } then { | |
21c40443 | 538 | unresolved "$test preserving OS/ABI" |
28902555 BS |
539 | } else { |
540 | verbose -log "grep OS/ABI tmpdir/osabi.out" | |
541 | catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out | |
542 | if { "$osabi_in" == "$osabi_out" } { | |
543 | pass "$test preserving OS/ABI" | |
544 | } else { | |
545 | fail "$test preserving OS/ABI" | |
546 | } | |
547 | } | |
548 | } | |
549 | ||
1983fbac L |
550 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { |
551 | untested $test | |
552 | return | |
553 | } | |
554 | ||
555 | if [is_remote host] { | |
8d263650 | 556 | set objfile [remote_download host tmpdir/testprog.o] |
252b5132 RH |
557 | } else { |
558 | set objfile tmpdir/testprog.o | |
559 | } | |
560 | ||
561 | set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"] | |
7dd36a6f | 562 | set exec_output [prune_warnings $exec_output] |
21c40443 | 563 | if ![string equal "" $exec_output] { |
252b5132 RH |
564 | fail $test |
565 | return | |
566 | } | |
567 | ||
568 | set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"] | |
7dd36a6f | 569 | set exec_output [prune_warnings $exec_output] |
68a4c073 | 570 | if ![string match "*: no symbols*" $exec_output] { |
252b5132 RH |
571 | fail $test |
572 | return | |
573 | } | |
574 | ||
575 | pass $test | |
576 | } | |
577 | ||
578 | strip_test | |
579 | ||
580 | # Test stripping an object file with saving a symbol | |
581 | ||
582 | proc strip_test_with_saving_a_symbol { } { | |
583 | global CC | |
584 | global STRIP | |
585 | global STRIPFLAGS | |
586 | global NM | |
587 | global NMFLAGS | |
588 | global srcdir | |
589 | global subdir | |
590 | ||
591 | set test "strip with saving a symbol" | |
592 | ||
593 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { | |
594 | untested $test | |
595 | return | |
596 | } | |
597 | ||
598 | if [is_remote host] { | |
8d263650 | 599 | set objfile [remote_download host tmpdir/testprog.o] |
252b5132 RH |
600 | } else { |
601 | set objfile tmpdir/testprog.o | |
602 | } | |
603 | ||
604 | set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"] | |
7dd36a6f | 605 | set exec_output [prune_warnings $exec_output] |
21c40443 | 606 | if ![string equal "" $exec_output] { |
252b5132 RH |
607 | fail $test |
608 | return | |
609 | } | |
610 | ||
611 | set exec_output [binutils_run $NM "$NMFLAGS $objfile"] | |
7dd36a6f | 612 | set exec_output [prune_warnings $exec_output] |
195fe636 | 613 | if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \ |
252b5132 RH |
614 | && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} { |
615 | fail $test | |
616 | return | |
617 | } | |
618 | ||
619 | pass $test | |
620 | } | |
621 | ||
622 | strip_test_with_saving_a_symbol | |
623 | ||
624 | # Build a final executable. | |
625 | ||
99ad8390 | 626 | if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } { |
f8a7038a NC |
627 | set test_prog "testprog.exe" |
628 | } else { | |
629 | set test_prog "testprog" | |
630 | } | |
631 | ||
252b5132 RH |
632 | proc copy_setup { } { |
633 | global srcdir | |
634 | global subdir | |
9a580d9a | 635 | global gcc_gas_flag |
f8a7038a | 636 | global test_prog |
72d4c02f | 637 | global host_triplet |
21c40443 | 638 | |
8d263650 BE |
639 | set res [build_wrapper testglue.o] |
640 | set flags { debug } | |
21c40443 | 641 | |
f1ac0afe | 642 | if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] && ![istarget arm*-*-uclinuxfdpiceabi] } { |
903b1f5b NS |
643 | return 1 |
644 | } | |
21c40443 | 645 | |
252b5132 | 646 | if { $res != "" } { |
8d263650 BE |
647 | lappend flags "additional_flags=[lindex $res 1]" |
648 | set add_libs "testglue.o" | |
252b5132 | 649 | } else { |
8d263650 | 650 | set add_libs "" |
252b5132 RH |
651 | } |
652 | ||
5940a93c TS |
653 | if { [istarget *-*-linux*] |
654 | || [istarget *-*-gnu*] } { | |
9a580d9a L |
655 | foreach i $gcc_gas_flag { |
656 | set flags "additional_flags=$i $flags" | |
657 | } | |
658 | } | |
f8a7038a | 659 | if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } { |
252b5132 RH |
660 | return 2 |
661 | } | |
662 | ||
8d263650 BE |
663 | set result [remote_load target tmpdir/$test_prog] |
664 | set status [lindex $result 0] | |
252b5132 RH |
665 | |
666 | if { $status != "pass" } { | |
72d4c02f | 667 | send_log "cannot run executable, status = ${status} on ${host_triplet}\n" |
21c40443 | 668 | return 3 |
252b5132 RH |
669 | } |
670 | ||
671 | return 0 | |
672 | } | |
673 | ||
674 | # Test copying an executable. | |
675 | ||
676 | proc copy_executable { prog flags test1 test2 } { | |
f8a7038a | 677 | global test_prog |
252b5132 RH |
678 | |
679 | if [is_remote host] { | |
8d263650 | 680 | set testfile [remote_download host tmpdir/$test_prog] |
252b5132 RH |
681 | set testcopy copyprog |
682 | } else { | |
f8a7038a | 683 | set testfile tmpdir/$test_prog |
252b5132 RH |
684 | set testcopy tmpdir/copyprog |
685 | } | |
8d263650 | 686 | remote_file host delete $testcopy |
252b5132 RH |
687 | |
688 | set exec_output [binutils_run $prog "$flags $testfile $testcopy"] | |
689 | ||
21c40443 | 690 | if ![string equal "" $exec_output] { |
252b5132 | 691 | fail $test1 |
21c40443 | 692 | if [string equal "" $test2] { |
0d063f63 AM |
693 | return |
694 | } | |
252b5132 RH |
695 | fail $test2 |
696 | return | |
697 | } | |
698 | ||
699 | if [is_remote host] { | |
700 | remote_upload host $testcopy tmpdir/copyprog | |
701 | } | |
702 | ||
f8a7038a | 703 | set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"] |
8d263650 | 704 | set exec_output [lindex $status 1] |
252b5132 | 705 | |
21c40443 | 706 | if [string equal "" $exec_output] then { |
252b5132 RH |
707 | pass $test1 |
708 | } else { | |
709 | send_log "$exec_output\n" | |
710 | verbose "$exec_output" | |
711 | ||
712 | # This will fail for many reasons. For example, it will most | |
713 | # likely fail if a non-GNU linker is used. Therefore, we do | |
714 | # not insist that it pass. If you are using an assembler and | |
715 | # linker based on the same BFD as objcopy, it is worth | |
716 | # investigating to see why this failure occurs. If we are | |
717 | # cross compiling, we assume that a GNU linker is being used, | |
718 | # and expect it to succeed. | |
719 | if {[isnative]} then { | |
720 | setup_xfail "*-*-*" | |
721 | } | |
722 | ||
9cc0123f | 723 | # This also fails for some mips targets. See elf32-mips.c |
252b5132 | 724 | # mips_elf_sym_is_global. |
9cc0123f AM |
725 | if { [is_bad_symtab] } then { |
726 | setup_xfail "*-*-*" | |
727 | } | |
252b5132 | 728 | |
11d47adc | 729 | setup_xfail "arm*-*-pe" |
88f14853 | 730 | setup_xfail "*-*-mingw*" |
899396fe | 731 | setup_xfail "*-*-cygwin*" |
21c40443 | 732 | |
252b5132 RH |
733 | fail $test1 |
734 | } | |
735 | ||
21c40443 | 736 | if [string equal "" $test2] { |
0d063f63 AM |
737 | return |
738 | } | |
739 | ||
252b5132 | 740 | set output [remote_load target tmpdir/copyprog] |
8d263650 | 741 | set status [lindex $output 0] |
252b5132 RH |
742 | if { $status != "pass" } { |
743 | fail $test2 | |
744 | } else { | |
745 | pass $test2 | |
746 | } | |
747 | } | |
748 | ||
749 | # Test stripping an executable | |
750 | ||
21c40443 | 751 | proc strip_executable { prog flags test1 test2 } { |
252b5132 RH |
752 | global NM |
753 | global NMFLAGS | |
28902555 | 754 | global READELF |
252b5132 | 755 | |
0fcc17f8 | 756 | remote_file build delete tmpdir/striprog |
252b5132 RH |
757 | remote_download build tmpdir/copyprog tmpdir/striprog |
758 | if [is_remote host] { | |
8d263650 | 759 | set copyfile [remote_download host tmpdir/striprog] |
252b5132 RH |
760 | } else { |
761 | set copyfile tmpdir/striprog | |
762 | } | |
763 | ||
28902555 BS |
764 | set osabi_fail "false" |
765 | if [is_elf_format] { | |
766 | verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in" | |
767 | set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"] | |
768 | if { [lindex $exec_output 0] != 0 } then { | |
21c40443 | 769 | unresolved "$test1 preserving OS/ABI" |
28902555 BS |
770 | set osabi_fail "true" |
771 | } else { | |
772 | verbose -log "grep OS/ABI tmpdir/osabi.in" | |
773 | catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in | |
774 | } | |
775 | } | |
776 | ||
252b5132 | 777 | set exec_output [binutils_run $prog "$flags ${copyfile}"] |
21c40443 AM |
778 | if ![string equal "" $exec_output] { |
779 | fail $test1 | |
780 | if [string equal "" $test2] { | |
781 | return | |
782 | } | |
783 | fail $test2 | |
252b5132 RH |
784 | return |
785 | } | |
786 | ||
787 | if [is_remote host] { | |
8d263650 | 788 | remote_upload host ${copyfile} tmpdir/striprog |
252b5132 RH |
789 | } |
790 | ||
28902555 BS |
791 | if { $osabi_fail != "true" && [is_elf_format] } { |
792 | verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out" | |
793 | set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"] | |
794 | if { [lindex $exec_output 0] != 0 } then { | |
21c40443 | 795 | unresolved "$test1 preserving OS/ABI" |
28902555 BS |
796 | } else { |
797 | verbose -log "grep OS/ABI tmpdir/osabi.out" | |
798 | catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out | |
799 | if { "$osabi_in" == "$osabi_out" } { | |
21c40443 | 800 | pass "$test1 preserving OS/ABI" |
28902555 | 801 | } else { |
21c40443 | 802 | fail "$test1 preserving OS/ABI" |
28902555 BS |
803 | } |
804 | } | |
805 | } | |
806 | ||
252b5132 | 807 | set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"] |
68a4c073 | 808 | if ![string match "*: no symbols*" $exec_output] { |
21c40443 | 809 | fail $test1 |
252b5132 RH |
810 | return |
811 | } | |
21c40443 AM |
812 | |
813 | if [string equal "" $test2] { | |
814 | return | |
815 | } | |
816 | ||
817 | set result [remote_load target tmpdir/striprog] | |
818 | set status [lindex $result 0] | |
819 | if { $status != "pass" } { | |
820 | fail $test2 | |
821 | return | |
822 | } | |
823 | ||
824 | pass $test2 | |
252b5132 RH |
825 | } |
826 | ||
827 | # Test stripping an executable with saving a symbol | |
828 | ||
21c40443 | 829 | proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } { |
252b5132 RH |
830 | global NM |
831 | global NMFLAGS | |
832 | ||
0fcc17f8 | 833 | remote_file build delete tmpdir/striprog |
252b5132 RH |
834 | remote_download build tmpdir/copyprog tmpdir/striprog |
835 | if [is_remote host] { | |
8d263650 | 836 | set copyfile [remote_download host tmpdir/striprog] |
252b5132 RH |
837 | } else { |
838 | set copyfile tmpdir/striprog | |
839 | } | |
840 | ||
841 | set exec_output [binutils_run $prog "$flags ${copyfile}"] | |
21c40443 AM |
842 | if ![string equal "" $exec_output] { |
843 | fail $test1 | |
844 | if [string equal "" $test2] { | |
845 | return | |
846 | } | |
847 | fail $test2 | |
252b5132 RH |
848 | return |
849 | } | |
850 | ||
252b5132 | 851 | set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"] |
edc45ab9 HPN |
852 | if { [istarget mmix-knuth-mmixware] } { |
853 | # Whenever there's a symbol in the mmo format, there's the symbol | |
854 | # Main, so remove it manually from the expected output for sake of | |
855 | # this test. | |
856 | ||
857 | # Using "" not {} to get the \n and \r translated. | |
858 | regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output | |
859 | } | |
860 | ||
adacfc81 JJ |
861 | if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \ |
862 | && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} { | |
21c40443 | 863 | fail $test1 |
252b5132 RH |
864 | return |
865 | } | |
21c40443 AM |
866 | |
867 | if [string equal "" $test2] { | |
868 | return | |
869 | } | |
870 | ||
871 | if [is_remote host] { | |
872 | remote_upload host ${copyfile} tmpdir/striprog | |
873 | } | |
874 | ||
875 | set result [remote_load target tmpdir/striprog] | |
876 | set status [lindex $result 0] | |
877 | if { $status != "pass" } { | |
878 | fail $test2 | |
879 | return | |
880 | } | |
881 | ||
882 | pass $test2 | |
252b5132 RH |
883 | } |
884 | ||
11701589 JK |
885 | # Test keeping only debug symbols of an executable |
886 | ||
887 | proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } { | |
0fcc17f8 | 888 | remote_file build delete tmpdir/striprog |
11701589 JK |
889 | remote_download build tmpdir/copyprog tmpdir/striprog |
890 | if [is_remote host] { | |
891 | set copyfile [remote_download host tmpdir/striprog] | |
892 | } else { | |
893 | set copyfile tmpdir/striprog | |
894 | } | |
895 | ||
896 | set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"] | |
21c40443 | 897 | if ![string equal "" $exec_output] { |
11701589 JK |
898 | fail $test1 |
899 | return | |
900 | } | |
901 | pass $test1 | |
902 | ||
903 | set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"] | |
21c40443 | 904 | if ![string equal "" $exec_output] { |
11701589 JK |
905 | fail $test2 |
906 | return | |
907 | } | |
908 | pass $test2 | |
909 | } | |
910 | ||
63b9bbb7 | 911 | # Tests that in a debug only copy of a file the sections |
de194d85 | 912 | # headers whose types have been changed to NOBITS still |
63b9bbb7 NC |
913 | # retain their sh_link fields. |
914 | ||
915 | proc keep_debug_symbols_and_check_links { prog flags test } { | |
916 | global READELF | |
917 | ||
918 | remote_file build delete tmpdir/striprog | |
919 | remote_download build tmpdir/copyprog tmpdir/striprog | |
920 | if [is_remote host] { | |
921 | set copyfile [remote_download host tmpdir/striprog] | |
922 | } else { | |
923 | set copyfile tmpdir/striprog | |
924 | } | |
925 | ||
926 | set exec_output [binutils_run $prog "$flags ${copyfile}"] | |
21c40443 | 927 | if ![string equal "" $exec_output] { |
63b9bbb7 NC |
928 | fail $test |
929 | return | |
930 | } | |
931 | ||
932 | set got [binutils_run $READELF "-S --wide ${copyfile}"] | |
933 | ||
934 | set fails 0 | |
935 | # Regexp to match a section with NOBITS type and extract its name and sh_link fields | |
936 | while {[regexp \ | |
937 | {[^a-zA-Z]+([a-zA-Z0-9_\.]+)[ ]+NOBITS[ ]+[0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9]+[ A]+([0-9]+)(.*)} \ | |
938 | $got all name link rest]} { | |
939 | set sh_link 0x$link | |
940 | if {$sh_link == 0} { | |
941 | # Only some NOBITS sections should have a non-zero sh_link field. | |
942 | # Look for them by name. | |
943 | verbose "NOBITS section .$name has a 0 sh_link field\n" | |
944 | switch $name { | |
945 | "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
946 | "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
947 | "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
948 | "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
949 | "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
950 | "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
951 | "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
952 | "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" } | |
953 | } | |
954 | } | |
955 | set got $rest | |
956 | } | |
957 | ||
958 | if {$fails == 0} { | |
959 | pass $test | |
960 | } else { | |
961 | fail $test | |
962 | } | |
963 | } | |
964 | ||
965 | ||
252b5132 | 966 | set test1 "simple objcopy of executable" |
21c40443 AM |
967 | set test1r "run objcopy of executable" |
968 | set test2 "strip executable" | |
969 | set test2r "run stripped executable" | |
970 | set test3 "strip executable with saving a symbol" | |
971 | set test3r "run stripped executable with saving a symbol" | |
972 | set test4 "keep only debug data" | |
973 | set test5 "simple objcopy of debug data" | |
63b9bbb7 | 974 | if [is_elf_format] { |
21c40443 | 975 | set test6 "NOBITS sections retain sh_link field" |
63b9bbb7 | 976 | } |
252b5132 RH |
977 | |
978 | switch [copy_setup] { | |
979 | "1" { | |
980 | # do nothing | |
981 | } | |
982 | "2" { | |
983 | untested $test1 | |
21c40443 | 984 | untested $test1r |
252b5132 | 985 | untested $test2 |
21c40443 | 986 | untested $test2r |
252b5132 | 987 | untested $test3 |
21c40443 | 988 | untested $test3r |
252b5132 | 989 | untested $test4 |
11701589 | 990 | untested $test5 |
63b9bbb7 | 991 | if [is_elf_format] { |
21c40443 | 992 | untested $test6 |
63b9bbb7 | 993 | } |
252b5132 RH |
994 | } |
995 | "3" { | |
0d063f63 | 996 | copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "" |
21c40443 AM |
997 | unsupported $test1r |
998 | strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "" | |
999 | unsupported $test2r | |
1000 | strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "" | |
1001 | unsupported $test3r | |
1002 | keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \ | |
1003 | "$OBJCOPY" "$OBJCOPYFLAGS" "$test5" | |
63b9bbb7 | 1004 | if [is_elf_format] { |
21c40443 | 1005 | keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6" |
63b9bbb7 | 1006 | } |
252b5132 RH |
1007 | } |
1008 | "0" { | |
21c40443 AM |
1009 | copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r" |
1010 | strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r" | |
1011 | strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r" | |
1012 | keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \ | |
1013 | "$OBJCOPY" "$OBJCOPYFLAGS" "$test5" | |
63b9bbb7 | 1014 | if [is_elf_format] { |
21c40443 | 1015 | keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6" |
63b9bbb7 | 1016 | } |
252b5132 RH |
1017 | } |
1018 | } | |
ad2fb2cd | 1019 | |
6a0d0afd L |
1020 | proc objcopy_test_readelf {testname srcfile} { |
1021 | global OBJCOPY | |
1022 | global OBJCOPYFLAGS | |
1023 | global READELF | |
1024 | global srcdir | |
1025 | global subdir | |
1026 | ||
1027 | if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then { | |
1028 | unresolved "objcopy ($testname)" | |
1029 | return | |
1030 | } | |
1031 | ||
1032 | verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o" | |
7f6a71ff JM |
1033 | set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"] |
1034 | if { [lindex $exec_output 0] != 0 | |
21c40443 | 1035 | || ![string equal "" [lindex $exec_output 1]] } then { |
6a0d0afd | 1036 | fail "objcopy ($testname)" |
004314cc | 1037 | return |
6a0d0afd L |
1038 | } |
1039 | ||
1040 | verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out" | |
7f6a71ff JM |
1041 | set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"] |
1042 | if { [lindex $exec_output 0] != 0 } then { | |
1043 | unresolved "objcopy ($testname)" | |
1044 | return | |
1045 | } | |
1046 | set exec_output [prune_warnings [lindex $exec_output 1]] | |
21c40443 | 1047 | if ![string equal "" $exec_output] then { |
6a0d0afd L |
1048 | unresolved "objcopy ($testname)" |
1049 | return | |
1050 | } | |
1051 | ||
1052 | verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out" | |
7f6a71ff JM |
1053 | set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"] |
1054 | if { [lindex $exec_output 0] != 0 } then { | |
1055 | unresolved "objcopy ($testname)" | |
1056 | return | |
1057 | } | |
1058 | set exec_output [prune_warnings [lindex $exec_output 1]] | |
21c40443 | 1059 | if ![string equal "" $exec_output] then { |
6a0d0afd L |
1060 | unresolved "objcopy ($testname)" |
1061 | return | |
1062 | } | |
1063 | ||
1064 | verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out" | |
1065 | catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output | |
1066 | set exec_output [prune_warnings $exec_output] | |
1067 | ||
21c40443 | 1068 | if [string equal "" $exec_output] then { |
6a0d0afd L |
1069 | pass "objcopy ($testname)" |
1070 | } else { | |
1071 | fail "objcopy ($testname)" | |
1072 | } | |
1073 | } | |
1074 | ||
0b45135e AB |
1075 | proc objcopy_test_symbol_manipulation {} { |
1076 | global srcdir | |
1077 | global subdir | |
1078 | ||
1079 | set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]] | |
1080 | foreach t $test_list { | |
1081 | # We need to strip the ".d", but can leave the dirname. | |
1082 | verbose [file rootname $t] | |
1083 | run_dump_test [file rootname $t] | |
1084 | } | |
1085 | } | |
1086 | ||
b8871f35 L |
1087 | proc objcopy_test_elf_common_symbols {} { |
1088 | global srcdir | |
1089 | global subdir | |
1090 | ||
1091 | # hpux has a non-standard common directive. | |
1092 | if { [istarget "*-*-hpux*"] } then { | |
1093 | return | |
1094 | } | |
1095 | ||
1096 | set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]] | |
1097 | foreach t $test_list { | |
1098 | # We need to strip the ".d", but can leave the dirname. | |
1099 | verbose [file rootname $t] | |
1100 | run_dump_test [file rootname $t] | |
1101 | } | |
1102 | } | |
1103 | ||
ad2fb2cd L |
1104 | # ia64 specific tests |
1105 | if { ([istarget "ia64-*-elf*"] | |
1106 | || [istarget "ia64-*-linux*"]) } { | |
1fafefd5 | 1107 | objcopy_test "ia64 link order" link-order.s object "" "" |
64bb95af | 1108 | } |
ad2fb2cd | 1109 | |
64bb95af | 1110 | # ELF specific tests |
506b86a4 | 1111 | set elf64 "" |
64bb95af | 1112 | if [is_elf_format] { |
0b45135e | 1113 | objcopy_test_symbol_manipulation |
b8871f35 | 1114 | objcopy_test_elf_common_symbols |
1fafefd5 | 1115 | objcopy_test "ELF unknown section type" unknown.s object "" "" |
a0a8a934 NC |
1116 | objcopy_test_readelf "ELF group 1" group.s |
1117 | objcopy_test_readelf "ELF group 2" group-2.s | |
1118 | objcopy_test_readelf "ELF group 3" group-3.s | |
1119 | objcopy_test_readelf "ELF group 4" group-4.s | |
a91e1603 | 1120 | objcopy_test_readelf "GNU_MBIND section" mbind1.s |
b2fc24d4 | 1121 | run_dump_test "group-5" |
4c8e8a7e | 1122 | run_dump_test "group-6" |
6e5e9d58 AM |
1123 | run_dump_test "group-7a" |
1124 | run_dump_test "group-7b" | |
1125 | run_dump_test "group-7c" | |
85d7f0b9 | 1126 | run_dump_test "copy-1" |
0930eddd | 1127 | run_dump_test "note-1" |
acea835f AM |
1128 | # Use copytest.o from the note-1 test to determine ELF32 or ELF64 |
1129 | if [is_elf64 tmpdir/copytest.o] { | |
506b86a4 | 1130 | set elf64 "--defsym ELF64=1" |
9ef920e9 | 1131 | run_dump_test "note-2-64" |
714da62f | 1132 | run_dump_test "note-3-64" |
6f156d7a | 1133 | run_dump_test "note-4-64" |
5c49f2cd | 1134 | run_dump_test "note-6-64" |
9ef920e9 NC |
1135 | } else { |
1136 | run_dump_test "note-2-32" | |
714da62f | 1137 | run_dump_test "note-3-32" |
6f156d7a | 1138 | run_dump_test "note-4-32" |
5c49f2cd | 1139 | run_dump_test "note-6-32" |
9ef920e9 | 1140 | } |
0df8ad28 | 1141 | run_dump_test "note-5" |
ad2fb2cd | 1142 | } |
af3c5dea | 1143 | |
af3c5dea | 1144 | run_dump_test "copy-2" |
81fc501a | 1145 | run_dump_test "copy-3" |
0691f7af | 1146 | run_dump_test "copy-4" |
de564eb5 | 1147 | run_dump_test "copy-5" |
e3ecd631 | 1148 | run_dump_test "copy-6" |
de564eb5 | 1149 | |
506b86a4 AM |
1150 | # Use bintest.o from the copy-4 test to determine ELF reloc type |
1151 | set reloc_format rel | |
1152 | if { [is_elf_format] && [is_rela tmpdir/bintest.o] } { | |
1153 | set reloc_format rela | |
1154 | } | |
cbd44e24 L |
1155 | run_dump_test "pr19020a" |
1156 | run_dump_test "pr19020b" | |
d58c2e3a RS |
1157 | |
1158 | if [is_elf_format] { | |
748fc5e9 L |
1159 | run_dump_test "strip-1" |
1160 | run_dump_test "strip-2" | |
1aa9ef63 | 1161 | run_dump_test "strip-3" |
eb3980ce L |
1162 | run_dump_test "strip-4" |
1163 | run_dump_test "strip-5" | |
f206e905 L |
1164 | run_dump_test "strip-6" |
1165 | run_dump_test "strip-7" | |
e1e87d1e L |
1166 | run_dump_test "strip-8" |
1167 | run_dump_test "strip-9" | |
d4ac1f87 | 1168 | run_dump_test "strip-12" |
2f8ceb38 MR |
1169 | |
1170 | if { [istarget "mips64*-*-openbsd*"] } { | |
1171 | set reloc_format mips64 | |
2f8ceb38 | 1172 | } |
506b86a4 | 1173 | # A relocation type not supported by any target |
fbaf61ad NC |
1174 | if { [istarget "nds32*-*"] } { |
1175 | set reloc 255 | |
1176 | } else { | |
1177 | set reloc 215 | |
1178 | } | |
506b86a4 AM |
1179 | run_dump_test "strip-13" [list \ |
1180 | [list source strip-13${reloc_format}.s] \ | |
1181 | [list as "${elf64} --defsym RELOC=${reloc}"]] | |
3f97ba9f MR |
1182 | # Select a relocation number that corresponds to one actually |
1183 | # supported by the target and ABI being tested. | |
1184 | if { [istarget "aarch64*-*"] } { | |
1185 | set reloc 259 | |
1186 | } elseif { [istarget "ia64*-*"] \ | |
1187 | || [istarget "m32r*-*"] \ | |
d4ae1932 | 1188 | || [istarget "nds32*-*"] \ |
3f97ba9f MR |
1189 | || [istarget "v850*-*"] } { |
1190 | set reloc 50 | |
d4ae1932 AM |
1191 | } elseif { [istarget "pru-*"] } { |
1192 | set reloc 11 | |
3f97ba9f MR |
1193 | } else { |
1194 | set reloc 1 | |
1195 | } | |
1196 | run_dump_test "strip-14" [list \ | |
506b86a4 AM |
1197 | [list source strip-14${reloc_format}.s] \ |
1198 | [list as "${elf64} --defsym RELOC=${reloc}"]] | |
d52e3d06 | 1199 | run_dump_test "strip-15" [list \ |
506b86a4 AM |
1200 | [list source strip-15${reloc_format}.s] \ |
1201 | [list as "${elf64} --defsym RELOC=${reloc}"]] | |
f3185997 | 1202 | |
a43942db MR |
1203 | # This requires STB_GNU_UNIQUE support with OSABI set to GNU. |
1204 | if { [supports_gnu_unique] } { | |
4cd28456 AM |
1205 | run_dump_test "strip-10" |
1206 | } | |
211dc24b | 1207 | run_dump_test "strip-11" |
312aaa3c | 1208 | |
b8b6abe0 AM |
1209 | if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } { |
1210 | # Check to make sure we don't strip a symbol named in relocations. | |
1211 | set test "objcopy keeps symbols needed by relocs" | |
312aaa3c | 1212 | |
b8b6abe0 | 1213 | set srcfile $srcdir/$subdir/needed-by-reloc.s |
312aaa3c | 1214 | |
b8b6abe0 AM |
1215 | if {![binutils_assemble $srcfile tmpdir/bintest.o]} then { |
1216 | unresolved $test | |
1217 | } else { | |
1218 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"] | |
312aaa3c | 1219 | |
b8b6abe0 AM |
1220 | if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] { |
1221 | pass $test | |
1222 | } else { | |
1223 | fail $test | |
1224 | } | |
1225 | } | |
312aaa3c | 1226 | } |
b8b6abe0 | 1227 | |
8560e02a HPN |
1228 | # The symbol table for some MIPS targets is sorted differently than |
1229 | # the ELF canonical order, so the regexps in localize-hidden-1.d fail | |
9cc0123f AM |
1230 | # to match. |
1231 | if { [is_bad_symtab] } then { | |
1232 | setup_xfail "*-*-*" | |
8560e02a | 1233 | } |
b8b6abe0 | 1234 | run_dump_test "localize-hidden-1" |
afeb3d7f L |
1235 | run_dump_test "testranges" |
1236 | run_dump_test "testranges-ia64" | |
500ee42e ILT |
1237 | |
1238 | run_dump_test "add-section" | |
2b35fb28 | 1239 | run_dump_test "add-symbol" |
500ee42e | 1240 | run_dump_test "add-empty-section" |
18ae9cc1 L |
1241 | |
1242 | run_dump_test "exclude-1a" | |
1243 | run_dump_test "exclude-1b" | |
e511c9b1 AB |
1244 | |
1245 | run_dump_test "only-section-01" | |
1246 | run_dump_test "remove-section-01" | |
d3e5f6c8 | 1247 | |
64f52b3e FS |
1248 | run_dump_test "keep-section-1" |
1249 | run_dump_test "keep-section-2" | |
1250 | ||
d3e5f6c8 AB |
1251 | # Test the remove relocation functionality |
1252 | set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]] | |
1253 | foreach t $test_list { | |
1254 | # We need to strip the ".d", but can leave the dirname. | |
1255 | verbose [file rootname $t] | |
1256 | run_dump_test [file rootname $t] | |
1257 | } | |
312aaa3c | 1258 | } |
b8b6abe0 | 1259 | run_dump_test "localize-hidden-2" |
14f2c699 L |
1260 | |
1261 | # Test objcopying an object file without global symbol | |
1262 | ||
1263 | proc objcopy_test_without_global_symbol { } { | |
1264 | global OBJCOPY | |
1265 | global OBJCOPYFLAGS | |
1266 | global OBJDUMP | |
1267 | global OBJDUMPFLAGS | |
1268 | global srcdir | |
1269 | global subdir | |
1270 | ||
1271 | set test "strip without global symbol " | |
1272 | ||
1273 | if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } { | |
1274 | untested $test | |
1275 | return | |
1276 | } | |
1277 | ||
1278 | if [is_remote host] { | |
1279 | set objfile [remote_download host tmpdir/pr19547.o] | |
1280 | } else { | |
1281 | set objfile tmpdir/pr19547.o | |
1282 | } | |
1283 | ||
1284 | set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"] | |
7dd36a6f | 1285 | set exec_output [prune_warnings $exec_output] |
14f2c699 L |
1286 | if ![string equal "" $exec_output] { |
1287 | fail $test | |
1288 | return | |
1289 | } | |
1290 | ||
1291 | set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"] | |
7dd36a6f | 1292 | set exec_output [prune_warnings $exec_output] |
14f2c699 L |
1293 | if {![regexp "no symbols" $exec_output]} { |
1294 | fail $test | |
1295 | return | |
1296 | } | |
1297 | ||
1298 | pass $test | |
1299 | } | |
1300 | ||
41699fa4 NC |
1301 | # The AArch64 and ARM targets preserve mapping symbols |
1302 | # in object files, so they will fail this test. | |
1303 | setup_xfail aarch64*-*-* arm*-*-* | |
1304 | ||
14f2c699 | 1305 | objcopy_test_without_global_symbol |
f9853190 AM |
1306 | |
1307 | # objcopy remove relocation from executable test | |
1308 | ||
1309 | proc objcopy_remove_relocations_from_executable { } { | |
1310 | global OBJCOPY | |
1311 | global srcdir | |
1312 | global subdir | |
1313 | global READELF | |
1314 | ||
1315 | set test "remove-section relocation sections" | |
1316 | ||
1317 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/pr23611 executable debug] != "" } { | |
1318 | untested $test | |
1319 | return | |
1320 | } | |
1321 | ||
1322 | if [is_remote host] { | |
1323 | set objfile [remote_download host tmpdir/pr23611] | |
1324 | } else { | |
1325 | set objfile tmpdir/pr23611 | |
1326 | } | |
1327 | set out tmpdir/pr23611.out | |
1328 | ||
1329 | set exec_output1 [binutils_run $OBJCOPY "-R .rela.plt -R .rela.dyn -R .rel.plt -R .rel.dyn $objfile $out"] | |
1330 | set exec_output2 [binutils_run $READELF "-S $out"] | |
1331 | if { [string match "*.rel.plt*" $exec_output2] || [string match "*.rela.plt*" $exec_output2] || [string match "*.rel.dyn*" $exec_output2] || [string match "*.rela.dyn*" $exec_output2] } { | |
1332 | fail $test | |
1333 | return | |
1334 | } | |
1335 | pass $test | |
1336 | } | |
1337 | ||
1338 | objcopy_remove_relocations_from_executable | |
319dbdfb L |
1339 | |
1340 | run_dump_test "pr23633" | |
de4859ea NC |
1341 | |
1342 | run_dump_test "set-section-alignment" | |
1fafefd5 JL |
1343 | |
1344 | objcopy_test "pr25662" pr25662.s executable "" "-T$srcdir/$subdir/pr25662.ld" |