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;
329std::vector<typename ELFT::Rel>
363 Rel.setType(getRelativeRelocationType(),
false);
364 std::vector<Elf_Rel> Relocs;
367 using Addr =
typename ELFT::uint;
370 for (Elf_Relr R : relrs) {
371 typename ELFT::uint Entry = R;
372 if ((Entry & 1) == 0) {
374 Rel.r_offset = Entry;
375 Relocs.push_back(Rel);
381 if ((Entry & 1) != 0) {
383 Relocs.push_back(Rel);
385 Base += (CHAR_BIT *
sizeof(Entry) - 1) *
sizeof(
Addr);
403 return createError(
"invalid packed relocation header");
407 uint64_t NumRelocs = Data.getSLEB128(Cur);
414 std::vector<Elf_Rela> Relocs;
415 Relocs.reserve(NumRelocs);
417 uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
420 if (NumRelocsInGroup > NumRelocs)
421 return createError(
"relocation group unexpectedly large");
422 NumRelocs -= NumRelocsInGroup;
424 uint64_t GroupFlags = Data.getSLEB128(Cur);
431 if (GroupedByOffsetDelta)
432 GroupOffsetDelta = Data.getSLEB128(Cur);
436 GroupRInfo = Data.getSLEB128(Cur);
438 if (GroupedByAddend && GroupHasAddend)
439 Addend += Data.getSLEB128(Cur);
444 for (
uint64_t I = 0; Cur &&
I != NumRelocsInGroup; ++
I) {
446 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
448 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
449 if (GroupHasAddend && !GroupedByAddend)
450 Addend += Data.getSLEB128(Cur);
464#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
468#define DYNAMIC_TAG(n, v)
472#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
473#include "llvm/BinaryFormat/DynamicTags.def"
474#undef AARCH64_DYNAMIC_TAG
480#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
481#include "llvm/BinaryFormat/DynamicTags.def"
482#undef HEXAGON_DYNAMIC_TAG
488#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
489#include "llvm/BinaryFormat/DynamicTags.def"
490#undef MIPS_DYNAMIC_TAG
496#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
497#include "llvm/BinaryFormat/DynamicTags.def"
498#undef PPC_DYNAMIC_TAG
504#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
505#include "llvm/BinaryFormat/DynamicTags.def"
506#undef PPC64_DYNAMIC_TAG
512#define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
513#include "llvm/BinaryFormat/DynamicTags.def"
514#undef RISCV_DYNAMIC_TAG
521#define AARCH64_DYNAMIC_TAG(name, value)
522#define MIPS_DYNAMIC_TAG(name, value)
523#define HEXAGON_DYNAMIC_TAG(name, value)
524#define PPC_DYNAMIC_TAG(name, value)
525#define PPC64_DYNAMIC_TAG(name, value)
526#define RISCV_DYNAMIC_TAG(name, value)
528#define DYNAMIC_TAG_MARKER(name, value)
529#define DYNAMIC_TAG(name, value) case value: return #name;
530#include "llvm/BinaryFormat/DynamicTags.def"
532#undef AARCH64_DYNAMIC_TAG
533#undef MIPS_DYNAMIC_TAG
534#undef HEXAGON_DYNAMIC_TAG
535#undef PPC_DYNAMIC_TAG
536#undef PPC64_DYNAMIC_TAG
537#undef RISCV_DYNAMIC_TAG
538#undef DYNAMIC_TAG_MARKER
539#undef DYNAMIC_STRINGIFY_ENUM
541 return "<unknown:>0x" + utohexstr(
Type,
true);
547 return getDynamicTagAsString(getHeader().e_machine,
Type);
554 auto ProgramHeadersOrError = program_headers();
555 if (!ProgramHeadersOrError)
556 return ProgramHeadersOrError.takeError();
558 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
560 Dyn =
ArrayRef(
reinterpret_cast<const Elf_Dyn *
>(base() + Phdr.p_offset),
561 Phdr.p_filesz /
sizeof(Elf_Dyn));
570 if (!SectionsOrError)
571 return SectionsOrError.takeError();
573 for (
const Elf_Shdr &Sec : *SectionsOrError) {
576 getSectionContentsAsArray<Elf_Dyn>(Sec);
589 return createError(
"invalid empty dynamic section");
591 if (Dyn.
back().d_tag != ELF::DT_NULL)
592 return createError(
"dynamic sections must be DT_NULL terminated");
600 auto ProgramHeadersOrError = program_headers();
601 if (!ProgramHeadersOrError)
602 return ProgramHeadersOrError.takeError();
606 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError)
608 LoadSegments.
push_back(
const_cast<Elf_Phdr *
>(&Phdr));
612 return A->p_vaddr <
B->p_vaddr;
616 WarnHandler(
"loadable segments are unsorted by virtual address"))
623 return VAddr < Phdr->p_vaddr;
626 if (
I == LoadSegments.
begin())
627 return createError(
"virtual address is not in any segment: 0x" +
630 const Elf_Phdr &Phdr = **
I;
631 uint64_t Delta = VAddr - Phdr.p_vaddr;
632 if (Delta >= Phdr.p_filesz)
633 return createError(
"virtual address is not in any segment: 0x" +
637 if (
Offset >= getBufSize())
638 return createError(
"can't map virtual address 0x" +
640 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
641 ": the segment ends at 0x" +
643 ", which is greater than the file size (0x" +
654template <
typename IntTy, std::enable_if_t<std::is_
unsigned_v<IntTy>,
int> = 0>
656 Error &ULEBSizeErr) {
662 if (
Value > std::numeric_limits<IntTy>::max()) {
663 ULEBSizeErr =
createError(
"ULEB128 value at offset 0x" +
665 Twine(std::numeric_limits<IntTy>::digits) +
669 return static_cast<IntTy
>(
Value);
672template <
typename ELFT>
677 std::vector<PGOAnalysisMap> *PGOAnalyses) {
684 if (IsRelocatable && RelaSec) {
686 "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
687 "object file without providing a relocation section.");
690 return createError(
"unable to read relocations for section " +
694 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
696 auto GetAddressForRelocation =
699 FunctionOffsetTranslations.
find(RelocationOffsetInSection);
700 if (FOTIterator == FunctionOffsetTranslations.
end()) {
701 return createError(
"failed to get relocation data for offset: " +
703 " in section " +
describe(EF, Sec));
705 return FOTIterator->second;
712 std::vector<BBAddrMap> FunctionEntries;
729 GetAddressForRelocation(RelocationOffsetInSection);
732 return *AddressOrErr;
738 while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
741 Version = Data.getU8(Cur);
745 return createError(
"unsupported SHT_LLVM_BB_ADDR_MAP version: " +
746 Twine(
static_cast<int>(Version)));
747 Feature = Data.getU8(Cur);
751 if (!FeatEnableOrErr)
752 return FeatEnableOrErr.takeError();
753 FeatEnable = *FeatEnableOrErr;
754 if (Feature != 0 && Version < 2 && Cur)
756 "version should be >= 2 for SHT_LLVM_BB_ADDR_MAP when "
757 "PGO features are enabled: version = " +
758 Twine(
static_cast<int>(Version)) +
759 " feature = " +
Twine(
static_cast<int>(Feature)));
764 std::vector<BBAddrMap::BBEntry> BBEntries;
765 if (FeatEnable.MultiBBRange) {
766 NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
767 if (!Cur || ULEBSizeErr)
770 return createError(
"invalid zero number of BB ranges at offset " +
774 auto AddressOrErr = ExtractAddress();
776 return AddressOrErr.takeError();
777 RangeBaseAddress = *AddressOrErr;
778 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
780 std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
782 for (
uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
785 if (FeatEnable.MultiBBRange) {
786 auto AddressOrErr = ExtractAddress();
788 return AddressOrErr.takeError();
789 RangeBaseAddress = *AddressOrErr;
790 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
792 for (
uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr && Cur &&
793 (BlockIndex < NumBlocksInBBRange);
796 ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
798 uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
799 uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
800 uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
803 Offset += PrevBBEndOffset;
808 if (!MetadataOrErr) {
809 MetadataDecodeErr = MetadataOrErr.
takeError();
812 BBEntries.push_back({
ID,
Offset,
Size, *MetadataOrErr});
814 TotalNumBlocks += BBEntries.size();
815 BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
817 FunctionEntries.push_back({std::move(BBRangeEntries)});
819 if (PGOAnalyses || FeatEnable.hasPGOAnalysis()) {
822 FeatEnable.FuncEntryCount
823 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
826 std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
828 FeatEnable.hasPGOAnalysisBBData() && !MetadataDecodeErr &&
829 !ULEBSizeErr && Cur && (BlockIndex < TotalNumBlocks);
833 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
839 if (FeatEnable.BrProb) {
840 auto SuccCount = readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr);
842 uint32_t BBID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
843 uint32_t BrProb = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
850 PGOBBEntries.push_back({
BlockFrequency(BBF), std::move(Successors)});
854 PGOAnalyses->push_back(
860 if (!Cur || ULEBSizeErr || MetadataDecodeErr)
862 std::move(MetadataDecodeErr));
863 return FunctionEntries;
869 std::vector<PGOAnalysisMap> *PGOAnalyses)
const {
870 size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
873 if (!AddrMapsOrErr && PGOAnalyses)
874 PGOAnalyses->resize(OriginalPGOSize);
875 return std::move(AddrMapsOrErr);
887 if (!DoesSectionMatch) {
891 if (*DoesSectionMatch) {
892 if (SecToRelocMap.
insert(std::make_pair(&Sec, (
const Elf_Shdr *)
nullptr))
904 ": failed to get a relocated section: " +
908 const Elf_Shdr *ContentsSec = *RelSecOrErr;
910 if (!DoesRelTargetMatch) {
914 if (*DoesRelTargetMatch)
915 SecToRelocMap[ContentsSec] = &Sec;
918 return std::move(Errors);
919 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")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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)