]> git.saurik.com Git - apple/dyld.git/blob - dyld3/shared-cache/CacheBuilder.cpp
dyld-519.2.2.tar.gz
[apple/dyld.git] / dyld3 / shared-cache / CacheBuilder.cpp
1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 *
3 * Copyright (c) 2014 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26 #include <unistd.h>
27 #include <dirent.h>
28 #include <sys/errno.h>
29 #include <sys/fcntl.h>
30 #include <sys/param.h>
31 #include <mach/mach.h>
32 #include <mach/mach_time.h>
33 #include <mach-o/loader.h>
34 #include <mach-o/fat.h>
35 #include <mach/shared_region.h>
36 #include <assert.h>
37 #include <CommonCrypto/CommonHMAC.h>
38 #include <CommonCrypto/CommonDigest.h>
39 #include <CommonCrypto/CommonDigestSPI.h>
40 #include <pthread/pthread.h>
41
42 #include <string>
43 #include <vector>
44 #include <unordered_map>
45 #include <unordered_set>
46
47 #include "MachOParser.h"
48 #include "CodeSigningTypes.h"
49 #include "DyldSharedCache.h"
50 #include "CacheBuilder.h"
51 #include "FileAbstraction.hpp"
52 #include "LaunchCacheWriter.h"
53 #include "Trie.hpp"
54 #include "Diagnostics.h"
55 #include "ImageProxy.h"
56
57 #if __has_include("dyld_cache_config.h")
58 #include "dyld_cache_config.h"
59 #else
60 #define ARM_SHARED_REGION_START 0x1A000000ULL
61 #define ARM_SHARED_REGION_SIZE 0x26000000ULL
62 #define ARM64_SHARED_REGION_START 0x180000000ULL
63 #define ARM64_SHARED_REGION_SIZE 0x40000000ULL
64 #endif
65
66 const CacheBuilder::ArchLayout CacheBuilder::_s_archLayout[] = {
67 { 0x7FFF20000000ULL, 0xEFE00000ULL, 0x40000000, 0xFFFF000000000000, "x86_64", 0, 0, 0, 12, true, true },
68 { 0x7FFF20000000ULL, 0xEFE00000ULL, 0x40000000, 0xFFFF000000000000, "x86_64h", 0, 0, 0, 12, true, true },
69 { SHARED_REGION_BASE_I386, SHARED_REGION_SIZE_I386, 0x00200000, 0x0, "i386", 0, 0, 0, 12, false, false },
70 { ARM64_SHARED_REGION_START, ARM64_SHARED_REGION_SIZE, 0x02000000, 0x00FFFF0000000000, "arm64", 0x0000C000, 0x00100000, 0x07F00000, 14, false, true },
71 { ARM64_SHARED_REGION_START, ARM64_SHARED_REGION_SIZE, 0x02000000, 0x00FFFF0000000000, "arm64e", 0x0000C000, 0x00100000, 0x07F00000, 14, false, true },
72 { ARM_SHARED_REGION_START, ARM_SHARED_REGION_SIZE, 0x02000000, 0xE0000000, "armv7s", 0, 0, 0, 14, false, false },
73 { ARM_SHARED_REGION_START, ARM_SHARED_REGION_SIZE, 0x00400000, 0xE0000000, "armv7k", 0, 0, 0, 14, false, false },
74 { 0x40000000, 0x40000000, 0x02000000, 0x0, "sim-x86", 0, 0, 0, 14, false, false }
75 };
76
77
78 // These are dylibs that may be interposed, so stubs calling into them should never be bypassed
79 const char* const CacheBuilder::_s_neverStubEliminate[] = {
80 "/usr/lib/system/libdispatch.dylib",
81 nullptr
82 };
83
84
85 CacheBuilder::CacheBuilder(const DyldSharedCache::CreateOptions& options)
86 : _options(options)
87 , _buffer(nullptr)
88 , _diagnostics(options.loggingPrefix, options.verbose)
89 , _archLayout(nullptr)
90 , _aliasCount(0)
91 , _slideInfoFileOffset(0)
92 , _slideInfoBufferSizeAllocated(0)
93 , _allocatedBufferSize(0)
94 , _currentFileSize(0)
95 , _vmSize(0)
96 , _branchPoolsLinkEditStartAddr(0)
97 {
98
99 std::string targetArch = options.archName;
100 if ( options.forSimulator && (options.archName == "i386") )
101 targetArch = "sim-x86";
102
103 for (const ArchLayout& layout : _s_archLayout) {
104 if ( layout.archName == targetArch ) {
105 _archLayout = &layout;
106 break;
107 }
108 }
109 }
110
111
112 std::string CacheBuilder::errorMessage()
113 {
114 return _diagnostics.errorMessage();
115 }
116
117 const std::set<std::string> CacheBuilder::warnings()
118 {
119 return _diagnostics.warnings();
120 }
121
122 void CacheBuilder::deleteBuffer()
123 {
124 vm_deallocate(mach_task_self(), (vm_address_t)_buffer, _allocatedBufferSize);
125 _buffer = nullptr;
126 _allocatedBufferSize = 0;
127 }
128
129 std::vector<DyldSharedCache::MappedMachO>
130 CacheBuilder::makeSortedDylibs(const std::vector<DyldSharedCache::MappedMachO>& dylibs, const std::unordered_map<std::string, unsigned> sortOrder)
131 {
132 std::vector<DyldSharedCache::MappedMachO> sortedDylibs = dylibs;
133
134 std::sort(sortedDylibs.begin(), sortedDylibs.end(), [&](const DyldSharedCache::MappedMachO& a, const DyldSharedCache::MappedMachO& b) {
135 const auto& orderA = sortOrder.find(a.runtimePath);
136 const auto& orderB = sortOrder.find(b.runtimePath);
137 bool foundA = (orderA != sortOrder.end());
138 bool foundB = (orderB != sortOrder.end());
139
140 // Order all __DATA_DIRTY segments specified in the order file first, in
141 // the order specified in the file, followed by any other __DATA_DIRTY
142 // segments in lexicographic order.
143 if ( foundA && foundB )
144 return orderA->second < orderB->second;
145 else if ( foundA )
146 return true;
147 else if ( foundB )
148 return false;
149 else
150 return a.runtimePath < b.runtimePath;
151 });
152
153 return sortedDylibs;
154 }
155
156
157 inline uint32_t absolutetime_to_milliseconds(uint64_t abstime)
158 {
159 return (uint32_t)(abstime/1000/1000);
160 }
161
162 struct DylibAndSize
163 {
164 const char* installName;
165 uint64_t size;
166 };
167
168 bool CacheBuilder::cacheOverflow(const dyld_cache_mapping_info regions[3])
169 {
170 if ( _archLayout->sharedRegionsAreDiscontiguous ) {
171 // for macOS x86_64 cache, need to check each region for overflow
172 return ( (regions[0].size > 0x60000000) || (regions[1].size > 0x40000000) || (regions[2].size > 0x3FE00000) );
173 }
174 else {
175 return (_vmSize > _archLayout->sharedMemorySize);
176 }
177 }
178
179 bool CacheBuilder::build(const std::vector<DyldSharedCache::MappedMachO>& dylibs,
180 const std::vector<DyldSharedCache::MappedMachO>& otherOsDylibsInput,
181 const std::vector<DyldSharedCache::MappedMachO>& osExecutables)
182 {
183 // <rdar://problem/21317611> error out instead of crash if cache has no dylibs
184 // FIXME: plist should specify required vs optional dylibs
185 if ( dylibs.size() < 30 ) {
186 _diagnostics.error("missing required minimum set of dylibs");
187 return false;
188 }
189 uint64_t t1 = mach_absolute_time();
190
191
192 // make copy of dylib list and sort
193 std::vector<DyldSharedCache::MappedMachO> sortedDylibs = makeSortedDylibs(dylibs, _options.dylibOrdering);
194 std::vector<DyldSharedCache::MappedMachO> otherOsDylibs = otherOsDylibsInput;
195
196 // assign addresses for each segment of each dylib in new cache
197 dyld_cache_mapping_info regions[3];
198 SegmentMapping segmentMapping = assignSegmentAddresses(sortedDylibs, regions);
199 if ( cacheOverflow(regions) ) {
200 if ( !_options.evictLeafDylibsOnOverflow ) {
201 _diagnostics.error("cache overflow: %lluMB (max %lluMB)", _vmSize / 1024 / 1024, (_archLayout->sharedMemorySize) / 1024 / 1024);
202 return false;
203 }
204 // find all leaf (not referenced by anything else in cache) dylibs
205
206 // build count of how many references there are to each dylib
207 __block std::map<std::string, unsigned int> referenceCount;
208 for (const DyldSharedCache::MappedMachO& dylib : sortedDylibs) {
209 dyld3::MachOParser parser(dylib.mh);
210 parser.forEachDependentDylib(^(const char* loadPath, bool isWeak, bool isReExport, bool isUpward, uint32_t compatVersion, uint32_t curVersion, bool &stop) {
211 referenceCount[loadPath] += 1;
212 });
213 }
214
215 // find all dylibs not referenced
216 std::vector<DylibAndSize> unreferencedDylibs;
217 for (const DyldSharedCache::MappedMachO& dylib : sortedDylibs) {
218 dyld3::MachOParser parser(dylib.mh);
219 const char* installName = parser.installName();
220 if ( referenceCount.count(installName) == 0 ) {
221 // conservative: sum up all segments except LINKEDIT
222 __block uint64_t segsSize = 0;
223 parser.forEachSegment(^(const char* segName, uint32_t fileOffset, uint32_t fileSize, uint64_t vmAddr, uint64_t vmSize, uint8_t protections, bool &stop) {
224 if ( strcmp(segName, "__LINKEDIT") != 0 )
225 segsSize += vmSize;
226 });
227 unreferencedDylibs.push_back({installName, segsSize});
228 }
229 }
230 // sort leaf dylibs by size
231 std::sort(unreferencedDylibs.begin(), unreferencedDylibs.end(), [&](const DylibAndSize& a, const DylibAndSize& b) {
232 return ( a.size > b.size );
233 });
234
235 // build set of dylibs that if removed will allow cache to build
236 uint64_t reductionTarget = _vmSize - _archLayout->sharedMemorySize;
237 std::set<std::string> toRemove;
238 for (DylibAndSize& dylib : unreferencedDylibs) {
239 if ( _options.verbose )
240 _diagnostics.warning("to prevent cache overflow, not caching %s", dylib.installName);
241 toRemove.insert(dylib.installName);
242 if ( dylib.size > reductionTarget )
243 break;
244 reductionTarget -= dylib.size;
245 }
246 // transfer overflow dylibs from cached vector to other vector
247 for (const std::string& installName : toRemove) {
248 for (std::vector<DyldSharedCache::MappedMachO>::iterator it=sortedDylibs.begin(); it != sortedDylibs.end(); ++it) {
249 dyld3::MachOParser parser(it->mh);
250 if ( installName == parser.installName() ) {
251 otherOsDylibs.push_back(*it);
252 sortedDylibs.erase(it);
253 break;
254 }
255 }
256 }
257 // re-layout cache
258 segmentMapping = assignSegmentAddresses(sortedDylibs, regions);
259 if ( cacheOverflow(regions) ) {
260 _diagnostics.error("cache overflow, tried evicting %ld leaf daylibs, but still too big: %lluMB (max %lluMB)",
261 toRemove.size(), _vmSize / 1024 / 1024, (_archLayout->sharedMemorySize) / 1024 / 1024);
262 return false;
263 }
264 }
265
266 // allocate buffer for new cache
267 _allocatedBufferSize = std::max(_currentFileSize, (uint64_t)0x100000)*1.1; // add 10% to allocation to support large closures
268 if ( vm_allocate(mach_task_self(), (vm_address_t*)&_buffer, _allocatedBufferSize, VM_FLAGS_ANYWHERE) != 0 ) {
269 _diagnostics.error("could not allocate buffer");
270 return false;
271 }
272 _currentFileSize = _allocatedBufferSize;
273
274 // write unoptimized cache
275 writeCacheHeader(regions, sortedDylibs, segmentMapping);
276 copyRawSegments(sortedDylibs, segmentMapping);
277 adjustAllImagesForNewSegmentLocations(sortedDylibs, segmentMapping);
278 if ( _diagnostics.hasError() )
279 return false;
280
281 bindAllImagesInCacheFile(regions);
282 if ( _diagnostics.hasError() )
283 return false;
284
285 // optimize ObjC
286 if ( _options.optimizeObjC )
287 optimizeObjC(_buffer, _archLayout->is64, _options.optimizeStubs, _pointersForASLR, _diagnostics);
288 if ( _diagnostics.hasError() )
289 return false;
290
291 // optimize away stubs
292 std::vector<uint64_t> branchPoolOffsets;
293 uint64_t cacheStartAddress = _archLayout->sharedMemoryStart;
294 if ( _options.optimizeStubs ) {
295 std::vector<uint64_t> branchPoolStartAddrs;
296 const uint64_t* p = (uint64_t*)((uint8_t*)_buffer + _buffer->header.branchPoolsOffset);
297 for (int i=0; i < _buffer->header.branchPoolsCount; ++i) {
298 uint64_t poolAddr = p[i];
299 branchPoolStartAddrs.push_back(poolAddr);
300 branchPoolOffsets.push_back(poolAddr - cacheStartAddress);
301 }
302 bypassStubs(_buffer, branchPoolStartAddrs, _s_neverStubEliminate, _diagnostics);
303 }
304 uint64_t t2 = mach_absolute_time();
305
306 // FIPS seal corecrypto, This must be done after stub elimination (so that
307 // __TEXT,__text is not changed after sealing), but before LINKEDIT
308 // optimization (so that we still have access to local symbols)
309 fipsSign();
310
311 // merge and compact LINKEDIT segments
312 dyld_cache_local_symbols_info* localsInfo = nullptr;
313 if ( dylibs.size() == 0 )
314 _currentFileSize = 0x1000;
315 else
316 _currentFileSize = optimizeLinkedit(_buffer, _archLayout->is64, _options.excludeLocalSymbols, _options.optimizeStubs, branchPoolOffsets, _diagnostics, &localsInfo);
317
318 uint64_t t3 = mach_absolute_time();
319
320 // add ImageGroup for all dylibs in cache
321 __block std::vector<DyldSharedCache::MappedMachO> cachedDylibs;
322 std::unordered_map<std::string, const DyldSharedCache::MappedMachO*> mapIntoSortedDylibs;
323 for (const DyldSharedCache::MappedMachO& entry : sortedDylibs) {
324 mapIntoSortedDylibs[entry.runtimePath] = &entry;
325 }
326 _buffer->forEachImage(^(const mach_header* mh, const char* installName) {
327 auto pos = mapIntoSortedDylibs.find(installName);
328 if ( pos != mapIntoSortedDylibs.end() ) {
329 DyldSharedCache::MappedMachO newEntry = *(pos->second);
330 newEntry.mh = mh;
331 cachedDylibs.push_back(newEntry);
332 }
333 else {
334 bool found = false;
335 for (const std::string& prefix : _options.pathPrefixes) {
336 std::string fullPath = prefix + installName;
337 char resolvedPath[PATH_MAX];
338 if ( realpath(fullPath.c_str(), resolvedPath) != nullptr ) {
339 std::string resolvedUnPrefixed = &resolvedPath[prefix.size()];
340 pos = mapIntoSortedDylibs.find(resolvedUnPrefixed);
341 if ( pos != mapIntoSortedDylibs.end() ) {
342 DyldSharedCache::MappedMachO newEntry = *(pos->second);
343 newEntry.mh = mh;
344 cachedDylibs.push_back(newEntry);
345 found = true;
346 }
347 }
348 }
349 if ( !found )
350 fprintf(stderr, "missing mapping for %s\n", installName);
351 }
352 });
353 dyld3::DyldCacheParser dyldCacheParser(_buffer, true);
354 dyld3::ImageProxyGroup* dylibGroup = dyld3::ImageProxyGroup::makeDyldCacheDylibsGroup(_diagnostics, dyldCacheParser, cachedDylibs,
355 _options.pathPrefixes, _patchTable,
356 _options.optimizeStubs, !_options.dylibsRemovedDuringMastering);
357 if ( _diagnostics.hasError() )
358 return false;
359 addCachedDylibsImageGroup(dylibGroup);
360 if ( _diagnostics.hasError() )
361 return false;
362
363 uint64_t t4 = mach_absolute_time();
364
365 // add ImageGroup for other OS dylibs and bundles
366 dyld3::ImageProxyGroup* otherGroup = dyld3::ImageProxyGroup::makeOtherOsGroup(_diagnostics, dyldCacheParser, dylibGroup, otherOsDylibs,
367 _options.inodesAreSameAsRuntime, _options.pathPrefixes);
368 if ( _diagnostics.hasError() )
369 return false;
370 addCachedOtherDylibsImageGroup(otherGroup);
371 if ( _diagnostics.hasError() )
372 return false;
373
374 uint64_t t5 = mach_absolute_time();
375
376 // compute and add launch closures
377 std::map<std::string, const dyld3::launch_cache::binary_format::Closure*> closures;
378 for (const DyldSharedCache::MappedMachO& mainProg : osExecutables) {
379 Diagnostics clsDiag;
380 const dyld3::launch_cache::binary_format::Closure* cls = dyld3::ImageProxyGroup::makeClosure(clsDiag, dyldCacheParser, dylibGroup, otherGroup, mainProg,
381 _options.inodesAreSameAsRuntime, _options.pathPrefixes);
382 if ( clsDiag.hasError() ) {
383 // if closure cannot be built, silently skip it, unless in verbose mode
384 if ( _options.verbose ) {
385 _diagnostics.warning("building closure for '%s': %s", mainProg.runtimePath.c_str(), clsDiag.errorMessage().c_str());
386 for (const std::string& warn : clsDiag.warnings() )
387 _diagnostics.warning("%s", warn.c_str());
388 }
389 }
390 else {
391 closures[mainProg.runtimePath] = cls;
392 }
393 }
394 addClosures(closures);
395 if ( _diagnostics.hasError() )
396 return false;
397
398 uint64_t t6 = mach_absolute_time();
399
400 // fill in slide info at start of region[2]
401 // do this last because it modifies pointers in DATA segments
402 if ( _options.cacheSupportsASLR ) {
403 if ( _archLayout->is64 )
404 writeSlideInfoV2<Pointer64<LittleEndian>>();
405 else
406 writeSlideInfoV2<Pointer32<LittleEndian>>();
407 }
408
409 uint64_t t7 = mach_absolute_time();
410
411 // update last region size
412 dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)_buffer + _buffer->header.mappingOffset);
413 _currentFileSize = align(_currentFileSize, _archLayout->sharedRegionAlignP2);
414 mappings[2].size = _currentFileSize - mappings[2].fileOffset;
415
416 // record cache bounds
417 _buffer->header.sharedRegionStart = _archLayout->sharedMemoryStart;
418 _buffer->header.sharedRegionSize = _archLayout->sharedMemorySize;
419 if ( _archLayout->sharedRegionsAreDiscontiguous ) {
420 // special case x86_64 which has three non-contiguous chunks each in their own 1GB regions
421 uint64_t maxSlide0 = 0x60000000 - mappings[0].size; // TEXT region has 1.5GB region
422 uint64_t maxSlide1 = 0x40000000 - mappings[1].size;
423 uint64_t maxSlide2 = 0x3FE00000 - mappings[2].size;
424 _buffer->header.maxSlide = std::min(std::min(maxSlide0, maxSlide1), maxSlide2);
425 }
426 else {
427 _buffer->header.maxSlide = (_archLayout->sharedMemoryStart + _archLayout->sharedMemorySize) - (mappings[2].address + mappings[2].size);
428 }
429
430 // append "unmapped" local symbols region
431 if ( _options.excludeLocalSymbols ) {
432 size_t localsInfoSize = align(localsInfo->stringsOffset + localsInfo->stringsSize, _archLayout->sharedRegionAlignP2);
433 if ( _currentFileSize + localsInfoSize > _allocatedBufferSize ) {
434 _diagnostics.warning("local symbols omitted because cache buffer overflow");
435 }
436 else {
437 memcpy((char*)_buffer+_currentFileSize, localsInfo, localsInfoSize);
438 _buffer->header.localSymbolsOffset = _currentFileSize;
439 _buffer->header.localSymbolsSize = localsInfoSize;
440 _currentFileSize += localsInfoSize;
441 }
442 free((void*)localsInfo);
443 }
444
445 recomputeCacheUUID();
446
447 // Calculate the VMSize of the resulting cache
448 __block uint64_t endAddr = 0;
449 _buffer->forEachRegion(^(const void* content, uint64_t vmAddr, uint64_t size, uint32_t permissions) {
450 if (vmAddr+size > endAddr)
451 endAddr = vmAddr+size;
452 });
453 _vmSize = endAddr - cacheStartAddress;
454
455 // last sanity check on size
456 if ( _vmSize > _archLayout->sharedMemorySize ) {
457 _diagnostics.error("cache overflow after optimizations. %lluMB (max %lluMB)", _vmSize / 1024 / 1024, (_archLayout->sharedMemorySize) / 1024 / 1024);
458 return true;
459 }
460
461 // codesignature is part of file, but is not mapped
462 codeSign();
463 if ( _diagnostics.hasError() )
464 return false;
465
466 uint64_t t8 = mach_absolute_time();
467
468 if ( _options.verbose ) {
469 fprintf(stderr, "time to copy and bind cached dylibs: %ums\n", absolutetime_to_milliseconds(t2-t1));
470 fprintf(stderr, "time to optimize LINKEDITs: %ums\n", absolutetime_to_milliseconds(t3-t2));
471 fprintf(stderr, "time to build ImageGroup of %lu cached dylibs: %ums\n", sortedDylibs.size(), absolutetime_to_milliseconds(t4-t3));
472 fprintf(stderr, "time to build ImageGroup of %lu other dylibs: %ums\n", otherOsDylibs.size(), absolutetime_to_milliseconds(t5-t4));
473 fprintf(stderr, "time to build %lu closures: %ums\n", osExecutables.size(), absolutetime_to_milliseconds(t6-t5));
474 fprintf(stderr, "time to compute slide info: %ums\n", absolutetime_to_milliseconds(t7-t6));
475 fprintf(stderr, "time to compute UUID and codesign cache file: %ums\n", absolutetime_to_milliseconds(t8-t7));
476 }
477
478 // trim over allocated buffer
479 if ( _allocatedBufferSize > _currentFileSize ) {
480 uint8_t* startOfUnused = (uint8_t*)_buffer+_currentFileSize;
481 size_t unusedLen = _allocatedBufferSize-_currentFileSize;
482 vm_deallocate(mach_task_self(), (vm_address_t)startOfUnused, unusedLen);
483 _allocatedBufferSize = _currentFileSize;
484 }
485
486 return false;
487 }
488
489
490 void CacheBuilder::writeCacheHeader(const dyld_cache_mapping_info regions[3], const std::vector<DyldSharedCache::MappedMachO>& dylibs, const SegmentMapping& segmentMappings)
491 {
492 // "dyld_v1" + spaces + archName(), with enough spaces to pad to 15 bytes
493 std::string magic = "dyld_v1";
494 magic.append(15 - magic.length() - _options.archName.length(), ' ');
495 magic.append(_options.archName);
496 assert(magic.length() == 15);
497
498 // fill in header
499 memcpy(_buffer->header.magic, magic.c_str(), 16);
500 _buffer->header.mappingOffset = sizeof(dyld_cache_header);
501 _buffer->header.mappingCount = 3;
502 _buffer->header.imagesOffset = (uint32_t)(_buffer->header.mappingOffset + 3*sizeof(dyld_cache_mapping_info) + sizeof(uint64_t)*_branchPoolStarts.size());
503 _buffer->header.imagesCount = (uint32_t)dylibs.size() + _aliasCount;
504 _buffer->header.dyldBaseAddress = 0;
505 _buffer->header.codeSignatureOffset= 0;
506 _buffer->header.codeSignatureSize = 0;
507 _buffer->header.slideInfoOffset = _slideInfoFileOffset;
508 _buffer->header.slideInfoSize = _slideInfoBufferSizeAllocated;
509 _buffer->header.localSymbolsOffset = 0;
510 _buffer->header.localSymbolsSize = 0;
511 _buffer->header.cacheType = _options.optimizeStubs ? kDyldSharedCacheTypeProduction : kDyldSharedCacheTypeDevelopment;
512 _buffer->header.accelerateInfoAddr = 0;
513 _buffer->header.accelerateInfoSize = 0;
514 bzero(_buffer->header.uuid, 16); // overwritten later by recomputeCacheUUID()
515 _buffer->header.branchPoolsOffset = _buffer->header.mappingOffset + 3*sizeof(dyld_cache_mapping_info);
516 _buffer->header.branchPoolsCount = (uint32_t)_branchPoolStarts.size();
517 _buffer->header.imagesTextOffset = _buffer->header.imagesOffset + sizeof(dyld_cache_image_info)*_buffer->header.imagesCount;
518 _buffer->header.imagesTextCount = dylibs.size();
519 _buffer->header.platform = (uint8_t)_options.platform;
520 _buffer->header.formatVersion = dyld3::launch_cache::binary_format::kFormatVersion;
521 _buffer->header.dylibsExpectedOnDisk = !_options.dylibsRemovedDuringMastering;
522 _buffer->header.simulator = _options.forSimulator;
523
524 // fill in mappings
525 dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)_buffer + _buffer->header.mappingOffset);
526 mappings[0] = regions[0];
527 mappings[1] = regions[1];
528 mappings[2] = regions[2];
529
530 // fill in branch pool addresses
531 uint64_t* p = (uint64_t*)((char*)_buffer + _buffer->header.branchPoolsOffset);
532 for (uint64_t pool : _branchPoolStarts) {
533 *p++ = pool;
534 }
535
536 // fill in image table
537 dyld_cache_image_info* images = (dyld_cache_image_info*)((char*)_buffer + _buffer->header.imagesOffset);
538 for (const DyldSharedCache::MappedMachO& dylib : dylibs) {
539 const std::vector<SegmentMappingInfo>& segs = segmentMappings.at(dylib.mh);
540 dyld3::MachOParser parser(dylib.mh);
541 const char* installName = parser.installName();
542 images->address = segs[0].dstCacheAddress;
543 if ( _options.dylibsRemovedDuringMastering ) {
544 images->modTime = 0;
545 images->inode = pathHash(installName);
546 }
547 else {
548 images->modTime = dylib.modTime;
549 images->inode = dylib.inode;
550 }
551 uint32_t installNameOffsetInTEXT = (uint32_t)(installName - (char*)dylib.mh);
552 images->pathFileOffset = (uint32_t)segs[0].dstCacheOffset + installNameOffsetInTEXT;
553 ++images;
554 }
555 // append aliases image records and strings
556 /*
557 for (auto &dylib : _dylibs) {
558 if (!dylib->installNameAliases.empty()) {
559 for (const std::string& alias : dylib->installNameAliases) {
560 images->set_address(_segmentMap[dylib][0].address);
561 if (_manifest.platform() == "osx") {
562 images->modTime = dylib->lastModTime;
563 images->inode = dylib->inode;
564 }
565 else {
566 images->modTime = 0;
567 images->inode = pathHash(alias.c_str());
568 }
569 images->pathFileOffset = offset;
570 //fprintf(stderr, "adding alias %s for %s\n", alias.c_str(), dylib->installName.c_str());
571 ::strcpy((char*)&_buffer[offset], alias.c_str());
572 offset += alias.size() + 1;
573 ++images;
574 }
575 }
576 }
577 */
578 // calculate start of text image array and trailing string pool
579 dyld_cache_image_text_info* textImages = (dyld_cache_image_text_info*)((char*)_buffer + _buffer->header.imagesTextOffset);
580 uint32_t stringOffset = (uint32_t)(_buffer->header.imagesTextOffset + sizeof(dyld_cache_image_text_info) * dylibs.size());
581
582 // write text image array and image names pool at same time
583 for (const DyldSharedCache::MappedMachO& dylib : dylibs) {
584 const std::vector<SegmentMappingInfo>& segs = segmentMappings.at(dylib.mh);
585 dyld3::MachOParser parser(dylib.mh);
586 parser.getUuid(textImages->uuid);
587 textImages->loadAddress = segs[0].dstCacheAddress;
588 textImages->textSegmentSize = (uint32_t)segs[0].dstCacheSegmentSize;
589 textImages->pathOffset = stringOffset;
590 const char* installName = parser.installName();
591 ::strcpy((char*)_buffer + stringOffset, installName);
592 stringOffset += (uint32_t)strlen(installName)+1;
593 ++textImages;
594 }
595
596 // make sure header did not overflow into first mapped image
597 const dyld_cache_image_info* firstImage = (dyld_cache_image_info*)((char*)_buffer + _buffer->header.imagesOffset);
598 assert(stringOffset <= (firstImage->address - mappings[0].address));
599 }
600
601
602 void CacheBuilder::copyRawSegments(const std::vector<DyldSharedCache::MappedMachO>& dylibs, const SegmentMapping& mapping)
603 {
604 uint8_t* cacheBytes = (uint8_t*)_buffer;
605 for (const DyldSharedCache::MappedMachO& dylib : dylibs) {
606 auto pos = mapping.find(dylib.mh);
607 assert(pos != mapping.end());
608 for (const SegmentMappingInfo& info : pos->second) {
609 //fprintf(stderr, "copy %s segment %s (0x%08X bytes) from %p to %p (logical addr 0x%llX) for %s\n", _options.archName.c_str(), info.segName, info.copySegmentSize, info.srcSegment, &cacheBytes[info.dstCacheOffset], info.dstCacheAddress, dylib.runtimePath.c_str());
610 ::memcpy(&cacheBytes[info.dstCacheOffset], info.srcSegment, info.copySegmentSize);
611 }
612 }
613 }
614
615 void CacheBuilder::adjustAllImagesForNewSegmentLocations(const std::vector<DyldSharedCache::MappedMachO>& dylibs, const SegmentMapping& mapping)
616 {
617 uint8_t* cacheBytes = (uint8_t*)_buffer;
618 for (const DyldSharedCache::MappedMachO& dylib : dylibs) {
619 auto pos = mapping.find(dylib.mh);
620 assert(pos != mapping.end());
621 mach_header* mhInCache = (mach_header*)&cacheBytes[pos->second[0].dstCacheOffset];
622 adjustDylibSegments(_buffer, _archLayout->is64, mhInCache, pos->second, _pointersForASLR, _diagnostics);
623 if ( _diagnostics.hasError() )
624 break;
625 }
626 }
627
628 struct Counts {
629 unsigned long lazyCount = 0;
630 unsigned long nonLazyCount = 0;
631 };
632
633 void CacheBuilder::bindAllImagesInCacheFile(const dyld_cache_mapping_info regions[3])
634 {
635 const bool log = false;
636 __block std::unordered_map<std::string, Counts> useCounts;
637
638 // build map of install names to mach_headers
639 __block std::unordered_map<std::string, const mach_header*> installNameToMH;
640 __block std::vector<const mach_header*> dylibMHs;
641 _buffer->forEachImage(^(const mach_header* mh, const char* installName) {
642 installNameToMH[installName] = mh;
643 dylibMHs.push_back(mh);
644 });
645
646 __block Diagnostics parsingDiag;
647 bool (^dylibFinder)(uint32_t, const char*, void* , const mach_header**, void**) = ^(uint32_t depIndex, const char* depLoadPath, void* extra, const mach_header** foundMH, void** foundExtra) {
648 auto pos = installNameToMH.find(depLoadPath);
649 if ( pos != installNameToMH.end() ) {
650 *foundMH = pos->second;
651 *foundExtra = nullptr;
652 return true;
653 }
654 parsingDiag.error("dependent dylib %s not found", depLoadPath);
655 return false;
656 };
657 if ( parsingDiag.hasError() ) {
658 _diagnostics.error("%s", parsingDiag.errorMessage().c_str());
659 return;
660 }
661
662 // bind every dylib in cache
663 for (const mach_header* mh : dylibMHs) {
664 dyld3::MachOParser parser(mh, true);
665 bool is64 = parser.is64();
666 const char* depPaths[256];
667 const char** depPathsArray = depPaths;
668 __block int depIndex = 1;
669 parser.forEachDependentDylib(^(const char* loadPath, bool isWeak, bool isReExport, bool isUpward, uint32_t compatVersion, uint32_t curVersion, bool& stop) {
670 depPathsArray[depIndex++] = loadPath;
671 });
672 uint8_t* segCacheStarts[10];
673 uint64_t segCacheAddrs[10];
674 uint8_t** segCacheStartsArray = segCacheStarts;
675 uint64_t* segCacheAddrsArray = segCacheAddrs;
676 __block int segIndex = 0;
677 parser.forEachSegment(^(const char* segName, uint32_t fileOffset, uint32_t fileSize, uint64_t vmAddr, uint64_t vmSize, uint8_t protections, bool& stop) {
678 segCacheStartsArray[segIndex] = (segIndex == 0) ? (uint8_t*)mh : (uint8_t*)_buffer + fileOffset;
679 segCacheAddrsArray[segIndex] = vmAddr;
680 ++segIndex;
681 });
682 __block Diagnostics bindingDiag;
683 parser.forEachBind(bindingDiag, ^(uint32_t dataSegIndex, uint64_t dataSegOffset, uint8_t type, int libOrdinal, uint64_t addend, const char* symbolName, bool weakImport, bool lazy, bool& stop) {
684 if ( log ) {
685 if ( lazy )
686 useCounts[symbolName].lazyCount += 1;
687 else
688 useCounts[symbolName].nonLazyCount += 1;
689 }
690 const mach_header* targetMH = nullptr;
691 if ( libOrdinal == BIND_SPECIAL_DYLIB_SELF ) {
692 targetMH = mh;
693 }
694 else if ( libOrdinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE ) {
695 parsingDiag.error("bind ordinal BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE not supported in dylibs in dyld shared cache (found in %s)", parser.installName());
696 stop = true;
697 return;
698 }
699 else if ( libOrdinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP ) {
700 parsingDiag.error("bind ordinal BIND_SPECIAL_DYLIB_FLAT_LOOKUP not supported in dylibs in dyld shared cache (found in %s)", parser.installName());
701 stop = true;
702 return;
703 }
704 else {
705 const char* fromPath = depPathsArray[libOrdinal];
706 auto pos = installNameToMH.find(fromPath);
707 if (pos == installNameToMH.end()) {
708 if (!weakImport) {
709 _diagnostics.error("dependent dylib %s not found", fromPath);
710 }
711 return;
712 }
713 targetMH = pos->second;
714 }
715 dyld3::MachOParser targetParser(targetMH, true);
716 dyld3::MachOParser::FoundSymbol foundInfo;
717 uint64_t targetValue = 0;
718 uint8_t* fixupLoc = segCacheStartsArray[dataSegIndex] + dataSegOffset;
719 if ( targetParser.findExportedSymbol(parsingDiag, symbolName, nullptr, foundInfo, dylibFinder) ) {
720 const mach_header* foundInMH = foundInfo.foundInDylib;
721 dyld3::MachOParser foundInParser(foundInMH, true);
722 uint64_t foundInBaseAddress = foundInParser.preferredLoadAddress();
723 switch ( foundInfo.kind ) {
724 case dyld3::MachOParser::FoundSymbol::Kind::resolverOffset:
725 // Bind to the target stub for resolver based functions.
726 // There may be a later optimization to alter the client
727 // stubs to directly to the target stub's lazy pointer.
728 case dyld3::MachOParser::FoundSymbol::Kind::headerOffset:
729 targetValue = foundInBaseAddress + foundInfo.value + addend;
730 _pointersForASLR.push_back((void*)fixupLoc);
731 if ( foundInMH != mh ) {
732 uint32_t mhVmOffset = (uint32_t)((uint8_t*)foundInMH - (uint8_t*)_buffer);
733 uint32_t definitionCacheVmOffset = (uint32_t)(mhVmOffset + foundInfo.value);
734 uint32_t referenceCacheDataVmOffset = (uint32_t)(segCacheAddrsArray[dataSegIndex] + dataSegOffset - regions[1].address);
735 assert(referenceCacheDataVmOffset < (1<<30));
736 dyld3::launch_cache::binary_format::PatchOffset entry;
737 entry.last = false;
738 entry.hasAddend = (addend != 0);
739 entry.dataRegionOffset = referenceCacheDataVmOffset;
740 _patchTable[foundInMH][definitionCacheVmOffset].insert(*((uint32_t*)&entry));
741 }
742 break;
743 case dyld3::MachOParser::FoundSymbol::Kind::absolute:
744 // pointers set to absolute values are not slid
745 targetValue = foundInfo.value + addend;
746 break;
747 }
748 }
749 else if ( weakImport ) {
750 // weak pointers set to zero are not slid
751 targetValue = 0;
752 }
753 else {
754 parsingDiag.error("cannot find symbol %s, needed in dylib %s", symbolName, parser.installName());
755 stop = true;
756 }
757 switch ( type ) {
758 case BIND_TYPE_POINTER:
759 if ( is64 )
760 *((uint64_t*)fixupLoc) = targetValue;
761 else
762 *((uint32_t*)fixupLoc) = (uint32_t)targetValue;
763 break;
764 case BIND_TYPE_TEXT_ABSOLUTE32:
765 case BIND_TYPE_TEXT_PCREL32:
766 parsingDiag.error("text relocs not supported for shared cache binding in %s", parser.installName());
767 stop = true;
768 break;
769 default:
770 parsingDiag.error("bad bind type (%d) in %s", type, parser.installName());
771 stop = true;
772 break;
773
774 }
775 });
776 if ( bindingDiag.hasError() ) {
777 parsingDiag.error("%s in dylib %s", bindingDiag.errorMessage().c_str(), parser.installName());
778 }
779 if ( parsingDiag.hasError() )
780 break;
781 // also need to add patch locations for weak-binds that point within same image, since they are not captured by binds above
782 parser.forEachWeakDef(bindingDiag, ^(bool strongDef, uint32_t dataSegIndex, uint64_t dataSegOffset, uint64_t addend, const char* symbolName, bool &stop) {
783 if ( strongDef )
784 return;
785 uint8_t* fixupLoc = segCacheStartsArray[dataSegIndex] + dataSegOffset;
786 dyld3::MachOParser::FoundSymbol weakFoundInfo;
787 Diagnostics weakLookupDiag;
788 if ( parser.findExportedSymbol(weakLookupDiag, symbolName, nullptr, weakFoundInfo, nullptr) ) {
789 // this is an interior pointing (rebased) pointer
790 uint64_t targetValue;
791 if ( is64 )
792 targetValue = *((uint64_t*)fixupLoc);
793 else
794 targetValue = *((uint32_t*)fixupLoc);
795 uint32_t definitionCacheVmOffset = (uint32_t)(targetValue - regions[0].address);
796 uint32_t referenceCacheDataVmOffset = (uint32_t)(segCacheAddrsArray[dataSegIndex] + dataSegOffset - regions[1].address);
797 assert(referenceCacheDataVmOffset < (1<<30));
798 dyld3::launch_cache::binary_format::PatchOffset entry;
799 entry.last = false;
800 entry.hasAddend = (addend != 0);
801 entry.dataRegionOffset = referenceCacheDataVmOffset;
802 _patchTable[mh][definitionCacheVmOffset].insert(*((uint32_t*)&entry));
803 }
804 });
805 if ( bindingDiag.hasError() ) {
806 parsingDiag.error("%s in dylib %s", bindingDiag.errorMessage().c_str(), parser.installName());
807 }
808 if ( parsingDiag.hasError() )
809 break;
810 }
811
812 if ( log ) {
813 unsigned lazyCount = 0;
814 unsigned nonLazyCount = 0;
815 std::unordered_set<std::string> lazyTargets;
816 for (auto entry : useCounts) {
817 fprintf(stderr, "% 3ld % 3ld %s\n", entry.second.lazyCount, entry.second.nonLazyCount, entry.first.c_str());
818 lazyCount += entry.second.lazyCount;
819 nonLazyCount += entry.second.nonLazyCount;
820 if ( entry.second.lazyCount != 0 )
821 lazyTargets.insert(entry.first);
822 }
823 fprintf(stderr, "lazyCount = %d\n", lazyCount);
824 fprintf(stderr, "nonLazyCount = %d\n", nonLazyCount);
825 fprintf(stderr, "unique lazys = %ld\n", lazyTargets.size());
826 }
827
828 if ( parsingDiag.hasError() )
829 _diagnostics.error("%s", parsingDiag.errorMessage().c_str());
830 }
831
832
833 void CacheBuilder::recomputeCacheUUID(void)
834 {
835 // Clear existing UUID, then MD5 whole cache buffer.
836 uint8_t* uuidLoc = _buffer->header.uuid;
837 bzero(uuidLoc, 16);
838 CC_MD5(_buffer, (unsigned)_currentFileSize, uuidLoc);
839 // <rdar://problem/6723729> uuids should conform to RFC 4122 UUID version 4 & UUID version 5 formats
840 uuidLoc[6] = ( uuidLoc[6] & 0x0F ) | ( 3 << 4 );
841 uuidLoc[8] = ( uuidLoc[8] & 0x3F ) | 0x80;
842 }
843
844
845 CacheBuilder::SegmentMapping CacheBuilder::assignSegmentAddresses(const std::vector<DyldSharedCache::MappedMachO>& dylibs, dyld_cache_mapping_info regions[3])
846 {
847 // calculate size of header info and where first dylib's mach_header should start
848 size_t startOffset = sizeof(dyld_cache_header) + 3*sizeof(dyld_cache_mapping_info);
849 size_t maxPoolCount = 0;
850 if ( _archLayout->branchReach != 0 )
851 maxPoolCount = (_archLayout->sharedMemorySize / _archLayout->branchReach);
852 startOffset += maxPoolCount * sizeof(uint64_t);
853 startOffset += sizeof(dyld_cache_image_info) * dylibs.size();
854 startOffset += sizeof(dyld_cache_image_text_info) * dylibs.size();
855 for (const DyldSharedCache::MappedMachO& dylib : dylibs) {
856 dyld3::MachOParser parser(dylib.mh);
857 startOffset += (strlen(parser.installName()) + 1);
858 }
859 //fprintf(stderr, "%s total header size = 0x%08lX\n", _options.archName.c_str(), startOffset);
860 startOffset = align(startOffset, 12);
861
862 _branchPoolStarts.clear();
863 __block uint64_t addr = _archLayout->sharedMemoryStart;
864 __block SegmentMapping result;
865
866 // assign TEXT segment addresses
867 regions[0].address = addr;
868 regions[0].fileOffset = 0;
869 regions[0].initProt = VM_PROT_READ | VM_PROT_EXECUTE;
870 regions[0].maxProt = VM_PROT_READ | VM_PROT_EXECUTE;
871 addr += startOffset; // header
872
873 __block uint64_t lastPoolAddress = addr;
874 for (const DyldSharedCache::MappedMachO& dylib : dylibs) {
875 dyld3::MachOParser parser(dylib.mh, true);
876 parser.forEachSegment(^(const char* segName, uint32_t fileOffset, uint32_t fileSize, uint64_t vmAddr, uint64_t vmSize, uint8_t protections, uint32_t segIndex, uint64_t sizeOfSections, uint8_t p2align, bool& stop) {
877 if ( protections != (VM_PROT_READ | VM_PROT_EXECUTE) )
878 return;
879 // Insert branch island pools every 128MB for arm64
880 if ( (_archLayout->branchPoolTextSize != 0) && ((addr + vmSize - lastPoolAddress) > _archLayout->branchReach) ) {
881 _branchPoolStarts.push_back(addr);
882 _diagnostics.verbose("adding branch pool at 0x%llX\n", addr);
883 lastPoolAddress = addr;
884 addr += _archLayout->branchPoolTextSize;
885 }
886 // Keep __TEXT segments 4K or more aligned
887 addr = align(addr, std::max(p2align, (uint8_t)12));
888 SegmentMappingInfo info;
889 info.srcSegment = (uint8_t*)dylib.mh + fileOffset;
890 info.segName = segName;
891 info.dstCacheAddress = addr;
892 info.dstCacheOffset = (uint32_t)(addr - regions[0].address + regions[0].fileOffset);
893 info.dstCacheSegmentSize = (uint32_t)align(sizeOfSections, 12);
894 info.copySegmentSize = (uint32_t)align(sizeOfSections, 12);
895 info.srcSegmentIndex = segIndex;
896 result[dylib.mh].push_back(info);
897 addr += info.dstCacheSegmentSize;
898 });
899 }
900 // align TEXT region end
901 uint64_t endTextAddress = align(addr, _archLayout->sharedRegionAlignP2);
902 regions[0].size = endTextAddress - regions[0].address;
903
904 // assign __DATA* addresses
905 if ( _archLayout->sharedRegionsAreDiscontiguous )
906 addr = _archLayout->sharedMemoryStart + 0x60000000;
907 else
908 addr = align((addr + _archLayout->sharedRegionPadding), _archLayout->sharedRegionAlignP2);
909 regions[1].address = addr;
910 regions[1].fileOffset = regions[0].fileOffset + regions[0].size;
911 regions[1].initProt = VM_PROT_READ | VM_PROT_WRITE;
912 regions[1].maxProt = VM_PROT_READ | VM_PROT_WRITE;
913
914 // layout all __DATA_CONST segments
915 __block int dataConstSegmentCount = 0;
916 for (const DyldSharedCache::MappedMachO& dylib : dylibs) {
917 dyld3::MachOParser parser(dylib.mh, true);
918 parser.forEachSegment(^(const char* segName, uint32_t fileOffset, uint32_t fileSize, uint64_t vmAddr, uint64_t vmSize, uint8_t protections, uint32_t segIndex, uint64_t sizeOfSections, uint8_t p2align, bool& stop) {
919 if ( protections != (VM_PROT_READ | VM_PROT_WRITE) )
920 return;
921 if ( strcmp(segName, "__DATA_CONST") != 0 )
922 return;
923 ++dataConstSegmentCount;
924 // Pack __DATA_CONST segments
925 addr = align(addr, p2align);
926 size_t copySize = std::min((size_t)fileSize, (size_t)sizeOfSections);
927 SegmentMappingInfo info;
928 info.srcSegment = (uint8_t*)dylib.mh + fileOffset;
929 info.segName = segName;
930 info.dstCacheAddress = addr;
931 info.dstCacheOffset = (uint32_t)(addr - regions[1].address + regions[1].fileOffset);
932 info.dstCacheSegmentSize = (uint32_t)sizeOfSections;
933 info.copySegmentSize = (uint32_t)copySize;
934 info.srcSegmentIndex = segIndex;
935 result[dylib.mh].push_back(info);
936 addr += info.dstCacheSegmentSize;
937 });
938 }
939
940 // layout all __DATA segments (and other r/w non-dirty, non-const) segments
941 for (const DyldSharedCache::MappedMachO& dylib : dylibs) {
942 dyld3::MachOParser parser(dylib.mh, true);
943 parser.forEachSegment(^(const char* segName, uint32_t fileOffset, uint32_t fileSize, uint64_t vmAddr, uint64_t vmSize, uint8_t protections, uint32_t segIndex, uint64_t sizeOfSections, uint8_t p2align, bool& stop) {
944 if ( protections != (VM_PROT_READ | VM_PROT_WRITE) )
945 return;
946 if ( strcmp(segName, "__DATA_CONST") == 0 )
947 return;
948 if ( strcmp(segName, "__DATA_DIRTY") == 0 )
949 return;
950 if ( dataConstSegmentCount > 10 ) {
951 // Pack __DATA segments only if we also have __DATA_CONST segments
952 addr = align(addr, p2align);
953 }
954 else {
955 // Keep __DATA segments 4K or more aligned
956 addr = align(addr, std::max(p2align, (uint8_t)12));
957 }
958 size_t copySize = std::min((size_t)fileSize, (size_t)sizeOfSections);
959 SegmentMappingInfo info;
960 info.srcSegment = (uint8_t*)dylib.mh + fileOffset;
961 info.segName = segName;
962 info.dstCacheAddress = addr;
963 info.dstCacheOffset = (uint32_t)(addr - regions[1].address + regions[1].fileOffset);
964 info.dstCacheSegmentSize = (uint32_t)sizeOfSections;
965 info.copySegmentSize = (uint32_t)copySize;
966 info.srcSegmentIndex = segIndex;
967 result[dylib.mh].push_back(info);
968 addr += info.dstCacheSegmentSize;
969 });
970 }
971
972 // layout all __DATA_DIRTY segments, sorted
973 addr = align(addr, 12);
974 std::vector<DyldSharedCache::MappedMachO> dirtyDataDylibs = makeSortedDylibs(dylibs, _options.dirtyDataSegmentOrdering);
975 for (const DyldSharedCache::MappedMachO& dylib : dirtyDataDylibs) {
976 dyld3::MachOParser parser(dylib.mh, true);
977 parser.forEachSegment(^(const char* segName, uint32_t fileOffset, uint32_t fileSize, uint64_t vmAddr, uint64_t vmSize, uint8_t protections, uint32_t segIndex, uint64_t sizeOfSections, uint8_t p2align, bool& stop) {
978 if ( protections != (VM_PROT_READ | VM_PROT_WRITE) )
979 return;
980 if ( strcmp(segName, "__DATA_DIRTY") != 0 )
981 return;
982 // Pack __DATA_DIRTY segments
983 addr = align(addr, p2align);
984 size_t copySize = std::min((size_t)fileSize, (size_t)sizeOfSections);
985 SegmentMappingInfo info;
986 info.srcSegment = (uint8_t*)dylib.mh + fileOffset;
987 info.segName = segName;
988 info.dstCacheAddress = addr;
989 info.dstCacheOffset = (uint32_t)(addr - regions[1].address + regions[1].fileOffset);
990 info.dstCacheSegmentSize = (uint32_t)sizeOfSections;
991 info.copySegmentSize = (uint32_t)copySize;
992 info.srcSegmentIndex = segIndex;
993 result[dylib.mh].push_back(info);
994 addr += info.dstCacheSegmentSize;
995 });
996 }
997
998 // align DATA region end
999 uint64_t endDataAddress = align(addr, _archLayout->sharedRegionAlignP2);
1000 regions[1].size = endDataAddress - regions[1].address;
1001
1002 // start read-only region
1003 if ( _archLayout->sharedRegionsAreDiscontiguous )
1004 addr = _archLayout->sharedMemoryStart + 0xA0000000;
1005 else
1006 addr = align((addr + _archLayout->sharedRegionPadding), _archLayout->sharedRegionAlignP2);
1007 regions[2].address = addr;
1008 regions[2].fileOffset = regions[1].fileOffset + regions[1].size;
1009 regions[2].maxProt = VM_PROT_READ;
1010 regions[2].initProt = VM_PROT_READ;
1011
1012 // reserve space for kernel ASLR slide info at start of r/o region
1013 if ( _options.cacheSupportsASLR ) {
1014 _slideInfoBufferSizeAllocated = align((regions[1].size/4096) * 4, _archLayout->sharedRegionAlignP2); // only need 2 bytes per page
1015 _slideInfoFileOffset = regions[2].fileOffset;
1016 addr += _slideInfoBufferSizeAllocated;
1017 }
1018
1019 // layout all read-only (but not LINKEDIT) segments
1020 for (const DyldSharedCache::MappedMachO& dylib : dylibs) {
1021 dyld3::MachOParser parser(dylib.mh, true);
1022 parser.forEachSegment(^(const char* segName, uint32_t fileOffset, uint32_t fileSize, uint64_t vmAddr, uint64_t vmSize, uint8_t protections, uint32_t segIndex, uint64_t sizeOfSections, uint8_t p2align, bool& stop) {
1023 if ( protections != VM_PROT_READ )
1024 return;
1025 if ( strcmp(segName, "__LINKEDIT") == 0 )
1026 return;
1027 // Keep segments segments 4K or more aligned
1028 addr = align(addr, std::max(p2align, (uint8_t)12));
1029 SegmentMappingInfo info;
1030 info.srcSegment = (uint8_t*)dylib.mh + fileOffset;
1031 info.segName = segName;
1032 info.dstCacheAddress = addr;
1033 info.dstCacheOffset = (uint32_t)(addr - regions[2].address + regions[2].fileOffset);
1034 info.dstCacheSegmentSize = (uint32_t)align(sizeOfSections, 12);
1035 info.copySegmentSize = (uint32_t)sizeOfSections;
1036 info.srcSegmentIndex = segIndex;
1037 result[dylib.mh].push_back(info);
1038 addr += info.dstCacheSegmentSize;
1039 });
1040 }
1041 // layout all LINKEDIT segments (after other read-only segments)
1042 for (const DyldSharedCache::MappedMachO& dylib : dylibs) {
1043 dyld3::MachOParser parser(dylib.mh, true);
1044 parser.forEachSegment(^(const char* segName, uint32_t fileOffset, uint32_t fileSize, uint64_t vmAddr, uint64_t vmSize, uint8_t protections, uint32_t segIndex, uint64_t sizeOfSections, uint8_t p2align, bool& stop) {
1045 if ( protections != VM_PROT_READ )
1046 return;
1047 if ( strcmp(segName, "__LINKEDIT") != 0 )
1048 return;
1049 // Keep segments segments 4K or more aligned
1050 addr = align(addr, std::max(p2align, (uint8_t)12));
1051 SegmentMappingInfo info;
1052 info.srcSegment = (uint8_t*)dylib.mh + fileOffset;
1053 info.segName = segName;
1054 info.dstCacheAddress = addr;
1055 info.dstCacheOffset = (uint32_t)(addr - regions[2].address + regions[2].fileOffset);
1056 info.dstCacheSegmentSize = (uint32_t)align(sizeOfSections, 12);
1057 info.copySegmentSize = (uint32_t)align(fileSize, 12);
1058 info.srcSegmentIndex = segIndex;
1059 result[dylib.mh].push_back(info);
1060 addr += info.dstCacheSegmentSize;
1061 });
1062 }
1063 // add room for branch pool linkedits
1064 _branchPoolsLinkEditStartAddr = addr;
1065 addr += (_branchPoolStarts.size() * _archLayout->branchPoolLinkEditSize);
1066
1067 // align r/o region end
1068 uint64_t endReadOnlyAddress = align(addr, _archLayout->sharedRegionAlignP2);
1069 regions[2].size = endReadOnlyAddress - regions[2].address;
1070 _currentFileSize = regions[2].fileOffset + regions[2].size;
1071
1072 // FIXME: Confirm these numbers for all platform/arch combos
1073 // assume LINKEDIT optimzation reduces LINKEDITs to %40 of original size
1074 if ( _options.excludeLocalSymbols ) {
1075 _vmSize = regions[2].address + (regions[2].size * 2 / 5) - regions[0].address;
1076 }
1077 else {
1078 _vmSize = regions[2].address + (regions[2].size * 9 / 10) - regions[0].address;
1079 }
1080
1081 // sort SegmentMappingInfo for each image to be in the same order as original segments
1082 for (auto& entry : result) {
1083 std::vector<SegmentMappingInfo>& infos = entry.second;
1084 std::sort(infos.begin(), infos.end(), [&](const SegmentMappingInfo& a, const SegmentMappingInfo& b) {
1085 return a.srcSegmentIndex < b.srcSegmentIndex;
1086 });
1087 }
1088
1089 return result;
1090 }
1091
1092 uint64_t CacheBuilder::pathHash(const char* path)
1093 {
1094 uint64_t sum = 0;
1095 for (const char* s=path; *s != '\0'; ++s)
1096 sum += sum*4 + *s;
1097 return sum;
1098 }
1099
1100
1101 void CacheBuilder::findDylibAndSegment(const void* contentPtr, std::string& foundDylibName, std::string& foundSegName)
1102 {
1103 foundDylibName = "???";
1104 foundSegName = "???";
1105 uint32_t cacheOffset = (uint32_t)((uint8_t*)contentPtr - (uint8_t*)_buffer);
1106 _buffer->forEachImage(^(const mach_header* mh, const char* installName) {
1107 dyld3::MachOParser parser(mh, true);
1108 parser.forEachSegment(^(const char* segName, uint32_t fileOffset, uint32_t fileSize, uint64_t vmAddr, uint64_t vmSize, uint8_t protections, bool& stop) {
1109 if ( (cacheOffset > fileOffset) && (cacheOffset < (fileOffset+vmSize)) ) {
1110 foundDylibName = installName;
1111 foundSegName = segName;
1112 }
1113 });
1114 });
1115 }
1116
1117
1118 template <typename P>
1119 bool CacheBuilder::makeRebaseChain(uint8_t* pageContent, uint16_t lastLocationOffset, uint16_t offset, const dyld_cache_slide_info2* info)
1120 {
1121 typedef typename P::uint_t pint_t;
1122
1123 const pint_t deltaMask = (pint_t)(info->delta_mask);
1124 const pint_t valueMask = ~deltaMask;
1125 const pint_t valueAdd = (pint_t)(info->value_add);
1126 const unsigned deltaShift = __builtin_ctzll(deltaMask) - 2;
1127 const uint32_t maxDelta = (uint32_t)(deltaMask >> deltaShift);
1128
1129 pint_t* lastLoc = (pint_t*)&pageContent[lastLocationOffset+0];
1130 pint_t lastValue = (pint_t)P::getP(*lastLoc);
1131 if ( (lastValue - valueAdd) & deltaMask ) {
1132 std::string dylibName;
1133 std::string segName;
1134 findDylibAndSegment((void*)pageContent, dylibName, segName);
1135 _diagnostics.error("rebase pointer does not point within cache. lastOffset=0x%04X, seg=%s, dylib=%s\n",
1136 lastLocationOffset, segName.c_str(), dylibName.c_str());
1137 return false;
1138 }
1139 if ( offset <= (lastLocationOffset+maxDelta) ) {
1140 // previous location in range, make link from it
1141 // encode this location into last value
1142 pint_t delta = offset - lastLocationOffset;
1143 pint_t newLastValue = ((lastValue - valueAdd) & valueMask) | (delta << deltaShift);
1144 //warning(" add chain: delta = %d, lastOffset=0x%03X, offset=0x%03X, org value=0x%08lX, new value=0x%08lX",
1145 // offset - lastLocationOffset, lastLocationOffset, offset, (long)lastValue, (long)newLastValue);
1146 P::setP(*lastLoc, newLastValue);
1147 return true;
1148 }
1149 //warning(" too big delta = %d, lastOffset=0x%03X, offset=0x%03X", offset - lastLocationOffset, lastLocationOffset, offset);
1150
1151 // distance between rebase locations is too far
1152 // see if we can make a chain from non-rebase locations
1153 uint16_t nonRebaseLocationOffsets[1024];
1154 unsigned nrIndex = 0;
1155 for (uint16_t i = lastLocationOffset; i < offset-maxDelta; ) {
1156 nonRebaseLocationOffsets[nrIndex] = 0;
1157 for (int j=maxDelta; j > 0; j -= 4) {
1158 pint_t value = (pint_t)P::getP(*(pint_t*)&pageContent[i+j]);
1159 if ( value == 0 ) {
1160 // Steal values of 0 to be used in the rebase chain
1161 nonRebaseLocationOffsets[nrIndex] = i+j;
1162 break;
1163 }
1164 }
1165 if ( nonRebaseLocationOffsets[nrIndex] == 0 ) {
1166 lastValue = (pint_t)P::getP(*lastLoc);
1167 pint_t newValue = ((lastValue - valueAdd) & valueMask);
1168 //warning(" no way to make non-rebase delta chain, terminate off=0x%03X, old value=0x%08lX, new value=0x%08lX", lastLocationOffset, (long)value, (long)newValue);
1169 P::setP(*lastLoc, newValue);
1170 return false;
1171 }
1172 i = nonRebaseLocationOffsets[nrIndex];
1173 ++nrIndex;
1174 }
1175
1176 // we can make chain. go back and add each non-rebase location to chain
1177 uint16_t prevOffset = lastLocationOffset;
1178 pint_t* prevLoc = (pint_t*)&pageContent[prevOffset];
1179 for (int n=0; n < nrIndex; ++n) {
1180 uint16_t nOffset = nonRebaseLocationOffsets[n];
1181 assert(nOffset != 0);
1182 pint_t* nLoc = (pint_t*)&pageContent[nOffset];
1183 uint32_t delta2 = nOffset - prevOffset;
1184 pint_t value = (pint_t)P::getP(*prevLoc);
1185 pint_t newValue;
1186 if ( value == 0 )
1187 newValue = (delta2 << deltaShift);
1188 else
1189 newValue = ((value - valueAdd) & valueMask) | (delta2 << deltaShift);
1190 //warning(" non-rebase delta = %d, to off=0x%03X, old value=0x%08lX, new value=0x%08lX", delta2, nOffset, (long)value, (long)newValue);
1191 P::setP(*prevLoc, newValue);
1192 prevOffset = nOffset;
1193 prevLoc = nLoc;
1194 }
1195 uint32_t delta3 = offset - prevOffset;
1196 pint_t value = (pint_t)P::getP(*prevLoc);
1197 pint_t newValue;
1198 if ( value == 0 )
1199 newValue = (delta3 << deltaShift);
1200 else
1201 newValue = ((value - valueAdd) & valueMask) | (delta3 << deltaShift);
1202 //warning(" non-rebase delta = %d, to off=0x%03X, old value=0x%08lX, new value=0x%08lX", delta3, offset, (long)value, (long)newValue);
1203 P::setP(*prevLoc, newValue);
1204
1205 return true;
1206 }
1207
1208
1209 template <typename P>
1210 void CacheBuilder::addPageStarts(uint8_t* pageContent, const bool bitmap[], const dyld_cache_slide_info2* info,
1211 std::vector<uint16_t>& pageStarts, std::vector<uint16_t>& pageExtras)
1212 {
1213 typedef typename P::uint_t pint_t;
1214
1215 const pint_t deltaMask = (pint_t)(info->delta_mask);
1216 const pint_t valueMask = ~deltaMask;
1217 const uint32_t pageSize = info->page_size;
1218 const pint_t valueAdd = (pint_t)(info->value_add);
1219
1220 uint16_t startValue = DYLD_CACHE_SLIDE_PAGE_ATTR_NO_REBASE;
1221 uint16_t lastLocationOffset = 0xFFFF;
1222 for(int i=0; i < pageSize/4; ++i) {
1223 unsigned offset = i*4;
1224 if ( bitmap[i] ) {
1225 if ( startValue == DYLD_CACHE_SLIDE_PAGE_ATTR_NO_REBASE ) {
1226 // found first rebase location in page
1227 startValue = i;
1228 }
1229 else if ( !makeRebaseChain<P>(pageContent, lastLocationOffset, offset, info) ) {
1230 // can't record all rebasings in one chain
1231 if ( (startValue & DYLD_CACHE_SLIDE_PAGE_ATTR_EXTRA) == 0 ) {
1232 // switch page_start to "extras" which is a list of chain starts
1233 unsigned indexInExtras = (unsigned)pageExtras.size();
1234 if ( indexInExtras > 0x3FFF ) {
1235 _diagnostics.error("rebase overflow in page extras");
1236 return;
1237 }
1238 pageExtras.push_back(startValue);
1239 startValue = indexInExtras | DYLD_CACHE_SLIDE_PAGE_ATTR_EXTRA;
1240 }
1241 pageExtras.push_back(i);
1242 }
1243 lastLocationOffset = offset;
1244 }
1245 }
1246 if ( lastLocationOffset != 0xFFFF ) {
1247 // mark end of chain
1248 pint_t* lastLoc = (pint_t*)&pageContent[lastLocationOffset];
1249 pint_t lastValue = (pint_t)P::getP(*lastLoc);
1250 pint_t newValue = ((lastValue - valueAdd) & valueMask);
1251 P::setP(*lastLoc, newValue);
1252 }
1253 if ( startValue & DYLD_CACHE_SLIDE_PAGE_ATTR_EXTRA ) {
1254 // add end bit to extras
1255 pageExtras.back() |= DYLD_CACHE_SLIDE_PAGE_ATTR_END;
1256 }
1257 pageStarts.push_back(startValue);
1258 }
1259
1260 template <typename P>
1261 void CacheBuilder::writeSlideInfoV2()
1262 {
1263 typedef typename P::uint_t pint_t;
1264 typedef typename P::E E;
1265 const uint32_t pageSize = 4096;
1266
1267 // build one 1024/4096 bool bitmap per page (4KB/16KB) of DATA
1268 const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)_buffer + _buffer->header.mappingOffset);
1269 uint8_t* const dataStart = (uint8_t*)_buffer + mappings[1].fileOffset;
1270 uint8_t* const dataEnd = dataStart + mappings[1].size;
1271 unsigned pageCount = (unsigned)(mappings[1].size+pageSize-1)/pageSize;
1272 const long bitmapSize = pageCount*(pageSize/4)*sizeof(bool);
1273 bool* bitmap = (bool*)calloc(bitmapSize, 1);
1274 for (void* p : _pointersForASLR) {
1275 if ( (p < dataStart) || ( p > dataEnd) ) {
1276 _diagnostics.error("DATA pointer for sliding, out of range\n");
1277 free(bitmap);
1278 return;
1279 }
1280 long byteOffset = (long)((uint8_t*)p - dataStart);
1281 if ( (byteOffset % 4) != 0 ) {
1282 _diagnostics.error("pointer not 4-byte aligned in DATA offset 0x%08lX\n", byteOffset);
1283 free(bitmap);
1284 return;
1285 }
1286 long boolIndex = byteOffset / 4;
1287 // work around <rdar://24941083> by ignoring pointers to be slid that are NULL on disk
1288 if ( *((pint_t*)p) == 0 ) {
1289 std::string dylibName;
1290 std::string segName;
1291 findDylibAndSegment(p, dylibName, segName);
1292 _diagnostics.warning("NULL pointer asked to be slid in %s at DATA region offset 0x%04lX of %s", segName.c_str(), byteOffset, dylibName.c_str());
1293 continue;
1294 }
1295 bitmap[boolIndex] = true;
1296 }
1297
1298 // fill in fixed info
1299 assert(_slideInfoFileOffset != 0);
1300 dyld_cache_slide_info2* info = (dyld_cache_slide_info2*)((uint8_t*)_buffer + _slideInfoFileOffset);
1301 info->version = 2;
1302 info->page_size = pageSize;
1303 info->delta_mask = _archLayout->pointerDeltaMask;
1304 info->value_add = (sizeof(pint_t) == 8) ? 0 : _archLayout->sharedMemoryStart; // only value_add for 32-bit archs
1305
1306 // set page starts and extras for each page
1307 std::vector<uint16_t> pageStarts;
1308 std::vector<uint16_t> pageExtras;
1309 pageStarts.reserve(pageCount);
1310 uint8_t* pageContent = dataStart;;
1311 const bool* bitmapForPage = bitmap;
1312 for (unsigned i=0; i < pageCount; ++i) {
1313 //warning("page[%d]", i);
1314 addPageStarts<P>(pageContent, bitmapForPage, info, pageStarts, pageExtras);
1315 if ( _diagnostics.hasError() ) {
1316 free(bitmap);
1317 return;
1318 }
1319 pageContent += pageSize;
1320 bitmapForPage += (sizeof(bool)*(pageSize/4));
1321 }
1322 free((void*)bitmap);
1323
1324 // fill in computed info
1325 info->page_starts_offset = sizeof(dyld_cache_slide_info2);
1326 info->page_starts_count = (unsigned)pageStarts.size();
1327 info->page_extras_offset = (unsigned)(sizeof(dyld_cache_slide_info2)+pageStarts.size()*sizeof(uint16_t));
1328 info->page_extras_count = (unsigned)pageExtras.size();
1329 uint16_t* pageStartsBuffer = (uint16_t*)((char*)info + info->page_starts_offset);
1330 uint16_t* pageExtrasBuffer = (uint16_t*)((char*)info + info->page_extras_offset);
1331 for (unsigned i=0; i < pageStarts.size(); ++i)
1332 pageStartsBuffer[i] = pageStarts[i];
1333 for (unsigned i=0; i < pageExtras.size(); ++i)
1334 pageExtrasBuffer[i] = pageExtras[i];
1335 // update header with final size
1336 _buffer->header.slideInfoSize = align(info->page_extras_offset + pageExtras.size()*sizeof(uint16_t), _archLayout->sharedRegionAlignP2);
1337 if ( _buffer->header.slideInfoSize > _slideInfoBufferSizeAllocated ) {
1338 _diagnostics.error("kernel slide info overflow buffer");
1339 }
1340 //warning("pageCount=%u, page_starts_count=%lu, page_extras_count=%lu", pageCount, pageStarts.size(), pageExtras.size());
1341 }
1342
1343
1344 /*
1345 void CacheBuilder::writeSlideInfoV1()
1346 {
1347 // build one 128-byte bitmap per page (4096) of DATA
1348 uint8_t* const dataStart = (uint8_t*)_buffer.get() + regions[1].fileOffset;
1349 uint8_t* const dataEnd = dataStart + regions[1].size;
1350 const long bitmapSize = (dataEnd - dataStart)/(4*8);
1351 uint8_t* bitmap = (uint8_t*)calloc(bitmapSize, 1);
1352 for (void* p : _pointersForASLR) {
1353 if ( (p < dataStart) || ( p > dataEnd) )
1354 terminate("DATA pointer for sliding, out of range\n");
1355 long offset = (long)((uint8_t*)p - dataStart);
1356 if ( (offset % 4) != 0 )
1357 terminate("pointer not 4-byte aligned in DATA offset 0x%08lX\n", offset);
1358 long byteIndex = offset / (4*8);
1359 long bitInByte = (offset % 32) >> 2;
1360 bitmap[byteIndex] |= (1 << bitInByte);
1361 }
1362
1363 // allocate worst case size block of all slide info
1364 const unsigned entry_size = 4096/(8*4); // 8 bits per byte, possible pointer every 4 bytes.
1365 const unsigned toc_count = (unsigned)bitmapSize/entry_size;
1366 dyld_cache_slide_info* slideInfo = (dyld_cache_slide_info*)((uint8_t*)_buffer + _slideInfoFileOffset);
1367 slideInfo->version = 1;
1368 slideInfo->toc_offset = sizeof(dyld_cache_slide_info);
1369 slideInfo->toc_count = toc_count;
1370 slideInfo->entries_offset = (slideInfo->toc_offset+2*toc_count+127)&(-128);
1371 slideInfo->entries_count = 0;
1372 slideInfo->entries_size = entry_size;
1373 // append each unique entry
1374 const dyldCacheSlideInfoEntry* bitmapAsEntries = (dyldCacheSlideInfoEntry*)bitmap;
1375 dyldCacheSlideInfoEntry* const entriesInSlidInfo = (dyldCacheSlideInfoEntry*)((char*)slideInfo+slideInfo->entries_offset());
1376 int entry_count = 0;
1377 for (int i=0; i < toc_count; ++i) {
1378 const dyldCacheSlideInfoEntry* thisEntry = &bitmapAsEntries[i];
1379 // see if it is same as one already added
1380 bool found = false;
1381 for (int j=0; j < entry_count; ++j) {
1382 if ( memcmp(thisEntry, &entriesInSlidInfo[j], entry_size) == 0 ) {
1383 slideInfo->set_toc(i, j);
1384 found = true;
1385 break;
1386 }
1387 }
1388 if ( !found ) {
1389 // append to end
1390 memcpy(&entriesInSlidInfo[entry_count], thisEntry, entry_size);
1391 slideInfo->set_toc(i, entry_count++);
1392 }
1393 }
1394 slideInfo->entries_count = entry_count;
1395 ::free((void*)bitmap);
1396
1397 _buffer.header->slideInfoSize = align(slideInfo->entries_offset + entry_count*entry_size, _archLayout->sharedRegionAlignP2);
1398 }
1399
1400 */
1401
1402 void CacheBuilder::fipsSign() {
1403 __block bool found = false;
1404 _buffer->forEachImage(^(const mach_header* mh, const char* installName) {
1405 __block void *hash_location = nullptr;
1406 // Return if this is not corecrypto
1407 if (strcmp(installName, "/usr/lib/system/libcorecrypto.dylib") != 0) {
1408 return;
1409 }
1410 found = true;
1411 auto parser = dyld3::MachOParser(mh, true);
1412 parser.forEachLocalSymbol(_diagnostics, ^(const char *symbolName, uint64_t n_value, uint8_t n_type, uint8_t n_sect, uint16_t n_desc, bool &stop) {
1413 if (strcmp(symbolName, "_fipspost_precalc_hmac") != 0)
1414 return;
1415 hash_location = (void *)(n_value - _archLayout->sharedMemoryStart + (uintptr_t)_buffer);
1416 stop = true;
1417 });
1418
1419 // Bail out if we did not find the symbol
1420 if (hash_location == nullptr) {
1421 _diagnostics.warning("Could not find _fipspost_precalc_hmac, skipping FIPS sealing");
1422 return;
1423 }
1424
1425 parser.forEachSection(^(const char *segName, const char *sectionName, uint32_t flags, const void *content, size_t size, bool illegalSectionSize, bool &stop) {
1426 // FIXME: If we ever implement userspace __TEXT_EXEC this will need to be updated
1427 if ( (strcmp(segName, "__TEXT" ) != 0) || (strcmp(sectionName, "__text") != 0) ) {
1428 return;
1429 }
1430
1431 if (illegalSectionSize) {
1432 _diagnostics.error("FIPS section %s/%s extends beyond the end of the segment", segName, sectionName);
1433 return;
1434 }
1435
1436 //We have _fipspost_precalc_hmac and __TEXT,__text, seal it
1437 unsigned char hmac_key = 0;
1438 CCHmac(kCCHmacAlgSHA256, &hmac_key, 1, content, size, hash_location);
1439 stop = true;
1440 });
1441 });
1442
1443 if (!found) {
1444 _diagnostics.warning("Could not find /usr/lib/system/libcorecrypto.dylib, skipping FIPS sealing");
1445 }
1446 }
1447
1448 void CacheBuilder::codeSign()
1449 {
1450 uint8_t dscHashType;
1451 uint8_t dscHashSize;
1452 uint32_t dscDigestFormat;
1453 bool agile = false;
1454
1455 // select which codesigning hash
1456 switch (_options.codeSigningDigestMode) {
1457 case DyldSharedCache::Agile:
1458 agile = true;
1459 // Fall through to SHA1, because the main code directory remains SHA1 for compatibility.
1460 case DyldSharedCache::SHA1only:
1461 dscHashType = CS_HASHTYPE_SHA1;
1462 dscHashSize = CS_HASH_SIZE_SHA1;
1463 dscDigestFormat = kCCDigestSHA1;
1464 break;
1465 case DyldSharedCache::SHA256only:
1466 dscHashType = CS_HASHTYPE_SHA256;
1467 dscHashSize = CS_HASH_SIZE_SHA256;
1468 dscDigestFormat = kCCDigestSHA256;
1469 break;
1470 default:
1471 _diagnostics.error("codeSigningDigestMode has unknown, unexpected value %d, bailing out.",
1472 _options.codeSigningDigestMode);
1473 return;
1474 }
1475
1476 std::string cacheIdentifier = "com.apple.dyld.cache." + _options.archName;
1477 if ( _options.dylibsRemovedDuringMastering ) {
1478 if ( _options.optimizeStubs )
1479 cacheIdentifier = "com.apple.dyld.cache." + _options.archName + ".release";
1480 else
1481 cacheIdentifier = "com.apple.dyld.cache." + _options.archName + ".development";
1482 }
1483 // get pointers into shared cache buffer
1484 size_t inBbufferSize = _currentFileSize;
1485 const uint8_t* inBuffer = (uint8_t*)_buffer;
1486 uint8_t* csBuffer = (uint8_t*)_buffer+inBbufferSize;
1487
1488 // layout code signature contents
1489 uint32_t blobCount = agile ? 4 : 3;
1490 size_t idSize = cacheIdentifier.size()+1; // +1 for terminating 0
1491 uint32_t slotCount = (uint32_t)((inBbufferSize + CS_PAGE_SIZE - 1) / CS_PAGE_SIZE);
1492 uint32_t xSlotCount = CSSLOT_REQUIREMENTS;
1493 size_t idOffset = offsetof(CS_CodeDirectory, end_withExecSeg);
1494 size_t hashOffset = idOffset+idSize + dscHashSize*xSlotCount;
1495 size_t hash256Offset = idOffset+idSize + CS_HASH_SIZE_SHA256*xSlotCount;
1496 size_t cdSize = hashOffset + (slotCount * dscHashSize);
1497 size_t cd256Size = agile ? hash256Offset + (slotCount * CS_HASH_SIZE_SHA256) : 0;
1498 size_t reqsSize = 12;
1499 size_t cmsSize = sizeof(CS_Blob);
1500 size_t cdOffset = sizeof(CS_SuperBlob) + blobCount*sizeof(CS_BlobIndex);
1501 size_t cd256Offset = cdOffset + cdSize;
1502 size_t reqsOffset = cd256Offset + cd256Size; // equals cdOffset + cdSize if not agile
1503 size_t cmsOffset = reqsOffset + reqsSize;
1504 size_t sbSize = cmsOffset + cmsSize;
1505 size_t sigSize = align(sbSize, 14); // keep whole cache 16KB aligned
1506
1507 if ( _currentFileSize+sigSize > _allocatedBufferSize ) {
1508 _diagnostics.error("cache buffer too small to hold code signature (buffer size=%lldMB, signature size=%ldMB, free space=%lldMB)",
1509 _allocatedBufferSize/1024/1024, sigSize/1024/1024, (_allocatedBufferSize-_currentFileSize)/1024/1024);
1510 return;
1511 }
1512
1513 // create overall code signature which is a superblob
1514 CS_SuperBlob* sb = reinterpret_cast<CS_SuperBlob*>(csBuffer);
1515 sb->magic = htonl(CSMAGIC_EMBEDDED_SIGNATURE);
1516 sb->length = htonl(sbSize);
1517 sb->count = htonl(blobCount);
1518 sb->index[0].type = htonl(CSSLOT_CODEDIRECTORY);
1519 sb->index[0].offset = htonl(cdOffset);
1520 sb->index[1].type = htonl(CSSLOT_REQUIREMENTS);
1521 sb->index[1].offset = htonl(reqsOffset);
1522 sb->index[2].type = htonl(CSSLOT_CMS_SIGNATURE);
1523 sb->index[2].offset = htonl(cmsOffset);
1524 if ( agile ) {
1525 sb->index[3].type = htonl(CSSLOT_ALTERNATE_CODEDIRECTORIES + 0);
1526 sb->index[3].offset = htonl(cd256Offset);
1527 }
1528
1529 // fill in empty requirements
1530 CS_RequirementsBlob* reqs = (CS_RequirementsBlob*)(((char*)sb)+reqsOffset);
1531 reqs->magic = htonl(CSMAGIC_REQUIREMENTS);
1532 reqs->length = htonl(sizeof(CS_RequirementsBlob));
1533 reqs->data = 0;
1534
1535 // initialize fixed fields of Code Directory
1536 CS_CodeDirectory* cd = (CS_CodeDirectory*)(((char*)sb)+cdOffset);
1537 cd->magic = htonl(CSMAGIC_CODEDIRECTORY);
1538 cd->length = htonl(cdSize);
1539 cd->version = htonl(0x20400); // supports exec segment
1540 cd->flags = htonl(kSecCodeSignatureAdhoc);
1541 cd->hashOffset = htonl(hashOffset);
1542 cd->identOffset = htonl(idOffset);
1543 cd->nSpecialSlots = htonl(xSlotCount);
1544 cd->nCodeSlots = htonl(slotCount);
1545 cd->codeLimit = htonl(inBbufferSize);
1546 cd->hashSize = dscHashSize;
1547 cd->hashType = dscHashType;
1548 cd->platform = 0; // not platform binary
1549 cd->pageSize = __builtin_ctz(CS_PAGE_SIZE); // log2(CS_PAGE_SIZE);
1550 cd->spare2 = 0; // unused (must be zero)
1551 cd->scatterOffset = 0; // not supported anymore
1552 cd->teamOffset = 0; // no team ID
1553 cd->spare3 = 0; // unused (must be zero)
1554 cd->codeLimit64 = 0; // falls back to codeLimit
1555
1556 // executable segment info
1557 const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)_buffer + _buffer->header.mappingOffset);
1558 cd->execSegBase = htonll(mappings[0].fileOffset); // base of TEXT segment
1559 cd->execSegLimit = htonll(mappings[0].size); // size of TEXT segment
1560 cd->execSegFlags = 0; // not a main binary
1561
1562 // initialize dynamic fields of Code Directory
1563 strcpy((char*)cd + idOffset, cacheIdentifier.c_str());
1564
1565 // add special slot hashes
1566 uint8_t* hashSlot = (uint8_t*)cd + hashOffset;
1567 uint8_t* reqsHashSlot = &hashSlot[-CSSLOT_REQUIREMENTS*dscHashSize];
1568 CCDigest(dscDigestFormat, (uint8_t*)reqs, sizeof(CS_RequirementsBlob), reqsHashSlot);
1569
1570 CS_CodeDirectory* cd256;
1571 uint8_t* hash256Slot;
1572 uint8_t* reqsHash256Slot;
1573 if ( agile ) {
1574 // Note that the assumption here is that the size up to the hashes is the same as for
1575 // sha1 code directory, and that they come last, after everything else.
1576
1577 cd256 = (CS_CodeDirectory*)(((char*)sb)+cd256Offset);
1578 cd256->magic = htonl(CSMAGIC_CODEDIRECTORY);
1579 cd256->length = htonl(cd256Size);
1580 cd256->version = htonl(0x20400); // supports exec segment
1581 cd256->flags = htonl(kSecCodeSignatureAdhoc);
1582 cd256->hashOffset = htonl(hash256Offset);
1583 cd256->identOffset = htonl(idOffset);
1584 cd256->nSpecialSlots = htonl(xSlotCount);
1585 cd256->nCodeSlots = htonl(slotCount);
1586 cd256->codeLimit = htonl(inBbufferSize);
1587 cd256->hashSize = CS_HASH_SIZE_SHA256;
1588 cd256->hashType = CS_HASHTYPE_SHA256;
1589 cd256->platform = 0; // not platform binary
1590 cd256->pageSize = __builtin_ctz(CS_PAGE_SIZE); // log2(CS_PAGE_SIZE);
1591 cd256->spare2 = 0; // unused (must be zero)
1592 cd256->scatterOffset = 0; // not supported anymore
1593 cd256->teamOffset = 0; // no team ID
1594 cd256->spare3 = 0; // unused (must be zero)
1595 cd256->codeLimit64 = 0; // falls back to codeLimit
1596
1597 // executable segment info
1598 cd256->execSegBase = cd->execSegBase;
1599 cd256->execSegLimit = cd->execSegLimit;
1600 cd256->execSegFlags = cd->execSegFlags;
1601
1602 // initialize dynamic fields of Code Directory
1603 strcpy((char*)cd256 + idOffset, cacheIdentifier.c_str());
1604
1605 // add special slot hashes
1606 hash256Slot = (uint8_t*)cd256 + hash256Offset;
1607 reqsHash256Slot = &hash256Slot[-CSSLOT_REQUIREMENTS*CS_HASH_SIZE_SHA256];
1608 CCDigest(kCCDigestSHA256, (uint8_t*)reqs, sizeof(CS_RequirementsBlob), reqsHash256Slot);
1609 }
1610 else {
1611 cd256 = NULL;
1612 hash256Slot = NULL;
1613 reqsHash256Slot = NULL;
1614 }
1615
1616 // fill in empty CMS blob for ad-hoc signing
1617 CS_Blob* cms = (CS_Blob*)(((char*)sb)+cmsOffset);
1618 cms->magic = htonl(CSMAGIC_BLOBWRAPPER);
1619 cms->length = htonl(sizeof(CS_Blob));
1620
1621 // alter header of cache to record size and location of code signature
1622 // do this *before* hashing each page
1623 _buffer->header.codeSignatureOffset = inBbufferSize;
1624 _buffer->header.codeSignatureSize = sigSize;
1625
1626 // compute hashes
1627 const uint8_t* code = inBuffer;
1628 for (uint32_t i=0; i < slotCount; ++i) {
1629 CCDigest(dscDigestFormat, code, CS_PAGE_SIZE, hashSlot);
1630 hashSlot += dscHashSize;
1631
1632 if ( agile ) {
1633 CCDigest(kCCDigestSHA256, code, CS_PAGE_SIZE, hash256Slot);
1634 hash256Slot += CS_HASH_SIZE_SHA256;
1635 }
1636 code += CS_PAGE_SIZE;
1637 }
1638
1639 // hash of entire code directory (cdHash) uses same hash as each page
1640 uint8_t fullCdHash[dscHashSize];
1641 CCDigest(dscDigestFormat, (const uint8_t*)cd, cdSize, fullCdHash);
1642 // Note: cdHash is defined as first 20 bytes of hash
1643 memcpy(_cdHashFirst, fullCdHash, 20);
1644 if ( agile ) {
1645 uint8_t fullCdHash256[CS_HASH_SIZE_SHA256];
1646 CCDigest(kCCDigestSHA256, (const uint8_t*)cd256, cd256Size, fullCdHash256);
1647 // Note: cdHash is defined as first 20 bytes of hash, even for sha256
1648 memcpy(_cdHashSecond, fullCdHash256, 20);
1649 }
1650 else {
1651 memset(_cdHashSecond, 0, 20);
1652 }
1653
1654 // increase file size to include newly append code signature
1655 _currentFileSize += sigSize;
1656 }
1657
1658 const bool CacheBuilder::agileSignature()
1659 {
1660 return _options.codeSigningDigestMode == DyldSharedCache::Agile;
1661 }
1662
1663 static const std::string cdHash(uint8_t hash[20])
1664 {
1665 char buff[48];
1666 for (int i = 0; i < 20; ++i)
1667 sprintf(&buff[2*i], "%2.2x", hash[i]);
1668 return buff;
1669 }
1670
1671 const std::string CacheBuilder::cdHashFirst()
1672 {
1673 return cdHash(_cdHashFirst);
1674 }
1675
1676 const std::string CacheBuilder::cdHashSecond()
1677 {
1678 return cdHash(_cdHashSecond);
1679 }
1680
1681 void CacheBuilder::addCachedDylibsImageGroup(dyld3::ImageProxyGroup* dylibGroup)
1682 {
1683 const dyld3::launch_cache::binary_format::ImageGroup* groupBinary = dylibGroup->makeImageGroupBinary(_diagnostics, _s_neverStubEliminate);
1684 if (!groupBinary)
1685 return;
1686
1687 dyld3::launch_cache::ImageGroup group(groupBinary);
1688 size_t groupSize = group.size();
1689
1690 if ( _currentFileSize+groupSize > _allocatedBufferSize ) {
1691 _diagnostics.error("cache buffer too small to hold group[0] info (buffer size=%lldMB, group size=%ldMB, free space=%lldMB)",
1692 _allocatedBufferSize/1024/1024, groupSize/1024/1024, (_allocatedBufferSize-_currentFileSize)/1024/1024);
1693 return;
1694 }
1695
1696 // append ImageGroup data to read-only region of cache
1697 uint8_t* loc = (uint8_t*)_buffer + _currentFileSize;
1698 memcpy(loc, groupBinary, groupSize);
1699 dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)_buffer + _buffer->header.mappingOffset);
1700 _buffer->header.dylibsImageGroupAddr = mappings[2].address + (_currentFileSize - mappings[2].fileOffset);
1701 _buffer->header.dylibsImageGroupSize = (uint32_t)groupSize;
1702 _currentFileSize += groupSize;
1703 free((void*)groupBinary);
1704 }
1705
1706
1707 void CacheBuilder::addCachedOtherDylibsImageGroup(dyld3::ImageProxyGroup* otherGroup)
1708 {
1709 const dyld3::launch_cache::binary_format::ImageGroup* groupBinary = otherGroup->makeImageGroupBinary(_diagnostics);
1710 if (!groupBinary)
1711 return;
1712
1713 dyld3::launch_cache::ImageGroup group(groupBinary);
1714 size_t groupSize = group.size();
1715
1716 if ( _currentFileSize+groupSize > _allocatedBufferSize ) {
1717 _diagnostics.error("cache buffer too small to hold group[1] info (buffer size=%lldMB, group size=%ldMB, free space=%lldMB)",
1718 _allocatedBufferSize/1024/1024, groupSize/1024/1024, (_allocatedBufferSize-_currentFileSize)/1024/1024);
1719 return;
1720 }
1721
1722 // append ImageGroup data to read-only region of cache
1723 uint8_t* loc = (uint8_t*)_buffer + _currentFileSize;
1724 memcpy(loc, groupBinary, groupSize);
1725 dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)_buffer + _buffer->header.mappingOffset);
1726 _buffer->header.otherImageGroupAddr = mappings[2].address + (_currentFileSize - mappings[2].fileOffset);
1727 _buffer->header.otherImageGroupSize = (uint32_t)groupSize;
1728 _currentFileSize += groupSize;
1729 free((void*)groupBinary);
1730 }
1731
1732 void CacheBuilder::addClosures(const std::map<std::string, const dyld3::launch_cache::binary_format::Closure*>& closures)
1733 {
1734 // preflight space needed
1735 size_t closuresSpace = 0;
1736 for (const auto& entry : closures) {
1737 dyld3::launch_cache::Closure closure(entry.second);
1738 closuresSpace += closure.size();
1739 }
1740 size_t freeSpace = _allocatedBufferSize - _currentFileSize;
1741 if ( closuresSpace > freeSpace ) {
1742 _diagnostics.error("cache buffer too small to hold all closures (buffer size=%lldMB, closures size=%ldMB, free space=%ldMB)",
1743 _allocatedBufferSize/1024/1024, closuresSpace/1024/1024, freeSpace/1024/1024);
1744 return;
1745 }
1746
1747 dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)_buffer + _buffer->header.mappingOffset);
1748 _buffer->header.progClosuresAddr = mappings[2].address + (_currentFileSize - mappings[2].fileOffset);
1749 uint8_t* closuresBase = (uint8_t*)_buffer + _currentFileSize;
1750 std::vector<DylibIndexTrie::Entry> closureEntrys;
1751 uint32_t currentClosureOffset = 0;
1752 for (const auto& entry : closures) {
1753 const dyld3::launch_cache::binary_format::Closure* closBuf = entry.second;
1754 closureEntrys.push_back(DylibIndexTrie::Entry(entry.first, DylibIndex(currentClosureOffset)));
1755 dyld3::launch_cache::Closure closure(closBuf);
1756 size_t size = closure.size();
1757 assert((size % 4) == 0);
1758 memcpy(closuresBase+currentClosureOffset, closBuf, size);
1759 currentClosureOffset += size;
1760 freeSpace -= size;
1761 free((void*)closBuf);
1762 }
1763 _buffer->header.progClosuresSize = currentClosureOffset;
1764 _currentFileSize += currentClosureOffset;
1765 freeSpace = _allocatedBufferSize - _currentFileSize;
1766
1767 // build trie of indexes into closures list
1768 DylibIndexTrie closureTrie(closureEntrys);
1769 std::vector<uint8_t> trieBytes;
1770 closureTrie.emit(trieBytes);
1771 while ( (trieBytes.size() % 8) != 0 )
1772 trieBytes.push_back(0);
1773 if ( trieBytes.size() > freeSpace ) {
1774 _diagnostics.error("cache buffer too small to hold all closures trie (buffer size=%lldMB, trie size=%ldMB, free space=%ldMB)",
1775 _allocatedBufferSize/1024/1024, trieBytes.size()/1024/1024, freeSpace/1024/1024);
1776 return;
1777 }
1778 memcpy((uint8_t*)_buffer + _currentFileSize, &trieBytes[0], trieBytes.size());
1779 _buffer->header.progClosuresTrieAddr = mappings[2].address + (_currentFileSize - mappings[2].fileOffset);
1780 _buffer->header.progClosuresTrieSize = trieBytes.size();
1781 _currentFileSize += trieBytes.size();
1782 }
1783
1784