LLVM 17.0.0git
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/MapVector.h"
20#include "llvm/ADT/StringRef.h"
23#include "llvm/Object/Error.h"
24#include "llvm/Support/Endian.h"
25#include "llvm/Support/Error.h"
26#include <cassert>
27#include <cstddef>
28#include <cstdint>
29#include <limits>
30#include <utility>
31
32namespace llvm {
33namespace object {
34
35struct VerdAux {
36 unsigned Offset;
37 std::string Name;
38};
39
40struct VerDef {
41 unsigned Offset;
42 unsigned Version;
43 unsigned Flags;
44 unsigned Ndx;
45 unsigned Cnt;
46 unsigned Hash;
47 std::string Name;
48 std::vector<VerdAux> AuxV;
49};
50
51struct VernAux {
52 unsigned Hash;
53 unsigned Flags;
54 unsigned Other;
55 unsigned Offset;
56 std::string Name;
57};
58
59struct VerNeed {
60 unsigned Version;
61 unsigned Cnt;
62 unsigned Offset;
63 std::string File;
64 std::vector<VernAux> AuxV;
65};
66
68 std::string Name;
70};
71
75
76// Subclasses of ELFFile may need this for template instantiation
77inline std::pair<unsigned char, unsigned char>
79 if (Object.size() < ELF::EI_NIDENT)
80 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,
81 (uint8_t)ELF::ELFDATANONE);
82 return std::make_pair((uint8_t)Object[ELF::EI_CLASS],
83 (uint8_t)Object[ELF::EI_DATA]);
84}
85
87 PADDI_R12_NO_DISP = 0x0610000039800000,
91 PLD_R12_NO_DISP = 0x04100000E5800000,
92 MTCTR_R12 = 0x7D8903A6,
93 BCTR = 0x4E800420,
94};
95
96template <class ELFT> class ELFFile;
97
98template <class T> struct DataRegion {
99 // This constructor is used when we know the start and the size of a data
100 // region. We assume that Arr does not go past the end of the file.
101 DataRegion(ArrayRef<T> Arr) : First(Arr.data()), Size(Arr.size()) {}
102
103 // Sometimes we only know the start of a data region. We still don't want to
104 // read past the end of the file, so we provide the end of a buffer.
105 DataRegion(const T *Data, const uint8_t *BufferEnd)
106 : First(Data), BufEnd(BufferEnd) {}
107
109 assert(Size || BufEnd);
110 if (Size) {
111 if (N >= *Size)
112 return createError(
113 "the index is greater than or equal to the number of entries (" +
114 Twine(*Size) + ")");
115 } else {
116 const uint8_t *EntryStart = (const uint8_t *)First + N * sizeof(T);
117 if (EntryStart + sizeof(T) > BufEnd)
118 return createError("can't read past the end of the file");
119 }
120 return *(First + N);
121 }
122
123 const T *First;
124 std::optional<uint64_t> Size;
125 const uint8_t *BufEnd = nullptr;
126};
127
128template <class ELFT>
129std::string getSecIndexForError(const ELFFile<ELFT> &Obj,
130 const typename ELFT::Shdr &Sec) {
131 auto TableOrErr = Obj.sections();
132 if (TableOrErr)
133 return "[index " + std::to_string(&Sec - &TableOrErr->front()) + "]";
134 // To make this helper be more convenient for error reporting purposes we
135 // drop the error. But really it should never be triggered. Before this point,
136 // our code should have called 'sections()' and reported a proper error on
137 // failure.
138 llvm::consumeError(TableOrErr.takeError());
139 return "[unknown index]";
140}
141
142template <class ELFT>
143static std::string describe(const ELFFile<ELFT> &Obj,
144 const typename ELFT::Shdr &Sec) {
145 unsigned SecNdx = &Sec - &cantFail(Obj.sections()).front();
146 return (object::getELFSectionTypeName(Obj.getHeader().e_machine,
147 Sec.sh_type) +
148 " section with index " + Twine(SecNdx))
149 .str();
150}
151
152template <class ELFT>
153std::string getPhdrIndexForError(const ELFFile<ELFT> &Obj,
154 const typename ELFT::Phdr &Phdr) {
155 auto Headers = Obj.program_headers();
156 if (Headers)
157 return ("[index " + Twine(&Phdr - &Headers->front()) + "]").str();
158 // See comment in the getSecIndexForError() above.
159 llvm::consumeError(Headers.takeError());
160 return "[unknown index]";
161}
162
163static inline Error defaultWarningHandler(const Twine &Msg) {
164 return createError(Msg);
165}
166
167template <class ELFT>
168class ELFFile {
169public:
171
172 // This is a callback that can be passed to a number of functions.
173 // It can be used to ignore non-critical errors (warnings), which is
174 // useful for dumpers, like llvm-readobj.
175 // It accepts a warning message string and returns a success
176 // when the warning should be ignored or an error otherwise.
178
179 const uint8_t *base() const { return Buf.bytes_begin(); }
180 const uint8_t *end() const { return base() + getBufSize(); }
181
182 size_t getBufSize() const { return Buf.size(); }
183
184private:
185 StringRef Buf;
186 std::vector<Elf_Shdr> FakeSections;
187 SmallString<0> FakeSectionStrings;
188
190
191public:
192 const Elf_Ehdr &getHeader() const {
193 return *reinterpret_cast<const Elf_Ehdr *>(base());
194 }
195
196 template <typename T>
197 Expected<const T *> getEntry(uint32_t Section, uint32_t Entry) const;
198 template <typename T>
199 Expected<const T *> getEntry(const Elf_Shdr &Section, uint32_t Entry) const;
200
202 getVersionDefinitions(const Elf_Shdr &Sec) const;
204 const Elf_Shdr &Sec,
205 WarningHandler WarnHandler = &defaultWarningHandler) const;
207 uint32_t SymbolVersionIndex, bool &IsDefault,
208 SmallVector<std::optional<VersionEntry>, 0> &VersionMap,
209 std::optional<bool> IsSymHidden) const;
210
212 getStringTable(const Elf_Shdr &Section,
213 WarningHandler WarnHandler = &defaultWarningHandler) const;
214 Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
215 Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section,
216 Elf_Shdr_Range Sections) const;
217 Expected<StringRef> getLinkAsStrtab(const typename ELFT::Shdr &Sec) const;
218
219 Expected<ArrayRef<Elf_Word>> getSHNDXTable(const Elf_Shdr &Section) const;
220 Expected<ArrayRef<Elf_Word>> getSHNDXTable(const Elf_Shdr &Section,
221 Elf_Shdr_Range Sections) const;
222
224
227 SmallVectorImpl<char> &Result) const;
229
230 std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const;
231 std::string getDynamicTagAsString(uint64_t Type) const;
232
233 /// Get the symbol for a given relocation.
235 const Elf_Shdr *SymTab) const;
236
238 loadVersionMap(const Elf_Shdr *VerNeedSec, const Elf_Shdr *VerDefSec) const;
239
241
242 bool isLE() const {
243 return getHeader().getDataEncoding() == ELF::ELFDATA2LSB;
244 }
245
246 bool isMipsELF64() const {
247 return getHeader().e_machine == ELF::EM_MIPS &&
248 getHeader().getFileClass() == ELF::ELFCLASS64;
249 }
250
251 bool isMips64EL() const { return isMipsELF64() && isLE(); }
252
254
256
259 WarningHandler WarnHandler = &defaultWarningHandler) const;
260
261 Expected<Elf_Sym_Range> symbols(const Elf_Shdr *Sec) const {
262 if (!Sec)
263 return ArrayRef<Elf_Sym>(nullptr, nullptr);
264 return getSectionContentsAsArray<Elf_Sym>(*Sec);
265 }
266
267 Expected<Elf_Rela_Range> relas(const Elf_Shdr &Sec) const {
268 return getSectionContentsAsArray<Elf_Rela>(Sec);
269 }
270
271 Expected<Elf_Rel_Range> rels(const Elf_Shdr &Sec) const {
272 return getSectionContentsAsArray<Elf_Rel>(Sec);
273 }
274
275 Expected<Elf_Relr_Range> relrs(const Elf_Shdr &Sec) const {
276 return getSectionContentsAsArray<Elf_Relr>(Sec);
277 }
278
279 std::vector<Elf_Rel> decode_relrs(Elf_Relr_Range relrs) const;
280
281 Expected<std::vector<Elf_Rela>> android_relas(const Elf_Shdr &Sec) const;
282
283 /// Iterate over program header table.
285 if (getHeader().e_phnum && getHeader().e_phentsize != sizeof(Elf_Phdr))
286 return createError("invalid e_phentsize: " +
287 Twine(getHeader().e_phentsize));
288
289 uint64_t HeadersSize =
290 (uint64_t)getHeader().e_phnum * getHeader().e_phentsize;
291 uint64_t PhOff = getHeader().e_phoff;
292 if (PhOff + HeadersSize < PhOff || PhOff + HeadersSize > getBufSize())
293 return createError("program headers are longer than binary of size " +
294 Twine(getBufSize()) + ": e_phoff = 0x" +
295 Twine::utohexstr(getHeader().e_phoff) +
296 ", e_phnum = " + Twine(getHeader().e_phnum) +
297 ", e_phentsize = " + Twine(getHeader().e_phentsize));
298
299 auto *Begin = reinterpret_cast<const Elf_Phdr *>(base() + PhOff);
300 return ArrayRef(Begin, Begin + getHeader().e_phnum);
301 }
302
303 /// Get an iterator over notes in a program header.
304 ///
305 /// The program header must be of type \c PT_NOTE.
306 ///
307 /// \param Phdr the program header to iterate over.
308 /// \param Err [out] an error to support fallible iteration, which should
309 /// be checked after iteration ends.
310 Elf_Note_Iterator notes_begin(const Elf_Phdr &Phdr, Error &Err) const {
311 assert(Phdr.p_type == ELF::PT_NOTE && "Phdr is not of type PT_NOTE");
312 ErrorAsOutParameter ErrAsOutParam(&Err);
313 if (Phdr.p_offset + Phdr.p_filesz > getBufSize()) {
314 Err =
315 createError("invalid offset (0x" + Twine::utohexstr(Phdr.p_offset) +
316 ") or size (0x" + Twine::utohexstr(Phdr.p_filesz) + ")");
317 return Elf_Note_Iterator(Err);
318 }
319 return Elf_Note_Iterator(base() + Phdr.p_offset, Phdr.p_filesz, Err);
320 }
321
322 /// Get an iterator over notes in a section.
323 ///
324 /// The section must be of type \c SHT_NOTE.
325 ///
326 /// \param Shdr the section to iterate over.
327 /// \param Err [out] an error to support fallible iteration, which should
328 /// be checked after iteration ends.
329 Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const {
330 assert(Shdr.sh_type == ELF::SHT_NOTE && "Shdr is not of type SHT_NOTE");
331 ErrorAsOutParameter ErrAsOutParam(&Err);
332 if (Shdr.sh_offset + Shdr.sh_size > getBufSize()) {
333 Err =
334 createError("invalid offset (0x" + Twine::utohexstr(Shdr.sh_offset) +
335 ") or size (0x" + Twine::utohexstr(Shdr.sh_size) + ")");
336 return Elf_Note_Iterator(Err);
337 }
338 return Elf_Note_Iterator(base() + Shdr.sh_offset, Shdr.sh_size, Err);
339 }
340
341 /// Get the end iterator for notes.
342 Elf_Note_Iterator notes_end() const {
343 return Elf_Note_Iterator();
344 }
345
346 /// Get an iterator range over notes of a program header.
347 ///
348 /// The program header must be of type \c PT_NOTE.
349 ///
350 /// \param Phdr the program header to iterate over.
351 /// \param Err [out] an error to support fallible iteration, which should
352 /// be checked after iteration ends.
354 Error &Err) const {
355 return make_range(notes_begin(Phdr, Err), notes_end());
356 }
357
358 /// Get an iterator range over notes of a section.
359 ///
360 /// The section must be of type \c SHT_NOTE.
361 ///
362 /// \param Shdr the section to iterate over.
363 /// \param Err [out] an error to support fallible iteration, which should
364 /// be checked after iteration ends.
366 Error &Err) const {
367 return make_range(notes_begin(Shdr, Err), notes_end());
368 }
369
371 Elf_Shdr_Range Sections,
372 WarningHandler WarnHandler = &defaultWarningHandler) const;
373 Expected<uint32_t> getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
374 DataRegion<Elf_Word> ShndxTable) const;
375 Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym,
376 const Elf_Shdr *SymTab,
377 DataRegion<Elf_Word> ShndxTable) const;
378 Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym,
379 Elf_Sym_Range Symtab,
380 DataRegion<Elf_Word> ShndxTable) const;
382
383 Expected<const Elf_Sym *> getSymbol(const Elf_Shdr *Sec,
384 uint32_t Index) const;
385
387 getSectionName(const Elf_Shdr &Section,
388 WarningHandler WarnHandler = &defaultWarningHandler) const;
389 Expected<StringRef> getSectionName(const Elf_Shdr &Section,
390 StringRef DotShstrtab) const;
391 template <typename T>
392 Expected<ArrayRef<T>> getSectionContentsAsArray(const Elf_Shdr &Sec) const;
393 Expected<ArrayRef<uint8_t>> getSectionContents(const Elf_Shdr &Sec) const;
394 Expected<ArrayRef<uint8_t>> getSegmentContents(const Elf_Phdr &Phdr) const;
395
396 /// Returns a vector of BBAddrMap structs corresponding to each function
397 /// within the text section that the SHT_LLVM_BB_ADDR_MAP section \p Sec
398 /// is associated with. If the current ELFFile is relocatable, a corresponding
399 /// \p RelaSec must be passed in as an argument.
401 decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec = nullptr) const;
402
403 /// Returns a map from every section matching \p IsMatch to its relocation
404 /// section, or \p nullptr if it has no relocation section. This function
405 /// returns an error if any of the \p IsMatch calls fail or if it fails to
406 /// retrieve the content section of any relocation section.
409 std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const;
410
411 void createFakeSections();
412};
413
418
419template <class ELFT>
421getSection(typename ELFT::ShdrRange Sections, uint32_t Index) {
422 if (Index >= Sections.size())
423 return createError("invalid section index: " + Twine(Index));
424 return &Sections[Index];
425}
426
427template <class ELFT>
429getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym, unsigned SymIndex,
431 assert(Sym.st_shndx == ELF::SHN_XINDEX);
432 if (!ShndxTable.First)
433 return createError(
434 "found an extended symbol index (" + Twine(SymIndex) +
435 "), but unable to locate the extended symbol index table");
436
437 Expected<typename ELFT::Word> TableOrErr = ShndxTable[SymIndex];
438 if (!TableOrErr)
439 return createError("unable to read an extended symbol table at index " +
440 Twine(SymIndex) + ": " +
441 toString(TableOrErr.takeError()));
442 return *TableOrErr;
443}
444
445template <class ELFT>
447ELFFile<ELFT>::getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
448 DataRegion<Elf_Word> ShndxTable) const {
449 uint32_t Index = Sym.st_shndx;
450 if (Index == ELF::SHN_XINDEX) {
451 Expected<uint32_t> ErrorOrIndex =
452 getExtendedSymbolTableIndex<ELFT>(Sym, &Sym - Syms.begin(), ShndxTable);
453 if (!ErrorOrIndex)
454 return ErrorOrIndex.takeError();
455 return *ErrorOrIndex;
456 }
458 return 0;
459 return Index;
460}
461
462template <class ELFT>
464ELFFile<ELFT>::getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab,
465 DataRegion<Elf_Word> ShndxTable) const {
466 auto SymsOrErr = symbols(SymTab);
467 if (!SymsOrErr)
468 return SymsOrErr.takeError();
469 return getSection(Sym, *SymsOrErr, ShndxTable);
470}
471
472template <class ELFT>
474ELFFile<ELFT>::getSection(const Elf_Sym &Sym, Elf_Sym_Range Symbols,
475 DataRegion<Elf_Word> ShndxTable) const {
476 auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable);
477 if (!IndexOrErr)
478 return IndexOrErr.takeError();
479 uint32_t Index = *IndexOrErr;
480 if (Index == 0)
481 return nullptr;
482 return getSection(Index);
483}
484
485template <class ELFT>
487ELFFile<ELFT>::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
488 auto SymsOrErr = symbols(Sec);
489 if (!SymsOrErr)
490 return SymsOrErr.takeError();
491
492 Elf_Sym_Range Symbols = *SymsOrErr;
493 if (Index >= Symbols.size())
494 return createError("unable to get symbol from section " +
495 getSecIndexForError(*this, *Sec) +
496 ": invalid symbol index (" + Twine(Index) + ")");
497 return &Symbols[Index];
498}
499
500template <class ELFT>
501template <typename T>
504 if (Sec.sh_entsize != sizeof(T) && sizeof(T) != 1)
505 return createError("section " + getSecIndexForError(*this, Sec) +
506 " has invalid sh_entsize: expected " + Twine(sizeof(T)) +
507 ", but got " + Twine(Sec.sh_entsize));
508
509 uintX_t Offset = Sec.sh_offset;
510 uintX_t Size = Sec.sh_size;
511
512 if (Size % sizeof(T))
513 return createError("section " + getSecIndexForError(*this, Sec) +
514 " has an invalid sh_size (" + Twine(Size) +
515 ") which is not a multiple of its sh_entsize (" +
516 Twine(Sec.sh_entsize) + ")");
517 if (std::numeric_limits<uintX_t>::max() - Offset < Size)
518 return createError("section " + getSecIndexForError(*this, Sec) +
519 " has a sh_offset (0x" + Twine::utohexstr(Offset) +
520 ") + sh_size (0x" + Twine::utohexstr(Size) +
521 ") that cannot be represented");
522 if (Offset + Size > Buf.size())
523 return createError("section " + getSecIndexForError(*this, Sec) +
524 " has a sh_offset (0x" + Twine::utohexstr(Offset) +
525 ") + sh_size (0x" + Twine::utohexstr(Size) +
526 ") that is greater than the file size (0x" +
527 Twine::utohexstr(Buf.size()) + ")");
528
529 if (Offset % alignof(T))
530 // TODO: this error is untested.
531 return createError("unaligned data");
532
533 const T *Start = reinterpret_cast<const T *>(base() + Offset);
534 return ArrayRef(Start, Size / sizeof(T));
535}
536
537template <class ELFT>
539ELFFile<ELFT>::getSegmentContents(const Elf_Phdr &Phdr) const {
540 uintX_t Offset = Phdr.p_offset;
541 uintX_t Size = Phdr.p_filesz;
542
543 if (std::numeric_limits<uintX_t>::max() - Offset < Size)
544 return createError("program header " + getPhdrIndexForError(*this, Phdr) +
545 " has a p_offset (0x" + Twine::utohexstr(Offset) +
546 ") + p_filesz (0x" + Twine::utohexstr(Size) +
547 ") that cannot be represented");
548 if (Offset + Size > Buf.size())
549 return createError("program header " + getPhdrIndexForError(*this, Phdr) +
550 " has a p_offset (0x" + Twine::utohexstr(Offset) +
551 ") + p_filesz (0x" + Twine::utohexstr(Size) +
552 ") that is greater than the file size (0x" +
553 Twine::utohexstr(Buf.size()) + ")");
554 return ArrayRef(base() + Offset, Size);
555}
556
557template <class ELFT>
559ELFFile<ELFT>::getSectionContents(const Elf_Shdr &Sec) const {
560 return getSectionContentsAsArray<uint8_t>(Sec);
561}
562
563template <class ELFT>
565 return getELFRelocationTypeName(getHeader().e_machine, Type);
566}
567
568template <class ELFT>
570 SmallVectorImpl<char> &Result) const {
571 if (!isMipsELF64()) {
572 StringRef Name = getRelocationTypeName(Type);
573 Result.append(Name.begin(), Name.end());
574 } else {
575 // The Mips N64 ABI allows up to three operations to be specified per
576 // relocation record. Unfortunately there's no easy way to test for the
577 // presence of N64 ELFs as they have no special flag that identifies them
578 // as being N64. We can safely assume at the moment that all Mips
579 // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
580 // information to disambiguate between old vs new ABIs.
581 uint8_t Type1 = (Type >> 0) & 0xFF;
582 uint8_t Type2 = (Type >> 8) & 0xFF;
583 uint8_t Type3 = (Type >> 16) & 0xFF;
584
585 // Concat all three relocation type names.
586 StringRef Name = getRelocationTypeName(Type1);
587 Result.append(Name.begin(), Name.end());
588
589 Name = getRelocationTypeName(Type2);
590 Result.append(1, '/');
591 Result.append(Name.begin(), Name.end());
592
593 Name = getRelocationTypeName(Type3);
594 Result.append(1, '/');
595 Result.append(Name.begin(), Name.end());
596 }
597}
598
599template <class ELFT>
601 return getELFRelativeRelocationType(getHeader().e_machine);
602}
603
604template <class ELFT>
606ELFFile<ELFT>::loadVersionMap(const Elf_Shdr *VerNeedSec,
607 const Elf_Shdr *VerDefSec) const {
609
610 // The first two version indexes are reserved.
611 // Index 0 is VER_NDX_LOCAL, index 1 is VER_NDX_GLOBAL.
612 VersionMap.push_back(VersionEntry());
613 VersionMap.push_back(VersionEntry());
614
615 auto InsertEntry = [&](unsigned N, StringRef Version, bool IsVerdef) {
616 if (N >= VersionMap.size())
617 VersionMap.resize(N + 1);
618 VersionMap[N] = {std::string(Version), IsVerdef};
619 };
620
621 if (VerDefSec) {
622 Expected<std::vector<VerDef>> Defs = getVersionDefinitions(*VerDefSec);
623 if (!Defs)
624 return Defs.takeError();
625 for (const VerDef &Def : *Defs)
626 InsertEntry(Def.Ndx & ELF::VERSYM_VERSION, Def.Name, true);
627 }
628
629 if (VerNeedSec) {
630 Expected<std::vector<VerNeed>> Deps = getVersionDependencies(*VerNeedSec);
631 if (!Deps)
632 return Deps.takeError();
633 for (const VerNeed &Dep : *Deps)
634 for (const VernAux &Aux : Dep.AuxV)
635 InsertEntry(Aux.Other & ELF::VERSYM_VERSION, Aux.Name, false);
636 }
637
638 return VersionMap;
639}
640
641template <class ELFT>
644 const Elf_Shdr *SymTab) const {
645 uint32_t Index = Rel.getSymbol(isMips64EL());
646 if (Index == 0)
647 return nullptr;
648 return getEntry<Elf_Sym>(*SymTab, Index);
649}
650
651template <class ELFT>
654 WarningHandler WarnHandler) const {
655 uint32_t Index = getHeader().e_shstrndx;
656 if (Index == ELF::SHN_XINDEX) {
657 // If the section name string table section index is greater than
658 // or equal to SHN_LORESERVE, then the actual index of the section name
659 // string table section is contained in the sh_link field of the section
660 // header at index 0.
661 if (Sections.empty())
662 return createError(
663 "e_shstrndx == SHN_XINDEX, but the section header table is empty");
664
665 Index = Sections[0].sh_link;
666 }
667
668 // There is no section name string table. Return FakeSectionStrings which
669 // is non-empty if we have created fake sections.
670 if (!Index)
671 return FakeSectionStrings;
672
673 if (Index >= Sections.size())
674 return createError("section header string table index " + Twine(Index) +
675 " does not exist");
676 return getStringTable(Sections[Index], WarnHandler);
677}
678
679/// This function finds the number of dynamic symbols using a GNU hash table.
680///
681/// @param Table The GNU hash table for .dynsym.
682template <class ELFT>
684getDynSymtabSizeFromGnuHash(const typename ELFT::GnuHash &Table,
685 const void *BufEnd) {
686 using Elf_Word = typename ELFT::Word;
687 if (Table.nbuckets == 0)
688 return Table.symndx + 1;
689 uint64_t LastSymIdx = 0;
690 // Find the index of the first symbol in the last chain.
691 for (Elf_Word Val : Table.buckets())
692 LastSymIdx = std::max(LastSymIdx, (uint64_t)Val);
693 const Elf_Word *It =
694 reinterpret_cast<const Elf_Word *>(Table.values(LastSymIdx).end());
695 // Locate the end of the chain to find the last symbol index.
696 while (It < BufEnd && (*It & 1) == 0) {
697 ++LastSymIdx;
698 ++It;
699 }
700 if (It >= BufEnd) {
701 return createStringError(
703 "no terminator found for GNU hash section before buffer end");
704 }
705 return LastSymIdx + 1;
706}
707
708/// This function determines the number of dynamic symbols. It reads section
709/// headers first. If section headers are not available, the number of
710/// symbols will be inferred by parsing dynamic hash tables.
711template <class ELFT>
713 // Read .dynsym section header first if available.
714 Expected<Elf_Shdr_Range> SectionsOrError = sections();
715 if (!SectionsOrError)
716 return SectionsOrError.takeError();
717 for (const Elf_Shdr &Sec : *SectionsOrError) {
718 if (Sec.sh_type == ELF::SHT_DYNSYM) {
719 if (Sec.sh_size % Sec.sh_entsize != 0) {
721 "SHT_DYNSYM section has sh_size (" +
722 Twine(Sec.sh_size) + ") % sh_entsize (" +
723 Twine(Sec.sh_entsize) + ") that is not 0");
724 }
725 return Sec.sh_size / Sec.sh_entsize;
726 }
727 }
728
729 if (!SectionsOrError->empty()) {
730 // Section headers are available but .dynsym header is not found.
731 // Return 0 as .dynsym does not exist.
732 return 0;
733 }
734
735 // Section headers do not exist. Falling back to infer
736 // upper bound of .dynsym from .gnu.hash and .hash.
737 Expected<Elf_Dyn_Range> DynTable = dynamicEntries();
738 if (!DynTable)
739 return DynTable.takeError();
740 std::optional<uint64_t> ElfHash;
741 std::optional<uint64_t> ElfGnuHash;
742 for (const Elf_Dyn &Entry : *DynTable) {
743 switch (Entry.d_tag) {
744 case ELF::DT_HASH:
745 ElfHash = Entry.d_un.d_ptr;
746 break;
747 case ELF::DT_GNU_HASH:
748 ElfGnuHash = Entry.d_un.d_ptr;
749 break;
750 }
751 }
752 if (ElfGnuHash) {
753 Expected<const uint8_t *> TablePtr = toMappedAddr(*ElfGnuHash);
754 if (!TablePtr)
755 return TablePtr.takeError();
756 const Elf_GnuHash *Table =
757 reinterpret_cast<const Elf_GnuHash *>(TablePtr.get());
758 return getDynSymtabSizeFromGnuHash<ELFT>(*Table, this->Buf.bytes_end());
759 }
760
761 // Search SYSV hash table to try to find the upper bound of dynsym.
762 if (ElfHash) {
763 Expected<const uint8_t *> TablePtr = toMappedAddr(*ElfHash);
764 if (!TablePtr)
765 return TablePtr.takeError();
766 const Elf_Hash *Table = reinterpret_cast<const Elf_Hash *>(TablePtr.get());
767 return Table->nchain;
768 }
769 return 0;
770}
771
772template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
773
774template <class ELFT>
776 if (sizeof(Elf_Ehdr) > Object.size())
777 return createError("invalid buffer: the size (" + Twine(Object.size()) +
778 ") is smaller than an ELF header (" +
779 Twine(sizeof(Elf_Ehdr)) + ")");
780 return ELFFile(Object);
781}
782
783/// Used by llvm-objdump -d (which needs sections for disassembly) to
784/// disassemble objects without a section header table (e.g. ET_CORE objects
785/// analyzed by linux perf or ET_EXEC with llvm-strip --strip-sections).
786template <class ELFT> void ELFFile<ELFT>::createFakeSections() {
787 if (!FakeSections.empty())
788 return;
789 auto PhdrsOrErr = program_headers();
790 if (!PhdrsOrErr)
791 return;
792
793 FakeSectionStrings += '\0';
794 for (auto [Idx, Phdr] : llvm::enumerate(*PhdrsOrErr)) {
795 if (Phdr.p_type != ELF::PT_LOAD || !(Phdr.p_flags & ELF::PF_X))
796 continue;
797 Elf_Shdr FakeShdr = {};
798 FakeShdr.sh_type = ELF::SHT_PROGBITS;
799 FakeShdr.sh_flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
800 FakeShdr.sh_addr = Phdr.p_vaddr;
801 FakeShdr.sh_size = Phdr.p_memsz;
802 FakeShdr.sh_offset = Phdr.p_offset;
803 // Create a section name based on the p_type and index.
804 FakeShdr.sh_name = FakeSectionStrings.size();
805 FakeSectionStrings += ("PT_LOAD#" + Twine(Idx)).str();
806 FakeSectionStrings += '\0';
807 FakeSections.push_back(FakeShdr);
808 }
809}
810
811template <class ELFT>
813 const uintX_t SectionTableOffset = getHeader().e_shoff;
814 if (SectionTableOffset == 0) {
815 if (!FakeSections.empty())
816 return ArrayRef(FakeSections.data(), FakeSections.size());
817 return ArrayRef<Elf_Shdr>();
818 }
819
820 if (getHeader().e_shentsize != sizeof(Elf_Shdr))
821 return createError("invalid e_shentsize in ELF header: " +
822 Twine(getHeader().e_shentsize));
823
824 const uint64_t FileSize = Buf.size();
825 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize ||
826 SectionTableOffset + (uintX_t)sizeof(Elf_Shdr) < SectionTableOffset)
827 return createError(
828 "section header table goes past the end of the file: e_shoff = 0x" +
829 Twine::utohexstr(SectionTableOffset));
830
831 // Invalid address alignment of section headers
832 if (SectionTableOffset & (alignof(Elf_Shdr) - 1))
833 // TODO: this error is untested.
834 return createError("invalid alignment of section headers");
835
836 const Elf_Shdr *First =
837 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
838
839 uintX_t NumSections = getHeader().e_shnum;
840 if (NumSections == 0)
841 NumSections = First->sh_size;
842
843 if (NumSections > UINT64_MAX / sizeof(Elf_Shdr))
844 return createError("invalid number of sections specified in the NULL "
845 "section's sh_size field (" +
846 Twine(NumSections) + ")");
847
848 const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr);
849 if (SectionTableOffset + SectionTableSize < SectionTableOffset)
850 return createError(
851 "invalid section header table offset (e_shoff = 0x" +
852 Twine::utohexstr(SectionTableOffset) +
853 ") or invalid number of sections specified in the first section "
854 "header's sh_size field (0x" +
855 Twine::utohexstr(NumSections) + ")");
856
857 // Section table goes past end of file!
858 if (SectionTableOffset + SectionTableSize > FileSize)
859 return createError("section table goes past the end of file");
860 return ArrayRef(First, NumSections);
861}
862
863template <class ELFT>
864template <typename T>
866 uint32_t Entry) const {
867 auto SecOrErr = getSection(Section);
868 if (!SecOrErr)
869 return SecOrErr.takeError();
870 return getEntry<T>(**SecOrErr, Entry);
871}
872
873template <class ELFT>
874template <typename T>
876 uint32_t Entry) const {
877 Expected<ArrayRef<T>> EntriesOrErr = getSectionContentsAsArray<T>(Section);
878 if (!EntriesOrErr)
879 return EntriesOrErr.takeError();
880
881 ArrayRef<T> Arr = *EntriesOrErr;
882 if (Entry >= Arr.size())
883 return createError(
884 "can't read an entry at 0x" +
885 Twine::utohexstr(Entry * static_cast<uint64_t>(sizeof(T))) +
886 ": it goes past the end of the section (0x" +
887 Twine::utohexstr(Section.sh_size) + ")");
888 return &Arr[Entry];
889}
890
891template <typename ELFT>
893 uint32_t SymbolVersionIndex, bool &IsDefault,
894 SmallVector<std::optional<VersionEntry>, 0> &VersionMap,
895 std::optional<bool> IsSymHidden) const {
896 size_t VersionIndex = SymbolVersionIndex & llvm::ELF::VERSYM_VERSION;
897
898 // Special markers for unversioned symbols.
899 if (VersionIndex == llvm::ELF::VER_NDX_LOCAL ||
900 VersionIndex == llvm::ELF::VER_NDX_GLOBAL) {
901 IsDefault = false;
902 return "";
903 }
904
905 // Lookup this symbol in the version table.
906 if (VersionIndex >= VersionMap.size() || !VersionMap[VersionIndex])
907 return createError("SHT_GNU_versym section refers to a version index " +
908 Twine(VersionIndex) + " which is missing");
909
910 const VersionEntry &Entry = *VersionMap[VersionIndex];
911 // A default version (@@) is only available for defined symbols.
912 if (!Entry.IsVerDef || IsSymHidden.value_or(false))
913 IsDefault = false;
914 else
915 IsDefault = !(SymbolVersionIndex & llvm::ELF::VERSYM_HIDDEN);
916 return Entry.Name.c_str();
917}
918
919template <class ELFT>
921ELFFile<ELFT>::getVersionDefinitions(const Elf_Shdr &Sec) const {
922 Expected<StringRef> StrTabOrErr = getLinkAsStrtab(Sec);
923 if (!StrTabOrErr)
924 return StrTabOrErr.takeError();
925
926 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
927 if (!ContentsOrErr)
928 return createError("cannot read content of " + describe(*this, Sec) + ": " +
929 toString(ContentsOrErr.takeError()));
930
931 const uint8_t *Start = ContentsOrErr->data();
932 const uint8_t *End = Start + ContentsOrErr->size();
933
934 auto ExtractNextAux = [&](const uint8_t *&VerdauxBuf,
935 unsigned VerDefNdx) -> Expected<VerdAux> {
936 if (VerdauxBuf + sizeof(Elf_Verdaux) > End)
937 return createError("invalid " + describe(*this, Sec) +
938 ": version definition " + Twine(VerDefNdx) +
939 " refers to an auxiliary entry that goes past the end "
940 "of the section");
941
942 auto *Verdaux = reinterpret_cast<const Elf_Verdaux *>(VerdauxBuf);
943 VerdauxBuf += Verdaux->vda_next;
944
945 VerdAux Aux;
946 Aux.Offset = VerdauxBuf - Start;
947 if (Verdaux->vda_name <= StrTabOrErr->size())
948 Aux.Name = std::string(StrTabOrErr->drop_front(Verdaux->vda_name));
949 else
950 Aux.Name = ("<invalid vda_name: " + Twine(Verdaux->vda_name) + ">").str();
951 return Aux;
952 };
953
954 std::vector<VerDef> Ret;
955 const uint8_t *VerdefBuf = Start;
956 for (unsigned I = 1; I <= /*VerDefsNum=*/Sec.sh_info; ++I) {
957 if (VerdefBuf + sizeof(Elf_Verdef) > End)
958 return createError("invalid " + describe(*this, Sec) +
959 ": version definition " + Twine(I) +
960 " goes past the end of the section");
961
962 if (reinterpret_cast<uintptr_t>(VerdefBuf) % sizeof(uint32_t) != 0)
963 return createError(
964 "invalid " + describe(*this, Sec) +
965 ": found a misaligned version definition entry at offset 0x" +
966 Twine::utohexstr(VerdefBuf - Start));
967
968 unsigned Version = *reinterpret_cast<const Elf_Half *>(VerdefBuf);
969 if (Version != 1)
970 return createError("unable to dump " + describe(*this, Sec) +
971 ": version " + Twine(Version) +
972 " is not yet supported");
973
974 const Elf_Verdef *D = reinterpret_cast<const Elf_Verdef *>(VerdefBuf);
975 VerDef &VD = *Ret.emplace(Ret.end());
976 VD.Offset = VerdefBuf - Start;
977 VD.Version = D->vd_version;
978 VD.Flags = D->vd_flags;
979 VD.Ndx = D->vd_ndx;
980 VD.Cnt = D->vd_cnt;
981 VD.Hash = D->vd_hash;
982
983 const uint8_t *VerdauxBuf = VerdefBuf + D->vd_aux;
984 for (unsigned J = 0; J < D->vd_cnt; ++J) {
985 if (reinterpret_cast<uintptr_t>(VerdauxBuf) % sizeof(uint32_t) != 0)
986 return createError("invalid " + describe(*this, Sec) +
987 ": found a misaligned auxiliary entry at offset 0x" +
988 Twine::utohexstr(VerdauxBuf - Start));
989
990 Expected<VerdAux> AuxOrErr = ExtractNextAux(VerdauxBuf, I);
991 if (!AuxOrErr)
992 return AuxOrErr.takeError();
993
994 if (J == 0)
995 VD.Name = AuxOrErr->Name;
996 else
997 VD.AuxV.push_back(*AuxOrErr);
998 }
999
1000 VerdefBuf += D->vd_next;
1001 }
1002
1003 return Ret;
1004}
1005
1006template <class ELFT>
1009 WarningHandler WarnHandler) const {
1010 StringRef StrTab;
1011 Expected<StringRef> StrTabOrErr = getLinkAsStrtab(Sec);
1012 if (!StrTabOrErr) {
1013 if (Error E = WarnHandler(toString(StrTabOrErr.takeError())))
1014 return std::move(E);
1015 } else {
1016 StrTab = *StrTabOrErr;
1017 }
1018
1019 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
1020 if (!ContentsOrErr)
1021 return createError("cannot read content of " + describe(*this, Sec) + ": " +
1022 toString(ContentsOrErr.takeError()));
1023
1024 const uint8_t *Start = ContentsOrErr->data();
1025 const uint8_t *End = Start + ContentsOrErr->size();
1026 const uint8_t *VerneedBuf = Start;
1027
1028 std::vector<VerNeed> Ret;
1029 for (unsigned I = 1; I <= /*VerneedNum=*/Sec.sh_info; ++I) {
1030 if (VerneedBuf + sizeof(Elf_Verdef) > End)
1031 return createError("invalid " + describe(*this, Sec) +
1032 ": version dependency " + Twine(I) +
1033 " goes past the end of the section");
1034
1035 if (reinterpret_cast<uintptr_t>(VerneedBuf) % sizeof(uint32_t) != 0)
1036 return createError(
1037 "invalid " + describe(*this, Sec) +
1038 ": found a misaligned version dependency entry at offset 0x" +
1039 Twine::utohexstr(VerneedBuf - Start));
1040
1041 unsigned Version = *reinterpret_cast<const Elf_Half *>(VerneedBuf);
1042 if (Version != 1)
1043 return createError("unable to dump " + describe(*this, Sec) +
1044 ": version " + Twine(Version) +
1045 " is not yet supported");
1046
1047 const Elf_Verneed *Verneed =
1048 reinterpret_cast<const Elf_Verneed *>(VerneedBuf);
1049
1050 VerNeed &VN = *Ret.emplace(Ret.end());
1051 VN.Version = Verneed->vn_version;
1052 VN.Cnt = Verneed->vn_cnt;
1053 VN.Offset = VerneedBuf - Start;
1054
1055 if (Verneed->vn_file < StrTab.size())
1056 VN.File = std::string(StrTab.data() + Verneed->vn_file);
1057 else
1058 VN.File = ("<corrupt vn_file: " + Twine(Verneed->vn_file) + ">").str();
1059
1060 const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux;
1061 for (unsigned J = 0; J < Verneed->vn_cnt; ++J) {
1062 if (reinterpret_cast<uintptr_t>(VernauxBuf) % sizeof(uint32_t) != 0)
1063 return createError("invalid " + describe(*this, Sec) +
1064 ": found a misaligned auxiliary entry at offset 0x" +
1065 Twine::utohexstr(VernauxBuf - Start));
1066
1067 if (VernauxBuf + sizeof(Elf_Vernaux) > End)
1068 return createError(
1069 "invalid " + describe(*this, Sec) + ": version dependency " +
1070 Twine(I) +
1071 " refers to an auxiliary entry that goes past the end "
1072 "of the section");
1073
1074 const Elf_Vernaux *Vernaux =
1075 reinterpret_cast<const Elf_Vernaux *>(VernauxBuf);
1076
1077 VernAux &Aux = *VN.AuxV.emplace(VN.AuxV.end());
1078 Aux.Hash = Vernaux->vna_hash;
1079 Aux.Flags = Vernaux->vna_flags;
1080 Aux.Other = Vernaux->vna_other;
1081 Aux.Offset = VernauxBuf - Start;
1082 if (StrTab.size() <= Vernaux->vna_name)
1083 Aux.Name = "<corrupt>";
1084 else
1085 Aux.Name = std::string(StrTab.drop_front(Vernaux->vna_name));
1086
1087 VernauxBuf += Vernaux->vna_next;
1088 }
1089 VerneedBuf += Verneed->vn_next;
1090 }
1091 return Ret;
1092}
1093
1094template <class ELFT>
1097 auto TableOrErr = sections();
1098 if (!TableOrErr)
1099 return TableOrErr.takeError();
1100 return object::getSection<ELFT>(*TableOrErr, Index);
1101}
1102
1103template <class ELFT>
1105ELFFile<ELFT>::getStringTable(const Elf_Shdr &Section,
1106 WarningHandler WarnHandler) const {
1107 if (Section.sh_type != ELF::SHT_STRTAB)
1108 if (Error E = WarnHandler("invalid sh_type for string table section " +
1109 getSecIndexForError(*this, Section) +
1110 ": expected SHT_STRTAB, but got " +
1112 getHeader().e_machine, Section.sh_type)))
1113 return std::move(E);
1114
1115 auto V = getSectionContentsAsArray<char>(Section);
1116 if (!V)
1117 return V.takeError();
1118 ArrayRef<char> Data = *V;
1119 if (Data.empty())
1120 return createError("SHT_STRTAB string table section " +
1121 getSecIndexForError(*this, Section) + " is empty");
1122 if (Data.back() != '\0')
1123 return createError("SHT_STRTAB string table section " +
1124 getSecIndexForError(*this, Section) +
1125 " is non-null terminated");
1126 return StringRef(Data.begin(), Data.size());
1127}
1128
1129template <class ELFT>
1131ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section) const {
1132 auto SectionsOrErr = sections();
1133 if (!SectionsOrErr)
1134 return SectionsOrErr.takeError();
1135 return getSHNDXTable(Section, *SectionsOrErr);
1136}
1137
1138template <class ELFT>
1140ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section,
1141 Elf_Shdr_Range Sections) const {
1142 assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
1143 auto VOrErr = getSectionContentsAsArray<Elf_Word>(Section);
1144 if (!VOrErr)
1145 return VOrErr.takeError();
1146 ArrayRef<Elf_Word> V = *VOrErr;
1147 auto SymTableOrErr = object::getSection<ELFT>(Sections, Section.sh_link);
1148 if (!SymTableOrErr)
1149 return SymTableOrErr.takeError();
1150 const Elf_Shdr &SymTable = **SymTableOrErr;
1151 if (SymTable.sh_type != ELF::SHT_SYMTAB &&
1152 SymTable.sh_type != ELF::SHT_DYNSYM)
1153 return createError(
1154 "SHT_SYMTAB_SHNDX section is linked with " +
1155 object::getELFSectionTypeName(getHeader().e_machine, SymTable.sh_type) +
1156 " section (expected SHT_SYMTAB/SHT_DYNSYM)");
1157
1158 uint64_t Syms = SymTable.sh_size / sizeof(Elf_Sym);
1159 if (V.size() != Syms)
1160 return createError("SHT_SYMTAB_SHNDX has " + Twine(V.size()) +
1161 " entries, but the symbol table associated has " +
1162 Twine(Syms));
1163
1164 return V;
1165}
1166
1167template <class ELFT>
1169ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
1170 auto SectionsOrErr = sections();
1171 if (!SectionsOrErr)
1172 return SectionsOrErr.takeError();
1173 return getStringTableForSymtab(Sec, *SectionsOrErr);
1174}
1175
1176template <class ELFT>
1179 Elf_Shdr_Range Sections) const {
1180
1181 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
1182 return createError(
1183 "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
1184 Expected<const Elf_Shdr *> SectionOrErr =
1185 object::getSection<ELFT>(Sections, Sec.sh_link);
1186 if (!SectionOrErr)
1187 return SectionOrErr.takeError();
1188 return getStringTable(**SectionOrErr);
1189}
1190
1191template <class ELFT>
1193ELFFile<ELFT>::getLinkAsStrtab(const typename ELFT::Shdr &Sec) const {
1195 getSection(Sec.sh_link);
1196 if (!StrTabSecOrErr)
1197 return createError("invalid section linked to " + describe(*this, Sec) +
1198 ": " + toString(StrTabSecOrErr.takeError()));
1199
1200 Expected<StringRef> StrTabOrErr = getStringTable(**StrTabSecOrErr);
1201 if (!StrTabOrErr)
1202 return createError("invalid string table linked to " +
1203 describe(*this, Sec) + ": " +
1204 toString(StrTabOrErr.takeError()));
1205 return *StrTabOrErr;
1206}
1207
1208template <class ELFT>
1210ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section,
1211 WarningHandler WarnHandler) const {
1212 auto SectionsOrErr = sections();
1213 if (!SectionsOrErr)
1214 return SectionsOrErr.takeError();
1215 auto Table = getSectionStringTable(*SectionsOrErr, WarnHandler);
1216 if (!Table)
1217 return Table.takeError();
1218 return getSectionName(Section, *Table);
1219}
1220
1221template <class ELFT>
1223 StringRef DotShstrtab) const {
1224 uint32_t Offset = Section.sh_name;
1225 if (Offset == 0)
1226 return StringRef();
1227 if (Offset >= DotShstrtab.size())
1228 return createError("a section " + getSecIndexForError(*this, Section) +
1229 " has an invalid sh_name (0x" +
1231 ") offset which goes past the end of the "
1232 "section name string table");
1233 return StringRef(DotShstrtab.data() + Offset);
1234}
1235
1236/// This function returns the hash value for a symbol in the .dynsym section
1237/// Name of the API remains consistent as specified in the libelf
1238/// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
1239inline unsigned hashSysV(StringRef SymbolName) {
1240 unsigned h = 0, g;
1241 for (char C : SymbolName) {
1242 h = (h << 4) + C;
1243 g = h & 0xf0000000L;
1244 if (g != 0)
1245 h ^= g >> 24;
1246 h &= ~g;
1247 }
1248 return h;
1249}
1250
1251/// This function returns the hash value for a symbol in the .dynsym section
1252/// for the GNU hash table. The implementation is defined in the GNU hash ABI.
1253/// REF : https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elf.c#l222
1255 uint32_t H = 5381;
1256 for (uint8_t C : Name)
1257 H = (H << 5) + H + C;
1258 return H;
1259}
1260
1261} // end namespace object
1262} // end namespace llvm
1263
1264#endif // LLVM_OBJECT_ELF_H
aarch64 promote const
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static bool isMips64EL(const ELFYAML::Object &Obj)
Elf_Shdr Shdr
std::string Name
uint64_t Size
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:104
ELFYAML::ELF_REL Type3
Definition: ELFYAML.cpp:1780
ELFYAML::ELF_REL Type2
Definition: ELFYAML.cpp:1779
#define I(x, y, z)
Definition: MD5.cpp:58
#define H(x, y, z)
Definition: MD5.cpp:57
This file implements a map that provides insertion order iteration.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
This file defines the SmallVector class.
INLINE void g(uint32_t *state, size_t a, size_t b, size_t c, size_t d, uint32_t x, uint32_t y)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
Helper for Errors used as out-parameters.
Definition: Error.h:1104
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
reference get()
Returns a reference to the stored T value.
Definition: Error.h:567
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
void resize(size_type N)
Definition: SmallVector.h:642
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
const unsigned char * bytes_end() const
Definition: StringRef.h:118
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:596
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
const unsigned char * bytes_begin() const
Definition: StringRef.h:115
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:404
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
An efficient, type-erasing, non-owning reference to a callable.
A range adaptor for a pair of iterators.
const Elf_Ehdr & getHeader() const
Definition: ELF.h:192
Expected< std::vector< Elf_Rela > > android_relas(const Elf_Shdr &Sec) const
Definition: ELF.cpp:386
Expected< StringRef > getLinkAsStrtab(const typename ELFT::Shdr &Sec) const
Definition: ELF.h:1193
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:775
Expected< const Elf_Sym * > getSymbol(const Elf_Shdr *Sec, uint32_t Index) const
Definition: ELF.h:487
Expected< std::vector< VerDef > > getVersionDefinitions(const Elf_Shdr &Sec) const
Definition: ELF.h:921
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
Definition: ELF.cpp:454
Expected< ArrayRef< Elf_Word > > getSHNDXTable(const Elf_Shdr &Section) const
Definition: ELF.h:1131
Expected< Elf_Sym_Range > symbols(const Elf_Shdr *Sec) const
Definition: ELF.h:261
Expected< std::vector< BBAddrMap > > decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec=nullptr) const
Returns a vector of BBAddrMap structs corresponding to each function within the text section that the...
Definition: ELF.cpp:643
Expected< ArrayRef< uint8_t > > getSegmentContents(const Elf_Phdr &Phdr) const
Definition: ELF.h:539
Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const
Get an iterator over notes in a section.
Definition: ELF.h:329
uint32_t getRelativeRelocationType() const
Definition: ELF.h:600
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:353
Expected< StringRef > getSymbolVersionByIndex(uint32_t SymbolVersionIndex, bool &IsDefault, SmallVector< std::optional< VersionEntry >, 0 > &VersionMap, std::optional< bool > IsSymHidden) const
Definition: ELF.h:892
Elf_Note_Iterator notes_begin(const Elf_Phdr &Phdr, Error &Err) const
Get an iterator over notes in a program header.
Definition: ELF.h:310
Expected< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr &Sec) const
Definition: ELF.h:559
Expected< Elf_Rela_Range > relas(const Elf_Shdr &Sec) const
Definition: ELF.h:267
Expected< Elf_Phdr_Range > program_headers() const
Iterate over program header table.
Definition: ELF.h:284
Expected< StringRef > getStringTableForSymtab(const Elf_Shdr &Section) const
Definition: ELF.h:1169
Expected< std::vector< VerNeed > > getVersionDependencies(const Elf_Shdr &Sec, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:1008
size_t getBufSize() const
Definition: ELF.h:182
Expected< const T * > getEntry(uint32_t Section, uint32_t Entry) const
Definition: ELF.h:865
Expected< const Elf_Sym * > getRelocationSymbol(const Elf_Rel &Rel, const Elf_Shdr *SymTab) const
Get the symbol for a given relocation.
Definition: ELF.h:643
const uint8_t * end() const
Definition: ELF.h:180
Expected< StringRef > getSectionStringTable(Elf_Shdr_Range Sections, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:653
Expected< uint64_t > getDynSymtabSize() const
This function determines the number of dynamic symbols.
Definition: ELF.h:712
Expected< Elf_Dyn_Range > dynamicEntries() const
Definition: ELF.cpp:543
void createFakeSections()
Used by llvm-objdump -d (which needs sections for disassembly) to disassemble objects without a secti...
Definition: ELF.h:786
Expected< Elf_Shdr_Range > sections() const
Definition: ELF.h:812
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:365
const uint8_t * base() const
Definition: ELF.h:179
bool isMipsELF64() const
Definition: ELF.h:246
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.cpp:591
Expected< Elf_Relr_Range > relrs(const Elf_Shdr &Sec) const
Definition: ELF.h:275
Expected< MapVector< const Elf_Shdr *, const Elf_Shdr * > > getSectionAndRelocations(std::function< Expected< bool >(const Elf_Shdr &)> IsMatch) const
Returns a map from every section matching IsMatch to its relocation section, or nullptr if it has no ...
Definition: ELF.cpp:744
bool isLE() const
Definition: ELF.h:242
bool isMips64EL() const
Definition: ELF.h:251
Elf_Note_Iterator notes_end() const
Get the end iterator for notes.
Definition: ELF.h:342
Expected< StringRef > getSectionName(const Elf_Shdr &Section, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:1210
StringRef getRelocationTypeName(uint32_t Type) const
Definition: ELF.h:564
Expected< StringRef > getStringTable(const Elf_Shdr &Section, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:1105
llvm::function_ref< Error(const Twine &Msg)> WarningHandler
Definition: ELF.h:177
Expected< ArrayRef< T > > getSectionContentsAsArray(const Elf_Shdr &Sec) const
Definition: ELF.h:503
Expected< SmallVector< std::optional< VersionEntry >, 0 > > loadVersionMap(const Elf_Shdr *VerNeedSec, const Elf_Shdr *VerDefSec) const
Definition: ELF.h:606
Expected< Elf_Rel_Range > rels(const Elf_Shdr &Sec) const
Definition: ELF.h:271
Expected< const Elf_Shdr * > getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab, DataRegion< Elf_Word > ShndxTable) const
Definition: ELF.h:464
std::vector< Elf_Rel > decode_relrs(Elf_Relr_Range relrs) const
Definition: ELF.cpp:322
Expected< uint32_t > getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms, DataRegion< Elf_Word > ShndxTable) const
Definition: ELF.h:447
#define UINT64_MAX
Definition: DataTypes.h:77
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SHF_ALLOC
Definition: ELF.h:1083
@ SHF_EXECINSTR
Definition: ELF.h:1086
@ PF_X
Definition: ELF.h:1429
@ ELFCLASS64
Definition: ELF.h:329
@ ELFCLASSNONE
Definition: ELF.h:327
@ EM_MIPS
Definition: ELF.h:141
@ ELFDATANONE
Definition: ELF.h:334
@ ELFDATA2LSB
Definition: ELF.h:335
@ SHN_XINDEX
Definition: ELF.h:988
@ SHN_UNDEF
Definition: ELF.h:980
@ SHN_LORESERVE
Definition: ELF.h:981
@ SHT_STRTAB
Definition: ELF.h:997
@ SHT_PROGBITS
Definition: ELF.h:995
@ SHT_SYMTAB
Definition: ELF.h:996
@ SHT_SYMTAB_SHNDX
Definition: ELF.h:1010
@ SHT_NOTE
Definition: ELF.h:1001
@ SHT_DYNSYM
Definition: ELF.h:1005
@ PT_LOAD
Definition: ELF.h:1382
@ PT_NOTE
Definition: ELF.h:1385
@ EI_DATA
Definition: ELF.h:53
@ EI_NIDENT
Definition: ELF.h:58
@ EI_CLASS
Definition: ELF.h:52
@ VER_NDX_GLOBAL
Definition: ELF.h:1535
@ VERSYM_VERSION
Definition: ELF.h:1536
@ VER_NDX_LOCAL
Definition: ELF.h:1534
@ VERSYM_HIDDEN
Definition: ELF.h:1537
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
Expected< uint32_t > getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym, unsigned SymIndex, DataRegion< typename ELFT::Word > ShndxTable)
Definition: ELF.h:429
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:421
Error createError(const Twine &Err)
Definition: Error.h:84
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:22
static Expected< uint64_t > getDynSymtabSizeFromGnuHash(const typename ELFT::GnuHash &Table, const void *BufEnd)
This function finds the number of dynamic symbols using a GNU hash table.
Definition: ELF.h:684
uint32_t getELFRelativeRelocationType(uint32_t Machine)
Definition: ELF.cpp:191
static std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
Definition: ELF.h:143
std::string getPhdrIndexForError(const ELFFile< ELFT > &Obj, const typename ELFT::Phdr &Phdr)
Definition: ELF.h:153
uint32_t hashGnu(StringRef Name)
This function returns the hash value for a symbol in the .dynsym section for the GNU hash table.
Definition: ELF.h:1254
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
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:1239
PPCInstrMasks
Definition: ELF.h:86
@ PLD_R12_NO_DISP
Definition: ELF.h:91
@ ADDIS_R12_TO_R2_NO_DISP
Definition: ELF.h:88
@ ADDI_R12_TO_R12_NO_DISP
Definition: ELF.h:90
@ ADDI_R12_TO_R2_NO_DISP
Definition: ELF.h:89
@ PADDI_R12_NO_DISP
Definition: ELF.h:87
@ BCTR
Definition: ELF.h:93
@ MTCTR_R12
Definition: ELF.h:92
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
Definition: ELF.h:78
static Error defaultWarningHandler(const Twine &Msg)
Definition: ELF.h:163
std::string getSecIndexForError(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
Definition: ELF.h:129
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1777
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are are tuples (A,...
Definition: STLExtras.h:2430
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1246
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:745
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
#define N
Expected< T > operator[](uint64_t N)
Definition: ELF.h:108
DataRegion(const T *Data, const uint8_t *BufferEnd)
Definition: ELF.h:105
const T * First
Definition: ELF.h:123
DataRegion(ArrayRef< T > Arr)
Definition: ELF.h:101
const uint8_t * BufEnd
Definition: ELF.h:125
std::optional< uint64_t > Size
Definition: ELF.h:124
unsigned Cnt
Definition: ELF.h:45
unsigned Version
Definition: ELF.h:42
unsigned Flags
Definition: ELF.h:43
unsigned Ndx
Definition: ELF.h:44
std::string Name
Definition: ELF.h:47
unsigned Hash
Definition: ELF.h:46
std::vector< VerdAux > AuxV
Definition: ELF.h:48
unsigned Offset
Definition: ELF.h:41
unsigned Cnt
Definition: ELF.h:61
std::string File
Definition: ELF.h:63
std::vector< VernAux > AuxV
Definition: ELF.h:64
unsigned Offset
Definition: ELF.h:62
unsigned Version
Definition: ELF.h:60
unsigned Offset
Definition: ELF.h:36
std::string Name
Definition: ELF.h:37
unsigned Hash
Definition: ELF.h:52
unsigned Offset
Definition: ELF.h:55
unsigned Flags
Definition: ELF.h:53
std::string Name
Definition: ELF.h:56
unsigned Other
Definition: ELF.h:54
std::string Name
Definition: ELF.h:68