Loading...
other-tools/dyld_closure_util.cpp dyld-1340 dyld-955
--- dyld/dyld-1340/other-tools/dyld_closure_util.cpp
+++ dyld/dyld-955/other-tools/dyld_closure_util.cpp
@@ -32,7 +32,6 @@
 #include <errno.h>
 #include <sys/mman.h>
 #include <sys/syslimits.h>
-#include <sys/stat.h>
 #include <mach-o/arch.h>
 #include <mach-o/loader.h>
 #include <mach-o/dyld_priv.h>
@@ -87,6 +86,11 @@
     const char*               printClosureFile = nullptr;
     bool                      listCacheClosures = false;
     bool                      printCachedDylibs = false;
+    bool                      verboseFixups = false;
+    bool                      allowAtPaths = true;
+    bool                      allowFallbackPaths = true;
+    bool                      allowInsertionFailures = false;
+    bool                      printRaw = false;
     std::vector<const char*>  envArgs;
     char                      fsRootRealPath[PATH_MAX];
     char                      fsOverlayRealPath[PATH_MAX];
@@ -112,6 +116,21 @@
                 return 1;
             }
         }
+        else if ( strcmp(arg, "-verbose_fixups") == 0 ) {
+           verboseFixups = true;
+        }
+        else if ( strcmp(arg, "-no_at_paths") == 0 ) {
+            allowAtPaths = false;
+        }
+        else if ( strcmp(arg, "-no_fallback_paths") == 0 ) {
+            allowFallbackPaths = false;
+        }
+        else if ( strcmp(arg, "-allow_insertion_failures") == 0 ) {
+            allowInsertionFailures = true;
+        }
+        else if ( strcmp(arg, "-raw") == 0 ) {
+            printRaw = true;
+        }
         else if ( strcmp(arg, "-fs_root") == 0 ) {
             fsRootPath = argv[++i];
             if ( fsRootPath == nullptr ) {
@@ -181,12 +200,14 @@
 
     std::vector<const DyldSharedCache*> dyldCaches;
     const DyldSharedCache* dyldCache = nullptr;
+    bool dyldCacheIsLive = true;
     if ( cacheFilePath != nullptr ) {
         dyldCaches = DyldSharedCache::mapCacheFiles(cacheFilePath);
         // mapCacheFile prints an error if something goes wrong, so just return in that case.
         if ( dyldCaches.empty() )
             return 1;
         dyldCache = dyldCaches.front();
+        dyldCacheIsLive = false;
     }
     else {
         size_t len;
@@ -201,26 +222,12 @@
             exit(1);
         }
 
-        std::string_view libSystemPath;
-        if ( dyldCache->platform() == mach_o::Platform::driverKit ) {
-            libSystemPath = "/System/DriverKit/usr/lib/libSystem.dylib";
-        } else if ( dyldCache->platform().isExclaveKit() ) {
-            libSystemPath = "/System/ExclaveKit/usr/lib/libSystem.dylib";
-        } else {
-            libSystemPath = "/usr/lib/libSystem.B.dylib";
-        }
-
         // HACK: use libSystem.dylib from cache as main executable to bootstrap state
         uint32_t imageIndex;
-        if ( dyldCache->hasImagePath(libSystemPath.data(), imageIndex) ) {
+        if ( dyldCache->hasImagePath("/usr/lib/libSystem.B.dylib", imageIndex) ) {
             uint64_t ignore1;
             uint64_t ignore2;
             mainMA = (MachOAnalyzer*)dyldCache->getIndexedImageEntry(imageIndex, ignore1, ignore2);
-        }
-
-        if ( mainMA == nullptr ) {
-            fprintf(stderr, "dyld_closure_util: can't find libSystem in dyld cache\n");
-            exit(1);
         }
     }
 
@@ -230,16 +237,12 @@
     osDelegate._rootPath    = fsRootPath;
     osDelegate._overlayPath = fsOverlayPath;
 
-    Allocator&             alloc = MemoryManager::memoryManager().defaultAllocator();
-    __block ProcessConfig  config(&kernArgs, osDelegate, alloc);
-    RuntimeLocks           locks;
-    RuntimeState           state(config, locks, alloc);
+    __block ProcessConfig  config(&kernArgs, osDelegate);
+    RuntimeState           state(config);
 
      if ( inputMainExecutablePath != nullptr ) {
-        struct stat statBuf;
-        stat(inputMainExecutablePath, &statBuf);
-        config.reset(mainMA, inputMainExecutablePath, statBuf.st_size, osDelegate._dyldCache);
-        state.resetCachedDylibsArrays(dyldCache->dylibsLoaderSet());
+        config.reset(mainMA, inputMainExecutablePath, osDelegate._dyldCache);
+        state.resetCachedDylibsArrays();
 
         // Load the executable from disk
         Diagnostics launchDiag;
@@ -247,17 +250,20 @@
         options.staticLinkage   = true;
         options.launching       = true;
         options.canBeExecutable = true;
-        if ( Loader* mainLoader = JustInTimeLoader::makeJustInTimeLoaderDisk(launchDiag, state, inputMainExecutablePath, options, false, 0, nullptr) ) {
+        if ( Loader* mainLoader = JustInTimeLoader::makeJustInTimeLoaderDisk(launchDiag, state, inputMainExecutablePath, options) ) {
             state.setMainLoader(mainLoader);
 
             // platform was a guess from libSystem.dylib, now we have the actual binary loaded, use its platform
-            mach_o::PlatformAndVersions pvs = ((mach_o::Header*)mainLoader->loadAddress(state))->platformAndVersions();
-            config.process.platform = pvs.platform;
+            mainLoader->loadAddress(state)->forEachSupportedPlatform(^(dyld3::Platform plat, uint32_t minOS, uint32_t sdk) {
+                const dyld3::Platform* p = &config.process.platform;
+                *(dyld3::Platform*)p = plat;
+            });
 
             // now that main executable is loaded, use its actual platform as the global platform
-            if ( state.config.process.platform == mach_o::Platform::macOS ) {
-                mach_o::PlatformAndVersions pvsExe = ((mach_o::Header*)mainLoader->loadAddress(state))->platformAndVersions();
-                config.process.platform = pvsExe.platform;
+            if ( state.config.process.platform == dyld3::Platform::macOS ) {
+                mainLoader->loadAddress(state)->forEachSupportedPlatform(^(dyld3::Platform exePlatform, uint32_t minOS, uint32_t sdk) {
+                    config.process.platform = exePlatform;
+                });
             }
             __block MissingPaths missingPaths;
             auto missingLogger = ^(const char* mustBeMissingPath) {
@@ -305,14 +311,9 @@
     else if ( printClosureFile ) {
         size_t       mappedSize;
         Diagnostics  diag;
-        if ( const dyld4::PrebuiltLoaderSet* pbls = (dyld4::PrebuiltLoaderSet*)config.syscall.mapFileReadOnly(diag, printClosureFile, nullptr /* fd */, &mappedSize) ) {
-            if ( pbls->validHeader(state) ) {
-                state.setProcessPrebuiltLoaderSet(pbls);
-                pbls->print(state, stdout, /* printComments */ true);
-            }
-            else {
-                fprintf(stderr, "dyld_closure_util: invalid closure file '%s'\n", printClosureFile);
-            }
+        if ( const dyld4::PrebuiltLoaderSet* pbls = (dyld4::PrebuiltLoaderSet*)config.syscall.mapFileReadOnly(diag, printClosureFile, &mappedSize) ) {
+            state.setProcessPrebuiltLoaderSet(pbls);
+            pbls->print(state, stdout, /* printComments */ true);
             config.syscall.unmapFile(pbls, mappedSize);
         }
         else {
@@ -320,7 +321,7 @@
         }
     }
     else if ( printCachedDylibs ) {
-        state.resetCachedDylibsArrays(config.dyldCache.addr->dylibsLoaderSet());
+        state.resetCachedDylibsArrays();
         if ( const dyld4::PrebuiltLoaderSet* pbls = state.cachedDylibsPrebuiltLoaderSet()) {
             for (int i=0; i < pbls->loaderCount(); ++i) {
                 const dyld4::PrebuiltLoader* pldr = pbls->atIndex(i);
@@ -329,7 +330,7 @@
         }
     }
     else if ( printCachedDylib != nullptr ) {
-        state.resetCachedDylibsArrays(config.dyldCache.addr->dylibsLoaderSet());
+        state.resetCachedDylibsArrays();
         if ( const dyld4::PrebuiltLoader* pldr = config.dyldCache.addr->findPrebuiltLoader(printCachedDylib)  ) {
             pldr->print(state, stdout, /* printComments */ true);
         }