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 ELFObjectFileBase : public ObjectFile {
40 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
43 virtual std::error_code getRelocationAddend(DataRefImpl Rel,
44 int64_t &Res) const = 0;
45 virtual std::pair<symbol_iterator, symbol_iterator>
46 getELFDynamicSymbolIterators() const = 0;
48 virtual std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
49 bool &IsDefault) const = 0;
51 virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
52 virtual uint32_t getSectionType(SectionRef Sec) const = 0;
54 static inline bool classof(const Binary *v) { return v->isELF(); }
57 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
59 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
61 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
63 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
64 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
65 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
66 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
67 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
68 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
70 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
71 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
72 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
77 void moveSymbolNext(DataRefImpl &Symb) const override;
78 std::error_code getSymbolName(DataRefImpl Symb,
79 StringRef &Res) const override;
80 std::error_code getSymbolAddress(DataRefImpl Symb,
81 uint64_t &Res) const override;
82 std::error_code getSymbolAlignment(DataRefImpl Symb,
83 uint32_t &Res) const override;
84 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
85 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
86 std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
87 std::error_code getSymbolType(DataRefImpl Symb,
88 SymbolRef::Type &Res) const override;
89 std::error_code getSymbolSection(DataRefImpl Symb,
90 section_iterator &Res) const override;
92 void moveSectionNext(DataRefImpl &Sec) const override;
93 std::error_code getSectionName(DataRefImpl Sec,
94 StringRef &Res) const override;
95 uint64_t getSectionAddress(DataRefImpl Sec) const override;
96 uint64_t getSectionSize(DataRefImpl Sec) const override;
97 std::error_code getSectionContents(DataRefImpl Sec,
98 StringRef &Res) const override;
99 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
100 bool isSectionText(DataRefImpl Sec) const override;
101 bool isSectionData(DataRefImpl Sec) const override;
102 bool isSectionBSS(DataRefImpl Sec) const override;
103 bool isSectionVirtual(DataRefImpl Sec) const override;
104 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
105 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
106 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
107 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
109 void moveRelocationNext(DataRefImpl &Rel) const override;
110 std::error_code getRelocationAddress(DataRefImpl Rel,
111 uint64_t &Res) const override;
112 std::error_code getRelocationOffset(DataRefImpl Rel,
113 uint64_t &Res) const override;
114 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
115 std::error_code getRelocationType(DataRefImpl Rel,
116 uint64_t &Res) const override;
118 getRelocationTypeName(DataRefImpl Rel,
119 SmallVectorImpl<char> &Result) const override;
121 getRelocationValueString(DataRefImpl Rel,
122 SmallVectorImpl<char> &Result) const override;
124 uint64_t getROffset(DataRefImpl Rel) const;
125 StringRef getRelocationTypeName(uint32_t Type) const;
127 /// \brief Get the relocation section that contains \a Rel.
128 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
129 return EF.getSection(Rel.d.a);
132 const Elf_Rel *getRel(DataRefImpl Rel) const;
133 const Elf_Rela *getRela(DataRefImpl Rela) const;
135 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
136 bool IsDynamic = Symb.p & 1;
139 EF.begin_dynamic_symbols().getEntSize(),
140 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
141 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
142 reinterpret_cast<const char *>(Symb.p), IsDynamic);
145 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
147 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
148 static_cast<uintptr_t>(Symb.isDynamic());
152 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
153 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
154 reinterpret_cast<const char *>(Sec.p));
157 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
159 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
163 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
165 DRI.p = reinterpret_cast<uintptr_t>(Sec);
169 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
170 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
171 reinterpret_cast<const char *>(Dyn.p));
174 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
176 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
180 // This flag is used for classof, to distinguish ELFObjectFile from
181 // its subclass. If more subclasses will be created, this flag will
182 // have to become an enum.
183 bool isDyldELFObject;
186 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
188 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
190 basic_symbol_iterator symbol_begin_impl() const override;
191 basic_symbol_iterator symbol_end_impl() const override;
193 symbol_iterator dynamic_symbol_begin() const;
194 symbol_iterator dynamic_symbol_end() const;
196 section_iterator section_begin() const override;
197 section_iterator section_end() const override;
199 std::error_code getRelocationAddend(DataRefImpl Rel,
200 int64_t &Res) const override;
201 std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
202 bool &IsDefault) const override;
204 uint64_t getSectionFlags(SectionRef Sec) const override;
205 uint32_t getSectionType(SectionRef Sec) const override;
207 uint8_t getBytesInAddress() const override;
208 StringRef getFileFormatName() const override;
209 unsigned getArch() const override;
210 StringRef getLoadName() const;
212 std::error_code getPlatformFlags(unsigned &Result) const override {
213 Result = EF.getHeader()->e_flags;
214 return object_error::success;
217 const ELFFile<ELFT> *getELFFile() const { return &EF; }
219 bool isDyldType() const { return isDyldELFObject; }
220 static inline bool classof(const Binary *v) {
221 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
225 std::pair<symbol_iterator, symbol_iterator>
226 getELFDynamicSymbolIterators() const override;
228 bool isRelocatableObject() const override;
231 // Use an alignment of 2 for the typedefs since that is the worst case for
232 // ELF files in archives.
233 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
234 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
235 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
236 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
238 template <class ELFT>
239 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
240 Symb = toDRI(++toELFSymIter(Symb));
243 template <class ELFT>
244 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
245 StringRef &Result) const {
246 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
248 return Name.getError();
250 return object_error::success;
253 template <class ELFT>
254 std::error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
256 bool &IsDefault) const {
257 DataRefImpl Symb = SymRef.getRawDataRefImpl();
258 const Elf_Sym *symb = getSymbol(Symb);
259 ErrorOr<StringRef> Ver =
260 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
262 return Ver.getError();
264 return object_error::success;
267 template <class ELFT>
268 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
269 DataRefImpl DRI = Sec.getRawDataRefImpl();
270 return toELFShdrIter(DRI)->sh_flags;
273 template <class ELFT>
274 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
275 DataRefImpl DRI = Sec.getRawDataRefImpl();
276 return toELFShdrIter(DRI)->sh_type;
279 template <class ELFT>
280 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
281 uint64_t &Result) const {
282 const Elf_Sym *ESym = getSymbol(Symb);
283 switch (EF.getSymbolTableIndex(ESym)) {
284 case ELF::SHN_COMMON:
286 Result = UnknownAddressOrSize;
287 return object_error::success;
289 Result = ESym->st_value;
290 return object_error::success;
295 const Elf_Ehdr *Header = EF.getHeader();
296 Result = ESym->st_value;
298 // Clear the ARM/Thumb or microMIPS indicator flag.
299 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
300 ESym->getType() == ELF::STT_FUNC)
303 if (Header->e_type == ELF::ET_REL)
304 Result += EF.getSection(ESym)->sh_addr;
306 return object_error::success;
309 template <class ELFT>
310 std::error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
311 uint32_t &Res) const {
312 Elf_Sym_Iter Sym = toELFSymIter(Symb);
313 if (Sym->st_shndx == ELF::SHN_COMMON)
317 return object_error::success;
320 template <class ELFT>
321 std::error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
322 uint64_t &Result) const {
323 Result = toELFSymIter(Symb)->st_size;
324 return object_error::success;
327 template <class ELFT>
328 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
329 uint8_t &Result) const {
330 Result = toELFSymIter(Symb)->st_other;
331 return object_error::success;
334 template <class ELFT>
336 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
337 SymbolRef::Type &Result) const {
338 const Elf_Sym *ESym = getSymbol(Symb);
340 switch (ESym->getType()) {
341 case ELF::STT_NOTYPE:
342 Result = SymbolRef::ST_Unknown;
344 case ELF::STT_SECTION:
345 Result = SymbolRef::ST_Debug;
348 Result = SymbolRef::ST_File;
351 Result = SymbolRef::ST_Function;
353 case ELF::STT_OBJECT:
354 case ELF::STT_COMMON:
356 Result = SymbolRef::ST_Data;
359 Result = SymbolRef::ST_Other;
362 return object_error::success;
365 template <class ELFT>
366 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
367 Elf_Sym_Iter EIter = toELFSymIter(Symb);
368 const Elf_Sym *ESym = &*EIter;
370 uint32_t Result = SymbolRef::SF_None;
372 if (ESym->getBinding() != ELF::STB_LOCAL)
373 Result |= SymbolRef::SF_Global;
375 if (ESym->getBinding() == ELF::STB_WEAK)
376 Result |= SymbolRef::SF_Weak;
378 if (ESym->st_shndx == ELF::SHN_ABS)
379 Result |= SymbolRef::SF_Absolute;
381 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
382 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
383 Result |= SymbolRef::SF_FormatSpecific;
385 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
386 Result |= SymbolRef::SF_Undefined;
388 if (ESym->getType() == ELF::STT_COMMON ||
389 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
390 Result |= SymbolRef::SF_Common;
395 template <class ELFT>
397 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
398 section_iterator &Res) const {
399 const Elf_Sym *ESym = getSymbol(Symb);
400 const Elf_Shdr *ESec = EF.getSection(ESym);
405 Sec.p = reinterpret_cast<intptr_t>(ESec);
406 Res = section_iterator(SectionRef(Sec, this));
408 return object_error::success;
411 template <class ELFT>
412 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
413 Sec = toDRI(++toELFShdrIter(Sec));
416 template <class ELFT>
417 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
418 StringRef &Result) const {
419 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
421 return Name.getError();
423 return object_error::success;
426 template <class ELFT>
427 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
428 return toELFShdrIter(Sec)->sh_addr;
431 template <class ELFT>
432 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
433 return toELFShdrIter(Sec)->sh_size;
436 template <class ELFT>
438 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
439 StringRef &Result) const {
440 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
441 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
442 return object_error::success;
445 template <class ELFT>
446 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
447 return toELFShdrIter(Sec)->sh_addralign;
450 template <class ELFT>
451 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
452 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
455 template <class ELFT>
456 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
457 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
458 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
459 EShdr->sh_type == ELF::SHT_PROGBITS;
462 template <class ELFT>
463 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
464 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
465 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
466 EShdr->sh_type == ELF::SHT_NOBITS;
469 template <class ELFT>
470 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
471 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
474 template <class ELFT>
475 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
476 DataRefImpl Symb) const {
477 Elf_Sym_Iter ESym = toELFSymIter(Symb);
479 uintX_t Index = ESym->st_shndx;
480 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
482 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
485 template <class ELFT>
487 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
489 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
490 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
492 return relocation_iterator(RelocationRef(RelData, this));
495 template <class ELFT>
497 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
499 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
500 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
501 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
502 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
505 RelData.d.b = S->sh_size / S->sh_entsize;
507 return relocation_iterator(RelocationRef(RelData, this));
510 template <class ELFT>
512 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
513 if (EF.getHeader()->e_type != ELF::ET_REL)
514 return section_end();
516 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
517 uintX_t Type = EShdr->sh_type;
518 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
519 return section_end();
521 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
522 return section_iterator(SectionRef(toDRI(R), this));
526 template <class ELFT>
527 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
531 template <class ELFT>
533 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
535 const Elf_Shdr *sec = getRelSection(Rel);
536 switch (sec->sh_type) {
538 report_fatal_error("Invalid section type in Rel!");
540 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
543 case ELF::SHT_RELA: {
544 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
551 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
553 DataRefImpl SymbolData;
554 switch (SymSec->sh_type) {
556 report_fatal_error("Invalid symbol table section type!");
557 case ELF::SHT_SYMTAB:
558 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
560 case ELF::SHT_DYNSYM:
561 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
565 return symbol_iterator(SymbolRef(SymbolData, this));
568 template <class ELFT>
570 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
571 uint64_t &Result) const {
572 uint64_t ROffset = getROffset(Rel);
573 const Elf_Ehdr *Header = EF.getHeader();
575 if (Header->e_type == ELF::ET_REL) {
576 const Elf_Shdr *RelocationSec = getRelSection(Rel);
577 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
578 Result = ROffset + RelocatedSec->sh_addr;
583 return object_error::success;
586 template <class ELFT>
588 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
589 uint64_t &Result) const {
590 assert(EF.getHeader()->e_type == ELF::ET_REL &&
591 "Only relocatable object files have relocation offsets");
592 Result = getROffset(Rel);
593 return object_error::success;
596 template <class ELFT>
597 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
598 const Elf_Shdr *sec = getRelSection(Rel);
599 switch (sec->sh_type) {
601 report_fatal_error("Invalid section type in Rel!");
603 return getRel(Rel)->r_offset;
605 return getRela(Rel)->r_offset;
609 template <class ELFT>
610 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
611 uint64_t &Result) const {
612 const Elf_Shdr *sec = getRelSection(Rel);
613 switch (sec->sh_type) {
615 report_fatal_error("Invalid section type in Rel!");
617 Result = getRel(Rel)->getType(EF.isMips64EL());
620 case ELF::SHT_RELA: {
621 Result = getRela(Rel)->getType(EF.isMips64EL());
625 return object_error::success;
628 template <class ELFT>
629 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
630 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
633 template <class ELFT>
634 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
635 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
636 const Elf_Shdr *sec = getRelSection(Rel);
638 switch (sec->sh_type) {
640 return object_error::parse_failed;
642 type = getRel(Rel)->getType(EF.isMips64EL());
645 case ELF::SHT_RELA: {
646 type = getRela(Rel)->getType(EF.isMips64EL());
651 EF.getRelocationTypeName(type, Result);
652 return object_error::success;
655 template <class ELFT>
657 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
658 int64_t &Result) const {
659 const Elf_Shdr *sec = getRelSection(Rel);
660 switch (sec->sh_type) {
662 report_fatal_error("Invalid section type in Rel!");
665 return object_error::success;
667 case ELF::SHT_RELA: {
668 Result = getRela(Rel)->r_addend;
669 return object_error::success;
674 template <class ELFT>
675 std::error_code ELFObjectFile<ELFT>::getRelocationValueString(
676 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
677 const Elf_Shdr *sec = getRelSection(Rel);
681 uint16_t symbol_index = 0;
682 switch (sec->sh_type) {
684 return object_error::parse_failed;
686 type = getRel(Rel)->getType(EF.isMips64EL());
687 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
688 // TODO: Read implicit addend from section data.
691 case ELF::SHT_RELA: {
692 type = getRela(Rel)->getType(EF.isMips64EL());
693 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
694 addend = getRela(Rel)->r_addend;
698 const Elf_Sym *symb =
699 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
700 ErrorOr<StringRef> SymName =
701 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
703 return SymName.getError();
704 switch (EF.getHeader()->e_machine) {
707 case ELF::R_X86_64_PC8:
708 case ELF::R_X86_64_PC16:
709 case ELF::R_X86_64_PC32: {
711 raw_string_ostream fmt(fmtbuf);
712 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
714 Result.append(fmtbuf.begin(), fmtbuf.end());
716 case ELF::R_X86_64_8:
717 case ELF::R_X86_64_16:
718 case ELF::R_X86_64_32:
719 case ELF::R_X86_64_32S:
720 case ELF::R_X86_64_64: {
722 raw_string_ostream fmt(fmtbuf);
723 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
725 Result.append(fmtbuf.begin(), fmtbuf.end());
731 case ELF::EM_AARCH64: {
733 raw_string_ostream fmt(fmtbuf);
736 fmt << (addend < 0 ? "" : "+") << addend;
738 Result.append(fmtbuf.begin(), fmtbuf.end());
743 case ELF::EM_HEXAGON:
751 Result.append(res.begin(), res.end());
752 return object_error::success;
755 template <class ELFT>
756 const typename ELFFile<ELFT>::Elf_Sym *
757 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
758 return &*toELFSymIter(Symb);
761 template <class ELFT>
762 const typename ELFObjectFile<ELFT>::Elf_Rel *
763 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
764 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
767 template <class ELFT>
768 const typename ELFObjectFile<ELFT>::Elf_Rela *
769 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
770 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
773 template <class ELFT>
774 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
776 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
780 EF(Data.getBuffer(), EC) {}
782 template <class ELFT>
783 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
784 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
787 template <class ELFT>
788 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
789 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
792 template <class ELFT>
793 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
794 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
797 template <class ELFT>
798 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
799 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
802 template <class ELFT>
803 section_iterator ELFObjectFile<ELFT>::section_begin() const {
804 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
807 template <class ELFT>
808 section_iterator ELFObjectFile<ELFT>::section_end() const {
809 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
812 template <class ELFT>
813 StringRef ELFObjectFile<ELFT>::getLoadName() const {
814 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
815 Elf_Dyn_Iter DE = EF.end_dynamic_table();
817 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
821 return EF.getDynamicString(DI->getVal());
825 template <class ELFT>
826 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
827 return ELFT::Is64Bits ? 8 : 4;
830 template <class ELFT>
831 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
832 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
833 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
834 case ELF::ELFCLASS32:
835 switch (EF.getHeader()->e_machine) {
839 return "ELF32-x86-64";
841 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
842 case ELF::EM_HEXAGON:
843 return "ELF32-hexagon";
849 case ELF::EM_SPARC32PLUS:
850 return "ELF32-sparc";
852 return "ELF32-unknown";
854 case ELF::ELFCLASS64:
855 switch (EF.getHeader()->e_machine) {
859 return "ELF64-x86-64";
860 case ELF::EM_AARCH64:
861 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
863 return "ELF64-ppc64";
866 case ELF::EM_SPARCV9:
867 return "ELF64-sparc";
871 return "ELF64-unknown";
874 // FIXME: Proper error handling.
875 report_fatal_error("Invalid ELFCLASS!");
879 template <class ELFT>
880 unsigned ELFObjectFile<ELFT>::getArch() const {
881 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
882 switch (EF.getHeader()->e_machine) {
886 return Triple::x86_64;
887 case ELF::EM_AARCH64:
888 return Triple::aarch64;
891 case ELF::EM_HEXAGON:
892 return Triple::hexagon;
894 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
895 case ELF::ELFCLASS32:
896 return IsLittleEndian ? Triple::mipsel : Triple::mips;
897 case ELF::ELFCLASS64:
898 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
900 report_fatal_error("Invalid ELFCLASS!");
905 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
907 return Triple::systemz;
910 case ELF::EM_SPARC32PLUS:
911 return Triple::sparc;
912 case ELF::EM_SPARCV9:
913 return Triple::sparcv9;
916 return Triple::UnknownArch;
920 template <class ELFT>
921 std::pair<symbol_iterator, symbol_iterator>
922 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
923 return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
926 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
927 return EF.getHeader()->e_type == ELF::ET_REL;
930 inline std::error_code getELFRelocationAddend(const RelocationRef R,
932 const ObjectFile *Obj = R.getObjectFile();
933 DataRefImpl DRI = R.getRawDataRefImpl();
934 return cast<ELFObjectFileBase>(Obj)->getRelocationAddend(DRI, Addend);
937 inline std::pair<symbol_iterator, symbol_iterator>
938 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
939 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
942 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
943 const SymbolRef &Sym,
946 return cast<ELFObjectFileBase>(Obj)
947 ->getSymbolVersion(Sym, Version, IsDefault);