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
|
/* dld -- dynamic link/unlink editor for C
Copyright (C) 1990 by W. Wilson Ho.
Version 3.2.7
The author can be reached electronically by how@cs.ucdavis.edu or
through physical mail at:
W. Wilson Ho
Division of Computer Science
University of California at Davis
Davis, CA 95616
*/
/* 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 1, or (at your option) any
later version.
This program borrows and modifies a number of functions and data
structures from the implementation of the GNU `ld' link editor. The
original copyleft notice from the GNU `ld' is also included. */
/* Linker `ld' for GNU
Copyright (C) 1988 Free Software Foundation, Inc.
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 1, 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. */
#include "defs.h"
#if 0
/*
* This part is taken out after the elimination of use of alloca().
*/
/*
* Alloca include.
*/
/* If compiled with GNU C, use the built-in alloca */
#ifdef __GNUC__
#define alloca __builtin_alloca
#endif
#if defined(sun) && defined(sparc) && !defined(__GNUC__)
#include <alloca.h>
#endif
#endif
/* System dependencies */
#ifdef atarist
#include <basepage.h>
/* _initial_stack determines the amount of memory available as stack/heap, */
/* if it's negative this is the amount available to the system, 1L means */
/* use a ratio of 1:3 (stack:system), 2L means ratio 1:1, 3L stands for 3:1 */
static char __patch_str[] = "{PatchVar}stack = %ld bytes";
extern long _initial_stack = - 100 * 1024L;
#endif /* atarist */
/* Ordinary 4.3 bsd lacks these macros in a.out.h */
#ifndef N_TXTADDR
#ifdef vax
#define N_TXTADDR(x) 0
#endif
#ifdef sequent
#define N_TXTADDR(x) (N_ADDRADJ(x))
#endif
#endif
#ifndef N_DATADDR
#ifdef vax
#define N_DATADDR(x) \
(((x).a_magic == OMAGIC) ? (N_TXTADDR(x)+(x).a_text) \
: (page_size + ((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
#endif
#ifdef sequent
#define N_DATADDR(x) \
(((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
: (page_size+(((x).a_text-1) & ~(page_size-1))))
#endif
#endif
#ifndef N_BSSADDR
#ifdef vax
#define N_BSSADDR(x) (N_DATADDR(x)+(x).a_data)
#endif
#endif
/*
* Ok. Following are the relocation information macros. If your
* system cannot use the default set (below), you must define all of these:
* relocation_info: This must be typedef'd (or #define'd) to the type
* of structure that is stored in the relocation info section of your
* a.out files. Often this is defined in the a.out.h for your system.
*
* RELOC_ADDRESS (rval): Offset into the current section of the
* <whatever> to be relocated. *Must be an lvalue*.
*
* RELOC_EXTERN_P (rval): Is this relocation entry based on an
* external symbol (1), or was it fully resolved upon entering the
* loader (0) in which case some combination of the value in memory
* (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
* what the value of the relocation actually was. *Must be an lvalue*.
*
* RELOC_SYMBOL (rval): For an external relocation, this is the
* index of its symbol in the symbol table. *Must be an lvalue*.
*
* RELOC_PCREL_P (rval): True if the relocation value described is
* pc relative.
*
* RELOC_MEMORY_SUB_P (rval): If this is nonzero, the value previously
* present in the memory location to be relocated is *subtracted*
* from the relocation value, to produce the final result.
* By default, this is always 0.
*
* RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
* an extra value to be added to the relocation value based on the
* individual relocation entry. *Must be an lvalue if defined*.
*
* RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
* final relocation value before putting it where it belongs.
*
* RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
* bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
* == 1; 4 bytes == 2, and etc. This is somewhat redundant (we could
* do everything in terms of the bit operators below), but having this
* macro could end up producing better code on machines without fancy
* bit twiddling. Also, it's easier to understand/code big/little
* endian distinctions with this macro.
*
* RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
* with the bits of the relocation value. It may be assumed by the
* code that the relocation value will fit into this many bits. This
* may be larger than RELOC_TARGET_SIZE if such be useful.
*
*
* Things I haven't implemented
* ----------------------------
*
* Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
*
* Pc relative relocation for External references.
*
*
*/
#if defined(sun) && defined(sparc)
/* Sparc (Sun 4) macros */
#undef relocation_info
#define relocation_info reloc_info_sparc
#define RELOC_ADDRESS(r) ((r)->r_address)
#define RELOC_EXTERN_P(r) ((r)->r_extern)
#define RELOC_SYMBOL(r) ((r)->r_index)
#define RELOC_MEMORY_SUB_P(r) 0
#define RELOC_ADD_EXTRA(r) ((r)->r_addend)
#define RELOC_PCREL_P(r) \
((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
#define RELOC_VALUE_RIGHTSHIFT(r) (reloc_target_rightshift[(r)->r_type])
#define RELOC_TARGET_SIZE(r) (reloc_target_size[(r)->r_type])
#define RELOC_TARGET_BITSIZE(r) (reloc_target_bitsize[(r)->r_type])
/* Note that these are very dependent on the order of the enums in
enum reloc_type (in a.out.h); if they change the following must be
changed */
/* Also note that the last few may be incorrect; I have no information */
static int reloc_target_rightshift[] = {
0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
};
static int reloc_target_size[] = {
0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
};
static int reloc_target_bitsize[] = {
8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
};
#endif
#if defined(sequent)
#define RELOC_ADDRESS(r) ((r)->r_address)
#define RELOC_EXTERN_P(r) ((r)->r_extern)
#define RELOC_SYMBOL(r) ((r)->r_symbolnum)
#define RELOC_MEMORY_SUB_P(r) ((r)->r_bsr)
#undef RELOC_ADD_EXTRA
#define RELOC_PCREL_P(r) ((r)->r_pcrel || (r)->r_bsr)
#define RELOC_TARGET_SIZE(r) ((r)->r_length)
#endif
/* Default macros */
#ifndef RELOC_ADDRESS
#define RELOC_ADDRESS(r) ((r)->r_address)
#define RELOC_EXTERN_P(r) ((r)->r_extern)
#define RELOC_SYMBOL(r) ((r)->r_symbolnum)
#define RELOC_MEMORY_SUB_P(r) 0
#undef RELOC_ADD_EXTRA
#define RELOC_PCREL_P(r) ((r)->r_pcrel)
#define RELOC_TARGET_SIZE(r) ((r)->r_length)
#endif
/* Size of a page; obtained from the operating system. */
static int page_size;
/* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
symbol *_dld_symtab[TABSIZE];
/* Count the number of global symbols referenced and not defined. */
int dld_undefined_sym_count = 0;
/* internal format of relocation info entry. */
struct dld_reloc_info {
/* corresponding symbol table entry. */
symbol *sp;
/* The real relocation info entry.
This is an ugly design. In the original relocation_info structure.
The corresponding symbol definition is located by an index to the
nlist array. This array is not kept online, and so this index
must be replace by the address of the corresponding symbol table
kept online. However, on most machine this index takes only 24bits,
which is not large enough (in general) to hold a pointer. So we
need a structure definition with a sp field as shown above. The
complete original relocation info entry is kept here. This is not
very space economical (24 bits are wasted). But since different
system has different format for STRUCT RELOCATION_INFO, I'd rather
let the include file <a.out.h> take care of the difference, than
using a separate definition for each system. */
struct relocation_info reloc_info;
};
/* Format of __.SYMDEF:
First, a longword containing the size of the 'symdef' data that follows.
Second, zero or more 'symdef' structures.
Third, a word containing the length of symbol name strings.
Fourth, zero or more symbol name strings (each followed by a zero). */
struct symdef {
int symbol_name_string_index;
int library_member_offset;
};
/* variable for saving the environment */
jmp_buf _dld_env;
/* pointer to the lastest (newest) file entry */
struct file_entry *_dld_latest_entry = 0;
/* dummy file_entry to hold all "dangling" symbols. */
struct file_entry *_dld_dummy_entry = 0;
/* To avoid close a file and then open the same file again, the following
two variables remember the file that is currently open. Both are zero
if no file is open.
*/
static struct file_entry *input_file = 0;
static int input_desc = 0;
/* global variables to return the error code to the caller */
int dld_errno;
char *dld_errname;
/* true if the executable flags are up-to-date */
char _dld_exec_flags_valid;
/* Miscellaneous routines */
/* save the error code in dld_errno */
static void
fatal (errno)
register int errno;
{
dld_errno = errno;
longjmp (_dld_env, 1);
} /* fatal */
/* Like malloc but get fatal error if memory is exhausted. */
int
_dld_malloc (size)
int size;
{
register int result = (int) malloc (size);
if (!result)
fatal (DLD_ENOMEMORY);
return result;
} /* _dld_malloc */
/* Return a newly-allocated string
whose contents concatenate the strings S1, S2, S3. */
static char *
concat (s1, s2, s3)
const char *s1;
const char *s2;
const char *s3;
{
register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
register char *result = (char *) _dld_malloc (len1 + len2 + len3 + 1);
strcpy (result, s1);
strcpy (result + len1, s2);
strcpy (result + len1 + len2, s3);
result[len1 + len2 + len3] = 0;
return result;
} /* concat */
/* Add a new entry to the file chain if it is not already there.
Return 0 if no actual insertion is needed, otherwise, return 1. */
static int
insert_entry (head, entry)
struct file_chain **head;
register struct file_entry *entry;
{
register struct file_chain *fc = *head;
while (fc) {
if (fc->entry == entry)
return 0;
else fc = fc->next;
}
fc = (struct file_chain *) _dld_malloc (sizeof (struct file_chain));
fc->next = *head;
fc->entry = entry;
*head = fc;
return 1;
} /* insert_entry */
/* Close the input file that is now open. */
static void
file_close ()
{
if (input_desc) close (input_desc);
input_desc = 0;
input_file = 0;
} /* file_close */
/* Open the input file specified by 'entry', and return a descriptor.
The open file is remembered; if the same file is opened twice in a row,
a new open is not actually done. */
static int
file_open (entry)
register struct file_entry *entry;
{
register int desc;
if (entry == 0) fatal (DLD_ENOFILE);
if (entry->superfile)
return file_open (entry->superfile);
if (entry == input_file)
return input_desc;
if (input_file) file_close ();
desc = open (entry->filename, O_RDONLY, 0);
if (desc > 0) {
input_file = entry;
input_desc = desc;
return desc;
}
fatal (DLD_ENOFILE);
return(1); /* To supress warning message */
} /* file_open */
/* Medium-level input routines for rel files. */
/* Read a file's header into the proper place in the file_entry.
DESC is the descriptor on which the file is open.
ENTRY is the file's entry. */
/* For Convex C-Series, the header information is scattered about in the
* filehdr, scnhdr, and opthdr. Hence, these must be processed and read
* so that the exec structure is properly handled */
static void
read_header (desc, entry)
int desc;
register struct file_entry *entry;
{
register int len;
struct exec *loc = &entry->header;
lseek (desc, entry->starting_offset, 0);
#ifdef _CONVEX_SOURCE
/* This is a big-time kluge to fit SOFF into BSDOFF */
{
register int i, nscns;
struct scnhdr *shptr;
struct opthdr oh;
struct filehdr fh;
/* Read filehdr */
#if 0
lseek( desc, 0, SEEK_SET );
#endif
len = read (desc, &fh, sizeof( struct filehdr ) );
if ( len != sizeof ( struct filehdr ) )
fatal (DLD_EBADHEADER);
/* Read opthdr */
len = read (desc, &oh, sizeof( struct opthdr ) );
if ( len != sizeof ( struct opthdr ) )
fatal (DLD_EBADHEADER);
/* Make everyone happy! */
loc->a_magic = fh.h_magic;
loc->a_entry = oh.o_entry;
loc->a_syms = (oh.o_nsyms) * sizeof( struct nlist );
loc->a_trsize = 0;
loc->a_drsize = 0;
loc->a_tdrsize = 0;
loc->cnx_symoff = oh.o_symptr;
loc->cnx_stroff = fh.h_strptr;
entry->string_size = fh.h_strsiz;
nscns = fh.h_nscns;
/* Read section header(s) */
/* Position file pointer at first section */
lseek( desc, (entry->starting_offset) + (unsigned int) fh.h_scnptr, SEEK_SET);
/* Read all the section headers */
len = nscns * sizeof( struct scnhdr );
shptr = (struct scnhdr *)_dld_malloc( len );
if ( len != read (desc, shptr, len ) )
{
free (shptr);
fatal (DLD_EBADHEADER);
}
while( nscns-- > 0 )
{
switch( shptr->s_flags )
{
case S_TEXT:
loc->a_text = shptr->s_size;
loc->cnx_txtoff = shptr->s_scnptr;
loc->a_trsize += shptr->s_nrel * sizeof( struct relocation_info );
break;
case S_DATA:
loc->a_data = shptr->s_size;
loc->cnx_datoff = shptr->s_scnptr;
loc->a_drsize += shptr->s_nrel * sizeof( struct relocation_info );
break;
case S_TDATA:
loc->a_tdata = shptr->s_size;
loc->cnx_tdatoff = shptr->s_scnptr;
loc->a_tdrsize += shptr->s_nrel * sizeof( struct relocation_info );
break;
case S_BSS:
loc->a_bss = shptr->s_size;
break;
case S_TBSS:
loc->a_tbss = shptr->s_size;
break;
default:
break;
}
shptr++;
} /* endwhile( nscns-- > 0 ) */
} /* end convex block */
#else
len = read (desc, loc, sizeof (struct exec));
if (len != sizeof (struct exec))
fatal (DLD_EBADHEADER);
#endif /* _CONVEX_SOURCE */
if (N_BADMAG (*loc))
fatal (DLD_EBADMAGIC);
entry->header_read_flag = 1;
} /* read_header */
#ifdef atarist
#include <st-out.h>
static void
st_read_header (desc, entry)
int desc;
register struct file_entry *entry;
{
register int len;
struct exec *loc = &entry->header;
struct aexec file_header;
lseek (desc, 0L, 0);
len = read (desc, &file_header, sizeof (struct aexec));
if (len != sizeof (struct aexec))
fatal (DLD_EBADHEADER);
if (A_BADMAG (file_header))
fatal (DLD_EBADMAGIC);
if (!file_header.a_AZero2)
fatal (DLD_ENOSTRINGS);
bzero (loc, sizeof (struct exec));
loc->a_magic = NMAGIC; /* flag different header size */
loc->a_text = file_header.a_text;
loc->a_data = file_header.a_data;
loc->a_bss = file_header.a_bss;
loc->a_syms = file_header.a_syms - file_header.a_AZero2;
entry->header_read_flag = 1;
} /* st_read_header */
#endif /* atarist */
/* Read the symbols of file ENTRY into core.
Assume it is already open, on descriptor DESC.
Also read the length of the string table, which follows the symbol table,
but don't read the contents of the string table. */
static void
read_entry_symbols (desc, entry)
struct file_entry *entry;
int desc;
{
int str_size;
if (!entry->header_read_flag)
read_header (desc, entry);
/* Get symbol table */
if (entry->header.a_syms <= 0)
fatal (DLD_ENOSYMBOLS);
entry->symbols = (struct nlist *) _dld_malloc (entry->header.a_syms);
lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
if (entry->header.a_syms !=
read (desc, entry->symbols, entry->header.a_syms)) {
free (entry->symbols);
entry->symbols = 0;
fatal (DLD_ENOSYMBOLS);
}
/* Get size of string table */
#ifdef _CONVEX_SOURCE
/* Already done in read_header, so why bother with it here? */
#else
lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
if (sizeof str_size != read (desc, &str_size, sizeof str_size)) {
free (entry->symbols);
entry->symbols = 0;
fatal (DLD_ENOSTRINGS);
}
entry->string_size = str_size;
#endif /* _CONVEX_SOURCE */
} /* read_entry_symbols */
/* Read the string table of file ENTRY into core.
Assume it is already open, on descriptor DESC. */
static void
read_entry_strings (desc, entry)
struct file_entry *entry;
int desc;
{
if (!entry->header_read_flag)
read_header (desc, entry);
entry->strings = (char *) _dld_malloc (entry->string_size);
lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
if (entry->string_size != read (desc, entry->strings,
entry->string_size)) {
free (entry->strings);
entry->strings = 0;
fatal (DLD_ENOSTRINGS);
}
} /* read_entry_strings */
/* Verify the validity of the relocation information.
DATA_SIZE is the length of the contents.
RELOC_INFO is the address of the relocation info, in core.
RELOC_SIZE is its length in bytes.
If everything is ok, return the number of external relocation entry.
Otherwise, return -1. */
int
reloc_info_ok (data_size, reloc_info, reloc_size, sym_size)
register int data_size;
register struct relocation_info *reloc_info;
register int reloc_size;
register int sym_size;
{
register struct relocation_info *p = reloc_info;
register struct relocation_info *end = p +
reloc_size / sizeof (struct relocation_info);
register int extern_count = 0; /* number of external relocation */
for (; p < end; p++) {
register int symbolnum = RELOC_SYMBOL(p);
if (RELOC_ADDRESS(p) >= data_size)
return -1;
if (RELOC_EXTERN_P(p)) {
extern_count++;
if (symbolnum * sizeof (struct nlist) >= sym_size)
return -1;
} else if (symbolnum != N_TEXT && symbolnum != (N_TEXT | N_EXT) &&
symbolnum != N_DATA && symbolnum != (N_DATA | N_EXT) &&
#ifdef _CONVEX_SOURCE
symbolnum != N_TDATA && symbolnum != (N_TDATA | N_EXT) &&
symbolnum != N_TBSS && symbolnum != (N_TBSS | N_EXT) &&
#endif /*_CONVEX_SOURCE */
symbolnum != N_BSS && symbolnum != (N_BSS | N_EXT))
return -1;
if (RELOC_TARGET_SIZE(p) > 2) return -1;
}
return extern_count++; /* this postincrement does nothing */
/* but preincrement causes bug */
} /* reloc_info_ok */
/* symbol table management */
/* Compute the hash code for symbol name KEY. */
static int
hash_string (key)
const char *key;
{
register const char *cp;
register int k;
cp = key;
k = 0;
while (*cp)
k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
return k;
} /* hash_string */
/* Get the symbol table entry for the global symbol named KEY.
Create one if there is none. */
symbol *
_dld_getsym (key)
const char *key;
{
register int hashval;
register symbol *bp;
/* Determine the proper bucket. */
hashval = hash_string (key) % TABSIZE;
/* Search the bucket. */
for (bp = _dld_symtab[hashval]; bp; bp = bp->link)
if (! strcmp (key, bp->name))
return bp;
/* Nothing was found; create a new symbol table entry. */
bp = (symbol *) _dld_malloc (sizeof (symbol));
bzero (bp, sizeof (symbol));
bp->name = (char *) _dld_malloc (strlen (key) + 1);
strcpy (bp->name, key);
/* Add the entry to the bucket. */
bp->link = _dld_symtab[hashval];
_dld_symtab[hashval] = bp;
return bp;
} /* _dld_getsym */
/* Like `_dld_getsym' but return 0 if the symbol is not already known. */
symbol *
_dld_getsym_soft (key)
const char *key;
{
register int hashval;
register symbol *bp;
/* Determine which bucket. */
hashval = hash_string (key) % TABSIZE;
/* Search the bucket. */
for (bp = _dld_symtab[hashval]; bp; bp = bp->link)
if (! strcmp (key, bp->name))
return bp;
return 0;
} /* _dld_getsym_soft */
/* Enter one global symbol in the hash table.
NLIST_P points to the `struct nlist' read from the file
that describes the global symbol. NAME is the symbol's name.
ENTRY is the file entry for the file the symbol comes from.
The `struct nlist' is modified by making n_name point back to the
corresponding symbols.
For the common definition, the symbol is considered defined by the
first entry that defines it. The problem here is that uninitialized
global variables are all treated as common definitions. Strictly
speacking, in a program there should be exactly one definition
(initialized or uninitialized) of any global variable and all other
files that reference that variable should declare it with the 'extern'
keyword. However, most compiler allows the omission of the 'extern'
keyword, and let the linker map all these definitions to the same
location. In other words, multiple definitions of the same symbol is
allowed. Now, when one of the files that defines such symbol is to be
unlinked, should this symbol become undefined? Or should it remain in
core? The decision made is to treat the symbol defined by the first
file that defines it. All subsequence definitions of the same symbol
will be treated as extern references. However, if any of the
following definition defines the symbol as initialized global, it will
be considered multiple definition and be treated as an error.
*/
void
_dld_enter_global_ref (entry, nlist_p, name)
struct file_entry *entry;
register struct nlist *nlist_p;
const char *name;
{
register symbol *sp = _dld_getsym (name);
register int type = nlist_p->n_type;
register int common = (type == (N_UNDF | N_EXT) && nlist_p->n_value);
int oldref = sp->referenced;
int olddef = sp->defined;
nlist_p->n_un.n_name = (char *) sp;
sp->referenced = 1;
/* common definition */
if (common && !olddef)
#ifndef linux
type = N_COMM | N_EXT;
#else
type = N_TYPE | N_EXT;
#endif
if (type != (N_UNDF | N_EXT)) {
/* definition of a symbol */
if (olddef) {
dld_errname = sp->name;
fatal (DLD_EMULTDEFS);
}
else {
sp->defined = type;
sp->value = common ? (long) _dld_malloc (nlist_p->n_value) :
nlist_p->n_value;
if (common) bzero ((void *)sp->value, nlist_p->n_value);
sp->defined_by = entry;
}
if (oldref && !olddef) {
register struct file_chain *p = sp->referenced_by;
register struct file_chain *prev = 0;
dld_undefined_sym_count--;
/* We do not set up references link to and from the dummy
entry, nor do we touch the ref_count of it.
Furthermore, if a symbol referenced by the dummy entry is
defined, we remove all connections between the dummy entry
and this symbol. */
/* If the dummy entry references anything other than a library
member, this reference is removed. */
while (p) {
if (entry != _dld_dummy_entry) {
if (p->entry == _dld_dummy_entry && entry->superfile)
entry->ref_count++;
else if (insert_entry (&(p->entry->refs), entry) &&
insert_entry (&(entry->refs_by), p->entry) &&
p->entry != entry)
entry->ref_count++;
}
if (p->entry == _dld_dummy_entry) {
/* we force this to be an entry directly linked-in by
the user (top-level module). */
entry->already_unlink = 0;
del_link_list_elt (sp->referenced_by, prev, p, next);
continue;
} else p->entry->undefined_symbol_count--;
prev = p;
p = p->next;
}
}
} else {
/* this is just a reference */
if (sp->defined_by && sp->defined_by != _dld_dummy_entry)
if (entry != _dld_dummy_entry) {
if (insert_entry (&(entry->refs), sp->defined_by) &&
entry != sp->defined_by)
sp->defined_by->ref_count++;
} else if (sp->defined_by->superfile) {
sp->defined_by->ref_count++;
sp->defined_by->already_unlink = 0;
}
if (entry != _dld_dummy_entry) {
if (insert_entry (&(sp->referenced_by), entry) && !sp->defined)
entry->undefined_symbol_count++;
} else {
if (!sp->defined)
insert_entry (&(sp->referenced_by), entry);
}
if (!oldref) {
dld_undefined_sym_count++;
sp->defined = 0;
sp->value = 0;
}
}
}
/* Enter the external symbol defs and refs of ENTRY in the hash table. */
static void
enter_file_symbols (entry)
struct file_entry *entry;
{
register struct nlist *p,
*end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
for (p = entry->symbols; p < end; p++)
#ifdef linux
if (p->n_type & N_EXT) {
if (p->n_type == (N_INDR | N_EXT)) {
symbol *sp;
/* Enter an indirect symbol and the following undefined symbol
as it's reference (see ".../linux/a.out.h") */
/* First enter the referenced symbol */
_dld_enter_global_ref (entry, p+1, (p+1)->n_un.n_strx + entry->strings);
/* Get the address of the corresponding 'struct glosym' */
sp = (symbol *)((p+1)->n_un.n_name);
/* Now enter the indirect symbol (alias) */
_dld_enter_global_ref (entry, p, p->n_un.n_strx + entry->strings);
/* Make 'indirect' point to the referenced symbol */
((symbol *)(p->n_un.n_name))->indirect = sp;
p++;
}
else
_dld_enter_global_ref (entry, p, p->n_un.n_strx + entry->strings);
}
#else
if (p->n_type & N_EXT)
_dld_enter_global_ref (entry, p, p->n_un.n_strx + entry->strings);
#endif
}
/* remove all symbols that are no longer needed */
static void
cleanup_symtab ()
{
register int i;
for (i = 0; i < TABSIZE; i++) {
register symbol *sp = _dld_symtab[i];
register symbol *prev_sp = 0;
while (sp) {
register struct file_chain *p = sp->referenced_by;
register struct file_chain *prev = 0;
register int obsolete = (sp->defined_by &&
sp->defined_by->ref_count == 0);
while (p)
if (p->entry->ref_count == 0) {
del_link_list_elt (sp->referenced_by, prev, p, next);
} else {
if (obsolete)
p->entry->undefined_symbol_count++;
prev = p;
p = p->next;
}
if (obsolete) {
#ifndef linux
if (sp->defined == (N_COMM | N_EXT) && sp->value)
#else
if (sp->defined == (N_TYPE | N_EXT) && sp->value)
#endif
free ((void *)sp->value);
if (sp->referenced_by == 0) {
free (sp->name);
del_link_list_elt (_dld_symtab[i], prev_sp, sp, link);
continue;
} else {
dld_undefined_sym_count++;
sp->defined = 0;
sp->defined_by = 0;
sp->value = 0;
}
}
if (sp->defined == 0 && sp->referenced_by == 0) {
dld_undefined_sym_count--;
free (sp->name);
#ifndef linux
if (sp->defined == (N_COMM | N_EXT) && sp->value)
#else
if (sp->defined == (N_TYPE | N_EXT) && sp->value)
#endif
free ((void *)sp->value);
del_link_list_elt (_dld_symtab[i], prev_sp, sp, link);
continue;
}
prev_sp = sp;
sp = sp->link;
}
}
} /* cleanup_symtab */
/* Searching libraries */
static struct file_entry *decode_library_subfile ();
static void linear_library (), symdef_library ();
static int subfile_wanted_p ();
static void read_text_and_data ();
/* Search the library ENTRY, already open on descriptor DESC.
This means deciding which library members to load,
making a chain of `struct file_entry' for those members,
and entering their global symbols in the hash table. */
static void
search_library (desc, entry)
int desc;
struct file_entry *entry;
{
int member_length;
register char *name;
register struct file_entry *subentry;
if (!dld_undefined_sym_count) return;
/* Examine its first member, which starts SARMAG bytes in. */
subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
if (!subentry) return;
name = subentry->filename;
free (subentry);
/* Search via __.SYMDEF if that exists, else linearly. */
/* There used to be a large, complicated #if dependent on gcc
version number here. But instead, lets just have all linuxes
try both the usual and mutant linux spellings. */
if (strcmp (name, "__.SYMDEF")
#ifdef linux
&& strcmp (name, "__.SYMDEF/")
#endif
)
linear_library (desc, entry);
else
symdef_library (desc, entry, member_length);
free (name);
} /* search_library */
/* Construct and return a file_entry for a library member.
The library's file_entry is library_entry, and the library is open on DESC.
SUBFILE_OFFSET is the byte index in the library of this member's header.
We store the length of the member into *LENGTH_LOC. */
static struct file_entry *
decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
int desc;
struct file_entry *library_entry;
int subfile_offset;
int *length_loc;
{
int bytes_read;
register int namelen;
int member_length;
register char *name;
struct ar_hdr hdr1;
register struct file_entry *subentry;
lseek (desc, subfile_offset, 0);
bytes_read = read (desc, &hdr1, sizeof hdr1);
if (!bytes_read)
return 0; /* end of archive */
if (sizeof hdr1 != bytes_read)
fatal (DLD_EBADLIBRARY);
if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
fatal (DLD_EBADLIBRARY);
subentry = (struct file_entry *) _dld_malloc (sizeof (struct file_entry));
bzero (subentry, sizeof (struct file_entry));
for (namelen = 0;
namelen < sizeof hdr1.ar_name
&& hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' ';
namelen++);
name = (char *) _dld_malloc (namelen+1);
strncpy (name, hdr1.ar_name, namelen);
name[namelen] = 0;
subentry->filename = name;
subentry->local_sym_name = name;
subentry->starting_offset = subfile_offset + sizeof hdr1;
subentry->superfile = library_entry;
subentry->total_size = member_length;
(*length_loc) = member_length;
return subentry;
} /* decode_library_subfile */
/* Search a library that has a __.SYMDEF member.
DESC is a descriptor on which the library is open.
The file pointer is assumed to point at the __.SYMDEF data.
ENTRY is the library's file_entry.
MEMBER_LENGTH is the length of the __.SYMDEF data. */
static void
symdef_library (desc, entry, member_length)
int desc;
struct file_entry *entry;
int member_length;
{
int *symdef_data = (int *) _dld_malloc (member_length);
register struct symdef *symdef_base;
char *sym_name_base;
int number_of_symdefs;
int length_of_strings;
int not_finished;
int bytes_read;
register int i;
struct file_entry *prev = 0;
int prev_offset = 0;
bytes_read = read (desc, symdef_data, member_length);
if (bytes_read != member_length) {
free (symdef_data);
fatal (DLD_EBADLIBRARY);
}
/* Ugly kludge here: save the address for symdef_data in
entry->strings so that if fatal() is called, this memory block will
be freed by clean_up(). If no error occurs, it is freed before this
function returns. I use entry->strings just for convenience and
don't want to add an extra entry in struct file_entry.
Originally storage for symdef_data was
allocated by alloca(), but I want to avoid the use of that. */
entry->strings = (char *) symdef_data;
number_of_symdefs = *symdef_data / sizeof (struct symdef);
if (number_of_symdefs < 0 ||
number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) >=
member_length)
fatal (DLD_EBADLIBRARY);
symdef_base = (struct symdef *) (symdef_data + 1);
length_of_strings = *(int *) (symdef_base + number_of_symdefs);
if (length_of_strings < 0
|| number_of_symdefs * sizeof (struct symdef) + length_of_strings
+ 2 * sizeof (int) != member_length)
fatal (DLD_EBADLIBRARY);
sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
/* Check all the string indexes for validity. */
for (i = 0; i < number_of_symdefs; i++) {
register int index = symdef_base[i].symbol_name_string_index;
if (index < 0 || index >= length_of_strings
|| (index && *(sym_name_base + index - 1)))
fatal (DLD_EBADLIBRARY);
}
/* Search the symdef data for members to load.
Do this until one whole pass finds nothing to load. */
not_finished = 1;
while (not_finished) {
not_finished = 0;
/* Scan all the symbols mentioned in the symdef for ones that we need.
Load the library members that contain such symbols. */
for (i = 0; i < number_of_symdefs && dld_undefined_sym_count; i++)
if (symdef_base[i].symbol_name_string_index >= 0) {
register symbol *sp;
sp = _dld_getsym_soft (sym_name_base +
symdef_base[i].symbol_name_string_index);
/* If we find a symbol that appears to be needed,
think carefully about the archive member that
the symbol is in. */
if (sp && sp->referenced && !sp->defined) {
int junk;
register int j;
register int offset = symdef_base[i].library_member_offset;
struct file_entry *subentry;
/* Don't think carefully about any archive member
more than once in a given pass. */
if (prev_offset == offset)
continue;
prev_offset = offset;
/* Read the symbol table of the archive member. */
subentry = decode_library_subfile (desc, entry, offset, &junk);
if (prev)
prev->chain = subentry;
else entry->subfiles = subentry;
read_entry_symbols (desc, subentry);
read_entry_strings (desc, subentry);
/* Now scan the symbol table and decide whether to load. */
if (!subfile_wanted_p (subentry)) {
if (prev)
prev->chain = 0;
else entry->subfiles = 0;
free (subentry->filename);
free (subentry->symbols);
free (subentry->strings);
free (subentry);
} else {
/* This member is needed; load it.
Since we are loading something on this pass,
we must make another pass through the symdef data. */
not_finished = 1;
/* all library members are considered unlinked
already so that they will be garbage collected
whenever no other modules reference them. */
subentry->already_unlink = 1;
enter_file_symbols (subentry);
free (subentry->strings);
subentry->strings = 0;
read_text_and_data (desc, subentry);
prev = subentry;
/* Clear out this member's symbols from the symdef data
so that following passes won't waste time on them. */
for (j = 0; j < number_of_symdefs; j++) {
if (symdef_base[j].library_member_offset == offset)
symdef_base[j].symbol_name_string_index = -1;
}
}
}
}
}
/* refer to "Ugly kludge" above. */
free (entry->strings);
entry->strings = 0;
} /* symdef_library */
/* Search a library that has no __.SYMDEF.
ENTRY is the library's file_entry.
DESC is the descriptor it is open on. */
static void
linear_library (desc, entry)
int desc;
struct file_entry *entry;
{
register struct file_entry *prev = 0;
register int this_subfile_offset = SARMAG;
while (dld_undefined_sym_count) {
int member_length;
register struct file_entry *subentry;
subentry = decode_library_subfile (desc, entry, this_subfile_offset, &member_length);
if (!subentry) return;
read_entry_symbols (desc, subentry);
read_entry_strings (desc, subentry);
if (!subfile_wanted_p (subentry)) {
free (subentry->filename);
free (subentry->symbols);
free (subentry->strings);
free (subentry);
} else {
subentry->already_unlink = 1;
enter_file_symbols (subentry);
free (subentry->strings);
subentry->strings = 0;
read_text_and_data (desc, subentry);
if (prev)
prev->chain = subentry;
else entry->subfiles = subentry;
prev = subentry;
}
this_subfile_offset += member_length + sizeof (struct ar_hdr);
if (this_subfile_offset & 1) this_subfile_offset++;
}
} /* linear_library */
/* ENTRY is an entry for a library member.
Its symbols have been read into core, but not entered.
Return nonzero if we ought to load this member. */
static int
subfile_wanted_p (entry)
struct file_entry *entry;
{
register struct nlist *p;
register struct nlist *end
= entry->symbols + entry->header.a_syms / sizeof (struct nlist);
for (p = entry->symbols; p < end; p++) {
register int type = p->n_type;
if (type & N_EXT && (type != (N_UNDF | N_EXT) || p->n_value)) {
register char *name = p->n_un.n_strx + entry->strings;
register symbol *sp = _dld_getsym_soft (name);
/* If this symbol has not been hashed, we can't be looking
for it. */
if (!sp) continue;
/* We are looking for definition of external symbols or common
blocks that have not been defined but have been referenced */
if (sp->referenced && !sp->defined &&
(type != (N_UNDF | N_EXT) || p->n_value))
return 1;
}
}
return 0;
} /* subfile_wanted_p */
/* Relocate the addresses of the file's symbols. */
static void
relocate_symbol_address (entry)
register struct file_entry *entry;
{
register struct nlist *p;
register struct nlist *end
= entry->symbols + entry->header.a_syms / sizeof (struct nlist);
register int text_relocation, data_relocation, bss_relocation;
#ifdef _CONVEX_SOURCE
text_relocation = entry->text_start_address;
data_relocation = entry->data_start_address;
bss_relocation = entry->bss_start_address;
#else
text_relocation = entry->text_start_address;
data_relocation = entry->data_start_address - entry->header.a_text;
bss_relocation = entry->bss_start_address - entry->header.a_text -
entry->header.a_data;
#endif /* _CONVEX_SOURCE */
for (p = entry->symbols; p < end; p++) {
/* If this belongs to a section,
update it by the section's start address */
register int type = p->n_type;
register symbol *sp;
if ((type & N_EXT) == 0) continue;
sp = (symbol *) p->n_un.n_name;
if (type == (N_TEXT | N_EXT))
sp->value += text_relocation;
else if (type == (N_DATA | N_EXT))
/* A symbol whose value is in the data section
is present in the input file as if the data section
started at an address equal to the length of the
file's text. */
sp->value += data_relocation;
else if (type == (N_BSS | N_EXT))
/* likewise for symbols with value in BSS. */
sp->value += bss_relocation;
}
} /* relocate_symbol_address */
/* Actually performs the relocation of local symbols.
Do it once and for all for each entry.
Then keep only those with external references online.
This function is copied almost directly from perform_relocation (). */
static void
do_local_relocation (data, pc_relocation, reloc_info, dld_reloc_p, reloc_size,
entry)
char *data;
struct relocation_info *reloc_info;
struct dld_reloc_info *dld_reloc_p;
struct file_entry *entry;
int pc_relocation;
int reloc_size;
{
register struct relocation_info *p = reloc_info;
struct relocation_info *end
= reloc_info + reloc_size / sizeof (struct relocation_info);
int text_relocation = entry->text_start_address;
#ifdef _CONVEX_SOURCE
int data_relocation = entry->data_start_address;
int tdata_relocation = entry->tdata_start_address;
int bss_relocation = entry->bss_start_address;
int tbss_relocation = entry->tbss_start_address;
#else
int data_relocation = entry->data_start_address - entry->header.a_text;
int bss_relocation
= entry->bss_start_address - entry->header.a_text - entry->header.a_data;
#endif /* _CONVEX_SOURCE */
for (; p < end; p++) {
register int relocation = 0;
register int addr = RELOC_ADDRESS(p);
register int symbolnum = RELOC_SYMBOL(p);
#if defined(sun) && defined(sparc)
register unsigned int mask = 0;
#else
register int length = RELOC_TARGET_SIZE(p);
#endif
if (RELOC_EXTERN_P(p)) {
register int symindex = symbolnum * sizeof (struct nlist);
dld_reloc_p->sp = ((symbol *)
(((struct nlist *)
(((char *)entry->symbols) + symindex))
->n_un.n_name));
bcopy (p, &(dld_reloc_p->reloc_info),
sizeof (struct relocation_info));
dld_reloc_p++;
continue;
} else switch (symbolnum) {
case N_TEXT:
case N_TEXT | N_EXT:
relocation = text_relocation;
break;
case N_DATA:
case N_DATA | N_EXT:
/* A word that points to beginning of the the data section
initially contains not 0 but rather the "address" of
that section in the input file, which is the length of
the file's text. */
relocation = data_relocation;
break;
case N_BSS:
case N_BSS | N_EXT:
/* Similarly, an input word pointing to the beginning of
the bss initially contains the length of text plus data
of the file. */
relocation = bss_relocation;
break;
#ifdef _CONVEX_SOURCE
case N_TDATA:
case N_TDATA|N_EXT:
relocation = tdata_relocation;
break;
case N_TBSS:
case N_TBSS|N_EXT:
relocation = tbss_relocation;
break;
#if 0
case N_COMM:
case N_COMM|N_EXT:
case N_TCDATA:
case N_TCDATA|N_EXT:
case N_TCBSS:
case N_TCBSS|N_EXT:
break;
#endif /* These may need to be considered at some point */
#endif /* _CONVEX_SOURCE */
case N_ABS:
case N_ABS | N_EXT:
/* just in case */
break;
default:
break;
}
if (RELOC_PCREL_P(p))
relocation -= pc_relocation;
#if defined(sun) && defined(sparc)
relocation += RELOC_ADD_EXTRA(p);
relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
/* Unshifted mask for relocation */
mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
mask |= mask - 1;
relocation &= mask;
switch (RELOC_TARGET_SIZE(p)) {
case 0:
*(char *) (data + addr) &= ~mask;
*(char *) (data + addr) |= relocation;
break;
case 1:
*(short *) (data + addr) &= ~mask;
*(short *) (data + addr) |= relocation;
break;
case 2:
*(long *) (data + addr) &= ~mask;
*(long *) (data + addr) |= relocation;
break;
default: break;
}
#else
switch (length) {
case 0: *(char *) (data + addr) += relocation;
break;
case 1: *(short *) (data + addr) += relocation;
break;
case 2: *(int *) (data + addr) += relocation;
break;
default: break;
}
#endif
}
} /* do_local_relocation */
/* Read the relocation information of file ENTRY into core.
Assume it is already open, on descriptor DESC.
Then relocate all the local (non-external) symbols. Save only the
relocation info for the external symbol references.
Update entry->header.a_{trsize,drsize} to reflect the new relocation
table size (in bytes).
*/
#ifndef _CONVEX_SOURCE
static void
relocate_local_refs (desc, entry)
int desc;
struct file_entry *entry;
{
int text_offset;
/* number of relocation info that describes an external references. */
int tr_entry_count = 0, dr_entry_count = 0;
struct relocation_info *reloc_buf;
if (!entry->header_read_flag)
read_header (desc, entry);
text_offset = entry->starting_offset + N_TXTOFF (entry->header);
/* For the text segment */
if (entry->header.a_trsize) {
reloc_buf = (struct relocation_info *)
_dld_malloc (entry->header.a_trsize);
lseek (desc, text_offset + entry->header.a_text +
entry->header.a_data, 0);
if (entry->header.a_trsize !=
read (desc, reloc_buf, entry->header.a_trsize)) {
free (reloc_buf);
fatal (DLD_ENOTXTRELOC);
}
if ((tr_entry_count =
reloc_info_ok (entry->header.a_text, reloc_buf,
entry->header.a_trsize, entry->header.a_syms)
) == -1) {
free (reloc_buf);
fatal (DLD_EBADRELOC);
} else {
entry->text_reloc = (struct dld_reloc_info *)
_dld_malloc (tr_entry_count * sizeof (struct dld_reloc_info));
do_local_relocation (entry->text_start_address,
entry->text_start_address,
reloc_buf, entry->text_reloc,
entry->header.a_trsize, entry);
free (reloc_buf);
}
}
/* For the data segment */
if (entry->header.a_drsize) {
reloc_buf = (struct relocation_info *)
_dld_malloc (entry->header.a_drsize);
lseek (desc, text_offset + entry->header.a_text +
entry->header.a_data + entry->header.a_trsize, 0);
if (entry->header.a_drsize !=
read (desc, reloc_buf, entry->header.a_drsize)) {
free (reloc_buf);
fatal (DLD_ENODATRELOC);
}
if ((dr_entry_count =
reloc_info_ok (entry->header.a_data, reloc_buf,
entry->header.a_drsize, entry->header.a_syms)
) == -1) {
free (reloc_buf);
fatal (DLD_EBADRELOC);
} else {
entry->data_reloc = (struct dld_reloc_info *)
_dld_malloc (dr_entry_count * sizeof (struct dld_reloc_info));
do_local_relocation (entry->data_start_address,
entry->data_start_address -
entry->header.a_data,
reloc_buf, entry->data_reloc,
entry->header.a_drsize, entry);
free (reloc_buf);
}
}
entry->header.a_trsize = tr_entry_count * sizeof (struct dld_reloc_info);
entry->header.a_drsize = dr_entry_count * sizeof (struct dld_reloc_info);
/* Free the nlist array. */
if (entry->symbols) {
free (entry->symbols);
entry->symbols = 0;
}
} /* relocate_local_refs */
#endif /* not _CONVEX_SOURCE */
#ifdef _CONVEX_SOURCE
static void
relocate_local_refs (desc, entry)
int desc;
struct file_entry *entry;
{
int text_offset;
/* number of relocation info that describes an external references. */
int tr_entry_count = 0, dr_entry_count = 0, tdr_entry_count = 0;
int nscns, size;
struct exec *loc = &entry->header;
struct filehdr fh;
struct scnhdr *shptr, *shstart, *shend;
struct relocation_info *reloc_buf;
if (!entry->header_read_flag)
read_header (desc, entry);
/****************
* Read filehdr *
****************/
lseek( desc, (entry->starting_offset), SEEK_SET );
if ( read( desc, &fh, sizeof( struct filehdr ) ) != sizeof ( struct filehdr ) )
fatal (DLD_EBADHEADER);
/* Position file pointer at first section */
size = fh.h_nscns * sizeof( struct scnhdr );
shstart = (struct scnhdr *) _dld_malloc( size );
shend = shstart + fh.h_nscns;
/* Read the section headers */
lseek( desc, (entry->starting_offset) + (unsigned int) fh.h_scnptr, SEEK_SET);
if ( size != read (desc, shstart, size) )
fatal (DLD_EBADHEADER);
/* Loop through the section headers, picking up relocation info along
* the way. This is due to difference in SOFF and BSDOFF */
for( shptr=shstart; shptr < shend; shptr++ )
{
if( shptr->s_nrel <= 0 )
continue;
size = shptr->s_nrel * sizeof( struct relocation_info );
reloc_buf = (struct relocation_info *) _dld_malloc( size );
lseek( desc, (entry->starting_offset) +
(unsigned int) (shptr->s_relptr), SEEK_SET );
/* Read the section header */
if ( size != read(desc,reloc_buf,size))
{
free( reloc_buf ); free( shstart );
fatal( DLD_EBADRELOC );
}
switch( shptr->s_flags )
{
case S_TEXT:
{
if ((tr_entry_count = reloc_info_ok(loc->a_text,
reloc_buf, loc->a_trsize, loc->a_syms) ) == -1)
{
free( reloc_buf ); free( shstart );
fatal( DLD_EBADRELOC );
}
entry->text_reloc = (struct dld_reloc_info *)
_dld_malloc (tr_entry_count * sizeof (struct dld_reloc_info));
do_local_relocation (entry->text_start_address,
entry->text_start_address, reloc_buf,
entry->text_reloc, loc->a_trsize, entry);
free( reloc_buf );
break;
} /* end case S_TEXT */
case S_DATA:
{
if ((dr_entry_count = reloc_info_ok(loc->a_data,
reloc_buf, loc->a_drsize, loc->a_syms) ) == -1)
{
free( reloc_buf ); free( shstart );
fatal( DLD_EBADRELOC );
}
entry->data_reloc = (struct dld_reloc_info *)
_dld_malloc( dr_entry_count * sizeof(struct dld_reloc_info));
do_local_relocation (entry->data_start_address,
entry->data_start_address, reloc_buf,
entry->data_reloc, loc->a_drsize, entry);
free( reloc_buf );
break;
} /* end case S_DATA */
case S_TDATA:
{
if ((tdr_entry_count = reloc_info_ok(loc->a_tdata,
reloc_buf, loc->a_tdrsize, loc->a_syms) ) == -1)
{
free( reloc_buf ); free( shstart );
fatal( DLD_EBADRELOC );
}
entry->tdata_reloc = (struct dld_reloc_info *)
_dld_malloc (tdr_entry_count * sizeof (struct dld_reloc_info));
do_local_relocation (entry->tdata_start_address,
entry->tdata_start_address, reloc_buf,
entry->tdata_reloc, loc->a_tdrsize, entry);
free( reloc_buf );
break;
} /* end case S_TDATA */
default:
break;
} /* end switch ( shptr->s_flags ) */
} /* end for(shptr<shend) */
entry->header.a_trsize = tr_entry_count * sizeof (struct dld_reloc_info);
entry->header.a_drsize = dr_entry_count * sizeof (struct dld_reloc_info);
entry->header.a_tdrsize = tdr_entry_count * sizeof (struct dld_reloc_info);
/* Free the nlist array */
if (entry->symbols) {
free (entry->symbols);
entry->symbols = 0;
}
} /* relocate_local_refs */
#endif /* _CONVEX_SOURCE */
/* Relocate ENTRY's text or data section contents.
DATA is the address of the contents, in core.
DATA_SIZE is the length of the contents.
PC_RELOCATION is the difference between the address of the contents
in the output file and its address in the input file.
RELOC_INFO is the address of the relocation info, in core.
RELOC_SIZE is its length in bytes.
REVERSE is true when an 'un-relocation' is to be done.
*/
static void
perform_relocation (data, pc_relocation, reloc_info, reloc_size, reverse)
char *data;
struct dld_reloc_info *reloc_info;
int pc_relocation;
int reloc_size;
int reverse;
{
register struct dld_reloc_info *p = reloc_info;
struct dld_reloc_info *end
= reloc_info + reloc_size / sizeof (struct dld_reloc_info);
#if defined(sun) && defined(sparc)
if (reverse) return;
#endif
for (; p < end; p++) {
#ifdef linux
register int relocation;
register symbol *sp = p->sp;
#else
register int relocation = p->sp->value;
#endif
register struct relocation_info *r = &(p->reloc_info);
register int addr = RELOC_ADDRESS(r);
#if defined(sun) && defined(sparc)
register unsigned int mask = 0;
#else
register int length = RELOC_TARGET_SIZE(r);
#endif
#ifdef linux
while (sp->indirect) sp = sp->indirect;
relocation = sp->value;
#endif
if (RELOC_PCREL_P(r))
relocation -= pc_relocation;
#if defined(sun) && defined(sparc)
relocation += RELOC_ADD_EXTRA(r);
relocation >>= RELOC_VALUE_RIGHTSHIFT(r);
/* Unshifted mask for relocation */
mask = 1 << RELOC_TARGET_BITSIZE(r) - 1;
mask |= mask - 1;
relocation &= mask;
switch (RELOC_TARGET_SIZE(r)) {
case 0:
*(char *) (data + addr) &= ~mask;
*(char *) (data + addr) |= relocation;
break;
case 1:
*(short *) (data + addr) &= ~mask;
*(short *) (data + addr) |= relocation;
break;
case 2:
*(long *) (data + addr) &= ~mask;
*(long *) (data + addr) |= relocation;
break;
default: break;
}
#else
if (reverse) relocation = - relocation;
switch (length) {
case 0:
if (RELOC_MEMORY_SUB_P(r))
*(char *) (data + addr) = relocation - *(char *) (data + addr);
else *(char *) (data + addr) += relocation;
break;
case 1:
if (RELOC_MEMORY_SUB_P(r))
*(short *) (data + addr) =
relocation - *(short *) (data + addr);
else *(char *) (data + addr) += relocation;
break;
case 2:
if (RELOC_MEMORY_SUB_P(r))
*(int *) (data + addr) = relocation - *(int *) (data + addr);
else *(int *) (data + addr) += relocation;
break;
default: break;
}
#endif
}
} /* perform_relocation */
/* ABSOLUTE_FILENAME_P (fname): True if fname is an absolute filename */
#ifdef atarist
#define ABSOLUTE_FILENAME_P(fname) ((fname[0] == '/') || \
(fname[0] && (fname[1] == ':')))
#else
#define ABSOLUTE_FILENAME_P(fname) (fname[0] == '/')
#endif /* atarist */
/* given a file name, create an appropriate file_entry for it */
static struct file_entry *
make_entry (filename)
const char *filename;
{
register struct file_entry *entry =
(struct file_entry *) _dld_malloc (sizeof (struct file_entry));
bzero (entry, sizeof (struct file_entry));
entry->filename = entry->local_sym_name =
(char *) _dld_malloc (strlen (filename) + 1);
strcpy (entry->local_sym_name, filename);
if (!ABSOLUTE_FILENAME_P(filename)) {
char name[MAXPATHLEN];
entry->filename = concat (getwd(name), "/", filename);
}
entry->chain = _dld_latest_entry;
entry->ref_count = 1;
return entry;
} /* make_entry */
/* If ENTRY is a rel file, read its symbol and string sections into core.
If it is a library, search it and load the appropriate members
(which means calling this function recursively on those members). */
static void
read_file_symbols (desc, entry, load_text)
register int desc;
register struct file_entry *entry;
int load_text; /* used only by dld_init */
{
register int len;
int magicnum;
len = read (desc, &magicnum, sizeof magicnum);
if (len != sizeof magicnum)
fatal (DLD_EBADHEADER);
#ifdef atarist
if (!load_text || !N_BADMAG (*((struct exec *)&magicnum))) {
#else
if (!N_BADMAG (*((struct exec *)&magicnum))) {
#endif
read_entry_symbols (desc, entry);
read_entry_strings (desc, entry);
enter_file_symbols (entry);
free (entry->strings);
entry->strings = 0;
if (load_text) read_text_and_data (desc, entry);
} else {
char armag[SARMAG];
lseek (desc, 0, 0);
if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
fatal (DLD_EBADOBJECT);
entry->library_flag = 1;
search_library (desc, entry);
}
} /* read_file_symbols */
/* Allocate memory for all text, data and bss segments and read them in
from the file. */
static void
read_text_and_data (desc, entry)
int desc;
register struct file_entry *entry;
{
#ifdef _CONVEX_SOURCE
int size, len, pagesize, npages;
int align_text, align_data, align_tdata, align_bss, align_tbss;
struct exec *loc = &entry->header;
if ( loc->a_text & 0x07 )
align_text = loc->a_text + (8 - (loc->a_text & 0x07));
else
align_text = loc->a_text;
if ( loc->a_data & 0x07 )
align_data = loc->a_data + (8 - (loc->a_data & 0x07));
else
align_data = loc->a_data;
if ( loc->a_tdata & 0x07 )
align_tdata = loc->a_tdata + (8 - (loc->a_tdata & 0x07));
else
align_tdata = loc->a_tdata;
if ( loc->a_bss & 0x07 )
align_bss = loc->a_bss + (8 - (loc->a_bss & 0x07));
else
align_bss = loc->a_bss;
if ( loc->a_tbss & 0x07 )
align_tbss = loc->a_tbss + (8 - (loc->a_tbss & 0x07));
else
align_tbss = loc->a_tbss;
size = align_text + align_data + align_tdata + align_bss + align_tbss;
/* Pass mmap a value that is a multiple of pagesize */
pagesize = getpagesize();
npages = size / pagesize;
if ( size > ( npages * pagesize ) ) npages++;
len = npages * pagesize;
entry->text_start_address = (int)mmap( (caddr_t)NULL, &len,
PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON , NOFD, (off_t)0 );
if ( (int)(entry->text_start_address) == -1)
fatal (DLD_ENOMEMORY);
if (size - align_text > 0) {
entry->data_start_address = entry->text_start_address + align_text;
entry->tdata_start_address = entry->data_start_address + align_data;
entry->bss_start_address = entry->tdata_start_address + align_tdata;
entry->tbss_start_address = entry->bss_start_address + align_bss;
} else {
entry->data_start_address = entry->bss_start_address = 0;
entry->tdata_start_address = entry->tbss_start_address = 0;
}
/* Read text and data sections into core.
* Note that the bss segment does not actually take up space in the
* object file, so its size must be subtracted from SIZE
*/
/* Read text */
lseek (desc, entry->starting_offset + N_TXTOFF(entry->header), 0);
if (loc->a_text != read (desc, (void *) entry->text_start_address, loc->a_text))
{
munmap( entry->text_start_address , size );
entry->text_start_address = entry->data_start_address =
entry->bss_start_address = 0;
entry->tdata_start_address = entry->tbss_start_address = 0;
fatal (DLD_ENODATA);
}
/* Read data */
if ( loc->a_data > 0 )
{
lseek (desc, entry->starting_offset + loc->cnx_datoff, 0);
if (loc->a_data != read (desc, (void *) entry->data_start_address, loc->a_data))
{
munmap( entry->text_start_address , size );
entry->text_start_address = entry->data_start_address =
entry->bss_start_address = 0;
entry->tdata_start_address = entry->tbss_start_address = 0;
fatal (DLD_ENODATA);
}
}
/* Read tdata */
if ( loc->a_tdata > 0 )
{
lseek (desc, entry->starting_offset + loc->cnx_tdatoff, 0);
if (loc->a_tdata != read (desc, (void *) entry->tdata_start_address, loc->a_tdata))
{
munmap( entry->text_start_address , size );
entry->text_start_address = entry->data_start_address =
entry->bss_start_address = 0;
entry->tdata_start_address = entry->tbss_start_address = 0;
fatal (DLD_ENODATA);
}
}
/* zero the bss segment */
if (loc->a_bss)
bzero ((void *) entry->bss_start_address, loc->a_bss);
if (loc->a_tbss)
bzero ((void *) entry->tbss_start_address, loc->a_tbss);
#else
register size = entry->header.a_text + entry->header.a_data +
entry->header.a_bss;
entry->text_start_address = _dld_malloc (size);
if (size - entry->header.a_text > 0) {
entry->data_start_address = entry->text_start_address +
entry->header.a_text;
entry->bss_start_address = entry->data_start_address +
entry->header.a_data;
} else entry->data_start_address = entry->bss_start_address = 0;
/* Read text and data sections into core.
Note that the bss segment does not actually take up space in the
object file, so its size must be subtracted from SIZE */
lseek (desc, entry->starting_offset + N_TXTOFF(entry->header), 0);
size -= entry->header.a_bss;
if (size != read (desc, (char *)entry->text_start_address, size)) {
free ((void *)entry->text_start_address);
entry->text_start_address = entry->data_start_address =
entry->bss_start_address = 0;
fatal (DLD_ENODATA);
}
/* zero the bss segment */
if (entry->header.a_bss)
bzero ((void *) entry->bss_start_address, entry->header.a_bss);
#endif /* _CONVEX_SOURCE */
} /* read_text_and_data */
/* Allocate memory for the text and data segments and relocate all local
symbols */
static void
relocate_entry_symbols (desc, entry)
int desc;
register struct file_entry *entry;
{
/* Compute start addresses of each sections and symbols. */
if (entry->library_flag) {
register struct file_entry *subentry = entry->subfiles;
for (; subentry; subentry = subentry->chain) {
relocate_symbol_address (subentry);
relocate_local_refs (desc, subentry);
}
} else {
relocate_symbol_address (entry);
relocate_local_refs (desc, entry);
}
} /* relocate_entry_symbols */
/* Find all modules have all external references defined but not resolved. */
void
_dld_patch_all_files (entry)
register struct file_entry *entry;
{
while (entry) {
if (entry->library_flag)
_dld_patch_all_files (entry->subfiles);
else if (!entry->all_symbols_resolved_flag &&
entry->undefined_symbol_count == 0) {
/* entry whose global references have just been resolved */
perform_relocation (entry->text_start_address,
entry->text_start_address,
entry->text_reloc,
entry->header.a_trsize, 0);
perform_relocation (entry->data_start_address,
entry->data_start_address -
entry->header.a_data,
entry->data_reloc,
entry->header.a_drsize, 0);
entry->all_symbols_resolved_flag = 1;
} else if (entry->all_symbols_resolved_flag &&
entry->undefined_symbol_count != 0) {
/* entry that has some of its global references being
"un-defined" */
perform_relocation (entry->text_start_address,
entry->text_start_address,
entry->text_reloc,
entry->header.a_trsize, 1);
perform_relocation (entry->data_start_address,
entry->data_start_address -
entry->header.a_data,
entry->data_reloc,
entry->header.a_drsize, 1);
entry->all_symbols_resolved_flag = 0;
}
entry = entry->chain;
}
} /* _dld_patch_all_files */
/*
* reset the executable_flag of the given entry, and then recursively
* propagate this to all modules that reference symbols in this entry.
*/
static void Invalidate (entry)
struct file_entry *entry;
{
register struct file_chain *p;
if (entry == 0)
return;
entry->executable_flag = 0;
for (p = entry->refs_by; p; p = p->next)
if (p->entry->executable_flag)
Invalidate (p->entry);
} /* invalidate */
/*
* For all modules loaded, determine which of them can be safely executed.
* For those that can, set the flag executable_flag.
*/
static void
find_all_executable_modules ()
{
register struct file_entry *p;
/* set all executable flags */
for (p = _dld_latest_entry; p ; p = p->chain) {
if (p->library_flag) {
register struct file_entry *q = p->subfiles;
while (q) {
q->executable_flag = q->all_symbols_resolved_flag;
q = q->chain;
}
}
p->executable_flag = p->all_symbols_resolved_flag;
}
/* invalidate those modules that are not (yet) executable. */
for (p = _dld_latest_entry; p; p = p->chain) {
if (p->library_flag) {
register struct file_entry *q = p->subfiles;
while (q) {
if (!q->all_symbols_resolved_flag && q->refs_by)
Invalidate (q);
q = q->chain;
}
} else if (!p->all_symbols_resolved_flag && p->refs_by)
Invalidate (p);
}
_dld_exec_flags_valid = 1;
} /* find_all_executable_modules */
/* remove all reference pointers *TO* ENTRY */
static void
remove_cross_references (head_of_chain, entry)
struct file_entry *head_of_chain, *entry;
{
register struct file_entry *ep = head_of_chain;
while (ep) {
register struct file_chain *p = ep->refs;
if (ep->library_flag)
remove_cross_references (ep->subfiles, entry);
else {
register struct file_chain *prev = 0;
while (p) {
if (p->entry == entry || p->entry->superfile == entry) {
del_link_list_elt (ep->refs, prev, p, next);
} else {
prev = p;
p = p->next;
}
}
}
ep = ep->chain;
}
} /* remove_cross_references */
/* remove all cross reference pointers related to an obsolete file entry.
It is assumed that the ref_count of this entry is zero.
Also clear up all other entries that become obsolete when this entry is
removed. */
static void
cleanup_obsolete_entries (entry)
struct file_entry *entry;
{
register struct file_chain *p;
if (entry->ref_count != 0) return;
if (entry->library_flag) {
register struct file_entry *subentry = entry->subfiles;
for (; subentry; subentry = subentry->chain)
cleanup_obsolete_entries (subentry);
}
p = entry->refs;
entry->refs = 0; /* to prevent loop; might be redundant*/
while (p) {
register struct file_chain *next = p->next;
if (p->entry->ref_count) /* ref_count may already be set to zero */
(p->entry->ref_count)--;
if (p->entry->ref_count == 0)
cleanup_obsolete_entries (p->entry);
free (p);
p = next;
}
} /* cleanup_obsolete_entries */
/* remove all memory blocks assigned for ENTRY,
return the pointers to the next entry in chain */
static struct file_entry *
kill_entry (entry)
register struct file_entry *entry;
{
register struct file_chain *p;
if (entry->library_flag) {
register struct file_entry *subentry = entry->subfiles;
while (subentry)
subentry = kill_entry (subentry);
}
p = entry->refs;
while (p) {
register struct file_chain *next_chain = p->next;
free (p);
p = next_chain;
}
p = entry->refs_by;
while (p) {
register struct file_chain *next_chain = p->next;
free (p);
p = next_chain;
}
if (entry->local_sym_name) free (entry->local_sym_name);
if (entry->filename != entry->local_sym_name && entry->filename)
free (entry->filename);
if (entry->symbols) free (entry->symbols);
if (entry->strings) free (entry->strings);
if (entry->text_reloc) free (entry->text_reloc);
if (entry->data_reloc) free (entry->data_reloc);
#if _CONVEX_SOURCE
{
register int size;
struct exec *loc = &entry->header;
if ( loc->a_text & 0x07 )
size = loc->a_text + (8 - (loc->a_text & 0x07));
else
size = loc->a_text;
if ( loc->a_data & 0x07 )
size += loc->a_data + (8 - (loc->a_data & 0x07));
else
size += loc->a_data;
if ( loc->a_tdata & 0x07 )
size += loc->a_tdata + (8 - (loc->a_tdata & 0x07));
else
size += loc->a_tdata;
if ( loc->a_bss & 0x07 )
size += loc->a_bss + (8 - (loc->a_bss & 0x07));
else
size += loc->a_bss;
if ( loc->a_tbss & 0x07 )
size += loc->a_tbss + (8 - (loc->a_tbss & 0x07));
else
size += loc->a_tbss;
if ( entry->text_start_address && (size > 0 ))
munmap ( entry->text_start_address, size );
}
/* Clean up after ourselves... */
entry->local_sym_name = 0;
entry->filename = 0;
entry->symbols = 0;
entry->strings = 0;
entry->text_reloc = 0;
entry->data_reloc = 0;
entry->text_start_address = 0;
#else
if (entry->text_start_address) free ((void *)entry->text_start_address);
#endif /* _CONVEX_SOURCE */
{
register struct file_entry *next = entry->chain;
free (entry);
return (next);
}
} /* kill_entry */
/* clean all data structures so that they return to the original states
after last call to dld */
static void
clean_up ()
{
if (!_dld_latest_entry)
return;
_dld_latest_entry->ref_count = 0;
if (_dld_latest_entry->library_flag) {
register struct file_entry *subentry = _dld_latest_entry->subfiles;
for (; subentry; subentry = subentry->chain)
subentry->ref_count = 0;
}
remove_cross_references (_dld_latest_entry, _dld_latest_entry);
cleanup_obsolete_entries (_dld_latest_entry);
cleanup_symtab ();
_dld_latest_entry = kill_entry (_dld_latest_entry);
} /* clean_up */
/* Actually perform the unlink operation.
Search through the list of file entries, unlink those whose ref_count is
zero. */
static struct file_entry *
do_unlink (entry)
struct file_entry *entry;
{
register struct file_entry *p = entry;
register struct file_entry *prev = 0;
if (p == 0) return p;
while (p) {
if (p->library_flag) {
p->subfiles = do_unlink (p->subfiles);
/* if all subentries are gone, remove itself */
if (p->subfiles == 0)
p->ref_count = 0;
}
if (p->ref_count == 0) {
register struct file_entry *next;
next = kill_entry (p);
if (prev == 0) {
entry = next;
p = entry;
} else {
prev->chain = next;
p = prev->chain;
}
} else {
prev = p;
p = p->chain;
}
}
_dld_exec_flags_valid = 0;
return entry;
} /* do_unlink */
int
dld_init (myname)
const char *myname;
{
int desc;
page_size = getpagesize ();
bzero (_dld_symtab, TABSIZE * sizeof(symbol *));
_dld_latest_entry = 0;
_dld_dummy_entry = 0;
dld_undefined_sym_count = 0;
_dld_exec_flags_valid = 0;
if (myname == 0) {
dld_errno = DLD_ENOFILE;
return dld_errno;
}
if (setjmp (_dld_env)) {
clean_up ();
file_close ();
return dld_errno;
}
_dld_latest_entry = make_entry (myname);
desc = file_open (_dld_latest_entry);
#ifdef atarist
st_read_header (desc, _dld_latest_entry);
#endif /* atarist */
read_file_symbols (desc, _dld_latest_entry, 0);
#ifdef atarist
/* do a consistency check of data in entry against data in _base */
if ((_dld_latest_entry->header.a_text != _base->p_tlen) ||
(_dld_latest_entry->header.a_data != _base->p_dlen) ||
(_dld_latest_entry->header.a_bss != _base->p_blen)) {
if (_dld_latest_entry->symbols) {
free (_dld_latest_entry->symbols);
_dld_latest_entry->symbols = 0;
}
file_close ();
dld_errno = DLD_EBADHEADER;
return dld_errno;
}
_dld_latest_entry->text_start_address = (int) _base->p_tbase;
_dld_latest_entry->data_start_address = (int) _base->p_dbase;
_dld_latest_entry->bss_start_address = (int) _base->p_bbase;
relocate_symbol_address (_dld_latest_entry);
#endif /* atarist */
if (_dld_latest_entry->symbols) {
free (_dld_latest_entry->symbols);
_dld_latest_entry->symbols = 0;
}
file_close ();
_dld_latest_entry->undefined_symbol_count = 0;
_dld_latest_entry->all_symbols_resolved_flag = 1;
return 0;
} /* dld_init */
int
dld_link (object_file)
const char *object_file;
{
register int desc;
struct file_entry *old_latest_entry = _dld_latest_entry;
if (setjmp (_dld_env)) {
if (old_latest_entry != _dld_latest_entry)
clean_up ();
file_close ();
return dld_errno;
}
dld_errno = 0;
file_close (); /* file might be opened in the last
call */
if (object_file == 0) {
dld_errno = DLD_ENOFILE;
return dld_errno;
}
_dld_latest_entry = make_entry (object_file);
desc = file_open (_dld_latest_entry);
read_file_symbols (desc, _dld_latest_entry, 1);
if (_dld_latest_entry->library_flag && _dld_latest_entry->subfiles == 0) {
free (_dld_latest_entry->local_sym_name);
if (_dld_latest_entry->filename != _dld_latest_entry->local_sym_name)
free (_dld_latest_entry->filename);
_dld_latest_entry->filename = NULL;
_dld_latest_entry->local_sym_name = NULL;
_dld_latest_entry = kill_entry (_dld_latest_entry);
file_close ();
return 0;
}
relocate_entry_symbols (desc, _dld_latest_entry);
file_close ();
_dld_patch_all_files (_dld_latest_entry);
_dld_exec_flags_valid = 0;
return 0;
} /* dld_link */
/* return the location of the given symbol without prepending a '_'. */
unsigned long
dld_get_bare_symbol (name)
const char *name;
{
register symbol *sp;
if (name == 0)
return 0;
sp = _dld_getsym_soft (name);
#ifdef linux
if (sp) {
while (sp->indirect) sp = sp->indirect;
if (sp->defined)
return sp->value;
}
#else
if (sp)
if (sp->defined)
return sp->value;
#endif
return 0;
} /* dld_get_bare_symbol */
/* given a file_entry, unlink that (and all its decendents). Modules still
referenced by the remainings will not be unlinked.
ENTRY is assumed to be a valid pointer to a file_entry structure.
if FORCE is true, remove this entry event regardless if it is still
referenced by others */
void
_dld_unlink_entry (entry, force)
struct file_entry *entry;
int force;
{
if (force) {
entry->ref_count = 0;
if (entry->library_flag) {
register struct file_entry *subentry = entry->subfiles;
for (; subentry; subentry = subentry->chain)
subentry->ref_count = 0;
}
} else {
if (entry->library_flag || entry->already_unlink)
return;
entry->ref_count--;
entry->already_unlink = 1;
}
if (entry->ref_count == 0) {
remove_cross_references (_dld_latest_entry, entry);
cleanup_obsolete_entries (entry);
cleanup_symtab ();
_dld_latest_entry = do_unlink (_dld_latest_entry);
}
_dld_patch_all_files (_dld_latest_entry);
_dld_exec_flags_valid = 0;
} /* _dld_unlink_entry */
/*
* return true if the named function can be safely exeucted.
*/
int
dld_function_executable_p (name)
const char name[];
{
register symbol *sp;
register char *p;
if (name == 0)
return 0;
if (setjmp (_dld_env))
return 0;
/* prepend an '_' to name, as required by the C convention */
p = (char *) _dld_malloc (strlen(name) + 2);
*p = '_';
strcpy (p+1, name);
sp = _dld_getsym_soft (p);
free (p);
#ifdef linux
if (sp && (sp->defined == (N_EXT | N_TEXT) ||
sp->defined == (N_EXT | N_INDR))) {
#else
if (sp && sp->defined == (N_EXT | N_TEXT)) {
#endif
register struct file_entry *fe = sp->defined_by;
if (fe == 0) return 0;
#ifdef linux
while (sp->indirect) sp = sp->indirect;
if (sp->defined != (N_EXT | N_TEXT)) return 0;
#endif
if (!_dld_exec_flags_valid)
find_all_executable_modules ();
return fe->executable_flag;
}
#ifdef linux
if (sp && sp->defined == (N_EXT | N_ABS))
return 1;
#endif
return 0;
} /* dld_function_executable_p */
|