Skip to content

Instantly share code, notes, and snippets.

@stanislaw
Created February 21, 2018 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stanislaw/8db921897800df9bba4d867bb94520c4 to your computer and use it in GitHub Desktop.
Save stanislaw/8db921897800df9bba4d867bb94520c4 to your computer and use it in GitHub Desktop.
Fixup selectors
// Resolve the relocations for all symbols we currently know about.
void RuntimeDyldImpl::resolveRelocations() {
MutexGuard locked(lock);
// Print out the sections prior to relocation.
DEBUG(
for (int i = 0, e = Sections.size(); i != e; ++i)
dumpSectionMemory(Sections[i], "before relocations");
);
// First, resolve relocations associated with external symbols.
resolveExternalSymbols();
// Iterate over all outstanding relocations
for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) {
// The Section here (Sections[i]) refers to the section in which the
// symbol for the relocation is located. The SectionID in the relocation
// entry provides the section to which the relocation will be applied.
int Idx = it->first;
uint64_t Addr = Sections[Idx].getLoadAddress();
DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t"
<< format("%p", (uintptr_t)Addr) << "\n");
resolveRelocationList(it->second, Addr);
// OBJC CLASSES
if (Sections[Idx].getName().startswith("__objc_classrefs")) {
errs() << "OBJC CLASSES" << "\n";
SectionEntry &selrefsSection = Sections[Idx];
uint64_t FinalAddress = selrefsSection.getLoadAddress() + it->second[0].Offset;
errs() << "FinalADdress: " << (void *)FinalAddress << "\n";
// __objc_selrefs
uint8_t *DataAddr = selrefsSection.getAddress();
uint64_t LoadAddr = selrefsSection.getLoadAddress();
Class *classrefs = (uint8_t * )
remapClassRef(&classrefs[i]);
}
SEL *sels = (SEL *)DataAddr;
for (i = 0; i < count; i++) {
SEL *selector = (SEL *)cursor;
const char *name = sel_getName(*selector);
errs() << "Trying to register selector: " << name << "\n";
*selector = sel_registerName(name);
}
// sels[0];
exit(1);
}
// OBJC SELECTORS
if (Sections[Idx].getName().startswith("__objc_selrefs")) {
errs() << "OBJC SELECTORS" << "\n";
SectionEntry &selrefsSection = Sections[Idx];
uint64_t FinalAddress = selrefsSection.getLoadAddress() + it->second[0].Offset;
errs() << "FinalADdress: " << (void *)FinalAddress << "\n";
// __objc_selrefs
uint8_t *DataAddr = selrefsSection.getAddress();
uint64_t LoadAddr = selrefsSection.getLoadAddress();
SEL *sels = (SEL *)DataAddr;
for (uint8_t *cursor = DataAddr;
cursor < (DataAddr + selrefsSection.getSize());
cursor = cursor + sizeof(SEL)) {
SEL *selector = (SEL *)cursor;
const char *name = sel_getName(*selector);
errs() << "Trying to register selector: " << name << "\n";
*selector = sel_registerName(name);
}
// sels[0];
// exit(1);
}
}
Relocations.clear();
// Print out sections after relocation.
DEBUG(
for (int i = 0, e = Sections.size(); i != e; ++i)
dumpSectionMemory(Sections[i], "after relocations");
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment