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/SmallVector.h"
18 #include "llvm/Object/ELFTypes.h"
19 #include "llvm/Support/MemoryBuffer.h"
24 StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
26 // Subclasses of ELFFile may need this for template instantiation
27 inline std::pair<unsigned char, unsigned char>
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]);
39 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
40 typedef typename std::conditional<ELFT::Is64Bits,
41 uint64_t, uint32_t>::type uintX_t;
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 iterator_range<const Elf_Dyn *> Elf_Dyn_Range;
57 typedef iterator_range<const Elf_Shdr *> Elf_Shdr_Range;
58 typedef iterator_range<const Elf_Sym *> Elf_Sym_Range;
60 const uint8_t *base() const {
61 return reinterpret_cast<const uint8_t *>(Buf.data());
68 const Elf_Ehdr *Header;
69 const Elf_Shdr *SectionHeaderTable = nullptr;
70 StringRef DotShstrtab; // Section header string table.
74 const T *getEntry(uint32_t Section, uint32_t Entry) const;
76 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
78 ErrorOr<StringRef> getStringTable(const Elf_Shdr *Section) const;
79 ErrorOr<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
81 ErrorOr<ArrayRef<Elf_Word>> getSHNDXTable(const Elf_Shdr &Section) const;
83 void VerifyStrTab(const Elf_Shdr *sh) const;
85 StringRef getRelocationTypeName(uint32_t Type) const;
86 void getRelocationTypeName(uint32_t Type,
87 SmallVectorImpl<char> &Result) const;
89 /// \brief Get the symbol table section and symbol for a given relocation.
91 std::pair<const Elf_Shdr *, const Elf_Sym *>
92 getRelocationSymbol(const Elf_Shdr *RelSec, const RelT *Rel) const;
94 ELFFile(StringRef Object, std::error_code &EC);
96 bool isMipsELF64() const {
97 return Header->e_machine == ELF::EM_MIPS &&
98 Header->getFileClass() == ELF::ELFCLASS64;
101 bool isMips64EL() const {
102 return Header->e_machine == ELF::EM_MIPS &&
103 Header->getFileClass() == ELF::ELFCLASS64 &&
104 Header->getDataEncoding() == ELF::ELFDATA2LSB;
107 ErrorOr<const Elf_Dyn *> dynamic_table_begin(const Elf_Phdr *Phdr) const;
108 ErrorOr<const Elf_Dyn *> dynamic_table_end(const Elf_Phdr *Phdr) const;
109 ErrorOr<Elf_Dyn_Range> dynamic_table(const Elf_Phdr *Phdr) const {
110 ErrorOr<const Elf_Dyn *> Begin = dynamic_table_begin(Phdr);
111 if (std::error_code EC = Begin.getError())
113 ErrorOr<const Elf_Dyn *> End = dynamic_table_end(Phdr);
114 if (std::error_code EC = End.getError())
116 return make_range(*Begin, *End);
119 const Elf_Shdr *section_begin() const;
120 const Elf_Shdr *section_end() const;
121 Elf_Shdr_Range sections() const {
122 return make_range(section_begin(), section_end());
125 const Elf_Sym *symbol_begin(const Elf_Shdr *Sec) const {
128 if (Sec->sh_entsize != sizeof(Elf_Sym))
129 report_fatal_error("Invalid symbol size");
130 return reinterpret_cast<const Elf_Sym *>(base() + Sec->sh_offset);
132 const Elf_Sym *symbol_end(const Elf_Shdr *Sec) const {
135 uint64_t Size = Sec->sh_size;
136 if (Size % sizeof(Elf_Sym))
137 report_fatal_error("Invalid symbol table size");
138 return symbol_begin(Sec) + Size / sizeof(Elf_Sym);
140 Elf_Sym_Range symbols(const Elf_Shdr *Sec) const {
141 return make_range(symbol_begin(Sec), symbol_end(Sec));
144 typedef iterator_range<const Elf_Rela *> Elf_Rela_Range;
146 const Elf_Rela *rela_begin(const Elf_Shdr *sec) const {
147 if (sec->sh_entsize != sizeof(Elf_Rela))
148 report_fatal_error("Invalid relocation entry size");
149 return reinterpret_cast<const Elf_Rela *>(base() + sec->sh_offset);
152 const Elf_Rela *rela_end(const Elf_Shdr *sec) const {
153 uint64_t Size = sec->sh_size;
154 if (Size % sizeof(Elf_Rela))
155 report_fatal_error("Invalid relocation table size");
156 return rela_begin(sec) + Size / sizeof(Elf_Rela);
159 Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
160 return make_range(rela_begin(Sec), rela_end(Sec));
163 const Elf_Rel *rel_begin(const Elf_Shdr *sec) const {
164 if (sec->sh_entsize != sizeof(Elf_Rel))
165 report_fatal_error("Invalid relocation entry size");
166 return reinterpret_cast<const Elf_Rel *>(base() + sec->sh_offset);
169 const Elf_Rel *rel_end(const Elf_Shdr *sec) const {
170 uint64_t Size = sec->sh_size;
171 if (Size % sizeof(Elf_Rel))
172 report_fatal_error("Invalid relocation table size");
173 return rel_begin(sec) + Size / sizeof(Elf_Rel);
176 typedef iterator_range<const Elf_Rel *> Elf_Rel_Range;
177 Elf_Rel_Range rels(const Elf_Shdr *Sec) const {
178 return make_range(rel_begin(Sec), rel_end(Sec));
181 /// \brief Iterate over program header table.
182 const Elf_Phdr *program_header_begin() const {
183 if (Header->e_phnum && Header->e_phentsize != sizeof(Elf_Phdr))
184 report_fatal_error("Invalid program header size");
185 return reinterpret_cast<const Elf_Phdr *>(base() + Header->e_phoff);
188 const Elf_Phdr *program_header_end() const {
189 return program_header_begin() + Header->e_phnum;
192 typedef iterator_range<const Elf_Phdr *> Elf_Phdr_Range;
194 const Elf_Phdr_Range program_headers() const {
195 return make_range(program_header_begin(), program_header_end());
198 uint64_t getNumSections() const;
199 uintX_t getStringTableIndex() const;
201 getExtendedSymbolTableIndex(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
202 ArrayRef<Elf_Word> ShndxTable) const;
203 const Elf_Ehdr *getHeader() const { return Header; }
204 ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *Sym,
205 const Elf_Shdr *SymTab,
206 ArrayRef<Elf_Word> ShndxTable) const;
207 ErrorOr<const Elf_Shdr *> getSection(uint32_t Index) const;
209 const Elf_Sym *getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
210 return &*(symbol_begin(Sec) + Index);
213 ErrorOr<StringRef> getSectionName(const Elf_Shdr *Section) const;
214 ErrorOr<ArrayRef<uint8_t> > getSectionContents(const Elf_Shdr *Sec) const;
217 typedef ELFFile<ELFType<support::little, false>> ELF32LEFile;
218 typedef ELFFile<ELFType<support::little, true>> ELF64LEFile;
219 typedef ELFFile<ELFType<support::big, false>> ELF32BEFile;
220 typedef ELFFile<ELFType<support::big, true>> ELF64BEFile;
222 template <class ELFT>
223 ELF::Elf64_Word ELFFile<ELFT>::getExtendedSymbolTableIndex(
224 const Elf_Sym *Sym, const Elf_Shdr *SymTab,
225 ArrayRef<Elf_Word> ShndxTable) const {
226 assert(Sym->st_shndx == ELF::SHN_XINDEX);
227 unsigned Index = Sym - symbol_begin(SymTab);
229 // FIXME: error checking
230 return ShndxTable[Index];
233 template <class ELFT>
234 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
235 ELFFile<ELFT>::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
236 ArrayRef<Elf_Word> ShndxTable) const {
237 uint32_t Index = Sym->st_shndx;
238 if (Index == ELF::SHN_XINDEX)
239 return getSection(getExtendedSymbolTableIndex(Sym, SymTab, ShndxTable));
241 if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
243 return getSection(Sym->st_shndx);
246 template <class ELFT>
247 ErrorOr<ArrayRef<uint8_t> >
248 ELFFile<ELFT>::getSectionContents(const Elf_Shdr *Sec) const {
249 if (Sec->sh_offset + Sec->sh_size > Buf.size())
250 return object_error::parse_failed;
251 const uint8_t *Start = base() + Sec->sh_offset;
252 return makeArrayRef(Start, Sec->sh_size);
255 template <class ELFT>
256 StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
257 return getELFRelocationTypeName(Header->e_machine, Type);
260 template <class ELFT>
261 void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
262 SmallVectorImpl<char> &Result) const {
263 if (!isMipsELF64()) {
264 StringRef Name = getRelocationTypeName(Type);
265 Result.append(Name.begin(), Name.end());
267 // The Mips N64 ABI allows up to three operations to be specified per
268 // relocation record. Unfortunately there's no easy way to test for the
269 // presence of N64 ELFs as they have no special flag that identifies them
270 // as being N64. We can safely assume at the moment that all Mips
271 // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
272 // information to disambiguate between old vs new ABIs.
273 uint8_t Type1 = (Type >> 0) & 0xFF;
274 uint8_t Type2 = (Type >> 8) & 0xFF;
275 uint8_t Type3 = (Type >> 16) & 0xFF;
277 // Concat all three relocation type names.
278 StringRef Name = getRelocationTypeName(Type1);
279 Result.append(Name.begin(), Name.end());
281 Name = getRelocationTypeName(Type2);
282 Result.append(1, '/');
283 Result.append(Name.begin(), Name.end());
285 Name = getRelocationTypeName(Type3);
286 Result.append(1, '/');
287 Result.append(Name.begin(), Name.end());
291 template <class ELFT>
292 template <class RelT>
293 std::pair<const typename ELFFile<ELFT>::Elf_Shdr *,
294 const typename ELFFile<ELFT>::Elf_Sym *>
295 ELFFile<ELFT>::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
297 return std::make_pair(nullptr, nullptr);
298 ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Sec->sh_link);
299 if (std::error_code EC = SymTableOrErr.getError())
300 report_fatal_error(EC.message());
301 const Elf_Shdr *SymTable = *SymTableOrErr;
302 return std::make_pair(
303 SymTable, getEntry<Elf_Sym>(SymTable, Rel->getSymbol(isMips64EL())));
306 template <class ELFT>
307 uint64_t ELFFile<ELFT>::getNumSections() const {
308 assert(Header && "Header not initialized!");
309 if (Header->e_shnum == ELF::SHN_UNDEF && Header->e_shoff > 0) {
310 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
311 return SectionHeaderTable->sh_size;
313 return Header->e_shnum;
316 template <class ELFT>
317 typename ELFFile<ELFT>::uintX_t ELFFile<ELFT>::getStringTableIndex() const {
318 if (Header->e_shnum == ELF::SHN_UNDEF) {
319 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
320 return SectionHeaderTable->sh_link;
321 if (Header->e_shstrndx >= getNumSections())
324 return Header->e_shstrndx;
327 template <class ELFT>
328 ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
330 const uint64_t FileSize = Buf.size();
332 if (sizeof(Elf_Ehdr) > FileSize) {
334 EC = object_error::parse_failed;
338 Header = reinterpret_cast<const Elf_Ehdr *>(base());
340 if (Header->e_shoff == 0)
343 const uint64_t SectionTableOffset = Header->e_shoff;
345 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) {
346 // Section header table goes past end of file!
347 EC = object_error::parse_failed;
351 // The getNumSections() call below depends on SectionHeaderTable being set.
353 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
354 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
356 if (SectionTableOffset + SectionTableSize > FileSize) {
357 // Section table goes past end of file!
358 EC = object_error::parse_failed;
362 // Get string table sections.
363 uintX_t StringTableIndex = getStringTableIndex();
364 if (StringTableIndex) {
365 ErrorOr<const Elf_Shdr *> StrTabSecOrErr = getSection(StringTableIndex);
366 if ((EC = StrTabSecOrErr.getError()))
369 ErrorOr<StringRef> StringTableOrErr = getStringTable(*StrTabSecOrErr);
370 if ((EC = StringTableOrErr.getError()))
372 DotShstrtab = *StringTableOrErr;
375 EC = std::error_code();
378 template <class ELFT>
379 static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
380 return VAddr < Phdr->p_vaddr;
383 template <class ELFT>
384 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
385 if (Header->e_shentsize != sizeof(Elf_Shdr))
387 "Invalid section header entry size (e_shentsize) in ELF header");
388 return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
391 template <class ELFT>
392 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_end() const {
393 return section_begin() + getNumSections();
396 template <class ELFT>
397 ErrorOr<const typename ELFFile<ELFT>::Elf_Dyn *>
398 ELFFile<ELFT>::dynamic_table_begin(const Elf_Phdr *Phdr) const {
401 assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
402 uintX_t Offset = Phdr->p_offset;
403 if (Offset > Buf.size())
404 return object_error::parse_failed;
405 return reinterpret_cast<const Elf_Dyn *>(base() + Offset);
408 template <class ELFT>
409 ErrorOr<const typename ELFFile<ELFT>::Elf_Dyn *>
410 ELFFile<ELFT>::dynamic_table_end(const Elf_Phdr *Phdr) const {
413 assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
414 uintX_t Size = Phdr->p_filesz;
415 if (Size % sizeof(Elf_Dyn))
416 return object_error::elf_invalid_dynamic_table_size;
417 // FIKME: Check for overflow?
418 uintX_t End = Phdr->p_offset + Size;
419 if (End > Buf.size())
420 return object_error::parse_failed;
421 return reinterpret_cast<const Elf_Dyn *>(base() + End);
424 template <class ELFT>
425 template <typename T>
426 const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
427 ErrorOr<const Elf_Shdr *> Sec = getSection(Section);
428 if (std::error_code EC = Sec.getError())
429 report_fatal_error(EC.message());
430 return getEntry<T>(*Sec, Entry);
433 template <class ELFT>
434 template <typename T>
435 const T *ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
436 uint32_t Entry) const {
437 return reinterpret_cast<const T *>(base() + Section->sh_offset +
438 (Entry * Section->sh_entsize));
441 template <class ELFT>
442 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
443 ELFFile<ELFT>::getSection(uint32_t Index) const {
444 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
445 if (Index >= getNumSections())
446 return object_error::invalid_section_index;
448 return reinterpret_cast<const Elf_Shdr *>(
449 reinterpret_cast<const char *>(SectionHeaderTable) +
450 (Index * Header->e_shentsize));
453 template <class ELFT>
455 ELFFile<ELFT>::getStringTable(const Elf_Shdr *Section) const {
456 if (Section->sh_type != ELF::SHT_STRTAB)
457 return object_error::parse_failed;
458 uint64_t Offset = Section->sh_offset;
459 uint64_t Size = Section->sh_size;
460 if (Offset + Size > Buf.size())
461 return object_error::parse_failed;
462 StringRef Data((const char *)base() + Section->sh_offset, Size);
463 if (Data[Size - 1] != '\0')
464 return object_error::string_table_non_null_end;
468 template <class ELFT>
469 ErrorOr<ArrayRef<typename ELFFile<ELFT>::Elf_Word>>
470 ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section) const {
471 assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
472 const Elf_Word *ShndxTableBegin =
473 reinterpret_cast<const Elf_Word *>(base() + Section.sh_offset);
474 uintX_t Size = Section.sh_offset;
475 if (Size % sizeof(uintX_t))
476 return object_error::parse_failed;
477 const Elf_Word *ShndxTableEnd = ShndxTableBegin + Size / sizeof(uintX_t);
478 if (reinterpret_cast<const char *>(ShndxTableEnd) > Buf.end())
479 return object_error::parse_failed;
480 return ArrayRef<Elf_Word>(ShndxTableBegin, ShndxTableEnd);
483 template <class ELFT>
485 ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
486 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
487 return object_error::parse_failed;
488 ErrorOr<const Elf_Shdr *> SectionOrErr = getSection(Sec.sh_link);
489 if (std::error_code EC = SectionOrErr.getError())
491 return getStringTable(*SectionOrErr);
494 template <class ELFT>
496 ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
497 uint32_t Offset = Section->sh_name;
500 if (Offset >= DotShstrtab.size())
501 return object_error::parse_failed;
502 return StringRef(DotShstrtab.data() + Offset);
505 /// This function returns the hash value for a symbol in the .dynsym section
506 /// Name of the API remains consistent as specified in the libelf
507 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
508 static inline unsigned elf_hash(StringRef &symbolName) {
510 for (unsigned i = 0, j = symbolName.size(); i < j; i++) {
511 h = (h << 4) + symbolName[i];
519 } // end namespace object
520 } // end namespace llvm