[llvm] r240708 - Add an ELFSymbolRef type.

Rafael Espindola rafael.espindola at gmail.com
Thu Jun 25 15:10:05 PDT 2015


Author: rafael
Date: Thu Jun 25 17:10:04 2015
New Revision: 240708

URL: http://llvm.org/viewvc/llvm-project?rev=240708&view=rev
Log:
Add an ELFSymbolRef type.

This allows user code to say Sym.getSize() instead of having to manually fetch
the object.

Modified:
    llvm/trunk/include/llvm/Object/ELFObjectFile.h
    llvm/trunk/lib/Object/SymbolSize.cpp
    llvm/trunk/lib/Target/X86/MCTargetDesc/X86ELFRelocationInfo.cpp
    llvm/trunk/tools/llvm-nm/llvm-nm.cpp
    llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp

Modified: llvm/trunk/include/llvm/Object/ELFObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELFObjectFile.h?rev=240708&r1=240707&r2=240708&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELFObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ELFObjectFile.h Thu Jun 25 17:10:04 2015
@@ -35,10 +35,17 @@
 namespace llvm {
 namespace object {
 
+class elf_symbol_iterator;
+class ELFSymbolRef;
+
 class ELFObjectFileBase : public ObjectFile {
+  friend class ELFSymbolRef;
+
 protected:
   ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
 
+  virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
+
 public:
   virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
 
@@ -46,17 +53,55 @@ public:
   // and addend or not.
   virtual bool hasRelocationAddend(DataRefImpl Rel) const = 0;
 
-  virtual symbol_iterator_range getDynamicSymbolIterators() const = 0;
+  typedef iterator_range<elf_symbol_iterator> elf_symbol_iterator_range;
+  virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
 
   virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
   virtual uint32_t getSectionType(SectionRef Sec) const = 0;
 
-  virtual uint64_t getSymbolSize(SymbolRef Symb) const = 0;
+  elf_symbol_iterator_range symbols() const;
 
   static inline bool classof(const Binary *v) { return v->isELF(); }
 };
 
+class ELFSymbolRef : public SymbolRef {
+public:
+  ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
+    assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
+  }
+
+  const ELFObjectFileBase *getObject() const {
+    return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
+  }
+
+  uint64_t getSize() const {
+    return getObject()->getSymbolSize(getRawDataRefImpl());
+  }
+};
+
+class elf_symbol_iterator : public symbol_iterator {
+public:
+  elf_symbol_iterator(const basic_symbol_iterator &B)
+      : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
+                                  cast<ELFObjectFileBase>(B->getObject()))) {}
+
+  const ELFSymbolRef *operator->() const {
+    return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
+  }
+
+  const ELFSymbolRef &operator*() const {
+    return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
+  }
+};
+
+inline ELFObjectFileBase::elf_symbol_iterator_range
+ELFObjectFileBase::symbols() const {
+  return elf_symbol_iterator_range(symbol_begin(), symbol_end());
+}
+
 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
+  uint64_t getSymbolSize(DataRefImpl Sym) const override;
+
 public:
   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
 
@@ -73,8 +118,6 @@ public:
   typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
   typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
 
-  uint64_t getSymbolSize(SymbolRef Symb) const override;
-
 protected:
   ELFFile<ELFT> EF;
 
@@ -206,8 +249,8 @@ public:
   basic_symbol_iterator symbol_begin_impl() const override;
   basic_symbol_iterator symbol_end_impl() const override;
 
-  symbol_iterator dynamic_symbol_begin() const;
-  symbol_iterator dynamic_symbol_end() const;
+  elf_symbol_iterator dynamic_symbol_begin() const;
+  elf_symbol_iterator dynamic_symbol_end() const;
 
   section_iterator section_begin() const override;
   section_iterator section_end() const override;
@@ -236,7 +279,7 @@ public:
                                       ELFT::Is64Bits);
   }
 
-  symbol_iterator_range getDynamicSymbolIterators() const override;
+  elf_symbol_iterator_range getDynamicSymbolIterators() const override;
 
   bool isRelocatableObject() const override;
 };
@@ -327,8 +370,8 @@ uint32_t ELFObjectFile<ELFT>::getSymbolA
 }
 
 template <class ELFT>
