File: pro_section.c

package info (click to toggle)
dwarfutils 20180809-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,228 kB
  • sloc: ansic: 63,382; sh: 5,222; cpp: 4,041; makefile: 548; python: 445; awk: 11
file content (3583 lines) | stat: -rw-r--r-- 117,411 bytes parent folder | download
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
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
/*
  Copyright (C) 2000,2004,2006 Silicon Graphics, Inc.  All Rights Reserved.
  Portions Copyright (C) 2007-2017 David Anderson. All Rights Reserved.
  Portions Copyright 2002-2010 Sun Microsystems, Inc. All rights reserved.
  Portions Copyright 2012 SN Systems Ltd. All rights reserved.

  This program is free software; you can redistribute it
  and/or modify it under the terms of version 2.1 of the
  GNU Lesser General Public License as published by the Free
  Software Foundation.

  This program is distributed in the hope that it would be
  useful, but WITHOUT ANY WARRANTY; without even the implied
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.

  Further, this software is distributed without any warranty
  that it is free of the rightful claim of any third person
  regarding infringement or the like.  Any license provided
  herein, whether implied or otherwise, applies only to this
  software file.  Patent licenses, if any, provided herein
  do not apply to combinations of this program with other
  software, or any other product whatsoever.

  You should have received a copy of the GNU Lesser General
  Public License along with this program; if not, write the
  Free Software Foundation, Inc., 51 Franklin Street - Fifth
  Floor, Boston MA 02110-1301, USA.

*/

#include "config.h"
#include "libdwarfdefs.h"
#include <stdio.h>
#include <string.h>
#ifdef   HAVE_ELFACCESS_H
#include <elfaccess.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "pro_incl.h"
#include "pro_section.h"
#include "pro_line.h"
#include "pro_frame.h"
#include "pro_die.h"
#include "pro_macinfo.h"
#include "pro_types.h"
#include "pro_dnames.h"


#ifndef SHF_MIPS_NOSTRIP
/* if this is not defined, we probably don't need it: just use 0 */
#define SHF_MIPS_NOSTRIP 0
#endif
#ifndef R_MIPS_NONE
#define R_MIPS_NONE 0
#endif

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

struct Dwarf_Sort_Abbrev_s {
    unsigned dsa_attr;
    unsigned dsa_form;
    Dwarf_P_Attribute dsa_attrp;
};


/* Must match up with pro_section.h defines of DEBUG_INFO etc
and sectnames (below).  REL_SEC_PREFIX is either ".rel" or ".rela"
see pro_incl.h
*/
const char *_dwarf_rel_section_names[] = {
    REL_SEC_PREFIX ".debug_info",
    REL_SEC_PREFIX ".debug_line",
    REL_SEC_PREFIX ".debug_abbrev",     /* Nothing here refers to anything. */
    REL_SEC_PREFIX ".debug_frame",
    REL_SEC_PREFIX ".debug_aranges",
    REL_SEC_PREFIX ".debug_pubnames",
    REL_SEC_PREFIX ".debug_funcnames",  /* sgi extension */
    REL_SEC_PREFIX ".debug_typenames",  /* sgi extension */
    REL_SEC_PREFIX ".debug_varnames",   /* sgi extension */
    REL_SEC_PREFIX ".debug_weaknames",  /* sgi extension */
    REL_SEC_PREFIX ".debug_macinfo",
    REL_SEC_PREFIX ".debug_loc",
    REL_SEC_PREFIX ".debug_ranges",
    REL_SEC_PREFIX ".debug_types",      /* new in DWARF4 */
    REL_SEC_PREFIX ".debug_pubtypes",   /* new in DWARF3 */
    REL_SEC_PREFIX ".debug_names",      /* DWARF5 aka dnames */
    REL_SEC_PREFIX ".debug_str",        /* Nothing here refers to anything.*/
    REL_SEC_PREFIX ".debug_rnglists",   /* DWARF5. */
    REL_SEC_PREFIX ".debug_line_str",   /* DWARF5. Nothing referselsewhere */
    REL_SEC_PREFIX ".debug_macro",      /* DWARF5. */
    REL_SEC_PREFIX ".debug_loclists",   /* DWARF5. */
    REL_SEC_PREFIX ".debug_rnglists",   /* DWARF5. */
};

/*  names of sections. Ensure that it matches the defines
    in pro_section.h, in the same order
    Must match also _dwarf_rel_section_names above
*/
const char *_dwarf_sectnames[] = {
    ".debug_info",
    ".debug_line",
    ".debug_abbrev",
    ".debug_frame",
    ".debug_aranges",
    ".debug_pubnames",
    ".debug_funcnames",         /* sgi extension */
    ".debug_typenames",         /* sgi extension */
    ".debug_varnames",          /* sgi extension */
    ".debug_weaknames",         /* sgi extension */
    ".debug_macinfo",
    ".debug_loc",
    ".debug_ranges",
    ".debug_types",             /* new in DWARF4 */
    ".debug_pubtypes",          /* new in DWARF3 */
    ".debug_names",             /* new in DWARF5. aka dnames */
    ".debug_str",
    ".debug_line_str",          /* new in DWARF5 */
    ".debug_macro",             /* new in DWARF5 */
    ".debug_loclists",          /* new in DWARF5 */
    ".debug_rnglists",          /* new in DWARF5 */
};




static const Dwarf_Ubyte std_opcode_len[] = { 0, /* DW_LNS_copy */
    1,                          /* DW_LNS_advance_pc */
    1,                          /* DW_LNS_advance_line */
    1,                          /* DW_LNS_set_file */
    1,                          /* DW_LNS_set_column */
    0,                          /* DW_LNS_negate_stmt */
    0,                          /* DW_LNS_set_basic_block */
    0,                          /* DW_LNS_const_add_pc */
    1,                          /* DW_LNS_fixed_advance_pc */
    /*  The following for DWARF3 and DWARF4, though GNU
        uses these in DWARF2 as well. */
    0,                          /* DW_LNS_set_prologue_end */
    0,                          /* DW_LNS_set_epilogue_begin */
    1,                          /* DW_LNS_set_isa */
};

/*  struct to hold relocation entries. Its mantained as a linked
    list of relocation structs, and will then be written at as a
    whole into the relocation section. Whether its 32 bit or
    64 bit will be obtained from Dwarf_Debug pointer.
*/

typedef struct Dwarf_P_Rel_s *Dwarf_P_Rel;
struct Dwarf_P_Rel_s {
    Dwarf_P_Rel dr_next;
    void *dr_rel_datap;
};
typedef struct Dwarf_P_Rel_Head_s *Dwarf_P_Rel_Head;
struct Dwarf_P_Rel_Head_s {
    struct Dwarf_P_Rel_s *drh_head;
    struct Dwarf_P_Rel_s *drh_tail;
};

static int
_dwarf_pro_generate_debug_line_str(Dwarf_P_Debug dbg,
    Dwarf_Signed *nbufs, Dwarf_Error * error);
static int _dwarf_pro_generate_debug_names(Dwarf_P_Debug dbg,
    Dwarf_Signed *nbufs, Dwarf_Error * error);
static int _dwarf_pro_generate_debug_str(Dwarf_P_Debug dbg,
    Dwarf_Signed *nbufs, Dwarf_Error * error);
static int _dwarf_pro_generate_debugline(Dwarf_P_Debug dbg,
    Dwarf_Signed *nbufs, Dwarf_Error * error);
static int _dwarf_pro_generate_debugframe(Dwarf_P_Debug dbg,
    Dwarf_Signed *nbufs, Dwarf_Error * error);
static int _dwarf_pro_generate_debuginfo(Dwarf_P_Debug dbg,
    Dwarf_Signed *nbufs, Dwarf_Error * error);

#if 0
static void
dump_bytes(char * msg,Dwarf_Small * start, long len)
{
    Dwarf_Small *end = start + len;
    Dwarf_Small *cur = start;

    printf("%s ",msg);
    for (; cur < end; cur++) {
        printf("%02x ", *cur);
    }
    printf("\n");
}
#endif

static void
print_single_abbrev(Dwarf_P_Abbrev c, unsigned idx)
{
    unsigned j = 0;

    printf(" %2u idx %2u tag 0x%x attrct %2u\n",idx,
        (unsigned)c->abb_idx,
        (unsigned)c->abb_tag,
        (unsigned)c->abb_n_attr);

    for ( ; j < (unsigned)c->abb_n_attr; ++j) {
        printf("  %2u attr 0x%2x  form 0x%2x\n",
            j,
            (unsigned)c->abb_attrs[j],
            (unsigned)c->abb_forms[j]);
    }
}
#if 0
static void
print_curabbrev(const char *where,
    Dwarf_P_Abbrev curabbrev)
{
    Dwarf_P_Abbrev ca = 0;
    unsigned i = 0;
    for(ca = curabbrev; ca ; ca = ca->abb_next,++i) {
        printf("ABBREV %u from %s\n",i,where);
        print_single_abbrev(ca,i);
    }
}
#endif


/* These macros used as return value for _dwarf_pro_get_opc. */
#define         OPC_INCS_ZERO           -1
#define         OPC_OUT_OF_RANGE        -2
#define         LINE_OUT_OF_RANGE       -3
/*  Given address advance and line advance, it gives
    either special opcode, or a number < 0

    FIXME: Check all three negative values.
    Are any negatives really hard errors?
*/
static int
_dwarf_pro_get_opc(
    struct Dwarf_P_Line_Inits_s *inits,
    Dwarf_Unsigned addr_adv,
    int line_adv)
{
    int line_base = inits->pi_line_base;
    int line_range =inits->pi_line_range;
    Dwarf_Unsigned factored_adv = 0;

    factored_adv = addr_adv / inits->pi_minimum_instruction_length;
    if (line_adv == 0 && factored_adv == 0) {
        return OPC_INCS_ZERO;
    }
    if (line_adv >= line_base && line_adv < line_base + line_range) {
        int opc = (line_adv - line_base) + (factored_adv * line_range) +
            inits->pi_opcode_base;
        if (opc > 255) {
            return OPC_OUT_OF_RANGE;
        }
        return opc;
    }
    return LINE_OUT_OF_RANGE;
}



/*  OFFSET_PLUS_EXTENSION_SIZE is the size of the 'length' field in total.
    Which may be 4,8, or 12 bytes!
    4 is standard DWARF2.
    8 is non-standard MIPS-IRIX 64-bit.
    12 is standard DWARF3 for 64 bit offsets.
    Used in various routines: local variable names
    must match the names here.
*/
#define OFFSET_PLUS_EXTENSION_SIZE (offset_size + extension_size)

/*  Return TRUE if we need the section, FALSE otherwise

    If any of the 'line-data-related' calls were made
    including file or directory entries,
    produce .debug_line .

*/
static int
dwarf_need_debug_line_section(Dwarf_P_Debug dbg)
{
    if (dbg->de_output_version > 4) {
        return FALSE;
    }
    if (dbg->de_lines == NULL && dbg->de_file_entries == NULL
        && dbg->de_inc_dirs == NULL) {
        return FALSE;
    }
    return TRUE;
}

/*  DWARF5 only. */
static int
dwarf_need_debug_names_section(Dwarf_P_Debug dbg)
{
    if (dbg->de_output_version <  5) {
        return FALSE;
    }
    if (!dbg->de_dnames) {
        return FALSE;
    }
    if (!dbg->de_dnames->dn_create_section) {
        return FALSE;
    }
    return TRUE;
}

