1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2006-2015 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
24 #ifndef __DYLD_CACHE_FORMAT__
25 #define __DYLD_CACHE_FORMAT__
28 #include <uuid/uuid.h>
31 struct dyld_cache_header
33 char magic
[16]; // e.g. "dyld_v0 i386"
34 uint32_t mappingOffset
; // file offset to first dyld_cache_mapping_info
35 uint32_t mappingCount
; // number of dyld_cache_mapping_info entries
36 uint32_t imagesOffset
; // file offset to first dyld_cache_image_info
37 uint32_t imagesCount
; // number of dyld_cache_image_info entries
38 uint64_t dyldBaseAddress
; // base address of dyld when cache was built
39 uint64_t codeSignatureOffset
; // file offset of code signature blob
40 uint64_t codeSignatureSize
; // size of code signature blob (zero means to end of file)
41 uint64_t slideInfoOffset
; // file offset of kernel slid info
42 uint64_t slideInfoSize
; // size of kernel slid info
43 uint64_t localSymbolsOffset
; // file offset of where local symbols are stored
44 uint64_t localSymbolsSize
; // size of local symbols information
45 uint8_t uuid
[16]; // unique value for each shared cache file
46 uint64_t cacheType
; // 0 for development, 1 for production
47 uint32_t branchPoolsOffset
; // file offset to table of uint64_t pool addresses
48 uint32_t branchPoolsCount
; // number of uint64_t entries
49 uint64_t accelerateInfoAddr
; // (unslid) address of optimization info
50 uint64_t accelerateInfoSize
; // size of optimization info
51 uint64_t imagesTextOffset
; // file offset to first dyld_cache_image_text_info
52 uint64_t imagesTextCount
; // number of dyld_cache_image_text_info entries
53 uint64_t dylibsImageGroupAddr
; // (unslid) address of ImageGroup for dylibs in this cache
54 uint64_t dylibsImageGroupSize
; // size of ImageGroup for dylibs in this cache
55 uint64_t otherImageGroupAddr
; // (unslid) address of ImageGroup for other OS dylibs
56 uint64_t otherImageGroupSize
; // size of oImageGroup for other OS dylibs
57 uint64_t progClosuresAddr
; // (unslid) address of list of program launch closures
58 uint64_t progClosuresSize
; // size of list of program launch closures
59 uint64_t progClosuresTrieAddr
; // (unslid) address of trie of indexes into program launch closures
60 uint64_t progClosuresTrieSize
; // size of trie of indexes into program launch closures
61 uint32_t platform
; // platform number (macOS=1, etc)
62 uint32_t formatVersion
: 8, // launch_cache::binary_format::kFormatVersion
63 dylibsExpectedOnDisk
: 1, // dyld should expect the dylib exists on disk and to compare inode/mtime to see if cache is valid
64 simulator
: 1; // for simulator of specified platform
65 uint64_t sharedRegionStart
; // base load address of cache if not slid
66 uint64_t sharedRegionSize
; // overall size of region cache can be mapped into
67 uint64_t maxSlide
; // runtime slide of cache can be between zero and this value
71 struct dyld_cache_mapping_info
{
79 struct dyld_cache_image_info
84 uint32_t pathFileOffset
;
88 struct dyld_cache_image_info_extra
90 uint64_t exportsTrieAddr
; // address of trie in unslid cache
91 uint64_t weakBindingsAddr
;
92 uint32_t exportsTrieSize
;
93 uint32_t weakBindingsSize
;
94 uint32_t dependentsStartArrayIndex
;
95 uint32_t reExportsStartArrayIndex
;
99 struct dyld_cache_accelerator_info
101 uint32_t version
; // currently 1
102 uint32_t imageExtrasCount
; // does not include aliases
103 uint32_t imagesExtrasOffset
; // offset into this chunk of first dyld_cache_image_info_extra
104 uint32_t bottomUpListOffset
; // offset into this chunk to start of 16-bit array of sorted image indexes
105 uint32_t dylibTrieOffset
; // offset into this chunk to start of trie containing all dylib paths
106 uint32_t dylibTrieSize
; // size of trie containing all dylib paths
107 uint32_t initializersOffset
; // offset into this chunk to start of initializers list
108 uint32_t initializersCount
; // size of initializers list
109 uint32_t dofSectionsOffset
; // offset into this chunk to start of DOF sections list
110 uint32_t dofSectionsCount
; // size of initializers list
111 uint32_t reExportListOffset
; // offset into this chunk to start of 16-bit array of re-exports
112 uint32_t reExportCount
; // size of re-exports
113 uint32_t depListOffset
; // offset into this chunk to start of 16-bit array of dependencies (0x8000 bit set if upward)
114 uint32_t depListCount
; // size of dependencies
115 uint32_t rangeTableOffset
; // offset into this chunk to start of ss
116 uint32_t rangeTableCount
; // size of dependencies
117 uint64_t dyldSectionAddr
; // address of libdyld's __dyld section in unslid cache
120 struct dyld_cache_accelerator_initializer
122 uint32_t functionOffset
; // address offset from start of cache mapping
126 struct dyld_cache_range_entry
128 uint64_t startAddress
; // unslid address of start of region
133 struct dyld_cache_accelerator_dof
135 uint64_t sectionAddress
; // unslid address of start of region
136 uint32_t sectionSize
;
140 struct dyld_cache_image_text_info
143 uint64_t loadAddress
; // unslid address of start of __TEXT
144 uint32_t textSegmentSize
;
145 uint32_t pathOffset
; // offset from start of cache file
149 // The rebasing info is to allow the kernel to lazily rebase DATA pages of the
150 // dyld shared cache. Rebasing is adding the slide to interior pointers.
151 struct dyld_cache_slide_info
153 uint32_t version
; // currently 1
156 uint32_t entries_offset
;
157 uint32_t entries_count
;
158 uint32_t entries_size
; // currently 128
159 // uint16_t toc[toc_count];
160 // entrybitmap entries[entries_count];
164 // The version 2 of the slide info uses a different compression scheme. Since
165 // only interior pointers (pointers that point within the cache) are rebased
166 // (slid), we know the possible range of the pointers and thus know there are
167 // unused bits in each pointer. We use those bits to form a linked list of
168 // locations needing rebasing in each page.
172 // pageIndex = (pageAddress - startOfAllDataAddress)/info->page_size
173 // pageStarts[] = info + info->page_starts_offset
174 // pageExtras[] = info + info->page_extras_offset
175 // valueMask = ~(info->delta_mask)
176 // deltaShift = __builtin_ctzll(info->delta_mask) - 2
178 // There are three cases:
180 // 1) pageStarts[pageIndex] == DYLD_CACHE_SLIDE_PAGE_ATTR_NO_REBASE
181 // The page contains no values that need rebasing.
183 // 2) (pageStarts[pageIndex] & DYLD_CACHE_SLIDE_PAGE_ATTR_EXTRA) == 0
184 // All rebase locations are in one linked list. The offset of the first
185 // rebase location in the page is pageStarts[pageIndex] * 4.
187 // 3) pageStarts[pageIndex] & DYLD_CACHE_SLIDE_PAGE_ATTR_EXTRA
188 // Multiple linked lists are needed for all rebase locations in a page.
189 // The pagesExtras array contains 2 or more entries each of which is the
190 // start of a new linked list in the page. The first is at:
191 // extrasStartIndex = (pageStarts[pageIndex] & 0x3FFF)
192 // The next is at extrasStartIndex+1. The last is denoted by
193 // having the high bit (DYLD_CACHE_SLIDE_PAGE_ATTR_END) of the pageExtras[]
196 // For 64-bit architectures, there is always enough free bits to encode all
197 // possible deltas. The info->delta_mask field shows where the delta is located
198 // in the pointer. That value must be masked off (valueMask) before the slide
199 // is added to the pointer.
201 // For 32-bit architectures, there are only three bits free (the three most
202 // significant bits). To extract the delta, you must first subtract value_add
203 // from the pointer value, then AND with delta_mask, then shift by deltaShift.
204 // That still leaves a maximum delta to the next rebase location of 28 bytes.
205 // To reduce the number or chains needed, an optimization was added. Turns
206 // out zero is common in the DATA region. A zero can be turned into a
207 // non-rebasing entry in the linked list. The can be done because nothing
208 // in the shared cache should point out of its dylib to the start of the shared
211 // The code for processing a linked list (chain) is:
213 // uint32_t delta = 1;
214 // while ( delta != 0 ) {
215 // uint8_t* loc = pageStart + pageOffset;
216 // uintptr_t rawValue = *((uintptr_t*)loc);
217 // delta = ((rawValue & deltaMask) >> deltaShift);
218 // uintptr_t newValue = (rawValue & valueMask);
219 // if ( newValue != 0 ) {
220 // newValue += valueAdd;
221 // newValue += slideAmount;
223 // *((uintptr_t*)loc) = newValue;
224 // pageOffset += delta;
228 struct dyld_cache_slide_info2
230 uint32_t version
; // currently 2
231 uint32_t page_size
; // currently 4096 (may also be 16384)
232 uint32_t page_starts_offset
;
233 uint32_t page_starts_count
;
234 uint32_t page_extras_offset
;
235 uint32_t page_extras_count
;
236 uint64_t delta_mask
; // which (contiguous) set of bits contains the delta to the next rebase location
238 //uint16_t page_starts[page_starts_count];
239 //uint16_t page_extras[page_extras_count];
241 #define DYLD_CACHE_SLIDE_PAGE_ATTRS 0xC000 // high bits of uint16_t are flags
242 #define DYLD_CACHE_SLIDE_PAGE_ATTR_EXTRA 0x8000 // index is into extras array (not starts array)
243 #define DYLD_CACHE_SLIDE_PAGE_ATTR_NO_REBASE 0x4000 // page has no rebasing
244 #define DYLD_CACHE_SLIDE_PAGE_ATTR_END 0x8000 // last chain entry for page
247 struct dyld_cache_local_symbols_info
249 uint32_t nlistOffset
; // offset into this chunk of nlist entries
250 uint32_t nlistCount
; // count of nlist entries
251 uint32_t stringsOffset
; // offset into this chunk of string pool
252 uint32_t stringsSize
; // byte count of string pool
253 uint32_t entriesOffset
; // offset into this chunk of array of dyld_cache_local_symbols_entry
254 uint32_t entriesCount
; // number of elements in dyld_cache_local_symbols_entry array
257 struct dyld_cache_local_symbols_entry
259 uint32_t dylibOffset
; // offset in cache file of start of dylib
260 uint32_t nlistStartIndex
; // start index of locals for this dylib
261 uint32_t nlistCount
; // number of local symbols for this dylib
266 #define MACOSX_DYLD_SHARED_CACHE_DIR "/private/var/db/dyld/"
267 #define IPHONE_DYLD_SHARED_CACHE_DIR "/System/Library/Caches/com.apple.dyld/"
268 #define DYLD_SHARED_CACHE_BASE_NAME "dyld_shared_cache_"
269 #define DYLD_SHARED_CACHE_DEVELOPMENT_EXT ".development"
271 static const uint64_t kDyldSharedCacheTypeDevelopment
= 0;
272 static const uint64_t kDyldSharedCacheTypeProduction
= 1;
277 #endif // __DYLD_CACHE_FORMAT__