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 const Elf_Sym *getRelocationSymbol(const RelT *Rel,
92 const Elf_Shdr *SymTab) 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;
200 uint32_t getExtendedSymbolTableIndex(const Elf_Sym *Sym,
201 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 uint32_t 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 // The size of the table was checked in getSHNDXTable.
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 const typename ELFFile<ELFT>::Elf_Sym *
294 ELFFile<ELFT>::getRelocationSymbol(const RelT *Rel,
295 const Elf_Shdr *SymTab) const {
296 uint32_t Index = Rel->getSymbol(isMips64EL());
299 return getEntry<Elf_Sym>(SymTab, Index);
302 template <class ELFT>
303 uint64_t ELFFile<ELFT>::getNumSections() const {
304 assert(Header && "Header not initialized!");
305 if (Header->e_shnum == ELF::SHN_UNDEF && Header->e_shoff > 0) {
306 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
307 return SectionHeaderTable->sh_size;
309 return Header->e_shnum;
312 template <class ELFT>
313 typename ELFFile<ELFT>::uintX_t ELFFile<ELFT>::getStringTableIndex() const {
314 if (Header->e_shnum == ELF::SHN_UNDEF) {
315 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
316 return SectionHeaderTable->sh_link;
317 if (Header->e_shstrndx >= getNumSections())
320 return Header->e_shstrndx;
323 template <class ELFT>
324 ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
326 const uint64_t FileSize = Buf.size();
328 if (sizeof(Elf_Ehdr) > FileSize) {
330 EC = object_error::parse_failed;
334 Header = reinterpret_cast<const Elf_Ehdr *>(base());
336 if (Header->e_shoff == 0)
339 const uint64_t SectionTableOffset = Header->e_shoff;
341 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) {
342 // Section header table goes past end of file!
343 EC = object_error::parse_failed;
347 // The getNumSections() call below depends on SectionHeaderTable being set.
349 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
350 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
352 if (SectionTableOffset + SectionTableSize > FileSize) {
353 // Section table goes past end of file!
354 EC = object_error::parse_failed;
358 // Get string table sections.
359 uintX_t StringTableIndex = getStringTableIndex();
360 if (StringTableIndex) {
361 ErrorOr<const Elf_Shdr *> StrTabSecOrErr = getSection(StringTableIndex);
362 if ((EC = StrTabSecOrErr.getError()))
365 ErrorOr<StringRef> StringTableOrErr = getStringTable(*StrTabSecOrErr);
366 if ((EC = StringTableOrErr.getError()))
368 DotShstrtab = *StringTableOrErr;
371 EC = std::error_code();
374 template <class ELFT>
375 static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
376 return VAddr < Phdr->p_vaddr;
379 template <class ELFT>
380 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
381 if (Header->e_shentsize != sizeof(Elf_Shdr))
383 "Invalid section header entry size (e_shentsize) in ELF header");
384 return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
387 template <class ELFT>
388 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_end() const {
389 return section_begin() + getNumSections();
392 template <class ELFT>
393 ErrorOr<const typename ELFFile<ELFT>::Elf_Dyn *>
394 ELFFile<ELFT>::dynamic_table_begin(const Elf_Phdr *Phdr) const {
397 assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
398 uintX_t Offset = Phdr->p_offset;
399 if (Offset > Buf.size())
400 return object_error::parse_failed;
401 return reinterpret_cast<const Elf_Dyn *>(base() + Offset);
404 template <class ELFT>
405 ErrorOr<const typename ELFFile<ELFT>::Elf_Dyn *>
406 ELFFile<ELFT>::dynamic_table_end(const Elf_Phdr *Phdr) const {
409 assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
410 uintX_t Size = Phdr->p_filesz;
411 if (Size % sizeof(Elf_Dyn))
412 return object_error::elf_invalid_dynamic_table_size;
413 // FIKME: Check for overflow?
414 uintX_t End = Phdr->p_offset + Size;
415 if (End > Buf.size())
416 return object_error::parse_failed;
417 return reinterpret_cast<const Elf_Dyn *>(base() + End);
420 template <class ELFT>
421 template <typename T>
422 const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
423 ErrorOr<const Elf_Shdr *> Sec = getSection(Section);
424 if (std::error_code EC = Sec.getError())
425 report_fatal_error(EC.message());
426 return getEntry<T>(*Sec, Entry);
429 template <class ELFT>
430 template <typename T>
431 const T *ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
432 uint32_t Entry) const {
433 return reinterpret_cast<const T *>(base() + Section->sh_offset +
434 (Entry * Section->sh_entsize));
437 template <class ELFT>
438 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
439 ELFFile<ELFT>::getSection(uint32_t Index) const {
440 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
441 if (Index >= getNumSections())
442 return object_error::invalid_section_index;
444 return reinterpret_cast<const Elf_Shdr *>(
445 reinterpret_cast<const char *>(SectionHeaderTable) +
446 (Index * Header->e_shentsize));
449 template <class ELFT>
451 ELFFile<ELFT>::getStringTable(const Elf_Shdr *Section) const {
452 if (Section->sh_type != ELF::SHT_STRTAB)
453 return object_error::parse_failed;
454 uint64_t Offset = Section->sh_offset;
455 uint64_t Size = Section->sh_size;
456 if (Offset + Size > Buf.size())
457 return object_error::parse_failed;
458 StringRef Data((const char *)base() + Section->sh_offset, Size);
459 if (Data[Size - 1] != '\0')
460 return object_error::string_table_non_null_end;
464 template <class ELFT>
465 ErrorOr<ArrayRef<typename ELFFile<ELFT>::Elf_Word>>
466 ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section) const {
467 assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
468 const Elf_Word *ShndxTableBegin =
469 reinterpret_cast<const Elf_Word *>(base() + Section.sh_offset);
470 uintX_t Size = Section.sh_size;
471 if (Size % sizeof(uint32_t))
472 return object_error::parse_failed;
473 uintX_t NumSymbols = Size / sizeof(uint32_t);
474 const Elf_Word *ShndxTableEnd = ShndxTableBegin + NumSymbols;
475 if (reinterpret_cast<const char *>(ShndxTableEnd) > Buf.end())
476 return object_error::parse_failed;
477 ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Section.sh_link);
478 if (std::error_code EC = SymTableOrErr.getError())
480 const Elf_Shdr &SymTable = **SymTableOrErr;
481 if (SymTable.sh_type != ELF::SHT_SYMTAB &&
482 SymTable.sh_type != ELF::SHT_DYNSYM)
483 return object_error::parse_failed;
484 if (NumSymbols != (SymTable.sh_size / sizeof(Elf_Sym)))
485 return object_error::parse_failed;
486 return ArrayRef<Elf_Word>(ShndxTableBegin, ShndxTableEnd);
489 template <class ELFT>
491 ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
492 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
493 return object_error::parse_failed;
494 ErrorOr<const Elf_Shdr *> SectionOrErr = getSection(Sec.sh_link);
495 if (std::error_code EC = SectionOrErr.getError())
497 return getStringTable(*SectionOrErr);
500 template <class ELFT>
502 ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
503 uint32_t Offset = Section->sh_name;
506 if (Offset >= DotShstrtab.size())
507 return object_error::parse_failed;
508 return StringRef(DotShstrtab.data() + Offset);
511 /// This function returns the hash value for a symbol in the .dynsym section
512 /// Name of the API remains consistent as specified in the libelf
513 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
514 static inline unsigned elf_hash(StringRef &symbolName) {
516 for (unsigned i = 0, j = symbolName.size(); i < j; i++) {
517 h = (h << 4) + symbolName[i];
525 } // end namespace object
526 } // end namespace llvm