Loading...
cache_builder/BuilderConfig.cpp dyld-1340 dyld-1241.17
--- dyld/dyld-1340/cache_builder/BuilderConfig.cpp
+++ dyld/dyld-1241.17/cache_builder/BuilderConfig.cpp
@@ -25,17 +25,13 @@
 #include "BuilderConfig.h"
 #include "BuilderOptions.h"
 #include "CodeSigningTypes.h"
-#include "Architecture.h"
-#include "Platform.h"
 
 #include "dyld_cache_config.h"
 
 #include <assert.h>
 
 using namespace cache_builder;
-
-using mach_o::Architecture;
-using mach_o::Platform;
+using dyld3::GradedArchs;
 
 //
 // MARK: --- cache_builder::Logger methods ---
@@ -44,11 +40,9 @@
 cache_builder::Logger::Logger(const BuilderOptions& options)
     : logPrefix(options.logPrefix)
 {
-    this->printTimers           = options.timePasses;
-    this->printStats            = options.stats;
-    this->printDebug            = options.debug;
-    this->printDebugIMPCaches   = options.debugIMPCaches;
-    this->printDebugCacheLayout = options.debugCacheLayout;
+    this->printTimers = options.timePasses;
+    this->printStats  = options.stats;
+    this->printDebug  = options.debug;
 }
 
 void cache_builder::Logger::log(const char* format, ...) const
@@ -68,46 +62,55 @@
 // MARK: --- cache_builder::Layout methods ---
 //
 
