xref: /freebsd/contrib/llvm-project/llvm/lib/Object/ELF.cpp (revision 1d479bf6b4741fdc24490ad7179bbef0a78af288)
1  //===- ELF.cpp - ELF object file implementation ---------------------------===//
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  #include "llvm/Object/ELF.h"
10  #include "llvm/ADT/StringExtras.h"
11  #include "llvm/BinaryFormat/ELF.h"
12  #include "llvm/Support/DataExtractor.h"
13  
14  using namespace llvm;
15  using namespace object;
16  
17  #define STRINGIFY_ENUM_CASE(ns, name)                                          \
18    case ns::name:                                                               \
19      return #name;
20  
21  #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
22  
23  StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
24                                                   uint32_t Type) {
25    switch (Machine) {
26    case ELF::EM_68K:
27      switch (Type) {
28  #include "llvm/BinaryFormat/ELFRelocs/M68k.def"
29      default:
30        break;
31      }
32      break;
33    case ELF::EM_X86_64:
34      switch (Type) {
35  #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
36      default:
37        break;
38      }
39      break;
40    case ELF::EM_386:
41    case ELF::EM_IAMCU:
42      switch (Type) {
43  #include "llvm/BinaryFormat/ELFRelocs/i386.def"
44      default:
45        break;
46      }
47      break;
48    case ELF::EM_MIPS:
49      switch (Type) {
50  #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
51      default:
52        break;
53      }
54      break;
55    case ELF::EM_AARCH64:
56      switch (Type) {
57  #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
58      default:
59        break;
60      }
61      break;
62    case ELF::EM_ARM:
63      switch (Type) {
64  #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
65      default:
66        break;
67      }
68      break;
69    case ELF::EM_ARC_COMPACT:
70    case ELF::EM_ARC_COMPACT2:
71      switch (Type) {
72  #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
73      default:
74        break;
75      }
76      break;
77    case ELF::EM_AVR:
78      switch (Type) {
79  #include "llvm/BinaryFormat/ELFRelocs/AVR.def"
80      default:
81        break;
82      }
83      break;
84    case ELF::EM_HEXAGON:
85      switch (Type) {
86  #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
87      default:
88        break;
89      }
90      break;
91    case ELF::EM_LANAI:
92      switch (Type) {
93  #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
94      default:
95        break;
96      }
97      break;
98    case ELF::EM_PPC:
99      switch (Type) {
100  #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
101      default:
102        break;
103      }
104      break;
105    case ELF::EM_PPC64:
106      switch (Type) {
107  #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
108      default:
109        break;
110      }
111      break;
112    case ELF::EM_RISCV:
113      switch (Type) {
114  #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
115      default:
116        break;
117      }
118      break;
119    case ELF::EM_S390:
120      switch (Type) {
121  #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
122      default:
123        break;
124      }
125      break;
126    case ELF::EM_SPARC:
127    case ELF::EM_SPARC32PLUS:
128    case ELF::EM_SPARCV9:
129      switch (Type) {
130  #include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
131      default:
132        break;
133      }
134      break;
135    case ELF::EM_AMDGPU:
136      switch (Type) {
137  #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
138      default:
139        break;
140      }
141      break;
142    case ELF::EM_BPF:
143      switch (Type) {
144  #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
145      default:
146        break;
147      }
148      break;
149    case ELF::EM_MSP430:
150      switch (Type) {
151  #include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
152      default:
153        break;
154      }
155      break;
156    case ELF::EM_VE:
157      switch (Type) {
158  #include "llvm/BinaryFormat/ELFRelocs/VE.def"
159      default:
160        break;
161      }
162      break;
163    case ELF::EM_CSKY:
164      switch (Type) {
165  #include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
166      default:
167        break;
168      }
169      break;
170    case ELF::EM_LOONGARCH:
171      switch (Type) {
172  #include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
173      default:
174        break;
175      }
176      break;
177    case ELF::EM_XTENSA:
178      switch (Type) {
179  #include "llvm/BinaryFormat/ELFRelocs/Xtensa.def"
180      default:
181        break;
182      }
183      break;
184    default:
185      break;
186    }
187    return "Unknown";
188  }
189  
190  #undef ELF_RELOC
191  
192  uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
193    switch (Machine) {
194    case ELF::EM_X86_64:
195      return ELF::R_X86_64_RELATIVE;
196    case ELF::EM_386:
197    case ELF::EM_IAMCU:
198      return ELF::R_386_RELATIVE;
199    case ELF::EM_MIPS:
200      break;
201    case ELF::EM_AARCH64:
202      return ELF::R_AARCH64_RELATIVE;
203    case ELF::EM_ARM:
204      return ELF::R_ARM_RELATIVE;
205    case ELF::EM_ARC_COMPACT:
206    case ELF::EM_ARC_COMPACT2:
207      return ELF::R_ARC_RELATIVE;
208    case ELF::EM_AVR:
209      break;
210    case ELF::EM_HEXAGON:
211      return ELF::R_HEX_RELATIVE;
212    case ELF::EM_LANAI:
213      break;
214    case ELF::EM_PPC:
215      break;
216    case ELF::EM_PPC64:
217      return ELF::R_PPC64_RELATIVE;
218    case ELF::EM_RISCV:
219      return ELF::R_RISCV_RELATIVE;
220    case ELF::EM_S390:
221      return ELF::R_390_RELATIVE;
222    case ELF::EM_SPARC:
223    case ELF::EM_SPARC32PLUS:
224    case ELF::EM_SPARCV9:
225      return ELF::R_SPARC_RELATIVE;
226    case ELF::EM_CSKY:
227      return ELF::R_CKCORE_RELATIVE;
228    case ELF::EM_VE:
229      return ELF::R_VE_RELATIVE;
230    case ELF::EM_AMDGPU:
231      break;
232    case ELF::EM_BPF:
233      break;
234    case ELF::EM_LOONGARCH:
235      return ELF::R_LARCH_RELATIVE;
236    default:
237      break;
238    }
239    return 0;
240  }
241  
242  StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
243    switch (Machine) {
244    case ELF::EM_ARM:
245      switch (Type) {
246        STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
247        STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
248        STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
249        STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
250        STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
251      }
252      break;
253    case ELF::EM_HEXAGON:
254      switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
255      break;
256    case ELF::EM_X86_64:
257      switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
258      break;
259    case ELF::EM_MIPS:
260    case ELF::EM_MIPS_RS3_LE:
261      switch (Type) {
262        STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
263        STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
264        STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
265        STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
266      }
267      break;
268    case ELF::EM_MSP430:
269      switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_MSP430_ATTRIBUTES); }
270      break;
271    case ELF::EM_RISCV:
272      switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
273      break;
274    case ELF::EM_AARCH64:
275      switch (Type) {
276        STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC);
277        STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_STATIC);
278      }
279    default:
280      break;
281    }
282  
283    switch (Type) {
284      STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
285      STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
286      STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
287      STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
288      STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
289      STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
290      STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
291      STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
292      STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
293      STRINGIFY_ENUM_CASE(ELF, SHT_REL);
294      STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
295      STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
296      STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
297      STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
298      STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
299      STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
300      STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
301      STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
302      STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
303      STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
304      STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
305      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
306      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
307      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
308      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
309      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
310      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
311      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
312      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
313      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP_V0);
314      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP);
315      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_OFFLOADING);
316      STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LTO);
317      STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
318      STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
319      STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
320      STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
321      STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
322    default:
323      return "Unknown";
324    }
325  }
326  
327  template <class ELFT>
328  std::vector<typename ELFT::Rel>
329  ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
330    // This function decodes the contents of an SHT_RELR packed relocation
331    // section.
332    //
333    // Proposal for adding SHT_RELR sections to generic-abi is here:
334    //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
335    //
336    // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
337    // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
338    //
339    // i.e. start with an address, followed by any number of bitmaps. The address
340    // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
341    // relocations each, at subsequent offsets following the last address entry.
342    //
343    // The bitmap entries must have 1 in the least significant bit. The assumption
344    // here is that an address cannot have 1 in lsb. Odd addresses are not
345    // supported.
346    //
347    // Excluding the least significant bit in the bitmap, each non-zero bit in
348    // the bitmap represents a relocation to be applied to a corresponding machine
349    // word that follows the base address word. The second least significant bit
350    // represents the machine word immediately following the initial address, and
351    // each bit that follows represents the next word, in linear order. As such,
352    // a single bitmap can encode up to 31 relocations in a 32-bit object, and
353    // 63 relocations in a 64-bit object.
354    //
355    // This encoding has a couple of interesting properties:
356    // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
357    //    even means address, odd means bitmap.
358    // 2. Just a simple list of addresses is a valid encoding.
359  
360    Elf_Rel Rel;
361    Rel.r_info = 0;
362    Rel.setType(getRelativeRelocationType(), false);
363    std::vector<Elf_Rel> Relocs;
364  
365    // Word type: uint32_t for Elf32, and uint64_t for Elf64.
366    using Addr = typename ELFT::uint;
367  
368    Addr Base = 0;
369    for (Elf_Relr R : relrs) {
370      typename ELFT::uint Entry = R;
371      if ((Entry & 1) == 0) {
372        // Even entry: encodes the offset for next relocation.
373        Rel.r_offset = Entry;
374        Relocs.push_back(Rel);
375        // Set base offset for subsequent bitmap entries.
376        Base = Entry + sizeof(Addr);
377      } else {
378        // Odd entry: encodes bitmap for relocations starting at base.
379        for (Addr Offset = Base; (Entry >>= 1) != 0; Offset += sizeof(Addr))
380          if ((Entry & 1) != 0) {
381            Rel.r_offset = Offset;
382            Relocs.push_back(Rel);
383          }
384        Base += (CHAR_BIT * sizeof(Entry) - 1) * sizeof(Addr);
385      }
386    }
387  
388    return Relocs;
389  }
390  
391  template <class ELFT>
392  Expected<std::vector<typename ELFT::Rela>>
393  ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
394    // This function reads relocations in Android's packed relocation format,
395    // which is based on SLEB128 and delta encoding.
396    Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
397    if (!ContentsOrErr)
398      return ContentsOrErr.takeError();
399    ArrayRef<uint8_t> Content = *ContentsOrErr;
400    if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' ||
401        Content[2] != 'S' || Content[3] != '2')
402      return createError("invalid packed relocation header");
403    DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
404    DataExtractor::Cursor Cur(/*Offset=*/4);
405  
406    uint64_t NumRelocs = Data.getSLEB128(Cur);
407    uint64_t Offset = Data.getSLEB128(Cur);
408    uint64_t Addend = 0;
409  
410    if (!Cur)
411      return std::move(Cur.takeError());
412  
413    std::vector<Elf_Rela> Relocs;
414    Relocs.reserve(NumRelocs);
415    while (NumRelocs) {
416      uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
417      if (!Cur)
418        return std::move(Cur.takeError());
419      if (NumRelocsInGroup > NumRelocs)
420        return createError("relocation group unexpectedly large");
421      NumRelocs -= NumRelocsInGroup;
422  
423      uint64_t GroupFlags = Data.getSLEB128(Cur);
424      bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
425      bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
426      bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
427      bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
428  
429      uint64_t GroupOffsetDelta;
430      if (GroupedByOffsetDelta)
431        GroupOffsetDelta = Data.getSLEB128(Cur);
432  
433      uint64_t GroupRInfo;
434      if (GroupedByInfo)
435        GroupRInfo = Data.getSLEB128(Cur);
436  
437      if (GroupedByAddend && GroupHasAddend)
438        Addend += Data.getSLEB128(Cur);
439  
440      if (!GroupHasAddend)
441        Addend = 0;
442  
443      for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) {
444        Elf_Rela R;
445        Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
446        R.r_offset = Offset;
447        R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
448        if (GroupHasAddend && !GroupedByAddend)
449          Addend += Data.getSLEB128(Cur);
450        R.r_addend = Addend;
451        Relocs.push_back(R);
452      }
453      if (!Cur)
454        return std::move(Cur.takeError());
455    }
456  
457    return Relocs;
458  }
459  
460  template <class ELFT>
461  std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
462                                                   uint64_t Type) const {
463  #define DYNAMIC_STRINGIFY_ENUM(tag, value)                                     \
464    case value:                                                                  \
465      return #tag;
466  
467  #define DYNAMIC_TAG(n, v)
468    switch (Arch) {
469    case ELF::EM_AARCH64:
470      switch (Type) {
471  #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
472  #include "llvm/BinaryFormat/DynamicTags.def"
473  #undef AARCH64_DYNAMIC_TAG
474      }
475      break;
476  
477    case ELF::EM_HEXAGON:
478      switch (Type) {
479  #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
480  #include "llvm/BinaryFormat/DynamicTags.def"
481  #undef HEXAGON_DYNAMIC_TAG
482      }
483      break;
484  
485    case ELF::EM_MIPS:
486      switch (Type) {
487  #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
488  #include "llvm/BinaryFormat/DynamicTags.def"
489  #undef MIPS_DYNAMIC_TAG
490      }
491      break;
492  
493    case ELF::EM_PPC:
494      switch (Type) {
495  #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
496  #include "llvm/BinaryFormat/DynamicTags.def"
497  #undef PPC_DYNAMIC_TAG
498      }
499      break;
500  
501    case ELF::EM_PPC64:
502      switch (Type) {
503  #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
504  #include "llvm/BinaryFormat/DynamicTags.def"
505  #undef PPC64_DYNAMIC_TAG
506      }
507      break;
508  
509    case ELF::EM_RISCV:
510      switch (Type) {
511  #define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
512  #include "llvm/BinaryFormat/DynamicTags.def"
513  #undef RISCV_DYNAMIC_TAG
514      }
515      break;
516    }
517  #undef DYNAMIC_TAG
518    switch (Type) {
519  // Now handle all dynamic tags except the architecture specific ones
520  #define AARCH64_DYNAMIC_TAG(name, value)
521  #define MIPS_DYNAMIC_TAG(name, value)
522  #define HEXAGON_DYNAMIC_TAG(name, value)
523  #define PPC_DYNAMIC_TAG(name, value)
524  #define PPC64_DYNAMIC_TAG(name, value)
525  #define RISCV_DYNAMIC_TAG(name, value)
526  // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
527  #define DYNAMIC_TAG_MARKER(name, value)
528  #define DYNAMIC_TAG(name, value) case value: return #name;
529  #include "llvm/BinaryFormat/DynamicTags.def"
530  #undef DYNAMIC_TAG
531  #undef AARCH64_DYNAMIC_TAG
532  #undef MIPS_DYNAMIC_TAG
533  #undef HEXAGON_DYNAMIC_TAG
534  #undef PPC_DYNAMIC_TAG
535  #undef PPC64_DYNAMIC_TAG
536  #undef RISCV_DYNAMIC_TAG
537  #undef DYNAMIC_TAG_MARKER
538  #undef DYNAMIC_STRINGIFY_ENUM
539    default:
540      return "<unknown:>0x" + utohexstr(Type, true);
541    }
542  }
543  
544  template <class ELFT>
545  std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
546    return getDynamicTagAsString(getHeader().e_machine, Type);
547  }
548  
549  template <class ELFT>
550  Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
551    ArrayRef<Elf_Dyn> Dyn;
552  
553    auto ProgramHeadersOrError = program_headers();
554    if (!ProgramHeadersOrError)
555      return ProgramHeadersOrError.takeError();
556  
557    for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
558      if (Phdr.p_type == ELF::PT_DYNAMIC) {
559        Dyn = ArrayRef(reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
560                       Phdr.p_filesz / sizeof(Elf_Dyn));
561        break;
562      }
563    }
564  
565    // If we can't find the dynamic section in the program headers, we just fall
566    // back on the sections.
567    if (Dyn.empty()) {
568      auto SectionsOrError = sections();
569      if (!SectionsOrError)
570        return SectionsOrError.takeError();
571  
572      for (const Elf_Shdr &Sec : *SectionsOrError) {
573        if (Sec.sh_type == ELF::SHT_DYNAMIC) {
574          Expected<ArrayRef<Elf_Dyn>> DynOrError =
575              getSectionContentsAsArray<Elf_Dyn>(Sec);
576          if (!DynOrError)
577            return DynOrError.takeError();
578          Dyn = *DynOrError;
579          break;
580        }
581      }
582  
583      if (!Dyn.data())
584        return ArrayRef<Elf_Dyn>();
585    }
586  
587    if (Dyn.empty())
588      return createError("invalid empty dynamic section");
589  
590    if (Dyn.back().d_tag != ELF::DT_NULL)
591      return createError("dynamic sections must be DT_NULL terminated");
592  
593    return Dyn;
594  }
595  
596  template <class ELFT>
597  Expected<const uint8_t *>
598  ELFFile<ELFT>::toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler) const {
599    auto ProgramHeadersOrError = program_headers();
600    if (!ProgramHeadersOrError)
601      return ProgramHeadersOrError.takeError();
602  
603    llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
604  
605    for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
606      if (Phdr.p_type == ELF::PT_LOAD)
607        LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
608  
609    auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A,
610                       const Elf_Phdr_Impl<ELFT> *B) {
611      return A->p_vaddr < B->p_vaddr;
612    };
613    if (!llvm::is_sorted(LoadSegments, SortPred)) {
614      if (Error E =
615              WarnHandler("loadable segments are unsorted by virtual address"))
616        return std::move(E);
617      llvm::stable_sort(LoadSegments, SortPred);
618    }
619  
620    const Elf_Phdr *const *I = llvm::upper_bound(
621        LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
622          return VAddr < Phdr->p_vaddr;
623        });
624  
625    if (I == LoadSegments.begin())
626      return createError("virtual address is not in any segment: 0x" +
627                         Twine::utohexstr(VAddr));
628    --I;
629    const Elf_Phdr &Phdr = **I;
630    uint64_t Delta = VAddr - Phdr.p_vaddr;
631    if (Delta >= Phdr.p_filesz)
632      return createError("virtual address is not in any segment: 0x" +
633                         Twine::utohexstr(VAddr));
634  
635    uint64_t Offset = Phdr.p_offset + Delta;
636    if (Offset >= getBufSize())
637      return createError("can't map virtual address 0x" +
638                         Twine::utohexstr(VAddr) + " to the segment with index " +
639                         Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
640                         ": the segment ends at 0x" +
641                         Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
642                         ", which is greater than the file size (0x" +
643                         Twine::utohexstr(getBufSize()) + ")");
644  
645    return base() + Offset;
646  }
647  
648  template <class ELFT>
649  Expected<std::vector<BBAddrMap>>
650  ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec,
651                                 const Elf_Shdr *RelaSec) const {
652    bool IsRelocatable = getHeader().e_type == ELF::ET_REL;
653  
654    // This DenseMap maps the offset of each function (the location of the
655    // reference to the function in the SHT_LLVM_BB_ADDR_MAP section) to the
656    // addend (the location of the function in the text section).
657    llvm::DenseMap<uint64_t, uint64_t> FunctionOffsetTranslations;
658    if (IsRelocatable && RelaSec) {
659      assert(RelaSec &&
660             "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
661             "object file without providing a relocation section.");
662      Expected<Elf_Rela_Range> Relas = this->relas(*RelaSec);
663      if (!Relas)
664        return createError("unable to read relocations for section " +
665                           describe(*this, Sec) + ": " +
666                           toString(Relas.takeError()));
667      for (Elf_Rela Rela : *Relas)
668        FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
669    }
670    Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
671    if (!ContentsOrErr)
672      return ContentsOrErr.takeError();
673    ArrayRef<uint8_t> Content = *ContentsOrErr;
674    DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
675    std::vector<BBAddrMap> FunctionEntries;
676  
677    DataExtractor::Cursor Cur(0);
678    Error ULEBSizeErr = Error::success();
679    Error MetadataDecodeErr = Error::success();
680    // Helper to extract and decode the next ULEB128 value as uint32_t.
681    // Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the uint32_t
682    // limit.
683    // Also returns zero if ULEBSizeErr is already in an error state.
684    auto ReadULEB128AsUInt32 = [&Data, &Cur, &ULEBSizeErr]() -> uint32_t {
685      // Bail out and do not extract data if ULEBSizeErr is already set.
686      if (ULEBSizeErr)
687        return 0;
688      uint64_t Offset = Cur.tell();
689      uint64_t Value = Data.getULEB128(Cur);
690      if (Value > UINT32_MAX) {
691        ULEBSizeErr = createError(
692            "ULEB128 value at offset 0x" + Twine::utohexstr(Offset) +
693            " exceeds UINT32_MAX (0x" + Twine::utohexstr(Value) + ")");
694        return 0;
695      }
696      return static_cast<uint32_t>(Value);
697    };
698  
699    uint8_t Version = 0;
700    while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
701           Cur.tell() < Content.size()) {
702      if (Sec.sh_type == ELF::SHT_LLVM_BB_ADDR_MAP) {
703        Version = Data.getU8(Cur);
704        if (!Cur)
705          break;
706        if (Version > 2)
707          return createError("unsupported SHT_LLVM_BB_ADDR_MAP version: " +
708                             Twine(static_cast<int>(Version)));
709        Data.getU8(Cur); // Feature byte
710      }
711      uint64_t SectionOffset = Cur.tell();
712      uintX_t Address = static_cast<uintX_t>(Data.getAddress(Cur));
713      if (!Cur)
714        return Cur.takeError();
715      if (IsRelocatable) {
716        assert(Address == 0);
717        auto FOTIterator = FunctionOffsetTranslations.find(SectionOffset);
718        if (FOTIterator == FunctionOffsetTranslations.end()) {
719          return createError("failed to get relocation data for offset: " +
720                             Twine::utohexstr(SectionOffset) + " in section " +
721                             describe(*this, Sec));
722        }
723        Address = FOTIterator->second;
724      }
725      uint32_t NumBlocks = ReadULEB128AsUInt32();
726      std::vector<BBAddrMap::BBEntry> BBEntries;
727      uint32_t PrevBBEndOffset = 0;
728      for (uint32_t BlockIndex = 0;
729           !MetadataDecodeErr && !ULEBSizeErr && Cur && (BlockIndex < NumBlocks);
730           ++BlockIndex) {
731        uint32_t ID = Version >= 2 ? ReadULEB128AsUInt32() : BlockIndex;
732        uint32_t Offset = ReadULEB128AsUInt32();
733        uint32_t Size = ReadULEB128AsUInt32();
734        uint32_t MD = ReadULEB128AsUInt32();
735        if (Version >= 1) {
736          // Offset is calculated relative to the end of the previous BB.
737          Offset += PrevBBEndOffset;
738          PrevBBEndOffset = Offset + Size;
739        }
740        Expected<BBAddrMap::BBEntry::Metadata> MetadataOrErr =
741            BBAddrMap::BBEntry::Metadata::decode(MD);
742        if (!MetadataOrErr) {
743          MetadataDecodeErr = MetadataOrErr.takeError();
744          break;
745        }
746        BBEntries.push_back({ID, Offset, Size, *MetadataOrErr});
747      }
748      FunctionEntries.push_back({Address, std::move(BBEntries)});
749    }
750    // Either Cur is in the error state, or we have an error in ULEBSizeErr or
751    // MetadataDecodeErr (but not both), but we join all errors here to be safe.
752    if (!Cur || ULEBSizeErr || MetadataDecodeErr)
753      return joinErrors(joinErrors(Cur.takeError(), std::move(ULEBSizeErr)),
754                        std::move(MetadataDecodeErr));
755    return FunctionEntries;
756  }
757  
758  template <class ELFT>
759  Expected<
760      MapVector<const typename ELFT::Shdr *, const typename ELFT::Shdr *>>
761  ELFFile<ELFT>::getSectionAndRelocations(
762      std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const {
763    MapVector<const Elf_Shdr *, const Elf_Shdr *> SecToRelocMap;
764    Error Errors = Error::success();
765    for (const Elf_Shdr &Sec : cantFail(this->sections())) {
766      Expected<bool> DoesSectionMatch = IsMatch(Sec);
767      if (!DoesSectionMatch) {
768        Errors = joinErrors(std::move(Errors), DoesSectionMatch.takeError());
769        continue;
770      }
771      if (*DoesSectionMatch) {
772        if (SecToRelocMap.insert(std::make_pair(&Sec, (const Elf_Shdr *)nullptr))
773                .second)
774          continue;
775      }
776  
777      if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL)
778        continue;
779  
780      Expected<const Elf_Shdr *> RelSecOrErr = this->getSection(Sec.sh_info);
781      if (!RelSecOrErr) {
782        Errors = joinErrors(std::move(Errors),
783                            createError(describe(*this, Sec) +
784                                        ": failed to get a relocated section: " +
785                                        toString(RelSecOrErr.takeError())));
786        continue;
787      }
788      const Elf_Shdr *ContentsSec = *RelSecOrErr;
789      Expected<bool> DoesRelTargetMatch = IsMatch(*ContentsSec);
790      if (!DoesRelTargetMatch) {
791        Errors = joinErrors(std::move(Errors), DoesRelTargetMatch.takeError());
792        continue;
793      }
794      if (*DoesRelTargetMatch)
795        SecToRelocMap[ContentsSec] = &Sec;
796    }
797    if(Errors)
798      return std::move(Errors);
799    return SecToRelocMap;
800  }
801  
802  template class llvm::object::ELFFile<ELF32LE>;
803  template class llvm::object::ELFFile<ELF32BE>;
804  template class llvm::object::ELFFile<ELF64LE>;
805  template class llvm::object::ELFFile<ELF64BE>;
806