Commit | Line | Data |
---|---|---|
aef6203b | 1 | # Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
312aaa3c | 2 | # 2004, 2006, 2007 |
8c2bc687 | 3 | # Free Software Foundation, Inc. |
252b5132 RH |
4 | |
5 | # This program is free software; you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
32866df7 | 7 | # the Free Software Foundation; either version 3 of the License, or |
252b5132 RH |
8 | # (at your option) any later version. |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program; if not, write to the Free Software | |
b43b5d5f | 17 | # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
252b5132 RH |
18 | |
19 | # Please email any bugs, comments, and/or additions to this file to: | |
20 | # bug-dejagnu@prep.ai.mit.edu | |
21 | ||
22 | # Written by Ian Lance Taylor <ian@cygnus.com> | |
23 | ||
24 | if ![is_remote host] { | |
25 | if {[which $OBJCOPY] == 0} then { | |
26 | perror "$OBJCOPY does not exist" | |
27 | return | |
28 | } | |
29 | } | |
30 | ||
31 | send_user "Version [binutil_version $OBJCOPY]" | |
32 | ||
252b5132 | 33 | if ![is_remote host] { |
8d263650 BE |
34 | set tempfile tmpdir/bintest.o |
35 | set copyfile tmpdir/copy | |
252b5132 RH |
36 | } else { |
37 | set tempfile [remote_download host tmpdir/bintest.o] | |
38 | set copyfile copy | |
39 | } | |
40 | ||
41 | # Test that objcopy does not modify a file when copying it. | |
42 | ||
64bb95af L |
43 | proc objcopy_test {testname srcfile} { |
44 | global OBJCOPY | |
45 | global OBJCOPYFLAGS | |
46 | global srcdir | |
47 | global subdir | |
48 | global tempfile | |
49 | global copyfile | |
252b5132 | 50 | |
64bb95af L |
51 | if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then { |
52 | perror "unresolved $testname" | |
53 | unresolved "objcopy ($testname)" | |
54 | return | |
252b5132 | 55 | } |
252b5132 | 56 | |
64bb95af L |
57 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"] |
58 | ||
59 | if ![string match "" $got] then { | |
60 | fail "objcopy ($testname)" | |
252b5132 | 61 | } else { |
64bb95af L |
62 | send_log "cmp $tempfile ${copyfile}.o\n" |
63 | verbose "cmp $tempfile ${copyfile}.o" | |
64 | if [is_remote host] { | |
65 | set src1 tmpdir/bintest.o | |
66 | set src2 tmpdir/copy.o | |
67 | remote_upload host $tempfile $src1 | |
68 | remote_upload host ${copyfile}.o $src2 | |
69 | } else { | |
70 | set src1 ${tempfile} | |
71 | set src2 ${copyfile}.o | |
72 | } | |
73 | set status [remote_exec build cmp "${src1} ${src2}"] | |
74 | set exec_output [lindex $status 1] | |
75 | set exec_output [prune_warnings $exec_output] | |
76 | ||
77 | # On some systems the result of objcopy will not be identical. | |
78 | # Usually this is just because gas isn't using bfd to write the | |
79 | # files in the first place, and may order things a little | |
80 | # differently. Those systems should use setup_xfail here. | |
81 | ||
82 | setup_xfail "h8300-*-rtems*" "h8300-*-coff" | |
83 | setup_xfail "h8500-*-rtems*" "h8500-*-coff" | |
84 | setup_xfail "hppa*-*-*" | |
85 | setup_xfail "i960-*" | |
86 | setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*" | |
87 | setup_xfail "m68*-*-sysv*" "m68*-apple-aux*" | |
88 | setup_xfail "m8*-*" | |
89 | setup_xfail "or32-*-rtems*" "or32-*-coff" | |
90 | setup_xfail "sh-*-coff*" "sh-*-rtems*" | |
91 | setup_xfail "tic4x-*-*" "tic80-*-*" "w65-*" | |
33c70472 | 92 | setup_xfail "*-*-cygwin*" |
64bb95af L |
93 | |
94 | clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*" | |
95 | clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*" | |
96 | clear_xfail "m68*-*-sysv4*" | |
97 | ||
98 | if [string match "" $exec_output] then { | |
99 | pass "objcopy ($testname)" | |
100 | } else { | |
101 | send_log "$exec_output\n" | |
102 | verbose "$exec_output" 1 | |
252b5132 | 103 | |
64bb95af L |
104 | # On OSF/1, this succeeds with gas and fails with /bin/as. |
105 | setup_xfail "alpha*-*-osf*" | |
252b5132 | 106 | |
64bb95af L |
107 | fail "objcopy ($testname)" |
108 | } | |
252b5132 RH |
109 | } |
110 | } | |
111 | ||
64bb95af L |
112 | objcopy_test "simple copy" bintest.s |
113 | ||
9e48b4c6 NC |
114 | # Test reversing bytes in a section. |
115 | ||
116 | set reversed ${tempfile}-reversed | |
117 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"] | |
118 | ||
119 | if ![string match "" $got] then { | |
120 | fail "objcopy --reverse-bytes" | |
121 | } else { | |
122 | if [is_remote host] { | |
123 | remote_upload host ${reversed} tmpdir/copy-reversed.o | |
124 | set reversed tmpdir/copy-reversed.o | |
125 | } | |
126 | ||
127 | set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"] | |
128 | set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"] | |
129 | ||
130 | set want "^ \[0-9\]+ (\[0-9\]+)" | |
131 | set found_orig [regexp -lineanchor $want $origdata -> origdata] | |
132 | set found_rev [regexp -lineanchor $want $revdata -> revdata] | |
133 | ||
134 | if {$found_orig == 0 || $found_rev == 0} then { | |
135 | fail "objcopy --reverse-bytes" | |
136 | } else { | |
137 | scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4 | |
138 | scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1 | |
139 | ||
140 | if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then { | |
141 | pass "objcopy --reverse-bytes" | |
142 | } else { | |
143 | fail "objcopy --reverse-bytes" | |
144 | } | |
145 | } | |
146 | } | |
147 | ||
252b5132 RH |
148 | # Test generating S records. |
149 | ||
150 | # We make the srec filename 8.3 compatible. Note that the header string | |
151 | # matched against depends on the name of the file. Ugh. | |
152 | ||
153 | if [is_remote host] { | |
154 | set srecfile copy.sre | |
155 | set header_string S00B0000636F70792E737265C1 | |
156 | } else { | |
157 | set srecfile ${copyfile}.srec | |
158 | set header_string S0130000746D706469722F636F70792E7372656397 | |
159 | } | |
160 | ||
161 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"] | |
162 | ||
163 | if ![string match "" $got] then { | |
164 | fail "objcopy -O srec" | |
165 | } else { | |
166 | if [is_remote host] { | |
8d263650 BE |
167 | remote_upload host ${srecfile} tmpdir/copy.srec |
168 | set srecfile tmpdir/copy.srec | |
252b5132 RH |
169 | } |
170 | set file [open ${srecfile} r] | |
171 | ||
172 | # The first S record is fixed by the file name we are using. | |
173 | gets $file line | |
174 | send_log "$line\n" | |
175 | verbose $line | |
176 | if ![regexp "$header_string.*" $line] { | |
177 | send_log "bad header\n" | |
178 | fail "objcopy -O srec" | |
179 | } else { | |
180 | while {[gets $file line] != -1 \ | |
181 | && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} { | |
182 | send_log "$line\n" | |
183 | verbose $line | |
184 | set line "**EOF**" | |
185 | } | |
186 | send_log "$line\n" | |
187 | verbose $line | |
188 | if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then { | |
189 | send_log "bad trailer\n" | |
190 | fail "objcopy -O srec" | |
191 | } else { | |
192 | if {[gets $file line] != -1} then { | |
193 | send_log "garbage at end\n" | |
194 | send_log "$line\n" | |
195 | verbose $line | |
196 | fail "objcopy -O srec" | |
197 | } else { | |
7f6a71ff | 198 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"] |
252b5132 RH |
199 | if ![regexp "file format srec" $got] then { |
200 | send_log "objdump failed\n" | |
201 | fail "objcopy -O srec" | |
202 | } else { | |
203 | pass "objcopy -O srec" | |
204 | } | |
205 | } | |
206 | } | |
207 | } | |
208 | ||
209 | close $file | |
210 | } | |
211 | ||
212 | # Test setting and adjusting the start address. We only test this | |
213 | # while generating S records, because we may not be able to set the | |
214 | # start address for other object file formats, and the S record case | |
215 | # is the only useful one anyhow. | |
216 | ||
217 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"] | |
218 | if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then { | |
219 | perror "objdump can not recognize bintest.o" | |
220 | set origstart "" | |
221 | } else { | |
627fe3fb | 222 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"] |
252b5132 RH |
223 | if ![string match "" $got] then { |
224 | fail "objcopy --set-start" | |
225 | } else { | |
226 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"] | |
227 | if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then { | |
228 | fail "objcopy --set-start" | |
229 | } else { | |
230 | if {$srecstart != 0x7654} then { | |
231 | send_log "$srecstart != 0x7654\n" | |
232 | fail "objcopy --set-start" | |
233 | } else { | |
234 | pass "objcopy --set-start" | |
235 | } | |
236 | } | |
237 | } | |
238 | ||
627fe3fb | 239 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"] |
252b5132 RH |
240 | if ![string match "" $got] then { |
241 | fail "objcopy --adjust-start" | |
242 | } else { | |
243 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"] | |
244 | if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then { | |
245 | fail "objcopy --adjust-start" | |
246 | } else { | |
247 | if {$srecstart != $origstart + 0x123} then { | |
248 | send_log "$srecstart != $origstart + 0x123\n" | |
249 | fail "objcopy --adjust-start" | |
250 | } else { | |
251 | pass "objcopy --adjust-start" | |
252 | } | |
253 | } | |
254 | } | |
255 | } | |
256 | ||
257 | # Test adjusting the overall VMA, and adjusting the VMA of a | |
258 | # particular section. We again only test this when generating S | |
259 | # records. | |
260 | ||
261 | set low "" | |
262 | set lowname "" | |
263 | ||
264 | set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"] | |
265 | ||
266 | set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)" | |
267 | ||
268 | set got $headers | |
269 | while {[regexp $headers_regexp $got all name size vma rest]} { | |
270 | set vma 0x$vma | |
271 | set size 0x$size | |
272 | if {$size != 0} { | |
273 | if {$low == "" || $vma < $low} { | |
274 | set low $vma | |
275 | set lowname $name | |
276 | } | |
277 | } | |
278 | set got $rest | |
279 | } | |
280 | ||
281 | if {$low == "" || $origstart == ""} then { | |
282 | perror "objdump can not recognize bintest.o" | |
283 | } else { | |
627fe3fb | 284 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"] |
252b5132 RH |
285 | if ![string match "" $got] then { |
286 | fail "objcopy --adjust-vma" | |
287 | } else { | |
288 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"] | |
289 | set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)" | |
290 | if ![regexp $want $got all start vma] then { | |
291 | fail "objcopy --adjust-vma" | |
292 | } else { | |
293 | set vma 0x$vma | |
294 | if {$vma != $low + 0x123} then { | |
295 | send_log "$vma != $low + 0x123\n" | |
296 | fail "objcopy --adjust-vma" | |
297 | } else { | |
298 | if {$start != $origstart + 0x123} then { | |
299 | send_log "$start != $origstart + 0x123\n" | |
300 | fail "objcopy --adjust-vma" | |
301 | } else { | |
302 | pass "objcopy --adjust-vma" | |
303 | } | |
304 | } | |
305 | } | |
306 | } | |
307 | ||
308 | set arg "" | |
309 | set got $headers | |
310 | while {[regexp $headers_regexp $got all name size vma rest]} { | |
311 | set vma 0x$vma | |
312 | if {$vma == $low} then { | |
313 | set arg "$arg --adjust-section-vma $name+4" | |
314 | } | |
315 | set got $rest | |
316 | } | |
317 | ||
627fe3fb | 318 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"] |
252b5132 RH |
319 | if ![string match "" $got] then { |
320 | fail "objcopy --adjust-section-vma +" | |
321 | } else { | |
322 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"] | |
323 | set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)" | |
324 | if ![regexp $want $got all vma] then { | |
325 | fail "objcopy --adjust-section-vma +" | |
326 | } else { | |
327 | set vma 0x$vma | |
328 | if {$vma != $low + 4} then { | |
329 | send_log "$vma != $low + 4\n" | |
330 | fail "objcopy --adjust-section-vma +" | |
331 | } else { | |
332 | pass "objcopy --adjust-section-vma +" | |
333 | } | |
334 | } | |
335 | } | |
336 | ||
337 | regsub -all "\\+4" $arg "=[expr $low + 4]" argeq | |
627fe3fb | 338 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"] |
252b5132 RH |
339 | if ![string match "" $got] then { |
340 | fail "objcopy --adjust-section-vma =" | |
341 | } else { | |
342 | set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"] | |
343 | set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)" | |
344 | if ![regexp $want $got all vma] then { | |
345 | fail "objcopy --adjust-section-vma =" | |
346 | } else { | |
347 | set vma 0x$vma | |
348 | if {$vma != $low + 4} then { | |
349 | send_log "$vma != $low + 4\n" | |
350 | fail "objcopy --adjust-section-vma =" | |
351 | } else { | |
352 | pass "objcopy --adjust-section-vma =" | |
353 | } | |
354 | } | |
355 | } | |
356 | } | |
357 | ||
358 | # Test stripping an object. | |
359 | ||
360 | proc strip_test { } { | |
1983fbac | 361 | global AR |
252b5132 RH |
362 | global CC |
363 | global STRIP | |
364 | global STRIPFLAGS | |
365 | global NM | |
366 | global NMFLAGS | |
367 | global srcdir | |
368 | global subdir | |
369 | ||
370 | set test "strip" | |
371 | ||
372 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { | |
373 | untested $test | |
374 | return | |
375 | } | |
376 | ||
377 | if [is_remote host] { | |
1983fbac | 378 | set archive libstrip.a |
8d263650 | 379 | set objfile [remote_download host tmpdir/testprog.o] |
1983fbac L |
380 | remote_file host delete $archive |
381 | } else { | |
382 | set archive tmpdir/libstrip.a | |
383 | set objfile tmpdir/testprog.o | |
384 | } | |
385 | ||
386 | remote_file build delete tmpdir/libstrip.a | |
387 | ||
388 | set exec_output [binutils_run $AR "rc $archive ${objfile}"] | |
389 | if ![string match "" $exec_output] { | |
390 | fail $test | |
391 | return | |
392 | } | |
393 | ||
8648f88f L |
394 | set exec_output [binutils_run $STRIP "-g $archive"] |
395 | if ![string match "" $exec_output] { | |
396 | fail $test | |
397 | return | |
398 | } | |
399 | ||
1983fbac L |
400 | set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"] |
401 | if ![string match "" $exec_output] { | |
402 | fail $test | |
403 | return | |
404 | } | |
405 | ||
406 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { | |
407 | untested $test | |
408 | return | |
409 | } | |
410 | ||
411 | if [is_remote host] { | |
8d263650 | 412 | set objfile [remote_download host tmpdir/testprog.o] |
252b5132 RH |
413 | } else { |
414 | set objfile tmpdir/testprog.o | |
415 | } | |
416 | ||
417 | set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"] | |
418 | if ![string match "" $exec_output] { | |
419 | fail $test | |
420 | return | |
421 | } | |
422 | ||
423 | set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"] | |
68a4c073 | 424 | if ![string match "*: no symbols*" $exec_output] { |
252b5132 RH |
425 | fail $test |
426 | return | |
427 | } | |
428 | ||
429 | pass $test | |
430 | } | |
431 | ||
432 | strip_test | |
433 | ||
434 | # Test stripping an object file with saving a symbol | |
435 | ||
436 | proc strip_test_with_saving_a_symbol { } { | |
437 | global CC | |
438 | global STRIP | |
439 | global STRIPFLAGS | |
440 | global NM | |
441 | global NMFLAGS | |
442 | global srcdir | |
443 | global subdir | |
444 | ||
445 | set test "strip with saving a symbol" | |
446 | ||
447 | if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } { | |
448 | untested $test | |
449 | return | |
450 | } | |
451 | ||
452 | if [is_remote host] { | |
8d263650 | 453 | set objfile [remote_download host tmpdir/testprog.o] |
252b5132 RH |
454 | } else { |
455 | set objfile tmpdir/testprog.o | |
456 | } | |
457 | ||
458 | set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"] | |
459 | if ![string match "" $exec_output] { | |
460 | fail $test | |
461 | return | |
462 | } | |
463 | ||
464 | set exec_output [binutils_run $NM "$NMFLAGS $objfile"] | |
195fe636 | 465 | if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \ |
252b5132 RH |
466 | && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} { |
467 | fail $test | |
468 | return | |
469 | } | |
470 | ||
471 | pass $test | |
472 | } | |
473 | ||
474 | strip_test_with_saving_a_symbol | |
475 | ||
476 | # Build a final executable. | |
477 | ||
99ad8390 | 478 | if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } { |
f8a7038a NC |
479 | set test_prog "testprog.exe" |
480 | } else { | |
481 | set test_prog "testprog" | |
482 | } | |
483 | ||
252b5132 RH |
484 | proc copy_setup { } { |
485 | global srcdir | |
486 | global subdir | |
9a580d9a | 487 | global gcc_gas_flag |
f8a7038a | 488 | global test_prog |
72d4c02f | 489 | global host_triplet |
252b5132 | 490 | |
8d263650 BE |
491 | set res [build_wrapper testglue.o] |
492 | set flags { debug } | |
252b5132 | 493 | |
903b1f5b NS |
494 | if { [istarget *-*-uclinux*] } { |
495 | return 1 | |
496 | } | |
497 | ||
252b5132 | 498 | if { $res != "" } { |
8d263650 BE |
499 | lappend flags "additional_flags=[lindex $res 1]" |
500 | set add_libs "testglue.o" | |
252b5132 | 501 | } else { |
8d263650 | 502 | set add_libs "" |
252b5132 RH |
503 | } |
504 | ||
9a580d9a L |
505 | if { [istarget *-*-linux*] } { |
506 | foreach i $gcc_gas_flag { | |
507 | set flags "additional_flags=$i $flags" | |
508 | } | |
509 | } | |
f8a7038a | 510 | if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } { |
252b5132 RH |
511 | return 2 |
512 | } | |
513 | ||
8d263650 BE |
514 | set result [remote_load target tmpdir/$test_prog] |
515 | set status [lindex $result 0] | |
252b5132 RH |
516 | |
517 | if { $status != "pass" } { | |
72d4c02f KT |
518 | send_log "cannot run executable, status = ${status} on ${host_triplet}\n" |
519 | if { [istarget $host_triplet] } { | |
520 | return 3 | |
521 | } | |
522 | set status "pass" | |
252b5132 RH |
523 | } |
524 | ||
525 | return 0 | |
526 | } | |
527 | ||
528 | # Test copying an executable. | |
529 | ||
530 | proc copy_executable { prog flags test1 test2 } { | |
f8a7038a | 531 | global test_prog |
72d4c02f | 532 | global host_triplet |
252b5132 RH |
533 | |
534 | if [is_remote host] { | |
8d263650 | 535 | set testfile [remote_download host tmpdir/$test_prog] |
252b5132 RH |
536 | set testcopy copyprog |
537 | } else { | |
f8a7038a | 538 | set testfile tmpdir/$test_prog |
252b5132 RH |
539 | set testcopy tmpdir/copyprog |
540 | } | |
8d263650 | 541 | remote_file host delete $testcopy |
252b5132 RH |
542 | |
543 | set exec_output [binutils_run $prog "$flags $testfile $testcopy"] | |
544 | ||
545 | if ![string match "" $exec_output] { | |
546 | fail $test1 | |
0d063f63 AM |
547 | if [string match "" $test2] { |
548 | return | |
549 | } | |
252b5132 RH |
550 | fail $test2 |
551 | return | |
552 | } | |
553 | ||
554 | if [is_remote host] { | |
555 | remote_upload host $testcopy tmpdir/copyprog | |
556 | } | |
557 | ||
f8a7038a | 558 | set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"] |
8d263650 | 559 | set exec_output [lindex $status 1] |
252b5132 RH |
560 | |
561 | if [string match "" $exec_output] then { | |
562 | pass $test1 | |
563 | } else { | |
564 | send_log "$exec_output\n" | |
565 | verbose "$exec_output" | |
566 | ||
567 | # This will fail for many reasons. For example, it will most | |
568 | # likely fail if a non-GNU linker is used. Therefore, we do | |
569 | # not insist that it pass. If you are using an assembler and | |
570 | # linker based on the same BFD as objcopy, it is worth | |
571 | # investigating to see why this failure occurs. If we are | |
572 | # cross compiling, we assume that a GNU linker is being used, | |
573 | # and expect it to succeed. | |
574 | if {[isnative]} then { | |
575 | setup_xfail "*-*-*" | |
576 | } | |
577 | ||
578 | # This also fails for mips*-*-elf targets. See elf32-mips.c | |
579 | # mips_elf_sym_is_global. | |
580 | setup_xfail "mips*-*-elf" | |
581 | ||
9da7f49f | 582 | setup_xfail "*arm*-*-coff" |
70bbfa26 | 583 | setup_xfail "xscale-*-coff" |
11d47adc | 584 | setup_xfail "arm*-*-pe" |
c58c5ee7 NC |
585 | setup_xfail "thumb*-*-coff" |
586 | setup_xfail "thumb*-*-pe" | |
72d4c02f | 587 | setup_xfail "x86_64-*-mingw*" |
11d47adc | 588 | |
252b5132 RH |
589 | fail $test1 |
590 | } | |
591 | ||
0d063f63 AM |
592 | if [string match "" $test2] { |
593 | return | |
594 | } | |
595 | ||
252b5132 | 596 | set output [remote_load target tmpdir/copyprog] |
8d263650 | 597 | set status [lindex $output 0] |
72d4c02f KT |
598 | if { ![istarget $host_triplet] } { |
599 | set status "pass" | |
600 | } | |
252b5132 RH |
601 | if { $status != "pass" } { |
602 | fail $test2 | |
603 | } else { | |
604 | pass $test2 | |
605 | } | |
606 | } | |
607 | ||
608 | # Test stripping an executable | |
609 | ||
610 | proc strip_executable { prog flags test } { | |
611 | global NM | |
612 | global NMFLAGS | |
72d4c02f | 613 | global host_triplet |
252b5132 | 614 | |
0fcc17f8 | 615 | remote_file build delete tmpdir/striprog |
252b5132 RH |
616 | remote_download build tmpdir/copyprog tmpdir/striprog |
617 | if [is_remote host] { | |
8d263650 | 618 | set copyfile [remote_download host tmpdir/striprog] |
252b5132 RH |
619 | } else { |
620 | set copyfile tmpdir/striprog | |
621 | } | |
622 | ||
623 | set exec_output [binutils_run $prog "$flags ${copyfile}"] | |
624 | if ![string match "" $exec_output] { | |
625 | fail $test | |
626 | return | |
627 | } | |
628 | ||
629 | if [is_remote host] { | |
8d263650 | 630 | remote_upload host ${copyfile} tmpdir/striprog |
252b5132 RH |
631 | } |
632 | ||
633 | set result [remote_load target tmpdir/striprog] | |
8d263650 | 634 | set status [lindex $result 0] |
72d4c02f KT |
635 | if { ![istarget $host_triplet] } { |
636 | set status "pass" | |
637 | } | |
252b5132 RH |
638 | if { $status != "pass" } { |
639 | fail $test | |
640 | return | |
641 | } | |
642 | ||
643 | set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"] | |
68a4c073 | 644 | if ![string match "*: no symbols*" $exec_output] { |
252b5132 RH |
645 | fail $test |
646 | return | |
647 | } | |
648 | pass $test | |
649 | } | |
650 | ||
651 | # Test stripping an executable with saving a symbol | |
652 | ||
653 | proc strip_executable_with_saving_a_symbol { prog flags test } { | |
654 | global NM | |
655 | global NMFLAGS | |
72d4c02f | 656 | global host_triplet |
252b5132 | 657 | |
0fcc17f8 | 658 | remote_file build delete tmpdir/striprog |
252b5132 RH |
659 | remote_download build tmpdir/copyprog tmpdir/striprog |
660 | if [is_remote host] { | |
8d263650 | 661 | set copyfile [remote_download host tmpdir/striprog] |
252b5132 RH |
662 | } else { |
663 | set copyfile tmpdir/striprog | |
664 | } | |
665 | ||
666 | set exec_output [binutils_run $prog "$flags ${copyfile}"] | |
667 | if ![string match "" $exec_output] { | |
668 | fail $test | |
669 | return | |
670 | } | |
671 | ||
672 | if [is_remote host] { | |
8d263650 | 673 | remote_upload host ${copyfile} tmpdir/striprog |
252b5132 RH |
674 | } |
675 | ||
676 | set result [remote_load target tmpdir/striprog] | |
8d263650 | 677 | set status [lindex $result 0] |
72d4c02f KT |
678 | if { ![istarget $host_triplet] } { |
679 | set status "pass" | |
680 | } | |
252b5132 RH |
681 | if { $status != "pass" } { |
682 | fail $test | |
683 | return | |
684 | } | |
685 | ||
686 | set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"] | |
edc45ab9 HPN |
687 | if { [istarget mmix-knuth-mmixware] } { |
688 | # Whenever there's a symbol in the mmo format, there's the symbol | |
689 | # Main, so remove it manually from the expected output for sake of | |
690 | # this test. | |
691 | ||
692 | # Using "" not {} to get the \n and \r translated. | |
693 | regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output | |
694 | } | |
695 | ||
adacfc81 JJ |
696 | if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \ |
697 | && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} { | |
252b5132 RH |
698 | fail $test |
699 | return | |
700 | } | |
701 | pass $test | |
702 | } | |
703 | ||
11701589 JK |
704 | # Test keeping only debug symbols of an executable |
705 | ||
706 | proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } { | |
707 | global NM | |
708 | global NMFLAGS | |
709 | ||
0fcc17f8 | 710 | remote_file build delete tmpdir/striprog |
11701589 JK |
711 | remote_download build tmpdir/copyprog tmpdir/striprog |
712 | if [is_remote host] { | |
713 | set copyfile [remote_download host tmpdir/striprog] | |
714 | } else { | |
715 | set copyfile tmpdir/striprog | |
716 | } | |
717 | ||
718 | set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"] | |
719 | if ![string match "" $exec_output] { | |
720 | fail $test1 | |
721 | return | |
722 | } | |
723 | pass $test1 | |
724 | ||
725 | set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"] | |
726 | if ![string match "" $exec_output] { | |
727 | fail $test2 | |
728 | return | |
729 | } | |
730 | pass $test2 | |
731 | } | |
732 | ||
252b5132 RH |
733 | set test1 "simple objcopy of executable" |
734 | set test2 "run objcopy of executable" | |
735 | set test3 "run stripped executable" | |
736 | set test4 "run stripped executable with saving a symbol" | |
11701589 JK |
737 | set test5 "keep only debug data" |
738 | set test6 "simple objcopy of debug data" | |
252b5132 RH |
739 | |
740 | switch [copy_setup] { | |
741 | "1" { | |
742 | # do nothing | |
743 | } | |
744 | "2" { | |
745 | untested $test1 | |
746 | untested $test2 | |
747 | untested $test3 | |
748 | untested $test4 | |
11701589 JK |
749 | untested $test5 |
750 | untested $test6 | |
252b5132 RH |
751 | } |
752 | "3" { | |
0d063f63 AM |
753 | copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "" |
754 | unsupported $test2 | |
755 | unsupported $test3 | |
756 | unsupported $test4 | |
11701589 JK |
757 | unsupported $test5 |
758 | unsupported $test6 | |
252b5132 RH |
759 | } |
760 | "0" { | |
761 | copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2" | |
762 | strip_executable "$STRIP" "$STRIPFLAGS" "$test3" | |
763 | strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test4" | |
11701589 JK |
764 | keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test5" \ |
765 | "$OBJCOPY" "$OBJCOPYFLAGS" "$test6" | |
252b5132 RH |
766 | } |
767 | } | |
ad2fb2cd | 768 | |
6a0d0afd L |
769 | proc objcopy_test_readelf {testname srcfile} { |
770 | global OBJCOPY | |
771 | global OBJCOPYFLAGS | |
772 | global READELF | |
773 | global srcdir | |
774 | global subdir | |
775 | ||
776 | if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then { | |
777 | unresolved "objcopy ($testname)" | |
778 | return | |
779 | } | |
780 | ||
781 | verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o" | |
7f6a71ff JM |
782 | set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"] |
783 | if { [lindex $exec_output 0] != 0 | |
784 | || ![string match "" [lindex $exec_output 1]] } then { | |
6a0d0afd | 785 | fail "objcopy ($testname)" |
004314cc | 786 | return |
6a0d0afd L |
787 | } |
788 | ||
789 | verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out" | |
7f6a71ff JM |
790 | set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"] |
791 | if { [lindex $exec_output 0] != 0 } then { | |
792 | unresolved "objcopy ($testname)" | |
793 | return | |
794 | } | |
795 | set exec_output [prune_warnings [lindex $exec_output 1]] | |
6a0d0afd L |
796 | if ![string match "" $exec_output] then { |
797 | unresolved "objcopy ($testname)" | |
798 | return | |
799 | } | |
800 | ||
801 | verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out" | |
7f6a71ff JM |
802 | set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"] |
803 | if { [lindex $exec_output 0] != 0 } then { | |
804 | unresolved "objcopy ($testname)" | |
805 | return | |
806 | } | |
807 | set exec_output [prune_warnings [lindex $exec_output 1]] | |
6a0d0afd L |
808 | if ![string match "" $exec_output] then { |
809 | unresolved "objcopy ($testname)" | |
810 | return | |
811 | } | |
812 | ||
813 | verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out" | |
814 | catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output | |
815 | set exec_output [prune_warnings $exec_output] | |
816 | ||
817 | if [string match "" $exec_output] then { | |
818 | pass "objcopy ($testname)" | |
819 | } else { | |
820 | fail "objcopy ($testname)" | |
821 | } | |
822 | } | |
823 | ||
ad2fb2cd L |
824 | # ia64 specific tests |
825 | if { ([istarget "ia64-*-elf*"] | |
826 | || [istarget "ia64-*-linux*"]) } { | |
64bb95af L |
827 | objcopy_test "ia64 link order" link-order.s |
828 | } | |
ad2fb2cd | 829 | |
64bb95af L |
830 | # ELF specific tests |
831 | if [is_elf_format] { | |
832 | objcopy_test "ELF unknown section type" unknown.s | |
6a0d0afd | 833 | objcopy_test_readelf "ELF group" group.s |
eb3980ce | 834 | objcopy_test_readelf "ELF group" group-2.s |
f206e905 | 835 | objcopy_test_readelf "ELF group" group-3.s |
e1e87d1e | 836 | objcopy_test_readelf "ELF group" group-4.s |
85d7f0b9 | 837 | run_dump_test "copy-1" |
ad2fb2cd | 838 | } |
af3c5dea | 839 | |
af3c5dea | 840 | run_dump_test "copy-2" |
81fc501a | 841 | run_dump_test "copy-3" |
0691f7af | 842 | run_dump_test "copy-4" |
d58c2e3a RS |
843 | |
844 | if [is_elf_format] { | |
748fc5e9 L |
845 | run_dump_test "strip-1" |
846 | run_dump_test "strip-2" | |
1aa9ef63 | 847 | run_dump_test "strip-3" |
eb3980ce L |
848 | run_dump_test "strip-4" |
849 | run_dump_test "strip-5" | |
f206e905 L |
850 | run_dump_test "strip-6" |
851 | run_dump_test "strip-7" | |
e1e87d1e L |
852 | run_dump_test "strip-8" |
853 | run_dump_test "strip-9" | |
312aaa3c | 854 | |
b8b6abe0 AM |
855 | if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } { |
856 | # Check to make sure we don't strip a symbol named in relocations. | |
857 | set test "objcopy keeps symbols needed by relocs" | |
312aaa3c | 858 | |
b8b6abe0 | 859 | set srcfile $srcdir/$subdir/needed-by-reloc.s |
312aaa3c | 860 | |
b8b6abe0 AM |
861 | if {![binutils_assemble $srcfile tmpdir/bintest.o]} then { |
862 | unresolved $test | |
863 | } else { | |
864 | set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"] | |
312aaa3c | 865 | |
b8b6abe0 AM |
866 | if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] { |
867 | pass $test | |
868 | } else { | |
869 | fail $test | |
870 | } | |
871 | } | |
312aaa3c | 872 | } |
b8b6abe0 AM |
873 | |
874 | run_dump_test "localize-hidden-1" | |
312aaa3c | 875 | } |
b8b6abe0 | 876 | run_dump_test "localize-hidden-2" |