1 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the ELFObjectFile template class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Object/ELF.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
38 class ELFObjectFileBase : public ObjectFile {
40 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
43 virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
45 // FIXME: This is a bit of a hack. Every caller should know if it expecting
47 virtual bool hasRelocationAddend(DataRefImpl Rel) const = 0;
49 virtual std::pair<symbol_iterator, symbol_iterator>
50 getELFDynamicSymbolIterators() const = 0;
52 virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
53 virtual uint32_t getSectionType(SectionRef Sec) const = 0;
55 virtual uint64_t getSymbolSize(SymbolRef Symb) const = 0;
57 static inline bool classof(const Binary *v) { return v->isELF(); }
60 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
62 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
64 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
66 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
67 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
68 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
69 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
70 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
71 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
73 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
74 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
75 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
77 uint64_t getSymbolSize(SymbolRef Symb) const override;
82 void moveSymbolNext(DataRefImpl &Symb) const override;
83 std::error_code getSymbolName(DataRefImpl Symb,
84 StringRef &Res) const override;
85 std::error_code getSymbolAddress(DataRefImpl Symb,
86 uint64_t &Res) const override;
87 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
88 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
89 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
90 std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
91 std::error_code getSymbolType(DataRefImpl Symb,
92 SymbolRef::Type &Res) const override;
93 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
94 std::error_code getSymbolSection(DataRefImpl Symb,
95 section_iterator &Res) const override;
97 void moveSectionNext(DataRefImpl &Sec) const override;
98 std::error_code getSectionName(DataRefImpl Sec,
99 StringRef &Res) const override;
100 uint64_t getSectionAddress(DataRefImpl Sec) const override;
101 uint64_t getSectionSize(DataRefImpl Sec) const override;
102 std::error_code getSectionContents(DataRefImpl Sec,
103 StringRef &Res) const override;
104 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
105 bool isSectionText(DataRefImpl Sec) const override;
106 bool isSectionData(DataRefImpl Sec) const override;
107 bool isSectionBSS(DataRefImpl Sec) const override;
108 bool isSectionVirtual(DataRefImpl Sec) const override;
109 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
110 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
111 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
112 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
114 void moveRelocationNext(DataRefImpl &Rel) const override;
115 std::error_code getRelocationAddress(DataRefImpl Rel,
116 uint64_t &Res) const override;
117 std::error_code getRelocationOffset(DataRefImpl Rel,
118 uint64_t &Res) const override;
119 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
120 std::error_code getRelocationType(DataRefImpl Rel,
121 uint64_t &Res) const override;
123 getRelocationTypeName(DataRefImpl Rel,
124 SmallVectorImpl<char> &Result) const override;
126 uint64_t getROffset(DataRefImpl Rel) const;
127 StringRef getRelocationTypeName(uint32_t Type) const;
129 /// \brief Get the relocation section that contains \a Rel.
130 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
131 return EF.getSection(Rel.d.a);
134 const Elf_Rel *getRel(DataRefImpl Rel) const;
135 const Elf_Rela *getRela(DataRefImpl Rela) const;
137 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
138 bool IsDynamic = Symb.p & 1;
141 EF.begin_dynamic_symbols().getEntSize(),
142 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
143 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
144 reinterpret_cast<const char *>(Symb.p), IsDynamic);
147 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
149 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
150 static_cast<uintptr_t>(Symb.isDynamic());
154 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
155 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
156 reinterpret_cast<const char *>(Sec.p));
159 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
161 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
165 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
167 DRI.p = reinterpret_cast<uintptr_t>(Sec);
171 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
172 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
173 reinterpret_cast<const char *>(Dyn.p));
176 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
178 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
182 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
183 unsigned char Binding = ESym->getBinding();
184 unsigned char Visibility = ESym->getVisibility();
186 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
187 // visibility is either DEFAULT or PROTECTED. All other symbols are not
189 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
190 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
196 // This flag is used for classof, to distinguish ELFObjectFile from
197 // its subclass. If more subclasses will be created, this flag will
198 // have to become an enum.
199 bool isDyldELFObject;
202 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
204 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
206 basic_symbol_iterator symbol_begin_impl() const override;
207 basic_symbol_iterator symbol_end_impl() const override;
209 symbol_iterator dynamic_symbol_begin() const;
210 symbol_iterator dynamic_symbol_end() const;
212 section_iterator section_begin() const override;
213 section_iterator section_end() const override;
215 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
216 bool hasRelocationAddend(DataRefImpl Rel) const override;
218 uint64_t getSectionFlags(SectionRef Sec) const override;
219 uint32_t getSectionType(SectionRef Sec) const override;
221 uint8_t getBytesInAddress() const override;
222 StringRef getFileFormatName() const override;
223 unsigned getArch() const override;
224 StringRef getLoadName() const;
226 std::error_code getPlatformFlags(unsigned &Result) const override {
227 Result = EF.getHeader()->e_flags;
228 return std::error_code();
231 const ELFFile<ELFT> *getELFFile() const { return &EF; }
233 bool isDyldType() const { return isDyldELFObject; }
234 static inline bool classof(const Binary *v) {
235 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
239 std::pair<symbol_iterator, symbol_iterator>
240 getELFDynamicSymbolIterators() const override;
242 bool isRelocatableObject() const override;
245 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
246 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
247 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
248 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
250 template <class ELFT>
251 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
252 Symb = toDRI(++toELFSymIter(Symb));
255 template <class ELFT>
256 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
257 StringRef &Result) const {
258 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
260 return Name.getError();
262 return std::error_code();
265 template <class ELFT>
266 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
267 DataRefImpl DRI = Sec.getRawDataRefImpl();
268 return toELFShdrIter(DRI)->sh_flags;
271 template <class ELFT>
272 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
273 DataRefImpl DRI = Sec.getRawDataRefImpl();
274 return toELFShdrIter(DRI)->sh_type;
277 template <class ELFT>
278 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
279 uint64_t &Result) const {
280 const Elf_Sym *ESym = getSymbol(Symb);
281 switch (EF.getSymbolTableIndex(ESym)) {
282 case ELF::SHN_COMMON:
284 Result = UnknownAddress;
285 return std::error_code();
287 Result = ESym->st_value;
288 return std::error_code();
293 const Elf_Ehdr *Header = EF.getHeader();
294 Result = ESym->st_value;
296 // Clear the ARM/Thumb or microMIPS indicator flag.
297 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
298 ESym->getType() == ELF::STT_FUNC)
301 if (Header->e_type == ELF::ET_REL) {
302 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
303 if (Section != nullptr)
304 Result += Section->sh_addr;
307 return std::error_code();
310 template <class ELFT>
311 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
312 Elf_Sym_Iter Sym = toELFSymIter(Symb);
313 if (Sym->st_shndx == ELF::SHN_COMMON)
314 return Sym->st_value;
318 template <class ELFT>
319 uint64_t ELFObjectFile<ELFT>::getSymbolSize(SymbolRef Symb) const {
320 return toELFSymIter(Symb.getRawDataRefImpl())->st_size;
323 template <class ELFT>
324 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
325 return toELFSymIter(Symb)->st_size;
328 template <class ELFT>
329 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
330 uint8_t &Result) const {
331 Result = toELFSymIter(Symb)->st_other;
332 return std::error_code();
335 template <class ELFT>
337 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
338 SymbolRef::Type &Result) const {
339 const Elf_Sym *ESym = getSymbol(Symb);
341 switch (ESym->getType()) {
342 case ELF::STT_NOTYPE:
343 Result = SymbolRef::ST_Unknown;
345 case ELF::STT_SECTION:
346 Result = SymbolRef::ST_Debug;
349 Result = SymbolRef::ST_File;
352 Result = SymbolRef::ST_Function;
354 case ELF::STT_OBJECT:
355 case ELF::STT_COMMON:
357 Result = SymbolRef::ST_Data;
360 Result = SymbolRef::ST_Other;
363 return std::error_code();
366 template <class ELFT>
367 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
368 Elf_Sym_Iter EIter = toELFSymIter(Symb);
369 const Elf_Sym *ESym = &*EIter;
371 uint32_t Result = SymbolRef::SF_None;
373 if (ESym->getBinding() != ELF::STB_LOCAL)
374 Result |= SymbolRef::SF_Global;
376 if (ESym->getBinding() == ELF::STB_WEAK)
377 Result |= SymbolRef::SF_Weak;
379 if (ESym->st_shndx == ELF::SHN_ABS)
380 Result |= SymbolRef::SF_Absolute;
382 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
383 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
384 Result |= SymbolRef::SF_FormatSpecific;
386 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
387 Result |= SymbolRef::SF_Undefined;
389 if (ESym->getType() == ELF::STT_COMMON ||
390 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
391 Result |= SymbolRef::SF_Common;
393 if (isExportedToOtherDSO(ESym))
394 Result |= SymbolRef::SF_Exported;
396 if (ESym->getVisibility() == ELF::STV_HIDDEN)
397 Result |= SymbolRef::SF_Hidden;
402 template <class ELFT>
404 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
405 const Elf_Shdr *ESec = EF.getSection(ESym);
407 return section_end();
410 Sec.p = reinterpret_cast<intptr_t>(ESec);
411 return section_iterator(SectionRef(Sec, this));
415 template <class ELFT>
417 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
418 section_iterator &Res) const {
419 Res = getSymbolSection(getSymbol(Symb));
420 return std::error_code();
423 template <class ELFT>
424 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
425 Sec = toDRI(++toELFShdrIter(Sec));
428 template <class ELFT>
429 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
430 StringRef &Result) const {
431 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
433 return Name.getError();
435 return std::error_code();
438 template <class ELFT>
439 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
440 return toELFShdrIter(Sec)->sh_addr;
443 template <class ELFT>
444 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
445 return toELFShdrIter(Sec)->sh_size;
448 template <class ELFT>
450 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
451 StringRef &Result) const {
452 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
453 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
454 return std::error_code();
457 template <class ELFT>
458 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
459 return toELFShdrIter(Sec)->sh_addralign;
462 template <class ELFT>
463 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
464 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
467 template <class ELFT>
468 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
469 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
470 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
471 EShdr->sh_type == ELF::SHT_PROGBITS;
474 template <class ELFT>
475 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
476 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
477 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
478 EShdr->sh_type == ELF::SHT_NOBITS;
481 template <class ELFT>
482 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
483 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
486 template <class ELFT>
487 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
488 DataRefImpl Symb) const {
489 Elf_Sym_Iter ESym = toELFSymIter(Symb);
491 uintX_t Index = ESym->st_shndx;
492 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
494 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
497 template <class ELFT>
499 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
501 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
502 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
504 return relocation_iterator(RelocationRef(RelData, this));
507 template <class ELFT>
509 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
511 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
512 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
513 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
514 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
517 RelData.d.b = S->sh_size / S->sh_entsize;
519 return relocation_iterator(RelocationRef(RelData, this));
522 template <class ELFT>
524 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
525 if (EF.getHeader()->e_type != ELF::ET_REL)
526 return section_end();
528 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
529 uintX_t Type = EShdr->sh_type;
530 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
531 return section_end();
533 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
534 return section_iterator(SectionRef(toDRI(R), this));
538 template <class ELFT>
539 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
543 template <class ELFT>
545 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
547 const Elf_Shdr *sec = getRelSection(Rel);
548 switch (sec->sh_type) {
550 report_fatal_error("Invalid section type in Rel!");
552 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
555 case ELF::SHT_RELA: {
556 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
563 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
565 DataRefImpl SymbolData;
566 switch (SymSec->sh_type) {
568 report_fatal_error("Invalid symbol table section type!");
569 case ELF::SHT_SYMTAB:
570 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
572 case ELF::SHT_DYNSYM:
573 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
577 return symbol_iterator(SymbolRef(SymbolData, this));
580 template <class ELFT>
582 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
583 uint64_t &Result) const {
584 uint64_t ROffset = getROffset(Rel);
585 const Elf_Ehdr *Header = EF.getHeader();
587 if (Header->e_type == ELF::ET_REL) {
588 const Elf_Shdr *RelocationSec = getRelSection(Rel);
589 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
590 Result = ROffset + RelocatedSec->sh_addr;
595 return std::error_code();
598 template <class ELFT>
600 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
601 uint64_t &Result) const {
602 assert(EF.getHeader()->e_type == ELF::ET_REL &&
603 "Only relocatable object files have relocation offsets");
604 Result = getROffset(Rel);
605 return std::error_code();
608 template <class ELFT>
609 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
610 const Elf_Shdr *sec = getRelSection(Rel);
611 switch (sec->sh_type) {
613 report_fatal_error("Invalid section type in Rel!");
615 return getRel(Rel)->r_offset;
617 return getRela(Rel)->r_offset;
621 template <class ELFT>
622 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
623 uint64_t &Result) const {
624 const Elf_Shdr *sec = getRelSection(Rel);
625 switch (sec->sh_type) {
627 report_fatal_error("Invalid section type in Rel!");
629 Result = getRel(Rel)->getType(EF.isMips64EL());
632 case ELF::SHT_RELA: {
633 Result = getRela(Rel)->getType(EF.isMips64EL());
637 return std::error_code();
640 template <class ELFT>
641 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
642 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
645 template <class ELFT>
646 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
647 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
648 const Elf_Shdr *sec = getRelSection(Rel);
650 switch (sec->sh_type) {
652 return object_error::parse_failed;
654 type = getRel(Rel)->getType(EF.isMips64EL());
657 case ELF::SHT_RELA: {
658 type = getRela(Rel)->getType(EF.isMips64EL());
663 EF.getRelocationTypeName(type, Result);
664 return std::error_code();
667 template <class ELFT>
669 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
670 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
671 return object_error::parse_failed;
672 return (int64_t)getRela(Rel)->r_addend;
675 template <class ELFT>
676 bool ELFObjectFile<ELFT>::hasRelocationAddend(DataRefImpl Rel) const {
677 return getRelSection(Rel)->sh_type == ELF::SHT_RELA;
680 template <class ELFT>
681 const typename ELFFile<ELFT>::Elf_Sym *
682 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
683 return &*toELFSymIter(Symb);
686 template <class ELFT>
687 const typename ELFObjectFile<ELFT>::Elf_Rel *
688 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
689 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
692 template <class ELFT>
693 const typename ELFObjectFile<ELFT>::Elf_Rela *
694 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
695 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
698 template <class ELFT>
699 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
701 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
705 EF(Data.getBuffer(), EC) {}
707 template <class ELFT>
708 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
709 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
712 template <class ELFT>
713 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
714 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
717 template <class ELFT>
718 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
719 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
722 template <class ELFT>
723 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
724 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
727 template <class ELFT>
728 section_iterator ELFObjectFile<ELFT>::section_begin() const {
729 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
732 template <class ELFT>
733 section_iterator ELFObjectFile<ELFT>::section_end() const {
734 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
737 template <class ELFT>
738 StringRef ELFObjectFile<ELFT>::getLoadName() const {
739 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
740 Elf_Dyn_Iter DE = EF.end_dynamic_table();
742 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
746 return EF.getDynamicString(DI->getVal());
750 template <class ELFT>
751 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
752 return ELFT::Is64Bits ? 8 : 4;
755 template <class ELFT>
756 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
757 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
758 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
759 case ELF::ELFCLASS32:
760 switch (EF.getHeader()->e_machine) {
764 return "ELF32-x86-64";
766 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
767 case ELF::EM_HEXAGON:
768 return "ELF32-hexagon";
774 case ELF::EM_SPARC32PLUS:
775 return "ELF32-sparc";
777 return "ELF32-unknown";
779 case ELF::ELFCLASS64:
780 switch (EF.getHeader()->e_machine) {
784 return "ELF64-x86-64";
785 case ELF::EM_AARCH64:
786 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
788 return "ELF64-ppc64";
791 case ELF::EM_SPARCV9:
792 return "ELF64-sparc";
796 return "ELF64-unknown";
799 // FIXME: Proper error handling.
800 report_fatal_error("Invalid ELFCLASS!");
804 template <class ELFT>
805 unsigned ELFObjectFile<ELFT>::getArch() const {
806 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
807 switch (EF.getHeader()->e_machine) {
811 return Triple::x86_64;
812 case ELF::EM_AARCH64:
813 return Triple::aarch64;
816 case ELF::EM_HEXAGON:
817 return Triple::hexagon;
819 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
820 case ELF::ELFCLASS32:
821 return IsLittleEndian ? Triple::mipsel : Triple::mips;
822 case ELF::ELFCLASS64:
823 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
825 report_fatal_error("Invalid ELFCLASS!");
830 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
832 return Triple::systemz;
835 case ELF::EM_SPARC32PLUS:
836 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
837 case ELF::EM_SPARCV9:
838 return Triple::sparcv9;
841 return Triple::UnknownArch;
845 template <class ELFT>
846 std::pair<symbol_iterator, symbol_iterator>
847 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
848 return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
851 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
852 return EF.getHeader()->e_type == ELF::ET_REL;
855 inline std::pair<symbol_iterator, symbol_iterator>
856 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
857 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();