[C++11] Introduce ObjectFile::symbols() to use range-based loops.

Reviewers: rafael

Reviewed By: rafael

CC: llvm-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204031 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alexey Samsonov 2014-03-17 07:28:19 +00:00
parent 6e3aceffd3
commit 133aacf0dd
10 changed files with 90 additions and 90 deletions

View File

@ -286,6 +286,10 @@ protected:
virtual error_code getLibraryPath(DataRefImpl Lib, StringRef &Res) const = 0; virtual error_code getLibraryPath(DataRefImpl Lib, StringRef &Res) const = 0;
public: public:
typedef iterator_range<symbol_iterator> symbol_iterator_range;
symbol_iterator_range symbols() const {
return symbol_iterator_range(symbol_begin(), symbol_end());
}
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;

View File

@ -37,13 +37,12 @@ MCObjectDisassembler::MCObjectDisassembler(const ObjectFile &Obj,
: Obj(Obj), Dis(Dis), MIA(MIA), MOS(0) {} : Obj(Obj), Dis(Dis), MIA(MIA), MOS(0) {}
uint64_t MCObjectDisassembler::getEntrypoint() { uint64_t MCObjectDisassembler::getEntrypoint() {
for (symbol_iterator SI = Obj.symbol_begin(), SE = Obj.symbol_end(); for (const SymbolRef &Symbol : Obj.symbols()) {
SI != SE; ++SI) {
StringRef Name; StringRef Name;
SI->getName(Name); Symbol.getName(Name);
if (Name == "main" || Name == "_main") { if (Name == "main" || Name == "_main") {
uint64_t Entrypoint; uint64_t Entrypoint;
SI->getAddress(Entrypoint); Symbol.getAddress(Entrypoint);
return getEffectiveLoadAddr(Entrypoint); return getEffectiveLoadAddr(Entrypoint);
} }
} }
@ -181,13 +180,12 @@ void MCObjectDisassembler::buildCFG(MCModule *Module) {
AddressSetTy Splits; AddressSetTy Splits;
AddressSetTy Calls; AddressSetTy Calls;
for (symbol_iterator SI = Obj.symbol_begin(), SE = Obj.symbol_end(); for (const SymbolRef &Symbol : Obj.symbols()) {
SI != SE; ++SI) {
SymbolRef::Type SymType; SymbolRef::Type SymType;
SI->getType(SymType); Symbol.getType(SymType);
if (SymType == SymbolRef::ST_Function) { if (SymType == SymbolRef::ST_Function) {
uint64_t SymAddr; uint64_t SymAddr;
SI->getAddress(SymAddr); Symbol.getAddress(SymAddr);
SymAddr = getEffectiveLoadAddr(SymAddr); SymAddr = getEffectiveLoadAddr(SymAddr);
Calls.push_back(SymAddr); Calls.push_back(SymAddr);
Splits.push_back(SymAddr); Splits.push_back(SymAddr);

View File

@ -153,14 +153,17 @@ tryAddingSymbolicOperand(MCInst &MI, raw_ostream &cStream,
return false; return false;
uint64_t UValue = Value; uint64_t UValue = Value;
// FIXME: map instead of looping each time? // FIXME: map instead of looping each time?
for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end(); for (const SymbolRef &Symbol : Obj->symbols()) {
SI != SE; ++SI) { uint64_t SymAddr;
uint64_t SymAddr; SI->getAddress(SymAddr); Symbol.getAddress(SymAddr);
uint64_t SymSize; SI->getSize(SymSize); uint64_t SymSize;
StringRef SymName; SI->getName(SymName); Symbol.getSize(SymSize);
SymbolRef::Type SymType; SI->getType(SymType); StringRef SymName;
if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize Symbol.getName(SymName);
|| SymName.empty() || SymType != SymbolRef::ST_Function) SymbolRef::Type SymType;
Symbol.getType(SymType);
if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize ||
SymName.empty() || SymType != SymbolRef::ST_Function)
continue; continue;
if ( SymAddr == UValue || if ( SymAddr == UValue ||

View File

@ -296,16 +296,16 @@ static void printRelocationTargetName(const MachOObjectFile *O,
if (IsScattered) { if (IsScattered) {
uint32_t Val = O->getPlainRelocationSymbolNum(RE); uint32_t Val = O->getPlainRelocationSymbolNum(RE);
for (symbol_iterator SI = O->symbol_begin(), SE = O->symbol_end(); for (const SymbolRef &Symbol : O->symbols()) {
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 = Symbol.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 = Symbol.getName(Name)))
report_fatal_error(ec.message()); report_fatal_error(ec.message());
fmt << Name; fmt << Name;
return; return;
@ -528,8 +528,8 @@ error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
} }
// Unfortunately symbols are unsorted so we need to touch all // Unfortunately symbols are unsorted so we need to touch all
// symbols from load command // symbols from load command
for (symbol_iterator I = symbol_begin(), E = symbol_end(); I != E; ++I) { for (const SymbolRef &Symbol : symbols()) {
DataRefImpl DRI = I->getRawDataRefImpl(); DataRefImpl DRI = Symbol.getRawDataRefImpl();
Entry = getSymbolTableEntryBase(this, DRI); Entry = getSymbolTableEntryBase(this, DRI);
getSymbolAddress(DRI, Value); getSymbolAddress(DRI, Value);
if (Entry.n_sect == SectionIndex && Value > BeginOffset) if (Entry.n_sect == SectionIndex && Value > BeginOffset)

View File

@ -146,17 +146,14 @@ static void DumpDataInCode(const char *bytes, uint64_t Size,
} }
} }
static void static void getSectionsAndSymbols(const MachO::mach_header Header,
getSectionsAndSymbols(const MachO::mach_header Header,
MachOObjectFile *MachOObj, MachOObjectFile *MachOObj,
std::vector<SectionRef> &Sections, std::vector<SectionRef> &Sections,
std::vector<SymbolRef> &Symbols, std::vector<SymbolRef> &Symbols,
SmallVectorImpl<uint64_t> &FoundFns, SmallVectorImpl<uint64_t> &FoundFns,
uint64_t &BaseSegmentAddress) { uint64_t &BaseSegmentAddress) {
for (symbol_iterator SI = MachOObj->symbol_begin(), for (const SymbolRef &Symbol : MachOObj->symbols())
SE = MachOObj->symbol_end(); Symbols.push_back(Symbol);
SI != SE; ++SI)
Symbols.push_back(*SI);
for (const SectionRef &Section : MachOObj->sections()) { for (const SectionRef &Section : MachOObj->sections()) {
StringRef SectName; StringRef SectName;

View File

@ -408,13 +408,12 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
break; break;
// Make a list of all the symbols in this section. // Make a list of all the symbols in this section.
std::vector<std::pair<uint64_t, StringRef> > Symbols; std::vector<std::pair<uint64_t, StringRef>> Symbols;
for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end(); for (const SymbolRef &Symbol : Obj->symbols()) {
SI != SE; ++SI) {
bool contains; bool contains;
if (!error(Section.containsSymbol(*SI, contains)) && contains) { if (!error(Section.containsSymbol(Symbol, contains)) && contains) {
uint64_t Address; uint64_t Address;
if (error(SI->getAddress(Address))) if (error(Symbol.getAddress(Address)))
break; break;
if (Address == UnknownAddressOrSize) if (Address == UnknownAddressOrSize)
continue; continue;
@ -423,7 +422,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
continue; continue;
StringRef Name; StringRef Name;
if (error(SI->getName(Name))) if (error(Symbol.getName(Name)))
break; break;
Symbols.push_back(std::make_pair(Address, Name)); Symbols.push_back(std::make_pair(Address, Name));
} }
@ -703,19 +702,23 @@ static void PrintSymbolTable(const ObjectFile *o) {
if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
PrintCOFFSymbolTable(coff); PrintCOFFSymbolTable(coff);
else { else {
for (symbol_iterator si = o->symbol_begin(), se = o->symbol_end(); for (const SymbolRef &Symbol : o->symbols()) {
si != se; ++si) {
StringRef Name; StringRef Name;
uint64_t Address; uint64_t Address;
SymbolRef::Type Type; SymbolRef::Type Type;
uint64_t Size; uint64_t Size;
uint32_t Flags = si->getFlags(); uint32_t Flags = Symbol.getFlags();
section_iterator Section = o->section_end(); section_iterator Section = o->section_end();
if (error(si->getName(Name))) continue; if (error(Symbol.getName(Name)))
if (error(si->getAddress(Address))) continue; continue;
if (error(si->getType(Type))) continue; if (error(Symbol.getAddress(Address)))
if (error(si->getSize(Size))) continue; continue;
if (error(si->getSection(Section))) continue; if (error(Symbol.getType(Type)))
continue;
if (error(Symbol.getSize(Size)))
continue;
if (error(Symbol.getSection(Section)))
continue;
bool Global = Flags & SymbolRef::SF_Global; bool Global = Flags & SymbolRef::SF_Global;
bool Weak = Flags & SymbolRef::SF_Weak; bool Weak = Flags & SymbolRef::SF_Weak;

View File

@ -38,7 +38,7 @@ public:
virtual void printUnwindInfo() override; virtual void printUnwindInfo() override;
private: private:
void printSymbol(symbol_iterator SymI); void printSymbol(const SymbolRef &Symbol);
void printRelocation(const RelocationRef &Reloc); void printRelocation(const RelocationRef &Reloc);
@ -255,13 +255,12 @@ void MachODumper::printSections(const MachOObjectFile *Obj) {
if (opts::SectionSymbols) { if (opts::SectionSymbols) {
ListScope D(W, "Symbols"); ListScope D(W, "Symbols");
for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end(); for (const SymbolRef &Symbol : Obj->symbols()) {
SymI != SymE; ++SymI) {
bool Contained = false; bool Contained = false;
if (Section.containsSymbol(*SymI, Contained) || !Contained) if (Section.containsSymbol(Symbol, Contained) || !Contained)
continue; continue;
printSymbol(SymI); printSymbol(Symbol);
} }
} }
@ -354,9 +353,8 @@ void MachODumper::printRelocation(const MachOObjectFile *Obj,
void MachODumper::printSymbols() { void MachODumper::printSymbols() {
ListScope Group(W, "Symbols"); ListScope Group(W, "Symbols");
for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end(); for (const SymbolRef &Symbol : Obj->symbols()) {
SymI != SymE; ++SymI) { printSymbol(Symbol);
printSymbol(SymI);
} }
} }
@ -364,38 +362,37 @@ void MachODumper::printDynamicSymbols() {
ListScope Group(W, "DynamicSymbols"); ListScope Group(W, "DynamicSymbols");
} }
void MachODumper::printSymbol(symbol_iterator SymI) { void MachODumper::printSymbol(const SymbolRef &Symbol) {
StringRef SymbolName; StringRef SymbolName;
if (SymI->getName(SymbolName)) if (Symbol.getName(SymbolName))
SymbolName = ""; SymbolName = "";
MachOSymbol Symbol; MachOSymbol MOSymbol;
getSymbol(Obj, SymI->getRawDataRefImpl(), Symbol); getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
StringRef SectionName = ""; StringRef SectionName = "";
section_iterator SecI(Obj->section_begin()); section_iterator SecI(Obj->section_begin());
if (!error(SymI->getSection(SecI)) && if (!error(Symbol.getSection(SecI)) && SecI != Obj->section_end())
SecI != Obj->section_end())
error(SecI->getName(SectionName)); error(SecI->getName(SectionName));
DictScope D(W, "Symbol"); DictScope D(W, "Symbol");
W.printNumber("Name", SymbolName, Symbol.StringIndex); W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
if (Symbol.Type & MachO::N_STAB) { if (MOSymbol.Type & MachO::N_STAB) {
W.printHex ("Type", "SymDebugTable", Symbol.Type); W.printHex("Type", "SymDebugTable", MOSymbol.Type);
} else { } else {
if (Symbol.Type & MachO::N_PEXT) if (MOSymbol.Type & MachO::N_PEXT)
W.startLine() << "PrivateExtern\n"; W.startLine() << "PrivateExtern\n";
if (Symbol.Type & MachO::N_EXT) if (MOSymbol.Type & MachO::N_EXT)
W.startLine() << "Extern\n"; W.startLine() << "Extern\n";
W.printEnum("Type", uint8_t(Symbol.Type & MachO::N_TYPE), W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
makeArrayRef(MachOSymbolTypes)); makeArrayRef(MachOSymbolTypes));
} }
W.printHex ("Section", SectionName, Symbol.SectionIndex); W.printHex("Section", SectionName, MOSymbol.SectionIndex);
W.printEnum ("RefType", static_cast<uint16_t>(Symbol.Flags & 0xF), W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0xF),
makeArrayRef(MachOSymbolRefTypes)); makeArrayRef(MachOSymbolRefTypes));
W.printFlags ("Flags", static_cast<uint16_t>(Symbol.Flags & ~0xF), W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0xF),
makeArrayRef(MachOSymbolFlags)); makeArrayRef(MachOSymbolFlags));
W.printHex ("Value", Symbol.Value); W.printHex("Value", MOSymbol.Value);
} }
void MachODumper::printUnwindInfo() { void MachODumper::printUnwindInfo() {

View File

@ -53,9 +53,8 @@ static void patchFunctionNameInDILineInfo(const std::string &NewFunctionName,
ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx) ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
: Module(Obj), DebugInfoContext(DICtx) { : Module(Obj), DebugInfoContext(DICtx) {
for (symbol_iterator si = Module->symbol_begin(), se = Module->symbol_end(); for (const SymbolRef &Symbol : Module->symbols()) {
si != se; ++si) { addSymbol(Symbol);
addSymbol(si);
} }
bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end()); bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end());
if (NoSymbolTable && Module->isELF()) { if (NoSymbolTable && Module->isELF()) {
@ -63,20 +62,19 @@ ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
std::pair<symbol_iterator, symbol_iterator> IDyn = std::pair<symbol_iterator, symbol_iterator> IDyn =
getELFDynamicSymbolIterators(Module); getELFDynamicSymbolIterators(Module);
for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) { for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) {
addSymbol(si); addSymbol(*si);
} }
} }
} }
void ModuleInfo::addSymbol(const symbol_iterator &Sym) { void ModuleInfo::addSymbol(const SymbolRef &Symbol) {
SymbolRef::Type SymbolType; SymbolRef::Type SymbolType;
if (error(Sym->getType(SymbolType))) if (error(Symbol.getType(SymbolType)))
return; return;
if (SymbolType != SymbolRef::ST_Function && if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
SymbolType != SymbolRef::ST_Data)
return; return;
uint64_t SymbolAddress; uint64_t SymbolAddress;
if (error(Sym->getAddress(SymbolAddress)) || if (error(Symbol.getAddress(SymbolAddress)) ||
SymbolAddress == UnknownAddressOrSize) SymbolAddress == UnknownAddressOrSize)
return; return;
uint64_t SymbolSize; uint64_t SymbolSize;
@ -84,11 +82,11 @@ void ModuleInfo::addSymbol(const symbol_iterator &Sym) {
// occupies the memory range up to the following symbol. // occupies the memory range up to the following symbol.
if (isa<MachOObjectFile>(Module)) if (isa<MachOObjectFile>(Module))
SymbolSize = 0; SymbolSize = 0;
else if (error(Sym->getSize(SymbolSize)) || else if (error(Symbol.getSize(SymbolSize)) ||
SymbolSize == UnknownAddressOrSize) SymbolSize == UnknownAddressOrSize)
return; return;
StringRef SymbolName; StringRef SymbolName;
if (error(Sym->getName(SymbolName))) if (error(Symbol.getName(SymbolName)))
return; return;
// Mach-O symbol table names have leading underscore, skip it. // Mach-O symbol table names have leading underscore, skip it.
if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_') if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')

View File

@ -101,7 +101,7 @@ private:
bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address, bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
std::string &Name, uint64_t &Addr, std::string &Name, uint64_t &Addr,
uint64_t &Size) const; uint64_t &Size) const;
void addSymbol(const symbol_iterator &Sym); void addSymbol(const SymbolRef &Symbol);
ObjectFile *Module; ObjectFile *Module;
std::unique_ptr<DIContext> DebugInfoContext; std::unique_ptr<DIContext> DebugInfoContext;

View File

@ -202,9 +202,8 @@ static int DumpSymtabCommand(const MachOObjectFile &Obj) {
// Dump the symbol table. // Dump the symbol table.
outs() << " ('_symbols', [\n"; outs() << " ('_symbols', [\n";
unsigned SymNum = 0; unsigned SymNum = 0;
for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E; for (const SymbolRef &Symbol : Obj.symbols()) {
++I, ++SymNum) { DataRefImpl DRI = Symbol.getRawDataRefImpl();
DataRefImpl DRI = I->getRawDataRefImpl();
if (Obj.is64Bit()) { if (Obj.is64Bit()) {
MachO::nlist_64 STE = Obj.getSymbol64TableEntry(DRI); MachO::nlist_64 STE = Obj.getSymbol64TableEntry(DRI);
DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type, DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
@ -216,6 +215,7 @@ static int DumpSymtabCommand(const MachOObjectFile &Obj) {
STE.n_sect, STE.n_desc, STE.n_value, STE.n_sect, STE.n_desc, STE.n_value,
StringTable); StringTable);
} }
SymNum++;
} }
outs() << " ])\n"; outs() << " ])\n";