Loading...
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 | /* * Copyright (c) 2001 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights * Reserved. This file contains Original Code and/or Modifications of * Original Code as defined in and that are subject to the Apple Public * Source License Version 1.0 (the 'License'). You may not use this file * except in compliance with the License. Please obtain a copy of the * License at http://www.apple.com/publicsource and read it before using * this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License." * * @APPLE_LICENSE_HEADER_END@ */ /* * History: * 2001-05-30 gvdl Initial implementation of the vtable patcher. */ // 45678901234567890123456789012345678901234567890123456789012345678901234567890 #include <mach-o/fat.h> #include <mach-o/loader.h> #include <mach-o/nlist.h> #include <mach-o/reloc.h> #if KERNEL #include <stdarg.h> #include <string.h> #include <sys/systm.h> #include <libkern/OSTypes.h> #include <libsa/stdlib.h> #include <libsa/mach/mach.h> #include "mach_loader.h" #include <vm/vm_kern.h> enum { false = 0, true = 1 }; #define vm_page_size page_size extern load_return_t fatfile_getarch( void * vp, // normally a (struct vnode *) vm_offset_t data_ptr, struct fat_arch * archret); __private_extern__ char *strstr(const char *in, const char *str); #else /* !KERNEL */ #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/errno.h> #include <sys/fcntl.h> #include <sys/stat.h> #include <sys/mman.h> #include <sys/vm.h> #include <mach/mach.h> #include <mach/mach_error.h> #include <mach-o/arch.h> #include <CoreFoundation/CoreFoundation.h> #define PAGE_SIZE vm_page_size #define PAGE_MASK (PAGE_SIZE - 1) #endif /* KERNEL */ #include "kld_patch.h" #include "c++rem3.h" #if 0 #define DIE() do { for (;;) ; } while(0) #if KERNEL # define LOG_DELAY() /* IODelay(200000) */ # define DEBUG_LOG(x) do { IOLog x; LOG_DELAY(); } while(0) #else # define LOG_DELAY() # define DEBUG_LOG(x) do { printf x; } while(0) #endif #else #define DIE() #define LOG_DELAY() #define DEBUG_LOG(x) #endif // OSObject symbol prefixes and suffixes #define kCPPSymbolPrefix "_Z" #define kVTablePrefix "_" kCPPSymbolPrefix "TV" #define kOSObjPrefix "_" kCPPSymbolPrefix "N" #define kReservedNamePrefix "_RESERVED" #define k29SuperClassSuffix "superClass" #define k31SuperClassSuffix "10superClassE" #define kGMetaSuffix "10gMetaClassE" #define kLinkEditSegName SEG_LINKEDIT // GCC 2.95 drops 2 leading constants in the vtable #define kVTablePreambleLen 2 // Last address that I'm willing to try find vm in #define kTopAddr ((unsigned char *) (1024 * 1024 * 1024)) // Size in bytes that Data Ref object's get increased in size // Must be a power of 2 #define kDataCapacityIncrement 128 // My usual set of helper macros. I personally find these macros // easier to read in the code rather than an explicit error condition // check. If I don't make it easy then I may get lazy ond not check // everything. I'm sorry if you find this code harder to read. // break_if will evaluate the expression and if it is true // then it will print the msg, which is enclosed in parens // and then break. Usually used in loops are do { } while (0) #define break_if(expr, msg) \ if (expr) { \ errprintf msg; \ break; \ } // return_if will evaluate expr and if true it will log the // msg, which is enclosed in parens, and then it will return // with the return code of ret. #define return_if(expr, ret, msg) do { \ if (expr) { \ errprintf msg; \ return ret; \ } \ } while (0) #ifndef MIN #define MIN(a,b) (((a)<(b))?(a):(b)) #endif /* MIN */ #ifndef MAX #define MAX(a,b) (((a)>(b))?(a):(b)) #endif /* MAX */ typedef struct Data { unsigned long fLength, fCapacity; unsigned char *fData; } Data, *DataRef; struct sectionRecord { const struct section *fSection; DataRef fRelocCache; }; enum patchState { kSymbolIdentical, kSymbolLocal, kSymbolPadUpdate, kSymbolSuperUpdate, kSymbolMismatch }; struct patchRecord { struct nlist *fSymbol; enum patchState fType; }; struct relocRecord { void *fValue; const struct nlist *fSymbol; struct relocation_info *fRInfo; void *reserved; }; struct metaClassRecord { char *fSuperName; struct fileRecord *fFile; const struct nlist *fVTableSym; struct patchRecord *fPatchedVTable; char fClassName[1]; }; struct fileRecord { size_t fMapSize, fMachOSize; unsigned char *fMap, *fMachO, *fPadEnd; DataRef fClassList; DataRef fSectData; DataRef fNewSymbols, fNewStringBlocks; DataRef fSym2Strings; struct symtab_command *fSymtab; struct sectionRecord *fSections; struct segment_command *fLinkEditSeg; const char **fSymbToStringTable; char *fStringBase; struct nlist *fSymbolBase; const struct nlist *fLocalSyms; unsigned int fNSects; int fNLocal; Boolean fIsKernel, fNoKernelExecutable, fIsKmem; Boolean fImageDirty, fSymbolsDirty; Boolean fRemangled, fFoundOSObject; Boolean fIgnoreFile; const char fPath[1]; }; static DataRef sFilesTable; static struct fileRecord *sKernelFile; static DataRef sMergedFiles; static DataRef sMergeMetaClasses; static Boolean sMergedKernel; static void errprintf(const char *fmt, ...) { extern void kld_error_vprintf(const char *format, va_list ap); va_list ap; va_start(ap, fmt); kld_error_vprintf(fmt, ap); va_end(ap); DIE(); } static __inline__ unsigned long DataGetLength(DataRef data) { return data->fLength; } static __inline__ unsigned char *DataGetPtr(DataRef data) { return data->fData; } static __inline__ unsigned char *DataGetEndPtr(DataRef data) { return data->fData + data->fLength; } static __inline__ unsigned long DataRemaining(DataRef data) { return data->fCapacity - data->fLength; } static __inline__ Boolean DataContainsAddr(DataRef data, void *vAddr) { vm_offset_t offset = (vm_address_t) vAddr; if (!data) return false; offset = (vm_address_t) vAddr - (vm_address_t) data->fData; return (offset < data->fLength); } static Boolean DataEnsureCapacity(DataRef data, unsigned long capacity) { // Don't bother to ever shrink a data object. if (capacity > data->fCapacity) { unsigned char *newData; capacity += kDataCapacityIncrement - 1; capacity &= ~(kDataCapacityIncrement - 1); newData = (unsigned char *) realloc(data->fData, capacity); if (!newData) return false; bzero(newData + data->fCapacity, capacity - data->fCapacity); data->fData = newData; data->fCapacity = capacity; } return true; } static __inline__ Boolean DataSetLength(DataRef data, unsigned long length) { if (DataEnsureCapacity(data, length)) { data->fLength = length; return true; } else return false; } static __inline__ Boolean DataAddLength(DataRef data, unsigned long length) { return DataSetLength(data, data->fLength + length); } static __inline__ Boolean DataAppendBytes(DataRef data, const void *addr, unsigned int len) { unsigned long size = DataGetLength(data); if (!DataAddLength(data, len)) return false; bcopy(addr, DataGetPtr(data) + size, len); return true; } static __inline__ Boolean DataAppendData(DataRef dst, DataRef src) { return DataAppendBytes(dst, DataGetPtr(src), DataGetLength(src)); } static DataRef DataCreate(unsigned long capacity) { DataRef data = (DataRef) malloc(sizeof(Data)); if (data) { if (!capacity) data->fCapacity = kDataCapacityIncrement; else { data->fCapacity = capacity + kDataCapacityIncrement - 1; data->fCapacity &= ~(kDataCapacityIncrement - 1); } data->fData = (unsigned char *) malloc(data->fCapacity); if (!data->fData) { free(data); return NULL; } bzero(data->fData, data->fCapacity); data->fLength = 0; } return data; } static void DataRelease(DataRef data) { if (data) { if (data->fData) free(data->fData); data->fData = 0; free(data); } } static __inline__ const char * symNameByIndex(const struct fileRecord *file, unsigned int symInd) { return file->fSymbToStringTable[symInd]; } static __inline__ const char * symbolname(const struct fileRecord *file, const struct nlist *sym) { unsigned int index; index = sym - file->fSymbolBase; if (index < file->fSymtab->nsyms) return symNameByIndex(file, index); if (-1 == sym->n_un.n_strx) return (const char *) sym->n_value; // If the preceding tests fail then we have a getNewSymbol patch and // the file it refers to has already been patched as the n_strx is set // to -1 temporarily while we are still processing a file. // Once we have finished with a file then we repair the 'strx' offset // to be valid for the repaired file's string table. return file->fStringBase + sym->n_un.n_strx; } static struct fileRecord * getFile(const char *path) { if (sFilesTable) { int i, nfiles; struct fileRecord **files; // Check to see if we have already merged this file nfiles = DataGetLength(sFilesTable) / sizeof(struct fileRecord *); files = (struct fileRecord **) DataGetPtr(sFilesTable); for (i = 0; i < nfiles; i++) { if (!strcmp(path, files[i]->fPath)) return files[i]; } } return NULL; } static struct fileRecord * addFile(struct fileRecord *file, const char *path) { struct fileRecord *newFile; if (!sFilesTable) { sFilesTable = DataCreate(0); if (!sFilesTable) return NULL; } newFile = (struct fileRecord *) malloc(sizeof(struct fileRecord) + strlen(path)); if (!newFile) return NULL; if (!DataAppendBytes(sFilesTable, &newFile, sizeof(newFile))) { free(newFile); return NULL; } bcopy(file, newFile, sizeof(struct fileRecord) - 1); strcpy((char *) newFile->fPath, path); return newFile; } // @@@ gvdl: need to clean up the sMergeMetaClasses // @@@ gvdl: I had better fix the object file up again static void unmapFile(struct fileRecord *file) { if (file->fSectData) { struct sectionRecord *section; unsigned int i, nsect; nsect = file->fNSects; section = file->fSections; for (i = 0; i < nsect; i++, section++) { if (section->fRelocCache) { DataRelease(section->fRelocCache); section->fRelocCache = 0; } } DataRelease(file->fSectData); file->fSectData = 0; file->fSections = 0; file->fNSects = 0; } if (file->fSym2Strings) { DataRelease(file->fSym2Strings); file->fSym2Strings = 0; } if (file->fMap) { #if KERNEL if (file->fIsKmem) kmem_free(kernel_map, (vm_address_t) file->fMap, file->fMapSize); #else /* !KERNEL */ if (file->fPadEnd) { vm_address_t padVM; vm_size_t padSize; padVM = round_page((vm_address_t) file->fMap + file->fMapSize); padSize = (vm_size_t) ((vm_address_t) file->fPadEnd - padVM); (void) vm_deallocate(mach_task_self(), padVM, padSize); file->fPadEnd = 0; } (void) munmap((caddr_t) file->fMap, file->fMapSize); #endif /* !KERNEL */ file->fMap = 0; } } static void removeFile(struct fileRecord *file) { if (file->fClassList) { DataRelease(file->fClassList); file->fClassList = 0; } unmapFile(file); free(file); } #if !KERNEL static Boolean mapObjectFile(struct fileRecord *file, const char *pathName) { Boolean result = false; static unsigned char *sFileMapBaseAddr = 0; int fd = 0; if (!sFileMapBaseAddr) { kern_return_t ret; vm_address_t probeAddr; // If we don't already have a base addr find any random chunk // of 32 meg of VM and to use the 16 meg boundrary as a base. ret = vm_allocate(mach_task_self(), &probeAddr, 32 * 1024 * 1024, VM_FLAGS_ANYWHERE); return_if(KERN_SUCCESS != ret, false, ("Unable to allocate base memory %s\n", mach_error_string(ret))); (void) vm_deallocate(mach_task_self(), probeAddr, 32 * 1024 * 1024); // Now round to the next 16 Meg boundrary probeAddr = (probeAddr + (16 * 1024 * 1024 - 1)) & ~(16 * 1024 * 1024 - 1); sFileMapBaseAddr = (unsigned char *) probeAddr; } fd = open(pathName, O_RDONLY, 0); return_if(fd == -1, false, ("Can't open %s for reading - %s\n", pathName, strerror(errno))); do { kern_return_t ret; struct stat sb; int retaddr = -1; break_if(fstat(fd, &sb) == -1, ("Can't stat %s - %s\n", file->fPath, strerror(errno))); file->fMapSize = sb.st_size; file->fMap = sFileMapBaseAddr; ret = KERN_SUCCESS; while (file->fMap < kTopAddr) { vm_address_t padVM; vm_address_t padVMEnd; vm_size_t padSize; padVM = round_page((vm_address_t) file->fMap + file->fMapSize); retaddr = (int) mmap(file->fMap, file->fMapSize, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE, fd, 0); if (-1 == retaddr) { break_if(ENOMEM != errno, ("mmap failed %d - %s\n", errno, strerror(errno))); file->fMap = (unsigned char *) padVM; continue; } // Round up padVM to the next page after the file and assign at // least another fMapSize more room rounded up to the next page // boundary. padVMEnd = round_page(padVM + file->fMapSize); padSize = padVMEnd - padVM; ret = vm_allocate( mach_task_self(), &padVM, padSize, VM_FLAGS_FIXED); if (KERN_SUCCESS == ret) { file->fPadEnd = (unsigned char *) padVMEnd; break; } else { munmap(file->fMap, file->fMapSize); break_if(KERN_INVALID_ADDRESS != ret, ("Unable to allocate pad vm for %s - %s\n", pathName, mach_error_string(ret))); file->fMap = (unsigned char *) padVMEnd; continue; // try again wherever the vm system wants } } if (-1 == retaddr || KERN_SUCCESS != ret) break; break_if(file->fMap >= kTopAddr, ("Unable to map memory %s\n", file->fPath)); sFileMapBaseAddr = file->fPadEnd; result = true; } while(0); close(fd); return result; } #endif /* !KERNEL */ static Boolean findBestArch(struct fileRecord *file, const char *pathName) { unsigned long magic; struct fat_header *fat; file->fMachOSize = file->fMapSize; file->fMachO = file->fMap; magic = ((const struct mach_header *) file->fMachO)->magic; fat = (struct fat_header *) file->fMachO; // Try to figure out what type of file this is return_if(file->fMapSize < sizeof(unsigned long), false, ("%s isn't a valid object file - no magic\n", pathName)); #if KERNEL // CIGAM is byte-swapped MAGIC if (magic == FAT_MAGIC || magic == FAT_CIGAM) { load_return_t load_return; struct fat_arch fatinfo; load_return = fatfile_getarch(NULL, (vm_address_t) fat, &fatinfo); return_if(load_return != LOAD_SUCCESS, false, ("Extension \"%s\": has no code for this computer\n", pathName)); file->fMachO = file->fMap + fatinfo.offset; file->fMachOSize = fatinfo.size; magic = ((const struct mach_header *) file->fMachO)->magic; } #else /* !KERNEL */ // Do we need to in-place swap the endianness of the fat header? if (magic == FAT_CIGAM) { unsigned long i; struct fat_arch *arch; fat->nfat_arch = NXSwapBigLongToHost(fat->nfat_arch); return_if(file->fMapSize < sizeof(struct fat_header) + fat->nfat_arch * sizeof(struct fat_arch), false, ("%s is too fat\n", file->fPath)); arch = (struct fat_arch *) &fat[1]; for (i = 0; i < fat->nfat_arch; i++) { arch[i].cputype = NXSwapBigLongToHost(arch[i].cputype); arch[i].cpusubtype = NXSwapBigLongToHost(arch[i].cpusubtype); arch[i].offset = NXSwapBigLongToHost(arch[i].offset); arch[i].size = NXSwapBigLongToHost(arch[i].size); arch[i].align = NXSwapBigLongToHost(arch[i].align); } magic = NXSwapBigLongToHost(fat->magic); } // Now see if we can find any valid architectures if (magic == FAT_MAGIC) { const NXArchInfo *myArch; unsigned long fatsize; struct fat_arch *arch; fatsize = sizeof(struct fat_header) + fat->nfat_arch * sizeof(struct fat_arch); return_if(file->fMapSize < fatsize, false, ("%s isn't a valid fat file\n", pathName)); myArch = NXGetLocalArchInfo(); arch = NXFindBestFatArch(myArch->cputype, myArch->cpusubtype, (struct fat_arch *) &fat[1], fat->nfat_arch); return_if(!arch, false, ("%s hasn't got arch for %s\n", pathName, myArch->name)); return_if(arch->offset + arch->size > file->fMapSize, false, ("%s's %s arch is incomplete\n", pathName, myArch->name)); file->fMachO = file->fMap + arch->offset; file->fMachOSize = arch->size; magic = ((const struct mach_header *) file->fMachO)->magic; } #endif /* KERNEL */ return_if(magic != MH_MAGIC, false, ("%s isn't a valid mach-o\n", pathName)); return true; } static Boolean parseSegments(struct fileRecord *file, struct segment_command *seg) { struct sectionRecord *sections; int i, nsects = seg->nsects; const struct segmentMap { struct segment_command seg; const struct section sect[1]; } *segMap; if (!file->fSectData) { file->fSectData = DataCreate(0); if (!file->fSectData) return false; } // Increase length of section DataRef and cache data pointer if (!DataAddLength(file->fSectData, nsects * sizeof(struct sectionRecord))) return false; file->fSections = (struct sectionRecord *) DataGetPtr(file->fSectData); // Initialise the new sections sections = &file->fSections[file->fNSects]; file->fNSects += nsects; for (i = 0, segMap = (struct segmentMap *) seg; i < nsects; i++) sections[i].fSection = &segMap->sect[i]; return true; } static Boolean remangleExternSymbols(struct fileRecord *file, const char *pathName) { const struct nlist *sym; int i, nsyms, len; DataRef strings = NULL; DEBUG_LOG(("Remangling %s\n", pathName)); file->fNewStringBlocks = DataCreate(0); return_if(!file->fNewStringBlocks, false, ("Unable to allocate new string table for %s\n", pathName)); nsyms = file->fSymtab->nsyms; for (i = 0, sym = file->fSymbolBase; i < nsyms; i++, sym++) { Rem3Return ret; const char *symname; char *newname; unsigned char n_type = sym->n_type; // Not an external symbol or it is a stab in any case don't bother if ((n_type ^ N_EXT) & (N_STAB | N_EXT)) continue; symname = symNameByIndex(file, i); tryRemangleAgain: if (!strings) { strings = DataCreate(16 * 1024); // Arbitrary block size return_if(!strings, false, ("Unable to allocate new string block for %s\n", pathName)); } len = DataRemaining(strings); newname = DataGetEndPtr(strings); ret = rem3_remangle_name(newname, &len, symname); switch (ret) { case kR3InternalNotRemangled: errprintf("Remangler fails on %s in %s\n", symname, pathName); /* No break */ case kR3NotRemangled: break; case kR3Remangled: file->fSymbToStringTable[i] = newname; file->fRemangled = file->fSymbolsDirty = true; DataAddLength(strings, len + 1); // returns strlen break; case kR3BufferTooSmallRemangled: return_if(!DataAppendBytes (file->fNewStringBlocks, &strings, sizeof(strings)), false, ("Unable to allocate string table for %s\n", pathName)); strings = NULL; goto tryRemangleAgain; case kR3BadArgument: default: return_if(true, false, ("Internal error - remangle of %s\n", pathName)); } } if (strings) { return_if(!DataAppendBytes (file->fNewStringBlocks, &strings, sizeof(strings)), false, ("Unable to allocate string table for %s\n", pathName)); } return true; } static Boolean parseSymtab(struct fileRecord *file, const char *pathName) { const struct nlist *sym; unsigned int i, firstlocal, nsyms; unsigned long strsize; const char *strbase; Boolean foundOSObject, found295CPP; // we found a link edit segment so recompute the bases if (file->fLinkEditSeg) { struct segment_command *link = file->fLinkEditSeg; file->fSymbolBase = (struct nlist *) (link->vmaddr + (file->fSymtab->symoff - link->fileoff)); file->fStringBase = (char *) (link->vmaddr + (file->fSymtab->stroff - link->fileoff)); return_if( ( (caddr_t) file->fStringBase + file->fSymtab->strsize > (caddr_t) link->vmaddr + link->vmsize ), false, ("%s isn't a valid mach-o le, bad symbols\n", pathName)); } else { file->fSymbolBase = (struct nlist *) (file->fMachO + file->fSymtab->symoff); file->fStringBase = (char *) (file->fMachO + file->fSymtab->stroff); return_if( ( file->fSymtab->stroff + file->fSymtab->strsize > file->fMachOSize ), false, ("%s isn't a valid mach-o, bad symbols\n", pathName)); } nsyms = file->fSymtab->nsyms; // If this file the kernel and do we have an executable image file->fNoKernelExecutable = (vm_page_size == file->fSymtab->symoff) && (file->fSections[0].fSection->size == 0); // Generate a table of pointers to strings indexed by the symbol number file->fSym2Strings = DataCreate(nsyms * sizeof(const char *)); DataSetLength(file->fSym2Strings, nsyms * sizeof(const char *)); return_if(!file->fSym2Strings, false, ("Unable to allocate memory - symbol string trans\n", pathName)); file->fSymbToStringTable = (const char **) DataGetPtr(file->fSym2Strings); // Search for the first non-stab symbol in table strsize = file->fSymtab->strsize; strbase = file->fStringBase; firstlocal = 0; found295CPP = foundOSObject = false; for (i = 0, sym = file->fSymbolBase; i < nsyms; i++, sym++) { long strx = sym->n_un.n_strx; const char *symname = strbase + strx; unsigned char n_type; return_if(((unsigned long) strx > strsize), false, ("%s has an illegal string offset in symbol %d\n", pathName, i)); // Load up lookup symbol look table with sym names file->fSymbToStringTable[i] = symname; n_type = sym->n_type & (N_TYPE | N_EXT); // Find the first exported symbol if ( !firstlocal && (n_type & N_EXT) ) { firstlocal = i; file->fLocalSyms = sym; } // Find the a OSObject based subclass by searching for symbols // that have a suffix of '10superClassE' symname++; // Skip leading '_' if (!foundOSObject && (n_type == (N_SECT | N_EXT) || n_type == (N_ABS | N_EXT)) && strx) { const char *suffix, *endSym; endSym = symname + strlen(symname); // Find out if this symbol has the superclass suffix. if (symname[0] == kCPPSymbolPrefix[0] && symname[1] == kCPPSymbolPrefix[1]) { suffix = endSym - sizeof(k31SuperClassSuffix) + 1; // Check for a gcc3 OSObject subclass if (suffix > symname && !strcmp(suffix, k31SuperClassSuffix)) foundOSObject = true; } else { suffix = endSym - sizeof(k29SuperClassSuffix); // Check for a gcc295 OSObject subclass if (suffix > symname && ('.' == *suffix || '$' == *suffix) && !strcmp(suffix+1, k29SuperClassSuffix)) { found295CPP = foundOSObject = true; } else if (!found295CPP) { // Finally just check if we need to remangle symname++; // skip leading '__' while (*symname) { if ('_' == *symname++ && '_' == *symname++) { found295CPP = true; break; } } } } } else if (sym->n_type == (N_EXT | N_UNDF)) { if ( !file->fNLocal) // Find the last local symbol file->fNLocal = i - firstlocal; if (!found295CPP) { symname++; // Skip possible second '_' at start. while (*symname) { if ('_' == *symname++ && '_' == *symname++) { found295CPP = true; break; } } } } // Note symname is trashed at this point } return_if(i < nsyms, false, ("%s isn't a valid mach-o, bad symbol strings\n", pathName)); return_if(!file->fLocalSyms, false, ("%s has no symbols?\n", pathName)); // If we don't have any undefined symbols then all symbols // must be local so just compute it now if necessary. if ( !file->fNLocal ) file->fNLocal = i - firstlocal; file->fFoundOSObject = foundOSObject; if (found295CPP && !remangleExternSymbols(file, pathName)) return false; return true; } // @@@ gvdl: These functions need to be hashed they are // going to be way too slow for production code. static const struct nlist * findSymbolByAddress(const struct fileRecord *file, void *entry) { // not quite so dumb linear search of all symbols const struct nlist *sym; int i, nsyms; // First try to find the symbol in the most likely place which is the // extern symbols sym = file->fLocalSyms; for (i = 0, nsyms = file->fNLocal; i < nsyms; i++, sym++) { if (sym->n_value == (unsigned long) entry && !(sym->n_type & N_STAB) ) return sym; } // Didn't find it in the external symbols so try to local symbols before // giving up. sym = file->fSymbolBase; for (i = 0, nsyms = file->fSymtab->nsyms; i < nsyms; i++, sym++) { if ( (sym->n_type & N_EXT) ) return NULL; if ( sym->n_value == (unsigned long) entry && !(sym->n_type & N_STAB) ) return sym; } return NULL; } struct searchContext { const char *fSymname; const struct fileRecord *fFile; }; static int symbolSearch(const void *vKey, const void *vSym) { const struct searchContext *key = (const struct searchContext *) vKey; const struct nlist *sym = (const struct nlist *) vSym; return strcmp(key->fSymname + 1, symbolname(key->fFile, sym) + 1); } static const struct nlist * findSymbolByName(struct fileRecord *file, const char *symname) { if (file->fRemangled) { // @@@ gvdl: Performance problem // Linear search as we don't sort after remangling const struct nlist *sym; int i = file->fLocalSyms - file->fSymbolBase; int nLocal = file->fNLocal + i; for (sym = file->fLocalSyms; i < nLocal; i++, sym++) if (!strcmp(symNameByIndex(file, i) + 1, symname + 1)) return sym; return NULL; } else { struct searchContext context; context.fSymname = symname; context.fFile = file; return (struct nlist *) bsearch(&context, file->fLocalSyms, file->fNLocal, sizeof(struct nlist), symbolSearch); } } static Boolean relocateSection(const struct fileRecord *file, struct sectionRecord *sectionRec) { const struct nlist *symbol; const struct section *section; struct relocRecord *rec; struct relocation_info *rinfo; unsigned long i; unsigned long r_address, r_symbolnum, r_length; enum reloc_type_generic r_type; UInt8 *sectionBase; void **entry; sectionRec->fRelocCache = DataCreate( sectionRec->fSection->nreloc * sizeof(struct relocRecord)); if (!sectionRec->fRelocCache) return false; section = sectionRec->fSection; sectionBase = file->fMachO + section->offset; rec = (struct relocRecord *) DataGetPtr(sectionRec->fRelocCache); rinfo = (struct relocation_info *) (file->fMachO + section->reloff); for (i = 0; i < section->nreloc; i++, rec++, rinfo++) { // Totally uninterested in scattered relocation entries if ( (rinfo->r_address & R_SCATTERED) ) continue; r_address = rinfo->r_address; entry = (void **) (sectionBase + r_address); /* * The r_address field is really an offset into the contents of the * section and must reference something inside the section (Note * that this is not the case for PPC_RELOC_PAIR entries but this * can't be one with the above checks). */ return_if(r_address >= section->size, false, ("Invalid relocation entry in %s - not in section\n", file->fPath)); // If we don't have a VANILLA entry or the Vanilla entry isn't // a 'long' then ignore the entry and try the next. r_type = (enum reloc_type_generic) rinfo->r_type; r_length = rinfo->r_length; if (r_type != GENERIC_RELOC_VANILLA || r_length != 2) continue; r_symbolnum = rinfo->r_symbolnum; /* * If rinfo->r_extern is set this relocation entry is an external entry * else it is a local entry. */ if (rinfo->r_extern) { /* * This is an external relocation entry. * r_symbolnum is an index into the input file's symbol table * of the symbol being refered to. The symbol must be * undefined to be used in an external relocation entry. */ return_if(r_symbolnum >= file->fSymtab->nsyms, false, ("Invalid relocation entry in %s - no symbol\n", file->fPath)); /* * If this is an indirect symbol resolve indirection (all chains * of indirect symbols have been resolved so that they point at * a symbol that is not an indirect symbol). */ symbol = file->fSymbolBase; if ((symbol[r_symbolnum].n_type & N_TYPE) == N_INDR) r_symbolnum = symbol[r_symbolnum].n_value; symbol = &symbol[r_symbolnum]; return_if(symbol->n_type != (N_EXT | N_UNDF), false, ("Invalid relocation entry in %s - extern\n", file->fPath)); } else { /* * If the symbol is not in any section then it can't be a * pointer to a local segment and I don't care about it. */ if (r_symbolnum == R_ABS) continue; // Note segment references are offset by 1 from 0. return_if(r_symbolnum > file->fNSects, false, ("Invalid relocation entry in %s - local\n", file->fPath)); // Find the symbol, if any, that backs this entry symbol = findSymbolByAddress(file, *entry); } rec->fValue = *entry; // Save the previous value rec->fRInfo = rinfo; // Save a pointer to the reloc rec->fSymbol = symbol; // Record the current symbol *entry = (void *) rec; // Save pointer to record in object image } DataSetLength(sectionRec->fRelocCache, i * sizeof(struct relocRecord)); ((struct fileRecord *) file)->fImageDirty = true; return true; } static const struct nlist * findSymbolRefAtLocation(const struct fileRecord *file, struct sectionRecord *sctn, void **loc) { if (file->fIsKernel) { if (*loc) return findSymbolByAddress(file, *loc); } else if (sctn->fRelocCache || relocateSection(file, sctn)) { struct relocRecord *reloc = (struct relocRecord *) *loc; if (DataContainsAddr(sctn->fRelocCache, reloc)) return reloc->fSymbol; } return NULL; } static Boolean addClass(struct fileRecord *file, struct metaClassRecord *inClass, const char *cname) { Boolean result = false; struct metaClassRecord *newClass = NULL; struct metaClassRecord **fileClasses = NULL; int len; if (!file->fClassList) { file->fClassList = DataCreate(0); if (!file->fClassList) return false; } do { // Attempt to allocate all necessary resource first len = strlen(cname) + 1 + (int) (&((struct metaClassRecord *) 0)->fClassName); newClass = (struct metaClassRecord *) malloc(len); if (!newClass) break; if (!DataAddLength(file->fClassList, sizeof(struct metaClassRecord *))) break; fileClasses = (struct metaClassRecord **) (DataGetPtr(file->fClassList) + DataGetLength(file->fClassList)); // Copy the meta Class structure and string name into newClass and // insert object at end of the file->fClassList and sMergeMetaClasses *newClass = *inClass; strcpy(newClass->fClassName, cname); fileClasses[-1] = newClass; return true; } while (0); if (fileClasses) DataAddLength(file->fClassList, -sizeof(struct metaClassRecord *)); if (newClass) free(newClass); return result; } static struct metaClassRecord *getClass(DataRef classList, const char *cname) { if (classList) { int i, nclass; struct metaClassRecord **classes, *thisClass; nclass = DataGetLength(classList) / sizeof(struct metaClassRecord *); classes = (struct metaClassRecord **) DataGetPtr(classList); for (i = 0; i < nclass; i++) { thisClass = classes[i]; if (!strcmp(thisClass->fClassName, cname)) return thisClass; } } return NULL; } // Add the class 'cname' to the list of known OSObject based classes // Note 'sym' is the <cname>10superClassE symbol. static Boolean recordClass(struct fileRecord *file, const char *cname, const struct nlist *sym) { Boolean result = false; char *supername = NULL; const char *classname = NULL; struct metaClassRecord newClass; char strbuffer[1024]; // Only do the work to find the super class if we are // not currently working on the kernel. The kernel is the end // of all superclass chains by definition as the kernel must be binary // compatible with itself. if (!file->fIsKernel) { const char *suffix; const struct nlist *supersym; const struct section *section; struct sectionRecord *sectionRec; unsigned char sectind = sym->n_sect; const char *superstr; void **location; int snamelen; // We can't resolve anything that isn't in a real section // Note that the sectind is starts at one to make room for the // NO_SECT flag but the fNSects field isn't offset so we have a // '>' test. Which means this isn't an OSObject based class if (sectind == NO_SECT || sectind > file->fNSects) { result = true; goto finish; } sectionRec = file->fSections + sectind - 1; section = sectionRec->fSection; location = (void **) ( file->fMachO + section->offset + sym->n_value - section->addr ); supersym = findSymbolRefAtLocation(file, sectionRec, location); if (!supersym) { result = true; // No superclass symbol then it isn't an OSObject. goto finish; } // Find string in file and skip leading '_' and then find the suffix superstr = symbolname(file, supersym) + 1; suffix = superstr + strlen(superstr) - sizeof(kGMetaSuffix) + 1; if (suffix <= superstr || strcmp(suffix, kGMetaSuffix)) { result = true; // Not an OSObject superclass so ignore it.. goto finish; } // Got a candidate so hand it over for class processing. snamelen = suffix - superstr - sizeof(kOSObjPrefix) + 2; supername = (char *) malloc(snamelen + 1); bcopy(superstr + sizeof(kOSObjPrefix) - 2, supername, snamelen); supername[snamelen] = '\0'; } do { break_if(getClass(file->fClassList, cname), ("Duplicate class %s in %s\n", cname, file->fPath)); snprintf(strbuffer, sizeof(strbuffer), "%s%s", kVTablePrefix, cname); newClass.fVTableSym = findSymbolByName(file, strbuffer); break_if(!newClass.fVTableSym, ("Can't find vtable %s in %s\n", cname, file->fPath)); newClass.fFile = file; newClass.fSuperName = supername; newClass.fPatchedVTable = NULL; // Can't use cname as it may be a stack variable // However the vtable's string has the class name as a suffix // so why don't we use that rather than mallocing a string. classname = symbolname(file, newClass.fVTableSym) + sizeof(kVTablePrefix) - 1; break_if(!addClass(file, &newClass, classname), ("recordClass - no memory?\n")); supername = NULL; result = true; } while (0); finish: if (supername) free(supername); return result; } static Boolean getMetaClassGraph(struct fileRecord *file) { const struct nlist *sym; int i, nsyms; // Search the symbol table for the local symbols that are generated // by the metaclass system. There are three metaclass variables // that are relevant. // // <ClassName>.metaClass A pointer to the meta class structure. // <ClassName>.superClass A pointer to the super class's meta class. // <ClassName>.gMetaClass The meta class structure itself. // ___vt<ClassName> The VTable for the class <ClassName>. // // In this code I'm going to search for any symbols that // ends in k31SuperClassSuffix as this indicates this class is a conforming // OSObject subclass and will need to be patched, and it also // contains a pointer to the super class's meta class structure. sym = file->fLocalSyms; for (i = 0, nsyms = file->fNLocal; i < nsyms; i++, sym++) { const char *symname; const char *suffix; char classname[1024]; unsigned char n_type = sym->n_type & (N_TYPE | N_EXT); int cnamelen; // Check that the symbols is a global and that it has a name. if (((N_SECT | N_EXT) != n_type && (N_ABS | N_EXT) != n_type) || !sym->n_un.n_strx) continue; // Only search from the last *sep* in the symbol. // but skip the leading '_' in all symbols first. symname = symbolname(file, sym) + 1; if (symname[0] != kCPPSymbolPrefix[0] || symname[1] != kCPPSymbolPrefix[1]) continue; suffix = symname + strlen(symname) - sizeof(k31SuperClassSuffix) + 1; if (suffix <= symname || strcmp(suffix, k31SuperClassSuffix)) continue; // Got a candidate so hand it over for class processing. cnamelen = suffix - symname - sizeof(kOSObjPrefix) + 2; return_if(cnamelen + 1 >= (int) sizeof(classname), false, ("Symbol %s is too long", symname)); bcopy(symname + sizeof(kOSObjPrefix) - 2, classname, cnamelen); classname[cnamelen] = '\0'; if (!recordClass(file, classname, sym)) return false; } return_if(!file->fClassList, false, ("Internal error, " "getMetaClassGraph(%s) found no classes", file->fPath)); DEBUG_LOG(("Found %ld classes in %p for %s\n", DataGetLength(file->fClassList)/sizeof(void*), file->fClassList, file->fPath)); return true; } static Boolean mergeOSObjectsForFile(const struct fileRecord *file) { int i, nmerged; Boolean foundDuplicates = false; DEBUG_LOG(("Merging file %s\n", file->fPath)); // @@@ gvdl: if (!file->fClassList) return true; if (!sMergedFiles) { sMergedFiles = DataCreate(0); return_if(!sMergedFiles, false, ("Unable to allocate memory metaclass list\n", file->fPath)); } // Check to see if we have already merged this file nmerged = DataGetLength(sMergedFiles) / sizeof(struct fileRecord *); for (i = 0; i < nmerged; i++) { if (file == ((void **) DataGetPtr(sMergedFiles))[i]) return true; } if (!sMergeMetaClasses) { sMergeMetaClasses = DataCreate(0); return_if(!sMergeMetaClasses, false, ("Unable to allocate memory metaclass list\n", file->fPath)); } else { /* perform a duplicate check */ int i, j, cnt1, cnt2; struct metaClassRecord **list1, **list2; list1 = (struct metaClassRecord **) DataGetPtr(file->fClassList); cnt1 = DataGetLength(file->fClassList) / sizeof(*list1); list2 = (struct metaClassRecord **) DataGetPtr(sMergeMetaClasses); cnt2 = DataGetLength(sMergeMetaClasses) / sizeof(*list2); for (i = 0; i < cnt1; i++) { for (j = 0; j < cnt2; j++) { if (!strcmp(list1[i]->fClassName, list2[j]->fClassName)) { errprintf("duplicate class %s in %s & %s\n", list1[i]->fClassName, file->fPath, list2[j]->fFile->fPath); } } } } if (foundDuplicates) return false; return_if(!DataAppendBytes(sMergedFiles, &file, sizeof(file)), false, ("Unable to allocate memory to merge %s\n", file->fPath)); return_if(!DataAppendData(sMergeMetaClasses, file->fClassList), false, ("Unable to allocate memory to merge %s\n", file->fPath)); if (file == sKernelFile) sMergedKernel = true; return true; } // Returns a pointer to the base of the section offset by the sections // base address. The offset is so that we can add nlist::n_values directly // to this address and get a valid pointer in our memory. static unsigned char * getSectionForSymbol(const struct fileRecord *file, const struct nlist *symb, void ***endP) { const struct section *section; unsigned char sectind; unsigned char *base; sectind = symb->n_sect; // Default to symbols section if ((symb->n_type & N_TYPE) == N_ABS && file->fIsKernel) { // Absolute symbol so we have to iterate over our sections for (sectind = 1; sectind <= file->fNSects; sectind++) { unsigned long start, end; section = file->fSections[sectind - 1].fSection; start = section->addr; end = start + section->size; if (start <= symb->n_value && symb->n_value < end) { // Found the relevant section break; } } } // Is the vtable in a valid section? return_if(sectind == NO_SECT || sectind > file->fNSects, (unsigned char *) -1, ("%s isn't a valid kext, bad section reference\n", file->fPath)); section = file->fSections[sectind - 1].fSection; // for when we start walking the vtable so compute offset's now. base = file->fMachO + section->offset; *endP = (void **) (base + section->size); return base - section->addr; // return with addr offset } static Boolean resolveKernelVTable(struct metaClassRecord *metaClass) { const struct fileRecord *file; struct patchRecord *patchedVTable; void **curEntry, **vtableEntries, **endSection; unsigned char *sectionBase; struct patchRecord *curPatch; int classSize; // Should never occur but it doesn't cost us anything to check. if (metaClass->fPatchedVTable) return true; DEBUG_LOG(("Kernel vtable %s\n", metaClass->fClassName)); // @@@ gvdl: // Do we have a valid vtable to patch? return_if(!metaClass->fVTableSym, false, ("Internal error - no class vtable symbol?\n")); file = metaClass->fFile; // If the metaClass we are being to ask is in the kernel then we // need to do a quick scan to grab the fPatchList in a reliable format // however we don't need to check the superclass in the kernel // as the kernel vtables are always correct wrt themselves. // Note this ends the superclass chain recursion. return_if(!file->fIsKernel, false, ("Internal error - resolveKernelVTable not kernel\n")); if (file->fNoKernelExecutable) { // Oh dear attempt to map the kernel's VM into my memory space return_if(file->fNoKernelExecutable, false, ("Internal error - fNoKernelExecutable not implemented yet\n")); } // We are going to need the base and the end sectionBase = getSectionForSymbol(file, metaClass->fVTableSym, &endSection); if (-1 == (long) sectionBase) return false; vtableEntries = (void **) (sectionBase + metaClass->fVTableSym->n_value); curEntry = vtableEntries + kVTablePreambleLen; for (classSize = 0; curEntry < endSection && *curEntry; classSize++) curEntry++; return_if(*curEntry, false, ("Bad kernel image, short section\n")); patchedVTable = (struct patchRecord *) malloc((classSize + 1) * sizeof(struct patchRecord)); return_if(!patchedVTable, false, ("resolveKernelVTable - no memory\n")); // Copy the vtable of this class into the patch table curPatch = patchedVTable; curEntry = vtableEntries + kVTablePreambleLen; for (; *curEntry; curEntry++, curPatch++) { curPatch->fSymbol = (struct nlist *) findSymbolByAddress(file, *curEntry); curPatch->fType = kSymbolLocal; } // Tag the end of the patch vtable curPatch->fSymbol = NULL; metaClass->fPatchedVTable = patchedVTable; return true; } static const char *addNewString(struct fileRecord *file, const char *strname, int namelen) { DataRef strings = 0; const char *newStr; namelen++; // Include terminating '\0'; // Make sure we have a string table as well for this symbol if (file->fNewStringBlocks) { DataRef *blockTable = (DataRef *) DataGetPtr(file->fNewStringBlocks); int index = DataGetLength(file->fNewStringBlocks) / sizeof(DataRef*); strings = blockTable[index - 1]; if (DataRemaining(strings) < namelen) strings = 0; } else { file->fNewStringBlocks = DataCreate(0); return_if(!file->fNewStringBlocks, NULL, ("Unable to allocate new string table %s\n", file->fPath)); } if (!strings) { int size = (namelen + 1023) & ~1023; if (size < 16 * 1024) size = 16 * 1024; strings = DataCreate(size); return_if(!strings, NULL, ("Unable to allocate new string block %s\n", file->fPath)); return_if( !DataAppendBytes(file->fNewStringBlocks, &strings, sizeof(strings)), false, ("Unable to allocate string table for %s\n", file->fPath)); } newStr = DataGetEndPtr(strings); DataAppendBytes(strings, strname, namelen); return newStr; } // reloc->fPatch must contain a valid pointer static struct nlist * getNewSymbol(struct fileRecord *file, const struct relocRecord *reloc, const char *supername) { unsigned int size, i; struct nlist **sym; struct nlist *msym; struct relocation_info *rinfo; const char *newStr; if (!file->fNewSymbols) { file->fNewSymbols = DataCreate(0); return_if(!file->fNewSymbols, NULL, ("Unable to allocate new symbol table for %s\n", file->fPath)); } rinfo = (struct relocation_info *) reloc->fRInfo; size = DataGetLength(file->fNewSymbols) / sizeof(struct nlist *); sym = (struct nlist **) DataGetPtr(file->fNewSymbols); for (i = 0; i < size; i++, sym++) { int symnum = i + file->fSymtab->nsyms; newStr = symNameByIndex(file, symnum); if (!strcmp(newStr, supername)) { rinfo->r_symbolnum = symnum; file->fSymbolsDirty = true; return *sym; } } // Assert that this is a vaild symbol. I need this condition to be true // for the later code to make non-zero. So the first time through I'd // better make sure that it is 0. return_if(reloc->fSymbol->n_sect, NULL, ("Undefined symbol entry with non-zero section %s:%s\n", file->fPath, symbolname(file, reloc->fSymbol))); // If we are here we didn't find the symbol so create a new one now msym = (struct nlist *) malloc(sizeof(struct nlist)); return_if(!msym, NULL, ("Unable to create symbol table entry for %s", file->fPath)); return_if(!DataAppendBytes(file->fNewSymbols, &msym, sizeof(msym)), NULL, ("Unable to grow symbol table for %s\n", file->fPath)); newStr = addNewString(file, supername, strlen(supername)); if (!newStr) return NULL; // If we are here we didn't find the symbol so create a new one now return_if(!DataAppendBytes(file->fSym2Strings, &newStr, sizeof(newStr)), NULL, ("Unable to grow symbol table for %s\n", file->fPath)); file->fSymbToStringTable = (const char **) DataGetPtr(file->fSym2Strings); // Offset the string index by the original string table size // and negate the address to indicate that this is a 'new' symbol msym->n_un.n_strx = -1; msym->n_type = (N_EXT | N_UNDF); msym->n_sect = NO_SECT; msym->n_desc = 0; msym->n_value = (unsigned long) newStr; // Mark the old symbol as being potentially deletable I can use the // n_sect field as the input symbol must be of type N_UNDF which means // that the n_sect field must be set to NO_SECT otherwise it is an // invalid input file. // // However the symbol may have been just inserted by the fixOldSymbol path. // If this is the case then we know it is in use and we don't have to // mark it as a deletable symbol. if (reloc->fSymbol->n_un.n_strx >= 0) { ((struct nlist *) reloc->fSymbol)->n_un.n_strx = -reloc->fSymbol->n_un.n_strx; ((struct nlist *) reloc->fSymbol)->n_sect = (unsigned char) -1; } rinfo->r_symbolnum = i + file->fSymtab->nsyms; file->fSymbolsDirty = true; return msym; } static struct nlist * fixOldSymbol(struct fileRecord *file, const struct relocRecord *reloc, const char *supername) { unsigned int namelen; struct nlist *sym = (struct nlist *) reloc->fSymbol; const char *oldname = symbolname(file, sym); // assert(sym->n_un.n_strx >= 0); namelen = strlen(supername); sym->n_un.n_strx = -sym->n_un.n_strx; if (oldname && namelen < strlen(oldname)) { // Overwrite old string in string table strcpy((char *) oldname, supername); file->fSymbolsDirty = true; return sym; } oldname = addNewString(file, supername, namelen); if (!oldname) return NULL; file->fSymbToStringTable[sym - file->fSymbolBase] = oldname; file->fSymbolsDirty = true; return sym; } static enum patchState symbolCompare(const struct fileRecord *file, const struct nlist *classsym, const char *supername) { const char *classname; // Check to see if the target function is locally defined // if it is then we can assume this is a local vtable override if ((classsym->n_type & N_TYPE) != N_UNDF) return kSymbolLocal; // Check to see if both symbols point to the same symbol name // if so then we are still identical. classname = symbolname(file, classsym); if (!strcmp(classname, supername)) return kSymbolIdentical; // We know that the target's vtable entry is different from the // superclass' vtable entry. This means that we will have to apply a // patch to the current entry, however before returning lets check to // see if we have a _RESERVEDnnn field 'cause we can use this as a // registration point that must align between vtables. if (strstr(supername, kReservedNamePrefix)) return kSymbolMismatch; // OK, we have a superclass difference where the superclass doesn't // reference a pad function so assume that the superclass is correct. if (strstr(classname, kReservedNamePrefix)) return kSymbolPadUpdate; else return kSymbolSuperUpdate; } static Boolean patchVTable(struct metaClassRecord *metaClass) { struct metaClassRecord *super = NULL; struct fileRecord *file; struct patchRecord *patchedVTable; struct relocRecord **curReloc, **vtableRelocs, **endSection; unsigned char *sectionBase; int classSize; // Should never occur but it doesn't cost us anything to check. if (metaClass->fPatchedVTable) return true; // Do we have a valid vtable to patch? return_if(!metaClass->fVTableSym, false, ("Internal error - no class vtable symbol?\n")); file = metaClass->fFile; // If the metaClass we are being to ask is in the kernel then we // need to do a quick scan to grab the fPatchList in a reliable format // however we don't need to check the superclass in the kernel // as the kernel vtables are always correct wrt themselves. // Note this ends the superclass chain recursion. return_if(file->fIsKernel, false, ("Internal error - patchVTable shouldn't used for kernel\n")); if (!metaClass->fSuperName) return false; // The class isn't in the kernel so make sure that the super class // is patched before patching ouselves. super = getClass(sMergeMetaClasses, metaClass->fSuperName); return_if(!super, false, ("Can't find superclass for %s : %s\n", metaClass->fClassName, metaClass->fSuperName)); // Superclass recursion if necessary if (!super->fPatchedVTable) { Boolean res; if (super->fFile->fIsKernel) res = resolveKernelVTable(super); else res = patchVTable(super); if (!res) return false; } DEBUG_LOG(("Patching %s\n", metaClass->fClassName)); // @@@ gvdl: // We are going to need the base and the end sectionBase = getSectionForSymbol(file, metaClass->fVTableSym, (void ***) &endSection); if (-1 == (long) sectionBase) return false; vtableRelocs = (struct relocRecord **) (sectionBase + metaClass->fVTableSym->n_value); curReloc = vtableRelocs + kVTablePreambleLen; for (classSize = 0; curReloc < endSection && *curReloc; classSize++) curReloc++; return_if(*curReloc, false, ("%s isn't a valid kext, short section\n", file->fPath)); patchedVTable = (struct patchRecord *) malloc((classSize + 1) * sizeof(struct patchRecord)); return_if(!patchedVTable, false, ("patchedVTable - no memory\n")); do { struct patchRecord *curPatch; struct nlist *symbol; curPatch = patchedVTable; curReloc = vtableRelocs + kVTablePreambleLen; // Grab the super table patches if necessary // Can't be patching a kernel table as we don't walk super // class chains in the kernel symbol space. if (super && super->fPatchedVTable) { const struct patchRecord *spp; spp = super->fPatchedVTable; for ( ; spp->fSymbol; curReloc++, spp++, curPatch++) { const char *supername = symbolname(super->fFile, spp->fSymbol); symbol = (struct nlist *) (*curReloc)->fSymbol; curPatch->fType = symbolCompare(file, symbol, supername); switch (curPatch->fType) { case kSymbolIdentical: case kSymbolLocal: break; case kSymbolSuperUpdate: symbol = getNewSymbol(file, (*curReloc), supername); break; case kSymbolPadUpdate: symbol = fixOldSymbol(file, (*curReloc), supername); break; case kSymbolMismatch: errprintf("%s is not compatible with its superclass, " "%s superclass changed?\n", metaClass->fClassName, super->fClassName); goto abortPatch; default: errprintf("Internal error - unknown patch type\n"); goto abortPatch; } if (symbol) { curPatch->fSymbol = symbol; (*curReloc)->fSymbol = symbol; } else goto abortPatch; } } // Copy the remainder of this class' vtable into the patch table for (; *curReloc; curReloc++, curPatch++) { // Local reloc symbols curPatch->fType = kSymbolLocal; curPatch->fSymbol = (struct nlist *) (*curReloc)->fSymbol; } // Tag the end of the patch vtable curPatch->fSymbol = NULL; metaClass->fPatchedVTable = patchedVTable; return true; } while(0); abortPatch: if (patchedVTable) free(patchedVTable); return false; } static Boolean growImage(struct fileRecord *file, vm_size_t delta) { #if !KERNEL file->fMachOSize += delta; return (file->fMachO + file->fMachOSize <= file->fPadEnd); #else /* KERNEL */ vm_address_t startMachO, endMachO, endMap; vm_offset_t newMachO; vm_size_t newsize; unsigned long i, last = 0; struct metaClassRecord **classes = NULL; struct sectionRecord *section; kern_return_t ret; startMachO = (vm_address_t) file->fMachO; endMachO = startMachO + file->fMachOSize + delta; endMap = (vm_address_t) file->fMap + file->fMapSize; // Do we have room in the current mapped image if (endMachO < round_page(endMap)) { file->fMachOSize += delta; return true; } newsize = endMachO - startMachO; if (newsize < round_page(file->fMapSize)) { DEBUG_LOG(("Growing image %s by moving\n", file->fPath)); // We have room in the map if we shift the macho image within the // current map. We will have to patch up pointers into the object. newMachO = (vm_offset_t) file->fMap; bcopy((char *) startMachO, (char *) newMachO, file->fMachOSize); } else if (file->fIsKmem) { // kmem_alloced mapping so we can try a kmem_realloc ret = kmem_realloc(kernel_map, (vm_address_t) file->fMap, (vm_size_t) file->fMapSize, &newMachO, newsize); if (KERN_SUCCESS != ret) return false; // If the mapping didn't move then just return if ((vm_address_t) file->fMap == newMachO) { file->fMachOSize = file->fMapSize = newsize; return true; } DEBUG_LOG(("Growing image %s by reallocing\n", file->fPath)); // We have relocated the kmem image so we are going to have to // move all of the pointers into the image around. } else { DEBUG_LOG(("Growing image %s by allocating\n", file->fPath)); // The image doesn't have room for us and I can't kmem_realloc // then I just have to bite the bullet and copy the object code // into a bigger memory segment ret = kmem_alloc(kernel_map, &newMachO, newsize); if (KERN_SUCCESS != ret) return false; bcopy((char *) startMachO, (void *) newMachO, file->fMachOSize); file->fIsKmem = true; } file->fMap = file->fMachO = (unsigned char *) newMachO; file->fMapSize = newsize; file->fMachOSize += delta; // Increment the image size // If we are here then we have shifted the object image in memory // I really should change all of my pointers into the image to machO offsets // but I have run out of time. So I'm going to very quickly go over the // cached data structures and add adjustments to the addresses that are // affected. I wonder how long it will take me to get them all. // // For every pointer into the MachO I need to add an adjustment satisfying // the following simultanous equations // addr_old = macho_old + fixed_offset // addr_new = macho_new + fixed_offset therefore: // addr_new = addr_old + (macho_new - macho_old) #define REBASE(addr, delta) ( ((vm_address_t) (addr)) += (delta) ) delta = newMachO - startMachO; // Rebase the cached-in object 'struct symtab_command' pointer REBASE(file->fSymtab, delta); // Rebase the cached-in object 'struct nlist' pointer for all symbols REBASE(file->fSymbolBase, delta); // Rebase the cached-in object 'struct nlist' pointer for local symbols REBASE(file->fLocalSyms, delta); // Rebase the cached-in object 'char' pointer for the string table REBASE(file->fStringBase, delta); // Ok now we have to go over all of the relocs one last time // to clean up the pad updates which had their string index negated // to indicate that we have finished with them. section = file->fSections; for (i = 0, last = file->fNSects; i < last; i++, section++) REBASE(section->fSection, delta); // We only ever grow images that contain class lists so dont bother // the check if file->fClassList is non-zero 'cause it can't be // assert(file->fClassList); last = DataGetLength(file->fClassList) / sizeof(struct metaClassRecord *); classes = (struct metaClassRecord **) DataGetPtr(file->fClassList); for (i = 0; i < last; i++) { struct patchRecord *patch; for (patch = classes[i]->fPatchedVTable; patch->fSymbol; patch++) { vm_address_t symAddr = (vm_address_t) patch->fSymbol; // Only need to rebase if the symbol is part of the image // If this is a new symbol then it was independantly allocated if (symAddr >= startMachO && symAddr < endMachO) REBASE(patch->fSymbol, delta); } } // Finally rebase all of the string table pointers last = file->fSymtab->nsyms; for (i = 0; i < last; i++) REBASE(file->fSymbToStringTable[i], delta); #undef REBASE return true; #endif /* KERNEL */ } static Boolean prepareFileForLink(struct fileRecord *file) { unsigned long i, last, numnewsyms, newsymsize, newstrsize; struct sectionRecord *section; struct nlist **symp, *sym; DataRef newStrings, *stringBlocks; // If we didn't even do a pseudo 'relocate' and dirty the image // then we can just return now. if (!file->fImageDirty) return true; DEBUG_LOG(("Linking 2 %s\n", file->fPath)); // @@@ gvdl: // We have to go over all of the relocs to repair the damage // that we have done to the image when we did our 'relocation' section = file->fSections; for (i = 0, last = file->fNSects; i < last; i++, section++) { unsigned char *sectionBase; struct relocRecord *rec; unsigned long j, nreloc; if (section->fRelocCache) { sectionBase = file->fMachO + section->fSection->offset; nreloc = section->fSection->nreloc; rec = (struct relocRecord *) DataGetPtr(section->fRelocCache); // We will need to repair the reloc list for (j = 0; j < nreloc; j++, rec++) { void **entry; struct nlist *sym; // Repair Damage to object image entry = (void **) (sectionBase + rec->fRInfo->r_address); *entry = rec->fValue; // Check if the symbol that this relocation entry points // to is marked as erasable sym = (struct nlist *) rec->fSymbol; if (sym && sym->n_type == (N_EXT | N_UNDF) && sym->n_sect == (unsigned char) -1) { // It is in use so we better clear the mark sym->n_un.n_strx = -sym->n_un.n_strx; sym->n_sect = NO_SECT; } } // Clean up the fRelocCache we don't need it any more. DataRelease(section->fRelocCache); section->fRelocCache = 0; } } file->fImageDirty = false; // Image is clean // If we didn't dirty the symbol table then just return if (!file->fSymbolsDirty) return true; // calculate total file size increase and check against padding if (file->fNewSymbols) { numnewsyms = DataGetLength(file->fNewSymbols); symp = (struct nlist **) DataGetPtr(file->fNewSymbols); } else { numnewsyms = 0; symp = 0; } numnewsyms /= sizeof(struct nlist *); file->fSymtab->nsyms += numnewsyms; // old sting size + 30% rounded up to nearest page newstrsize = file->fSymtab->strsize * 21 / 16; newstrsize = (newstrsize + PAGE_MASK) & ~PAGE_MASK; newStrings = DataCreate(newstrsize); return_if(!newStrings, false, ("Unable to allocate a copy aside buffer, no memory\n")); newsymsize = numnewsyms * sizeof(struct nlist); file->fStringBase += newsymsize; file->fSymtab->stroff += newsymsize; last = file->fSymtab->nsyms - numnewsyms; newstrsize = 0; DataAppendBytes(newStrings, &newstrsize, 4); // Leading nuls sym = file->fSymbolBase; // Pre-compute an already offset new symbol pointer. The offset is the // orignal symbol table. symp -= last; for (i = 0; i < file->fSymtab->nsyms; i++, sym++) { const char *str = symNameByIndex(file, i); int len = strlen(str) + 1; unsigned int strx; // Rebase sym in the new symbol region if (i >= last) sym = symp[i]; if (sym->n_un.n_strx < 0 && sym->n_type == (N_EXT | N_UNDF) && (unsigned char) -1 == sym->n_sect) { // after patching we find that this symbol is no longer in // use. So invalidate it by converting it into an N_ABS // symbol, remove the external bit and null out the name. bzero(sym, sizeof(*sym)); sym->n_type = N_ABS; } else { // Repair the symbol for the getNewSymbol case. if (-1 == sym->n_un.n_strx) sym->n_value = 0; // Record the offset of the string in the new table strx = DataGetLength(newStrings); return_if(!DataAppendBytes(newStrings, str, len), false, ("Unable to append string, no memory\n")); sym->n_un.n_strx = strx; file->fSymbToStringTable[i] = file->fStringBase + strx; } } // Don't need the new strings any more last = DataGetLength(file->fNewStringBlocks) / sizeof(DataRef); stringBlocks = (DataRef *) DataGetPtr(file->fNewStringBlocks); for (i = 0; i < last; i++) DataRelease(stringBlocks[i]); DataRelease(file->fNewStringBlocks); file->fNewStringBlocks = 0; newstrsize = DataGetLength(newStrings); newstrsize = (newstrsize + 3) & ~3; // Round to nearest word return_if( !growImage(file, newsymsize + newstrsize - file->fSymtab->strsize), false, ("Unable to patch the extension, no memory\n", file->fPath)); // Push out the new symbol table if necessary if (numnewsyms) { caddr_t base; // Append the new symbols to the original symbol table. base = (caddr_t) file->fSymbolBase + (file->fSymtab->nsyms - numnewsyms) * sizeof(struct nlist); symp = (struct nlist **) DataGetPtr(file->fNewSymbols); for (i = 0; i < numnewsyms; i++, base += sizeof(struct nlist), symp++) bcopy(*symp, base, sizeof(struct nlist)); DataRelease(file->fNewSymbols); file->fNewSymbols = 0; } // Push out the new string table if necessary if (newStrings) { unsigned long *base = (unsigned long *) file->fStringBase; unsigned long actuallen = DataGetLength(newStrings); // Set the last word in string table to zero before copying data base[(newstrsize / sizeof(unsigned long)) - 1] = 0; // Now copy the new strings back to the end of the file bcopy((caddr_t) DataGetPtr(newStrings), file->fStringBase, actuallen); file->fSymtab->strsize = newstrsize; DataRelease(newStrings); } file->fSymbolsDirty = false; return true; } Boolean #if KERNEL kld_file_map(const char *pathName, unsigned char *map, size_t mapSize, Boolean isKmem) #else kld_file_map(const char *pathName) #endif /* KERNEL */ { struct fileRecord file, *fp = 0; // Already done no need to repeat fp = getFile(pathName); if (fp) return true; bzero(&file, sizeof(file)); #if KERNEL file.fMap = map; file.fMapSize = mapSize; file.fIsKmem = isKmem; #else if (!mapObjectFile(&file, pathName)) return false; #endif /* KERNEL */ do { const struct machOMapping { struct mach_header h; struct load_command c[1]; } *machO; const struct load_command *cmd; int i; if (!findBestArch(&file, pathName)) break; machO = (const struct machOMapping *) file.fMachO; if (file.fMachOSize < machO->h.sizeofcmds) break; file.fIsKernel = (MH_EXECUTE == machO->h.filetype); // If the file type is MH_EXECUTE then this must be a kernel // as all Kernel extensions must be of type MH_OBJECT for (i = 0, cmd = &machO->c[0]; i < machO->h.ncmds; i++) { if (cmd->cmd == LC_SYMTAB) file.fSymtab = (struct symtab_command *) cmd; else if (cmd->cmd == LC_SEGMENT) { struct segment_command *seg = (struct segment_command *) cmd; int nsects = seg->nsects; if (nsects) return_if(!parseSegments(&file, seg), false, ("%s isn't a valid mach-o, bad segment", pathName)); else if (file.fIsKernel) { #if KERNEL // We don't need to look for the LinkEdit segment unless // we are running in the kernel environment. if (!strcmp(kLinkEditSegName, seg->segname)) file.fLinkEditSeg = seg; #endif } } cmd = (struct load_command *) ((UInt8 *) cmd + cmd->cmdsize); } break_if(!file.fSymtab, ("%s isn't a valid mach-o, no symbols\n", pathName)); if (!parseSymtab(&file, pathName)) break; fp = addFile(&file, pathName); if (!fp) break; if (file.fFoundOSObject && !getMetaClassGraph(fp)) break; if (file.fIsKernel) sKernelFile = fp; #if KERNEL // Automatically load the kernel's link edit segment if we are // attempting to load a driver. if (!sKernelFile) { extern struct mach_header _mh_execute_header; extern struct segment_command *getsegbyname(char *seg_name); struct segment_command *sg; size_t kernelSize; Boolean ret; sg = (struct segment_command *) getsegbyname(kLinkEditSegName); break_if(!sg, ("Can't find kernel link edit segment\n")); kernelSize = sg->vmaddr + sg->vmsize - (size_t) &_mh_execute_header; ret = kld_file_map(kld_basefile_name, (unsigned char *) &_mh_execute_header, kernelSize, /* isKmem */ false); break_if(!ret, ("kld can't map kernel file")); } #endif /* KERNEL */ return true; } while(0); // Failure path, then clean up if (fp) // @@@ gvdl: for the time being leak the file ref in the file table removeFile(fp); else unmapFile(&file); return false; } void *kld_file_getaddr(const char *pathName, long *size) { struct fileRecord *file = getFile(pathName); if (!file) return 0; if (size) *size = file->fMachOSize; return file->fMachO; } void *kld_file_lookupsymbol(const char *pathName, const char *symname) { struct fileRecord *file = getFile(pathName); const struct nlist *sym; const struct section *section; unsigned char *sectionBase; unsigned char sectind; return_if(!file, NULL, ("Unknown file %s\n", pathName)); sym = findSymbolByName(file, symname); // May be a non-extern symbol so look for it there if (!sym) { unsigned int i, nsyms; sym = file->fSymbolBase; for (i = 0, nsyms = file->fSymtab->nsyms; i < nsyms; i++, sym++) { if ( (sym->n_type & N_EXT) ) { sym = 0; break; // Terminate search when we hit an extern } if ( (sym->n_type & N_STAB) ) continue; if ( !strcmp(symname, symNameByIndex(file, i)) ) break; } } return_if(!sym, NULL, ("Unknown symbol %s in %s\n", symname, pathName)); // Is the vtable in a valid section? sectind = sym->n_sect; return_if(sectind == NO_SECT || sectind > file->fNSects, NULL, ("Malformed object file, invalid section reference for %s in %s\n", symname, pathName)); section = file->fSections[sectind - 1].fSection; sectionBase = file->fMachO + section->offset - section->addr; return (void *) (sectionBase + sym->n_value); } Boolean kld_file_merge_OSObjects(const char *pathName) { struct fileRecord *file = getFile(pathName); return_if(!file, false, ("Internal error - unable to find file %s\n", pathName)); return mergeOSObjectsForFile(file); } Boolean kld_file_patch_OSObjects(const char *pathName) { struct fileRecord *file = getFile(pathName); struct metaClassRecord **classes; unsigned long i, last; return_if(!file, false, ("Internal error - unable to find file %s\n", pathName)); DEBUG_LOG(("Patch file %s\n", pathName)); // @@@ gvdl: // If we don't have any classes we can return now. if (!file->fClassList) return true; // If we haven't alread merged the kernel then do it now if (!sMergedKernel && sKernelFile) mergeOSObjectsForFile(sKernelFile); return_if(!sMergedKernel, false, ("Internal error no kernel?\n")); if (!mergeOSObjectsForFile(file)) return false; // Patch all of the classes in this executable last = DataGetLength(file->fClassList) / sizeof(void *); classes = (struct metaClassRecord **) DataGetPtr(file->fClassList); for (i = 0; i < last; i++) { if (!patchVTable(classes[i])) { // RY: Set a flag in the file list to invalidate this data. // I would remove the file from the list, but that seems to be // not worth the effort. file->fIgnoreFile = TRUE; return false; } } return true; } Boolean kld_file_prepare_for_link() { if (sMergedFiles) { unsigned long i, nmerged = 0; struct fileRecord **files; // Check to see if we have already merged this file nmerged = DataGetLength(sMergedFiles) / sizeof(struct fileRecord *); files = (struct fileRecord **) DataGetPtr(sMergedFiles); for (i = 0; i < nmerged; i++) { if (!files[i]->fIgnoreFile && !prepareFileForLink(files[i])) return false; } } // Clear down the meta class table and merged file lists DataRelease(sMergeMetaClasses); DataRelease(sMergedFiles); sMergedFiles = sMergeMetaClasses = NULL; sMergedKernel = false; return true; } void kld_file_cleanup_all_resources() { unsigned long i, nfiles; #if KERNEL // @@@ gvdl: // Debugger("kld_file_cleanup_all_resources"); #endif if (!sFilesTable || !(nfiles = DataGetLength(sFilesTable))) return; // Nothing to do just return now nfiles /= sizeof(struct fileRecord *); for (i = 0; i < nfiles; i++) removeFile(((void **) DataGetPtr(sFilesTable))[i]); DataRelease(sFilesTable); sFilesTable = NULL; // Don't really have to clean up anything more as the whole // malloc engine is going to be released and I couldn't be bothered. } #if !KERNEL #if 0 static const struct fileRecord *sortFile; static int symCompare(const void *vSym1, const void *vSym2) { const struct nlist *sym1 = vSym1; const struct nlist *sym2 = vSym2; { unsigned int ind1, ind2; ind1 = sym1->n_type & N_TYPE; ind2 = sym2->n_type & N_TYPE; if (ind1 != ind2) { // if sym1 is undefined then sym1 must come later than sym2 if (ind1 == N_UNDF) return 1; // if sym2 is undefined then sym1 must come earlier than sym2 if (ind2 == N_UNDF) return -1; /* drop out if neither are undefined */ } } { const struct fileRecord *file = sortFile; const char *name1, *name2; name1 = file->fStringBase + sym1->n_un.n_strx; name2 = file->fStringBase + sym2->n_un.n_strx; return strcmp(name1, name2); } } #endif /* 0 */ Boolean kld_file_debug_dump(const char *pathName, const char *outName) { const struct fileRecord *file = getFile(pathName); int fd; Boolean ret = false; return_if(!file, false, ("Unknown file %s for dumping\n", pathName)); fd = open(outName, O_WRONLY|O_CREAT|O_TRUNC, 0666); return_if(-1 == fd, false, ("Can't create output file %s - %s(%d)\n", outName, strerror(errno), errno)); do { #if 0 // Sorting doesn't work until I fix the relocs too? // sort the symbol table appropriately unsigned int nsyms = file->fSymtab->nsyms - (file->fLocalSyms - file->fSymbolBase); sortFile = file; heapsort((void *) file->fLocalSyms, nsyms, sizeof(struct nlist), symCompare); #endif break_if(-1 == write(fd, file->fMachO, file->fMachOSize), ("Can't dump output file %s - %s(%d)\n", outName, strerror(errno), errno)); ret = true; } while(0); close(fd); return ret; } #endif /* !KERNEL */ |