/*  Convert debug information to  a format such that
    it can be written on disk.
    Called exactly once per execution.
    This is the traditional interface. Bad interface design.
*/
Dwarf_Signed
dwarf_transform_to_disk_form(Dwarf_P_Debug dbg, Dwarf_Error * error)
{
    Dwarf_Signed count = 0;
    int res = 0;

    res = dwarf_transform_to_disk_form_a(dbg, &count,error);
    if (res == DW_DLV_ERROR) {
        return DW_DLV_NOCOUNT;
    }
    return count;
}
/*  Convert debug information to  a format such that
    it can be written on disk.
    Called exactly once per execution.
    This is the interface design used with the consumer
    interface, so easier for callers to work with.
*/
int
dwarf_transform_to_disk_form_a(Dwarf_P_Debug dbg, Dwarf_Signed *count,
    Dwarf_Error * error)
{
    /*  Section data in written out in a number of buffers. Each
        _generate_*() function returns a cumulative count of buffers for
        all the sections.
        dwarf_get_section_bytes() returns pointers to these
        buffers one at a time. */
    Dwarf_Signed nbufs = 0;
    int sect = 0;
    int err = 0;
    Dwarf_Unsigned du = 0;

    if (dbg->de_version_magic_number != PRO_VERSION_MAGIC) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_IA, DW_DLV_ERROR);
    }

    /* Create dwarf section headers */
    for (sect = 0; sect < NUM_DEBUG_SECTIONS; sect++) {
        long flags = 0;

        switch (sect) {

        case DEBUG_INFO:
            if (dbg->de_dies == NULL) {
                continue;
            }
            break;

        case DEBUG_LINE:
            if (dwarf_need_debug_line_section(dbg) == FALSE) {
                continue;
            }
            break;

        case DEBUG_ABBREV:
            if (dbg->de_dies == NULL) {
                continue;
            }
            break;

        case DEBUG_FRAME:
            if (dbg->de_frame_cies == NULL) {
                continue;
            }
            flags = SHF_MIPS_NOSTRIP;
            break;

        case DEBUG_ARANGES:
            if (dbg->de_arange == NULL) {
                continue;
            }
            break;

        case DEBUG_PUBNAMES:
            if (dbg->de_simple_name_headers[dwarf_snk_pubname].
                sn_head == NULL) {
                continue;
            }
            break;
        case DEBUG_PUBTYPES:
            if (dbg->de_simple_name_headers[dwarf_snk_pubtype].
                sn_head == NULL) {
                continue;
            }
            break;

        case DEBUG_STR:
            if (dbg->de_debug_str->ds_data == NULL) {
                continue;
            }
            break;

        case DEBUG_FUNCNAMES:
            if (dbg->de_simple_name_headers[dwarf_snk_funcname].
                sn_head == NULL) {
                continue;
            }
            break;

        case DEBUG_TYPENAMES:
            if (dbg->de_simple_name_headers[dwarf_snk_typename].
                sn_head == NULL) {
                continue;
            }
            break;

        case DEBUG_VARNAMES:
            if (dbg->de_simple_name_headers[dwarf_snk_varname].
                sn_head == NULL) {
                continue;
            }
            break;

        case DEBUG_WEAKNAMES:
            if (dbg->de_simple_name_headers[dwarf_snk_weakname].
                sn_head == NULL) {
                continue;
            }
            break;

        case DEBUG_MACINFO:
            if (dbg->de_first_macinfo == NULL) {
                continue;
            }
            break;
        case DEBUG_NAMES: /* DWARF5 */
            if (dwarf_need_debug_names_section(dbg) == FALSE) {
                continue;
            }
            break;
        case DEBUG_LOC:
            /* Not handled yet. */
            continue;
        case DEBUG_RANGES:
            /* Not handled yet. */
            continue;
        case DEBUG_TYPES:
            /* Not handled yet. */
            continue;
        case DEBUG_MACRO:
            /* Not handled yet. */
            continue;
        case DEBUG_LOCLISTS:
            /* Not handled yet. */
            continue;
        case DEBUG_RNGLISTS:
            /* Not handled yet. */
            continue;
        case DEBUG_LINE_STR:
            if (dwarf_need_debug_line_section(dbg) == FALSE) {
                continue;
            }
            /* Not handled yet. */
            continue;
        default:
            /* logic error: missing a case */
            DWARF_P_DBG_ERROR(dbg, DW_DLE_ELF_SECT_ERR, DW_DLV_ERROR);
        }
        {
            int new_base_elf_sect = 0;

            if (dbg->de_callback_func) {
                new_base_elf_sect =
                    dbg->de_callback_func(_dwarf_sectnames[sect],
                        /* rec size */ 1,
                        SECTION_TYPE,
                        flags, SHN_UNDEF, 0, &du,
                        dbg->de_user_data, &err);
            }
            if (new_base_elf_sect == -1) {
                DWARF_P_DBG_ERROR(dbg, DW_DLE_ELF_SECT_ERR,
                    DW_DLV_ERROR);
            }
            dbg->de_elf_sects[sect] = new_base_elf_sect;
            dbg->de_sect_name_idx[sect] = du;
        }
    }

    nbufs = 0;

    /*  Changing the order in which the sections are generated may cause
        problems because of relocations. */

    if (dwarf_need_debug_line_section(dbg) == TRUE) {
        int res = _dwarf_pro_generate_debugline(dbg,&nbufs, error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }

    if (dbg->de_frame_cies) {
        int res = _dwarf_pro_generate_debugframe(dbg,&nbufs,error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }
    if (dbg->de_first_macinfo) {
        /* For DWARF 2,3,4 only */
        /* Need new code for DWARF5 macro info. FIXME*/
        int res  = _dwarf_pro_transform_macro_info_to_disk(dbg,
            &nbufs,error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }

    if (dbg->de_dies) {
        int res= _dwarf_pro_generate_debuginfo(dbg, &nbufs, error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }

    if (dbg->de_debug_str->ds_data) {
        int res = _dwarf_pro_generate_debug_str(dbg,&nbufs, error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }
    if (dbg->de_debug_line_str->ds_data) {
        int res = _dwarf_pro_generate_debug_line_str(dbg,&nbufs, error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }



    if (dbg->de_arange) {
        int res = _dwarf_transform_arange_to_disk(dbg,&nbufs, error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }
    if (dbg->de_output_version < 5) {
        if (dbg->de_simple_name_headers[dwarf_snk_pubname].sn_head) {
            int res = _dwarf_transform_simplename_to_disk(dbg,
                dwarf_snk_pubname,
                DEBUG_PUBNAMES,
                &nbufs,
                error);
            if (res == DW_DLV_ERROR) {
                return res;
            }
        }
        if (dbg->de_simple_name_headers[dwarf_snk_pubtype].sn_head) {
            int res = _dwarf_transform_simplename_to_disk(dbg,
                dwarf_snk_pubtype,
                DEBUG_PUBTYPES,
                &nbufs,
                error);
            if (res == DW_DLV_ERROR) {
                return res;
            }
        }

        if (dbg->de_simple_name_headers[dwarf_snk_funcname].sn_head) {
            int res = _dwarf_transform_simplename_to_disk(dbg,
                dwarf_snk_funcname,
                DEBUG_FUNCNAMES,
                &nbufs,
                error);
            if (res == DW_DLV_ERROR) {
                return res;
            }
        }

        if (dbg->de_simple_name_headers[dwarf_snk_typename].sn_head) {
            int res = _dwarf_transform_simplename_to_disk(dbg,
                dwarf_snk_typename,
                DEBUG_TYPENAMES,
                &nbufs,
                error);
            if (res == DW_DLV_ERROR) {
                return res;
            }
        }

        if (dbg->de_simple_name_headers[dwarf_snk_varname].sn_head) {
            int res = _dwarf_transform_simplename_to_disk(dbg,
                dwarf_snk_varname,
                DEBUG_VARNAMES,
                &nbufs,
                error);
            if (res == DW_DLV_ERROR) {
                return res;
            }
        }

        if (dbg->de_simple_name_headers[dwarf_snk_weakname].sn_head) {
            int res = _dwarf_transform_simplename_to_disk(dbg,
                dwarf_snk_weakname, DEBUG_WEAKNAMES,
                &nbufs,
                error);
            if (res == DW_DLV_ERROR) {
                return res;
            }
        }
    }
    if (dwarf_need_debug_names_section(dbg) == TRUE) {
        int res = _dwarf_pro_generate_debug_names(dbg,&nbufs, error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }
#if 0  /* FIXME: TODO new sections */
    if (dwarf_need_debug_macro_section(dbg) == TRUE) {
        int res = _dwarf_pro_generate_debug_macro(dbg,&nbufs, error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }
    if (dwarf_need_debug_loclists_section(dbg) == TRUE) {
        int res = _dwarf_pro_generate_debug_loclists(dbg,&nbufs, error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }
    if (dwarf_need_debug_rnglists_section(dbg) == TRUE) {
        int res = _dwarf_pro_generate_debug_rnglists(dbg,&nbufs, error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }
#endif

    {
        Dwarf_Signed new_chunks = 0;
        int res = 0;

        res = dbg->de_transform_relocs_to_disk(dbg, &new_chunks);
        if (res != DW_DLV_OK) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_RELOCS_ERROR,
                DW_DLV_ERROR);
        }
        nbufs += new_chunks;
    }
    *count = nbufs;
    return DW_DLV_OK;
}

static int
write_fixed_size(Dwarf_Unsigned val,
    Dwarf_P_Debug dbg,
    int elfsectno,
    Dwarf_Unsigned size,
    unsigned * size_out,
    Dwarf_Error* error)
{
    unsigned char *data = 0;
    GET_CHUNK_ERR(dbg, elfsectno, data, size, error);
    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &val,
        sizeof(val), size);
    *size_out = size;
    return DW_DLV_OK;
}

static int
write_ubyte(unsigned val,
    Dwarf_P_Debug dbg,
    int elfsectno,
    unsigned *len_out,
    Dwarf_Error* error)
{
    Dwarf_Ubyte db = val;
    unsigned char *data = 0;
    unsigned len = sizeof(Dwarf_Ubyte);
    GET_CHUNK_ERR(dbg, elfsectno, data,
        len, error);
    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
        sizeof(db), len);
    *len_out = 1;
    return DW_DLV_OK;;
}
static int
pretend_write_uval(Dwarf_Unsigned val,
    Dwarf_P_Debug dbg,
    unsigned *uval_len_out,
    Dwarf_Error* error)
{
    char buff1[ENCODE_SPACE_NEEDED];
    int nbytes = 0;
    int res = 0;

    res = _dwarf_pro_encode_leb128_nm(val,
        &nbytes, buff1,
        sizeof(buff1));
    if (res != DW_DLV_OK) {
        DWARF_P_DBG_ERROR(dbg,DW_DLE_LEB_OUT_ERROR , DW_DLV_ERROR);
    }
    *uval_len_out = nbytes;
    return DW_DLV_OK;
}

static int
write_sval(Dwarf_Signed val,
    Dwarf_P_Debug dbg,
    int elfsectno,
    unsigned *sval_len_out,
    Dwarf_Error* error)
{
    char buff1[ENCODE_SPACE_NEEDED];
    unsigned char *data = 0;
    int nbytes = 0;
    int res =  _dwarf_pro_encode_signed_leb128_nm(val,
        &nbytes, buff1,
        sizeof(buff1));
    if (res != DW_DLV_OK) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR);
    }
    GET_CHUNK(dbg, elfsectno, data, nbytes, error);
    memcpy((void *) data, (const void *) buff1, nbytes);
    *sval_len_out = nbytes;
    return DW_DLV_OK;
}

/*  This one does not allocate a chunk, uses
    an already existing chunk.
    data points into that existing chunk. */
static int
append_uval(Dwarf_Unsigned val,
    Dwarf_P_Debug dbg,
    unsigned char *data,
    unsigned * uval_len_out,
    Dwarf_Error* error)
{
    char buff1[ENCODE_SPACE_NEEDED];
    int nbytes = 0;
    int res =  _dwarf_pro_encode_leb128_nm(val,
        &nbytes, buff1,
        sizeof(buff1));
    if (res != DW_DLV_OK) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR);
    }
    memcpy((void *) data, (const void *) buff1, nbytes);
    *uval_len_out = nbytes;
    return DW_DLV_OK;
}


static int
write_uval(Dwarf_Unsigned val,
    Dwarf_P_Debug dbg,
    int elfsectno,
    unsigned * uval_len_out,
    Dwarf_Error* error)
{
    char buff1[ENCODE_SPACE_NEEDED];
    unsigned char *data = 0;
    int nbytes = 0;
    int res =  _dwarf_pro_encode_leb128_nm(val,
        &nbytes, buff1,
        sizeof(buff1));
    if (res != DW_DLV_OK) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR);
    }
    GET_CHUNK_ERR(dbg, elfsectno, data, nbytes, error);
    memcpy((void *) data, (const void *) buff1, nbytes);
    *uval_len_out = nbytes;
    return DW_DLV_OK;
}


static unsigned
write_opcode_uval(int opcode,
    Dwarf_P_Debug dbg,
    int elfsectno,
    Dwarf_Unsigned val,
    unsigned *len_out,
    Dwarf_Error* error)
{
    unsigned ublen = 0;
    int res = 0;
    unsigned uvlen = 0;
    res  = write_ubyte(opcode,dbg,elfsectno,&ublen,error);
    if (res != DW_DLV_OK) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR);
    }
    res = write_uval(val,dbg,elfsectno,&uvlen,error);
    if (res != DW_DLV_OK) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR);
    }
    *len_out = ublen +uvlen;
    return DW_DLV_OK;
}

static int
determine_form_size(Dwarf_P_Debug dbg,
    unsigned format_count,
    struct Dwarf_P_Line_format_s *format,
    unsigned *size_out,
    Dwarf_Bool write_out,
    unsigned char *data,
    Dwarf_Error *error)
{
    unsigned calculated_size = 0;
    unsigned n = 0;
    int res = 0;

    /*  entry format itself */
    calculated_size += sizeof_ubyte(dbg);

    /*  Space for the format details. */
    for(n = 0; n < format_count; ++n) {
        struct Dwarf_P_Line_format_s *lf = format+n;
        unsigned val_len = 0;
        unsigned val_len2 = 0;

        if (write_out) {
            res = append_uval(lf->def_content_type, dbg,
                data,
                &val_len,error);
        } else {
            res = pretend_write_uval(lf->def_content_type, dbg,
                &val_len,error);
        }
        data += val_len;
        if(res != DW_DLV_OK) {
            return res;
        }
        if (write_out) {
            res = append_uval(lf->def_form_code, dbg,
                data,
                &val_len2,error);
        } else {
            res = pretend_write_uval(lf->def_form_code, dbg,
                &val_len2,error);
        }
        if(res != DW_DLV_OK) {
            return res;
        }
        data += val_len2;
        calculated_size += val_len + val_len2;
    }
    *size_out = calculated_size;
    return DW_DLV_OK;
}

