Loading...
--- dyld/dyld-195.5/src/ImageLoaderMachOCompressed.cpp
+++ dyld/dyld-132.13/src/ImageLoaderMachOCompressed.cpp
@@ -67,13 +67,14 @@
uint64_t slice = *p & 0x7f;
- if (bit > 63)
- dyld::throwf("uleb128 too big for uint64, bit=%d, result=0x%0llX", bit, result);
+ if (bit >= 64 || slice << bit >> bit != slice)
+ dyld::throwf("uleb128 too big");
else {
result |= (slice << bit);
bit += 7;
}
- } while (*p++ & 0x80);
+ }
+ while (*p++ & 0x80);
return result;
}
@@ -107,12 +108,11 @@
image->setSlide(slide);
// for PIE record end of program, to know where to start loading dylibs
- if ( slide != 0 )
+ if ( (mh->flags & MH_PIE) && !context.noPIE )
fgNextPIEDylibAddress = (uintptr_t)image->getEnd();
image->setNeverUnload();
image->instantiateFinish(context);
- image->setMapped(context);
if ( context.verboseMapping ) {
dyld::log("dyld: Main executable mapped %s\n", path);
@@ -131,8 +131,7 @@
// create image by mapping in a mach-o file
ImageLoaderMachOCompressed* ImageLoaderMachOCompressed::instantiateFromFile(const char* path, int fd, const uint8_t* fileData,
uint64_t offsetInFat, uint64_t lenInFat, const struct stat& info,
- unsigned int segCount, unsigned int libCount,
- const struct linkedit_data_command* codeSigCmd, const LinkContext& context)
+ unsigned int segCount, unsigned int libCount, const LinkContext& context)
{
ImageLoaderMachOCompressed* image = ImageLoaderMachOCompressed::instantiateStart((macho_header*)fileData, path, segCount, libCount);
@@ -140,12 +139,6 @@
// record info about file
image->setFileInfo(info.st_dev, info.st_ino, info.st_mtime);
- #if CODESIGNING_SUPPORT
- // if this image is code signed, let kernel validate signature before mapping any pages from image
- if ( codeSigCmd != NULL )
- image->loadCodeSignature(codeSigCmd, fd, offsetInFat);
- #endif
-
// mmap segments
image->mapSegments(fd, offsetInFat, lenInFat, info.st_size, context);
@@ -172,9 +165,6 @@
else
image->setPath(path);
- // make sure path is stable before recording in dyld_all_image_infos
- image->setMapped(context);
-
// pre-fetch content of __DATA and __LINKEDIT segment for faster launches
// don't do this on prebound images or if prefetching is disabled
if ( !context.preFetchDisabled && !image->isPrebindable()) {
@@ -193,9 +183,8 @@
}
// create image by using cached mach-o file
-ImageLoaderMachOCompressed* ImageLoaderMachOCompressed::instantiateFromCache(const macho_header* mh, const char* path, long slide,
- const struct stat& info, unsigned int segCount,
- unsigned int libCount, const LinkContext& context)
+ImageLoaderMachOCompressed* ImageLoaderMachOCompressed::instantiateFromCache(const macho_header* mh, const char* path, const struct stat& info,
+ unsigned int segCount, unsigned int libCount, const LinkContext& context)
{
ImageLoaderMachOCompressed* image = ImageLoaderMachOCompressed::instantiateStart(mh, path, segCount, libCount);
try {
@@ -204,9 +193,7 @@
// remember this is from shared cache and cannot be unloaded
image->fInSharedCache = true;
- image->fGoodFirstSegment = true;
image->setNeverUnload();
- image->setSlide(slide);
// segments already mapped in cache
if ( context.verboseMapping ) {
@@ -217,7 +204,6 @@
}
image->instantiateFinish(context);
- image->setMapped(context);
}
catch (...) {
// ImageLoader::setMapped() can throw an exception to block loading of image
@@ -250,7 +236,6 @@
image->setPath(moduleName);
image->instantiateFinish(context);
- image->setMapped(context);
}
catch (...) {
// ImageLoader::setMapped() can throw an exception to block loading of image
@@ -296,6 +281,9 @@
{
// now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide
this->parseLoadCmds();
+
+ // notify state change
+ this->setMapped(context);
}
uint32_t* ImageLoaderMachOCompressed::segmentCommandOffsets() const
@@ -307,8 +295,8 @@
ImageLoader* ImageLoaderMachOCompressed::libImage(unsigned int libIndex) const
{
const uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed) + fSegmentsCount*sizeof(uint32_t)));
- // mask off low bits
- return (ImageLoader*)(images[libIndex] & (-4));
+ // mask off low bit
+ return (ImageLoader*)(images[libIndex] & (-2));
}
bool ImageLoaderMachOCompressed::libReExported(unsigned int libIndex) const
@@ -318,22 +306,13 @@
return ((images[libIndex] & 1) != 0);
}
-bool ImageLoaderMachOCompressed::libIsUpward(unsigned int libIndex) const
-{
- const uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed) + fSegmentsCount*sizeof(uint32_t)));
- // re-export flag is second bit
- return ((images[libIndex] & 2) != 0);
-}
-
-
-void ImageLoaderMachOCompressed::setLibImage(unsigned int libIndex, ImageLoader* image, bool reExported, bool upward)
+
+void ImageLoaderMachOCompressed::setLibImage(unsigned int libIndex, ImageLoader* image, bool reExported)
{
uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed) + fSegmentsCount*sizeof(uint32_t)));
uintptr_t value = (uintptr_t)image;
if ( reExported )
value |= 1;
- if ( upward )
- value |= 2;
images[libIndex] = value;
}
@@ -385,7 +364,7 @@
const char* adstr = "sequential";
if ( advise == MADV_FREE )
adstr = "free";
- dyld::log("%18s %s 0x%0lX -> 0x%0lX for %s\n", "__LINKEDIT", adstr, start, end-1, this->getPath());
+ dyld::log("%18s %s 0x%0lX -> 0x%0lX\n", "__LINKEDIT", adstr, start, end-1);
}
}
@@ -417,7 +396,6 @@
void ImageLoaderMachOCompressed::rebase(const LinkContext& context)
{
- CRSetCrashLogMessage2(this->getPath());
const uintptr_t slide = this->fSlide;
const uint8_t* const start = fLinkEditBase + fDyldInfo->rebase_off;
const uint8_t* const end = &start[fDyldInfo->rebase_size];
@@ -503,117 +481,70 @@
free((void*)msg);
throw newMsg;
}
- CRSetCrashLogMessage2(NULL);
-}
-
-//
-// This function is the hotspot of symbol lookup. It was pulled out of findExportedSymbol()
-// to enable it to be re-written in assembler if needed.
-//
-const uint8_t* ImageLoaderMachOCompressed::trieWalk(const uint8_t* start, const uint8_t* end, const char* s)
-{
+}
+
+
+
+
+const ImageLoader::Symbol* ImageLoaderMachOCompressed::findExportedSymbol(const char* symbol, const ImageLoader** foundIn) const
+{
+ //dyld::log("findExportedSymbolCompressed(%s) in %s\n", symbol, this->getShortName());
+ if ( fDyldInfo->export_size == 0 )
+ return NULL;
+ ++ImageLoaderMachO::fgSymbolTrieSearchs;
+ const uint8_t* start = &fLinkEditBase[fDyldInfo->export_off];
+ const uint8_t* end = &start[fDyldInfo->export_size];
const uint8_t* p = start;
- while ( p != NULL ) {
- uint32_t terminalSize = *p++;
- if ( terminalSize > 127 ) {
- // except for re-export-with-rename, all terminal sizes fit in one byte
- --p;
- terminalSize = read_uleb128(p, end);
- }
+ const char* s = symbol;
+ do {
+ const uint8_t terminalSize = *p++;
+ const uint8_t* children = p + terminalSize;
if ( (*s == '\0') && (terminalSize != 0) ) {
- //dyld::log("trieWalk(%p) returning %p\n", start, p);
- return p;
- }
- const uint8_t* children = p + terminalSize;
- //dyld::log("trieWalk(%p) sym=%s, terminalSize=%d, children=%p\n", start, s, terminalSize, children);
- uint8_t childrenRemaining = *children++;
- p = children;
- uint32_t nodeOffset = 0;
- for (; childrenRemaining > 0; --childrenRemaining) {
+ // found match, return pointer to terminal part of node
+ //dyld::log("findExportedSymbol(%s) in %s found match, returning %p\n", symbol, this->getShortName(), p);
+ if ( foundIn != NULL )
+ *foundIn = (ImageLoader*)this;
+ return (Symbol*)p;
+ }
+ const uint8_t childrenCount = *children++;
+ const uint8_t* e = children;
+ const uint8_t* newNode = NULL;
+ for (uint8_t i=0; i < childrenCount; ++i) {
const char* ss = s;
- //dyld::log("trieWalk(%p) child str=%s\n", start, (char*)p);
bool wrongEdge = false;
+ //dyld::log("findExportedSymbol() looking at edge %s for match to %s\n", e, s);
// scan whole edge to get to next edge
// if edge is longer than target symbol name, don't read past end of symbol name
- char c = *p;
- while ( c != '\0' ) {
+ while ( *e != '\0' ) {
if ( !wrongEdge ) {
- if ( c != *ss )
+ if ( *e != *ss++ )
wrongEdge = true;
- ++ss;
}
- ++p;
- c = *p;
+ ++e;
}
if ( wrongEdge ) {
// advance to next child
- ++p; // skip over zero terminator
- // skip over uleb128 until last byte is found
- while ( (*p & 0x80) != 0 )
- ++p;
- ++p; // skil over last byte of uleb128
+ ++e;
+ read_uleb128(e, end);
}
else {
- // the symbol so far matches this edge (child)
+ // the symbol so far matches this edge (child)
// so advance to the child's node
- ++p;
- nodeOffset = read_uleb128(p, end);
+ ++e;
+ uint32_t nodeOffset = read_uleb128(e, end);
+ newNode = &start[nodeOffset];
s = ss;
- //dyld::log("trieWalk() found matching edge advancing to node 0x%x\n", nodeOffset);
+ //dyld::log("findExportedSymbol() found matching edge advancing to node 0x%x\n", nodeOffset);
break;
}
}
- if ( nodeOffset != 0 )
- p = &start[nodeOffset];
- else
- p = NULL;
- }
- //dyld::log("trieWalk(%p) return NULL\n", start);
- return NULL;
-}
-
-
-const ImageLoader::Symbol* ImageLoaderMachOCompressed::findExportedSymbol(const char* symbol, const ImageLoader** foundIn) const
-{
- //dyld::log("Compressed::findExportedSymbol(%s) in %s\n", symbol, this->getShortName());
- if ( fDyldInfo->export_size == 0 )
- return NULL;
-#if LOG_BINDINGS
- dyld::logBindings("%s: %s\n", this->getShortName(), symbol);
-#endif
- ++ImageLoaderMachO::fgSymbolTrieSearchs;
- const uint8_t* start = &fLinkEditBase[fDyldInfo->export_off];
- const uint8_t* end = &start[fDyldInfo->export_size];
- const uint8_t* foundNodeStart = this->trieWalk(start, end, symbol);
- if ( foundNodeStart != NULL ) {
- const uint8_t* p = foundNodeStart;
- const uint32_t flags = read_uleb128(p, end);
- // found match, return pointer to terminal part of node
- if ( flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
- // re-export from another dylib, lookup there
- const uint32_t ordinal = read_uleb128(p, end);
- const char* importedName = (char*)p;
- if ( importedName[0] == '\0' )
- importedName = symbol;
- if ( (ordinal > 0) && (ordinal <= libraryCount()) ) {
- const ImageLoader* reexportedFrom = libImage(ordinal-1);
- //dyld::log("Compressed::findExportedSymbol(), %s -> %s/%s\n", symbol, reexportedFrom->getShortName(), importedName);
- return reexportedFrom->findExportedSymbol(importedName, true, foundIn);
- }
- else {
- //dyld::throwf("bad mach-o binary, library ordinal (%u) invalid (max %u) for re-exported symbol %s in %s",
- // ordinal, libraryCount(), symbol, this->getPath());
- }
- }
+ if ( newNode != NULL )
+ p = newNode;
else {
- //dyld::log("findExportedSymbol(%s) in %s found match, returning %p\n", symbol, this->getShortName(), p);
- if ( foundIn != NULL )
- *foundIn = (ImageLoader*)this;
- // return pointer to terminal part of node
- return (Symbol*)foundNodeStart;
- }
- }
- return NULL;
+ //dyld::log("findExportedSymbol(%s) in %s failed\n", symbol, this->getShortName());
+ return NULL;
+ }
+ } while ( true );
}
@@ -625,35 +556,18 @@
}
-uintptr_t ImageLoaderMachOCompressed::exportedSymbolAddress(const LinkContext& context, const Symbol* symbol, bool runResolver) const
+uintptr_t ImageLoaderMachOCompressed::exportedSymbolAddress(const Symbol* symbol) const
{
const uint8_t* exportNode = (uint8_t*)symbol;
const uint8_t* exportTrieStart = fLinkEditBase + fDyldInfo->export_off;
const uint8_t* exportTrieEnd = exportTrieStart + fDyldInfo->export_size;
if ( (exportNode < exportTrieStart) || (exportNode > exportTrieEnd) )
throw "symbol is not in trie";
- //dyld::log("exportedSymbolAddress(): node=%p, nodeOffset=0x%04X in %s\n", symbol, (int)((uint8_t*)symbol - exportTrieStart), this->getShortName());
uint32_t flags = read_uleb128(exportNode, exportTrieEnd);
- if ( (flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_REGULAR ) {
- if ( runResolver && (flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) ) {
- // this node has a stub and resolver, run the resolver to get target address
- read_uleb128(exportNode, exportTrieEnd); // skip over stub
- typedef uintptr_t (*ResolverProc)(void);
- ResolverProc resolver = (ResolverProc)(read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData);
- uintptr_t result = (*resolver)();
- if ( context.verboseBind )
- dyld::log("dyld: resolver at %p returned 0x%08lX\n", resolver, result);
- return result;
- }
+ if ( (flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_REGULAR )
return read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData;
- }
- else if ( (flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL ) {
- if ( flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
- dyld::throwf("unsupported exported symbol kind. flags=%d at node=%p", flags, symbol);
- return read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData;
- }
else
- dyld::throwf("unsupported exported symbol kind. flags=%d at node=%p", flags, symbol);
+ throw "unsupported exported symbol kind";
}
bool ImageLoaderMachOCompressed::exportedSymbolIsWeakDefintion(const Symbol* symbol) const
@@ -700,37 +614,36 @@
-uintptr_t ImageLoaderMachOCompressed::resolveFlat(const LinkContext& context, const char* symbolName, bool weak_import,
- bool runResolver, const ImageLoader** foundIn)
+uintptr_t ImageLoaderMachOCompressed::resolveFlat(const LinkContext& context, const char* symbolName, bool weak_import, const ImageLoader** foundIn)
{
const Symbol* sym;
if ( context.flatExportFinder(symbolName, &sym, foundIn) ) {
if ( (*foundIn != this) && !(*foundIn)->neverUnload() )
this->addDynamicReference(*foundIn);
- return (*foundIn)->getExportedSymbolAddress(sym, context, this, runResolver);
+ return (*foundIn)->getExportedSymbolAddress(sym, context, this);
}
// if a bundle is loaded privately the above will not find its exports
if ( this->isBundle() && this->hasHiddenExports() ) {
// look in self for needed symbol
sym = this->ImageLoaderMachO::findExportedSymbol(symbolName, false, foundIn);
if ( sym != NULL )
- return (*foundIn)->getExportedSymbolAddress(sym, context, this, runResolver);
+ return (*foundIn)->getExportedSymbolAddress(sym, context, this);
}
if ( weak_import ) {
// definition can't be found anywhere, ok because it is weak, just return 0
return 0;
}
- throwSymbolNotFound(context, symbolName, this->getPath(), "flat namespace");
+ throwSymbolNotFound(symbolName, this->getPath(), "flat namespace");
}
uintptr_t ImageLoaderMachOCompressed::resolveTwolevel(const LinkContext& context, const ImageLoader* targetImage, bool weak_import,
- const char* symbolName, bool runResolver, const ImageLoader** foundIn)
+ const char* symbolName, const ImageLoader** foundIn)
{
// two level lookup
const Symbol* sym = targetImage->findExportedSymbol(symbolName, true, foundIn);
if ( sym != NULL ) {
- return (*foundIn)->getExportedSymbolAddress(sym, context, this, runResolver);
+ return (*foundIn)->getExportedSymbolAddress(sym, context, this);
}
if ( weak_import ) {
@@ -739,13 +652,13 @@
}
// nowhere to be found
- throwSymbolNotFound(context, symbolName, this->getPath(), targetImage->getPath());
+ throwSymbolNotFound(symbolName, this->getPath(), targetImage->getPath());
}
uintptr_t ImageLoaderMachOCompressed::resolve(const LinkContext& context, const char* symbolName,
uint8_t symboFlags, int libraryOrdinal, const ImageLoader** targetImage,
- LastLookup* last, bool runResolver)
+ LastLookup* last)
{
*targetImage = NULL;
@@ -762,7 +675,7 @@
bool weak_import = (symboFlags & BIND_SYMBOL_FLAGS_WEAK_IMPORT);
uintptr_t symbolAddress;
if ( context.bindFlat || (libraryOrdinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP) ) {
- symbolAddress = this->resolveFlat(context, symbolName, weak_import, runResolver, targetImage);
+ symbolAddress = this->resolveFlat(context, symbolName, weak_import, targetImage);
}
else {
if ( libraryOrdinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE ) {
@@ -793,7 +706,7 @@
}
}
else {
- symbolAddress = resolveTwolevel(context, *targetImage, weak_import, symbolName, runResolver, targetImage);
+ symbolAddress = resolveTwolevel(context, *targetImage, weak_import, symbolName, targetImage);
}
}
@@ -810,14 +723,13 @@
}
uintptr_t ImageLoaderMachOCompressed::bindAt(const LinkContext& context, uintptr_t addr, uint8_t type, const char* symbolName,
- uint8_t symboFlags, intptr_t addend, int libraryOrdinal, const char* msg,
- LastLookup* last, bool runResolver)
+ uint8_t symboFlags, intptr_t addend, int libraryOrdinal, const char* msg, LastLookup* last)
{
const ImageLoader* targetImage;
uintptr_t symbolAddress;
// resolve symbol
- symbolAddress = this->resolve(context, symbolName, symboFlags, libraryOrdinal, &targetImage, last, runResolver);
+ symbolAddress = this->resolve(context, symbolName, symboFlags, libraryOrdinal, &targetImage, last);
// do actual update
return this->bindLocation(context, addr, symbolAddress, targetImage, type, symbolName, addend, msg);
@@ -834,50 +746,28 @@
void ImageLoaderMachOCompressed::doBind(const LinkContext& context, bool forceLazysBound)
{
- CRSetCrashLogMessage2(this->getPath());
-
// if prebound and loaded at prebound address, and all libraries are same as when this was prebound, then no need to bind
// note: flat-namespace binaries need to have imports rebound (even if correctly prebound)
if ( this->usablePrebinding(context) ) {
// don't need to bind
}
else {
-
- #if TEXT_RELOC_SUPPORT
- // if there are __TEXT fixups, temporarily make __TEXT writable
- if ( fTextSegmentBinds )
- this->makeTextSegmentWritable(context, true);
- #endif
-
// run through all binding opcodes
eachBind(context, &ImageLoaderMachOCompressed::bindAt);
- #if TEXT_RELOC_SUPPORT
- // if there were __TEXT fixups, restore write protection
- if ( fTextSegmentBinds )
- this->makeTextSegmentWritable(context, false);
- #endif
-
- // if this image is in the shared cache, but depends on something no longer in the shared cache,
+ // if this image is in the shared cache, but depends on someting no longer in the shared cache,
// there is no way to reset the lazy pointers, so force bind them now
- if ( forceLazysBound || fInSharedCache )
+ if ( forceLazysBound || fInSharedCache )
this->doBindJustLazies(context);
-
- // this image is in cache, but something below it is not. If
- // this image has lazy pointer to a resolver function, then
- // the stub may have been altered to point to a shared lazy pointer.
- if ( fInSharedCache )
- this->updateOptimizedLazyPointers(context);
-
- // tell kernel we are done with chunks of LINKEDIT
- if ( !context.preFetchDisabled )
- this->markFreeLINKEDIT(context);
}
// set up dyld entry points in image
// do last so flat main executables will have __dyld or __program_vars set up
this->setupLazyPointerHandler(context);
- CRSetCrashLogMessage2(NULL);
+
+ // tell kernel we are done with chunks of LINKEDIT
+ if ( !context.preFetchDisabled )
+ this->markFreeLINKEDIT(context);
}
@@ -954,19 +844,19 @@
case BIND_OPCODE_DO_BIND:
if ( address >= segmentEndAddress )
throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
- (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
+ (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last);
address += sizeof(intptr_t);
break;
case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
if ( address >= segmentEndAddress )
throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
- (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
+ (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last);
address += read_uleb128(p, end) + sizeof(intptr_t);
break;
case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
if ( address >= segmentEndAddress )
throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
- (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
+ (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last);
address += immediate*sizeof(intptr_t) + sizeof(intptr_t);
break;
case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
@@ -975,7 +865,7 @@
for (uint32_t i=0; i < count; ++i) {
if ( address >= segmentEndAddress )
throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
- (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
+ (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last);
address += skip + sizeof(intptr_t);
}
break;
@@ -1056,7 +946,7 @@
case BIND_OPCODE_DO_BIND:
if ( address >= segmentEndAddress )
throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
- (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "lazy forced", NULL, true);
+ (this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "lazy forced", NULL);
address += sizeof(intptr_t);
break;
case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
@@ -1149,25 +1039,12 @@
}
-uintptr_t ImageLoaderMachOCompressed::doBindFastLazySymbol(uint32_t lazyBindingInfoOffset, const LinkContext& context,
- void (*lock)(), void (*unlock)())
-{
- // <rdar://problem/8663923> race condition with flat-namespace lazy binding
- if ( this->usesTwoLevelNameSpace() ) {
- // two-level namespace lookup does not require lock because dependents can't be unloaded before this image
- }
- else {
- // acquire dyld global lock
- if ( lock != NULL )
- lock();
- }
-
+uintptr_t ImageLoaderMachOCompressed::doBindFastLazySymbol(uint32_t lazyBindingInfoOffset, const LinkContext& context)
+{
const uint8_t* const start = fLinkEditBase + fDyldInfo->lazy_bind_off;
const uint8_t* const end = &start[fDyldInfo->lazy_bind_size];
- if ( lazyBindingInfoOffset > fDyldInfo->lazy_bind_size ) {
- dyld::throwf("fast lazy bind offset out of range (%u, max=%u) in image %s",
- lazyBindingInfoOffset, fDyldInfo->lazy_bind_size, this->getPath());
- }
+ if ( lazyBindingInfoOffset > fDyldInfo->lazy_bind_size )
+ throw "fast lazy bind offset out of range";
uint8_t type = BIND_TYPE_POINTER;
uintptr_t address = 0;
@@ -1217,9 +1094,7 @@
address = segActualLoadAddress(immediate) + read_uleb128(p, end);
break;
case BIND_OPCODE_DO_BIND:
-
-
- result = this->bindAt(context, address, type, symbolName, 0, 0, libraryOrdinal, "lazy ", NULL, true);
+ result = this->bindAt(context, address, type, symbolName, 0, 0, libraryOrdinal, "lazy ", NULL);
break;
case BIND_OPCODE_SET_ADDEND_SLEB:
case BIND_OPCODE_ADD_ADDR_ULEB:
@@ -1230,12 +1105,6 @@
dyld::throwf("bad lazy bind opcode %d", *p);
}
}
-
- if ( !this->usesTwoLevelNameSpace() ) {
- // release dyld global lock
- if ( unlock != NULL )
- unlock();
- }
return result;
}
@@ -1421,35 +1290,6 @@
this->addDynamicReference(targetImage);
}
-uintptr_t ImageLoaderMachOCompressed::interposeAt(const LinkContext& context, uintptr_t addr, uint8_t type, const char*,
- uint8_t, intptr_t, int, const char*, LastLookup*, bool runResolver)
-{
- if ( type == BIND_TYPE_POINTER ) {
- uintptr_t* fixupLocation = (uintptr_t*)addr;
- uintptr_t value = *fixupLocation;
- for (std::vector<InterposeTuple>::iterator it=fgInterposingTuples.begin(); it != fgInterposingTuples.end(); it++) {
- // replace all references to 'replacee' with 'replacement'
- if ( (value == it->replacee) && (this != it->replacementImage) ) {
- if ( context.verboseInterposing ) {
- dyld::log("dyld: interposing: at %p replace 0x%lX with 0x%lX in %s\n",
- fixupLocation, it->replacee, it->replacement, this->getPath());
- }
- *fixupLocation = it->replacement;
- }
- }
- }
- return 0;
-}
-
-void ImageLoaderMachOCompressed::doInterpose(const LinkContext& context)
-{
- if ( context.verboseInterposing )
- dyld::log("dyld: interposing %lu tuples onto image: %s\n", fgInterposingTuples.size(), this->getPath());
-
- // update prebound symbols
- eachBind(context, &ImageLoaderMachOCompressed::interposeAt);
- eachLazyBind(context, &ImageLoaderMachOCompressed::interposeAt);
-}
@@ -1514,14 +1354,7 @@
}
}
if ( bestSymbol != NULL ) {
-#if __arm__
- if (bestSymbol->n_desc & N_ARM_THUMB_DEF)
- *closestAddr = (void*)((bestSymbol->n_value | 1) + fSlide);
- else
- *closestAddr = (void*)(bestSymbol->n_value + fSlide);
-#else
*closestAddr = (void*)(bestSymbol->n_value + fSlide);
-#endif
return &symbolTableStrings[bestSymbol->n_un.n_strx];
}
return NULL;
@@ -1535,96 +1368,3 @@
}
#endif
-
-#if __arm__ || __x86_64__
-void ImageLoaderMachOCompressed::updateAlternateLazyPointer(uint8_t* stub, void** originalLazyPointerAddr)
-{
-#if __arm__
- uint32_t* instructions = (uint32_t*)stub;
- // sanity check this is a stub we understand
- if ( (instructions[0] != 0xe59fc004) || (instructions[1] != 0xe08fc00c) || (instructions[2] != 0xe59cf000) )
- return;
-
- void** lazyPointerAddr = (void**)(instructions[3] + (stub + 12));
-#endif
-#if __x86_64__
- // sanity check this is a stub we understand
- if ( (stub[0] != 0xFF) || (stub[1] != 0x25) )
- return;
- int32_t ripOffset = *((int32_t*)(&stub[2]));
- void** lazyPointerAddr = (void**)(ripOffset + stub + 6);
-#endif
-
- // if stub does not use original lazy pointer (meaning it was optimized by update_dyld_shared_cache)
- if ( lazyPointerAddr != originalLazyPointerAddr ) {
- // copy newly re-bound lazy pointer value to shared lazy pointer
- *lazyPointerAddr = *originalLazyPointerAddr;
- }
-}
-#endif
-
-
-// <rdar://problem/8890875> overriding shared cache dylibs with resolvers fails
-void ImageLoaderMachOCompressed::updateOptimizedLazyPointers(const LinkContext& context)
-{
-#if __arm__ || __x86_64__
- // find stubs and lazy pointer sections
- const struct macho_section* stubsSection = NULL;
- const struct macho_section* lazyPointerSection = NULL;
- const dysymtab_command* dynSymbolTable = NULL;
- const macho_header* mh = (macho_header*)fMachOData;
- const uint32_t cmd_count = mh->ncmds;
- const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
- const struct load_command* cmd = cmds;
- for (uint32_t i = 0; i < cmd_count; ++i) {
- if (cmd->cmd == LC_SEGMENT_COMMAND) {
- const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
- const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
- const struct macho_section* const sectionsEnd = §ionsStart[seg->nsects];
- for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
- const uint8_t type = sect->flags & SECTION_TYPE;
- if ( type == S_SYMBOL_STUBS )
- stubsSection = sect;
- else if ( type == S_LAZY_SYMBOL_POINTERS )
- lazyPointerSection = sect;
- }
- }
- else if ( cmd->cmd == LC_DYSYMTAB ) {
- dynSymbolTable = (struct dysymtab_command*)cmd;
- }
- cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
- }
-
- // sanity check
- if ( dynSymbolTable == NULL )
- return;
- if ( (stubsSection == NULL) || (lazyPointerSection == NULL) )
- return;
- const uint32_t stubsCount = stubsSection->size / stubsSection->reserved2;
- const uint32_t lazyPointersCount = lazyPointerSection->size / sizeof(void*);
- if ( stubsCount != lazyPointersCount )
- return;
- const uint32_t stubsIndirectTableOffset = stubsSection->reserved1;
- const uint32_t lazyPointersIndirectTableOffset = lazyPointerSection->reserved1;
- if ( (stubsIndirectTableOffset+stubsCount) > dynSymbolTable->nindirectsyms )
- return;
- if ( (lazyPointersIndirectTableOffset+lazyPointersCount) > dynSymbolTable->nindirectsyms )
- return;
-
- // walk stubs and lazy pointers
- const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[dynSymbolTable->indirectsymoff];
- void** const lazyPointersStartAddr = (void**)(lazyPointerSection->addr + this->fSlide);
- uint8_t* const stubsStartAddr = (uint8_t*)(stubsSection->addr + this->fSlide);
- uint8_t* stub = stubsStartAddr;
- void** lpa = lazyPointersStartAddr;
- for(uint32_t i=0; i < stubsCount; ++i, stub += stubsSection->reserved2, ++lpa) {
- // sanity check symbol index of stub and lazy pointer match
- if ( indirectTable[stubsIndirectTableOffset+i] != indirectTable[lazyPointersIndirectTableOffset+i] )
- continue;
- this->updateAlternateLazyPointer(stub, lpa);
- }
-
-#endif
-}
-
-