• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ELF.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 ELFFile template class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_OBJECT_ELF_H
15 #define LLVM_OBJECT_ELF_H
16 
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Object/ELFTypes.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 
21 namespace llvm {
22 namespace object {
23 
24 StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
25 
26 // Subclasses of ELFFile may need this for template instantiation
27 inline std::pair<unsigned char, unsigned char>
getElfArchType(StringRef Object)28 getElfArchType(StringRef Object) {
29   if (Object.size() < ELF::EI_NIDENT)
30     return std::make_pair((uint8_t)ELF::ELFCLASSNONE,
31                           (uint8_t)ELF::ELFDATANONE);
32   return std::make_pair((uint8_t)Object[ELF::EI_CLASS],
33                         (uint8_t)Object[ELF::EI_DATA]);
34 }
35 
36 template <class ELFT>
37 class ELFFile {
38 public:
39   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
40   typedef typename std::conditional<ELFT::Is64Bits,
41                                     uint64_t, uint32_t>::type uintX_t;
42 
43   typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
44   typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
45   typedef Elf_Sym_Impl<ELFT> Elf_Sym;
46   typedef Elf_Dyn_Impl<ELFT> Elf_Dyn;
47   typedef Elf_Phdr_Impl<ELFT> Elf_Phdr;
48   typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
49   typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
50   typedef Elf_Verdef_Impl<ELFT> Elf_Verdef;
51   typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
52   typedef Elf_Verneed_Impl<ELFT> Elf_Verneed;
53   typedef Elf_Vernaux_Impl<ELFT> Elf_Vernaux;
54   typedef Elf_Versym_Impl<ELFT> Elf_Versym;
55   typedef Elf_Hash_Impl<ELFT> Elf_Hash;
56   typedef Elf_GnuHash_Impl<ELFT> Elf_GnuHash;
57   typedef typename ELFT::DynRange Elf_Dyn_Range;
58   typedef typename ELFT::ShdrRange Elf_Shdr_Range;
59   typedef typename ELFT::SymRange Elf_Sym_Range;
60   typedef typename ELFT::RelRange Elf_Rel_Range;
61   typedef typename ELFT::RelaRange Elf_Rela_Range;
62   typedef typename ELFT::PhdrRange Elf_Phdr_Range;
63 
base()64   const uint8_t *base() const {
65     return reinterpret_cast<const uint8_t *>(Buf.data());
66   }
67 
getBufSize()68   size_t getBufSize() const { return Buf.size(); }
69 
70 private:
71 
72   StringRef Buf;
73 
74   const Elf_Ehdr *Header;
75   const Elf_Shdr *SectionHeaderTable = nullptr;
76   StringRef DotShstrtab;                    // Section header string table.
77 
78 public:
79   template<typename T>
80   const T        *getEntry(uint32_t Section, uint32_t Entry) const;
81   template <typename T>
82   const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
83 
84   ErrorOr<StringRef> getStringTable(const Elf_Shdr *Section) const;
85   ErrorOr<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
86 
87   ErrorOr<ArrayRef<Elf_Word>> getSHNDXTable(const Elf_Shdr &Section) const;
88 
89   void VerifyStrTab(const Elf_Shdr *sh) const;
90 
91   StringRef getRelocationTypeName(uint32_t Type) const;
92   void getRelocationTypeName(uint32_t Type,
93                              SmallVectorImpl<char> &Result) const;
94 
95   /// \brief Get the symbol for a given relocation.
96   const Elf_Sym *getRelocationSymbol(const Elf_Rel *Rel,
97                                      const Elf_Shdr *SymTab) const;
98 
99   ELFFile(StringRef Object, std::error_code &EC);
100 
isMipsELF64()101   bool isMipsELF64() const {
102     return Header->e_machine == ELF::EM_MIPS &&
103       Header->getFileClass() == ELF::ELFCLASS64;
104   }
105 
isMips64EL()106   bool isMips64EL() const {
107     return Header->e_machine == ELF::EM_MIPS &&
108       Header->getFileClass() == ELF::ELFCLASS64 &&
109       Header->getDataEncoding() == ELF::ELFDATA2LSB;
110   }
111 
112   ErrorOr<Elf_Shdr_Range> sections() const;
113 
symbol_begin(const Elf_Shdr * Sec)114   const Elf_Sym *symbol_begin(const Elf_Shdr *Sec) const {
115     if (!Sec)
116       return nullptr;
117     if (Sec->sh_entsize != sizeof(Elf_Sym))
118       report_fatal_error("Invalid symbol size");
119     return reinterpret_cast<const Elf_Sym *>(base() + Sec->sh_offset);
120   }
symbol_end(const Elf_Shdr * Sec)121   const Elf_Sym *symbol_end(const Elf_Shdr *Sec) const {
122     if (!Sec)
123       return nullptr;
124     uint64_t Size = Sec->sh_size;
125     if (Size % sizeof(Elf_Sym))
126       report_fatal_error("Invalid symbol table size");
127     return symbol_begin(Sec) + Size / sizeof(Elf_Sym);
128   }
symbols(const Elf_Shdr * Sec)129   Elf_Sym_Range symbols(const Elf_Shdr *Sec) const {
130     return makeArrayRef(symbol_begin(Sec), symbol_end(Sec));
131   }
132 
rela_begin(const Elf_Shdr * sec)133   const Elf_Rela *rela_begin(const Elf_Shdr *sec) const {
134     if (sec->sh_entsize != sizeof(Elf_Rela))
135       report_fatal_error("Invalid relocation entry size");
136     return reinterpret_cast<const Elf_Rela *>(base() + sec->sh_offset);
137   }
138 
rela_end(const Elf_Shdr * sec)139   const Elf_Rela *rela_end(const Elf_Shdr *sec) const {
140     uint64_t Size = sec->sh_size;
141     if (Size % sizeof(Elf_Rela))
142       report_fatal_error("Invalid relocation table size");
143     return rela_begin(sec) + Size / sizeof(Elf_Rela);
144   }
145 
relas(const Elf_Shdr * Sec)146   Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
147     return makeArrayRef(rela_begin(Sec), rela_end(Sec));
148   }
149 
rel_begin(const Elf_Shdr * sec)150   const Elf_Rel *rel_begin(const Elf_Shdr *sec) const {
151     if (sec->sh_entsize != sizeof(Elf_Rel))
152       report_fatal_error("Invalid relocation entry size");
153     return reinterpret_cast<const Elf_Rel *>(base() + sec->sh_offset);
154   }
155 
rel_end(const Elf_Shdr * sec)156   const Elf_Rel *rel_end(const Elf_Shdr *sec) const {
157     uint64_t Size = sec->sh_size;
158     if (Size % sizeof(Elf_Rel))
159       report_fatal_error("Invalid relocation table size");
160     return rel_begin(sec) + Size / sizeof(Elf_Rel);
161   }
162 
rels(const Elf_Shdr * Sec)163   Elf_Rel_Range rels(const Elf_Shdr *Sec) const {
164     return makeArrayRef(rel_begin(Sec), rel_end(Sec));
165   }
166 
167   /// \brief Iterate over program header table.
program_header_begin()168   const Elf_Phdr *program_header_begin() const {
169     if (Header->e_phnum && Header->e_phentsize != sizeof(Elf_Phdr))
170       report_fatal_error("Invalid program header size");
171     return reinterpret_cast<const Elf_Phdr *>(base() + Header->e_phoff);
172   }
173 
program_header_end()174   const Elf_Phdr *program_header_end() const {
175     return program_header_begin() + Header->e_phnum;
176   }
177 
program_headers()178   const Elf_Phdr_Range program_headers() const {
179     return makeArrayRef(program_header_begin(), program_header_end());
180   }
181 
182   uint64_t getNumSections() const;
183   uintX_t getStringTableIndex() const;
184   uint32_t getExtendedSymbolTableIndex(const Elf_Sym *Sym,
185                                        const Elf_Shdr *SymTab,
186                                        ArrayRef<Elf_Word> ShndxTable) const;
187   uint32_t getExtendedSymbolTableIndex(const Elf_Sym *Sym,
188                                        const Elf_Sym *FirstSym,
189                                        ArrayRef<Elf_Word> ShndxTable) const;
getHeader()190   const Elf_Ehdr *getHeader() const { return Header; }
191   ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *Sym,
192                                        const Elf_Shdr *SymTab,
193                                        ArrayRef<Elf_Word> ShndxTable) const;
194   ErrorOr<const Elf_Shdr *> getSection(uint32_t Index) const;
195 
getSymbol(const Elf_Shdr * Sec,uint32_t Index)196   const Elf_Sym *getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
197     return &*(symbol_begin(Sec) + Index);
198   }
199 
200   ErrorOr<StringRef> getSectionName(const Elf_Shdr *Section) const;
201   template <typename T>
202   ErrorOr<ArrayRef<T>> getSectionContentsAsArray(const Elf_Shdr *Sec) const;
203   ErrorOr<ArrayRef<uint8_t> > getSectionContents(const Elf_Shdr *Sec) const;
204 };
205 
206 typedef ELFFile<ELFType<support::little, false>> ELF32LEFile;
207 typedef ELFFile<ELFType<support::little, true>> ELF64LEFile;
208 typedef ELFFile<ELFType<support::big, false>> ELF32BEFile;
209 typedef ELFFile<ELFType<support::big, true>> ELF64BEFile;
210 
211 template <class ELFT>
getExtendedSymbolTableIndex(const Elf_Sym * Sym,const Elf_Shdr * SymTab,ArrayRef<Elf_Word> ShndxTable)212 uint32_t ELFFile<ELFT>::getExtendedSymbolTableIndex(
213     const Elf_Sym *Sym, const Elf_Shdr *SymTab,
214     ArrayRef<Elf_Word> ShndxTable) const {
215   return getExtendedSymbolTableIndex(Sym, symbol_begin(SymTab), ShndxTable);
216 }
217 
218 template <class ELFT>
getExtendedSymbolTableIndex(const Elf_Sym * Sym,const Elf_Sym * FirstSym,ArrayRef<Elf_Word> ShndxTable)219 uint32_t ELFFile<ELFT>::getExtendedSymbolTableIndex(
220     const Elf_Sym *Sym, const Elf_Sym *FirstSym,
221     ArrayRef<Elf_Word> ShndxTable) const {
222   assert(Sym->st_shndx == ELF::SHN_XINDEX);
223   unsigned Index = Sym - FirstSym;
224 
225   // The size of the table was checked in getSHNDXTable.
226   return ShndxTable[Index];
227 }
228 
229 template <class ELFT>
230 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
getSection(const Elf_Sym * Sym,const Elf_Shdr * SymTab,ArrayRef<Elf_Word> ShndxTable)231 ELFFile<ELFT>::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
232                           ArrayRef<Elf_Word> ShndxTable) const {
233   uint32_t Index = Sym->st_shndx;
234   if (Index == ELF::SHN_XINDEX)
235     return getSection(getExtendedSymbolTableIndex(Sym, SymTab, ShndxTable));
236 
237   if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
238     return nullptr;
239   return getSection(Sym->st_shndx);
240 }
241 
242 template <class ELFT>
243 template <typename T>
244 ErrorOr<ArrayRef<T>>
getSectionContentsAsArray(const Elf_Shdr * Sec)245 ELFFile<ELFT>::getSectionContentsAsArray(const Elf_Shdr *Sec) const {
246   uintX_t Offset = Sec->sh_offset;
247   uintX_t Size = Sec->sh_size;
248 
249   if (Size % sizeof(T))
250     return object_error::parse_failed;
251   if (Offset + Size > Buf.size())
252     return object_error::parse_failed;
253 
254   const T *Start = reinterpret_cast<const T *>(base() + Offset);
255   return makeArrayRef(Start, Size / sizeof(T));
256 }
257 
258 template <class ELFT>
259 ErrorOr<ArrayRef<uint8_t>>
getSectionContents(const Elf_Shdr * Sec)260 ELFFile<ELFT>::getSectionContents(const Elf_Shdr *Sec) const {
261   return getSectionContentsAsArray<uint8_t>(Sec);
262 }
263 
264 template <class ELFT>
getRelocationTypeName(uint32_t Type)265 StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
266   return getELFRelocationTypeName(Header->e_machine, Type);
267 }
268 
269 template <class ELFT>
getRelocationTypeName(uint32_t Type,SmallVectorImpl<char> & Result)270 void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
271                                           SmallVectorImpl<char> &Result) const {
272   if (!isMipsELF64()) {
273     StringRef Name = getRelocationTypeName(Type);
274     Result.append(Name.begin(), Name.end());
275   } else {
276     // The Mips N64 ABI allows up to three operations to be specified per
277     // relocation record. Unfortunately there's no easy way to test for the
278     // presence of N64 ELFs as they have no special flag that identifies them
279     // as being N64. We can safely assume at the moment that all Mips
280     // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
281     // information to disambiguate between old vs new ABIs.
282     uint8_t Type1 = (Type >> 0) & 0xFF;
283     uint8_t Type2 = (Type >> 8) & 0xFF;
284     uint8_t Type3 = (Type >> 16) & 0xFF;
285 
286     // Concat all three relocation type names.
287     StringRef Name = getRelocationTypeName(Type1);
288     Result.append(Name.begin(), Name.end());
289 
290     Name = getRelocationTypeName(Type2);
291     Result.append(1, '/');
292     Result.append(Name.begin(), Name.end());
293 
294     Name = getRelocationTypeName(Type3);
295     Result.append(1, '/');
296     Result.append(Name.begin(), Name.end());
297   }
298 }
299 
300 template <class ELFT>
301 const typename ELFFile<ELFT>::Elf_Sym *
getRelocationSymbol(const Elf_Rel * Rel,const Elf_Shdr * SymTab)302 ELFFile<ELFT>::getRelocationSymbol(const Elf_Rel *Rel,
303                                    const Elf_Shdr *SymTab) const {
304   uint32_t Index = Rel->getSymbol(isMips64EL());
305   if (Index == 0)
306     return nullptr;
307   return getEntry<Elf_Sym>(SymTab, Index);
308 }
309 
310 template <class ELFT>
getNumSections()311 uint64_t ELFFile<ELFT>::getNumSections() const {
312   assert(Header && "Header not initialized!");
313   if (Header->e_shnum == ELF::SHN_UNDEF && Header->e_shoff > 0) {
314     assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
315     return SectionHeaderTable->sh_size;
316   }
317   return Header->e_shnum;
318 }
319 
320 template <class ELFT>
getStringTableIndex()321 typename ELFFile<ELFT>::uintX_t ELFFile<ELFT>::getStringTableIndex() const {
322   if (Header->e_shnum == ELF::SHN_UNDEF) {
323     if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
324       return SectionHeaderTable->sh_link;
325     if (Header->e_shstrndx >= getNumSections())
326       return 0;
327   }
328   return Header->e_shstrndx;
329 }
330 
331 template <class ELFT>
ELFFile(StringRef Object,std::error_code & EC)332 ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
333     : Buf(Object) {
334   const uint64_t FileSize = Buf.size();
335 
336   if (sizeof(Elf_Ehdr) > FileSize) {
337     // File too short!
338     EC = object_error::parse_failed;
339     return;
340   }
341 
342   Header = reinterpret_cast<const Elf_Ehdr *>(base());
343 
344   if (Header->e_shoff == 0)
345     return;
346 
347   const uint64_t SectionTableOffset = Header->e_shoff;
348 
349   if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) {
350     // Section header table goes past end of file!
351     EC = object_error::parse_failed;
352     return;
353   }
354 
355   // The getNumSections() call below depends on SectionHeaderTable being set.
356   SectionHeaderTable =
357     reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
358   const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
359 
360   if (SectionTableOffset + SectionTableSize > FileSize) {
361     // Section table goes past end of file!
362     EC = object_error::parse_failed;
363     return;
364   }
365 
366   // Get string table sections.
367   uintX_t StringTableIndex = getStringTableIndex();
368   if (StringTableIndex) {
369     ErrorOr<const Elf_Shdr *> StrTabSecOrErr = getSection(StringTableIndex);
370     if ((EC = StrTabSecOrErr.getError()))
371       return;
372 
373     ErrorOr<StringRef> StringTableOrErr = getStringTable(*StrTabSecOrErr);
374     if ((EC = StringTableOrErr.getError()))
375       return;
376     DotShstrtab = *StringTableOrErr;
377   }
378 
379   EC = std::error_code();
380 }
381 
382 template <class ELFT>
compareAddr(uint64_t VAddr,const Elf_Phdr_Impl<ELFT> * Phdr)383 static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
384   return VAddr < Phdr->p_vaddr;
385 }
386 
387 template <class ELFT>
sections()388 ErrorOr<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const {
389   // Invalid section header entry size (e_shentsize) in ELF header
390   if (Header->e_shentsize != sizeof(Elf_Shdr))
391     return object_error::parse_failed;
392   auto *Begin = reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
393   return makeArrayRef(Begin, Begin + getNumSections());
394 }
395 
396 template <class ELFT>
397 template <typename T>
getEntry(uint32_t Section,uint32_t Entry)398 const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
399   ErrorOr<const Elf_Shdr *> Sec = getSection(Section);
400   if (std::error_code EC = Sec.getError())
401     report_fatal_error(EC.message());
402   return getEntry<T>(*Sec, Entry);
403 }
404 
405 template <class ELFT>
406 template <typename T>
getEntry(const Elf_Shdr * Section,uint32_t Entry)407 const T *ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
408                                  uint32_t Entry) const {
409   return reinterpret_cast<const T *>(base() + Section->sh_offset +
410                                      (Entry * Section->sh_entsize));
411 }
412 
413 template <class ELFT>
414 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
getSection(uint32_t Index)415 ELFFile<ELFT>::getSection(uint32_t Index) const {
416   assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
417   if (Index >= getNumSections())
418     return object_error::invalid_section_index;
419 
420   return reinterpret_cast<const Elf_Shdr *>(
421       reinterpret_cast<const char *>(SectionHeaderTable) +
422       (Index * Header->e_shentsize));
423 }
424 
425 template <class ELFT>
426 ErrorOr<StringRef>
getStringTable(const Elf_Shdr * Section)427 ELFFile<ELFT>::getStringTable(const Elf_Shdr *Section) const {
428   if (Section->sh_type != ELF::SHT_STRTAB)
429     return object_error::parse_failed;
430   uint64_t Offset = Section->sh_offset;
431   uint64_t Size = Section->sh_size;
432   if (Offset + Size > Buf.size())
433     return object_error::parse_failed;
434   StringRef Data((const char *)base() + Section->sh_offset, Size);
435   if (Data[Size - 1] != '\0')
436     return object_error::string_table_non_null_end;
437   return Data;
438 }
439 
440 template <class ELFT>
441 ErrorOr<ArrayRef<typename ELFFile<ELFT>::Elf_Word>>
getSHNDXTable(const Elf_Shdr & Section)442 ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section) const {
443   assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
444   const Elf_Word *ShndxTableBegin =
445       reinterpret_cast<const Elf_Word *>(base() + Section.sh_offset);
446   uintX_t Size = Section.sh_size;
447   if (Size % sizeof(uint32_t))
448     return object_error::parse_failed;
449   uintX_t NumSymbols = Size / sizeof(uint32_t);
450   const Elf_Word *ShndxTableEnd = ShndxTableBegin + NumSymbols;
451   if (reinterpret_cast<const char *>(ShndxTableEnd) > Buf.end())
452     return object_error::parse_failed;
453   ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Section.sh_link);
454   if (std::error_code EC = SymTableOrErr.getError())
455     return EC;
456   const Elf_Shdr &SymTable = **SymTableOrErr;
457   if (SymTable.sh_type != ELF::SHT_SYMTAB &&
458       SymTable.sh_type != ELF::SHT_DYNSYM)
459     return object_error::parse_failed;
460   if (NumSymbols != (SymTable.sh_size / sizeof(Elf_Sym)))
461     return object_error::parse_failed;
462   return makeArrayRef(ShndxTableBegin, ShndxTableEnd);
463 }
464 
465 template <class ELFT>
466 ErrorOr<StringRef>
getStringTableForSymtab(const Elf_Shdr & Sec)467 ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
468   if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
469     return object_error::parse_failed;
470   ErrorOr<const Elf_Shdr *> SectionOrErr = getSection(Sec.sh_link);
471   if (std::error_code EC = SectionOrErr.getError())
472     return EC;
473   return getStringTable(*SectionOrErr);
474 }
475 
476 template <class ELFT>
477 ErrorOr<StringRef>
getSectionName(const Elf_Shdr * Section)478 ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
479   uint32_t Offset = Section->sh_name;
480   if (Offset == 0)
481     return StringRef();
482   if (Offset >= DotShstrtab.size())
483     return object_error::parse_failed;
484   return StringRef(DotShstrtab.data() + Offset);
485 }
486 
487 /// This function returns the hash value for a symbol in the .dynsym section
488 /// Name of the API remains consistent as specified in the libelf
489 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
elf_hash(StringRef & symbolName)490 static inline unsigned elf_hash(StringRef &symbolName) {
491   unsigned h = 0, g;
492   for (unsigned i = 0, j = symbolName.size(); i < j; i++) {
493     h = (h << 4) + symbolName[i];
494     g = h & 0xf0000000L;
495     if (g != 0)
496       h ^= g >> 24;
497     h &= ~g;
498   }
499   return h;
500 }
501 } // end namespace object
502 } // end namespace llvm
503 
504 #endif
505