static int
determine_file_content_size(Dwarf_P_Debug dbg,
    Dwarf_P_F_Entry entry_list,
    Dwarf_Unsigned format_count,
    struct Dwarf_P_Line_format_s *format,
    unsigned *size_out,
    Dwarf_Bool write_out,
    unsigned char *data,
    Dwarf_Error *error)
{
    unsigned calculated_size = 0;
    unsigned count_len   = 0;
    Dwarf_P_F_Entry  cur = 0;
    Dwarf_P_F_Entry  nxt = 0;
    unsigned n           = 0;
    int res              = 0;
    Dwarf_Unsigned offset_size = 0;

    offset_size = dbg->de_offset_size;
    res = pretend_write_uval(format_count,dbg,
        &count_len,error);
    if(res != DW_DLV_OK) {
        return res;
    }
    calculated_size += count_len;

    cur =  entry_list;
    for(n = 0; cur; n++,cur = nxt) {
        unsigned f = 0;
        nxt = cur->dfe_next;

        for( ; f < format_count; f++) {
            struct Dwarf_P_Line_format_s *lf = format+f;
            unsigned ctype = lf->def_content_type;
            unsigned cform = lf->def_form_code;

            switch (ctype) {
            case DW_LNCT_path: {
                switch(cform) {
                case DW_FORM_string: {
                    unsigned slen = strlen(cur->dfe_name) +1;
                    calculated_size += slen;
                    if (write_out) {
                        strcpy((char *)data, cur->dfe_name);
                        data += slen;
                    }
                    }
                    break;
                case DW_FORM_strp: {
                    unsigned slen = strlen(cur->dfe_name) +1;
                    if (write_out) {
                        Dwarf_Unsigned stroffset = 0;
                        res = _dwarf_insert_or_find_in_debug_str(
                            dbg,
                            cur->dfe_name,
                            _dwarf_hash_debug_str,
                            slen,
                            &stroffset,error);
                        if (res != DW_DLV_OK) {
                            return res;
                        }
                        WRITE_UNALIGNED(dbg, (void *) data,
                            (const void *) &stroffset,
                            sizeof(stroffset), offset_size);
                        data += offset_size;
                    }
                    calculated_size += offset_size;
                    }
                    break;
                case DW_FORM_line_strp: {
                    unsigned slen = strlen(cur->dfe_name) +1;
                    if (write_out) {
                        Dwarf_Unsigned stroffset = 0;
                        res = _dwarf_insert_or_find_in_debug_str(
                            dbg,
                            cur->dfe_name,
                            _dwarf_hash_debug_line_str,
                            slen,
                            &stroffset,error);
                        if (res != DW_DLV_OK) {
                            return res;
                        }
                        WRITE_UNALIGNED(dbg, (void *) data,
                            (const void *) &stroffset,
                            sizeof(stroffset), offset_size);
                        data += offset_size;
                    }
                    calculated_size += offset_size;
                    }
                    break;
                case DW_FORM_strp_sup:
                /* Following in dwo only. */
                case DW_FORM_strx:
                case DW_FORM_strx1:
                case DW_FORM_strx2:
                case DW_FORM_strx3:
                case DW_FORM_strx4:
                default:
                    DWARF_P_DBG_ERROR(dbg,
                        DW_DLE_LNCT_FORM_CODE_NOT_HANDLED, DW_DLV_ERROR);
                    break;
                }
                }
                break;
            case DW_LNCT_directory_index: {
                switch(cform) {
                case DW_FORM_data1:
                    calculated_size += 1;
                    if (write_out) {
                        unsigned char ub = cur->dfe_index;
                        *data = ub;
                        data += 1;
                    }
                    break;
                case DW_FORM_data2:
                    calculated_size += 2;
                    if (write_out) {
                        Dwarf_Half uh = cur->dfe_index;
                        memcpy(data,&uh,2);
                        data += 2;
                    }
                    break;
                case DW_FORM_udata: {
                    unsigned val_len = 0;
                    if (write_out) {
                        res = append_uval(cur->dfe_index,
                            dbg,
                            data,
                            &val_len,error);
                        data += val_len;
                    } else {
                        res = pretend_write_uval(cur->dfe_index,
                            dbg, &val_len,error);
                    }
                    if (res != DW_DLV_OK) {
                        return res;
                    }
                    calculated_size += val_len;
                    }
                    break;
                default:
                    DWARF_P_DBG_ERROR(dbg,
                        DW_DLE_LNCT_FORM_CODE_NOT_HANDLED, DW_DLV_ERROR);
                }
                }
                break;
            case DW_LNCT_timestamp: {
                switch(cform) {
                case DW_FORM_udata: {
                    unsigned val_len = 0;
                    if (write_out) {
                        res = append_uval(cur->dfe_timestamp,
                            dbg,
                            data,
                            &val_len,error);
                        data += val_len;
                    } else {
                        res = pretend_write_uval(cur->dfe_timestamp,
                            dbg, &val_len,error);
                    }
                    if (res != DW_DLV_OK) {
                        return res;
                    }
                    calculated_size += val_len;
                    }
                    break;
                case DW_FORM_data4: {
                    calculated_size += 4;
                    if (write_out) {
                        Dwarf_ufixed u4 = cur->dfe_timestamp;
                        memcpy(data,&u4,4);
                        data += 4;
                    }
                    }
                    break;
                case DW_FORM_data8:
                    /*  As of 2017 there is no 8 byte timestamp
                        defined, though it does have to happen.
                        before 2038. */
                    calculated_size += 8;
                    if (write_out) {
                        Dwarf_Unsigned u8 = cur->dfe_index;
                        WRITE_UNALIGNED(dbg, (void *) data,
                            (const void *) &u8,
                            sizeof(u8), 8);
                        data += 8;
                    }
                    break;
                case DW_FORM_block:
                default:
                    DWARF_P_DBG_ERROR(dbg,
                        DW_DLE_LNCT_FORM_CODE_NOT_HANDLED, DW_DLV_ERROR);
                }
                }
                break;
            case DW_LNCT_size: {
                switch(cform) {
                case DW_FORM_data1:
                    calculated_size += 1;
                    if (write_out) {
                        unsigned char ub = cur->dfe_index;
                        *data = ub;
                        data += 1;
                    }
                    break;
                case DW_FORM_data2:
                    calculated_size += 2;
                    if (write_out) {
                        Dwarf_Half uh = cur->dfe_index;
                        memcpy(data,&uh,2);

                    }
                    break;
                case DW_FORM_data4:
                    calculated_size += 4;
                    if (write_out) {
                        Dwarf_ufixed u4 = cur->dfe_index;
                        memcpy(data,&u4,4);
                        data += 4;
                    }
                    break;
                case DW_FORM_data8:
                    calculated_size += 8;
                    if (write_out) {
                        Dwarf_Unsigned u8 = cur->dfe_index;
                        WRITE_UNALIGNED(dbg, (void *) data,
                            (const void *) &u8,
                            sizeof(u8), 8);
                        data += 8;
                    }
                    break;
                case DW_FORM_udata: {
                    unsigned val_len = 0;
                    if (write_out) {
                        res = append_uval(cur->dfe_size,
                            dbg,
                            data,
                            &val_len,error);
                        data += val_len;
                    } else {
                        res = pretend_write_uval(cur->dfe_size,
                            dbg, &val_len,error);
                    }
                    if (res != DW_DLV_OK) {
                        return res;
                    }
                    calculated_size += val_len;
                    }
                    break;
                default:
                    DWARF_P_DBG_ERROR(dbg,
                        DW_DLE_LNCT_FORM_CODE_NOT_HANDLED, DW_DLV_ERROR);
                }
                }
                break;
            case DW_LNCT_MD5: {
                switch(cform) {
                case DW_FORM_data16:
                    if (write_out) {
                        memcpy(data,cur->dfe_md5,sizeof(cur->dfe_md5));
                        data += 16;
                    }
                    calculated_size += 16;
                    break;
                default:
                    DWARF_P_DBG_ERROR(dbg,
                        DW_DLE_LNCT_FORM_CODE_NOT_HANDLED, DW_DLV_ERROR);
                }
                }
                break;
            default:
                DWARF_P_DBG_ERROR(dbg, DW_DLE_LNCT_CODE_UNKNOWN, DW_DLV_ERROR);
            }
        }
    }
    *size_out = calculated_size;
    return DW_DLV_OK;
}

static int
calculate_size_of_line_header5(Dwarf_P_Debug dbg,
    struct Dwarf_P_Line_Inits_s *inits,
    unsigned *prolog_size_out,
    Dwarf_Error *error)
{
    unsigned prolog_size = 0;
    int offset_size = dbg->de_offset_size;
    int extension_size = dbg->de_64bit_extension ? 4 : 0;
    int res = 0;

    prolog_size += OFFSET_PLUS_EXTENSION_SIZE +
        sizeof_uhalf(dbg) + /* version # */
        sizeof_ubyte(dbg) +     /* address_size */
        sizeof_ubyte(dbg) +     /* segment_selector_size */
        offset_size       +     /* header length */
        sizeof_ubyte(dbg) +     /* min_instr length */
        sizeof_ubyte(dbg) +     /* maximum_operations_per_instruction */
        sizeof_ubyte(dbg) +     /* default is_stmt */
        sizeof_ubyte(dbg) +     /* linebase */
        sizeof_ubyte(dbg) +     /* linerange */
        sizeof_ubyte(dbg);      /* opcode base */
        /* For maximum_operations_per_instruction. */
        prolog_size += sizeof_ubyte(dbg);

    /* standard_opcode_lengths table len */
    prolog_size += inits->pi_opcode_base-1;

    {
        unsigned fsize = 0;
        res = determine_form_size(dbg,
            inits->pi_directory_entry_format_count,
            inits->pi_incformats,
            &fsize,FALSE,0,error);
        if (res != DW_DLV_OK) {
            return res;
        }
        prolog_size += fsize;
    }
    {
        unsigned dir_count_len = 0;
        res = determine_file_content_size(dbg,
            dbg->de_inc_dirs,
            dbg->de_line_inits.pi_directory_entry_format_count,
            dbg->de_line_inits.pi_incformats,
            &dir_count_len,
            FALSE,0,
            error);
        if (res != DW_DLV_OK) {
            return res;
        }
        prolog_size += dir_count_len;
    }
    {
        unsigned fsize = 0;
        res = determine_form_size(dbg,
            inits->pi_file_entry_format_count,
            inits->pi_fileformats,
            &fsize,
            FALSE,0,
            error);
        if (res != DW_DLV_OK) {
            return res;
        }
        prolog_size += fsize;
    }
    {
        unsigned file_count_len = 0;
        res = determine_file_content_size(dbg,
            dbg->de_file_entries,
            dbg->de_line_inits.pi_file_entry_format_count,
            dbg->de_line_inits.pi_fileformats,
            &file_count_len,
            FALSE,0,
            error);
        if (res != DW_DLV_OK) {
            return res;
        }
        prolog_size += file_count_len;
    }
    *prolog_size_out = prolog_size;
    return DW_DLV_OK;
}

/* For DWARF 2,3,4 */
static int
calculate_size_of_line_header4(Dwarf_P_Debug dbg,
    struct Dwarf_P_Line_Inits_s *inits,
    unsigned *prolog_size_out,
    UNUSEDARG Dwarf_Error *error)
{
    Dwarf_P_F_Entry curdir = 0;
    Dwarf_P_F_Entry curentry = 0;
    unsigned prolog_size = 0;
    int offset_size = dbg->de_offset_size;
    int extension_size = dbg->de_64bit_extension ? 4 : 0;

    prolog_size += OFFSET_PLUS_EXTENSION_SIZE +
        sizeof_uhalf(dbg) +  /* version # */
        offset_size       +  /* header length */
        sizeof_ubyte(dbg) +  /* min_instr length */
        sizeof_ubyte(dbg) +  /* default is_stmt */
        sizeof_ubyte(dbg) +  /* linebase */
        sizeof_ubyte(dbg) +  /* linerange */
        sizeof_ubyte(dbg);   /* opcode base */
    if (inits->pi_version == DW_LINE_VERSION4) {
        /* For maximum_operations_per_instruction. */
        prolog_size += sizeof_ubyte(dbg);
    }
    /* standard_opcode_lengths table len */
    prolog_size += inits->pi_opcode_base-1;

    /* include directories */
    curdir = dbg->de_inc_dirs;
    while (curdir) {
        prolog_size += strlen(curdir->dfe_name) + 1;
        curdir = curdir->dfe_next;
    }
    prolog_size++; /* last null following last directory
        entry. */

    /* file entries */
    curentry = dbg->de_file_entries;
    while (curentry) {
        prolog_size +=
            strlen(curentry->dfe_name) + 1 + curentry->dfe_nbytes;
        curentry = curentry->dfe_next;
    }
    prolog_size++; /* last null byte */
    *prolog_size_out = prolog_size;
    return DW_DLV_OK;
}


