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