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