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