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//===----------------------------------------------------------------------===// 18#define STRINGIFY_ENUM_CASE(ns, name) \ 22#define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name) 29#include "llvm/BinaryFormat/ELFRelocs/M68k.def" 36#include "llvm/BinaryFormat/ELFRelocs/x86_64.def" 44#include "llvm/BinaryFormat/ELFRelocs/i386.def" 51#include "llvm/BinaryFormat/ELFRelocs/Mips.def" 58#include "llvm/BinaryFormat/ELFRelocs/AArch64.def" 65#include "llvm/BinaryFormat/ELFRelocs/ARM.def" 73#include "llvm/BinaryFormat/ELFRelocs/ARC.def" 80#include "llvm/BinaryFormat/ELFRelocs/AVR.def" 87#include "llvm/BinaryFormat/ELFRelocs/Hexagon.def" 94#include "llvm/BinaryFormat/ELFRelocs/Lanai.def" 101#include "llvm/BinaryFormat/ELFRelocs/PowerPC.def" 108#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def" 115#include "llvm/BinaryFormat/ELFRelocs/RISCV.def" 122#include "llvm/BinaryFormat/ELFRelocs/SystemZ.def" 131#include "llvm/BinaryFormat/ELFRelocs/Sparc.def" 138#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def" 145#include "llvm/BinaryFormat/ELFRelocs/BPF.def" 152#include "llvm/BinaryFormat/ELFRelocs/MSP430.def" 159#include "llvm/BinaryFormat/ELFRelocs/VE.def" 166#include "llvm/BinaryFormat/ELFRelocs/CSKY.def" 173#include "llvm/BinaryFormat/ELFRelocs/LoongArch.def" 180#include "llvm/BinaryFormat/ELFRelocs/Xtensa.def" 196return ELF::R_X86_64_RELATIVE;
199return ELF::R_386_RELATIVE;
203return ELF::R_AARCH64_RELATIVE;
205return ELF::R_ARM_RELATIVE;
208return ELF::R_ARC_RELATIVE;
212return ELF::R_HEX_RELATIVE;
218return ELF::R_PPC64_RELATIVE;
220return ELF::R_RISCV_RELATIVE;
222return ELF::R_390_RELATIVE;
226return ELF::R_SPARC_RELATIVE;
228return ELF::R_CKCORE_RELATIVE;
230return ELF::R_VE_RELATIVE;
236return ELF::R_LARCH_RELATIVE;
335std::vector<typename ELFT::Rel>
337// This function decodes the contents of an SHT_RELR packed relocation 340// Proposal for adding SHT_RELR sections to generic-abi is here: 341// https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg 343// The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks 344// like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ] 346// i.e. start with an address, followed by any number of bitmaps. The address 347// entry encodes 1 relocation. The subsequent bitmap entries encode up to 63 348// relocations each, at subsequent offsets following the last address entry. 350// The bitmap entries must have 1 in the least significant bit. The assumption 351// here is that an address cannot have 1 in lsb. Odd addresses are not 354// Excluding the least significant bit in the bitmap, each non-zero bit in 355// the bitmap represents a relocation to be applied to a corresponding machine 356// word that follows the base address word. The second least significant bit 357// represents the machine word immediately following the initial address, and 358// each bit that follows represents the next word, in linear order. As such, 359// a single bitmap can encode up to 31 relocations in a 32-bit object, and 360// 63 relocations in a 64-bit object. 362// This encoding has a couple of interesting properties: 363// 1. Looking at any entry, it is clear whether it's an address or a bitmap: 364// even means address, odd means bitmap. 365// 2. Just a simple list of addresses is a valid encoding. 369 Rel.setType(getRelativeRelocationType(),
false);
370 std::vector<Elf_Rel> Relocs;
372// Word type: uint32_t for Elf32, and uint64_t for Elf64. 373usingAddr =
typename ELFT::uint;
376for (Elf_Relr R : relrs) {
377typename ELFT::uint Entry = R;
378if ((Entry & 1) == 0) {
379// Even entry: encodes the offset for next relocation. 380 Rel.r_offset = Entry;
381 Relocs.push_back(Rel);
382// Set base offset for subsequent bitmap entries. 385// Odd entry: encodes bitmap for relocations starting at base. 387if ((Entry & 1) != 0) {
389 Relocs.push_back(Rel);
391Base += (CHAR_BIT *
sizeof(Entry) - 1) *
sizeof(
Addr);
404 Hdr = Data.getULEB128(&Hdr, &Err);
413 std::vector<Elf_Rel> Rels;
414 std::vector<Elf_Rela> Relas;
417Error Err = object::decodeCrel<ELFT::Is64Bits>(
428 Relas[
I].r_offset = Crel.r_offset;
429 Relas[
I].setSymbolAndType(Crel.r_symidx, Crel.r_type,
false);
430 Relas[
I++].r_addend = Crel.r_addend;
432 Rels[
I].r_offset = Crel.r_offset;
433 Rels[
I++].setSymbolAndType(Crel.r_symidx, Crel.r_type,
false);
437return std::move(Err);
438return std::make_pair(std::move(Rels), std::move(Relas));
453// This function reads relocations in Android's packed relocation format, 454// which is based on SLEB128 and delta encoding. 461returncreateError(
"invalid packed relocation header");
465uint64_t NumRelocs = Data.getSLEB128(Cur);
472 std::vector<Elf_Rela> Relocs;
473 Relocs.reserve(NumRelocs);
475uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
478if (NumRelocsInGroup > NumRelocs)
479returncreateError(
"relocation group unexpectedly large");
480 NumRelocs -= NumRelocsInGroup;
482uint64_t GroupFlags = Data.getSLEB128(Cur);
489if (GroupedByOffsetDelta)
490 GroupOffsetDelta = Data.getSLEB128(Cur);
494 GroupRInfo = Data.getSLEB128(Cur);
496if (GroupedByAddend && GroupHasAddend)
497 Addend += Data.getSLEB128(Cur);
502for (
uint64_tI = 0; Cur &&
I != NumRelocsInGroup; ++
I) {
504Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
506 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
507if (GroupHasAddend && !GroupedByAddend)
508 Addend += Data.getSLEB128(Cur);
522#define DYNAMIC_STRINGIFY_ENUM(tag, value) \ 526#define DYNAMIC_TAG(n, v) 530#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 531#include "llvm/BinaryFormat/DynamicTags.def" 532#undef AARCH64_DYNAMIC_TAG 538#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 539#include "llvm/BinaryFormat/DynamicTags.def" 540#undef HEXAGON_DYNAMIC_TAG 546#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 547#include "llvm/BinaryFormat/DynamicTags.def" 548#undef MIPS_DYNAMIC_TAG 554#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 555#include "llvm/BinaryFormat/DynamicTags.def" 556#undef PPC_DYNAMIC_TAG 562#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 563#include "llvm/BinaryFormat/DynamicTags.def" 564#undef PPC64_DYNAMIC_TAG 570#define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 571#include "llvm/BinaryFormat/DynamicTags.def" 572#undef RISCV_DYNAMIC_TAG 578// Now handle all dynamic tags except the architecture specific ones 579#define AARCH64_DYNAMIC_TAG(name, value) 580#define MIPS_DYNAMIC_TAG(name, value) 581#define HEXAGON_DYNAMIC_TAG(name, value) 582#define PPC_DYNAMIC_TAG(name, value) 583#define PPC64_DYNAMIC_TAG(name, value) 584#define RISCV_DYNAMIC_TAG(name, value) 585// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. 586#define DYNAMIC_TAG_MARKER(name, value) 587#define DYNAMIC_TAG(name, value) case value: return #name; 588#include "llvm/BinaryFormat/DynamicTags.def" 590#undef AARCH64_DYNAMIC_TAG 591#undef MIPS_DYNAMIC_TAG 592#undef HEXAGON_DYNAMIC_TAG 593#undef PPC_DYNAMIC_TAG 594#undef PPC64_DYNAMIC_TAG 595#undef RISCV_DYNAMIC_TAG 596#undef DYNAMIC_TAG_MARKER 597#undef DYNAMIC_STRINGIFY_ENUM 599return"<unknown:>0x" + utohexstr(
Type,
true);
605return getDynamicTagAsString(getHeader().e_machine,
Type);
612auto ProgramHeadersOrError = program_headers();
613if (!ProgramHeadersOrError)
614return ProgramHeadersOrError.takeError();
616for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
618constuint8_t *DynOffset = base() + Phdr.p_offset;
619if (DynOffset > end())
621"dynamic section offset past file size: corrupted ELF");
622 Dyn =
ArrayRef(
reinterpret_cast<constElf_Dyn *
>(DynOffset),
623 Phdr.p_filesz /
sizeof(Elf_Dyn));
628// If we can't find the dynamic section in the program headers, we just fall 629// back on the sections. 633return SectionsOrError.takeError();
635for (
const Elf_Shdr &Sec : *SectionsOrError) {
638 getSectionContentsAsArray<Elf_Dyn>(Sec);
653if (Dyn.
back().d_tag != ELF::DT_NULL)
654returncreateError(
"dynamic sections must be DT_NULL terminated");
662auto ProgramHeadersOrError = program_headers();
663if (!ProgramHeadersOrError)
664return ProgramHeadersOrError.takeError();
668for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError)
670 LoadSegments.
push_back(
const_cast<Elf_Phdr *
>(&Phdr));
674returnA->p_vaddr <
B->p_vaddr;
678 WarnHandler(
"loadable segments are unsorted by virtual address"))
685return VAddr < Phdr->p_vaddr;
688if (
I == LoadSegments.
begin())
689returncreateError(
"virtual address is not in any segment: 0x" +
692const Elf_Phdr &Phdr = **
I;
693uint64_t Delta = VAddr - Phdr.p_vaddr;
694if (Delta >= Phdr.p_filesz)
695returncreateError(
"virtual address is not in any segment: 0x" +
699if (
Offset >= getBufSize())
702Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
703": the segment ends at 0x" +
705", which is greater than the file size (0x" +
711// Helper to extract and decode the next ULEB128 value as unsigned int. 712// Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the unsigned 714// Also returns zero if ULEBSizeErr is already in an error state. 715// ULEBSizeErr is an out variable if an error occurs. 716template <
typename IntTy, std::enable_if_t<std::is_
unsigned_v<IntTy>,
int> = 0>
719// Bail out and do not extract data if ULEBSizeErr is already set. 724if (
Value > std::numeric_limits<IntTy>::max()) {
725 ULEBSizeErr =
createError(
"ULEB128 value at offset 0x" +
727Twine(std::numeric_limits<IntTy>::digits) +
731returnstatic_cast<IntTy
>(
Value);
734template <
typename ELFT>
739 std::vector<PGOAnalysisMap> *PGOAnalyses) {
742// This DenseMap maps the offset of each function (the location of the 743// reference to the function in the SHT_LLVM_BB_ADDR_MAP section) to the 744// addend (the location of the function in the text section). 746if (IsRelocatable && RelaSec) {
748"Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable " 749"object file without providing a relocation section.");
752returncreateError(
"unable to read relocations for section " +
756 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
758auto GetAddressForRelocation =
761 FunctionOffsetTranslations.
find(RelocationOffsetInSection);
762if (FOTIterator == FunctionOffsetTranslations.
end()) {
763returncreateError(
"failed to get relocation data for offset: " +
767return FOTIterator->second;
774 std::vector<BBAddrMap> FunctionEntries;
780// Helper lampda to extract the (possiblly relocatable) address stored at Cur. 791 GetAddressForRelocation(RelocationOffsetInSection);
800while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
803 Version = Data.getU8(Cur);
807returncreateError(
"unsupported SHT_LLVM_BB_ADDR_MAP version: " +
808Twine(
static_cast<int>(Version)));
809 Feature = Data.getU8(Cur);
// Feature byte 814return FeatEnableOrErr.takeError();
815 FeatEnable = *FeatEnableOrErr;
816if (Feature != 0 && Version < 2 && Cur)
818"version should be >= 2 for SHT_LLVM_BB_ADDR_MAP when " 819"PGO features are enabled: version = " +
820Twine(
static_cast<int>(Version)) +
821" feature = " +
Twine(
static_cast<int>(Feature)));
826if (FeatEnable.MultiBBRange) {
827 NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
828if (!Cur || ULEBSizeErr)
831returncreateError(
"invalid zero number of BB ranges at offset " +
835auto AddressOrErr = ExtractAddress();
837return AddressOrErr.takeError();
838 RangeBaseAddress = *AddressOrErr;
839 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
841 std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
843for (
uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
846if (FeatEnable.MultiBBRange) {
847auto AddressOrErr = ExtractAddress();
849return AddressOrErr.takeError();
850 RangeBaseAddress = *AddressOrErr;
851 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
853 std::vector<BBAddrMap::BBEntry> BBEntries;
854if (!FeatEnable.OmitBBEntries) {
855for (
uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr &&
856 Cur && (BlockIndex < NumBlocksInBBRange);
859 ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
862uint32_tSize = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
863uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
865// Offset is calculated relative to the end of the previous BB. 872 MetadataDecodeErr = MetadataOrErr.
takeError();
877 TotalNumBlocks += BBEntries.size();
879 BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
881 FunctionEntries.push_back({std::move(BBRangeEntries)});
883if (PGOAnalyses || FeatEnable.hasPGOAnalysis()) {
884// Function entry count 886 FeatEnable.FuncEntryCount
887 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
890 std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
892 FeatEnable.hasPGOAnalysisBBData() && !MetadataDecodeErr &&
893 !ULEBSizeErr && Cur && (BlockIndex < TotalNumBlocks);
897 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
903if (FeatEnable.BrProb) {
904auto SuccCount = readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr);
906uint32_t BBID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
914 PGOBBEntries.push_back({
BlockFrequency(BBF), std::move(Successors)});
918 PGOAnalyses->push_back(
922// Either Cur is in the error state, or we have an error in ULEBSizeErr or 923// MetadataDecodeErr (but not both), but we join all errors here to be safe. 924if (!Cur || ULEBSizeErr || MetadataDecodeErr)
926 std::move(MetadataDecodeErr));
927return FunctionEntries;
933 std::vector<PGOAnalysisMap> *PGOAnalyses)
const{
934size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
936// remove new analyses when an error occurs 937if (!AddrMapsOrErr && PGOAnalyses)
938 PGOAnalyses->resize(OriginalPGOSize);
939return std::move(AddrMapsOrErr);
951if (!DoesSectionMatch) {
955if (*DoesSectionMatch) {
956if (SecToRelocMap.
insert(std::make_pair(&Sec, (
const Elf_Shdr *)
nullptr))
968": failed to get a relocated section: " +
972const Elf_Shdr *ContentsSec = *RelSecOrErr;
974if (!DoesRelTargetMatch) {
978if (*DoesRelTargetMatch)
979 SecToRelocMap[ContentsSec] = &Sec;
982return std::move(Errors);
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())
This file contains some functions that are useful when dealing with strings.
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)
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
uint64_t tell() const
Return the current position of this Cursor.
Error takeError()
Return error contained inside this Cursor, if any.
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< RelsOrRelas > decodeCrel(ArrayRef< uint8_t > Content) const
Expected< uint64_t > getCrelHeader(ArrayRef< uint8_t > Content) 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 ...
Expected< RelsOrRelas > crels(const Elf_Shdr &Sec) const
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
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)
static Error decodeCrel(ArrayRef< uint8_t > Content, function_ref< void(uint64_t, bool)> HdrHandler, function_ref< void(Elf_Crel_Impl< Is64 >)> EntryHandler)
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.
const char * toString(DWARFSectionKind Kind)
static Expected< Metadata > decode(uint32_t V)
static Expected< Features > decode(uint8_t Val)