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 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- * * Copyright (c) 2014 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@ */ #ifndef NewSharedCacheBuilder_h #define NewSharedCacheBuilder_h #include "Defines.h" #include "BuilderConfig.h" #include "BuilderOptions.h" #include "CacheDylib.h" #include "Chunk.h" #include "Diagnostics.h" #include "Error.h" #include "ImpCachesBuilder.h" #include "JSONReader.h" #include "MachOFile.h" #include "NewAdjustDylibSegments.h" #include "Optimizers.h" #include "OptimizerObjC.h" #include "PerfectHash.h" #include "SectionCoalescer.h" #include "SubCache.h" #include "Timer.h" #include "Types.h" #include <list> #include <memory> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> namespace dyld3 { namespace closure { class FileSystem; } } namespace cache_builder { struct CacheBuffer { uint8_t* bufferData = nullptr; size_t bufferSize = 0; std::string cdHash = ""; std::string uuid = ""; // Something like .development, .development.data, .symbols, etc std::string cacheFileSuffix; // True if customer/universal caches need this buffer bool usedByCustomerConfig = false; // True if development/universal caches need this buffer bool usedByDevelopmentConfig = false; // The builder executable also passes back the fd. This should typically be used instead of the data buffer #if !SUPPORT_CACHE_BUILDER_MEMORY_BUFFERS int fd = 0; std::string tempPath = ""; #endif }; class SharedCacheBuilder { public: SharedCacheBuilder(BuilderOptions& options, const dyld3::closure::FileSystem& fileSystem); void forEachWarning(void (^callback)(const std::string_view& str)) const; void forEachCacheDylib(void (^callback)(const std::string_view& path)) const; void forEachCacheSymlink(void (^callback)(const std::string_view& path)) const; void addFile(const void* buffer, size_t bufferSize, std::string_view path, uint64_t inode, uint64_t modTime, bool forceNotCacheEligible); void setAliases(const std::vector<FileAlias>& aliases, const std::vector<FileAlias>& intermediateAliases); error::Error build(); // If we overflow, then build() should return an error, and this should be the list of evicted dylibs std::span<const std::string_view> getEvictedDylibs() const; void getResults(std::vector<CacheBuffer>& results) const; std::string getMapFileBuffer() const; // All caches have a logging prefix, UUID and JSON map which represents the development cache. // Universal caches may also have customer versions of these std::string developmentLoggingPrefix() const; std::string developmentJSONMap(std::string_view disposition) const; std::string developmentCacheUUID() const; std::string customerLoggingPrefix() const; std::string customerJSONMap(std::string_view disposition) const; std::string customerCacheUUID() const; #if BUILDING_CACHE_BUILDER_UNIT_TESTS // We need everything public to write tests public: #else private: #endif // Phases of the build() method error::Error calculateInputs(); error::Error estimateGlobalOptimizations(); error::Error createSubCaches(); error::Error preDylibEmitChunks(); error::Error runDylibPasses(); error::Error postDylibEmitChunks(); error::Error finalize(); // Passes to run before we have cache buffers void categorizeInputs(); void verifySelfContained(); void calculateDylibAliases(); void sortDylibs(); error::Error calculateDylibDependents(); void categorizeDylibSegments(); void categorizeDylibLinkedit(); void estimateIMPCaches(); void findObjCDylibs(); void findCanonicalObjCSelectors(); void findCanonicalObjCClassNames(); void findCanonicalObjCProtocolNames(); void findObjCClasses(); void findObjCProtocols(); void findObjCCategories(); void estimateObjCHashTableSizes(); void calculateObjCCanonicalProtocolsSize(); void calculateObjCCategoriesSize(); void estimateSwiftHashTableSizes(); void calculateCacheDylibsTrie(); void estimatePatchTableSize(); void estimateCacheLoadersSize(); void setupStubOptimizer(); void addObjCOptimizationsToSubCache(SubCache& subCache); void addGlobalOptimizationsToSubCache(SubCache& subCache); void addFinalChunksToSubCache(SubCache& subCache); void computeSubCaches(); void computeLargeSubCache(); void makeLargeLayoutSubCaches(SubCache* firstSubCache, std::list<SubCache>& otherCaches); void setSubCacheNames(); error::Error calculateSubCacheSymbolStrings(); error::Error calculateUniqueGOTs(); void sortSubCacheSegments(); void calculateSlideInfoSize(); void calculateCodeSignatureSize(); void printSubCaches() const; error::Error computeSubCacheDiscontiguousVMLayout(); error::Error computeSubCacheContiguousVMLayout(); void evictLeafDylibs(CacheVMSize reductionTarget); error::Error computeSubCacheLayout(); error::Error allocateSubCacheBuffers(); void setupDylibLinkedit(); void setupSplitSegAdjustors(); void adjustObjCClasses(); void adjustObjCProtocols(); void adjustObjCCategories(); // Final passes to run, after dylib passes void emitObjCSelectorStrings(); void emitObjCClassNameStrings(); void emitObjCProtocolNameStrings(); void emitObjCSwiftDemangledNameStrings(); void emitObjCHashTables(); void emitObjCHeaderInfo(); void emitObjCOptsHeader(); error::Error emitSwiftHashTables(); void optimizeTLVs(); error::Error emitUniquedGOTs(); error::Error emitCanonicalObjCProtocols(); error::Error computeObjCClassLayout(); error::Error emitPreAttachedObjCCategories(); void computeSlideInfo(); void emitCacheDylibsTrie(); error::Error emitPatchTable(); error::Error emitCacheDylibsPrebuiltLoaders(); error::Error emitExecutablePrebuiltLoaders(); void emitSymbolTable(); void emitUnmappedLocalSymbols(); void writeSubCacheHeader(SubCache& subCache); uint64_t getMaxSlide() const; void addObjcSegments(); void computeCacheHeaders(); static bool regionIsSharedCacheMapping(const Region& region); void codeSign(); std::string generateJSONMap(std::string_view disposition, const SubCache& mainSubCache) const; typedef std::unordered_map<const InputFile*, CacheDylib*> FileToDylibMap; typedef std::unordered_map<const InputFile*, UnmappedSymbolsOptimizer::LocalSymbolInfo*> FileToSymbolInfoMap; typedef std::unordered_map<uint32_t, uint32_t> OldToNewIndicesMap; error::Error copyLocalSymbols(SubCache& subCache, const std::span<LinkeditDataChunk*> symbolStringChunks, const FileToDylibMap& fileToDylibMap, const FileToSymbolInfoMap& fileToSymbolInfoMap, const uint32_t redactedStringOffset, uint32_t& stringBufferSize, uint32_t& sourceStringSize, uint32_t& sourceStringCount); error::Error copyExportedSymbols(SubCache& subCache, const std::span<LinkeditDataChunk*> symbolStringChunks, const FileToDylibMap& fileToDylibMap, const FileToSymbolInfoMap& fileToSymbolInfoMap, std::vector<OldToNewIndicesMap>& oldToNewIndicesMaps, const uint32_t redactedStringOffset, uint32_t& stringBufferSize, uint32_t& sourceStringSize, uint32_t& sourceStringCount); error::Error copyImportedSymbols(SubCache& subCache, const std::span<LinkeditDataChunk*> symbolStringChunks, const FileToDylibMap& fileToDylibMap, const FileToSymbolInfoMap& fileToSymbolInfoMap, std::vector<OldToNewIndicesMap>& oldToNewIndicesMaps, const uint32_t redactedStringOffset, uint32_t& stringBufferSize, uint32_t& sourceStringSize, uint32_t& sourceStringCount); __attribute__((format(printf, 2, 3))) void warning(const char* format, ...); void debug(const char* installName) const; const BuilderOptions options; const dyld3::closure::FileSystem& fileSystem; BuilderConfig config; std::vector<InputFile> allInputFiles; std::vector<FileAlias> inputAliases; std::vector<FileAlias> inputIntermediateAliases; std::vector<CacheDylib> cacheDylibs; std::vector<InputFile*> exeInputFiles; std::vector<InputFile*> nonCacheDylibInputFiles; std::vector<std::string_view> evictedDylibs; std::vector<SubCache> subCaches; CacheVMSize totalVMSize; std::unordered_map<std::string, CacheDylib*> dylibAliases; bool dylibHasMissingDependency = false; std::vector<std::string> warnings; // Some optimizers are run just once per cache, so live at the top level here ObjCOptimizer objcOptimizer; ObjCIMPCachesOptimizer objcIMPCachesOptimizer; ObjCSelectorOptimizer objcSelectorOptimizer; ObjCClassOptimizer objcClassOptimizer; ObjCProtocolOptimizer objcProtocolOptimizer; ObjCCategoryOptimizer objcCategoryOptimizer; SwiftProtocolConformanceOptimizer swiftProtocolConformanceOptimizer; DylibTrieOptimizer dylibTrieOptimizer; PatchTableOptimizer patchTableOptimizer; PrebuiltLoaderBuilder prebuiltLoaderBuilder; UnmappedSymbolsOptimizer unmappedSymbolsOptimizer; StubOptimizer stubOptimizer; }; } // namespace cache_builder #endif /* NewSharedCacheBuilder_h */ |