[llvm] r205508 - Implement get getSymbolFileOffset with getSymbolAddress.

Rafael Espindola rafael.espindola at gmail.com
Wed Apr 2 20:13:33 PDT 2014


Author: rafael
Date: Wed Apr  2 22:13:33 2014
New Revision: 205508

URL: http://llvm.org/viewvc/llvm-project?rev=205508&view=rev
Log:
Implement get getSymbolFileOffset with getSymbolAddress.

This has the following advantages:
* Less code.
* The old ELF implementation was wrong for non-relocatable objects.
* The old ELF implementation (and I think MachO) was wrong for thumb.

No current testcase since this is only used from MCJIT and it only uses
relocatable objects and I don't think it supports thumb yet.

Modified:
    llvm/trunk/include/llvm/Object/COFF.h
    llvm/trunk/include/llvm/Object/ELFObjectFile.h
    llvm/trunk/include/llvm/Object/MachO.h
    llvm/trunk/include/llvm/Object/ObjectFile.h
    llvm/trunk/lib/Object/COFFObjectFile.cpp
    llvm/trunk/lib/Object/MachOObjectFile.cpp

Modified: llvm/trunk/include/llvm/Object/COFF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/COFF.h?rev=205508&r1=205507&r2=205508&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/COFF.h (original)
+++ llvm/trunk/include/llvm/Object/COFF.h Wed Apr  2 22:13:33 2014
@@ -363,8 +363,6 @@ protected:
   void moveSymbolNext(DataRefImpl &Symb) const override;
   error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const override;
   error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const override;
-  error_code getSymbolFileOffset(DataRefImpl Symb,
-                                 uint64_t &Res) const override;
   error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
   error_code getSymbolType(DataRefImpl Symb,

Modified: llvm/trunk/include/llvm/Object/ELFObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELFObjectFile.h?rev=205508&r1=205507&r2=205508&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELFObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ELFObjectFile.h Wed Apr  2 22:13:33 2014
@@ -58,8 +58,6 @@ protected:
 
   void moveSymbolNext(DataRefImpl &Symb) const override;
   error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const override;
-  error_code getSymbolFileOffset(DataRefImpl Symb,
-                                 uint64_t &Res) const override;
   error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const override;
   error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const override;
   error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
@@ -239,39 +237,6 @@ error_code ELFObjectFile<ELFT>::getSymbo
 }
 
 template <class ELFT>
