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
|
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Mono.Debugger.Languages;
using Mono.Debugger.Languages.Native;
using Mono.Debugger.Languages.Mono;
namespace Mono.Debugger.Backend
{
internal class DwarfException : Exception
{
public DwarfException (Bfd bfd, string message, params object[] args)
: base (String.Format ("{0}: {1}", bfd.FileName,
String.Format (message, args)))
{ }
public DwarfException (Bfd bfd, string message, Exception inner)
: base (String.Format ("{0}: {1}", bfd.FileName, message), inner)
{ }
}
internal class DwarfBinaryReader : TargetBinaryReader
{
Bfd bfd;
bool is64bit;
public DwarfBinaryReader (Bfd bfd, TargetBlob blob, bool is64bit)
: base (blob)
{
this.bfd = bfd;
this.is64bit = is64bit;
}
public Bfd Bfd {
get {
return bfd;
}
}
public long PeekOffset (long pos)
{
if (is64bit)
return PeekInt64 (pos);
else
return PeekInt32 (pos);
}
public long PeekOffset (long pos, out int size)
{
if (is64bit) {
size = 8;
return PeekInt64 (pos);
} else {
size = 4;
return PeekInt32 (pos);
}
}
public long ReadOffset ()
{
if (is64bit)
return ReadInt64 ();
else
return ReadInt32 ();
}
public long ReadInitialLength ()
{
bool is64bit;
return ReadInitialLength (out is64bit);
}
public long ReadInitialLength (out bool is64bit)
{
long length = ReadInt32 ();
if (length < 0xfffffff0) {
is64bit = false;
return length;
} else if (length == 0xffffffff) {
is64bit = true;
return ReadInt64 ();
} else
throw new DwarfException (
bfd, "Unknown initial length field {0:x}",
length);
}
}
internal class DwarfReader : DebuggerMarshalByRefObject
{
protected Bfd bfd;
protected Module module;
protected string filename;
bool is64bit;
byte address_size;
ObjectCache debug_loc_reader;
ObjectCache debug_info_reader;
ObjectCache debug_abbrev_reader;
ObjectCache debug_line_reader;
ObjectCache debug_aranges_reader;
ObjectCache debug_pubnames_reader;
ObjectCache debug_pubtypes_reader;
ObjectCache debug_str_reader;
Hashtable source_file_hash;
Hashtable method_source_hash;
Hashtable method_hash;
Hashtable compile_unit_hash;
DwarfSymbolTable symtab;
ArrayList aranges;
Hashtable pubnames;
Hashtable pubtypes;
TargetMemoryInfo target_info;
public DwarfReader (Bfd bfd, Module module)
{
this.bfd = bfd;
this.module = module;
this.filename = bfd.FileName;
this.target_info = bfd.TargetMemoryInfo;
debug_info_reader = create_reader (".debug_info");
DwarfBinaryReader reader = DebugInfoReader;
reader.ReadInitialLength (out is64bit);
int version = reader.ReadInt16 ();
if (version < 2)
throw new DwarfException (
bfd, "Wrong DWARF version: {0}", version);
reader.ReadOffset ();
address_size = reader.ReadByte ();
if ((address_size != 4) && (address_size != 8))
throw new DwarfException (
bfd, "Unknown address size: {0}", address_size);
debug_abbrev_reader = create_reader (".debug_abbrev");
debug_line_reader = create_reader (".debug_line");
debug_aranges_reader = create_reader (".debug_aranges");
debug_pubnames_reader = create_reader (".debug_pubnames");
debug_pubtypes_reader = create_reader (".debug_pubtypes");
debug_str_reader = create_reader (".debug_str");
debug_loc_reader = create_reader (".debug_loc");
compile_unit_hash = Hashtable.Synchronized (new Hashtable ());
method_source_hash = Hashtable.Synchronized (new Hashtable ());
method_hash = Hashtable.Synchronized (new Hashtable ());
source_file_hash = Hashtable.Synchronized (new Hashtable ());
if (bfd.IsLoaded) {
aranges = ArrayList.Synchronized (read_aranges ());
symtab = new DwarfSymbolTable (this, aranges);
pubnames = read_pubnames ();
pubtypes = read_pubtypes ();
}
long offset = 0;
while (offset < reader.Size) {
CompileUnitBlock block = new CompileUnitBlock (this, offset);
compile_unit_hash.Add (offset, block);
offset += block.length;
}
}
public void ModuleLoaded ()
{
if (aranges != null)
return;
aranges = ArrayList.Synchronized (read_aranges ());
symtab = new DwarfSymbolTable (this, aranges);
pubnames = read_pubnames ();
pubtypes = read_pubtypes ();
}
public static bool IsSupported (Bfd bfd)
{
if ((bfd.Target == "elf32-i386") || (bfd.Target == "elf64-x86-64"))
return bfd.HasSection (".debug_info");
else
return false;
}
public TargetMemoryInfo TargetMemoryInfo {
get {
return target_info;
}
}
protected TargetAddress GetAddress (long address)
{
if (!bfd.IsLoaded)
throw new InvalidOperationException (
"Trying to get an address from not-loaded " +
"symbol file `" + bfd.FileName + "'");
return bfd.GetAddress (address);
}
protected ISymbolTable get_symtab_at_offset (long offset)
{
CompileUnitBlock block = (CompileUnitBlock) compile_unit_hash [offset];
// This either return the already-read symbol table or acquire the
// thread lock and read it.
return block.SymbolTable;
}
public SourceFile[] Sources {
get {
SourceFile[] retval = new SourceFile [source_file_hash.Count];
source_file_hash.Values.CopyTo (retval, 0);
return retval;
}
}
public Method GetMethod (long handle)
{
DwarfTargetMethod method = (DwarfTargetMethod) method_hash [handle];
if ((method == null) || !method.CheckLoaded ())
return null;
return method;
}
public MethodSource[] GetMethods (SourceFile file)
{
ArrayList list = new ArrayList ();
foreach (CompileUnitBlock block in compile_unit_hash.Values) {
foreach (CompilationUnit comp_unit in block.CompilationUnits) {
if (comp_unit.DieCompileUnit.SourceFile != file)
continue;
foreach (Die child in comp_unit.DieCompileUnit.Subprograms) {
DieSubprogram subprog = child as DieSubprogram;
if ((subprog == null) || (subprog.MethodSource == null))
continue;
list.Add (subprog.MethodSource);
}
}
}
MethodSource[] methods = new MethodSource [list.Count];
list.CopyTo (methods, 0);
return methods;
}
public MethodSource FindMethod (string name)
{
if (pubnames == null)
return null;
NameEntry entry = (NameEntry) pubnames [name];
if (entry == null)
return null;
MethodSource source;
source = (MethodSource) method_source_hash [entry.AbsoluteOffset];
if (source != null)
return source;
CompileUnitBlock block = (CompileUnitBlock) compile_unit_hash [entry.FileOffset];
return block.GetMethod (entry.AbsoluteOffset);
}
protected DwarfMethodSource GetMethodSource (DieSubprogram subprog,
int start_row, int end_row)
{
DwarfMethodSource source;
source = (DwarfMethodSource) method_source_hash [subprog.Offset];
if (source != null)
return source;
source = new DwarfMethodSource (subprog, start_row, end_row);
method_source_hash.Add (subprog.Offset, source);
return source;
}
protected SourceFile GetSourceFile (string filename)
{
SourceFile file = (SourceFile) source_file_hash [filename];
if (file == null) {
file = new SourceFile (module, filename);
source_file_hash.Add (filename, file);
}
return file;
}
protected void AddType (DieType type)
{
bfd.BfdContainer.AddType (type);
}
bool types_initialized;
public void ReadTypes ()
{
if (types_initialized)
return;
foreach (CompileUnitBlock block in compile_unit_hash.Values)
block.ReadSymbolTable ();
types_initialized = true;
}
protected class CompileUnitBlock
{
public readonly DwarfReader dwarf;
public readonly long offset;
public readonly long length;
SymbolTableCollection symtabs;
ArrayList compile_units;
bool initialized;
bool symbols_initialized;
public Method Lookup (TargetAddress address)
{
build_symtabs ();
return symtabs.Lookup (address);
}
public CompilationUnit[] CompilationUnits {
get {
CompilationUnit[] list = new CompilationUnit [compile_units.Count];
compile_units.CopyTo (list, 0);
return list;
}
}
public ISymbolTable SymbolTable {
get {
build_symtabs ();
return symtabs;
}
}
public void ReadSymbolTable ()
{
read_children ();
}
CompilationUnit get_comp_unit (long offset)
{
foreach (CompilationUnit comp_unit in compile_units) {
long start = comp_unit.RealStartOffset;
long end = start + comp_unit.UnitLength;
if ((offset >= start) && (offset < end))
return comp_unit;
}
return null;
}
public MethodSource GetMethod (long offset)
{
build_symtabs ();
CompilationUnit comp_unit = get_comp_unit (offset);
if (comp_unit == null)
return null;
DieCompileUnit die = comp_unit.DieCompileUnit;
DieSubprogram subprog = die.GetSubprogram (offset);
if (subprog == null)
return null;
return subprog.MethodSource;
}
void read_children ()
{
// If we're already initialized, we don't need to do any locking,
// so do this check here without locking.
if (initialized)
return;
lock (this) {
// We need to check this again after we acquired the thread
// lock to avoid a race condition.
if (initialized)
return;
foreach (CompilationUnit comp_unit in compile_units)
comp_unit.DieCompileUnit.ReadChildren ();
initialized = true;
}
}
void build_symtabs ()
{
// If we're already initialized, we don't need to do any locking,
// so do this check here without locking.
if (symbols_initialized)
return;
lock (this) {
// We need to check this again after we acquired the thread
// lock to avoid a race condition.
if (symbols_initialized)
return;
symtabs = new SymbolTableCollection ();
symtabs.Lock ();
foreach (CompilationUnit comp_unit in compile_units)
symtabs.AddSymbolTable (comp_unit.SymbolTable);
symtabs.UnLock ();
symbols_initialized = true;
}
}
public CompileUnitBlock (DwarfReader dwarf, long start)
{
this.dwarf = dwarf;
this.offset = start;
DwarfBinaryReader reader = dwarf.DebugInfoReader;
reader.Position = offset;
long length_field = reader.ReadInitialLength ();
long stop = reader.Position + length_field;
length = stop - offset;
int version = reader.ReadInt16 ();
if (version < 2)
throw new DwarfException (
dwarf.bfd, "Wrong DWARF version: {0}", version);
reader.ReadOffset ();
int address_size = reader.ReadByte ();
reader.Position = offset;
if ((address_size != 4) && (address_size != 8))
throw new DwarfException (
dwarf.bfd, "Unknown address size: {0}",
address_size);
compile_units = new ArrayList ();
while (reader.Position < stop) {
CompilationUnit comp_unit = new CompilationUnit (dwarf, reader);
compile_units.Add (comp_unit);
}
}
public override string ToString ()
{
return String.Format ("CompileUnitBlock ({0}:{1}:{2})",
dwarf.FileName, offset, length);
}
}
public ISymbolTable SymbolTable {
get {
return symtab;
}
}
protected class DwarfSymbolTable : SymbolTable
{
DwarfReader dwarf;
ArrayList ranges;
public DwarfSymbolTable (DwarfReader dwarf, ArrayList ranges)
{
this.dwarf = dwarf;
this.ranges = ranges;
this.ranges.Sort ();
}
public override bool HasRanges {
get {
return true;
}
}
public override ISymbolRange[] SymbolRanges {
get {
ISymbolRange[] retval = new ISymbolRange [ranges.Count];
ranges.CopyTo (retval, 0);
return retval;
}
}
public override bool HasMethods {
get {
return false;
}
}
protected override ArrayList GetMethods ()
{
throw new InvalidOperationException ();
}
public ArrayList GetAllMethods ()
{
ArrayList methods = new ArrayList ();
foreach (RangeEntry range in ranges) {
ISymbolTable symtab = dwarf.get_symtab_at_offset (range.FileOffset);
if (!symtab.IsLoaded || !symtab.HasMethods)
continue;
methods.AddRange (symtab.Methods);
}
return methods;
}
public override string ToString ()
{
return String.Format ("{0} ({1}:{2})", GetType (),
dwarf.FileName, ranges.Count);
}
}
private class RangeEntry : SymbolRangeEntry
{
public readonly long FileOffset;
DwarfReader dwarf;
public RangeEntry (DwarfReader dwarf, long offset,
TargetAddress address, long size)
: base (address, address + size)
{
this.dwarf = dwarf;
this.FileOffset = offset;
}
protected override ISymbolLookup GetSymbolLookup ()
{
return dwarf.get_symtab_at_offset (FileOffset);
}
public override string ToString ()
{
return String.Format ("RangeEntry ({0}:{1}:{2})",
StartAddress, EndAddress, FileOffset);
}
}
ArrayList read_aranges ()
{
DwarfBinaryReader reader = DebugArangesReader;
ArrayList ranges = new ArrayList ();
while (!reader.IsEof) {
long length = reader.ReadInitialLength ();
long stop = reader.Position + length;
int version = reader.ReadInt16 ();
long offset = reader.ReadOffset ();
int address_size = reader.ReadByte ();
int segment_size = reader.ReadByte ();
if ((address_size != 4) && (address_size != 8))
throw new DwarfException (
bfd, "Unknown address size: {0}", address_size);
if (segment_size != 0)
throw new DwarfException (
bfd, "Segmented address mode not supported");
if (version != 2)
throw new DwarfException (
bfd, "Wrong version in .debug_aranges: {0}",
version);
if (AddressSize == 8)
reader.Position = ((reader.Position+15) >> 4) * 16;
else
reader.Position = ((reader.Position+7) >> 3) * 8;
while (reader.Position < stop) {
long address = reader.ReadAddress ();
long size = reader.ReadAddress ();
if ((address == 0) && (size == 0))
break;
TargetAddress taddress = GetAddress (address);
ranges.Add (new RangeEntry (this, offset, taddress, size));
}
}
return ranges;
}
private class NameEntry
{
public readonly long FileOffset;
public readonly long Offset;
public long AbsoluteOffset {
get { return FileOffset + Offset; }
}
public NameEntry (long file_offset, long offset)
{
this.FileOffset = file_offset;
this.Offset = offset;
}
public override string ToString ()
{
return String.Format ("NameEntry ({0}:{1})",
FileOffset, Offset);
}
}
Hashtable read_pubnames ()
{
DwarfBinaryReader reader = DebugPubnamesReader;
// if the reader comes back null, we just can't look up symbols
// by name, return null.
if (reader == null)
return null;
Hashtable names = Hashtable.Synchronized (new Hashtable ());
while (!reader.IsEof) {
long length = reader.ReadInitialLength ();
long stop = reader.Position + length;
int version = reader.ReadInt16 ();
long debug_offset = reader.ReadOffset ();
reader.ReadOffset ();
if (version != 2)
throw new DwarfException (
bfd, "Wrong version in .debug_pubnames: {0}",
version);
while (reader.Position < stop) {
long offset = reader.ReadInt32 ();
if (offset == 0)
break;
string name = reader.ReadString ();
if (!names.Contains (name))
names.Add (name, new NameEntry (debug_offset, offset));
}
}
return names;
}
Hashtable read_pubtypes ()
{
DwarfBinaryReader reader = DebugPubtypesReader;
// if the reader comes back null, we just can't look up types
// by name, return null.
if (reader == null)
return null;
Hashtable names = Hashtable.Synchronized (new Hashtable ());
while (!reader.IsEof) {
long length = reader.ReadInitialLength ();
long stop = reader.Position + length;
int version = reader.ReadInt16 ();
long debug_offset = reader.ReadOffset ();
reader.ReadOffset ();
if (version != 2)
throw new DwarfException (
bfd, "Wrong version in .debug_pubtypes: {0}",
version);
while (reader.Position < stop) {
long offset = reader.ReadInt32 ();
if (offset == 0)
break;
string name = reader.ReadString ();
if (!names.Contains (name))
names.Add (name, new NameEntry (debug_offset, offset));
}
}
return names;
}
object create_reader_func (object user_data)
{
try {
byte[] contents = bfd.GetSectionContents ((string) user_data);
return new TargetBlob (contents, bfd.TargetMemoryInfo);
} catch {
Report.Debug (DebugFlags.DwarfReader,
"{1} Can't find DWARF 2 debugging info in section `{0}'",
bfd.FileName, (string) user_data);
return null;
}
}
ObjectCache create_reader (string section_name)
{
return new ObjectCache (new ObjectCacheFunc (create_reader_func), section_name, 5);
}
//
// These properties always create a new DwarfBinaryReader instance, but all these instances
// share the buffer they're reading from. A DwarfBinaryReader just contains a reference to
// the data and the current position - so by creating a new instance each time we start a
// read operation, reading will be thread-safe.
//
public DwarfBinaryReader DebugInfoReader {
get {
return new DwarfBinaryReader (
bfd, (TargetBlob) debug_info_reader.Data, Is64Bit);
}
}
public DwarfBinaryReader DebugAbbrevReader {
get {
return new DwarfBinaryReader (
bfd, (TargetBlob) debug_abbrev_reader.Data, Is64Bit);
}
}
public DwarfBinaryReader DebugPubnamesReader {
get {
TargetBlob blob = (TargetBlob) debug_pubnames_reader.Data;
if (blob == null)
return null;
else
return new DwarfBinaryReader (
bfd, (TargetBlob) debug_pubnames_reader.Data, Is64Bit);
}
}
public DwarfBinaryReader DebugPubtypesReader {
get {
TargetBlob blob = (TargetBlob) debug_pubtypes_reader.Data;
if (blob == null)
return null;
else
return new DwarfBinaryReader (
bfd, (TargetBlob) debug_pubtypes_reader.Data, Is64Bit);
}
}
public DwarfBinaryReader DebugLineReader {
get {
return new DwarfBinaryReader (
bfd, (TargetBlob) debug_line_reader.Data, Is64Bit);
}
}
public DwarfBinaryReader DebugArangesReader {
get {
return new DwarfBinaryReader (
bfd, (TargetBlob) debug_aranges_reader.Data, Is64Bit);
}
}
public DwarfBinaryReader DebugStrReader {
get {
return new DwarfBinaryReader (
bfd, (TargetBlob) debug_str_reader.Data, Is64Bit);
}
}
public DwarfBinaryReader DebugLocationReader {
get {
return new DwarfBinaryReader (
bfd, (TargetBlob) debug_loc_reader.Data, Is64Bit);
}
}
public string FileName {
get {
return filename;
}
}
public bool Is64Bit {
get {
return is64bit;
}
}
public byte AddressSize {
get {
return address_size;
}
}
[Conditional("REAL_DEBUG")]
static void debug (string message, params object[] args)
{
// Console.WriteLine (String.Format (message, args));
}
protected enum DwarfLang {
C89 = 0x0001,
C = 0x0002,
Ada83 = 0x0003,
C_plus_plus = 0x0004,
Cobol74 = 0x0005,
Cobol85 = 0x0006,
Fortran77 = 0x0007,
Fortran90 = 0x0008,
Pascal83 = 0x0009,
Modula2 = 0x000a,
None = 0x8001
}
protected enum DwarfTag {
array_type = 0x01,
class_type = 0x02,
entry_point = 0x03,
enumeration_type = 0x04,
formal_parameter = 0x05,
imported_declaration = 0x08,
label = 0x0a,
lexical_block = 0x0b,
member = 0x0d,
pointer_type = 0x0f,
reference_type = 0x10,
compile_unit = 0x11,
string_type = 0x12,
structure_type = 0x13,
subroutine_type = 0x15,
typedef = 0x16,
union_type = 0x17,
unspecified_parameters = 0x18,
variant = 0x19,
common_block = 0x1a,
comp_dir = 0x1b,
inheritance = 0x1c,
inlined_subroutine = 0x1d,
module = 0x1e,
ptr_to_member_type = 0x1f,
set_type = 0x20,
subrange_type = 0x21,
with_stmt = 0x22,
access_declaration = 0x23,
base_type = 0x24,
catch_block = 0x25,
const_type = 0x26,
constant = 0x27,
enumerator = 0x28,
file_type = 0x29,
friend = 0x2a,
namelist = 0x2b,
namelist_item = 0x2c,
packed_type = 0x2d,
subprogram = 0x2e,
template_type_param = 0x2f,
template_value_param = 0x30,
thrown_type = 0x31,
try_block = 0x32,
variant_block = 0x33,
variable = 0x34,
volatile_type = 0x35
}
protected enum DwarfAttribute {
sibling = 0x01,
location = 0x02,
name = 0x03,
ordering = 0x09,
byte_size = 0x0b,
bit_offset = 0x0c,
bit_size = 0x0d,
stmt_list = 0x10,
low_pc = 0x11,
high_pc = 0x12,
language = 0x13,
discr = 0x15,
discr_value = 0x16,
visibility = 0x17,
import = 0x18,
string_length = 0x19,
common_reference = 0x1a,
comp_dir = 0x1b,
const_value = 0x1c,
containing_type = 0x1d,
default_value = 0x1e,
inline = 0x20,
is_optional = 0x21,
lower_bound = 0x22,
producer = 0x25,
prototyped = 0x27,
return_addr = 0x2a,
start_scope = 0x2c,
stride_size = 0x2e,
upper_bound = 0x2f,
abstract_origin = 0x31,
accessibility = 0x32,
address_class = 0x33,
artificial = 0x34,
base_types = 0x35,
calling_convention = 0x36,
count = 0x37,
data_member_location = 0x38,
decl_column = 0x39,
decl_file = 0x3a,
decl_line = 0x3b,
declaration = 0x3c,
discr_list = 0x3d,
encoding = 0x3e,
external = 0x3f,
frame_base = 0x40,
friend = 0x41,
identifier_case = 0x42,
macro_info = 0x43,
namelist_item = 0x44,
priority = 0x45,
segment = 0x46,
specification = 0x47,
static_link = 0x48,
type = 0x49,
use_location = 0x4a,
variable_parameter = 0x4b,
virtuality = 0x4c,
vtable_elem_location = 0x4d
}
protected enum DwarfBaseTypeEncoding {
address = 0x01,
boolean = 0x02,
complex_float = 0x03,
normal_float = 0x04,
signed = 0x05,
signed_char = 0x06,
unsigned = 0x07,
unsigned_char = 0x08,
imaginary_float = 0x09
}
protected enum DwarfForm {
addr = 0x01,
block2 = 0x03,
block4 = 0x04,
data2 = 0x05,
data4 = 0x06,
data8 = 0x07,
cstring = 0x08,
block = 0x09,
block1 = 0x0a,
data1 = 0x0b,
flag = 0x0c,
sdata = 0x0d,
strp = 0x0e,
udata = 0x0f,
ref_addr = 0x10,
ref1 = 0x11,
ref2 = 0x12,
ref4 = 0x13,
ref8 = 0x14,
ref_udata = 0x15,
indirect = 0x16
}
protected enum DwarfInline {
not_inlined = 0x00,
inlined = 0x01,
declared_not_inlined = 0x02,
declared_inline = 0x03
}
protected struct LineNumber : IComparable
{
public readonly long Offset;
public readonly int Line;
public readonly int File;
public LineNumber (int file, int line, long offset)
{
this.File = file;
this.Line = line;
this.Offset = offset;
}
public int CompareTo (object obj)
{
LineNumber entry = (LineNumber) obj;
if (entry.Offset < Offset)
return 1;
else if (entry.Offset > Offset)
return -1;
else
return 0;
}
public override string ToString ()
{
return String.Format ("LineNumber ({0}:{1}:{2:x})",
File, Line, Offset);
}
}
protected class LineNumberEngine : LineNumberTable
{
protected DieCompileUnit comp_unit;
protected DwarfBinaryReader reader;
protected byte minimum_insn_length;
protected bool default_is_stmt;
protected byte opcode_base;
protected int line_base, line_range;
protected int const_add_pc_range;
protected ArrayList source_files;
long offset;
long length;
int version;
long header_length, data_offset, end_offset;
int[] standard_opcode_lengths;
ArrayList include_dirs;
string compilation_dir;
ArrayList lines;
LineNumber[] addresses;
StatementMachine stm;
protected class StatementMachine
{
public LineNumberEngine engine;
public long st_address;
public int st_line;
public int st_file;
public int st_column;
public bool is_stmt;
public bool basic_block;
public bool end_sequence;
public bool prologue_end;
public bool epilogue_begin;
public long start_offset;
public long end_offset;
public int start_file;
public int start_line, end_line;
bool creating;
DwarfBinaryReader reader;
int const_add_pc_range;
public StatementMachine (LineNumberEngine engine, long offset,
long end_offset)
{
this.engine = engine;
this.reader = this.engine.reader;
this.creating = true;
this.start_offset = offset;
this.end_offset = end_offset;
this.st_address = 0;
this.st_file = 1;
this.st_line = 1;
this.st_column = 0;
this.is_stmt = this.engine.default_is_stmt;
this.basic_block = false;
this.end_sequence = false;
this.prologue_end = false;
this.epilogue_begin = false;
this.start_file = st_file;
this.const_add_pc_range =
((0xff - engine.opcode_base) / engine.line_range) *
engine.minimum_insn_length;
}
public void set_end_sequence ()
{
engine.debug ("SET END SEQUENCE");
end_sequence = true;
end_line = st_line;
st_address = 0;
st_file = 1;
st_line = 1;
st_column = 0;
is_stmt = engine.default_is_stmt;
basic_block = false;
end_sequence = false;
prologue_end = false;
epilogue_begin = false;
start_file = st_file;
}
}
protected enum StandardOpcode
{
extended_op = 0,
copy = 1,
advance_pc = 2,
advance_line = 3,
set_file = 4,
set_column = 5,
negate_stmt = 6,
set_basic_block = 7,
const_add_pc = 8,
fixed_advance_pc = 9,
set_prologue_end = 10,
set_epilogue_begin = 11,
set_isa = 12
}
protected enum ExtendedOpcode
{
end_sequence = 1,
set_address = 2,
define_file = 3
}
protected struct FileEntry {
public readonly string FileName;
public readonly int Directory;
public readonly int LastModificationTime;
public readonly int Length;
public readonly SourceFile File;
public FileEntry (LineNumberEngine engine, DwarfBinaryReader reader)
{
FileName = reader.ReadString ();
Directory = reader.ReadLeb128 ();
LastModificationTime = reader.ReadLeb128 ();
Length = reader.ReadLeb128 ();
string dir_name;
if (Directory > 0)
dir_name = (string) engine.include_dirs [Directory - 1];
else
dir_name = engine.compilation_dir;
string full_name;
if (dir_name != null)
full_name = Path.Combine (dir_name, FileName);
else
full_name = FileName;
File = engine.comp_unit.dwarf.GetSourceFile (full_name);
}
public override string ToString ()
{
return String.Format ("FileEntry({0},{1})", FileName, Directory);
}
}
void commit ()
{
debug ("COMMIT: {0:x} {1} {2} {3}", stm.st_address, stm.st_line,
stm.st_file, stm.start_file);
lines.Add (new LineNumber (stm.st_file, stm.st_line, stm.st_address));
stm.basic_block = false;
stm.prologue_end = false;
stm.epilogue_begin = false;
}
void warning (string message, params object[] args)
{
Console.WriteLine (message, args);
}
void error (string message, params object[] args)
{
throw new DwarfException (comp_unit.dwarf.bfd, message, args);
}
[Conditional("REAL_DEBUG")]
void debug (string message, params object[] args)
{
// Console.WriteLine (String.Format (message, args));
}
public LineNumberEngine (DieCompileUnit comp_unit, long offset,
string compilation_dir)
{
this.comp_unit = comp_unit;
this.offset = offset;
this.reader = comp_unit.dwarf.DebugLineReader;
this.compilation_dir = compilation_dir;
debug ("NEW LNE: {0}", offset);
reader.Position = offset;
length = reader.ReadInitialLength ();
end_offset = reader.Position + length;
version = reader.ReadInt16 ();
header_length = reader.ReadOffset ();
data_offset = reader.Position + header_length;
minimum_insn_length = reader.ReadByte ();
default_is_stmt = reader.ReadByte () != 0;
line_base = (sbyte) reader.ReadByte ();
line_range = reader.ReadByte ();
opcode_base = reader.ReadByte ();
standard_opcode_lengths = new int [opcode_base - 1];
for (int i = 0; i < opcode_base - 1; i++)
standard_opcode_lengths [i] = reader.ReadByte ();
include_dirs = new ArrayList ();
while (reader.PeekByte () != 0)
include_dirs.Add (reader.ReadString ());
reader.Position++;
source_files = new ArrayList ();
while (reader.PeekByte () != 0)
source_files.Add (new FileEntry (this, reader));
reader.Position++;
const_add_pc_range = ((0xff - opcode_base) / line_range) *
minimum_insn_length;
debug ("NEW LNE #1: {0} {1} - {2} {3} {4}",
reader.Position, offset, length,
data_offset, end_offset);
lines = new ArrayList ();
stm = new StatementMachine (this, data_offset, end_offset);
Read ();
lines.Sort ();
addresses = new LineNumber [lines.Count];
lines.CopyTo (addresses, 0);
}
protected void Read ()
{
reader.Position = data_offset;
while (reader.Position < end_offset) {
byte opcode = reader.ReadByte ();
debug ("OPCODE: {0:x}", opcode);
if (opcode == 0)
do_extended_opcode ();
else if (opcode < opcode_base)
do_standard_opcode (opcode);
else {
opcode -= opcode_base;
int addr_inc = (opcode / line_range) * minimum_insn_length;
int line_inc = line_base + (opcode % line_range);
debug ("INC: {0:x} {1:x} {2:x} {3:x} - {4} {5}",
opcode, opcode_base, addr_inc, line_inc,
opcode % line_range, opcode / line_range);
stm.st_line += line_inc;
stm.st_address += addr_inc;
commit ();
}
}
}
void do_standard_opcode (byte opcode)
{
debug ("STANDARD OPCODE: {0:x} {1}", opcode, (StandardOpcode) opcode);
switch ((StandardOpcode) opcode) {
case StandardOpcode.copy:
commit ();
break;
case StandardOpcode.advance_pc:
stm.st_address += minimum_insn_length * reader.ReadLeb128 ();
break;
case StandardOpcode.advance_line:
stm.st_line += reader.ReadSLeb128 ();
break;
case StandardOpcode.set_file:
stm.st_file = reader.ReadLeb128 ();
debug ("FILE: {0}", stm.st_file);
break;
case StandardOpcode.set_column:
debug ("SET COLUMN");
stm.st_column = reader.ReadLeb128 ();
break;
case StandardOpcode.const_add_pc:
stm.st_address += const_add_pc_range;
break;
case StandardOpcode.set_prologue_end:
debug ("PROLOGUE END");
stm.prologue_end = true;
break;
case StandardOpcode.set_epilogue_begin:
debug ("EPILOGUE BEGIN");
stm.epilogue_begin = true;
break;
default:
error ("Unknown standard opcode {0:x} in line number engine",
opcode);
break;
}
}
void do_extended_opcode ()
{
byte size = reader.ReadByte ();
long end_pos = reader.Position + size;
byte opcode = reader.ReadByte ();
debug ("EXTENDED OPCODE: {0:x} {1:x}", size, opcode);
switch ((ExtendedOpcode) opcode) {
case ExtendedOpcode.set_address:
stm.st_address = reader.ReadAddress ();
debug ("SETTING ADDRESS TO {0:x}", stm.st_address);
break;
case ExtendedOpcode.end_sequence:
stm.set_end_sequence ();
break;
default:
warning ("Unknown extended opcode {0:x} in line number " +
"engine at offset {1}", opcode, reader.Position);
break;
}
reader.Position = end_pos;
}
public override TargetAddress Lookup (int line)
{
for (int i = 0; i < addresses.Length; i++) {
LineNumber entry = (LineNumber) addresses [i];
if (entry.Line != line)
continue;
return comp_unit.dwarf.GetAddress (entry.Offset);
}
return TargetAddress.Null;
}
SourceAddress do_lookup (TargetAddress address, int start_pos, int end_pos)
{
if (end_pos - start_pos >= 4) {
int middle_pos = (start_pos + end_pos) / 2;
LineNumber middle = (LineNumber) addresses [middle_pos];
TargetAddress maddr = comp_unit.dwarf.GetAddress (middle.Offset);
if (address < maddr)
return do_lookup (address, start_pos, middle_pos);
else
return do_lookup (address, middle_pos, end_pos);
}
TargetAddress next_address;
if (end_pos == addresses.Length)
next_address = comp_unit.EndAddress;
else {
LineNumber end_line = addresses [end_pos];
next_address = comp_unit.dwarf.GetAddress (end_line.Offset);
}
for (int i = end_pos-1; i >= start_pos; i--) {
LineNumber line = addresses [i];
int range = (int) (next_address - address);
next_address = comp_unit.dwarf.GetAddress (line.Offset);
if (next_address > address)
continue;
int offset = (int) (address - next_address);
FileEntry file = (FileEntry) source_files [line.File - 1];
return new SourceAddress (
file.File, null, line.Line, offset, range);
}
return null;
}
public override SourceAddress Lookup (TargetAddress address)
{
return do_lookup (address, 0, addresses.Length-1);
}
public override void DumpLineNumbers ()
{
Console.WriteLine ("--------");
Console.WriteLine ("DUMPING DWARF LINE NUMBER TABLE");
Console.WriteLine ("--------");
for (int i = 0; i < addresses.Length; i++) {
LineNumber line = (LineNumber) addresses [i];
Console.WriteLine ("{0,4} {1,4} {2:x}", i,
line.Line, line.Offset);
}
Console.WriteLine ("--------");
}
public override string ToString ()
{
return String.Format (
"LineNumberEngine ({0:x},{1:x},{2},{3} - {4},{5},{6},{7})",
offset, length, version, header_length,
default_is_stmt, line_base, line_range, opcode_base);
}
}
protected struct AttributeEntry
{
DwarfReader dwarf;
DwarfAttribute attr;
DwarfForm form;
public AttributeEntry (DwarfReader dwarf, DwarfAttribute attr, DwarfForm form)
{
this.dwarf = dwarf;
this.attr = attr;
this.form = form;
}
public DwarfAttribute DwarfAttribute {
get {
return attr;
}
}
public DwarfForm DwarfForm {
get {
return form;
}
}
public Attribute ReadAttribute (long offset)
{
return new Attribute (dwarf, offset, attr, form);
}
public override string ToString ()
{
return String.Format ("AttributeEntry ({0}:{1})", DwarfAttribute,
DwarfForm);
}
}
protected class Attribute
{
DwarfReader dwarf;
DwarfAttribute attr;
DwarfForm form;
long offset;
bool has_datasize, has_data;
int data_size;
object data;
public Attribute (DwarfReader dwarf, long offset,
DwarfAttribute attr, DwarfForm form)
{
this.dwarf = dwarf;
this.offset = offset;
this.attr = attr;
this.form = form;
}
public DwarfAttribute DwarfAttribute {
get {
return attr;
}
}
public DwarfForm DwarfForm {
get {
return form;
}
}
int get_datasize ()
{
switch (form) {
case DwarfForm.ref1:
case DwarfForm.data1:
case DwarfForm.flag:
return 1;
case DwarfForm.ref2:
case DwarfForm.data2:
return 2;
case DwarfForm.ref4:
case DwarfForm.data4:
return 4;
case DwarfForm.ref8:
case DwarfForm.data8:
return 8;
case DwarfForm.addr:
case DwarfForm.ref_addr:
return dwarf.AddressSize;
case DwarfForm.block1:
return dwarf.DebugInfoReader.PeekByte (offset) + 1;
case DwarfForm.block2:
return dwarf.DebugInfoReader.PeekInt16 (offset) + 2;
case DwarfForm.block4:
return dwarf.DebugInfoReader.PeekInt32 (offset) + 4;
case DwarfForm.block:
case DwarfForm.ref_udata: {
int size, size2;
size2 = dwarf.DebugInfoReader.PeekLeb128 (offset, out size);
return size + size2;
}
case DwarfForm.udata:
case DwarfForm.sdata: {
int size;
dwarf.DebugInfoReader.PeekLeb128 (offset, out size);
return size;
}
case DwarfForm.strp:
return dwarf.Is64Bit ? 8 : 4;
case DwarfForm.cstring: {
string str = dwarf.DebugInfoReader.PeekString (offset);
return str.Length + 1;
}
default:
throw new DwarfException (
dwarf.bfd, "Unknown DW_FORM: 0x{0:x}",
(int) form);
}
}
public int DataSize {
get {
if (has_datasize)
return data_size;
data_size = get_datasize ();
has_datasize = true;
return data_size;
}
}
object read_data ()
{
DwarfBinaryReader reader = dwarf.DebugInfoReader;
switch (form) {
case DwarfForm.flag:
data_size = 1;
return reader.PeekByte (offset) != 0;
case DwarfForm.ref1:
case DwarfForm.data1:
data_size = 1;
return (long) reader.PeekByte (offset);
case DwarfForm.ref2:
case DwarfForm.data2:
data_size = 2;
return (long) reader.PeekInt16 (offset);
case DwarfForm.ref4:
case DwarfForm.data4:
data_size = 4;
return (long) reader.PeekInt32 (offset);
case DwarfForm.ref8:
case DwarfForm.data8:
data_size = 8;
return (long) reader.PeekInt64 (offset);
case DwarfForm.addr:
data_size = dwarf.AddressSize;
return (long) reader.PeekAddress (offset);
case DwarfForm.cstring: {
string retval = reader.PeekString (offset);
data_size = retval.Length + 1;
return retval;
}
case DwarfForm.block1:
data_size = reader.PeekByte (offset) + 1;
return reader.PeekBuffer (offset + 1, data_size - 1);
case DwarfForm.block2:
data_size = reader.PeekInt16 (offset) + 2;
return reader.PeekBuffer (offset + 2, data_size - 2);
case DwarfForm.block4:
data_size = reader.PeekInt32 (offset) + 4;
return reader.PeekBuffer (offset + 4, data_size - 4);
case DwarfForm.block: {
int size;
data_size = reader.PeekLeb128 (offset, out size);
return reader.PeekBuffer (offset + size, data_size);
}
case DwarfForm.strp: {
long str_offset = reader.PeekOffset (offset, out data_size);
return dwarf.DebugStrReader.PeekString (str_offset);
}
case DwarfForm.ref_udata:
case DwarfForm.udata:
case DwarfForm.sdata:
return (long) reader.PeekLeb128 (offset, out data_size);
case DwarfForm.ref_addr:
return (long) reader.PeekOffset (offset, out data_size);
default:
throw new DwarfException (
dwarf.bfd, "Unknown DW_FORM: 0x{0:x}",
(int) form);
}
}
public object Data {
get {
if (has_data)
return data;
data = read_data ();
has_datasize = true;
has_data = true;
return data;
}
}
public override string ToString ()
{
return String.Format ("Attribute ({2}({0:x}),{3}({1:x}))",
(int) attr, (int) form, attr, form);
}
}
protected class AbbrevEntry
{
int abbrev_id;
DwarfTag tag;
bool has_children;
public readonly ArrayList Attributes;
public AbbrevEntry (DwarfReader dwarf, DwarfBinaryReader reader)
{
abbrev_id = reader.ReadLeb128 ();
tag = (DwarfTag) reader.ReadLeb128 ();
has_children = reader.ReadByte () != 0;
Attributes = new ArrayList ();
do {
int attr = reader.ReadLeb128 ();
int form = reader.ReadLeb128 ();
if ((attr == 0) && (form == 0))
break;
Attributes.Add (new AttributeEntry (
dwarf, (DwarfAttribute) attr, (DwarfForm) form));
} while (true);
}
public int ID {
get {
return abbrev_id;
}
}
public DwarfTag Tag {
get {
return tag;
}
}
public bool HasChildren {
get {
return has_children;
}
}
public override string ToString ()
{
return String.Format ("AbbrevEntry ({0},{1},{2})",
abbrev_id, tag, has_children);
}
}
// <summary>
// Base class for all DIE's - The DWARF Debugging Information Entry.
// </summary>
protected class Die
{
public readonly CompilationUnit comp_unit;
public readonly DwarfReader dwarf;
public readonly AbbrevEntry abbrev;
public readonly long Offset;
public readonly long ChildrenOffset;
protected virtual int ReadAttributes (DwarfBinaryReader reader)
{
int total_size = 0;
foreach (AttributeEntry entry in abbrev.Attributes) {
Attribute attribute = entry.ReadAttribute (Offset + total_size);
ProcessAttribute (attribute);
total_size += attribute.DataSize;
}
return total_size;
}
protected virtual void ProcessAttribute (Attribute attribute)
{ }
ArrayList children;
protected virtual ArrayList ReadChildren (DwarfBinaryReader reader)
{
if (!abbrev.HasChildren)
return null;
children = new ArrayList ();
while (reader.PeekByte () != 0)
children.Add (CreateDie (reader, comp_unit));
reader.Position++;
return children;
}
public ArrayList Children {
get {
if (!abbrev.HasChildren)
return null;
if (children == null) {
DwarfBinaryReader reader = dwarf.DebugInfoReader;
long old_pos = reader.Position;
reader.Position = ChildrenOffset;
children = ReadChildren (reader);
reader.Position = old_pos;
}
return children;
}
}
protected Die (DwarfBinaryReader reader, CompilationUnit comp_unit,
AbbrevEntry abbrev)
{
this.comp_unit = comp_unit;
this.dwarf = comp_unit.DwarfReader;
this.abbrev = abbrev;
Offset = reader.Position;
ChildrenOffset = Offset + ReadAttributes (reader);
reader.Position = ChildrenOffset;
debug ("NEW DIE: {0} {1} {2} {3}", GetType (),
abbrev, Offset, ChildrenOffset);
if (this is DieCompileUnit)
return;
ReadChildren (reader);
}
public Die CreateDie (DwarfBinaryReader reader, CompilationUnit comp_unit)
{
long offset = reader.Position;
int abbrev_id = reader.ReadLeb128 ();
AbbrevEntry abbrev = comp_unit [abbrev_id];
return CreateDie (reader, comp_unit, offset, abbrev);
}
public static DieCompileUnit CreateDieCompileUnit (DwarfBinaryReader reader,
CompilationUnit comp_unit)
{
int abbrev_id = reader.ReadLeb128 ();
AbbrevEntry abbrev = comp_unit [abbrev_id];
return new DieCompileUnit (reader, comp_unit, abbrev);
}
protected virtual Die CreateDie (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
{
switch (abbrev.Tag) {
case DwarfTag.compile_unit:
throw new InternalError ();
case DwarfTag.subprogram:
return new DieSubprogram (reader, comp_unit, offset, abbrev);
case DwarfTag.base_type:
return new DieBaseType (reader, comp_unit, offset, abbrev);
case DwarfTag.const_type:
return new DieConstType (reader, comp_unit, offset, abbrev);
case DwarfTag.pointer_type:
return new DiePointerType (reader, comp_unit, offset, abbrev);
case DwarfTag.class_type: // for now just treat classes and structs the same.
case DwarfTag.structure_type:
return new DieStructureType (reader, comp_unit, offset, abbrev, false);
case DwarfTag.union_type:
return new DieStructureType (reader, comp_unit, offset, abbrev, true);
case DwarfTag.array_type:
return new DieArrayType (reader, comp_unit, offset, abbrev);
case DwarfTag.subrange_type:
return new DieSubrangeType (reader, comp_unit, abbrev);
case DwarfTag.enumeration_type:
return new DieEnumerationType (reader, comp_unit, offset, abbrev);
case DwarfTag.enumerator:
return new DieEnumerator (reader, comp_unit, abbrev);
case DwarfTag.typedef:
return new DieTypedef (reader, comp_unit, offset, abbrev);
case DwarfTag.subroutine_type:
return new DieSubroutineType (reader, comp_unit, offset, abbrev);
case DwarfTag.member:
return new DieMember (reader, comp_unit, abbrev);
case DwarfTag.inlined_subroutine:
debug ("INLINED SUBROUTINE!");
return new DieSubprogram (reader, comp_unit, offset, abbrev);
default:
return new Die (reader, comp_unit, abbrev);
}
}
public DieCompileUnit DieCompileUnit {
get { return comp_unit.DieCompileUnit; }
}
}
// <summary>
// The Debugging Information Entry corresponding to compilation units.
// </summary>
// <remarks>
// From the DWARF spec: <em>A compilation unit typically
// represents the text and data contributed to an executable
// by a single relocatable object file. It may be derived
// from several source files, including pre-processed
// ``include files.''</em>
// </remarks>
protected class DieCompileUnit : Die, ISymbolContainer
{
public DieCompileUnit (DwarfBinaryReader reader, CompilationUnit comp_unit,
AbbrevEntry abbrev)
: base (reader, comp_unit, abbrev)
{
if ((start_pc != 0) && (end_pc != 0))
is_continuous = true;
string file_name;
if (comp_dir != null)
file_name = String.Concat (
comp_dir, Path.DirectorySeparatorChar, name);
else
file_name = name;
file = dwarf.GetSourceFile (file_name);
}
long start_pc, end_pc;
string name;
string comp_dir;
bool is_continuous;
DwarfLang language;
SourceFile file;
CompileUnitSymbolTable symtab;
ArrayList children;
LineNumberEngine engine;
bool children_initialized;
protected long line_offset;
protected bool has_lines;
void read_children ()
{
if (children != null)
return;
children = new ArrayList ();
if (abbrev.HasChildren) {
foreach (Die child in Children) {
DieSubprogram subprog = child as DieSubprogram;
if ((subprog == null) || !subprog.IsContinuous)
continue;
children.Add (subprog);
}
}
}
void initialize_children ()
{
if (children_initialized)
return;
read_children ();
children.Sort ();
if (has_lines) {
engine = new LineNumberEngine (this, line_offset, comp_dir);
foreach (DieSubprogram subprog in children)
subprog.SetEngine (engine);
}
children_initialized = true;
}
public void ReadChildren ()
{
read_children ();
}
void read_symtab ()
{
if ((symtab != null) || !dwarf.bfd.IsLoaded)
return;
initialize_children ();
symtab = new CompileUnitSymbolTable (this);
}
protected LineNumberEngine Engine {
get {
initialize_children ();
return engine;
}
}
public ArrayList Subprograms {
get {
initialize_children ();
return children;
}
}
public DieSubprogram GetSubprogram (long offset)
{
initialize_children ();
foreach (DieSubprogram subprog in children) {
if (subprog.RealOffset == offset)
return subprog;
}
return null;
}
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.low_pc:
start_pc = (long) attribute.Data;
break;
case DwarfAttribute.high_pc:
end_pc = (long) attribute.Data;
break;
case DwarfAttribute.stmt_list:
line_offset = (long) attribute.Data;
has_lines = true;
break;
case DwarfAttribute.comp_dir:
comp_dir = (string) attribute.Data;
break;
case DwarfAttribute.name:
name = (string) attribute.Data;
break;
case DwarfAttribute.language:
#if FIXME
language = (DwarfLang)attribute.Data;
Console.WriteLine ("DieCompileUnit {0} has language {1}", name, language);
#endif
break;
}
}
public string ImageFile {
get {
return dwarf.FileName;
}
}
public string CompilationDirectory {
get {
return comp_dir;
}
}
public bool IsContinuous {
get {
return is_continuous;
}
}
public long LineNumberOffset {
get {
if (!has_lines)
return -1;
return line_offset;
}
}
public ISymbolTable SymbolTable {
get {
read_symtab ();
return symtab;
}
}
public TargetAddress StartAddress {
get {
if (!is_continuous)
throw new InvalidOperationException ();
return dwarf.GetAddress (start_pc);
}
}
public TargetAddress EndAddress {
get {
if (!is_continuous)
throw new InvalidOperationException ();
return dwarf.GetAddress (end_pc);
}
}
public SourceFile SourceFile {
get {
return file;
}
}
protected class CompileUnitSymbolTable : SymbolTable
{
DieCompileUnit comp_unit_die;
public CompileUnitSymbolTable (DieCompileUnit comp_unit_die)
: base (comp_unit_die)
{
this.comp_unit_die = comp_unit_die;
}
public override bool HasRanges {
get {
return false;
}
}
public override ISymbolRange[] SymbolRanges {
get {
throw new InvalidOperationException ();
}
}
public override bool HasMethods {
get {
return true;
}
}
protected override ArrayList GetMethods ()
{
ArrayList methods = new ArrayList ();
ArrayList list = comp_unit_die.Subprograms;
foreach (DieSubprogram subprog in list)
methods.Add (subprog.Method);
return methods;
}
}
}
// <summary>
// The Debugging Information Entry corresponding to a
// subprogram, which in most languages means a method or
// function or subroutine.
// </summary>
protected class DieSubprogram : Die, IComparable, ISymbolContainer
{
long abstract_origin, specification;
long real_offset, start_pc, end_pc;
bool is_continuous, resolved;
string name;
DwarfTargetMethod method;
LineNumberEngine engine;
ArrayList param_dies, local_dies;
TargetVariable[] parameters, locals;
DwarfLocation frame_base;
protected override void ProcessAttribute (Attribute attribute)
{
debug ("SUBPROG PROCESS ATTRIBUTE: {0} {1}", Offset, attribute);
switch (attribute.DwarfAttribute) {
case DwarfAttribute.low_pc:
start_pc = (long) attribute.Data;
// Console.WriteLine ("{0}: start_pc = {1:x}", Offset, start_pc);
break;
case DwarfAttribute.high_pc:
end_pc = (long) attribute.Data;
// Console.WriteLine ("{0}: end_pc = {1:x}", Offset, end_pc);
break;
case DwarfAttribute.name:
name = (string) attribute.Data;
// Console.WriteLine ("{0}: name = {1}", Offset, name);
break;
case DwarfAttribute.frame_base:
frame_base = new DwarfLocation (this, attribute, false);
break;
case DwarfAttribute.decl_file:
debug ("{0}: decl_file = {1}",
Offset, (long) attribute.Data);
break;
case DwarfAttribute.decl_line:
debug ("{0}: decl_line = {1}",
Offset, (long) attribute.Data);
break;
case DwarfAttribute.inline:
debug ("{0}: inline = {1}",
Offset, (DwarfInline) (long)attribute.Data);
break;
case DwarfAttribute.abstract_origin:
abstract_origin = (long) attribute.Data;
// debug ("{0} ABSTRACT ORIGIN: {1}", Offset, abstract_origin);
break;
case DwarfAttribute.specification:
specification = (long) attribute.Data;
debug ("{0} SPECIFICATION: {1}", Offset, specification);
break;
}
}
protected override Die CreateDie (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
{
switch (abbrev.Tag) {
case DwarfTag.formal_parameter:
return new DieFormalParameter (this, reader, comp_unit, abbrev);
case DwarfTag.variable:
return new DieVariable (this, reader, comp_unit, abbrev);
default:
return base.CreateDie (reader, comp_unit, offset, abbrev);
}
}
public DieSubprogram (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
: base (reader, comp_unit, abbrev)
{
this.real_offset = offset;
if ((start_pc != 0) && (end_pc != 0))
is_continuous = true;
comp_unit.AddSubprogram (offset, this);
}
public SourceFile SourceFile {
get {
return DieCompileUnit.SourceFile;
}
}
public MethodSource MethodSource {
get {
if (method == null)
return null;
return method.MethodSource;
}
}
public DwarfLocation FrameBase {
get {
return frame_base;
}
}
public string ImageFile {
get {
return dwarf.filename;
}
}
public string Name {
get {
return name != null ? name : "<unknown>";
}
}
public bool IsContinuous {
get {
return is_continuous;
}
}
TargetAddress ISymbolContainer.StartAddress {
get {
return dwarf.GetAddress (StartAddress);
}
}
TargetAddress ISymbolContainer.EndAddress {
get {
return dwarf.GetAddress (EndAddress);
}
}
public long StartAddress {
get {
if (!is_continuous)
throw new InvalidOperationException ();
return start_pc;
}
}
public long EndAddress {
get {
if (!is_continuous)
throw new InvalidOperationException ();
return end_pc;
}
}
internal long RealOffset {
get {
return real_offset;
}
}
public int CompareTo (object obj)
{
DieSubprogram die = (DieSubprogram) obj;
if (die.start_pc < start_pc)
return 1;
else if (die.start_pc > start_pc)
return -1;
else
return 0;
}
public Method Method {
get {
if (method == null)
throw new InvalidOperationException ();
return method;
}
}
public LineNumberEngine Engine {
get {
if (engine == null)
throw new InvalidOperationException ();
return engine;
}
}
public void SetEngine (LineNumberEngine engine)
{
this.engine = engine;
method = new DwarfTargetMethod (this, engine);
}
public void AddParameter (DieMethodVariable variable)
{
if (param_dies == null)
param_dies = new ArrayList ();
param_dies.Add (variable);
}
public void AddLocal (DieMethodVariable variable)
{
if (local_dies == null)
local_dies = new ArrayList ();
local_dies.Add (variable);
}
TargetVariable[] resolve_variables (ArrayList variables)
{
if (variables == null)
return new TargetVariable [0];
ArrayList list = new ArrayList ();
foreach (DieMethodVariable variable in variables) {
TargetVariable resolved = variable.Variable;
if (resolved != null)
list.Add (resolved);
}
TargetVariable[] retval = new TargetVariable [list.Count];
list.CopyTo (retval, 0);
return retval;
}
public TargetVariable[] Parameters {
get {
if (parameters == null)
parameters = resolve_variables (param_dies);
return parameters;
}
}
public TargetVariable[] Locals {
get {
if (locals == null)
locals = resolve_variables (local_dies);
return locals;
}
}
public void ReadLineNumbers ()
{
if (resolved)
return;
resolved = true;
if (abstract_origin != 0) {
debug ("READ LINE NUMBERS: {0} {1}", this,
abstract_origin);
DieSubprogram aorigin = comp_unit.GetSubprogram (abstract_origin);
debug ("READ LINE NUMBERS #1: {0}", aorigin);
if (aorigin == null)
throw new InternalError ();
aorigin.ReadLineNumbers ();
}
if (specification != 0) {
debug ("READ LINE NUMBERS #2: {0} {1}", this, specification);
DieSubprogram ssubprog = comp_unit.GetSubprogram (specification);
debug ("READ LINE NUMBERS #1: {0}", ssubprog);
if (ssubprog == null)
throw new InternalError ();
ssubprog.ReadLineNumbers ();
}
}
public override string ToString ()
{
return String.Format ("{0} ({1}:{2}:{3}:{4:x}:{5:x})", GetType (),
Name, Offset, RealOffset, start_pc, end_pc);
}
}
protected class DwarfMethodSource : MethodSource
{
DieSubprogram subprog;
int start_row;
int end_row;
public DwarfMethodSource (DieSubprogram subprog, int start_row, int end_row)
{
this.subprog = subprog;
this.start_row = start_row;
this.end_row = end_row;
}
internal long Handle {
get { return subprog.Offset; }
}
public override Module Module {
get { return subprog.dwarf.module; }
}
public override string Name {
get { return subprog.Name; }
}
public override bool IsManaged {
get { return false; }
}
public override bool IsDynamic {
get { return false; }
}
public override TargetClassType DeclaringType {
get { throw new InvalidOperationException (); }
}
public override TargetFunctionType Function {
get { throw new InvalidOperationException (); }
}
public override bool HasSourceFile {
get { return subprog.SourceFile != null; }
}
public override SourceFile SourceFile {
get { return subprog.SourceFile; }
}
public override bool HasSourceBuffer {
get { return false; }
}
public override SourceBuffer SourceBuffer {
get { throw new InvalidOperationException (); }
}
public override int StartRow {
get { return start_row; }
}
public override int EndRow {
get { return end_row; }
}
public override Method NativeMethod {
get {return subprog.Method; }
}
}
protected class DwarfTargetMethod : Method
{
LineNumberEngine engine;
DieSubprogram subprog;
DwarfMethodSource source;
int start_row, end_row;
public DwarfTargetMethod (DieSubprogram subprog, LineNumberEngine engine)
: base (subprog.Name, subprog.ImageFile, subprog.dwarf.module)
{
this.subprog = subprog;
this.engine = engine;
CheckLoaded ();
}
public DwarfReader DwarfReader {
get { return subprog.dwarf; }
}
public override object MethodHandle {
get { return this; }
}
public override int Domain {
get { return -1; }
}
public override TargetClassType GetDeclaringType (Thread target)
{
return null;
}
public override bool HasThis {
get { return false; }
}
public override TargetVariable GetThis (Thread target)
{
throw new InvalidOperationException ();
}
public override TargetVariable[] GetParameters (Thread target)
{
return subprog.Parameters;
}
public override TargetVariable[] GetLocalVariables (Thread target)
{
return subprog.Locals;
}
public override bool HasSource {
get {
read_line_numbers ();
return source != null;
}
}
public override MethodSource MethodSource {
get {
read_line_numbers ();
return source;
}
}
public override string[] GetNamespaces ()
{
return null;
}
public int StartRow {
get { return start_row; }
}
public int EndRow {
get { return end_row; }
}
void read_line_numbers ()
{
if (source != null)
return;
subprog.ReadLineNumbers ();
SourceAddress start = engine.Lookup (StartAddress);
SourceAddress end = engine.Lookup (EndAddress);
debug ("DTM - READ LNT #0: {0} {1} - {2} {3}",
this, subprog, start, end);
if ((start == null) || (end == null))
return;
start_row = start.Row;
end_row = end.Row;
debug ("DTM - READ LNT: {0} {1} - {2} {3}", start.SourceOffset,
start.SourceRange, end.SourceOffset, end.SourceRange);
SetMethodBounds (StartAddress + start.SourceRange,
EndAddress - end.SourceOffset);
source = subprog.dwarf.GetMethodSource (subprog, start_row, end_row);
subprog.dwarf.method_hash.Add (source.Handle, this);
}
public bool CheckLoaded ()
{
if (!subprog.dwarf.bfd.IsLoaded)
return false;
ISymbolContainer sc = (ISymbolContainer) subprog;
if (sc.IsContinuous)
SetAddresses (sc.StartAddress, sc.EndAddress);
SetLineNumbers (engine);
read_line_numbers ();
#if FIXME
if ((lines != null) && (lines.Length > 2)) {
LineNumber start = lines [1];
LineNumber end = lines [lines.Length - 1];
SetMethodBounds (
subprog.dwarf.GetAddress (start.Offset),
subprog.dwarf.GetAddress (end.Offset));
}
#endif
return true;
}
internal override MethodSource GetTrampoline (TargetMemoryAccess memory,
TargetAddress address)
{
return null;
}
}
protected class CompilationUnit
{
DwarfReader dwarf;
long real_start_offset, start_offset, unit_length, abbrev_offset;
int version, address_size;
DieCompileUnit comp_unit_die;
Hashtable abbrevs;
Hashtable types;
Hashtable subprogs;
public CompilationUnit (DwarfReader dwarf, DwarfBinaryReader reader)
{
this.dwarf = dwarf;
real_start_offset = reader.Position;
unit_length = reader.ReadInitialLength ();
start_offset = reader.Position;
version = reader.ReadInt16 ();
abbrev_offset = reader.ReadOffset ();
address_size = reader.ReadByte ();
if (version < 2)
throw new DwarfException (
dwarf.bfd, "Wrong DWARF version: {0}",
version);
abbrevs = new Hashtable ();
types = new Hashtable ();
subprogs = new Hashtable ();
DwarfBinaryReader abbrev_reader = dwarf.DebugAbbrevReader;
abbrev_reader.Position = abbrev_offset;
while (abbrev_reader.PeekByte () != 0) {
AbbrevEntry entry = new AbbrevEntry (dwarf, abbrev_reader);
abbrevs.Add (entry.ID, entry);
}
comp_unit_die = Die.CreateDieCompileUnit (reader, this);
reader.Position = start_offset + unit_length;
}
public DwarfReader DwarfReader {
get {
return dwarf;
}
}
public DieCompileUnit DieCompileUnit {
get {
return comp_unit_die;
}
}
public ISymbolTable SymbolTable {
get {
return DieCompileUnit.SymbolTable;
}
}
internal long RealStartOffset {
get {
return real_start_offset;
}
}
internal long UnitLength {
get {
return unit_length;
}
}
public AbbrevEntry this [int abbrev_id] {
get {
if (abbrevs.Contains (abbrev_id))
return (AbbrevEntry) abbrevs [abbrev_id];
throw new DwarfException (
dwarf.bfd, "{0} does not contain an " +
"abbreviation entry {1}", this, abbrev_id);
}
}
public void AddType (long offset, DieType type)
{
types.Add (offset, type);
}
public void AddSubprogram (long offset, DieSubprogram subprog)
{
subprogs.Add (offset, subprog);
}
public DieType GetType (long offset)
{
return (DieType) types [real_start_offset + offset];
}
public DieSubprogram GetSubprogram (long offset)
{
return (DieSubprogram) subprogs [real_start_offset + offset];
}
public override string ToString ()
{
return String.Format ("CompilationUnit ({0},{1},{2} - {3},{4},{5})",
dwarf.Is64Bit ? "64-bit" : "32-bit", version,
address_size, real_start_offset,
unit_length, abbrev_offset);
}
}
protected class DwarfLocation
{
DieSubprogram subprog;
byte[] location_block;
long loclist_offset;
bool has_loclist;
bool is_byref;
public DwarfLocation (DieSubprogram subprog, Attribute attribute, bool is_byref)
{
this.subprog = subprog;
this.is_byref = is_byref;
if (subprog == null)
throw new InternalError ();
switch (attribute.DwarfForm) {
case DwarfForm.block1:
location_block = (byte []) attribute.Data;
break;
case DwarfForm.data1:
case DwarfForm.data2:
case DwarfForm.data4:
case DwarfForm.data8:
loclist_offset = (long) attribute.Data;
has_loclist = true;
break;
default:
throw new InternalError ();
}
}
TargetLocation GetLocation (StackFrame frame, TargetMemoryAccess memory,
byte[] data)
{
TargetBinaryReader locreader = new TargetBinaryReader (
data, subprog.dwarf.TargetMemoryInfo);
byte opcode = locreader.ReadByte ();
bool is_regoffset;
int reg, off;
if ((opcode >= 0x50) && (opcode <= 0x6f)) { // DW_OP_reg
reg = opcode - 0x50 + 3;
off = 0;
is_regoffset = false;
} else if ((opcode >= 0x70) && (opcode <= 0x8f)) { // DW_OP_breg
reg = opcode - 0x70 + 3;
off = locreader.ReadSLeb128 ();
is_regoffset = true;
} else if (opcode == 0x90) { // DW_OP_regx
reg = locreader.ReadLeb128 () + 3;
off = 0;
is_regoffset = false;
} else if (opcode == 0x91) { // DW_OP_fbreg
off = locreader.ReadSLeb128 ();
if (subprog.FrameBase != null) {
TargetLocation rloc = new RelativeTargetLocation (
subprog.FrameBase.GetLocation (frame, memory),
off);
if (is_byref)
return new DereferencedTargetLocation (rloc);
else
return rloc;
} else {
is_regoffset = true;
reg = 2;
}
} else if (opcode == 0x92) { // DW_OP_bregx
reg = locreader.ReadLeb128 () + 3;
off = locreader.ReadSLeb128 ();
is_regoffset = true;
} else {
Console.WriteLine ("UNKNOWN OPCODE: {0:x}", opcode);
return null;
}
reg = subprog.dwarf.bfd.Architecture.DwarfFrameRegisterMap [reg];
MonoVariableLocation loc = MonoVariableLocation.Create (
memory, is_regoffset, frame.Registers [reg],
off, is_byref);
if (!locreader.IsEof)
return null;
return loc;
}
public TargetLocation GetLocation (StackFrame frame, TargetMemoryAccess memory)
{
if (location_block != null)
return GetLocation (frame, memory, location_block);
DwarfBinaryReader reader = subprog.dwarf.DebugLocationReader;
reader.Position = loclist_offset;
TargetAddress address = frame.TargetAddress;
TargetAddress base_address = subprog.DieCompileUnit.StartAddress;
while (true) {
long start = reader.ReadAddress ();
long end = reader.ReadAddress ();
if (start == -1) {
Console.WriteLine ("BASE SELECTION: {0:x}", end);
base_address = subprog.dwarf.GetAddress (end);
continue;
}
if ((start == 0) && (end == 0))
break;
int size = reader.ReadInt16 ();
byte[] data = reader.ReadBuffer (size);
if ((address < base_address+start) || (address >= base_address+end))
continue;
return GetLocation (frame, memory, data);
}
return null;
}
}
protected class DwarfTargetVariable : TargetVariable
{
readonly DieSubprogram subprog;
readonly string name;
readonly TargetType type;
readonly DwarfLocation location;
// int offset;
protected DwarfTargetVariable (DieSubprogram subprog, string name, TargetType type)
{
this.subprog = subprog;
this.name = name;
this.type = type;
}
public DwarfTargetVariable (DieSubprogram subprog, string name, TargetType type,
DwarfLocation location)
: this (subprog, name, type)
{
this.location = location;
}
public override string Name {
get { return name; }
}
public override TargetType Type {
get { return type; }
}
public override bool IsInScope (TargetAddress address)
{
return true;
}
public override bool IsAlive (TargetAddress address)
{
return true;
}
public bool CheckValid (StackFrame frame)
{
return true;
}
public override TargetObject GetObject (StackFrame frame)
{
return (TargetObject) frame.Thread.ThreadServant.DoTargetAccess (
delegate (TargetMemoryAccess memory) {
TargetLocation loc = location.GetLocation (frame, memory);
if (loc == null)
return null;
return type.GetObject (memory, loc);
});
}
public override string PrintLocation (StackFrame frame)
{
return (string) frame.Thread.ThreadServant.DoTargetAccess (
delegate (TargetMemoryAccess memory) {
TargetLocation loc = location.GetLocation (frame, memory);
if (loc == null)
return null;
return loc.Print ();
});
}
public override bool CanWrite {
get { return type.Kind == TargetObjectKind.Fundamental; }
}
public override void SetObject (StackFrame frame, TargetObject obj)
{
if (obj.Type != Type)
throw new InvalidOperationException ();
TargetFundamentalObject var_object = GetObject (frame) as TargetFundamentalObject;
if (var_object == null)
return;
var_object.SetObject (frame.Thread, obj);
}
public override string ToString ()
{
return String.Format ("NativeVariable [{0}:{1}]", Name, Type);
}
}
protected abstract class DieType : Die, ITypeEntry
{
string name;
protected long offset;
bool resolved, ok, type_created;
protected readonly Language language;
TargetType type;
public DieType (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
: base (reader, comp_unit, abbrev)
{
this.offset = offset;
this.language = reader.Bfd.Language;
comp_unit.AddType (offset, this);
if (name != null)
comp_unit.DwarfReader.AddType (this);
}
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.name:
name = (string) attribute.Data;
break;
}
}
protected DieType GetReference (long offset)
{
return comp_unit.GetType (offset);
}
public virtual bool IsComplete {
get { return true; }
}
public TargetType ResolveType ()
{
if (resolved)
return type;
type = CreateType ();
resolved = true;
if (type == null) {
type_created = true;
return null;
}
if (!type_created) {
type_created = true;
PopulateType ();
}
return type;
}
protected abstract TargetType CreateType ();
protected virtual void PopulateType ()
{ }
public bool HasType {
get {
if (!resolved || !ok)
throw new InvalidOperationException ();
return type != null;
}
}
public TargetType Type {
get {
if (!HasType)
return (TargetType) language.VoidType;
else
return type;
}
}
public string Name {
get {
return name;
}
}
protected void SetName (string name)
{
if (resolved)
throw new InvalidOperationException ();
this.name = name;
}
internal TargetType GetAlias (string name)
{
if (this.name == null) {
this.name = name;
return Type;
} else
return new NativeTypeAlias (language, name, this.name, Type);
}
public override string ToString ()
{
return String.Format ("{0} ({1}:{2}:{3})", GetType (),
offset, Name, type);
}
}
// <summary>
// Debugging Information Entry corresponding to base types.
// </summary>
// <remarks>
// From the DWARF spec: <em>A base type is a data type that
// is not defined in terms of other data types. Each
// programming language has a set of base types that are
// considered to be built into that language. </em>
// </remarks>
protected class DieBaseType : DieType
{
int byte_size;
int encoding;
FundamentalKind kind;
public DieBaseType (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
: base (reader, comp_unit, offset, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.byte_size:
byte_size = (int) (long) attribute.Data;
break;
case DwarfAttribute.encoding:
encoding = (int) (long) attribute.Data;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
protected override TargetType CreateType ()
{
kind = GetMonoType (
(DwarfBaseTypeEncoding) encoding, byte_size);
if (kind == FundamentalKind.Unknown)
return new NativeOpaqueType (language, Name, byte_size);
return new NativeFundamentalType (language, Name, kind, byte_size);
}
protected FundamentalKind GetMonoType (DwarfBaseTypeEncoding encoding,
int byte_size)
{
switch (encoding) {
case DwarfBaseTypeEncoding.signed:
if (byte_size == 1)
return FundamentalKind.SByte;
else if (byte_size == 2)
return FundamentalKind.Int16;
else if (byte_size <= 4)
return FundamentalKind.Int32;
else if (byte_size <= 8)
return FundamentalKind.Int64;
break;
case DwarfBaseTypeEncoding.unsigned:
if (byte_size == 1)
return FundamentalKind.Byte;
else if (byte_size == 2)
return FundamentalKind.UInt16;
else if (byte_size <= 4)
return FundamentalKind.UInt32;
else if (byte_size <= 8)
return FundamentalKind.UInt64;
break;
case DwarfBaseTypeEncoding.signed_char:
if (byte_size == 1)
return FundamentalKind.SByte;
else
return FundamentalKind.Char;
case DwarfBaseTypeEncoding.unsigned_char:
if (byte_size == 1)
return FundamentalKind.Byte;
else
return FundamentalKind.Char;
case DwarfBaseTypeEncoding.normal_float:
if (byte_size <= 4)
return FundamentalKind.Single;
else if (byte_size <= 8)
return FundamentalKind.Double;
break;
}
return FundamentalKind.Unknown;
}
}
protected class DiePointerType : DieType
{
int byte_size;
long type_offset;
DieType reference;
public DiePointerType (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
: base (reader, comp_unit, offset, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.byte_size:
byte_size = (int) (long) attribute.Data;
break;
case DwarfAttribute.type:
type_offset = (long) attribute.Data;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
protected override TargetType CreateType ()
{
TargetType ref_type;
if (type_offset == 0)
ref_type = language.VoidType;
else {
reference = GetReference (type_offset);
if (reference == null) {
Console.WriteLine (
"UNKNOWN POINTER: {0}",
comp_unit.RealStartOffset + type_offset);
return null;
}
ref_type = reference.ResolveType ();
}
if (ref_type == null)
return null;
TargetFundamentalType fundamental = ref_type as TargetFundamentalType;
if ((fundamental != null) && (fundamental.Name == "char"))
return new NativeStringType (language, byte_size);
string name;
if (Name != null)
name = Name;
else
name = String.Format ("{0}*", ref_type.Name);
return new NativePointerType (language, name, ref_type, byte_size);
}
}
protected class DieSubrangeType : Die
{
int upper_bound;
int lower_bound;
public DieSubrangeType (DwarfBinaryReader reader, CompilationUnit comp_unit,
AbbrevEntry abbrev)
: base (reader, comp_unit, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.upper_bound:
upper_bound = (int) (long) attribute.Data;
break;
case DwarfAttribute.lower_bound:
lower_bound = (int) (long) attribute.Data;
break;
case DwarfAttribute.count:
lower_bound = 0;
upper_bound = ((int) (long) attribute.Data) - 1;
break;
}
}
public int UpperBound {
get {
return upper_bound;
}
}
public int LowerBound {
get {
return lower_bound;
}
}
}
protected class DieArrayType : DieType
{
int ordering;
int byte_size;
int stride_size;
long type_offset;
DieType reference;
public DieArrayType (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
: base (reader, comp_unit, offset, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.byte_size:
byte_size = (int) (long) attribute.Data;
break;
case DwarfAttribute.stride_size:
stride_size = (int) (long) attribute.Data;
break;
case DwarfAttribute.type:
type_offset = (long) attribute.Data;
break;
case DwarfAttribute.ordering:
ordering = (int) (long) attribute.Data;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
public int Ordering {
get { return ordering; }
}
public int ByteSize {
get { return byte_size; }
}
public int StrideSize {
get { return stride_size; }
}
protected override TargetType CreateType ()
{
reference = GetReference (type_offset);
if (reference == null) {
Console.WriteLine (
"UNKNOWN POINTER: {0}",
comp_unit.RealStartOffset + type_offset);
return null;
}
TargetType ref_type = reference.ResolveType ();
if (ref_type == null)
return null;
#if false
/* not sure we want this */
if (ref_type.TypeHandle == typeof (char))
return new NativeStringType (byte_size);
#endif
string name;
if (Name != null)
name = Name;
else
name = String.Format ("{0} []", ref_type.Name);
/* XXX for now just find the first
* Subrange child and use that for the
* array dimensions. This should
* really support multidimensional
* arrays */
DieSubrangeType subrange = null;
foreach (Die d in Children) {
subrange = d as DieSubrangeType;
if (subrange == null) continue;
break;
}
return new NativeArrayType (
language, name, ref_type,
subrange.LowerBound, subrange.UpperBound, byte_size);
}
}
protected class DieEnumerator : Die
{
string name;
int const_value;
public DieEnumerator (DwarfBinaryReader reader, CompilationUnit comp_unit,
AbbrevEntry abbrev)
: base (reader, comp_unit, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.name:
name = (string) attribute.Data;
break;
case DwarfAttribute.const_value:
const_value = (int) (long) attribute.Data;
break;
}
}
public string Name {
get {
return name;
}
}
public int ConstValue {
get {
return const_value;
}
}
}
protected class DieEnumerationType : DieType
{
int byte_size;
public DieEnumerationType (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
: base (reader, comp_unit, offset, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.byte_size:
byte_size = (int) (long) attribute.Data;
break;
case DwarfAttribute.specification:
Console.WriteLine ("ugh, specification");
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
protected override TargetType CreateType ()
{
int num_elements = 0;
string name;
foreach (Die d in Children)
if (d is DieEnumerator) num_elements ++;
if (Name != null)
name = Name;
else
name = "<unknown enum>";
string[] names = new string [num_elements];
int[] values = new int [num_elements];
int i = 0;
foreach (Die d in Children) {
DieEnumerator e = d as DieEnumerator;
if (e == null) continue;
names[i] = e.Name;
values[i] = e.ConstValue;
i++;
}
return new NativeEnumType (language, name, byte_size, names, values);
}
}
protected class DieConstType : DieType
{
long type_offset;
DieType reference;
public DieConstType (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
: base (reader, comp_unit, offset, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.type:
type_offset = (long) attribute.Data;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
protected override TargetType CreateType ()
{
reference = GetReference (type_offset);
if (reference == null)
return null;
return reference.ResolveType ();
}
}
// <summary>
// Debugging Information Entry corresponding to arbitrary
// types that are assigned names by the programmer.
// </summary>
protected class DieTypedef : DieType
{
long type_offset;
DieType reference;
new NativeTypeAlias type;
public DieTypedef (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
: base (reader, comp_unit, offset, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.type:
type_offset = (long) attribute.Data;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
public override bool IsComplete {
get { return false; }
}
protected override TargetType CreateType ()
{
reference = GetReference (type_offset);
if (reference == null)
return null;
type = new NativeTypeAlias (language, Name, reference.Name);
return type;
}
protected override void PopulateType ()
{
type.TargetType = reference.ResolveType ();
}
}
// <summary>
// Debugging Information Entry corresponding to
// inheritance information.
// </summary>
// <remarks>
// From the DWARF spec: <em>The class type of
// structure type entry that describes a derived class
// or structure owns debugging information entries
// describing each of the classes or structures it is
// derived from, ordered as they were in the source
// program.</em>
// </remarks>
protected class DieInheritance : Die
{
long type_offset;
long data_member_location;
DieType reference;
public DieInheritance (DwarfBinaryReader reader,
CompilationUnit comp_unit,
AbbrevEntry abbrev)
: base (reader, comp_unit, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.type:
type_offset = (long) attribute.Data;
break;
case DwarfAttribute.data_member_location:
// the location is specified as a
// block, it appears.. not sure the
// format, but it definitely isn't a (long).
//
// data_member_location = (long)attribute.Data;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
public long TypeOffset {
get { return type_offset; }
}
}
protected class DieStructureType : DieType
{
int byte_size;
public readonly bool IsUnion;
public DieStructureType (DwarfBinaryReader reader,
CompilationUnit comp_unit, long offset,
AbbrevEntry abbrev, bool is_union)
: base (reader, comp_unit, offset, abbrev)
{
this.IsUnion = is_union;
}
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.byte_size:
byte_size = (int) (long) attribute.Data;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
protected override Die CreateDie (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
{
switch (abbrev.Tag) {
case DwarfTag.inheritance:
return new DieInheritance (reader, comp_unit, abbrev);
default:
return base.CreateDie (reader, comp_unit, offset, abbrev);
}
}
ArrayList members;
NativeFieldInfo[] fields;
new NativeStructType type;
public override bool IsComplete {
get { return abbrev.HasChildren; }
}
protected override TargetType CreateType ()
{
if (abbrev.HasChildren)
type = new NativeStructType (language, Name, fields, byte_size);
else
return new NativeTypeAlias (language, Name, Name);
return type;
}
protected override void PopulateType ()
{
if (!abbrev.HasChildren)
return;
ArrayList list = new ArrayList ();
foreach (Die child in Children) {
DieMember member = child as DieMember;
if ((member == null) || !member.Resolve (this))
continue;
TargetType mtype = member.Type;
if (mtype == null)
mtype = (TargetType) language.VoidType;
NativeFieldInfo field;
if (member.IsBitfield)
field = new NativeFieldInfo (
mtype, member.Name, list.Count,
member.DataOffset, member.BitOffset,
member.BitSize);
else
field = new NativeFieldInfo (
mtype, member.Name, list.Count,
member.DataOffset);
list.Add (field);
}
fields = new NativeFieldInfo [list.Count];
list.CopyTo (fields);
type.SetFields (fields);
}
}
protected class DieSubroutineType : DieType
{
long type_offset;
bool prototyped;
DieType return_type;
public DieSubroutineType (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
: base (reader, comp_unit, offset, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.type:
type_offset = (long) attribute.Data;
break;
case DwarfAttribute.prototyped:
prototyped = (bool) attribute.Data;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
protected override Die CreateDie (DwarfBinaryReader reader, CompilationUnit comp_unit,
long offset, AbbrevEntry abbrev)
{
switch (abbrev.Tag) {
case DwarfTag.formal_parameter:
return new DieFormalParameter (null, reader, comp_unit, abbrev);
default:
return base.CreateDie (reader, comp_unit, offset, abbrev);
}
}
protected override TargetType CreateType ()
{
if (!prototyped)
return null;
if (type_offset != 0) {
return_type = GetReference (type_offset);
if (return_type == null)
return null;
}
TargetType ret_type = null;
if (return_type != null)
ret_type = return_type.ResolveType ();
if (ret_type == null)
ret_type = (TargetType) language.VoidType;
TargetType[] param_types = new TargetType [0];
NativeFunctionType func_type = new NativeFunctionType (
language, "test", ret_type, param_types);
return func_type;
}
protected override void PopulateType ()
{
if (!abbrev.HasChildren)
return;
ArrayList args = new ArrayList ();
foreach (Die child in Children) {
DieFormalParameter formal = child as DieFormalParameter;
if (formal == null)
continue;
args.Add (formal);
}
}
}
protected abstract class DieVariableBase : Die
{
string name;
long type_offset;
public DieVariableBase (DwarfBinaryReader reader, CompilationUnit comp_unit,
AbbrevEntry abbrev)
: base (reader, comp_unit, abbrev)
{ }
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.name:
name = (string) attribute.Data;
break;
case DwarfAttribute.type:
type_offset = (long) attribute.Data;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
public string Name {
get {
return name;
}
}
public long TypeOffset {
get {
return type_offset;
}
}
}
protected abstract class DieMethodVariable : DieVariableBase
{
public DieMethodVariable (DieSubprogram subprog, DwarfBinaryReader reader,
CompilationUnit comp_unit, AbbrevEntry abbrev,
bool is_local)
: base (reader, comp_unit, abbrev)
{
this.target_info = reader.TargetMemoryInfo;
this.subprog = subprog;
if (subprog != null) {
if (is_local)
subprog.AddLocal (this);
else
subprog.AddParameter (this);
}
}
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.location:
location_attr = attribute;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
Attribute location_attr;
TargetVariable variable;
TargetMemoryInfo target_info;
DieSubprogram subprog;
bool resolved;
protected bool DoResolve ()
{
if ((TypeOffset == 0) || (Name == null) || (subprog == null) ||
(location_attr == null))
return false;
DieType reference = comp_unit.GetType (TypeOffset);
if (reference == null)
return false;
TargetType type = reference.ResolveType ();
if (type == null)
return false;
DwarfLocation location = new DwarfLocation (subprog, location_attr, type.IsByRef);
variable = new DwarfTargetVariable (subprog, Name, type, location);
return true;
}
public TargetVariable Variable {
get {
if (!resolved) {
DoResolve ();
resolved = true;
}
return variable;
}
}
}
protected class DieFormalParameter : DieMethodVariable
{
public DieFormalParameter (DieSubprogram parent, DwarfBinaryReader reader,
CompilationUnit comp_unit, AbbrevEntry abbrev)
: base (parent, reader, comp_unit, abbrev, false)
{ }
}
protected class DieVariable : DieMethodVariable
{
public DieVariable (DieSubprogram parent, DwarfBinaryReader reader,
CompilationUnit comp_unit, AbbrevEntry abbrev)
: base (parent, reader, comp_unit, abbrev, true)
{ }
}
protected class DieMember : DieVariableBase
{
public DieMember (DwarfBinaryReader reader, CompilationUnit comp_unit,
AbbrevEntry abbrev)
: base (reader, comp_unit, abbrev)
{
this.target_info = reader.TargetMemoryInfo;
}
protected override void ProcessAttribute (Attribute attribute)
{
switch (attribute.DwarfAttribute) {
case DwarfAttribute.data_member_location:
location = (byte []) attribute.Data;
break;
case DwarfAttribute.bit_offset:
bit_offset = (int) (long) attribute.Data;
break;
case DwarfAttribute.bit_size:
bit_size = (int) (long) attribute.Data;
break;
default:
base.ProcessAttribute (attribute);
break;
}
}
byte[] location;
bool resolved, ok;
DieType type_die;
TargetType type;
TargetMemoryInfo target_info;
int bit_offset, bit_size;
int offset;
public bool Resolve (DieStructureType die_struct)
{
if (resolved)
return ok;
type = ResolveType (die_struct);
resolved = true;
ok = type != null;
return ok;
}
public bool IsBitfield {
get { return bit_size != 0; }
}
public int BitOffset {
get { return bit_offset; }
}
public int BitSize {
get { return bit_size; }
}
bool read_location ()
{
TargetBinaryReader locreader = new TargetBinaryReader (
location, target_info);
switch (locreader.ReadByte ()) {
case 0x23: // DW_OP_plus_uconstant
offset = locreader.ReadLeb128 ();
return locreader.IsEof;
default:
return false;
}
}
protected TargetType ResolveType (DieStructureType die_struct)
{
if ((TypeOffset == 0) || (Name == null))
return null;
if ((location == null) && !die_struct.IsUnion)
return null;
type_die = comp_unit.GetType (TypeOffset);
if (type_die == null)
return null;
if ((location != null) && !read_location ())
return null;
type = type_die.ResolveType ();
return type;
}
public TargetType Type {
get {
if (!resolved)
throw new InvalidOperationException ();
return type;
}
}
public int DataOffset {
get {
if (!resolved)
throw new InvalidOperationException ();
return offset;
}
}
}
}
}
|