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