/* Generate debug_line section
   Dwarf2, dwarf3 headers are the same (DW3 acknowledges 64bit).
   DWARF4 adds the maximum_operations_per_instruction field.
   DWARF5 adds address size and address selector size
   and replaces the entire directories/files list with
   very different stuff.
*/
static int
_dwarf_pro_generate_debugline(Dwarf_P_Debug dbg,
    Dwarf_Signed * nbufs,
    Dwarf_Error * error)
{
    Dwarf_P_F_Entry curdir = 0;
    Dwarf_P_F_Entry curentry = 0;
    Dwarf_P_Line curline = 0;
    Dwarf_P_Line prevline = 0;
    struct Dwarf_P_Line_Inits_s *inits = 0;

    /* all data named cur* are used to loop thru linked lists */

    int sum_bytes = 0;
    unsigned prolog_size = 0;
    unsigned char *data = 0;    /* holds disk form data */
    int elfsectno = 0;
    unsigned char *start_line_sec = 0;  /* pointer to the buffer at
        section start */
    /* temps for memcpy */
    Dwarf_Unsigned du = 0;
    Dwarf_Ubyte db = 0;
    Dwarf_Half dh = 0;
    int res = 0;
    Dwarf_Half version = dbg->de_output_version;
    int offset_size = dbg->de_offset_size;
    Dwarf_Ubyte extension_size = dbg->de_64bit_extension ? 4 : 0;
    Dwarf_Ubyte address_size = dbg->de_pointer_size;

    sum_bytes = 0;

    elfsectno = dbg->de_elf_sects[DEBUG_LINE];

    inits = &dbg->de_line_inits;
    if (version < 5) {
        res  = calculate_size_of_line_header4(dbg,inits,&prolog_size,
            error);
    } else if (version == 5) {
        res  = calculate_size_of_line_header5(dbg,inits,&prolog_size,
            error);
    } else {
        _dwarf_p_error(dbg, error,DW_DLE_VERSION_STAMP_ERROR );
        return DW_DLV_ERROR;
    }
    if (res != DW_DLV_OK) {
        return res;
    }
    /* Allocate a chunk, put address in 'data' */
    GET_CHUNK_ERR(dbg, elfsectno, data, prolog_size, error);

    start_line_sec = data;

    /* Copy the prologue data into 'data' */
    /* total_length */
    du = 0;
    if (extension_size) {
        Dwarf_Word x = DISTINGUISHED_VALUE;

        WRITE_UNALIGNED(dbg, (void *) data, (const void *) &x,
        sizeof(x), extension_size);
        data += extension_size;
    }

    /*  We will adjust this later, we do not know the full length
        of the line_section content for this cu  yet. */
    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &du,
        sizeof(du), offset_size);
    data += offset_size;

    dh =  inits->pi_version;
    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &dh,
        sizeof(dh), DWARF_HALF_SIZE);
    data +=  DWARF_HALF_SIZE;
    if (version == 5 ) {
        /* address size, seg sel size now */
        db = inits->pi_address_size;
        WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
            sizeof(db), sizeof(db));
        data += sizeof(db);
        db = inits->pi_segment_size; /* segment selector size */
        WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
            sizeof(db), sizeof(db));
        data += sizeof(db);
    }

    {
        /*  header length (called prolog length in DWARF2)
            This we do know, we calculated the prolog length
            already and it is prolog_size so just
            */
        Dwarf_Unsigned sofar = data  - start_line_sec;

        du = prolog_size - sofar - offset_size;
        WRITE_UNALIGNED(dbg, (void *) data, (const void *) &du,
            sizeof(du), offset_size);
        data += offset_size;
    }
    db =  inits->pi_minimum_instruction_length;
    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
        sizeof(db), sizeof(Dwarf_Ubyte));
    data += sizeof(Dwarf_Ubyte);

    if (inits->pi_version == 4 || inits->pi_version == 5) {
        db =  inits->pi_maximum_operations_per_instruction;
        WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
            sizeof(db), sizeof(Dwarf_Ubyte));
        data += sizeof(Dwarf_Ubyte);
    }

    db =  inits->pi_default_is_stmt;
    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
        sizeof(db), sizeof(Dwarf_Ubyte));
    data += sizeof(Dwarf_Ubyte);
    db =  inits->pi_line_base;
    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
        sizeof(db), sizeof(Dwarf_Ubyte));
    data += sizeof(Dwarf_Ubyte);
    db =  inits->pi_line_range;
    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
        sizeof(db), sizeof(Dwarf_Ubyte));
    data += sizeof(Dwarf_Ubyte);
    db =  inits->pi_opcode_base;
    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
        sizeof(db), sizeof(Dwarf_Ubyte));
    data += sizeof(Dwarf_Ubyte);
    WRITE_UNALIGNED(dbg, (void *) data, (const void *) std_opcode_len,
        inits->pi_opcode_base-1,
        inits->pi_opcode_base-1);
    data += inits->pi_opcode_base-1;

    if (version < 5) {
        /* copy over include directories */
        curdir = dbg->de_inc_dirs;
        while (curdir) {
            strcpy((char *) data, curdir->dfe_name);
            data += strlen(curdir->dfe_name) + 1;
            curdir = curdir->dfe_next;
        }
        *data = '\0';               /* last null */
        data++;

        /* copy file entries */
        curentry = dbg->de_file_entries;
        while (curentry) {
            strcpy((char *) data, curentry->dfe_name);
            data += strlen(curentry->dfe_name) + 1;
            /* copies of leb numbers, no endian issues */
            memcpy((void *) data,
                (const void *) curentry->dfe_args, curentry->dfe_nbytes);
            data += curentry->dfe_nbytes;
            curentry = curentry->dfe_next;
        }
        *data = '\0';
        data++;
    } else if (version == 5) {
        {
            unsigned fsize = 0;
            res = determine_form_size(dbg,
                inits->pi_directory_entry_format_count,
                inits->pi_incformats,
                &fsize,
                TRUE,data,
                error);
            if (res != DW_DLV_OK) {
                return res;
            }
            data += fsize;
        }
        {
            unsigned dir_count_len = 0;
            res = determine_file_content_size(dbg,
                dbg->de_inc_dirs,
                inits->pi_directory_entry_format_count,
                inits->pi_incformats,
                &dir_count_len,
                TRUE,data,
                error);
            if (res != DW_DLV_OK) {
                return res;
            }
            data += dir_count_len;
        }
        {
            unsigned fsize = 0;
            res = determine_form_size(dbg,
                inits->pi_file_entry_format_count,
                inits->pi_fileformats,
                &fsize,
                TRUE,data,
                error);
            if (res != DW_DLV_OK) {
                return res;
            }
            data += fsize;
        }
        {
            unsigned file_count_len = 0;
            res = determine_file_content_size(dbg,
                dbg->de_file_entries,
                dbg->de_line_inits.pi_file_entry_format_count,
                dbg->de_line_inits.pi_fileformats,
                &file_count_len,
                TRUE,data,
                error);
            if (res != DW_DLV_OK) {
                return res;
            }
            data += file_count_len;
        }
    }

    {
        Dwarf_Unsigned sofar = data - start_line_sec;
        if (sofar != prolog_size) {
            /* We miscalculated something. */
            _dwarf_p_error(dbg, error,
                DW_DLE_LINE_HEADER_LENGTH_BOTCH);
            return DW_DLV_ERROR;
        }
        sum_bytes += prolog_size;
    }

    curline = dbg->de_lines;
    prevline = (Dwarf_P_Line)
        _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Line_s));
    if (prevline == NULL) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_LINE_ALLOC, DW_DLV_ERROR);
    }
    _dwarf_pro_reg_init(dbg,prevline);
    /* generate opcodes for line numbers */
    while (curline) {
        int opc = 0;
        int no_lns_copy = 0; /* if lns copy opcode does not need to be
            generated, if special opcode or end
            sequence */
        Dwarf_Unsigned addr_adv = 0;
        int line_adv = 0;           /* supposed to be a reasonably small
            number, so the size should not be a
            problem. ? */

        no_lns_copy = 0;
        if (curline->dpl_opc != 0) {
            int inst_bytes = 0;     /* no of bytes in extended opcode */
            unsigned writelen = 0;

            switch (curline->dpl_opc) {
            case DW_LNE_end_sequence:
                /* Advance pc to end of text section. */
                addr_adv = curline->dpl_address - prevline->dpl_address;
                if (addr_adv > 0) {
                    res = write_opcode_uval(DW_LNS_advance_pc,dbg,
                        elfsectno,
                        addr_adv/inits->pi_minimum_instruction_length,
                        &writelen,
                        error);
                    if (res != DW_DLV_OK) {
                        return res;
                    }
                    sum_bytes += writelen;
                    prevline->dpl_address = curline->dpl_address;
                }

                /* first null byte */
                db = 0;
                res = write_ubyte(db,dbg,elfsectno,
                    &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* write length of extended opcode */
                inst_bytes = sizeof(Dwarf_Ubyte);
                res = write_uval(inst_bytes,dbg,elfsectno,
                    &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* write extended opcode */
                res = write_ubyte(DW_LNE_end_sequence,dbg,elfsectno,
                    &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* reset value to original values */
                _dwarf_pro_reg_init(dbg,prevline);
                no_lns_copy = 1;
                /*  this is set only for end_sequence, so that a
                    dw_lns_copy is not generated */
                break;

            case DW_LNE_set_address:

                /* first null byte */
                db = 0;
                res = write_ubyte(db,dbg,elfsectno,
                    &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* write length of extended opcode */
                inst_bytes = sizeof(Dwarf_Ubyte) + address_size;
                res = write_uval(inst_bytes,dbg,elfsectno,
                    &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* write extended opcode */
                res = write_ubyte(DW_LNE_set_address,dbg,elfsectno,
                    &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* reloc for address */
                res = dbg->de_relocate_by_name_symbol(dbg,
                    DEBUG_LINE,
                    sum_bytes,  /* r_offset  */
                    curline->dpl_r_symidx,
                    dwarf_drt_data_reloc,
                    offset_size);
                if (res != DW_DLV_OK) {
                    DWARF_P_DBG_ERROR(dbg, DW_DLE_CHUNK_ALLOC, DW_DLV_ERROR);
                }

                /* write offset (address) */
                du = curline->dpl_address;
                res = write_fixed_size(du,dbg,elfsectno,
                    address_size,&writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;
                prevline->dpl_address = curline->dpl_address;
                no_lns_copy = 1;
                break;
            case DW_LNE_define_file:
                /*  Not supported, all add-file entries
                    are added via dbg  -> de_file_entries,
                    which adds to the line table header.  */
                no_lns_copy = 1;
                break;
            case DW_LNE_set_discriminator: {/* DWARF4 */
                unsigned val_len = 0;
                /* first null byte */
                db = 0;
                res = write_ubyte(db,dbg,elfsectno,&writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* Write len of opcode + value here. */
                res = pretend_write_uval(curline->dpl_discriminator,
                    dbg, &val_len,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                val_len++;

                res = write_uval(val_len +1,dbg,elfsectno,
                    &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* Write opcode */
                res = write_ubyte(DW_LNE_set_discriminator,
                    dbg,elfsectno,
                    &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* Write the value itself. */
                res = write_uval(curline->dpl_discriminator,
                    dbg,elfsectno,&writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;
                no_lns_copy = 1;
                }
                break;
            }
        } else {
            unsigned writelen = 0;
            if (inits->pi_opcode_base >12) {
                /*  We have the newer standard opcodes
                    DW_LNS_set_prologue_end, DW_LNS_set_epilogue_end,
                    DW_LNS_set_isa, we do not write them if not
                    in the table. DWARF3 and DWARF4 */
                /*  Should we check if a change? These reset automatically
                    in the line processing/reading engine,
                    so I think no check of prevline is wanted. */
                if (curline->dpl_epilogue_begin) {
                    res = write_ubyte(DW_LNS_set_epilogue_begin,dbg,
                        elfsectno,&writelen, error);
                    if (res != DW_DLV_OK) {
                        return res;
                    }
                    sum_bytes += writelen;
                }
                if (curline->dpl_prologue_end) {
                    res = write_ubyte(DW_LNS_set_prologue_end,dbg,
                        elfsectno, &writelen,error);
                    if (res != DW_DLV_OK) {
                        return res;
                    }
                    sum_bytes += writelen;
                }
                if (curline->dpl_isa != prevline->dpl_isa) {
                    res = write_opcode_uval(DW_LNS_set_isa,dbg,
                        elfsectno, curline->dpl_isa,
                        &writelen ,error);
                    if (res != DW_DLV_OK) {
                        return res;
                    }
                    sum_bytes += writelen;
                }
            }
            if (curline->dpl_file != prevline->dpl_file) {
                db = DW_LNS_set_file;
                res = write_opcode_uval(db,dbg,
                    elfsectno,
                        curline->dpl_file,&writelen ,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                prevline->dpl_file = curline->dpl_file;
            }
            if (curline->dpl_column != prevline->dpl_column) {
                db = DW_LNS_set_column;
                res = write_opcode_uval(db,dbg,
                    elfsectno, curline->dpl_column , &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;
                prevline->dpl_column = curline->dpl_column;
            }
            if (curline->dpl_is_stmt != prevline->dpl_is_stmt) {
                res = write_ubyte(DW_LNS_negate_stmt,dbg,elfsectno,
                    &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;
                prevline->dpl_is_stmt = curline->dpl_is_stmt;
            }
            if (curline->dpl_basic_block == true &&
                prevline->dpl_basic_block == false) {
                res = write_ubyte(DW_LNS_set_basic_block,dbg,
                    elfsectno,&writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;
                prevline->dpl_basic_block = curline->dpl_basic_block;
            }
            if (curline->dpl_discriminator) {
                /*  This is dwarf4, but because it is an extended op
                    not a standard op,
                    we allow it without testing version.
                    GNU seems to set this from time to time. */
                unsigned val_len = 0;
                /* first null byte */
                db = 0;
                res = write_ubyte(db,dbg,elfsectno,&writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* Write len of opcode + value here. */
                res = pretend_write_uval(curline->dpl_discriminator,
                    dbg, &val_len,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                val_len ++;
                res = write_uval(val_len +1,dbg,elfsectno,
                    &writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* Write opcode */
                res = write_ubyte(DW_LNE_set_discriminator,
                    dbg,elfsectno,&writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;

                /* Write the value itself. */
                res = write_uval(curline->dpl_discriminator,
                    dbg,elfsectno,&writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;
            }

            addr_adv = curline->dpl_address - prevline->dpl_address;

            line_adv = (int) (curline->dpl_line - prevline->dpl_line);
            if ((addr_adv % MIN_INST_LENGTH) != 0) {
                DWARF_P_DBG_ERROR(dbg, DW_DLE_WRONG_ADDRESS, DW_DLV_ERROR);
            }
            opc = _dwarf_pro_get_opc(inits,addr_adv, line_adv);
            if (opc > 0) {
                /* Use special opcode. */
                no_lns_copy = 1;
                res = write_ubyte(opc,dbg,elfsectno,&writelen,error);
                if (res != DW_DLV_OK) {
                    return res;
                }
                sum_bytes += writelen;
                prevline->dpl_basic_block = false;
                prevline->dpl_address = curline->dpl_address;
                prevline->dpl_line = curline->dpl_line;
            } else {
                /*  opc says use standard opcodes. */
                if (addr_adv > 0) {
                    db = DW_LNS_advance_pc;
                    res = write_opcode_uval(db,dbg,
                        elfsectno,
                        addr_adv/inits->pi_minimum_instruction_length,
                        &writelen,
                        error);
                    if (res != DW_DLV_OK) {
                        return res;
                    }
                    sum_bytes += writelen;
                    prevline->dpl_basic_block = false;
                    prevline->dpl_address = curline->dpl_address;
                }
                if (line_adv != 0) {
                    db = DW_LNS_advance_line;
                    res = write_ubyte(db,dbg,
                        elfsectno,
                        &writelen,
                        error);
                    sum_bytes += writelen;
                    res = write_sval(line_adv,dbg,
                        elfsectno,
                        &writelen,
                        error);
                    if (res != DW_DLV_OK) {
                        return res;
                    }
                    sum_bytes += writelen;
                    prevline->dpl_basic_block = false;
                    prevline->dpl_line = curline->dpl_line;
                }
            }
        }   /* ends else for opc <= 0 */
        if (no_lns_copy == 0) { /* if not a special or dw_lne_end_seq
            generate a matrix line */
            unsigned writelen = 0;
            res = write_ubyte(DW_LNS_copy,dbg,elfsectno,&writelen,error);
            if (res != DW_DLV_OK) {
                return res;
            }
            sum_bytes += writelen;
            prevline->dpl_basic_block = false;
        }
        curline = curline->dpl_next;
    }

    /* write total length field */
    du = sum_bytes - OFFSET_PLUS_EXTENSION_SIZE;
    {
        start_line_sec += extension_size;
        WRITE_UNALIGNED(dbg, (void *) start_line_sec,
            (const void *) &du, sizeof(du), offset_size);
    }

    *nbufs = dbg->de_n_debug_sect;
    return DW_DLV_OK;
}

/*
    Generate debug_frame section  */
static int
_dwarf_pro_generate_debugframe(Dwarf_P_Debug dbg,
    Dwarf_Signed *nbufs,
    Dwarf_Error * error)
{
    int elfsectno = 0;
    int i = 0;
    int firsttime = 1;
    Dwarf_P_Cie curcie = 0;
    Dwarf_P_Fde curfde = 0;
    unsigned char *data = 0;
    Dwarf_sfixed dsw = 0;
    Dwarf_Unsigned du = 0;
    Dwarf_Ubyte db = 0;
    long *cie_offs = 0;   /* Holds byte offsets for links to fde's */
    unsigned long cie_length = 0;
    int cie_no = 0;
    Dwarf_Ubyte offset_size = dbg->de_offset_size;
    Dwarf_Ubyte extension_size = dbg->de_64bit_extension ? 4 : 0;
    Dwarf_Ubyte address_size = dbg->de_pointer_size;
    Dwarf_Unsigned cur_off = 0; /* current offset of written data, held
        for relocation info */

    elfsectno = dbg->de_elf_sects[DEBUG_FRAME];

    curcie = dbg->de_frame_cies;
    cie_length = 0;
    cur_off = 0;
    cie_offs = (long *)
        _dwarf_p_get_alloc(dbg, sizeof(long) * dbg->de_n_cie);
    if (cie_offs == NULL) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_OFFS_ALLOC, DW_DLV_ERROR);
    }
    /*  Generate cie number as we go along.  This writes
        all CIEs first before any FDEs, which is rather
        different from the order a compiler might like (which
        might be each CIE followed by its FDEs then the next CIE, and
        so on). */
    cie_no = 1;
    while (curcie) {
        char *code_al = 0;
        int codeal_bytes = 0;
        char *data_al = 0;
        int data_align_bytes = 0;
        int pad = 0;     /* Pad for padding to align cies and fdes */
        int res = 0;
        char buff1[ENCODE_SPACE_NEEDED];
        char buff2[ENCODE_SPACE_NEEDED];
        char buff3[ENCODE_SPACE_NEEDED];
        char *augmentation = 0;
        char *augmented_al = 0;
        long augmented_fields_length = 0;
        int irix_auglen_v0 = 0;
        Dwarf_Half version = curcie->cie_version;

        res = _dwarf_pro_encode_leb128_nm(curcie->cie_code_align,
            &codeal_bytes,
            buff1, sizeof(buff1));
        if (res != DW_DLV_OK) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_OFFS_ALLOC, DW_DLV_ERROR);
        }
        /*  Before April 1999, the following was using an unsigned
            encode. That worked ok even though the decoder used the
            correct signed leb read, but doing the encode correctly
            (according to the dwarf spec) saves space in the output file
            and is completely compatible.

            Note the actual stored amount on MIPS was 10 bytes (!) to
            store the value -4. (hex)fc ffffffff ffffffff 01 The
            libdwarf consumer consumed all 10 bytes too!

            old version res =
            _dwarf_pro_encode_leb128_nm(curcie->cie_data_align,

            below is corrected signed version. */
        res = _dwarf_pro_encode_signed_leb128_nm(curcie->cie_data_align,
            &data_align_bytes,
            buff2, sizeof(buff2));
        if (res != DW_DLV_OK) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_OFFS_ALLOC, DW_DLV_ERROR);
        }
        code_al = buff1;
        data_al = buff2;

        /* get the correct offset */
        if (firsttime) {
            cie_offs[cie_no - 1] = 0;
            firsttime = 0;
        } else {
            cie_offs[cie_no - 1] = cie_offs[cie_no - 2] +
                (long) cie_length + OFFSET_PLUS_EXTENSION_SIZE;
        }
        cie_no++;
        augmentation = curcie->cie_aug;
        cie_length = offset_size +  /* cie_id */
            sizeof(Dwarf_Ubyte) +   /* cie version */
            strlen(curcie->cie_aug) + 1 +   /* augmentation */
            codeal_bytes +       /* code alignment factor */
            data_align_bytes +       /* data alignment factor */
            sizeof(Dwarf_Ubyte) +   /* return reg address */
            curcie->cie_inst_bytes;
        if (dbg->de_irix_exc_augmentation &&
            (strcmp(augmentation, DW_CIE_AUGMENTER_STRING_V0) == 0)) {

            /* IRIX specific. */
            augmented_fields_length = 0;
            res = _dwarf_pro_encode_leb128_nm(augmented_fields_length,
                &irix_auglen_v0, buff3,
                sizeof(buff3));
            augmented_al = buff3;
            if (res != DW_DLV_OK) {
                DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_OFFS_ALLOC,
                    DW_DLV_ERROR);
            }
            cie_length += irix_auglen_v0 ;       /* augmentation length */
        }
        if (version >= 4) {
            /* address size, segment selector size */
            cie_length += 1 +1;
        }

        pad = (int) PADDING(cie_length, address_size);
        cie_length += pad;

        /* Now we have the cie length with padding,
            allocate a buffer for that plus the header
            length. */
        GET_CHUNK_ERR(dbg, elfsectno, data, cie_length +
            OFFSET_PLUS_EXTENSION_SIZE,
            error);
        if (extension_size) {
            Dwarf_Unsigned x = DISTINGUISHED_VALUE;

            WRITE_UNALIGNED(dbg, (void *) data,
                (const void *) &x,
                sizeof(x), extension_size);
            data += extension_size;

        }
        du = cie_length;
        /* total length of cie */
        WRITE_UNALIGNED(dbg, (void *) data,
            (const void *) &du, sizeof(du), offset_size);
        data += offset_size;

        /* cie-id is a special value. */
        du = DW_CIE_ID;
        WRITE_UNALIGNED(dbg, (void *) data, (const void *) &du,
            sizeof(du), offset_size);
        data += offset_size;

        db = curcie->cie_version;
        WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
            sizeof(db), sizeof(Dwarf_Ubyte));
        data += sizeof(Dwarf_Ubyte);

        strcpy((char *) data, curcie->cie_aug);
        data += strlen(curcie->cie_aug) + 1;

        if (curcie->cie_version >= 4) {
            /* emit address-size, segment selector size */
            db = dbg->de_pointer_size;
            WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
                sizeof(db), sizeof(Dwarf_Ubyte));
            data += sizeof(Dwarf_Ubyte);

            db = dbg->de_segment_selector_size;
            WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
                sizeof(db), sizeof(Dwarf_Ubyte));
            data += sizeof(Dwarf_Ubyte);
        }


        memcpy((void *) data, (const void *) code_al, codeal_bytes);
        data += codeal_bytes;

        memcpy((void *) data, (const void *) data_al, data_align_bytes);
        data += data_align_bytes;

        db = curcie->cie_ret_reg;
        WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
            sizeof(db), sizeof(Dwarf_Ubyte));
        data += sizeof(Dwarf_Ubyte);

        if (dbg->de_irix_exc_augmentation &&
            strcmp(augmentation, DW_CIE_AUGMENTER_STRING_V0) == 0) {

            /* IRIX only */
            memcpy((void *) data, (const void *) augmented_al,
                irix_auglen_v0);
            data += irix_auglen_v0;
        }
        memcpy((void *) data, (const void *) curcie->cie_inst,
            curcie->cie_inst_bytes);
        data += curcie->cie_inst_bytes;

        for (i = 0; i < pad; i++) {
            *data = DW_CFA_nop;
            data++;
        }
        curcie = curcie->cie_next;
    }
    /* calculate current offset */
    cur_off = cie_offs[cie_no - 2] + cie_length +
        OFFSET_PLUS_EXTENSION_SIZE;

    /* write out fde's */
    curfde = dbg->de_frame_fdes;
    while (curfde) {
        Dwarf_P_Frame_Pgm curinst = 0;
        long fde_length = 0;
        int pad2 = 0;
        Dwarf_P_Cie cie_ptr = 0;
        Dwarf_Word cie_index = 0;
        /* index is a global in string.h, so don't name anything index. */
        Dwarf_Word indx = 0;
        int oet_length = 0;
        int afl_length = 0;
        int res = 0;
        int v0_augmentation = 0;
        char afl_buff[ENCODE_SPACE_NEEDED];

        /* Find the CIE associated with this fde. */
        cie_ptr = dbg->de_frame_cies;
        cie_index = curfde->fde_cie;
        indx = 1; /* The cie_index of the first cie is 1, not 0. */
        while (cie_ptr && indx < cie_index) {
            cie_ptr = cie_ptr->cie_next;
            indx++;
        }
        if (cie_ptr == NULL) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_NULL, DW_DLV_ERROR);
        }

        fde_length = curfde->fde_n_bytes +
            OFFSET_PLUS_EXTENSION_SIZE +
            /* cie pointer */
            address_size + /* initial loc */
            address_size;  /* address range */
        if (dbg->de_irix_exc_augmentation &&
            strcmp(cie_ptr->cie_aug, DW_CIE_AUGMENTER_STRING_V0) == 0) {

            v0_augmentation = 1;
            oet_length = sizeof(Dwarf_sfixed);
            /* encode the length of augmented fields. */
            res = _dwarf_pro_encode_leb128_nm(oet_length,
                &afl_length, afl_buff,
                sizeof(afl_buff));
            if (res != DW_DLV_OK) {
                DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_OFFS_ALLOC,
                    DW_DLV_ERROR);
            }

            fde_length +=
                afl_length +    /* augmented field length */
                oet_length;     /* exception_table offset */
        }

        if (curfde->fde_die) {
            /*  IRIX/MIPS extension:
                Using fde offset, generate DW_AT_MIPS_fde attribute for the
                die corresponding to this fde.  */
            if (_dwarf_pro_add_AT_fde(dbg, curfde->fde_die, cur_off,
                error) < 0) {
                return DW_DLV_ERROR;
            }
        }

        /* store relocation for cie pointer */

        res = dbg->de_relocate_by_name_symbol(dbg,
            DEBUG_FRAME, cur_off +
            OFFSET_PLUS_EXTENSION_SIZE /* r_offset */,
            dbg->de_sect_name_idx[DEBUG_FRAME],
            dwarf_drt_data_reloc, offset_size);
        if (res != DW_DLV_OK) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_CHUNK_ALLOC, res );
        }

        /* store relocation information for initial location */
        res = dbg->de_relocate_by_name_symbol(dbg,
            DEBUG_FRAME,
            cur_off + OFFSET_PLUS_EXTENSION_SIZE +
                address_size /* r_offset */,
            curfde->fde_r_symidx,
            dwarf_drt_data_reloc, address_size);
        if (res != DW_DLV_OK) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_CHUNK_ALLOC, res);
        }
        /*  Store the relocation information for the
            offset_into_exception_info field, if the offset is valid (0
            is a valid offset). */
        if (v0_augmentation &&
            curfde->fde_offset_into_exception_tables >= 0) {

            res = dbg->de_relocate_by_name_symbol(dbg,
                DEBUG_FRAME,
                /* r_offset, where in cie this field starts */
                cur_off + OFFSET_PLUS_EXTENSION_SIZE +
                    offset_size + 2 * address_size +
                    afl_length,
                curfde->fde_exception_table_symbol,
                dwarf_drt_segment_rel,
                sizeof(Dwarf_sfixed));
            if (res != DW_DLV_OK) {
                DWARF_P_DBG_ERROR(dbg, DW_DLE_CHUNK_ALLOC, res);
            }
        }

        /* adjust for padding */
        pad2 = (int) PADDING(fde_length, address_size);
        fde_length += pad2;


        /* write out fde */
        GET_CHUNK(dbg, elfsectno, data, fde_length +
            OFFSET_PLUS_EXTENSION_SIZE,
            error);
        du = fde_length;
        {
            if (extension_size) {
                Dwarf_Word x = DISTINGUISHED_VALUE;

                WRITE_UNALIGNED(dbg, (void *) data,
                    (const void *) &x,
                    sizeof(x), extension_size);
                data += extension_size;
            }
            /* length */
            WRITE_UNALIGNED(dbg, (void *) data,
                (const void *) &du,
                sizeof(du), offset_size);
            data += offset_size;

            /* offset to cie */
            du = cie_offs[curfde->fde_cie - 1];
            WRITE_UNALIGNED(dbg, (void *) data,
                (const void *) &du,
                sizeof(du), offset_size);
            data += offset_size;

            du = curfde->fde_initloc;
            WRITE_UNALIGNED(dbg, (void *) data,
                (const void *) &du,
                sizeof(du), address_size);
            data += address_size;

            if (dbg->de_relocate_pair_by_symbol &&
                curfde->fde_end_symbol != 0 &&
                curfde->fde_addr_range == 0) {
                /*  symbolic reloc, need reloc for length What if we
                    really know the length? If so, should use the other
                    part of 'if'. */
                Dwarf_Unsigned val;

                res = dbg->de_relocate_pair_by_symbol(dbg,
                    DEBUG_FRAME,
                    cur_off + 2 * offset_size + address_size,
                    /* r_offset */ curfde->fde_r_symidx,
                    curfde->fde_end_symbol,
                    dwarf_drt_first_of_length_pair,
                    address_size);
                if (res != DW_DLV_OK) {
                    {
                        _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
                        return DW_DLV_ERROR;
                    }
                }

                /*  arrange pre-calc so assem text can do .word end -
                    begin + val (gets val from stream) */
                val = curfde->fde_end_symbol_offset -
                    curfde->fde_initloc;
                WRITE_UNALIGNED(dbg, data,
                    (const void *) &val,
                    sizeof(val), address_size);
                data += address_size;
            } else {

                du = curfde->fde_addr_range;
                WRITE_UNALIGNED(dbg, (void *) data,
                    (const void *) &du,
                    sizeof(du), address_size);
                data += address_size;
            }
        }

        if (v0_augmentation) {
            /* IRIX only. */
            /* write the encoded augmented field length. */
            memcpy((void *) data, (const void *) afl_buff, afl_length);
            data += afl_length;
            /* write the offset_into_exception_tables field. */
            dsw =
                (Dwarf_sfixed) curfde->fde_offset_into_exception_tables;
            WRITE_UNALIGNED(dbg, (void *) data, (const void *) &dsw,
                sizeof(dsw), sizeof(Dwarf_sfixed));
            data += sizeof(Dwarf_sfixed);
        }

        curinst = curfde->fde_inst;
        if (curfde->fde_block) {
            unsigned long size = curfde->fde_inst_block_size;
            memcpy((void *) data, (const void *) curfde->fde_block, size);
            data += size;
        } else {
            while (curinst) {
                db = curinst->dfp_opcode;
                WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
                    sizeof(db), sizeof(Dwarf_Ubyte));
                data += sizeof(Dwarf_Ubyte);
                memcpy((void *) data,
                    (const void *) curinst->dfp_args,
                    curinst->dfp_nbytes);
                data += curinst->dfp_nbytes;
                curinst = curinst->dfp_next;
            }
        }
        /* padding */
        for (i = 0; i < pad2; i++) {
            *data = DW_CFA_nop;
            data++;
        }
        cur_off += fde_length + offset_size;
        curfde = curfde->fde_next;
    }


    *nbufs =  dbg->de_n_debug_sect;
    return DW_DLV_OK;
}

/*
  These functions remember all the markers we see along
  with the right offset in the .debug_info section so that
  we can dump them all back to the user with the section info.
*/

static int
marker_init(Dwarf_P_Debug dbg,
    unsigned count)
{
    dbg->de_marker_n_alloc = count;
    dbg->de_markers = NULL;
    if (count > 0) {
        dbg->de_markers = _dwarf_p_get_alloc(dbg,
            sizeof(struct Dwarf_P_Marker_s) * dbg->de_marker_n_alloc);
        if (dbg->de_markers == NULL) {
            dbg->de_marker_n_alloc = 0;
            return -1;
        }
    }
    return 0;
}

static int
marker_add(Dwarf_P_Debug dbg,
    Dwarf_Unsigned offset,
    Dwarf_Unsigned marker)
{
    if (dbg->de_marker_n_alloc >= (dbg->de_marker_n_used + 1)) {
        unsigned n = dbg->de_marker_n_used++;
        dbg->de_markers[n].ma_offset = offset;
        dbg->de_markers[n].ma_marker = marker;
        return 0;
    }

    return -1;
}

Dwarf_Signed
dwarf_get_die_markers(Dwarf_P_Debug dbg,
    Dwarf_P_Marker * marker_list, /* pointer to a pointer */
    Dwarf_Unsigned * marker_count,
    Dwarf_Error * error)
{
    if (marker_list == NULL || marker_count == NULL) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_IA, DW_DLV_BADADDR);
    }
    if (dbg->de_marker_n_used != dbg->de_marker_n_alloc) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_MAF, DW_DLV_BADADDR);
    }

    *marker_list = dbg->de_markers;
    *marker_count = dbg->de_marker_n_used;
    return DW_DLV_OK;
}

