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
|
; flat assembler core
; Copyright (c) 1999-2021, Tomasz Grysztar.
; All rights reserved.
formatter:
mov [current_offset],edi
cmp [output_file],0
jne output_path_ok
mov esi,[input_file]
mov edi,[free_additional_memory]
duplicate_output_path:
lods byte [esi]
cmp edi,[structures_buffer]
jae out_of_memory
stos byte [edi]
or al,al
jnz duplicate_output_path
dec edi
mov eax,edi
find_extension:
dec eax
cmp eax,[free_additional_memory]
jb extension_found
cmp byte [eax],'\'
je extension_found
cmp byte [eax],'/'
je extension_found
cmp byte [eax],'.'
jne find_extension
mov edi,eax
extension_found:
lea eax,[edi+9]
cmp eax,[structures_buffer]
jae out_of_memory
cmp [file_extension],0
jne extension_specified
mov al,[output_format]
cmp al,2
je exe_extension
jb bin_extension
cmp al,4
je obj_extension
cmp al,5
je o_extension
cmp al,3
jne no_extension
cmp [subsystem],1
je sys_extension
cmp [subsystem],10
jae efi_extension
bt [format_flags],8
jnc exe_extension
mov eax,'.dll'
jmp make_extension
sys_extension:
mov eax,'.sys'
jmp make_extension
efi_extension:
mov eax,'.efi'
jmp make_extension
bin_extension:
mov eax,'.bin'
bt [format_flags],0
jnc make_extension
mov eax,'.com'
jmp make_extension
obj_extension:
mov eax,'.obj'
jmp make_extension
o_extension:
mov eax,'.o'
bt [format_flags],0
jnc make_extension
no_extension:
xor eax,eax
jmp make_extension
exe_extension:
mov eax,'.exe'
make_extension:
xchg eax,[edi]
scas dword [edi]
mov byte [edi],0
scas byte [edi]
mov esi,edi
stos dword [edi]
sub edi,9
xor eax,eax
mov ebx,characters
adapt_case:
mov al,[esi]
or al,al
jz adapt_next
xlat byte [ebx]
cmp al,[esi]
je adapt_ok
sub byte [edi],20h
adapt_ok:
inc esi
adapt_next:
inc edi
cmp byte [edi],0
jne adapt_case
jmp extension_ok
extension_specified:
mov al,'.'
stos byte [edi]
mov esi,[file_extension]
copy_extension:
lods byte [esi]
stos byte [edi]
test al,al
jnz copy_extension
dec edi
extension_ok:
mov esi,edi
lea ecx,[esi+1]
sub ecx,[free_additional_memory]
mov edi,[structures_buffer]
dec edi
std
rep movs byte [edi],[esi]
cld
inc edi
mov [structures_buffer],edi
mov [output_file],edi
output_path_ok:
cmp [symbols_file],0
je labels_table_ok
mov ecx,[memory_end]
sub ecx,[labels_list]
mov edi,[tagged_blocks]
sub edi,8
mov [edi],ecx
or dword [edi+4],-1
sub edi,ecx
cmp edi,[current_offset]
jbe out_of_memory
mov [tagged_blocks],edi
mov esi,[memory_end]
copy_labels:
sub esi,32
cmp esi,[labels_list]
jb labels_table_ok
mov ecx,32 shr 2
rep movs dword [edi],[esi]
sub esi,32
jmp copy_labels
labels_table_ok:
mov edi,[current_offset]
cmp [output_format],4
je coff_formatter
cmp [output_format],5
jne common_formatter
bt [format_flags],0
jnc elf_formatter
common_formatter:
mov eax,edi
sub eax,[code_start]
mov [real_code_size],eax
cmp edi,[undefined_data_end]
jne calculate_code_size
mov edi,[undefined_data_start]
calculate_code_size:
mov [current_offset],edi
sub edi,[code_start]
mov [code_size],edi
and [written_size],0
mov edx,[output_file]
call create
jc write_failed
cmp [output_format],3
jne stub_written
mov edx,[code_start]
mov ecx,[stub_size]
sub edx,ecx
add [written_size],ecx
call write
stub_written:
cmp [output_format],2
jne write_output
call write_mz_header
write_output:
call write_code
output_written:
call close
cmp [symbols_file],0
jne dump_symbols
ret
write_code:
mov eax,[written_size]
mov [headers_size],eax
mov edx,[code_start]
mov ecx,[code_size]
add [written_size],ecx
lea eax,[edx+ecx]
call write
jc write_failed
ret
format_directive:
cmp edi,[code_start]
jne unexpected_instruction
mov ebp,[addressing_space]
test byte [ds:ebp+0Ah],1
jnz unexpected_instruction
cmp [output_format],0
jne unexpected_instruction
lods byte [esi]
cmp al,1Ch
je format_prefix
cmp al,18h
jne invalid_argument
lods byte [esi]
select_format:
mov dl,al
shr al,4
mov [output_format],al
and edx,0Fh
or [format_flags],edx
cmp al,2
je format_mz
cmp al,3
je format_pe
cmp al,4
je format_coff
cmp al,5
je format_elf
format_defined:
cmp byte [esi],86h
jne instruction_assembled
cmp word [esi+1],'('
jne invalid_argument
mov eax,[esi+3]
add esi,3+4
mov [file_extension],esi
lea esi,[esi+eax+1]
jmp instruction_assembled
format_prefix:
lods byte [esi]
mov ah,al
lods byte [esi]
cmp al,18h
jne invalid_argument
lods byte [esi]
mov edx,eax
shr dl,4
shr dh,4
cmp dl,dh
jne invalid_argument
or al,ah
jmp select_format
entry_directive:
bts [format_flags],10h
jc setting_already_specified
mov al,[output_format]
cmp al,2
je mz_entry
cmp al,3
je pe_entry
cmp al,5
jne illegal_instruction
bt [format_flags],0
jc elf_entry
jmp illegal_instruction
stack_directive:
bts [format_flags],11h
jc setting_already_specified
mov al,[output_format]
cmp al,2
je mz_stack
cmp al,3
je pe_stack
jmp illegal_instruction
heap_directive:
bts [format_flags],12h
jc setting_already_specified
mov al,[output_format]
cmp al,2
je mz_heap
cmp al,3
je pe_heap
jmp illegal_instruction
segment_directive:
mov al,[output_format]
cmp al,2
je mz_segment
cmp al,5
je elf_segment
jmp illegal_instruction
section_directive:
mov al,[output_format]
cmp al,3
je pe_section
cmp al,4
je coff_section
cmp al,5
je elf_section
jmp illegal_instruction
public_directive:
mov al,[output_format]
cmp al,4
je public_allowed
cmp al,5
jne illegal_instruction
bt [format_flags],0
jc illegal_instruction
public_allowed:
mov [base_code],0C0h
lods byte [esi]
cmp al,2
je public_label
cmp al,1Dh
jne invalid_argument
lods byte [esi]
and al,7
add [base_code],al
lods byte [esi]
cmp al,2
jne invalid_argument
public_label:
lods dword [esi]
cmp eax,0Fh
jb invalid_use_of_symbol
je reserved_word_used_as_symbol
inc esi
mov dx,[current_pass]
mov [eax+18],dx
or byte [eax+8],8
mov ebx,eax
call store_label_reference
mov eax,ebx
mov ebx,[free_additional_memory]
lea edx,[ebx+10h]
cmp edx,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],edx
mov [ebx+8],eax
mov eax,[current_line]
mov [ebx+0Ch],eax
lods byte [esi]
cmp al,86h
jne invalid_argument
lods word [esi]
cmp ax,'('
jne invalid_argument
mov [ebx+4],esi
lods dword [esi]
lea esi,[esi+eax+1]
mov al,[base_code]
mov [ebx],al
jmp instruction_assembled
extrn_directive:
mov al,[output_format]
cmp al,4
je extrn_allowed
cmp al,5
jne illegal_instruction
bt [format_flags],0
jc illegal_instruction
extrn_allowed:
lods word [esi]
cmp ax,'('
jne invalid_argument
mov ebx,esi
lods dword [esi]
lea esi,[esi+eax+1]
mov edx,[free_additional_memory]
lea eax,[edx+0Ch]
cmp eax,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],eax
mov byte [edx],80h
mov [edx+4],ebx
lods byte [esi]
cmp al,86h
jne invalid_argument
lods byte [esi]
cmp al,2
jne invalid_argument
lods dword [esi]
cmp eax,0Fh
jb invalid_use_of_symbol
je reserved_word_used_as_symbol
inc esi
mov ebx,eax
xor ah,ah
lods byte [esi]
cmp al,':'
je get_extrn_size
dec esi
cmp al,11h
jne extrn_size_ok
get_extrn_size:
lods word [esi]
cmp al,11h
jne invalid_argument
extrn_size_ok:
mov [address_symbol],edx
mov [label_size],ah
movzx ecx,ah
mov [edx+8],ecx
xor eax,eax
xor edx,edx
xor ebp,ebp
mov [address_sign],0
mov ch,2
test [format_flags],8
jz make_free_label
mov ch,4
jmp make_free_label
mark_relocation:
cmp [value_type],0
je relocation_ok
mov ebp,[addressing_space]
test byte [ds:ebp+0Ah],1
jnz relocation_ok
cmp [output_format],2
je mark_mz_relocation
cmp [output_format],3
je mark_pe_relocation
cmp [output_format],4
je mark_coff_relocation
cmp [output_format],5
je mark_elf_relocation
relocation_ok:
ret
close_pass:
mov al,[output_format]
cmp al,3
je close_pe
cmp al,4
je close_coff
cmp al,5
je close_elf
ret
format_mz:
mov edx,[additional_memory]
push edi
mov edi,edx
mov ecx,1Ch shr 2
xor eax,eax
rep stos dword [edi]
mov [free_additional_memory],edi
pop edi
mov word [edx+0Ch],0FFFFh
mov word [edx+10h],1000h
mov [code_type],16
jmp format_defined
mark_mz_relocation:
push eax ebx
inc word [number_of_relocations]
jz format_limitations_exceeded
mov ebx,[free_additional_memory]
mov eax,edi
sub eax,[code_start]
mov [ebx],ax
shr eax,16
shl ax,12
mov [ebx+2],ax
cmp word [ebx],0FFFFh
jne mz_relocation_ok
inc word [ebx+2]
sub word [ebx],10h
mz_relocation_ok:
add ebx,4
cmp ebx,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],ebx
pop ebx eax
ret
mz_segment:
lods byte [esi]
cmp al,2
jne invalid_argument
lods dword [esi]
cmp eax,0Fh
jb invalid_use_of_symbol
je reserved_word_used_as_symbol
inc esi
mov ebx,eax
mov eax,edi
sub eax,[code_start]
mov ecx,0Fh
add eax,0Fh
and eax,1111b
sub ecx,eax
mov edx,edi
xor eax,eax
rep stos byte [edi]
mov eax,edx
call undefined_data
push ebx
call create_addressing_space
pop ebx
mov eax,edi
sub eax,[code_start]
shr eax,4
cmp eax,10000h
jae value_out_of_range
mov edx,eax
mov al,16
cmp byte [esi],13h
jne segment_type_ok
inc esi
lods byte [esi]
segment_type_ok:
mov [code_type],al
mov eax,edx
mov ch,1
mov [address_sign],0
xor edx,edx
xor ebp,ebp
mov [label_size],0
mov [address_symbol],edx
jmp make_free_label
mz_entry:
lods byte [esi]
cmp al,'('
jne invalid_argument
call get_word_value
cmp [value_type],1
je initial_cs_ok
call recoverable_invalid_address
initial_cs_ok:
mov edx,[additional_memory]
mov [edx+16h],ax
lods byte [esi]
cmp al,':'
jne invalid_argument
lods byte [esi]
cmp al,'('
jne invalid_argument
ja invalid_address
call get_word_value
cmp [value_type],0
jne invalid_use_of_symbol
mov edx,[additional_memory]
mov [edx+14h],ax
jmp instruction_assembled
recoverable_invalid_address:
cmp [error_line],0
jne ignore_invalid_address
push [current_line]
pop [error_line]
mov [error],invalid_address
ignore_invalid_address:
ret
mz_stack:
lods byte [esi]
cmp al,'('
jne invalid_argument
call get_word_value
cmp byte [esi],':'
je stack_pointer
cmp ax,10h
jb invalid_value
cmp [value_type],0
jne invalid_use_of_symbol
mov edx,[additional_memory]
mov [edx+10h],ax
jmp instruction_assembled
stack_pointer:
cmp [value_type],1
je initial_ss_ok
call recoverable_invalid_address
initial_ss_ok:
mov edx,[additional_memory]
mov [edx+0Eh],ax
lods byte [esi]
cmp al,':'
jne invalid_argument
lods byte [esi]
cmp al,'('
jne invalid_argument
call get_word_value
cmp [value_type],0
jne invalid_use_of_symbol
mov edx,[additional_memory]
mov [edx+10h],ax
bts [format_flags],4
jmp instruction_assembled
mz_heap:
cmp [output_format],2
jne illegal_instruction
lods byte [esi]
call get_size_operator
cmp ah,1
je invalid_value
cmp ah,2
ja invalid_value
cmp al,'('
jne invalid_argument
call get_word_value
cmp [value_type],0
jne invalid_use_of_symbol
mov edx,[additional_memory]
mov [edx+0Ch],ax
jmp instruction_assembled
write_mz_header:
mov edx,[additional_memory]
bt [format_flags],4
jc mz_stack_ok
mov eax,[real_code_size]
dec eax
shr eax,4
inc eax
mov [edx+0Eh],ax
shl eax,4
movzx ecx,word [edx+10h]
add eax,ecx
mov [real_code_size],eax
mz_stack_ok:
mov edi,[free_additional_memory]
mov eax,[number_of_relocations]
shl eax,2
add eax,1Ch
sub edi,eax
xchg edi,[free_additional_memory]
mov ecx,0Fh
add eax,0Fh
and eax,1111b
sub ecx,eax
xor al,al
rep stos byte [edi]
sub edi,[free_additional_memory]
mov ecx,edi
shr edi,4
mov word [edx],'MZ' ; signature
mov [edx+8],di ; header size in paragraphs
mov eax,[number_of_relocations]
mov [edx+6],ax ; number of relocation entries
mov eax,[code_size]
add eax,ecx
mov esi,eax
shr esi,9
and eax,1FFh
inc si
or ax,ax
jnz mz_size_ok
dec si
mz_size_ok:
mov [edx+2],ax ; number of bytes in last page
mov [edx+4],si ; number of pages
mov eax,[real_code_size]
dec eax
shr eax,4
inc eax
mov esi,[code_size]
dec esi
shr esi,4
inc esi
sub eax,esi
mov [edx+0Ah],ax ; minimum memory in addition to code
add [edx+0Ch],ax ; maximum memory in addition to code
salc
mov ah,al
or [edx+0Ch],ax
mov word [edx+18h],1Ch ; offset of relocation table
add [written_size],ecx
call write
jc write_failed
ret
make_stub:
mov [stub_file],edx
or edx,edx
jnz stub_from_file
push esi
mov edx,edi
xor eax,eax
mov ecx,20h
rep stos dword [edi]
mov eax,40h+default_stub_end-default_stub
mov cx,100h+default_stub_end-default_stub
mov word [edx],'MZ'
mov byte [edx+4],1
mov word [edx+2],ax
mov byte [edx+8],4
mov byte [edx+0Ah],10h
mov word [edx+0Ch],0FFFFh
mov word [edx+10h],cx
mov word [edx+3Ch],ax
mov byte [edx+18h],40h
lea edi,[edx+40h]
mov esi,default_stub
mov ecx,default_stub_end-default_stub
rep movs byte [edi],[esi]
pop esi
jmp stub_ok
default_stub:
use16
push cs
pop ds
mov dx,stub_message-default_stub
mov ah,9
int 21h
mov ax,4C01h
int 21h
stub_message db 'This program cannot be run in DOS mode.',0Dh,0Ah,24h
rq 1
default_stub_end:
use32
stub_from_file:
push esi
mov esi,edx
call open_binary_file
mov edx,edi
mov ecx,1Ch
mov esi,edx
call read
jc binary_stub
cmp word [esi],'MZ'
jne binary_stub
add edi,1Ch
movzx ecx,word [esi+6]
add ecx,11b
and ecx,not 11b
add ecx,(40h-1Ch) shr 2
lea eax,[edi+ecx*4]
cmp edi,[tagged_blocks]
jae out_of_memory
xor eax,eax
rep stos dword [edi]
mov edx,40h
xchg dx,[esi+18h]
xor al,al
call lseek
movzx ecx,word [esi+6]
shl ecx,2
lea edx,[esi+40h]
call read
mov edx,edi
sub edx,esi
shr edx,4
xchg dx,[esi+8]
shl edx,4
xor al,al
call lseek
movzx ecx,word [esi+4]
dec ecx
shl ecx,9
movzx edx,word [esi+2]
test edx,edx
jnz stub_header_size_ok
mov dx,200h
stub_header_size_ok:
add ecx,edx
mov edx,edi
sub ecx,eax
je read_stub_code
jb stub_code_ok
push ecx
dec ecx
shr ecx,3
inc ecx
shl ecx,1
lea eax,[edi+ecx*4]
cmp eax,[tagged_blocks]
jae out_of_memory
xor eax,eax
rep stos dword [edi]
pop ecx
read_stub_code:
call read
stub_code_ok:
call close
mov edx,edi
sub edx,esi
mov ax,dx
and ax,1FFh
mov [esi+2],ax
dec edx
shr edx,9
inc edx
mov [esi+4],dx
mov eax,edi
sub eax,esi
mov [esi+3Ch],eax
pop esi
stub_ok:
ret
binary_stub:
mov esi,edi
mov ecx,40h shr 2
xor eax,eax
rep stos dword [edi]
mov al,2
xor edx,edx
call lseek
push eax
xor al,al
xor edx,edx
call lseek
mov ecx,[esp]
add ecx,40h+111b
and ecx,not 111b
mov ax,cx
and ax,1FFh
mov [esi+2],ax
lea eax,[ecx+1FFh]
shr eax,9
mov [esi+4],ax
mov [esi+3Ch],ecx
sub ecx,40h
mov eax,10000h
sub eax,ecx
jbe binary_heap_ok
shr eax,4
mov [esi+0Ah],ax
binary_heap_ok:
mov word [esi],'MZ'
mov byte [esi+8],4
mov ax,0FFFFh
mov [esi+0Ch],ax
dec ax
mov [esi+10h],ax
sub ax,0Eh
mov [esi+0Eh],ax
mov [esi+16h],ax
mov word [esi+14h],100h
mov byte [esi+18h],40h
mov eax,[tagged_blocks]
sub eax,ecx
cmp edi,eax
jae out_of_memory
mov edx,edi
shr ecx,2
xor eax,eax
rep stos dword [edi]
pop ecx
call read
call close
pop esi
ret
format_pe:
xor edx,edx
mov [machine],14Ch
mov [subsystem],3
mov [subsystem_version],3 + 10 shl 16
mov [image_base],400000h
and [image_base_high],0
test [format_flags],8
jz pe_settings
mov [machine],8664h
mov [subsystem_version],5 + 0 shl 16
pe_settings:
cmp byte [esi],84h
je get_stub_name
cmp byte [esi],80h
je get_pe_base
cmp byte [esi],1Bh
jne pe_settings_ok
lods byte [esi]
lods byte [esi]
test al,80h+40h
jz subsystem_setting
cmp al,80h
je dll_flag
cmp al,81h
je wdm_flag
cmp al,82h
je large_flag
cmp al,83h
je nx_flag
jmp pe_settings
dll_flag:
bts [format_flags],8
jc setting_already_specified
jmp pe_settings
wdm_flag:
bts [format_flags],9
jc setting_already_specified
jmp pe_settings
large_flag:
bts [format_flags],11
jc setting_already_specified
test [format_flags],8
jnz invalid_argument
jmp pe_settings
nx_flag:
bts [format_flags],12
jc setting_already_specified
jmp pe_settings
subsystem_setting:
bts [format_flags],7
jc setting_already_specified
and ax,3Fh
mov [subsystem],ax
cmp ax,10
jb subsystem_type_ok
or [format_flags],4
subsystem_type_ok:
cmp byte [esi],'('
jne pe_settings
inc esi
cmp byte [esi],'.'
jne invalid_value
inc esi
push edx
cmp byte [esi+11],0
jne invalid_value
cmp byte [esi+10],2
ja invalid_value
mov dx,[esi+8]
cmp dx,8000h
je zero_version
mov eax,[esi+4]
cmp dx,7
jg invalid_value
mov cx,7
sub cx,dx
mov eax,[esi+4]
shr eax,cl
mov ebx,eax
shr ebx,24
cmp bl,100
jae invalid_value
and eax,0FFFFFFh
mov ecx,100
mul ecx
shrd eax,edx,24
jnc version_value_ok
inc eax
version_value_ok:
shl eax,16
mov ax,bx
jmp subsystem_version_ok
zero_version:
xor eax,eax
subsystem_version_ok:
pop edx
add esi,13
mov [subsystem_version],eax
jmp pe_settings
get_pe_base:
bts [format_flags],10
jc setting_already_specified
lods word [esi]
cmp ah,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
push edx edi
add edi,[stub_size]
test [format_flags],4
jnz get_peplus_base
call get_dword_value
mov [image_base],eax
jmp pe_base_ok
get_peplus_base:
call get_qword_value
mov [image_base],eax
mov [image_base_high],edx
pe_base_ok:
pop edi edx
cmp [value_type],0
jne invalid_use_of_symbol
cmp byte [esi],84h
jne pe_settings_ok
get_stub_name:
lods byte [esi]
lods word [esi]
cmp ax,'('
jne invalid_argument
lods dword [esi]
mov edx,esi
add esi,eax
inc esi
pe_settings_ok:
mov ebp,[stub_size]
or ebp,ebp
jz make_pe_stub
cmp edx,[stub_file]
je pe_stub_ok
sub edi,[stub_size]
mov [code_start],edi
make_pe_stub:
call make_stub
mov eax,edi
sub eax,[code_start]
mov [stub_size],eax
mov [code_start],edi
mov ebp,eax
pe_stub_ok:
mov edx,edi
mov ecx,18h+0E0h
test [format_flags],4
jz zero_pe_header
add ecx,10h
zero_pe_header:
add ebp,ecx
shr ecx,2
xor eax,eax
rep stos dword [edi]
mov word [edx],'PE' ; signature
mov ax,[machine]
mov word [edx+4],ax
mov byte [edx+38h+1],10h ; section alignment
mov byte [edx+3Ch+1],2 ; file alignment
mov byte [edx+40h],1 ; OS version
mov eax,[subsystem_version]
mov [edx+48h],eax
mov ax,[subsystem]
mov [edx+5Ch],ax
cmp ax,1
jne pe_alignment_ok
mov eax,20h
mov dword [edx+38h],eax
mov dword [edx+3Ch],eax
pe_alignment_ok:
mov word [edx+1Ah],VERSION_MAJOR + VERSION_MINOR shl 8
test [format_flags],4
jnz init_peplus_specific
mov byte [edx+14h],0E0h ; size of optional header
mov dword [edx+16h],10B010Fh; flags and magic value
mov eax,[image_base]
mov [edx+34h],eax
mov byte [edx+60h+1],10h ; stack reserve
mov byte [edx+64h+1],10h ; stack commit
mov byte [edx+68h+2],1 ; heap reserve
mov byte [edx+74h],16 ; number of directories
jmp pe_header_ok
init_peplus_specific:
mov byte [edx+14h],0F0h ; size of optional header
mov dword [edx+16h],20B002Fh; flags and magic value
mov eax,[image_base]
mov [edx+30h],eax
mov eax,[image_base_high]
mov [edx+34h],eax
mov byte [edx+60h+1],10h ; stack reserve
mov byte [edx+68h+1],10h ; stack commit
mov byte [edx+70h+2],1 ; heap reserve
mov byte [edx+84h],16 ; number of directories
pe_header_ok:
bsf ecx,[edx+3Ch]
imul ebx,[number_of_sections],28h
or ebx,ebx
jnz reserve_space_for_section_headers
mov ebx,28h
reserve_space_for_section_headers:
add ebx,ebp
dec ebx
shr ebx,cl
inc ebx
shl ebx,cl
sub ebx,ebp
mov ecx,ebx
mov eax,[tagged_blocks]
sub eax,ecx
cmp edi,eax
jae out_of_memory
shr ecx,2
xor eax,eax
rep stos dword [edi]
mov eax,edi
sub eax,[code_start]
add eax,[stub_size]
mov [edx+54h],eax ; size of headers
mov ecx,[edx+38h]
dec ecx
add eax,ecx
not ecx
and eax,ecx
bt [format_flags],8
jc pe_entry_init_ok
mov [edx+28h],eax ; entry point rva
pe_entry_init_ok:
and [number_of_sections],0
movzx ebx,word [edx+14h]
lea ebx,[edx+18h+ebx]
mov [current_section],ebx
mov dword [ebx],'.fla'
mov dword [ebx+4],'t'
mov [ebx+14h],edi
mov [ebx+0Ch],eax
mov dword [ebx+24h],0E0000060h
xor ecx,ecx
xor bl,bl
not eax
not ecx
not bl
add eax,1
adc ecx,0
adc bl,0
add eax,edi
adc ecx,0
adc bl,0
test [format_flags],4
jnz peplus_org
sub eax,[edx+34h]
sbb ecx,0
sbb bl,0
jmp pe_org_ok
peplus_org:
sub eax,[edx+30h]
sbb ecx,[edx+34h]
sbb bl,0
pe_org_ok:
test [format_flags],8
jnz pe64_code
mov bh,2
mov [code_type],32
jmp pe_code_type_ok
pe64_code:
mov bh,4
mov [code_type],64
pe_code_type_ok:
bt [resolver_flags],0
jc pe_labels_type_ok
xor bh,bh
pe_labels_type_ok:
push eax ebx
call init_addressing_space
mov ebp,ebx
pop ebx eax
mov [ds:ebp],eax
mov [ds:ebp+4],ecx
mov [ds:ebp+8],bx
mov [ds:ebp+18h],edi
bt [format_flags],8
jnc dll_flag_ok
or byte [edx+16h+1],20h
dll_flag_ok:
bt [format_flags],9
jnc wdm_flag_ok
or byte [edx+5Eh+1],20h
wdm_flag_ok:
bt [format_flags],11
jnc large_flag_ok
or byte [edx+16h],20h
large_flag_ok:
bt [format_flags],12
jnc nx_ok
or byte [edx+5Eh+1],1
nx_ok:
jmp format_defined
pe_section:
call close_pe_section
push eax ebx
call create_addressing_space
mov ebp,ebx
pop ebx eax
bts [format_flags],5
lea ecx,[ebx+28h]
add edx,[edx+54h]
sub edx,[stub_size]
cmp ecx,edx
jbe new_section
lea ebx,[edx-28h]
or [next_pass_needed],-1
push edi
mov edi,ebx
mov ecx,28h shr 4
xor eax,eax
rep stos dword [edi]
pop edi
new_section:
mov [ebx+0Ch],eax
lods word [esi]
cmp ax,'('
jne invalid_argument
lea edx,[esi+4]
mov ecx,[esi]
lea esi,[esi+4+ecx+1]
cmp ecx,8
ja name_too_long
xor eax,eax
mov [ebx],eax
mov [ebx+4],eax
push esi edi
mov edi,ebx
mov esi,edx
rep movs byte [edi],[esi]
pop edi esi
and dword [ebx+24h],0
mov [ebx+14h],edi
mov edx,[code_start]
mov eax,edi
xor ecx,ecx
sub eax,[ebx+0Ch]
sbb ecx,0
sbb byte [ds:ebp+8],0
mov byte [ds:ebp+9],2
mov [code_type],32
test [format_flags],8
jz pe_section_code_type_ok
mov byte [ds:ebp+9],4
mov [code_type],64
pe_section_code_type_ok:
test [format_flags],4
jnz peplus_section_org
sub eax,[edx+34h]
sbb ecx,0
sbb byte [ds:ebp+8],0
bt [resolver_flags],0
jc pe_section_org_ok
mov byte [ds:ebp+9],0
jmp pe_section_org_ok
peplus_section_org:
sub eax,[edx+30h]
sbb ecx,[edx+34h]
sbb byte [ds:ebp+8],0
bt [resolver_flags],0
jc pe_section_org_ok
mov byte [ds:ebp+9],0
pe_section_org_ok:
mov [ds:ebp],eax
mov [ds:ebp+4],ecx
mov [ds:ebp+18h],edi
get_section_flags:
lods byte [esi]
cmp al,1Ah
je set_directory
cmp al,19h
je section_flag
dec esi
jmp instruction_assembled
set_directory:
movzx eax,byte [esi]
inc esi
mov ecx,ebx
test [format_flags],4
jnz peplus_directory
xchg ecx,[edx+78h+eax*8]
mov dword [edx+78h+eax*8+4],-1
jmp pe_directory_set
peplus_directory:
xchg ecx,[edx+88h+eax*8]
mov dword [edx+88h+eax*8+4],-1
pe_directory_set:
or ecx,ecx
jnz data_already_defined
push ebx edx
call generate_pe_data
pop edx ebx
jmp get_section_flags
section_flag:
lods byte [esi]
cmp al,9
je invalid_argument
cmp al,11
je invalid_argument
mov cl,al
mov eax,1
shl eax,cl
test dword [ebx+24h],eax
jnz setting_already_specified
or dword [ebx+24h],eax
jmp get_section_flags
close_pe_section:
mov ebx,[current_section]
mov edx,[code_start]
mov eax,edi
sub eax,[ebx+14h]
jnz finish_section
bt [format_flags],5
jc finish_section
mov eax,[ebx+0Ch]
ret
finish_section:
mov [ebx+8],eax
cmp edi,[undefined_data_end]
jne align_section
cmp dword [edx+38h],1000h
jb align_section
mov edi,[undefined_data_start]
align_section:
and [undefined_data_end],0
mov ebp,edi
sub ebp,[ebx+14h]
mov ecx,[edx+3Ch]
dec ecx
lea eax,[ebp+ecx]
not ecx
and eax,ecx
mov [ebx+10h],eax
sub eax,ebp
mov ecx,eax
xor al,al
rep stos byte [edi]
mov eax,[code_start]
sub eax,[stub_size]
sub [ebx+14h],eax
mov ecx,[ebx+10h]
test byte [ebx+24h],20h
jz pe_code_sum_ok
add [edx+1Ch],ecx
cmp dword [edx+2Ch],0
jne pe_code_sum_ok
mov eax,[ebx+0Ch]
mov [edx+2Ch],eax
pe_code_sum_ok:
test byte [ebx+24h],40h
jz pe_data_sum_ok
add [edx+20h],ecx
test [format_flags],4
jnz pe_data_sum_ok
cmp dword [edx+30h],0
jne pe_data_sum_ok
mov eax,[ebx+0Ch]
mov [edx+30h],eax
pe_data_sum_ok:
mov eax,[ebx+8]
or eax,eax
jz udata_ok
cmp dword [ebx+10h],0
jne udata_ok
or byte [ebx+24h],80h
add [edx+24h],ecx
udata_ok:
mov ecx,[edx+38h]
dec ecx
add eax,ecx
not ecx
and eax,ecx
add eax,[ebx+0Ch]
add ebx,28h
mov [current_section],ebx
inc word [number_of_sections]
jz format_limitations_exceeded
ret
data_directive:
cmp [output_format],3
jne illegal_instruction
lods byte [esi]
cmp al,1Ah
je predefined_data_type
cmp al,'('
jne invalid_argument
call get_byte_value
cmp al,16
jb data_type_ok
jmp invalid_value
predefined_data_type:
movzx eax,byte [esi]
inc esi
data_type_ok:
mov ebx,[current_section]
mov ecx,edi
sub ecx,[ebx+14h]
add ecx,[ebx+0Ch]
mov edx,[code_start]
test [format_flags],4
jnz peplus_data
xchg ecx,[edx+78h+eax*8]
jmp init_pe_data
peplus_data:
xchg ecx,[edx+88h+eax*8]
init_pe_data:
or ecx,ecx
jnz data_already_defined
call allocate_structure_data
mov word [ebx],data_directive-instruction_handler
mov [ebx+2],al
mov edx,[current_line]
mov [ebx+4],edx
call generate_pe_data
jmp instruction_assembled
end_data:
cmp [output_format],3
jne illegal_instruction
call find_structure_data
jc unexpected_instruction
movzx eax,byte [ebx+2]
mov edx,[current_section]
mov ecx,edi
sub ecx,[edx+14h]
add ecx,[edx+0Ch]
mov edx,[code_start]
test [format_flags],4
jnz end_peplus_data
sub ecx,[edx+78h+eax*8]
mov [edx+78h+eax*8+4],ecx
jmp remove_structure_data
end_peplus_data:
sub ecx,[edx+88h+eax*8]
mov [edx+88h+eax*8+4],ecx
jmp remove_structure_data
pe_entry:
lods byte [esi]
cmp al,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
test [format_flags],8
jnz pe64_entry
call get_dword_value
mov bl,2
bt [resolver_flags],0
jc check_pe_entry_label_type
xor bl,bl
check_pe_entry_label_type:
cmp [value_type],bl
je pe_entry_ok
call recoverable_invalid_address
pe_entry_ok:
cdq
test [format_flags],4
jnz pe64_entry_type_ok
mov edx,[code_start]
sub eax,[edx+34h]
mov [edx+28h],eax
jmp instruction_assembled
pe64_entry:
call get_qword_value
mov bl,4
bt [resolver_flags],0
jc check_pe64_entry_label_type
xor bl,bl
check_pe64_entry_label_type:
cmp [value_type],bl
je pe64_entry_type_ok
call recoverable_invalid_address
pe64_entry_type_ok:
mov ecx,[code_start]
sub eax,[ecx+30h]
sbb edx,[ecx+34h]
jz pe64_entry_range_ok
call recoverable_overflow
pe64_entry_range_ok:
mov [ecx+28h],eax
jmp instruction_assembled
pe_stack:
lods byte [esi]
cmp al,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
test [format_flags],4
jnz peplus_stack
call get_count_value
mov edx,[code_start]
mov [edx+60h],eax
cmp byte [esi],','
jne default_stack_commit
lods byte [esi]
lods byte [esi]
cmp al,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
call get_count_value
mov edx,[code_start]
mov [edx+64h],eax
cmp eax,[edx+60h]
ja value_out_of_range
jmp instruction_assembled
default_stack_commit:
mov dword [edx+64h],1000h
mov eax,[edx+60h]
cmp eax,1000h
ja instruction_assembled
mov dword [edx+64h],eax
jmp instruction_assembled
peplus_stack:
call get_qword_value
cmp [value_type],0
jne invalid_use_of_symbol
mov ecx,[code_start]
mov [ecx+60h],eax
mov [ecx+64h],edx
cmp byte [esi],','
jne default_peplus_stack_commit
lods byte [esi]
lods byte [esi]
cmp al,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
call get_qword_value
cmp [value_type],0
jne invalid_use_of_symbol
mov ecx,[code_start]
mov [ecx+68h],eax
mov [ecx+6Ch],edx
cmp edx,[ecx+64h]
ja value_out_of_range
jb instruction_assembled
cmp eax,[ecx+60h]
ja value_out_of_range
jmp instruction_assembled
default_peplus_stack_commit:
mov dword [ecx+68h],1000h
cmp dword [ecx+64h],0
jne instruction_assembled
mov eax,[ecx+60h]
cmp eax,1000h
ja instruction_assembled
mov dword [ecx+68h],eax
jmp instruction_assembled
pe_heap:
lods byte [esi]
cmp al,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
test [format_flags],4
jnz peplus_heap
call get_count_value
mov edx,[code_start]
mov [edx+68h],eax
cmp byte [esi],','
jne instruction_assembled
lods byte [esi]
lods byte [esi]
cmp al,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
call get_count_value
mov edx,[code_start]
mov [edx+6Ch],eax
cmp eax,[edx+68h]
ja value_out_of_range
jmp instruction_assembled
peplus_heap:
call get_qword_value
cmp [value_type],0
jne invalid_use_of_symbol
mov ecx,[code_start]
mov [ecx+70h],eax
mov [ecx+74h],edx
cmp byte [esi],','
jne instruction_assembled
lods byte [esi]
lods byte [esi]
cmp al,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
call get_qword_value
cmp [value_type],0
jne invalid_use_of_symbol
mov ecx,[code_start]
mov [ecx+78h],eax
mov [ecx+7Ch],edx
cmp edx,[ecx+74h]
ja value_out_of_range
jb instruction_assembled
cmp eax,[ecx+70h]
ja value_out_of_range
jmp instruction_assembled
mark_pe_relocation:
push eax ebx
test [format_flags],4
jz check_standard_pe_relocation_type
cmp [value_type],4
je pe_relocation_type_ok
check_standard_pe_relocation_type:
cmp [value_type],2
je pe_relocation_type_ok
call recoverable_misuse
pe_relocation_type_ok:
mov ebx,[current_section]
mov eax,edi
sub eax,[ebx+14h]
add eax,[ebx+0Ch]
mov ebx,[free_additional_memory]
inc [number_of_relocations]
add ebx,5
cmp ebx,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],ebx
mov [ebx-5],eax
cmp [value_type],2
je fixup_32bit
mov byte [ebx-1],0Ah
jmp fixup_ok
fixup_32bit:
mov byte [ebx-1],3
fixup_ok:
pop ebx eax
ret
generate_pe_data:
cmp al,2
je make_pe_resource
cmp al,5
je make_pe_fixups
ret
make_pe_fixups:
mov edx,[code_start]
and byte [edx+16h],not 1
or byte [edx+5Eh],40h
bts [resolver_flags],0
jc fixups_ready
or [next_pass_needed],-1
fixups_ready:
and [last_fixup_base],0
call make_fixups
xchg eax,[actual_fixups_size]
sub eax,[actual_fixups_size]
ja reserve_forward_fixups
xor eax,eax
reserve_forward_fixups:
mov [reserved_fixups],edi
add edi,eax
mov [reserved_fixups_size],eax
ret
make_fixups:
push esi
xor ecx,ecx
xchg ecx,[number_of_relocations]
mov esi,[free_additional_memory]
lea eax,[ecx*5]
sub esi,eax
mov [free_additional_memory],esi
mov edx,[last_fixup_base]
mov ebx,[last_fixup_header]
mov ebp,edi
jecxz fixups_done
make_fixup:
cmp [esi],edx
jb store_fixup
mov eax,edi
sub eax,ebp
test eax,11b
jz fixups_block
xor ax,ax
stos word [edi]
add dword [ebx],2
fixups_block:
mov eax,edx
add edx,1000h
cmp [esi],edx
jae fixups_block
stos dword [edi]
mov ebx,edi
mov eax,8
stos dword [edi]
store_fixup:
add dword [ebx],2
mov ah,[esi+1]
and ah,0Fh
mov al,[esi+4]
shl al,4
or ah,al
mov al,[esi]
stos word [edi]
add esi,5
loop make_fixup
fixups_done:
mov [last_fixup_base],edx
mov [last_fixup_header],ebx
pop esi
mov eax,edi
sub eax,ebp
ret
make_pe_resource:
cmp byte [esi],82h
jne resource_done
inc esi
lods word [esi]
cmp ax,'('
jne invalid_argument
lods dword [esi]
mov edx,esi
lea esi,[esi+eax+1]
cmp [next_pass_needed],0
je resource_from_file
cmp [current_pass],0
jne reserve_space_for_resource
and [resource_size],0
reserve_space_for_resource:
add edi,[resource_size]
cmp edi,[tagged_blocks]
ja out_of_memory
jmp resource_done
resource_from_file:
push esi
mov esi,edx
call open_binary_file
push ebx
mov esi,[free_additional_memory]
lea eax,[esi+20h]
cmp eax,[structures_buffer]
ja out_of_memory
mov edx,esi
mov ecx,20h
call read
jc invalid_file_format
xor eax,eax
cmp [esi],eax
jne invalid_file_format
mov ax,0FFFFh
cmp [esi+8],eax
jne invalid_file_format
cmp [esi+12],eax
jne invalid_file_format
mov eax,20h
cmp [esi+4],eax
jne invalid_file_format
read_resource_headers:
test eax,11b
jz resource_file_alignment_ok
mov edx,4
and eax,11b
sub edx,eax
mov al,1
call lseek
jc resource_headers_ok
resource_file_alignment_ok:
mov [esi],eax
lea edx,[esi+12]
mov ecx,8
call read
jc resource_headers_ok
mov ecx,[esi+16]
add [esi],ecx
lea edx,[esi+20]
sub ecx,8
mov [esi+16],ecx
lea eax,[edx+ecx]
cmp eax,[structures_buffer]
ja out_of_memory
call read
jc invalid_file_format
mov edx,[esi]
add edx,[esi+12]
mov eax,[esi+16]
lea ecx,[esi+20]
lea esi,[ecx+eax]
add ecx,2
cmp word [ecx-2],0FFFFh
je resource_header_type_ok
check_resource_header_type:
cmp ecx,esi
jae invalid_file_format
cmp word [ecx],0
je resource_header_type_ok
add ecx,2
jmp check_resource_header_type
resource_header_type_ok:
add ecx,2
cmp word [ecx],0FFFFh
je resource_header_name_ok
check_resource_header_name:
cmp ecx,esi
jae invalid_file_format
cmp word [ecx],0
je resource_header_name_ok
add ecx,2
jmp check_resource_header_name
resource_header_name_ok:
xor al,al
call lseek
jnc read_resource_headers
resource_headers_ok:
cmp esi,[free_additional_memory]
je invalid_file_format
xor eax,eax
mov [esi],eax
mov [resource_data],edi
lea eax,[edi+16]
cmp eax,[tagged_blocks]
jae out_of_memory
xor eax,eax
stos dword [edi]
call make_timestamp
stos dword [edi]
xor eax,eax
stos dword [edi]
stos dword [edi]
xor ebx,ebx
make_type_name_directory:
mov esi,[free_additional_memory]
xor edx,edx
find_type_name:
cmp dword [esi],0
je type_name_ok
add esi,20
cmp word [esi],0FFFFh
je check_next_type_name
or ebx,ebx
jz check_this_type_name
xor ecx,ecx
compare_with_previous_type_name:
mov ax,[esi+ecx]
cmp ax,[ebx+ecx]
ja check_this_type_name
jb check_next_type_name
add ecx,2
mov ax,[esi+ecx]
or ax,[ebx+ecx]
jnz compare_with_previous_type_name
jmp check_next_type_name
check_this_type_name:
or edx,edx
jz type_name_found
xor ecx,ecx
compare_with_current_type_name:
mov ax,[esi+ecx]
cmp ax,[edx+ecx]
ja check_next_type_name
jb type_name_found
add ecx,2
mov ax,[esi+ecx]
or ax,[edx+ecx]
jnz compare_with_current_type_name
jmp same_type_name
type_name_found:
mov edx,esi
same_type_name:
mov [esi-16],edi
check_next_type_name:
mov eax,[esi-4]
add esi,eax
jmp find_type_name
type_name_ok:
or edx,edx
jz type_name_directory_done
mov ebx,edx
make_type_name_entry:
mov eax,[resource_data]
inc word [eax+12]
lea eax,[edi+8]
cmp eax,[tagged_blocks]
jae out_of_memory
mov eax,ebx
stos dword [edi]
xor eax,eax
stos dword [edi]
jmp make_type_name_directory
type_name_directory_done:
mov ebx,-1
make_type_id_directory:
mov esi,[free_additional_memory]
mov edx,10000h
find_type_id:
cmp dword [esi],0
je type_id_ok
add esi,20
cmp word [esi],0FFFFh
jne check_next_type_id
movzx eax,word [esi+2]
cmp eax,ebx
jle check_next_type_id
cmp eax,edx
jg check_next_type_id
mov edx,eax
mov [esi-16],edi
check_next_type_id:
mov eax,[esi-4]
add esi,eax
jmp find_type_id
type_id_ok:
cmp edx,10000h
je type_id_directory_done
mov ebx,edx
make_type_id_entry:
mov eax,[resource_data]
inc word [eax+14]
lea eax,[edi+8]
cmp eax,[tagged_blocks]
jae out_of_memory
mov eax,ebx
stos dword [edi]
xor eax,eax
stos dword [edi]
jmp make_type_id_directory
type_id_directory_done:
mov esi,[resource_data]
add esi,10h
mov ecx,[esi-4]
or cx,cx
jz resource_directories_ok
make_resource_directories:
push ecx
push edi
mov edx,edi
sub edx,[resource_data]
bts edx,31
mov [esi+4],edx
lea eax,[edi+16]
cmp eax,[tagged_blocks]
jae out_of_memory
xor eax,eax
stos dword [edi]
call make_timestamp
stos dword [edi]
xor eax,eax
stos dword [edi]
stos dword [edi]
mov ebp,esi
xor ebx,ebx
make_resource_name_directory:
mov esi,[free_additional_memory]
xor edx,edx
find_resource_name:
cmp dword [esi],0
je resource_name_ok
push esi
cmp [esi+4],ebp
jne check_next_resource_name
add esi,20
call skip_resource_name
cmp word [esi],0FFFFh
je check_next_resource_name
or ebx,ebx
jz check_this_resource_name
xor ecx,ecx
compare_with_previous_resource_name:
mov ax,[esi+ecx]
cmp ax,[ebx+ecx]
ja check_this_resource_name
jb check_next_resource_name
add ecx,2
mov ax,[esi+ecx]
or ax,[ebx+ecx]
jnz compare_with_previous_resource_name
jmp check_next_resource_name
skip_resource_name:
cmp word [esi],0FFFFh
jne skip_unicode_string
add esi,4
ret
skip_unicode_string:
add esi,2
cmp word [esi-2],0
jne skip_unicode_string
ret
check_this_resource_name:
or edx,edx
jz resource_name_found
xor ecx,ecx
compare_with_current_resource_name:
mov ax,[esi+ecx]
cmp ax,[edx+ecx]
ja check_next_resource_name
jb resource_name_found
add ecx,2
mov ax,[esi+ecx]
or ax,[edx+ecx]
jnz compare_with_current_resource_name
jmp same_resource_name
resource_name_found:
mov edx,esi
same_resource_name:
mov eax,[esp]
mov [eax+8],edi
check_next_resource_name:
pop esi
mov eax,[esi+16]
lea esi,[esi+20+eax]
jmp find_resource_name
resource_name_ok:
or edx,edx
jz resource_name_directory_done
mov ebx,edx
make_resource_name_entry:
mov eax,[esp]
inc word [eax+12]
lea eax,[edi+8]
cmp eax,[tagged_blocks]
jae out_of_memory
mov eax,ebx
stos dword [edi]
xor eax,eax
stos dword [edi]
jmp make_resource_name_directory
resource_name_directory_done:
mov ebx,-1
make_resource_id_directory:
mov esi,[free_additional_memory]
mov edx,10000h
find_resource_id:
cmp dword [esi],0
je resource_id_ok
push esi
cmp [esi+4],ebp
jne check_next_resource_id
add esi,20
call skip_resource_name
cmp word [esi],0FFFFh
jne check_next_resource_id
movzx eax,word [esi+2]
cmp eax,ebx
jle check_next_resource_id
cmp eax,edx
jg check_next_resource_id
mov edx,eax
mov eax,[esp]
mov [eax+8],edi
check_next_resource_id:
pop esi
mov eax,[esi+16]
lea esi,[esi+20+eax]
jmp find_resource_id
resource_id_ok:
cmp edx,10000h
je resource_id_directory_done
mov ebx,edx
make_resource_id_entry:
mov eax,[esp]
inc word [eax+14]
lea eax,[edi+8]
cmp eax,[tagged_blocks]
jae out_of_memory
mov eax,ebx
stos dword [edi]
xor eax,eax
stos dword [edi]
jmp make_resource_id_directory
resource_id_directory_done:
pop eax
mov esi,ebp
pop ecx
add esi,8
dec cx
jnz make_resource_directories
resource_directories_ok:
shr ecx,16
jnz make_resource_directories
mov esi,[resource_data]
add esi,10h
movzx eax,word [esi-4]
movzx edx,word [esi-2]
add eax,edx
lea esi,[esi+eax*8]
push edi ; address of language directories
update_resource_directories:
cmp esi,[esp]
je resource_directories_updated
add esi,10h
mov ecx,[esi-4]
or cx,cx
jz language_directories_ok
make_language_directories:
push ecx
push edi
mov edx,edi
sub edx,[resource_data]
bts edx,31
mov [esi+4],edx
lea eax,[edi+16]
cmp eax,[tagged_blocks]
jae out_of_memory
xor eax,eax
stos dword [edi]
call make_timestamp
stos dword [edi]
xor eax,eax
stos dword [edi]
stos dword [edi]
mov ebp,esi
mov ebx,-1
make_language_id_directory:
mov esi,[free_additional_memory]
mov edx,10000h
find_language_id:
cmp dword [esi],0
je language_id_ok
push esi
cmp [esi+8],ebp
jne check_next_language_id
add esi,20
mov eax,esi
call skip_resource_name
call skip_resource_name
neg eax
add eax,esi
and eax,11b
add esi,eax
get_language_id:
movzx eax,word [esi+6]
cmp eax,ebx
jle check_next_language_id
cmp eax,edx
jge check_next_language_id
mov edx,eax
mov eax,[esp]
mov dword [value],eax
check_next_language_id:
pop esi
mov eax,[esi+16]
lea esi,[esi+20+eax]
jmp find_language_id
language_id_ok:
cmp edx,10000h
je language_id_directory_done
mov ebx,edx
make_language_id_entry:
mov eax,[esp]
inc word [eax+14]
lea eax,[edi+8]
cmp eax,[tagged_blocks]
jae out_of_memory
mov eax,ebx
stos dword [edi]
mov eax,dword [value]
stos dword [edi]
jmp make_language_id_directory
language_id_directory_done:
pop eax
mov esi,ebp
pop ecx
add esi,8
dec cx
jnz make_language_directories
language_directories_ok:
shr ecx,16
jnz make_language_directories
jmp update_resource_directories
resource_directories_updated:
mov esi,[resource_data]
push edi
make_name_strings:
add esi,10h
movzx eax,word [esi-2]
movzx ecx,word [esi-4]
add eax,ecx
lea eax,[esi+eax*8]
push eax
or ecx,ecx
jz string_entries_processed
process_string_entries:
push ecx
mov edx,edi
sub edx,[resource_data]
bts edx,31
xchg [esi],edx
mov ebx,edi
xor ax,ax
stos word [edi]
copy_string_data:
lea eax,[edi+2]
cmp eax,[tagged_blocks]
jae out_of_memory
mov ax,[edx]
or ax,ax
jz string_data_copied
stos word [edi]
inc word [ebx]
add edx,2
jmp copy_string_data
string_data_copied:
add esi,8
pop ecx
loop process_string_entries
string_entries_processed:
pop esi
cmp esi,[esp]
jb make_name_strings
mov eax,edi
sub eax,[resource_data]
test al,11b
jz resource_strings_alignment_ok
xor ax,ax
stos word [edi]
resource_strings_alignment_ok:
pop edx
pop ebx ; address of language directories
mov ebp,edi
update_language_directories:
add ebx,10h
movzx eax,word [ebx-2]
movzx ecx,word [ebx-4]
add ecx,eax
make_data_records:
push ecx
mov esi,edi
sub esi,[resource_data]
xchg esi,[ebx+4]
lea eax,[edi+16]
cmp eax,[tagged_blocks]
jae out_of_memory
mov eax,esi
stos dword [edi]
mov eax,[esi+12]
stos dword [edi]
xor eax,eax
stos dword [edi]
stos dword [edi]
pop ecx
add ebx,8
loop make_data_records
cmp ebx,edx
jb update_language_directories
pop ebx ; file handle
mov esi,ebp
mov ebp,edi
update_data_records:
push ebp
mov ecx,edi
mov eax,[current_section]
sub ecx,[eax+14h]
add ecx,[eax+0Ch]
xchg ecx,[esi]
mov edx,[ecx]
xor al,al
call lseek
mov edx,edi
mov ecx,[esi+4]
add edi,ecx
cmp edi,[tagged_blocks]
ja out_of_memory
call read
mov eax,edi
sub eax,[resource_data]
and eax,11b
jz resource_data_alignment_ok
mov ecx,4
sub ecx,eax
xor al,al
rep stos byte [edi]
resource_data_alignment_ok:
pop ebp
add esi,16
cmp esi,ebp
jb update_data_records
pop esi
call close
mov eax,edi
sub eax,[resource_data]
mov [resource_size],eax
resource_done:
ret
close_pe:
call close_pe_section
mov edx,[code_start]
mov [edx+50h],eax
call make_timestamp
mov edx,[code_start]
mov [edx+8],eax
mov eax,[number_of_sections]
mov [edx+6],ax
imul eax,28h
movzx ecx,word [edx+14h]
lea eax,[eax+18h+ecx]
add eax,[stub_size]
mov ecx,[edx+3Ch]
dec ecx
add eax,ecx
not ecx
and eax,ecx
cmp eax,[edx+54h]
je pe_sections_ok
or [next_pass_needed],-1
pe_sections_ok:
xor ecx,ecx
add edx,78h
test [format_flags],4
jz process_directories
add edx,10h
process_directories:
mov eax,[edx+ecx*8]
or eax,eax
jz directory_ok
cmp dword [edx+ecx*8+4],-1
jne directory_ok
section_data:
mov ebx,[edx+ecx*8]
mov eax,[ebx+0Ch]
mov [edx+ecx*8],eax ; directory rva
mov eax,[ebx+8]
mov [edx+ecx*8+4],eax ; directory size
directory_ok:
inc cl
cmp cl,10h
jb process_directories
cmp dword [edx+5*8],0
jne finish_pe_relocations
mov eax,[number_of_relocations]
shl eax,2
sub [free_additional_memory],eax
btr [resolver_flags],0
jnc pe_relocations_ok
or [next_pass_needed],-1
jmp pe_relocations_ok
finish_pe_relocations:
push edi
mov edi,[reserved_fixups]
call make_fixups
pop edi
add [actual_fixups_size],eax
cmp eax,[reserved_fixups_size]
je pe_relocations_ok
or [next_pass_needed],-1
pe_relocations_ok:
mov ebx,[code_start]
sub ebx,[stub_size]
mov ecx,edi
sub ecx,ebx
mov ebp,ecx
shr ecx,1
xor eax,eax
cdq
calculate_checksum:
mov dx,[ebx]
add eax,edx
mov dx,ax
shr eax,16
add eax,edx
add ebx,2
loop calculate_checksum
add eax,ebp
mov ebx,[code_start]
mov [ebx+58h],eax
ret
format_coff:
mov eax,[additional_memory]
mov [symbols_stream],eax
mov ebx,eax
add eax,20h
cmp eax,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],eax
xor eax,eax
mov [ebx],al
mov [ebx+4],eax
mov [ebx+8],edi
mov al,4
mov [ebx+10h],eax
mov al,60h
bt [format_flags],0
jnc flat_section_flags_ok
or eax,0E0000000h
flat_section_flags_ok:
mov dword [ebx+14h],eax
mov [current_section],ebx
xor eax,eax
mov [number_of_sections],eax
mov edx,ebx
call init_addressing_space
mov [ebx+14h],edx
mov byte [ebx+9],2
mov [code_type],32
test [format_flags],8
jz format_defined
mov byte [ebx+9],4
mov [code_type],64
jmp format_defined
coff_section:
call close_coff_section
mov ebx,[free_additional_memory]
lea eax,[ebx+20h]
cmp eax,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],eax
mov [current_section],ebx
inc [number_of_sections]
xor eax,eax
mov [ebx],al
mov [ebx+8],edi
mov [ebx+10h],eax
mov [ebx+14h],eax
mov edx,ebx
call create_addressing_space
xchg edx,ebx
mov [edx+14h],ebx
mov byte [edx+9],2
test [format_flags],8
jz coff_labels_type_ok
mov byte [edx+9],4
coff_labels_type_ok:
lods word [esi]
cmp ax,'('
jne invalid_argument
mov [ebx+4],esi
mov ecx,[esi]
lea esi,[esi+4+ecx+1]
cmp ecx,8
ja name_too_long
coff_section_flags:
cmp byte [esi],8Ch
je coff_section_alignment
cmp byte [esi],19h
jne coff_section_settings_ok
inc esi
lods byte [esi]
bt [format_flags],0
jc coff_section_flag_ok
cmp al,7
ja invalid_argument
coff_section_flag_ok:
mov cl,al
mov eax,1
shl eax,cl
test dword [ebx+14h],eax
jnz setting_already_specified
or dword [ebx+14h],eax
jmp coff_section_flags
coff_section_alignment:
bt [format_flags],0
jnc invalid_argument
inc esi
lods byte [esi]
cmp al,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
push ebx
call get_count_value
pop ebx
mov edx,eax
dec edx
test eax,edx
jnz invalid_value
or eax,eax
jz invalid_value
cmp eax,2000h
ja invalid_value
bsf edx,eax
inc edx
shl edx,20
or [ebx+14h],edx
xchg [ebx+10h],eax
or eax,eax
jnz setting_already_specified
jmp coff_section_flags
coff_section_settings_ok:
cmp dword [ebx+10h],0
jne instruction_assembled
mov dword [ebx+10h],4
bt [format_flags],0
jnc instruction_assembled
or dword [ebx+14h],300000h
jmp instruction_assembled
close_coff_section:
mov ebx,[current_section]
mov eax,edi
mov edx,[ebx+8]
sub eax,edx
mov [ebx+0Ch],eax
xor eax,eax
xchg [undefined_data_end],eax
cmp eax,edi
jne coff_section_ok
cmp edx,[undefined_data_start]
jne coff_section_ok
mov edi,edx
or byte [ebx+14h],80h
coff_section_ok:
ret
mark_coff_relocation:
cmp [value_type],3
je coff_relocation_relative
push ebx eax
test [format_flags],8
jnz coff_64bit_relocation
mov al,6
cmp [value_type],2
je coff_relocation
cmp [value_type],5
jne invalid_use_of_symbol
inc al
jmp coff_relocation
coff_64bit_relocation:
mov al,1
cmp [value_type],4
je coff_relocation
mov al,2
cmp [value_type],2
je coff_relocation
cmp [value_type],5
jne invalid_use_of_symbol
inc al
jmp coff_relocation
coff_relocation_relative:
push ebx
bt [format_flags],0
jnc relative_ok
mov ebx,[current_section]
mov ebx,[ebx+8]
sub ebx,edi
sub eax,ebx
add eax,4
relative_ok:
mov ebx,[addressing_space]
push eax
mov al,20
test [format_flags],8
jnz relative_coff_64bit_relocation
cmp byte [ebx+9],2
jne invalid_use_of_symbol
jmp coff_relocation
relative_coff_64bit_relocation:
mov al,4
cmp byte [ebx+9],4
jne invalid_use_of_symbol
coff_relocation:
mov ebx,[free_additional_memory]
add ebx,0Ch
cmp ebx,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],ebx
mov byte [ebx-0Ch],al
mov eax,[current_section]
mov eax,[eax+8]
neg eax
add eax,edi
mov [ebx-0Ch+4],eax
mov eax,[symbol_identifier]
mov [ebx-0Ch+8],eax
pop eax ebx
ret
close_coff:
call close_coff_section
cmp [next_pass_needed],0
je coff_closed
mov eax,[symbols_stream]
mov [free_additional_memory],eax
coff_closed:
ret
coff_formatter:
sub edi,[code_start]
mov [code_size],edi
call prepare_default_section
mov edi,[free_additional_memory]
mov ebx,edi
mov ecx,28h shr 2
imul ecx,[number_of_sections]
add ecx,14h shr 2
lea eax,[edi+ecx*4]
cmp eax,[structures_buffer]
jae out_of_memory
xor eax,eax
rep stos dword [edi]
mov word [ebx],14Ch
test [format_flags],8
jz coff_magic_ok
mov word [ebx],8664h
coff_magic_ok:
mov word [ebx+12h],104h
bt [format_flags],0
jnc coff_flags_ok
or byte [ebx+12h],80h
coff_flags_ok:
push ebx
call make_timestamp
pop ebx
mov [ebx+4],eax
mov eax,[number_of_sections]
mov [ebx+2],ax
mov esi,[symbols_stream]
xor eax,eax
xor ecx,ecx
enumerate_symbols:
cmp esi,[free_additional_memory]
je symbols_enumerated
mov dl,[esi]
or dl,dl
jz enumerate_section
cmp dl,0C0h
jae enumerate_public
cmp dl,80h
jae enumerate_extrn
add esi,0Ch
jmp enumerate_symbols
enumerate_section:
mov edx,eax
shl edx,8
mov [esi],edx
inc eax
inc ecx
mov [esi+1Eh],cx
add esi,20h
jmp enumerate_symbols
enumerate_public:
mov edx,eax
shl edx,8
mov dl,[esi]
mov [esi],edx
mov edx,[esi+8]
add esi,10h
inc eax
cmp byte [edx+11],0
je enumerate_symbols
mov edx,[edx+20]
cmp byte [edx],0C0h
jae enumerate_symbols
cmp byte [edx],80h
jb enumerate_symbols
inc eax
jmp enumerate_symbols
enumerate_extrn:
mov edx,eax
shl edx,8
mov dl,[esi]
mov [esi],edx
add esi,0Ch
inc eax
jmp enumerate_symbols
prepare_default_section:
mov ebx,[symbols_stream]
cmp dword [ebx+0Ch],0
jne default_section_ok
cmp [number_of_sections],0
je default_section_ok
mov edx,ebx
find_references_to_default_section:
cmp ebx,[free_additional_memory]
jne check_reference
add [symbols_stream],20h
ret
check_reference:
mov al,[ebx]
or al,al
jz skip_other_section
cmp al,0C0h
jae check_public_reference
cmp al,80h
jae next_reference
cmp edx,[ebx+8]
je default_section_ok
next_reference:
add ebx,0Ch
jmp find_references_to_default_section
check_public_reference:
mov eax,[ebx+8]
add ebx,10h
test byte [eax+8],1
jz find_references_to_default_section
mov cx,[current_pass]
cmp cx,[eax+16]
jne find_references_to_default_section
cmp edx,[eax+20]
je default_section_ok
jmp find_references_to_default_section
skip_other_section:
add ebx,20h
jmp find_references_to_default_section
default_section_ok:
inc [number_of_sections]
ret
symbols_enumerated:
mov [ebx+0Ch],eax
mov ebp,edi
sub ebp,ebx
push ebp
lea edi,[ebx+14h]
mov esi,[symbols_stream]
find_section:
cmp esi,[free_additional_memory]
je sections_finished
mov al,[esi]
or al,al
jz section_found
add esi,0Ch
cmp al,0C0h
jb find_section
add esi,4
jmp find_section
section_found:
push esi edi
mov esi,[esi+4]
or esi,esi
jz default_section
mov ecx,[esi]
add esi,4
rep movs byte [edi],[esi]
jmp section_name_ok
default_section:
mov al,'.'
stos byte [edi]
mov eax,'flat'
stos dword [edi]
section_name_ok:
pop edi esi
mov eax,[esi+0Ch]
mov [edi+10h],eax
mov eax,[esi+14h]
mov [edi+24h],eax
test al,80h
jnz section_ptr_ok
mov eax,[esi+8]
sub eax,[code_start]
add eax,ebp
mov [edi+14h],eax
section_ptr_ok:
mov ebx,[code_start]
mov edx,[code_size]
add ebx,edx
add edx,ebp
xor ecx,ecx
add esi,20h
find_relocations:
cmp esi,[free_additional_memory]
je section_relocations_done
mov al,[esi]
or al,al
jz section_relocations_done
cmp al,80h
jb add_relocation
cmp al,0C0h
jb next_relocation
add esi,10h
jmp find_relocations
add_relocation:
lea eax,[ebx+0Ah]
cmp eax,[tagged_blocks]
ja out_of_memory
mov eax,[esi+4]
mov [ebx],eax
mov eax,[esi+8]
mov eax,[eax]
shr eax,8
mov [ebx+4],eax
movzx ax,byte [esi]
mov [ebx+8],ax
add ebx,0Ah
inc ecx
next_relocation:
add esi,0Ch
jmp find_relocations
section_relocations_done:
cmp ecx,10000h
jb section_relocations_count_16bit
bt [format_flags],0
jnc format_limitations_exceeded
mov word [edi+20h],0FFFFh
or dword [edi+24h],1000000h
mov [edi+18h],edx
push esi edi
push ecx
lea esi,[ebx-1]
add ebx,0Ah
lea edi,[ebx-1]
imul ecx,0Ah
std
rep movs byte [edi],[esi]
cld
pop ecx
inc esi
inc ecx
mov [esi],ecx
xor eax,eax
mov [esi+4],eax
mov [esi+8],ax
pop edi esi
jmp section_relocations_ok
section_relocations_count_16bit:
mov [edi+20h],cx
jcxz section_relocations_ok
mov [edi+18h],edx
section_relocations_ok:
sub ebx,[code_start]
mov [code_size],ebx
add edi,28h
jmp find_section
sections_finished:
mov edx,[free_additional_memory]
mov ebx,[code_size]
add ebp,ebx
mov [edx+8],ebp
add ebx,[code_start]
mov edi,ebx
mov ecx,[edx+0Ch]
imul ecx,12h shr 1
xor eax,eax
shr ecx,1
jnc zero_symbols_table
stos word [edi]
zero_symbols_table:
rep stos dword [edi]
mov edx,edi
stos dword [edi]
mov esi,[symbols_stream]
make_symbols_table:
cmp esi,[free_additional_memory]
je symbols_table_ok
mov al,[esi]
cmp al,0C0h
jae add_public_symbol
cmp al,80h
jae add_extrn_symbol
or al,al
jz add_section_symbol
add esi,0Ch
jmp make_symbols_table
add_section_symbol:
call store_symbol_name
movzx eax,word [esi+1Eh]
mov [ebx+0Ch],ax
mov byte [ebx+10h],3
add esi,20h
add ebx,12h
jmp make_symbols_table
add_extrn_symbol:
call store_symbol_name
mov byte [ebx+10h],2
add esi,0Ch
add ebx,12h
jmp make_symbols_table
add_public_symbol:
call store_symbol_name
mov eax,[esi+0Ch]
mov [current_line],eax
mov eax,[esi+8]
test byte [eax+8],1
jz undefined_coff_public
mov cx,[current_pass]
cmp cx,[eax+16]
jne undefined_coff_public
mov cl,[eax+11]
or cl,cl
jz public_constant
test [format_flags],8
jnz check_64bit_public_symbol
cmp cl,2
je public_symbol_type_ok
jmp invalid_use_of_symbol
undefined_coff_public:
mov [error_info],eax
jmp undefined_symbol
check_64bit_public_symbol:
cmp cl,4
jne invalid_use_of_symbol
public_symbol_type_ok:
mov ecx,[eax+20]
cmp byte [ecx],80h
je alias_symbol
cmp byte [ecx],0
jne invalid_use_of_symbol
mov cx,[ecx+1Eh]
mov [ebx+0Ch],cx
public_symbol_section_ok:
movzx ecx,byte [eax+9]
shr cl,1
and cl,1
neg ecx
cmp ecx,[eax+4]
jne value_out_of_range
xor ecx,[eax]
js value_out_of_range
mov eax,[eax]
mov [ebx+8],eax
mov al,2
cmp byte [esi],0C0h
je store_symbol_class
inc al
cmp byte [esi],0C1h
je store_symbol_class
mov al,105
store_symbol_class:
mov byte [ebx+10h],al
add esi,10h
add ebx,12h
jmp make_symbols_table
alias_symbol:
bt [format_flags],0
jnc invalid_use_of_symbol
mov ecx,[eax]
or ecx,[eax+4]
jnz invalid_use_of_symbol
mov byte [ebx+10h],69h
mov byte [ebx+11h],1
add ebx,12h
mov ecx,[eax+20]
mov ecx,[ecx]
shr ecx,8
mov [ebx],ecx
mov byte [ebx+4],3
add esi,10h
add ebx,12h
jmp make_symbols_table
public_constant:
mov word [ebx+0Ch],0FFFFh
jmp public_symbol_section_ok
symbols_table_ok:
mov eax,edi
sub eax,edx
mov [edx],eax
sub edi,[code_start]
mov [code_size],edi
and [written_size],0
mov edx,[output_file]
call create
jc write_failed
mov edx,[free_additional_memory]
pop ecx
add [written_size],ecx
call write
jc write_failed
jmp write_output
store_symbol_name:
push esi
mov esi,[esi+4]
or esi,esi
jz default_name
lods dword [esi]
mov ecx,eax
cmp ecx,8
ja add_string
push edi
mov edi,ebx
rep movs byte [edi],[esi]
pop edi esi
ret
default_name:
mov dword [ebx],'.fla'
mov dword [ebx+4],'t'
pop esi
ret
add_string:
mov eax,edi
sub eax,edx
mov [ebx+4],eax
inc ecx
rep movs byte [edi],[esi]
pop esi
ret
format_elf:
test [format_flags],8
jnz format_elf64
mov edx,edi
mov ecx,34h shr 2
lea eax,[edi+ecx*4]
cmp eax,[tagged_blocks]
jae out_of_memory
xor eax,eax
rep stos dword [edi]
mov dword [edx],7Fh + 'ELF' shl 8
mov al,1
mov [edx+4],al
mov [edx+5],al
mov [edx+6],al
mov [edx+14h],al
mov byte [edx+12h],3
mov byte [edx+28h],34h
mov byte [edx+2Eh],28h
mov [code_type],32
mov byte [edx+10h],2
cmp word [esi],1D19h
je format_elf_exe
mov byte [edx+10h],3
cmp word [esi],021Eh
je format_elf_exe
elf_header_ok:
mov byte [edx+10h],1
mov eax,[additional_memory]
mov [symbols_stream],eax
mov ebx,eax
add eax,20h
cmp eax,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],eax
xor eax,eax
mov [current_section],ebx
mov [number_of_sections],eax
mov [ebx],al
mov [ebx+4],eax
mov [ebx+8],edi
mov al,111b
mov [ebx+14h],eax
mov al,4
mov [ebx+10h],eax
mov edx,ebx
call init_addressing_space
xchg edx,ebx
mov [edx+14h],ebx
mov byte [edx+9],2
test [format_flags],8
jz format_defined
mov byte [edx+9],4
mov byte [ebx+10h],8
jmp format_defined
format_elf64:
mov edx,edi
mov ecx,40h shr 2
lea eax,[edi+ecx*4]
cmp eax,[tagged_blocks]
jae out_of_memory
xor eax,eax
rep stos dword [edi]
mov dword [edx],7Fh + 'ELF' shl 8
mov al,1
mov [edx+5],al
mov [edx+6],al
mov [edx+14h],al
mov byte [edx+4],2
mov byte [edx+12h],62
mov byte [edx+34h],40h
mov byte [edx+3Ah],40h
mov [code_type],64
mov byte [edx+10h],2
cmp word [esi],1D19h
je format_elf64_exe
mov byte [edx+10h],3
cmp word [esi],021Eh
je format_elf64_exe
jmp elf_header_ok
elf_section:
bt [format_flags],0
jc illegal_instruction
call close_coff_section
mov ebx,[free_additional_memory]
lea eax,[ebx+20h]
cmp eax,[structures_buffer]
jae out_of_memory
mov [free_additional_memory],eax
mov [current_section],ebx
inc word [number_of_sections]
jz format_limitations_exceeded
xor eax,eax
mov [ebx],al
mov [ebx+8],edi
mov [ebx+10h],eax
mov al,10b
mov [ebx+14h],eax
mov edx,ebx
call create_addressing_space
xchg edx,ebx
mov [edx+14h],ebx
mov byte [edx+9],2
test [format_flags],8
jz elf_labels_type_ok
mov byte [edx+9],4
elf_labels_type_ok:
lods word [esi]
cmp ax,'('
jne invalid_argument
mov [ebx+4],esi
mov ecx,[esi]
lea esi,[esi+4+ecx+1]
elf_section_flags:
cmp byte [esi],8Ch
je elf_section_alignment
cmp byte [esi],19h
jne elf_section_settings_ok
inc esi
lods byte [esi]
sub al,28
xor al,11b
test al,not 10b
jnz invalid_argument
mov cl,al
mov al,1
shl al,cl
test byte [ebx+14h],al
jnz setting_already_specified
or byte [ebx+14h],al
jmp elf_section_flags
elf_section_alignment:
inc esi
lods byte [esi]
cmp al,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
push ebx
call get_count_value
pop ebx
mov edx,eax
dec edx
test eax,edx
jnz invalid_value
or eax,eax
jz invalid_value
xchg [ebx+10h],eax
or eax,eax
jnz setting_already_specified
jmp elf_section_flags
elf_section_settings_ok:
cmp dword [ebx+10h],0
jne instruction_assembled
mov dword [ebx+10h],4
test [format_flags],8
jz instruction_assembled
mov byte [ebx+10h],8
jmp instruction_assembled
mark_elf_relocation:
test [format_flags],1
jnz invalid_use_of_symbol
push ebx
mov ebx,[addressing_space]
cmp [value_type],3
je elf_relocation_relative
cmp [value_type],7
je elf_relocation_relative
push eax
cmp [value_type],5
je elf_gotoff_relocation
ja invalid_use_of_symbol
mov al,1 ; R_386_32 / R_AMD64_64
test [format_flags],8
jz coff_relocation
cmp [value_type],4
je coff_relocation
mov al,11 ; R_AMD64_32S
jmp coff_relocation
elf_gotoff_relocation:
test [format_flags],8
jnz invalid_use_of_symbol
mov al,9 ; R_386_GOTOFF
jmp coff_relocation
elf_relocation_relative:
cmp byte [ebx+9],0
je invalid_use_of_symbol
mov ebx,[current_section]
mov ebx,[ebx+8]
sub ebx,edi
sub eax,ebx
push eax
mov al,2 ; R_386_PC32 / R_AMD64_PC32
cmp [value_type],3
je coff_relocation
mov al,4 ; R_386_PLT32 / R_AMD64_PLT32
jmp coff_relocation
close_elf:
bt [format_flags],0
jc close_elf_exe
call close_coff_section
cmp [next_pass_needed],0
je elf_closed
mov eax,[symbols_stream]
mov [free_additional_memory],eax
elf_closed:
ret
elf_formatter:
mov ecx,edi
sub ecx,[code_start]
neg ecx
and ecx,111b
test [format_flags],8
jnz align_elf_structures
and ecx,11b
align_elf_structures:
xor al,al
rep stos byte [edi]
push edi
call prepare_default_section
mov esi,[symbols_stream]
mov edi,[free_additional_memory]
xor eax,eax
mov ecx,4
rep stos dword [edi]
test [format_flags],8
jz find_first_section
mov ecx,2
rep stos dword [edi]
find_first_section:
mov al,[esi]
or al,al
jz first_section_found
cmp al,0C0h
jb skip_other_symbol
add esi,4
skip_other_symbol:
add esi,0Ch
jmp find_first_section
first_section_found:
mov ebx,esi
mov ebp,esi
add esi,20h
xor ecx,ecx
xor edx,edx
find_next_section:
cmp esi,[free_additional_memory]
je make_section_symbol
mov al,[esi]
or al,al
jz make_section_symbol
cmp al,0C0h
jae skip_public
cmp al,80h
jae skip_extrn
or byte [ebx+14h],40h
skip_extrn:
add esi,0Ch
jmp find_next_section
skip_public:
add esi,10h
jmp find_next_section
make_section_symbol:
mov eax,edi
xchg eax,[ebx+4]
stos dword [edi]
test [format_flags],8
jnz elf64_section_symbol
xor eax,eax
stos dword [edi]
stos dword [edi]
call store_section_index
jmp section_symbol_ok
store_section_index:
inc ecx
mov eax,ecx
shl eax,8
mov [ebx],eax
inc dx
jz format_limitations_exceeded
mov eax,edx
shl eax,16
mov al,3
test byte [ebx+14h],40h
jz section_index_ok
or ah,-1
inc dx
jz format_limitations_exceeded
section_index_ok:
stos dword [edi]
ret
elf64_section_symbol:
call store_section_index
xor eax,eax
stos dword [edi]
stos dword [edi]
stos dword [edi]
stos dword [edi]
section_symbol_ok:
mov ebx,esi
add esi,20h
cmp ebx,[free_additional_memory]
jne find_next_section
inc dx
jz format_limitations_exceeded
mov [current_section],edx
mov esi,[symbols_stream]
find_other_symbols:
cmp esi,[free_additional_memory]
je elf_symbol_table_ok
mov al,[esi]
or al,al
jz skip_section
cmp al,0C0h
jae make_public_symbol
cmp al,80h
jae make_extrn_symbol
add esi,0Ch
jmp find_other_symbols
skip_section:
add esi,20h
jmp find_other_symbols
make_public_symbol:
mov eax,[esi+0Ch]
mov [current_line],eax
cmp byte [esi],0C0h
jne invalid_argument
mov ebx,[esi+8]
test byte [ebx+8],1
jz undefined_public
mov ax,[current_pass]
cmp ax,[ebx+16]
jne undefined_public
mov dl,[ebx+11]
or dl,dl
jz public_absolute
mov eax,[ebx+20]
cmp byte [eax],0
jne invalid_use_of_symbol
mov eax,[eax+4]
test [format_flags],8
jnz elf64_public
cmp dl,2
jne invalid_use_of_symbol
mov dx,[eax+0Eh]
jmp section_for_public_ok
undefined_public:
mov [error_info],ebx
jmp undefined_symbol
elf64_public:
cmp dl,4
jne invalid_use_of_symbol
mov dx,[eax+6]
jmp section_for_public_ok
public_absolute:
mov dx,0FFF1h
section_for_public_ok:
mov eax,[esi+4]
stos dword [edi]
test [format_flags],8
jnz elf64_public_symbol
movzx eax,byte [ebx+9]
shr al,1
and al,1
neg eax
cmp eax,[ebx+4]
jne value_out_of_range
xor eax,[ebx]
js value_out_of_range
mov eax,[ebx]
stos dword [edi]
xor eax,eax
mov al,[ebx+10]
stos dword [edi]
mov eax,edx
shl eax,16
mov al,10h
cmp byte [ebx+10],0
je elf_public_function
or al,1
jmp store_elf_public_info
elf_public_function:
or al,2
store_elf_public_info:
stos dword [edi]
jmp public_symbol_ok
elf64_public_symbol:
mov eax,edx
shl eax,16
mov al,10h
cmp byte [ebx+10],0
je elf64_public_function
or al,1
jmp store_elf64_public_info
elf64_public_function:
or al,2
store_elf64_public_info:
stos dword [edi]
mov al,[ebx+9]
shl eax,31-1
xor eax,[ebx+4]
js value_out_of_range
mov eax,[ebx]
stos dword [edi]
mov eax,[ebx+4]
stos dword [edi]
mov al,[ebx+10]
stos dword [edi]
xor al,al
stos dword [edi]
public_symbol_ok:
inc ecx
mov eax,ecx
shl eax,8
mov al,0C0h
mov [esi],eax
add esi,10h
jmp find_other_symbols
make_extrn_symbol:
mov eax,[esi+4]
stos dword [edi]
test [format_flags],8
jnz elf64_extrn_symbol
xor eax,eax
stos dword [edi]
mov eax,[esi+8]
stos dword [edi]
mov eax,10h
stos dword [edi]
jmp extrn_symbol_ok
elf64_extrn_symbol:
mov eax,10h
stos dword [edi]
xor al,al
stos dword [edi]
stos dword [edi]
mov eax,[esi+8]
stos dword [edi]
xor eax,eax
stos dword [edi]
extrn_symbol_ok:
inc ecx
mov eax,ecx
shl eax,8
mov al,80h
mov [esi],eax
add esi,0Ch
jmp find_other_symbols
elf_symbol_table_ok:
mov edx,edi
mov ebx,[free_additional_memory]
xor al,al
stos byte [edi]
add edi,16
mov [edx+1],edx
add ebx,10h
test [format_flags],8
jz make_string_table
add ebx,8
make_string_table:
cmp ebx,edx
je elf_string_table_ok
test [format_flags],8
jnz make_elf64_string
cmp byte [ebx+0Dh],0
je rel_prefix_ok
mov byte [ebx+0Dh],0
mov eax,'.rel'
stos dword [edi]
rel_prefix_ok:
mov esi,edi
sub esi,edx
xchg esi,[ebx]
add ebx,10h
make_elf_string:
or esi,esi
jz default_string
lods dword [esi]
mov ecx,eax
rep movs byte [edi],[esi]
xor al,al
stos byte [edi]
jmp make_string_table
make_elf64_string:
cmp byte [ebx+5],0
je elf64_rel_prefix_ok
mov byte [ebx+5],0
mov eax,'.rel'
stos dword [edi]
mov al,'a'
stos byte [edi]
elf64_rel_prefix_ok:
mov esi,edi
sub esi,edx
xchg esi,[ebx]
add ebx,18h
jmp make_elf_string
default_string:
mov eax,'.fla'
stos dword [edi]
mov ax,'t'
stos word [edi]
jmp make_string_table
elf_string_table_ok:
mov [edx+1+8],edi
mov ebx,[code_start]
mov eax,edi
sub eax,[free_additional_memory]
xor ecx,ecx
sub ecx,eax
test [format_flags],8
jnz finish_elf64_header
and ecx,11b
add eax,ecx
mov [ebx+20h],eax
mov eax,[current_section]
inc ax
jz format_limitations_exceeded
mov [ebx+32h],ax
inc ax
jz format_limitations_exceeded
mov [ebx+30h],ax
jmp elf_header_finished
finish_elf64_header:
and ecx,111b
add eax,ecx
mov [ebx+28h],eax
mov eax,[current_section]
inc ax
jz format_limitations_exceeded
mov [ebx+3Eh],ax
inc ax
jz format_limitations_exceeded
mov [ebx+3Ch],ax
elf_header_finished:
xor eax,eax
add ecx,10*4
rep stos byte [edi]
test [format_flags],8
jz elf_null_section_ok
mov ecx,6*4
rep stos byte [edi]
elf_null_section_ok:
mov esi,ebp
xor ecx,ecx
make_section_entry:
mov ebx,edi
mov eax,[esi+4]
mov eax,[eax]
stos dword [edi]
mov eax,1
cmp dword [esi+0Ch],0
je bss_section
test byte [esi+14h],80h
jz section_type_ok
bss_section:
mov al,8
section_type_ok:
stos dword [edi]
mov eax,[esi+14h]
and al,3Fh
call store_elf_machine_word
xor eax,eax
call store_elf_machine_word
mov eax,[esi+8]
mov [image_base],eax
sub eax,[code_start]
call store_elf_machine_word
mov eax,[esi+0Ch]
call store_elf_machine_word
xor eax,eax
stos dword [edi]
stos dword [edi]
mov eax,[esi+10h]
call store_elf_machine_word
xor eax,eax
call store_elf_machine_word
inc ecx
add esi,20h
xchg edi,[esp]
mov ebp,edi
convert_relocations:
cmp esi,[free_additional_memory]
je relocations_converted
mov al,[esi]
or al,al
jz relocations_converted
cmp al,80h
jb make_relocation_entry
cmp al,0C0h
jb relocation_entry_ok
add esi,10h
jmp convert_relocations
make_relocation_entry:
test [format_flags],8
jnz make_elf64_relocation_entry
mov eax,[esi+4]
stos dword [edi]
mov eax,[esi+8]
mov eax,[eax]
mov al,[esi]
stos dword [edi]
jmp relocation_entry_ok
make_elf64_relocation_entry:
mov eax,[esi+4]
stos dword [edi]
xor eax,eax
stos dword [edi]
movzx eax,byte [esi]
stos dword [edi]
mov eax,[esi+8]
mov eax,[eax]
shr eax,8
stos dword [edi]
xor eax,eax
push edx
mov edx,[esi+4]
add edx,[image_base]
xchg eax,[edx]
stos dword [edi]
cmp byte [esi],1
je addend_64bit
pop edx
sar eax,31
stos dword [edi]
jmp relocation_entry_ok
addend_64bit:
xor eax,eax
xchg eax,[edx+4]
stos dword [edi]
pop edx
relocation_entry_ok:
add esi,0Ch
jmp convert_relocations
store_elf_machine_word:
stos dword [edi]
test [format_flags],8
jz elf_machine_word_ok
and dword [edi],0
add edi,4
elf_machine_word_ok:
ret
relocations_converted:
cmp edi,ebp
xchg edi,[esp]
je rel_section_ok
mov eax,[ebx]
sub eax,4
test [format_flags],8
jz store_relocations_name_offset
dec eax
store_relocations_name_offset:
stos dword [edi]
test [format_flags],8
jnz rela_section
mov eax,9
jmp store_relocations_type
rela_section:
mov eax,4
store_relocations_type:
stos dword [edi]
xor al,al
call store_elf_machine_word
call store_elf_machine_word
mov eax,ebp
sub eax,[code_start]
call store_elf_machine_word
mov eax,[esp]
sub eax,ebp
call store_elf_machine_word
mov eax,[current_section]
stos dword [edi]
mov eax,ecx
stos dword [edi]
inc ecx
test [format_flags],8
jnz finish_elf64_rela_section
mov eax,4
stos dword [edi]
mov al,8
stos dword [edi]
jmp rel_section_ok
finish_elf64_rela_section:
mov eax,8
stos dword [edi]
xor al,al
stos dword [edi]
mov al,24
stos dword [edi]
xor al,al
stos dword [edi]
rel_section_ok:
cmp esi,[free_additional_memory]
jne make_section_entry
pop eax
mov ebx,[code_start]
sub eax,ebx
mov [code_size],eax
mov ecx,20h
test [format_flags],8
jz adjust_elf_section_headers_offset
mov ecx,28h
adjust_elf_section_headers_offset:
add [ebx+ecx],eax
mov eax,1
stos dword [edi]
mov al,2
stos dword [edi]
xor al,al
call store_elf_machine_word
call store_elf_machine_word
mov eax,[code_size]
call store_elf_machine_word
mov eax,[edx+1]
sub eax,[free_additional_memory]
call store_elf_machine_word
mov eax,[current_section]
inc eax
stos dword [edi]
mov eax,[number_of_sections]
inc eax
stos dword [edi]
test [format_flags],8
jnz finish_elf64_sym_section
mov eax,4
stos dword [edi]
mov al,10h
stos dword [edi]
jmp sym_section_ok
finish_elf64_sym_section:
mov eax,8
stos dword [edi]
xor al,al
stos dword [edi]
mov al,18h
stos dword [edi]
xor al,al
stos dword [edi]
sym_section_ok:
mov al,1+8
stos dword [edi]
mov al,3
stos dword [edi]
xor al,al
call store_elf_machine_word
call store_elf_machine_word
mov eax,[edx+1]
sub eax,[free_additional_memory]
add eax,[code_size]
call store_elf_machine_word
mov eax,[edx+1+8]
sub eax,[edx+1]
call store_elf_machine_word
xor eax,eax
stos dword [edi]
stos dword [edi]
mov al,1
call store_elf_machine_word
xor eax,eax
call store_elf_machine_word
mov eax,'tab'
mov dword [edx+1],'.sym'
mov [edx+1+4],eax
mov dword [edx+1+8],'.str'
mov [edx+1+8+4],eax
mov [resource_data],edx
mov [written_size],0
mov edx,[output_file]
call create
jc write_failed
call write_code
mov ecx,edi
mov edx,[free_additional_memory]
sub ecx,edx
add [written_size],ecx
call write
jc write_failed
jmp output_written
format_elf_exe:
add esi,2
or [format_flags],1
cmp byte [esi],'('
jne elf_exe_brand_ok
inc esi
cmp byte [esi],'.'
je invalid_value
push edx
call get_byte_value
cmp [value_type],0
jne invalid_use_of_symbol
pop edx
mov [edx+7],al
elf_exe_brand_ok:
mov [image_base],8048000h
cmp byte [esi],80h
jne elf_exe_base_ok
lods word [esi]
cmp ah,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
push edx
call get_dword_value
cmp [value_type],0
jne invalid_use_of_symbol
mov [image_base],eax
pop edx
elf_exe_base_ok:
mov byte [edx+2Ah],20h
mov ebx,edi
mov ecx,20h shr 2
cmp [current_pass],0
je init_elf_segments
imul ecx,[number_of_sections]
init_elf_segments:
xor eax,eax
rep stos dword [edi]
and [number_of_sections],0
mov byte [ebx],1
mov word [ebx+1Ch],1000h
mov byte [ebx+18h],111b
mov ebp,[image_base]
and dword [ebx+4],0
mov [ebx+8],ebp
mov [ebx+0Ch],ebp
mov eax,edi
sub eax,[code_start]
add eax,ebp
mov [edx+18h],eax
and [image_base_high],0
elf_exe_addressing_setup:
call init_addressing_space
call setup_elf_exe_labels_type
mov eax,[code_start]
xor edx,edx
xor cl,cl
sub eax,[image_base]
sbb edx,[image_base_high]
sbb cl,0
mov [ebx],eax
mov [ebx+4],edx
mov [ebx+8],cl
mov [symbols_stream],edi
jmp format_defined
format_elf64_exe:
add esi,2
or [format_flags],1
cmp byte [esi],'('
jne elf64_exe_brand_ok
inc esi
cmp byte [esi],'.'
je invalid_value
push edx
call get_byte_value
cmp [value_type],0
jne invalid_use_of_symbol
pop edx
mov [edx+7],al
elf64_exe_brand_ok:
mov [image_base],400000h
and [image_base_high],0
cmp byte [esi],80h
jne elf64_exe_base_ok
lods word [esi]
cmp ah,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
push edx
call get_qword_value
cmp [value_type],0
jne invalid_use_of_symbol
mov [image_base],eax
mov [image_base_high],edx
pop edx
elf64_exe_base_ok:
mov byte [edx+36h],38h
mov ebx,edi
mov ecx,38h shr 2
cmp [current_pass],0
je init_elf64_segments
imul ecx,[number_of_sections]
init_elf64_segments:
xor eax,eax
rep stos dword [edi]
and [number_of_sections],0
mov byte [ebx],1
mov word [ebx+30h],1000h
mov byte [ebx+4],111b
mov ebp,[image_base]
mov ecx,[image_base_high]
and dword [ebx+8],0
mov [ebx+10h],ebp
mov [ebx+10h+4],ecx
mov [ebx+18h],ebp
mov [ebx+18h+4],ecx
mov eax,edi
sub eax,[code_start]
add eax,ebp
adc ecx,0
mov [edx+18h],eax
mov [edx+18h+4],ecx
jmp elf_exe_addressing_setup
setup_elf_exe_labels_type:
mov eax,[code_start]
cmp byte [eax+10h],3
jne elf_exe_labels_type_ok
mov byte [ebx+9],2
test [format_flags],8
jz elf_exe_labels_type_ok
mov byte [ebx+9],4
elf_exe_labels_type_ok:
ret
elf_entry:
lods byte [esi]
cmp al,'('
jne invalid_argument
cmp byte [esi],'.'
je invalid_value
test [format_flags],8
jnz elf64_entry
call get_dword_value
mov edx,[code_start]
mov [edx+18h],eax
jmp instruction_assembled
elf64_entry:
call get_qword_value
mov ebx,[code_start]
mov [ebx+18h],eax
mov [ebx+1Ch],edx
jmp instruction_assembled
elf_segment:
bt [format_flags],0
jnc illegal_instruction
test [format_flags],8
jnz elf64_segment
call close_elf_segment
push eax
call create_addressing_space
call setup_elf_exe_labels_type
mov ebp,ebx
mov ebx,[number_of_sections]
shl ebx,5
add ebx,[code_start]
add ebx,34h
cmp ebx,[symbols_stream]
jb new_elf_segment
mov ebx,[symbols_stream]
sub ebx,20h
or [next_pass_needed],-1
new_elf_segment:
mov byte [ebx],1
and dword [ebx+18h],0
mov word [ebx+1Ch],1000h
elf_segment_flags:
cmp byte [esi],1Eh
je elf_segment_type
cmp byte [esi],19h
jne elf_segment_flags_ok
lods word [esi]
sub ah,28
jbe invalid_argument
cmp ah,1
je mark_elf_segment_flag
cmp ah,3
ja invalid_argument
xor ah,1
cmp ah,2
je mark_elf_segment_flag
inc ah
mark_elf_segment_flag:
test [ebx+18h],ah
jnz setting_already_specified
or [ebx+18h],ah
jmp elf_segment_flags
elf_segment_type:
cmp byte [ebx],1
jne setting_already_specified
lods word [esi]
mov ecx,[number_of_sections]
jecxz elf_segment_type_ok
mov edx,[code_start]
add edx,34h
scan_elf_segment_types:
cmp edx,[symbols_stream]
jae elf_segment_type_ok
cmp [edx],ah
je data_already_defined
add edx,20h
loop scan_elf_segment_types
elf_segment_type_ok:
mov [ebx],ah
mov word [ebx+1Ch],1
cmp ah,50h
jb elf_segment_flags
or dword [ebx],6474E500h
jmp elf_segment_flags
elf_segment_flags_ok:
pop edx
cmp byte [ebx],1
jne no_elf_segment_merging
cmp [merge_segment],0
jne merge_elf_segment
no_elf_segment_merging:
mov eax,edi
sub eax,[code_start]
mov [ebx+4],eax
and eax,0FFFh
add eax,edx
mov [ebx+8],eax
mov [ebx+0Ch],eax
xor edx,edx
elf_segment_addressing_setup:
xor cl,cl
not eax
not edx
not cl
add eax,1
adc edx,0
adc cl,0
add eax,edi
adc edx,0
adc cl,0
mov [ds:ebp],eax
mov [ds:ebp+4],edx
mov [ds:ebp+8],cl
inc [number_of_sections]
jmp instruction_assembled
merge_elf_segment:
xor ecx,ecx
xchg ecx,[merge_segment]
cmp ecx,-1
je merge_elf_header
mov eax,[ecx+8]
mov ecx,[ecx+4]
elf_segment_separated_base:
mov [ebx+8],eax
mov [ebx+0Ch],eax
mov [ebx+4],ecx
sub eax,ecx
add eax,edi
sub eax,[code_start]
xor edx,edx
jmp elf_segment_addressing_setup
merge_elf_header:
mov eax,[image_base]
xor ecx,ecx
jmp elf_segment_separated_base
close_elf_segment:
cmp [number_of_sections],0
jne finish_elf_segment
cmp edi,[symbols_stream]
jne first_elf_segment_ok
or [merge_segment],-1
mov eax,[image_base]
ret
first_elf_segment_ok:
and [merge_segment],0
inc [number_of_sections]
finish_elf_segment:
mov ebx,[number_of_sections]
dec ebx
shl ebx,5
add ebx,[code_start]
add ebx,34h
mov eax,edi
sub eax,[code_start]
sub eax,[ebx+4]
mov edx,edi
cmp edi,[undefined_data_end]
jne elf_segment_size_ok
cmp byte [ebx],1
jne elf_segment_size_ok
mov edi,[undefined_data_start]
elf_segment_size_ok:
mov [ebx+14h],eax
add eax,edi
sub eax,edx
mov [ebx+10h],eax
and [undefined_data_end],0
mov eax,[ebx+8]
cmp byte [ebx],1
je elf_segment_position_move_and_align
cmp [merge_segment],0
jne elf_segment_position_move
cmp byte [ebx],4
je elf_segment_position_ok
cmp byte [ebx],51h
je elf_segment_position_ok
mov [merge_segment],ebx
elf_segment_position_move:
add eax,[ebx+14h]
jmp elf_segment_position_ok
elf_segment_position_move_and_align:
add eax,[ebx+14h]
add eax,0FFFh
elf_segment_position_ok:
and eax,not 0FFFh
ret
elf64_segment:
call close_elf64_segment
push eax edx
call create_addressing_space
call setup_elf_exe_labels_type
mov ebp,ebx
mov ebx,[number_of_sections]
imul ebx,38h
add ebx,[code_start]
add ebx,40h
cmp ebx,[symbols_stream]
jb new_elf64_segment
or [next_pass_needed],-1
new_elf64_segment:
mov byte [ebx],1
and dword [ebx+4],0
mov word [ebx+30h],1000h
elf64_segment_flags:
cmp byte [esi],1Eh
je elf64_segment_type
cmp byte [esi],19h
jne elf64_segment_flags_ok
lods word [esi]
sub ah,28
jbe invalid_argument
cmp ah,1
je mark_elf64_segment_flag
cmp ah,3
ja invalid_argument
xor ah,1
cmp ah,2
je mark_elf64_segment_flag
inc ah
mark_elf64_segment_flag:
test [ebx+4],ah
jnz setting_already_specified
or [ebx+4],ah
jmp elf64_segment_flags
elf64_segment_type:
cmp byte [ebx],1
jne setting_already_specified
lods word [esi]
mov ecx,[number_of_sections]
jecxz elf64_segment_type_ok
mov edx,[code_start]
add edx,40h
scan_elf64_segment_types:
cmp edx,[symbols_stream]
jae elf64_segment_type_ok
cmp [edx],ah
je data_already_defined
add edx,38h
loop scan_elf64_segment_types
elf64_segment_type_ok:
mov [ebx],ah
mov word [ebx+30h],1
cmp ah,50h
jb elf64_segment_flags
or dword [ebx],6474E500h
jmp elf64_segment_flags
elf64_segment_flags_ok:
pop edx eax
cmp byte [ebx],1
jne no_elf64_segment_merging
cmp [merge_segment],0
jne merge_elf64_segment
no_elf64_segment_merging:
mov ecx,edi
sub ecx,[code_start]
mov [ebx+8],ecx
and ecx,0FFFh
add eax,ecx
adc edx,0
mov [ebx+10h],eax
mov [ebx+10h+4],edx
mov [ebx+18h],eax
mov [ebx+18h+4],edx
jmp elf_segment_addressing_setup
merge_elf64_segment:
xor ecx,ecx
xchg ecx,[merge_segment]
cmp ecx,-1
je merge_elf64_header
mov eax,[ecx+10h]
mov edx,[ecx+10h+4]
mov ecx,[ecx+8]
elf64_segment_separated_base:
mov [ebx+10h],eax
mov [ebx+10h+4],edx
mov [ebx+18h],eax
mov [ebx+18h+4],edx
mov [ebx+8],ecx
neg ecx
add ecx,edi
sub ecx,[code_start]
add eax,ecx
adc edx,0
jmp elf_segment_addressing_setup
merge_elf64_header:
mov eax,[image_base]
mov edx,[image_base_high]
xor ecx,ecx
jmp elf64_segment_separated_base
close_elf64_segment:
cmp [number_of_sections],0
jne finish_elf64_segment
cmp edi,[symbols_stream]
jne first_elf64_segment_ok
or [merge_segment],-1
mov eax,[image_base]
mov edx,[image_base_high]
ret
first_elf64_segment_ok:
and [merge_segment],0
inc [number_of_sections]
finish_elf64_segment:
mov ebx,[number_of_sections]
dec ebx
imul ebx,38h
add ebx,[code_start]
add ebx,40h
mov eax,edi
sub eax,[code_start]
sub eax,[ebx+8]
mov edx,edi
cmp edi,[undefined_data_end]
jne elf64_segment_size_ok
cmp byte [ebx],1
jne elf64_segment_size_ok
mov edi,[undefined_data_start]
elf64_segment_size_ok:
mov [ebx+28h],eax
add eax,edi
sub eax,edx
mov [ebx+20h],eax
and [undefined_data_end],0
mov eax,[ebx+10h]
mov edx,[ebx+10h+4]
cmp byte [ebx],1
je elf64_segment_position_move_and_align
cmp [merge_segment],0
jne elf64_segment_position_move
cmp byte [ebx],4
je elf64_segment_position_ok
cmp byte [ebx],51h
je elf64_segment_position_ok
mov [merge_segment],ebx
elf64_segment_position_move:
add eax,[ebx+28h]
adc edx,0
jmp elf64_segment_position_ok
elf64_segment_position_move_and_align:
add eax,[ebx+28h]
adc edx,0
add eax,0FFFh
adc edx,0
elf64_segment_position_ok:
and eax,not 0FFFh
ret
close_elf_exe:
test [format_flags],8
jnz close_elf64_exe
call close_elf_segment
mov edx,[code_start]
mov eax,[number_of_sections]
mov byte [edx+1Ch],34h
mov [edx+2Ch],ax
shl eax,5
add eax,edx
add eax,34h
cmp eax,[symbols_stream]
je elf_exe_ok
or [next_pass_needed],-1
elf_exe_ok:
ret
close_elf64_exe:
call close_elf64_segment
mov edx,[code_start]
mov eax,[number_of_sections]
mov byte [edx+20h],40h
mov [edx+38h],ax
imul eax,38h
add eax,edx
add eax,40h
cmp eax,[symbols_stream]
je elf64_exe_ok
or [next_pass_needed],-1
elf64_exe_ok:
ret
|