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_Shdr_Iter Elf_Shdr_Iter;
163 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
168 void moveSymbolNext(DataRefImpl &Symb) const override;
169 std::error_code getSymbolName(DataRefImpl Symb,
170 StringRef &Res) const override;
171 std::error_code getSymbolAddress(DataRefImpl Symb,
172 uint64_t &Res) const override;
173 uint64_t getSymbolValue(DataRefImpl Symb) const override;
174 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
175 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
176 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
177 uint8_t getSymbolOther(DataRefImpl Symb) const override;
178 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
179 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
180 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
181 std::error_code getSymbolSection(DataRefImpl Symb,
182 section_iterator &Res) const override;
184 void moveSectionNext(DataRefImpl &Sec) const override;
185 std::error_code getSectionName(DataRefImpl Sec,
186 StringRef &Res) const override;
187 uint64_t getSectionAddress(DataRefImpl Sec) const override;
188 uint64_t getSectionSize(DataRefImpl Sec) const override;
189 std::error_code getSectionContents(DataRefImpl Sec,
190 StringRef &Res) const override;
191 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
192 bool isSectionText(DataRefImpl Sec) const override;
193 bool isSectionData(DataRefImpl Sec) const override;
194 bool isSectionBSS(DataRefImpl Sec) const override;
195 bool isSectionVirtual(DataRefImpl Sec) const override;
196 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
197 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
198 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
199 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
201 void moveRelocationNext(DataRefImpl &Rel) const override;
202 std::error_code getRelocationAddress(DataRefImpl Rel,
203 uint64_t &Res) const override;
204 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
205 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
206 std::error_code getRelocationType(DataRefImpl Rel,
207 uint64_t &Res) const override;
209 getRelocationTypeName(DataRefImpl Rel,
210 SmallVectorImpl<char> &Result) const override;
212 uint32_t getSectionType(DataRefImpl Sec) const override;
213 uint64_t getSectionFlags(DataRefImpl Sec) const override;
214 uint64_t getROffset(DataRefImpl Rel) const;
215 StringRef getRelocationTypeName(uint32_t Type) const;
217 /// \brief Get the relocation section that contains \a Rel.
218 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
219 return EF.getSection(Rel.d.a);
222 const Elf_Rel *getRel(DataRefImpl Rel) const;
223 const Elf_Rela *getRela(DataRefImpl Rela) const;
225 const Elf_Sym *toELFSymIter(DataRefImpl Sym) const {
226 return reinterpret_cast<const Elf_Sym *>(Sym.p & ~uintptr_t(1));
229 DataRefImpl toDRI(const Elf_Sym *Sym, bool IsDynamic) const {
232 reinterpret_cast<uintptr_t>(Sym) | static_cast<uintptr_t>(IsDynamic);
236 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
237 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
238 reinterpret_cast<const char *>(Sec.p));
241 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
243 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
247 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
249 DRI.p = reinterpret_cast<uintptr_t>(Sec);
253 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
254 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
255 reinterpret_cast<const char *>(Dyn.p));
258 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
260 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
264 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
265 unsigned char Binding = ESym->getBinding();
266 unsigned char Visibility = ESym->getVisibility();
268 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
269 // visibility is either DEFAULT or PROTECTED. All other symbols are not
271 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
272 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
278 // This flag is used for classof, to distinguish ELFObjectFile from
279 // its subclass. If more subclasses will be created, this flag will
280 // have to become an enum.
281 bool isDyldELFObject;
284 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
286 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
288 basic_symbol_iterator symbol_begin_impl() const override;
289 basic_symbol_iterator symbol_end_impl() const override;
291 elf_symbol_iterator dynamic_symbol_begin() const;
292 elf_symbol_iterator dynamic_symbol_end() const;
294 section_iterator section_begin() const override;
295 section_iterator section_end() const override;
297 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
298 bool hasRelocationAddend(DataRefImpl Rel) const override;
300 uint8_t getBytesInAddress() const override;
301 StringRef getFileFormatName() const override;
302 unsigned getArch() const override;
303 StringRef getLoadName() const;
305 std::error_code getPlatformFlags(unsigned &Result) const override {
306 Result = EF.getHeader()->e_flags;
307 return std::error_code();
310 const ELFFile<ELFT> *getELFFile() const { return &EF; }
312 bool isDyldType() const { return isDyldELFObject; }
313 static inline bool classof(const Binary *v) {
314 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
318 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
320 bool isRelocatableObject() const override;
323 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
324 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
325 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
326 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
328 template <class ELFT>
329 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
330 const Elf_Sym *S = toELFSymIter(Sym);
331 Sym = toDRI(++S, Sym.p & 1);
334 template <class ELFT>
335 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym,
336 StringRef &Result) const {
337 const Elf_Sym *ESym = toELFSymIter(Sym);
338 ErrorOr<StringRef> Name = EF.getSymbolName(ESym, Sym.p & 1);
340 return Name.getError();
342 return std::error_code();
345 template <class ELFT>
346 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
347 return toELFShdrIter(Sec)->sh_flags;
350 template <class ELFT>
351 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
352 return toELFShdrIter(Sec)->sh_type;
355 template <class ELFT>
356 uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
357 const Elf_Sym *ESym = getSymbol(Symb);
358 switch (ESym->st_shndx) {
359 case ELF::SHN_COMMON:
361 return UnknownAddress;
363 return ESym->st_value;
366 const Elf_Ehdr *Header = EF.getHeader();
367 uint64_t Ret = ESym->st_value;
369 // Clear the ARM/Thumb or microMIPS indicator flag.
370 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
371 ESym->getType() == ELF::STT_FUNC)
377 template <class ELFT>
378 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
379 uint64_t &Result) const {
380 Result = getSymbolValue(Symb);
381 const Elf_Sym *ESym = getSymbol(Symb);
382 switch (ESym->st_shndx) {
383 case ELF::SHN_COMMON:
386 return std::error_code();
389 const Elf_Ehdr *Header = EF.getHeader();
391 if (Header->e_type == ELF::ET_REL) {
392 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
393 if (Section != nullptr)
394 Result += Section->sh_addr;
397 return std::error_code();
400 template <class ELFT>
401 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
402 const Elf_Sym *Sym = toELFSymIter(Symb);
403 if (Sym->st_shndx == ELF::SHN_COMMON)
404 return Sym->st_value;
408 template <class ELFT>
409 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
410 return toELFSymIter(Sym)->st_size;
413 template <class ELFT>
414 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
415 return toELFSymIter(Symb)->st_size;
418 template <class ELFT>
419 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
420 return toELFSymIter(Symb)->st_other;
423 template <class ELFT>
424 uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
425 return toELFSymIter(Symb)->getType();
428 template <class ELFT>
429 SymbolRef::Type ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
430 const Elf_Sym *ESym = getSymbol(Symb);
432 switch (ESym->getType()) {
433 case ELF::STT_NOTYPE:
434 return SymbolRef::ST_Unknown;
435 case ELF::STT_SECTION:
436 return SymbolRef::ST_Debug;
438 return SymbolRef::ST_File;
440 return SymbolRef::ST_Function;
441 case ELF::STT_OBJECT:
442 case ELF::STT_COMMON:
444 return SymbolRef::ST_Data;
446 return SymbolRef::ST_Other;
450 template <class ELFT>
451 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
452 const Elf_Sym *ESym = toELFSymIter(Sym);
454 uint32_t Result = SymbolRef::SF_None;
456 if (ESym->getBinding() != ELF::STB_LOCAL)
457 Result |= SymbolRef::SF_Global;
459 if (ESym->getBinding() == ELF::STB_WEAK)
460 Result |= SymbolRef::SF_Weak;
462 if (ESym->st_shndx == ELF::SHN_ABS)
463 Result |= SymbolRef::SF_Absolute;
465 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
466 ESym == EF.begin_symbols() || ESym == EF.begin_dynamic_symbols())
467 Result |= SymbolRef::SF_FormatSpecific;
469 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
470 ErrorOr<StringRef> NameOrErr = EF.getSymbolName(ESym, Sym.p & 1);
472 StringRef Name = *NameOrErr;
473 if (Name.startswith("$d") || Name.startswith("$t") ||
474 Name.startswith("$a"))
475 Result |= SymbolRef::SF_FormatSpecific;
479 if (ESym->st_shndx == ELF::SHN_UNDEF)
480 Result |= SymbolRef::SF_Undefined;
482 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
483 Result |= SymbolRef::SF_Common;
485 if (isExportedToOtherDSO(ESym))
486 Result |= SymbolRef::SF_Exported;
488 if (ESym->getVisibility() == ELF::STV_HIDDEN)
489 Result |= SymbolRef::SF_Hidden;
494 template <class ELFT>
496 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
497 const Elf_Shdr *ESec = EF.getSection(ESym);
499 return section_end();
502 Sec.p = reinterpret_cast<intptr_t>(ESec);
503 return section_iterator(SectionRef(Sec, this));
507 template <class ELFT>
509 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
510 section_iterator &Res) const {
511 Res = getSymbolSection(getSymbol(Symb));
512 return std::error_code();
515 template <class ELFT>
516 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
517 Sec = toDRI(++toELFShdrIter(Sec));
520 template <class ELFT>
521 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
522 StringRef &Result) const {
523 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
525 return Name.getError();
527 return std::error_code();
530 template <class ELFT>
531 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
532 return toELFShdrIter(Sec)->sh_addr;
535 template <class ELFT>
536 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
537 return toELFShdrIter(Sec)->sh_size;
540 template <class ELFT>
542 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
543 StringRef &Result) const {
544 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
545 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
546 return std::error_code();
549 template <class ELFT>
550 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
551 return toELFShdrIter(Sec)->sh_addralign;
554 template <class ELFT>
555 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
556 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
559 template <class ELFT>
560 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
561 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
562 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
563 EShdr->sh_type == ELF::SHT_PROGBITS;
566 template <class ELFT>
567 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
568 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
569 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
570 EShdr->sh_type == ELF::SHT_NOBITS;
573 template <class ELFT>
574 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
575 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
578 template <class ELFT>
579 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
580 DataRefImpl Symb) const {
581 const Elf_Sym *ESym = toELFSymIter(Symb);
583 uintX_t Index = ESym->st_shndx;
584 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
586 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
589 template <class ELFT>
591 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
593 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
594 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
596 return relocation_iterator(RelocationRef(RelData, this));
599 template <class ELFT>
601 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
603 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
604 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
605 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
606 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
609 RelData.d.b = S->sh_size / S->sh_entsize;
611 return relocation_iterator(RelocationRef(RelData, this));
614 template <class ELFT>
616 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
617 if (EF.getHeader()->e_type != ELF::ET_REL)
618 return section_end();
620 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
621 uintX_t Type = EShdr->sh_type;
622 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
623 return section_end();
625 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
626 return section_iterator(SectionRef(toDRI(R), this));
630 template <class ELFT>
631 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
635 template <class ELFT>
637 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
639 const Elf_Shdr *sec = getRelSection(Rel);
640 switch (sec->sh_type) {
642 report_fatal_error("Invalid section type in Rel!");
644 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
647 case ELF::SHT_RELA: {
648 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
655 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
657 DataRefImpl SymbolData;
658 switch (SymSec->sh_type) {
660 report_fatal_error("Invalid symbol table section type!");
661 case ELF::SHT_SYMTAB:
662 SymbolData = toDRI(EF.begin_symbols() + symbolIdx, false);
664 case ELF::SHT_DYNSYM:
665 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx, true);
669 return symbol_iterator(SymbolRef(SymbolData, this));
672 template <class ELFT>
674 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
675 uint64_t &Result) const {
676 uint64_t ROffset = getROffset(Rel);
677 const Elf_Ehdr *Header = EF.getHeader();
679 if (Header->e_type == ELF::ET_REL) {
680 const Elf_Shdr *RelocationSec = getRelSection(Rel);
681 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
682 Result = ROffset + RelocatedSec->sh_addr;
687 return std::error_code();
690 template <class ELFT>
691 uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
692 assert(EF.getHeader()->e_type == ELF::ET_REL &&
693 "Only relocatable object files have relocation offsets");
694 return getROffset(Rel);
697 template <class ELFT>
698 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
699 const Elf_Shdr *sec = getRelSection(Rel);
700 switch (sec->sh_type) {
702 report_fatal_error("Invalid section type in Rel!");
704 return getRel(Rel)->r_offset;
706 return getRela(Rel)->r_offset;
710 template <class ELFT>
711 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
712 uint64_t &Result) const {
713 const Elf_Shdr *sec = getRelSection(Rel);
714 switch (sec->sh_type) {
716 report_fatal_error("Invalid section type in Rel!");
718 Result = getRel(Rel)->getType(EF.isMips64EL());
721 case ELF::SHT_RELA: {
722 Result = getRela(Rel)->getType(EF.isMips64EL());
726 return std::error_code();
729 template <class ELFT>
730 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
731 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
734 template <class ELFT>
735 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
736 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
737 const Elf_Shdr *sec = getRelSection(Rel);
739 switch (sec->sh_type) {
741 return object_error::parse_failed;
743 type = getRel(Rel)->getType(EF.isMips64EL());
746 case ELF::SHT_RELA: {
747 type = getRela(Rel)->getType(EF.isMips64EL());
752 EF.getRelocationTypeName(type, Result);
753 return std::error_code();
756 template <class ELFT>
758 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
759 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
760 return object_error::parse_failed;
761 return (int64_t)getRela(Rel)->r_addend;
764 template <class ELFT>
765 bool ELFObjectFile<ELFT>::hasRelocationAddend(DataRefImpl Rel) const {
766 return getRelSection(Rel)->sh_type == ELF::SHT_RELA;
769 template <class ELFT>
770 const typename ELFFile<ELFT>::Elf_Sym *
771 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
772 return &*toELFSymIter(Symb);
775 template <class ELFT>
776 const typename ELFObjectFile<ELFT>::Elf_Rel *
777 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
778 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
781 template <class ELFT>
782 const typename ELFObjectFile<ELFT>::Elf_Rela *
783 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
784 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
787 template <class ELFT>
788 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
790 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
794 EF(Data.getBuffer(), EC) {}
796 template <class ELFT>
797 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
798 DataRefImpl Sym = toDRI(EF.begin_symbols(), false);
799 return basic_symbol_iterator(SymbolRef(Sym, this));
802 template <class ELFT>
803 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
804 DataRefImpl Sym = toDRI(EF.end_symbols(), false);
805 return basic_symbol_iterator(SymbolRef(Sym, this));
808 template <class ELFT>
809 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
810 DataRefImpl Sym = toDRI(EF.begin_dynamic_symbols(), true);
811 return symbol_iterator(SymbolRef(Sym, this));
814 template <class ELFT>
815 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
816 DataRefImpl Sym = toDRI(EF.end_dynamic_symbols(), true);
817 return symbol_iterator(SymbolRef(Sym, this));
820 template <class ELFT>
821 section_iterator ELFObjectFile<ELFT>::section_begin() const {
822 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
825 template <class ELFT>
826 section_iterator ELFObjectFile<ELFT>::section_end() const {
827 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
830 template <class ELFT>
831 StringRef ELFObjectFile<ELFT>::getLoadName() const {
832 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
833 Elf_Dyn_Iter DE = EF.end_dynamic_table();
835 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
839 return EF.getDynamicString(DI->getVal());
843 template <class ELFT>
844 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
845 return ELFT::Is64Bits ? 8 : 4;
848 template <class ELFT>
849 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
850 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
851 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
852 case ELF::ELFCLASS32:
853 switch (EF.getHeader()->e_machine) {
857 return "ELF32-x86-64";
859 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
860 case ELF::EM_HEXAGON:
861 return "ELF32-hexagon";
867 case ELF::EM_SPARC32PLUS:
868 return "ELF32-sparc";
870 return "ELF32-unknown";
872 case ELF::ELFCLASS64:
873 switch (EF.getHeader()->e_machine) {
877 return "ELF64-x86-64";
878 case ELF::EM_AARCH64:
879 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
881 return "ELF64-ppc64";
884 case ELF::EM_SPARCV9:
885 return "ELF64-sparc";
889 return "ELF64-unknown";
892 // FIXME: Proper error handling.
893 report_fatal_error("Invalid ELFCLASS!");
897 template <class ELFT>
898 unsigned ELFObjectFile<ELFT>::getArch() const {
899 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
900 switch (EF.getHeader()->e_machine) {
904 return Triple::x86_64;
905 case ELF::EM_AARCH64:
906 return Triple::aarch64;
909 case ELF::EM_HEXAGON:
910 return Triple::hexagon;
912 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
913 case ELF::ELFCLASS32:
914 return IsLittleEndian ? Triple::mipsel : Triple::mips;
915 case ELF::ELFCLASS64:
916 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
918 report_fatal_error("Invalid ELFCLASS!");
923 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
925 return Triple::systemz;
928 case ELF::EM_SPARC32PLUS:
929 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
930 case ELF::EM_SPARCV9:
931 return Triple::sparcv9;
934 return Triple::UnknownArch;
938 template <class ELFT>
939 ELFObjectFileBase::elf_symbol_iterator_range
940 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
941 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
944 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
945 return EF.getHeader()->e_type == ELF::ET_REL;