-
-
- // add stubs to fAllAtoms
- if ( fAllSynthesizedStubs.size() != 0 ) {
- std::vector<ObjectFile::Atom*> textStubs;
- std::vector<ObjectFile::Atom*> importStubs;
- for (typename std::vector<class StubAtom<A>*>::iterator sit=fAllSynthesizedStubs.begin(); sit != fAllSynthesizedStubs.end(); ++sit) {
- ObjectFile::Atom* stubAtom = *sit;
- if ( strcmp(stubAtom->getSegment().getName(), "__TEXT") == 0 )
- textStubs.push_back(stubAtom);
- else
- importStubs.push_back(stubAtom);
- }
- // any helper stubs go right after regular stubs
- if ( fAllSynthesizedStubHelpers.size() != 0 )
- textStubs.insert(textStubs.end(), fAllSynthesizedStubHelpers.begin(), fAllSynthesizedStubHelpers.end());
- // insert text stubs right after __text section
- ObjectFile::Section* curSection = NULL;
- ObjectFile::Atom* prevAtom = NULL;
- for (std::vector<ObjectFile::Atom*>::iterator it=fAllAtoms->begin(); it != fAllAtoms->end(); it++) {
- ObjectFile::Atom* atom = *it;
- ObjectFile::Section* nextSection = atom->getSection();
- if ( nextSection != curSection ) {
- if ( (prevAtom != NULL) && (strcmp(prevAtom->getSectionName(), "__text") == 0) ) {
- // found end of __text section, insert stubs here
- fAllAtoms->insert(it, textStubs.begin(), textStubs.end());
- break;
- }
- curSection = nextSection;
- }
- prevAtom = atom;
- }
- if ( importStubs.size() != 0 ) {
- // insert __IMPORTS stubs right before __LINKEDIT
- for (std::vector<ObjectFile::Atom*>::iterator it=fAllAtoms->begin(); it != fAllAtoms->end(); it++) {
- ObjectFile::Atom* atom = *it;
- ObjectFile::Section* nextSection = atom->getSection();
- if ( nextSection != curSection ) {
- // for i386 where stubs are not in __TEXT segment
- if ( ((prevAtom != NULL) && (strcmp(prevAtom->getSegment().getName(), "__IMPORT") == 0))
- || (strcmp(atom->getSegment().getName(), "__LINKEDIT") == 0) ) {
- // insert stubs at end of __IMPORT segment, or before __LINKEDIT
- fAllAtoms->insert(it, importStubs.begin(), importStubs.end());
- break;
- }
- curSection = nextSection;
- }
- prevAtom = atom;
- }
- }
- }
-
-
- // add non-lazy pointers to fAllAtoms
- if ( fAllSynthesizedNonLazyPointers.size() != 0 ) {
- ObjectFile::Section* curSection = NULL;
- ObjectFile::Atom* prevAtom = NULL;
- bool inserted = false;
- // first try to insert at end of __nl_symbol_ptr
- for (std::vector<ObjectFile::Atom*>::iterator it=fAllAtoms->begin(); it != fAllAtoms->end(); it++) {
- ObjectFile::Atom* atom = *it;
- ObjectFile::Section* nextSection = atom->getSection();
- if ( nextSection != curSection ) {
- if ( (prevAtom != NULL) && (strcmp(prevAtom->getSectionName(), "__nl_symbol_ptr") == 0) ) {
- // found end of __nl_symbol_ptr section, insert non-lazy pointers at end of it
- fAllAtoms->insert(it, fAllSynthesizedNonLazyPointers.begin(), fAllSynthesizedNonLazyPointers.end());
- inserted = true;
- break;
- }
- curSection = nextSection;
- }
- prevAtom = atom;
- }
- if ( !inserted ) {
- // next try to insert after __dyld section
- for (std::vector<ObjectFile::Atom*>::iterator it=fAllAtoms->begin(); it != fAllAtoms->end(); it++) {
- ObjectFile::Atom* atom = *it;
- ObjectFile::Section* nextSection = atom->getSection();
- if ( nextSection != curSection ) {
- if ( strcmp(atom->getSegment().getName(), "__DATA") == 0 ) {
- const char* prevSectionName = (prevAtom != NULL) ? prevAtom->getSectionName() : "";
- if ( (strcmp(prevSectionName, "__dyld") != 0)
- && (strcmp(prevSectionName, "__program_vars") != 0)
- && (strcmp(prevSectionName, "__mod_init_func") != 0) ) {
- // found end of __dyld section, insert non-lazy pointers here
- fAllAtoms->insert(it, fAllSynthesizedNonLazyPointers.begin(), fAllSynthesizedNonLazyPointers.end());
- inserted = true;
- break;
- }
- }
- }
- prevAtom = atom;
- }
- if ( !inserted ) {
- // might not be any __DATA sections, insert after end of __TEXT
- for (std::vector<ObjectFile::Atom*>::iterator it=fAllAtoms->begin(); it != fAllAtoms->end(); it++) {
- ObjectFile::Atom* atom = *it;
- ObjectFile::Section* nextSection = atom->getSection();
- if ( nextSection != curSection ) {
- if ( (prevAtom != NULL) && (strcmp(prevAtom->getSegment().getName(), "__TEXT") == 0) && (strcmp(atom->getSegment().getName(), "__TEXT") != 0)) {
- // found end of __TEXT segment, insert non-lazy pointers at end of it
- fAllAtoms->insert(it, fAllSynthesizedNonLazyPointers.begin(), fAllSynthesizedNonLazyPointers.end());
- inserted = true;
- break;
- }
- curSection = nextSection;
- }
- prevAtom = atom;
- }
- }
- if ( !inserted )
- throw "can't insert non-lazy pointers, __dyld section not found";
- }
- }
-
- // add lazy dylib pointers to fAllAtoms
- if ( fAllSynthesizedLazyDylibPointers.size() != 0 ) {
- ObjectFile::Section* curSection = NULL;
- ObjectFile::Atom* prevAtom = NULL;
- bool inserted = false;
- for (std::vector<ObjectFile::Atom*>::iterator it=fAllAtoms->begin(); it != fAllAtoms->end(); it++) {
- ObjectFile::Atom* atom = *it;
- ObjectFile::Section* nextSection = atom->getSection();
- if ( nextSection != curSection ) {
- if ( (prevAtom != NULL) &&
- ( (strcmp(prevAtom->getSectionName(), "__dyld") == 0)
- || (strcmp(prevAtom->getSectionName(), "__program_vars") == 0)
- || (strcmp(prevAtom->getSectionName(), "__nl_symbol_ptr") == 0) ) ) {
- // found end of __dyld section, insert lazy pointers here
- fAllAtoms->insert(it, fAllSynthesizedLazyDylibPointers.begin(), fAllSynthesizedLazyDylibPointers.end());
- inserted = true;
- break;
- }
- curSection = nextSection;
- }
- prevAtom = atom;
- }
- if ( !inserted ) {
- throw "can't insert lazy pointers, __dyld section not found";
- }
- }
-
- // add lazy pointers to fAllAtoms
- if ( fAllSynthesizedLazyPointers.size() != 0 ) {
- ObjectFile::Section* curSection = NULL;
- ObjectFile::Atom* prevAtom = NULL;
- bool inserted = false;
- for (std::vector<ObjectFile::Atom*>::iterator it=fAllAtoms->begin(); it != fAllAtoms->end(); it++) {
- ObjectFile::Atom* atom = *it;
- ObjectFile::Section* nextSection = atom->getSection();
- if ( nextSection != curSection ) {
- if ( (prevAtom != NULL) &&
- ( (strcmp(prevAtom->getSectionName(), "__dyld") == 0)
- || (strcmp(prevAtom->getSectionName(), "__program_vars") == 0)
- || (strcmp(prevAtom->getSectionName(), "__nl_symbol_ptr") == 0) ) ) {
- // found end of __dyld section, insert lazy pointers here
- fAllAtoms->insert(it, fAllSynthesizedLazyPointers.begin(), fAllSynthesizedLazyPointers.end());
- inserted = true;
- break;
- }
- curSection = nextSection;
- }
- prevAtom = atom;
- }
- if ( !inserted ) {
- throw "can't insert lazy pointers, __dyld section not found";
- }
- }