[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;
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_end() const = 0;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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