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 ErrorOr<const Elf_Dyn *> dynamic_table_begin(const Elf_Phdr *Phdr) const;
145 ErrorOr<const Elf_Dyn *> dynamic_table_end(const Elf_Phdr *Phdr) const;
146 ErrorOr<Elf_Dyn_Range> dynamic_table(const Elf_Phdr *Phdr) const {
147 ErrorOr<const Elf_Dyn *> Begin = dynamic_table_begin(Phdr);
148 if (std::error_code EC = Begin.getError())
150 ErrorOr<const Elf_Dyn *> End = dynamic_table_end(Phdr);
151 if (std::error_code EC = End.getError())
153 return make_range(*Begin, *End);
156 const Elf_Shdr *section_begin() const;
157 const Elf_Shdr *section_end() const;
158 Elf_Shdr_Range sections() const {
159 return make_range(section_begin(), section_end());
162 const Elf_Sym *symbol_begin(const Elf_Shdr *Sec) const {
165 if (Sec->sh_entsize != sizeof(Elf_Sym))
166 report_fatal_error("Invalid symbol size");
167 return reinterpret_cast<const Elf_Sym *>(base() + Sec->sh_offset);
169 const Elf_Sym *symbol_end(const Elf_Shdr *Sec) const {
172 uint64_t Size = Sec->sh_size;
173 if (Size % sizeof(Elf_Sym))
174 report_fatal_error("Invalid symbol table size");
175 return symbol_begin(Sec) + Size / sizeof(Elf_Sym);
177 Elf_Sym_Range symbols(const Elf_Shdr *Sec) const {
178 return make_range(symbol_begin(Sec), symbol_end(Sec));
181 const Elf_Sym *symbol_begin() const { return symbol_begin(dot_symtab_sec); }
182 const Elf_Sym *symbol_end() const { return symbol_end(dot_symtab_sec); }
183 Elf_Sym_Range symbols() const { return symbols(dot_symtab_sec); }
185 const Elf_Sym *dynamic_symbol_begin() const {
186 return symbol_begin(DotDynSymSec);
188 const Elf_Sym *dynamic_symbol_end() const { return symbol_end(DotDynSymSec); }
189 Elf_Sym_Range dynamic_symbols() const { return symbols(DotDynSymSec); }
191 typedef iterator_range<const Elf_Rela *> Elf_Rela_Range;
193 const Elf_Rela *rela_begin(const Elf_Shdr *sec) const {
194 if (sec->sh_entsize != sizeof(Elf_Rela))
195 report_fatal_error("Invalid relocation entry size");
196 return reinterpret_cast<const Elf_Rela *>(base() + sec->sh_offset);
199 const Elf_Rela *rela_end(const Elf_Shdr *sec) const {
200 uint64_t Size = sec->sh_size;
201 if (Size % sizeof(Elf_Rela))
202 report_fatal_error("Invalid relocation table size");
203 return rela_begin(sec) + Size / sizeof(Elf_Rela);
206 Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
207 return make_range(rela_begin(Sec), rela_end(Sec));
210 const Elf_Rel *rel_begin(const Elf_Shdr *sec) const {
211 if (sec->sh_entsize != sizeof(Elf_Rel))
212 report_fatal_error("Invalid relocation entry size");
213 return reinterpret_cast<const Elf_Rel *>(base() + sec->sh_offset);
216 const Elf_Rel *rel_end(const Elf_Shdr *sec) const {
217 uint64_t Size = sec->sh_size;
218 if (Size % sizeof(Elf_Rel))
219 report_fatal_error("Invalid relocation table size");
220 return rel_begin(sec) + Size / sizeof(Elf_Rel);
223 typedef iterator_range<const Elf_Rel *> Elf_Rel_Range;
224 Elf_Rel_Range rels(const Elf_Shdr *Sec) const {
225 return make_range(rel_begin(Sec), rel_end(Sec));
228 /// \brief Iterate over program header table.
229 const Elf_Phdr *program_header_begin() const {
230 if (Header->e_phnum && Header->e_phentsize != sizeof(Elf_Phdr))
231 report_fatal_error("Invalid program header size");
232 return reinterpret_cast<const Elf_Phdr *>(base() + Header->e_phoff);
235 const Elf_Phdr *program_header_end() const {
236 return program_header_begin() + Header->e_phnum;
239 typedef iterator_range<const Elf_Phdr *> Elf_Phdr_Range;
241 const Elf_Phdr_Range program_headers() const {
242 return make_range(program_header_begin(), program_header_end());
245 uint64_t getNumSections() const;
246 uintX_t getStringTableIndex() const;
247 ELF::Elf64_Word getExtendedSymbolTableIndex(const Elf_Sym *symb) const;
248 const Elf_Ehdr *getHeader() const { return Header; }
249 ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *symb) const;
250 ErrorOr<const Elf_Shdr *> getSection(uint32_t Index) const;
252 const Elf_Sym *getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
253 return &*(symbol_begin(Sec) + Index);
256 ErrorOr<StringRef> getSectionName(const Elf_Shdr *Section) const;
257 ErrorOr<ArrayRef<uint8_t> > getSectionContents(const Elf_Shdr *Sec) const;
260 typedef ELFFile<ELFType<support::little, false>> ELF32LEFile;
261 typedef ELFFile<ELFType<support::little, true>> ELF64LEFile;
262 typedef ELFFile<ELFType<support::big, false>> ELF32BEFile;
263 typedef ELFFile<ELFType<support::big, true>> ELF64BEFile;
265 template <class ELFT>
267 ELFFile<ELFT>::getExtendedSymbolTableIndex(const Elf_Sym *symb) const {
268 assert(symb->st_shndx == ELF::SHN_XINDEX);
269 return ExtendedSymbolTable.lookup(symb);
272 template <class ELFT>
273 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
274 ELFFile<ELFT>::getSection(const Elf_Sym *symb) const {
275 uint32_t Index = symb->st_shndx;
276 if (Index == ELF::SHN_XINDEX)
277 return getSection(ExtendedSymbolTable.lookup(symb));
278 if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
280 return getSection(symb->st_shndx);
283 template <class ELFT>
284 ErrorOr<ArrayRef<uint8_t> >
285 ELFFile<ELFT>::getSectionContents(const Elf_Shdr *Sec) const {
286 if (Sec->sh_offset + Sec->sh_size > Buf.size())
287 return object_error::parse_failed;
288 const uint8_t *Start = base() + Sec->sh_offset;
289 return makeArrayRef(Start, Sec->sh_size);
292 template <class ELFT>
293 StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
294 return getELFRelocationTypeName(Header->e_machine, Type);
297 template <class ELFT>
298 void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
299 SmallVectorImpl<char> &Result) const {
300 if (!isMipsELF64()) {
301 StringRef Name = getRelocationTypeName(Type);
302 Result.append(Name.begin(), Name.end());
304 // The Mips N64 ABI allows up to three operations to be specified per
305 // relocation record. Unfortunately there's no easy way to test for the
306 // presence of N64 ELFs as they have no special flag that identifies them
307 // as being N64. We can safely assume at the moment that all Mips
308 // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
309 // information to disambiguate between old vs new ABIs.
310 uint8_t Type1 = (Type >> 0) & 0xFF;
311 uint8_t Type2 = (Type >> 8) & 0xFF;
312 uint8_t Type3 = (Type >> 16) & 0xFF;
314 // Concat all three relocation type names.
315 StringRef Name = getRelocationTypeName(Type1);
316 Result.append(Name.begin(), Name.end());
318 Name = getRelocationTypeName(Type2);
319 Result.append(1, '/');
320 Result.append(Name.begin(), Name.end());
322 Name = getRelocationTypeName(Type3);
323 Result.append(1, '/');
324 Result.append(Name.begin(), Name.end());
328 template <class ELFT>
329 template <class RelT>
330 std::pair<const typename ELFFile<ELFT>::Elf_Shdr *,
331 const typename ELFFile<ELFT>::Elf_Sym *>
332 ELFFile<ELFT>::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
334 return std::make_pair(nullptr, nullptr);
335 ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Sec->sh_link);
336 if (std::error_code EC = SymTableOrErr.getError())
337 report_fatal_error(EC.message());
338 const Elf_Shdr *SymTable = *SymTableOrErr;
339 return std::make_pair(
340 SymTable, getEntry<Elf_Sym>(SymTable, Rel->getSymbol(isMips64EL())));
343 template <class ELFT>
344 uint64_t ELFFile<ELFT>::getNumSections() const {
345 assert(Header && "Header not initialized!");
346 if (Header->e_shnum == ELF::SHN_UNDEF && Header->e_shoff > 0) {
347 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
348 return SectionHeaderTable->sh_size;
350 return Header->e_shnum;
353 template <class ELFT>
354 typename ELFFile<ELFT>::uintX_t ELFFile<ELFT>::getStringTableIndex() const {
355 if (Header->e_shnum == ELF::SHN_UNDEF) {
356 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
357 return SectionHeaderTable->sh_link;
358 if (Header->e_shstrndx >= getNumSections())
361 return Header->e_shstrndx;
364 template <class ELFT>
365 ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
367 const uint64_t FileSize = Buf.size();
369 if (sizeof(Elf_Ehdr) > FileSize) {
371 EC = object_error::parse_failed;
375 Header = reinterpret_cast<const Elf_Ehdr *>(base());
377 if (Header->e_shoff == 0)
380 const uint64_t SectionTableOffset = Header->e_shoff;
382 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) {
383 // Section header table goes past end of file!
384 EC = object_error::parse_failed;
388 // The getNumSections() call below depends on SectionHeaderTable being set.
390 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
391 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
393 if (SectionTableOffset + SectionTableSize > FileSize) {
394 // Section table goes past end of file!
395 EC = object_error::parse_failed;
399 // Scan sections for special sections.
401 for (const Elf_Shdr &Sec : sections()) {
402 switch (Sec.sh_type) {
403 case ELF::SHT_SYMTAB_SHNDX:
404 if (SymbolTableSectionHeaderIndex) {
405 // More than one .symtab_shndx!
406 EC = object_error::parse_failed;
409 SymbolTableSectionHeaderIndex = &Sec;
411 case ELF::SHT_SYMTAB: {
412 if (dot_symtab_sec) {
413 // More than one .symtab!
414 EC = object_error::parse_failed;
417 dot_symtab_sec = &Sec;
418 ErrorOr<StringRef> SymtabOrErr = getStringTableForSymtab(Sec);
419 if ((EC = SymtabOrErr.getError()))
421 DotStrtab = *SymtabOrErr;
423 case ELF::SHT_DYNSYM: {
425 // More than one .dynsym!
426 EC = object_error::parse_failed;
435 // Get string table sections.
436 uintX_t StringTableIndex = getStringTableIndex();
437 if (StringTableIndex) {
438 ErrorOr<const Elf_Shdr *> StrTabSecOrErr = getSection(StringTableIndex);
439 if ((EC = StrTabSecOrErr.getError()))
442 ErrorOr<StringRef> SymtabOrErr = getStringTable(*StrTabSecOrErr);
443 if ((EC = SymtabOrErr.getError()))
445 DotShstrtab = *SymtabOrErr;
448 // Build symbol name side-mapping if there is one.
449 if (SymbolTableSectionHeaderIndex) {
450 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
451 SymbolTableSectionHeaderIndex->sh_offset);
452 for (const Elf_Sym &S : symbols()) {
453 if (*ShndxTable != ELF::SHN_UNDEF)
454 ExtendedSymbolTable[&S] = *ShndxTable;
459 EC = std::error_code();
462 template <class ELFT>
463 static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
464 return VAddr < Phdr->p_vaddr;
467 template <class ELFT>
468 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
469 if (Header->e_shentsize != sizeof(Elf_Shdr))
471 "Invalid section header entry size (e_shentsize) in ELF header");
472 return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
475 template <class ELFT>
476 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_end() const {
477 return section_begin() + getNumSections();
480 template <class ELFT>
481 ErrorOr<const typename ELFFile<ELFT>::Elf_Dyn *>
482 ELFFile<ELFT>::dynamic_table_begin(const Elf_Phdr *Phdr) const {
485 assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
486 uintX_t Offset = Phdr->p_offset;
487 if (Offset > Buf.size())
488 return object_error::parse_failed;
489 return reinterpret_cast<const Elf_Dyn *>(base() + Offset);
492 template <class ELFT>
493 ErrorOr<const typename ELFFile<ELFT>::Elf_Dyn *>
494 ELFFile<ELFT>::dynamic_table_end(const Elf_Phdr *Phdr) const {
497 assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
498 uintX_t Size = Phdr->p_filesz;
499 if (Size % sizeof(Elf_Dyn))
500 return object_error::elf_invalid_dynamic_table_size;
501 // FIKME: Check for overflow?
502 uintX_t End = Phdr->p_offset + Size;
503 if (End > Buf.size())
504 return object_error::parse_failed;
505 return reinterpret_cast<const Elf_Dyn *>(base() + End);
508 template <class ELFT>
509 template <typename T>
510 const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
511 ErrorOr<const Elf_Shdr *> Sec = getSection(Section);
512 if (std::error_code EC = Sec.getError())
513 report_fatal_error(EC.message());
514 return getEntry<T>(*Sec, Entry);
517 template <class ELFT>
518 template <typename T>
519 const T *ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
520 uint32_t Entry) const {
521 return reinterpret_cast<const T *>(base() + Section->sh_offset +
522 (Entry * Section->sh_entsize));
525 template <class ELFT>
526 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
527 ELFFile<ELFT>::getSection(uint32_t Index) const {
528 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
529 if (Index >= getNumSections())
530 return object_error::invalid_section_index;
532 return reinterpret_cast<const Elf_Shdr *>(
533 reinterpret_cast<const char *>(SectionHeaderTable) +
534 (Index * Header->e_shentsize));
537 template <class ELFT>
539 ELFFile<ELFT>::getStringTable(const Elf_Shdr *Section) const {
540 if (Section->sh_type != ELF::SHT_STRTAB)
541 return object_error::parse_failed;
542 uint64_t Offset = Section->sh_offset;
543 uint64_t Size = Section->sh_size;
544 if (Offset + Size > Buf.size())
545 return object_error::parse_failed;
546 StringRef Data((const char *)base() + Section->sh_offset, Size);
547 if (Data[Size - 1] != '\0')
548 return object_error::string_table_non_null_end;
552 template <class ELFT>
554 ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
555 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
556 return object_error::parse_failed;
557 ErrorOr<const Elf_Shdr *> SectionOrErr = getSection(Sec.sh_link);
558 if (std::error_code EC = SectionOrErr.getError())
560 return getStringTable(*SectionOrErr);
563 template <class ELFT>
565 ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
566 uint32_t Offset = Section->sh_name;
569 if (Offset >= DotShstrtab.size())
570 return object_error::parse_failed;
571 return StringRef(DotShstrtab.data() + Offset);
574 /// This function returns the hash value for a symbol in the .dynsym section
575 /// Name of the API remains consistent as specified in the libelf
576 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
577 static inline unsigned elf_hash(StringRef &symbolName) {
579 for (unsigned i = 0, j = symbolName.size(); i < j; i++) {
580 h = (h << 4) + symbolName[i];
588 } // end namespace object
589 } // end namespace llvm