1 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===// 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 // This file declares the ELFObjectFile template class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H 14 #define LLVM_OBJECT_ELFOBJECTFILE_H 15 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/ADT/iterator_range.h" 20 #include "llvm/BinaryFormat/ELF.h" 21 #include "llvm/Object/Binary.h" 22 #include "llvm/Object/ELF.h" 23 #include "llvm/Object/ELFTypes.h" 24 #include "llvm/Object/Error.h" 25 #include "llvm/Object/ObjectFile.h" 26 #include "llvm/Object/SymbolicFile.h" 27 #include "llvm/Support/Casting.h" 28 #include "llvm/Support/ELFAttributeParser.h" 29 #include "llvm/Support/ELFAttributes.h" 30 #include "llvm/Support/Endian.h" 31 #include "llvm/Support/Error.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/MemoryBufferRef.h" 34 #include "llvm/Support/ScopedPrinter.h" 35 #include "llvm/TargetParser/SubtargetFeature.h" 36 #include "llvm/TargetParser/Triple.h" 37 #include <cassert> 38 #include <cstdint> 39 40 namespace llvm { 41 42 template <typename T> class SmallVectorImpl; 43 44 namespace object { 45 46 constexpr int NumElfSymbolTypes = 16; 47 extern const llvm::EnumEntry<unsigned> ElfSymbolTypes[NumElfSymbolTypes]; 48 49 class elf_symbol_iterator; 50 51 struct ELFPltEntry { 52 StringRef Section; 53 std::optional<DataRefImpl> Symbol; 54 uint64_t Address; 55 }; 56 57 class ELFObjectFileBase : public ObjectFile { 58 friend class ELFRelocationRef; 59 friend class ELFSectionRef; 60 friend class ELFSymbolRef; 61 62 SubtargetFeatures getMIPSFeatures() const; 63 SubtargetFeatures getARMFeatures() const; 64 Expected<SubtargetFeatures> getRISCVFeatures() const; 65 SubtargetFeatures getLoongArchFeatures() const; 66 67 StringRef getAMDGPUCPUName() const; 68 69 protected: 70 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source); 71 72 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0; 73 virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0; 74 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0; 75 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0; 76 77 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0; 78 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0; 79 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0; 80 81 virtual Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0; 82 virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const = 0; 83 84 public: 85 using elf_symbol_iterator_range = iterator_range<elf_symbol_iterator>; 86 87 virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0; 88 89 /// Returns platform-specific object flags, if any. 90 virtual unsigned getPlatformFlags() const = 0; 91 92 elf_symbol_iterator_range symbols() const; 93 94 static bool classof(const Binary *v) { return v->isELF(); } 95 96 Expected<SubtargetFeatures> getFeatures() const override; 97 98 std::optional<StringRef> tryGetCPUName() const override; 99 100 void setARMSubArch(Triple &TheTriple) const override; 101 102 virtual uint16_t getEType() const = 0; 103 104 virtual uint16_t getEMachine() const = 0; 105 106 std::vector<ELFPltEntry> getPltEntries() const; 107 108 /// Returns a vector containing a symbol version for each dynamic symbol. 109 /// Returns an empty vector if version sections do not exist. 110 Expected<std::vector<VersionEntry>> readDynsymVersions() const; 111 112 /// Returns a vector of all BB address maps in the object file. When 113 // `TextSectionIndex` is specified, only returns the BB address maps 114 // corresponding to the section with that index. 115 Expected<std::vector<BBAddrMap>> 116 readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt) const; 117 }; 118 119 class ELFSectionRef : public SectionRef { 120 public: 121 ELFSectionRef(const SectionRef &B) : SectionRef(B) { 122 assert(isa<ELFObjectFileBase>(SectionRef::getObject())); 123 } 124 125 const ELFObjectFileBase *getObject() const { 126 return cast<ELFObjectFileBase>(SectionRef::getObject()); 127 } 128 129 uint32_t getType() const { 130 return getObject()->getSectionType(getRawDataRefImpl()); 131 } 132 133 uint64_t getFlags() const { 134 return getObject()->getSectionFlags(getRawDataRefImpl()); 135 } 136 137 uint64_t getOffset() const { 138 return getObject()->getSectionOffset(getRawDataRefImpl()); 139 } 140 }; 141 142 class elf_section_iterator : public section_iterator { 143 public: 144 elf_section_iterator(const section_iterator &B) : section_iterator(B) { 145 assert(isa<ELFObjectFileBase>(B->getObject())); 146 } 147 148 const ELFSectionRef *operator->() const { 149 return static_cast<const ELFSectionRef *>(section_iterator::operator->()); 150 } 151 152 const ELFSectionRef &operator*() const { 153 return static_cast<const ELFSectionRef &>(section_iterator::operator*()); 154 } 155 }; 156 157 class ELFSymbolRef : public SymbolRef { 158 public: 159 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) { 160 assert(isa<ELFObjectFileBase>(SymbolRef::getObject())); 161 } 162 163 const ELFObjectFileBase *getObject() const { 164 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject()); 165 } 166 167 uint64_t getSize() const { 168 return getObject()->getSymbolSize(getRawDataRefImpl()); 169 } 170 171 uint8_t getBinding() const { 172 return getObject()->getSymbolBinding(getRawDataRefImpl()); 173 } 174 175 uint8_t getOther() const { 176 return getObject()->getSymbolOther(getRawDataRefImpl()); 177 } 178 179 uint8_t getELFType() const { 180 return getObject()->getSymbolELFType(getRawDataRefImpl()); 181 } 182 183 StringRef getELFTypeName() const { 184 uint8_t Type = getELFType(); 185 for (const auto &EE : ElfSymbolTypes) { 186 if (EE.Value == Type) { 187 return EE.AltName; 188 } 189 } 190 return ""; 191 } 192 }; 193 194 class elf_symbol_iterator : public symbol_iterator { 195 public: 196 elf_symbol_iterator(const basic_symbol_iterator &B) 197 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(), 198 cast<ELFObjectFileBase>(B->getObject()))) {} 199 200 const ELFSymbolRef *operator->() const { 201 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->()); 202 } 203 204 const ELFSymbolRef &operator*() const { 205 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*()); 206 } 207 }; 208 209 class ELFRelocationRef : public RelocationRef { 210 public: 211 ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) { 212 assert(isa<ELFObjectFileBase>(RelocationRef::getObject())); 213 } 214 215 const ELFObjectFileBase *getObject() const { 216 return cast<ELFObjectFileBase>(RelocationRef::getObject()); 217 } 218 219 Expected<int64_t> getAddend() const { 220 return getObject()->getRelocationAddend(getRawDataRefImpl()); 221 } 222 }; 223 224 class elf_relocation_iterator : public relocation_iterator { 225 public: 226 elf_relocation_iterator(const relocation_iterator &B) 227 : relocation_iterator(RelocationRef( 228 B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {} 229 230 const ELFRelocationRef *operator->() const { 231 return static_cast<const ELFRelocationRef *>( 232 relocation_iterator::operator->()); 233 } 234 235 const ELFRelocationRef &operator*() const { 236 return static_cast<const ELFRelocationRef &>( 237 relocation_iterator::operator*()); 238 } 239 }; 240 241 inline ELFObjectFileBase::elf_symbol_iterator_range 242 ELFObjectFileBase::symbols() const { 243 return elf_symbol_iterator_range(symbol_begin(), symbol_end()); 244 } 245 246 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase { 247 uint16_t getEMachine() const override; 248 uint16_t getEType() const override; 249 uint64_t getSymbolSize(DataRefImpl Sym) const override; 250 251 public: 252 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 253 254 SectionRef toSectionRef(const Elf_Shdr *Sec) const { 255 return SectionRef(toDRI(Sec), this); 256 } 257 258 ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const { 259 return ELFSymbolRef({toDRI(SymTable, SymbolNum), this}); 260 } 261 262 bool IsContentValid() const { return ContentValid; } 263 264 private: 265 ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF, 266 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec, 267 const Elf_Shdr *DotSymtabShndxSec); 268 269 bool ContentValid = false; 270 271 protected: 272 ELFFile<ELFT> EF; 273 274 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section. 275 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section. 276 const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section. 277 278 Error initContent() override; 279 280 void moveSymbolNext(DataRefImpl &Symb) const override; 281 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override; 282 Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override; 283 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override; 284 uint32_t getSymbolAlignment(DataRefImpl Symb) const override; 285 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override; 286 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override; 287 uint8_t getSymbolBinding(DataRefImpl Symb) const override; 288 uint8_t getSymbolOther(DataRefImpl Symb) const override; 289 uint8_t getSymbolELFType(DataRefImpl Symb) const override; 290 Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override; 291 Expected<section_iterator> getSymbolSection(const Elf_Sym *Symb, 292 const Elf_Shdr *SymTab) const; 293 Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override; 294 295 void moveSectionNext(DataRefImpl &Sec) const override; 296 Expected<StringRef> getSectionName(DataRefImpl Sec) const override; 297 uint64_t getSectionAddress(DataRefImpl Sec) const override; 298 uint64_t getSectionIndex(DataRefImpl Sec) const override; 299 uint64_t getSectionSize(DataRefImpl Sec) const override; 300 Expected<ArrayRef<uint8_t>> 301 getSectionContents(DataRefImpl Sec) const override; 302 uint64_t getSectionAlignment(DataRefImpl Sec) const override; 303 bool isSectionCompressed(DataRefImpl Sec) const override; 304 bool isSectionText(DataRefImpl Sec) const override; 305 bool isSectionData(DataRefImpl Sec) const override; 306 bool isSectionBSS(DataRefImpl Sec) const override; 307 bool isSectionVirtual(DataRefImpl Sec) const override; 308 bool isBerkeleyText(DataRefImpl Sec) const override; 309 bool isBerkeleyData(DataRefImpl Sec) const override; 310 bool isDebugSection(DataRefImpl Sec) const override; 311 relocation_iterator section_rel_begin(DataRefImpl Sec) const override; 312 relocation_iterator section_rel_end(DataRefImpl Sec) const override; 313 std::vector<SectionRef> dynamic_relocation_sections() const override; 314 Expected<section_iterator> 315 getRelocatedSection(DataRefImpl Sec) const override; 316 317 void moveRelocationNext(DataRefImpl &Rel) const override; 318 uint64_t getRelocationOffset(DataRefImpl Rel) const override; 319 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override; 320 uint64_t getRelocationType(DataRefImpl Rel) const override; 321 void getRelocationTypeName(DataRefImpl Rel, 322 SmallVectorImpl<char> &Result) const override; 323 324 uint32_t getSectionType(DataRefImpl Sec) const override; 325 uint64_t getSectionFlags(DataRefImpl Sec) const override; 326 uint64_t getSectionOffset(DataRefImpl Sec) const override; 327 StringRef getRelocationTypeName(uint32_t Type) const; 328 329 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const { 330 DataRefImpl DRI; 331 if (!SymTable) { 332 DRI.d.a = 0; 333 DRI.d.b = 0; 334 return DRI; 335 } 336 assert(SymTable->sh_type == ELF::SHT_SYMTAB || 337 SymTable->sh_type == ELF::SHT_DYNSYM); 338 339 auto SectionsOrErr = EF.sections(); 340 if (!SectionsOrErr) { 341 DRI.d.a = 0; 342 DRI.d.b = 0; 343 return DRI; 344 } 345 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin()); 346 unsigned SymTableIndex = 347 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr); 348 349 DRI.d.a = SymTableIndex; 350 DRI.d.b = SymbolNum; 351 return DRI; 352 } 353 354 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const { 355 return reinterpret_cast<const Elf_Shdr *>(Sec.p); 356 } 357 358 DataRefImpl toDRI(const Elf_Shdr *Sec) const { 359 DataRefImpl DRI; 360 DRI.p = reinterpret_cast<uintptr_t>(Sec); 361 return DRI; 362 } 363 364 DataRefImpl toDRI(const Elf_Dyn *Dyn) const { 365 DataRefImpl DRI; 366 DRI.p = reinterpret_cast<uintptr_t>(Dyn); 367 return DRI; 368 } 369 370 bool isExportedToOtherDSO(const Elf_Sym *ESym) const { 371 unsigned char Binding = ESym->getBinding(); 372 unsigned char Visibility = ESym->getVisibility(); 373 374 // A symbol is exported if its binding is either GLOBAL or WEAK, and its 375 // visibility is either DEFAULT or PROTECTED. All other symbols are not 376 // exported. 377 return ( 378 (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK || 379 Binding == ELF::STB_GNU_UNIQUE) && 380 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED)); 381 } 382 383 Error getBuildAttributes(ELFAttributeParser &Attributes) const override { 384 auto SectionsOrErr = EF.sections(); 385 if (!SectionsOrErr) 386 return SectionsOrErr.takeError(); 387 388 for (const Elf_Shdr &Sec : *SectionsOrErr) { 389 if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES || 390 Sec.sh_type == ELF::SHT_RISCV_ATTRIBUTES) { 391 auto ErrorOrContents = EF.getSectionContents(Sec); 392 if (!ErrorOrContents) 393 return ErrorOrContents.takeError(); 394 395 auto Contents = ErrorOrContents.get(); 396 if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1) 397 return Error::success(); 398 399 if (Error E = Attributes.parse(Contents, ELFT::TargetEndianness)) 400 return E; 401 break; 402 } 403 } 404 return Error::success(); 405 } 406 407 // This flag is used for classof, to distinguish ELFObjectFile from 408 // its subclass. If more subclasses will be created, this flag will 409 // have to become an enum. 410 bool isDyldELFObject = false; 411 412 public: 413 ELFObjectFile(ELFObjectFile<ELFT> &&Other); 414 static Expected<ELFObjectFile<ELFT>> create(MemoryBufferRef Object, 415 bool InitContent = true); 416 417 const Elf_Rel *getRel(DataRefImpl Rel) const; 418 const Elf_Rela *getRela(DataRefImpl Rela) const; 419 420 Expected<const Elf_Sym *> getSymbol(DataRefImpl Sym) const { 421 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b); 422 } 423 424 /// Get the relocation section that contains \a Rel. 425 const Elf_Shdr *getRelSection(DataRefImpl Rel) const { 426 auto RelSecOrErr = EF.getSection(Rel.d.a); 427 if (!RelSecOrErr) 428 report_fatal_error( 429 Twine(errorToErrorCode(RelSecOrErr.takeError()).message())); 430 return *RelSecOrErr; 431 } 432 433 const Elf_Shdr *getSection(DataRefImpl Sec) const { 434 return reinterpret_cast<const Elf_Shdr *>(Sec.p); 435 } 436 437 basic_symbol_iterator symbol_begin() const override; 438 basic_symbol_iterator symbol_end() const override; 439 440 bool is64Bit() const override { return getBytesInAddress() == 8; } 441 442 elf_symbol_iterator dynamic_symbol_begin() const; 443 elf_symbol_iterator dynamic_symbol_end() const; 444 445 section_iterator section_begin() const override; 446 section_iterator section_end() const override; 447 448 Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const override; 449 450 uint8_t getBytesInAddress() const override; 451 StringRef getFileFormatName() const override; 452 Triple::ArchType getArch() const override; 453 Expected<uint64_t> getStartAddress() const override; 454 455 unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; } 456 457 const ELFFile<ELFT> &getELFFile() const { return EF; } 458 459 bool isDyldType() const { return isDyldELFObject; } 460 static bool classof(const Binary *v) { 461 return v->getType() == getELFType(ELFT::TargetEndianness == support::little, 462 ELFT::Is64Bits); 463 } 464 465 elf_symbol_iterator_range getDynamicSymbolIterators() const override; 466 467 bool isRelocatableObject() const override; 468 469 void createFakeSections() { EF.createFakeSections(); } 470 }; 471 472 using ELF32LEObjectFile = ELFObjectFile<ELF32LE>; 473 using ELF64LEObjectFile = ELFObjectFile<ELF64LE>; 474 using ELF32BEObjectFile = ELFObjectFile<ELF32BE>; 475 using ELF64BEObjectFile = ELFObjectFile<ELF64BE>; 476 477 template <class ELFT> 478 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const { 479 ++Sym.d.b; 480 } 481 482 template <class ELFT> Error ELFObjectFile<ELFT>::initContent() { 483 auto SectionsOrErr = EF.sections(); 484 if (!SectionsOrErr) 485 return SectionsOrErr.takeError(); 486 487 for (const Elf_Shdr &Sec : *SectionsOrErr) { 488 switch (Sec.sh_type) { 489 case ELF::SHT_DYNSYM: { 490 if (!DotDynSymSec) 491 DotDynSymSec = &Sec; 492 break; 493 } 494 case ELF::SHT_SYMTAB: { 495 if (!DotSymtabSec) 496 DotSymtabSec = &Sec; 497 break; 498 } 499 case ELF::SHT_SYMTAB_SHNDX: { 500 if (!DotSymtabShndxSec) 501 DotSymtabShndxSec = &Sec; 502 break; 503 } 504 } 505 } 506 507 ContentValid = true; 508 return Error::success(); 509 } 510 511 template <class ELFT> 512 Expected<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const { 513 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym); 514 if (!SymOrErr) 515 return SymOrErr.takeError(); 516 auto SymTabOrErr = EF.getSection(Sym.d.a); 517 if (!SymTabOrErr) 518 return SymTabOrErr.takeError(); 519 const Elf_Shdr *SymTableSec = *SymTabOrErr; 520 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link); 521 if (!StrTabOrErr) 522 return StrTabOrErr.takeError(); 523 const Elf_Shdr *StringTableSec = *StrTabOrErr; 524 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec); 525 if (!SymStrTabOrErr) 526 return SymStrTabOrErr.takeError(); 527 Expected<StringRef> Name = (*SymOrErr)->getName(*SymStrTabOrErr); 528 if (Name && !Name->empty()) 529 return Name; 530 531 // If the symbol name is empty use the section name. 532 if ((*SymOrErr)->getType() == ELF::STT_SECTION) { 533 Expected<section_iterator> SecOrErr = getSymbolSection(Sym); 534 if (SecOrErr) 535 return (*SecOrErr)->getName(); 536 return SecOrErr.takeError(); 537 } 538 return Name; 539 } 540 541 template <class ELFT> 542 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const { 543 return getSection(Sec)->sh_flags; 544 } 545 546 template <class ELFT> 547 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const { 548 return getSection(Sec)->sh_type; 549 } 550 551 template <class ELFT> 552 uint64_t ELFObjectFile<ELFT>::getSectionOffset(DataRefImpl Sec) const { 553 return getSection(Sec)->sh_offset; 554 } 555 556 template <class ELFT> 557 uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const { 558 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb); 559 if (!SymOrErr) 560 report_fatal_error(SymOrErr.takeError()); 561 562 uint64_t Ret = (*SymOrErr)->st_value; 563 if ((*SymOrErr)->st_shndx == ELF::SHN_ABS) 564 return Ret; 565 566 const Elf_Ehdr &Header = EF.getHeader(); 567 // Clear the ARM/Thumb or microMIPS indicator flag. 568 if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) && 569 (*SymOrErr)->getType() == ELF::STT_FUNC) 570 Ret &= ~1; 571 572 return Ret; 573 } 574 575 template <class ELFT> 576 Expected<uint64_t> 577 ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const { 578 Expected<uint64_t> SymbolValueOrErr = getSymbolValue(Symb); 579 if (!SymbolValueOrErr) 580 // TODO: Test this error. 581 return SymbolValueOrErr.takeError(); 582 583 uint64_t Result = *SymbolValueOrErr; 584 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb); 585 if (!SymOrErr) 586 return SymOrErr.takeError(); 587 588 switch ((*SymOrErr)->st_shndx) { 589 case ELF::SHN_COMMON: 590 case ELF::SHN_UNDEF: 591 case ELF::SHN_ABS: 592 return Result; 593 } 594 595 auto SymTabOrErr = EF.getSection(Symb.d.a); 596 if (!SymTabOrErr) 597 return SymTabOrErr.takeError(); 598 599 if (EF.getHeader().e_type == ELF::ET_REL) { 600 ArrayRef<Elf_Word> ShndxTable; 601 if (DotSymtabShndxSec) { 602 // TODO: Test this error. 603 if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr = 604 EF.getSHNDXTable(*DotSymtabShndxSec)) 605 ShndxTable = *ShndxTableOrErr; 606 else 607 return ShndxTableOrErr.takeError(); 608 } 609 610 Expected<const Elf_Shdr *> SectionOrErr = 611 EF.getSection(**SymOrErr, *SymTabOrErr, ShndxTable); 612 if (!SectionOrErr) 613 return SectionOrErr.takeError(); 614 const Elf_Shdr *Section = *SectionOrErr; 615 if (Section) 616 Result += Section->sh_addr; 617 } 618 619 return Result; 620 } 621 622 template <class ELFT> 623 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const { 624 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb); 625 if (!SymOrErr) 626 report_fatal_error(SymOrErr.takeError()); 627 if ((*SymOrErr)->st_shndx == ELF::SHN_COMMON) 628 return (*SymOrErr)->st_value; 629 return 0; 630 } 631 632 template <class ELFT> 633 uint16_t ELFObjectFile<ELFT>::getEMachine() const { 634 return EF.getHeader().e_machine; 635 } 636 637 template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const { 638 return EF.getHeader().e_type; 639 } 640 641 template <class ELFT> 642 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const { 643 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym); 644 if (!SymOrErr) 645 report_fatal_error(SymOrErr.takeError()); 646 return (*SymOrErr)->st_size; 647 } 648 649 template <class ELFT> 650 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const { 651 return getSymbolSize(Symb); 652 } 653 654 template <class ELFT> 655 uint8_t ELFObjectFile<ELFT>::getSymbolBinding(DataRefImpl Symb) const { 656 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb); 657 if (!SymOrErr) 658 report_fatal_error(SymOrErr.takeError()); 659 return (*SymOrErr)->getBinding(); 660 } 661 662 template <class ELFT> 663 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const { 664 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb); 665 if (!SymOrErr) 666 report_fatal_error(SymOrErr.takeError()); 667 return (*SymOrErr)->st_other; 668 } 669 670 template <class ELFT> 671 uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const { 672 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb); 673 if (!SymOrErr) 674 report_fatal_error(SymOrErr.takeError()); 675 return (*SymOrErr)->getType(); 676 } 677 678 template <class ELFT> 679 Expected<SymbolRef::Type> 680 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const { 681 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb); 682 if (!SymOrErr) 683 return SymOrErr.takeError(); 684 685 switch ((*SymOrErr)->getType()) { 686 case ELF::STT_NOTYPE: 687 return SymbolRef::ST_Unknown; 688 case ELF::STT_SECTION: 689 return SymbolRef::ST_Debug; 690 case ELF::STT_FILE: 691 return SymbolRef::ST_File; 692 case ELF::STT_FUNC: 693 return SymbolRef::ST_Function; 694 case ELF::STT_OBJECT: 695 case ELF::STT_COMMON: 696 return SymbolRef::ST_Data; 697 case ELF::STT_TLS: 698 default: 699 return SymbolRef::ST_Other; 700 } 701 } 702 703 template <class ELFT> 704 Expected<uint32_t> ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const { 705 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym); 706 if (!SymOrErr) 707 return SymOrErr.takeError(); 708 709 const Elf_Sym *ESym = *SymOrErr; 710 uint32_t Result = SymbolRef::SF_None; 711 712 if (ESym->getBinding() != ELF::STB_LOCAL) 713 Result |= SymbolRef::SF_Global; 714 715 if (ESym->getBinding() == ELF::STB_WEAK) 716 Result |= SymbolRef::SF_Weak; 717 718 if (ESym->st_shndx == ELF::SHN_ABS) 719 Result |= SymbolRef::SF_Absolute; 720 721 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION) 722 Result |= SymbolRef::SF_FormatSpecific; 723 724 if (Expected<typename ELFT::SymRange> SymbolsOrErr = 725 EF.symbols(DotSymtabSec)) { 726 // Set the SF_FormatSpecific flag for the 0-index null symbol. 727 if (ESym == SymbolsOrErr->begin()) 728 Result |= SymbolRef::SF_FormatSpecific; 729 } else 730 // TODO: Test this error. 731 return SymbolsOrErr.takeError(); 732 733 if (Expected<typename ELFT::SymRange> SymbolsOrErr = 734 EF.symbols(DotDynSymSec)) { 735 // Set the SF_FormatSpecific flag for the 0-index null symbol. 736 if (ESym == SymbolsOrErr->begin()) 737 Result |= SymbolRef::SF_FormatSpecific; 738 } else 739 // TODO: Test this error. 740 return SymbolsOrErr.takeError(); 741 742 if (EF.getHeader().e_machine == ELF::EM_AARCH64) { 743 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) { 744 StringRef Name = *NameOrErr; 745 if (Name.startswith("$d") || Name.startswith("$x")) 746 Result |= SymbolRef::SF_FormatSpecific; 747 } else { 748 // TODO: Actually report errors helpfully. 749 consumeError(NameOrErr.takeError()); 750 } 751 } else if (EF.getHeader().e_machine == ELF::EM_ARM) { 752 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) { 753 StringRef Name = *NameOrErr; 754 // TODO Investigate why empty name symbols need to be marked. 755 if (Name.empty() || Name.startswith("$d") || Name.startswith("$t") || 756 Name.startswith("$a")) 757 Result |= SymbolRef::SF_FormatSpecific; 758 } else { 759 // TODO: Actually report errors helpfully. 760 consumeError(NameOrErr.takeError()); 761 } 762 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1) 763 Result |= SymbolRef::SF_Thumb; 764 } else if (EF.getHeader().e_machine == ELF::EM_RISCV) { 765 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) { 766 // Mark empty name symbols used for label differences. 767 if (NameOrErr->empty()) 768 Result |= SymbolRef::SF_FormatSpecific; 769 } else { 770 // TODO: Actually report errors helpfully. 771 consumeError(NameOrErr.takeError()); 772 } 773 } 774 775 if (ESym->st_shndx == ELF::SHN_UNDEF) 776 Result |= SymbolRef::SF_Undefined; 777 778 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON) 779 Result |= SymbolRef::SF_Common; 780 781 if (isExportedToOtherDSO(ESym)) 782 Result |= SymbolRef::SF_Exported; 783 784 if (ESym->getType() == ELF::STT_GNU_IFUNC) 785 Result |= SymbolRef::SF_Indirect; 786 787 if (ESym->getVisibility() == ELF::STV_HIDDEN) 788 Result |= SymbolRef::SF_Hidden; 789 790 return Result; 791 } 792 793 template <class ELFT> 794 Expected<section_iterator> 795 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym, 796 const Elf_Shdr *SymTab) const { 797 ArrayRef<Elf_Word> ShndxTable; 798 if (DotSymtabShndxSec) { 799 // TODO: Test this error. 800 Expected<ArrayRef<Elf_Word>> ShndxTableOrErr = 801 EF.getSHNDXTable(*DotSymtabShndxSec); 802 if (!ShndxTableOrErr) 803 return ShndxTableOrErr.takeError(); 804 ShndxTable = *ShndxTableOrErr; 805 } 806 807 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable); 808 if (!ESecOrErr) 809 return ESecOrErr.takeError(); 810 811 const Elf_Shdr *ESec = *ESecOrErr; 812 if (!ESec) 813 return section_end(); 814 815 DataRefImpl Sec; 816 Sec.p = reinterpret_cast<intptr_t>(ESec); 817 return section_iterator(SectionRef(Sec, this)); 818 } 819 820 template <class ELFT> 821 Expected<section_iterator> 822 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const { 823 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb); 824 if (!SymOrErr) 825 return SymOrErr.takeError(); 826 827 auto SymTabOrErr = EF.getSection(Symb.d.a); 828 if (!SymTabOrErr) 829 return SymTabOrErr.takeError(); 830 return getSymbolSection(*SymOrErr, *SymTabOrErr); 831 } 832 833 template <class ELFT> 834 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const { 835 const Elf_Shdr *ESec = getSection(Sec); 836 Sec = toDRI(++ESec); 837 } 838 839 template <class ELFT> 840 Expected<StringRef> ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec) const { 841 return EF.getSectionName(*getSection(Sec)); 842 } 843 844 template <class ELFT> 845 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const { 846 return getSection(Sec)->sh_addr; 847 } 848 849 template <class ELFT> 850 uint64_t ELFObjectFile<ELFT>::getSectionIndex(DataRefImpl Sec) const { 851 auto SectionsOrErr = EF.sections(); 852 handleAllErrors(std::move(SectionsOrErr.takeError()), 853 [](const ErrorInfoBase &) { 854 llvm_unreachable("unable to get section index"); 855 }); 856 const Elf_Shdr *First = SectionsOrErr->begin(); 857 return getSection(Sec) - First; 858 } 859 860 template <class ELFT> 861 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const { 862 return getSection(Sec)->sh_size; 863 } 864 865 template <class ELFT> 866 Expected<ArrayRef<uint8_t>> 867 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec) const { 868 const Elf_Shdr *EShdr = getSection(Sec); 869 if (EShdr->sh_type == ELF::SHT_NOBITS) 870 return ArrayRef((const uint8_t *)base(), (size_t)0); 871 if (Error E = 872 checkOffset(getMemoryBufferRef(), 873 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size)) 874 return std::move(E); 875 return ArrayRef((const uint8_t *)base() + EShdr->sh_offset, EShdr->sh_size); 876 } 877 878 template <class ELFT> 879 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const { 880 return getSection(Sec)->sh_addralign; 881 } 882 883 template <class ELFT> 884 bool ELFObjectFile<ELFT>::isSectionCompressed(DataRefImpl Sec) const { 885 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED; 886 } 887 888 template <class ELFT> 889 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const { 890 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR; 891 } 892 893 template <class ELFT> 894 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const { 895 const Elf_Shdr *EShdr = getSection(Sec); 896 return EShdr->sh_type == ELF::SHT_PROGBITS && 897 EShdr->sh_flags & ELF::SHF_ALLOC && 898 !(EShdr->sh_flags & ELF::SHF_EXECINSTR); 899 } 900 901 template <class ELFT> 902 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const { 903 const Elf_Shdr *EShdr = getSection(Sec); 904 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) && 905 EShdr->sh_type == ELF::SHT_NOBITS; 906 } 907 908 template <class ELFT> 909 std::vector<SectionRef> 910 ELFObjectFile<ELFT>::dynamic_relocation_sections() const { 911 std::vector<SectionRef> Res; 912 std::vector<uintptr_t> Offsets; 913 914 auto SectionsOrErr = EF.sections(); 915 if (!SectionsOrErr) 916 return Res; 917 918 for (const Elf_Shdr &Sec : *SectionsOrErr) { 919 if (Sec.sh_type != ELF::SHT_DYNAMIC) 920 continue; 921 Elf_Dyn *Dynamic = 922 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset); 923 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) { 924 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA || 925 Dynamic->d_tag == ELF::DT_JMPREL) { 926 Offsets.push_back(Dynamic->d_un.d_val); 927 } 928 } 929 } 930 for (const Elf_Shdr &Sec : *SectionsOrErr) { 931 if (is_contained(Offsets, Sec.sh_addr)) 932 Res.emplace_back(toDRI(&Sec), this); 933 } 934 return Res; 935 } 936 937 template <class ELFT> 938 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const { 939 return getSection(Sec)->sh_type == ELF::SHT_NOBITS; 940 } 941 942 template <class ELFT> 943 bool ELFObjectFile<ELFT>::isBerkeleyText(DataRefImpl Sec) const { 944 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC && 945 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR || 946 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE)); 947 } 948 949 template <class ELFT> 950 bool ELFObjectFile<ELFT>::isBerkeleyData(DataRefImpl Sec) const { 951 const Elf_Shdr *EShdr = getSection(Sec); 952 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS && 953 EShdr->sh_flags & ELF::SHF_ALLOC; 954 } 955 956 template <class ELFT> 957 bool ELFObjectFile<ELFT>::isDebugSection(DataRefImpl Sec) const { 958 Expected<StringRef> SectionNameOrErr = getSectionName(Sec); 959 if (!SectionNameOrErr) { 960 // TODO: Report the error message properly. 961 consumeError(SectionNameOrErr.takeError()); 962 return false; 963 } 964 StringRef SectionName = SectionNameOrErr.get(); 965 return SectionName.startswith(".debug") || 966 SectionName.startswith(".zdebug") || SectionName == ".gdb_index"; 967 } 968 969 template <class ELFT> 970 relocation_iterator 971 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const { 972 DataRefImpl RelData; 973 auto SectionsOrErr = EF.sections(); 974 if (!SectionsOrErr) 975 return relocation_iterator(RelocationRef()); 976 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin()); 977 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize; 978 RelData.d.b = 0; 979 return relocation_iterator(RelocationRef(RelData, this)); 980 } 981 982 template <class ELFT> 983 relocation_iterator 984 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const { 985 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p); 986 relocation_iterator Begin = section_rel_begin(Sec); 987 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL) 988 return Begin; 989 DataRefImpl RelData = Begin->getRawDataRefImpl(); 990 const Elf_Shdr *RelSec = getRelSection(RelData); 991 992 // Error check sh_link here so that getRelocationSymbol can just use it. 993 auto SymSecOrErr = EF.getSection(RelSec->sh_link); 994 if (!SymSecOrErr) 995 report_fatal_error( 996 Twine(errorToErrorCode(SymSecOrErr.takeError()).message())); 997 998 RelData.d.b += S->sh_size / S->sh_entsize; 999 return relocation_iterator(RelocationRef(RelData, this)); 1000 } 1001 1002 template <class ELFT> 1003 Expected<section_iterator> 1004 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const { 1005 const Elf_Shdr *EShdr = getSection(Sec); 1006 uintX_t Type = EShdr->sh_type; 1007 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA) 1008 return section_end(); 1009 1010 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info); 1011 if (!SecOrErr) 1012 return SecOrErr.takeError(); 1013 return section_iterator(SectionRef(toDRI(*SecOrErr), this)); 1014 } 1015 1016 // Relocations 1017 template <class ELFT> 1018 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const { 1019 ++Rel.d.b; 1020 } 1021 1022 template <class ELFT> 1023 symbol_iterator 1024 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const { 1025 uint32_t symbolIdx; 1026 const Elf_Shdr *sec = getRelSection(Rel); 1027 if (sec->sh_type == ELF::SHT_REL) 1028 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL()); 1029 else 1030 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL()); 1031 if (!symbolIdx) 1032 return symbol_end(); 1033 1034 // FIXME: error check symbolIdx 1035 DataRefImpl SymbolData; 1036 SymbolData.d.a = sec->sh_link; 1037 SymbolData.d.b = symbolIdx; 1038 return symbol_iterator(SymbolRef(SymbolData, this)); 1039 } 1040 1041 template <class ELFT> 1042 uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const { 1043 const Elf_Shdr *sec = getRelSection(Rel); 1044 if (sec->sh_type == ELF::SHT_REL) 1045 return getRel(Rel)->r_offset; 1046 1047 return getRela(Rel)->r_offset; 1048 } 1049 1050 template <class ELFT> 1051 uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const { 1052 const Elf_Shdr *sec = getRelSection(Rel); 1053 if (sec->sh_type == ELF::SHT_REL) 1054 return getRel(Rel)->getType(EF.isMips64EL()); 1055 else 1056 return getRela(Rel)->getType(EF.isMips64EL()); 1057 } 1058 1059 template <class ELFT> 1060 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const { 1061 return getELFRelocationTypeName(EF.getHeader().e_machine, Type); 1062 } 1063 1064 template <class ELFT> 1065 void ELFObjectFile<ELFT>::getRelocationTypeName( 1066 DataRefImpl Rel, SmallVectorImpl<char> &Result) const { 1067 uint32_t type = getRelocationType(Rel); 1068 EF.getRelocationTypeName(type, Result); 1069 } 1070 1071 template <class ELFT> 1072 Expected<int64_t> 1073 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const { 1074 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA) 1075 return createError("Section is not SHT_RELA"); 1076 return (int64_t)getRela(Rel)->r_addend; 1077 } 1078 1079 template <class ELFT> 1080 const typename ELFObjectFile<ELFT>::Elf_Rel * 1081 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const { 1082 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL); 1083 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b); 1084 if (!Ret) 1085 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message())); 1086 return *Ret; 1087 } 1088 1089 template <class ELFT> 1090 const typename ELFObjectFile<ELFT>::Elf_Rela * 1091 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const { 1092 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA); 1093 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b); 1094 if (!Ret) 1095 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message())); 1096 return *Ret; 1097 } 1098 1099 template <class ELFT> 1100 Expected<ELFObjectFile<ELFT>> 1101 ELFObjectFile<ELFT>::create(MemoryBufferRef Object, bool InitContent) { 1102 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer()); 1103 if (Error E = EFOrErr.takeError()) 1104 return std::move(E); 1105 1106 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr, 1107 nullptr}; 1108 if (InitContent) 1109 if (Error E = Obj.initContent()) 1110 return std::move(E); 1111 return std::move(Obj); 1112 } 1113 1114 template <class ELFT> 1115 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF, 1116 const Elf_Shdr *DotDynSymSec, 1117 const Elf_Shdr *DotSymtabSec, 1118 const Elf_Shdr *DotSymtabShndx) 1119 : ELFObjectFileBase( 1120 getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits), 1121 Object), 1122 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec), 1123 DotSymtabShndxSec(DotSymtabShndx) {} 1124 1125 template <class ELFT> 1126 ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other) 1127 : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec, 1128 Other.DotSymtabSec, Other.DotSymtabShndxSec) {} 1129 1130 template <class ELFT> 1131 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin() const { 1132 DataRefImpl Sym = 1133 toDRI(DotSymtabSec, 1134 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0); 1135 return basic_symbol_iterator(SymbolRef(Sym, this)); 1136 } 1137 1138 template <class ELFT> 1139 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end() const { 1140 const Elf_Shdr *SymTab = DotSymtabSec; 1141 if (!SymTab) 1142 return symbol_begin(); 1143 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym)); 1144 return basic_symbol_iterator(SymbolRef(Sym, this)); 1145 } 1146 1147 template <class ELFT> 1148 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const { 1149 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym)) 1150 // Ignore errors here where the dynsym is empty or sh_size less than the 1151 // size of one symbol. These should be handled elsewhere. 1152 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this)); 1153 // Skip 0-index NULL symbol. 1154 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this)); 1155 } 1156 1157 template <class ELFT> 1158 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const { 1159 const Elf_Shdr *SymTab = DotDynSymSec; 1160 if (!SymTab) 1161 return dynamic_symbol_begin(); 1162 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym)); 1163 return basic_symbol_iterator(SymbolRef(Sym, this)); 1164 } 1165 1166 template <class ELFT> 1167 section_iterator ELFObjectFile<ELFT>::section_begin() const { 1168 auto SectionsOrErr = EF.sections(); 1169 if (!SectionsOrErr) 1170 return section_iterator(SectionRef()); 1171 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this)); 1172 } 1173 1174 template <class ELFT> 1175 section_iterator ELFObjectFile<ELFT>::section_end() const { 1176 auto SectionsOrErr = EF.sections(); 1177 if (!SectionsOrErr) 1178 return section_iterator(SectionRef()); 1179 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this)); 1180 } 1181 1182 template <class ELFT> 1183 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const { 1184 return ELFT::Is64Bits ? 8 : 4; 1185 } 1186 1187 template <class ELFT> 1188 StringRef ELFObjectFile<ELFT>::getFileFormatName() const { 1189 constexpr bool IsLittleEndian = ELFT::TargetEndianness == support::little; 1190 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) { 1191 case ELF::ELFCLASS32: 1192 switch (EF.getHeader().e_machine) { 1193 case ELF::EM_68K: 1194 return "elf32-m68k"; 1195 case ELF::EM_386: 1196 return "elf32-i386"; 1197 case ELF::EM_IAMCU: 1198 return "elf32-iamcu"; 1199 case ELF::EM_X86_64: 1200 return "elf32-x86-64"; 1201 case ELF::EM_ARM: 1202 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm"); 1203 case ELF::EM_AVR: 1204 return "elf32-avr"; 1205 case ELF::EM_HEXAGON: 1206 return "elf32-hexagon"; 1207 case ELF::EM_LANAI: 1208 return "elf32-lanai"; 1209 case ELF::EM_MIPS: 1210 return "elf32-mips"; 1211 case ELF::EM_MSP430: 1212 return "elf32-msp430"; 1213 case ELF::EM_PPC: 1214 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc"); 1215 case ELF::EM_RISCV: 1216 return "elf32-littleriscv"; 1217 case ELF::EM_CSKY: 1218 return "elf32-csky"; 1219 case ELF::EM_SPARC: 1220 case ELF::EM_SPARC32PLUS: 1221 return "elf32-sparc"; 1222 case ELF::EM_AMDGPU: 1223 return "elf32-amdgpu"; 1224 case ELF::EM_LOONGARCH: 1225 return "elf32-loongarch"; 1226 case ELF::EM_XTENSA: 1227 return "elf32-xtensa"; 1228 default: 1229 return "elf32-unknown"; 1230 } 1231 case ELF::ELFCLASS64: 1232 switch (EF.getHeader().e_machine) { 1233 case ELF::EM_386: 1234 return "elf64-i386"; 1235 case ELF::EM_X86_64: 1236 return "elf64-x86-64"; 1237 case ELF::EM_AARCH64: 1238 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64"); 1239 case ELF::EM_PPC64: 1240 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc"); 1241 case ELF::EM_RISCV: 1242 return "elf64-littleriscv"; 1243 case ELF::EM_S390: 1244 return "elf64-s390"; 1245 case ELF::EM_SPARCV9: 1246 return "elf64-sparc"; 1247 case ELF::EM_MIPS: 1248 return "elf64-mips"; 1249 case ELF::EM_AMDGPU: 1250 return "elf64-amdgpu"; 1251 case ELF::EM_BPF: 1252 return "elf64-bpf"; 1253 case ELF::EM_VE: 1254 return "elf64-ve"; 1255 case ELF::EM_LOONGARCH: 1256 return "elf64-loongarch"; 1257 default: 1258 return "elf64-unknown"; 1259 } 1260 default: 1261 // FIXME: Proper error handling. 1262 report_fatal_error("Invalid ELFCLASS!"); 1263 } 1264 } 1265 1266 template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const { 1267 bool IsLittleEndian = ELFT::TargetEndianness == support::little; 1268 switch (EF.getHeader().e_machine) { 1269 case ELF::EM_68K: 1270 return Triple::m68k; 1271 case ELF::EM_386: 1272 case ELF::EM_IAMCU: 1273 return Triple::x86; 1274 case ELF::EM_X86_64: 1275 return Triple::x86_64; 1276 case ELF::EM_AARCH64: 1277 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be; 1278 case ELF::EM_ARM: 1279 return Triple::arm; 1280 case ELF::EM_AVR: 1281 return Triple::avr; 1282 case ELF::EM_HEXAGON: 1283 return Triple::hexagon; 1284 case ELF::EM_LANAI: 1285 return Triple::lanai; 1286 case ELF::EM_MIPS: 1287 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) { 1288 case ELF::ELFCLASS32: 1289 return IsLittleEndian ? Triple::mipsel : Triple::mips; 1290 case ELF::ELFCLASS64: 1291 return IsLittleEndian ? Triple::mips64el : Triple::mips64; 1292 default: 1293 report_fatal_error("Invalid ELFCLASS!"); 1294 } 1295 case ELF::EM_MSP430: 1296 return Triple::msp430; 1297 case ELF::EM_PPC: 1298 return IsLittleEndian ? Triple::ppcle : Triple::ppc; 1299 case ELF::EM_PPC64: 1300 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64; 1301 case ELF::EM_RISCV: 1302 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) { 1303 case ELF::ELFCLASS32: 1304 return Triple::riscv32; 1305 case ELF::ELFCLASS64: 1306 return Triple::riscv64; 1307 default: 1308 report_fatal_error("Invalid ELFCLASS!"); 1309 } 1310 case ELF::EM_S390: 1311 return Triple::systemz; 1312 1313 case ELF::EM_SPARC: 1314 case ELF::EM_SPARC32PLUS: 1315 return IsLittleEndian ? Triple::sparcel : Triple::sparc; 1316 case ELF::EM_SPARCV9: 1317 return Triple::sparcv9; 1318 1319 case ELF::EM_AMDGPU: { 1320 if (!IsLittleEndian) 1321 return Triple::UnknownArch; 1322 1323 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH; 1324 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST && 1325 MACH <= ELF::EF_AMDGPU_MACH_R600_LAST) 1326 return Triple::r600; 1327 if (MACH >= ELF::EF_AMDGPU_MACH_AMDGCN_FIRST && 1328 MACH <= ELF::EF_AMDGPU_MACH_AMDGCN_LAST) 1329 return Triple::amdgcn; 1330 1331 return Triple::UnknownArch; 1332 } 1333 1334 case ELF::EM_BPF: 1335 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb; 1336 1337 case ELF::EM_VE: 1338 return Triple::ve; 1339 case ELF::EM_CSKY: 1340 return Triple::csky; 1341 1342 case ELF::EM_LOONGARCH: 1343 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) { 1344 case ELF::ELFCLASS32: 1345 return Triple::loongarch32; 1346 case ELF::ELFCLASS64: 1347 return Triple::loongarch64; 1348 default: 1349 report_fatal_error("Invalid ELFCLASS!"); 1350 } 1351 1352 case ELF::EM_XTENSA: 1353 return Triple::xtensa; 1354 1355 default: 1356 return Triple::UnknownArch; 1357 } 1358 } 1359 1360 template <class ELFT> 1361 Expected<uint64_t> ELFObjectFile<ELFT>::getStartAddress() const { 1362 return EF.getHeader().e_entry; 1363 } 1364 1365 template <class ELFT> 1366 ELFObjectFileBase::elf_symbol_iterator_range 1367 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const { 1368 return make_range(dynamic_symbol_begin(), dynamic_symbol_end()); 1369 } 1370 1371 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const { 1372 return EF.getHeader().e_type == ELF::ET_REL; 1373 } 1374 1375 } // end namespace object 1376 } // end namespace llvm 1377 1378 #endif // LLVM_OBJECT_ELFOBJECTFILE_H 1379