[C++11] Introduce ObjectFile::symbols() to use range-based loops.
authorAlexey Samsonov <samsonov@google.com>
Mon, 17 Mar 2014 07:28:19 +0000 (07:28 +0000)
committerAlexey Samsonov <samsonov@google.com>
Mon, 17 Mar 2014 07:28:19 +0000 (07:28 +0000)
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

include/llvm/Object/ObjectFile.h
lib/MC/MCObjectDisassembler.cpp
lib/MC/MCObjectSymbolizer.cpp
lib/Object/MachOObjectFile.cpp
tools/llvm-objdump/MachODump.cpp
tools/llvm-objdump/llvm-objdump.cpp
tools/llvm-readobj/MachODumper.cpp
tools/llvm-symbolizer/LLVMSymbolize.cpp
tools/llvm-symbolizer/LLVMSymbolize.h
tools/macho-dump/macho-dump.cpp

index 089f624d3db5b9eb07de4258e6dade4c9c1a590f..790257cdc234b2470178c8d005d176c99d557235 100644 (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;
index 2cd7031f82c0642842f7a5241b0145dddea0e3dc..146da6d1ed3f346ae49b33e8b9580f41782d9f0a 100644 (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);
index ee2d9553ecbfa06b780275ff6a990d42b397d6c6..dc1253d6292b97a4d8d809bb42e94355db438d06 100644 (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 ||
index fb8a21467904c8fb26bfc3e8834a6f1a9dc36bc7..450693f64e1b23571c696fe32fdfec6980b8842b 100644 (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)
index e5247a64b08b826104d105314de2127b8ab73802..89b038fc068ceee28a9a339bf53dcd8c6af25864 100644 (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;
index 9f6ea35eaf58f0d09c9c5a4c55c6e813aeb5f30b..dab242cf654dac308f532fb9c402958896af96f3 100644 (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;
index a5f29b9c12c3a508e087417281a46c26977caa2c..2fd5d4acaa844afd5f6981deff8387cc2607e0b1 100644 (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() {
index c1a0654e87f4d6533c602b1514887540d8ce5a54..13f2f8fc587901684fd7150036b11722e59ec939 100644 (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] == '_')
index 53fd81837866a0363b2af3b13da6c821c2639e21..288be8022e732a9fa74b0a145a41afe94c10fd44 100644 (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;
 
index 9cfbdfb1b18f1799b6c551402a5f4d7640226e17..beeef5b176765a679be942e19fc4859e1724611c 100644 (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";