Loading...
cache_builder/SubCache.cpp dyld-1340 dyld-1286.10
--- dyld/dyld-1340/cache_builder/SubCache.cpp
+++ dyld/dyld-1286.10/cache_builder/SubCache.cpp
@@ -38,6 +38,7 @@
 #include <CommonCrypto/CommonDigest.h>
 #include <CommonCrypto/CommonDigestSPI.h>
 
+using dyld3::GradedArchs;
 using dyld3::MachOFile;
 
 using mach_o::Header;
@@ -426,8 +427,8 @@
     const char* readonlySuffix = forceDevelopmentSubCacheSuffix ? ".development.dyldreadonly" : ".dyldreadonly";
     const char* subCacheSuffix = forceDevelopmentSubCacheSuffix ? ".development" : "";
 
-    if ( (platform == Platform::macOS) || platform.isSimulator() ) {
-        // macOS/sims never has a .development suffix
+    if ( platform == Platform::macOS ) {
+        // macOS never has a .development suffix
         this->fileSuffix = "." + json::decimal(subCacheIndex);
     } else if ( platform == Platform::driverKit ) {
         // driverKit never has a .development suffix
@@ -455,7 +456,7 @@
 static std::string getCodeSigningIdentifier(const BuilderOptions& options)
 {
     std::string cacheIdentifier = "com.apple.dyld.cache.";
-    cacheIdentifier += options.arch.name();
+    cacheIdentifier += options.archs.name();
     if ( options.dylibsRemovedFromDisk ) {
         switch ( options.kind ) {
             case CacheKind::development:
@@ -743,14 +744,6 @@
     // Note: cdHash is defined as first 20 bytes of hash
     memcpy(this->cdHash, fullCdHash, 20);
 
-    if ( layout.agile ) {
-        // hash of entire code directory (cdHash) uses same hash as each page
-        uint8_t altfullCdHash[CS_HASH_SIZE_SHA256];
-        CCDigest(kCCDigestSHA256, (const uint8_t*)cd256, layout.cd256Size, altfullCdHash);
-        // Note: cdHash is defined as first 20 bytes of hash
-        memcpy(this->agilecdHash, altfullCdHash, 20);
-    }
-
     // Set the UUID string in the subcache
     uuid_unparse_upper(dyldCacheHeader->uuid, this->uuidString);
 }
@@ -969,8 +962,7 @@
     return count;
 }
 
-void SubCache::addCacheHeaderChunk(const BuilderOptions& options, const BuilderConfig& config,
-                                   const std::span<CacheDylib> cacheDylibs)
+void SubCache::addCacheHeaderChunk(const BuilderConfig& config, const std::span<CacheDylib> cacheDylibs)
 {
     // calculate size of header info and where first dylib's mach_header should start
     uint64_t numMappings = this->regions.size();
@@ -983,7 +975,7 @@
         startOffset += sizeof(dyld_cache_tpro_mapping_info) * numTPRORegions(config, this, this->subCaches);
     }
 
-    if ( this->needsCacheHeaderImageList(options) ) {
+    if ( this->needsCacheHeaderImageList() ) {
         startOffset += sizeof(dyld_cache_image_info) * cacheDylibs.size();
         startOffset += sizeof(dyld_cache_image_text_info) * cacheDylibs.size();
         for ( const CacheDylib& cacheDylib : cacheDylibs ) {
@@ -1225,11 +1217,12 @@
 
 void SubCache::addPatchTableChunk(PatchTableOptimizer& patchTableOptimizer)
 {
-    // We can't compute the size yet so just make an empty chunk
+    // We can't compute the size yet.  We need to know how many fixups we have
+    // And yet we have an estimate, so we'll use it
 
     this->patchTable = std::make_unique<PatchTableChunk>();
-    this->patchTable->cacheVMSize       = CacheVMSize(0ULL);
-    this->patchTable->subCacheFileSize  = CacheFileSize(0ULL);
+    this->patchTable->cacheVMSize       = CacheVMSize(patchTableOptimizer.patchTableTotalByteSize);
+    this->patchTable->subCacheFileSize  = CacheFileSize(patchTableOptimizer.patchTableTotalByteSize);
 
     patchTableOptimizer.patchTableChunk = this->patchTable.get();
 
@@ -1549,16 +1542,15 @@
 }
 
 void SubCache::writeCacheHeader(const BuilderOptions& options, const BuilderConfig& config,
-                                const std::span<CacheDylib> cacheDylibs,
-                                uint32_t osVersion, uint32_t altPlatform, uint32_t altOsVersion)
+                                const std::span<CacheDylib> cacheDylibs)
 {
     Chunk& cacheHeaderChunk = *this->cacheHeader.get();
     dyld_cache_header* dyldCacheHeader = (dyld_cache_header*)cacheHeaderChunk.subCacheBuffer;
 
     // "dyld_v1" + spaces + archName(), with enough spaces to pad to 15 bytes
     std::string magic = "dyld_v1";
-    magic.append(15 - magic.length() - strlen(options.arch.name()), ' ');
-    magic.append(options.arch.name());
+    magic.append(15 - magic.length() - strlen(options.archs.name()), ' ');
+    magic.append(options.archs.name());
     assert(magic.length() == 15);
 
     // Num of mappings depends on cache layout.
@@ -1629,9 +1621,9 @@
     dyldCacheHeader->programsPBLSetPoolSize        = 0; // set later only on the main cache file
     dyldCacheHeader->programTrieAddr               = 0; // set later only on the main cache file
     dyldCacheHeader->programTrieSize               = 0; // set later only on the main cache file
-    dyldCacheHeader->osVersion                     = osVersion;
-    dyldCacheHeader->altPlatform                   = altPlatform;
-    dyldCacheHeader->altOsVersion                  = altOsVersion;
+    dyldCacheHeader->osVersion                     = 0; // set later only on the main cache file
+    dyldCacheHeader->altPlatform                   = 0; // set later only on the main cache file
+    dyldCacheHeader->altOsVersion                  = 0; // set later only on the main cache file
     dyldCacheHeader->swiftOptsOffset               = 0; // set later only on the main cache file
     dyldCacheHeader->swiftOptsSize                 = 0; // set later only on the main cache file
     dyldCacheHeader->subCacheArrayOffset           = 0;
@@ -1663,6 +1655,7 @@
 void SubCache::addMainCacheHeaderInfo(const BuilderOptions& options, const BuilderConfig& config,
                                       const std::span<CacheDylib> cacheDylibs,
                                       CacheVMSize totalVMSize, uint64_t maxSlide,
+                                      uint32_t osVersion, uint32_t altPlatform, uint32_t altOsVersion,
                                       CacheVMAddress dyldInCacheUnslidAddr,
                                       CacheVMAddress dyldInCacheEntryUnslidAddr,
                                       const DylibTrieOptimizer& dylibTrieOptimizer,
@@ -1706,6 +1699,10 @@
 
     dyldCacheHeader->dyldInCacheMH      = dyldInCacheUnslidAddr.rawValue();
     dyldCacheHeader->dyldInCacheEntry   = dyldInCacheEntryUnslidAddr.rawValue();
+
+    dyldCacheHeader->osVersion      = osVersion;
+    dyldCacheHeader->altPlatform    = altPlatform;
+    dyldCacheHeader->altOsVersion   = altOsVersion;
 
     // record max slide now that final size is established
     dyldCacheHeader->maxSlide           = maxSlide;
@@ -1786,7 +1783,7 @@
                                        const BuilderConfig& config,
                                        const std::span<CacheDylib> cacheDylibs)
 {
-    if ( !this->needsCacheHeaderImageList(options) )
+    if ( !this->needsCacheHeaderImageList() )
         return;
 
     Chunk&             cacheHeaderChunk   = *this->cacheHeader.get();
@@ -1920,17 +1917,16 @@
     return this->kind == Kind::stubsCustomer;
 }
 
-bool SubCache::needsCacheHeaderImageList(const BuilderOptions& options) const
+bool SubCache::needsCacheHeaderImageList() const
 {
     // Symbols and stubs files don't need an image list
+    // We'd like to not add the image list to subcaches, only the main cache, but Rosetta needs
+    // the image list on subCaches.
     switch ( this->kind ) {
         case Kind::mainDevelopment:
         case Kind::mainCustomer:
+        case Kind::subUniversal:
             return true;
-        case Kind::subUniversal:
-            // We'd like to not add the image list to subcaches, only the main cache, but Rosetta needs
-            // the image list on subCaches.
-            return options.arch.sameCpu(mach_o::Architecture::x86_64);
         case Kind::stubsDevelopment:
         case Kind::stubsCustomer:
         case Kind::symbols: