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