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
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
|
/* $Id: dbgmoddwarf.cpp $ */
/** @file
* IPRT - Debug Info Reader For DWARF.
*/
/*
* Copyright (C) 2011-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_DBG_DWARF
#include <iprt/dbg.h>
#include "internal/iprt.h"
#include <iprt/asm.h>
#include <iprt/ctype.h>
#include <iprt/err.h>
#include <iprt/list.h>
#include <iprt/log.h>
#include <iprt/mem.h>
#define RTDBGMODDWARF_WITH_MEM_CACHE
#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
# include <iprt/memcache.h>
#endif
#include <iprt/path.h>
#include <iprt/string.h>
#include <iprt/strcache.h>
#include "internal/dbgmod.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @name Standard DWARF Line Number Opcodes
* @{ */
#define DW_LNS_extended UINT8_C(0x00)
#define DW_LNS_copy UINT8_C(0x01)
#define DW_LNS_advance_pc UINT8_C(0x02)
#define DW_LNS_advance_line UINT8_C(0x03)
#define DW_LNS_set_file UINT8_C(0x04)
#define DW_LNS_set_column UINT8_C(0x05)
#define DW_LNS_negate_stmt UINT8_C(0x06)
#define DW_LNS_set_basic_block UINT8_C(0x07)
#define DW_LNS_const_add_pc UINT8_C(0x08)
#define DW_LNS_fixed_advance_pc UINT8_C(0x09)
#define DW_LNS_set_prologue_end UINT8_C(0x0a)
#define DW_LNS_set_epilogue_begin UINT8_C(0x0b)
#define DW_LNS_set_isa UINT8_C(0x0c)
#define DW_LNS_what_question_mark UINT8_C(0x0d)
/** @} */
/** @name Extended DWARF Line Number Opcodes
* @{ */
#define DW_LNE_end_sequence UINT8_C(1)
#define DW_LNE_set_address UINT8_C(2)
#define DW_LNE_define_file UINT8_C(3)
#define DW_LNE_set_descriminator UINT8_C(4)
/** @} */
/** @name DIE Tags.
* @{ */
#define DW_TAG_array_type UINT16_C(0x0001)
#define DW_TAG_class_type UINT16_C(0x0002)
#define DW_TAG_entry_point UINT16_C(0x0003)
#define DW_TAG_enumeration_type UINT16_C(0x0004)
#define DW_TAG_formal_parameter UINT16_C(0x0005)
#define DW_TAG_imported_declaration UINT16_C(0x0008)
#define DW_TAG_label UINT16_C(0x000a)
#define DW_TAG_lexical_block UINT16_C(0x000b)
#define DW_TAG_member UINT16_C(0x000d)
#define DW_TAG_pointer_type UINT16_C(0x000f)
#define DW_TAG_reference_type UINT16_C(0x0010)
#define DW_TAG_compile_unit UINT16_C(0x0011)
#define DW_TAG_string_type UINT16_C(0x0012)
#define DW_TAG_structure_type UINT16_C(0x0013)
#define DW_TAG_subroutine_type UINT16_C(0x0015)
#define DW_TAG_typedef UINT16_C(0x0016)
#define DW_TAG_union_type UINT16_C(0x0017)
#define DW_TAG_unspecified_parameters UINT16_C(0x0018)
#define DW_TAG_variant UINT16_C(0x0019)
#define DW_TAG_common_block UINT16_C(0x001a)
#define DW_TAG_common_inclusion UINT16_C(0x001b)
#define DW_TAG_inheritance UINT16_C(0x001c)
#define DW_TAG_inlined_subroutine UINT16_C(0x001d)
#define DW_TAG_module UINT16_C(0x001e)
#define DW_TAG_ptr_to_member_type UINT16_C(0x001f)
#define DW_TAG_set_type UINT16_C(0x0020)
#define DW_TAG_subrange_type UINT16_C(0x0021)
#define DW_TAG_with_stmt UINT16_C(0x0022)
#define DW_TAG_access_declaration UINT16_C(0x0023)
#define DW_TAG_base_type UINT16_C(0x0024)
#define DW_TAG_catch_block UINT16_C(0x0025)
#define DW_TAG_const_type UINT16_C(0x0026)
#define DW_TAG_constant UINT16_C(0x0027)
#define DW_TAG_enumerator UINT16_C(0x0028)
#define DW_TAG_file_type UINT16_C(0x0029)
#define DW_TAG_friend UINT16_C(0x002a)
#define DW_TAG_namelist UINT16_C(0x002b)
#define DW_TAG_namelist_item UINT16_C(0x002c)
#define DW_TAG_packed_type UINT16_C(0x002d)
#define DW_TAG_subprogram UINT16_C(0x002e)
#define DW_TAG_template_type_parameter UINT16_C(0x002f)
#define DW_TAG_template_value_parameter UINT16_C(0x0030)
#define DW_TAG_thrown_type UINT16_C(0x0031)
#define DW_TAG_try_block UINT16_C(0x0032)
#define DW_TAG_variant_part UINT16_C(0x0033)
#define DW_TAG_variable UINT16_C(0x0034)
#define DW_TAG_volatile_type UINT16_C(0x0035)
#define DW_TAG_dwarf_procedure UINT16_C(0x0036)
#define DW_TAG_restrict_type UINT16_C(0x0037)
#define DW_TAG_interface_type UINT16_C(0x0038)
#define DW_TAG_namespace UINT16_C(0x0039)
#define DW_TAG_imported_module UINT16_C(0x003a)
#define DW_TAG_unspecified_type UINT16_C(0x003b)
#define DW_TAG_partial_unit UINT16_C(0x003c)
#define DW_TAG_imported_unit UINT16_C(0x003d)
#define DW_TAG_condition UINT16_C(0x003f)
#define DW_TAG_shared_type UINT16_C(0x0040)
#define DW_TAG_type_unit UINT16_C(0x0041)
#define DW_TAG_rvalue_reference_type UINT16_C(0x0042)
#define DW_TAG_template_alias UINT16_C(0x0043)
#define DW_TAG_lo_user UINT16_C(0x4080)
#define DW_TAG_GNU_call_site UINT16_C(0x4109)
#define DW_TAG_GNU_call_site_parameter UINT16_C(0x410a)
#define DW_TAG_hi_user UINT16_C(0xffff)
/** @} */
/** @name DIE Attributes.
* @{ */
#define DW_AT_sibling UINT16_C(0x0001)
#define DW_AT_location UINT16_C(0x0002)
#define DW_AT_name UINT16_C(0x0003)
#define DW_AT_ordering UINT16_C(0x0009)
#define DW_AT_byte_size UINT16_C(0x000b)
#define DW_AT_bit_offset UINT16_C(0x000c)
#define DW_AT_bit_size UINT16_C(0x000d)
#define DW_AT_stmt_list UINT16_C(0x0010)
#define DW_AT_low_pc UINT16_C(0x0011)
#define DW_AT_high_pc UINT16_C(0x0012)
#define DW_AT_language UINT16_C(0x0013)
#define DW_AT_discr UINT16_C(0x0015)
#define DW_AT_discr_value UINT16_C(0x0016)
#define DW_AT_visibility UINT16_C(0x0017)
#define DW_AT_import UINT16_C(0x0018)
#define DW_AT_string_length UINT16_C(0x0019)
#define DW_AT_common_reference UINT16_C(0x001a)
#define DW_AT_comp_dir UINT16_C(0x001b)
#define DW_AT_const_value UINT16_C(0x001c)
#define DW_AT_containing_type UINT16_C(0x001d)
#define DW_AT_default_value UINT16_C(0x001e)
#define DW_AT_inline UINT16_C(0x0020)
#define DW_AT_is_optional UINT16_C(0x0021)
#define DW_AT_lower_bound UINT16_C(0x0022)
#define DW_AT_producer UINT16_C(0x0025)
#define DW_AT_prototyped UINT16_C(0x0027)
#define DW_AT_return_addr UINT16_C(0x002a)
#define DW_AT_start_scope UINT16_C(0x002c)
#define DW_AT_bit_stride UINT16_C(0x002e)
#define DW_AT_upper_bound UINT16_C(0x002f)
#define DW_AT_abstract_origin UINT16_C(0x0031)
#define DW_AT_accessibility UINT16_C(0x0032)
#define DW_AT_address_class UINT16_C(0x0033)
#define DW_AT_artificial UINT16_C(0x0034)
#define DW_AT_base_types UINT16_C(0x0035)
#define DW_AT_calling_convention UINT16_C(0x0036)
#define DW_AT_count UINT16_C(0x0037)
#define DW_AT_data_member_location UINT16_C(0x0038)
#define DW_AT_decl_column UINT16_C(0x0039)
#define DW_AT_decl_file UINT16_C(0x003a)
#define DW_AT_decl_line UINT16_C(0x003b)
#define DW_AT_declaration UINT16_C(0x003c)
#define DW_AT_discr_list UINT16_C(0x003d)
#define DW_AT_encoding UINT16_C(0x003e)
#define DW_AT_external UINT16_C(0x003f)
#define DW_AT_frame_base UINT16_C(0x0040)
#define DW_AT_friend UINT16_C(0x0041)
#define DW_AT_identifier_case UINT16_C(0x0042)
#define DW_AT_macro_info UINT16_C(0x0043)
#define DW_AT_namelist_item UINT16_C(0x0044)
#define DW_AT_priority UINT16_C(0x0045)
#define DW_AT_segment UINT16_C(0x0046)
#define DW_AT_specification UINT16_C(0x0047)
#define DW_AT_static_link UINT16_C(0x0048)
#define DW_AT_type UINT16_C(0x0049)
#define DW_AT_use_location UINT16_C(0x004a)
#define DW_AT_variable_parameter UINT16_C(0x004b)
#define DW_AT_virtuality UINT16_C(0x004c)
#define DW_AT_vtable_elem_location UINT16_C(0x004d)
#define DW_AT_allocated UINT16_C(0x004e)
#define DW_AT_associated UINT16_C(0x004f)
#define DW_AT_data_location UINT16_C(0x0050)
#define DW_AT_byte_stride UINT16_C(0x0051)
#define DW_AT_entry_pc UINT16_C(0x0052)
#define DW_AT_use_UTF8 UINT16_C(0x0053)
#define DW_AT_extension UINT16_C(0x0054)
#define DW_AT_ranges UINT16_C(0x0055)
#define DW_AT_trampoline UINT16_C(0x0056)
#define DW_AT_call_column UINT16_C(0x0057)
#define DW_AT_call_file UINT16_C(0x0058)
#define DW_AT_call_line UINT16_C(0x0059)
#define DW_AT_description UINT16_C(0x005a)
#define DW_AT_binary_scale UINT16_C(0x005b)
#define DW_AT_decimal_scale UINT16_C(0x005c)
#define DW_AT_small UINT16_C(0x005d)
#define DW_AT_decimal_sign UINT16_C(0x005e)
#define DW_AT_digit_count UINT16_C(0x005f)
#define DW_AT_picture_string UINT16_C(0x0060)
#define DW_AT_mutable UINT16_C(0x0061)
#define DW_AT_threads_scaled UINT16_C(0x0062)
#define DW_AT_explicit UINT16_C(0x0063)
#define DW_AT_object_pointer UINT16_C(0x0064)
#define DW_AT_endianity UINT16_C(0x0065)
#define DW_AT_elemental UINT16_C(0x0066)
#define DW_AT_pure UINT16_C(0x0067)
#define DW_AT_recursive UINT16_C(0x0068)
#define DW_AT_signature UINT16_C(0x0069)
#define DW_AT_main_subprogram UINT16_C(0x006a)
#define DW_AT_data_bit_offset UINT16_C(0x006b)
#define DW_AT_const_expr UINT16_C(0x006c)
#define DW_AT_enum_class UINT16_C(0x006d)
#define DW_AT_linkage_name UINT16_C(0x006e)
#define DW_AT_lo_user UINT16_C(0x2000)
/** Used by GCC and others, same as DW_AT_linkage_name. See http://wiki.dwarfstd.org/index.php?title=DW_AT_linkage_name*/
#define DW_AT_MIPS_linkage_name UINT16_C(0x2007)
#define DW_AT_hi_user UINT16_C(0x3fff)
/** @} */
/** @name DIE Forms.
* @{ */
#define DW_FORM_addr UINT16_C(0x01)
/* What was 0x02? */
#define DW_FORM_block2 UINT16_C(0x03)
#define DW_FORM_block4 UINT16_C(0x04)
#define DW_FORM_data2 UINT16_C(0x05)
#define DW_FORM_data4 UINT16_C(0x06)
#define DW_FORM_data8 UINT16_C(0x07)
#define DW_FORM_string UINT16_C(0x08)
#define DW_FORM_block UINT16_C(0x09)
#define DW_FORM_block1 UINT16_C(0x0a)
#define DW_FORM_data1 UINT16_C(0x0b)
#define DW_FORM_flag UINT16_C(0x0c)
#define DW_FORM_sdata UINT16_C(0x0d)
#define DW_FORM_strp UINT16_C(0x0e)
#define DW_FORM_udata UINT16_C(0x0f)
#define DW_FORM_ref_addr UINT16_C(0x10)
#define DW_FORM_ref1 UINT16_C(0x11)
#define DW_FORM_ref2 UINT16_C(0x12)
#define DW_FORM_ref4 UINT16_C(0x13)
#define DW_FORM_ref8 UINT16_C(0x14)
#define DW_FORM_ref_udata UINT16_C(0x15)
#define DW_FORM_indirect UINT16_C(0x16)
#define DW_FORM_sec_offset UINT16_C(0x17)
#define DW_FORM_exprloc UINT16_C(0x18)
#define DW_FORM_flag_present UINT16_C(0x19)
#define DW_FORM_ref_sig8 UINT16_C(0x20)
/** @} */
/** @name Address classes.
* @{ */
#define DW_ADDR_none UINT8_C(0)
#define DW_ADDR_i386_near16 UINT8_C(1)
#define DW_ADDR_i386_far16 UINT8_C(2)
#define DW_ADDR_i386_huge16 UINT8_C(3)
#define DW_ADDR_i386_near32 UINT8_C(4)
#define DW_ADDR_i386_far32 UINT8_C(5)
/** @} */
/** @name Location Expression Opcodes
* @{ */
#define DW_OP_addr UINT8_C(0x03) /**< 1 operand, a constant address (size target specific). */
#define DW_OP_deref UINT8_C(0x06) /**< 0 operands. */
#define DW_OP_const1u UINT8_C(0x08) /**< 1 operand, a 1-byte constant. */
#define DW_OP_const1s UINT8_C(0x09) /**< 1 operand, a 1-byte constant. */
#define DW_OP_const2u UINT8_C(0x0a) /**< 1 operand, a 2-byte constant. */
#define DW_OP_const2s UINT8_C(0x0b) /**< 1 operand, a 2-byte constant. */
#define DW_OP_const4u UINT8_C(0x0c) /**< 1 operand, a 4-byte constant. */
#define DW_OP_const4s UINT8_C(0x0d) /**< 1 operand, a 4-byte constant. */
#define DW_OP_const8u UINT8_C(0x0e) /**< 1 operand, a 8-byte constant. */
#define DW_OP_const8s UINT8_C(0x0f) /**< 1 operand, a 8-byte constant. */
#define DW_OP_constu UINT8_C(0x10) /**< 1 operand, a ULEB128 constant. */
#define DW_OP_consts UINT8_C(0x11) /**< 1 operand, a SLEB128 constant. */
#define DW_OP_dup UINT8_C(0x12) /**< 0 operands. */
#define DW_OP_drop UINT8_C(0x13) /**< 0 operands. */
#define DW_OP_over UINT8_C(0x14) /**< 0 operands. */
#define DW_OP_pick UINT8_C(0x15) /**< 1 operands, a 1-byte stack index. */
#define DW_OP_swap UINT8_C(0x16) /**< 0 operands. */
#define DW_OP_rot UINT8_C(0x17) /**< 0 operands. */
#define DW_OP_xderef UINT8_C(0x18) /**< 0 operands. */
#define DW_OP_abs UINT8_C(0x19) /**< 0 operands. */
#define DW_OP_and UINT8_C(0x1a) /**< 0 operands. */
#define DW_OP_div UINT8_C(0x1b) /**< 0 operands. */
#define DW_OP_minus UINT8_C(0x1c) /**< 0 operands. */
#define DW_OP_mod UINT8_C(0x1d) /**< 0 operands. */
#define DW_OP_mul UINT8_C(0x1e) /**< 0 operands. */
#define DW_OP_neg UINT8_C(0x1f) /**< 0 operands. */
#define DW_OP_not UINT8_C(0x20) /**< 0 operands. */
#define DW_OP_or UINT8_C(0x21) /**< 0 operands. */
#define DW_OP_plus UINT8_C(0x22) /**< 0 operands. */
#define DW_OP_plus_uconst UINT8_C(0x23) /**< 1 operands, a ULEB128 addend. */
#define DW_OP_shl UINT8_C(0x24) /**< 0 operands. */
#define DW_OP_shr UINT8_C(0x25) /**< 0 operands. */
#define DW_OP_shra UINT8_C(0x26) /**< 0 operands. */
#define DW_OP_xor UINT8_C(0x27) /**< 0 operands. */
#define DW_OP_skip UINT8_C(0x2f) /**< 1 signed 2-byte constant. */
#define DW_OP_bra UINT8_C(0x28) /**< 1 signed 2-byte constant. */
#define DW_OP_eq UINT8_C(0x29) /**< 0 operands. */
#define DW_OP_ge UINT8_C(0x2a) /**< 0 operands. */
#define DW_OP_gt UINT8_C(0x2b) /**< 0 operands. */
#define DW_OP_le UINT8_C(0x2c) /**< 0 operands. */
#define DW_OP_lt UINT8_C(0x2d) /**< 0 operands. */
#define DW_OP_ne UINT8_C(0x2e) /**< 0 operands. */
#define DW_OP_lit0 UINT8_C(0x30) /**< 0 operands - literals 0..31 */
#define DW_OP_lit31 UINT8_C(0x4f) /**< last litteral. */
#define DW_OP_reg0 UINT8_C(0x50) /**< 0 operands - reg 0..31. */
#define DW_OP_reg31 UINT8_C(0x6f) /**< last register. */
#define DW_OP_breg0 UINT8_C(0x70) /**< 1 operand, a SLEB128 offset. */
#define DW_OP_breg31 UINT8_C(0x8f) /**< last branch register. */
#define DW_OP_regx UINT8_C(0x90) /**< 1 operand, a ULEB128 register. */
#define DW_OP_fbreg UINT8_C(0x91) /**< 1 operand, a SLEB128 offset. */
#define DW_OP_bregx UINT8_C(0x92) /**< 2 operands, a ULEB128 register followed by a SLEB128 offset. */
#define DW_OP_piece UINT8_C(0x93) /**< 1 operand, a ULEB128 size of piece addressed. */
#define DW_OP_deref_size UINT8_C(0x94) /**< 1 operand, a 1-byte size of data retrieved. */
#define DW_OP_xderef_size UINT8_C(0x95) /**< 1 operand, a 1-byte size of data retrieved. */
#define DW_OP_nop UINT8_C(0x96) /**< 0 operands. */
#define DW_OP_lo_user UINT8_C(0xe0) /**< First user opcode */
#define DW_OP_hi_user UINT8_C(0xff) /**< Last user opcode. */
/** @} */
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Pointer to a DWARF section reader. */
typedef struct RTDWARFCURSOR *PRTDWARFCURSOR;
/** Pointer to an attribute descriptor. */
typedef struct RTDWARFATTRDESC const *PCRTDWARFATTRDESC;
/** Pointer to a DIE. */
typedef struct RTDWARFDIE *PRTDWARFDIE;
/** Pointer to a const DIE. */
typedef struct RTDWARFDIE const *PCRTDWARFDIE;
/**
* DWARF sections.
*/
typedef enum krtDbgModDwarfSect
{
krtDbgModDwarfSect_abbrev = 0,
krtDbgModDwarfSect_aranges,
krtDbgModDwarfSect_frame,
krtDbgModDwarfSect_info,
krtDbgModDwarfSect_inlined,
krtDbgModDwarfSect_line,
krtDbgModDwarfSect_loc,
krtDbgModDwarfSect_macinfo,
krtDbgModDwarfSect_pubnames,
krtDbgModDwarfSect_pubtypes,
krtDbgModDwarfSect_ranges,
krtDbgModDwarfSect_str,
krtDbgModDwarfSect_types,
/** End of valid parts (exclusive). */
krtDbgModDwarfSect_End
} krtDbgModDwarfSect;
/**
* Abbreviation cache entry.
*/
typedef struct RTDWARFABBREV
{
/** Whether there are children or not. */
bool fChildren;
/** The tag. */
uint16_t uTag;
/** Offset into the abbrev section of the specification pairs. */
uint32_t offSpec;
/** The abbreviation table offset this is entry is valid for.
* UINT32_MAX if not valid. */
uint32_t offAbbrev;
} RTDWARFABBREV;
/** Pointer to an abbreviation cache entry. */
typedef RTDWARFABBREV *PRTDWARFABBREV;
/** Pointer to a const abbreviation cache entry. */
typedef RTDWARFABBREV const *PCRTDWARFABBREV;
/**
* Structure for gathering segment info.
*/
typedef struct RTDBGDWARFSEG
{
/** The highest offset in the segment. */
uint64_t offHighest;
/** Calculated base address. */
uint64_t uBaseAddr;
/** Estimated The segment size. */
uint64_t cbSegment;
/** Segment number (RTLDRSEG::Sel16bit). */
RTSEL uSegment;
} RTDBGDWARFSEG;
/** Pointer to segment info. */
typedef RTDBGDWARFSEG *PRTDBGDWARFSEG;
/**
* The instance data of the DWARF reader.
*/
typedef struct RTDBGMODDWARF
{
/** The debug container containing doing the real work. */
RTDBGMOD hCnt;
/** The image module (no reference). */
PRTDBGMODINT pImgMod;
/** The debug info module (no reference). */
PRTDBGMODINT pDbgInfoMod;
/** Nested image module (with reference ofc). */
PRTDBGMODINT pNestedMod;
/** DWARF debug info sections. */
struct
{
/** The file offset of the part. */
RTFOFF offFile;
/** The size of the part. */
size_t cb;
/** The memory mapping of the part. */
void const *pv;
/** Set if present. */
bool fPresent;
/** The debug info ordinal number in the image file. */
uint32_t iDbgInfo;
} aSections[krtDbgModDwarfSect_End];
/** The offset into the abbreviation section of the current cache. */
uint32_t offCachedAbbrev;
/** The number of cached abbreviations we've allocated space for. */
uint32_t cCachedAbbrevsAlloced;
/** Array of cached abbreviations, indexed by code. */
PRTDWARFABBREV paCachedAbbrevs;
/** Used by rtDwarfAbbrev_Lookup when the result is uncachable. */
RTDWARFABBREV LookupAbbrev;
/** The list of compilation units (RTDWARFDIE). */
RTLISTANCHOR CompileUnitList;
/** Set if we have to use link addresses because the module does not have
* fixups (mach_kernel). */
bool fUseLinkAddress;
/** This is set to -1 if we're doing everything in one pass.
* Otherwise it's 1 or 2:
* - In pass 1, we collect segment info.
* - In pass 2, we add debug info to the container.
* The two pass parsing is necessary for watcom generated symbol files as
* these contains no information about the code and data segments in the
* image. So we have to figure out some approximate stuff based on the
* segments and offsets we encounter in the debug info. */
int8_t iWatcomPass;
/** Segment index hint. */
uint16_t iSegHint;
/** The number of segments in paSegs.
* (During segment copying, this is abused to count useful segments.) */
uint32_t cSegs;
/** Pointer to segments if iWatcomPass isn't -1. */
PRTDBGDWARFSEG paSegs;
#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
/** DIE allocators. */
struct
{
RTMEMCACHE hMemCache;
uint32_t cbMax;
} aDieAllocators[2];
#endif
} RTDBGMODDWARF;
/** Pointer to instance data of the DWARF reader. */
typedef RTDBGMODDWARF *PRTDBGMODDWARF;
/**
* DWARF cursor for reading byte data.
*/
typedef struct RTDWARFCURSOR
{
/** The current position. */
uint8_t const *pb;
/** The number of bytes left to read. */
size_t cbLeft;
/** The number of bytes left to read in the current unit. */
size_t cbUnitLeft;
/** The DWARF debug info reader instance. */
PRTDBGMODDWARF pDwarfMod;
/** Set if this is 64-bit DWARF, clear if 32-bit. */
bool f64bitDwarf;
/** Set if the format endian is native, clear if endian needs to be
* inverted. */
bool fNativEndian;
/** The size of a native address. */
uint8_t cbNativeAddr;
/** The cursor status code. This is VINF_SUCCESS until some error
* occurs. */
int rc;
/** The start of the area covered by the cursor.
* Used for repositioning the cursor relative to the start of a section. */
uint8_t const *pbStart;
/** The section. */
krtDbgModDwarfSect enmSect;
} RTDWARFCURSOR;
/**
* DWARF line number program state.
*/
typedef struct RTDWARFLINESTATE
{
/** Virtual Line Number Machine Registers. */
struct
{
uint64_t uAddress;
uint64_t idxOp;
uint32_t iFile;
uint32_t uLine;
uint32_t uColumn;
bool fIsStatement;
bool fBasicBlock;
bool fEndSequence;
bool fPrologueEnd;
bool fEpilogueBegin;
uint32_t uIsa;
uint32_t uDiscriminator;
RTSEL uSegment;
} Regs;
/** @} */
/** Header. */
struct
{
uint32_t uVer;
uint64_t offFirstOpcode;
uint8_t cbMinInstr;
uint8_t cMaxOpsPerInstr;
uint8_t u8DefIsStmt;
int8_t s8LineBase;
uint8_t u8LineRange;
uint8_t u8OpcodeBase;
uint8_t const *pacStdOperands;
} Hdr;
/** @name Include Path Table (0-based)
* @{ */
const char **papszIncPaths;
uint32_t cIncPaths;
/** @} */
/** @name File Name Table (0-based, dummy zero entry)
* @{ */
char **papszFileNames;
uint32_t cFileNames;
/** @} */
/** The DWARF debug info reader instance. */
PRTDBGMODDWARF pDwarfMod;
} RTDWARFLINESTATE;
/** Pointer to a DWARF line number program state. */
typedef RTDWARFLINESTATE *PRTDWARFLINESTATE;
/**
* Decodes an attribute and stores it in the specified DIE member field.
*
* @returns IPRT status code.
* @param pDie Pointer to the DIE structure.
* @param pbMember Pointer to the first byte in the member.
* @param pDesc The attribute descriptor.
* @param uForm The data form.
* @param pDataCursor The cursor to read data from.
*/
typedef DECLCALLBACK(int) FNRTDWARFATTRDECODER(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
uint32_t uForm, PRTDWARFCURSOR pCursor);
/** Pointer to an attribute decoder callback. */
typedef FNRTDWARFATTRDECODER *PFNRTDWARFATTRDECODER;
/**
* Attribute descriptor.
*/
typedef struct RTDWARFATTRDESC
{
/** The attribute. */
uint16_t uAttr;
/** The data member offset. */
uint16_t off;
/** The data member size and initialization method. */
uint8_t cbInit;
uint8_t bPadding[3]; /**< Alignment padding. */
/** The decoder function. */
PFNRTDWARFATTRDECODER pfnDecoder;
} RTDWARFATTRDESC;
/** Define a attribute entry. */
#define ATTR_ENTRY(a_uAttr, a_Struct, a_Member, a_Init, a_pfnDecoder) \
{ \
a_uAttr, \
(uint16_t)RT_OFFSETOF(a_Struct, a_Member), \
a_Init | ((uint8_t)RT_SIZEOFMEMB(a_Struct, a_Member) & ATTR_SIZE_MASK), \
{ 0, 0, 0 }, \
a_pfnDecoder\
}
/** @name Attribute size and init methods.
* @{ */
#define ATTR_INIT_ZERO UINT8_C(0x00)
#define ATTR_INIT_FFFS UINT8_C(0x80)
#define ATTR_INIT_MASK UINT8_C(0x80)
#define ATTR_SIZE_MASK UINT8_C(0x3f)
#define ATTR_GET_SIZE(a_pAttrDesc) ((a_pAttrDesc)->cbInit & ATTR_SIZE_MASK)
/** @} */
/**
* DIE descriptor.
*/
typedef struct RTDWARFDIEDESC
{
/** The size of the DIE. */
size_t cbDie;
/** The number of attributes. */
size_t cAttributes;
/** The */
PCRTDWARFATTRDESC paAttributes;
} RTDWARFDIEDESC;
typedef struct RTDWARFDIEDESC const *PCRTDWARFDIEDESC;
/** DIE descriptor initializer. */
#define DIE_DESC_INIT(a_Type, a_aAttrs) { sizeof(a_Type), RT_ELEMENTS(a_aAttrs), &a_aAttrs[0] }
/**
* DIE core structure, all inherits (starts with) this.
*/
typedef struct RTDWARFDIE
{
/** Pointer to the parent node. NULL if root unit. */
struct RTDWARFDIE *pParent;
/** Our node in the sibling list. */
RTLISTNODE SiblingNode;
/** List of children. */
RTLISTNODE ChildList;
/** The number of attributes successfully decoded. */
uint8_t cDecodedAttrs;
/** The number of unknown or otherwise unhandled attributes. */
uint8_t cUnhandledAttrs;
#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
/** The allocator index. */
uint8_t iAllocator;
#endif
/** The die tag, indicating which union structure to use. */
uint16_t uTag;
/** Offset of the abbreviation specification (within debug_abbrev). */
uint32_t offSpec;
} RTDWARFDIE;
/**
* DWARF address structure.
*/
typedef struct RTDWARFADDR
{
/** The address. */
uint64_t uAddress;
} RTDWARFADDR;
typedef RTDWARFADDR *PRTDWARFADDR;
typedef RTDWARFADDR const *PCRTDWARFADDR;
/**
* DWARF address range.
*/
typedef struct RTDWARFADDRRANGE
{
uint64_t uLowAddress;
uint64_t uHighAddress;
uint8_t const *pbRanges; /* ?? */
uint8_t cAttrs : 2;
uint8_t fHaveLowAddress : 1;
uint8_t fHaveHighAddress : 1;
uint8_t fHaveHighIsAddress : 1;
uint8_t fHaveRanges : 1;
} RTDWARFADDRRANGE;
typedef RTDWARFADDRRANGE *PRTDWARFADDRRANGE;
typedef RTDWARFADDRRANGE const *PCRTDWARFADDRRANGE;
/** What a RTDWARFREF is relative to. */
typedef enum krtDwarfRef
{
krtDwarfRef_NotSet,
krtDwarfRef_LineSection,
krtDwarfRef_LocSection,
krtDwarfRef_RangesSection,
krtDwarfRef_InfoSection,
krtDwarfRef_SameUnit,
krtDwarfRef_TypeId64
} krtDwarfRef;
/**
* DWARF reference.
*/
typedef struct RTDWARFREF
{
/** The offset. */
uint64_t off;
/** What the offset is relative to. */
krtDwarfRef enmWrt;
} RTDWARFREF;
typedef RTDWARFREF *PRTDWARFREF;
typedef RTDWARFREF const *PCRTDWARFREF;
/**
* DWARF Location state.
*/
typedef struct RTDWARFLOCST
{
/** The input cursor. */
RTDWARFCURSOR Cursor;
/** Points to the current top of the stack. Initial value -1. */
int32_t iTop;
/** The value stack. */
uint64_t auStack[64];
} RTDWARFLOCST;
/** Pointer to location state. */
typedef RTDWARFLOCST *PRTDWARFLOCST;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static FNRTDWARFATTRDECODER rtDwarfDecode_Address;
static FNRTDWARFATTRDECODER rtDwarfDecode_Bool;
static FNRTDWARFATTRDECODER rtDwarfDecode_LowHighPc;
static FNRTDWARFATTRDECODER rtDwarfDecode_Ranges;
static FNRTDWARFATTRDECODER rtDwarfDecode_Reference;
static FNRTDWARFATTRDECODER rtDwarfDecode_SectOff;
static FNRTDWARFATTRDECODER rtDwarfDecode_String;
static FNRTDWARFATTRDECODER rtDwarfDecode_UnsignedInt;
static FNRTDWARFATTRDECODER rtDwarfDecode_SegmentLoc;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** RTDWARFDIE description. */
static const RTDWARFDIEDESC g_CoreDieDesc = { sizeof(RTDWARFDIE), 0, NULL };
/**
* DW_TAG_compile_unit & DW_TAG_partial_unit.
*/
typedef struct RTDWARFDIECOMPILEUNIT
{
/** The DIE core structure. */
RTDWARFDIE Core;
/** The unit name. */
const char *pszName;
/** The address range of the code belonging to this unit. */
RTDWARFADDRRANGE PcRange;
/** The language name. */
uint16_t uLanguage;
/** The identifier case. */
uint8_t uIdentifierCase;
/** String are UTF-8 encoded. If not set, the encoding is
* unknown. */
bool fUseUtf8;
/** The unit contains main() or equivalent. */
bool fMainFunction;
/** The line numbers for this unit. */
RTDWARFREF StmtListRef;
/** The macro information for this unit. */
RTDWARFREF MacroInfoRef;
/** Reference to the base types. */
RTDWARFREF BaseTypesRef;
/** Working directory for the unit. */
const char *pszCurDir;
/** The name of the compiler or whatever that produced this unit. */
const char *pszProducer;
/** @name From the unit header.
* @{ */
/** The offset into debug_info of this unit (for references). */
uint64_t offUnit;
/** The length of this unit. */
uint64_t cbUnit;
/** The offset into debug_abbrev of the abbreviation for this unit. */
uint64_t offAbbrev;
/** The native address size. */
uint8_t cbNativeAddr;
/** The DWARF version. */
uint8_t uDwarfVer;
/** @} */
} RTDWARFDIECOMPILEUNIT;
typedef RTDWARFDIECOMPILEUNIT *PRTDWARFDIECOMPILEUNIT;
/** RTDWARFDIECOMPILEUNIT attributes. */
static const RTDWARFATTRDESC g_aCompileUnitAttrs[] =
{
ATTR_ENTRY(DW_AT_name, RTDWARFDIECOMPILEUNIT, pszName, ATTR_INIT_ZERO, rtDwarfDecode_String),
ATTR_ENTRY(DW_AT_low_pc, RTDWARFDIECOMPILEUNIT, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
ATTR_ENTRY(DW_AT_high_pc, RTDWARFDIECOMPILEUNIT, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
ATTR_ENTRY(DW_AT_ranges, RTDWARFDIECOMPILEUNIT, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_Ranges),
ATTR_ENTRY(DW_AT_language, RTDWARFDIECOMPILEUNIT, uLanguage, ATTR_INIT_ZERO, rtDwarfDecode_UnsignedInt),
ATTR_ENTRY(DW_AT_macro_info, RTDWARFDIECOMPILEUNIT, MacroInfoRef, ATTR_INIT_ZERO, rtDwarfDecode_SectOff),
ATTR_ENTRY(DW_AT_stmt_list, RTDWARFDIECOMPILEUNIT, StmtListRef, ATTR_INIT_ZERO, rtDwarfDecode_SectOff),
ATTR_ENTRY(DW_AT_comp_dir, RTDWARFDIECOMPILEUNIT, pszCurDir, ATTR_INIT_ZERO, rtDwarfDecode_String),
ATTR_ENTRY(DW_AT_producer, RTDWARFDIECOMPILEUNIT, pszProducer, ATTR_INIT_ZERO, rtDwarfDecode_String),
ATTR_ENTRY(DW_AT_identifier_case, RTDWARFDIECOMPILEUNIT, uIdentifierCase,ATTR_INIT_ZERO, rtDwarfDecode_UnsignedInt),
ATTR_ENTRY(DW_AT_base_types, RTDWARFDIECOMPILEUNIT, BaseTypesRef, ATTR_INIT_ZERO, rtDwarfDecode_Reference),
ATTR_ENTRY(DW_AT_use_UTF8, RTDWARFDIECOMPILEUNIT, fUseUtf8, ATTR_INIT_ZERO, rtDwarfDecode_Bool),
ATTR_ENTRY(DW_AT_main_subprogram, RTDWARFDIECOMPILEUNIT, fMainFunction, ATTR_INIT_ZERO, rtDwarfDecode_Bool)
};
/** RTDWARFDIECOMPILEUNIT description. */
static const RTDWARFDIEDESC g_CompileUnitDesc = DIE_DESC_INIT(RTDWARFDIECOMPILEUNIT, g_aCompileUnitAttrs);
/**
* DW_TAG_subprogram.
*/
typedef struct RTDWARFDIESUBPROGRAM
{
/** The DIE core structure. */
RTDWARFDIE Core;
/** The name. */
const char *pszName;
/** The linkage name. */
const char *pszLinkageName;
/** The address range of the code belonging to this unit. */
RTDWARFADDRRANGE PcRange;
/** The first instruction in the function. */
RTDWARFADDR EntryPc;
/** Segment number (watcom). */
RTSEL uSegment;
/** Reference to the specification. */
RTDWARFREF SpecRef;
} RTDWARFDIESUBPROGRAM;
/** Pointer to a DW_TAG_subprogram DIE. */
typedef RTDWARFDIESUBPROGRAM *PRTDWARFDIESUBPROGRAM;
/** Pointer to a const DW_TAG_subprogram DIE. */
typedef RTDWARFDIESUBPROGRAM const *PCRTDWARFDIESUBPROGRAM;
/** RTDWARFDIESUBPROGRAM attributes. */
static const RTDWARFATTRDESC g_aSubProgramAttrs[] =
{
ATTR_ENTRY(DW_AT_name, RTDWARFDIESUBPROGRAM, pszName, ATTR_INIT_ZERO, rtDwarfDecode_String),
ATTR_ENTRY(DW_AT_linkage_name, RTDWARFDIESUBPROGRAM, pszLinkageName, ATTR_INIT_ZERO, rtDwarfDecode_String),
ATTR_ENTRY(DW_AT_MIPS_linkage_name, RTDWARFDIESUBPROGRAM, pszLinkageName, ATTR_INIT_ZERO, rtDwarfDecode_String),
ATTR_ENTRY(DW_AT_low_pc, RTDWARFDIESUBPROGRAM, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
ATTR_ENTRY(DW_AT_high_pc, RTDWARFDIESUBPROGRAM, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
ATTR_ENTRY(DW_AT_ranges, RTDWARFDIESUBPROGRAM, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_Ranges),
ATTR_ENTRY(DW_AT_entry_pc, RTDWARFDIESUBPROGRAM, EntryPc, ATTR_INIT_ZERO, rtDwarfDecode_Address),
ATTR_ENTRY(DW_AT_segment, RTDWARFDIESUBPROGRAM, uSegment, ATTR_INIT_ZERO, rtDwarfDecode_SegmentLoc),
ATTR_ENTRY(DW_AT_specification, RTDWARFDIESUBPROGRAM, SpecRef, ATTR_INIT_ZERO, rtDwarfDecode_Reference)
};
/** RTDWARFDIESUBPROGRAM description. */
static const RTDWARFDIEDESC g_SubProgramDesc = DIE_DESC_INIT(RTDWARFDIESUBPROGRAM, g_aSubProgramAttrs);
/** RTDWARFDIESUBPROGRAM attributes for the specification hack. */
static const RTDWARFATTRDESC g_aSubProgramSpecHackAttrs[] =
{
ATTR_ENTRY(DW_AT_name, RTDWARFDIESUBPROGRAM, pszName, ATTR_INIT_ZERO, rtDwarfDecode_String),
ATTR_ENTRY(DW_AT_linkage_name, RTDWARFDIESUBPROGRAM, pszLinkageName, ATTR_INIT_ZERO, rtDwarfDecode_String),
ATTR_ENTRY(DW_AT_MIPS_linkage_name, RTDWARFDIESUBPROGRAM, pszLinkageName, ATTR_INIT_ZERO, rtDwarfDecode_String),
};
/** RTDWARFDIESUBPROGRAM description for the specification hack. */
static const RTDWARFDIEDESC g_SubProgramSpecHackDesc = DIE_DESC_INIT(RTDWARFDIESUBPROGRAM, g_aSubProgramSpecHackAttrs);
/**
* DW_TAG_label.
*/
typedef struct RTDWARFDIELABEL
{
/** The DIE core structure. */
RTDWARFDIE Core;
/** The name. */
const char *pszName;
/** The address of the first instruction. */
RTDWARFADDR Address;
/** Segment number (watcom). */
RTSEL uSegment;
/** Externally visible? */
bool fExternal;
} RTDWARFDIELABEL;
/** Pointer to a DW_TAG_label DIE. */
typedef RTDWARFDIELABEL *PRTDWARFDIELABEL;
/** Pointer to a const DW_TAG_label DIE. */
typedef RTDWARFDIELABEL const *PCRTDWARFDIELABEL;
/** RTDWARFDIESUBPROGRAM attributes. */
static const RTDWARFATTRDESC g_aLabelAttrs[] =
{
ATTR_ENTRY(DW_AT_name, RTDWARFDIELABEL, pszName, ATTR_INIT_ZERO, rtDwarfDecode_String),
ATTR_ENTRY(DW_AT_low_pc, RTDWARFDIELABEL, Address, ATTR_INIT_ZERO, rtDwarfDecode_Address),
ATTR_ENTRY(DW_AT_segment, RTDWARFDIELABEL, uSegment, ATTR_INIT_ZERO, rtDwarfDecode_SegmentLoc),
ATTR_ENTRY(DW_AT_external, RTDWARFDIELABEL, fExternal, ATTR_INIT_ZERO, rtDwarfDecode_Bool)
};
/** RTDWARFDIESUBPROGRAM description. */
static const RTDWARFDIEDESC g_LabelDesc = DIE_DESC_INIT(RTDWARFDIELABEL, g_aLabelAttrs);
/**
* Tag names and descriptors.
*/
static const struct RTDWARFTAGDESC
{
/** The tag value. */
uint16_t uTag;
/** The tag name as string. */
const char *pszName;
/** The DIE descriptor to use. */
PCRTDWARFDIEDESC pDesc;
} g_aTagDescs[] =
{
#define TAGDESC(a_Name, a_pDesc) { DW_ ## a_Name, #a_Name, a_pDesc }
#define TAGDESC_EMPTY() { 0, NULL, NULL }
#define TAGDESC_CORE(a_Name) TAGDESC(a_Name, &g_CoreDieDesc)
TAGDESC_EMPTY(), /* 0x00 */
TAGDESC_CORE(TAG_array_type),
TAGDESC_CORE(TAG_class_type),
TAGDESC_CORE(TAG_entry_point),
TAGDESC_CORE(TAG_enumeration_type), /* 0x04 */
TAGDESC_CORE(TAG_formal_parameter),
TAGDESC_EMPTY(),
TAGDESC_EMPTY(),
TAGDESC_CORE(TAG_imported_declaration), /* 0x08 */
TAGDESC_EMPTY(),
TAGDESC(TAG_label, &g_LabelDesc),
TAGDESC_CORE(TAG_lexical_block),
TAGDESC_EMPTY(), /* 0x0c */
TAGDESC_CORE(TAG_member),
TAGDESC_EMPTY(),
TAGDESC_CORE(TAG_pointer_type),
TAGDESC_CORE(TAG_reference_type), /* 0x10 */
TAGDESC_CORE(TAG_compile_unit),
TAGDESC_CORE(TAG_string_type),
TAGDESC_CORE(TAG_structure_type),
TAGDESC_EMPTY(), /* 0x14 */
TAGDESC_CORE(TAG_subroutine_type),
TAGDESC_CORE(TAG_typedef),
TAGDESC_CORE(TAG_union_type),
TAGDESC_CORE(TAG_unspecified_parameters), /* 0x18 */
TAGDESC_CORE(TAG_variant),
TAGDESC_CORE(TAG_common_block),
TAGDESC_CORE(TAG_common_inclusion),
TAGDESC_CORE(TAG_inheritance), /* 0x1c */
TAGDESC_CORE(TAG_inlined_subroutine),
TAGDESC_CORE(TAG_module),
TAGDESC_CORE(TAG_ptr_to_member_type),
TAGDESC_CORE(TAG_set_type), /* 0x20 */
TAGDESC_CORE(TAG_subrange_type),
TAGDESC_CORE(TAG_with_stmt),
TAGDESC_CORE(TAG_access_declaration),
TAGDESC_CORE(TAG_base_type), /* 0x24 */
TAGDESC_CORE(TAG_catch_block),
TAGDESC_CORE(TAG_const_type),
TAGDESC_CORE(TAG_constant),
TAGDESC_CORE(TAG_enumerator), /* 0x28 */
TAGDESC_CORE(TAG_file_type),
TAGDESC_CORE(TAG_friend),
TAGDESC_CORE(TAG_namelist),
TAGDESC_CORE(TAG_namelist_item), /* 0x2c */
TAGDESC_CORE(TAG_packed_type),
TAGDESC(TAG_subprogram, &g_SubProgramDesc),
TAGDESC_CORE(TAG_template_type_parameter),
TAGDESC_CORE(TAG_template_value_parameter), /* 0x30 */
TAGDESC_CORE(TAG_thrown_type),
TAGDESC_CORE(TAG_try_block),
TAGDESC_CORE(TAG_variant_part),
TAGDESC_CORE(TAG_variable), /* 0x34 */
TAGDESC_CORE(TAG_volatile_type),
TAGDESC_CORE(TAG_dwarf_procedure),
TAGDESC_CORE(TAG_restrict_type),
TAGDESC_CORE(TAG_interface_type), /* 0x38 */
TAGDESC_CORE(TAG_namespace),
TAGDESC_CORE(TAG_imported_module),
TAGDESC_CORE(TAG_unspecified_type),
TAGDESC_CORE(TAG_partial_unit), /* 0x3c */
TAGDESC_CORE(TAG_imported_unit),
TAGDESC_EMPTY(),
TAGDESC_CORE(TAG_condition),
TAGDESC_CORE(TAG_shared_type), /* 0x40 */
TAGDESC_CORE(TAG_type_unit),
TAGDESC_CORE(TAG_rvalue_reference_type),
TAGDESC_CORE(TAG_template_alias)
#undef TAGDESC
#undef TAGDESC_EMPTY
#undef TAGDESC_CORE
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int rtDwarfInfo_ParseDie(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie, PCRTDWARFDIEDESC pDieDesc,
PRTDWARFCURSOR pCursor, PCRTDWARFABBREV pAbbrev, bool fInitDie);
#if defined(LOG_ENABLED) || defined(RT_STRICT)
/**
* Turns a tag value into a string for logging purposes.
*
* @returns String name.
* @param uTag The tag.
*/
static const char *rtDwarfLog_GetTagName(uint32_t uTag)
{
if (uTag < RT_ELEMENTS(g_aTagDescs))
{
const char *pszTag = g_aTagDescs[uTag].pszName;
if (pszTag)
return pszTag;
}
static char s_szStatic[32];
RTStrPrintf(s_szStatic, sizeof(s_szStatic),"DW_TAG_%#x", uTag);
return s_szStatic;
}
/**
* Turns an attributevalue into a string for logging purposes.
*
* @returns String name.
* @param uAttr The attribute.
*/
static const char *rtDwarfLog_AttrName(uint32_t uAttr)
{
switch (uAttr)
{
RT_CASE_RET_STR(DW_AT_sibling);
RT_CASE_RET_STR(DW_AT_location);
RT_CASE_RET_STR(DW_AT_name);
RT_CASE_RET_STR(DW_AT_ordering);
RT_CASE_RET_STR(DW_AT_byte_size);
RT_CASE_RET_STR(DW_AT_bit_offset);
RT_CASE_RET_STR(DW_AT_bit_size);
RT_CASE_RET_STR(DW_AT_stmt_list);
RT_CASE_RET_STR(DW_AT_low_pc);
RT_CASE_RET_STR(DW_AT_high_pc);
RT_CASE_RET_STR(DW_AT_language);
RT_CASE_RET_STR(DW_AT_discr);
RT_CASE_RET_STR(DW_AT_discr_value);
RT_CASE_RET_STR(DW_AT_visibility);
RT_CASE_RET_STR(DW_AT_import);
RT_CASE_RET_STR(DW_AT_string_length);
RT_CASE_RET_STR(DW_AT_common_reference);
RT_CASE_RET_STR(DW_AT_comp_dir);
RT_CASE_RET_STR(DW_AT_const_value);
RT_CASE_RET_STR(DW_AT_containing_type);
RT_CASE_RET_STR(DW_AT_default_value);
RT_CASE_RET_STR(DW_AT_inline);
RT_CASE_RET_STR(DW_AT_is_optional);
RT_CASE_RET_STR(DW_AT_lower_bound);
RT_CASE_RET_STR(DW_AT_producer);
RT_CASE_RET_STR(DW_AT_prototyped);
RT_CASE_RET_STR(DW_AT_return_addr);
RT_CASE_RET_STR(DW_AT_start_scope);
RT_CASE_RET_STR(DW_AT_bit_stride);
RT_CASE_RET_STR(DW_AT_upper_bound);
RT_CASE_RET_STR(DW_AT_abstract_origin);
RT_CASE_RET_STR(DW_AT_accessibility);
RT_CASE_RET_STR(DW_AT_address_class);
RT_CASE_RET_STR(DW_AT_artificial);
RT_CASE_RET_STR(DW_AT_base_types);
RT_CASE_RET_STR(DW_AT_calling_convention);
RT_CASE_RET_STR(DW_AT_count);
RT_CASE_RET_STR(DW_AT_data_member_location);
RT_CASE_RET_STR(DW_AT_decl_column);
RT_CASE_RET_STR(DW_AT_decl_file);
RT_CASE_RET_STR(DW_AT_decl_line);
RT_CASE_RET_STR(DW_AT_declaration);
RT_CASE_RET_STR(DW_AT_discr_list);
RT_CASE_RET_STR(DW_AT_encoding);
RT_CASE_RET_STR(DW_AT_external);
RT_CASE_RET_STR(DW_AT_frame_base);
RT_CASE_RET_STR(DW_AT_friend);
RT_CASE_RET_STR(DW_AT_identifier_case);
RT_CASE_RET_STR(DW_AT_macro_info);
RT_CASE_RET_STR(DW_AT_namelist_item);
RT_CASE_RET_STR(DW_AT_priority);
RT_CASE_RET_STR(DW_AT_segment);
RT_CASE_RET_STR(DW_AT_specification);
RT_CASE_RET_STR(DW_AT_static_link);
RT_CASE_RET_STR(DW_AT_type);
RT_CASE_RET_STR(DW_AT_use_location);
RT_CASE_RET_STR(DW_AT_variable_parameter);
RT_CASE_RET_STR(DW_AT_virtuality);
RT_CASE_RET_STR(DW_AT_vtable_elem_location);
RT_CASE_RET_STR(DW_AT_allocated);
RT_CASE_RET_STR(DW_AT_associated);
RT_CASE_RET_STR(DW_AT_data_location);
RT_CASE_RET_STR(DW_AT_byte_stride);
RT_CASE_RET_STR(DW_AT_entry_pc);
RT_CASE_RET_STR(DW_AT_use_UTF8);
RT_CASE_RET_STR(DW_AT_extension);
RT_CASE_RET_STR(DW_AT_ranges);
RT_CASE_RET_STR(DW_AT_trampoline);
RT_CASE_RET_STR(DW_AT_call_column);
RT_CASE_RET_STR(DW_AT_call_file);
RT_CASE_RET_STR(DW_AT_call_line);
RT_CASE_RET_STR(DW_AT_description);
RT_CASE_RET_STR(DW_AT_binary_scale);
RT_CASE_RET_STR(DW_AT_decimal_scale);
RT_CASE_RET_STR(DW_AT_small);
RT_CASE_RET_STR(DW_AT_decimal_sign);
RT_CASE_RET_STR(DW_AT_digit_count);
RT_CASE_RET_STR(DW_AT_picture_string);
RT_CASE_RET_STR(DW_AT_mutable);
RT_CASE_RET_STR(DW_AT_threads_scaled);
RT_CASE_RET_STR(DW_AT_explicit);
RT_CASE_RET_STR(DW_AT_object_pointer);
RT_CASE_RET_STR(DW_AT_endianity);
RT_CASE_RET_STR(DW_AT_elemental);
RT_CASE_RET_STR(DW_AT_pure);
RT_CASE_RET_STR(DW_AT_recursive);
RT_CASE_RET_STR(DW_AT_signature);
RT_CASE_RET_STR(DW_AT_main_subprogram);
RT_CASE_RET_STR(DW_AT_data_bit_offset);
RT_CASE_RET_STR(DW_AT_const_expr);
RT_CASE_RET_STR(DW_AT_enum_class);
RT_CASE_RET_STR(DW_AT_linkage_name);
RT_CASE_RET_STR(DW_AT_MIPS_linkage_name);
}
static char s_szStatic[32];
RTStrPrintf(s_szStatic, sizeof(s_szStatic),"DW_AT_%#x", uAttr);
return s_szStatic;
}
/**
* Turns a form value into a string for logging purposes.
*
* @returns String name.
* @param uForm The form.
*/
static const char *rtDwarfLog_FormName(uint32_t uForm)
{
switch (uForm)
{
RT_CASE_RET_STR(DW_FORM_addr);
RT_CASE_RET_STR(DW_FORM_block2);
RT_CASE_RET_STR(DW_FORM_block4);
RT_CASE_RET_STR(DW_FORM_data2);
RT_CASE_RET_STR(DW_FORM_data4);
RT_CASE_RET_STR(DW_FORM_data8);
RT_CASE_RET_STR(DW_FORM_string);
RT_CASE_RET_STR(DW_FORM_block);
RT_CASE_RET_STR(DW_FORM_block1);
RT_CASE_RET_STR(DW_FORM_data1);
RT_CASE_RET_STR(DW_FORM_flag);
RT_CASE_RET_STR(DW_FORM_sdata);
RT_CASE_RET_STR(DW_FORM_strp);
RT_CASE_RET_STR(DW_FORM_udata);
RT_CASE_RET_STR(DW_FORM_ref_addr);
RT_CASE_RET_STR(DW_FORM_ref1);
RT_CASE_RET_STR(DW_FORM_ref2);
RT_CASE_RET_STR(DW_FORM_ref4);
RT_CASE_RET_STR(DW_FORM_ref8);
RT_CASE_RET_STR(DW_FORM_ref_udata);
RT_CASE_RET_STR(DW_FORM_indirect);
RT_CASE_RET_STR(DW_FORM_sec_offset);
RT_CASE_RET_STR(DW_FORM_exprloc);
RT_CASE_RET_STR(DW_FORM_flag_present);
RT_CASE_RET_STR(DW_FORM_ref_sig8);
}
static char s_szStatic[32];
RTStrPrintf(s_szStatic, sizeof(s_szStatic),"DW_FORM_%#x", uForm);
return s_szStatic;
}
#endif /* LOG_ENABLED || RT_STRICT */
/** @callback_method_impl{FNRTLDRENUMSEGS} */
static DECLCALLBACK(int) rtDbgModDwarfScanSegmentsCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx\n",
pSeg->cchName, pSeg->pszName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb));
NOREF(hLdrMod);
/* Count relevant segments. */
if (pSeg->RVA != NIL_RTLDRADDR)
pThis->cSegs++;
return VINF_SUCCESS;
}
/** @callback_method_impl{FNRTLDRENUMSEGS} */
static DECLCALLBACK(int) rtDbgModDwarfAddSegmentsCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx cbMapped=%#llx\n",
pSeg->cchName, pSeg->pszName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb, pSeg->cbMapped));
NOREF(hLdrMod);
Assert(pSeg->cchName > 0);
Assert(!pSeg->pszName[pSeg->cchName]);
/* If the segment doesn't have a mapping, just add a dummy so the indexing
works out correctly (same as for the image). */
if (pSeg->RVA == NIL_RTLDRADDR)
return RTDbgModSegmentAdd(pThis->hCnt, 0, 0, pSeg->pszName, 0 /*fFlags*/, NULL);
/* The link address is 0 for all segments in a relocatable ELF image. */
RTLDRADDR cb = RT_MAX(pSeg->cb, pSeg->cbMapped);
return RTDbgModSegmentAdd(pThis->hCnt, pSeg->RVA, cb, pSeg->pszName, 0 /*fFlags*/, NULL);
}
/**
* Calls pfnSegmentAdd for each segment in the executable image.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
*/
static int rtDbgModDwarfAddSegmentsFromImage(PRTDBGMODDWARF pThis)
{
AssertReturn(pThis->pImgMod && pThis->pImgMod->pImgVt, VERR_INTERNAL_ERROR_2);
Assert(!pThis->cSegs);
int rc = pThis->pImgMod->pImgVt->pfnEnumSegments(pThis->pImgMod, rtDbgModDwarfScanSegmentsCallback, pThis);
if (RT_SUCCESS(rc))
{
if (pThis->cSegs == 0)
pThis->iWatcomPass = 1;
else
{
pThis->cSegs = 0;
pThis->iWatcomPass = -1;
rc = pThis->pImgMod->pImgVt->pfnEnumSegments(pThis->pImgMod, rtDbgModDwarfAddSegmentsCallback, pThis);
}
}
return rc;
}
/**
* Looks up a segment.
*
* @returns Pointer to the segment on success, NULL if not found.
* @param pThis The DWARF instance.
* @param uSeg The segment number / selector.
*/
static PRTDBGDWARFSEG rtDbgModDwarfFindSegment(PRTDBGMODDWARF pThis, RTSEL uSeg)
{
uint32_t cSegs = pThis->cSegs;
uint32_t iSeg = pThis->iSegHint;
PRTDBGDWARFSEG paSegs = pThis->paSegs;
if ( iSeg < cSegs
&& paSegs[iSeg].uSegment == uSeg)
return &paSegs[iSeg];
for (iSeg = 0; iSeg < cSegs; iSeg++)
if (uSeg == paSegs[iSeg].uSegment)
{
pThis->iSegHint = iSeg;
return &paSegs[iSeg];
}
AssertFailed();
return NULL;
}
/**
* Record a segment:offset during pass 1.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
* @param uSeg The segment number / selector.
* @param offSeg The segment offset.
*/
static int rtDbgModDwarfRecordSegOffset(PRTDBGMODDWARF pThis, RTSEL uSeg, uint64_t offSeg)
{
/* Look up the segment. */
uint32_t cSegs = pThis->cSegs;
uint32_t iSeg = pThis->iSegHint;
PRTDBGDWARFSEG paSegs = pThis->paSegs;
if ( iSeg >= cSegs
|| paSegs[iSeg].uSegment != uSeg)
{
for (iSeg = 0; iSeg < cSegs; iSeg++)
if (uSeg <= paSegs[iSeg].uSegment)
break;
if ( iSeg >= cSegs
|| paSegs[iSeg].uSegment != uSeg)
{
/* Add */
void *pvNew = RTMemRealloc(paSegs, (pThis->cSegs + 1) * sizeof(paSegs[0]));
if (!pvNew)
return VERR_NO_MEMORY;
pThis->paSegs = paSegs = (PRTDBGDWARFSEG)pvNew;
if (iSeg != cSegs)
memmove(&paSegs[iSeg + 1], &paSegs[iSeg], (cSegs - iSeg) * sizeof(paSegs[0]));
paSegs[iSeg].offHighest = offSeg;
paSegs[iSeg].uBaseAddr = 0;
paSegs[iSeg].cbSegment = 0;
paSegs[iSeg].uSegment = uSeg;
pThis->cSegs++;
}
pThis->iSegHint = iSeg;
}
/* Increase it's range? */
if (paSegs[iSeg].offHighest < offSeg)
{
Log3(("rtDbgModDwarfRecordSegOffset: iSeg=%d uSeg=%#06x offSeg=%#llx\n", iSeg, uSeg, offSeg));
paSegs[iSeg].offHighest = offSeg;
}
return VINF_SUCCESS;
}
/**
* Calls pfnSegmentAdd for each segment in the executable image.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
*/
static int rtDbgModDwarfAddSegmentsFromPass1(PRTDBGMODDWARF pThis)
{
AssertReturn(pThis->cSegs, VERR_DWARF_BAD_INFO);
uint32_t const cSegs = pThis->cSegs;
PRTDBGDWARFSEG paSegs = pThis->paSegs;
/*
* Are the segments assigned more or less in numerical order?
*/
if ( paSegs[0].uSegment < 16U
&& paSegs[cSegs - 1].uSegment - paSegs[0].uSegment + 1U <= cSegs + 16U)
{
/** @todo heuristics, plase. */
AssertFailedReturn(VERR_DWARF_TODO);
}
/*
* Assume DOS segmentation.
*/
else
{
for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
paSegs[iSeg].uBaseAddr = (uint32_t)paSegs[iSeg].uSegment << 16;
for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
paSegs[iSeg].cbSegment = paSegs[iSeg].offHighest;
}
/*
* Add them.
*/
for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
{
Log3(("rtDbgModDwarfAddSegmentsFromPass1: Seg#%u: %#010llx LB %#llx uSegment=%#x\n",
iSeg, paSegs[iSeg].uBaseAddr, paSegs[iSeg].cbSegment, paSegs[iSeg].uSegment));
char szName[32];
RTStrPrintf(szName, sizeof(szName), "seg-%#04xh", paSegs[iSeg].uSegment);
int rc = RTDbgModSegmentAdd(pThis->hCnt, paSegs[iSeg].uBaseAddr, paSegs[iSeg].cbSegment,
szName, 0 /*fFlags*/, NULL);
if (RT_FAILURE(rc))
return rc;
}
return VINF_SUCCESS;
}
/**
* Loads a DWARF section from the image file.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
* @param enmSect The section to load.
*/
static int rtDbgModDwarfLoadSection(PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
{
/*
* Don't load stuff twice.
*/
if (pThis->aSections[enmSect].pv)
return VINF_SUCCESS;
/*
* Sections that are not present cannot be loaded, treat them like they
* are empty
*/
if (!pThis->aSections[enmSect].fPresent)
{
Assert(pThis->aSections[enmSect].cb);
return VINF_SUCCESS;
}
if (!pThis->aSections[enmSect].cb)
return VINF_SUCCESS;
/*
* Sections must be readable with the current image interface.
*/
if (pThis->aSections[enmSect].offFile < 0)
return VERR_OUT_OF_RANGE;
/*
* Do the job.
*/
return pThis->pDbgInfoMod->pImgVt->pfnMapPart(pThis->pDbgInfoMod,
pThis->aSections[enmSect].iDbgInfo,
pThis->aSections[enmSect].offFile,
pThis->aSections[enmSect].cb,
&pThis->aSections[enmSect].pv);
}
#ifdef SOME_UNUSED_FUNCTION
/**
* Unloads a DWARF section previously mapped by rtDbgModDwarfLoadSection.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
* @param enmSect The section to unload.
*/
static int rtDbgModDwarfUnloadSection(PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
{
if (!pThis->aSections[enmSect].pv)
return VINF_SUCCESS;
int rc = pThis->pDbgInfoMod->pImgVt->pfnUnmapPart(pThis->pDbgInfoMod, pThis->aSections[enmSect].cb, &pThis->aSections[enmSect].pv);
AssertRC(rc);
return rc;
}
#endif
/**
* Converts to UTF-8 or otherwise makes sure it's valid UTF-8.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
* @param ppsz Pointer to the string pointer. May be
* reallocated (RTStr*).
*/
static int rtDbgModDwarfStringToUtf8(PRTDBGMODDWARF pThis, char **ppsz)
{
/** @todo DWARF & UTF-8. */
NOREF(pThis);
RTStrPurgeEncoding(*ppsz);
return VINF_SUCCESS;
}
/**
* Convers a link address into a segment+offset or RVA.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
* @param uSegment The segment, 0 if not applicable.
* @param LinkAddress The address to convert..
* @param piSeg The segment index.
* @param poffSeg Where to return the segment offset.
*/
static int rtDbgModDwarfLinkAddressToSegOffset(PRTDBGMODDWARF pThis, RTSEL uSegment, uint64_t LinkAddress,
PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
{
if (pThis->paSegs)
{
PRTDBGDWARFSEG pSeg = rtDbgModDwarfFindSegment(pThis, uSegment);
if (pSeg)
{
*piSeg = pSeg - pThis->paSegs;
*poffSeg = LinkAddress;
return VINF_SUCCESS;
}
}
if (pThis->fUseLinkAddress)
return pThis->pImgMod->pImgVt->pfnLinkAddressToSegOffset(pThis->pImgMod, LinkAddress, piSeg, poffSeg);
return pThis->pImgMod->pImgVt->pfnRvaToSegOffset(pThis->pImgMod, LinkAddress, piSeg, poffSeg);
}
/*
*
* DWARF Cursor.
* DWARF Cursor.
* DWARF Cursor.
*
*/
/**
* Reads a 8-bit unsigned integer and advances the cursor.
*
* @returns 8-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue What to return on read error.
*/
static uint8_t rtDwarfCursor_GetU8(PRTDWARFCURSOR pCursor, uint8_t uErrValue)
{
if (pCursor->cbUnitLeft < 1)
{
pCursor->rc = VERR_DWARF_UNEXPECTED_END;
return uErrValue;
}
uint8_t u8 = pCursor->pb[0];
pCursor->pb += 1;
pCursor->cbUnitLeft -= 1;
pCursor->cbLeft -= 1;
return u8;
}
/**
* Reads a 16-bit unsigned integer and advances the cursor.
*
* @returns 16-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue What to return on read error.
*/
static uint16_t rtDwarfCursor_GetU16(PRTDWARFCURSOR pCursor, uint16_t uErrValue)
{
if (pCursor->cbUnitLeft < 2)
{
pCursor->pb += pCursor->cbUnitLeft;
pCursor->cbLeft -= pCursor->cbUnitLeft;
pCursor->cbUnitLeft = 0;
pCursor->rc = VERR_DWARF_UNEXPECTED_END;
return uErrValue;
}
uint16_t u16 = RT_MAKE_U16(pCursor->pb[0], pCursor->pb[1]);
pCursor->pb += 2;
pCursor->cbUnitLeft -= 2;
pCursor->cbLeft -= 2;
if (!pCursor->fNativEndian)
u16 = RT_BSWAP_U16(u16);
return u16;
}
/**
* Reads a 32-bit unsigned integer and advances the cursor.
*
* @returns 32-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue What to return on read error.
*/
static uint32_t rtDwarfCursor_GetU32(PRTDWARFCURSOR pCursor, uint32_t uErrValue)
{
if (pCursor->cbUnitLeft < 4)
{
pCursor->pb += pCursor->cbUnitLeft;
pCursor->cbLeft -= pCursor->cbUnitLeft;
pCursor->cbUnitLeft = 0;
pCursor->rc = VERR_DWARF_UNEXPECTED_END;
return uErrValue;
}
uint32_t u32 = RT_MAKE_U32_FROM_U8(pCursor->pb[0], pCursor->pb[1], pCursor->pb[2], pCursor->pb[3]);
pCursor->pb += 4;
pCursor->cbUnitLeft -= 4;
pCursor->cbLeft -= 4;
if (!pCursor->fNativEndian)
u32 = RT_BSWAP_U32(u32);
return u32;
}
/**
* Reads a 64-bit unsigned integer and advances the cursor.
*
* @returns 64-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue What to return on read error.
*/
static uint64_t rtDwarfCursor_GetU64(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
{
if (pCursor->cbUnitLeft < 8)
{
pCursor->pb += pCursor->cbUnitLeft;
pCursor->cbLeft -= pCursor->cbUnitLeft;
pCursor->cbUnitLeft = 0;
pCursor->rc = VERR_DWARF_UNEXPECTED_END;
return uErrValue;
}
uint64_t u64 = RT_MAKE_U64_FROM_U8(pCursor->pb[0], pCursor->pb[1], pCursor->pb[2], pCursor->pb[3],
pCursor->pb[4], pCursor->pb[5], pCursor->pb[6], pCursor->pb[7]);
pCursor->pb += 8;
pCursor->cbUnitLeft -= 8;
pCursor->cbLeft -= 8;
if (!pCursor->fNativEndian)
u64 = RT_BSWAP_U64(u64);
return u64;
}
/**
* Reads an unsigned LEB128 encoded number.
*
* @returns unsigned 64-bit number. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue The value to return on error.
*/
static uint64_t rtDwarfCursor_GetULeb128(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
{
if (pCursor->cbUnitLeft < 1)
{
pCursor->rc = VERR_DWARF_UNEXPECTED_END;
return uErrValue;
}
/*
* Special case - single byte.
*/
uint8_t b = pCursor->pb[0];
if (!(b & 0x80))
{
pCursor->pb += 1;
pCursor->cbUnitLeft -= 1;
pCursor->cbLeft -= 1;
return b;
}
/*
* Generic case.
*/
/* Decode. */
uint32_t off = 1;
uint64_t u64Ret = b & 0x7f;
do
{
if (off == pCursor->cbUnitLeft)
{
pCursor->rc = VERR_DWARF_UNEXPECTED_END;
u64Ret = uErrValue;
break;
}
b = pCursor->pb[off];
u64Ret |= (b & 0x7f) << off * 7;
off++;
} while (b & 0x80);
/* Update the cursor. */
pCursor->pb += off;
pCursor->cbUnitLeft -= off;
pCursor->cbLeft -= off;
/* Check the range. */
uint32_t cBits = off * 7;
if (cBits > 64)
{
pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
u64Ret = uErrValue;
}
return u64Ret;
}
/**
* Reads a signed LEB128 encoded number.
*
* @returns signed 64-bit number. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param sErrValue The value to return on error.
*/
static int64_t rtDwarfCursor_GetSLeb128(PRTDWARFCURSOR pCursor, int64_t sErrValue)
{
if (pCursor->cbUnitLeft < 1)
{
pCursor->rc = VERR_DWARF_UNEXPECTED_END;
return sErrValue;
}
/*
* Special case - single byte.
*/
uint8_t b = pCursor->pb[0];
if (!(b & 0x80))
{
pCursor->pb += 1;
pCursor->cbUnitLeft -= 1;
pCursor->cbLeft -= 1;
if (b & 0x40)
b |= 0x80;
return (int8_t)b;
}
/*
* Generic case.
*/
/* Decode it. */
uint32_t off = 1;
uint64_t u64Ret = b & 0x7f;
do
{
if (off == pCursor->cbUnitLeft)
{
pCursor->rc = VERR_DWARF_UNEXPECTED_END;
u64Ret = (uint64_t)sErrValue;
break;
}
b = pCursor->pb[off];
u64Ret |= (b & 0x7f) << off * 7;
off++;
} while (b & 0x80);
/* Update cursor. */
pCursor->pb += off;
pCursor->cbUnitLeft -= off;
pCursor->cbLeft -= off;
/* Check the range. */
uint32_t cBits = off * 7;
if (cBits > 64)
{
pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
u64Ret = (uint64_t)sErrValue;
}
/* Sign extend the value. */
else if (u64Ret & RT_BIT_64(cBits - 1))
u64Ret |= ~(RT_BIT_64(cBits - 1) - 1);
return (int64_t)u64Ret;
}
/**
* Reads an unsigned LEB128 encoded number, max 32-bit width.
*
* @returns unsigned 32-bit number. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue The value to return on error.
*/
static uint32_t rtDwarfCursor_GetULeb128AsU32(PRTDWARFCURSOR pCursor, uint32_t uErrValue)
{
uint64_t u64 = rtDwarfCursor_GetULeb128(pCursor, uErrValue);
if (u64 > UINT32_MAX)
{
pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
return uErrValue;
}
return (uint32_t)u64;
}
/**
* Reads a signed LEB128 encoded number, max 32-bit width.
*
* @returns signed 32-bit number. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param sErrValue The value to return on error.
*/
static int32_t rtDwarfCursor_GetSLeb128AsS32(PRTDWARFCURSOR pCursor, int32_t sErrValue)
{
int64_t s64 = rtDwarfCursor_GetSLeb128(pCursor, sErrValue);
if (s64 > INT32_MAX || s64 < INT32_MIN)
{
pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
return sErrValue;
}
return (int32_t)s64;
}
/**
* Skips a LEB128 encoded number.
*
* @returns IPRT status code.
* @param pCursor The cursor.
*/
static int rtDwarfCursor_SkipLeb128(PRTDWARFCURSOR pCursor)
{
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
if (pCursor->cbUnitLeft < 1)
return pCursor->rc = VERR_DWARF_UNEXPECTED_END;
uint32_t offSkip = 1;
if (pCursor->pb[0] & 0x80)
do
{
if (offSkip == pCursor->cbUnitLeft)
{
pCursor->rc = VERR_DWARF_UNEXPECTED_END;
break;
}
} while (pCursor->pb[offSkip++] & 0x80);
pCursor->pb += offSkip;
pCursor->cbUnitLeft -= offSkip;
pCursor->cbLeft -= offSkip;
return pCursor->rc;
}
/**
* Advances the cursor a given number of bytes.
*
* @returns IPRT status code.
* @param pCursor The cursor.
* @param offSkip The number of bytes to advance.
*/
static int rtDwarfCursor_SkipBytes(PRTDWARFCURSOR pCursor, uint64_t offSkip)
{
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
if (pCursor->cbUnitLeft < offSkip)
return pCursor->rc = VERR_DWARF_UNEXPECTED_END;
size_t const offSkipSizeT = (size_t)offSkip;
pCursor->cbUnitLeft -= offSkipSizeT;
pCursor->cbLeft -= offSkipSizeT;
pCursor->pb += offSkipSizeT;
return VINF_SUCCESS;
}
/**
* Reads a zero terminated string, advancing the cursor beyond the terminator.
*
* @returns Pointer to the string.
* @param pCursor The cursor.
* @param pszErrValue What to return if the string isn't terminated
* before the end of the unit.
*/
static const char *rtDwarfCursor_GetSZ(PRTDWARFCURSOR pCursor, const char *pszErrValue)
{
const char *pszRet = (const char *)pCursor->pb;
for (;;)
{
if (!pCursor->cbUnitLeft)
{
pCursor->rc = VERR_DWARF_BAD_STRING;
return pszErrValue;
}
pCursor->cbUnitLeft--;
pCursor->cbLeft--;
if (!*pCursor->pb++)
break;
}
return pszRet;
}
/**
* Reads a 1, 2, 4 or 8 byte unsgined value.
*
* @returns 64-bit unsigned value.
* @param pCursor The cursor.
* @param cbValue The value size.
* @param uErrValue The error value.
*/
static uint64_t rtDwarfCursor_GetVarSizedU(PRTDWARFCURSOR pCursor, size_t cbValue, uint64_t uErrValue)
{
uint64_t u64Ret;
switch (cbValue)
{
case 1: u64Ret = rtDwarfCursor_GetU8( pCursor, UINT8_MAX); break;
case 2: u64Ret = rtDwarfCursor_GetU16(pCursor, UINT16_MAX); break;
case 4: u64Ret = rtDwarfCursor_GetU32(pCursor, UINT32_MAX); break;
case 8: u64Ret = rtDwarfCursor_GetU64(pCursor, UINT64_MAX); break;
default:
pCursor->rc = VERR_DWARF_BAD_INFO;
return uErrValue;
}
if (RT_FAILURE(pCursor->rc))
return uErrValue;
return u64Ret;
}
#if 0 /* unused */
/**
* Gets the pointer to a variable size block and advances the cursor.
*
* @returns Pointer to the block at the current cursor location. On error
* RTDWARFCURSOR::rc is set and NULL returned.
* @param pCursor The cursor.
* @param cbBlock The block size.
*/
static const uint8_t *rtDwarfCursor_GetBlock(PRTDWARFCURSOR pCursor, uint32_t cbBlock)
{
if (cbBlock > pCursor->cbUnitLeft)
{
pCursor->rc = VERR_DWARF_UNEXPECTED_END;
return NULL;
}
uint8_t const *pb = &pCursor->pb[0];
pCursor->pb += cbBlock;
pCursor->cbUnitLeft -= cbBlock;
pCursor->cbLeft -= cbBlock;
return pb;
}
#endif
/**
* Reads an unsigned DWARF half number.
*
* @returns The number. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue What to return on error.
*/
static uint16_t rtDwarfCursor_GetUHalf(PRTDWARFCURSOR pCursor, uint16_t uErrValue)
{
return rtDwarfCursor_GetU16(pCursor, uErrValue);
}
/**
* Reads an unsigned DWARF byte number.
*
* @returns The number. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue What to return on error.
*/
static uint8_t rtDwarfCursor_GetUByte(PRTDWARFCURSOR pCursor, uint8_t uErrValue)
{
return rtDwarfCursor_GetU8(pCursor, uErrValue);
}
/**
* Reads a signed DWARF byte number.
*
* @returns The number. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue What to return on error.
*/
static int8_t rtDwarfCursor_GetSByte(PRTDWARFCURSOR pCursor, int8_t iErrValue)
{
return (int8_t)rtDwarfCursor_GetU8(pCursor, (uint8_t)iErrValue);
}
/**
* Reads a unsigned DWARF offset value.
*
* @returns The value. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue What to return on error.
*/
static uint64_t rtDwarfCursor_GetUOff(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
{
if (pCursor->f64bitDwarf)
return rtDwarfCursor_GetU64(pCursor, uErrValue);
return rtDwarfCursor_GetU32(pCursor, (uint32_t)uErrValue);
}
/**
* Reads a unsigned DWARF native offset value.
*
* @returns The value. On error RTDWARFCURSOR::rc is set and @a
* uErrValue is returned.
* @param pCursor The cursor.
* @param uErrValue What to return on error.
*/
static uint64_t rtDwarfCursor_GetNativeUOff(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
{
switch (pCursor->cbNativeAddr)
{
case 1: return rtDwarfCursor_GetU8(pCursor, (uint8_t )uErrValue);
case 2: return rtDwarfCursor_GetU16(pCursor, (uint16_t)uErrValue);
case 4: return rtDwarfCursor_GetU32(pCursor, (uint32_t)uErrValue);
case 8: return rtDwarfCursor_GetU64(pCursor, uErrValue);
default:
pCursor->rc = VERR_INTERNAL_ERROR_2;
return uErrValue;
}
}
/**
* Gets the unit length, updating the unit length member and DWARF bitness
* members of the cursor.
*
* @returns The unit length.
* @param pCursor The cursor.
*/
static uint64_t rtDwarfCursor_GetInitalLength(PRTDWARFCURSOR pCursor)
{
/*
* Read the initial length.
*/
pCursor->cbUnitLeft = pCursor->cbLeft;
uint64_t cbUnit = rtDwarfCursor_GetU32(pCursor, 0);
if (cbUnit != UINT32_C(0xffffffff))
pCursor->f64bitDwarf = false;
else
{
pCursor->f64bitDwarf = true;
cbUnit = rtDwarfCursor_GetU64(pCursor, 0);
}
/*
* Set the unit length, quitely fixing bad lengths.
*/
pCursor->cbUnitLeft = (size_t)cbUnit;
if ( pCursor->cbUnitLeft > pCursor->cbLeft
|| pCursor->cbUnitLeft != cbUnit)
pCursor->cbUnitLeft = pCursor->cbLeft;
return cbUnit;
}
/**
* Calculates the section offset corresponding to the current cursor position.
*
* @returns 32-bit section offset. If out of range, RTDWARFCURSOR::rc will be
* set and UINT32_MAX returned.
* @param pCursor The cursor.
*/
static uint32_t rtDwarfCursor_CalcSectOffsetU32(PRTDWARFCURSOR pCursor)
{
size_t off = pCursor->pb - (uint8_t const *)pCursor->pDwarfMod->aSections[pCursor->enmSect].pv;
uint32_t offRet = (uint32_t)off;
if (offRet != off)
{
AssertFailed();
pCursor->rc = VERR_OUT_OF_RANGE;
offRet = UINT32_MAX;
}
return offRet;
}
/**
* Calculates an absolute cursor position from one relative to the current
* cursor position.
*
* @returns The absolute cursor position.
* @param pCursor The cursor.
* @param offRelative The relative position. Must be a positive
* offset.
*/
static uint8_t const *rtDwarfCursor_CalcPos(PRTDWARFCURSOR pCursor, size_t offRelative)
{
if (offRelative > pCursor->cbUnitLeft)
{
Log(("rtDwarfCursor_CalcPos: bad position %#zx, cbUnitLeft=%#zu\n", offRelative, pCursor->cbUnitLeft));
pCursor->rc = VERR_DWARF_BAD_POS;
return NULL;
}
return pCursor->pb + offRelative;
}
/**
* Advances the cursor to the given position.
*
* @returns IPRT status code.
* @param pCursor The cursor.
* @param pbNewPos The new position - returned by
* rtDwarfCursor_CalcPos().
*/
static int rtDwarfCursor_AdvanceToPos(PRTDWARFCURSOR pCursor, uint8_t const *pbNewPos)
{
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
AssertPtr(pbNewPos);
if ((uintptr_t)pbNewPos < (uintptr_t)pCursor->pb)
{
Log(("rtDwarfCursor_AdvanceToPos: bad position %p, current %p\n", pbNewPos, pCursor->pb));
return pCursor->rc = VERR_DWARF_BAD_POS;
}
uintptr_t cbAdj = (uintptr_t)pbNewPos - (uintptr_t)pCursor->pb;
if (RT_UNLIKELY(cbAdj > pCursor->cbUnitLeft))
{
AssertFailed();
pCursor->rc = VERR_DWARF_BAD_POS;
cbAdj = pCursor->cbUnitLeft;
}
pCursor->cbUnitLeft -= cbAdj;
pCursor->cbLeft -= cbAdj;
pCursor->pb += cbAdj;
return pCursor->rc;
}
/**
* Check if the cursor is at the end of the current DWARF unit.
*
* @retval @c true if at the end or a cursor error is pending.
* @retval @c false if not.
* @param pCursor The cursor.
*/
static bool rtDwarfCursor_IsAtEndOfUnit(PRTDWARFCURSOR pCursor)
{
return !pCursor->cbUnitLeft || RT_FAILURE(pCursor->rc);
}
/**
* Skips to the end of the current unit.
*
* @returns IPRT status code.
* @param pCursor The cursor.
*/
static int rtDwarfCursor_SkipUnit(PRTDWARFCURSOR pCursor)
{
pCursor->pb += pCursor->cbUnitLeft;
pCursor->cbLeft -= pCursor->cbUnitLeft;
pCursor->cbUnitLeft = 0;
return pCursor->rc;
}
/**
* Check if the cursor is at the end of the section (or whatever the cursor is
* processing).
*
* @retval @c true if at the end or a cursor error is pending.
* @retval @c false if not.
* @param pCursor The cursor.
*/
static bool rtDwarfCursor_IsAtEnd(PRTDWARFCURSOR pCursor)
{
return !pCursor->cbLeft || RT_FAILURE(pCursor->rc);
}
/**
* Initialize a section reader cursor.
*
* @returns IPRT status code.
* @param pCursor The cursor.
* @param pThis The dwarf module.
* @param enmSect The name of the section to read.
*/
static int rtDwarfCursor_Init(PRTDWARFCURSOR pCursor, PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
{
int rc = rtDbgModDwarfLoadSection(pThis, enmSect);
if (RT_FAILURE(rc))
return rc;
pCursor->enmSect = enmSect;
pCursor->pbStart = (uint8_t const *)pThis->aSections[enmSect].pv;
pCursor->pb = pCursor->pbStart;
pCursor->cbLeft = pThis->aSections[enmSect].cb;
pCursor->cbUnitLeft = pCursor->cbLeft;
pCursor->pDwarfMod = pThis;
pCursor->f64bitDwarf = false;
/** @todo ask the image about the endian used as well as the address
* width. */
pCursor->fNativEndian = true;
pCursor->cbNativeAddr = 4;
pCursor->rc = VINF_SUCCESS;
return VINF_SUCCESS;
}
/**
* Initialize a section reader cursor with an offset.
*
* @returns IPRT status code.
* @param pCursor The cursor.
* @param pThis The dwarf module.
* @param enmSect The name of the section to read.
* @param offSect The offset into the section.
*/
static int rtDwarfCursor_InitWithOffset(PRTDWARFCURSOR pCursor, PRTDBGMODDWARF pThis,
krtDbgModDwarfSect enmSect, uint32_t offSect)
{
if (offSect > pThis->aSections[enmSect].cb)
{
Log(("rtDwarfCursor_InitWithOffset: offSect=%#x cb=%#x enmSect=%d\n", offSect, pThis->aSections[enmSect].cb, enmSect));
return VERR_DWARF_BAD_POS;
}
int rc = rtDwarfCursor_Init(pCursor, pThis, enmSect);
if (RT_SUCCESS(rc))
{
pCursor->pbStart += offSect;
pCursor->pb += offSect;
pCursor->cbLeft -= offSect;
pCursor->cbUnitLeft -= offSect;
}
return rc;
}
/**
* Initialize a cursor for a block (subsection) retrieved from the given cursor.
*
* The parent cursor will be advanced past the block.
*
* @returns IPRT status code.
* @param pCursor The cursor.
* @param pParent The parent cursor. Will be moved by @a cbBlock.
* @param cbBlock The size of the block the new cursor should
* cover.
*/
static int rtDwarfCursor_InitForBlock(PRTDWARFCURSOR pCursor, PRTDWARFCURSOR pParent, uint32_t cbBlock)
{
if (RT_FAILURE(pParent->rc))
return pParent->rc;
if (pParent->cbUnitLeft < cbBlock)
{
Log(("rtDwarfCursor_InitForBlock: cbUnitLeft=%#x < cbBlock=%#x \n", pParent->cbUnitLeft, cbBlock));
return VERR_DWARF_BAD_POS;
}
*pCursor = *pParent;
pCursor->cbLeft = cbBlock;
pCursor->cbUnitLeft = cbBlock;
pParent->pb += cbBlock;
pParent->cbLeft -= cbBlock;
pParent->cbUnitLeft -= cbBlock;
return VINF_SUCCESS;
}
/**
* Deletes a section reader initialized by rtDwarfCursor_Init.
*
* @returns @a rcOther or RTDWARCURSOR::rc.
* @param pCursor The section reader.
* @param rcOther Other error code to be returned if it indicates
* error or if the cursor status is OK.
*/
static int rtDwarfCursor_Delete(PRTDWARFCURSOR pCursor, int rcOther)
{
/* ... and a drop of poison. */
pCursor->pb = NULL;
pCursor->cbLeft = ~(size_t)0;
pCursor->cbUnitLeft = ~(size_t)0;
pCursor->pDwarfMod = NULL;
if (RT_FAILURE(pCursor->rc) && RT_SUCCESS(rcOther))
rcOther = pCursor->rc;
pCursor->rc = VERR_INTERNAL_ERROR_4;
return rcOther;
}
/*
*
* DWARF Line Numbers.
* DWARF Line Numbers.
* DWARF Line Numbers.
*
*/
/**
* Defines a file name.
*
* @returns IPRT status code.
* @param pLnState The line number program state.
* @param pszFilename The name of the file.
* @param idxInc The include path index.
*/
static int rtDwarfLine_DefineFileName(PRTDWARFLINESTATE pLnState, const char *pszFilename, uint64_t idxInc)
{
/*
* Resize the array if necessary.
*/
uint32_t iFileName = pLnState->cFileNames;
if ((iFileName % 2) == 0)
{
void *pv = RTMemRealloc(pLnState->papszFileNames, sizeof(pLnState->papszFileNames[0]) * (iFileName + 2));
if (!pv)
return VERR_NO_MEMORY;
pLnState->papszFileNames = (char **)pv;
}
/*
* Add the file name.
*/
if ( pszFilename[0] == '/'
|| pszFilename[0] == '\\'
|| (RT_C_IS_ALPHA(pszFilename[0]) && pszFilename[1] == ':') )
pLnState->papszFileNames[iFileName] = RTStrDup(pszFilename);
else if (idxInc < pLnState->cIncPaths)
pLnState->papszFileNames[iFileName] = RTPathJoinA(pLnState->papszIncPaths[idxInc], pszFilename);
else
return VERR_DWARF_BAD_LINE_NUMBER_HEADER;
if (!pLnState->papszFileNames[iFileName])
return VERR_NO_STR_MEMORY;
pLnState->cFileNames = iFileName + 1;
/*
* Sanitize the name.
*/
int rc = rtDbgModDwarfStringToUtf8(pLnState->pDwarfMod, &pLnState->papszFileNames[iFileName]);
Log((" File #%02u = '%s'\n", iFileName, pLnState->papszFileNames[iFileName]));
return rc;
}
/**
* Adds a line to the table and resets parts of the state (DW_LNS_copy).
*
* @returns IPRT status code
* @param pLnState The line number program state.
* @param offOpCode The opcode offset (for logging
* purposes).
*/
static int rtDwarfLine_AddLine(PRTDWARFLINESTATE pLnState, uint32_t offOpCode)
{
PRTDBGMODDWARF pThis = pLnState->pDwarfMod;
int rc;
if (pThis->iWatcomPass == 1)
rc = rtDbgModDwarfRecordSegOffset(pThis, pLnState->Regs.uSegment, pLnState->Regs.uAddress + 1);
else
{
const char *pszFile = pLnState->Regs.iFile < pLnState->cFileNames
? pLnState->papszFileNames[pLnState->Regs.iFile]
: "<bad file name index>";
NOREF(offOpCode);
RTDBGSEGIDX iSeg;
RTUINTPTR offSeg;
rc = rtDbgModDwarfLinkAddressToSegOffset(pLnState->pDwarfMod, pLnState->Regs.uSegment, pLnState->Regs.uAddress,
&iSeg, &offSeg); /*AssertRC(rc);*/
if (RT_SUCCESS(rc))
{
Log2(("rtDwarfLine_AddLine: %x:%08llx (%#llx) %s(%d) [offOpCode=%08x]\n", iSeg, offSeg, pLnState->Regs.uAddress, pszFile, pLnState->Regs.uLine, offOpCode));
rc = RTDbgModLineAdd(pLnState->pDwarfMod->hCnt, pszFile, pLnState->Regs.uLine, iSeg, offSeg, NULL);
/* Ignore address conflicts for now. */
if (rc == VERR_DBG_ADDRESS_CONFLICT)
rc = VINF_SUCCESS;
}
else
rc = VINF_SUCCESS; /* ignore failure */
}
pLnState->Regs.fBasicBlock = false;
pLnState->Regs.fPrologueEnd = false;
pLnState->Regs.fEpilogueBegin = false;
pLnState->Regs.uDiscriminator = 0;
return rc;
}
/**
* Reset the program to the start-of-sequence state.
*
* @param pLnState The line number program state.
*/
static void rtDwarfLine_ResetState(PRTDWARFLINESTATE pLnState)
{
pLnState->Regs.uAddress = 0;
pLnState->Regs.idxOp = 0;
pLnState->Regs.iFile = 1;
pLnState->Regs.uLine = 1;
pLnState->Regs.uColumn = 0;
pLnState->Regs.fIsStatement = RT_BOOL(pLnState->Hdr.u8DefIsStmt);
pLnState->Regs.fBasicBlock = false;
pLnState->Regs.fEndSequence = false;
pLnState->Regs.fPrologueEnd = false;
pLnState->Regs.fEpilogueBegin = false;
pLnState->Regs.uIsa = 0;
pLnState->Regs.uDiscriminator = 0;
pLnState->Regs.uSegment = 0;
}
/**
* Runs the line number program.
*
* @returns IPRT status code.
* @param pLnState The line number program state.
* @param pCursor The cursor.
*/
static int rtDwarfLine_RunProgram(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
{
LogFlow(("rtDwarfLine_RunProgram: cbUnitLeft=%zu\n", pCursor->cbUnitLeft));
int rc = VINF_SUCCESS;
rtDwarfLine_ResetState(pLnState);
while (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
{
#ifdef LOG_ENABLED
uint32_t const offOpCode = rtDwarfCursor_CalcSectOffsetU32(pCursor);
#else
uint32_t const offOpCode = 0;
#endif
uint8_t bOpCode = rtDwarfCursor_GetUByte(pCursor, DW_LNS_extended);
if (bOpCode >= pLnState->Hdr.u8OpcodeBase)
{
/*
* Special opcode.
*/
uint8_t const bLogOpCode = bOpCode; NOREF(bLogOpCode);
bOpCode -= pLnState->Hdr.u8OpcodeBase;
int32_t const cLineDelta = bOpCode % pLnState->Hdr.u8LineRange + (int32_t)pLnState->Hdr.s8LineBase;
bOpCode /= pLnState->Hdr.u8LineRange;
uint64_t uTmp = bOpCode + pLnState->Regs.idxOp;
uint64_t const cAddressDelta = uTmp / pLnState->Hdr.cMaxOpsPerInstr * pLnState->Hdr.cbMinInstr;
uint64_t const cOpIndexDelta = uTmp % pLnState->Hdr.cMaxOpsPerInstr;
pLnState->Regs.uLine += cLineDelta;
pLnState->Regs.uAddress += cAddressDelta;
pLnState->Regs.idxOp += cOpIndexDelta;
Log2(("%08x: DW Special Opcode %#04x: uLine + %d => %u; uAddress + %#llx => %#llx; idxOp + %#llx => %#llx\n",
offOpCode, bLogOpCode, cLineDelta, pLnState->Regs.uLine, cAddressDelta, pLnState->Regs.uAddress,
cOpIndexDelta, pLnState->Regs.idxOp));
rc = rtDwarfLine_AddLine(pLnState, offOpCode);
}
else
{
switch (bOpCode)
{
/*
* Standard opcode.
*/
case DW_LNS_copy:
Log2(("%08x: DW_LNS_copy\n", offOpCode));
rc = rtDwarfLine_AddLine(pLnState, offOpCode);
break;
case DW_LNS_advance_pc:
{
uint64_t u64Adv = rtDwarfCursor_GetULeb128(pCursor, 0);
pLnState->Regs.uAddress += (pLnState->Regs.idxOp + u64Adv) / pLnState->Hdr.cMaxOpsPerInstr
* pLnState->Hdr.cbMinInstr;
pLnState->Regs.idxOp += (pLnState->Regs.idxOp + u64Adv) % pLnState->Hdr.cMaxOpsPerInstr;
Log2(("%08x: DW_LNS_advance_pc: u64Adv=%#llx (%lld) )\n", offOpCode, u64Adv, u64Adv));
break;
}
case DW_LNS_advance_line:
{
int32_t cLineDelta = rtDwarfCursor_GetSLeb128AsS32(pCursor, 0);
pLnState->Regs.uLine += cLineDelta;
Log2(("%08x: DW_LNS_advance_line: uLine + %d => %u\n", offOpCode, cLineDelta, pLnState->Regs.uLine));
break;
}
case DW_LNS_set_file:
pLnState->Regs.iFile = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
Log2(("%08x: DW_LNS_set_file: iFile=%u\n", offOpCode, pLnState->Regs.iFile));
break;
case DW_LNS_set_column:
pLnState->Regs.uColumn = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
Log2(("%08x: DW_LNS_set_column\n", offOpCode));
break;
case DW_LNS_negate_stmt:
pLnState->Regs.fIsStatement = !pLnState->Regs.fIsStatement;
Log2(("%08x: DW_LNS_negate_stmt\n", offOpCode));
break;
case DW_LNS_set_basic_block:
pLnState->Regs.fBasicBlock = true;
Log2(("%08x: DW_LNS_set_basic_block\n", offOpCode));
break;
case DW_LNS_const_add_pc:
pLnState->Regs.uAddress += (pLnState->Regs.idxOp + 255) / pLnState->Hdr.cMaxOpsPerInstr
* pLnState->Hdr.cbMinInstr;
pLnState->Regs.idxOp += (pLnState->Regs.idxOp + 255) % pLnState->Hdr.cMaxOpsPerInstr;
Log2(("%08x: DW_LNS_const_add_pc\n", offOpCode));
break;
case DW_LNS_fixed_advance_pc:
pLnState->Regs.uAddress += rtDwarfCursor_GetUHalf(pCursor, 0);
pLnState->Regs.idxOp = 0;
Log2(("%08x: DW_LNS_fixed_advance_pc\n", offOpCode));
break;
case DW_LNS_set_prologue_end:
pLnState->Regs.fPrologueEnd = true;
Log2(("%08x: DW_LNS_set_prologue_end\n", offOpCode));
break;
case DW_LNS_set_epilogue_begin:
pLnState->Regs.fEpilogueBegin = true;
Log2(("%08x: DW_LNS_set_epilogue_begin\n", offOpCode));
break;
case DW_LNS_set_isa:
pLnState->Regs.uIsa = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
Log2(("%08x: DW_LNS_set_isa %#x\n", offOpCode, pLnState->Regs.uIsa));
break;
default:
{
unsigned cOpsToSkip = pLnState->Hdr.pacStdOperands[bOpCode - 1];
Log(("rtDwarfLine_RunProgram: Unknown standard opcode %#x, %#x operands, at %08x.\n", bOpCode, cOpsToSkip, offOpCode));
while (cOpsToSkip-- > 0)
rc = rtDwarfCursor_SkipLeb128(pCursor);
break;
}
/*
* Extended opcode.
*/
case DW_LNS_extended:
{
/* The instruction has a length prefix. */
uint64_t cbInstr = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
if (cbInstr > pCursor->cbUnitLeft)
return VERR_DWARF_BAD_LNE;
uint8_t const * const pbEndOfInstr = rtDwarfCursor_CalcPos(pCursor, cbInstr);
/* Get the opcode and deal with it if we know it. */
bOpCode = rtDwarfCursor_GetUByte(pCursor, 0);
switch (bOpCode)
{
case DW_LNE_end_sequence:
#if 0 /* No need for this, I think. */
pLnState->Regs.fEndSequence = true;
rc = rtDwarfLine_AddLine(pLnState, offOpCode);
#endif
rtDwarfLine_ResetState(pLnState);
Log2(("%08x: DW_LNE_end_sequence\n", offOpCode));
break;
case DW_LNE_set_address:
pLnState->Regs.uAddress = rtDwarfCursor_GetVarSizedU(pCursor, cbInstr - 1, UINT64_MAX);
pLnState->Regs.idxOp = 0;
Log2(("%08x: DW_LNE_set_address: %#llx\n", offOpCode, pLnState->Regs.uAddress));
break;
case DW_LNE_define_file:
{
const char *pszFilename = rtDwarfCursor_GetSZ(pCursor, NULL);
uint32_t idxInc = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
rtDwarfCursor_SkipLeb128(pCursor); /* st_mtime */
rtDwarfCursor_SkipLeb128(pCursor); /* st_size */
Log2(("%08x: DW_LNE_define_file: {%d}/%s\n", offOpCode, idxInc, pszFilename));
rc = rtDwarfCursor_AdvanceToPos(pCursor, pbEndOfInstr);
if (RT_SUCCESS(rc))
rc = rtDwarfLine_DefineFileName(pLnState, pszFilename, idxInc);
break;
}
/*
* Note! Was defined in DWARF 4. But... Watcom used it
* for setting the segment in DWARF 2, creating
* an incompatibility with the newer standard.
*/
case DW_LNE_set_descriminator:
if (pLnState->Hdr.uVer != 2)
{
Assert(pLnState->Hdr.uVer >= 4);
pLnState->Regs.uDiscriminator = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
Log2(("%08x: DW_LNE_set_descriminator: %u\n", offOpCode, pLnState->Regs.uDiscriminator));
}
else
{
uint64_t uSeg = rtDwarfCursor_GetVarSizedU(pCursor, cbInstr - 1, UINT64_MAX);
Log2(("%08x: DW_LNE_set_segment: %#llx, cbInstr=%#x - Watcom Extension\n", offOpCode, uSeg, cbInstr));
pLnState->Regs.uSegment = (RTSEL)uSeg;
AssertStmt(pLnState->Regs.uSegment == uSeg, rc = VERR_DWARF_BAD_INFO);
}
break;
default:
Log(("rtDwarfLine_RunProgram: Unknown extended opcode %#x, length %#x at %08x\n", bOpCode, cbInstr, offOpCode));
break;
}
/* Advance the cursor to the end of the instruction . */
rtDwarfCursor_AdvanceToPos(pCursor, pbEndOfInstr);
break;
}
}
}
/*
* Check the status before looping.
*/
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
}
return rc;
}
/**
* Reads the include directories for a line number unit.
*
* @returns IPRT status code
* @param pLnState The line number program state.
* @param pCursor The cursor.
*/
static int rtDwarfLine_ReadFileNames(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
{
int rc = rtDwarfLine_DefineFileName(pLnState, "/<bad-zero-file-name-entry>", 0);
if (RT_FAILURE(rc))
return rc;
for (;;)
{
const char *psz = rtDwarfCursor_GetSZ(pCursor, NULL);
if (!*psz)
break;
uint64_t idxInc = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
rtDwarfCursor_SkipLeb128(pCursor); /* st_mtime */
rtDwarfCursor_SkipLeb128(pCursor); /* st_size */
rc = rtDwarfLine_DefineFileName(pLnState, psz, idxInc);
if (RT_FAILURE(rc))
return rc;
}
return pCursor->rc;
}
/**
* Reads the include directories for a line number unit.
*
* @returns IPRT status code
* @param pLnState The line number program state.
* @param pCursor The cursor.
*/
static int rtDwarfLine_ReadIncludePaths(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
{
const char *psz = ""; /* The zeroth is the unit dir. */
for (;;)
{
if ((pLnState->cIncPaths % 2) == 0)
{
void *pv = RTMemRealloc(pLnState->papszIncPaths, sizeof(pLnState->papszIncPaths[0]) * (pLnState->cIncPaths + 2));
if (!pv)
return VERR_NO_MEMORY;
pLnState->papszIncPaths = (const char **)pv;
}
Log((" Path #%02u = '%s'\n", pLnState->cIncPaths, psz));
pLnState->papszIncPaths[pLnState->cIncPaths] = psz;
pLnState->cIncPaths++;
psz = rtDwarfCursor_GetSZ(pCursor, NULL);
if (!*psz)
break;
}
return pCursor->rc;
}
/**
* Explodes the line number table for a compilation unit.
*
* @returns IPRT status code
* @param pThis The DWARF instance.
* @param pCursor The cursor to read the line number information
* via.
*/
static int rtDwarfLine_ExplodeUnit(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor)
{
RTDWARFLINESTATE LnState;
RT_ZERO(LnState);
LnState.pDwarfMod = pThis;
/*
* Parse the header.
*/
rtDwarfCursor_GetInitalLength(pCursor);
LnState.Hdr.uVer = rtDwarfCursor_GetUHalf(pCursor, 0);
if ( LnState.Hdr.uVer < 2
|| LnState.Hdr.uVer > 4)
return rtDwarfCursor_SkipUnit(pCursor);
LnState.Hdr.offFirstOpcode = rtDwarfCursor_GetUOff(pCursor, 0);
uint8_t const * const pbFirstOpcode = rtDwarfCursor_CalcPos(pCursor, LnState.Hdr.offFirstOpcode);
LnState.Hdr.cbMinInstr = rtDwarfCursor_GetUByte(pCursor, 0);
if (LnState.Hdr.uVer >= 4)
LnState.Hdr.cMaxOpsPerInstr = rtDwarfCursor_GetUByte(pCursor, 0);
else
LnState.Hdr.cMaxOpsPerInstr = 1;
LnState.Hdr.u8DefIsStmt = rtDwarfCursor_GetUByte(pCursor, 0);
LnState.Hdr.s8LineBase = rtDwarfCursor_GetSByte(pCursor, 0);
LnState.Hdr.u8LineRange = rtDwarfCursor_GetUByte(pCursor, 0);
LnState.Hdr.u8OpcodeBase = rtDwarfCursor_GetUByte(pCursor, 0);
if ( !LnState.Hdr.u8OpcodeBase
|| !LnState.Hdr.cMaxOpsPerInstr
|| !LnState.Hdr.u8LineRange
|| LnState.Hdr.u8DefIsStmt > 1)
return VERR_DWARF_BAD_LINE_NUMBER_HEADER;
Log2(("DWARF Line number header:\n"
" uVer %d\n"
" offFirstOpcode %#llx\n"
" cbMinInstr %u\n"
" cMaxOpsPerInstr %u\n"
" u8DefIsStmt %u\n"
" s8LineBase %d\n"
" u8LineRange %u\n"
" u8OpcodeBase %u\n",
LnState.Hdr.uVer, LnState.Hdr.offFirstOpcode, LnState.Hdr.cbMinInstr, LnState.Hdr.cMaxOpsPerInstr,
LnState.Hdr.u8DefIsStmt, LnState.Hdr.s8LineBase, LnState.Hdr.u8LineRange, LnState.Hdr.u8OpcodeBase));
LnState.Hdr.pacStdOperands = pCursor->pb;
for (uint8_t iStdOpcode = 1; iStdOpcode < LnState.Hdr.u8OpcodeBase; iStdOpcode++)
rtDwarfCursor_GetUByte(pCursor, 0);
int rc = pCursor->rc;
if (RT_SUCCESS(rc))
rc = rtDwarfLine_ReadIncludePaths(&LnState, pCursor);
if (RT_SUCCESS(rc))
rc = rtDwarfLine_ReadFileNames(&LnState, pCursor);
/*
* Run the program....
*/
if (RT_SUCCESS(rc))
rc = rtDwarfCursor_AdvanceToPos(pCursor, pbFirstOpcode);
if (RT_SUCCESS(rc))
rc = rtDwarfLine_RunProgram(&LnState, pCursor);
/*
* Clean up.
*/
size_t i = LnState.cFileNames;
while (i-- > 0)
RTStrFree(LnState.papszFileNames[i]);
RTMemFree(LnState.papszFileNames);
RTMemFree(LnState.papszIncPaths);
Assert(rtDwarfCursor_IsAtEndOfUnit(pCursor) || RT_FAILURE(rc));
return rc;
}
/**
* Explodes the line number table.
*
* The line numbers are insered into the debug info container.
*
* @returns IPRT status code
* @param pThis The DWARF instance.
*/
static int rtDwarfLine_ExplodeAll(PRTDBGMODDWARF pThis)
{
if (!pThis->aSections[krtDbgModDwarfSect_line].fPresent)
return VINF_SUCCESS;
RTDWARFCURSOR Cursor;
int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_line);
if (RT_FAILURE(rc))
return rc;
while ( !rtDwarfCursor_IsAtEnd(&Cursor)
&& RT_SUCCESS(rc))
rc = rtDwarfLine_ExplodeUnit(pThis, &Cursor);
return rtDwarfCursor_Delete(&Cursor, rc);
}
/*
*
* DWARF Abbreviations.
* DWARF Abbreviations.
* DWARF Abbreviations.
*
*/
/**
* Deals with a cache miss in rtDwarfAbbrev_Lookup.
*
* @returns Pointer to abbreviation cache entry (read only). May be rendered
* invalid by subsequent calls to this function.
* @param pThis The DWARF instance.
* @param uCode The abbreviation code to lookup.
*/
static PCRTDWARFABBREV rtDwarfAbbrev_LookupMiss(PRTDBGMODDWARF pThis, uint32_t uCode)
{
/*
* There is no entry with code zero.
*/
if (!uCode)
return NULL;
/*
* Resize the cache array if the code is considered cachable.
*/
bool fFillCache = true;
if (pThis->cCachedAbbrevsAlloced < uCode)
{
if (uCode >= _64K)
fFillCache = false;
else
{
uint32_t cNew = RT_ALIGN(uCode, 64);
void *pv = RTMemRealloc(pThis->paCachedAbbrevs, sizeof(pThis->paCachedAbbrevs[0]) * cNew);
if (!pv)
fFillCache = false;
else
{
Log(("rtDwarfAbbrev_LookupMiss: Growing from %u to %u...\n", pThis->cCachedAbbrevsAlloced, cNew));
pThis->paCachedAbbrevs = (PRTDWARFABBREV)pv;
for (uint32_t i = pThis->cCachedAbbrevsAlloced; i < cNew; i++)
pThis->paCachedAbbrevs[i].offAbbrev = UINT32_MAX;
pThis->cCachedAbbrevsAlloced = cNew;
}
}
}
/*
* Walk the abbreviations till we find the desired code.
*/
RTDWARFCURSOR Cursor;
int rc = rtDwarfCursor_InitWithOffset(&Cursor, pThis, krtDbgModDwarfSect_abbrev, pThis->offCachedAbbrev);
if (RT_FAILURE(rc))
return NULL;
PRTDWARFABBREV pRet = NULL;
if (fFillCache)
{
/*
* Search for the entry and fill the cache while doing so.
* We assume that abbreviation codes for a unit will stop when we see
* zero code or when the code value drops.
*/
uint32_t uPrevCode = 0;
for (;;)
{
/* Read the 'header'. Skipping zero code bytes. */
uint32_t const uCurCode = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
if (pRet && (uCurCode == 0 || uCurCode < uPrevCode))
break; /* probably end of unit. */
if (uCurCode != 0)
{
uint32_t const uCurTag = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
uint8_t const uChildren = rtDwarfCursor_GetU8(&Cursor, 0);
if (RT_FAILURE(Cursor.rc))
break;
if ( uCurTag > 0xffff
|| uChildren > 1)
{
Cursor.rc = VERR_DWARF_BAD_ABBREV;
break;
}
/* Cache it? */
if (uCurCode <= pThis->cCachedAbbrevsAlloced)
{
PRTDWARFABBREV pEntry = &pThis->paCachedAbbrevs[uCurCode - 1];
if (pEntry->offAbbrev != pThis->offCachedAbbrev)
{
pEntry->offAbbrev = pThis->offCachedAbbrev;
pEntry->fChildren = RT_BOOL(uChildren);
pEntry->uTag = uCurTag;
pEntry->offSpec = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
if (uCurCode == uCode)
{
Assert(!pRet);
pRet = pEntry;
if (uCurCode == pThis->cCachedAbbrevsAlloced)
break;
}
}
else if (pRet)
break; /* Next unit, don't cache more. */
/* else: We're growing the cache and re-reading old data. */
}
/* Skip the specification. */
uint32_t uAttr, uForm;
do
{
uAttr = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
uForm = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
} while (uAttr != 0);
}
if (RT_FAILURE(Cursor.rc))
break;
/* Done? (Maximize cache filling.) */
if ( pRet != NULL
&& uCurCode >= pThis->cCachedAbbrevsAlloced)
break;
}
}
else
{
/*
* Search for the entry with the desired code, no cache filling.
*/
for (;;)
{
/* Read the 'header'. */
uint32_t const uCurCode = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
uint32_t const uCurTag = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
uint8_t const uChildren = rtDwarfCursor_GetU8(&Cursor, 0);
if (RT_FAILURE(Cursor.rc))
break;
if ( uCurTag > 0xffff
|| uChildren > 1)
{
Cursor.rc = VERR_DWARF_BAD_ABBREV;
break;
}
/* Do we have a match? */
if (uCurCode == uCode)
{
pRet = &pThis->LookupAbbrev;
pRet->fChildren = RT_BOOL(uChildren);
pRet->uTag = uCurTag;
pRet->offSpec = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
pRet->offAbbrev = pThis->offCachedAbbrev;
break;
}
/* Skip the specification. */
uint32_t uAttr, uForm;
do
{
uAttr = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
uForm = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
} while (uAttr != 0);
if (RT_FAILURE(Cursor.rc))
break;
}
}
rtDwarfCursor_Delete(&Cursor, VINF_SUCCESS);
return pRet;
}
/**
* Looks up an abbreviation.
*
* @returns Pointer to abbreviation cache entry (read only). May be rendered
* invalid by subsequent calls to this function.
* @param pThis The DWARF instance.
* @param uCode The abbreviation code to lookup.
*/
static PCRTDWARFABBREV rtDwarfAbbrev_Lookup(PRTDBGMODDWARF pThis, uint32_t uCode)
{
if ( uCode - 1 >= pThis->cCachedAbbrevsAlloced
|| pThis->paCachedAbbrevs[uCode - 1].offAbbrev != pThis->offCachedAbbrev)
return rtDwarfAbbrev_LookupMiss(pThis, uCode);
return &pThis->paCachedAbbrevs[uCode - 1];
}
/**
* Sets the abbreviation offset of the current unit.
*
* @param pThis The DWARF instance.
* @param offAbbrev The offset into the abbreviation section.
*/
static void rtDwarfAbbrev_SetUnitOffset(PRTDBGMODDWARF pThis, uint32_t offAbbrev)
{
pThis->offCachedAbbrev = offAbbrev;
}
/*
*
* DIE Attribute Parsers.
* DIE Attribute Parsers.
* DIE Attribute Parsers.
*
*/
/**
* Gets the compilation unit a DIE belongs to.
*
* @returns The compilation unit DIE.
* @param pDie Some DIE in the unit.
*/
static PRTDWARFDIECOMPILEUNIT rtDwarfDie_GetCompileUnit(PRTDWARFDIE pDie)
{
while (pDie->pParent)
pDie = pDie->pParent;
AssertReturn( pDie->uTag == DW_TAG_compile_unit
|| pDie->uTag == DW_TAG_partial_unit,
NULL);
return (PRTDWARFDIECOMPILEUNIT)pDie;
}
/**
* Resolves a string section (debug_str) reference.
*
* @returns Pointer to the string (inside the string section).
* @param pThis The DWARF instance.
* @param pCursor The cursor.
* @param pszErrValue What to return on failure (@a
* pCursor->rc is set).
*/
static const char *rtDwarfDecodeHlp_GetStrp(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor, const char *pszErrValue)
{
uint64_t offDebugStr = rtDwarfCursor_GetUOff(pCursor, UINT64_MAX);
if (RT_FAILURE(pCursor->rc))
return pszErrValue;
if (offDebugStr >= pThis->aSections[krtDbgModDwarfSect_str].cb)
{
/* Ugly: Exploit the cursor status field for reporting errors. */
pCursor->rc = VERR_DWARF_BAD_INFO;
return pszErrValue;
}
if (!pThis->aSections[krtDbgModDwarfSect_str].pv)
{
int rc = rtDbgModDwarfLoadSection(pThis, krtDbgModDwarfSect_str);
if (RT_FAILURE(rc))
{
/* Ugly: Exploit the cursor status field for reporting errors. */
pCursor->rc = rc;
return pszErrValue;
}
}
return (const char *)pThis->aSections[krtDbgModDwarfSect_str].pv + (size_t)offDebugStr;
}
/** @callback_method_impl{FNRTDWARFATTRDECODER} */
static DECLCALLBACK(int) rtDwarfDecode_Address(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
uint32_t uForm, PRTDWARFCURSOR pCursor)
{
AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDR), VERR_INTERNAL_ERROR_3);
NOREF(pDie);
uint64_t uAddr;
switch (uForm)
{
case DW_FORM_addr: uAddr = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
case DW_FORM_data1: uAddr = rtDwarfCursor_GetU8(pCursor, 0); break;
case DW_FORM_data2: uAddr = rtDwarfCursor_GetU16(pCursor, 0); break;
case DW_FORM_data4: uAddr = rtDwarfCursor_GetU32(pCursor, 0); break;
case DW_FORM_data8: uAddr = rtDwarfCursor_GetU64(pCursor, 0); break;
case DW_FORM_udata: uAddr = rtDwarfCursor_GetULeb128(pCursor, 0); break;
default:
AssertMsgFailedReturn(("%#x (%s)\n", uForm, rtDwarfLog_FormName(uForm)), VERR_DWARF_UNEXPECTED_FORM);
}
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
PRTDWARFADDR pAddr = (PRTDWARFADDR)pbMember;
pAddr->uAddress = uAddr;
Log4((" %-20s %#010llx [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), uAddr, rtDwarfLog_FormName(uForm)));
return VINF_SUCCESS;
}
/** @callback_method_impl{FNRTDWARFATTRDECODER} */
static DECLCALLBACK(int) rtDwarfDecode_Bool(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
uint32_t uForm, PRTDWARFCURSOR pCursor)
{
AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(bool), VERR_INTERNAL_ERROR_3);
NOREF(pDie);
bool *pfMember = (bool *)pbMember;
switch (uForm)
{
case DW_FORM_flag:
{
uint8_t b = rtDwarfCursor_GetU8(pCursor, UINT8_MAX);
if (b > 1)
{
Log(("Unexpected boolean value %#x\n", b));
return RT_FAILURE(pCursor->rc) ? pCursor->rc : pCursor->rc = VERR_DWARF_BAD_INFO;
}
*pfMember = RT_BOOL(b);
break;
}
case DW_FORM_flag_present:
*pfMember = true;
break;
default:
AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
}
Log4((" %-20s %RTbool [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), *pfMember, rtDwarfLog_FormName(uForm)));
return VINF_SUCCESS;
}
/** @callback_method_impl{FNRTDWARFATTRDECODER} */
static DECLCALLBACK(int) rtDwarfDecode_LowHighPc(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
uint32_t uForm, PRTDWARFCURSOR pCursor)
{
AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDRRANGE), VERR_INTERNAL_ERROR_3);
AssertReturn(pDesc->uAttr == DW_AT_low_pc || pDesc->uAttr == DW_AT_high_pc, VERR_INTERNAL_ERROR_3);
NOREF(pDie);
uint64_t uAddr;
switch (uForm)
{
case DW_FORM_addr: uAddr = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
case DW_FORM_data1: uAddr = rtDwarfCursor_GetU8(pCursor, 0); break;
case DW_FORM_data2: uAddr = rtDwarfCursor_GetU16(pCursor, 0); break;
case DW_FORM_data4: uAddr = rtDwarfCursor_GetU32(pCursor, 0); break;
case DW_FORM_data8: uAddr = rtDwarfCursor_GetU64(pCursor, 0); break;
case DW_FORM_udata: uAddr = rtDwarfCursor_GetULeb128(pCursor, 0); break;
default:
AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
}
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
PRTDWARFADDRRANGE pRange = (PRTDWARFADDRRANGE)pbMember;
if (pDesc->uAttr == DW_AT_low_pc)
{
if (pRange->fHaveLowAddress)
{
Log(("rtDwarfDecode_LowHighPc: Duplicate DW_AT_low_pc\n"));
return pCursor->rc = VERR_DWARF_BAD_INFO;
}
pRange->fHaveLowAddress = true;
pRange->uLowAddress = uAddr;
}
else
{
if (pRange->fHaveHighAddress)
{
Log(("rtDwarfDecode_LowHighPc: Duplicate DW_AT_high_pc\n"));
return pCursor->rc = VERR_DWARF_BAD_INFO;
}
pRange->fHaveHighAddress = true;
pRange->fHaveHighIsAddress = uForm == DW_FORM_addr;
if (!pRange->fHaveHighIsAddress && pRange->fHaveLowAddress)
{
pRange->fHaveHighIsAddress = true;
pRange->uHighAddress = uAddr + pRange->uLowAddress;
}
else
pRange->uHighAddress = uAddr;
}
pRange->cAttrs++;
Log4((" %-20s %#010llx [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), uAddr, rtDwarfLog_FormName(uForm)));
return VINF_SUCCESS;
}
/** @callback_method_impl{FNRTDWARFATTRDECODER} */
static DECLCALLBACK(int) rtDwarfDecode_Ranges(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
uint32_t uForm, PRTDWARFCURSOR pCursor)
{
AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDRRANGE), VERR_INTERNAL_ERROR_3);
AssertReturn(pDesc->uAttr == DW_AT_ranges, VERR_INTERNAL_ERROR_3);
NOREF(pDie);
/* Decode it. */
uint64_t off;
switch (uForm)
{
case DW_FORM_addr: off = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
case DW_FORM_data4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
case DW_FORM_data8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
case DW_FORM_sec_offset: off = rtDwarfCursor_GetUOff(pCursor, 0); break;
default:
AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
}
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
/* Validate the offset and load the ranges. */
PRTDBGMODDWARF pThis = pCursor->pDwarfMod;
if (off >= pThis->aSections[krtDbgModDwarfSect_ranges].cb)
{
Log(("rtDwarfDecode_Ranges: bad ranges off=%#llx\n", off));
return pCursor->rc = VERR_DWARF_BAD_POS;
}
if (!pThis->aSections[krtDbgModDwarfSect_ranges].pv)
{
int rc = rtDbgModDwarfLoadSection(pThis, krtDbgModDwarfSect_ranges);
if (RT_FAILURE(rc))
return pCursor->rc = rc;
}
/* Store the result. */
PRTDWARFADDRRANGE pRange = (PRTDWARFADDRRANGE)pbMember;
if (pRange->fHaveRanges)
{
Log(("rtDwarfDecode_Ranges: Duplicate DW_AT_ranges\n"));
return pCursor->rc = VERR_DWARF_BAD_INFO;
}
pRange->fHaveRanges = true;
pRange->cAttrs++;
pRange->pbRanges = (uint8_t const *)pThis->aSections[krtDbgModDwarfSect_ranges].pv + (size_t)off;
Log4((" %-20s TODO [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), rtDwarfLog_FormName(uForm)));
return VINF_SUCCESS;
}
/** @callback_method_impl{FNRTDWARFATTRDECODER} */
static DECLCALLBACK(int) rtDwarfDecode_Reference(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
uint32_t uForm, PRTDWARFCURSOR pCursor)
{
AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFREF), VERR_INTERNAL_ERROR_3);
/* Decode it. */
uint64_t off;
krtDwarfRef enmWrt = krtDwarfRef_SameUnit;
switch (uForm)
{
case DW_FORM_ref1: off = rtDwarfCursor_GetU8(pCursor, 0); break;
case DW_FORM_ref2: off = rtDwarfCursor_GetU16(pCursor, 0); break;
case DW_FORM_ref4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
case DW_FORM_ref8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
case DW_FORM_ref_udata: off = rtDwarfCursor_GetULeb128(pCursor, 0); break;
case DW_FORM_ref_addr:
enmWrt = krtDwarfRef_InfoSection;
off = rtDwarfCursor_GetUOff(pCursor, 0);
break;
case DW_FORM_ref_sig8:
enmWrt = krtDwarfRef_TypeId64;
off = rtDwarfCursor_GetU64(pCursor, 0);
break;
default:
AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
}
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
/* Validate the offset and convert to debug_info relative offsets. */
if (enmWrt == krtDwarfRef_InfoSection)
{
if (off >= pCursor->pDwarfMod->aSections[krtDbgModDwarfSect_info].cb)
{
Log(("rtDwarfDecode_Reference: bad info off=%#llx\n", off));
return pCursor->rc = VERR_DWARF_BAD_POS;
}
}
else if (enmWrt == krtDwarfRef_SameUnit)
{
PRTDWARFDIECOMPILEUNIT pUnit = rtDwarfDie_GetCompileUnit(pDie);
if (off >= pUnit->cbUnit)
{
Log(("rtDwarfDecode_Reference: bad unit off=%#llx\n", off));
return pCursor->rc = VERR_DWARF_BAD_POS;
}
off += pUnit->offUnit;
enmWrt = krtDwarfRef_InfoSection;
}
/* else: not bother verifying/resolving the indirect type reference yet. */
/* Store it */
PRTDWARFREF pRef = (PRTDWARFREF)pbMember;
pRef->enmWrt = enmWrt;
pRef->off = off;
Log4((" %-20s %d:%#010llx [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), enmWrt, off, rtDwarfLog_FormName(uForm)));
return VINF_SUCCESS;
}
/** @callback_method_impl{FNRTDWARFATTRDECODER} */
static DECLCALLBACK(int) rtDwarfDecode_SectOff(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
uint32_t uForm, PRTDWARFCURSOR pCursor)
{
AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFREF), VERR_INTERNAL_ERROR_3);
NOREF(pDie);
uint64_t off;
switch (uForm)
{
case DW_FORM_data4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
case DW_FORM_data8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
case DW_FORM_sec_offset: off = rtDwarfCursor_GetUOff(pCursor, 0); break;
default:
AssertMsgFailedReturn(("%#x (%s)\n", uForm, rtDwarfLog_FormName(uForm)), VERR_DWARF_UNEXPECTED_FORM);
}
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
krtDbgModDwarfSect enmSect;
krtDwarfRef enmWrt;
switch (pDesc->uAttr)
{
case DW_AT_stmt_list: enmSect = krtDbgModDwarfSect_line; enmWrt = krtDwarfRef_LineSection; break;
case DW_AT_macro_info: enmSect = krtDbgModDwarfSect_loc; enmWrt = krtDwarfRef_LocSection; break;
case DW_AT_ranges: enmSect = krtDbgModDwarfSect_ranges; enmWrt = krtDwarfRef_RangesSection; break;
default:
AssertMsgFailedReturn(("%u (%s)\n", pDesc->uAttr, rtDwarfLog_AttrName(pDesc->uAttr)), VERR_INTERNAL_ERROR_4);
}
size_t cbSect = pCursor->pDwarfMod->aSections[enmSect].cb;
if (off >= cbSect)
{
/* Watcom generates offset past the end of the section, increasing the
offset by one for each compile unit. So, just fudge it. */
Log(("rtDwarfDecode_SectOff: bad off=%#llx, attr %#x (%s), enmSect=%d cb=%#llx; Assuming watcom/gcc.\n", off,
pDesc->uAttr, rtDwarfLog_AttrName(pDesc->uAttr), enmSect, cbSect));
off = cbSect;
}
PRTDWARFREF pRef = (PRTDWARFREF)pbMember;
pRef->enmWrt = enmWrt;
pRef->off = off;
Log4((" %-20s %d:%#010llx [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), enmWrt, off, rtDwarfLog_FormName(uForm)));
return VINF_SUCCESS;
}
/** @callback_method_impl{FNRTDWARFATTRDECODER} */
static DECLCALLBACK(int) rtDwarfDecode_String(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
uint32_t uForm, PRTDWARFCURSOR pCursor)
{
AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(const char *), VERR_INTERNAL_ERROR_3);
NOREF(pDie);
const char *psz;
switch (uForm)
{
case DW_FORM_string:
psz = rtDwarfCursor_GetSZ(pCursor, NULL);
break;
case DW_FORM_strp:
psz = rtDwarfDecodeHlp_GetStrp(pCursor->pDwarfMod, pCursor, NULL);
break;
default:
AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
}
*(const char **)pbMember = psz;
Log4((" %-20s '%s' [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), psz, rtDwarfLog_FormName(uForm)));
return pCursor->rc;
}
/** @callback_method_impl{FNRTDWARFATTRDECODER} */
static DECLCALLBACK(int) rtDwarfDecode_UnsignedInt(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
uint32_t uForm, PRTDWARFCURSOR pCursor)
{
NOREF(pDie);
uint64_t u64Val;
switch (uForm)
{
case DW_FORM_udata: u64Val = rtDwarfCursor_GetULeb128(pCursor, 0); break;
case DW_FORM_data1: u64Val = rtDwarfCursor_GetU8(pCursor, 0); break;
case DW_FORM_data2: u64Val = rtDwarfCursor_GetU16(pCursor, 0); break;
case DW_FORM_data4: u64Val = rtDwarfCursor_GetU32(pCursor, 0); break;
case DW_FORM_data8: u64Val = rtDwarfCursor_GetU64(pCursor, 0); break;
default:
AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
}
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
switch (ATTR_GET_SIZE(pDesc))
{
case 1:
*pbMember = (uint8_t)u64Val;
if (*pbMember != u64Val)
{
AssertFailed();
return VERR_OUT_OF_RANGE;
}
break;
case 2:
*(uint16_t *)pbMember = (uint16_t)u64Val;
if (*(uint16_t *)pbMember != u64Val)
{
AssertFailed();
return VERR_OUT_OF_RANGE;
}
break;
case 4:
*(uint32_t *)pbMember = (uint32_t)u64Val;
if (*(uint32_t *)pbMember != u64Val)
{
AssertFailed();
return VERR_OUT_OF_RANGE;
}
break;
case 8:
*(uint64_t *)pbMember = (uint64_t)u64Val;
if (*(uint64_t *)pbMember != u64Val)
{
AssertFailed();
return VERR_OUT_OF_RANGE;
}
break;
default:
AssertMsgFailedReturn(("%#x\n", ATTR_GET_SIZE(pDesc)), VERR_INTERNAL_ERROR_2);
}
return VINF_SUCCESS;
}
/**
* Initialize location interpreter state from cursor & form.
*
* @returns IPRT status code.
* @retval VERR_NOT_FOUND if no location information (i.e. there is source but
* it resulted in no byte code).
* @param pLoc The location state structure to initialize.
* @param pCursor The cursor to read from.
* @param uForm The attribute form.
*/
static int rtDwarfLoc_Init(PRTDWARFLOCST pLoc, PRTDWARFCURSOR pCursor, uint32_t uForm)
{
uint32_t cbBlock;
switch (uForm)
{
case DW_FORM_block1:
cbBlock = rtDwarfCursor_GetU8(pCursor, 0);
break;
case DW_FORM_block2:
cbBlock = rtDwarfCursor_GetU16(pCursor, 0);
break;
case DW_FORM_block4:
cbBlock = rtDwarfCursor_GetU32(pCursor, 0);
break;
case DW_FORM_block:
cbBlock = rtDwarfCursor_GetULeb128(pCursor, 0);
break;
default:
AssertMsgFailedReturn(("uForm=%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
}
if (!cbBlock)
return VERR_NOT_FOUND;
int rc = rtDwarfCursor_InitForBlock(&pLoc->Cursor, pCursor, cbBlock);
if (RT_FAILURE(rc))
return rc;
pLoc->iTop = -1;
return VINF_SUCCESS;
}
/**
* Pushes a value onto the stack.
*
* @returns VINF_SUCCESS or VERR_DWARF_STACK_OVERFLOW.
* @param pLoc The state.
* @param uValue The value to push.
*/
static int rtDwarfLoc_Push(PRTDWARFLOCST pLoc, uint64_t uValue)
{
int iTop = pLoc->iTop + 1;
AssertReturn((unsigned)iTop < RT_ELEMENTS(pLoc->auStack), VERR_DWARF_STACK_OVERFLOW);
pLoc->auStack[iTop] = uValue;
pLoc->iTop = iTop;
return VINF_SUCCESS;
}
static int rtDwarfLoc_Evaluate(PRTDWARFLOCST pLoc, void *pvLater, void *pvUser)
{
while (!rtDwarfCursor_IsAtEndOfUnit(&pLoc->Cursor))
{
/* Read the next opcode.*/
uint8_t const bOpcode = rtDwarfCursor_GetU8(&pLoc->Cursor, 0);
/* Get its operands. */
uint64_t uOperand1 = 0;
uint64_t uOperand2 = 0;
switch (bOpcode)
{
case DW_OP_addr:
uOperand1 = rtDwarfCursor_GetNativeUOff(&pLoc->Cursor, 0);
break;
case DW_OP_pick:
case DW_OP_const1u:
case DW_OP_deref_size:
case DW_OP_xderef_size:
uOperand1 = rtDwarfCursor_GetU8(&pLoc->Cursor, 0);
break;
case DW_OP_const1s:
uOperand1 = (int8_t)rtDwarfCursor_GetU8(&pLoc->Cursor, 0);
break;
case DW_OP_const2u:
uOperand1 = rtDwarfCursor_GetU16(&pLoc->Cursor, 0);
break;
case DW_OP_skip:
case DW_OP_bra:
case DW_OP_const2s:
uOperand1 = (int16_t)rtDwarfCursor_GetU16(&pLoc->Cursor, 0);
break;
case DW_OP_const4u:
uOperand1 = rtDwarfCursor_GetU32(&pLoc->Cursor, 0);
break;
case DW_OP_const4s:
uOperand1 = (int32_t)rtDwarfCursor_GetU32(&pLoc->Cursor, 0);
break;
case DW_OP_const8u:
uOperand1 = rtDwarfCursor_GetU64(&pLoc->Cursor, 0);
break;
case DW_OP_const8s:
uOperand1 = rtDwarfCursor_GetU64(&pLoc->Cursor, 0);
break;
case DW_OP_regx:
case DW_OP_piece:
case DW_OP_plus_uconst:
case DW_OP_constu:
uOperand1 = rtDwarfCursor_GetULeb128(&pLoc->Cursor, 0);
break;
case DW_OP_consts:
case DW_OP_fbreg:
case DW_OP_breg0+0: case DW_OP_breg0+1: case DW_OP_breg0+2: case DW_OP_breg0+3:
case DW_OP_breg0+4: case DW_OP_breg0+5: case DW_OP_breg0+6: case DW_OP_breg0+7:
case DW_OP_breg0+8: case DW_OP_breg0+9: case DW_OP_breg0+10: case DW_OP_breg0+11:
case DW_OP_breg0+12: case DW_OP_breg0+13: case DW_OP_breg0+14: case DW_OP_breg0+15:
case DW_OP_breg0+16: case DW_OP_breg0+17: case DW_OP_breg0+18: case DW_OP_breg0+19:
case DW_OP_breg0+20: case DW_OP_breg0+21: case DW_OP_breg0+22: case DW_OP_breg0+23:
case DW_OP_breg0+24: case DW_OP_breg0+25: case DW_OP_breg0+26: case DW_OP_breg0+27:
case DW_OP_breg0+28: case DW_OP_breg0+29: case DW_OP_breg0+30: case DW_OP_breg0+31:
uOperand1 = rtDwarfCursor_GetSLeb128(&pLoc->Cursor, 0);
break;
case DW_OP_bregx:
uOperand1 = rtDwarfCursor_GetULeb128(&pLoc->Cursor, 0);
uOperand2 = rtDwarfCursor_GetSLeb128(&pLoc->Cursor, 0);
break;
}
if (RT_FAILURE(pLoc->Cursor.rc))
break;
/* Interpret the opcode. */
int rc;
switch (bOpcode)
{
case DW_OP_const1u:
case DW_OP_const1s:
case DW_OP_const2u:
case DW_OP_const2s:
case DW_OP_const4u:
case DW_OP_const4s:
case DW_OP_const8u:
case DW_OP_const8s:
case DW_OP_constu:
case DW_OP_consts:
case DW_OP_addr:
rc = rtDwarfLoc_Push(pLoc, uOperand1);
break;
case DW_OP_lit0 + 0: case DW_OP_lit0 + 1: case DW_OP_lit0 + 2: case DW_OP_lit0 + 3:
case DW_OP_lit0 + 4: case DW_OP_lit0 + 5: case DW_OP_lit0 + 6: case DW_OP_lit0 + 7:
case DW_OP_lit0 + 8: case DW_OP_lit0 + 9: case DW_OP_lit0 + 10: case DW_OP_lit0 + 11:
case DW_OP_lit0 + 12: case DW_OP_lit0 + 13: case DW_OP_lit0 + 14: case DW_OP_lit0 + 15:
case DW_OP_lit0 + 16: case DW_OP_lit0 + 17: case DW_OP_lit0 + 18: case DW_OP_lit0 + 19:
case DW_OP_lit0 + 20: case DW_OP_lit0 + 21: case DW_OP_lit0 + 22: case DW_OP_lit0 + 23:
case DW_OP_lit0 + 24: case DW_OP_lit0 + 25: case DW_OP_lit0 + 26: case DW_OP_lit0 + 27:
case DW_OP_lit0 + 28: case DW_OP_lit0 + 29: case DW_OP_lit0 + 30: case DW_OP_lit0 + 31:
rc = rtDwarfLoc_Push(pLoc, bOpcode - DW_OP_lit0);
break;
case DW_OP_nop:
break;
case DW_OP_dup: /** @todo 0 operands. */
case DW_OP_drop: /** @todo 0 operands. */
case DW_OP_over: /** @todo 0 operands. */
case DW_OP_pick: /** @todo 1 operands, a 1-byte stack index. */
case DW_OP_swap: /** @todo 0 operands. */
case DW_OP_rot: /** @todo 0 operands. */
case DW_OP_abs: /** @todo 0 operands. */
case DW_OP_and: /** @todo 0 operands. */
case DW_OP_div: /** @todo 0 operands. */
case DW_OP_minus: /** @todo 0 operands. */
case DW_OP_mod: /** @todo 0 operands. */
case DW_OP_mul: /** @todo 0 operands. */
case DW_OP_neg: /** @todo 0 operands. */
case DW_OP_not: /** @todo 0 operands. */
case DW_OP_or: /** @todo 0 operands. */
case DW_OP_plus: /** @todo 0 operands. */
case DW_OP_plus_uconst: /** @todo 1 operands, a ULEB128 addend. */
case DW_OP_shl: /** @todo 0 operands. */
case DW_OP_shr: /** @todo 0 operands. */
case DW_OP_shra: /** @todo 0 operands. */
case DW_OP_xor: /** @todo 0 operands. */
case DW_OP_skip: /** @todo 1 signed 2-byte constant. */
case DW_OP_bra: /** @todo 1 signed 2-byte constant. */
case DW_OP_eq: /** @todo 0 operands. */
case DW_OP_ge: /** @todo 0 operands. */
case DW_OP_gt: /** @todo 0 operands. */
case DW_OP_le: /** @todo 0 operands. */
case DW_OP_lt: /** @todo 0 operands. */
case DW_OP_ne: /** @todo 0 operands. */
case DW_OP_reg0 + 0: case DW_OP_reg0 + 1: case DW_OP_reg0 + 2: case DW_OP_reg0 + 3: /** @todo 0 operands - reg 0..31. */
case DW_OP_reg0 + 4: case DW_OP_reg0 + 5: case DW_OP_reg0 + 6: case DW_OP_reg0 + 7:
case DW_OP_reg0 + 8: case DW_OP_reg0 + 9: case DW_OP_reg0 + 10: case DW_OP_reg0 + 11:
case DW_OP_reg0 + 12: case DW_OP_reg0 + 13: case DW_OP_reg0 + 14: case DW_OP_reg0 + 15:
case DW_OP_reg0 + 16: case DW_OP_reg0 + 17: case DW_OP_reg0 + 18: case DW_OP_reg0 + 19:
case DW_OP_reg0 + 20: case DW_OP_reg0 + 21: case DW_OP_reg0 + 22: case DW_OP_reg0 + 23:
case DW_OP_reg0 + 24: case DW_OP_reg0 + 25: case DW_OP_reg0 + 26: case DW_OP_reg0 + 27:
case DW_OP_reg0 + 28: case DW_OP_reg0 + 29: case DW_OP_reg0 + 30: case DW_OP_reg0 + 31:
case DW_OP_breg0+ 0: case DW_OP_breg0+ 1: case DW_OP_breg0+ 2: case DW_OP_breg0+ 3: /** @todo 1 operand, a SLEB128 offset. */
case DW_OP_breg0+ 4: case DW_OP_breg0+ 5: case DW_OP_breg0+ 6: case DW_OP_breg0+ 7:
case DW_OP_breg0+ 8: case DW_OP_breg0+ 9: case DW_OP_breg0+ 10: case DW_OP_breg0+ 11:
case DW_OP_breg0+ 12: case DW_OP_breg0+ 13: case DW_OP_breg0+ 14: case DW_OP_breg0+ 15:
case DW_OP_breg0+ 16: case DW_OP_breg0+ 17: case DW_OP_breg0+ 18: case DW_OP_breg0+ 19:
case DW_OP_breg0+ 20: case DW_OP_breg0+ 21: case DW_OP_breg0+ 22: case DW_OP_breg0+ 23:
case DW_OP_breg0+ 24: case DW_OP_breg0+ 25: case DW_OP_breg0+ 26: case DW_OP_breg0+ 27:
case DW_OP_breg0+ 28: case DW_OP_breg0+ 29: case DW_OP_breg0+ 30: case DW_OP_breg0+ 31:
case DW_OP_piece: /** @todo 1 operand, a ULEB128 size of piece addressed. */
case DW_OP_regx: /** @todo 1 operand, a ULEB128 register. */
case DW_OP_fbreg: /** @todo 1 operand, a SLEB128 offset. */
case DW_OP_bregx: /** @todo 2 operands, a ULEB128 register followed by a SLEB128 offset. */
case DW_OP_deref: /** @todo 0 operands. */
case DW_OP_deref_size: /** @todo 1 operand, a 1-byte size of data retrieved. */
case DW_OP_xderef: /** @todo 0 operands. */
case DW_OP_xderef_size: /** @todo 1 operand, a 1-byte size of data retrieved. */
AssertMsgFailedReturn(("bOpcode=%#x\n", bOpcode), VERR_DWARF_TODO);
default:
AssertMsgFailedReturn(("bOpcode=%#x\n", bOpcode), VERR_DWARF_UNKNOWN_LOC_OPCODE);
}
}
return pLoc->Cursor.rc;
}
/** @callback_method_impl{FNRTDWARFATTRDECODER} */
static DECLCALLBACK(int) rtDwarfDecode_SegmentLoc(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
uint32_t uForm, PRTDWARFCURSOR pCursor)
{
NOREF(pDie);
AssertReturn(ATTR_GET_SIZE(pDesc) == 2, VERR_DWARF_IPE);
RTDWARFLOCST LocSt;
int rc = rtDwarfLoc_Init(&LocSt, pCursor, uForm);
if (RT_SUCCESS(rc))
{
rc = rtDwarfLoc_Evaluate(&LocSt, NULL, NULL);
if (RT_SUCCESS(rc))
{
if (LocSt.iTop >= 0)
{
*(uint16_t *)pbMember = LocSt.auStack[LocSt.iTop];
Log4((" %-20s %#06llx [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr),
LocSt.auStack[LocSt.iTop], rtDwarfLog_FormName(uForm)));
return VINF_SUCCESS;
}
rc = VERR_DWARF_STACK_UNDERFLOW;
}
}
return rc;
}
/*
*
* DWARF debug_info parser
* DWARF debug_info parser
* DWARF debug_info parser
*
*/
/**
* Special hack to get the name and/or linkage name for a subprogram via a
* specification reference.
*
* Since this is a hack, we ignore failure.
*
* If we want to really make use of DWARF info, we'll have to create some kind
* of lookup tree for handling this. But currently we don't, so a hack will
* suffice.
*
* @param pThis The DWARF instance.
* @param pSubProgram The subprogram which is short on names.
*/
static void rtDwarfInfo_TryGetSubProgramNameFromSpecRef(PRTDBGMODDWARF pThis, PRTDWARFDIESUBPROGRAM pSubProgram)
{
/*
* Must have a spec ref, and it must be in the info section.
*/
if (pSubProgram->SpecRef.enmWrt != krtDwarfRef_InfoSection)
return;
/*
* Create a cursor for reading the info and then the abbrivation code
* starting the off the DIE.
*/
RTDWARFCURSOR InfoCursor;
int rc = rtDwarfCursor_InitWithOffset(&InfoCursor, pThis, krtDbgModDwarfSect_info, pSubProgram->SpecRef.off);
if (RT_FAILURE(rc))
return;
uint32_t uAbbrCode = rtDwarfCursor_GetULeb128AsU32(&InfoCursor, UINT32_MAX);
if (uAbbrCode)
{
/* Only references to subprogram tags are interesting here. */
PCRTDWARFABBREV pAbbrev = rtDwarfAbbrev_Lookup(pThis, uAbbrCode);
if ( pAbbrev
&& pAbbrev->uTag == DW_TAG_subprogram)
{
/*
* Use rtDwarfInfo_ParseDie to do the parsing, but with a different
* attribute spec than usual.
*/
rtDwarfInfo_ParseDie(pThis, &pSubProgram->Core, &g_SubProgramSpecHackDesc, &InfoCursor,
pAbbrev, false /*fInitDie*/);
}
}
rtDwarfCursor_Delete(&InfoCursor, VINF_SUCCESS);
}
/**
* Select which name to use.
*
* @returns One of the names.
* @param pszName The DWARF name, may exclude namespace and class.
* Can also be NULL.
* @param pszLinkageName The linkage name. Can be NULL.
*/
static const char *rtDwarfInfo_SelectName(const char *pszName, const char *pszLinkageName)
{
if (!pszName || !pszLinkageName)
return pszName ? pszName : pszLinkageName;
/*
* Some heuristics for selecting the link name if the normal name is missing
* namespace or class prefixes.
*/
size_t cchName = strlen(pszName);
size_t cchLinkageName = strlen(pszLinkageName);
if (cchLinkageName <= cchName + 1)
return pszName;
const char *psz = strstr(pszLinkageName, pszName);
if (!psz || psz - pszLinkageName < 4)
return pszName;
return pszLinkageName;
}
/**
* Parse the attributes of a DIE.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
* @param pDie The internal DIE structure to fill.
*/
static int rtDwarfInfo_SnoopSymbols(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie)
{
int rc = VINF_SUCCESS;
switch (pDie->uTag)
{
case DW_TAG_subprogram:
{
PRTDWARFDIESUBPROGRAM pSubProgram = (PRTDWARFDIESUBPROGRAM)pDie;
/* Obtain referenced specification there is only partial info. */
if ( pSubProgram->PcRange.cAttrs
&& !pSubProgram->pszName)
rtDwarfInfo_TryGetSubProgramNameFromSpecRef(pThis, pSubProgram);
if (pSubProgram->PcRange.cAttrs)
{
if (pSubProgram->PcRange.fHaveRanges)
Log5(("subprogram %s (%s) <implement ranges>\n", pSubProgram->pszName, pSubProgram->pszLinkageName));
else
{
Log5(("subprogram %s (%s) %#llx-%#llx%s\n", pSubProgram->pszName, pSubProgram->pszLinkageName,
pSubProgram->PcRange.uLowAddress, pSubProgram->PcRange.uHighAddress,
pSubProgram->PcRange.cAttrs == 2 ? "" : " !bad!"));
if ( ( pSubProgram->pszName || pSubProgram->pszLinkageName)
&& pSubProgram->PcRange.cAttrs == 2)
{
if (pThis->iWatcomPass == 1)
rc = rtDbgModDwarfRecordSegOffset(pThis, pSubProgram->uSegment, pSubProgram->PcRange.uHighAddress);
else
{
RTDBGSEGIDX iSeg;
RTUINTPTR offSeg;
rc = rtDbgModDwarfLinkAddressToSegOffset(pThis, pSubProgram->uSegment,
pSubProgram->PcRange.uLowAddress,
&iSeg, &offSeg);
if (RT_SUCCESS(rc))
{
uint64_t cb;
if (pSubProgram->PcRange.uHighAddress >= pSubProgram->PcRange.uLowAddress)
cb = pSubProgram->PcRange.uHighAddress - pSubProgram->PcRange.uLowAddress;
else
cb = 1;
rc = RTDbgModSymbolAdd(pThis->hCnt,
rtDwarfInfo_SelectName(pSubProgram->pszName, pSubProgram->pszLinkageName),
iSeg, offSeg, cb, 0 /*fFlags*/, NULL /*piOrdinal*/);
if (RT_FAILURE(rc))
{
if ( rc == VERR_DBG_DUPLICATE_SYMBOL
|| rc == VERR_DBG_ADDRESS_CONFLICT /** @todo figure why this happens with 10.6.8 mach_kernel, 32-bit. */
)
rc = VINF_SUCCESS;
else
AssertMsgFailed(("%Rrc\n", rc));
}
}
else if ( pSubProgram->PcRange.uLowAddress == 0 /* see with vmlinux */
&& pSubProgram->PcRange.uHighAddress == 0)
{
Log5(("rtDbgModDwarfLinkAddressToSegOffset: Ignoring empty range.\n"));
rc = VINF_SUCCESS; /* ignore */
}
else
{
AssertRC(rc);
Log5(("rtDbgModDwarfLinkAddressToSegOffset failed: %Rrc\n", rc));
}
}
}
}
}
else
Log5(("subprogram %s (%s) external\n", pSubProgram->pszName, pSubProgram->pszLinkageName));
break;
}
case DW_TAG_label:
{
PCRTDWARFDIELABEL pLabel = (PCRTDWARFDIELABEL)pDie;
if (pLabel->fExternal)
{
Log5(("label %s %#x:%#llx\n", pLabel->pszName, pLabel->uSegment, pLabel->Address.uAddress));
if (pThis->iWatcomPass == 1)
rc = rtDbgModDwarfRecordSegOffset(pThis, pLabel->uSegment, pLabel->Address.uAddress);
else
{
RTDBGSEGIDX iSeg;
RTUINTPTR offSeg;
rc = rtDbgModDwarfLinkAddressToSegOffset(pThis, pLabel->uSegment, pLabel->Address.uAddress,
&iSeg, &offSeg);
AssertRC(rc);
if (RT_SUCCESS(rc))
{
rc = RTDbgModSymbolAdd(pThis->hCnt, pLabel->pszName, iSeg, offSeg, 0 /*cb*/,
0 /*fFlags*/, NULL /*piOrdinal*/);
AssertRC(rc);
}
else
Log5(("rtDbgModDwarfLinkAddressToSegOffset failed: %Rrc\n", rc));
}
}
break;
}
}
return rc;
}
/**
* Initializes the non-core fields of an internal DIE structure.
*
* @param pDie The DIE structure.
* @param pDieDesc The DIE descriptor.
*/
static void rtDwarfInfo_InitDie(PRTDWARFDIE pDie, PCRTDWARFDIEDESC pDieDesc)
{
size_t i = pDieDesc->cAttributes;
while (i-- > 0)
{
switch (pDieDesc->paAttributes[i].cbInit & ATTR_INIT_MASK)
{
case ATTR_INIT_ZERO:
/* Nothing to do (RTMemAllocZ). */
break;
case ATTR_INIT_FFFS:
switch (pDieDesc->paAttributes[i].cbInit & ATTR_SIZE_MASK)
{
case 1:
*(uint8_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT8_MAX;
break;
case 2:
*(uint16_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT16_MAX;
break;
case 4:
*(uint32_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT32_MAX;
break;
case 8:
*(uint64_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT64_MAX;
break;
default:
AssertFailed();
memset((uint8_t *)pDie + pDieDesc->paAttributes[i].off, 0xff,
pDieDesc->paAttributes[i].cbInit & ATTR_SIZE_MASK);
break;
}
break;
default:
AssertFailed();
}
}
}
/**
* Creates a new internal DIE structure and links it up.
*
* @returns Pointer to the new DIE structure.
* @param pThis The DWARF instance.
* @param pDieDesc The DIE descriptor (for size and init).
* @param pAbbrev The abbreviation cache entry.
* @param pParent The parent DIE (NULL if unit).
*/
static PRTDWARFDIE rtDwarfInfo_NewDie(PRTDBGMODDWARF pThis, PCRTDWARFDIEDESC pDieDesc,
PCRTDWARFABBREV pAbbrev, PRTDWARFDIE pParent)
{
NOREF(pThis);
Assert(pDieDesc->cbDie >= sizeof(RTDWARFDIE));
#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
uint32_t iAllocator = pDieDesc->cbDie > pThis->aDieAllocators[0].cbMax;
Assert(pDieDesc->cbDie <= pThis->aDieAllocators[iAllocator].cbMax);
PRTDWARFDIE pDie = (PRTDWARFDIE)RTMemCacheAlloc(pThis->aDieAllocators[iAllocator].hMemCache);
#else
PRTDWARFDIE pDie = (PRTDWARFDIE)RTMemAllocZ(pDieDesc->cbDie);
#endif
if (pDie)
{
#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
RT_BZERO(pDie, pDieDesc->cbDie);
pDie->iAllocator = iAllocator;
#endif
rtDwarfInfo_InitDie(pDie, pDieDesc);
pDie->uTag = pAbbrev->uTag;
pDie->offSpec = pAbbrev->offSpec;
pDie->pParent = pParent;
if (pParent)
RTListAppend(&pParent->ChildList, &pDie->SiblingNode);
else
RTListInit(&pDie->SiblingNode);
RTListInit(&pDie->ChildList);
}
return pDie;
}
/**
* Free all children of a DIE.
*
* @param pThis The DWARF instance.
* @param pParent The parent DIE.
*/
static void rtDwarfInfo_FreeChildren(PRTDBGMODDWARF pThis, PRTDWARFDIE pParentDie)
{
PRTDWARFDIE pChild, pNextChild;
RTListForEachSafe(&pParentDie->ChildList, pChild, pNextChild, RTDWARFDIE, SiblingNode)
{
if (!RTListIsEmpty(&pChild->ChildList))
rtDwarfInfo_FreeChildren(pThis, pChild);
RTListNodeRemove(&pChild->SiblingNode);
#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
RTMemCacheFree(pThis->aDieAllocators[pChild->iAllocator].hMemCache, pChild);
#else
RTMemFree(pChild);
#endif
}
}
/**
* Free a DIE an all its children.
*
* @param pThis The DWARF instance.
* @param pDie The DIE to free.
*/
static void rtDwarfInfo_FreeDie(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie)
{
rtDwarfInfo_FreeChildren(pThis, pDie);
RTListNodeRemove(&pDie->SiblingNode);
#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
RTMemCacheFree(pThis->aDieAllocators[pDie->iAllocator].hMemCache, pDie);
#else
RTMemFree(pChild);
#endif
}
/**
* Skips a form.
* @returns IPRT status code
* @param pCursor The cursor.
* @param uForm The form to skip.
*/
static int rtDwarfInfo_SkipForm(PRTDWARFCURSOR pCursor, uint32_t uForm)
{
switch (uForm)
{
case DW_FORM_addr:
return rtDwarfCursor_SkipBytes(pCursor, pCursor->cbNativeAddr);
case DW_FORM_block:
case DW_FORM_exprloc:
return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetULeb128(pCursor, 0));
case DW_FORM_block1:
return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU8(pCursor, 0));
case DW_FORM_block2:
return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU16(pCursor, 0));
case DW_FORM_block4:
return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU32(pCursor, 0));
case DW_FORM_data1:
case DW_FORM_ref1:
case DW_FORM_flag:
return rtDwarfCursor_SkipBytes(pCursor, 1);
case DW_FORM_data2:
case DW_FORM_ref2:
return rtDwarfCursor_SkipBytes(pCursor, 2);
case DW_FORM_data4:
case DW_FORM_ref4:
return rtDwarfCursor_SkipBytes(pCursor, 4);
case DW_FORM_data8:
case DW_FORM_ref8:
case DW_FORM_ref_sig8:
return rtDwarfCursor_SkipBytes(pCursor, 8);
case DW_FORM_udata:
case DW_FORM_sdata:
case DW_FORM_ref_udata:
return rtDwarfCursor_SkipLeb128(pCursor);
case DW_FORM_string:
rtDwarfCursor_GetSZ(pCursor, NULL);
return pCursor->rc;
case DW_FORM_indirect:
return rtDwarfInfo_SkipForm(pCursor, rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX));
case DW_FORM_strp:
case DW_FORM_ref_addr:
case DW_FORM_sec_offset:
return rtDwarfCursor_SkipBytes(pCursor, pCursor->f64bitDwarf ? 8 : 4);
case DW_FORM_flag_present:
return pCursor->rc; /* no data */
default:
return VERR_DWARF_UNKNOWN_FORM;
}
}
#ifdef SOME_UNUSED_FUNCTION
/**
* Skips a DIE.
*
* @returns IPRT status code.
* @param pCursor The cursor.
* @param pAbbrevCursor The abbreviation cursor.
*/
static int rtDwarfInfo_SkipDie(PRTDWARFCURSOR pCursor, PRTDWARFCURSOR pAbbrevCursor)
{
for (;;)
{
uint32_t uAttr = rtDwarfCursor_GetULeb128AsU32(pAbbrevCursor, 0);
uint32_t uForm = rtDwarfCursor_GetULeb128AsU32(pAbbrevCursor, 0);
if (uAttr == 0 && uForm == 0)
break;
int rc = rtDwarfInfo_SkipForm(pCursor, uForm);
if (RT_FAILURE(rc))
return rc;
}
return RT_FAILURE(pCursor->rc) ? pCursor->rc : pAbbrevCursor->rc;
}
#endif
/**
* Parse the attributes of a DIE.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
* @param pDie The internal DIE structure to fill.
* @param pDieDesc The DIE descriptor.
* @param pCursor The debug_info cursor.
* @param pAbbrev The abbreviation cache entry.
* @param fInitDie Whether to initialize the DIE first. If not (@c
* false) it's safe to assume we're following a
* DW_AT_specification or DW_AT_abstract_origin,
* and that we shouldn't be snooping any symbols.
*/
static int rtDwarfInfo_ParseDie(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie, PCRTDWARFDIEDESC pDieDesc,
PRTDWARFCURSOR pCursor, PCRTDWARFABBREV pAbbrev, bool fInitDie)
{
RTDWARFCURSOR AbbrevCursor;
int rc = rtDwarfCursor_InitWithOffset(&AbbrevCursor, pThis, krtDbgModDwarfSect_abbrev, pAbbrev->offSpec);
if (RT_FAILURE(rc))
return rc;
if (fInitDie)
rtDwarfInfo_InitDie(pDie, pDieDesc);
for (;;)
{
uint32_t uAttr = rtDwarfCursor_GetULeb128AsU32(&AbbrevCursor, 0);
uint32_t uForm = rtDwarfCursor_GetULeb128AsU32(&AbbrevCursor, 0);
if (uAttr == 0)
break;
if (uForm == DW_FORM_indirect)
uForm = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
/* Look up the attribute in the descriptor and invoke the decoder. */
PCRTDWARFATTRDESC pAttr = NULL;
size_t i = pDieDesc->cAttributes;
while (i-- > 0)
if (pDieDesc->paAttributes[i].uAttr == uAttr)
{
pAttr = &pDieDesc->paAttributes[i];
rc = pAttr->pfnDecoder(pDie, (uint8_t *)pDie + pAttr->off, pAttr, uForm, pCursor);
break;
}
/* Some house keeping. */
if (pAttr)
pDie->cDecodedAttrs++;
else
{
pDie->cUnhandledAttrs++;
rc = rtDwarfInfo_SkipForm(pCursor, uForm);
Log4((" %-20s [%s]\n", rtDwarfLog_AttrName(uAttr), rtDwarfLog_FormName(uForm)));
}
if (RT_FAILURE(rc))
break;
}
rc = rtDwarfCursor_Delete(&AbbrevCursor, rc);
if (RT_SUCCESS(rc))
rc = pCursor->rc;
/*
* Snoop up symbols on the way out.
*/
if (RT_SUCCESS(rc) && fInitDie)
{
rc = rtDwarfInfo_SnoopSymbols(pThis, pDie);
/* Ignore duplicates, get work done instead. */
/** @todo clean up global/static symbol mess. */
if (rc == VERR_DBG_DUPLICATE_SYMBOL)
rc = VINF_SUCCESS;
}
return rc;
}
/**
* Load the debug information of a unit.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
* @param pCursor The debug_info cursor.
* @param fKeepDies Whether to keep the DIEs or discard them as soon
* as possible.
*/
static int rtDwarfInfo_LoadUnit(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor, bool fKeepDies)
{
Log(("rtDwarfInfo_LoadUnit: %#x\n", rtDwarfCursor_CalcSectOffsetU32(pCursor)));
/*
* Read the compilation unit header.
*/
uint64_t offUnit = rtDwarfCursor_CalcSectOffsetU32(pCursor);
uint64_t cbUnit = rtDwarfCursor_GetInitalLength(pCursor);
cbUnit += rtDwarfCursor_CalcSectOffsetU32(pCursor) - offUnit;
uint16_t const uVer = rtDwarfCursor_GetUHalf(pCursor, 0);
if ( uVer < 2
|| uVer > 4)
return rtDwarfCursor_SkipUnit(pCursor);
uint64_t const offAbbrev = rtDwarfCursor_GetUOff(pCursor, UINT64_MAX);
uint8_t const cbNativeAddr = rtDwarfCursor_GetU8(pCursor, UINT8_MAX);
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
Log((" uVer=%d offAbbrev=%#llx cbNativeAddr=%d\n", uVer, offAbbrev, cbNativeAddr));
/*
* Set up the abbreviation cache and store the native address size in the cursor.
*/
if (offAbbrev > UINT32_MAX)
{
Log(("Unexpected abbrviation code offset of %#llx\n", offAbbrev));
return VERR_DWARF_BAD_INFO;
}
rtDwarfAbbrev_SetUnitOffset(pThis, (uint32_t)offAbbrev);
pCursor->cbNativeAddr = cbNativeAddr;
/*
* The first DIE is a compile or partial unit, parse it here.
*/
uint32_t uAbbrCode = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
if (!uAbbrCode)
{
Log(("Unexpected abbrviation code of zero\n"));
return VERR_DWARF_BAD_INFO;
}
PCRTDWARFABBREV pAbbrev = rtDwarfAbbrev_Lookup(pThis, uAbbrCode);
if (!pAbbrev)
return VERR_DWARF_ABBREV_NOT_FOUND;
if ( pAbbrev->uTag != DW_TAG_compile_unit
&& pAbbrev->uTag != DW_TAG_partial_unit)
{
Log(("Unexpected compile/partial unit tag %#x\n", pAbbrev->uTag));
return VERR_DWARF_BAD_INFO;
}
PRTDWARFDIECOMPILEUNIT pUnit;
pUnit = (PRTDWARFDIECOMPILEUNIT)rtDwarfInfo_NewDie(pThis, &g_CompileUnitDesc, pAbbrev, NULL /*pParent*/);
if (!pUnit)
return VERR_NO_MEMORY;
pUnit->offUnit = offUnit;
pUnit->cbUnit = cbUnit;
pUnit->offAbbrev = offAbbrev;
pUnit->cbNativeAddr = cbNativeAddr;
pUnit->uDwarfVer = (uint8_t)uVer;
RTListAppend(&pThis->CompileUnitList, &pUnit->Core.SiblingNode);
int rc = rtDwarfInfo_ParseDie(pThis, &pUnit->Core, &g_CompileUnitDesc, pCursor, pAbbrev, true /*fInitDie*/);
if (RT_FAILURE(rc))
return rc;
/*
* Parse DIEs.
*/
uint32_t cDepth = 0;
PRTDWARFDIE pParentDie = &pUnit->Core;
while (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
{
#ifdef LOG_ENABLED
uint32_t offLog = rtDwarfCursor_CalcSectOffsetU32(pCursor);
#endif
uAbbrCode = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
if (!uAbbrCode)
{
/* End of siblings, up one level. (Is this correct?) */
if (pParentDie->pParent)
{
pParentDie = pParentDie->pParent;
cDepth--;
if (!fKeepDies && pParentDie->pParent)
rtDwarfInfo_FreeChildren(pThis, pParentDie);
}
}
else
{
/*
* Look up the abbreviation and match the tag up with a descriptor.
*/
pAbbrev = rtDwarfAbbrev_Lookup(pThis, uAbbrCode);
if (!pAbbrev)
return VERR_DWARF_ABBREV_NOT_FOUND;
PCRTDWARFDIEDESC pDieDesc;
const char *pszName;
if (pAbbrev->uTag < RT_ELEMENTS(g_aTagDescs))
{
Assert(g_aTagDescs[pAbbrev->uTag].uTag == pAbbrev->uTag || g_aTagDescs[pAbbrev->uTag].uTag == 0);
pszName = g_aTagDescs[pAbbrev->uTag].pszName;
pDieDesc = g_aTagDescs[pAbbrev->uTag].pDesc;
}
else
{
pszName = "<unknown>";
pDieDesc = &g_CoreDieDesc;
}
Log4(("%08x: %*stag=%s (%#x, abbrev %u)%s\n", offLog, cDepth * 2, "", pszName,
pAbbrev->uTag, uAbbrCode, pAbbrev->fChildren ? " has children" : ""));
/*
* Create a new internal DIE structure and parse the
* attributes.
*/
PRTDWARFDIE pNewDie = rtDwarfInfo_NewDie(pThis, pDieDesc, pAbbrev, pParentDie);
if (!pNewDie)
return VERR_NO_MEMORY;
if (pAbbrev->fChildren)
{
pParentDie = pNewDie;
cDepth++;
}
rc = rtDwarfInfo_ParseDie(pThis, pNewDie, pDieDesc, pCursor, pAbbrev, true /*fInitDie*/);
if (RT_FAILURE(rc))
return rc;
if (!fKeepDies && !pAbbrev->fChildren)
rtDwarfInfo_FreeDie(pThis, pNewDie);
}
} /* while more DIEs */
/* Unlink and free child DIEs if told to do so. */
if (!fKeepDies)
rtDwarfInfo_FreeChildren(pThis, &pUnit->Core);
return RT_SUCCESS(rc) ? pCursor->rc : rc;
}
/**
* Extracts the symbols.
*
* The symbols are insered into the debug info container.
*
* @returns IPRT status code
* @param pThis The DWARF instance.
*/
static int rtDwarfInfo_LoadAll(PRTDBGMODDWARF pThis)
{
RTDWARFCURSOR Cursor;
int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_info);
if (RT_SUCCESS(rc))
{
while ( !rtDwarfCursor_IsAtEnd(&Cursor)
&& RT_SUCCESS(rc))
rc = rtDwarfInfo_LoadUnit(pThis, &Cursor, false /* fKeepDies */);
rc = rtDwarfCursor_Delete(&Cursor, rc);
}
return rc;
}
/*
*
* DWARF Debug module implementation.
* DWARF Debug module implementation.
* DWARF Debug module implementation.
*
*/
/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByAddr} */
static DECLCALLBACK(int) rtDbgModDwarf_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
return RTDbgModLineByAddr(pThis->hCnt, iSeg, off, poffDisp, pLineInfo);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByOrdinal} */
static DECLCALLBACK(int) rtDbgModDwarf_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
return RTDbgModLineByOrdinal(pThis->hCnt, iOrdinal, pLineInfo);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnLineCount} */
static DECLCALLBACK(uint32_t) rtDbgModDwarf_LineCount(PRTDBGMODINT pMod)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
return RTDbgModLineCount(pThis->hCnt);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnLineAdd} */
static DECLCALLBACK(int) rtDbgModDwarf_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
Assert(!pszFile[cchFile]); NOREF(cchFile);
return RTDbgModLineAdd(pThis->hCnt, pszFile, uLineNo, iSeg, off, piOrdinal);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByAddr} */
static DECLCALLBACK(int) rtDbgModDwarf_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
return RTDbgModSymbolByAddr(pThis->hCnt, iSeg, off, fFlags, poffDisp, pSymInfo);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByName} */
static DECLCALLBACK(int) rtDbgModDwarf_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
PRTDBGSYMBOL pSymInfo)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
Assert(!pszSymbol[cchSymbol]);
return RTDbgModSymbolByName(pThis->hCnt, pszSymbol/*, cchSymbol*/, pSymInfo);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByOrdinal} */
static DECLCALLBACK(int) rtDbgModDwarf_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
return RTDbgModSymbolByOrdinal(pThis->hCnt, iOrdinal, pSymInfo);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolCount} */
static DECLCALLBACK(uint32_t) rtDbgModDwarf_SymbolCount(PRTDBGMODINT pMod)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
return RTDbgModSymbolCount(pThis->hCnt);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolAdd} */
static DECLCALLBACK(int) rtDbgModDwarf_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
uint32_t *piOrdinal)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
Assert(!pszSymbol[cchSymbol]); NOREF(cchSymbol);
return RTDbgModSymbolAdd(pThis->hCnt, pszSymbol, iSeg, off, cb, fFlags, piOrdinal);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentByIndex} */
static DECLCALLBACK(int) rtDbgModDwarf_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
return RTDbgModSegmentByIndex(pThis->hCnt, iSeg, pSegInfo);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentCount} */
static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDwarf_SegmentCount(PRTDBGMODINT pMod)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
return RTDbgModSegmentCount(pThis->hCnt);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentAdd} */
static DECLCALLBACK(int) rtDbgModDwarf_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName, size_t cchName,
uint32_t fFlags, PRTDBGSEGIDX piSeg)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
Assert(!pszName[cchName]); NOREF(cchName);
return RTDbgModSegmentAdd(pThis->hCnt, uRva, cb, pszName, fFlags, piSeg);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnImageSize} */
static DECLCALLBACK(RTUINTPTR) rtDbgModDwarf_ImageSize(PRTDBGMODINT pMod)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
RTUINTPTR cb1 = RTDbgModImageSize(pThis->hCnt);
RTUINTPTR cb2 = pThis->pImgMod->pImgVt->pfnImageSize(pMod);
return RT_MAX(cb1, cb2);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnRvaToSegOff} */
static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDwarf_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
return RTDbgModRvaToSegOff(pThis->hCnt, uRva, poffSeg);
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnClose} */
static DECLCALLBACK(int) rtDbgModDwarf_Close(PRTDBGMODINT pMod)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
for (unsigned iSect = 0; iSect < RT_ELEMENTS(pThis->aSections); iSect++)
if (pThis->aSections[iSect].pv)
pThis->pDbgInfoMod->pImgVt->pfnUnmapPart(pThis->pDbgInfoMod, pThis->aSections[iSect].cb, &pThis->aSections[iSect].pv);
RTDbgModRelease(pThis->hCnt);
RTMemFree(pThis->paCachedAbbrevs);
if (pThis->pNestedMod)
{
pThis->pNestedMod->pImgVt->pfnClose(pThis->pNestedMod);
RTStrCacheRelease(g_hDbgModStrCache, pThis->pNestedMod->pszName);
RTStrCacheRelease(g_hDbgModStrCache, pThis->pNestedMod->pszDbgFile);
RTMemFree(pThis->pNestedMod);
pThis->pNestedMod = NULL;
}
#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
uint32_t i = RT_ELEMENTS(pThis->aDieAllocators);
while (i-- > 0)
{
RTMemCacheDestroy(pThis->aDieAllocators[i].hMemCache);
pThis->aDieAllocators[i].hMemCache = NIL_RTMEMCACHE;
}
#endif
RTMemFree(pThis);
return VINF_SUCCESS;
}
/** @callback_method_impl{FNRTLDRENUMDBG} */
static DECLCALLBACK(int) rtDbgModDwarfEnumCallback(RTLDRMOD hLdrMod, PCRTLDRDBGINFO pDbgInfo, void *pvUser)
{
/*
* Skip stuff we can't handle.
*/
if (pDbgInfo->enmType != RTLDRDBGINFOTYPE_DWARF)
return VINF_SUCCESS;
const char *pszSection = pDbgInfo->u.Dwarf.pszSection;
if (!pszSection || !*pszSection)
return VINF_SUCCESS;
Assert(!pDbgInfo->pszExtFile);
/*
* Must have a part name starting with debug_ and possibly prefixed by dots
* or underscores.
*/
if (!strncmp(pszSection, RT_STR_TUPLE(".debug_"))) /* ELF */
pszSection += sizeof(".debug_") - 1;
else if (!strncmp(pszSection, RT_STR_TUPLE("__debug_"))) /* Mach-O */
pszSection += sizeof("__debug_") - 1;
else if (!strcmp(pszSection, ".WATCOM_references"))
return VINF_SUCCESS; /* Ignore special watcom section for now.*/
else if ( !strcmp(pszSection, "__apple_types")
|| !strcmp(pszSection, "__apple_namespac")
|| !strcmp(pszSection, "__apple_objc")
|| !strcmp(pszSection, "__apple_names"))
return VINF_SUCCESS; /* Ignore special apple sections for now. */
else
AssertMsgFailedReturn(("%s\n", pszSection), VINF_SUCCESS /*ignore*/);
/*
* Figure out which part we're talking about.
*/
krtDbgModDwarfSect enmSect;
if (0) { /* dummy */ }
#define ELSE_IF_STRCMP_SET(a_Name) else if (!strcmp(pszSection, #a_Name)) enmSect = krtDbgModDwarfSect_ ## a_Name
ELSE_IF_STRCMP_SET(abbrev);
ELSE_IF_STRCMP_SET(aranges);
ELSE_IF_STRCMP_SET(frame);
ELSE_IF_STRCMP_SET(info);
ELSE_IF_STRCMP_SET(inlined);
ELSE_IF_STRCMP_SET(line);
ELSE_IF_STRCMP_SET(loc);
ELSE_IF_STRCMP_SET(macinfo);
ELSE_IF_STRCMP_SET(pubnames);
ELSE_IF_STRCMP_SET(pubtypes);
ELSE_IF_STRCMP_SET(ranges);
ELSE_IF_STRCMP_SET(str);
ELSE_IF_STRCMP_SET(types);
#undef ELSE_IF_STRCMP_SET
else
{
AssertMsgFailed(("%s\n", pszSection));
return VINF_SUCCESS;
}
/*
* Record the section.
*/
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
AssertMsgReturn(!pThis->aSections[enmSect].fPresent, ("duplicate %s\n", pszSection), VINF_SUCCESS /*ignore*/);
pThis->aSections[enmSect].fPresent = true;
pThis->aSections[enmSect].offFile = pDbgInfo->offFile;
pThis->aSections[enmSect].pv = NULL;
pThis->aSections[enmSect].cb = (size_t)pDbgInfo->cb;
pThis->aSections[enmSect].iDbgInfo = pDbgInfo->iDbgInfo;
if (pThis->aSections[enmSect].cb != pDbgInfo->cb)
pThis->aSections[enmSect].cb = ~(size_t)0;
return VINF_SUCCESS;
}
static int rtDbgModDwarfTryOpenDbgFile(PRTDBGMODINT pDbgMod, PRTDBGMODDWARF pThis, RTLDRARCH enmArch)
{
if ( !pDbgMod->pszDbgFile
|| RTPathIsSame(pDbgMod->pszDbgFile, pDbgMod->pszImgFile) == (int)true /* returns VERR too */)
return VERR_DBG_NO_MATCHING_INTERPRETER;
/*
* Only open the image.
*/
PRTDBGMODINT pDbgInfoMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgInfoMod));
if (!pDbgInfoMod)
return VERR_NO_MEMORY;
int rc;
pDbgInfoMod->u32Magic = RTDBGMOD_MAGIC;
pDbgInfoMod->cRefs = 1;
if (RTStrCacheRetain(pDbgMod->pszDbgFile) != UINT32_MAX)
{
pDbgInfoMod->pszImgFile = pDbgMod->pszDbgFile;
if (RTStrCacheRetain(pDbgMod->pszName) != UINT32_MAX)
{
pDbgInfoMod->pszName = pDbgMod->pszName;
pDbgInfoMod->pImgVt = &g_rtDbgModVtImgLdr;
rc = pDbgInfoMod->pImgVt->pfnTryOpen(pDbgInfoMod, enmArch);
if (RT_SUCCESS(rc))
{
pThis->pDbgInfoMod = pDbgInfoMod;
pThis->pNestedMod = pDbgInfoMod;
return VINF_SUCCESS;
}
RTStrCacheRelease(g_hDbgModStrCache, pDbgInfoMod->pszName);
}
else
rc = VERR_NO_STR_MEMORY;
RTStrCacheRelease(g_hDbgModStrCache, pDbgInfoMod->pszImgFile);
}
else
rc = VERR_NO_STR_MEMORY;
RTMemFree(pDbgInfoMod);
return rc;
}
/** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
static DECLCALLBACK(int) rtDbgModDwarf_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
{
/*
* DWARF is only supported when part of an image.
*/
if (!pMod->pImgVt)
return VERR_DBG_NO_MATCHING_INTERPRETER;
/*
* Create the module instance data.
*/
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)RTMemAllocZ(sizeof(*pThis));
if (!pThis)
return VERR_NO_MEMORY;
pThis->pDbgInfoMod = pMod;
pThis->pImgMod = pMod;
RTListInit(&pThis->CompileUnitList);
/** @todo better fUseLinkAddress heuristics! */
if ( (pMod->pszDbgFile && strstr(pMod->pszDbgFile, "mach_kernel"))
|| (pMod->pszImgFile && strstr(pMod->pszImgFile, "mach_kernel")) )
pThis->fUseLinkAddress = true;
#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
AssertCompile(RT_ELEMENTS(pThis->aDieAllocators) == 2);
pThis->aDieAllocators[0].cbMax = sizeof(RTDWARFDIE);
pThis->aDieAllocators[1].cbMax = sizeof(RTDWARFDIECOMPILEUNIT);
for (uint32_t i = 0; i < RT_ELEMENTS(g_aTagDescs); i++)
if (g_aTagDescs[i].pDesc && g_aTagDescs[i].pDesc->cbDie > pThis->aDieAllocators[1].cbMax)
pThis->aDieAllocators[1].cbMax = (uint32_t)g_aTagDescs[i].pDesc->cbDie;
pThis->aDieAllocators[1].cbMax = RT_ALIGN_32(pThis->aDieAllocators[1].cbMax, sizeof(uint64_t));
for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aDieAllocators); i++)
{
int rc = RTMemCacheCreate(&pThis->aDieAllocators[i].hMemCache, pThis->aDieAllocators[i].cbMax, sizeof(uint64_t),
UINT32_MAX, NULL /*pfnCtor*/, NULL /*pfnDtor*/, NULL /*pvUser*/, 0 /*fFlags*/);
if (RT_FAILURE(rc))
{
while (i-- > 0)
RTMemCacheDestroy(pThis->aDieAllocators[i].hMemCache);
RTMemFree(pThis);
return rc;
}
}
#endif
/*
* If the debug file name is set, let's see if it's an ELF image with DWARF
* inside it. In that case we'll have to deal with two image modules, one
* for segments and address translation and one for the debug information.
*/
if (pMod->pszDbgFile != NULL)
rtDbgModDwarfTryOpenDbgFile(pMod, pThis, enmArch);
/*
* Enumerate the debug info in the module, looking for DWARF bits.
*/
int rc = pThis->pDbgInfoMod->pImgVt->pfnEnumDbgInfo(pThis->pDbgInfoMod, rtDbgModDwarfEnumCallback, pThis);
if (RT_SUCCESS(rc))
{
if (pThis->aSections[krtDbgModDwarfSect_info].fPresent)
{
/*
* Extract / explode the data we want (symbols and line numbers)
* storing them in a container module.
*/
rc = RTDbgModCreate(&pThis->hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
if (RT_SUCCESS(rc))
{
pMod->pvDbgPriv = pThis;
rc = rtDbgModDwarfAddSegmentsFromImage(pThis);
if (RT_SUCCESS(rc))
rc = rtDwarfInfo_LoadAll(pThis);
if (RT_SUCCESS(rc))
rc = rtDwarfLine_ExplodeAll(pThis);
if (RT_SUCCESS(rc) && pThis->iWatcomPass == 1)
{
rc = rtDbgModDwarfAddSegmentsFromPass1(pThis);
pThis->iWatcomPass = 2;
if (RT_SUCCESS(rc))
rc = rtDwarfInfo_LoadAll(pThis);
if (RT_SUCCESS(rc))
rc = rtDwarfLine_ExplodeAll(pThis);
}
if (RT_SUCCESS(rc))
{
/*
* Free the cached abbreviations and unload all sections.
*/
pThis->cCachedAbbrevsAlloced = 0;
RTMemFree(pThis->paCachedAbbrevs);
pThis->paCachedAbbrevs = NULL;
for (unsigned iSect = 0; iSect < RT_ELEMENTS(pThis->aSections); iSect++)
if (pThis->aSections[iSect].pv)
pThis->pDbgInfoMod->pImgVt->pfnUnmapPart(pThis->pDbgInfoMod, pThis->aSections[iSect].cb,
&pThis->aSections[iSect].pv);
/** @todo Kill pThis->CompileUnitList and the alloc caches. */
return VINF_SUCCESS;
}
/* bail out. */
RTDbgModRelease(pThis->hCnt);
pMod->pvDbgPriv = NULL;
}
}
else
rc = VERR_DBG_NO_MATCHING_INTERPRETER;
}
RTMemFree(pThis->paCachedAbbrevs);
#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
uint32_t i = RT_ELEMENTS(pThis->aDieAllocators);
while (i-- > 0)
{
RTMemCacheDestroy(pThis->aDieAllocators[i].hMemCache);
pThis->aDieAllocators[i].hMemCache = NIL_RTMEMCACHE;
}
#endif
RTMemFree(pThis);
return rc;
}
/** Virtual function table for the DWARF debug info reader. */
DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgDwarf =
{
/*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
/*.fSupports = */ RT_DBGTYPE_DWARF,
/*.pszName = */ "dwarf",
/*.pfnTryOpen = */ rtDbgModDwarf_TryOpen,
/*.pfnClose = */ rtDbgModDwarf_Close,
/*.pfnRvaToSegOff = */ rtDbgModDwarf_RvaToSegOff,
/*.pfnImageSize = */ rtDbgModDwarf_ImageSize,
/*.pfnSegmentAdd = */ rtDbgModDwarf_SegmentAdd,
/*.pfnSegmentCount = */ rtDbgModDwarf_SegmentCount,
/*.pfnSegmentByIndex = */ rtDbgModDwarf_SegmentByIndex,
/*.pfnSymbolAdd = */ rtDbgModDwarf_SymbolAdd,
/*.pfnSymbolCount = */ rtDbgModDwarf_SymbolCount,
/*.pfnSymbolByOrdinal = */ rtDbgModDwarf_SymbolByOrdinal,
/*.pfnSymbolByName = */ rtDbgModDwarf_SymbolByName,
/*.pfnSymbolByAddr = */ rtDbgModDwarf_SymbolByAddr,
/*.pfnLineAdd = */ rtDbgModDwarf_LineAdd,
/*.pfnLineCount = */ rtDbgModDwarf_LineCount,
/*.pfnLineByOrdinal = */ rtDbgModDwarf_LineByOrdinal,
/*.pfnLineByAddr = */ rtDbgModDwarf_LineByAddr,
/*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
};
|