Loading...
cache_builder/SubCache.cpp dyld-1284.13 dyld-1245.1
--- dyld/dyld-1284.13/cache_builder/SubCache.cpp
+++ dyld/dyld-1245.1/cache_builder/SubCache.cpp
@@ -28,7 +28,6 @@
 #include "Chunk.h"
 #include "CodeSigningTypes.h"
 #include "DyldSharedCache.h"
-#include "Header.h"
 #include "SubCache.h"
 #include "JSONWriter.h"
 
@@ -40,9 +39,6 @@
 
 using dyld3::GradedArchs;
 using dyld3::MachOFile;
-
-using mach_o::Header;
-using mach_o::Platform;
 
 using error::Error;
 
@@ -417,7 +413,8 @@
     return false;
 }
 
-void SubCache::setSuffix(Platform platform, bool forceDevelopmentSubCacheSuffix, size_t subCacheIndex)
+void SubCache::setSuffix(dyld3::Platform platform, bool forceDevelopmentSubCacheSuffix,
+                         size_t subCacheIndex)
 {
     assert(this->isSubCache() || this->isStubsCache());
     assert(subCacheIndex > 0);
@@ -427,29 +424,29 @@
     const char* readonlySuffix = forceDevelopmentSubCacheSuffix ? ".development.dyldreadonly" : ".dyldreadonly";
     const char* subCacheSuffix = forceDevelopmentSubCacheSuffix ? ".development" : "";
 
-    if ( platform == Platform::macOS ) {
+    if ( platform == dyld3::Platform::macOS ) {
         // macOS never has a .development suffix
-        this->fileSuffix = "." + json::decimal(subCacheIndex);
-    } else if ( platform == Platform::driverKit ) {
+        this->fileSuffix = "." + dyld3::json::decimal(subCacheIndex);
+    } else if ( platform == dyld3::Platform::driverKit ) {
         // driverKit never has a .development suffix
-        this->fileSuffix = "." + json::decimal(subCacheIndex);
+        this->fileSuffix = "." + dyld3::json::decimal(subCacheIndex);
     } else if ( this->isStubsDevelopmentCache() ) {
         // Dev stubs always have a suffix
-        this->fileSuffix = "." + json::decimal(subCacheIndex) + ".development";
+        this->fileSuffix = "." + dyld3::json::decimal(subCacheIndex) + ".development";
     } else if ( this->isStubsCustomerCache() ) {
         // Customer stubs never have a suffix
-        this->fileSuffix = "." + json::decimal(subCacheIndex);
+        this->fileSuffix = "." + dyld3::json::decimal(subCacheIndex);
     } else if ( hasDataRegion(this->regions) ) {
         // Data only subcaches have their own suffix
-        this->fileSuffix = "." + json::decimal(subCacheIndex) + dataSuffix;
+        this->fileSuffix = "." + dyld3::json::decimal(subCacheIndex) + dataSuffix;
     } else if ( hasReadOnlyRegion(this->regions) ) {
         // read-only only subcaches have their own suffix
-        this->fileSuffix = "." + json::decimal(subCacheIndex) + readonlySuffix;
+        this->fileSuffix = "." + dyld3::json::decimal(subCacheIndex) + readonlySuffix;
     } else if ( hasLinkeditRegion(this->regions) ) {
         // Linkedit only subcaches have their own suffix
-        this->fileSuffix = "." + json::decimal(subCacheIndex) + linkeditSuffix;
+        this->fileSuffix = "." + dyld3::json::decimal(subCacheIndex) + linkeditSuffix;
     } else {
-        this->fileSuffix = "." + json::decimal(subCacheIndex) + subCacheSuffix;
+        this->fileSuffix = "." + dyld3::json::decimal(subCacheIndex) + subCacheSuffix;
     }
 }
 
@@ -846,7 +843,7 @@
                 break;
             case Chunk::Kind::dylibDataDirty:
                 // On arm64e, dataDirty goes in to auth
-                if ( cacheDylib.inputHdr->isArch("arm64e") )
+                if ( cacheDylib.inputMF->isArch("arm64e") )
                     this->addAuthChunk(&segmentInfo);
                 else
                     this->addDataChunk(&segmentInfo);
@@ -868,6 +865,8 @@
                 break;
         }
     }
+
+    this->addLinkeditFromDylib(cacheDylib);
 }
 
 // Linkedit is stored in Chunks in its own array on the dylib.  This adds it to the subCache.
@@ -1057,7 +1056,7 @@
     addCodeSignatureChunk(this->codeSignature.get());
 }
 
