aboutsummaryrefslogtreecommitdiff
path: root/ld/testsuite/lib/ld-lib.exp
blob: a0e734eb6c073109b3971b81adacc01a0d3ac137 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
# Support routines for LD testsuite.
#   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
#    2004, 2005, 2006 Free Software Foundation, Inc.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.

# Extract and print the version number of ld.
#
proc default_ld_version { ld } {
    global host_triplet

    if { [which $ld] == 0 } then {
	perror "$ld does not exist"
	exit 1
    }

    catch "exec $ld --version" tmp
    set tmp [prune_warnings $tmp]
    regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
    if [info exists number] then {
	clone_output "$ld $number\n"
    }
}

# Link an object using relocation.
#
proc default_ld_relocate { ld target objects } {
    global HOSTING_EMU
    global host_triplet

    if { [which $ld] == 0 } then {
	perror "$ld does not exist"
	return 0
    }

    verbose -log "$ld $HOSTING_EMU -o $target -r $objects"

    catch "exec $ld $HOSTING_EMU -o $target -r $objects" exec_output
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {
	return 1
    } else {
	verbose -log "$exec_output"
	return 0
    }
}

# Check to see if ld is being invoked with a non-endian output format
#
proc is_endian_output_format { object_flags } {

    if {[string match "*-oformat binary*" $object_flags] ||      \
        [string match "*-oformat ieee*" $object_flags] ||        \
	[string match "*-oformat ihex*" $object_flags] ||        \
	[string match "*-oformat netbsd-core*" $object_flags] || \
	[string match "*-oformat srec*" $object_flags] ||        \
	[string match "*-oformat tekhex*" $object_flags] ||      \
	[string match "*-oformat trad-core*" $object_flags] } then {
        return 0
    } else {
	return 1
    }
}

# Look for big-endian or little-endian switches in the multlib
# options and translate these into a -EB or -EL switch.  Note
# we cannot rely upon proc process_multilib_options to do this
# for us because for some targets the compiler does not support
# -EB/-EL but it does support -mbig-endian/-mlittle-endian, and
# the site.exp file will include the switch "-mbig-endian"
# (rather than "big-endian") which is not detected by proc
# process_multilib_options.
#
proc big_or_little_endian {} {

    if [board_info [target_info name] exists multilib_flags] {
	set tmp_flags " [board_info [target_info name] multilib_flags]"

	foreach x $tmp_flags {
	    case $x in {
		{*big*endian eb EB -eb -EB -mb} {
		    set flags " -EB"
		    return $flags
		}
		{*little*endian el EL -el -EL -ml} {
		    set flags " -EL"
		    return $flags
		}
	    }
	}
    }

    set flags ""
    return $flags
}

# Link a program using ld.
#
proc default_ld_link { ld target objects } {
    global HOSTING_EMU
    global HOSTING_CRT0
    global HOSTING_LIBS
    global LIBS
    global host_triplet
    global link_output

    set objs "$HOSTING_CRT0 $objects"
    set libs "$LIBS $HOSTING_LIBS"

    if { [which $ld] == 0 } then {
	perror "$ld does not exist"
	return 0
    }

    if [is_endian_output_format $objects] then {
	set flags [big_or_little_endian]
    } else {
	set flags ""
    }
    verbose -log "$ld $HOSTING_EMU $flags -o $target $objs $libs"

    catch "exec $ld $HOSTING_EMU $flags -o $target $objs $libs" link_output
    set exec_output [prune_warnings $link_output]
    if [string match "" $link_output] then {
	return 1
    } else {
	verbose -log "$link_output"
	return 0
    }
}

# Link a program using ld, without including any libraries.
#
proc default_ld_simple_link { ld target objects } {
    global host_triplet
    global link_output
    global gcc_ld_flag

    if { [which $ld] == 0 } then {
	perror "$ld does not exist"
	return 0
    }

    if [is_endian_output_format $objects] then {
	set flags [big_or_little_endian]
    } else {
	set flags ""
    }

    # If we are compiling with gcc, we want to add gcc_ld_flag to
    # flags.  Rather than determine this in some complex way, we guess
    # based on the name of the compiler.
    set ldexe $ld
    set ldparm [string first " " $ld]
    if { $ldparm > 0 } then {
	set ldexe [string range $ld 0 $ldparm]
    }
    set ldexe [string replace $ldexe 0 [string last "/" $ldexe] ""]
    if {[string match "*gcc*" $ldexe] || [string match "*++*" $ldexe]} then {
	set flags "$gcc_ld_flag $flags"
    }

    verbose -log "$ld $flags -o $target $objects"

    catch "exec $ld $flags -o $target $objects" link_output
    set exec_output [prune_warnings $link_output]

    # We don't care if we get a warning about a non-existent start
    # symbol, since the default linker script might use ENTRY.
    regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output

    if [string match "" $exec_output] then {
	return 1
    } else {
	verbose -log "$exec_output"
	return 0
    }
}

