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 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
181 unsigned char Binding = ESym->getBinding();
182 unsigned char Visibility = ESym->getVisibility();
184 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
185 // visibility is either DEFAULT or PROTECTED. All other symbols are not
187 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
188 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
194 // This flag is used for classof, to distinguish ELFObjectFile from
195 // its subclass. If more subclasses will be created, this flag will
196 // have to become an enum.
197 bool isDyldELFObject;
200 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
202 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
204 basic_symbol_iterator symbol_begin_impl() const override;
205 basic_symbol_iterator symbol_end_impl() const override;
207 symbol_iterator dynamic_symbol_begin() const;
208 symbol_iterator dynamic_symbol_end() const;
210 section_iterator section_begin() const override;
211 section_iterator section_end() const override;
213 std::error_code getRelocationAddend(DataRefImpl Rel,
214 int64_t &Res) const override;
215 std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
216 bool &IsDefault) const override;
218 uint64_t getSectionFlags(SectionRef Sec) const override;
219 uint32_t getSectionType(SectionRef Sec) const override;
221 uint8_t getBytesInAddress() const override;
222 StringRef getFileFormatName() const override;
223 unsigned getArch() const override;
224 StringRef getLoadName() const;
226 std::error_code getPlatformFlags(unsigned &Result) const override {
227 Result = EF.getHeader()->e_flags;
228 return object_error::success;
231 const ELFFile<ELFT> *getELFFile() const { return &EF; }
233 bool isDyldType() const { return isDyldELFObject; }
234 static inline bool classof(const Binary *v) {
235 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
239 std::pair<symbol_iterator, symbol_iterator>
240 getELFDynamicSymbolIterators() const override;
242 bool isRelocatableObject() const override;
245 // Use an alignment of 2 for the typedefs since that is the worst case for
246 // ELF files in archives.
247 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
248 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
249 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
250 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
252 template <class ELFT>
253 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
254 Symb = toDRI(++toELFSymIter(Symb));
257 template <class ELFT>
258 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
259 StringRef &Result) const {
260 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
262 return Name.getError();
264 return object_error::success;
267 template <class ELFT>
268 std::error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
270 bool &IsDefault) const {
271 DataRefImpl Symb = SymRef.getRawDataRefImpl();
272 const Elf_Sym *symb = getSymbol(Symb);
273 ErrorOr<StringRef> Ver =
274 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
276 return Ver.getError();
278 return object_error::success;
281 template <class ELFT>
282 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
283 DataRefImpl DRI = Sec.getRawDataRefImpl();
284 return toELFShdrIter(DRI)->sh_flags;
287 template <class ELFT>
288 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
289 DataRefImpl DRI = Sec.getRawDataRefImpl();
290 return toELFShdrIter(DRI)->sh_type;
293 template <class ELFT>
294 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
295 uint64_t &Result) const {
296 const Elf_Sym *ESym = getSymbol(Symb);
297 switch (EF.getSymbolTableIndex(ESym)) {
298 case ELF::SHN_COMMON:
300 Result = UnknownAddressOrSize;
301 return object_error::success;
303 Result = ESym->st_value;
304 return object_error::success;
309 const Elf_Ehdr *Header = EF.getHeader();
310 Result = ESym->st_value;
312 // Clear the ARM/Thumb or microMIPS indicator flag.
313 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
314 ESym->getType() == ELF::STT_FUNC)
317 if (Header->e_type == ELF::ET_REL)
318 Result += EF.getSection(ESym)->sh_addr;
320 return object_error::success;
323 template <class ELFT>
324 std::error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
325 uint32_t &Res) const {
326 Elf_Sym_Iter Sym = toELFSymIter(Symb);
327 if (Sym->st_shndx == ELF::SHN_COMMON)
331 return object_error::success;
334 template <class ELFT>
335 std::error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
336 uint64_t &Result) const {
337 Result = toELFSymIter(Symb)->st_size;
338 return object_error::success;
341 template <class ELFT>
342 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
343 uint8_t &Result) const {
344 Result = toELFSymIter(Symb)->st_other;
345 return object_error::success;
348 template <class ELFT>
350 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
351 SymbolRef::Type &Result) const {
352 const Elf_Sym *ESym = getSymbol(Symb);
354 switch (ESym->getType()) {
355 case ELF::STT_NOTYPE:
356 Result = SymbolRef::ST_Unknown;
358 case ELF::STT_SECTION:
359 Result = SymbolRef::ST_Debug;
362 Result = SymbolRef::ST_File;
365 Result = SymbolRef::ST_Function;
367 case ELF::STT_OBJECT:
368 case ELF::STT_COMMON:
370 Result = SymbolRef::ST_Data;
373 Result = SymbolRef::ST_Other;
376 return object_error::success;
379 template <class ELFT>
380 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
381 Elf_Sym_Iter EIter = toELFSymIter(Symb);
382 const Elf_Sym *ESym = &*EIter;
384 uint32_t Result = SymbolRef::SF_None;
386 if (ESym->getBinding() != ELF::STB_LOCAL)
387 Result |= SymbolRef::SF_Global;
389 if (ESym->getBinding() == ELF::STB_WEAK)
390 Result |= SymbolRef::SF_Weak;
392 if (ESym->st_shndx == ELF::SHN_ABS)
393 Result |= SymbolRef::SF_Absolute;
395 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
396 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
397 Result |= SymbolRef::SF_FormatSpecific;
399 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
400 Result |= SymbolRef::SF_Undefined;
402 if (ESym->getType() == ELF::STT_COMMON ||
403 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
404 Result |= SymbolRef::SF_Common;
406 if (isExportedToOtherDSO(ESym))
407 Result |= SymbolRef::SF_Exported;
412 template <class ELFT>
414 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
415 section_iterator &Res) const {
416 const Elf_Sym *ESym = getSymbol(Symb);
417 const Elf_Shdr *ESec = EF.getSection(ESym);
422 Sec.p = reinterpret_cast<intptr_t>(ESec);
423 Res = section_iterator(SectionRef(Sec, this));
425 return object_error::success;
428 template <class ELFT>
429 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
430 Sec = toDRI(++toELFShdrIter(Sec));
433 template <class ELFT>
434 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
435 StringRef &Result) const {
436 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
438 return Name.getError();
440 return object_error::success;
443 template <class ELFT>
444 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
445 return toELFShdrIter(Sec)->sh_addr;
448 template <class ELFT>
449 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
450 return toELFShdrIter(Sec)->sh_size;
453 template <class ELFT>
455 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
456 StringRef &Result) const {
457 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
458 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
459 return object_error::success;
462 template <class ELFT>
463 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
464 return toELFShdrIter(Sec)->sh_addralign;
467 template <class ELFT>
468 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
469 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
472 template <class ELFT>
473 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
474 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
475 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
476 EShdr->sh_type == ELF::SHT_PROGBITS;
479 template <class ELFT>
480 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
481 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
482 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
483 EShdr->sh_type == ELF::SHT_NOBITS;
486 template <class ELFT>
487 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
488 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
491 template <class ELFT>
492 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
493 DataRefImpl Symb) const {
494 Elf_Sym_Iter ESym = toELFSymIter(Symb);
496 uintX_t Index = ESym->st_shndx;
497 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
499 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
502 template <class ELFT>
504 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
506 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
507 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
509 return relocation_iterator(RelocationRef(RelData, this));
512 template <class ELFT>
514 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
516 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
517 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
518 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
519 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
522 RelData.d.b = S->sh_size / S->sh_entsize;
524 return relocation_iterator(RelocationRef(RelData, this));
527 template <class ELFT>
529 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
530 if (EF.getHeader()->e_type != ELF::ET_REL)
531 return section_end();
533 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
534 uintX_t Type = EShdr->sh_type;
535 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
536 return section_end();
538 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
539 return section_iterator(SectionRef(toDRI(R), this));
543 template <class ELFT>
544 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
548 template <class ELFT>
550 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
552 const Elf_Shdr *sec = getRelSection(Rel);
553 switch (sec->sh_type) {
555 report_fatal_error("Invalid section type in Rel!");
557 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
560 case ELF::SHT_RELA: {
561 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
568 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
570 DataRefImpl SymbolData;
571 switch (SymSec->sh_type) {
573 report_fatal_error("Invalid symbol table section type!");
574 case ELF::SHT_SYMTAB:
575 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
577 case ELF::SHT_DYNSYM:
578 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
582 return symbol_iterator(SymbolRef(SymbolData, this));
585 template <class ELFT>
587 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
588 uint64_t &Result) const {
589 uint64_t ROffset = getROffset(Rel);
590 const Elf_Ehdr *Header = EF.getHeader();
592 if (Header->e_type == ELF::ET_REL) {
593 const Elf_Shdr *RelocationSec = getRelSection(Rel);
594 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
595 Result = ROffset + RelocatedSec->sh_addr;
600 return object_error::success;
603 template <class ELFT>
605 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
606 uint64_t &Result) const {
607 assert(EF.getHeader()->e_type == ELF::ET_REL &&
608 "Only relocatable object files have relocation offsets");
609 Result = getROffset(Rel);
610 return object_error::success;
613 template <class ELFT>
614 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
615 const Elf_Shdr *sec = getRelSection(Rel);
616 switch (sec->sh_type) {
618 report_fatal_error("Invalid section type in Rel!");
620 return getRel(Rel)->r_offset;
622 return getRela(Rel)->r_offset;
626 template <class ELFT>
627 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
628 uint64_t &Result) const {
629 const Elf_Shdr *sec = getRelSection(Rel);
630 switch (sec->sh_type) {
632 report_fatal_error("Invalid section type in Rel!");
634 Result = getRel(Rel)->getType(EF.isMips64EL());
637 case ELF::SHT_RELA: {
638 Result = getRela(Rel)->getType(EF.isMips64EL());
642 return object_error::success;
645 template <class ELFT>
646 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
647 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
650 template <class ELFT>
651 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
652 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
653 const Elf_Shdr *sec = getRelSection(Rel);
655 switch (sec->sh_type) {
657 return object_error::parse_failed;
659 type = getRel(Rel)->getType(EF.isMips64EL());
662 case ELF::SHT_RELA: {
663 type = getRela(Rel)->getType(EF.isMips64EL());
668 EF.getRelocationTypeName(type, Result);
669 return object_error::success;
672 template <class ELFT>
674 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
675 int64_t &Result) const {
676 const Elf_Shdr *sec = getRelSection(Rel);
677 switch (sec->sh_type) {
679 report_fatal_error("Invalid section type in Rel!");
682 return object_error::success;
684 case ELF::SHT_RELA: {
685 Result = getRela(Rel)->r_addend;
686 return object_error::success;
691 template <class ELFT>
692 std::error_code ELFObjectFile<ELFT>::getRelocationValueString(
693 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
694 const Elf_Shdr *sec = getRelSection(Rel);
698 uint16_t symbol_index = 0;
699 switch (sec->sh_type) {
701 return object_error::parse_failed;
703 type = getRel(Rel)->getType(EF.isMips64EL());
704 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
705 // TODO: Read implicit addend from section data.
708 case ELF::SHT_RELA: {
709 type = getRela(Rel)->getType(EF.isMips64EL());
710 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
711 addend = getRela(Rel)->r_addend;
715 const Elf_Sym *symb =
716 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
717 ErrorOr<StringRef> SymName =
718 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
720 return SymName.getError();
721 switch (EF.getHeader()->e_machine) {
724 case ELF::R_X86_64_PC8:
725 case ELF::R_X86_64_PC16:
726 case ELF::R_X86_64_PC32: {
728 raw_string_ostream fmt(fmtbuf);
729 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
731 Result.append(fmtbuf.begin(), fmtbuf.end());
733 case ELF::R_X86_64_8:
734 case ELF::R_X86_64_16:
735 case ELF::R_X86_64_32:
736 case ELF::R_X86_64_32S:
737 case ELF::R_X86_64_64: {
739 raw_string_ostream fmt(fmtbuf);
740 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
742 Result.append(fmtbuf.begin(), fmtbuf.end());
748 case ELF::EM_AARCH64: {
750 raw_string_ostream fmt(fmtbuf);
753 fmt << (addend < 0 ? "" : "+") << addend;
755 Result.append(fmtbuf.begin(), fmtbuf.end());
760 case ELF::EM_HEXAGON:
768 Result.append(res.begin(), res.end());
769 return object_error::success;
772 template <class ELFT>
773 const typename ELFFile<ELFT>::Elf_Sym *
774 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
775 return &*toELFSymIter(Symb);
778 template <class ELFT>
779 const typename ELFObjectFile<ELFT>::Elf_Rel *
780 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
781 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
784 template <class ELFT>
785 const typename ELFObjectFile<ELFT>::Elf_Rela *
786 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
787 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
790 template <class ELFT>
791 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
793 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
797 EF(Data.getBuffer(), EC) {}
799 template <class ELFT>
800 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
801 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
804 template <class ELFT>
805 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
806 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
809 template <class ELFT>
810 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
811 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
814 template <class ELFT>
815 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
816 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
819 template <class ELFT>
820 section_iterator ELFObjectFile<ELFT>::section_begin() const {
821 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
824 template <class ELFT>
825 section_iterator ELFObjectFile<ELFT>::section_end() const {
826 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
829 template <class ELFT>
830 StringRef ELFObjectFile<ELFT>::getLoadName() const {
831 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
832 Elf_Dyn_Iter DE = EF.end_dynamic_table();
834 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
838 return EF.getDynamicString(DI->getVal());
842 template <class ELFT>
843 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
844 return ELFT::Is64Bits ? 8 : 4;
847 template <class ELFT>
848 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
849 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
850 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
851 case ELF::ELFCLASS32:
852 switch (EF.getHeader()->e_machine) {
856 return "ELF32-x86-64";
858 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
859 case ELF::EM_HEXAGON:
860 return "ELF32-hexagon";
866 case ELF::EM_SPARC32PLUS:
867 return "ELF32-sparc";
869 return "ELF32-unknown";
871 case ELF::ELFCLASS64:
872 switch (EF.getHeader()->e_machine) {
876 return "ELF64-x86-64";
877 case ELF::EM_AARCH64:
878 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
880 return "ELF64-ppc64";
883 case ELF::EM_SPARCV9:
884 return "ELF64-sparc";
888 return "ELF64-unknown";
891 // FIXME: Proper error handling.
892 report_fatal_error("Invalid ELFCLASS!");
896 template <class ELFT>
897 unsigned ELFObjectFile<ELFT>::getArch() const {
898 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
899 switch (EF.getHeader()->e_machine) {
903 return Triple::x86_64;
904 case ELF::EM_AARCH64:
905 return Triple::aarch64;
908 case ELF::EM_HEXAGON:
909 return Triple::hexagon;
911 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
912 case ELF::ELFCLASS32:
913 return IsLittleEndian ? Triple::mipsel : Triple::mips;
914 case ELF::ELFCLASS64:
915 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
917 report_fatal_error("Invalid ELFCLASS!");
922 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
924 return Triple::systemz;
927 case ELF::EM_SPARC32PLUS:
928 return Triple::sparc;
929 case ELF::EM_SPARCV9:
930 return Triple::sparcv9;
933 return Triple::UnknownArch;
937 template <class ELFT>
938 std::pair<symbol_iterator, symbol_iterator>
939 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
940 return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
943 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
944 return EF.getHeader()->e_type == ELF::ET_REL;
947 inline std::error_code getELFRelocationAddend(const RelocationRef R,
949 const ObjectFile *Obj = R.getObjectFile();
950 DataRefImpl DRI = R.getRawDataRefImpl();
951 return cast<ELFObjectFileBase>(Obj)->getRelocationAddend(DRI, Addend);
954 inline std::pair<symbol_iterator, symbol_iterator>
955 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
956 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
959 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
960 const SymbolRef &Sym,
963 return cast<ELFObjectFileBase>(Obj)
964 ->getSymbolVersion(Sym, Version, IsDefault);