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 case ELF::EM_RISCV: 497 switch (Type) { 498 #define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 499 #include "llvm/BinaryFormat/DynamicTags.def" 500 #undef RISCV_DYNAMIC_TAG 501 } 502 break; 503 } 504 #undef DYNAMIC_TAG 505 switch (Type) { 506 // Now handle all dynamic tags except the architecture specific ones 507 #define AARCH64_DYNAMIC_TAG(name, value) 508 #define MIPS_DYNAMIC_TAG(name, value) 509 #define HEXAGON_DYNAMIC_TAG(name, value) 510 #define PPC_DYNAMIC_TAG(name, value) 511 #define PPC64_DYNAMIC_TAG(name, value) 512 #define RISCV_DYNAMIC_TAG(name, value) 513 // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. 514 #define DYNAMIC_TAG_MARKER(name, value) 515 #define DYNAMIC_TAG(name, value) case value: return #name; 516 #include "llvm/BinaryFormat/DynamicTags.def" 517 #undef DYNAMIC_TAG 518 #undef AARCH64_DYNAMIC_TAG 519 #undef MIPS_DYNAMIC_TAG 520 #undef HEXAGON_DYNAMIC_TAG 521 #undef PPC_DYNAMIC_TAG 522 #undef PPC64_DYNAMIC_TAG 523 #undef RISCV_DYNAMIC_TAG 524 #undef DYNAMIC_TAG_MARKER 525 #undef DYNAMIC_STRINGIFY_ENUM 526 default: 527 return "<unknown:>0x" + utohexstr(Type, true); 528 } 529 } 530 531 template <class ELFT> 532 std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const { 533 return getDynamicTagAsString(getHeader().e_machine, Type); 534 } 535 536 template <class ELFT> 537 Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const { 538 ArrayRef<Elf_Dyn> Dyn; 539 540 auto ProgramHeadersOrError = program_headers(); 541 if (!ProgramHeadersOrError) 542 return ProgramHeadersOrError.takeError(); 543 544 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) { 545 if (Phdr.p_type == ELF::PT_DYNAMIC) { 546 Dyn = makeArrayRef( 547 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset), 548 Phdr.p_filesz / sizeof(Elf_Dyn)); 549 break; 550 } 551 } 552 553 // If we can't find the dynamic section in the program headers, we just fall 554 // back on the sections. 555 if (Dyn.empty()) { 556 auto SectionsOrError = sections(); 557 if (!SectionsOrError) 558 return SectionsOrError.takeError(); 559 560 for (const Elf_Shdr &Sec : *SectionsOrError) { 561 if (Sec.sh_type == ELF::SHT_DYNAMIC) { 562 Expected<ArrayRef<Elf_Dyn>> DynOrError = 563 getSectionContentsAsArray<Elf_Dyn>(Sec); 564 if (!DynOrError) 565 return DynOrError.takeError(); 566 Dyn = *DynOrError; 567 break; 568 } 569 } 570 571 if (!Dyn.data()) 572 return ArrayRef<Elf_Dyn>(); 573 } 574 575 if (Dyn.empty()) 576 // TODO: this error is untested. 577 return createError("invalid empty dynamic section"); 578 579 if (Dyn.back().d_tag != ELF::DT_NULL) 580 // TODO: this error is untested. 581 return createError("dynamic sections must be DT_NULL terminated"); 582 583 return Dyn; 584 } 585 586 template <class ELFT> 587 Expected<const uint8_t *> 588 ELFFile<ELFT>::toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler) const { 589 auto ProgramHeadersOrError = program_headers(); 590 if (!ProgramHeadersOrError) 591 return ProgramHeadersOrError.takeError(); 592 593 llvm::SmallVector<Elf_Phdr *, 4> LoadSegments; 594 595 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) 596 if (Phdr.p_type == ELF::PT_LOAD) 597 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr)); 598 599 auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A, 600 const Elf_Phdr_Impl<ELFT> *B) { 601 return A->p_vaddr < B->p_vaddr; 602 }; 603 if (!llvm::is_sorted(LoadSegments, SortPred)) { 604 if (Error E = 605 WarnHandler("loadable segments are unsorted by virtual address")) 606 return std::move(E); 607 llvm::stable_sort(LoadSegments, SortPred); 608 } 609 610 const Elf_Phdr *const *I = llvm::upper_bound( 611 LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) { 612 return VAddr < Phdr->p_vaddr; 613 }); 614 615 if (I == LoadSegments.begin()) 616 return createError("virtual address is not in any segment: 0x" + 617 Twine::utohexstr(VAddr)); 618 --I; 619 const Elf_Phdr &Phdr = **I; 620 uint64_t Delta = VAddr - Phdr.p_vaddr; 621 if (Delta >= Phdr.p_filesz) 622 return createError("virtual address is not in any segment: 0x" + 623 Twine::utohexstr(VAddr)); 624 625 uint64_t Offset = Phdr.p_offset + Delta; 626 if (Offset >= getBufSize()) 627 return createError("can't map virtual address 0x" + 628 Twine::utohexstr(VAddr) + " to the segment with index " + 629 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) + 630 ": the segment ends at 0x" + 631 Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) + 632 ", which is greater than the file size (0x" + 633 Twine::utohexstr(getBufSize()) + ")"); 634 635 return base() + Offset; 636 } 637 638 template <class ELFT> 639 Expected<std::vector<typename ELFT::BBAddrMap>> 640 ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec) const { 641 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec); 642 if (!ContentsOrErr) 643 return ContentsOrErr.takeError(); 644 ArrayRef<uint8_t> Content = *ContentsOrErr; 645 DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4); 646 std::vector<Elf_BBAddrMap> FunctionEntries; 647 648 DataExtractor::Cursor Cur(0); 649 Error ULEBSizeErr = Error::success(); 650 651 // Helper to extract and decode the next ULEB128 value as uint32_t. 652 // Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the uint32_t 653 // limit. 654 // Also returns zero if ULEBSizeErr is already in an error state. 655 auto ReadULEB128AsUInt32 = [&Data, &Cur, &ULEBSizeErr]() -> uint32_t { 656 // Bail out and do not extract data if ULEBSizeErr is already set. 657 if (ULEBSizeErr) 658 return 0; 659 uint64_t Offset = Cur.tell(); 660 uint64_t Value = Data.getULEB128(Cur); 661 if (Value > UINT32_MAX) { 662 ULEBSizeErr = createError( 663 "ULEB128 value at offset 0x" + Twine::utohexstr(Offset) + 664 " exceeds UINT32_MAX (0x" + Twine::utohexstr(Value) + ")"); 665 return 0; 666 } 667 return static_cast<uint32_t>(Value); 668 }; 669 670 while (!ULEBSizeErr && Cur && Cur.tell() < Content.size()) { 671 uintX_t Address = static_cast<uintX_t>(Data.getAddress(Cur)); 672 uint32_t NumBlocks = ReadULEB128AsUInt32(); 673 std::vector<typename Elf_BBAddrMap::BBEntry> BBEntries; 674 for (uint32_t BlockID = 0; !ULEBSizeErr && Cur && (BlockID < NumBlocks); 675 ++BlockID) { 676 uint32_t Offset = ReadULEB128AsUInt32(); 677 uint32_t Size = ReadULEB128AsUInt32(); 678 uint32_t Metadata = ReadULEB128AsUInt32(); 679 BBEntries.push_back({Offset, Size, Metadata}); 680 } 681 FunctionEntries.push_back({Address, BBEntries}); 682 } 683 // Either Cur is in the error state, or ULEBSizeError is set (not both), but 684 // we join the two errors here to be safe. 685 if (!Cur || ULEBSizeErr) 686 return joinErrors(Cur.takeError(), std::move(ULEBSizeErr)); 687 return FunctionEntries; 688 } 689 690 template class llvm::object::ELFFile<ELF32LE>; 691 template class llvm::object::ELFFile<ELF32BE>; 692 template class llvm::object::ELFFile<ELF64LE>; 693 template class llvm::object::ELFFile<ELF64BE>; 694