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
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
|
/* $Id: dbgmoddwarf.cpp $ */
/** @file
* IPRT - Debug Info Reader For DWARF.
*/
/*
* Copyright (C) 2011-2019 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 <iprt/x86.h>
#include <iprt/formats/dwarf.h>
#include "internal/dbgmod.h"
/*********************************************************************************************************************************
* 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;
#ifdef LOG_ENABLED
uint8_t cbHdr; /**< For calcing ABGOFF matching dwarfdump. */
#endif
/** 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. (Can be NULL for eh_frame.) */
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 pCursor 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;
/** Pointer to the array of attributes. */
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)
# if 0 /* unused */
/**
* 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;
}
# endif
/**
* 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);
RT_CASE_RET_STR(DW_AT_WATCOM_memory_model);
RT_CASE_RET_STR(DW_AT_WATCOM_references_start);
RT_CASE_RET_STR(DW_AT_WATCOM_parm_entry);
}
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 = pSeg->cb;
if ( cb < pSeg->cbMapped
&& RTLdrGetFormat(hLdrMod) != RTLDRFMT_LX /* for debugging our drivers; 64KB section align by linker, 4KB by loader. */
)
cb = pSeg->cbMapped;
return RTDbgModSegmentAdd(pThis->hCnt, pSeg->RVA, cb, pSeg->pszName, 0 /*fFlags*/, NULL);
}
/**
* Calls rtDbgModDwarfAddSegmentsCallback 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);
/* If we have a non-zero segment number, assume it's correct for now.
This helps loading watcom linked LX drivers. */
if (uSegment > 0)
{
*piSeg = uSegment - 1;
*poffSeg = LinkAddress;
return VINF_SUCCESS;
}
return pThis->pImgMod->pImgVt->pfnRvaToSegOffset(pThis->pImgMod, LinkAddress, piSeg, poffSeg);
}
/**
* Converts a segment+offset address into an RVA.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
* @param idxSegment The segment index.
* @param offSegment The segment offset.
* @param puRva Where to return the calculated RVA.
*/
static int rtDbgModDwarfSegOffsetToRva(PRTDBGMODDWARF pThis, RTDBGSEGIDX idxSegment, uint64_t offSegment, PRTUINTPTR puRva)
{
if (pThis->paSegs)
{
PRTDBGDWARFSEG pSeg = rtDbgModDwarfFindSegment(pThis, idxSegment);
if (pSeg)
{
*puRva = pSeg->uBaseAddr + offSegment;
return VINF_SUCCESS;
}
}
RTUINTPTR uRva = RTDbgModSegmentRva(pThis->pImgMod, idxSegment);
if (uRva != RTUINTPTR_MAX)
{
*puRva = uRva + offSegment;
return VINF_SUCCESS;
}
return VERR_INVALID_POINTER;
}
/**
* Converts a segment+offset address into an RVA.
*
* @returns IPRT status code.
* @param pThis The DWARF instance.
* @param uRva The RVA to convert.
* @param pidxSegment Where to return the segment index.
* @param poffSegment Where to return the segment offset.
*/
static int rtDbgModDwarfRvaToSegOffset(PRTDBGMODDWARF pThis, RTUINTPTR uRva, RTDBGSEGIDX *pidxSegment, uint64_t *poffSegment)
{
RTUINTPTR offSeg = 0;
RTDBGSEGIDX idxSeg = RTDbgModRvaToSegOff(pThis->pImgMod, uRva, &offSeg);
if (idxSeg != NIL_RTDBGSEGIDX)
{
*pidxSegment = idxSeg;
*poffSegment = offSeg;
return VINF_SUCCESS;
}
return VERR_INVALID_POINTER;
}
/*
*
* 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 unsigned 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 iErrValue 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;
}
}
/**
* Reads a 1, 2, 4 or 8 byte unsigned value.
*
* @returns 64-bit unsigned value.
* @param pCursor The cursor.
* @param bPtrEnc The pointer encoding.
* @param uErrValue The error value.
*/
static uint64_t rtDwarfCursor_GetPtrEnc(PRTDWARFCURSOR pCursor, uint8_t bPtrEnc, uint64_t uErrValue)
{
uint64_t u64Ret;
switch (bPtrEnc & DW_EH_PE_FORMAT_MASK)
{
case DW_EH_PE_ptr:
u64Ret = rtDwarfCursor_GetNativeUOff(pCursor, uErrValue);
break;
case DW_EH_PE_uleb128:
u64Ret = rtDwarfCursor_GetULeb128(pCursor, uErrValue);
break;
case DW_EH_PE_udata2:
u64Ret = rtDwarfCursor_GetU16(pCursor, UINT16_MAX);
break;
case DW_EH_PE_udata4:
u64Ret = rtDwarfCursor_GetU32(pCursor, UINT32_MAX);
break;
case DW_EH_PE_udata8:
u64Ret = rtDwarfCursor_GetU64(pCursor, UINT64_MAX);
break;
case DW_EH_PE_sleb128:
u64Ret = rtDwarfCursor_GetSLeb128(pCursor, uErrValue);
break;
case DW_EH_PE_sdata2:
u64Ret = (int64_t)(int16_t)rtDwarfCursor_GetU16(pCursor, UINT16_MAX);
break;
case DW_EH_PE_sdata4:
u64Ret = (int64_t)(int32_t)rtDwarfCursor_GetU32(pCursor, UINT32_MAX);
break;
case DW_EH_PE_sdata8:
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;
}
/**
* 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_GetInitialLength(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 - pCursor->pbStart;
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 true if at the end or a cursor error is pending.
* @retval 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 true if at the end or a cursor error is pending.
* @retval 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 a skip 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 to skip 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; - we're skipping, offsets are relative to start of section... */
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;
}
/**
* Initialize a reader cursor for a memory block (eh_frame).
*
* @returns IPRT status code.
* @param pCursor The cursor.
* @param pvMem The memory block.
* @param cbMem The size of the memory block.
*/
static int rtDwarfCursor_InitForMem(PRTDWARFCURSOR pCursor, void const *pvMem, size_t cbMem)
{
pCursor->enmSect = krtDbgModDwarfSect_End;
pCursor->pbStart = (uint8_t const *)pvMem;
pCursor->pb = (uint8_t const *)pvMem;
pCursor->cbLeft = cbMem;
pCursor->cbUnitLeft = cbMem;
pCursor->pDwarfMod = NULL;
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;
}
/**
* 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 Frame Unwind Information.
* DWARF Frame Unwind Information.
* DWARF Frame Unwind Information.
*
*/
/**
* Common information entry (CIE) information.
*/
typedef struct RTDWARFCIEINFO
{
/** The segment location of the CIE. */
uint64_t offCie;
/** The DWARF version. */
uint8_t uDwarfVer;
/** The address pointer encoding. */
uint8_t bAddressPtrEnc;
/** The segment size (v4). */
uint8_t cbSegment;
/** The return register column. UINT8_MAX if default register. */
uint8_t bRetReg;
/** The LSDA pointer encoding. */
uint8_t bLsdaPtrEnc;
/** Set if the EH data field is present ('eh'). */
bool fHasEhData : 1;
/** Set if there is an augmentation data size ('z'). */
bool fHasAugmentationSize : 1;
/** Set if the augmentation data contains a LSDA (pointer size byte in CIE,
* pointer in FDA) ('L'). */
bool fHasLanguageSpecificDataArea : 1;
/** Set if the augmentation data contains a personality routine
* (pointer size + pointer) ('P'). */
bool fHasPersonalityRoutine : 1;
/** Set if the augmentation data contains the address encoding . */
bool fHasAddressEnc : 1;
/** Set if signal frame. */
bool fIsSignalFrame : 1;
/** Set if we've encountered unknown augmentation data. This
* means the CIE is incomplete and cannot be used. */
bool fHasUnknowAugmentation : 1;
/** Copy of the augmentation string. */
const char *pszAugmentation;
/** Code alignment factor for the instruction. */
uint64_t uCodeAlignFactor;
/** Data alignment factor for the instructions. */
int64_t iDataAlignFactor;
/** Pointer to the instruction sequence. */
uint8_t const *pbInstructions;
/** The length of the instruction sequence. */
size_t cbInstructions;
} RTDWARFCIEINFO;
/** Pointer to CIE info. */
typedef RTDWARFCIEINFO *PRTDWARFCIEINFO;
/** Pointer to const CIE info. */
typedef RTDWARFCIEINFO const *PCRTDWARFCIEINFO;
/** Number of registers we care about.
* @note We're currently not expecting to be decoding ppc, arm, ia64 or such,
* only x86 and x86_64. We can easily increase the column count. */
#define RTDWARFCF_MAX_REGISTERS 96
/**
* Call frame state row.
*/
typedef struct RTDWARFCFROW
{
/** Stack worked by DW_CFA_remember_state and DW_CFA_restore_state. */
struct RTDWARFCFROW *pNextOnStack;
/** @name CFA - Canonical frame address expression.
* Since there are partial CFA instructions, we cannot be lazy like with the
* register but keep register+offset around. For DW_CFA_def_cfa_expression
* we just take down the program location, though.
* @{ */
/** Pointer to DW_CFA_def_cfa_expression instruction, NULL if reg+offset. */
uint8_t const *pbCfaExprInstr;
/** The CFA register offset. */
int64_t offCfaReg;
/** The CFA base register number. */
uint16_t uCfaBaseReg;
/** Set if we've got a valid CFA definition. */
bool fCfaDefined : 1;
/** @} */
/** Set if on the heap and needs freeing. */
bool fOnHeap : 1;
/** Pointer to the instructions bytes defining registers.
* NULL means */
uint8_t const *apbRegInstrs[RTDWARFCF_MAX_REGISTERS];
} RTDWARFCFROW;
typedef RTDWARFCFROW *PRTDWARFCFROW;
typedef RTDWARFCFROW const *PCRTDWARFCFROW;
/** Row program execution state. */
typedef struct RTDWARFCFEXEC
{
PRTDWARFCFROW pRow;
/** Number of PC bytes left to advance before we get a hit. */
uint64_t cbLeftToAdvance;
/** Number of pushed rows. */
uint32_t cPushes;
/** Set if little endian, clear if big endian. */
bool fLittleEndian;
/** The CIE. */
PCRTDWARFCIEINFO pCie;
/** The program counter value for the FDE. Subjected to segment.
* Needed for DW_CFA_set_loc. */
uint64_t uPcBegin;
/** The offset relative to uPcBegin for which we're searching for a row.
* Needed for DW_CFA_set_loc. */
uint64_t offInRange;
} RTDWARFCFEXEC;
typedef RTDWARFCFEXEC *PRTDWARFCFEXEC;
/* Set of macros for getting and skipping operands. */
#define SKIP_ULEB128_OR_LEB128() \
do \
{ \
AssertReturn(offInstr < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO); \
} while (pbInstr[offInstr++] & 0x80)
#define GET_ULEB128_AS_U14(a_uDst) \
do \
{ \
AssertReturn(offInstr < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO); \
uint8_t b = pbInstr[offInstr++]; \
(a_uDst) = b & 0x7f; \
if (b & 0x80) \
{ \
AssertReturn(offInstr < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO); \
b = pbInstr[offInstr++]; \
AssertReturn(!(b & 0x80), VERR_DBG_MALFORMED_UNWIND_INFO); \
(a_uDst) |= (uint16_t)b << 7; \
} \
} while (0)
#define GET_ULEB128_AS_U63(a_uDst) \
do \
{ \
AssertReturn(offInstr < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO); \
uint8_t b = pbInstr[offInstr++]; \
(a_uDst) = b & 0x7f; \
if (b & 0x80) \
{ \
unsigned cShift = 7; \
do \
{ \
AssertReturn(offInstr < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO); \
AssertReturn(cShift < 63, VERR_DWARF_LEB_OVERFLOW); \
b = pbInstr[offInstr++]; \
(a_uDst) |= (uint16_t)(b & 0x7f) << cShift; \
cShift += 7; \
} while (b & 0x80); \
} \
} while (0)
#define GET_LEB128_AS_I63(a_uDst) \
do \
{ \
AssertReturn(offInstr < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO); \
uint8_t b = pbInstr[offInstr++]; \
if (!(b & 0x80)) \
(a_uDst) = !(b & 0x40) ? b : (int64_t)(int8_t)(b | 0x80); \
else \
{ \
/* Read value into unsigned variable: */ \
unsigned cShift = 7; \
uint64_t uTmp = b & 0x7f; \
do \
{ \
AssertReturn(offInstr < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO); \
AssertReturn(cShift < 63, VERR_DWARF_LEB_OVERFLOW); \
b = pbInstr[offInstr++]; \
uTmp |= (uint16_t)(b & 0x7f) << cShift; \
cShift += 7; \
} while (b & 0x80); \
/* Sign extend before setting the destination value: */ \
cShift -= 7 + 1; \
if (uTmp & RT_BIT_64(cShift)) \
uTmp |= ~(RT_BIT_64(cShift) - 1); \
(a_uDst) = (int64_t)uTmp; \
} \
} while (0)
#define SKIP_BLOCK() \
do \
{ \
uint16_t cbBlock; \
GET_ULEB128_AS_U14(cbBlock); \
AssertReturn(offInstr + cbBlock <= cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO); \
offInstr += cbBlock; \
} while (0)
static int rtDwarfUnwind_Execute(PRTDWARFCFEXEC pExecState, uint8_t const *pbInstr, uint32_t cbInstr)
{
PRTDWARFCFROW pRow = pExecState->pRow;
for (uint32_t offInstr = 0; offInstr < cbInstr;)
{
/*
* Instruction switches.
*/
uint8_t const bInstr = pbInstr[offInstr++];
switch (bInstr & DW_CFA_high_bit_mask)
{
case DW_CFA_advance_loc:
{
uint8_t const cbAdvance = bInstr & ~DW_CFA_high_bit_mask;
if (cbAdvance > pExecState->cbLeftToAdvance)
return VINF_SUCCESS;
pExecState->cbLeftToAdvance -= cbAdvance;
break;
}
case DW_CFA_offset:
{
uint8_t iReg = bInstr & ~DW_CFA_high_bit_mask;
if (iReg < RT_ELEMENTS(pRow->apbRegInstrs))
pRow->apbRegInstrs[iReg] = &pbInstr[offInstr - 1];
SKIP_ULEB128_OR_LEB128();
break;
}
case 0:
switch (bInstr)
{
case DW_CFA_nop:
break;
/*
* Register instructions.
*/
case DW_CFA_register:
case DW_CFA_offset_extended:
case DW_CFA_offset_extended_sf:
case DW_CFA_val_offset:
case DW_CFA_val_offset_sf:
{
uint8_t const * const pbCurInstr = &pbInstr[offInstr - 1];
uint16_t iReg;
GET_ULEB128_AS_U14(iReg);
if (iReg < RT_ELEMENTS(pRow->apbRegInstrs))
pRow->apbRegInstrs[iReg] = pbCurInstr;
SKIP_ULEB128_OR_LEB128();
break;
}
case DW_CFA_expression:
case DW_CFA_val_expression:
{
uint8_t const * const pbCurInstr = &pbInstr[offInstr - 1];
uint16_t iReg;
GET_ULEB128_AS_U14(iReg);
if (iReg < RT_ELEMENTS(pRow->apbRegInstrs))
pRow->apbRegInstrs[iReg] = pbCurInstr;
SKIP_BLOCK();
break;
}
case DW_CFA_restore_extended:
{
uint8_t const * const pbCurInstr = &pbInstr[offInstr - 1];
uint16_t iReg;
GET_ULEB128_AS_U14(iReg);
if (iReg < RT_ELEMENTS(pRow->apbRegInstrs))
pRow->apbRegInstrs[iReg] = pbCurInstr;
break;
}
case DW_CFA_undefined:
{
uint16_t iReg;
GET_ULEB128_AS_U14(iReg);
if (iReg < RT_ELEMENTS(pRow->apbRegInstrs))
pRow->apbRegInstrs[iReg] = NULL;
break;
}
case DW_CFA_same_value:
{
uint8_t const * const pbCurInstr = &pbInstr[offInstr - 1];
uint16_t iReg;
GET_ULEB128_AS_U14(iReg);
if (iReg < RT_ELEMENTS(pRow->apbRegInstrs))
pRow->apbRegInstrs[iReg] = pbCurInstr;
break;
}
/*
* CFA instructions.
*/
case DW_CFA_def_cfa:
{
GET_ULEB128_AS_U14(pRow->uCfaBaseReg);
uint64_t offCfaReg;
GET_ULEB128_AS_U63(offCfaReg);
pRow->offCfaReg = offCfaReg;
pRow->pbCfaExprInstr = NULL;
pRow->fCfaDefined = true;
break;
}
case DW_CFA_def_cfa_register:
{
GET_ULEB128_AS_U14(pRow->uCfaBaseReg);
pRow->pbCfaExprInstr = NULL;
pRow->fCfaDefined = true;
/* Leaves offCfaReg as is. */
break;
}
case DW_CFA_def_cfa_offset:
{
uint64_t offCfaReg;
GET_ULEB128_AS_U63(offCfaReg);
pRow->offCfaReg = offCfaReg;
pRow->pbCfaExprInstr = NULL;
pRow->fCfaDefined = true;
/* Leaves uCfaBaseReg as is. */
break;
}
case DW_CFA_def_cfa_sf:
GET_ULEB128_AS_U14(pRow->uCfaBaseReg);
GET_LEB128_AS_I63(pRow->offCfaReg);
pRow->pbCfaExprInstr = NULL;
pRow->fCfaDefined = true;
break;
case DW_CFA_def_cfa_offset_sf:
GET_LEB128_AS_I63(pRow->offCfaReg);
pRow->pbCfaExprInstr = NULL;
pRow->fCfaDefined = true;
/* Leaves uCfaBaseReg as is. */
break;
case DW_CFA_def_cfa_expression:
pRow->pbCfaExprInstr = &pbInstr[offInstr - 1];
pRow->fCfaDefined = true;
SKIP_BLOCK();
break;
/*
* Less likely instructions:
*/
case DW_CFA_advance_loc1:
{
AssertReturn(offInstr < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO);
uint8_t const cbAdvance = pbInstr[offInstr++];
if (cbAdvance > pExecState->cbLeftToAdvance)
return VINF_SUCCESS;
pExecState->cbLeftToAdvance -= cbAdvance;
break;
}
case DW_CFA_advance_loc2:
{
AssertReturn(offInstr + 1 < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO);
uint16_t const cbAdvance = pExecState->fLittleEndian
? RT_MAKE_U16(pbInstr[offInstr], pbInstr[offInstr + 1])
: RT_MAKE_U16(pbInstr[offInstr + 1], pbInstr[offInstr]);
if (cbAdvance > pExecState->cbLeftToAdvance)
return VINF_SUCCESS;
pExecState->cbLeftToAdvance -= cbAdvance;
offInstr += 2;
break;
}
case DW_CFA_advance_loc4:
{
AssertReturn(offInstr + 3 < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO);
uint32_t const cbAdvance = pExecState->fLittleEndian
? RT_MAKE_U32_FROM_U8(pbInstr[offInstr + 0], pbInstr[offInstr + 1],
pbInstr[offInstr + 2], pbInstr[offInstr + 3])
: RT_MAKE_U32_FROM_U8(pbInstr[offInstr + 3], pbInstr[offInstr + 2],
pbInstr[offInstr + 1], pbInstr[offInstr + 0]);
if (cbAdvance > pExecState->cbLeftToAdvance)
return VINF_SUCCESS;
pExecState->cbLeftToAdvance -= cbAdvance;
offInstr += 4;
break;
}
/*
* This bugger is really annoying and probably never used.
*/
case DW_CFA_set_loc:
{
/* Ignore the segment number. */
if (pExecState->pCie->cbSegment)
{
offInstr += pExecState->pCie->cbSegment;
AssertReturn(offInstr < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO);
}
/* Retrieve the address. sigh. */
uint64_t uAddress;
switch (pExecState->pCie->bAddressPtrEnc & (DW_EH_PE_FORMAT_MASK | DW_EH_PE_indirect))
{
case DW_EH_PE_udata2:
AssertReturn(offInstr + 1 < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO);
if (pExecState->fLittleEndian)
uAddress = RT_MAKE_U16(pbInstr[offInstr], pbInstr[offInstr + 1]);
else
uAddress = RT_MAKE_U16(pbInstr[offInstr + 1], pbInstr[offInstr]);
offInstr += 2;
break;
case DW_EH_PE_sdata2:
AssertReturn(offInstr + 1 < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO);
if (pExecState->fLittleEndian)
uAddress = (int64_t)(int16_t)RT_MAKE_U16(pbInstr[offInstr], pbInstr[offInstr + 1]);
else
uAddress = (int64_t)(int16_t)RT_MAKE_U16(pbInstr[offInstr + 1], pbInstr[offInstr]);
offInstr += 2;
break;
case DW_EH_PE_udata4:
AssertReturn(offInstr + 3 < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO);
if (pExecState->fLittleEndian)
uAddress = RT_MAKE_U32_FROM_U8(pbInstr[offInstr + 0], pbInstr[offInstr + 1],
pbInstr[offInstr + 2], pbInstr[offInstr + 3]);
else
uAddress = RT_MAKE_U32_FROM_U8(pbInstr[offInstr + 3], pbInstr[offInstr + 2],
pbInstr[offInstr + 1], pbInstr[offInstr + 0]);
offInstr += 4;
break;
case DW_EH_PE_sdata4:
AssertReturn(offInstr + 3 < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO);
if (pExecState->fLittleEndian)
uAddress = (int64_t)(int32_t)RT_MAKE_U32_FROM_U8(pbInstr[offInstr + 0], pbInstr[offInstr + 1],
pbInstr[offInstr + 2], pbInstr[offInstr + 3]);
else
uAddress = (int64_t)(int32_t)RT_MAKE_U32_FROM_U8(pbInstr[offInstr + 3], pbInstr[offInstr + 2],
pbInstr[offInstr + 1], pbInstr[offInstr + 0]);
offInstr += 4;
break;
case DW_EH_PE_udata8:
case DW_EH_PE_sdata8:
AssertReturn(offInstr + 7 < cbInstr, VERR_DBG_MALFORMED_UNWIND_INFO);
if (pExecState->fLittleEndian)
uAddress = RT_MAKE_U64_FROM_U8(pbInstr[offInstr + 0], pbInstr[offInstr + 1],
pbInstr[offInstr + 2], pbInstr[offInstr + 3],
pbInstr[offInstr + 4], pbInstr[offInstr + 5],
pbInstr[offInstr + 6], pbInstr[offInstr + 7]);
else
uAddress = RT_MAKE_U64_FROM_U8(pbInstr[offInstr + 7], pbInstr[offInstr + 6],
pbInstr[offInstr + 5], pbInstr[offInstr + 4],
pbInstr[offInstr + 3], pbInstr[offInstr + 2],
pbInstr[offInstr + 1], pbInstr[offInstr + 0]);
offInstr += 8;
break;
case DW_EH_PE_sleb128:
case DW_EH_PE_uleb128:
default:
AssertMsgFailedReturn(("%#x\n", pExecState->pCie->bAddressPtrEnc), VERR_DWARF_TODO);
}
AssertReturn(uAddress >= pExecState->uPcBegin, VERR_DBG_MALFORMED_UNWIND_INFO);
/* Did we advance past the desire address already? */
if (uAddress > pExecState->uPcBegin + pExecState->offInRange)
return VINF_SUCCESS;
pExecState->cbLeftToAdvance = pExecState->uPcBegin + pExecState->offInRange - uAddress;
break;
/*
* Row state push/pop instructions.
*/
case DW_CFA_remember_state:
{
AssertReturn(pExecState->cPushes < 10, VERR_DBG_MALFORMED_UNWIND_INFO);
PRTDWARFCFROW pNewRow = (PRTDWARFCFROW)RTMemTmpAlloc(sizeof(*pNewRow));
AssertReturn(pNewRow, VERR_NO_TMP_MEMORY);
memcpy(pNewRow, pRow, sizeof(*pNewRow));
pNewRow->pNextOnStack = pRow;
pNewRow->fOnHeap = true;
pExecState->pRow = pNewRow;
pExecState->cPushes += 1;
pRow = pNewRow;
break;
}
case DW_CFA_restore_state:
AssertReturn(pRow->pNextOnStack, VERR_DBG_MALFORMED_UNWIND_INFO);
Assert(pRow->fOnHeap);
Assert(pExecState->cPushes > 0);
pExecState->cPushes -= 1;
pExecState->pRow = pRow->pNextOnStack;
RTMemTmpFree(pRow);
pRow = pExecState->pRow;
break;
}
}
break;
case DW_CFA_restore:
{
uint8_t const * const pbCurInstr = &pbInstr[offInstr - 1];
uint8_t const iReg = bInstr & ~DW_CFA_high_bit_mask;
if (iReg < RT_ELEMENTS(pRow->apbRegInstrs))
pRow->apbRegInstrs[iReg] = pbCurInstr;
break;
}
}
}
return VINF_TRY_AGAIN;
}
/**
* Register getter for AMD64.
*
* @returns true if found, false if not.
* @param pState The unwind state to get the register from.
* @param iReg The dwarf register number.
* @param puValue Where to store the register value.
*/
static bool rtDwarfUnwind_Amd64GetRegFromState(PCRTDBGUNWINDSTATE pState, uint16_t iReg, uint64_t *puValue)
{
switch (iReg)
{
case DWREG_AMD64_RAX: *puValue = pState->u.x86.auRegs[X86_GREG_xAX]; return true;
case DWREG_AMD64_RDX: *puValue = pState->u.x86.auRegs[X86_GREG_xDX]; return true;
case DWREG_AMD64_RCX: *puValue = pState->u.x86.auRegs[X86_GREG_xCX]; return true;
case DWREG_AMD64_RBX: *puValue = pState->u.x86.auRegs[X86_GREG_xBX]; return true;
case DWREG_AMD64_RSI: *puValue = pState->u.x86.auRegs[X86_GREG_xSI]; return true;
case DWREG_AMD64_RDI: *puValue = pState->u.x86.auRegs[X86_GREG_xDI]; return true;
case DWREG_AMD64_RBP: *puValue = pState->u.x86.auRegs[X86_GREG_xBP]; return true;
case DWREG_AMD64_RSP: *puValue = pState->u.x86.auRegs[X86_GREG_xSP]; return true;
case DWREG_AMD64_R8: *puValue = pState->u.x86.auRegs[X86_GREG_x8]; return true;
case DWREG_AMD64_R9: *puValue = pState->u.x86.auRegs[X86_GREG_x9]; return true;
case DWREG_AMD64_R10: *puValue = pState->u.x86.auRegs[X86_GREG_x10]; return true;
case DWREG_AMD64_R11: *puValue = pState->u.x86.auRegs[X86_GREG_x11]; return true;
case DWREG_AMD64_R12: *puValue = pState->u.x86.auRegs[X86_GREG_x12]; return true;
case DWREG_AMD64_R13: *puValue = pState->u.x86.auRegs[X86_GREG_x13]; return true;
case DWREG_AMD64_R14: *puValue = pState->u.x86.auRegs[X86_GREG_x14]; return true;
case DWREG_AMD64_R15: *puValue = pState->u.x86.auRegs[X86_GREG_x15]; return true;
case DWREG_AMD64_RFLAGS: *puValue = pState->u.x86.uRFlags; return true;
case DWREG_AMD64_ES: *puValue = pState->u.x86.auSegs[X86_SREG_ES]; return true;
case DWREG_AMD64_CS: *puValue = pState->u.x86.auSegs[X86_SREG_CS]; return true;
case DWREG_AMD64_SS: *puValue = pState->u.x86.auSegs[X86_SREG_SS]; return true;
case DWREG_AMD64_DS: *puValue = pState->u.x86.auSegs[X86_SREG_DS]; return true;
case DWREG_AMD64_FS: *puValue = pState->u.x86.auSegs[X86_SREG_FS]; return true;
case DWREG_AMD64_GS: *puValue = pState->u.x86.auSegs[X86_SREG_GS]; return true;
}
return false;
}
/**
* Register getter for 386+.
*
* @returns true if found, false if not.
* @param pState The unwind state to get the register from.
* @param iReg The dwarf register number.
* @param puValue Where to store the register value.
*/
static bool rtDwarfUnwind_X86GetRegFromState(PCRTDBGUNWINDSTATE pState, uint16_t iReg, uint64_t *puValue)
{
switch (iReg)
{
case DWREG_X86_EAX: *puValue = pState->u.x86.auRegs[X86_GREG_xAX]; return true;
case DWREG_X86_ECX: *puValue = pState->u.x86.auRegs[X86_GREG_xCX]; return true;
case DWREG_X86_EDX: *puValue = pState->u.x86.auRegs[X86_GREG_xDX]; return true;
case DWREG_X86_EBX: *puValue = pState->u.x86.auRegs[X86_GREG_xBX]; return true;
case DWREG_X86_ESP: *puValue = pState->u.x86.auRegs[X86_GREG_xSP]; return true;
case DWREG_X86_EBP: *puValue = pState->u.x86.auRegs[X86_GREG_xBP]; return true;
case DWREG_X86_ESI: *puValue = pState->u.x86.auRegs[X86_GREG_xSI]; return true;
case DWREG_X86_EDI: *puValue = pState->u.x86.auRegs[X86_GREG_xDI]; return true;
case DWREG_X86_EFLAGS: *puValue = pState->u.x86.uRFlags; return true;
case DWREG_X86_ES: *puValue = pState->u.x86.auSegs[X86_SREG_ES]; return true;
case DWREG_X86_CS: *puValue = pState->u.x86.auSegs[X86_SREG_CS]; return true;
case DWREG_X86_SS: *puValue = pState->u.x86.auSegs[X86_SREG_SS]; return true;
case DWREG_X86_DS: *puValue = pState->u.x86.auSegs[X86_SREG_DS]; return true;
case DWREG_X86_FS: *puValue = pState->u.x86.auSegs[X86_SREG_FS]; return true;
case DWREG_X86_GS: *puValue = pState->u.x86.auSegs[X86_SREG_GS]; return true;
}
return false;
}
/** Register getter. */
typedef bool FNDWARFUNWINDGEREGFROMSTATE(PCRTDBGUNWINDSTATE pState, uint16_t iReg, uint64_t *puValue);
/** Pointer to a register getter. */
typedef FNDWARFUNWINDGEREGFROMSTATE *PFNDWARFUNWINDGEREGFROMSTATE;
/**
* Does the heavy work for figuring out the return value of a register.
*
* @returns IPRT status code.
* @retval VERR_NOT_FOUND if register is undefined.
*
* @param pRow The DWARF unwind table "row" to use.
* @param uReg The DWARF register number.
* @param pCie The corresponding CIE.
* @param uCfa The canonical frame address to use.
* @param pState The unwind to use when reading stack.
* @param pOldState The unwind state to get register values from.
* @param pfnGetReg The register value getter.
* @param puValue Where to store the return value.
* @param cbValue The size this register would have on the stack.
*/
static int rtDwarfUnwind_CalcRegisterValue(PRTDWARFCFROW pRow, unsigned uReg, PCRTDWARFCIEINFO pCie, uint64_t uCfa,
PRTDBGUNWINDSTATE pState, PCRTDBGUNWINDSTATE pOldState,
PFNDWARFUNWINDGEREGFROMSTATE pfnGetReg, uint64_t *puValue, uint8_t cbValue)
{
Assert(uReg < RT_ELEMENTS(pRow->apbRegInstrs));
uint8_t const *pbInstr = pRow->apbRegInstrs[uReg];
if (!pbInstr)
return VERR_NOT_FOUND;
uint32_t cbInstr = UINT32_MAX / 2;
uint32_t offInstr = 1;
uint8_t const bInstr = *pbInstr;
switch (bInstr)
{
default:
if ((bInstr & DW_CFA_high_bit_mask) == DW_CFA_offset)
{
uint64_t offCfa;
GET_ULEB128_AS_U63(offCfa);
int rc = pState->pfnReadStack(pState, uCfa + (int64_t)offCfa * pCie->iDataAlignFactor, cbValue, puValue);
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_offset %#RX64: %Rrc, %#RX64\n", uReg, uCfa + (int64_t)offCfa * pCie->iDataAlignFactor, rc, *puValue));
return rc;
}
AssertReturn((bInstr & DW_CFA_high_bit_mask) == DW_CFA_restore, VERR_INTERNAL_ERROR);
RT_FALL_THRU();
case DW_CFA_restore_extended:
/* Need to search the CIE for the rule. */
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_restore/extended:\n", uReg));
AssertFailedReturn(VERR_DWARF_TODO);
case DW_CFA_offset_extended:
{
SKIP_ULEB128_OR_LEB128();
uint64_t offCfa;
GET_ULEB128_AS_U63(offCfa);
int rc = pState->pfnReadStack(pState, uCfa + (int64_t)offCfa * pCie->iDataAlignFactor, cbValue, puValue);
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_offset_extended %#RX64: %Rrc, %#RX64\n", uReg, uCfa + (int64_t)offCfa * pCie->iDataAlignFactor, rc, *puValue));
return rc;
}
case DW_CFA_offset_extended_sf:
{
SKIP_ULEB128_OR_LEB128();
int64_t offCfa;
GET_LEB128_AS_I63(offCfa);
int rc = pState->pfnReadStack(pState, uCfa + offCfa * pCie->iDataAlignFactor, cbValue, puValue);
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_offset_extended_sf %#RX64: %Rrc, %#RX64\n", uReg, uCfa + offCfa * pCie->iDataAlignFactor, rc, *puValue));
return rc;
}
case DW_CFA_val_offset:
{
SKIP_ULEB128_OR_LEB128();
uint64_t offCfa;
GET_ULEB128_AS_U63(offCfa);
*puValue = uCfa + (int64_t)offCfa * pCie->iDataAlignFactor;
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_val_offset: %#RX64\n", uReg, *puValue));
return VINF_SUCCESS;
}
case DW_CFA_val_offset_sf:
{
SKIP_ULEB128_OR_LEB128();
int64_t offCfa;
GET_LEB128_AS_I63(offCfa);
*puValue = uCfa + offCfa * pCie->iDataAlignFactor;
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_val_offset_sf: %#RX64\n", uReg, *puValue));
return VINF_SUCCESS;
}
case DW_CFA_register:
{
SKIP_ULEB128_OR_LEB128();
uint16_t iSrcReg;
GET_ULEB128_AS_U14(iSrcReg);
if (pfnGetReg(pOldState, uReg, puValue))
{
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_register: %#RX64\n", uReg, *puValue));
return VINF_SUCCESS;
}
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_register: VERR_NOT_FOUND\n", uReg));
return VERR_NOT_FOUND;
}
case DW_CFA_expression:
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_expression: TODO\n", uReg));
AssertFailedReturn(VERR_DWARF_TODO);
case DW_CFA_val_expression:
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_val_expression: TODO\n", uReg));
AssertFailedReturn(VERR_DWARF_TODO);
case DW_CFA_undefined:
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_undefined\n", uReg));
return VERR_NOT_FOUND;
case DW_CFA_same_value:
if (pfnGetReg(pOldState, uReg, puValue))
{
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_same_value: %#RX64\n", uReg, *puValue));
return VINF_SUCCESS;
}
Log8(("rtDwarfUnwind_CalcRegisterValue(%#x): DW_CFA_same_value: VERR_NOT_FOUND\n", uReg));
return VERR_NOT_FOUND;
}
}
DECLINLINE(void) rtDwarfUnwind_UpdateX86GRegFromRow(PRTDBGUNWINDSTATE pState, PCRTDBGUNWINDSTATE pOldState, unsigned idxGReg,
PRTDWARFCFROW pRow, unsigned idxDwReg, PCRTDWARFCIEINFO pCie,
uint64_t uCfa, PFNDWARFUNWINDGEREGFROMSTATE pfnGetReg, uint8_t cbGReg)
{
int rc = rtDwarfUnwind_CalcRegisterValue(pRow, idxDwReg, pCie, uCfa, pState, pOldState, pfnGetReg,
&pState->u.x86.auRegs[idxGReg], cbGReg);
if (RT_SUCCESS(rc))
pState->u.x86.Loaded.s.fRegs |= RT_BIT_32(idxGReg);
}
DECLINLINE(void) rtDwarfUnwind_UpdateX86SRegFromRow(PRTDBGUNWINDSTATE pState, PCRTDBGUNWINDSTATE pOldState, unsigned idxSReg,
PRTDWARFCFROW pRow, unsigned idxDwReg, PCRTDWARFCIEINFO pCie,
uint64_t uCfa, PFNDWARFUNWINDGEREGFROMSTATE pfnGetReg)
{
uint64_t uValue = pState->u.x86.auSegs[idxSReg];
int rc = rtDwarfUnwind_CalcRegisterValue(pRow, idxDwReg, pCie, uCfa, pState, pOldState, pfnGetReg, &uValue, sizeof(uint16_t));
if (RT_SUCCESS(rc))
{
pState->u.x86.auSegs[idxSReg] = (uint16_t)uValue;
pState->u.x86.Loaded.s.fSegs |= RT_BIT_32(idxSReg);
}
}
DECLINLINE(void) rtDwarfUnwind_UpdateX86RFlagsFromRow(PRTDBGUNWINDSTATE pState, PCRTDBGUNWINDSTATE pOldState,
PRTDWARFCFROW pRow, unsigned idxDwReg, PCRTDWARFCIEINFO pCie,
uint64_t uCfa, PFNDWARFUNWINDGEREGFROMSTATE pfnGetReg)
{
int rc = rtDwarfUnwind_CalcRegisterValue(pRow, idxDwReg, pCie, uCfa, pState, pOldState, pfnGetReg,
&pState->u.x86.uRFlags, sizeof(uint32_t));
if (RT_SUCCESS(rc))
pState->u.x86.Loaded.s.fRFlags = 1;
}
DECLINLINE(void) rtDwarfUnwind_UpdatePCFromRow(PRTDBGUNWINDSTATE pState, PCRTDBGUNWINDSTATE pOldState,
PRTDWARFCFROW pRow, unsigned idxDwReg, PCRTDWARFCIEINFO pCie,
uint64_t uCfa, PFNDWARFUNWINDGEREGFROMSTATE pfnGetReg, uint8_t cbPc)
{
if (pCie->bRetReg != UINT8_MAX)
idxDwReg = pCie->bRetReg;
int rc = rtDwarfUnwind_CalcRegisterValue(pRow, idxDwReg, pCie, uCfa, pState, pOldState, pfnGetReg, &pState->uPc, cbPc);
if (RT_SUCCESS(rc))
pState->u.x86.Loaded.s.fPc = 1;
else
{
rc = pState->pfnReadStack(pState, uCfa - cbPc, cbPc, &pState->uPc);
if (RT_SUCCESS(rc))
pState->u.x86.Loaded.s.fPc = 1;
}
}
/**
* Updates @a pState with the rules found in @a pRow.
*
* @returns IPRT status code.
* @param pState The unwind state to update.
* @param pRow The "row" in the dwarf unwind table.
* @param pCie The CIE structure for the row.
* @param enmImageArch The image architecture.
*/
static int rtDwarfUnwind_UpdateStateFromRow(PRTDBGUNWINDSTATE pState, PRTDWARFCFROW pRow,
PCRTDWARFCIEINFO pCie, RTLDRARCH enmImageArch)
{
/*
* We need to make a copy of the current state so we can get at the
* current register values while calculating the ones of the next frame.
*/
RTDBGUNWINDSTATE const Old = *pState;
/*
* Get the register state getter.
*/
PFNDWARFUNWINDGEREGFROMSTATE pfnGetReg;
switch (enmImageArch)
{
case RTLDRARCH_AMD64:
pfnGetReg = rtDwarfUnwind_Amd64GetRegFromState;
break;
case RTLDRARCH_X86_32:
case RTLDRARCH_X86_16:
pfnGetReg = rtDwarfUnwind_X86GetRegFromState;
break;
default:
return VERR_NOT_SUPPORTED;
}
/*
* Calc the canonical frame address for the current row.
*/
AssertReturn(pRow->fCfaDefined, VERR_DBG_MALFORMED_UNWIND_INFO);
uint64_t uCfa = 0;
if (!pRow->pbCfaExprInstr)
{
pfnGetReg(&Old, pRow->uCfaBaseReg, &uCfa);
uCfa += pRow->offCfaReg;
}
else
{
AssertFailed();
return VERR_DWARF_TODO;
}
Log8(("rtDwarfUnwind_UpdateStateFromRow: uCfa=%RX64\n", uCfa));
/*
* Do the architecture specific register updating.
*/
switch (enmImageArch)
{
case RTLDRARCH_AMD64:
pState->enmRetType = RTDBGRETURNTYPE_NEAR64;
pState->u.x86.FrameAddr.off = uCfa - 8*2;
pState->u.x86.Loaded.fAll = 0;
pState->u.x86.Loaded.s.fFrameAddr = 1;
rtDwarfUnwind_UpdatePCFromRow(pState, &Old, pRow, DWREG_AMD64_RA, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86RFlagsFromRow(pState, &Old, pRow, DWREG_AMD64_RFLAGS, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xAX, pRow, DWREG_AMD64_RAX, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xCX, pRow, DWREG_AMD64_RCX, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xDX, pRow, DWREG_AMD64_RDX, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xBX, pRow, DWREG_AMD64_RBX, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xSP, pRow, DWREG_AMD64_RSP, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xBP, pRow, DWREG_AMD64_RBP, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xSI, pRow, DWREG_AMD64_RSI, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xDI, pRow, DWREG_AMD64_RDI, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_x8, pRow, DWREG_AMD64_R8, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_x9, pRow, DWREG_AMD64_R9, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_x10, pRow, DWREG_AMD64_R10, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_x11, pRow, DWREG_AMD64_R11, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_x12, pRow, DWREG_AMD64_R12, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_x13, pRow, DWREG_AMD64_R13, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_x14, pRow, DWREG_AMD64_R14, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_x15, pRow, DWREG_AMD64_R15, pCie, uCfa, pfnGetReg, sizeof(uint64_t));
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_ES, pRow, DWREG_AMD64_ES, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_CS, pRow, DWREG_AMD64_CS, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_SS, pRow, DWREG_AMD64_SS, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_DS, pRow, DWREG_AMD64_DS, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_FS, pRow, DWREG_AMD64_FS, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_GS, pRow, DWREG_AMD64_GS, pCie, uCfa, pfnGetReg);
break;
case RTLDRARCH_X86_32:
case RTLDRARCH_X86_16:
pState->enmRetType = RTDBGRETURNTYPE_NEAR32;
pState->u.x86.FrameAddr.off = uCfa - 4*2;
pState->u.x86.Loaded.fAll = 0;
pState->u.x86.Loaded.s.fFrameAddr = 1;
rtDwarfUnwind_UpdatePCFromRow(pState, &Old, pRow, DWREG_X86_RA, pCie, uCfa, pfnGetReg, sizeof(uint32_t));
rtDwarfUnwind_UpdateX86RFlagsFromRow(pState, &Old, pRow, DWREG_X86_EFLAGS, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xAX, pRow, DWREG_X86_EAX, pCie, uCfa, pfnGetReg, sizeof(uint32_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xCX, pRow, DWREG_X86_ECX, pCie, uCfa, pfnGetReg, sizeof(uint32_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xDX, pRow, DWREG_X86_EDX, pCie, uCfa, pfnGetReg, sizeof(uint32_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xBX, pRow, DWREG_X86_EBX, pCie, uCfa, pfnGetReg, sizeof(uint32_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xSP, pRow, DWREG_X86_ESP, pCie, uCfa, pfnGetReg, sizeof(uint32_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xBP, pRow, DWREG_X86_EBP, pCie, uCfa, pfnGetReg, sizeof(uint32_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xSI, pRow, DWREG_X86_ESI, pCie, uCfa, pfnGetReg, sizeof(uint32_t));
rtDwarfUnwind_UpdateX86GRegFromRow(pState, &Old, X86_GREG_xDI, pRow, DWREG_X86_EDI, pCie, uCfa, pfnGetReg, sizeof(uint32_t));
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_ES, pRow, DWREG_X86_ES, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_CS, pRow, DWREG_X86_CS, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_SS, pRow, DWREG_X86_SS, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_DS, pRow, DWREG_X86_DS, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_FS, pRow, DWREG_X86_FS, pCie, uCfa, pfnGetReg);
rtDwarfUnwind_UpdateX86SRegFromRow(pState, &Old, X86_SREG_GS, pRow, DWREG_X86_GS, pCie, uCfa, pfnGetReg);
if (pState->u.x86.Loaded.s.fRegs & RT_BIT_32(X86_GREG_xSP))
pState->u.x86.FrameAddr.off = pState->u.x86.auRegs[X86_GREG_xSP] - 8;
else
pState->u.x86.FrameAddr.off = uCfa - 8;
pState->u.x86.FrameAddr.sel = pState->u.x86.auSegs[X86_SREG_SS];
if (pState->u.x86.Loaded.s.fSegs & RT_BIT_32(X86_SREG_CS))
{
if ((pState->uPc >> 16) == pState->u.x86.auSegs[X86_SREG_CS])
{
pState->enmRetType = RTDBGRETURNTYPE_FAR16;
pState->uPc &= UINT16_MAX;
Log8(("rtDwarfUnwind_UpdateStateFromRow: Detected FAR16 return to %04x:%04RX64\n", pState->u.x86.auSegs[X86_SREG_CS], pState->uPc));
}
else
{
pState->enmRetType = RTDBGRETURNTYPE_FAR32;
Log8(("rtDwarfUnwind_UpdateStateFromRow: CS loaded, assume far return.\n"));
}
}
break;
default:
AssertFailedReturn(VERR_NOT_SUPPORTED);
}
return VINF_SUCCESS;
}
/**
* Processes a FDE, taking over after the PC range field.
*
* @returns IPRT status code.
* @param pCursor The cursor.
* @param pCie Information about the corresponding CIE.
* @param uPcBegin The PC begin field value (sans segment).
* @param cbPcRange The PC range from @a uPcBegin.
* @param offInRange The offset into the range corresponding to
* pState->uPc.
* @param enmImageArch The image architecture.
* @param pState The unwind state to work.
*/
static int rtDwarfUnwind_ProcessFde(PRTDWARFCURSOR pCursor, PCRTDWARFCIEINFO pCie, uint64_t uPcBegin,
uint64_t cbPcRange, uint64_t offInRange, RTLDRARCH enmImageArch, PRTDBGUNWINDSTATE pState)
{
/*
* Deal with augmented data fields.
*/
/* The size. */
size_t cbInstr = ~(size_t)0;
if (pCie->fHasAugmentationSize)
{
uint64_t cbAugData = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
if (cbAugData > pCursor->cbUnitLeft)
return VERR_DBG_MALFORMED_UNWIND_INFO;
cbInstr = pCursor->cbUnitLeft - cbAugData;
}
else if (pCie->fHasUnknowAugmentation)
return VERR_DBG_MALFORMED_UNWIND_INFO;
/* Parse the string and fetch FDE fields. */
if (!pCie->fHasEhData)
for (const char *pszAug = pCie->pszAugmentation; *pszAug != '\0'; pszAug++)
switch (*pszAug)
{
case 'L':
if (pCie->bLsdaPtrEnc != DW_EH_PE_omit)
rtDwarfCursor_GetPtrEnc(pCursor, pCie->bLsdaPtrEnc, 0);
break;
}
/* Skip unconsumed bytes. */
if ( cbInstr != ~(size_t)0
&& pCursor->cbUnitLeft > cbInstr)
rtDwarfCursor_SkipBytes(pCursor, pCursor->cbUnitLeft - cbInstr);
if (RT_FAILURE(pCursor->rc))
return pCursor->rc;
/*
* Now "execute" the programs till we've constructed the desired row.
*/
RTDWARFCFROW Row;
RTDWARFCFEXEC ExecState = { &Row, offInRange, 0, true /** @todo byte-order*/, pCie, uPcBegin, offInRange };
RT_ZERO(Row);
int rc = rtDwarfUnwind_Execute(&ExecState, pCie->pbInstructions, (uint32_t)pCie->cbInstructions);
if (rc == VINF_TRY_AGAIN)
rc = rtDwarfUnwind_Execute(&ExecState, pCursor->pb, (uint32_t)pCursor->cbUnitLeft);
/* On success, extract whatever state we've got. */
if (RT_SUCCESS(rc))
rc = rtDwarfUnwind_UpdateStateFromRow(pState, &Row, pCie, enmImageArch);
/*
* Clean up allocations in case of pushes.
*/
if (ExecState.pRow == &Row)
Assert(!ExecState.pRow->fOnHeap);
else
do
{
PRTDWARFCFROW pPopped = ExecState.pRow;
ExecState.pRow = ExecState.pRow->pNextOnStack;
Assert(pPopped->fOnHeap);
RTMemTmpFree(pPopped);
} while (ExecState.pRow && ExecState.pRow != &Row);
RT_NOREF(pState, uPcBegin, cbPcRange, offInRange);
return rc;
}
/**
* Load the information we need from a CIE.
*
* This starts after the initial length and CIE_pointer fields has
* been processed.
*
* @returns IPRT status code.
* @param pCursor The cursor.
* @param pNewCie The structure to populate with parsed CIE info.
* @param offUnit The unit offset.
* @param bDefaultPtrEnc The default pointer encoding.
*/
static int rtDwarfUnwind_LoadCie(PRTDWARFCURSOR pCursor, PRTDWARFCIEINFO pNewCie, uint64_t offUnit, uint8_t bDefaultPtrEnc)
{
/*
* Initialize the CIE record and get the version.
*/
RT_ZERO(*pNewCie);
pNewCie->offCie = offUnit;
pNewCie->bLsdaPtrEnc = DW_EH_PE_omit;
pNewCie->bAddressPtrEnc = DW_EH_PE_omit; /* set later */
pNewCie->uDwarfVer = rtDwarfCursor_GetUByte(pCursor, 0);
if ( pNewCie->uDwarfVer >= 1 /* Note! Some GCC versions may emit v1 here. */
&& pNewCie->uDwarfVer <= 5)
{ /* likely */ }
else
{
Log(("rtDwarfUnwind_LoadCie(%RX64): uDwarfVer=%u: VERR_VERSION_MISMATCH\n", offUnit, pNewCie->uDwarfVer));
return VERR_VERSION_MISMATCH;
}
/*
* The augmentation string.
*
* First deal with special "eh" string from oldish GCC (dwarf2out.c about 1997), specified in LSB:
* https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html
*/
pNewCie->pszAugmentation = rtDwarfCursor_GetSZ(pCursor, "");
if ( pNewCie->pszAugmentation[0] == 'e'
&& pNewCie->pszAugmentation[1] == 'h'
&& pNewCie->pszAugmentation[2] == '\0')
{
pNewCie->fHasEhData = true;
rtDwarfCursor_GetPtrEnc(pCursor, bDefaultPtrEnc, 0);
}
else
{
/* Regular augmentation string. */
for (const char *pszAug = pNewCie->pszAugmentation; *pszAug != '\0'; pszAug++)
switch (*pszAug)
{
case 'z':
pNewCie->fHasAugmentationSize = true;
break;
case 'L':
pNewCie->fHasLanguageSpecificDataArea = true;
break;
case 'P':
pNewCie->fHasPersonalityRoutine = true;
break;
case 'R':
pNewCie->fHasAddressEnc = true;
break;
case 'S':
pNewCie->fIsSignalFrame = true;
break;
default:
pNewCie->fHasUnknowAugmentation = true;
break;
}
}
/*
* More standard fields
*/
uint8_t cbAddress = 0;
if (pNewCie->uDwarfVer >= 4)
{
cbAddress = rtDwarfCursor_GetU8(pCursor, bDefaultPtrEnc == DW_EH_PE_udata8 ? 8 : 4);
pNewCie->cbSegment = rtDwarfCursor_GetU8(pCursor, 0);
}
pNewCie->uCodeAlignFactor = rtDwarfCursor_GetULeb128(pCursor, 1);
pNewCie->iDataAlignFactor = rtDwarfCursor_GetSLeb128(pCursor, 1);
pNewCie->bRetReg = rtDwarfCursor_GetU8(pCursor, UINT8_MAX);
/*
* Augmentation data.
*/
if (!pNewCie->fHasEhData)
{
/* The size. */
size_t cbInstr = ~(size_t)0;
if (pNewCie->fHasAugmentationSize)
{
uint64_t cbAugData = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
if (RT_FAILURE(pCursor->rc))
{
Log(("rtDwarfUnwind_LoadCie(%#RX64): rtDwarfCursor_GetULeb128 -> %Rrc!\n", offUnit, pCursor->rc));
return pCursor->rc;
}
if (cbAugData > pCursor->cbUnitLeft)
{
Log(("rtDwarfUnwind_LoadCie(%#RX64): cbAugData=%#x pCursor->cbUnitLeft=%#x -> VERR_DBG_MALFORMED_UNWIND_INFO!\n", offUnit, cbAugData, pCursor->cbUnitLeft));
return VERR_DBG_MALFORMED_UNWIND_INFO;
}
cbInstr = pCursor->cbUnitLeft - cbAugData;
}
else if (pNewCie->fHasUnknowAugmentation)
{
Log(("rtDwarfUnwind_LoadCie(%#RX64): fHasUnknowAugmentation=1 -> VERR_DBG_MALFORMED_UNWIND_INFO!\n", offUnit));
return VERR_DBG_MALFORMED_UNWIND_INFO;
}
/* Parse the string. */
for (const char *pszAug = pNewCie->pszAugmentation; *pszAug != '\0'; pszAug++)
switch (*pszAug)
{
case 'L':
pNewCie->bLsdaPtrEnc = rtDwarfCursor_GetU8(pCursor, DW_EH_PE_omit);
break;
case 'P':
rtDwarfCursor_GetPtrEnc(pCursor, rtDwarfCursor_GetU8(pCursor, DW_EH_PE_omit), 0);
break;
case 'R':
pNewCie->bAddressPtrEnc = rtDwarfCursor_GetU8(pCursor, DW_EH_PE_omit);
break;
}
/* Skip unconsumed bytes. */
if ( cbInstr != ~(size_t)0
&& pCursor->cbUnitLeft > cbInstr)
rtDwarfCursor_SkipBytes(pCursor, pCursor->cbUnitLeft - cbInstr);
}
/*
* Note down where the instructions are.
*/
pNewCie->pbInstructions = pCursor->pb;
pNewCie->cbInstructions = pCursor->cbUnitLeft;
/*
* Determine the target address encoding. Make sure we resolve DW_EH_PE_ptr.
*/
if (pNewCie->bAddressPtrEnc == DW_EH_PE_omit)
switch (cbAddress)
{
case 2: pNewCie->bAddressPtrEnc = DW_EH_PE_udata2; break;
case 4: pNewCie->bAddressPtrEnc = DW_EH_PE_udata4; break;
case 8: pNewCie->bAddressPtrEnc = DW_EH_PE_udata8; break;
default: pNewCie->bAddressPtrEnc = bDefaultPtrEnc; break;
}
else if ((pNewCie->bAddressPtrEnc & DW_EH_PE_FORMAT_MASK) == DW_EH_PE_ptr)
pNewCie->bAddressPtrEnc = bDefaultPtrEnc;
return VINF_SUCCESS;
}
/**
* Does a slow unwind of a '.debug_frame' or '.eh_frame' section.
*
* @returns IPRT status code.
* @param pCursor The cursor.
* @param uRvaCursor The RVA corrsponding to the cursor start location.
* @param idxSeg The segment of the PC location.
* @param offSeg The segment offset of the PC location.
* @param uRva The RVA of the PC location.
* @param pState The unwind state to work.
* @param bDefaultPtrEnc The default pointer encoding.
* @param fIsEhFrame Set if this is a '.eh_frame'. GCC generate these
* with different CIE_pointer values.
* @param enmImageArch The image architecture.
*/
DECLHIDDEN(int) rtDwarfUnwind_Slow(PRTDWARFCURSOR pCursor, RTUINTPTR uRvaCursor,
RTDBGSEGIDX idxSeg, RTUINTPTR offSeg, RTUINTPTR uRva,
PRTDBGUNWINDSTATE pState, uint8_t bDefaultPtrEnc, bool fIsEhFrame, RTLDRARCH enmImageArch)
{
Log8(("rtDwarfUnwind_Slow: idxSeg=%#x offSeg=%RTptr uRva=%RTptr enmArch=%d PC=%#RX64\n", idxSeg, offSeg, uRva, pState->enmArch, pState->uPc));
/*
* CIE info we collect.
*/
PRTDWARFCIEINFO paCies = NULL;
uint32_t cCies = 0;
PRTDWARFCIEINFO pCieHint = NULL;
/*
* Do the scanning.
*/
uint64_t const offCieOffset = pCursor->f64bitDwarf ? UINT64_MAX : UINT32_MAX;
int rc = VERR_DBG_UNWIND_INFO_NOT_FOUND;
while (!rtDwarfCursor_IsAtEnd(pCursor))
{
uint64_t const offUnit = rtDwarfCursor_CalcSectOffsetU32(pCursor);
if (rtDwarfCursor_GetInitialLength(pCursor) == 0)
break;
uint64_t const offRelCie = rtDwarfCursor_GetUOff(pCursor, offCieOffset);
if (offRelCie != offCieOffset)
{
/*
* Frame descriptor entry (FDE).
*/
/* Locate the corresponding CIE. The CIE pointer is self relative
in .eh_frame and section relative in .debug_frame. */
PRTDWARFCIEINFO pCieForFde;
uint64_t offCie = fIsEhFrame ? offUnit + 4 - offRelCie : offRelCie;
if (pCieHint && pCieHint->offCie == offCie)
pCieForFde = pCieHint;
else
{
pCieForFde = NULL;
uint32_t i = cCies;
while (i-- > 0)
if (paCies[i].offCie == offCie)
{
pCieHint = pCieForFde = &paCies[i];
break;
}
}
if (pCieForFde)
{
/* Read the PC range covered by this FDE (the fields are also known as initial_location). */
RTDBGSEGIDX idxFdeSeg = RTDBGSEGIDX_RVA;
if (pCieForFde->cbSegment)
idxFdeSeg = rtDwarfCursor_GetVarSizedU(pCursor, pCieForFde->cbSegment, RTDBGSEGIDX_RVA);
uint64_t uPcBegin;
switch (pCieForFde->bAddressPtrEnc & DW_EH_PE_APPL_MASK)
{
default: AssertFailed();
RT_FALL_THRU();
case DW_EH_PE_absptr:
uPcBegin = rtDwarfCursor_GetPtrEnc(pCursor, pCieForFde->bAddressPtrEnc, 0);
break;
case DW_EH_PE_pcrel:
{
uPcBegin = rtDwarfCursor_CalcSectOffsetU32(pCursor) + uRvaCursor;
uPcBegin += rtDwarfCursor_GetPtrEnc(pCursor, pCieForFde->bAddressPtrEnc, 0);
break;
}
}
uint64_t cbPcRange = rtDwarfCursor_GetPtrEnc(pCursor, pCieForFde->bAddressPtrEnc, 0);
/* Match it with what we're looking for. */
bool fMatch = idxFdeSeg == RTDBGSEGIDX_RVA
? uRva - uPcBegin < cbPcRange
: idxSeg == idxFdeSeg && offSeg - uPcBegin < cbPcRange;
Log8(("%#08RX64: FDE pCie=%p idxFdeSeg=%#x uPcBegin=%#RX64 cbPcRange=%#x fMatch=%d\n",
offUnit, pCieForFde, idxFdeSeg, uPcBegin, cbPcRange, fMatch));
if (fMatch)
{
rc = rtDwarfUnwind_ProcessFde(pCursor, pCieForFde, uPcBegin, cbPcRange,
idxFdeSeg == RTDBGSEGIDX_RVA ? uRva - uPcBegin : offSeg - uPcBegin,
enmImageArch, pState);
break;
}
}
else
Log8(("%#08RX64: FDE - pCie=NULL!! offCie=%#RX64 offRelCie=%#RX64 fIsEhFrame=%d\n", offUnit, offCie, offRelCie, fIsEhFrame));
}
else
{
/*
* Common information entry (CIE). Record the info we need about it.
*/
if ((cCies & 8) == 0)
{
void *pvNew = RTMemRealloc(paCies, sizeof(paCies[0]) * (cCies + 8));
if (pvNew)
paCies = (PRTDWARFCIEINFO)pvNew;
else
{
rc = VERR_NO_MEMORY;
break;
}
}
Log8(("%#08RX64: CIE\n", offUnit));
int rc2 = rtDwarfUnwind_LoadCie(pCursor, &paCies[cCies], offUnit, bDefaultPtrEnc);
if (RT_SUCCESS(rc2))
{
Log8(("%#08RX64: CIE #%u: offCie=%#RX64\n", offUnit, cCies, paCies[cCies].offCie));
cCies++;
}
}
rtDwarfCursor_SkipUnit(pCursor);
}
/*
* Cleanup.
*/
if (paCies)
RTMemFree(paCies);
Log8(("rtDwarfUnwind_Slow: returns %Rrc PC=%#RX64\n", rc, pState->uPc));
return rc;
}
/**
* Helper for translating a loader architecture value to a pointe encoding.
*
* @returns Pointer encoding.
* @param enmLdrArch The loader architecture value to convert.
*/
static uint8_t rtDwarfUnwind_ArchToPtrEnc(RTLDRARCH enmLdrArch)
{
switch (enmLdrArch)
{
case RTLDRARCH_AMD64:
case RTLDRARCH_ARM64:
return DW_EH_PE_udata8;
case RTLDRARCH_X86_16:
case RTLDRARCH_X86_32:
case RTLDRARCH_ARM32:
return DW_EH_PE_udata4;
case RTLDRARCH_HOST:
case RTLDRARCH_WHATEVER:
case RTLDRARCH_INVALID:
case RTLDRARCH_END:
case RTLDRARCH_32BIT_HACK:
break;
}
AssertFailed();
return DW_EH_PE_udata4;
}
/**
* Interface for the loader code.
*
* @returns IPRT status.
* @param pvSection The '.eh_frame' section data.
* @param cbSection The size of the '.eh_frame' section data.
* @param uRvaSection The RVA of the '.eh_frame' section.
* @param idxSeg The segment of the PC location.
* @param offSeg The segment offset of the PC location.
* @param uRva The RVA of the PC location.
* @param pState The unwind state to work.
* @param enmArch The image architecture.
*/
DECLHIDDEN(int) rtDwarfUnwind_EhData(void const *pvSection, size_t cbSection, RTUINTPTR uRvaSection,
RTDBGSEGIDX idxSeg, RTUINTPTR offSeg, RTUINTPTR uRva,
PRTDBGUNWINDSTATE pState, RTLDRARCH enmArch)
{
RTDWARFCURSOR Cursor;
rtDwarfCursor_InitForMem(&Cursor, pvSection, cbSection);
int rc = rtDwarfUnwind_Slow(&Cursor, uRvaSection, idxSeg, offSeg, uRva, pState,
rtDwarfUnwind_ArchToPtrEnc(enmArch), true /*fIsEhFrame*/, enmArch);
LogFlow(("rtDwarfUnwind_EhData: rtDwarfUnwind_Slow -> %Rrc\n", rc));
rc = rtDwarfCursor_Delete(&Cursor, rc);
LogFlow(("rtDwarfUnwind_EhData: returns %Rrc\n", rc));
return rc;
}
/*
*
* 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));
/*
* LLVM emits debug info for global constructors (_GLOBAL__I_a) which are not part of source
* code but are inserted by the compiler: The resulting line number will be 0
* because they are not part of the source file obviously (see https://reviews.llvm.org/rL205999),
* so skip adding them when they are encountered.
*/
if (pLnState->Regs.uLine)
rc = rtDwarfLine_AddLine(pLnState, offOpCode);
}
else
{
switch (bOpCode)
{
/*
* Standard opcode.
*/
case DW_LNS_copy:
Log2(("%08x: DW_LNS_copy\n", offOpCode));
/* See the comment about LLVM above. */
if (pLnState->Regs.uLine)
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:
{
uint8_t u8Adv = (255 - pLnState->Hdr.u8OpcodeBase) / pLnState->Hdr.u8LineRange;
if (pLnState->Hdr.cMaxOpsPerInstr <= 1)
pLnState->Regs.uAddress += (uint32_t)pLnState->Hdr.cbMinInstr * u8Adv;
else
{
pLnState->Regs.uAddress += (pLnState->Regs.idxOp + u8Adv) / pLnState->Hdr.cMaxOpsPerInstr
* pLnState->Hdr.cbMinInstr;
pLnState->Regs.idxOp = (pLnState->Regs.idxOp + u8Adv) % 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_GetInitialLength(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. */
#ifdef LOG_ENABLED
uint32_t const offStart = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
#endif
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);
#ifdef LOG_ENABLED
pEntry->cbHdr = (uint8_t)(pEntry->offSpec - offStart);
Log7(("rtDwarfAbbrev_LookupMiss(%#x): fill: %#x: uTag=%#x offAbbrev=%#x%s\n",
uCode, offStart, pEntry->uTag, pEntry->offAbbrev, pEntry->fChildren ? " has-children" : ""));
#endif
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;
uPrevCode = uCurCode;
}
if (pRet)
Log6(("rtDwarfAbbrev_LookupMiss(%#x): uTag=%#x offSpec=%#x offAbbrev=%#x [fill]\n",
uCode, pRet->uTag, pRet->offSpec, pRet->offAbbrev));
else
Log6(("rtDwarfAbbrev_LookupMiss(%#x): failed [fill]\n", uCode));
}
else
{
/*
* Search for the entry with the desired code, no cache filling.
*/
for (;;)
{
/* Read the 'header'. */
#ifdef LOG_ENABLED
uint32_t const offStart = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
#endif
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;
#ifdef LOG_ENABLED
pRet->cbHdr = (uint8_t)(pRet->offSpec - offStart);
#endif
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;
}
if (pRet)
Log6(("rtDwarfAbbrev_LookupMiss(%#x): uTag=%#x offSpec=%#x offAbbrev=%#x [no-fill]\n",
uCode, pRet->uTag, pRet->offSpec, pRet->offAbbrev));
else
Log6(("rtDwarfAbbrev_LookupMiss(%#x): failed [no-fill]\n", uCode));
}
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)
{
uCode -= 1;
if (uCode < pThis->cCachedAbbrevsAlloced)
{
if (pThis->paCachedAbbrevs[uCode].offAbbrev == pThis->offCachedAbbrev)
return &pThis->paCachedAbbrevs[uCode];
}
return rtDwarfAbbrev_LookupMiss(pThis, 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)
{
RT_NOREF_PV(pvLater); RT_NOREF_PV(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);
int rc;
if ( uForm == DW_FORM_block
|| uForm == DW_FORM_block1
|| uForm == DW_FORM_block2
|| uForm == DW_FORM_block4)
{
RTDWARFLOCST LocSt;
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;
}
}
}
else
rc = rtDwarfDecode_UnsignedInt(pDie, pbMember, pDesc, uForm, pCursor);
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*/);
AssertMsg(RT_SUCCESS(rc) || rc == VERR_DBG_ADDRESS_CONFLICT,
("%Rrc %s %x:%x\n", rc, pLabel->pszName, iSeg, offSeg));
}
else
Log5(("rtDbgModDwarfLinkAddressToSegOffset failed: %Rrc\n", rc));
/* Ignore errors regarding local labels. */
if (RT_FAILURE(rc) && !pLabel->fExternal)
rc = -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 pParentDie 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:
Log(("rtDwarfInfo_SkipForm: Unknown form %#x\n", uForm));
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 (;;)
{
#ifdef LOG_ENABLED
uint32_t const off = (uint32_t)(AbbrevCursor.pb - AbbrevCursor.pbStart);
#endif
uint32_t uAttr = rtDwarfCursor_GetULeb128AsU32(&AbbrevCursor, 0);
uint32_t uForm = rtDwarfCursor_GetULeb128AsU32(&AbbrevCursor, 0);
Log4((" %04x: %-23s [%s]\n", off, rtDwarfLog_AttrName(uAttr), rtDwarfLog_FormName(uForm)));
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);
}
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 == VERR_DBG_ADDRESS_CONFLICT)
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_GetInitialLength(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 @ %#x)%s\n", offLog, cDepth * 2, "", pszName,
pAbbrev->uTag, uAbbrCode, pAbbrev->offSpec - pAbbrev->cbHdr, 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;
}
/*
*
* Public and image level symbol handling.
* Public and image level symbol handling.
* Public and image level symbol handling.
* Public and image level symbol handling.
*
*
*/
#define RTDBGDWARF_SYM_ENUM_BASE_ADDRESS UINT32_C(0x200000)
/** @callback_method_impl{FNRTLDRENUMSYMS,
* Adds missing symbols from the image symbol table.} */
static DECLCALLBACK(int) rtDwarfSyms_EnumSymbolsCallback(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol,
RTLDRADDR Value, void *pvUser)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
RT_NOREF_PV(hLdrMod); RT_NOREF_PV(uSymbol);
Assert(pThis->iWatcomPass != 1);
RTLDRADDR uRva = Value - RTDBGDWARF_SYM_ENUM_BASE_ADDRESS;
if ( Value >= RTDBGDWARF_SYM_ENUM_BASE_ADDRESS
&& uRva < _1G)
{
RTDBGSYMBOL SymInfo;
RTINTPTR offDisp;
int rc = RTDbgModSymbolByAddr(pThis->hCnt, RTDBGSEGIDX_RVA, uRva, RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &offDisp, &SymInfo);
if ( RT_FAILURE(rc)
|| offDisp != 0)
{
rc = RTDbgModSymbolAdd(pThis->hCnt, pszSymbol, RTDBGSEGIDX_RVA, uRva, 1, 0 /*fFlags*/, NULL /*piOrdinal*/);
Log(("Dwarf: Symbol #%05u %#018RTptr %s [%Rrc]\n", uSymbol, Value, pszSymbol, rc)); NOREF(rc);
}
}
else
Log(("Dwarf: Symbol #%05u %#018RTptr '%s' [SKIPPED - INVALID ADDRESS]\n", uSymbol, Value, pszSymbol));
return VINF_SUCCESS;
}
/**
* Loads additional symbols from the pubnames section and the executable image.
*
* The symbols are insered into the debug info container.
*
* @returns IPRT status code
* @param pThis The DWARF instance.
*/
static int rtDwarfSyms_LoadAll(PRTDBGMODDWARF pThis)
{
/*
* pubnames.
*/
int rc = VINF_SUCCESS;
if (pThis->aSections[krtDbgModDwarfSect_pubnames].fPresent)
{
// 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;
}
/*
* The executable image.
*/
if ( pThis->pImgMod
&& pThis->pImgMod->pImgVt->pfnEnumSymbols
&& pThis->iWatcomPass != 1
&& RT_SUCCESS(rc))
{
rc = pThis->pImgMod->pImgVt->pfnEnumSymbols(pThis->pImgMod,
RTLDR_ENUM_SYMBOL_FLAGS_ALL | RTLDR_ENUM_SYMBOL_FLAGS_NO_FWD,
RTDBGDWARF_SYM_ENUM_BASE_ADDRESS,
rtDwarfSyms_EnumSymbolsCallback,
pThis);
}
return rc;
}
/*
*
* DWARF Debug module implementation.
* DWARF Debug module implementation.
* DWARF Debug module implementation.
*
*/
/** @interface_method_impl{RTDBGMODVTDBG,pfnUnwindFrame} */
static DECLCALLBACK(int) rtDbgModDwarf_UnwindFrame(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTDBGUNWINDSTATE pState)
{
PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
/*
* Unwinding info is stored in the '.debug_frame' section, or altertively
* in the '.eh_frame' one in the image. In the latter case the dbgmodldr.cpp
* part of the operation will take care of it. Since the sections contain the
* same data, we just create a cursor and call a common function to do the job.
*/
if (pThis->aSections[krtDbgModDwarfSect_frame].fPresent)
{
RTDWARFCURSOR Cursor;
int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_frame);
if (RT_SUCCESS(rc))
{
/* Figure default pointer encoding from image arch. */
uint8_t bPtrEnc = rtDwarfUnwind_ArchToPtrEnc(pMod->pImgVt->pfnGetArch(pMod));
/* Make sure we've got both seg:off and rva for the input address. */
RTUINTPTR uRva = off;
if (iSeg == RTDBGSEGIDX_RVA)
rtDbgModDwarfRvaToSegOffset(pThis, uRva, &iSeg, &off);
else
rtDbgModDwarfSegOffsetToRva(pThis, iSeg, off, &uRva);
/* Do the work */
rc = rtDwarfUnwind_Slow(&Cursor, 0 /** @todo .debug_frame RVA*/, iSeg, off, uRva,
pState, bPtrEnc, false /*fIsEhFrame*/, pMod->pImgVt->pfnGetArch(pMod));
rc = rtDwarfCursor_Delete(&Cursor, rc);
}
return rc;
}
return VERR_DBG_NO_UNWIND_INFO;
}
/** @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]); RT_NOREF_PV(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)
{
RT_NOREF_PV(hLdrMod);
/*
* 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! */
/* mach_kernel: */
if ( (pMod->pszDbgFile && strstr(pMod->pszDbgFile, "mach_kernel"))
|| (pMod->pszImgFile && strstr(pMod->pszImgFile, "mach_kernel"))
|| (pMod->pszImgFileSpecified && strstr(pMod->pszImgFileSpecified, "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 = rtDwarfSyms_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 = rtDwarfSyms_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,
/*.pfnUnwindFrame = */ rtDbgModDwarf_UnwindFrame,
/*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
};
|