[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / Object / ELF.cpp
blob1352f91d3223c2f6a862bbf37af1e5bb86bb0bed
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Object/ELF.h"
10 #include "llvm/BinaryFormat/ELF.h"
11 #include "llvm/Support/DataExtractor.h"
13 using namespace llvm;
14 using namespace object;
16 #define STRINGIFY_ENUM_CASE(ns, name) \
17 case ns::name: \
18 return #name;
20 #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
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;
31 break;
32 case ELF::EM_X86_64:
33 switch (Type) {
34 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
35 default:
36 break;
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;
46 break;
47 case ELF::EM_MIPS:
48 switch (Type) {
49 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
50 default:
51 break;
53 break;
54 case ELF::EM_AARCH64:
55 switch (Type) {
56 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
57 default:
58 break;
60 break;
61 case ELF::EM_ARM:
62 switch (Type) {
63 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
64 default:
65 break;
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;
75 break;
76 case ELF::EM_AVR:
77 switch (Type) {
78 #include "llvm/BinaryFormat/ELFRelocs/AVR.def"
79 default:
80 break;
82 break;
83 case ELF::EM_HEXAGON:
84 switch (Type) {
85 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
86 default:
87 break;
89 break;
90 case ELF::EM_LANAI:
91 switch (Type) {
92 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
93 default:
94 break;
96 break;
97 case ELF::EM_PPC:
98 switch (Type) {
99 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
100 default:
101 break;
103 break;
104 case ELF::EM_PPC64:
105 switch (Type) {
106 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
107 default:
108 break;
110 break;
111 case ELF::EM_RISCV:
112 switch (Type) {
113 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
114 default:
115 break;
117 break;
118 case ELF::EM_S390:
119 switch (Type) {
120 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
121 default:
122 break;
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;
133 break;
134 case ELF::EM_AMDGPU:
135 switch (Type) {
136 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
137 default:
138 break;
140 break;
141 case ELF::EM_BPF:
142 switch (Type) {
143 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
144 default:
145 break;
147 break;
148 case ELF::EM_MSP430:
149 switch (Type) {
150 #include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
151 default:
152 break;
154 break;
155 case ELF::EM_VE:
156 switch (Type) {
157 #include "llvm/BinaryFormat/ELFRelocs/VE.def"
158 default:
159 break;
161 break;
162 case ELF::EM_CSKY:
163 switch (Type) {
164 #include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
165 default:
166 break;
168 break;
169 default:
170 break;
172 return "Unknown";
175 #undef ELF_RELOC
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_AMDGPU:
214 break;
215 case ELF::EM_BPF:
216 break;
217 default:
218 break;
220 return 0;
223 StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
224 switch (Machine) {
225 case ELF::EM_ARM:
226 switch (Type) {
227 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
228 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
229 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
230 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
231 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
233 break;
234 case ELF::EM_HEXAGON:
235 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
236 break;
237 case ELF::EM_X86_64:
238 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
239 break;
240 case ELF::EM_MIPS:
241 case ELF::EM_MIPS_RS3_LE:
242 switch (Type) {
243 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
244 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
245 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
246 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
248 break;
249 case ELF::EM_RISCV:
250 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
251 break;
252 default:
253 break;
256 switch (Type) {
257 STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
258 STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
259 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
260 STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
261 STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
262 STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
263 STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
264 STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
265 STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
266 STRINGIFY_ENUM_CASE(ELF, SHT_REL);
267 STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
268 STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
269 STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
270 STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
271 STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
272 STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
273 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
274 STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
275 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
276 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
277 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
278 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
279 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
280 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
281 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
282 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
283 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
284 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
285 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
286 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP);
287 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
288 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
289 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
290 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
291 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
292 default:
293 return "Unknown";
297 template <class ELFT>
298 std::vector<typename ELFT::Rel>
299 ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
300 // This function decodes the contents of an SHT_RELR packed relocation
301 // section.
303 // Proposal for adding SHT_RELR sections to generic-abi is here:
304 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
306 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
307 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
309 // i.e. start with an address, followed by any number of bitmaps. The address
310 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
311 // relocations each, at subsequent offsets following the last address entry.
313 // The bitmap entries must have 1 in the least significant bit. The assumption
314 // here is that an address cannot have 1 in lsb. Odd addresses are not
315 // supported.
317 // Excluding the least significant bit in the bitmap, each non-zero bit in
318 // the bitmap represents a relocation to be applied to a corresponding machine
319 // word that follows the base address word. The second least significant bit
320 // represents the machine word immediately following the initial address, and
321 // each bit that follows represents the next word, in linear order. As such,
322 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
323 // 63 relocations in a 64-bit object.
325 // This encoding has a couple of interesting properties:
326 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
327 // even means address, odd means bitmap.
328 // 2. Just a simple list of addresses is a valid encoding.
330 Elf_Rel Rel;
331 Rel.r_info = 0;
332 Rel.setType(getRelativeRelocationType(), false);
333 std::vector<Elf_Rel> Relocs;
335 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
336 typedef typename ELFT::uint Word;
338 // Word size in number of bytes.
339 const size_t WordSize = sizeof(Word);
341 // Number of bits used for the relocation offsets bitmap.
342 // These many relative relocations can be encoded in a single entry.
343 const size_t NBits = 8*WordSize - 1;
345 Word Base = 0;
346 for (const Elf_Relr &R : relrs) {
347 Word Entry = R;
348 if ((Entry&1) == 0) {
349 // Even entry: encodes the offset for next relocation.
350 Rel.r_offset = Entry;
351 Relocs.push_back(Rel);
352 // Set base offset for subsequent bitmap entries.
353 Base = Entry + WordSize;
354 continue;
357 // Odd entry: encodes bitmap for relocations starting at base.
358 Word Offset = Base;
359 while (Entry != 0) {
360 Entry >>= 1;
361 if ((Entry&1) != 0) {
362 Rel.r_offset = Offset;
363 Relocs.push_back(Rel);
365 Offset += WordSize;
368 // Advance base offset by NBits words.
369 Base += NBits * WordSize;
372 return Relocs;
375 template <class ELFT>
376 Expected<std::vector<typename ELFT::Rela>>
377 ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
378 // This function reads relocations in Android's packed relocation format,
379 // which is based on SLEB128 and delta encoding.
380 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
381 if (!ContentsOrErr)
382 return ContentsOrErr.takeError();
383 ArrayRef<uint8_t> Content = *ContentsOrErr;
384 if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' ||
385 Content[2] != 'S' || Content[3] != '2')
386 return createError("invalid packed relocation header");
387 DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
388 DataExtractor::Cursor Cur(/*Offset=*/4);
390 uint64_t NumRelocs = Data.getSLEB128(Cur);
391 uint64_t Offset = Data.getSLEB128(Cur);
392 uint64_t Addend = 0;
394 if (!Cur)
395 return std::move(Cur.takeError());
397 std::vector<Elf_Rela> Relocs;
398 Relocs.reserve(NumRelocs);
399 while (NumRelocs) {
400 uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
401 if (!Cur)
402 return std::move(Cur.takeError());
403 if (NumRelocsInGroup > NumRelocs)
404 return createError("relocation group unexpectedly large");
405 NumRelocs -= NumRelocsInGroup;
407 uint64_t GroupFlags = Data.getSLEB128(Cur);
408 bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
409 bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
410 bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
411 bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
413 uint64_t GroupOffsetDelta;
414 if (GroupedByOffsetDelta)
415 GroupOffsetDelta = Data.getSLEB128(Cur);
417 uint64_t GroupRInfo;
418 if (GroupedByInfo)
419 GroupRInfo = Data.getSLEB128(Cur);
421 if (GroupedByAddend && GroupHasAddend)
422 Addend += Data.getSLEB128(Cur);
424 if (!GroupHasAddend)
425 Addend = 0;
427 for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) {
428 Elf_Rela R;
429 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
430 R.r_offset = Offset;
431 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
432 if (GroupHasAddend && !GroupedByAddend)
433 Addend += Data.getSLEB128(Cur);
434 R.r_addend = Addend;
435 Relocs.push_back(R);
437 if (!Cur)
438 return std::move(Cur.takeError());
441 return Relocs;
444 template <class ELFT>
445 std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
446 uint64_t Type) const {
447 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
448 case value: \
449 return #tag;
451 #define DYNAMIC_TAG(n, v)
452 switch (Arch) {
453 case ELF::EM_AARCH64:
454 switch (Type) {
455 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
456 #include "llvm/BinaryFormat/DynamicTags.def"
457 #undef AARCH64_DYNAMIC_TAG
459 break;
461 case ELF::EM_HEXAGON:
462 switch (Type) {
463 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
464 #include "llvm/BinaryFormat/DynamicTags.def"
465 #undef HEXAGON_DYNAMIC_TAG
467 break;
469 case ELF::EM_MIPS:
470 switch (Type) {
471 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
472 #include "llvm/BinaryFormat/DynamicTags.def"
473 #undef MIPS_DYNAMIC_TAG
475 break;
477 case ELF::EM_PPC:
478 switch (Type) {
479 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
480 #include "llvm/BinaryFormat/DynamicTags.def"
481 #undef PPC_DYNAMIC_TAG
483 break;
485 case ELF::EM_PPC64:
486 switch (Type) {
487 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
488 #include "llvm/BinaryFormat/DynamicTags.def"
489 #undef PPC64_DYNAMIC_TAG
491 break;
493 #undef DYNAMIC_TAG
494 switch (Type) {
495 // Now handle all dynamic tags except the architecture specific ones
496 #define AARCH64_DYNAMIC_TAG(name, value)
497 #define MIPS_DYNAMIC_TAG(name, value)
498 #define HEXAGON_DYNAMIC_TAG(name, value)
499 #define PPC_DYNAMIC_TAG(name, value)
500 #define PPC64_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 DYNAMIC_TAG_MARKER
512 #undef DYNAMIC_STRINGIFY_ENUM
513 default:
514 return "<unknown:>0x" + utohexstr(Type, true);
518 template <class ELFT>
519 std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
520 return getDynamicTagAsString(getHeader().e_machine, Type);
523 template <class ELFT>
524 Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
525 ArrayRef<Elf_Dyn> Dyn;
527 auto ProgramHeadersOrError = program_headers();
528 if (!ProgramHeadersOrError)
529 return ProgramHeadersOrError.takeError();
531 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
532 if (Phdr.p_type == ELF::PT_DYNAMIC) {
533 Dyn = makeArrayRef(
534 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
535 Phdr.p_filesz / sizeof(Elf_Dyn));
536 break;
540 // If we can't find the dynamic section in the program headers, we just fall
541 // back on the sections.
542 if (Dyn.empty()) {
543 auto SectionsOrError = sections();
544 if (!SectionsOrError)
545 return SectionsOrError.takeError();
547 for (const Elf_Shdr &Sec : *SectionsOrError) {
548 if (Sec.sh_type == ELF::SHT_DYNAMIC) {
549 Expected<ArrayRef<Elf_Dyn>> DynOrError =
550 getSectionContentsAsArray<Elf_Dyn>(Sec);
551 if (!DynOrError)
552 return DynOrError.takeError();
553 Dyn = *DynOrError;
554 break;
558 if (!Dyn.data())
559 return ArrayRef<Elf_Dyn>();
562 if (Dyn.empty())
563 // TODO: this error is untested.
564 return createError("invalid empty dynamic section");
566 if (Dyn.back().d_tag != ELF::DT_NULL)
567 // TODO: this error is untested.
568 return createError("dynamic sections must be DT_NULL terminated");
570 return Dyn;
573 template <class ELFT>
574 Expected<const uint8_t *>
575 ELFFile<ELFT>::toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler) const {
576 auto ProgramHeadersOrError = program_headers();
577 if (!ProgramHeadersOrError)
578 return ProgramHeadersOrError.takeError();
580 llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
582 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
583 if (Phdr.p_type == ELF::PT_LOAD)
584 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
586 auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A,
587 const Elf_Phdr_Impl<ELFT> *B) {
588 return A->p_vaddr < B->p_vaddr;
590 if (!llvm::is_sorted(LoadSegments, SortPred)) {
591 if (Error E =
592 WarnHandler("loadable segments are unsorted by virtual address"))
593 return std::move(E);
594 llvm::stable_sort(LoadSegments, SortPred);
597 const Elf_Phdr *const *I = llvm::upper_bound(
598 LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
599 return VAddr < Phdr->p_vaddr;
602 if (I == LoadSegments.begin())
603 return createError("virtual address is not in any segment: 0x" +
604 Twine::utohexstr(VAddr));
605 --I;
606 const Elf_Phdr &Phdr = **I;
607 uint64_t Delta = VAddr - Phdr.p_vaddr;
608 if (Delta >= Phdr.p_filesz)
609 return createError("virtual address is not in any segment: 0x" +
610 Twine::utohexstr(VAddr));
612 uint64_t Offset = Phdr.p_offset + Delta;
613 if (Offset >= getBufSize())
614 return createError("can't map virtual address 0x" +
615 Twine::utohexstr(VAddr) + " to the segment with index " +
616 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
617 ": the segment ends at 0x" +
618 Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
619 ", which is greater than the file size (0x" +
620 Twine::utohexstr(getBufSize()) + ")");
622 return base() + Offset;
625 template <class ELFT>
626 Expected<std::vector<typename ELFT::BBAddrMap>>
627 ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec) const {
628 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
629 if (!ContentsOrErr)
630 return ContentsOrErr.takeError();
631 ArrayRef<uint8_t> Content = *ContentsOrErr;
632 DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
633 std::vector<Elf_BBAddrMap> FunctionEntries;
635 DataExtractor::Cursor Cur(0);
636 Error ULEBSizeErr = Error::success();
638 // Helper to extract and decode the next ULEB128 value as uint32_t.
639 // Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the uint32_t
640 // limit.
641 // Also returns zero if ULEBSizeErr is already in an error state.
642 auto ReadULEB128AsUInt32 = [&Data, &Cur, &ULEBSizeErr]() -> uint32_t {
643 // Bail out and do not extract data if ULEBSizeErr is already set.
644 if (ULEBSizeErr)
645 return 0;
646 uint64_t Offset = Cur.tell();
647 uint64_t Value = Data.getULEB128(Cur);
648 if (Value > UINT32_MAX) {
649 ULEBSizeErr = createError(
650 "ULEB128 value at offset 0x" + Twine::utohexstr(Offset) +
651 " exceeds UINT32_MAX (0x" + Twine::utohexstr(Value) + ")");
652 return 0;
654 return static_cast<uint32_t>(Value);
657 while (!ULEBSizeErr && Cur && Cur.tell() < Content.size()) {
658 uintX_t Address = static_cast<uintX_t>(Data.getAddress(Cur));
659 uint32_t NumBlocks = ReadULEB128AsUInt32();
660 std::vector<typename Elf_BBAddrMap::BBEntry> BBEntries;
661 for (uint32_t BlockID = 0; !ULEBSizeErr && Cur && (BlockID < NumBlocks);
662 ++BlockID) {
663 uint32_t Offset = ReadULEB128AsUInt32();
664 uint32_t Size = ReadULEB128AsUInt32();
665 uint32_t Metadata = ReadULEB128AsUInt32();
666 BBEntries.push_back({Offset, Size, Metadata});
668 FunctionEntries.push_back({Address, BBEntries});
670 // Either Cur is in the error state, or ULEBSizeError is set (not both), but
671 // we join the two errors here to be safe.
672 if (!Cur || ULEBSizeErr)
673 return joinErrors(Cur.takeError(), std::move(ULEBSizeErr));
674 return FunctionEntries;
677 template class llvm::object::ELFFile<ELF32LE>;
678 template class llvm::object::ELFFile<ELF32BE>;
679 template class llvm::object::ELFFile<ELF64LE>;
680 template class llvm::object::ELFFile<ELF64BE>;