/*  These functions provide the offsets of DW_FORM_string
    attributes in the section section_index. These information
    will enable a producer app that is generating assembly
    text output to easily emit those attributes in ascii form
    without having to decode the byte stream.  */
static int
string_attr_init (Dwarf_P_Debug dbg,
    Dwarf_Signed section_index,
    unsigned count)
{
    Dwarf_P_Per_Sect_String_Attrs sect_sa =
        &dbg->de_sect_string_attr[section_index];

    sect_sa->sect_sa_n_alloc = count;
    sect_sa->sect_sa_list = NULL;
    if (count > 0) {
        sect_sa->sect_sa_section_number = section_index;
        sect_sa->sect_sa_list = _dwarf_p_get_alloc(dbg,
            sizeof(struct Dwarf_P_String_Attr_s) * sect_sa->sect_sa_n_alloc);
        if (sect_sa->sect_sa_list == NULL) {
            sect_sa->sect_sa_n_alloc = 0;
            return DW_DLV_ERROR;
        }
    }
    return DW_DLV_OK;
}

static int
string_attr_add (Dwarf_P_Debug dbg,
    Dwarf_Signed section_index,
    Dwarf_Unsigned offset,
    Dwarf_P_Attribute attr)
{
    Dwarf_P_Per_Sect_String_Attrs sect_sa = &dbg->de_sect_string_attr[section_index];
    if (sect_sa->sect_sa_n_alloc >= (sect_sa->sect_sa_n_used + 1)) {
        unsigned n = sect_sa->sect_sa_n_used++;
        sect_sa->sect_sa_list[n].sa_offset = offset;
        sect_sa->sect_sa_list[n].sa_nbytes = attr->ar_nbytes;
        return DW_DLV_OK;
    }

    return DW_DLV_ERROR;
}

