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;
53 virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
55 // FIXME: This is a bit of a hack. Every caller should know if it expecting
57 virtual bool hasRelocationAddend(DataRefImpl Rel) const = 0;
59 typedef iterator_range<elf_symbol_iterator> elf_symbol_iterator_range;
60 virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
62 virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
64 elf_symbol_iterator_range symbols() const;
66 static inline bool classof(const Binary *v) { return v->isELF(); }
69 class ELFSectionRef : public SectionRef {
71 ELFSectionRef(const SectionRef &B) : SectionRef(B) {
72 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
75 const ELFObjectFileBase *getObject() const {
76 return cast<ELFObjectFileBase>(SectionRef::getObject());
79 uint32_t getType() const {
80 return getObject()->getSectionType(getRawDataRefImpl());
84 class ELFSymbolRef : public SymbolRef {
86 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
87 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
90 const ELFObjectFileBase *getObject() const {
91 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
94 uint64_t getSize() const {
95 return getObject()->getSymbolSize(getRawDataRefImpl());
98 uint8_t getOther() const {
99 return getObject()->getSymbolOther(getRawDataRefImpl());
103 class elf_symbol_iterator : public symbol_iterator {
105 elf_symbol_iterator(const basic_symbol_iterator &B)
106 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
107 cast<ELFObjectFileBase>(B->getObject()))) {}
109 const ELFSymbolRef *operator->() const {
110 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
113 const ELFSymbolRef &operator*() const {
114 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
118 inline ELFObjectFileBase::elf_symbol_iterator_range
119 ELFObjectFileBase::symbols() const {
120 return elf_symbol_iterator_range(symbol_begin(), symbol_end());
123 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
124 uint64_t getSymbolSize(DataRefImpl Sym) const override;
127 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
129 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
131 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
132 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
133 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
134 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
135 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
136 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
138 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
139 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
140 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
145 void moveSymbolNext(DataRefImpl &Symb) const override;
146 std::error_code getSymbolName(DataRefImpl Symb,
147 StringRef &Res) const override;
148 std::error_code getSymbolAddress(DataRefImpl Symb,
149 uint64_t &Res) const override;
150 uint64_t getSymbolValue(DataRefImpl Symb) const override;
151 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
152 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
153 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
154 uint8_t getSymbolOther(DataRefImpl Symb) const override;
155 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
156 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
157 std::error_code getSymbolSection(DataRefImpl Symb,
158 section_iterator &Res) const override;
160 void moveSectionNext(DataRefImpl &Sec) const override;
161 std::error_code getSectionName(DataRefImpl Sec,
162 StringRef &Res) const override;
163 uint64_t getSectionAddress(DataRefImpl Sec) const override;
164 uint64_t getSectionSize(DataRefImpl Sec) const override;
165 std::error_code getSectionContents(DataRefImpl Sec,
166 StringRef &Res) const override;
167 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
168 bool isSectionText(DataRefImpl Sec) const override;
169 bool isSectionData(DataRefImpl Sec) const override;
170 bool isSectionBSS(DataRefImpl Sec) const override;
171 bool isSectionVirtual(DataRefImpl Sec) const override;
172 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
173 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
174 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
175 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
177 void moveRelocationNext(DataRefImpl &Rel) const override;
178 std::error_code getRelocationAddress(DataRefImpl Rel,
179 uint64_t &Res) const override;
180 std::error_code getRelocationOffset(DataRefImpl Rel,
181 uint64_t &Res) const override;
182 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
183 std::error_code getRelocationType(DataRefImpl Rel,
184 uint64_t &Res) const override;
186 getRelocationTypeName(DataRefImpl Rel,
187 SmallVectorImpl<char> &Result) const override;
189 uint32_t getSectionType(DataRefImpl Sec) const override;
190 uint64_t getROffset(DataRefImpl Rel) const;
191 StringRef getRelocationTypeName(uint32_t Type) const;
193 /// \brief Get the relocation section that contains \a Rel.
194 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
195 return EF.getSection(Rel.d.a);
198 const Elf_Rel *getRel(DataRefImpl Rel) const;
199 const Elf_Rela *getRela(DataRefImpl Rela) const;
201 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
202 bool IsDynamic = Symb.p & 1;
205 EF.begin_dynamic_symbols().getEntSize(),
206 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
207 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
208 reinterpret_cast<const char *>(Symb.p), IsDynamic);
211 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
213 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
214 static_cast<uintptr_t>(Symb.isDynamic());
218 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
219 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
220 reinterpret_cast<const char *>(Sec.p));
223 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
225 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
229 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
231 DRI.p = reinterpret_cast<uintptr_t>(Sec);
235 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
236 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
237 reinterpret_cast<const char *>(Dyn.p));
240 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
242 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
246 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
247 unsigned char Binding = ESym->getBinding();
248 unsigned char Visibility = ESym->getVisibility();
250 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
251 // visibility is either DEFAULT or PROTECTED. All other symbols are not
253 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
254 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
260 // This flag is used for classof, to distinguish ELFObjectFile from
261 // its subclass. If more subclasses will be created, this flag will
262 // have to become an enum.
263 bool isDyldELFObject;
266 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
268 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
270 basic_symbol_iterator symbol_begin_impl() const override;
271 basic_symbol_iterator symbol_end_impl() const override;
273 elf_symbol_iterator dynamic_symbol_begin() const;
274 elf_symbol_iterator dynamic_symbol_end() const;
276 section_iterator section_begin() const override;
277 section_iterator section_end() const override;
279 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
280 bool hasRelocationAddend(DataRefImpl Rel) const override;
282 uint64_t getSectionFlags(SectionRef Sec) const override;
284 uint8_t getBytesInAddress() const override;
285 StringRef getFileFormatName() const override;
286 unsigned getArch() const override;
287 StringRef getLoadName() const;
289 std::error_code getPlatformFlags(unsigned &Result) const override {
290 Result = EF.getHeader()->e_flags;
291 return std::error_code();
294 const ELFFile<ELFT> *getELFFile() const { return &EF; }
296 bool isDyldType() const { return isDyldELFObject; }
297 static inline bool classof(const Binary *v) {
298 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
302 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
304 bool isRelocatableObject() const override;
307 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
308 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
309 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
310 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
312 template <class ELFT>
313 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
314 Symb = toDRI(++toELFSymIter(Symb));
317 template <class ELFT>
318 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
319 StringRef &Result) const {
320 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
322 return Name.getError();
324 return std::error_code();
327 template <class ELFT>
328 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
329 DataRefImpl DRI = Sec.getRawDataRefImpl();
330 return toELFShdrIter(DRI)->sh_flags;
333 template <class ELFT>
334 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
335 return toELFShdrIter(Sec)->sh_type;
338 template <class ELFT>
339 uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
340 const Elf_Sym *ESym = getSymbol(Symb);
341 switch (ESym->st_shndx) {
342 case ELF::SHN_COMMON:
344 return UnknownAddress;
346 return ESym->st_value;
349 const Elf_Ehdr *Header = EF.getHeader();
350 uint64_t Ret = ESym->st_value;
352 // Clear the ARM/Thumb or microMIPS indicator flag.
353 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
354 ESym->getType() == ELF::STT_FUNC)
360 template <class ELFT>
361 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
362 uint64_t &Result) const {
363 Result = getSymbolValue(Symb);
364 const Elf_Sym *ESym = getSymbol(Symb);
365 switch (ESym->st_shndx) {
366 case ELF::SHN_COMMON:
369 return std::error_code();
372 const Elf_Ehdr *Header = EF.getHeader();
374 if (Header->e_type == ELF::ET_REL) {
375 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
376 if (Section != nullptr)
377 Result += Section->sh_addr;
380 return std::error_code();
383 template <class ELFT>
384 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
385 Elf_Sym_Iter Sym = toELFSymIter(Symb);
386 if (Sym->st_shndx == ELF::SHN_COMMON)
387 return Sym->st_value;
391 template <class ELFT>
392 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
393 return toELFSymIter(Sym)->st_size;
396 template <class ELFT>
397 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
398 return toELFSymIter(Symb)->st_size;
401 template <class ELFT>
402 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
403 return toELFSymIter(Symb)->st_other;
406 template <class ELFT>
407 SymbolRef::Type ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
408 const Elf_Sym *ESym = getSymbol(Symb);
410 switch (ESym->getType()) {
411 case ELF::STT_NOTYPE:
412 return SymbolRef::ST_Unknown;
413 case ELF::STT_SECTION:
414 return SymbolRef::ST_Debug;
416 return SymbolRef::ST_File;
418 return SymbolRef::ST_Function;
419 case ELF::STT_OBJECT:
420 case ELF::STT_COMMON:
422 return SymbolRef::ST_Data;
424 return SymbolRef::ST_Other;
428 template <class ELFT>
429 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
430 Elf_Sym_Iter EIter = toELFSymIter(Symb);
431 const Elf_Sym *ESym = &*EIter;
433 uint32_t Result = SymbolRef::SF_None;
435 if (ESym->getBinding() != ELF::STB_LOCAL)
436 Result |= SymbolRef::SF_Global;
438 if (ESym->getBinding() == ELF::STB_WEAK)
439 Result |= SymbolRef::SF_Weak;
441 if (ESym->st_shndx == ELF::SHN_ABS)
442 Result |= SymbolRef::SF_Absolute;
444 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
445 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
446 Result |= SymbolRef::SF_FormatSpecific;
448 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
449 if (ErrorOr<StringRef> NameOrErr = EF.getSymbolName(EIter)) {
450 StringRef Name = *NameOrErr;
451 if (Name.startswith("$d") || Name.startswith("$t") ||
452 Name.startswith("$a"))
453 Result |= SymbolRef::SF_FormatSpecific;
457 if (ESym->st_shndx == ELF::SHN_UNDEF)
458 Result |= SymbolRef::SF_Undefined;
460 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
461 Result |= SymbolRef::SF_Common;
463 if (isExportedToOtherDSO(ESym))
464 Result |= SymbolRef::SF_Exported;
466 if (ESym->getVisibility() == ELF::STV_HIDDEN)
467 Result |= SymbolRef::SF_Hidden;
472 template <class ELFT>
474 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
475 const Elf_Shdr *ESec = EF.getSection(ESym);
477 return section_end();
480 Sec.p = reinterpret_cast<intptr_t>(ESec);
481 return section_iterator(SectionRef(Sec, this));
485 template <class ELFT>
487 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
488 section_iterator &Res) const {
489 Res = getSymbolSection(getSymbol(Symb));
490 return std::error_code();
493 template <class ELFT>
494 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
495 Sec = toDRI(++toELFShdrIter(Sec));
498 template <class ELFT>
499 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
500 StringRef &Result) const {
501 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
503 return Name.getError();
505 return std::error_code();
508 template <class ELFT>
509 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
510 return toELFShdrIter(Sec)->sh_addr;
513 template <class ELFT>
514 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
515 return toELFShdrIter(Sec)->sh_size;
518 template <class ELFT>
520 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
521 StringRef &Result) const {
522 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
523 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
524 return std::error_code();
527 template <class ELFT>
528 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
529 return toELFShdrIter(Sec)->sh_addralign;
532 template <class ELFT>
533 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
534 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
537 template <class ELFT>
538 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
539 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
540 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
541 EShdr->sh_type == ELF::SHT_PROGBITS;
544 template <class ELFT>
545 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
546 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
547 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
548 EShdr->sh_type == ELF::SHT_NOBITS;
551 template <class ELFT>
552 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
553 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
556 template <class ELFT>
557 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
558 DataRefImpl Symb) const {
559 Elf_Sym_Iter ESym = toELFSymIter(Symb);
561 uintX_t Index = ESym->st_shndx;
562 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
564 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
567 template <class ELFT>
569 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
571 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
572 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
574 return relocation_iterator(RelocationRef(RelData, this));
577 template <class ELFT>
579 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
581 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
582 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
583 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
584 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
587 RelData.d.b = S->sh_size / S->sh_entsize;
589 return relocation_iterator(RelocationRef(RelData, this));
592 template <class ELFT>
594 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
595 if (EF.getHeader()->e_type != ELF::ET_REL)
596 return section_end();
598 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
599 uintX_t Type = EShdr->sh_type;
600 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
601 return section_end();
603 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
604 return section_iterator(SectionRef(toDRI(R), this));
608 template <class ELFT>
609 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
613 template <class ELFT>
615 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
617 const Elf_Shdr *sec = getRelSection(Rel);
618 switch (sec->sh_type) {
620 report_fatal_error("Invalid section type in Rel!");
622 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
625 case ELF::SHT_RELA: {
626 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
633 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
635 DataRefImpl SymbolData;
636 switch (SymSec->sh_type) {
638 report_fatal_error("Invalid symbol table section type!");
639 case ELF::SHT_SYMTAB:
640 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
642 case ELF::SHT_DYNSYM:
643 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
647 return symbol_iterator(SymbolRef(SymbolData, this));
650 template <class ELFT>
652 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
653 uint64_t &Result) const {
654 uint64_t ROffset = getROffset(Rel);
655 const Elf_Ehdr *Header = EF.getHeader();
657 if (Header->e_type == ELF::ET_REL) {
658 const Elf_Shdr *RelocationSec = getRelSection(Rel);
659 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
660 Result = ROffset + RelocatedSec->sh_addr;
665 return std::error_code();
668 template <class ELFT>
670 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
671 uint64_t &Result) const {
672 assert(EF.getHeader()->e_type == ELF::ET_REL &&
673 "Only relocatable object files have relocation offsets");
674 Result = getROffset(Rel);
675 return std::error_code();
678 template <class ELFT>
679 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
680 const Elf_Shdr *sec = getRelSection(Rel);
681 switch (sec->sh_type) {
683 report_fatal_error("Invalid section type in Rel!");
685 return getRel(Rel)->r_offset;
687 return getRela(Rel)->r_offset;
691 template <class ELFT>
692 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
693 uint64_t &Result) const {
694 const Elf_Shdr *sec = getRelSection(Rel);
695 switch (sec->sh_type) {
697 report_fatal_error("Invalid section type in Rel!");
699 Result = getRel(Rel)->getType(EF.isMips64EL());
702 case ELF::SHT_RELA: {
703 Result = getRela(Rel)->getType(EF.isMips64EL());
707 return std::error_code();
710 template <class ELFT>
711 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
712 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
715 template <class ELFT>
716 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
717 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
718 const Elf_Shdr *sec = getRelSection(Rel);
720 switch (sec->sh_type) {
722 return object_error::parse_failed;
724 type = getRel(Rel)->getType(EF.isMips64EL());
727 case ELF::SHT_RELA: {
728 type = getRela(Rel)->getType(EF.isMips64EL());
733 EF.getRelocationTypeName(type, Result);
734 return std::error_code();
737 template <class ELFT>
739 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
740 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
741 return object_error::parse_failed;
742 return (int64_t)getRela(Rel)->r_addend;
745 template <class ELFT>
746 bool ELFObjectFile<ELFT>::hasRelocationAddend(DataRefImpl Rel) const {
747 return getRelSection(Rel)->sh_type == ELF::SHT_RELA;
750 template <class ELFT>
751 const typename ELFFile<ELFT>::Elf_Sym *
752 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
753 return &*toELFSymIter(Symb);
756 template <class ELFT>
757 const typename ELFObjectFile<ELFT>::Elf_Rel *
758 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
759 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
762 template <class ELFT>
763 const typename ELFObjectFile<ELFT>::Elf_Rela *
764 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
765 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
768 template <class ELFT>
769 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
771 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
775 EF(Data.getBuffer(), EC) {}
777 template <class ELFT>
778 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
779 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
782 template <class ELFT>
783 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
784 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
787 template <class ELFT>
788 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
789 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
792 template <class ELFT>
793 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
794 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
797 template <class ELFT>
798 section_iterator ELFObjectFile<ELFT>::section_begin() const {
799 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
802 template <class ELFT>
803 section_iterator ELFObjectFile<ELFT>::section_end() const {
804 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
807 template <class ELFT>
808 StringRef ELFObjectFile<ELFT>::getLoadName() const {
809 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
810 Elf_Dyn_Iter DE = EF.end_dynamic_table();
812 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
816 return EF.getDynamicString(DI->getVal());
820 template <class ELFT>
821 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
822 return ELFT::Is64Bits ? 8 : 4;
825 template <class ELFT>
826 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
827 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
828 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
829 case ELF::ELFCLASS32:
830 switch (EF.getHeader()->e_machine) {
834 return "ELF32-x86-64";
836 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
837 case ELF::EM_HEXAGON:
838 return "ELF32-hexagon";
844 case ELF::EM_SPARC32PLUS:
845 return "ELF32-sparc";
847 return "ELF32-unknown";
849 case ELF::ELFCLASS64:
850 switch (EF.getHeader()->e_machine) {
854 return "ELF64-x86-64";
855 case ELF::EM_AARCH64:
856 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
858 return "ELF64-ppc64";
861 case ELF::EM_SPARCV9:
862 return "ELF64-sparc";
866 return "ELF64-unknown";
869 // FIXME: Proper error handling.
870 report_fatal_error("Invalid ELFCLASS!");
874 template <class ELFT>
875 unsigned ELFObjectFile<ELFT>::getArch() const {
876 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
877 switch (EF.getHeader()->e_machine) {
881 return Triple::x86_64;
882 case ELF::EM_AARCH64:
883 return Triple::aarch64;
886 case ELF::EM_HEXAGON:
887 return Triple::hexagon;
889 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
890 case ELF::ELFCLASS32:
891 return IsLittleEndian ? Triple::mipsel : Triple::mips;
892 case ELF::ELFCLASS64:
893 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
895 report_fatal_error("Invalid ELFCLASS!");
900 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
902 return Triple::systemz;
905 case ELF::EM_SPARC32PLUS:
906 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
907 case ELF::EM_SPARCV9:
908 return Triple::sparcv9;
911 return Triple::UnknownArch;
915 template <class ELFT>
916 ELFObjectFileBase::elf_symbol_iterator_range
917 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
918 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
921 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
922 return EF.getHeader()->e_type == ELF::ET_REL;