diff options
Diffstat (limited to 'lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp')
| -rw-r--r-- | lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp | 208 |
1 files changed, 98 insertions, 110 deletions
diff --git a/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp b/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp index f80e2ac467fc..e830db9fcc7b 100644 --- a/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp +++ b/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp @@ -406,14 +406,8 @@ bool Util::TextSectionSorter::operator()(const SectionInfo *left, } void Util::organizeSections() { - if (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT) { - // Leave sections ordered as normalized file specified. - uint32_t sectionIndex = 1; - for (SectionInfo *si : _sectionInfos) { - si->finalSectionIndex = sectionIndex++; - } - } else { - switch (_ctx.outputMachOType()) { + // NOTE!: Keep this in sync with assignAddressesToSections. + switch (_ctx.outputMachOType()) { case llvm::MachO::MH_EXECUTE: // Main executables, need a zero-page segment segmentForName("__PAGEZERO"); @@ -425,32 +419,30 @@ void Util::organizeSections() { break; default: break; - } - // Group sections into segments. - for (SectionInfo *si : _sectionInfos) { - SegmentInfo *seg = segmentForName(si->segmentName); - seg->sections.push_back(si); - } - // Sort segments. - std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter()); + } + // Group sections into segments. + for (SectionInfo *si : _sectionInfos) { + SegmentInfo *seg = segmentForName(si->segmentName); + seg->sections.push_back(si); + } + // Sort segments. + std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter()); - // Sort sections within segments. - for (SegmentInfo *seg : _segmentInfos) { - if (seg->name.equals("__TEXT")) { - std::sort(seg->sections.begin(), seg->sections.end(), - TextSectionSorter()); - } + // Sort sections within segments. + for (SegmentInfo *seg : _segmentInfos) { + if (seg->name.equals("__TEXT")) { + std::sort(seg->sections.begin(), seg->sections.end(), + TextSectionSorter()); } + } - // Record final section indexes. - uint32_t segmentIndex = 0; - uint32_t sectionIndex = 1; - for (SegmentInfo *seg : _segmentInfos) { - seg->normalizedSegmentIndex = segmentIndex++; - for (SectionInfo *sect : seg->sections) { - sect->finalSectionIndex = sectionIndex++; - } - } + // Record final section indexes. + uint32_t segmentIndex = 0; + uint32_t sectionIndex = 1; + for (SegmentInfo *seg : _segmentInfos) { + seg->normalizedSegmentIndex = segmentIndex++; + for (SectionInfo *sect : seg->sections) + sect->finalSectionIndex = sectionIndex++; } } @@ -487,54 +479,39 @@ void Util::layoutSectionsInTextSegment(size_t hlcSize, SegmentInfo *seg, } void Util::assignAddressesToSections(const NormalizedFile &file) { + // NOTE!: Keep this in sync with organizeSections. size_t hlcSize = headerAndLoadCommandsSize(file); uint64_t address = 0; - if (_ctx.outputMachOType() != llvm::MachO::MH_OBJECT) { - for (SegmentInfo *seg : _segmentInfos) { - if (seg->name.equals("__PAGEZERO")) { - seg->size = _ctx.pageZeroSize(); - address += seg->size; - } - else if (seg->name.equals("__TEXT")) { - // _ctx.baseAddress() == 0 implies it was either unspecified or - // pageZeroSize is also 0. In either case resetting address is safe. - address = _ctx.baseAddress() ? _ctx.baseAddress() : address; - layoutSectionsInTextSegment(hlcSize, seg, address); - } else - layoutSectionsInSegment(seg, address); - - address = llvm::RoundUpToAlignment(address, _ctx.pageSize()); + for (SegmentInfo *seg : _segmentInfos) { + if (seg->name.equals("__PAGEZERO")) { + seg->size = _ctx.pageZeroSize(); + address += seg->size; } - DEBUG_WITH_TYPE("WriterMachO-norm", - llvm::dbgs() << "assignAddressesToSections()\n"; - for (SegmentInfo *sgi : _segmentInfos) { - llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address) - << ", size=" << llvm::format("0x%08llX", sgi->size) - << ", segment-name='" << sgi->name - << "'\n"; - for (SectionInfo *si : sgi->sections) { - llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address) - << ", size=" << llvm::format("0x%08llX", si->size) - << ", section-name='" << si->sectionName - << "\n"; - } + else if (seg->name.equals("__TEXT")) { + // _ctx.baseAddress() == 0 implies it was either unspecified or + // pageZeroSize is also 0. In either case resetting address is safe. + address = _ctx.baseAddress() ? _ctx.baseAddress() : address; + layoutSectionsInTextSegment(hlcSize, seg, address); + } else + layoutSectionsInSegment(seg, address); + + address = llvm::RoundUpToAlignment(address, _ctx.pageSize()); + } + DEBUG_WITH_TYPE("WriterMachO-norm", + llvm::dbgs() << "assignAddressesToSections()\n"; + for (SegmentInfo *sgi : _segmentInfos) { + llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address) + << ", size=" << llvm::format("0x%08llX", sgi->size) + << ", segment-name='" << sgi->name + << "'\n"; + for (SectionInfo *si : sgi->sections) { + llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address) + << ", size=" << llvm::format("0x%08llX", si->size) + << ", section-name='" << si->sectionName + << "\n"; } - ); - } else { - for (SectionInfo *sect : _sectionInfos) { - sect->address = llvm::RoundUpToAlignment(address, sect->alignment); - address = sect->address + sect->size; } - DEBUG_WITH_TYPE("WriterMachO-norm", - llvm::dbgs() << "assignAddressesToSections()\n"; - for (SectionInfo *si : _sectionInfos) { - llvm::dbgs() << " section=" << si->sectionName - << " address= " << llvm::format("0x%08X", si->address) - << " size= " << llvm::format("0x%08X", si->size) - << "\n"; - } - ); - } + ); } void Util::copySegmentInfo(NormalizedFile &file) { @@ -604,16 +581,9 @@ void Util::copySectionContent(NormalizedFile &file) { void Util::copySectionInfo(NormalizedFile &file) { file.sections.reserve(_sectionInfos.size()); - // For final linked images, write sections grouped by segment. - if (_ctx.outputMachOType() != llvm::MachO::MH_OBJECT) { - for (SegmentInfo *sgi : _segmentInfos) { - for (SectionInfo *si : sgi->sections) { - appendSection(si, file); - } - } - } else { - // Object files write sections in default order. - for (SectionInfo *si : _sectionInfos) { + // Write sections grouped by segment. + for (SegmentInfo *sgi : _segmentInfos) { + for (SectionInfo *si : sgi->sections) { appendSection(si, file); } } @@ -621,20 +591,12 @@ void Util::copySectionInfo(NormalizedFile &file) { void Util::updateSectionInfo(NormalizedFile &file) { file.sections.reserve(_sectionInfos.size()); - if (_ctx.outputMachOType() != llvm::MachO::MH_OBJECT) { - // For final linked images, sections grouped by segment. - for (SegmentInfo *sgi : _segmentInfos) { - Segment *normSeg = &file.segments[sgi->normalizedSegmentIndex]; - normSeg->address = sgi->address; - normSeg->size = sgi->size; - for (SectionInfo *si : sgi->sections) { - Section *normSect = &file.sections[si->normalizedSectionIndex]; - normSect->address = si->address; - } - } - } else { - // Object files write sections in default order. - for (SectionInfo *si : _sectionInfos) { + // sections grouped by segment. + for (SegmentInfo *sgi : _segmentInfos) { + Segment *normSeg = &file.segments[sgi->normalizedSegmentIndex]; + normSeg->address = sgi->address; + normSeg->size = sgi->size; + for (SectionInfo *si : sgi->sections) { Section *normSect = &file.sections[si->normalizedSectionIndex]; normSect->address = si->address; } @@ -663,19 +625,47 @@ void Util::buildAtomToAddressMap() { _entryAtom = info.atom; } DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs() - << " address=" - << llvm::format("0x%016X", _atomToAddress[info.atom]) - << " atom=" << info.atom - << " name=" << info.atom->name() << "\n"); + << " address=" + << llvm::format("0x%016X", _atomToAddress[info.atom]) + << llvm::format(" 0x%09lX", info.atom) + << ", file=#" + << info.atom->file().ordinal() + << ", atom=#" + << info.atom->ordinal() + << ", name=" + << info.atom->name() + << ", type=" + << info.atom->contentType() + << "\n"); } } + DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs() + << "assign header alias atom addresses:\n"); for (const Atom *atom : _machHeaderAliasAtoms) { _atomToAddress[atom] = _ctx.baseAddress(); - DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs() - << " address=" - << llvm::format("0x%016X", _atomToAddress[atom]) - << " atom=" << atom - << " name=" << atom->name() << "\n"); +#ifndef NDEBUG + if (auto *definedAtom = dyn_cast<DefinedAtom>(atom)) { + DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs() + << " address=" + << llvm::format("0x%016X", _atomToAddress[atom]) + << llvm::format(" 0x%09lX", atom) + << ", file=#" + << definedAtom->file().ordinal() + << ", atom=#" + << definedAtom->ordinal() + << ", name=" + << definedAtom->name() + << ", type=" + << definedAtom->contentType() + << "\n"); + } else { + DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs() + << " address=" + << llvm::format("0x%016X", _atomToAddress[atom]) + << " atom=" << atom + << " name=" << atom->name() << "\n"); + } +#endif } } @@ -999,11 +989,9 @@ void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex, uint32_t Util::sectionIndexForAtom(const Atom *atom) { uint64_t address = _atomToAddress[atom]; - uint32_t index = 1; for (const SectionInfo *si : _sectionInfos) { if ((si->address <= address) && (address < si->address+si->size)) - return index; - ++index; + return si->finalSectionIndex; } llvm_unreachable("atom not in any section"); } |
