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/BinaryFormat/ELF.h"
11 #include "llvm/Support/DataExtractor.h"
14 using namespace object;
16 #define STRINGIFY_ENUM_CASE(ns, name) \
20 #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
22 StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
27 #include "llvm/BinaryFormat/ELFRelocs/M68k.def"
34 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
42 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
49 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
56 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
63 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
68 case ELF::EM_ARC_COMPACT:
69 case ELF::EM_ARC_COMPACT2:
71 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
78 #include "llvm/BinaryFormat/ELFRelocs/AVR.def"
85 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
92 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
99 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
106 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
113 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
120 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
126 case ELF::EM_SPARC32PLUS:
127 case ELF::EM_SPARCV9:
129 #include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
136 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
143 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
150 #include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
157 #include "llvm/BinaryFormat/ELFRelocs/VE.def"
164 #include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
177 uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
180 return ELF::R_X86_64_RELATIVE;
183 return ELF::R_386_RELATIVE;
186 case ELF::EM_AARCH64:
187 return ELF::R_AARCH64_RELATIVE;
189 return ELF::R_ARM_RELATIVE;
190 case ELF::EM_ARC_COMPACT:
191 case ELF::EM_ARC_COMPACT2:
192 return ELF::R_ARC_RELATIVE;
195 case ELF::EM_HEXAGON:
196 return ELF::R_HEX_RELATIVE;
202 return ELF::R_PPC64_RELATIVE;
204 return ELF::R_RISCV_RELATIVE;
206 return ELF::R_390_RELATIVE;
208 case ELF::EM_SPARC32PLUS:
209 case ELF::EM_SPARCV9:
210 return ELF::R_SPARC_RELATIVE;
212 return ELF::R_CKCORE_RELATIVE;
214 return ELF::R_VE_RELATIVE;
225 StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
229 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
230 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
231 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
232 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
233 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
236 case ELF::EM_HEXAGON:
237 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
240 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
243 case ELF::EM_MIPS_RS3_LE:
245 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
246 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
247 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
248 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
252 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_MSP430_ATTRIBUTES); }
255 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
262 STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
263 STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
264 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
265 STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
266 STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
267 STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
268 STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
269 STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
270 STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
271 STRINGIFY_ENUM_CASE(ELF, SHT_REL);
272 STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
273 STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
274 STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
275 STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
276 STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
277 STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
278 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
279 STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
280 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
281 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
282 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
283 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
284 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
285 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
286 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
287 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
288 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
289 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
290 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
291 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP);
292 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
293 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
294 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
295 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
296 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
302 template <class ELFT>
303 std::vector<typename ELFT::Rel>
304 ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
305 // This function decodes the contents of an SHT_RELR packed relocation
308 // Proposal for adding SHT_RELR sections to generic-abi is here:
309 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
311 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
312 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
314 // i.e. start with an address, followed by any number of bitmaps. The address
315 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
316 // relocations each, at subsequent offsets following the last address entry.
318 // The bitmap entries must have 1 in the least significant bit. The assumption
319 // here is that an address cannot have 1 in lsb. Odd addresses are not
322 // Excluding the least significant bit in the bitmap, each non-zero bit in
323 // the bitmap represents a relocation to be applied to a corresponding machine
324 // word that follows the base address word. The second least significant bit
325 // represents the machine word immediately following the initial address, and
326 // each bit that follows represents the next word, in linear order. As such,
327 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
328 // 63 relocations in a 64-bit object.
330 // This encoding has a couple of interesting properties:
331 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
332 // even means address, odd means bitmap.
333 // 2. Just a simple list of addresses is a valid encoding.
337 Rel.setType(getRelativeRelocationType(), false);
338 std::vector<Elf_Rel> Relocs;
340 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
341 using Addr = typename ELFT::uint;
344 for (Elf_Relr R : relrs) {
345 typename ELFT::uint Entry = R;
346 if ((Entry & 1) == 0) {
347 // Even entry: encodes the offset for next relocation.
348 Rel.r_offset = Entry;
349 Relocs.push_back(Rel);
350 // Set base offset for subsequent bitmap entries.
351 Base = Entry + sizeof(Addr);
353 // Odd entry: encodes bitmap for relocations starting at base.
354 for (Addr Offset = Base; (Entry >>= 1) != 0; Offset += sizeof(Addr))
355 if ((Entry & 1) != 0) {
356 Rel.r_offset = Offset;
357 Relocs.push_back(Rel);
359 Base += (CHAR_BIT * sizeof(Entry) - 1) * sizeof(Addr);
366 template <class ELFT>
367 Expected<std::vector<typename ELFT::Rela>>
368 ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
369 // This function reads relocations in Android's packed relocation format,
370 // which is based on SLEB128 and delta encoding.
371 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
373 return ContentsOrErr.takeError();
374 ArrayRef<uint8_t> Content = *ContentsOrErr;
375 if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' ||
376 Content[2] != 'S' || Content[3] != '2')
377 return createError("invalid packed relocation header");
378 DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
379 DataExtractor::Cursor Cur(/*Offset=*/4);
381 uint64_t NumRelocs = Data.getSLEB128(Cur);
382 uint64_t Offset = Data.getSLEB128(Cur);
386 return std::move(Cur.takeError());
388 std::vector<Elf_Rela> Relocs;
389 Relocs.reserve(NumRelocs);
391 uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
393 return std::move(Cur.takeError());
394 if (NumRelocsInGroup > NumRelocs)
395 return createError("relocation group unexpectedly large");
396 NumRelocs -= NumRelocsInGroup;
398 uint64_t GroupFlags = Data.getSLEB128(Cur);
399 bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
400 bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
401 bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
402 bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
404 uint64_t GroupOffsetDelta;
405 if (GroupedByOffsetDelta)
406 GroupOffsetDelta = Data.getSLEB128(Cur);
410 GroupRInfo = Data.getSLEB128(Cur);
412 if (GroupedByAddend && GroupHasAddend)
413 Addend += Data.getSLEB128(Cur);
418 for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) {
420 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
422 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
423 if (GroupHasAddend && !GroupedByAddend)
424 Addend += Data.getSLEB128(Cur);
429 return std::move(Cur.takeError());
435 template <class ELFT>
436 std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
437 uint64_t Type) const {
438 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
442 #define DYNAMIC_TAG(n, v)
444 case ELF::EM_AARCH64:
446 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
447 #include "llvm/BinaryFormat/DynamicTags.def"
448 #undef AARCH64_DYNAMIC_TAG
452 case ELF::EM_HEXAGON:
454 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
455 #include "llvm/BinaryFormat/DynamicTags.def"
456 #undef HEXAGON_DYNAMIC_TAG
462 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
463 #include "llvm/BinaryFormat/DynamicTags.def"
464 #undef MIPS_DYNAMIC_TAG
470 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
471 #include "llvm/BinaryFormat/DynamicTags.def"
472 #undef PPC_DYNAMIC_TAG
478 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
479 #include "llvm/BinaryFormat/DynamicTags.def"
480 #undef PPC64_DYNAMIC_TAG
486 #define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
487 #include "llvm/BinaryFormat/DynamicTags.def"
488 #undef RISCV_DYNAMIC_TAG
494 // Now handle all dynamic tags except the architecture specific ones
495 #define AARCH64_DYNAMIC_TAG(name, value)
496 #define MIPS_DYNAMIC_TAG(name, value)
497 #define HEXAGON_DYNAMIC_TAG(name, value)
498 #define PPC_DYNAMIC_TAG(name, value)
499 #define PPC64_DYNAMIC_TAG(name, value)
500 #define RISCV_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"
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 RISCV_DYNAMIC_TAG
512 #undef DYNAMIC_TAG_MARKER
513 #undef DYNAMIC_STRINGIFY_ENUM
515 return "<unknown:>0x" + utohexstr(Type, true);
519 template <class ELFT>
520 std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
521 return getDynamicTagAsString(getHeader().e_machine, Type);
524 template <class ELFT>
525 Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
526 ArrayRef<Elf_Dyn> Dyn;
528 auto ProgramHeadersOrError = program_headers();
529 if (!ProgramHeadersOrError)
530 return ProgramHeadersOrError.takeError();
532 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
533 if (Phdr.p_type == ELF::PT_DYNAMIC) {
535 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
536 Phdr.p_filesz / sizeof(Elf_Dyn));
541 // If we can't find the dynamic section in the program headers, we just fall
542 // back on the sections.
544 auto SectionsOrError = sections();
545 if (!SectionsOrError)
546 return SectionsOrError.takeError();
548 for (const Elf_Shdr &Sec : *SectionsOrError) {
549 if (Sec.sh_type == ELF::SHT_DYNAMIC) {
550 Expected<ArrayRef<Elf_Dyn>> DynOrError =
551 getSectionContentsAsArray<Elf_Dyn>(Sec);
553 return DynOrError.takeError();
560 return ArrayRef<Elf_Dyn>();
564 // TODO: this error is untested.
565 return createError("invalid empty dynamic section");
567 if (Dyn.back().d_tag != ELF::DT_NULL)
568 // TODO: this error is untested.
569 return createError("dynamic sections must be DT_NULL terminated");
574 template <class ELFT>
575 Expected<const uint8_t *>
576 ELFFile<ELFT>::toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler) const {
577 auto ProgramHeadersOrError = program_headers();
578 if (!ProgramHeadersOrError)
579 return ProgramHeadersOrError.takeError();
581 llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
583 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
584 if (Phdr.p_type == ELF::PT_LOAD)
585 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
587 auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A,
588 const Elf_Phdr_Impl<ELFT> *B) {
589 return A->p_vaddr < B->p_vaddr;
591 if (!llvm::is_sorted(LoadSegments, SortPred)) {
593 WarnHandler("loadable segments are unsorted by virtual address"))
595 llvm::stable_sort(LoadSegments, SortPred);
598 const Elf_Phdr *const *I = llvm::upper_bound(
599 LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
600 return VAddr < Phdr->p_vaddr;
603 if (I == LoadSegments.begin())
604 return createError("virtual address is not in any segment: 0x" +
605 Twine::utohexstr(VAddr));
607 const Elf_Phdr &Phdr = **I;
608 uint64_t Delta = VAddr - Phdr.p_vaddr;
609 if (Delta >= Phdr.p_filesz)
610 return createError("virtual address is not in any segment: 0x" +
611 Twine::utohexstr(VAddr));
613 uint64_t Offset = Phdr.p_offset + Delta;
614 if (Offset >= getBufSize())
615 return createError("can't map virtual address 0x" +
616 Twine::utohexstr(VAddr) + " to the segment with index " +
617 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
618 ": the segment ends at 0x" +
619 Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
620 ", which is greater than the file size (0x" +
621 Twine::utohexstr(getBufSize()) + ")");
623 return base() + Offset;
626 template <class ELFT>
627 Expected<std::vector<BBAddrMap>>
628 ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec) const {
629 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
631 return ContentsOrErr.takeError();
632 ArrayRef<uint8_t> Content = *ContentsOrErr;
633 DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
634 std::vector<BBAddrMap> FunctionEntries;
636 DataExtractor::Cursor Cur(0);
637 Error ULEBSizeErr = Error::success();
639 // Helper to extract and decode the next ULEB128 value as uint32_t.
640 // Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the uint32_t
642 // Also returns zero if ULEBSizeErr is already in an error state.
643 auto ReadULEB128AsUInt32 = [&Data, &Cur, &ULEBSizeErr]() -> uint32_t {
644 // Bail out and do not extract data if ULEBSizeErr is already set.
647 uint64_t Offset = Cur.tell();
648 uint64_t Value = Data.getULEB128(Cur);
649 if (Value > UINT32_MAX) {
650 ULEBSizeErr = createError(
651 "ULEB128 value at offset 0x" + Twine::utohexstr(Offset) +
652 " exceeds UINT32_MAX (0x" + Twine::utohexstr(Value) + ")");
655 return static_cast<uint32_t>(Value);
658 while (!ULEBSizeErr && Cur && Cur.tell() < Content.size()) {
659 uintX_t Address = static_cast<uintX_t>(Data.getAddress(Cur));
660 uint32_t NumBlocks = ReadULEB128AsUInt32();
661 std::vector<BBAddrMap::BBEntry> BBEntries;
662 for (uint32_t BlockID = 0; !ULEBSizeErr && Cur && (BlockID < NumBlocks);
664 uint32_t Offset = ReadULEB128AsUInt32();
665 uint32_t Size = ReadULEB128AsUInt32();
666 uint32_t Metadata = ReadULEB128AsUInt32();
667 BBEntries.push_back({Offset, Size, Metadata});
669 FunctionEntries.push_back({Address, BBEntries});
671 // Either Cur is in the error state, or ULEBSizeError is set (not both), but
672 // we join the two errors here to be safe.
673 if (!Cur || ULEBSizeErr)
674 return joinErrors(Cur.takeError(), std::move(ULEBSizeErr));
675 return FunctionEntries;
678 template class llvm::object::ELFFile<ELF32LE>;
679 template class llvm::object::ELFFile<ELF32BE>;
680 template class llvm::object::ELFFile<ELF64LE>;
681 template class llvm::object::ELFFile<ELF64BE>;