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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | /* * Copyright (c) 2017 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 <stdlib.h> #include <assert.h> #include <string.h> #include <stdio.h> #include <TargetConditionals.h> #if !TARGET_OS_EXCLAVEKIT #include <sys/errno.h> #include <sys/fcntl.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #endif // !TARGET_OS_EXCLAVEKIT #include <AvailabilityMacros.h> #include <mach-o/dyld_introspection.h> #include <mach-o/dyld_priv.h> #if BUILDING_DYLD && !TARGET_OS_EXCLAVEKIT #include <subsystem.h> #endif // BUILDING_DYLD && !TARGET_OS_EXCLAVEKIT #include "DyldSharedCache.h" #include "Misc.h" #include "SupportedArchs.h" #include "Universal.h" #include "Archive.h" #include "Header.h" namespace mach_o { #if !TARGET_OS_EXCLAVEKIT //////////////////////////// posix wrappers //////////////////////////////////////// // <rdar://problem/10111032> wrap calls to stat() with check for EAGAIN int resilient_stat(const char* path, struct stat* buf) { int result; do { #if BUILDING_DYLD result = ::stat_with_subsystem(path, buf); #else result = ::stat(path, buf); #endif } while ((result == -1) && ((errno == EAGAIN) || (errno == EINTR))); return result; } // <rdar://problem/13805025> dyld should retry open() if it gets an EGAIN int resilient_open(const char* path, int flag, int other) { int result; do { #if BUILDING_DYLD if (flag & O_CREAT) result = ::open(path, flag, other); else result = ::open_with_subsystem(path, flag); #else result = ::open(path, flag, other); #endif } while ((result == -1) && ((errno == EAGAIN) || (errno == EINTR))); return result; } #endif // !TARGET_OS_EXCLAVEKIT //////////////////////////// ULEB128 helpers //////////////////////////////////////// uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end, bool& malformed) { uint64_t result = 0; int bit = 0; malformed = false; do { if ( p == end ) { malformed = true; break; } uint64_t slice = *p & 0x7f; if ( bit > 63 ) { malformed = true; break; } else { result |= (slice << bit); bit += 7; } } while (*p++ & 0x80); return result; } int64_t read_sleb128(const uint8_t*& p, const uint8_t* end, bool& malformed) { int64_t result = 0; int bit = 0; uint8_t byte = 0; malformed = false; do { if ( p == end ) { malformed = true; break; } byte = *p++; result |= (((int64_t)(byte & 0x7f)) << bit); bit += 7; } while (byte & 0x80); // sign extend negative numbers if ( ((byte & 0x40) != 0) && (bit < 64) ) result |= (~0ULL) << bit; return result; } uint32_t uleb128_size(uint64_t value) { uint32_t result = 0; do { value = value >> 7; ++result; } while ( value != 0 ); return result; } #if BUILDING_NM || BUILDING_DYLDINFO 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)) { 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 ( const Header* mh = 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) ) { 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 !TARGET_OS_OSX || (MAC_OS_X_VERSION_MIN_REQUIRED >= 120000) if ( !found ) { size_t cacheLen; __block const DyldSharedCache* dyldCache = (DyldSharedCache*)_dyld_get_shared_cache_range(&cacheLen); __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 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)); } } } #else (void)found; #endif } } #endif } // namespace mach_o |