-void SubCache::addObjCOptsHeaderChunk(const BuilderConfig& config, ObjCOptimizer& objcOptimizer)
+void SubCache::addObjCOptsHeaderChunk(ObjCOptimizer& objcOptimizer)
 {
     this->objcOptsHeader = std::make_unique<ObjCOptsHeaderChunk>();
     this->objcOptsHeader->cacheVMSize       = CacheVMSize(objcOptimizer.optsHeaderByteSize);
@@ -1065,7 +1064,7 @@
 
     objcOptimizer.optsHeaderChunk = this->objcOptsHeader.get();
 
-    this->addReadOnlyChunk(config, this->objcOptsHeader.get());
+    this->addLinkeditChunk(this->objcOptsHeader.get());
 }
 
 void SubCache::addObjCHeaderInfoReadOnlyChunk(const BuilderConfig& config, ObjCOptimizer& objcOptimizer)
@@ -1180,7 +1179,7 @@
     addObjCReadWriteChunk(config, this->objcCanonicalProtocols.get());
 }
 
-void SubCache::addObjCIMPCachesChunk(const BuilderConfig& config, ObjCIMPCachesOptimizer& objcIMPCachesOptimizer)
+void SubCache::addObjCIMPCachesChunk(ObjCIMPCachesOptimizer& objcIMPCachesOptimizer)
 {
     this->objcIMPCaches = std::make_unique<ObjCIMPCachesChunk>();
     this->objcIMPCaches->cacheVMSize                        = CacheVMSize(objcIMPCachesOptimizer.impCachesTotalByteSize);
@@ -1188,7 +1187,7 @@
 
     objcIMPCachesOptimizer.impCachesChunk = this->objcIMPCaches.get();
 
-    this->addReadOnlyChunk(config, this->objcIMPCaches.get());
+    this->addLinkeditChunk(this->objcIMPCaches.get());
 }
 
 void SubCache::addObjCCategoriesChunk(const BuilderConfig& config,
@@ -1229,17 +1228,6 @@
     this->addLinkeditChunk(this->patchTable.get());
 }
 
-void SubCache::addFunctionVariantsChunk(FunctionVariantsOptimizer& optimizer)
-{
-    this->functionVariants                    = std::make_unique<FunctionVariantsPatchTableChunk>();
-    this->functionVariants->cacheVMSize       = CacheVMSize(optimizer.fvInfoTotalByteSize);
-    this->functionVariants->subCacheFileSize  = CacheFileSize(optimizer.fvInfoTotalByteSize);
-
-    optimizer.chunk = this->functionVariants.get();
-
-    this->addLinkeditChunk(this->functionVariants.get());
-}
-
 void SubCache::addCacheDylibsLoaderChunk(PrebuiltLoaderBuilder& builder)
 {
     // We can't compute the size yet.
@@ -1279,21 +1267,7 @@
     this->addLinkeditChunk(this->executablesTrie.get());
 }
 