# Compile an object using cc.
#
proc default_ld_compile { cc source object } {
    global CFLAGS
    global srcdir
    global subdir
    global host_triplet
    global gcc_gas_flag

    set cc_prog $cc
    if {[llength $cc_prog] > 1} then {
	set cc_prog [lindex $cc_prog 0]
    }
    if {[which $cc_prog] == 0} then {
	perror "$cc_prog does not exist"
	return 0
    }

    catch "exec rm -f $object" exec_output

    set flags "-I$srcdir/$subdir $CFLAGS"

    # If we are compiling with gcc, we want to add gcc_gas_flag to
    # flags.  Rather than determine this in some complex way, we guess
    # based on the name of the compiler.
    set ccexe $cc
    set ccparm [string first " " $cc]
    set ccflags ""
    if { $ccparm > 0 } then {
	set ccflags [string range $cc $ccparm end]
	set ccexe [string range $cc 0 $ccparm]
	set cc $ccexe
    }
    set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
    if {[string match "*gcc*" $ccexe] || [string match "*++*" $ccexe]} then {
	set flags "$gcc_gas_flag $flags"
    }

    if [board_info [target_info name] exists multilib_flags] {
	append flags " [board_info [target_info name] multilib_flags]"
    }

    verbose -log "$cc $flags $ccflags -c $source -o $object"

    catch "exec $cc $flags $ccflags -c $source -o $object" exec_output
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {
	if {![file exists $object]} then {
	    regexp ".*/(\[^/\]*)$" $source all dobj
	    regsub "\\.c" $dobj ".o" realobj
	    verbose "looking for $realobj"
	    if {[file exists $realobj]} then {
		verbose -log "mv $realobj $object"
		catch "exec mv $realobj $object" exec_output
		set exec_output [prune_warnings $exec_output]
		if {![string match "" $exec_output]} then {
		    verbose -log "$exec_output"
		    perror "could not move $realobj to $object"
		    return 0
		}
	    } else {
		perror "$object not found after compilation"
		return 0
	    }
	}
	return 1
    } else {
	verbose -log "$exec_output"
	perror "$source: compilation failed"
	return 0
    }
}

# Assemble a file.
#
proc default_ld_assemble { as source object } {
    global ASFLAGS
    global host_triplet

    if {[which $as] == 0} then {
	perror "$as does not exist"
	return 0
    }

    if ![info exists ASFLAGS] { set ASFLAGS "" }

    set flags [big_or_little_endian]

    verbose -log "$as $flags $ASFLAGS -o $object $source"

    catch "exec $as $flags $ASFLAGS -o $object $source" exec_output
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {
	return 1
    } else {
	verbose -log "$exec_output"
	perror "$source: assembly failed"
	return 0
    }
}

# Run nm on a file, putting the result in the array nm_output.
#
proc default_ld_nm { nm nmflags object } {
    global NMFLAGS
    global nm_output
    global host_triplet

    if {[which $nm] == 0} then {
	perror "$nm does not exist"
	return 0
    }

    if {[info exists nm_output]} {
      unset nm_output
    }

    if ![info exists NMFLAGS] { set NMFLAGS "" }

    # Ensure consistent sorting of symbols
    if {[info exists env(LC_ALL)]} {
	set old_lc_all $env(LC_ALL)
    }
    set env(LC_ALL) "C"
    verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"

    catch "exec $nm $NMFLAGS $nmflags $object >tmpdir/nm.out" exec_output
    if {[info exists old_lc_all]} {
	set env(LC_ALL) $old_lc_all
    } else {
	unset env(LC_ALL)
    }
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {
	set file [open tmpdir/nm.out r]
	while { [gets $file line] != -1 } {
	    verbose "$line" 2
	    if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
                set name [string trimleft $name "_"]
		verbose "Setting nm_output($name) to 0x$value" 2
		set nm_output($name) 0x$value
	    }
	}
	close $file
	return 1
    } else {
	verbose -log "$exec_output"
	perror "$object: nm failed"
	return 0
    }
}