-static uint32_t defaultPageSize(Architecture arch)
-{
-    if ( arch.sameCpu(Architecture::x86_64) )
+static uint32_t defaultPageSize(std::string_view archName)
+{
+    if ( (archName == "x86_64") || (archName == "x86_64h") )
         return 4096;
     else
         return 16384;
 }
 
-static bool hasAuthRegion(Architecture arch)
-{
-    return arch == Architecture::arm64e;
-}
-
-static uint32_t supportsTPROMapping(Architecture arch)
-{
-    return !arch.sameCpu(Architecture::x86_64);
+static bool hasAuthRegion(std::string_view archName)
+{
+    return archName == "arm64e";
+}
+
+static uint32_t supportsTPROMapping(std::string_view archName)
+{
+    return (archName != "x86_64") && (archName != "x86_64h");
 }
 
 cache_builder::Layout::Layout(const BuilderOptions& options)
-: is64(options.arch.is64())
-    , hasAuthRegion(::hasAuthRegion(options.arch))
-    , tproIsInData(!::supportsTPROMapping(options.arch))
-    , pageSize(defaultPageSize(options.arch))
-{
-    if ( options.arch.sameCpu(Architecture::x86_64) ) {
+    : is64(options.archs.supports64())
+    , hasAuthRegion(::hasAuthRegion(options.archs.name()))
+    , tproIsInData(!::supportsTPROMapping(options.archs.name()))
+    , pageSize(defaultPageSize(options.archs.name()))
+{
+    std::string_view archName = options.archs.name();
+
+    if ( (archName == "x86_64") || (archName == "x86_64h") ) {
         // x86_64 uses discontiguous mappings
         this->discontiguous.emplace();
 
         this->discontiguous->regionAlignment = 1_GB;
-        this->discontiguous->subCacheTextLimit = CacheVMSize(512_MB);
     } else {
         // Everyone else uses contiguous mappings
         this->contiguous.emplace();
         this->contiguous->regionPadding = CacheVMSize(32_MB);
-        this->contiguous->subCacheTextDataLimit = CacheVMSize(2_GB);
         this->contiguous->subCacheStubsLimit = CacheVMSize(110_MB);
-
-        // Note we have 2 padding regions in total in a given TEXT/DATA/AUTH/... region
-        // 1 between TEXT/DATA_CONST and DATA, then another from DATA to LINKEDIT.
-        this->contiguous->subCachePadding = this->contiguous->regionPadding + this->contiguous->regionPadding;
+    }
+
+    if ( (archName == "x86_64") || (archName == "x86_64h") ) {
+        this->subCacheTextLimit = CacheVMSize(512_MB);
+    } else {
+        // Note the 64MB is to give us just a little more space before making another
+        // sub cache file.  We want to make as few files as possible for things like page-tables
+        // The real constraint here is that TEXT+DATA must stay within 2GB for int32_t relative
+        // offsets in objc/swift metadata and in unwind info.  But also the __objc_opt section
+        // in libobjc needs to reach the __OBJC_RO in the read-only subcache.  That comes after
+        // data and as of writing leaves 150MB from its end until the 2GB mark.  So take 64MB from
+        // that 150MB and hope its ok for now.
+        this->subCacheTextLimit = CacheVMSize(1.5_GB + 64_MB);
     }
 
     struct CacheLayout
@@ -117,10 +120,10 @@
     };
     CacheLayout layout;
 
-    if ( options.arch.sameCpu(Architecture::x86_64) ) {
+    if ( (archName == "x86_64") || (archName == "x86_64h") ) {
         layout.baseAddress = X86_64_SHARED_REGION_START;
         layout.cacheSize = X86_64_SHARED_REGION_SIZE;
-    } else if ( options.arch.sameCpu(Architecture::arm64) ) {
+    } else if ( (archName == "arm64") || (archName == "arm64e") ) {
         layout.baseAddress = ARM64_SHARED_REGION_START;
 
         if ( options.isSimulator() ) {
@@ -134,14 +137,9 @@
         // caches putting 1.5GB of TEXT in the first cache region, this will ensure that
         // this 1.5GB of TEXT will stay in the same 2GB region.  <rdar://problem/49852839>
         cacheMaxSlide = 512_MB;
-    } else if ( options.arch == Architecture::arm64_32 ) {
+    } else if ( archName == "arm64_32" ) {
         layout.baseAddress = ARM64_32_SHARED_REGION_START;
         layout.cacheSize = 2_GB;
-
-        // The cache contents can't exceed 2GB, but use the space above it for the slide
-        if ( ARM64_32_SHARED_REGION_SIZE >= layout.cacheSize ) {
-            this->cacheFixedSlide = ARM64_32_SHARED_REGION_SIZE - layout.cacheSize;
-        }
     } else {
         assert("Unknown arch");
     }
@@ -160,7 +158,8 @@
     if ( options.isSimulator() )
         return;
 
-    if ( options.arch.sameCpu(Architecture::x86_64) || (options.arch == Architecture::arm64) ) {
+    std::string_view archName = options.archs.name();
+    if ( (archName == "x86_64") || (archName == "x86_64h") || (archName == "arm64") ) {
         this->slideInfoFormat = SlideInfoFormat::v2;
 
         // 1 uint16_t per page
@@ -170,14 +169,14 @@
         this->slideInfoDeltaMask = 0x00FFFF0000000000ULL;
 
         // Only x86_64 needs a value add field on slide info V2
-        if ( options.arch.sameCpu(Architecture::x86_64) ) {
+        if ( (archName == "x86_64") || (archName == "x86_64h") ) {
             this->slideInfoValueAdd = layout.cacheBaseAddress;
         }
         else {
             this->slideInfoValueAdd = CacheVMAddress(0ULL);
         }
     }
-    else if ( options.arch == Architecture::arm64e ) {
+    else if ( archName == "arm64e" ) {
         // 1 uint16_t per page
         this->slideInfoBytesPerDataPage = 2;
 
@@ -190,7 +189,7 @@
             this->slideInfoFormat = SlideInfoFormat::v3;
         }
     }
-    else if ( options.arch == Architecture::arm64_32 ) {
+    else if ( archName == "arm64_32" ) {
         this->slideInfoFormat = SlideInfoFormat::v1;
 
         // 128 bytes per page.  Enough for a bitmap with 1-bit entry per 32-bit location
@@ -206,26 +205,27 @@
 // MARK: --- cache_builder::CodeSign methods ---
 //
 
-static cache_builder::CodeSign::Mode platformCodeSigningDigestMode(Platform platform)
-{
-    if ( platform == Platform::watchOS )
+static cache_builder::CodeSign::Mode platformCodeSigningDigestMode(dyld3::Platform platform)
+{
+    if ( platform == dyld3::Platform::watchOS )
         return cache_builder::CodeSign::Mode::agile;
     return cache_builder::CodeSign::Mode::onlySHA256;
 }
 
-static uint32_t codeSigningPageSize(Platform platform, Architecture arch)
-{
-    if ( (arch == Architecture::arm64e) || (arch == Architecture::arm64_32) )
+static uint32_t codeSigningPageSize(dyld3::Platform platform, const GradedArchs& arch)
+{
+    std::string_view archName = arch.name();
+    if ( (archName == "arm64e") || (archName == "arm64_32") )
         return CS_PAGE_SIZE_16K;
 
     // arm64 on iOS is new enough for 16k pages, as is arm64 on macOS (ie the simulator)
-    if ( arch == Architecture::arm64 ) {
-        if ( platform.isSimulator() || (platform == Platform::iOS) )
+    if ( archName == "arm64") {
+        if ( dyld3::MachOFile::isSimulatorPlatform(platform) || (platform == dyld3::Platform::iOS) )
             return CS_PAGE_SIZE_16K;
         return CS_PAGE_SIZE_4K;
     }
 
-    if ( arch.sameCpu(Architecture::x86_64) )
+    if ( (archName == "x86_64") || (archName == "x86_64h") )
         return CS_PAGE_SIZE_4K;
 
     // Unknown arch
@@ -234,7 +234,7 @@
 
 cache_builder::CodeSign::CodeSign(const BuilderOptions& options)
     : mode(platformCodeSigningDigestMode(options.platform))
-    , pageSize(codeSigningPageSize(options.platform, options.arch))
+    , pageSize(codeSigningPageSize(options.platform, options.archs))
 {
 }