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