-void SubCache::addPrewarmingDataChunk(const BuilderConfig& config, PrewarmingOptimizer& opt)
-{
-    if ( opt.prewarmingByteSize == 0 )
-        return;
-
-    this->prewarmingChunk = std::make_unique<PrewarmingChunk>(Chunk::Kind::prewarmingData);
-    this->prewarmingChunk->cacheVMSize      = CacheVMSize(opt.prewarmingByteSize);
-    this->prewarmingChunk->subCacheFileSize = CacheFileSize(opt.prewarmingByteSize);
-
-    opt.prewarmingChunk = this->prewarmingChunk.get();
-
-    this->addReadOnlyChunk(config, this->prewarmingChunk.get());
-}
-
-void SubCache::addSwiftOptsHeaderChunk(const BuilderConfig& config, SwiftOptimizer& opt)
+void SubCache::addSwiftOptsHeaderChunk(SwiftProtocolConformanceOptimizer& opt)
 {
     this->swiftOptsHeader = std::make_unique<SwiftOptsHeaderChunk>();
     this->swiftOptsHeader->cacheVMSize      = CacheVMSize(opt.optsHeaderByteSize);
@@ -1301,10 +1275,10 @@
 
     opt.optsHeaderChunk = this->swiftOptsHeader.get();
 
-    this->addReadOnlyChunk(config, this->swiftOptsHeader.get());
-}
-
-void SubCache::addSwiftTypeHashTableChunk(const BuilderConfig& config, SwiftOptimizer& opt)
+    this->addLinkeditChunk(this->swiftOptsHeader.get());
+}
+
+void SubCache::addSwiftTypeHashTableChunk(SwiftProtocolConformanceOptimizer& opt)
 {
     this->swiftTypeHashTable = std::make_unique<SwiftProtocolConformancesHashTableChunk>();
     this->swiftTypeHashTable->cacheVMSize       = CacheVMSize(opt.typeConformancesHashTableSize);
@@ -1312,10 +1286,10 @@
 
     opt.typeConformancesHashTable = this->swiftTypeHashTable.get();
 
-    this->addReadOnlyChunk(config, this->swiftTypeHashTable.get());
-}
-
-void SubCache::addSwiftMetadataHashTableChunk(const BuilderConfig& config, SwiftOptimizer& opt)
+    this->addLinkeditChunk(this->swiftTypeHashTable.get());
+}
+
+void SubCache::addSwiftMetadataHashTableChunk(SwiftProtocolConformanceOptimizer& opt)
 {
     this->swiftMetadataHashTable = std::make_unique<SwiftProtocolConformancesHashTableChunk>();
     this->swiftMetadataHashTable->cacheVMSize       = CacheVMSize(opt.metadataConformancesHashTableSize);
@@ -1323,10 +1297,10 @@
 
     opt.metadataConformancesHashTable = this->swiftMetadataHashTable.get();
 
-    this->addReadOnlyChunk(config, this->swiftMetadataHashTable.get());
-}
-
-void SubCache::addSwiftForeignHashTableChunk(const BuilderConfig& config, SwiftOptimizer& opt)
+    this->addLinkeditChunk(this->swiftMetadataHashTable.get());
+}
+
+void SubCache::addSwiftForeignHashTableChunk(SwiftProtocolConformanceOptimizer& opt)
 {
     this->swiftForeignTypeHashTable = std::make_unique<SwiftProtocolConformancesHashTableChunk>();
     this->swiftForeignTypeHashTable->cacheVMSize        = CacheVMSize(opt.foreignTypeConformancesHashTableSize);
@@ -1334,10 +1308,10 @@
 
     opt.foreignTypeConformancesHashTable = this->swiftForeignTypeHashTable.get();
 
-    this->addReadOnlyChunk(config, this->swiftForeignTypeHashTable.get());
-}
-
-void SubCache::addSwiftPrespecializedMetadataPointerTableChunks(const BuilderConfig& config, SwiftOptimizer& opt)
+    this->addLinkeditChunk(this->swiftForeignTypeHashTable.get());
+}
+
+void SubCache::addSwiftPrespecializedMetadataPointerTableChunks(SwiftProtocolConformanceOptimizer& opt)
 {
     for ( PointerHashTableOptimizerInfo& tableInfo : opt.prespecializedMetadataHashTables ) {
         PointerHashTableChunk* chunk = this->pointerHashTables.emplace_back(std::make_unique<PointerHashTableChunk>()).get();
@@ -1345,7 +1319,7 @@
         chunk->subCacheFileSize  = CacheFileSize(tableInfo.size);
 
         tableInfo.chunk = chunk;
-        this->addReadOnlyChunk(config, chunk);
+        this->addLinkeditChunk(chunk);
     }
 }
 
@@ -1596,14 +1570,12 @@
     dyldCacheHeader->progClosuresSize              = 0; // no longer used
     dyldCacheHeader->progClosuresTrieAddr          = 0; // no longer used
     dyldCacheHeader->progClosuresTrieSize          = 0; // no longer used
-    dyldCacheHeader->platform                      = options.platform.value();
+    dyldCacheHeader->platform                      = (uint8_t)options.platform;
     dyldCacheHeader->formatVersion                 = 0; //dyld3::closure::kFormatVersion;
     dyldCacheHeader->dylibsExpectedOnDisk          = !options.dylibsRemovedFromDisk;
     dyldCacheHeader->simulator                     = options.isSimulator();
     dyldCacheHeader->locallyBuiltCache             = options.isLocallyBuiltCache;
     dyldCacheHeader->builtFromChainedFixups        = false; // no longer used
-    dyldCacheHeader->newFormatTLVs                 = true;
-    dyldCacheHeader->padding                       = 0;
     dyldCacheHeader->sharedRegionStart             = this->subCacheVMAddress.rawValue();
     dyldCacheHeader->sharedRegionSize              = 0;
     dyldCacheHeader->maxSlide                      = 0; // overwritten later in build if the cache supports ASLR
