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 static inline bool classof(const Binary *v) { return v->isELF(); }
58 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
60 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
62 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
64 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
65 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
66 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
67 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
68 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
69 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
71 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
72 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
73 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
78 void moveSymbolNext(DataRefImpl &Symb) const override;
79 std::error_code getSymbolName(DataRefImpl Symb,
80 StringRef &Res) const override;
81 std::error_code getSymbolAddress(DataRefImpl Symb,
82 uint64_t &Res) const override;
83 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
84 uint64_t getSymbolSize(DataRefImpl Symb) const override;
85 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
86 std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
87 std::error_code getSymbolType(DataRefImpl Symb,
88 SymbolRef::Type &Res) const override;
89 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
90 std::error_code getSymbolSection(DataRefImpl Symb,
91 section_iterator &Res) const override;
93 void moveSectionNext(DataRefImpl &Sec) const override;
94 std::error_code getSectionName(DataRefImpl Sec,
95 StringRef &Res) const override;
96 uint64_t getSectionAddress(DataRefImpl Sec) const override;
97 uint64_t getSectionSize(DataRefImpl Sec) const override;
98 std::error_code getSectionContents(DataRefImpl Sec,
99 StringRef &Res) const override;
100 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
101 bool isSectionText(DataRefImpl Sec) const override;
102 bool isSectionData(DataRefImpl Sec) const override;
103 bool isSectionBSS(DataRefImpl Sec) const override;
104 bool isSectionVirtual(DataRefImpl Sec) const override;
105 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
106 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
107 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
108 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
110 void moveRelocationNext(DataRefImpl &Rel) const override;
111 std::error_code getRelocationAddress(DataRefImpl Rel,
112 uint64_t &Res) const override;
113 std::error_code getRelocationOffset(DataRefImpl Rel,
114 uint64_t &Res) const override;
115 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
116 std::error_code getRelocationType(DataRefImpl Rel,
117 uint64_t &Res) const override;
119 getRelocationTypeName(DataRefImpl Rel,
120 SmallVectorImpl<char> &Result) const override;
122 uint64_t getROffset(DataRefImpl Rel) const;
123 StringRef getRelocationTypeName(uint32_t Type) const;
125 /// \brief Get the relocation section that contains \a Rel.
126 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
127 return EF.getSection(Rel.d.a);
130 const Elf_Rel *getRel(DataRefImpl Rel) const;
131 const Elf_Rela *getRela(DataRefImpl Rela) const;
133 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
134 bool IsDynamic = Symb.p & 1;
137 EF.begin_dynamic_symbols().getEntSize(),
138 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
139 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
140 reinterpret_cast<const char *>(Symb.p), IsDynamic);
143 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
145 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
146 static_cast<uintptr_t>(Symb.isDynamic());
150 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
151 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
152 reinterpret_cast<const char *>(Sec.p));
155 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
157 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
161 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
163 DRI.p = reinterpret_cast<uintptr_t>(Sec);
167 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
168 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
169 reinterpret_cast<const char *>(Dyn.p));
172 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
174 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
178 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
179 unsigned char Binding = ESym->getBinding();
180 unsigned char Visibility = ESym->getVisibility();
182 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
183 // visibility is either DEFAULT or PROTECTED. All other symbols are not
185 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
186 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
192 // This flag is used for classof, to distinguish ELFObjectFile from
193 // its subclass. If more subclasses will be created, this flag will
194 // have to become an enum.
195 bool isDyldELFObject;
198 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
200 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
202 basic_symbol_iterator symbol_begin_impl() const override;
203 basic_symbol_iterator symbol_end_impl() const override;
205 symbol_iterator dynamic_symbol_begin() const;
206 symbol_iterator dynamic_symbol_end() const;
208 section_iterator section_begin() const override;
209 section_iterator section_end() const override;
211 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
212 bool hasRelocationAddend(DataRefImpl Rel) const override;
214 uint64_t getSectionFlags(SectionRef Sec) const override;
215 uint32_t getSectionType(SectionRef Sec) const override;
217 uint8_t getBytesInAddress() const override;
218 StringRef getFileFormatName() const override;
219 unsigned getArch() const override;
220 StringRef getLoadName() const;
222 std::error_code getPlatformFlags(unsigned &Result) const override {
223 Result = EF.getHeader()->e_flags;
224 return std::error_code();
227 const ELFFile<ELFT> *getELFFile() const { return &EF; }
229 bool isDyldType() const { return isDyldELFObject; }
230 static inline bool classof(const Binary *v) {
231 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
235 std::pair<symbol_iterator, symbol_iterator>
236 getELFDynamicSymbolIterators() const override;
238 bool isRelocatableObject() const override;
241 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
242 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
243 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
244 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
246 template <class ELFT>
247 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
248 Symb = toDRI(++toELFSymIter(Symb));
251 template <class ELFT>
252 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
253 StringRef &Result) const {
254 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
256 return Name.getError();
258 return std::error_code();
261 template <class ELFT>
262 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
263 DataRefImpl DRI = Sec.getRawDataRefImpl();
264 return toELFShdrIter(DRI)->sh_flags;
267 template <class ELFT>
268 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
269 DataRefImpl DRI = Sec.getRawDataRefImpl();
270 return toELFShdrIter(DRI)->sh_type;
273 template <class ELFT>
274 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
275 uint64_t &Result) const {
276 const Elf_Sym *ESym = getSymbol(Symb);
277 switch (EF.getSymbolTableIndex(ESym)) {
278 case ELF::SHN_COMMON:
280 Result = UnknownAddressOrSize;
281 return std::error_code();
283 Result = ESym->st_value;
284 return std::error_code();
289 const Elf_Ehdr *Header = EF.getHeader();
290 Result = ESym->st_value;
292 // Clear the ARM/Thumb or microMIPS indicator flag.
293 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
294 ESym->getType() == ELF::STT_FUNC)
297 if (Header->e_type == ELF::ET_REL) {
298 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
299 if (Section != nullptr)
300 Result += Section->sh_addr;
303 return std::error_code();
306 template <class ELFT>
307 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
308 Elf_Sym_Iter Sym = toELFSymIter(Symb);
309 if (Sym->st_shndx == ELF::SHN_COMMON)
310 return Sym->st_value;
314 template <class ELFT>
315 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb) const {
316 return toELFSymIter(Symb)->st_size;
319 template <class ELFT>
320 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
321 uint8_t &Result) const {
322 Result = toELFSymIter(Symb)->st_other;
323 return std::error_code();
326 template <class ELFT>
328 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
329 SymbolRef::Type &Result) const {
330 const Elf_Sym *ESym = getSymbol(Symb);
332 switch (ESym->getType()) {
333 case ELF::STT_NOTYPE:
334 Result = SymbolRef::ST_Unknown;
336 case ELF::STT_SECTION:
337 Result = SymbolRef::ST_Debug;
340 Result = SymbolRef::ST_File;
343 Result = SymbolRef::ST_Function;
345 case ELF::STT_OBJECT:
346 case ELF::STT_COMMON:
348 Result = SymbolRef::ST_Data;
351 Result = SymbolRef::ST_Other;
354 return std::error_code();
357 template <class ELFT>
358 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
359 Elf_Sym_Iter EIter = toELFSymIter(Symb);
360 const Elf_Sym *ESym = &*EIter;
362 uint32_t Result = SymbolRef::SF_None;
364 if (ESym->getBinding() != ELF::STB_LOCAL)
365 Result |= SymbolRef::SF_Global;
367 if (ESym->getBinding() == ELF::STB_WEAK)
368 Result |= SymbolRef::SF_Weak;
370 if (ESym->st_shndx == ELF::SHN_ABS)
371 Result |= SymbolRef::SF_Absolute;
373 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
374 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
375 Result |= SymbolRef::SF_FormatSpecific;
377 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
378 Result |= SymbolRef::SF_Undefined;
380 if (ESym->getType() == ELF::STT_COMMON ||
381 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
382 Result |= SymbolRef::SF_Common;
384 if (isExportedToOtherDSO(ESym))
385 Result |= SymbolRef::SF_Exported;
387 if (ESym->getVisibility() == ELF::STV_HIDDEN)
388 Result |= SymbolRef::SF_Hidden;
393 template <class ELFT>
395 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
396 const Elf_Shdr *ESec = EF.getSection(ESym);
398 return section_end();
401 Sec.p = reinterpret_cast<intptr_t>(ESec);
402 return section_iterator(SectionRef(Sec, this));
406 template <class ELFT>
408 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
409 section_iterator &Res) const {
410 Res = getSymbolSection(getSymbol(Symb));
411 return std::error_code();
414 template <class ELFT>
415 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
416 Sec = toDRI(++toELFShdrIter(Sec));
419 template <class ELFT>
420 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
421 StringRef &Result) const {
422 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
424 return Name.getError();
426 return std::error_code();
429 template <class ELFT>
430 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
431 return toELFShdrIter(Sec)->sh_addr;
434 template <class ELFT>
435 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
436 return toELFShdrIter(Sec)->sh_size;
439 template <class ELFT>
441 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
442 StringRef &Result) const {
443 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
444 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
445 return std::error_code();
448 template <class ELFT>
449 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
450 return toELFShdrIter(Sec)->sh_addralign;
453 template <class ELFT>
454 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
455 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
458 template <class ELFT>
459 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
460 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
461 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
462 EShdr->sh_type == ELF::SHT_PROGBITS;
465 template <class ELFT>
466 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
467 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
468 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
469 EShdr->sh_type == ELF::SHT_NOBITS;
472 template <class ELFT>
473 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
474 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
477 template <class ELFT>
478 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
479 DataRefImpl Symb) const {
480 Elf_Sym_Iter ESym = toELFSymIter(Symb);
482 uintX_t Index = ESym->st_shndx;
483 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
485 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
488 template <class ELFT>
490 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
492 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
493 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
495 return relocation_iterator(RelocationRef(RelData, this));
498 template <class ELFT>
500 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
502 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
503 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
504 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
505 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
508 RelData.d.b = S->sh_size / S->sh_entsize;
510 return relocation_iterator(RelocationRef(RelData, this));
513 template <class ELFT>
515 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
516 if (EF.getHeader()->e_type != ELF::ET_REL)
517 return section_end();
519 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
520 uintX_t Type = EShdr->sh_type;
521 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
522 return section_end();
524 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
525 return section_iterator(SectionRef(toDRI(R), this));
529 template <class ELFT>
530 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
534 template <class ELFT>
536 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
538 const Elf_Shdr *sec = getRelSection(Rel);
539 switch (sec->sh_type) {
541 report_fatal_error("Invalid section type in Rel!");
543 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
546 case ELF::SHT_RELA: {
547 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
554 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
556 DataRefImpl SymbolData;
557 switch (SymSec->sh_type) {
559 report_fatal_error("Invalid symbol table section type!");
560 case ELF::SHT_SYMTAB:
561 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
563 case ELF::SHT_DYNSYM:
564 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
568 return symbol_iterator(SymbolRef(SymbolData, this));
571 template <class ELFT>
573 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
574 uint64_t &Result) const {
575 uint64_t ROffset = getROffset(Rel);
576 const Elf_Ehdr *Header = EF.getHeader();
578 if (Header->e_type == ELF::ET_REL) {
579 const Elf_Shdr *RelocationSec = getRelSection(Rel);
580 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
581 Result = ROffset + RelocatedSec->sh_addr;
586 return std::error_code();
589 template <class ELFT>
591 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
592 uint64_t &Result) const {
593 assert(EF.getHeader()->e_type == ELF::ET_REL &&
594 "Only relocatable object files have relocation offsets");
595 Result = getROffset(Rel);
596 return std::error_code();
599 template <class ELFT>
600 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
601 const Elf_Shdr *sec = getRelSection(Rel);
602 switch (sec->sh_type) {
604 report_fatal_error("Invalid section type in Rel!");
606 return getRel(Rel)->r_offset;
608 return getRela(Rel)->r_offset;
612 template <class ELFT>
613 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
614 uint64_t &Result) const {
615 const Elf_Shdr *sec = getRelSection(Rel);
616 switch (sec->sh_type) {
618 report_fatal_error("Invalid section type in Rel!");
620 Result = getRel(Rel)->getType(EF.isMips64EL());
623 case ELF::SHT_RELA: {
624 Result = getRela(Rel)->getType(EF.isMips64EL());
628 return std::error_code();
631 template <class ELFT>
632 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
633 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
636 template <class ELFT>
637 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
638 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
639 const Elf_Shdr *sec = getRelSection(Rel);
641 switch (sec->sh_type) {
643 return object_error::parse_failed;
645 type = getRel(Rel)->getType(EF.isMips64EL());
648 case ELF::SHT_RELA: {
649 type = getRela(Rel)->getType(EF.isMips64EL());
654 EF.getRelocationTypeName(type, Result);
655 return std::error_code();
658 template <class ELFT>
660 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
661 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
662 return object_error::parse_failed;
663 return (int64_t)getRela(Rel)->r_addend;
666 template <class ELFT>
667 bool ELFObjectFile<ELFT>::hasRelocationAddend(DataRefImpl Rel) const {
668 return getRelSection(Rel)->sh_type == ELF::SHT_RELA;
671 template <class ELFT>
672 const typename ELFFile<ELFT>::Elf_Sym *
673 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
674 return &*toELFSymIter(Symb);
677 template <class ELFT>
678 const typename ELFObjectFile<ELFT>::Elf_Rel *
679 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
680 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
683 template <class ELFT>
684 const typename ELFObjectFile<ELFT>::Elf_Rela *
685 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
686 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
689 template <class ELFT>
690 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
692 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
696 EF(Data.getBuffer(), EC) {}
698 template <class ELFT>
699 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
700 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
703 template <class ELFT>
704 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
705 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
708 template <class ELFT>
709 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
710 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
713 template <class ELFT>
714 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
715 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
718 template <class ELFT>
719 section_iterator ELFObjectFile<ELFT>::section_begin() const {
720 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
723 template <class ELFT>
724 section_iterator ELFObjectFile<ELFT>::section_end() const {
725 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
728 template <class ELFT>
729 StringRef ELFObjectFile<ELFT>::getLoadName() const {
730 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
731 Elf_Dyn_Iter DE = EF.end_dynamic_table();
733 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
737 return EF.getDynamicString(DI->getVal());
741 template <class ELFT>
742 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
743 return ELFT::Is64Bits ? 8 : 4;
746 template <class ELFT>
747 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
748 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
749 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
750 case ELF::ELFCLASS32:
751 switch (EF.getHeader()->e_machine) {
755 return "ELF32-x86-64";
757 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
758 case ELF::EM_HEXAGON:
759 return "ELF32-hexagon";
765 case ELF::EM_SPARC32PLUS:
766 return "ELF32-sparc";
768 return "ELF32-unknown";
770 case ELF::ELFCLASS64:
771 switch (EF.getHeader()->e_machine) {
775 return "ELF64-x86-64";
776 case ELF::EM_AARCH64:
777 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
779 return "ELF64-ppc64";
782 case ELF::EM_SPARCV9:
783 return "ELF64-sparc";
787 return "ELF64-unknown";
790 // FIXME: Proper error handling.
791 report_fatal_error("Invalid ELFCLASS!");
795 template <class ELFT>
796 unsigned ELFObjectFile<ELFT>::getArch() const {
797 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
798 switch (EF.getHeader()->e_machine) {
802 return Triple::x86_64;
803 case ELF::EM_AARCH64:
804 return Triple::aarch64;
807 case ELF::EM_HEXAGON:
808 return Triple::hexagon;
810 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
811 case ELF::ELFCLASS32:
812 return IsLittleEndian ? Triple::mipsel : Triple::mips;
813 case ELF::ELFCLASS64:
814 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
816 report_fatal_error("Invalid ELFCLASS!");
821 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
823 return Triple::systemz;
826 case ELF::EM_SPARC32PLUS:
827 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
828 case ELF::EM_SPARCV9:
829 return Triple::sparcv9;
832 return Triple::UnknownArch;
836 template <class ELFT>
837 std::pair<symbol_iterator, symbol_iterator>
838 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
839 return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
842 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
843 return EF.getHeader()->e_type == ELF::ET_REL;
846 inline std::pair<symbol_iterator, symbol_iterator>
847 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
848 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
851 } // namespace object