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