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