-error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
-                                                    uint64_t &Result) const {
-  const Elf_Sym *ESym = getSymbol(Symb);
-  const Elf_Shdr *ESec;
-  switch (EF.getSymbolTableIndex(ESym)) {
-  case ELF::SHN_COMMON:
-  // Unintialized symbols have no offset in the object file
-  case ELF::SHN_UNDEF:
-    Result = UnknownAddressOrSize;
-    return object_error::success;
-  case ELF::SHN_ABS:
-    Result = ESym->st_value;
-    return object_error::success;
-  default:
-    ESec = EF.getSection(ESym);
-  }
-
-  switch (ESym->getType()) {
-  case ELF::STT_SECTION:
-    Result = ESec ? ESec->sh_offset : UnknownAddressOrSize;
-    return object_error::success;
-  case ELF::STT_FUNC:
-  case ELF::STT_OBJECT:
-  case ELF::STT_NOTYPE:
-    Result = ESym->st_value + (ESec ? ESec->sh_offset : 0);
-    return object_error::success;
-  default:
-    Result = UnknownAddressOrSize;
-    return object_error::success;
-  }
-}
-
-template <class ELFT>
 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
                                                  uint64_t &Result) const {
   const Elf_Sym *ESym = getSymbol(Symb);

Modified: llvm/trunk/include/llvm/Object/MachO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachO.h?rev=205508&r1=205507&r2=205508&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachO.h (original)
+++ llvm/trunk/include/llvm/Object/MachO.h Wed Apr  2 22:13:33 2014
@@ -62,8 +62,6 @@ public:
   void moveSymbolNext(DataRefImpl &Symb) const override;
   error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const override;
   error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const override;
-  error_code getSymbolFileOffset(DataRefImpl Symb,
-                                 uint64_t &Res) const override;
   error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const override;
   error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
   error_code getSymbolType(DataRefImpl Symb,

Modified: llvm/trunk/include/llvm/Object/ObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ObjectFile.h?rev=205508&r1=205507&r2=205508&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ObjectFile.h Wed Apr  2 22:13:33 2014
@@ -228,7 +228,6 @@ protected:
   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
   error_code printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override;
   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const = 0;
-  virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res)const=0;
   virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
   virtual error_code getSymbolType(DataRefImpl Symb,
@@ -352,7 +351,30 @@ inline error_code SymbolRef::getAddress(
 }
 
 inline error_code SymbolRef::getFileOffset(uint64_t &Result) const {
-  return getObject()->getSymbolFileOffset(getRawDataRefImpl(), Result);
+  uint64_t Address;
+  if (error_code EC = getAddress(Address))
+    return EC;
+
+  const ObjectFile *Obj = getObject();
+  section_iterator SecI(Obj->section_begin());
+  if (error_code EC = getSection(SecI))
+    return EC;
+
+  uint64_t SectionAddress;
+  if (error_code EC = SecI->getAddress(SectionAddress))
+    return EC;
+
+  uint64_t OffsetInSection = Address - SectionAddress;
+
+  StringRef SecContents;
+  if (error_code EC = SecI->getContents(SecContents))
+    return EC;
+
+  // FIXME: this is a hack.
+  uint64_t SectionOffset = (uint64_t)SecContents.data() - (uint64_t)Obj->base();
+
+  Result = SectionOffset + OffsetInSection;
+  return object_error::success;
 }
 
 inline error_code SymbolRef::getAlignment(uint32_t &Result) const {

Modified: llvm/trunk/lib/Object/COFFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFObjectFile.cpp?rev=205508&r1=205507&r2=205508&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/COFFObjectFile.cpp Wed Apr  2 22:13:33 2014
@@ -135,22 +135,6 @@ error_code COFFObjectFile::getSymbolName
   return getSymbolName(Symb, Result);
 }
 
-error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref,
-                                            uint64_t &Result) const {
-  const coff_symbol *Symb = toSymb(Ref);
-  const coff_section *Section = NULL;
-  if (error_code EC = getSection(Symb->SectionNumber, Section))
-    return EC;
-
-  if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
-    Result = UnknownAddressOrSize;
-  else if (Section)
-    Result = Section->PointerToRawData + Symb->Value;
-  else
-    Result = Symb->Value;
-  return object_error::success;
-}
-
 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
                                             uint64_t &Result) const {
   const coff_symbol *Symb = toSymb(Ref);

Modified: llvm/trunk/lib/Object/MachOObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObjectFile.cpp?rev=205508&r1=205507&r2=205508&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/MachOObjectFile.cpp Wed Apr  2 22:13:33 2014
@@ -479,29 +479,6 @@ error_code MachOObjectFile::getSymbolAdd
   return object_error::success;
 }
 
-error_code
-MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
-                                     uint64_t &Res) const {
-  nlist_base Entry = getSymbolTableEntryBase(this, Symb);
-  getSymbolAddress(Symb, Res);
-  if (Entry.n_sect) {
-    uint64_t Delta;
-    DataRefImpl SecRel;
-    SecRel.d.a = Entry.n_sect-1;
-    if (is64Bit()) {
-      MachO::section_64 Sec = getSection64(SecRel);
-      Delta = Sec.offset - Sec.addr;
-    } else {
-      MachO::section Sec = getSection(SecRel);
-      Delta = Sec.offset - Sec.addr;
-    }
-
-    Res += Delta;
-  }
-
-  return object_error::success;
-}
-
 error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
                                                uint32_t &Result) const {
   uint32_t flags = getSymbolFlags(DRI);





More information about the llvm-commits mailing list