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 elf_symbol_iterator;
41 class ELFObjectFileBase : public ObjectFile {
42 friend class ELFSymbolRef;
43 friend class ELFSectionRef;
46 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
48 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
49 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
50 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
52 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
53 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
56 virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
58 // FIXME: This is a bit of a hack. Every caller should know if it expecting
60 virtual bool hasRelocationAddend(DataRefImpl Rel) const = 0;
62 typedef iterator_range<elf_symbol_iterator> elf_symbol_iterator_range;
63 virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
65 elf_symbol_iterator_range symbols() const;
67 static inline bool classof(const Binary *v) { return v->isELF(); }
70 class ELFSectionRef : public SectionRef {
72 ELFSectionRef(const SectionRef &B) : SectionRef(B) {
73 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
76 const ELFObjectFileBase *getObject() const {
77 return cast<ELFObjectFileBase>(SectionRef::getObject());
80 uint32_t getType() const {
81 return getObject()->getSectionType(getRawDataRefImpl());
84 uint64_t getFlags() const {
85 return getObject()->getSectionFlags(getRawDataRefImpl());
89 class elf_section_iterator : public section_iterator {
91 elf_section_iterator(const section_iterator &B) : section_iterator(B) {
92 assert(isa<ELFObjectFileBase>(B->getObject()));
95 const ELFSectionRef *operator->() const {
96 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
99 const ELFSectionRef &operator*() const {
100 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
104 class ELFSymbolRef : public SymbolRef {
106 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
107 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
110 const ELFObjectFileBase *getObject() const {
111 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
114 uint64_t getSize() const {
115 return getObject()->getSymbolSize(getRawDataRefImpl());
118 uint8_t getOther() const {
119 return getObject()->getSymbolOther(getRawDataRefImpl());
122 uint8_t getELFType() const {
123 return getObject()->getSymbolELFType(getRawDataRefImpl());
127 class elf_symbol_iterator : public symbol_iterator {
129 elf_symbol_iterator(const basic_symbol_iterator &B)
130 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
131 cast<ELFObjectFileBase>(B->getObject()))) {}
133 const ELFSymbolRef *operator->() const {
134 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
137 const ELFSymbolRef &operator*() const {
138 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
142 inline ELFObjectFileBase::elf_symbol_iterator_range
143 ELFObjectFileBase::symbols() const {
144 return elf_symbol_iterator_range(symbol_begin(), symbol_end());
147 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
148 uint64_t getSymbolSize(DataRefImpl Sym) const override;
151 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
153 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
155 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
156 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
157 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
158 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
159 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
160 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
162 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
163 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
164 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
169 void moveSymbolNext(DataRefImpl &Symb) const override;
170 std::error_code getSymbolName(DataRefImpl Symb,
171 StringRef &Res) const override;
172 std::error_code getSymbolAddress(DataRefImpl Symb,
173 uint64_t &Res) const override;
174 uint64_t getSymbolValue(DataRefImpl Symb) const override;
175 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
176 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
177 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
178 uint8_t getSymbolOther(DataRefImpl Symb) const override;
179 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
180 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
181 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
182 std::error_code getSymbolSection(DataRefImpl Symb,
183 section_iterator &Res) const override;
185 void moveSectionNext(DataRefImpl &Sec) const override;
186 std::error_code getSectionName(DataRefImpl Sec,
187 StringRef &Res) const override;
188 uint64_t getSectionAddress(DataRefImpl Sec) const override;
189 uint64_t getSectionSize(DataRefImpl Sec) const override;
190 std::error_code getSectionContents(DataRefImpl Sec,
191 StringRef &Res) const override;
192 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
193 bool isSectionText(DataRefImpl Sec) const override;
194 bool isSectionData(DataRefImpl Sec) const override;
195 bool isSectionBSS(DataRefImpl Sec) const override;
196 bool isSectionVirtual(DataRefImpl Sec) const override;
197 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
198 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
199 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
200 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
202 void moveRelocationNext(DataRefImpl &Rel) const override;
203 std::error_code getRelocationAddress(DataRefImpl Rel,
204 uint64_t &Res) const override;
205 std::error_code getRelocationOffset(DataRefImpl Rel,
206 uint64_t &Res) const override;
207 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
208 std::error_code getRelocationType(DataRefImpl Rel,
209 uint64_t &Res) const override;
211 getRelocationTypeName(DataRefImpl Rel,
212 SmallVectorImpl<char> &Result) const override;
214 uint32_t getSectionType(DataRefImpl Sec) const override;
215 uint64_t getSectionFlags(DataRefImpl Sec) const override;
216 uint64_t getROffset(DataRefImpl Rel) const;
217 StringRef getRelocationTypeName(uint32_t Type) const;
219 /// \brief Get the relocation section that contains \a Rel.
220 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
221 return EF.getSection(Rel.d.a);
224 const Elf_Rel *getRel(DataRefImpl Rel) const;
225 const Elf_Rela *getRela(DataRefImpl Rela) const;
227 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
228 bool IsDynamic = Symb.p & 1;
231 EF.begin_dynamic_symbols().getEntSize(),
232 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
233 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
234 reinterpret_cast<const char *>(Symb.p), IsDynamic);
237 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
239 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
240 static_cast<uintptr_t>(Symb.isDynamic());
244 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
245 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
246 reinterpret_cast<const char *>(Sec.p));
249 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
251 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
255 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
257 DRI.p = reinterpret_cast<uintptr_t>(Sec);
261 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
262 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
263 reinterpret_cast<const char *>(Dyn.p));
266 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
268 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
272 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
273 unsigned char Binding = ESym->getBinding();
274 unsigned char Visibility = ESym->getVisibility();
276 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
277 // visibility is either DEFAULT or PROTECTED. All other symbols are not
279 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
280 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
286 // This flag is used for classof, to distinguish ELFObjectFile from
287 // its subclass. If more subclasses will be created, this flag will
288 // have to become an enum.
289 bool isDyldELFObject;
292 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
294 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
296 basic_symbol_iterator symbol_begin_impl() const override;
297 basic_symbol_iterator symbol_end_impl() const override;
299 elf_symbol_iterator dynamic_symbol_begin() const;
300 elf_symbol_iterator dynamic_symbol_end() const;
302 section_iterator section_begin() const override;
303 section_iterator section_end() const override;
305 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
306 bool hasRelocationAddend(DataRefImpl Rel) const override;
308 uint8_t getBytesInAddress() const override;
309 StringRef getFileFormatName() const override;
310 unsigned getArch() const override;
311 StringRef getLoadName() const;
313 std::error_code getPlatformFlags(unsigned &Result) const override {
314 Result = EF.getHeader()->e_flags;
315 return std::error_code();
318 const ELFFile<ELFT> *getELFFile() const { return &EF; }
320 bool isDyldType() const { return isDyldELFObject; }
321 static inline bool classof(const Binary *v) {
322 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
326 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
328 bool isRelocatableObject() const override;
331 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
332 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
333 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
334 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
336 template <class ELFT>
337 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
338 Symb = toDRI(++toELFSymIter(Symb));
341 template <class ELFT>
342 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
343 StringRef &Result) const {
344 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
346 return Name.getError();
348 return std::error_code();
351 template <class ELFT>
352 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
353 return toELFShdrIter(Sec)->sh_flags;
356 template <class ELFT>
357 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
358 return toELFShdrIter(Sec)->sh_type;
361 template <class ELFT>
362 uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
363 const Elf_Sym *ESym = getSymbol(Symb);
364 switch (ESym->st_shndx) {
365 case ELF::SHN_COMMON:
367 return UnknownAddress;
369 return ESym->st_value;
372 const Elf_Ehdr *Header = EF.getHeader();
373 uint64_t Ret = ESym->st_value;
375 // Clear the ARM/Thumb or microMIPS indicator flag.
376 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
377 ESym->getType() == ELF::STT_FUNC)
383 template <class ELFT>
384 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
385 uint64_t &Result) const {
386 Result = getSymbolValue(Symb);
387 const Elf_Sym *ESym = getSymbol(Symb);
388 switch (ESym->st_shndx) {
389 case ELF::SHN_COMMON:
392 return std::error_code();
395 const Elf_Ehdr *Header = EF.getHeader();
397 if (Header->e_type == ELF::ET_REL) {
398 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
399 if (Section != nullptr)
400 Result += Section->sh_addr;
403 return std::error_code();
406 template <class ELFT>
407 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
408 Elf_Sym_Iter Sym = toELFSymIter(Symb);
409 if (Sym->st_shndx == ELF::SHN_COMMON)
410 return Sym->st_value;
414 template <class ELFT>
415 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
416 return toELFSymIter(Sym)->st_size;
419 template <class ELFT>
420 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
421 return toELFSymIter(Symb)->st_size;
424 template <class ELFT>
425 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
426 return toELFSymIter(Symb)->st_other;
429 template <class ELFT>
430 uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
431 return toELFSymIter(Symb)->getType();
434 template <class ELFT>
435 SymbolRef::Type ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
436 const Elf_Sym *ESym = getSymbol(Symb);
438 switch (ESym->getType()) {
439 case ELF::STT_NOTYPE:
440 return SymbolRef::ST_Unknown;
441 case ELF::STT_SECTION:
442 return SymbolRef::ST_Debug;
444 return SymbolRef::ST_File;
446 return SymbolRef::ST_Function;
447 case ELF::STT_OBJECT:
448 case ELF::STT_COMMON:
450 return SymbolRef::ST_Data;
452 return SymbolRef::ST_Other;
456 template <class ELFT>
457 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
458 Elf_Sym_Iter EIter = toELFSymIter(Symb);
459 const Elf_Sym *ESym = &*EIter;
461 uint32_t Result = SymbolRef::SF_None;
463 if (ESym->getBinding() != ELF::STB_LOCAL)
464 Result |= SymbolRef::SF_Global;
466 if (ESym->getBinding() == ELF::STB_WEAK)
467 Result |= SymbolRef::SF_Weak;
469 if (ESym->st_shndx == ELF::SHN_ABS)
470 Result |= SymbolRef::SF_Absolute;
472 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
473 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
474 Result |= SymbolRef::SF_FormatSpecific;
476 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
477 if (ErrorOr<StringRef> NameOrErr = EF.getSymbolName(EIter)) {
478 StringRef Name = *NameOrErr;
479 if (Name.startswith("$d") || Name.startswith("$t") ||
480 Name.startswith("$a"))
481 Result |= SymbolRef::SF_FormatSpecific;
485 if (ESym->st_shndx == ELF::SHN_UNDEF)
486 Result |= SymbolRef::SF_Undefined;
488 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
489 Result |= SymbolRef::SF_Common;
491 if (isExportedToOtherDSO(ESym))
492 Result |= SymbolRef::SF_Exported;
494 if (ESym->getVisibility() == ELF::STV_HIDDEN)
495 Result |= SymbolRef::SF_Hidden;
500 template <class ELFT>
502 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
503 const Elf_Shdr *ESec = EF.getSection(ESym);
505 return section_end();
508 Sec.p = reinterpret_cast<intptr_t>(ESec);
509 return section_iterator(SectionRef(Sec, this));
513 template <class ELFT>
515 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
516 section_iterator &Res) const {
517 Res = getSymbolSection(getSymbol(Symb));
518 return std::error_code();
521 template <class ELFT>
522 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
523 Sec = toDRI(++toELFShdrIter(Sec));
526 template <class ELFT>
527 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
528 StringRef &Result) const {
529 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
531 return Name.getError();
533 return std::error_code();
536 template <class ELFT>
537 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
538 return toELFShdrIter(Sec)->sh_addr;
541 template <class ELFT>
542 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
543 return toELFShdrIter(Sec)->sh_size;
546 template <class ELFT>
548 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
549 StringRef &Result) const {
550 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
551 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
552 return std::error_code();
555 template <class ELFT>
556 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
557 return toELFShdrIter(Sec)->sh_addralign;
560 template <class ELFT>
561 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
562 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
565 template <class ELFT>
566 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
567 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
568 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
569 EShdr->sh_type == ELF::SHT_PROGBITS;
572 template <class ELFT>
573 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
574 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
575 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
576 EShdr->sh_type == ELF::SHT_NOBITS;
579 template <class ELFT>
580 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
581 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
584 template <class ELFT>
585 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
586 DataRefImpl Symb) const {
587 Elf_Sym_Iter ESym = toELFSymIter(Symb);
589 uintX_t Index = ESym->st_shndx;
590 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
592 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
595 template <class ELFT>
597 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
599 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
600 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
602 return relocation_iterator(RelocationRef(RelData, this));
605 template <class ELFT>
607 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
609 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
610 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
611 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
612 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
615 RelData.d.b = S->sh_size / S->sh_entsize;
617 return relocation_iterator(RelocationRef(RelData, this));
620 template <class ELFT>
622 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
623 if (EF.getHeader()->e_type != ELF::ET_REL)
624 return section_end();
626 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
627 uintX_t Type = EShdr->sh_type;
628 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
629 return section_end();
631 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
632 return section_iterator(SectionRef(toDRI(R), this));
636 template <class ELFT>
637 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
641 template <class ELFT>
643 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
645 const Elf_Shdr *sec = getRelSection(Rel);
646 switch (sec->sh_type) {
648 report_fatal_error("Invalid section type in Rel!");
650 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
653 case ELF::SHT_RELA: {
654 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
661 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
663 DataRefImpl SymbolData;
664 switch (SymSec->sh_type) {
666 report_fatal_error("Invalid symbol table section type!");
667 case ELF::SHT_SYMTAB:
668 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
670 case ELF::SHT_DYNSYM:
671 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
675 return symbol_iterator(SymbolRef(SymbolData, this));
678 template <class ELFT>
680 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
681 uint64_t &Result) const {
682 uint64_t ROffset = getROffset(Rel);
683 const Elf_Ehdr *Header = EF.getHeader();
685 if (Header->e_type == ELF::ET_REL) {
686 const Elf_Shdr *RelocationSec = getRelSection(Rel);
687 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
688 Result = ROffset + RelocatedSec->sh_addr;
693 return std::error_code();
696 template <class ELFT>
698 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
699 uint64_t &Result) const {
700 assert(EF.getHeader()->e_type == ELF::ET_REL &&
701 "Only relocatable object files have relocation offsets");
702 Result = getROffset(Rel);
703 return std::error_code();
706 template <class ELFT>
707 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
708 const Elf_Shdr *sec = getRelSection(Rel);
709 switch (sec->sh_type) {
711 report_fatal_error("Invalid section type in Rel!");
713 return getRel(Rel)->r_offset;
715 return getRela(Rel)->r_offset;
719 template <class ELFT>
720 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
721 uint64_t &Result) const {
722 const Elf_Shdr *sec = getRelSection(Rel);
723 switch (sec->sh_type) {
725 report_fatal_error("Invalid section type in Rel!");
727 Result = getRel(Rel)->getType(EF.isMips64EL());
730 case ELF::SHT_RELA: {
731 Result = getRela(Rel)->getType(EF.isMips64EL());
735 return std::error_code();
738 template <class ELFT>
739 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
740 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
743 template <class ELFT>
744 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
745 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
746 const Elf_Shdr *sec = getRelSection(Rel);
748 switch (sec->sh_type) {
750 return object_error::parse_failed;
752 type = getRel(Rel)->getType(EF.isMips64EL());
755 case ELF::SHT_RELA: {
756 type = getRela(Rel)->getType(EF.isMips64EL());
761 EF.getRelocationTypeName(type, Result);
762 return std::error_code();
765 template <class ELFT>
767 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
768 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
769 return object_error::parse_failed;
770 return (int64_t)getRela(Rel)->r_addend;
773 template <class ELFT>
774 bool ELFObjectFile<ELFT>::hasRelocationAddend(DataRefImpl Rel) const {
775 return getRelSection(Rel)->sh_type == ELF::SHT_RELA;
778 template <class ELFT>
779 const typename ELFFile<ELFT>::Elf_Sym *
780 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
781 return &*toELFSymIter(Symb);
784 template <class ELFT>
785 const typename ELFObjectFile<ELFT>::Elf_Rel *
786 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
787 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
790 template <class ELFT>
791 const typename ELFObjectFile<ELFT>::Elf_Rela *
792 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
793 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
796 template <class ELFT>
797 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
799 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
803 EF(Data.getBuffer(), EC) {}
805 template <class ELFT>
806 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
807 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
810 template <class ELFT>
811 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
812 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
815 template <class ELFT>
816 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
817 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
820 template <class ELFT>
821 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
822 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
825 template <class ELFT>
826 section_iterator ELFObjectFile<ELFT>::section_begin() const {
827 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
830 template <class ELFT>
831 section_iterator ELFObjectFile<ELFT>::section_end() const {
832 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
835 template <class ELFT>
836 StringRef ELFObjectFile<ELFT>::getLoadName() const {
837 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
838 Elf_Dyn_Iter DE = EF.end_dynamic_table();
840 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
844 return EF.getDynamicString(DI->getVal());
848 template <class ELFT>
849 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
850 return ELFT::Is64Bits ? 8 : 4;
853 template <class ELFT>
854 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
855 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
856 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
857 case ELF::ELFCLASS32:
858 switch (EF.getHeader()->e_machine) {
862 return "ELF32-x86-64";
864 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
865 case ELF::EM_HEXAGON:
866 return "ELF32-hexagon";
872 case ELF::EM_SPARC32PLUS:
873 return "ELF32-sparc";
875 return "ELF32-unknown";
877 case ELF::ELFCLASS64:
878 switch (EF.getHeader()->e_machine) {
882 return "ELF64-x86-64";
883 case ELF::EM_AARCH64:
884 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
886 return "ELF64-ppc64";
889 case ELF::EM_SPARCV9:
890 return "ELF64-sparc";
894 return "ELF64-unknown";
897 // FIXME: Proper error handling.
898 report_fatal_error("Invalid ELFCLASS!");
902 template <class ELFT>
903 unsigned ELFObjectFile<ELFT>::getArch() const {
904 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
905 switch (EF.getHeader()->e_machine) {
909 return Triple::x86_64;
910 case ELF::EM_AARCH64:
911 return Triple::aarch64;
914 case ELF::EM_HEXAGON:
915 return Triple::hexagon;
917 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
918 case ELF::ELFCLASS32:
919 return IsLittleEndian ? Triple::mipsel : Triple::mips;
920 case ELF::ELFCLASS64:
921 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
923 report_fatal_error("Invalid ELFCLASS!");
928 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
930 return Triple::systemz;
933 case ELF::EM_SPARC32PLUS:
934 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
935 case ELF::EM_SPARCV9:
936 return Triple::sparcv9;
939 return Triple::UnknownArch;
943 template <class ELFT>
944 ELFObjectFileBase::elf_symbol_iterator_range
945 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
946 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
949 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
950 return EF.getHeader()->e_type == ELF::ET_REL;