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;
52 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
54 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
56 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
58 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
59 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
60 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
61 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
62 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
63 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
65 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
66 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
67 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
72 void moveSymbolNext(DataRefImpl &Symb) const override;
73 std::error_code getSymbolName(DataRefImpl Symb,
74 StringRef &Res) const override;
75 std::error_code getSymbolAddress(DataRefImpl Symb,
76 uint64_t &Res) const override;
77 std::error_code getSymbolAlignment(DataRefImpl Symb,
78 uint32_t &Res) const override;
79 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
80 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
81 std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
82 std::error_code getSymbolType(DataRefImpl Symb,
83 SymbolRef::Type &Res) const override;
84 std::error_code getSymbolSection(DataRefImpl Symb,
85 section_iterator &Res) const override;
87 void moveSectionNext(DataRefImpl &Sec) const override;
88 std::error_code getSectionName(DataRefImpl Sec,
89 StringRef &Res) const override;
90 std::error_code getSectionAddress(DataRefImpl Sec,
91 uint64_t &Res) const override;
92 std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
93 std::error_code getSectionContents(DataRefImpl Sec,
94 StringRef &Res) const override;
95 std::error_code getSectionAlignment(DataRefImpl Sec,
96 uint64_t &Res) const override;
97 std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
98 std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
99 std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
100 std::error_code isSectionRequiredForExecution(DataRefImpl Sec,
101 bool &Res) const override;
102 std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
103 std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
104 std::error_code isSectionReadOnlyData(DataRefImpl Sec,
105 bool &Res) const override;
106 std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
107 bool &Result) const override;
108 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
109 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
110 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
112 void moveRelocationNext(DataRefImpl &Rel) const override;
113 std::error_code getRelocationAddress(DataRefImpl Rel,
114 uint64_t &Res) const override;
115 std::error_code getRelocationOffset(DataRefImpl Rel,
116 uint64_t &Res) const override;
117 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
118 std::error_code getRelocationType(DataRefImpl Rel,
119 uint64_t &Res) const override;
121 getRelocationTypeName(DataRefImpl Rel,
122 SmallVectorImpl<char> &Result) const override;
124 getRelocationValueString(DataRefImpl Rel,
125 SmallVectorImpl<char> &Result) const override;
127 uint64_t getROffset(DataRefImpl Rel) const;
128 StringRef getRelocationTypeName(uint32_t Type) const;
130 /// \brief Get the relocation section that contains \a Rel.
131 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
132 return EF.getSection(Rel.d.a);
135 const Elf_Rel *getRel(DataRefImpl Rel) const;
136 const Elf_Rela *getRela(DataRefImpl Rela) const;
138 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
139 bool IsDynamic = Symb.p & 1;
142 EF.begin_dynamic_symbols().getEntSize(),
143 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
144 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
145 reinterpret_cast<const char *>(Symb.p), IsDynamic);
148 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
150 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
151 static_cast<uintptr_t>(Symb.isDynamic());
155 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
156 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
157 reinterpret_cast<const char *>(Sec.p));
160 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
162 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
166 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
168 DRI.p = reinterpret_cast<uintptr_t>(Sec);
172 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
173 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
174 reinterpret_cast<const char *>(Dyn.p));
177 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
179 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
183 // This flag is used for classof, to distinguish ELFObjectFile from
184 // its subclass. If more subclasses will be created, this flag will
185 // have to become an enum.
186 bool isDyldELFObject;
189 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
191 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
193 basic_symbol_iterator symbol_begin_impl() const override;
194 basic_symbol_iterator symbol_end_impl() const override;
196 symbol_iterator dynamic_symbol_begin() const;
197 symbol_iterator dynamic_symbol_end() const;
199 section_iterator section_begin() const override;
200 section_iterator section_end() const override;
202 std::error_code getRelocationAddend(DataRefImpl Rel,
203 int64_t &Res) const override;
204 std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
205 bool &IsDefault) 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 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
269 uint64_t &Result) const {
270 const Elf_Sym *ESym = getSymbol(Symb);
271 switch (EF.getSymbolTableIndex(ESym)) {
272 case ELF::SHN_COMMON:
274 Result = UnknownAddressOrSize;
275 return object_error::success;
277 Result = ESym->st_value;
278 return object_error::success;
283 const Elf_Ehdr *Header = EF.getHeader();
284 Result = ESym->st_value;
286 // Clear the ARM/Thumb indicator flag.
287 if (Header->e_machine == ELF::EM_ARM && ESym->getType() == ELF::STT_FUNC)
290 if (Header->e_type == ELF::ET_REL)
291 Result += EF.getSection(ESym)->sh_addr;
293 return object_error::success;
296 template <class ELFT>
297 std::error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
298 uint32_t &Res) const {
299 Elf_Sym_Iter Sym = toELFSymIter(Symb);
300 if (Sym->st_shndx == ELF::SHN_COMMON)
304 return object_error::success;
307 template <class ELFT>
308 std::error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
309 uint64_t &Result) const {
310 Result = toELFSymIter(Symb)->st_size;
311 return object_error::success;
314 template <class ELFT>
315 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
316 uint8_t &Result) const {
317 Result = toELFSymIter(Symb)->st_other;
318 return object_error::success;
321 template <class ELFT>
323 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
324 SymbolRef::Type &Result) const {
325 const Elf_Sym *ESym = getSymbol(Symb);
327 switch (ESym->getType()) {
328 case ELF::STT_NOTYPE:
329 Result = SymbolRef::ST_Unknown;
331 case ELF::STT_SECTION:
332 Result = SymbolRef::ST_Debug;
335 Result = SymbolRef::ST_File;
338 Result = SymbolRef::ST_Function;
340 case ELF::STT_OBJECT:
341 case ELF::STT_COMMON:
343 Result = SymbolRef::ST_Data;
346 Result = SymbolRef::ST_Other;
349 return object_error::success;
352 template <class ELFT>
353 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
354 Elf_Sym_Iter EIter = toELFSymIter(Symb);
355 const Elf_Sym *ESym = &*EIter;
357 uint32_t Result = SymbolRef::SF_None;
359 if (ESym->getBinding() != ELF::STB_LOCAL)
360 Result |= SymbolRef::SF_Global;
362 if (ESym->getBinding() == ELF::STB_WEAK)
363 Result |= SymbolRef::SF_Weak;
365 if (ESym->st_shndx == ELF::SHN_ABS)
366 Result |= SymbolRef::SF_Absolute;
368 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
369 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
370 Result |= SymbolRef::SF_FormatSpecific;
372 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
373 Result |= SymbolRef::SF_Undefined;
375 if (ESym->getType() == ELF::STT_COMMON ||
376 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
377 Result |= SymbolRef::SF_Common;
382 template <class ELFT>
384 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
385 section_iterator &Res) const {
386 const Elf_Sym *ESym = getSymbol(Symb);
387 const Elf_Shdr *ESec = EF.getSection(ESym);
392 Sec.p = reinterpret_cast<intptr_t>(ESec);
393 Res = section_iterator(SectionRef(Sec, this));
395 return object_error::success;
398 template <class ELFT>
399 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
400 Sec = toDRI(++toELFShdrIter(Sec));
403 template <class ELFT>
404 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
405 StringRef &Result) const {
406 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
408 return Name.getError();
410 return object_error::success;
413 template <class ELFT>
414 std::error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
415 uint64_t &Result) const {
416 Result = toELFShdrIter(Sec)->sh_addr;
417 return object_error::success;
420 template <class ELFT>
421 std::error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
422 uint64_t &Result) const {
423 Result = toELFShdrIter(Sec)->sh_size;
424 return object_error::success;
427 template <class ELFT>
429 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
430 StringRef &Result) const {
431 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
432 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
433 return object_error::success;
436 template <class ELFT>
438 ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
439 uint64_t &Result) const {
440 Result = toELFShdrIter(Sec)->sh_addralign;
441 return object_error::success;
444 template <class ELFT>
445 std::error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
446 bool &Result) const {
447 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
448 return object_error::success;
451 template <class ELFT>
452 std::error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
453 bool &Result) const {
454 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
455 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
456 EShdr->sh_type == ELF::SHT_PROGBITS;
457 return object_error::success;
460 template <class ELFT>
461 std::error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
462 bool &Result) const {
463 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
464 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
465 EShdr->sh_type == ELF::SHT_NOBITS;
466 return object_error::success;
469 template <class ELFT>
471 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
472 bool &Result) const {
473 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
474 return object_error::success;
477 template <class ELFT>
478 std::error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
479 bool &Result) const {
480 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
481 return object_error::success;
484 template <class ELFT>
485 std::error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
486 bool &Result) const {
487 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
488 return object_error::success;
491 template <class ELFT>
492 std::error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
493 bool &Result) const {
494 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
495 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
496 return object_error::success;
499 template <class ELFT>
500 std::error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
502 bool &Result) const {
503 Elf_Sym_Iter ESym = toELFSymIter(Symb);
505 uintX_t Index = ESym->st_shndx;
506 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
508 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
509 return object_error::success;
512 template <class ELFT>
514 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
516 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
517 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
519 return relocation_iterator(RelocationRef(RelData, this));
522 template <class ELFT>
524 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
526 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
527 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
528 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
529 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
532 RelData.d.b = S->sh_size / S->sh_entsize;
534 return relocation_iterator(RelocationRef(RelData, this));
537 template <class ELFT>
539 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
540 if (EF.getHeader()->e_type != ELF::ET_REL)
541 return section_end();
543 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
544 uintX_t Type = EShdr->sh_type;
545 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
546 return section_end();
548 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
549 return section_iterator(SectionRef(toDRI(R), this));
553 template <class ELFT>
554 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
558 template <class ELFT>
560 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
562 const Elf_Shdr *sec = getRelSection(Rel);
563 switch (sec->sh_type) {
565 report_fatal_error("Invalid section type in Rel!");
567 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
570 case ELF::SHT_RELA: {
571 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
578 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
580 DataRefImpl SymbolData;
581 switch (SymSec->sh_type) {
583 report_fatal_error("Invalid symbol table section type!");
584 case ELF::SHT_SYMTAB:
585 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
587 case ELF::SHT_DYNSYM:
588 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
592 return symbol_iterator(SymbolRef(SymbolData, this));
595 template <class ELFT>
597 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
598 uint64_t &Result) const {
599 uint64_t ROffset = getROffset(Rel);
600 const Elf_Ehdr *Header = EF.getHeader();
602 if (Header->e_type == ELF::ET_REL) {
603 const Elf_Shdr *RelocationSec = getRelSection(Rel);
604 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
605 Result = ROffset + RelocatedSec->sh_addr;
610 return object_error::success;
613 template <class ELFT>
615 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
616 uint64_t &Result) const {
617 assert(EF.getHeader()->e_type == ELF::ET_REL &&
618 "Only relocatable object files have relocation offsets");
619 Result = getROffset(Rel);
620 return object_error::success;
623 template <class ELFT>
624 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
625 const Elf_Shdr *sec = getRelSection(Rel);
626 switch (sec->sh_type) {
628 report_fatal_error("Invalid section type in Rel!");
630 return getRel(Rel)->r_offset;
632 return getRela(Rel)->r_offset;
636 template <class ELFT>
637 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
638 uint64_t &Result) const {
639 const Elf_Shdr *sec = getRelSection(Rel);
640 switch (sec->sh_type) {
642 report_fatal_error("Invalid section type in Rel!");
644 Result = getRel(Rel)->getType(EF.isMips64EL());
647 case ELF::SHT_RELA: {
648 Result = getRela(Rel)->getType(EF.isMips64EL());
652 return object_error::success;
655 template <class ELFT>
656 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
657 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
660 template <class ELFT>
661 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
662 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
663 const Elf_Shdr *sec = getRelSection(Rel);
665 switch (sec->sh_type) {
667 return object_error::parse_failed;
669 type = getRel(Rel)->getType(EF.isMips64EL());
672 case ELF::SHT_RELA: {
673 type = getRela(Rel)->getType(EF.isMips64EL());
678 EF.getRelocationTypeName(type, Result);
679 return object_error::success;
682 template <class ELFT>
684 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
685 int64_t &Result) const {
686 const Elf_Shdr *sec = getRelSection(Rel);
687 switch (sec->sh_type) {
689 report_fatal_error("Invalid section type in Rel!");
692 return object_error::success;
694 case ELF::SHT_RELA: {
695 Result = getRela(Rel)->r_addend;
696 return object_error::success;
701 template <class ELFT>
702 std::error_code ELFObjectFile<ELFT>::getRelocationValueString(
703 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
704 const Elf_Shdr *sec = getRelSection(Rel);
708 uint16_t symbol_index = 0;
709 switch (sec->sh_type) {
711 return object_error::parse_failed;
713 type = getRel(Rel)->getType(EF.isMips64EL());
714 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
715 // TODO: Read implicit addend from section data.
718 case ELF::SHT_RELA: {
719 type = getRela(Rel)->getType(EF.isMips64EL());
720 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
721 addend = getRela(Rel)->r_addend;
725 const Elf_Sym *symb =
726 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
727 ErrorOr<StringRef> SymName =
728 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
730 return SymName.getError();
731 switch (EF.getHeader()->e_machine) {
734 case ELF::R_X86_64_PC8:
735 case ELF::R_X86_64_PC16:
736 case ELF::R_X86_64_PC32: {
738 raw_string_ostream fmt(fmtbuf);
739 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
741 Result.append(fmtbuf.begin(), fmtbuf.end());
743 case ELF::R_X86_64_8:
744 case ELF::R_X86_64_16:
745 case ELF::R_X86_64_32:
746 case ELF::R_X86_64_32S:
747 case ELF::R_X86_64_64: {
749 raw_string_ostream fmt(fmtbuf);
750 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
752 Result.append(fmtbuf.begin(), fmtbuf.end());
758 case ELF::EM_AARCH64: {
760 raw_string_ostream fmt(fmtbuf);
763 fmt << (addend < 0 ? "" : "+") << addend;
765 Result.append(fmtbuf.begin(), fmtbuf.end());
769 case ELF::EM_HEXAGON:
777 Result.append(res.begin(), res.end());
778 return object_error::success;
781 template <class ELFT>
782 const typename ELFFile<ELFT>::Elf_Sym *
783 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
784 return &*toELFSymIter(Symb);
787 template <class ELFT>
788 const typename ELFObjectFile<ELFT>::Elf_Rel *
789 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
790 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
793 template <class ELFT>
794 const typename ELFObjectFile<ELFT>::Elf_Rela *
795 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
796 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
799 template <class ELFT>
800 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
802 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
806 EF(Data.getBuffer(), EC) {}
808 template <class ELFT>
809 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
810 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
813 template <class ELFT>
814 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
815 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
818 template <class ELFT>
819 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
820 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
823 template <class ELFT>
824 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
825 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
828 template <class ELFT>
829 section_iterator ELFObjectFile<ELFT>::section_begin() const {
830 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
833 template <class ELFT>
834 section_iterator ELFObjectFile<ELFT>::section_end() const {
835 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
838 template <class ELFT>
839 StringRef ELFObjectFile<ELFT>::getLoadName() const {
840 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
841 Elf_Dyn_Iter DE = EF.end_dynamic_table();
843 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
847 return EF.getDynamicString(DI->getVal());
851 template <class ELFT>
852 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
853 return ELFT::Is64Bits ? 8 : 4;
856 template <class ELFT>
857 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
858 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
859 case ELF::ELFCLASS32:
860 switch (EF.getHeader()->e_machine) {
864 return "ELF32-x86-64";
867 case ELF::EM_HEXAGON:
868 return "ELF32-hexagon";
874 case ELF::EM_SPARC32PLUS:
875 return "ELF32-sparc";
877 return "ELF32-unknown";
879 case ELF::ELFCLASS64:
880 switch (EF.getHeader()->e_machine) {
884 return "ELF64-x86-64";
885 case ELF::EM_AARCH64:
886 return "ELF64-aarch64";
888 return "ELF64-ppc64";
891 case ELF::EM_SPARCV9:
892 return "ELF64-sparc";
896 return "ELF64-unknown";
899 // FIXME: Proper error handling.
900 report_fatal_error("Invalid ELFCLASS!");
904 template <class ELFT>
905 unsigned ELFObjectFile<ELFT>::getArch() const {
906 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
907 switch (EF.getHeader()->e_machine) {
911 return Triple::x86_64;
912 case ELF::EM_AARCH64:
913 return Triple::aarch64;
916 case ELF::EM_HEXAGON:
917 return Triple::hexagon;
919 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
920 case ELF::ELFCLASS32:
921 return IsLittleEndian ? Triple::mipsel : Triple::mips;
922 case ELF::ELFCLASS64:
923 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
925 report_fatal_error("Invalid ELFCLASS!");
928 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
930 return Triple::systemz;
933 case ELF::EM_SPARC32PLUS:
934 return Triple::sparc;
935 case ELF::EM_SPARCV9:
936 return Triple::sparcv9;
939 return Triple::UnknownArch;
943 template <class ELFT>
944 std::pair<symbol_iterator, symbol_iterator>
945 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
946 return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
949 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
950 return EF.getHeader()->e_type == ELF::ET_REL;
953 inline std::error_code getELFRelocationAddend(const RelocationRef R,
955 const ObjectFile *Obj = R.getObjectFile();
956 DataRefImpl DRI = R.getRawDataRefImpl();
957 return cast<ELFObjectFileBase>(Obj)->getRelocationAddend(DRI, Addend);
960 inline std::pair<symbol_iterator, symbol_iterator>
961 getELFDynamicSymbolIterators(SymbolicFile *Obj) {
962 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
965 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
966 const SymbolRef &Sym,
969 return cast<ELFObjectFileBase>(Obj)
970 ->getSymbolVersion(Sym, Version, IsDefault);