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;
108 const Elf_Shdr *dot_gnu_version_sec = nullptr; // .gnu.version
109 const Elf_Shdr *dot_gnu_version_r_sec = nullptr; // .gnu.version_r
110 const Elf_Shdr *dot_gnu_version_d_sec = nullptr; // .gnu.version_d
112 // Records for each version index the corresponding Verdef or Vernaux entry.
113 // This is filled the first time LoadVersionMap() is called.
114 class VersionMapEntry : public PointerIntPair<const void*, 1> {
116 // If the integer is 0, this is an Elf_Verdef*.
117 // If the integer is 1, this is an Elf_Vernaux*.
118 VersionMapEntry() : PointerIntPair<const void*, 1>(nullptr, 0) { }
119 VersionMapEntry(const Elf_Verdef *verdef)
120 : PointerIntPair<const void*, 1>(verdef, 0) { }
121 VersionMapEntry(const Elf_Vernaux *vernaux)
122 : PointerIntPair<const void*, 1>(vernaux, 1) { }
123 bool isNull() const { return getPointer() == nullptr; }
124 bool isVerdef() const { return !isNull() && getInt() == 0; }
125 bool isVernaux() const { return !isNull() && getInt() == 1; }
126 const Elf_Verdef *getVerdef() const {
127 return isVerdef() ? (const Elf_Verdef*)getPointer() : nullptr;
129 const Elf_Vernaux *getVernaux() const {
130 return isVernaux() ? (const Elf_Vernaux*)getPointer() : nullptr;
133 mutable SmallVector<VersionMapEntry, 16> VersionMap;
134 void LoadVersionDefs(const Elf_Shdr *sec) const;
135 void LoadVersionNeeds(const Elf_Shdr *ec) const;
136 void LoadVersionMap() const;
140 const T *getEntry(uint32_t Section, uint32_t Entry) const;
141 template <typename T>
142 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
144 const Elf_Shdr *getDotSymtabSec() const { return dot_symtab_sec; }
145 const Elf_Shdr *getDotDynSymSec() const { return DotDynSymSec; }
147 ErrorOr<StringRef> getStringTable(const Elf_Shdr *Section) const;
148 ErrorOr<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
150 ErrorOr<StringRef> getSymbolVersion(StringRef StrTab, const Elf_Sym *Symb,
151 bool &IsDefault) const;
152 void VerifyStrTab(const Elf_Shdr *sh) const;
154 StringRef getRelocationTypeName(uint32_t Type) const;
155 void getRelocationTypeName(uint32_t Type,
156 SmallVectorImpl<char> &Result) const;
158 /// \brief Get the symbol table section and symbol for a given relocation.
159 template <class RelT>
160 std::pair<const Elf_Shdr *, const Elf_Sym *>
161 getRelocationSymbol(const Elf_Shdr *RelSec, const RelT *Rel) const;
163 ELFFile(StringRef Object, std::error_code &EC);
165 bool isMipsELF64() const {
166 return Header->e_machine == ELF::EM_MIPS &&
167 Header->getFileClass() == ELF::ELFCLASS64;
170 bool isMips64EL() const {
171 return Header->e_machine == ELF::EM_MIPS &&
172 Header->getFileClass() == ELF::ELFCLASS64 &&
173 Header->getDataEncoding() == ELF::ELFDATA2LSB;
176 const Elf_Shdr *section_begin() const;
177 const Elf_Shdr *section_end() const;
178 Elf_Shdr_Range sections() const {
179 return make_range(section_begin(), section_end());
182 const Elf_Sym *symbol_begin(const Elf_Shdr *Sec) const {
185 if (Sec->sh_entsize != sizeof(Elf_Sym))
186 report_fatal_error("Invalid symbol size");
187 return reinterpret_cast<const Elf_Sym *>(base() + Sec->sh_offset);
189 const Elf_Sym *symbol_end(const Elf_Shdr *Sec) const {
192 uint64_t Size = Sec->sh_size;
193 if (Size % sizeof(Elf_Sym))
194 report_fatal_error("Invalid symbol table size");
195 return symbol_begin(Sec) + Size / sizeof(Elf_Sym);
197 Elf_Sym_Range symbols(const Elf_Shdr *Sec) const {
198 return make_range(symbol_begin(Sec), symbol_end(Sec));
201 const Elf_Sym *symbol_begin() const { return symbol_begin(dot_symtab_sec); }
202 const Elf_Sym *symbol_end() const { return symbol_end(dot_symtab_sec); }
203 Elf_Sym_Range symbols() const { return symbols(dot_symtab_sec); }
205 const Elf_Sym *dynamic_symbol_begin() const {
206 return symbol_begin(DotDynSymSec);
208 const Elf_Sym *dynamic_symbol_end() const { return symbol_end(DotDynSymSec); }
209 Elf_Sym_Range dynamic_symbols() const { return symbols(DotDynSymSec); }
211 typedef iterator_range<const Elf_Rela *> Elf_Rela_Range;
213 const Elf_Rela *rela_begin(const Elf_Shdr *sec) const {
214 if (sec->sh_entsize != sizeof(Elf_Rela))
215 report_fatal_error("Invalid relocation entry size");
216 return reinterpret_cast<const Elf_Rela *>(base() + sec->sh_offset);
219 const Elf_Rela *rela_end(const Elf_Shdr *sec) const {
220 uint64_t Size = sec->sh_size;
221 if (Size % sizeof(Elf_Rela))
222 report_fatal_error("Invalid relocation table size");
223 return rela_begin(sec) + Size / sizeof(Elf_Rela);
226 Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
227 return make_range(rela_begin(Sec), rela_end(Sec));
230 const Elf_Rel *rel_begin(const Elf_Shdr *sec) const {
231 if (sec->sh_entsize != sizeof(Elf_Rel))
232 report_fatal_error("Invalid relocation entry size");
233 return reinterpret_cast<const Elf_Rel *>(base() + sec->sh_offset);
236 const Elf_Rel *rel_end(const Elf_Shdr *sec) const {
237 uint64_t Size = sec->sh_size;
238 if (Size % sizeof(Elf_Rel))
239 report_fatal_error("Invalid relocation table size");
240 return rel_begin(sec) + Size / sizeof(Elf_Rel);
243 typedef iterator_range<const Elf_Rel *> Elf_Rel_Range;
244 Elf_Rel_Range rels(const Elf_Shdr *Sec) const {
245 return make_range(rel_begin(Sec), rel_end(Sec));
248 /// \brief Iterate over program header table.
249 const Elf_Phdr *program_header_begin() const {
250 if (Header->e_phnum && Header->e_phentsize != sizeof(Elf_Phdr))
251 report_fatal_error("Invalid program header size");
252 return reinterpret_cast<const Elf_Phdr *>(base() + Header->e_phoff);
255 const Elf_Phdr *program_header_end() const {
256 return program_header_begin() + Header->e_phnum;
259 typedef iterator_range<const Elf_Phdr *> Elf_Phdr_Range;
261 const Elf_Phdr_Range program_headers() const {
262 return make_range(program_header_begin(), program_header_end());
265 uint64_t getNumSections() const;
266 uintX_t getStringTableIndex() const;
267 ELF::Elf64_Word getExtendedSymbolTableIndex(const Elf_Sym *symb) const;
268 const Elf_Ehdr *getHeader() const { return Header; }
269 ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *symb) const;
270 ErrorOr<const Elf_Shdr *> getSection(uint32_t Index) const;
272 const Elf_Sym *getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
273 return &*(symbol_begin(Sec) + Index);
275 const Elf_Sym *getSymbol(uint32_t Index) const {
276 return getSymbol(dot_symtab_sec, Index);
279 ErrorOr<StringRef> getSectionName(const Elf_Shdr *Section) const;
280 ErrorOr<ArrayRef<uint8_t> > getSectionContents(const Elf_Shdr *Sec) const;
283 typedef ELFFile<ELFType<support::little, false>> ELF32LEFile;
284 typedef ELFFile<ELFType<support::little, true>> ELF64LEFile;
285 typedef ELFFile<ELFType<support::big, false>> ELF32BEFile;
286 typedef ELFFile<ELFType<support::big, true>> ELF64BEFile;
288 // Iterate through the version definitions, and place each Elf_Verdef
289 // in the VersionMap according to its index.
290 template <class ELFT>
291 void ELFFile<ELFT>::LoadVersionDefs(const Elf_Shdr *sec) const {
292 unsigned vd_size = sec->sh_size; // Size of section in bytes
293 unsigned vd_count = sec->sh_info; // Number of Verdef entries
294 const char *sec_start = (const char*)base() + sec->sh_offset;
295 const char *sec_end = sec_start + vd_size;
296 // The first Verdef entry is at the start of the section.
297 const char *p = sec_start;
298 for (unsigned i = 0; i < vd_count; i++) {
299 if (p + sizeof(Elf_Verdef) > sec_end)
300 report_fatal_error("Section ended unexpectedly while scanning "
301 "version definitions.");
302 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
303 if (vd->vd_version != ELF::VER_DEF_CURRENT)
304 report_fatal_error("Unexpected verdef version");
305 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
306 if (index >= VersionMap.size())
307 VersionMap.resize(index + 1);
308 VersionMap[index] = VersionMapEntry(vd);
313 // Iterate through the versions needed section, and place each Elf_Vernaux
314 // in the VersionMap according to its index.
315 template <class ELFT>
316 void ELFFile<ELFT>::LoadVersionNeeds(const Elf_Shdr *sec) const {
317 unsigned vn_size = sec->sh_size; // Size of section in bytes
318 unsigned vn_count = sec->sh_info; // Number of Verneed entries
319 const char *sec_start = (const char *)base() + sec->sh_offset;
320 const char *sec_end = sec_start + vn_size;
321 // The first Verneed entry is at the start of the section.
322 const char *p = sec_start;
323 for (unsigned i = 0; i < vn_count; i++) {
324 if (p + sizeof(Elf_Verneed) > sec_end)
325 report_fatal_error("Section ended unexpectedly while scanning "
326 "version needed records.");
327 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
328 if (vn->vn_version != ELF::VER_NEED_CURRENT)
329 report_fatal_error("Unexpected verneed version");
330 // Iterate through the Vernaux entries
331 const char *paux = p + vn->vn_aux;
332 for (unsigned j = 0; j < vn->vn_cnt; j++) {
333 if (paux + sizeof(Elf_Vernaux) > sec_end)
334 report_fatal_error("Section ended unexpected while scanning auxiliary "
335 "version needed records.");
336 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
337 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
338 if (index >= VersionMap.size())
339 VersionMap.resize(index + 1);
340 VersionMap[index] = VersionMapEntry(vna);
341 paux += vna->vna_next;
347 template <class ELFT>
348 void ELFFile<ELFT>::LoadVersionMap() const {
349 // If there is no dynamic symtab or version table, there is nothing to do.
350 if (!DotDynSymSec || !dot_gnu_version_sec)
353 // Has the VersionMap already been loaded?
354 if (VersionMap.size() > 0)
357 // The first two version indexes are reserved.
358 // Index 0 is LOCAL, index 1 is GLOBAL.
359 VersionMap.push_back(VersionMapEntry());
360 VersionMap.push_back(VersionMapEntry());
362 if (dot_gnu_version_d_sec)
363 LoadVersionDefs(dot_gnu_version_d_sec);
365 if (dot_gnu_version_r_sec)
366 LoadVersionNeeds(dot_gnu_version_r_sec);
369 template <class ELFT>
371 ELFFile<ELFT>::getExtendedSymbolTableIndex(const Elf_Sym *symb) const {
372 assert(symb->st_shndx == ELF::SHN_XINDEX);
373 return ExtendedSymbolTable.lookup(symb);
376 template <class ELFT>
377 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
378 ELFFile<ELFT>::getSection(const Elf_Sym *symb) const {
379 uint32_t Index = symb->st_shndx;
380 if (Index == ELF::SHN_XINDEX)
381 return getSection(ExtendedSymbolTable.lookup(symb));
382 if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
384 return getSection(symb->st_shndx);
387 template <class ELFT>
388 ErrorOr<ArrayRef<uint8_t> >
389 ELFFile<ELFT>::getSectionContents(const Elf_Shdr *Sec) const {
390 if (Sec->sh_offset + Sec->sh_size > Buf.size())
391 return object_error::parse_failed;
392 const uint8_t *Start = base() + Sec->sh_offset;
393 return makeArrayRef(Start, Sec->sh_size);
396 template <class ELFT>
397 StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
398 return getELFRelocationTypeName(Header->e_machine, Type);
401 template <class ELFT>
402 void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
403 SmallVectorImpl<char> &Result) const {
404 if (!isMipsELF64()) {
405 StringRef Name = getRelocationTypeName(Type);
406 Result.append(Name.begin(), Name.end());
408 // The Mips N64 ABI allows up to three operations to be specified per
409 // relocation record. Unfortunately there's no easy way to test for the
410 // presence of N64 ELFs as they have no special flag that identifies them
411 // as being N64. We can safely assume at the moment that all Mips
412 // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
413 // information to disambiguate between old vs new ABIs.
414 uint8_t Type1 = (Type >> 0) & 0xFF;
415 uint8_t Type2 = (Type >> 8) & 0xFF;
416 uint8_t Type3 = (Type >> 16) & 0xFF;
418 // Concat all three relocation type names.
419 StringRef Name = getRelocationTypeName(Type1);
420 Result.append(Name.begin(), Name.end());
422 Name = getRelocationTypeName(Type2);
423 Result.append(1, '/');
424 Result.append(Name.begin(), Name.end());
426 Name = getRelocationTypeName(Type3);
427 Result.append(1, '/');
428 Result.append(Name.begin(), Name.end());
432 template <class ELFT>
433 template <class RelT>
434 std::pair<const typename ELFFile<ELFT>::Elf_Shdr *,
435 const typename ELFFile<ELFT>::Elf_Sym *>
436 ELFFile<ELFT>::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
438 return std::make_pair(nullptr, nullptr);
439 ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Sec->sh_link);
440 if (std::error_code EC = SymTableOrErr.getError())
441 report_fatal_error(EC.message());
442 const Elf_Shdr *SymTable = *SymTableOrErr;
443 return std::make_pair(
444 SymTable, getEntry<Elf_Sym>(SymTable, Rel->getSymbol(isMips64EL())));
447 template <class ELFT>
448 uint64_t ELFFile<ELFT>::getNumSections() const {
449 assert(Header && "Header not initialized!");
450 if (Header->e_shnum == ELF::SHN_UNDEF && Header->e_shoff > 0) {
451 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
452 return SectionHeaderTable->sh_size;
454 return Header->e_shnum;
457 template <class ELFT>
458 typename ELFFile<ELFT>::uintX_t ELFFile<ELFT>::getStringTableIndex() const {
459 if (Header->e_shnum == ELF::SHN_UNDEF) {
460 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
461 return SectionHeaderTable->sh_link;
462 if (Header->e_shstrndx >= getNumSections())
465 return Header->e_shstrndx;
468 template <class ELFT>
469 ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
471 const uint64_t FileSize = Buf.size();
473 if (sizeof(Elf_Ehdr) > FileSize) {
475 EC = object_error::parse_failed;
479 Header = reinterpret_cast<const Elf_Ehdr *>(base());
481 if (Header->e_shoff == 0)
484 const uint64_t SectionTableOffset = Header->e_shoff;
486 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) {
487 // Section header table goes past end of file!
488 EC = object_error::parse_failed;
492 // The getNumSections() call below depends on SectionHeaderTable being set.
494 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
495 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
497 if (SectionTableOffset + SectionTableSize > FileSize) {
498 // Section table goes past end of file!
499 EC = object_error::parse_failed;
503 // Scan sections for special sections.
505 for (const Elf_Shdr &Sec : sections()) {
506 switch (Sec.sh_type) {
507 case ELF::SHT_SYMTAB_SHNDX:
508 if (SymbolTableSectionHeaderIndex) {
509 // More than one .symtab_shndx!
510 EC = object_error::parse_failed;
513 SymbolTableSectionHeaderIndex = &Sec;
515 case ELF::SHT_SYMTAB: {
516 if (dot_symtab_sec) {
517 // More than one .symtab!
518 EC = object_error::parse_failed;
521 dot_symtab_sec = &Sec;
522 ErrorOr<StringRef> SymtabOrErr = getStringTableForSymtab(Sec);
523 if ((EC = SymtabOrErr.getError()))
525 DotStrtab = *SymtabOrErr;
527 case ELF::SHT_DYNSYM: {
529 // More than one .dynsym!
530 EC = object_error::parse_failed;
536 case ELF::SHT_GNU_versym:
537 if (dot_gnu_version_sec != nullptr) {
538 // More than one .gnu.version section!
539 EC = object_error::parse_failed;
542 dot_gnu_version_sec = &Sec;
544 case ELF::SHT_GNU_verdef:
545 if (dot_gnu_version_d_sec != nullptr) {
546 // More than one .gnu.version_d section!
547 EC = object_error::parse_failed;
550 dot_gnu_version_d_sec = &Sec;
552 case ELF::SHT_GNU_verneed:
553 if (dot_gnu_version_r_sec != nullptr) {
554 // More than one .gnu.version_r section!
555 EC = object_error::parse_failed;
558 dot_gnu_version_r_sec = &Sec;
563 // Get string table sections.
564 uintX_t StringTableIndex = getStringTableIndex();
565 if (StringTableIndex) {
566 ErrorOr<const Elf_Shdr *> StrTabSecOrErr =
567 getSection(getStringTableIndex());
568 if ((EC = StrTabSecOrErr.getError()))
571 ErrorOr<StringRef> SymtabOrErr = getStringTable(*StrTabSecOrErr);
572 if ((EC = SymtabOrErr.getError()))
574 DotShstrtab = *SymtabOrErr;
577 // Build symbol name side-mapping if there is one.
578 if (SymbolTableSectionHeaderIndex) {
579 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
580 SymbolTableSectionHeaderIndex->sh_offset);
581 for (const Elf_Sym &S : symbols()) {
582 if (*ShndxTable != ELF::SHN_UNDEF)
583 ExtendedSymbolTable[&S] = *ShndxTable;
588 EC = std::error_code();
591 template <class ELFT>
592 static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
593 return VAddr < Phdr->p_vaddr;
596 template <class ELFT>
597 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
598 if (Header->e_shentsize != sizeof(Elf_Shdr))
600 "Invalid section header entry size (e_shentsize) in ELF header");
601 return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
604 template <class ELFT>
605 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_end() const {
606 return section_begin() + getNumSections();
609 template <class ELFT>
610 template <typename T>
611 const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
612 ErrorOr<const Elf_Shdr *> Sec = getSection(Section);
613 if (std::error_code EC = Sec.getError())
614 report_fatal_error(EC.message());
615 return getEntry<T>(*Sec, Entry);
618 template <class ELFT>
619 template <typename T>
620 const T *ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
621 uint32_t Entry) const {
622 return reinterpret_cast<const T *>(base() + Section->sh_offset +
623 (Entry * Section->sh_entsize));
626 template <class ELFT>
627 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
628 ELFFile<ELFT>::getSection(uint32_t Index) const {
629 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
630 if (Index >= getNumSections())
631 return object_error::invalid_section_index;
633 return reinterpret_cast<const Elf_Shdr *>(
634 reinterpret_cast<const char *>(SectionHeaderTable) +
635 (Index * Header->e_shentsize));
638 template <class ELFT>
640 ELFFile<ELFT>::getStringTable(const Elf_Shdr *Section) const {
641 if (Section->sh_type != ELF::SHT_STRTAB)
642 return object_error::parse_failed;
643 uint64_t Offset = Section->sh_offset;
644 uint64_t Size = Section->sh_size;
645 if (Offset + Size > Buf.size())
646 return object_error::parse_failed;
647 StringRef Data((const char *)base() + Section->sh_offset, Size);
648 if (Data[Size - 1] != '\0')
649 return object_error::string_table_non_null_end;
653 template <class ELFT>
655 ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
656 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
657 return object_error::parse_failed;
658 ErrorOr<const Elf_Shdr *> SectionOrErr = getSection(Sec.sh_link);
659 if (std::error_code EC = SectionOrErr.getError())
661 return getStringTable(*SectionOrErr);
664 template <class ELFT>
666 ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
667 uint32_t Offset = Section->sh_name;
670 if (Offset >= DotShstrtab.size())
671 return object_error::parse_failed;
672 return StringRef(DotShstrtab.data() + Offset);
675 template <class ELFT>
676 ErrorOr<StringRef> ELFFile<ELFT>::getSymbolVersion(StringRef StrTab,
678 bool &IsDefault) const {
679 // This is a dynamic symbol. Look in the GNU symbol version table.
680 if (!dot_gnu_version_sec) {
683 return StringRef("");
686 // Determine the position in the symbol table of this entry.
688 (reinterpret_cast<uintptr_t>(symb) - DotDynSymSec->sh_offset -
689 reinterpret_cast<uintptr_t>(base())) /
692 // Get the corresponding version index entry
693 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
694 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
696 // Special markers for unversioned symbols.
697 if (version_index == ELF::VER_NDX_LOCAL ||
698 version_index == ELF::VER_NDX_GLOBAL) {
700 return StringRef("");
703 // Lookup this symbol in the version table
705 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
706 return object_error::parse_failed;
707 const VersionMapEntry &entry = VersionMap[version_index];
709 // Get the version name string
711 if (entry.isVerdef()) {
712 // The first Verdaux entry holds the name.
713 name_offset = entry.getVerdef()->getAux()->vda_name;
715 name_offset = entry.getVernaux()->vna_name;
719 if (entry.isVerdef()) {
720 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
725 if (name_offset >= StrTab.size())
726 return object_error::parse_failed;
727 return StringRef(StrTab.data() + name_offset);
730 /// This function returns the hash value for a symbol in the .dynsym section
731 /// Name of the API remains consistent as specified in the libelf
732 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
733 static inline unsigned elf_hash(StringRef &symbolName) {
735 for (unsigned i = 0, j = symbolName.size(); i < j; i++) {
736 h = (h << 4) + symbolName[i];
744 } // end namespace object
745 } // end namespace llvm