Loading...
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 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * * Copyright (c) 2009-2012 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 "MiscFileUtils.h" // mach_o #include "Archive.h" #include "Error.h" #include "Header.h" #include "Universal.h" #include <fcntl.h> #include <sys/mman.h> #include <sys/stat.h> #include <unistd.h> #include <AvailabilityMacros.h> #include <mach-o/dyld_introspection.h> #include <mach-o/dyld_priv.h> using mach_o::Archive; using mach_o::Error; using mach_o::Header; using mach_o::Universal; namespace other_tools { bool withReadOnlyMappedFile(const char* path, void (^handler)(std::span<const uint8_t>)) { struct stat statbuf; if ( ::stat(path, &statbuf) == -1 ) return false; int fd = ::open(path, O_RDONLY, 0); if ( fd == -1 ) return false; const void* mapping = ::mmap(nullptr, (size_t)statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); ::close(fd); if ( mapping == MAP_FAILED ) return false; handler(std::span((uint8_t*)mapping, (size_t)statbuf.st_size)); ::munmap((void*)mapping, (size_t)statbuf.st_size); return true; } static bool inStringVector(const std::span<const char*>& vect, const char* target) { for (const char* str : vect) { if ( strcmp(str, target) == 0 ) return true; } return false; } void forSelectedSliceInPaths(std::span<const char*> paths, std::span<const char*> archFilter, void (^handler)(const char* path, const Header* slice, size_t len)) { forSelectedSliceInPaths(paths, archFilter, /* dyld cache */ nullptr, handler); } void forSelectedSliceInPaths(std::span<const char*> paths, std::span<const char*> archFilter, const DyldSharedCache* dyldCache, void (^handler)(const char* path, const Header* slice, size_t len)) { const auto handleArchive = [handler](const char* path, const Archive& ar) { Error err1 = ar.forEachMachO(^(const Archive::Member& m, const mach_o::Header * header, bool &stop) { char objPath[PATH_MAX]; snprintf(objPath, sizeof(objPath), "%s(%s)", path, m.name.data()); handler(objPath, header, m.contents.size()); }); if ( err1.hasError() ) fprintf(stderr, "malformed archive '%s': %s\n", path, err1.message()); }; for (const char* path : paths) { bool found = withReadOnlyMappedFile(path, ^(std::span<const uint8_t> buffer) { if ( const Universal* uni = Universal::isUniversal(buffer) ) { uni->forEachSlice(^(Universal::Slice slice, bool& stopSlice) { const char* sliceArchName = slice.arch.name(); if ( archFilter.empty() || inStringVector(archFilter, sliceArchName) ) { if ( std::optional<Archive> ar = Archive::isArchive(slice.buffer) ) { handleArchive(path, *ar); } else if ( Header::isMachO(slice.buffer) ) { handler(path, (Header*)slice.buffer.data(), slice.buffer.size()); } else { fprintf(stderr, "%s slice in %s is not a mach-o\n", sliceArchName, path); } } }); } else if ( const Header* mh = Header::isMachO(buffer) ) { if ( archFilter.empty() || inStringVector(archFilter, mh->archName()) ) handler(path, (Header*)buffer.data(), buffer.size()); } else if ( std::optional<Archive> ar = Archive::isArchive(buffer) ) { handleArchive(path, *ar); } }); // dyld_for_each_installed_shared_cache() only available in macOS 12 aligned platforms // and we only build this code for earlier versions on macOS if ( !found ) { size_t cacheLen; if ( dyldCache == nullptr ) dyldCache = (DyldSharedCache*)_dyld_get_shared_cache_range(&cacheLen); #if !TARGET_OS_OSX || (MAC_OS_X_VERSION_MIN_REQUIRED >= 120000) __block const DyldSharedCache* dyldCacheDK = nullptr; __block const char* currentArch = dyldCache->archName(); if ( strncmp(path, "/System/DriverKit/", 18) == 0 ) { if ( dyldCacheDK == nullptr ) { dyld_for_each_installed_shared_cache(^(dyld_shared_cache_t cacheRef) { //__block bool firstCacheFile = false; dyld_shared_cache_for_each_file(cacheRef, ^(const char* aCacheFilePath) { // skip non-driverkit caches if ( strncmp(aCacheFilePath, "/System/DriverKit/", 18) != 0 ) return; // skip cache files for all but matching arch with no extension const char* founddk = strstr(aCacheFilePath, currentArch); if ( founddk == nullptr || strlen(founddk) != strlen(currentArch) ) return; std::vector<const DyldSharedCache*> dyldCaches = DyldSharedCache::mapCacheFiles(aCacheFilePath); if ( dyldCaches.empty() ) return; dyldCacheDK = dyldCaches.front(); }); }); } if ( dyldCacheDK != nullptr ) { uint32_t imageIndex; if ( dyldCacheDK->hasImagePath(path, imageIndex) ) { const mach_header* mh = dyldCacheDK->getIndexedImageEntry(imageIndex); handler(path, (Header*)mh, (size_t)(-1)); } } } else #endif if ( dyldCache != nullptr ) { // see if path is in current dyld shared cache uint32_t imageIndex; if ( dyldCache->hasImagePath(path, imageIndex) ) { const mach_header* mh = dyldCache->getIndexedImageEntry(imageIndex); handler(path, (Header*)mh, (size_t)(-1)); } } } } } } // namespace other_tools |