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