-uint64_t ELFObjectFile<ELFT>::getSymbolSize(SymbolRef Symb) const {
-  return toELFSymIter(Symb.getRawDataRefImpl())->st_size;
+uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
+  return toELFSymIter(Sym)->st_size;
 }
 
 template <class ELFT>
@@ -734,12 +777,12 @@ basic_symbol_iterator ELFObjectFile<ELFT
 }
 
 template <class ELFT>
-symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
+elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
   return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
 }
 
 template <class ELFT>
-symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
+elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
   return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
 }
 
@@ -862,7 +905,7 @@ unsigned ELFObjectFile<ELFT>::getArch()
 }
 
 template <class ELFT>
-ObjectFile::symbol_iterator_range
+ELFObjectFileBase::elf_symbol_iterator_range
 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
   return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
 }

Modified: llvm/trunk/lib/Object/SymbolSize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/SymbolSize.cpp?rev=240708&r1=240707&r2=240708&view=diff
==============================================================================
--- llvm/trunk/lib/Object/SymbolSize.cpp (original)
+++ llvm/trunk/lib/Object/SymbolSize.cpp Thu Jun 25 17:10:04 2015
@@ -55,8 +55,8 @@ llvm::object::computeSymbolSizes(const O
     auto Syms = E->symbols();
     if (Syms.begin() == Syms.end())
       Syms = E->getDynamicSymbolIterators();
-    for (SymbolRef Sym : Syms)
-      Ret.push_back({Sym, E->getSymbolSize(Sym)});
+    for (ELFSymbolRef Sym : Syms)
+      Ret.push_back({Sym, Sym.getSize()});
     return Ret;
   }
 

Modified: llvm/trunk/lib/Target/X86/MCTargetDesc/X86ELFRelocationInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/MCTargetDesc/X86ELFRelocationInfo.cpp?rev=240708&r1=240707&r2=240708&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/MCTargetDesc/X86ELFRelocationInfo.cpp (original)
+++ llvm/trunk/lib/Target/X86/MCTargetDesc/X86ELFRelocationInfo.cpp Thu Jun 25 17:10:04 2015
@@ -27,12 +27,12 @@ public:
 
   const MCExpr *createExprForRelocation(RelocationRef Rel) override {
     uint64_t RelType; Rel.getType(RelType);
-    symbol_iterator SymI = Rel.getSymbol();
+    elf_symbol_iterator SymI = Rel.getSymbol();
 
     StringRef SymName; SymI->getName(SymName);
     uint64_t  SymAddr; SymI->getAddress(SymAddr);
     auto *Obj = cast<ELFObjectFileBase>(Rel.getObjectFile());
-    uint64_t SymSize = Obj->getSymbolSize(*SymI);
+    uint64_t SymSize = SymI->getSize();
     int64_t Addend = *Obj->getRelocationAddend(Rel.getRawDataRefImpl());
 
     MCSymbol *Sym = Ctx.getOrCreateSymbol(SymName);

Modified: llvm/trunk/tools/llvm-nm/llvm-nm.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-nm/llvm-nm.cpp?rev=240708&r1=240707&r2=240708&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-nm/llvm-nm.cpp (original)
+++ llvm/trunk/tools/llvm-nm/llvm-nm.cpp Thu Jun 25 17:10:04 2015
@@ -930,8 +930,8 @@ static void dumpSymbolNamesFromObject(Sy
     S.Size = 0;
     S.Address = UnknownAddress;
     if (PrintSize) {
-      if (auto *E = dyn_cast<ELFObjectFileBase>(&Obj))
-        S.Size = E->getSymbolSize(Sym);
+      if (isa<ELFObjectFileBase>(&Obj))
+        S.Size = ELFSymbolRef(Sym).getSize();
     }
     if (PrintAddress && isa<ObjectFile>(Obj)) {
       if (error(SymbolRef(Sym).getAddress(S.Address)))

Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=240708&r1=240707&r2=240708&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Thu Jun 25 17:10:04 2015
@@ -1149,8 +1149,8 @@ void llvm::PrintSymbolTable(const Object
 
     outs() << '\t';
     if (Common || isa<ELFObjectFileBase>(o)) {
-      uint64_t Val = Common ? Symbol.getAlignment()
-                            : cast<ELFObjectFileBase>(o)->getSymbolSize(Symbol);
+      uint64_t Val =
+          Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
       outs() << format("\t %08" PRIx64 " ", Val);
     }
 





More information about the llvm-commits mailing list