LLVM  10.0.0svn
ELF.h
Go to the documentation of this file.
1 //===- ELF.h - ELF object file implementation -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the ELFFile template class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_OBJECT_ELF_H
14 #define LLVM_OBJECT_ELF_H
15 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/Object/ELFTypes.h"
21 #include "llvm/Object/Error.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/Error.h"
24 #include <cassert>
25 #include <cstddef>
26 #include <cstdint>
27 #include <limits>
28 #include <utility>
29 
30 namespace llvm {
31 namespace object {
32 
36 
37 // Subclasses of ELFFile may need this for template instantiation
38 inline std::pair<unsigned char, unsigned char>
40  if (Object.size() < ELF::EI_NIDENT)
41  return std::make_pair((uint8_t)ELF::ELFCLASSNONE,
42  (uint8_t)ELF::ELFDATANONE);
43  return std::make_pair((uint8_t)Object[ELF::EI_CLASS],
44  (uint8_t)Object[ELF::EI_DATA]);
45 }
46 
47 static inline Error createError(const Twine &Err) {
48  return make_error<StringError>(Err, object_error::parse_failed);
49 }
50 
51 template <class ELFT> class ELFFile;
52 
53 template <class ELFT>
54 std::string getSecIndexForError(const ELFFile<ELFT> *Obj,
55  const typename ELFT::Shdr *Sec) {
56  auto TableOrErr = Obj->sections();
57  if (TableOrErr)
58  return "[index " + std::to_string(Sec - &TableOrErr->front()) + "]";
59  // To make this helper be more convenient for error reporting purposes we
60  // drop the error. But really it should never be triggered. Before this point,
61  // our code should have called 'sections()' and reported a proper error on
62  // failure.
63  llvm::consumeError(TableOrErr.takeError());
64  return "[unknown index]";
65 }
66 
67 static inline Error defaultWarningHandler(const Twine &Msg) {
68  return createError(Msg);
69 }
70 
71 template <class ELFT>
72 class ELFFile {
73 public:
75  using uintX_t = typename ELFT::uint;
76  using Elf_Ehdr = typename ELFT::Ehdr;
77  using Elf_Shdr = typename ELFT::Shdr;
78  using Elf_Sym = typename ELFT::Sym;
79  using Elf_Dyn = typename ELFT::Dyn;
80  using Elf_Phdr = typename ELFT::Phdr;
81  using Elf_Rel = typename ELFT::Rel;
82  using Elf_Rela = typename ELFT::Rela;
83  using Elf_Relr = typename ELFT::Relr;
84  using Elf_Verdef = typename ELFT::Verdef;
85  using Elf_Verdaux = typename ELFT::Verdaux;
86  using Elf_Verneed = typename ELFT::Verneed;
87  using Elf_Vernaux = typename ELFT::Vernaux;
88  using Elf_Versym = typename ELFT::Versym;
89  using Elf_Hash = typename ELFT::Hash;
90  using Elf_GnuHash = typename ELFT::GnuHash;
91  using Elf_Nhdr = typename ELFT::Nhdr;
92  using Elf_Note = typename ELFT::Note;
93  using Elf_Note_Iterator = typename ELFT::NoteIterator;
94  using Elf_Dyn_Range = typename ELFT::DynRange;
95  using Elf_Shdr_Range = typename ELFT::ShdrRange;
96  using Elf_Sym_Range = typename ELFT::SymRange;
97  using Elf_Rel_Range = typename ELFT::RelRange;
98  using Elf_Rela_Range = typename ELFT::RelaRange;
99  using Elf_Relr_Range = typename ELFT::RelrRange;
100  using Elf_Phdr_Range = typename ELFT::PhdrRange;
101 
102  // This is a callback that can be passed to a number of functions.
103  // It can be used to ignore non-critical errors (warnings), which is
104  // useful for dumpers, like llvm-readobj.
105  // It accepts a warning message string and returns a success
106  // when the warning should be ignored or an error otherwise.
108 
109  const uint8_t *base() const { return Buf.bytes_begin(); }
110 
111  size_t getBufSize() const { return Buf.size(); }
112 
113 private:
114  StringRef Buf;
115 
116  ELFFile(StringRef Object);
117 
118 public:
119  const Elf_Ehdr *getHeader() const {
120  return reinterpret_cast<const Elf_Ehdr *>(base());
121  }
122 
123  template <typename T>
125  template <typename T>
126  Expected<const T *> getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
127 
129  getStringTable(const Elf_Shdr *Section,
130  WarningHandler WarnHandler = &defaultWarningHandler) const;
133  Elf_Shdr_Range Sections) const;
134 
135  Expected<ArrayRef<Elf_Word>> getSHNDXTable(const Elf_Shdr &Section) const;
137  Elf_Shdr_Range Sections) const;
138 
141  SmallVectorImpl<char> &Result) const;
143 
144  std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const;
145  std::string getDynamicTagAsString(uint64_t Type) const;
146 
147  /// Get the symbol for a given relocation.
149  const Elf_Shdr *SymTab) const;
150 
151  static Expected<ELFFile> create(StringRef Object);
152 
153  bool isMipsELF64() const {
154  return getHeader()->e_machine == ELF::EM_MIPS &&
155  getHeader()->getFileClass() == ELF::ELFCLASS64;
156  }
157 
158  bool isMips64EL() const {
159  return isMipsELF64() &&
160  getHeader()->getDataEncoding() == ELF::ELFDATA2LSB;
161  }
162 
164 
166 
167  Expected<const uint8_t *> toMappedAddr(uint64_t VAddr) const;
168 
170  if (!Sec)
171  return makeArrayRef<Elf_Sym>(nullptr, nullptr);
172  return getSectionContentsAsArray<Elf_Sym>(Sec);
173  }
174 
176  return getSectionContentsAsArray<Elf_Rela>(Sec);
177  }
178 
180  return getSectionContentsAsArray<Elf_Rel>(Sec);
181  }
182 
184  return getSectionContentsAsArray<Elf_Relr>(Sec);
185  }
186 
188 
190 
191  /// Iterate over program header table.
193  if (getHeader()->e_phnum && getHeader()->e_phentsize != sizeof(Elf_Phdr))
194  return createError("invalid e_phentsize: " +
195  Twine(getHeader()->e_phentsize));
196  if (getHeader()->e_phoff +
197  (getHeader()->e_phnum * getHeader()->e_phentsize) >
198  getBufSize())
199  return createError("program headers are longer than binary of size " +
200  Twine(getBufSize()) + ": e_phoff = 0x" +
201  Twine::utohexstr(getHeader()->e_phoff) +
202  ", e_phnum = " + Twine(getHeader()->e_phnum) +
203  ", e_phentsize = " + Twine(getHeader()->e_phentsize));
204  auto *Begin =
205  reinterpret_cast<const Elf_Phdr *>(base() + getHeader()->e_phoff);
206  return makeArrayRef(Begin, Begin + getHeader()->e_phnum);
207  }
208 
209  /// Get an iterator over notes in a program header.
210  ///
211  /// The program header must be of type \c PT_NOTE.
212  ///
213  /// \param Phdr the program header to iterate over.
214  /// \param Err [out] an error to support fallible iteration, which should
215  /// be checked after iteration ends.
216  Elf_Note_Iterator notes_begin(const Elf_Phdr &Phdr, Error &Err) const {
217  assert(Phdr.p_type == ELF::PT_NOTE && "Phdr is not of type PT_NOTE");
218  ErrorAsOutParameter ErrAsOutParam(&Err);
219  if (Phdr.p_offset + Phdr.p_filesz > getBufSize()) {
220  Err = createError("PT_NOTE header has invalid offset (0x" +
221  Twine::utohexstr(Phdr.p_offset) + ") or size (0x" +
222  Twine::utohexstr(Phdr.p_filesz) + ")");
223  return Elf_Note_Iterator(Err);
224  }
225  return Elf_Note_Iterator(base() + Phdr.p_offset, Phdr.p_filesz, Err);
226  }
227 
228  /// Get an iterator over notes in a section.
229  ///
230  /// The section must be of type \c SHT_NOTE.
231  ///
232  /// \param Shdr the section to iterate over.
233  /// \param Err [out] an error to support fallible iteration, which should
234  /// be checked after iteration ends.
235  Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const {
236  assert(Shdr.sh_type == ELF::SHT_NOTE && "Shdr is not of type SHT_NOTE");
237  ErrorAsOutParameter ErrAsOutParam(&Err);
238  if (Shdr.sh_offset + Shdr.sh_size > getBufSize()) {
239  Err = createError("SHT_NOTE section " + getSecIndexForError(this, &Shdr) +
240  " has invalid offset (0x" +
241  Twine::utohexstr(Shdr.sh_offset) + ") or size (0x" +
242  Twine::utohexstr(Shdr.sh_size) + ")");
243  return Elf_Note_Iterator(Err);
244  }
245  return Elf_Note_Iterator(base() + Shdr.sh_offset, Shdr.sh_size, Err);
246  }
247 
248  /// Get the end iterator for notes.
250  return Elf_Note_Iterator();
251  }
252 
253  /// Get an iterator range over notes of a program header.
254  ///
255  /// The program header must be of type \c PT_NOTE.
256  ///
257  /// \param Phdr the program header to iterate over.
258  /// \param Err [out] an error to support fallible iteration, which should
259  /// be checked after iteration ends.
261  Error &Err) const {
262  return make_range(notes_begin(Phdr, Err), notes_end());
263  }
264 
265  /// Get an iterator range over notes of a section.
266  ///
267  /// The section must be of type \c SHT_NOTE.
268  ///
269  /// \param Shdr the section to iterate over.
270  /// \param Err [out] an error to support fallible iteration, which should
271  /// be checked after iteration ends.
273  Error &Err) const {
274  return make_range(notes_begin(Shdr, Err), notes_end());
275  }
276 
278  Elf_Shdr_Range Sections,
279  WarningHandler WarnHandler = &defaultWarningHandler) const;
281  ArrayRef<Elf_Word> ShndxTable) const;
283  const Elf_Shdr *SymTab,
284  ArrayRef<Elf_Word> ShndxTable) const;
286  Elf_Sym_Range Symtab,
287  ArrayRef<Elf_Word> ShndxTable) const;
289 
291  uint32_t Index) const;
292 
294  getSectionName(const Elf_Shdr *Section,
295  WarningHandler WarnHandler = &defaultWarningHandler) const;
297  StringRef DotShstrtab) const;
298  template <typename T>
301 };
302 
307 
308 template <class ELFT>
310 getSection(typename ELFT::ShdrRange Sections, uint32_t Index) {
311  if (Index >= Sections.size())
312  return createError("invalid section index: " + Twine(Index));
313  return &Sections[Index];
314 }
315 
316 template <class ELFT>
317 inline Expected<uint32_t>
318 getExtendedSymbolTableIndex(const typename ELFT::Sym *Sym,
319  const typename ELFT::Sym *FirstSym,
320  ArrayRef<typename ELFT::Word> ShndxTable) {
321  assert(Sym->st_shndx == ELF::SHN_XINDEX);
322  unsigned Index = Sym - FirstSym;
323  if (Index >= ShndxTable.size())
324  return createError(
325  "extended symbol index (" + Twine(Index) +
326  ") is past the end of the SHT_SYMTAB_SHNDX section of size " +
327  Twine(ShndxTable.size()));
328 
329  // The size of the table was checked in getSHNDXTable.
330  return ShndxTable[Index];
331 }
332 
333 template <class ELFT>
336  ArrayRef<Elf_Word> ShndxTable) const {
337  uint32_t Index = Sym->st_shndx;
338  if (Index == ELF::SHN_XINDEX) {
339  auto ErrorOrIndex = getExtendedSymbolTableIndex<ELFT>(
340  Sym, Syms.begin(), ShndxTable);
341  if (!ErrorOrIndex)
342  return ErrorOrIndex.takeError();
343  return *ErrorOrIndex;
344  }
345  if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
346  return 0;
347  return Index;
348 }
349 
350 template <class ELFT>
352 ELFFile<ELFT>::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
353  ArrayRef<Elf_Word> ShndxTable) const {
354  auto SymsOrErr = symbols(SymTab);
355  if (!SymsOrErr)
356  return SymsOrErr.takeError();
357  return getSection(Sym, *SymsOrErr, ShndxTable);
358 }
359 
360 template <class ELFT>
363  ArrayRef<Elf_Word> ShndxTable) const {
364  auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable);
365  if (!IndexOrErr)
366  return IndexOrErr.takeError();
367  uint32_t Index = *IndexOrErr;
368  if (Index == 0)
369  return nullptr;
370  return getSection(Index);
371 }
372 
373 template <class ELFT>
376  auto SymsOrErr = symbols(Sec);
377  if (!SymsOrErr)
378  return SymsOrErr.takeError();
379 
380  Elf_Sym_Range Symbols = *SymsOrErr;
381  if (Index >= Symbols.size())
382  return createError("unable to get symbol from section " +
383  getSecIndexForError(this, Sec) +
384  ": invalid symbol index (" + Twine(Index) + ")");
385  return &Symbols[Index];
386 }
387 
388 template <class ELFT>
389 template <typename T>
392  if (Sec->sh_entsize != sizeof(T) && sizeof(T) != 1)
393  return createError("section " + getSecIndexForError(this, Sec) +
394  " has an invalid sh_entsize: " + Twine(Sec->sh_entsize));
395 
396  uintX_t Offset = Sec->sh_offset;
397  uintX_t Size = Sec->sh_size;
398 
399  if (Size % sizeof(T))
400  return createError("section " + getSecIndexForError(this, Sec) +
401  " has an invalid sh_size (" + Twine(Size) +
402  ") which is not a multiple of its sh_entsize (" +
403  Twine(Sec->sh_entsize) + ")");
404  if ((std::numeric_limits<uintX_t>::max() - Offset < Size) ||
405  Offset + Size > Buf.size())
406  return createError("section " + getSecIndexForError(this, Sec) +
407  " has a sh_offset (0x" + Twine::utohexstr(Offset) +
408  ") + sh_size (0x" + Twine(Size) +
409  ") that cannot be represented");
410 
411  if (Offset % alignof(T))
412  // TODO: this error is untested.
413  return createError("unaligned data");
414 
415  const T *Start = reinterpret_cast<const T *>(base() + Offset);
416  return makeArrayRef(Start, Size / sizeof(T));
417 }
418 
419 template <class ELFT>
422  return getSectionContentsAsArray<uint8_t>(Sec);
423 }
424 
425 template <class ELFT>
427  return getELFRelocationTypeName(getHeader()->e_machine, Type);
428 }
429 
430 template <class ELFT>
432  SmallVectorImpl<char> &Result) const {
433  if (!isMipsELF64()) {
435  Result.append(Name.begin(), Name.end());
436  } else {
437  // The Mips N64 ABI allows up to three operations to be specified per
438  // relocation record. Unfortunately there's no easy way to test for the
439  // presence of N64 ELFs as they have no special flag that identifies them
440  // as being N64. We can safely assume at the moment that all Mips
441  // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
442  // information to disambiguate between old vs new ABIs.
443  uint8_t Type1 = (Type >> 0) & 0xFF;
444  uint8_t Type2 = (Type >> 8) & 0xFF;
445  uint8_t Type3 = (Type >> 16) & 0xFF;
446 
447  // Concat all three relocation type names.
449  Result.append(Name.begin(), Name.end());
450 
451  Name = getRelocationTypeName(Type2);
452  Result.append(1, '/');
453  Result.append(Name.begin(), Name.end());
454 
455  Name = getRelocationTypeName(Type3);
456  Result.append(1, '/');
457  Result.append(Name.begin(), Name.end());
458  }
459 }
460 
461 template <class ELFT>
463  return getELFRelativeRelocationType(getHeader()->e_machine);
464 }
465 
466 template <class ELFT>
469  const Elf_Shdr *SymTab) const {
470  uint32_t Index = Rel->getSymbol(isMips64EL());
471  if (Index == 0)
472  return nullptr;
473  return getEntry<Elf_Sym>(SymTab, Index);
474 }
475 
476 template <class ELFT>
479  WarningHandler WarnHandler) const {
480  uint32_t Index = getHeader()->e_shstrndx;
481  if (Index == ELF::SHN_XINDEX)
482  Index = Sections[0].sh_link;
483 
484  if (!Index) // no section string table.
485  return "";
486  if (Index >= Sections.size())
487  return createError("section header string table index " + Twine(Index) +
488  " does not exist");
489  return getStringTable(&Sections[Index], WarnHandler);
490 }
491 
492 template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
493 
494 template <class ELFT>
496  if (sizeof(Elf_Ehdr) > Object.size())
497  return createError("invalid buffer: the size (" + Twine(Object.size()) +
498  ") is smaller than an ELF header (" +
499  Twine(sizeof(Elf_Ehdr)) + ")");
500  return ELFFile(Object);
501 }
502 
503 template <class ELFT>
505  const uintX_t SectionTableOffset = getHeader()->e_shoff;
506  if (SectionTableOffset == 0)
507  return ArrayRef<Elf_Shdr>();
508 
509  if (getHeader()->e_shentsize != sizeof(Elf_Shdr))
510  return createError("invalid e_shentsize in ELF header: " +
511  Twine(getHeader()->e_shentsize));
512 
513  const uint64_t FileSize = Buf.size();
514  if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize ||
515  SectionTableOffset + (uintX_t)sizeof(Elf_Shdr) < SectionTableOffset)
516  return createError(
517  "section header table goes past the end of the file: e_shoff = 0x" +
518  Twine::utohexstr(SectionTableOffset));
519 
520  // Invalid address alignment of section headers
521  if (SectionTableOffset & (alignof(Elf_Shdr) - 1))
522  // TODO: this error is untested.
523  return createError("invalid alignment of section headers");
524 
525  const Elf_Shdr *First =
526  reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
527 
528  uintX_t NumSections = getHeader()->e_shnum;
529  if (NumSections == 0)
530  NumSections = First->sh_size;
531 
532  if (NumSections > UINT64_MAX / sizeof(Elf_Shdr))
533  return createError("invalid number of sections specified in the NULL "
534  "section's sh_size field (" +
535  Twine(NumSections) + ")");
536 
537  const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr);
538  if (SectionTableOffset + SectionTableSize < SectionTableOffset)
539  return createError(
540  "invalid section header table offset (e_shoff = 0x" +
541  Twine::utohexstr(SectionTableOffset) +
542  ") or invalid number of sections specified in the first section "
543  "header's sh_size field (0x" +
544  Twine::utohexstr(NumSections) + ")");
545 
546  // Section table goes past end of file!
547  if (SectionTableOffset + SectionTableSize > FileSize)
548  return createError("section table goes past the end of file");
549  return makeArrayRef(First, NumSections);
550 }
551 
552 template <class ELFT>
553 template <typename T>
555  uint32_t Entry) const {
556  auto SecOrErr = getSection(Section);
557  if (!SecOrErr)
558  return SecOrErr.takeError();
559  return getEntry<T>(*SecOrErr, Entry);
560 }
561 
562 template <class ELFT>
563 template <typename T>
565  uint32_t Entry) const {
566  if (sizeof(T) != Section->sh_entsize)
567  return createError("section " + getSecIndexForError(this, Section) +
568  " has invalid sh_entsize: expected " + Twine(sizeof(T)) +
569  ", but got " + Twine(Section->sh_entsize));
570  size_t Pos = Section->sh_offset + Entry * sizeof(T);
571  if (Pos + sizeof(T) > Buf.size())
572  return createError("unable to access section " +
573  getSecIndexForError(this, Section) + " data at 0x" +
574  Twine::utohexstr(Pos) +
575  ": offset goes past the end of file");
576  return reinterpret_cast<const T *>(base() + Pos);
577 }
578 
579 template <class ELFT>
582  auto TableOrErr = sections();
583  if (!TableOrErr)
584  return TableOrErr.takeError();
585  return object::getSection<ELFT>(*TableOrErr, Index);
586 }
587 
588 template <class ELFT>
591  WarningHandler WarnHandler) const {
592  if (Section->sh_type != ELF::SHT_STRTAB)
593  if (Error E = WarnHandler("invalid sh_type for string table section " +
594  getSecIndexForError(this, Section) +
595  ": expected SHT_STRTAB, but got " +
597  getHeader()->e_machine, Section->sh_type)))
598  return std::move(E);
599 
600  auto V = getSectionContentsAsArray<char>(Section);
601  if (!V)
602  return V.takeError();
603  ArrayRef<char> Data = *V;
604  if (Data.empty())
605  return createError("SHT_STRTAB string table section " +
606  getSecIndexForError(this, Section) + " is empty");
607  if (Data.back() != '\0')
608  return createError("SHT_STRTAB string table section " +
609  getSecIndexForError(this, Section) +
610  " is non-null terminated");
611  return StringRef(Data.begin(), Data.size());
612 }
613 
614 template <class ELFT>
617  auto SectionsOrErr = sections();
618  if (!SectionsOrErr)
619  return SectionsOrErr.takeError();
620  return getSHNDXTable(Section, *SectionsOrErr);
621 }
622 
623 template <class ELFT>
626  Elf_Shdr_Range Sections) const {
627  assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
628  auto VOrErr = getSectionContentsAsArray<Elf_Word>(&Section);
629  if (!VOrErr)
630  return VOrErr.takeError();
631  ArrayRef<Elf_Word> V = *VOrErr;
632  auto SymTableOrErr = object::getSection<ELFT>(Sections, Section.sh_link);
633  if (!SymTableOrErr)
634  return SymTableOrErr.takeError();
635  const Elf_Shdr &SymTable = **SymTableOrErr;
636  if (SymTable.sh_type != ELF::SHT_SYMTAB &&
637  SymTable.sh_type != ELF::SHT_DYNSYM)
638  return createError("SHT_SYMTAB_SHNDX section is linked with " +
640  SymTable.sh_type) +
641  " section (expected SHT_SYMTAB/SHT_DYNSYM)");
642 
643  if (V.size() != (SymTable.sh_size / sizeof(Elf_Sym)))
644  return createError("SHT_SYMTAB_SHNDX section has sh_size (" +
645  Twine(SymTable.sh_size) +
646  ") which is not equal to the number of symbols (" +
647  Twine(V.size()) + ")");
648  return V;
649 }
650 
651 template <class ELFT>
654  auto SectionsOrErr = sections();
655  if (!SectionsOrErr)
656  return SectionsOrErr.takeError();
657  return getStringTableForSymtab(Sec, *SectionsOrErr);
658 }
659 
660 template <class ELFT>
663  Elf_Shdr_Range Sections) const {
664 
665  if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
666  // TODO: this error is untested.
667  return createError(
668  "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
669  auto SectionOrErr = object::getSection<ELFT>(Sections, Sec.sh_link);
670  if (!SectionOrErr)
671  return SectionOrErr.takeError();
672  return getStringTable(*SectionOrErr);
673 }
674 
675 template <class ELFT>
678  WarningHandler WarnHandler) const {
679  auto SectionsOrErr = sections();
680  if (!SectionsOrErr)
681  return SectionsOrErr.takeError();
682  auto Table = getSectionStringTable(*SectionsOrErr, WarnHandler);
683  if (!Table)
684  return Table.takeError();
685  return getSectionName(Section, *Table);
686 }
687 
688 template <class ELFT>
690  StringRef DotShstrtab) const {
691  uint32_t Offset = Section->sh_name;
692  if (Offset == 0)
693  return StringRef();
694  if (Offset >= DotShstrtab.size())
695  return createError("a section " + getSecIndexForError(this, Section) +
696  " has an invalid sh_name (0x" +
697  Twine::utohexstr(Offset) +
698  ") offset which goes past the end of the "
699  "section name string table");
700  return StringRef(DotShstrtab.data() + Offset);
701 }
702 
703 /// This function returns the hash value for a symbol in the .dynsym section
704 /// Name of the API remains consistent as specified in the libelf
705 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
706 inline unsigned hashSysV(StringRef SymbolName) {
707  unsigned h = 0, g;
708  for (char C : SymbolName) {
709  h = (h << 4) + C;
710  g = h & 0xf0000000L;
711  if (g != 0)
712  h ^= g >> 24;
713  h &= ~g;
714  }
715  return h;
716 }
717 
718 } // end namespace object
719 } // end namespace llvm
720 
721 #endif // LLVM_OBJECT_ELF_H
typename ELFT::Dyn Elf_Dyn
Definition: ELF.h:79
uint64_t CallInst * C
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:157
typename ELFT::Rel Elf_Rel
Definition: ELF.h:81
iterator_range< Elf_Note_Iterator > notes(const Elf_Phdr &Phdr, Error &Err) const
Get an iterator range over notes of a program header.
Definition: ELF.h:260
Expected< const T * > getEntry(uint32_t Section, uint32_t Entry) const
Definition: ELF.h:554
This class represents lattice values for constants.
Definition: AllocatorList.h:23
typename ELFT::Note Elf_Note
Definition: ELF.h:92
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:495
iterator begin() const
Definition: ArrayRef.h:136
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Expected< StringRef > getStringTable(const Elf_Shdr *Section, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:590
typename ELFT::Vernaux Elf_Vernaux
Definition: ELF.h:87
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
StringRef getRelocationTypeName(uint32_t Type) const
Definition: ELF.h:426
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:104
ELFYAML::ELF_REL Type3
Definition: ELFYAML.cpp:1181
Expected< std::vector< Elf_Rela > > decode_relrs(Elf_Relr_Range relrs) const
Definition: ELF.cpp:272
unsigned hashSysV(StringRef SymbolName)
This function returns the hash value for a symbol in the .dynsym section Name of the API remains cons...
Definition: ELF.h:706
typename ELFT::Sym Elf_Sym
Definition: ELF.h:78
Expected< Elf_Phdr_Range > program_headers() const
Iterate over program header table.
Definition: ELF.h:192
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr) const
Definition: ELF.cpp:557
const uint8_t * base() const
Definition: ELF.h:109
Expected< const Elf_Shdr * > getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, ArrayRef< Elf_Word > ShndxTable) const
Definition: ELF.h:352
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
bool isMips64EL() const
Definition: ELF.h:158
iterator_range< Elf_Note_Iterator > notes(const Elf_Shdr &Shdr, Error &Err) const
Get an iterator range over notes of a section.
Definition: ELF.h:272
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
Expected< Elf_Shdr_Range > sections() const
Definition: ELF.h:504
typename ELFT::PhdrRange Elf_Phdr_Range
Definition: ELF.h:100
typename ELFT::Phdr Elf_Phdr
Definition: ELF.h:80
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
uint32_t getELFRelativeRelocationType(uint32_t Machine)
Definition: ELF.cpp:156
Expected< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr *Sec) const
Definition: ELF.h:421
#define UINT64_MAX
Definition: DataTypes.h:83
static Error createError(const Twine &Err)
Definition: ELF.h:47
typename ELFT::Relr Elf_Relr
Definition: ELF.h:83
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
Definition: ELF.h:39
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
typename ELFT::GnuHash Elf_GnuHash
Definition: ELF.h:90
static Error defaultWarningHandler(const Twine &Msg)
Definition: ELF.h:67
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:365
typename ELFT::SymRange Elf_Sym_Range
Definition: ELF.h:96
Elf_Note_Iterator notes_end() const
Get the end iterator for notes.
Definition: ELF.h:249
Expected< ArrayRef< T > > getSectionContentsAsArray(const Elf_Shdr *Sec) const
Definition: ELF.h:391
Expected< Elf_Rel_Range > rels(const Elf_Shdr *Sec) const
Definition: ELF.h:179
Expected< ArrayRef< Elf_Word > > getSHNDXTable(const Elf_Shdr &Section) const
Definition: ELF.h:616
bool isMipsELF64() const
Definition: ELF.h:153
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const
Get an iterator over notes in a section.
Definition: ELF.h:235
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Expected< StringRef > getStringTableForSymtab(const Elf_Shdr &Section) const
Definition: ELF.h:653
Expected< Elf_Rela_Range > relas(const Elf_Shdr *Sec) const
Definition: ELF.h:175
typename ELFT::uint uintX_t
Definition: ELF.h:75
Expected< uint32_t > getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, ArrayRef< Elf_Word > ShndxTable) const
Definition: ELF.h:335
Expected< Elf_Relr_Range > relrs(const Elf_Shdr *Sec) const
Definition: ELF.h:183
typename ELFT::NoteIterator Elf_Note_Iterator
Definition: ELF.h:93
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
Definition: ELF.cpp:431
typename ELFT::Nhdr Elf_Nhdr
Definition: ELF.h:91
Expected< StringRef > getSectionName(const Elf_Shdr *Section, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:677
typename ELFT::Verdaux Elf_Verdaux
Definition: ELF.h:85
typename ELFT::Ehdr Elf_Ehdr
Definition: ELF.h:76
typename ELFT::RelrRange Elf_Relr_Range
Definition: ELF.h:99
typename ELFT::Rela Elf_Rela
Definition: ELF.h:82
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:981
typename ELFT::Verneed Elf_Verneed
Definition: ELF.h:86
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:22
Expected< uint32_t > getExtendedSymbolTableIndex(const typename ELFT::Sym *Sym, const typename ELFT::Sym *FirstSym, ArrayRef< typename ELFT::Word > ShndxTable)
Definition: ELF.h:318
size_t getBufSize() const
Definition: ELF.h:111
Expected< StringRef > getSectionStringTable(Elf_Shdr_Range Sections, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:478
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:387
Expected< const Elf_Sym * > getRelocationSymbol(const Elf_Rel *Rel, const Elf_Shdr *SymTab) const
Get the symbol for a given relocation.
Definition: ELF.h:468
A range adaptor for a pair of iterators.
Helper for Errors used as out-parameters.
Definition: Error.h:1035
iterator begin() const
Definition: StringRef.h:115
Elf_Note_Iterator notes_begin(const Elf_Phdr &Phdr, Error &Err) const
Get an iterator over notes in a program header.
Definition: ELF.h:216
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
typename ELFT::DynRange Elf_Dyn_Range
Definition: ELF.h:94
ELFYAML::ELF_REL Type2
Definition: ELFYAML.cpp:1180
Expected< std::vector< Elf_Rela > > android_relas(const Elf_Shdr *Sec) const
Definition: ELF.cpp:351
uint32_t getRelativeRelocationType() const
Definition: ELF.h:462
const unsigned char * bytes_begin() const
Definition: StringRef.h:119
Expected< const Elf_Sym * > getSymbol(const Elf_Shdr *Sec, uint32_t Index) const
Definition: ELF.h:375
Expected< Elf_Sym_Range > symbols(const Elf_Shdr *Sec) const
Definition: ELF.h:169
uint32_t Size
Definition: Profile.cpp:46
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
typename ELFT::Verdef Elf_Verdef
Definition: ELF.h:84
Expected< Elf_Dyn_Range > dynamicEntries() const
Definition: ELF.cpp:500
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:136
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
typename ELFT::RelRange Elf_Rel_Range
Definition: ELF.h:97
aarch64 promote const
typename ELFT::Versym Elf_Versym
Definition: ELF.h:88
typename ELFT::RelaRange Elf_Rela_Range
Definition: ELF.h:98
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:103
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
typename ELFT::ShdrRange Elf_Shdr_Range
Definition: ELF.h:95
const Elf_Ehdr * getHeader() const
Definition: ELF.h:119
std::string getSecIndexForError(const ELFFile< ELFT > *Obj, const typename ELFT::Shdr *Sec)
Definition: ELF.h:54
iterator end() const
Definition: StringRef.h:117
typename ELFT::Shdr Elf_Shdr
Definition: ELF.h:77
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143
typename ELFT::Hash Elf_Hash
Definition: ELF.h:89