[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

llvm-svn: 203927
This commit is contained in:
Alexey Samsonov 2014-03-14 14:22:49 +00:00
parent 6862794206
commit aa4d29571c
9 changed files with 77 additions and 90 deletions

View File

@ -112,6 +112,10 @@ public:
relocation_iterator relocation_begin() const; relocation_iterator relocation_begin() const;
relocation_iterator relocation_end() const; relocation_iterator relocation_end() const;
typedef iterator_range<relocation_iterator> relocation_iterator_range;
relocation_iterator_range relocations() const {
return relocation_iterator_range(relocation_begin(), relocation_end());
}
section_iterator getRelocatedSection() const; section_iterator getRelocatedSection() const;
DataRefImpl getRawDataRefImpl() const; DataRefImpl getRawDataRefImpl() const;

View File

@ -699,26 +699,24 @@ DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj)
if (Section.relocation_begin() != Section.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 = Section.relocation_begin(), for (const RelocationRef &Reloc : Section.relocations()) {
reloc_e = Section.relocation_end();
reloc_i != reloc_e; ++reloc_i) {
uint64_t Address; uint64_t Address;
reloc_i->getOffset(Address); Reloc.getOffset(Address);
uint64_t Type; uint64_t Type;
reloc_i->getType(Type); Reloc.getType(Type);
uint64_t SymAddr = 0; uint64_t SymAddr = 0;
// ELF relocations may need the symbol address // ELF relocations may need the symbol address
if (Obj->isELF()) { if (Obj->isELF()) {
object::symbol_iterator Sym = reloc_i->getSymbol(); object::symbol_iterator Sym = Reloc.getSymbol();
Sym->getAddress(SymAddr); Sym->getAddress(SymAddr);
} }
object::RelocVisitor V(Obj->getFileFormatName()); object::RelocVisitor V(Obj->getFileFormatName());
// The section address is always 0 for debug sections. // 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()) { if (V.error()) {
SmallString<32> Name; SmallString<32> Name;
error_code ec(reloc_i->getTypeName(Name)); error_code ec(Reloc.getTypeName(Name));
if (ec) { if (ec) {
errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n"; errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
} }

View File

@ -166,9 +166,7 @@ ObjectImage* RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
StubMap Stubs; StubMap Stubs;
section_iterator RelocatedSection = SI->getRelocatedSection(); section_iterator RelocatedSection = SI->getRelocatedSection();
for (relocation_iterator I = SI->relocation_begin(), for (const RelocationRef &Reloc : SI->relocations()) {
E = SI->relocation_end();
I != E; ++I) {
// If it's the first relocation in this section, find its SectionID // If it's the first relocation in this section, find its SectionID
if (IsFirstRelocation) { if (IsFirstRelocation) {
bool IsCode = false; bool IsCode = false;
@ -179,7 +177,7 @@ ObjectImage* RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
IsFirstRelocation = false; IsFirstRelocation = false;
} }
processRelocationRef(SectionID, *I, *Obj, LocalSections, LocalSymbols, processRelocationRef(SectionID, Reloc, *Obj, LocalSections, LocalSymbols,
Stubs); Stubs);
} }
} }
@ -306,9 +304,8 @@ unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj,
if (!(RelSecI == Section)) if (!(RelSecI == Section))
continue; continue;
for (relocation_iterator I = SI->relocation_begin(), for (const RelocationRef &Reloc : SI->relocations()) {
E = SI->relocation_end(); (void)Reloc;
I != E; ++I) {
StubBufSize += StubSize; StubBufSize += StubSize;
} }
} }

View File

