xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Object/ELF.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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/DataExtractor.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 
33 namespace llvm {
34 namespace object {
35 
36 struct VerdAux {
37   unsigned Offset;
38   std::string Name;
39 };
40 
41 struct 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 
52 struct VernAux {
53   unsigned Hash;
54   unsigned Flags;
55   unsigned Other;
56   unsigned Offset;
57   std::string Name;
58 };
59 
60 struct VerNeed {
61   unsigned Version;
62   unsigned Cnt;
63   unsigned Offset;
64   std::string File;
65   std::vector<VernAux> AuxV;
66 };
67 
68 struct VersionEntry {
69   std::string Name;
70   bool IsVerDef;
71 };
72 
73 StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
74 uint32_t getELFRelativeRelocationType(uint32_t Machine);
75 StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type);
76 
77 // Subclasses of ELFFile may need this for template instantiation
78 inline std::pair<unsigned char, unsigned char>
getElfArchType(StringRef Object)79 getElfArchType(StringRef Object) {
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 
87 enum PPCInstrMasks : uint64_t {
88   PADDI_R12_NO_DISP = 0x0610000039800000,
89   ADDIS_R12_TO_R2_NO_DISP = 0x3D820000,
90   ADDI_R12_TO_R2_NO_DISP = 0x39820000,
91   ADDI_R12_TO_R12_NO_DISP = 0x398C0000,
92   PLD_R12_NO_DISP = 0x04100000E5800000,
93   MTCTR_R12 = 0x7D8903A6,
94   BCTR = 0x4E800420,
95 };
96 
97 template <class ELFT> class ELFFile;
98 
99 template <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.
DataRegionDataRegion102   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.
DataRegionDataRegion106   DataRegion(const T *Data, const uint8_t *BufferEnd)
107       : First(Data), BufEnd(BufferEnd) {}
108 
109   Expected<T> operator[](uint64_t N) {
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 
129 template <class ELFT>
getSecIndexForError(const ELFFile<ELFT> & Obj,const typename ELFT::Shdr & Sec)130 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 
143 template <class ELFT>
describe(const ELFFile<ELFT> & Obj,const typename ELFT::Shdr & Sec)144 static 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 
153 template <class ELFT>
getPhdrIndexForError(const ELFFile<ELFT> & Obj,const typename ELFT::Phdr & Phdr)154 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 
defaultWarningHandler(const Twine & Msg)164 static inline Error defaultWarningHandler(const Twine &Msg) {
165   return createError(Msg);
166 }
167 
168 template <class ELFT>
checkSectionOffsets(const typename ELFT::Phdr & Phdr,const typename ELFT::Shdr & Sec)169 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.
186 template <class ELFT>
checkSectionVMA(const typename ELFT::Phdr & Phdr,const typename ELFT::Shdr & Sec)187 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 
205 template <class ELFT>
isSectionInSegment(const typename ELFT::Phdr & Phdr,const typename ELFT::Shdr & Sec)206 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.
214 template <bool Is64>
decodeCrel(ArrayRef<uint8_t> Content,function_ref<void (uint64_t,bool)> HdrHandler,function_ref<void (Elf_Crel_Impl<Is64>)> EntryHandler)215 Error decodeCrel(
216     ArrayRef<uint8_t> Content,
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
221   DataExtractor::Cursor Cur(0);
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 
253 template <class ELFT>
254 class ELFFile {
255 public:
256   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
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.
263   using WarningHandler = llvm::function_ref<Error(const Twine &Msg)>;
264 
base()265   const uint8_t *base() const { return Buf.bytes_begin(); }
end()266   const uint8_t *end() const { return base() + getBufSize(); }
267 
getBufSize()268   size_t getBufSize() const { return Buf.size(); }
269 
270 private:
271   StringRef Buf;
272   std::vector<Elf_Shdr> FakeSections;
273   SmallString<0> FakeSectionStrings;
274 
275   ELFFile(StringRef Object);
276 
277 public:
getHeader()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 
287   Expected<std::vector<VerDef>>
288   getVersionDefinitions(const Elf_Shdr &Sec) const;
289   Expected<std::vector<VerNeed>> getVersionDependencies(
290       const Elf_Shdr &Sec,
291       WarningHandler WarnHandler = &defaultWarningHandler) const;
292   Expected<StringRef> getSymbolVersionByIndex(
293       uint32_t SymbolVersionIndex, bool &IsDefault,
294       SmallVector<std::optional<VersionEntry>, 0> &VersionMap,
295       std::optional<bool> IsSymHidden) const;
296 
297   Expected<StringRef>
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 
309   Expected<uint64_t> getDynSymtabSize() const;
310 
311   StringRef getRelocationTypeName(uint32_t Type) const;
312   void getRelocationTypeName(uint32_t Type,
313                              SmallVectorImpl<char> &Result) const;
314   uint32_t getRelativeRelocationType() 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.
320   Expected<const Elf_Sym *> getRelocationSymbol(const Elf_Rel &Rel,
321                                                 const Elf_Shdr *SymTab) const;
322 
323   Expected<SmallVector<std::optional<VersionEntry>, 0>>
324   loadVersionMap(const Elf_Shdr *VerNeedSec, const Elf_Shdr *VerDefSec) const;
325 
326   static Expected<ELFFile> create(StringRef Object);
327 
isLE()328   bool isLE() const {
329     return getHeader().getDataEncoding() == ELF::ELFDATA2LSB;
330   }
331 
isMipsELF64()332   bool isMipsELF64() const {
333     return getHeader().e_machine == ELF::EM_MIPS &&
334            getHeader().getFileClass() == ELF::ELFCLASS64;
335   }
336 
isMips64EL()337   bool isMips64EL() const { return isMipsELF64() && isLE(); }
338 
339   Expected<Elf_Shdr_Range> sections() const;
340 
341   Expected<Elf_Dyn_Range> dynamicEntries() const;
342 
343   Expected<const uint8_t *>
344   toMappedAddr(uint64_t VAddr,
345                WarningHandler WarnHandler = &defaultWarningHandler) const;
346 
symbols(const Elf_Shdr * Sec)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 
relas(const Elf_Shdr & Sec)353   Expected<Elf_Rela_Range> relas(const Elf_Shdr &Sec) const {
354     return getSectionContentsAsArray<Elf_Rela>(Sec);
355   }
356 
rels(const Elf_Shdr & Sec)357   Expected<Elf_Rel_Range> rels(const Elf_Shdr &Sec) const {
358     return getSectionContentsAsArray<Elf_Rel>(Sec);
359   }
360 
relrs(const Elf_Shdr & Sec)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 
367   Expected<uint64_t> getCrelHeader(ArrayRef<uint8_t> Content) const;
368   using RelsOrRelas = std::pair<std::vector<Elf_Rel>, std::vector<Elf_Rela>>;
369   Expected<RelsOrRelas> decodeCrel(ArrayRef<uint8_t> Content) const;
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.
program_headers()375   Expected<Elf_Phdr_Range> program_headers() const {
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.
notes_begin(const Elf_Phdr & Phdr,Error & Err)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.
notes_begin(const Elf_Shdr & Shdr,Error & Err)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.
notes_end()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.
notes(const Elf_Phdr & Phdr,Error & Err)461   iterator_range<Elf_Note_Iterator> notes(const Elf_Phdr &Phdr,
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.
notes(const Elf_Shdr & Shdr,Error & Err)473   iterator_range<Elf_Note_Iterator> notes(const Elf_Shdr &Shdr,
474                                           Error &Err) const {
475     return make_range(notes_begin(Shdr, Err), notes_end());
476   }
477 
478   Expected<StringRef> getSectionStringTable(
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;
483   Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym,
484                                         const Elf_Shdr *SymTab,
485                                         DataRegion<Elf_Word> ShndxTable) const;
486   Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym,
487                                         Elf_Sym_Range Symtab,
488                                         DataRegion<Elf_Word> ShndxTable) const;
489   Expected<const Elf_Shdr *> getSection(uint32_t Index) const;
490 
491   Expected<const Elf_Sym *> getSymbol(const Elf_Shdr *Sec,
492                                       uint32_t Index) const;
493 
494   Expected<StringRef>
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.
511   Expected<std::vector<BBAddrMap>>
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.
519   Expected<MapVector<const Elf_Shdr *, const Elf_Shdr *>>
520   getSectionAndRelocations(
521       std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const;
522 
523   void createFakeSections();
524 };
525 
526 using ELF32LEFile = ELFFile<ELF32LE>;
527 using ELF64LEFile = ELFFile<ELF64LE>;
528 using ELF32BEFile = ELFFile<ELF32BE>;
529 using ELF64BEFile = ELFFile<ELF64BE>;
530 
531 template <class ELFT>
532 inline Expected<const typename ELFT::Shdr *>
getSection(typename ELFT::ShdrRange Sections,uint32_t Index)533 getSection(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 
539 template <class ELFT>
540 inline Expected<uint32_t>
getExtendedSymbolTableIndex(const typename ELFT::Sym & Sym,unsigned SymIndex,DataRegion<typename ELFT::Word> ShndxTable)541 getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym, unsigned SymIndex,
542                             DataRegion<typename ELFT::Word> ShndxTable) {
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 
557 template <class ELFT>
558 Expected<uint32_t>
getSectionIndex(const Elf_Sym & Sym,Elf_Sym_Range Syms,DataRegion<Elf_Word> ShndxTable)559 ELFFile<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   }
569   if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
570     return 0;
571   return Index;
572 }
573 
574 template <class ELFT>
575 Expected<const typename ELFT::Shdr *>
getSection(const Elf_Sym & Sym,const Elf_Shdr * SymTab,DataRegion<Elf_Word> ShndxTable)576 ELFFile<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 
584 template <class ELFT>
585 Expected<const typename ELFT::Shdr *>
getSection(const Elf_Sym & Sym,Elf_Sym_Range Symbols,DataRegion<Elf_Word> ShndxTable)586 ELFFile<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 
597 template <class ELFT>
598 Expected<const typename ELFT::Sym *>
getSymbol(const Elf_Shdr * Sec,uint32_t Index)599 ELFFile<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 
612 template <class ELFT>
613 template <typename T>
614 Expected<ArrayRef<T>>
getSectionContentsAsArray(const Elf_Shdr & Sec)615 ELFFile<ELFT>::getSectionContentsAsArray(const Elf_Shdr &Sec) const {
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 
649 template <class ELFT>
650 Expected<ArrayRef<uint8_t>>
getSegmentContents(const Elf_Phdr & Phdr)651 ELFFile<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 
669 template <class ELFT>
670 Expected<ArrayRef<uint8_t>>
getSectionContents(const Elf_Shdr & Sec)671 ELFFile<ELFT>::getSectionContents(const Elf_Shdr &Sec) const {
672   return getSectionContentsAsArray<uint8_t>(Sec);
673 }
674 
675 template <class ELFT>
getRelocationTypeName(uint32_t Type)676 StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
677   return getELFRelocationTypeName(getHeader().e_machine, Type);
678 }
679 
680 template <class ELFT>
getRelocationTypeName(uint32_t Type,SmallVectorImpl<char> & Result)681 void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
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 
711 template <class ELFT>
getRelativeRelocationType()712 uint32_t ELFFile<ELFT>::getRelativeRelocationType() const {
713   return getELFRelativeRelocationType(getHeader().e_machine);
714 }
715 
716 template <class ELFT>
717 Expected<SmallVector<std::optional<VersionEntry>, 0>>
loadVersionMap(const Elf_Shdr * VerNeedSec,const Elf_Shdr * VerDefSec)718 ELFFile<ELFT>::loadVersionMap(const Elf_Shdr *VerNeedSec,
719                               const Elf_Shdr *VerDefSec) const {
720   SmallVector<std::optional<VersionEntry>, 0> VersionMap;
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 
753 template <class ELFT>
754 Expected<const typename ELFT::Sym *>
getRelocationSymbol(const Elf_Rel & Rel,const Elf_Shdr * SymTab)755 ELFFile<ELFT>::getRelocationSymbol(const Elf_Rel &Rel,
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 
763 template <class ELFT>
764 Expected<StringRef>
getSectionStringTable(Elf_Shdr_Range Sections,WarningHandler WarnHandler)765 ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections,
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.
794 template <class ELFT>
795 static Expected<uint64_t>
getDynSymtabSizeFromGnuHash(const typename ELFT::GnuHash & Table,const void * BufEnd)796 getDynSymtabSizeFromGnuHash(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(
814         object_error::parse_failed,
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.
823 template <class ELFT>
getDynSymtabSize()824 Expected<uint64_t> ELFFile<ELFT>::getDynSymtabSize() const {
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) {
832         return createStringError(object_error::parse_failed,
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 
ELFFile(StringRef Object)884 template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
885 
886 template <class ELFT>
create(StringRef Object)887 Expected<ELFFile<ELFT>> ELFFile<ELFT>::create(StringRef Object) {
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).
createFakeSections()898 template <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 
923 template <class ELFT>
sections()924 Expected<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const {
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 
975 template <class ELFT>
976 template <typename T>
getEntry(uint32_t Section,uint32_t Entry)977 Expected<const T *> ELFFile<ELFT>::getEntry(uint32_t Section,
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 
985 template <class ELFT>
986 template <typename T>
getEntry(const Elf_Shdr & Section,uint32_t Entry)987 Expected<const T *> ELFFile<ELFT>::getEntry(const Elf_Shdr &Section,
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 
1003 template <typename ELFT>
getSymbolVersionByIndex(uint32_t SymbolVersionIndex,bool & IsDefault,SmallVector<std::optional<VersionEntry>,0> & VersionMap,std::optional<bool> IsSymHidden)1004 Expected<StringRef> ELFFile<ELFT>::getSymbolVersionByIndex(
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 
1031 template <class ELFT>
1032 Expected<std::vector<VerDef>>
getVersionDefinitions(const Elf_Shdr & Sec)1033 ELFFile<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 
1118 template <class ELFT>
1119 Expected<std::vector<VerNeed>>
getVersionDependencies(const Elf_Shdr & Sec,WarningHandler WarnHandler)1120 ELFFile<ELFT>::getVersionDependencies(const Elf_Shdr &Sec,
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 
1206 template <class ELFT>
1207 Expected<const typename ELFT::Shdr *>
getSection(uint32_t Index)1208 ELFFile<ELFT>::getSection(uint32_t Index) const {
1209   auto TableOrErr = sections();
1210   if (!TableOrErr)
1211     return TableOrErr.takeError();
1212   return object::getSection<ELFT>(*TableOrErr, Index);
1213 }
1214 
1215 template <class ELFT>
1216 Expected<StringRef>
getStringTable(const Elf_Shdr & Section,WarningHandler WarnHandler)1217 ELFFile<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 " +
1223                               object::getELFSectionTypeName(
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 
1241 template <class ELFT>
1242 Expected<ArrayRef<typename ELFT::Word>>
getSHNDXTable(const Elf_Shdr & Section)1243 ELFFile<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 
1250 template <class ELFT>
1251 Expected<ArrayRef<typename ELFT::Word>>
getSHNDXTable(const Elf_Shdr & Section,Elf_Shdr_Range Sections)1252 ELFFile<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 
1279 template <class ELFT>
1280 Expected<StringRef>
getStringTableForSymtab(const Elf_Shdr & Sec)1281 ELFFile<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 
1288 template <class ELFT>
1289 Expected<StringRef>
getStringTableForSymtab(const Elf_Shdr & Sec,Elf_Shdr_Range Sections)1290 ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec,
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 
1303 template <class ELFT>
1304 Expected<StringRef>
getLinkAsStrtab(const typename ELFT::Shdr & Sec)1305 ELFFile<ELFT>::getLinkAsStrtab(const typename ELFT::Shdr &Sec) const {
1306   Expected<const typename ELFT::Shdr *> StrTabSecOrErr =
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 
1320 template <class ELFT>
1321 Expected<StringRef>
getSectionName(const Elf_Shdr & Section,WarningHandler WarnHandler)1322 ELFFile<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 
1333 template <class ELFT>
getSectionName(const Elf_Shdr & Section,StringRef DotShstrtab)1334 Expected<StringRef> ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section,
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" +
1342                        Twine::utohexstr(Offset) +
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
hashSysV(StringRef SymbolName)1351 inline 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
hashGnu(StringRef Name)1363 inline uint32_t hashGnu(StringRef Name) {
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
1374