Add a testcase for PR binutils/14481
[external/binutils.git] / binutils / testsuite / binutils-all / ar.exp
1 #   Copyright 1995, 1997, 2002, 2004, 2007, 2008, 2009, 2010
2 #   Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
17
18 # Please email any bugs, comments, and/or additions to this file to:
19 # bug-dejagnu@prep.ai.mit.edu
20
21 # Written by Ian Lance Taylor <ian@cygnus.com>
22
23 if ![is_remote host] {
24     if {[which $AR] == 0} then {
25         perror "$AR does not exist"
26         return
27     }
28 }
29
30 set bfdtest1 [findfile $base_dir/bfdtest1]
31
32 # send_user "Version [binutil_version $AR]"
33
34 # Test long file name support
35
36 proc long_filenames { } {
37     global AR
38     global host_triplet
39     global bfdtest1
40
41     set testname "ar long file names"
42
43     set n1 "abcdefghijklmnopqrstuvwxyz1"
44     set n2 "abcdefghijklmnopqrstuvwxyz2"
45     set file1 tmpdir/$n1
46     set file2 tmpdir/$n2
47
48     remote_file build delete $file1
49     remote_file host delete $n1
50
51     # Some file systems truncate file names at 14 characters, which
52     # makes it impossible to run this test.  Check for that now.
53     set status [catch "set f [open tmpdir/$n1 w]" errs]
54     if { $status != 0 } {
55         verbose -log "open tmpdir/$n1 returned $errs"
56         unsupported $testname
57         return
58     }
59     puts $f "first"
60     close $f
61
62     remote_file build delete $file2
63     remote_file host delete $n2
64
65     set status [catch "set f [open tmpdir/$n2 w]" errs]
66     if { $status != 0 } {
67         verbose -log "open tmpdir/$n2 returned $errs"
68         unsupported $testname
69         return
70     }
71     puts $f "second"
72     close $f
73
74     if [is_remote host] {
75         set file1 [remote_download host $file1]
76         set file2 [remote_download host $file2]
77         set dest artest.a
78     } else {
79         set dest tmpdir/artest.a
80     }
81
82     remote_file host delete $dest
83
84     set got [binutils_run $AR "rc $dest $file1 $file2"]
85     if [is_remote host] {
86         remote_upload host $file1 tmpdir/$n1
87     }
88
89     set f [open tmpdir/$n1 r]
90     gets $f string
91     close $f
92     if ![string match "first" $string] {
93         verbose -log "reading tmpdir/$n1 returned $string"
94         unsupported $testname
95         return
96     }
97
98     remote_file host delete $dest
99     set got [binutils_run $AR "rc $dest $file1 $file2"]
100
101     if ![string match "" $got] {
102         fail $testname
103         return
104     }
105
106     remote_file build delete tmpdir/$n1
107     remote_file build delete tmpdir/$n2
108
109     set got [binutils_run $AR "t $dest"]
110     regsub "\[\r\n \t\]*$" "$got" "" got
111     if ![string match "$n1*$n2" $got] {
112         fail $testname
113         return
114     }
115     
116     if [is_remote host] {
117         remote_file host delete $file1
118         remote_file host delete $file2
119     }
120
121     verbose -log "$AR x $dest"
122     set exec_output [binutils_run $AR "x $dest"]
123     set exec_output [prune_warnings $exec_output]
124     if ![string match "" $exec_output] {
125         verbose -log $exec_output
126         fail $testname
127         return
128     }
129
130     verbose -log "$bfdtest1 $dest"
131     set exec_output [binutils_run $bfdtest1 "$dest"]
132     if ![string match "" $exec_output] {
133         verbose -log $exec_output
134         fail "$testname (bfdtest1)"
135         return
136     }
137
138     if [is_remote host] {
139         remote_upload host $n1 tmpdir/$n1
140         remote_upload host $n2 tmpdir/$n2
141         set file1 tmpdir/$n1
142         set file2 tmpdir/$n2
143     } else {
144         set file1 $n1
145         set file2 $n2
146     }
147
148     if ![file exists $file1] {
149         verbose -log "$file1 does not exist"
150         fail $testname
151         return
152     }
153     if ![file exists $file2] {
154         verbose -log "$file2 does not exist"
155         fail $testname
156         return
157     }
158
159     set f [open $file1 r]
160     if { [gets $f line] == -1 || $line != "first" } {
161         verbose -log "$file1 contents:"
162         verbose -log "$line"
163         close $f
164         fail $testname
165         return
166     }
167     close $f
168
169     set f [open $file2 r]
170     if { [gets $f line] == -1 || $line != "second" } {
171         verbose -log "$file2 contents:"
172         verbose -log "$line"
173         close $f
174         fail $testname
175         return
176     }
177     close $f
178
179     file delete $file1 $file2
180     pass $testname
181 }
182
183 # Test building the symbol table.
184
185 proc symbol_table { } {
186     global AR
187     global AS
188     global NM
189     global srcdir
190     global subdir
191
192     set testname "ar symbol table"
193
194     if ![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o] {
195         unresolved $testname
196         return
197     }
198
199     if [is_remote host] {
200         set archive artest.a
201         set objfile [remote_download host tmpdir/bintest.o]
202         remote_file host delete $archive
203     } else {
204         set archive tmpdir/artest.a
205         set objfile tmpdir/bintest.o
206     }
207
208     remote_file build delete tmpdir/artest.a
209
210     set got [binutils_run $AR "rc $archive ${objfile}"]
211     if ![string match "" $got] {
212         fail $testname
213         return
214     }
215
216     set got [binutils_run $NM "--print-armap $archive"]
217     if { ![string match "*text_symbol in bintest.o*" $got] \
218          || ![string match "*data_symbol in bintest.o*" $got] \
219          || ![string match "*common_symbol in bintest.o*" $got] \
220          || [string match "*static_text_symbol in bintest.o*" $got] \
221          || [string match "*static_data_symbol in bintest.o*" $got] \
222          || [string match "*external_symbol in bintest.o*" $got] } {
223         fail $testname
224         return
225     }
226
227     pass $testname
228 }
229
230 # Test building a thin archive.
231
232 proc thin_archive { } {
233     global AR
234     global AS
235     global NM
236     global srcdir
237     global subdir
238     global bfdtest1
239
240     set testname "ar thin archive"
241
242     if ![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o] {
243         unresolved $testname
244         return
245     }
246
247     if [is_remote host] {
248         set archive artest.a
249         set objfile [remote_download host tmpdir/bintest.o]
250         remote_file host delete $archive
251     } else {
252         set archive tmpdir/artest.a
253         set objfile tmpdir/bintest.o
254     }
255
256     remote_file build delete tmpdir/artest.a
257
258     set got [binutils_run $AR "rcT $archive ${objfile}"]
259     if ![string match "" $got] {
260         fail $testname
261         return
262     }
263
264     verbose -log "$bfdtest1 $archive"
265     set exec_output [binutils_run $bfdtest1 "$archive"]
266     if ![string match "" $exec_output] {
267         verbose -log $exec_output
268         fail "$testname (bfdtest1)"
269         return
270     }
271
272     set got [binutils_run $NM "--print-armap $archive"]
273     if { ![string match "*text_symbol in *bintest.o*" $got] \
274          || ![string match "*data_symbol in *bintest.o*" $got] \
275          || ![string match "*common_symbol in *bintest.o*" $got] \
276          || [string match "*static_text_symbol in *bintest.o*" $got] \
277          || [string match "*static_data_symbol in *bintest.o*" $got] \
278          || [string match "*external_symbol in *bintest.o*" $got] } {
279         fail $testname
280         return
281     }
282
283     pass $testname
284 }
285
286 # Test building a thin archive with a nested archive.
287
288 proc thin_archive_with_nested { } {
289     global AR
290     global AS
291     global NM
292     global srcdir
293     global subdir
294     global bfdtest1
295
296     set testname "ar thin archive with nested archive"
297
298     if ![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o] {
299         unresolved $testname
300         return
301     }
302
303     if [is_remote host] {
304         set archive artest.a
305         set archive2 artest2.a
306         set objfile [remote_download host tmpdir/bintest.o]
307         remote_file host delete $archive
308     } else {
309         set archive tmpdir/artest.a
310         set archive2 tmpdir/artest2.a
311         set objfile tmpdir/bintest.o
312     }
313
314     remote_file build delete tmpdir/artest.a
315
316     set got [binutils_run $AR "rc $archive ${objfile}"]
317     if ![string match "" $got] {
318         fail $testname
319         return
320     }
321
322     remote_file build delete tmpdir/artest2.a
323
324     set got [binutils_run $AR "rcT $archive2 ${archive}"]
325     if ![string match "" $got] {
326         fail $testname
327         return
328     }
329
330     verbose -log "$bfdtest1 $archive"
331     set exec_output [binutils_run $bfdtest1 "$archive"]
332     if ![string match "" $exec_output] {
333         verbose -log $exec_output
334         fail "$testname (bfdtest1)"
335         return
336     }
337
338     set got [binutils_run $NM "--print-armap $archive"]
339     if { ![string match "*text_symbol in *bintest.o*" $got] \
340          || ![string match "*data_symbol in *bintest.o*" $got] \
341          || ![string match "*common_symbol in *bintest.o*" $got] \
342          || [string match "*static_text_symbol in *bintest.o*" $got] \
343          || [string match "*static_data_symbol in *bintest.o*" $got] \
344          || [string match "*external_symbol in *bintest.o*" $got] } {
345         fail $testname
346         return
347     }
348
349     pass $testname
350 }
351
352 # Test POSIX-compatible argument parsing.
353
354 proc argument_parsing { } {
355     global AR
356     global AS
357     global srcdir
358     global subdir
359
360     set testname "ar argument parsing"
361
362     if ![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o] {
363         unresolved $testname
364         return
365     }
366
367     if [is_remote host] {
368         set archive artest.a
369         set objfile [remote_download host tmpdir/bintest.o]
370         remote_file host delete $archive
371     } else {
372         set archive tmpdir/artest.a
373         set objfile tmpdir/bintest.o
374     }
375
376     remote_file build delete tmpdir/artest.a
377
378     set got [binutils_run $AR "-r -c $archive ${objfile}"]
379     if ![string match "" $got] {
380         fail $testname
381         return
382     }
383
384     pass $testname
385 }
386
387 # Test building a deterministic archive.
388
389 proc deterministic_archive { } {
390     global AR
391     global AS
392     global NM
393     global srcdir
394     global subdir
395
396     set testname "ar deterministic archive"
397
398     if ![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o] {
399         unresolved $testname
400         return
401     }
402
403     if [is_remote host] {
404         set archive artest.a
405         set objfile [remote_download host tmpdir/bintest.o]
406         remote_file host delete $archive
407     } else {
408         set archive tmpdir/artest.a
409         set objfile tmpdir/bintest.o
410     }
411
412     remote_file build delete tmpdir/artest.a
413
414     set got [binutils_run $AR "rcD $archive ${objfile}"]
415     if ![string match "" $got] {
416         fail $testname
417         return
418     }
419
420     set got [binutils_run $AR "tv $archive"]
421     # This only checks the file mode and uid/gid.  We can't easily match
422     # date because it's printed with the user's timezone.
423     if ![string match "rw-r--r-- 0/0 *bintest.o*" $got] {
424         fail $testname
425         return
426     }
427
428     pass $testname
429 }
430
431 proc unique_symbol { } {
432     global AR
433     global AS
434     global NM
435     global srcdir
436     global subdir
437
438     set testname "ar unique symbol in archive"
439
440     if ![binutils_assemble $srcdir/$subdir/unique.s tmpdir/unique.o] {
441         unresolved $testname
442     }
443
444     if [is_remote host] {
445         set archive artest.a
446         set objfile [remote_download host tmpdir/unique.o]
447         remote_file host delete $archive
448     } else {
449         set archive tmpdir/artest.a
450         set objfile tmpdir/unique.o
451     }
452
453     remote_file build delete tmpdir/artest.a
454
455     set got [binutils_run $AR "-s -r -c $archive ${objfile}"]
456     if ![string match "" $got] {
457         fail $testname
458         return
459     }
460
461     set got [binutils_run $NM "--print-armap $archive"]
462     if ![string match "*foo in *unique.o*" $got] {
463         fail $testname
464         return
465     }
466
467     pass $testname
468 }
469
470 # Test deleting an element.
471
472 proc delete_an_element { } {
473     global AR
474     global AS
475     global srcdir
476     global subdir
477
478     set testname "ar deleting an element"
479
480     if ![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o] {
481         unresolved $testname
482         return
483     }
484
485     if [is_remote host] {
486         set archive artest.a
487         set objfile [remote_download host tmpdir/bintest.o]
488         remote_file host delete $archive
489     } else {
490         set archive tmpdir/artest.a
491         set objfile tmpdir/bintest.o
492     }
493
494     remote_file build delete tmpdir/artest.a
495
496     set got [binutils_run $AR "-r -c $archive ${objfile}"]
497     if ![string match "" $got] {
498         fail $testname
499         return
500     }
501
502     set got [binutils_run $AR "-d $archive ${objfile}"]
503     if ![string match "" $got] {
504         fail $testname
505         return
506     }
507
508     pass $testname
509 }
510
511 # Test moving an element.
512
513 proc move_an_element { } {
514     global AR
515     global AS
516     global srcdir
517     global subdir
518
519     set testname "ar moving an element"
520
521     if ![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o] {
522         unresolved $testname
523         return
524     }
525
526     if [is_remote host] {
527         set archive artest.a
528         set objfile [remote_download host tmpdir/bintest.o]
529         remote_file host delete $archive
530     } else {
531         set archive tmpdir/artest.a
532         set objfile tmpdir/bintest.o
533     }
534
535     remote_file build delete tmpdir/artest.a
536
537     set got [binutils_run $AR "-r -c $archive ${objfile}"]
538     if ![string match "" $got] {
539         fail $testname
540         return
541     }
542
543     set got [binutils_run $AR "-m $archive ${objfile}"]
544     if ![string match "" $got] {
545         fail $testname
546         return
547     }
548
549     pass $testname
550 }
551
552 # Run the tests.
553
554 long_filenames
555 symbol_table
556 thin_archive
557 thin_archive_with_nested
558 argument_parsing
559 deterministic_archive
560 delete_an_element
561 move_an_element
562 if { [is_elf_format]
563      && ![istarget "*-*-hpux*"]
564      && ![istarget "msp*-*-*"] } {
565     unique_symbol
566 }