+ uintptr_t value = *location;
+ uintptr_t newValue = interposedAddress(context, value, this);
+ if ( newValue != value )
+ *location = newValue;
+ }
+ break;
+ }
+ }
+ }
+}
+
+void ImageLoaderMachOClassic::dynamicInterpose(const LinkContext& context)
+{
+ if ( context.verboseInterposing )
+ dyld::log("dyld: dynamic interposing %lu tuples onto image: %s\n", context.dynamicInterposeCount, this->getPath());
+
+ // scan indirect symbols
+ const uint32_t cmd_count = ((macho_header*)fMachOData)->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) {
+ switch (cmd->cmd) {
+ case 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_NON_LAZY_SYMBOL_POINTERS) || (type == S_LAZY_SYMBOL_POINTERS) ) {
+ const size_t pointerCount = sect->size / sizeof(uintptr_t);
+ uintptr_t* const symbolPointers = (uintptr_t*)(sect->addr + fSlide);
+ for (size_t pointerIndex=0; pointerIndex < pointerCount; ++pointerIndex) {
+ for(size_t j=0; j < context.dynamicInterposeCount; ++j) {
+ // replace all references to 'replacee' with 'replacement'
+ if ( symbolPointers[pointerIndex] == (uintptr_t)context.dynamicInterposeArray[j].replacee ) {
+ if ( context.verboseInterposing ) {
+ dyld::log("dyld: dynamic interposing: at %p replace %p with %p in %s\n",
+ &symbolPointers[pointerIndex], context.dynamicInterposeArray[j].replacee, context.dynamicInterposeArray[j].replacement, this->getPath());
+ }
+ symbolPointers[pointerIndex] = (uintptr_t)context.dynamicInterposeArray[j].replacement;
+ }
+ }
+ }
+ }
+ }
+ }
+ break;
+ }
+ cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
+ }
+
+ // scan external relocations
+ const uintptr_t relocBase = this->getRelocBase();
+ const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->extreloff]);
+ const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nextrel];
+ for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) {
+ if (reloc->r_length == RELOC_SIZE) {
+ switch(reloc->r_type) {
+ case POINTER_RELOC:
+ {
+ uintptr_t* location = ((uintptr_t*)(reloc->r_address + relocBase));
+ for(size_t i=0; i < context.dynamicInterposeCount; ++i) {