From 5b645797db05926bffdd6214e94a527267445cc9 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Tue, 18 Mar 2014 06:53:02 +0000 Subject: [PATCH] [C++11] Change the interface of getCOFF{Section,Relocation,Symbol} to make it work with range-based for loops. Reviewers: ruiu Reviewed By: ruiu CC: llvm-commits Differential Revision: http://llvm-reviews.chandlerc.com/D3097 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204120 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/COFF.h | 6 +-- lib/Object/COFFObjectFile.cpp | 25 ++++++----- tools/llvm-nm/llvm-nm.cpp | 4 +- tools/llvm-objdump/COFFDump.cpp | 11 +++-- tools/llvm-readobj/COFFDumper.cpp | 91 +++++++++++++++++---------------------- tools/obj2yaml/coff2yaml.cpp | 17 +++----- 6 files changed, 71 insertions(+), 83 deletions(-) diff --git a/include/llvm/Object/COFF.h b/include/llvm/Object/COFF.h index 8c292164e9a..569318fc75d 100644 --- a/include/llvm/Object/COFF.h +++ b/include/llvm/Object/COFF.h @@ -356,9 +356,9 @@ public: section_iterator section_begin() const override; section_iterator section_end() const override; - const coff_section *getCOFFSection(section_iterator &It) const; - const coff_symbol *getCOFFSymbol(symbol_iterator &It) const; - const coff_relocation *getCOFFRelocation(relocation_iterator &It) const; + const coff_section *getCOFFSection(const SectionRef &Section) const; + const coff_symbol *getCOFFSymbol(const SymbolRef &Symbol) const; + const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const; uint8_t getBytesInAddress() const override; StringRef getFileFormatName() const override; diff --git a/lib/Object/COFFObjectFile.cpp b/lib/Object/COFFObjectFile.cpp index 461dbac1168..7f66c6a29c8 100644 --- a/lib/Object/COFFObjectFile.cpp +++ b/lib/Object/COFFObjectFile.cpp @@ -431,9 +431,8 @@ error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const { // Returns the file offset for the given RVA. error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const { - for (section_iterator I = section_begin(), E = section_end(); I != E; - ++I) { - const coff_section *Section = getCOFFSection(I); + for (const SectionRef &S : sections()) { + const coff_section *Section = getCOFFSection(S); uint32_t SectionStart = Section->VirtualAddress; uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; if (SectionStart <= Addr && Addr < SectionEnd) { @@ -872,21 +871,25 @@ error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, return object_error::success; } -const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const { - return toSec(It->getRawDataRefImpl()); +const coff_section * +COFFObjectFile::getCOFFSection(const SectionRef &Section) const { + return toSec(Section.getRawDataRefImpl()); } -const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const { - return toSymb(It->getRawDataRefImpl()); +const coff_symbol * +COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const { + return toSymb(Symbol.getRawDataRefImpl()); } const coff_relocation * -COFFObjectFile::getCOFFRelocation(relocation_iterator &It) const { - return toRel(It->getRawDataRefImpl()); +COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const { + return toRel(Reloc.getRawDataRefImpl()); } -#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \ - case COFF::enum: Res = #enum; break; +#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \ + case COFF::reloc_type: \ + Res = #reloc_type; \ + break; error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl &Result) const { diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index 88603d45753..a1ef74efeb5 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -300,7 +300,7 @@ static char getSymbolNMTypeChar(ELFObjectFile &Obj, } static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) { - const coff_symbol *Symb = Obj.getCOFFSymbol(I); + const coff_symbol *Symb = Obj.getCOFFSymbol(*I); // OK, this is COFF. symbol_iterator SymI(I); @@ -323,7 +323,7 @@ static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) { section_iterator SecI = Obj.section_end(); if (error(SymI->getSection(SecI))) return '?'; - const coff_section *Section = Obj.getCOFFSection(SecI); + const coff_section *Section = Obj.getCOFFSection(*SecI); Characteristics = Section->Characteristics; } diff --git a/tools/llvm-objdump/COFFDump.cpp b/tools/llvm-objdump/COFFDump.cpp index e2d65a48177..49f27553c13 100644 --- a/tools/llvm-objdump/COFFDump.cpp +++ b/tools/llvm-objdump/COFFDump.cpp @@ -166,7 +166,7 @@ static error_code resolveSectionAndAddress(const COFFObjectFile *Obj, section_iterator iter(Obj->section_begin()); if (error_code EC = Sym.getSection(iter)) return EC; - ResolvedSection = Obj->getCOFFSection(iter); + ResolvedSection = Obj->getCOFFSection(*iter); return object_error::success; } @@ -381,16 +381,15 @@ static void printExportTable(const COFFObjectFile *Obj) { static bool getPDataSection(const COFFObjectFile *Obj, std::vector &Rels, const RuntimeFunction *&RFStart, int &NumRFs) { - for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end(); - SI != SE; ++SI) { + for (const SectionRef &Section : Obj->sections()) { StringRef Name; - if (error(SI->getName(Name))) + if (error(Section.getName(Name))) continue; if (Name != ".pdata") continue; - const coff_section *Pdata = Obj->getCOFFSection(SI); - for (const RelocationRef &Reloc : SI->relocations()) + const coff_section *Pdata = Obj->getCOFFSection(Section); + for (const RelocationRef &Reloc : Section.relocations()) Rels.push_back(Reloc); // Sort relocations by address. diff --git a/tools/llvm-readobj/COFFDumper.cpp b/tools/llvm-readobj/COFFDumper.cpp index 34115f481ae..e790e5ca0c4 100644 --- a/tools/llvm-readobj/COFFDumper.cpp +++ b/tools/llvm-readobj/COFFDumper.cpp @@ -55,8 +55,8 @@ public: virtual void printUnwindInfo() override; private: - void printSymbol(symbol_iterator SymI); - void printRelocation(section_iterator SecI, const RelocationRef &Reloc); + void printSymbol(const SymbolRef &Sym); + void printRelocation(const SectionRef &Section, const RelocationRef &Reloc); void printDataDirectory(uint32_t Index, const std::string &FieldName); void printX64UnwindInfo(); @@ -74,9 +74,9 @@ private: uint64_t OffsetInSection, const std::vector &Rels); - void printUnwindCode(const Win64EH::UnwindInfo& UI, ArrayRef UCs); + void printUnwindCode(const Win64EH::UnwindInfo &UI, ArrayRef UCs); - void printCodeViewLineTables(section_iterator SecI); + void printCodeViewLineTables(const SectionRef &Section); void cacheRelocations(); @@ -188,7 +188,7 @@ static error_code resolveSectionAndAddress(const COFFObjectFile *Obj, if (error_code EC = Sym.getSection(iter)) return EC; - ResolvedSection = Obj->getCOFFSection(iter); + ResolvedSection = Obj->getCOFFSection(*iter); return object_error::success; } @@ -540,11 +540,10 @@ error_code COFFDumper::getSection( } void COFFDumper::cacheRelocations() { - for (section_iterator SecI = Obj->section_begin(), SecE = Obj->section_end(); - SecI != SecE; ++SecI) { - const coff_section *Section = Obj->getCOFFSection(SecI); + for (const SectionRef &S : Obj->sections()) { + const coff_section *Section = Obj->getCOFFSection(S); - for (const RelocationRef &Reloc : SecI->relocations()) + for (const RelocationRef &Reloc : S.relocations()) RelocMap[Section].push_back(Reloc); // Sort relocations by address. @@ -653,9 +652,10 @@ void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) { void COFFDumper::printBaseOfDataField(const pe32plus_header *) {} -void COFFDumper::printCodeViewLineTables(section_iterator SecI) { +void COFFDumper::printCodeViewLineTables(const SectionRef &Section) { StringRef Data; - if (error(SecI->getContents(Data))) return; + if (error(Section.getContents(Data))) + return; SmallVector FunctionNames; StringMap FunctionLineTables; @@ -706,8 +706,8 @@ void COFFDumper::printCodeViewLineTables(section_iterator SecI) { } StringRef FunctionName; - if (error(resolveSymbolName(RelocMap[Obj->getCOFFSection(SecI)], Offset, - FunctionName))) + if (error(resolveSymbolName(RelocMap[Obj->getCOFFSection(Section)], + Offset, FunctionName))) return; W.printString("FunctionName", FunctionName); if (FunctionLineTables.count(FunctionName) != 0) { @@ -814,15 +814,13 @@ void COFFDumper::printCodeViewLineTables(section_iterator SecI) { void COFFDumper::printSections() { ListScope SectionsD(W, "Sections"); int SectionNumber = 0; - for (section_iterator SecI = Obj->section_begin(), - SecE = Obj->section_end(); - SecI != SecE; ++SecI) { + for (const SectionRef &Sec : Obj->sections()) { ++SectionNumber; - const coff_section *Section = Obj->getCOFFSection(SecI); + const coff_section *Section = Obj->getCOFFSection(Sec); StringRef Name; - if (error(SecI->getName(Name))) - Name = ""; + if (error(Sec.getName(Name))) + Name = ""; DictScope D(W, "Section"); W.printNumber("Number", SectionNumber); @@ -841,29 +839,28 @@ void COFFDumper::printSections() { if (opts::SectionRelocations) { ListScope D(W, "Relocations"); - for (const RelocationRef &Reloc : SecI->relocations()) - printRelocation(SecI, Reloc); + for (const RelocationRef &Reloc : Sec.relocations()) + printRelocation(Sec, Reloc); } if (opts::SectionSymbols) { ListScope D(W, "Symbols"); - for (symbol_iterator SymI = Obj->symbol_begin(), - SymE = Obj->symbol_end(); - SymI != SymE; ++SymI) { + for (const SymbolRef &Symbol : Obj->symbols()) { bool Contained = false; - if (SecI->containsSymbol(*SymI, Contained) || !Contained) + if (Sec.containsSymbol(Symbol, Contained) || !Contained) continue; - printSymbol(SymI); + printSymbol(Symbol); } } if (Name == ".debug$S" && opts::CodeViewLineTables) - printCodeViewLineTables(SecI); + printCodeViewLineTables(Sec); if (opts::SectionData) { StringRef Data; - if (error(SecI->getContents(Data))) break; + if (error(Sec.getContents(Data))) + break; W.printBinaryBlock("SectionData", Data); } @@ -874,23 +871,21 @@ void COFFDumper::printRelocations() { ListScope D(W, "Relocations"); int SectionNumber = 0; - for (section_iterator SecI = Obj->section_begin(), - SecE = Obj->section_end(); - SecI != SecE; ++SecI) { + for (const SectionRef &Section : Obj->sections()) { ++SectionNumber; StringRef Name; - if (error(SecI->getName(Name))) + if (error(Section.getName(Name))) continue; bool PrintedGroup = false; - for (const RelocationRef &Reloc : SecI->relocations()) { + for (const RelocationRef &Reloc : Section.relocations()) { if (!PrintedGroup) { W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; W.indent(); PrintedGroup = true; } - printRelocation(SecI, Reloc); + printRelocation(Section, Reloc); } if (PrintedGroup) { @@ -900,7 +895,7 @@ void COFFDumper::printRelocations() { } } -void COFFDumper::printRelocation(section_iterator SecI, +void COFFDumper::printRelocation(const SectionRef &Section, const RelocationRef &Reloc) { uint64_t Offset; uint64_t RelocType; @@ -916,7 +911,7 @@ void COFFDumper::printRelocation(section_iterator SecI, symbol_iterator Symbol = Reloc.getSymbol(); if (error(Symbol->getName(SymbolName))) return; - if (error(SecI->getContents(Contents))) + if (error(Section.getContents(Contents))) return; if (opts::ExpandRelocs) { @@ -936,19 +931,16 @@ void COFFDumper::printRelocation(section_iterator SecI, void COFFDumper::printSymbols() { ListScope Group(W, "Symbols"); - for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end(); - SymI != SymE; ++SymI) - printSymbol(SymI); + for (const SymbolRef &Symbol : Obj->symbols()) + printSymbol(Symbol); } -void COFFDumper::printDynamicSymbols() { - ListScope Group(W, "DynamicSymbols"); -} +void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); } -void COFFDumper::printSymbol(symbol_iterator SymI) { +void COFFDumper::printSymbol(const SymbolRef &Sym) { DictScope D(W, "Symbol"); - const coff_symbol *Symbol = Obj->getCOFFSymbol(SymI); + const coff_symbol *Symbol = Obj->getCOFFSymbol(Sym); const coff_section *Section; if (error_code EC = Obj->getSection(Symbol->SectionNumber, Section)) { W.startLine() << "Invalid section number: " << EC.message() << "\n"; @@ -1095,20 +1087,17 @@ void COFFDumper::printUnwindInfo() { } void COFFDumper::printX64UnwindInfo() { - for (section_iterator SecI = Obj->section_begin(), - SecE = Obj->section_end(); - SecI != SecE; ++SecI) { + for (const SectionRef &Section : Obj->sections()) { StringRef Name; - if (error(SecI->getName(Name))) + if (error(Section.getName(Name))) continue; if (Name != ".pdata" && !Name.startswith(".pdata$")) continue; - const coff_section *PData = Obj->getCOFFSection(SecI); + const coff_section *PData = Obj->getCOFFSection(Section); ArrayRef Contents; - if (error(Obj->getSectionContents(PData, Contents)) || - Contents.empty()) + if (error(Obj->getSectionContents(PData, Contents)) || Contents.empty()) continue; ArrayRef RFs( diff --git a/tools/obj2yaml/coff2yaml.cpp b/tools/obj2yaml/coff2yaml.cpp index 8fdd4aee0c9..6cf79aeabc7 100644 --- a/tools/obj2yaml/coff2yaml.cpp +++ b/tools/obj2yaml/coff2yaml.cpp @@ -51,9 +51,8 @@ void COFFDumper::dumpHeader(const object::coff_file_header *Header) { void COFFDumper::dumpSections(unsigned NumSections) { std::vector &Sections = YAMLObj.Sections; - for (object::section_iterator iter = Obj.section_begin(); - iter != Obj.section_end(); ++iter) { - const object::coff_section *Sect = Obj.getCOFFSection(iter); + for (const auto &Section : Obj.sections()) { + const object::coff_section *Sect = Obj.getCOFFSection(Section); COFFYAML::Section Sec; Sec.Name = Sect->Name; // FIXME: check the null termination! uint32_t Characteristics = Sect->Characteristics; @@ -65,11 +64,10 @@ void COFFDumper::dumpSections(unsigned NumSections) { Sec.SectionData = object::yaml::BinaryRef(sectionData); std::vector Relocations; - for (object::relocation_iterator rIter = iter->relocation_begin(); - rIter != iter->relocation_end(); ++rIter) { - const object::coff_relocation *reloc = Obj.getCOFFRelocation(rIter); + for (const auto &Reloc : Section.relocations()) { + const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc); COFFYAML::Relocation Rel; - object::symbol_iterator Sym = rIter->getSymbol(); + object::symbol_iterator Sym = Reloc.getSymbol(); Sym->getName(Rel.SymbolName); Rel.VirtualAddress = reloc->VirtualAddress; Rel.Type = reloc->Type; @@ -82,9 +80,8 @@ void COFFDumper::dumpSections(unsigned NumSections) { void COFFDumper::dumpSymbols(unsigned NumSymbols) { std::vector &Symbols = YAMLObj.Symbols; - for (object::symbol_iterator iter = Obj.symbol_begin(); - iter != Obj.symbol_end(); ++iter) { - const object::coff_symbol *Symbol = Obj.getCOFFSymbol(iter); + for (const auto &S : Obj.symbols()) { + const object::coff_symbol *Symbol = Obj.getCOFFSymbol(S); COFFYAML::Symbol Sym; Obj.getSymbolName(Symbol, Sym.Name); Sym.SimpleType = COFF::SymbolBaseType(Symbol->getBaseType()); -- 2.11.0