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
|
{
Copyright (c) 1998-2006 by Peter Vreman
Contains the base stuff for binary object file writers
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit ogbase;
{$i fpcdefs.inc}
interface
uses
{ common }
cutils,
cclasses,
{ targets }
systems,globtype,
{ outputwriters }
owbase,
{ assembler }
aasmbase,
cpuinfo;
type
TObjSection = class;
TObjData = class;
TExeSection = class;
TExeSymbol = class;
TExeOutput = class;
TObjRelocationType = (
{ Relocation to absolute address }
RELOC_ABSOLUTE,
{$ifdef x86_64}
{ 32bit Relocation to absolute address }
RELOC_ABSOLUTE32,
{ 64 bit coff only }
RELOC_RELATIVE_1,
RELOC_RELATIVE_2,
RELOC_RELATIVE_3,
RELOC_RELATIVE_4,
RELOC_RELATIVE_5,
{ PIC }
RELOC_GOTPCREL,
RELOC_PLT32,
{$endif x86_64}
{$ifdef i386}
{ PIC }
RELOC_GOTPC,
RELOC_GOT32,
RELOC_PLT32,
{$endif i386}
{$ifdef i8086}
RELOC_ABSOLUTE32,
RELOC_RELATIVE32,
RELOC_FARPTR,
RELOC_FARPTR_RELATIVEOFFSET,
RELOC_FARPTR48,
RELOC_FARPTR48_RELATIVEOFFSET,
RELOC_SEG,
RELOC_SEGREL,
RELOC_DGROUP,
RELOC_DGROUPREL,
RELOC_FARDATASEG,
RELOC_FARDATASEGREL,
{$endif i8086}
{$ifdef arm}
RELOC_RELATIVE_24,
RELOC_RELATIVE_CALL,
RELOC_RELATIVE_24_THUMB,
RELOC_RELATIVE_CALL_THUMB,
RELOC_GOT32,
{$endif arm}
{ Relative relocation }
RELOC_RELATIVE,
{ PECoff (Windows) RVA relocation }
RELOC_RVA,
{ PECoff (Windows) section relocation, required by DWARF2 debug info }
RELOC_SECREL32,
{ Generate a 0 value at the place of the relocation,
this is used to remove unused vtable entries }
RELOC_ZERO,
{ No relocation is needed. It is used in ARM object files.
Also internal linker use this reloc to make virtual (not real)
links to some sections }
RELOC_NONE,
{ Darwin relocation, using PAIR }
RELOC_PIC_PAIR,
{ Relative to GOT/gp }
RELOC_GOTOFF,
{ Untranslated target-specific value }
RELOC_RAW
);
{$if defined(x86_64)}
{ no special aliases for x86_64 }
{$elseif defined(i8086)}
const
RELOC_ABSOLUTE16 = RELOC_ABSOLUTE;
RELOC_RELATIVE16 = RELOC_RELATIVE;
{$else}
const
RELOC_ABSOLUTE32 = RELOC_ABSOLUTE;
{$endif}
const
{ stab types }
N_GSYM = $20;
N_STSYM = 38; { initialized const }
N_LCSYM = 40; { non initialized variable}
N_Function = $24; { function or const }
N_TextLine = $44;
N_DataLine = $46;
N_BssLine = $48;
N_RSYM = $40; { register variable }
N_LSYM = $80;
N_tsym = 160;
N_SourceFile = $64;
N_IncludeFile = $84;
N_BINCL = $82;
N_EINCL = $A2;
N_LBRAC = $C0;
N_EXCL = $C2;
N_RBRAC = $E0;
{ GNU extensions }
debuglinkname='.gnu_debuglink';
{ TObjRelocation.flags }
{ 'ftype' field contains platform-specific value }
rf_raw = 1;
{ relocation must be added to dynamic list }
rf_dynamic = 2;
{ relocation target is absent/irrelevant (e.g. R_ARM_V4BX) }
rf_nosymbol = 4;
type
TObjSectionOption = (
{ Has Data available in the file }
oso_Data,
{ Is loaded into memory }
oso_load,
{ Writable }
oso_write,
{ Contains executable instructions }
oso_executable,
{ Never discard section }
oso_keep,
{ Procedure Linkage Table specific treatment }
oso_plt,
{ Contains debug info and can be stripped }
oso_debug,
{ Contains only strings }
oso_strings,
{ Must be cloned when writing separate debug file }
oso_debug_copy,
{ Has relocations with explicit addends (ELF-specific) }
oso_rela_relocs,
{ Supports bss-like allocation of data, even though it is written in file (i.e. also has oso_Data) }
oso_sparse_data,
{ Section to support the resolution of multiple symbols with the same name }
oso_comdat
);
TObjSectionOptions = set of TObjSectionOption;
TObjSectionComdatSelection = (
{ Section is not a COMDAT section }
oscs_none,
{ Select any of the symbols }
oscs_any,
{ Select any symbol, but abort if they differ in size }
oscs_same_size,
{ Select any symbol, but abort if they differ in size or content }
oscs_exact_match,
{ Select the symbol only if the associated symbol is linked as well }
oscs_associative,
{ Select the largest symbol }
oscs_largest
);
{$ifdef i8086}
{ allow 32-bit sections on i8086. Useful for the dwarf debug info, as well
as to allow linking 32-bit obj modules. }
TObjSectionOfs = LongWord;
{$else i8086}
TObjSectionOfs = PUInt;
{$endif i8086}
TObjSectionGroup = class;
TObjSymbol = class(TFPHashObject)
public
bind : TAsmsymbind;
typ : TAsmsymtype;
{ Current assemble pass, used to detect duplicate labels }
pass : byte;
{ how the symbol is referenced (target-specific bitfield) }
refs : byte;
symidx : longint;
objsection : TObjSection;
offset,
size : TObjSectionOfs;
{ Used for external and common solving during linking }
exesymbol : TExeSymbol;
{ Darwin asm is using indirect symbols resolving }
indsymbol : TObjSymbol;
{ Used by the OMF object format and its complicated relocation records }
group: TObjSectionGroup;
{$ifdef ARM}
ThumbFunc : boolean;
{$endif ARM}
constructor create(AList:TFPHashObjectList;const AName:string);
function address:qword;
procedure SetAddress(apass:byte;aobjsec:TObjSection;abind:TAsmsymbind;atyp:Tasmsymtype);
function ObjData: TObjData;
{ string representation for the linker map file }
function AddressStr(AImageBase: qword): string;virtual;
end;
TObjSymbolClass = class of TObjSymbol;
{ Stabs is common for all targets }
TObjStabEntry=packed record
strpos : longint;
ntype : byte;
nother : byte;
ndesc : word;
nvalue : longint;
end;
PObjStabEntry=^TObjStabEntry;
TObjRelocation = class
private
function GetType:TObjRelocationType;
procedure SetType(v:TObjRelocationType);
public
DataOffset,
orgsize : TObjSectionOfs; { COFF: original size of the symbol to relocate }
{ ELF: explicit addend }
symbol : TObjSymbol;
objsection : TObjSection; { only used if symbol=nil }
group : TObjSectionGroup; { only used if symbol=nil and objsection=nil }
ftype : byte;
size : byte;
flags : byte;
constructor CreateSymbol(ADataOffset:TObjSectionOfs;s:TObjSymbol;Atyp:TObjRelocationType);
constructor CreateSection(ADataOffset:TObjSectionOfs;aobjsec:TObjSection;Atyp:TObjRelocationType);
constructor CreateGroup(ADataOffset:TObjSectionOfs;grp:TObjSectionGroup;Atyp:TObjRelocationType);
constructor CreateRaw(ADataOffset:TObjSectionOfs;s:TObjSymbol;ARawType:byte);
function TargetName:TSymStr;
property typ: TObjRelocationType read GetType write SetType;
end;
TObjSection = class(TFPHashObject)
private
FData : TDynamicArray;
FSecOptions : TObjSectionOptions;
FComdatSelection : TObjSectionComdatSelection;
FCachedFullName : pshortstring;
FSizeLimit : TObjSectionOfs;
procedure SetSecOptions(Aoptions:TObjSectionOptions);
procedure SectionTooLargeError;
public
ObjData : TObjData;
index : longword; { index of section in section headers }
SecSymIdx : longint; { index for the section in symtab }
SecAlign : longint; { alignment of the section }
{ section Data }
Size,
DataPos : TObjSectionOfs;
MemPos : qword;
Group : TObjSectionGroup;
AssociativeSection : TObjSection;
ComdatSelection : TObjSectionComdatSelection;
DataAlignBytes : shortint;
{ Relocations (=references) to other sections }
ObjRelocations : TFPObjectList;
{ executable linking }
ExeSection : TExeSection;
USed : Boolean;
VTRefList : TFPObjectList;
constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);virtual;
destructor destroy;override;
function write(const d;l:TObjSectionOfs):TObjSectionOfs;
{ writes string plus zero byte }
function writestr(const s:string):TObjSectionOfs;
function WriteZeros(l:longword):TObjSectionOfs;
{ writes content of s without null termination }
function WriteBytes(const s:string):TObjSectionOfs;
procedure writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);virtual;
function setmempos(mpos:qword):qword;
procedure setDatapos(var dpos:TObjSectionOfs);
procedure alloc(l:TObjSectionOfs);
procedure addsymReloc(ofs:TObjSectionOfs;p:TObjSymbol;Reloctype:TObjRelocationType);
procedure addsectionReloc(ofs:TObjSectionOfs;aobjsec:TObjSection;Reloctype:TObjRelocationType);
procedure addrawReloc(ofs:TObjSectionOfs;p:TObjSymbol;RawReloctype:byte);
procedure ReleaseData;
function FullName:string;
{ string representation for the linker map file }
function MemPosStr(AImageBase: qword): string;virtual;
property Data:TDynamicArray read FData;
property SecOptions:TObjSectionOptions read FSecOptions write SetSecOptions;
property SizeLimit:TObjSectionOfs read FSizeLimit write FSizeLimit;
end;
TObjSectionClass = class of TObjSection;
TObjSectionGroup = class(TFPHashObject)
public
index: longword; { index of group in group headers }
members: array of TObjSection;
iscomdat: boolean;
end;
TObjSectionGroupClass = class of TObjSectionGroup;
TString80 = string[80];
TObjSymbolList = class(TFPHashObjectList)
public
Owner: TObjData;
end;
{$ifdef i8086}
{ on i8086 we use a longint, to support 32-bit relocations as well (e.g.
for allowing 386+ instructions with 32-bit addresses in inline asm code) }
TRelocDataInt = longint;
{$else i8086}
TRelocDataInt = aint;
{$endif i8086}
TObjData = class(TLinkedListItem)
private
FCurrObjSec : TObjSection;
FObjSectionList : TFPHashObjectList;
FCObjSymbol : TObjSymbolClass;
FCObjSection : TObjSectionClass;
FCObjSectionGroup: TObjSectionGroupClass;
{ Symbols that will be defined in this object file }
FObjSymbolList : TObjSymbolList;
FCachedAsmSymbolList : TFPObjectList;
{ Special info sections that are written to during object generation }
FStabsObjSec,
FStabStrObjSec : TObjSection;
FGroupsList : TFPHashObjectList;
FCPUType : tcputype;
procedure section_reset(p:TObject;arg:pointer);
procedure section_afteralloc(p:TObject;arg:pointer);
procedure section_afterwrite(p:TObject;arg:pointer);
protected
FName : TString80;
property CObjSection:TObjSectionClass read FCObjSection write FCObjSection;
property CObjSectionGroup: TObjSectionGroupClass read FCObjSectionGroup write FCObjSectionGroup;
public
CurrPass : byte;
ExecStack : boolean;
{$ifdef ARM}
ThumbFunc : boolean;
{$endif ARM}
constructor create(const n:string);virtual;
destructor destroy;override;
{ Sections }
function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;virtual;abstract;
function sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;virtual;
function sectiontype2align(atype:TAsmSectiontype):longint;virtual;
function createsection(atype:TAsmSectionType;const aname:string='';aorder:TAsmSectionOrder=secorder_default):TObjSection;virtual;
function createsection(const aname:string;aalign:longint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean=true):TObjSection;virtual;
function createsectiongroup(const aname:string):TObjSectionGroup;
procedure CreateDebugSections;virtual;
function findsection(const aname:string):TObjSection;
procedure setsection(asec:TObjSection);
{ Symbols }
function createsymbol(const aname:string):TObjSymbol;
function symboldefine(asmsym:TAsmSymbol):TObjSymbol;
function symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
function symbolref(asmsym:TAsmSymbol):TObjSymbol;
function symbolref(const aname:string):TObjSymbol;
procedure ResetCachedAsmSymbols;
{ Allocation }
procedure alloc(len:aword);
procedure allocalign(len:longint);
procedure writebytes(const Data;len:aword);
procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);virtual;abstract;
procedure beforealloc;virtual;
procedure beforewrite;virtual;
procedure afteralloc;virtual;
procedure afterwrite;virtual;
procedure resetsections;
procedure layoutsections(var datapos:TObjSectionOfs);
property Name:TString80 read FName;
property CurrObjSec:TObjSection read FCurrObjSec;
property ObjSymbolList:TObjSymbolList read FObjSymbolList;
property ObjSectionList:TFPHashObjectList read FObjSectionList;
property GroupsList:TFPHashObjectList read FGroupsList;
property StabsSec:TObjSection read FStabsObjSec write FStabsObjSec;
property StabStrSec:TObjSection read FStabStrObjSec write FStabStrObjSec;
property CObjSymbol: TObjSymbolClass read FCObjSymbol write FCObjSymbol;
{ Current CPU type for the internal asm writer.
Instructions, not supported by the given CPU should produce an error.
A value of 'cpu_none' means no restrictions (all instructions should be accepted) }
property CPUType : tcputype read FCPUType write FCPUType;
end;
TObjDataClass = class of TObjData;
TObjOutput = class
private
FCObjData : TObjDataClass;
protected
{ writer }
FWriter : TObjectwriter;
function writeData(Data:TObjData):boolean;virtual;abstract;
property CObjData : TObjDataClass read FCObjData write FCObjData;
procedure WriteSectionContent(Data:TObjData);
public
constructor create(AWriter:TObjectWriter);virtual;
destructor destroy;override;
function newObjData(const n:string):TObjData;
function startObjectfile(const fn:string):boolean;
function writeobjectfile(Data:TObjData):boolean;
procedure exportsymbol(p:TObjSymbol);
property Writer:TObjectWriter read FWriter;
end;
TObjOutputClass=class of TObjOutput;
TObjInput = class
private
FCObjData : TObjDataClass;
protected
{ reader }
FReader : TObjectReader;
InputFileName : string;
property CObjData : TObjDataClass read FCObjData write FCObjData;
procedure ReadSectionContent(Data:TObjData);
public
constructor create;virtual;
function ReadObjData(AReader:TObjectreader;out Data:TObjData):boolean;virtual;abstract;
class function CanReadObjData(AReader:TObjectreader):boolean;virtual;
procedure inputerror(const s : string);
end;
TObjInputClass=class of TObjInput;
TVTableEntry=record
ObjRelocation : TObjRelocation;
orgreloctype,
orgrelocflags : byte;
Enabled,
Used : Boolean;
end;
PVTableEntry=^TVTableEntry;
TExeVTable = class
private
procedure CheckIdx(VTableIdx:longint);
public
ExeSymbol : TExeSymbol;
EntryCnt : Longint;
EntryArray : PVTableEntry;
Consolidated : Boolean;
ChildList : TFPObjectList;
constructor Create(AExeSymbol:TExeSymbol);
destructor Destroy;override;
procedure AddChild(vt:TExeVTable);
procedure AddEntry(VTableIdx:Longint);
procedure SetVTableSize(ASize:longint);
function VTableRef(VTableIdx:Longint):TObjRelocation;
end;
TSymbolState = (
symstate_undefined,
symstate_undefweak, // undefined but has only weak refs - don't complain
symstate_defined,
symstate_defweak,
symstate_common,
symstate_dynamic // a matching symbol has been seen in .so
);
TExeSymbol = class(TFPHashObject)
ObjSymbol : TObjSymbol;
State : TSymbolState;
used : boolean;
{ Used for vmt references optimization }
VTable : TExeVTable;
{ fields for ELF linking }
gotoffset : aword;
dynindex : aword;
{ A thunk used to redirect some references to symbol (like absolute
jumps/calls to PIC code).
This probably is also needed for ARM/Thumb interworking and alike.
TODO: consider reusing objsymbol.indsymbol for this purpose }
{$ifdef mips}
stubsymbol : TObjSymbol;
{$endif mips}
end;
TExeSection = class(TFPHashObject)
private
FSecSymIdx : longint;
FObjSectionList : TFPObjectList;
public
Size,
DataPos,
MemPos : qword;
SecAlign : longint;
Disabled : boolean;
SecOptions : TObjSectionOptions;
constructor create(AList:TFPHashObjectList;const AName:string);virtual;
destructor destroy;override;
procedure AddObjSection(objsec:TObjSection;ignoreprops:boolean=false);virtual;
property ObjSectionList:TFPObjectList read FObjSectionList;
property SecSymIdx:longint read FSecSymIdx write FSecSymIdx;
end;
TExeSectionClass=class of TExeSection;
TlibKind = (lkArchive,lkObject,lkGroup);
TStaticLibrary = class(TObject)
private
FName : TCmdStr;
FPayload : TObject; { lkArchive: TObjectReader }
{ lkObject: TObjData }
{ lkGroup: TFPObjectList }
FObjInputClass : TObjInputClass;
FKind: TlibKind;
FAsNeeded : Boolean;
function GetArReader:TObjectReader;
function GetGroupMembers:TFPObjectList;
function GetObjData:TObjData;
public
constructor create(const AName:TCmdStr;AReader:TObjectReader;AObjInputClass:TObjInputClass);
constructor create_object(AObjData:TObjData);
constructor create_group;
destructor destroy;override;
property ArReader:TObjectReader read GetArReader;
property ObjInputClass:TObjInputClass read FObjInputClass;
property GroupMembers:TFPObjectList read GetGroupMembers;
property ObjData:TObjData read GetObjData;
property AsNeeded:Boolean read FAsNeeded write FAsNeeded;
property Kind:TLibKind read FKind;
end;
TImportLibrary = class(TFPHashObject)
private
FImportSymbolList : TFPHashObjectList;
public
constructor create(AList:TFPHashObjectList;const AName:string);
destructor destroy;override;
property ImportSymbolList:TFPHashObjectList read FImportSymbolList;
end;
TImportSymbol = class(TFPHashObject)
private
FOrdNr : longint;
FIsVar : boolean;
FMangledName : string;
FCachedExeSymbol: TExeSymbol;
public
constructor create(AList:TFPHashObjectList;const AName,AMangledName:string;AOrdNr:longint;AIsVar:boolean);
property OrdNr: longint read FOrdNr;
property MangledName: string read FMangledName;
property IsVar: boolean read FIsVar;
property CachedExeSymbol: TExeSymbol read FCachedExeSymbol write FCachedExeSymbol;
end;
TExeWriteMode = (ewm_exefull,ewm_dbgonly,ewm_exeonly);
TExeOutput = class
private
{ ExeSectionList }
FCObjSymbol : TObjSymbolClass;
FCObjData : TObjDataClass;
FCExeSection : TExeSectionClass;
FCurrExeSec : TExeSection;
FExeSectionList : TFPHashObjectList;
Fzeronr : longint;
Fvaluesnr : longint;
{ Symbols }
FExeSymbolList : TFPHashObjectList;
FUnresolvedExeSymbols : TFPObjectList;
FExternalObjSymbols,
FCommonObjSymbols : TFPObjectList;
FProvidedObjSymbols : TFPObjectList;
FIndirectObjSymbols : TFPObjectList;
FEntryName : string;
FExeVTableList : TFPObjectList;
{ Objects }
FObjDataList : TFPObjectList;
{ Position calculation }
FImageBase : aword;
FCurrMemPos : qword;
procedure SetCurrMemPos(const AValue: qword);
protected
{ writer }
FExeWriteMode : TExeWriteMode;
FWriter : TObjectwriter;
commonObjSection : TObjSection;
internalObjData : TObjData;
EntrySym : TObjSymbol;
SectionDataAlign,
SectionMemAlign : aword;
ComdatGroups : TFPHashList;
FixedSectionAlign : boolean;
AllowUndefinedSymbols : boolean;
function writeData:boolean;virtual;abstract;
property CExeSection:TExeSectionClass read FCExeSection write FCExeSection;
property CObjData:TObjDataClass read FCObjData write FCObjData;
property CObjSymbol:TObjSymbolClass read FCObjSymbol write FCObjSymbol;
procedure Order_ObjSectionList(ObjSectionList : TFPObjectList; const aPattern:string);virtual;
procedure WriteExeSectionContent;
procedure DoRelocationFixup(objsec:TObjSection);virtual;abstract;
function MemAlign(exesec: TExeSection): longword;
function DataAlign(exesec: TExeSection): longword;
procedure ReplaceExeSectionList(newlist: TFPList);
public
CurrDataPos : aword;
MaxMemPos : qword;
IsSharedLibrary : boolean;
ExecStack : boolean;
constructor create;virtual;
destructor destroy;override;
function FindExeSection(const aname:string):TExeSection;
procedure AddObjData(ObjData:TObjData);
procedure Load_Start;virtual;
procedure Load_EntryName(const aname:string);virtual;
procedure Load_Symbol(const aname:string);virtual;
procedure Load_ProvideSymbol(const aname:string);virtual;
procedure Load_IsSharedLibrary;
procedure Load_ImageBase(const avalue:string);
procedure Load_DynamicObject(ObjData:TObjData;asneeded:boolean);virtual;
procedure Order_Start;virtual;
procedure Order_End;virtual;
procedure Order_ExeSection(const aname:string);virtual;
procedure Order_Align(const avalue:string);virtual;
procedure Order_Zeros(const avalue:string);virtual;
procedure Order_Values(bytesize : aword; const avalue:string);virtual;
procedure Order_Symbol(const aname:string);virtual;
procedure Order_ProvideSymbol(const aname:string);virtual;
procedure Order_EndExeSection;virtual;
procedure Order_ObjSection(const aname:string);virtual;
procedure MemPos_Start;virtual;
procedure MemPos_Header;virtual;
procedure MemPos_ExeSection(exesec:TExeSection);
procedure MemPos_ExeSection(const aname:string);virtual;
procedure MemPos_EndExeSection;virtual;
procedure DataPos_Start;virtual;
procedure DataPos_Header;virtual;
procedure DataPos_ExeSection(exesec:TExeSection);
procedure DataPos_ExeSection(const aname:string);virtual;
procedure DataPos_EndExeSection;virtual;
procedure DataPos_Symbols;virtual;
procedure BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
procedure PackUnresolvedExeSymbols(const s:string);
procedure ResolveSymbols(StaticLibraryList:TFPObjectList);
procedure PrintMemoryMap;
procedure FixupSymbols;
procedure FixupRelocations;virtual;
procedure RemoveUnusedExeSymbols;
procedure MergeStabs;
procedure MarkEmptySections;
procedure RemoveUnreferencedSections;
procedure RemoveDisabledSections;
procedure RemoveDebugInfo;
procedure MarkTargetSpecificSections(WorkList:TFPObjectList);virtual;
procedure AfterUnusedSectionRemoval;virtual;
procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);virtual;
procedure GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
function WriteExeFile(const fn:string):boolean;
procedure ParseScript (linkscript:TCmdStrList); virtual;
property Writer:TObjectWriter read FWriter;
property ExeSectionList:TFPHashObjectList read FExeSectionList;
property ObjDataList:TFPObjectList read FObjDataList;
property ExeSymbolList:TFPHashObjectList read FExeSymbolList;
property UnresolvedExeSymbols:TFPObjectList read FUnresolvedExeSymbols;
property ExternalObjSymbols:TFPObjectList read FExternalObjSymbols;
property CommonObjSymbols:TFPObjectList read FCommonObjSymbols;
property IndirectObjSymbols:TFPObjectList read FIndirectObjSymbols;
property ExeVTableList:TFPObjectList read FExeVTableList;
property EntryName:string read FEntryName write FEntryName;
property ImageBase:aword read FImageBase write FImageBase;
property CurrExeSec:TExeSection read FCurrExeSec;
property ExeWriteMode:TExeWriteMode read FExeWriteMode write FExeWriteMode;
property CurrMemPos:qword read FCurrMemPos write SetCurrMemPos;
end;
TExeOutputClass=class of TExeOutput;
const
SectionDataMaxGrow = 4096;
var
exeoutput : TExeOutput;
function align_aword(v:aword;a:longword):aword;
function align_qword(v:qword;a:longword):qword;
function align_objsecofs(v:TObjSectionOfs;a:longword):TObjSectionOfs;
implementation
uses
SysUtils,
globals,verbose,ogmap;
{$ifdef MEMDEBUG}
var
memobjsymbols,
memobjsections : TMemDebug;
{$endif MEMDEBUG}
{*****************************************************************************
Helpers
*****************************************************************************}
function align_aword(v:aword;a:longword):aword;
begin
if a<=1 then
result:=v
else
result:=((v+a-1) div a) * a;
end;
function align_qword(v:qword;a:longword):qword;
begin
if a<=1 then
result:=v
else
result:=((v+a-1) div a) * a;
end;
function align_objsecofs(v:TObjSectionOfs;a:longword):TObjSectionOfs;
begin
if a<=1 then
result:=v
else
result:=((v+a-1) div a) * a;
end;
procedure MaybeSwapStab(var v:TObjStabEntry);
begin
if source_info.endian<>target_info.endian then
begin
v.strpos:=SwapEndian(v.strpos);
v.nvalue:=SwapEndian(v.nvalue);
v.ndesc:=SwapEndian(v.ndesc);
end;
end;
{*****************************************************************************
TObjSymbol
*****************************************************************************}
constructor TObjSymbol.create(AList:TFPHashObjectList;const AName:string);
begin
inherited create(AList,AName);
bind:=AB_EXTERNAL;
typ:=AT_NONE;
symidx:=-1;
size:=0;
offset:=0;
objsection:=nil;
end;
function TObjSymbol.address:qword;
begin
if assigned(objsection) then
result:=offset+objsection.mempos
else
result:=0;
end;
procedure TObjSymbol.SetAddress(apass:byte;aobjsec:TObjSection;abind:TAsmsymbind;atyp:Tasmsymtype);
begin
if not(abind in [AB_GLOBAL,AB_LOCAL,AB_COMMON,AB_IMPORT]) then
internalerror(200603016);
if not assigned(aobjsec) then
internalerror(200603017);
if (bind in [AB_EXTERNAL,AB_LAZY]) or
{ Put all COMMON to GLOBAL in step 3 of
TExeOutput.ResolveSymbols }
((abind=AB_GLOBAL) and (bind=AB_COMMON)) then
begin
{ Do not change the AB_TYPE of common symbols yet }
{ This will be done in FixupSymbols }
if (pass<>0) or (bind<>AB_COMMON) then
bind:=abind;
typ:=atyp;
end
else
begin
if pass=apass then
begin
Message1(asmw_e_duplicate_label,name);
exit;
end;
end;
pass:=apass;
{ Code can never grow after a pass }
if assigned(objsection) and
(objsection=aobjsec) and
(aobjsec.size>offset) then
internalerror(200603014);
objsection:=aobjsec;
offset:=aobjsec.size;
end;
function TObjSymbol.ObjData: TObjData;
begin
result:=(OwnerList as TObjSymbolList).Owner;
end;
function TObjSymbol.AddressStr(AImageBase: qword): string;
begin
Result:='0x'+HexStr(address+Aimagebase,sizeof(pint)*2);
end;
{****************************************************************************
TObjRelocation
****************************************************************************}
constructor TObjRelocation.CreateSymbol(ADataOffset:TObjSectionOfs;s:TObjSymbol;Atyp:TObjRelocationType);
begin
if not assigned(s) then
internalerror(200603034);
DataOffset:=ADataOffset;
Symbol:=s;
OrgSize:=0;
Group:=nil;
ObjSection:=nil;
ftype:=ord(Atyp);
end;
constructor TObjRelocation.CreateSection(ADataOffset:TObjSectionOfs;aobjsec:TObjSection;Atyp:TObjRelocationType);
begin
if not assigned(aobjsec) then
internalerror(200603036);
DataOffset:=ADataOffset;
Symbol:=nil;
OrgSize:=0;
Group:=nil;
ObjSection:=aobjsec;
ftype:=ord(Atyp);
end;
constructor TObjRelocation.CreateGroup(ADataOffset:TObjSectionOfs;grp:TObjSectionGroup;Atyp:TObjRelocationType);
begin
if not assigned(grp) then
internalerror(2015111201);
DataOffset:=ADataOffset;
Symbol:=nil;
ObjSection:=nil;
OrgSize:=0;
Group:=grp;
ftype:=ord(Atyp);
end;
constructor TObjRelocation.CreateRaw(ADataOffset:TObjSectionOfs;s:TObjSymbol;ARawType:byte);
begin
{ nil symbol is allowed here }
DataOffset:=ADataOffset;
Symbol:=s;
ObjSection:=nil;
Group:=nil;
orgsize:=0;
ftype:=ARawType;
flags:=rf_raw;
end;
function TObjRelocation.GetType:TObjRelocationType;
begin
if (flags and rf_raw)=0 then
result:=TObjRelocationType(ftype)
else
result:=RELOC_RAW;
end;
procedure TObjRelocation.SetType(v:TObjRelocationType);
begin
ftype:=ord(v);
flags:=flags and (not rf_raw);
end;
function TObjRelocation.TargetName:TSymStr;
begin
if assigned(symbol) then
if symbol.typ=AT_SECTION then
result:=symbol.objsection.name
else
result:=symbol.Name
else
result:=objsection.Name;
end;
{****************************************************************************
TObjSection
****************************************************************************}
constructor TObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);
begin
inherited Create(AList,Aname);
{ Data }
Size:=0;
Datapos:=0;
mempos:=0;
FData:=Nil;
{$ifdef i8086}
FSizeLimit:=high(word);
{$else i8086}
FSizeLimit:=high(TObjSectionOfs);
{$endif i8086}
{ Setting the secoptions allocates Data if needed }
secoptions:=Aoptions;
secalign:=Aalign;
secsymidx:=0;
{ relocation }
ObjRelocations:=TFPObjectList.Create(true);
VTRefList:=TFPObjectList.Create(false);
end;
destructor TObjSection.destroy;
begin
if assigned(Data) then
Data.Free;
stringdispose(FCachedFullName);
ObjRelocations.Free;
VTRefList.Free;
inherited destroy;
end;
procedure TObjSection.SetSecOptions(Aoptions:TObjSectionOptions);
begin
FSecOptions:=FSecOptions+AOptions;
if (oso_Data in secoptions) and
not assigned(FData) then
FData:=TDynamicArray.Create(SectionDataMaxGrow);
end;
procedure TObjSection.SectionTooLargeError;
begin
if oso_executable in SecOptions then
Message(asmw_f_code_segment_too_large)
else
Message(asmw_f_data_segment_too_large);
end;
function TObjSection.write(const d;l:TObjSectionOfs):TObjSectionOfs;
begin
result:=size;
if assigned(Data) then
begin
if Size<>Data.size then
internalerror(200602281);
{$ifndef cpu64bitalu}
if (qword(size)+l)>SizeLimit then
SectionTooLargeError;
{$endif}
Data.write(d,l);
inc(Size,l);
end
else
internalerror(200602289);
end;
function TObjSection.writestr(const s:string):TObjSectionOfs;
var
b: byte;
begin
result:=Write(s[1],length(s));
b:=0;
Write(b,1);
end;
function TObjSection.WriteBytes(const s:string):TObjSectionOfs;
begin
result:=Write(s[1],length(s));
end;
function TObjSection.WriteZeros(l:longword):TObjSectionOfs;
var
empty : array[0..1023] of byte;
begin
result:=Size;
if l>sizeof(empty) then
begin
fillchar(empty,sizeof(empty),0);
while l>sizeof(empty) do
begin
Write(empty,sizeof(empty));
Dec(l,sizeof(empty));
end;
if l>0 then
Write(empty,l);
end
else if l>0 then
begin
fillchar(empty,l,0);
Write(empty,l);
end;
end;
{ Writes relocation to (section+offset) without need to have a symbol at that location.
Not an abstract method because not every backend needs this functionality. }
procedure TObjSection.writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);
begin
InternalError(2012081501);
end;
procedure TObjSection.setDatapos(var dpos:TObjSectionOfs);
begin
if oso_Data in secoptions then
begin
{ get aligned Datapos }
Datapos:=align_aword(dpos,secalign);
Dataalignbytes:=Datapos-dpos;
{ return updated Datapos }
dpos:=Datapos+size;
end
else
Datapos:=dpos;
end;
function TObjSection.setmempos(mpos:qword):qword;
begin
mempos:=align_qword(mpos,secalign);
{ return updated mempos }
result:=mempos+size;
end;
procedure TObjSection.alloc(l:TObjSectionOfs);
begin
{$ifndef cpu64bitalu}
if (qword(size)+l)>SizeLimit then
SectionTooLargeError;
{$endif}
if oso_sparse_data in SecOptions then
WriteZeros(l)
else
inc(size,l);
end;
procedure TObjSection.addsymReloc(ofs:TObjSectionOfs;p:TObjSymbol;Reloctype:TObjRelocationType);
begin
ObjRelocations.Add(TObjRelocation.CreateSymbol(ofs,p,reloctype));
end;
procedure TObjSection.addsectionReloc(ofs:TObjSectionOfs;aobjsec:TObjSection;Reloctype:TObjRelocationType);
begin
ObjRelocations.Add(TObjRelocation.CreateSection(ofs,aobjsec,reloctype));
end;
procedure TObjSection.addrawReloc(ofs:TObjSectionOfs;p:TObjSymbol;RawReloctype:byte);
begin
ObjRelocations.Add(TObjRelocation.CreateRaw(ofs,p,RawReloctype));
end;
procedure TObjSection.ReleaseData;
begin
if assigned(FData) then
begin
FData.free;
FData:=nil;
end;
ObjRelocations.free;
ObjRelocations:=nil;
if assigned(FCachedFullName) then
begin
stringdispose(FCachedFullName);
FCachedFullName:=nil;
end;
end;
function TObjSection.FullName:string;
begin
if not assigned(FCachedFullName) then
begin
if assigned(ObjData) then
FCachedFullName:=stringdup(ObjData.Name+'('+Name+')')
else
FCachedFullName:=stringdup(Name);
end;
result:=FCachedFullName^;
end;
function TObjSection.MemPosStr(AImageBase: qword): string;
begin
result:='0x'+HexStr(mempos+AImageBase,sizeof(pint)*2);
end;
{****************************************************************************
TObjData
****************************************************************************}
constructor TObjData.create(const n:string);
begin
inherited create;
FName:=ExtractFileName(n);
FObjSectionList:=TFPHashObjectList.Create(true);
FStabsObjSec:=nil;
FStabStrObjSec:=nil;
{ symbols }
FCObjSymbol:=TObjSymbol;
FObjSymbolList:=TObjSymbolList.Create(true);
FObjSymbolList.Owner:=Self;
FCachedAsmSymbolList:=TFPObjectList.Create(false);
{ section class type for creating of new sections }
FCObjSection:=TObjSection;
FCObjSectionGroup:=TObjSectionGroup;
{$ifdef ARM}
ThumbFunc:=false;
{$endif ARM}
end;
destructor TObjData.destroy;
begin
{ Symbols }
{$ifdef MEMDEBUG}
MemObjSymbols.Start;
{$endif}
ResetCachedAsmSymbols;
FCachedAsmSymbolList.free;
FObjSymbolList.free;
{$ifdef MEMDEBUG}
MemObjSymbols.Stop;
{$endif}
GroupsList.free;
{ Sections }
{$ifdef MEMDEBUG}
MemObjSections.Start;
{$endif}
FObjSectionList.free;
{$ifdef MEMDEBUG}
MemObjSections.Stop;
{$endif}
inherited destroy;
end;
function TObjData.sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;
const
secoptions : array[TAsmSectiontype] of TObjSectionOptions = ([],
{user} [oso_Data,oso_load,oso_write],
{code} [oso_Data,oso_load,oso_executable],
{Data} [oso_Data,oso_load,oso_write],
{ Readonly data with relocations must be initially writable for some targets.
Moreover, e.g. for ELF it depends on whether the executable is linked statically or
dynamically. Here we declare it writable, target-specific descendants must provide
further handling. }
{roData} [oso_Data,oso_load,oso_write],
{roData_norel} [oso_Data,oso_load],
{bss} [oso_load,oso_write],
{threadvar} [oso_load,oso_write],
{pdata} [oso_data,oso_load],
{stub} [oso_Data,oso_load,oso_executable],
{data_nonlazy} [oso_Data,oso_load,oso_write],
{data_lazy} [oso_Data,oso_load,oso_write],
{init_func} [oso_Data,oso_load],
{term_func} [oso_Data,oso_load],
{stab} [oso_Data,oso_debug],
{stabstr} [oso_Data,oso_strings,oso_debug],
{iData2} [oso_Data,oso_load,oso_write],
{iData4} [oso_Data,oso_load,oso_write],
{iData5} [oso_Data,oso_load,oso_write],
{iData6} [oso_Data,oso_load,oso_write],
{iData7} [oso_Data,oso_load,oso_write],
{eData} [oso_Data,oso_load],
{eh_frame} [oso_Data,oso_load],
{debug_frame} [oso_Data,oso_debug],
{debug_info} [oso_Data,oso_debug],
{debug_line} [oso_Data,oso_debug],
{debug_abbrev} [oso_Data,oso_debug],
{debug_aranges} [oso_Data,oso_debug],
{debug_ranges} [oso_Data,oso_debug],
{fpc} [oso_Data,oso_load,oso_write],
{toc} [oso_Data,oso_load],
{init} [oso_Data,oso_load,oso_executable],
{fini} [oso_Data,oso_load,oso_executable],
{objc_class} [oso_data,oso_load],
{objc_meta_class} [oso_data,oso_load],
{objc_cat_cls_meth} [oso_data,oso_load],
{objc_cat_inst_meth} [oso_data,oso_load],
{objc_protocol} [oso_data,oso_load],
{objc_string_object} [oso_data,oso_load],
{objc_cls_meth} [oso_data,oso_load],
{objc_inst_meth} [oso_data,oso_load],
{objc_cls_refs} [oso_data,oso_load],
{objc_message_refs} [oso_data,oso_load],
{objc_symbols} [oso_data,oso_load],
{objc_category} [oso_data,oso_load],
{objc_class_vars} [oso_data,oso_load],
{objc_instance_vars} [oso_data,oso_load],
{objc_module_info} [oso_data,oso_load],
{objc_class_names} [oso_data,oso_load],
{objc_meth_var_types} [oso_data,oso_load],
{objc_meth_var_names} [oso_data,oso_load],
{objc_selector_strs} [oso_data,oso_load],
{objc_protocol_ext} [oso_data,oso_load],
{objc_class_ext} [oso_data,oso_load],
{objc_property} [oso_data,oso_load],
{objc_image_info} [oso_data,oso_load],
{objc_cstring_object} [oso_data,oso_load],
{objc_sel_fixup} [oso_data,oso_load],
{sec_objc_data} [oso_data,oso_load],
{sec_objc_const} [oso_data,oso_load],
{sec_objc_sup_refs} [oso_data,oso_load],
{sec_data_coalesced} [oso_data,oso_load],
{sec_objc_classlist} [oso_data,oso_load],
{sec_objc_nlclasslist} [oso_data,oso_load],
{sec_objc_catlist} [oso_data,oso_load],
{sec_objc_nlcatlist} [oso_data,oso_load],
{sec_objc_protolist'} [oso_data,oso_load],
{stack} [oso_load,oso_write],
{heap} [oso_load,oso_write]
);
begin
result:=secoptions[atype];
end;
function TObjData.sectiontype2align(atype:TAsmSectiontype):longint;
begin
case atype of
sec_stabstr,sec_debug_info,sec_debug_line,sec_debug_abbrev,sec_debug_aranges,sec_debug_ranges:
result:=1;
sec_code,
sec_bss,
sec_data:
result:=16;
{ For idata (at least idata2) it must be 4 bytes, because
an entry is always (also in win64) 20 bytes and aligning
on 8 bytes will insert 4 bytes between the entries resulting
in a corrupt idata section.
Same story with .pdata, it has 4-byte elements which should
be packed without gaps. }
sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7,sec_pdata:
result:=4;
else
result:=sizeof(pint);
end;
end;
function TObjData.createsection(atype:TAsmSectionType;const aname:string;aorder:TAsmSectionOrder):TObjSection;
begin
result:=createsection(sectionname(atype,aname,aorder),sectiontype2align(atype),sectiontype2options(atype));
end;
function TObjData.createsection(const aname:string;aalign:longint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean):TObjSection;
begin
if DiscardDuplicate then
result:=TObjSection(FObjSectionList.Find(aname))
else
result:=nil;
if not assigned(result) then
begin
result:=CObjSection.create(FObjSectionList,aname,aalign,aoptions);
result.ObjData:=self;
end;
FCurrObjSec:=result;
end;
function TObjData.CreateSectionGroup(const aname:string):TObjSectionGroup;
begin
if FGroupsList=nil then
FGroupsList:=TFPHashObjectList.Create(true);
result:=CObjSectionGroup.Create(FGroupsList,aname);
end;
procedure TObjData.CreateDebugSections;
begin
end;
function TObjData.FindSection(const aname:string):TObjSection;
begin
result:=TObjSection(FObjSectionList.Find(aname));
end;
procedure TObjData.setsection(asec:TObjSection);
begin
if asec.ObjData<>self then
internalerror(200403041);
FCurrObjSec:=asec;
end;
function TObjData.createsymbol(const aname:string):TObjSymbol;
begin
result:=TObjSymbol(FObjSymbolList.Find(aname));
if not assigned(result) then
result:=CObjSymbol.Create(FObjSymbolList,aname);
{$ifdef ARM}
result.ThumbFunc:=ThumbFunc;
ThumbFunc:=false;
{$endif ARM}
end;
function TObjData.symboldefine(asmsym:TAsmSymbol):TObjSymbol;
begin
if assigned(asmsym) then
begin
if asmsym.typ = AT_NONE then
InternalError(2018062800);
if not assigned(asmsym.cachedObjSymbol) then
begin
result:=symboldefine(asmsym.name,asmsym.bind,asmsym.typ);
asmsym.cachedObjSymbol:=result;
FCachedAsmSymbolList.add(asmsym);
end
else
begin
result:=TObjSymbol(asmsym.cachedObjSymbol);
result.SetAddress(CurrPass,CurrObjSec,asmsym.bind,asmsym.typ);
end;
end
else
result:=nil;
end;
function TObjData.symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
begin
if not assigned(CurrObjSec) then
internalerror(200603051);
result:=CreateSymbol(aname);
result.SetAddress(CurrPass,CurrObjSec,abind,atyp);
end;
function TObjData.symbolref(asmsym:TAsmSymbol):TObjSymbol;
var
s:string;
begin
if assigned(asmsym) then
begin
if not assigned(asmsym.cachedObjSymbol) then
begin
s:=asmsym.name;
result:=TObjSymbol(FObjSymbolList.Find(s));
if result=nil then
begin
result:=CObjSymbol.Create(FObjSymbolList,s);
if asmsym.bind=AB_WEAK_EXTERNAL then
result.bind:=AB_WEAK_EXTERNAL;
end;
asmsym.cachedObjSymbol:=result;
FCachedAsmSymbolList.add(asmsym);
end
else
result:=TObjSymbol(asmsym.cachedObjSymbol);
{ The weak bit could have been removed from asmsym. }
if (asmsym.bind=AB_EXTERNAL) and (result.bind=AB_WEAK_EXTERNAL) then
result.bind:=AB_EXTERNAL;
end
else
result:=nil;
end;
function TObjData.symbolref(const aname:string):TObjSymbol;
begin
if not assigned(CurrObjSec) then
internalerror(200603052);
result:=CreateSymbol(aname);
end;
procedure TObjData.ResetCachedAsmSymbols;
var
i : longint;
begin
for i:=0 to FCachedAsmSymbolList.Count-1 do
tasmsymbol(FCachedAsmSymbolList[i]).cachedObjSymbol:=nil;
FCachedAsmSymbolList.Clear;
end;
procedure TObjData.writebytes(const Data;len:aword);
begin
if not assigned(CurrObjSec) then
internalerror(200402251);
CurrObjSec.write(Data,len);
end;
procedure TObjData.alloc(len:aword);
begin
if not assigned(CurrObjSec) then
internalerror(200402252);
CurrObjSec.alloc(len);
end;
procedure TObjData.allocalign(len:longint);
begin
if not assigned(CurrObjSec) then
internalerror(200402253);
CurrObjSec.alloc(align_objsecofs(CurrObjSec.size,len)-CurrObjSec.size);
end;
procedure TObjData.section_afteralloc(p:TObject;arg:pointer);
begin
with TObjSection(p) do
alloc(align_objsecofs(size,secalign)-size);
end;
procedure TObjData.section_afterwrite(p:TObject;arg:pointer);
begin
with TObjSection(p) do
begin
if assigned(Data) then
writezeros(align_objsecofs(size,secalign)-size);
end;
end;
procedure TObjData.section_reset(p:TObject;arg:pointer);
begin
with TObjSection(p) do
begin
Size:=0;
Datapos:=0;
mempos:=0;
if assigned(Data) then
Data.reset;
end;
end;
procedure TObjData.beforealloc;
begin
FCPUType:=current_settings.cputype;
{ create stabs sections if debugging }
if assigned(StabsSec) then
begin
StabsSec.Alloc(sizeof(TObjStabEntry));
StabStrSec.Alloc(1);
end;
end;
procedure TObjData.beforewrite;
begin
FCPUType:=current_settings.cputype;
{ create stabs sections if debugging }
if assigned(StabsSec) then
begin
{ Create dummy HdrSym stab, it will be overwritten in AfterWrite }
StabsSec.WriteZeros(sizeof(TObjStabEntry));
{ start of stabstr }
StabStrSec.writeZeros(1);
end;
end;
procedure TObjData.afteralloc;
begin
FObjSectionList.ForEachCall(@section_afteralloc,nil);
end;
procedure TObjData.afterwrite;
var
hstab : TObjStabEntry;
begin
FObjSectionList.ForEachCall(@section_afterwrite,nil);
{ For the stab section we need an HdrSym which can now be
calculated more easily }
if assigned(StabsSec) then
begin
{ end of stabstr }
StabStrSec.writeZeros(1);
{ header stab }
hstab.strpos:=1;
hstab.ntype:=0;
hstab.nother:=0;
{$push}{$R-}
{ for jwawindows.pas, this causes an range check error, it contains too much stab symbols }
hstab.ndesc:=(StabsSec.Size div sizeof(TObjStabEntry))-1;
{$pop}
hstab.nvalue:=StabStrSec.Size;
MaybeSwapStab(hstab);
StabsSec.Data.seek(0);
StabsSec.Data.write(hstab,sizeof(hstab));
end;
end;
procedure TObjData.resetsections;
begin
FObjSectionList.ForEachCall(@section_reset,nil);
end;
procedure TObjData.layoutsections(var DataPos:TObjSectionOfs);
var
i: longint;
begin
for i:=0 to FObjSectionList.Count-1 do
TObjSection(FObjSectionList[i]).setDatapos(DataPos);
end;
{****************************************************************************
TObjOutput
****************************************************************************}
constructor TObjOutput.create(AWriter:TObjectWriter);
begin
FWriter:=AWriter;
CObjData:=TObjData;
end;
destructor TObjOutput.destroy;
begin
inherited destroy;
end;
function TObjOutput.newObjData(const n:string):TObjData;
begin
result:=CObjData.create(n);
if (cs_use_lineinfo in current_settings.globalswitches) or
(cs_debuginfo in current_settings.moduleswitches) then
result.CreateDebugSections;
end;
function TObjOutput.startObjectfile(const fn:string):boolean;
begin
result:=false;
{ start the writer already, so the .a generation can initialize
the position of the current objectfile }
if not FWriter.createfile(fn) then
Comment(V_Fatal,'Can''t create object '+fn);
result:=true;
end;
function TObjOutput.writeobjectfile(Data:TObjData):boolean;
begin
if errorcount=0 then
result:=writeData(Data)
else
result:=true;
{ close the writer }
FWriter.closefile;
end;
procedure TObjOutput.exportsymbol(p:TObjSymbol);
begin
{ export globals and common symbols, this is needed
for .a files }
if p.bind in [AB_GLOBAL,AB_COMMON] then
FWriter.writesym(p.name);
end;
procedure TObjOutput.WriteSectionContent(Data:TObjData);
var
i:longint;
sec:TObjSection;
begin
for i:=0 to Data.ObjSectionList.Count-1 do
begin
sec:=TObjSection(Data.ObjSectionList[i]);
if (oso_data in sec.SecOptions) then
begin
if sec.Data=nil then
internalerror(200403073);
FWriter.writezeros(sec.dataalignbytes);
if sec.Datapos<>FWriter.ObjSize then
internalerror(200604031);
FWriter.writearray(sec.data);
end;
end;
end;
{****************************************************************************
TExeVTable
****************************************************************************}
constructor TExeVTable.Create(AExeSymbol:TExeSymbol);
begin
ExeSymbol:=AExeSymbol;
if ExeSymbol.State=symstate_undefined then
internalerror(200604012);
ChildList:=TFPObjectList.Create(false);
end;
destructor TExeVTable.Destroy;
begin
ChildList.Free;
if assigned(EntryArray) then
Freemem(EntryArray);
end;
procedure TExeVTable.CheckIdx(VTableIdx:longint);
var
OldEntryCnt : longint;
begin
if VTableIdx>=EntryCnt then
begin
OldEntryCnt:=EntryCnt;
EntryCnt:=VTableIdx+1;
ReAllocMem(EntryArray,EntryCnt*sizeof(TVTableEntry));
FillChar(EntryArray[OldEntryCnt],(EntryCnt-OldEntryCnt)*sizeof(TVTableEntry),0);
end;
end;
procedure TExeVTable.AddChild(vt:TExeVTable);
begin
ChildList.Add(vt);
end;
procedure TExeVTable.AddEntry(VTableIdx:Longint);
var
i : longint;
objreloc : TObjRelocation;
vtblentryoffset : aword;
begin
CheckIdx(VTableIdx);
vtblentryoffset:=ExeSymbol.ObjSymbol.Offset+longword(VTableIdx)*sizeof(pint);
{ Find and disable relocation }
for i:=0 to ExeSymbol.ObjSymbol.ObjSection.ObjRelocations.Count-1 do
begin
objreloc:=TObjRelocation(ExeSymbol.ObjSymbol.ObjSection.ObjRelocations[i]);
if objreloc.dataoffset=vtblentryoffset then
begin
EntryArray[VTableIdx].ObjRelocation:=objreloc;
EntryArray[VTableIdx].OrgRelocType:=objreloc.ftype;
EntryArray[VTableIdx].OrgRelocFlags:=objreloc.flags;
objreloc.typ:=RELOC_ZERO;
objreloc.flags:=objreloc.flags or rf_nosymbol;
break;
end;
end;
if not assigned(EntryArray[VTableIdx].ObjRelocation) then
internalerror(200604011);
end;
procedure TExeVTable.SetVTableSize(ASize:longint);
begin
if EntryCnt<>0 then
internalerror(200603313);
EntryCnt:=ASize div sizeof(pint);
EntryArray:=AllocMem(EntryCnt*sizeof(TVTableEntry));
end;
function TExeVTable.VTableRef(VTableIdx:Longint):TObjRelocation;
begin
result:=nil;
CheckIdx(VTableIdx);
if EntryArray[VTableIdx].Used then
exit;
{ Restore relocation if available }
if assigned(EntryArray[VTableIdx].ObjRelocation) then
begin
EntryArray[VTableIdx].ObjRelocation.ftype:=EntryArray[VTableIdx].OrgRelocType;
EntryArray[VTableIdx].ObjRelocation.flags:=EntryArray[VTableIdx].OrgRelocFlags;
result:=EntryArray[VTableIdx].ObjRelocation;
end;
EntryArray[VTableIdx].Used:=true;
end;
{****************************************************************************
TExeSection
****************************************************************************}
constructor TExeSection.create(AList:TFPHashObjectList;const AName:string);
begin
inherited create(AList,AName);
Size:=0;
MemPos:=0;
DataPos:=0;
FSecSymIdx:=0;
FObjSectionList:=TFPObjectList.Create(false);
end;
destructor TExeSection.destroy;
begin
ObjSectionList.Free;
inherited destroy;
end;
procedure TExeSection.AddObjSection(objsec:TObjSection;ignoreprops:boolean);
begin
ObjSectionList.Add(objsec);
{ relate ObjSection to ExeSection, and mark it Used by default }
objsec.ExeSection:=self;
objsec.Used:=true;
if ignoreprops then
exit;
if (SecOptions<>[]) then
begin
{ Only if the section contains (un)initialized data the
data flag must match. }
if ((oso_Data in SecOptions)<>(oso_Data in objsec.SecOptions)) then
Comment(V_Error,'Incompatible section options');
end
else
begin
{ inherit section options }
SecOptions:=SecOptions+objsec.SecOptions;
end;
SecAlign:=max(objsec.SecAlign,SecAlign);
end;
{****************************************************************************
TStaticLibrary
****************************************************************************}
constructor TStaticLibrary.create(const AName:TCmdStr;AReader:TObjectReader;AObjInputClass:TObjInputClass);
begin
FName:=AName;
FPayload:=AReader;
FObjInputClass:=AObjInputClass;
FKind:=lkArchive;
end;
constructor TStaticLibrary.create_object(AObjData:TObjData);
begin
FPayload:=AObjData;
FKind:=lkObject;
end;
constructor TStaticLibrary.create_group;
begin
FPayload:=TFPObjectList.Create(true);
FKind:=lkGroup;
end;
destructor TStaticLibrary.destroy;
begin
FPayload.Free;
inherited destroy;
end;
function TStaticLibrary.GetArReader: TObjectReader;
begin
if (FKind<>lkArchive) then
InternalError(2012071501);
result:=TObjectReader(FPayload);
end;
function TStaticLibrary.GetGroupMembers: TFPObjectList;
begin
if (FKind<>lkGroup) then
InternalError(2012071502);
result:=TFPObjectList(FPayload);
end;
function TStaticLibrary.GetObjData: TObjData;
begin
if (FKind<>lkObject) then
InternalError(2012071503);
result:=TObjData(FPayload);
end;
{****************************************************************************
TImportLibrary
****************************************************************************}
constructor TImportLibrary.create(AList:TFPHashObjectList;const AName:string);
begin
inherited create(AList,AName);
FImportSymbolList:=TFPHashObjectList.Create(true);
end;
destructor TImportLibrary.destroy;
begin
ImportSymbolList.Free;
inherited destroy;
end;
{****************************************************************************
TImportSymbol
****************************************************************************}
constructor TImportSymbol.create(AList:TFPHashObjectList;
const AName,AMangledName:string;AOrdNr:longint;AIsVar:boolean);
begin
inherited Create(AList, AName);
FOrdNr:=AOrdNr;
FIsVar:=AIsVar;
FMangledName:=AMangledName;
{ Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
{ This allows to import VC++ mangled names from DLLs. }
if target_info.system in systems_all_windows then
begin
Replace(FMangledName,'?','__q$$');
{$ifdef arm}
{ @ symbol is not allowed in ARM assembler only }
Replace(FMangledName,'@','__a$$');
{$endif arm}
end;
end;
{****************************************************************************
TExeOutput
****************************************************************************}
constructor TExeOutput.create;
begin
{ init writer }
FWriter:=TObjectwriter.create;
FExeWriteMode:=ewm_exefull;
{ object files }
FObjDataList:=TFPObjectList.Create(true);
{ symbols }
FExeSymbolList:=TFPHashObjectList.Create(true);
FUnresolvedExeSymbols:=TFPObjectList.Create(false);
FExternalObjSymbols:=TFPObjectList.Create(false);
FCommonObjSymbols:=TFPObjectList.Create(false);
FProvidedObjSymbols:=TFPObjectList.Create(false);
FIndirectObjSymbols:=TFPObjectList.Create(false);
FExeVTableList:=TFPObjectList.Create(false);
ComdatGroups:=TFPHashList.Create;
{ sections }
FExeSectionList:=TFPHashObjectList.Create(true);
FImageBase:=0;
{$ifdef cpu16bitaddr}
SectionMemAlign:=$10;
SectionDataAlign:=$10;
{$else cpu16bitaddr}
SectionMemAlign:=$1000;
SectionDataAlign:=$200;
{$endif cpu16bitaddr}
FixedSectionAlign:=True;
FCExeSection:=TExeSection;
FCObjData:=TObjData;
FCObjSymbol:=TObjSymbol;
end;
destructor TExeOutput.destroy;
begin
FExeSymbolList.free;
UnresolvedExeSymbols.free;
ExternalObjSymbols.free;
FProvidedObjSymbols.free;
FIndirectObjSymbols.free;
CommonObjSymbols.free;
ExeVTableList.free;
FExeSectionList.free;
ComdatGroups.free;
ObjDatalist.free;
FWriter.free;
inherited destroy;
end;
function TExeOutput.MemAlign(exesec:TExeSection):longword;
begin
if FixedSectionAlign then
result:=SectionMemAlign
else
result:=exesec.SecAlign;
end;
function TExeOutput.DataAlign(exesec:TExeSection):longword;
begin
if FixedSectionAlign then
result:=SectionDataAlign
else
result:=exesec.SecAlign;
end;
function TExeOutput.WriteExeFile(const fn:string):boolean;
begin
result:=false;
if FWriter.createfile(fn) then
begin
{ Only write the .o if there are no errors }
if errorcount=0 then
result:=writedata
else
result:=true;
{ close the writer }
FWriter.closefile;
end
else
Comment(V_Fatal,'Can''t create executable '+fn);
end;
procedure TExeOutput.ParseScript (linkscript:TCmdStrList);
begin
end;
function TExeOutput.FindExeSection(const aname:string):TExeSection;
begin
result:=TExeSection(ExeSectionList.Find(aname));
end;
procedure TExeOutput.AddObjData(ObjData:TObjData);
begin
if ObjData.classtype<>FCObjData then
Comment(V_Error,'Invalid input object format for '+ObjData.name+' got '+ObjData.classname+' expected '+FCObjData.classname);
ObjDataList.Add(ObjData);
ExecStack:=ExecStack or ObjData.ExecStack;
end;
procedure TExeOutput.Load_Start;
begin
ObjDataList.Clear;
{ Globals defined in the linker script }
if not assigned(internalObjData) then
internalObjData:=CObjData.create('*Internal*');
AddObjData(internalObjData);
{ Common Data section }
commonObjSection:=internalObjData.createsection(sec_bss,'');
end;
procedure TExeOutput.Load_EntryName(const aname:string);
begin
FEntryName:=aname;
end;
procedure TExeOutput.Load_IsSharedLibrary;
begin
IsSharedLibrary:=true;
end;
procedure TExeOutput.Load_ImageBase(const avalue:string);
var
code : integer;
objsec : TObjSection;
objsym : TObjSymbol;
exesym : TExeSymbol;
begin
val(avalue,FImageBase,code);
if code<>0 then
Comment(V_Error,'Invalid number '+avalue);
{ Create __image_base__ symbol, create the symbol
in a section with adress 0 and at offset 0 }
objsec:=internalObjData.createsection('*__image_base__',0,[]);
internalObjData.setsection(objsec);
objsym:=internalObjData.SymbolDefine('__image_base__',AB_GLOBAL,AT_DATA);
exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
exesym.ObjSymbol:=objsym;
end;
procedure TExeOutput.Load_Symbol(const aname:string);
begin
internalObjData.createsection('*'+aname,0,[]);
internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_DATA);
end;
procedure TExeOutput.Load_ProvideSymbol(const aname:string);
begin
if assigned(ExeSymbolList.Find(aname)) then
exit;
internalObjData.createsection('*'+aname,0,[]);
// Use AB_COMMON to avoid muliple defined complaints
internalObjData.SymbolDefine(aname,AB_COMMON,AT_DATA);
end;
procedure TExeOutput.Load_DynamicObject(ObjData:TObjData;asneeded:boolean);
begin
end;
procedure TExeOutput.Order_Start;
begin
end;
procedure TExeOutput.Order_End;
begin
internalObjData.afterwrite;
end;
procedure TExeOutput.Order_ExeSection(const aname:string);
var
sec : TExeSection;
begin
sec:=FindExeSection(aname);
if not assigned(sec) then
sec:=CExeSection.create(ExeSectionList,aname);
{ Clear ExeSection contents }
FCurrExeSec:=sec;
end;
procedure TExeOutput.Order_EndExeSection;
begin
if not assigned(CurrExeSec) then
internalerror(200602184);
FCurrExeSec:=nil;
end;
procedure TExeOutput.Order_ObjSection(const aname:string);
var
i,j : longint;
ObjData : TObjData;
objsec : TObjSection;
TmpObjSectionList : TFPObjectList;
begin
if not assigned(CurrExeSec) then
internalerror(200602181);
TmpObjSectionList:=TFPObjectList.Create(false);
for i:=0 to ObjDataList.Count-1 do
begin
ObjData:=TObjData(ObjDataList[i]);
for j:=0 to ObjData.ObjSectionList.Count-1 do
begin
objsec:=TObjSection(ObjData.ObjSectionList[j]);
if (not objsec.Used) and
MatchPattern(aname,objsec.name) then
TmpObjSectionList.Add(objsec);
end;
end;
{ Order list if needed }
Order_ObjSectionList(TmpObjSectionList,aname);
{ Add the (ordered) list to the current ExeSection }
for i:=0 to TmpObjSectionList.Count-1 do
begin
objsec:=TObjSection(TmpObjSectionList[i]);
CurrExeSec.AddObjSection(objsec);
end;
TmpObjSectionList.Free;
end;
procedure TExeOutput.Order_ObjSectionList(ObjSectionList : TFPObjectList; const aPattern:string);
begin
end;
procedure TExeOutput.Order_Symbol(const aname:string);
var
objsym: TObjSymbol;
begin
objsym:=TObjSymbol(internalObjData.ObjSymbolList.Find(aname));
if (objsym=nil) or (objsym.ObjSection.ObjData<>internalObjData) then
internalerror(200603041);
CurrExeSec.AddObjSection(objsym.ObjSection,True);
end;
procedure TExeOutput.Order_ProvideSymbol(const aname:string);
var
objsym : TObjSymbol;
exesym : TExeSymbol;
begin
objsym:=TObjSymbol(internalObjData.ObjSymbolList.Find(aname));
if (objsym=nil) or (objsym.ObjSection.ObjData<>internalObjData) then
internalerror(200603041);
exesym:=TExeSymbol(ExeSymbolList.Find(aname));
if not assigned(exesym) then
internalerror(201206301);
{ Only include this section if it actually resolves
the symbol }
if exesym.objsymbol=objsym then
CurrExeSec.AddObjSection(objsym.ObjSection,True);
end;
procedure TExeOutput.Order_Align(const avalue:string);
var
code : integer;
alignval : shortint;
objsec : TObjSection;
begin
val(avalue,alignval,code);
if code<>0 then
Comment(V_Error,'Invalid number '+avalue);
if alignval<=0 then
exit;
{ Create an empty section with the required aligning }
inc(Fzeronr);
objsec:=internalObjData.createsection('*align'+tostr(Fzeronr),alignval,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
CurrExeSec.AddObjSection(objsec);
end;
procedure TExeOutput.Order_Zeros(const avalue:string);
var
zeros : array[0..1023] of byte;
code : integer;
len : longint;
objsec : TObjSection;
begin
val(avalue,len,code);
if code<>0 then
Comment(V_Error,'Invalid number '+avalue);
if len<=0 then
exit;
if len>sizeof(zeros) then
internalerror(200602254);
fillchar(zeros,len,0);
inc(Fzeronr);
objsec:=internalObjData.createsection('*zeros'+tostr(Fzeronr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
internalObjData.writebytes(zeros,len);
CurrExeSec.AddObjSection(objsec);
end;
procedure TExeOutput.Order_Values(bytesize : aword; const avalue:string);
const
MAXVAL = 128;
var
bytevalues : array[0..MAXVAL-1] of byte;
twobytevalues : array[0..MAXVAL-1] of word;
fourbytevalues : array[0..MAXVAL-1] of dword;
eightbytevalues : array[0..MAXVAL-1] of qword;
allvals, oneval : string;
len, commapos : longint;
indexpos, code : integer;
anumval : qword;
signedval : int64;
objsec : TObjSection;
begin
indexpos:=0;
allvals:=avalue;
{ avoid warnings }
bytevalues[0]:=0;
twobytevalues[0]:=0;
fourbytevalues[0]:=0;
eightbytevalues[0]:=0;
repeat
commapos:=pos(',',allvals);
if commapos>0 then
begin
oneval:=trim(copy(allvals,1,commapos-1));
allvals:=copy(allvals,commapos+1,length(allvals));
end
else
begin
oneval:=trim(allvals);
allvals:='';
end;
if oneval<>'' then
begin
if oneval[1]='-' then
begin
val(oneval,signedval,code);
anumval:=qword(signedval);
end
else
val(oneval,anumval,code);
if code<>0 then
Comment(V_Error,'Invalid number '+avalue)
else
begin
if (indexpos<MAXVAL) then
begin
if source_info.endian<>target_info.endian then
swapendian(anumval);
{ No range checking here }
if bytesize=1 then
bytevalues[indexpos]:=byte(anumval)
else if bytesize=2 then
twobytevalues[indexpos]:=word(anumval)
else if bytesize=4 then
fourbytevalues[indexpos]:=dword(anumval)
else if bytesize=8 then
eightbytevalues[indexpos]:=anumval;
inc(indexpos);
end
else
Comment(V_Error,'Buffer overrun in Order_values');
end;
end;
until allvals='';
if indexpos=0 then
begin
Comment(V_Error,'Invalid number '+avalue);
exit;
end;
if indexpos=MAXVAL then
begin
Comment(V_Error,'Too many values '+avalue);
internalerror(200602254);
end;
len:=bytesize*indexpos;
inc(Fvaluesnr);
objsec:=internalObjData.createsection('*values'+tostr(Fvaluesnr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
if bytesize=1 then
internalObjData.writebytes(bytevalues,len)
else if bytesize=2 then
internalObjData.writebytes(twobytevalues,len)
else if bytesize=4 then
internalObjData.writebytes(fourbytevalues,len)
else if bytesize=8 then
internalObjData.writebytes(eightbytevalues,len);
CurrExeSec.AddObjSection(objsec);
end;
procedure TExeOutput.MemPos_Start;
begin
CurrMemPos:=0;
RemoveDisabledSections;
end;
procedure TExeOutput.MemPos_Header;
begin
end;
procedure TExeOutput.MemPos_ExeSection(exesec:TExeSection);
var
i : longint;
objsec : TObjSection;
begin
{ Alignment of ExeSection }
CurrMemPos:=align_qword(CurrMemPos,MemAlign(exesec));
exesec.MemPos:=CurrMemPos;
{ set position of object ObjSections }
for i:=0 to exesec.ObjSectionList.Count-1 do
begin
objsec:=TObjSection(exesec.ObjSectionList[i]);
CurrMemPos:=objsec.setmempos(CurrMemPos);
end;
{ calculate size of the section }
exesec.Size:=CurrMemPos-exesec.MemPos;
end;
procedure TExeOutput.MemPos_ExeSection(const aname:string);
begin
{ Section can be removed }
FCurrExeSec:=FindExeSection(aname);
if not assigned(CurrExeSec) then
exit;
MemPos_ExeSection(CurrExeSec);
end;
procedure TExeOutput.MemPos_EndExeSection;
begin
if not assigned(CurrExeSec) then
exit;
FCurrExeSec:=nil;
end;
procedure TExeOutput.DataPos_Start;
begin
end;
procedure TExeOutput.DataPos_Header;
begin
end;
procedure TExeOutput.DataPos_ExeSection(exesec:TExeSection);
begin
{ don't write normal section if writing only debug info }
if (ExeWriteMode=ewm_dbgonly) and
(exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
exit;
if (oso_Data in exesec.SecOptions) then
begin
CurrDataPos:=align_aword(CurrDataPos,DataAlign(exesec));
exesec.DataPos:=CurrDataPos;
CurrDataPos:=CurrDataPos+exesec.Size;
end;
end;
procedure TExeOutput.DataPos_ExeSection(const aname:string);
begin
{ Section can be removed }
FCurrExeSec:=FindExeSection(aname);
if not assigned(CurrExeSec) then
exit;
DataPos_ExeSection(CurrExeSec);
end;
procedure TExeOutput.DataPos_EndExeSection;
begin
if not assigned(CurrExeSec) then
exit;
FCurrExeSec:=nil;
end;
procedure TExeOutput.DataPos_Symbols;
begin
end;
procedure TExeOutput.BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
var
hs : string;
code : integer;
i,k,
vtableidx : longint;
vtableexesym,
childexesym,
parentexesym : TExeSymbol;
objsym : TObjSymbol;
begin
{ Build inheritance tree from VTINHERIT }
for i:=0 to VTInheritList.Count-1 do
begin
objsym:=TObjSymbol(VTInheritList[i]);
hs:=objsym.name;
{ VTINHERIT_<ChildVMTName>$$<ParentVMTName> }
Delete(hs,1,Pos('_',hs));
k:=Pos('$$',hs);
if k=0 then
internalerror(200603311);
childexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
parentexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,k+2,length(hs)-k-1)));
if not assigned(childexesym) or
not assigned(parentexesym)then
internalerror(200603312);
if not assigned(childexesym.vtable) then
begin
childexesym.vtable:=TExeVTable.Create(childexesym);
ExeVTableList.Add(childexesym.vtable);
end;
if not assigned(parentexesym.vtable) then
begin
parentexesym.vtable:=TExeVTable.Create(parentexesym);
ExeVTableList.Add(parentexesym.vtable);
end;
childexesym.vtable.SetVTableSize(childexesym.ObjSymbol.Size);
if parentexesym<>childexesym then
parentexesym.vtable.AddChild(childexesym.vtable);
end;
{ Find VTable entries from VTENTRY }
for i:=0 to VTEntryList.Count-1 do
begin
objsym:=TObjSymbol(VTEntryList[i]);
hs:=objsym.name;
{ VTENTRY_<VTableName>$$<Index> }
Delete(hs,1,Pos('_',hs));
k:=Pos('$$',hs);
if k=0 then
internalerror(200603319);
vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
if (code<>0) then
internalerror(200603318);
if not assigned(vtableexesym) then
internalerror(2006033110);
vtableexesym.vtable.AddEntry(vtableidx);
end;
end;
procedure TExeOutput.PackUnresolvedExeSymbols(const s:string);
var
i : longint;
exesym : TExeSymbol;
begin
{ Generate a list of Unresolved External symbols }
for i:=0 to UnresolvedExeSymbols.count-1 do
begin
exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
if not (exesym.State in [symstate_undefined,symstate_undefweak]) then
UnresolvedExeSymbols[i]:=nil;
end;
UnresolvedExeSymbols.Pack;
Comment(V_Debug,'Number of unresolved externals '+s+' '+tostr(UnresolvedExeSymbols.Count));
end;
procedure TExeOutput.ResolveSymbols(StaticLibraryList:TFPObjectList);
var
ObjData : TObjData;
exesym : TExeSymbol;
objsym,
commonsym : TObjSymbol;
firstarchive,
firstcommon : boolean;
i : longint;
VTEntryList,
VTInheritList : TFPObjectList;
procedure LoadObjDataSymbols(ObjData:TObjData);
var
j : longint;
hs : string;
exesym : TExeSymbol;
objsym : TObjSymbol;
grp : TObjSectionGroup;
begin
for j:=0 to ObjData.ObjSymbolList.Count-1 do
begin
objsym:=TObjSymbol(ObjData.ObjSymbolList[j]);
{ From the local symbols we are only interressed in the
VTENTRY and VTINHERIT symbols }
if objsym.bind=AB_LOCAL then
begin
if cs_link_opt_vtable in current_settings.globalswitches then
begin
hs:=objsym.name;
if (hs[1]='V') then
begin
if Copy(hs,1,5)='VTREF' then
begin
if not assigned(objsym.ObjSection.VTRefList) then
objsym.ObjSection.VTRefList:=TFPObjectList.Create(false);
objsym.ObjSection.VTRefList.Add(objsym);
end
else if Copy(hs,1,7)='VTENTRY' then
VTEntryList.Add(objsym)
else if Copy(hs,1,9)='VTINHERIT' then
VTInheritList.Add(objsym);
end;
end;
continue;
end;
{ If this symbol comes from COMDAT group, see if a group with
matching signature is already included. }
if assigned(objsym.objsection) and
assigned(objsym.objsection.group) then
begin
grp:=objsym.objsection.group;
if grp.IsComdat then
begin
if ComdatGroups.Find(grp.name)=nil then
ComdatGroups.Add(grp.name,grp)
else
begin
{ Undefine the symbol, causing relocations to it from same
objdata to be redirected to the symbol in the actually
linked group. }
if objsym.bind=AB_GLOBAL then
objsym.bind:=AB_EXTERNAL;
{ AB_WEAK_EXTERNAL remains unchanged }
objsym.objsection:=nil;
end;
end;
end;
{ Search for existing exesymbol }
exesym:=texesymbol(FExeSymbolList.Find(objsym.name));
if not assigned(exesym) then
begin
exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
exesym.ObjSymbol:=objsym;
end;
objsym.ExeSymbol:=exesym;
case objsym.bind of
AB_GLOBAL :
begin
if exesym.State<>symstate_defined then
begin
exesym.ObjSymbol:=objsym;
exesym.State:=symstate_defined;
end
else
if (oso_comdat in exesym.ObjSymbol.objsection.SecOptions) and
(oso_comdat in objsym.objsection.SecOptions) then
begin
if exesym.ObjSymbol.objsection.ComdatSelection=objsym.objsection.ComdatSelection then
begin
case objsym.objsection.ComdatSelection of
oscs_none:
Message1(link_e_duplicate_symbol,objsym.name);
oscs_any:
Message1(link_d_comdat_discard_any,objsym.name);
oscs_same_size:
if exesym.ObjSymbol.size<>objsym.size then
Message1(link_e_comdat_size_differs,objsym.name)
else
Message1(link_d_comdat_discard_size,objsym.name);
oscs_exact_match:
if (exesym.ObjSymbol.size<>objsym.size) and not exesym.ObjSymbol.objsection.Data.equal(objsym.objsection.Data) then
Message1(link_e_comdat_content_differs,objsym.name)
else
Message1(link_d_comdat_discard_content,objsym.name);
oscs_associative:
{ this is handled in a different way }
Message1(link_e_duplicate_symbol,objsym.name);
oscs_largest:
if objsym.size>exesym.ObjSymbol.size then
begin
Message1(link_d_comdat_replace_size,objsym.name);
exesym.ObjSymbol.exesymbol:=nil;
exesym.ObjSymbol:=objsym;
end;
end;
end
else
Message1(link_e_comdat_selection_differs,objsym.name);
end
else
{ specific error if ComDat flags are different? }
Message1(link_e_duplicate_symbol,objsym.name);
end;
AB_EXTERNAL :
begin
ExternalObjSymbols.add(objsym);
{ Register unresolved symbols only the first time they
are registered }
if exesym.ObjSymbol=objsym then
UnresolvedExeSymbols.Add(exesym)
{ Normal reference removes any existing "weakness" }
else if exesym.state=symstate_undefweak then
begin
exesym.state:=symstate_undefined;
exesym.ObjSymbol:=objsym;
end;
end;
AB_COMMON :
begin
{ A COMMON definition overrides weak one.
Also select the symbol with largest size. }
if (exesym.State in [symstate_undefined,symstate_undefweak,symstate_defweak]) or
((exesym.State=symstate_common) and (objsym.size>exesym.ObjSymbol.size)) then
begin
exesym.ObjSymbol:=objsym;
exesym.State:=symstate_common;
end;
if assigned(objsym.objsection) and
(objsym.objsection.objdata=internalObjData) then
FProvidedObjSymbols.add(objsym)
else
CommonObjSymbols.add(objsym);
end;
AB_WEAK_EXTERNAL :
begin
if objsym.objsection=nil then { a weak reference }
begin
ExternalObjSymbols.add(objsym);
if exesym.ObjSymbol=objsym then
begin
UnresolvedExeSymbols.Add(exesym);
exesym.state:=symstate_undefweak;
end;
end
else { a weak definition }
begin
if exesym.State in [symstate_undefined,symstate_undefweak] then
begin
exesym.ObjSymbol:=objsym;
exesym.state:=symstate_defweak;
end;
end;
end;
end;
end;
end;
procedure LoadLibrary(lib:TStaticLibrary);
var
j,k,oldcount: longint;
members: TFPObjectList;
exesym: TExeSymbol;
objinput: TObjInput;
begin
case lib.Kind of
lkArchive:
begin
{ Process list of Unresolved External symbols, we need
to use a while loop because the list can be extended when
we load members from the library. }
j:=0;
while (j<UnresolvedExeSymbols.count) do
begin
exesym:=TExeSymbol(UnresolvedExeSymbols[j]);
{ Check first if the symbol is still undefined }
if (exesym.State=symstate_undefined) then
begin
if lib.ArReader.OpenFile(exesym.name) then
begin
if assigned(exemap) then
begin
if firstarchive then
begin
exemap.Add('');
exemap.Add('Archive member included because of file (symbol)');
exemap.Add('');
firstarchive:=false;
end;
exemap.Add(lib.ArReader.FileName+' - '+
{exesym.ObjSymbol.ObjSection.FullName+}
'('+exesym.Name+')');
end;
objinput:=lib.ObjInputClass.Create;
objinput.ReadObjData(lib.ArReader,objdata);
objinput.free;
AddObjData(objdata);
LoadObjDataSymbols(objdata);
lib.ArReader.CloseFile;
end;
end;
inc(j);
end;
end;
lkGroup:
begin
{ repeatedly process members of the group until no new
unresolved symbols appear }
members:=lib.GroupMembers;
repeat
oldcount:=UnresolvedExeSymbols.count;
for k:=0 to members.Count-1 do
LoadLibrary(TStaticLibrary(members[k]));
until UnresolvedExeSymbols.count=oldcount;
end;
lkObject:
{ TODO: ownership of objdata }
//if lib.objdata.is_dynamic then
Load_DynamicObject(lib.objdata,lib.AsNeeded);
{else
begin
AddObjData(lib.objdata);
LoadObjDataSymbols(lib.objdata);
end;}
end;
end;
begin
VTEntryList:=TFPObjectList.Create(false);
VTInheritList:=TFPObjectList.Create(false);
{
The symbol resolving is done in 4 steps:
1. Register symbols from objects
2. Find symbols in static libraries
3. Define symbols PROVIDEd by the link script
4. Define still undefined common symbols
}
{ Step 1, Register symbols from objects }
for i:=0 to ObjDataList.Count-1 do
begin
ObjData:=TObjData(ObjDataList[i]);
LoadObjDataSymbols(ObjData);
end;
PackUnresolvedExeSymbols('in objects');
{ Step 2, Find unresolved symbols in the libraries }
firstarchive:=true;
for i:=0 to StaticLibraryList.Count-1 do
LoadLibrary(TStaticLibrary(StaticLibraryList[i]));
PackUnresolvedExeSymbols('after static libraries');
{ Step 3, handle symbols provided in script }
for i:=0 to FProvidedObjSymbols.count-1 do
begin
objsym:=TObjSymbol(FProvidedObjSymbols[i]);
if objsym.exesymbol.State=symstate_defined then
continue;
objsym.exesymbol.objsymbol:=objsym;
objsym.bind:=AB_GLOBAL;
objsym.exesymbol.State:=symstate_defined;
end;
PackUnresolvedExeSymbols('after defining symbols provided by link script');
{ Step 4, Match common symbols or add to the globals }
firstcommon:=true;
for i:=0 to CommonObjSymbols.count-1 do
begin
objsym:=TObjSymbol(CommonObjSymbols[i]);
if objsym.exesymbol.State=symstate_defined then
begin
if objsym.exesymbol.ObjSymbol.size<>objsym.size then
Comment(V_Debug,'Size of common symbol '+objsym.name+' is different, expected '+tostr(objsym.size)+' got '+tostr(objsym.exesymbol.ObjSymbol.size));
end
else
begin
{ allocate new objsymbol in .bss of *COMMON* and assign
it to the exesymbol }
if firstcommon then
begin
if assigned(exemap) then
exemap.AddCommonSymbolsHeader;
firstcommon:=false;
end;
internalObjData.setsection(commonObjSection);
{ TODO: support alignment of common symbols (ELF targets at least),
increase commonObjSection.SecAlign if necessary here. }
internalObjData.allocalign(used_align(size_2_align(objsym.size),0,commonObjSection.SecAlign));
commonsym:=internalObjData.symboldefine(objsym.name,AB_GLOBAL,AT_DATA);
commonsym.size:=objsym.size;
internalObjData.alloc(objsym.size);
if assigned(exemap) then
exemap.AddCommonSymbol(objsym);
{ Assign to the exesymbol }
objsym.exesymbol.objsymbol:=commonsym;
objsym.exesymbol.state:=symstate_defined;
end;
end;
PackUnresolvedExeSymbols('after defining COMMON symbols');
{ Find entry symbol and print in map }
if (EntryName<>'') then
begin
exesym:=texesymbol(ExeSymbolList.Find(EntryName));
if assigned(exesym) then
begin
EntrySym:=exesym.ObjSymbol;
if assigned(exemap) then
begin
exemap.Add('');
exemap.Add('Entry symbol '+EntryName);
end;
end
else
Comment(V_Error,'Entrypoint '+EntryName+' not defined');
end;
{ Generate VTable tree }
if cs_link_opt_vtable in current_settings.globalswitches then
BuildVTableTree(VTInheritList,VTEntryList);
VTInheritList.Free;
VTEntryList.Free;
end;
procedure TExeOutput.GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
var
debuglink : array[0..1023] of byte;
len : longint;
objsec : TObjSection;
exesec : TExeSection;
begin
{ From the gdb manual chapter 15. GDB Files:
* A filename, with any leading directory components removed, followed by a zero byte,
* zero to three bytes of padding, as needed to reach the next four-byte boundary within the section, and
* a four-byte CRC checksum, stored in the same endianness used for the executable file itself. The checksum is computed
on the debugging information file's full contents by the function given below, passing zero as the crc argument.
}
fillchar(debuglink,sizeof(debuglink),0);
len:=0;
move(dbgname[1],debuglink[len],length(dbgname));
inc(len,length(dbgname)+1);
len:=align(len,4);
if source_info.endian<>target_info.endian then
SwapEndian(dbgcrc);
move(dbgcrc,debuglink[len],sizeof(cardinal));
inc(len,4);
{ Add section }
exesec:=FindExeSection(debuglinkname);
if not assigned(exesec) then
exesec:=CExeSection.create(ExeSectionList,debuglinkname);
exesec.SecOptions:=[oso_data,oso_keep];
exesec.SecAlign:=4;
objsec:=internalObjData.createsection(exesec.name,1,exesec.SecOptions);
internalObjData.writebytes(debuglink,len);
exesec.AddObjSection(objsec);
end;
procedure TExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
begin
end;
procedure TExeOutput.MarkTargetSpecificSections(WorkList:TFPObjectList);
begin
end;
procedure TExeOutput.AfterUnusedSectionRemoval;
begin
end;
function ByAddress(item1,item2:pointer):longint;
var
sym1:TObjSymbol absolute item1;
sym2:TObjSymbol absolute item2;
begin
if sym1.address>sym2.address then
result:=1
else if sym1.address<sym2.address then
result:=-1
else
result:=0;
end;
procedure TExeOutput.PrintMemoryMap;
var
exesec : TExeSection;
objsec : TObjSection;
objsym : TObjSymbol;
i,j,k,m: longint;
list : TFPList;
flag : boolean;
begin
if not assigned(exemap) then
exit;
{ create a list of symbols sorted by address }
list:=TFPList.Create;
for i:=0 to ExeSymbolList.Count-1 do
list.Add(TExeSymbol(ExeSymbolList[i]).ObjSymbol);
list.Sort(@ByAddress);
exemap.AddMemoryMapHeader(ImageBase);
k:=0;
for i:=0 to ExeSectionList.Count-1 do
begin
exesec:=TExeSection(ExeSectionList[i]);
exemap.AddMemoryMapExeSection(exesec);
for j:=0 to exesec.ObjSectionList.count-1 do
begin
objsec:=TObjSection(exesec.ObjSectionList[j]);
exemap.AddMemoryMapObjectSection(objsec);
while (k<list.Count) and (TObjSymbol(list[k]).Address<objsec.MemPos) do
inc(k);
while (k<list.Count) do
begin
objsym:=TObjSymbol(list[k]);
if objsym.address>objsec.MemPos+objsec.Size then
break;
if objsym.objsection=objsec then
exemap.AddMemoryMapSymbol(objsym)
else
begin
{ Got a symbol with address falling into current section, but
belonging to a different section. This may happen for zero-length
sections because symbol list is sorted by address but not by section.
Do some look-ahead in this case. }
m:=k+1;
flag:=false;
while (m<list.Count) and (TObjSymbol(list[m]).Address=objsym.address) do
begin
if TObjSymbol(list[m]).objsection=objsec then
begin
flag:=true;
list.Exchange(k,m);
exemap.AddMemoryMapSymbol(TObjSymbol(list[k]));
break;
end;
inc(m);
end;
if not flag then
break;
end;
inc(k);
end;
end;
end;
list.Free;
end;
procedure TExeOutput.FixupSymbols;
procedure UpdateSymbol(objsym:TObjSymbol);
begin
objsym.bind:=objsym.ExeSymbol.ObjSymbol.bind;
objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
objsym.size:=objsym.ExeSymbol.ObjSymbol.size;
objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
objsym.group:=objsym.ExeSymbol.ObjSymbol.group;
end;
var
i : longint;
objsym : TObjSymbol;
exesym : TExeSymbol;
begin
{ Print list of Unresolved External symbols }
if not AllowUndefinedSymbols then
for i:=0 to UnresolvedExeSymbols.count-1 do
begin
exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
if (exesym.State=symstate_undefined) then
Comment(V_Error,'Undefined symbol: '+exesym.name);
end;
{
Fixing up symbols is done in the following steps:
1. Update common references
2. Update external references
Symbols with objsection<>nil are removed from the lists,
remaining ones can be processed later by calling this method again.
}
{ Step 1, Update commons. Preserve the original symbol size and bind,
this is needed for correct relocation of DJCOFF files. }
for i:=0 to CommonObjSymbols.count-1 do
begin
objsym:=TObjSymbol(CommonObjSymbols[i]);
if objsym.bind<>AB_COMMON then
internalerror(200606241);
objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
end;
{ Step 2, Update externals }
for i:=0 to ExternalObjSymbols.count-1 do
begin
objsym:=TObjSymbol(ExternalObjSymbols[i]);
if not (objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) then
internalerror(200606242);
UpdateSymbol(objsym);
{ Collect symbols that resolve to indirect functions,
they will need additional target-specific processing. }
if objsym.typ=AT_GNU_IFUNC then
IndirectObjSymbols.Add(objsym)
else if assigned(objsym.objsection) then
ExternalObjSymbols[i]:=nil;
end;
CommonObjSymbols.Clear;
ExternalObjSymbols.Pack;
end;
procedure TExeOutput.MergeStabs;
var
stabexesec,
stabstrexesec : TExeSection;
relocsec,
currstabsec,
currstabstrsec,
mergedstabsec,
mergedstabstrsec : TObjSection;
hstabreloc,
currstabreloc : TObjRelocation;
i,j : longint;
currstabrelocidx,
mergestabcnt,
stabcnt : longword;
skipstab : boolean;
skipfun : boolean;
hstab : TObjStabEntry;
stabrelocofs : longword;
buf : array[0..1023] of byte;
bufend,
bufsize : longint;
begin
stabexesec:=FindExeSection('.stab');
stabstrexesec:=FindExeSection('.stabstr');
if (stabexesec=nil) or
(stabstrexesec=nil) or
(stabexesec.ObjSectionlist.count=0) then
exit;
{ Create new stabsection }
stabRelocofs:=pbyte(@hstab.nvalue)-pbyte(@hstab);
mergedstabsec:=internalObjData.CreateSection(sec_stab,'');
mergedstabstrsec:=internalObjData.CreateSection(sec_stabstr,'');
{ write stab for hdrsym }
mergedstabsec.writeZeros(sizeof(TObjStabEntry));
mergestabcnt:=1;
{ .stabstr starts with a #0 }
buf[0]:=0;
mergedstabstrsec.write(buf[0],1);
skipfun:=false;
{ Copy stabs and corresponding Relocations }
for i:=0 to stabexesec.ObjSectionList.Count-1 do
begin
currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
currstabstrsec:=currstabsec.ObjData.findsection('.stabstr');
if assigned(currstabstrsec) then
begin
stabcnt:=currstabsec.Data.size div sizeof(TObjStabEntry);
currstabsec.Data.seek(0);
currstabrelocidx:=0;
for j:=0 to stabcnt-1 do
begin
hstabreloc:=nil;
skipstab:=false;
currstabsec.Data.read(hstab,sizeof(TObjStabEntry));
MaybeSwapStab(hstab);
{ Only include first hdrsym stab }
if hstab.ntype=0 then
skipstab:=true;
if skipfun then
begin
{ Skip all stabs for function body until N_RBRAC }
skipfun:=hstab.ntype<>N_RBRAC;
skipstab:=true;
end;
if not skipstab then
begin
{ Find corresponding Relocation }
currstabreloc:=nil;
while (currstabrelocidx<longword(currstabsec.ObjRelocations.Count)) do
begin
currstabreloc:=TObjRelocation(currstabsec.ObjRelocations[currstabrelocidx]);
if assigned(currstabreloc) and
(currstabreloc.dataoffset>=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
break;
inc(currstabrelocidx);
end;
if assigned(currstabreloc) and
(currstabreloc.dataoffset=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
begin
hstabReloc:=currstabReloc;
inc(currstabrelocidx);
end;
{ Check if the stab is refering to a removed section }
if assigned(hstabreloc) then
begin
if assigned(hstabreloc.Symbol) then
relocsec:=hstabreloc.Symbol.ObjSection
else
relocsec:=hstabreloc.ObjSection;
if not assigned(relocsec) then
internalerror(200603302);
if not relocsec.Used then
begin
skipstab:=true;
if (hstab.ntype=N_Function) and (hstab.strpos<>0) then
begin
currstabstrsec.Data.seek(hstab.strpos);
bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
bufend:=indexbyte(buf,bufsize,Ord(':'));
if (bufend<>-1) and (bufend<bufsize-1) and (buf[bufend+1]=Ord('F')) then
skipfun:=true;
end;
end;
end;
end;
if not skipstab then
begin
{ Copy string in stabstr }
if hstab.strpos<>0 then
begin
currstabstrsec.Data.seek(hstab.strpos);
hstab.strpos:=mergedstabstrsec.Size;
repeat
bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
bufend:=indexbyte(buf,bufsize,0);
if bufend=-1 then
bufend:=bufsize
else
begin
{ include the #0 }
inc(bufend);
end;
mergedstabstrsec.write(buf,bufend);
until (buf[bufend-1]=0) or (bufsize<sizeof(buf));
end;
{ Copy and Update the relocation }
if assigned(hstabreloc) then
begin
hstabreloc.Dataoffset:=mergestabcnt*sizeof(TObjStabEntry)+stabRelocofs;
{ Remove from List without freeing the object }
currstabsec.ObjRelocations.List[currstabrelocidx-1]:=nil;
mergedstabsec.ObjRelocations.Add(hstabreloc);
end;
{ Write updated stab }
MaybeSwapStab(hstab);
mergedstabsec.write(hstab,sizeof(hstab));
inc(mergestabcnt);
end;
end;
end;
{ Unload stabs }
if assigned(currstabstrsec) then
begin
currstabstrsec.Used:=False;
currstabstrsec.ReleaseData;
end;
currstabsec.Used:=false;
currstabsec.ReleaseData;
end;
{ Generate new HdrSym }
if mergedstabsec.Size>0 then
begin
hstab.strpos:=1;
hstab.ntype:=0;
hstab.nother:=0;
hstab.ndesc:=word(mergestabcnt-1);
hstab.nvalue:=mergedstabstrsec.Size;
MaybeSwapStab(hstab);
mergedstabsec.Data.seek(0);
mergedstabsec.Data.write(hstab,sizeof(hstab));
end;
{ Replace all sections with our combined stabsec }
stabexesec.ObjSectionList.Clear;
stabstrexesec.ObjSectionList.Clear;
stabexesec.AddObjSection(mergedstabsec);
stabstrexesec.AddObjSection(mergedstabstrsec);
end;
procedure TExeOutput.MarkEmptySections;
var
i, j : longint;
exesec : TExeSection;
doremove : boolean;
begin
for i:=0 to ExeSectionList.Count-1 do
begin
exesec:=TExeSection(ExeSectionList[i]);
doremove:=not(oso_keep in exesec.SecOptions) and
(
(exesec.ObjSectionlist.count=0) or
(
(cs_link_strip in current_settings.globalswitches) and
not(cs_link_separate_dbg_file in current_settings.globalswitches) and
(oso_debug in exesec.SecOptions)
)
);
if not doremove then
begin
{ Check if section has no actual data }
doremove:=true;
for j:=0 to exesec.ObjSectionList.Count-1 do
if TObjSection(exesec.ObjSectionList[j]).Size<>0 then
begin
doremove:=false;
break;
end;
end;
if doremove then
begin
Comment(V_Debug,'Disabling empty section '+exesec.name);
exesec.Disabled:=true;
end;
end;
end;
procedure TExeOutput.RemoveDisabledSections;
var
i: longint;
exesec: TExeSection;
begin
for i:=0 to ExeSectionList.Count-1 do
begin
exesec:=TExeSection(ExeSectionList[i]);
if exesec.Disabled then
ExeSectionList[i]:=nil;
end;
ExeSectionList.Pack;
end;
procedure TExeOutput.RemoveDebugInfo;
var
i : longint;
exesec : TExeSection;
begin
for i:=0 to ExeSectionList.Count-1 do
begin
exesec:=TExeSection(ExeSectionList[i]);
if (oso_debug in exesec.SecOptions) then
ExeSectionList[i]:=nil;
end;
ExeSectionList.Pack;
end;
procedure TExeOutput.RemoveUnreferencedSections;
var
ObjSectionWorkList : TFPObjectList;
procedure AddToObjSectionWorkList(aobjsec:TObjSection);
begin
if not aobjsec.Used then
begin
aobjsec.Used:=true;
ObjSectionWorkList.Add(aobjsec);
end;
end;
procedure DoReloc(objreloc:TObjRelocation);
var
objsym : TObjSymbol;
refobjsec : TObjSection;
refgrp : TObjSectionGroup;
begin
{ Disabled Relocation to 0 }
if (objreloc.flags and rf_nosymbol)<>0 then
exit;
refobjsec:=nil;
refgrp:=nil;
if assigned(objreloc.symbol) then
begin
objsym:=objreloc.symbol;
if objsym.bind<>AB_LOCAL then
begin
if not assigned(objsym.exesymbol) then
internalerror(200603063);
objsym.exesymbol.used:=true;
objsym:=objsym.exesymbol.objsymbol;
end;
if not assigned(objsym.objsection) then
exit
else
refobjsec:=objsym.objsection;
end
else if assigned(objreloc.objsection) then
refobjsec:=objreloc.objsection
else if assigned(objreloc.group) then
refgrp:=objreloc.group
else
internalerror(200603316);
if assigned(exemap) then
begin
objsym:=objreloc.symbol;
if assigned(objsym) and (objsym.typ<>AT_SECTION) then
exemap.Add(' References '+objsym.name+' in '
+refobjsec.fullname)
else if assigned(refobjsec) then
exemap.Add(' References '+refobjsec.fullname)
else if assigned(refgrp) then
exemap.Add(' References '+refgrp.Name)
else
internalerror(200603316);
end;
if assigned(refobjsec) then
AddToObjSectionWorkList(refobjsec);
end;
procedure DoVTableRef(vtable:TExeVTable;VTableIdx:longint);
var
i : longint;
objreloc : TObjRelocation;
begin
objreloc:=vtable.VTableRef(VTableIdx);
if assigned(objreloc) then
begin
{ Process the relocation now if the ObjSection is
already processed and marked as used. Otherwise we leave it
unprocessed. It'll then be resolved when the ObjSection is
changed to Used }
if vtable.ExeSymbol.ObjSymbol.ObjSection.Used then
DoReloc(objreloc);
end;
{ This recursive walking is done here instead of
in TExeVTable.VTableRef because we can now process
all needed relocations }
for i:=0 to vtable.ChildList.Count-1 do
DoVTableRef(TExeVTable(vtable.ChildList[i]),VTableIdx);
end;
procedure ProcessWorkList;
var
hs : string;
i,k : longint;
objsec : TObjSection;
objsym : TObjSymbol;
code : integer;
vtableidx : longint;
vtableexesym : TExeSymbol;
begin
while ObjSectionWorkList.Count>0 do
begin
objsec:=TObjSection(ObjSectionWorkList.Last);
if assigned(exemap) then
exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjRelocations.Count)+' references');
ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
{ Process Relocations }
for i:=0 to objsec.ObjRelocations.count-1 do
DoReloc(TObjRelocation(objsec.ObjRelocations[i]));
{ Process Virtual Entry calls }
if cs_link_opt_vtable in current_settings.globalswitches then
begin
for i:=0 to objsec.VTRefList.count-1 do
begin
objsym:=TObjSymbol(objsec.VTRefList[i]);
hs:=objsym.name;
Delete(hs,1,Pos('_',hs));
k:=Pos('$$',hs);
if k=0 then
internalerror(200603314);
vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
if (code<>0) then
internalerror(200603317);
if not assigned(vtableexesym) then
internalerror(200603315);
if not assigned(vtableexesym.vtable) then
internalerror(200603316);
DoVTableRef(vtableexesym.vtable,vtableidx);
end;
end;
end;
end;
var
i,j : longint;
exesec : TExeSection;
objdata : TObjData;
objsec : TObjSection;
begin
ObjSectionWorkList:=TFPObjectList.Create(false);
if assigned(exemap) then
exemap.AddHeader('Removing unreferenced sections');
{ Initialize by marking all sections unused and
adding the sections with oso_keep flags to the ObjSectionWorkList }
for i:=0 to ObjDataList.Count-1 do
begin
ObjData:=TObjData(ObjDataList[i]);
for j:=0 to ObjData.ObjSectionList.Count-1 do
begin
objsec:=TObjSection(ObjData.ObjSectionList[j]);
objsec.Used:=false;
{ TODO: remove debug section always keep}
if oso_debug in objsec.secoptions then
objsec.Used:=true;
if (oso_keep in objsec.secoptions) then
begin
AddToObjSectionWorkList(objsec);
if objsec.name='.fpc.n_links' then
objsec.Used:=false;
end;
end;
end;
if assigned(entrysym) then
AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
{ Process all sections, add new sections to process based
on the symbol references }
ProcessWorkList;
{ Handle stuff like .pdata, i.e. sections that are not referenced
but must be included if sections they reference are included.
Loop is necessary because .pdata can reference (via .xdata)
more text sections, VMTs of exception classes, etc. }
repeat
MarkTargetSpecificSections(ObjSectionWorkList);
if (ObjSectionWorkList.Count=0) then
break;
ProcessWorkList;
until False;
ObjSectionWorkList.Free;
ObjSectionWorkList:=nil;
{ Remove unused objsections from ExeSectionList }
for i:=0 to ExeSectionList.Count-1 do
begin
exesec:=TExeSection(ExeSectionList[i]);
for j:=0 to exesec.ObjSectionlist.count-1 do
begin
objsec:=TObjSection(exesec.ObjSectionlist[j]);
if not objsec.used then
begin
if assigned(exemap) then
exemap.Add('Removing '+objsec.FullName);
exesec.ObjSectionlist[j]:=nil;
objsec.ReleaseData;
end;
end;
exesec.ObjSectionlist.Pack;
end;
end;
procedure TExeOutput.FixupRelocations;
var
i,j : longint;
exesec : TExeSection;
objsec : TObjSection;
begin
for i:=0 to ExeSectionList.Count-1 do
begin
exesec:=TExeSection(ExeSectionList[i]);
if not assigned(exesec) then
continue;
for j:=0 to exesec.ObjSectionlist.count-1 do
begin
objsec:=TObjSection(exesec.ObjSectionlist[j]);
if not objsec.Used then
internalerror(200603301);
if (objsec.ObjRelocations.Count>0) and
not assigned(objsec.data) then
internalerror(200205183);
DoRelocationFixup(objsec);
{for size = 0 data is not valid PM }
if assigned(objsec.data) and (objsec.data.size<>objsec.size) then
internalerror(2010092801);
end;
end;
end;
procedure TExeOutput.RemoveUnusedExeSymbols;
var
i : longint;
sym : TExeSymbol;
begin
{ Removing unused symbols }
for i:=0 to ExeSymbolList.Count-1 do
begin
sym:=TExeSymbol(ExeSymbolList[i]);
{ an unresolved weak symbol has objsection=nil }
if assigned(sym.ObjSymbol.objsection) and
(not sym.ObjSymbol.objsection.Used) then
ExeSymbolList[i]:=nil;
end;
ExeSymbolList.Pack;
end;
procedure TExeOutput.SetCurrMemPos(const AValue: qword);
begin
if AValue>MaxMemPos then
Message1(link_f_executable_too_big, target_os_string);
FCurrMemPos:=AValue;
end;
procedure TExeOutput.WriteExeSectionContent;
var
exesec : TExeSection;
objsec : TObjSection;
i,j : longint;
dpos,pad: aword;
begin
for j:=0 to ExeSectionList.Count-1 do
begin
exesec:=TExeSection(ExeSectionList[j]);
{ don't write normal section if writing only debug info }
if (ExeWriteMode=ewm_dbgonly) and
(exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
continue;
if oso_data in exesec.SecOptions then
begin
if exesec.DataPos<FWriter.Size then
InternalError(2012103001);
FWriter.Writezeros(exesec.DataPos-FWriter.Size);
for i:=0 to exesec.ObjSectionList.Count-1 do
begin
objsec:=TObjSection(exesec.ObjSectionList[i]);
if oso_data in objsec.secoptions then
begin
if not assigned(objsec.data) then
internalerror(200603042);
dpos:=objsec.MemPos-exesec.MemPos+exesec.DataPos;
pad:=dpos-FWriter.Size;
{ objsection must be within SecAlign bytes from the previous one }
if (dpos<FWriter.Size) or
(pad>=max(objsec.SecAlign,1)) then
internalerror(200602251);
FWriter.writeZeros(pad);
FWriter.writearray(objsec.data);
end;
end;
end;
end;
end;
procedure TExeOutput.ReplaceExeSectionList(newlist: TFPList);
var
tmp: TFPHashObjectList;
i: longint;
begin
tmp:=TFPHashObjectList.Create(true);
for i:=0 to newlist.count-1 do
TFPHashObject(newlist[i]).ChangeOwner(tmp);
{ prevent destruction of existing sections }
for i:=0 to ExeSectionList.count-1 do
ExeSectionList.List[i]:=nil;
FExeSectionList.Free;
FExeSectionList:=tmp;
end;
{****************************************************************************
TObjInput
****************************************************************************}
constructor TObjInput.create;
begin
end;
procedure TObjInput.inputerror(const s : string);
begin
Comment(V_Error,s+' while reading '+InputFileName);
end;
class function TObjInput.CanReadObjData(AReader:TObjectReader):boolean;
begin
result:=false;
end;
procedure TObjInput.ReadSectionContent(Data:TObjData);
var
i: longint;
sec: TObjSection;
begin
for i:=0 to Data.ObjSectionList.Count-1 do
begin
sec:=TObjSection(Data.ObjSectionList[i]);
{ Skip debug sections }
if (oso_debug in sec.SecOptions) and
(cs_link_strip in current_settings.globalswitches) and
not(cs_link_separate_dbg_file in current_settings.globalswitches) then
continue;
if assigned(sec.Data) then
begin
FReader.Seek(sec.datapos);
if not FReader.ReadArray(sec.data,sec.Size) then
begin
InputError('Can''t read object data');
exit;
end;
end;
end;
end;
{$ifdef MEMDEBUG}
initialization
memobjsymbols:=TMemDebug.create('ObjSymbols');
memobjsymbols.stop;
memobjsections:=TMemDebug.create('ObjSections');
memobjsections.stop;
finalization
memobjsymbols.free;
memobjsections.free;
{$endif MEMDEBUG}
end.
|