10b57cec5SDimitry Andric //===- ELF.cpp - ELF object file implementation ---------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/Object/ELF.h" 1006c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h" 110b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h" 12fe6060f1SDimitry Andric #include "llvm/Support/DataExtractor.h" 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric using namespace llvm; 150b57cec5SDimitry Andric using namespace object; 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #define STRINGIFY_ENUM_CASE(ns, name) \ 180b57cec5SDimitry Andric case ns::name: \ 190b57cec5SDimitry Andric return #name; 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name) 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine, 240b57cec5SDimitry Andric uint32_t Type) { 250b57cec5SDimitry Andric switch (Machine) { 26fe6060f1SDimitry Andric case ELF::EM_68K: 27fe6060f1SDimitry Andric switch (Type) { 28fe6060f1SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/M68k.def" 29fe6060f1SDimitry Andric default: 30fe6060f1SDimitry Andric break; 31fe6060f1SDimitry Andric } 32fe6060f1SDimitry Andric break; 330b57cec5SDimitry Andric case ELF::EM_X86_64: 340b57cec5SDimitry Andric switch (Type) { 350b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/x86_64.def" 360b57cec5SDimitry Andric default: 370b57cec5SDimitry Andric break; 380b57cec5SDimitry Andric } 390b57cec5SDimitry Andric break; 400b57cec5SDimitry Andric case ELF::EM_386: 410b57cec5SDimitry Andric case ELF::EM_IAMCU: 420b57cec5SDimitry Andric switch (Type) { 430b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/i386.def" 440b57cec5SDimitry Andric default: 450b57cec5SDimitry Andric break; 460b57cec5SDimitry Andric } 470b57cec5SDimitry Andric break; 480b57cec5SDimitry Andric case ELF::EM_MIPS: 490b57cec5SDimitry Andric switch (Type) { 500b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/Mips.def" 510b57cec5SDimitry Andric default: 520b57cec5SDimitry Andric break; 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric break; 550b57cec5SDimitry Andric case ELF::EM_AARCH64: 560b57cec5SDimitry Andric switch (Type) { 570b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/AArch64.def" 580b57cec5SDimitry Andric default: 590b57cec5SDimitry Andric break; 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric break; 620b57cec5SDimitry Andric case ELF::EM_ARM: 630b57cec5SDimitry Andric switch (Type) { 640b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/ARM.def" 650b57cec5SDimitry Andric default: 660b57cec5SDimitry Andric break; 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric break; 690b57cec5SDimitry Andric case ELF::EM_ARC_COMPACT: 700b57cec5SDimitry Andric case ELF::EM_ARC_COMPACT2: 710b57cec5SDimitry Andric switch (Type) { 720b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/ARC.def" 730b57cec5SDimitry Andric default: 740b57cec5SDimitry Andric break; 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric break; 770b57cec5SDimitry Andric case ELF::EM_AVR: 780b57cec5SDimitry Andric switch (Type) { 790b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/AVR.def" 800b57cec5SDimitry Andric default: 810b57cec5SDimitry Andric break; 820b57cec5SDimitry Andric } 830b57cec5SDimitry Andric break; 840b57cec5SDimitry Andric case ELF::EM_HEXAGON: 850b57cec5SDimitry Andric switch (Type) { 860b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def" 870b57cec5SDimitry Andric default: 880b57cec5SDimitry Andric break; 890b57cec5SDimitry Andric } 900b57cec5SDimitry Andric break; 910b57cec5SDimitry Andric case ELF::EM_LANAI: 920b57cec5SDimitry Andric switch (Type) { 930b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/Lanai.def" 940b57cec5SDimitry Andric default: 950b57cec5SDimitry Andric break; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric break; 980b57cec5SDimitry Andric case ELF::EM_PPC: 990b57cec5SDimitry Andric switch (Type) { 1000b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def" 1010b57cec5SDimitry Andric default: 1020b57cec5SDimitry Andric break; 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric break; 1050b57cec5SDimitry Andric case ELF::EM_PPC64: 1060b57cec5SDimitry Andric switch (Type) { 1070b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def" 1080b57cec5SDimitry Andric default: 1090b57cec5SDimitry Andric break; 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric break; 1120b57cec5SDimitry Andric case ELF::EM_RISCV: 1130b57cec5SDimitry Andric switch (Type) { 1140b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/RISCV.def" 1150b57cec5SDimitry Andric default: 1160b57cec5SDimitry Andric break; 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric break; 1190b57cec5SDimitry Andric case ELF::EM_S390: 1200b57cec5SDimitry Andric switch (Type) { 1210b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def" 1220b57cec5SDimitry Andric default: 1230b57cec5SDimitry Andric break; 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric break; 1260b57cec5SDimitry Andric case ELF::EM_SPARC: 1270b57cec5SDimitry Andric case ELF::EM_SPARC32PLUS: 1280b57cec5SDimitry Andric case ELF::EM_SPARCV9: 1290b57cec5SDimitry Andric switch (Type) { 1300b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/Sparc.def" 1310b57cec5SDimitry Andric default: 1320b57cec5SDimitry Andric break; 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric break; 1350b57cec5SDimitry Andric case ELF::EM_AMDGPU: 1360b57cec5SDimitry Andric switch (Type) { 1370b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def" 1380b57cec5SDimitry Andric default: 1390b57cec5SDimitry Andric break; 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric break; 1420b57cec5SDimitry Andric case ELF::EM_BPF: 1430b57cec5SDimitry Andric switch (Type) { 1440b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/BPF.def" 1450b57cec5SDimitry Andric default: 1460b57cec5SDimitry Andric break; 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric break; 1490b57cec5SDimitry Andric case ELF::EM_MSP430: 1500b57cec5SDimitry Andric switch (Type) { 1510b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/MSP430.def" 1520b57cec5SDimitry Andric default: 1530b57cec5SDimitry Andric break; 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric break; 1565ffd83dbSDimitry Andric case ELF::EM_VE: 1575ffd83dbSDimitry Andric switch (Type) { 1585ffd83dbSDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/VE.def" 1595ffd83dbSDimitry Andric default: 1605ffd83dbSDimitry Andric break; 1615ffd83dbSDimitry Andric } 1625ffd83dbSDimitry Andric break; 163e8d8bef9SDimitry Andric case ELF::EM_CSKY: 164e8d8bef9SDimitry Andric switch (Type) { 165e8d8bef9SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/CSKY.def" 166e8d8bef9SDimitry Andric default: 167e8d8bef9SDimitry Andric break; 168e8d8bef9SDimitry Andric } 169e8d8bef9SDimitry Andric break; 17081ad6265SDimitry Andric case ELF::EM_LOONGARCH: 17181ad6265SDimitry Andric switch (Type) { 17281ad6265SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/LoongArch.def" 17381ad6265SDimitry Andric default: 17481ad6265SDimitry Andric break; 17581ad6265SDimitry Andric } 17681ad6265SDimitry Andric break; 177bdd1243dSDimitry Andric case ELF::EM_XTENSA: 178bdd1243dSDimitry Andric switch (Type) { 179bdd1243dSDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/Xtensa.def" 180bdd1243dSDimitry Andric default: 181bdd1243dSDimitry Andric break; 182bdd1243dSDimitry Andric } 183bdd1243dSDimitry Andric break; 1840b57cec5SDimitry Andric default: 1850b57cec5SDimitry Andric break; 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric return "Unknown"; 1880b57cec5SDimitry Andric } 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric #undef ELF_RELOC 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) { 1930b57cec5SDimitry Andric switch (Machine) { 1940b57cec5SDimitry Andric case ELF::EM_X86_64: 1950b57cec5SDimitry Andric return ELF::R_X86_64_RELATIVE; 1960b57cec5SDimitry Andric case ELF::EM_386: 1970b57cec5SDimitry Andric case ELF::EM_IAMCU: 1980b57cec5SDimitry Andric return ELF::R_386_RELATIVE; 1990b57cec5SDimitry Andric case ELF::EM_MIPS: 2000b57cec5SDimitry Andric break; 2010b57cec5SDimitry Andric case ELF::EM_AARCH64: 2020b57cec5SDimitry Andric return ELF::R_AARCH64_RELATIVE; 2030b57cec5SDimitry Andric case ELF::EM_ARM: 2040b57cec5SDimitry Andric return ELF::R_ARM_RELATIVE; 2050b57cec5SDimitry Andric case ELF::EM_ARC_COMPACT: 2060b57cec5SDimitry Andric case ELF::EM_ARC_COMPACT2: 2070b57cec5SDimitry Andric return ELF::R_ARC_RELATIVE; 2080b57cec5SDimitry Andric case ELF::EM_AVR: 2090b57cec5SDimitry Andric break; 2100b57cec5SDimitry Andric case ELF::EM_HEXAGON: 2110b57cec5SDimitry Andric return ELF::R_HEX_RELATIVE; 2120b57cec5SDimitry Andric case ELF::EM_LANAI: 2130b57cec5SDimitry Andric break; 2140b57cec5SDimitry Andric case ELF::EM_PPC: 2150b57cec5SDimitry Andric break; 2160b57cec5SDimitry Andric case ELF::EM_PPC64: 2170b57cec5SDimitry Andric return ELF::R_PPC64_RELATIVE; 2180b57cec5SDimitry Andric case ELF::EM_RISCV: 2190b57cec5SDimitry Andric return ELF::R_RISCV_RELATIVE; 2200b57cec5SDimitry Andric case ELF::EM_S390: 2210b57cec5SDimitry Andric return ELF::R_390_RELATIVE; 2220b57cec5SDimitry Andric case ELF::EM_SPARC: 2230b57cec5SDimitry Andric case ELF::EM_SPARC32PLUS: 2240b57cec5SDimitry Andric case ELF::EM_SPARCV9: 2250b57cec5SDimitry Andric return ELF::R_SPARC_RELATIVE; 226e8d8bef9SDimitry Andric case ELF::EM_CSKY: 227e8d8bef9SDimitry Andric return ELF::R_CKCORE_RELATIVE; 2280eae32dcSDimitry Andric case ELF::EM_VE: 2290eae32dcSDimitry Andric return ELF::R_VE_RELATIVE; 2300b57cec5SDimitry Andric case ELF::EM_AMDGPU: 2310b57cec5SDimitry Andric break; 2320b57cec5SDimitry Andric case ELF::EM_BPF: 2330b57cec5SDimitry Andric break; 234bdd1243dSDimitry Andric case ELF::EM_LOONGARCH: 235bdd1243dSDimitry Andric return ELF::R_LARCH_RELATIVE; 2360b57cec5SDimitry Andric default: 2370b57cec5SDimitry Andric break; 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric return 0; 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) { 2430b57cec5SDimitry Andric switch (Machine) { 2440b57cec5SDimitry Andric case ELF::EM_ARM: 2450b57cec5SDimitry Andric switch (Type) { 2460b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX); 2470b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP); 2480b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES); 2490b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY); 2500b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION); 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric break; 2530b57cec5SDimitry Andric case ELF::EM_HEXAGON: 2540b57cec5SDimitry Andric switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); } 2550b57cec5SDimitry Andric break; 2560b57cec5SDimitry Andric case ELF::EM_X86_64: 2570b57cec5SDimitry Andric switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); } 2580b57cec5SDimitry Andric break; 2590b57cec5SDimitry Andric case ELF::EM_MIPS: 2600b57cec5SDimitry Andric case ELF::EM_MIPS_RS3_LE: 2610b57cec5SDimitry Andric switch (Type) { 2620b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO); 2630b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS); 2640b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF); 2650b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS); 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric break; 268349cc55cSDimitry Andric case ELF::EM_MSP430: 269349cc55cSDimitry Andric switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_MSP430_ATTRIBUTES); } 270349cc55cSDimitry Andric break; 2715ffd83dbSDimitry Andric case ELF::EM_RISCV: 2725ffd83dbSDimitry Andric switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); } 2735ffd83dbSDimitry Andric break; 27406c3fb27SDimitry Andric case ELF::EM_AARCH64: 27506c3fb27SDimitry Andric switch (Type) { 2765f757f3fSDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_AUTH_RELR); 27706c3fb27SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC); 27806c3fb27SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_STATIC); 27906c3fb27SDimitry Andric } 2800b57cec5SDimitry Andric default: 2810b57cec5SDimitry Andric break; 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric switch (Type) { 2850b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_NULL); 2860b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS); 2870b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB); 2880b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB); 2890b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_RELA); 2900b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_HASH); 2910b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC); 2920b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_NOTE); 2930b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS); 2940b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_REL); 2950b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB); 2960b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM); 2970b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY); 2980b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY); 2990b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY); 3000b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GROUP); 3010b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX); 3020b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_RELR); 3030b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL); 3040b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA); 3050b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR); 3060b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB); 3070b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS); 3080b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE); 3090b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG); 3100b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES); 3110b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART); 3128bcb0991SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR); 3138bcb0991SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR); 31481ad6265SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP_V0); 315e8d8bef9SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP); 316753f127fSDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_OFFLOADING); 31706c3fb27SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LTO); 3180b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES); 3190b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH); 3200b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef); 3210b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed); 3220b57cec5SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym); 3230b57cec5SDimitry Andric default: 3240b57cec5SDimitry Andric return "Unknown"; 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric template <class ELFT> 329e8d8bef9SDimitry Andric std::vector<typename ELFT::Rel> 3300b57cec5SDimitry Andric ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const { 3310b57cec5SDimitry Andric // This function decodes the contents of an SHT_RELR packed relocation 3320b57cec5SDimitry Andric // section. 3330b57cec5SDimitry Andric // 3340b57cec5SDimitry Andric // Proposal for adding SHT_RELR sections to generic-abi is here: 3350b57cec5SDimitry Andric // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg 3360b57cec5SDimitry Andric // 3370b57cec5SDimitry Andric // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks 3380b57cec5SDimitry Andric // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ] 3390b57cec5SDimitry Andric // 3400b57cec5SDimitry Andric // i.e. start with an address, followed by any number of bitmaps. The address 3410b57cec5SDimitry Andric // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63 3420b57cec5SDimitry Andric // relocations each, at subsequent offsets following the last address entry. 3430b57cec5SDimitry Andric // 3440b57cec5SDimitry Andric // The bitmap entries must have 1 in the least significant bit. The assumption 3450b57cec5SDimitry Andric // here is that an address cannot have 1 in lsb. Odd addresses are not 3460b57cec5SDimitry Andric // supported. 3470b57cec5SDimitry Andric // 3480b57cec5SDimitry Andric // Excluding the least significant bit in the bitmap, each non-zero bit in 3490b57cec5SDimitry Andric // the bitmap represents a relocation to be applied to a corresponding machine 3500b57cec5SDimitry Andric // word that follows the base address word. The second least significant bit 3510b57cec5SDimitry Andric // represents the machine word immediately following the initial address, and 3520b57cec5SDimitry Andric // each bit that follows represents the next word, in linear order. As such, 3530b57cec5SDimitry Andric // a single bitmap can encode up to 31 relocations in a 32-bit object, and 3540b57cec5SDimitry Andric // 63 relocations in a 64-bit object. 3550b57cec5SDimitry Andric // 3560b57cec5SDimitry Andric // This encoding has a couple of interesting properties: 3570b57cec5SDimitry Andric // 1. Looking at any entry, it is clear whether it's an address or a bitmap: 3580b57cec5SDimitry Andric // even means address, odd means bitmap. 3590b57cec5SDimitry Andric // 2. Just a simple list of addresses is a valid encoding. 3600b57cec5SDimitry Andric 361e8d8bef9SDimitry Andric Elf_Rel Rel; 362e8d8bef9SDimitry Andric Rel.r_info = 0; 363e8d8bef9SDimitry Andric Rel.setType(getRelativeRelocationType(), false); 364e8d8bef9SDimitry Andric std::vector<Elf_Rel> Relocs; 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric // Word type: uint32_t for Elf32, and uint64_t for Elf64. 367349cc55cSDimitry Andric using Addr = typename ELFT::uint; 3680b57cec5SDimitry Andric 369349cc55cSDimitry Andric Addr Base = 0; 370349cc55cSDimitry Andric for (Elf_Relr R : relrs) { 371349cc55cSDimitry Andric typename ELFT::uint Entry = R; 3720b57cec5SDimitry Andric if ((Entry & 1) == 0) { 3730b57cec5SDimitry Andric // Even entry: encodes the offset for next relocation. 374e8d8bef9SDimitry Andric Rel.r_offset = Entry; 375e8d8bef9SDimitry Andric Relocs.push_back(Rel); 3760b57cec5SDimitry Andric // Set base offset for subsequent bitmap entries. 377349cc55cSDimitry Andric Base = Entry + sizeof(Addr); 378349cc55cSDimitry Andric } else { 3790b57cec5SDimitry Andric // Odd entry: encodes bitmap for relocations starting at base. 380349cc55cSDimitry Andric for (Addr Offset = Base; (Entry >>= 1) != 0; Offset += sizeof(Addr)) 3810b57cec5SDimitry Andric if ((Entry & 1) != 0) { 382e8d8bef9SDimitry Andric Rel.r_offset = Offset; 383e8d8bef9SDimitry Andric Relocs.push_back(Rel); 3840b57cec5SDimitry Andric } 385349cc55cSDimitry Andric Base += (CHAR_BIT * sizeof(Entry) - 1) * sizeof(Addr); 3860b57cec5SDimitry Andric } 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric return Relocs; 3900b57cec5SDimitry Andric } 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric template <class ELFT> 3930b57cec5SDimitry Andric Expected<std::vector<typename ELFT::Rela>> 394e8d8bef9SDimitry Andric ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const { 3950b57cec5SDimitry Andric // This function reads relocations in Android's packed relocation format, 3960b57cec5SDimitry Andric // which is based on SLEB128 and delta encoding. 3970b57cec5SDimitry Andric Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec); 3980b57cec5SDimitry Andric if (!ContentsOrErr) 3990b57cec5SDimitry Andric return ContentsOrErr.takeError(); 400fe6060f1SDimitry Andric ArrayRef<uint8_t> Content = *ContentsOrErr; 401fe6060f1SDimitry Andric if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' || 402fe6060f1SDimitry Andric Content[2] != 'S' || Content[3] != '2') 4030b57cec5SDimitry Andric return createError("invalid packed relocation header"); 404fe6060f1SDimitry Andric DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4); 405fe6060f1SDimitry Andric DataExtractor::Cursor Cur(/*Offset=*/4); 4060b57cec5SDimitry Andric 407fe6060f1SDimitry Andric uint64_t NumRelocs = Data.getSLEB128(Cur); 408fe6060f1SDimitry Andric uint64_t Offset = Data.getSLEB128(Cur); 4090b57cec5SDimitry Andric uint64_t Addend = 0; 4100b57cec5SDimitry Andric 411fe6060f1SDimitry Andric if (!Cur) 412fe6060f1SDimitry Andric return std::move(Cur.takeError()); 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric std::vector<Elf_Rela> Relocs; 4150b57cec5SDimitry Andric Relocs.reserve(NumRelocs); 4160b57cec5SDimitry Andric while (NumRelocs) { 417fe6060f1SDimitry Andric uint64_t NumRelocsInGroup = Data.getSLEB128(Cur); 418fe6060f1SDimitry Andric if (!Cur) 419fe6060f1SDimitry Andric return std::move(Cur.takeError()); 4200b57cec5SDimitry Andric if (NumRelocsInGroup > NumRelocs) 4210b57cec5SDimitry Andric return createError("relocation group unexpectedly large"); 4220b57cec5SDimitry Andric NumRelocs -= NumRelocsInGroup; 4230b57cec5SDimitry Andric 424fe6060f1SDimitry Andric uint64_t GroupFlags = Data.getSLEB128(Cur); 4250b57cec5SDimitry Andric bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG; 4260b57cec5SDimitry Andric bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG; 4270b57cec5SDimitry Andric bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG; 4280b57cec5SDimitry Andric bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG; 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric uint64_t GroupOffsetDelta; 4310b57cec5SDimitry Andric if (GroupedByOffsetDelta) 432fe6060f1SDimitry Andric GroupOffsetDelta = Data.getSLEB128(Cur); 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric uint64_t GroupRInfo; 4350b57cec5SDimitry Andric if (GroupedByInfo) 436fe6060f1SDimitry Andric GroupRInfo = Data.getSLEB128(Cur); 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric if (GroupedByAddend && GroupHasAddend) 439fe6060f1SDimitry Andric Addend += Data.getSLEB128(Cur); 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric if (!GroupHasAddend) 4420b57cec5SDimitry Andric Addend = 0; 4430b57cec5SDimitry Andric 444fe6060f1SDimitry Andric for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) { 4450b57cec5SDimitry Andric Elf_Rela R; 446fe6060f1SDimitry Andric Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur); 4470b57cec5SDimitry Andric R.r_offset = Offset; 448fe6060f1SDimitry Andric R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur); 4490b57cec5SDimitry Andric if (GroupHasAddend && !GroupedByAddend) 450fe6060f1SDimitry Andric Addend += Data.getSLEB128(Cur); 4510b57cec5SDimitry Andric R.r_addend = Addend; 4520b57cec5SDimitry Andric Relocs.push_back(R); 4530b57cec5SDimitry Andric } 454fe6060f1SDimitry Andric if (!Cur) 455fe6060f1SDimitry Andric return std::move(Cur.takeError()); 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric return Relocs; 4590b57cec5SDimitry Andric } 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric template <class ELFT> 4620b57cec5SDimitry Andric std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch, 4630b57cec5SDimitry Andric uint64_t Type) const { 4640b57cec5SDimitry Andric #define DYNAMIC_STRINGIFY_ENUM(tag, value) \ 4650b57cec5SDimitry Andric case value: \ 4660b57cec5SDimitry Andric return #tag; 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric #define DYNAMIC_TAG(n, v) 4690b57cec5SDimitry Andric switch (Arch) { 4700b57cec5SDimitry Andric case ELF::EM_AARCH64: 4710b57cec5SDimitry Andric switch (Type) { 4720b57cec5SDimitry Andric #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 4730b57cec5SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def" 4740b57cec5SDimitry Andric #undef AARCH64_DYNAMIC_TAG 4750b57cec5SDimitry Andric } 4760b57cec5SDimitry Andric break; 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric case ELF::EM_HEXAGON: 4790b57cec5SDimitry Andric switch (Type) { 4800b57cec5SDimitry Andric #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 4810b57cec5SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def" 4820b57cec5SDimitry Andric #undef HEXAGON_DYNAMIC_TAG 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric break; 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric case ELF::EM_MIPS: 4870b57cec5SDimitry Andric switch (Type) { 4880b57cec5SDimitry Andric #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 4890b57cec5SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def" 4900b57cec5SDimitry Andric #undef MIPS_DYNAMIC_TAG 4910b57cec5SDimitry Andric } 4920b57cec5SDimitry Andric break; 4930b57cec5SDimitry Andric 494349cc55cSDimitry Andric case ELF::EM_PPC: 495349cc55cSDimitry Andric switch (Type) { 496349cc55cSDimitry Andric #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 497349cc55cSDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def" 498349cc55cSDimitry Andric #undef PPC_DYNAMIC_TAG 499349cc55cSDimitry Andric } 500349cc55cSDimitry Andric break; 501349cc55cSDimitry Andric 5020b57cec5SDimitry Andric case ELF::EM_PPC64: 5030b57cec5SDimitry Andric switch (Type) { 5040b57cec5SDimitry Andric #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 5050b57cec5SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def" 5060b57cec5SDimitry Andric #undef PPC64_DYNAMIC_TAG 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric break; 509349cc55cSDimitry Andric 510349cc55cSDimitry Andric case ELF::EM_RISCV: 511349cc55cSDimitry Andric switch (Type) { 512349cc55cSDimitry Andric #define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 513349cc55cSDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def" 514349cc55cSDimitry Andric #undef RISCV_DYNAMIC_TAG 515349cc55cSDimitry Andric } 516349cc55cSDimitry Andric break; 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric #undef DYNAMIC_TAG 5190b57cec5SDimitry Andric switch (Type) { 5200b57cec5SDimitry Andric // Now handle all dynamic tags except the architecture specific ones 5210b57cec5SDimitry Andric #define AARCH64_DYNAMIC_TAG(name, value) 5220b57cec5SDimitry Andric #define MIPS_DYNAMIC_TAG(name, value) 5230b57cec5SDimitry Andric #define HEXAGON_DYNAMIC_TAG(name, value) 524349cc55cSDimitry Andric #define PPC_DYNAMIC_TAG(name, value) 5250b57cec5SDimitry Andric #define PPC64_DYNAMIC_TAG(name, value) 526349cc55cSDimitry Andric #define RISCV_DYNAMIC_TAG(name, value) 5270b57cec5SDimitry Andric // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. 5280b57cec5SDimitry Andric #define DYNAMIC_TAG_MARKER(name, value) 529480093f4SDimitry Andric #define DYNAMIC_TAG(name, value) case value: return #name; 5300b57cec5SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def" 5310b57cec5SDimitry Andric #undef DYNAMIC_TAG 5320b57cec5SDimitry Andric #undef AARCH64_DYNAMIC_TAG 5330b57cec5SDimitry Andric #undef MIPS_DYNAMIC_TAG 5340b57cec5SDimitry Andric #undef HEXAGON_DYNAMIC_TAG 535349cc55cSDimitry Andric #undef PPC_DYNAMIC_TAG 5360b57cec5SDimitry Andric #undef PPC64_DYNAMIC_TAG 537349cc55cSDimitry Andric #undef RISCV_DYNAMIC_TAG 5380b57cec5SDimitry Andric #undef DYNAMIC_TAG_MARKER 5390b57cec5SDimitry Andric #undef DYNAMIC_STRINGIFY_ENUM 5400b57cec5SDimitry Andric default: 5410b57cec5SDimitry Andric return "<unknown:>0x" + utohexstr(Type, true); 5420b57cec5SDimitry Andric } 5430b57cec5SDimitry Andric } 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric template <class ELFT> 5460b57cec5SDimitry Andric std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const { 547e8d8bef9SDimitry Andric return getDynamicTagAsString(getHeader().e_machine, Type); 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric template <class ELFT> 5510b57cec5SDimitry Andric Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const { 5520b57cec5SDimitry Andric ArrayRef<Elf_Dyn> Dyn; 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric auto ProgramHeadersOrError = program_headers(); 5550b57cec5SDimitry Andric if (!ProgramHeadersOrError) 5560b57cec5SDimitry Andric return ProgramHeadersOrError.takeError(); 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) { 5590b57cec5SDimitry Andric if (Phdr.p_type == ELF::PT_DYNAMIC) { 560bdd1243dSDimitry Andric Dyn = ArrayRef(reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset), 5610b57cec5SDimitry Andric Phdr.p_filesz / sizeof(Elf_Dyn)); 5620b57cec5SDimitry Andric break; 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric } 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric // If we can't find the dynamic section in the program headers, we just fall 5670b57cec5SDimitry Andric // back on the sections. 5680b57cec5SDimitry Andric if (Dyn.empty()) { 5690b57cec5SDimitry Andric auto SectionsOrError = sections(); 5700b57cec5SDimitry Andric if (!SectionsOrError) 5710b57cec5SDimitry Andric return SectionsOrError.takeError(); 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric for (const Elf_Shdr &Sec : *SectionsOrError) { 5740b57cec5SDimitry Andric if (Sec.sh_type == ELF::SHT_DYNAMIC) { 5750b57cec5SDimitry Andric Expected<ArrayRef<Elf_Dyn>> DynOrError = 576e8d8bef9SDimitry Andric getSectionContentsAsArray<Elf_Dyn>(Sec); 5770b57cec5SDimitry Andric if (!DynOrError) 5780b57cec5SDimitry Andric return DynOrError.takeError(); 5790b57cec5SDimitry Andric Dyn = *DynOrError; 5800b57cec5SDimitry Andric break; 5810b57cec5SDimitry Andric } 5820b57cec5SDimitry Andric } 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andric if (!Dyn.data()) 5850b57cec5SDimitry Andric return ArrayRef<Elf_Dyn>(); 5860b57cec5SDimitry Andric } 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric if (Dyn.empty()) 5890b57cec5SDimitry Andric return createError("invalid empty dynamic section"); 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric if (Dyn.back().d_tag != ELF::DT_NULL) 5920b57cec5SDimitry Andric return createError("dynamic sections must be DT_NULL terminated"); 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric return Dyn; 5950b57cec5SDimitry Andric } 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric template <class ELFT> 598e8d8bef9SDimitry Andric Expected<const uint8_t *> 599e8d8bef9SDimitry Andric ELFFile<ELFT>::toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler) const { 6000b57cec5SDimitry Andric auto ProgramHeadersOrError = program_headers(); 6010b57cec5SDimitry Andric if (!ProgramHeadersOrError) 6020b57cec5SDimitry Andric return ProgramHeadersOrError.takeError(); 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric llvm::SmallVector<Elf_Phdr *, 4> LoadSegments; 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) 6070b57cec5SDimitry Andric if (Phdr.p_type == ELF::PT_LOAD) 6080b57cec5SDimitry Andric LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr)); 6090b57cec5SDimitry Andric 610e8d8bef9SDimitry Andric auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A, 611e8d8bef9SDimitry Andric const Elf_Phdr_Impl<ELFT> *B) { 612e8d8bef9SDimitry Andric return A->p_vaddr < B->p_vaddr; 613e8d8bef9SDimitry Andric }; 614e8d8bef9SDimitry Andric if (!llvm::is_sorted(LoadSegments, SortPred)) { 615e8d8bef9SDimitry Andric if (Error E = 616e8d8bef9SDimitry Andric WarnHandler("loadable segments are unsorted by virtual address")) 617e8d8bef9SDimitry Andric return std::move(E); 618e8d8bef9SDimitry Andric llvm::stable_sort(LoadSegments, SortPred); 619e8d8bef9SDimitry Andric } 620e8d8bef9SDimitry Andric 621e8d8bef9SDimitry Andric const Elf_Phdr *const *I = llvm::upper_bound( 622e8d8bef9SDimitry Andric LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) { 6230b57cec5SDimitry Andric return VAddr < Phdr->p_vaddr; 6240b57cec5SDimitry Andric }); 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric if (I == LoadSegments.begin()) 6270b57cec5SDimitry Andric return createError("virtual address is not in any segment: 0x" + 6280b57cec5SDimitry Andric Twine::utohexstr(VAddr)); 6290b57cec5SDimitry Andric --I; 6300b57cec5SDimitry Andric const Elf_Phdr &Phdr = **I; 6310b57cec5SDimitry Andric uint64_t Delta = VAddr - Phdr.p_vaddr; 6320b57cec5SDimitry Andric if (Delta >= Phdr.p_filesz) 6330b57cec5SDimitry Andric return createError("virtual address is not in any segment: 0x" + 6340b57cec5SDimitry Andric Twine::utohexstr(VAddr)); 6355ffd83dbSDimitry Andric 6365ffd83dbSDimitry Andric uint64_t Offset = Phdr.p_offset + Delta; 6375ffd83dbSDimitry Andric if (Offset >= getBufSize()) 6385ffd83dbSDimitry Andric return createError("can't map virtual address 0x" + 6395ffd83dbSDimitry Andric Twine::utohexstr(VAddr) + " to the segment with index " + 6405ffd83dbSDimitry Andric Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) + 6415ffd83dbSDimitry Andric ": the segment ends at 0x" + 6425ffd83dbSDimitry Andric Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) + 6435ffd83dbSDimitry Andric ", which is greater than the file size (0x" + 6445ffd83dbSDimitry Andric Twine::utohexstr(getBufSize()) + ")"); 6455ffd83dbSDimitry Andric 6465ffd83dbSDimitry Andric return base() + Offset; 6470b57cec5SDimitry Andric } 6480b57cec5SDimitry Andric 6495f757f3fSDimitry Andric // Helper to extract and decode the next ULEB128 value as unsigned int. 6505f757f3fSDimitry Andric // Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the unsigned 6515f757f3fSDimitry Andric // int limit. 6525f757f3fSDimitry Andric // Also returns zero if ULEBSizeErr is already in an error state. 6535f757f3fSDimitry Andric // ULEBSizeErr is an out variable if an error occurs. 6545f757f3fSDimitry Andric template <typename IntTy, std::enable_if_t<std::is_unsigned_v<IntTy>, int> = 0> 6555f757f3fSDimitry Andric static IntTy readULEB128As(DataExtractor &Data, DataExtractor::Cursor &Cur, 6565f757f3fSDimitry Andric Error &ULEBSizeErr) { 6575f757f3fSDimitry Andric // Bail out and do not extract data if ULEBSizeErr is already set. 6585f757f3fSDimitry Andric if (ULEBSizeErr) 6595f757f3fSDimitry Andric return 0; 6605f757f3fSDimitry Andric uint64_t Offset = Cur.tell(); 6615f757f3fSDimitry Andric uint64_t Value = Data.getULEB128(Cur); 6625f757f3fSDimitry Andric if (Value > std::numeric_limits<IntTy>::max()) { 6635f757f3fSDimitry Andric ULEBSizeErr = createError("ULEB128 value at offset 0x" + 6645f757f3fSDimitry Andric Twine::utohexstr(Offset) + " exceeds UINT" + 6655f757f3fSDimitry Andric Twine(std::numeric_limits<IntTy>::digits) + 6665f757f3fSDimitry Andric "_MAX (0x" + Twine::utohexstr(Value) + ")"); 6675f757f3fSDimitry Andric return 0; 6685f757f3fSDimitry Andric } 6695f757f3fSDimitry Andric return static_cast<IntTy>(Value); 6705f757f3fSDimitry Andric } 6715f757f3fSDimitry Andric 6725f757f3fSDimitry Andric template <typename ELFT> 6735f757f3fSDimitry Andric static Expected<std::vector<BBAddrMap>> 6745f757f3fSDimitry Andric decodeBBAddrMapImpl(const ELFFile<ELFT> &EF, 6755f757f3fSDimitry Andric const typename ELFFile<ELFT>::Elf_Shdr &Sec, 6765f757f3fSDimitry Andric const typename ELFFile<ELFT>::Elf_Shdr *RelaSec, 6775f757f3fSDimitry Andric std::vector<PGOAnalysisMap> *PGOAnalyses) { 6785f757f3fSDimitry Andric bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL; 67906c3fb27SDimitry Andric 68006c3fb27SDimitry Andric // This DenseMap maps the offset of each function (the location of the 68106c3fb27SDimitry Andric // reference to the function in the SHT_LLVM_BB_ADDR_MAP section) to the 68206c3fb27SDimitry Andric // addend (the location of the function in the text section). 68306c3fb27SDimitry Andric llvm::DenseMap<uint64_t, uint64_t> FunctionOffsetTranslations; 68406c3fb27SDimitry Andric if (IsRelocatable && RelaSec) { 68506c3fb27SDimitry Andric assert(RelaSec && 68606c3fb27SDimitry Andric "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable " 68706c3fb27SDimitry Andric "object file without providing a relocation section."); 6885f757f3fSDimitry Andric Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas = EF.relas(*RelaSec); 68906c3fb27SDimitry Andric if (!Relas) 69006c3fb27SDimitry Andric return createError("unable to read relocations for section " + 6915f757f3fSDimitry Andric describe(EF, Sec) + ": " + 69206c3fb27SDimitry Andric toString(Relas.takeError())); 6935f757f3fSDimitry Andric for (typename ELFFile<ELFT>::Elf_Rela Rela : *Relas) 69406c3fb27SDimitry Andric FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend; 69506c3fb27SDimitry Andric } 6965f757f3fSDimitry Andric Expected<ArrayRef<uint8_t>> ContentsOrErr = EF.getSectionContents(Sec); 697fe6060f1SDimitry Andric if (!ContentsOrErr) 698fe6060f1SDimitry Andric return ContentsOrErr.takeError(); 699fe6060f1SDimitry Andric ArrayRef<uint8_t> Content = *ContentsOrErr; 7005f757f3fSDimitry Andric DataExtractor Data(Content, EF.isLE(), ELFT::Is64Bits ? 8 : 4); 701349cc55cSDimitry Andric std::vector<BBAddrMap> FunctionEntries; 702fe6060f1SDimitry Andric 703fe6060f1SDimitry Andric DataExtractor::Cursor Cur(0); 704fe6060f1SDimitry Andric Error ULEBSizeErr = Error::success(); 70506c3fb27SDimitry Andric Error MetadataDecodeErr = Error::success(); 706fe6060f1SDimitry Andric 70781ad6265SDimitry Andric uint8_t Version = 0; 7085f757f3fSDimitry Andric uint8_t Feature = 0; 7095f757f3fSDimitry Andric PGOAnalysisMap::Features FeatEnable{}; 71006c3fb27SDimitry Andric while (!ULEBSizeErr && !MetadataDecodeErr && Cur && 71106c3fb27SDimitry Andric Cur.tell() < Content.size()) { 71281ad6265SDimitry Andric if (Sec.sh_type == ELF::SHT_LLVM_BB_ADDR_MAP) { 71381ad6265SDimitry Andric Version = Data.getU8(Cur); 71481ad6265SDimitry Andric if (!Cur) 71581ad6265SDimitry Andric break; 716bdd1243dSDimitry Andric if (Version > 2) 71781ad6265SDimitry Andric return createError("unsupported SHT_LLVM_BB_ADDR_MAP version: " + 71881ad6265SDimitry Andric Twine(static_cast<int>(Version))); 7195f757f3fSDimitry Andric Feature = Data.getU8(Cur); // Feature byte 7205f757f3fSDimitry Andric if (!Cur) 7215f757f3fSDimitry Andric break; 7225f757f3fSDimitry Andric auto FeatEnableOrErr = PGOAnalysisMap::Features::decode(Feature); 7235f757f3fSDimitry Andric if (!FeatEnableOrErr) 7245f757f3fSDimitry Andric return FeatEnableOrErr.takeError(); 7255f757f3fSDimitry Andric FeatEnable = 7265f757f3fSDimitry Andric FeatEnableOrErr ? *FeatEnableOrErr : PGOAnalysisMap::Features{}; 7275f757f3fSDimitry Andric if (Feature != 0 && Version < 2 && Cur) 7285f757f3fSDimitry Andric return createError( 7295f757f3fSDimitry Andric "version should be >= 2 for SHT_LLVM_BB_ADDR_MAP when " 7305f757f3fSDimitry Andric "PGO features are enabled: version = " + 7315f757f3fSDimitry Andric Twine(static_cast<int>(Version)) + 7325f757f3fSDimitry Andric " feature = " + Twine(static_cast<int>(Feature))); 73381ad6265SDimitry Andric } 73406c3fb27SDimitry Andric uint64_t SectionOffset = Cur.tell(); 7355f757f3fSDimitry Andric auto Address = 7365f757f3fSDimitry Andric static_cast<typename ELFFile<ELFT>::uintX_t>(Data.getAddress(Cur)); 73706c3fb27SDimitry Andric if (!Cur) 73806c3fb27SDimitry Andric return Cur.takeError(); 73906c3fb27SDimitry Andric if (IsRelocatable) { 74006c3fb27SDimitry Andric assert(Address == 0); 74106c3fb27SDimitry Andric auto FOTIterator = FunctionOffsetTranslations.find(SectionOffset); 74206c3fb27SDimitry Andric if (FOTIterator == FunctionOffsetTranslations.end()) { 74306c3fb27SDimitry Andric return createError("failed to get relocation data for offset: " + 74406c3fb27SDimitry Andric Twine::utohexstr(SectionOffset) + " in section " + 7455f757f3fSDimitry Andric describe(EF, Sec)); 74606c3fb27SDimitry Andric } 74706c3fb27SDimitry Andric Address = FOTIterator->second; 74806c3fb27SDimitry Andric } 7495f757f3fSDimitry Andric uint32_t NumBlocks = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr); 7505f757f3fSDimitry Andric 751349cc55cSDimitry Andric std::vector<BBAddrMap::BBEntry> BBEntries; 75281ad6265SDimitry Andric uint32_t PrevBBEndOffset = 0; 753bdd1243dSDimitry Andric for (uint32_t BlockIndex = 0; 75406c3fb27SDimitry Andric !MetadataDecodeErr && !ULEBSizeErr && Cur && (BlockIndex < NumBlocks); 75506c3fb27SDimitry Andric ++BlockIndex) { 7565f757f3fSDimitry Andric uint32_t ID = Version >= 2 7575f757f3fSDimitry Andric ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr) 7585f757f3fSDimitry Andric : BlockIndex; 7595f757f3fSDimitry Andric uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr); 7605f757f3fSDimitry Andric uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr); 7615f757f3fSDimitry Andric uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr); 76281ad6265SDimitry Andric if (Version >= 1) { 76381ad6265SDimitry Andric // Offset is calculated relative to the end of the previous BB. 76481ad6265SDimitry Andric Offset += PrevBBEndOffset; 76581ad6265SDimitry Andric PrevBBEndOffset = Offset + Size; 76681ad6265SDimitry Andric } 76706c3fb27SDimitry Andric Expected<BBAddrMap::BBEntry::Metadata> MetadataOrErr = 76806c3fb27SDimitry Andric BBAddrMap::BBEntry::Metadata::decode(MD); 76906c3fb27SDimitry Andric if (!MetadataOrErr) { 77006c3fb27SDimitry Andric MetadataDecodeErr = MetadataOrErr.takeError(); 77106c3fb27SDimitry Andric break; 77206c3fb27SDimitry Andric } 77306c3fb27SDimitry Andric BBEntries.push_back({ID, Offset, Size, *MetadataOrErr}); 774fe6060f1SDimitry Andric } 7755f757f3fSDimitry Andric FunctionEntries.emplace_back(Address, std::move(BBEntries)); 7765f757f3fSDimitry Andric 777*1db9f3b2SDimitry Andric if (PGOAnalyses || FeatEnable.anyEnabled()) { 7785f757f3fSDimitry Andric // Function entry count 7795f757f3fSDimitry Andric uint64_t FuncEntryCount = 7805f757f3fSDimitry Andric FeatEnable.FuncEntryCount 7815f757f3fSDimitry Andric ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr) 7825f757f3fSDimitry Andric : 0; 7835f757f3fSDimitry Andric 7845f757f3fSDimitry Andric std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries; 785*1db9f3b2SDimitry Andric for (uint32_t BlockIndex = 0; 786*1db9f3b2SDimitry Andric (FeatEnable.BBFreq || FeatEnable.BrProb) && !MetadataDecodeErr && 787*1db9f3b2SDimitry Andric !ULEBSizeErr && Cur && (BlockIndex < NumBlocks); 7885f757f3fSDimitry Andric ++BlockIndex) { 7895f757f3fSDimitry Andric // Block frequency 7905f757f3fSDimitry Andric uint64_t BBF = FeatEnable.BBFreq 7915f757f3fSDimitry Andric ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr) 7925f757f3fSDimitry Andric : 0; 7935f757f3fSDimitry Andric 7945f757f3fSDimitry Andric // Branch probability 7955f757f3fSDimitry Andric llvm::SmallVector<PGOAnalysisMap::PGOBBEntry::SuccessorEntry, 2> 7965f757f3fSDimitry Andric Successors; 7975f757f3fSDimitry Andric if (FeatEnable.BrProb) { 7985f757f3fSDimitry Andric auto SuccCount = readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr); 7995f757f3fSDimitry Andric for (uint64_t I = 0; I < SuccCount; ++I) { 8005f757f3fSDimitry Andric uint32_t BBID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr); 8015f757f3fSDimitry Andric uint32_t BrProb = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr); 8025f757f3fSDimitry Andric if (PGOAnalyses) 8035f757f3fSDimitry Andric Successors.push_back({BBID, BranchProbability::getRaw(BrProb)}); 8045f757f3fSDimitry Andric } 8055f757f3fSDimitry Andric } 8065f757f3fSDimitry Andric 8075f757f3fSDimitry Andric if (PGOAnalyses) 8085f757f3fSDimitry Andric PGOBBEntries.push_back({BlockFrequency(BBF), std::move(Successors)}); 8095f757f3fSDimitry Andric } 8105f757f3fSDimitry Andric 8115f757f3fSDimitry Andric if (PGOAnalyses) 8125f757f3fSDimitry Andric PGOAnalyses->push_back( 8135f757f3fSDimitry Andric {FuncEntryCount, std::move(PGOBBEntries), FeatEnable}); 8145f757f3fSDimitry Andric } 815fe6060f1SDimitry Andric } 81606c3fb27SDimitry Andric // Either Cur is in the error state, or we have an error in ULEBSizeErr or 81706c3fb27SDimitry Andric // MetadataDecodeErr (but not both), but we join all errors here to be safe. 81806c3fb27SDimitry Andric if (!Cur || ULEBSizeErr || MetadataDecodeErr) 81906c3fb27SDimitry Andric return joinErrors(joinErrors(Cur.takeError(), std::move(ULEBSizeErr)), 82006c3fb27SDimitry Andric std::move(MetadataDecodeErr)); 821fe6060f1SDimitry Andric return FunctionEntries; 822fe6060f1SDimitry Andric } 823fe6060f1SDimitry Andric 82406c3fb27SDimitry Andric template <class ELFT> 8255f757f3fSDimitry Andric Expected<std::vector<BBAddrMap>> 8265f757f3fSDimitry Andric ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec, 8275f757f3fSDimitry Andric std::vector<PGOAnalysisMap> *PGOAnalyses) const { 8285f757f3fSDimitry Andric size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0; 8295f757f3fSDimitry Andric auto AddrMapsOrErr = decodeBBAddrMapImpl(*this, Sec, RelaSec, PGOAnalyses); 8305f757f3fSDimitry Andric // remove new analyses when an error occurs 8315f757f3fSDimitry Andric if (!AddrMapsOrErr && PGOAnalyses) 8325f757f3fSDimitry Andric PGOAnalyses->resize(OriginalPGOSize); 8335f757f3fSDimitry Andric return std::move(AddrMapsOrErr); 8345f757f3fSDimitry Andric } 8355f757f3fSDimitry Andric 8365f757f3fSDimitry Andric template <class ELFT> 83706c3fb27SDimitry Andric Expected< 83806c3fb27SDimitry Andric MapVector<const typename ELFT::Shdr *, const typename ELFT::Shdr *>> 83906c3fb27SDimitry Andric ELFFile<ELFT>::getSectionAndRelocations( 84006c3fb27SDimitry Andric std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const { 84106c3fb27SDimitry Andric MapVector<const Elf_Shdr *, const Elf_Shdr *> SecToRelocMap; 84206c3fb27SDimitry Andric Error Errors = Error::success(); 84306c3fb27SDimitry Andric for (const Elf_Shdr &Sec : cantFail(this->sections())) { 84406c3fb27SDimitry Andric Expected<bool> DoesSectionMatch = IsMatch(Sec); 84506c3fb27SDimitry Andric if (!DoesSectionMatch) { 84606c3fb27SDimitry Andric Errors = joinErrors(std::move(Errors), DoesSectionMatch.takeError()); 84706c3fb27SDimitry Andric continue; 84806c3fb27SDimitry Andric } 84906c3fb27SDimitry Andric if (*DoesSectionMatch) { 85006c3fb27SDimitry Andric if (SecToRelocMap.insert(std::make_pair(&Sec, (const Elf_Shdr *)nullptr)) 85106c3fb27SDimitry Andric .second) 85206c3fb27SDimitry Andric continue; 85306c3fb27SDimitry Andric } 85406c3fb27SDimitry Andric 85506c3fb27SDimitry Andric if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL) 85606c3fb27SDimitry Andric continue; 85706c3fb27SDimitry Andric 85806c3fb27SDimitry Andric Expected<const Elf_Shdr *> RelSecOrErr = this->getSection(Sec.sh_info); 85906c3fb27SDimitry Andric if (!RelSecOrErr) { 86006c3fb27SDimitry Andric Errors = joinErrors(std::move(Errors), 86106c3fb27SDimitry Andric createError(describe(*this, Sec) + 86206c3fb27SDimitry Andric ": failed to get a relocated section: " + 86306c3fb27SDimitry Andric toString(RelSecOrErr.takeError()))); 86406c3fb27SDimitry Andric continue; 86506c3fb27SDimitry Andric } 86606c3fb27SDimitry Andric const Elf_Shdr *ContentsSec = *RelSecOrErr; 86706c3fb27SDimitry Andric Expected<bool> DoesRelTargetMatch = IsMatch(*ContentsSec); 86806c3fb27SDimitry Andric if (!DoesRelTargetMatch) { 86906c3fb27SDimitry Andric Errors = joinErrors(std::move(Errors), DoesRelTargetMatch.takeError()); 87006c3fb27SDimitry Andric continue; 87106c3fb27SDimitry Andric } 87206c3fb27SDimitry Andric if (*DoesRelTargetMatch) 87306c3fb27SDimitry Andric SecToRelocMap[ContentsSec] = &Sec; 87406c3fb27SDimitry Andric } 87506c3fb27SDimitry Andric if(Errors) 87606c3fb27SDimitry Andric return std::move(Errors); 87706c3fb27SDimitry Andric return SecToRelocMap; 87806c3fb27SDimitry Andric } 87906c3fb27SDimitry Andric 8800b57cec5SDimitry Andric template class llvm::object::ELFFile<ELF32LE>; 8810b57cec5SDimitry Andric template class llvm::object::ELFFile<ELF32BE>; 8820b57cec5SDimitry Andric template class llvm::object::ELFFile<ELF64LE>; 8830b57cec5SDimitry Andric template class llvm::object::ELFFile<ELF64BE>; 884