LLVM 23.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"
26#include "llvm/Support/Error.h"
27#include <cassert>
28#include <cstddef>
29#include <cstdint>
30#include <limits>
31#include <type_traits>
32#include <utility>
33
34namespace llvm {
35namespace object {
36
37struct VerdAux {
38 unsigned Offset;
39 std::string Name;
40};
41
42struct VerDef {
43 unsigned Offset;
48 unsigned Hash;
49 std::string Name;
50 std::vector<VerdAux> AuxV;
51};
52
53struct VernAux {
54 unsigned Hash;
55 unsigned Flags;
56 unsigned Other;
57 unsigned Offset;
58 std::string Name;
59};
60
61struct VerNeed {
62 unsigned Version;
63 unsigned Cnt;
64 unsigned Offset;
65 std::string File;
66 std::vector<VernAux> AuxV;
67};
68
70 std::string Name;
72};
73
76 StringRef Vendor);
79
80// Subclasses of ELFFile may need this for template instantiation
81inline std::pair<unsigned char, unsigned char>
83 if (Object.size() < ELF::EI_NIDENT)
84 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,
86 return std::make_pair((uint8_t)Object[ELF::EI_CLASS],
87 (uint8_t)Object[ELF::EI_DATA]);
88}
89
91 PADDI_R12_NO_DISP = 0x0610000039800000,
95 PLD_R12_NO_DISP = 0x04100000E5800000,
96 MTCTR_R12 = 0x7D8903A6,
97 BCTR = 0x4E800420,
98};
99
100template <class ELFT> class ELFFile;
101
102template <class T> struct DataRegion {
103 // This constructor is used when we know the start and the size of a data
104 // region. We assume that Arr does not go past the end of the file.
105 DataRegion(ArrayRef<T> Arr) : First(Arr.data()), Size(Arr.size()) {}
106
107 // Sometimes we only know the start of a data region. We still don't want to
108 // read past the end of the file, so we provide the end of a buffer.
109 DataRegion(const T *Data, const uint8_t *BufferEnd)
110 : First(Data), BufEnd(BufferEnd) {}
111
113 assert(Size || BufEnd);
114 if (Size) {
115 if (N >= *Size)
116 return createError(
117 "the index is greater than or equal to the number of entries (" +
118 Twine(*Size) + ")");
119 } else {
120 const uint8_t *EntryStart = (const uint8_t *)First + N * sizeof(T);
121 if (EntryStart + sizeof(T) > BufEnd)
122 return createError("can't read past the end of the file");
123 }
124 return *(First + N);
125 }
126
127 const T *First;
128 std::optional<uint64_t> Size;
129 const uint8_t *BufEnd = nullptr;
130};
131
132template <class ELFT>
133std::string getSecIndexForError(const ELFFile<ELFT> &Obj,
134 const typename ELFT::Shdr &Sec) {
135 auto TableOrErr = Obj.sections();
136 if (TableOrErr)
137 return "[index " + std::to_string(&Sec - &TableOrErr->front()) + "]";
138 // To make this helper be more convenient for error reporting purposes we
139 // drop the error. But really it should never be triggered. Before this point,
140 // our code should have called 'sections()' and reported a proper error on
141 // failure.
142 llvm::consumeError(TableOrErr.takeError());
143 return "[unknown index]";
144}
145
146template <class ELFT>
147std::string describe(const ELFFile<ELFT> &Obj, const typename ELFT::Shdr &Sec) {
148 unsigned SecNdx = &Sec - &cantFail(Obj.sections()).front();
149 return (object::getELFSectionTypeName(Obj.getHeader().e_machine,
150 Sec.sh_type) +
151 " section with index " + Twine(SecNdx))
152 .str();
153}
154
155template <class ELFT>
156std::string getPhdrIndexForError(const ELFFile<ELFT> &Obj,
157 const typename ELFT::Phdr &Phdr) {
158 auto Headers = Obj.program_headers();
159 if (Headers)
160 return ("[index " + Twine(&Phdr - &Headers->front()) + "]").str();
161 // See comment in the getSecIndexForError() above.
162 llvm::consumeError(Headers.takeError());
163 return "[unknown index]";
164}
165
166static inline Error defaultWarningHandler(const Twine &Msg) {
167 return createError(Msg);
168}
169
170template <class ELFT>
171bool checkSectionOffsets(const typename ELFT::Phdr &Phdr,
172 const typename ELFT::Shdr &Sec) {
173 // SHT_NOBITS sections don't need to have an offset inside the segment.
174 if (Sec.sh_type == ELF::SHT_NOBITS)
175 return true;
176
177 if (Sec.sh_offset < Phdr.p_offset)
178 return false;
179
180 // Only non-empty sections can be at the end of a segment.
181 if (Sec.sh_size == 0)
182 return (Sec.sh_offset + 1 <= Phdr.p_offset + Phdr.p_filesz);
183 return Sec.sh_offset + Sec.sh_size <= Phdr.p_offset + Phdr.p_filesz;
184}
185
186// Check that an allocatable section belongs to a virtual address
187// space of a segment.
188template <class ELFT>
189bool checkSectionVMA(const typename ELFT::Phdr &Phdr,
190 const typename ELFT::Shdr &Sec) {
191 if (!(Sec.sh_flags & ELF::SHF_ALLOC))
192 return true;
193
194 if (Sec.sh_addr < Phdr.p_vaddr)
195 return false;
196
197 bool IsTbss =
198 (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
199 // .tbss is special, it only has memory in PT_TLS and has NOBITS properties.
200 bool IsTbssInNonTLS = IsTbss && Phdr.p_type != ELF::PT_TLS;
201 // Only non-empty sections can be at the end of a segment.
202 if (Sec.sh_size == 0 || IsTbssInNonTLS)
203 return Sec.sh_addr + 1 <= Phdr.p_vaddr + Phdr.p_memsz;
204 return Sec.sh_addr + Sec.sh_size <= Phdr.p_vaddr + Phdr.p_memsz;
205}
206
207template <class ELFT>
208bool isSectionInSegment(const typename ELFT::Phdr &Phdr,
209 const typename ELFT::Shdr &Sec) {
210 return checkSectionOffsets<ELFT>(Phdr, Sec) &&
211 checkSectionVMA<ELFT>(Phdr, Sec);
212}
213
214// HdrHandler is called once with the number of relocations and whether the
215// relocations have addends. EntryHandler is called once per decoded relocation.
216template <bool Is64>
218 ArrayRef<uint8_t> Content,
219 function_ref<void(uint64_t /*relocation count*/, bool /*explicit addends*/)>
220 HdrHandler,
221 function_ref<void(Elf_Crel_Impl<Is64>)> EntryHandler) {
222 DataExtractor Data(Content, true); // endian is unused
224 const uint64_t Hdr = Data.getULEB128(Cur);
225 size_t Count = Hdr / 8;
226 const size_t FlagBits = Hdr & ELF::CREL_HDR_ADDEND ? 3 : 2;
227 const size_t Shift = Hdr % ELF::CREL_HDR_ADDEND;
228 using uint = typename Elf_Crel_Impl<Is64>::uint;
229 uint Offset = 0, Addend = 0;
230 HdrHandler(Count, Hdr & ELF::CREL_HDR_ADDEND);
231 uint32_t SymIdx = 0, Type = 0;
232 for (; Count; --Count) {
233 // The delta offset and flags member may be larger than uint64_t. Special
234 // case the first byte (2 or 3 flag bits; the rest are offset bits). Other
235 // ULEB128 bytes encode the remaining delta offset bits.
236 const uint8_t B = Data.getU8(Cur);
237 Offset += B >> FlagBits;
238 if (B >= 0x80)
239 Offset += (Data.getULEB128(Cur) << (7 - FlagBits)) - (0x80 >> FlagBits);
240 // Delta symidx/type/addend members (SLEB128).
241 if (B & 1)
242 SymIdx += Data.getSLEB128(Cur);
243 if (B & 2)
244 Type += Data.getSLEB128(Cur);
245 if (B & 4 & Hdr)
246 Addend += Data.getSLEB128(Cur);
247 if (!Cur)
248 break;
249 EntryHandler(
251 }
252 return Cur.takeError();
253}
254
255template <class ELFT>
256class ELFFile {
257public:
259
260 // Default ctor and copy assignment operator required to instantiate the
261 // template for DLL export.
262 ELFFile(const ELFFile &) = default;
263 ELFFile &operator=(const ELFFile &) = default;
264
265 ELFFile(ELFFile &&) = default;
266
267 // This is a callback that can be passed to a number of functions.
268 // It can be used to ignore non-critical errors (warnings), which is
269 // useful for dumpers, like llvm-readobj.
270 // It accepts a warning message string and returns a success
271 // when the warning should be ignored or an error otherwise.
273
274 const uint8_t *base() const { return Buf.bytes_begin(); }
275 const uint8_t *end() const { return base() + getBufSize(); }
276
277 size_t getBufSize() const { return Buf.size(); }
278
279private:
280 StringRef Buf;
281 std::vector<Elf_Shdr> FakeSections;
282 SmallString<0> FakeSectionStrings;
283
284 // When the number of program headers is >= PN_XNUM, the actual number is
285 // contained in the sh_info field of the section header at index 0.
286 std::optional<uint32_t> RealPhNum;
287 // When the number of section headers is >= SHN_LORESERVE, the actual number
288 // is contained in the sh_size field of the section header at index 0.
289 std::optional<uint64_t> RealShNum;
290 // When the section index of the section name table is >= SHN_LORESERVE, the
291 // actual number is contained in the sh_link field of the section header at
292 // index 0.
293 std::optional<uint32_t> RealShStrNdx;
294
295 ELFFile(StringRef Object);
296
297 Error readShdrZero();
298
299public:
301 if (!RealPhNum) {
302 if (Error E = const_cast<ELFFile<ELFT> *>(this)->readShdrZero()) {
303 // If RealPhNum is set, the error was not emitted due to reading the
304 // program header count, so we can ignore it in this context.
305 if (RealPhNum) {
306 consumeError(std::move(E));
307 return *RealPhNum;
308 }
309 return std::move(E);
310 }
311 }
312 return *RealPhNum;
313 }
314
316 if (!RealShNum) {
317 if (Error E = const_cast<ELFFile<ELFT> *>(this)->readShdrZero()) {
318 // If RealShNum is set, the error was not emitted due to reading the
319 // section header count, so we can ignore it in this context.
320 if (RealShNum) {
321 consumeError(std::move(E));
322 return *RealShNum;
323 }
324 return std::move(E);
325 }
326 }
327 return *RealShNum;
328 }
329
331 if (!RealShStrNdx) {
332 if (Error E = const_cast<ELFFile<ELFT> *>(this)->readShdrZero()) {
333 // If RealShStrNdx is set, the error was not emitted due to reading the
334 // section header string table index, so we can ignore it in this
335 // context.
336 if (RealShStrNdx) {
337 consumeError(std::move(E));
338 return *RealShStrNdx;
339 }
340 return std::move(E);
341 }
342 }
343 return *RealShStrNdx;
344 }
345
346 const Elf_Ehdr &getHeader() const {
347 return *reinterpret_cast<const Elf_Ehdr *>(base());
348 }
349
350 template <typename T>
352 template <typename T>
353 Expected<const T *> getEntry(const Elf_Shdr &Section, uint32_t Entry) const;
354
356 getVersionDefinitions(const Elf_Shdr &Sec) const;
358 const Elf_Shdr &Sec,
359 WarningHandler WarnHandler = &defaultWarningHandler) const;
361 uint32_t SymbolVersionIndex, bool &IsDefault,
362 SmallVector<std::optional<VersionEntry>, 0> &VersionMap,
363 std::optional<bool> IsSymHidden) const;
364
366 getStringTable(const Elf_Shdr &Section,
367 WarningHandler WarnHandler = &defaultWarningHandler) const;
368 Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
370 Elf_Shdr_Range Sections) const;
371 Expected<StringRef> getLinkAsStrtab(const typename ELFT::Shdr &Sec) const;
372
373 Expected<ArrayRef<Elf_Word>> getSHNDXTable(const Elf_Shdr &Section) const;
375 Elf_Shdr_Range Sections) const;
376
378
381 SmallVectorImpl<char> &Result) const;
383
384 std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const;
386
387 /// Get the symbol for a given relocation.
389 const Elf_Shdr *SymTab) const;
390
392 loadVersionMap(const Elf_Shdr *VerNeedSec, const Elf_Shdr *VerDefSec) const;
393
395
396 bool isLE() const {
397 return getHeader().getDataEncoding() == ELF::ELFDATA2LSB;
398 }
399
400 bool isMipsELF64() const {
401 return getHeader().e_machine == ELF::EM_MIPS &&
402 getHeader().getFileClass() == ELF::ELFCLASS64;
403 }
404
405 bool isMips64EL() const { return isMipsELF64() && isLE(); }
406
408
410
413 WarningHandler WarnHandler = &defaultWarningHandler) const;
414
415 Expected<Elf_Sym_Range> symbols(const Elf_Shdr *Sec) const {
416 if (!Sec)
417 return ArrayRef<Elf_Sym>(nullptr, nullptr);
419 }
420
421 Expected<Elf_Rela_Range> relas(const Elf_Shdr &Sec) const {
423 }
424
425 Expected<Elf_Rel_Range> rels(const Elf_Shdr &Sec) const {
427 }
428
429 Expected<Elf_Relr_Range> relrs(const Elf_Shdr &Sec) const {
431 }
432
433 std::vector<Elf_Rel> decode_relrs(Elf_Relr_Range relrs) const;
434
436 using RelsOrRelas = std::pair<std::vector<Elf_Rel>, std::vector<Elf_Rela>>;
438 Expected<RelsOrRelas> crels(const Elf_Shdr &Sec) const;
439
441
442 /// Iterate over program header table.
444 uint32_t NumPh;
445 if (Expected<uint32_t> PhNumOrErr = getPhNum())
446 NumPh = *PhNumOrErr;
447 else
448 return PhNumOrErr.takeError();
449 if (NumPh && getHeader().e_phentsize != sizeof(Elf_Phdr))
450 return createError("invalid e_phentsize: " +
451 Twine(getHeader().e_phentsize));
452
453 uint64_t HeadersSize = (uint64_t)NumPh * getHeader().e_phentsize;
454 uint64_t PhOff = getHeader().e_phoff;
455 if (PhOff + HeadersSize < PhOff || PhOff + HeadersSize > getBufSize())
456 return createError("program headers are longer than binary of size " +
457 Twine(getBufSize()) + ": e_phoff = 0x" +
458 Twine::utohexstr(getHeader().e_phoff) +
459 ", e_phnum = " + Twine(NumPh) +
460 ", e_phentsize = " + Twine(getHeader().e_phentsize));
461
462 auto *Begin = reinterpret_cast<const Elf_Phdr *>(base() + PhOff);
463 return ArrayRef(Begin, Begin + NumPh);
464 }
465
466 /// Get an iterator over notes in a program header.
467 ///
468 /// The program header must be of type \c PT_NOTE.
469 ///
470 /// \param Phdr the program header to iterate over.
471 /// \param Err [out] an error to support fallible iteration, which should
472 /// be checked after iteration ends.
473 Elf_Note_Iterator notes_begin(const Elf_Phdr &Phdr, Error &Err) const {
474 assert(Phdr.p_type == ELF::PT_NOTE && "Phdr is not of type PT_NOTE");
475 ErrorAsOutParameter ErrAsOutParam(Err);
476 if (Phdr.p_offset + Phdr.p_filesz > getBufSize() ||
477 Phdr.p_offset + Phdr.p_filesz < Phdr.p_offset) {
478 Err =
479 createError("invalid offset (0x" + Twine::utohexstr(Phdr.p_offset) +
480 ") or size (0x" + Twine::utohexstr(Phdr.p_filesz) + ")");
481 return Elf_Note_Iterator(Err);
482 }
483 // Allow 4, 8, and (for Linux core dumps) 0.
484 // TODO: Disallow 1 after all tests are fixed.
485 if (Phdr.p_align != 0 && Phdr.p_align != 1 && Phdr.p_align != 4 &&
486 Phdr.p_align != 8) {
487 Err =
488 createError("alignment (" + Twine(Phdr.p_align) + ") is not 4 or 8");
489 return Elf_Note_Iterator(Err);
490 }
491 return Elf_Note_Iterator(base() + Phdr.p_offset, Phdr.p_filesz,
492 std::max<size_t>(Phdr.p_align, 4), Err);
493 }
494
495 /// Get an iterator over notes in a section.
496 ///
497 /// The section must be of type \c SHT_NOTE.
498 ///
499 /// \param Shdr the section to iterate over.
500 /// \param Err [out] an error to support fallible iteration, which should
501 /// be checked after iteration ends.
502 Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const {
503 assert(Shdr.sh_type == ELF::SHT_NOTE && "Shdr is not of type SHT_NOTE");
504 ErrorAsOutParameter ErrAsOutParam(Err);
505 if (Shdr.sh_offset + Shdr.sh_size > getBufSize() ||
506 Shdr.sh_offset + Shdr.sh_size < Shdr.sh_offset) {
507 Err =
508 createError("invalid offset (0x" + Twine::utohexstr(Shdr.sh_offset) +
509 ") or size (0x" + Twine::utohexstr(Shdr.sh_size) + ")");
510 return Elf_Note_Iterator(Err);
511 }
512 // TODO: Allow just 4 and 8 after all tests are fixed.
513 if (Shdr.sh_addralign != 0 && Shdr.sh_addralign != 1 &&
514 Shdr.sh_addralign != 4 && Shdr.sh_addralign != 8) {
515 Err = createError("alignment (" + Twine(Shdr.sh_addralign) +
516 ") is not 4 or 8");
517 return Elf_Note_Iterator(Err);
518 }
519 return Elf_Note_Iterator(base() + Shdr.sh_offset, Shdr.sh_size,
520 std::max<size_t>(Shdr.sh_addralign, 4), Err);
521 }
522
523 /// Get the end iterator for notes.
524 Elf_Note_Iterator notes_end() const {
525 return Elf_Note_Iterator();
526 }
527
528 /// Get an iterator range over notes of a program header.
529 ///
530 /// The program header must be of type \c PT_NOTE.
531 ///
532 /// \param Phdr the program header to iterate over.
533 /// \param Err [out] an error to support fallible iteration, which should
534 /// be checked after iteration ends.
536 Error &Err) const {
537 return make_range(notes_begin(Phdr, Err), notes_end());
538 }
539
540 /// Get an iterator range over notes of a section.
541 ///
542 /// The section must be of type \c SHT_NOTE.
543 ///
544 /// \param Shdr the section to iterate over.
545 /// \param Err [out] an error to support fallible iteration, which should
546 /// be checked after iteration ends.
548 Error &Err) const {
549 return make_range(notes_begin(Shdr, Err), notes_end());
550 }
551
553 Elf_Shdr_Range Sections,
554 WarningHandler WarnHandler = &defaultWarningHandler) const;
555 Expected<uint32_t> getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
556 DataRegion<Elf_Word> ShndxTable) const;
558 const Elf_Shdr *SymTab,
559 DataRegion<Elf_Word> ShndxTable) const;
561 Elf_Sym_Range Symtab,
562 DataRegion<Elf_Word> ShndxTable) const;
564
566 uint32_t Index) const;
567
569 getSectionName(const Elf_Shdr &Section,
570 WarningHandler WarnHandler = &defaultWarningHandler) const;
571 Expected<StringRef> getSectionName(const Elf_Shdr &Section,
572 StringRef DotShstrtab) const;
573 template <typename T>
576 Expected<ArrayRef<uint8_t>> getSegmentContents(const Elf_Phdr &Phdr) const;
577
578 /// Returns a vector of BBAddrMap structs corresponding to each function
579 /// within the text section that the SHT_LLVM_BB_ADDR_MAP section \p Sec
580 /// is associated with. If the current ELFFile is relocatable, a corresponding
581 /// \p RelaSec must be passed in as an argument.
582 /// Optional out variable to collect all PGO Analyses. New elements are only
583 /// added if no error occurs. If not provided, the PGO Analyses are decoded
584 /// then ignored.
586 decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec = nullptr,
587 std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;
588
589 /// Returns a map from every section matching \p IsMatch to its relocation
590 /// section, or \p nullptr if it has no relocation section. This function
591 /// returns an error if any of the \p IsMatch calls fail or if it fails to
592 /// retrieve the content section of any relocation section.
595 std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const;
596
598};
599
604
605template <class ELFT>
607getSection(typename ELFT::ShdrRange Sections, uint32_t Index) {
608 if (Index >= Sections.size())
609 return createError("invalid section index: " + Twine(Index));
610 return &Sections[Index];
611}
612
613template <class ELFT>
615getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym, unsigned SymIndex,
617 assert(Sym.st_shndx == ELF::SHN_XINDEX);
618 if (!ShndxTable.First)
619 return createError(
620 "found an extended symbol index (" + Twine(SymIndex) +
621 "), but unable to locate the extended symbol index table");
622
623 Expected<typename ELFT::Word> TableOrErr = ShndxTable[SymIndex];
624 if (!TableOrErr)
625 return createError("unable to read an extended symbol table at index " +
626 Twine(SymIndex) + ": " +
627 toString(TableOrErr.takeError()));
628 return *TableOrErr;
629}
630
631template <class ELFT>
633ELFFile<ELFT>::getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
634 DataRegion<Elf_Word> ShndxTable) const {
635 uint32_t Index = Sym.st_shndx;
636 if (Index == ELF::SHN_XINDEX) {
637 Expected<uint32_t> ErrorOrIndex =
638 getExtendedSymbolTableIndex<ELFT>(Sym, &Sym - Syms.begin(), ShndxTable);
639 if (!ErrorOrIndex)
640 return ErrorOrIndex.takeError();
641 return *ErrorOrIndex;
642 }
643 if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
644 return 0;
645 return Index;
646}
647
648template <class ELFT>
650ELFFile<ELFT>::getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab,
651 DataRegion<Elf_Word> ShndxTable) const {
652 auto SymsOrErr = symbols(SymTab);
653 if (!SymsOrErr)
654 return SymsOrErr.takeError();
655 return getSection(Sym, *SymsOrErr, ShndxTable);
656}
657
658template <class ELFT>
660ELFFile<ELFT>::getSection(const Elf_Sym &Sym, Elf_Sym_Range Symbols,
661 DataRegion<Elf_Word> ShndxTable) const {
662 auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable);
663 if (!IndexOrErr)
664 return IndexOrErr.takeError();
665 uint32_t Index = *IndexOrErr;
666 if (Index == 0)
667 return nullptr;
668 return getSection(Index);
669}
670
671template <class ELFT>
673ELFFile<ELFT>::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
674 auto SymsOrErr = symbols(Sec);
675 if (!SymsOrErr)
676 return SymsOrErr.takeError();
677
678 Elf_Sym_Range Symbols = *SymsOrErr;
679 if (Index >= Symbols.size())
680 return createError("unable to get symbol from section " +
681 getSecIndexForError(*this, *Sec) +
682 ": invalid symbol index (" + Twine(Index) + ")");
683 return &Symbols[Index];
684}
685
686template <class ELFT>
687template <typename T>
690 if (Sec.sh_entsize != sizeof(T) && sizeof(T) != 1)
691 return createError("section " + getSecIndexForError(*this, Sec) +
692 " has invalid sh_entsize: expected " + Twine(sizeof(T)) +
693 ", but got " + Twine(Sec.sh_entsize));
694
695 uintX_t Offset = Sec.sh_offset;
696 uintX_t Size = Sec.sh_size;
697
698 if (Size % sizeof(T))
699 return createError("section " + getSecIndexForError(*this, Sec) +
700 " has an invalid sh_size (" + Twine(Size) +
701 ") which is not a multiple of its sh_entsize (" +
702 Twine(Sec.sh_entsize) + ")");
703 if (std::numeric_limits<uintX_t>::max() - Offset < Size)
704 return createError("section " + getSecIndexForError(*this, Sec) +
705 " has a sh_offset (0x" + Twine::utohexstr(Offset) +
706 ") + sh_size (0x" + Twine::utohexstr(Size) +
707 ") that cannot be represented");
708 if (Offset + Size > Buf.size())
709 return createError("section " + getSecIndexForError(*this, Sec) +
710 " has a sh_offset (0x" + Twine::utohexstr(Offset) +
711 ") + sh_size (0x" + Twine::utohexstr(Size) +
712 ") that is greater than the file size (0x" +
713 Twine::utohexstr(Buf.size()) + ")");
714
715 if (Offset % alignof(T))
716 // TODO: this error is untested.
717 return createError("unaligned data");
718
719 const T *Start = reinterpret_cast<const T *>(base() + Offset);
720 return ArrayRef(Start, Size / sizeof(T));
721}
722
723template <class ELFT>
725ELFFile<ELFT>::getSegmentContents(const Elf_Phdr &Phdr) const {
726 uintX_t Offset = Phdr.p_offset;
727 uintX_t Size = Phdr.p_filesz;
728
729 if (std::numeric_limits<uintX_t>::max() - Offset < Size)
730 return createError("program header " + getPhdrIndexForError(*this, Phdr) +
731 " has a p_offset (0x" + Twine::utohexstr(Offset) +
732 ") + p_filesz (0x" + Twine::utohexstr(Size) +
733 ") that cannot be represented");
734 if (Offset + Size > Buf.size())
735 return createError("program header " + getPhdrIndexForError(*this, Phdr) +
736 " has a p_offset (0x" + Twine::utohexstr(Offset) +
737 ") + p_filesz (0x" + Twine::utohexstr(Size) +
738 ") that is greater than the file size (0x" +
739 Twine::utohexstr(Buf.size()) + ")");
740 return ArrayRef(base() + Offset, Size);
741}
742
743template <class ELFT>
745ELFFile<ELFT>::getSectionContents(const Elf_Shdr &Sec) const {
747}
748
749template <class ELFT>
753
754template <class ELFT>
756 SmallVectorImpl<char> &Result) const {
757 if (!isMipsELF64()) {
759 Result.append(Name.begin(), Name.end());
760 } else {
761 // The Mips N64 ABI allows up to three operations to be specified per
762 // relocation record. Unfortunately there's no easy way to test for the
763 // presence of N64 ELFs as they have no special flag that identifies them
764 // as being N64. We can safely assume at the moment that all Mips
765 // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
766 // information to disambiguate between old vs new ABIs.
767 uint8_t Type1 = (Type >> 0) & 0xFF;
768 uint8_t Type2 = (Type >> 8) & 0xFF;
769 uint8_t Type3 = (Type >> 16) & 0xFF;
770
771 // Concat all three relocation type names.
772 StringRef Name = getRelocationTypeName(Type1);
773 Result.append(Name.begin(), Name.end());
774
775 Name = getRelocationTypeName(Type2);
776 Result.append(1, '/');
777 Result.append(Name.begin(), Name.end());
778
779 Name = getRelocationTypeName(Type3);
780 Result.append(1, '/');
781 Result.append(Name.begin(), Name.end());
782 }
783}
784
785template <class ELFT>
789
790template <class ELFT>
792ELFFile<ELFT>::loadVersionMap(const Elf_Shdr *VerNeedSec,
793 const Elf_Shdr *VerDefSec) const {
795
796 // The first two version indexes are reserved.
797 // Index 0 is VER_NDX_LOCAL, index 1 is VER_NDX_GLOBAL.
798 VersionMap.push_back(VersionEntry());
799 VersionMap.push_back(VersionEntry());
800
801 auto InsertEntry = [&](unsigned N, StringRef Version, bool IsVerdef) {
802 if (N >= VersionMap.size())
803 VersionMap.resize(N + 1);
804 VersionMap[N] = {std::string(Version), IsVerdef};
805 };
806
807 if (VerDefSec) {
809 if (!Defs)
810 return Defs.takeError();
811 for (const VerDef &Def : *Defs)
812 InsertEntry(Def.Ndx & ELF::VERSYM_VERSION, Def.Name, true);
813 }
814
815 if (VerNeedSec) {
817 if (!Deps)
818 return Deps.takeError();
819 for (const VerNeed &Dep : *Deps)
820 for (const VernAux &Aux : Dep.AuxV)
821 InsertEntry(Aux.Other & ELF::VERSYM_VERSION, Aux.Name, false);
822 }
823
824 return VersionMap;
825}
826
827template <class ELFT>
830 const Elf_Shdr *SymTab) const {
831 uint32_t Index = Rel.getSymbol(isMips64EL());
832 if (Index == 0)
833 return nullptr;
834 return getEntry<Elf_Sym>(*SymTab, Index);
835}
836
837template <class ELFT>
840 WarningHandler WarnHandler) const {
841 Expected<uint32_t> ShStrNdxOrErr = getShStrNdx();
842 if (!ShStrNdxOrErr)
843 return createError(
844 "e_shstrndx == SHN_XINDEX, but cannot read section header 0: " +
845 toString(ShStrNdxOrErr.takeError()));
846
847 uint32_t Index = *ShStrNdxOrErr;
848 // There is no section name string table. Return FakeSectionStrings which
849 // is non-empty if we have created fake sections.
850 if (!Index)
851 return FakeSectionStrings;
852
853 if (Index >= Sections.size())
854 return createError("section header string table index " + Twine(Index) +
855 " does not exist");
856 return getStringTable(Sections[Index], WarnHandler);
857}
858
859/// This function finds the number of dynamic symbols using a GNU hash table.
860///
861/// @param Table The GNU hash table for .dynsym.
862template <class ELFT>
864getDynSymtabSizeFromGnuHash(const typename ELFT::GnuHash &Table,
865 const void *BufEnd) {
866 using Elf_Word = typename ELFT::Word;
867 if (Table.nbuckets == 0)
868 return Table.symndx + 1;
869 uint64_t LastSymIdx = 0;
870 // Find the index of the first symbol in the last chain.
871 for (Elf_Word Val : Table.buckets())
872 LastSymIdx = std::max(LastSymIdx, (uint64_t)Val);
873 const Elf_Word *It =
874 reinterpret_cast<const Elf_Word *>(Table.values(LastSymIdx).end());
875 // Locate the end of the chain to find the last symbol index.
876 while (It < BufEnd && (*It & 1) == 0) {
877 ++LastSymIdx;
878 ++It;
879 }
880 if (It >= BufEnd) {
881 return createStringError(
883 "no terminator found for GNU hash section before buffer end");
884 }
885 return LastSymIdx + 1;
886}
887
888/// This function determines the number of dynamic symbols. It reads section
889/// headers first. If section headers are not available, the number of
890/// symbols will be inferred by parsing dynamic hash tables.
891template <class ELFT>
893 // Read .dynsym section header first if available.
894 Expected<Elf_Shdr_Range> SectionsOrError = sections();
895 if (!SectionsOrError)
896 return SectionsOrError.takeError();
897 for (const Elf_Shdr &Sec : *SectionsOrError) {
898 if (Sec.sh_type == ELF::SHT_DYNSYM) {
899 if (Sec.sh_size % Sec.sh_entsize != 0) {
901 "SHT_DYNSYM section has sh_size (" +
902 Twine(Sec.sh_size) + ") % sh_entsize (" +
903 Twine(Sec.sh_entsize) + ") that is not 0");
904 }
905 return Sec.sh_size / Sec.sh_entsize;
906 }
907 }
908
909 if (!SectionsOrError->empty()) {
910 // Section headers are available but .dynsym header is not found.
911 // Return 0 as .dynsym does not exist.
912 return 0;
913 }
914
915 // Section headers do not exist. Falling back to infer
916 // upper bound of .dynsym from .gnu.hash and .hash.
918 if (!DynTable)
919 return DynTable.takeError();
920 std::optional<uint64_t> ElfHash;
921 std::optional<uint64_t> ElfGnuHash;
922 for (const Elf_Dyn &Entry : *DynTable) {
923 switch (Entry.d_tag) {
924 case ELF::DT_HASH:
925 ElfHash = Entry.d_un.d_ptr;
926 break;
927 case ELF::DT_GNU_HASH:
928 ElfGnuHash = Entry.d_un.d_ptr;
929 break;
930 }
931 }
932 if (ElfGnuHash) {
933 Expected<const uint8_t *> TablePtr = toMappedAddr(*ElfGnuHash);
934 if (!TablePtr)
935 return TablePtr.takeError();
936 const Elf_GnuHash *Table =
937 reinterpret_cast<const Elf_GnuHash *>(TablePtr.get());
938 return getDynSymtabSizeFromGnuHash<ELFT>(*Table, this->Buf.bytes_end());
939 }
940
941 // Search SYSV hash table to try to find the upper bound of dynsym.
942 if (ElfHash) {
943 Expected<const uint8_t *> TablePtr = toMappedAddr(*ElfHash);
944 if (!TablePtr)
945 return TablePtr.takeError();
946 const Elf_Hash *Table = reinterpret_cast<const Elf_Hash *>(TablePtr.get());
947 return Table->nchain;
948 }
949 return 0;
950}
951
952template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
953
954template <class ELFT> Error ELFFile<ELFT>::readShdrZero() {
955 const Elf_Ehdr &Header = getHeader();
956
957 // If e_shnum == 0 && e_shoff == 0, this indicates that there are no sections,
958 // which is valid for an ELF file.
959 //
960 // However, if e_phnum == PN_XNUM or e_shstrndx == SHN_XINDEX while
961 // e_shoff == 0, the file is inconsistent, because such entries indicate
962 // information should be stored in the index 0 section header, whereas e_shoff
963 // 0 indicates that there are no section headers. In that case, an error will
964 // be triggered later when getSection() is called and detects that e_shoff ==
965 // 0.
966 if ((Header.e_phnum == ELF::PN_XNUM ||
967 (Header.e_shnum == 0 && Header.e_shoff != 0) ||
968 Header.e_shstrndx == ELF::SHN_XINDEX)) {
969 // Pretend we have section 0 or sections() would call getShNum and thus
970 // become an infinite recursion.
971 RealShNum = 1;
972 auto SecOrErr = getSection(0);
973 if (!SecOrErr) {
974 if (Header.e_shnum != 0)
975 RealShNum = Header.e_shnum;
976 else
977 RealShNum = std::nullopt;
978 if (Header.e_phnum != ELF::PN_XNUM)
979 RealPhNum = Header.e_phnum;
980 if (Header.e_shstrndx != ELF::SHN_XINDEX)
981 RealShStrNdx = Header.e_shstrndx;
982 return SecOrErr.takeError();
983 }
984
985 RealPhNum =
986 Header.e_phnum == ELF::PN_XNUM ? (*SecOrErr)->sh_info : Header.e_phnum;
987 RealShNum = Header.e_shnum == 0 ? (*SecOrErr)->sh_size : Header.e_shnum;
988 RealShStrNdx = Header.e_shstrndx == ELF::SHN_XINDEX ? (*SecOrErr)->sh_link
989 : Header.e_shstrndx;
990 } else {
991 RealPhNum = Header.e_phnum;
992 RealShNum = Header.e_shnum;
993 RealShStrNdx = Header.e_shstrndx;
994 }
995
996 return Error::success();
997}
998
999template <class ELFT>
1001 if (sizeof(Elf_Ehdr) > Object.size())
1002 return createError("invalid buffer: the size (" + Twine(Object.size()) +
1003 ") is smaller than an ELF header (" +
1004 Twine(sizeof(Elf_Ehdr)) + ")");
1005 return ELFFile(Object);
1006}
1007
1008/// Used by llvm-objdump -d (which needs sections for disassembly) to
1009/// disassemble objects without a section header table (e.g. ET_CORE objects
1010/// analyzed by linux perf or ET_EXEC with llvm-strip --strip-sections).
1011template <class ELFT> void ELFFile<ELFT>::createFakeSections() {
1012 if (!FakeSections.empty())
1013 return;
1014 auto PhdrsOrErr = program_headers();
1015 if (!PhdrsOrErr)
1016 return;
1017
1018 FakeSectionStrings += '\0';
1019 for (auto [Idx, Phdr] : llvm::enumerate(*PhdrsOrErr)) {
1020 if (Phdr.p_type != ELF::PT_LOAD || !(Phdr.p_flags & ELF::PF_X))
1021 continue;
1022 Elf_Shdr FakeShdr = {};
1023 FakeShdr.sh_type = ELF::SHT_PROGBITS;
1024 FakeShdr.sh_flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
1025 FakeShdr.sh_addr = Phdr.p_vaddr;
1026 FakeShdr.sh_size = Phdr.p_memsz;
1027 FakeShdr.sh_offset = Phdr.p_offset;
1028 // Create a section name based on the p_type and index.
1029 FakeShdr.sh_name = FakeSectionStrings.size();
1030 FakeSectionStrings += ("PT_LOAD#" + Twine(Idx)).str();
1031 FakeSectionStrings += '\0';
1032 FakeSections.push_back(FakeShdr);
1033 }
1034}
1035
1036template <class ELFT>
1038 const uintX_t SectionTableOffset = getHeader().e_shoff;
1039 if (SectionTableOffset == 0) {
1040 if (!FakeSections.empty())
1041 return ArrayRef(FakeSections);
1042 return ArrayRef<Elf_Shdr>();
1043 }
1044
1045 if (getHeader().e_shentsize != sizeof(Elf_Shdr))
1046 return createError("invalid e_shentsize in ELF header: " +
1047 Twine(getHeader().e_shentsize));
1048
1049 const uint64_t FileSize = Buf.size();
1050 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize ||
1051 SectionTableOffset + (uintX_t)sizeof(Elf_Shdr) < SectionTableOffset)
1052 return createError(
1053 "section header table goes past the end of the file: e_shoff = 0x" +
1054 Twine::utohexstr(SectionTableOffset));
1055
1056 // Invalid address alignment of section headers
1057 if (SectionTableOffset & (alignof(Elf_Shdr) - 1))
1058 // TODO: this error is untested.
1059 return createError("invalid alignment of section headers");
1060
1061 const Elf_Shdr *First =
1062 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
1063
1064 uintX_t NumSections = 0;
1065 if (Expected<uint64_t> ShNumOrErr = getShNum())
1066 NumSections = *ShNumOrErr;
1067 else
1068 return ShNumOrErr.takeError();
1069
1070 if (NumSections > UINT64_MAX / sizeof(Elf_Shdr))
1071 return createError("invalid number of sections specified in the NULL "
1072 "section's sh_size field (" +
1073 Twine(NumSections) + ")");
1074
1075 const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr);
1076 if (SectionTableOffset + SectionTableSize < SectionTableOffset)
1077 return createError(
1078 "invalid section header table offset (e_shoff = 0x" +
1079 Twine::utohexstr(SectionTableOffset) +
1080 ") or invalid number of sections specified in the first section "
1081 "header's sh_size field (0x" +
1082 Twine::utohexstr(NumSections) + ")");
1083
1084 // Section table goes past end of file!
1085 if (SectionTableOffset + SectionTableSize > FileSize)
1086 return createError("section table goes past the end of file");
1087 return ArrayRef(First, NumSections);
1088}
1089
1090template <class ELFT>
1091template <typename T>
1093 uint32_t Entry) const {
1094 auto SecOrErr = getSection(Section);
1095 if (!SecOrErr)
1096 return SecOrErr.takeError();
1097 return getEntry<T>(**SecOrErr, Entry);
1098}
1099
1100template <class ELFT>
1101template <typename T>
1103 uint32_t Entry) const {
1104 Expected<ArrayRef<T>> EntriesOrErr = getSectionContentsAsArray<T>(Section);
1105 if (!EntriesOrErr)
1106 return EntriesOrErr.takeError();
1107
1108 ArrayRef<T> Arr = *EntriesOrErr;
1109 if (Entry >= Arr.size())
1110 return createError(
1111 "can't read an entry at 0x" +
1112 Twine::utohexstr(Entry * static_cast<uint64_t>(sizeof(T))) +
1113 ": it goes past the end of the section (0x" +
1114 Twine::utohexstr(Section.sh_size) + ")");
1115 return &Arr[Entry];
1116}
1117
1118template <typename ELFT>
1120 uint32_t SymbolVersionIndex, bool &IsDefault,
1121 SmallVector<std::optional<VersionEntry>, 0> &VersionMap,
1122 std::optional<bool> IsSymHidden) const {
1123 size_t VersionIndex = SymbolVersionIndex & llvm::ELF::VERSYM_VERSION;
1124
1125 // Special markers for unversioned symbols.
1126 if (VersionIndex == llvm::ELF::VER_NDX_LOCAL ||
1127 VersionIndex == llvm::ELF::VER_NDX_GLOBAL) {
1128 IsDefault = false;
1129 return "";
1130 }
1131
1132 // Lookup this symbol in the version table.
1133 if (VersionIndex >= VersionMap.size() || !VersionMap[VersionIndex])
1134 return createError("SHT_GNU_versym section refers to a version index " +
1135 Twine(VersionIndex) + " which is missing");
1136
1137 const VersionEntry &Entry = *VersionMap[VersionIndex];
1138 // A default version (@@) is only available for defined symbols.
1139 if (!Entry.IsVerDef || IsSymHidden.value_or(false))
1140 IsDefault = false;
1141 else
1142 IsDefault = !(SymbolVersionIndex & llvm::ELF::VERSYM_HIDDEN);
1143 return Entry.Name.c_str();
1144}
1145
1146template <class ELFT>
1148ELFFile<ELFT>::getVersionDefinitions(const Elf_Shdr &Sec) const {
1149 Expected<StringRef> StrTabOrErr = getLinkAsStrtab(Sec);
1150 if (!StrTabOrErr)
1151 return StrTabOrErr.takeError();
1152
1153 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
1154 if (!ContentsOrErr)
1155 return createError("cannot read content of " + describe(*this, Sec) + ": " +
1156 toString(ContentsOrErr.takeError()));
1157
1158 const uint8_t *Start = ContentsOrErr->data();
1159 const uint8_t *End = Start + ContentsOrErr->size();
1160
1161 auto ExtractNextAux = [&](const uint8_t *&VerdauxBuf,
1162 unsigned VerDefNdx) -> Expected<VerdAux> {
1163 if (VerdauxBuf + sizeof(Elf_Verdaux) > End)
1164 return createError("invalid " + describe(*this, Sec) +
1165 ": version definition " + Twine(VerDefNdx) +
1166 " refers to an auxiliary entry that goes past the end "
1167 "of the section");
1168
1169 auto *Verdaux = reinterpret_cast<const Elf_Verdaux *>(VerdauxBuf);
1170 VerdauxBuf += Verdaux->vda_next;
1171
1172 VerdAux Aux;
1173 Aux.Offset = VerdauxBuf - Start;
1174 if (Verdaux->vda_name < StrTabOrErr->size())
1175 Aux.Name = std::string(StrTabOrErr->drop_front(Verdaux->vda_name).data());
1176 else
1177 Aux.Name = ("<invalid vda_name: " + Twine(Verdaux->vda_name) + ">").str();
1178 return Aux;
1179 };
1180
1181 std::vector<VerDef> Ret;
1182 const uint8_t *VerdefBuf = Start;
1183 for (unsigned I = 1; I <= /*VerDefsNum=*/Sec.sh_info; ++I) {
1184 if (VerdefBuf + sizeof(Elf_Verdef) > End)
1185 return createError("invalid " + describe(*this, Sec) +
1186 ": version definition " + Twine(I) +
1187 " goes past the end of the section");
1188
1189 if (reinterpret_cast<uintptr_t>(VerdefBuf) % sizeof(uint32_t) != 0)
1190 return createError(
1191 "invalid " + describe(*this, Sec) +
1192 ": found a misaligned version definition entry at offset 0x" +
1193 Twine::utohexstr(VerdefBuf - Start));
1194
1195 unsigned Version = *reinterpret_cast<const Elf_Half *>(VerdefBuf);
1196 if (Version != 1)
1197 return createError("unable to dump " + describe(*this, Sec) +
1198 ": version " + Twine(Version) +
1199 " is not yet supported");
1200
1201 const Elf_Verdef *D = reinterpret_cast<const Elf_Verdef *>(VerdefBuf);
1202 VerDef &VD = *Ret.emplace(Ret.end());
1203 VD.Offset = VerdefBuf - Start;
1204 VD.Version = D->vd_version;
1205 VD.Flags = D->vd_flags;
1206 VD.Ndx = D->vd_ndx;
1207 VD.Cnt = D->vd_cnt;
1208 VD.Hash = D->vd_hash;
1209
1210 const uint8_t *VerdauxBuf = VerdefBuf + D->vd_aux;
1211 for (unsigned J = 0; J < D->vd_cnt; ++J) {
1212 if (reinterpret_cast<uintptr_t>(VerdauxBuf) % sizeof(uint32_t) != 0)
1213 return createError("invalid " + describe(*this, Sec) +
1214 ": found a misaligned auxiliary entry at offset 0x" +
1215 Twine::utohexstr(VerdauxBuf - Start));
1216
1217 Expected<VerdAux> AuxOrErr = ExtractNextAux(VerdauxBuf, I);
1218 if (!AuxOrErr)
1219 return AuxOrErr.takeError();
1220
1221 if (J == 0)
1222 VD.Name = AuxOrErr->Name;
1223 else
1224 VD.AuxV.push_back(*AuxOrErr);
1225 }
1226
1227 VerdefBuf += D->vd_next;
1228 }
1229
1230 return Ret;
1231}
1232
1233template <class ELFT>
1236 WarningHandler WarnHandler) const {
1237 StringRef StrTab;
1238 Expected<StringRef> StrTabOrErr = getLinkAsStrtab(Sec);
1239 if (!StrTabOrErr) {
1240 if (Error E = WarnHandler(toString(StrTabOrErr.takeError())))
1241 return std::move(E);
1242 } else {
1243 StrTab = *StrTabOrErr;
1244 }
1245
1246 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
1247 if (!ContentsOrErr)
1248 return createError("cannot read content of " + describe(*this, Sec) + ": " +
1249 toString(ContentsOrErr.takeError()));
1250
1251 const uint8_t *Start = ContentsOrErr->data();
1252 const uint8_t *End = Start + ContentsOrErr->size();
1253 const uint8_t *VerneedBuf = Start;
1254
1255 std::vector<VerNeed> Ret;
1256 for (unsigned I = 1; I <= /*VerneedNum=*/Sec.sh_info; ++I) {
1257 if (VerneedBuf + sizeof(Elf_Verdef) > End)
1258 return createError("invalid " + describe(*this, Sec) +
1259 ": version dependency " + Twine(I) +
1260 " goes past the end of the section");
1261
1262 if (reinterpret_cast<uintptr_t>(VerneedBuf) % sizeof(uint32_t) != 0)
1263 return createError(
1264 "invalid " + describe(*this, Sec) +
1265 ": found a misaligned version dependency entry at offset 0x" +
1266 Twine::utohexstr(VerneedBuf - Start));
1267
1268 unsigned Version = *reinterpret_cast<const Elf_Half *>(VerneedBuf);
1269 if (Version != 1)
1270 return createError("unable to dump " + describe(*this, Sec) +
1271 ": version " + Twine(Version) +
1272 " is not yet supported");
1273
1274 const Elf_Verneed *Verneed =
1275 reinterpret_cast<const Elf_Verneed *>(VerneedBuf);
1276
1277 VerNeed &VN = *Ret.emplace(Ret.end());
1278 VN.Version = Verneed->vn_version;
1279 VN.Cnt = Verneed->vn_cnt;
1280 VN.Offset = VerneedBuf - Start;
1281
1282 if (Verneed->vn_file < StrTab.size())
1283 VN.File = std::string(StrTab.data() + Verneed->vn_file);
1284 else
1285 VN.File = ("<corrupt vn_file: " + Twine(Verneed->vn_file) + ">").str();
1286
1287 const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux;
1288 for (unsigned J = 0; J < Verneed->vn_cnt; ++J) {
1289 if (reinterpret_cast<uintptr_t>(VernauxBuf) % sizeof(uint32_t) != 0)
1290 return createError("invalid " + describe(*this, Sec) +
1291 ": found a misaligned auxiliary entry at offset 0x" +
1292 Twine::utohexstr(VernauxBuf - Start));
1293
1294 if (VernauxBuf + sizeof(Elf_Vernaux) > End)
1295 return createError(
1296 "invalid " + describe(*this, Sec) + ": version dependency " +
1297 Twine(I) +
1298 " refers to an auxiliary entry that goes past the end "
1299 "of the section");
1300
1301 const Elf_Vernaux *Vernaux =
1302 reinterpret_cast<const Elf_Vernaux *>(VernauxBuf);
1303
1304 VernAux &Aux = *VN.AuxV.emplace(VN.AuxV.end());
1305 Aux.Hash = Vernaux->vna_hash;
1306 Aux.Flags = Vernaux->vna_flags;
1307 Aux.Other = Vernaux->vna_other;
1308 Aux.Offset = VernauxBuf - Start;
1309 if (StrTab.size() <= Vernaux->vna_name)
1310 Aux.Name = "<corrupt>";
1311 else
1312 Aux.Name = std::string(StrTab.drop_front(Vernaux->vna_name));
1313
1314 VernauxBuf += Vernaux->vna_next;
1315 }
1316 VerneedBuf += Verneed->vn_next;
1317 }
1318 return Ret;
1319}
1320
1321template <class ELFT>
1324 auto TableOrErr = sections();
1325 if (!TableOrErr)
1326 return TableOrErr.takeError();
1327 return object::getSection<ELFT>(*TableOrErr, Index);
1328}
1329
1330template <class ELFT>
1332ELFFile<ELFT>::getStringTable(const Elf_Shdr &Section,
1333 WarningHandler WarnHandler) const {
1334 if (Section.sh_type != ELF::SHT_STRTAB)
1335 if (Error E = WarnHandler("invalid sh_type for string table section " +
1336 getSecIndexForError(*this, Section) +
1337 ": expected SHT_STRTAB, but got " +
1339 getHeader().e_machine, Section.sh_type)))
1340 return std::move(E);
1341
1342 auto V = getSectionContentsAsArray<char>(Section);
1343 if (!V)
1344 return V.takeError();
1345 ArrayRef<char> Data = *V;
1346 if (Data.empty())
1347 return createError("SHT_STRTAB string table section " +
1348 getSecIndexForError(*this, Section) + " is empty");
1349 if (Data.back() != '\0')
1350 return createError("SHT_STRTAB string table section " +
1351 getSecIndexForError(*this, Section) +
1352 " is non-null terminated");
1353 return StringRef(Data.begin(), Data.size());
1354}
1355
1356template <class ELFT>
1358ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section) const {
1359 auto SectionsOrErr = sections();
1360 if (!SectionsOrErr)
1361 return SectionsOrErr.takeError();
1362 return getSHNDXTable(Section, *SectionsOrErr);
1363}
1364
1365template <class ELFT>
1367ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section,
1368 Elf_Shdr_Range Sections) const {
1369 assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
1370 auto VOrErr = getSectionContentsAsArray<Elf_Word>(Section);
1371 if (!VOrErr)
1372 return VOrErr.takeError();
1373 ArrayRef<Elf_Word> V = *VOrErr;
1374 auto SymTableOrErr = object::getSection<ELFT>(Sections, Section.sh_link);
1375 if (!SymTableOrErr)
1376 return SymTableOrErr.takeError();
1377 const Elf_Shdr &SymTable = **SymTableOrErr;
1378 if (SymTable.sh_type != ELF::SHT_SYMTAB &&
1379 SymTable.sh_type != ELF::SHT_DYNSYM)
1380 return createError(
1381 "SHT_SYMTAB_SHNDX section is linked with " +
1382 object::getELFSectionTypeName(getHeader().e_machine, SymTable.sh_type) +
1383 " section (expected SHT_SYMTAB/SHT_DYNSYM)");
1384
1385 uint64_t Syms = SymTable.sh_size / sizeof(Elf_Sym);
1386 if (V.size() != Syms)
1387 return createError("SHT_SYMTAB_SHNDX has " + Twine(V.size()) +
1388 " entries, but the symbol table associated has " +
1389 Twine(Syms));
1390
1391 return V;
1392}
1393
1394template <class ELFT>
1396ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
1397 auto SectionsOrErr = sections();
1398 if (!SectionsOrErr)
1399 return SectionsOrErr.takeError();
1400 return getStringTableForSymtab(Sec, *SectionsOrErr);
1401}
1402
1403template <class ELFT>
1406 Elf_Shdr_Range Sections) const {
1407
1408 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
1409 return createError(
1410 "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
1411 Expected<const Elf_Shdr *> SectionOrErr =
1412 object::getSection<ELFT>(Sections, Sec.sh_link);
1413 if (!SectionOrErr)
1414 return SectionOrErr.takeError();
1415 return getStringTable(**SectionOrErr);
1416}
1417
1418template <class ELFT>
1420ELFFile<ELFT>::getLinkAsStrtab(const typename ELFT::Shdr &Sec) const {
1422 getSection(Sec.sh_link);
1423 if (!StrTabSecOrErr)
1424 return createError("invalid section linked to " + describe(*this, Sec) +
1425 ": " + toString(StrTabSecOrErr.takeError()));
1426
1427 Expected<StringRef> StrTabOrErr = getStringTable(**StrTabSecOrErr);
1428 if (!StrTabOrErr)
1429 return createError("invalid string table linked to " +
1430 describe(*this, Sec) + ": " +
1431 toString(StrTabOrErr.takeError()));
1432 return *StrTabOrErr;
1433}
1434
1435template <class ELFT>
1437ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section,
1438 WarningHandler WarnHandler) const {
1439 auto SectionsOrErr = sections();
1440 if (!SectionsOrErr)
1441 return SectionsOrErr.takeError();
1442 auto Table = getSectionStringTable(*SectionsOrErr, WarnHandler);
1443 if (!Table)
1444 return Table.takeError();
1445 return getSectionName(Section, *Table);
1446}
1447
1448template <class ELFT>
1450 StringRef DotShstrtab) const {
1451 uint32_t Offset = Section.sh_name;
1452 if (Offset == 0)
1453 return StringRef();
1454 if (Offset >= DotShstrtab.size())
1455 return createError("a section " + getSecIndexForError(*this, Section) +
1456 " has an invalid sh_name (0x" +
1458 ") offset which goes past the end of the "
1459 "section name string table");
1460 return StringRef(DotShstrtab.data() + Offset);
1461}
1462
1463/// This function returns the hash value for a symbol in the .dynsym section
1464/// Name of the API remains consistent as specified in the libelf
1465/// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
1466inline uint32_t hashSysV(StringRef SymbolName) {
1467 uint32_t H = 0;
1468 for (uint8_t C : SymbolName) {
1469 H = (H << 4) + C;
1470 H ^= (H >> 24) & 0xf0;
1471 }
1472 return H & 0x0fffffff;
1473}
1474
1475/// This function returns the hash value for a symbol in the .dynsym section
1476/// for the GNU hash table. The implementation is defined in the GNU hash ABI.
1477/// REF : https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elf.c#l222
1479 uint32_t H = 5381;
1480 for (uint8_t C : Name)
1481 H = (H << 5) + H + C;
1482 return H;
1483}
1484
1489
1490} // end namespace object
1491} // end namespace llvm
1492
1493#endif // LLVM_OBJECT_ELF_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
bbsections Prepares for basic block sections
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_TEMPLATE_ABI
Definition Compiler.h:216
static bool isMips64EL(const ELFYAML::Object &Obj)
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition ELFTypes.h:119
#define I(x, y, z)
Definition MD5.cpp:57
#define H(x, y, z)
Definition MD5.cpp:56
This file implements a map that provides insertion order iteration.
#define T
Function const char TargetMachine * Machine
This file defines the SmallString class.
This file defines the SmallVector class.
static Split data
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Error takeError()
Return error contained inside this Cursor, if any.
Helper for Errors used as out-parameters.
Definition Error.h:1160
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
const unsigned char * bytes_end() const
Definition StringRef.h:125
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:635
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
An efficient, type-erasing, non-owning reference to a callable.
A range adaptor for a pair of iterators.
ELFFile(const ELFFile &)=default
llvm::function_ref< Error(const Twine &Msg)> WarningHandler
Definition ELF.h:272
const Elf_Ehdr & getHeader() const
Definition ELF.h:346
Expected< std::vector< Elf_Rela > > android_relas(const Elf_Shdr &Sec) const
Definition ELF.cpp:468
Expected< StringRef > getLinkAsStrtab(const typename ELFT::Shdr &Sec) const
Definition ELF.h:1420
static Expected< ELFFile > create(StringRef Object)
Definition ELF.h:1000
Expected< const Elf_Shdr * > getSection(uint32_t Index) const
Definition ELF.h:1323
Expected< StringRef > getSectionName(const Elf_Shdr &Section, StringRef DotShstrtab) const
Definition ELF.h:1449
Expected< ArrayRef< Elf_Word > > getSHNDXTable(const Elf_Shdr &Section, Elf_Shdr_Range Sections) const
Definition ELF.h:1367
Expected< const Elf_Sym * > getSymbol(const Elf_Shdr *Sec, uint32_t Index) const
Definition ELF.h:673
Expected< std::vector< VerDef > > getVersionDefinitions(const Elf_Shdr &Sec) const
Definition ELF.h:1148
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
Definition ELF.cpp:536
Expected< ArrayRef< Elf_Word > > getSHNDXTable(const Elf_Shdr &Section) const
Definition ELF.h:1358
Expected< const Elf_Shdr * > getSection(const Elf_Sym &Sym, Elf_Sym_Range Symtab, DataRegion< Elf_Word > ShndxTable) const
Definition ELF.h:660
Expected< Elf_Sym_Range > symbols(const Elf_Shdr *Sec) const
Definition ELF.h:415
Expected< uint64_t > getShNum() const
Definition ELF.h:315
Expected< ArrayRef< uint8_t > > getSegmentContents(const Elf_Phdr &Phdr) const
Definition ELF.h:725
Expected< std::vector< BBAddrMap > > decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec=nullptr, std::vector< PGOAnalysisMap > *PGOAnalyses=nullptr) const
Returns a vector of BBAddrMap structs corresponding to each function within the text section that the...
Definition ELF.cpp:840
Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const
Get an iterator over notes in a section.
Definition ELF.h:502
uint32_t getRelativeRelocationType() const
Definition ELF.h:786
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:535
Expected< StringRef > getSymbolVersionByIndex(uint32_t SymbolVersionIndex, bool &IsDefault, SmallVector< std::optional< VersionEntry >, 0 > &VersionMap, std::optional< bool > IsSymHidden) const
Definition ELF.h:1119
Elf_Note_Iterator notes_begin(const Elf_Phdr &Phdr, Error &Err) const
Get an iterator over notes in a program header.
Definition ELF.h:473
Expected< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr &Sec) const
Definition ELF.h:745
Expected< Elf_Rela_Range > relas(const Elf_Shdr &Sec) const
Definition ELF.h:421
Expected< Elf_Phdr_Range > program_headers() const
Iterate over program header table.
Definition ELF.h:443
Expected< uint32_t > getShStrNdx() const
Definition ELF.h:330
Expected< StringRef > getStringTableForSymtab(const Elf_Shdr &Section) const
Definition ELF.h:1396
Expected< std::vector< VerNeed > > getVersionDependencies(const Elf_Shdr &Sec, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition ELF.h:1235
size_t getBufSize() const
Definition ELF.h:277
Expected< const T * > getEntry(uint32_t Section, uint32_t Entry) const
Definition ELF.h:1092
void getRelocationTypeName(uint32_t Type, SmallVectorImpl< char > &Result) const
Definition ELF.h:755
Expected< const Elf_Sym * > getRelocationSymbol(const Elf_Rel &Rel, const Elf_Shdr *SymTab) const
Get the symbol for a given relocation.
Definition ELF.h:829
Expected< RelsOrRelas > decodeCrel(ArrayRef< uint8_t > Content) const
Definition ELF.cpp:428
const uint8_t * end() const
Definition ELF.h:275
Expected< StringRef > getSectionStringTable(Elf_Shdr_Range Sections, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition ELF.h:839
Expected< uint64_t > getDynSymtabSize() const
This function determines the number of dynamic symbols.
Definition ELF.h:892
Expected< const T * > getEntry(const Elf_Shdr &Section, uint32_t Entry) const
Definition ELF.h:1102
Expected< uint64_t > getCrelHeader(ArrayRef< uint8_t > Content) const
Definition ELF.cpp:416
Expected< Elf_Dyn_Range > dynamicEntries() const
Definition ELF.cpp:625
void createFakeSections()
Used by llvm-objdump -d (which needs sections for disassembly) to disassemble objects without a secti...
Definition ELF.h:1011
ELFFile(const ELFFile &)=default
Expected< Elf_Shdr_Range > sections() const
Definition ELF.h:1037
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:547
const uint8_t * base() const
Definition ELF.h:274
bool isMipsELF64() const
Definition ELF.h:400
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition ELF.cpp:677
Expected< Elf_Relr_Range > relrs(const Elf_Shdr &Sec) const
Definition ELF.h:429
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:853
std::string getDynamicTagAsString(uint64_t Type) const
Definition ELF.cpp:620
bool isLE() const
Definition ELF.h:396
bool isMips64EL() const
Definition ELF.h:405
Elf_Note_Iterator notes_end() const
Get the end iterator for notes.
Definition ELF.h:524
Expected< StringRef > getSectionName(const Elf_Shdr &Section, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition ELF.h:1437
StringRef getRelocationTypeName(uint32_t Type) const
Definition ELF.h:750
Expected< StringRef > getStringTable(const Elf_Shdr &Section, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition ELF.h:1332
llvm::function_ref< Error(const Twine &Msg)> WarningHandler
Definition ELF.h:272
Expected< uint32_t > getPhNum() const
Definition ELF.h:300
Expected< StringRef > getStringTableForSymtab(const Elf_Shdr &Section, Elf_Shdr_Range Sections) const
Definition ELF.h:1405
Expected< ArrayRef< T > > getSectionContentsAsArray(const Elf_Shdr &Sec) const
Definition ELF.h:689
Expected< RelsOrRelas > crels(const Elf_Shdr &Sec) const
Definition ELF.cpp:459
Expected< SmallVector< std::optional< VersionEntry >, 0 > > loadVersionMap(const Elf_Shdr *VerNeedSec, const Elf_Shdr *VerDefSec) const
Definition ELF.h:792
Expected< Elf_Rel_Range > rels(const Elf_Shdr &Sec) const
Definition ELF.h:425
Expected< const Elf_Shdr * > getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab, DataRegion< Elf_Word > ShndxTable) const
Definition ELF.h:650
std::vector< Elf_Rel > decode_relrs(Elf_Relr_Range relrs) const
Definition ELF.cpp:352
std::pair< std::vector< Elf_Rel >, std::vector< Elf_Rela > > RelsOrRelas
Definition ELF.h:436
Expected< uint32_t > getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms, DataRegion< Elf_Word > ShndxTable) const
Definition ELF.h:633
#define UINT64_MAX
Definition DataTypes.h:77
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ SHN_XINDEX
Definition ELF.h:1148
@ SHN_UNDEF
Definition ELF.h:1140
@ SHN_LORESERVE
Definition ELF.h:1141
@ PF_X
Definition ELF.h:1616
@ SHF_ALLOC
Definition ELF.h:1256
@ SHF_TLS
Definition ELF.h:1281
@ SHF_EXECINSTR
Definition ELF.h:1259
@ EI_DATA
Definition ELF.h:56
@ EI_NIDENT
Definition ELF.h:61
@ EI_CLASS
Definition ELF.h:55
@ EM_MIPS
Definition ELF.h:146
@ SHT_STRTAB
Definition ELF.h:1157
@ SHT_PROGBITS
Definition ELF.h:1155
@ SHT_NOBITS
Definition ELF.h:1162
@ SHT_SYMTAB
Definition ELF.h:1156
@ SHT_SYMTAB_SHNDX
Definition ELF.h:1170
@ SHT_NOTE
Definition ELF.h:1161
@ SHT_DYNSYM
Definition ELF.h:1165
constexpr unsigned CREL_HDR_ADDEND
Definition ELF.h:2062
@ ELFDATANONE
Definition ELF.h:339
@ ELFDATA2LSB
Definition ELF.h:340
@ PT_LOAD
Definition ELF.h:1566
@ PT_TLS
Definition ELF.h:1572
@ PT_NOTE
Definition ELF.h:1569
@ ELFCLASS64
Definition ELF.h:334
@ ELFCLASSNONE
Definition ELF.h:332
@ VER_NDX_GLOBAL
Definition ELF.h:1722
@ VERSYM_VERSION
Definition ELF.h:1723
@ VER_NDX_LOCAL
Definition ELF.h:1721
@ VERSYM_HIDDEN
Definition ELF.h:1724
@ PN_XNUM
Definition ELF.h:1136
static constexpr const StringLiteral & getSectionName(DebugSectionKind SectionKind)
Return the name of the section.
Expected< uint32_t > getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym, unsigned SymIndex, DataRegion< typename ELFT::Word > ShndxTable)
Definition ELF.h:615
Error decodeCrel(ArrayRef< uint8_t > Content, function_ref< void(uint64_t, bool)> HdrHandler, function_ref< void(Elf_Crel_Impl< Is64 >)> EntryHandler)
Definition ELF.h:217
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition ELF.h:607
Error createError(const Twine &Err)
Definition Error.h:86
LLVM_ABI StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition ELF.cpp:25
bool checkSectionOffsets(const typename ELFT::Phdr &Phdr, const typename ELFT::Shdr &Sec)
Definition ELF.h:171
LLVM_ABI uint32_t getELFRelativeRelocationType(uint32_t Machine)
Definition ELF.cpp:207
LLVM_ABI StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
std::string getPhdrIndexForError(const ELFFile< ELFT > &Obj, const typename ELFT::Phdr &Phdr)
Definition ELF.h:156
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:1478
LLVM_ABI StringRef getRISCVVendorRelocationTypeName(uint32_t Type, StringRef Vendor)
Definition ELF.cpp:194
PPCInstrMasks
Definition ELF.h:90
@ PLD_R12_NO_DISP
Definition ELF.h:95
@ ADDIS_R12_TO_R2_NO_DISP
Definition ELF.h:92
@ ADDI_R12_TO_R12_NO_DISP
Definition ELF.h:94
@ ADDI_R12_TO_R2_NO_DISP
Definition ELF.h:93
@ PADDI_R12_NO_DISP
Definition ELF.h:91
@ MTCTR_R12
Definition ELF.h:96
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
Definition ELF.h:82
static Error defaultWarningHandler(const Twine &Msg)
Definition ELF.h:166
bool isSectionInSegment(const typename ELFT::Phdr &Phdr, const typename ELFT::Shdr &Sec)
Definition ELF.h:208
ELFFile< ELF32LE > ELF32LEFile
Definition ELF.h:600
bool checkSectionVMA(const typename ELFT::Phdr &Phdr, const typename ELFT::Shdr &Sec)
Definition ELF.h:189
ELFFile< ELF64BE > ELF64BEFile
Definition ELF.h:603
ELFFile< ELF32BE > ELF32BEFile
Definition ELF.h:602
std::string getSecIndexForError(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
Definition ELF.h:133
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:1466
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:864
std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
Definition ELF.h:147
ELFFile< ELF64LE > ELF64LEFile
Definition ELF.h:601
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:573
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:1669
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
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:1321
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
#define N
Expected< T > operator[](uint64_t N)
Definition ELF.h:112
DataRegion(const T *Data, const uint8_t *BufferEnd)
Definition ELF.h:109
DataRegion(ArrayRef< T > Arr)
Definition ELF.h:105
const uint8_t * BufEnd
Definition ELF.h:129
std::optional< uint64_t > Size
Definition ELF.h:128
std::conditional_t< Is64, uint64_t, uint32_t > uint
Definition ELFTypes.h:502
std::string Name
Definition ELF.h:49
uint16_t Version
Definition ELF.h:44
uint16_t Flags
Definition ELF.h:45
uint16_t Ndx
Definition ELF.h:46
uint16_t Cnt
Definition ELF.h:47
unsigned Hash
Definition ELF.h:48
std::vector< VerdAux > AuxV
Definition ELF.h:50
unsigned Offset
Definition ELF.h:43
unsigned Cnt
Definition ELF.h:63
std::string File
Definition ELF.h:65
std::vector< VernAux > AuxV
Definition ELF.h:66
unsigned Offset
Definition ELF.h:64
unsigned Version
Definition ELF.h:62
unsigned Offset
Definition ELF.h:38
std::string Name
Definition ELF.h:39
unsigned Hash
Definition ELF.h:54
unsigned Offset
Definition ELF.h:57
unsigned Flags
Definition ELF.h:55
std::string Name
Definition ELF.h:58
unsigned Other
Definition ELF.h:56
std::string Name
Definition ELF.h:70