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
189 ELFFile(StringRef Object);
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
240 static Expected<ELFFile> create(StringRef Object);
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 // Allow 4, 8, and (for Linux core dumps) 0.
320 // TODO: Disallow 1 after all tests are fixed.
321 if (Phdr.p_align != 0 && Phdr.p_align != 1 && Phdr.p_align != 4 &&
322 Phdr.p_align != 8) {
323 Err =
324 createError("alignment (" + Twine(Phdr.p_align) + ") is not 4 or 8");
325 return Elf_Note_Iterator(Err);
326 }
327 return Elf_Note_Iterator(base() + Phdr.p_offset, Phdr.p_filesz,
328 std::max<size_t>(Phdr.p_align, 4), Err);
329 }
330
331 /// Get an iterator over notes in a section.
332 ///
333 /// The section must be of type \c SHT_NOTE.
334 ///
335 /// \param Shdr the section to iterate over.
336 /// \param Err [out] an error to support fallible iteration, which should
337 /// be checked after iteration ends.
338 Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const {
339 assert(Shdr.sh_type == ELF::SHT_NOTE && "Shdr is not of type SHT_NOTE");
340 ErrorAsOutParameter ErrAsOutParam(&Err);
341 if (Shdr.sh_offset + Shdr.sh_size > getBufSize()) {
342 Err =
343 createError("invalid offset (0x" + Twine::utohexstr(Shdr.sh_offset) +
344 ") or size (0x" + Twine::utohexstr(Shdr.sh_size) + ")");
345 return Elf_Note_Iterator(Err);
346 }
347 // TODO: Allow just 4 and 8 after all tests are fixed.
348 if (Shdr.sh_addralign != 0 && Shdr.sh_addralign != 1 &&
349 Shdr.sh_addralign != 4 && Shdr.sh_addralign != 8) {
350 Err = createError("alignment (" + Twine(Shdr.sh_addralign) +
351 ") is not 4 or 8");
352 return Elf_Note_Iterator(Err);
353 }
354 return Elf_Note_Iterator(base() + Shdr.sh_offset, Shdr.sh_size,
355 std::max<size_t>(Shdr.sh_addralign, 4), Err);
356 }
357
358 /// Get the end iterator for notes.
359 Elf_Note_Iterator notes_end() const {
360 return Elf_Note_Iterator();
361 }
362
363 /// Get an iterator range over notes of a program header.
364 ///
365 /// The program header must be of type \c PT_NOTE.
366 ///
367 /// \param Phdr the program header to iterate over.
368 /// \param Err [out] an error to support fallible iteration, which should
369 /// be checked after iteration ends.
371 Error &Err) const {
372 return make_range(notes_begin(Phdr, Err), notes_end());
373 }
374
375 /// Get an iterator range over notes of a section.
376 ///
377 /// The section must be of type \c SHT_NOTE.
378 ///
379 /// \param Shdr the section to iterate over.
380 /// \param Err [out] an error to support fallible iteration, which should
381 /// be checked after iteration ends.
383 Error &Err) const {
384 return make_range(notes_begin(Shdr, Err), notes_end());
385 }
386
388 Elf_Shdr_Range Sections,
389 WarningHandler WarnHandler = &defaultWarningHandler) const;
390 Expected<uint32_t> getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
391 DataRegion<Elf_Word> ShndxTable) const;
393 const Elf_Shdr *SymTab,
394 DataRegion<Elf_Word> ShndxTable) const;
396 Elf_Sym_Range Symtab,
397 DataRegion<Elf_Word> ShndxTable) const;
399
400 Expected<const Elf_Sym *> getSymbol(const Elf_Shdr *Sec,
401 uint32_t Index) const;
402
404 getSectionName(const Elf_Shdr &Section,
405 WarningHandler WarnHandler = &defaultWarningHandler) const;
406 Expected<StringRef> getSectionName(const Elf_Shdr &Section,
407 StringRef DotShstrtab) const;
408 template <typename T>
409 Expected<ArrayRef<T>> getSectionContentsAsArray(const Elf_Shdr &Sec) const;
410 Expected<ArrayRef<uint8_t>> getSectionContents(const Elf_Shdr &Sec) const;
411 Expected<ArrayRef<uint8_t>> getSegmentContents(const Elf_Phdr &Phdr) const;
412
413 /// Returns a vector of BBAddrMap structs corresponding to each function
414 /// within the text section that the SHT_LLVM_BB_ADDR_MAP section \p Sec
415 /// is associated with. If the current ELFFile is relocatable, a corresponding
416 /// \p RelaSec must be passed in as an argument.
418 decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec = nullptr) const;
419
420 /// Returns a map from every section matching \p IsMatch to its relocation
421 /// section, or \p nullptr if it has no relocation section. This function
422 /// returns an error if any of the \p IsMatch calls fail or if it fails to
423 /// retrieve the content section of any relocation section.
426 std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const;
427
428 void createFakeSections();
429};
430
435
436template <class ELFT>
438getSection(typename ELFT::ShdrRange Sections, uint32_t Index) {
439 if (Index >= Sections.size())
440 return createError("invalid section index: " + Twine(Index));
441 return &Sections[Index];
442}
443
444template <class ELFT>
446getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym, unsigned SymIndex,
448 assert(Sym.st_shndx == ELF::SHN_XINDEX);
449 if (!ShndxTable.First)
450 return createError(
451 "found an extended symbol index (" + Twine(SymIndex) +
452 "), but unable to locate the extended symbol index table");
453
454 Expected<typename ELFT::Word> TableOrErr = ShndxTable[SymIndex];
455 if (!TableOrErr)
456 return createError("unable to read an extended symbol table at index " +
457 Twine(SymIndex) + ": " +
458 toString(TableOrErr.takeError()));
459 return *TableOrErr;
460}
461
462template <class ELFT>
464ELFFile<ELFT>::getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
465 DataRegion<Elf_Word> ShndxTable) const {
466 uint32_t Index = Sym.st_shndx;
467 if (Index == ELF::SHN_XINDEX) {
468 Expected<uint32_t> ErrorOrIndex =
469 getExtendedSymbolTableIndex<ELFT>(Sym, &Sym - Syms.begin(), ShndxTable);
470 if (!ErrorOrIndex)
471 return ErrorOrIndex.takeError();
472 return *ErrorOrIndex;
473 }
475 return 0;
476 return Index;
477}
478
479template <class ELFT>
481ELFFile<ELFT>::getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab,
482 DataRegion<Elf_Word> ShndxTable) const {
483 auto SymsOrErr = symbols(SymTab);
484 if (!SymsOrErr)
485 return SymsOrErr.takeError();
486 return getSection(Sym, *SymsOrErr, ShndxTable);
487}
488
489template <class ELFT>
491ELFFile<ELFT>::getSection(const Elf_Sym &Sym, Elf_Sym_Range Symbols,
492 DataRegion<Elf_Word> ShndxTable) const {
493 auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable);
494 if (!IndexOrErr)
495 return IndexOrErr.takeError();
496 uint32_t Index = *IndexOrErr;
497 if (Index == 0)
498 return nullptr;
499 return getSection(Index);
500}
501
502template <class ELFT>
504ELFFile<ELFT>::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
505 auto SymsOrErr = symbols(Sec);
506 if (!SymsOrErr)
507 return SymsOrErr.takeError();
508
509 Elf_Sym_Range Symbols = *SymsOrErr;
510 if (Index >= Symbols.size())
511 return createError("unable to get symbol from section " +
512 getSecIndexForError(*this, *Sec) +
513 ": invalid symbol index (" + Twine(Index) + ")");
514 return &Symbols[Index];
515}
516
517template <class ELFT>
518template <typename T>
521 if (Sec.sh_entsize != sizeof(T) && sizeof(T) != 1)
522 return createError("section " + getSecIndexForError(*this, Sec) +
523 " has invalid sh_entsize: expected " + Twine(sizeof(T)) +
524 ", but got " + Twine(Sec.sh_entsize));
525
526 uintX_t Offset = Sec.sh_offset;
527 uintX_t Size = Sec.sh_size;
528
529 if (Size % sizeof(T))
530 return createError("section " + getSecIndexForError(*this, Sec) +
531 " has an invalid sh_size (" + Twine(Size) +
532 ") which is not a multiple of its sh_entsize (" +
533 Twine(Sec.sh_entsize) + ")");
534 if (std::numeric_limits<uintX_t>::max() - Offset < Size)
535 return createError("section " + getSecIndexForError(*this, Sec) +
536 " has a sh_offset (0x" + Twine::utohexstr(Offset) +
537 ") + sh_size (0x" + Twine::utohexstr(Size) +
538 ") that cannot be represented");
539 if (Offset + Size > Buf.size())
540 return createError("section " + getSecIndexForError(*this, Sec) +
541 " has a sh_offset (0x" + Twine::utohexstr(Offset) +
542 ") + sh_size (0x" + Twine::utohexstr(Size) +
543 ") that is greater than the file size (0x" +
544 Twine::utohexstr(Buf.size()) + ")");
545
546 if (Offset % alignof(T))
547 // TODO: this error is untested.
548 return createError("unaligned data");
549
550 const T *Start = reinterpret_cast<const T *>(base() + Offset);
551 return ArrayRef(Start, Size / sizeof(T));
552}
553
554template <class ELFT>
556ELFFile<ELFT>::getSegmentContents(const Elf_Phdr &Phdr) const {
557 uintX_t Offset = Phdr.p_offset;
558 uintX_t Size = Phdr.p_filesz;
559
560 if (std::numeric_limits<uintX_t>::max() - Offset < Size)
561 return createError("program header " + getPhdrIndexForError(*this, Phdr) +
562 " has a p_offset (0x" + Twine::utohexstr(Offset) +
563 ") + p_filesz (0x" + Twine::utohexstr(Size) +
564 ") that cannot be represented");
565 if (Offset + Size > Buf.size())
566 return createError("program header " + getPhdrIndexForError(*this, Phdr) +
567 " has a p_offset (0x" + Twine::utohexstr(Offset) +
568 ") + p_filesz (0x" + Twine::utohexstr(Size) +
569 ") that is greater than the file size (0x" +
570 Twine::utohexstr(Buf.size()) + ")");
571 return ArrayRef(base() + Offset, Size);
572}
573
574template <class ELFT>
576ELFFile<ELFT>::getSectionContents(const Elf_Shdr &Sec) const {
577 return getSectionContentsAsArray<uint8_t>(Sec);
578}
579
580template <class ELFT>
582 return getELFRelocationTypeName(getHeader().e_machine, Type);
583}
584
585template <class ELFT>
587 SmallVectorImpl<char> &Result) const {
588 if (!isMipsELF64()) {
589 StringRef Name = getRelocationTypeName(Type);
590 Result.append(Name.begin(), Name.end());
591 } else {
592 // The Mips N64 ABI allows up to three operations to be specified per
593 // relocation record. Unfortunately there's no easy way to test for the
594 // presence of N64 ELFs as they have no special flag that identifies them
595 // as being N64. We can safely assume at the moment that all Mips
596 // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
597 // information to disambiguate between old vs new ABIs.
598 uint8_t Type1 = (Type >> 0) & 0xFF;
599 uint8_t Type2 = (Type >> 8) & 0xFF;
600 uint8_t Type3 = (Type >> 16) & 0xFF;
601
602 // Concat all three relocation type names.
603 StringRef Name = getRelocationTypeName(Type1);
604 Result.append(Name.begin(), Name.end());
605
606 Name = getRelocationTypeName(Type2);
607 Result.append(1, '/');
608 Result.append(Name.begin(), Name.end());
609
610 Name = getRelocationTypeName(Type3);
611 Result.append(1, '/');
612 Result.append(Name.begin(), Name.end());
613 }
614}
615
616template <class ELFT>
618 return getELFRelativeRelocationType(getHeader().e_machine);
619}
620
621template <class ELFT>
623ELFFile<ELFT>::loadVersionMap(const Elf_Shdr *VerNeedSec,
624 const Elf_Shdr *VerDefSec) const {
626
627 // The first two version indexes are reserved.
628 // Index 0 is VER_NDX_LOCAL, index 1 is VER_NDX_GLOBAL.
629 VersionMap.push_back(VersionEntry());
630 VersionMap.push_back(VersionEntry());
631
632 auto InsertEntry = [&](unsigned N, StringRef Version, bool IsVerdef) {
633 if (N >= VersionMap.size())
634 VersionMap.resize(N + 1);
635 VersionMap[N] = {std::string(Version), IsVerdef};
636 };
637
638 if (VerDefSec) {
639 Expected<std::vector<VerDef>> Defs = getVersionDefinitions(*VerDefSec);
640 if (!Defs)
641 return Defs.takeError();
642 for (const VerDef &Def : *Defs)
643 InsertEntry(Def.Ndx & ELF::VERSYM_VERSION, Def.Name, true);
644 }
645
646 if (VerNeedSec) {
647 Expected<std::vector<VerNeed>> Deps = getVersionDependencies(*VerNeedSec);
648 if (!Deps)
649 return Deps.takeError();
650 for (const VerNeed &Dep : *Deps)
651 for (const VernAux &Aux : Dep.AuxV)
652 InsertEntry(Aux.Other & ELF::VERSYM_VERSION, Aux.Name, false);
653 }
654
655 return VersionMap;
656}
657
658template <class ELFT>
661 const Elf_Shdr *SymTab) const {
662 uint32_t Index = Rel.getSymbol(isMips64EL());
663 if (Index == 0)
664 return nullptr;
665 return getEntry<Elf_Sym>(*SymTab, Index);
666}
667
668template <class ELFT>
671 WarningHandler WarnHandler) const {
672 uint32_t Index = getHeader().e_shstrndx;
673 if (Index == ELF::SHN_XINDEX) {
674 // If the section name string table section index is greater than
675 // or equal to SHN_LORESERVE, then the actual index of the section name
676 // string table section is contained in the sh_link field of the section
677 // header at index 0.
678 if (Sections.empty())
679 return createError(
680 "e_shstrndx == SHN_XINDEX, but the section header table is empty");
681
682 Index = Sections[0].sh_link;
683 }
684
685 // There is no section name string table. Return FakeSectionStrings which
686 // is non-empty if we have created fake sections.
687 if (!Index)
688 return FakeSectionStrings;
689
690 if (Index >= Sections.size())
691 return createError("section header string table index " + Twine(Index) +
692 " does not exist");
693 return getStringTable(Sections[Index], WarnHandler);
694}
695
696/// This function finds the number of dynamic symbols using a GNU hash table.
697///
698/// @param Table The GNU hash table for .dynsym.
699template <class ELFT>
701getDynSymtabSizeFromGnuHash(const typename ELFT::GnuHash &Table,
702 const void *BufEnd) {
703 using Elf_Word = typename ELFT::Word;
704 if (Table.nbuckets == 0)
705 return Table.symndx + 1;
706 uint64_t LastSymIdx = 0;
707 // Find the index of the first symbol in the last chain.
708 for (Elf_Word Val : Table.buckets())
709 LastSymIdx = std::max(LastSymIdx, (uint64_t)Val);
710 const Elf_Word *It =
711 reinterpret_cast<const Elf_Word *>(Table.values(LastSymIdx).end());
712 // Locate the end of the chain to find the last symbol index.
713 while (It < BufEnd && (*It & 1) == 0) {
714 ++LastSymIdx;
715 ++It;
716 }
717 if (It >= BufEnd) {
718 return createStringError(
720 "no terminator found for GNU hash section before buffer end");
721 }
722 return LastSymIdx + 1;
723}
724
725/// This function determines the number of dynamic symbols. It reads section
726/// headers first. If section headers are not available, the number of
727/// symbols will be inferred by parsing dynamic hash tables.
728template <class ELFT>
730 // Read .dynsym section header first if available.
731 Expected<Elf_Shdr_Range> SectionsOrError = sections();
732 if (!SectionsOrError)
733 return SectionsOrError.takeError();
734 for (const Elf_Shdr &Sec : *SectionsOrError) {
735 if (Sec.sh_type == ELF::SHT_DYNSYM) {
736 if (Sec.sh_size % Sec.sh_entsize != 0) {
738 "SHT_DYNSYM section has sh_size (" +
739 Twine(Sec.sh_size) + ") % sh_entsize (" +
740 Twine(Sec.sh_entsize) + ") that is not 0");
741 }
742 return Sec.sh_size / Sec.sh_entsize;
743 }
744 }
745
746 if (!SectionsOrError->empty()) {
747 // Section headers are available but .dynsym header is not found.
748 // Return 0 as .dynsym does not exist.
749 return 0;
750 }
751
752 // Section headers do not exist. Falling back to infer
753 // upper bound of .dynsym from .gnu.hash and .hash.
754 Expected<Elf_Dyn_Range> DynTable = dynamicEntries();
755 if (!DynTable)
756 return DynTable.takeError();
757 std::optional<uint64_t> ElfHash;
758 std::optional<uint64_t> ElfGnuHash;
759 for (const Elf_Dyn &Entry : *DynTable) {
760 switch (Entry.d_tag) {
761 case ELF::DT_HASH:
762 ElfHash = Entry.d_un.d_ptr;
763 break;
764 case ELF::DT_GNU_HASH:
765 ElfGnuHash = Entry.d_un.d_ptr;
766 break;
767 }
768 }
769 if (ElfGnuHash) {
770 Expected<const uint8_t *> TablePtr = toMappedAddr(*ElfGnuHash);
771 if (!TablePtr)
772 return TablePtr.takeError();
773 const Elf_GnuHash *Table =
774 reinterpret_cast<const Elf_GnuHash *>(TablePtr.get());
775 return getDynSymtabSizeFromGnuHash<ELFT>(*Table, this->Buf.bytes_end());
776 }
777
778 // Search SYSV hash table to try to find the upper bound of dynsym.
779 if (ElfHash) {
780 Expected<const uint8_t *> TablePtr = toMappedAddr(*ElfHash);
781 if (!TablePtr)
782 return TablePtr.takeError();
783 const Elf_Hash *Table = reinterpret_cast<const Elf_Hash *>(TablePtr.get());
784 return Table->nchain;
785 }
786 return 0;
787}
788
789template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
790
791template <class ELFT>
793 if (sizeof(Elf_Ehdr) > Object.size())
794 return createError("invalid buffer: the size (" + Twine(Object.size()) +
795 ") is smaller than an ELF header (" +
796 Twine(sizeof(Elf_Ehdr)) + ")");
797 return ELFFile(Object);
798}
799
800/// Used by llvm-objdump -d (which needs sections for disassembly) to
801/// disassemble objects without a section header table (e.g. ET_CORE objects
802/// analyzed by linux perf or ET_EXEC with llvm-strip --strip-sections).
803template <class ELFT> void ELFFile<ELFT>::createFakeSections() {
804 if (!FakeSections.empty())
805 return;
806 auto PhdrsOrErr = program_headers();
807 if (!PhdrsOrErr)
808 return;
809
810 FakeSectionStrings += '\0';
811 for (auto [Idx, Phdr] : llvm::enumerate(*PhdrsOrErr)) {
812 if (Phdr.p_type != ELF::PT_LOAD || !(Phdr.p_flags & ELF::PF_X))
813 continue;
814 Elf_Shdr FakeShdr = {};
815 FakeShdr.sh_type = ELF::SHT_PROGBITS;
816 FakeShdr.sh_flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
817 FakeShdr.sh_addr = Phdr.p_vaddr;
818 FakeShdr.sh_size = Phdr.p_memsz;
819 FakeShdr.sh_offset = Phdr.p_offset;
820 // Create a section name based on the p_type and index.
821 FakeShdr.sh_name = FakeSectionStrings.size();
822 FakeSectionStrings += ("PT_LOAD#" + Twine(Idx)).str();
823 FakeSectionStrings += '\0';
824 FakeSections.push_back(FakeShdr);
825 }
826}
827
828template <class ELFT>
830 const uintX_t SectionTableOffset = getHeader().e_shoff;
831 if (SectionTableOffset == 0) {
832 if (!FakeSections.empty())
833 return ArrayRef(FakeSections.data(), FakeSections.size());
834 return ArrayRef<Elf_Shdr>();
835 }
836
837 if (getHeader().e_shentsize != sizeof(Elf_Shdr))
838 return createError("invalid e_shentsize in ELF header: " +
839 Twine(getHeader().e_shentsize));
840
841 const uint64_t FileSize = Buf.size();
842 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize ||
843 SectionTableOffset + (uintX_t)sizeof(Elf_Shdr) < SectionTableOffset)
844 return createError(
845 "section header table goes past the end of the file: e_shoff = 0x" +
846 Twine::utohexstr(SectionTableOffset));
847
848 // Invalid address alignment of section headers
849 if (SectionTableOffset & (alignof(Elf_Shdr) - 1))
850 // TODO: this error is untested.
851 return createError("invalid alignment of section headers");
852
853 const Elf_Shdr *First =
854 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
855
856 uintX_t NumSections = getHeader().e_shnum;
857 if (NumSections == 0)
858 NumSections = First->sh_size;
859
860 if (NumSections > UINT64_MAX / sizeof(Elf_Shdr))
861 return createError("invalid number of sections specified in the NULL "
862 "section's sh_size field (" +
863 Twine(NumSections) + ")");
864
865 const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr);
866 if (SectionTableOffset + SectionTableSize < SectionTableOffset)
867 return createError(
868 "invalid section header table offset (e_shoff = 0x" +
869 Twine::utohexstr(SectionTableOffset) +
870 ") or invalid number of sections specified in the first section "
871 "header's sh_size field (0x" +
872 Twine::utohexstr(NumSections) + ")");
873
874 // Section table goes past end of file!
875 if (SectionTableOffset + SectionTableSize > FileSize)
876 return createError("section table goes past the end of file");
877 return ArrayRef(First, NumSections);
878}
879
880template <class ELFT>
881template <typename T>
883 uint32_t Entry) const {
884 auto SecOrErr = getSection(Section);
885 if (!SecOrErr)
886 return SecOrErr.takeError();
887 return getEntry<T>(**SecOrErr, Entry);
888}
889
890template <class ELFT>
891template <typename T>
893 uint32_t Entry) const {
894 Expected<ArrayRef<T>> EntriesOrErr = getSectionContentsAsArray<T>(Section);
895 if (!EntriesOrErr)
896 return EntriesOrErr.takeError();
897
898 ArrayRef<T> Arr = *EntriesOrErr;
899 if (Entry >= Arr.size())
900 return createError(
901 "can't read an entry at 0x" +
902 Twine::utohexstr(Entry * static_cast<uint64_t>(sizeof(T))) +
903 ": it goes past the end of the section (0x" +
904 Twine::utohexstr(Section.sh_size) + ")");
905 return &Arr[Entry];
906}
907
908template <typename ELFT>
910 uint32_t SymbolVersionIndex, bool &IsDefault,
911 SmallVector<std::optional<VersionEntry>, 0> &VersionMap,
912 std::optional<bool> IsSymHidden) const {
913 size_t VersionIndex = SymbolVersionIndex & llvm::ELF::VERSYM_VERSION;
914
915 // Special markers for unversioned symbols.
916 if (VersionIndex == llvm::ELF::VER_NDX_LOCAL ||
917 VersionIndex == llvm::ELF::VER_NDX_GLOBAL) {
918 IsDefault = false;
919 return "";
920 }
921
922 // Lookup this symbol in the version table.
923 if (VersionIndex >= VersionMap.size() || !VersionMap[VersionIndex])
924 return createError("SHT_GNU_versym section refers to a version index " +
925 Twine(VersionIndex) + " which is missing");
926
927 const VersionEntry &Entry = *VersionMap[VersionIndex];
928 // A default version (@@) is only available for defined symbols.
929 if (!Entry.IsVerDef || IsSymHidden.value_or(false))
930 IsDefault = false;
931 else
932 IsDefault = !(SymbolVersionIndex & llvm::ELF::VERSYM_HIDDEN);
933 return Entry.Name.c_str();
934}
935
936template <class ELFT>
938ELFFile<ELFT>::getVersionDefinitions(const Elf_Shdr &Sec) const {
939 Expected<StringRef> StrTabOrErr = getLinkAsStrtab(Sec);
940 if (!StrTabOrErr)
941 return StrTabOrErr.takeError();
942
943 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
944 if (!ContentsOrErr)
945 return createError("cannot read content of " + describe(*this, Sec) + ": " +
946 toString(ContentsOrErr.takeError()));
947
948 const uint8_t *Start = ContentsOrErr->data();
949 const uint8_t *End = Start + ContentsOrErr->size();
950
951 auto ExtractNextAux = [&](const uint8_t *&VerdauxBuf,
952 unsigned VerDefNdx) -> Expected<VerdAux> {
953 if (VerdauxBuf + sizeof(Elf_Verdaux) > End)
954 return createError("invalid " + describe(*this, Sec) +
955 ": version definition " + Twine(VerDefNdx) +
956 " refers to an auxiliary entry that goes past the end "
957 "of the section");
958
959 auto *Verdaux = reinterpret_cast<const Elf_Verdaux *>(VerdauxBuf);
960 VerdauxBuf += Verdaux->vda_next;
961
962 VerdAux Aux;
963 Aux.Offset = VerdauxBuf - Start;
964 if (Verdaux->vda_name <= StrTabOrErr->size())
965 Aux.Name = std::string(StrTabOrErr->drop_front(Verdaux->vda_name));
966 else
967 Aux.Name = ("<invalid vda_name: " + Twine(Verdaux->vda_name) + ">").str();
968 return Aux;
969 };
970
971 std::vector<VerDef> Ret;
972 const uint8_t *VerdefBuf = Start;
973 for (unsigned I = 1; I <= /*VerDefsNum=*/Sec.sh_info; ++I) {
974 if (VerdefBuf + sizeof(Elf_Verdef) > End)
975 return createError("invalid " + describe(*this, Sec) +
976 ": version definition " + Twine(I) +
977 " goes past the end of the section");
978
979 if (reinterpret_cast<uintptr_t>(VerdefBuf) % sizeof(uint32_t) != 0)
980 return createError(
981 "invalid " + describe(*this, Sec) +
982 ": found a misaligned version definition entry at offset 0x" +
983 Twine::utohexstr(VerdefBuf - Start));
984
985 unsigned Version = *reinterpret_cast<const Elf_Half *>(VerdefBuf);
986 if (Version != 1)
987 return createError("unable to dump " + describe(*this, Sec) +
988 ": version " + Twine(Version) +
989 " is not yet supported");
990
991 const Elf_Verdef *D = reinterpret_cast<const Elf_Verdef *>(VerdefBuf);
992 VerDef &VD = *Ret.emplace(Ret.end());
993 VD.Offset = VerdefBuf - Start;
994 VD.Version = D->vd_version;
995 VD.Flags = D->vd_flags;
996 VD.Ndx = D->vd_ndx;
997 VD.Cnt = D->vd_cnt;
998 VD.Hash = D->vd_hash;
999
1000 const uint8_t *VerdauxBuf = VerdefBuf + D->vd_aux;
1001 for (unsigned J = 0; J < D->vd_cnt; ++J) {
1002 if (reinterpret_cast<uintptr_t>(VerdauxBuf) % sizeof(uint32_t) != 0)
1003 return createError("invalid " + describe(*this, Sec) +
1004 ": found a misaligned auxiliary entry at offset 0x" +
1005 Twine::utohexstr(VerdauxBuf - Start));
1006
1007 Expected<VerdAux> AuxOrErr = ExtractNextAux(VerdauxBuf, I);
1008 if (!AuxOrErr)
1009 return AuxOrErr.takeError();
1010
1011 if (J == 0)
1012 VD.Name = AuxOrErr->Name;
1013 else
1014 VD.AuxV.push_back(*AuxOrErr);
1015 }
1016
1017 VerdefBuf += D->vd_next;
1018 }
1019
1020 return Ret;
1021}
1022
1023template <class ELFT>
1026 WarningHandler WarnHandler) const {
1027 StringRef StrTab;
1028 Expected<StringRef> StrTabOrErr = getLinkAsStrtab(Sec);
1029 if (!StrTabOrErr) {
1030 if (Error E = WarnHandler(toString(StrTabOrErr.takeError())))
1031 return std::move(E);
1032 } else {
1033 StrTab = *StrTabOrErr;
1034 }
1035
1036 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
1037 if (!ContentsOrErr)
1038 return createError("cannot read content of " + describe(*this, Sec) + ": " +
1039 toString(ContentsOrErr.takeError()));
1040
1041 const uint8_t *Start = ContentsOrErr->data();
1042 const uint8_t *End = Start + ContentsOrErr->size();
1043 const uint8_t *VerneedBuf = Start;
1044
1045 std::vector<VerNeed> Ret;
1046 for (unsigned I = 1; I <= /*VerneedNum=*/Sec.sh_info; ++I) {
1047 if (VerneedBuf + sizeof(Elf_Verdef) > End)
1048 return createError("invalid " + describe(*this, Sec) +
1049 ": version dependency " + Twine(I) +
1050 " goes past the end of the section");
1051
1052 if (reinterpret_cast<uintptr_t>(VerneedBuf) % sizeof(uint32_t) != 0)
1053 return createError(
1054 "invalid " + describe(*this, Sec) +
1055 ": found a misaligned version dependency entry at offset 0x" +
1056 Twine::utohexstr(VerneedBuf - Start));
1057
1058 unsigned Version = *reinterpret_cast<const Elf_Half *>(VerneedBuf);
1059 if (Version != 1)
1060 return createError("unable to dump " + describe(*this, Sec) +
1061 ": version " + Twine(Version) +
1062 " is not yet supported");
1063
1064 const Elf_Verneed *Verneed =
1065 reinterpret_cast<const Elf_Verneed *>(VerneedBuf);
1066
1067 VerNeed &VN = *Ret.emplace(Ret.end());
1068 VN.Version = Verneed->vn_version;
1069 VN.Cnt = Verneed->vn_cnt;
1070 VN.Offset = VerneedBuf - Start;
1071
1072 if (Verneed->vn_file < StrTab.size())
1073 VN.File = std::string(StrTab.data() + Verneed->vn_file);
1074 else
1075 VN.File = ("<corrupt vn_file: " + Twine(Verneed->vn_file) + ">").str();
1076
1077 const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux;
1078 for (unsigned J = 0; J < Verneed->vn_cnt; ++J) {
1079 if (reinterpret_cast<uintptr_t>(VernauxBuf) % sizeof(uint32_t) != 0)
1080 return createError("invalid " + describe(*this, Sec) +
1081 ": found a misaligned auxiliary entry at offset 0x" +
1082 Twine::utohexstr(VernauxBuf - Start));
1083
1084 if (VernauxBuf + sizeof(Elf_Vernaux) > End)
1085 return createError(
1086 "invalid " + describe(*this, Sec) + ": version dependency " +
1087 Twine(I) +
1088 " refers to an auxiliary entry that goes past the end "
1089 "of the section");
1090
1091 const Elf_Vernaux *Vernaux =
1092 reinterpret_cast<const Elf_Vernaux *>(VernauxBuf);
1093
1094 VernAux &Aux = *VN.AuxV.emplace(VN.AuxV.end());
1095 Aux.Hash = Vernaux->vna_hash;
1096 Aux.Flags = Vernaux->vna_flags;
1097 Aux.Other = Vernaux->vna_other;
1098 Aux.Offset = VernauxBuf - Start;
1099 if (StrTab.size() <= Vernaux->vna_name)
1100 Aux.Name = "<corrupt>";
1101 else
1102 Aux.Name = std::string(StrTab.drop_front(Vernaux->vna_name));
1103
1104 VernauxBuf += Vernaux->vna_next;
1105 }
1106 VerneedBuf += Verneed->vn_next;
1107 }
1108 return Ret;
1109}
1110
1111template <class ELFT>
1114 auto TableOrErr = sections();
1115 if (!TableOrErr)
1116 return TableOrErr.takeError();
1117 return object::getSection<ELFT>(*TableOrErr, Index);
1118}
1119
1120template <class ELFT>
1122ELFFile<ELFT>::getStringTable(const Elf_Shdr &Section,
1123 WarningHandler WarnHandler) const {
1124 if (Section.sh_type != ELF::SHT_STRTAB)
1125 if (Error E = WarnHandler("invalid sh_type for string table section " +
1126 getSecIndexForError(*this, Section) +
1127 ": expected SHT_STRTAB, but got " +
1129 getHeader().e_machine, Section.sh_type)))
1130 return std::move(E);
1131
1132 auto V = getSectionContentsAsArray<char>(Section);
1133 if (!V)
1134 return V.takeError();
1135 ArrayRef<char> Data = *V;
1136 if (Data.empty())
1137 return createError("SHT_STRTAB string table section " +
1138 getSecIndexForError(*this, Section) + " is empty");
1139 if (Data.back() != '\0')
1140 return createError("SHT_STRTAB string table section " +
1141 getSecIndexForError(*this, Section) +
1142 " is non-null terminated");
1143 return StringRef(Data.begin(), Data.size());
1144}
1145
1146template <class ELFT>
1148ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section) const {
1149 auto SectionsOrErr = sections();
1150 if (!SectionsOrErr)
1151 return SectionsOrErr.takeError();
1152 return getSHNDXTable(Section, *SectionsOrErr);
1153}
1154
1155template <class ELFT>
1157ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section,
1158 Elf_Shdr_Range Sections) const {
1159 assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
1160 auto VOrErr = getSectionContentsAsArray<Elf_Word>(Section);
1161 if (!VOrErr)
1162 return VOrErr.takeError();
1163 ArrayRef<Elf_Word> V = *VOrErr;
1164 auto SymTableOrErr = object::getSection<ELFT>(Sections, Section.sh_link);
1165 if (!SymTableOrErr)
1166 return SymTableOrErr.takeError();
1167 const Elf_Shdr &SymTable = **SymTableOrErr;
1168 if (SymTable.sh_type != ELF::SHT_SYMTAB &&
1169 SymTable.sh_type != ELF::SHT_DYNSYM)
1170 return createError(
1171 "SHT_SYMTAB_SHNDX section is linked with " +
1172 object::getELFSectionTypeName(getHeader().e_machine, SymTable.sh_type) +
1173 " section (expected SHT_SYMTAB/SHT_DYNSYM)");
1174
1175 uint64_t Syms = SymTable.sh_size / sizeof(Elf_Sym);
1176 if (V.size() != Syms)
1177 return createError("SHT_SYMTAB_SHNDX has " + Twine(V.size()) +
1178 " entries, but the symbol table associated has " +
1179 Twine(Syms));
1180
1181 return V;
1182}
1183
1184template <class ELFT>
1186ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
1187 auto SectionsOrErr = sections();
1188 if (!SectionsOrErr)
1189 return SectionsOrErr.takeError();
1190 return getStringTableForSymtab(Sec, *SectionsOrErr);
1191}
1192
1193template <class ELFT>
1196 Elf_Shdr_Range Sections) const {
1197
1198 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
1199 return createError(
1200 "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
1201 Expected<const Elf_Shdr *> SectionOrErr =
1202 object::getSection<ELFT>(Sections, Sec.sh_link);
1203 if (!SectionOrErr)
1204 return SectionOrErr.takeError();
1205 return getStringTable(**SectionOrErr);
1206}
1207
1208template <class ELFT>
1210ELFFile<ELFT>::getLinkAsStrtab(const typename ELFT::Shdr &Sec) const {
1212 getSection(Sec.sh_link);
1213 if (!StrTabSecOrErr)
1214 return createError("invalid section linked to " + describe(*this, Sec) +
1215 ": " + toString(StrTabSecOrErr.takeError()));
1216
1217 Expected<StringRef> StrTabOrErr = getStringTable(**StrTabSecOrErr);
1218 if (!StrTabOrErr)
1219 return createError("invalid string table linked to " +
1220 describe(*this, Sec) + ": " +
1221 toString(StrTabOrErr.takeError()));
1222 return *StrTabOrErr;
1223}
1224
1225template <class ELFT>
1227ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section,
1228 WarningHandler WarnHandler) const {
1229 auto SectionsOrErr = sections();
1230 if (!SectionsOrErr)
1231 return SectionsOrErr.takeError();
1232 auto Table = getSectionStringTable(*SectionsOrErr, WarnHandler);
1233 if (!Table)
1234 return Table.takeError();
1235 return getSectionName(Section, *Table);
1236}
1237
1238template <class ELFT>
1240 StringRef DotShstrtab) const {
1241 uint32_t Offset = Section.sh_name;
1242 if (Offset == 0)
1243 return StringRef();
1244 if (Offset >= DotShstrtab.size())
1245 return createError("a section " + getSecIndexForError(*this, Section) +
1246 " has an invalid sh_name (0x" +
1248 ") offset which goes past the end of the "
1249 "section name string table");
1250 return StringRef(DotShstrtab.data() + Offset);
1251}
1252
1253/// This function returns the hash value for a symbol in the .dynsym section
1254/// Name of the API remains consistent as specified in the libelf
1255/// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
1256inline uint32_t hashSysV(StringRef SymbolName) {
1257 uint32_t H = 0;
1258 for (uint8_t C : SymbolName) {
1259 H = (H << 4) + C;
1260 H ^= (H >> 24) & 0xf0;
1261 }
1262 return H & 0x0fffffff;
1263}
1264
1265/// This function returns the hash value for a symbol in the .dynsym section
1266/// for the GNU hash table. The implementation is defined in the GNU hash ABI.
1267/// REF : https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elf.c#l222
1269 uint32_t H = 5381;
1270 for (uint8_t C : Name)
1271 H = (H << 5) + H + C;
1272 return H;
1273}
1274
1275} // end namespace object
1276} // end namespace llvm
1277
1278#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:1789
ELFYAML::ELF_REL Type2
Definition: ELFYAML.cpp:1788
bool End
Definition: ELF_riscv.cpp:464
Symbol * Sym
Definition: ELF_riscv.cpp:463
#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.
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:613
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:391
Expected< StringRef > getLinkAsStrtab(const typename ELFT::Shdr &Sec) const
Definition: ELF.h:1210
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:792
Expected< const Elf_Sym * > getSymbol(const Elf_Shdr *Sec, uint32_t Index) const
Definition: ELF.h:504
Expected< std::vector< VerDef > > getVersionDefinitions(const Elf_Shdr &Sec) const
Definition: ELF.h:938
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
Definition: ELF.cpp:459
Expected< ArrayRef< Elf_Word > > getSHNDXTable(const Elf_Shdr &Section) const
Definition: ELF.h:1148
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:648
Expected< ArrayRef< uint8_t > > getSegmentContents(const Elf_Phdr &Phdr) const
Definition: ELF.h:556
Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const
Get an iterator over notes in a section.
Definition: ELF.h:338
uint32_t getRelativeRelocationType() const
Definition: ELF.h:617
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:370
Expected< StringRef > getSymbolVersionByIndex(uint32_t SymbolVersionIndex, bool &IsDefault, SmallVector< std::optional< VersionEntry >, 0 > &VersionMap, std::optional< bool > IsSymHidden) const
Definition: ELF.h:909
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:576
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:1186
Expected< std::vector< VerNeed > > getVersionDependencies(const Elf_Shdr &Sec, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:1025
size_t getBufSize() const
Definition: ELF.h:182
Expected< const T * > getEntry(uint32_t Section, uint32_t Entry) const
Definition: ELF.h:882
Expected< const Elf_Sym * > getRelocationSymbol(const Elf_Rel &Rel, const Elf_Shdr *SymTab) const
Get the symbol for a given relocation.
Definition: ELF.h:660
const uint8_t * end() const
Definition: ELF.h:180
Expected< StringRef > getSectionStringTable(Elf_Shdr_Range Sections, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:670
Expected< uint64_t > getDynSymtabSize() const
This function determines the number of dynamic symbols.
Definition: ELF.h:729
Expected< Elf_Dyn_Range > dynamicEntries() const
Definition: ELF.cpp:548
void createFakeSections()
Used by llvm-objdump -d (which needs sections for disassembly) to disassemble objects without a secti...
Definition: ELF.h:803
Expected< Elf_Shdr_Range > sections() const
Definition: ELF.h:829
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:382
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:596
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:759
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:359
Expected< StringRef > getSectionName(const Elf_Shdr &Section, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:1227
StringRef getRelocationTypeName(uint32_t Type) const
Definition: ELF.h:581
Expected< StringRef > getStringTable(const Elf_Shdr &Section, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.h:1122
llvm::function_ref< Error(const Twine &Msg)> WarningHandler
Definition: ELF.h:177
Expected< ArrayRef< T > > getSectionContentsAsArray(const Elf_Shdr &Sec) const
Definition: ELF.h:520
Expected< SmallVector< std::optional< VersionEntry >, 0 > > loadVersionMap(const Elf_Shdr *VerNeedSec, const Elf_Shdr *VerDefSec) const
Definition: ELF.h:623
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:481
std::vector< Elf_Rel > decode_relrs(Elf_Relr_Range relrs) const
Definition: ELF.cpp:327
Expected< uint32_t > getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms, DataRegion< Elf_Word > ShndxTable) const
Definition: ELF.h:464
#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:1088
@ SHF_EXECINSTR
Definition: ELF.h:1091
@ PF_X
Definition: ELF.h:1434
@ 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:993
@ SHN_UNDEF
Definition: ELF.h:985
@ SHN_LORESERVE
Definition: ELF.h:986
@ SHT_STRTAB
Definition: ELF.h:1002
@ SHT_PROGBITS
Definition: ELF.h:1000
@ SHT_SYMTAB
Definition: ELF.h:1001
@ SHT_SYMTAB_SHNDX
Definition: ELF.h:1015
@ SHT_NOTE
Definition: ELF.h:1006
@ SHT_DYNSYM
Definition: ELF.h:1010
@ PT_LOAD
Definition: ELF.h:1387
@ PT_NOTE
Definition: ELF.h:1390
@ EI_DATA
Definition: ELF.h:53
@ EI_NIDENT
Definition: ELF.h:58
@ EI_CLASS
Definition: ELF.h:52
@ VER_NDX_GLOBAL
Definition: ELF.h:1540
@ VERSYM_VERSION
Definition: ELF.h:1541
@ VER_NDX_LOCAL
Definition: ELF.h:1539
@ VERSYM_HIDDEN
Definition: ELF.h:1542
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:446
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:438
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:701
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:1268
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
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
uint32_t 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:1256
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:440
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