dyld-750.6.tar.gz
[apple/dyld.git] / launch-cache / dsc_iterator.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009-2012 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 #include <stdlib.h>
26 #include <stdio.h>
27 #include <Availability.h>
28
29
30 #include "dsc_iterator.h"
31 #include "dyld_cache_format.h"
32 #define NO_ULEB
33 #include "Architectures.hpp"
34 #include "MachOFileAbstraction.hpp"
35 #include "CacheFileAbstraction.hpp"
36 #include "SupportedArchs.h"
37
38 namespace dyld {
39
40
41 // convert an address in the shared region where the cache would normally be mapped, into an address where the cache is currently mapped
42 template <typename E>
43 const uint8_t* mappedAddress(const uint8_t* cache, const uint8_t* cacheEnd, uint64_t addr)
44 {
45 const dyldCacheHeader<E>* header = (dyldCacheHeader<E>*)cache;
46 const dyldCacheFileMapping<E>* mappings = (dyldCacheFileMapping<E>*)&cache[header->mappingOffset()];
47 for (uint32_t i=0; i < header->mappingCount(); ++i) {
48 if ( (mappings[i].address() <= addr) && (addr < (mappings[i].address() + mappings[i].size())) ) {
49 uint64_t cacheOffset = mappings[i].file_offset() + addr - mappings[i].address();
50 const uint8_t* result = &cache[cacheOffset];
51 if ( result < cacheEnd )
52 return result;
53 else
54 return NULL;
55 }
56 }
57 return NULL;
58 }
59
60 // call the callback block on each segment in this image
61 template <typename A>
62 int walkSegments(const uint8_t* cache, const uint8_t* cacheEnd, const uint8_t* firstSeg, const char* dylibPath, uint64_t inode,uint64_t modTime, const uint8_t* machHeader, uint64_t cache_unslid_base_address,
63 void (^callback)(const dyld_shared_cache_dylib_info* dylibInfo, const dyld_shared_cache_segment_info* segInfo))
64 {
65 typedef typename A::P P;
66 typedef typename A::P::E E;
67 dyld_shared_cache_dylib_info dylibInfo;
68 dyld_shared_cache_segment_info segInfo;
69 dylibInfo.version = 2;
70 dylibInfo.isAlias = (dylibPath < (char*)firstSeg); // paths for aliases are store between cache header and first segment
71 dylibInfo.machHeader = machHeader;
72 dylibInfo.path = dylibPath;
73 dylibInfo.inode = inode;
74 dylibInfo.modTime = modTime;
75 const macho_header<P>* mh = (const macho_header<P>*)machHeader;
76 const macho_load_command<P>* const cmds = (macho_load_command<P>*)(machHeader + sizeof(macho_header<P>));
77 if ( (machHeader+ mh->sizeofcmds()) > cacheEnd )
78 return -1;
79 const uint32_t cmd_count = mh->ncmds();
80 const macho_load_command<P>* cmd = cmds;
81 // scan for LC_UUID
82 dylibInfo.uuid = NULL;
83 for (uint32_t i = 0; i < cmd_count; ++i) {
84 if ( cmd->cmd() == LC_UUID ) {
85 const uuid_command* uc = (const uuid_command*)cmd;
86 dylibInfo.uuid = &uc->uuid;
87 break;
88 }
89 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
90 }
91 // callback for each LC_SEGMENT
92 cmd = cmds;
93 for (uint32_t i = 0; i < cmd_count; ++i) {
94 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
95 macho_segment_command<P>* segCmd = (macho_segment_command<P>*)cmd;
96 uint64_t fileOffset = segCmd->fileoff();
97 // work around until <rdar://problem/7022345> is fixed
98 if ( fileOffset == 0 ) {
99 fileOffset = (machHeader - cache);
100 }
101 uint64_t sizem = segCmd->vmsize();
102 if ( strcmp(segCmd->segname(), "__LINKEDIT") == 0 ) {
103 // clip LINKEDIT size if bigger than cache file
104 if ( (fileOffset+sizem) > (uint64_t)(cacheEnd-cache) )
105 sizem = (cacheEnd-cache)-fileOffset;
106 }
107 segInfo.version = 2;
108 segInfo.name = segCmd->segname();
109 segInfo.fileOffset = fileOffset;
110 segInfo.fileSize = sizem;
111 if ( segCmd->filesize() > segCmd->vmsize() )
112 return -1;
113 segInfo.address = segCmd->vmaddr();
114 segInfo.addressOffset = segInfo.address - cache_unslid_base_address;
115 callback(&dylibInfo, &segInfo);
116 }
117 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
118 }
119 return 0;
120 }
121
122
123 // call walkSegments on each image in the cache
124 template <typename A>
125 int walkImages(const uint8_t* cache, uint32_t size, void (^callback)(const dyld_shared_cache_dylib_info* dylibInfo, const dyld_shared_cache_segment_info* segInfo))
126 {
127 // Sanity check there is at least a header
128 if ( (size > 0) && (size < 0x7000) )
129 return -1;
130 typedef typename A::P::E E;
131 typedef typename A::P P;
132 const dyldCacheHeader<E>* header = (dyldCacheHeader<E>*)cache;
133 const dyldCacheImageInfo<E>* dylibs = (dyldCacheImageInfo<E>*)&cache[header->imagesOffset()];
134 const dyldCacheFileMapping<E>* mappings = (dyldCacheFileMapping<E>*)&cache[header->mappingOffset()];
135 uint64_t unslid_base_address = mappings[0].address();
136 uint64_t greatestMappingOffset = 0;
137 for (uint32_t i=0; i < header->mappingCount(); ++i) {
138 if ( (size != 0) && (mappings[i].file_offset() > size) )
139 return -1;
140 uint64_t endOffset = mappings[i].file_offset()+mappings[i].size();
141 if ( (size != 0) && (endOffset > size) )
142 return -1;
143 if ( endOffset > greatestMappingOffset )
144 greatestMappingOffset = endOffset;
145 }
146 const uint8_t* cacheEnd = &cache[size];
147 if ( size == 0 ) {
148 // Zero size means old API is being used, assume all mapped
149 cacheEnd = &cache[greatestMappingOffset];
150 }
151 else {
152 // verifiy mappings are not bigger than size
153 if ( size < greatestMappingOffset )
154 return -1;
155 }
156 // verify all image infos are mapped
157 if ( (const uint8_t*)&dylibs[header->imagesCount()] > cacheEnd )
158 return -1;
159 const uint8_t* firstSeg = NULL;
160 for (uint32_t i=0; i < header->imagesCount(); ++i) {
161 const char* dylibPath = (char*)cache + dylibs[i].pathFileOffset();
162 uint64_t inode = dylibs[i].inode();
163 uint64_t modTime = dylibs[i].modTime();
164 if ( (const uint8_t*)dylibPath > cacheEnd )
165 return -1;
166 const uint8_t* machHeader = mappedAddress<E>(cache, cacheEnd, dylibs[i].address());
167 if ( machHeader == NULL )
168 return -1;
169 if ( machHeader > cacheEnd )
170 return -1;
171 if ( firstSeg == NULL )
172 firstSeg = machHeader;
173 int result = walkSegments<A>(cache, cacheEnd, firstSeg, dylibPath, inode, modTime, machHeader, unslid_base_address, callback);
174 if ( result != 0 )
175 return result;
176 }
177 return 0;
178 }
179
180 }
181
182
183 // Given a pointer to an in-memory copy of a dyld shared cache file,
184 // this routine will call the callback block once for each segment
185 // in each dylib in the shared cache file.
186 // Returns -1 if there was an error, otherwise 0.
187 extern int dyld_shared_cache_iterate(const void* shared_cache_file, uint32_t shared_cache_size,
188 void (^callback)(const dyld_shared_cache_dylib_info* dylibInfo, const dyld_shared_cache_segment_info* segInfo)) {
189 const uint8_t* cache = (uint8_t*)shared_cache_file;
190 if ( strcmp((char*)cache, "dyld_v1 i386") == 0 )
191 return dyld::walkImages<x86>(cache, shared_cache_size, callback);
192 else if ( strcmp((char*)cache, "dyld_v1 x86_64") == 0 )
193 return dyld::walkImages<x86_64>(cache, shared_cache_size, callback);
194 else if ( strcmp((char*)cache, "dyld_v1 x86_64h") == 0 )
195 return dyld::walkImages<x86_64>(cache, shared_cache_size, callback);
196 else if ( strcmp((char*)cache, "dyld_v1 armv5") == 0 )
197 return dyld::walkImages<arm>(cache, shared_cache_size, callback);
198 else if ( strcmp((char*)cache, "dyld_v1 armv6") == 0 )
199 return dyld::walkImages<arm>(cache, shared_cache_size, callback);
200 else if ( strcmp((char*)cache, "dyld_v1 armv7") == 0 )
201 return dyld::walkImages<arm>(cache, shared_cache_size, callback);
202 else if ( strncmp((char*)cache, "dyld_v1 armv7", 14) == 0 )
203 return dyld::walkImages<arm>(cache, shared_cache_size, callback);
204 else if ( strcmp((char*)cache, "dyld_v1 arm64") == 0 )
205 return dyld::walkImages<arm64>(cache, shared_cache_size, callback);
206 #if SUPPORT_ARCH_arm64_32
207 else if ( strcmp((char*)cache, "dyld_v1arm64_32") == 0 )
208 return dyld::walkImages<arm64_32>(cache, shared_cache_size, callback);
209 #endif
210 #if SUPPORT_ARCH_arm64e
211 else if ( strcmp((char*)cache, "dyld_v1 arm64e") == 0 )
212 return dyld::walkImages<arm64>(cache, shared_cache_size, callback);
213 #endif
214 else
215 return -1;
216 }
217
218
219 // implement old version by calling new version
220 int dyld_shared_cache_iterate_segments_with_slide(const void* shared_cache_file, dyld_shared_cache_iterator_slide_t callback)
221 {
222 return dyld_shared_cache_iterate(shared_cache_file, 0, ^(const dyld_shared_cache_dylib_info* dylibInfo, const dyld_shared_cache_segment_info* segInfo) {
223 callback(dylibInfo->path, segInfo->name, segInfo->fileOffset, segInfo->fileSize, segInfo->address, 0);
224 });
225 }
226
227 // implement non-block version by calling block version
228 int dyld_shared_cache_iterate_segments_with_slide_nb(const void* shared_cache_file, dyld_shared_cache_iterator_slide_nb_t func, void* userData)
229 {
230 return dyld_shared_cache_iterate_segments_with_slide(shared_cache_file, ^(const char* dylibName, const char* segName,
231 uint64_t offset, uint64_t size, uint64_t mappedddress, uint64_t slide) {
232 (*func)(dylibName, segName, offset, size, mappedddress, slide, userData);
233 });
234 }
235
236
237 // implement non-slide version by wrapping slide version in block
238 int dyld_shared_cache_iterate_segments(const void* shared_cache_file, dyld_shared_cache_iterator_t callback)
239 {
240 dyld_shared_cache_iterator_slide_t wrapper_cb = ^(const char* dylibName, const char* segName, uint64_t offset,
241 uint64_t size, uint64_t mappedddress, uint64_t slide) {
242 callback(dylibName, segName, offset, size, mappedddress);
243 };
244 return dyld_shared_cache_iterate_segments_with_slide(shared_cache_file, wrapper_cb);
245 }
246
247 // implement non-slide,non-block version by wrapping slide version in block
248 int dyld_shared_cache_iterate_segments_nb(const void* shared_cache_file, dyld_shared_cache_iterator_nb_t func, void* userData)
249 {
250 return dyld_shared_cache_iterate_segments_with_slide(shared_cache_file, ^(const char* dylibName, const char* segName,
251 uint64_t offset, uint64_t size, uint64_t mappedddress, uint64_t slide) {
252 (*func)(dylibName, segName, offset, size, mappedddress, userData);
253 });
254 }
255