1 //===- ELF.h - ELF object file implementation -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the ELFFile template class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_ELF_H
15 #define LLVM_OBJECT_ELF_H
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"
39 StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
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]);
54 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
55 typedef typename std::conditional<ELFT::Is64Bits,
56 uint64_t, uint32_t>::type uintX_t;
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;
74 /// \brief Archive files are 2 byte aligned, so we need this for
75 /// PointerIntPair to work.
77 class ArchivePointerTypeTraits {
79 static inline const void *getAsVoidPointer(T *P) { return P; }
80 static inline T *getFromVoidPointer(const void *P) {
81 return static_cast<T *>(P);
83 enum { NumLowBitsAvailable = 1 };
86 typedef iterator_range<const Elf_Sym *> Elf_Sym_Range;
88 const uint8_t *base() const {
89 return reinterpret_cast<const uint8_t *>(Buf.data());
93 typedef SmallVector<const Elf_Shdr *, 2> Sections_t;
94 typedef DenseMap<unsigned, unsigned> IndexMap_t;
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.
105 const Elf_Shdr *SymbolTableSectionHeaderIndex = nullptr;
106 DenseMap<const Elf_Sym *, ELF::Elf64_Word> ExtendedSymbolTable;
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;
114 const Elf_Shdr *getDotSymtabSec() const { return dot_symtab_sec; }
115 const Elf_Shdr *getDotDynSymSec() const { return DotDynSymSec; }
117 ErrorOr<StringRef> getStringTable(const Elf_Shdr *Section) const;
118 ErrorOr<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
120 void VerifyStrTab(const Elf_Shdr *sh) const;
122 StringRef getRelocationTypeName(uint32_t Type) const;
123 void getRelocationTypeName(uint32_t Type,
124 SmallVectorImpl<char> &Result) const;
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;
131 ELFFile(StringRef Object, std::error_code &EC);
133 bool isMipsELF64() const {
134 return Header->e_machine == ELF::EM_MIPS &&
135 Header->getFileClass() == ELF::ELFCLASS64;
138 bool isMips64EL() const {
139 return Header->e_machine == ELF::EM_MIPS &&
140 Header->getFileClass() == ELF::ELFCLASS64 &&
141 Header->getDataEncoding() == ELF::ELFDATA2LSB;
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());
150 const Elf_Sym *symbol_begin(const Elf_Shdr *Sec) const {
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);
157 const Elf_Sym *symbol_end(const Elf_Shdr *Sec) const {
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);
165 Elf_Sym_Range symbols(const Elf_Shdr *Sec) const {
166 return make_range(symbol_begin(Sec), symbol_end(Sec));
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); }
173 const Elf_Sym *dynamic_symbol_begin() const {
174 return symbol_begin(DotDynSymSec);
176 const Elf_Sym *dynamic_symbol_end() const { return symbol_end(DotDynSymSec); }
177 Elf_Sym_Range dynamic_symbols() const { return symbols(DotDynSymSec); }
179 typedef iterator_range<const Elf_Rela *> Elf_Rela_Range;
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);
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);
194 Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
195 return make_range(rela_begin(Sec), rela_end(Sec));
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);
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);
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));
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);
223 const Elf_Phdr *program_header_end() const {
224 return program_header_begin() + Header->e_phnum;
227 typedef iterator_range<const Elf_Phdr *> Elf_Phdr_Range;
229 const Elf_Phdr_Range program_headers() const {
230 return make_range(program_header_begin(), program_header_end());
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;
240 const Elf_Sym *getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
241 return &*(symbol_begin(Sec) + Index);
244 ErrorOr<StringRef> getSectionName(const Elf_Shdr *Section) const;
245 ErrorOr<ArrayRef<uint8_t> > getSectionContents(const Elf_Shdr *Sec) const;
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;
253 template <class ELFT>
255 ELFFile<ELFT>::getExtendedSymbolTableIndex(const Elf_Sym *symb) const {
256 assert(symb->st_shndx == ELF::SHN_XINDEX);
257 return ExtendedSymbolTable.lookup(symb);
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)
268 return getSection(symb->st_shndx);
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);
280 template <class ELFT>
281 StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
282 return getELFRelocationTypeName(Header->e_machine, Type);
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());
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;
302 // Concat all three relocation type names.
303 StringRef Name = getRelocationTypeName(Type1);
304 Result.append(Name.begin(), Name.end());
306 Name = getRelocationTypeName(Type2);
307 Result.append(1, '/');
308 Result.append(Name.begin(), Name.end());
310 Name = getRelocationTypeName(Type3);
311 Result.append(1, '/');
312 Result.append(Name.begin(), Name.end());
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 {
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())));
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;
338 return Header->e_shnum;
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())
349 return Header->e_shstrndx;
352 template <class ELFT>
353 ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
355 const uint64_t FileSize = Buf.size();
357 if (sizeof(Elf_Ehdr) > FileSize) {
359 EC = object_error::parse_failed;
363 Header = reinterpret_cast<const Elf_Ehdr *>(base());
365 if (Header->e_shoff == 0)
368 const uint64_t SectionTableOffset = Header->e_shoff;
370 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) {
371 // Section header table goes past end of file!
372 EC = object_error::parse_failed;
376 // The getNumSections() call below depends on SectionHeaderTable being set.
378 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
379 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
381 if (SectionTableOffset + SectionTableSize > FileSize) {
382 // Section table goes past end of file!
383 EC = object_error::parse_failed;
387 // Scan sections for special sections.
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;
397 SymbolTableSectionHeaderIndex = &Sec;
399 case ELF::SHT_SYMTAB: {
400 if (dot_symtab_sec) {
401 // More than one .symtab!
402 EC = object_error::parse_failed;
405 dot_symtab_sec = &Sec;
406 ErrorOr<StringRef> SymtabOrErr = getStringTableForSymtab(Sec);
407 if ((EC = SymtabOrErr.getError()))
409 DotStrtab = *SymtabOrErr;
411 case ELF::SHT_DYNSYM: {
413 // More than one .dynsym!
414 EC = object_error::parse_failed;
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()))
430 ErrorOr<StringRef> SymtabOrErr = getStringTable(*StrTabSecOrErr);
431 if ((EC = SymtabOrErr.getError()))
433 DotShstrtab = *SymtabOrErr;
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;
447 EC = std::error_code();
450 template <class ELFT>
451 static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
452 return VAddr < Phdr->p_vaddr;
455 template <class ELFT>
456 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
457 if (Header->e_shentsize != sizeof(Elf_Shdr))
459 "Invalid section header entry size (e_shentsize) in ELF header");
460 return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
463 template <class ELFT>
464 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_end() const {
465 return section_begin() + getNumSections();
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);
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));
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;
492 return reinterpret_cast<const Elf_Shdr *>(
493 reinterpret_cast<const char *>(SectionHeaderTable) +
494 (Index * Header->e_shentsize));
497 template <class ELFT>
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;
512 template <class ELFT>
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())
520 return getStringTable(*SectionOrErr);
523 template <class ELFT>
525 ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
526 uint32_t Offset = Section->sh_name;
529 if (Offset >= DotShstrtab.size())
530 return object_error::parse_failed;
531 return StringRef(DotShstrtab.data() + Offset);
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) {
539 for (unsigned i = 0, j = symbolName.size(); i < j; i++) {
540 h = (h << 4) + symbolName[i];
548 } // end namespace object
549 } // end namespace llvm