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 static inline bool classof(const Binary *v) { return v->isELF(); }
54 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
56 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
58 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
60 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
61 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
62 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
63 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
64 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
65 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
67 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
68 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
69 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
74 void moveSymbolNext(DataRefImpl &Symb) const override;
75 std::error_code getSymbolName(DataRefImpl Symb,
76 StringRef &Res) const override;
77 std::error_code getSymbolAddress(DataRefImpl Symb,
78 uint64_t &Res) const override;
79 std::error_code getSymbolAlignment(DataRefImpl Symb,
80 uint32_t &Res) const override;
81 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
82 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
83 std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
84 std::error_code getSymbolType(DataRefImpl Symb,
85 SymbolRef::Type &Res) const override;
86 std::error_code getSymbolSection(DataRefImpl Symb,
87 section_iterator &Res) const override;
89 void moveSectionNext(DataRefImpl &Sec) const override;
90 std::error_code getSectionName(DataRefImpl Sec,
91 StringRef &Res) const override;
92 uint64_t getSectionAddress(DataRefImpl Sec) const override;
93 uint64_t getSectionSize(DataRefImpl Sec) const override;
94 std::error_code getSectionContents(DataRefImpl Sec,
95 StringRef &Res) const override;
96 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
97 bool isSectionText(DataRefImpl Sec) const override;
98 bool isSectionData(DataRefImpl Sec) const override;
99 bool isSectionBSS(DataRefImpl Sec) const override;
100 bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
101 bool isSectionVirtual(DataRefImpl Sec) const override;
102 bool isSectionZeroInit(DataRefImpl Sec) const override;
103 bool isSectionReadOnlyData(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 uint8_t getBytesInAddress() const override;
205 StringRef getFileFormatName() const override;
206 unsigned getArch() const override;
207 StringRef getLoadName() const;
209 std::error_code getPlatformFlags(unsigned &Result) const override {
210 Result = EF.getHeader()->e_flags;
211 return object_error::success;
214 const ELFFile<ELFT> *getELFFile() const { return &EF; }
216 bool isDyldType() const { return isDyldELFObject; }
217 static inline bool classof(const Binary *v) {
218 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
222 std::pair<symbol_iterator, symbol_iterator>
223 getELFDynamicSymbolIterators() const override;
225 bool isRelocatableObject() const override;
228 // Use an alignment of 2 for the typedefs since that is the worst case for
229 // ELF files in archives.
230 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
231 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
232 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
233 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
235 template <class ELFT>
236 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
237 Symb = toDRI(++toELFSymIter(Symb));
240 template <class ELFT>
241 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
242 StringRef &Result) const {
243 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
245 return Name.getError();
247 return object_error::success;
250 template <class ELFT>
251 std::error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
253 bool &IsDefault) const {
254 DataRefImpl Symb = SymRef.getRawDataRefImpl();
255 const Elf_Sym *symb = getSymbol(Symb);
256 ErrorOr<StringRef> Ver =
257 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
259 return Ver.getError();
261 return object_error::success;
264 template <class ELFT>
265 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
266 uint64_t &Result) const {
267 const Elf_Sym *ESym = getSymbol(Symb);
268 switch (EF.getSymbolTableIndex(ESym)) {
269 case ELF::SHN_COMMON:
271 Result = UnknownAddressOrSize;
272 return object_error::success;
274 Result = ESym->st_value;
275 return object_error::success;
280 const Elf_Ehdr *Header = EF.getHeader();
281 Result = ESym->st_value;
283 // Clear the ARM/Thumb indicator flag.
284 if (Header->e_machine == ELF::EM_ARM && ESym->getType() == ELF::STT_FUNC)
287 if (Header->e_type == ELF::ET_REL)
288 Result += EF.getSection(ESym)->sh_addr;
290 return object_error::success;
293 template <class ELFT>
294 std::error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
295 uint32_t &Res) const {
296 Elf_Sym_Iter Sym = toELFSymIter(Symb);
297 if (Sym->st_shndx == ELF::SHN_COMMON)
301 return object_error::success;
304 template <class ELFT>
305 std::error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
306 uint64_t &Result) const {
307 Result = toELFSymIter(Symb)->st_size;
308 return object_error::success;
311 template <class ELFT>
312 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
313 uint8_t &Result) const {
314 Result = toELFSymIter(Symb)->st_other;
315 return object_error::success;
318 template <class ELFT>
320 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
321 SymbolRef::Type &Result) const {
322 const Elf_Sym *ESym = getSymbol(Symb);
324 switch (ESym->getType()) {
325 case ELF::STT_NOTYPE:
326 Result = SymbolRef::ST_Unknown;
328 case ELF::STT_SECTION:
329 Result = SymbolRef::ST_Debug;
332 Result = SymbolRef::ST_File;
335 Result = SymbolRef::ST_Function;
337 case ELF::STT_OBJECT:
338 case ELF::STT_COMMON:
340 Result = SymbolRef::ST_Data;
343 Result = SymbolRef::ST_Other;
346 return object_error::success;
349 template <class ELFT>
350 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
351 Elf_Sym_Iter EIter = toELFSymIter(Symb);
352 const Elf_Sym *ESym = &*EIter;
354 uint32_t Result = SymbolRef::SF_None;
356 if (ESym->getBinding() != ELF::STB_LOCAL)
357 Result |= SymbolRef::SF_Global;
359 if (ESym->getBinding() == ELF::STB_WEAK)
360 Result |= SymbolRef::SF_Weak;
362 if (ESym->st_shndx == ELF::SHN_ABS)
363 Result |= SymbolRef::SF_Absolute;
365 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
366 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
367 Result |= SymbolRef::SF_FormatSpecific;
369 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
370 Result |= SymbolRef::SF_Undefined;
372 if (ESym->getType() == ELF::STT_COMMON ||
373 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
374 Result |= SymbolRef::SF_Common;
379 template <class ELFT>
381 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
382 section_iterator &Res) const {
383 const Elf_Sym *ESym = getSymbol(Symb);
384 const Elf_Shdr *ESec = EF.getSection(ESym);
389 Sec.p = reinterpret_cast<intptr_t>(ESec);
390 Res = section_iterator(SectionRef(Sec, this));
392 return object_error::success;
395 template <class ELFT>
396 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
397 Sec = toDRI(++toELFShdrIter(Sec));
400 template <class ELFT>
401 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
402 StringRef &Result) const {
403 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
405 return Name.getError();
407 return object_error::success;
410 template <class ELFT>
411 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
412 return toELFShdrIter(Sec)->sh_addr;
415 template <class ELFT>
416 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
417 return toELFShdrIter(Sec)->sh_size;
420 template <class ELFT>
422 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
423 StringRef &Result) const {
424 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
425 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
426 return object_error::success;
429 template <class ELFT>
430 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
431 return toELFShdrIter(Sec)->sh_addralign;
434 template <class ELFT>
435 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
436 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
439 template <class ELFT>
440 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
441 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
442 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
443 EShdr->sh_type == ELF::SHT_PROGBITS;
446 template <class ELFT>
447 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
448 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
449 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
450 EShdr->sh_type == ELF::SHT_NOBITS;
453 template <class ELFT>
454 bool ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec) const {
455 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
458 template <class ELFT>
459 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
460 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
463 template <class ELFT>
464 bool ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec) const {
465 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
468 template <class ELFT>
469 bool ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec) const {
470 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
471 return !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
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);