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;
45 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
47 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
48 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
51 virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
53 // FIXME: This is a bit of a hack. Every caller should know if it expecting
55 virtual bool hasRelocationAddend(DataRefImpl Rel) const = 0;
57 typedef iterator_range<elf_symbol_iterator> elf_symbol_iterator_range;
58 virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
60 virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
61 virtual uint32_t getSectionType(SectionRef Sec) const = 0;
63 elf_symbol_iterator_range symbols() const;
65 static inline bool classof(const Binary *v) { return v->isELF(); }
68 class ELFSymbolRef : public SymbolRef {
70 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
71 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
74 const ELFObjectFileBase *getObject() const {
75 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
78 uint64_t getSize() const {
79 return getObject()->getSymbolSize(getRawDataRefImpl());
82 uint8_t getOther() const {
83 return getObject()->getSymbolOther(getRawDataRefImpl());
87 class elf_symbol_iterator : public symbol_iterator {
89 elf_symbol_iterator(const basic_symbol_iterator &B)
90 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
91 cast<ELFObjectFileBase>(B->getObject()))) {}
93 const ELFSymbolRef *operator->() const {
94 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
97 const ELFSymbolRef &operator*() const {
98 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
102 inline ELFObjectFileBase::elf_symbol_iterator_range
103 ELFObjectFileBase::symbols() const {
104 return elf_symbol_iterator_range(symbol_begin(), symbol_end());
107 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
108 uint64_t getSymbolSize(DataRefImpl Sym) const override;
111 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
113 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
115 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
116 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
117 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
118 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
119 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
120 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
122 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
123 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
124 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
129 void moveSymbolNext(DataRefImpl &Symb) const override;
130 std::error_code getSymbolName(DataRefImpl Symb,
131 StringRef &Res) const override;
132 std::error_code getSymbolAddress(DataRefImpl Symb,
133 uint64_t &Res) const override;
134 uint64_t getSymbolValue(DataRefImpl Symb) const override;
135 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
136 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
137 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
138 uint8_t getSymbolOther(DataRefImpl Symb) const override;
139 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
140 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
141 std::error_code getSymbolSection(DataRefImpl Symb,
142 section_iterator &Res) const override;
144 void moveSectionNext(DataRefImpl &Sec) const override;
145 std::error_code getSectionName(DataRefImpl Sec,
146 StringRef &Res) const override;
147 uint64_t getSectionAddress(DataRefImpl Sec) const override;
148 uint64_t getSectionSize(DataRefImpl Sec) const override;
149 std::error_code getSectionContents(DataRefImpl Sec,
150 StringRef &Res) const override;
151 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
152 bool isSectionText(DataRefImpl Sec) const override;
153 bool isSectionData(DataRefImpl Sec) const override;
154 bool isSectionBSS(DataRefImpl Sec) const override;
155 bool isSectionVirtual(DataRefImpl Sec) const override;
156 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
157 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
158 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
159 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
161 void moveRelocationNext(DataRefImpl &Rel) const override;
162 std::error_code getRelocationAddress(DataRefImpl Rel,
163 uint64_t &Res) const override;
164 std::error_code getRelocationOffset(DataRefImpl Rel,
165 uint64_t &Res) const override;
166 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
167 std::error_code getRelocationType(DataRefImpl Rel,
168 uint64_t &Res) const override;
170 getRelocationTypeName(DataRefImpl Rel,
171 SmallVectorImpl<char> &Result) const override;
173 uint64_t getROffset(DataRefImpl Rel) const;
174 StringRef getRelocationTypeName(uint32_t Type) const;
176 /// \brief Get the relocation section that contains \a Rel.
177 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
178 return EF.getSection(Rel.d.a);
181 const Elf_Rel *getRel(DataRefImpl Rel) const;
182 const Elf_Rela *getRela(DataRefImpl Rela) const;
184 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
185 bool IsDynamic = Symb.p & 1;
188 EF.begin_dynamic_symbols().getEntSize(),
189 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
190 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
191 reinterpret_cast<const char *>(Symb.p), IsDynamic);
194 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
196 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
197 static_cast<uintptr_t>(Symb.isDynamic());
201 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
202 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
203 reinterpret_cast<const char *>(Sec.p));
206 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
208 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
212 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
214 DRI.p = reinterpret_cast<uintptr_t>(Sec);
218 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
219 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
220 reinterpret_cast<const char *>(Dyn.p));
223 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
225 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
229 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
230 unsigned char Binding = ESym->getBinding();
231 unsigned char Visibility = ESym->getVisibility();
233 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
234 // visibility is either DEFAULT or PROTECTED. All other symbols are not
236 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
237 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
243 // This flag is used for classof, to distinguish ELFObjectFile from
244 // its subclass. If more subclasses will be created, this flag will
245 // have to become an enum.
246 bool isDyldELFObject;
249 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
251 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
253 basic_symbol_iterator symbol_begin_impl() const override;
254 basic_symbol_iterator symbol_end_impl() const override;
256 elf_symbol_iterator dynamic_symbol_begin() const;
257 elf_symbol_iterator dynamic_symbol_end() const;
259 section_iterator section_begin() const override;
260 section_iterator section_end() const override;
262 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
263 bool hasRelocationAddend(DataRefImpl Rel) const override;
265 uint64_t getSectionFlags(SectionRef Sec) const override;
266 uint32_t getSectionType(SectionRef Sec) const override;
268 uint8_t getBytesInAddress() const override;
269 StringRef getFileFormatName() const override;
270 unsigned getArch() const override;
271 StringRef getLoadName() const;
273 std::error_code getPlatformFlags(unsigned &Result) const override {
274 Result = EF.getHeader()->e_flags;
275 return std::error_code();
278 const ELFFile<ELFT> *getELFFile() const { return &EF; }
280 bool isDyldType() const { return isDyldELFObject; }
281 static inline bool classof(const Binary *v) {
282 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
286 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
288 bool isRelocatableObject() const override;
291 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
292 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
293 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
294 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
296 template <class ELFT>
297 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
298 Symb = toDRI(++toELFSymIter(Symb));
301 template <class ELFT>
302 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
303 StringRef &Result) const {
304 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
306 return Name.getError();
308 return std::error_code();
311 template <class ELFT>
312 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
313 DataRefImpl DRI = Sec.getRawDataRefImpl();
314 return toELFShdrIter(DRI)->sh_flags;
317 template <class ELFT>
318 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
319 DataRefImpl DRI = Sec.getRawDataRefImpl();
320 return toELFShdrIter(DRI)->sh_type;
323 template <class ELFT>
324 uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
325 const Elf_Sym *ESym = getSymbol(Symb);
326 switch (ESym->st_shndx) {
327 case ELF::SHN_COMMON:
329 return UnknownAddress;
331 return ESym->st_value;
334 const Elf_Ehdr *Header = EF.getHeader();
335 uint64_t Ret = ESym->st_value;
337 // Clear the ARM/Thumb or microMIPS indicator flag.
338 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
339 ESym->getType() == ELF::STT_FUNC)
345 template <class ELFT>
346 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
347 uint64_t &Result) const {
348 Result = getSymbolValue(Symb);
349 const Elf_Sym *ESym = getSymbol(Symb);
350 switch (ESym->st_shndx) {
351 case ELF::SHN_COMMON:
354 return std::error_code();
357 const Elf_Ehdr *Header = EF.getHeader();
359 if (Header->e_type == ELF::ET_REL) {
360 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
361 if (Section != nullptr)
362 Result += Section->sh_addr;
365 return std::error_code();
368 template <class ELFT>
369 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
370 Elf_Sym_Iter Sym = toELFSymIter(Symb);
371 if (Sym->st_shndx == ELF::SHN_COMMON)
372 return Sym->st_value;
376 template <class ELFT>
377 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
378 return toELFSymIter(Sym)->st_size;
381 template <class ELFT>
382 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
383 return toELFSymIter(Symb)->st_size;
386 template <class ELFT>
387 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
388 return toELFSymIter(Symb)->st_other;
391 template <class ELFT>
392 SymbolRef::Type ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
393 const Elf_Sym *ESym = getSymbol(Symb);
395 switch (ESym->getType()) {
396 case ELF::STT_NOTYPE:
397 return SymbolRef::ST_Unknown;
398 case ELF::STT_SECTION:
399 return SymbolRef::ST_Debug;
401 return SymbolRef::ST_File;
403 return SymbolRef::ST_Function;
404 case ELF::STT_OBJECT:
405 case ELF::STT_COMMON:
407 return SymbolRef::ST_Data;
409 return SymbolRef::ST_Other;
413 template <class ELFT>
414 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
415 Elf_Sym_Iter EIter = toELFSymIter(Symb);
416 const Elf_Sym *ESym = &*EIter;
418 uint32_t Result = SymbolRef::SF_None;
420 if (ESym->getBinding() != ELF::STB_LOCAL)
421 Result |= SymbolRef::SF_Global;
423 if (ESym->getBinding() == ELF::STB_WEAK)
424 Result |= SymbolRef::SF_Weak;
426 if (ESym->st_shndx == ELF::SHN_ABS)
427 Result |= SymbolRef::SF_Absolute;
429 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
430 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
431 Result |= SymbolRef::SF_FormatSpecific;
433 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
434 if (ErrorOr<StringRef> NameOrErr = EF.getSymbolName(EIter)) {
435 StringRef Name = *NameOrErr;
436 if (Name.startswith("$d") || Name.startswith("$t") ||
437 Name.startswith("$a"))
438 Result |= SymbolRef::SF_FormatSpecific;
442 if (ESym->st_shndx == ELF::SHN_UNDEF)
443 Result |= SymbolRef::SF_Undefined;
445 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
446 Result |= SymbolRef::SF_Common;
448 if (isExportedToOtherDSO(ESym))
449 Result |= SymbolRef::SF_Exported;
451 if (ESym->getVisibility() == ELF::STV_HIDDEN)
452 Result |= SymbolRef::SF_Hidden;
457 template <class ELFT>
459 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
460 const Elf_Shdr *ESec = EF.getSection(ESym);
462 return section_end();
465 Sec.p = reinterpret_cast<intptr_t>(ESec);
466 return section_iterator(SectionRef(Sec, this));
470 template <class ELFT>
472 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
473 section_iterator &Res) const {
474 Res = getSymbolSection(getSymbol(Symb));
475 return std::error_code();
478 template <class ELFT>
479 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
480 Sec = toDRI(++toELFShdrIter(Sec));
483 template <class ELFT>
484 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
485 StringRef &Result) const {
486 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
488 return Name.getError();
490 return std::error_code();
493 template <class ELFT>
494 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
495 return toELFShdrIter(Sec)->sh_addr;
498 template <class ELFT>
499 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
500 return toELFShdrIter(Sec)->sh_size;
503 template <class ELFT>
505 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
506 StringRef &Result) const {
507 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
508 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
509 return std::error_code();
512 template <class ELFT>
513 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
514 return toELFShdrIter(Sec)->sh_addralign;
517 template <class ELFT>
518 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
519 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
522 template <class ELFT>
523 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
524 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
525 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
526 EShdr->sh_type == ELF::SHT_PROGBITS;
529 template <class ELFT>
530 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
531 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
532 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
533 EShdr->sh_type == ELF::SHT_NOBITS;
536 template <class ELFT>
537 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
538 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
541 template <class ELFT>
542 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
543 DataRefImpl Symb) const {
544 Elf_Sym_Iter ESym = toELFSymIter(Symb);
546 uintX_t Index = ESym->st_shndx;
547 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
549 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
552 template <class ELFT>
554 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
556 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
557 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
559 return relocation_iterator(RelocationRef(RelData, this));
562 template <class ELFT>
564 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
566 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
567 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
568 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
569 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
572 RelData.d.b = S->sh_size / S->sh_entsize;
574 return relocation_iterator(RelocationRef(RelData, this));
577 template <class ELFT>
579 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
580 if (EF.getHeader()->e_type != ELF::ET_REL)
581 return section_end();
583 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
584 uintX_t Type = EShdr->sh_type;
585 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
586 return section_end();
588 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
589 return section_iterator(SectionRef(toDRI(R), this));
593 template <class ELFT>
594 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
598 template <class ELFT>
600 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
602 const Elf_Shdr *sec = getRelSection(Rel);
603 switch (sec->sh_type) {
605 report_fatal_error("Invalid section type in Rel!");
607 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
610 case ELF::SHT_RELA: {
611 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
618 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
620 DataRefImpl SymbolData;
621 switch (SymSec->sh_type) {
623 report_fatal_error("Invalid symbol table section type!");
624 case ELF::SHT_SYMTAB:
625 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
627 case ELF::SHT_DYNSYM:
628 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
632 return symbol_iterator(SymbolRef(SymbolData, this));
635 template <class ELFT>
637 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
638 uint64_t &Result) const {
639 uint64_t ROffset = getROffset(Rel);
640 const Elf_Ehdr *Header = EF.getHeader();
642 if (Header->e_type == ELF::ET_REL) {
643 const Elf_Shdr *RelocationSec = getRelSection(Rel);
644 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
645 Result = ROffset + RelocatedSec->sh_addr;
650 return std::error_code();
653 template <class ELFT>
655 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
656 uint64_t &Result) const {
657 assert(EF.getHeader()->e_type == ELF::ET_REL &&
658 "Only relocatable object files have relocation offsets");
659 Result = getROffset(Rel);
660 return std::error_code();
663 template <class ELFT>
664 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
665 const Elf_Shdr *sec = getRelSection(Rel);
666 switch (sec->sh_type) {
668 report_fatal_error("Invalid section type in Rel!");
670 return getRel(Rel)->r_offset;
672 return getRela(Rel)->r_offset;
676 template <class ELFT>
677 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
678 uint64_t &Result) const {
679 const Elf_Shdr *sec = getRelSection(Rel);
680 switch (sec->sh_type) {
682 report_fatal_error("Invalid section type in Rel!");
684 Result = getRel(Rel)->getType(EF.isMips64EL());
687 case ELF::SHT_RELA: {
688 Result = getRela(Rel)->getType(EF.isMips64EL());
692 return std::error_code();
695 template <class ELFT>
696 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
697 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
700 template <class ELFT>
701 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
702 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
703 const Elf_Shdr *sec = getRelSection(Rel);
705 switch (sec->sh_type) {
707 return object_error::parse_failed;
709 type = getRel(Rel)->getType(EF.isMips64EL());
712 case ELF::SHT_RELA: {
713 type = getRela(Rel)->getType(EF.isMips64EL());
718 EF.getRelocationTypeName(type, Result);
719 return std::error_code();
722 template <class ELFT>
724 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
725 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
726 return object_error::parse_failed;
727 return (int64_t)getRela(Rel)->r_addend;
730 template <class ELFT>
731 bool ELFObjectFile<ELFT>::hasRelocationAddend(DataRefImpl Rel) const {
732 return getRelSection(Rel)->sh_type == ELF::SHT_RELA;
735 template <class ELFT>
736 const typename ELFFile<ELFT>::Elf_Sym *
737 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
738 return &*toELFSymIter(Symb);
741 template <class ELFT>
742 const typename ELFObjectFile<ELFT>::Elf_Rel *
743 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
744 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
747 template <class ELFT>
748 const typename ELFObjectFile<ELFT>::Elf_Rela *
749 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
750 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
753 template <class ELFT>
754 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
756 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
760 EF(Data.getBuffer(), EC) {}
762 template <class ELFT>
763 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
764 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
767 template <class ELFT>
768 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
769 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
772 template <class ELFT>
773 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
774 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
777 template <class ELFT>
778 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
779 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
782 template <class ELFT>
783 section_iterator ELFObjectFile<ELFT>::section_begin() const {
784 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
787 template <class ELFT>
788 section_iterator ELFObjectFile<ELFT>::section_end() const {
789 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
792 template <class ELFT>
793 StringRef ELFObjectFile<ELFT>::getLoadName() const {
794 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
795 Elf_Dyn_Iter DE = EF.end_dynamic_table();
797 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
801 return EF.getDynamicString(DI->getVal());
805 template <class ELFT>
806 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
807 return ELFT::Is64Bits ? 8 : 4;
810 template <class ELFT>
811 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
812 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
813 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
814 case ELF::ELFCLASS32:
815 switch (EF.getHeader()->e_machine) {
819 return "ELF32-x86-64";
821 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
822 case ELF::EM_HEXAGON:
823 return "ELF32-hexagon";
829 case ELF::EM_SPARC32PLUS:
830 return "ELF32-sparc";
832 return "ELF32-unknown";
834 case ELF::ELFCLASS64:
835 switch (EF.getHeader()->e_machine) {
839 return "ELF64-x86-64";
840 case ELF::EM_AARCH64:
841 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
843 return "ELF64-ppc64";
846 case ELF::EM_SPARCV9:
847 return "ELF64-sparc";
851 return "ELF64-unknown";
854 // FIXME: Proper error handling.
855 report_fatal_error("Invalid ELFCLASS!");
859 template <class ELFT>
860 unsigned ELFObjectFile<ELFT>::getArch() const {
861 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
862 switch (EF.getHeader()->e_machine) {
866 return Triple::x86_64;
867 case ELF::EM_AARCH64:
868 return Triple::aarch64;
871 case ELF::EM_HEXAGON:
872 return Triple::hexagon;
874 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
875 case ELF::ELFCLASS32:
876 return IsLittleEndian ? Triple::mipsel : Triple::mips;
877 case ELF::ELFCLASS64:
878 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
880 report_fatal_error("Invalid ELFCLASS!");
885 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
887 return Triple::systemz;
890 case ELF::EM_SPARC32PLUS:
891 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
892 case ELF::EM_SPARCV9:
893 return Triple::sparcv9;
896 return Triple::UnknownArch;
900 template <class ELFT>
901 ELFObjectFileBase::elf_symbol_iterator_range
902 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
903 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
906 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
907 return EF.getHeader()->e_type == ELF::ET_REL;