int
dwarf_get_string_attributes_count(Dwarf_P_Debug dbg,
    Dwarf_Unsigned *
    count_of_sa_sections,
    int *drd_buffer_version,
    UNUSEDARG Dwarf_Error *error)
{
    int i = 0;
    unsigned int count = 0;

    for (i = 0; i < NUM_DEBUG_SECTIONS; ++i) {
        if (dbg->de_sect_string_attr[i].sect_sa_n_used > 0) {
            ++count;
        }
    }
    *count_of_sa_sections = (Dwarf_Unsigned) count;
    *drd_buffer_version = DWARF_DRD_BUFFER_VERSION;

    return DW_DLV_OK;
}

int
dwarf_get_string_attributes_info(Dwarf_P_Debug dbg,
    Dwarf_Signed *elf_section_index,
    Dwarf_Unsigned *sect_sa_buffer_count,
    Dwarf_P_String_Attr *sect_sa_buffer,
    UNUSEDARG Dwarf_Error *error)
{
    int i = 0;
    int next = dbg->de_sect_sa_next_to_return;

    for (i = next; i < NUM_DEBUG_SECTIONS; ++i) {
        Dwarf_P_Per_Sect_String_Attrs sect_sa = &dbg->de_sect_string_attr[i];
        if (sect_sa->sect_sa_n_used > 0) {
            dbg->de_sect_sa_next_to_return = i + 1;
            *elf_section_index = sect_sa->sect_sa_section_number;
            *sect_sa_buffer_count = sect_sa->sect_sa_n_used;
            *sect_sa_buffer = sect_sa->sect_sa_list;
            return DW_DLV_OK;
        }
    }
    return DW_DLV_NO_ENTRY;
}


static int
has_sibling_die_already(Dwarf_P_Die d)
{
    Dwarf_P_Attribute a = 0;
    for(a = d->di_attrs; a ; a = a->ar_next) {
        if(a->ar_attribute == DW_AT_sibling) {
            return TRUE;
        }
    }
    return FALSE;
}

/*  For DW_FORM_strp we need to set the symindex so we need
    to check that such applies.  */
static int
if_relocatable_string_form(Dwarf_P_Debug dbg, Dwarf_P_Attribute curattr,
    int *debug_str_reloc,
    Dwarf_Error *error)
{
    if (curattr->ar_rel_type == R_MIPS_NONE) {
        *debug_str_reloc = 0;
        return DW_DLV_OK;
    }
    if (curattr->ar_attribute_form != DW_FORM_strp) {
        _dwarf_p_error(dbg, error,DW_DLE_DEBUGSTR_UNEXPECTED_REL);
        return DW_DLV_ERROR;
    }
    if (curattr->ar_rel_type != dbg->de_offset_reloc) {
        _dwarf_p_error(dbg, error,DW_DLE_DEBUGSTR_UNEXPECTED_REL);
        return DW_DLV_ERROR;
    }
    *debug_str_reloc = 1;
    return DW_DLV_OK;
}

/*  Tries to see if given attribute and form combination
    of the attr exists in the given abbreviation.
    abbrevs and attrs are sorted in attrnum order. */
static int
_dwarf_pro_match_attr(Dwarf_P_Attribute attr,
    Dwarf_P_Abbrev abbrev, int no_attr)
{
    int i = 0;
    Dwarf_P_Attribute curatp = attr;

    for (i = 0; i < no_attr && curatp; i++,curatp = curatp->ar_next ) {
        if (curatp->ar_attribute != abbrev->abb_attrs[i] ||
            curatp->ar_attribute_form != abbrev->abb_forms[i]) {
            return 0;
        }
    }
    return 1;
}

static int
verify_ab_no_dups(struct Dwarf_Sort_Abbrev_s *sortab,
    int attrcount)
{
    int k = 0;
    unsigned preva = 0;
    struct Dwarf_Sort_Abbrev_s *ab = sortab;

    if (attrcount < 2) {
        return DW_DLV_OK;
    }
    for(k = 0; k < attrcount; ++k,++ab) {
        if (k) {
            if (preva >= ab->dsa_attr) {
                return DW_DLV_ERROR;
            }
        }
        preva = ab->dsa_attr;
    }
    return DW_DLV_OK;
}

static int
abcompare(const void *l_in, const void *r_in)
{
    struct Dwarf_Sort_Abbrev_s *l =
        (struct Dwarf_Sort_Abbrev_s *)l_in;
    struct Dwarf_Sort_Abbrev_s *r =
        (struct Dwarf_Sort_Abbrev_s *)r_in;
    if (l->dsa_attr < r->dsa_attr) {
        return -1;
    }
    if (l->dsa_attr > r->dsa_attr) {
        return +1;
    }
    /* ASSERT: This never happens in correct dwarf. */
    return 0;
}

/*  Handles abbreviations. It takes a die, searches through
    current list of abbreviations for a matching one. If it
    finds one, it returns a pointer to the abbrev through
    the ab_out pointer, and if it does not,
    it returns a new abbrev through the ab_out pointer.

    The die->die_attrs are sorted by attribute and the curabbrev
    attrs are too.

    It is up to the user of this function to
    link it up to the abbreviation head. If it is a new abbrev
    abb_idx has 0. */
static int
_dwarf_pro_getabbrev(Dwarf_P_Debug dbg,
    Dwarf_P_Die die, Dwarf_P_Abbrev head,
    Dwarf_P_Abbrev*ab_out,Dwarf_Error *error)
{
    Dwarf_P_Abbrev curabbrev = 0;
    Dwarf_P_Attribute curattr = 0;
    int match = 0;
    Dwarf_ufixed *forms = 0;
    Dwarf_ufixed *attrs = 0;
    int attrcount = die->di_n_attr;

    curabbrev = head;
    /*  Loop thru the currently known abbreviations needed
        to see if we can share an existing abbrev.  */
    while (curabbrev) {
        if ((die->di_tag == curabbrev->abb_tag) &&
            ((die->di_child != NULL &&
            curabbrev->abb_children == DW_CHILDREN_yes) ||
            (die->di_child == NULL &&
            curabbrev->abb_children == DW_CHILDREN_no)) &&
            (attrcount == curabbrev->abb_n_attr)) {

            /*  There is a chance of a match, basic
                characterists match. Now Check the attrs and
                forms. */
            curattr = die->di_attrs;
            match = _dwarf_pro_match_attr(curattr,
                curabbrev,
                (int) curabbrev->abb_n_attr);
            if (match == 1) {
                /*  This tag/children/abbrev-list matches
                    the incoming die needs exactly. Reuse
                    this abbreviation. */
                *ab_out = curabbrev;
                return DW_DLV_OK;
            }
        }
        curabbrev = curabbrev->abb_next;
    }
    /* no match, create new abbreviation */
    if (attrcount) {
        forms = (Dwarf_ufixed *)
            _dwarf_p_get_alloc(die->di_dbg,
                sizeof(Dwarf_ufixed) * attrcount);
        if (forms == NULL) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
        }
        attrs = (Dwarf_ufixed *)
            _dwarf_p_get_alloc(die->di_dbg,
                sizeof(Dwarf_ufixed) * attrcount);
        if (attrs == NULL) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
        }
    }
    curattr = die->di_attrs;
    if (forms && attrs && attrcount) {
        struct Dwarf_Sort_Abbrev_s *sortab = 0;
        struct Dwarf_Sort_Abbrev_s *ap = 0;
        int k = 0;
        int res = 0;

        sortab = (struct Dwarf_Sort_Abbrev_s *)
            malloc(sizeof(struct Dwarf_Sort_Abbrev_s)*attrcount);
        if(!sortab) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
        }
        /*  ASSERT curattr->ar_next chain length == attrcount  */
        ap = sortab;
        for(; curattr; ++ap, curattr = curattr->ar_next) {
            ap->dsa_attr = curattr->ar_attribute;
            ap->dsa_form = curattr->ar_attribute_form;
            ap->dsa_attrp = 0;
        }

        qsort(sortab,attrcount,sizeof(struct Dwarf_Sort_Abbrev_s),
            abcompare);
        ap = sortab;
        k = 0;
        res = verify_ab_no_dups(sortab,attrcount);
        if (res != DW_DLV_OK) {
            DWARF_P_DBG_ERROR(dbg,DW_DLE_DUP_ATTR_ON_DIE,DW_DLV_ERROR);
        }
        for( ; k < attrcount; ++k,++ap) {
            attrs[k] = ap->dsa_attr;
            forms[k] = ap->dsa_form;
        }
        free(sortab);
    }

    curabbrev = (Dwarf_P_Abbrev)
        _dwarf_p_get_alloc(die->di_dbg, sizeof(struct Dwarf_P_Abbrev_s));
    if (curabbrev == NULL) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
    }

    if (die->di_child == NULL) {
        curabbrev->abb_children = DW_CHILDREN_no;
    } else {
        curabbrev->abb_children = DW_CHILDREN_yes;
    }
    curabbrev->abb_tag = die->di_tag;
    curabbrev->abb_attrs = attrs;
    curabbrev->abb_forms = forms;
    curabbrev->abb_n_attr = attrcount;
    curabbrev->abb_idx = 0;
    curabbrev->abb_next = NULL;
    *ab_out = curabbrev;
    return DW_DLV_OK;
}

/* Generate debug_info and debug_abbrev sections */


/*  DWARF 2,3,4  */
static int
generate_debuginfo_header_2(Dwarf_P_Debug dbg,
    unsigned *abbrev_offset_io,
    unsigned char **data_io,
    int     *cu_header_size_out,
    Dwarf_Small **abbr_off_ptr_out,
    Dwarf_Half version,
    int extension_size,
    Dwarf_Ubyte address_size,
    Dwarf_Error * error)
{
    unsigned abbrev_offset = *abbrev_offset_io;
    unsigned char * data = *data_io;
    int offset_size = dbg->de_offset_size;
    int elfsectno_of_debug_info = dbg->de_elf_sects[DEBUG_INFO];
    int cu_header_size = 0;
    Dwarf_Unsigned du = 0;
    Dwarf_Small *abbr_off_ptr = 0;

    /* write cu header. abbrev_offset used to
        generate relocation record below */
    abbrev_offset =  OFFSET_PLUS_EXTENSION_SIZE +
        DWARF_HALF_SIZE  ;

    cu_header_size = abbrev_offset +
        offset_size + sizeof(Dwarf_Ubyte);

    GET_CHUNK_ERR(dbg, elfsectno_of_debug_info, data, cu_header_size,
        error);
    if (extension_size) {
        /* This for a dwarf-standard 64bit offset. */
        du = DISTINGUISHED_VALUE;
        WRITE_UNALIGNED(dbg, (void *) data,
            (const void *) &du, sizeof(du), extension_size);
        data += extension_size;
    }
    abbr_off_ptr = data;
    du = 0; /* length of debug_info, not counting
        this field itself (unknown at this point). */
    WRITE_UNALIGNED(dbg, (void *) data,
        (const void *) &du, sizeof(du), offset_size);
    data += offset_size;

    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &version,
        sizeof(version), DWARF_HALF_SIZE);
    data += DWARF_HALF_SIZE;

    du = 0;/* offset into abbrev table, not yet known. */
    WRITE_UNALIGNED(dbg, (void *) data,
        (const void *) &du, sizeof(du), offset_size);
    data += offset_size;

    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &address_size,
        sizeof(address_size), sizeof(Dwarf_Ubyte));
    data += sizeof(Dwarf_Ubyte);

    /*  We have filled the chunk we got with GET_CHUNK.
        At this point we
        no longer dare use "data"  as a
        pointer any longer except to refer to that first
        small chunk for the cu header to update
        the section length. */
    *abbrev_offset_io = abbrev_offset;
    *data_io = data;
    *cu_header_size_out = cu_header_size;
    *abbr_off_ptr_out = abbr_off_ptr;
    return DW_DLV_OK;
}