# True if the object format is known to be ELF.
#
proc is_elf_format {} {
    if { ![istarget *-*-sysv4*] \
	 && ![istarget *-*-unixware*] \
	 && ![istarget *-*-elf*] \
	 && ![istarget *-*-eabi*] \
	 && ![istarget hppa*64*-*-hpux*] \
	 && ![istarget *-*-linux*] \
	 && ![istarget frv-*-uclinux*] \
	 && ![istarget *-*-irix5*] \
	 && ![istarget *-*-irix6*] \
	 && ![istarget *-*-netbsd*] \
	 && ![istarget *-*-solaris2*] } {
	return 0
    }

    if { [istarget *-*-linux*aout*] \
	 || [istarget *-*-linux*oldld*] } {
	return 0
    }

    if { ![istarget *-*-netbsdelf*] \
	 && ([istarget *-*-netbsd*aout*] \
	     || [istarget *-*-netbsdpe*] \
	     || [istarget arm*-*-netbsd*] \
	     || [istarget sparc-*-netbsd*] \
	     || [istarget i*86-*-netbsd*] \
	     || [istarget m68*-*-netbsd*] \
	     || [istarget vax-*-netbsd*] \
	     || [istarget ns32k-*-netbsd*]) } {
    	return 0
    }
    return 1
}

# True if the object format is known to be 64-bit ELF.
#
proc is_elf64 { binary_file } {
    global READELF
    global READELFFLAGS

    set readelf_size ""
    catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got

    if ![string match "" $got] then {
	return 0
    }

    if { ![regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
	   [file_contents readelf.out] nil readelf_size] } {
	return 0
    }

    if { $readelf_size == "64" } {
	return 1
    }

    return 0
}

# True if the object format is known to be a.out.
#
proc is_aout_format {} {
    if { [istarget *-*-*\[ab\]out*] \
	     || [istarget *-*-linux*oldld*] \
	     || [istarget *-*-msdos*] \
	     || [istarget arm-*-netbsd] \
	     || [istarget i?86-*-netbsd] \
	     || [istarget i?86-*-mach*] \
	     || [istarget i?86-*-vsta] \
	     || [istarget pdp11-*-*] \
	     || [istarget m68*-ericsson-ose] \
	     || [istarget m68k-hp-bsd*] \
	     || [istarget m68*-*-hpux*] \
	     || [istarget m68*-*-netbsd] \
	     || [istarget m68*-*-netbsd*4k*] \
	     || [istarget m68k-sony-*] \
	     || [istarget m68*-sun-sunos\[34\]*] \
	     || [istarget m68*-wrs-vxworks*] \
	     || [istarget ns32k-*-*] \
	     || [istarget sparc*-*-netbsd] \
	     || [istarget sparc-sun-sunos4*] \
	     || [istarget vax-dec-ultrix*] \
	     || [istarget vax-*-netbsd] } {
	return 1
    }
    return 0
}

# True if the object format is known to be PE COFF.
#
proc is_pecoff_format {} {
    if { ![istarget *-*-mingw32*] \
	 && ![istarget *-*-cygwin*] \
	 && ![istarget *-*-pe*] } {
	return 0
    }

    return 1
}

# Compares two files line-by-line.
#   Returns differences if exist.
#   Returns null if file(s) cannot be opened.
#
proc simple_diff { file_1 file_2 } {
    global target

    set eof -1
    set differences 0

    if [file exists $file_1] then {
	set file_a [open $file_1 r]
    } else {
	warning "$file_1 doesn't exist"
	return
    }

    if [file exists $file_2] then {
	set file_b [open $file_2 r]
    } else {
	fail "$file_2 doesn't exist"
	return
    }

    verbose "# Diff'ing: $file_1 $file_2\n" 2

    while { [gets $file_a line] != $eof } {
	if [regexp "^#.*$" $line] then {
	    continue
	} else {
	    lappend list_a $line
	}
    }
    close $file_a

    while { [gets $file_b line] != $eof } {
	if [regexp "^#.*$" $line] then {
	    continue
	} else {
	    lappend list_b $line
	}
    }
    close $file_b

    for { set i 0 } { $i < [llength $list_a] } { incr i } {
	set line_a [lindex $list_a $i]
	set line_b [lindex $list_b $i]

	verbose "\t$file_1: $i: $line_a\n" 3
	verbose "\t$file_2: $i: $line_b\n" 3
	if [string compare $line_a $line_b] then {
	    verbose -log "\t$file_1: $i: $line_a\n"
	    verbose -log "\t$file_2: $i: $line_b\n"

	    fail "Test: $target"
	    return
	}
    }

    if { [llength $list_a] != [llength $list_b] } {
	fail "Test: $target"
	return
    }

    if $differences<1 then {
	pass "Test: $target"
    }
}

