+ // count how many have not already had weakbinding done
+ int countNotYetWeakBound = 0;
+ int countOfImagesWithWeakDefinitionsNotInSharedCache = 0;
+ for(int i=0; i < count; ++i) {
+ if ( ! imagesNeedingCoalescing[i]->weakSymbolsBound(imageIndexes[i]) )
+ ++countNotYetWeakBound;
+ if ( ! imagesNeedingCoalescing[i]->inSharedCache() )
+ ++countOfImagesWithWeakDefinitionsNotInSharedCache;
+ }
+
+ // don't need to do any coalescing if only one image has overrides, or all have already been done
+ if ( (countOfImagesWithWeakDefinitionsNotInSharedCache > 0) && (countNotYetWeakBound > 0) ) {
+ // make symbol iterators for each
+ ImageLoader::CoalIterator iterators[count];
+ ImageLoader::CoalIterator* sortedIts[count];
+ for(int i=0; i < count; ++i) {
+ imagesNeedingCoalescing[i]->initializeCoalIterator(iterators[i], i, imageIndexes[i]);
+ sortedIts[i] = &iterators[i];
+ if ( context.verboseWeakBind )
+ dyld::log("dyld: weak bind load order %d/%d for %s\n", i, count, imagesNeedingCoalescing[i]->getIndexedPath(imageIndexes[i]));
+ }
+
+ // walk all symbols keeping iterators in sync by
+ // only ever incrementing the iterator with the lowest symbol
+ int doneCount = 0;
+ while ( doneCount != count ) {
+ //for(int i=0; i < count; ++i)
+ // dyld::log("sym[%d]=%s ", sortedIts[i]->loadOrder, sortedIts[i]->symbolName);
+ //dyld::log("\n");
+ // increment iterator with lowest symbol
+ if ( sortedIts[0]->image->incrementCoalIterator(*sortedIts[0]) )
+ ++doneCount;
+ // re-sort iterators
+ for(int i=1; i < count; ++i) {
+ int result = strcmp(sortedIts[i-1]->symbolName, sortedIts[i]->symbolName);
+ if ( result == 0 )
+ sortedIts[i-1]->symbolMatches = true;
+ if ( result > 0 ) {
+ // new one is bigger then next, so swap
+ ImageLoader::CoalIterator* temp = sortedIts[i-1];
+ sortedIts[i-1] = sortedIts[i];
+ sortedIts[i] = temp;
+ }
+ if ( result < 0 )
+ break;
+ }
+ // process all matching symbols just before incrementing the lowest one that matches
+ if ( sortedIts[0]->symbolMatches && !sortedIts[0]->done ) {
+ const char* nameToCoalesce = sortedIts[0]->symbolName;
+ // pick first symbol in load order (and non-weak overrides weak)
+ uintptr_t targetAddr = 0;
+ ImageLoader* targetImage = NULL;
+ unsigned targetImageIndex = 0;
+ for(int i=0; i < count; ++i) {
+ if ( strcmp(iterators[i].symbolName, nameToCoalesce) == 0 ) {
+ if ( context.verboseWeakBind )
+ dyld::log("dyld: weak bind, found %s weak=%d in %s \n", nameToCoalesce, iterators[i].weakSymbol, iterators[i].image->getIndexedPath((unsigned)iterators[i].imageIndex));
+ if ( iterators[i].weakSymbol ) {
+ if ( targetAddr == 0 ) {
+ targetAddr = iterators[i].image->getAddressCoalIterator(iterators[i], context);
+ if ( targetAddr != 0 ) {
+ targetImage = iterators[i].image;
+ targetImageIndex = (unsigned)iterators[i].imageIndex;
+ }
+ }
+ }
+ else {
+ targetAddr = iterators[i].image->getAddressCoalIterator(iterators[i], context);
+ if ( targetAddr != 0 ) {
+ targetImage = iterators[i].image;
+ targetImageIndex = (unsigned)iterators[i].imageIndex;
+ // strong implementation found, stop searching
+ break;
+ }
+ }
+ }
+ }
+ // tell each to bind to this symbol (unless already bound)
+ if ( targetAddr != 0 ) {
+ if ( context.verboseWeakBind ) {
+ dyld::log("dyld: weak binding all uses of %s to copy from %s\n",
+ nameToCoalesce, targetImage->getIndexedShortName(targetImageIndex));
+ }
+ for(int i=0; i < count; ++i) {
+ if ( strcmp(iterators[i].symbolName, nameToCoalesce) == 0 ) {
+ if ( context.verboseWeakBind ) {
+ dyld::log("dyld: weak bind, setting all uses of %s in %s to 0x%lX from %s\n",
+ nameToCoalesce, iterators[i].image->getIndexedShortName((unsigned)iterators[i].imageIndex),
+ targetAddr, targetImage->getIndexedShortName(targetImageIndex));
+ }
+ if ( ! iterators[i].image->weakSymbolsBound(imageIndexes[i]) )
+ iterators[i].image->updateUsesCoalIterator(iterators[i], targetAddr, targetImage, targetImageIndex, context);
+ iterators[i].symbolMatches = false;
+ }
+ }
+ }
+
+ }
+ }
+
+ // mark all as having all weak symbols bound
+ for(int i=0; i < count; ++i) {
+ imagesNeedingCoalescing[i]->setWeakSymbolsBound(imageIndexes[i]);