/*  DWARF 5 */
static int
generate_debuginfo_header_5(Dwarf_P_Debug dbg,
    unsigned       *abbrev_offset_io,
    unsigned char **data_io,
    int            *cu_header_size_out,
    Dwarf_Small   **abbr_off_ptr_out,
    Dwarf_Half      version,
    Dwarf_Ubyte     unit_type,
    int             extension_size,
    Dwarf_Ubyte     address_size,
    Dwarf_Error    *error)
{
    int offset_size = dbg->de_offset_size;
    unsigned abbrev_offset = *abbrev_offset_io;
    unsigned char * data = *data_io;
    int elfsectno_of_debug_info = dbg->de_elf_sects[DEBUG_INFO];
    int cu_header_size = 0;
    Dwarf_Unsigned du = 0;
    Dwarf_Small *abbr_off_ptr = 0;


    /*  write cu header. abbrev_offset used to
        generate relocation record below */
    abbrev_offset =  OFFSET_PLUS_EXTENSION_SIZE +
        DWARF_HALF_SIZE + /* version stamp */
        sizeof(unit_type) +
        sizeof(Dwarf_Ubyte);
    cu_header_size = abbrev_offset + offset_size;

    GET_CHUNK_ERR(dbg, elfsectno_of_debug_info, data, cu_header_size,
        error);
    if (extension_size) {
        /* Impossible in DW5, really, is for IRIX64. But we allow it. */
        du = DISTINGUISHED_VALUE;
        WRITE_UNALIGNED(dbg, (void *) data,
            (const void *) &du, sizeof(du), extension_size);
        data += extension_size;
    }
    abbr_off_ptr = data;
    du = 0; /* length of debug_info, not counting
        this field itself (unknown at this point). */
    WRITE_UNALIGNED(dbg, (void *) data,
        (const void *) &du, sizeof(du), offset_size);
    data += offset_size;

    WRITE_UNALIGNED(dbg, (void *) data,
        (const void *) &version,
        sizeof(version), DWARF_HALF_SIZE);
    data += DWARF_HALF_SIZE;

    WRITE_UNALIGNED(dbg, (void *) data,
        (const void *) &unit_type,
        sizeof(unit_type), sizeof(Dwarf_Ubyte));
    data += sizeof(Dwarf_Ubyte);

    WRITE_UNALIGNED(dbg, (void *) data, (const void *) &address_size,
        sizeof(address_size), sizeof(Dwarf_Ubyte));
    data += sizeof(Dwarf_Ubyte);

    du = 0;/* offset into abbrev table, not yet known. */
    WRITE_UNALIGNED(dbg, (void *) data,
        (const void *) &du, sizeof(du), offset_size);
    data += offset_size;

    /*  We have filled the chunk we got with GET_CHUNK.
        At this point we
        no longer dare use "data" as a pointer any
        longer except to refer to that first small chunk for the cu
        header to update the section length. */

    *abbrev_offset_io = abbrev_offset;
    *data_io = data;
    *cu_header_size_out = cu_header_size;
    *abbr_off_ptr_out = abbr_off_ptr;
    return DW_DLV_OK;
}

/* Write out debug_abbrev section */
static int
write_out_debug_abbrev(Dwarf_P_Debug dbg,
    Dwarf_P_Abbrev abbrev_head,
    Dwarf_Error * error)
{
    Dwarf_P_Abbrev curabbrev = abbrev_head;
    unsigned char *data = 0;
    int res = 0;
    int abbrevsectno = dbg->de_elf_sects[DEBUG_ABBREV];

    while (curabbrev) {
        int idx = 0;
        unsigned lebcount = 0;
        Dwarf_Ubyte db = 0;

        res  = write_uval(curabbrev->abb_idx,dbg,abbrevsectno,
            &lebcount,error);
        if (res != DW_DLV_OK) {
            return res;
        }

        res  = write_uval(curabbrev->abb_tag,dbg,abbrevsectno,
            &lebcount,error);
        if (res != DW_DLV_OK) {
            return res;
        }

        db = curabbrev->abb_children;
        res = write_ubyte(db,dbg,abbrevsectno,&lebcount,error);
        if (res != DW_DLV_OK) {
            return res;
        }

        /* add attributes and forms */
        for (idx = 0; idx < curabbrev->abb_n_attr; idx++) {
            res =write_uval(curabbrev->abb_attrs[idx],
                dbg,abbrevsectno,
                &lebcount,error);
            if (res != DW_DLV_OK) {
                return res;
            }
            res =write_uval(curabbrev->abb_forms[idx],
                dbg,abbrevsectno,
                &lebcount,error);
            if (res != DW_DLV_OK) {
                return res;
            }
        }
        /* Two zeros, for last entry, see dwarf2 sec 7.5.3 */
        GET_CHUNK_ERR(dbg, abbrevsectno, data, 2, error);
        *data = 0;
        data++;
        *data = 0;

        curabbrev = curabbrev->abb_next;
    }
    /* one zero, for end of cu, see dwarf2 sec 7.5.3 */
    GET_CHUNK_ERR(dbg, abbrevsectno, data, 1, error);
    *data = 0;
    return DW_DLV_OK;
}

static int
sort_die_attrs(Dwarf_P_Debug dbg,Dwarf_P_Die die,
    Dwarf_Error *error)
{
    struct Dwarf_Sort_Abbrev_s *sortab = 0;
    struct Dwarf_Sort_Abbrev_s *ap = 0;
    Dwarf_P_Attribute at = 0;
    Dwarf_P_Attribute sorted_attrlist = 0;
    Dwarf_P_Attribute sorted_tail = 0;
    int attrcount = die->di_n_attr;
    int res = 0;
    unsigned ct = 0;

    int k = 0;

    if (attrcount < 2) {
        return DW_DLV_OK;
    }

    sortab = (struct Dwarf_Sort_Abbrev_s *)
        malloc(sizeof(struct Dwarf_Sort_Abbrev_s)*attrcount);
    if(!sortab) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
    }
    /*  ASSERT at->ar_next chain length == attrcount  */
    ap = sortab;
    at = die->di_attrs;
    for(; at; ++ap, at = at->ar_next) {
        ap->dsa_attr = at->ar_attribute;
        ap->dsa_form = at->ar_attribute_form;
        ap->dsa_attrp = at;
        ++ct;
    }
    qsort(sortab,attrcount,sizeof(struct Dwarf_Sort_Abbrev_s),
        abcompare);
    res = verify_ab_no_dups(sortab,attrcount);
    if (res != DW_DLV_OK) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_DUP_ATTR_ON_DIE, DW_DLV_ERROR);
    }
    ap = sortab;
    k = 0;
    for( ; k < attrcount; ++k,++ap) {
        Dwarf_P_Attribute localptr = ap->dsa_attrp;
        if (!sorted_attrlist) {
            sorted_attrlist = localptr;
            sorted_tail = sorted_attrlist;
            localptr->ar_next = 0;
            continue;
        }
        sorted_tail->ar_next  = localptr;
        sorted_tail = localptr;
        localptr->ar_next = 0;
    }
    /*  Now replace the list with the same pointers
        but in order sorted by attribute. */
    die->di_attrs = sorted_attrlist;
    free(sortab);
    return DW_DLV_OK;
}



static int
_dwarf_pro_generate_debuginfo(Dwarf_P_Debug dbg,
    Dwarf_Signed *nbufs,
    Dwarf_Error * error)
{
    int elfsectno_of_debug_info = 0;
    unsigned char *data = 0;
    int cu_header_size = 0;
    Dwarf_P_Abbrev curabbrev = 0;
    Dwarf_P_Abbrev abbrev_head = 0;
    Dwarf_P_Abbrev abbrev_tail = 0;
    Dwarf_P_Die curdie = 0;
    Dwarf_P_Die first_child = 0;
    Dwarf_Word dw = 0;
    Dwarf_Unsigned du = 0;
    Dwarf_Half dh = 0;
    Dwarf_Unsigned die_off = 0; /* Offset of die in debug_info. */
    int n_abbrevs = 0;
    unsigned  abbrev_offset = 0;
    int res = 0;
    unsigned marker_count = 0;
    unsigned string_attr_count = 0;
    unsigned string_attr_offset = 0;
    Dwarf_Small *abbr_off_ptr = 0;

    int offset_size = dbg->de_offset_size;
    /*  extension_size is oddly names. The standard calls
        for a 64bit offset to have a 4 byte 0xffff
        while original IRIX64 did not.
        So if dbg->de_64bit_extension set this is a standard
        DWARF 64bit offset and if de_64bit_extension not set
        this is non-standard IRIX64 64 bit offset. */
    Dwarf_Half version = dbg->de_output_version;
    int extension_size = dbg->de_64bit_extension ? 4 : 0;

    /* For now just assume DW_UT_compile FIXME */
    Dwarf_Ubyte unit_type = DW_UT_compile;
    Dwarf_Ubyte address_size = 0;

    elfsectno_of_debug_info = dbg->de_elf_sects[DEBUG_INFO];
    address_size = dbg->de_pointer_size;
    if (version  < 5) {
        res = generate_debuginfo_header_2(dbg,
            &abbrev_offset,
            &data,
            &cu_header_size,
            &abbr_off_ptr,
            version,  extension_size, address_size,
            error);
        if (res != DW_DLV_OK) {
            return res;
        }
    } else if (version == 5) {
        res = generate_debuginfo_header_5(dbg,
            &abbrev_offset,
            &data,
            &cu_header_size,
            &abbr_off_ptr,
            version, unit_type, extension_size, address_size,
            error);
        if (res != DW_DLV_OK) {
            return res;
        }
    } else {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_VERSION_STAMP_ERROR,
            DW_DLV_ERROR);
    }

    curdie = dbg->de_dies;

    /*  Create AT_macro_info if appropriate */
    if( version < 5) {
        if (dbg->de_first_macinfo != NULL) {
            res = _dwarf_pro_add_AT_macro_info(dbg, curdie, 0, error);
            if (res != DW_DLV_OK) {
                return res;
            }
        }
    } else {
        /* FIXME need to add code to emit DWARF5 macro data. */
#if 0
            res = _dwarf_pro_add_AT_macro5_info(dbg, curdie, 0, error);
#endif
    }

    /* Create AT_stmt_list attribute if necessary */
    if (dwarf_need_debug_line_section(dbg) == TRUE) {
        res =_dwarf_pro_add_AT_stmt_list(dbg, curdie, error);
        if (res != DW_DLV_OK) {
            return res;
        }
    }
    die_off = cu_header_size;

    /*  Relocation for abbrev offset in cu header store relocation
        record in linked list */
    res = dbg->de_relocate_by_name_symbol(dbg,
        DEBUG_INFO,
        abbrev_offset /* r_offset */,
        dbg->de_sect_name_idx[DEBUG_ABBREV],
        dwarf_drt_data_reloc, offset_size);
    if (res != DW_DLV_OK) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC, DW_DLV_ERROR);
    }

    /*  Pass 0: only top level dies, add at_sibling attribute to those
        dies with children, but if and only if
        there is no sibling attribute already. */
    first_child = curdie->di_child;
    while (first_child && first_child->di_right) {
        if (first_child->di_child) {
            if (!has_sibling_die_already(first_child)) {
                dwarf_add_AT_reference(dbg,
                    first_child,
                    DW_AT_sibling,
                    first_child->di_right, error);
            }
        }
        first_child = first_child->di_right;
    }

    /* Pass 1: create abbrev info, get die offsets, calc relocations */
    abbrev_head = abbrev_tail = NULL;
    marker_count = 0;
    string_attr_count = 0;
    while (curdie != NULL) {
        int nbytes = 0;
        Dwarf_P_Attribute curattr = 0;
        char *space = 0;
        int cres = 0;
        char buff1[ENCODE_SPACE_NEEDED];
        int i = 0;

        curdie->di_offset = die_off;

        if (curdie->di_marker != 0) {
            marker_count++;
        }
        cres  =sort_die_attrs(dbg,curdie,error);
        if (cres != DW_DLV_OK) {
            /* DW_DLV_NO_ENTRY is impossible. */
            return cres;
        }
        /*  Find or create a final abbrev record for the
            debug_abbrev section we will write (below). */
        cres  = _dwarf_pro_getabbrev(dbg,curdie, abbrev_head,&curabbrev,
            error);
        if (cres != DW_DLV_OK) {
            return cres;
        }
        if (abbrev_head == NULL) {
            n_abbrevs = 1;
            curabbrev->abb_idx = n_abbrevs;
            abbrev_tail = abbrev_head = curabbrev;
        } else {
            /* Check if it is a new abbreviation, if yes, add to tail */
            if (curabbrev->abb_idx == 0) {
                n_abbrevs++;
                curabbrev->abb_idx = n_abbrevs;
                abbrev_tail->abb_next = curabbrev;
                abbrev_tail = curabbrev;
            }
        }
        /*  We know the abbrev number to use now.
            So create the bytes of the leb with the
            value and save those bytes in di_abbrev,
            we will emit in Pass 2 (below). */
        cres = _dwarf_pro_encode_leb128_nm(curabbrev->abb_idx,
            &nbytes,
            buff1, sizeof(buff1));
        if (cres != DW_DLV_OK) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
        }
        space = _dwarf_p_get_alloc(dbg, nbytes);
        if (space == NULL) {
            DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
        }
        memcpy(space, buff1, nbytes);
        curdie->di_abbrev = space;
        curdie->di_abbrev_nbytes = nbytes;
        die_off += nbytes;

        curattr = curdie->di_attrs;
        /*  The abbrev and DIE attr lists match, so the die
            abbrevs are in the correct order,
            curdie->di_attrs.  */

        /*  Now we attach the attributes list to the die. */
        curattr = curdie->di_attrs;

        while (curattr) {
            if (curattr->ar_rel_type != R_MIPS_NONE) {
                int rres=0;
                switch (curattr->ar_attribute) {
                case DW_AT_stmt_list:
                    curattr->ar_rel_symidx =
                        dbg->de_sect_name_idx[DEBUG_LINE];
                    break;
                case DW_AT_MIPS_fde:
                    curattr->ar_rel_symidx =
                        dbg->de_sect_name_idx[DEBUG_FRAME];
                    break;
                case DW_AT_macro_info:
                    curattr->ar_rel_symidx =
                        dbg->de_sect_name_idx[DEBUG_MACINFO];
                    break;
                /* See also: pro_forms.c for same strings attribute list. */
                case DW_AT_comp_dir:
                case DW_AT_const_value:
                case DW_AT_linkage_name: /* DWARF5 */
                case DW_AT_MIPS_abstract_name:
                case DW_AT_MIPS_linkage_name:
                case DW_AT_name:
                case DW_AT_producer: {
                    int is_debug_str = 0;
                    int nres = if_relocatable_string_form(dbg,curattr,
                        &is_debug_str,error);
                    if (nres != DW_DLV_OK) {
                        return res;
                    }
                    if (is_debug_str) {
                        curattr->ar_rel_symidx =
                            dbg->de_sect_name_idx[DEBUG_STR];
                    }
                    }
                    break;
                default:
                    break;
                }
                rres = dbg->de_relocate_by_name_symbol(dbg,
                    DEBUG_INFO,
                    die_off + curattr->ar_rel_offset,/* r_offset */
                    curattr->ar_rel_symidx,
                    dwarf_drt_data_reloc,
                    curattr->ar_reloc_len);

                if (rres != DW_DLV_OK) {
                    DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC, DW_DLV_ERROR);
                }
            }
            if (curattr->ar_attribute_form == DW_FORM_string) {
                string_attr_count++;
            }
            die_off += curattr->ar_nbytes;
            curattr = curattr->ar_next;
        }
        /* Depth first access to all the DIEs. */
        if (curdie->di_child) {
            curdie = curdie->di_child;
        } else {
            while (curdie != NULL && curdie->di_right == NULL) {
                curdie = curdie->di_parent;
                die_off++; /* since we are writing a
                    null die at the end of each
                    sibling chain */
            }
            if (curdie != NULL) {
                curdie = curdie->di_right;
            }
        }

    } /* end while (curdie != NULL), the per-die loop */

    res = marker_init(dbg, marker_count);
    if (res == -1) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC, DW_DLV_ERROR);
    }
    res = string_attr_init(dbg, DEBUG_INFO, string_attr_count);
    if (res != DW_DLV_OK) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC, DW_DLV_ERROR);
    }

    /*  Pass 2: Write out the die information Here 'data' is a
        temporary, one block for each GET_CHUNK.  'data' is overused. */
    curdie = dbg->de_dies;
    while (curdie != NULL) {
        Dwarf_P_Attribute curattr;

        if (curdie->di_marker != 0) {
            res = marker_add(dbg, curdie->di_offset, curdie->di_marker);
            if (res == -1) {
                DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC, DW_DLV_ERROR);
            }
        }

        /* Index to abbreviation table */
        GET_CHUNK_ERR(dbg, elfsectno_of_debug_info,
            data, curdie->di_abbrev_nbytes, error);

        memcpy((void *) data,
            (const void *) curdie->di_abbrev,
            curdie->di_abbrev_nbytes);

        /* Attribute values - need to fill in all form attributes */
        curattr = curdie->di_attrs;
        string_attr_offset = curdie->di_offset + curdie->di_abbrev_nbytes;

        while (curattr) {
            GET_CHUNK_ERR(dbg, elfsectno_of_debug_info, data,
                (unsigned long) curattr->ar_nbytes, error);
            switch (curattr->ar_attribute_form) {
            case DW_FORM_ref1:
                {
                    Dwarf_Ubyte db = 0;
                    if (curattr->ar_ref_die->di_offset >
                        (unsigned) 0xff) {
                        DWARF_P_DBG_ERROR(dbg, DW_DLE_OFFSET_UFLW, DW_DLV_ERROR);
                    }
                    db = curattr->ar_ref_die->di_offset;
                    WRITE_UNALIGNED(dbg, (void *) data,
                        (const void *) &db,
                        sizeof(db), sizeof(Dwarf_Ubyte));
                    break;
                }
            case DW_FORM_ref2:
                {
                    if (curattr->ar_ref_die->di_offset >
                        (unsigned) 0xffff) {
                        DWARF_P_DBG_ERROR(dbg, DW_DLE_OFFSET_UFLW, DW_DLV_ERROR);
                    }
                    dh = curattr->ar_ref_die->di_offset;
                    WRITE_UNALIGNED(dbg, (void *) data,
                        (const void *) &dh,
                        sizeof(dh), DWARF_HALF_SIZE);
                    break;
                }
            case DW_FORM_ref_addr:
                {
                    /*  curattr->ar_ref_die == NULL!

                        DW_FORM_ref_addr doesn't take a CU-offset.
                        This is different than other refs.
                        This value will be set by the user of the
                        producer library using a relocation.
                        No need to set a value here.  */
                    break;

                }
            case DW_FORM_ref4:
                {
                    if (curattr->ar_ref_die->di_offset >
                        (unsigned) 0xffffffff) {
                        DWARF_P_DBG_ERROR(dbg, DW_DLE_OFFSET_UFLW,
                            DW_DLV_ERROR);
                    }
                    dw = (Dwarf_Word) curattr->ar_ref_die->di_offset;
                    WRITE_UNALIGNED(dbg, (void *) data,
                        (const void *) &dw,
                        sizeof(dw), sizeof(Dwarf_ufixed));
                    break;
                }
            case DW_FORM_ref8:
                du = curattr->ar_ref_die->di_offset;
                WRITE_UNALIGNED(dbg, (void *) data,
                    (const void *) &du,
                    sizeof(du), sizeof(Dwarf_Unsigned));
                break;
            case DW_FORM_ref_udata:
                {               /* unsigned leb128 offset */

                    int nbytes;
                    char buff1[ENCODE_SPACE_NEEDED];

                    res =
                        _dwarf_pro_encode_leb128_nm(curattr->
                            ar_ref_die->
                            di_offset, &nbytes,
                            buff1,
                            sizeof(buff1));
                    if (res != DW_DLV_OK) {
                        DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC,
                            DW_DLV_ERROR);
                    }

                    memcpy(data, buff1, nbytes);
                    break;
                }
            default:
                memcpy((void *) data,
                    (const void *) curattr->ar_data,
                    curattr->ar_nbytes);
                break;
            }
            if (curattr->ar_attribute_form == DW_FORM_string) {
                string_attr_add(dbg, DEBUG_INFO, string_attr_offset, curattr);
            }
            string_attr_offset += curattr->ar_nbytes;
            curattr = curattr->ar_next;
        }

        /* depth first search */
        if (curdie->di_child)
            curdie = curdie->di_child;
        else {
            while (curdie != NULL && curdie->di_right == NULL) {
                GET_CHUNK_ERR(dbg, elfsectno_of_debug_info, data, 1, error);
                *data = '\0';
                curdie = curdie->di_parent;
            }
            if (curdie != NULL)
                curdie = curdie->di_right;
        }
    } /* end while (curdir != NULL) */

    /*  Write out debug_info size, now that we know it
        This is back-patching the CU header we created
        above. */
    du = die_off - OFFSET_PLUS_EXTENSION_SIZE;
    WRITE_UNALIGNED(dbg, (void *) abbr_off_ptr,
        (const void *) &du, sizeof(du), offset_size);


    data = 0;                   /* Emphasise not usable now */

    res = write_out_debug_abbrev(dbg,
        abbrev_head, error);
    if (res != DW_DLV_OK) {
        return res;
    }

    *nbufs =  dbg->de_n_debug_sect;
    return DW_DLV_OK;
}

