1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2008 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
29 #include <sys/types.h>
30 #include <sys/fcntl.h>
33 #include <sys/param.h>
34 #include <mach/mach.h>
35 #include <mach/thread_status.h>
36 #include <mach-o/loader.h>
38 #include "ImageLoaderMachOCompressed.h"
39 #include "mach-o/dyld_images.h"
42 // relocation_info.r_length field has value 3 for 64-bit executables and value 2 for 32-bit executables
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
{};
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
{};
60 static uintptr_t read_uleb128(const uint8_t*& p
, const uint8_t* end
)
66 dyld::throwf("malformed uleb128");
68 uint64_t slice
= *p
& 0x7f;
71 dyld::throwf("uleb128 too big for uint64, bit=%d, result=0x%0llX", bit
, result
);
73 result
|= (slice
<< bit
);
76 } while (*p
++ & 0x80);
81 static intptr_t read_sleb128(const uint8_t*& p
, const uint8_t* end
)
88 throw "malformed sleb128";
90 result
|= ((byte
& 0x7f) << bit
);
92 } while (byte
& 0x80);
93 // sign extend negative numbers
94 if ( (byte
& 0x40) != 0 )
95 result
|= (-1LL) << bit
;
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
)
104 ImageLoaderMachOCompressed
* image
= ImageLoaderMachOCompressed::instantiateStart(mh
, path
, segCount
, libCount
);
106 // set slide for PIE programs
107 image
->setSlide(slide
);
109 // for PIE record end of program, to know where to start loading dylibs
111 fgNextPIEDylibAddress
= (uintptr_t)image
->getEnd();
113 image
->setNeverUnload();
114 image
->instantiateFinish(context
);
115 image
->setMapped(context
);
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
));
124 dyld::log("%18s at 0x%08lX->0x%08lX\n", name
, image
->segActualLoadAddress(i
), image
->segActualEndAddress(i
));
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
)
137 ImageLoaderMachOCompressed
* image
= ImageLoaderMachOCompressed::instantiateStart((macho_header
*)fileData
, path
, segCount
, libCount
);
140 // record info about file
141 image
->setFileInfo(info
.st_dev
, info
.st_ino
, info
.st_mtime
);
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
);
150 image
->mapSegments(fd
, offsetInFat
, lenInFat
, info
.st_size
, context
);
152 // finish construction
153 image
->instantiateFinish(context
);
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");
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
);
170 image
->setPath(path
);
173 image
->setPath(path
);
175 // make sure path is stable before recording in dyld_all_image_infos
176 image
->setMapped(context
);
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
);
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
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
)
200 ImageLoaderMachOCompressed
* image
= ImageLoaderMachOCompressed::instantiateStart(mh
, path
, segCount
, libCount
);
202 // record info about file
203 image
->setFileInfo(info
.st_dev
, info
.st_ino
, info
.st_mtime
);
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
);
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
));
219 image
->instantiateFinish(context
);
220 image
->setMapped(context
);
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
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
)
236 ImageLoaderMachOCompressed
* image
= ImageLoaderMachOCompressed::instantiateStart(mh
, moduleName
, segCount
, libCount
);
239 if ( mh
->filetype
== MH_EXECUTE
)
240 throw "can't load another MH_EXECUTE";
243 image
->mapSegments((const void*)mh
, len
, context
);
245 // for compatibility, never unload dylibs loaded from memory
246 image
->setNeverUnload();
248 // bundle loads need path copied
249 if ( moduleName
!= NULL
)
250 image
->setPath(moduleName
);
252 image
->instantiateFinish(context
);
253 image
->setMapped(context
);
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
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
)
272 ImageLoaderMachOCompressed::~ImageLoaderMachOCompressed()
274 // don't do clean up in ~ImageLoaderMachO() because virtual call to segmentCommandOffsets() won't work
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
)
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
);
294 // common code to finish initializing object
295 void ImageLoaderMachOCompressed::instantiateFinish(const LinkContext
& context
)
297 // now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide
298 this->parseLoadCmds();
301 uint32_t* ImageLoaderMachOCompressed::segmentCommandOffsets() const
303 return ((uint32_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed
)));
307 ImageLoader
* ImageLoaderMachOCompressed::libImage(unsigned int libIndex
) const
309 const uintptr_t* images
= ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed
) + fSegmentsCount
*sizeof(uint32_t)));
311 return (ImageLoader
*)(images
[libIndex
] & (-4));
314 bool ImageLoaderMachOCompressed::libReExported(unsigned int libIndex
) const
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);
321 bool ImageLoaderMachOCompressed::libIsUpward(unsigned int libIndex
) const
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);
329 void ImageLoaderMachOCompressed::setLibImage(unsigned int libIndex
, ImageLoader
* image
, bool reExported
, bool upward
)
331 uintptr_t* images
= ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed
) + fSegmentsCount
*sizeof(uint32_t)));
332 uintptr_t value
= (uintptr_t)image
;
337 images
[libIndex
] = value
;
341 void ImageLoaderMachOCompressed::markFreeLINKEDIT(const LinkContext
& context
)
343 // mark that we are done with rebase and bind info
344 markLINKEDIT(context
, MADV_FREE
);
347 void ImageLoaderMachOCompressed::markSequentialLINKEDIT(const LinkContext
& context
)
349 // mark the rebase and bind info and using sequential access
350 markLINKEDIT(context
, MADV_SEQUENTIAL
);
353 void ImageLoaderMachOCompressed::markLINKEDIT(const LinkContext
& context
, int advise
)
355 // if not loaded at preferred address, mark rebase info
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
;
362 return; // no binding info to prefetch
364 // end is at end of bind info
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
;
374 // round to whole pages
375 start
= start
& (-4096);
376 end
= (end
+ 4095) & (-4096);
378 // do nothing if only one page of rebase/bind info
379 if ( (end
-start
) <= 4096 )
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
)
388 dyld::log("%18s %s 0x%0lX -> 0x%0lX for %s\n", "__LINKEDIT", adstr
, start
, end
-1, this->getPath());
394 void ImageLoaderMachOCompressed::rebaseAt(const LinkContext
& context
, uintptr_t addr
, uintptr_t slide
, uint8_t type
)
396 //dyld::log("0x%08lX type=%d\n", addr, type);
397 uintptr_t* locationToFix
= (uintptr_t*)addr
;
399 case REBASE_TYPE_POINTER
:
400 *locationToFix
+= slide
;
402 case REBASE_TYPE_TEXT_ABSOLUTE32
:
403 *locationToFix
+= slide
;
406 dyld::throwf("bad rebase type %d", type
);
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
)
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
);
418 void ImageLoaderMachOCompressed::rebase(const LinkContext
& context
)
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
;
428 int segmentIndex
= 0;
429 uintptr_t address
= segActualLoadAddress(0);
430 uintptr_t segmentEndAddress
= segActualEndAddress(0);
434 while ( !done
&& (p
< end
) ) {
435 uint8_t immediate
= *p
& REBASE_IMMEDIATE_MASK
;
436 uint8_t opcode
= *p
& REBASE_OPCODE_MASK
;
439 case REBASE_OPCODE_DONE
:
442 case REBASE_OPCODE_SET_TYPE_IMM
:
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
);
453 case REBASE_OPCODE_ADD_ADDR_ULEB
:
454 address
+= read_uleb128(p
, end
);
456 case REBASE_OPCODE_ADD_ADDR_IMM_SCALED
:
457 address
+= immediate
*sizeof(uintptr_t);
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);
466 fgTotalRebaseFixups
+= immediate
;
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);
476 fgTotalRebaseFixups
+= count
;
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
;
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);
494 fgTotalRebaseFixups
+= count
;
497 dyld::throwf("bad rebase opcode %d", *p
);
501 catch (const char* msg
) {
502 const char* newMsg
= dyld::mkstringf("%s in %s", msg
, this->getPath());
506 CRSetCrashLogMessage2(NULL
);
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.
513 const uint8_t* ImageLoaderMachOCompressed::trieWalk(const uint8_t* start
, const uint8_t* end
, const char* s
)
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
521 terminalSize
= read_uleb128(p
, end
);
523 if ( (*s
== '\0') && (terminalSize
!= 0) ) {
524 //dyld::log("trieWalk(%p) returning %p\n", start, p);
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
++;
531 uint32_t nodeOffset
= 0;
532 for (; childrenRemaining
> 0; --childrenRemaining
) {
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
539 while ( c
!= '\0' ) {
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 )
554 ++p
; // skil over last byte of uleb128
557 // the symbol so far matches this edge (child)
558 // so advance to the child's node
560 nodeOffset
= read_uleb128(p
, end
);
562 //dyld::log("trieWalk() found matching edge advancing to node 0x%x\n", nodeOffset);
566 if ( nodeOffset
!= 0 )
567 p
= &start
[nodeOffset
];
571 //dyld::log("trieWalk(%p) return NULL\n", start);
576 const ImageLoader::Symbol
* ImageLoaderMachOCompressed::findExportedSymbol(const char* symbol
, const ImageLoader
** foundIn
) const
578 //dyld::log("Compressed::findExportedSymbol(%s) in %s\n", symbol, this->getShortName());
579 if ( fDyldInfo
->export_size
== 0 )
582 dyld::logBindings("%s: %s\n", this->getShortName(), symbol
);
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
);
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());
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
;
620 bool ImageLoaderMachOCompressed::containsSymbol(const void* addr
) const
622 const uint8_t* start
= &fLinkEditBase
[fDyldInfo
->export_off
];
623 const uint8_t* end
= &start
[fDyldInfo
->export_size
];
624 return ( (start
<= addr
) && (addr
< end
) );
628 uintptr_t ImageLoaderMachOCompressed::exportedSymbolAddress(const LinkContext
& context
, const Symbol
* symbol
, bool runResolver
) const
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
);
648 return read_uleb128(exportNode
, exportTrieEnd
) + (uintptr_t)fMachOData
;
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
;
656 dyld::throwf("unsupported exported symbol kind. flags=%d at node=%p", flags
, symbol
);
659 bool ImageLoaderMachOCompressed::exportedSymbolIsWeakDefintion(const Symbol
* symbol
) const
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
);
671 const char* ImageLoaderMachOCompressed::exportedSymbolName(const Symbol
* symbol
) const
673 throw "NSNameOfSymbol() not supported with compressed LINKEDIT";
676 unsigned int ImageLoaderMachOCompressed::exportedSymbolCount() const
678 throw "NSSymbolDefinitionCountInObjectFileImage() not supported with compressed LINKEDIT";
681 const ImageLoader::Symbol
* ImageLoaderMachOCompressed::exportedSymbolIndexed(unsigned int index
) const
683 throw "NSSymbolDefinitionNameInObjectFileImage() not supported with compressed LINKEDIT";
686 unsigned int ImageLoaderMachOCompressed::importedSymbolCount() const
688 throw "NSSymbolReferenceCountInObjectFileImage() not supported with compressed LINKEDIT";
691 const ImageLoader::Symbol
* ImageLoaderMachOCompressed::importedSymbolIndexed(unsigned int index
) const
693 throw "NSSymbolReferenceCountInObjectFileImage() not supported with compressed LINKEDIT";
696 const char* ImageLoaderMachOCompressed::importedSymbolName(const Symbol
* symbol
) const
698 throw "NSSymbolReferenceNameInObjectFileImage() not supported with compressed LINKEDIT";
703 uintptr_t ImageLoaderMachOCompressed::resolveFlat(const LinkContext
& context
, const char* symbolName
, bool weak_import
,
704 bool runResolver
, const ImageLoader
** foundIn
)
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
);
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
);
717 return (*foundIn
)->getExportedSymbolAddress(sym
, context
, this, runResolver
);
720 // definition can't be found anywhere, ok because it is weak, just return 0
723 throwSymbolNotFound(context
, symbolName
, this->getPath(), "flat namespace");
727 uintptr_t ImageLoaderMachOCompressed::resolveTwolevel(const LinkContext
& context
, const ImageLoader
* targetImage
, bool weak_import
,
728 const char* symbolName
, bool runResolver
, const ImageLoader
** foundIn
)
731 const Symbol
* sym
= targetImage
->findExportedSymbol(symbolName
, true, foundIn
);
733 return (*foundIn
)->getExportedSymbolAddress(sym
, context
, this, runResolver
);
737 // definition can't be found anywhere, ok because it is weak, just return 0
741 // nowhere to be found
742 throwSymbolNotFound(context
, symbolName
, this->getPath(), targetImage
->getPath());
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
)
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
;
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
);
768 if ( libraryOrdinal
== BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE
) {
769 *targetImage
= context
.mainExecutable
;
771 else if ( libraryOrdinal
== BIND_SPECIAL_DYLIB_SELF
) {
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());
778 else if ( (unsigned)libraryOrdinal
<= libraryCount() ) {
779 *targetImage
= libImage(libraryOrdinal
-1);
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());
785 if ( *targetImage
== NULL
) {
787 // if target library not loaded and reference is weak or library is weak return 0
791 dyld::throwf("can't resolve symbol %s in %s because dependent dylib #%d could not be loaded",
792 symbolName
, this->getPath(), libraryOrdinal
);
796 symbolAddress
= resolveTwolevel(context
, *targetImage
, weak_import
, symbolName
, runResolver
, targetImage
);
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
;
809 return symbolAddress
;
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
)
816 const ImageLoader
* targetImage
;
817 uintptr_t symbolAddress
;
820 symbolAddress
= this->resolve(context
, symbolName
, symboFlags
, libraryOrdinal
, &targetImage
, last
, runResolver
);
823 return this->bindLocation(context
, addr
, symbolAddress
, targetImage
, type
, symbolName
, addend
, msg
);
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
)
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
);
835 void ImageLoaderMachOCompressed::doBind(const LinkContext
& context
, bool forceLazysBound
)
837 CRSetCrashLogMessage2(this->getPath());
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
846 #if TEXT_RELOC_SUPPORT
847 // if there are __TEXT fixups, temporarily make __TEXT writable
848 if ( fTextSegmentBinds
)
849 this->makeTextSegmentWritable(context
, true);
852 // run through all binding opcodes
853 eachBind(context
, &ImageLoaderMachOCompressed::bindAt
);
855 #if TEXT_RELOC_SUPPORT
856 // if there were __TEXT fixups, restore write protection
857 if ( fTextSegmentBinds
)
858 this->makeTextSegmentWritable(context
, false);
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
);
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
);
872 // tell kernel we are done with chunks of LINKEDIT
873 if ( !context
.preFetchDisabled
)
874 this->markFreeLINKEDIT(context
);
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
);
884 void ImageLoaderMachOCompressed::doBindJustLazies(const LinkContext
& context
)
886 eachLazyBind(context
, &ImageLoaderMachOCompressed::bindAt
);
889 void ImageLoaderMachOCompressed::eachBind(const LinkContext
& context
, bind_handler handler
)
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;
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
;
907 while ( !done
&& (p
< end
) ) {
908 uint8_t immediate
= *p
& BIND_IMMEDIATE_MASK
;
909 uint8_t opcode
= *p
& BIND_OPCODE_MASK
;
912 case BIND_OPCODE_DONE
:
915 case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
:
916 libraryOrdinal
= immediate
;
918 case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB
:
919 libraryOrdinal
= read_uleb128(p
, end
);
921 case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM
:
922 // the special ordinals are negative numbers
923 if ( immediate
== 0 )
926 int8_t signExtended
= BIND_OPCODE_MASK
| immediate
;
927 libraryOrdinal
= signExtended
;
930 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
:
931 symbolName
= (char*)p
;
932 symboFlags
= immediate
;
937 case BIND_OPCODE_SET_TYPE_IMM
:
940 case BIND_OPCODE_SET_ADDEND_SLEB
:
941 addend
= read_sleb128(p
, end
);
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
);
951 case BIND_OPCODE_ADD_ADDR_ULEB
:
952 address
+= read_uleb128(p
, end
);
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);
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);
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);
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);
983 dyld::throwf("bad bind opcode %d in bind info", *p
);
987 catch (const char* msg
) {
988 const char* newMsg
= dyld::mkstringf("%s in %s", msg
, this->getPath());
994 void ImageLoaderMachOCompressed::eachLazyBind(const LinkContext
& context
, bind_handler handler
)
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
;
1009 while ( !done
&& (p
< end
) ) {
1010 uint8_t immediate
= *p
& BIND_IMMEDIATE_MASK
;
1011 uint8_t opcode
= *p
& BIND_OPCODE_MASK
;
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
1017 case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
:
1018 libraryOrdinal
= immediate
;
1020 case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB
:
1021 libraryOrdinal
= read_uleb128(p
, end
);
1023 case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM
:
1024 // the special ordinals are negative numbers
1025 if ( immediate
== 0 )
1028 int8_t signExtended
= BIND_OPCODE_MASK
| immediate
;
1029 libraryOrdinal
= signExtended
;
1032 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
:
1033 symbolName
= (char*)p
;
1034 symboFlags
= immediate
;
1039 case BIND_OPCODE_SET_TYPE_IMM
:
1042 case BIND_OPCODE_SET_ADDEND_SLEB
:
1043 addend
= read_sleb128(p
, end
);
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
);
1053 case BIND_OPCODE_ADD_ADDR_ULEB
:
1054 address
+= read_uleb128(p
, end
);
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);
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
:
1066 dyld::throwf("bad lazy bind opcode %d", *p
);
1071 catch (const char* msg
) {
1072 const char* newMsg
= dyld::mkstringf("%s in %s", msg
, this->getPath());
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
)
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
) {
1093 const struct symtab_command
* symtab
= (struct symtab_command
*)cmd
;
1094 symbolTableStrings
= (const char*)&fLinkEditBase
[symtab
->stroff
];
1095 symbolTable
= (macho_nlist
*)(&fLinkEditBase
[symtab
->symoff
]);
1099 dynSymbolTable
= (struct dysymtab_command
*)cmd
;
1102 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
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());
1108 // scan for all lazy-pointer sections
1109 const bool twoLevel
= this->usesTwoLevelNameSpace();
1110 const uint32_t* const indirectTable
= (uint32_t*)&fLinkEditBase
[dynSymbolTable
->indirectsymoff
];
1112 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1114 case LC_SEGMENT_COMMAND
:
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
= §ionsStart
[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
];
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
;
1146 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
1148 dyld::throwf("lazy pointer not found at address %p in image %s", lazyPointer
, this->getPath());
1152 uintptr_t ImageLoaderMachOCompressed::doBindFastLazySymbol(uint32_t lazyBindingInfoOffset
, const LinkContext
& context
,
1153 void (*lock
)(), void (*unlock
)())
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
1160 // acquire dyld global lock
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());
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;
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
;
1185 case BIND_OPCODE_DONE
:
1188 case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
:
1189 libraryOrdinal
= immediate
;
1191 case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB
:
1192 libraryOrdinal
= read_uleb128(p
, end
);
1194 case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM
:
1195 // the special ordinals are negative numbers
1196 if ( immediate
== 0 )
1199 int8_t signExtended
= BIND_OPCODE_MASK
| immediate
;
1200 libraryOrdinal
= signExtended
;
1203 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
:
1204 symbolName
= (char*)p
;
1205 symboFlags
= immediate
;
1210 case BIND_OPCODE_SET_TYPE_IMM
:
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
);
1219 case BIND_OPCODE_DO_BIND
:
1222 result
= this->bindAt(context
, address
, type
, symbolName
, 0, 0, libraryOrdinal
, "lazy ", NULL
, true);
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
:
1230 dyld::throwf("bad lazy bind opcode %d", *p
);
1234 if ( !this->usesTwoLevelNameSpace() ) {
1235 // release dyld global lock
1236 if ( unlock
!= NULL
)
1242 void ImageLoaderMachOCompressed::initializeCoalIterator(CoalIterator
& it
, unsigned int loadOrder
)
1245 it
.symbolName
= " ";
1246 it
.loadOrder
= loadOrder
;
1247 it
.weakSymbol
= false;
1248 it
.symbolMatches
= false;
1251 it
.endIndex
= this->fDyldInfo
->weak_bind_size
;
1258 bool ImageLoaderMachOCompressed::incrementCoalIterator(CoalIterator
& it
)
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
1266 it
.symbolName
= "~~~";
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
;
1275 uint8_t immediate
= *p
& BIND_IMMEDIATE_MASK
;
1276 uint8_t opcode
= *p
& BIND_OPCODE_MASK
;
1279 case BIND_OPCODE_DONE
:
1281 it
.curIndex
= p
- start
;
1282 it
.symbolName
= "~~~"; // sorts to end
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;
1291 it
.curIndex
= p
- start
;
1293 case BIND_OPCODE_SET_TYPE_IMM
:
1294 it
.type
= immediate
;
1296 case BIND_OPCODE_SET_ADDEND_SLEB
:
1297 it
.addend
= read_sleb128(p
, end
);
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
);
1305 case BIND_OPCODE_ADD_ADDR_ULEB
:
1306 it
.address
+= read_uleb128(p
, end
);
1308 case BIND_OPCODE_DO_BIND
:
1309 it
.address
+= sizeof(intptr_t);
1311 case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB
:
1312 it
.address
+= read_uleb128(p
, end
) + sizeof(intptr_t);
1314 case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED
:
1315 it
.address
+= immediate
*sizeof(intptr_t) + sizeof(intptr_t);
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);
1325 dyld::throwf("bad weak bind opcode %d", *p
);
1328 /// hmmm, BIND_OPCODE_DONE is missing...
1330 it
.symbolName
= "~~~";
1331 //dyld::log("missing BIND_OPCODE_DONE for image %s\n", this->getPath());
1335 uintptr_t ImageLoaderMachOCompressed::getAddressCoalIterator(CoalIterator
& it
, const LinkContext
& context
)
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);
1348 void ImageLoaderMachOCompressed::updateUsesCoalIterator(CoalIterator
& it
, uintptr_t value
, ImageLoader
* targetImage
, const LinkContext
& context
)
1350 // <rdar://problem/6570879> weak binding done too early with inserted libraries
1351 if ( this->getState() < dyld_image_state_bound
)
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
;
1358 uint8_t type
= it
.type
;
1359 uintptr_t address
= it
.address
;
1360 const char* symbolName
= it
.symbolName
;
1361 intptr_t addend
= it
.addend
;
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
;
1371 case BIND_OPCODE_DONE
:
1374 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
:
1377 case BIND_OPCODE_SET_TYPE_IMM
:
1380 case BIND_OPCODE_SET_ADDEND_SLEB
:
1381 addend
= read_sleb128(p
, end
);
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
);
1389 case BIND_OPCODE_ADD_ADDR_ULEB
:
1390 address
+= read_uleb128(p
, end
);
1392 case BIND_OPCODE_DO_BIND
:
1393 bindLocation(context
, address
, value
, targetImage
, type
, symbolName
, addend
, "weak ");
1394 boundSomething
= true;
1395 address
+= sizeof(intptr_t);
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);
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);
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);
1417 dyld::throwf("bad bind opcode %d in weak binding info", *p
);
1420 if ( boundSomething
&& (targetImage
!= this) && !targetImage
->neverUnload() )
1421 this->addDynamicReference(targetImage
);
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
)
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());
1437 *fixupLocation
= it
->replacement
;
1444 void ImageLoaderMachOCompressed::doInterpose(const LinkContext
& context
)
1446 if ( context
.verboseInterposing
)
1447 dyld::log("dyld: interposing %lu tuples onto image: %s\n", fgInterposingTuples
.size(), this->getPath());
1449 // update prebound symbols
1450 eachBind(context
, &ImageLoaderMachOCompressed::interposeAt
);
1451 eachLazyBind(context
, &ImageLoaderMachOCompressed::interposeAt
);
1457 const char* ImageLoaderMachOCompressed::findClosestSymbol(const void* addr
, const void** closestAddr
) const
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
) {
1471 const struct symtab_command
* symtab
= (struct symtab_command
*)cmd
;
1472 symbolTableStrings
= (const char*)&fLinkEditBase
[symtab
->stroff
];
1473 symbolTable
= (macho_nlist
*)(&fLinkEditBase
[symtab
->symoff
]);
1477 dynSymbolTable
= (struct dysymtab_command
*)cmd
;
1480 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
1482 // no symbol table => no lookup by address
1483 if ( (symbolTable
== NULL
) || (dynSymbolTable
== NULL
) )
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
)
1497 else if ( (s
->n_value
<= targetAddress
) && (bestSymbol
->n_value
< s
->n_value
) ) {
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
)
1511 else if ( (s
->n_value
<= targetAddress
) && (bestSymbol
->n_value
< s
->n_value
) ) {
1516 if ( bestSymbol
!= NULL
) {
1518 if (bestSymbol
->n_desc
& N_ARM_THUMB_DEF
)
1519 *closestAddr
= (void*)((bestSymbol
->n_value
| 1) + fSlide
);
1521 *closestAddr
= (void*)(bestSymbol
->n_value
+ fSlide
);
1523 *closestAddr
= (void*)(bestSymbol
->n_value
+ fSlide
);
1525 return &symbolTableStrings
[bestSymbol
->n_un
.n_strx
];
1531 #if PREBOUND_IMAGE_SUPPORT
1532 void ImageLoaderMachOCompressed::resetPreboundLazyPointers(const LinkContext
& context
)
1534 // no way to back off a prebound compress image
1539 #if __arm__ || __x86_64__
1540 void ImageLoaderMachOCompressed::updateAlternateLazyPointer(uint8_t* stub
, void** originalLazyPointerAddr
)
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) )
1548 void** lazyPointerAddr
= (void**)(instructions
[3] + (stub
+ 12));
1551 // sanity check this is a stub we understand
1552 if ( (stub
[0] != 0xFF) || (stub
[1] != 0x25) )
1554 int32_t ripOffset
= *((int32_t*)(&stub
[2]));
1555 void** lazyPointerAddr
= (void**)(ripOffset
+ stub
+ 6);
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
;
1567 // <rdar://problem/8890875> overriding shared cache dylibs with resolvers fails
1568 void ImageLoaderMachOCompressed::updateOptimizedLazyPointers(const LinkContext
& context
)
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
= §ionsStart
[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
;
1592 else if ( cmd
->cmd
== LC_DYSYMTAB
) {
1593 dynSymbolTable
= (struct dysymtab_command
*)cmd
;
1595 cmd
= (const struct load_command
*)(((char*)cmd
)+cmd
->cmdsize
);
1599 if ( dynSymbolTable
== NULL
)
1601 if ( (stubsSection
== NULL
) || (lazyPointerSection
== NULL
) )
1603 const uint32_t stubsCount
= stubsSection
->size
/ stubsSection
->reserved2
;
1604 const uint32_t lazyPointersCount
= lazyPointerSection
->size
/ sizeof(void*);
1605 if ( stubsCount
!= lazyPointersCount
)
1607 const uint32_t stubsIndirectTableOffset
= stubsSection
->reserved1
;
1608 const uint32_t lazyPointersIndirectTableOffset
= lazyPointerSection
->reserved1
;
1609 if ( (stubsIndirectTableOffset
+stubsCount
) > dynSymbolTable
->nindirectsyms
)
1611 if ( (lazyPointersIndirectTableOffset
+lazyPointersCount
) > dynSymbolTable
->nindirectsyms
)
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
] )
1624 this->updateAlternateLazyPointer(stub
, lpa
);