@ -263,9 +263,7 @@ void MCObjectSymbolizer::buildRelocationByAddrMap() {
RelSecI->isRequiredForExecution(RequiredForExec); RelSecI->isRequiredForExecution(RequiredForExec);
if (RequiredForExec == false || Size == 0) if (RequiredForExec == false || Size == 0)
continue; continue;
for (relocation_iterator RI = Section.relocation_begin(), for (const RelocationRef &Reloc : Section.relocations()) {
RE = Section.relocation_end();
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,
// and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset // and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset
@ -277,18 +275,18 @@ void MCObjectSymbolizer::buildRelocationByAddrMap() {
if (ELFObj == 0) if (ELFObj == 0)
break; break;
if (ELFObj->getELFFile()->getHeader()->e_type == ELF::ET_REL) { if (ELFObj->getELFFile()->getHeader()->e_type == ELF::ET_REL) {
RI->getOffset(Offset); Reloc.getOffset(Offset);
Offset += StartAddr; Offset += StartAddr;
} else { } else {
RI->getAddress(Offset); Reloc.getAddress(Offset);
} }
} else { } else {
RI->getOffset(Offset); Reloc.getOffset(Offset);
Offset += StartAddr; Offset += StartAddr;
} }
// At a specific address, only keep the first relocation. // At a specific address, only keep the first relocation.
if (AddrToReloc.find(Offset) == AddrToReloc.end()) if (AddrToReloc.find(Offset) == AddrToReloc.end())
AddrToReloc[Offset] = *RI; AddrToReloc[Offset] = Reloc;
} }
} }
} }

View File

