Changeset View
Changeset View
Standalone View
Standalone View
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
Show First 20 Lines • Show All 166 Lines • ▼ Show 20 Lines | void segIndexForSection(const SectionInfo *sect, | ||||
uint8_t &segmentIndex, uint64_t &segmentStartAddr); | uint8_t &segmentIndex, uint64_t &segmentStartAddr); | ||||
const Atom *targetOfLazyPointer(const DefinedAtom *lpAtom); | const Atom *targetOfLazyPointer(const DefinedAtom *lpAtom); | ||||
const Atom *targetOfStub(const DefinedAtom *stubAtom); | const Atom *targetOfStub(const DefinedAtom *stubAtom); | ||||
llvm::Error getSymbolTableRegion(const DefinedAtom* atom, | llvm::Error getSymbolTableRegion(const DefinedAtom* atom, | ||||
bool &inGlobalsRegion, | bool &inGlobalsRegion, | ||||
SymbolScope &symbolScope); | SymbolScope &symbolScope); | ||||
void appendSection(SectionInfo *si, NormalizedFile &file); | void appendSection(SectionInfo *si, NormalizedFile &file); | ||||
uint32_t sectionIndexForAtom(const Atom *atom); | uint32_t sectionIndexForAtom(const Atom *atom); | ||||
void fixLazyReferenceImm(const DefinedAtom *atom, uint32_t offset, | |||||
NormalizedFile &file); | |||||
typedef llvm::DenseMap<const Atom*, uint32_t> AtomToIndex; | typedef llvm::DenseMap<const Atom*, uint32_t> AtomToIndex; | ||||
struct AtomAndIndex { const Atom *atom; uint32_t index; SymbolScope scope; }; | struct AtomAndIndex { const Atom *atom; uint32_t index; SymbolScope scope; }; | ||||
struct AtomSorter { | struct AtomSorter { | ||||
bool operator()(const AtomAndIndex &left, const AtomAndIndex &right); | bool operator()(const AtomAndIndex &left, const AtomAndIndex &right); | ||||
}; | }; | ||||
struct SegmentSorter { | struct SegmentSorter { | ||||
bool operator()(const SegmentInfo *left, const SegmentInfo *right); | bool operator()(const SegmentInfo *left, const SegmentInfo *right); | ||||
▲ Show 20 Lines • Show All 1,235 Lines • ▼ Show 20 Lines | |||||
void Util::addRebaseAndBindingInfo(const lld::File &atomFile, | void Util::addRebaseAndBindingInfo(const lld::File &atomFile, | ||||
NormalizedFile &nFile) { | NormalizedFile &nFile) { | ||||
if (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT) | if (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT) | ||||
return; | return; | ||||
uint8_t segmentIndex; | uint8_t segmentIndex; | ||||
uint64_t segmentStartAddr; | uint64_t segmentStartAddr; | ||||
uint32_t offsetInBindInfo = 0; | |||||
for (SectionInfo *sect : _sectionInfos) { | for (SectionInfo *sect : _sectionInfos) { | ||||
segIndexForSection(sect, segmentIndex, segmentStartAddr); | segIndexForSection(sect, segmentIndex, segmentStartAddr); | ||||
for (const AtomInfo &info : sect->atomsAndOffsets) { | for (const AtomInfo &info : sect->atomsAndOffsets) { | ||||
const DefinedAtom *atom = info.atom; | const DefinedAtom *atom = info.atom; | ||||
for (const Reference *ref : *atom) { | for (const Reference *ref : *atom) { | ||||
uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom() | uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom() | ||||
- segmentStartAddr; | - segmentStartAddr; | ||||
const Atom* targ = ref->target(); | const Atom* targ = ref->target(); | ||||
Show All 28 Lines | for (const AtomInfo &info : sect->atomsAndOffsets) { | ||||
} | } | ||||
bind.segIndex = segmentIndex; | bind.segIndex = segmentIndex; | ||||
bind.segOffset = segmentOffset; | bind.segOffset = segmentOffset; | ||||
bind.kind = llvm::MachO::BIND_TYPE_POINTER; | bind.kind = llvm::MachO::BIND_TYPE_POINTER; | ||||
bind.canBeNull = false; //sa->canBeNullAtRuntime(); | bind.canBeNull = false; //sa->canBeNullAtRuntime(); | ||||
bind.symbolName = targ->name(); | bind.symbolName = targ->name(); | ||||
bind.addend = ref->addend(); | bind.addend = ref->addend(); | ||||
nFile.lazyBindingInfo.push_back(bind); | nFile.lazyBindingInfo.push_back(bind); | ||||
// Now that we know the segmentOffset and the ordinal attribute, | |||||
// we can fix the helper's code | |||||
fixLazyReferenceImm(atom, offsetInBindInfo, nFile); | |||||
// 5 bytes for opcodes + variable sizes (target name + \0 and offset | |||||
// encode's size) | |||||
offsetInBindInfo += | |||||
6 + targ->name().size() + llvm::getULEB128Size(bind.segOffset); | |||||
if (bind.ordinal > BIND_IMMEDIATE_MASK) | |||||
offsetInBindInfo += llvm::getULEB128Size(bind.ordinal); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | |||||
void Util::fixLazyReferenceImm(const DefinedAtom *atom, uint32_t offset, | |||||
NormalizedFile &file) { | |||||
for (const auto &ref : *atom) { | |||||
const DefinedAtom *da = dyn_cast<DefinedAtom>(ref->target()); | |||||
if (da == nullptr) | |||||
return; | |||||
const Reference *helperRef = nullptr; | |||||
for (const Reference *hr : *da) { | |||||
if (hr->kindValue() == _archHandler.lazyImmediateLocationKind()) { | |||||
helperRef = hr; | |||||
break; | |||||
} | |||||
} | |||||
if (helperRef == nullptr) | |||||
continue; | |||||
// TODO: maybe get the fixed atom content from _archHandler ? | |||||
for (SectionInfo *sectInfo : _sectionInfos) { | |||||
for (const AtomInfo &atomInfo : sectInfo->atomsAndOffsets) { | |||||
if (atomInfo.atom == helperRef->target()) { | |||||
auto sectionContent = | |||||
file.sections[sectInfo->normalizedSectionIndex].content; | |||||
uint8_t *rawb = | |||||
file.ownedAllocations.Allocate<uint8_t>(sectionContent.size()); | |||||
llvm::MutableArrayRef<uint8_t> newContent{rawb, | |||||
sectionContent.size()}; | |||||
std::copy(sectionContent.begin(), sectionContent.end(), | |||||
newContent.begin()); | |||||
llvm::support::ulittle32_t *loc = | |||||
reinterpret_cast<llvm::support::ulittle32_t *>( | |||||
&newContent[atomInfo.offsetInSection + | |||||
helperRef->offsetInAtom()]); | |||||
*loc = offset; | |||||
file.sections[sectInfo->normalizedSectionIndex].content = newContent; | |||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void Util::addExportInfo(const lld::File &atomFile, NormalizedFile &nFile) { | void Util::addExportInfo(const lld::File &atomFile, NormalizedFile &nFile) { | ||||
if (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT) | if (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT) | ||||
▲ Show 20 Lines • Show All 123 Lines • Show Last 20 Lines |