xref: /llvm-project/llvm/include/llvm/Object/ELF.h (revision d02c1676d75a6bab1252b48da9a955fc7dc1251f)
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"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/Object/ELFTypes.h"
23 #include "llvm/Object/Error.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/DataExtractor.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 
34 namespace llvm {
35 namespace object {
36 
37 struct VerdAux {
38   unsigned Offset;
39   std::string Name;
40 };
41 
42 struct 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 
53 struct VernAux {
54   unsigned Hash;
55   unsigned Flags;
56   unsigned Other;
57   unsigned Offset;
58   std::string Name;
59 };
60 
61 struct VerNeed {
62   unsigned Version;
63   unsigned Cnt;
64   unsigned Offset;
65   std::string File;
66   std::vector<VernAux> AuxV;
67 };
68 
69 struct VersionEntry {
70   std::string Name;
71   bool IsVerDef;
72 };
73 
74 StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
75 uint32_t getELFRelativeRelocationType(uint32_t Machine);
76 StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type);
77 
78 // Subclasses of ELFFile may need this for template instantiation
79 inline std::pair<unsigned char, unsigned char>
80 getElfArchType(StringRef Object) {
81   if (Object.size() < ELF::EI_NIDENT)
82     return std::make_pair((uint8_t)ELF::ELFCLASSNONE,
83                           (uint8_t)ELF::ELFDATANONE);
84   return std::make_pair((uint8_t)Object[ELF::EI_CLASS],
85                         (uint8_t)Object[ELF::EI_DATA]);
86 }
87 
88 enum PPCInstrMasks : uint64_t {
89   PADDI_R12_NO_DISP = 0x0610000039800000,
90   ADDIS_R12_TO_R2_NO_DISP = 0x3D820000,
91   ADDI_R12_TO_R2_NO_DISP = 0x39820000,
92   ADDI_R12_TO_R12_NO_DISP = 0x398C0000,
93   PLD_R12_NO_DISP = 0x04100000E5800000,
94   MTCTR_R12 = 0x7D8903A6,
95   BCTR = 0x4E800420,
96 };
97 
98 template <class ELFT> class ELFFile;
99 
100 template <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 
110   Expected<T> operator[](uint64_t N) {
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 
130 template <class ELFT>
131 static 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 
144 template <class ELFT>
145 static 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 
154 template <class ELFT>
155 static 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 
165 static inline Error defaultWarningHandler(const Twine &Msg) {
166   return createError(Msg);
167 }
168 
169 template <class ELFT>
170 static 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.
187 template <class ELFT>
188 static 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 
206 template <class ELFT>
207 static 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.
215 template <bool Is64>
216 static Error decodeCrel(
217     ArrayRef<uint8_t> Content,
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
222   DataExtractor::Cursor Cur(0);
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 
254 template <class ELFT>
255 class ELFFile {
256 public:
257   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
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.
264   using WarningHandler = llvm::function_ref<Error(const Twine &Msg)>;
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 
271 private:
272   StringRef Buf;
273   std::vector<Elf_Shdr> FakeSections;
274   SmallString<0> FakeSectionStrings;
275 
276   ELFFile(StringRef Object);
277 
278 public:
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 
288   Expected<std::vector<VerDef>>
289   getVersionDefinitions(const Elf_Shdr &Sec) const;
290   Expected<std::vector<VerNeed>> getVersionDependencies(
291       const Elf_Shdr &Sec,
292       WarningHandler WarnHandler = &defaultWarningHandler) const;
293   Expected<StringRef> getSymbolVersionByIndex(
294       uint32_t SymbolVersionIndex, bool &IsDefault,
295       SmallVector<std::optional<VersionEntry>, 0> &VersionMap,
296       std::optional<bool> IsSymHidden) const;
297 
298   Expected<StringRef>
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 
310   Expected<uint64_t> getDynSymtabSize() const;
311 
312   StringRef getRelocationTypeName(uint32_t Type) const;
313   void getRelocationTypeName(uint32_t Type,
314                              SmallVectorImpl<char> &Result) const;
315   uint32_t getRelativeRelocationType() 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.
321   Expected<const Elf_Sym *> getRelocationSymbol(const Elf_Rel &Rel,
322                                                 const Elf_Shdr *SymTab) const;
323 
324   Expected<SmallVector<std::optional<VersionEntry>, 0>>
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 
340   Expected<Elf_Shdr_Range> sections() const;
341 
342   Expected<Elf_Dyn_Range> dynamicEntries() const;
343 
344   Expected<const uint8_t *>
345   toMappedAddr(uint64_t VAddr,
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 
368   Expected<uint64_t> getCrelHeader(ArrayRef<uint8_t> Content) const;
369   using RelsOrRelas = std::pair<std::vector<Elf_Rel>, std::vector<Elf_Rela>>;
370   Expected<RelsOrRelas> decodeCrel(ArrayRef<uint8_t> Content) const;
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.
376   Expected<Elf_Phdr_Range> program_headers() const {
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.
462   iterator_range<Elf_Note_Iterator> notes(const Elf_Phdr &Phdr,
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.
474   iterator_range<Elf_Note_Iterator> notes(const Elf_Shdr &Shdr,
475                                           Error &Err) const {
476     return make_range(notes_begin(Shdr, Err), notes_end());
477   }
478 
479   Expected<StringRef> getSectionStringTable(
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;
484   Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym,
485                                         const Elf_Shdr *SymTab,
486                                         DataRegion<Elf_Word> ShndxTable) const;
487   Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym,
488                                         Elf_Sym_Range Symtab,
489                                         DataRegion<Elf_Word> ShndxTable) const;
490   Expected<const Elf_Shdr *> getSection(uint32_t Index) const;
491 
492   Expected<const Elf_Sym *> getSymbol(const Elf_Shdr *Sec,
493                                       uint32_t Index) const;
494 
495   Expected<StringRef>
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.
512   Expected<std::vector<BBAddrMap>>
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.
520   Expected<MapVector<const Elf_Shdr *, const Elf_Shdr *>>
521   getSectionAndRelocations(
522       std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const;
523 
524   void createFakeSections();
525 };
526 
527 using ELF32LEFile = ELFFile<ELF32LE>;
528 using ELF64LEFile = ELFFile<ELF64LE>;
529 using ELF32BEFile = ELFFile<ELF32BE>;
530 using ELF64BEFile = ELFFile<ELF64BE>;
531 
532 template <class ELFT>
533 inline Expected<const typename ELFT::Shdr *>
534 getSection(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 
540 template <class ELFT>
541 inline Expected<uint32_t>
542 getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym, unsigned SymIndex,
543                             DataRegion<typename ELFT::Word> ShndxTable) {
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 
558 template <class ELFT>
559 Expected<uint32_t>
560 ELFFile<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   }
570   if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
571     return 0;
572   return Index;
573 }
574 
575 template <class ELFT>
576 Expected<const typename ELFT::Shdr *>
577 ELFFile<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 
585 template <class ELFT>
586 Expected<const typename ELFT::Shdr *>
587 ELFFile<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 
598 template <class ELFT>
599 Expected<const typename ELFT::Sym *>
600 ELFFile<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 
613 template <class ELFT>
614 template <typename T>
615 Expected<ArrayRef<T>>
616 ELFFile<ELFT>::getSectionContentsAsArray(const Elf_Shdr &Sec) const {
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 
650 template <class ELFT>
651 Expected<ArrayRef<uint8_t>>
652 ELFFile<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 
670 template <class ELFT>
671 Expected<ArrayRef<uint8_t>>
672 ELFFile<ELFT>::getSectionContents(const Elf_Shdr &Sec) const {
673   return getSectionContentsAsArray<uint8_t>(Sec);
674 }
675 
676 template <class ELFT>
677 StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
678   return getELFRelocationTypeName(getHeader().e_machine, Type);
679 }
680 
681 template <class ELFT>
682 void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
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 
712 template <class ELFT>
713 uint32_t ELFFile<ELFT>::getRelativeRelocationType() const {
714   return getELFRelativeRelocationType(getHeader().e_machine);
715 }
716 
717 template <class ELFT>
718 Expected<SmallVector<std::optional<VersionEntry>, 0>>
719 ELFFile<ELFT>::loadVersionMap(const Elf_Shdr *VerNeedSec,
720                               const Elf_Shdr *VerDefSec) const {
721   SmallVector<std::optional<VersionEntry>, 0> VersionMap;
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 
754 template <class ELFT>
755 Expected<const typename ELFT::Sym *>
756 ELFFile<ELFT>::getRelocationSymbol(const Elf_Rel &Rel,
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 
764 template <class ELFT>
765 Expected<StringRef>
766 ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections,
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.
795 template <class ELFT>
796 static Expected<uint64_t>
797 getDynSymtabSizeFromGnuHash(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(
815         object_error::parse_failed,
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.
824 template <class ELFT>
825 Expected<uint64_t> ELFFile<ELFT>::getDynSymtabSize() const {
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) {
833         return createStringError(object_error::parse_failed,
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 
885 template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
886 
887 template <class ELFT>
888 Expected<ELFFile<ELFT>> ELFFile<ELFT>::create(StringRef Object) {
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).
899 template <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 
924 template <class ELFT>
925 Expected<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const {
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 
976 template <class ELFT>
977 template <typename T>
978 Expected<const T *> ELFFile<ELFT>::getEntry(uint32_t Section,
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 
986 template <class ELFT>
987 template <typename T>
988 Expected<const T *> ELFFile<ELFT>::getEntry(const Elf_Shdr &Section,
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 
1004 template <typename ELFT>
1005 Expected<StringRef> ELFFile<ELFT>::getSymbolVersionByIndex(
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 
1032 template <class ELFT>
1033 Expected<std::vector<VerDef>>
1034 ELFFile<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 
1119 template <class ELFT>
1120 Expected<std::vector<VerNeed>>
1121 ELFFile<ELFT>::getVersionDependencies(const Elf_Shdr &Sec,
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 
1207 template <class ELFT>
1208 Expected<const typename ELFT::Shdr *>
1209 ELFFile<ELFT>::getSection(uint32_t Index) const {
1210   auto TableOrErr = sections();
1211   if (!TableOrErr)
1212     return TableOrErr.takeError();
1213   return object::getSection<ELFT>(*TableOrErr, Index);
1214 }
1215 
1216 template <class ELFT>
1217 Expected<StringRef>
1218 ELFFile<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 " +
1224                               object::getELFSectionTypeName(
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 
1242 template <class ELFT>
1243 Expected<ArrayRef<typename ELFT::Word>>
1244 ELFFile<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 
1251 template <class ELFT>
1252 Expected<ArrayRef<typename ELFT::Word>>
1253 ELFFile<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 
1280 template <class ELFT>
1281 Expected<StringRef>
1282 ELFFile<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 
1289 template <class ELFT>
1290 Expected<StringRef>
1291 ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec,
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 
1304 template <class ELFT>
1305 Expected<StringRef>
1306 ELFFile<ELFT>::getLinkAsStrtab(const typename ELFT::Shdr &Sec) const {
1307   Expected<const typename ELFT::Shdr *> StrTabSecOrErr =
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 
1321 template <class ELFT>
1322 Expected<StringRef>
1323 ELFFile<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 
1334 template <class ELFT>
1335 Expected<StringRef> ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section,
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" +
1343                        Twine::utohexstr(Offset) +
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
1352 inline 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
1364 inline uint32_t hashGnu(StringRef Name) {
1365   uint32_t H = 5381;
1366   for (uint8_t C : Name)
1367     H = (H << 5) + H + C;
1368   return H;
1369 }
1370 
1371 extern template class LLVM_TEMPLATE_ABI llvm::object::ELFFile<ELF32LE>;
1372 extern template class LLVM_TEMPLATE_ABI llvm::object::ELFFile<ELF32BE>;
1373 extern template class LLVM_TEMPLATE_ABI llvm::object::ELFFile<ELF64LE>;
1374 extern 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
1380