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