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 uint32_t getSectionType(DataRefImpl Sec) const = 0;
51 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
54 virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
56 // FIXME: This is a bit of a hack. Every caller should know if it expecting
58 virtual bool hasRelocationAddend(DataRefImpl Rel) const = 0;
60 typedef iterator_range<elf_symbol_iterator> elf_symbol_iterator_range;
61 virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
63 elf_symbol_iterator_range symbols() const;
65 static inline bool classof(const Binary *v) { return v->isELF(); }
68 class ELFSectionRef : public SectionRef {
70 ELFSectionRef(const SectionRef &B) : SectionRef(B) {
71 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
74 const ELFObjectFileBase *getObject() const {
75 return cast<ELFObjectFileBase>(SectionRef::getObject());
78 uint32_t getType() const {
79 return getObject()->getSectionType(getRawDataRefImpl());
82 uint64_t getFlags() const {
83 return getObject()->getSectionFlags(getRawDataRefImpl());
87 class ELFSymbolRef : public SymbolRef {
89 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
90 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
93 const ELFObjectFileBase *getObject() const {
94 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
97 uint64_t getSize() const {
98 return getObject()->getSymbolSize(getRawDataRefImpl());
101 uint8_t getOther() const {
102 return getObject()->getSymbolOther(getRawDataRefImpl());
106 class elf_symbol_iterator : public symbol_iterator {
108 elf_symbol_iterator(const basic_symbol_iterator &B)
109 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
110 cast<ELFObjectFileBase>(B->getObject()))) {}
112 const ELFSymbolRef *operator->() const {
113 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
116 const ELFSymbolRef &operator*() const {
117 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
121 inline ELFObjectFileBase::elf_symbol_iterator_range
122 ELFObjectFileBase::symbols() const {
123 return elf_symbol_iterator_range(symbol_begin(), symbol_end());
126 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
127 uint64_t getSymbolSize(DataRefImpl Sym) const override;
130 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
132 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
134 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
135 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
136 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
137 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
138 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
139 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
141 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
142 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
143 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
148 void moveSymbolNext(DataRefImpl &Symb) const override;
149 std::error_code getSymbolName(DataRefImpl Symb,
150 StringRef &Res) const override;
151 std::error_code getSymbolAddress(DataRefImpl Symb,
152 uint64_t &Res) const override;
153 uint64_t getSymbolValue(DataRefImpl Symb) const override;
154 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
155 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
156 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
157 uint8_t getSymbolOther(DataRefImpl Symb) const override;
158 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
159 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
160 std::error_code getSymbolSection(DataRefImpl Symb,
161 section_iterator &Res) const override;
163 void moveSectionNext(DataRefImpl &Sec) const override;
164 std::error_code getSectionName(DataRefImpl Sec,
165 StringRef &Res) const override;
166 uint64_t getSectionAddress(DataRefImpl Sec) const override;
167 uint64_t getSectionSize(DataRefImpl Sec) const override;
168 std::error_code getSectionContents(DataRefImpl Sec,
169 StringRef &Res) const override;
170 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
171 bool isSectionText(DataRefImpl Sec) const override;
172 bool isSectionData(DataRefImpl Sec) const override;
173 bool isSectionBSS(DataRefImpl Sec) const override;
174 bool isSectionVirtual(DataRefImpl Sec) const override;
175 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
176 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
177 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
178 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
180 void moveRelocationNext(DataRefImpl &Rel) const override;
181 std::error_code getRelocationAddress(DataRefImpl Rel,
182 uint64_t &Res) const override;
183 std::error_code getRelocationOffset(DataRefImpl Rel,
184 uint64_t &Res) const override;
185 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
186 std::error_code getRelocationType(DataRefImpl Rel,
187 uint64_t &Res) const override;
189 getRelocationTypeName(DataRefImpl Rel,
190 SmallVectorImpl<char> &Result) const override;
192 uint32_t getSectionType(DataRefImpl Sec) const override;
193 uint64_t getSectionFlags(DataRefImpl Sec) const override;
194 uint64_t getROffset(DataRefImpl Rel) const;
195 StringRef getRelocationTypeName(uint32_t Type) const;
197 /// \brief Get the relocation section that contains \a Rel.
198 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
199 return EF.getSection(Rel.d.a);
202 const Elf_Rel *getRel(DataRefImpl Rel) const;
203 const Elf_Rela *getRela(DataRefImpl Rela) const;
205 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
206 bool IsDynamic = Symb.p & 1;
209 EF.begin_dynamic_symbols().getEntSize(),
210 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
211 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
212 reinterpret_cast<const char *>(Symb.p), IsDynamic);
215 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
217 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
218 static_cast<uintptr_t>(Symb.isDynamic());
222 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
223 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
224 reinterpret_cast<const char *>(Sec.p));
227 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
229 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
233 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
235 DRI.p = reinterpret_cast<uintptr_t>(Sec);
239 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
240 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
241 reinterpret_cast<const char *>(Dyn.p));
244 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
246 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
250 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
251 unsigned char Binding = ESym->getBinding();
252 unsigned char Visibility = ESym->getVisibility();
254 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
255 // visibility is either DEFAULT or PROTECTED. All other symbols are not
257 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
258 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
264 // This flag is used for classof, to distinguish ELFObjectFile from
265 // its subclass. If more subclasses will be created, this flag will
266 // have to become an enum.
267 bool isDyldELFObject;
270 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
272 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
274 basic_symbol_iterator symbol_begin_impl() const override;
275 basic_symbol_iterator symbol_end_impl() const override;
277 elf_symbol_iterator dynamic_symbol_begin() const;
278 elf_symbol_iterator dynamic_symbol_end() const;
280 section_iterator section_begin() const override;
281 section_iterator section_end() const override;
283 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
284 bool hasRelocationAddend(DataRefImpl Rel) const override;
286 uint8_t getBytesInAddress() const override;
287 StringRef getFileFormatName() const override;
288 unsigned getArch() const override;
289 StringRef getLoadName() const;
291 std::error_code getPlatformFlags(unsigned &Result) const override {
292 Result = EF.getHeader()->e_flags;
293 return std::error_code();
296 const ELFFile<ELFT> *getELFFile() const { return &EF; }
298 bool isDyldType() const { return isDyldELFObject; }
299 static inline bool classof(const Binary *v) {
300 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
304 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
306 bool isRelocatableObject() const override;
309 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
310 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
311 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
312 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
314 template <class ELFT>
315 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
316 Symb = toDRI(++toELFSymIter(Symb));
319 template <class ELFT>
320 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
321 StringRef &Result) const {
322 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
324 return Name.getError();
326 return std::error_code();
329 template <class ELFT>
330 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
331 return toELFShdrIter(Sec)->sh_flags;
334 template <class ELFT>
335 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
336 return toELFShdrIter(Sec)->sh_type;
339 template <class ELFT>
340 uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
341 const Elf_Sym *ESym = getSymbol(Symb);
342 switch (ESym->st_shndx) {
343 case ELF::SHN_COMMON:
345 return UnknownAddress;
347 return ESym->st_value;
350 const Elf_Ehdr *Header = EF.getHeader();
351 uint64_t Ret = ESym->st_value;
353 // Clear the ARM/Thumb or microMIPS indicator flag.
354 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
355 ESym->getType() == ELF::STT_FUNC)
361 template <class ELFT>
362 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
363 uint64_t &Result) const {
364 Result = getSymbolValue(Symb);
365 const Elf_Sym *ESym = getSymbol(Symb);
366 switch (ESym->st_shndx) {
367 case ELF::SHN_COMMON:
370 return std::error_code();
373 const Elf_Ehdr *Header = EF.getHeader();
375 if (Header->e_type == ELF::ET_REL) {
376 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
377 if (Section != nullptr)
378 Result += Section->sh_addr;
381 return std::error_code();
384 template <class ELFT>
385 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
386 Elf_Sym_Iter Sym = toELFSymIter(Symb);
387 if (Sym->st_shndx == ELF::SHN_COMMON)
388 return Sym->st_value;
392 template <class ELFT>
393 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
394 return toELFSymIter(Sym)->st_size;
397 template <class ELFT>
398 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
399 return toELFSymIter(Symb)->st_size;
402 template <class ELFT>
403 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
404 return toELFSymIter(Symb)->st_other;
407 template <class ELFT>
408 SymbolRef::Type ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
409 const Elf_Sym *ESym = getSymbol(Symb);
411 switch (ESym->getType()) {
412 case ELF::STT_NOTYPE:
413 return SymbolRef::ST_Unknown;
414 case ELF::STT_SECTION:
415 return SymbolRef::ST_Debug;
417 return SymbolRef::ST_File;
419 return SymbolRef::ST_Function;
420 case ELF::STT_OBJECT:
421 case ELF::STT_COMMON:
423 return SymbolRef::ST_Data;
425 return SymbolRef::ST_Other;
429 template <class ELFT>
430 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
431 Elf_Sym_Iter EIter = toELFSymIter(Symb);
432 const Elf_Sym *ESym = &*EIter;
434 uint32_t Result = SymbolRef::SF_None;
436 if (ESym->getBinding() != ELF::STB_LOCAL)
437 Result |= SymbolRef::SF_Global;
439 if (ESym->getBinding() == ELF::STB_WEAK)
440 Result |= SymbolRef::SF_Weak;
442 if (ESym->st_shndx == ELF::SHN_ABS)
443 Result |= SymbolRef::SF_Absolute;
445 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
446 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
447 Result |= SymbolRef::SF_FormatSpecific;
449 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
450 if (ErrorOr<StringRef> NameOrErr = EF.getSymbolName(EIter)) {
451 StringRef Name = *NameOrErr;
452 if (Name.startswith("$d") || Name.startswith("$t") ||
453 Name.startswith("$a"))
454 Result |= SymbolRef::SF_FormatSpecific;
458 if (ESym->st_shndx == ELF::SHN_UNDEF)
459 Result |= SymbolRef::SF_Undefined;
461 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
462 Result |= SymbolRef::SF_Common;
464 if (isExportedToOtherDSO(ESym))
465 Result |= SymbolRef::SF_Exported;
467 if (ESym->getVisibility() == ELF::STV_HIDDEN)
468 Result |= SymbolRef::SF_Hidden;
473 template <class ELFT>
475 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
476 const Elf_Shdr *ESec = EF.getSection(ESym);
478 return section_end();
481 Sec.p = reinterpret_cast<intptr_t>(ESec);
482 return section_iterator(SectionRef(Sec, this));
486 template <class ELFT>
488 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
489 section_iterator &Res) const {
490 Res = getSymbolSection(getSymbol(Symb));
491 return std::error_code();
494 template <class ELFT>
495 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
496 Sec = toDRI(++toELFShdrIter(Sec));
499 template <class ELFT>
500 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
501 StringRef &Result) const {
502 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
504 return Name.getError();
506 return std::error_code();
509 template <class ELFT>
510 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
511 return toELFShdrIter(Sec)->sh_addr;
514 template <class ELFT>
515 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
516 return toELFShdrIter(Sec)->sh_size;
519 template <class ELFT>
521 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
522 StringRef &Result) const {
523 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
524 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
525 return std::error_code();
528 template <class ELFT>
529 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
530 return toELFShdrIter(Sec)->sh_addralign;
533 template <class ELFT>
534 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
535 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
538 template <class ELFT>
539 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
540 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
541 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
542 EShdr->sh_type == ELF::SHT_PROGBITS;
545 template <class ELFT>
546 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
547 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
548 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
549 EShdr->sh_type == ELF::SHT_NOBITS;
552 template <class ELFT>
553 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
554 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
557 template <class ELFT>
558 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
559 DataRefImpl Symb) const {
560 Elf_Sym_Iter ESym = toELFSymIter(Symb);
562 uintX_t Index = ESym->st_shndx;
563 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
565 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
568 template <class ELFT>
570 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
572 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
573 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
575 return relocation_iterator(RelocationRef(RelData, this));
578 template <class ELFT>
580 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
582 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
583 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
584 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
585 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
588 RelData.d.b = S->sh_size / S->sh_entsize;
590 return relocation_iterator(RelocationRef(RelData, this));
593 template <class ELFT>
595 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
596 if (EF.getHeader()->e_type != ELF::ET_REL)
597 return section_end();
599 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
600 uintX_t Type = EShdr->sh_type;
601 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
602 return section_end();
604 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
605 return section_iterator(SectionRef(toDRI(R), this));
609 template <class ELFT>
610 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
614 template <class ELFT>
616 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
618 const Elf_Shdr *sec = getRelSection(Rel);
619 switch (sec->sh_type) {
621 report_fatal_error("Invalid section type in Rel!");
623 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
626 case ELF::SHT_RELA: {
627 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
634 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
636 DataRefImpl SymbolData;
637 switch (SymSec->sh_type) {
639 report_fatal_error("Invalid symbol table section type!");
640 case ELF::SHT_SYMTAB:
641 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
643 case ELF::SHT_DYNSYM:
644 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
648 return symbol_iterator(SymbolRef(SymbolData, this));
651 template <class ELFT>
653 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
654 uint64_t &Result) const {
655 uint64_t ROffset = getROffset(Rel);
656 const Elf_Ehdr *Header = EF.getHeader();
658 if (Header->e_type == ELF::ET_REL) {
659 const Elf_Shdr *RelocationSec = getRelSection(Rel);
660 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
661 Result = ROffset + RelocatedSec->sh_addr;
666 return std::error_code();
669 template <class ELFT>
671 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
672 uint64_t &Result) const {
673 assert(EF.getHeader()->e_type == ELF::ET_REL &&
674 "Only relocatable object files have relocation offsets");
675 Result = getROffset(Rel);
676 return std::error_code();
679 template <class ELFT>
680 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
681 const Elf_Shdr *sec = getRelSection(Rel);
682 switch (sec->sh_type) {
684 report_fatal_error("Invalid section type in Rel!");
686 return getRel(Rel)->r_offset;
688 return getRela(Rel)->r_offset;
692 template <class ELFT>
693 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
694 uint64_t &Result) const {
695 const Elf_Shdr *sec = getRelSection(Rel);
696 switch (sec->sh_type) {
698 report_fatal_error("Invalid section type in Rel!");
700 Result = getRel(Rel)->getType(EF.isMips64EL());
703 case ELF::SHT_RELA: {
704 Result = getRela(Rel)->getType(EF.isMips64EL());
708 return std::error_code();
711 template <class ELFT>
712 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
713 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
716 template <class ELFT>
717 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
718 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
719 const Elf_Shdr *sec = getRelSection(Rel);
721 switch (sec->sh_type) {
723 return object_error::parse_failed;
725 type = getRel(Rel)->getType(EF.isMips64EL());
728 case ELF::SHT_RELA: {
729 type = getRela(Rel)->getType(EF.isMips64EL());
734 EF.getRelocationTypeName(type, Result);
735 return std::error_code();
738 template <class ELFT>
740 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
741 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
742 return object_error::parse_failed;
743 return (int64_t)getRela(Rel)->r_addend;
746 template <class ELFT>
747 bool ELFObjectFile<ELFT>::hasRelocationAddend(DataRefImpl Rel) const {
748 return getRelSection(Rel)->sh_type == ELF::SHT_RELA;
751 template <class ELFT>
752 const typename ELFFile<ELFT>::Elf_Sym *
753 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
754 return &*toELFSymIter(Symb);
757 template <class ELFT>
758 const typename ELFObjectFile<ELFT>::Elf_Rel *
759 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
760 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
763 template <class ELFT>
764 const typename ELFObjectFile<ELFT>::Elf_Rela *
765 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
766 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
769 template <class ELFT>
770 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
772 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
776 EF(Data.getBuffer(), EC) {}
778 template <class ELFT>
779 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
780 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
783 template <class ELFT>
784 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
785 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
788 template <class ELFT>
789 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
790 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
793 template <class ELFT>
794 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
795 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
798 template <class ELFT>
799 section_iterator ELFObjectFile<ELFT>::section_begin() const {
800 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
803 template <class ELFT>
804 section_iterator ELFObjectFile<ELFT>::section_end() const {
805 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
808 template <class ELFT>
809 StringRef ELFObjectFile<ELFT>::getLoadName() const {
810 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
811 Elf_Dyn_Iter DE = EF.end_dynamic_table();
813 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
817 return EF.getDynamicString(DI->getVal());
821 template <class ELFT>
822 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
823 return ELFT::Is64Bits ? 8 : 4;
826 template <class ELFT>
827 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
828 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
829 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
830 case ELF::ELFCLASS32:
831 switch (EF.getHeader()->e_machine) {
835 return "ELF32-x86-64";
837 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
838 case ELF::EM_HEXAGON:
839 return "ELF32-hexagon";
845 case ELF::EM_SPARC32PLUS:
846 return "ELF32-sparc";
848 return "ELF32-unknown";
850 case ELF::ELFCLASS64:
851 switch (EF.getHeader()->e_machine) {
855 return "ELF64-x86-64";
856 case ELF::EM_AARCH64:
857 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
859 return "ELF64-ppc64";
862 case ELF::EM_SPARCV9:
863 return "ELF64-sparc";
867 return "ELF64-unknown";
870 // FIXME: Proper error handling.
871 report_fatal_error("Invalid ELFCLASS!");
875 template <class ELFT>
876 unsigned ELFObjectFile<ELFT>::getArch() const {
877 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
878 switch (EF.getHeader()->e_machine) {
882 return Triple::x86_64;
883 case ELF::EM_AARCH64:
884 return Triple::aarch64;
887 case ELF::EM_HEXAGON:
888 return Triple::hexagon;
890 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
891 case ELF::ELFCLASS32:
892 return IsLittleEndian ? Triple::mipsel : Triple::mips;
893 case ELF::ELFCLASS64:
894 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
896 report_fatal_error("Invalid ELFCLASS!");
901 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
903 return Triple::systemz;
906 case ELF::EM_SPARC32PLUS:
907 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
908 case ELF::EM_SPARCV9:
909 return Triple::sparcv9;
912 return Triple::UnknownArch;
916 template <class ELFT>
917 ELFObjectFileBase::elf_symbol_iterator_range
918 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
919 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
922 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
923 return EF.getHeader()->e_type == ELF::ET_REL;