@ -390,10 +390,8 @@ static bool getPDataSection(const COFFObjectFile *Obj,
continue; continue;
const coff_section *Pdata = Obj->getCOFFSection(SI); const coff_section *Pdata = Obj->getCOFFSection(SI);
for (relocation_iterator RI = SI->relocation_begin(), for (const RelocationRef &Reloc : SI->relocations())
RE = SI->relocation_end(); Rels.push_back(Reloc);
RI != RE; ++RI)
Rels.push_back(*RI);
// Sort relocations by address. // Sort relocations by address.
std::sort(Rels.begin(), Rels.end(), RelocAddressLess); std::sort(Rels.begin(), Rels.end(), RelocAddressLess);

View File

@ -326,15 +326,13 @@ static void DisassembleInputMachO2(StringRef Filename,
// Parse relocations. // Parse relocations.
std::vector<std::pair<uint64_t, SymbolRef>> Relocs; std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
for (relocation_iterator RI = Sections[SectIdx].relocation_begin(), for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
RE = Sections[SectIdx].relocation_end();
RI != RE; ++RI) {
uint64_t RelocOffset, SectionAddress; uint64_t RelocOffset, SectionAddress;
RI->getOffset(RelocOffset); Reloc.getOffset(RelocOffset);
Sections[SectIdx].getAddress(SectionAddress); Sections[SectIdx].getAddress(SectionAddress);
RelocOffset -= SectionAddress; RelocOffset -= SectionAddress;
symbol_iterator RelocSym = RI->getSymbol(); symbol_iterator RelocSym = Reloc.getSymbol();
Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
} }

View File

@ -435,14 +435,10 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
// Make a list of all the relocations for this section. // Make a list of all the relocations for this section.
std::vector<RelocationRef> Rels; std::vector<RelocationRef> Rels;
if (InlineRelocs) { if (InlineRelocs) {
SmallVectorImpl<SectionRef> *RelocSecs = &SectionRelocMap[Section]; for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
for (SmallVectorImpl<SectionRef>::iterator RelocSec = RelocSecs->begin(), for (const RelocationRef &Reloc : RelocSec.relocations()) {
E = RelocSecs->end(); Rels.push_back(Reloc);
RelocSec != E; ++RelocSec) { }
for (relocation_iterator RI = RelocSec->relocation_begin(),
RE = RelocSec->relocation_end();
RI != RE; ++RI)
Rels.push_back(*RI);
} }
} }
@ -560,18 +556,21 @@ static void PrintRelocations(const ObjectFile *Obj) {
if (error(Section.getName(secname))) if (error(Section.getName(secname)))
continue; continue;
outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n"; outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
for (relocation_iterator ri = Section.relocation_begin(), for (const RelocationRef &Reloc : Section.relocations()) {
re = Section.relocation_end();
ri != re; ++ri) {
bool hidden; bool hidden;
uint64_t address; uint64_t address;
SmallString<32> relocname; SmallString<32> relocname;
SmallString<32> valuestr; SmallString<32> valuestr;
if (error(ri->getHidden(hidden))) continue; if (error(Reloc.getHidden(hidden)))
if (hidden) continue; continue;
if (error(ri->getTypeName(relocname))) continue; if (hidden)
if (error(ri->getOffset(address))) continue; continue;
if (error(ri->getValueString(valuestr))) 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() << address << " " << relocname << " " << valuestr << "\n";
} }
outs() << "\n"; outs() << "\n";

View File

@ -56,7 +56,7 @@ public:
private: private:
void printSymbol(symbol_iterator SymI); 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 printDataDirectory(uint32_t Index, const std::string &FieldName);
void printX64UnwindInfo(); void printX64UnwindInfo();
@ -540,15 +540,12 @@ error_code COFFDumper::getSection(
} }
void COFFDumper::cacheRelocations() { void COFFDumper::cacheRelocations() {
for (section_iterator SecI = Obj->section_begin(), for (section_iterator SecI = Obj->section_begin(), SecE = Obj->section_end();
SecE = Obj->section_end();
SecI != SecE; ++SecI) { SecI != SecE; ++SecI) {
const coff_section *Section = Obj->getCOFFSection(SecI); const coff_section *Section = Obj->getCOFFSection(SecI);
for (relocation_iterator RelI = SecI->relocation_begin(), for (const RelocationRef &Reloc : SecI->relocations())
RelE = SecI->relocation_end(); RelocMap[Section].push_back(Reloc);
RelI != RelE; ++RelI)
RelocMap[Section].push_back(*RelI);
// Sort relocations by address. // Sort relocations by address.
std::sort(RelocMap[Section].begin(), RelocMap[Section].end(), std::sort(RelocMap[Section].begin(), RelocMap[Section].end(),
@ -844,10 +841,8 @@ void COFFDumper::printSections() {
if (opts::SectionRelocations) { if (opts::SectionRelocations) {
ListScope D(W, "Relocations"); ListScope D(W, "Relocations");
for (relocation_iterator RelI = SecI->relocation_begin(), for (const RelocationRef &Reloc : SecI->relocations())
RelE = SecI->relocation_end(); printRelocation(SecI, Reloc);
RelI != RelE; ++RelI)
printRelocation(SecI, RelI);
} }
if (opts::SectionSymbols) { if (opts::SectionSymbols) {
@ -888,16 +883,14 @@ void COFFDumper::printRelocations() {
continue; continue;
bool PrintedGroup = false; bool PrintedGroup = false;
for (relocation_iterator RelI = SecI->relocation_begin(), for (const RelocationRef &Reloc : SecI->relocations()) {
RelE = SecI->relocation_end();
RelI != RelE; ++RelI) {
if (!PrintedGroup) { if (!PrintedGroup) {
W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
W.indent(); W.indent();
PrintedGroup = true; PrintedGroup = true;
} }
printRelocation(SecI, RelI); printRelocation(SecI, Reloc);
} }
if (PrintedGroup) { if (PrintedGroup) {
@ -908,18 +901,23 @@ void COFFDumper::printRelocations() {
} }
void COFFDumper::printRelocation(section_iterator SecI, void COFFDumper::printRelocation(section_iterator SecI,
relocation_iterator RelI) { const RelocationRef &Reloc) {
uint64_t Offset; uint64_t Offset;
uint64_t RelocType; uint64_t RelocType;
SmallString<32> RelocName; SmallString<32> RelocName;
StringRef SymbolName; StringRef SymbolName;
StringRef Contents; StringRef Contents;
if (error(RelI->getOffset(Offset))) return; if (error(Reloc.getOffset(Offset)))
if (error(RelI->getType(RelocType))) return; return;
if (error(RelI->getTypeName(RelocName))) return; if (error(Reloc.getType(RelocType)))
symbol_iterator Symbol = RelI->getSymbol(); return;
if (error(Symbol->getName(SymbolName))) return; if (error(Reloc.getTypeName(RelocName)))
if (error(SecI->getContents(Contents))) return; return;
symbol_iterator Symbol = Reloc.getSymbol();
if (error(Symbol->getName(SymbolName)))
return;
if (error(SecI->getContents(Contents)))
return;
if (opts::ExpandRelocs) { if (opts::ExpandRelocs) {
DictScope Group(W, "Relocation"); DictScope Group(W, "Relocation");

View File

@ -40,9 +40,9 @@ public:
private: private:
void printSymbol(symbol_iterator SymI); 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); void printSections(const MachOObjectFile *Obj);
@ -249,10 +249,8 @@ void MachODumper::printSections(const MachOObjectFile *Obj) {
if (opts::SectionRelocations) { if (opts::SectionRelocations) {
ListScope D(W, "Relocations"); ListScope D(W, "Relocations");
for (relocation_iterator RelI = Section.relocation_begin(), for (const RelocationRef &Reloc : Section.relocations())
RelE = Section.relocation_end(); printRelocation(Reloc);
RelI != RelE; ++RelI)
printRelocation(RelI);
} }
if (opts::SectionSymbols) { if (opts::SectionSymbols) {
@ -287,16 +285,14 @@ void MachODumper::printRelocations() {
continue; continue;
bool PrintedGroup = false; bool PrintedGroup = false;
for (relocation_iterator RelI = Section.relocation_begin(), for (const RelocationRef &Reloc : Section.relocations()) {
RelE = Section.relocation_end();
RelI != RelE; ++RelI) {
if (!PrintedGroup) { if (!PrintedGroup) {
W.startLine() << "Section " << Name << " {\n"; W.startLine() << "Section " << Name << " {\n";
W.indent(); W.indent();
PrintedGroup = true; PrintedGroup = true;
} }
printRelocation(RelI); printRelocation(Reloc);
} }
if (PrintedGroup) { if (PrintedGroup) {
@ -306,23 +302,24 @@ void MachODumper::printRelocations() {
} }
} }
void MachODumper::printRelocation(relocation_iterator RelI) { void MachODumper::printRelocation(const RelocationRef &Reloc) {
return printRelocation(Obj, RelI); return printRelocation(Obj, Reloc);
} }
void MachODumper::printRelocation(const MachOObjectFile *Obj, void MachODumper::printRelocation(const MachOObjectFile *Obj,
relocation_iterator RelI) { const RelocationRef &Reloc) {
uint64_t Offset; uint64_t Offset;
SmallString<32> RelocName; SmallString<32> RelocName;
StringRef SymbolName; StringRef SymbolName;
if (error(RelI->getOffset(Offset))) return; if (error(Reloc.getOffset(Offset)))
if (error(RelI->getTypeName(RelocName))) return; return;
symbol_iterator Symbol = RelI->getSymbol(); if (error(Reloc.getTypeName(RelocName)))
if (Symbol != Obj->symbol_end() && return;
error(Symbol->getName(SymbolName))) symbol_iterator Symbol = Reloc.getSymbol();
if (Symbol != Obj->symbol_end() && error(Symbol->getName(SymbolName)))
return; return;
DataRefImpl DR = RelI->getRawDataRefImpl(); DataRefImpl DR = Reloc.getRawDataRefImpl();
MachO::any_relocation_info RE = Obj->getRelocation(DR); MachO::any_relocation_info RE = Obj->getRelocation(DR);
bool IsScattered = Obj->isRelocationScattered(RE); bool IsScattered = Obj->isRelocationScattered(RE);