From 6f07b35b8f7cd4e7ad85d25a968db1daa86535ff Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Fri, 14 Mar 2014 14:22:49 +0000 Subject: [PATCH] [C++11] Introduce SectionRef::relocations() to use range-based loops Reviewers: rafael Reviewed By: rafael CC: llvm-commits Differential Revision: http://llvm-reviews.chandlerc.com/D3077 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203927 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ObjectFile.h | 4 ++ lib/DebugInfo/DWARFContext.cpp | 14 +++---- .../RuntimeDyld/RuntimeDyld.cpp | 13 +++--- lib/MC/MCObjectSymbolizer.cpp | 12 +++--- tools/llvm-objdump/COFFDump.cpp | 6 +-- tools/llvm-objdump/MachODump.cpp | 10 ++--- tools/llvm-objdump/llvm-objdump.cpp | 31 +++++++------- tools/llvm-readobj/COFFDumper.cpp | 42 +++++++++---------- tools/llvm-readobj/MachODumper.cpp | 35 +++++++--------- 9 files changed, 77 insertions(+), 90 deletions(-) diff --git a/include/llvm/Object/ObjectFile.h b/include/llvm/Object/ObjectFile.h index a3301dbe435..b01eb000238 100644 --- a/include/llvm/Object/ObjectFile.h +++ b/include/llvm/Object/ObjectFile.h @@ -112,6 +112,10 @@ public: relocation_iterator relocation_begin() const; relocation_iterator relocation_end() const; + typedef iterator_range relocation_iterator_range; + relocation_iterator_range relocations() const { + return relocation_iterator_range(relocation_begin(), relocation_end()); + } section_iterator getRelocatedSection() const; DataRefImpl getRawDataRefImpl() const; diff --git a/lib/DebugInfo/DWARFContext.cpp b/lib/DebugInfo/DWARFContext.cpp index 8a245a0301d..0afe6ef6e13 100644 --- a/lib/DebugInfo/DWARFContext.cpp +++ b/lib/DebugInfo/DWARFContext.cpp @@ -699,26 +699,24 @@ DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) if (Section.relocation_begin() != Section.relocation_end()) { uint64_t SectionSize; RelocatedSection->getSize(SectionSize); - for (object::relocation_iterator reloc_i = Section.relocation_begin(), - reloc_e = Section.relocation_end(); - reloc_i != reloc_e; ++reloc_i) { + for (const RelocationRef &Reloc : Section.relocations()) { uint64_t Address; - reloc_i->getOffset(Address); + Reloc.getOffset(Address); uint64_t Type; - reloc_i->getType(Type); + Reloc.getType(Type); uint64_t SymAddr = 0; // ELF relocations may need the symbol address if (Obj->isELF()) { - object::symbol_iterator Sym = reloc_i->getSymbol(); + object::symbol_iterator Sym = Reloc.getSymbol(); Sym->getAddress(SymAddr); } object::RelocVisitor V(Obj->getFileFormatName()); // The section address is always 0 for debug sections. - object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr)); + object::RelocToApply R(V.visit(Type, Reloc, 0, SymAddr)); if (V.error()) { SmallString<32> Name; - error_code ec(reloc_i->getTypeName(Name)); + error_code ec(Reloc.getTypeName(Name)); if (ec) { errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n"; } diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index 723a790b87e..5d6a2c0f123 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -166,9 +166,7 @@ ObjectImage* RuntimeDyldImpl::loadObject(ObjectImage *InputObject) { StubMap Stubs; section_iterator RelocatedSection = SI->getRelocatedSection(); - for (relocation_iterator I = SI->relocation_begin(), - E = SI->relocation_end(); - I != E; ++I) { + for (const RelocationRef &Reloc : SI->relocations()) { // If it's the first relocation in this section, find its SectionID if (IsFirstRelocation) { bool IsCode = false; @@ -179,7 +177,7 @@ ObjectImage* RuntimeDyldImpl::loadObject(ObjectImage *InputObject) { IsFirstRelocation = false; } - processRelocationRef(SectionID, *I, *Obj, LocalSections, LocalSymbols, + processRelocationRef(SectionID, Reloc, *Obj, LocalSections, LocalSymbols, Stubs); } } @@ -306,13 +304,12 @@ unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj, if (!(RelSecI == Section)) continue; - for (relocation_iterator I = SI->relocation_begin(), - E = SI->relocation_end(); - I != E; ++I) { + for (const RelocationRef &Reloc : SI->relocations()) { + (void)Reloc; StubBufSize += StubSize; } } - + // Get section data size and alignment uint64_t Alignment64; uint64_t DataSize; diff --git a/lib/MC/MCObjectSymbolizer.cpp b/lib/MC/MCObjectSymbolizer.cpp index 4548a7d4251..ee2d9553ecb 100644 --- a/lib/MC/MCObjectSymbolizer.cpp +++ b/lib/MC/MCObjectSymbolizer.cpp @@ -263,9 +263,7 @@ void MCObjectSymbolizer::buildRelocationByAddrMap() { RelSecI->isRequiredForExecution(RequiredForExec); if (RequiredForExec == false || Size == 0) continue; - for (relocation_iterator RI = Section.relocation_begin(), - RE = Section.relocation_end(); - RI != RE; ++RI) { + for (const RelocationRef &Reloc : Section.relocations()) { // FIXME: libObject is inconsistent regarding error handling. The // overwhelming majority of methods always return object_error::success, // and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset @@ -277,18 +275,18 @@ void MCObjectSymbolizer::buildRelocationByAddrMap() { if (ELFObj == 0) break; if (ELFObj->getELFFile()->getHeader()->e_type == ELF::ET_REL) { - RI->getOffset(Offset); + Reloc.getOffset(Offset); Offset += StartAddr; } else { - RI->getAddress(Offset); + Reloc.getAddress(Offset); } } else { - RI->getOffset(Offset); + Reloc.getOffset(Offset); Offset += StartAddr; } // At a specific address, only keep the first relocation. if (AddrToReloc.find(Offset) == AddrToReloc.end()) - AddrToReloc[Offset] = *RI; + AddrToReloc[Offset] = Reloc; } } } diff --git a/tools/llvm-objdump/COFFDump.cpp b/tools/llvm-objdump/COFFDump.cpp index 3df443149a6..e2d65a48177 100644 --- a/tools/llvm-objdump/COFFDump.cpp +++ b/tools/llvm-objdump/COFFDump.cpp @@ -390,10 +390,8 @@ static bool getPDataSection(const COFFObjectFile *Obj, continue; const coff_section *Pdata = Obj->getCOFFSection(SI); - for (relocation_iterator RI = SI->relocation_begin(), - RE = SI->relocation_end(); - RI != RE; ++RI) - Rels.push_back(*RI); + for (const RelocationRef &Reloc : SI->relocations()) + Rels.push_back(Reloc); // Sort relocations by address. std::sort(Rels.begin(), Rels.end(), RelocAddressLess); diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index 087fb40628a..e5247a64b08 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -325,16 +325,14 @@ static void DisassembleInputMachO2(StringRef Filename, bool symbolTableWorked = false; // Parse relocations. - std::vector > Relocs; - for (relocation_iterator RI = Sections[SectIdx].relocation_begin(), - RE = Sections[SectIdx].relocation_end(); - RI != RE; ++RI) { + std::vector> Relocs; + for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) { uint64_t RelocOffset, SectionAddress; - RI->getOffset(RelocOffset); + Reloc.getOffset(RelocOffset); Sections[SectIdx].getAddress(SectionAddress); RelocOffset -= SectionAddress; - symbol_iterator RelocSym = RI->getSymbol(); + symbol_iterator RelocSym = Reloc.getSymbol(); Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); } diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index 7fa01f7633e..9f6ea35eaf5 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -435,14 +435,10 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { // Make a list of all the relocations for this section. std::vector Rels; if (InlineRelocs) { - SmallVectorImpl *RelocSecs = &SectionRelocMap[Section]; - for (SmallVectorImpl::iterator RelocSec = RelocSecs->begin(), - E = RelocSecs->end(); - RelocSec != E; ++RelocSec) { - for (relocation_iterator RI = RelocSec->relocation_begin(), - RE = RelocSec->relocation_end(); - RI != RE; ++RI) - Rels.push_back(*RI); + for (const SectionRef &RelocSec : SectionRelocMap[Section]) { + for (const RelocationRef &Reloc : RelocSec.relocations()) { + Rels.push_back(Reloc); + } } } @@ -560,18 +556,21 @@ static void PrintRelocations(const ObjectFile *Obj) { if (error(Section.getName(secname))) continue; outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n"; - for (relocation_iterator ri = Section.relocation_begin(), - re = Section.relocation_end(); - ri != re; ++ri) { + for (const RelocationRef &Reloc : Section.relocations()) { bool hidden; uint64_t address; SmallString<32> relocname; SmallString<32> valuestr; - if (error(ri->getHidden(hidden))) continue; - if (hidden) continue; - if (error(ri->getTypeName(relocname))) continue; - if (error(ri->getOffset(address))) continue; - if (error(ri->getValueString(valuestr))) continue; + if (error(Reloc.getHidden(hidden))) + continue; + if (hidden) + continue; + if (error(Reloc.getTypeName(relocname))) + continue; + if (error(Reloc.getOffset(address))) + continue; + if (error(Reloc.getValueString(valuestr))) + continue; outs() << address << " " << relocname << " " << valuestr << "\n"; } outs() << "\n"; diff --git a/tools/llvm-readobj/COFFDumper.cpp b/tools/llvm-readobj/COFFDumper.cpp index 613cc5b7c97..dfe89d69ec5 100644 --- a/tools/llvm-readobj/COFFDumper.cpp +++ b/tools/llvm-readobj/COFFDumper.cpp @@ -56,7 +56,7 @@ public: private: void printSymbol(symbol_iterator SymI); - void printRelocation(section_iterator SecI, relocation_iterator RelI); + void printRelocation(section_iterator SecI, const RelocationRef &Reloc); void printDataDirectory(uint32_t Index, const std::string &FieldName); void printX64UnwindInfo(); @@ -540,15 +540,12 @@ error_code COFFDumper::getSection( } void COFFDumper::cacheRelocations() { - for (section_iterator SecI = Obj->section_begin(), - SecE = Obj->section_end(); + for (section_iterator SecI = Obj->section_begin(), SecE = Obj->section_end(); SecI != SecE; ++SecI) { const coff_section *Section = Obj->getCOFFSection(SecI); - for (relocation_iterator RelI = SecI->relocation_begin(), - RelE = SecI->relocation_end(); - RelI != RelE; ++RelI) - RelocMap[Section].push_back(*RelI); + for (const RelocationRef &Reloc : SecI->relocations()) + RelocMap[Section].push_back(Reloc); // Sort relocations by address. std::sort(RelocMap[Section].begin(), RelocMap[Section].end(), @@ -844,10 +841,8 @@ void COFFDumper::printSections() { if (opts::SectionRelocations) { ListScope D(W, "Relocations"); - for (relocation_iterator RelI = SecI->relocation_begin(), - RelE = SecI->relocation_end(); - RelI != RelE; ++RelI) - printRelocation(SecI, RelI); + for (const RelocationRef &Reloc : SecI->relocations()) + printRelocation(SecI, Reloc); } if (opts::SectionSymbols) { @@ -888,16 +883,14 @@ void COFFDumper::printRelocations() { continue; bool PrintedGroup = false; - for (relocation_iterator RelI = SecI->relocation_begin(), - RelE = SecI->relocation_end(); - RelI != RelE; ++RelI) { + for (const RelocationRef &Reloc : SecI->relocations()) { if (!PrintedGroup) { W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; W.indent(); PrintedGroup = true; } - printRelocation(SecI, RelI); + printRelocation(SecI, Reloc); } if (PrintedGroup) { @@ -908,18 +901,23 @@ void COFFDumper::printRelocations() { } void COFFDumper::printRelocation(section_iterator SecI, - relocation_iterator RelI) { + const RelocationRef &Reloc) { uint64_t Offset; uint64_t RelocType; SmallString<32> RelocName; StringRef SymbolName; StringRef Contents; - if (error(RelI->getOffset(Offset))) return; - if (error(RelI->getType(RelocType))) return; - if (error(RelI->getTypeName(RelocName))) return; - symbol_iterator Symbol = RelI->getSymbol(); - if (error(Symbol->getName(SymbolName))) return; - if (error(SecI->getContents(Contents))) return; + if (error(Reloc.getOffset(Offset))) + return; + if (error(Reloc.getType(RelocType))) + return; + if (error(Reloc.getTypeName(RelocName))) + return; + symbol_iterator Symbol = Reloc.getSymbol(); + if (error(Symbol->getName(SymbolName))) + return; + if (error(SecI->getContents(Contents))) + return; if (opts::ExpandRelocs) { DictScope Group(W, "Relocation"); diff --git a/tools/llvm-readobj/MachODumper.cpp b/tools/llvm-readobj/MachODumper.cpp index 79a04c81a6f..a5f29b9c12c 100644 --- a/tools/llvm-readobj/MachODumper.cpp +++ b/tools/llvm-readobj/MachODumper.cpp @@ -40,9 +40,9 @@ public: private: void printSymbol(symbol_iterator SymI); - void printRelocation(relocation_iterator RelI); + void printRelocation(const RelocationRef &Reloc); - void printRelocation(const MachOObjectFile *Obj, relocation_iterator RelI); + void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc); void printSections(const MachOObjectFile *Obj); @@ -249,10 +249,8 @@ void MachODumper::printSections(const MachOObjectFile *Obj) { if (opts::SectionRelocations) { ListScope D(W, "Relocations"); - for (relocation_iterator RelI = Section.relocation_begin(), - RelE = Section.relocation_end(); - RelI != RelE; ++RelI) - printRelocation(RelI); + for (const RelocationRef &Reloc : Section.relocations()) + printRelocation(Reloc); } if (opts::SectionSymbols) { @@ -287,16 +285,14 @@ void MachODumper::printRelocations() { continue; bool PrintedGroup = false; - for (relocation_iterator RelI = Section.relocation_begin(), - RelE = Section.relocation_end(); - RelI != RelE; ++RelI) { + for (const RelocationRef &Reloc : Section.relocations()) { if (!PrintedGroup) { W.startLine() << "Section " << Name << " {\n"; W.indent(); PrintedGroup = true; } - printRelocation(RelI); + printRelocation(Reloc); } if (PrintedGroup) { @@ -306,23 +302,24 @@ void MachODumper::printRelocations() { } } -void MachODumper::printRelocation(relocation_iterator RelI) { - return printRelocation(Obj, RelI); +void MachODumper::printRelocation(const RelocationRef &Reloc) { + return printRelocation(Obj, Reloc); } void MachODumper::printRelocation(const MachOObjectFile *Obj, - relocation_iterator RelI) { + const RelocationRef &Reloc) { uint64_t Offset; SmallString<32> RelocName; StringRef SymbolName; - if (error(RelI->getOffset(Offset))) return; - if (error(RelI->getTypeName(RelocName))) return; - symbol_iterator Symbol = RelI->getSymbol(); - if (Symbol != Obj->symbol_end() && - error(Symbol->getName(SymbolName))) + if (error(Reloc.getOffset(Offset))) + return; + if (error(Reloc.getTypeName(RelocName))) + return; + symbol_iterator Symbol = Reloc.getSymbol(); + if (Symbol != Obj->symbol_end() && error(Symbol->getName(SymbolName))) return; - DataRefImpl DR = RelI->getRawDataRefImpl(); + DataRefImpl DR = Reloc.getRawDataRefImpl(); MachO::any_relocation_info RE = Obj->getRelocation(DR); bool IsScattered = Obj->isRelocationScattered(RE); -- 2.34.1