Skip to content

Commit

Permalink
530
Browse files Browse the repository at this point in the history
  • Loading branch information
apple-opensource committed May 19, 2020
1 parent 8545b6e commit e7f03bb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/ld/HeaderAndLoadCommands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ HeaderAndLoadCommandsAtom<A>::HeaderAndLoadCommandsAtom(const Options& opts, ld:
_hasUUIDLoadCommand = false;
_hasDynamicSymbolTableLoadCommand = false;
for (std::vector<ld::Internal::FinalSection*>::iterator it = _state.sections.begin(); it != _state.sections.end(); ++it) {
if ( (*it)->type() == ld::Section::typeNonLazyPointer ) {
if ( ((*it)->type() == ld::Section::typeNonLazyPointer) || ((*it)->type() == ld::Section::typeTLVPointers) ) {
_hasDynamicSymbolTableLoadCommand = true;
break;
}
Expand Down
46 changes: 27 additions & 19 deletions src/ld/LinkEditClassic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,32 @@ ld::Section SymbolTableAtom<A>::_s_section("__LINKEDIT", "__symbol_table", ld::S
template <typename A>
int SymbolTableAtom<A>::_s_anonNameIndex = 1;

static bool chainLeadsTo(const ld::Atom* startAtom, const ld::Atom* targetAtom)
{
if ( startAtom == targetAtom )
return true;

for (ld::Fixup::iterator fit = startAtom->fixupsBegin(); fit != startAtom->fixupsEnd(); ++fit) {
if ( (fit->kind == ld::Fixup::kindNoneFollowOn) && (fit->binding == Fixup::bindingDirectlyBound) ) {
const Atom* nextAtom = fit->u.target;
assert(nextAtom != NULL);
if ( chainLeadsTo(nextAtom, targetAtom) )
return true;
}
}
return false;
}

template <typename A>
bool SymbolTableAtom<A>::isAltEntry(const ld::Atom* atom)
{
// alt entries have a group subordinate reference to the previous atom
for (ld::Fixup::iterator fit = atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) {
if ( fit->kind == ld::Fixup::kindNoneGroupSubordinate ) {
if ( fit->binding == Fixup::bindingDirectlyBound ) {
const Atom* prevAtom = fit->u.target;
assert(prevAtom != NULL);
for (ld::Fixup::iterator fit2 = prevAtom->fixupsBegin(); fit2 != prevAtom->fixupsEnd(); ++fit2) {
if ( fit2->kind == ld::Fixup::kindNoneFollowOn ) {
if ( fit2->binding == Fixup::bindingDirectlyBound ) {
if ( fit2->u.target == atom )
return true;
}
}
}
if ( (fit->kind == ld::Fixup::kindNoneGroupSubordinate) && (fit->binding == Fixup::bindingDirectlyBound) ) {
const Atom* chainStart = fit->u.target;
assert(chainStart != NULL);
if ( chainLeadsTo(chainStart, atom) ) {
return true;
}
}
}
Expand Down Expand Up @@ -700,10 +708,14 @@ void SymbolTableAtom<A>::encode()

// go back to start and make nlist entries for all local symbols
std::vector<const ld::Atom*>& localAtoms = this->_writer._localAtoms;
_locals.reserve(localsCount);
symbolIndex = 0;
this->_writer._localSymbolsStartIndex = 0;
_stabsIndexStart = 0;
symbolIndex = 0;
_locals.reserve(localsCount);
for (const ld::Atom* atom : localAtoms) {
if ( this->addLocal(atom, this->_writer._stringPoolAtom) )
this->_writer._atomToSymbolIndex[atom] = symbolIndex++;
}
_stabsIndexStart = symbolIndex;
_stabsStringsOffsetStart = this->_writer._stringPoolAtom->currentOffset();
for (const ld::relocatable::File::Stab& stab : _state.stabs) {
macho_nlist<P> entry;
Expand All @@ -717,10 +729,6 @@ void SymbolTableAtom<A>::encode()
}
_stabsIndexEnd = symbolIndex;
_stabsStringsOffsetEnd = this->_writer._stringPoolAtom->currentOffset();
for (const ld::Atom* atom : localAtoms) {
if ( this->addLocal(atom, this->_writer._stringPoolAtom) )
this->_writer._atomToSymbolIndex[atom] = symbolIndex++;
}
this->_writer._localSymbolsCount = symbolIndex;
}

Expand Down
12 changes: 10 additions & 2 deletions src/ld/parsers/macho_relocatable_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2177,7 +2177,9 @@ bool Parser<A>::parseLoadCommands(const ld::VersionSet& cmdLinePlatforms, bool i
// validate just one segment
if ( segment == NULL )
throw "missing LC_SEGMENT";
if ( segment->filesize() > _fileLength )
if ( segment->fileoff() > _fileLength )
throw "LC_SEGMENT fileoff too large";
if ( (segment->fileoff()+segment->filesize()) > _fileLength )
throw "LC_SEGMENT filesize too large";

// record and validate sections
Expand Down Expand Up @@ -5903,7 +5905,13 @@ void TLVPointerSection<arm>::makeFixups(class Parser<arm>& parser, const struct
target.weakImport = false;
target.addend = 0;
if ( symIndex == INDIRECT_SYMBOL_LOCAL ) {
throwf("unexpected INDIRECT_SYMBOL_LOCAL in section %s", this->sectionName());
// use direct reference for local symbols
const pint_t* nlpContent = (pint_t*)(this->file().fileContent() + sect->offset() + addr - sect->addr());
pint_t targetAddr = P::getP(*nlpContent);
target.atom = parser.findAtomByAddress(targetAddr);
target.weakImport = false;
target.addend = 0;
assert(target.atom->contentType() == ld::Atom::ContentType::typeTLV);
}
else {
const macho_nlist<P>& sym = parser.symbolFromIndex(symIndex);
Expand Down

0 comments on commit e7f03bb

Please sign in to comment.