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 | /* * 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 "MachOAppCache.h" #include <list> #include <CoreFoundation/CFArray.h> #include <CoreFoundation/CFPropertyList.h> #include <CoreFoundation/CFString.h> #ifndef LC_FILESET_ENTRY #define LC_FILESET_ENTRY (0x35 | LC_REQ_DYLD) /* used with fileset_entry_command */ struct fileset_entry_command { uint32_t cmd; /* LC_FILESET_ENTRY */ uint32_t cmdsize; /* includes id string */ uint64_t vmaddr; /* memory address of the dylib */ uint64_t fileoff; /* file offset of the dylib */ union lc_str entry_id; /* contained entry id */ uint32_t reserved; /* entry_id is 32-bits long, so this is the reserved padding */ }; #endif namespace dyld3 { void MachOAppCache::forEachDylib(Diagnostics& diag, void (^callback)(const MachOAnalyzer* ma, const char* name, bool& stop)) const { const intptr_t slide = getSlide(); forEachLoadCommand(diag, ^(const load_command *cmd, bool &stop) { if (cmd->cmd == LC_FILESET_ENTRY) { const fileset_entry_command* app_cache_cmd = (const fileset_entry_command*)cmd; const char* name = (char*)app_cache_cmd + app_cache_cmd->entry_id.offset; callback((const MachOAnalyzer*)(app_cache_cmd->vmaddr + slide), name, stop); return; } }); } void MachOAppCache::forEachPrelinkInfoLibrary(Diagnostics& diags, void (^callback)(const char* bundleName, const char* relativePath, const std::vector<const char*>& deps)) const { __block std::list<std::string> nonASCIIStrings; auto getString = ^(Diagnostics& diagnostics, CFStringRef symbolNameRef) { const char* symbolName = CFStringGetCStringPtr(symbolNameRef, kCFStringEncodingUTF8); if ( symbolName != nullptr ) return symbolName; CFIndex len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(symbolNameRef), kCFStringEncodingUTF8); char buffer[len + 1]; if ( !CFStringGetCString(symbolNameRef, buffer, len, kCFStringEncodingUTF8) ) { diagnostics.error("Could not convert string to ASCII"); return (const char*)nullptr; } buffer[len] = '\0'; nonASCIIStrings.push_back(buffer); return nonASCIIStrings.back().c_str(); }; const uint8_t* prelinkInfoBuffer = nullptr; uint64_t prelinkInfoBufferSize = 0; prelinkInfoBuffer = (const uint8_t*)findSectionContent("__PRELINK_INFO", "__info", prelinkInfoBufferSize); if ( prelinkInfoBuffer == nullptr ) return; CFReadStreamRef readStreamRef = CFReadStreamCreateWithBytesNoCopy(kCFAllocatorDefault, prelinkInfoBuffer, prelinkInfoBufferSize, kCFAllocatorNull); if ( !CFReadStreamOpen(readStreamRef) ) { fprintf(stderr, "Could not open plist stream\n"); exit(1); } CFErrorRef errorRef = nullptr; CFPropertyListRef plistRef = CFPropertyListCreateWithStream(kCFAllocatorDefault, readStreamRef, prelinkInfoBufferSize, kCFPropertyListImmutable, nullptr, &errorRef); if ( errorRef != nullptr ) { CFStringRef stringRef = CFErrorCopyFailureReason(errorRef); fprintf(stderr, "Could not read plist because: %s\n", CFStringGetCStringPtr(stringRef, kCFStringEncodingASCII)); CFRelease(stringRef); exit(1); } assert(CFGetTypeID(plistRef) == CFDictionaryGetTypeID()); // Get the "_PrelinkInfoDictionary" array CFArrayRef prelinkInfoDictionaryArrayRef = (CFArrayRef)CFDictionaryGetValue((CFDictionaryRef)plistRef, CFSTR("_PrelinkInfoDictionary")); assert(CFGetTypeID(prelinkInfoDictionaryArrayRef) == CFArrayGetTypeID()); for (CFIndex i = 0; i != CFArrayGetCount(prelinkInfoDictionaryArrayRef); ++i) { CFDictionaryRef kextInfoDictionary = (CFDictionaryRef)CFArrayGetValueAtIndex(prelinkInfoDictionaryArrayRef, i); assert(CFGetTypeID(kextInfoDictionary) == CFDictionaryGetTypeID()); CFStringRef bundleIdentifierStringRef = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)kextInfoDictionary, CFSTR("CFBundleIdentifier")); assert(CFGetTypeID(bundleIdentifierStringRef) == CFStringGetTypeID()); const char* bundleID = getString(diags, bundleIdentifierStringRef); if ( bundleID == nullptr ) return; const char* relativePath = nullptr; CFStringRef relativePathStringRef = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)kextInfoDictionary, CFSTR("_PrelinkExecutableRelativePath")); if ( relativePathStringRef != nullptr ) { assert(CFGetTypeID(relativePathStringRef) == CFStringGetTypeID()); relativePath = getString(diags, relativePathStringRef); if ( relativePath == nullptr ) return; } std::vector<const char*> dependencies; CFDictionaryRef bundleLibrariesDictionaryRef = (CFDictionaryRef)CFDictionaryGetValue((CFDictionaryRef)kextInfoDictionary, CFSTR("OSBundleLibraries")); if (bundleLibrariesDictionaryRef != nullptr) { // Add the libraries to the dependencies // If we didn't have bundle libraries then a placeholder was added assert(CFGetTypeID(bundleLibrariesDictionaryRef) == CFDictionaryGetTypeID()); struct ApplyContext { Diagnostics* diagnostics; std::vector<const char*>* dependencies = nullptr; const char* (^getString)(Diagnostics& diags, CFStringRef symbolNameRef) = nullptr; }; CFDictionaryApplierFunction applyCallback = [](const void *key, const void *value, void *context) { CFStringRef keyStringRef = (CFStringRef)key; assert(CFGetTypeID(keyStringRef) == CFStringGetTypeID()); ApplyContext* applyContext = (ApplyContext*)context; const char* depString = applyContext->getString(*applyContext->diagnostics, keyStringRef); if ( !depString ) return; applyContext->dependencies->push_back(depString); }; ApplyContext applyContext = { &diags, &dependencies, getString }; CFDictionaryApplyFunction(bundleLibrariesDictionaryRef, applyCallback, &applyContext); if ( diags.hasError() ) return; } callback(bundleID, relativePath, dependencies); } CFRelease(plistRef); CFRelease(readStreamRef); } } // namespace dyld3 |