# run_dump_test FILE
# Copied from gas testsuite, tweaked and further extended.
#
# Assemble a .s file, then run some utility on it and check the output.
#
# There should be an assembly language file named FILE.s in the test
# suite directory, and a pattern file called FILE.d.  `run_dump_test'
# will assemble FILE.s, run some tool like `objdump', `objcopy', or
# `nm' on the .o file to produce textual output, and then analyze that
# with regexps.  The FILE.d file specifies what program to run, and
# what to expect in its output.
#
# The FILE.d file begins with zero or more option lines, which specify
# flags to pass to the assembler, the program to run to dump the
# assembler's output, and the options it wants.  The option lines have
# the syntax:
#
#         # OPTION: VALUE
#
# OPTION is the name of some option, like "name" or "objdump", and
# VALUE is OPTION's value.  The valid options are described below.
# Whitespace is ignored everywhere, except within VALUE.  The option
# list ends with the first line that doesn't match the above syntax
# (hmm, not great for error detection).
#
# The interesting options are:
#
#   name: TEST-NAME
#	The name of this test, passed to DejaGNU's `pass' and `fail'
#       commands.  If omitted, this defaults to FILE, the root of the
#       .s and .d files' names.
#
#   as: FLAGS
#	When assembling, pass FLAGS to the assembler.
#       If assembling several files, you can pass different assembler
#       options in the "source" directives.  See below.
#
#   ld: FLAGS
#       Link assembled files using FLAGS, in the order of the "source"
#       directives, when using multiple files.
#
#   objcopy_linked_file: FLAGS
#	Run objcopy on the linked file with the specified flags.
#	This lets you transform the linked file using objcopy, before the
#	result is analyzed by an analyzer program specified below (which
#	may in turn *also* be objcopy).
#
#   PROG: PROGRAM-NAME
#       The name of the program to run to analyze the .o file produced
#       by the assembler or the linker output.  This can be omitted;
#       run_dump_test will guess which program to run by seeing which of
#       the flags options below is present.
#
#   objdump: FLAGS
#   nm: FLAGS
#   objcopy: FLAGS
#	Use the specified program to analyze the assembler or linker
#       output file, and pass it FLAGS, in addition to the output name.
#	Note that they are run with LC_ALL=C in the environment to give
#	consistent sorting of symbols.
#
#   source: SOURCE [FLAGS]
#	Assemble the file SOURCE.s using the flags in the "as" directive
#       and the (optional) FLAGS.  If omitted, the source defaults to
#       FILE.s.
#       This is useful if several .d files want to share a .s file.
#       More than one "source" directive can be given, which is useful
#       when testing linking.
#
#   xfail: TARGET
#       The test is expected to fail on TARGET.  This may occur more than
#       once.
#
#   target: TARGET
#       Only run the test for TARGET.  This may occur more than once; the
#       target being tested must match at least one.
#
#   notarget: TARGET
#       Do not run the test for TARGET.  This may occur more than once;
#       the target being tested must not match any of them.
#
#   error: REGEX
#	An error with message matching REGEX must be emitted for the test
#	to pass.  The PROG, objdump, nm and objcopy options have no
#	meaning and need not supplied if this is present.
#
#   warning: REGEX
#	Expect a linker warning matching REGEX.  It is an error to issue
#	both "error" and "warning".
#
# Each option may occur at most once unless otherwise mentioned.
#
# After the option lines come regexp lines.  `run_dump_test' calls
# `regexp_diff' to compare the output of the dumping tool against the
# regexps in FILE.d.  `regexp_diff' is defined later in this file; see
# further comments there.
#
proc run_dump_test { name } {
    global subdir srcdir
    global OBJDUMP NM AS OBJCOPY READELF LD
    global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
    global host_triplet runtests
    global env

    if [string match "*/*" $name] {
	set file $name
	set name [file tail $name]
    } else {
	set file "$srcdir/$subdir/$name"
    }

    if ![runtest_file_p $runtests $name] then {
	return
    }

    set opt_array [slurp_options "${file}.d"]
    if { $opt_array == -1 } {
	perror "error reading options from $file.d"
	unresolved $subdir/$name
	return
    }
    set dumpfile tmpdir/dump.out
    set run_ld 0
    set run_objcopy 0
    set opts(as) {}
    set opts(ld) {}
    set opts(xfail) {}
    set opts(target) {}
    set opts(notarget) {}
    set opts(objdump) {}
    set opts(nm) {}
    set opts(objcopy) {}
    set opts(readelf) {}
    set opts(name) {}
    set opts(PROG) {}
    set opts(source) {}
    set opts(error) {}
    set opts(warning) {}
    set opts(objcopy_linked_file) {}
    set asflags(${file}.s) {}

    foreach i $opt_array {
	set opt_name [lindex $i 0]
	set opt_val [lindex $i 1]
	if ![info exists opts($opt_name)] {
	    perror "unknown option $opt_name in file $file.d"
	    unresolved $subdir/$name
	    return
	}

	switch -- $opt_name {
	    xfail {}
	    target {}
	    notarget {}
	    source {
		# Move any source-specific as-flags to a separate array to
		# simplify processing.
		if { [llength $opt_val] > 1 } {
		    set asflags([lindex $opt_val 0]) [lrange $opt_val 1 end]
		    set opt_val [lindex $opt_val 0]
		} else {
		    set asflags($opt_val) {}
		}
	    }
	    default {
		if [string length $opts($opt_name)] {
		    perror "option $opt_name multiply set in $file.d"
		    unresolved $subdir/$name
		    return
		}

		# A single "# ld:" with no options should do the right thing.
		if { $opt_name == "ld" } {
		    set run_ld 1
		}
		# Likewise objcopy_linked_file.
		if { $opt_name == "objcopy_linked_file" } {
		    set run_objcopy 1
		}
	    }
	}
	set opts($opt_name) [concat $opts($opt_name) $opt_val]
    }

    # Decide early whether we should run the test for this target.
    if { [llength $opts(target)] > 0 } {
	set targmatch 0
	foreach targ $opts(target) {
	    if [istarget $targ] {
		set targmatch 1
		break
	    }
	}
	if { $targmatch == 0 } {
	    return
	}
    }
    foreach targ $opts(notarget) {
	if [istarget $targ] {
	    return
	}
    }

    set program ""
    # It's meaningless to require an output-testing method when we
    # expect an error.
    if { $opts(error) == "" } {
	if {$opts(PROG) != ""} {
	    switch -- $opts(PROG) {
		objdump	{ set program objdump }
		nm	{ set program nm }
		objcopy	{ set program objcopy }
		readelf	{ set program readelf }
		default
		{ perror "unrecognized program option $opts(PROG) in $file.d"
		  unresolved $subdir/$name
		  return }
	    }
	} else {
	# Guess which program to run, by seeing which option was specified.
	    foreach p {objdump objcopy nm readelf} {
		if {$opts($p) != ""} {
		    if {$program != ""} {
			perror "ambiguous dump program in $file.d"
			unresolved $subdir/$name
			return
		    } else {
			set program $p
		    }
		}
	    }
	}
	if { $program == "" && $opts(warning) == "" } {
	    perror "dump program unspecified in $file.d"
	    unresolved $subdir/$name
	    return
	}
    }

    if { $opts(name) == "" } {
	set testname "$subdir/$name"
    } else {
	set testname $opts(name)
    }

    if { $opts(source) == "" } {
	set sourcefiles [list ${file}.s]
    } else {
	set sourcefiles {}
	foreach sf $opts(source) {
	    if { [string match "/*" $sf] } {
		lappend sourcefiles "$sf"
	    } else {
		lappend sourcefiles "$srcdir/$subdir/$sf"
	    }
	    # Must have asflags indexed on source name.
	    set asflags($srcdir/$subdir/$sf) $asflags($sf)
	}
    }

    # Time to setup xfailures.
    foreach targ $opts(xfail) {
	setup_xfail $targ
    }

    # Assemble each file.
    set objfiles {}
    for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
	set sourcefile [lindex $sourcefiles $i]

	set objfile "tmpdir/dump$i.o"
	lappend objfiles $objfile
	set cmd "$AS $ASFLAGS $opts(as) $asflags($sourcefile) -o $objfile $sourcefile"

	send_log "$cmd\n"
	set cmdret [catch "exec $cmd" comp_output]
	set comp_output [prune_warnings $comp_output]

	if { $cmdret != 0 || ![string match "" $comp_output] } then {
	    send_log "$comp_output\n"
	    verbose "$comp_output" 3

	    set exitstat "succeeded"
	    if { $cmdret != 0 } { set exitstat "failed" }
	    verbose -log "$exitstat with: <$comp_output>"
	    fail $testname
	    return
	}
    }

    set expmsg $opts(error)
    if { $opts(warning) != "" } {
	if { $expmsg != "" } {
	    perror "$testname: mixing error and warning test-directives"
	    return
	}
	set expmsg $opts(warning)
    }

    # Perhaps link the file(s).
    if { $run_ld } {
	set objfile "tmpdir/dump"

	# Add -L$srcdir/$subdir so that the linker command can use
	# linker scripts in the source directory.
	set cmd "$LD $LDFLAGS -L$srcdir/$subdir \
		   $opts(ld) -o $objfile $objfiles"

	send_log "$cmd\n"
	set cmdret [catch "exec $cmd" comp_output]
	set comp_output [prune_warnings $comp_output]

	if { $cmdret != 0 } then {
	    # If the executed program writes to stderr and stderr is not
	    # redirected, exec *always* returns failure, regardless of the
	    # program exit code.  Thankfully, we can retrieve the true
	    # return status from a special variable.  Redirection would
	    # cause a Tcl-specific message to be appended, and we'd rather
	    # not deal with that if we can help it.
	    global errorCode
	    if { [lindex $errorCode 0] == "NONE" } {
		set cmdret 0
	    }
	}

	if { $cmdret == 0 && $run_objcopy } {
	    set infile $objfile
	    set objfile "tmpdir/dump1"

	    # Note that we don't use OBJCOPYFLAGS here; any flags must be
	    # explicitly specified.
	    set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"

	    send_log "$cmd\n"
	    set cmdret [catch "exec $cmd" comp_output]
	    append comp_output [prune_warnings $comp_output]

	    if { $cmdret != 0 } then {
		global errorCode
		if { [lindex $errorCode 0] == "NONE" } {
		    set cmdret 0
		}
	    }
	}

	if { $cmdret != 0 || $comp_output != "" || $expmsg != "" } then {
	    set exitstat "succeeded"
	    if { $cmdret != 0 } { set exitstat "failed" }
	    verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
	    send_log "$comp_output\n"
	    verbose "$comp_output" 3

	    if { [regexp $expmsg $comp_output] \
		    && (($cmdret == 0) == ($opts(warning) != "")) } {
		# We have the expected output from ld.
		if { $opts(error) != "" || $program == "" } {
		    pass $testname
		    return
		}
	    } else {
		verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
		fail $testname
		return
	    }
	}
    } else {
	set objfile "tmpdir/dump0.o"
    }

    # We must not have expected failure if we get here.
    if { $opts(error) != "" } {
	fail $testname
	return
    }

    set progopts1 $opts($program)
    eval set progopts \$[string toupper $program]FLAGS
    eval set binary \$[string toupper $program]

    if { [which $binary] == 0 } {
	untested $testname
	return
    }

    if { $progopts1 == "" } { set $progopts1 "-r" }
    verbose "running $binary $progopts $progopts1" 3

    # Objcopy, unlike the other two, won't send its output to stdout,
    # so we have to run it specially.
    set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
    if { $program == "objcopy" } {
	set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
    }

    # Ensure consistent sorting of symbols
    if {[info exists env(LC_ALL)]} {
	set old_lc_all $env(LC_ALL)
    }
    set env(LC_ALL) "C"
    send_log "$cmd\n"
    catch "exec $cmd" comp_output
    if {[info exists old_lc_all]} {
	set env(LC_ALL) $old_lc_all
    } else {
	unset env(LC_ALL)
    }
    set comp_output [prune_warnings $comp_output]
    if ![string match "" $comp_output] then {
	send_log "$comp_output\n"
	fail $testname
	return
    }

    verbose_eval {[file_contents $dumpfile]} 3
    if { [regexp_diff $dumpfile "${file}.d"] } then {
	fail $testname
	verbose "output is [file_contents $dumpfile]" 2
	return
    }

    pass $testname
}

