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/PointerIntPair.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Object/ELFTypes.h"
23 #include "llvm/Object/Error.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/ErrorOr.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/raw_ostream.h"
38 StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
40 // Subclasses of ELFFile may need this for template instantiation
41 inline std::pair<unsigned char, unsigned char>
42 getElfArchType(StringRef Object) {
43 if (Object.size() < ELF::EI_NIDENT)
44 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,
45 (uint8_t)ELF::ELFDATANONE);
46 return std::make_pair((uint8_t)Object[ELF::EI_CLASS],
47 (uint8_t)Object[ELF::EI_DATA]);
53 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
54 typedef typename std::conditional<ELFT::Is64Bits,
55 uint64_t, uint32_t>::type uintX_t;
57 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
58 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
59 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
60 typedef Elf_Dyn_Impl<ELFT> Elf_Dyn;
61 typedef Elf_Phdr_Impl<ELFT> Elf_Phdr;
62 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
63 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
64 typedef Elf_Verdef_Impl<ELFT> Elf_Verdef;
65 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
66 typedef Elf_Verneed_Impl<ELFT> Elf_Verneed;
67 typedef Elf_Vernaux_Impl<ELFT> Elf_Vernaux;
68 typedef Elf_Versym_Impl<ELFT> Elf_Versym;
69 typedef Elf_Hash_Impl<ELFT> Elf_Hash;
70 typedef iterator_range<const Elf_Dyn *> Elf_Dyn_Range;
71 typedef iterator_range<const Elf_Shdr *> Elf_Shdr_Range;
73 /// \brief Archive files are 2 byte aligned, so we need this for
74 /// PointerIntPair to work.
76 class ArchivePointerTypeTraits {
78 static inline const void *getAsVoidPointer(T *P) { return P; }
79 static inline T *getFromVoidPointer(const void *P) {
80 return static_cast<T *>(P);
82 enum { NumLowBitsAvailable = 1 };
85 typedef iterator_range<const Elf_Sym *> Elf_Sym_Range;
87 const uint8_t *base() const {
88 return reinterpret_cast<const uint8_t *>(Buf.data());
92 typedef SmallVector<const Elf_Shdr *, 2> Sections_t;
96 const Elf_Ehdr *Header;
97 const Elf_Shdr *SectionHeaderTable = nullptr;
98 StringRef DotShstrtab; // Section header string table.
99 StringRef DotStrtab; // Symbol header string table.
100 const Elf_Shdr *dot_symtab_sec = nullptr; // Symbol table section.
101 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
103 const Elf_Shdr *SymbolTableSectionHeaderIndex = nullptr;
107 const T *getEntry(uint32_t Section, uint32_t Entry) const;
108 template <typename T>
109 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
111 const Elf_Shdr *getDotSymtabSec() const { return dot_symtab_sec; }
112 const Elf_Shdr *getDotDynSymSec() const { return DotDynSymSec; }
114 ErrorOr<StringRef> getStringTable(const Elf_Shdr *Section) const;
115 ErrorOr<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
117 void VerifyStrTab(const Elf_Shdr *sh) const;
119 StringRef getRelocationTypeName(uint32_t Type) const;
120 void getRelocationTypeName(uint32_t Type,
121 SmallVectorImpl<char> &Result) const;
123 /// \brief Get the symbol table section and symbol for a given relocation.
124 template <class RelT>
125 std::pair<const Elf_Shdr *, const Elf_Sym *>
126 getRelocationSymbol(const Elf_Shdr *RelSec, const RelT *Rel) const;
128 ELFFile(StringRef Object, std::error_code &EC);
130 bool isMipsELF64() const {
131 return Header->e_machine == ELF::EM_MIPS &&
132 Header->getFileClass() == ELF::ELFCLASS64;
135 bool isMips64EL() const {
136 return Header->e_machine == ELF::EM_MIPS &&
137 Header->getFileClass() == ELF::ELFCLASS64 &&
138 Header->getDataEncoding() == ELF::ELFDATA2LSB;
141 ErrorOr<const Elf_Dyn *> dynamic_table_begin(const Elf_Phdr *Phdr) const;
142 ErrorOr<const Elf_Dyn *> dynamic_table_end(const Elf_Phdr *Phdr) const;
143 ErrorOr<Elf_Dyn_Range> dynamic_table(const Elf_Phdr *Phdr) const {
144 ErrorOr<const Elf_Dyn *> Begin = dynamic_table_begin(Phdr);
145 if (std::error_code EC = Begin.getError())
147 ErrorOr<const Elf_Dyn *> End = dynamic_table_end(Phdr);
148 if (std::error_code EC = End.getError())
150 return make_range(*Begin, *End);
153 const Elf_Shdr *section_begin() const;
154 const Elf_Shdr *section_end() const;
155 Elf_Shdr_Range sections() const {
156 return make_range(section_begin(), section_end());
159 const Elf_Sym *symbol_begin(const Elf_Shdr *Sec) const {
162 if (Sec->sh_entsize != sizeof(Elf_Sym))
163 report_fatal_error("Invalid symbol size");
164 return reinterpret_cast<const Elf_Sym *>(base() + Sec->sh_offset);
166 const Elf_Sym *symbol_end(const Elf_Shdr *Sec) const {
169 uint64_t Size = Sec->sh_size;
170 if (Size % sizeof(Elf_Sym))
171 report_fatal_error("Invalid symbol table size");
172 return symbol_begin(Sec) + Size / sizeof(Elf_Sym);
174 Elf_Sym_Range symbols(const Elf_Shdr *Sec) const {
175 return make_range(symbol_begin(Sec), symbol_end(Sec));
178 const Elf_Sym *symbol_begin() const { return symbol_begin(dot_symtab_sec); }
179 const Elf_Sym *symbol_end() const { return symbol_end(dot_symtab_sec); }
180 Elf_Sym_Range symbols() const { return symbols(dot_symtab_sec); }
182 const Elf_Sym *dynamic_symbol_begin() const {
183 return symbol_begin(DotDynSymSec);
185 const Elf_Sym *dynamic_symbol_end() const { return symbol_end(DotDynSymSec); }
186 Elf_Sym_Range dynamic_symbols() const { return symbols(DotDynSymSec); }
188 typedef iterator_range<const Elf_Rela *> Elf_Rela_Range;
190 const Elf_Rela *rela_begin(const Elf_Shdr *sec) const {
191 if (sec->sh_entsize != sizeof(Elf_Rela))
192 report_fatal_error("Invalid relocation entry size");
193 return reinterpret_cast<const Elf_Rela *>(base() + sec->sh_offset);
196 const Elf_Rela *rela_end(const Elf_Shdr *sec) const {
197 uint64_t Size = sec->sh_size;
198 if (Size % sizeof(Elf_Rela))
199 report_fatal_error("Invalid relocation table size");
200 return rela_begin(sec) + Size / sizeof(Elf_Rela);
203 Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
204 return make_range(rela_begin(Sec), rela_end(Sec));
207 const Elf_Rel *rel_begin(const Elf_Shdr *sec) const {
208 if (sec->sh_entsize != sizeof(Elf_Rel))
209 report_fatal_error("Invalid relocation entry size");
210 return reinterpret_cast<const Elf_Rel *>(base() + sec->sh_offset);
213 const Elf_Rel *rel_end(const Elf_Shdr *sec) const {
214 uint64_t Size = sec->sh_size;
215 if (Size % sizeof(Elf_Rel))
216 report_fatal_error("Invalid relocation table size");
217 return rel_begin(sec) + Size / sizeof(Elf_Rel);
220 typedef iterator_range<const Elf_Rel *> Elf_Rel_Range;
221 Elf_Rel_Range rels(const Elf_Shdr *Sec) const {
222 return make_range(rel_begin(Sec), rel_end(Sec));
225 /// \brief Iterate over program header table.
226 const Elf_Phdr *program_header_begin() const {
227 if (Header->e_phnum && Header->e_phentsize != sizeof(Elf_Phdr))
228 report_fatal_error("Invalid program header size");
229 return reinterpret_cast<const Elf_Phdr *>(base() + Header->e_phoff);
232 const Elf_Phdr *program_header_end() const {
233 return program_header_begin() + Header->e_phnum;
236 typedef iterator_range<const Elf_Phdr *> Elf_Phdr_Range;
238 const Elf_Phdr_Range program_headers() const {
239 return make_range(program_header_begin(), program_header_end());
242 uint64_t getNumSections() const;
243 uintX_t getStringTableIndex() const;
244 ELF::Elf64_Word getExtendedSymbolTableIndex(const Elf_Sym *symb) const;
245 const Elf_Ehdr *getHeader() const { return Header; }
246 ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *symb) const;
247 ErrorOr<const Elf_Shdr *> getSection(uint32_t Index) const;
249 const Elf_Sym *getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
250 return &*(symbol_begin(Sec) + Index);
253 ErrorOr<StringRef> getSectionName(const Elf_Shdr *Section) const;
254 ErrorOr<ArrayRef<uint8_t> > getSectionContents(const Elf_Shdr *Sec) const;
257 typedef ELFFile<ELFType<support::little, false>> ELF32LEFile;
258 typedef ELFFile<ELFType<support::little, true>> ELF64LEFile;
259 typedef ELFFile<ELFType<support::big, false>> ELF32BEFile;
260 typedef ELFFile<ELFType<support::big, true>> ELF64BEFile;
262 template <class ELFT>
264 ELFFile<ELFT>::getExtendedSymbolTableIndex(const Elf_Sym *Sym) const {
265 assert(Sym->st_shndx == ELF::SHN_XINDEX);
266 unsigned Index = Sym - symbol_begin();
268 // FIXME: error checking
269 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word *>(
270 base() + SymbolTableSectionHeaderIndex->sh_offset);
271 return ShndxTable[Index];
274 template <class ELFT>
275 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
276 ELFFile<ELFT>::getSection(const Elf_Sym *symb) const {
277 uint32_t Index = symb->st_shndx;
278 if (Index == ELF::SHN_XINDEX)
279 return getSection(getExtendedSymbolTableIndex(symb));
280 if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
282 return getSection(symb->st_shndx);
285 template <class ELFT>
286 ErrorOr<ArrayRef<uint8_t> >
287 ELFFile<ELFT>::getSectionContents(const Elf_Shdr *Sec) const {
288 if (Sec->sh_offset + Sec->sh_size > Buf.size())
289 return object_error::parse_failed;
290 const uint8_t *Start = base() + Sec->sh_offset;
291 return makeArrayRef(Start, Sec->sh_size);
294 template <class ELFT>
295 StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
296 return getELFRelocationTypeName(Header->e_machine, Type);
299 template <class ELFT>
300 void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
301 SmallVectorImpl<char> &Result) const {
302 if (!isMipsELF64()) {
303 StringRef Name = getRelocationTypeName(Type);
304 Result.append(Name.begin(), Name.end());
306 // The Mips N64 ABI allows up to three operations to be specified per
307 // relocation record. Unfortunately there's no easy way to test for the
308 // presence of N64 ELFs as they have no special flag that identifies them
309 // as being N64. We can safely assume at the moment that all Mips
310 // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
311 // information to disambiguate between old vs new ABIs.
312 uint8_t Type1 = (Type >> 0) & 0xFF;
313 uint8_t Type2 = (Type >> 8) & 0xFF;
314 uint8_t Type3 = (Type >> 16) & 0xFF;
316 // Concat all three relocation type names.
317 StringRef Name = getRelocationTypeName(Type1);
318 Result.append(Name.begin(), Name.end());
320 Name = getRelocationTypeName(Type2);
321 Result.append(1, '/');
322 Result.append(Name.begin(), Name.end());
324 Name = getRelocationTypeName(Type3);
325 Result.append(1, '/');
326 Result.append(Name.begin(), Name.end());
330 template <class ELFT>
331 template <class RelT>
332 std::pair<const typename ELFFile<ELFT>::Elf_Shdr *,
333 const typename ELFFile<ELFT>::Elf_Sym *>
334 ELFFile<ELFT>::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
336 return std::make_pair(nullptr, nullptr);
337 ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Sec->sh_link);
338 if (std::error_code EC = SymTableOrErr.getError())
339 report_fatal_error(EC.message());
340 const Elf_Shdr *SymTable = *SymTableOrErr;
341 return std::make_pair(
342 SymTable, getEntry<Elf_Sym>(SymTable, Rel->getSymbol(isMips64EL())));
345 template <class ELFT>
346 uint64_t ELFFile<ELFT>::getNumSections() const {
347 assert(Header && "Header not initialized!");
348 if (Header->e_shnum == ELF::SHN_UNDEF && Header->e_shoff > 0) {
349 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
350 return SectionHeaderTable->sh_size;
352 return Header->e_shnum;
355 template <class ELFT>
356 typename ELFFile<ELFT>::uintX_t ELFFile<ELFT>::getStringTableIndex() const {
357 if (Header->e_shnum == ELF::SHN_UNDEF) {
358 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
359 return SectionHeaderTable->sh_link;
360 if (Header->e_shstrndx >= getNumSections())
363 return Header->e_shstrndx;
366 template <class ELFT>
367 ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
369 const uint64_t FileSize = Buf.size();
371 if (sizeof(Elf_Ehdr) > FileSize) {
373 EC = object_error::parse_failed;
377 Header = reinterpret_cast<const Elf_Ehdr *>(base());
379 if (Header->e_shoff == 0)
382 const uint64_t SectionTableOffset = Header->e_shoff;
384 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) {
385 // Section header table goes past end of file!
386 EC = object_error::parse_failed;
390 // The getNumSections() call below depends on SectionHeaderTable being set.
392 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
393 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
395 if (SectionTableOffset + SectionTableSize > FileSize) {
396 // Section table goes past end of file!
397 EC = object_error::parse_failed;
401 // Scan sections for special sections.
403 for (const Elf_Shdr &Sec : sections()) {
404 switch (Sec.sh_type) {
405 case ELF::SHT_SYMTAB_SHNDX:
406 if (SymbolTableSectionHeaderIndex) {
407 // More than one .symtab_shndx!
408 EC = object_error::parse_failed;
411 SymbolTableSectionHeaderIndex = &Sec;
413 case ELF::SHT_SYMTAB: {
414 if (dot_symtab_sec) {
415 // More than one .symtab!
416 EC = object_error::parse_failed;
419 dot_symtab_sec = &Sec;
420 ErrorOr<StringRef> SymtabOrErr = getStringTableForSymtab(Sec);
421 if ((EC = SymtabOrErr.getError()))
423 DotStrtab = *SymtabOrErr;
425 case ELF::SHT_DYNSYM: {
427 // More than one .dynsym!
428 EC = object_error::parse_failed;
437 // Get string table sections.
438 uintX_t StringTableIndex = getStringTableIndex();
439 if (StringTableIndex) {
440 ErrorOr<const Elf_Shdr *> StrTabSecOrErr = getSection(StringTableIndex);
441 if ((EC = StrTabSecOrErr.getError()))
444 ErrorOr<StringRef> SymtabOrErr = getStringTable(*StrTabSecOrErr);
445 if ((EC = SymtabOrErr.getError()))
447 DotShstrtab = *SymtabOrErr;
450 EC = std::error_code();
453 template <class ELFT>
454 static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
455 return VAddr < Phdr->p_vaddr;
458 template <class ELFT>
459 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
460 if (Header->e_shentsize != sizeof(Elf_Shdr))
462 "Invalid section header entry size (e_shentsize) in ELF header");
463 return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
466 template <class ELFT>
467 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_end() const {
468 return section_begin() + getNumSections();
471 template <class ELFT>
472 ErrorOr<const typename ELFFile<ELFT>::Elf_Dyn *>
473 ELFFile<ELFT>::dynamic_table_begin(const Elf_Phdr *Phdr) const {
476 assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
477 uintX_t Offset = Phdr->p_offset;
478 if (Offset > Buf.size())
479 return object_error::parse_failed;
480 return reinterpret_cast<const Elf_Dyn *>(base() + Offset);
483 template <class ELFT>
484 ErrorOr<const typename ELFFile<ELFT>::Elf_Dyn *>
485 ELFFile<ELFT>::dynamic_table_end(const Elf_Phdr *Phdr) const {
488 assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
489 uintX_t Size = Phdr->p_filesz;
490 if (Size % sizeof(Elf_Dyn))
491 return object_error::elf_invalid_dynamic_table_size;
492 // FIKME: Check for overflow?
493 uintX_t End = Phdr->p_offset + Size;
494 if (End > Buf.size())
495 return object_error::parse_failed;
496 return reinterpret_cast<const Elf_Dyn *>(base() + End);
499 template <class ELFT>
500 template <typename T>
501 const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
502 ErrorOr<const Elf_Shdr *> Sec = getSection(Section);
503 if (std::error_code EC = Sec.getError())
504 report_fatal_error(EC.message());
505 return getEntry<T>(*Sec, Entry);
508 template <class ELFT>
509 template <typename T>
510 const T *ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
511 uint32_t Entry) const {
512 return reinterpret_cast<const T *>(base() + Section->sh_offset +
513 (Entry * Section->sh_entsize));
516 template <class ELFT>
517 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
518 ELFFile<ELFT>::getSection(uint32_t Index) const {
519 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
520 if (Index >= getNumSections())
521 return object_error::invalid_section_index;
523 return reinterpret_cast<const Elf_Shdr *>(
524 reinterpret_cast<const char *>(SectionHeaderTable) +
525 (Index * Header->e_shentsize));
528 template <class ELFT>
530 ELFFile<ELFT>::getStringTable(const Elf_Shdr *Section) const {
531 if (Section->sh_type != ELF::SHT_STRTAB)
532 return object_error::parse_failed;
533 uint64_t Offset = Section->sh_offset;
534 uint64_t Size = Section->sh_size;
535 if (Offset + Size > Buf.size())
536 return object_error::parse_failed;
537 StringRef Data((const char *)base() + Section->sh_offset, Size);
538 if (Data[Size - 1] != '\0')
539 return object_error::string_table_non_null_end;
543 template <class ELFT>
545 ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
546 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
547 return object_error::parse_failed;
548 ErrorOr<const Elf_Shdr *> SectionOrErr = getSection(Sec.sh_link);
549 if (std::error_code EC = SectionOrErr.getError())
551 return getStringTable(*SectionOrErr);
554 template <class ELFT>
556 ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
557 uint32_t Offset = Section->sh_name;
560 if (Offset >= DotShstrtab.size())
561 return object_error::parse_failed;
562 return StringRef(DotShstrtab.data() + Offset);
565 /// This function returns the hash value for a symbol in the .dynsym section
566 /// Name of the API remains consistent as specified in the libelf
567 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
568 static inline unsigned elf_hash(StringRef &symbolName) {
570 for (unsigned i = 0, j = symbolName.size(); i < j; i++) {
571 h = (h << 4) + symbolName[i];
579 } // end namespace object
580 } // end namespace llvm