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 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
319 if (Section != nullptr)
320 Result += Section->sh_addr;
323 return object_error::success;
326 template <class ELFT>
327 std::error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
328 uint32_t &Res) const {
329 Elf_Sym_Iter Sym = toELFSymIter(Symb);
330 if (Sym->st_shndx == ELF::SHN_COMMON)
334 return object_error::success;
337 template <class ELFT>
338 std::error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
339 uint64_t &Result) const {
340 Result = toELFSymIter(Symb)->st_size;
341 return object_error::success;
344 template <class ELFT>
345 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
346 uint8_t &Result) const {
347 Result = toELFSymIter(Symb)->st_other;
348 return object_error::success;
351 template <class ELFT>
353 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
354 SymbolRef::Type &Result) const {
355 const Elf_Sym *ESym = getSymbol(Symb);
357 switch (ESym->getType()) {
358 case ELF::STT_NOTYPE:
359 Result = SymbolRef::ST_Unknown;
361 case ELF::STT_SECTION:
362 Result = SymbolRef::ST_Debug;
365 Result = SymbolRef::ST_File;
368 Result = SymbolRef::ST_Function;
370 case ELF::STT_OBJECT:
371 case ELF::STT_COMMON:
373 Result = SymbolRef::ST_Data;
376 Result = SymbolRef::ST_Other;
379 return object_error::success;
382 template <class ELFT>
383 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
384 Elf_Sym_Iter EIter = toELFSymIter(Symb);
385 const Elf_Sym *ESym = &*EIter;
387 uint32_t Result = SymbolRef::SF_None;
389 if (ESym->getBinding() != ELF::STB_LOCAL)
390 Result |= SymbolRef::SF_Global;
392 if (ESym->getBinding() == ELF::STB_WEAK)
393 Result |= SymbolRef::SF_Weak;
395 if (ESym->st_shndx == ELF::SHN_ABS)
396 Result |= SymbolRef::SF_Absolute;
398 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
399 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
400 Result |= SymbolRef::SF_FormatSpecific;
402 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
403 Result |= SymbolRef::SF_Undefined;
405 if (ESym->getType() == ELF::STT_COMMON ||
406 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
407 Result |= SymbolRef::SF_Common;
409 if (isExportedToOtherDSO(ESym))
410 Result |= SymbolRef::SF_Exported;
415 template <class ELFT>
417 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
418 section_iterator &Res) const {
419 const Elf_Sym *ESym = getSymbol(Symb);
420 const Elf_Shdr *ESec = EF.getSection(ESym);
425 Sec.p = reinterpret_cast<intptr_t>(ESec);
426 Res = section_iterator(SectionRef(Sec, this));
428 return object_error::success;
431 template <class ELFT>
432 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
433 Sec = toDRI(++toELFShdrIter(Sec));
436 template <class ELFT>
437 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
438 StringRef &Result) const {
439 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
441 return Name.getError();
443 return object_error::success;
446 template <class ELFT>
447 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
448 return toELFShdrIter(Sec)->sh_addr;
451 template <class ELFT>
452 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
453 return toELFShdrIter(Sec)->sh_size;
456 template <class ELFT>
458 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
459 StringRef &Result) const {
460 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
461 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
462 return object_error::success;
465 template <class ELFT>
466 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
467 return toELFShdrIter(Sec)->sh_addralign;
470 template <class ELFT>
471 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
472 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
475 template <class ELFT>
476 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
477 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
478 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
479 EShdr->sh_type == ELF::SHT_PROGBITS;
482 template <class ELFT>
483 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
484 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
485 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
486 EShdr->sh_type == ELF::SHT_NOBITS;
489 template <class ELFT>
490 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
491 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
494 template <class ELFT>
495 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
496 DataRefImpl Symb) const {
497 Elf_Sym_Iter ESym = toELFSymIter(Symb);
499 uintX_t Index = ESym->st_shndx;
500 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
502 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
505 template <class ELFT>
507 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
509 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
510 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
512 return relocation_iterator(RelocationRef(RelData, this));
515 template <class ELFT>
517 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
519 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
520 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
521 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
522 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
525 RelData.d.b = S->sh_size / S->sh_entsize;
527 return relocation_iterator(RelocationRef(RelData, this));
530 template <class ELFT>
532 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
533 if (EF.getHeader()->e_type != ELF::ET_REL)
534 return section_end();
536 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
537 uintX_t Type = EShdr->sh_type;
538 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
539 return section_end();
541 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
542 return section_iterator(SectionRef(toDRI(R), this));
546 template <class ELFT>
547 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
551 template <class ELFT>
553 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
555 const Elf_Shdr *sec = getRelSection(Rel);
556 switch (sec->sh_type) {
558 report_fatal_error("Invalid section type in Rel!");
560 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
563 case ELF::SHT_RELA: {
564 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
571 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
573 DataRefImpl SymbolData;
574 switch (SymSec->sh_type) {
576 report_fatal_error("Invalid symbol table section type!");
577 case ELF::SHT_SYMTAB:
578 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
580 case ELF::SHT_DYNSYM:
581 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
585 return symbol_iterator(SymbolRef(SymbolData, this));
588 template <class ELFT>
590 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
591 uint64_t &Result) const {
592 uint64_t ROffset = getROffset(Rel);
593 const Elf_Ehdr *Header = EF.getHeader();
595 if (Header->e_type == ELF::ET_REL) {
596 const Elf_Shdr *RelocationSec = getRelSection(Rel);
597 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
598 Result = ROffset + RelocatedSec->sh_addr;
603 return object_error::success;
606 template <class ELFT>
608 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
609 uint64_t &Result) const {
610 assert(EF.getHeader()->e_type == ELF::ET_REL &&
611 "Only relocatable object files have relocation offsets");
612 Result = getROffset(Rel);
613 return object_error::success;
616 template <class ELFT>
617 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
618 const Elf_Shdr *sec = getRelSection(Rel);
619 switch (sec->sh_type) {
621 report_fatal_error("Invalid section type in Rel!");
623 return getRel(Rel)->r_offset;
625 return getRela(Rel)->r_offset;
629 template <class ELFT>
630 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
631 uint64_t &Result) const {
632 const Elf_Shdr *sec = getRelSection(Rel);
633 switch (sec->sh_type) {
635 report_fatal_error("Invalid section type in Rel!");
637 Result = getRel(Rel)->getType(EF.isMips64EL());
640 case ELF::SHT_RELA: {
641 Result = getRela(Rel)->getType(EF.isMips64EL());
645 return object_error::success;
648 template <class ELFT>
649 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
650 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
653 template <class ELFT>
654 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
655 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
656 const Elf_Shdr *sec = getRelSection(Rel);
658 switch (sec->sh_type) {
660 return object_error::parse_failed;
662 type = getRel(Rel)->getType(EF.isMips64EL());
665 case ELF::SHT_RELA: {
666 type = getRela(Rel)->getType(EF.isMips64EL());
671 EF.getRelocationTypeName(type, Result);
672 return object_error::success;
675 template <class ELFT>
677 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
678 int64_t &Result) const {
679 const Elf_Shdr *sec = getRelSection(Rel);
680 switch (sec->sh_type) {
682 report_fatal_error("Invalid section type in Rel!");
685 return object_error::success;
687 case ELF::SHT_RELA: {
688 Result = getRela(Rel)->r_addend;
689 return object_error::success;
694 template <class ELFT>
695 std::error_code ELFObjectFile<ELFT>::getRelocationValueString(
696 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
697 const Elf_Shdr *sec = getRelSection(Rel);
701 uint16_t symbol_index = 0;
702 switch (sec->sh_type) {
704 return object_error::parse_failed;
706 type = getRel(Rel)->getType(EF.isMips64EL());
707 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
708 // TODO: Read implicit addend from section data.
711 case ELF::SHT_RELA: {
712 type = getRela(Rel)->getType(EF.isMips64EL());
713 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
714 addend = getRela(Rel)->r_addend;
718 const Elf_Sym *symb =
719 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
720 ErrorOr<StringRef> SymName =
721 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
723 return SymName.getError();
724 switch (EF.getHeader()->e_machine) {
727 case ELF::R_X86_64_PC8:
728 case ELF::R_X86_64_PC16:
729 case ELF::R_X86_64_PC32: {
731 raw_string_ostream fmt(fmtbuf);
732 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
734 Result.append(fmtbuf.begin(), fmtbuf.end());
736 case ELF::R_X86_64_8:
737 case ELF::R_X86_64_16:
738 case ELF::R_X86_64_32:
739 case ELF::R_X86_64_32S:
740 case ELF::R_X86_64_64: {
742 raw_string_ostream fmt(fmtbuf);
743 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
745 Result.append(fmtbuf.begin(), fmtbuf.end());
751 case ELF::EM_AARCH64: {
753 raw_string_ostream fmt(fmtbuf);
756 fmt << (addend < 0 ? "" : "+") << addend;
758 Result.append(fmtbuf.begin(), fmtbuf.end());
763 case ELF::EM_HEXAGON:
771 Result.append(res.begin(), res.end());
772 return object_error::success;
775 template <class ELFT>
776 const typename ELFFile<ELFT>::Elf_Sym *
777 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
778 return &*toELFSymIter(Symb);
781 template <class ELFT>
782 const typename ELFObjectFile<ELFT>::Elf_Rel *
783 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
784 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
787 template <class ELFT>
788 const typename ELFObjectFile<ELFT>::Elf_Rela *
789 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
790 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
793 template <class ELFT>
794 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
796 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
800 EF(Data.getBuffer(), EC) {}
802 template <class ELFT>
803 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
804 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
807 template <class ELFT>
808 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
809 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
812 template <class ELFT>
813 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
814 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
817 template <class ELFT>
818 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
819 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
822 template <class ELFT>
823 section_iterator ELFObjectFile<ELFT>::section_begin() const {
824 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
827 template <class ELFT>
828 section_iterator ELFObjectFile<ELFT>::section_end() const {
829 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
832 template <class ELFT>
833 StringRef ELFObjectFile<ELFT>::getLoadName() const {
834 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
835 Elf_Dyn_Iter DE = EF.end_dynamic_table();
837 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
841 return EF.getDynamicString(DI->getVal());
845 template <class ELFT>
846 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
847 return ELFT::Is64Bits ? 8 : 4;
850 template <class ELFT>
851 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
852 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
853 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
854 case ELF::ELFCLASS32:
855 switch (EF.getHeader()->e_machine) {
859 return "ELF32-x86-64";
861 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
862 case ELF::EM_HEXAGON:
863 return "ELF32-hexagon";
869 case ELF::EM_SPARC32PLUS:
870 return "ELF32-sparc";
872 return "ELF32-unknown";
874 case ELF::ELFCLASS64:
875 switch (EF.getHeader()->e_machine) {
879 return "ELF64-x86-64";
880 case ELF::EM_AARCH64:
881 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
883 return "ELF64-ppc64";
886 case ELF::EM_SPARCV9:
887 return "ELF64-sparc";
891 return "ELF64-unknown";
894 // FIXME: Proper error handling.
895 report_fatal_error("Invalid ELFCLASS!");
899 template <class ELFT>
900 unsigned ELFObjectFile<ELFT>::getArch() const {
901 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
902 switch (EF.getHeader()->e_machine) {
906 return Triple::x86_64;
907 case ELF::EM_AARCH64:
908 return Triple::aarch64;
911 case ELF::EM_HEXAGON:
912 return Triple::hexagon;
914 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
915 case ELF::ELFCLASS32:
916 return IsLittleEndian ? Triple::mipsel : Triple::mips;
917 case ELF::ELFCLASS64:
918 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
920 report_fatal_error("Invalid ELFCLASS!");
925 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
927 return Triple::systemz;
930 case ELF::EM_SPARC32PLUS:
931 return Triple::sparc;
932 case ELF::EM_SPARCV9:
933 return Triple::sparcv9;
936 return Triple::UnknownArch;
940 template <class ELFT>
941 std::pair<symbol_iterator, symbol_iterator>
942 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
943 return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
946 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
947 return EF.getHeader()->e_type == ELF::ET_REL;
950 inline std::error_code getELFRelocationAddend(const RelocationRef R,
952 const ObjectFile *Obj = R.getObjectFile();
953 DataRefImpl DRI = R.getRawDataRefImpl();
954 return cast<ELFObjectFileBase>(Obj)->getRelocationAddend(DRI, Addend);
957 inline std::pair<symbol_iterator, symbol_iterator>
958 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
959 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
962 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
963 const SymbolRef &Sym,
966 return cast<ELFObjectFileBase>(Obj)
967 ->getSymbolVersion(Sym, Version, IsDefault);