15using namespace object;
17#define STRINGIFY_ENUM_CASE(ns, name) \
21#define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
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"
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"
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"
172#include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
179#include "llvm/BinaryFormat/ELFRelocs/Xtensa.def"
195 return ELF::R_X86_64_RELATIVE;
198 return ELF::R_386_RELATIVE;
202 return ELF::R_AARCH64_RELATIVE;
204 return ELF::R_ARM_RELATIVE;
207 return ELF::R_ARC_RELATIVE;
211 return ELF::R_HEX_RELATIVE;
217 return ELF::R_PPC64_RELATIVE;
219 return ELF::R_RISCV_RELATIVE;
221 return ELF::R_390_RELATIVE;
225 return ELF::R_SPARC_RELATIVE;
227 return ELF::R_CKCORE_RELATIVE;
229 return ELF::R_VE_RELATIVE;
235 return ELF::R_LARCH_RELATIVE;
332std::vector<typename ELFT::Rel>
366 Rel.setType(getRelativeRelocationType(),
false);
367 std::vector<Elf_Rel> Relocs;
370 using Addr =
typename ELFT::uint;
373 for (Elf_Relr R : relrs) {
374 typename ELFT::uint Entry = R;
375 if ((Entry & 1) == 0) {
377 Rel.r_offset = Entry;
378 Relocs.push_back(Rel);
384 if ((Entry & 1) != 0) {
386 Relocs.push_back(Rel);
388 Base += (CHAR_BIT *
sizeof(Entry) - 1) *
sizeof(
Addr);
406 return createError(
"invalid packed relocation header");
410 uint64_t NumRelocs = Data.getSLEB128(Cur);
417 std::vector<Elf_Rela> Relocs;
418 Relocs.reserve(NumRelocs);
420 uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
423 if (NumRelocsInGroup > NumRelocs)
424 return createError(
"relocation group unexpectedly large");
425 NumRelocs -= NumRelocsInGroup;
427 uint64_t GroupFlags = Data.getSLEB128(Cur);
434 if (GroupedByOffsetDelta)
435 GroupOffsetDelta = Data.getSLEB128(Cur);
439 GroupRInfo = Data.getSLEB128(Cur);
441 if (GroupedByAddend && GroupHasAddend)
442 Addend += Data.getSLEB128(Cur);
447 for (
uint64_t I = 0; Cur &&
I != NumRelocsInGroup; ++
I) {
449 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
451 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
452 if (GroupHasAddend && !GroupedByAddend)
453 Addend += Data.getSLEB128(Cur);
467#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
471#define DYNAMIC_TAG(n, v)
475#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
476#include "llvm/BinaryFormat/DynamicTags.def"
477#undef AARCH64_DYNAMIC_TAG
483#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
484#include "llvm/BinaryFormat/DynamicTags.def"
485#undef HEXAGON_DYNAMIC_TAG
491#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
492#include "llvm/BinaryFormat/DynamicTags.def"
493#undef MIPS_DYNAMIC_TAG
499#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
500#include "llvm/BinaryFormat/DynamicTags.def"
501#undef PPC_DYNAMIC_TAG
507#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
508#include "llvm/BinaryFormat/DynamicTags.def"
509#undef PPC64_DYNAMIC_TAG
515#define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
516#include "llvm/BinaryFormat/DynamicTags.def"
517#undef RISCV_DYNAMIC_TAG
524#define AARCH64_DYNAMIC_TAG(name, value)
525#define MIPS_DYNAMIC_TAG(name, value)
526#define HEXAGON_DYNAMIC_TAG(name, value)
527#define PPC_DYNAMIC_TAG(name, value)
528#define PPC64_DYNAMIC_TAG(name, value)
529#define RISCV_DYNAMIC_TAG(name, value)
531#define DYNAMIC_TAG_MARKER(name, value)
532#define DYNAMIC_TAG(name, value) case value: return #name;
533#include "llvm/BinaryFormat/DynamicTags.def"
535#undef AARCH64_DYNAMIC_TAG
536#undef MIPS_DYNAMIC_TAG
537#undef HEXAGON_DYNAMIC_TAG
538#undef PPC_DYNAMIC_TAG
539#undef PPC64_DYNAMIC_TAG
540#undef RISCV_DYNAMIC_TAG
541#undef DYNAMIC_TAG_MARKER
542#undef DYNAMIC_STRINGIFY_ENUM
544 return "<unknown:>0x" + utohexstr(
Type,
true);
550 return getDynamicTagAsString(getHeader().e_machine,
Type);
557 auto ProgramHeadersOrError = program_headers();
558 if (!ProgramHeadersOrError)
559 return ProgramHeadersOrError.takeError();
561 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
563 const uint8_t *DynOffset = base() + Phdr.p_offset;
564 if (DynOffset > end())
566 "dynamic section offset past file size: corrupted ELF");
567 Dyn =
ArrayRef(
reinterpret_cast<const Elf_Dyn *
>(DynOffset),
568 Phdr.p_filesz /
sizeof(Elf_Dyn));
577 if (!SectionsOrError)
578 return SectionsOrError.takeError();
580 for (
const Elf_Shdr &Sec : *SectionsOrError) {
583 getSectionContentsAsArray<Elf_Dyn>(Sec);
596 return createError(
"invalid empty dynamic section");
598 if (Dyn.
back().d_tag != ELF::DT_NULL)
599 return createError(
"dynamic sections must be DT_NULL terminated");
607 auto ProgramHeadersOrError = program_headers();
608 if (!ProgramHeadersOrError)
609 return ProgramHeadersOrError.takeError();
613 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError)
615 LoadSegments.
push_back(
const_cast<Elf_Phdr *
>(&Phdr));
619 return A->p_vaddr <
B->p_vaddr;
623 WarnHandler(
"loadable segments are unsorted by virtual address"))
630 return VAddr < Phdr->p_vaddr;
633 if (
I == LoadSegments.
begin())
634 return createError(
"virtual address is not in any segment: 0x" +
637 const Elf_Phdr &Phdr = **
I;
638 uint64_t Delta = VAddr - Phdr.p_vaddr;
639 if (Delta >= Phdr.p_filesz)
640 return createError(
"virtual address is not in any segment: 0x" +
644 if (
Offset >= getBufSize())
645 return createError(
"can't map virtual address 0x" +
647 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
648 ": the segment ends at 0x" +
650 ", which is greater than the file size (0x" +
661template <
typename IntTy, std::enable_if_t<std::is_
unsigned_v<IntTy>,
int> = 0>
663 Error &ULEBSizeErr) {
669 if (
Value > std::numeric_limits<IntTy>::max()) {
670 ULEBSizeErr =
createError(
"ULEB128 value at offset 0x" +
672 Twine(std::numeric_limits<IntTy>::digits) +
676 return static_cast<IntTy
>(
Value);
679template <
typename ELFT>
684 std::vector<PGOAnalysisMap> *PGOAnalyses) {
691 if (IsRelocatable && RelaSec) {
693 "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
694 "object file without providing a relocation section.");
697 return createError(
"unable to read relocations for section " +
701 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
703 auto GetAddressForRelocation =
706 FunctionOffsetTranslations.
find(RelocationOffsetInSection);
707 if (FOTIterator == FunctionOffsetTranslations.
end()) {
708 return createError(
"failed to get relocation data for offset: " +
710 " in section " +
describe(EF, Sec));
712 return FOTIterator->second;
719 std::vector<BBAddrMap> FunctionEntries;
736 GetAddressForRelocation(RelocationOffsetInSection);
739 return *AddressOrErr;
745 while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
748 Version = Data.getU8(Cur);
752 return createError(
"unsupported SHT_LLVM_BB_ADDR_MAP version: " +
753 Twine(
static_cast<int>(Version)));
754 Feature = Data.getU8(Cur);
758 if (!FeatEnableOrErr)
759 return FeatEnableOrErr.takeError();
760 FeatEnable = *FeatEnableOrErr;
761 if (Feature != 0 && Version < 2 && Cur)
763 "version should be >= 2 for SHT_LLVM_BB_ADDR_MAP when "
764 "PGO features are enabled: version = " +
765 Twine(
static_cast<int>(Version)) +
766 " feature = " +
Twine(
static_cast<int>(Feature)));
771 std::vector<BBAddrMap::BBEntry> BBEntries;
772 if (FeatEnable.MultiBBRange) {
773 NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
774 if (!Cur || ULEBSizeErr)
777 return createError(
"invalid zero number of BB ranges at offset " +
781 auto AddressOrErr = ExtractAddress();
783 return AddressOrErr.takeError();
784 RangeBaseAddress = *AddressOrErr;
785 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
787 std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
789 for (
uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
792 if (FeatEnable.MultiBBRange) {
793 auto AddressOrErr = ExtractAddress();
795 return AddressOrErr.takeError();
796 RangeBaseAddress = *AddressOrErr;
797 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
799 for (
uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr && Cur &&
800 (BlockIndex < NumBlocksInBBRange);
803 ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
805 uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
806 uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
807 uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
810 Offset += PrevBBEndOffset;
815 if (!MetadataOrErr) {
816 MetadataDecodeErr = MetadataOrErr.
takeError();
819 BBEntries.push_back({
ID,
Offset,
Size, *MetadataOrErr});
821 TotalNumBlocks += BBEntries.size();
822 BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
824 FunctionEntries.push_back({std::move(BBRangeEntries)});
826 if (PGOAnalyses || FeatEnable.hasPGOAnalysis()) {
829 FeatEnable.FuncEntryCount
830 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
833 std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
835 FeatEnable.hasPGOAnalysisBBData() && !MetadataDecodeErr &&
836 !ULEBSizeErr && Cur && (BlockIndex < TotalNumBlocks);
840 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
846 if (FeatEnable.BrProb) {
847 auto SuccCount = readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr);
849 uint32_t BBID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
850 uint32_t BrProb = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
857 PGOBBEntries.push_back({
BlockFrequency(BBF), std::move(Successors)});
861 PGOAnalyses->push_back(
867 if (!Cur || ULEBSizeErr || MetadataDecodeErr)
869 std::move(MetadataDecodeErr));
870 return FunctionEntries;
876 std::vector<PGOAnalysisMap> *PGOAnalyses)
const {
877 size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
880 if (!AddrMapsOrErr && PGOAnalyses)
881 PGOAnalyses->resize(OriginalPGOSize);
882 return std::move(AddrMapsOrErr);
894 if (!DoesSectionMatch) {
898 if (*DoesSectionMatch) {
899 if (SecToRelocMap.
insert(std::make_pair(&Sec, (
const Elf_Shdr *)
nullptr))
911 ": failed to get a relocated section: " +
915 const Elf_Shdr *ContentsSec = *RelSecOrErr;
917 if (!DoesRelTargetMatch) {
921 if (*DoesRelTargetMatch)
922 SecToRelocMap[ContentsSec] = &Sec;
925 return std::move(Errors);
926 return SecToRelocMap;
bbsections Prepares for basic block sections
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
COFF::MachineTypes Machine
static IntTy readULEB128As(DataExtractor &Data, DataExtractor::Cursor &Cur, Error &ULEBSizeErr)
#define STRINGIFY_ENUM_CASE(ns, name)
static Expected< std::vector< BBAddrMap > > decodeBBAddrMapImpl(const ELFFile< ELFT > &EF, const typename ELFFile< ELFT >::Elf_Shdr &Sec, const typename ELFFile< ELFT >::Elf_Shdr *RelaSec, std::vector< PGOAnalysisMap > *PGOAnalyses)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
const T & back() const
back - Get the last element.
bool empty() const
empty - Check if the array is empty.
static BranchProbability getRaw(uint32_t N)
iterator find(const_arg_type_t< KeyT > Val)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
This class implements a map that also provides access to all stored values in a deterministic order.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static Twine utohexstr(const uint64_t &Val)
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
An efficient, type-erasing, non-owning reference to a callable.
const Elf_Ehdr & getHeader() const
Expected< std::vector< Elf_Rela > > android_relas(const Elf_Shdr &Sec) const
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
Expected< std::vector< BBAddrMap > > decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec=nullptr, std::vector< PGOAnalysisMap > *PGOAnalyses=nullptr) const
Returns a vector of BBAddrMap structs corresponding to each function within the text section that the...
Expected< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr &Sec) const
Expected< Elf_Rela_Range > relas(const Elf_Shdr &Sec) const
Expected< Elf_Dyn_Range > dynamicEntries() const
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler=&defaultWarningHandler) const
Expected< MapVector< const Elf_Shdr *, const Elf_Shdr * > > getSectionAndRelocations(std::function< Expected< bool >(const Elf_Shdr &)> IsMatch) const
Returns a map from every section matching IsMatch to its relocation section, or nullptr if it has no ...
std::vector< Elf_Rel > decode_relrs(Elf_Relr_Range relrs) const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG
@ RELOCATION_GROUPED_BY_INFO_FLAG
@ RELOCATION_GROUPED_BY_ADDEND_FLAG
@ RELOCATION_GROUP_HAS_ADDEND_FLAG
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Error createError(const Twine &Err)
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
uint32_t getELFRelativeRelocationType(uint32_t Machine)
static std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Error joinErrors(Error E1, Error E2)
Concatenate errors.
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
static Expected< Metadata > decode(uint32_t V)
static Expected< Features > decode(uint8_t Val)