1 //===- ELF.cpp - ELF object file implementation ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Object/ELF.h"
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/Support/DataExtractor.h"
15 using namespace object
;
17 #define STRINGIFY_ENUM_CASE(ns, name) \
21 #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
23 StringRef
llvm::object::getELFRelocationTypeName(uint32_t Machine
,
28 #include "llvm/BinaryFormat/ELFRelocs/M68k.def"
35 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
43 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
50 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
57 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
64 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
69 case ELF::EM_ARC_COMPACT
:
70 case ELF::EM_ARC_COMPACT2
:
72 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
79 #include "llvm/BinaryFormat/ELFRelocs/AVR.def"
86 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
93 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
100 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
107 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
114 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
121 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
127 case ELF::EM_SPARC32PLUS
:
128 case ELF::EM_SPARCV9
:
130 #include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
137 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
144 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
151 #include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
158 #include "llvm/BinaryFormat/ELFRelocs/VE.def"
165 #include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
170 case ELF::EM_LOONGARCH
:
172 #include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
179 #include "llvm/BinaryFormat/ELFRelocs/Xtensa.def"
192 uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine
) {
195 return ELF::R_X86_64_RELATIVE
;
198 return ELF::R_386_RELATIVE
;
201 case ELF::EM_AARCH64
:
202 return ELF::R_AARCH64_RELATIVE
;
204 return ELF::R_ARM_RELATIVE
;
205 case ELF::EM_ARC_COMPACT
:
206 case ELF::EM_ARC_COMPACT2
:
207 return ELF::R_ARC_RELATIVE
;
210 case ELF::EM_HEXAGON
:
211 return ELF::R_HEX_RELATIVE
;
217 return ELF::R_PPC64_RELATIVE
;
219 return ELF::R_RISCV_RELATIVE
;
221 return ELF::R_390_RELATIVE
;
223 case ELF::EM_SPARC32PLUS
:
224 case ELF::EM_SPARCV9
:
225 return ELF::R_SPARC_RELATIVE
;
227 return ELF::R_CKCORE_RELATIVE
;
229 return ELF::R_VE_RELATIVE
;
234 case ELF::EM_LOONGARCH
:
235 return ELF::R_LARCH_RELATIVE
;
242 StringRef
llvm::object::getELFSectionTypeName(uint32_t Machine
, unsigned 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
);
253 case ELF::EM_HEXAGON
:
254 switch (Type
) { STRINGIFY_ENUM_CASE(ELF
, SHT_HEX_ORDERED
); }
257 switch (Type
) { STRINGIFY_ENUM_CASE(ELF
, SHT_X86_64_UNWIND
); }
260 case ELF::EM_MIPS_RS3_LE
:
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
);
269 switch (Type
) { STRINGIFY_ENUM_CASE(ELF
, SHT_MSP430_ATTRIBUTES
); }
272 switch (Type
) { STRINGIFY_ENUM_CASE(ELF
, SHT_RISCV_ATTRIBUTES
); }
274 case ELF::EM_AARCH64
:
276 STRINGIFY_ENUM_CASE(ELF
, SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC
);
277 STRINGIFY_ENUM_CASE(ELF
, SHT_AARCH64_MEMTAG_GLOBALS_STATIC
);
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
);
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
333 // Proposal for adding SHT_RELR sections to generic-abi is here:
334 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
336 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
337 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
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.
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
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.
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.
362 Rel
.setType(getRelativeRelocationType(), false);
363 std::vector
<Elf_Rel
> Relocs
;
365 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
366 using Addr
= typename
ELFT::uint
;
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
);
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
);
384 Base
+= (CHAR_BIT
* sizeof(Entry
) - 1) * sizeof(Addr
);
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
);
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);
406 uint64_t NumRelocs
= Data
.getSLEB128(Cur
);
407 uint64_t Offset
= Data
.getSLEB128(Cur
);
411 return std::move(Cur
.takeError());
413 std::vector
<Elf_Rela
> Relocs
;
414 Relocs
.reserve(NumRelocs
);
416 uint64_t NumRelocsInGroup
= Data
.getSLEB128(Cur
);
418 return std::move(Cur
.takeError());
419 if (NumRelocsInGroup
> NumRelocs
)
420 return createError("relocation group unexpectedly large");
421 NumRelocs
-= NumRelocsInGroup
;
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
;
429 uint64_t GroupOffsetDelta
;
430 if (GroupedByOffsetDelta
)
431 GroupOffsetDelta
= Data
.getSLEB128(Cur
);
435 GroupRInfo
= Data
.getSLEB128(Cur
);
437 if (GroupedByAddend
&& GroupHasAddend
)
438 Addend
+= Data
.getSLEB128(Cur
);
443 for (uint64_t I
= 0; Cur
&& I
!= NumRelocsInGroup
; ++I
) {
445 Offset
+= GroupedByOffsetDelta
? GroupOffsetDelta
: Data
.getSLEB128(Cur
);
447 R
.r_info
= GroupedByInfo
? GroupRInfo
: Data
.getSLEB128(Cur
);
448 if (GroupHasAddend
&& !GroupedByAddend
)
449 Addend
+= Data
.getSLEB128(Cur
);
454 return std::move(Cur
.takeError());
460 template <class ELFT
>
461 std::string ELFFile
<ELFT
>::getDynamicTagAsString(unsigned Arch
,
462 uint64_t Type
) const {
463 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
467 #define DYNAMIC_TAG(n, v)
469 case ELF::EM_AARCH64
:
471 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
472 #include "llvm/BinaryFormat/DynamicTags.def"
473 #undef AARCH64_DYNAMIC_TAG
477 case ELF::EM_HEXAGON
:
479 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
480 #include "llvm/BinaryFormat/DynamicTags.def"
481 #undef HEXAGON_DYNAMIC_TAG
487 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
488 #include "llvm/BinaryFormat/DynamicTags.def"
489 #undef MIPS_DYNAMIC_TAG
495 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
496 #include "llvm/BinaryFormat/DynamicTags.def"
497 #undef PPC_DYNAMIC_TAG
503 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
504 #include "llvm/BinaryFormat/DynamicTags.def"
505 #undef PPC64_DYNAMIC_TAG
511 #define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
512 #include "llvm/BinaryFormat/DynamicTags.def"
513 #undef RISCV_DYNAMIC_TAG
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"
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
540 return "<unknown:>0x" + utohexstr(Type
, true);
544 template <class ELFT
>
545 std::string ELFFile
<ELFT
>::getDynamicTagAsString(uint64_t Type
) const {
546 return getDynamicTagAsString(getHeader().e_machine
, Type
);
549 template <class ELFT
>
550 Expected
<typename
ELFT::DynRange
> ELFFile
<ELFT
>::dynamicEntries() const {
551 ArrayRef
<Elf_Dyn
> Dyn
;
553 auto ProgramHeadersOrError
= program_headers();
554 if (!ProgramHeadersOrError
)
555 return ProgramHeadersOrError
.takeError();
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
));
565 // If we can't find the dynamic section in the program headers, we just fall
566 // back on the sections.
568 auto SectionsOrError
= sections();
569 if (!SectionsOrError
)
570 return SectionsOrError
.takeError();
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
);
577 return DynOrError
.takeError();
584 return ArrayRef
<Elf_Dyn
>();
588 return createError("invalid empty dynamic section");
590 if (Dyn
.back().d_tag
!= ELF::DT_NULL
)
591 return createError("dynamic sections must be DT_NULL terminated");
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();
603 llvm::SmallVector
<Elf_Phdr
*, 4> LoadSegments
;
605 for (const Elf_Phdr
&Phdr
: *ProgramHeadersOrError
)
606 if (Phdr
.p_type
== ELF::PT_LOAD
)
607 LoadSegments
.push_back(const_cast<Elf_Phdr
*>(&Phdr
));
609 auto SortPred
= [](const Elf_Phdr_Impl
<ELFT
> *A
,
610 const Elf_Phdr_Impl
<ELFT
> *B
) {
611 return A
->p_vaddr
< B
->p_vaddr
;
613 if (!llvm::is_sorted(LoadSegments
, SortPred
)) {
615 WarnHandler("loadable segments are unsorted by virtual address"))
617 llvm::stable_sort(LoadSegments
, SortPred
);
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
;
625 if (I
== LoadSegments
.begin())
626 return createError("virtual address is not in any segment: 0x" +
627 Twine::utohexstr(VAddr
));
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
));
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()) + ")");
645 return base() + Offset
;
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
;
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
) {
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
);
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
;
670 Expected
<ArrayRef
<uint8_t>> ContentsOrErr
= getSectionContents(Sec
);
672 return ContentsOrErr
.takeError();
673 ArrayRef
<uint8_t> Content
= *ContentsOrErr
;
674 DataExtractor
Data(Content
, isLE(), ELFT::Is64Bits
? 8 : 4);
675 std::vector
<BBAddrMap
> FunctionEntries
;
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
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.
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
) + ")");
696 return static_cast<uint32_t>(Value
);
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
);
707 return createError("unsupported SHT_LLVM_BB_ADDR_MAP version: " +
708 Twine(static_cast<int>(Version
)));
709 Data
.getU8(Cur
); // Feature byte
711 uint64_t SectionOffset
= Cur
.tell();
712 uintX_t Address
= static_cast<uintX_t
>(Data
.getAddress(Cur
));
714 return Cur
.takeError();
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
));
723 Address
= FOTIterator
->second
;
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
);
731 uint32_t ID
= Version
>= 2 ? ReadULEB128AsUInt32() : BlockIndex
;
732 uint32_t Offset
= ReadULEB128AsUInt32();
733 uint32_t Size
= ReadULEB128AsUInt32();
734 uint32_t MD
= ReadULEB128AsUInt32();
736 // Offset is calculated relative to the end of the previous BB.
737 Offset
+= PrevBBEndOffset
;
738 PrevBBEndOffset
= Offset
+ Size
;
740 Expected
<BBAddrMap::BBEntry::Metadata
> MetadataOrErr
=
741 BBAddrMap::BBEntry::Metadata::decode(MD
);
742 if (!MetadataOrErr
) {
743 MetadataDecodeErr
= MetadataOrErr
.takeError();
746 BBEntries
.push_back({ID
, Offset
, Size
, *MetadataOrErr
});
748 FunctionEntries
.push_back({Address
, std::move(BBEntries
)});
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
;
758 template <class ELFT
>
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());
771 if (*DoesSectionMatch
) {
772 if (SecToRelocMap
.insert(std::make_pair(&Sec
, (const Elf_Shdr
*)nullptr))
777 if (Sec
.sh_type
!= ELF::SHT_RELA
&& Sec
.sh_type
!= ELF::SHT_REL
)
780 Expected
<const Elf_Shdr
*> RelSecOrErr
= this->getSection(Sec
.sh_info
);
782 Errors
= joinErrors(std::move(Errors
),
783 createError(describe(*this, Sec
) +
784 ": failed to get a relocated section: " +
785 toString(RelSecOrErr
.takeError())));
788 const Elf_Shdr
*ContentsSec
= *RelSecOrErr
;
789 Expected
<bool> DoesRelTargetMatch
= IsMatch(*ContentsSec
);
790 if (!DoesRelTargetMatch
) {
791 Errors
= joinErrors(std::move(Errors
), DoesRelTargetMatch
.takeError());
794 if (*DoesRelTargetMatch
)
795 SecToRelocMap
[ContentsSec
] = &Sec
;
798 return std::move(Errors
);
799 return SecToRelocMap
;
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
>;