@@ -1642,8 +1614,6 @@
     dyldCacheHeader->dynamicDataMaxSize            = 0; // set later only on the main cache file
     dyldCacheHeader->tproMappingsOffset            = 0; // set later only on the main cache file
     dyldCacheHeader->tproMappingsCount             = 0; // set later only on the main cache file
-    dyldCacheHeader->prewarmingDataOffset          = 0; // set later only on the main cache file
-    dyldCacheHeader->prewarmingDataSize            = 0; // set later only on the main cache file
 
     // Fill in old mappings
     // And new mappings which also have slide info
@@ -1660,11 +1630,9 @@
                                       CacheVMAddress dyldInCacheEntryUnslidAddr,
                                       const DylibTrieOptimizer& dylibTrieOptimizer,
                                       const ObjCOptimizer& objcOptimizer,
-                                      const SwiftOptimizer& swiftOpt,
+                                      const SwiftProtocolConformanceOptimizer& swiftProtocolConformanceOpt,
                                       const PatchTableOptimizer& patchTableOptimizer,
-                                      const FunctionVariantsOptimizer& functionVariantOptimizer,
-                                      const PrebuiltLoaderBuilder& prebuiltLoaderBuilder,
-                                      const PrewarmingOptimizer& prewarmingOptimizer)
+                                      const PrebuiltLoaderBuilder& prebuiltLoaderBuilder)
 {
     const CacheVMAddress cacheBaseAddress = config.layout.cacheBaseAddress;
 
@@ -1677,13 +1645,13 @@
     dyldCacheHeader->dylibsTrieAddr = dylibTrieOptimizer.dylibsTrieChunk->cacheVMAddress.rawValue();
     dyldCacheHeader->dylibsTrieSize = dylibTrieOptimizer.dylibsTrieChunk->subCacheFileSize.rawValue();
 
-    // Disable objc optimizations from EK shared cache
-    bool emitObjcOpts = !options.platform.isExclaveKit();
-    if ( !objcOptimizer.objcDylibs.empty() && emitObjcOpts ) {
+    if ( !objcOptimizer.objcDylibs.empty() ) {
         dyldCacheHeader->objcOptsOffset = (objcOptimizer.optsHeaderChunk->cacheVMAddress - cacheBaseAddress).rawValue();
         dyldCacheHeader->objcOptsSize   = objcOptimizer.optsHeaderChunk->subCacheFileSize.rawValue();
-
-        const auto& opt = swiftOpt;
+    }
+
+    if ( !objcOptimizer.objcDylibs.empty() ) {
+        const auto& opt = swiftProtocolConformanceOpt;
         dyldCacheHeader->swiftOptsOffset = (opt.optsHeaderChunk->cacheVMAddress - cacheBaseAddress).rawValue();
         dyldCacheHeader->swiftOptsSize   = opt.optsHeaderChunk->subCacheFileSize.rawValue();
     }
@@ -1710,9 +1678,6 @@
     // TODO: Build the atlas
     dyldCacheHeader->cacheAtlasOffset              = 0; // set later only on the main cache file
     dyldCacheHeader->cacheAtlasSize                = 0; // set later only on the main cache file
-
-    dyldCacheHeader->functionVariantInfoAddr = functionVariantOptimizer.chunk->cacheVMAddress.rawValue();
-    dyldCacheHeader->functionVariantInfoSize = functionVariantOptimizer.fvInfoTotalByteSize;
 
     // The main cache has offsets to all the caches
     if ( !this->subCaches.empty() ) {
@@ -1752,11 +1717,6 @@
             ++index;
         });
     }
-
-    if ( prewarmingOptimizer.prewarmingChunk != nullptr ) {
-        dyldCacheHeader->prewarmingDataOffset = (prewarmingOptimizer.prewarmingChunk->cacheVMAddress - cacheBaseAddress).rawValue();
-        dyldCacheHeader->prewarmingDataSize   = prewarmingOptimizer.prewarmingChunk->subCacheFileSize.rawValue();
-    }
 }
 
 void SubCache::addSymbolsCacheHeaderInfo(const UnmappedSymbolsOptimizer& optimizer)
@@ -1809,7 +1769,7 @@
 
     // write text image array and image names pool at same time
     for ( const CacheDylib& cacheDylib : cacheDylibs ) {
-        cacheDylib.inputHdr->getUuid(textImages->uuid);
+        cacheDylib.inputMF->getUuid(textImages->uuid);
         textImages->loadAddress     = cacheDylib.cacheLoadAddress.rawValue();
         textImages->textSegmentSize = (uint32_t)cacheDylib.segments.front().cacheVMSize.rawValue();
         textImages->pathOffset      = stringOffset;