summaryrefslogtreecommitdiff
path: root/gdb/testsuite/lib/dwarf.exp
blob: 3d833e5cd5820ccc8a5803e8aeea68c629773f2d (plain)
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
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
# Copyright 2010-2022 Free Software Foundation, Inc.

# This program 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 3 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, see <http://www.gnu.org/licenses/>.

# Return true if the target supports DWARF-2 and uses gas.
# For now pick a sampling of likely targets.
proc dwarf2_support {} {
    if {[istarget *-*-linux*]
	|| [istarget *-*-gnu*]
	|| [istarget *-*-elf*]
	|| [istarget *-*-openbsd*]
	|| [istarget arm*-*-eabi*]
	|| [istarget powerpc-*-eabi*]} {
	return 1
    }

    return 0
}

# Use 'objcopy --extract-dwo to extract DWO information from
# OBJECT_FILE and place it into DWO_FILE.
#
# Return 0 on success, otherwise, return -1.
proc extract_dwo_information { object_file dwo_file } {
    set objcopy [gdb_find_objcopy]
    set command "$objcopy --extract-dwo $object_file $dwo_file"
    verbose -log "Executing $command"
    set result [catch "exec $command" output]
    verbose -log "objcopy --extract-dwo output: $output"
    if { $result == 1 } {
	return -1
    }
    return 0
}

# Use 'objcopy --strip-dwo to remove DWO information from
# FILENAME.
#
# Return 0 on success, otherwise, return -1.
proc strip_dwo_information { filename } {
    set objcopy [gdb_find_objcopy]
    set command "$objcopy --strip-dwo $filename"
    verbose -log "Executing $command"
    set result [catch "exec $command" output]
    verbose -log "objcopy --strip-dwo output: $output"
    if { $result == 1 } {
	return -1
    }
    return 0
}

# Build an executable, with the debug information split out into a
# separate .dwo file.
#
# This function is based on build_executable_from_specs in
# lib/gdb.exp, but with threading support, and rust support removed.
#
# TESTNAME is the name of the test; this is passed to 'untested' if
# something fails.
#
# EXECUTABLE is the executable to create, this can be an absolute
# path, or a relative path, in which case the EXECUTABLE will be
# created in the standard output directory.
#
# OPTIONS is passed to the final link, using gdb_compile.  If OPTIONS
# contains any option that indicates threads is required, of if the
# option rust is included, then this function will return failure.
#
# ARGS is a series of lists.  Each list is a spec for one source file
# that will be compiled to make EXECUTABLE.  Each spec in ARGS has the
# form:
#	[ SOURCE OPTIONS ]
# or:
#       [ SOURCE OPTIONS OBJFILE ]
#
# Where SOURCE is the path to the source file to compile.  This can be
# absolute, or relative to the standard global ${subdir}/${srcdir}/
# path.
#
# OPTIONS are the options to use when compiling SOURCE into an object
# file.
#
# OBJFILE is optional, if present this is the name of the object file
# to create for SOURCE.  If this is not provided then a suitable name
# will be auto-generated.
#
# If OPTIONS contains the option 'split-dwo' then the debug
# information is extracted from the object file created by compiling
# SOURCE and placed into a file with a dwo extension.  The name of
# this file is generated based on the name of the object file that was
# created (with the .o replaced with .dwo).
proc build_executable_and_dwo_files { testname executable options args } {
    global subdir
    global srcdir

    if { ! [regexp "^/" "$executable"] } then {
	set binfile [standard_output_file $executable]
    } else {
	set binfile $executable
    }

    set func gdb_compile
    if {[lsearch -regexp $options \
	     {^(pthreads|shlib|shlib_pthreads|openmp)$}] != -1} {
	# Currently don't support compiling thread based tests here.
	# If this is required then look to build_executable_from_specs
	# for inspiration.
	return -1
    }
    if {[lsearch -exact $options rust] != -1} {
	# Currently don't support compiling rust tests here.  If this
	# is required then look to build_executable_from_specs for
	# inspiration.
	return -1
    }

    # Must be run on local host due to use of objcopy.
    if [is_remote host] {
	return -1
    }

    set objects {}
    set i 0
    foreach spec $args {
	if {[llength $spec] < 2} {
	    error "invalid spec length"
	    return -1
	}

	verbose -log "APB: SPEC: $spec"

	set s [lindex $spec 0]
	set local_options [lindex $spec 1]

	if { ! [regexp "^/" "$s"] } then {
	    set s "$srcdir/$subdir/$s"
	}

	if {[llength $spec] > 2} {
	    set objfile [lindex $spec 2]
	} else {
	    set objfile "${binfile}${i}.o"
	    incr i
	}

	if  { [$func "${s}" "${objfile}" object $local_options] != "" } {
	    untested $testname
	    return -1
	}

	lappend objects "$objfile"

	if {[lsearch -exact $local_options "split-dwo"] >= 0} {
	    # Split out the DWO file.
	    set dwo_file "[file rootname ${objfile}].dwo"

	    if { [extract_dwo_information $objfile $dwo_file] == -1 } {
		untested $testname
		return -1
	    }

	    if { [strip_dwo_information $objfile] == -1 } {
		untested $testname
		return -1
	    }
	}
    }

    verbose -log "APB: OBJECTS = $objects"

    set ret [$func $objects "${binfile}" executable $options]
    if  { $ret != "" } {
        untested $testname
        return -1
    }

    return 0
}

# Utility function for procs shared_gdb_*.

proc init_shared_gdb {} {
    global shared_gdb_enabled
    global shared_gdb_started

    if { ! [info exists shared_gdb_enabled] } {
	set shared_gdb_enabled 0
	set shared_gdb_started 0
    }
}

# Cluster of four procs:
# - shared_gdb_enable
# - shared_gdb_disable
# - shared_gdb_start_use SRC OPTIONS
# - shared_gdb_end_use
#
# Can be used like so:
#
#   {
#     if { $share } shared_gdb_enable
#     ...
#     shared_gdb_start_use $src $options
#     ...
#     shared_gdb_end_use
#     ...
#     shared_gdb_start_use $src $options
#     ...
#     shared_gdb_end_use
#     ...
#     if { $share } shared_gdb_disable
#   }
#
# to write functionalty that could share ($share == 1) or could not
# share ($share == 0) a gdb session between two uses.

proc shared_gdb_enable {} {
    set me shared_gdb_enable

    init_shared_gdb
    global shared_gdb_enabled
    global shared_gdb_started

    if { $shared_gdb_enabled } {
	error "$me: gdb sharing already enabled"
    }
    set shared_gdb_enabled 1

    if { $shared_gdb_started } {
	error "$me: gdb sharing not stopped"
    }
}

# See above.

proc shared_gdb_disable {} {
    init_shared_gdb
    global shared_gdb_enabled
    global shared_gdb_started

    if { ! $shared_gdb_enabled } {
	error "$me: gdb sharing not enabled"
    }
    set shared_gdb_enabled 0

    if { $shared_gdb_started } {
	gdb_exit
	set shared_gdb_started 0
    }
}

# Share the current gdb session.

proc share_gdb { src options } {
    global shared_gdb_started
    global shared_gdb_src
    global shared_gdb_options

    set shared_gdb_started 1
    set shared_gdb_src $src
    set shared_gdb_options $options
}

# See above.

proc shared_gdb_start_use { src options } {
    set me shared_gdb_start_use

    init_shared_gdb
    global shared_gdb_enabled
    global shared_gdb_started
    global shared_gdb_src
    global shared_gdb_options

    set do_start 1
    if { $shared_gdb_enabled && $shared_gdb_started } {
	if { $shared_gdb_src != $src
	     || $shared_gdb_options != $options } {
	    error "$me: gdb sharing inconsistent"
	}

	set do_start 0
    }

    if { $do_start } {
	set exe [standard_temp_file func_addr[pid].x]

	gdb_compile $src $exe executable $options

	gdb_exit
	gdb_start
	gdb_load "$exe"

	if { $shared_gdb_enabled } {
	    share_gdb $src $options
	}
    }
}

# See above.

proc shared_gdb_end_use {} {
    init_shared_gdb
    global shared_gdb_enabled

    if { ! $shared_gdb_enabled } {
	gdb_exit
    }
}

# Enable gdb session sharing within BODY.

proc with_shared_gdb { body } {
    shared_gdb_enable
    set code [catch { uplevel 1 $body } result]
    shared_gdb_disable

    # Return as appropriate.
    if { $code == 1 } {
	global errorInfo errorCode
	return -code error -errorinfo $errorInfo -errorcode $errorCode $result
    } elseif { $code > 1 } {
	return -code $code $result
    }

    return $result
}

# Return a list of expressions about function FUNC's address and length.
# The first expression is the address of function FUNC, and the second
# one is FUNC's length.  SRC is the source file having function FUNC.
# An internal label ${func}_label must be defined inside FUNC:
#
#  int main (void)
#  {
#    asm ("main_label: .globl main_label");
#    return 0;
#  }
#
# This label is needed to compute the start address of function FUNC.
# If the compiler is gcc, we can do the following to get function start
# and end address too:
#
# asm ("func_start: .globl func_start");
# static void func (void) {}
# asm ("func_end: .globl func_end");
#
# however, this isn't portable, because other compilers, such as clang,
# may not guarantee the order of global asms and function.  The code
# becomes:
#
# asm ("func_start: .globl func_start");
# asm ("func_end: .globl func_end");
# static void func (void) {}
#

proc function_range { func src {options {debug}} } {
    global decimal gdb_prompt

    shared_gdb_start_use $src $options

    # Compute the label offset, and we can get the function start address
    # by "${func}_label - $func_label_offset".
    set func_label_offset ""
    set test "p ${func}_label - ${func}"
    gdb_test_multiple $test $test {
	-re ".* = ($decimal)\r\n$gdb_prompt $" {
	    set func_label_offset $expect_out(1,string)
	}
    }

    # Compute the function length.
    global hex
    set func_length ""
    set test "disassemble $func"
    gdb_test_multiple $test $test {
	-re ".*$hex <\\+($decimal)>:\[^\r\n\]+\r\nEnd of assembler dump\.\r\n$gdb_prompt $" {
	    set func_length $expect_out(1,string)
	}
    }

    # Compute the size of the last instruction.
    if { $func_length == 0 } then {
	set func_pattern "$func"
    } else {
	set func_pattern "$func\\+$func_length"
    }
    set test "x/2i $func+$func_length"
    gdb_test_multiple $test $test {
	-re ".*($hex) <$func_pattern>:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
	    set start $expect_out(1,string)
	    set end $expect_out(2,string)

	    set func_length [expr $func_length + $end - $start]
	}
    }

    shared_gdb_end_use

    return [list "${func}_label - $func_label_offset" $func_length]
}

# Extract the start, length, and end for function called NAME and
# create suitable variables in the callers scope.
# Return the list of created variables.
proc get_func_info { name {options {debug}} } {
    global srcdir subdir srcfile

    upvar 1 "${name}_start" func_start
    upvar 1 "${name}_len" func_len
    upvar 1 "${name}_end" func_end

    lassign [function_range ${name} \
		 [list ${srcdir}/${subdir}/$srcfile] \
		 ${options}]  \
	func_start func_len
    set func_end "$func_start + $func_len"

    return [list \
		"${name}_start" \
		"${name}_len" \
		"${name}_end"]
}