static int
_dwarf_pro_generate_debug_names(Dwarf_P_Debug dbg,
    UNUSEDARG Dwarf_Signed *nbufs,
    Dwarf_Error * error UNUSEDARG)
{
#if 0
    int elfsectno_of_debug_names =  dbg->de_elf_sects[DEBUG_NAMES];
    FIXME: Needs implementation
    unsigned char *data = 0;

    GET_CHUNK(dbg, elfsectno_of_debug_names, data,
        dbg->de_debug_names->ds_nbytes,
        error);
    memcpy(data,dbg->de_debug_names->ds_data,dbg->de_debug_names->ds_nbytes);
#endif
    *nbufs = dbg->de_n_debug_sect;
    return DW_DLV_OK;
}

static int
_dwarf_pro_generate_debug_str(Dwarf_P_Debug dbg,
    Dwarf_Signed *nbufs,
    Dwarf_Error * error)
{
    int elfsectno_of_debug_str = 0;
    unsigned char *data = 0;

    elfsectno_of_debug_str = dbg->de_elf_sects[DEBUG_STR];
    GET_CHUNK(dbg, elfsectno_of_debug_str, data, dbg->de_debug_str->ds_nbytes,
        error);
    memcpy(data,dbg->de_debug_str->ds_data,dbg->de_debug_str->ds_nbytes);
    *nbufs = dbg->de_n_debug_sect;
    return DW_DLV_OK;
}
static int
_dwarf_pro_generate_debug_line_str(Dwarf_P_Debug dbg,
    Dwarf_Signed *nbufs,
    Dwarf_Error * error)
{
    int elfsectno_of_debug_line_str = 0;
    unsigned char *data = 0;

    elfsectno_of_debug_line_str = dbg->de_elf_sects[DEBUG_LINE_STR];
    GET_CHUNK(dbg, elfsectno_of_debug_line_str, data,
        dbg->de_debug_line_str->ds_nbytes,
        error);
    memcpy(data,dbg->de_debug_line_str->ds_data,
        dbg->de_debug_line_str->ds_nbytes);
    *nbufs = dbg->de_n_debug_sect;
    return DW_DLV_OK;
}


/*  Get a buffer of section data.
    section_idx is the elf-section number that this data applies to.
    length shows length of returned data
    This is the original format. Hard to check for error. */

/*ARGSUSED*/                   /* pretend all args used */
Dwarf_Ptr
dwarf_get_section_bytes(Dwarf_P_Debug dbg,
    UNUSEDARG Dwarf_Signed dwarf_section,
    Dwarf_Signed * section_idx,
    Dwarf_Unsigned * length, Dwarf_Error * error)
{
    Dwarf_Ptr s_bytes = 0;
    int res = 0;

    res = dwarf_get_section_bytes_a(dbg,
        dwarf_section,
        section_idx,
        length,
        &s_bytes,
        error);
    if (res == DW_DLV_ERROR) {
        return (Dwarf_Ptr)DW_DLV_BADADDR;
    }
    if (res == DW_DLV_NO_ENTRY) {
        return NULL;
    }
    return s_bytes;
}

/*  Get a buffer of section data.
    section_idx is the elf-section number that this data applies to.
    length shows length of returned data
    This is the September 2016 format. Preferred. */
int
dwarf_get_section_bytes_a(Dwarf_P_Debug dbg,
    UNUSEDARG Dwarf_Signed dwarf_section,
    Dwarf_Signed   * section_idx,
    Dwarf_Unsigned * length,
    Dwarf_Ptr      * section_bytes,
    Dwarf_Error * error)
{
    Dwarf_Ptr buf = 0;

    if (dbg->de_version_magic_number != PRO_VERSION_MAGIC) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_IA, DW_DLV_ERROR);
    }
    *section_bytes = 0;
    *length = 0;
    if (dbg->de_debug_sects == 0) {
        /* no more data !! */
        return DW_DLV_NO_ENTRY;
    }
    if (dbg->de_debug_sects->ds_elf_sect_no == MAGIC_SECT_NO) {
        /* no data ever entered !! */
        return DW_DLV_NO_ENTRY;
    }
    *section_idx = dbg->de_debug_sects->ds_elf_sect_no;
    *length = dbg->de_debug_sects->ds_nbytes;

    buf = (Dwarf_Ptr *) dbg->de_debug_sects->ds_data;

    /*  Here is the iterator so the next call gets
        the next section. */
    dbg->de_debug_sects = dbg->de_debug_sects->ds_next;

    /*  We may want to call the section stuff more than once: see
        dwarf_reset_section_bytes() do not do: dbg->de_n_debug_sect--; */

    *section_bytes = buf;
    return DW_DLV_OK;
}

/* No errors possible.  */
void
dwarf_reset_section_bytes(Dwarf_P_Debug dbg)
{
    dbg->de_debug_sects = dbg->de_first_debug_sect;
    /*  No need to reset; commented out decrement. dbg->de_n_debug_sect
        = ???; */
    dbg->de_reloc_next_to_return = 0;
    dbg->de_sect_sa_next_to_return = 0;
}

/*  Storage handler. Gets either a new chunk of memory, or
    a pointer in existing memory, from the linked list attached
    to dbg at de_debug_sects, depending on size of nbytes

    Assume dbg not null, checked in top level routine

    Returns a pointer to the allocated buffer space for the
    lib to fill in,  predincrements next-to-use count so the
    space requested is already counted 'used'
    when this returns (ie, reserved).

*/
Dwarf_Small *
_dwarf_pro_buffer(Dwarf_P_Debug dbg,
    int elfsectno, unsigned long nbytes)
{
    Dwarf_P_Section_Data cursect = 0;

    cursect = dbg->de_current_active_section;
    /*  By using MAGIC_SECT_NO we allow the following MAGIC_SECT_NO must
        not match any legit section number. test to have just two
        clauses (no NULL pointer test) See dwarf_producer_init(). */
    if ((cursect->ds_elf_sect_no != elfsectno) ||
        ((cursect->ds_nbytes + nbytes) > cursect->ds_orig_alloc)
        ) {

        /*  Either the elf section has changed or there is not enough
            space in the current section.

            Create a new Dwarf_P_Section_Data_s for the chunk. and have
            space 'on the end' for the buffer itself so we just do one
            malloc (not two).  */
        unsigned long space = nbytes;

        if (nbytes < CHUNK_SIZE)
            space = CHUNK_SIZE;

        cursect = (Dwarf_P_Section_Data)
            _dwarf_p_get_alloc(dbg,
                sizeof(struct Dwarf_P_Section_Data_s)
                + space);
        if (cursect == NULL) {
            return (NULL);
        }

        /* _dwarf_p_get_alloc zeroes the space... */

        cursect->ds_data = (char *) cursect +
            sizeof(struct Dwarf_P_Section_Data_s);
        cursect->ds_orig_alloc = space;
        cursect->ds_elf_sect_no = elfsectno;
        cursect->ds_nbytes = nbytes;    /* reserve this number of bytes
            of space for caller to fill in */
        /*  Now link on the end of the list, and mark this one as the
            current one */

        if (dbg->de_debug_sects->ds_elf_sect_no == MAGIC_SECT_NO) {
            /*  The only entry is the special one for 'no entry' so
                delete that phony one while adding this initial real
                one. */
            dbg->de_debug_sects = cursect;
            dbg->de_current_active_section = cursect;
            dbg->de_first_debug_sect = cursect;
        } else {
            dbg->de_current_active_section->ds_next = cursect;
            dbg->de_current_active_section = cursect;
        }
        dbg->de_n_debug_sect++;

        return ((Dwarf_Small *) cursect->ds_data);
    }

    /* There is enough space in the current buffer */
    {
        Dwarf_Small *space_for_caller = (Dwarf_Small *)
            (cursect->ds_data + cursect->ds_nbytes);

        cursect->ds_nbytes += nbytes;
        return space_for_caller;
    }
}