1 //===- ELFObjectFile.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 // Part of the ELFObjectFile class implementation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Object/ELFObjectFile.h" 14 #include "llvm/ADT/Triple.h" 15 #include "llvm/BinaryFormat/ELF.h" 16 #include "llvm/MC/MCInstrAnalysis.h" 17 #include "llvm/MC/SubtargetFeature.h" 18 #include "llvm/MC/TargetRegistry.h" 19 #include "llvm/Object/ELF.h" 20 #include "llvm/Object/ELFTypes.h" 21 #include "llvm/Object/Error.h" 22 #include "llvm/Support/ARMAttributeParser.h" 23 #include "llvm/Support/ARMBuildAttributes.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include "llvm/Support/MathExtras.h" 26 #include "llvm/Support/RISCVAttributeParser.h" 27 #include "llvm/Support/RISCVAttributes.h" 28 #include <algorithm> 29 #include <cstddef> 30 #include <cstdint> 31 #include <memory> 32 #include <string> 33 #include <utility> 34 35 using namespace llvm; 36 using namespace object; 37 38 const EnumEntry<unsigned> llvm::object::ElfSymbolTypes[NumElfSymbolTypes] = { 39 {"None", "NOTYPE", ELF::STT_NOTYPE}, 40 {"Object", "OBJECT", ELF::STT_OBJECT}, 41 {"Function", "FUNC", ELF::STT_FUNC}, 42 {"Section", "SECTION", ELF::STT_SECTION}, 43 {"File", "FILE", ELF::STT_FILE}, 44 {"Common", "COMMON", ELF::STT_COMMON}, 45 {"TLS", "TLS", ELF::STT_TLS}, 46 {"Unknown", "<unknown>: 7", 7}, 47 {"Unknown", "<unknown>: 8", 8}, 48 {"Unknown", "<unknown>: 9", 9}, 49 {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC}, 50 {"OS Specific", "<OS specific>: 11", 11}, 51 {"OS Specific", "<OS specific>: 12", 12}, 52 {"Proc Specific", "<processor specific>: 13", 13}, 53 {"Proc Specific", "<processor specific>: 14", 14}, 54 {"Proc Specific", "<processor specific>: 15", 15} 55 }; 56 57 ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source) 58 : ObjectFile(Type, Source) {} 59 60 template <class ELFT> 61 static Expected<std::unique_ptr<ELFObjectFile<ELFT>>> 62 createPtr(MemoryBufferRef Object, bool InitContent) { 63 auto Ret = ELFObjectFile<ELFT>::create(Object, InitContent); 64 if (Error E = Ret.takeError()) 65 return std::move(E); 66 return std::make_unique<ELFObjectFile<ELFT>>(std::move(*Ret)); 67 } 68 69 Expected<std::unique_ptr<ObjectFile>> 70 ObjectFile::createELFObjectFile(MemoryBufferRef Obj, bool InitContent) { 71 std::pair<unsigned char, unsigned char> Ident = 72 getElfArchType(Obj.getBuffer()); 73 std::size_t MaxAlignment = 74 1ULL << countTrailingZeros( 75 reinterpret_cast<uintptr_t>(Obj.getBufferStart())); 76 77 if (MaxAlignment < 2) 78 return createError("Insufficient alignment"); 79 80 if (Ident.first == ELF::ELFCLASS32) { 81 if (Ident.second == ELF::ELFDATA2LSB) 82 return createPtr<ELF32LE>(Obj, InitContent); 83 else if (Ident.second == ELF::ELFDATA2MSB) 84 return createPtr<ELF32BE>(Obj, InitContent); 85 else 86 return createError("Invalid ELF data"); 87 } else if (Ident.first == ELF::ELFCLASS64) { 88 if (Ident.second == ELF::ELFDATA2LSB) 89 return createPtr<ELF64LE>(Obj, InitContent); 90 else if (Ident.second == ELF::ELFDATA2MSB) 91 return createPtr<ELF64BE>(Obj, InitContent); 92 else 93 return createError("Invalid ELF data"); 94 } 95 return createError("Invalid ELF class"); 96 } 97 98 SubtargetFeatures ELFObjectFileBase::getMIPSFeatures() const { 99 SubtargetFeatures Features; 100 unsigned PlatformFlags = getPlatformFlags(); 101 102 switch (PlatformFlags & ELF::EF_MIPS_ARCH) { 103 case ELF::EF_MIPS_ARCH_1: 104 break; 105 case ELF::EF_MIPS_ARCH_2: 106 Features.AddFeature("mips2"); 107 break; 108 case ELF::EF_MIPS_ARCH_3: 109 Features.AddFeature("mips3"); 110 break; 111 case ELF::EF_MIPS_ARCH_4: 112 Features.AddFeature("mips4"); 113 break; 114 case ELF::EF_MIPS_ARCH_5: 115 Features.AddFeature("mips5"); 116 break; 117 case ELF::EF_MIPS_ARCH_32: 118 Features.AddFeature("mips32"); 119 break; 120 case ELF::EF_MIPS_ARCH_64: 121 Features.AddFeature("mips64"); 122 break; 123 case ELF::EF_MIPS_ARCH_32R2: 124 Features.AddFeature("mips32r2"); 125 break; 126 case ELF::EF_MIPS_ARCH_64R2: 127 Features.AddFeature("mips64r2"); 128 break; 129 case ELF::EF_MIPS_ARCH_32R6: 130 Features.AddFeature("mips32r6"); 131 break; 132 case ELF::EF_MIPS_ARCH_64R6: 133 Features.AddFeature("mips64r6"); 134 break; 135 default: 136 llvm_unreachable("Unknown EF_MIPS_ARCH value"); 137 } 138 139 switch (PlatformFlags & ELF::EF_MIPS_MACH) { 140 case ELF::EF_MIPS_MACH_NONE: 141 // No feature associated with this value. 142 break; 143 case ELF::EF_MIPS_MACH_OCTEON: 144 Features.AddFeature("cnmips"); 145 break; 146 default: 147 llvm_unreachable("Unknown EF_MIPS_ARCH value"); 148 } 149 150 if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16) 151 Features.AddFeature("mips16"); 152 if (PlatformFlags & ELF::EF_MIPS_MICROMIPS) 153 Features.AddFeature("micromips"); 154 155 return Features; 156 } 157 158 SubtargetFeatures ELFObjectFileBase::getARMFeatures() const { 159 SubtargetFeatures Features; 160 ARMAttributeParser Attributes; 161 if (Error E = getBuildAttributes(Attributes)) { 162 consumeError(std::move(E)); 163 return SubtargetFeatures(); 164 } 165 166 // both ARMv7-M and R have to support thumb hardware div 167 bool isV7 = false; 168 Optional<unsigned> Attr = 169 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch); 170 if (Attr) 171 isV7 = Attr.value() == ARMBuildAttrs::v7; 172 173 Attr = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile); 174 if (Attr) { 175 switch (Attr.value()) { 176 case ARMBuildAttrs::ApplicationProfile: 177 Features.AddFeature("aclass"); 178 break; 179 case ARMBuildAttrs::RealTimeProfile: 180 Features.AddFeature("rclass"); 181 if (isV7) 182 Features.AddFeature("hwdiv"); 183 break; 184 case ARMBuildAttrs::MicroControllerProfile: 185 Features.AddFeature("mclass"); 186 if (isV7) 187 Features.AddFeature("hwdiv"); 188 break; 189 } 190 } 191 192 Attr = Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use); 193 if (Attr) { 194 switch (Attr.value()) { 195 default: 196 break; 197 case ARMBuildAttrs::Not_Allowed: 198 Features.AddFeature("thumb", false); 199 Features.AddFeature("thumb2", false); 200 break; 201 case ARMBuildAttrs::AllowThumb32: 202 Features.AddFeature("thumb2"); 203 break; 204 } 205 } 206 207 Attr = Attributes.getAttributeValue(ARMBuildAttrs::FP_arch); 208 if (Attr) { 209 switch (Attr.value()) { 210 default: 211 break; 212 case ARMBuildAttrs::Not_Allowed: 213 Features.AddFeature("vfp2sp", false); 214 Features.AddFeature("vfp3d16sp", false); 215 Features.AddFeature("vfp4d16sp", false); 216 break; 217 case ARMBuildAttrs::AllowFPv2: 218 Features.AddFeature("vfp2"); 219 break; 220 case ARMBuildAttrs::AllowFPv3A: 221 case ARMBuildAttrs::AllowFPv3B: 222 Features.AddFeature("vfp3"); 223 break; 224 case ARMBuildAttrs::AllowFPv4A: 225 case ARMBuildAttrs::AllowFPv4B: 226 Features.AddFeature("vfp4"); 227 break; 228 } 229 } 230 231 Attr = Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch); 232 if (Attr) { 233 switch (Attr.value()) { 234 default: 235 break; 236 case ARMBuildAttrs::Not_Allowed: 237 Features.AddFeature("neon", false); 238 Features.AddFeature("fp16", false); 239 break; 240 case ARMBuildAttrs::AllowNeon: 241 Features.AddFeature("neon"); 242 break; 243 case ARMBuildAttrs::AllowNeon2: 244 Features.AddFeature("neon"); 245 Features.AddFeature("fp16"); 246 break; 247 } 248 } 249 250 Attr = Attributes.getAttributeValue(ARMBuildAttrs::MVE_arch); 251 if (Attr) { 252 switch (Attr.value()) { 253 default: 254 break; 255 case ARMBuildAttrs::Not_Allowed: 256 Features.AddFeature("mve", false); 257 Features.AddFeature("mve.fp", false); 258 break; 259 case ARMBuildAttrs::AllowMVEInteger: 260 Features.AddFeature("mve.fp", false); 261 Features.AddFeature("mve"); 262 break; 263 case ARMBuildAttrs::AllowMVEIntegerAndFloat: 264 Features.AddFeature("mve.fp"); 265 break; 266 } 267 } 268 269 Attr = Attributes.getAttributeValue(ARMBuildAttrs::DIV_use); 270 if (Attr) { 271 switch (Attr.value()) { 272 default: 273 break; 274 case ARMBuildAttrs::DisallowDIV: 275 Features.AddFeature("hwdiv", false); 276 Features.AddFeature("hwdiv-arm", false); 277 break; 278 case ARMBuildAttrs::AllowDIVExt: 279 Features.AddFeature("hwdiv"); 280 Features.AddFeature("hwdiv-arm"); 281 break; 282 } 283 } 284 285 return Features; 286 } 287 288 SubtargetFeatures ELFObjectFileBase::getRISCVFeatures() const { 289 SubtargetFeatures Features; 290 unsigned PlatformFlags = getPlatformFlags(); 291 292 if (PlatformFlags & ELF::EF_RISCV_RVC) { 293 Features.AddFeature("c"); 294 } 295 296 // Add features according to the ELF attribute section. 297 // If there are any unrecognized features, ignore them. 298 RISCVAttributeParser Attributes; 299 if (Error E = getBuildAttributes(Attributes)) { 300 // TODO Propagate Error. 301 consumeError(std::move(E)); 302 return Features; // Keep "c" feature if there is one in PlatformFlags. 303 } 304 305 Optional<StringRef> Attr = Attributes.getAttributeString(RISCVAttrs::ARCH); 306 if (Attr) { 307 // The Arch pattern is [rv32|rv64][i|e]version(_[m|a|f|d|c]version)* 308 // Version string pattern is (major)p(minor). Major and minor are optional. 309 // For example, a version number could be 2p0, 2, or p92. 310 StringRef Arch = *Attr; 311 if (Arch.consume_front("rv32")) 312 Features.AddFeature("64bit", false); 313 else if (Arch.consume_front("rv64")) 314 Features.AddFeature("64bit"); 315 316 while (!Arch.empty()) { 317 switch (Arch[0]) { 318 default: 319 break; // Ignore unexpected features. 320 case 'i': 321 Features.AddFeature("e", false); 322 break; 323 case 'd': 324 Features.AddFeature("f"); // D-ext will imply F-ext. 325 LLVM_FALLTHROUGH; 326 case 'e': 327 case 'm': 328 case 'a': 329 case 'f': 330 case 'c': 331 Features.AddFeature(Arch.take_front()); 332 break; 333 } 334 335 // FIXME: Handle version numbers. 336 Arch = Arch.drop_until([](char c) { return c == '_' || c == '\0'; }); 337 Arch = Arch.drop_while([](char c) { return c == '_'; }); 338 } 339 } 340 341 return Features; 342 } 343 344 SubtargetFeatures ELFObjectFileBase::getFeatures() const { 345 switch (getEMachine()) { 346 case ELF::EM_MIPS: 347 return getMIPSFeatures(); 348 case ELF::EM_ARM: 349 return getARMFeatures(); 350 case ELF::EM_RISCV: 351 return getRISCVFeatures(); 352 default: 353 return SubtargetFeatures(); 354 } 355 } 356 357 Optional<StringRef> ELFObjectFileBase::tryGetCPUName() const { 358 switch (getEMachine()) { 359 case ELF::EM_AMDGPU: 360 return getAMDGPUCPUName(); 361 case ELF::EM_PPC64: 362 return StringRef("future"); 363 default: 364 return None; 365 } 366 } 367 368 StringRef ELFObjectFileBase::getAMDGPUCPUName() const { 369 assert(getEMachine() == ELF::EM_AMDGPU); 370 unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH; 371 372 switch (CPU) { 373 // Radeon HD 2000/3000 Series (R600). 374 case ELF::EF_AMDGPU_MACH_R600_R600: 375 return "r600"; 376 case ELF::EF_AMDGPU_MACH_R600_R630: 377 return "r630"; 378 case ELF::EF_AMDGPU_MACH_R600_RS880: 379 return "rs880"; 380 case ELF::EF_AMDGPU_MACH_R600_RV670: 381 return "rv670"; 382 383 // Radeon HD 4000 Series (R700). 384 case ELF::EF_AMDGPU_MACH_R600_RV710: 385 return "rv710"; 386 case ELF::EF_AMDGPU_MACH_R600_RV730: 387 return "rv730"; 388 case ELF::EF_AMDGPU_MACH_R600_RV770: 389 return "rv770"; 390 391 // Radeon HD 5000 Series (Evergreen). 392 case ELF::EF_AMDGPU_MACH_R600_CEDAR: 393 return "cedar"; 394 case ELF::EF_AMDGPU_MACH_R600_CYPRESS: 395 return "cypress"; 396 case ELF::EF_AMDGPU_MACH_R600_JUNIPER: 397 return "juniper"; 398 case ELF::EF_AMDGPU_MACH_R600_REDWOOD: 399 return "redwood"; 400 case ELF::EF_AMDGPU_MACH_R600_SUMO: 401 return "sumo"; 402 403 // Radeon HD 6000 Series (Northern Islands). 404 case ELF::EF_AMDGPU_MACH_R600_BARTS: 405 return "barts"; 406 case ELF::EF_AMDGPU_MACH_R600_CAICOS: 407 return "caicos"; 408 case ELF::EF_AMDGPU_MACH_R600_CAYMAN: 409 return "cayman"; 410 case ELF::EF_AMDGPU_MACH_R600_TURKS: 411 return "turks"; 412 413 // AMDGCN GFX6. 414 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX600: 415 return "gfx600"; 416 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX601: 417 return "gfx601"; 418 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX602: 419 return "gfx602"; 420 421 // AMDGCN GFX7. 422 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX700: 423 return "gfx700"; 424 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX701: 425 return "gfx701"; 426 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX702: 427 return "gfx702"; 428 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX703: 429 return "gfx703"; 430 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX704: 431 return "gfx704"; 432 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX705: 433 return "gfx705"; 434 435 // AMDGCN GFX8. 436 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX801: 437 return "gfx801"; 438 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX802: 439 return "gfx802"; 440 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX803: 441 return "gfx803"; 442 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX805: 443 return "gfx805"; 444 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX810: 445 return "gfx810"; 446 447 // AMDGCN GFX9. 448 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX900: 449 return "gfx900"; 450 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX902: 451 return "gfx902"; 452 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX904: 453 return "gfx904"; 454 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX906: 455 return "gfx906"; 456 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX908: 457 return "gfx908"; 458 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX909: 459 return "gfx909"; 460 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A: 461 return "gfx90a"; 462 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C: 463 return "gfx90c"; 464 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX940: 465 return "gfx940"; 466 467 // AMDGCN GFX10. 468 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010: 469 return "gfx1010"; 470 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011: 471 return "gfx1011"; 472 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012: 473 return "gfx1012"; 474 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013: 475 return "gfx1013"; 476 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030: 477 return "gfx1030"; 478 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031: 479 return "gfx1031"; 480 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032: 481 return "gfx1032"; 482 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033: 483 return "gfx1033"; 484 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034: 485 return "gfx1034"; 486 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035: 487 return "gfx1035"; 488 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1036: 489 return "gfx1036"; 490 491 // AMDGCN GFX11. 492 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1100: 493 return "gfx1100"; 494 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1101: 495 return "gfx1101"; 496 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1102: 497 return "gfx1102"; 498 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1103: 499 return "gfx1103"; 500 default: 501 llvm_unreachable("Unknown EF_AMDGPU_MACH value"); 502 } 503 } 504 505 // FIXME Encode from a tablegen description or target parser. 506 void ELFObjectFileBase::setARMSubArch(Triple &TheTriple) const { 507 if (TheTriple.getSubArch() != Triple::NoSubArch) 508 return; 509 510 ARMAttributeParser Attributes; 511 if (Error E = getBuildAttributes(Attributes)) { 512 // TODO Propagate Error. 513 consumeError(std::move(E)); 514 return; 515 } 516 517 std::string Triple; 518 // Default to ARM, but use the triple if it's been set. 519 if (TheTriple.isThumb()) 520 Triple = "thumb"; 521 else 522 Triple = "arm"; 523 524 Optional<unsigned> Attr = 525 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch); 526 if (Attr) { 527 switch (Attr.value()) { 528 case ARMBuildAttrs::v4: 529 Triple += "v4"; 530 break; 531 case ARMBuildAttrs::v4T: 532 Triple += "v4t"; 533 break; 534 case ARMBuildAttrs::v5T: 535 Triple += "v5t"; 536 break; 537 case ARMBuildAttrs::v5TE: 538 Triple += "v5te"; 539 break; 540 case ARMBuildAttrs::v5TEJ: 541 Triple += "v5tej"; 542 break; 543 case ARMBuildAttrs::v6: 544 Triple += "v6"; 545 break; 546 case ARMBuildAttrs::v6KZ: 547 Triple += "v6kz"; 548 break; 549 case ARMBuildAttrs::v6T2: 550 Triple += "v6t2"; 551 break; 552 case ARMBuildAttrs::v6K: 553 Triple += "v6k"; 554 break; 555 case ARMBuildAttrs::v7: { 556 Optional<unsigned> ArchProfileAttr = 557 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile); 558 if (ArchProfileAttr && 559 ArchProfileAttr.value() == ARMBuildAttrs::MicroControllerProfile) 560 Triple += "v7m"; 561 else 562 Triple += "v7"; 563 break; 564 } 565 case ARMBuildAttrs::v6_M: 566 Triple += "v6m"; 567 break; 568 case ARMBuildAttrs::v6S_M: 569 Triple += "v6sm"; 570 break; 571 case ARMBuildAttrs::v7E_M: 572 Triple += "v7em"; 573 break; 574 case ARMBuildAttrs::v8_A: 575 Triple += "v8a"; 576 break; 577 case ARMBuildAttrs::v8_R: 578 Triple += "v8r"; 579 break; 580 case ARMBuildAttrs::v8_M_Base: 581 Triple += "v8m.base"; 582 break; 583 case ARMBuildAttrs::v8_M_Main: 584 Triple += "v8m.main"; 585 break; 586 case ARMBuildAttrs::v8_1_M_Main: 587 Triple += "v8.1m.main"; 588 break; 589 case ARMBuildAttrs::v9_A: 590 Triple += "v9a"; 591 break; 592 } 593 } 594 if (!isLittleEndian()) 595 Triple += "eb"; 596 597 TheTriple.setArchName(Triple); 598 } 599 600 std::vector<std::pair<Optional<DataRefImpl>, uint64_t>> 601 ELFObjectFileBase::getPltAddresses() const { 602 std::string Err; 603 const auto Triple = makeTriple(); 604 const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err); 605 if (!T) 606 return {}; 607 uint64_t JumpSlotReloc = 0; 608 switch (Triple.getArch()) { 609 case Triple::x86: 610 JumpSlotReloc = ELF::R_386_JUMP_SLOT; 611 break; 612 case Triple::x86_64: 613 JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT; 614 break; 615 case Triple::aarch64: 616 case Triple::aarch64_be: 617 JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT; 618 break; 619 default: 620 return {}; 621 } 622 std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo()); 623 std::unique_ptr<const MCInstrAnalysis> MIA( 624 T->createMCInstrAnalysis(MII.get())); 625 if (!MIA) 626 return {}; 627 Optional<SectionRef> Plt = None, RelaPlt = None, GotPlt = None; 628 for (const SectionRef &Section : sections()) { 629 Expected<StringRef> NameOrErr = Section.getName(); 630 if (!NameOrErr) { 631 consumeError(NameOrErr.takeError()); 632 continue; 633 } 634 StringRef Name = *NameOrErr; 635 636 if (Name == ".plt") 637 Plt = Section; 638 else if (Name == ".rela.plt" || Name == ".rel.plt") 639 RelaPlt = Section; 640 else if (Name == ".got.plt") 641 GotPlt = Section; 642 } 643 if (!Plt || !RelaPlt || !GotPlt) 644 return {}; 645 Expected<StringRef> PltContents = Plt->getContents(); 646 if (!PltContents) { 647 consumeError(PltContents.takeError()); 648 return {}; 649 } 650 auto PltEntries = MIA->findPltEntries(Plt->getAddress(), 651 arrayRefFromStringRef(*PltContents), 652 GotPlt->getAddress(), Triple); 653 // Build a map from GOT entry virtual address to PLT entry virtual address. 654 DenseMap<uint64_t, uint64_t> GotToPlt; 655 for (const auto &Entry : PltEntries) 656 GotToPlt.insert(std::make_pair(Entry.second, Entry.first)); 657 // Find the relocations in the dynamic relocation table that point to 658 // locations in the GOT for which we know the corresponding PLT entry. 659 std::vector<std::pair<Optional<DataRefImpl>, uint64_t>> Result; 660 for (const auto &Relocation : RelaPlt->relocations()) { 661 if (Relocation.getType() != JumpSlotReloc) 662 continue; 663 auto PltEntryIter = GotToPlt.find(Relocation.getOffset()); 664 if (PltEntryIter != GotToPlt.end()) { 665 symbol_iterator Sym = Relocation.getSymbol(); 666 if (Sym == symbol_end()) 667 Result.emplace_back(None, PltEntryIter->second); 668 else 669 Result.emplace_back(Sym->getRawDataRefImpl(), PltEntryIter->second); 670 } 671 } 672 return Result; 673 } 674 675 template <class ELFT> 676 Expected<std::vector<BBAddrMap>> 677 readBBAddrMapImpl(const ELFFile<ELFT> &EF, 678 Optional<unsigned> TextSectionIndex) { 679 using Elf_Shdr = typename ELFT::Shdr; 680 std::vector<BBAddrMap> BBAddrMaps; 681 const auto &Sections = cantFail(EF.sections()); 682 for (const Elf_Shdr &Sec : Sections) { 683 if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP && 684 Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP_V0) 685 continue; 686 if (TextSectionIndex) { 687 Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link); 688 if (!TextSecOrErr) 689 return createError("unable to get the linked-to section for " + 690 describe(EF, Sec) + ": " + 691 toString(TextSecOrErr.takeError())); 692 if (*TextSectionIndex != std::distance(Sections.begin(), *TextSecOrErr)) 693 continue; 694 } 695 Expected<std::vector<BBAddrMap>> BBAddrMapOrErr = EF.decodeBBAddrMap(Sec); 696 if (!BBAddrMapOrErr) 697 return createError("unable to read " + describe(EF, Sec) + ": " + 698 toString(BBAddrMapOrErr.takeError())); 699 std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(), 700 std::back_inserter(BBAddrMaps)); 701 } 702 return BBAddrMaps; 703 } 704 705 template <class ELFT> 706 static Expected<std::vector<VersionEntry>> 707 readDynsymVersionsImpl(const ELFFile<ELFT> &EF, 708 ELFObjectFileBase::elf_symbol_iterator_range Symbols) { 709 using Elf_Shdr = typename ELFT::Shdr; 710 const Elf_Shdr *VerSec = nullptr; 711 const Elf_Shdr *VerNeedSec = nullptr; 712 const Elf_Shdr *VerDefSec = nullptr; 713 // The user should ensure sections() can't fail here. 714 for (const Elf_Shdr &Sec : cantFail(EF.sections())) { 715 if (Sec.sh_type == ELF::SHT_GNU_versym) 716 VerSec = &Sec; 717 else if (Sec.sh_type == ELF::SHT_GNU_verdef) 718 VerDefSec = &Sec; 719 else if (Sec.sh_type == ELF::SHT_GNU_verneed) 720 VerNeedSec = &Sec; 721 } 722 if (!VerSec) 723 return std::vector<VersionEntry>(); 724 725 Expected<SmallVector<Optional<VersionEntry>, 0>> MapOrErr = 726 EF.loadVersionMap(VerNeedSec, VerDefSec); 727 if (!MapOrErr) 728 return MapOrErr.takeError(); 729 730 std::vector<VersionEntry> Ret; 731 size_t I = 0; 732 for (const ELFSymbolRef &Sym : Symbols) { 733 ++I; 734 Expected<const typename ELFT::Versym *> VerEntryOrErr = 735 EF.template getEntry<typename ELFT::Versym>(*VerSec, I); 736 if (!VerEntryOrErr) 737 return createError("unable to read an entry with index " + Twine(I) + 738 " from " + describe(EF, *VerSec) + ": " + 739 toString(VerEntryOrErr.takeError())); 740 741 Expected<uint32_t> FlagsOrErr = Sym.getFlags(); 742 if (!FlagsOrErr) 743 return createError("unable to read flags for symbol with index " + 744 Twine(I) + ": " + toString(FlagsOrErr.takeError())); 745 746 bool IsDefault; 747 Expected<StringRef> VerOrErr = EF.getSymbolVersionByIndex( 748 (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr, 749 (*FlagsOrErr) & SymbolRef::SF_Undefined); 750 if (!VerOrErr) 751 return createError("unable to get a version for entry " + Twine(I) + 752 " of " + describe(EF, *VerSec) + ": " + 753 toString(VerOrErr.takeError())); 754 755 Ret.push_back({(*VerOrErr).str(), IsDefault}); 756 } 757 758 return Ret; 759 } 760 761 Expected<std::vector<VersionEntry>> 762 ELFObjectFileBase::readDynsymVersions() const { 763 elf_symbol_iterator_range Symbols = getDynamicSymbolIterators(); 764 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this)) 765 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols); 766 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this)) 767 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols); 768 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this)) 769 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols); 770 return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(), 771 Symbols); 772 } 773 774 Expected<std::vector<BBAddrMap>> 775 ELFObjectFileBase::readBBAddrMap(Optional<unsigned> TextSectionIndex) const { 776 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this)) 777 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex); 778 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this)) 779 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex); 780 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this)) 781 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex); 782 if (const auto *Obj = cast<ELF64BEObjectFile>(this)) 783 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex); 784 else 785 llvm_unreachable("Unsupported binary format"); 786 } 787