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
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- * * Copyright (c) 2014 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * 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 2.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.opensource.apple.com/apsl/ 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, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <TargetConditionals.h> #if !TARGET_OS_EXCLAVEKIT #include "ObjCVisitor.h" #if SUPPORT_VM_LAYOUT #include "MachOAnalyzer.h" #endif #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS #include "ASLRTracker.h" #include <unordered_set> #endif using namespace objc_visitor; using ResolvedValue = metadata_visitor::ResolvedValue; using mach_o::Header; #if !SUPPORT_VM_LAYOUT using metadata_visitor::Segment; #endif // // MARK: --- Class methods --- // const void* Class::getFieldPos(const Visitor& objcVisitor, Field field) const { if ( objcVisitor.pointerSize == 4 ) { const class32_t* class32 = (const class32_t*)this->classPos.value(); switch ( field ) { case Field::isa: return &class32->isaVMAddr; case Field::superclass: return &class32->superclassVMAddr; case Field::methodCacheBuckets: return &class32->methodCacheBuckets; case Field::methodCacheProperties: return &class32->methodCacheProperties; case Field::data: return &class32->dataVMAddrAndFastFlags; case Field::swiftClassFlags: return &class32->swiftClassFlags; } } else { const class64_t* class64 = (const class64_t*)this->classPos.value(); switch ( field ) { case Field::isa: return &class64->isaVMAddr; case Field::superclass: return &class64->superclassVMAddr; case Field::methodCacheBuckets: return &class64->methodCacheBuckets; case Field::methodCacheProperties: return &class64->methodCacheProperties; case Field::data: return &class64->dataVMAddrAndFastFlags; case Field::swiftClassFlags: return &class64->swiftClassFlags; } } } ResolvedValue Class::getISA(const Visitor& objcVisitor, bool& isPatchableClass) const { ResolvedValue field = objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::isa)); return objcVisitor.resolveBindOrRebase(field, isPatchableClass); } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS || BUILDING_SHARED_CACHE_UTIL std::optional<ResolvedValue> Class::getSuperclass(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::superclass)); return objcVisitor.resolveOptionalRebase(field); } std::optional<VMAddress> Class::getSuperclassVMAddr(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::superclass)); std::optional<VMAddress> targetAddress = objcVisitor.resolveOptionalRebaseToVMAddress(field); if ( !targetAddress.has_value() ) return std::nullopt; return targetAddress; } #endif ResolvedValue Class::getSuperclassField(const Visitor& objcVisitor) const { return objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::superclass)); } ResolvedValue Class::getMethodCache(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::methodCacheBuckets)); bool unusedIsPatchableClass; return objcVisitor.resolveBindOrRebase(field, unusedIsPatchableClass); } std::optional<ResolvedValue> Class::getMethodCacheProperties(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::methodCacheProperties)); return objcVisitor.resolveOptionalRebase(field); } ResolvedValue Class::getMethodCachePropertiesField(const Visitor& objcVisitor) const { return objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::methodCacheProperties)); } std::optional<VMAddress> Class::getMethodCachePropertiesVMAddr(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::methodCacheProperties)); // The field might be null. Get the target value, then see if it has a value std::optional<ResolvedValue> targetValue = objcVisitor.resolveOptionalRebase(field); if ( targetValue.has_value() ) return targetValue->vmAddress(); return { }; } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS void Class::setMethodCachePropertiesVMAddr(const Visitor& objcVisitor, VMAddress vmAddr, const dyld3::MachOFile::PointerMetaData& PMD) { ResolvedValue field = objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::methodCacheProperties)); objcVisitor.setTargetVMAddress(field, CacheVMAddress(vmAddr.rawValue()), PMD); } #endif #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS || BUILDING_SHARED_CACHE_UTIL void Class::withSuperclass(const Visitor& objcVisitor, void (^handler)(const dyld3::MachOFile::ChainedFixupPointerOnDisk* fixup, uint16_t pointerFormat)) const { // HACK: The visitor classes need to be refactored to handle cache util. For now just force the caller of this method in // cache util to have the chain format #if BUILDING_SHARED_CACHE_UTIL uint16_t chainedPointerFormat = 0; #else uint16_t chainedPointerFormat = this->classPos.chainedPointerFormat().value(); #endif const void* fieldPos = this->getFieldPos(objcVisitor, Field::superclass); dyld3::MachOFile::ChainedFixupPointerOnDisk* fieldFixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)fieldPos; handler(fieldFixup, chainedPointerFormat); } #endif bool Class::isUnfixedBackwardDeployingStableSwift(const Visitor& objcVisitor) const { // Only classes marked as Swift legacy need apply. if ( !this->isSwiftLegacy(objcVisitor) ) return false; std::optional<uint32_t> swiftClassFlags = this->swiftClassFlags(objcVisitor); if ( swiftClassFlags.has_value() ) { // Check the true legacy vs stable distinguisher. // The low bit of Swift's ClassFlags is SET for true legacy // and UNSET for stable pretending to be legacy. bool isActuallySwiftLegacy = (swiftClassFlags.value() & isSwiftPreStableABI) != 0; return !isActuallySwiftLegacy; } else { // Is this possible? We were a legacy class but had no flags? return false; } } bool Class::isSwiftLegacy(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::data)); ResolvedValue fieldValue = objcVisitor.resolveRebase(field); // Mask out the flags from the data value uint64_t rawDataVMAddr = fieldValue.vmAddress().rawValue(); return (rawDataVMAddr & FAST_IS_SWIFT_LEGACY) != 0; } bool Class::isSwiftStable(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::data)); ResolvedValue fieldValue = objcVisitor.resolveRebase(field); // Mask out the flags from the data value uint64_t rawDataVMAddr = fieldValue.vmAddress().rawValue(); return (rawDataVMAddr & FAST_IS_SWIFT_STABLE) != 0; } bool Class::isSwift(const Visitor& objcVisitor) const { return this->isSwiftStable(objcVisitor) || this->isSwiftLegacy(objcVisitor); } std::optional<uint32_t> Class::swiftClassFlags(const Visitor& objcVisitor) const { if ( !this->isSwift(objcVisitor) ) return { }; return *(uint32_t*)this->getFieldPos(objcVisitor, Field::swiftClassFlags); } ResolvedValue Class::getDataField(const Visitor& objcVisitor) const { return objcVisitor.getField(this->classPos, this->getFieldPos(objcVisitor, Field::data)); } ClassData Class::getClassData(const Visitor& objcVisitor) const { ResolvedValue field = this->getDataField(objcVisitor); ResolvedValue targetValue = objcVisitor.resolveRebase(field); // Mask out the low bits, if they are set VMAddress vmAddr = targetValue.vmAddress(); uint64_t mask = (objcVisitor.pointerSize == 4) ? (uint64_t)FAST_DATA_MASK32 : FAST_DATA_MASK64; uint64_t rawVMAddr = vmAddr.rawValue(); uint64_t maskedVMAddr = rawVMAddr & mask; if ( maskedVMAddr != rawVMAddr ) { // Adjust the pointer as we have bits to remove uint64_t adjust = rawVMAddr - maskedVMAddr; const uint8_t* unadjustedValue = (const uint8_t*)targetValue.value(); const uint8_t* adjustedValue = unadjustedValue - adjust; //return ClassData(ResolvedValue(targetValue, adjustedValue)); // We can't just construct a new ResolvedValue here with the adjusted value as we don't // have the right constructors when building for dyld. Instead we'll pretend we are // just accessing a field of a struct, where the value we have is the struct and we are // resolving to a value before it. Eg, "struct foo ... return &foo - 2" ResolvedValue adjustedField = objcVisitor.getField(targetValue, adjustedValue); return ClassData(adjustedField); } return ClassData(targetValue); } VMAddress Class::getClassDataVMAddr(const Visitor& objcVisitor) const { return getClassData(objcVisitor).getVMAddress(); } bool Class::isRootClass(const Visitor& objcVisitor) const { ClassData data = getClassData(objcVisitor); uint32_t flags = *(uint32_t*)data.getFieldPos(objcVisitor, ClassData::Field::flags); const uint32_t RO_ROOT = (1 << 1); return (flags & RO_ROOT) != 0; } const char* Class::getName(const Visitor& objcVisitor) const { ClassData classData = getClassData(objcVisitor); ResolvedValue field = classData.getField(objcVisitor, ClassData::Field::name); return (const char*)objcVisitor.resolveRebase(field).value(); } VMAddress Class::getNameVMAddr(const Visitor& objcVisitor) const { ClassData classData = getClassData(objcVisitor); ResolvedValue field = classData.getField(objcVisitor, ClassData::Field::name); return objcVisitor.resolveRebase(field).vmAddress(); } MethodList Class::getBaseMethods(const Visitor& objcVisitor) const { ClassData classData = getClassData(objcVisitor); ResolvedValue field = classData.getField(objcVisitor, ClassData::Field::baseMethods); return objcVisitor.resolveOptionalRebase(field); } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS ResolvedValue Class::setBaseMethodsVMAddr(const Visitor& objcVisitor, VMAddress vmAddr, const dyld3::MachOFile::PointerMetaData& PMD) { ClassData classData = getClassData(objcVisitor); ResolvedValue field = classData.getField(objcVisitor, ClassData::Field::baseMethods);; objcVisitor.setTargetVMAddress(field, CacheVMAddress(vmAddr.rawValue()), PMD); return field; } #endif ProtocolList Class::getBaseProtocols(const Visitor& objcVisitor) const { ClassData classData = getClassData(objcVisitor); ResolvedValue field = classData.getField(objcVisitor, ClassData::Field::baseProtocols); return objcVisitor.resolveOptionalRebase(field); } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS ResolvedValue Class::setBaseProtocolsVMAddr(const Visitor& objcVisitor, VMAddress vmAddr) { ClassData classData = getClassData(objcVisitor); ResolvedValue field = classData.getField(objcVisitor, ClassData::Field::baseProtocols); objcVisitor.updateTargetVMAddress(field, CacheVMAddress(vmAddr.rawValue())); return field; } #endif IVarList Class::getIVars(const Visitor& objcVisitor) const { ClassData classData = getClassData(objcVisitor); ResolvedValue field = classData.getField(objcVisitor, ClassData::Field::ivars); return objcVisitor.resolveOptionalRebase(field); } PropertyList Class::getBaseProperties(const Visitor& objcVisitor) const { ClassData classData = getClassData(objcVisitor); ResolvedValue field = classData.getField(objcVisitor, ClassData::Field::baseProperties); return objcVisitor.resolveOptionalRebase(field); } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS ResolvedValue Class::setBasePropertiesVMAddr(const Visitor& objcVisitor, VMAddress vmAddr) { ClassData classData = getClassData(objcVisitor); ResolvedValue field = classData.getField(objcVisitor, ClassData::Field::baseProperties); objcVisitor.updateTargetVMAddress(field, CacheVMAddress(vmAddr.rawValue())); return field; } #endif uint32_t Class::getInstanceStart(const Visitor& objcVisitor) const { ClassData data = getClassData(objcVisitor); return *(uint32_t*)data.getFieldPos(objcVisitor, ClassData::Field::instanceStart); } void Class::setInstanceStart(const Visitor& objcVisitor, uint32_t value) const { ClassData data = getClassData(objcVisitor); *(uint32_t*)data.getFieldPos(objcVisitor, ClassData::Field::instanceStart) = value; } uint32_t Class::getInstanceSize(const Visitor& objcVisitor) const { ClassData data = getClassData(objcVisitor); return *(uint32_t*)data.getFieldPos(objcVisitor, ClassData::Field::instanceSize); } void Class::setInstanceSize(const Visitor& objcVisitor, uint32_t value) const { ClassData data = getClassData(objcVisitor); *(uint32_t*)data.getFieldPos(objcVisitor, ClassData::Field::instanceSize) = value; } const void* Class::getLocation() const { return this->classPos.value(); } VMAddress Class::getVMAddress() const { return this->classPos.vmAddress(); } // // MARK: --- ClassData methods --- // const void* ClassData::getFieldPos(const Visitor& objcVisitor, Field field) const { if ( objcVisitor.pointerSize == 4 ) { const data32_t* data32 = (const data32_t*)this->classDataPos.value(); switch ( field ) { case Field::flags: return &data32->flags; case Field::instanceStart: return &data32->instanceStart; case Field::instanceSize: return &data32->instanceSize; case Field::ivarLayout: return &data32->ivarLayoutVMAddr; case Field::name: return &data32->nameVMAddr; case Field::baseMethods: return &data32->baseMethodsVMAddr; case Field::baseProtocols: return &data32->baseProtocolsVMAddr; case Field::ivars: return &data32->ivarsVMAddr; case Field::weakIvarLayout: return &data32->weakIvarLayoutVMAddr; case Field::baseProperties: return &data32->basePropertiesVMAddr; } } else { const data64_t* data64 = (const data64_t*)this->classDataPos.value(); switch ( field ) { case Field::flags: return &data64->flags; case Field::instanceStart: return &data64->instanceStart; case Field::instanceSize: return &data64->instanceSize; case Field::ivarLayout: return &data64->ivarLayoutVMAddr; case Field::name: return &data64->nameVMAddr; case Field::baseMethods: return &data64->baseMethodsVMAddr; case Field::baseProtocols: return &data64->baseProtocolsVMAddr; case Field::ivars: return &data64->ivarsVMAddr; case Field::weakIvarLayout: return &data64->weakIvarLayoutVMAddr; case Field::baseProperties: return &data64->basePropertiesVMAddr; } } } ResolvedValue ClassData::getField(const Visitor& objcVisitor, Field field) const { return objcVisitor.getField(this->classDataPos, this->getFieldPos(objcVisitor, field)); } const void* ClassData::getLocation() const { return this->classDataPos.value(); } VMAddress ClassData::getVMAddress() const { return this->classDataPos.vmAddress(); } // // MARK: --- Category methods --- // const void* Category::getFieldPos(const Visitor& objcVisitor, Field field) const { if ( objcVisitor.pointerSize == 4 ) { const category32_t* category32 = (const category32_t*)this->categoryPos.value(); switch ( field ) { case Field::name: return &category32->nameVMAddr; case Field::cls: return &category32->clsVMAddr; case Field::instanceMethods: return &category32->instanceMethodsVMAddr; case Field::classMethods: return &category32->classMethodsVMAddr; case Field::protocols: return &category32->protocolsVMAddr; case Field::instanceProperties: return &category32->instancePropertiesVMAddr; case Field::classProperties: return &category32->classPropertiesVMAddr; } } else { const category64_t* category64 = (const category64_t*)this->categoryPos.value(); switch ( field ) { case Field::name: return &category64->nameVMAddr; case Field::cls: return &category64->clsVMAddr; case Field::instanceMethods: return &category64->instanceMethodsVMAddr; case Field::classMethods: return &category64->classMethodsVMAddr; case Field::protocols: return &category64->protocolsVMAddr; case Field::instanceProperties: return &category64->instancePropertiesVMAddr; case Field::classProperties: return &category64->classPropertiesVMAddr; } } } const char* Category::getName(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->categoryPos, this->getFieldPos(objcVisitor, Field::name)); return (const char*)objcVisitor.resolveRebase(field).value(); } VMAddress Category::getVMAddress() const { return this->categoryPos.vmAddress(); } const void* Category::getLocation() const { return this->categoryPos.value(); } VMAddress Category::getNameVMAddr(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->categoryPos, this->getFieldPos(objcVisitor, Field::name)); return objcVisitor.resolveRebase(field).vmAddress(); } MethodList Category::getInstanceMethods(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->categoryPos, this->getFieldPos(objcVisitor, Field::instanceMethods)); return objcVisitor.resolveOptionalRebase(field); } MethodList Category::getClassMethods(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->categoryPos, this->getFieldPos(objcVisitor, Field::classMethods)); return objcVisitor.resolveOptionalRebase(field); } ProtocolList Category::getProtocols(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->categoryPos, this->getFieldPos(objcVisitor, Field::protocols)); return objcVisitor.resolveOptionalRebase(field); } PropertyList Category::getInstanceProperties(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->categoryPos, this->getFieldPos(objcVisitor, Field::instanceProperties)); return objcVisitor.resolveOptionalRebase(field); } PropertyList Category::getClassProperties(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->categoryPos, this->getFieldPos(objcVisitor, Field::classProperties)); return objcVisitor.resolveOptionalRebase(field); } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS || BUILDING_SHARED_CACHE_UTIL void Category::withClass(const Visitor& objcVisitor, void (^handler)(const dyld3::MachOFile::ChainedFixupPointerOnDisk* fixup, uint16_t pointerFormat)) const { // HACK: The visitor classes need to be refactored to handle cache util. For now just force the caller of this method in // cache util to have the chain format #if BUILDING_SHARED_CACHE_UTIL uint16_t chainedPointerFormat = 0; #else assert(objcVisitor.isOnDiskBinary()); uint16_t chainedPointerFormat = this->categoryPos.chainedPointerFormat().value(); #endif const void* fieldPos = nullptr; if ( objcVisitor.pointerSize == 8 ) { fieldPos = &((const category64_t*)this->categoryPos.value())->clsVMAddr; } else if (objcVisitor.pointerSize == 4 ){ fieldPos = &((const category32_t*)this->categoryPos.value())->clsVMAddr; } assert(fieldPos != nullptr); dyld3::MachOFile::ChainedFixupPointerOnDisk* fieldFixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)fieldPos; handler(fieldFixup, chainedPointerFormat); } #endif uint32_t Category::getSize(bool is64) { return is64 ? sizeof(category64_t) : sizeof(category32_t); } #if BUILDING_SHARED_CACHE_UTIL std::optional<VMAddress> Category::getClassVMAddr(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->categoryPos, this->getFieldPos(objcVisitor, Field::cls)); std::optional<ResolvedValue> targetValue = objcVisitor.resolveOptionalRebase(field); if ( targetValue ) return targetValue->vmAddress(); return { }; } #endif // // MARK: --- Protocol methods --- // const void* Protocol::getFieldPos(const Visitor& objcVisitor, Field field) const { if ( objcVisitor.pointerSize == 4 ) { const protocol32_t* protocol32 = (const protocol32_t*)this->protocolPos.value(); switch ( field ) { case Field::isa: return &protocol32->isaVMAddr; case Field::name: return &protocol32->nameVMAddr; case Field::protocols: return &protocol32->protocolsVMAddr; case Field::instanceMethods: return &protocol32->instanceMethodsVMAddr; case Field::classMethods: return &protocol32->classMethodsVMAddr; case Field::optionalInstanceMethods: return &protocol32->optionalInstanceMethodsVMAddr; case Field::optionalClassMethods: return &protocol32->optionalClassMethodsVMAddr; case Field::instanceProperties: return &protocol32->instancePropertiesVMAddr; case Field::size: return &protocol32->size; case Field::flags: return &protocol32->flags; case Field::extendedMethodTypes: return &protocol32->extendedMethodTypesVMAddr; case Field::demangledName: return &protocol32->demangledNameVMAddr; case Field::classProperties: return &protocol32->classPropertiesVMAddr; } } else { const protocol64_t* protocol64 = (const protocol64_t*)this->protocolPos.value(); switch ( field ) { case Field::isa: return &protocol64->isaVMAddr; case Field::name: return &protocol64->nameVMAddr; case Field::protocols: return &protocol64->protocolsVMAddr; case Field::instanceMethods: return &protocol64->instanceMethodsVMAddr; case Field::classMethods: return &protocol64->classMethodsVMAddr; case Field::optionalInstanceMethods: return &protocol64->optionalInstanceMethodsVMAddr; case Field::optionalClassMethods: return &protocol64->optionalClassMethodsVMAddr; case Field::instanceProperties: return &protocol64->instancePropertiesVMAddr; case Field::size: return &protocol64->size; case Field::flags: return &protocol64->flags; case Field::extendedMethodTypes: return &protocol64->extendedMethodTypesVMAddr; case Field::demangledName: return &protocol64->demangledNameVMAddr; case Field::classProperties: return &protocol64->classPropertiesVMAddr; } } } std::optional<VMAddress> Protocol::getISAVMAddr(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::isa)); std::optional<ResolvedValue> value = objcVisitor.resolveOptionalRebase(field); if ( value ) return value->vmAddress(); return { }; } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS void Protocol::setISA(const Visitor& objcVisitor, VMAddress vmAddr, const dyld3::MachOFile::PointerMetaData& PMD) { ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::isa)); objcVisitor.setTargetVMAddress(field, CacheVMAddress(vmAddr.rawValue()), PMD); } #endif const char* Protocol::getName(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::name)); return (const char*)objcVisitor.resolveRebase(field).value(); } VMAddress Protocol::getNameVMAddr(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::name)); return objcVisitor.resolveRebase(field).vmAddress(); } ProtocolList Protocol::getProtocols(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::protocols)); return objcVisitor.resolveOptionalRebase(field); } MethodList Protocol::getInstanceMethods(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::instanceMethods)); return objcVisitor.resolveOptionalRebase(field); } MethodList Protocol::getClassMethods(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::classMethods)); return objcVisitor.resolveOptionalRebase(field); } MethodList Protocol::getOptionalInstanceMethods(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::optionalInstanceMethods)); return objcVisitor.resolveOptionalRebase(field); } MethodList Protocol::getOptionalClassMethods(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::optionalClassMethods)); return objcVisitor.resolveOptionalRebase(field); } uint32_t Protocol::getSize(const Visitor& objcVisitor) const { return *(uint32_t*)this->getFieldPos(objcVisitor, Field::size); } void Protocol::setSize(const Visitor& objcVisitor, uint32_t size) { *(uint32_t*)this->getFieldPos(objcVisitor, Field::size) = size; } void Protocol::setFixedUp(const Visitor& objcVisitor) { uint32_t& flags = *(uint32_t*)this->getFieldPos(objcVisitor, Field::flags); assert((flags & (1<<30)) == 0); flags = flags | (1<<30); } void Protocol::setIsCanonical(const Visitor& objcVisitor) { uint32_t& flags = *(uint32_t*)this->getFieldPos(objcVisitor, Field::flags); assert((flags & (1<<29)) == 0); flags = flags | (1<<29); } std::optional<ResolvedValue> Protocol::getExtendedMethodTypes(const Visitor& objcVisitor) const { // extendedMethodTypes is not always present on disk. uint32_t structSize = this->getSize(objcVisitor); if ( objcVisitor.pointerSize == 4 ) { if ( structSize < (offsetof(protocol32_t, extendedMethodTypesVMAddr) + sizeof(protocol32_t::extendedMethodTypesVMAddr)) ) return std::nullopt; } else { if ( structSize < (offsetof(protocol64_t, extendedMethodTypesVMAddr) + sizeof(protocol64_t::extendedMethodTypesVMAddr)) ) return std::nullopt; } ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::extendedMethodTypes)); return objcVisitor.resolveOptionalRebase(field); } std::optional<const char*> Protocol::getDemangledName(const Visitor& objcVisitor) const { // demangledName is not always present on disk. uint32_t structSize = this->getSize(objcVisitor); if ( objcVisitor.pointerSize == 4 ) { if ( structSize < (offsetof(protocol32_t, demangledNameVMAddr) + sizeof(protocol32_t::demangledNameVMAddr)) ) return std::nullopt; } else { if ( structSize < (offsetof(protocol64_t, demangledNameVMAddr) + sizeof(protocol64_t::demangledNameVMAddr)) ) return std::nullopt; } ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::demangledName)); std::optional<ResolvedValue> value = objcVisitor.resolveOptionalRebase(field); if ( value.has_value() ) return (const char*)value->value(); return std::nullopt; } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS void Protocol::setDemangledName(const Visitor& objcVisitor, VMAddress vmAddr) { uint32_t structSize = this->getSize(objcVisitor); if ( objcVisitor.pointerSize == 4 ) { assert(structSize >= (offsetof(protocol32_t, demangledNameVMAddr) + sizeof(protocol32_t::demangledNameVMAddr))); } else { assert(structSize >= (offsetof(protocol64_t, demangledNameVMAddr) + sizeof(protocol64_t::demangledNameVMAddr))); } ResolvedValue field = objcVisitor.getField(this->protocolPos, this->getFieldPos(objcVisitor, Field::demangledName)); objcVisitor.updateTargetVMAddress(field, CacheVMAddress(vmAddr.rawValue())); } #endif #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS void Protocol::addFixups(const Visitor& objcVisitor, std::vector<void*>& fixups) const { if ( objcVisitor.pointerSize == 4 ) { protocol32_t* protocol = (protocol32_t*)this->protocolPos.value(); if ( protocol->isaVMAddr != 0 ) fixups.push_back(&protocol->isaVMAddr); if ( protocol->nameVMAddr != 0 ) fixups.push_back(&protocol->nameVMAddr); if ( protocol->protocolsVMAddr != 0 ) fixups.push_back(&protocol->protocolsVMAddr); if ( protocol->instanceMethodsVMAddr != 0 ) fixups.push_back(&protocol->instanceMethodsVMAddr); if ( protocol->classMethodsVMAddr != 0 ) fixups.push_back(&protocol->classMethodsVMAddr); if ( protocol->optionalInstanceMethodsVMAddr != 0 ) fixups.push_back(&protocol->optionalInstanceMethodsVMAddr); if ( protocol->optionalClassMethodsVMAddr != 0 ) fixups.push_back(&protocol->optionalClassMethodsVMAddr); if ( protocol->instancePropertiesVMAddr != 0 ) fixups.push_back(&protocol->instancePropertiesVMAddr); uint32_t structSize = ((const protocol32_t*)this->protocolPos.value())->size; if ( structSize >= (offsetof(protocol32_t, extendedMethodTypesVMAddr) + sizeof(protocol32_t::extendedMethodTypesVMAddr)) ) { if ( protocol->extendedMethodTypesVMAddr != 0 ) fixups.push_back(&protocol->extendedMethodTypesVMAddr); } if ( structSize >= (offsetof(protocol32_t, demangledNameVMAddr) + sizeof(protocol32_t::demangledNameVMAddr)) ) { if ( protocol->demangledNameVMAddr != 0 ) fixups.push_back(&protocol->demangledNameVMAddr); } if ( structSize >= (offsetof(protocol32_t, classPropertiesVMAddr) + sizeof(protocol32_t::classPropertiesVMAddr)) ) { if ( protocol->classPropertiesVMAddr != 0 ) fixups.push_back(&protocol->classPropertiesVMAddr); } } else { protocol64_t* protocol = (protocol64_t*)this->protocolPos.value(); if ( protocol->isaVMAddr != 0 ) fixups.push_back(&protocol->isaVMAddr); if ( protocol->nameVMAddr != 0 ) fixups.push_back(&protocol->nameVMAddr); if ( protocol->protocolsVMAddr != 0 ) fixups.push_back(&protocol->protocolsVMAddr); if ( protocol->instanceMethodsVMAddr != 0 ) fixups.push_back(&protocol->instanceMethodsVMAddr); if ( protocol->classMethodsVMAddr != 0 ) fixups.push_back(&protocol->classMethodsVMAddr); if ( protocol->optionalInstanceMethodsVMAddr != 0 ) fixups.push_back(&protocol->optionalInstanceMethodsVMAddr); if ( protocol->optionalClassMethodsVMAddr != 0 ) fixups.push_back(&protocol->optionalClassMethodsVMAddr); if ( protocol->instancePropertiesVMAddr != 0 ) fixups.push_back(&protocol->instancePropertiesVMAddr); uint32_t structSize = ((const protocol64_t*)this->protocolPos.value())->size; if ( structSize >= (offsetof(protocol64_t, extendedMethodTypesVMAddr) + sizeof(protocol64_t::extendedMethodTypesVMAddr)) ) { if ( protocol->extendedMethodTypesVMAddr != 0 ) fixups.push_back(&protocol->extendedMethodTypesVMAddr); } if ( structSize >= (offsetof(protocol64_t, demangledNameVMAddr) + sizeof(protocol64_t::demangledNameVMAddr)) ) { if ( protocol->demangledNameVMAddr != 0 ) fixups.push_back(&protocol->demangledNameVMAddr); } if ( structSize >= (offsetof(protocol64_t, classPropertiesVMAddr) + sizeof(protocol64_t::classPropertiesVMAddr)) ) { if ( protocol->classPropertiesVMAddr != 0 ) fixups.push_back(&protocol->classPropertiesVMAddr); } } } std::optional<uint16_t> Protocol::chainedPointerFormat() const { return this->protocolPos.chainedPointerFormat(); } #endif const void* Protocol::getLocation() const { return this->protocolPos.value(); } VMAddress Protocol::getVMAddress() const { return this->protocolPos.vmAddress(); } uint32_t Protocol::getSize(bool is64) { return is64 ? sizeof(protocol64_t) : sizeof(protocol32_t); } // // MARK: --- MethodList methods --- // uint32_t MethodList::numMethods() const { if ( !methodListPos.has_value() ) return 0; const ResolvedValue& methodListValue = this->methodListPos.value(); const method_list_t* methodList = (const method_list_t*)methodListValue.value(); assert(methodList != nullptr); return methodList->getMethodCount(); } uint32_t MethodList::listSize() const { if ( !methodListPos.has_value() ) return 0; const ResolvedValue& methodListValue = this->methodListPos.value(); const method_list_t* methodList = (const method_list_t*)methodListValue.value(); assert(methodList != nullptr); uint32_t size = sizeof(uint32_t) * 2; size += methodList->getMethodCount() * methodList->getMethodSize(); return size; } uint32_t MethodList::methodSize() const { if ( !methodListPos.has_value() ) return 0; const ResolvedValue& methodListValue = this->methodListPos.value(); const method_list_t* methodList = (const method_list_t*)methodListValue.value(); assert(methodList != nullptr); return methodList->getMethodSize(); } bool MethodList::usesRelativeOffsets() const { if ( !methodListPos.has_value() ) return false; const ResolvedValue& methodListValue = this->methodListPos.value(); const method_list_t* methodList = (const method_list_t*)methodListValue.value(); assert(methodList != nullptr); return methodList->usesRelativeOffsets(); } bool MethodList::usesOffsetsFromSelectorBuffer() const { if ( !methodListPos.has_value() ) return false; const ResolvedValue& methodListValue = this->methodListPos.value(); const method_list_t* methodList = (const method_list_t*)methodListValue.value(); assert(methodList != nullptr); return methodList->usesOffsetsFromSelectorBuffer(); } void MethodList::setIsUniqued() { if ( !methodListPos.has_value() ) return; const ResolvedValue& methodListValue = this->methodListPos.value(); method_list_t* methodList = (method_list_t*)methodListValue.value(); assert(methodList != nullptr); methodList->setIsUniqued(); } void MethodList::setIsSorted() { if ( !methodListPos.has_value() ) return; const ResolvedValue& methodListValue = this->methodListPos.value(); method_list_t* methodList = (method_list_t*)methodListValue.value(); assert(methodList != nullptr); methodList->setIsSorted(); } size_t MethodList::makeEmptyMethodList(void* buffer) { assert(buffer != nullptr); method_list_t* methodList = (method_list_t*)buffer; bzero(methodList, sizeof(method_list_t)); methodList->setIsUniqued(); methodList->setIsSorted(); return sizeof(method_list_t); } void MethodList::setUsesOffsetsFromSelectorBuffer() { if ( !methodListPos.has_value() ) return; const ResolvedValue& methodListValue = this->methodListPos.value(); method_list_t* methodList = (method_list_t*)methodListValue.value(); assert(methodList != nullptr); methodList->setUsesOffsetsFromSelectorBuffer(); } bool MethodList::isListOfLists() const { if ( !methodListPos.has_value() ) return false; const ResolvedValue& methodListValue = this->methodListPos.value(); return methodListValue.vmAddress().rawValue() & 1; } const void* MethodList::getLocation() const { if ( !this->methodListPos.has_value() ) return nullptr; return this->methodListPos->value(); } std::optional<VMAddress> MethodList::getVMAddress() const { if ( !this->methodListPos.has_value() ) return { }; return this->methodListPos->vmAddress(); } static Method::Kind getKind(const MethodList::method_list_t* methodList) { typedef Method::Kind Kind; if ( methodList->usesRelativeOffsets() ) return methodList->usesOffsetsFromSelectorBuffer() ? Kind::relativeDirect : Kind::relativeIndirect; else return Kind::pointer; } Method MethodList::getMethod(const Visitor& objcVisitor, uint32_t i) const { assert(methodListPos.has_value()); const ResolvedValue& methodListValue = this->methodListPos.value(); const method_list_t* methodList = (const method_list_t*)methodListValue.value(); assert(methodList != nullptr); const uint8_t* methodListBase = methodList->methodBase(); const uint8_t* method = methodListBase + (i * methodList->getMethodSize()); ResolvedValue methodValue = objcVisitor.getField(methodListValue, method); return Method(getKind(methodList), methodValue); } // // MARK: --- Method methods --- // const void* Method::getFieldPos(const Visitor& objcVisitor, Field field) const { if ( objcVisitor.pointerSize == 4 ) { const method32_t* method32 = (const method32_t*)this->methodPos.value(); switch ( field ) { case Field::name: return &method32->nameVMAddr; case Field::types: return &method32->typesVMAddr; case Field::imp: return &method32->impVMAddr; } } else { const method64_t* method64 = (const method64_t*)this->methodPos.value(); switch ( field ) { case Field::name: return &method64->nameVMAddr; case Field::types: return &method64->typesVMAddr; case Field::imp: return &method64->impVMAddr; } } } ResolvedValue Method::getNameField(const Visitor& objcVisitor) const { switch ( this->kind ) { case Kind::relativeIndirect: { assert(0); } case Kind::relativeDirect: { assert(0); } case Kind::pointer: { return objcVisitor.getField(this->methodPos, this->getFieldPos(objcVisitor, Field::name)); } } } ResolvedValue Method::getTypesField(const Visitor& objcVisitor) const { switch ( this->kind ) { case Kind::relativeIndirect: { assert(0); } case Kind::relativeDirect: { assert(0); } case Kind::pointer: { return objcVisitor.getField(this->methodPos, this->getFieldPos(objcVisitor, Field::types)); } } } ResolvedValue Method::getIMPField(const Visitor& objcVisitor) const { switch ( this->kind ) { case Kind::relativeIndirect: { assert(0); } case Kind::relativeDirect: { assert(0); } case Kind::pointer: { return objcVisitor.getField(this->methodPos, this->getFieldPos(objcVisitor, Field::imp)); } } } const char* Method::getName(const Visitor& objcVisitor) const { switch ( this->kind ) { case Kind::relativeIndirect: { // The uint32_t name field is an offset from itself to a selref. The selref then points to the selector string const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->nameOffset; int32_t relativeOffsetFromField = *(int32_t*)fieldPos; VMOffset relativeOffsetFromMethod((uint64_t)offsetof(relative_method_t, nameOffset) + relativeOffsetFromField); VMAddress methodVMAddr = this->methodPos.vmAddress(); VMAddress nameSelRefVMAddr = methodVMAddr + relativeOffsetFromMethod; ResolvedValue nameSelRefValue = objcVisitor.getValueFor(nameSelRefVMAddr); return (const char*)objcVisitor.resolveRebase(nameSelRefValue).value(); } case Kind::relativeDirect: { #if BUILDING_SHARED_CACHE_UTIL const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->nameOffset; uint32_t nameOffsetInBuffer = *(uint32_t*)fieldPos; VMAddress nameVMAddr = objcVisitor.sharedCacheSelectorStringsBaseAddress() + VMOffset((uint64_t)nameOffsetInBuffer); ResolvedValue nameValue = objcVisitor.getValueFor(nameVMAddr); return (const char*)nameValue.value(); #else // dyld should never walk direct methods as the objc closure optimizations skip cache dylibs assert(0); #endif } case Kind::pointer: { ResolvedValue nameField = this->getNameField(objcVisitor); return (const char*)objcVisitor.resolveRebase(nameField).value(); } } } const char* Method::getTypes(const Visitor& objcVisitor) const { switch ( this->kind ) { case Kind::relativeIndirect: { const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->typesOffset; int32_t relativeOffsetFromField = *(int32_t*)fieldPos; VMOffset relativeOffsetFromMethod((uint64_t)offsetof(relative_method_t, typesOffset) + relativeOffsetFromField); VMAddress methodVMAddr = this->methodPos.vmAddress(); VMAddress typeVMAddr = methodVMAddr + relativeOffsetFromMethod; ResolvedValue typeValue = objcVisitor.getValueFor(typeVMAddr); return (const char*)typeValue.value(); } case Kind::relativeDirect: { assert(0); } case Kind::pointer: { ResolvedValue typesField = this->getTypesField(objcVisitor); return (const char*)objcVisitor.resolveRebase(typesField).value(); } } } const void* Method::getIMP(const Visitor& objcVisitor) const { switch ( this->kind ) { case Kind::relativeIndirect: { const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->impOffset; int32_t relativeOffsetFromField = *(int32_t*)fieldPos; VMOffset relativeOffsetFromMethod((uint64_t)offsetof(relative_method_t, impOffset) + relativeOffsetFromField); VMAddress methodVMAddr = this->methodPos.vmAddress(); VMAddress impVMAddr = methodVMAddr + relativeOffsetFromMethod; ResolvedValue impValue = objcVisitor.getValueFor(impVMAddr); return (const char*)impValue.value(); } case Kind::relativeDirect: { assert(0); } case Kind::pointer: { ResolvedValue impField = this->getIMPField(objcVisitor); return (const char*)objcVisitor.resolveRebase(impField).value(); } } } // Get the selector string name. A method often indirects via a selector reference. This returns // the vmAddr of the final selector string, not the selector reference. VMAddress Method::getNameVMAddr(const Visitor& objcVisitor) const { switch ( this->kind ) { case Kind::relativeIndirect: { // The uint32_t name field is an offset from itself to a selref. The selref then points to the selector string const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->nameOffset; int32_t relativeOffsetFromField = *(int32_t*)fieldPos; VMOffset relativeOffsetFromMethod((uint64_t)offsetof(relative_method_t, nameOffset) + relativeOffsetFromField); VMAddress methodVMAddr = this->methodPos.vmAddress(); VMAddress nameSelRefVMAddr = methodVMAddr + relativeOffsetFromMethod; ResolvedValue nameSelRefValue = objcVisitor.getValueFor(nameSelRefVMAddr); return objcVisitor.resolveRebase(nameSelRefValue).vmAddress(); } case Kind::relativeDirect: { #if BUILDING_DYLD || BUILDING_CLOSURE_UTIL || BUILDING_UNIT_TESTS // dyld should never walk direct methods as the objc closure optimizations skip cache dylibs assert(0); #else const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->nameOffset; uint32_t nameOffsetInBuffer = *(uint32_t*)fieldPos; return objcVisitor.sharedCacheSelectorStringsBaseAddress() + VMOffset((uint64_t)nameOffsetInBuffer); #endif } case Kind::pointer: { ResolvedValue nameSelRefValue = this->getNameField(objcVisitor); return objcVisitor.resolveRebase(nameSelRefValue).vmAddress(); } } } VMAddress Method::getTypesVMAddr(const Visitor& objcVisitor) const { switch ( this->kind ) { case Kind::relativeIndirect: case Kind::relativeDirect: { const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->typesOffset; int32_t relativeOffsetFromField = *(int32_t*)fieldPos; VMOffset relativeOffsetFromMethod((uint64_t)offsetof(relative_method_t, typesOffset) + relativeOffsetFromField); VMAddress methodVMAddr = this->methodPos.vmAddress(); VMAddress typeVMAddr = methodVMAddr + relativeOffsetFromMethod; return typeVMAddr; } case Kind::pointer: { ResolvedValue typesRefValue = this->getTypesField(objcVisitor); return objcVisitor.resolveRebase(typesRefValue).vmAddress(); } } } std::optional<VMAddress> Method::getIMPVMAddr(const Visitor& objcVisitor) const { switch ( this->kind ) { case Kind::relativeIndirect: case Kind::relativeDirect: { const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->impOffset; int32_t relativeOffsetFromField = *(int32_t*)fieldPos; // protocols have null impls if ( relativeOffsetFromField == 0 ) return std::nullopt; VMOffset relativeOffsetFromMethod((uint64_t)offsetof(relative_method_t, impOffset) + relativeOffsetFromField); VMAddress methodVMAddr = this->methodPos.vmAddress(); VMAddress impVMAddr = methodVMAddr + relativeOffsetFromMethod; return impVMAddr; } case Kind::pointer: { ResolvedValue impRefValue = this->getIMPField(objcVisitor); return objcVisitor.resolveOptionalRebaseToVMAddress(impRefValue); } } } // Get the selector string name. A method often indirects via a selector reference. This returns // the vmAddr of the selector reference, not the final selector string VMAddress Method::getNameSelRefVMAddr(const Visitor& objcVisitor) const { switch ( this->kind ) { case Kind::relativeIndirect: { // The uint32_t name field is an offset from itself to a selref. The selref then points to the selector string const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->nameOffset; int32_t relativeOffsetFromField = *(int32_t*)fieldPos; VMOffset relativeOffsetFromMethod((uint64_t)offsetof(relative_method_t, nameOffset) + relativeOffsetFromField); VMAddress methodVMAddr = this->methodPos.vmAddress(); VMAddress nameSelRefVMAddr = methodVMAddr + relativeOffsetFromMethod; ResolvedValue nameSelRefValue = objcVisitor.getValueFor(nameSelRefVMAddr); return nameSelRefValue.vmAddress(); } case Kind::relativeDirect: { assert(0); } case Kind::pointer: { assert(0); } } } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS void Method::setName(const Visitor& objcVisitor, VMAddress nameVMAddr) { switch ( this->kind ) { case Kind::relativeIndirect: { assert(0); } case Kind::relativeDirect: { const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->nameOffset; VMOffset nameOffsetInBuffer = nameVMAddr - objcVisitor.sharedCacheSelectorStringsBaseAddress(); uint64_t relativeOffset = (uint64_t)nameOffsetInBuffer.rawValue(); assert((uint32_t)relativeOffset == relativeOffset); *(uint32_t*)fieldPos = (uint32_t)relativeOffset; break; } case Kind::pointer: { ResolvedValue selRefValue = this->getNameField(objcVisitor); objcVisitor.updateTargetVMAddress(selRefValue, CacheVMAddress(nameVMAddr.rawValue())); } } } void Method::setTypes(const Visitor& objcVisitor, VMAddress typesVMAddr) { switch ( this->kind ) { case Kind::relativeIndirect: case Kind::relativeDirect: { VMAddress methodVMAddr = this->methodPos.vmAddress(); VMAddress typesFieldVMAddr = methodVMAddr + VMOffset((uint64_t)offsetof(relative_method_t, typesOffset)); VMOffset typesRelativeOffset = typesVMAddr - typesFieldVMAddr; int64_t relativeOffset = (int64_t)typesRelativeOffset.rawValue(); const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->typesOffset; assert((int32_t)relativeOffset == relativeOffset); *(int32_t*)fieldPos = (int32_t)relativeOffset; break; } case Kind::pointer: { ResolvedValue refValue = this->getTypesField(objcVisitor); objcVisitor.updateTargetVMAddress(refValue, CacheVMAddress(typesVMAddr.rawValue())); } } } void Method::setIMP(const Visitor& objcVisitor, std::optional<VMAddress> impVMAddr) { switch ( this->kind ) { case Kind::relativeIndirect: case Kind::relativeDirect: { if ( !impVMAddr.has_value() ) { // A NULL imp is probably a protocol, and is expected. Every other IMP in the // protocol is also going to be NULL, so just make sure this one matches assert(!this->getIMPVMAddr(objcVisitor).has_value()); } else { VMAddress methodVMAddr = this->methodPos.vmAddress(); VMAddress impFieldVMAddr = methodVMAddr + VMOffset((uint64_t)offsetof(relative_method_t, impOffset)); VMOffset impRelativeOffset = impVMAddr.value() - impFieldVMAddr; int64_t relativeOffset = (int64_t)impRelativeOffset.rawValue(); const uint8_t* fieldPos = (const uint8_t*)&((const relative_method_t*)this->methodPos.value())->impOffset; assert((int32_t)relativeOffset == relativeOffset); *(int32_t*)fieldPos = (int32_t)relativeOffset; } break; } case Kind::pointer: { if ( !impVMAddr.has_value() ) { // A NULL imp is probably a protocol, and is expected. Every other IMP in the // protocol is also going to be NULL, so just make sure this one matches assert(!this->getIMPVMAddr(objcVisitor).has_value()); } else { ResolvedValue refValue = this->getIMPField(objcVisitor); objcVisitor.updateTargetVMAddress(refValue, CacheVMAddress(impVMAddr->rawValue())); } } } } #endif void Method::convertNameToOffset(const Visitor& objcVisitor, uint32_t nameOffset) { switch ( this->kind ) { case Kind::relativeIndirect: { // We are always looking at an indirect method when converting a name to an offset uint8_t* fieldPos = (uint8_t*)&((const relative_method_t*)this->methodPos.value())->nameOffset; *(uint32_t*)fieldPos = (uint32_t)nameOffset; // FIXME: Should we convert the kind field on this method to relativeDirect? break; } case Kind::relativeDirect: { // This shouldn't happen assert(0); } case Kind::pointer: { // This shouldn't happen assert(0); } } } uint32_t Method::getSize(bool is64) { return is64 ? sizeof(method64_t) : sizeof(method32_t); } // // MARK: --- IVarList methods --- // uint32_t IVarList::numIVars() const { if ( !ivarListPos.has_value() ) return 0; const ResolvedValue& ivarListValue = this->ivarListPos.value(); const ivar_list_t* ivarList = (const ivar_list_t*)ivarListValue.value(); assert(ivarList != nullptr); return ivarList->getCount(); } IVar IVarList::getIVar(const Visitor& objcVisitor, uint32_t i) const { assert(ivarListPos.has_value()); const ResolvedValue& ivarListValue = this->ivarListPos.value(); const ivar_list_t* ivarList = (const ivar_list_t*)ivarListValue.value(); assert(ivarList != nullptr); const uint8_t* ivarListBase = ivarList->ivarBase(); const uint8_t* ivar = ivarListBase + (i * ivarList->getElementSize()); ResolvedValue ivarValue = objcVisitor.getField(ivarListValue, ivar); return IVar(ivarValue); } #if BUILDING_CACHE_BUILDER_UNIT_TESTS const void* IVarList::getLocation() const { if ( !this->ivarListPos.has_value() ) return nullptr; return this->ivarListPos->value(); } std::optional<VMAddress> IVarList::getVMAddress() const { return this->ivarListPos->vmAddress(); } #endif // // MARK: --- PropertyList methods --- // uint32_t PropertyList::numProperties() const { if ( !this->propertyListPos.has_value() ) return 0; const ResolvedValue& propertyListValue = this->propertyListPos.value(); const property_list_t* propertyList = (const property_list_t*)propertyListValue.value(); assert(propertyList != nullptr); return propertyList->getCount(); } Property PropertyList::getProperty(const Visitor& objcVisitor, uint32_t i) const { assert(this->propertyListPos.has_value()); const ResolvedValue& propertyListValue = this->propertyListPos.value(); const property_list_t* propertyList = (const property_list_t*)propertyListValue.value(); assert(propertyList != nullptr); const uint8_t* propertyListBase = propertyList->propertyBase(); const uint8_t* property = propertyListBase + (i * propertyList->getElementSize()); ResolvedValue propertyValue = objcVisitor.getField(propertyListValue, property); return Property(propertyValue); } const void* PropertyList::getLocation() const { if ( !this->propertyListPos.has_value() ) return nullptr; return this->propertyListPos->value(); } std::optional<VMAddress> PropertyList::getVMAddress() const { if ( !this->propertyListPos.has_value() ) return { }; return this->propertyListPos->vmAddress(); } bool PropertyList::isListOfLists() const { if ( !propertyListPos.has_value() ) return false; const ResolvedValue& propertyListValue = this->propertyListPos.value(); return propertyListValue.vmAddress().rawValue() & 1; } // // MARK: --- ProtocolList methods --- // uint64_t ProtocolList::numProtocols(const Visitor& objcVisitor) const { if ( !this->protocolListPos.has_value() ) return 0; const ResolvedValue& protocolListValue = this->protocolListPos.value(); const void* protocolList = protocolListValue.value(); assert(protocolList != nullptr); if ( objcVisitor.pointerSize == 4 ) { return ((const protocol_list32_t*)protocolList)->count; } else { return ((const protocol_list64_t*)protocolList)->count; } } ResolvedValue ProtocolList::getProtocolField(const Visitor& objcVisitor, uint64_t i) const { assert(this->protocolListPos.has_value()); assert(i < this->numProtocols(objcVisitor)); const ResolvedValue& protocolListValue = this->protocolListPos.value(); const void* protocolList = protocolListValue.value(); assert(protocolList != nullptr); const void* protocolFixupLoc = nullptr; if ( objcVisitor.pointerSize == 4 ) { protocolFixupLoc = &((const protocol_list32_t*)protocolList)->list[i]; } else { protocolFixupLoc = &((const protocol_list64_t*)protocolList)->list[i]; } return objcVisitor.getField(protocolListValue, protocolFixupLoc); } Protocol ProtocolList::getProtocol(const Visitor& objcVisitor, uint64_t i) const { ResolvedValue field = this->getProtocolField(objcVisitor, i); ResolvedValue protocolValue = objcVisitor.resolveRebase(field); return Protocol(protocolValue); } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS void ProtocolList::setProtocol(const Visitor& objcVisitor, uint64_t i, VMAddress vmAddr) { ResolvedValue field = this->getProtocolField(objcVisitor, i); objcVisitor.updateTargetVMAddress(field, CacheVMAddress(vmAddr.rawValue())); } #endif const void* ProtocolList::getLocation() const { if ( !this->protocolListPos.has_value() ) return nullptr; return this->protocolListPos->value(); } std::optional<VMAddress> ProtocolList::getVMAddress() const { if ( !this->protocolListPos.has_value() ) return { }; return this->protocolListPos->vmAddress(); } bool ProtocolList::isListOfLists() const { if ( !protocolListPos.has_value() ) return false; const ResolvedValue& protocolListValue = this->protocolListPos.value(); return protocolListValue.vmAddress().rawValue() & 1; } void ProtocolList::dump(const Visitor& objcVisitor) const { if ( !this->protocolListPos.has_value() ) { fprintf(stdout, "no value\n"); return; } const ResolvedValue& protocolListValue = this->protocolListPos.value(); uint64_t count = this->numProtocols(objcVisitor); fprintf(stdout, "Protocol list (count %lld): vmAddr 0x%llx at %p\n", count, protocolListValue.vmAddress().rawValue(), protocolListValue.value()); for ( uint64_t i = 0; i != count; ++i ) { Protocol objCProtocol = this->getProtocol(objcVisitor, i); fprintf(stdout, " Protocol[%lld]: vmAddr 0x%llx at %p\n", i, objCProtocol.getVMAddress().rawValue(), objCProtocol.getLocation()); } } // // MARK: --- IVar methods --- // const void* IVar::getFieldPos(const Visitor& objcVisitor, Field field) const { if ( objcVisitor.pointerSize == 4 ) { const ivar32_t* ivar32 = (const ivar32_t*)this->ivarPos.value(); switch ( field ) { case Field::offset: return &ivar32->offsetVMAddr; case Field::name: return &ivar32->nameVMAddr; case Field::type: return &ivar32->typeVMAddr; case Field::alignment: return &ivar32->alignment; case Field::size: return &ivar32->size; } } else { const ivar64_t* ivar64 = (const ivar64_t*)this->ivarPos.value(); switch ( field ) { case Field::offset: return &ivar64->offsetVMAddr; case Field::name: return &ivar64->nameVMAddr; case Field::type: return &ivar64->typeVMAddr; case Field::alignment: return &ivar64->alignment; case Field::size: return &ivar64->size; } } } std::optional<uint32_t> IVar::getOffset(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->ivarPos, this->getFieldPos(objcVisitor, Field::offset)); // The offset might not be set, if it points to 0 std::optional<ResolvedValue> targetValue = objcVisitor.resolveOptionalRebase(field); if ( targetValue.has_value() ) return *(uint32_t*)targetValue->value(); return std::nullopt; } void IVar::setOffset(const Visitor& objcVisitor, uint32_t offset) const { ResolvedValue field = objcVisitor.getField(this->ivarPos, this->getFieldPos(objcVisitor, Field::offset)); ResolvedValue targetValue = objcVisitor.resolveRebase(field); *(uint32_t*)targetValue.value() = offset; } const char* IVar::getName(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->ivarPos, this->getFieldPos(objcVisitor, Field::name)); return (const char*)objcVisitor.resolveRebase(field).value(); } uint32_t IVar::getAlignment(const Visitor& objcVisitor) const { return *(uint32_t*)this->getFieldPos(objcVisitor, Field::alignment); } bool IVar::elided(const Visitor& objcVisitor) const { uint32_t size = *(uint32_t*)this->getFieldPos(objcVisitor, Field::size); // swift can optimize away ivars. It leaves the meta data about them, but they have no ivar offset to update return (size == 0); } // // MARK: --- Property methods --- // const void* Property::getFieldPos(const Visitor& objcVisitor, Field field) const { if ( objcVisitor.pointerSize == 4 ) { const property32_t* property32 = (const property32_t*)this->propertyPos.value(); switch ( field ) { case Field::name: return &property32->nameVMAddr; case Field::attributes: return &property32->attributesVMAddr; } } else { const property64_t* property64 = (const property64_t*)this->propertyPos.value(); switch ( field ) { case Field::name: return &property64->nameVMAddr; case Field::attributes: return &property64->attributesVMAddr; } } } const char* Property::getName(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->propertyPos, this->getFieldPos(objcVisitor, Field::name)); return (const char*)objcVisitor.resolveRebase(field).value(); } const char* Property::getAttributes(const Visitor& objcVisitor) const { ResolvedValue field = objcVisitor.getField(this->propertyPos, this->getFieldPos(objcVisitor, Field::attributes)); return (const char*)objcVisitor.resolveRebase(field).value(); } // // MARK: --- Visitor::Section methods --- // std::optional<Visitor::Section> Visitor::findSection(std::span<const char*> altSegNames, const char *sectionName) const { #if SUPPORT_VM_LAYOUT const dyld3::MachOFile* mf = this->dylibMA; #else const dyld3::MachOFile* mf = this->dylibMF; #endif __block std::optional<Visitor::Section> objcDataSection; ((const Header*)mf)->forEachSection(^(const Header::SegmentInfo& segInfo, const Header::SectionInfo& sectInfo, bool& stop) { bool segMatch = std::any_of(altSegNames.begin(), altSegNames.end(), [§Info](const char* segName) { return sectInfo.segmentName == segName; }); if ( !segMatch ) return; if ( sectInfo.sectionName != sectionName ) return; #if SUPPORT_VM_LAYOUT const void* targetValue = (const void*)(sectInfo.address + this->dylibMA->getSlide()); ResolvedValue target(targetValue, VMAddress(sectInfo.address)); #else VMOffset offsetInSegment(sectInfo.address - segInfo.vmaddr); ResolvedValue target(this->segments[sectInfo.segIndex], offsetInSegment); #endif objcDataSection.emplace(std::move(target), sectInfo.size); stop = true; }); return objcDataSection; } std::optional<Visitor::Section> Visitor::findObjCDataSection(const char *sectionName) const { static const char* objcDataSegments[] = { "__DATA", "__DATA_CONST", "__DATA_DIRTY" }; return findSection(objcDataSegments, sectionName); } std::optional<Visitor::Section> Visitor::findObjCTextSection(const char *sectionName) const { static const char* objcTextSegments[] = { "__TEXT" }; return findSection(objcTextSegments, sectionName); } // // MARK: --- Visitor methods --- // void Visitor::forEachClass(bool visitMetaClasses, const Visitor::Section& classListSection, void (^callback)(Class& objcClass, bool isMetaClass, bool& stopClass)) { assert((classListSection.sectSize % pointerSize) == 0); uint64_t numClasses = classListSection.sectSize / pointerSize; // Use the segment index to find the corresponding cache segment const ResolvedValue& sectionValue = classListSection.sectionBase; const uint8_t* sectionBase = (const uint8_t*)sectionValue.value(); for ( uint64_t classIndex = 0; classIndex != numClasses; ++classIndex ) { const uint8_t* classRefPos = sectionBase + (classIndex * pointerSize); ResolvedValue classRefValue = this->getField(sectionValue, classRefPos); bool isPatchableClass = false; ResolvedValue classPos = this->resolveBindOrRebase(classRefValue, isPatchableClass); Class objcClass(classPos, false, isPatchableClass); bool stopClass = false; callback(objcClass, false, stopClass); if ( stopClass ) return; // If we don't want the metaclass then skip to the next class if ( !visitMetaClasses) continue; bool isPatchableMetaClass = false; ResolvedValue objcClassISA = objcClass.getISA(*this, isPatchableMetaClass); Class objcMetaClass(objcClassISA, true, isPatchableMetaClass); callback(objcMetaClass, true, stopClass); if ( stopClass ) return; } } void Visitor::forEachClass(bool visitMetaClasses, void (^callback)(Class& objcClass, bool isMetaClass, bool& stopClass)) { std::optional<Section> classListSection = this->findObjCDataSection("__objc_classlist"); if ( !classListSection.has_value() ) return; this->forEachClass(visitMetaClasses, classListSection.value(), callback); } void Visitor::forEachClass(void (^callback)(const Class& objcClass, bool& stopClass)) { auto adaptor = ^(Class& objcClass, bool isMetaClass, bool& stopClass) { callback(objcClass, stopClass); }; forEachClass(false, adaptor); } void Visitor::forEachClass(void (^callback)(Class& objcClass, bool& stopClass)) { auto adaptor = ^(Class& objcClass, bool isMetaClass, bool& stopClass) { callback(objcClass, stopClass); }; forEachClass(false, adaptor); } void Visitor::forEachClassAndMetaClass(void (^callback)(const Class& objcClass, bool& stopClass)) { auto adaptor = ^(Class& objcClass, bool isMetaClass, bool& stopClass) { callback(objcClass, stopClass); }; forEachClass(true, adaptor); } void Visitor::forEachClassAndMetaClass(void (^callback)(Class& objcClass, bool& stopClass)) { auto adaptor = ^(Class& objcClass, bool isMetaClass, bool& stopClass) { callback(objcClass, stopClass); }; forEachClass(true, adaptor); } void Visitor::forEachCategory(void (^callback)(const Category& objcCategory, bool& stopCategory)) { for ( bool isCatlist2 : { false, true }) { const char* listSection = isCatlist2 ? "__objc_catlist2" : "__objc_catlist"; std::optional<Section> categoryListSection = findObjCDataSection(listSection); if ( !categoryListSection.has_value() ) continue; assert((categoryListSection->sectSize % pointerSize) == 0); uint64_t numCategories = categoryListSection->sectSize / pointerSize; const ResolvedValue& sectionValue = categoryListSection->sectionBase; const uint8_t* sectionBase = (const uint8_t*)sectionValue.value(); for ( uint64_t categoryIndex = 0; categoryIndex != numCategories; ++categoryIndex ) { const uint8_t* categoryRefPos = sectionBase + (categoryIndex * pointerSize); ResolvedValue categoryRefValue = this->getField(sectionValue, categoryRefPos); // Follow the category reference to get to the actual category ResolvedValue categoryPos = resolveRebase(categoryRefValue); Category objcCategory(categoryPos, isCatlist2); bool stopCategory = false; callback(objcCategory, stopCategory); if ( stopCategory ) break; } } } void Visitor::forEachProtocol(void (^callback)(const Protocol& objcProtocol, bool& stopProtocol)) { std::optional<Section> protocolListSection = findObjCDataSection("__objc_protolist"); if ( !protocolListSection.has_value() ) return; assert((protocolListSection->sectSize % pointerSize) == 0); uint64_t numCategories = protocolListSection->sectSize / pointerSize; const ResolvedValue& sectionValue = protocolListSection->sectionBase; const uint8_t* sectionBase = (const uint8_t*)sectionValue.value(); for ( uint64_t protocolIndex = 0; protocolIndex != numCategories; ++protocolIndex ) { const uint8_t* protocolRefPos = sectionBase + (protocolIndex * pointerSize); ResolvedValue protocolRefValue = this->getField(sectionValue, protocolRefPos); // Follow the protocol reference to get to the actual protocol ResolvedValue protocolPos = resolveRebase(protocolRefValue); Protocol objcProtocol(protocolPos); bool stopProtocol = false; callback(objcProtocol, stopProtocol); if ( stopProtocol ) break; } } void Visitor::forEachSelectorReference(void (^callback)(ResolvedValue& value)) const { std::optional<Section> selRefsSection = findObjCDataSection("__objc_selrefs"); if ( !selRefsSection.has_value() ) return; assert((selRefsSection->sectSize % pointerSize) == 0); uint64_t numSelRefs = selRefsSection->sectSize / pointerSize; const ResolvedValue& sectionValue = selRefsSection->sectionBase; const uint8_t* sectionBase = (const uint8_t*)sectionValue.value(); for ( uint64_t selRefIndex = 0; selRefIndex != numSelRefs; ++selRefIndex ) { const uint8_t* selRefPos = sectionBase + (selRefIndex * pointerSize); ResolvedValue selRefValue = this->getField(sectionValue, selRefPos); callback(selRefValue); } } void Visitor::forEachSelectorReference(void (^callback)(VMAddress selRefVMAddr, VMAddress selRefTargetVMAddr, const char* selectorString)) const { this->forEachSelectorReference(^(ResolvedValue& selRefValue) { ResolvedValue selRefTarget = this->resolveRebase(selRefValue); VMAddress selRefVMAddr = selRefValue.vmAddress(); VMAddress selRefTargetVMAddr = selRefTarget.vmAddress(); const char* selectorString = (const char*)selRefTarget.value(); callback(selRefVMAddr, selRefTargetVMAddr, selectorString); }); } void Visitor::forEachProtocolReference(void (^callback)(ResolvedValue& value)) { std::optional<Section> protocolRefsSection = findObjCDataSection("__objc_protorefs"); if ( !protocolRefsSection.has_value() ) return; assert((protocolRefsSection->sectSize % pointerSize) == 0); uint64_t numProtocolRefs = protocolRefsSection->sectSize / pointerSize; const ResolvedValue& sectionValue = protocolRefsSection->sectionBase; const uint8_t* sectionBase = (const uint8_t*)sectionValue.value(); for ( uint64_t protocolRefIndex = 0; protocolRefIndex != numProtocolRefs; ++protocolRefIndex ) { const uint8_t* protocolRefPos = sectionBase + (protocolRefIndex * pointerSize); ResolvedValue protocolRefValue = this->getField(sectionValue, protocolRefPos); callback(protocolRefValue); } } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS void Visitor::forEachMethodList(void (^callback)(MethodList& objcMethodList, std::optional<metadata_visitor::ResolvedValue> extendedMethodTypes)) { __block std::unordered_set<const void*> visitedLists; forEachClassAndMetaClass(^(const objc_visitor::Class& objcClass, bool&) { objc_visitor::MethodList objcMethodList = objcClass.getBaseMethods(*this); callback(objcMethodList, std::nullopt); visitedLists.insert(objcMethodList.getLocation()); }); forEachCategory(^(const objc_visitor::Category& objcCategory, bool&) { objc_visitor::MethodList instanceMethodList = objcCategory.getInstanceMethods(*this); objc_visitor::MethodList classMethodList = objcCategory.getClassMethods(*this); callback(instanceMethodList, std::nullopt); visitedLists.insert(instanceMethodList.getLocation()); callback(classMethodList, std::nullopt); visitedLists.insert(classMethodList.getLocation()); }); forEachProtocol(^(const objc_visitor::Protocol& objcProtocol, bool&) { objc_visitor::MethodList instanceMethodList = objcProtocol.getInstanceMethods(*this); objc_visitor::MethodList classMethodList = objcProtocol.getClassMethods(*this); objc_visitor::MethodList optionalInstanceMethodList = objcProtocol.getOptionalInstanceMethods(*this); objc_visitor::MethodList optionalClassMethodList = objcProtocol.getOptionalClassMethods(*this); // This is an optional flat array with entries for all method lists. // Each method list of length N has N char* entries in this list, if its present std::optional<metadata_visitor::ResolvedValue> extendedMethodTypes = objcProtocol.getExtendedMethodTypes(*this); const uint8_t* currentMethodTypes = extendedMethodTypes.has_value() ? (const uint8_t*)extendedMethodTypes->value() : nullptr; callback(instanceMethodList, extendedMethodTypes); visitedLists.insert(instanceMethodList.getLocation()); if ( extendedMethodTypes.has_value() ) { currentMethodTypes += (instanceMethodList.numMethods() * pointerSize); extendedMethodTypes.emplace(metadata_visitor::ResolvedValue(extendedMethodTypes.value(), currentMethodTypes)); } callback(classMethodList, extendedMethodTypes); visitedLists.insert(classMethodList.getLocation()); if ( extendedMethodTypes.has_value() ) { currentMethodTypes += (classMethodList.numMethods() * pointerSize); extendedMethodTypes.emplace(metadata_visitor::ResolvedValue(extendedMethodTypes.value(), currentMethodTypes)); } callback(optionalInstanceMethodList, extendedMethodTypes); visitedLists.insert(optionalInstanceMethodList.getLocation()); if ( extendedMethodTypes.has_value() ) { currentMethodTypes += (optionalInstanceMethodList.numMethods() * pointerSize); extendedMethodTypes.emplace(metadata_visitor::ResolvedValue(extendedMethodTypes.value(), currentMethodTypes)); } callback(optionalClassMethodList, extendedMethodTypes); visitedLists.insert(optionalClassMethodList.getLocation()); if ( extendedMethodTypes.has_value() ) { currentMethodTypes += (optionalClassMethodList.numMethods() * pointerSize); extendedMethodTypes.emplace(metadata_visitor::ResolvedValue(extendedMethodTypes.value(), currentMethodTypes)); } }); // rdar://129304028 (dyld cache builder support for relative method lists in Swift generic classes) // Also scan the entire __objc_methlist section looking for other method lists that // aren't referenced through the regular ObjC metadata. std::optional<Section> methodListSection = findObjCTextSection("__objc_methlist"); if ( !methodListSection.has_value() ) return; assert((methodListSection->sectSize % 4) == 0); const ResolvedValue& sectionValue = methodListSection->sectionBase; const uint8_t* sectionPos = (const uint8_t*)sectionValue.value(); const uint8_t* sectionEnd = (const uint8_t*)sectionValue.value() + methodListSection->sectSize; while ( sectionPos < sectionEnd ) { ResolvedValue methodListValue = this->getField(sectionValue, sectionPos); // method lists are 8-byte alligned, a valid method list can never start // with a 0 because that's where the method size entry and flags are encoded if ( *(uint32_t*)methodListValue.value() == 0 ) { sectionPos += sizeof(uint32_t); continue; } MethodList methodList(methodListValue); // sanity check entry - all lists in __objc_methlist are relative and // a relative method list entry is 12 bytes large assert(methodList.usesRelativeOffsets() && methodList.methodSize() == 12 && "not a relative method list"); // skip method lists that were visited through classes etc. if ( !visitedLists.contains(methodList.getLocation()) ) { callback(methodList, std::nullopt); } uint32_t size = methodList.listSize(); assert(size != 0 && "method list can't be empty"); sectionPos += size; } assert(sectionPos == sectionEnd && "malformed __objc_methlist section"); } #endif void Visitor::withImageInfo(void (^callback)(const uint32_t version, const uint32_t flags)) const { std::optional<Section> imageInfoSection = findObjCDataSection("__objc_imageinfo"); if ( !imageInfoSection.has_value() ) return; assert((imageInfoSection->sectSize % pointerSize) == 0); const ResolvedValue& sectionValue = imageInfoSection->sectionBase; struct objc_image_info { int32_t version; uint32_t flags; }; const objc_image_info* sectionBase = (const objc_image_info*)sectionValue.value(); callback(sectionBase->version, sectionBase->flags); } #endif // !TARGET_OS_EXCLAVEKIT