1 //===- ELF.cpp - ELF object file implementation ---------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Object/ELF.h" 10 #include "llvm/BinaryFormat/ELF.h" 11 #include "llvm/Support/DataExtractor.h" 12 13 using namespace llvm; 14 using namespace object; 15 16 #define STRINGIFY_ENUM_CASE(ns, name) \ 17 case ns::name: \ 18 return #name; 19 20 #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name) 21 22 StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine, 23 uint32_t Type) { 24 switch (Machine) { 25 case ELF::EM_68K: 26 switch (Type) { 27 #include "llvm/BinaryFormat/ELFRelocs/M68k.def" 28 default: 29 break; 30 } 31 break; 32 case ELF::EM_X86_64: 33 switch (Type) { 34 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def" 35 default: 36 break; 37 } 38 break; 39 case ELF::EM_386: 40 case ELF::EM_IAMCU: 41 switch (Type) { 42 #include "llvm/BinaryFormat/ELFRelocs/i386.def" 43 default: 44 break; 45 } 46 break; 47 case ELF::EM_MIPS: 48 switch (Type) { 49 #include "llvm/BinaryFormat/ELFRelocs/Mips.def" 50 default: 51 break; 52 } 53 break; 54 case ELF::EM_AARCH64: 55 switch (Type) { 56 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def" 57 default: 58 break; 59 } 60 break; 61 case ELF::EM_ARM: 62 switch (Type) { 63 #include "llvm/BinaryFormat/ELFRelocs/ARM.def" 64 default: 65 break; 66 } 67 break; 68 case ELF::EM_ARC_COMPACT: 69 case ELF::EM_ARC_COMPACT2: 70 switch (Type) { 71 #include "llvm/BinaryFormat/ELFRelocs/ARC.def" 72 default: 73 break; 74 } 75 break; 76 case ELF::EM_AVR: 77 switch (Type) { 78 #include "llvm/BinaryFormat/ELFRelocs/AVR.def" 79 default: 80 break; 81 } 82 break; 83 case ELF::EM_HEXAGON: 84 switch (Type) { 85 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def" 86 default: 87 break; 88 } 89 break; 90 case ELF::EM_LANAI: 91 switch (Type) { 92 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def" 93 default: 94 break; 95 } 96 break; 97 case ELF::EM_PPC: 98 switch (Type) { 99 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def" 100 default: 101 break; 102 } 103 break; 104 case ELF::EM_PPC64: 105 switch (Type) { 106 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def" 107 default: 108 break; 109 } 110 break; 111 case ELF::EM_RISCV: 112 switch (Type) { 113 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def" 114 default: 115 break; 116 } 117 break; 118 case ELF::EM_S390: 119 switch (Type) { 120 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def" 121 default: 122 break; 123 } 124 break; 125 case ELF::EM_SPARC: 126 case ELF::EM_SPARC32PLUS: 127 case ELF::EM_SPARCV9: 128 switch (Type) { 129 #include "llvm/BinaryFormat/ELFRelocs/Sparc.def" 130 default: 131 break; 132 } 133 break; 134 case ELF::EM_AMDGPU: 135 switch (Type) { 136 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def" 137 default: 138 break; 139 } 140 break; 141 case ELF::EM_BPF: 142 switch (Type) { 143 #include "llvm/BinaryFormat/ELFRelocs/BPF.def" 144 default: 145 break; 146 } 147 break; 148 case ELF::EM_MSP430: 149 switch (Type) { 150 #include "llvm/BinaryFormat/ELFRelocs/MSP430.def" 151 default: 152 break; 153 } 154 break; 155 case ELF::EM_VE: 156 switch (Type) { 157 #include "llvm/BinaryFormat/ELFRelocs/VE.def" 158 default: 159 break; 160 } 161 break; 162 case ELF::EM_CSKY: 163 switch (Type) { 164 #include "llvm/BinaryFormat/ELFRelocs/CSKY.def" 165 default: 166 break; 167 } 168 break; 169 default: 170 break; 171 } 172 return "Unknown"; 173 } 174 175 #undef ELF_RELOC 176 177 uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) { 178 switch (Machine) { 179 case ELF::EM_X86_64: 180 return ELF::R_X86_64_RELATIVE; 181 case ELF::EM_386: 182 case ELF::EM_IAMCU: 183 return ELF::R_386_RELATIVE; 184 case ELF::EM_MIPS: 185 break; 186 case ELF::EM_AARCH64: 187 return ELF::R_AARCH64_RELATIVE; 188 case ELF::EM_ARM: 189 return ELF::R_ARM_RELATIVE; 190 case ELF::EM_ARC_COMPACT: 191 case ELF::EM_ARC_COMPACT2: 192 return ELF::R_ARC_RELATIVE; 193 case ELF::EM_AVR: 194 break; 195 case ELF::EM_HEXAGON: 196 return ELF::R_HEX_RELATIVE; 197 case ELF::EM_LANAI: 198 break; 199 case ELF::EM_PPC: 200 break; 201 case ELF::EM_PPC64: 202 return ELF::R_PPC64_RELATIVE; 203 case ELF::EM_RISCV: 204 return ELF::R_RISCV_RELATIVE; 205 case ELF::EM_S390: 206 return ELF::R_390_RELATIVE; 207 case ELF::EM_SPARC: 208 case ELF::EM_SPARC32PLUS: 209 case ELF::EM_SPARCV9: 210 return ELF::R_SPARC_RELATIVE; 211 case ELF::EM_CSKY: 212 return ELF::R_CKCORE_RELATIVE; 213 case ELF::EM_AMDGPU: 214 break; 215 case ELF::EM_BPF: 216 break; 217 default: 218 break; 219 } 220 return 0; 221 } 222 223 StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) { 224 switch (Machine) { 225 case ELF::EM_ARM: 226 switch (Type) { 227 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX); 228 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP); 229 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES); 230 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY); 231 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION); 232 } 233 break; 234 case ELF::EM_HEXAGON: 235 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); } 236 break; 237 case ELF::EM_X86_64: 238 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); } 239 break; 240 case ELF::EM_MIPS: 241 case ELF::EM_MIPS_RS3_LE: 242 switch (Type) { 243 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO); 244 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS); 245 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF); 246 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS); 247 } 248 break; 249 case ELF::EM_MSP430: 250 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_MSP430_ATTRIBUTES); } 251 break; 252 case ELF::EM_RISCV: 253 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); } 254 break; 255 default: 256 break; 257 } 258 259 switch (Type) { 260 STRINGIFY_ENUM_CASE(ELF, SHT_NULL); 261 STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS); 262 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB); 263 STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB); 264 STRINGIFY_ENUM_CASE(ELF, SHT_RELA); 265 STRINGIFY_ENUM_CASE(ELF, SHT_HASH); 266 STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC); 267 STRINGIFY_ENUM_CASE(ELF, SHT_NOTE); 268 STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS); 269 STRINGIFY_ENUM_CASE(ELF, SHT_REL); 270 STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB); 271 STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM); 272 STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY); 273 STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY); 274 STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY); 275 STRINGIFY_ENUM_CASE(ELF, SHT_GROUP); 276 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX); 277 STRINGIFY_ENUM_CASE(ELF, SHT_RELR); 278 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL); 279 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA); 280 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR); 281 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB); 282 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS); 283 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE); 284 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG); 285 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES); 286 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART); 287 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR); 288 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR); 289 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP); 290 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES); 291 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH); 292 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef); 293 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed); 294 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym); 295 default: 296 return "Unknown"; 297 } 298 } 299 300 template <class ELFT> 301 std::vector<typename ELFT::Rel> 302 ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const { 303 // This function decodes the contents of an SHT_RELR packed relocation 304 // section. 305 // 306 // Proposal for adding SHT_RELR sections to generic-abi is here: 307 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg 308 // 309 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks 310 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ] 311 // 312 // i.e. start with an address, followed by any number of bitmaps. The address 313 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63 314 // relocations each, at subsequent offsets following the last address entry. 315 // 316 // The bitmap entries must have 1 in the least significant bit. The assumption 317 // here is that an address cannot have 1 in lsb. Odd addresses are not 318 // supported. 319 // 320 // Excluding the least significant bit in the bitmap, each non-zero bit in 321 // the bitmap represents a relocation to be applied to a corresponding machine 322 // word that follows the base address word. The second least significant bit 323 // represents the machine word immediately following the initial address, and 324 // each bit that follows represents the next word, in linear order. As such, 325 // a single bitmap can encode up to 31 relocations in a 32-bit object, and 326 // 63 relocations in a 64-bit object. 327 // 328 // This encoding has a couple of interesting properties: 329 // 1. Looking at any entry, it is clear whether it's an address or a bitmap: 330 // even means address, odd means bitmap. 331 // 2. Just a simple list of addresses is a valid encoding. 332 333 Elf_Rel Rel; 334 Rel.r_info = 0; 335 Rel.setType(getRelativeRelocationType(), false); 336 std::vector<Elf_Rel> Relocs; 337 338 // Word type: uint32_t for Elf32, and uint64_t for Elf64. 339 typedef typename ELFT::uint Word; 340 341 // Word size in number of bytes. 342 const size_t WordSize = sizeof(Word); 343 344 // Number of bits used for the relocation offsets bitmap. 345 // These many relative relocations can be encoded in a single entry. 346 const size_t NBits = 8*WordSize - 1; 347 348 Word Base = 0; 349 for (const Elf_Relr &R : relrs) { 350 Word Entry = R; 351 if ((Entry&1) == 0) { 352 // Even entry: encodes the offset for next relocation. 353 Rel.r_offset = Entry; 354 Relocs.push_back(Rel); 355 // Set base offset for subsequent bitmap entries. 356 Base = Entry + WordSize; 357 continue; 358 } 359 360 // Odd entry: encodes bitmap for relocations starting at base. 361 Word Offset = Base; 362 while (Entry != 0) { 363 Entry >>= 1; 364 if ((Entry&1) != 0) { 365 Rel.r_offset = Offset; 366 Relocs.push_back(Rel); 367 } 368 Offset += WordSize; 369 } 370 371 // Advance base offset by NBits words. 372 Base += NBits * WordSize; 373 } 374 375 return Relocs; 376 } 377 378 template <class ELFT> 379 Expected<std::vector<typename ELFT::Rela>> 380 ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const { 381 // This function reads relocations in Android's packed relocation format, 382 // which is based on SLEB128 and delta encoding. 383 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec); 384 if (!ContentsOrErr) 385 return ContentsOrErr.takeError(); 386 ArrayRef<uint8_t> Content = *ContentsOrErr; 387 if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' || 388 Content[2] != 'S' || Content[3] != '2') 389 return createError("invalid packed relocation header"); 390 DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4); 391 DataExtractor::Cursor Cur(/*Offset=*/4); 392 393 uint64_t NumRelocs = Data.getSLEB128(Cur); 394 uint64_t Offset = Data.getSLEB128(Cur); 395 uint64_t Addend = 0; 396 397 if (!Cur) 398 return std::move(Cur.takeError()); 399 400 std::vector<Elf_Rela> Relocs; 401 Relocs.reserve(NumRelocs); 402 while (NumRelocs) { 403 uint64_t NumRelocsInGroup = Data.getSLEB128(Cur); 404 if (!Cur) 405 return std::move(Cur.takeError()); 406 if (NumRelocsInGroup > NumRelocs) 407 return createError("relocation group unexpectedly large"); 408 NumRelocs -= NumRelocsInGroup; 409 410 uint64_t GroupFlags = Data.getSLEB128(Cur); 411 bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG; 412 bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG; 413 bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG; 414 bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG; 415 416 uint64_t GroupOffsetDelta; 417 if (GroupedByOffsetDelta) 418 GroupOffsetDelta = Data.getSLEB128(Cur); 419 420 uint64_t GroupRInfo; 421 if (GroupedByInfo) 422 GroupRInfo = Data.getSLEB128(Cur); 423 424 if (GroupedByAddend && GroupHasAddend) 425 Addend += Data.getSLEB128(Cur); 426 427 if (!GroupHasAddend) 428 Addend = 0; 429 430 for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) { 431 Elf_Rela R; 432 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur); 433 R.r_offset = Offset; 434 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur); 435 if (GroupHasAddend && !GroupedByAddend) 436 Addend += Data.getSLEB128(Cur); 437 R.r_addend = Addend; 438 Relocs.push_back(R); 439 } 440 if (!Cur) 441 return std::move(Cur.takeError()); 442 } 443 444 return Relocs; 445 } 446 447 template <class ELFT> 448 std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch, 449 uint64_t Type) const { 450 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \ 451 case value: \ 452 return #tag; 453 454 #define DYNAMIC_TAG(n, v) 455 switch (Arch) { 456 case ELF::EM_AARCH64: 457 switch (Type) { 458 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 459 #include "llvm/BinaryFormat/DynamicTags.def" 460 #undef AARCH64_DYNAMIC_TAG 461 } 462 break; 463 464 case ELF::EM_HEXAGON: 465 switch (Type) { 466 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 467 #include "llvm/BinaryFormat/DynamicTags.def" 468 #undef HEXAGON_DYNAMIC_TAG 469 } 470 break; 471 472 case ELF::EM_MIPS: 473 switch (Type) { 474 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 475 #include "llvm/BinaryFormat/DynamicTags.def" 476 #undef MIPS_DYNAMIC_TAG 477 } 478 break; 479 480 case ELF::EM_PPC: 481 switch (Type) { 482 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 483 #include "llvm/BinaryFormat/DynamicTags.def" 484 #undef PPC_DYNAMIC_TAG 485 } 486 break; 487 488 case ELF::EM_PPC64: 489 switch (Type) { 490 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 491 #include "llvm/BinaryFormat/DynamicTags.def" 492 #undef PPC64_DYNAMIC_TAG 493 } 494 break; 495 } 496 #undef DYNAMIC_TAG 497 switch (Type) { 498 // Now handle all dynamic tags except the architecture specific ones 499 #define AARCH64_DYNAMIC_TAG(name, value) 500 #define MIPS_DYNAMIC_TAG(name, value) 501 #define HEXAGON_DYNAMIC_TAG(name, value) 502 #define PPC_DYNAMIC_TAG(name, value) 503 #define PPC64_DYNAMIC_TAG(name, value) 504 // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. 505 #define DYNAMIC_TAG_MARKER(name, value) 506 #define DYNAMIC_TAG(name, value) case value: return #name; 507 #include "llvm/BinaryFormat/DynamicTags.def" 508 #undef DYNAMIC_TAG 509 #undef AARCH64_DYNAMIC_TAG 510 #undef MIPS_DYNAMIC_TAG 511 #undef HEXAGON_DYNAMIC_TAG 512 #undef PPC_DYNAMIC_TAG 513 #undef PPC64_DYNAMIC_TAG 514 #undef DYNAMIC_TAG_MARKER 515 #undef DYNAMIC_STRINGIFY_ENUM 516 default: 517 return "<unknown:>0x" + utohexstr(Type, true); 518 } 519 } 520 521 template <class ELFT> 522 std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const { 523 return getDynamicTagAsString(getHeader().e_machine, Type); 524 } 525 526 template <class ELFT> 527 Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const { 528 ArrayRef<Elf_Dyn> Dyn; 529 530 auto ProgramHeadersOrError = program_headers(); 531 if (!ProgramHeadersOrError) 532 return ProgramHeadersOrError.takeError(); 533 534 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) { 535 if (Phdr.p_type == ELF::PT_DYNAMIC) { 536 Dyn = makeArrayRef( 537 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset), 538 Phdr.p_filesz / sizeof(Elf_Dyn)); 539 break; 540 } 541 } 542 543 // If we can't find the dynamic section in the program headers, we just fall 544 // back on the sections. 545 if (Dyn.empty()) { 546 auto SectionsOrError = sections(); 547 if (!SectionsOrError) 548 return SectionsOrError.takeError(); 549 550 for (const Elf_Shdr &Sec : *SectionsOrError) { 551 if (Sec.sh_type == ELF::SHT_DYNAMIC) { 552 Expected<ArrayRef<Elf_Dyn>> DynOrError = 553 getSectionContentsAsArray<Elf_Dyn>(Sec); 554 if (!DynOrError) 555 return DynOrError.takeError(); 556 Dyn = *DynOrError; 557 break; 558 } 559 } 560 561 if (!Dyn.data()) 562 return ArrayRef<Elf_Dyn>(); 563 } 564 565 if (Dyn.empty()) 566 // TODO: this error is untested. 567 return createError("invalid empty dynamic section"); 568 569 if (Dyn.back().d_tag != ELF::DT_NULL) 570 // TODO: this error is untested. 571 return createError("dynamic sections must be DT_NULL terminated"); 572 573 return Dyn; 574 } 575 576 template <class ELFT> 577 Expected<const uint8_t *> 578 ELFFile<ELFT>::toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler) const { 579 auto ProgramHeadersOrError = program_headers(); 580 if (!ProgramHeadersOrError) 581 return ProgramHeadersOrError.takeError(); 582 583 llvm::SmallVector<Elf_Phdr *, 4> LoadSegments; 584 585 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) 586 if (Phdr.p_type == ELF::PT_LOAD) 587 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr)); 588 589 auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A, 590 const Elf_Phdr_Impl<ELFT> *B) { 591 return A->p_vaddr < B->p_vaddr; 592 }; 593 if (!llvm::is_sorted(LoadSegments, SortPred)) { 594 if (Error E = 595 WarnHandler("loadable segments are unsorted by virtual address")) 596 return std::move(E); 597 llvm::stable_sort(LoadSegments, SortPred); 598 } 599 600 const Elf_Phdr *const *I = llvm::upper_bound( 601 LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) { 602 return VAddr < Phdr->p_vaddr; 603 }); 604 605 if (I == LoadSegments.begin()) 606 return createError("virtual address is not in any segment: 0x" + 607 Twine::utohexstr(VAddr)); 608 --I; 609 const Elf_Phdr &Phdr = **I; 610 uint64_t Delta = VAddr - Phdr.p_vaddr; 611 if (Delta >= Phdr.p_filesz) 612 return createError("virtual address is not in any segment: 0x" + 613 Twine::utohexstr(VAddr)); 614 615 uint64_t Offset = Phdr.p_offset + Delta; 616 if (Offset >= getBufSize()) 617 return createError("can't map virtual address 0x" + 618 Twine::utohexstr(VAddr) + " to the segment with index " + 619 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) + 620 ": the segment ends at 0x" + 621 Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) + 622 ", which is greater than the file size (0x" + 623 Twine::utohexstr(getBufSize()) + ")"); 624 625 return base() + Offset; 626 } 627 628 template <class ELFT> 629 Expected<std::vector<typename ELFT::BBAddrMap>> 630 ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec) const { 631 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec); 632 if (!ContentsOrErr) 633 return ContentsOrErr.takeError(); 634 ArrayRef<uint8_t> Content = *ContentsOrErr; 635 DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4); 636 std::vector<Elf_BBAddrMap> FunctionEntries; 637 638 DataExtractor::Cursor Cur(0); 639 Error ULEBSizeErr = Error::success(); 640 641 // Helper to extract and decode the next ULEB128 value as uint32_t. 642 // Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the uint32_t 643 // limit. 644 // Also returns zero if ULEBSizeErr is already in an error state. 645 auto ReadULEB128AsUInt32 = [&Data, &Cur, &ULEBSizeErr]() -> uint32_t { 646 // Bail out and do not extract data if ULEBSizeErr is already set. 647 if (ULEBSizeErr) 648 return 0; 649 uint64_t Offset = Cur.tell(); 650 uint64_t Value = Data.getULEB128(Cur); 651 if (Value > UINT32_MAX) { 652 ULEBSizeErr = createError( 653 "ULEB128 value at offset 0x" + Twine::utohexstr(Offset) + 654 " exceeds UINT32_MAX (0x" + Twine::utohexstr(Value) + ")"); 655 return 0; 656 } 657 return static_cast<uint32_t>(Value); 658 }; 659 660 while (!ULEBSizeErr && Cur && Cur.tell() < Content.size()) { 661 uintX_t Address = static_cast<uintX_t>(Data.getAddress(Cur)); 662 uint32_t NumBlocks = ReadULEB128AsUInt32(); 663 std::vector<typename Elf_BBAddrMap::BBEntry> BBEntries; 664 for (uint32_t BlockID = 0; !ULEBSizeErr && Cur && (BlockID < NumBlocks); 665 ++BlockID) { 666 uint32_t Offset = ReadULEB128AsUInt32(); 667 uint32_t Size = ReadULEB128AsUInt32(); 668 uint32_t Metadata = ReadULEB128AsUInt32(); 669 BBEntries.push_back({Offset, Size, Metadata}); 670 } 671 FunctionEntries.push_back({Address, BBEntries}); 672 } 673 // Either Cur is in the error state, or ULEBSizeError is set (not both), but 674 // we join the two errors here to be safe. 675 if (!Cur || ULEBSizeErr) 676 return joinErrors(Cur.takeError(), std::move(ULEBSizeErr)); 677 return FunctionEntries; 678 } 679 680 template class llvm::object::ELFFile<ELF32LE>; 681 template class llvm::object::ELFFile<ELF32BE>; 682 template class llvm::object::ELFFile<ELF64LE>; 683 template class llvm::object::ELFFile<ELF64BE>; 684