proc slurp_options { file } {
    if [catch { set f [open $file r] } x] {
	#perror "couldn't open `$file': $x"
	perror "$x"
	return -1
    }
    set opt_array {}
    # whitespace expression
    set ws  {[ 	]*}
    set nws {[^ 	]*}
    # whitespace is ignored anywhere except within the options list;
    # option names are alphabetic plus underscore only.
    set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
    while { [gets $f line] != -1 } {
	set line [string trim $line]
	# Whitespace here is space-tab.
	if [regexp $pat $line xxx opt_name opt_val] {
	    # match!
	    lappend opt_array [list $opt_name $opt_val]
	} else {
	    break
	}
    }
    close $f
    return $opt_array
}

# regexp_diff, copied from gas, based on simple_diff above.
#	compares two files line-by-line
#	file1 contains strings, file2 contains regexps and #-comments
#	blank lines are ignored in either file
#	returns non-zero if differences exist
#
proc regexp_diff { file_1 file_2 } {

    set eof -1
    set end_1 0
    set end_2 0
    set differences 0
    set diff_pass 0

    if [file exists $file_1] then {
	set file_a [open $file_1 r]
    } else {
	warning "$file_1 doesn't exist"
	return 1
    }

    if [file exists $file_2] then {
	set file_b [open $file_2 r]
    } else {
	fail "$file_2 doesn't exist"
	close $file_a
	return 1
    }

    verbose " Regexp-diff'ing: $file_1 $file_2" 2

    while { 1 } {
	set line_a ""
	set line_b ""
	while { [string length $line_a] == 0 } {
	    if { [gets $file_a line_a] == $eof } {
		set end_1 1
		break
	    }
	}
	while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
	    if [ string match "#pass" $line_b ] {
		set end_2 1
		set diff_pass 1
		break
	    } elseif [ string match "#..." $line_b ] {
		if { [gets $file_b line_b] == $eof } {
		    set end_2 1
		    set diff_pass 1
		    break
		}
		verbose "looking for \"^$line_b$\"" 3
		while { ![regexp "^$line_b$" "$line_a"] } {
		    verbose "skipping    \"$line_a\"" 3
		    if { [gets $file_a line_a] == $eof } {
			set end_1 1
			break
		    }
		}
		break
	    }
	    if { [gets $file_b line_b] == $eof } {
		set end_2 1
		break
	    }
	}

        if { $diff_pass } {
            break
        } elseif { $end_1 && $end_2 } {
            break
        } elseif { $end_1 } {
            send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
            verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
            set differences 1
            break
        } elseif { $end_2 } {
            send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
            verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
            set differences 1
            break
        } else {
            verbose "regexp \"^$line_b$\"\nline   \"$line_a\"" 3
            if ![regexp "^$line_b$" "$line_a"] {
		send_log "regexp_diff match failure\n"
		send_log "regexp \"^$line_b$\"\nline   \"$line_a\"\n"
		set differences 1
            }
        }
    }

    if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
	send_log "$file_1 and $file_2 are different lengths\n"
	verbose "$file_1 and $file_2 are different lengths" 3
	set differences 1
    }

    close $file_a
    close $file_b

    return $differences
}

