Home | History | Annotate | Download | only in Object
      1 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file declares the ELFObjectFile template class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
     15 #define LLVM_OBJECT_ELFOBJECTFILE_H
     16 
     17 #include "llvm/ADT/DenseMap.h"
     18 #include "llvm/ADT/PointerIntPair.h"
     19 #include "llvm/ADT/SmallVector.h"
     20 #include "llvm/ADT/StringSwitch.h"
     21 #include "llvm/ADT/Triple.h"
     22 #include "llvm/Object/ELF.h"
     23 #include "llvm/Object/ObjectFile.h"
     24 #include "llvm/Support/Casting.h"
     25 #include "llvm/Support/ELF.h"
     26 #include "llvm/Support/Endian.h"
     27 #include "llvm/Support/ErrorHandling.h"
     28 #include "llvm/Support/MemoryBuffer.h"
     29 #include "llvm/Support/raw_ostream.h"
     30 #include <algorithm>
     31 #include <cctype>
     32 #include <limits>
     33 #include <utility>
     34 
     35 namespace llvm {
     36 namespace object {
     37 
     38 class ELFObjectFileBase : public ObjectFile {
     39 protected:
     40   ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
     41 
     42 public:
     43   virtual std::error_code getRelocationAddend(DataRefImpl Rel,
     44                                               int64_t &Res) const = 0;
     45   virtual std::pair<symbol_iterator, symbol_iterator>
     46   getELFDynamicSymbolIterators() const = 0;
     47 
     48   virtual std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
     49                                            bool &IsDefault) const = 0;
     50 
     51   virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
     52   virtual uint32_t getSectionType(SectionRef Sec) const = 0;
     53 
     54   static inline bool classof(const Binary *v) { return v->isELF(); }
     55 };
     56 
     57 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
     58 public:
     59   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
     60 
     61   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
     62 
     63   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
     64   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
     65   typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
     66   typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
     67   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
     68   typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
     69 
     70   typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
     71   typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
     72   typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
     73 
     74 protected:
     75   ELFFile<ELFT> EF;
     76 
     77   void moveSymbolNext(DataRefImpl &Symb) const override;
     78   std::error_code getSymbolName(DataRefImpl Symb,
     79                                 StringRef &Res) const override;
     80   std::error_code getSymbolAddress(DataRefImpl Symb,
     81                                    uint64_t &Res) const override;
     82   std::error_code getSymbolAlignment(DataRefImpl Symb,
     83                                      uint32_t &Res) const override;
     84   std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
     85   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
     86   std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
     87   std::error_code getSymbolType(DataRefImpl Symb,
     88                                 SymbolRef::Type &Res) const override;
     89   std::error_code getSymbolSection(DataRefImpl Symb,
     90                                    section_iterator &Res) const override;
     91 
     92   void moveSectionNext(DataRefImpl &Sec) const override;
     93   std::error_code getSectionName(DataRefImpl Sec,
     94                                  StringRef &Res) const override;
     95   uint64_t getSectionAddress(DataRefImpl Sec) const override;
     96   uint64_t getSectionSize(DataRefImpl Sec) const override;
     97   std::error_code getSectionContents(DataRefImpl Sec,
     98                                      StringRef &Res) const override;
     99   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
    100   bool isSectionText(DataRefImpl Sec) const override;
    101   bool isSectionData(DataRefImpl Sec) const override;
    102   bool isSectionBSS(DataRefImpl Sec) const override;
    103   bool isSectionVirtual(DataRefImpl Sec) const override;
    104   bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
    105   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
    106   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
    107   section_iterator getRelocatedSection(DataRefImpl Sec) const override;
    108 
    109   void moveRelocationNext(DataRefImpl &Rel) const override;
    110   std::error_code getRelocationAddress(DataRefImpl Rel,
    111                                        uint64_t &Res) const override;
    112   std::error_code getRelocationOffset(DataRefImpl Rel,
    113                                       uint64_t &Res) const override;
    114   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
    115   std::error_code getRelocationType(DataRefImpl Rel,
    116                                     uint64_t &Res) const override;
    117   std::error_code
    118   getRelocationTypeName(DataRefImpl Rel,
    119                         SmallVectorImpl<char> &Result) const override;
    120   std::error_code
    121   getRelocationValueString(DataRefImpl Rel,
    122                            SmallVectorImpl<char> &Result) const override;
    123 
    124   uint64_t getROffset(DataRefImpl Rel) const;
    125   StringRef getRelocationTypeName(uint32_t Type) const;
    126 
    127   /// \brief Get the relocation section that contains \a Rel.
    128   const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
    129     return EF.getSection(Rel.d.a);
    130   }
    131 
    132   const Elf_Rel *getRel(DataRefImpl Rel) const;
    133   const Elf_Rela *getRela(DataRefImpl Rela) const;
    134 
    135   Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
    136     bool IsDynamic = Symb.p & 1;
    137     if (IsDynamic)
    138       return Elf_Sym_Iter(
    139           EF.begin_dynamic_symbols().getEntSize(),
    140           reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
    141     return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
    142                         reinterpret_cast<const char *>(Symb.p), IsDynamic);
    143   }
    144 
    145   DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
    146     DataRefImpl DRI;
    147     DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
    148       static_cast<uintptr_t>(Symb.isDynamic());
    149     return DRI;
    150   }
    151 
    152   Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
    153     return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
    154                          reinterpret_cast<const char *>(Sec.p));
    155   }
    156 
    157   DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
    158     DataRefImpl DRI;
    159     DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
    160     return DRI;
    161   }
    162 
    163   DataRefImpl toDRI(const Elf_Shdr *Sec) const {
    164     DataRefImpl DRI;
    165     DRI.p = reinterpret_cast<uintptr_t>(Sec);
    166     return DRI;
    167   }
    168 
    169   Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
    170     return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
    171                         reinterpret_cast<const char *>(Dyn.p));
    172   }
    173 
    174   DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
    175     DataRefImpl DRI;
    176     DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
    177     return DRI;
    178   }
    179 
    180   bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
    181     unsigned char Binding = ESym->getBinding();
    182     unsigned char Visibility = ESym->getVisibility();
    183 
    184     // A symbol is exported if its binding is either GLOBAL or WEAK, and its
    185     // visibility is either DEFAULT or PROTECTED. All other symbols are not
    186     // exported.
    187     if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
    188         (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
    189       return true;
    190 
    191     return false;
    192   }
    193 
    194   // This flag is used for classof, to distinguish ELFObjectFile from
    195   // its subclass. If more subclasses will be created, this flag will
    196   // have to become an enum.
    197   bool isDyldELFObject;
    198 
    199 public:
    200   ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
    201 
    202   const Elf_Sym *getSymbol(DataRefImpl Symb) const;
    203 
    204   basic_symbol_iterator symbol_begin_impl() const override;
    205   basic_symbol_iterator symbol_end_impl() const override;
    206 
    207   symbol_iterator dynamic_symbol_begin() const;
    208   symbol_iterator dynamic_symbol_end() const;
    209 
    210   section_iterator section_begin() const override;
    211   section_iterator section_end() const override;
    212 
    213   std::error_code getRelocationAddend(DataRefImpl Rel,
    214                                       int64_t &Res) const override;
    215   std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
    216                                    bool &IsDefault) const override;
    217 
    218   uint64_t getSectionFlags(SectionRef Sec) const override;
    219   uint32_t getSectionType(SectionRef Sec) const override;
    220 
    221   uint8_t getBytesInAddress() const override;
    222   StringRef getFileFormatName() const override;
    223   unsigned getArch() const override;
    224   StringRef getLoadName() const;
    225 
    226   std::error_code getPlatformFlags(unsigned &Result) const override {
    227     Result = EF.getHeader()->e_flags;
    228     return object_error::success;
    229   }
    230 
    231   const ELFFile<ELFT> *getELFFile() const { return &EF; }
    232 
    233   bool isDyldType() const { return isDyldELFObject; }
    234   static inline bool classof(const Binary *v) {
    235     return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
    236                                       ELFT::Is64Bits);
    237   }
    238 
    239   std::pair<symbol_iterator, symbol_iterator>
    240   getELFDynamicSymbolIterators() const override;
    241 
    242   bool isRelocatableObject() const override;
    243 };
    244 
    245 // Use an alignment of 2 for the typedefs since that is the worst case for
    246 // ELF files in archives.
    247 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
    248 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
    249 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
    250 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
    251 
    252 template <class ELFT>
    253 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
    254   Symb = toDRI(++toELFSymIter(Symb));
    255 }
    256 
    257 template <class ELFT>
    258 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
    259                                                    StringRef &Result) const {
    260   ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
    261   if (!Name)
    262     return Name.getError();
    263   Result = *Name;
    264   return object_error::success;
    265 }
    266 
    267 template <class ELFT>
    268 std::error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
    269                                                       StringRef &Version,
    270                                                       bool &IsDefault) const {
    271   DataRefImpl Symb = SymRef.getRawDataRefImpl();
    272   const Elf_Sym *symb = getSymbol(Symb);
    273   ErrorOr<StringRef> Ver =
    274       EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
    275   if (!Ver)
    276     return Ver.getError();
    277   Version = *Ver;
    278   return object_error::success;
    279 }
    280 
    281 template <class ELFT>
    282 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
    283   DataRefImpl DRI = Sec.getRawDataRefImpl();
    284   return toELFShdrIter(DRI)->sh_flags;
    285 }
    286 
    287 template <class ELFT>
    288 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
    289   DataRefImpl DRI = Sec.getRawDataRefImpl();
    290   return toELFShdrIter(DRI)->sh_type;
    291 }
    292 
    293 template <class ELFT>
    294 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
    295                                                       uint64_t &Result) const {
    296   const Elf_Sym *ESym = getSymbol(Symb);
    297   switch (EF.getSymbolTableIndex(ESym)) {
    298   case ELF::SHN_COMMON:
    299   case ELF::SHN_UNDEF:
    300     Result = UnknownAddressOrSize;
    301     return object_error::success;
    302   case ELF::SHN_ABS:
    303     Result = ESym->st_value;
    304     return object_error::success;
    305   default:
    306     break;
    307   }
    308 
    309   const Elf_Ehdr *Header = EF.getHeader();
    310   Result = ESym->st_value;
    311 
    312   // Clear the ARM/Thumb or microMIPS indicator flag.
    313   if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
    314       ESym->getType() == ELF::STT_FUNC)
    315     Result &= ~1;
    316 
    317   if (Header->e_type == ELF::ET_REL) {
    318     const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
    319     if (Section != nullptr)
    320       Result += Section->sh_addr;
    321   }
    322 
    323   return object_error::success;
    324 }
    325 
    326 template <class ELFT>
    327 std::error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
    328                                                         uint32_t &Res) const {
    329   Elf_Sym_Iter Sym = toELFSymIter(Symb);
    330   if (Sym->st_shndx == ELF::SHN_COMMON)
    331     Res = Sym->st_value;
    332   else
    333     Res = 0;
    334   return object_error::success;
    335 }
    336 
    337 template <class ELFT>
    338 std::error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
    339                                                    uint64_t &Result) const {
    340   Result = toELFSymIter(Symb)->st_size;
    341   return object_error::success;
    342 }
    343 
    344 template <class ELFT>
    345 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
    346                                                     uint8_t &Result) const {
    347   Result = toELFSymIter(Symb)->st_other;
    348   return object_error::success;
    349 }
    350 
    351 template <class ELFT>
    352 std::error_code
    353 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
    354                                    SymbolRef::Type &Result) const {
    355   const Elf_Sym *ESym = getSymbol(Symb);
    356 
    357   switch (ESym->getType()) {
    358   case ELF::STT_NOTYPE:
    359     Result = SymbolRef::ST_Unknown;
    360     break;
    361   case ELF::STT_SECTION:
    362     Result = SymbolRef::ST_Debug;
    363     break;
    364   case ELF::STT_FILE:
    365     Result = SymbolRef::ST_File;
    366     break;
    367   case ELF::STT_FUNC:
    368     Result = SymbolRef::ST_Function;
    369     break;
    370   case ELF::STT_OBJECT:
    371   case ELF::STT_COMMON:
    372   case ELF::STT_TLS:
    373     Result = SymbolRef::ST_Data;
    374     break;
    375   default:
    376     Result = SymbolRef::ST_Other;
    377     break;
    378   }
    379   return object_error::success;
    380 }
    381 
    382 template <class ELFT>
    383 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
    384   Elf_Sym_Iter EIter = toELFSymIter(Symb);
    385   const Elf_Sym *ESym = &*EIter;
    386 
    387   uint32_t Result = SymbolRef::SF_None;
    388 
    389   if (ESym->getBinding() != ELF::STB_LOCAL)
    390     Result |= SymbolRef::SF_Global;
    391 
    392   if (ESym->getBinding() == ELF::STB_WEAK)
    393     Result |= SymbolRef::SF_Weak;
    394 
    395   if (ESym->st_shndx == ELF::SHN_ABS)
    396     Result |= SymbolRef::SF_Absolute;
    397 
    398   if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
    399       EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
    400     Result |= SymbolRef::SF_FormatSpecific;
    401 
    402   if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
    403     Result |= SymbolRef::SF_Undefined;
    404 
    405   if (ESym->getType() == ELF::STT_COMMON ||
    406       EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
    407     Result |= SymbolRef::SF_Common;
    408 
    409   if (isExportedToOtherDSO(ESym))
    410     Result |= SymbolRef::SF_Exported;
    411 
    412   return Result;
    413 }
    414 
    415 template <class ELFT>
    416 std::error_code
    417 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
    418                                       section_iterator &Res) const {
    419   const Elf_Sym *ESym = getSymbol(Symb);
    420   const Elf_Shdr *ESec = EF.getSection(ESym);
    421   if (!ESec)
    422     Res = section_end();
    423   else {
    424     DataRefImpl Sec;
    425     Sec.p = reinterpret_cast<intptr_t>(ESec);
    426     Res = section_iterator(SectionRef(Sec, this));
    427   }
    428   return object_error::success;
    429 }
    430 
    431 template <class ELFT>
    432 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
    433   Sec = toDRI(++toELFShdrIter(Sec));
    434 }
    435 
    436 template <class ELFT>
    437 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
    438                                                     StringRef &Result) const {
    439   ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
    440   if (!Name)
    441     return Name.getError();
    442   Result = *Name;
    443   return object_error::success;
    444 }
    445 
    446 template <class ELFT>
    447 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
    448   return toELFShdrIter(Sec)->sh_addr;
    449 }
    450 
    451 template <class ELFT>
    452 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
    453   return toELFShdrIter(Sec)->sh_size;
    454 }
    455 
    456 template <class ELFT>
    457 std::error_code
    458 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
    459                                         StringRef &Result) const {
    460   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
    461   Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
    462   return object_error::success;
    463 }
    464 
    465 template <class ELFT>
    466 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
    467   return toELFShdrIter(Sec)->sh_addralign;
    468 }
    469 
    470 template <class ELFT>
    471 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
    472   return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
    473 }
    474 
    475 template <class ELFT>
    476 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
    477   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
    478   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
    479          EShdr->sh_type == ELF::SHT_PROGBITS;
    480 }
    481 
    482 template <class ELFT>
    483 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
    484   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
    485   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
    486          EShdr->sh_type == ELF::SHT_NOBITS;
    487 }
    488 
    489 template <class ELFT>
    490 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
    491   return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
    492 }
    493 
    494 template <class ELFT>
    495 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
    496                                                 DataRefImpl Symb) const {
    497   Elf_Sym_Iter ESym = toELFSymIter(Symb);
    498 
    499   uintX_t Index = ESym->st_shndx;
    500   bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
    501 
    502   return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
    503 }
    504 
    505 template <class ELFT>
    506 relocation_iterator
    507 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
    508   DataRefImpl RelData;
    509   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
    510   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
    511   RelData.d.b = 0;
    512   return relocation_iterator(RelocationRef(RelData, this));
    513 }
    514 
    515 template <class ELFT>
    516 relocation_iterator
    517 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
    518   DataRefImpl RelData;
    519   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
    520   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
    521   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
    522   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
    523     RelData.d.b = 0;
    524   else
    525     RelData.d.b = S->sh_size / S->sh_entsize;
    526 
    527   return relocation_iterator(RelocationRef(RelData, this));
    528 }
    529 
    530 template <class ELFT>
    531 section_iterator
    532 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
    533   if (EF.getHeader()->e_type != ELF::ET_REL)
    534     return section_end();
    535 
    536   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
    537   uintX_t Type = EShdr->sh_type;
    538   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
    539     return section_end();
    540 
    541   const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
    542   return section_iterator(SectionRef(toDRI(R), this));
    543 }
    544 
    545 // Relocations
    546 template <class ELFT>
    547 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
    548   ++Rel.d.b;
    549 }
    550 
    551 template <class ELFT>
    552 symbol_iterator
    553 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
    554   uint32_t symbolIdx;
    555   const Elf_Shdr *sec = getRelSection(Rel);
    556   switch (sec->sh_type) {
    557   default:
    558     report_fatal_error("Invalid section type in Rel!");
    559   case ELF::SHT_REL: {
    560     symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
    561     break;
    562   }
    563   case ELF::SHT_RELA: {
    564     symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
    565     break;
    566   }
    567   }
    568   if (!symbolIdx)
    569     return symbol_end();
    570 
    571   const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
    572 
    573   DataRefImpl SymbolData;
    574   switch (SymSec->sh_type) {
    575   default:
    576     report_fatal_error("Invalid symbol table section type!");
    577   case ELF::SHT_SYMTAB:
    578     SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
    579     break;
    580   case ELF::SHT_DYNSYM:
    581     SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
    582     break;
    583   }
    584 
    585   return symbol_iterator(SymbolRef(SymbolData, this));
    586 }
    587 
    588 template <class ELFT>
    589 std::error_code
    590 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
    591                                           uint64_t &Result) const {
    592   uint64_t ROffset = getROffset(Rel);
    593   const Elf_Ehdr *Header = EF.getHeader();
    594 
    595   if (Header->e_type == ELF::ET_REL) {
    596     const Elf_Shdr *RelocationSec = getRelSection(Rel);
    597     const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
    598     Result = ROffset + RelocatedSec->sh_addr;
    599   } else {
    600     Result = ROffset;
    601   }
    602 
    603   return object_error::success;
    604 }
    605 
    606 template <class ELFT>
    607 std::error_code
    608 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
    609                                          uint64_t &Result) const {
    610   assert(EF.getHeader()->e_type == ELF::ET_REL &&
    611          "Only relocatable object files have relocation offsets");
    612   Result = getROffset(Rel);
    613   return object_error::success;
    614 }
    615 
    616 template <class ELFT>
    617 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
    618   const Elf_Shdr *sec = getRelSection(Rel);
    619   switch (sec->sh_type) {
    620   default:
    621     report_fatal_error("Invalid section type in Rel!");
    622   case ELF::SHT_REL:
    623     return getRel(Rel)->r_offset;
    624   case ELF::SHT_RELA:
    625     return getRela(Rel)->r_offset;
    626   }
    627 }
    628 
    629 template <class ELFT>
    630 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
    631                                                        uint64_t &Result) const {
    632   const Elf_Shdr *sec = getRelSection(Rel);
    633   switch (sec->sh_type) {
    634   default:
    635     report_fatal_error("Invalid section type in Rel!");
    636   case ELF::SHT_REL: {
    637     Result = getRel(Rel)->getType(EF.isMips64EL());
    638     break;
    639   }
    640   case ELF::SHT_RELA: {
    641     Result = getRela(Rel)->getType(EF.isMips64EL());
    642     break;
    643   }
    644   }
    645   return object_error::success;
    646 }
    647 
    648 template <class ELFT>
    649 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
    650   return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
    651 }
    652 
    653 template <class ELFT>
    654 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
    655     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
    656   const Elf_Shdr *sec = getRelSection(Rel);
    657   uint32_t type;
    658   switch (sec->sh_type) {
    659   default:
    660     return object_error::parse_failed;
    661   case ELF::SHT_REL: {
    662     type = getRel(Rel)->getType(EF.isMips64EL());
    663     break;
    664   }
    665   case ELF::SHT_RELA: {
    666     type = getRela(Rel)->getType(EF.isMips64EL());
    667     break;
    668   }
    669   }
    670 
    671   EF.getRelocationTypeName(type, Result);
    672   return object_error::success;
    673 }
    674 
    675 template <class ELFT>
    676 std::error_code
    677 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
    678                                          int64_t &Result) const {
    679   const Elf_Shdr *sec = getRelSection(Rel);
    680   switch (sec->sh_type) {
    681   default:
    682     report_fatal_error("Invalid section type in Rel!");
    683   case ELF::SHT_REL: {
    684     Result = 0;
    685     return object_error::success;
    686   }
    687   case ELF::SHT_RELA: {
    688     Result = getRela(Rel)->r_addend;
    689     return object_error::success;
    690   }
    691   }
    692 }
    693 
    694 template <class ELFT>
    695 std::error_code ELFObjectFile<ELFT>::getRelocationValueString(
    696     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
    697   const Elf_Shdr *sec = getRelSection(Rel);
    698   uint8_t type;
    699   StringRef res;
    700   int64_t addend = 0;
    701   uint16_t symbol_index = 0;
    702   switch (sec->sh_type) {
    703   default:
    704     return object_error::parse_failed;
    705   case ELF::SHT_REL: {
    706     type = getRel(Rel)->getType(EF.isMips64EL());
    707     symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
    708     // TODO: Read implicit addend from section data.
    709     break;
    710   }
    711   case ELF::SHT_RELA: {
    712     type = getRela(Rel)->getType(EF.isMips64EL());
    713     symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
    714     addend = getRela(Rel)->r_addend;
    715     break;
    716   }
    717   }
    718   const Elf_Sym *symb =
    719       EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
    720   ErrorOr<StringRef> SymName =
    721       EF.getSymbolName(EF.getSection(sec->sh_link), symb);
    722   if (!SymName)
    723     return SymName.getError();
    724   switch (EF.getHeader()->e_machine) {
    725   case ELF::EM_X86_64:
    726     switch (type) {
    727     case ELF::R_X86_64_PC8:
    728     case ELF::R_X86_64_PC16:
    729     case ELF::R_X86_64_PC32: {
    730       std::string fmtbuf;
    731       raw_string_ostream fmt(fmtbuf);
    732       fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
    733       fmt.flush();
    734       Result.append(fmtbuf.begin(), fmtbuf.end());
    735     } break;
    736     case ELF::R_X86_64_8:
    737     case ELF::R_X86_64_16:
    738     case ELF::R_X86_64_32:
    739     case ELF::R_X86_64_32S:
    740     case ELF::R_X86_64_64: {
    741       std::string fmtbuf;
    742       raw_string_ostream fmt(fmtbuf);
    743       fmt << *SymName << (addend < 0 ? "" : "+") << addend;
    744       fmt.flush();
    745       Result.append(fmtbuf.begin(), fmtbuf.end());
    746     } break;
    747     default:
    748       res = "Unknown";
    749     }
    750     break;
    751   case ELF::EM_AARCH64: {
    752     std::string fmtbuf;
    753     raw_string_ostream fmt(fmtbuf);
    754     fmt << *SymName;
    755     if (addend != 0)
    756       fmt << (addend < 0 ? "" : "+") << addend;
    757     fmt.flush();
    758     Result.append(fmtbuf.begin(), fmtbuf.end());
    759     break;
    760   }
    761   case ELF::EM_386:
    762   case ELF::EM_ARM:
    763   case ELF::EM_HEXAGON:
    764   case ELF::EM_MIPS:
    765     res = *SymName;
    766     break;
    767   default:
    768     res = "Unknown";
    769   }
    770   if (Result.empty())
    771     Result.append(res.begin(), res.end());
    772   return object_error::success;
    773 }
    774 
    775 template <class ELFT>
    776 const typename ELFFile<ELFT>::Elf_Sym *
    777 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
    778   return &*toELFSymIter(Symb);
    779 }
    780 
    781 template <class ELFT>
    782 const typename ELFObjectFile<ELFT>::Elf_Rel *
    783 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
    784   return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
    785 }
    786 
    787 template <class ELFT>
    788 const typename ELFObjectFile<ELFT>::Elf_Rela *
    789 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
    790   return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
    791 }
    792 
    793 template <class ELFT>
    794 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
    795     : ELFObjectFileBase(
    796           getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
    797                          support::little,
    798                      ELFT::Is64Bits),
    799           Object),
    800       EF(Data.getBuffer(), EC) {}
    801 
    802 template <class ELFT>
    803 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
    804   return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
    805 }
    806 
    807 template <class ELFT>
    808 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
    809   return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
    810 }
    811 
    812 template <class ELFT>
    813 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
    814   return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
    815 }
    816 
    817 template <class ELFT>
    818 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
    819   return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
    820 }
    821 
    822 template <class ELFT>
    823 section_iterator ELFObjectFile<ELFT>::section_begin() const {
    824   return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
    825 }
    826 
    827 template <class ELFT>
    828 section_iterator ELFObjectFile<ELFT>::section_end() const {
    829   return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
    830 }
    831 
    832 template <class ELFT>
    833 StringRef ELFObjectFile<ELFT>::getLoadName() const {
    834   Elf_Dyn_Iter DI = EF.begin_dynamic_table();
    835   Elf_Dyn_Iter DE = EF.end_dynamic_table();
    836 
    837   while (DI != DE && DI->getTag() != ELF::DT_SONAME)
    838     ++DI;
    839 
    840   if (DI != DE)
    841     return EF.getDynamicString(DI->getVal());
    842   return "";
    843 }
    844 
    845 template <class ELFT>
    846 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
    847   return ELFT::Is64Bits ? 8 : 4;
    848 }
    849 
    850 template <class ELFT>
    851 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
    852   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
    853   switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
    854   case ELF::ELFCLASS32:
    855     switch (EF.getHeader()->e_machine) {
    856     case ELF::EM_386:
    857       return "ELF32-i386";
    858     case ELF::EM_X86_64:
    859       return "ELF32-x86-64";
    860     case ELF::EM_ARM:
    861       return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
    862     case ELF::EM_HEXAGON:
    863       return "ELF32-hexagon";
    864     case ELF::EM_MIPS:
    865       return "ELF32-mips";
    866     case ELF::EM_PPC:
    867       return "ELF32-ppc";
    868     case ELF::EM_SPARC:
    869     case ELF::EM_SPARC32PLUS:
    870       return "ELF32-sparc";
    871     default:
    872       return "ELF32-unknown";
    873     }
    874   case ELF::ELFCLASS64:
    875     switch (EF.getHeader()->e_machine) {
    876     case ELF::EM_386:
    877       return "ELF64-i386";
    878     case ELF::EM_X86_64:
    879       return "ELF64-x86-64";
    880     case ELF::EM_AARCH64:
    881       return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
    882     case ELF::EM_PPC64:
    883       return "ELF64-ppc64";
    884     case ELF::EM_S390:
    885       return "ELF64-s390";
    886     case ELF::EM_SPARCV9:
    887       return "ELF64-sparc";
    888     case ELF::EM_MIPS:
    889       return "ELF64-mips";
    890     default:
    891       return "ELF64-unknown";
    892     }
    893   default:
    894     // FIXME: Proper error handling.
    895     report_fatal_error("Invalid ELFCLASS!");
    896   }
    897 }
    898 
    899 template <class ELFT>
    900 unsigned ELFObjectFile<ELFT>::getArch() const {
    901   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
    902   switch (EF.getHeader()->e_machine) {
    903   case ELF::EM_386:
    904     return Triple::x86;
    905   case ELF::EM_X86_64:
    906     return Triple::x86_64;
    907   case ELF::EM_AARCH64:
    908     return Triple::aarch64;
    909   case ELF::EM_ARM:
    910     return Triple::arm;
    911   case ELF::EM_HEXAGON:
    912     return Triple::hexagon;
    913   case ELF::EM_MIPS:
    914     switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
    915     case ELF::ELFCLASS32:
    916       return IsLittleEndian ? Triple::mipsel : Triple::mips;
    917     case ELF::ELFCLASS64:
    918       return IsLittleEndian ? Triple::mips64el : Triple::mips64;
    919     default:
    920       report_fatal_error("Invalid ELFCLASS!");
    921     }
    922   case ELF::EM_PPC:
    923     return Triple::ppc;
    924   case ELF::EM_PPC64:
    925     return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
    926   case ELF::EM_S390:
    927     return Triple::systemz;
    928 
    929   case ELF::EM_SPARC:
    930   case ELF::EM_SPARC32PLUS:
    931     return Triple::sparc;
    932   case ELF::EM_SPARCV9:
    933     return Triple::sparcv9;
    934 
    935   default:
    936     return Triple::UnknownArch;
    937   }
    938 }
    939 
    940 template <class ELFT>
    941 std::pair<symbol_iterator, symbol_iterator>
    942 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
    943   return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
    944 }
    945 
    946 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
    947   return EF.getHeader()->e_type == ELF::ET_REL;
    948 }
    949 
    950 inline std::error_code getELFRelocationAddend(const RelocationRef R,
    951                                               int64_t &Addend) {
    952   const ObjectFile *Obj = R.getObjectFile();
    953   DataRefImpl DRI = R.getRawDataRefImpl();
    954   return cast<ELFObjectFileBase>(Obj)->getRelocationAddend(DRI, Addend);
    955 }
    956 
    957 inline std::pair<symbol_iterator, symbol_iterator>
    958 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
    959   return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
    960 }
    961 
    962 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
    963                                            const SymbolRef &Sym,
    964                                            StringRef &Version,
    965                                            bool &IsDefault) {
    966   return cast<ELFObjectFileBase>(Obj)
    967       ->getSymbolVersion(Sym, Version, IsDefault);
    968 }
    969 }
    970 }
    971 
    972 #endif
    973