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