proc file_contents { filename } {
    set file [open $filename r]
    set contents [read $file]
    close $file
    return $contents
}

# List contains test-items with 3 items followed by 2 lists, one item and
# one optional item:
# 0:name 1:ld options 2:assembler options
# 3:filenames of assembler files 4: action and options. 5: name of output file
# 6:compiler flags (optional)
#
# Actions:
# objdump: Apply objdump options on result.  Compare with regex (last arg).
# nm: Apply nm options on result.  Compare with regex (last arg).
# readelf: Apply readelf options on result.  Compare with regex (last arg).
#
proc run_ld_link_tests { ldtests } {
    global ld
    global as
    global nm
    global objdump
    global READELF
    global srcdir
    global subdir
    global env
    global CC
    global CFLAGS

    foreach testitem $ldtests {
	set testname [lindex $testitem 0]
	set ld_options [lindex $testitem 1]
	set as_options [lindex $testitem 2]
	set src_files  [lindex $testitem 3]
	set actions [lindex $testitem 4]
	set binfile tmpdir/[lindex $testitem 5]
	set cflags [lindex $testitem 6]
	set objfiles {}
	set is_unresolved 0
	set failed 0

#	verbose -log "Testname is $testname"
#	verbose -log "ld_options is $ld_options"
#	verbose -log "as_options is $as_options"
#	verbose -log "src_files is $src_files"
#	verbose -log "actions is $actions"
#	verbose -log "binfile is $binfile"

	# Assemble each file in the test.
	foreach src_file $src_files {
	    set objfile "tmpdir/[file rootname $src_file].o"
	    lappend objfiles $objfile

	    if { [file extension $src_file] == ".c" } {
		set as_file "tmpdir/[file rootname $src_file].s"
		if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
		    set is_unresolved 1
		    break
		}
	    } else {
		set as_file "$srcdir/$subdir/$src_file"
	    }
	    if ![ld_assemble $as "$as_options $as_file" $objfile] {
		set is_unresolved 1
		break
	    }
	}

	# Catch assembler errors.
	if { $is_unresolved != 0 } {
	    unresolved $testname
	    continue
	}

	if ![ld_simple_link $ld $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
	    fail $testname
	} else {
	    set failed 0
	    foreach actionlist $actions {
		set action [lindex $actionlist 0]
		set progopts [lindex $actionlist 1]

		# There are actions where we run regexp_diff on the
		# output, and there are other actions (presumably).
		# Handling of the former look the same.
		set dump_prog ""
		switch -- $action {
		    objdump
		        { set dump_prog $objdump }
		    nm
		        { set dump_prog $nm }
		    readelf
		        { set dump_prog $READELF }
		    default
			{
			    perror "Unrecognized action $action"
			    set is_unresolved 1
			    break
			}
		    }

		if { $dump_prog != "" } {
		    set dumpfile [lindex $actionlist 2]
		    set binary $dump_prog

		    # Ensure consistent sorting of symbols
		    if {[info exists env(LC_ALL)]} {
			set old_lc_all $env(LC_ALL)
		    }
		    set env(LC_ALL) "C"
		    set cmd "$binary $progopts $binfile > dump.out"
		    send_log "$cmd\n"
		    catch "exec $cmd" comp_output
		    if {[info exists old_lc_all]} {
			set env(LC_ALL) $old_lc_all
		    } else {
			unset env(LC_ALL)
		    }
		    set comp_output [prune_warnings $comp_output]

		    if ![string match "" $comp_output] then {
			send_log "$comp_output\n"
			set failed 1
			break
		    }

		    if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
			verbose "output is [file_contents "dump.out"]" 2
			set failed 1
			break
		    }
		}
	    }

	    if { $failed != 0 } {
		fail $testname
	    } else { if { $is_unresolved == 0 } {
		pass $testname
	    } }
	}

	# Catch action errors.
	if { $is_unresolved != 0 } {
	    unresolved $testname
	    continue
	}
    }
}