# A DWARF assembler.
#
# All the variables in this namespace are private to the
# implementation.  Also, any procedure whose name starts with "_" is
# private as well.  Do not use these.
#
# Exported functions are documented at their definition.
#
# In addition to the hand-written functions documented below, this
# module automatically generates a function for each DWARF tag.  For
# most tags, two forms are made: a full name, and one with the
# "DW_TAG_" prefix stripped.  For example, you can use either
# 'DW_TAG_compile_unit' or 'compile_unit' interchangeably.
#
# There are two exceptions to this rule: DW_TAG_variable and
# DW_TAG_namespace.  For these, the full name must always be used,
# as the short name conflicts with Tcl builtins.  (Should future
# versions of Tcl or DWARF add more conflicts, this list will grow.
# If you want to be safe you should always use the full names.)
#
# Each tag procedure is defined like:
#
# proc DW_TAG_mumble {{attrs {}} {children {}}} { ... }
#
# ATTRS is an optional list of attributes.
# It is run through 'subst' in the caller's context before processing.
#
# Each attribute in the list has one of two forms:
#   1. { NAME VALUE }
#   2. { NAME VALUE FORM }
#
# In each case, NAME is the attribute's name.
# This can either be the full name, like 'DW_AT_name', or a shortened
# name, like 'name'.  These are fully equivalent.
#
# Besides DWARF standard attributes, assembler supports 'macro' attribute
# which will be substituted by one or more standard or macro attributes.
# supported macro attributes are:
#
#  - MACRO_AT_range { FUNC }
#  It is substituted by DW_AT_low_pc and DW_AT_high_pc with the start and
#  end address of function FUNC in file $srcdir/$subdir/$srcfile.
#
#  - MACRO_AT_func { FUNC }
#  It is substituted by DW_AT_name with FUNC and MACRO_AT_range.
#
# If FORM is given, it should name a DW_FORM_ constant.
# This can either be the short form, like 'DW_FORM_addr', or a
# shortened version, like 'addr'.  If the form is given, VALUE
# is its value; see below.  In some cases, additional processing
# is done; for example, DW_FORM_strp manages the .debug_str
# section automatically.
#
# If FORM is 'SPECIAL_expr', then VALUE is treated as a location
# expression.  The effective form is then DW_FORM_block or DW_FORM_exprloc
# for DWARF version >= 4, and VALUE is passed to the (internal)
# '_location' proc to be translated.
# This proc implements a miniature DW_OP_ assembler.
#
# If FORM is not given, it is guessed:
# * If VALUE starts with the "@" character, the rest of VALUE is
#   looked up as a DWARF constant, and DW_FORM_sdata is used.  For
#   example, '@DW_LANG_c89' could be used.
# * If VALUE starts with the ":" character, then it is a label
#   reference.  The rest of VALUE is taken to be the name of a label,
#   and DW_FORM_ref4 is used.  See 'new_label' and 'define_label'.
# * If VALUE starts with the "%" character, then it is a label
#   reference too, but DW_FORM_ref_addr is used.
# * Otherwise, if the attribute name has a default form (f.i. DW_FORM_addr for
#   DW_AT_low_pc), then that one is used.
# * Otherwise, an error is reported.  Either specify a form explicitly, or
#   add a default for the the attribute name in _default_form.
#
# CHILDREN is just Tcl code that can be used to define child DIEs.  It
# is evaluated in the caller's context.
#
# Currently this code is missing nice support for CFA handling, and
# probably other things as well.

namespace eval Dwarf {
    # True if the module has been initialized.
    variable _initialized 0

    # Constants from dwarf2.h.
    variable _constants
    # DW_AT short names.
    variable _AT
    # DW_FORM short names.
    variable _FORM
    # DW_OP short names.
    variable _OP

    # The current output file.
    variable _output_file

    # Note: The _cu_ values here also apply to type units (TUs).
    # Think of a TU as a special kind of CU.

    # Current CU count.
    variable _cu_count

    # The current CU's base label.
    variable _cu_label

    # The current CU's version.
    variable _cu_version

    # The current CU's address size.
    variable _cu_addr_size
    # The current CU's offset size.
    variable _cu_offset_size

    # Label generation number.
    variable _label_num

    # The deferred output array.  The index is the section name; the
    # contents hold the data for that section.
    variable _deferred_output

    # If empty, we should write directly to the output file.
    # Otherwise, this is the name of a section to write to.
    variable _defer

    # The abbrev section.  Typically .debug_abbrev but can be .debug_abbrev.dwo
    # for Fission.
    variable _abbrev_section

    # The next available abbrev number in the current CU's abbrev
    # table.
    variable _abbrev_num

    # The string table for this assembly.  The key is the string; the
    # value is the label for that string.
    variable _strings

    # Current .debug_line unit count.
    variable _line_count

    # A Label for line table header generation.
    variable _line_header_end_label

    # The address size for debug ranges section.
    variable _debug_ranges_64_bit

    # The index into the .debug_addr section (used for fission
    # generation).
    variable _debug_addr_index

    # Flag, true if the current CU is contains fission information,
    # otherwise false.
    variable _cu_is_fission

