]>
Commit | Line | Data |
---|---|---|
39a8cd10 A |
1 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- |
2 | * | |
412ebb8e | 3 | * Copyright (c) 2004-2010 Apple Inc. All rights reserved. |
39a8cd10 A |
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 | // work around until conformance work is complete rdar://problem/4508801 | |
26 | #define __srr0 srr0 | |
27 | #define __eip eip | |
28 | #define __rip rip | |
29 | ||
30 | ||
31 | #include <string.h> | |
32 | #include <fcntl.h> | |
33 | #include <errno.h> | |
34 | #include <sys/types.h> | |
35 | #include <sys/fcntl.h> | |
36 | #include <sys/stat.h> | |
37 | #include <sys/mman.h> | |
38 | #include <mach/mach.h> | |
39 | #include <mach/thread_status.h> | |
40 | #include <mach-o/loader.h> | |
41 | #include <mach-o/reloc.h> | |
42 | #include <mach-o/nlist.h> | |
43 | #include <sys/sysctl.h> | |
44 | #include <libkern/OSAtomic.h> | |
45 | #include <libkern/OSCacheControl.h> | |
cf998323 | 46 | #include <mach-o/dyld_images.h> |
39a8cd10 | 47 | |
39a8cd10 A |
48 | #if __x86_64__ |
49 | #include <mach-o/x86_64/reloc.h> | |
50 | #endif | |
51 | #if __arm__ | |
52 | #include <mach-o/arm/reloc.h> | |
53 | #endif | |
54 | ||
55 | #include "ImageLoaderMachOClassic.h" | |
39a8cd10 | 56 | |
39a8cd10 | 57 | // in dyldStartup.s |
19894a12 | 58 | extern "C" void stub_binding_helper_i386_old(); |
39a8cd10 A |
59 | |
60 | ||
61 | #if __x86_64__ | |
62 | #define POINTER_RELOC X86_64_RELOC_UNSIGNED | |
63 | #else | |
64 | #define POINTER_RELOC GENERIC_RELOC_VANILLA | |
65 | #endif | |
66 | ||
67 | ||
68 | // relocation_info.r_length field has value 3 for 64-bit executables and value 2 for 32-bit executables | |
69 | #if __LP64__ | |
70 | #define RELOC_SIZE 3 | |
71 | #define LC_SEGMENT_COMMAND LC_SEGMENT_64 | |
72 | #define LC_ROUTINES_COMMAND LC_ROUTINES_64 | |
73 | struct macho_segment_command : public segment_command_64 {}; | |
74 | struct macho_section : public section_64 {}; | |
75 | struct macho_routines_command : public routines_command_64 {}; | |
76 | #else | |
77 | #define RELOC_SIZE 2 | |
78 | #define LC_SEGMENT_COMMAND LC_SEGMENT | |
79 | #define LC_ROUTINES_COMMAND LC_ROUTINES | |
80 | struct macho_segment_command : public segment_command {}; | |
81 | struct macho_section : public section {}; | |
82 | struct macho_routines_command : public routines_command {}; | |
83 | #endif | |
84 | ||
85 | ||
86 | ||
87 | ||
88 | // create image for main executable | |
89 | ImageLoaderMachOClassic* ImageLoaderMachOClassic::instantiateMainExecutable(const macho_header* mh, uintptr_t slide, const char* path, | |
90 | unsigned int segCount, unsigned int libCount, const LinkContext& context) | |
91 | { | |
92 | ImageLoaderMachOClassic* image = ImageLoaderMachOClassic::instantiateStart(mh, path, segCount, libCount); | |
93 | ||
94 | // set slide for PIE programs | |
95 | image->setSlide(slide); | |
96 | ||
97 | // for PIE record end of program, to know where to start loading dylibs | |
412ebb8e | 98 | if ( slide != 0 ) |
39a8cd10 | 99 | fgNextPIEDylibAddress = (uintptr_t)image->getEnd(); |
df9d6cf7 A |
100 | |
101 | image->disableCoverageCheck(); | |
39a8cd10 | 102 | image->instantiateFinish(context); |
412ebb8e A |
103 | image->setMapped(context); |
104 | ||
39a8cd10 A |
105 | #if __i386__ |
106 | // kernel may have mapped in __IMPORT segment read-only, we need it read/write to do binding | |
107 | if ( image->fReadOnlyImportSegment ) { | |
108 | for(unsigned int i=0; i < image->fSegmentsCount; ++i) { | |
109 | if ( image->segIsReadOnlyImport(i) ) | |
110 | image->segMakeWritable(i, context); | |
111 | } | |
112 | } | |
113 | #endif | |
114 | ||
115 | if ( context.verboseMapping ) { | |
116 | dyld::log("dyld: Main executable mapped %s\n", path); | |
117 | for(unsigned int i=0, e=image->segmentCount(); i < e; ++i) { | |
118 | const char* name = image->segName(i); | |
119 | if ( (strcmp(name, "__PAGEZERO") == 0) || (strcmp(name, "__UNIXSTACK") == 0) ) | |
120 | dyld::log("%18s at 0x%08lX->0x%08lX\n", name, image->segPreferredLoadAddress(i), image->segPreferredLoadAddress(i)+image->segSize(i)); | |
121 | else | |
122 | dyld::log("%18s at 0x%08lX->0x%08lX\n", name, image->segActualLoadAddress(i), image->segActualEndAddress(i)); | |
123 | } | |
124 | } | |
125 | ||
126 | return image; | |
127 | } | |
128 | ||
129 | // create image by mapping in a mach-o file | |
df9d6cf7 | 130 | ImageLoaderMachOClassic* ImageLoaderMachOClassic::instantiateFromFile(const char* path, int fd, const uint8_t* fileData, size_t lenFileData, |
39a8cd10 | 131 | uint64_t offsetInFat, uint64_t lenInFat, const struct stat& info, |
412ebb8e A |
132 | unsigned int segCount, unsigned int libCount, |
133 | const struct linkedit_data_command* codeSigCmd, const LinkContext& context) | |
39a8cd10 A |
134 | { |
135 | ImageLoaderMachOClassic* image = ImageLoaderMachOClassic::instantiateStart((macho_header*)fileData, path, segCount, libCount); | |
136 | try { | |
137 | // record info about file | |
138 | image->setFileInfo(info.st_dev, info.st_ino, info.st_mtime); | |
139 | ||
412ebb8e | 140 | // if this image is code signed, let kernel validate signature before mapping any pages from image |
2fd3f4e8 | 141 | image->loadCodeSignature(codeSigCmd, fd, offsetInFat, context); |
39a8cd10 | 142 | |
df9d6cf7 A |
143 | // Validate that first data we read with pread actually matches with code signature |
144 | image->validateFirstPages(codeSigCmd, fd, fileData, lenFileData, offsetInFat, context); | |
145 | ||
412ebb8e A |
146 | // mmap segments |
147 | image->mapSegmentsClassic(fd, offsetInFat, lenInFat, info.st_size, context); | |
148 | ||
149 | // finish up | |
150 | image->instantiateFinish(context); | |
151 | ||
39a8cd10 A |
152 | // if path happens to be same as in LC_DYLIB_ID load command use that, otherwise malloc a copy of the path |
153 | const char* installName = image->getInstallPath(); | |
154 | if ( (installName != NULL) && (strcmp(installName, path) == 0) && (path[0] == '/') ) | |
155 | image->setPathUnowned(installName); | |
832b6fce A |
156 | else if ( (path[0] != '/') || (strstr(path, "../") != NULL) ) { |
157 | // rdar://problem/10733082 Fix up @path based paths during introspection | |
39a8cd10 A |
158 | // rdar://problem/5135363 turn relative paths into absolute paths so gdb, Symbolication can later find them |
159 | char realPath[MAXPATHLEN]; | |
832b6fce A |
160 | if ( fcntl(fd, F_GETPATH, realPath) == 0 ) |
161 | image->setPaths(path, realPath); | |
39a8cd10 A |
162 | else |
163 | image->setPath(path); | |
164 | } | |
165 | else | |
166 | image->setPath(path); | |
167 | ||
412ebb8e A |
168 | // make sure path is stable before recording in dyld_all_image_infos |
169 | image->setMapped(context); | |
170 | ||
39a8cd10 A |
171 | } |
172 | catch (...) { | |
173 | // ImageLoader::setMapped() can throw an exception to block loading of image | |
174 | // <rdar://problem/6169686> Leaked fSegmentsArray and image segments during failed dlopen_preflight | |
175 | delete image; | |
176 | throw; | |
177 | } | |
178 | ||
179 | return image; | |
180 | } | |
181 | ||
182 | // create image by using cached mach-o file | |
412ebb8e | 183 | ImageLoaderMachOClassic* ImageLoaderMachOClassic::instantiateFromCache(const macho_header* mh, const char* path, long slide, const struct stat& info, |
39a8cd10 A |
184 | unsigned int segCount, unsigned int libCount, const LinkContext& context) |
185 | { | |
186 | ImageLoaderMachOClassic* image = ImageLoaderMachOClassic::instantiateStart(mh, path, segCount, libCount); | |
187 | try { | |
188 | // record info about file | |
189 | image->setFileInfo(info.st_dev, info.st_ino, info.st_mtime); | |
190 | ||
191 | // remember this is from shared cache and cannot be unloaded | |
192 | image->fInSharedCache = true; | |
193 | image->setNeverUnload(); | |
df9d6cf7 | 194 | image->disableCoverageCheck(); |
39a8cd10 A |
195 | |
196 | // segments already mapped in cache | |
197 | if ( context.verboseMapping ) { | |
198 | dyld::log("dyld: Using shared cached for %s\n", path); | |
199 | for(unsigned int i=0, e=image->segmentCount(); i < e; ++i) { | |
200 | dyld::log("%18s at 0x%08lX->0x%08lX\n", image->segName(i), image->segActualLoadAddress(i), image->segActualEndAddress(i)); | |
201 | } | |
202 | } | |
203 | ||
204 | image->instantiateFinish(context); | |
412ebb8e | 205 | image->setMapped(context); |
39a8cd10 A |
206 | } |
207 | catch (...) { | |
208 | // ImageLoader::setMapped() can throw an exception to block loading of image | |
209 | // <rdar://problem/6169686> Leaked fSegmentsArray and image segments during failed dlopen_preflight | |
210 | delete image; | |
211 | throw; | |
212 | } | |
213 | ||
214 | return image; | |
215 | } | |
216 | ||
217 | // create image by copying an in-memory mach-o file | |
218 | ImageLoaderMachOClassic* ImageLoaderMachOClassic::instantiateFromMemory(const char* moduleName, const macho_header* mh, uint64_t len, | |
219 | unsigned int segCount, unsigned int libCount, const LinkContext& context) | |
220 | { | |
221 | ImageLoaderMachOClassic* image = ImageLoaderMachOClassic::instantiateStart(mh, moduleName, segCount, libCount); | |
222 | try { | |
223 | // map segments | |
224 | if ( mh->filetype == MH_EXECUTE ) | |
225 | throw "can't load another MH_EXECUTE"; | |
226 | ||
227 | // vmcopy segments | |
228 | image->ImageLoaderMachO::mapSegments((const void*)mh, len, context); | |
229 | ||
230 | // for compatibility, never unload dylibs loaded from memory | |
231 | image->setNeverUnload(); | |
232 | ||
df9d6cf7 A |
233 | image->disableCoverageCheck(); |
234 | ||
39a8cd10 A |
235 | // bundle loads need path copied |
236 | if ( moduleName != NULL ) | |
237 | image->setPath(moduleName); | |
238 | ||
239 | image->instantiateFinish(context); | |
412ebb8e | 240 | image->setMapped(context); |
39a8cd10 A |
241 | } |
242 | catch (...) { | |
243 | // ImageLoader::setMapped() can throw an exception to block loading of image | |
244 | // <rdar://problem/6169686> Leaked fSegmentsArray and image segments during failed dlopen_preflight | |
245 | delete image; | |
246 | throw; | |
247 | } | |
248 | ||
249 | return image; | |
250 | } | |
251 | ||
252 | ||
253 | ImageLoaderMachOClassic::ImageLoaderMachOClassic(const macho_header* mh, const char* path, | |
254 | unsigned int segCount, uint32_t segOffsets[], unsigned int libCount) | |
255 | : ImageLoaderMachO(mh, path, segCount, segOffsets, libCount), fStrings(NULL), fSymbolTable(NULL), fDynamicInfo(NULL) | |
256 | { | |
257 | } | |
258 | ||
259 | // construct ImageLoaderMachOClassic using "placement new" with SegmentMachO objects array at end | |
260 | ImageLoaderMachOClassic* ImageLoaderMachOClassic::instantiateStart(const macho_header* mh, const char* path, | |
261 | unsigned int segCount, unsigned int libCount) | |
262 | { | |
263 | size_t size = sizeof(ImageLoaderMachOClassic) + segCount * sizeof(uint32_t) + libCount * sizeof(ImageLoader*); | |
264 | ImageLoaderMachOClassic* allocatedSpace = static_cast<ImageLoaderMachOClassic*>(malloc(size)); | |
265 | if ( allocatedSpace == NULL ) | |
266 | throw "malloc failed"; | |
267 | uint32_t* segOffsets = ((uint32_t*)(((uint8_t*)allocatedSpace) + sizeof(ImageLoaderMachOClassic))); | |
268 | bzero(&segOffsets[segCount], libCount*sizeof(void*)); // zero out lib array | |
269 | return new (allocatedSpace) ImageLoaderMachOClassic(mh, path, segCount, segOffsets, libCount); | |
270 | } | |
271 | ||
272 | ||
273 | ||
274 | // common code to finish initializing object | |
275 | void ImageLoaderMachOClassic::instantiateFinish(const LinkContext& context) | |
276 | { | |
277 | // now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide | |
df9d6cf7 | 278 | this->parseLoadCmds(context); |
39a8cd10 A |
279 | } |
280 | ||
281 | ImageLoaderMachOClassic::~ImageLoaderMachOClassic() | |
282 | { | |
283 | // don't do clean up in ~ImageLoaderMachO() because virtual call to segmentCommandOffsets() won't work | |
284 | destroy(); | |
285 | } | |
286 | ||
287 | uint32_t* ImageLoaderMachOClassic::segmentCommandOffsets() const | |
288 | { | |
289 | return ((uint32_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOClassic))); | |
290 | } | |
291 | ||
292 | ||
293 | ImageLoader* ImageLoaderMachOClassic::libImage(unsigned int libIndex) const | |
294 | { | |
295 | const uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOClassic) + fSegmentsCount*sizeof(uint32_t))); | |
412ebb8e A |
296 | // mask off low bits |
297 | return (ImageLoader*)(images[libIndex] & (-4)); | |
39a8cd10 A |
298 | } |
299 | ||
300 | bool ImageLoaderMachOClassic::libReExported(unsigned int libIndex) const | |
301 | { | |
302 | const uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOClassic) + fSegmentsCount*sizeof(uint32_t))); | |
303 | // re-export flag is low bit | |
304 | return ((images[libIndex] & 1) != 0); | |
305 | } | |
306 | ||
412ebb8e A |
307 | bool ImageLoaderMachOClassic::libIsUpward(unsigned int libIndex) const |
308 | { | |
309 | const uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOClassic) + fSegmentsCount*sizeof(uint32_t))); | |
310 | // upward flag is second bit | |
311 | return ((images[libIndex] & 2) != 0); | |
312 | } | |
313 | ||
39a8cd10 | 314 | |
412ebb8e | 315 | void ImageLoaderMachOClassic::setLibImage(unsigned int libIndex, ImageLoader* image, bool reExported, bool upward) |
39a8cd10 A |
316 | { |
317 | uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOClassic) + fSegmentsCount*sizeof(uint32_t))); | |
318 | uintptr_t value = (uintptr_t)image; | |
319 | if ( reExported ) | |
320 | value |= 1; | |
412ebb8e A |
321 | if ( upward ) |
322 | value |= 2; | |
39a8cd10 A |
323 | images[libIndex] = value; |
324 | } | |
325 | ||
326 | ||
327 | void ImageLoaderMachOClassic::setSymbolTableInfo(const macho_nlist* symbols, const char* strings, const dysymtab_command* dynSym) | |
328 | { | |
329 | fSymbolTable = symbols; | |
330 | fStrings = strings; | |
331 | fDynamicInfo = dynSym; | |
332 | } | |
333 | ||
39a8cd10 A |
334 | |
335 | ||
336 | #if SPLIT_SEG_DYLIB_SUPPORT | |
337 | unsigned int | |
338 | ImageLoaderMachOClassic::getExtraZeroFillEntriesCount() | |
339 | { | |
340 | // calculate mapping entries | |
341 | unsigned int extraZeroFillEntries = 0; | |
342 | for(unsigned int i=0; i < fSegmentsCount; ++i) { | |
343 | if ( segHasTrailingZeroFill(i) ) | |
344 | ++extraZeroFillEntries; | |
345 | } | |
346 | ||
347 | return extraZeroFillEntries; | |
348 | } | |
349 | ||
350 | void | |
351 | ImageLoaderMachOClassic::initMappingTable(uint64_t offsetInFat, | |
352 | shared_file_mapping_np *mappingTable) | |
353 | { | |
354 | for(unsigned int i=0,entryIndex=0; i < fSegmentsCount; ++i, ++entryIndex) { | |
355 | shared_file_mapping_np* entry = &mappingTable[entryIndex]; | |
356 | entry->sfm_address = segActualLoadAddress(i); | |
357 | entry->sfm_size = segFileSize(i); | |
358 | entry->sfm_file_offset = segFileOffset(i) + offsetInFat; | |
359 | entry->sfm_init_prot = VM_PROT_NONE; | |
360 | if ( !segUnaccessible(i) ) { | |
361 | if ( segExecutable(i) ) | |
362 | entry->sfm_init_prot |= VM_PROT_EXECUTE; | |
363 | if ( segReadable(i) ) | |
364 | entry->sfm_init_prot |= VM_PROT_READ; | |
365 | if ( segWriteable(i) ) | |
366 | entry->sfm_init_prot |= VM_PROT_WRITE | VM_PROT_COW; | |
367 | } | |
368 | entry->sfm_max_prot = entry->sfm_init_prot; | |
369 | if ( segHasTrailingZeroFill(i) ) { | |
370 | shared_file_mapping_np* zfentry = &mappingTable[++entryIndex]; | |
371 | zfentry->sfm_address = entry->sfm_address + segFileSize(i); | |
372 | zfentry->sfm_size = segSize(i) - segFileSize(i); | |
373 | zfentry->sfm_file_offset = 0; | |
374 | zfentry->sfm_init_prot = entry->sfm_init_prot | VM_PROT_COW | VM_PROT_ZF; | |
375 | zfentry->sfm_max_prot = zfentry->sfm_init_prot; | |
376 | } | |
377 | } | |
378 | } | |
379 | ||
380 | int | |
381 | ImageLoaderMachOClassic::mapSplitSegDylibOutsideSharedRegion(int fd, | |
382 | uint64_t offsetInFat, | |
383 | uint64_t lenInFat, | |
384 | uint64_t fileLen, | |
385 | const LinkContext& context) | |
386 | { | |
387 | uintptr_t nextAltLoadAddress = 0; | |
388 | const unsigned int segmentCount = fSegmentsCount; | |
389 | const unsigned int extraZeroFillEntries = getExtraZeroFillEntriesCount(); | |
390 | const unsigned int regionCount = segmentCount+extraZeroFillEntries; | |
391 | shared_file_mapping_np regions[regionCount]; | |
392 | initMappingTable(offsetInFat, regions); | |
393 | int r = -1; | |
394 | // find space somewhere to allocate split seg | |
395 | bool foundRoom = false; | |
396 | while ( ! foundRoom ) { | |
397 | foundRoom = true; | |
398 | for(unsigned int i=0; i < regionCount; ++i) { | |
10b92d3b A |
399 | vm_address_t addr = (vm_address_t)(nextAltLoadAddress + regions[i].sfm_address - regions[0].sfm_address); |
400 | vm_size_t size = (vm_size_t)regions[i].sfm_size ; | |
39a8cd10 A |
401 | r = vm_allocate(mach_task_self(), &addr, size, false /*only this range*/); |
402 | if ( 0 != r ) { | |
403 | // no room here, deallocate what has succeeded so far | |
404 | for(unsigned int j=0; j < i; ++j) { | |
10b92d3b A |
405 | addr = (vm_address_t)(nextAltLoadAddress + regions[j].sfm_address - regions[0].sfm_address); |
406 | size = (vm_size_t)(regions[j].sfm_size); | |
39a8cd10 A |
407 | (void)vm_deallocate(mach_task_self(), addr, size); |
408 | } | |
409 | nextAltLoadAddress += 0x00100000; // skip ahead 1MB and try again | |
410 | // skip over shared region | |
411 | if ( (SHARED_REGION_BASE <= nextAltLoadAddress) && (nextAltLoadAddress < (SHARED_REGION_BASE + SHARED_REGION_SIZE)) ) | |
412 | nextAltLoadAddress = (SHARED_REGION_BASE + SHARED_REGION_SIZE); | |
413 | if ( nextAltLoadAddress > 0xFF000000 ) | |
414 | throw "can't map split seg anywhere"; | |
415 | foundRoom = false; | |
416 | break; | |
417 | } | |
418 | } | |
419 | } | |
420 | ||
421 | // map in each region | |
10b92d3b | 422 | uintptr_t slide = (uintptr_t)(nextAltLoadAddress - regions[0].sfm_address); |
39a8cd10 A |
423 | this->setSlide(slide); |
424 | for(unsigned int i=0; i < regionCount; ++i) { | |
425 | if ( ((regions[i].sfm_init_prot & VM_PROT_ZF) != 0) || (regions[i].sfm_size == 0) ) { | |
426 | // nothing to mmap for zero-fills areas, they are just vm_allocated | |
427 | } | |
428 | else { | |
429 | void* mmapAddress = (void*)(uintptr_t)(regions[i].sfm_address + slide); | |
10b92d3b | 430 | size_t size = (size_t)regions[i].sfm_size; |
39a8cd10 A |
431 | int protection = 0; |
432 | if ( regions[i].sfm_init_prot & VM_PROT_EXECUTE ) | |
433 | protection |= PROT_EXEC; | |
434 | if ( regions[i].sfm_init_prot & VM_PROT_READ ) | |
435 | protection |= PROT_READ; | |
436 | if ( regions[i].sfm_init_prot & VM_PROT_WRITE ) | |
437 | protection |= PROT_WRITE; | |
438 | off_t offset = regions[i].sfm_file_offset; | |
439 | //dyld::log("mmap(%p, 0x%08lX, %s\n", mmapAddress, size, fPath); | |
440 | mmapAddress = mmap(mmapAddress, size, protection, MAP_FIXED | MAP_PRIVATE, fd, offset); | |
441 | if ( mmapAddress == ((void*)(-1)) ) | |
442 | throw "mmap error"; | |
443 | } | |
444 | } | |
445 | ||
446 | // logging | |
447 | if ( context.verboseMapping ) { | |
448 | dyld::log("dyld: Mapping split-seg outside shared region, slid by 0x%08lX %s\n", this->fSlide, this->getPath()); | |
449 | for(unsigned int segIndex=0,entryIndex=0; segIndex < segmentCount; ++segIndex, ++entryIndex){ | |
450 | const shared_file_mapping_np* entry = ®ions[entryIndex]; | |
451 | if ( (entry->sfm_init_prot & VM_PROT_ZF) == 0 ) | |
452 | dyld::log("%18s at 0x%08lX->0x%08lX\n", | |
453 | segName(segIndex), segActualLoadAddress(segIndex), segActualEndAddress(segIndex)-1); | |
454 | if ( entryIndex < (regionCount-1) ) { | |
455 | const shared_file_mapping_np* nextEntry = ®ions[entryIndex+1]; | |
456 | if ( (nextEntry->sfm_init_prot & VM_PROT_ZF) != 0 ) { | |
457 | uint64_t segOffset = nextEntry->sfm_address - entry->sfm_address; | |
458 | dyld::log("%18s at 0x%08lX->0x%08lX (zerofill)\n", | |
459 | segName(segIndex), (uintptr_t)(segActualLoadAddress(segIndex) + segOffset), (uintptr_t)(segActualLoadAddress(segIndex) + segOffset + nextEntry->sfm_size - 1)); | |
460 | ++entryIndex; | |
461 | } | |
462 | } | |
463 | } | |
464 | } | |
465 | ||
466 | return r; | |
467 | } | |
468 | #endif // SPLIT_SEG_DYLIB_SUPPORT | |
469 | ||
470 | ||
471 | void ImageLoaderMachOClassic::mapSegmentsClassic(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context) | |
472 | { | |
473 | // non-split segment libraries handled by super class | |
474 | if ( !fIsSplitSeg ) | |
475 | return ImageLoaderMachO::mapSegments(fd, offsetInFat, lenInFat, fileLen, context); | |
476 | ||
477 | #if SPLIT_SEG_SHARED_REGION_SUPPORT | |
412ebb8e A |
478 | // don't map split-seg dylibs into shared region if shared cache is in use |
479 | if ( ! context.dyldLoadedAtSameAddressNeededBySharedCache ) { | |
480 | // try to map into shared region at preferred address | |
481 | if ( mapSplitSegDylibInfoSharedRegion(fd, offsetInFat, lenInFat, fileLen, context) == 0) | |
482 | return; | |
483 | } | |
39a8cd10 A |
484 | // if there is a problem, fall into case where we map file somewhere outside the shared region |
485 | #endif | |
486 | ||
487 | #if SPLIT_SEG_DYLIB_SUPPORT | |
488 | // support old split-seg dylibs by mapping them where ever we find space | |
489 | if ( mapSplitSegDylibOutsideSharedRegion(fd, offsetInFat, lenInFat, fileLen, context) != 0 ) | |
490 | #endif | |
491 | throw "mapping error"; | |
492 | } | |
493 | ||
494 | ||
495 | #if SPLIT_SEG_SHARED_REGION_SUPPORT | |
496 | static int _shared_region_map_np(int fd, uint32_t count, const shared_file_mapping_np mappings[]) | |
497 | { | |
498 | return syscall(295, fd, count, mappings); | |
499 | } | |
500 | ||
501 | int | |
502 | ImageLoaderMachOClassic::mapSplitSegDylibInfoSharedRegion(int fd, | |
503 | uint64_t offsetInFat, | |
504 | uint64_t lenInFat, | |
505 | uint64_t fileLen, | |
506 | const LinkContext& context) | |
507 | { | |
508 | // build table of segments to map | |
509 | const unsigned int segmentCount = fSegmentsCount; | |
510 | const unsigned int extraZeroFillEntries = getExtraZeroFillEntriesCount(); | |
511 | const unsigned int mappingTableCount = segmentCount+extraZeroFillEntries; | |
512 | shared_file_mapping_np mappingTable[mappingTableCount]; | |
513 | initMappingTable(offsetInFat, mappingTable); | |
514 | ||
515 | // try to map it in shared | |
516 | int r = _shared_region_map_np(fd, mappingTableCount, mappingTable); | |
517 | if ( 0 == r ) { | |
518 | this->setNeverUnload(); | |
519 | if ( context.verboseMapping ) { | |
520 | dyld::log("dyld: Mapping split-seg shared %s\n", this->getPath()); | |
521 | for(unsigned int segIndex=0,entryIndex=0; segIndex < segmentCount; ++segIndex, ++entryIndex){ | |
522 | const shared_file_mapping_np* entry = &mappingTable[entryIndex]; | |
523 | if ( (entry->sfm_init_prot & VM_PROT_ZF) == 0 ) | |
524 | dyld::log("%18s at 0x%08lX->0x%08lX\n", | |
525 | segName(segIndex), segActualLoadAddress(segIndex), segActualEndAddress(segIndex)-1); | |
526 | if ( entryIndex < (mappingTableCount-1) ) { | |
527 | const shared_file_mapping_np* nextEntry = &mappingTable[entryIndex+1]; | |
528 | if ( (nextEntry->sfm_init_prot & VM_PROT_ZF) != 0 ) { | |
529 | uint64_t segOffset = nextEntry->sfm_address - entry->sfm_address; | |
530 | dyld::log("%18s at 0x%08lX->0x%08lX\n", | |
531 | segName(segIndex), (uintptr_t)(segActualLoadAddress(segIndex) + segOffset), | |
532 | (uintptr_t)(segActualLoadAddress(segIndex) + segOffset + nextEntry->sfm_size - 1)); | |
533 | ++entryIndex; | |
534 | } | |
535 | } | |
536 | } | |
537 | } | |
538 | } | |
539 | return r; | |
540 | } | |
541 | ||
542 | #endif // SPLIT_SEG_SHARED_REGION_SUPPORT | |
543 | ||
544 | // test if this image is re-exported through parent (the image that loaded this one) | |
545 | bool ImageLoaderMachOClassic::isSubframeworkOf(const LinkContext& context, const ImageLoader* parent) const | |
546 | { | |
547 | if ( fInUmbrella ) { | |
548 | const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds; | |
549 | const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; | |
550 | const struct load_command* cmd = cmds; | |
551 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
552 | if (cmd->cmd == LC_SUB_FRAMEWORK) { | |
553 | const struct sub_framework_command* subf = (struct sub_framework_command*)cmd; | |
554 | const char* exportThruName = (char*)cmd + subf->umbrella.offset; | |
555 | // need to match LC_SUB_FRAMEWORK string against the leaf name of the install location of parent... | |
556 | const char* parentInstallPath = parent->getInstallPath(); | |
557 | if ( parentInstallPath != NULL ) { | |
558 | const char* lastSlash = strrchr(parentInstallPath, '/'); | |
559 | if ( lastSlash != NULL ) { | |
560 | if ( strcmp(&lastSlash[1], exportThruName) == 0 ) | |
561 | return true; | |
562 | if ( context.imageSuffix != NULL ) { | |
563 | // when DYLD_IMAGE_SUFFIX is used, lastSlash string needs imageSuffix removed from end | |
6cae9b63 A |
564 | for(const char* const* suffix = context.imageSuffix; *suffix != NULL; ++suffix) { |
565 | char reexportAndSuffix[strlen(*suffix)+strlen(exportThruName)+1]; | |
566 | strcpy(reexportAndSuffix, exportThruName); | |
567 | strcat(reexportAndSuffix, *suffix); | |
568 | if ( strcmp(&lastSlash[1], reexportAndSuffix) == 0 ) | |
569 | return true; | |
570 | } | |
39a8cd10 A |
571 | } |
572 | } | |
573 | } | |
574 | } | |
575 | cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); | |
576 | } | |
577 | } | |
578 | return false; | |
579 | } | |
580 | ||
581 | // test if child is re-exported | |
582 | bool ImageLoaderMachOClassic::hasSubLibrary(const LinkContext& context, const ImageLoader* child) const | |
583 | { | |
584 | if ( fHasSubLibraries ) { | |
585 | // need to match LC_SUB_LIBRARY string against the leaf name (without extension) of the install location of child... | |
586 | const char* childInstallPath = child->getInstallPath(); | |
587 | if ( childInstallPath != NULL ) { | |
588 | const char* lastSlash = strrchr(childInstallPath, '/'); | |
589 | if ( lastSlash != NULL ) { | |
590 | const char* firstDot = strchr(lastSlash, '.'); | |
19894a12 | 591 | size_t len; |
39a8cd10 A |
592 | if ( firstDot == NULL ) |
593 | len = strlen(lastSlash); | |
594 | else | |
595 | len = firstDot-lastSlash-1; | |
596 | char childLeafName[len+1]; | |
597 | strncpy(childLeafName, &lastSlash[1], len); | |
598 | childLeafName[len] = '\0'; | |
599 | const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds; | |
600 | const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; | |
601 | const struct load_command* cmd = cmds; | |
602 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
603 | switch (cmd->cmd) { | |
604 | case LC_SUB_LIBRARY: | |
605 | { | |
606 | const struct sub_library_command* lib = (struct sub_library_command*)cmd; | |
607 | const char* aSubLibName = (char*)cmd + lib->sub_library.offset; | |
608 | if ( strcmp(aSubLibName, childLeafName) == 0 ) | |
609 | return true; | |
610 | if ( context.imageSuffix != NULL ) { | |
611 | // when DYLD_IMAGE_SUFFIX is used, childLeafName string needs imageSuffix removed from end | |
6cae9b63 A |
612 | for(const char* const* suffix = context.imageSuffix; *suffix != NULL; ++suffix) { |
613 | char aSubLibNameAndSuffix[strlen(*suffix)+strlen(aSubLibName)+1]; | |
614 | strcpy(aSubLibNameAndSuffix, aSubLibName); | |
615 | strcat(aSubLibNameAndSuffix, *suffix); | |
616 | if ( strcmp(aSubLibNameAndSuffix, childLeafName) == 0 ) | |
617 | return true; | |
618 | } | |
39a8cd10 A |
619 | } |
620 | } | |
621 | break; | |
622 | } | |
623 | cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); | |
624 | } | |
625 | } | |
626 | } | |
627 | } | |
628 | if ( fHasSubUmbrella ) { | |
629 | // need to match LC_SUB_UMBRELLA string against the leaf name of install location of child... | |
630 | const char* childInstallPath = child->getInstallPath(); | |
631 | if ( childInstallPath != NULL ) { | |
632 | const char* lastSlash = strrchr(childInstallPath, '/'); | |
633 | if ( lastSlash != NULL ) { | |
634 | const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds; | |
635 | const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; | |
636 | const struct load_command* cmd = cmds; | |
637 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
638 | switch (cmd->cmd) { | |
639 | case LC_SUB_UMBRELLA: | |
640 | { | |
641 | const struct sub_umbrella_command* um = (struct sub_umbrella_command*)cmd; | |
642 | const char* aSubUmbrellaName = (char*)cmd + um->sub_umbrella.offset; | |
643 | if ( strcmp(aSubUmbrellaName, &lastSlash[1]) == 0 ) | |
644 | return true; | |
645 | if ( context.imageSuffix != NULL ) { | |
646 | // when DYLD_IMAGE_SUFFIX is used, lastSlash string needs imageSuffix removed from end | |
6cae9b63 A |
647 | for(const char* const* suffix = context.imageSuffix; *suffix != NULL; ++suffix) { |
648 | char umbrellaAndSuffix[strlen(*suffix)+strlen(aSubUmbrellaName)+1]; | |
649 | strcpy(umbrellaAndSuffix, aSubUmbrellaName); | |
650 | strcat(umbrellaAndSuffix, *suffix); | |
651 | if ( strcmp(umbrellaAndSuffix, &lastSlash[1]) == 0 ) | |
652 | return true; | |
653 | } | |
39a8cd10 A |
654 | } |
655 | } | |
656 | break; | |
657 | } | |
658 | cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); | |
659 | } | |
660 | } | |
661 | } | |
662 | } | |
663 | return false; | |
664 | } | |
665 | ||
666 | ||
667 | uintptr_t ImageLoaderMachOClassic::getFirstWritableSegmentAddress() | |
668 | { | |
669 | // in split segment libraries r_address is offset from first writable segment | |
670 | for(unsigned int i=0; i < fSegmentsCount; ++i) { | |
671 | if ( segWriteable(i) ) | |
672 | return segActualLoadAddress(i); | |
673 | } | |
674 | throw "no writable segment"; | |
675 | } | |
676 | ||
677 | uintptr_t ImageLoaderMachOClassic::getRelocBase() | |
678 | { | |
679 | // r_address is either an offset from the first segment address | |
680 | // or from the first writable segment address | |
681 | #if __x86_64__ | |
682 | return getFirstWritableSegmentAddress(); | |
683 | #else | |
684 | if ( fIsSplitSeg ) | |
685 | return getFirstWritableSegmentAddress(); | |
686 | else | |
687 | return segActualLoadAddress(0); | |
688 | #endif | |
689 | } | |
690 | ||
691 | ||
39a8cd10 A |
692 | #if PREBOUND_IMAGE_SUPPORT |
693 | void ImageLoaderMachOClassic::resetPreboundLazyPointers(const LinkContext& context) | |
694 | { | |
695 | // loop through all local (internal) relocation records looking for pre-bound-lazy-pointer values | |
696 | const uintptr_t relocBase = this->getRelocBase(); | |
10b92d3b | 697 | const uintptr_t slide = this->fSlide; |
39a8cd10 A |
698 | const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->locreloff]); |
699 | const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nlocrel]; | |
700 | for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) { | |
701 | if ( (reloc->r_address & R_SCATTERED) != 0 ) { | |
702 | const struct scattered_relocation_info* sreloc = (struct scattered_relocation_info*)reloc; | |
703 | if (sreloc->r_length == RELOC_SIZE) { | |
704 | uintptr_t* locationToFix = (uintptr_t*)(sreloc->r_address + relocBase); | |
705 | switch(sreloc->r_type) { | |
39a8cd10 A |
706 | #if __i386__ |
707 | case GENERIC_RELOC_PB_LA_PTR: | |
708 | *locationToFix = sreloc->r_value + slide; | |
709 | break; | |
710 | #endif | |
711 | #if __arm__ | |
712 | case ARM_RELOC_PB_LA_PTR: | |
713 | *locationToFix = sreloc->r_value + slide; | |
714 | break; | |
715 | #endif | |
716 | } | |
717 | } | |
718 | } | |
719 | } | |
720 | } | |
721 | #endif | |
722 | ||
723 | ||
724 | ||
725 | ||
9f83892a | 726 | void ImageLoaderMachOClassic::rebase(const LinkContext& context, uintptr_t slide) |
39a8cd10 | 727 | { |
412ebb8e | 728 | CRSetCrashLogMessage2(this->getPath()); |
39a8cd10 | 729 | const uintptr_t relocBase = this->getRelocBase(); |
cf998323 | 730 | |
39a8cd10 A |
731 | // loop through all local (internal) relocation records |
732 | const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->locreloff]); | |
733 | const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nlocrel]; | |
734 | for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) { | |
832b6fce | 735 | uintptr_t rebaseAddr; |
39a8cd10 A |
736 | try { |
737 | #if LINKEDIT_USAGE_DEBUG | |
738 | noteAccessedLinkEditAddress(reloc); | |
739 | #endif | |
740 | #if __x86_64__ | |
741 | // only one kind of local relocation supported for x86_64 | |
742 | if ( reloc->r_length != 3 ) | |
743 | throw "bad local relocation length"; | |
744 | if ( reloc->r_type != X86_64_RELOC_UNSIGNED ) | |
745 | throw "unknown local relocation type"; | |
746 | if ( reloc->r_pcrel != 0 ) | |
747 | throw "bad local relocation pc_rel"; | |
748 | if ( reloc->r_extern != 0 ) | |
749 | throw "extern relocation found with local relocations"; | |
832b6fce A |
750 | rebaseAddr = reloc->r_address + relocBase; |
751 | if ( ! this->containsAddress((void*)rebaseAddr) ) | |
752 | dyld::throwf("local reloc %p not in mapped image\n", (void*)rebaseAddr); | |
753 | *((uintptr_t*)rebaseAddr) += slide; | |
754 | if ( context.verboseRebase ) | |
755 | dyld::log("dyld: rebase: %s:*0x%08lX += 0x%08lX\n", this->getShortName(), rebaseAddr, slide); | |
39a8cd10 A |
756 | #else |
757 | if ( (reloc->r_address & R_SCATTERED) == 0 ) { | |
758 | if ( reloc->r_symbolnum == R_ABS ) { | |
759 | // ignore absolute relocations | |
760 | } | |
761 | else if (reloc->r_length == RELOC_SIZE) { | |
762 | switch(reloc->r_type) { | |
763 | case GENERIC_RELOC_VANILLA: | |
832b6fce A |
764 | rebaseAddr = reloc->r_address + relocBase; |
765 | if ( ! this->containsAddress((void*)rebaseAddr) ) | |
766 | dyld::throwf("local reloc %p not in mapped image\n", (void*)rebaseAddr); | |
767 | *((uintptr_t*)rebaseAddr) += slide; | |
768 | if ( context.verboseRebase ) | |
769 | dyld::log("dyld: rebase: %s:*0x%08lX += 0x%08lX\n", this->getShortName(), rebaseAddr, slide); | |
39a8cd10 | 770 | break; |
39a8cd10 A |
771 | default: |
772 | throw "unknown local relocation type"; | |
773 | } | |
774 | } | |
775 | else { | |
776 | throw "bad local relocation length"; | |
777 | } | |
778 | } | |
779 | else { | |
780 | const struct scattered_relocation_info* sreloc = (struct scattered_relocation_info*)reloc; | |
781 | if (sreloc->r_length == RELOC_SIZE) { | |
782 | uintptr_t* locationToFix = (uintptr_t*)(sreloc->r_address + relocBase); | |
783 | switch(sreloc->r_type) { | |
784 | case GENERIC_RELOC_VANILLA: | |
832b6fce A |
785 | if ( ! this->containsAddress((void*)locationToFix) ) |
786 | dyld::throwf("local scattered reloc %p not in mapped image\n", locationToFix); | |
39a8cd10 | 787 | *locationToFix += slide; |
832b6fce A |
788 | if ( context.verboseRebase ) |
789 | dyld::log("dyld: rebase: %s:*0x%08lX += 0x%08lX\n", this->getShortName(), (uintptr_t)locationToFix, slide); | |
39a8cd10 | 790 | break; |
832b6fce | 791 | #if __i386__ |
39a8cd10 A |
792 | case GENERIC_RELOC_PB_LA_PTR: |
793 | // do nothing | |
794 | break; | |
795 | #elif __arm__ | |
796 | case ARM_RELOC_PB_LA_PTR: | |
797 | // do nothing | |
798 | break; | |
799 | #endif | |
800 | default: | |
801 | throw "unknown local scattered relocation type"; | |
802 | } | |
803 | } | |
804 | else { | |
805 | throw "bad local scattered relocation length"; | |
806 | } | |
807 | } | |
808 | #endif // x86_64 | |
809 | } | |
810 | catch (const char* msg) { | |
811 | const uint8_t* r = (uint8_t*)reloc; | |
812 | dyld::throwf("%s in %s. reloc record at %p: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X", | |
813 | msg, this->getPath(), reloc, r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7]); | |
814 | } | |
815 | } | |
816 | ||
817 | // update stats | |
818 | fgTotalRebaseFixups += fDynamicInfo->nlocrel; | |
412ebb8e | 819 | CRSetCrashLogMessage2(NULL); |
39a8cd10 A |
820 | } |
821 | ||
822 | ||
823 | ||
824 | const struct macho_nlist* ImageLoaderMachOClassic::binarySearchWithToc(const char* key, const char stringPool[], const struct macho_nlist symbols[], | |
825 | const struct dylib_table_of_contents toc[], uint32_t symbolCount, uint32_t hintIndex) const | |
826 | { | |
827 | int32_t high = symbolCount-1; | |
828 | int32_t mid = hintIndex; | |
829 | ||
830 | // handle out of range hint | |
831 | if ( mid >= (int32_t)symbolCount ) | |
832 | mid = symbolCount/2; | |
833 | ++ImageLoaderMachO::fgSymbolTableBinarySearchs; | |
834 | ++fgTotalBindImageSearches; | |
835 | ||
836 | //dyld::log("dyld: binarySearchWithToc for %s in %s\n", key, this->getShortName()); | |
837 | ||
838 | for (int32_t low = 0; low <= high; mid = (low+high)/2) { | |
839 | const uint32_t index = toc[mid].symbol_index; | |
840 | const struct macho_nlist* pivot = &symbols[index]; | |
841 | const char* pivotStr = &stringPool[pivot->n_un.n_strx]; | |
842 | #if LINKEDIT_USAGE_DEBUG | |
843 | noteAccessedLinkEditAddress(&toc[mid]); | |
844 | noteAccessedLinkEditAddress(pivot); | |
845 | noteAccessedLinkEditAddress(pivotStr); | |
846 | #endif | |
832b6fce | 847 | int cmp = strcmp(key, pivotStr); |
39a8cd10 A |
848 | if ( cmp == 0 ) |
849 | return pivot; | |
850 | if ( cmp > 0 ) { | |
851 | // key > pivot | |
852 | low = mid + 1; | |
853 | } | |
854 | else { | |
855 | // key < pivot | |
856 | high = mid - 1; | |
857 | } | |
858 | } | |
859 | return NULL; | |
860 | } | |
861 | ||
862 | const struct macho_nlist* ImageLoaderMachOClassic::binarySearch(const char* key, const char stringPool[], const struct macho_nlist symbols[], uint32_t symbolCount) const | |
863 | { | |
864 | // update stats | |
865 | ++fgTotalBindImageSearches; | |
866 | ++ImageLoaderMachO::fgSymbolTableBinarySearchs; | |
867 | ||
868 | //dyld::log("dyld: binarySearch for %s in %s, stringpool=%p, symbols=%p, symbolCount=%u\n", | |
869 | // key, this->getShortName(), stringPool, symbols, symbolCount); | |
870 | ||
871 | const struct macho_nlist* base = symbols; | |
872 | for (uint32_t n = symbolCount; n > 0; n /= 2) { | |
873 | const struct macho_nlist* pivot = &base[n/2]; | |
874 | const char* pivotStr = &stringPool[pivot->n_un.n_strx]; | |
875 | #if LINKEDIT_USAGE_DEBUG | |
876 | noteAccessedLinkEditAddress(pivot); | |
877 | noteAccessedLinkEditAddress(pivotStr); | |
878 | #endif | |
832b6fce | 879 | int cmp = strcmp(key, pivotStr); |
39a8cd10 A |
880 | if ( cmp == 0 ) |
881 | return pivot; | |
882 | if ( cmp > 0 ) { | |
883 | // key > pivot | |
884 | // move base to symbol after pivot | |
885 | base = &pivot[1]; | |
886 | --n; | |
887 | } | |
888 | else { | |
889 | // key < pivot | |
890 | // keep same base | |
891 | } | |
892 | } | |
893 | return NULL; | |
894 | } | |
895 | ||
896 | ||
9f83892a | 897 | const ImageLoader::Symbol* ImageLoaderMachOClassic::findShallowExportedSymbol(const char* name, const ImageLoader** foundIn) const |
39a8cd10 A |
898 | { |
899 | const struct macho_nlist* sym = NULL; | |
900 | if ( fDynamicInfo->tocoff == 0 ) | |
901 | sym = binarySearch(name, fStrings, &fSymbolTable[fDynamicInfo->iextdefsym], fDynamicInfo->nextdefsym); | |
902 | else | |
903 | sym = binarySearchWithToc(name, fStrings, fSymbolTable, (dylib_table_of_contents*)&fLinkEditBase[fDynamicInfo->tocoff], | |
904 | fDynamicInfo->ntoc, fDynamicInfo->nextdefsym); | |
905 | if ( sym != NULL ) { | |
906 | if ( foundIn != NULL ) | |
907 | *foundIn = (ImageLoader*)this; | |
908 | return (const Symbol*)sym; | |
909 | } | |
910 | return NULL; | |
911 | } | |
912 | ||
913 | ||
914 | ||
915 | bool ImageLoaderMachOClassic::containsSymbol(const void* addr) const | |
916 | { | |
917 | return ( (fSymbolTable <= addr) && (addr < fStrings) ); | |
918 | } | |
919 | ||
920 | ||
2fd3f4e8 | 921 | uintptr_t ImageLoaderMachOClassic::exportedSymbolAddress(const LinkContext& context, const Symbol* symbol, const ImageLoader* requestor, bool runResolver) const |
39a8cd10 A |
922 | { |
923 | const struct macho_nlist* sym = (macho_nlist*)symbol; | |
924 | uintptr_t result = sym->n_value + fSlide; | |
925 | #if __arm__ | |
926 | // processor assumes code address with low bit set is thumb | |
927 | if (sym->n_desc & N_ARM_THUMB_DEF) | |
928 | result |= 1; | |
929 | #endif | |
930 | return result; | |
931 | } | |
932 | ||
933 | bool ImageLoaderMachOClassic::exportedSymbolIsWeakDefintion(const Symbol* symbol) const | |
934 | { | |
935 | const struct macho_nlist* nlistSym = (const struct macho_nlist*)symbol; | |
936 | return ( (nlistSym->n_desc & N_WEAK_DEF) != 0 ); | |
937 | } | |
938 | ||
939 | const char* ImageLoaderMachOClassic::exportedSymbolName(const Symbol* symbol) const | |
940 | { | |
941 | const struct macho_nlist* nlistSym = (const struct macho_nlist*)symbol; | |
942 | return &fStrings[nlistSym->n_un.n_strx]; | |
943 | } | |
944 | ||
945 | unsigned int ImageLoaderMachOClassic::exportedSymbolCount() const | |
946 | { | |
947 | return fDynamicInfo->nextdefsym; | |
948 | } | |
949 | ||
950 | const ImageLoader::Symbol* ImageLoaderMachOClassic::exportedSymbolIndexed(unsigned int index) const | |
951 | { | |
952 | if ( index < fDynamicInfo->nextdefsym ) { | |
953 | const struct macho_nlist* sym = &fSymbolTable[fDynamicInfo->iextdefsym + index]; | |
954 | return (const ImageLoader::Symbol*)sym; | |
955 | } | |
956 | return NULL; | |
957 | } | |
958 | ||
959 | unsigned int ImageLoaderMachOClassic::importedSymbolCount() const | |
960 | { | |
961 | return fDynamicInfo->nundefsym; | |
962 | } | |
963 | ||
964 | const ImageLoader::Symbol* ImageLoaderMachOClassic::importedSymbolIndexed(unsigned int index) const | |
965 | { | |
966 | if ( index < fDynamicInfo->nundefsym ) { | |
967 | const struct macho_nlist* sym = &fSymbolTable[fDynamicInfo->iundefsym + index]; | |
968 | return (const ImageLoader::Symbol*)sym; | |
969 | } | |
970 | return NULL; | |
971 | } | |
972 | ||
973 | const char* ImageLoaderMachOClassic::importedSymbolName(const Symbol* symbol) const | |
974 | { | |
975 | const struct macho_nlist* nlistSym = (const struct macho_nlist*)symbol; | |
976 | return &fStrings[nlistSym->n_un.n_strx]; | |
977 | } | |
978 | ||
979 | ||
980 | ||
981 | bool ImageLoaderMachOClassic::symbolIsWeakDefinition(const struct macho_nlist* symbol) | |
982 | { | |
983 | // if a define and weak ==> coalesced | |
984 | if ( ((symbol->n_type & N_TYPE) == N_SECT) && ((symbol->n_desc & N_WEAK_DEF) != 0) ) | |
985 | return true; | |
986 | ||
987 | // regular symbol | |
988 | return false; | |
989 | } | |
990 | ||
991 | bool ImageLoaderMachOClassic::symbolIsWeakReference(const struct macho_nlist* symbol) | |
992 | { | |
993 | // if an undefine and not referencing a weak symbol ==> coalesced | |
994 | if ( ((symbol->n_type & N_TYPE) != N_SECT) && ((symbol->n_desc & N_REF_TO_WEAK) != 0) ) | |
995 | return true; | |
996 | ||
997 | // regular symbol | |
998 | return false; | |
999 | } | |
1000 | ||
412ebb8e | 1001 | uintptr_t ImageLoaderMachOClassic::getSymbolAddress(const macho_nlist* sym, const LinkContext& context, bool runResolver) const |
39a8cd10 | 1002 | { |
412ebb8e | 1003 | return ImageLoaderMachO::getSymbolAddress((Symbol*)sym, this, context, runResolver); |
39a8cd10 A |
1004 | } |
1005 | ||
1006 | uintptr_t ImageLoaderMachOClassic::resolveUndefined(const LinkContext& context, const struct macho_nlist* undefinedSymbol, | |
9f83892a | 1007 | bool twoLevel, bool dontCoalesce, bool runResolver, const ImageLoader** foundIn) |
39a8cd10 A |
1008 | { |
1009 | ++fgTotalBindSymbolsResolved; | |
1010 | const char* symbolName = &fStrings[undefinedSymbol->n_un.n_strx]; | |
1011 | ||
1012 | #if LINKEDIT_USAGE_DEBUG | |
1013 | noteAccessedLinkEditAddress(undefinedSymbol); | |
1014 | noteAccessedLinkEditAddress(symbolName); | |
1015 | #endif | |
1016 | if ( context.bindFlat || !twoLevel ) { | |
1017 | // flat lookup | |
1018 | if ( ((undefinedSymbol->n_type & N_PEXT) != 0) && ((undefinedSymbol->n_type & N_TYPE) == N_SECT) ) { | |
1019 | // is a multi-module private_extern internal reference that the linker did not optimize away | |
412ebb8e | 1020 | uintptr_t addr = this->getSymbolAddress(undefinedSymbol, context, false); |
39a8cd10 A |
1021 | *foundIn = this; |
1022 | return addr; | |
1023 | } | |
1024 | const Symbol* sym; | |
1025 | if ( context.flatExportFinder(symbolName, &sym, foundIn) ) { | |
2fd3f4e8 A |
1026 | if ( *foundIn != this ) |
1027 | context.addDynamicReference(this, const_cast<ImageLoader*>(*foundIn)); | |
39a8cd10 A |
1028 | return (*foundIn)->getExportedSymbolAddress(sym, context, this); |
1029 | } | |
1030 | // if a bundle is loaded privately the above will not find its exports | |
1031 | if ( this->isBundle() && this->hasHiddenExports() ) { | |
1032 | // look in self for needed symbol | |
9f83892a | 1033 | sym = this->findShallowExportedSymbol(symbolName, foundIn); |
39a8cd10 A |
1034 | if ( sym != NULL ) |
1035 | return (*foundIn)->getExportedSymbolAddress(sym, context, this); | |
1036 | } | |
1037 | if ( (undefinedSymbol->n_desc & N_WEAK_REF) != 0 ) { | |
1038 | // definition can't be found anywhere | |
1039 | // if reference is weak_import, then it is ok, just return 0 | |
1040 | return 0; | |
1041 | } | |
19894a12 | 1042 | throwSymbolNotFound(context, symbolName, this->getPath(), "", "flat namespace"); |
39a8cd10 A |
1043 | } |
1044 | else { | |
1045 | // symbol requires searching images with coalesced symbols (not done during prebinding) | |
1046 | if ( !context.prebinding && !dontCoalesce && (symbolIsWeakReference(undefinedSymbol) || symbolIsWeakDefinition(undefinedSymbol)) ) { | |
1047 | const Symbol* sym; | |
6cae9b63 | 1048 | if ( context.coalescedExportFinder(symbolName, &sym, foundIn, nullptr) ) { |
2fd3f4e8 A |
1049 | if ( *foundIn != this ) |
1050 | context.addDynamicReference(this, const_cast<ImageLoader*>(*foundIn)); | |
39a8cd10 A |
1051 | return (*foundIn)->getExportedSymbolAddress(sym, context, this); |
1052 | } | |
412ebb8e | 1053 | //throwSymbolNotFound(context, symbolName, this->getPath(), "coalesced namespace"); |
39a8cd10 A |
1054 | //dyld::log("dyld: coalesced symbol %s not found in any coalesced image, falling back to two-level lookup", symbolName); |
1055 | } | |
1056 | ||
1057 | // if this is a real definition (not an undefined symbol) there is no ordinal | |
1058 | if ( (undefinedSymbol->n_type & N_TYPE) == N_SECT ) { | |
1059 | // static linker should never generate this case, but if it does, do something sane | |
412ebb8e | 1060 | uintptr_t addr = this->getSymbolAddress(undefinedSymbol, context, false); |
39a8cd10 A |
1061 | *foundIn = this; |
1062 | return addr; | |
1063 | } | |
1064 | ||
1065 | // two level lookup | |
1066 | ImageLoader* target = NULL; | |
1067 | uint8_t ord = GET_LIBRARY_ORDINAL(undefinedSymbol->n_desc); | |
1068 | if ( ord == EXECUTABLE_ORDINAL ) { | |
1069 | target = context.mainExecutable; | |
1070 | } | |
1071 | else if ( ord == SELF_LIBRARY_ORDINAL ) { | |
1072 | target = this; | |
1073 | } | |
1074 | else if ( ord == DYNAMIC_LOOKUP_ORDINAL ) { | |
1075 | // rnielsen: HACKHACK | |
1076 | // flat lookup | |
1077 | const Symbol* sym; | |
1078 | if ( context.flatExportFinder(symbolName, &sym, foundIn) ) | |
1079 | return (*foundIn)->getExportedSymbolAddress(sym, context, this); | |
1080 | // no image has exports this symbol | |
1081 | // report error | |
1082 | context.undefinedHandler(symbolName); | |
1083 | // try looking again | |
1084 | if ( context.flatExportFinder(symbolName, &sym, foundIn) ) | |
1085 | return (*foundIn)->getExportedSymbolAddress(sym, context, this); | |
1086 | ||
19894a12 | 1087 | throwSymbolNotFound(context, symbolName, this->getPath(), "", "dynamic lookup"); |
39a8cd10 A |
1088 | } |
1089 | else if ( ord <= libraryCount() ) { | |
1090 | target = libImage(ord-1); | |
1091 | if ( target == NULL ) { | |
1092 | // if target library not loaded and reference is weak or library is weak return 0 | |
1093 | return 0; | |
1094 | } | |
1095 | } | |
1096 | else { | |
1097 | dyld::throwf("bad mach-o binary, library ordinal (%u) too big (max %u) for symbol %s in %s", | |
1098 | ord, libraryCount(), symbolName, this->getPath()); | |
1099 | } | |
1100 | ||
1101 | if ( target == NULL ) { | |
1102 | //dyld::log("resolveUndefined(%s) in %s\n", symbolName, this->getPath()); | |
1103 | throw "symbol not found"; | |
1104 | } | |
9f83892a A |
1105 | |
1106 | uintptr_t address; | |
1107 | if ( target->findExportedSymbolAddress(context, symbolName, this, ord, runResolver, foundIn, &address) ) | |
1108 | return address; | |
1109 | ||
1110 | if ( (undefinedSymbol->n_type & N_PEXT) != 0 ) { | |
39a8cd10 A |
1111 | // don't know why the static linker did not eliminate the internal reference to a private extern definition |
1112 | *foundIn = this; | |
412ebb8e | 1113 | return this->getSymbolAddress(undefinedSymbol, context, false); |
39a8cd10 A |
1114 | } |
1115 | else if ( (undefinedSymbol->n_desc & N_WEAK_REF) != 0 ) { | |
1116 | // if definition not found and reference is weak return 0 | |
1117 | return 0; | |
1118 | } | |
1119 | ||
1120 | // nowhere to be found | |
19894a12 | 1121 | throwSymbolNotFound(context, symbolName, this->getPath(), "", target->getPath()); |
39a8cd10 A |
1122 | } |
1123 | } | |
1124 | ||
1125 | ||
1126 | ||
1127 | // returns if 'addr' is within the address range of section 'sectionIndex' | |
1128 | // fSlide is not used. 'addr' is assumed to be a prebound address in this image | |
1129 | bool ImageLoaderMachOClassic::isAddrInSection(uintptr_t addr, uint8_t sectionIndex) | |
1130 | { | |
1131 | uint8_t currentSectionIndex = 1; | |
1132 | const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds; | |
1133 | const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; | |
1134 | const struct load_command* cmd = cmds; | |
1135 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
1136 | if ( cmd->cmd == LC_SEGMENT_COMMAND ) { | |
1137 | const struct macho_segment_command* seg = (struct macho_segment_command*)cmd; | |
1138 | if ( (currentSectionIndex <= sectionIndex) && (sectionIndex < currentSectionIndex+seg->nsects) ) { | |
1139 | // 'sectionIndex' is in this segment, get section info | |
1140 | const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command)); | |
1141 | const struct macho_section* const section = §ionsStart[sectionIndex-currentSectionIndex]; | |
1142 | return ( (section->addr <= addr) && (addr < section->addr+section->size) ); | |
1143 | } | |
1144 | else { | |
1145 | // 'sectionIndex' not in this segment, skip to next segment | |
1146 | currentSectionIndex += seg->nsects; | |
1147 | } | |
1148 | } | |
1149 | cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); | |
1150 | } | |
1151 | ||
1152 | return false; | |
1153 | } | |
1154 | ||
1155 | void ImageLoaderMachOClassic::doBindExternalRelocations(const LinkContext& context) | |
1156 | { | |
1157 | const uintptr_t relocBase = this->getRelocBase(); | |
1158 | const bool twoLevel = this->usesTwoLevelNameSpace(); | |
1159 | const bool prebound = this->isPrebindable(); | |
1160 | ||
1161 | #if TEXT_RELOC_SUPPORT | |
1162 | // if there are __TEXT fixups, temporarily make __TEXT writable | |
1163 | if ( fTextSegmentBinds ) | |
1164 | this->makeTextSegmentWritable(context, true); | |
1165 | #endif | |
1166 | // cache last lookup | |
1167 | const struct macho_nlist* lastUndefinedSymbol = NULL; | |
1168 | uintptr_t symbolAddr = 0; | |
1169 | const ImageLoader* image = NULL; | |
1170 | ||
1171 | // loop through all external relocation records and bind each | |
1172 | const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->extreloff]); | |
1173 | const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nextrel]; | |
1174 | for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) { | |
1175 | if (reloc->r_length == RELOC_SIZE) { | |
1176 | switch(reloc->r_type) { | |
1177 | case POINTER_RELOC: | |
1178 | { | |
1179 | const struct macho_nlist* undefinedSymbol = &fSymbolTable[reloc->r_symbolnum]; | |
1180 | uintptr_t* location = ((uintptr_t*)(reloc->r_address + relocBase)); | |
832b6fce A |
1181 | if ( ! this->containsAddress((void*)location) ) |
1182 | dyld::throwf("external reloc %p not in mapped image %s\n", (void*)location, this->getPath()); | |
39a8cd10 A |
1183 | uintptr_t value = *location; |
1184 | bool symbolAddrCached = true; | |
1185 | #if __i386__ | |
1186 | if ( reloc->r_pcrel ) { | |
1187 | value += (uintptr_t)location + 4 - fSlide; | |
1188 | } | |
1189 | #endif | |
1190 | if ( prebound ) { | |
1191 | // we are doing relocations, so prebinding was not usable | |
1192 | // in a prebound executable, the n_value field of an undefined symbol is set to the address where the symbol was found when prebound | |
1193 | // so, subtracting that gives the initial displacement which we need to add to the newly found symbol address | |
1194 | // if mach-o relocation structs had an "addend" field this complication would not be necessary. | |
1195 | if ( ((undefinedSymbol->n_type & N_TYPE) == N_SECT) && ((undefinedSymbol->n_desc & N_WEAK_DEF) != 0) ) { | |
1196 | // weak symbols need special casing, since *location may have been prebound to a definition in another image. | |
1197 | // If *location is currently prebound to somewhere in the same section as the weak definition, we assume | |
1198 | // that we can subtract off the weak symbol address to get the addend. | |
1199 | // If prebound elsewhere, we've lost the addend and have to assume it is zero. | |
1200 | // The prebinding to elsewhere only happens with 10.4+ update_prebinding which only operates on a small set of Apple dylibs | |
1201 | if ( (value == undefinedSymbol->n_value) || this->isAddrInSection(value, undefinedSymbol->n_sect) ) { | |
1202 | value -= undefinedSymbol->n_value; | |
1203 | #if __arm__ | |
1204 | // if weak and thumb subtract off extra thumb bit | |
1205 | if ( (undefinedSymbol->n_desc & N_ARM_THUMB_DEF) != 0 ) | |
1206 | value -= 1; | |
1207 | #endif | |
1208 | } | |
1209 | else | |
1210 | value = 0; | |
1211 | } | |
1212 | #if __arm__ | |
1213 | else if ( ((undefinedSymbol->n_type & N_TYPE) == N_SECT) && ((undefinedSymbol->n_desc & N_ARM_THUMB_DEF) != 0) ) { | |
1214 | // it was prebound to a defined symbol for thumb code in the same linkage unit | |
1215 | // we need to subtract off one to get real addend | |
1216 | value -= (undefinedSymbol->n_value+1); | |
1217 | } | |
1218 | #endif | |
1219 | else { | |
1220 | // is undefined or non-weak symbol, so do subtraction to get addend | |
1221 | value -= undefinedSymbol->n_value; | |
1222 | } | |
1223 | } | |
1224 | // if undefinedSymbol is same as last time, then symbolAddr and image will resolve to the same too | |
1225 | if ( undefinedSymbol != lastUndefinedSymbol ) { | |
1226 | bool dontCoalesce = true; | |
1227 | if ( symbolIsWeakReference(undefinedSymbol) ) { | |
1228 | // when weakbind() is run on a classic mach-o encoding, it won't try | |
1229 | // to coalesce N_REF_TO_WEAK symbols because they are not in the sorted | |
1230 | // range of global symbols. To handle that case we do the coalesing now. | |
1231 | dontCoalesce = false; | |
1232 | } | |
9f83892a | 1233 | symbolAddr = this->resolveUndefined(context, undefinedSymbol, twoLevel, dontCoalesce, false, &image); |
39a8cd10 A |
1234 | lastUndefinedSymbol = undefinedSymbol; |
1235 | symbolAddrCached = false; | |
1236 | } | |
cf998323 | 1237 | if ( context.verboseBind && (undefinedSymbol != NULL) ) { |
39a8cd10 A |
1238 | const char *path = NULL; |
1239 | if ( image != NULL ) { | |
1240 | path = image->getShortName(); | |
1241 | } | |
1242 | const char* cachedString = "(cached)"; | |
1243 | if ( !symbolAddrCached ) | |
1244 | cachedString = ""; | |
1245 | if ( value == 0 ) { | |
1246 | dyld::log("dyld: bind: %s:0x%08lX = %s:%s, *0x%08lX = 0x%08lX%s\n", | |
1247 | this->getShortName(), (uintptr_t)location, | |
1248 | path, &fStrings[undefinedSymbol->n_un.n_strx], (uintptr_t)location, symbolAddr, cachedString); | |
1249 | } | |
1250 | else { | |
1251 | dyld::log("dyld: bind: %s:0x%08lX = %s:%s, *0x%08lX = 0x%08lX%s + %ld\n", | |
1252 | this->getShortName(), (uintptr_t)location, | |
1253 | path, &fStrings[undefinedSymbol->n_un.n_strx], (uintptr_t)location, symbolAddr, cachedString, value); | |
1254 | } | |
1255 | } | |
1256 | value += symbolAddr; | |
1257 | #if __i386__ | |
1258 | if ( reloc->r_pcrel ) { | |
1259 | *location = value - ((uintptr_t)location + 4); | |
1260 | } | |
1261 | else { | |
1262 | // don't dirty page if prebound value was correct | |
1263 | if ( !prebound || (*location != value) ) | |
1264 | *location = value; | |
1265 | } | |
1266 | #else | |
1267 | // don't dirty page if prebound value was correct | |
1268 | if ( !prebound || (*location != value) ) | |
1269 | *location = value; | |
1270 | #endif | |
1271 | // update stats | |
1272 | ++fgTotalBindFixups; | |
1273 | } | |
1274 | break; | |
1275 | default: | |
1276 | throw "unknown external relocation type"; | |
1277 | } | |
1278 | } | |
1279 | else { | |
1280 | throw "bad external relocation length"; | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | #if TEXT_RELOC_SUPPORT | |
1285 | // if there were __TEXT fixups, restore write protection | |
1286 | if ( fTextSegmentBinds ) { | |
1287 | this->makeTextSegmentWritable(context, true); | |
1288 | } | |
1289 | #endif | |
1290 | } | |
1291 | ||
1292 | ||
1293 | ||
1294 | uintptr_t ImageLoaderMachOClassic::bindIndirectSymbol(uintptr_t* ptrToBind, const struct macho_section* sect, const char* symbolName, uintptr_t targetAddr, const ImageLoader* targetImage, const LinkContext& context) | |
1295 | { | |
1296 | if ( context.verboseBind ) { | |
1297 | const char* path = NULL; | |
1298 | if ( targetImage != NULL ) | |
1299 | path = targetImage->getShortName(); | |
1300 | dyld::log("dyld: bind indirect sym: %s:%s$%s = %s:%s, *0x%08lx = 0x%08lx\n", | |
1301 | this->getShortName(), symbolName, (((sect->flags & SECTION_TYPE)==S_NON_LAZY_SYMBOL_POINTERS) ? "non_lazy_ptr" : "lazy_ptr"), | |
1302 | ((path != NULL) ? path : "<weak_import-not-found>"), symbolName, (uintptr_t)ptrToBind, targetAddr); | |
1303 | } | |
1304 | if ( context.bindingHandler != NULL ) { | |
1305 | const char* path = NULL; | |
1306 | if ( targetImage != NULL ) | |
1307 | path = targetImage->getShortName(); | |
1308 | targetAddr = (uintptr_t)context.bindingHandler(path, symbolName, (void *)targetAddr); | |
1309 | } | |
1310 | #if __i386__ | |
1311 | // i386 has special self-modifying stubs that change from "CALL rel32" to "JMP rel32" | |
1312 | if ( ((sect->flags & SECTION_TYPE) == S_SYMBOL_STUBS) && ((sect->flags & S_ATTR_SELF_MODIFYING_CODE) != 0) && (sect->reserved2 == 5) ) { | |
1313 | uint32_t rel32 = targetAddr - (((uint32_t)ptrToBind)+5); | |
1314 | // re-write instruction in a thread-safe manner | |
1315 | // use 8-byte compare-and-swap to alter 5-byte jump table entries | |
1316 | // loop is required in case the extra three bytes that cover the next entry are altered by another thread | |
1317 | bool done = false; | |
1318 | while ( !done ) { | |
1319 | volatile int64_t* jumpPtr = (int64_t*)ptrToBind; | |
1320 | int pad = 0; | |
1321 | // By default the three extra bytes swapped follow the 5-byte JMP. | |
1322 | // But, if the 5-byte jump is up against the end of the __IMPORT segment | |
1323 | // We don't want to access bytes off the end of the segment, so we shift | |
1324 | // the extra bytes to precede the 5-byte JMP. | |
1325 | if ( (((uint32_t)ptrToBind + 8) & 0x00000FFC) == 0x00000000 ) { | |
1326 | jumpPtr = (int64_t*)((uint32_t)ptrToBind - 3); | |
1327 | pad = 3; | |
1328 | } | |
1329 | int64_t oldEntry = *jumpPtr; | |
1330 | union { | |
1331 | int64_t int64; | |
1332 | uint8_t bytes[8]; | |
1333 | } newEntry; | |
1334 | newEntry.int64 = oldEntry; | |
1335 | newEntry.bytes[pad+0] = 0xE9; // JMP rel32 | |
1336 | newEntry.bytes[pad+1] = rel32 & 0xFF; | |
1337 | newEntry.bytes[pad+2] = (rel32 >> 8) & 0xFF; | |
1338 | newEntry.bytes[pad+3] = (rel32 >> 16) & 0xFF; | |
1339 | newEntry.bytes[pad+4] = (rel32 >> 24) & 0xFF; | |
1340 | done = OSAtomicCompareAndSwap64Barrier(oldEntry, newEntry.int64, (int64_t*)jumpPtr); | |
1341 | } | |
1342 | } | |
1343 | else | |
1344 | #endif | |
1345 | *ptrToBind = targetAddr; | |
1346 | return targetAddr; | |
1347 | } | |
1348 | ||
412ebb8e | 1349 | uintptr_t ImageLoaderMachOClassic::doBindFastLazySymbol(uint32_t lazyBindingInfoOffset, const LinkContext& context, void (*lock)(), void (*unlock)()) |
39a8cd10 A |
1350 | { |
1351 | throw "compressed LINKEDIT lazy binder called with classic LINKEDIT"; | |
1352 | } | |
1353 | ||
797cc951 A |
1354 | uintptr_t ImageLoaderMachOClassic::doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context, |
1355 | DyldSharedCache::DataConstLazyScopedWriter& patcher) | |
39a8cd10 A |
1356 | { |
1357 | // scan for all lazy-pointer sections | |
1358 | const bool twoLevel = this->usesTwoLevelNameSpace(); | |
1359 | const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds; | |
1360 | const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; | |
1361 | const struct load_command* cmd = cmds; | |
1362 | const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[fDynamicInfo->indirectsymoff]; | |
1363 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
1364 | switch (cmd->cmd) { | |
1365 | case LC_SEGMENT_COMMAND: | |
1366 | { | |
1367 | const struct macho_segment_command* seg = (struct macho_segment_command*)cmd; | |
1368 | const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command)); | |
1369 | const struct macho_section* const sectionsEnd = §ionsStart[seg->nsects]; | |
1370 | for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) { | |
1371 | const uint8_t type = sect->flags & SECTION_TYPE; | |
1372 | uint32_t symbolIndex = INDIRECT_SYMBOL_LOCAL; | |
1373 | if ( type == S_LAZY_SYMBOL_POINTERS ) { | |
19894a12 | 1374 | const size_t pointerCount = sect->size / sizeof(uintptr_t); |
39a8cd10 A |
1375 | uintptr_t* const symbolPointers = (uintptr_t*)(sect->addr + fSlide); |
1376 | if ( (lazyPointer >= symbolPointers) && (lazyPointer < &symbolPointers[pointerCount]) ) { | |
1377 | const uint32_t indirectTableOffset = sect->reserved1; | |
19894a12 | 1378 | const size_t lazyIndex = lazyPointer - symbolPointers; |
39a8cd10 A |
1379 | symbolIndex = indirectTable[indirectTableOffset + lazyIndex]; |
1380 | } | |
1381 | } | |
1382 | #if __i386__ | |
1383 | else if ( (type == S_SYMBOL_STUBS) && (sect->flags & S_ATTR_SELF_MODIFYING_CODE) && (sect->reserved2 == 5) ) { | |
1384 | // 5 bytes stubs on i386 are new "fast stubs" | |
1385 | uint8_t* const jmpTableBase = (uint8_t*)(sect->addr + fSlide); | |
1386 | uint8_t* const jmpTableEnd = jmpTableBase + sect->size; | |
1387 | // initial CALL instruction in jump table leaves pointer to next entry, so back up | |
1388 | uint8_t* const jmpTableEntryToPatch = ((uint8_t*)lazyPointer) - 5; | |
1389 | lazyPointer = (uintptr_t*)jmpTableEntryToPatch; | |
1390 | if ( (jmpTableEntryToPatch >= jmpTableBase) && (jmpTableEntryToPatch < jmpTableEnd) ) { | |
1391 | const uint32_t indirectTableOffset = sect->reserved1; | |
1392 | const uint32_t entryIndex = (jmpTableEntryToPatch - jmpTableBase)/5; | |
1393 | symbolIndex = indirectTable[indirectTableOffset + entryIndex]; | |
1394 | } | |
1395 | } | |
1396 | #endif | |
1397 | if ( symbolIndex != INDIRECT_SYMBOL_ABS && symbolIndex != INDIRECT_SYMBOL_LOCAL ) { | |
1398 | const char* symbolName = &fStrings[fSymbolTable[symbolIndex].n_un.n_strx]; | |
1399 | const ImageLoader* image = NULL; | |
9f83892a | 1400 | uintptr_t symbolAddr = this->resolveUndefined(context, &fSymbolTable[symbolIndex], twoLevel, false, true, &image); |
39a8cd10 A |
1401 | symbolAddr = this->bindIndirectSymbol(lazyPointer, sect, symbolName, symbolAddr, image, context); |
1402 | ++fgTotalLazyBindFixups; | |
1403 | return symbolAddr; | |
1404 | } | |
1405 | } | |
1406 | } | |
1407 | break; | |
1408 | } | |
1409 | cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); | |
1410 | } | |
1411 | dyld::throwf("lazy pointer not found at address %p in image %s", lazyPointer, this->getPath()); | |
1412 | } | |
1413 | ||
1414 | ||
1415 | ||
9f83892a | 1416 | void ImageLoaderMachOClassic::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder, unsigned) |
39a8cd10 A |
1417 | { |
1418 | it.image = this; | |
1419 | it.symbolName = " "; | |
1420 | it.loadOrder = loadOrder; | |
1421 | it.weakSymbol = false; | |
1422 | it.symbolMatches = false; | |
1423 | it.done = false; | |
1424 | it.type = 0; | |
1425 | if ( fDynamicInfo->tocoff != 0 ) { | |
1426 | it.curIndex = 0; | |
1427 | it.endIndex = fDynamicInfo->ntoc; | |
1428 | } | |
1429 | else { | |
1430 | it.curIndex = 0; | |
1431 | it.endIndex = fDynamicInfo->nextdefsym; | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | ||
1436 | bool ImageLoaderMachOClassic::incrementCoalIterator(CoalIterator& it) | |
1437 | { | |
1438 | if ( it.done ) | |
1439 | return false; | |
1440 | ||
1441 | if ( fDynamicInfo->tocoff != 0 ) { | |
1442 | if ( it.curIndex >= fDynamicInfo->ntoc ) { | |
1443 | it.done = true; | |
1444 | it.symbolName = "~~~"; | |
1445 | return true; | |
1446 | } | |
1447 | else { | |
1448 | const dylib_table_of_contents* toc = (dylib_table_of_contents*)&fLinkEditBase[fDynamicInfo->tocoff]; | |
1449 | const uint32_t index = toc[it.curIndex].symbol_index; | |
1450 | const struct macho_nlist* sym = &fSymbolTable[index]; | |
1451 | const char* symStr = &fStrings[sym->n_un.n_strx]; | |
1452 | it.symbolName = symStr; | |
1453 | it.weakSymbol = (sym->n_desc & N_WEAK_DEF); | |
1454 | it.symbolMatches = false; | |
1455 | it.type = 0; // clear flag that says we applied updates for this symbol | |
1456 | //dyld::log("incrementCoalIterator() curIndex=%ld, symbolName=%s in %s\n", it.curIndex, symStr, this->getPath()); | |
1457 | it.curIndex++; | |
1458 | return false; | |
1459 | } | |
1460 | } | |
1461 | else { | |
1462 | if ( it.curIndex >= fDynamicInfo->nextdefsym ) { | |
1463 | it.done = true; | |
1464 | it.symbolName = "~~~"; | |
1465 | return true; | |
1466 | } | |
1467 | else { | |
1468 | const struct macho_nlist* sym = &fSymbolTable[fDynamicInfo->iextdefsym+it.curIndex]; | |
1469 | const char* symStr = &fStrings[sym->n_un.n_strx]; | |
1470 | it.symbolName = symStr; | |
1471 | it.weakSymbol = (sym->n_desc & N_WEAK_DEF); | |
1472 | it.symbolMatches = false; | |
1473 | it.type = 0; // clear flag that says we applied updates for this symbol | |
1474 | //dyld::log("incrementCoalIterator() curIndex=%ld, symbolName=%s in %s\n", it.curIndex, symStr, this->getPath()); | |
1475 | it.curIndex++; | |
1476 | return false; | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | return false; | |
1481 | } | |
1482 | ||
1483 | uintptr_t ImageLoaderMachOClassic::getAddressCoalIterator(CoalIterator& it, const LinkContext& context) | |
1484 | { | |
1485 | uint32_t symbol_index = 0; | |
1486 | if ( fDynamicInfo->tocoff != 0 ) { | |
1487 | const dylib_table_of_contents* toc = (dylib_table_of_contents*)&fLinkEditBase[fDynamicInfo->tocoff]; | |
1488 | symbol_index = toc[it.curIndex-1].symbol_index; | |
1489 | } | |
1490 | else { | |
19894a12 | 1491 | symbol_index = fDynamicInfo->iextdefsym + (uint32_t)it.curIndex - 1; |
39a8cd10 A |
1492 | } |
1493 | const struct macho_nlist* sym = &fSymbolTable[symbol_index]; | |
1494 | //dyld::log("getAddressCoalIterator() => 0x%llX, %s symbol_index=%d, in %s\n", (uint64_t)(sym->n_value + fSlide), &fStrings[sym->n_un.n_strx], symbol_index, this->getPath()); | |
412ebb8e A |
1495 | #if __arm__ |
1496 | // processor assumes code address with low bit set is thumb | |
1497 | if (sym->n_desc & N_ARM_THUMB_DEF) | |
1498 | return (sym->n_value | 1) + fSlide ; | |
1499 | else | |
1500 | return sym->n_value + fSlide; | |
1501 | #else | |
39a8cd10 | 1502 | return sym->n_value + fSlide; |
412ebb8e | 1503 | #endif |
39a8cd10 A |
1504 | } |
1505 | ||
1506 | ||
9f83892a | 1507 | void ImageLoaderMachOClassic::updateUsesCoalIterator(CoalIterator& it, uintptr_t value, ImageLoader* targetImage, unsigned targetIndex, const LinkContext& context) |
39a8cd10 A |
1508 | { |
1509 | // flat_namespace images with classic LINKEDIT do not need late coalescing. | |
1510 | // They still need to be iterated becuase they may implement | |
1511 | // something needed by other coalescing images. | |
1512 | // But they need no updating because during the bind phase every symbol lookup is a full scan. | |
1513 | if ( !this->usesTwoLevelNameSpace() ) | |
1514 | return; | |
1515 | ||
1516 | // <rdar://problem/6570879> weak binding done too early with inserted libraries | |
1517 | if ( this->getState() < dyld_image_state_bound ) | |
1518 | return; | |
1519 | ||
1520 | uint32_t symbol_index = 0; | |
1521 | if ( fDynamicInfo->tocoff != 0 ) { | |
1522 | const dylib_table_of_contents* toc = (dylib_table_of_contents*)&fLinkEditBase[fDynamicInfo->tocoff]; | |
1523 | symbol_index = toc[it.curIndex-1].symbol_index; | |
1524 | } | |
1525 | else { | |
19894a12 | 1526 | symbol_index = fDynamicInfo->iextdefsym + (uint32_t)it.curIndex - 1; |
39a8cd10 A |
1527 | } |
1528 | ||
1529 | // if this image's copy of the symbol is not a weak definition nor a weak reference then nothing to coalesce here | |
1530 | if ( !symbolIsWeakReference(&fSymbolTable[symbol_index]) && !symbolIsWeakDefinition(&fSymbolTable[symbol_index]) ) { | |
1531 | return; | |
1532 | } | |
1533 | ||
1534 | // <rdar://problem/6555720> malformed dylib with duplicate weak symbols causes re-binding | |
1535 | if ( it.type ) | |
1536 | return; | |
1537 | ||
1538 | bool boundSomething = false; | |
1539 | // scan external relocations for uses of symbol_index | |
1540 | const uintptr_t relocBase = this->getRelocBase(); | |
1541 | const bool prebound = this->isPrebindable(); | |
1542 | const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->extreloff]); | |
1543 | const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nextrel]; | |
1544 | for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) { | |
1545 | if ( reloc->r_symbolnum == symbol_index ) { | |
1546 | //dyld::log("found external reloc using symbol_index=%d in %s\n",symbol_index, this->getPath()); | |
1547 | const struct macho_nlist* undefinedSymbol = &fSymbolTable[reloc->r_symbolnum]; | |
1548 | const char* symbolName = &fStrings[undefinedSymbol->n_un.n_strx]; | |
1549 | uintptr_t* location = ((uintptr_t*)(reloc->r_address + relocBase)); | |
1550 | const uintptr_t initialValue = *location; | |
1551 | uintptr_t addend = 0; | |
1552 | if ( prebound ) { | |
1553 | // we are doing relocations, so prebinding was not usable | |
1554 | // in a prebound executable, the n_value field of an undefined symbol is set to the address where the symbol was found when prebound | |
1555 | // so, subtracting that gives the initial displacement which we need to add to the newly found symbol address | |
1556 | // if mach-o relocation structs had an "addend" field this complication would not be necessary. | |
1557 | if ( ((undefinedSymbol->n_type & N_TYPE) == N_SECT) && ((undefinedSymbol->n_desc & N_WEAK_DEF) != 0) ) { | |
1558 | // weak symbols need special casing, since *location may have been prebound to a definition in another image. | |
1559 | // If *location is currently prebound to somewhere in the same section as the weak definition, we assume | |
1560 | // that we can subtract off the weak symbol address to get the addend. | |
1561 | // If prebound elsewhere, we've lost the addend and have to assume it is zero. | |
1562 | // The prebinding to elsewhere only happens with 10.4+ update_prebinding which only operates on a small set of Apple dylibs | |
1563 | if ( (initialValue == undefinedSymbol->n_value) || this->isAddrInSection(initialValue, undefinedSymbol->n_sect) ) { | |
1564 | addend = initialValue - undefinedSymbol->n_value; | |
1565 | #if __arm__ | |
1566 | // if weak and thumb subtract off extra thumb bit | |
1567 | if ( (undefinedSymbol->n_desc & N_ARM_THUMB_DEF) != 0 ) | |
412ebb8e | 1568 | addend &= -2; |
39a8cd10 A |
1569 | #endif |
1570 | } | |
1571 | } | |
1572 | #if __arm__ | |
1573 | else if ( ((undefinedSymbol->n_type & N_TYPE) == N_SECT) && ((undefinedSymbol->n_desc & N_ARM_THUMB_DEF) != 0) ) { | |
1574 | // it was prebound to a defined symbol for thumb code in the same linkage unit | |
1575 | // we need to subtract off one to get real addend | |
1576 | addend = initialValue - (undefinedSymbol->n_value+1); | |
1577 | } | |
1578 | #endif | |
1579 | else { | |
1580 | // is undefined or non-weak symbol, so do subtraction to get addend | |
1581 | addend = initialValue - undefinedSymbol->n_value; | |
1582 | } | |
1583 | } | |
1584 | else { | |
1585 | // non-prebound case | |
1586 | if ( ((undefinedSymbol->n_type & N_TYPE) == N_SECT) && ((undefinedSymbol->n_desc & N_WEAK_DEF) != 0) ) { | |
1587 | // if target is weak-def in same linkage unit, then bind phase has already set initialValue | |
1588 | // to be definition address plus addend | |
1589 | //dyld::log("weak def, initialValue=0x%lX, undefAddr=0x%lX\n", initialValue, undefinedSymbol->n_value+fSlide); | |
1590 | addend = initialValue - (undefinedSymbol->n_value + fSlide); | |
412ebb8e A |
1591 | #if __arm__ |
1592 | // if weak and thumb subtract off extra thumb bit | |
1593 | if ( (undefinedSymbol->n_desc & N_ARM_THUMB_DEF) != 0 ) | |
1594 | addend &= -2; | |
1595 | #endif | |
39a8cd10 A |
1596 | } |
1597 | else { | |
1598 | // nothing fixed up yet, addend is just initial value | |
1599 | //dyld::log("addend=0x%lX\n", initialValue); | |
1600 | addend = initialValue; | |
1601 | } | |
1602 | } | |
1603 | ||
1604 | uint8_t type = BIND_TYPE_POINTER; | |
1605 | #if __i386__ | |
1606 | if ( reloc->r_pcrel ) | |
1607 | type = BIND_TYPE_TEXT_PCREL32; | |
1608 | #endif | |
6cae9b63 | 1609 | this->bindLocation(context, this->imageBaseAddress(), (uintptr_t)location, value, type, symbolName, addend, this->getPath(), targetImage ? targetImage->getPath() : NULL, "weak ", NULL, fSlide); |
39a8cd10 A |
1610 | boundSomething = true; |
1611 | } | |
1612 | } | |
1613 | ||
1614 | // scan lazy and non-lazy pointers for uses of symbol_index | |
1615 | const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds; | |
1616 | const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; | |
1617 | const struct load_command* cmd = cmds; | |
1618 | const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[fDynamicInfo->indirectsymoff]; | |
1619 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
1620 | if ( cmd->cmd == LC_SEGMENT_COMMAND ) { | |
1621 | const struct macho_segment_command* seg = (struct macho_segment_command*)cmd; | |
1622 | const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command)); | |
1623 | const struct macho_section* const sectionsEnd = §ionsStart[seg->nsects]; | |
1624 | for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) { | |
1625 | uint32_t elementSize = sizeof(uintptr_t); | |
1626 | switch ( sect->flags & SECTION_TYPE ) { | |
1627 | #if __i386__ | |
1628 | case S_SYMBOL_STUBS: | |
1629 | if ( ((sect->flags & S_ATTR_SELF_MODIFYING_CODE) ==0) || (sect->reserved2 != 5) ) | |
1630 | continue; | |
1631 | elementSize = 5; | |
cf998323 | 1632 | [[clang::fallthrough]]; |
39a8cd10 A |
1633 | #endif |
1634 | case S_NON_LAZY_SYMBOL_POINTERS: | |
1635 | case S_LAZY_SYMBOL_POINTERS: | |
1636 | { | |
19894a12 | 1637 | size_t elementCount = sect->size / elementSize; |
39a8cd10 A |
1638 | const uint32_t indirectTableOffset = sect->reserved1; |
1639 | uint8_t* ptrToBind = (uint8_t*)(sect->addr + fSlide); | |
1640 | //dyld::log(" scanning section %s of %s starting at %p\n", sect->sectname, this->getShortName(), ptrToBind); | |
19894a12 | 1641 | for (size_t j=0; j < elementCount; ++j, ptrToBind += elementSize) { |
39a8cd10 A |
1642 | if ( indirectTable[indirectTableOffset + j] == symbol_index ) { |
1643 | //dyld::log(" found symbol index match at %d/%d, ptrToBind=%p\n", j, elementCount, ptrToBind); | |
1644 | // update pointer | |
1645 | this->bindIndirectSymbol((uintptr_t*)ptrToBind, sect, it.symbolName, value, targetImage, context); | |
1646 | boundSomething = true; | |
1647 | } | |
1648 | } | |
1649 | } | |
1650 | break; | |
1651 | } | |
1652 | } | |
1653 | } | |
1654 | cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); | |
1655 | } | |
2fd3f4e8 A |
1656 | if ( boundSomething && (targetImage != this) ) { |
1657 | context.addDynamicReference(this, targetImage); | |
1658 | } | |
39a8cd10 A |
1659 | |
1660 | // mark that this symbol has already been bound, so we don't try to bind again | |
1661 | it.type = 1; | |
1662 | } | |
1663 | ||
1664 | ||
1665 | void ImageLoaderMachOClassic::bindIndirectSymbolPointers(const LinkContext& context, bool bindNonLazys, bool bindLazys) | |
1666 | { | |
1667 | // scan for all non-lazy-pointer sections | |
1668 | const bool twoLevel = this->usesTwoLevelNameSpace(); | |
1669 | const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds; | |
1670 | const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; | |
1671 | const struct load_command* cmd = cmds; | |
1672 | const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[fDynamicInfo->indirectsymoff]; | |
1673 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
1674 | switch (cmd->cmd) { | |
1675 | case LC_SEGMENT_COMMAND: | |
1676 | { | |
1677 | const struct macho_segment_command* seg = (struct macho_segment_command*)cmd; | |
1678 | const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command)); | |
1679 | const struct macho_section* const sectionsEnd = §ionsStart[seg->nsects]; | |
1680 | for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) { | |
1681 | bool isLazySymbol = false; | |
1682 | const uint8_t type = sect->flags & SECTION_TYPE; | |
1683 | uint32_t elementSize = sizeof(uintptr_t); | |
19894a12 | 1684 | size_t elementCount = sect->size / elementSize; |
39a8cd10 A |
1685 | if ( type == S_NON_LAZY_SYMBOL_POINTERS ) { |
1686 | if ( ! bindNonLazys ) | |
1687 | continue; | |
1688 | } | |
1689 | else if ( type == S_LAZY_SYMBOL_POINTERS ) { | |
1690 | // process each symbol pointer in this section | |
1691 | fgTotalPossibleLazyBindFixups += elementCount; | |
1692 | isLazySymbol = true; | |
1693 | if ( ! bindLazys ) | |
1694 | continue; | |
1695 | } | |
1696 | #if __i386__ | |
1697 | else if ( (type == S_SYMBOL_STUBS) && (sect->flags & S_ATTR_SELF_MODIFYING_CODE) && (sect->reserved2 == 5) ) { | |
1698 | // process each jmp entry in this section | |
1699 | elementCount = sect->size / 5; | |
1700 | elementSize = 5; | |
1701 | fgTotalPossibleLazyBindFixups += elementCount; | |
1702 | isLazySymbol = true; | |
1703 | if ( ! bindLazys ) | |
1704 | continue; | |
1705 | } | |
1706 | #endif | |
1707 | else { | |
1708 | continue; | |
1709 | } | |
1710 | const uint32_t indirectTableOffset = sect->reserved1; | |
1711 | uint8_t* ptrToBind = (uint8_t*)(sect->addr + fSlide); | |
19894a12 | 1712 | for (size_t j=0; j < elementCount; ++j, ptrToBind += elementSize) { |
39a8cd10 A |
1713 | #if LINKEDIT_USAGE_DEBUG |
1714 | noteAccessedLinkEditAddress(&indirectTable[indirectTableOffset + j]); | |
1715 | #endif | |
1716 | uint32_t symbolIndex = indirectTable[indirectTableOffset + j]; | |
1717 | if ( symbolIndex == INDIRECT_SYMBOL_LOCAL) { | |
1718 | *((uintptr_t*)ptrToBind) += this->fSlide; | |
1719 | } | |
1720 | else if ( symbolIndex == INDIRECT_SYMBOL_ABS) { | |
1721 | // do nothing since already has absolute address | |
1722 | } | |
1723 | else { | |
1724 | const struct macho_nlist* sym = &fSymbolTable[symbolIndex]; | |
1725 | if ( symbolIndex == 0 ) { | |
1726 | // This could be rdar://problem/3534709 | |
1727 | if ( ((const macho_header*)fMachOData)->filetype == MH_EXECUTE ) { | |
1728 | static bool alreadyWarned = false; | |
1729 | if ( (sym->n_type & N_TYPE) != N_UNDF ) { | |
1730 | // The indirect table parallels the (non)lazy pointer sections. For | |
1731 | // instance, to find info about the fifth lazy pointer you look at the | |
1732 | // fifth entry in the indirect table. (try otool -Iv on a file). | |
1733 | // The entry in the indirect table contains an index into the symbol table. | |
1734 | ||
1735 | // The bug in ld caused the entry in the indirect table to be zero | |
1736 | // (instead of a magic value that means a local symbol). So, if the | |
1737 | // symbolIndex == 0, we may be encountering the bug, or 0 may be a valid | |
1738 | // symbol table index. The check I put in place is to see if the zero'th | |
1739 | // symbol table entry is an import entry (usually it is a local symbol | |
1740 | // definition). | |
1741 | if ( context.verboseWarnings && !alreadyWarned ) { | |
1742 | dyld::log("dyld: malformed executable '%s', skipping indirect symbol to %s\n", | |
1743 | this->getPath(), &fStrings[sym->n_un.n_strx]); | |
1744 | alreadyWarned = true; | |
1745 | } | |
1746 | continue; | |
1747 | } | |
1748 | } | |
1749 | } | |
1750 | const ImageLoader* image = NULL; | |
1751 | // let weak definitions resolve to themselves, later coalescing may overwrite them | |
1752 | bool dontCoalesce = true; | |
1753 | if ( bindLazys && isLazySymbol ) { | |
1754 | // if this is something normally lazy bound, but we are forcing | |
1755 | // it to be bound now, do coalescing | |
1756 | dontCoalesce = false; | |
1757 | } | |
1758 | if ( symbolIsWeakReference(sym) ) { | |
1759 | // when weakbind() is run on a classic mach-o encoding, it won't try | |
1760 | // to coalesce N_REF_TO_WEAK symbols because they are not in the sorted | |
1761 | // range of global symbols. To handle that case we do the coalesing now. | |
1762 | dontCoalesce = false; | |
1763 | } | |
9f83892a | 1764 | uintptr_t symbolAddr = resolveUndefined(context, sym, twoLevel, dontCoalesce, false, &image); |
39a8cd10 | 1765 | // update pointer |
cf998323 | 1766 | this->bindIndirectSymbol((uintptr_t*)ptrToBind, sect, &fStrings[sym->n_un.n_strx], symbolAddr, image, context); |
39a8cd10 A |
1767 | // update stats |
1768 | ++fgTotalBindFixups; | |
1769 | } | |
1770 | } | |
1771 | } | |
1772 | } | |
1773 | break; | |
1774 | } | |
1775 | cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); | |
1776 | } | |
1777 | } | |
1778 | ||
1779 | ||
1780 | ||
1781 | #if __i386__ | |
1782 | void ImageLoaderMachOClassic::initializeLazyStubs(const LinkContext& context) | |
1783 | { | |
1784 | if ( ! this->usablePrebinding(context) ) { | |
1785 | // reset all "fast" stubs | |
1786 | const macho_header* mh = (macho_header*)fMachOData; | |
1787 | const uint32_t cmd_count = mh->ncmds; | |
1788 | const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; | |
1789 | const struct load_command* cmd = cmds; | |
1790 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
1791 | switch (cmd->cmd) { | |
1792 | case LC_SEGMENT_COMMAND: | |
1793 | { | |
1794 | const struct macho_segment_command* seg = (struct macho_segment_command*)cmd; | |
1795 | const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command)); | |
1796 | const struct macho_section* const sectionsEnd = §ionsStart[seg->nsects]; | |
1797 | for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) { | |
1798 | const uint8_t type = sect->flags & SECTION_TYPE; | |
1799 | if ( (type == S_SYMBOL_STUBS) && (sect->flags & S_ATTR_SELF_MODIFYING_CODE) && (sect->reserved2 == 5) ) { | |
1800 | // reset each jmp entry in this section | |
1801 | const uint32_t indirectTableOffset = sect->reserved1; | |
1802 | const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[fDynamicInfo->indirectsymoff]; | |
1803 | uint8_t* start = (uint8_t*)(sect->addr + this->fSlide); | |
1804 | uint8_t* end = start + sect->size; | |
19894a12 | 1805 | uintptr_t dyldHandler = (uintptr_t)&stub_binding_helper_i386_old; |
39a8cd10 A |
1806 | uint32_t entryIndex = 0; |
1807 | for (uint8_t* entry = start; entry < end; entry += 5, ++entryIndex) { | |
1808 | bool installLazyHandler = true; | |
1809 | // jump table entries that cross a (64-byte) cache line boundary have the potential to cause crashes | |
1810 | // if the instruction is updated by one thread while being executed by another | |
1811 | if ( ((uint32_t)entry & 0xFFFFFFC0) != ((uint32_t)entry+4 & 0xFFFFFFC0) ) { | |
1812 | // need to bind this now to avoid a potential problem if bound lazily | |
1813 | uint32_t symbolIndex = indirectTable[indirectTableOffset + entryIndex]; | |
1814 | // the latest linker marks 64-byte crossing stubs with INDIRECT_SYMBOL_ABS so they are not used | |
1815 | if ( symbolIndex != INDIRECT_SYMBOL_ABS ) { | |
1816 | const char* symbolName = &fStrings[fSymbolTable[symbolIndex].n_un.n_strx]; | |
1817 | const ImageLoader* image = NULL; | |
1818 | try { | |
9f83892a | 1819 | uintptr_t symbolAddr = this->resolveUndefined(context, &fSymbolTable[symbolIndex], this->usesTwoLevelNameSpace(), false, false, &image); |
39a8cd10 A |
1820 | symbolAddr = this->bindIndirectSymbol((uintptr_t*)entry, sect, symbolName, symbolAddr, image, context); |
1821 | ++fgTotalBindFixups; | |
1822 | uint32_t rel32 = symbolAddr - (((uint32_t)entry)+5); | |
1823 | entry[0] = 0xE9; // JMP rel32 | |
1824 | entry[1] = rel32 & 0xFF; | |
1825 | entry[2] = (rel32 >> 8) & 0xFF; | |
1826 | entry[3] = (rel32 >> 16) & 0xFF; | |
1827 | entry[4] = (rel32 >> 24) & 0xFF; | |
1828 | installLazyHandler = false; | |
1829 | } | |
1830 | catch (const char* msg) { | |
1831 | // ignore errors when binding symbols early | |
1832 | // maybe the function is never called, and therefore erroring out now would be a regression | |
1833 | } | |
1834 | } | |
1835 | } | |
1836 | if ( installLazyHandler ) { | |
1837 | uint32_t rel32 = dyldHandler - (((uint32_t)entry)+5); | |
1838 | entry[0] = 0xE8; // CALL rel32 | |
1839 | entry[1] = rel32 & 0xFF; | |
1840 | entry[2] = (rel32 >> 8) & 0xFF; | |
1841 | entry[3] = (rel32 >> 16) & 0xFF; | |
1842 | entry[4] = (rel32 >> 24) & 0xFF; | |
1843 | } | |
1844 | } | |
1845 | } | |
1846 | } | |
1847 | } | |
1848 | } | |
1849 | cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); | |
1850 | } | |
1851 | } | |
1852 | } | |
1853 | #endif // __i386__ | |
1854 | ||
1855 | ||
bc3b7c8c | 1856 | void ImageLoaderMachOClassic::doBind(const LinkContext& context, bool forceLazysBound, const ImageLoader* reExportParent) |
39a8cd10 | 1857 | { |
412ebb8e | 1858 | CRSetCrashLogMessage2(this->getPath()); |
39a8cd10 A |
1859 | #if __i386__ |
1860 | this->initializeLazyStubs(context); | |
1861 | #endif | |
1862 | ||
1863 | // if prebound and loaded at prebound address, and all libraries are same as when this was prebound, then no need to bind | |
1864 | // note: flat-namespace binaries need to have imports rebound (even if correctly prebound) | |
1865 | if ( this->usablePrebinding(context) ) { | |
1866 | // binding already up to date | |
1867 | } | |
1868 | else { | |
1869 | // no valid prebinding, so bind symbols. | |
1870 | // values bound by name are stored two different ways in classic mach-o: | |
1871 | ||
412ebb8e A |
1872 | #if TEXT_RELOC_SUPPORT |
1873 | // if there are __TEXT fixups, temporarily make __TEXT writable | |
1874 | if ( fTextSegmentBinds ) | |
1875 | this->makeTextSegmentWritable(context, true); | |
1876 | #endif | |
1877 | ||
39a8cd10 A |
1878 | // 1) external relocations are used for data initialized to external symbols |
1879 | this->doBindExternalRelocations(context); | |
1880 | ||
1881 | // 2) "indirect symbols" are used for code references to external symbols | |
1882 | // if this image is in the shared cache, there is no way to reset the lazy pointers, so bind them now | |
1883 | this->bindIndirectSymbolPointers(context, true, forceLazysBound || fInSharedCache); | |
1884 | ||
412ebb8e A |
1885 | #if TEXT_RELOC_SUPPORT |
1886 | // if there were __TEXT fixups, restore write protection | |
1887 | if ( fTextSegmentBinds ) | |
1888 | this->makeTextSegmentWritable(context, false); | |
1889 | #endif | |
39a8cd10 A |
1890 | } |
1891 | ||
1892 | // set up dyld entry points in image | |
1893 | this->setupLazyPointerHandler(context); | |
412ebb8e A |
1894 | |
1895 | CRSetCrashLogMessage2(NULL); | |
39a8cd10 A |
1896 | } |
1897 | ||
797cc951 | 1898 | void ImageLoaderMachOClassic::doBindJustLazies(const LinkContext& context, DyldSharedCache::DataConstLazyScopedWriter& patcher) |
39a8cd10 A |
1899 | { |
1900 | // some API called requested that all lazy pointers in this image be force bound | |
1901 | this->bindIndirectSymbolPointers(context, false, true); | |
1902 | } | |
1903 | ||
412ebb8e A |
1904 | void ImageLoaderMachOClassic::doInterpose(const LinkContext& context) |
1905 | { | |
1906 | if ( context.verboseInterposing ) | |
1907 | dyld::log("dyld: interposing %lu tuples onto: %s\n", fgInterposingTuples.size(), this->getPath()); | |
1908 | ||
1909 | // scan indirect symbols | |
1910 | const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds; | |
1911 | const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; | |
1912 | const struct load_command* cmd = cmds; | |
1913 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
1914 | switch (cmd->cmd) { | |
1915 | case LC_SEGMENT_COMMAND: | |
1916 | { | |
1917 | const struct macho_segment_command* seg = (struct macho_segment_command*)cmd; | |
1918 | const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command)); | |
1919 | const struct macho_section* const sectionsEnd = §ionsStart[seg->nsects]; | |
1920 | for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) { | |
1921 | const uint8_t type = sect->flags & SECTION_TYPE; | |
1922 | if ( (type == S_NON_LAZY_SYMBOL_POINTERS) || (type == S_LAZY_SYMBOL_POINTERS) ) { | |
19894a12 | 1923 | const size_t pointerCount = sect->size / sizeof(uintptr_t); |
412ebb8e | 1924 | uintptr_t* const symbolPointers = (uintptr_t*)(sect->addr + fSlide); |
19894a12 A |
1925 | for (size_t pointerIndex=0; pointerIndex < pointerCount; ++pointerIndex) { |
1926 | uintptr_t newValue = interposedAddress(context, symbolPointers[pointerIndex], this); | |
1927 | if ( newValue != symbolPointers[pointerIndex] ) | |
1928 | symbolPointers[pointerIndex] = newValue; | |
412ebb8e A |
1929 | } |
1930 | } | |
1931 | #if __i386__ | |
1932 | // i386 has special self-modifying stubs that might be prebound to "JMP rel32" that need checking | |
1933 | else if ( (type == S_SYMBOL_STUBS) && ((sect->flags & S_ATTR_SELF_MODIFYING_CODE) != 0) && (sect->reserved2 == 5) ) { | |
1934 | // check each jmp entry in this section | |
1935 | uint8_t* start = (uint8_t*)(sect->addr + this->fSlide); | |
1936 | uint8_t* end = start + sect->size; | |
1937 | for (uint8_t* entry = start; entry < end; entry += 5) { | |
1938 | if ( entry[0] == 0xE9 ) { // 0xE9 == JMP | |
1939 | uint32_t rel32 = *((uint32_t*)&entry[1]); // assume unaligned load of uint32_t is ok | |
1940 | uint32_t target = (uint32_t)&entry[5] + rel32; | |
19894a12 A |
1941 | uint32_t newTarget = interposedAddress(context, target, this); |
1942 | if ( newTarget != target ) { | |
1943 | uint32_t newRel32 = newTarget - (uint32_t)&entry[5]; | |
1944 | *((uint32_t*)&entry[1]) = newRel32; // assume unaligned store of uint32_t is ok | |
412ebb8e A |
1945 | } |
1946 | } | |
1947 | } | |
1948 | } | |
1949 | #endif | |
1950 | } | |
1951 | } | |
1952 | break; | |
1953 | } | |
1954 | cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); | |
1955 | } | |
1956 | ||
1957 | // scan external relocations | |
1958 | const uintptr_t relocBase = this->getRelocBase(); | |
1959 | const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->extreloff]); | |
1960 | const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nextrel]; | |
1961 | for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) { | |
1962 | if (reloc->r_length == RELOC_SIZE) { | |
1963 | switch(reloc->r_type) { | |
1964 | case POINTER_RELOC: | |
1965 | { | |
1966 | uintptr_t* location = ((uintptr_t*)(reloc->r_address + relocBase)); | |
19894a12 A |
1967 | uintptr_t value = *location; |
1968 | uintptr_t newValue = interposedAddress(context, value, this); | |
1969 | if ( newValue != value ) | |
1970 | *location = newValue; | |
1971 | } | |
1972 | break; | |
1973 | } | |
1974 | } | |
1975 | } | |
1976 | } | |
1977 | ||
1978 | void ImageLoaderMachOClassic::dynamicInterpose(const LinkContext& context) | |
1979 | { | |
1980 | if ( context.verboseInterposing ) | |
1981 | dyld::log("dyld: dynamic interposing %lu tuples onto image: %s\n", context.dynamicInterposeCount, this->getPath()); | |
1982 | ||
1983 | // scan indirect symbols | |
1984 | const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds; | |
1985 | const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; | |
1986 | const struct load_command* cmd = cmds; | |
1987 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
1988 | switch (cmd->cmd) { | |
1989 | case LC_SEGMENT_COMMAND: | |
1990 | { | |
1991 | const struct macho_segment_command* seg = (struct macho_segment_command*)cmd; | |
1992 | const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command)); | |
1993 | const struct macho_section* const sectionsEnd = §ionsStart[seg->nsects]; | |
1994 | for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) { | |
1995 | const uint8_t type = sect->flags & SECTION_TYPE; | |
1996 | if ( (type == S_NON_LAZY_SYMBOL_POINTERS) || (type == S_LAZY_SYMBOL_POINTERS) ) { | |
1997 | const size_t pointerCount = sect->size / sizeof(uintptr_t); | |
1998 | uintptr_t* const symbolPointers = (uintptr_t*)(sect->addr + fSlide); | |
1999 | for (size_t pointerIndex=0; pointerIndex < pointerCount; ++pointerIndex) { | |
9f83892a | 2000 | for(size_t j=0; j < context.dynamicInterposeCount; ++j) { |
19894a12 | 2001 | // replace all references to 'replacee' with 'replacement' |
9f83892a | 2002 | if ( symbolPointers[pointerIndex] == (uintptr_t)context.dynamicInterposeArray[j].replacee ) { |
19894a12 A |
2003 | if ( context.verboseInterposing ) { |
2004 | dyld::log("dyld: dynamic interposing: at %p replace %p with %p in %s\n", | |
9f83892a | 2005 | &symbolPointers[pointerIndex], context.dynamicInterposeArray[j].replacee, context.dynamicInterposeArray[j].replacement, this->getPath()); |
19894a12 | 2006 | } |
9f83892a | 2007 | symbolPointers[pointerIndex] = (uintptr_t)context.dynamicInterposeArray[j].replacement; |
19894a12 A |
2008 | } |
2009 | } | |
2010 | } | |
2011 | } | |
2012 | } | |
2013 | } | |
2014 | break; | |
2015 | } | |
2016 | cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); | |
2017 | } | |
2018 | ||
2019 | // scan external relocations | |
2020 | const uintptr_t relocBase = this->getRelocBase(); | |
2021 | const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->extreloff]); | |
2022 | const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nextrel]; | |
2023 | for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) { | |
2024 | if (reloc->r_length == RELOC_SIZE) { | |
2025 | switch(reloc->r_type) { | |
2026 | case POINTER_RELOC: | |
2027 | { | |
2028 | uintptr_t* location = ((uintptr_t*)(reloc->r_address + relocBase)); | |
2029 | for(size_t i=0; i < context.dynamicInterposeCount; ++i) { | |
412ebb8e | 2030 | // replace all references to 'replacee' with 'replacement' |
19894a12 | 2031 | if ( *location == (uintptr_t)context.dynamicInterposeArray[i].replacee ) { |
412ebb8e | 2032 | if ( context.verboseInterposing ) { |
19894a12 A |
2033 | dyld::log("dyld: dynamic interposing: at %p replace %p with %p in %s\n", |
2034 | location, context.dynamicInterposeArray[i].replacee, context.dynamicInterposeArray[i].replacement, this->getPath()); | |
412ebb8e | 2035 | } |
19894a12 | 2036 | *location = (uintptr_t)context.dynamicInterposeArray[i].replacement; |
412ebb8e A |
2037 | } |
2038 | } | |
2039 | } | |
2040 | break; | |
2041 | } | |
2042 | } | |
2043 | } | |
2044 | } | |
2045 | ||
2046 | ||
39a8cd10 A |
2047 | const char* ImageLoaderMachOClassic::findClosestSymbol(const void* addr, const void** closestAddr) const |
2048 | { | |
2049 | uintptr_t targetAddress = (uintptr_t)addr - fSlide; | |
2050 | const struct macho_nlist* bestSymbol = NULL; | |
2051 | // first walk all global symbols | |
2052 | const struct macho_nlist* const globalsStart = &fSymbolTable[fDynamicInfo->iextdefsym]; | |
2053 | const struct macho_nlist* const globalsEnd= &globalsStart[fDynamicInfo->nextdefsym]; | |
2054 | for (const struct macho_nlist* s = globalsStart; s < globalsEnd; ++s) { | |
2055 | if ( (s->n_type & N_TYPE) == N_SECT ) { | |
2056 | if ( bestSymbol == NULL ) { | |
2057 | if ( s->n_value <= targetAddress ) | |
2058 | bestSymbol = s; | |
2059 | } | |
2060 | else if ( (s->n_value <= targetAddress) && (bestSymbol->n_value < s->n_value) ) { | |
2061 | bestSymbol = s; | |
2062 | } | |
2063 | } | |
2064 | } | |
2065 | // next walk all local symbols | |
2066 | const struct macho_nlist* const localsStart = &fSymbolTable[fDynamicInfo->ilocalsym]; | |
2067 | const struct macho_nlist* const localsEnd= &localsStart[fDynamicInfo->nlocalsym]; | |
2068 | for (const struct macho_nlist* s = localsStart; s < localsEnd; ++s) { | |
2069 | if ( ((s->n_type & N_TYPE) == N_SECT) && ((s->n_type & N_STAB) == 0) ) { | |
2070 | if ( bestSymbol == NULL ) { | |
2071 | if ( s->n_value <= targetAddress ) | |
2072 | bestSymbol = s; | |
2073 | } | |
2074 | else if ( (s->n_value <= targetAddress) && (bestSymbol->n_value < s->n_value) ) { | |
2075 | bestSymbol = s; | |
2076 | } | |
2077 | } | |
2078 | } | |
2079 | if ( bestSymbol != NULL ) { | |
412ebb8e A |
2080 | #if __arm__ |
2081 | if (bestSymbol->n_desc & N_ARM_THUMB_DEF) | |
2082 | *closestAddr = (void*)((bestSymbol->n_value | 1) + fSlide); | |
2083 | else | |
2084 | *closestAddr = (void*)(bestSymbol->n_value + fSlide); | |
2085 | #else | |
39a8cd10 | 2086 | *closestAddr = (void*)(bestSymbol->n_value + fSlide); |
412ebb8e | 2087 | #endif |
39a8cd10 A |
2088 | return &fStrings[bestSymbol->n_un.n_strx]; |
2089 | } | |
2090 | return NULL; | |
2091 | } | |
2092 | ||
2093 |