package linksem
A formalisation of the core ELF and DWARF file formats written in Lem
Install
Dune Dependency
Authors
Maintainers
Sources
0.8.tar.gz
md5=2075c56715539b3b8f54ae65cc808b8c
sha512=f7c16e4036a1440a6a8d13707a43f0f9f9db0c68489215f948cc300b6a164dba5bf852e58f89503e9d9f38180ee658d9478156ca1a1ef64d6861eec5f9cf43d2
doc/src/linksem_zarith/dwarf.ml.html
Source file dwarf.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605
(*Generated by Lem from dwarf.lem.*) (* -*-tuareg-*- *) open Lem_basic_classes open Lem_bool open Lem_function open Lem_maybe open Lem_num open Lem_string open Lem_list (* TODO: check why this is not imported in ELF *) open Byte_sequence open Error open Hex_printing open Missing_pervasives open Show open Default_printing open Endianness open String_table open Elf_dynamic open Elf_file open Elf_header open Elf_program_header_table open Elf_relocation open Elf_section_header_table open Elf_symbol_table open Elf_types_native_uint (** ***************** experimental DWARF reading *********** *) (* This defines a representation of some of the DWARF debug information, with parsing functions to extract it from the byte sequences of the relevant ELF sections, and pretty-printing function to dump it in a human-readable form, similar to that of readelf. The main functions for this are: val extract_dwarf : elf64_file -> maybe dwarf val pp_dwarf : dwarf -> string It also defines evaluation of DWARF expressions and analysis functions to convert the variable location information to a form suitable for looking up variable names from machine addresses that arise during execution, including the call frame address calculation. The main types and functions for this are: type analysed_location_data val analyse_locations : dwarf -> analysed_location_data type evaluated_frame_info val evaluate_frame_info : dwarf -> evaluated_frame_info type dwarf_static val extract_dwarf_static : elf64_file -> maybe dwarf_static The last collects all the above - information that can be computed statically. Then to do lookup from addresses to source-code names, we have: type analysed_location_data_at_pc val analysed_locations_at_pc : evaluation_context -> dwarf_static -> natural -> analysed_location_data_at_pc val names_of_address : dwarf -> analysed_location_data_at_pc -> natural -> list string The definitions are deliberately simple-minded, to be quick to write, easy to see the correspondence to the DWARF text specification, and potentially support generation of theorem-prover definitions in future. They are in a pure functional style, making the information dependencies explicit. They are not written for performance, though they may be efficient enough for small examples as-is. They are written in Lem, and compiled from that to executable OCaml. The development follows the DWARF 4 pdf specification at http://www.dwarfstd.org/ though tweaked in places where our examples use earlier versions. It doesn't systematically cover all the DWARF versions. It doesn't cover the GNU extensions (at https://fedorahosted.org/elfutils/wiki/DwarfExtensions). The representation, parsing, and pretty printing are mostly complete for the data in these DWARF ELF sections: .debug_abbrev .debug_info .debug_types .debug_loc .debug_str .debug_ranges .debug_frame (without augmentations) .debug_line The following DWARF ELF sections are not covered: .debug_aranges .debug_macinfo .debug_pubnames .debug_pubtypes The evaluation of DWARF expressions covers only some of the operations - probably enough for common cases. The analysis of DWARF location data should be enough to look up names from the addresses of variables and formal parameters. It does not currently handle the DWARF type data, so will not be useful for accesses strictly within the extent of a variable or parameter. The 'dwarf' type gives a lightly parsed representation of some of the dwarf information, with the byte sequences of the above .debug_* sections parsed into a structured representation. That makes the list and tree structures explicit, and converts the various numeric types into just natural, integer, and byte sequences. The lem natural and integer could be replaced by unsigned and signed 64-bit types; that'd probably be better for execution but not for theorem-prover use. *) (* some spec ambiguities (more in comments in-line below): *) (* can a location list be referenced from multiple compilation units, with different base addresses? *) (** debug *) (* workaround debug.lem linking *) (*val print_endline : string -> unit*) let my_debug s:unit= () (*print_endline s*) let my_debug2 s:unit= () (*print_endline s*) let my_debug3 s:unit= () (*print_endline s*) let my_debug4 s:unit= (print_endline s) let my_debug5 s:unit= (print_endline s) (** ************************************************************ *) (** ** dwarf representation types **************************** *) (** ************************************************************ *) type dwarf_attribute_classes = | DWA_7_5_3 | DWA_address | DWA_block | DWA_constant | DWA_dash | DWA_exprloc | DWA_flag | DWA_lineptr | DWA_loclistptr | DWA_macptr | DWA_rangelistptr | DWA_reference | DWA_string (* operations and expression evalution *) type operation_argument_type = | OAT_addr | OAT_dwarf_format_t | OAT_uint8 | OAT_uint16 | OAT_uint32 | OAT_uint64 | OAT_sint8 | OAT_sint16 | OAT_sint32 | OAT_sint64 | OAT_ULEB128 | OAT_SLEB128 | OAT_block type operation_argument_value = | OAV_natural of Nat_big_num.num | OAV_integer of Nat_big_num.num | OAV_block of Nat_big_num.num * byte_sequence0 type operation_stack = Nat_big_num.num list type arithmetic_context = { ac_bitwidth: Nat_big_num.num; ac_half: Nat_big_num.num; (* 2 ^ (ac_bitwidth -1) *) ac_all: Nat_big_num.num; (* 2 ^ ac_bitwidth *) ac_max: Nat_big_num.num; (* (2 ^ ac_bitwidth) -1 *) (* also the representation of -1 *) } type operation_semantics = | OpSem_lit | OpSem_deref | OpSem_stack of (arithmetic_context -> operation_stack -> operation_argument_value list -> operation_stack option) | OpSem_not_supported | OpSem_binary of (arithmetic_context -> Nat_big_num.num -> Nat_big_num.num -> Nat_big_num.num option) | OpSem_unary of (arithmetic_context -> Nat_big_num.num -> Nat_big_num.num option) | OpSem_opcode_lit of Nat_big_num.num | OpSem_reg | OpSem_breg | OpSem_bregx | OpSem_fbreg | OpSem_deref_size | OpSem_nop | OpSem_piece | OpSem_bit_piece | OpSem_implicit_value | OpSem_stack_value | OpSem_call_frame_cfa type operation = { op_code: Nat_big_num.num; op_string: string; op_argument_values: operation_argument_value list; op_semantics: operation_semantics; } (* the result of a location expression evaluation is a single_location (or failure) *) type simple_location = | SL_memory_address of Nat_big_num.num | SL_register of Nat_big_num.num | SL_implicit of byte_sequence0 (* used for implicit and stack values *) | SL_empty type composite_location_piece = | CLP_piece of Nat_big_num.num * simple_location | CLP_bit_piece of Nat_big_num.num * Nat_big_num.num * simple_location type single_location = | SL_simple of simple_location | SL_composite of composite_location_piece list (* location expression evaluation is a stack machine operating over the following state *) type state = { s_stack: operation_stack; s_value: simple_location; s_location_pieces: composite_location_piece list; } (* location expression evaluation can involve register and memory reads, via the following interface *) type 'a register_read_result = | RRR_result of Nat_big_num.num | RRR_not_currently_available | RRR_bad_register_number type 'a memory_read_result = | MRR_result of Nat_big_num.num | MRR_not_currently_available | MRR_bad_address type evaluation_context = { read_register: Nat_big_num.num -> Nat_big_num.num register_read_result; read_memory: Nat_big_num.num -> Nat_big_num.num -> Nat_big_num.num memory_read_result; } (* dwarf sections *) type dwarf_format = | Dwarf32 | Dwarf64 (* .debug_abbrev section *) type abbreviation_declaration = { ad_abbreviation_code: Nat_big_num.num; ad_tag: Nat_big_num.num; ad_has_children: bool; ad_attribute_specifications: (Nat_big_num.num * Nat_big_num.num) list; } type abbreviations_table = { at_offset: Nat_big_num.num; at_table: abbreviation_declaration list; } (* .debug_info section *) type attribute_value = (* following Figure 3 *) | AV_addr of Nat_big_num.num | AV_block of Nat_big_num.num * byte_sequence0 | AV_constantN of Nat_big_num.num * byte_sequence0 | AV_constant_SLEB128 of Nat_big_num.num | AV_constant_ULEB128 of Nat_big_num.num | AV_exprloc of Nat_big_num.num * byte_sequence0 | AV_flag of bool | AV_ref of Nat_big_num.num | AV_ref_addr of Nat_big_num.num (* dwarf_format dependent *) | AV_ref_sig8 of Nat_big_num.num | AV_sec_offset of Nat_big_num.num | AV_string of byte_sequence0 (* not including terminating null *) | AV_strp of Nat_big_num.num (* dwarf_format dependent *) type die = { die_offset: Nat_big_num.num; die_abbreviation_code: Nat_big_num.num; die_abbreviation_declaration: abbreviation_declaration; die_attribute_values: (Nat_big_num.num (*pos*) * attribute_value) list; die_children: die list; } type die_index = (Nat_big_num.num, ( die list * die)) Pmap.map type compilation_unit_header = { cuh_offset: Nat_big_num.num; cuh_dwarf_format: dwarf_format; cuh_unit_length: Nat_big_num.num; cuh_version: Nat_big_num.num; cuh_debug_abbrev_offset: Nat_big_num.num; cuh_address_size: Nat_big_num.num; } type compilation_unit = { cu_header: compilation_unit_header; cu_abbreviations_table: abbreviations_table; cu_die: die; cu_index: die_index } type compilation_units = compilation_unit list (* .debug_type section *) type type_unit_header = { tuh_cuh: compilation_unit_header; tuh_type_signature: Nat_big_num.num; tuh_type_offset: Nat_big_num.num; } type type_unit = { tu_header: type_unit_header; tu_abbreviations_table: abbreviations_table; tu_die: die; } type type_units = type_unit list (* .debug_loc section *) type single_location_description = byte_sequence0 type location_list_entry = { lle_beginning_address_offset: Nat_big_num.num; lle_ending_address_offset: Nat_big_num.num; lle_single_location_description: single_location_description; } type base_address_selection_entry = { base_address: Nat_big_num.num; } type location_list_item = | LLI_lle of location_list_entry | LLI_base of base_address_selection_entry type location_list = Nat_big_num.num (*offset*) * location_list_item list type location_list_list = location_list list (* .debug_ranges section *) type range_list_entry = { rle_beginning_address_offset: Nat_big_num.num; rle_ending_address_offset: Nat_big_num.num; } type range_list_item = | RLI_rle of range_list_entry | RLI_base of base_address_selection_entry type range_list = Nat_big_num.num (*offset (of range_list from start of .debug_ranges section?) *) * range_list_item list type range_list_list = range_list list (* .debug_frame section: call frame instructions *) type cfa_address = Nat_big_num.num type cfa_block = byte_sequence0 type cfa_delta = Nat_big_num.num type cfa_offset = Nat_big_num.num type cfa_register = Nat_big_num.num type cfa_sfoffset = Nat_big_num.num type call_frame_argument_type = | CFAT_address | CFAT_delta1 | CFAT_delta2 | CFAT_delta4 | CFAT_delta_ULEB128 | CFAT_offset (*ULEB128*) | CFAT_sfoffset (*SLEB128*) | CFAT_register (*ULEB128*) | CFAT_block type call_frame_argument_value = | CFAV_address of cfa_address | CFAV_block of cfa_block | CFAV_delta of cfa_delta | CFAV_offset of cfa_offset | CFAV_register of cfa_register | CFAV_sfoffset of cfa_sfoffset type call_frame_instruction = | DW_CFA_advance_loc of cfa_delta | DW_CFA_offset of cfa_register * cfa_offset | DW_CFA_restore of cfa_register | DW_CFA_nop | DW_CFA_set_loc of cfa_address | DW_CFA_advance_loc1 of cfa_delta | DW_CFA_advance_loc2 of cfa_delta | DW_CFA_advance_loc4 of cfa_delta | DW_CFA_offset_extended of cfa_register * cfa_offset | DW_CFA_restore_extended of cfa_register | DW_CFA_undefined of cfa_register | DW_CFA_same_value of cfa_register | DW_CFA_register of cfa_register * cfa_register | DW_CFA_remember_state | DW_CFA_restore_state | DW_CFA_def_cfa of cfa_register * cfa_offset | DW_CFA_def_cfa_register of cfa_register | DW_CFA_def_cfa_offset of cfa_offset | DW_CFA_def_cfa_expression of cfa_block | DW_CFA_expression of cfa_register * cfa_block | DW_CFA_offset_extended_sf of cfa_register * cfa_sfoffset | DW_CFA_def_cfa_sf of cfa_register * cfa_sfoffset | DW_CFA_def_cfa_offset_sf of cfa_sfoffset | DW_CFA_val_offset of cfa_register * cfa_offset | DW_CFA_val_offset_sf of cfa_register * cfa_sfoffset | DW_CFA_val_expression of cfa_register * cfa_block | DW_CFA_AARCH64_negate_ra_state | DW_CFA_unknown of char (* .debug_frame section: top-level *) type cie = { cie_offset: Nat_big_num.num; cie_length: Nat_big_num.num; cie_id: Nat_big_num.num; cie_version: Nat_big_num.num; cie_augmentation: byte_sequence0; (* not including terminating null *) cie_address_size: Nat_big_num.num option; cie_segment_size: Nat_big_num.num option; cie_code_alignment_factor: Nat_big_num.num; cie_data_alignment_factor: Nat_big_num.num; cie_return_address_register: cfa_register; cie_initial_instructions_bytes: byte_sequence0; cie_initial_instructions: call_frame_instruction list; } type fde = { fde_offset: Nat_big_num.num; fde_length: Nat_big_num.num; fde_cie_pointer: Nat_big_num.num; fde_initial_location_segment_selector: Nat_big_num.num option; fde_initial_location_address: Nat_big_num.num; fde_address_range: Nat_big_num.num; fde_instructions_bytes: byte_sequence0; fde_instructions: call_frame_instruction list; } type frame_info_element = | FIE_cie of cie | FIE_fde of fde type frame_info = frame_info_element list (* evaluated cfa data *) type cfa_rule = | CR_undefined | CR_register of cfa_register * Nat_big_num.num | CR_expression of single_location_description type register_rule = | RR_undefined (*A register that has this rule has no recoverable value in the previous frame. (By convention, it is not preserved by a callee.)*) | RR_same_value (*This register has not been modified from the previous frame. (By convention, it is preserved by the callee, but the callee has not modified it.)*) | RR_offset of Nat_big_num.num (* The previous value of this register is saved at the address CFA+N where CFA is the current CFA value and N is a signed offset.*) | RR_val_offset of Nat_big_num.num (* The previous value of this register is the value CFA+N where CFA is the current CFA value and N is a signed offset.*) | RR_register of Nat_big_num.num (* The previous value of this register is stored in another register numbered R.*) | RR_expression of single_location_description (* The previous value of this register is located at the address produced by executing the DWARF expression E.*) | RR_val_expression of single_location_description (* The previous value of this register is the value produced by executing the DWARF expression E.*) | RR_architectural (*The rule is defined externally to this specification by the augmenter*) type register_rule_map = (cfa_register * register_rule) list type cfa_table_row = { ctr_loc: Nat_big_num.num; ctr_cfa: cfa_rule; ctr_regs: register_rule_map; } type cfa_state = { cs_current_row: cfa_table_row; cs_previous_rows: cfa_table_row list; cs_initial_instructions_row: cfa_table_row; cs_row_stack: cfa_table_row list; } type evaluated_frame_info = (fde * cfa_table_row list) list (* line number *) type line_number_argument_type = | LNAT_address | LNAT_ULEB128 | LNAT_SLEB128 | LNAT_uint16 | LNAT_string type line_number_argument_value = | LNAV_address of Nat_big_num.num | LNAV_ULEB128 of Nat_big_num.num | LNAV_SLEB128 of Nat_big_num.num | LNAV_uint16 of Nat_big_num.num | LNAV_string of byte_sequence0 (* not including terminating null *) type line_number_operation = (* standard *) | DW_LNS_copy | DW_LNS_advance_pc of Nat_big_num.num | DW_LNS_advance_line of Nat_big_num.num | DW_LNS_set_file of Nat_big_num.num | DW_LNS_set_column of Nat_big_num.num | DW_LNS_negate_stmt | DW_LNS_set_basic_block | DW_LNS_const_add_pc | DW_LNS_fixed_advance_pc of Nat_big_num.num | DW_LNS_set_prologue_end | DW_LNS_set_epilogue_begin | DW_LNS_set_isa of Nat_big_num.num (* extended *) | DW_LNE_end_sequence | DW_LNE_set_address of Nat_big_num.num | DW_LNE_define_file of byte_sequence0 * Nat_big_num.num * Nat_big_num.num * Nat_big_num.num | DW_LNE_set_discriminator of Nat_big_num.num (* special *) | DW_LN_special of Nat_big_num.num (* the adjusted opcode *) type line_number_file_entry = { lnfe_path: byte_sequence0; lnfe_directory_index: Nat_big_num.num; lnfe_last_modification: Nat_big_num.num; lnfe_length: Nat_big_num.num; } type line_number_header = { lnh_offset: Nat_big_num.num; lnh_dwarf_format: dwarf_format; lnh_unit_length: Nat_big_num.num; lnh_version: Nat_big_num.num; lnh_header_length: Nat_big_num.num; lnh_minimum_instruction_length: Nat_big_num.num; lnh_maximum_operations_per_instruction: Nat_big_num.num; lnh_default_is_stmt: bool; lnh_line_base: Nat_big_num.num; lnh_line_range: Nat_big_num.num; lnh_opcode_base: Nat_big_num.num; lnh_standard_opcode_lengths: Nat_big_num.num list; lnh_include_directories: (byte_sequence0) list; lnh_file_entries: line_number_file_entry list; lnh_comp_dir: string option; (* passed down from cu DW_AT_comp_dir *) } type line_number_program = { lnp_header: line_number_header; lnp_operations: line_number_operation list; } (* line number evaluation *) type line_number_registers = { lnr_address: Nat_big_num.num; lnr_op_index: Nat_big_num.num; lnr_file: Nat_big_num.num; lnr_line: Nat_big_num.num; lnr_column: Nat_big_num.num; lnr_is_stmt: bool; lnr_basic_block: bool; lnr_end_sequence: bool; lnr_prologue_end: bool; lnr_epilogue_begin: bool; lnr_isa: Nat_big_num.num; lnr_discriminator: Nat_big_num.num; } type unpacked_file_entry = ( string option (*comp_dir*)) * ( string option (*dir*)) * string (*file*) type unpacked_decl = unpacked_file_entry * int(*line*) * string(*subprogram name*) (* top-level collection of dwarf data *) type dwarf = { d_endianness: Endianness.endianness; (* from the ELF *) d_str: byte_sequence0; d_compilation_units: compilation_units; d_type_units: type_units; d_loc: location_list_list; d_ranges: range_list_list; d_frame_info: frame_info; d_line_info: line_number_program list; } (* analysed location data *) type analysed_location_data = ((compilation_unit * ( die list) * die) * ( (Nat_big_num.num * Nat_big_num.num * single_location_description)list)option) list type analysed_location_data_at_pc = ((compilation_unit * ( die list) * die) * (Nat_big_num.num * Nat_big_num.num * single_location_description * single_location error)) list (* evaluated line data *) type evaluated_line_info = (line_number_header * line_number_registers list) list (* all dwarf static data *) type dwarf_static = { ds_dwarf: dwarf; ds_analysed_location_data: analysed_location_data; ds_evaluated_frame_info: evaluated_frame_info; ds_evaluated_line_info: evaluated_line_info; ds_subprogram_line_extents: (unpacked_file_entry * (string * unpacked_file_entry * Nat_big_num.num) list ) list; } type dwarf_dynamic_at_pc = analysed_location_data_at_pc (** context for parsing and pp functions *) type p_context = { endianness: Endianness.endianness; } (* type descriptions *) (* NB these do not cover all the DWARF-expressible types; only some common C cases *) (* ignore base type DW_endianity and DW_bitsize for now *) type cupdie = compilation_unit * ( die list) * die type decl = { decl_file: string option; decl_line: Nat_big_num.num option; } type 't array_dimension = Nat_big_num.num option(*count*) * 't option(*subrange type*) type 't struct_union_member = cupdie * ( string option)(*mname*) * 't * Nat_big_num.num option(*data_member_location, non-Nothing for structs*) type struct_union_type_kind = | Atk_structure | Atk_union type enumeration_member = cupdie * ( string option)(*mname*) * Nat_big_num.num(*const_value*) type 't c_type_top = | CT_missing of cupdie | CT_base of cupdie * string(*name*) * Nat_big_num.num(*encoding*) * ( Nat_big_num.num option)(*byte_size*) | CT_pointer of cupdie * 't option | CT_const of cupdie * 't option | CT_volatile of cupdie * 't | CT_restrict of cupdie * 't | CT_typedef of cupdie * string(*name*) * 't * decl | CT_array of cupdie * 't * ( 't array_dimension) list | CT_struct_union of cupdie * struct_union_type_kind * ( string option)(*mname*) * ( Nat_big_num.num option)(*byte_size*) * decl * ( ( 't struct_union_member)list(*members*))option | CT_enumeration of cupdie * ( string option)(*mname*) * ( 't option)(*mtyp*) * ( Nat_big_num.num option)(*mbyte_size*) * decl * ( (enumeration_member)list(*members*))option | CT_subroutine of cupdie * (bool)(*prototyped*) * ( 't option)(*mresult_type*) * ( 't list)(*parameter_types*) * (bool)(*variable_parameter_list*) (* In the CT_struct_union and C_enumeration cases, the final maybe(list(...member)) is Nothing if the analysis has not been recursed into the members, and Just ... if it has - which will typically be only one level deep *) type c_type = | CT of ( c_type c_type_top) (* simple die tree *) (* this unifies variables and formal parameters, and also subprograms and inlined_subroutines (but not lexical_blocks). Debatable what's best *) (* not including DW_AT_low_pc/DW_AT_high_pc or DW_AT_ranges - might want that*) (* also not including per-instruction line number info *) type variable_or_formal_parameter_kind = | SVPK_var | SVPK_param type sdt_unspecified_parameter = unit type sdt_variable_or_formal_parameter = { svfp_cupdie : cupdie; svfp_name : string; svfp_kind : variable_or_formal_parameter_kind; svfp_type : c_type option; svfp_abstract_origin : sdt_variable_or_formal_parameter option; (* invariant: non-Nothing iff inlined *) svfp_const_value : Nat_big_num.num option; svfp_external : bool; svfp_declaration : bool; svfp_locations : ( (Nat_big_num.num * Nat_big_num.num * operation list (*the parsed single_location_description*))list)option; svfp_decl : unpacked_decl option; } type sdt_subroutine_kind = | SSK_subprogram | SSK_inlined_subroutine type sdt_subroutine = (* subprogram or inlined subroutine *) { ss_cupdie : cupdie; ss_name : string option; ss_kind : sdt_subroutine_kind; ss_call_site : unpacked_decl option; ss_abstract_origin : sdt_subroutine option; (* invariant: non-Nothing iff inlined *) ss_type : c_type option; ss_vars : sdt_variable_or_formal_parameter list; ss_pc_ranges : ( (Nat_big_num.num*Nat_big_num.num)list)option; ss_entry_address : Nat_big_num.num option; ss_unspecified_parameters : sdt_unspecified_parameter list; ss_subroutines : sdt_subroutine list; (* invariant: all inlined*) ss_lexical_blocks : sdt_lexical_block list; ss_decl : unpacked_decl option; ss_noreturn : bool; ss_external : bool; } and sdt_lexical_block = { slb_cupdie : cupdie; slb_vars : sdt_variable_or_formal_parameter list; (* invariant: all variables *) slb_pc_ranges : ( (Nat_big_num.num*Nat_big_num.num)list)option; slb_subroutines : sdt_subroutine list; (* invariant: all inlined*) slb_lexical_blocks : sdt_lexical_block list; } type sdt_compilation_unit = { scu_cupdie : cupdie; scu_name : string; scu_subroutines : sdt_subroutine list; (* invariant: none inlined(?) *) scu_vars : sdt_variable_or_formal_parameter list; scu_pc_ranges : ( (Nat_big_num.num*Nat_big_num.num)list)option; } type sdt_dwarf = { sd_compilation_units : sdt_compilation_unit list; } (* inlined subroutine data *) type inlined_subroutine_const_param = { iscp_abstract_origin: compilation_unit * ( die list) * die; iscp_value: Nat_big_num.num; } type inlined_subroutine = { is_inlined_subroutine: compilation_unit * ( die list) * die; is_abstract_origin: compilation_unit * ( die list) * die; is_inlined_subroutine_sdt: sdt_subroutine; is_inlined_subroutine_sdt_parents: sdt_subroutine list; is_name : string; is_call_file: unpacked_file_entry; is_call_line: Nat_big_num.num; is_pc_ranges: (Nat_big_num.num * Nat_big_num.num) list; is_const_params : inlined_subroutine_const_param list; } (* ignoring the nesting structure of inlined subroutines for now *) type inlined_subroutine_data = inlined_subroutine list type inlined_subroutine_data_by_range_entry = (Nat_big_num.num*Nat_big_num.num)(*range*) * (Nat_big_num.num*Nat_big_num.num) (*range m-of-n*) * inlined_subroutine type inlined_subroutine_data_by_range = inlined_subroutine_data_by_range_entry list (*type inlined_subroutine_data_at_pc = list ((compilation_unit * (list die) * die) * (natural * natural * single_location_description * error single_location))*) (** ************************************************************ *) (** ** missing pervasives ************************************ *) (** ************************************************************ *) (* natural version of List.index *) (*val index_natural : forall 'a. list 'a -> natural -> maybe 'a*) let rec index_natural l n:'a option= ((match l with | [] -> None | x :: xs -> if Nat_big_num.equal n( (Nat_big_num.of_int 0)) then Some x else index_natural xs (Nat_big_num.sub_nat n( (Nat_big_num.of_int 1))) )) let partialNaturalFromInteger (i:Nat_big_num.num) : Nat_big_num.num= (if Nat_big_num.less i( (Nat_big_num.of_int 0)) then failwith "partialNaturalFromInteger" else Nat_big_num.abs i) (*val natural_nat_shift_left : natural -> nat -> natural*) (*val natural_nat_shift_right : natural -> nat -> natural*) (** ************************************************************ *) (** ** endianness *************************************** *) (** ************************************************************ *) let p_context_of_d (d:dwarf) : p_context= ({ endianness = (d.d_endianness) }) (** ************************************************************ *) (** ** dwarf encodings *************************************** *) (** ************************************************************ *) (* these encoding tables are pasted from the DWARF 4 specification *) (* tag encoding *) let tag_encodings:(string*Nat_big_num.num)list= ([ ("DW_TAG_array_type" , natural_of_hex "0x01" ); ("DW_TAG_class_type" , natural_of_hex "0x02" ); ("DW_TAG_entry_point" , natural_of_hex "0x03" ); ("DW_TAG_enumeration_type" , natural_of_hex "0x04" ); ("DW_TAG_formal_parameter" , natural_of_hex "0x05" ); ("DW_TAG_imported_declaration" , natural_of_hex "0x08" ); ("DW_TAG_label" , natural_of_hex "0x0a" ); ("DW_TAG_lexical_block" , natural_of_hex "0x0b" ); ("DW_TAG_member" , natural_of_hex "0x0d" ); ("DW_TAG_pointer_type" , natural_of_hex "0x0f" ); ("DW_TAG_reference_type" , natural_of_hex "0x10" ); ("DW_TAG_compile_unit" , natural_of_hex "0x11" ); ("DW_TAG_string_type" , natural_of_hex "0x12" ); ("DW_TAG_structure_type" , natural_of_hex "0x13" ); ("DW_TAG_subroutine_type" , natural_of_hex "0x15" ); ("DW_TAG_typedef" , natural_of_hex "0x16" ); ("DW_TAG_union_type" , natural_of_hex "0x17" ); ("DW_TAG_unspecified_parameters" , natural_of_hex "0x18" ); ("DW_TAG_variant" , natural_of_hex "0x19" ); ("DW_TAG_common_block" , natural_of_hex "0x1a" ); ("DW_TAG_common_inclusion" , natural_of_hex "0x1b" ); ("DW_TAG_inheritance" , natural_of_hex "0x1c" ); ("DW_TAG_inlined_subroutine" , natural_of_hex "0x1d" ); ("DW_TAG_module" , natural_of_hex "0x1e" ); ("DW_TAG_ptr_to_member_type" , natural_of_hex "0x1f" ); ("DW_TAG_set_type" , natural_of_hex "0x20" ); ("DW_TAG_subrange_type" , natural_of_hex "0x21" ); ("DW_TAG_with_stmt" , natural_of_hex "0x22" ); ("DW_TAG_access_declaration" , natural_of_hex "0x23" ); ("DW_TAG_base_type" , natural_of_hex "0x24" ); ("DW_TAG_catch_block" , natural_of_hex "0x25" ); ("DW_TAG_const_type" , natural_of_hex "0x26" ); ("DW_TAG_constant" , natural_of_hex "0x27" ); ("DW_TAG_enumerator" , natural_of_hex "0x28" ); ("DW_TAG_file_type" , natural_of_hex "0x29" ); ("DW_TAG_friend" , natural_of_hex "0x2a" ); ("DW_TAG_namelist" , natural_of_hex "0x2b" ); ("DW_TAG_namelist_item" , natural_of_hex "0x2c" ); ("DW_TAG_packed_type" , natural_of_hex "0x2d" ); ("DW_TAG_subprogram" , natural_of_hex "0x2e" ); ("DW_TAG_template_type_parameter" , natural_of_hex "0x2f" ); ("DW_TAG_template_value_parameter" , natural_of_hex "0x30" ); ("DW_TAG_thrown_type" , natural_of_hex "0x31" ); ("DW_TAG_try_block" , natural_of_hex "0x32" ); ("DW_TAG_variant_part" , natural_of_hex "0x33" ); ("DW_TAG_variable" , natural_of_hex "0x34" ); ("DW_TAG_volatile_type" , natural_of_hex "0x35" ); ("DW_TAG_dwarf_procedure" , natural_of_hex "0x36" ); ("DW_TAG_restrict_type" , natural_of_hex "0x37" ); ("DW_TAG_interface_type" , natural_of_hex "0x38" ); ("DW_TAG_namespace" , natural_of_hex "0x39" ); ("DW_TAG_imported_module" , natural_of_hex "0x3a" ); ("DW_TAG_unspecified_type" , natural_of_hex "0x3b" ); ("DW_TAG_partial_unit" , natural_of_hex "0x3c" ); ("DW_TAG_imported_unit" , natural_of_hex "0x3d" ); ("DW_TAG_condition" , natural_of_hex "0x3f" ); ("DW_TAG_shared_type" , natural_of_hex "0x40" ); ("DW_TAG_type_unit" , natural_of_hex "0x41" ); ("DW_TAG_rvalue_reference_type" , natural_of_hex "0x42" ); ("DW_TAG_template_alias" , natural_of_hex "0x43" ); ("DW_TAG_lo_user" , natural_of_hex "0x4080"); ("DW_TAG_hi_user" , natural_of_hex "0xffff") ]) (* child determination encoding *) let vDW_CHILDREN_no:Nat_big_num.num= (natural_of_hex "0x00") let vDW_CHILDREN_yes:Nat_big_num.num= (natural_of_hex "0x01") (* attribute encoding *) let attribute_encodings:(string*Nat_big_num.num*(dwarf_attribute_classes)list)list= ([ ("DW_AT_sibling" , natural_of_hex "0x01", [DWA_reference]) ; ("DW_AT_location" , natural_of_hex "0x02", [DWA_exprloc; DWA_loclistptr]) ; ("DW_AT_name" , natural_of_hex "0x03", [DWA_string]) ; ("DW_AT_ordering" , natural_of_hex "0x09", [DWA_constant]) ; ("DW_AT_byte_size" , natural_of_hex "0x0b", [DWA_constant; DWA_exprloc; DWA_reference]) ; ("DW_AT_bit_offset" , natural_of_hex "0x0c", [DWA_constant; DWA_exprloc; DWA_reference]) ; ("DW_AT_bit_size" , natural_of_hex "0x0d", [DWA_constant; DWA_exprloc; DWA_reference]) ; ("DW_AT_stmt_list" , natural_of_hex "0x10", [DWA_lineptr]) ; ("DW_AT_low_pc" , natural_of_hex "0x11", [DWA_address]) ; ("DW_AT_high_pc" , natural_of_hex "0x12", [DWA_address; DWA_constant]) ; ("DW_AT_language" , natural_of_hex "0x13", [DWA_constant]) ; ("DW_AT_discr" , natural_of_hex "0x15", [DWA_reference]) ; ("DW_AT_discr_value" , natural_of_hex "0x16", [DWA_constant]) ; ("DW_AT_visibility" , natural_of_hex "0x17", [DWA_constant]) ; ("DW_AT_import" , natural_of_hex "0x18", [DWA_reference]) ; ("DW_AT_string_length" , natural_of_hex "0x19", [DWA_exprloc; DWA_loclistptr]) ; ("DW_AT_common_reference" , natural_of_hex "0x1a", [DWA_reference]) ; ("DW_AT_comp_dir" , natural_of_hex "0x1b", [DWA_string]) ; ("DW_AT_const_value" , natural_of_hex "0x1c", [DWA_block; DWA_constant; DWA_string]) ; ("DW_AT_containing_type" , natural_of_hex "0x1d", [DWA_reference]) ; ("DW_AT_default_value" , natural_of_hex "0x1e", [DWA_reference]) ; ("DW_AT_inline" , natural_of_hex "0x20", [DWA_constant]) ; ("DW_AT_is_optional" , natural_of_hex "0x21", [DWA_flag]) ; ("DW_AT_lower_bound" , natural_of_hex "0x22", [DWA_constant; DWA_exprloc; DWA_reference]) ; ("DW_AT_producer" , natural_of_hex "0x25", [DWA_string]) ; ("DW_AT_prototyped" , natural_of_hex "0x27", [DWA_flag]) ; ("DW_AT_return_addr" , natural_of_hex "0x2a", [DWA_exprloc; DWA_loclistptr]) ; ("DW_AT_start_scope" , natural_of_hex "0x2c", [DWA_constant; DWA_rangelistptr]) ; ("DW_AT_bit_stride" , natural_of_hex "0x2e", [DWA_constant; DWA_exprloc; DWA_reference]) ; ("DW_AT_upper_bound" , natural_of_hex "0x2f", [DWA_constant; DWA_exprloc; DWA_reference]) ; ("DW_AT_abstract_origin" , natural_of_hex "0x31", [DWA_reference]) ; ("DW_AT_accessibility" , natural_of_hex "0x32", [DWA_constant]) ; ("DW_AT_address_class" , natural_of_hex "0x33", [DWA_constant]) ; ("DW_AT_artificial" , natural_of_hex "0x34", [DWA_flag]) ; ("DW_AT_base_types" , natural_of_hex "0x35", [DWA_reference]) ; ("DW_AT_calling_convention" , natural_of_hex "0x36", [DWA_constant]) ; ("DW_AT_count" , natural_of_hex "0x37", [DWA_constant; DWA_exprloc; DWA_reference]) ; ("DW_AT_data_member_location" , natural_of_hex "0x38", [DWA_constant; DWA_exprloc; DWA_loclistptr]) ; ("DW_AT_decl_column" , natural_of_hex "0x39", [DWA_constant]) ; ("DW_AT_decl_file" , natural_of_hex "0x3a", [DWA_constant]) ; ("DW_AT_decl_line" , natural_of_hex "0x3b", [DWA_constant]) ; ("DW_AT_declaration" , natural_of_hex "0x3c", [DWA_flag]) ; ("DW_AT_discr_list" , natural_of_hex "0x3d", [DWA_block]) ; ("DW_AT_encoding" , natural_of_hex "0x3e", [DWA_constant]) ; ("DW_AT_external" , natural_of_hex "0x3f", [DWA_flag]) ; ("DW_AT_frame_base" , natural_of_hex "0x40", [DWA_exprloc; DWA_loclistptr]) ; ("DW_AT_friend" , natural_of_hex "0x41", [DWA_reference]) ; ("DW_AT_identifier_case" , natural_of_hex "0x42", [DWA_constant]) ; ("DW_AT_macro_info" , natural_of_hex "0x43", [DWA_macptr]) ; ("DW_AT_namelist_item" , natural_of_hex "0x44", [DWA_reference]) ; ("DW_AT_priority" , natural_of_hex "0x45", [DWA_reference]) ; ("DW_AT_segment" , natural_of_hex "0x46", [DWA_exprloc; DWA_loclistptr]) ; ("DW_AT_specification" , natural_of_hex "0x47", [DWA_reference]) ; ("DW_AT_static_link" , natural_of_hex "0x48", [DWA_exprloc; DWA_loclistptr]) ; ("DW_AT_type" , natural_of_hex "0x49", [DWA_reference]) ; ("DW_AT_use_location" , natural_of_hex "0x4a", [DWA_exprloc; DWA_loclistptr]) ; ("DW_AT_variable_parameter" , natural_of_hex "0x4b", [DWA_flag]) ; ("DW_AT_virtuality" , natural_of_hex "0x4c", [DWA_constant]) ; ("DW_AT_vtable_elem_location" , natural_of_hex "0x4d", [DWA_exprloc; DWA_loclistptr]) ; ("DW_AT_allocated" , natural_of_hex "0x4e", [DWA_constant; DWA_exprloc; DWA_reference]) ; ("DW_AT_associated" , natural_of_hex "0x4f", [DWA_constant; DWA_exprloc; DWA_reference]) ; ("DW_AT_data_location" , natural_of_hex "0x50", [DWA_exprloc]) ; ("DW_AT_byte_stride" , natural_of_hex "0x51", [DWA_constant; DWA_exprloc; DWA_reference]) ; ("DW_AT_entry_pc" , natural_of_hex "0x52", [DWA_address]) ; ("DW_AT_use_UTF8" , natural_of_hex "0x53", [DWA_flag]) ; ("DW_AT_extension" , natural_of_hex "0x54", [DWA_reference]) ; ("DW_AT_ranges" , natural_of_hex "0x55", [DWA_rangelistptr]) ; ("DW_AT_trampoline" , natural_of_hex "0x56", [DWA_address; DWA_flag; DWA_reference; DWA_string]); ("DW_AT_call_column" , natural_of_hex "0x57", [DWA_constant]) ; ("DW_AT_call_file" , natural_of_hex "0x58", [DWA_constant]) ; ("DW_AT_call_line" , natural_of_hex "0x59", [DWA_constant]) ; ("DW_AT_description" , natural_of_hex "0x5a", [DWA_string]) ; ("DW_AT_binary_scale" , natural_of_hex "0x5b", [DWA_constant]) ; ("DW_AT_decimal_scale" , natural_of_hex "0x5c", [DWA_constant]) ; ("DW_AT_small" , natural_of_hex "0x5d", [DWA_reference]) ; ("DW_AT_decimal_sign" , natural_of_hex "0x5e", [DWA_constant]) ; ("DW_AT_digit_count" , natural_of_hex "0x5f", [DWA_constant]) ; ("DW_AT_picture_string" , natural_of_hex "0x60", [DWA_string]) ; ("DW_AT_mutable" , natural_of_hex "0x61", [DWA_flag]) ; ("DW_AT_threads_scaled" , natural_of_hex "0x62", [DWA_flag]) ; ("DW_AT_explicit" , natural_of_hex "0x63", [DWA_flag]) ; ("DW_AT_object_pointer" , natural_of_hex "0x64", [DWA_reference]) ; ("DW_AT_endianity" , natural_of_hex "0x65", [DWA_constant]) ; ("DW_AT_elemental" , natural_of_hex "0x66", [DWA_flag]) ; ("DW_AT_pure" , natural_of_hex "0x67", [DWA_flag]) ; ("DW_AT_recursive" , natural_of_hex "0x68", [DWA_flag]) ; ("DW_AT_signature" , natural_of_hex "0x69", [DWA_reference]) ; ("DW_AT_main_subprogram" , natural_of_hex "0x6a", [DWA_flag]) ; ("DW_AT_data_bit_offset" , natural_of_hex "0x6b", [DWA_constant]) ; ("DW_AT_const_expr" , natural_of_hex "0x6c", [DWA_flag]) ; ("DW_AT_enum_class" , natural_of_hex "0x6d", [DWA_flag]) ; ("DW_AT_linkage_name" , natural_of_hex "0x6e", [DWA_string]) ; (* DW_AT_noreturn is a gcc extension to support the C11 _Noreturn keyword*) ("DW_AT_noreturn" , natural_of_hex "0x87", [DWA_flag]) ; ("DW_AT_alignment" , natural_of_hex "0x88", [DWA_constant]) ; ("DW_AT_lo_user" , natural_of_hex "0x2000", [DWA_dash]) ; ("DW_AT_hi_user" , natural_of_hex "0x3fff", [DWA_dash]) ]) (* attribute form encoding *) let attribute_form_encodings:(string*Nat_big_num.num*(dwarf_attribute_classes)list)list= ([ ("DW_FORM_addr" , natural_of_hex "0x01", [DWA_address]) ; ("DW_FORM_block2" , natural_of_hex "0x03", [DWA_block]) ; ("DW_FORM_block4" , natural_of_hex "0x04", [DWA_block]) ; ("DW_FORM_data2" , natural_of_hex "0x05", [DWA_constant]) ; ("DW_FORM_data4" , natural_of_hex "0x06", [DWA_constant]) ; ("DW_FORM_data8" , natural_of_hex "0x07", [DWA_constant]) ; ("DW_FORM_string" , natural_of_hex "0x08", [DWA_string]) ; ("DW_FORM_block" , natural_of_hex "0x09", [DWA_block]) ; ("DW_FORM_block1" , natural_of_hex "0x0a", [DWA_block]) ; ("DW_FORM_data1" , natural_of_hex "0x0b", [DWA_constant]) ; ("DW_FORM_flag" , natural_of_hex "0x0c", [DWA_flag]) ; ("DW_FORM_sdata" , natural_of_hex "0x0d", [DWA_constant]) ; ("DW_FORM_strp" , natural_of_hex "0x0e", [DWA_string]) ; ("DW_FORM_udata" , natural_of_hex "0x0f", [DWA_constant]) ; ("DW_FORM_ref_addr" , natural_of_hex "0x10", [DWA_reference]); ("DW_FORM_ref1" , natural_of_hex "0x11", [DWA_reference]); ("DW_FORM_ref2" , natural_of_hex "0x12", [DWA_reference]); ("DW_FORM_ref4" , natural_of_hex "0x13", [DWA_reference]); ("DW_FORM_ref8" , natural_of_hex "0x14", [DWA_reference]); ("DW_FORM_ref_udata" , natural_of_hex "0x15", [DWA_reference]); ("DW_FORM_indirect" , natural_of_hex "0x16", [DWA_7_5_3]) ; ("DW_FORM_sec_offset" , natural_of_hex "0x17", [DWA_lineptr; DWA_loclistptr; DWA_macptr; DWA_rangelistptr]) ; ("DW_FORM_exprloc" , natural_of_hex "0x18", [DWA_exprloc]) ; ("DW_FORM_flag_present", natural_of_hex "0x19", [DWA_flag]) ; ("DW_FORM_ref_sig8" , natural_of_hex "0x20", [DWA_reference]) ]) (* operation encoding *) let operation_encodings:(string*Nat_big_num.num*(operation_argument_type)list*operation_semantics)list= ([ ("DW_OP_addr", natural_of_hex "0x03", [OAT_addr] , OpSem_lit); (*1*) (*constant address (size target specific)*) ("DW_OP_deref", natural_of_hex "0x06", [] , OpSem_deref); (*0*) ("DW_OP_const1u", natural_of_hex "0x08", [OAT_uint8] , OpSem_lit); (*1*) (* 1-byte constant *) ("DW_OP_const1s", natural_of_hex "0x09", [OAT_sint8] , OpSem_lit); (*1*) (* 1-byte constant *) ("DW_OP_const2u", natural_of_hex "0x0a", [OAT_uint16] , OpSem_lit); (*1*) (* 2-byte constant *) ("DW_OP_const2s", natural_of_hex "0x0b", [OAT_sint16] , OpSem_lit); (*1*) (* 2-byte constant *) ("DW_OP_const4u", natural_of_hex "0x0c", [OAT_uint32] , OpSem_lit); (*1*) (* 4-byte constant *) ("DW_OP_const4s", natural_of_hex "0x0d", [OAT_sint32] , OpSem_lit); (*1*) (* 4-byte constant *) ("DW_OP_const8u", natural_of_hex "0x0e", [OAT_uint64] , OpSem_lit); (*1*) (* 8-byte constant *) ("DW_OP_const8s", natural_of_hex "0x0f", [OAT_sint64] , OpSem_lit); (*1*) (* 8-byte constant *) ("DW_OP_constu", natural_of_hex "0x10", [OAT_ULEB128] , OpSem_lit); (*1*) (* ULEB128 constant *) ("DW_OP_consts", natural_of_hex "0x11", [OAT_SLEB128] , OpSem_lit); (*1*) (* SLEB128 constant *) ("DW_OP_dup", natural_of_hex "0x12", [] , OpSem_stack (fun ac vs args -> (match vs with v::vs -> Some (v::(v::vs)) | _ -> None ))); (*0*) ("DW_OP_drop", natural_of_hex "0x13", [] , OpSem_stack (fun ac vs args -> (match vs with v::vs -> Some vs | _ -> None ))); (*0*) ("DW_OP_over", natural_of_hex "0x14", [] , OpSem_stack (fun ac vs args -> (match vs with v::v'::vs -> Some (v'::(v::(v'::vs))) | _ -> None ))); (*0*) ("DW_OP_pick", natural_of_hex "0x15", [OAT_uint8] , OpSem_stack (fun ac vs args -> (match args with [OAV_natural n] -> (match index_natural vs n with Some v -> Some (v::vs) | None -> None ) | _ -> None ))); (*1*) (* 1-byte stack index *) ("DW_OP_swap", natural_of_hex "0x16", [] , OpSem_stack (fun ac vs args -> (match vs with v::v'::vs -> Some (v'::(v::vs)) | _ -> None ))); (*0*) ("DW_OP_rot", natural_of_hex "0x17", [] , OpSem_stack (fun ac vs args -> (match vs with v::v'::v''::vs -> Some (v'::(v''::(v::vs))) | _ -> None ))); (*0*) ("DW_OP_xderef", natural_of_hex "0x18", [] , OpSem_not_supported); (*0*) ("DW_OP_abs", natural_of_hex "0x19", [] , OpSem_unary (fun ac v -> if Nat_big_num.less v ac.ac_half then Some v else if Nat_big_num.equal v ac.ac_max then None else Some (Nat_big_num.sub_nat ac.ac_all v))); (*0*) ("DW_OP_and", natural_of_hex "0x1a", [] , OpSem_binary (fun ac v1 v2 -> Some (Nat_big_num.bitwise_and v1 v2))); (*0*) ("DW_OP_div", natural_of_hex "0x1b", [] , OpSem_not_supported) (*TODO*); (*0*) ("DW_OP_minus", natural_of_hex "0x1c", [] , OpSem_binary (fun ac v1 v2 -> Some (partialNaturalFromInteger ( Nat_big_num.modulus( Nat_big_num.sub( v1) ( v2)) ( ac.ac_all))))); (*0*) ("DW_OP_mod", natural_of_hex "0x1d", [] , OpSem_binary (fun ac v1 v2 -> Some ( Nat_big_num.modulus v1 v2))); (*0*) ("DW_OP_mul", natural_of_hex "0x1e", [] , OpSem_binary (fun ac v1 v2 -> Some (partialNaturalFromInteger ( Nat_big_num.modulus( Nat_big_num.mul( v1) ( v2)) ( ac.ac_all))))); (*0*) ("DW_OP_neg", natural_of_hex "0x1f", [] , OpSem_unary (fun ac v -> if Nat_big_num.less v ac.ac_half then Some ( Nat_big_num.sub_nat ac.ac_max v) else if Nat_big_num.equal v ac.ac_half then None else Some ( Nat_big_num.sub_nat ac.ac_all v))); (*0*) ("DW_OP_not", natural_of_hex "0x20", [] , OpSem_unary (fun ac v -> Some (Nat_big_num.bitwise_xor v ac.ac_max))); (*0*) ("DW_OP_or", natural_of_hex "0x21", [] , OpSem_binary (fun ac v1 v2 -> Some (Nat_big_num.bitwise_or v1 v2))); (*0*) ("DW_OP_plus", natural_of_hex "0x22", [] , OpSem_binary (fun ac v1 v2 -> Some ( Nat_big_num.modulus( Nat_big_num.add v1 v2) ac.ac_all))); (*0*) ("DW_OP_plus_uconst", natural_of_hex "0x23", [OAT_ULEB128] , OpSem_stack (fun ac vs args -> (match args with [OAV_natural n] -> (match vs with v::vs' -> let v' = (Nat_big_num.modulus (Nat_big_num.add v n) ac.ac_all) in Some (v'::vs) | [] -> None ) | _ -> None ))); (*1*) (* ULEB128 addend *) ("DW_OP_shl", natural_of_hex "0x24", [] , OpSem_binary (fun ac v1 v2 -> if Nat_big_num.greater_equal v2 ac.ac_bitwidth then Some( (Nat_big_num.of_int 0)) else Some (Nat_big_num.shift_left v1 (Nat_big_num.to_int v2)))); (*0*) ("DW_OP_shr", natural_of_hex "0x25", [] , OpSem_binary (fun ac v1 v2 -> if Nat_big_num.greater_equal v2 ac.ac_bitwidth then Some( (Nat_big_num.of_int 0)) else Some (Nat_big_num.shift_right v1 (Nat_big_num.to_int v2)))); (*0*) ("DW_OP_shra", natural_of_hex "0x26", [] , OpSem_binary (fun ac v1 v2 -> if Nat_big_num.less v1 ac.ac_half then (if Nat_big_num.greater_equal v2 ac.ac_bitwidth then Some( (Nat_big_num.of_int 0)) else Some (Nat_big_num.shift_right v1 (Nat_big_num.to_int v2))) else (if Nat_big_num.greater_equal v2 ac.ac_bitwidth then Some ac.ac_max else Some ( Nat_big_num.sub_nat ac.ac_max (Nat_big_num.shift_right ( Nat_big_num.sub_nat ac.ac_max v1) (Nat_big_num.to_int v2)))))); (*0*) ("DW_OP_xor", natural_of_hex "0x27", [] , OpSem_binary (fun ac v1 v2 -> Some (Nat_big_num.bitwise_xor v1 v2))); (*0*) ("DW_OP_skip", natural_of_hex "0x2f", [OAT_sint16] , OpSem_not_supported); (*1*) (* signed 2-byte constant *) ("DW_OP_bra", natural_of_hex "0x28", [OAT_sint16] , OpSem_not_supported); (*1*) (* signed 2-byte constant *) ("DW_OP_eq", natural_of_hex "0x29", [] , OpSem_not_supported); (*0*) ("DW_OP_ge", natural_of_hex "0x2a", [] , OpSem_not_supported); (*0*) ("DW_OP_gt", natural_of_hex "0x2b", [] , OpSem_not_supported); (*0*) ("DW_OP_le", natural_of_hex "0x2c", [] , OpSem_not_supported); (*0*) ("DW_OP_lt", natural_of_hex "0x2d", [] , OpSem_not_supported); (*0*) ("DW_OP_ne", natural_of_hex "0x2e", [] , OpSem_not_supported); (*0*) ("DW_OP_lit0", natural_of_hex "0x30", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) (* literals 0..31 =(DW_OP_lit0 + literal) *) ("DW_OP_lit1", natural_of_hex "0x31", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit2", natural_of_hex "0x32", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit3", natural_of_hex "0x33", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit4", natural_of_hex "0x34", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit5", natural_of_hex "0x35", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit6", natural_of_hex "0x36", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit7", natural_of_hex "0x37", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit8", natural_of_hex "0x38", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit9", natural_of_hex "0x39", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit10", natural_of_hex "0x3a", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit11", natural_of_hex "0x3b", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit12", natural_of_hex "0x3c", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit13", natural_of_hex "0x3d", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit14", natural_of_hex "0x3e", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit15", natural_of_hex "0x3f", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit16", natural_of_hex "0x40", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit17", natural_of_hex "0x41", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit18", natural_of_hex "0x42", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit19", natural_of_hex "0x43", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit20", natural_of_hex "0x44", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit21", natural_of_hex "0x45", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit22", natural_of_hex "0x46", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit23", natural_of_hex "0x47", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit24", natural_of_hex "0x48", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit25", natural_of_hex "0x49", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit26", natural_of_hex "0x4a", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit27", natural_of_hex "0x4b", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit28", natural_of_hex "0x4c", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit29", natural_of_hex "0x4d", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit30", natural_of_hex "0x4e", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_lit31", natural_of_hex "0x4f", [] , OpSem_opcode_lit (natural_of_hex "0x30")); (*0*) ("DW_OP_reg0", natural_of_hex "0x50", [] , OpSem_reg); (*1*) (* reg 0..31 = (DW_OP_reg0 + regnum) *) ("DW_OP_reg1", natural_of_hex "0x51", [] , OpSem_reg); (*1*) ("DW_OP_reg2", natural_of_hex "0x52", [] , OpSem_reg); (*1*) ("DW_OP_reg3", natural_of_hex "0x53", [] , OpSem_reg); (*1*) ("DW_OP_reg4", natural_of_hex "0x54", [] , OpSem_reg); (*1*) ("DW_OP_reg5", natural_of_hex "0x55", [] , OpSem_reg); (*1*) ("DW_OP_reg6", natural_of_hex "0x56", [] , OpSem_reg); (*1*) ("DW_OP_reg7", natural_of_hex "0x57", [] , OpSem_reg); (*1*) ("DW_OP_reg8", natural_of_hex "0x58", [] , OpSem_reg); (*1*) ("DW_OP_reg9", natural_of_hex "0x59", [] , OpSem_reg); (*1*) ("DW_OP_reg10", natural_of_hex "0x5a", [] , OpSem_reg); (*1*) ("DW_OP_reg11", natural_of_hex "0x5b", [] , OpSem_reg); (*1*) ("DW_OP_reg12", natural_of_hex "0x5c", [] , OpSem_reg); (*1*) ("DW_OP_reg13", natural_of_hex "0x5d", [] , OpSem_reg); (*1*) ("DW_OP_reg14", natural_of_hex "0x5e", [] , OpSem_reg); (*1*) ("DW_OP_reg15", natural_of_hex "0x5f", [] , OpSem_reg); (*1*) ("DW_OP_reg16", natural_of_hex "0x60", [] , OpSem_reg); (*1*) ("DW_OP_reg17", natural_of_hex "0x61", [] , OpSem_reg); (*1*) ("DW_OP_reg18", natural_of_hex "0x62", [] , OpSem_reg); (*1*) ("DW_OP_reg19", natural_of_hex "0x63", [] , OpSem_reg); (*1*) ("DW_OP_reg20", natural_of_hex "0x64", [] , OpSem_reg); (*1*) ("DW_OP_reg21", natural_of_hex "0x65", [] , OpSem_reg); (*1*) ("DW_OP_reg22", natural_of_hex "0x66", [] , OpSem_reg); (*1*) ("DW_OP_reg23", natural_of_hex "0x67", [] , OpSem_reg); (*1*) ("DW_OP_reg24", natural_of_hex "0x68", [] , OpSem_reg); (*1*) ("DW_OP_reg25", natural_of_hex "0x69", [] , OpSem_reg); (*1*) ("DW_OP_reg26", natural_of_hex "0x6a", [] , OpSem_reg); (*1*) ("DW_OP_reg27", natural_of_hex "0x6b", [] , OpSem_reg); (*1*) ("DW_OP_reg28", natural_of_hex "0x6c", [] , OpSem_reg); (*1*) ("DW_OP_reg29", natural_of_hex "0x6d", [] , OpSem_reg); (*1*) ("DW_OP_reg30", natural_of_hex "0x6e", [] , OpSem_reg); (*1*) ("DW_OP_reg31", natural_of_hex "0x6f", [] , OpSem_reg); (*1*) ("DW_OP_breg0", natural_of_hex "0x70", [OAT_SLEB128] , OpSem_breg); (*1*) (* base register 0..31 = (DW_OP_breg0 + regnum) *) ("DW_OP_breg1", natural_of_hex "0x71", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg2", natural_of_hex "0x72", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg3", natural_of_hex "0x73", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg4", natural_of_hex "0x74", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg5", natural_of_hex "0x75", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg6", natural_of_hex "0x76", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg7", natural_of_hex "0x77", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg8", natural_of_hex "0x78", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg9", natural_of_hex "0x79", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg10", natural_of_hex "0x7a", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg11", natural_of_hex "0x7b", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg12", natural_of_hex "0x7c", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg13", natural_of_hex "0x7d", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg14", natural_of_hex "0x7e", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg15", natural_of_hex "0x7f", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg16", natural_of_hex "0x80", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg17", natural_of_hex "0x81", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg18", natural_of_hex "0x82", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg19", natural_of_hex "0x83", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg20", natural_of_hex "0x84", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg21", natural_of_hex "0x85", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg22", natural_of_hex "0x86", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg23", natural_of_hex "0x87", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg24", natural_of_hex "0x88", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg25", natural_of_hex "0x89", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg26", natural_of_hex "0x8a", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg27", natural_of_hex "0x8b", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg28", natural_of_hex "0x8c", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg29", natural_of_hex "0x8d", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg30", natural_of_hex "0x8e", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_breg31", natural_of_hex "0x8f", [OAT_SLEB128] , OpSem_breg); (*1*) ("DW_OP_regx", natural_of_hex "0x90", [OAT_ULEB128] , OpSem_lit); (*1*) (* ULEB128 register *) ("DW_OP_fbreg", natural_of_hex "0x91", [OAT_SLEB128] , OpSem_fbreg); (*1*) (* SLEB128 offset *) ("DW_OP_bregx", natural_of_hex "0x92", [OAT_ULEB128; OAT_SLEB128] , OpSem_bregx); (*2*) (* ULEB128 register followed by SLEB128 offset *) ("DW_OP_piece", natural_of_hex "0x93", [OAT_ULEB128] , OpSem_piece); (*1*) (* ULEB128 size of piece addressed *) ("DW_OP_deref_size", natural_of_hex "0x94", [OAT_uint8] , OpSem_deref_size); (*1*) (* 1-byte size of data retrieved *) ("DW_OP_xderef_size", natural_of_hex "0x95", [OAT_uint8] , OpSem_not_supported); (*1*) (* 1-byte size of data retrieved *) ("DW_OP_nop", natural_of_hex "0x96", [] , OpSem_nop); (*0*) ("DW_OP_push_object_address", natural_of_hex "0x97", [] , OpSem_not_supported); (*0*) ("DW_OP_call2", natural_of_hex "0x98", [OAT_uint16] , OpSem_not_supported); (*1*) (* 2-byte offset of DIE *) ("DW_OP_call4", natural_of_hex "0x99", [OAT_uint32] , OpSem_not_supported); (*1*) (* 4-byte offset of DIE *) ("DW_OP_call_ref", natural_of_hex "0x9a", [OAT_dwarf_format_t] , OpSem_not_supported); (*1*) (* 4- or 8-byte offset of DIE *) ("DW_OP_form_tls_address", natural_of_hex "0x9b", [] , OpSem_not_supported); (*0*) ("DW_OP_call_frame_cfa", natural_of_hex "0x9c", [] , OpSem_call_frame_cfa); (*0*) ("DW_OP_bit_piece", natural_of_hex "0x9d", [OAT_ULEB128; OAT_ULEB128] , OpSem_bit_piece); (*2*) (* ULEB128 size followed by ULEB128 offset *) ("DW_OP_implicit_value", natural_of_hex "0x9e", [OAT_block] , OpSem_implicit_value); (*2*) (* ULEB128 size followed by block of that size *) ("DW_OP_stack_value", natural_of_hex "0x9f", [] , OpSem_stack_value); (*0*) (* these aren't real operations ("DW_OP_lo_user", natural_of_hex "0xe0", [] , ); ("DW_OP_hi_user", natural_of_hex "0xff", [] , ); *) (* GCC also produces these for our example: https://fedorahosted.org/elfutils/wiki/DwarfExtensions http://dwarfstd.org/ShowIssue.php?issue=100909.1 *) ("DW_GNU_OP_entry_value", natural_of_hex "0xf3", [OAT_block], OpSem_not_supported); (*2*) (* ULEB128 size followed by DWARF expression block of that size*) ("DW_OP_GNU_implicit_pointer", natural_of_hex "0xf2", [OAT_dwarf_format_t;OAT_SLEB128], OpSem_not_supported) ]) let vDW_OP_reg0:Nat_big_num.num= (natural_of_hex "0x50") let vDW_OP_breg0:Nat_big_num.num= (natural_of_hex "0x70") (* call frame instruction encoding *) let call_frame_instruction_encoding : (string * Nat_big_num.num * Nat_big_num.num * call_frame_argument_type list * (( call_frame_argument_value list) -> call_frame_instruction option)) list= ([ (* high-order 2 bits low-order 6 bits uniformly parsed arguments *) (* instructions using low-order 6 bits for first argument *) (* ("DW_CFA_advance_loc", 1, 0,(*delta *) []); ("DW_CFA_offset", 2, 0,(*register*) [CFAT_offset]); ("DW_CFA_restore", 3, 0,(*register*) []); *) (* instructions using low-order 6 bits as part of opcode *) ("DW_CFA_nop", (Nat_big_num.of_int 0), natural_of_hex "0x00", [], ( (* *)fun avs -> (match avs with [] -> Some (DW_CFA_nop) | _ -> None ))); ("DW_CFA_set_loc", (Nat_big_num.of_int 0), natural_of_hex "0x01", [CFAT_address], ( (* address *)fun avs -> (match avs with [CFAV_address a] -> Some (DW_CFA_set_loc a) | _ -> None ))); ("DW_CFA_advance_loc1", (Nat_big_num.of_int 0), natural_of_hex "0x02", [CFAT_delta1], ( (* 1-byte delta *)fun avs -> (match avs with [CFAV_delta d] -> Some (DW_CFA_advance_loc1 d) | _ -> None ))); ("DW_CFA_advance_loc2", (Nat_big_num.of_int 0), natural_of_hex "0x03", [CFAT_delta2], ( (* 2-byte delta *)fun avs -> (match avs with [CFAV_delta d] -> Some (DW_CFA_advance_loc2 d) | _ -> None ))); ("DW_CFA_advance_loc4", (Nat_big_num.of_int 0), natural_of_hex "0x04", [CFAT_delta4], ( (* 4-byte delta *)fun avs -> (match avs with [CFAV_delta d] -> Some (DW_CFA_advance_loc4 d) | _ -> None ))); ("DW_CFA_offset_extended", (Nat_big_num.of_int 0), natural_of_hex "0x05", [CFAT_register; CFAT_offset], ( (* ULEB128 register ULEB128 offset *)fun avs -> (match avs with [CFAV_register r; CFAV_offset n] -> Some (DW_CFA_offset_extended( r, n)) | _ -> None ))); ("DW_CFA_restore_extended", (Nat_big_num.of_int 0), natural_of_hex "0x06", [CFAT_register], ( (* ULEB128 register *)fun avs -> (match avs with [CFAV_register r] -> Some (DW_CFA_restore_extended r) | _ -> None ))); ("DW_CFA_undefined", (Nat_big_num.of_int 0), natural_of_hex "0x07", [CFAT_register], ( (* ULEB128 register *)fun avs -> (match avs with [CFAV_register r] -> Some (DW_CFA_undefined r) | _ -> None ))); ("DW_CFA_same_value", (Nat_big_num.of_int 0), natural_of_hex "0x08", [CFAT_register], ( (* ULEB128 register *)fun avs -> (match avs with [CFAV_register r] -> Some (DW_CFA_same_value r) | _ -> None ))); ("DW_CFA_register", (Nat_big_num.of_int 0), natural_of_hex "0x09", [CFAT_register; CFAT_register], ( (* ULEB128 register ULEB128 register *)fun avs -> (match avs with [CFAV_register r1; CFAV_register r2] -> Some (DW_CFA_register( r1, r2)) | _ -> None ))); ("DW_CFA_remember_state", (Nat_big_num.of_int 0), natural_of_hex "0x0a", [], ( (* *)fun avs -> (match avs with [] -> Some (DW_CFA_remember_state) | _ -> None ))); ("DW_CFA_restore_state", (Nat_big_num.of_int 0), natural_of_hex "0x0b", [], ( (* *)fun avs -> (match avs with [] -> Some (DW_CFA_restore_state) | _ -> None ))); ("DW_CFA_def_cfa", (Nat_big_num.of_int 0), natural_of_hex "0x0c", [CFAT_register; CFAT_offset], ( (* ULEB128 register ULEB128 offset *)fun avs -> (match avs with [CFAV_register r; CFAV_offset n] -> Some (DW_CFA_def_cfa( r, n)) | _ -> None ))); ("DW_CFA_def_cfa_register", (Nat_big_num.of_int 0), natural_of_hex "0x0d", [CFAT_register], ( (* ULEB128 register *)fun avs -> (match avs with [CFAV_register r] -> Some (DW_CFA_def_cfa_register r) | _ -> None ))); ("DW_CFA_def_cfa_offset", (Nat_big_num.of_int 0), natural_of_hex "0x0e", [CFAT_offset], ( (* ULEB128 offset *)fun avs -> (match avs with [CFAV_offset n] -> Some (DW_CFA_def_cfa_offset n) | _ -> None ))); ("DW_CFA_def_cfa_expression", (Nat_big_num.of_int 0), natural_of_hex "0x0f", [CFAT_block], ( (* BLOCK *)fun avs -> (match avs with [CFAV_block b] -> Some (DW_CFA_def_cfa_expression b) | _ -> None ))); ("DW_CFA_expression", (Nat_big_num.of_int 0), natural_of_hex "0x10", [CFAT_register; CFAT_block], ( (* ULEB128 register BLOCK *)fun avs -> (match avs with [CFAV_register r; CFAV_block b] -> Some (DW_CFA_expression( r, b)) | _ -> None ))); ("DW_CFA_offset_extended_sf", (Nat_big_num.of_int 0), natural_of_hex "0x11", [CFAT_register; CFAT_sfoffset], ( (* ULEB128 register SLEB128 offset *)fun avs -> (match avs with [CFAV_register r; CFAV_sfoffset i] -> Some (DW_CFA_offset_extended_sf( r, i)) | _ -> None ))); ("DW_CFA_def_cfa_sf", (Nat_big_num.of_int 0), natural_of_hex "0x12", [CFAT_register; CFAT_sfoffset], ( (* ULEB128 register SLEB128 offset *)fun avs -> (match avs with [CFAV_register r; CFAV_sfoffset i] -> Some (DW_CFA_def_cfa_sf( r, i)) | _ -> None ))); ("DW_CFA_def_cfa_offset_sf", (Nat_big_num.of_int 0), natural_of_hex "0x13", [CFAT_sfoffset], ( (* SLEB128 offset *)fun avs -> (match avs with [CFAV_sfoffset i] -> Some (DW_CFA_def_cfa_offset_sf i) | _ -> None ))); ("DW_CFA_val_offset", (Nat_big_num.of_int 0), natural_of_hex "0x14", [CFAT_register; CFAT_offset], ( (* ULEB128 ULEB128 *)fun avs -> (match avs with [CFAV_register r; CFAV_offset n] -> Some (DW_CFA_val_offset( r, n)) | _ -> None ))); ("DW_CFA_val_offset_sf", (Nat_big_num.of_int 0), natural_of_hex "0x15", [CFAT_register; CFAT_sfoffset], ( (* ULEB128 SLEB128 *)fun avs -> (match avs with [CFAV_register r; CFAV_sfoffset i] -> Some (DW_CFA_val_offset_sf( r, i)) | _ -> None ))); ("DW_CFA_val_expression", (Nat_big_num.of_int 0), natural_of_hex "0x16", [CFAT_register; CFAT_block], ( (* ULEB128 BLOCK *)fun avs -> (match avs with [CFAV_register r; CFAV_block b] -> Some (DW_CFA_val_expression( r, b)) | _ -> None ))); ("DW_CFA_AARCH64_negate_ra_state", (Nat_big_num.of_int 0), natural_of_hex "0x2d", [], ( (* *)fun avs -> (match avs with [] -> Some (DW_CFA_AARCH64_negate_ra_state) | _ -> None ))); ]) (* 0x2d DW_CFA_GNU_window_save is listed in https://sourceware.org/elfutils/DwarfExtensions as "magic shorthand used only by SPARC" https://elixir.bootlin.com/linux/v4.0/source/arch/arc/kernel/unwind.c#L842 no-ops it https://refspecs.linuxbase.org/LSB_3.0.0/LSB-PDA/LSB-PDA/dwarfext.html doesn't mention it https://github.com/gcc-mirror/gcc/blob/master/libgcc/unwind-dw2.c#L1189 says "This CFA is multiplexed with Sparc. On AArch64 it's used to toggle return address signing status." fs->regs.reg[DWARF_REGNUM_AARCH64_RA_STATE].loc.offset ^= 1; https://developer.arm.com/docs/ihi0057/c/dwarf-for-the-arm-64-bit-architecture-aarch64-abi-2018q4 "DWARF for the Arm® 64-bit Architecture (AArch64) - ABI 2018Q4" calls this "DW_CFA_AARCH64_negate_ra_state" "The DW_CFA_AARCH64_negate_ra_state operation negates bit[0] of the RA_SIGN_STATE pseudo-register. It does not take any operands." p10 says "The RA_SIGN_STATE pseudo-register records whether the return address has been signed with aPAC. This information can be used when unwinding. It is an unsigned integer with the same sizeas a general register. Only bit[0] is meaningful and is initialized to zero. A value of 0 indicates the return address has not been signed. A value of 1 indicates the return address has been signed" For our purposes it seems fine to nop-this. *) (* ("DW_CFA_lo_user", 0, natural_of_hex "0x1c", []); (* *) ("DW_CFA_hi_user", 0, natural_of_hex "0x3f", []); (* *) *) (* line number encodings *) let line_number_standard_encodings:(string*Nat_big_num.num*(line_number_argument_type)list*((line_number_argument_value)list ->(line_number_operation)option))list= ([ ("DW_LNS_copy" , natural_of_hex "0x01", [ ], (fun lnvs -> (match lnvs with [] -> Some DW_LNS_copy | _ -> None ))); ("DW_LNS_advance_pc" , natural_of_hex "0x02", [LNAT_ULEB128 ], (fun lnvs -> (match lnvs with [LNAV_ULEB128 n] -> Some (DW_LNS_advance_pc n) | _ -> None ))); ("DW_LNS_advance_line" , natural_of_hex "0x03", [LNAT_SLEB128 ], (fun lnvs -> (match lnvs with [LNAV_SLEB128 i] -> Some (DW_LNS_advance_line i) | _ -> None ))); ("DW_LNS_set_file" , natural_of_hex "0x04", [LNAT_ULEB128 ], (fun lnvs -> (match lnvs with [LNAV_ULEB128 n] -> Some (DW_LNS_set_file n) | _ -> None ))); ("DW_LNS_set_column" , natural_of_hex "0x05", [LNAT_ULEB128 ], (fun lnvs -> (match lnvs with [LNAV_ULEB128 n] -> Some (DW_LNS_set_column n) | _ -> None ))); ("DW_LNS_negate_stmt" , natural_of_hex "0x06", [ ], (fun lnvs -> (match lnvs with [] -> Some (DW_LNS_negate_stmt) | _ -> None ))); ("DW_LNS_set_basic_block" , natural_of_hex "0x07", [ ], (fun lnvs -> (match lnvs with [] -> Some (DW_LNS_set_basic_block) | _ -> None ))); ("DW_LNS_const_add_pc" , natural_of_hex "0x08", [ ], (fun lnvs -> (match lnvs with [] -> Some (DW_LNS_const_add_pc) | _ -> None ))); ("DW_LNS_fixed_advance_pc" , natural_of_hex "0x09", [LNAT_uint16 ], (fun lnvs -> (match lnvs with [LNAV_uint16 n] -> Some (DW_LNS_fixed_advance_pc n) | _ -> None ))); ("DW_LNS_set_prologue_end" , natural_of_hex "0x0a", [ ], (fun lnvs -> (match lnvs with [] -> Some (DW_LNS_set_prologue_end) | _ -> None ))); ("DW_LNS_set_epilogue_begin" , natural_of_hex "0x0b", [ ], (fun lnvs -> (match lnvs with [] -> Some (DW_LNS_set_epilogue_begin) | _ -> None ))); ("DW_LNS_set_isa" , natural_of_hex "0x0c", [LNAT_ULEB128 ], (fun lnvs -> (match lnvs with [LNAV_ULEB128 n] -> Some (DW_LNS_set_isa n) | _ -> None ))) ]) let line_number_extended_encodings:(string*Nat_big_num.num*(line_number_argument_type)list*((line_number_argument_value)list ->(line_number_operation)option))list= ([ ("DW_LNE_end_sequence" , natural_of_hex "0x01", [], (fun lnvs -> (match lnvs with [] -> Some (DW_LNE_end_sequence) | _ -> None ))); ("DW_LNE_set_address" , natural_of_hex "0x02", [LNAT_address], (fun lnvs -> (match lnvs with [LNAV_address n] -> Some (DW_LNE_set_address n) | _ -> None ))); ("DW_LNE_define_file" , natural_of_hex "0x03", [LNAT_string; LNAT_ULEB128; LNAT_ULEB128; LNAT_ULEB128], (fun lnvs -> (match lnvs with [LNAV_string s; LNAV_ULEB128 n1; LNAV_ULEB128 n2; LNAV_ULEB128 n3] -> Some (DW_LNE_define_file( s, n1, n2, n3)) | _ -> None ))); ("DW_LNE_set_discriminator" , natural_of_hex "0x04", [LNAT_ULEB128], (fun lnvs -> (match lnvs with [LNAV_ULEB128 n] -> Some (DW_LNE_set_discriminator n) | _ -> None ))) (* new in Dwarf 4*) ]) (* (DW_LNE_lo_user , natural_of_hex "0x80", "DW_LNE_lo_user"); (DW_LNE_hi_user , natural_of_hex "0xff", "DW_LNE_hi_user"); *) (* booleans encoded as a single byte containing the value 0 for âfalse,â and a non-zero value for âtrue.â *) (* base type attribute encoding *) let base_type_attribute_encodings:(string*Nat_big_num.num)list= ([ ("DW_ATE_address" , natural_of_hex "0x01"); ("DW_ATE_boolean" , natural_of_hex "0x02"); ("DW_ATE_complex_float" , natural_of_hex "0x03"); ("DW_ATE_float" , natural_of_hex "0x04"); ("DW_ATE_signed" , natural_of_hex "0x05"); ("DW_ATE_signed_char" , natural_of_hex "0x06"); ("DW_ATE_unsigned" , natural_of_hex "0x07"); ("DW_ATE_unsigned_char" , natural_of_hex "0x08"); ("DW_ATE_imaginary_float" , natural_of_hex "0x09"); ("DW_ATE_packed_decimal" , natural_of_hex "0x0a"); ("DW_ATE_numeric_string" , natural_of_hex "0x0b"); ("DW_ATE_edited" , natural_of_hex "0x0c"); ("DW_ATE_signed_fixed" , natural_of_hex "0x0d"); ("DW_ATE_unsigned_fixed" , natural_of_hex "0x0e"); ("DW_ATE_decimal_float" , natural_of_hex "0x0f"); ("DW_ATE_UTF" , natural_of_hex "0x10"); ("DW_ATE_lo_user" , natural_of_hex "0x80"); ("DW_ATE_signed_capability_hack_a0" , natural_of_hex "0xa0"); ("DW_ATE_unsigned_capability_hack_a1" , natural_of_hex "0xa1"); ("DW_ATE_hi_user" , natural_of_hex "0xff") ]) (** ************************************************************ *) (** ** more missing pervasives and bits *********************** *) (** ************************************************************ *) (* quick hacky workaround: this is in String.lem, in src_lem_library, but the linker doesn't find it *) (*val myconcat : string -> list string -> string*) let rec myconcat sep ss:string= ((match ss with | [] -> "" | s :: ss' -> (match ss' with | [] -> s | _ -> s ^ (sep ^ myconcat sep ss') ) )) (*val myhead : forall 'a. list 'a -> 'a*) let myhead l:'a= ((match l with | x::xs -> x | [] -> failwith "myhead of empty list" )) (*val myfindNonPure : forall 'a. ('a -> bool) -> list 'a -> 'a*) let myfindNonPure p0 l:'a= ((match (Lem_list.list_find_opt p0 l) with | Some e -> e | None -> failwith "myfindNonPure" )) (*val myfindmaybe : forall 'a 'b. ('a -> maybe 'b) -> list 'a -> maybe 'b*) let rec myfindmaybe f xs:'b option= ((match xs with | [] -> None | x::xs' -> (match f x with Some y -> Some y | None -> myfindmaybe f xs' ) )) (*val myfind : forall 'a. ('a -> bool) -> list 'a -> maybe 'a*) let rec myfind f xs:'a option= ((match xs with | [] -> None | x::xs' -> (match f x with true -> Some x | false -> myfind f xs' ) )) (*val myfiltermaybe : forall 'a 'b. ('a -> maybe 'b) -> list 'a -> list 'b*) let rec myfiltermaybe f xs:'b list= ((match xs with | [] -> [] | x::xs' -> (match f x with Some y -> y::myfiltermaybe f xs'| None -> myfiltermaybe f xs' ) )) (*val bytes_of_natural: endianness -> natural (*size*) -> natural (*value*) -> byte_sequence*) let bytes_of_natural en size2 n:Byte_sequence_wrapper.byte_sequence= (byte_sequence_of_byte_list ( if Nat_big_num.equal size2( (Nat_big_num.of_int 8)) then bytes_of_elf64_xword en (Uint64_wrapper.of_bigint n) else if Nat_big_num.equal size2( (Nat_big_num.of_int 4)) then bytes_of_elf32_word en (Uint32_wrapper.of_bigint n) else failwith "bytes_of_natural given size that is not 4 or 8")) let rec natural_of_bytes_little bs : Nat_big_num.num= ((match read_char bs with | Fail _ -> (Nat_big_num.of_int 0) | Success (b, bs') -> Nat_big_num.add (natural_of_byte b) (Nat_big_num.mul( (Nat_big_num.of_int 256)) (natural_of_bytes_little bs')) )) let rec natural_of_bytes_big acc bs:Nat_big_num.num= ((match read_char bs with | Fail _ -> acc | Success (b, bs') -> natural_of_bytes_big ( Nat_big_num.add(natural_of_byte b) (Nat_big_num.mul( (Nat_big_num.of_int 256)) acc)) bs' )) (*val natural_of_bytes: endianness -> byte_sequence -> natural*) let natural_of_bytes en bs:Nat_big_num.num= ((match en with | Little -> natural_of_bytes_little bs | Big -> natural_of_bytes_big( (Nat_big_num.of_int 0)) bs )) (* TODO: generalise *) (* match bs with | b0::b1::b2::b3::b4::b5::b6::b7::[] -> let v = if en=Little then natural_of_byte b0 + 256*natural_of_byte b1 + 256*256*natural_of_byte b2 + 256*256*256*natural_of_byte b3 + (256*256*256*256*(natural_of_byte b4 + 256*natural_of_byte b5 + 256*256*natural_of_byte b6 + 256*256*256*natural_of_byte b7)) else natural_of_byte b7 + 256*natural_of_byte b6 + 256*256*natural_of_byte b5 + 256*256*256*natural_of_byte b4 + (256*256*256*256*(natural_of_byte b3 + 256*natural_of_byte b2 + 256*256*natural_of_byte b1 + 256*256*256*natural_of_byte b0)) in v | b0::b1::b2::b3::[] -> let v = if en=Little then natural_of_byte b0 + 256*natural_of_byte b1 + 256*256*natural_of_byte b2 + 256*256*256*natural_of_byte b3 else natural_of_byte b3 + 256*natural_of_byte b2 + 256*256*natural_of_byte b1 + 256*256*256*natural_of_byte b0 in v | b0::b1::[] -> let v = if en=Little then natural_of_byte b0 + 256*natural_of_byte b1 else natural_of_byte b1 + 256*natural_of_byte b0 in v | b0::[] -> natural_of_byte b0 | _ -> Assert_extra.failwith "natural_of_bytes given not-8/4/2/1 bytes" end *) (*val bigunionListMap : forall 'a 'b. SetType 'b => ('a -> set 'b) -> list 'a -> set 'b*) let rec bigunionListMap dict_Basic_classes_SetType_b f xs:'b Pset.set= ((match xs with | [] ->(Pset.from_list dict_Basic_classes_SetType_b.setElemCompare_method []) | x::xs' -> Pset.(union) (f x) (bigunionListMap dict_Basic_classes_SetType_b f xs') )) let rec mytake' (n:Nat_big_num.num) acc xs:('a list*'a list)option= ( if(Nat_big_num.equal n ( (Nat_big_num.of_int 0))) then (Some (List.rev acc, xs)) else ((match xs with [] -> None | x::xs' -> mytake' (Nat_big_num.sub_nat n ( (Nat_big_num.of_int 1))) (x :: acc) xs' ))) (*val mytake : forall 'a. natural -> (list 'a) -> maybe (list 'a * list 'a)*) let mytake n xs:('a list*'a list)option= (mytake' n [] xs) (*val mynth : forall 'a. natural -> (list 'a) -> maybe 'a*) let rec mynth (n:Nat_big_num.num) xs:'a option= ( (*Assert_extra.failwith "mynth"*) if(Nat_big_num.equal n ( (Nat_big_num.of_int 0))) then ((match xs with x::xs' -> Some x | [] -> None )) else ((match xs with x::xs' -> mynth (Nat_big_num.sub_nat n ( (Nat_big_num.of_int 1))) xs' ))) (** basic pretty printing *) let pphexplain n:string= (unsafe_hex_string_of_natural 0 n) let pphex n:string= ("0x" ^ pphexplain n) (*val abs : integer -> natural*) (*declare isabelle target_rep function abs = `int` declare coq target_rep function abs n = (`Zpred` (`Zpos` (`P_of_succ_nat` n))) (* TODO: check*) *) let pphex_integer n:string= (if Nat_big_num.less n( (Nat_big_num.of_int 0)) then "-" ^ pphex (Nat_big_num.abs n) else pphex (Nat_big_num.abs n)) let ppbytes bs:string= (string_of_list instance_Show_Show_string_dict (Lem_list.map (fun x -> hex_string_of_byte x) (byte_list_of_byte_sequence bs))) let rec ppbytes2 n bs:string= ((match read_char bs with | Fail _ -> "" | Success (x,xs') -> "<" ^ (pphex n ^ ("> " ^ (hex_string_of_byte x ^ ("\n" ^ ppbytes2 (Nat_big_num.add n( (Nat_big_num.of_int 1))) xs')))) )) let rec ppbytesplain (c:p_context) (n:Nat_big_num.num) bs:string= (Nat_big_num.to_string (natural_of_bytes c.endianness bs)) (* unsafe_hex_string_of_uc_list (List.map unsigned_char_of_byte xs) (*match xs with | [] -> "" | x::xs' -> pphexplain x ^ ppbytesplain (n+1) xs' end*) *) (* workaround: from String *) (*val mytoString : list char -> string*) let string_of_bytes bs:string= (Xstring.implode (Lem_list.map (fun x-> x) bs)) let just_one s xs:'a= ((match xs with | [] -> failwith ("no " ^ s) | x1::x2::_ -> failwith ("more than one " ^ s) | [x] -> x )) let max_address (as': Nat_big_num.num) : Nat_big_num.num= ( if(Nat_big_num.equal as' ( (Nat_big_num.of_int 4))) then (natural_of_hex "0xffffffff") else ( if(Nat_big_num.equal as' ( (Nat_big_num.of_int 8))) then (natural_of_hex "0xffffffffffffffff") else (failwith "max_address size not 4 or 8"))) (** lookup of encodings *) (*val lookup_Ab_b : forall 'a 'b. Eq 'a => 'a -> list ('a * 'b) -> maybe 'b*) let rec lookup_Ab_b dict_Basic_classes_Eq_a x0 xys:'b option= ((match xys with | [] -> None | (x,y)::xys' -> if dict_Basic_classes_Eq_a.isEqual_method x x0 then Some y else lookup_Ab_b dict_Basic_classes_Eq_a x0 xys' )) (*val lookup_aB_a : forall 'a 'b. Eq 'b => 'b -> list ('a * 'b) -> maybe 'a*) let rec lookup_aB_a dict_Basic_classes_Eq_b y0 xys:'a option= ((match xys with | [] -> None | (x,y)::xys' -> if dict_Basic_classes_Eq_b.isEqual_method y y0 then Some x else lookup_aB_a dict_Basic_classes_Eq_b y0 xys' )) (*val lookup_aBc_a : forall 'a 'b 'c. Eq 'b => 'b -> list ('a * 'b * 'c) -> maybe 'a*) let rec lookup_aBc_a dict_Basic_classes_Eq_b y0 xyzs:'a option= ((match xyzs with | [] -> None | (x,y,_)::xyzs' -> if dict_Basic_classes_Eq_b.isEqual_method y y0 then Some x else lookup_aBc_a dict_Basic_classes_Eq_b y0 xyzs' )) (*val lookup_aBc_ac : forall 'a 'b 'c. Eq 'b => 'b -> list ('a * 'b * 'c) -> maybe ('a*'c)*) let rec lookup_aBc_ac dict_Basic_classes_Eq_b y0 xyzs:('a*'c)option= ((match xyzs with | [] -> None | (x,y,z)::xyzs' -> if dict_Basic_classes_Eq_b.isEqual_method y y0 then Some (x,z) else lookup_aBc_ac dict_Basic_classes_Eq_b y0 xyzs' )) (*val lookup_Abc_b : forall 'a 'b 'c. Eq 'a => 'a -> list ('a * 'b * 'c) -> maybe 'b*) let rec lookup_Abc_b dict_Basic_classes_Eq_a x0 xyzs:'b option= ((match xyzs with | [] -> None | (x,y,_)::xyzs' -> if dict_Basic_classes_Eq_a.isEqual_method x x0 then Some y else lookup_Abc_b dict_Basic_classes_Eq_a x0 xyzs' )) (*val lookup_aBcd_a : forall 'a 'b 'c 'd. Eq 'b => 'b -> list ('a * 'b * 'c * 'd) -> maybe 'a*) let rec lookup_aBcd_a dict_Basic_classes_Eq_b y0 xyzws:'a option= ((match xyzws with | [] -> None | (x,y,_,_)::xyzws' -> if dict_Basic_classes_Eq_b.isEqual_method y y0 then Some x else lookup_aBcd_a dict_Basic_classes_Eq_b y0 xyzws' )) (*val lookup_aBcd_acd : forall 'a 'b 'c 'd. Eq 'b => 'b -> list ('a * 'b * 'c * 'd) -> maybe ('a * 'c * 'd)*) let rec lookup_aBcd_acd dict_Basic_classes_Eq_b y0 xyzws:('a*'c*'d)option= ((match xyzws with | [] -> None | (x,y,z,w)::xyzws' -> if dict_Basic_classes_Eq_b.isEqual_method y y0 then Some (x,z,w) else lookup_aBcd_acd dict_Basic_classes_Eq_b y0 xyzws' )) (*val lookup_abCde_de : forall 'a 'b 'c 'd 'e. Eq 'c => 'c -> list ('a * 'b * 'c * 'd * 'e) -> maybe ('d * 'e)*) let rec lookup_abCde_de dict_Basic_classes_Eq_c z0 xyzwus:('d*'e)option= ((match xyzwus with | [] -> None | (x,y,z,w,u)::xyzwus' -> if dict_Basic_classes_Eq_c.isEqual_method z z0 then Some (w,u) else lookup_abCde_de dict_Basic_classes_Eq_c z0 xyzwus' )) let pp_maybe ppf n:string= ((match ppf n with Some s -> s | None -> "Unknown AT value: " ^ pphexplain n (*encoding not found: "" ^ pphex n*) )) let pp_tag_encoding n:string= (pp_maybe (fun n -> lookup_aB_a instance_Basic_classes_Eq_Num_natural_dict n tag_encodings) n) let pp_attribute_encoding n:string= (pp_maybe (fun n -> lookup_aBc_a instance_Basic_classes_Eq_Num_natural_dict n attribute_encodings) n) let pp_attribute_form_encoding n:string= (pp_maybe (fun n -> lookup_aBc_a instance_Basic_classes_Eq_Num_natural_dict n attribute_form_encodings) n) let pp_operation_encoding n:string= (pp_maybe (fun n -> lookup_aBcd_a instance_Basic_classes_Eq_Num_natural_dict n operation_encodings) n) let tag_encode (s: string) : Nat_big_num.num= ((match lookup_Ab_b instance_Basic_classes_Eq_string_dict s tag_encodings with | Some n -> n | None -> failwith ("tag_encode: \""^(s^"\"")) )) let attribute_encode (s: string) : Nat_big_num.num= ((match lookup_Abc_b instance_Basic_classes_Eq_string_dict s attribute_encodings with | Some n -> n | None -> failwith ("attribute_encode: \""^(s^"\"")) )) let attribute_form_encode (s: string) : Nat_big_num.num= ((match lookup_Abc_b instance_Basic_classes_Eq_string_dict s attribute_form_encodings with | Some n -> n | None -> failwith "attribute_form_encode" )) let base_type_attribute_encode (s: string) : Nat_big_num.num= ((match lookup_Ab_b instance_Basic_classes_Eq_string_dict s base_type_attribute_encodings with | Some n -> n | None -> failwith "base_type_attribute_encode" )) (** ************************************************************ *) (** ** parser combinators and primitives ********************* *) (** ************************************************************ *) (* parsing combinators *) type parse_context = { pc_bytes: byte_sequence0; pc_offset: Nat_big_num.num } type 'a parse_result = | PR_success of 'a * parse_context | PR_fail of string * parse_context type 'a parser = parse_context -> 'a parse_result let pp_parse_context pc:string= ("pc_offset = " ^ pphex pc.pc_offset) let pp_parse_fail s pc:string= ("Parse fail\n" ^ (s ^ (" at " ^ (pp_parse_context pc ^ "\n")))) let pp_parse_result ppa pr:string= ((match pr with | PR_success( x, pc) -> "Parse success\n" ^ (ppa x ^ ("\n" ^ (pp_parse_context pc ^ "\n"))) | PR_fail( s, pc) -> pp_parse_fail s pc )) (* [(>>=)] should be the monadic binding function for [parse_result]. *) (* but there's a type clash if we use >>=, and lem seems to output bad ocaml for >>>=. So we just use a non-infix version for now *) (*val pr_bind : forall 'a 'b. parse_result 'a -> ('a -> parser 'b) -> parse_result 'b*) let pr_bind x f:'b parse_result= ((match x with | PR_success( v, pc) -> f v pc | PR_fail( err, pc) -> PR_fail( err, pc) )) (*val pr_return : forall 'a. 'a -> (parser 'a)*) let pr_return x pc:'a parse_result= (PR_success( x, pc)) (*val pr_map : forall 'a 'b. ('a -> 'b) -> parse_result 'a -> parse_result 'b*) let pr_map f x:'b parse_result= ((match x with | PR_success( v, pc) -> PR_success( (f v), pc) | PR_fail( err, pc) -> PR_fail( err, pc) )) (*val pr_map2 : forall 'a 'b. ('a -> 'b) -> (parser 'a) -> (parser 'b)*) let pr_map2 f p:parse_context ->'b parse_result= (fun pc -> pr_map f (p pc)) (*val pr_post_map1 : forall 'a 'b. (parse_result 'a) -> ('a -> 'b) -> (parse_result 'b)*) let pr_post_map1 x f:'b parse_result= (pr_map f x) (* val pr_post_map : forall 'a 'b 'c. ('c -> parse_result 'a) -> ('a -> 'b) -> ('c -> parse_result 'b) let pr_post_map g f = fun x -> pr_map f (g x) *) (*val pr_post_map : forall 'a 'b. (parser 'a) -> ('a -> 'b) -> (parser 'b)*) let pr_post_map p f:parse_context ->'b parse_result= (fun (pc: parse_context) -> pr_map f (p pc)) (*val pr_with_pos : forall 'a. (parser 'a) -> (parser (natural * 'a))*) let pr_with_pos p:parse_context ->(Nat_big_num.num*'a)parse_result= (fun pc -> pr_map (fun x -> (pc.pc_offset,x)) (p pc)) (*val parse_pair : forall 'a 'b. (parser 'a) -> (parser 'b) -> (parser ('a * 'b))*) let parse_pair p1 p2:parse_context ->('a*'b)parse_result= (fun pc -> let _ = (my_debug "pair ") in pr_bind (p1 pc) (fun x pc' -> (match p2 pc' with | PR_success( y, pc'') -> PR_success( (x,y), pc'') | PR_fail( s, pc'') -> PR_fail( s, pc'') ))) (*val parse_triple : forall 'a 'b 'c. (parser 'a) -> (parser 'b) -> (parser 'c) -> parser ('a * ('b * 'c))*) let parse_triple p1 p2 p3:parse_context ->('a*('b*'c))parse_result= (parse_pair p1 (parse_pair p2 p3)) (*val parse_quadruple : forall 'a 'b 'c 'd. (parser 'a) -> (parser 'b) -> (parser 'c) -> (parser 'd) -> parser ('a * ('b * ('c * 'd)))*) let parse_quadruple p1 p2 p3 p4:parse_context ->('a*('b*('c*'d)))parse_result= (parse_pair p1 (parse_pair p2 (parse_pair p3 p4))) (*val parse_pentuple : forall 'a 'b 'c 'd 'e. (parser 'a) -> (parser 'b) -> (parser 'c) -> (parser 'd) -> (parser 'e) -> parser ('a * ('b * ('c * ('d * 'e))))*) let parse_pentuple p1 p2 p3 p4 p5:parse_context ->('a*('b*('c*('d*'e))))parse_result= (parse_pair p1 (parse_pair p2 (parse_pair p3 (parse_pair p4 p5)))) (*val parse_sextuple : forall 'a 'b 'c 'd 'e 'f. (parser 'a) -> (parser 'b) -> (parser 'c) -> (parser 'd) -> (parser 'e) -> (parser 'f) -> parser ('a * ('b * ('c * ('d * ('e * 'f)))))*) let parse_sextuple p1 p2 p3 p4 p5 p6:parse_context ->('a*('b*('c*('d*('e*'f)))))parse_result= (parse_pair p1 (parse_pair p2 (parse_pair p3 (parse_pair p4 (parse_pair p5 p6))))) (*val parse_dependent_pair : forall 'a 'b. (parser 'a) -> ('a -> parser 'b) -> (parser ('a * 'b))*) let parse_dependent_pair p1 p2:parse_context ->('a*'b)parse_result= (fun pc -> pr_bind (p1 pc) (fun x pc' -> (match p2 x pc' with | PR_success( y, pc'') -> PR_success( (x,y), pc'') | PR_fail( s, pc'') -> PR_fail( s, pc'') ))) (*val parse_dependent : forall 'a 'b. (parser 'a) -> ('a -> parser 'b) -> (parser 'b)*) let parse_dependent p1 p2:parse_context ->'b parse_result= (fun pc -> pr_bind (p1 pc) (fun x pc' -> p2 x pc')) (*val parse_list' : forall 'a. (parser (maybe 'a)) -> (list 'a -> parser (list 'a))*) let rec parse_list' p1:'a list ->parse_context ->('a list)parse_result= (fun acc pc -> let _ = (my_debug "list' ") in pr_bind (p1 pc) (fun mx pc' -> (match mx with | None -> PR_success( acc, pc') | Some x -> parse_list' p1 (x :: acc) pc' ))) (*val parse_list : forall 'a. (parser (maybe 'a)) -> (parser (list 'a))*) let parse_list p1:parse_context ->('a list)parse_result= (pr_post_map (parse_list' p1 []) (List.rev)) (*val parse_parser_list : forall 'a. (list (parser 'a)) -> (parser (list 'a))*) let rec parse_parser_list ps:parse_context ->('a list)parse_result= ((match ps with | [] -> pr_return [] | p::ps' -> (fun pc -> pr_bind (p pc) (fun x pc' -> (match parse_parser_list ps' pc' with | PR_success( xs, pc'') -> PR_success( (x::xs), pc'') | PR_fail( s, pc'') -> PR_fail( s, pc'') ))) )) (*val parse_maybe : forall 'a. parser 'a -> parser (maybe 'a)*) let parse_maybe p:parse_context ->('a option)parse_result= (fun pc -> if(Nat_big_num.equal (Byte_sequence.length0 pc.pc_bytes) ( (Nat_big_num.of_int 0))) then (pr_return None pc) else ((match p pc with | PR_success ( v , pc'' ) -> PR_success ( (Some v) , pc'' ) | PR_fail ( s , pc'' ) -> PR_fail ( s , pc'' ) ))) (*val parse_demaybe : forall 'a. string ->parser (maybe 'a) -> parser 'a*) let parse_demaybe s p:parse_context ->'a parse_result= (fun pc -> (match p pc with | PR_success( (Some v), pc'') -> PR_success( v, pc'') | PR_success( (None), pc'') -> PR_fail( s, pc'') | PR_fail( s, pc'') -> PR_fail( s, pc'') )) (*val parse_restrict_length : forall 'a. natural -> parser 'a -> parser 'a*) let parse_restrict_length n p:parse_context ->'a parse_result= (fun pc -> (match partition0 n pc.pc_bytes with | Fail _ -> failwith "parse_restrict_length not given enough bytes" | Success (xs,ys) -> let pc' = ({ pc_bytes = xs; pc_offset = (pc.pc_offset) }) in p pc' )) (* parsing of basic types *) let parse_byte :(char) parser= (fun (pc:parse_context) -> (match read_char pc.pc_bytes with | Fail _ -> PR_fail( "parse_byte", pc) | Success (b,bs) -> PR_success( b, ({pc_bytes=bs; pc_offset= (Nat_big_num.add pc.pc_offset( (Nat_big_num.of_int 1))) } )) )) let parse_n_bytes (n:Nat_big_num.num) : (byte_sequence0) parser= (fun (pc:parse_context) -> (match partition0 n pc.pc_bytes with | Fail _ -> PR_fail( ("parse_n_bytes n=" ^ pphex n), pc) | Success (xs,bs) -> PR_success( xs, ({pc_bytes=bs; pc_offset= (Nat_big_num.add pc.pc_offset (Byte_sequence.length0 xs)) } )) )) let bzero:char= (Char.chr (Nat_big_num.to_int ( (Nat_big_num.of_int 0)))) let parse_string : (byte_sequence0) parser= (fun (pc:parse_context) -> (match find_byte pc.pc_bytes bzero with | None -> PR_fail( "parse_string", pc) | Some n -> pr_bind (parse_n_bytes n pc) (fun res pc -> pr_bind (parse_byte pc) (fun _ pc -> pr_return res pc)) )) (* parse a null-terminated string; return Nothing if it is empty, Just s otherwise *) let parse_non_empty_string : ( byte_sequence0 option) parser= (fun (pc:parse_context) -> pr_bind (parse_string pc) (fun str pc -> if Nat_big_num.equal (Byte_sequence.length0 str)( (Nat_big_num.of_int 0)) then pr_return None pc else pr_return (Some str) pc)) let parse_uint8 : Nat_big_num.num parser= (fun (pc:parse_context) -> let _ = (my_debug "uint8 ") in (match read_char pc.pc_bytes with | Success (b, bytes) -> let v = (natural_of_byte b) in PR_success( v, ({ pc_bytes = bytes; pc_offset = (Nat_big_num.add pc.pc_offset( (Nat_big_num.of_int 1))) })) | _ -> PR_fail( "parse_uint32 not given enough bytes", pc) )) let parse_uint8_constant (v:Nat_big_num.num) : Nat_big_num.num parser= (fun (pc:parse_context) -> let _ = (my_debug "uint8_constant ") in PR_success( v, pc)) let parse_uint16 c : Nat_big_num.num parser= (fun (pc:parse_context) -> let _ = (my_debug "uint16 ") in (match read_2_bytes_be pc.pc_bytes with | Success ((b0,b1),bytes') -> let v = (if c.endianness=Little then Nat_big_num.add (natural_of_byte b0)(Nat_big_num.mul( (Nat_big_num.of_int 256))(natural_of_byte b1)) else Nat_big_num.add (natural_of_byte b1)(Nat_big_num.mul( (Nat_big_num.of_int 256))(natural_of_byte b0))) in PR_success( v, ({ pc_bytes = bytes'; pc_offset = (Nat_big_num.add pc.pc_offset( (Nat_big_num.of_int 2))) })) | _ -> PR_fail( "parse_uint32 not given enough bytes", pc) )) let parse_uint32 c : Nat_big_num.num parser= (fun (pc:parse_context) -> let _ = (my_debug "uint32 ") in (match read_4_bytes_be pc.pc_bytes with | Success ((b0,b1,b2,b3),bytes') -> let v = (if c.endianness=Little then Nat_big_num.add (Nat_big_num.add (Nat_big_num.add (natural_of_byte b0)(Nat_big_num.mul( (Nat_big_num.of_int 256))(natural_of_byte b1)))(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))(natural_of_byte b2)))(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))(natural_of_byte b3)) else Nat_big_num.add (Nat_big_num.add (Nat_big_num.add (natural_of_byte b3)(Nat_big_num.mul( (Nat_big_num.of_int 256))(natural_of_byte b2)))(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))(natural_of_byte b1)))(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))(natural_of_byte b0))) in PR_success( v, ({ pc_bytes = bytes'; pc_offset = (Nat_big_num.add pc.pc_offset( (Nat_big_num.of_int 4))) })) | _ -> PR_fail( "parse_uint32 not given enough bytes", pc) )) let parse_uint64 c : Nat_big_num.num parser= (fun (pc:parse_context) -> let _ = (my_debug "uint64 ") in (match read_8_bytes_be pc.pc_bytes with | Success ((b0,b1,b2,b3,b4,b5,b6,b7),bytes') -> let v = (if c.endianness=Little then Nat_big_num.add (Nat_big_num.add (Nat_big_num.add (Nat_big_num.add (natural_of_byte b0)(Nat_big_num.mul( (Nat_big_num.of_int 256))(natural_of_byte b1)))(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))(natural_of_byte b2)))(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))(natural_of_byte b3))) (Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( Nat_big_num.add (Nat_big_num.add (Nat_big_num.add(natural_of_byte b4)(Nat_big_num.mul( (Nat_big_num.of_int 256))(natural_of_byte b5)))(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))(natural_of_byte b6)))(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))(natural_of_byte b7)))) else Nat_big_num.add (Nat_big_num.add (Nat_big_num.add (Nat_big_num.add (natural_of_byte b7)(Nat_big_num.mul( (Nat_big_num.of_int 256))(natural_of_byte b6)))(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))(natural_of_byte b5)))(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))(natural_of_byte b4))) (Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( Nat_big_num.add (Nat_big_num.add (Nat_big_num.add(natural_of_byte b3)(Nat_big_num.mul( (Nat_big_num.of_int 256))(natural_of_byte b2)))(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))(natural_of_byte b1)))(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))(natural_of_byte b0))))) in PR_success( v, ({ pc_bytes = bytes'; pc_offset = (Nat_big_num.add pc.pc_offset( (Nat_big_num.of_int 8))) })) | _ -> PR_fail( "parse_uint64 not given enough bytes", pc) )) let integerFromTwosComplementNatural (n:Nat_big_num.num) (half: Nat_big_num.num) (all:Nat_big_num.num) : Nat_big_num.num= (if Nat_big_num.less n half then n else Nat_big_num.sub ( n) all) let partialTwosComplementNaturalFromInteger (i:Nat_big_num.num) (half: Nat_big_num.num) (all:Nat_big_num.num) : Nat_big_num.num= (if Nat_big_num.greater_equal i( (Nat_big_num.of_int 0)) && Nat_big_num.less i ( half) then partialNaturalFromInteger i else if Nat_big_num.greater_equal i (Nat_big_num.sub( (Nat_big_num.of_int 0))( half)) && Nat_big_num.less i( (Nat_big_num.of_int 0)) then partialNaturalFromInteger ( Nat_big_num.add all i) else failwith "partialTwosComplementNaturalFromInteger") let parse_sint8 : Nat_big_num.num parser= (pr_post_map (parse_uint8) (fun n -> integerFromTwosComplementNatural n( (Nat_big_num.of_int 128))( (Nat_big_num.of_int 256)))) let parse_sint16 c : Nat_big_num.num parser= (pr_post_map (parse_uint16 c) (fun n -> integerFromTwosComplementNatural n (Nat_big_num.mul( (Nat_big_num.of_int 128))( (Nat_big_num.of_int 256))) (Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256))))) let parse_sint32 c : Nat_big_num.num parser= (pr_post_map (parse_uint32 c) (fun n -> integerFromTwosComplementNatural n (Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 128))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256))) (Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256))))) let parse_sint64 c : Nat_big_num.num parser= (pr_post_map (parse_uint64 c) (fun n -> integerFromTwosComplementNatural n (Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 128))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256))) (Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 256))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256)))( (Nat_big_num.of_int 256))))) let rec parse_ULEB128' (acc: Nat_big_num.num) (shift_factor: Nat_big_num.num) : Nat_big_num.num parser= (fun (pc:parse_context) -> let _ = (my_debug "ULEB128' ") in (match read_char pc.pc_bytes with | Success (b,bytes') -> let n = (natural_of_byte b) in let acc' = (Nat_big_num.add (Nat_big_num.mul (Nat_big_num.bitwise_and n( (Nat_big_num.of_int 127))) shift_factor) acc) in let finished = ( Nat_big_num.equal(Nat_big_num.bitwise_and n( (Nat_big_num.of_int 128)))( (Nat_big_num.of_int 0))) in let pc' = ({ pc_bytes = bytes'; pc_offset = (Nat_big_num.add pc.pc_offset( (Nat_big_num.of_int 1))) }) in if finished then PR_success( acc', pc') else parse_ULEB128' acc' ( Nat_big_num.mul shift_factor( (Nat_big_num.of_int 128))) pc' | _ -> PR_fail( "parse_ULEB128' not given enough bytes", pc) )) let parse_ULEB128 : Nat_big_num.num parser= (fun (pc:parse_context) -> parse_ULEB128'( (Nat_big_num.of_int 0))( (Nat_big_num.of_int 1)) pc) let rec parse_SLEB128' (acc: Nat_big_num.num) (shift_factor: Nat_big_num.num) : (bool * Nat_big_num.num * Nat_big_num.num) parser= (fun (pc:parse_context) -> let _ = (my_debug "SLEB128' ") in (match read_char pc.pc_bytes with | Success (b,bytes') -> let n = (natural_of_byte b) in let acc' = (Nat_big_num.add acc (Nat_big_num.mul (Nat_big_num.bitwise_and n( (Nat_big_num.of_int 127))) shift_factor)) in let shift_factor' = (Nat_big_num.mul shift_factor( (Nat_big_num.of_int 128))) in let finished = ( Nat_big_num.equal(Nat_big_num.bitwise_and n( (Nat_big_num.of_int 128)))( (Nat_big_num.of_int 0))) in let positive = ( Nat_big_num.equal(Nat_big_num.bitwise_and n( (Nat_big_num.of_int 64)))( (Nat_big_num.of_int 0))) in let pc' = ({ pc_bytes = bytes'; pc_offset = (Nat_big_num.add pc.pc_offset( (Nat_big_num.of_int 1))) }) in if finished then PR_success( (positive, shift_factor', acc'), pc') else parse_SLEB128' acc' shift_factor' pc' | _ -> PR_fail( "parse_SLEB128' not given enough bytes", pc) )) let parse_SLEB128 : Nat_big_num.num parser= (pr_post_map (parse_SLEB128'( (Nat_big_num.of_int 0))( (Nat_big_num.of_int 1))) (fun (positive, shift_factor, acc) -> if positive then acc else Nat_big_num.sub ( acc) ( shift_factor))) let parse_nonzero_ULEB128_pair : ( (Nat_big_num.num*Nat_big_num.num)option) parser= (let _ = (my_debug "nonzero_ULEB128_pair ") in pr_post_map (parse_pair parse_ULEB128 parse_ULEB128) (fun (n1,n2) -> if Nat_big_num.equal n1( (Nat_big_num.of_int 0)) &&Nat_big_num.equal n2( (Nat_big_num.of_int 0)) then None else Some (n1,n2))) let parse_zero_terminated_ULEB128_pair_list : ( (Nat_big_num.num*Nat_big_num.num)list) parser= (let _ = (my_debug "zero_terminated_ULEB128_pair_list ") in parse_list parse_nonzero_ULEB128_pair) let parse_uintDwarfN c (df: dwarf_format) : Nat_big_num.num parser= ((match df with | Dwarf32 -> (parse_uint32 c) | Dwarf64 -> (parse_uint64 c) )) let parse_uint_address_size c (as': Nat_big_num.num) : Nat_big_num.num parser= ( if(Nat_big_num.equal as' ( (Nat_big_num.of_int 4))) then (parse_uint32 c) else ( if(Nat_big_num.equal as' ( (Nat_big_num.of_int 8))) then (parse_uint64 c) else (failwith ("cuh_address_size not 4 or 8: " ^ Nat_big_num.to_string as')))) let parse_uint_segment_selector_size c (ss: Nat_big_num.num) : ( Nat_big_num.num option) parser= ( if(Nat_big_num.equal ss ( (Nat_big_num.of_int 0))) then (pr_return None) else ( if(Nat_big_num.equal ss ( (Nat_big_num.of_int 1))) then (pr_post_map (parse_uint8) (fun n -> Some n)) else ( if(Nat_big_num.equal ss ( (Nat_big_num.of_int 2))) then (pr_post_map (parse_uint16 c) (fun n -> Some n)) else ( if(Nat_big_num.equal ss ( (Nat_big_num.of_int 4))) then (pr_post_map (parse_uint32 c) (fun n -> Some n)) else ( if(Nat_big_num.equal ss ( (Nat_big_num.of_int 8))) then (pr_post_map (parse_uint64 c) (fun n -> Some n)) else (failwith "cuh_address_size not 4 or 8")))))) (** ************************************************************ *) (** ** parsing and pretty printing of .debug_* sections ****** *) (** ************************************************************ *) (** abbreviations table: pp and parsing *) let pp_abbreviation_declaration (x:abbreviation_declaration):string= (" " ^ (Nat_big_num.to_string x.ad_abbreviation_code ^ (" " ^ (pp_tag_encoding x.ad_tag ^ (" " ^ ((if x.ad_has_children then "[has children]" else "[no children]") ^ ("\n" (* ^ " "^show (List.length x.ad_attribute_specifications) ^ " attributes\n"*) ^ (String.concat "" (Lem_list.map (fun (n1,n2) -> " " ^ (right_space_padded_to( (Nat_big_num.of_int 18)) (pp_attribute_encoding n1) ^ (" " ^ (pp_attribute_form_encoding n2 ^ "\n")))) x.ad_attribute_specifications) ^ " DW_AT value: 0 DW_FORM value: 0\n")))))))) let pp_abbreviations_table (x:abbreviations_table):string= ("offset: "^(pphex x.at_offset^("\n" ^ String.concat "" (Lem_list.map (pp_abbreviation_declaration) x.at_table)))) (* print the distinct abbreviation tables used by all compilation units *) let rec remove_duplicates dict_Basic_classes_Eq_a xys xys_acc:('a*'b)list= ((match xys with | [] -> List.rev xys_acc | (x,y)::xys' -> if List.exists (fun (x',y') -> dict_Basic_classes_Eq_a.isEqual_method x' x) xys_acc then remove_duplicates dict_Basic_classes_Eq_a xys' xys_acc else remove_duplicates dict_Basic_classes_Eq_a xys' ((x,y)::xys_acc) )) let pp_abbreviations_tables (d:dwarf):string= (let xs : (Nat_big_num.num * abbreviations_table) list = (Lem_list.map (fun cu -> (cu.cu_header.cuh_debug_abbrev_offset, cu.cu_abbreviations_table)) d.d_compilation_units) in let ys = (remove_duplicates instance_Basic_classes_Eq_Num_natural_dict xs []) in String.concat "*********************\n" (Lem_list.map (fun (x,y)->pp_abbreviations_table y) ys)) let parse_abbreviation_declaration c : ( abbreviation_declaration option) parser= (fun (pc: parse_context) -> pr_bind (parse_ULEB128 pc) (fun n1 pc' -> if Nat_big_num.equal n1( (Nat_big_num.of_int 0)) then PR_success( None, pc') else pr_bind (parse_ULEB128 pc') (fun n2 pc'' -> pr_bind (parse_uint8 pc'') (fun c pc''' -> pr_post_map1 (parse_zero_terminated_ULEB128_pair_list pc''') (fun l -> Some ( let ad = ({ ad_abbreviation_code = n1; ad_tag = n2; ad_has_children = (not (Nat_big_num.equal c( (Nat_big_num.of_int 0)))); ad_attribute_specifications = l; }) in (* let _ = my_debug2 (pp_abbreviation_declaration ad) in *) ad) ))))) let parse_abbreviations_table c:parse_context ->((abbreviation_declaration)list)parse_result= (parse_list (parse_abbreviation_declaration c)) (** debug_str entry *) let rec null_terminated_bs (bs: byte_sequence0) : byte_sequence0= ((match find_byte bs bzero with | Some i -> (match takebytes i bs with | Success bs' -> bs' | Fail _ -> failwith "find_byte or take_byte is broken" ) | None -> bs )) let pp_debug_str_entry (str: byte_sequence0) (n: Nat_big_num.num) : string= ((match dropbytes n str with | Fail _ -> "strp beyond .debug_str extent" | Success bs -> string_of_byte_sequence (null_terminated_bs bs) )) (** operations: pp and parsing *) let pp_operation_argument_value (oav:operation_argument_value) : string= ((match oav with | OAV_natural n -> pphex n | OAV_integer n -> pphex_integer n (* show n*) | OAV_block( n, bs) -> pphex n ^ (" " ^ ppbytes bs) )) let pp_operation_semantics (os: operation_semantics) : string= ((match os with | OpSem_lit -> "OpSem_lit" | OpSem_deref -> "OpSem_deref" | OpSem_stack _ -> "OpSem_stack ..." | OpSem_not_supported -> "OpSem_not_supported" | OpSem_binary _ -> "OpSem_binary ..." | OpSem_unary _ -> "OpSem_unary ..." | OpSem_opcode_lit _ -> "OpSem_opcode_lit ..." | OpSem_reg -> "OpSem_reg" | OpSem_breg -> "OpSem_breg" | OpSem_bregx -> "OpSem_bregx" | OpSem_fbreg -> "OpSem_fbreg" | OpSem_deref_size -> "OpSem_deref_size" | OpSem_nop -> "OpSem_nop" | OpSem_piece -> "OpSem_piece" | OpSem_bit_piece -> "OpSem_bitpiece" | OpSem_implicit_value -> "OpSem_implicit_value" | OpSem_stack_value -> "OpSem_stack_value" | OpSem_call_frame_cfa -> "OpSem_call_frame_cfa" )) let pp_operation_semantics_brief (os: operation_semantics) : string= ((match os with | OpSem_not_supported -> " (OpSem_not_supported)" | _ -> "" )) let pp_operation (op: operation) : string= (op.op_string ^ (((match op.op_argument_values with [] -> "" | _ -> " " ^ String.concat " " (Lem_list.map pp_operation_argument_value op.op_argument_values) )) ^ pp_operation_semantics_brief op.op_semantics)) let pp_operations (ops: operation list) : string= (String.concat "; " (Lem_list.map pp_operation ops)) (*val parser_of_operation_argument_type : p_context -> compilation_unit_header -> operation_argument_type -> (parser operation_argument_value)*) let parser_of_operation_argument_type c cuh oat:parse_context ->(operation_argument_value)parse_result= ((match oat with | OAT_addr -> pr_map2 (fun n -> OAV_natural n) (parse_uint_address_size c cuh.cuh_address_size) | OAT_dwarf_format_t -> pr_map2 (fun n -> OAV_natural n) (parse_uintDwarfN c cuh.cuh_dwarf_format) | OAT_uint8 -> pr_map2 (fun n -> OAV_natural n) (parse_uint8) | OAT_uint16 -> pr_map2 (fun n -> OAV_natural n) (parse_uint16 c) | OAT_uint32 -> pr_map2 (fun n -> OAV_natural n) (parse_uint32 c) | OAT_uint64 -> pr_map2 (fun n -> OAV_natural n) (parse_uint64 c) | OAT_sint8 -> pr_map2 (fun n -> OAV_integer n) (parse_sint8) | OAT_sint16 -> pr_map2 (fun n -> OAV_integer n) (parse_sint16 c) | OAT_sint32 -> pr_map2 (fun n -> OAV_integer n) (parse_sint32 c) | OAT_sint64 -> pr_map2 (fun n -> OAV_integer n) (parse_sint64 c) | OAT_ULEB128 -> pr_map2 (fun n -> OAV_natural n) parse_ULEB128 | OAT_SLEB128 -> pr_map2 (fun n -> OAV_integer n) parse_SLEB128 | OAT_block -> (fun pc -> pr_bind (parse_ULEB128 pc) (fun n pc' -> pr_map (fun bs -> OAV_block( n, bs)) (parse_n_bytes n pc'))) )) (*val parse_operation : p_context -> compilation_unit_header -> parser (maybe operation)*) let parse_operation c cuh pc:((operation)option)parse_result= ((match parse_uint8 pc with | PR_fail( s, pc') -> PR_success( None, pc) | PR_success( code, pc') -> (match lookup_aBcd_acd instance_Basic_classes_Eq_Num_natural_dict code operation_encodings with | None -> PR_fail( ("encoding not found: " ^ pphex code), pc) | Some (s,oats,opsem) -> let ps = (Lem_list.map (parser_of_operation_argument_type c cuh) oats) in (pr_post_map (parse_parser_list ps) (fun oavs -> Some { op_code = code; op_string = s; op_argument_values = oavs; op_semantics = opsem }) ) pc' ) )) (*val parse_operations : p_context -> compilation_unit_header -> parser (list operation)*) let parse_operations c cuh:parse_context ->((operation)list)parse_result= (parse_list (parse_operation c cuh)) let parse_operations_bs c cuh bs : operation list= (let pc = ({pc_bytes = bs; pc_offset =( (Nat_big_num.of_int 0)) }) in (match parse_operations c cuh pc with | PR_fail( s, pc') -> failwith ("parse_operations_bs fail: " ^ pp_parse_fail s pc') | PR_success( ops, pc') -> let _ = (if not (Nat_big_num.equal (Byte_sequence.length0 pc'.pc_bytes)( (Nat_big_num.of_int 0))) then failwith ("parse_operations_bs extra non-parsed bytes") else ()) in ops )) (*val parse_and_pp_operations : p_context -> compilation_unit_header -> byte_sequence -> string*) let parse_and_pp_operations c cuh bs:string= (let pc = ({pc_bytes = bs; pc_offset =( (Nat_big_num.of_int 0)) }) in (match parse_operations c cuh pc with | PR_fail( s, pc') -> "parse_operations fail: " ^ pp_parse_fail s pc' | PR_success( ops, pc') -> pp_operations ops ^ (if not (Nat_big_num.equal (Byte_sequence.length0 pc'.pc_bytes)( (Nat_big_num.of_int 0))) then " Warning: extra non-parsed bytes" else "") )) (** attribute values: pp and parsing *) (*val pp_attribute_value_plain : attribute_value -> string*) let pp_attribute_value_plain av:string= ((match av with | AV_addr x -> "AV_addr " ^ pphex x | AV_block( n, bs) -> "AV_block " ^ (Nat_big_num.to_string n ^ (" " ^ ppbytes bs)) | AV_constantN( n, bs) -> "AV_constantN " ^ (Nat_big_num.to_string n ^ (" " ^ ppbytes bs)) | AV_constant_SLEB128 i -> "AV_constant_SLEB128 " ^ Nat_big_num.to_string i | AV_constant_ULEB128 n -> "AV_constant_ULEB128 " ^ Nat_big_num.to_string n | AV_exprloc( n, bs) -> String.concat " " ["AV_exprloc"; Nat_big_num.to_string n; ppbytes bs] | AV_flag b -> "AV_flag " ^ string_of_bool b | AV_ref n -> "AV_ref " ^ pphex n | AV_ref_addr n -> "AV_ref_addr " ^ pphex n | AV_ref_sig8 n -> "AV_ref_sig8 " ^ pphex n | AV_sec_offset n -> "AV_sec_offset " ^ pphex n | AV_string bs -> string_of_byte_sequence bs | AV_strp n -> "AV_sec_offset " ^ (pphex n ^ " ") )) (*val pp_attribute_value : p_context -> compilation_unit_header -> byte_sequence -> natural (*attribute tag*) -> attribute_value -> string*) let pp_attribute_value c cuh str at av:string= ((match av with | AV_addr x -> "AV_addr " ^ pphex x | AV_block( n, bs) -> "AV_block " ^ (Nat_big_num.to_string n ^ (" " ^ (ppbytes bs ^ (if Nat_big_num.equal at (attribute_encode "DW_AT_location") then " " ^ parse_and_pp_operations c cuh bs else "")))) | AV_constantN( n, bs) -> "AV_constantN " ^ (Nat_big_num.to_string n ^ (" " ^ ppbytes bs)) | AV_constant_SLEB128 i -> "AV_constant_SLEB128 " ^ Nat_big_num.to_string i | AV_constant_ULEB128 n -> "AV_constant_ULEB128 " ^ Nat_big_num.to_string n | AV_exprloc( n, bs) -> String.concat " " ["AV_exprloc"; Nat_big_num.to_string n; ppbytes bs; parse_and_pp_operations c cuh bs] | AV_flag b -> "AV_flag " ^ string_of_bool b | AV_ref n -> "AV_ref " ^ pphex n | AV_ref_addr n -> "AV_ref_addr " ^ pphex n | AV_ref_sig8 n -> "AV_ref_sig8 " ^ pphex n | AV_sec_offset n -> "AV_sec_offset " ^ pphex n | AV_string bs -> string_of_byte_sequence bs | AV_strp n -> "AV_sec_offset " ^ (pphex n ^ (" " ^ pp_debug_str_entry str n)) )) (*val pp_attribute_value_like_objdump : p_context -> compilation_unit_header -> byte_sequence -> natural (*attribute tag*) -> attribute_value -> string*) let pp_attribute_value_like_objdump c cuh str at av:string= ((match av with | AV_addr x -> (*"AV_addr " ^*) pphex x | AV_block( n, bs) -> (*"AV_block " ^ show n ^ " " ^ ppbytes bs ^ if at = attribute_encode "DW_AT_location" then " " ^ parse_and_pp_operations c cuh bs else ""*) (* show n ^ " byte block: " *) ppbytesplain c n bs ^ (if Nat_big_num.equal at (attribute_encode "DW_AT_location") then " " ^ parse_and_pp_operations c cuh bs else "") | AV_constantN( n, bs) -> ppbytes bs (*"AV_constantN " ^ show n ^ " " ^ ppbytes bs*) | AV_constant_SLEB128 i -> (*"AV_constant_SLEB128 " ^*) Nat_big_num.to_string i | AV_constant_ULEB128 n -> (*"AV_constant_ULEB128 " ^*) Nat_big_num.to_string n | AV_exprloc( n, bs) -> (*"AV_exprloc " ^ show n ^ " " ^*) ppbytes bs ^ (" " ^ parse_and_pp_operations c cuh bs) | AV_flag b -> (*"AV_flag " ^*)if b then "1" else "0" | AV_ref n -> (*"AV_ref " ^*) "<"^(pphex ( Nat_big_num.add n cuh.cuh_offset)^">") | AV_ref_addr n -> (*"AV_ref_addr " ^*) "<"^(pphex n^">") | AV_ref_sig8 n -> "AV_ref_sig8 " ^ pphex n | AV_sec_offset n -> (*"AV_sec_offset " ^*) pphex n ^ (if Nat_big_num.equal at (attribute_encode "DW_AT_location") then " (location list)" else "") | AV_string bs -> string_of_byte_sequence bs | AV_strp n -> (*"AV_sec_offset " ^ pphex n ^ " " ^ pp_debug_str_entry str n*) "(indirect string, offset: "^(pphex n ^ ("): " ^ pp_debug_str_entry str n)) )) (*val parser_of_attribute_form_non_indirect : p_context -> compilation_unit_header -> natural -> parser attribute_value*) let parser_of_attribute_form_non_indirect c cuh n:parse_context ->(attribute_value)parse_result= ( (* address*)if Nat_big_num.equal n (attribute_form_encode "DW_FORM_addr") then pr_map2 (fun n -> AV_addr n) (parse_uint_address_size c cuh.cuh_address_size) (* block *) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_block1") then (fun pc -> pr_bind (parse_uint8 pc) (fun n pc' -> pr_map (fun bs -> AV_block( n, bs)) (parse_n_bytes n pc'))) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_block2") then (fun pc -> pr_bind (parse_uint16 c pc) (fun n pc' -> pr_map (fun bs -> AV_block( n, bs)) (parse_n_bytes n pc'))) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_block4") then (fun pc -> pr_bind (parse_uint32 c pc) (fun n pc' -> pr_map (fun bs -> AV_block( n, bs)) (parse_n_bytes n pc'))) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_block") then (fun pc -> pr_bind (parse_ULEB128 pc) (fun n pc' -> pr_map (fun bs -> AV_block( n, bs)) (parse_n_bytes n pc'))) (* constant *) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_data1") then pr_map2 (fun bs -> AV_block(( (Nat_big_num.of_int 1)), bs)) (parse_n_bytes( (Nat_big_num.of_int 1))) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_data2") then pr_map2 (fun bs -> AV_block(( (Nat_big_num.of_int 2)), bs)) (parse_n_bytes( (Nat_big_num.of_int 2))) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_data4") then pr_map2 (fun bs -> AV_block(( (Nat_big_num.of_int 4)), bs)) (parse_n_bytes( (Nat_big_num.of_int 4))) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_data8") then pr_map2 (fun bs -> AV_block(( (Nat_big_num.of_int 8)), bs)) (parse_n_bytes( (Nat_big_num.of_int 8))) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_sdata") then pr_map2 (fun i -> AV_constant_SLEB128 i) parse_SLEB128 else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_udata") then pr_map2 (fun n -> AV_constant_ULEB128 n) parse_ULEB128 (* exprloc *) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_exprloc") then (fun pc -> pr_bind (parse_ULEB128 pc) (fun n pc' -> pr_map (fun bs -> AV_exprloc( n, bs)) (parse_n_bytes n pc'))) (* flag *) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_flag") then pr_map2 (fun n -> AV_flag (not (Nat_big_num.equal n( (Nat_big_num.of_int 0))))) (parse_uint8) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_flag_present") then pr_map2 (fun () -> AV_flag true) (pr_return ()) (* lineptr, loclistptr, macptr, rangelistptr *) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_sec_offset") then pr_map2 (fun n -> AV_sec_offset n) (parse_uintDwarfN c cuh.cuh_dwarf_format) (* reference - first type *) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_ref1") then pr_map2 (fun n -> AV_ref n) (parse_uint8) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_ref2") then pr_map2 (fun n -> AV_ref n) (parse_uint16 c) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_ref4") then pr_map2 (fun n -> AV_ref n) (parse_uint32 c) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_ref8") then pr_map2 (fun n -> AV_ref n) (parse_uint64 c) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_ref_udata") then pr_map2 (fun n -> AV_ref n) parse_ULEB128 (* reference - second type *) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_ref_addr") then pr_map2 (fun n -> AV_ref_addr n) (parse_uintDwarfN c cuh.cuh_dwarf_format) (* reference - third type *) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_ref_sig8") then pr_map2 (fun n -> AV_ref_sig8 n) (parse_uint64 c) (* string *) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_string") then pr_map2 (fun bs -> AV_string bs) parse_string else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_strp") then pr_map2 (fun n -> AV_strp n) (parse_uintDwarfN c cuh.cuh_dwarf_format) (* indirect (cycle detection) *) else if Nat_big_num.equal n (attribute_form_encode "DW_FORM_indirect") then failwith "DW_FORM_INDIRECT cycle" (* unknown *) else failwith "parser_of_attribute_form_non_indirect: unknown attribute form") let parser_of_attribute_form c cuh n:parse_context ->(attribute_value)parse_result= (if Nat_big_num.equal n (attribute_form_encode "DW_FORM_indirect") then (fun pc -> pr_bind (parse_ULEB128 pc) (fun n -> parser_of_attribute_form_non_indirect c cuh n) ) else parser_of_attribute_form_non_indirect c cuh n) (* *** where to put this? *) let pp_pos pos:string= ("<" ^ (pphexplain pos ^">")) let pp_cupdie (cu,parents,die1):string= (pp_pos cu.cu_header.cuh_offset ^ ("/" ^ pp_pos die1.die_offset)) let pp_cupdie3 (cu,parents,die1):string= (pp_pos die1.die_offset ^ ("/" ^ (String.concat "/" (Lem_list.map (fun p -> pp_pos p.die_offset) parents) ^ ("/" ^ pp_pos cu.cu_header.cuh_offset)))) (** ************************************************************ *) (** ** finding things in the die tree *) (** ************************************************************ *) (*val find_maybe : forall 'a 'b. ('a -> maybe 'b) -> list 'a -> maybe 'b*) let rec find_maybe f l:'b option= ((match l with | [] -> None | x :: xs -> (match f x with | Some y -> Some y | None -> find_maybe f xs ) )) let rec find_die_by_offset_in_cu offset cu : cupdie option= ((match Pmap.lookup offset cu.cu_index with | Some (parents,die1) -> Some (cu, parents,die1) | None -> None )) let find_die_by_offset_in_all offset d : cupdie option= (find_maybe (fun cu -> find_die_by_offset_in_cu offset cu) d.d_compilation_units) (*val find_dies_in_die : (die->bool) -> compilation_unit -> list die -> die -> list cupdie*) let rec find_dies_in_die (p:die->bool) (cu:compilation_unit) (parents: die list) (d: die):(compilation_unit*(die)list*die)list= (let ds = (List.concat (map (find_dies_in_die p cu (d::parents)) d.die_children)) in if p d then (cu,parents,d)::ds else ds) let find_dies (p:die->bool) (d: dwarf) : cupdie list= (List.concat (map (fun cu -> find_dies_in_die p cu [] cu.cu_die) d.d_compilation_units)) (** convert attribute values to usable Lem types *) let string_of_string_attribute_value str av : string= ((match av with | AV_string bs -> string_of_byte_sequence bs | AV_strp n -> pp_debug_str_entry str n | _ -> "find_string_attribute_value_of_die AV not understood" )) let maybe_natural_of_constant_attribute_value die1 c av : Nat_big_num.num option= ((match av with | AV_constantN( n, bs) -> Some n | AV_constant_ULEB128 n -> Some n | AV_block( n, bs) -> Some (natural_of_bytes c.endianness bs) | _ -> None )) let natural_of_constant_attribute_value die1 c av : Nat_big_num.num= ((match maybe_natural_of_constant_attribute_value die1 c av with | Some n -> n | None -> failwith ("natural_of_constant_attribute_value fail at " ^ (pp_pos die1.die_offset ^ (" with av= " ^ pp_attribute_value_plain av))) )) let integer_of_constant_attribute_value c av : Nat_big_num.num= ((match av with | AV_constantN( n, bs) -> n | AV_constant_ULEB128 n -> n | AV_constant_SLEB128 n -> n | AV_block( n, bs) -> (natural_of_bytes c.endianness bs) | _ -> failwith ("integer_of_constant_attribute_value fail") )) let bool_of_flag_attribute_value av : bool= ((match av with | AV_flag b -> b | _ -> failwith ("bool_of_maybe_flag_attribute_value fail") )) let reference_of_reference_attribute_value c d cu str av : (compilation_unit * ( die list) * die)option= ((match av with (* "offset from the first byte of the compilation header for the compilation unit containing the reference" *) | AV_ref n -> let n' =(Nat_big_num.add n cu.cu_header.cuh_offset) in (match find_die_by_offset_in_all n' d (*cu.cu_die*) with | Some (cu',parents',die') -> Some (cu',parents',die') | None -> None (* Fail ("find_reference_attribute_of_die AV_ref failed (cuh="^pphex cu.cu_header.cuh_offset ^" n'="^pphex n'^")"^"\n"^ppd())*) ) (* offset in .debug_info *) | AV_ref_addr n -> (match find_die_by_offset_in_all n d with | Some (cu',parents',die') -> Some (cu',parents',die') | None -> None (*Fail ("find_reference_attribute_of_die AV_ref_addr failed\n"^ppd())*) ) | _ -> None (*Fail ("reference_of_reference_attribute AV ("^pp_attribute_value c cu.cu_header str (attribute_encode an) av^") not supported\n"^ppd() )*) (* TODO: handle the AV_ref_sig8 case for type signature references *) )) (** attribute find *) let find_attribute_value (an: string) (die1:die) : attribute_value option= (let at = (attribute_encode an) in let ats = (Lem_list.list_combine die1.die_abbreviation_declaration.ad_attribute_specifications die1.die_attribute_values) in myfindmaybe (fun (((at': Nat_big_num.num), (af: Nat_big_num.num)), ((pos: Nat_big_num.num),(av:attribute_value))) -> if Nat_big_num.equal at' at then Some av else None) ats) let find_string_attribute_value_of_die (an: string) str (die1:die) : string option= ((match find_attribute_value an die1 with | Some av -> let s = (string_of_string_attribute_value str av) in Some s | None -> None )) let find_natural_attribute_value_of_die c (an: string) (die1:die) : Nat_big_num.num option= ((match find_attribute_value an die1 with | Some av -> let n = (natural_of_constant_attribute_value die1 c av) in Some n | None -> None )) let find_integer_attribute_value_of_die c (an: string) (die1:die) : Nat_big_num.num option= ((match find_attribute_value an die1 with | Some av -> let n = (integer_of_constant_attribute_value c av) in Some n | None -> None )) let find_flag_attribute_value_of_die (an: string) (die1:die) : bool option= (Lem.option_map bool_of_flag_attribute_value (find_attribute_value an die1)) let find_flag_attribute_value_of_die_default_false (an: string) (die1:die) : bool= ((match find_flag_attribute_value_of_die an die1 with | Some b -> b | None -> false )) let find_name_of_die str die1 : string option= (find_string_attribute_value_of_die "DW_AT_name" str die1) let find_reference_attribute_of_die c d cu str an die1 : (compilation_unit * ( die list) * die)option= (let ppd ()= (pp_pos die1.die_offset) (*pp_die c cuh str true 0 false die ^ "\n"*) in (match find_attribute_value an die1 with | None -> None (*Fail ("find_reference_attribute_of_die found no " ^ an ^ "\n" ^ ppd())*) | Some av -> reference_of_reference_attribute_value c d cu str av )) let find_DW_AT_type_of_die c d cu str die1 : (compilation_unit * ( die list) * die)option= (find_reference_attribute_of_die c d cu str "DW_AT_type" die1) (* look up "an" in die. If not found, see if die has an abstract origin, and if so, look up "an" in that. Return the relevant cu, too *) let find_attribute_value_using_abstract_origin c d cu str an die1 : (compilation_unit * attribute_value)option= ((match find_attribute_value an die1 with | Some av -> Some (cu,av) | None -> (match find_reference_attribute_of_die c d cu str "DW_AT_abstract_origin" die1 with | None -> None (*s ^ " and no DW_AT_abstract_origin"*) | Some (cu',parents',die') -> (match find_attribute_value an die' with | Some av -> Some (cu',av) | None -> None ) ) )) let find_name_of_die_using_abstract_origin c d cu str die1 : string option= ((match find_attribute_value_using_abstract_origin c d cu str "DW_AT_name" die1 with | None -> None | Some (cu',av) -> Some (string_of_string_attribute_value str av) )) (* TODO: not sure how DW_AT_specification should interact with abstract origins *) let find_name_of_die_using_abstract_origin_and_spec c d cu str die1 mcupdie_spec : string option= ((match find_name_of_die_using_abstract_origin c d cu str die1 with | Some name1 -> Some name1 | None -> (match mcupdie_spec with | Some (((cu_spec,parents_spec,die_spec) as cupdie_spec)) -> find_name_of_die_using_abstract_origin c d cu_spec str die_spec | None -> None ) )) let find_reference_attribute_using_abstract_origin c d cu str an die1 : (compilation_unit * ( die list) * die)option= ((match find_attribute_value_using_abstract_origin c d cu str an die1 with | None -> None | Some (cu',av) -> reference_of_reference_attribute_value c d cu' str av )) let find_DW_AT_type_of_die_using_abstract_origin c d cu str die1 : (compilation_unit * ( die list) * die)option= (find_reference_attribute_using_abstract_origin c d cu str "DW_AT_type" die1) let find_flag_attribute_value_of_die_using_abstract_origin d (an: string) ((cu,parents,die1):cupdie) : bool option= (let c = (p_context_of_d d) in (match find_attribute_value_using_abstract_origin c d cu d.d_str an die1 with | None -> None | Some (cu',av) -> Some (bool_of_flag_attribute_value av) )) (** compilation unit header: pp and parsing *) let pp_dwarf_format df:string= ((match df with Dwarf32 -> "(32-bit)" | Dwarf64 -> "(64-bit)" )) let pp_unit_header (s:string) (x:compilation_unit_header) : string= ("**" ^ (s ^ (" Unit @ offset " ^ (pphex x.cuh_offset ^ ("\n" ^ (" " ^ (s ^ (" Unit @ offset " ^ (pphex x.cuh_offset ^ (":\n" ^ (" Length: " ^ (pphex x.cuh_unit_length ^ (" " ^ (pp_dwarf_format x.cuh_dwarf_format ^ ("\n" ^ (" Version: " ^ (Nat_big_num.to_string x.cuh_version ^ ("\n" ^ (" Abbrev Offset: " ^ (pphex x.cuh_debug_abbrev_offset ^ ("\n" ^ (" Pointer Size: " ^ (Nat_big_num.to_string x.cuh_address_size ^ "\n"))))))))))))))))))))))) let pp_compilation_unit_header (x:compilation_unit_header) : string= (pp_unit_header "Compilation" x) let parse_unit_length c : (dwarf_format * Nat_big_num.num) parser= (fun (pc: parse_context) -> pr_bind (parse_uint32 c pc) (fun x pc' -> if Nat_big_num.less x (natural_of_hex "0xfffffff0") then PR_success( (Dwarf32,x), pc') else if not (Nat_big_num.equal x (natural_of_hex "0xffffffff")) then PR_fail( "bad unit_length", pc) else pr_bind (parse_uint64 c pc') (fun x' pc'' -> PR_success( (Dwarf64, x'), pc')))) let parse_compilation_unit_header c : compilation_unit_header parser= (pr_post_map (pr_with_pos (parse_dependent_pair (parse_unit_length c) (fun (df,ul) -> parse_triple (parse_uint16 c) (* version *) (parse_uintDwarfN c df) (* debug abbrev offset *) (parse_uint8) (* address_size *)))) (fun (offset,((df,ul), (v, (dao, as')))) -> { cuh_offset = offset; cuh_dwarf_format = df; cuh_unit_length = ul; cuh_version = v; cuh_debug_abbrev_offset = dao; cuh_address_size = as'; })) (** type unit header: pp and parsing *) (* the test binaries don't have a .debug_types section, so this isn't tested *) let pp_type_unit_header (x:type_unit_header) : string= (pp_unit_header "Type" x.tuh_cuh ^ (" Type Signature: " ^ (pphex x.tuh_type_signature ^ ("\n" ^ (" Type Offset: " ^ (pphex x.tuh_type_offset ^ "\n")))))) let parse_type_unit_header c : type_unit_header parser= (pr_post_map (parse_dependent_pair (parse_compilation_unit_header c) (fun cuh -> parse_pair (parse_uint64 c) (* type signature *) (parse_uintDwarfN c cuh.cuh_dwarf_format) (* type offset *) )) (fun (cuh, (ts, to')) -> { tuh_cuh = cuh; tuh_type_signature = ts; tuh_type_offset = to'; })) (** debugging information entries: pp and parsing *) (* example pp from readelf <2><51>: Abbrev Number: 3 (DW_TAG_variable) <52> DW_AT_name : x <54> DW_AT_decl_file : 1 <55> DW_AT_decl_line : 2 <56> DW_AT_type : <0x6a> <5a> DW_AT_location : 2 byte block: 91 6c (DW_OP_fbreg: -20) *) (** debugging information entries: pp and parsing *) let indent_level (indent: bool) (level: Nat_big_num.num):string= (if indent then (Xstring.implode (replicate0 ( Nat_big_num.mul( (Nat_big_num.of_int 3)) level) ' ')) else " ") let indent_level_plus_one indent level:string= (if indent then indent_level indent (Nat_big_num.add level( (Nat_big_num.of_int 1))) else " "^" ") let pp_die_attribute c (cuh:compilation_unit_header) (str : byte_sequence0) (indent:bool) (level: Nat_big_num.num) (((at: Nat_big_num.num), (af: Nat_big_num.num)), ((pos: Nat_big_num.num),(av:attribute_value))) : string= (indent_level_plus_one indent level ^ (pp_pos pos ^ (" " ^ (right_space_padded_to( (Nat_big_num.of_int 18)) (pp_attribute_encoding at) ^ (": " ^ (if indent then "(" ^ (pp_attribute_form_encoding af ^ (") " ^ (pp_attribute_value c cuh str at av ^ "\n"))) else pp_attribute_value_like_objdump c cuh str at av ^ "\n")))))) (*val pp_die : p_context -> compilation_unit_header -> byte_sequence -> bool -> natural -> bool -> die -> string*) let rec pp_die c cuh str indent level (pp_children:bool) die1:string= (indent_level indent level ^ ("<" ^ (Nat_big_num.to_string level ^ (">" ^ (pp_pos die1.die_offset ^ (": Abbrev Number: " ^ (Nat_big_num.to_string die1.die_abbreviation_code ^ (" (" ^ (pp_tag_encoding die1.die_abbreviation_declaration.ad_tag ^(")\n" ^ (let ats = (Lem_list.list_combine die1.die_abbreviation_declaration.ad_attribute_specifications die1.die_attribute_values) in (String.concat "" (Lem_list.map (pp_die_attribute c cuh str indent level) ats)) ^ (if pp_children then String.concat "" (Lem_list.map (pp_die c cuh str indent ( Nat_big_num.add level( (Nat_big_num.of_int 1))) pp_children) die1.die_children) else "")))))))))))) (*val pp_die_abbrev : p_context -> compilation_unit_header -> byte_sequence -> natural -> bool -> (list die) -> die -> string*) let rec pp_die_abbrev c cuh str level (pp_children:bool) parents die1:string= (indent_level true level ^ (pp_tag_encoding die1.die_abbreviation_declaration.ad_tag ^ (" (" ^ (pp_pos die1.die_offset ^ (") " (* ^ ": Abbrev Number: " ^ show die.die_abbreviation_code *) ^ (((match find_name_of_die str die1 with Some s -> s | None -> "-" )) ^ (" : " ^ (String.concat " : " (Lem_list.map (fun die' -> pp_tag_encoding die'.die_abbreviation_declaration.ad_tag) parents) ^ ("\n" ^ ( (*(String.concat "" (List.map (pp_die_abbrev_attribute c cuh str) ats))*)if pp_children then String.concat "" (Lem_list.map (pp_die_abbrev c cuh str ( Nat_big_num.add level( (Nat_big_num.of_int 1))) pp_children (die1::parents)) die1.die_children) else "")))))))))) (* condensed pp for variables *) (*val pp_die_abbrev_var : p_context -> dwarf -> compilation_unit -> byte_sequence -> bool -> (list die) -> die -> (string (*name*) * string (*offset*) * string (*kind*))*) let rec pp_die_abbrev_var c d cu str (pp_children:bool) parents die1:string*string*string= (* (indent_level true level*) (* ^ pp_tag_encoding die.die_abbreviation_declaration.ad_tag*) (* ^ ": Abbrev Number: " ^ show die.die_abbreviation_code *) (((match find_name_of_die_using_abstract_origin c d cu str die1 with | Some s -> s | None -> "?" )) , pp_pos die1.die_offset, (if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_variable") then "var" else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_formal_parameter") then "param" else "other") ) (* condensed pp for variable parents *) (*val pp_die_abbrev_var_parent : p_context -> dwarf -> compilation_unit -> byte_sequence -> die -> string*) let pp_die_abbrev_var_parent c d cu str die1:string= ( (* (indent_level true level*) (* ^ pp_tag_encoding die.die_abbreviation_declaration.ad_tag*) (* ^ ": Abbrev Number: " ^ show die.die_abbreviation_code *)let name1 = ((match find_name_of_die_using_abstract_origin c d cu str die1 with Some s -> s | None -> "" )) in let offset = (pp_pos die1.die_offset) in (if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_compile_unit") then name1 else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_subprogram") then name1 (*"subprogram"*) else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_inlined_subroutine") then name1 ^ "(inlined)" else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_lexical_block") then "block" else name1 ^ ("(" ^ (pp_tag_encoding die1.die_abbreviation_declaration.ad_tag ^ ")")))) (*val pp_die_abbrev_var_parents : p_context -> dwarf -> compilation_unit -> byte_sequence -> list die -> string*) let pp_die_abbrev_var_parents c d cu str parents:string= (String.concat ":" (Lem_list.map (fun die1 -> pp_die_abbrev_var_parent c d cu str die1) parents)) (* ^ " : " ^ String.concat " : " (List.map (fun die' -> pp_tag_encoding die'.die_abbreviation_declaration.ad_tag) parents)*) (* ^ "\n"*) (* ^ (*(String.concat "" (List.map (pp_die_abbrev_attribute c cuh str) ats))*)*) (* if pp_children then String.concat "" (List.map (pp_die_abbrev c cuh str (level +1) pp_children (die::parents)) die.die_children) else ""*) (*val parse_die : p_context -> byte_sequence -> compilation_unit_header -> (natural->abbreviation_declaration) -> parser (maybe die)*) let rec parse_die c str cuh find_abbreviation_declaration:parse_context ->((die)option)parse_result= (fun (pc: parse_context) -> (* let _ = my_debug3 ("parse_die called at " ^ pp_parse_context pc ^ "\n") in *) pr_bind (parse_ULEB128 pc) (fun abbreviation_code pc' -> if Nat_big_num.equal abbreviation_code( (Nat_big_num.of_int 0)) then PR_success( None, pc') else (* let _ = my_debug3 ("parse_die abbreviation code "^pphex abbreviation_code ^"\n") in *) let ad = (find_abbreviation_declaration abbreviation_code) in let attribute_value_parsers = (Lem_list.map (fun (at,af) -> pr_with_pos (parser_of_attribute_form c cuh af)) ad.ad_attribute_specifications) in pr_bind (parse_parser_list attribute_value_parsers pc') (fun avs pc'' -> (* let die_header = <| die_offset = pc.pc_offset; die_abbreviation_code = abbreviation_code; die_abbreviation_declaration = ad; die_attribute_values = avs; die_children = []; |> in let _ = my_debug3 ("die_header " ^ pp_die cuh str true 999 die_header) in *) pr_bind (if ad.ad_has_children then parse_list (parse_die c str cuh find_abbreviation_declaration) pc'' else pr_return [] pc'') (fun dies pc''' -> PR_success( (Some ( let die1 = ({ die_offset = (pc.pc_offset); die_abbreviation_code = abbreviation_code; die_abbreviation_declaration = ad; die_attribute_values = avs; die_children = dies; }) in let _ = (my_debug3 ("die entire " ^ pp_die c cuh str true( (Nat_big_num.of_int 0)) false die1)) in die1)), pc'''))))) let has_attribute (an: string) (die1: die) : bool= (Lem_list.elem instance_Basic_classes_Eq_Num_natural_dict (attribute_encode an) (Lem_list.map fst die1.die_abbreviation_declaration.ad_attribute_specifications)) (** compilation units: pp and parsing *) let pp_compilation_unit c (indent:bool) (debug_str_section_body: byte_sequence0) cu:string= ("" (* "*** compilation unit header ***\n"*) ^ (pp_compilation_unit_header cu.cu_header ^ ("\n*** compilation unit abbreviation table\n" ^ (pp_abbreviations_table cu.cu_abbreviations_table ^ ("\n" ^ ("*** compilation unit die tree\n" ^ (pp_die c cu.cu_header debug_str_section_body indent( (Nat_big_num.of_int 0)) true cu.cu_die ^ "\n"))))))) let pp_compilation_units c (indent:bool) debug_string_section_body (compilation_units1: compilation_unit list) : string= (String.concat "" (Lem_list.map (pp_compilation_unit c indent debug_string_section_body) compilation_units1)) let pp_compilation_unit_abbrev c (debug_str_section_body: byte_sequence0) cu:string= (pp_compilation_unit_header cu.cu_header (* ^ pp_abbreviations_table cu.cu_abbreviations_table*) ^ pp_die_abbrev c cu.cu_header debug_str_section_body( (Nat_big_num.of_int 0)) true [] cu.cu_die) let pp_compilation_units_abbrev c debug_string_section_body (compilation_units1: compilation_unit list) : string= (String.concat "" (Lem_list.map (pp_compilation_unit_abbrev c debug_string_section_body) compilation_units1)) (*val add_die_to_index : die_index -> list die -> die -> die_index*) let rec add_die_to_index acc parents die1:((Nat_big_num.num),((die)list*die))Pmap.map= (let nacc : die_index = (Pmap.add die1.die_offset (parents,die1) acc) in List.fold_left (fun acc ndie -> add_die_to_index acc (die1::parents) ndie) nacc die1.die_children) let parse_compilation_unit c (debug_str_section_body: byte_sequence0) (debug_abbrev_section_body: byte_sequence0) : ( compilation_unit option) parser= (fun (pc:parse_context) -> if Nat_big_num.equal (Byte_sequence.length0 pc.pc_bytes)( (Nat_big_num.of_int 0)) then PR_success( None, pc) else let (cuh, pc') = ((match parse_compilation_unit_header c pc with | PR_fail( s, pc') -> failwith ("parse_cuh_header fail: " ^ pp_parse_fail s pc') | PR_success( cuh, pc') -> (cuh,pc') )) in (*let _ = my_debug4 (pp_compilation_unit_header cuh) in*) if Nat_big_num.equal cuh.cuh_unit_length( (Nat_big_num.of_int 0)) then PR_success( None, pc') else let pc_abbrev = ({pc_bytes = ((match dropbytes cuh.cuh_debug_abbrev_offset debug_abbrev_section_body with Success bs -> bs | Fail _ -> failwith "mydrop of debug_abbrev" )); pc_offset = (cuh.cuh_debug_abbrev_offset) }) in (* todo: this is reparsing the abbreviations table for each cu *) let abbreviations_table1 = ((match parse_abbreviations_table c pc_abbrev with | PR_fail( s, pc_abbrev') -> failwith ("parse_abbrevations_table fail: " ^ pp_parse_fail s pc_abbrev') | PR_success( at, pc_abbrev') -> { at_offset=(pc_abbrev.pc_offset); at_table= at} )) in (* let _ = my_debug4 (pp_abbreviations_table abbreviations_table) in *) let find_abbreviation_declaration (ac:Nat_big_num.num) : abbreviation_declaration= ( (* let _ = my_debug4 ("find_abbreviation_declaration "^pphex ac) in *)myfindNonPure (fun ad -> Nat_big_num.equal ad.ad_abbreviation_code ac) abbreviations_table1.at_table) in (* let _ = my_debug3 (pp_abbreviations_table abbreviations_table) in *) (match parse_die c debug_str_section_body cuh find_abbreviation_declaration pc' with | PR_fail( s, pc'') -> failwith ("parse_die fail: " ^ pp_parse_fail s pc'') | PR_success( (None), pc'') -> failwith ("parse_die returned Nothing: " ^ pp_parse_context pc'') | PR_success( (Some die1), pc'') -> let cu = ({ cu_header = cuh; cu_abbreviations_table = abbreviations_table1; cu_die = die1; cu_index = (add_die_to_index (Pmap.empty Nat_big_num.compare) [] die1) }) in PR_success( (Some cu), pc'') )) let parse_compilation_units c (debug_str_section_body: byte_sequence0) (debug_abbrev_section_body: byte_sequence0): ( compilation_unit list) parser= (parse_list (parse_compilation_unit c debug_str_section_body debug_abbrev_section_body)) (** type units: pp and parsing *) let pp_type_unit c (debug_str_section_body: byte_sequence0) tu:string= (pp_type_unit_header tu.tu_header ^ (pp_abbreviations_table tu.tu_abbreviations_table ^ pp_die c tu.tu_header.tuh_cuh debug_str_section_body true( (Nat_big_num.of_int 0)) true tu.tu_die)) let pp_type_units c debug_string_section_body (type_units1: type_unit list) : string= (String.concat "" (Lem_list.map (pp_type_unit c debug_string_section_body) type_units1)) let parse_type_unit c (debug_str_section_body: byte_sequence0) (debug_abbrev_section_body: byte_sequence0) : ( type_unit option) parser= (fun (pc:parse_context) -> if Nat_big_num.equal (Byte_sequence.length0 pc.pc_bytes)( (Nat_big_num.of_int 0)) then PR_success( None, pc) else let (tuh, pc') = ((match parse_type_unit_header c pc with | PR_fail( s, pc') -> failwith ("parse_tuh_header fail: " ^ pp_parse_fail s pc') | PR_success( tuh, pc') -> (tuh,pc') )) in (* let _ = my_debug4 (pp_type_unit_header tuh) in *) let pc_abbrev = (let n = (tuh.tuh_cuh.cuh_debug_abbrev_offset) in {pc_bytes = ((match dropbytes n debug_abbrev_section_body with Success bs -> bs | Fail _ -> failwith "mydrop of debug_abbrev" )); pc_offset = n }) in let abbreviations_table1 = ((match parse_abbreviations_table c pc_abbrev with | PR_fail( s, pc_abbrev') -> failwith ("parse_abbrevations_table fail: " ^ pp_parse_fail s pc_abbrev') | PR_success( at, pc_abbrev') -> { at_offset=(pc_abbrev.pc_offset); at_table= at} )) in (* let _ = my_debug4 (pp_abbreviations_table abbreviations_table) in *) let find_abbreviation_declaration (ac:Nat_big_num.num) : abbreviation_declaration= ( (* let _ = my_debug4 ("find_abbreviation_declaration "^pphex ac) in *)myfindNonPure (fun ad -> Nat_big_num.equal ad.ad_abbreviation_code ac) abbreviations_table1.at_table) in (* let _ = my_debug3 (pp_abbreviations_table abbreviations_table) in *) (match parse_die c debug_str_section_body tuh.tuh_cuh find_abbreviation_declaration pc' with | PR_fail( s, pc'') -> failwith ("parse_die fail: " ^ pp_parse_fail s pc'') | PR_success( (None), pc'') -> failwith ("parse_die returned Nothing: " ^ pp_parse_context pc'') | PR_success( (Some die1), pc'') -> let tu = ({ tu_header = tuh; tu_abbreviations_table = abbreviations_table1; tu_die = die1; }) in PR_success( (Some tu), pc'') )) let parse_type_units c (debug_str_section_body: byte_sequence0) (debug_abbrev_section_body: byte_sequence0): ( type_unit list) parser= (parse_list (parse_type_unit c debug_str_section_body debug_abbrev_section_body)) (** location lists, pp and parsing *) (* readelf example Contents of the .debug_loc section: Offset Begin End Expression 00000000 0000000000400168 0000000000400174 (DW_OP_reg0 (r0)) 00000000 0000000000400174 0000000000400184 (DW_OP_GNU_entry_value: (DW_OP_reg0 (r0)); DW_OP_stack_value) 00000000 <End of list> 00000039 000000000040017c 0000000000400180 (DW_OP_lit1; DW_OP_stack_value) *) let pp_location_list_entry c (cuh:compilation_unit_header) (offset:Nat_big_num.num) (x:location_list_entry) : string= (" " ^ (pphex offset ^ (" " ^ (pphex x.lle_beginning_address_offset ^ (" " ^ (pphex x.lle_ending_address_offset ^ (" (" ^ (parse_and_pp_operations c cuh x.lle_single_location_description ^(")" ^ "\n"))))))))) let pp_base_address_selection_entry c (cuh:compilation_unit_header) (offset:Nat_big_num.num) (x:base_address_selection_entry) : string= (" " ^ (pphex offset ^ (" " ^ (pphex x.base_address ^ "\n")))) let pp_location_list_item c (cuh: compilation_unit_header) (offset: Nat_big_num.num) (x:location_list_item):string= ((match x with | LLI_lle lle -> pp_location_list_entry c cuh offset lle | LLI_base base -> pp_base_address_selection_entry c cuh offset base )) let pp_location_list c (cuh: compilation_unit_header) ((offset:Nat_big_num.num), (llis: location_list_item list)):string= (String.concat "" (Lem_list.map (pp_location_list_item c cuh offset) llis)) (* ^ " " ^ pphex offset ^ " <End of list>\n"*) let pp_loc c (cuh: compilation_unit_header) (lls: location_list list):string= (" Offset Begin End Expression\n" ^ String.concat "" (Lem_list.map (pp_location_list c cuh) lls)) (* Note that this is just pp'ing the raw location list data - Section 3.1.1 says: The applicable base address of a location list entry is determined by the closest preceding base address selection entry in the same location list. If there is no such selection entry, then the applicable base address defaults to the base address of the compilation unit. That is handled by the interpret_location_list below *) let parse_location_list_item c (cuh: compilation_unit_header) : ( location_list_item option) parser= (fun (pc:parse_context) -> pr_bind (parse_pair (parse_uint_address_size c cuh.cuh_address_size) (parse_uint_address_size c cuh.cuh_address_size) pc) (fun ((a1: Nat_big_num.num),(a2:Nat_big_num.num)) pc' -> (* let _ = my_debug4 ("offset="^pphex pc.pc_offset ^ " begin=" ^ pphex a1 ^ " end=" ^ pphex a2) in *) if Nat_big_num.equal a1( (Nat_big_num.of_int 0)) &&Nat_big_num.equal a2( (Nat_big_num.of_int 0)) then PR_success( None, pc') else if Nat_big_num.equal a1 (max_address cuh.cuh_address_size) then let x = (LLI_base { (*base_offset=pc.pc_offset;*) base_address=a1 }) in PR_success( (Some x (*(pc.pc_offset, x)*)), pc') else pr_bind (parse_uint16 c pc') (fun n pc'' -> pr_post_map1 (parse_n_bytes n pc'') (fun bs -> let x = (LLI_lle { (*lle_offset = pc.pc_offset;*) lle_beginning_address_offset = a1; lle_ending_address_offset = a2; lle_single_location_description = bs; }) in Some x (*(pc.pc_offset, x)*)) ) )) let parse_location_list c cuh : ( location_list option) parser= (fun (pc: parse_context) -> if Nat_big_num.equal (Byte_sequence.length0 pc.pc_bytes)( (Nat_big_num.of_int 0)) then PR_success( None, pc) else pr_post_map1 (parse_list (parse_location_list_item c cuh) pc) (fun llis -> (Some (pc.pc_offset, llis)))) let parse_location_list_list c cuh : location_list_list parser= (parse_list (parse_location_list c cuh)) let find_location_list dloc n : location_list= (myfindNonPure (fun (n',_)->Nat_big_num.equal n' n) dloc) (* fails if location list not found *) (* interpretation of a location list applies the base_address and LLI_base offsets to give a list indexed by concrete address ranges *) let rec interpret_location_list (base_address1: Nat_big_num.num) (llis: location_list_item list) : (Nat_big_num.num * Nat_big_num.num * single_location_description) list= ((match llis with | [] -> [] | LLI_base base::llis' -> interpret_location_list base.base_address llis' | LLI_lle lle :: llis' -> (Nat_big_num.add base_address1 lle.lle_beginning_address_offset,Nat_big_num.add base_address1 lle.lle_ending_address_offset, lle.lle_single_location_description) :: interpret_location_list base_address1 llis' )) (** range lists, pp and parsing *) (* example output from: aarch64-linux-gnu-objdump --dwarf=Ranges Contents of the .debug_ranges section: Offset Begin End 00000000 00000000004000fc 0000000000400114 00000000 000000000040011c 0000000000400128 00000000 <End of list> ... 00000380 0000000000400598 000000000040059c 00000380 00000000004005a0 00000000004005a4 00000380 00000000004005b4 00000000004005b8 00000380 00000000004005bc 00000000004005bc (start == end) 00000380 00000000004005c0 00000000004005c4 00000380 <End of list> *) let pp_range_list_entry c (cuh:compilation_unit_header) (offset:Nat_big_num.num) (x:range_list_entry) : string= (" " ^ (pphex offset ^ (" " ^ (pphex x.rle_beginning_address_offset ^ (" " ^ (pphex x.rle_ending_address_offset ^ ((if Nat_big_num.equal x.rle_beginning_address_offset x.rle_ending_address_offset then " (start == end)" else "") ^ "\n"))))))) let pp_range_list_item c (cuh: compilation_unit_header) (offset: Nat_big_num.num) (x:range_list_item):string= ((match x with | RLI_rle rle -> pp_range_list_entry c cuh offset rle | RLI_base base -> pp_base_address_selection_entry c cuh offset base )) let pp_range_list c (cuh: compilation_unit_header) ((offset:Nat_big_num.num), (rlis: range_list_item list)):string= (String.concat "" (Lem_list.map (pp_range_list_item c cuh offset) rlis) ^ (" " ^ (pphex offset ^ " <End of list>\n"))) let pp_ranges c (cuh: compilation_unit_header) (rls: range_list list):string= (" Offset Begin End\n" ^ String.concat "" (Lem_list.map (pp_range_list c cuh) rls)) (* Note that this is just pp'ing the raw range list data - see also the interpret_range_list below *) let parse_range_list_item c (cuh: compilation_unit_header) : ( range_list_item option) parser= (fun (pc:parse_context) -> pr_bind (parse_pair (parse_uint_address_size c cuh.cuh_address_size) (parse_uint_address_size c cuh.cuh_address_size) pc) (fun ((a1: Nat_big_num.num),(a2:Nat_big_num.num)) pc' -> (* let _ = my_debug4 ("offset="^pphex pc.pc_offset ^ " begin=" ^ pphex a1 ^ " end=" ^ pphex a2) in *) if Nat_big_num.equal a1( (Nat_big_num.of_int 0)) &&Nat_big_num.equal a2( (Nat_big_num.of_int 0)) then PR_success( None, pc') else if Nat_big_num.equal a1 (max_address cuh.cuh_address_size) then let x = (RLI_base { base_address=a2 }) in PR_success( (Some x), pc') else let x = (RLI_rle { rle_beginning_address_offset = a1; rle_ending_address_offset = a2; }) in PR_success( (Some x (*(pc.pc_offset, x)*)), pc') )) (* compiler output includes DW_AT_ranges attributes that point to proper suffixes of range lists. We support that by explicitly including each suffix - though one could be more efficient *) let rec expand_range_list_suffixes cuh (offset,(rlis: range_list_item list)) : range_list list= ((match rlis with | [] -> [] | [rli] -> [(offset,rlis)] | rli::rlis' -> (offset,rlis) :: expand_range_list_suffixes cuh (( Nat_big_num.add offset(Nat_big_num.mul( (Nat_big_num.of_int 2))cuh.cuh_address_size)),rlis') )) let parse_range_list c cuh : ( ( range_list list)option) parser= (fun (pc: parse_context) -> if Nat_big_num.equal (Byte_sequence.length0 pc.pc_bytes)( (Nat_big_num.of_int 0)) then PR_success( None, pc) else pr_post_map1 (parse_list (parse_range_list_item c cuh) pc) (fun rlis -> (Some (expand_range_list_suffixes cuh (pc.pc_offset, rlis))))) let parse_range_list_list c cuh : range_list_list parser= (pr_map2 List.concat (parse_list (parse_range_list c cuh))) let find_range_list dranges n : range_list option= (Lem_list.list_find_opt (fun (n',_)->Nat_big_num.equal n' n) dranges) (* fails if range list not found *) (* interpretation of a range list applies the base_address and RLI_base offsets to give a list of concrete address ranges *) let rec interpret_range_list (base_address1: Nat_big_num.num) (rlis: range_list_item list) : (Nat_big_num.num * Nat_big_num.num) list= ((match rlis with | [] -> [] | RLI_base base::rlis' -> interpret_range_list base.base_address rlis' | RLI_rle rle :: rlis' -> (Nat_big_num.add base_address1 rle.rle_beginning_address_offset,Nat_big_num.add base_address1 rle.rle_ending_address_offset) :: interpret_range_list base_address1 rlis' )) (** frame information, pp and parsing *) (* readelf example Contents of the .debug_frame section: 00000000 0000000c ffffffff CIE Version: 1 Augmentation: "" Code alignment factor: 4 Data alignment factor: -8 Return address column: 65 DW_CFA_def_cfa: r1 ofs 0 00000010 00000024 00000000 FDE cie=00000000 pc=100000b0..10000120 DW_CFA_advance_loc: 8 to 100000b8 DW_CFA_def_cfa_offset: 80 DW_CFA_offset: r31 at cfa-8 DW_CFA_advance_loc: 4 to 100000bc DW_CFA_def_cfa_register: r31 DW_CFA_advance_loc: 80 to 1000010c DW_CFA_def_cfa: r1 ofs 0 DW_CFA_nop DW_CFA_nop DW_CFA_nop DW_CFA_nop 00000038 00000024 00000000 FDE cie=00000000 pc=10000120..100001a4 DW_CFA_advance_loc: 16 to 10000130 DW_CFA_def_cfa_offset: 144 DW_CFA_offset_extended_sf: r65 at cfa+16 DW_CFA_offset: r31 at cfa-8 DW_CFA_advance_loc: 4 to 10000134 DW_CFA_def_cfa_register: r31 DW_CFA_advance_loc: 84 to 10000188 DW_CFA_def_cfa: r1 ofs 0 *) let pp_cfa_address a:string= (pphex a) let pp_cfa_block b:string= (ppbytes b) let pp_cfa_delta d:string= (pphex d) (*let pp_cfa_offset n = pphex n let pp_cfa_register r = show r*) let pp_cfa_sfoffset dict_Show_Show_a i:string= ( dict_Show_Show_a.show_method i) let pp_cfa_register dict_Show_Show_a r:string= ("r"^ dict_Show_Show_a.show_method r) (*TODO: arch-specific register names *) let pp_cfa_offset (i:Nat_big_num.num):string= (if Nat_big_num.equal i( (Nat_big_num.of_int 0)) then "" else if Nat_big_num.less i( (Nat_big_num.of_int 0)) then Nat_big_num.to_string i else "+" ^ Nat_big_num.to_string i) let pp_cfa_rule (cr:cfa_rule) : string= ((match cr with | CR_undefined -> "u" | CR_register( r, i) -> pp_cfa_register instance_Show_Show_Num_natural_dict r ^ pp_cfa_offset i | CR_expression bs -> "exp" )) let pp_register_rule (rr:register_rule) : string= ( (*TODO make this more readelf-like *)(match rr with | RR_undefined -> "u" | RR_same_value -> "s" | RR_offset i -> "c" ^ pp_cfa_offset i | RR_val_offset i -> "val(c" ^ (pp_cfa_offset i ^ ")") | RR_register r -> pp_cfa_register instance_Show_Show_Num_natural_dict r | RR_expression bs -> "exp" | RR_val_expression bs -> "val(exp)" | RR_architectural -> "" )) let pp_call_frame_instruction i:string= ((match i with | DW_CFA_advance_loc d -> "DW_CFA_advance_loc" ^ (" " ^ pp_cfa_delta d) | DW_CFA_offset( r, n) -> "DW_CFA_offset" ^ (" " ^ (pp_cfa_register instance_Show_Show_Num_natural_dict r ^ (" " ^ pp_cfa_offset ( n)))) | DW_CFA_restore r -> "DW_CFA_restore" ^ (" " ^ pp_cfa_register instance_Show_Show_Num_natural_dict r) | DW_CFA_nop -> "DW_CFA_nop" | DW_CFA_set_loc a -> "DW_CFA_set_loc" ^ (" " ^ pp_cfa_address a) | DW_CFA_advance_loc1 d -> "DW_CFA_advance_loc1" ^ (" " ^ pp_cfa_delta d) | DW_CFA_advance_loc2 d -> "DW_CFA_advance_loc2" ^ (" " ^ pp_cfa_delta d) | DW_CFA_advance_loc4 d -> "DW_CFA_advance_loc4" ^ (" " ^ pp_cfa_delta d) | DW_CFA_offset_extended( r, n) -> "DW_CFA_offset_extended" ^ (" " ^ (pp_cfa_register instance_Show_Show_Num_natural_dict r ^ (" " ^ pp_cfa_offset ( n)))) | DW_CFA_restore_extended r -> "DW_CFA_restore_extended" ^ (" " ^ pp_cfa_register instance_Show_Show_Num_natural_dict r) | DW_CFA_undefined r -> "DW_CFA_undefined" ^ (" " ^ pp_cfa_register instance_Show_Show_Num_natural_dict r) | DW_CFA_same_value r -> "DW_CFA_same_value" ^ (" " ^ pp_cfa_register instance_Show_Show_Num_natural_dict r) | DW_CFA_register( r1, r2) -> "DW_CFA_register" ^ (" " ^ (pp_cfa_register instance_Show_Show_Num_natural_dict r1 ^ (" " ^ pp_cfa_register instance_Show_Show_Num_natural_dict r2))) | DW_CFA_remember_state -> "DW_CFA_remember_state" | DW_CFA_restore_state -> "DW_CFA_restore_state" | DW_CFA_def_cfa( r, n) -> "DW_CFA_def_cfa" ^ (" " ^ (pp_cfa_register instance_Show_Show_Num_natural_dict r ^ (" " ^ pp_cfa_offset ( n)))) | DW_CFA_def_cfa_register r -> "DW_CFA_def_cfa_register" ^ (" " ^ pp_cfa_register instance_Show_Show_Num_natural_dict r) | DW_CFA_def_cfa_offset n -> "DW_CFA_def_cfa_offset" ^ (" " ^ pp_cfa_offset ( n)) | DW_CFA_def_cfa_expression b -> "DW_CFA_def_cfa_expression" ^ (" " ^ pp_cfa_block b) | DW_CFA_expression( r, b) -> "DW_CFA_expression" ^ (" " ^ (pp_cfa_register instance_Show_Show_Num_natural_dict r ^ (" " ^ pp_cfa_block b))) | DW_CFA_offset_extended_sf( r, i) -> "DW_CFA_offset_extended_sf" ^ (" " ^ (pp_cfa_register instance_Show_Show_Num_natural_dict r ^ (" " ^ pp_cfa_sfoffset instance_Show_Show_Num_integer_dict i))) | DW_CFA_def_cfa_sf( r, i) -> "DW_CFA_def_cfa_sf" ^ (" " ^ (pp_cfa_register instance_Show_Show_Num_natural_dict r ^ (" " ^ pp_cfa_sfoffset instance_Show_Show_Num_integer_dict i))) | DW_CFA_def_cfa_offset_sf i -> "DW_CFA_def_cfa_offset_sf" ^ (" " ^ pp_cfa_sfoffset instance_Show_Show_Num_integer_dict i) | DW_CFA_val_offset( r, n) -> "DW_CFA_val_offset" ^ (" " ^ (pp_cfa_register instance_Show_Show_Num_natural_dict r ^ (" " ^ pp_cfa_offset ( n)))) | DW_CFA_val_offset_sf( r, i) -> "DW_CFA_val_offset_sf" ^ (pp_cfa_register instance_Show_Show_Num_natural_dict r ^ (" " ^ pp_cfa_sfoffset instance_Show_Show_Num_integer_dict i)) | DW_CFA_val_expression( r, b) -> "DW_CFA_val_expression" ^ (" " ^ (pp_cfa_register instance_Show_Show_Num_natural_dict r ^ (" " ^ pp_cfa_block b))) | DW_CFA_AARCH64_negate_ra_state -> "DW_CFA_AARCH64_negate_ra_state" | DW_CFA_unknown bt -> "DW_CFA_unknown" ^ (" " ^ hex_string_of_byte bt) )) let pp_call_frame_instructions is:string= (String.concat "" (Lem_list.map (fun i -> " " ^ (pp_call_frame_instruction i ^ "\n")) is)) let parser_of_call_frame_argument_type c cuh (cfat: call_frame_argument_type) : call_frame_argument_value parser= ((match cfat with | CFAT_address -> pr_map2 (fun n -> CFAV_address n) (parse_uint_address_size c cuh.cuh_address_size) | CFAT_delta1 -> pr_map2 (fun n -> CFAV_delta n) (parse_uint8) | CFAT_delta2 -> pr_map2 (fun n -> CFAV_delta n) (parse_uint16 c) | CFAT_delta4 -> pr_map2 (fun n -> CFAV_delta n) (parse_uint32 c) | CFAT_delta_ULEB128 -> pr_map2 (fun n -> CFAV_delta n) (parse_ULEB128) | CFAT_offset -> pr_map2 (fun n -> CFAV_offset n) (parse_ULEB128) | CFAT_sfoffset -> pr_map2 (fun n -> CFAV_sfoffset n) (parse_SLEB128) | CFAT_register -> pr_map2 (fun n -> CFAV_register n) (parse_ULEB128) | CFAT_block -> (fun pc -> pr_bind (parse_ULEB128 pc) (fun n pc' -> pr_map (fun bs -> CFAV_block bs) (parse_n_bytes n pc'))) )) let parse_call_frame_instruction c cuh : ( call_frame_instruction option) parser= (fun pc -> (match read_char pc.pc_bytes with | Fail _ -> PR_success( None, pc) | Success (b,bs') -> let pc' = ({ pc_bytes = bs'; pc_offset = (Nat_big_num.add pc.pc_offset( (Nat_big_num.of_int 1))) }) in let ch = (Uint32_wrapper.of_int (Char.code b)) in let high_bits = (Uint32_wrapper.logand ch (Uint32_wrapper.of_bigint( (Nat_big_num.of_int 192)))) in let low_bits = (Uint32_wrapper.to_bigint (Uint32_wrapper.logand ch (Uint32_wrapper.of_bigint( (Nat_big_num.of_int 63))))) in if high_bits = Uint32_wrapper.of_bigint( (Nat_big_num.of_int 0)) then (match lookup_abCde_de instance_Basic_classes_Eq_Num_natural_dict low_bits call_frame_instruction_encoding with | Some ((args: call_frame_argument_type list), result) -> let ps = (Lem_list.map (parser_of_call_frame_argument_type c cuh) args) in let p = (pr_post_map (parse_parser_list ps) result) in (match p pc' with | PR_success( (Some cfi), pc'') -> PR_success( (Some cfi), pc'') | PR_success( (None), pc'') -> failwith "bad call frame instruction argument 1" | PR_fail( s, pc'') -> failwith "bad call frame instruction argument 2" ) | None -> (*Assert_extra.failwith ("can't parse " ^ show b ^ " as call frame instruction")*) PR_success( (Some (DW_CFA_unknown b)), pc') ) else if high_bits = Uint32_wrapper.of_bigint( (Nat_big_num.of_int 64)) then PR_success( (Some (DW_CFA_advance_loc low_bits)), pc') else if high_bits = Uint32_wrapper.of_bigint( (Nat_big_num.of_int 192)) then PR_success( (Some (DW_CFA_restore low_bits)), pc') else let p = (parser_of_call_frame_argument_type c cuh CFAT_offset) in (match p pc' with | PR_success( (CFAV_offset n), pc'') -> PR_success( (Some (DW_CFA_offset( low_bits, n))), pc'') | PR_success( _, pc'') -> failwith "bad call frame instruction argument 3" | PR_fail( s, pc'') -> failwith "bad call frame instruction argument 4" ) )) let parse_call_frame_instructions c cuh : ( call_frame_instruction list) parser= (parse_list (parse_call_frame_instruction c cuh)) (*val parse_and_pp_call_frame_instructions : p_context -> compilation_unit_header -> byte_sequence -> string*) let parse_and_pp_call_frame_instructions c cuh bs:string= (let pc = ({pc_bytes = bs; pc_offset =( (Nat_big_num.of_int 0)) }) in (match parse_call_frame_instructions c cuh pc with | PR_fail( s, pc') -> "parse_call_frame_instructions fail: " ^ pp_parse_fail s pc' | PR_success( is, pc') -> pp_call_frame_instructions is ^ (if not (Nat_big_num.equal (Byte_sequence.length0 pc'.pc_bytes)( (Nat_big_num.of_int 0))) then " Warning: extra non-parsed bytes" else "") )) let pp_call_frame_instructions' c cuh bs:string= ( (* ppbytes bs ^ "\n" *)parse_and_pp_call_frame_instructions c cuh bs) let pp_cie c cuh cie1:string= (pphex cie1.cie_offset ^ (" " ^ (pphex cie1.cie_length ^ (" " ^ (pphex cie1.cie_id ^ (" CIE\n" ^ (" Version: " ^ (Nat_big_num.to_string cie1.cie_version ^ ("\n" ^ (" Augmentation: \""^ (string_of_string (string_of_byte_sequence cie1.cie_augmentation) ^ ("\"\n" ^ (" Code alignment factor: " ^ (Nat_big_num.to_string cie1.cie_code_alignment_factor ^ ("\n" ^ (" Data alignment factor: " ^ (Nat_big_num.to_string cie1.cie_data_alignment_factor ^ ("\n" ^ (" Return address column: " ^ (Nat_big_num.to_string cie1.cie_return_address_register ^ ("\n" ^ ("\n" ^ (ppbytes cie1.cie_initial_instructions_bytes ^ ("\n" ^ pp_call_frame_instructions cie1.cie_initial_instructions)))))))))))))))))))))))) (* cie_address_size: natural; (* not shown by readelf - must match compilation unit *)*) (* cie_segment_size: natural; (* not shown by readelf *)*) (* readelf says "Return address column", but the DWARF spec says "Return address register" *) let pp_fde c cuh fde1:string= (pphex fde1.fde_offset ^ (" " ^ (pphex fde1.fde_length ^ (" " ^ (pphex fde1.fde_cie_pointer (* not what this field of readelf output is *) ^ (" FDE" ^ (" cie=" ^ (pphex fde1.fde_cie_pointer (* duplicated?? *) ^ (" pc=" ^ ((match fde1.fde_initial_location_segment_selector with None -> "" | Some segment_selector -> "("^(pphex segment_selector^")") ) ^ (pphex fde1.fde_initial_location_address ^ (".." ^ (pphex ( Nat_big_num.add fde1.fde_initial_location_address fde1.fde_address_range) ^ ("\n" ^ (ppbytes fde1.fde_instructions_bytes ^ ("\n" ^ pp_call_frame_instructions fde1.fde_instructions)))))))))))))))) let pp_frame_info_element c cuh fie:string= ((match fie with | FIE_cie cie1 -> pp_cie c cuh cie1 | FIE_fde fde1 -> pp_fde c cuh fde1 )) let pp_frame_info c cuh fi:string= ("Contents of the .debug_frame section:\n\n" ^ (String.concat "\n" (Lem_list.map (pp_frame_info_element c cuh) fi) ^ "\n")) let rec find_cie fi cie_id1:cie= ((match fi with | [] -> failwith "find_cie: cie_id not found" | FIE_fde _ :: fi' -> find_cie fi' cie_id1 | FIE_cie cie1 :: fi' -> if Nat_big_num.equal cie_id1 cie1.cie_offset then cie1 else find_cie fi' cie_id1 )) let parse_initial_location c cuh mss mas' : (( Nat_big_num.num option) * Nat_big_num.num) parser= ( (*(segment selector and target address)*) (* assume segment selector size is zero unless given explicitly. Probably we need to do something architecture-specific for earlier dwarf versions?*)parse_pair (parse_uint_segment_selector_size c ((match mss with Some n -> n | None -> (Nat_big_num.of_int 0) ))) (parse_uint_address_size c ((match mas' with Some n -> n | None -> cuh.cuh_address_size )))) let parse_call_frame_instruction_bytes offset' ul:parse_context ->(Byte_sequence_wrapper.byte_sequence)parse_result= (fun (pc: parse_context) -> parse_n_bytes ( Nat_big_num.sub_nat ul ( Nat_big_num.sub_nat pc.pc_offset offset')) pc) let parse_frame_info_element c cuh (fi: frame_info_element list) : frame_info_element parser= (parse_dependent (pr_with_pos (parse_dependent_pair (parse_unit_length c) (fun (df,ul) -> pr_with_pos (parse_uintDwarfN c df) (* CIE_id (cie) or CIE_pointer (fde) *) ))) (fun (offset,((df,ul),(offset',cie_id1))) -> if ( Nat_big_num.equal cie_id1 (match df with | Dwarf32 -> natural_of_hex "0xffffffff" | Dwarf64 -> natural_of_hex "0xffffffffffffffff" )) then (* parse cie *) pr_post_map (parse_pair (parse_dependent_pair parse_uint8 (* version *) (fun v -> parse_triple parse_string (* augmentation *) (if Nat_big_num.equal v( (Nat_big_num.of_int 4)) ||Nat_big_num.equal v( (Nat_big_num.of_int 46)) then pr_post_map parse_uint8 (fun i->Some i) else pr_return None) (* address_size *) (if Nat_big_num.equal v( (Nat_big_num.of_int 4)) ||Nat_big_num.equal v( (Nat_big_num.of_int 46)) then pr_post_map parse_uint8 (fun i->Some i) else pr_return None))) (* segment_size *) (parse_quadruple parse_ULEB128 (* code_alignment_factor *) parse_SLEB128 (* data_alignment_factor *) parse_ULEB128 (* return address register *) (parse_call_frame_instruction_bytes offset' ul))) (fun ( (v,(aug,(mas',mss))), (caf,(daf,(rar,bs))) ) -> let pc = ({pc_bytes = bs; pc_offset =( (Nat_big_num.of_int 0)) }) in (match parse_call_frame_instructions c cuh pc with | PR_success( is, _) -> FIE_cie ( { cie_offset = offset; cie_length = ul; cie_id = cie_id1; cie_version = v; cie_augmentation = aug; cie_address_size = mas'; cie_segment_size = mss; cie_code_alignment_factor = caf; cie_data_alignment_factor = daf; cie_return_address_register = rar; cie_initial_instructions_bytes = bs; cie_initial_instructions = is; }) | PR_fail( s, _) -> failwith s ) ) else (* parse fde *) let cie1 = (find_cie fi cie_id1) in (* let _ = my_debug4 (pp_cie c cuh cie) in *) pr_post_map (parse_triple (parse_initial_location c cuh cie1.cie_segment_size cie1.cie_address_size) (*(segment selector and target address)*) (parse_uint_address_size c ((match cie1.cie_address_size with Some n -> n | None -> cuh.cuh_address_size ))) (* address_range (target address) *) (parse_call_frame_instruction_bytes offset' ul) ) (fun ( (ss,adr), (ar, bs)) -> let pc = ({pc_bytes = bs; pc_offset =( (Nat_big_num.of_int 0)) }) in (match parse_call_frame_instructions c cuh pc with | PR_success( is, _) -> FIE_fde ( { fde_offset = offset; fde_length = ul; fde_cie_pointer = cie_id1; fde_initial_location_segment_selector = ss; fde_initial_location_address = adr; fde_address_range = ar; fde_instructions_bytes = bs; fde_instructions = is; } ) | PR_fail( s, _) -> failwith s ) ) )) (* you can't even parse an fde without accessing the cie it refers to (to determine the segment selector size). Gratuitous complexity or what? Hence the following, which should be made more tail-recursive. *) (*val parse_dependent_list' : forall 'a. (list 'a -> parser 'a) -> list 'a -> parser (list 'a)*) let rec parse_dependent_list' p1 acc:parse_context ->('a list)parse_result= (fun pc -> if Nat_big_num.equal (Byte_sequence.length0 pc.pc_bytes)( (Nat_big_num.of_int 0)) then PR_success( (List.rev acc), pc) else pr_bind (p1 acc pc) (fun x pc' -> parse_dependent_list' p1 (x::acc) pc')) (*val parse_dependent_list : forall 'a. (list 'a -> parser 'a) -> parser (list 'a)*) let parse_dependent_list p1:parse_context ->('a list)parse_result= (parse_dependent_list' p1 []) let parse_frame_info c cuh : frame_info parser= (parse_dependent_list (parse_frame_info_element c cuh)) (** line numbers .debug_line, pp and parsing *) let pp_line_number_file_entry lnfe:string= ("lnfe_path = " ^ (string_of_byte_sequence lnfe.lnfe_path ^ ("\n" ^ ("lnfe_directory_index " ^ (Nat_big_num.to_string lnfe.lnfe_directory_index ^ ("\n" ^ ("lnfe_last_modification = " ^ (Nat_big_num.to_string lnfe.lnfe_last_modification ^ ("\n" ^ ("lnfe_length = " ^ (Nat_big_num.to_string lnfe.lnfe_length ^ "\n"))))))))))) let pp_line_number_header lnh:string= ("offset = " ^ (pphex lnh.lnh_offset ^ ("\n" ^ ("dwarf_format = " ^ (pp_dwarf_format lnh.lnh_dwarf_format ^ ("\n" ^ ("unit_length = " ^ (Nat_big_num.to_string lnh.lnh_unit_length ^ ("\n" ^ ("version = " ^ (Nat_big_num.to_string lnh.lnh_version ^ ("\n" ^ ("header_length = " ^ (Nat_big_num.to_string lnh.lnh_header_length ^ ("\n" ^ ("minimum_instruction_length = " ^ (Nat_big_num.to_string lnh.lnh_minimum_instruction_length ^ ("\n" ^ ("maximum_operations_per_instruction = " ^ (Nat_big_num.to_string lnh.lnh_maximum_operations_per_instruction ^ ("\n" ^ ("default_is_stmt = " ^ (string_of_bool lnh.lnh_default_is_stmt ^ ("\n" ^ ("line_base = " ^ (Nat_big_num.to_string lnh.lnh_line_base ^ ("\n" ^ ("line_range = " ^ (Nat_big_num.to_string lnh.lnh_line_range ^ ("\n" ^ ("opcode_base = " ^ (Nat_big_num.to_string lnh.lnh_opcode_base ^ ("\n" ^ ("standard_opcode_lengths = " ^ (string_of_list instance_Show_Show_Num_natural_dict lnh.lnh_standard_opcode_lengths ^ ("\n" ^ ("comp_dir = " ^ (string_of_maybe instance_Show_Show_string_dict lnh.lnh_comp_dir ^ ("\n" ^ ("include_directories = " ^ (String.concat ", " (Lem_list.map string_of_byte_sequence lnh.lnh_include_directories) ^ ("\n" ^ ("file_entries = \n\n" ^ (String.concat "\n" (Lem_list.map pp_line_number_file_entry lnh.lnh_file_entries) ^ "\n")))))))))))))))))))))))))))))))))))))))))))) let pp_line_number_operation lno:string= ((match lno with | DW_LNS_copy -> "DW_LNS_copy" | DW_LNS_advance_pc n -> "DW_LNS_advance_pc" ^ (" " ^ Nat_big_num.to_string n) | DW_LNS_advance_line i -> "DW_LNS_advance_line" ^ (" " ^ Nat_big_num.to_string i) | DW_LNS_set_file n -> "DW_LNS_set_file" ^ (" " ^ Nat_big_num.to_string n) | DW_LNS_set_column n -> "DW_LNS_set_column" ^ (" " ^ Nat_big_num.to_string n) | DW_LNS_negate_stmt -> "DW_LNS_negate_stmt" | DW_LNS_set_basic_block -> "DW_LNS_set_basic_block" | DW_LNS_const_add_pc -> "DW_LNS_const_add_pc" | DW_LNS_fixed_advance_pc n -> "DW_LNS_fixed_advance_pc" ^ (" " ^ Nat_big_num.to_string n) | DW_LNS_set_prologue_end -> "DW_LNS_set_prologue_end" | DW_LNS_set_epilogue_begin -> "DW_LNS_set_epilogue_begin" | DW_LNS_set_isa n -> "DW_LNS_set_isa" ^ (" " ^ Nat_big_num.to_string n) | DW_LNE_end_sequence -> "DW_LNE_end_sequence" | DW_LNE_set_address n -> "DW_LNE_set_address" ^ (" " ^ pphex n) | DW_LNE_define_file( s, n1, n2, n3) -> "DW_LNE_define_file" ^ (" " ^ (Byte_sequence_wrapper.to_string s ^ (" " ^ (Nat_big_num.to_string n1 ^ (" " ^ (Nat_big_num.to_string n2 ^ (" " ^ Nat_big_num.to_string n3))))))) | DW_LNE_set_discriminator n -> "DW_LNE_set_discriminator" ^ (" " ^ Nat_big_num.to_string n) | DW_LN_special n -> "DW_LN_special" ^ (" " ^ Nat_big_num.to_string n) )) let pp_line_number_program lnp:string= (pp_line_number_header lnp.lnp_header ^ ("[" ^ (String.concat ", " (Lem_list.map pp_line_number_operation lnp.lnp_operations) ^ "]\n"))) let parse_line_number_file_entry : ( line_number_file_entry option) parser= (parse_dependent (parse_non_empty_string) (fun ms -> (match ms with | None -> pr_return None | Some s -> pr_post_map (parse_triple parse_ULEB128 parse_ULEB128 parse_ULEB128 ) (fun (n1,(n2,n3)) -> (Some { lnfe_path = s; lnfe_directory_index = n1; lnfe_last_modification = n2; lnfe_length = n3; } ) ) ) )) let parse_line_number_header c (comp_dir: string option) : line_number_header parser= (parse_dependent ((pr_with_pos (parse_unit_length c) )) (fun (pos,(df,ul)) -> (* parse_dependent_pair (parse_pair (parse_triple (parse_uint16 c) (* version *) (parse_uintDwarfN c df) (* header_length *) (parse_uint8) (* minimum_instruction_length *) (* (parse_uint8) (* maximum_operations_per_instruction NOT IN DWARF 2*)*) ) (parse_quadruple (parse_uint8) (* default_is_stmt *) (parse_sint8) (* line_base *) (parse_uint8) (* line_range *) (parse_uint8) (* opcode_base *) )) (fun ((v,(hl,(minil(*,maxopi*)))),(dis,(lb,(lr,ob)))) -> *) (parse_dependent (parse_dependent_pair (parse_uint16 c) (* version *) (fun v -> (parse_pair (parse_triple (parse_uintDwarfN c df) (* header_length *) (parse_uint8) (* minimum_instruction_length *) (if Nat_big_num.less v( (Nat_big_num.of_int 4)) then (* maximum_operations_per_instruction*)(* NOT IN DWARF 2 or 3; in DWARF 4*) (parse_uint8_constant( (Nat_big_num.of_int 1))) else (parse_uint8) )) (parse_quadruple (parse_uint8) (* default_is_stmt *) (parse_sint8) (* line_base *) (parse_uint8) (* line_range *) (parse_uint8) (* opcode_base *) )))) (fun ((v,(((hl,(minil,maxopi))),(dis,(lb,(lr,ob)))))) -> pr_post_map (parse_triple (pr_post_map (parse_n_bytes (Nat_big_num.sub_nat ob( (Nat_big_num.of_int 1)))) (fun bs -> Lem_list.map natural_of_byte (byte_list_of_byte_sequence bs))) (* standard_opcode_lengths *) ((*pr_return [[]]*) parse_list parse_non_empty_string) (* include_directories *) (parse_list parse_line_number_file_entry) (* file names *) ) (fun (sols, (ids, fns)) -> { lnh_offset = pos; lnh_dwarf_format = df; lnh_unit_length = ul; lnh_version = v; lnh_header_length = hl; lnh_minimum_instruction_length = minil; lnh_maximum_operations_per_instruction = maxopi; lnh_default_is_stmt = (not (Nat_big_num.equal dis( (Nat_big_num.of_int 0)))); lnh_line_base = lb; lnh_line_range = lr; lnh_opcode_base = ob; lnh_standard_opcode_lengths = sols; lnh_include_directories = ids; lnh_file_entries = fns; lnh_comp_dir = comp_dir; } ) ) ) ) ) let parser_of_line_number_argument_type c (cuh: compilation_unit_header) (lnat: line_number_argument_type) : line_number_argument_value parser= ((match lnat with | LNAT_address -> pr_map2 (fun n -> LNAV_address n) (parse_uint_address_size c cuh.cuh_address_size) | LNAT_ULEB128 -> pr_map2 (fun n -> LNAV_ULEB128 n) (parse_ULEB128) | LNAT_SLEB128 -> pr_map2 (fun i -> LNAV_SLEB128 i) (parse_SLEB128) | LNAT_uint16 -> pr_map2 (fun n -> LNAV_uint16 n) (parse_uint16 c) | LNAT_string -> pr_map2 (fun s -> LNAV_string s) (parse_string) )) let parse_line_number_operation c (cuh: compilation_unit_header) (lnh: line_number_header) : line_number_operation parser= (parse_dependent parse_uint8 (fun opcode -> if Nat_big_num.equal opcode( (Nat_big_num.of_int 0)) then (* parse extended opcode *) parse_dependent (parse_pair parse_ULEB128 parse_uint8) (fun (size2,opcode') -> (match lookup_aBcd_acd instance_Basic_classes_Eq_Num_natural_dict opcode' line_number_extended_encodings with | Some (_, arg_types, result) -> let ps = (Lem_list.map (parser_of_line_number_argument_type c cuh) arg_types) in parse_demaybe ("parse_line_number_operation fail") (pr_post_map (parse_parser_list ps) result ) | None -> failwith ("parse_line_number_operation extended opcode not found: " ^ Nat_big_num.to_string opcode') )) (* it's not clear what the ULEB128 size field is for, as the extended opcides all seem to have well-defined sizes. perhaps there can be extra padding that needs to be absorbed? *) else if Nat_big_num.greater_equal opcode lnh.lnh_opcode_base then (* parse special opcode *) let adjusted_opcode = (Nat_big_num.sub_nat opcode lnh.lnh_opcode_base) in pr_return (DW_LN_special adjusted_opcode) else (* parse standard opcode *) (match lookup_aBcd_acd instance_Basic_classes_Eq_Num_natural_dict opcode line_number_standard_encodings with | Some (_, arg_types, result) -> let ps = (Lem_list.map (parser_of_line_number_argument_type c cuh) arg_types) in parse_demaybe ("parse_line_number_operation fail") (pr_post_map (parse_parser_list ps) result) | None -> failwith ("parse_line_number_operation standard opcode not found: " ^ Nat_big_num.to_string opcode) (* the standard_opcode_lengths machinery is intended to allow vendor specific extension instructions to be parsed and ignored, but here we couldn't usefully process such instructions in any case, so we just fail *) ))) let parse_line_number_operations c (cuh:compilation_unit_header) (lnh:line_number_header) : ( line_number_operation list) parser= (parse_list (parse_maybe (parse_line_number_operation c cuh lnh))) (* assume operations start immediately after the header - not completely clear in DWARF whether the header_length is just an optimisation or whether it's intended to allow the operations to start later *) (* line number operations have no no-op and no termination operation, so we have to cut down the available bytes to the right length *) let parse_line_number_program c (cuh:compilation_unit_header) (comp_dir: string option) : line_number_program parser= (parse_dependent (parse_line_number_header c comp_dir) (fun lnh -> let byte_count_of_operations = (Nat_big_num.sub_nat lnh.lnh_unit_length ( Nat_big_num.add (Nat_big_num.add lnh.lnh_header_length( (Nat_big_num.of_int 2))) ((match lnh.lnh_dwarf_format with Dwarf32 -> (Nat_big_num.of_int 4) | Dwarf64 -> (Nat_big_num.of_int 8) )))) in pr_post_map (parse_restrict_length byte_count_of_operations (parse_line_number_operations c cuh lnh) ) (fun ops -> { lnp_header = lnh; lnp_operations = ops; }) )) (*TODO: this should use find_natural_attribute_value_of_die *) let line_number_offset_of_compilation_unit c cu:Nat_big_num.num= ((match find_attribute_value "DW_AT_stmt_list" cu.cu_die with | Some (AV_sec_offset n) -> n | Some (AV_block( n, bs)) -> natural_of_bytes c.endianness bs (* a 32-bit MIPS example used a 4-byte AV_block not AV_sec_offset *) | Some _ -> (failwith ("compilation unit DW_AT_stmt_list attribute was not an AV_sec_offset" ^ pp_compilation_unit_header cu.cu_header)) | _ -> failwith ("compilation unit did not have a DW_AT_stmt_list attribute\n" ^ (pp_compilation_unit_header cu.cu_header ^ "\n")) )) let line_number_program_of_compilation_unit d cu:line_number_program= (let c = (p_context_of_d d) in let offset = (line_number_offset_of_compilation_unit c cu) in (match Lem_list.list_find_opt (fun lnp -> Nat_big_num.equal lnp.lnp_header.lnh_offset offset) d.d_line_info with | None -> failwith "compilation unit line number offset not found" | Some lnp ->lnp )) let filename d cu n:(string)option= (let lnp = (line_number_program_of_compilation_unit d cu) in if Nat_big_num.equal n( (Nat_big_num.of_int 0)) then None else (match mynth ( Nat_big_num.sub_nat n( (Nat_big_num.of_int 1))) lnp.lnp_header.lnh_file_entries with | Some lnfe -> Some (string_of_byte_sequence lnfe.lnfe_path) | None -> failwith ("line number file entry not found") )) let unpack_file_entry lnh file : unpacked_file_entry= ((match mynth ( Nat_big_num.sub_nat file( (Nat_big_num.of_int 1))) lnh.lnh_file_entries with | Some lnfe -> let directory = (if Nat_big_num.equal lnfe.lnfe_directory_index( (Nat_big_num.of_int 0)) then None else (match mynth ( Nat_big_num.sub_nat lnfe.lnfe_directory_index( (Nat_big_num.of_int 1))) lnh.lnh_include_directories with | Some d -> Some (string_of_byte_sequence d) | None -> Some "<unpack_file_entry: directory entry not found>" )) in (lnh.lnh_comp_dir, directory, string_of_byte_sequence lnfe.lnfe_path) | None -> (None,None,"<unpack_file_entry: file entry not found>") )) let pp_ufe (((mcomp_dir,mdir,file) as ufe) : unpacked_file_entry) : string= (file ^ (" dir=" ^ ((match mdir with | Some s->s|None->"<none>" ) ^ (" comp_dir=" ^ (match mcomp_dir with | Some s->s|None->"<none>" ))))) let pp_ud (((((mcomp_dir,mdir,file) as ufe) : unpacked_file_entry), (line:int), (subprogram_name:string)) : unpacked_decl) : string= (file ^ (":" ^ (Stdlib.string_of_int line ^ (" " ^ (subprogram_name ^ (" dir=" ^ ((match mdir with | Some s->s|None->"<none>" ) ^ (" comp_dir=" ^ (match mcomp_dir with | Some s->s|None->"<none>" ))))))))) let pp_ufe_brief (((mcomp_dir,mdir,file) as ufe) : unpacked_file_entry) : string= file (* ^ " dir=" ^ match mdir with | Just s->s|Nothing->"<none>" end ^ " comp_dir=" ^ match mcomp_dir with | Just s->s|Nothing->"<none>" end *) let parse_line_number_info c str (d_line: byte_sequence0) (cu: compilation_unit) : line_number_program= (let comp_dir = (find_string_attribute_value_of_die "DW_AT_comp_dir" str cu.cu_die) in let f n= (let d_line' = ((match dropbytes n d_line with Success xs -> xs | Fail _ -> failwith "parse_line_number_info drop" )) in let pc = ({ pc_bytes = d_line'; pc_offset = n}) in (match parse_line_number_program c cu.cu_header comp_dir pc with | PR_success( lnp, pc') -> (*let _ = print_endline (pp_line_number_program lnp) in*) lnp | PR_fail( s, pc') -> failwith ("parse_line_number_header failed: " ^ s) )) in f (line_number_offset_of_compilation_unit c cu)) let parse_line_number_infos c str debug_line_section_body compilation_units1:(line_number_program)list= (Lem_list.map (parse_line_number_info c str debug_line_section_body) compilation_units1) let pp_line_info li:string= (String.concat "\n" (Lem_list.map (pp_line_number_program) li)) (** all dwarf info: pp and parsing *) (* roughly matching objdump --dwarf=abbrev,info *) let pp_dwarf_like_objdump d:string= (let c = (p_context_of_d d) in "" (* ^ "\n*** compilation unit abbreviation table ***\n" *) ^ ("Contents of the .debug_abbrev section:\n\n" ^ (" Number TAG (0x0)\n" ^ (pp_abbreviations_tables d (* ^ "\n*** compilation unit die tree ***\n"*) (* "\n************** .debug_info section - abbreviated *****************\n" ^ pp_compilation_units_abbrev c d.d_str d.d_compilation_units ^*) (* ^"\n************** .debug_info section - full ************************\n"*) ^ ("\nContents of the .debug_info section:\n\n" ^ pp_compilation_units c false (*false for no indent, like objdump; true for nice indent *) d.d_str d.d_compilation_units))))) let pp_dwarf d:string= (let c = (p_context_of_d d) in (* "\n************** .debug_info section - abbreviated *****************\n" ^ pp_compilation_units_abbrev c d.d_str d.d_compilation_units ^*) "\n************** .debug_info section - full ************************\n" ^ (pp_compilation_units c true d.d_str d.d_compilation_units ^ ("\n************** .debug_loc section: location lists ****************\n" ^ (let (cuh_default : compilation_unit_header) = (let cu = (myhead d.d_compilation_units) in cu.cu_header) in pp_loc c cuh_default d.d_loc ^ ("\n************** .debug_ranges section: range lists ****************\n" ^ (pp_ranges c cuh_default d.d_ranges ^ ("\n************** .debug_frame section: frame info ****************\n" ^ (pp_frame_info c cuh_default d.d_frame_info ^ ("\n************** .debug_line section: line number info ****************\n" ^ pp_line_info d.d_line_info))))))))) (* TODO: don't use lists of bytes here! *) let parse_dwarf c (debug_info_section_body: byte_sequence0) (debug_abbrev_section_body: byte_sequence0) (debug_str_section_body: byte_sequence0) (debug_loc_section_body: byte_sequence0) (debug_ranges_section_body: byte_sequence0) (debug_frame_section_body: byte_sequence0) (debug_line_section_body: byte_sequence0) : dwarf= (let pc_info = ({pc_bytes = debug_info_section_body; pc_offset =( (Nat_big_num.of_int 0)) }) in let compilation_units1 = ((match parse_compilation_units c debug_str_section_body debug_abbrev_section_body pc_info with | PR_fail( s, pc_info') -> failwith ("parse_compilation_units: " ^ pp_parse_fail s pc_info') | PR_success( cus, pc_info') -> cus )) in (*let _ = my_debug5 (pp_compilation_units c debug_str_section_body compilation_units) in*) (* the DWARF4 spec doesn't seem to specify the address size used in the .debug_loc section, so we (hackishly) take it from the first compilation unit *) let (cuh_default : compilation_unit_header) = (let cu = (myhead compilation_units1) in cu.cu_header) in let pc_loc = ({pc_bytes = debug_loc_section_body; pc_offset =( (Nat_big_num.of_int 0)) }) in let loc = ((match parse_location_list_list c cuh_default pc_loc with | PR_fail( s, pc_info') -> failwith ("parse_location_list: " ^ pp_parse_fail s pc_info') | PR_success( loc, pc_loc') -> loc )) in let pc_ranges = ({pc_bytes = debug_ranges_section_body; pc_offset =( (Nat_big_num.of_int 0)) }) in let ranges = ((match parse_range_list_list c cuh_default pc_ranges with | PR_fail( s, pc_info') -> failwith ("parse_range_list: " ^ pp_parse_fail s pc_info') | PR_success( r, pc_loc') -> r )) in let pc_frame = ({pc_bytes = debug_frame_section_body; pc_offset =( (Nat_big_num.of_int 0)) }) in let fi = ( (* let _ = my_debug5 ("debug_frame_section_body:\n" ^ ppbytes2 0 debug_frame_section_body) in *)(match parse_frame_info c cuh_default pc_frame with | PR_fail( s, pc_info') -> failwith ("parse_frame_info: " ^ pp_parse_fail s pc_info') | PR_success( fi, pc_loc') -> fi )) in let li = (parse_line_number_infos c debug_str_section_body debug_line_section_body compilation_units1) in { d_endianness = (c.endianness); d_str = debug_str_section_body; d_compilation_units = compilation_units1; d_type_units = ([]); d_loc = loc; d_ranges = ranges; d_frame_info = fi; d_line_info = li; }) (*val extract_section_body : elf_file -> string -> bool -> p_context * natural * byte_sequence*) let extract_section_body (f:elf_file) (section_name:string) (strict1: bool):p_context*Nat_big_num.num*Byte_sequence_wrapper.byte_sequence= (let (en: Endianness.endianness) = ((match f with | ELF_File_32 f32 -> Elf_header.get_elf32_header_endianness f32.Elf_file.elf32_file_header | ELF_File_64 f64 -> Elf_header.get_elf64_header_endianness f64.Elf_file.elf64_file_header )) in let (c: p_context) = ({ endianness = en }) in (match f with | ELF_File_32 f32 -> let sections = (List.filter (fun x -> x.Elf_interpreted_section.elf32_section_name_as_string = section_name ) f32.elf32_file_interpreted_sections) in (match sections with | [section] -> let section_addr = (section.Elf_interpreted_section.elf32_section_addr) in let section_body = (section.Elf_interpreted_section.elf32_section_body) in (* let _ = my_debug4 (section_name ^ (": \n" ^ (Elf_interpreted_section.string_of_elf32_interpreted_section section ^ "\n" * ^ " body = " ^ ppbytes2 0 section_body ^ "\n"))) in *) (c,section_addr,section_body) | [] -> if strict1 then failwith ("" ^ (section_name ^ " section not present")) else (c, (Nat_big_num.of_int 0),Byte_sequence.empty) | _ -> failwith ("multiple " ^ (section_name ^ " sections present")) ) | ELF_File_64 f64 -> let sections = (List.filter (fun x -> x.Elf_interpreted_section.elf64_section_name_as_string = section_name ) f64.elf64_file_interpreted_sections) in (match sections with | [section] -> let section_addr = (section.Elf_interpreted_section.elf64_section_addr) in let section_body = (section.Elf_interpreted_section.elf64_section_body) in (c,section_addr,section_body) | [] -> if strict1 then failwith ("" ^ (section_name ^ " section not present")) else (c, (Nat_big_num.of_int 0),Byte_sequence.empty) | _ -> failwith ("multiple " ^ (section_name ^ " sections present")) ) )) (*val extract_dwarf : elf_file -> maybe dwarf*) let extract_dwarf f:(dwarf)option= (let (c, _, debug_info_section_body) = (extract_section_body f ".debug_info" true) in let (c, _, debug_abbrev_section_body) = (extract_section_body f ".debug_abbrev" false) in let (c, _, debug_str_section_body) = (extract_section_body f ".debug_str" false) in let (c, _, debug_loc_section_body) = (extract_section_body f ".debug_loc" false) in let (c, _, debug_ranges_section_body) = (extract_section_body f ".debug_ranges" false) in let (c, _, debug_frame_section_body) = (extract_section_body f ".debug_frame" false) in let (c, _, debug_line_section_body) = (extract_section_body f ".debug_line" false) in let d = (parse_dwarf c debug_info_section_body debug_abbrev_section_body debug_str_section_body debug_loc_section_body debug_ranges_section_body debug_frame_section_body debug_line_section_body) in Some d) (*val extract_text : elf_file -> p_context * natural * byte_sequence*) (* (p_context, elf32/64_section_addr, elf32/64_section_body) *) let extract_text f:p_context*Nat_big_num.num*byte_sequence0= (extract_section_body f ".text" true) (** ************************************************************ *) (** ****** location evaluation ******************************** *) (** ************************************************************ *) (** pp of locations *) (*val pp_simple_location : simple_location -> string*) let pp_simple_location sl:string= ((match sl with | SL_memory_address n -> pphex n | SL_register n -> "reg" ^ Nat_big_num.to_string n | SL_implicit bs -> "value: " ^ ppbytes bs | SL_empty -> "<empty>" )) (*val pp_composite_location_piece : composite_location_piece -> string*) let pp_composite_location_piece clp:string= ((match clp with | CLP_piece( n, sl) -> "piece (" ^ (Nat_big_num.to_string n ^ (") " ^ pp_simple_location sl)) | CLP_bit_piece( n1, n2, sl) -> "bit_piece (" ^ (Nat_big_num.to_string n1 ^ ("," ^ (Nat_big_num.to_string n2 ^ (") " ^ pp_simple_location sl)))) )) (*val pp_single_location: single_location -> string*) let pp_single_location sl:string= ((match sl with | SL_simple sl -> pp_simple_location sl | SL_composite clps -> "composite: " ^ String.concat ", " (Lem_list.map pp_composite_location_piece clps) )) (** evaluation of location expressions *) (* cf dwarflist, btw: https://fedorahosted.org/elfutils/wiki/DwarfLint?format=txt *) (* location description ::= | single location description | location list single location description ::= | simple location description | composite location description simple location description ::= | memory location description : non-empty dwarf expr, value is address of all or part of object in memory | register location description : single DW_OP_regN or DW_OP_regx, naming a register in which all the object is | implicit location description : single DW_OP_implicit_value or a non-empty dwarf expr ending in DW_OP_stack_value, giving the value of all/part of object | empty location description : an empty dwarf expr, indicating a part or all of an object that is not represented composite location description : a list of simple location descriptions, each followed by a DW_OP_piece or DW_OP_bitpiece (the simple location description can be a register location description: https://www.mail-archive.com/dwarf-discuss@lists.dwarfstd.org/msg00271.html) (contradicting "A register location description must stand alone as the entire description of an object or a piece of an object.") location list entry : a list of address ranges (possibly overlapping), each with a single location description Dwarf expressions can include data-dependent control flow choices (though we don't see that in the examples?), so we can't statically determine which kind of single location description or simple location description we have. We can distinguish: - empty -> simple.empty - DW_OP_regN/DW_OP_regx -> simple.register - DW_OP_implicit_value -> simple.implicit - any of those followed by DW_OP_piece or DW_OP_bitpiece, perhaps followed by more composite parts -> composite part :: composite otherwise run to the end, or a DW_OP_stack_value at the end, or to anything (except a DO_OP_regN/DW_OP_regx) followed by a DW_OP_piece/DW_OP_bitpiece. Pfeh. actually used in our examples (ignoring GNU extentions): DW_OP_addr literal DW_OP_lit1 literal DW_OP_const4u literal DW_OP_breg3 (r3) read register value and add offset DW_OP_and bitwise and DW_OP_plus addition (mod whatever) DW_OP_deref_size DW_OP_fbreg evaluate location description from DW_AT_frame_base attribute of the current function (which is DW_OP_call_frame_cfa in our examples) and add offset DW_OP_implicit_value the argument block is the actual value (not location) of the entity in question DW_OP_stack_value use the value at top of stack as the actual value (not location) of the entity in question DW_OP_reg0 (r0)) read register value DW_OP_call_frame_cfa go off to 6.4 and pull info out of .debug_frame (possibly involving other location expressions) *) let initial_state:state= ({ s_stack = ([]); s_value = SL_empty; s_location_pieces = ([]); }) (* the main location expression evaluation function *) (* location expression evaluation is basically a recursive function down a list of operations, maintaining an operation_stack (a list of naturals representing machine-address-size words), the current simple_location, and a list of any composite_location_piece's accumulated so far *) let arithmetic_context_of_cuh cuh:arithmetic_context= ( if(Nat_big_num.equal cuh.cuh_address_size ( (Nat_big_num.of_int 8))) then ({ ac_bitwidth =( (Nat_big_num.of_int 64)); ac_half = (Nat_big_num.pow_int ( (Nat_big_num.of_int 2)) 32); ac_all = (Nat_big_num.pow_int ( (Nat_big_num.of_int 2)) 64); ac_max = (Nat_big_num.sub_nat (Nat_big_num.pow_int ( (Nat_big_num.of_int 2)) 64) ( (Nat_big_num.of_int 1))); }) else ( if(Nat_big_num.equal cuh.cuh_address_size ( (Nat_big_num.of_int 4))) then ({ ac_bitwidth =( (Nat_big_num.of_int 32)); ac_half = (Nat_big_num.pow_int ( (Nat_big_num.of_int 2)) 16); ac_all = (Nat_big_num.pow_int ( (Nat_big_num.of_int 2)) 32); ac_max = (Nat_big_num.sub_nat (Nat_big_num.pow_int ( (Nat_big_num.of_int 2)) 32) ( (Nat_big_num.of_int 1))); }) else (failwith "arithmetic_context_of_cuh given non-4/8 size"))) let find_cfa_table_row_for_pc (evaluated_frame_info1: evaluated_frame_info) (pc: Nat_big_num.num) : cfa_table_row= ((match myfind (fun (fde1,rows) -> Nat_big_num.greater_equal pc fde1.fde_initial_location_address && Nat_big_num.less pc (Nat_big_num.add fde1.fde_initial_location_address fde1.fde_address_range)) evaluated_frame_info1 with | Some (fde1,rows) -> (match myfind (fun row -> Nat_big_num.greater_equal pc row.ctr_loc) rows with | Some row -> row | None -> failwith "evaluate_cfa: no matchine row" ) | None -> failwith "evaluate_cfa: no fde encloding pc" )) let rec evaluate_operation_list (c:p_context) (dloc: location_list_list) (evaluated_frame_info1: evaluated_frame_info) (cuh: compilation_unit_header) (ac: arithmetic_context) (ev: evaluation_context) (mfbloc: attribute_value option) (pc: Nat_big_num.num) (s: state) (ops: operation list) : single_location error= (let push_memory_address v vs'= (Success { s with s_stack = (v :: vs'); s_value = (SL_memory_address v) }) in let push_memory_address_maybe (mv: Nat_big_num.num option) vs' (err:string) op= ((match mv with | Some v -> push_memory_address v vs' | None -> Fail (err ^ pp_operation op) )) in let bregxi r i= ((match ev.read_register r with | RRR_result v -> push_memory_address (partialNaturalFromInteger ( Nat_big_num.modulus(Nat_big_num.add( v)i) ( ac.ac_all))) s.s_stack | RRR_not_currently_available -> Fail "RRR_not_currently_available" | RRR_bad_register_number -> Fail ("RRR_bad_register_number " ^ Nat_big_num.to_string r) )) in let deref_size n= ((match s.s_stack with | v::vs' -> (match ev.read_memory v n with | MRR_result v' -> push_memory_address v' vs' | MRR_not_currently_available -> Fail "MRR_not_currently_available" | MRR_bad_address -> Fail "MRR_bad_address" ) | _ -> Fail "OpSem unary not given an element on stack" )) in (match ops with | [] -> if (listEqualBy (=) s.s_location_pieces []) then Success (SL_simple s.s_value) else if s.s_value = SL_empty then Success (SL_composite s.s_location_pieces) else (* unclear what's supposed to happen in this case *) Fail "unfinished part of composite expression" | op::ops' -> let es' = ((match (op.op_semantics, op.op_argument_values) with | (OpSem_nop, []) -> Success s | (OpSem_lit, [OAV_natural n]) -> push_memory_address n s.s_stack | (OpSem_lit, [OAV_integer i]) -> push_memory_address (partialTwosComplementNaturalFromInteger i ac.ac_half ( ac.ac_all)) s.s_stack | (OpSem_stack f, []) -> (match f ac s.s_stack op.op_argument_values with | Some stack' -> let value' : simple_location = ((match stack' with [] -> SL_empty | v'::_ -> SL_memory_address v' )) in Success { s with s_stack = stack'; s_value = value' } | None -> Fail "OpSem_stack failed" ) | (OpSem_not_supported, []) -> Fail ("OpSem_not_supported: " ^ pp_operation op) | (OpSem_binary f, []) -> (match s.s_stack with | v1::v2::vs' -> push_memory_address_maybe (f ac v1 v2) vs' "OpSem_binary error: " op | _ -> Fail "OpSem binary not given two elements on stack" ) | (OpSem_unary f, []) -> (match s.s_stack with | v1::vs' -> push_memory_address_maybe (f ac v1) vs' "OpSem_unary error: " op | _ -> Fail "OpSem unary not given an element on stack" ) | (OpSem_opcode_lit base, []) -> if Nat_big_num.greater_equal op.op_code base && Nat_big_num.less op.op_code (Nat_big_num.add base( (Nat_big_num.of_int 32))) then push_memory_address ( Nat_big_num.sub_nat op.op_code base) s.s_stack else Fail "OpSem_opcode_lit opcode not within [base,base+32)" | (OpSem_reg, []) -> (* TODO: unclear whether this should push the register id or not *) let r = (Nat_big_num.sub_nat op.op_code vDW_OP_reg0) in Success { s with s_stack = (r :: s.s_stack); s_value = (SL_register r) } | (OpSem_breg, [OAV_integer i]) -> let r = (Nat_big_num.sub_nat op.op_code vDW_OP_breg0) in bregxi r i | (OpSem_bregx, [OAV_natural r; OAV_integer i]) -> bregxi r i | (OpSem_deref, []) -> deref_size cuh.cuh_address_size | (OpSem_deref_size, [OAV_natural n]) -> deref_size n | (OpSem_fbreg, [OAV_integer i]) -> (match mfbloc with | Some fbloc -> (*let _ = my_debug5 ("OpSem_fbreg (" ^ show i ^ ")\n") in*) (match evaluate_location_description c dloc evaluated_frame_info1 cuh ac ev (*mfbloc*)None pc fbloc with (* what to do if the recursive call also uses fbreg? for now assume that's not allowed *) | Success l -> (match l with | SL_simple (SL_memory_address a) -> (*let _ = my_debug5 ("OpSem_fbreg: a = "^ pphex a ^ "\n") in*) let vi = (Nat_big_num.modulus ( Nat_big_num.add( a) i) ( ac.ac_all)) in (*let _ = my_debug5 ("OpSem_fbreg: v = "^ show vi ^ "\n") in*) let v = (partialNaturalFromInteger vi) (*ac.ac_half (integerFromNatural ac.ac_all)*) in push_memory_address v s.s_stack | _ -> Fail "OpSem_fbreg got a non-SL_simple (SL_memory_address _) result" (* "The DW_OP_fbreg operation provides a signed LEB128 offset from the address specified by the location description in the DW_AT_frame_base attribute of the current function. " - so what to do if the location description returns a non-memory-address location? *) ) | Fail e -> Fail ("OpSem_fbreg failure: " ^ e) ) | None -> Fail "OpSem_fbreg: no frame base location description given" ) | (OpSem_piece, [OAV_natural size_bytes]) -> let piece = (CLP_piece( size_bytes, s.s_value)) in (* we allow a piece (or bit_piece) to be any simple_location, including implicit and stack values. Unclear if this is intended, esp. the latter *) let stack' = ([]) in let value' = SL_empty in Success { s_stack = stack'; s_value = value'; s_location_pieces = (List.rev_append (List.rev s.s_location_pieces) [piece]) } | (OpSem_bit_piece, [OAV_natural size_bits; OAV_natural offset_bits]) -> let piece = (CLP_bit_piece( size_bits, offset_bits, s.s_value)) in let stack' = ([]) in let value' = SL_empty in Success { s_stack = stack'; s_value = value'; s_location_pieces = (List.rev_append (List.rev s.s_location_pieces) [piece]) } | (OpSem_implicit_value, [OAV_block( size2, bs)]) -> let stack' = ([]) in let value' = (SL_implicit bs) in Success { s with s_stack = stack'; s_value = value' } | (OpSem_stack_value, []) -> (* "The DW_OP_stack_value operation terminates the expression." - does this refer to just the subexpression, ie allowing a stack value to be a piece of a composite location, or necessarily the whole expression? Why does DW_OP_stack_value have this clause while DW_OP_implicit_value does not? *) (* why doesn't DW_OP_stack_value have a size argument? *) (match s.s_stack with | v::vs' -> let stack' = ([]) in let value' = (SL_implicit (bytes_of_natural c.endianness cuh.cuh_address_size v)) in Success { s with s_stack = stack'; s_value = value' } | _ -> Fail "OpSem_stack_value not given an element on stack" ) | (OpSem_call_frame_cfa, []) -> let row = (find_cfa_table_row_for_pc evaluated_frame_info1 pc) in (match row.ctr_cfa with | CR_undefined -> failwith "evaluate_cfa of CR_undefined" | CR_register( r, i) -> bregxi r i (* same behaviour as an OpSem_bregx *) | CR_expression bs -> failwith "CR_expression" (*TODO: fix result type - not this evaluate_location_description_bytes c dloc evaluated_frame_info cuh ac ev mfbloc pc bs*) (* TODO: restrict allowed OpSem_* in that recursive call *) ) | (_, _) -> Fail ("bad OpSem invocation: op=" ^ (pp_operation op ^ (" arguments=" ^ String.concat "" (Lem_list.map pp_operation_argument_value op.op_argument_values)))) )) in (match es' with | Success s' -> evaluate_operation_list c dloc evaluated_frame_info1 cuh ac ev mfbloc pc s' ops' | Fail e -> Fail e ) )) and evaluate_location_description_bytes (c:p_context) (dloc: location_list_list) (evaluated_frame_info1: evaluated_frame_info) (cuh: compilation_unit_header) (ac: arithmetic_context) (ev: evaluation_context) (mfbloc: attribute_value option) (pc: Nat_big_num.num) (bs: byte_sequence0) : single_location error= (let parse_context1 = ({pc_bytes = bs; pc_offset =( (Nat_big_num.of_int 0)) }) in (match parse_operations c cuh parse_context1 with | PR_fail( s, pc') -> Fail ("evaluate_location_description_bytes: parse_operations fail: " ^ pp_parse_fail s pc') | PR_success( ops, pc') -> if not (Nat_big_num.equal (Byte_sequence.length0 pc'.pc_bytes)( (Nat_big_num.of_int 0))) then Fail "evaluate_location_description_bytes: extra non-parsed bytes" else evaluate_operation_list c dloc evaluated_frame_info1 cuh ac ev mfbloc pc initial_state ops )) and evaluate_location_description (c:p_context) (dloc: location_list_list) (evaluated_frame_info1: evaluated_frame_info) (cuh: compilation_unit_header) (ac: arithmetic_context) (ev: evaluation_context) (mfbloc: attribute_value option) (pc: Nat_big_num.num) (loc:attribute_value) : single_location error= ((match loc with | AV_exprloc( n, bs) -> evaluate_location_description_bytes c dloc evaluated_frame_info1 cuh ac ev mfbloc pc bs | AV_block( n, bs) -> evaluate_location_description_bytes c dloc evaluated_frame_info1 cuh ac ev mfbloc pc bs | AV_sec_offset n -> let location_list1 = (find_location_list dloc n) in let (offset,(llis: location_list_item list)) = location_list1 in let f (lli:location_list_item) : single_location_description option= ((match lli with | LLI_lle lle -> if Nat_big_num.greater_equal pc lle.lle_beginning_address_offset && Nat_big_num.less pc lle.lle_ending_address_offset then Some lle.lle_single_location_description else None | LLI_base _ -> None (* TODO: either refactor to do offset during parsing or update base offsets here. Should refactor to use "interpreted". *) )) in (match myfindmaybe f llis with | Some bs -> evaluate_location_description_bytes c dloc evaluated_frame_info1 cuh ac ev mfbloc pc bs | None -> Fail "evaluate_location_description didn't find pc in location list ranges" ) | _ -> Fail "evaluate_location_description av_location not understood" )) (** ************************************************************ *) (** **** evaluation of frame information ********************** *) (** ************************************************************ *) (** register maps *) (*val rrp_update : register_rule_map -> cfa_register -> register_rule -> register_rule_map*) let rrp_update rrp r rr:(Nat_big_num.num*register_rule)list= ((r,rr)::rrp) (*val rrp_lookup : cfa_register -> register_rule_map -> register_rule*) let rrp_lookup r rrp:register_rule= ((match (lookupBy Nat_big_num.equal r rrp) with | Some rr -> rr | None -> RR_undefined )) (*val rrp_empty : register_rule_map*) let rrp_empty:(cfa_register*register_rule)list= ([]) (** pp of evaluated cfa information from .debug_frame *) (* readelf --debug-dump=frames-interp test/a.out Contents of the .eh_frame section: 00000000 00000014 00000000 CIE "zR" cf=1 df=-8 ra=16 LOC CFA ra 0000000000000000 rsp+8 c-8 00000018 00000024 0000001c FDE cie=00000000 pc=004003b0..004003d0 LOC CFA ra 00000000004003b0 rsp+16 c-8 00000000004003b6 rsp+24 c-8 00000000004003c0 exp c-8 00000040 0000001c 00000044 FDE cie=00000000 pc=004004b4..004004ba LOC CFA rbp ra 00000000004004b4 rsp+8 u c-8 00000000004004b5 rsp+16 c-16 c-8 00000000004004b8 rbp+16 c-16 c-8 00000000004004b9 rsp+8 c-16 c-8 00000060 00000024 00000064 FDE cie=00000000 pc=004004c0..00400549 LOC CFA rbx rbp r12 r13 r14 r15 ra 00000000004004c0 rsp+8 u u u u u u c-8 00000000004004d1 rsp+8 u c-48 c-40 u u u c-8 00000000004004f0 rsp+64 c-56 c-48 c-40 c-32 c-24 c-16 c-8 0000000000400548 rsp+8 c-56 c-48 c-40 c-32 c-24 c-16 c-8 00000088 00000014 0000008c FDE cie=00000000 pc=00400550..00400552 LOC CFA ra 0000000000400550 rsp+8 c-8 000000a0 ZERO terminator *) (*val mytoList : forall 'a. SetType 'a => set 'a -> list 'a*) let register_footprint_rrp (rrp: register_rule_map) : cfa_register Pset.set= (Pset.from_list Nat_big_num.compare (Lem_list.map fst rrp)) let register_footprint (rows: cfa_table_row list) : cfa_register list= (Pset.elements (bigunionListMap instance_Basic_classes_SetType_Num_natural_dict (fun row -> register_footprint_rrp row.ctr_regs) rows)) (*val max_lengths : list (list string) -> list natural*) let rec max_lengths xss:(Nat_big_num.num)list= ((match xss with | [] -> failwith "max_lengths" | xs::xss' -> let lens = (Lem_list.map (fun x -> Nat_big_num.of_int (String.length x)) xs) in if (listEqualBy (listEqualBy (=)) xss' []) then lens else let lens' = (max_lengths xss') in let z = (Lem_list.list_combine lens lens') in let lens'' = (Lem_list.map (fun (l1,l2)-> Nat_big_num.max l1 l2) z) in lens'' )) let rec pad_row xs lens:(string)list= ((match (xs,lens) with | ([],[]) -> [] | ([x],[len]) -> [x] | (x::(((_::_) as xs')), len::(((_::_) as lens'))) -> right_space_padded_to len x :: pad_row xs' lens' )) let pad_rows (xss : ( string list) list) : string= ((match xss with | [] -> "" | _ -> let lens = (max_lengths xss) in String.concat "" (Lem_list.map (fun xs -> String.concat " " (pad_row xs lens) ^ "\n") xss) )) let pp_evaluated_fde (fde1, (rows: cfa_table_row list)) : string= (let regs = (register_footprint rows) in let header : string list = ("LOC" :: ("CFA" :: Lem_list.map (pp_cfa_register instance_Show_Show_Num_natural_dict) regs)) in let ppd_rows : ( string list) list = (Lem_list.map (fun row -> pphex row.ctr_loc :: (pp_cfa_rule row.ctr_cfa :: Lem_list.map (fun r -> pp_register_rule (rrp_lookup r row.ctr_regs)) regs)) rows) in pad_rows (header :: ppd_rows)) let semi_pp_evaluated_fde (fde1, (rows: cfa_table_row list)) : (Nat_big_num.num (*address*) * string (*cfa*) * (string*string) list (*register rules*) ) list= (let regs = (register_footprint rows) in let ppd_rows = (Lem_list.map (fun row -> (row.ctr_loc, pp_cfa_rule row.ctr_cfa, Lem_list.map (fun r -> (pp_cfa_register instance_Show_Show_Num_natural_dict r, pp_register_rule (rrp_lookup r row.ctr_regs))) regs)) rows) in ppd_rows) (*val semi_pp_evaluated_frame_info : evaluated_frame_info -> list (natural (*address*) * string (*cfa*) * list (string*string) (*register rules*) )*) let semi_pp_evaluated_frame_info efi:(Nat_big_num.num*string*(string*string)list)list= (List.concat (Lem_list.map semi_pp_evaluated_fde efi)) (** evaluation of cfa information from .debug_frame *) let evaluate_call_frame_instruction (fi: frame_info) (cie1: cie) (state1: cfa_state) (cfi: call_frame_instruction) : cfa_state= (let create_row (loc: Nat_big_num.num)= (let row = ({ state1.cs_current_row with ctr_loc = loc }) in { state1 with cs_current_row = row; cs_previous_rows = (state1.cs_current_row::state1.cs_previous_rows) }) in let update_cfa (cr:cfa_rule)= (let row = ({ state1.cs_current_row with ctr_cfa = cr }) in { state1 with cs_current_row = row }) in let update_reg r rr= (let row = ({ state1.cs_current_row with ctr_regs = (rrp_update state1.cs_current_row.ctr_regs r rr) }) in { state1 with cs_current_row = row }) in (match cfi with (* Row Creation Instructions *) | DW_CFA_set_loc a -> create_row a | DW_CFA_advance_loc d -> create_row ( Nat_big_num.add state1.cs_current_row.ctr_loc (Nat_big_num.mul d cie1.cie_code_alignment_factor)) | DW_CFA_advance_loc1 d -> create_row ( Nat_big_num.add state1.cs_current_row.ctr_loc (Nat_big_num.mul d cie1.cie_code_alignment_factor)) | DW_CFA_advance_loc2 d -> create_row ( Nat_big_num.add state1.cs_current_row.ctr_loc (Nat_big_num.mul d cie1.cie_code_alignment_factor)) | DW_CFA_advance_loc4 d -> create_row ( Nat_big_num.add state1.cs_current_row.ctr_loc (Nat_big_num.mul d cie1.cie_code_alignment_factor)) (* CFA Definition Instructions *) | DW_CFA_def_cfa( r, n) -> update_cfa (CR_register( r, ( n))) | DW_CFA_def_cfa_sf( r, i) -> update_cfa (CR_register( r, ( Nat_big_num.mul i cie1.cie_data_alignment_factor))) | DW_CFA_def_cfa_register r -> (match state1.cs_current_row.ctr_cfa with | CR_register( r', i) -> update_cfa (CR_register( r, i)) | CR_undefined -> (* FIXME: this is to handle a bug in riscv64-gcc. gcc generates "DW_CFA_def_cfa_register: r2 (sp)" as the first instruction. Dwarf5 documentation seems to suggest this is not valid. We think what gcc meant to generate is "DW_CFA_def_cfa: r2 (sp) ofs 0" *) update_cfa (CR_register( r,( (Nat_big_num.of_int 0)))) | CR_expression _ -> failwith "DW_CFA_def_cfa_register: current rule is CR_expression" ) | DW_CFA_def_cfa_offset n -> (match state1.cs_current_row.ctr_cfa with | CR_register( r, i) -> update_cfa (CR_register( r, ( n))) | _ -> failwith "DW_CFA_def_cfa_offset: current rule is not CR_register" ) | DW_CFA_def_cfa_offset_sf i -> (match state1.cs_current_row.ctr_cfa with | CR_register( r, i') -> update_cfa (CR_register( r, ( Nat_big_num.mul i' cie1.cie_data_alignment_factor))) | _ -> failwith "DW_CFA_def_cfa_offset_sf: current rule is not CR_register" ) | DW_CFA_def_cfa_expression b -> update_cfa (CR_expression b) (* Register Rule Instrutions *) | DW_CFA_undefined r -> update_reg r (RR_undefined) | DW_CFA_same_value r -> update_reg r (RR_same_value) | DW_CFA_offset( r, n) -> update_reg r (RR_offset ( Nat_big_num.mul( n) cie1.cie_data_alignment_factor)) | DW_CFA_offset_extended( r, n) -> update_reg r (RR_offset ( Nat_big_num.mul( n) cie1.cie_data_alignment_factor)) | DW_CFA_offset_extended_sf( r, i) -> update_reg r (RR_offset ( Nat_big_num.mul i cie1.cie_data_alignment_factor)) | DW_CFA_val_offset( r, n) -> update_reg r (RR_val_offset ( Nat_big_num.mul( n) cie1.cie_data_alignment_factor)) | DW_CFA_val_offset_sf( r, i) -> update_reg r (RR_val_offset ( Nat_big_num.mul i cie1.cie_data_alignment_factor)) | DW_CFA_register( r1, r2) -> update_reg r1 (RR_register r2) | DW_CFA_expression( r, b) -> update_reg r (RR_expression b) | DW_CFA_val_expression( r, b) -> update_reg r (RR_val_expression b) | DW_CFA_restore r -> update_reg r (rrp_lookup r state1.cs_initial_instructions_row.ctr_regs) (* RR_undefined if the lookup fails? *) | DW_CFA_restore_extended r -> update_reg r (rrp_lookup r state1.cs_initial_instructions_row.ctr_regs) (* Row State Instructions *) (* do these also push and restore the CFA rule? *) | DW_CFA_remember_state -> { state1 with cs_row_stack = (state1.cs_current_row :: state1.cs_row_stack) } | DW_CFA_restore_state -> (match state1.cs_row_stack with | r::rs -> { state1 with cs_current_row = r; cs_row_stack = rs } | [] -> failwith "DW_CFA_restore_state: empty row stack" ) (* Padding Instruction *) | DW_CFA_nop -> state1 (* DW_CFA_AARCH64_negate_ra_state Instruction *) | DW_CFA_AARCH64_negate_ra_state -> state1 (* Unknown *) | DW_CFA_unknown b -> failwith ("evaluate_call_frame_instruction: DW_CFA_unknown " ^ hex_string_of_byte b) )) let rec evaluate_call_frame_instructions (fi: frame_info) (cie1: cie) (state1: cfa_state) (cfis: call_frame_instruction list) : cfa_state= ((match cfis with | [] -> state1 | cfi::cfis' -> let state' = (evaluate_call_frame_instruction fi cie1 state1 cfi) in evaluate_call_frame_instructions fi cie1 state' cfis' )) let evaluate_fde (fi: frame_info) (fde1:fde) : cfa_table_row list= (let cie1 = (find_cie fi fde1.fde_cie_pointer) in let final_location = (Nat_big_num.add fde1.fde_initial_location_address fde1.fde_address_range) in let initial_cfa_state = (let initial_row = ({ ctr_loc = (fde1.fde_initial_location_address); ctr_cfa = CR_undefined; ctr_regs = rrp_empty; }) in { cs_current_row = initial_row; cs_previous_rows = ([]); cs_initial_instructions_row = initial_row; cs_row_stack = ([]); }) in let state' = (evaluate_call_frame_instructions fi cie1 initial_cfa_state cie1.cie_initial_instructions) in let initial_row' = (state'.cs_current_row) in let state'' = ({ initial_cfa_state with cs_current_row = initial_row'; cs_initial_instructions_row = initial_row' }) in let state''' = (evaluate_call_frame_instructions fi cie1 (*final_location*) state'' fde1.fde_instructions) in List.rev (state'''.cs_current_row:: state'''.cs_previous_rows)) (*val evaluate_frame_info : dwarf -> evaluated_frame_info*) let evaluate_frame_info (d: dwarf) : evaluated_frame_info= (Lem_list.mapMaybe (fun fie -> (match fie with FIE_fde fde1 -> Some (fde1, (evaluate_fde d.d_frame_info fde1)) | FIE_cie _ -> None )) d.d_frame_info) let pp_evaluated_frame_info (efi: evaluated_frame_info):string= (String.concat "\n" (Lem_list.map pp_evaluated_fde efi)) (** ************************************************************ *) (** ** pp of type info *) (** ************************************************************ *) (* partial analysis and pp of type info - incomplete, but enough for some C code *) (* analyse top level of C type structure, without recursing into type subterms *) let strict s x:'a= ((match x with | Some y -> y | None -> failwith ("analyse_type_info_die strict failure on \n" ^ (s () ^ "\n")) )) let analyse_type_info_top c (d: dwarf) (r:bool(*recurse into members*)) (cupdie1: cupdie) : cupdie c_type_top= (let (cu,parents,die1) = cupdie1 in let mname = (find_name_of_die d.d_str die1) in let mtyp = (find_DW_AT_type_of_die c d cu d.d_str die1) in let s ()= (pp_die c cu.cu_header d.d_str true( (Nat_big_num.of_int 0)) false die1) in if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_base_type") then let encoding = (let n = (strict s (find_natural_attribute_value_of_die c "DW_AT_encoding" die1)) in if not(List.exists (fun (s,n')->Nat_big_num.equal n n') base_type_attribute_encodings) then strict s None else n) in (* TODO: handle user encodings correctly *) let mbyte_size = (find_natural_attribute_value_of_die c "DW_AT_byte_size" die1) in CT_base( cupdie1, (strict s mname), encoding, mbyte_size) else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_pointer_type") then CT_pointer( cupdie1, mtyp) else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_const_type") then CT_const( cupdie1, mtyp) else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_volatile_type") then (* CT_volatile cupdie (strict s mtyp')*) (* TODO: this is a temporary hack, while we figure out what DW_TAG_volatile without a DW_AT_type is supposed to mean *) (match mtyp with | Some typ -> CT_volatile( cupdie1, typ) | None -> CT_missing cupdie1 ) else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_restrict_type") then CT_restrict( cupdie1, (strict s mtyp)) else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_typedef") then let decl1 = ({ decl_file = None; (* TODO *) decl_line = None; (* TODO *) }) in CT_typedef( cupdie1, (strict s mname), (strict s mtyp), decl1) else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_array_type") then let dims = (let subranges = (List.filter (fun die' -> Nat_big_num.equal die'.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_subrange_type")) die1.die_children) in Lem_list.map (fun die' -> (*WAS: let mcount = find_natural_attribute_value_of_die c "DW_AT_count" die' in*) let mcount = ((match find_attribute_value "DW_AT_count" die' with | None -> None | Some av -> (match maybe_natural_of_constant_attribute_value die' c av with | None -> None (* DWARF seems to sometimes use an AV_ref* attribute value for DW_AT_count, referring to a variable die, for a VLA length. In this case for the moment we will just forget the length information, which is what this clause does *) | Some n -> Some n ) )) in let msubrange_type = (find_DW_AT_type_of_die c d cu d.d_str die') in (mcount, msubrange_type)) subranges) in CT_array( cupdie1, (strict s mtyp), dims) else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_structure_type") || Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_union_type") then let atk = (if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_structure_type") then Atk_structure else Atk_union) in let mbyte_size = (find_natural_attribute_value_of_die c "DW_AT_byte_size" die1) in let decl1 = ({ decl_file = None; (* TODO *) decl_line = None; (* TODO *) }) in let members = (if r then let members_raw = (List.filter (fun die' -> Nat_big_num.equal die'.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_member")) die1.die_children) in Some (Lem_list.map (fun die' -> let cupdie' = (cu,(die1::parents),die') in let mname' = (find_name_of_die d.d_str die') in let typ' = (strict s (find_DW_AT_type_of_die c d cu d.d_str die')) in let mdata_member_location' = ((match atk with | Atk_structure -> Some (strict s (find_natural_attribute_value_of_die c "DW_AT_data_member_location" die')) | Atk_union -> (find_natural_attribute_value_of_die c "DW_AT_data_member_location" die') )) in (cupdie',mname',typ',mdata_member_location')) members_raw) else None) in CT_struct_union( cupdie1, atk, mname, mbyte_size, decl1, members) else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_enumeration_type") then let mbyte_size = (find_natural_attribute_value_of_die c "DW_AT_byte_size" die1) in let decl1 = ({ decl_file = None; (* TODO *) decl_line = None; (* TODO *) }) in let members = (if r then let members_raw = (List.filter (fun die' -> Nat_big_num.equal die'.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_enumerator")) die1.die_children) in Some (Lem_list.map (fun die' -> let cupdie' = (cu,(die1::parents),die') in let mname' = (find_name_of_die d.d_str die') in (*let _ = my_debug5 (s ()) in *) let const_value = (strict s (find_integer_attribute_value_of_die c "DW_AT_const_value" die')) in (*let _ = my_debug5 "ok" in*) (cupdie',mname',const_value)) members_raw) else None) in CT_enumeration( cupdie1, mname, mtyp, mbyte_size, decl1, members) else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_subroutine_type") then (* let prototyped = strict s (find_flag_attribute_value_of_die "DW_AT_prototyped" die) in*) let prototyped = (find_flag_attribute_value_of_die_default_false "DW_AT_prototyped" die1) in let mresult_type = mtyp in let parameter_types = (let parameter_types_raw = (List.filter (fun die' -> Nat_big_num.equal die'.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_formal_parameter")) die1.die_children) in (Lem_list.map (fun die' -> let cupdie' = (cu,(die1::parents),die') in let mname' = (find_name_of_die d.d_str die') in let typ' = (strict s (find_DW_AT_type_of_die c d cu d.d_str die')) in typ') parameter_types_raw)) in let (variable_parameter_list: bool) = (List.exists (fun die' -> Nat_big_num.equal die'.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_unspecified_parameters")) die1.die_children) in CT_subroutine( cupdie1, prototyped, mresult_type, parameter_types, variable_parameter_list) else failwith ("analyse_type_info_top didn't recognise tag: " ^ (pphex die1.die_abbreviation_declaration.ad_tag ^ (" for DIE " ^ pp_cupdie3 cupdie1)))) let rec analyse_type_info_deep (d: dwarf) (r:bool(*recurse_into_members*)) cupdie1 : c_type= (let c = (p_context_of_d d) in let (cu,parents,die1) = cupdie1 in let (typ: cupdie c_type_top) = (analyse_type_info_top c (d: dwarf) r cupdie1) in (match typ with | CT_missing cupdie1 -> CT (CT_missing cupdie1) | CT_base( cupdie1, name1, encoding, mbyte_size) -> CT (CT_base( cupdie1, name1, encoding, mbyte_size)) | CT_pointer( cupdie1, mtyp') -> CT (CT_pointer( cupdie1, (Lem.option_map (analyse_type_info_deep d r) mtyp'))) | CT_const( cupdie1, mtyp') -> CT (CT_const( cupdie1, (Lem.option_map (analyse_type_info_deep d r) mtyp'))) | CT_volatile( cupdie1, typ') -> CT (CT_volatile( cupdie1, (analyse_type_info_deep d r typ'))) | CT_restrict( cupdie1, typ') -> CT (CT_restrict( cupdie1, (analyse_type_info_deep d r typ'))) | CT_typedef( cupdie1, name1, typ', decl1) -> CT (CT_typedef( cupdie1, name1, (analyse_type_info_deep d r typ'), decl1)) | CT_array( cupdie1, typ', dims) -> CT (CT_array( cupdie1, (analyse_type_info_deep d r typ'), (Lem_list.map (fun (mcount,msubrange_typ) -> (mcount, (Lem.option_map (analyse_type_info_deep d r) msubrange_typ))) dims))) | CT_struct_union( cupdie1, atk, mname, mbyte_size, decl1, mmembers) -> CT (CT_struct_union( cupdie1, atk, mname, mbyte_size, decl1, (Lem.option_map (fun members -> (Lem_list.map (fun (((cupdie1,mname,typ,mdata_member_location) as am)) -> (cupdie1,mname,(analyse_type_info_deep d false typ),mdata_member_location))members)) mmembers))) | CT_enumeration( cupdie1, mname, mtyp', mbyte_size, decl1, mmembers) -> CT(CT_enumeration( cupdie1, mname, (Lem.option_map (analyse_type_info_deep d r) mtyp'), mbyte_size, decl1, mmembers)) | CT_subroutine( cupdie1, prototyped, mresult_type, parameter_types, variable_parameter_list) -> CT (CT_subroutine( cupdie1, prototyped, (Lem.option_map (analyse_type_info_deep d r) mresult_type), (Lem_list.map (fun typ -> analyse_type_info_deep d r typ) parameter_types), variable_parameter_list)) )) let find_DW_AT_type_of_die_deep d cupdie1 : c_type option= (let c = (p_context_of_d d) in let (cu,parents,die1) = cupdie1 in (match find_reference_attribute_of_die c d cu d.d_str "DW_AT_type" die1 with | None -> None | Some cupdie' -> Some (analyse_type_info_deep d false cupdie') )) let find_DW_AT_type_of_die_deep_using_abstract_origin d cupdie1 : c_type option= (let c = (p_context_of_d d) in let (cu,parents,die1) = cupdie1 in (match find_reference_attribute_using_abstract_origin c d cu d.d_str "DW_AT_type" die1 with | None -> None | Some cupdie' -> Some (analyse_type_info_deep d false cupdie') )) (* analyse and pp C type structure, but without going into the definitions of struct_union or enumeration types *) let pp_struct_union_type_kind atk:string= ((match atk with | Atk_structure -> "struct" | Atk_union -> "union" )) let pp_mbyte_size dict_Show_Show_a mbyte_size:string= ("size:" ^ (match mbyte_size with | Some n -> dict_Show_Show_a.show_method n | None -> "?" )) (* pp the top-level structure of a C type, omitting struct_union-type and enum member definitions*) let pp_type_info_top (ppa:'a->string) (typ: 'a c_type_top) : string= ((match typ with | CT_missing cupdie1 -> "missing at " ^ pp_cupdie cupdie1 | CT_base( cupdie1, name1, encoding, mbyte_size) -> name1 ^ (" (base type, " ^ ((match lookup_aB_a instance_Basic_classes_Eq_Num_natural_dict encoding base_type_attribute_encodings with Some s -> s | None -> Nat_big_num.to_string encoding ) ^ (" " ^ (pp_mbyte_size instance_Show_Show_Num_natural_dict mbyte_size ^ ")")))) | CT_pointer( cupdie1, mtyp') -> "pointer(" ^ ((match mtyp' with | Some typ' -> ppa typ' | None -> "no type" ) ^ ")") | CT_const( cupdie1, mtyp') -> "const(" ^ ((match mtyp' with Some typ'->ppa typ' | None -> "no type" ) ^ ")") | CT_volatile( cupdie1, typ') -> "volatile(" ^ (ppa typ' ^ ")") | CT_restrict( cupdie1, typ') -> "restrict(" ^ (ppa typ' ^ ")") | CT_typedef( cupdie1, name1, typ', decl1) -> "typedef("^(name1^("="^(ppa typ' ^ ")"))) | CT_array( cupdie1, typ', dims) -> ppa typ' ^ String.concat "" (Lem_list.map (fun (mcount,msubrange_typ) -> "["^((match mcount with | Some count -> Nat_big_num.to_string count | None -> "no count" ) ^"]")) dims) | CT_struct_union( cupdie1, atk, mname, mbyte_size, decl1, mmembers) -> pp_struct_union_type_kind atk ^ (" " ^ (((match mname with | Some s -> s | None -> "noname" )) ^ pp_cupdie cupdie1)) | CT_enumeration( cupdie1, mname, mtyp', mbyte_size, decl1, mmembers) -> "enum" ^ (" " ^ (((match mname with | Some s -> s | None -> "noname" )) ^ pp_cupdie cupdie1)) | CT_subroutine( cupdie1, prototyped, mresult_type, parameter_types, variable_parameter_list) -> "subroutine(" ^ ((if prototyped then "prototyped" else "not-prototyped") ^ (" " ^ (((match mresult_type with None -> "no type" | Some result_type -> ppa result_type )) ^ ("(" ^ (String.concat "," ( List.rev_append (List.rev (Lem_list.map ppa parameter_types)) (if variable_parameter_list then ["..."] else [])) ^ ")"))))) )) let rec pp_type_info_deep (ctyp:c_type) : string= (let ppa = pp_type_info_deep in (match ctyp with | CT typ -> pp_type_info_top ppa typ )) let rec pp_type_info_die c (d: dwarf) cupdie1 : string= (let (typ: cupdie c_type_top) = (analyse_type_info_top c (d: dwarf) false cupdie1) in let ppa = (pp_type_info_die c d) in pp_type_info_top ppa typ) let pp_struct_union_type_member c d (am: cupdie struct_union_member) : string list= (let (cupdie1,mname,typ,mdata_member_location) = am in [ " "; ((match mname with | Some s -> s | None -> "noname" )); (" @ " ^ ((match mdata_member_location with None -> "nodatamemberlocation" | Some data_member_location -> Nat_big_num.to_string data_member_location ))); (" : " ^ pp_type_info_die c d typ) ]) let pp_struct_union_type_defn c d cupdie1:string= (let (typ: cupdie c_type_top) = (analyse_type_info_top c (d: dwarf) true cupdie1) in (match typ with | CT_struct_union( cupdie1, atk, mname, mbyte_size, decl1, mmembers) -> ((match mname with | Some s -> s | None -> "noname" )) ^ (" " ^ (pp_cupdie cupdie1 ^ (" " ^ (pp_mbyte_size instance_Show_Show_Num_natural_dict mbyte_size ^ ("\n" ^ pad_rows ((match mmembers with Some members -> (Lem_list.map (pp_struct_union_type_member c d) members) | None -> [] ))))))) | _ -> failwith "pp_struct_union_type_defn called on non-struct_union" )) let pp_struct_union_type_member' (am: c_type struct_union_member) : string list= (let (cupdie1,mname,ctyp,mdata_member_location) = am in [ " "; ((match mname with | Some s -> s | None -> "noname" )); (" @ " ^ ((match mdata_member_location with None -> "nodatamemberlocation" | Some data_member_location -> Nat_big_num.to_string data_member_location ))); (" : " ^ pp_type_info_deep ctyp) ]) let pp_enum_type_member' (em:enumeration_member) : string list= (let (cupdie1,mname,const_value) = em in [ " "; ((match mname with | Some s -> s | None -> "noname" )); (" = " ^ Nat_big_num.to_string const_value) ]) let pp_struct_union_type_defn' (ctyp: c_type) :string= (let preamble mname kind cupdie1 mbyte_size= (((match mname with | Some s -> s | None -> "noname" )) ^ (" " ^ (kind ^ (" " ^ (pp_cupdie cupdie1 ^ (" " ^ pp_mbyte_size instance_Show_Show_Num_natural_dict mbyte_size)))))) in (match ctyp with | CT(CT_struct_union( cupdie1, atk, mname, mbyte_size, decl1, mmembers)) -> preamble mname (pp_struct_union_type_kind atk) cupdie1 mbyte_size ^ ("\n" ^ pad_rows ((match mmembers with Some members -> (Lem_list.map (pp_struct_union_type_member') members) | None -> [["warning: no members list"]] ))) | CT(CT_enumeration( cupdie1, mname, mtyp, mbyte_size, decl1, mmembers)) -> preamble mname "enum" cupdie1 mbyte_size ^ (" " ^ (((match mtyp with Some typ -> pp_type_info_deep typ | None -> "no representation type" )) ^ ("\n" ^ pad_rows ((match mmembers with Some members -> (Lem_list.map (pp_enum_type_member') members) | None -> [["warning: no members list"]] ))))) | _ -> failwith "pp_struct_union_type_defn called on non-struct_union" )) (* match typ with | CT_base cupdie name encoding mbyte_size -> name ^ " (base type, " ^ match lookup_aB_a encoding base_type_attribute_encodings with Just s -> s | Nothing -> show encoding end ^ " " ^ pp_mbyte_size mbyte_size ^ ")" | CT_pointer cupdie mtyp' -> "pointer(" ^ match mtyp' with | Just typ' -> pp_type_info_die c d typ' | Nothing -> "no type" end ^ ")" | CT_const cupdie mtyp' -> "const(" ^ match mtyp' with Just typ'->pp_type_info_die c d typ' | Nothing -> "no type" end ^ ")" | CT_volatile cupdie typ' -> "volatile(" ^ pp_type_info_die c d typ' ^ ")" | CT_restrict cupdie typ' -> "restrict(" ^ pp_type_info_die c d typ' ^ ")" | CT_typedef cupdie name typ' decl -> "typedef("^name^"="^pp_type_info_die c d typ' ^ ")" | CT_array cupdie typ' dims -> pp_type_info_die c d typ' ^ String.concat "" (List.map (fun (mcount,subrange_typ) -> "["^match mcount with | Just count -> show count | Nothing -> "no count" end ^"]") dims) | CT_struct_union cupdie atk mname mbyte_size decl members -> pp_struct_union_type_kind atk ^ " " ^ (match mname with | Just s -> s | Nothing -> "noname" end) ^ pp_cupdie cupdie | CT_enumeration cupdie mname mtyp' mbyte_size decl members -> "enum" ^ " " ^ (match mname with | Just s -> s | Nothing -> "noname" end) ^ pp_cupdie cupdie end *) (* expect the die to have a DW_AT_type, and pp it *) let pp_type_info_die_DW_AT_type c (d: dwarf) cu str die1:string= ((match find_DW_AT_type_of_die_using_abstract_origin c d cu str die1 with | Some (cu',parents',die') -> pp_type_info_die c (d: dwarf) (cu',parents',die') | None -> "DW_AT_abstract origin failed" )) let struct_union_enum_types (d:dwarf) : c_type list= (let cupdies = (find_dies (fun die1 -> Lem_list.elem instance_Basic_classes_Eq_Num_natural_dict die1.die_abbreviation_declaration.ad_tag [tag_encode "DW_TAG_structure_type"; tag_encode "DW_TAG_union_type"; tag_encode "DW_TAG_enumeration_type"]) d) in Lem_list.map (analyse_type_info_deep (d: dwarf) true) cupdies) (* let pp_all_struct_union_enum_types c d : string = String.concat "\n\n" (List.map ((fun (cu,parents,die) -> pp_struct_union_type_defn c d (cu,parents,die))) (struct_union_type_dies d)) *) let pp_all_struct_union_enum_types' d : string= (let ctyps : c_type list = (struct_union_enum_types d) in String.concat "" ((Lem_list.map pp_struct_union_type_defn') ctyps)) (** ************************************************************ *) (** ** analysis of location and frame data for reverse mapping *) (** ************************************************************ *) (** analysis *) (** simple-minded analysis of location *) let analyse_locations_raw c (d: dwarf):string= (let (cuh_default : compilation_unit_header) = (let cu = (myhead d.d_compilation_units) in cu.cu_header) in (* find all DW_TAG_variable and DW_TAG_formal_parameter dies with a DW_AT_name attribute *) let = (Lem_list.map tag_encode ["DW_TAG_variable"; "DW_TAG_formal_parameter"]) in let dies : (compilation_unit * ( die list) * die) list = (find_dies (fun die1 -> Lem_list.elem instance_Basic_classes_Eq_Num_natural_dict die1.die_abbreviation_declaration.ad_tag tags && has_attribute "DW_AT_name" die1) d) in String.concat "" (Lem_list.map (fun (cu,parents,die1) -> let ats = (Lem_list.list_combine die1.die_abbreviation_declaration.ad_attribute_specifications die1.die_attribute_values) in let find_ats (s:string)= (myfindNonPure (fun (((at: Nat_big_num.num), (af: Nat_big_num.num)), ((pos: Nat_big_num.num),(av:attribute_value))) -> Nat_big_num.equal (attribute_encode s) at) ats) in let ((_,_),(_,av_name)) = (find_ats "DW_AT_name") in let name1 = ((match av_name with | AV_string bs -> string_of_byte_sequence bs | AV_strp n -> pp_debug_str_entry d.d_str n | _ -> "av_name AV not understood" )) in let ((_,_),(_,av_location)) = (find_ats "DW_AT_location") in let ppd_location = ((match av_location with | AV_exprloc( n, bs) -> " "^(parse_and_pp_operations c cuh_default bs^"\n") | AV_block( n, bs) -> " "^(parse_and_pp_operations c cuh_default bs^"\n") | AV_sec_offset n -> let location_list1 = (myfindNonPure (fun (n',_)->Nat_big_num.equal n' n) d.d_loc) in pp_location_list c cuh_default location_list1 | _ -> "av_location AV not understood" )) in pp_tag_encoding die1.die_abbreviation_declaration.ad_tag ^ (" " ^ (name1 ^ (":\n" ^ (ppd_location ^ "\n")))) ) dies)) (** more proper analysis of locations *) (* TODO: handle this: In a variable entry representing the definition of a variable (that is, with no DW_AT_declaration attribute) if no location attribute is present, or if the location attribute is present but has an empty location description (as described in Section 2.6), the variable is assumed to exist in the source code but not in the executable program (but see number 10, below). In a variable entry representing a non-defining declaration of a variable, the location specified modifies the location specified by the defining declaration and only applies for the scope of the variable entry; if no location is specified, then the location specified in the defining declaration applies. The location of a variable may be further specified with a DW_AT_segment attribute, if appropriate. *) (* if there's a DW_AT_location that's a location list (DW_FORM_sec_offset/AV_sec_offset) : use that for both the range(s) and location; interpret the range(s) wrt the applicable base address of the compilation unit if there's a DW_AT_location that's a location expression (DW_FORM_exprloc/AV_exprloc or DW_block/AV_block), look for the closest enclosing range: - DW_AT_low_pc (AV_addr) and no DW_AT_high_pc or DW_AT_ranges: just the singleton address - DW_AT_low_pc (AV_addr) and DW_AT_high_pc (either an absolute AV_addr or an offset AV_constantN/AV_constant_SLEB128/AV_constantULEB128) : that range - DW_AT_ranges (DW_FORM_sec_offset/AV_sec_offset) : get a range list from .debug_ranges; interpret wrt the applicable base address of the compilation unit - for compilation units: a DW_AT_ranges together with a DW_AT_low_pc to specify the default base address to use in interpeting location and range lists DW_OP_fbreg in location expressions evaluate the DW_AT_frame_base of the closest enclosing function - which is either a location expression or a location list (what happens if the ranges of that location list don't cover where we are?) For each variable and formal parameter that has a DW_AT_name, we'll calculate a list of pairs of a concrete (low,high) range and a location expression. *) let cu_base_address cu:Nat_big_num.num= ((match find_attribute_value "DW_AT_low_pc" cu.cu_die with | Some (AV_addr n) -> n | _ -> (Nat_big_num.of_int 0) (*Nothing*) (*Assert_extra.failwith "no cu DW_AT_low_pc"*) )) let range_of_die c cuh str (dranges: range_list_list) (cu_base_address1: Nat_big_num.num) (die1: die) : ( (Nat_big_num.num * Nat_big_num.num)list)option= ((match (find_attribute_value "DW_AT_low_pc" die1, find_attribute_value "DW_AT_high_pc" die1, find_attribute_value "DW_AT_ranges" die1) with | (Some (AV_addr n), None, None ) -> Some [(n,Nat_big_num.add n( (Nat_big_num.of_int 1)))] (* unclear if this case is used? *) | (Some (AV_addr n1), Some (AV_addr n2), None ) -> Some [(n1,n2)] | (Some (AV_addr n1), Some (AV_constant_ULEB128 n2), None ) -> Some [(n1,Nat_big_num.add n1 n2)] (* should be mod all? *) | (Some (AV_addr n1), Some (AV_constant_SLEB128 i2), None ) -> Some [(n1, Nat_big_num.abs ( Nat_big_num.add( n1) i2))] (* should be mod all? *) | (Some (AV_addr n1), Some (AV_constantN( _, _)), None ) -> failwith "AV_constantN in range_of_die" | (Some (AV_addr n1), Some (AV_block( n, bs)), None ) -> let n2 = (natural_of_bytes c.endianness bs) in Some [(n1,Nat_big_num.add n1 n2)] (* should be mod all? *) (* signed or unsigned interp? *) | (_, None, Some (AV_sec_offset n)) -> let rlis = (snd ((match find_range_list dranges n with Some rlis->rlis | none0 -> failwith ("find_range_list failed on AV_sec_offset n=" ^ (Nat_big_num.to_string n ^ (" for die\n" ^ pp_die c cuh str false( (Nat_big_num.of_int 0)) false die1))) ))) in let nns = (interpret_range_list cu_base_address1 rlis) in Some nns | (None, None, None ) -> None | (_, _, _ ) -> Some [] (*Assert_extra.failwith "unexpected attribute values in closest_enclosing_range"*) )) let range_of_die_d (d:dwarf) cu (die1: die) : ( (Nat_big_num.num * Nat_big_num.num)list)option= (let c = (p_context_of_d d) in range_of_die c cu.cu_header d.d_str d.d_ranges (cu_base_address cu) die1) let entry_address (die1:die) : Nat_big_num.num option= ((match (find_attribute_value "DW_AT_low_pc" die1, find_attribute_value "DW_AT_entry_pc" die1) with | (_, Some (AV_addr n)) -> Some n | (Some (AV_addr n), _) -> Some n | (None,None) -> None )) let rec closest_enclosing_range c cuh str (dranges: range_list_list) (cu_base_address1: Nat_big_num.num) (parents: die list) : ( (Nat_big_num.num * Nat_big_num.num)list)option= ((match parents with | [] -> None | die1::parents' -> (match range_of_die c cuh str dranges cu_base_address1 die1 with | ((Some x) as y) -> y | None -> closest_enclosing_range c cuh str dranges cu_base_address1 parents' ) )) (* If one of the DW_FORM_data<n> forms is used to represent a signed or unsigned integer, it can be hard for a consumer to discover the context necessary to determine which interpretation is intended. Producers are therefore strongly encouraged to use DW_FORM_sdata or DW_FORM_udata for signed and unsigned integers respectively, rather than DW_FORM_data<n>. no kidding - if we get an AV_constantN for DW_AT_high_pc, should it be interpreted as signed or unsigned? *) let rec closest_enclosing_frame_base dloc (base_address1: Nat_big_num.num) (parents: die list) : attribute_value option= ((match parents with | [] -> None | die1::parents' -> (match find_attribute_value "DW_AT_frame_base" die1 with | Some av -> Some av | None -> closest_enclosing_frame_base dloc base_address1 parents' ) )) let interpreted_location_of_die c cuh str (dloc: location_list_list) (dranges: range_list_list) (base_address1: Nat_big_num.num) (parents: die list) (die1: die) : ( (Nat_big_num.num * Nat_big_num.num * single_location_description)list)option= ( (* for a simple location expression bs, we look in the enclosing die tree to find the associated pc range *)let location bs= ((match closest_enclosing_range c cuh str dranges base_address1 (die1::parents) with | Some nns -> Some (Lem_list.map (fun (n1,n2) -> (n1,n2,bs)) nns) | None -> (* if there is no such range, we take the full 0 - 0xfff.fff range*) Some [( (Nat_big_num.of_int 0),(arithmetic_context_of_cuh cuh).ac_max,bs)] )) in (match find_attribute_value "DW_AT_location" die1 with | Some (AV_exprloc( n, bs)) -> location bs | Some (AV_block( n, bs)) -> location bs (* while for a location list, we take the associated pc range from each element of the list *) | Some (AV_sec_offset n) -> let (_,llis) = (find_location_list dloc n) in Some (interpret_location_list base_address1 llis) | None -> None )) (*val analyse_locations : dwarf -> analysed_location_data*) let analyse_locations (d: dwarf) : analysed_location_data= (let c = (p_context_of_d d) in (* let (cuh_default : compilation_unit_header) = let cu = myhead d.d_compilation_units in cu.cu_header in*) (* find all DW_TAG_variable and DW_TAG_formal_parameter dies with a DW_AT_location attribute and either a DW_AT_name or a DW_abstract_origin *) (* (leaving formal parameters of inlined routines with a DW_AT_const_value to the future) *) let = (Lem_list.map tag_encode ["DW_TAG_variable"; "DW_TAG_formal_parameter"]) in let dies : (compilation_unit * ( die list) * die) list = (find_dies (fun die1 -> Lem_list.elem instance_Basic_classes_Eq_Num_natural_dict die1.die_abbreviation_declaration.ad_tag tags && ((has_attribute "DW_AT_name" die1 || has_attribute "DW_AT_abstract_origin" die1) && has_attribute "DW_AT_location" die1)) d) in Lem_list.map (fun ((((cu:compilation_unit), (parents: die list), (die1: die)) as x)) -> let base_address1 = (cu_base_address cu) in let interpreted_locations : ( (Nat_big_num.num * Nat_big_num.num * single_location_description)list)option = (interpreted_location_of_die c cu.cu_header d.d_str d.d_loc d.d_ranges base_address1 parents die1) in (x,interpreted_locations) ) dies) let pp_analysed_locations1 c cuh (nnls: (Nat_big_num.num * Nat_big_num.num * single_location_description) list) : string= (String.concat "" (Lem_list.map (fun (n1,n2,bs) -> " " ^ (pphex n1 ^ (" " ^ (pphex n2 ^ (" " ^ parse_and_pp_operations c cuh bs))))) nnls)) let pp_analysed_locations2 c cuh mnnls:string= ((match mnnls with | Some nnls -> pp_analysed_locations1 c cuh nnls | None -> " <no locations>" )) (* let pp_analysed_locations3 c d str (als: analysed_location_data) : string = pad_rows (List.map (fun ((cu,parents,die),mnnls) -> [" ";pp_die_abbrev_var c cu.cu_header str 0 false parents die ^ pp_type_info_die_DW_AT_type c d cu cu.cu_header str die; pp_analysed_locations2 c cu.cu_header mnnls] ) als ) let pp_analysed_location_data (d: dwarf) (als: analysed_location_data) : string = let c = p_context_of_d d in (* let cu = myhead d.d_compilation_units in let (cuh_default : compilation_unit_header) = cu.cu_header in *) pp_analysed_locations3 c (*HACK*) d d.d_str als *) let pp_analysed_locations3 c d str (removed:bool) (als: analysed_location_data) : (bool(*removed?*) * (string(*name*) * string(*offset*) * string(*kind*)) * (unit->string)(*string*)(*type*) * string(*locations*) * (unit->string)(*parents*)) list= (Lem_list.map (fun ((cu,parents,die1),mnnls) -> (removed, pp_die_abbrev_var c d cu str false parents die1, (*4.5s for only this*) (fun () -> pp_type_info_die_DW_AT_type c d cu str die1), (*12.2s for this and above*) pp_analysed_locations2 c cu.cu_header mnnls, (*12.4s for this and above*) (fun () -> pp_die_abbrev_var_parents c d cu str parents)) (*14.4s for this and above*) ) als) let pp_analysed_locations3_diff c d str (als_old: analysed_location_data) (als_new: analysed_location_data) : (bool(*removed?*) * (string(*name*) * string(*offset*) * string(*kind*)) * (unit->string)(*type*) * string(*locations*) *(unit->string)(*parents*)) list= ( (* maybe alpha sort these? *)let ppd_old = (pp_analysed_locations3 c d str true als_old) in let ppd_new = (pp_analysed_locations3 c d str false als_new) in (* the old entries that don't have a same-name new entry *) let ppd_gone = (List.filter (fun (removed,(name1,offset,kind),typ,locs,parents) -> not (List.exists (fun (removed',(name',offset',kind'),typ',locs',parents') -> name1=name') ppd_new)) ppd_old) in (* the new entries, each preceded by any same-name old entries (this will display strangely if there's any variable shadowing...) *) let ppd_upd = (List.concat (Lem_list.mapMaybe (fun (((removed,((name1,offset,kind) as y),typ,locs,parents) as x)) -> let same_name_old = (List.filter (fun (removed',(name',offset',kind'),typ',locs',parents') ->(Lem.pair_equal (=) (=) (name1,offset)(name',offset'))) ppd_old) in (match same_name_old with | [((removed',((name',offset',kind') as y'),typ',locs',parents') as x')] -> if (Lem.pair_equal (tripleEqual instance_Basic_classes_Eq_string_dict instance_Basic_classes_Eq_string_dict instance_Basic_classes_Eq_string_dict) (=) (y,(*typ,*)locs) (y',(*typ',*)locs')) then None else Some ( List.rev_append (List.rev same_name_old) [x]) | _ -> Some ( List.rev_append (List.rev same_name_old) [x]) )) ppd_new)) in List.rev_append (List.rev ppd_gone) ppd_upd) let pp_analysed_location_format (xs : (bool(*removed?*) * (string(*name*) * string(*offset*) * string(*kind*)) * (unit->string)(*string*)(*type*) * string(*locations*) * (unit->string)(*parents*)) list):string= (pad_rows (Lem_list.map (fun (((removed,(name1,offset,kind),typ,locs,parents) as x)) -> [ ((if removed then "-" else " ") ^ (name1 ^ (" (" ^ (offset ^ ("," ^ (kind ^ (") " ^ typ ()))))))); locs; parents ()] ) xs )) let pp_analysed_location_data (d: dwarf) (als: analysed_location_data) : string= (let c = (p_context_of_d d) in (* let cu = myhead d.d_compilation_units in let (cuh_default : compilation_unit_header) = cu.cu_header in *) pp_analysed_location_format (pp_analysed_locations3 c (*HACK*) d d.d_str false als)) let pp_analysed_location_data_diff (d: dwarf) (als_old: analysed_location_data) (als_new: analysed_location_data) : string= (let c = (p_context_of_d d) in (* let cu = myhead d.d_compilation_units in let (cuh_default : compilation_unit_header) = cu.cu_header in *) pp_analysed_location_format (pp_analysed_locations3_diff c (*HACK*) d d.d_str als_old als_new)) let pp_analysed_location_data_at_pc (d: dwarf) (alspc: analysed_location_data_at_pc) : string= (String.concat "" (Lem_list.map (fun ((cu,parents,die1),(n1,n2,sld,esl)) -> " " ^ (let name1 = ((match find_name_of_die d.d_str die1 with | Some s -> s | None -> "<no name>\n" )) in (match esl with | Success sl -> name1 ^ (" @ " ^ (pp_single_location sl ^"\n")) | Fail e -> name1 ^ (" @ " ^ ("<fail: " ^ (e ^ ">\n"))) )) ) alspc)) (*val analysed_locations_at_pc : evaluation_context -> dwarf_static -> natural -> analysed_location_data_at_pc*) let analysed_locations_at_pc (ev) (ds: dwarf_static) (pc: Nat_big_num.num) : analysed_location_data_at_pc= (let c : p_context = ({ endianness = (ds.ds_dwarf.d_endianness) }) in let xs = (Lem_list.mapMaybe (fun (cupd,mnns) -> (match mnns with | None -> None | Some nns -> let nns' = (List.filter (fun (n1,n2,sld) -> Nat_big_num.greater_equal pc n1 && Nat_big_num.less pc n2) nns) in (match nns' with | [] -> None | _ -> Some (cupd,nns') ) )) ds.ds_analysed_location_data) in List.concat (Lem_list.map (fun ((cu,parents,die1),nns) -> let ac = (arithmetic_context_of_cuh cu.cu_header) in let base_address1 = (cu_base_address cu) in let mfbloc : attribute_value option = (closest_enclosing_frame_base ds.ds_dwarf.d_loc base_address1 parents) in Lem_list.map (fun (n1,n2,sld) -> let el : single_location error = (evaluate_location_description_bytes c ds.ds_dwarf.d_loc ds.ds_evaluated_frame_info cu.cu_header ac ev mfbloc pc sld) in ((cu,parents,die1),(n1,n2,sld,el)) ) nns ) xs)) (*val names_of_address : dwarf -> analysed_location_data_at_pc -> natural -> list string*) let names_of_address (d: dwarf) (alspc: analysed_location_data_at_pc) (address: Nat_big_num.num) : string list= (Lem_list.mapMaybe (fun ((cu,parents,die1),(n1,n2,sld,esl)) -> (match esl with | Success (SL_simple (SL_memory_address a)) -> if Nat_big_num.equal a address then (match find_name_of_die d.d_str die1 with | Some s -> Some s | None -> None ) else None | Success _ -> None (* just suppress? *) | Fail e -> None (* just suppress? *) ) ) alspc) (*val filtered_analysed_location_data : dwarf_static -> natural -> analysed_location_data*) let filtered_analysed_location_data ds pc:((compilation_unit*(die)list*die)*((Nat_big_num.num*Nat_big_num.num*Byte_sequence_wrapper.byte_sequence)list)option)list= (Lem_list.mapMaybe (fun (cupd,mnns) -> (match mnns with | None -> None | Some nns -> let nns' = (List.filter (fun (n1,n2,sld) -> Nat_big_num.greater_equal pc n1 && Nat_big_num.less pc n2) nns) in (match nns' with | [] -> None (*Just (cupd,Nothing)*) | _::_ -> Some (cupd,Some nns') ) )) ds.ds_analysed_location_data) (** ********************************************************************** *) (** ** estimate source-file line extents of each (non-inlined) subprogram *) (** ********************************************************************** *) (* The line number info associates source-file line numbers to instruction addresses, but doesn't identify which subprogram those line numbers come from. To recover that, we can use the DW_TAG_subprogram die DW_AT_decl_file and DW_AT_decl_line info, which gives the start of each subprogram. For C, function definitions cannot be nested, so we can estimate their line-number extents as from their start to the start of the next. Note that this might be wrong if there are (eg) macro definitions between C functions. Because of the lack of nesting, for C, just taking the top-level DW_TAG_subprogram dies of each compilation unit should be basically ok, and seems also to exclude inlined instances of subprograms (which otherwise we could exclude by discarding any with an abstract origin). However, those top-level subprograms are not necessarily all from the "primary" file of the subprogram, and conceivably some functions in the file might not be included in that compilation unit but appear in another. We'll therefore take all top-level subprograms from all compilation units, partition by file (up to equality of (compilation directory, include directory, and path)), and then sort. This assumes that the directory and path strings from the line number info for different compilation units are nicely comparable. We also have to identify the compilation unit referred to by a line number file entry that's been reported from the line-number info. The DW_TAG_compile_unit DW_AT_name appears to be the path concatentation (inserting a "/", not just the string concatenation) of the lnfe_directory_index's string and the lnfe_path of one the lnfe's of the line number header pointed to by the compilation unit's DW_AT_stmt_list, but not necessarily any particular such lnfe.*) let subprogram_line_extents_compilation_unit d cu : (string * unpacked_file_entry * Nat_big_num.num) list= (let c = (p_context_of_d d) in let subprogram_dies = (List.filter (fun die' -> Nat_big_num.equal die'.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_subprogram")) cu.cu_die.die_children) in let lnp = (line_number_program_of_compilation_unit d cu) in let lnh = (lnp.lnp_header) in Lem_list.mapMaybe (fun die1 -> (match (find_name_of_die d.d_str die1, find_natural_attribute_value_of_die c "DW_AT_decl_file" die1, find_natural_attribute_value_of_die c "DW_AT_decl_line" die1) with | (Some name1, Some file, Some line) -> Some (name1, unpack_file_entry lnh file, line) | (_,_,_) -> None )) subprogram_dies) (* lookup in an association list and also return the list with that entry (if any) removed *) (*val extract : forall 'b 'c. Eq 'b => 'b -> list ('b * 'c) -> (maybe 'c) * list ('b * 'c)*) let rec extract dict_Basic_classes_Eq_b y yzs:'c option*('b*'c)list= ((match yzs with | [] -> (None, []) | (y',z')::yzs' -> if dict_Basic_classes_Eq_b.isEqual_method y' y then (Some z', yzs') else let (result,yzs'') = (extract dict_Basic_classes_Eq_b y yzs') in (result, ((y',z')::yzs'')) )) (* partition a list by the result of f, removing duplicates and sorting each partition by lt *) (*val partitionby: forall 'a 'b. Eq 'a , Eq 'b => ('a -> 'b) -> ('a -> 'a -> bool) -> list 'a -> list ('b * list 'a) -> list ('b * list 'a)*) let rec partitionby dict_Basic_classes_Eq_a dict_Basic_classes_Eq_b f lt xs acc:('b*'a list)list= ((match xs with | [] -> acc | x::xs' -> let y = (f x) in let (result, acc') = (extract dict_Basic_classes_Eq_b y acc) in let acc'' = ((match result with | Some xs'' -> if Lem_list.elem dict_Basic_classes_Eq_a x xs'' then acc else ((y, Lem_sorting.insertBy lt x xs'')::acc') | None -> (y,[x])::acc )) in partitionby dict_Basic_classes_Eq_a dict_Basic_classes_Eq_b f lt xs' acc'' )) let subprogram_line_extents d : (unpacked_file_entry * (string * unpacked_file_entry * Nat_big_num.num) list ) list= (let subprograms : (string * unpacked_file_entry * Nat_big_num.num) list = (List.concat (map (subprogram_line_extents_compilation_unit d) d.d_compilation_units)) in partitionby (instance_Basic_classes_Eq_tup3_dict instance_Basic_classes_Eq_string_dict (instance_Basic_classes_Eq_tup3_dict (instance_Basic_classes_Eq_Maybe_maybe_dict instance_Basic_classes_Eq_string_dict) (instance_Basic_classes_Eq_Maybe_maybe_dict instance_Basic_classes_Eq_string_dict) instance_Basic_classes_Eq_string_dict) instance_Basic_classes_Eq_Num_natural_dict) (instance_Basic_classes_Eq_tup3_dict (instance_Basic_classes_Eq_Maybe_maybe_dict instance_Basic_classes_Eq_string_dict) (instance_Basic_classes_Eq_Maybe_maybe_dict instance_Basic_classes_Eq_string_dict) instance_Basic_classes_Eq_string_dict) (fun (name1, ufe, line) -> ufe) (fun (name1,ufe,line) -> fun (name',ufe',line') -> Nat_big_num.less line line') subprograms []) let pp_subprograms dict_Show_Show_a sles:string= (String.concat "\n" (Lem_list.map (fun (ufe,sles') -> pp_ufe ufe ^ ("\n" ^ String.concat "" (Lem_list.map (fun (name1, ufe, line) -> " " ^ ( dict_Show_Show_a.show_method line ^ (" " ^ (name1 ^ "\n")))) sles'))) sles)) let rec find_by_line dict_Basic_classes_Ord_b line sles line_last name_last:'a= ((match sles with | [] -> name_last | (name',ufe',line') :: sles' -> if dict_Basic_classes_Ord_b.isGreaterEqual_method line line_last && dict_Basic_classes_Ord_b.isLess_method line line' then name_last else find_by_line dict_Basic_classes_Ord_b line sles' line' name' )) let subprogram_at_line subprogram_line_extents1 (ufe:unpacked_file_entry) (line:Nat_big_num.num) : string= ((match (lookupBy (tripleEqual (instance_Basic_classes_Eq_Maybe_maybe_dict instance_Basic_classes_Eq_string_dict) (instance_Basic_classes_Eq_Maybe_maybe_dict instance_Basic_classes_Eq_string_dict) instance_Basic_classes_Eq_string_dict) ufe subprogram_line_extents1) with | None -> "no matching unpacked_file_entry" | Some sles -> find_by_line instance_Basic_classes_Ord_Num_natural_dict line sles( (Nat_big_num.of_int 0)) "file preamble" )) (** ************************************************************ *) (** ** pull out subprograms *) (** ************************************************************ *) (* val analyse_subprograms : dwarf -> analysed_location_data let analyse_subprograms (d: dwarf) : analysed_location_data = let c = p_context_of_d d in let (cuh_default : compilation_unit_header) = let cu = myhead d.d_compilation_units in cu.cu_header in (* find all DW_TAG_subprogram dies *) let tags = List.map tag_encode ["DW_TAG_subprogram"] in let dies : list (compilation_unit * (list die) * die) = find_dies (fun die -> List.elem die.die_abbreviation_declaration.ad_tag tags && has_attribute "DW_AT_name" die && has_attribute "DW_AT_location" die) d in List.map (fun (((cu:compilation_unit), (parents: list die), (die: die)) as x) -> let name = match find_name_of_die d.d_str die with | Just s -> s | Nothing -> "<no name>\n" end in let entry_point : maybe attribute_value = match find_attribute_value "DW_AT_entry_pc" die with | Nothing -> Nothing | let base_address = cu_base_address cu in let interpreted_locations : maybe (list (natural * natural * single_location_description)) = interpreted_location_of_die c cuh_default d.d_loc d.d_ranges base_address parents die in (x,interpreted_locations) ) dies *) (** ************************************************************ *) (** ** evaluation of line-number info *) (** ************************************************************ *) let initial_line_number_registers (lnh: line_number_header) : line_number_registers= ({ lnr_address =( (Nat_big_num.of_int 0)); lnr_op_index =( (Nat_big_num.of_int 0)); lnr_file =( (Nat_big_num.of_int 1)); lnr_line =( (Nat_big_num.of_int 1)); lnr_column =( (Nat_big_num.of_int 0)); lnr_is_stmt = (lnh.lnh_default_is_stmt); lnr_basic_block = false; lnr_end_sequence = false; lnr_prologue_end = false; lnr_epilogue_begin = false; lnr_isa =( (Nat_big_num.of_int 0)); lnr_discriminator =( (Nat_big_num.of_int 0)); }) let evaluate_line_number_operation (lnh: line_number_header) ((s: line_number_registers), (lnrs: line_number_registers list)) (lno: line_number_operation) : line_number_registers * line_number_registers list= (let new_address s operation_advance= (Nat_big_num.add s.lnr_address (Nat_big_num.mul lnh.lnh_minimum_instruction_length (Nat_big_num.div( Nat_big_num.add s.lnr_op_index operation_advance)lnh.lnh_maximum_operations_per_instruction))) in let new_op_index s operation_advance= (Nat_big_num.modulus ( Nat_big_num.add s.lnr_op_index operation_advance) lnh.lnh_maximum_operations_per_instruction) in (match lno with | DW_LN_special adjusted_opcode -> let operation_advance = (Nat_big_num.div adjusted_opcode lnh.lnh_line_range) in let line_increment = (Nat_big_num.add lnh.lnh_line_base (( Nat_big_num.modulus adjusted_opcode lnh.lnh_line_range))) in let s' = ({ s with lnr_line = (partialNaturalFromInteger ( Nat_big_num.add( s.lnr_line) line_increment)); lnr_address = (new_address s operation_advance); lnr_op_index = (new_op_index s operation_advance); }) in let lnrs' = (s'::lnrs) in let s'' = ({ s' with lnr_basic_block = false; lnr_prologue_end = false; lnr_epilogue_begin = false; lnr_discriminator =( (Nat_big_num.of_int 0)); }) in (s'', lnrs') | DW_LNS_copy -> let lnrs' = (s::lnrs) in let s' = ({ s with lnr_basic_block = false; lnr_prologue_end = false; lnr_epilogue_begin = false; lnr_discriminator =( (Nat_big_num.of_int 0)); }) in (s', lnrs') | DW_LNS_advance_pc operation_advance -> let s' = ({ s with lnr_address = (new_address s operation_advance); lnr_op_index = (new_op_index s operation_advance); }) in (s', lnrs) | DW_LNS_advance_line line_increment -> let s' = ({ s with lnr_line = (partialNaturalFromInteger ( Nat_big_num.add( s.lnr_line) line_increment)) }) in (s', lnrs) | DW_LNS_set_file n -> let s' = ({ s with lnr_file = n }) in (s', lnrs) | DW_LNS_set_column n -> let s' = ({ s with lnr_column = n }) in (s', lnrs) | DW_LNS_negate_stmt -> let s' = ({ s with lnr_is_stmt = (not s.lnr_is_stmt) }) in (s', lnrs) | DW_LNS_set_basic_block -> let s' = ({ s with lnr_basic_block = true }) in (s', lnrs) | DW_LNS_const_add_pc -> let opcode =( (Nat_big_num.of_int 255)) in let adjusted_opcode = (Nat_big_num.sub_nat opcode lnh.lnh_opcode_base) in let operation_advance = (Nat_big_num.div adjusted_opcode lnh.lnh_line_range) in let s' = ({ s with lnr_address = (new_address s operation_advance); lnr_op_index = (new_op_index s operation_advance); }) in (s', lnrs) | DW_LNS_fixed_advance_pc n -> let s' = ({ s with lnr_address = (Nat_big_num.add s.lnr_address n); lnr_op_index =( (Nat_big_num.of_int 0)); }) in (s', lnrs) | DW_LNS_set_prologue_end -> let s' = ({ s with lnr_prologue_end = true }) in (s', lnrs) | DW_LNS_set_epilogue_begin -> let s' = ({ s with lnr_epilogue_begin = true }) in (s', lnrs) | DW_LNS_set_isa n -> let s' = ({ s with lnr_isa = n }) in (s', lnrs) | DW_LNE_end_sequence -> let s' = ({ s with lnr_end_sequence = true }) in let lnrs' = (s' :: lnrs) in let s'' = (initial_line_number_registers lnh) in (s'', lnrs') | DW_LNE_set_address n -> let s' = ({ s with lnr_address = n; lnr_op_index =( (Nat_big_num.of_int 0)); }) in (s', lnrs) | DW_LNE_define_file( s, n1, n2, n3) -> failwith "DW_LNE_define_file not implemented" (*TODO: add to file list in header - but why is this in the spec? *) | DW_LNE_set_discriminator n -> let s' = ({ s with lnr_discriminator = n }) in (s', lnrs) )) let rec evaluate_line_number_operations (lnh: line_number_header) ((s: line_number_registers), (lnrs: line_number_registers list)) (lnos: line_number_operation list) : line_number_registers * line_number_registers list= ((match lnos with | [] -> (s,lnrs) | lno :: lnos' -> let (s',lnrs') = (evaluate_line_number_operation lnh (s,lnrs) lno) in evaluate_line_number_operations lnh (s',lnrs') lnos' )) let evaluate_line_number_program (lnp:line_number_program) : line_number_registers list= (List.rev (snd (evaluate_line_number_operations lnp.lnp_header ((initial_line_number_registers lnp.lnp_header),[]) lnp.lnp_operations))) let evaluated_line_info_of_compilation_unit d cu evaluated_line_info1:'a= (let c = (p_context_of_d d) in let offset = (line_number_offset_of_compilation_unit c cu) in (match Lem_list.list_find_opt (fun (lnh,lnrs) -> Nat_big_num.equal lnh.lnh_offset offset) evaluated_line_info1 with | None -> failwith "compilation unit line number offset not found" | Some (lnh,lnrs) ->lnrs )) let pp_line_number_registers lnr:string= ("" ^ ("address = " ^ (pphex lnr.lnr_address ^ ("\n" ^ ("op_index = " ^ (Nat_big_num.to_string lnr.lnr_op_index ^ ("\n" ^ ("file = " ^ (Nat_big_num.to_string lnr.lnr_file ^ ("\n" ^ ("line = " ^ (Nat_big_num.to_string lnr.lnr_line ^ ("\n" ^ ("column = " ^ (Nat_big_num.to_string lnr.lnr_column ^ ("\n" ^ ("is_stmt = " ^ (string_of_bool lnr.lnr_is_stmt ^ ("\n" ^ ("basic_block = " ^ (string_of_bool lnr.lnr_basic_block ^ ("\n" ^ ("end_sequence = " ^ (string_of_bool lnr.lnr_end_sequence ^ ("\n" ^ ("prologue_end = " ^ (string_of_bool lnr.lnr_prologue_end ^ ("\n" ^ ("epilogue_begin = " ^ (string_of_bool lnr.lnr_epilogue_begin ^ ("\n" ^ ("isa = " ^ (Nat_big_num.to_string lnr.lnr_isa ^ ("\n" ^ ("discriminator = " ^ (pphex lnr.lnr_discriminator ^ "\n")))))))))))))))))))))))))))))))))))) let pp_line_number_registers_tight lnr : string list= ([ pphex lnr.lnr_address ; Nat_big_num.to_string lnr.lnr_op_index ; Nat_big_num.to_string lnr.lnr_file ; Nat_big_num.to_string lnr.lnr_line ; Nat_big_num.to_string lnr.lnr_column ; string_of_bool lnr.lnr_is_stmt ; string_of_bool lnr.lnr_basic_block ; string_of_bool lnr.lnr_end_sequence ; string_of_bool lnr.lnr_prologue_end ; string_of_bool lnr.lnr_epilogue_begin ; Nat_big_num.to_string lnr.lnr_isa ; pphex lnr.lnr_discriminator ]) let pp_line_number_registerss lnrs:string= (pad_rows ( ["address"; "op_index"; "file"; "line"; "column"; "is_stmt"; "basic_block"; "end_sequence"; "prologue_end"; "epilogue_begin"; "isa"; "discriminator"] :: (Lem_list.map pp_line_number_registers_tight lnrs) )) let pp_evaluated_line_info (eli: evaluated_line_info) : string= (String.concat "\n" (Lem_list.map (fun (lnh,lnrs) -> pp_line_number_header lnh ^ ("\n" ^ pp_line_number_registerss lnrs)) eli)) (* readef example: Decoded dump of debug contents of section .debug_line: CU: /var/local/stephen/work/devel/rsem/ppcmem2/system/tests-adhoc/simple-malloc/test-concurrent.c: File name Line number Starting address test-concurrent.c 11 0x400144 test-concurrent.c 12 0x40014c test-concurrent.c 13 0x400154 test-concurrent.c 14 0x400158 test-concurrent.c 17 0x400160 /var/local/stephen/work/devel/rsem/ppcmem2/system/tests-adhoc/simple-malloc/../thread_start_aarch64.h: thread_start_aarch64.h 34 0x400168 thread_start_aarch64.h 36 0x400174 /var/local/stephen/work/devel/rsem/ppcmem2/system/tests-adhoc/simple-malloc/test-concurrent.c: test-concurrent.c 19 0x400174 test-concurrent.c 20 0x40017c test-concurrent.c 22 0x400180 CU: /var/local/stephen/work/devel/rsem/ppcmem2/system/tests-adhoc/simple-malloc/malloc.c: ... *) let source_lines_of_address (ds:dwarf_static) (a: Nat_big_num.num) : ( unpacked_file_entry * Nat_big_num.num * line_number_registers * string (*function*)) list= (List.concat (Lem_list.map (fun (lnh, lnrs) -> myfiltermaybe (fun lnr -> if Nat_big_num.equal a lnr.lnr_address && not lnr.lnr_end_sequence then Some (unpack_file_entry lnh lnr.lnr_file, lnr.lnr_line, lnr, subprogram_at_line ds.ds_subprogram_line_extents (unpack_file_entry lnh lnr.lnr_file) lnr.lnr_line) else None) lnrs ) ds.ds_evaluated_line_info )) (** ************************************************************ *) (** ** collecting all the statically calculated analysis info *) (** ************************************************************ *) (*val extract_dwarf_static : elf_file -> maybe dwarf_static*) let extract_dwarf_static f1:(dwarf_static)option= ((match extract_dwarf f1 with | None -> None | Some dwarf1 -> (*let _ = my_debug5 (pp_dwarf dwarf) in *) let ald : analysed_location_data = (analyse_locations dwarf1) in let efi : evaluated_frame_info = (evaluate_frame_info dwarf1) in let eli : evaluated_line_info = (Lem_list.map (fun lnp -> (lnp.lnp_header, evaluate_line_number_program lnp)) dwarf1.d_line_info) in let sle = (subprogram_line_extents dwarf1) in let ds = ({ ds_dwarf = dwarf1; ds_analysed_location_data = ald; ds_evaluated_frame_info = efi; ds_evaluated_line_info = eli; ds_subprogram_line_extents = sle; }) in Some ds )) (** ************************************************************ *) (** ** collect simple die tree view *) (** ************************************************************ *) let decl_of_die d subprogram_line_extents1 cu die1 : (unpacked_file_entry * int (*line*) * string (*subprogram name*))option= (let c = (p_context_of_d d) in let lnp = (line_number_program_of_compilation_unit d cu) in let lnh = (lnp.lnp_header) in (match (find_natural_attribute_value_of_die c "DW_AT_decl_file" die1, find_natural_attribute_value_of_die c "DW_AT_decl_line" die1) with | (Some file, Some line) -> let ufe = (unpack_file_entry lnh file) in let subprogram_name = (subprogram_at_line subprogram_line_extents1 ufe line) in Some (ufe, Nat_big_num.to_int line, subprogram_name) | (_,_) -> None )) let call_site_of_die d subprogram_line_extents1 cu die1 : (unpacked_file_entry * int (*line*) * string (*subprogram name*))option= (let c = (p_context_of_d d) in let lnp = (line_number_program_of_compilation_unit d cu) in let lnh = (lnp.lnp_header) in (match (find_natural_attribute_value_of_die c "DW_AT_call_file" die1, find_natural_attribute_value_of_die c "DW_AT_call_line" die1) with | (Some file, Some line) -> let ufe = (unpack_file_entry lnh file) in let subprogram_name = (subprogram_at_line subprogram_line_extents1 ufe line) in Some (ufe, Nat_big_num.to_int line, subprogram_name) | (_,_) -> None )) let mk_sdt_unspecified_parameter (d:dwarf) subprogram_line_extents1 cu parents die1 : sdt_unspecified_parameter option= (if not(Lem_list.elem instance_Basic_classes_Eq_Num_natural_dict die1.die_abbreviation_declaration.ad_tag [tag_encode "DW_TAG_unspecified_parameters"]) then None else Some ()) (* let strict_msvfp x e s z = match x with | Just y -> y | Nothing -> Assert_extra.failwith ("mk_sdt_variable_or_formal_parameter strict failure " ^ e ^ " on \n" ^ s z ^ "\n") end *) let rec mk_sdt_variable_or_formal_parameter (d:dwarf) subprogram_line_extents1 cu parents die1 : sdt_variable_or_formal_parameter option= (if not(Lem_list.elem instance_Basic_classes_Eq_Num_natural_dict die1.die_abbreviation_declaration.ad_tag [tag_encode "DW_TAG_variable"; tag_encode "DW_TAG_formal_parameter"]) then None else let c = (p_context_of_d d) in (* let s (cu,parents,die) = pp_die c cu.cu_header d.d_str true 0 false die in*) let cupdie1 = (cu,parents,die1) in let kind = (if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_variable") then SVPK_var else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_formal_parameter") then SVPK_param else failwith ("unreachable bad kind")) in (* find aDW_AT_specification die, if it exists. TODO: how should this interact with abstract origins? *) let mcupdie_spec = (find_reference_attribute_of_die c d cu d.d_str "DW_AT_specification" die1) in Some ( { svfp_cupdie = cupdie1; svfp_kind = kind; (* svfp_name = strict_msvfp (find_name_of_die_using_abstract_origin_and_spec c d cu d.d_str die mcupdie_spec) "no name" s cupdie;*) svfp_name = ((match (find_name_of_die_using_abstract_origin_and_spec c d cu d.d_str die1 mcupdie_spec) with Some name1 -> name1 | None -> "no name" )); svfp_type = (*strict_msvfp*) (find_DW_AT_type_of_die_deep_using_abstract_origin d cupdie1) (*"no type" s cupdie*); svfp_abstract_origin = ((match find_reference_attribute_of_die c d cu d.d_str "DW_AT_abstract_origin" die1 with | None -> None | Some (((cu',parents',die') as cupdie')) -> mk_sdt_variable_or_formal_parameter d subprogram_line_extents1 cu' parents' die' )); svfp_const_value = (find_integer_attribute_value_of_die c "DW_AT_const_value" die1); svfp_external = ((match find_flag_attribute_value_of_die_using_abstract_origin d "DW_AT_external" cupdie1 with Some b -> b | None -> false )); svfp_declaration = ((match find_flag_attribute_value_of_die_using_abstract_origin d "DW_AT_declaration" cupdie1 with Some b -> b | None -> false )); svfp_locations = (let base_address1 = (cu_base_address cu) in let interpreted_locations : ( (Nat_big_num.num * Nat_big_num.num * single_location_description)list)option = (interpreted_location_of_die c cu.cu_header d.d_str d.d_loc d.d_ranges base_address1 parents die1) in Lem.option_map (fun nnbss -> Lem_list.map (fun (n1,n2,bs) -> (n1,n2,parse_operations_bs c cu.cu_header bs)) nnbss) interpreted_locations); svfp_decl = (decl_of_die d subprogram_line_extents1 cu die1); } )) let strict_mss x e s z:'a= ((match x with | Some y -> y | None -> failwith ("mk_sdt_subroutine strict failure " ^ (e ^ (" on \n" ^ (s z ^ "\n")))) )) let rec mk_sdt_subroutine (d:dwarf) subprogram_line_extents1 (cu:compilation_unit) parents (die1:die) : sdt_subroutine option= (if not(Lem_list.elem instance_Basic_classes_Eq_Num_natural_dict die1.die_abbreviation_declaration.ad_tag [tag_encode "DW_TAG_subprogram"; tag_encode "DW_TAG_inlined_subroutine"]) then None else let c = (p_context_of_d d) in (* let s (cu,parents,die) : string = pp_die c cu.cu_header d.d_str true 0 false die in*) let cupdie1 = (cu, parents, die1) in let parents' = (die1::parents) in let kind = (if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_subprogram") then SSK_subprogram else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_inlined_subroutine") then SSK_inlined_subroutine else failwith ("unreachable bad kind")) in Some ( { ss_cupdie = cupdie1; ss_name =( (*strict_mss ( *)find_name_of_die_using_abstract_origin c d cu d.d_str die1)(* ) "no name" s cupdie;*); ss_kind = kind; ss_call_site = (call_site_of_die d subprogram_line_extents1 cu die1); ss_abstract_origin = ((match find_reference_attribute_of_die c d cu d.d_str "DW_AT_abstract_origin" die1 with | None -> None | Some (((cu',parents',die') as cupdie')) -> mk_sdt_subroutine d subprogram_line_extents1 cu' parents' die' )); ss_type = (find_DW_AT_type_of_die_deep(*_using_abstract_origin*) d cupdie1); ss_vars = (Lem_list.mapMaybe (mk_sdt_variable_or_formal_parameter d subprogram_line_extents1 cu parents') die1.die_children); ss_unspecified_parameters = (Lem_list.mapMaybe (mk_sdt_unspecified_parameter d subprogram_line_extents1 cu parents') die1.die_children); ss_entry_address = (entry_address die1); ss_pc_ranges = (range_of_die_d d cu die1); ss_subroutines = (Lem_list.mapMaybe (mk_sdt_subroutine d subprogram_line_extents1 cu parents') die1.die_children); ss_lexical_blocks = (Lem_list.mapMaybe (mk_sdt_lexical_block d subprogram_line_extents1 cu parents') die1.die_children); ss_decl = (decl_of_die d subprogram_line_extents1 cu die1); ss_noreturn = ((match find_flag_attribute_value_of_die_using_abstract_origin d "DW_AT_noreturn" cupdie1 with Some b -> b | None -> false )); ss_external = ((match find_flag_attribute_value_of_die_using_abstract_origin d "DW_AT_external" cupdie1 with Some b -> b | None -> false )); } )) and mk_sdt_lexical_block (d:dwarf) subprogram_line_extents1 (cu:compilation_unit) parents (die1:die) : sdt_lexical_block option= (if not ( Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_lexical_block")) then None else let c = (p_context_of_d d) in (*let s (cu,parents,die) : string = pp_die c cu.cu_header d.d_str true 0 false die in*) let cupdie1 = (cu, parents, die1) in let parents' = (die1::parents) in Some ( { slb_cupdie = cupdie1; slb_vars = (Lem_list.mapMaybe (mk_sdt_variable_or_formal_parameter d subprogram_line_extents1 cu parents') die1.die_children); slb_pc_ranges = (range_of_die_d d cu die1); slb_subroutines = (Lem_list.mapMaybe (mk_sdt_subroutine d subprogram_line_extents1 cu parents') die1.die_children); slb_lexical_blocks = (Lem_list.mapMaybe (mk_sdt_lexical_block d subprogram_line_extents1 cu parents') die1.die_children); } )) let strict_mscu x e s z:'a= ((match x with | Some y -> y | None -> failwith ("mk_sdt_compilation_unit strict failure " ^ (e ^ (" on \n" ^ (s z ^ "\n")))) )) let mk_sdt_compilation_unit (d:dwarf) subprogram_line_extents1 (cu:compilation_unit) : sdt_compilation_unit= (let c = (p_context_of_d d) in let s (cu,(parents: die list),die1) : string= (pp_die c cu.cu_header d.d_str true( (Nat_big_num.of_int 0)) false die1) in let cupdie1 = (cu, [], cu.cu_die) in let parents' = ([cu.cu_die]) in { scu_cupdie = (cu, [], cu.cu_die); scu_name = (strict_mscu (find_name_of_die d.d_str cu.cu_die) "no name" s cupdie1); scu_subroutines = (Lem_list.mapMaybe (mk_sdt_subroutine d subprogram_line_extents1 cu parents') cu.cu_die.die_children); scu_vars = (Lem_list.mapMaybe (mk_sdt_variable_or_formal_parameter d subprogram_line_extents1 cu parents') cu.cu_die.die_children); scu_pc_ranges = (range_of_die_d d cu cu.cu_die); }) let mk_sdt_dwarf (d:dwarf) subprogram_line_extents1 : sdt_dwarf= ({ sd_compilation_units = (Lem_list.map (mk_sdt_compilation_unit d subprogram_line_extents1) d.d_compilation_units); }) (* **** verbose pp of simple die tree view *************** *) let pp_sdt_unspecified_parameter (level:Nat_big_num.num) (sup:sdt_unspecified_parameter) : string= (indent_level true level ^ ("unspecified parameters" ^ "\n")) let pp_parsed_single_location_description (level:Nat_big_num.num) ((n1:Nat_big_num.num), (n2:Nat_big_num.num), (ops: operation list)) : string= (let indent = (indent_level true level) in indent ^ (pphex n1 ^ (" " ^ (pphex n2 ^ (" (" ^ (pp_operations ops ^(")" ^"\n"))))))) let pp_pc_ranges (level:Nat_big_num.num) (rso: ( (Nat_big_num.num*Nat_big_num.num)list)option):string= ((match rso with | None -> "none\n" | Some rs -> let indent = (indent_level true level) in "\n" ^ String.concat "" (Lem_list.map (fun (n1,n2) -> indent ^ (pphex n1 ^ (" " ^ (pphex n2 ^ "\n")))) rs) )) let pp_sdt_maybe x f:string= ((match x with None -> "none\n" | Some y -> f y )) let pp_sdt_maybe' f x:string= (pp_sdt_maybe x f) let pp_sdt_list xs f:string= ((match xs with [] -> "none\n" | _ -> "\n" ^ String.concat "" ((Lem_list.map f) xs) )) let pp_sdt_variable_or_formal_parameter (level:Nat_big_num.num) (svfp: sdt_variable_or_formal_parameter) : string= (let indent = (indent_level true level) in "" ^ (indent ^ ("name:" ^ (svfp.svfp_name ^ ("\n" ^ (indent ^ ("cupdie:" ^ (pp_cupdie3 svfp.svfp_cupdie ^ ("\n" ^ (indent ^ ("kind:" ^ (((match svfp.svfp_kind with SVPK_var -> "var" | SVPK_param -> "param" )) ^ ("\n" ^ (indent ^ ("type:" ^ (pp_sdt_maybe' pp_type_info_deep svfp.svfp_type ^ ("\n" ^ (indent ^ ("const_value:" ^ (string_of_maybe instance_Show_Show_Num_integer_dict svfp.svfp_const_value ^ ("\n" ^ (indent ^ ("external:" ^ (string_of_bool svfp.svfp_external ^ ("\n" ^ (indent ^ ("declaration:" ^ (string_of_bool svfp.svfp_declaration ^ ("\n" ^ (indent ^ ("locations:" ^ (pp_sdt_maybe svfp.svfp_locations (fun locs -> "\n" ^ String.concat "" (Lem_list.map (pp_parsed_single_location_description (Nat_big_num.add level( (Nat_big_num.of_int 1)))) locs)) ^ (indent ^ ("decl:" ^ (pp_sdt_maybe svfp.svfp_decl (fun ud -> "\n" ^ (indent_level true (Nat_big_num.add level( (Nat_big_num.of_int 1))) ^ (pp_ud ud ^ "\n"))) ^ "\n"))))))))))))))))))))))))))))))))))) let rec pp_sdt_subroutine (level:Nat_big_num.num) (ss:sdt_subroutine) : string= (let indent = (indent_level true level) in "" ^ (indent ^ ("name:" ^ (pp_sdt_maybe ss.ss_name (fun name1 -> name1 ^ "\n") ^ (indent ^ ("cupdie:" ^ (pp_cupdie3 ss.ss_cupdie ^ ("\n" ^ (indent ^ ("kind:" ^ (((match ss.ss_kind with SSK_subprogram -> "subprogram" | SSK_inlined_subroutine -> "inlined subroutine" )) ^ ("\n" ^ (indent ^ ("call site:" ^ (pp_sdt_maybe ss.ss_call_site (fun ud -> "\n" ^ (indent_level true (Nat_big_num.add level( (Nat_big_num.of_int 1))) ^ (pp_ud ud ^ "\n"))) ^ (indent ^ ("abstract origin:" ^ (pp_sdt_maybe ss.ss_abstract_origin (pp_sdt_subroutine (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("type:" ^ (pp_sdt_maybe ss.ss_type (fun typ -> pp_type_info_deep typ ^"\n") ^ (indent ^ ("vars:" ^ (pp_sdt_list ss.ss_vars (pp_sdt_variable_or_formal_parameter (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("unspecified_parameters:" ^ (pp_sdt_list ss.ss_unspecified_parameters (pp_sdt_unspecified_parameter (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("entry address: " ^ (pp_sdt_maybe ss.ss_entry_address (fun n -> pphex n^"\n") ^ (indent ^ ("pc ranges:" ^ (pp_pc_ranges (Nat_big_num.add level( (Nat_big_num.of_int 1))) ss.ss_pc_ranges ^ (indent ^ ("subroutines:" ^ (pp_sdt_list ss.ss_subroutines (pp_sdt_subroutine (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("lexical_blocks:" ^ (pp_sdt_list ss.ss_lexical_blocks (pp_sdt_lexical_block (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("decl:" ^ (pp_sdt_maybe ss.ss_decl (fun ud -> "\n" ^ (indent_level true (Nat_big_num.add level( (Nat_big_num.of_int 1))) ^ (pp_ud ud ^ "\n"))) ^ (indent ^ ("noreturn:" ^ (string_of_bool ss.ss_noreturn ^ ("\n" ^ (indent ^ ("external:" ^ (string_of_bool ss.ss_external ^("\n" ^ "\n")))))))))))))))))))))))))))))))))))))))))))))))))) and pp_sdt_lexical_block (level:Nat_big_num.num) (lb:sdt_lexical_block) : string= (let indent = (indent_level true level) in "" ^ (indent ^ ("cupdie:" ^ (pp_cupdie3 lb.slb_cupdie ^ ("\n" ^ (indent ^ ("pc ranges:" ^ (pp_pc_ranges (Nat_big_num.add level( (Nat_big_num.of_int 1))) lb.slb_pc_ranges ^ (indent ^ ("vars:" ^ (pp_sdt_list lb.slb_vars (pp_sdt_variable_or_formal_parameter (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("subroutines :" ^ (pp_sdt_list lb.slb_subroutines (pp_sdt_subroutine (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("lexical_blocks:" ^ (pp_sdt_list lb.slb_lexical_blocks (pp_sdt_lexical_block (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ "\n"))))))))))))))))) let pp_sdt_compilation_unit (level:Nat_big_num.num) (cu:sdt_compilation_unit) : string= (let indent = (indent_level true level) in "" ^ (indent ^ ("name:" ^ (cu.scu_name ^ ("\n" ^ (indent ^ ("cupdie:" ^ (pp_cupdie3 cu.scu_cupdie ^ ("\n" ^ (indent ^ ("pc ranges:" ^ (pp_pc_ranges (Nat_big_num.add level( (Nat_big_num.of_int 1))) cu.scu_pc_ranges ^ (indent ^ ("vars:" ^ (pp_sdt_list cu.scu_vars (pp_sdt_variable_or_formal_parameter (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("subroutines :" ^ (pp_sdt_list cu.scu_subroutines (pp_sdt_subroutine (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ "\n")))))))))))))))))) let pp_sdt_dwarf (sdt_d:sdt_dwarf) : string= (let indent_level1 =( (Nat_big_num.of_int 0)) in String.concat "" (Lem_list.map (pp_sdt_compilation_unit indent_level1) sdt_d.sd_compilation_units)) (* **** concise pp of simple die tree view *************** *) (* **************** global vars ************* *) let pp_sdt_concise_variable_or_formal_parameter (level:Nat_big_num.num) (svfp: sdt_variable_or_formal_parameter) : string= (let indent = (indent_level true level) in "" ^ (indent (* ^ indent ^ "cupdie:" ^ pp_cupdie3 svfp.svfp_cupdie ^ "\n"*) (*^ indent ^ "name:" ^*) ^ (svfp.svfp_name ^ (" " (*^ indent ^ "kind:" *) ^ (((match svfp.svfp_kind with SVPK_var -> "var" | SVPK_param -> "param" )) ^ (" " (*^ indent ^ "type:" *) ^ (pp_sdt_maybe' pp_type_info_deep svfp.svfp_type ^ (" " (*^ indent ^ "const_value:"*) ^ ((match svfp.svfp_const_value with | None -> "" | Some v -> "const:"^(Nat_big_num.to_string v ^ " ") ) (*^ indent ^ "external:" ^ show svfp.svfp_external ^ "\n"*) (*^ indent ^ "declaration:" ^ show svfp.svfp_declaration ^ "\n"*) (*^ indent ^ "locations:" *) ^ ((match svfp.svfp_locations with None -> "no locations\n" | Some locs -> "\n" ^ String.concat "" (Lem_list.map (pp_parsed_single_location_description (Nat_big_num.add level( (Nat_big_num.of_int 1)))) locs) ))))))))))) (* ^ indent ^ "decl:" ^ (match svfp.svfp_decl with Nothing -> "none\n" | Just ((ufe,line) as ud) -> "\n" ^ indent_level true (level+1) ^ pp_ufe ufe ^ " " ^ show line ^ "\n" end)*) let pp_sdt_globals_compilation_unit (level:Nat_big_num.num) (cu:sdt_compilation_unit) : string= (let indent = (indent_level true level) in "" (* ^ indent ^ "cupdie:" ^ pp_cupdie3 cu.scu_cupdie ^ "\n"*) ^ (indent ^ ( (*"name:" ^*)cu.scu_name ^ ("\n" (* ^ indent ^ "vars:" ^ "\n"*) ^ String.concat "" (Lem_list.map (pp_sdt_concise_variable_or_formal_parameter (Nat_big_num.add level( (Nat_big_num.of_int 1)))) cu.scu_vars))))) (* ^ indent ^ "subroutines :" ^ (match cu.scu_subroutines with | [] -> "none\n" | sus -> "\n" ^ String.concat "\n" (List.map (pp_sdt_subroutine (level+1)) sus) end) *) let pp_sdt_globals_dwarf (sdt_d:sdt_dwarf) : string= (let indent_level1 =( (Nat_big_num.of_int 0)) in String.concat "" (Lem_list.map (pp_sdt_globals_compilation_unit indent_level1) sdt_d.sd_compilation_units)) (* ****************** local vars *************** *) let rec pp_sdt_locals_subroutine (level:Nat_big_num.num) (ss:sdt_subroutine) : string= (let indent = (indent_level true level) in "" ^ (indent (*^ "name:" ^*) ^ (pp_sdt_maybe ss.ss_name (fun name1 -> name1 ^ "\n") (* ^ indent ^ "cupdie:" ^ pp_cupdie3 ss.ss_cupdie ^ "\n"*) ^ (indent ^ ("kind:" ^ (((match ss.ss_kind with SSK_subprogram -> "subprogram" | SSK_inlined_subroutine -> "inlined subroutine" )) ^ ("\n" ^ (indent ^ ("entry address: " ^ (pp_sdt_maybe ss.ss_entry_address (fun n -> pphex n^"\n") ^ (indent ^ ("call site:" ^ (pp_sdt_maybe ss.ss_call_site (fun ud -> "\n" ^ (indent_level true (Nat_big_num.add level( (Nat_big_num.of_int 1))) ^ (pp_ud ud ^ "\n"))) ^ (indent ^ ("abstract origin:" ^ (pp_sdt_maybe ss.ss_abstract_origin (fun s -> "\n" ^ pp_sdt_locals_subroutine (Nat_big_num.add level( (Nat_big_num.of_int 1))) s) (* ^ indent ^ "type:" ^ pp_sdt_maybe ss.ss_type (fun typ -> pp_type_info_deep typ ^"\n" end)*) ^ (indent ^ ("vars:" ^ (pp_sdt_list ss.ss_vars (pp_sdt_concise_variable_or_formal_parameter (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("unspecified_parameters:" ^ (pp_sdt_list ss.ss_unspecified_parameters (pp_sdt_unspecified_parameter (Nat_big_num.add level( (Nat_big_num.of_int 1)))) (* ^ indent ^ "pc ranges:" ^ pp_pc_ranges (level+1) ss.ss_pc_ranges*) ^ (indent ^ ("subroutines:" ^ (pp_sdt_list ss.ss_subroutines (pp_sdt_locals_subroutine (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("lexical_blocks:" ^ (pp_sdt_list ss.ss_lexical_blocks (pp_sdt_locals_lexical_block (Nat_big_num.add level( (Nat_big_num.of_int 1)))) (* ^ indent ^ "decl:" ^ pp_sdt_maybe ss.ss_decl (fun ((ufe,line) as ud) -> "\n" ^ indent_level true (level+1) ^ pp_ufe ufe ^ " " ^ show line ^ "\n" end)*) (* ^ indent ^ "noreturn:" ^ show ss.ss_noreturn ^ "\n"*) (* ^ indent ^ "external:" ^ show ss.ss_external ^"\n"*) ^ "\n")))))))))))))))))))))))))))) and pp_sdt_locals_lexical_block (level:Nat_big_num.num) (lb:sdt_lexical_block) : string= (let indent = (indent_level true level) in "" (* ^ indent ^ "cupdie:" ^ pp_cupdie3 lb.slb_cupdie ^ "\n"*) ^ (indent ^ ("vars:" ^ (pp_sdt_list lb.slb_vars (pp_sdt_concise_variable_or_formal_parameter (Nat_big_num.add level( (Nat_big_num.of_int 1)))) (* ^ indent ^ "pc ranges:" ^ pp_pc_ranges (level+1) lb.slb_pc_ranges*) ^ (indent ^ ("subroutines :" ^ (pp_sdt_list lb.slb_subroutines (pp_sdt_locals_subroutine (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("lexical_blocks:" ^ (pp_sdt_list lb.slb_lexical_blocks (pp_sdt_locals_lexical_block (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ "\n")))))))))) let pp_sdt_locals_compilation_unit (level:Nat_big_num.num) (cu:sdt_compilation_unit) : string= (let indent = (indent_level true level) in "" ^ (indent (*^ "name:" *) ^ (cu.scu_name ^ ("\n" (* ^ indent ^ "cupdie:" ^ pp_cupdie3 cu.scu_cupdie ^ "\n"*) ^ (indent ^ ("vars:" ^ (pp_sdt_list cu.scu_vars (pp_sdt_concise_variable_or_formal_parameter (Nat_big_num.add level( (Nat_big_num.of_int 1)))) ^ (indent ^ ("subroutines :" ^ pp_sdt_list cu.scu_subroutines (pp_sdt_locals_subroutine (Nat_big_num.add level( (Nat_big_num.of_int 1))))))))))))) let pp_sdt_locals_dwarf (sdt_d:sdt_dwarf) : string= (let indent_level1 =( (Nat_big_num.of_int 0)) in String.concat "" (Lem_list.map (pp_sdt_locals_compilation_unit indent_level1) sdt_d.sd_compilation_units)) (** ************************************************************ *) (** ** analysis of inlined_subroutine data *) (** ************************************************************ *) (* old version, directly over die tree *) (* let strict_ais x e s z = match x with | Just y -> y | Nothing -> Assert_extra.failwith ("analyse_inlined_subroutine strict failure " ^ e ^ " on \n" ^ s z ^ "\n") end val analyse_inlined_subroutines : dwarf -> inlined_subroutine_data let analyse_inlined_subroutines (d: dwarf) : inlined_subroutine_data = let c = p_context_of_d d in let s (cu,parents,die) = pp_die c cu.cu_header d.d_str true 0 false die in let inlined_subroutines : list (compilation_unit * (list die) * die) = find_dies (fun die -> die.die_abbreviation_declaration.ad_tag = tag_encode "DW_TAG_inlined_subroutine") d in List.map (fun (((cu:compilation_unit), (parents: list die), (die: die)) as inlined_subroutine) -> let ((cu',parents,die') as abstract_origin) : compilation_unit * (list die) * die = strict_ais (find_reference_attribute_of_die c d cu d.d_str "DW_AT_abstract_origin" die) "no abstract origin" s inlined_subroutine in let name : string = strict_ais (find_name_of_die d.d_str die') "no abstract origin name" s abstract_origin in let call_file : unpacked_file_entry = let file_index = strict_ais (find_natural_attribute_value_of_die c "DW_AT_call_file" die) "no DW_AT_call_file" s inlined_subroutine in unpack_file_entry (line_number_program_of_compilation_unit d cu).lnp_header file_index in (* match filename d cu file_index with | Just s -> s | Nothing -> "none" end in*) let call_line : natural = strict_ais (find_natural_attribute_value_of_die c "DW_AT_call_line" die) "no DW_AT_call_line" s inlined_subroutine in let pc_ranges : list (natural*natural) = strict_ais (closest_enclosing_range c d.d_ranges (cu_base_address cu) [die](*deliberately ignore parents*)) "no pc ranges" s inlined_subroutine in let const_params = List.mapMaybe (fun die'' -> if die''.die_abbreviation_declaration.ad_tag = tag_encode "DW_TAG_formal_parameter" then match find_reference_attribute_of_die c d cu d.d_str "DW_AT_abstract_origin" die'' with | Nothing -> Nothing | Just abstract_origin' -> match find_integer_attribute_value_of_die c "DW_AT_const_value" die'' with | Nothing -> Nothing | Just n -> Just (<| iscp_abstract_origin = abstract_origin'; iscp_value = n; |>) end end else Nothing ) die.die_children in <| is_inlined_subroutine = inlined_subroutine; is_abstract_origin = abstract_origin; is_name = name; is_call_file = call_file; is_call_line = call_line; is_pc_ranges = pc_ranges; is_const_params = const_params; |> ) inlined_subroutines *) (* new version, over simple-die-tree view, but still producing the previous old-style datastructure *) let analyse_inlined_subroutines_sdt_const_param (svfp:sdt_variable_or_formal_parameter) : inlined_subroutine_const_param option= ((match (svfp.svfp_kind, svfp.svfp_abstract_origin, svfp.svfp_const_value) with | (SVPK_param, Some svfp', Some n) -> Some ({ iscp_abstract_origin = (svfp'.svfp_cupdie); iscp_value = n; }) | _ -> None )) let rec analyse_inlined_subroutines_sdt_subroutine (sdt_parents: sdt_subroutine list) (ss:sdt_subroutine) : inlined_subroutine list= (let this : inlined_subroutine list = ((match (ss.ss_kind, ss.ss_abstract_origin) with | (SSK_inlined_subroutine, Some ss') -> let ((call_file:unpacked_file_entry),(call_line:Nat_big_num.num)) = ((match ss.ss_call_site with | Some (((ufe,line,subprogram_name) as ud):unpacked_decl) -> (ufe,Nat_big_num.of_int line) | None -> failwith "analyse_inlined_subroutines_sdt_subroutine found no ss_call_site" )) in let pc_ranges = ((match ss.ss_pc_ranges with | Some pc_ranges -> pc_ranges | None -> failwith "analyse_inlined_subroutines_sdt_subroutine found no ss_pc_ranges" )) in let const_params = (Lem_list.mapMaybe analyse_inlined_subroutines_sdt_const_param ss.ss_vars) in [ ({ is_inlined_subroutine = (ss.ss_cupdie); is_abstract_origin = (ss'.ss_cupdie); is_inlined_subroutine_sdt = ss; is_inlined_subroutine_sdt_parents = sdt_parents; is_name = ((match ss.ss_name with Some name1->name1 | None -> "no name" )); is_call_file = call_file; is_call_line = call_line; is_pc_ranges = pc_ranges; is_const_params = const_params; } )] | (SSK_inlined_subroutine, None) -> failwith "analyse_inlined_subroutines_sdt_subroutine found SSK_inlined_subroutine without ss_abstract_origin" | _ -> [] )) in let sdt_parents' = (ss::sdt_parents) in List.rev_append (List.rev (List.rev_append (List.rev this) (List.concat (map (analyse_inlined_subroutines_sdt_subroutine sdt_parents') ss.ss_subroutines)))) (List.concat (map (analyse_inlined_subroutines_sdt_lexical_block sdt_parents') ss.ss_lexical_blocks))) and analyse_inlined_subroutines_sdt_lexical_block sdt_parents (lb:sdt_lexical_block) : inlined_subroutine list= (List.rev_append (List.rev (List.concat (map (analyse_inlined_subroutines_sdt_subroutine sdt_parents) lb.slb_subroutines))) (List.concat (map (analyse_inlined_subroutines_sdt_lexical_block sdt_parents) lb.slb_lexical_blocks))) let analyse_inlined_subroutines_sdt_compilation_unit (cu:sdt_compilation_unit) : inlined_subroutine list= (List.concat (map (analyse_inlined_subroutines_sdt_subroutine []) cu.scu_subroutines)) let analyse_inlined_subroutines_sdt_dwarf (sd: sdt_dwarf) : inlined_subroutine list= (List.concat (map analyse_inlined_subroutines_sdt_compilation_unit sd.sd_compilation_units)) let analyse_inlined_subroutine_by_range (is:inlined_subroutine) : inlined_subroutine_data_by_range= (let n_ranges = (List.length is.is_pc_ranges) in Lem_list.mapi (fun i -> fun (n1,n2) -> ((n1,n2),(Nat_big_num.of_int i, Nat_big_num.of_int n_ranges),is)) is.is_pc_ranges) let is_lt dict_Basic_classes_Eq_b dict_Basic_classes_Ord_a dict_Basic_classes_Ord_b ((n1,n2),(m,n),is) ((n1',n2'),(m',n'),is'):bool= ( dict_Basic_classes_Ord_b.isLess_method n1 n1' || ( dict_Basic_classes_Eq_b.isEqual_method n1 n1' && dict_Basic_classes_Ord_a.isGreater_method n2 n2')) let analyse_inlined_subroutines_by_range (iss:inlined_subroutine_data) : inlined_subroutine_data_by_range= (Lem_sorting.insertSortBy (is_lt instance_Basic_classes_Eq_Num_natural_dict instance_Basic_classes_Ord_Num_natural_dict instance_Basic_classes_Ord_Num_natural_dict) (List.concat (Lem_list.map analyse_inlined_subroutine_by_range iss))) (* pp the inlined_subroutine tree structure. Technically these die offsets each also need the compilation-unit offset to be globally unique, but that's locally constant *) let rec pp_inlined_subroutine_parents (ds: die list) : string= ((match ds with | [] -> "" | die1::ds' -> if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_inlined_subroutine") then pp_pos die1.die_offset ^ (":" ^ pp_inlined_subroutine_parents ds') else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_lexical_block") then "<lexical_block>:" ^ pp_inlined_subroutine_parents ds' else if Nat_big_num.equal die1.die_abbreviation_declaration.ad_tag (tag_encode "DW_TAG_subprogram") then "" else "<surprising ad_tag in " ^ (pp_pos die1.die_offset ^ (": " ^ (pphex die1.die_abbreviation_declaration.ad_tag ^ ">"))) )) let pp_inlined_subroutine_header ds is:string= (is.is_name ^ (" inlined from " ^ ((subprogram_at_line ds.ds_subprogram_line_extents is.is_call_file is.is_call_line) ^ (":" ^ (Nat_big_num.to_string is.is_call_line ^ (" (" ^ ((pp_ufe_brief is.is_call_file) ^ (")" ^ (" " ^ (let (cu,parents,die1) = (is.is_inlined_subroutine) in pp_inlined_subroutine_parents (die1::parents))))))))))) let pp_inlined_subroutine_const_params d is:string= (let c = (p_context_of_d d) in (match is.is_const_params with | [] -> "" | _ -> String.concat "" (Lem_list.map (fun iscp -> let fake_als : analysed_location_data = ([(iscp.iscp_abstract_origin,None)]) in let fake_diff = (pp_analysed_locations3_diff c (*HACK*) d d.d_str [] fake_als) in let const_in_place_of_locs = (Lem_list.map (fun (removed,(name1,offset,kind),typ,locs,parents) -> (removed,(name1,offset,kind),typ,("const="^Nat_big_num.to_string iscp.iscp_value),parents)) fake_diff) in pp_analysed_location_format const_in_place_of_locs ) is.is_const_params) )) let pp_inlined_subroutine ds is:string= (pp_inlined_subroutine_header ds is ^ ("\n" ^ (String.concat "" (Lem_list.map (fun (n1,n2) -> " " ^ (pphex n1 ^ (" " ^ (pphex n2 ^ "\n")))) is.is_pc_ranges) ^ pp_inlined_subroutine_const_params ds.ds_dwarf is))) let pp_inlined_subroutines ds iss:string= (String.concat "" (Lem_list.map (pp_inlined_subroutine ds) iss)) let pp_inlined_subroutine_by_range ds ((n1,n2),((m:Nat_big_num.num),(n:Nat_big_num.num)),is):string= (pphex n1 ^ (" " ^ (pphex n2 ^ (" " ^ ((if not (Nat_big_num.equal n( (Nat_big_num.of_int 1))) then "("^(Nat_big_num.to_string m^(" of "^(Nat_big_num.to_string n^") "))) else "") ^ (pp_inlined_subroutine_header ds is ^("\n" ^ (if Nat_big_num.equal m( (Nat_big_num.of_int 0)) then pp_inlined_subroutine_const_params ds.ds_dwarf is else "")))))))) let pp_inlined_subroutines_by_range ds iss:string= (String.concat "" (Lem_list.map (pp_inlined_subroutine_by_range ds) iss)) (** ************************************************************ *) (** ** pp of text section *) (** ************************************************************ *) (* assume 4-byte ARM instructions *) let rec words_of_byte_sequence (addr:Nat_big_num.num) (bs:byte_sequence0) (acc: (Nat_big_num.num * Nat_big_num.num)list) : (Nat_big_num.num * Nat_big_num.num) list= ((match read_4_bytes_be bs with | Success ((b0,b1,b2,b3), bs') -> let i : Nat_big_num.num = (Nat_big_num.add (Nat_big_num.add (Nat_big_num.add (natural_of_byte b0)(Nat_big_num.mul( (Nat_big_num.of_int 256))(natural_of_byte b1)))(Nat_big_num.mul( (Nat_big_num.of_int 65536))(natural_of_byte b2)))(Nat_big_num.mul(Nat_big_num.mul( (Nat_big_num.of_int 65536))( (Nat_big_num.of_int 256)))(natural_of_byte b3))) in words_of_byte_sequence (Nat_big_num.add addr( (Nat_big_num.of_int 4))) bs' ((addr,i)::acc) | Fail _ -> List.rev acc )) let pp_instruction ((addr:Nat_big_num.num),(i:Nat_big_num.num)):string= (Ml_bindings.hex_string_of_big_int_pad8 addr ^ (" " ^ (Ml_bindings.hex_string_of_big_int_pad8 i ^ "\n"))) (*val pp_text_section : elf_file -> string*) let pp_text_section f:string= (let (p_context1, addr, bs) = (extract_text f) in let instructions : (Nat_big_num.num * Nat_big_num.num) list = (words_of_byte_sequence addr bs []) in String.concat "" (Lem_list.map pp_instruction instructions)) (** ************************************************************ *) (** ** top level for main_elf ******************************** *) (** ************************************************************ *) (*val harness_string_of_elf_like_objdump : elf_file -> byte_sequence -> string*) let harness_string_of_elf_like_objdump f1 bs:string= (let mds = (extract_dwarf_static f1) in (match mds with | None -> "<no dwarf information extracted>" | Some ds -> "" (*pp_text_section f1*) ^ pp_dwarf_like_objdump ds.ds_dwarf )) (*val harness_string_of_elf : elf_file -> byte_sequence -> string*) let harness_string_of_elf f1 bs:string= (let mds = (extract_dwarf_static f1) in (match mds with | None -> "<no dwarf information extracted>" | Some ds -> let sdt_d = (mk_sdt_dwarf ds.ds_dwarf ds.ds_subprogram_line_extents) in "* emacs outline-mode configuration -*-outline-*- C-c C-{t,a,d,e}" ^ ("" (*pp_text_section f1*) ^ (pp_dwarf ds.ds_dwarf (* ^ analyse_locations_raw c d *) ^ ("************** evaluation of frame data *************************\n" ^ (pp_evaluated_frame_info ds.ds_evaluated_frame_info ^ ("************** analysis of location data *************************\n" ^ (pp_analysed_location_data ds.ds_dwarf ds.ds_analysed_location_data ^ ("************** line info *************************\n" ^ (pp_evaluated_line_info ds.ds_evaluated_line_info ^ ("************** inlined subroutine info *************************\n" ^ (let iss = (analyse_inlined_subroutines_sdt_dwarf sdt_d) in pp_inlined_subroutines ds iss ^ ("************** inlined subroutine info by range *************************\n" ^ (pp_inlined_subroutines_by_range ds (analyse_inlined_subroutines_by_range iss) ^ ("************** subprogram line-number extent info *************************\n" ^ (pp_subprograms instance_Show_Show_Num_natural_dict ds.ds_subprogram_line_extents ^ ("************** simple die tree *************************\n" ^ (pp_sdt_dwarf sdt_d ^ ("************** simple die tree globals *************************\n" ^ (pp_sdt_globals_dwarf sdt_d ^ ("************** simple die tree locals *************************\n" ^ pp_sdt_locals_dwarf sdt_d))))))))))))))))))) )) (*val harness_string_of_elf64_debug_info_section : elf64_file -> byte_sequence -> (*(natural -> string) -> (natural -> string) -> (natural -> string) -> elf64_header -> elf64_section_header_table -> string_table -> *) string*) let harness_string_of_elf64_debug_info_section f1 bs0:string= ( (*os proc usr hdr sht stbl*)harness_string_of_elf (ELF_File_64 f1) bs0) (*val harness_string_of_elf32_debug_info_section : elf32_file -> byte_sequence -> (* (natural -> string) -> (natural -> string) -> (natural -> string) -> elf32_header -> elf32_section_header_table -> string_table ->*) string*) let harness_string_of_elf32_debug_info_section f1 bs0:string= ( (*os proc usr hdr sht stbl*)harness_string_of_elf (ELF_File_32 f1) bs0) (*val harness_string_of_elf64_like_objdump : elf64_file -> byte_sequence -> (*(natural -> string) -> (natural -> string) -> (natural -> string) -> elf64_header -> elf64_section_header_table -> string_table -> *) string*) let harness_string_of_elf64_like_objdump f1 bs0:string= ( (*os proc usr hdr sht stbl*)harness_string_of_elf_like_objdump (ELF_File_64 f1) bs0) (*val harness_string_of_elf32_like_objdump : elf32_file -> byte_sequence -> (* (natural -> string) -> (natural -> string) -> (natural -> string) -> elf32_header -> elf32_section_header_table -> string_table ->*) string*) let harness_string_of_elf32_like_objdump f1 bs0:string= ( (*os proc usr hdr sht stbl*)harness_string_of_elf_like_objdump (ELF_File_32 f1) bs0)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>