]> git.saurik.com Git - apple/dyld.git/blob - src/ImageLoaderMachOCompressed.cpp
dyld-195.6.tar.gz
[apple/dyld.git] / src / ImageLoaderMachOCompressed.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26 #include <string.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/fcntl.h>
31 #include <sys/stat.h>
32 #include <sys/mman.h>
33 #include <sys/param.h>
34 #include <mach/mach.h>
35 #include <mach/thread_status.h>
36 #include <mach-o/loader.h>
37
38 #include "ImageLoaderMachOCompressed.h"
39 #include "mach-o/dyld_images.h"
40
41
42 // relocation_info.r_length field has value 3 for 64-bit executables and value 2 for 32-bit executables
43 #if __LP64__
44 #define RELOC_SIZE 3
45 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
46 #define LC_ROUTINES_COMMAND LC_ROUTINES_64
47 struct macho_segment_command : public segment_command_64 {};
48 struct macho_section : public section_64 {};
49 struct macho_routines_command : public routines_command_64 {};
50 #else
51 #define RELOC_SIZE 2
52 #define LC_SEGMENT_COMMAND LC_SEGMENT
53 #define LC_ROUTINES_COMMAND LC_ROUTINES
54 struct macho_segment_command : public segment_command {};
55 struct macho_section : public section {};
56 struct macho_routines_command : public routines_command {};
57 #endif
58
59
60 static uintptr_t read_uleb128(const uint8_t*& p, const uint8_t* end)
61 {
62 uint64_t result = 0;
63 int bit = 0;
64 do {
65 if (p == end)
66 dyld::throwf("malformed uleb128");
67
68 uint64_t slice = *p & 0x7f;
69
70 if (bit > 63)
71 dyld::throwf("uleb128 too big for uint64, bit=%d, result=0x%0llX", bit, result);
72 else {
73 result |= (slice << bit);
74 bit += 7;
75 }
76 } while (*p++ & 0x80);
77 return result;
78 }
79
80
81 static intptr_t read_sleb128(const uint8_t*& p, const uint8_t* end)
82 {
83 int64_t result = 0;
84 int bit = 0;
85 uint8_t byte;
86 do {
87 if (p == end)
88 throw "malformed sleb128";
89 byte = *p++;
90 result |= ((byte & 0x7f) << bit);
91 bit += 7;
92 } while (byte & 0x80);
93 // sign extend negative numbers
94 if ( (byte & 0x40) != 0 )
95 result |= (-1LL) << bit;
96 return result;
97 }
98
99
100 // create image for main executable
101 ImageLoaderMachOCompressed* ImageLoaderMachOCompressed::instantiateMainExecutable(const macho_header* mh, uintptr_t slide, const char* path,
102 unsigned int segCount, unsigned int libCount, const LinkContext& context)
103 {
104 ImageLoaderMachOCompressed* image = ImageLoaderMachOCompressed::instantiateStart(mh, path, segCount, libCount);
105
106 // set slide for PIE programs
107 image->setSlide(slide);
108
109 // for PIE record end of program, to know where to start loading dylibs
110 if ( slide != 0 )
111 fgNextPIEDylibAddress = (uintptr_t)image->getEnd();
112
113 image->setNeverUnload();
114 image->instantiateFinish(context);
115 image->setMapped(context);
116
117 if ( context.verboseMapping ) {
118 dyld::log("dyld: Main executable mapped %s\n", path);
119 for(unsigned int i=0, e=image->segmentCount(); i < e; ++i) {
120 const char* name = image->segName(i);
121 if ( (strcmp(name, "__PAGEZERO") == 0) || (strcmp(name, "__UNIXSTACK") == 0) )
122 dyld::log("%18s at 0x%08lX->0x%08lX\n", name, image->segPreferredLoadAddress(i), image->segPreferredLoadAddress(i)+image->segSize(i));
123 else
124 dyld::log("%18s at 0x%08lX->0x%08lX\n", name, image->segActualLoadAddress(i), image->segActualEndAddress(i));
125 }
126 }
127
128 return image;
129 }
130
131 // create image by mapping in a mach-o file
132 ImageLoaderMachOCompressed* ImageLoaderMachOCompressed::instantiateFromFile(const char* path, int fd, const uint8_t* fileData,
133 uint64_t offsetInFat, uint64_t lenInFat, const struct stat& info,
134 unsigned int segCount, unsigned int libCount,
135 const struct linkedit_data_command* codeSigCmd, const LinkContext& context)
136 {
137 ImageLoaderMachOCompressed* image = ImageLoaderMachOCompressed::instantiateStart((macho_header*)fileData, path, segCount, libCount);
138
139 try {
140 // record info about file
141 image->setFileInfo(info.st_dev, info.st_ino, info.st_mtime);
142
143 #if CODESIGNING_SUPPORT
144 // if this image is code signed, let kernel validate signature before mapping any pages from image
145 if ( codeSigCmd != NULL )
146 image->loadCodeSignature(codeSigCmd, fd, offsetInFat);
147 #endif
148
149 // mmap segments
150 image->mapSegments(fd, offsetInFat, lenInFat, info.st_size, context);
151
152 // finish construction
153 image->instantiateFinish(context);
154
155 // if path happens to be same as in LC_DYLIB_ID load command use that, otherwise malloc a copy of the path
156 const char* installName = image->getInstallPath();
157 if ( (installName != NULL) && (strcmp(installName, path) == 0) && (path[0] == '/') )
158 image->setPathUnowned(installName);
159 #if __MAC_OS_X_VERSION_MIN_REQUIRED
160 // <rdar://problem/6563887> app crashes when libSystem cannot be found
161 else if ( (installName != NULL) && (strcmp(path, "/usr/lib/libgcc_s.1.dylib") == 0) && (strcmp(installName, "/usr/lib/libSystem.B.dylib") == 0) )
162 image->setPathUnowned("/usr/lib/libSystem.B.dylib");
163 #endif
164 else if ( path[0] != '/' ) {
165 // rdar://problem/5135363 turn relative paths into absolute paths so gdb, Symbolication can later find them
166 char realPath[MAXPATHLEN];
167 if ( realpath(path, realPath) != NULL )
168 image->setPath(realPath);
169 else
170 image->setPath(path);
171 }
172 else
173 image->setPath(path);
174
175 // make sure path is stable before recording in dyld_all_image_infos
176 image->setMapped(context);
177
178 // pre-fetch content of __DATA and __LINKEDIT segment for faster launches
179 // don't do this on prebound images or if prefetching is disabled
180 if ( !context.preFetchDisabled && !image->isPrebindable()) {
181 image->preFetchDATA(fd, offsetInFat, context);
182 image->markSequentialLINKEDIT(context);
183 }
184 }
185 catch (...) {
186 // ImageLoader::setMapped() can throw an exception to block loading of image
187 // <rdar://problem/6169686> Leaked fSegmentsArray and image segments during failed dlopen_preflight
188 delete image;
189 throw;
190 }
191
192 return image;
193 }
194
195 // create image by using cached mach-o file
196 ImageLoaderMachOCompressed* ImageLoaderMachOCompressed::instantiateFromCache(const macho_header* mh, const char* path, long slide,
197 const struct stat& info, unsigned int segCount,
198 unsigned int libCount, const LinkContext& context)
199 {
200 ImageLoaderMachOCompressed* image = ImageLoaderMachOCompressed::instantiateStart(mh, path, segCount, libCount);
201 try {
202 // record info about file
203 image->setFileInfo(info.st_dev, info.st_ino, info.st_mtime);
204
205 // remember this is from shared cache and cannot be unloaded
206 image->fInSharedCache = true;
207 image->fGoodFirstSegment = true;
208 image->setNeverUnload();
209 image->setSlide(slide);
210
211 // segments already mapped in cache
212 if ( context.verboseMapping ) {
213 dyld::log("dyld: Using shared cached for %s\n", path);
214 for(unsigned int i=0; i < image->fSegmentsCount; ++i) {
215 dyld::log("%18s at 0x%08lX->0x%08lX\n", image->segName(i), image->segActualLoadAddress(i), image->segActualEndAddress(i));
216 }
217 }
218
219 image->instantiateFinish(context);
220 image->setMapped(context);
221 }
222 catch (...) {
223 // ImageLoader::setMapped() can throw an exception to block loading of image
224 // <rdar://problem/6169686> Leaked fSegmentsArray and image segments during failed dlopen_preflight
225 delete image;
226 throw;
227 }
228
229 return image;
230 }
231
232 // create image by copying an in-memory mach-o file
233 ImageLoaderMachOCompressed* ImageLoaderMachOCompressed::instantiateFromMemory(const char* moduleName, const macho_header* mh, uint64_t len,
234 unsigned int segCount, unsigned int libCount, const LinkContext& context)
235 {
236 ImageLoaderMachOCompressed* image = ImageLoaderMachOCompressed::instantiateStart(mh, moduleName, segCount, libCount);
237 try {
238 // map segments
239 if ( mh->filetype == MH_EXECUTE )
240 throw "can't load another MH_EXECUTE";
241
242 // vmcopy segments
243 image->mapSegments((const void*)mh, len, context);
244
245 // for compatibility, never unload dylibs loaded from memory
246 image->setNeverUnload();
247
248 // bundle loads need path copied
249 if ( moduleName != NULL )
250 image->setPath(moduleName);
251
252 image->instantiateFinish(context);
253 image->setMapped(context);
254 }
255 catch (...) {
256 // ImageLoader::setMapped() can throw an exception to block loading of image
257 // <rdar://problem/6169686> Leaked fSegmentsArray and image segments during failed dlopen_preflight
258 delete image;
259 throw;
260 }
261
262 return image;
263 }
264
265
266 ImageLoaderMachOCompressed::ImageLoaderMachOCompressed(const macho_header* mh, const char* path, unsigned int segCount,
267 uint32_t segOffsets[], unsigned int libCount)
268 : ImageLoaderMachO(mh, path, segCount, segOffsets, libCount), fDyldInfo(NULL)
269 {
270 }
271
272 ImageLoaderMachOCompressed::~ImageLoaderMachOCompressed()
273 {
274 // don't do clean up in ~ImageLoaderMachO() because virtual call to segmentCommandOffsets() won't work
275 destroy();
276 }
277
278
279
280 // construct ImageLoaderMachOCompressed using "placement new" with SegmentMachO objects array at end
281 ImageLoaderMachOCompressed* ImageLoaderMachOCompressed::instantiateStart(const macho_header* mh, const char* path,
282 unsigned int segCount, unsigned int libCount)
283 {
284 size_t size = sizeof(ImageLoaderMachOCompressed) + segCount * sizeof(uint32_t) + libCount * sizeof(ImageLoader*);
285 ImageLoaderMachOCompressed* allocatedSpace = static_cast<ImageLoaderMachOCompressed*>(malloc(size));
286 if ( allocatedSpace == NULL )
287 throw "malloc failed";
288 uint32_t* segOffsets = ((uint32_t*)(((uint8_t*)allocatedSpace) + sizeof(ImageLoaderMachOCompressed)));
289 bzero(&segOffsets[segCount], libCount*sizeof(void*)); // zero out lib array
290 return new (allocatedSpace) ImageLoaderMachOCompressed(mh, path, segCount, segOffsets, libCount);
291 }
292
293
294 // common code to finish initializing object
295 void ImageLoaderMachOCompressed::instantiateFinish(const LinkContext& context)
296 {
297 // now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide
298 this->parseLoadCmds();
299 }
300
301 uint32_t* ImageLoaderMachOCompressed::segmentCommandOffsets() const
302 {
303 return ((uint32_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed)));
304 }
305
306
307 ImageLoader* ImageLoaderMachOCompressed::libImage(unsigned int libIndex) const
308 {
309 const uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed) + fSegmentsCount*sizeof(uint32_t)));
310 // mask off low bits
311 return (ImageLoader*)(images[libIndex] & (-4));
312 }
313
314 bool ImageLoaderMachOCompressed::libReExported(unsigned int libIndex) const
315 {
316 const uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed) + fSegmentsCount*sizeof(uint32_t)));
317 // re-export flag is low bit
318 return ((images[libIndex] & 1) != 0);
319 }
320
321 bool ImageLoaderMachOCompressed::libIsUpward(unsigned int libIndex) const
322 {
323 const uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed) + fSegmentsCount*sizeof(uint32_t)));
324 // re-export flag is second bit
325 return ((images[libIndex] & 2) != 0);
326 }
327
328
329 void ImageLoaderMachOCompressed::setLibImage(unsigned int libIndex, ImageLoader* image, bool reExported, bool upward)
330 {
331 uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed) + fSegmentsCount*sizeof(uint32_t)));
332 uintptr_t value = (uintptr_t)image;
333 if ( reExported )
334 value |= 1;
335 if ( upward )
336 value |= 2;
337 images[libIndex] = value;
338 }
339
340
341 void ImageLoaderMachOCompressed::markFreeLINKEDIT(const LinkContext& context)
342 {
343 // mark that we are done with rebase and bind info
344 markLINKEDIT(context, MADV_FREE);
345 }
346
347 void ImageLoaderMachOCompressed::markSequentialLINKEDIT(const LinkContext& context)
348 {
349 // mark the rebase and bind info and using sequential access
350 markLINKEDIT(context, MADV_SEQUENTIAL);
351 }
352
353 void ImageLoaderMachOCompressed::markLINKEDIT(const LinkContext& context, int advise)
354 {
355 // if not loaded at preferred address, mark rebase info
356 uintptr_t start = 0;
357 if ( (fSlide != 0) && (fDyldInfo->rebase_size != 0) )
358 start = (uintptr_t)fLinkEditBase + fDyldInfo->rebase_off;
359 else if ( fDyldInfo->bind_off != 0 )
360 start = (uintptr_t)fLinkEditBase + fDyldInfo->bind_off;
361 else
362 return; // no binding info to prefetch
363
364 // end is at end of bind info
365 uintptr_t end = 0;
366 if ( fDyldInfo->bind_off != 0 )
367 end = (uintptr_t)fLinkEditBase + fDyldInfo->bind_off + fDyldInfo->bind_size;
368 else if ( fDyldInfo->rebase_off != 0 )
369 end = (uintptr_t)fLinkEditBase + fDyldInfo->rebase_off + fDyldInfo->rebase_size;
370 else
371 return;
372
373
374 // round to whole pages
375 start = start & (-4096);
376 end = (end + 4095) & (-4096);
377
378 // do nothing if only one page of rebase/bind info
379 if ( (end-start) <= 4096 )
380 return;
381
382 // tell kernel about our access to these pages
383 madvise((void*)start, end-start, advise);
384 if ( context.verboseMapping ) {
385 const char* adstr = "sequential";
386 if ( advise == MADV_FREE )
387 adstr = "free";
388 dyld::log("%18s %s 0x%0lX -> 0x%0lX for %s\n", "__LINKEDIT", adstr, start, end-1, this->getPath());
389 }
390 }
391
392
393
394 void ImageLoaderMachOCompressed::rebaseAt(const LinkContext& context, uintptr_t addr, uintptr_t slide, uint8_t type)
395 {
396 //dyld::log("0x%08lX type=%d\n", addr, type);
397 uintptr_t* locationToFix = (uintptr_t*)addr;
398 switch (type) {
399 case REBASE_TYPE_POINTER:
400 *locationToFix += slide;
401 break;
402 case REBASE_TYPE_TEXT_ABSOLUTE32:
403 *locationToFix += slide;
404 break;
405 default:
406 dyld::throwf("bad rebase type %d", type);
407 }
408 }
409
410 void ImageLoaderMachOCompressed::throwBadRebaseAddress(uintptr_t address, uintptr_t segmentEndAddress, int segmentIndex,
411 const uint8_t* startOpcodes, const uint8_t* endOpcodes, const uint8_t* pos)
412 {
413 dyld::throwf("malformed rebase opcodes (%ld/%ld): address 0x%08lX is beyond end of segment %s (0x%08lX -> 0x%08lX)",
414 (intptr_t)(pos-startOpcodes), (intptr_t)(endOpcodes-startOpcodes), address, segName(segmentIndex),
415 segActualLoadAddress(segmentIndex), segmentEndAddress);
416 }
417
418 void ImageLoaderMachOCompressed::rebase(const LinkContext& context)
419 {
420 CRSetCrashLogMessage2(this->getPath());
421 const uintptr_t slide = this->fSlide;
422 const uint8_t* const start = fLinkEditBase + fDyldInfo->rebase_off;
423 const uint8_t* const end = &start[fDyldInfo->rebase_size];
424 const uint8_t* p = start;
425
426 try {
427 uint8_t type = 0;
428 int segmentIndex = 0;
429 uintptr_t address = segActualLoadAddress(0);
430 uintptr_t segmentEndAddress = segActualEndAddress(0);
431 uint32_t count;
432 uint32_t skip;
433 bool done = false;
434 while ( !done && (p < end) ) {
435 uint8_t immediate = *p & REBASE_IMMEDIATE_MASK;
436 uint8_t opcode = *p & REBASE_OPCODE_MASK;
437 ++p;
438 switch (opcode) {
439 case REBASE_OPCODE_DONE:
440 done = true;
441 break;
442 case REBASE_OPCODE_SET_TYPE_IMM:
443 type = immediate;
444 break;
445 case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
446 segmentIndex = immediate;
447 if ( segmentIndex > fSegmentsCount )
448 dyld::throwf("REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n",
449 segmentIndex, fSegmentsCount);
450 address = segActualLoadAddress(segmentIndex) + read_uleb128(p, end);
451 segmentEndAddress = segActualEndAddress(segmentIndex);
452 break;
453 case REBASE_OPCODE_ADD_ADDR_ULEB:
454 address += read_uleb128(p, end);
455 break;
456 case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
457 address += immediate*sizeof(uintptr_t);
458 break;
459 case REBASE_OPCODE_DO_REBASE_IMM_TIMES:
460 for (int i=0; i < immediate; ++i) {
461 if ( address >= segmentEndAddress )
462 throwBadRebaseAddress(address, segmentEndAddress, segmentIndex, start, end, p);
463 rebaseAt(context, address, slide, type);
464 address += sizeof(uintptr_t);
465 }
466 fgTotalRebaseFixups += immediate;
467 break;
468 case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
469 count = read_uleb128(p, end);
470 for (uint32_t i=0; i < count; ++i) {
471 if ( address >= segmentEndAddress )
472 throwBadRebaseAddress(address, segmentEndAddress, segmentIndex, start, end, p);
473 rebaseAt(context, address, slide, type);
474 address += sizeof(uintptr_t);
475 }
476 fgTotalRebaseFixups += count;
477 break;
478 case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
479 if ( address >= segmentEndAddress )
480 throwBadRebaseAddress(address, segmentEndAddress, segmentIndex, start, end, p);
481 rebaseAt(context, address, slide, type);
482 address += read_uleb128(p, end) + sizeof(uintptr_t);
483 ++fgTotalRebaseFixups;
484 break;
485 case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
486 count = read_uleb128(p, end);
487 skip = read_uleb128(p, end);
488 for (uint32_t i=0; i < count; ++i) {
489 if ( address >= segmentEndAddress )
490 throwBadRebaseAddress(address, segmentEndAddress, segmentIndex, start, end, p);
491 rebaseAt(context, address, slide, type);
492 address += skip + sizeof(uintptr_t);
493 }
494 fgTotalRebaseFixups += count;
495 break;
496 default:
497 dyld::throwf("bad rebase opcode %d", *p);
498 }
499 }
500 }
501 catch (const char* msg) {
502 const char* newMsg = dyld::mkstringf("%s in %s", msg, this->getPath());
503 free((void*)msg);
504 throw newMsg;
505 }
506 CRSetCrashLogMessage2(NULL);
507 }
508
509 //
510 // This function is the hotspot of symbol lookup. It was pulled out of findExportedSymbol()
511 // to enable it to be re-written in assembler if needed.
512 //
513 const uint8_t* ImageLoaderMachOCompressed::trieWalk(const uint8_t* start, const uint8_t* end, const char* s)
514 {
515 const uint8_t* p = start;
516 while ( p != NULL ) {
517 uint32_t terminalSize = *p++;
518 if ( terminalSize > 127 ) {
519 // except for re-export-with-rename, all terminal sizes fit in one byte
520 --p;
521 terminalSize = read_uleb128(p, end);
522 }
523 if ( (*s == '\0') && (terminalSize != 0) ) {
524 //dyld::log("trieWalk(%p) returning %p\n", start, p);
525 return p;
526 }
527 const uint8_t* children = p + terminalSize;
528 //dyld::log("trieWalk(%p) sym=%s, terminalSize=%d, children=%p\n", start, s, terminalSize, children);
529 uint8_t childrenRemaining = *children++;
530 p = children;
531 uint32_t nodeOffset = 0;
532 for (; childrenRemaining > 0; --childrenRemaining) {
533 const char* ss = s;
534 //dyld::log("trieWalk(%p) child str=%s\n", start, (char*)p);
535 bool wrongEdge = false;
536 // scan whole edge to get to next edge
537 // if edge is longer than target symbol name, don't read past end of symbol name
538 char c = *p;
539 while ( c != '\0' ) {
540 if ( !wrongEdge ) {
541 if ( c != *ss )
542 wrongEdge = true;
543 ++ss;
544 }
545 ++p;
546 c = *p;
547 }
548 if ( wrongEdge ) {
549 // advance to next child
550 ++p; // skip over zero terminator
551 // skip over uleb128 until last byte is found
552 while ( (*p & 0x80) != 0 )
553 ++p;
554 ++p; // skil over last byte of uleb128
555 }
556 else {
557 // the symbol so far matches this edge (child)
558 // so advance to the child's node
559 ++p;
560 nodeOffset = read_uleb128(p, end);
561 s = ss;
562 //dyld::log("trieWalk() found matching edge advancing to node 0x%x\n", nodeOffset);
563 break;
564 }
565 }
566 if ( nodeOffset != 0 )
567 p = &start[nodeOffset];
568 else
569 p = NULL;
570 }
571 //dyld::log("trieWalk(%p) return NULL\n", start);
572 return NULL;
573 }
574
575
576 const ImageLoader::Symbol* ImageLoaderMachOCompressed::findExportedSymbol(const char* symbol, const ImageLoader** foundIn) const
577 {
578 //dyld::log("Compressed::findExportedSymbol(%s) in %s\n", symbol, this->getShortName());
579 if ( fDyldInfo->export_size == 0 )
580 return NULL;
581 #if LOG_BINDINGS
582 dyld::logBindings("%s: %s\n", this->getShortName(), symbol);
583 #endif
584 ++ImageLoaderMachO::fgSymbolTrieSearchs;
585 const uint8_t* start = &fLinkEditBase[fDyldInfo->export_off];
586 const uint8_t* end = &start[fDyldInfo->export_size];
587 const uint8_t* foundNodeStart = this->trieWalk(start, end, symbol);
588 if ( foundNodeStart != NULL ) {
589 const uint8_t* p = foundNodeStart;
590 const uint32_t flags = read_uleb128(p, end);
591 // found match, return pointer to terminal part of node
592 if ( flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
593 // re-export from another dylib, lookup there
594 const uint32_t ordinal = read_uleb128(p, end);
595 const char* importedName = (char*)p;
596 if ( importedName[0] == '\0' )
597 importedName = symbol;
598 if ( (ordinal > 0) && (ordinal <= libraryCount()) ) {
599 const ImageLoader* reexportedFrom = libImage(ordinal-1);
600 //dyld::log("Compressed::findExportedSymbol(), %s -> %s/%s\n", symbol, reexportedFrom->getShortName(), importedName);
601 return reexportedFrom->findExportedSymbol(importedName, true, foundIn);
602 }
603 else {
604 //dyld::throwf("bad mach-o binary, library ordinal (%u) invalid (max %u) for re-exported symbol %s in %s",
605 // ordinal, libraryCount(), symbol, this->getPath());
606 }
607 }
608 else {
609 //dyld::log("findExportedSymbol(%s) in %s found match, returning %p\n", symbol, this->getShortName(), p);
610 if ( foundIn != NULL )
611 *foundIn = (ImageLoader*)this;
612 // return pointer to terminal part of node
613 return (Symbol*)foundNodeStart;
614 }
615 }
616 return NULL;
617 }
618
619
620 bool ImageLoaderMachOCompressed::containsSymbol(const void* addr) const
621 {
622 const uint8_t* start = &fLinkEditBase[fDyldInfo->export_off];
623 const uint8_t* end = &start[fDyldInfo->export_size];
624 return ( (start <= addr) && (addr < end) );
625 }
626
627
628 uintptr_t ImageLoaderMachOCompressed::exportedSymbolAddress(const LinkContext& context, const Symbol* symbol, bool runResolver) const
629 {
630 const uint8_t* exportNode = (uint8_t*)symbol;
631 const uint8_t* exportTrieStart = fLinkEditBase + fDyldInfo->export_off;
632 const uint8_t* exportTrieEnd = exportTrieStart + fDyldInfo->export_size;
633 if ( (exportNode < exportTrieStart) || (exportNode > exportTrieEnd) )
634 throw "symbol is not in trie";
635 //dyld::log("exportedSymbolAddress(): node=%p, nodeOffset=0x%04X in %s\n", symbol, (int)((uint8_t*)symbol - exportTrieStart), this->getShortName());
636 uint32_t flags = read_uleb128(exportNode, exportTrieEnd);
637 if ( (flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_REGULAR ) {
638 if ( runResolver && (flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) ) {
639 // this node has a stub and resolver, run the resolver to get target address
640 read_uleb128(exportNode, exportTrieEnd); // skip over stub
641 typedef uintptr_t (*ResolverProc)(void);
642 ResolverProc resolver = (ResolverProc)(read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData);
643 uintptr_t result = (*resolver)();
644 if ( context.verboseBind )
645 dyld::log("dyld: resolver at %p returned 0x%08lX\n", resolver, result);
646 return result;
647 }
648 return read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData;
649 }
650 else if ( (flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL ) {
651 if ( flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
652 dyld::throwf("unsupported exported symbol kind. flags=%d at node=%p", flags, symbol);
653 return read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData;
654 }
655 else
656 dyld::throwf("unsupported exported symbol kind. flags=%d at node=%p", flags, symbol);
657 }
658
659 bool ImageLoaderMachOCompressed::exportedSymbolIsWeakDefintion(const Symbol* symbol) const
660 {
661 const uint8_t* exportNode = (uint8_t*)symbol;
662 const uint8_t* exportTrieStart = fLinkEditBase + fDyldInfo->export_off;
663 const uint8_t* exportTrieEnd = exportTrieStart + fDyldInfo->export_size;
664 if ( (exportNode < exportTrieStart) || (exportNode > exportTrieEnd) )
665 throw "symbol is not in trie";
666 uint32_t flags = read_uleb128(exportNode, exportTrieEnd);
667 return ( flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION );
668 }
669
670
671 const char* ImageLoaderMachOCompressed::exportedSymbolName(const Symbol* symbol) const
672 {
673 throw "NSNameOfSymbol() not supported with compressed LINKEDIT";
674 }
675
676 unsigned int ImageLoaderMachOCompressed::exportedSymbolCount() const
677 {
678 throw "NSSymbolDefinitionCountInObjectFileImage() not supported with compressed LINKEDIT";
679 }
680
681 const ImageLoader::Symbol* ImageLoaderMachOCompressed::exportedSymbolIndexed(unsigned int index) const
682 {
683 throw "NSSymbolDefinitionNameInObjectFileImage() not supported with compressed LINKEDIT";
684 }
685
686 unsigned int ImageLoaderMachOCompressed::importedSymbolCount() const
687 {
688 throw "NSSymbolReferenceCountInObjectFileImage() not supported with compressed LINKEDIT";
689 }
690
691 const ImageLoader::Symbol* ImageLoaderMachOCompressed::importedSymbolIndexed(unsigned int index) const
692 {
693 throw "NSSymbolReferenceCountInObjectFileImage() not supported with compressed LINKEDIT";
694 }
695
696 const char* ImageLoaderMachOCompressed::importedSymbolName(const Symbol* symbol) const
697 {
698 throw "NSSymbolReferenceNameInObjectFileImage() not supported with compressed LINKEDIT";
699 }
700
701
702
703 uintptr_t ImageLoaderMachOCompressed::resolveFlat(const LinkContext& context, const char* symbolName, bool weak_import,
704 bool runResolver, const ImageLoader** foundIn)
705 {
706 const Symbol* sym;
707 if ( context.flatExportFinder(symbolName, &sym, foundIn) ) {
708 if ( (*foundIn != this) && !(*foundIn)->neverUnload() )
709 this->addDynamicReference(*foundIn);
710 return (*foundIn)->getExportedSymbolAddress(sym, context, this, runResolver);
711 }
712 // if a bundle is loaded privately the above will not find its exports
713 if ( this->isBundle() && this->hasHiddenExports() ) {
714 // look in self for needed symbol
715 sym = this->ImageLoaderMachO::findExportedSymbol(symbolName, false, foundIn);
716 if ( sym != NULL )
717 return (*foundIn)->getExportedSymbolAddress(sym, context, this, runResolver);
718 }
719 if ( weak_import ) {
720 // definition can't be found anywhere, ok because it is weak, just return 0
721 return 0;
722 }
723 throwSymbolNotFound(context, symbolName, this->getPath(), "flat namespace");
724 }
725
726
727 uintptr_t ImageLoaderMachOCompressed::resolveTwolevel(const LinkContext& context, const ImageLoader* targetImage, bool weak_import,
728 const char* symbolName, bool runResolver, const ImageLoader** foundIn)
729 {
730 // two level lookup
731 const Symbol* sym = targetImage->findExportedSymbol(symbolName, true, foundIn);
732 if ( sym != NULL ) {
733 return (*foundIn)->getExportedSymbolAddress(sym, context, this, runResolver);
734 }
735
736 if ( weak_import ) {
737 // definition can't be found anywhere, ok because it is weak, just return 0
738 return 0;
739 }
740
741 // nowhere to be found
742 throwSymbolNotFound(context, symbolName, this->getPath(), targetImage->getPath());
743 }
744
745
746 uintptr_t ImageLoaderMachOCompressed::resolve(const LinkContext& context, const char* symbolName,
747 uint8_t symboFlags, int libraryOrdinal, const ImageLoader** targetImage,
748 LastLookup* last, bool runResolver)
749 {
750 *targetImage = NULL;
751
752 // only clients that benefit from caching last lookup pass in a LastLookup struct
753 if ( last != NULL ) {
754 if ( (last->ordinal == libraryOrdinal)
755 && (last->flags == symboFlags)
756 && (last->name == symbolName) ) {
757 *targetImage = last->foundIn;
758 return last->result;
759 }
760 }
761
762 bool weak_import = (symboFlags & BIND_SYMBOL_FLAGS_WEAK_IMPORT);
763 uintptr_t symbolAddress;
764 if ( context.bindFlat || (libraryOrdinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP) ) {
765 symbolAddress = this->resolveFlat(context, symbolName, weak_import, runResolver, targetImage);
766 }
767 else {
768 if ( libraryOrdinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE ) {
769 *targetImage = context.mainExecutable;
770 }
771 else if ( libraryOrdinal == BIND_SPECIAL_DYLIB_SELF ) {
772 *targetImage = this;
773 }
774 else if ( libraryOrdinal <= 0 ) {
775 dyld::throwf("bad mach-o binary, unknown special library ordinal (%u) too big for symbol %s in %s",
776 libraryOrdinal, symbolName, this->getPath());
777 }
778 else if ( (unsigned)libraryOrdinal <= libraryCount() ) {
779 *targetImage = libImage(libraryOrdinal-1);
780 }
781 else {
782 dyld::throwf("bad mach-o binary, library ordinal (%u) too big (max %u) for symbol %s in %s",
783 libraryOrdinal, libraryCount(), symbolName, this->getPath());
784 }
785 if ( *targetImage == NULL ) {
786 if ( weak_import ) {
787 // if target library not loaded and reference is weak or library is weak return 0
788 symbolAddress = 0;
789 }
790 else {
791 dyld::throwf("can't resolve symbol %s in %s because dependent dylib #%d could not be loaded",
792 symbolName, this->getPath(), libraryOrdinal);
793 }
794 }
795 else {
796 symbolAddress = resolveTwolevel(context, *targetImage, weak_import, symbolName, runResolver, targetImage);
797 }
798 }
799
800 // save off lookup results if client wants
801 if ( last != NULL ) {
802 last->ordinal = libraryOrdinal;
803 last->flags = symboFlags;
804 last->name = symbolName;
805 last->foundIn = *targetImage;
806 last->result = symbolAddress;
807 }
808
809 return symbolAddress;
810 }
811
812 uintptr_t ImageLoaderMachOCompressed::bindAt(const LinkContext& context, uintptr_t addr, uint8_t type, const char* symbolName,
813 uint8_t symboFlags, intptr_t addend, int libraryOrdinal, const char* msg,
814 LastLookup* last, bool runResolver)
815 {
816 const ImageLoader* targetImage;
817 uintptr_t symbolAddress;
818
819 // resolve symbol
820 symbolAddress = this->resolve(context, symbolName, symboFlags, libraryOrdinal, &targetImage, last, runResolver);
821
822 // do actual update
823 return this->bindLocation(context, addr, symbolAddress, targetImage, type, symbolName, addend, msg);
824 }
825
826 void ImageLoaderMachOCompressed::throwBadBindingAddress(uintptr_t address, uintptr_t segmentEndAddress, int segmentIndex,
827 const uint8_t* startOpcodes, const uint8_t* endOpcodes, const uint8_t* pos)
828 {
829 dyld::throwf("malformed binding opcodes (%ld/%ld): address 0x%08lX is beyond end of segment %s (0x%08lX -> 0x%08lX)",
830 (intptr_t)(pos-startOpcodes), (intptr_t)(endOpcodes-startOpcodes), address, segName(segmentIndex),
831 segActualLoadAddress(segmentIndex), segmentEndAddress);
832 }
833
834
835 void ImageLoaderMachOCompressed::doBind(const LinkContext& context, bool forceLazysBound)
836 {
837 CRSetCrashLogMessage2(this->getPath());
838
839 // if prebound and loaded at prebound address, and all libraries are same as when this was prebound, then no need to bind
840 // note: flat-namespace binaries need to have imports rebound (even if correctly prebound)
841 if ( this->usablePrebinding(context) ) {
842 // don't need to bind
843 }
844 else {
845
846 #if TEXT_RELOC_SUPPORT
847 // if there are __TEXT fixups, temporarily make __TEXT writable
848 if ( fTextSegmentBinds )
849 this->makeTextSegmentWritable(context, true);
850 #endif
851
852 // run through all binding opcodes
853 eachBind(context, &ImageLoaderMachOCompressed::bindAt);
854
855 #if TEXT_RELOC_SUPPORT
856 // if there were __TEXT fixups, restore write protection
857 if ( fTextSegmentBinds )
858 this->makeTextSegmentWritable(context, false);
859 #endif
860
861 // if this image is in the shared cache, but depends on something no longer in the shared cache,
862 // there is no way to reset the lazy pointers, so force bind them now
863 if ( forceLazysBound || fInSharedCache )
864 this->doBindJustLazies(context);
865
866 // this image is in cache, but something below it is not. If
867 // this image has lazy pointer to a resolver function, then
868 // the stub may have been altered to point to a shared lazy pointer.
869 if ( fInSharedCache )
870 this->updateOptimizedLazyPointers(context);
871
872 // tell kernel we are done with chunks of LINKEDIT
873 if ( !context.preFetchDisabled )
874 this->markFreeLINKEDIT(context);
875 }
876
877 // set up dyld entry points in image
878 // do last so flat main executables will have __dyld or __program_vars set up
879 this->setupLazyPointerHandler(context);
880 CRSetCrashLogMessage2(NULL);
881 }
882
883
884 void ImageLoaderMachOCompressed::doBindJustLazies(const LinkContext& context)
885 {
886 eachLazyBind(context, &ImageLoaderMachOCompressed::bindAt);
887 }
888
889 void ImageLoaderMachOCompressed::eachBind(const LinkContext& context, bind_handler handler)
890 {
891 try {
892 uint8_t type = 0;
893 int segmentIndex = 0;
894 uintptr_t address = segActualLoadAddress(0);
895 uintptr_t segmentEndAddress = segActualEndAddress(0);
896 const char* symbolName = NULL;
897 uint8_t symboFlags = 0;
898 int libraryOrdinal = 0;
899 intptr_t addend = 0;
900 uint32_t count;
901 uint32_t skip;
902 LastLookup last = { 0, 0, NULL, 0, NULL };
903 const uint8_t* const start = fLinkEditBase + fDyldInfo->bind_off;
904 const uint8_t* const end = &start[fDyldInfo->bind_size];
905 const uint8_t* p = start;
906 bool done = false;
907 while ( !done && (p < end) ) {
908 uint8_t immediate = *p & BIND_IMMEDIATE_MASK;
909 uint8_t opcode = *p & BIND_OPCODE_MASK;
910 ++p;
911 switch (opcode) {
912 case BIND_OPCODE_DONE:
913 done = true;
914 break;
915 case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
916 libraryOrdinal = immediate;
917 break;
918 case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
919 libraryOrdinal = read_uleb128(p, end);
920 break;
921 case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
922 // the special ordinals are negative numbers
923 if ( immediate == 0 )
924 libraryOrdinal = 0;
925 else {
926 int8_t signExtended = BIND_OPCODE_MASK | immediate;
927 libraryOrdinal = signExtended;
928 }
929 break;
930 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
931 symbolName = (char*)p;
932 symboFlags = immediate;
933 while (*p != '\0')
934 ++p;
935 ++p;
936 break;
937 case BIND_OPCODE_SET_TYPE_IMM:
938 type = immediate;
939 break;
940 case BIND_OPCODE_SET_ADDEND_SLEB:
941 addend = read_sleb128(p, end);
942 break;
943 case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
944 segmentIndex = immediate;
945 if ( segmentIndex > fSegmentsCount )
946 dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n",
947 segmentIndex, fSegmentsCount);
948 address = segActualLoadAddress(segmentIndex) + read_uleb128(p, end);
949 segmentEndAddress = segActualEndAddress(segmentIndex);
950 break;
951 case BIND_OPCODE_ADD_ADDR_ULEB:
952 address += read_uleb128(p, end);
953 break;
954 case BIND_OPCODE_DO_BIND:
955 if ( address >= segmentEndAddress )
956 throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
957 (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
958 address += sizeof(intptr_t);
959 break;
960 case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
961 if ( address >= segmentEndAddress )
962 throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
963 (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
964 address += read_uleb128(p, end) + sizeof(intptr_t);
965 break;
966 case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
967 if ( address >= segmentEndAddress )
968 throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
969 (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
970 address += immediate*sizeof(intptr_t) + sizeof(intptr_t);
971 break;
972 case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
973 count = read_uleb128(p, end);
974 skip = read_uleb128(p, end);
975 for (uint32_t i=0; i < count; ++i) {
976 if ( address >= segmentEndAddress )
977 throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
978 (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
979 address += skip + sizeof(intptr_t);
980 }
981 break;
982 default:
983 dyld::throwf("bad bind opcode %d in bind info", *p);
984 }
985 }
986 }
987 catch (const char* msg) {
988 const char* newMsg = dyld::mkstringf("%s in %s", msg, this->getPath());
989 free((void*)msg);
990 throw newMsg;
991 }
992 }
993
994 void ImageLoaderMachOCompressed::eachLazyBind(const LinkContext& context, bind_handler handler)
995 {
996 try {
997 uint8_t type = BIND_TYPE_POINTER;
998 int segmentIndex = 0;
999 uintptr_t address = segActualLoadAddress(0);
1000 uintptr_t segmentEndAddress = segActualEndAddress(0);
1001 const char* symbolName = NULL;
1002 uint8_t symboFlags = 0;
1003 int libraryOrdinal = 0;
1004 intptr_t addend = 0;
1005 const uint8_t* const start = fLinkEditBase + fDyldInfo->lazy_bind_off;
1006 const uint8_t* const end = &start[fDyldInfo->lazy_bind_size];
1007 const uint8_t* p = start;
1008 bool done = false;
1009 while ( !done && (p < end) ) {
1010 uint8_t immediate = *p & BIND_IMMEDIATE_MASK;
1011 uint8_t opcode = *p & BIND_OPCODE_MASK;
1012 ++p;
1013 switch (opcode) {
1014 case BIND_OPCODE_DONE:
1015 // there is BIND_OPCODE_DONE at end of each lazy bind, don't stop until end of whole sequence
1016 break;
1017 case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
1018 libraryOrdinal = immediate;
1019 break;
1020 case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
1021 libraryOrdinal = read_uleb128(p, end);
1022 break;
1023 case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
1024 // the special ordinals are negative numbers
1025 if ( immediate == 0 )
1026 libraryOrdinal = 0;
1027 else {
1028 int8_t signExtended = BIND_OPCODE_MASK | immediate;
1029 libraryOrdinal = signExtended;
1030 }
1031 break;
1032 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
1033 symbolName = (char*)p;
1034 symboFlags = immediate;
1035 while (*p != '\0')
1036 ++p;
1037 ++p;
1038 break;
1039 case BIND_OPCODE_SET_TYPE_IMM:
1040 type = immediate;
1041 break;
1042 case BIND_OPCODE_SET_ADDEND_SLEB:
1043 addend = read_sleb128(p, end);
1044 break;
1045 case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
1046 segmentIndex = immediate;
1047 if ( segmentIndex > fSegmentsCount )
1048 dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n",
1049 segmentIndex, fSegmentsCount);
1050 address = segActualLoadAddress(segmentIndex) + read_uleb128(p, end);
1051 segmentEndAddress = segActualEndAddress(segmentIndex);
1052 break;
1053 case BIND_OPCODE_ADD_ADDR_ULEB:
1054 address += read_uleb128(p, end);
1055 break;
1056 case BIND_OPCODE_DO_BIND:
1057 if ( address >= segmentEndAddress )
1058 throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
1059 (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "lazy forced", NULL, true);
1060 address += sizeof(intptr_t);
1061 break;
1062 case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
1063 case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
1064 case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
1065 default:
1066 dyld::throwf("bad lazy bind opcode %d", *p);
1067 }
1068 }
1069 }
1070
1071 catch (const char* msg) {
1072 const char* newMsg = dyld::mkstringf("%s in %s", msg, this->getPath());
1073 free((void*)msg);
1074 throw newMsg;
1075 }
1076 }
1077
1078 // A program built targeting 10.5 will have hybrid stubs. When used with weak symbols
1079 // the classic lazy loader is used even when running on 10.6
1080 uintptr_t ImageLoaderMachOCompressed::doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context)
1081 {
1082 // only works with compressed LINKEDIT if classic symbol table is also present
1083 const macho_nlist* symbolTable = NULL;
1084 const char* symbolTableStrings = NULL;
1085 const dysymtab_command* dynSymbolTable = NULL;
1086 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1087 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1088 const struct load_command* cmd = cmds;
1089 for (uint32_t i = 0; i < cmd_count; ++i) {
1090 switch (cmd->cmd) {
1091 case LC_SYMTAB:
1092 {
1093 const struct symtab_command* symtab = (struct symtab_command*)cmd;
1094 symbolTableStrings = (const char*)&fLinkEditBase[symtab->stroff];
1095 symbolTable = (macho_nlist*)(&fLinkEditBase[symtab->symoff]);
1096 }
1097 break;
1098 case LC_DYSYMTAB:
1099 dynSymbolTable = (struct dysymtab_command*)cmd;
1100 break;
1101 }
1102 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1103 }
1104 // no symbol table => no lookup by address
1105 if ( (symbolTable == NULL) || (dynSymbolTable == NULL) )
1106 dyld::throwf("classic lazy binding used with compressed LINKEDIT at %p in image %s", lazyPointer, this->getPath());
1107
1108 // scan for all lazy-pointer sections
1109 const bool twoLevel = this->usesTwoLevelNameSpace();
1110 const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[dynSymbolTable->indirectsymoff];
1111 cmd = cmds;
1112 for (uint32_t i = 0; i < cmd_count; ++i) {
1113 switch (cmd->cmd) {
1114 case LC_SEGMENT_COMMAND:
1115 {
1116 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1117 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1118 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
1119 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
1120 const uint8_t type = sect->flags & SECTION_TYPE;
1121 uint32_t symbolIndex = INDIRECT_SYMBOL_LOCAL;
1122 if ( type == S_LAZY_SYMBOL_POINTERS ) {
1123 const uint32_t pointerCount = sect->size / sizeof(uintptr_t);
1124 uintptr_t* const symbolPointers = (uintptr_t*)(sect->addr + fSlide);
1125 if ( (lazyPointer >= symbolPointers) && (lazyPointer < &symbolPointers[pointerCount]) ) {
1126 const uint32_t indirectTableOffset = sect->reserved1;
1127 const uint32_t lazyIndex = lazyPointer - symbolPointers;
1128 symbolIndex = indirectTable[indirectTableOffset + lazyIndex];
1129 }
1130 }
1131 if ( (symbolIndex != INDIRECT_SYMBOL_ABS) && (symbolIndex != INDIRECT_SYMBOL_LOCAL) ) {
1132 const macho_nlist* symbol = &symbolTable[symbolIndex];
1133 const char* symbolName = &symbolTableStrings[symbol->n_un.n_strx];
1134 int libraryOrdinal = GET_LIBRARY_ORDINAL(symbol->n_desc);
1135 if ( !twoLevel || context.bindFlat )
1136 libraryOrdinal = BIND_SPECIAL_DYLIB_FLAT_LOOKUP;
1137 uintptr_t ptrToBind = (uintptr_t)lazyPointer;
1138 uintptr_t symbolAddr = bindAt(context, ptrToBind, BIND_TYPE_POINTER, symbolName, 0, 0, libraryOrdinal, "lazy ", NULL);
1139 ++fgTotalLazyBindFixups;
1140 return symbolAddr;
1141 }
1142 }
1143 }
1144 break;
1145 }
1146 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1147 }
1148 dyld::throwf("lazy pointer not found at address %p in image %s", lazyPointer, this->getPath());
1149 }
1150
1151
1152 uintptr_t ImageLoaderMachOCompressed::doBindFastLazySymbol(uint32_t lazyBindingInfoOffset, const LinkContext& context,
1153 void (*lock)(), void (*unlock)())
1154 {
1155 // <rdar://problem/8663923> race condition with flat-namespace lazy binding
1156 if ( this->usesTwoLevelNameSpace() ) {
1157 // two-level namespace lookup does not require lock because dependents can't be unloaded before this image
1158 }
1159 else {
1160 // acquire dyld global lock
1161 if ( lock != NULL )
1162 lock();
1163 }
1164
1165 const uint8_t* const start = fLinkEditBase + fDyldInfo->lazy_bind_off;
1166 const uint8_t* const end = &start[fDyldInfo->lazy_bind_size];
1167 if ( lazyBindingInfoOffset > fDyldInfo->lazy_bind_size ) {
1168 dyld::throwf("fast lazy bind offset out of range (%u, max=%u) in image %s",
1169 lazyBindingInfoOffset, fDyldInfo->lazy_bind_size, this->getPath());
1170 }
1171
1172 uint8_t type = BIND_TYPE_POINTER;
1173 uintptr_t address = 0;
1174 const char* symbolName = NULL;
1175 uint8_t symboFlags = 0;
1176 int libraryOrdinal = 0;
1177 bool done = false;
1178 uintptr_t result = 0;
1179 const uint8_t* p = &start[lazyBindingInfoOffset];
1180 while ( !done && (p < end) ) {
1181 uint8_t immediate = *p & BIND_IMMEDIATE_MASK;
1182 uint8_t opcode = *p & BIND_OPCODE_MASK;
1183 ++p;
1184 switch (opcode) {
1185 case BIND_OPCODE_DONE:
1186 done = true;
1187 break;
1188 case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
1189 libraryOrdinal = immediate;
1190 break;
1191 case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
1192 libraryOrdinal = read_uleb128(p, end);
1193 break;
1194 case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
1195 // the special ordinals are negative numbers
1196 if ( immediate == 0 )
1197 libraryOrdinal = 0;
1198 else {
1199 int8_t signExtended = BIND_OPCODE_MASK | immediate;
1200 libraryOrdinal = signExtended;
1201 }
1202 break;
1203 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
1204 symbolName = (char*)p;
1205 symboFlags = immediate;
1206 while (*p != '\0')
1207 ++p;
1208 ++p;
1209 break;
1210 case BIND_OPCODE_SET_TYPE_IMM:
1211 type = immediate;
1212 break;
1213 case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
1214 if ( immediate > fSegmentsCount )
1215 dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n",
1216 immediate, fSegmentsCount);
1217 address = segActualLoadAddress(immediate) + read_uleb128(p, end);
1218 break;
1219 case BIND_OPCODE_DO_BIND:
1220
1221
1222 result = this->bindAt(context, address, type, symbolName, 0, 0, libraryOrdinal, "lazy ", NULL, true);
1223 break;
1224 case BIND_OPCODE_SET_ADDEND_SLEB:
1225 case BIND_OPCODE_ADD_ADDR_ULEB:
1226 case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
1227 case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
1228 case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
1229 default:
1230 dyld::throwf("bad lazy bind opcode %d", *p);
1231 }
1232 }
1233
1234 if ( !this->usesTwoLevelNameSpace() ) {
1235 // release dyld global lock
1236 if ( unlock != NULL )
1237 unlock();
1238 }
1239 return result;
1240 }
1241
1242 void ImageLoaderMachOCompressed::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder)
1243 {
1244 it.image = this;
1245 it.symbolName = " ";
1246 it.loadOrder = loadOrder;
1247 it.weakSymbol = false;
1248 it.symbolMatches = false;
1249 it.done = false;
1250 it.curIndex = 0;
1251 it.endIndex = this->fDyldInfo->weak_bind_size;
1252 it.address = 0;
1253 it.type = 0;
1254 it.addend = 0;
1255 }
1256
1257
1258 bool ImageLoaderMachOCompressed::incrementCoalIterator(CoalIterator& it)
1259 {
1260 if ( it.done )
1261 return false;
1262
1263 if ( this->fDyldInfo->weak_bind_size == 0 ) {
1264 /// hmmm, ld set MH_WEAK_DEFINES or MH_BINDS_TO_WEAK, but there is no weak binding info
1265 it.done = true;
1266 it.symbolName = "~~~";
1267 return true;
1268 }
1269 const uint8_t* start = fLinkEditBase + fDyldInfo->weak_bind_off;
1270 const uint8_t* p = start + it.curIndex;
1271 const uint8_t* end = fLinkEditBase + fDyldInfo->weak_bind_off + this->fDyldInfo->weak_bind_size;
1272 uint32_t count;
1273 uint32_t skip;
1274 while ( p < end ) {
1275 uint8_t immediate = *p & BIND_IMMEDIATE_MASK;
1276 uint8_t opcode = *p & BIND_OPCODE_MASK;
1277 ++p;
1278 switch (opcode) {
1279 case BIND_OPCODE_DONE:
1280 it.done = true;
1281 it.curIndex = p - start;
1282 it.symbolName = "~~~"; // sorts to end
1283 return true;
1284 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
1285 it.symbolName = (char*)p;
1286 it.weakSymbol = ((immediate & BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) == 0);
1287 it.symbolMatches = false;
1288 while (*p != '\0')
1289 ++p;
1290 ++p;
1291 it.curIndex = p - start;
1292 return false;
1293 case BIND_OPCODE_SET_TYPE_IMM:
1294 it.type = immediate;
1295 break;
1296 case BIND_OPCODE_SET_ADDEND_SLEB:
1297 it.addend = read_sleb128(p, end);
1298 break;
1299 case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
1300 if ( immediate > fSegmentsCount )
1301 dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n",
1302 immediate, fSegmentsCount);
1303 it.address = segActualLoadAddress(immediate) + read_uleb128(p, end);
1304 break;
1305 case BIND_OPCODE_ADD_ADDR_ULEB:
1306 it.address += read_uleb128(p, end);
1307 break;
1308 case BIND_OPCODE_DO_BIND:
1309 it.address += sizeof(intptr_t);
1310 break;
1311 case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
1312 it.address += read_uleb128(p, end) + sizeof(intptr_t);
1313 break;
1314 case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
1315 it.address += immediate*sizeof(intptr_t) + sizeof(intptr_t);
1316 break;
1317 case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
1318 count = read_uleb128(p, end);
1319 skip = read_uleb128(p, end);
1320 for (uint32_t i=0; i < count; ++i) {
1321 it.address += skip + sizeof(intptr_t);
1322 }
1323 break;
1324 default:
1325 dyld::throwf("bad weak bind opcode %d", *p);
1326 }
1327 }
1328 /// hmmm, BIND_OPCODE_DONE is missing...
1329 it.done = true;
1330 it.symbolName = "~~~";
1331 //dyld::log("missing BIND_OPCODE_DONE for image %s\n", this->getPath());
1332 return true;
1333 }
1334
1335 uintptr_t ImageLoaderMachOCompressed::getAddressCoalIterator(CoalIterator& it, const LinkContext& context)
1336 {
1337 //dyld::log("looking for %s in %s\n", it.symbolName, this->getPath());
1338 const ImageLoader* foundIn = NULL;
1339 const ImageLoader::Symbol* sym = this->findExportedSymbol(it.symbolName, &foundIn);
1340 if ( sym != NULL ) {
1341 //dyld::log("sym=%p, foundIn=%p\n", sym, foundIn);
1342 return foundIn->getExportedSymbolAddress(sym, context, this);
1343 }
1344 return 0;
1345 }
1346
1347
1348 void ImageLoaderMachOCompressed::updateUsesCoalIterator(CoalIterator& it, uintptr_t value, ImageLoader* targetImage, const LinkContext& context)
1349 {
1350 // <rdar://problem/6570879> weak binding done too early with inserted libraries
1351 if ( this->getState() < dyld_image_state_bound )
1352 return;
1353
1354 const uint8_t* start = fLinkEditBase + fDyldInfo->weak_bind_off;
1355 const uint8_t* p = start + it.curIndex;
1356 const uint8_t* end = fLinkEditBase + fDyldInfo->weak_bind_off + this->fDyldInfo->weak_bind_size;
1357
1358 uint8_t type = it.type;
1359 uintptr_t address = it.address;
1360 const char* symbolName = it.symbolName;
1361 intptr_t addend = it.addend;
1362 uint32_t count;
1363 uint32_t skip;
1364 bool done = false;
1365 bool boundSomething = false;
1366 while ( !done && (p < end) ) {
1367 uint8_t immediate = *p & BIND_IMMEDIATE_MASK;
1368 uint8_t opcode = *p & BIND_OPCODE_MASK;
1369 ++p;
1370 switch (opcode) {
1371 case BIND_OPCODE_DONE:
1372 done = true;
1373 break;
1374 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
1375 done = true;
1376 break;
1377 case BIND_OPCODE_SET_TYPE_IMM:
1378 type = immediate;
1379 break;
1380 case BIND_OPCODE_SET_ADDEND_SLEB:
1381 addend = read_sleb128(p, end);
1382 break;
1383 case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
1384 if ( immediate > fSegmentsCount )
1385 dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n",
1386 immediate, fSegmentsCount);
1387 address = segActualLoadAddress(immediate) + read_uleb128(p, end);
1388 break;
1389 case BIND_OPCODE_ADD_ADDR_ULEB:
1390 address += read_uleb128(p, end);
1391 break;
1392 case BIND_OPCODE_DO_BIND:
1393 bindLocation(context, address, value, targetImage, type, symbolName, addend, "weak ");
1394 boundSomething = true;
1395 address += sizeof(intptr_t);
1396 break;
1397 case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
1398 bindLocation(context, address, value, targetImage, type, symbolName, addend, "weak ");
1399 boundSomething = true;
1400 address += read_uleb128(p, end) + sizeof(intptr_t);
1401 break;
1402 case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
1403 bindLocation(context, address, value, targetImage, type, symbolName, addend, "weak ");
1404 boundSomething = true;
1405 address += immediate*sizeof(intptr_t) + sizeof(intptr_t);
1406 break;
1407 case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
1408 count = read_uleb128(p, end);
1409 skip = read_uleb128(p, end);
1410 for (uint32_t i=0; i < count; ++i) {
1411 bindLocation(context, address, value, targetImage, type, symbolName, addend, "weak ");
1412 boundSomething = true;
1413 address += skip + sizeof(intptr_t);
1414 }
1415 break;
1416 default:
1417 dyld::throwf("bad bind opcode %d in weak binding info", *p);
1418 }
1419 }
1420 if ( boundSomething && (targetImage != this) && !targetImage->neverUnload() )
1421 this->addDynamicReference(targetImage);
1422 }
1423
1424 uintptr_t ImageLoaderMachOCompressed::interposeAt(const LinkContext& context, uintptr_t addr, uint8_t type, const char*,
1425 uint8_t, intptr_t, int, const char*, LastLookup*, bool runResolver)
1426 {
1427 if ( type == BIND_TYPE_POINTER ) {
1428 uintptr_t* fixupLocation = (uintptr_t*)addr;
1429 uintptr_t value = *fixupLocation;
1430 for (std::vector<InterposeTuple>::iterator it=fgInterposingTuples.begin(); it != fgInterposingTuples.end(); it++) {
1431 // replace all references to 'replacee' with 'replacement'
1432 if ( (value == it->replacee) && (this != it->replacementImage) ) {
1433 if ( context.verboseInterposing ) {
1434 dyld::log("dyld: interposing: at %p replace 0x%lX with 0x%lX in %s\n",
1435 fixupLocation, it->replacee, it->replacement, this->getPath());
1436 }
1437 *fixupLocation = it->replacement;
1438 }
1439 }
1440 }
1441 return 0;
1442 }
1443
1444 void ImageLoaderMachOCompressed::doInterpose(const LinkContext& context)
1445 {
1446 if ( context.verboseInterposing )
1447 dyld::log("dyld: interposing %lu tuples onto image: %s\n", fgInterposingTuples.size(), this->getPath());
1448
1449 // update prebound symbols
1450 eachBind(context, &ImageLoaderMachOCompressed::interposeAt);
1451 eachLazyBind(context, &ImageLoaderMachOCompressed::interposeAt);
1452 }
1453
1454
1455
1456
1457 const char* ImageLoaderMachOCompressed::findClosestSymbol(const void* addr, const void** closestAddr) const
1458 {
1459 // called by dladdr()
1460 // only works with compressed LINKEDIT if classic symbol table is also present
1461 const macho_nlist* symbolTable = NULL;
1462 const char* symbolTableStrings = NULL;
1463 const dysymtab_command* dynSymbolTable = NULL;
1464 const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
1465 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1466 const struct load_command* cmd = cmds;
1467 for (uint32_t i = 0; i < cmd_count; ++i) {
1468 switch (cmd->cmd) {
1469 case LC_SYMTAB:
1470 {
1471 const struct symtab_command* symtab = (struct symtab_command*)cmd;
1472 symbolTableStrings = (const char*)&fLinkEditBase[symtab->stroff];
1473 symbolTable = (macho_nlist*)(&fLinkEditBase[symtab->symoff]);
1474 }
1475 break;
1476 case LC_DYSYMTAB:
1477 dynSymbolTable = (struct dysymtab_command*)cmd;
1478 break;
1479 }
1480 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1481 }
1482 // no symbol table => no lookup by address
1483 if ( (symbolTable == NULL) || (dynSymbolTable == NULL) )
1484 return NULL;
1485
1486 uintptr_t targetAddress = (uintptr_t)addr - fSlide;
1487 const struct macho_nlist* bestSymbol = NULL;
1488 // first walk all global symbols
1489 const struct macho_nlist* const globalsStart = &symbolTable[dynSymbolTable->iextdefsym];
1490 const struct macho_nlist* const globalsEnd= &globalsStart[dynSymbolTable->nextdefsym];
1491 for (const struct macho_nlist* s = globalsStart; s < globalsEnd; ++s) {
1492 if ( (s->n_type & N_TYPE) == N_SECT ) {
1493 if ( bestSymbol == NULL ) {
1494 if ( s->n_value <= targetAddress )
1495 bestSymbol = s;
1496 }
1497 else if ( (s->n_value <= targetAddress) && (bestSymbol->n_value < s->n_value) ) {
1498 bestSymbol = s;
1499 }
1500 }
1501 }
1502 // next walk all local symbols
1503 const struct macho_nlist* const localsStart = &symbolTable[dynSymbolTable->ilocalsym];
1504 const struct macho_nlist* const localsEnd= &localsStart[dynSymbolTable->nlocalsym];
1505 for (const struct macho_nlist* s = localsStart; s < localsEnd; ++s) {
1506 if ( ((s->n_type & N_TYPE) == N_SECT) && ((s->n_type & N_STAB) == 0) ) {
1507 if ( bestSymbol == NULL ) {
1508 if ( s->n_value <= targetAddress )
1509 bestSymbol = s;
1510 }
1511 else if ( (s->n_value <= targetAddress) && (bestSymbol->n_value < s->n_value) ) {
1512 bestSymbol = s;
1513 }
1514 }
1515 }
1516 if ( bestSymbol != NULL ) {
1517 #if __arm__
1518 if (bestSymbol->n_desc & N_ARM_THUMB_DEF)
1519 *closestAddr = (void*)((bestSymbol->n_value | 1) + fSlide);
1520 else
1521 *closestAddr = (void*)(bestSymbol->n_value + fSlide);
1522 #else
1523 *closestAddr = (void*)(bestSymbol->n_value + fSlide);
1524 #endif
1525 return &symbolTableStrings[bestSymbol->n_un.n_strx];
1526 }
1527 return NULL;
1528 }
1529
1530
1531 #if PREBOUND_IMAGE_SUPPORT
1532 void ImageLoaderMachOCompressed::resetPreboundLazyPointers(const LinkContext& context)
1533 {
1534 // no way to back off a prebound compress image
1535 }
1536 #endif
1537
1538
1539 #if __arm__ || __x86_64__
1540 void ImageLoaderMachOCompressed::updateAlternateLazyPointer(uint8_t* stub, void** originalLazyPointerAddr)
1541 {
1542 #if __arm__
1543 uint32_t* instructions = (uint32_t*)stub;
1544 // sanity check this is a stub we understand
1545 if ( (instructions[0] != 0xe59fc004) || (instructions[1] != 0xe08fc00c) || (instructions[2] != 0xe59cf000) )
1546 return;
1547
1548 void** lazyPointerAddr = (void**)(instructions[3] + (stub + 12));
1549 #endif
1550 #if __x86_64__
1551 // sanity check this is a stub we understand
1552 if ( (stub[0] != 0xFF) || (stub[1] != 0x25) )
1553 return;
1554 int32_t ripOffset = *((int32_t*)(&stub[2]));
1555 void** lazyPointerAddr = (void**)(ripOffset + stub + 6);
1556 #endif
1557
1558 // if stub does not use original lazy pointer (meaning it was optimized by update_dyld_shared_cache)
1559 if ( lazyPointerAddr != originalLazyPointerAddr ) {
1560 // copy newly re-bound lazy pointer value to shared lazy pointer
1561 *lazyPointerAddr = *originalLazyPointerAddr;
1562 }
1563 }
1564 #endif
1565
1566
1567 // <rdar://problem/8890875> overriding shared cache dylibs with resolvers fails
1568 void ImageLoaderMachOCompressed::updateOptimizedLazyPointers(const LinkContext& context)
1569 {
1570 #if __arm__ || __x86_64__
1571 // find stubs and lazy pointer sections
1572 const struct macho_section* stubsSection = NULL;
1573 const struct macho_section* lazyPointerSection = NULL;
1574 const dysymtab_command* dynSymbolTable = NULL;
1575 const macho_header* mh = (macho_header*)fMachOData;
1576 const uint32_t cmd_count = mh->ncmds;
1577 const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
1578 const struct load_command* cmd = cmds;
1579 for (uint32_t i = 0; i < cmd_count; ++i) {
1580 if (cmd->cmd == LC_SEGMENT_COMMAND) {
1581 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
1582 const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
1583 const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
1584 for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
1585 const uint8_t type = sect->flags & SECTION_TYPE;
1586 if ( type == S_SYMBOL_STUBS )
1587 stubsSection = sect;
1588 else if ( type == S_LAZY_SYMBOL_POINTERS )
1589 lazyPointerSection = sect;
1590 }
1591 }
1592 else if ( cmd->cmd == LC_DYSYMTAB ) {
1593 dynSymbolTable = (struct dysymtab_command*)cmd;
1594 }
1595 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
1596 }
1597
1598 // sanity check
1599 if ( dynSymbolTable == NULL )
1600 return;
1601 if ( (stubsSection == NULL) || (lazyPointerSection == NULL) )
1602 return;
1603 const uint32_t stubsCount = stubsSection->size / stubsSection->reserved2;
1604 const uint32_t lazyPointersCount = lazyPointerSection->size / sizeof(void*);
1605 if ( stubsCount != lazyPointersCount )
1606 return;
1607 const uint32_t stubsIndirectTableOffset = stubsSection->reserved1;
1608 const uint32_t lazyPointersIndirectTableOffset = lazyPointerSection->reserved1;
1609 if ( (stubsIndirectTableOffset+stubsCount) > dynSymbolTable->nindirectsyms )
1610 return;
1611 if ( (lazyPointersIndirectTableOffset+lazyPointersCount) > dynSymbolTable->nindirectsyms )
1612 return;
1613
1614 // walk stubs and lazy pointers
1615 const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[dynSymbolTable->indirectsymoff];
1616 void** const lazyPointersStartAddr = (void**)(lazyPointerSection->addr + this->fSlide);
1617 uint8_t* const stubsStartAddr = (uint8_t*)(stubsSection->addr + this->fSlide);
1618 uint8_t* stub = stubsStartAddr;
1619 void** lpa = lazyPointersStartAddr;
1620 for(uint32_t i=0; i < stubsCount; ++i, stub += stubsSection->reserved2, ++lpa) {
1621 // sanity check symbol index of stub and lazy pointer match
1622 if ( indirectTable[stubsIndirectTableOffset+i] != indirectTable[lazyPointersIndirectTableOffset+i] )
1623 continue;
1624 this->updateAlternateLazyPointer(stub, lpa);
1625 }
1626
1627 #endif
1628 }
1629
1630