proc verbose_eval { expr { level 1 } } {
    global verbose
    if $verbose>$level then { eval verbose "$expr" $level }
}

# This definition is taken from an unreleased version of DejaGnu.  Once
# that version gets released, and has been out in the world for a few
# months at least, it may be safe to delete this copy.
if ![string length [info proc prune_warnings]] {
    #
    # prune_warnings -- delete various system verbosities from TEXT
    #
    # An example is:
    # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
    #
    # Sites with particular verbose os's may wish to override this in site.exp.
    #
    proc prune_warnings { text } {
	# This is from sun4's.  Do it for all machines for now.
	# The "\\1" is to try to preserve a "\n" but only if necessary.
	regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text

	# It might be tempting to get carried away and delete blank lines, etc.
	# Just delete *exactly* what we're ask to, and that's it.
	return $text
    }
}

# targets_to_xfail is a list of target triplets to be xfailed.
# ldtests contains test-items with 3 items followed by 1 lists, 2 items
# and one optional item:
#   0:name
#   1:ld options
#   2:assembler options
#   3:filenames of assembler files
#   4:name of output file
#   5:expected output
#   6:compiler flags (optional)

proc run_ld_link_exec_tests { targets_to_xfail ldtests } {
    global ld
    global as
    global srcdir
    global subdir
    global env
    global CC
    global CFLAGS
    global errcnt

    foreach testitem $ldtests {
	foreach target $targets_to_xfail {
	    setup_xfail $target
	}
	set testname [lindex $testitem 0]
	set ld_options [lindex $testitem 1]
	set as_options [lindex $testitem 2]
	set src_files  [lindex $testitem 3]
	set binfile tmpdir/[lindex $testitem 4]
	set expfile [lindex $testitem 5]
	set cflags [lindex $testitem 6]
	set objfiles {}
	set failed 0

#	verbose -log "Testname is $testname"
#	verbose -log "ld_options is $ld_options"
#	verbose -log "as_options is $as_options"
#	verbose -log "src_files is $src_files"
#	verbose -log "actions is $actions"
#	verbose -log "binfile is $binfile"

	# Assemble each file in the test.
	foreach src_file $src_files {
	    set objfile "tmpdir/[file rootname $src_file].o"
	    lappend objfiles $objfile

	    # We ignore warnings since some compilers may generate
	    # incorrect section attributes and the assembler will warn
	    # them.
	    ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile

	    # We have to use $CC to build PIE and shared library.
	    if { [ string match "-shared" $ld_options ] \
		 || [ string match "-pie" $ld_options ] } {
		set link_proc ld_simple_link
		set link_cmd $CC
	    } else {
		set link_proc ld_link
		set link_cmd $ld
	    }

	    if ![$link_proc $link_cmd $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
		set failed 1
	    } else {
		set failed 0
		send_log "Running: $binfile > $binfile.out\n"
		verbose "Running: $binfile > $binfile.out"
		catch "exec $binfile > $binfile.out" exec_output
	    
		if ![string match "" $exec_output] then {
		    send_log "$exec_output\n"
		    verbose "$exec_output" 1
		    set failed 1
		} else {
		    send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
		    verbose "diff $binfile.out $srcdir/$subdir/$expfile"
		    catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
		    set exec_output [prune_warnings $exec_output]

		    if ![string match "" $exec_output] then {
			send_log "$exec_output\n"
			verbose "$exec_output" 1
			set failed 1
		    }
		}
	    }

	    if { $failed != 0 } {
		fail $testname
	    } else {
		set errcnt 0
		pass $testname
	    }
	}
    }
}