    proc _process_one_constant {name value} {
	variable _constants
	variable _AT
	variable _FORM
	variable _OP

	set _constants($name) $value

	if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
		  ignore prefix name2]} {
	    error "non-matching name: $name"
	}

	if {$name2 == "lo_user" || $name2 == "hi_user"} {
	    return
	}

	# We only try to shorten some very common things.
	# FIXME: CFA?
	switch -exact -- $prefix {
	    TAG {
		# Create two procedures for the tag.  These call
		# _handle_DW_TAG with the full tag name baked in; this
		# does all the actual work.
		proc $name {{attrs {}} {children {}}} \
		    "_handle_DW_TAG $name \$attrs \$children"

		# Filter out ones that are known to clash.
		if {$name2 == "variable" || $name2 == "namespace"} {
		    set name2 "tag_$name2"
		}

		if {[info commands $name2] != {}} {
		    error "duplicate proc name: from $name"
		}

		proc $name2 {{attrs {}} {children {}}} \
		    "_handle_DW_TAG $name \$attrs \$children"
	    }

	    AT {
		set _AT($name2) $name
	    }

	    FORM {
		set _FORM($name2) $name
	    }

	    OP {
		set _OP($name2) $name
	    }

	    default {
		return
	    }
	}
    }

    proc _read_constants {} {
	global srcdir hex decimal

	# DWARF name-matching regexp.
	set dwrx "DW_\[a-zA-Z0-9_\]+"
	# Whitespace regexp.
	set ws "\[ \t\]+"

	set fd [open [file join $srcdir .. .. include dwarf2.h]]
	while {![eof $fd]} {
	    set line [gets $fd]
	    if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
		     $line ignore name value ignore2]} {
		_process_one_constant $name $value
	    }
	}
	close $fd

	set fd [open [file join $srcdir .. .. include dwarf2.def]]
	while {![eof $fd]} {
	    set line [gets $fd]
	    if {[regexp -- \
		     "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
		     $line ignore name value ignore2]} {
		_process_one_constant $name $value
	    }
	}
	close $fd
    }

    proc _quote {string} {
	# FIXME
	return "\"${string}\\0\""
    }

    proc _nz_quote {string} {
	# For now, no quoting is done.
	return "\"${string}\""
    }

    proc _handle_DW_FORM {form value} {
	switch -exact -- $form {
	    DW_FORM_string  {
		_op .ascii [_quote $value]
	    }

	    DW_FORM_flag_present {
		# We don't need to emit anything.
	    }

	    DW_FORM_data4 -
	    DW_FORM_ref4 {
		_op .4byte $value
	    }

	    DW_FORM_ref_addr {
		variable _cu_offset_size
		variable _cu_version
		variable _cu_addr_size

		if {$_cu_version == 2} {
		    set size $_cu_addr_size
		} else {
		    set size $_cu_offset_size
		}

		_op .${size}byte $value
	    }

	    DW_FORM_GNU_ref_alt -
	    DW_FORM_GNU_strp_alt -
	    DW_FORM_sec_offset {
		variable _cu_offset_size
		_op_offset $_cu_offset_size $value
	    }

	    DW_FORM_ref1 -
	    DW_FORM_flag -
	    DW_FORM_data1 {
		_op .byte $value
	    }

	    DW_FORM_sdata {
		_op .sleb128 $value
	    }

	    DW_FORM_ref_udata -
	    DW_FORM_udata -
	    DW_FORM_loclistx -
	    DW_FORM_rnglistx {
		_op .uleb128 $value
	    }

	    DW_FORM_addr {
		variable _cu_addr_size

		_op .${_cu_addr_size}byte $value
	    }

	    DW_FORM_GNU_addr_index {
		variable _debug_addr_index
		variable _cu_addr_size

		_op .uleb128 ${_debug_addr_index}
		incr _debug_addr_index

		_defer_output .debug_addr {
		    _op .${_cu_addr_size}byte $value
		}
	    }

	    DW_FORM_data2 -
	    DW_FORM_ref2 {
		_op .2byte $value
	    }

	    DW_FORM_data8 -
	    DW_FORM_ref8 -
	    DW_FORM_ref_sig8 {
		_op .8byte $value
	    }

	    DW_FORM_data16 {
		_op .8byte $value
	    }

	    DW_FORM_strp {
		variable _strings
		variable _cu_offset_size

		if {![info exists _strings($value)]} {
		    set _strings($value) [new_label strp]
		    _defer_output .debug_str {
			define_label $_strings($value)
			_op .ascii [_quote $value]
		    }
		}

		_op_offset $_cu_offset_size $_strings($value) "strp: $value"
	    }

	    SPECIAL_expr {
		variable _cu_version
		variable _cu_addr_size
		variable _cu_offset_size

		set l1 [new_label "expr_start"]
		set l2 [new_label "expr_end"]
		_op .uleb128 "$l2 - $l1" "expression"
		define_label $l1
		_location $value $_cu_version $_cu_addr_size $_cu_offset_size
		define_label $l2
	    }

	    DW_FORM_block1 {
		set len [string length $value]
		if {$len > 255} {
		    error "DW_FORM_block1 length too long"
		}
		_op .byte $len
		_op .ascii [_nz_quote $value]
	    }

	    DW_FORM_block2 -
	    DW_FORM_block4 -

	    DW_FORM_block -

	    DW_FORM_ref2 -
	    DW_FORM_indirect -
	    DW_FORM_exprloc -

	    DW_FORM_strx -
	    DW_FORM_strx1 -
	    DW_FORM_strx2 -
	    DW_FORM_strx3 -
	    DW_FORM_strx4 -

	    DW_FORM_GNU_str_index -

	    default {
		error "unhandled form $form"
	    }
	}
    }

    proc _guess_form {value varname} {
	upvar $varname new_value

	switch -exact -- [string range $value 0 0] {
	    @ {
		# Constant reference.
		variable _constants

		set new_value $_constants([string range $value 1 end])
		# Just the simplest.
		return DW_FORM_sdata
	    }

	    : {
		# Label reference.
		variable _cu_label

		set new_value "[string range $value 1 end] - $_cu_label"

		return DW_FORM_ref4
	    }

	    % {
		# Label reference, an offset from .debug_info.
		set new_value "[string range $value 1 end]"

		return DW_FORM_ref_addr
	    }

	    default {
		return ""
	    }
	}
    }

    proc _default_form { attr } {
	switch -exact -- $attr {
	    DW_AT_low_pc  {
		return DW_FORM_addr
	    }
	    DW_AT_producer -
	    DW_AT_comp_dir -
	    DW_AT_linkage_name -
	    DW_AT_MIPS_linkage_name -
	    DW_AT_name {
		return DW_FORM_string
	    }
	    DW_AT_GNU_addr_base {
		return DW_FORM_sec_offset
	    }
	}
	return ""
    }

    # Map NAME to its canonical form.
    proc _map_name {name ary} {
	variable $ary

	if {[info exists ${ary}($name)]} {
	    set name [set ${ary}($name)]
	}

	return $name
    }

    proc _handle_attribute { attr_name attr_value attr_form } {
	variable _abbrev_section
	variable _constants
	variable _cu_version

	_handle_DW_FORM $attr_form $attr_value

	_defer_output $_abbrev_section {
	    if { $attr_form eq "SPECIAL_expr" } {
		if { $_cu_version < 4 } {
		    set attr_form_comment "DW_FORM_block"
		} else {
		    set attr_form_comment "DW_FORM_exprloc"
		}
	    } else {
		set attr_form_comment $attr_form
	    }
	    _op .uleb128 $_constants($attr_name) $attr_name
	    _op .uleb128 $_constants($attr_form) $attr_form_comment
	}
    }

    # Handle macro attribute MACRO_AT_range.

    proc _handle_macro_at_range { attr_value } {
	variable _cu_is_fission

	if {[llength $attr_value] != 1} {
	    error "usage: MACRO_AT_range { func }"
	}

	set func [lindex $attr_value 0]
	global srcdir subdir srcfile
	set src ${srcdir}/${subdir}/${srcfile}
	set result [function_range $func $src]

	set form DW_FORM_addr
	if { $_cu_is_fission } {
	    set form DW_FORM_GNU_addr_index
	}

	_handle_attribute DW_AT_low_pc [lindex $result 0] $form
	_handle_attribute DW_AT_high_pc \
	    "[lindex $result 0] + [lindex $result 1]" $form
    }

    # Handle macro attribute MACRO_AT_func.

    proc _handle_macro_at_func { attr_value } {
	if {[llength $attr_value] != 1} {
	    error "usage: MACRO_AT_func { func file }"
	}
	_handle_attribute DW_AT_name [lindex $attr_value 0] DW_FORM_string
	_handle_macro_at_range $attr_value
    }

    proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
	variable _abbrev_section
	variable _abbrev_num
	variable _constants

	set has_children [expr {[string length $children] > 0}]
	set my_abbrev [incr _abbrev_num]

	# We somewhat wastefully emit a new abbrev entry for each tag.
	# There's no reason for this other than laziness.
	_defer_output $_abbrev_section {
	    _op .uleb128 $my_abbrev "Abbrev start"
	    _op .uleb128 $_constants($tag_name) $tag_name
	    _op .byte $has_children "has_children"
	}

	_op .uleb128 $my_abbrev "Abbrev ($tag_name)"

	foreach attr $attrs {
	    set attr_name [_map_name [lindex $attr 0] _AT]

	    # When the length of ATTR is greater than 2, the last
	    # element of the list must be a form.  The second through
	    # the penultimate elements are joined together and
	    # evaluated using subst.  This allows constructs such as
	    # [gdb_target_symbol foo] to be used.

	    if {[llength $attr] > 2} {
	        set attr_value [uplevel 2 [list subst [join [lrange $attr 1 end-1]]]]
	    } else {
	        set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
	    }

	    if { [string equal "MACRO_AT_func" $attr_name] } {
		_handle_macro_at_func $attr_value
	    } elseif { [string equal "MACRO_AT_range" $attr_name] } {
		_handle_macro_at_range $attr_value
	    } else {
		if {[llength $attr] > 2} {
		    set attr_form [uplevel 2 [list subst [lindex $attr end]]]

		    if { [string index $attr_value 0] == ":" } {
			# It is a label, get its value.
			_guess_form $attr_value attr_value
		    }
		} else {
		    set attr_form [_guess_form $attr_value attr_value]
		    if { $attr_form eq "" } {
			set attr_form [_default_form $attr_name]
		    }
		    if { $attr_form eq "" } {
			error "No form for $attr_name $attr_value"
		    }
		}
		set attr_form [_map_name $attr_form _FORM]

		_handle_attribute $attr_name $attr_value $attr_form
	    }
	}

	_defer_output $_abbrev_section {
	    # Terminator.
	    _op .byte 0x0 "DW_AT - Terminator"
	    _op .byte 0x0 "DW_FORM - Terminator"
	}

	if {$has_children} {
	    uplevel 2 $children

	    # Terminate children.
	    _op .byte 0x0 "Terminate children"
	}
    }

    proc _emit {string} {
	variable _output_file
	variable _defer
	variable _deferred_output

	if {$_defer == ""} {
	    puts $_output_file $string
	} else {
	    append _deferred_output($_defer) ${string}\n
	}
    }

    proc _section {name {flags ""} {type ""}} {
	if {$flags == "" && $type == ""} {
	    _emit "        .section $name"
	} elseif {$type == ""} {
	    _emit "        .section $name, \"$flags\""
	} else {
	    _emit "        .section $name, \"$flags\", %$type"
	}
    }

    # SECTION_SPEC is a list of arguments to _section.
    proc _defer_output {section_spec body} {
	variable _defer
	variable _deferred_output

	set old_defer $_defer
	set _defer [lindex $section_spec 0]

	if {![info exists _deferred_output($_defer)]} {
	    set _deferred_output($_defer) ""
	    eval _section $section_spec
	}

	uplevel $body

	set _defer $old_defer
    }

    proc _defer_to_string {body} {
	variable _defer
	variable _deferred_output

	set old_defer $_defer
	set _defer temp

	set _deferred_output($_defer) ""

	uplevel $body

	set result $_deferred_output($_defer)
	unset _deferred_output($_defer)

	set _defer $old_defer
	return $result
    }

    proc _write_deferred_output {} {
	variable _output_file
	variable _deferred_output

	foreach section [array names _deferred_output] {
	    # The data already has a newline.
	    puts -nonewline $_output_file $_deferred_output($section)
	}

	# Save some memory.
	unset _deferred_output
    }

    proc _op {name value {comment ""}} {
	set text "        ${name}        ${value}"
	if {$comment != ""} {
	    # Try to make stuff line up nicely.
	    while {[string length $text] < 40} {
		append text " "
	    }
	    append text "/* ${comment} */"
	}
	_emit $text
    }

    proc _op_offset { size offset {comment ""} } {
	if { $size == 4 } {
	    _op .4byte $offset $comment
	} elseif { $size == 8 } {
	    if {[is_64_target]} {
		_op .8byte $offset $comment
	    } else {
		# This allows us to emit 64-bit dwarf for
		# 32-bit targets.
		if { [target_endianness] == "little" } {
		    _op .4byte $offset "$comment (lsw)"
		    _op .4byte 0 "$comment (msw)"
		} else {
		    _op .4byte 0 "$comment (msw)"
		    _op .4byte $offset "$comment (lsw)"
		}
	    }
	} else {
	    error "Don't know how to handle offset size $size"
	}
    }

    proc _compute_label {name} {
	return ".L${name}"
    }

    # Return a name suitable for use as a label.  If BASE_NAME is
    # specified, it is incorporated into the label name; this is to
    # make debugging the generated assembler easier.  If BASE_NAME is
    # not specified a generic default is used.  This proc does not
    # define the label; see 'define_label'.  'new_label' attempts to
    # ensure that label names are unique.
    proc new_label {{base_name label}} {
	variable _label_num

	return [_compute_label ${base_name}[incr _label_num]]
    }

    # Define a label named NAME.  Ordinarily, NAME comes from a call
    # to 'new_label', but this is not required.
    proc define_label {name} {
	_emit "${name}:"
    }

    # A higher-level interface to label handling.
    #
    # ARGS is a list of label descriptors.  Each one is either a
    # single element, or a list of two elements -- a name and some
    # text.  For each descriptor, 'new_label' is invoked.  If the list
    # form is used, the second element in the list is passed as an
    # argument.  The label name is used to define a variable in the
    # enclosing scope; this can be used to refer to the label later.
    # The label name is also used to define a new proc whose name is
    # the label name plus a trailing ":".  This proc takes a body as
    # an argument and can be used to define the label at that point;
    # then the body, if any, is evaluated in the caller's context.
    #
    # For example:
    #
    # declare_labels int_label
    # something { ... $int_label }   ;# refer to the label
    # int_label: constant { ... }    ;# define the label
    proc declare_labels {args} {
	foreach arg $args {
	    set name [lindex $arg 0]
	    set text [lindex $arg 1]

	    if { $text == "" } {
		set text $name
	    }

	    upvar $name label_var
	    set label_var [new_label $text]

	    proc ${name}: {args} [format {
		define_label %s
		uplevel $args
	    } $label_var]
	}
    }

    # Assign elements from LINE to the elements of an array named
    # "argvec" in the caller scope.  The keys used are named in ARGS.
    # If the wrong number of elements appear in LINE, error.
    proc _get_args {line op args} {
	if {[llength $line] != [llength $args] + 1} {
	    error "usage: $op [string toupper $args]"
	}

	upvar argvec argvec
	foreach var $args value [lreplace $line 0 0] {
	    set argvec($var) $value
	}
    }

    # This is a miniature assembler for location expressions.  It is
    # suitable for use in the attributes to a DIE.  Its output is
    # prefixed with "=" to make it automatically use DW_FORM_block.
    #
    # BODY is split by lines, and each line is taken to be a list.
    #
    # DWARF_VERSION is the DWARF version for the section where the location
    # description is found.
    #
    # ADDR_SIZE is the length in bytes (4 or 8) of an address on the target
    # machine (typically found in the header of the section where the location
    # description is found).
    #
    # OFFSET_SIZE is the length in bytes (4 or 8) of an offset into a DWARF
    # section.  This typically depends on whether 32-bit or 64-bit DWARF is
    # used, as indicated in the header of the section where the location
    # description is found.
    #
    # (FIXME should use 'info complete' here.)
    # Each list's first element is the opcode, either short or long
    # forms are accepted.
    # FIXME argument handling
    # FIXME move docs
    proc _location { body dwarf_version addr_size offset_size } {
	variable _constants

	foreach line [split $body \n] {
	    # Ignore blank lines, and allow embedded comments.
	    if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
		continue
	    }
	    set opcode [_map_name [lindex $line 0] _OP]
	    _op .byte $_constants($opcode) $opcode

	    array unset argvec *
	    switch -exact -- $opcode {
		DW_OP_addr {
		    _get_args $line $opcode size
		    _op .${addr_size}byte $argvec(size)
		}

		DW_OP_GNU_addr_index {
		    variable _debug_addr_index
		    variable _cu_addr_size

		    _op .uleb128 ${_debug_addr_index}
		    incr _debug_addr_index

		    _defer_output .debug_addr {
			_op .${_cu_addr_size}byte [lindex $line 1]
		    }
		}

		DW_OP_regx {
		    _get_args $line $opcode register
		    _op .uleb128 $argvec(register)
		}

		DW_OP_pick -
		DW_OP_const1u -
		DW_OP_const1s {
		    _get_args $line $opcode const
		    _op .byte $argvec(const)
		}

		DW_OP_const2u -
		DW_OP_const2s {
		    _get_args $line $opcode const
		    _op .2byte $argvec(const)
		}

		DW_OP_const4u -
		DW_OP_const4s {
		    _get_args $line $opcode const
		    _op .4byte $argvec(const)
		}

		DW_OP_const8u -
		DW_OP_const8s {
		    _get_args $line $opcode const
		    _op .8byte $argvec(const)
		}

		DW_OP_constu {
		    _get_args $line $opcode const
		    _op .uleb128 $argvec(const)
		}
		DW_OP_consts {
		    _get_args $line $opcode const
		    _op .sleb128 $argvec(const)
		}

		DW_OP_plus_uconst {
		    _get_args $line $opcode const
		    _op .uleb128 $argvec(const)
		}

		DW_OP_piece {
		    _get_args $line $opcode size
		    _op .uleb128 $argvec(size)
		}

		DW_OP_bit_piece {
		    _get_args $line $opcode size offset
		    _op .uleb128 $argvec(size)
		    _op .uleb128 $argvec(offset)
		}

		DW_OP_skip -
		DW_OP_bra {
		    _get_args $line $opcode label
		    _op .2byte $argvec(label)
		}

		DW_OP_implicit_value {
		    set l1 [new_label "value_start"]
		    set l2 [new_label "value_end"]
		    _op .uleb128 "$l2 - $l1"
		    define_label $l1
		    foreach value [lrange $line 1 end] {
			switch -regexp -- $value {
			    {^0x[[:xdigit:]]{1,2}$} {_op .byte $value}
			    {^0x[[:xdigit:]]{4}$} {_op .2byte $value}
			    {^0x[[:xdigit:]]{8}$} {_op .4byte $value}
			    {^0x[[:xdigit:]]{16}$} {_op .8byte $value}
			    default {
				error "bad value '$value' in DW_OP_implicit_value"
			    }
			}
		    }
		    define_label $l2
		}

		DW_OP_implicit_pointer -
		DW_OP_GNU_implicit_pointer {
		    _get_args $line $opcode label offset

		    # Here label is a section offset.
		    if { $dwarf_version == 2 } {
			_op .${addr_size}byte $argvec(label)
		    } else {
			_op_offset $offset_size $argvec(label)
		    }
		    _op .sleb128 $argvec(offset)
		}

		DW_OP_GNU_variable_value {
		    _get_args $line $opcode label

		    # Here label is a section offset.
		    if { $dwarf_version == 2 } {
			_op .${addr_size}byte $argvec(label)
		    } else {
			_op_offset $offset_size $argvec(label)
		    }
		}

		DW_OP_deref_size {
		    _get_args $line $opcode size
		    _op .byte $argvec(size)
		}

		DW_OP_bregx {
		    _get_args $line $opcode register offset
		    _op .uleb128 $argvec(register)
		    _op .sleb128 $argvec(offset)
		}

		DW_OP_fbreg {
		    _get_args $line $opcode offset
		    _op .sleb128 $argvec(offset)
		}

		DW_OP_fbreg {
		    _op .sleb128 [lindex $line 1]
		}

		default {
		    if {[llength $line] > 1} {
			error "Unimplemented: operands in location for $opcode"
		    }
		}
	    }
	}
    }

    # Return a label that references the current position in the
    # .debug_addr table.  When a user is creating split DWARF they
    # will define two CUs, the first will be the split DWARF content,
    # and the second will be the non-split stub CU.  The split DWARF
    # CU fills in the .debug_addr section, but the non-split CU
    # includes a reference to the start of the section.  The label
    # returned by this proc provides that reference.
    proc debug_addr_label {} {
	variable _debug_addr_index

	set lbl [new_label "debug_addr_idx_${_debug_addr_index}_"]
	_defer_output .debug_addr {
	    define_label $lbl
	}
	return $lbl
    }

    # Emit a DWARF CU.
    # OPTIONS is a list with an even number of elements containing
    # option-name and option-value pairs.
    # Current options are:
    # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
    #                default = 0 (32-bit)
    # version n    - DWARF version number to emit
    #                default = 4
    # addr_size n  - the size of addresses in bytes: 4, 8, or default
    #                default = default
    # fission 0|1  - boolean indicating if generating Fission debug info
    #                default = 0
    # label <label>
    #              - string indicating label to be defined at the start
    #                of the CU header.
    #                default = ""
    # BODY is Tcl code that emits the DIEs which make up the body of
    # the CU.  It is evaluated in the caller's context.
    proc cu {options body} {
	variable _constants
	variable _cu_count
	variable _abbrev_section
	variable _abbrev_num
	variable _cu_label
	variable _cu_version
	variable _cu_addr_size
	variable _cu_offset_size
	variable _cu_is_fission

	# Establish the defaults.
	set is_64 0
	set _cu_version 4
	set _cu_addr_size default
	set _cu_is_fission 0
	set section ".debug_info"
	set _abbrev_section ".debug_abbrev"
	set label ""

	foreach { name value } $options {
	    set value [uplevel 1 "subst \"$value\""]
	    switch -exact -- $name {
		is_64 { set is_64 $value }
		version { set _cu_version $value }
		addr_size { set _cu_addr_size $value }
		fission { set _cu_is_fission $value }
		label { set label $value }
		default { error "unknown option $name" }
	    }
	}
	if {$_cu_addr_size == "default"} {
	    if {[is_64_target]} {
		set _cu_addr_size 8
	    } else {
		set _cu_addr_size 4
	    }
	}
	set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
	if { $_cu_is_fission } {
	    set section ".debug_info.dwo"
	    set _abbrev_section ".debug_abbrev.dwo"
	}

	if {$_cu_version < 4} {
	    set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
	} else {
	    set _constants(SPECIAL_expr) $_constants(DW_FORM_exprloc)
	}

	_section $section

	set cu_num [incr _cu_count]
	set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
	set _abbrev_num 1

	set _cu_label [_compute_label "cu${cu_num}_begin"]
	set start_label [_compute_label "cu${cu_num}_start"]
	set end_label [_compute_label "cu${cu_num}_end"]

	if { $label != "" } {
	    upvar $label my_label
	    set my_label $_cu_label
	}

	define_label $_cu_label
	if {$is_64} {
	    _op .4byte 0xffffffff
	    _op .8byte "$end_label - $start_label"
	} else {
	    _op .4byte "$end_label - $start_label"
	}
	define_label $start_label
	_op .2byte $_cu_version Version

	# The CU header for DWARF 4 and 5 are slightly different.
	if { $_cu_version == 5 } {
	    _op .byte 0x1 "DW_UT_compile"
	    _op .byte $_cu_addr_size "Pointer size"
	    _op_offset $_cu_offset_size $my_abbrevs Abbrevs
	} else {
	    _op_offset $_cu_offset_size $my_abbrevs Abbrevs
	    _op .byte $_cu_addr_size "Pointer size"
	}

	_defer_output $_abbrev_section {
	    define_label $my_abbrevs
	}

	uplevel $body

	_defer_output $_abbrev_section {
	    # Emit the terminator.
	    _op .byte 0x0 "Abbrev end - Terminator"
	}

	define_label $end_label
    }

    # Emit a DWARF TU.
    # OPTIONS is a list with an even number of elements containing
    # option-name and option-value pairs.
    # Current options are:
    # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
    #                default = 0 (32-bit)
    # version n    - DWARF version number to emit
    #                default = 4
    # addr_size n  - the size of addresses in bytes: 4, 8, or default
    #                default = default
    # fission 0|1  - boolean indicating if generating Fission debug info
    #                default = 0
    # SIGNATURE is the 64-bit signature of the type.
    # TYPE_LABEL is the label of the type defined by this TU,
    # or "" if there is no type (i.e., type stubs in Fission).
    # BODY is Tcl code that emits the DIEs which make up the body of
    # the TU.  It is evaluated in the caller's context.
    proc tu {options signature type_label body} {
	variable _cu_count
	variable _abbrev_section
	variable _abbrev_num
	variable _cu_label
	variable _cu_version
	variable _cu_addr_size
	variable _cu_offset_size
	variable _cu_is_fission

	# Establish the defaults.
	set is_64 0
	set _cu_version 4
	set _cu_addr_size default
	set _cu_is_fission 0
	set section ".debug_types"
	set _abbrev_section ".debug_abbrev"

	foreach { name value } $options {
	    switch -exact -- $name {
		is_64 { set is_64 $value }
		version { set _cu_version $value }
		addr_size { set _cu_addr_size $value }
		fission { set _cu_is_fission $value }
		default { error "unknown option $name" }
	    }
	}
	if {$_cu_addr_size == "default"} {
	    if {[is_64_target]} {
		set _cu_addr_size 8
	    } else {
		set _cu_addr_size 4
	    }
	}
	set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
	if { $_cu_is_fission } {
	    set section ".debug_types.dwo"
	    set _abbrev_section ".debug_abbrev.dwo"
	}

	_section $section

	set cu_num [incr _cu_count]
	set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
	set _abbrev_num 1

	set _cu_label [_compute_label "cu${cu_num}_begin"]
	set start_label [_compute_label "cu${cu_num}_start"]
	set end_label [_compute_label "cu${cu_num}_end"]

	define_label $_cu_label
	if {$is_64} {
	    _op .4byte 0xffffffff
	    _op .8byte "$end_label - $start_label"
	} else {
	    _op .4byte "$end_label - $start_label"
	}
	define_label $start_label
	_op .2byte $_cu_version Version
	_op_offset $_cu_offset_size $my_abbrevs Abbrevs
	_op .byte $_cu_addr_size "Pointer size"
	_op .8byte $signature Signature
	if { $type_label != "" } {
	    uplevel declare_labels $type_label
	    upvar $type_label my_type_label
	    if {$is_64} {
		_op .8byte "$my_type_label - $_cu_label"
	    } else {
		_op .4byte "$my_type_label - $_cu_label"
	    }
	} else {
	    if {$is_64} {
		_op .8byte 0
	    } else {
		_op .4byte 0
	    }
	}

	_defer_output $_abbrev_section {
	    define_label $my_abbrevs
	}

	uplevel $body

	_defer_output $_abbrev_section {
	    # Emit the terminator.
	    _op .byte 0x0 "Abbrev end - Terminator"
	}

	define_label $end_label
    }

    # Emit a DWARF .debug_ranges unit.
    # OPTIONS is a list with an even number of elements containing
    # option-name and option-value pairs.
    # Current options are:
    # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
    #                default = 0 (32-bit)
    #
    # BODY is Tcl code that emits the content of the .debug_ranges
    # unit, it is evaluated in the caller's context.
    proc ranges {options body} {
	variable _debug_ranges_64_bit

	foreach { name value } $options {
	    switch -exact -- $name {
		is_64 { set _debug_ranges_64_bit [subst $value] }
		default { error "unknown option $name" }
	    }
	}

	set section ".debug_ranges"
	_section $section

	proc sequence { body } {
	    variable _debug_ranges_64_bit

	    # Emit the sequence of addresses.

	    proc base { addr } {
		variable _debug_ranges_64_bit

		if { $_debug_ranges_64_bit } then {
		    _op .8byte 0xffffffffffffffff "Base Marker"
		    _op .8byte $addr "Base Address"
		} else {
		    _op .4byte 0xffffffff "Base Marker"
		    _op .4byte $addr "Base Address"
		}
	    }

	    proc range { start end } {
		variable _debug_ranges_64_bit

		if { $_debug_ranges_64_bit } then {
		    _op .8byte $start "Start Address"
		    _op .8byte $end "End Address"
		} else {
		    _op .4byte $start "Start Address"
		    _op .4byte $end "End Address"
		}
	    }

	    uplevel $body

	    # End of the sequence.
	    if { $_debug_ranges_64_bit } then {
		_op .8byte 0x0 "End of Sequence Marker (Part 1)"
		_op .8byte 0x0 "End of Sequence Marker (Part 2)"
	    } else {
		_op .4byte 0x0 "End of Sequence Marker (Part 1)"
		_op .4byte 0x0 "End of Sequence Marker (Part 2)"
	    }
	}

	uplevel $body
    }

    # Emit a DWARF .debug_rnglists section.
    #
    # The target address size is based on the current target's address size.
    #
    # BODY must be Tcl code that emits the content of the section.  It is
    # evaluated in the caller's context.
    #
    # The `is-64 true|false` options tells whether to use 64-bit DWARF instead
    # of 32-bit DWARF.  The default is 32-bit.

    proc rnglists { options body } {
	variable _debug_rnglists_addr_size
	variable _debug_rnglists_offset_size
	variable _debug_rnglists_is_64_dwarf

	parse_options {{"is-64" "false"}}

	if [is_64_target] {
	    set _debug_rnglists_addr_size 8
	} else {
	    set _debug_rnglists_addr_size 4
	}

	if { ${is-64} } {
	    set _debug_rnglists_offset_size 8
	    set _debug_rnglists_is_64_dwarf true
	} else {
	    set _debug_rnglists_offset_size 4
	    set _debug_rnglists_is_64_dwarf false
	}

	_section ".debug_rnglists"

	# Count of tables in the section.
	variable _debug_rnglists_table_count 0

	# Compute the label name for list at index LIST_IDX, for the current
	# table.

	proc _compute_list_label { list_idx } {
	    variable _debug_rnglists_table_count

	    return ".Lrnglists_table_${_debug_rnglists_table_count}_list_${list_idx}"
	}

	with_override Dwarf::table Dwarf::_rnglists_table {
	    uplevel $body
	}
    }

    # Generate one rnglists table (header + offset array + range lists).
    #
    # This proc is meant to be used within proc rnglists' body.  It is made
    # available as `table` while inside proc rnglists' body.
    #
    # BODY must be Tcl code that emits the content of the table.  It may call
    # the LIST_ procedure to generate rnglists.  It is evaluated in the
    # caller's context.
    #
    # The `post-header-label` option can be used to define a label just after
    # the header of the table.  This is the label that a DW_AT_rnglists_base
    # attribute will usually refer to.
    #
    # The `with-offset-array true|false` option can be used to control whether
    # the headers of the location list tables have an array of offset.  The
    # default is true.

    proc _rnglists_table { options body } {
	variable _debug_rnglists_table_count
	variable _debug_rnglists_addr_size
	variable _debug_rnglists_offset_size
	variable _debug_rnglists_is_64_dwarf

	parse_options {
	    {post-header-label ""}
	    {with-offset-array true}
	}

	# Count of lists in the table.
	variable _debug_rnglists_list_count 0

	# Generate the lists ops first, because we need to know how many
	# lists there are to generate the header and offset table.
	set lists_ops [_defer_to_string {
	    with_override Dwarf::list_ Dwarf::_rnglists_list {
		uplevel $body
	    }
	}]

	set post_unit_len_label \
	    [_compute_label "rnglists_table_${_debug_rnglists_table_count}_post_unit_len"]
	set post_header_label \
	    [_compute_label "rnglists_table_${_debug_rnglists_table_count}_post_header"]
	set table_end_label \
	    [_compute_label "rnglists_table_${_debug_rnglists_table_count}_end"]

	# Emit the table header.
	if { $_debug_rnglists_is_64_dwarf } {
	    _op .4byte 0xffffffff "unit length 1/2"
	    _op .8byte "$table_end_label - $post_unit_len_label" "unit length 2/2"
	} else {
	    _op .4byte "$table_end_label - $post_unit_len_label" "unit length"
	}

	define_label $post_unit_len_label

	_op .2byte 5 "dwarf version"
	_op .byte $_debug_rnglists_addr_size "address size"
	_op .byte 0 "segment selector size"

	if { ${with-offset-array} } {
	  _op .4byte "$_debug_rnglists_list_count" "offset entry count"
	} else {
	  _op .4byte 0 "offset entry count"
	}

	define_label $post_header_label

	# Define the user post-header label, if provided.
	if { ${post-header-label} != "" } {
	    define_label ${post-header-label}
	}

	# Emit the offset array.
	if { ${with-offset-array} } {
	    for {set list_idx 0} {$list_idx < $_debug_rnglists_list_count} {incr list_idx} {
		set list_label [_compute_list_label $list_idx]
		_op_offset $_debug_rnglists_offset_size \
		    "$list_label - $post_header_label" \
		    "offset of list $list_idx"
	    }
	}

	# Emit the actual list data.
	_emit "$lists_ops"

	define_label $table_end_label

	incr _debug_rnglists_table_count
    }

    # Generate one rnglists range list.
    #
    # This proc is meant to be used within proc _rnglists_table's body.  It is
    # made available as `list_` while inside proc _rnglists_table's body.
    #
    # BODY may call the various procs defined below to generate list entries.
    # They correspond to the range list entry kinds described in section 2.17.3
    # of the DWARF 5 spec.
    #
    # To define a label pointing to the beginning of the list, use the
    # conventional way of declaring and defining labels:
    #
    #   declare_labels the_list
    #
    #   the_list: list_ { ...  }

    proc _rnglists_list { body } {
	variable _debug_rnglists_list_count

	# Define a label for this list.  It is used to build the offset
	# array later.
	set list_label [_compute_list_label $_debug_rnglists_list_count]
	define_label $list_label

	with_override Dwarf::start_end Dwarf::_rnglists_start_end {
	    uplevel $body
	}

	# Emit end of list.
	_op .byte 0x00 "DW_RLE_end_of_list"

	incr _debug_rnglists_list_count
    }

    # Emit a rnglists DW_RLE_start_end entry.
    #
    # This proc is meant to be used within proc _rnglists_list's body.  It is
    # made available as `start_end` while inside proc _rnglists_list's body.

    proc _rnglists_start_end { start end } {
	variable _debug_rnglists_addr_size

	_op .byte 0x06 "DW_RLE_start_end"
	_op .${_debug_rnglists_addr_size}byte $start "start"
	_op .${_debug_rnglists_addr_size}byte $end "end"
    }

    # Emit a DWARF .debug_loclists section.
    #
    # The target address size is based on the current target's address size.
    #
    # BODY must be Tcl code that emits the content of the section.  It is
    # evaluated in the caller's context.
    #
    # The `is-64 true|false` options tells whether to use 64-bit DWARF instead
    # of 32-bit DWARF.  The default is 32-bit.

    proc loclists { options body } {
	variable _debug_loclists_addr_size
	variable _debug_loclists_offset_size
	variable _debug_loclists_is_64_dwarf

	parse_options {{"is-64" "false"}}

	if [is_64_target] {
	    set _debug_loclists_addr_size 8
	} else {
	    set _debug_loclists_addr_size 4
	}

	if { ${is-64} } {
	    set _debug_loclists_offset_size 8
	    set _debug_loclists_is_64_dwarf true
	} else {
	    set _debug_loclists_offset_size 4
	    set _debug_loclists_is_64_dwarf false
	}

	_section ".debug_loclists"

	# Count of tables in the section.
	variable _debug_loclists_table_count 0

	# Compute the label name for list at index LIST_IDX, for the current
	# table.

	proc _compute_list_label { list_idx } {
	    variable _debug_loclists_table_count

	    return ".Lloclists_table_${_debug_loclists_table_count}_list_${list_idx}"
	}

	with_override Dwarf::table Dwarf::_loclists_table {
	    uplevel $body
	}
    }

    # Generate one loclists table (header + offset array + location lists).
    #
    # This proc is meant to be used within proc loclists' body.  It is made
    # available as `table` while inside proc rnglists' body.
    #
    # BODY must be Tcl code that emits the content of the table.  It may call
    # the LIST_ procedure to generate rnglists.  It is evaluated in the
    # caller's context.
    #
    # The `post-header-label` option can be used to define a label just after
    # the header of the table.  This is the label that a DW_AT_loclists_base
    # attribute will usually refer to.
    #
    # The `with-offset-array true|false` option can be used to control
    # whether the headers of the location list tables have an array of
    # offset.  The default is true.

    proc _loclists_table { options body } {
	variable _debug_loclists_table_count
	variable _debug_loclists_addr_size
	variable _debug_loclists_offset_size
	variable _debug_loclists_is_64_dwarf

	parse_options {
	    {post-header-label ""}
	    {with-offset-array true}
	}

	# Count of lists in the table.
	variable _debug_loclists_list_count 0

	# Generate the lists ops first, because we need to know how many
	# lists there are to generate the header and offset table.
	set lists_ops [_defer_to_string {
	    with_override Dwarf::list_ Dwarf::_loclists_list {
		uplevel $body
	    }
	}]

	set post_unit_len_label \
	    [_compute_label "loclists_table_${_debug_loclists_table_count}_post_unit_len"]
	set post_header_label \
	    [_compute_label "loclists_table_${_debug_loclists_table_count}_post_header"]
	set table_end_label \
	    [_compute_label "loclists_table_${_debug_loclists_table_count}_end"]

	# Emit the table header.
	if { $_debug_loclists_is_64_dwarf } {
	    _op .4byte 0xffffffff "unit length 1/2"
	    _op .8byte "$table_end_label - $post_unit_len_label" "unit length 2/2"
	} else {
	    _op .4byte "$table_end_label - $post_unit_len_label" "unit length"
	}

	define_label $post_unit_len_label

	_op .2byte 5 "DWARF version"
	_op .byte $_debug_loclists_addr_size "address size"
	_op .byte 0 "segment selector size"

	if { ${with-offset-array} } {
	  _op .4byte "$_debug_loclists_list_count" "offset entry count"
	} else {
	  _op .4byte 0 "offset entry count"
	}

	define_label $post_header_label

	# Define the user post-header label, if provided.
	if { ${post-header-label} != "" } {
	    define_label ${post-header-label}
	}

	# Emit the offset array.
	if { ${with-offset-array} } {
	    for {set list_idx 0} {$list_idx < $_debug_loclists_list_count} {incr list_idx} {
		set list_label [_compute_list_label $list_idx]
		_op_offset $_debug_loclists_offset_size \
		    "$list_label - $post_header_label" \
		    "offset of list $list_idx"
	    }
	}

	# Emit the actual list data.
	_emit "$lists_ops"

	define_label $table_end_label

	incr _debug_loclists_table_count
    }

    # Generate one loclists location list.
    #
    # This proc is meant to be used within proc _loclists_table's body.  It is
    # made available as `list_` while inside proc _loclists_table's body.
    #
    # BODY may call the various procs defined below to generate list
    # entries.  They correspond to the location list entry kinds
    # described in section 2.6.2 of the DWARF 5 spec.
    #
    # To define a label pointing to the beginning of the list, use
    # the conventional way of declaring and defining labels:
    #
    #   declare_labels the_list
    #
    #   the_list: list_ {
    #     ...
    #   }

    proc _loclists_list { body } {
	variable _debug_loclists_list_count

	# Count the location descriptions in this list.
	variable _debug_loclists_locdesc_count 0

	# Define a label for this list.  It is used to build the offset
	# array later.
	set list_label [_compute_list_label $_debug_loclists_list_count]
	define_label $list_label

	with_override Dwarf::start_length Dwarf::_loclists_start_length {
	with_override Dwarf::base_address Dwarf::_loclists_base_address {
	with_override Dwarf::start_end Dwarf::_loclists_start_end {
	    uplevel $body
	}}}

	# Emit end of list.
	_op .byte 0x00 "DW_LLE_end_of_list"

	incr _debug_loclists_list_count
    }

    # Emit a DW_LLE_start_length entry.
    #
    # This proc is meant to be used within proc _loclists_list's body.  It is
    # made available as `start_length` while inside proc _loclists_list's body.

    proc _loclists_start_length { start length locdesc } {
	variable _debug_loclists_is_64_dwarf
	variable _debug_loclists_addr_size
	variable _debug_loclists_offset_size
	variable _debug_loclists_table_count
	variable _debug_loclists_list_count
	variable _debug_loclists_locdesc_count

	set locdesc [uplevel [list subst $locdesc]]

	_op .byte 0x08 "DW_LLE_start_length"

	# Start and end of the address range.
	_op .${_debug_loclists_addr_size}byte $start "start"
	_op .uleb128 $length "length"

	# Length of location description.
	set locdesc_start_label ".Lloclists_table_${_debug_loclists_table_count}_list_${_debug_loclists_list_count}_locdesc_${_debug_loclists_locdesc_count}_start"
	set locdesc_end_label ".Lloclists_table_${_debug_loclists_table_count}_list_${_debug_loclists_list_count}_locdesc_${_debug_loclists_locdesc_count}_end"
	_op .uleb128 "$locdesc_end_label - $locdesc_start_label" "locdesc length"

	define_label $locdesc_start_label
	set dwarf_version 5
	_location $locdesc $dwarf_version $_debug_loclists_addr_size $_debug_loclists_offset_size
	define_label $locdesc_end_label

	incr _debug_loclists_locdesc_count
    }

    # Emit a DW_LLE_start_end entry.
    #
    # This proc is meant to be used within proc _loclists_list's body.  It is
    # made available as `start_end` while inside proc _loclists_list's body.

    proc _loclists_start_end { start end locdesc } {
	variable _debug_loclists_is_64_dwarf
	variable _debug_loclists_addr_size
	variable _debug_loclists_offset_size
	variable _debug_loclists_table_count
	variable _debug_loclists_list_count
	variable _debug_loclists_locdesc_count

	set locdesc [uplevel [list subst $locdesc]]

	_op .byte 0x07 "DW_LLE_start_end"

	# Start and end of the address range.
	_op .${_debug_loclists_addr_size}byte $start "start"
	_op .${_debug_loclists_addr_size}byte $end "end"

	# Length of location description.
	set locdesc_start_label ".Lloclists_table_${_debug_loclists_table_count}_list_${_debug_loclists_list_count}_locdesc_${_debug_loclists_locdesc_count}_start"
	set locdesc_end_label ".Lloclists_table_${_debug_loclists_table_count}_list_${_debug_loclists_list_count}_locdesc_${_debug_loclists_locdesc_count}_end"
	_op .uleb128 "$locdesc_end_label - $locdesc_start_label" "locdesc length"

	define_label $locdesc_start_label
	set dwarf_version 5
	_location $locdesc $dwarf_version $_debug_loclists_addr_size $_debug_loclists_offset_size
	define_label $locdesc_end_label

	incr _debug_loclists_locdesc_count
    }

    # Emit a DW_LLE_base_address entry.
    proc _loclists_base_address {addr} {
	variable _debug_loclists_addr_size
	variable _debug_loclists_locdesc_count
	_op .byte 0x06 "DW_LLE_base_address"
	_op .${_debug_loclists_addr_size}byte $addr "base_address"
	incr _debug_loclists_locdesc_count
    }

    # Emit a DWARF .debug_macro section.
    #
    # BODY must be Tcl code that emits the content of the section.  It is
    # evaluated in the caller's context.  The body can use the `unit` proc
    # (see `_macro_unit`) to generate macro units.

    proc macro { body } {
	_section ".debug_macro"

	with_override Dwarf::unit Dwarf::_macro_unit {
	    uplevel $body
	}
    }

    # Generate one macro unit.
    #
    # This proc is meant to be used within proc macro's body.  It is made
    # available as `unit` while inside proc macro's body.
    #
    # BODY must be Tcl code that emits the content of the unit.  It may call
    # procedures defined below, prefixed with `_macro_unit_`, to generate the
    # unit's content.  It is evaluated in the caller's context.
    #
    # The `is-64 true|false` options tells whether to use 64-bit DWARF instead
    # of 32-bit DWARF.  The default is 32-bit.
    #
    # If specified, the `debug-line-offset-label` option is the name of a label
    # to use for the unit header's `debug_line_offset` field value.  If
    # omitted, the unit header will not contain the `debug_line_offset` field.

    proc _macro_unit { options body } {
	parse_options {
	    {"is-64" "false"}
	    {"debug-line-offset-label" ""}
	}

	_op .2byte 5 "version"

	# Flags:
	#
	#   offset_size_flag           = set if is-64 is true
	#   debug_line_offset_flag     = set if debug-line-offset-label is set
	#   opcode_operands_table_flag = 0
	set flags 0

	if { ${is-64} } {
	    set flags [expr $flags | 0x1]
	}

	if { ${debug-line-offset-label} != "" } {
	    set flags [expr $flags | 0x2]
	}

	_op .byte $flags "flags"

	if { ${debug-line-offset-label} != "" } {
	    if { ${is-64} } {
		_op .8byte ${debug-line-offset-label} "debug_line offset"
	    } else {
		_op .4byte ${debug-line-offset-label} "debug_line offset"
	    }
	}

	with_override Dwarf::define Dwarf::_macro_unit_define {
	with_override Dwarf::start_file Dwarf::_macro_unit_start_file {
	with_override Dwarf::end_file Dwarf::_macro_unit_end_file {
	    uplevel $body
	}}}
    }

    # Emit a DW_MACRO_define entry.

    proc _macro_unit_define { lineno text } {
	_op .byte 0x1 "DW_MACRO_define"
	_op .uleb128 $lineno "Line number"
	_op .asciz "\"$text\"" "Macro definition"
    }

    # Emit a DW_MACRO_start_file entry.

    proc _macro_unit_start_file { lineno file_idx } {
	_op .byte 0x3 "DW_MACRO_start_file"
	_op .uleb128 $lineno
	_op .uleb128 $file_idx
    }

    # Emit a DW_MACRO_end_file entry.

    proc _macro_unit_end_file {} {
	_op .byte 0x4 "DW_MACRO_end_file"
    }

    # Emit a DWARF .debug_line unit.
    # OPTIONS is a list with an even number of elements containing
    # option-name and option-value pairs.
    # Current options are:
    # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
    #                default = 0 (32-bit)
    # version n    - DWARF version number to emit
    #                default = 4
    # addr_size n  - the size of addresses in bytes: 4, 8, or default
    #                default = default
    # seg_sel_size n
    #              - the size of segment selector_size in bytes:
    #                default = 0
    #
    # LABEL is the label of the current unit (which is probably
    # referenced by a DW_AT_stmt_list), or "" if there is no such
    # label.
    #
    # BODY is Tcl code that emits the parts which make up the body of
    # the line unit.  It is evaluated in the caller's context.  The
    # following commands are available for the BODY section:
    #
    #   include_dir "dirname" -- adds a new include directory
    #
    #   file_name "file.c" idx -- adds a new file name.  IDX is a
    #   1-based index referencing an include directory or 0 for
    #   current directory.

    proc lines {options label body} {
	variable _line_count
	variable _line_include_dirs
	variable _line_file_names
	variable _line_header_finalized
	variable _line_header_end_label
	variable _line_unit_version
	variable _line_is_64
	variable _line_string_form

	# Establish the defaults.
	set _line_is_64 0
	set _line_unit_version 4
	set _unit_addr_size default
	set _line_include_dirs {}
	set _line_file_names {}
	set _line_header_finalized 0
	set _default_is_stmt 1
	set _seg_sel_size 0
	#set _line_string_form string
	set _line_string_form line_strp

	foreach { name value } $options {
	    switch -exact -- $name {
		is_64 { set _line_is_64 $value }
		version { set _line_unit_version $value }
		addr_size { set _unit_addr_size $value }
		seg_sel_size { set _seg_sel_size $value }
		default_is_stmt { set _default_is_stmt $value }
		string_form { set _line_string_form $value }
		default { error "unknown option $name" }
	    }
	}
	if {$_unit_addr_size == "default"} {
	    if {[is_64_target]} {
		set _unit_addr_size 8
	    } else {
		set _unit_addr_size 4
	    }
	}

	set unit_num [incr _line_count]

	set section ".debug_line"
	_section $section

	if { "$label" != "" } {
	    # Define the user-provided label at this point.
	    $label:
	}

	set unit_len_label [_compute_label "line${_line_count}_start"]
	set unit_end_label [_compute_label "line${_line_count}_end"]
	set header_len_label [_compute_label "line${_line_count}_header_start"]
	set _line_header_end_label [_compute_label "line${_line_count}_header_end"]

	if {$_line_is_64} {
	    _op .4byte 0xffffffff
	    _op .8byte "$unit_end_label - $unit_len_label" "unit_length"
	} else {
	    _op .4byte "$unit_end_label - $unit_len_label" "unit_length"
	}

	define_label $unit_len_label

	_op .2byte $_line_unit_version version

	if { $_line_unit_version >= 5 } {
	    _op .byte $_unit_addr_size "address_size"
	    # Hardcode to 0 for now.
	    _op .byte $_seg_sel_size "seg_sel_size"
	}

	if {$_line_is_64} {
	    _op .8byte "$_line_header_end_label - $header_len_label" "header_length"
	} else {
	    _op .4byte "$_line_header_end_label - $header_len_label" "header_length"
	}

	define_label $header_len_label

	_op .byte 1 "minimum_instruction_length"
	if { $_line_unit_version >= 4 } {
	    # Assume non-VLIW for now.
	    _op .byte 1 "maximum_operations_per_instruction"
	}
	_op .byte $_default_is_stmt "default_is_stmt"
	_op .byte 1 "line_base"
	_op .byte 1 "line_range"
	_op .byte 11 "opcode_base"

	# The standard_opcode_lengths table.  The number of arguments
	# for each of the standard opcodes.  Generating 10 entries here
	# matches the use of 11 in the opcode_base above.  These 10
	# entries match the 9 standard opcodes for DWARF2 plus
	# DW_LNS_prologue_end from DWARF3.
	_op .byte 0 "standard opcode 1"
	_op .byte 1 "standard opcode 2"
	_op .byte 1 "standard opcode 3"
	_op .byte 1 "standard opcode 4"
	_op .byte 1 "standard opcode 5"
	_op .byte 0 "standard opcode 6"
	_op .byte 0 "standard opcode 7"
	_op .byte 0 "standard opcode 8"
	_op .byte 1 "standard opcode 9"
	_op .byte 0 "standard opcode 10"

	# Add a directory entry to the line table header's directory table.
	#
	# Return the index by which this entry can be referred to.
	proc include_dir {dirname} {
	    variable _line_include_dirs
	    lappend _line_include_dirs $dirname

	    if { $Dwarf::_line_unit_version >= 5 } {
		return [expr [llength $_line_include_dirs] - 1]
	    } else {
		return [llength $_line_include_dirs]
	    }
	}

	# Add a file name entry to the line table header's file names table.
	#
	# Return the index by which this entry can be referred to.
	proc file_name {filename diridx} {
	    variable _line_file_names
	    lappend _line_file_names $filename $diridx

	    if { $Dwarf::_line_unit_version >= 5 } {
		return [expr [llength $_line_file_names] - 1]
	    } else {
		return [llength $_line_file_names]
	    }
	}

	proc _line_finalize_header {} {
	    variable _line_header_finalized
	    if { $_line_header_finalized } {
		return
	    }
	    set _line_header_finalized 1

	    variable _line_include_dirs
	    variable _line_file_names

	    variable _line_unit_version
	    variable _line_is_64
	    variable _line_string_form
	    if { $_line_unit_version >= 5 } {
		_op .byte 1 "directory_entry_format_count"
		_op .uleb128 1 \
		    "directory_entry_format (content type code: DW_LNCT_path)"
		switch $_line_string_form {
		    string {
			_op .uleb128 0x08 \
			    "directory_entry_format (form: DW_FORM_string)"
		    }
		    line_strp {
			_op .uleb128 0x1f \
			    "directory_entry_format (form: DW_FORM_line_strp)"
		    }
		}

		set nr_dirs [llength $_line_include_dirs]
		_op .byte $nr_dirs "directory_count"

		foreach dirname $_line_include_dirs {
		    switch $_line_string_form {
			string {
			    _op .ascii [_quote $dirname]
			}
			line_strp {
			    declare_labels string_ptr
			    _defer_output .debug_line_str {
				string_ptr:
				_op .ascii [_quote $dirname]
			    }
			    _op_offset [expr $_line_is_64 ? 8 : 4] $string_ptr
			}
		    }
		}

		_op .byte 2 "file_name_entry_format_count"
		_op .uleb128 1 \
		    "file_name_entry_format (content type code: DW_LNCT_path)"
		switch $_line_string_form {
		    string {
			_op .uleb128 0x08 \
			    "directory_entry_format (form: DW_FORM_string)"
		    }
		    line_strp {
			_op .uleb128 0x1f \
			    "directory_entry_format (form: DW_FORM_line_strp)"
		    }
		}
		_op .uleb128 2 \
		    "file_name_entry_format (content type code: DW_LNCT_directory_index)"
		_op .uleb128 0x0f \
		    "file_name_entry_format (form: DW_FORM_udata)"

		set nr_files [expr [llength $_line_file_names] / 2]
		_op .byte $nr_files "file_names_count"

		foreach { filename diridx } $_line_file_names {
		    switch $_line_string_form {
			string {
			    _op .ascii [_quote $filename]
			}
			line_strp {
			    declare_labels string_ptr
			    _defer_output .debug_line_str {
				string_ptr:
				_op .ascii [_quote $filename]
			    }
			    _op_offset [expr $_line_is_64 ? 8 : 4] $string_ptr
			}
		    }
		    _op .uleb128 $diridx
		}
	    } else {
		foreach dirname $_line_include_dirs {
		    _op .ascii [_quote $dirname]
		}

		_op .byte 0 "Terminator (include_directories)"

		foreach { filename diridx } $_line_file_names {
		    _op .ascii [_quote $filename]
		    _op .sleb128 $diridx
		    _op .sleb128 0 "mtime"
		    _op .sleb128 0 "length"
		}

		_op .byte 0 "Terminator (file_names)"
	    }

	    set _line_include_dirs {}
	    set _line_file_names {}

	    variable _line_header_end_label
	    define_label $_line_header_end_label
	}

	proc program { body } {
	    variable _line_header_end_label
	    variable _line


	    set _line 1

	    _line_finalize_header

	    proc DW_LNE_set_address {addr} {
		_op .byte 0
		set start [new_label "set_address_start"]
		set end [new_label "set_address_end"]
		_op .uleb128 "${end} - ${start}"
		define_label ${start}
		_op .byte 2
		if {[is_64_target]} {
		    _op .8byte ${addr}
		} else {
		    _op .4byte ${addr}
		}
		define_label ${end}
	    }

	    proc DW_LNE_end_sequence {} {
		variable _line
		_op .byte 0
		_op .uleb128 1
		_op .byte 1
		set _line 1
	    }

	    proc DW_LNE_user { len opcode } {
		set DW_LNE_lo_usr 0x80
		set DW_LNE_hi_usr 0xff
		if { $DW_LNE_lo_usr <= $opcode
		     && $opcode <= $DW_LNE_hi_usr } {
		    _op .byte 0
		    _op .uleb128 $len
		    _op .byte $opcode
		    for {set i 1} {$i < $len} {incr i} {
			_op .byte 0
		    }
		} else {
		    error "unknown vendor specific extended opcode: $opcode"
		}
	    }

	    proc DW_LNS_copy {} {
		_op .byte 1
	    }

	    proc DW_LNS_negate_stmt {} {
		_op .byte 6
	    }

	    proc DW_LNS_set_prologue_end {} {
		_op .byte 0x0a
	    }

	    proc DW_LNS_advance_pc {offset} {
		_op .byte 2
		_op .uleb128 ${offset}
	    }

	    proc DW_LNS_advance_line {offset} {
		variable _line
		_op .byte 3
		_op .sleb128 ${offset}
		set _line [expr $_line + $offset]
	    }

	    # A pseudo line number program instruction, that can be used instead
	    # of DW_LNS_advance_line.  Rather than writing:
	    #   {DW_LNS_advance_line [expr $line1 - 1]}
	    #   {DW_LNS_advance_line [expr $line2 - $line1]}
	    #   {DW_LNS_advance_line [expr $line3 - $line2]}
	    # we can just write:
	    #   {line $line1}
	    #   {line $line2}
	    #   {line $line3}
	    proc line {line} {
		variable _line
		set offset [expr $line - $_line]
		DW_LNS_advance_line $offset
	    }

	    proc DW_LNS_set_file {num} {
		_op .byte 4
		_op .sleb128 ${num}
	    }

	    uplevel $body
	}

	uplevel $body

	rename include_dir ""
	rename file_name ""

	_line_finalize_header

	define_label $unit_end_label
    }

    # Emit a DWARF .debug_aranges entry.

    proc arange { options arange_start arange_length } {
	parse_options {
	    { comment "" }
	    { seg_sel "" }
	}

	if { $comment != "" } {
	    # Wrap
	    set comment " ($comment)"
	}

	if { $seg_sel != "" } {
	    variable _seg_size
	    if { $_seg_size == 8 } {
		set seg_op .8byte
	    } elseif { $_seg_size == 4 } {
		set seg_op .4byte
	    } else {
		error \
		    "Don't know how to handle segment selector size $_seg_size"
	    }
	    _op $seg_op $seg_sel "Address range segment selector$comment"
	}

	variable _addr_size
	if { $_addr_size == 8 } {
	    set addr_op .8byte
	} elseif { $_addr_size == 4 } {
	    set addr_op .4byte
	}

	_op $addr_op $arange_start "Address range start$comment"
	_op $addr_op $arange_length "Address range length$comment"
    }

    # Emit a DWARF .debug_aranges unit.
    #
    # OPTIONS is a list with an even number of elements containing
    # option-name and option-value pairs.
    # Current options are:
    # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
    #                default = 0 (32-bit)
    # cu_is_64 0|1 - boolean indicating if LABEL refers to a 64-bit DWARF CU
    #                default = 0 (32-bit)
    # section_version n
    #                - section version number to emit
    #                default = 2
    # seg_size n   - the size of the adress selector in bytes: 0, 4, or 8
    #                default = 0
    #
    # LABEL is the label of the corresponding CU.
    #
    # BODY is Tcl code that emits the parts which make up the body of
    # the aranges unit.  It is evaluated in the caller's context.  The
    # following commands are available for the BODY section:
    #
    #   arange [-c <comment>] [<segment selector>] <start> <length>
    #     -- adds an address range.

    proc aranges { options label body } {
	variable _addr_size
	variable _seg_size

	# Handle options.
	parse_options {
	    { is_64 0 }
	    { cu_is_64 0 }
	    { section_version 2 }
	    { seg_size 0 }
	}
	set _seg_size $seg_size

	if { [is_64_target] } {
	    set _addr_size 8
	} else {
	    set _addr_size 4
	}

	# Switch to .debug_aranges section.
	_section .debug_aranges

	# Keep track of offset from start of section entry to determine
	# padding amount.
	set offset 0

	# Initial length.
	declare_labels aranges_start aranges_end
	set length "$aranges_end - $aranges_start"
	set comment "Length"
	if { $is_64 } {
	    _op .4byte 0xffffffff
	    _op .8byte $length $comment
	    incr offset 12
	} else {
	    _op .4byte $length $comment
	    incr offset 4
	}

	# Start label.
	aranges_start:

	# Section version.
	_op .2byte $section_version "Section version"
	incr offset 2

	# Offset into .debug_info.
	upvar $label my_label
	if { $cu_is_64 } {
	    _op .8byte $my_label "Offset into .debug_info"
	    incr offset 8
	} else {
	    _op .4byte $my_label "Offset into .debug_info"
	    incr offset 4
	}

	# Address size.
	_op .byte $_addr_size "Address size"
	incr offset

	# Segment selector size.
	_op .byte $_seg_size "Segment selector size"
	incr offset

	# Padding.
	set tuple_size [expr 2 * $_addr_size + $_seg_size]
	while { 1 } {
	    if { [expr $offset % $tuple_size] == 0 } {
		break
	    }
	    _op .byte 0 "Pad to $tuple_size byte boundary"
	    incr offset
	}

	# Range tuples.
	uplevel $body

	# Terminator tuple.
	set comment "Terminator"
	if { $_seg_size == 0 } {
	    arange {comment $comment} 0 0
	} else {
	    arange {comment $comment seg_sel 0} 0 0
	}

	# End label.
	aranges_end:
    }

    # Emit a .debug_loc entry.

    proc _loc_entry { start end location_description } {
	# Determine how to emit addresses.
	variable _addr_size
	if { $_addr_size == 8 } {
	    set addr_op .8byte
	} elseif { $_addr_size == 4 } {
	    set addr_op .4byte
	}

	# Emit start and end address.
	_op $addr_op $start "Start address"
	_op $addr_op $end "End address"

	declare_labels location_description_start
	declare_labels location_description_end

	# Emit length of location description.
	set len "$location_description_end - $location_description_start"
	_op .2byte $len "Location description length"

	# Tag start of location description.
	define_label $location_description_start

	# Emit location description.
	variable _cu_version
	variable _cu_offset_size
	_location $location_description $_cu_version $_addr_size \
	    $_cu_offset_size

	# Tag end of location description.
	define_label $location_description_end
    }

    # Emit a DWARF .debug_loc contribution.
    #
    # OPTIONS is a list with an even number of elements containing
    # option-name and option-value pairs.
    # Current options are:
    # cu_is_64 0|1 - boolean indicating if references from location
    #                descriptions refer to a 64-bit DWARF CU.
    #                default = 0 (32-bit)
    # cu_version n - section version of DWARF CU referenced from location
    #                descriptions.
    #                default = 4
    #
    # BODY is Tcl code that emits the parts which make up the body of
    # the debug_loc contribution.  It is evaluated in the caller's context.
    # The following command is available for the BODY section:
    #
    #   entry <start> <end> <location description>
    #     -- emit a .debug_loc entry

    proc loc { options body } {
	# Handle options.
	parse_options {
	    { cu_version 4 }
	    { cu_is_64 0 }
	}

	# Export for use in BODY.
	variable _addr_size
	if { [is_64_target] } {
	    set _addr_size 8
	} else {
	    set _addr_size 4
	}
	variable _cu_version
	set _cu_version $cu_version
	variable _cu_offset_size
	if { $cu_is_64 == 1 } {
	    set _cu_offset_size 8
	} else {
	    set _cu_offset_size 4
	}

	# Switch to .debug_loc section.
	_section .debug_loc

	# Introduce command 'entry'.
	with_override Dwarf::entry Dwarf::_loc_entry {
	    # Emit entries.
	    uplevel $body
	}

	# Determine how to emit addresses.
	if { $_addr_size == 8 } {
	    set addr_op .8byte
	} elseif { $_addr_size == 4 } {
	    set addr_op .4byte
	}

	# Emit <End of list>.
	set comment "<End of list>"
	_op $addr_op 0 "$comment (Part 1/2)"
	_op $addr_op 0 "$comment (Part 2/2)"
    }

    proc _empty_array {name} {
	upvar $name the_array

	catch {unset the_array}
	set the_array(_) {}
	unset the_array(_)
    }

    # Emit a .gnu_debugaltlink section with the given file name and
    # build-id.  The buildid should be represented as a hexadecimal
    # string, like "ffeeddcc".
    proc gnu_debugaltlink {filename buildid} {
	_defer_output .gnu_debugaltlink {
	    _op .ascii [_quote $filename]
	    foreach {a b} [split $buildid {}] {
		_op .byte 0x$a$b
	    }
	}
    }

    proc _note {type name hexdata} {
	set namelen [expr [string length $name] + 1]

	# Name size.
	_op .4byte $namelen
	# Data size.
	_op .4byte [expr [string length $hexdata] / 2]
	# Type.
	_op .4byte $type
	# The name.
	_op .ascii [_quote $name]
	# Alignment.
	set align 2
	set total [expr {($namelen + (1 << $align) - 1) & -(1 << $align)}]
	for {set i $namelen} {$i < $total} {incr i} {
	    _op .byte 0
	}
	# The data.
	foreach {a b} [split $hexdata {}] {
	    _op .byte 0x$a$b
	}
    }

    # Emit a note section holding the given build-id.
    proc build_id {buildid} {
	_defer_output {.note.gnu.build-id a note} {
	    # From elf/common.h.
	    set NT_GNU_BUILD_ID 3

	    _note $NT_GNU_BUILD_ID GNU $buildid
	}
    }

    # Emit a dummy CU.
    proc dummy_cu {} {
	# Generate a CU with default options and empty body.
	cu {label dummy_cu} {
	}

	# Generate an .debug_aranges entry for the dummy CU.
	aranges {} dummy_cu {
	}
    }

    # Emit a DWARF .debug_names section.
    #
    # OPTIONS is a list with an even number of elements containing
    # option-name and option-value pairs.
    # Current options are:
    # is_64 0|1 - boolean indicating if the section contains 64-bit DWARF.
    #             default = 0 (32-bit)
    # version n - section version.
    #             default = 5.
    #
    # BODY is Tcl code that emits the parts which make up the body of
    # the .debug_names section.  It is evaluated in the caller's context.
    # The following commands are available for the BODY section:
    #
    #   cu <cu-label>
    #     -- add a CU.
    #
    #   name <name> <tag> <cu> <hash>
    #     -- add a name.

    proc debug_names { options body } {
	parse_options {
	    { is_64 0 }
	    { version 5 }
	}

	variable _debug_names_offset_size
	if { $is_64 == 1 } {
	    set _debug_names_offset_size 8
	} else {
	    set _debug_names_offset_size 4
	}

	# Section start.
	set section ".debug_names"
	_section $section

	# Header - initial length.
	declare_labels debug_names_start debug_names_end
	set length "$debug_names_end - $debug_names_start"
	set comment "Initial_length"
	if { $is_64 } {
	    _op .4byte 0xffffffff
	    _op .8byte $length $comment
	} else {
	    _op .4byte $length $comment
	}

	# Header - start label.
	debug_names_start:

	# Header - version + padding.
	_op .2byte $version "Version"
	_op .2byte 0 "Padding"

	# Parse the body.
	variable _debug_names_cus
	set _debug_names_cus []
	proc _debug_names_cu { cu } {
	    variable _debug_names_cus
	    lappend _debug_names_cus $cu
	}
	variable _debug_names
	set _debug_names []
	proc _debug_names_name { name tag cu hash } {
	    variable _debug_names
	    declare_labels entry_pool_offset
	    lappend _debug_names [list $name $tag $cu $hash $entry_pool_offset]
	}
	with_override Dwarf::cu Dwarf::_debug_names_cu {
	with_override Dwarf::name Dwarf::_debug_names_name {
	    uplevel $body
	}}

	# Header - CU / TU / foreign TU count.
	_op .4byte [llength $_debug_names_cus] "Comp_unit_count"
	_op .4byte 0 "Local_type_unit_count"
	_op .4byte 0 "Foreign_type_unit_count"

	# Header - bucket count.
	_op .4byte 1 "Bucket_count"

	# Header - name count.
	_op .4byte [llength $_debug_names] "Name_count"

	# Header - abbreviation table size.
	declare_labels debug_names_abbrev_table_start \
	    debug_names_abbrev_table_end
	set abbrev_table_size \
	    "$debug_names_abbrev_table_end - $debug_names_abbrev_table_start"
	_op .4byte $abbrev_table_size "Abbrev_table_size"

	# Header - augmentation string.
	_op .4byte 4 "Augmentation_string_size"
	_op .ascii [_quote GDB] "Augmentation_string"

	# List of CUs.
	set comment "CU offset"
	foreach cu $_debug_names_cus {
	    upvar $cu tmp
	    if { $is_64 } {
		_op .8byte $tmp $comment
	    } else {
		_op .4byte $tmp $comment
	    }
	}

	# List of Local TUs.
	#

	# List of Foreign TUs.
	#

	# Hash Lookup Table - array of buckets.
	_op .4byte 1 "bucket: hash array index 1"

	# Hash Lookup Table - array of hashes.
	foreach idx $_debug_names {
	    set name [lindex $idx 0]
	    set hash [lindex $idx 3]
	    _op .4byte $hash "hash: $name"
	}

	# Name Table - array of string offsets.
	foreach idx $_debug_names {
	    set name [lindex $idx 0]

	    variable _strings
	    if {![info exists _strings($name)]} {
		set _strings($name) [new_label strp]
		_defer_output .debug_str {
		    define_label $_strings($name)
		    _op .ascii [_quote $name]
		}
	    }

	    _op_offset $_debug_names_offset_size $_strings($name) "name: $name"
	}

	# Name Table - array of entry offsets.
	set base_label ""
	foreach idx $_debug_names {
	    set name [lindex $idx 0]
	    set label [lindex $idx 4]
	    if { [string equal $base_label ""]} {
		set base_label $label
	    }
	    _op_offset $_debug_names_offset_size "$label - $base_label" \
		"entry pool offset: $name"
	}

	# Abbreviations Table.
	debug_names_abbrev_table_start:
	set abbrev 1
	variable _constants
	foreach idx $_debug_names {
	    set name [lindex $idx 0]
	    set tag [lindex $idx 1]
	    _op .byte $abbrev "abbrev $abbrev"
	    _op .uleb128 $_constants(DW_TAG_$tag) "DW_TAG_$tag"
	    _op .byte 1  "DW_IDX_compile_unit (attribute)"
	    _op .byte 0x0f "DW_FORM_udata (form)"
	    _op .byte 0  "abbrev terminator (attribute)"
	    _op .byte 0  "abbrev terminator (form)"
	    incr abbrev
	}
	_op .byte 0 "Abbreviations Table terminator"
	debug_names_abbrev_table_end:

	# Entry Pool
	set abbrev 1
	foreach idx $_debug_names {
	    set name [lindex $idx 0]
	    set cu [lindex $idx 2]
	    set label [lindex $idx 4]

	    set cu_index 0
	    foreach idx2 $_debug_names_cus {
		if { $idx2 == $cu } {
		    break
		}
		incr cu_index
	    }

	    define_label $label
	    _op .byte $abbrev "$name: abbrev"
	    _op .uleb128 $cu_index "$name: CU index"
	    _op .byte 0 "$name: terminator"
	    incr abbrev
	}

	# Section end.
	debug_names_end:
    }

    # The top-level interface to the DWARF assembler.
    # OPTIONS is a list with an even number of elements containing
    # option-name and option-value pairs.
    # Current options are:
    # filename <string>
    #           - the name of the file where the generated assembly
    #             code is written.
    #             default = "".
    #  add_dummy_cus <0|1>
    #           - Whether to add dummy CUs before and after the CUs
    #             added in the BODY.
    #             default = 1.
    # As a special case, if OPTIONS is a list of length 1, it's
    # interpreted as specifing the filename.
    # BODY is Tcl code to emit the assembly.  It is evaluated via
    # "eval" -- not uplevel as you might expect, because it is
    # important to run the body in the Dwarf namespace.
    #
    # A typical invocation is something like:
    #    Dwarf::assemble $file {
    #        cu 0 2 8 {
    #            compile_unit {
    #            ...
    #            }
    #        }
    #        cu 0 2 8 {
    #        ...
    #        }
    #    }
    proc assemble {options body} {
	variable _initialized
	variable _output_file
	variable _deferred_output
	variable _defer
	variable _label_num
	variable _strings
	variable _cu_count
	variable _line_count
	variable _line_header_end_label
	variable _debug_ranges_64_bit
	variable _debug_addr_index

	if { [llength $options] == 1 } {
	    set options [list filename [lindex $options 0]]
	}

	parse_options {
	    { filename "" }
	    { add_dummy_cus 1 }
	}

	if {!$_initialized} {
	    _read_constants
	    set _initialized 1
	}

	set _output_file [open $filename w]
	set _cu_count -1
	_empty_array _deferred_output
	set _defer ""
	set _label_num 0
	_empty_array _strings

	set _line_count 0
	set _debug_ranges_64_bit [is_64_target]

	set _debug_addr_index 0

	# Dummy CU at the start to ensure that the first CU in $body is not
	# the first in .debug_info.
	if { $add_dummy_cus } {
	    dummy_cu
	}

	with_shared_gdb {
	    # Not "uplevel" here, because we want to evaluate in this
	    # namespace.  This is somewhat bad because it means we can't
	    # readily refer to outer variables.
	    eval $body
	}

	# Dummy CU at the end to ensure that the last CU in $body is not
	# the last in .debug_info.
	if { $add_dummy_cus } {
	    dummy_cu
	}

	_write_deferred_output

	catch {close $_output_file}
	set _output_file {}
    }
}