[C++11] Introduce ObjectFile::sections().

Summary:
This adds ObjectFile::section_iterator_range, that allows to write
range-based for-loops running over all sections of a given file.
Several files from lib/ are converted to the new interface. Similar fixes
should be applied to a variety of llvm-* tools.

Reviewers: rafael

Reviewed By: rafael

CC: llvm-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D3069

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203799 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alexey Samsonov
2014-03-13 13:52:54 +00:00
parent f8909fa140
commit b920dfe02b
5 changed files with 63 additions and 55 deletions

View File

@ -289,6 +289,11 @@ public:
virtual section_iterator section_begin() const = 0; virtual section_iterator section_begin() const = 0;
virtual section_iterator section_end() const = 0; virtual section_iterator section_end() const = 0;
typedef iterator_range<section_iterator> section_iterator_range;
section_iterator_range sections() const {
return section_iterator_range(section_begin(), section_end());
}
virtual library_iterator needed_library_begin() const = 0; virtual library_iterator needed_library_begin() const = 0;
virtual library_iterator needed_library_end() const = 0; virtual library_iterator needed_library_end() const = 0;

View File

@ -605,16 +605,14 @@ static bool consumeCompressedDebugSectionHeader(StringRef &data,
return true; return true;
} }
DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) : DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj)
IsLittleEndian(Obj->isLittleEndian()), : IsLittleEndian(Obj->isLittleEndian()),
AddressSize(Obj->getBytesInAddress()) { AddressSize(Obj->getBytesInAddress()) {
for (object::section_iterator i = Obj->section_begin(), for (const SectionRef &Section : Obj->sections()) {
e = Obj->section_end();
i != e; ++i) {
StringRef name; StringRef name;
i->getName(name); Section.getName(name);
StringRef data; StringRef data;
i->getContents(data); Section.getContents(data);
name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes. name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
@ -634,7 +632,7 @@ DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
UncompressedSections.push_back(std::move(UncompressedSection)); UncompressedSections.push_back(std::move(UncompressedSection));
} }
StringRef *Section = StringRef *SectionData =
StringSwitch<StringRef *>(name) StringSwitch<StringRef *>(name)
.Case("debug_info", &InfoSection.Data) .Case("debug_info", &InfoSection.Data)
.Case("debug_abbrev", &AbbrevSection) .Case("debug_abbrev", &AbbrevSection)
@ -656,8 +654,8 @@ DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
.Case("debug_addr", &AddrSection) .Case("debug_addr", &AddrSection)
// Any more debug info sections go here. // Any more debug info sections go here.
.Default(0); .Default(0);
if (Section) { if (SectionData) {
*Section = data; *SectionData = data;
if (name == "debug_ranges") { if (name == "debug_ranges") {
// FIXME: Use the other dwo range section when we emit it. // FIXME: Use the other dwo range section when we emit it.
RangeDWOSection = data; RangeDWOSection = data;
@ -665,12 +663,12 @@ DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
} else if (name == "debug_types") { } else if (name == "debug_types") {
// Find debug_types data by section rather than name as there are // Find debug_types data by section rather than name as there are
// multiple, comdat grouped, debug_types sections. // multiple, comdat grouped, debug_types sections.
TypesSections[*i].Data = data; TypesSections[Section].Data = data;
} else if (name == "debug_types.dwo") { } else if (name == "debug_types.dwo") {
TypesDWOSections[*i].Data = data; TypesDWOSections[Section].Data = data;
} }
section_iterator RelocatedSection = i->getRelocatedSection(); section_iterator RelocatedSection = Section.getRelocatedSection();
if (RelocatedSection == Obj->section_end()) if (RelocatedSection == Obj->section_end())
continue; continue;
@ -698,11 +696,11 @@ DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
continue; continue;
} }
if (i->relocation_begin() != i->relocation_end()) { if (Section.relocation_begin() != Section.relocation_end()) {
uint64_t SectionSize; uint64_t SectionSize;
RelocatedSection->getSize(SectionSize); RelocatedSection->getSize(SectionSize);
for (object::relocation_iterator reloc_i = i->relocation_begin(), for (object::relocation_iterator reloc_i = Section.relocation_begin(),
reloc_e = i->relocation_end(); reloc_e = Section.relocation_end();
reloc_i != reloc_e; ++reloc_i) { reloc_i != reloc_e; ++reloc_i) {
uint64_t Address; uint64_t Address;
reloc_i->getOffset(Address); reloc_i->getOffset(Address);

View File

@ -87,20 +87,24 @@ MCModule *MCObjectDisassembler::buildModule(bool withCFG) {
} }
void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) { void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) {
for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); for (const SectionRef &Section : Obj.sections()) {
SI != SE; ++SI) { bool isText;
bool isText; SI->isText(isText); Section.isText(isText);
bool isData; SI->isData(isData); bool isData;
Section.isData(isData);
if (!isData && !isText) if (!isData && !isText)
continue; continue;
uint64_t StartAddr; SI->getAddress(StartAddr); uint64_t StartAddr;
uint64_t SecSize; SI->getSize(SecSize); Section.getAddress(StartAddr);
uint64_t SecSize;
Section.getSize(SecSize);
if (StartAddr == UnknownAddressOrSize || SecSize == UnknownAddressOrSize) if (StartAddr == UnknownAddressOrSize || SecSize == UnknownAddressOrSize)
continue; continue;
StartAddr = getEffectiveLoadAddr(StartAddr); StartAddr = getEffectiveLoadAddr(StartAddr);
StringRef Contents; SI->getContents(Contents); StringRef Contents;
Section.getContents(Contents);
StringRefMemoryObject memoryObject(Contents, StartAddr); StringRefMemoryObject memoryObject(Contents, StartAddr);
// We don't care about things like non-file-backed sections yet. // We don't care about things like non-file-backed sections yet.
@ -108,7 +112,8 @@ void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) {
continue; continue;
uint64_t EndAddr = StartAddr + SecSize - 1; uint64_t EndAddr = StartAddr + SecSize - 1;
StringRef SecName; SI->getName(SecName); StringRef SecName;
Section.getName(SecName);
if (isText) { if (isText) {
MCTextAtom *Text = 0; MCTextAtom *Text = 0;
@ -495,17 +500,16 @@ MCMachOObjectDisassembler::MCMachOObjectDisassembler(
: MCObjectDisassembler(MOOF, Dis, MIA), MOOF(MOOF), : MCObjectDisassembler(MOOF, Dis, MIA), MOOF(MOOF),
VMAddrSlide(VMAddrSlide), HeaderLoadAddress(HeaderLoadAddress) { VMAddrSlide(VMAddrSlide), HeaderLoadAddress(HeaderLoadAddress) {
for (section_iterator SI = MOOF.section_begin(), SE = MOOF.section_end(); for (const SectionRef &Section : MOOF.sections()) {
SI != SE; ++SI) {
StringRef Name; StringRef Name;
SI->getName(Name); Section.getName(Name);
// FIXME: We should use the S_ section type instead of the name. // FIXME: We should use the S_ section type instead of the name.
if (Name == "__mod_init_func") { if (Name == "__mod_init_func") {
DEBUG(dbgs() << "Found __mod_init_func section!\n"); DEBUG(dbgs() << "Found __mod_init_func section!\n");
SI->getContents(ModInitContents); Section.getContents(ModInitContents);
} else if (Name == "__mod_exit_func") { } else if (Name == "__mod_exit_func") {
DEBUG(dbgs() << "Found __mod_exit_func section!\n"); DEBUG(dbgs() << "Found __mod_exit_func section!\n");
SI->getContents(ModExitContents); Section.getContents(ModExitContents);
} }
} }
} }

View File

@ -51,11 +51,11 @@ MCMachObjectSymbolizer::MCMachObjectSymbolizer(
: MCObjectSymbolizer(Ctx, RelInfo, MOOF), MOOF(MOOF), StubsStart(0), : MCObjectSymbolizer(Ctx, RelInfo, MOOF), MOOF(MOOF), StubsStart(0),
StubsCount(0), StubSize(0), StubsIndSymIndex(0) { StubsCount(0), StubSize(0), StubsIndSymIndex(0) {
for (section_iterator SI = MOOF->section_begin(), SE = MOOF->section_end(); for (const SectionRef &Section : MOOF->sections()) {
SI != SE; ++SI) { StringRef Name;
StringRef Name; SI->getName(Name); Section.getName(Name);
if (Name == "__stubs") { if (Name == "__stubs") {
SectionRef StubsSec = *SI; SectionRef StubsSec = Section;
if (MOOF->is64Bit()) { if (MOOF->is64Bit()) {
MachO::section_64 S = MOOF->getSection64(StubsSec.getRawDataRefImpl()); MachO::section_64 S = MOOF->getSection64(StubsSec.getRawDataRefImpl());
StubsIndSymIndex = S.reserved1; StubsIndSymIndex = S.reserved1;
@ -230,40 +230,41 @@ const RelocationRef *MCObjectSymbolizer::findRelocationAt(uint64_t Addr) {
} }
void MCObjectSymbolizer::buildSectionList() { void MCObjectSymbolizer::buildSectionList() {
for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end(); for (const SectionRef &Section : Obj->sections()) {
SI != SE; ++SI) { bool RequiredForExec;
bool RequiredForExec; SI->isRequiredForExecution(RequiredForExec); Section.isRequiredForExecution(RequiredForExec);
if (RequiredForExec == false) if (RequiredForExec == false)
continue; continue;
uint64_t SAddr; SI->getAddress(SAddr); uint64_t SAddr;
uint64_t SSize; SI->getSize(SSize); Section.getAddress(SAddr);
SortedSectionList::iterator It = std::lower_bound(SortedSections.begin(), uint64_t SSize;
SortedSections.end(), Section.getSize(SSize);
SAddr, SortedSectionList::iterator It =
std::lower_bound(SortedSections.begin(), SortedSections.end(), SAddr,
SectionStartsBefore); SectionStartsBefore);
if (It != SortedSections.end()) { if (It != SortedSections.end()) {
uint64_t FoundSAddr; It->getAddress(FoundSAddr); uint64_t FoundSAddr; It->getAddress(FoundSAddr);
if (FoundSAddr < SAddr + SSize) if (FoundSAddr < SAddr + SSize)
llvm_unreachable("Inserting overlapping sections"); llvm_unreachable("Inserting overlapping sections");
} }
SortedSections.insert(It, *SI); SortedSections.insert(It, Section);
} }
} }
void MCObjectSymbolizer::buildRelocationByAddrMap() { void MCObjectSymbolizer::buildRelocationByAddrMap() {
for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end(); for (const SectionRef &Section : Obj->sections()) {
SI != SE; ++SI) { section_iterator RelSecI = Section.getRelocatedSection();
section_iterator RelSecI = SI->getRelocatedSection();
if (RelSecI == Obj->section_end()) if (RelSecI == Obj->section_end())
continue; continue;
uint64_t StartAddr; RelSecI->getAddress(StartAddr); uint64_t StartAddr; RelSecI->getAddress(StartAddr);
uint64_t Size; RelSecI->getSize(Size); uint64_t Size; RelSecI->getSize(Size);
bool RequiredForExec; RelSecI->isRequiredForExecution(RequiredForExec); bool RequiredForExec;
RelSecI->isRequiredForExecution(RequiredForExec);
if (RequiredForExec == false || Size == 0) if (RequiredForExec == false || Size == 0)
continue; continue;
for (relocation_iterator RI = SI->relocation_begin(), for (relocation_iterator RI = Section.relocation_begin(),
RE = SI->relocation_end(); RE = Section.relocation_end();
RI != RE; ++RI) { RI != RE; ++RI) {
// FIXME: libObject is inconsistent regarding error handling. The // FIXME: libObject is inconsistent regarding error handling. The
// overwhelming majority of methods always return object_error::success, // overwhelming majority of methods always return object_error::success,

View File

@ -313,16 +313,16 @@ static void printRelocationTargetName(const MachOObjectFile *O,
// If we couldn't find a symbol that this relocation refers to, try // If we couldn't find a symbol that this relocation refers to, try
// to find a section beginning instead. // to find a section beginning instead.
for (section_iterator SI = O->section_begin(), SE = O->section_end(); for (const SectionRef &Section : O->sections()) {
SI != SE; ++SI) {
error_code ec; error_code ec;
uint64_t Addr; uint64_t Addr;
StringRef Name; StringRef Name;
if ((ec = SI->getAddress(Addr))) if ((ec = Section.getAddress(Addr)))
report_fatal_error(ec.message()); report_fatal_error(ec.message());
if (Addr != Val) continue; if (Addr != Val)
if ((ec = SI->getName(Name))) continue;
if ((ec = Section.getName(Name)))
report_fatal_error(ec.message()); report_fatal_error(ec.message());
fmt << Name; fmt << Name;
return; return;