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_ELF_OBJECT_FILE_H
15 #define LLVM_OBJECT_ELF_OBJECT_FILE_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"
39 class ELFObjectFile : public ObjectFile {
41 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
43 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
45 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
46 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
47 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
48 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
49 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
51 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
52 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
53 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
58 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
59 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
60 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
61 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
62 virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
63 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
64 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
65 virtual error_code getSymbolType(DataRefImpl Symb,
66 SymbolRef::Type &Res) const;
67 virtual error_code getSymbolSection(DataRefImpl Symb,
68 section_iterator &Res) const;
69 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
71 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
72 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
74 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
75 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
76 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
77 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
78 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
79 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
80 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
81 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
82 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
83 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
85 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
86 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
87 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
88 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
90 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const;
91 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const;
92 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
94 virtual error_code getRelocationNext(DataRefImpl Rel,
95 RelocationRef &Res) const;
96 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
97 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
98 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
99 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
100 virtual error_code getRelocationTypeName(DataRefImpl Rel,
101 SmallVectorImpl<char> &Result) const;
103 getRelocationValueString(DataRefImpl Rel,
104 SmallVectorImpl<char> &Result) const;
106 uint64_t getROffset(DataRefImpl Rel) const;
107 StringRef getRelocationTypeName(uint32_t Type) const;
109 /// \brief Get the relocation section that contains \a Rel.
110 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
111 return EF.getSection(Rel.d.a);
114 const Elf_Rel *getRel(DataRefImpl Rel) const;
115 const Elf_Rela *getRela(DataRefImpl Rela) const;
117 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
118 bool IsDynamic = Symb.p & 1;
121 EF.begin_dynamic_symbols().getEntSize(),
122 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
123 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
124 reinterpret_cast<const char *>(Symb.p), IsDynamic);
127 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
129 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
130 static_cast<uintptr_t>(Symb.isDynamic());
134 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
135 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
136 reinterpret_cast<const char *>(Sec.p));
139 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
141 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
145 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
147 DRI.p = reinterpret_cast<uintptr_t>(Sec);
151 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
152 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
153 reinterpret_cast<const char *>(Dyn.p));
156 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
158 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
162 // This flag is used for classof, to distinguish ELFObjectFile from
163 // its subclass. If more subclasses will be created, this flag will
164 // have to become an enum.
165 bool isDyldELFObject;
168 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
170 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
172 virtual symbol_iterator begin_symbols() const;
173 virtual symbol_iterator end_symbols() const;
175 virtual symbol_iterator begin_dynamic_symbols() const;
176 virtual symbol_iterator end_dynamic_symbols() const;
178 virtual section_iterator begin_sections() const;
179 virtual section_iterator end_sections() const;
181 virtual library_iterator begin_libraries_needed() const;
182 virtual library_iterator end_libraries_needed() const;
184 error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
185 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
186 bool &IsDefault) const;
188 virtual uint8_t getBytesInAddress() const;
189 virtual StringRef getFileFormatName() const;
190 virtual StringRef getObjectType() const { return "ELF"; }
191 virtual unsigned getArch() const;
192 virtual StringRef getLoadName() const;
194 const ELFFile<ELFT> *getELFFile() const { return &EF; }
196 bool isDyldType() const { return isDyldELFObject; }
197 static inline bool classof(const Binary *v) {
198 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
203 // Use an alignment of 2 for the typedefs since that is the worst case for
204 // ELF files in archives.
205 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
206 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
207 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
208 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
210 template <class ELFT>
211 error_code ELFObjectFile<ELFT>::getSymbolNext(DataRefImpl Symb,
212 SymbolRef &Result) const {
213 Result = SymbolRef(toDRI(++toELFSymIter(Symb)), this);
214 return object_error::success;
217 template <class ELFT>
218 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
219 StringRef &Result) const {
220 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
222 return Name.getError();
224 return object_error::success;
227 template <class ELFT>
228 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
230 bool &IsDefault) const {
231 DataRefImpl Symb = SymRef.getRawDataRefImpl();
232 const Elf_Sym *symb = getSymbol(Symb);
233 ErrorOr<StringRef> Ver =
234 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
236 return Ver.getError();
238 return object_error::success;
241 template <class ELFT>
242 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
243 uint64_t &Result) const {
244 const Elf_Sym *ESym = getSymbol(Symb);
245 const Elf_Shdr *ESec;
246 switch (EF.getSymbolTableIndex(ESym)) {
247 case ELF::SHN_COMMON:
248 // Unintialized symbols have no offset in the object file
250 Result = UnknownAddressOrSize;
251 return object_error::success;
253 Result = ESym->st_value;
254 return object_error::success;
256 ESec = EF.getSection(ESym);
259 switch (ESym->getType()) {
260 case ELF::STT_SECTION:
261 Result = ESec ? ESec->sh_offset : UnknownAddressOrSize;
262 return object_error::success;
264 case ELF::STT_OBJECT:
265 case ELF::STT_NOTYPE:
266 Result = ESym->st_value + (ESec ? ESec->sh_offset : 0);
267 return object_error::success;
269 Result = UnknownAddressOrSize;
270 return object_error::success;
274 template <class ELFT>
275 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
276 uint64_t &Result) const {
277 const Elf_Sym *ESym = getSymbol(Symb);
278 const Elf_Shdr *ESec;
279 switch (EF.getSymbolTableIndex(ESym)) {
280 case ELF::SHN_COMMON:
282 Result = UnknownAddressOrSize;
283 return object_error::success;
285 Result = ESym->st_value;
286 return object_error::success;
288 ESec = EF.getSection(ESym);
291 switch (ESym->getType()) {
292 case ELF::STT_SECTION:
293 Result = ESec ? ESec->sh_addr : UnknownAddressOrSize;
294 return object_error::success;
296 case ELF::STT_OBJECT:
297 case ELF::STT_NOTYPE:
299 switch (EF.getHeader()->e_type) {
302 IsRelocatable = false;
305 IsRelocatable = true;
307 Result = ESym->st_value;
309 // Clear the ARM/Thumb indicator flag.
310 if (EF.getHeader()->e_machine == ELF::EM_ARM)
313 if (IsRelocatable && ESec != 0)
314 Result += ESec->sh_addr;
315 return object_error::success;
317 Result = UnknownAddressOrSize;
318 return object_error::success;
322 template <class ELFT>
323 error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
324 uint32_t &Res) const {
325 Elf_Sym_Iter Sym = toELFSymIter(Symb);
326 if (Sym->st_shndx == ELF::SHN_COMMON)
330 return object_error::success;
333 template <class ELFT>
334 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
335 uint64_t &Result) const {
336 Result = toELFSymIter(Symb)->st_size;
337 return object_error::success;
340 template <class ELFT>
341 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
342 SymbolRef::Type &Result) const {
343 const Elf_Sym *ESym = getSymbol(Symb);
345 switch (ESym->getType()) {
346 case ELF::STT_NOTYPE:
347 Result = SymbolRef::ST_Unknown;
349 case ELF::STT_SECTION:
350 Result = SymbolRef::ST_Debug;
353 Result = SymbolRef::ST_File;
356 Result = SymbolRef::ST_Function;
358 case ELF::STT_OBJECT:
359 case ELF::STT_COMMON:
361 Result = SymbolRef::ST_Data;
364 Result = SymbolRef::ST_Other;
367 return object_error::success;
370 template <class ELFT>
371 error_code ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb,
372 uint32_t &Result) const {
373 const Elf_Sym *ESym = getSymbol(Symb);
375 Result = SymbolRef::SF_None;
377 if (ESym->getBinding() != ELF::STB_LOCAL)
378 Result |= SymbolRef::SF_Global;
380 if (ESym->getBinding() == ELF::STB_WEAK)
381 Result |= SymbolRef::SF_Weak;
383 if (ESym->st_shndx == ELF::SHN_ABS)
384 Result |= SymbolRef::SF_Absolute;
386 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
387 ESym == &*EF.begin_symbols())
388 Result |= SymbolRef::SF_FormatSpecific;
390 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
391 Result |= SymbolRef::SF_Undefined;
393 if (ESym->getType() == ELF::STT_COMMON ||
394 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
395 Result |= SymbolRef::SF_Common;
397 if (ESym->getType() == ELF::STT_TLS)
398 Result |= SymbolRef::SF_ThreadLocal;
400 return object_error::success;
403 template <class ELFT>
404 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
405 section_iterator &Res) const {
406 const Elf_Sym *ESym = getSymbol(Symb);
407 const Elf_Shdr *ESec = EF.getSection(ESym);
409 Res = end_sections();
412 Sec.p = reinterpret_cast<intptr_t>(ESec);
413 Res = section_iterator(SectionRef(Sec, this));
415 return object_error::success;
418 template <class ELFT>
419 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
420 uint64_t &Val) const {
421 const Elf_Sym *ESym = getSymbol(Symb);
422 Val = ESym->st_value;
423 return object_error::success;
426 template <class ELFT>
427 error_code ELFObjectFile<ELFT>::getSectionNext(DataRefImpl Sec,
428 SectionRef &Result) const {
429 Result = SectionRef(toDRI(++toELFShdrIter(Sec)), this);
430 return object_error::success;
433 template <class ELFT>
434 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 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
445 uint64_t &Result) const {
446 Result = toELFShdrIter(Sec)->sh_addr;
447 return object_error::success;
450 template <class ELFT>
451 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
452 uint64_t &Result) const {
453 Result = toELFShdrIter(Sec)->sh_size;
454 return object_error::success;
457 template <class ELFT>
458 error_code 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 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
467 uint64_t &Result) const {
468 Result = toELFShdrIter(Sec)->sh_addralign;
469 return object_error::success;
472 template <class ELFT>
473 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
474 bool &Result) const {
475 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
476 return object_error::success;
479 template <class ELFT>
480 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
481 bool &Result) const {
482 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
483 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
484 EShdr->sh_type == ELF::SHT_PROGBITS;
485 return object_error::success;
488 template <class ELFT>
489 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
490 bool &Result) const {
491 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
492 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
493 EShdr->sh_type == ELF::SHT_NOBITS;
494 return object_error::success;
497 template <class ELFT>
499 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
500 bool &Result) const {
501 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
502 return object_error::success;
505 template <class ELFT>
506 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
507 bool &Result) const {
508 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
509 return object_error::success;
512 template <class ELFT>
513 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
514 bool &Result) const {
515 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
516 return object_error::success;
519 template <class ELFT>
520 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
521 bool &Result) const {
522 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
523 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
524 return object_error::success;
527 template <class ELFT>
528 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
530 bool &Result) const {
531 Elf_Sym_Iter ESym = toELFSymIter(Symb);
533 uintX_t Index = ESym->st_shndx;
534 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
536 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
537 return object_error::success;
540 template <class ELFT>
542 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
544 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
545 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
547 return relocation_iterator(RelocationRef(RelData, this));
550 template <class ELFT>
552 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
554 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
555 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
556 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
557 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
560 RelData.d.b = S->sh_size / S->sh_entsize;
562 return relocation_iterator(RelocationRef(RelData, this));
565 template <class ELFT>
567 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
568 if (EF.getHeader()->e_type != ELF::ET_REL)
569 return end_sections();
571 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
572 uintX_t Type = EShdr->sh_type;
573 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
574 return end_sections();
576 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
577 return section_iterator(SectionRef(toDRI(R), this));
581 template <class ELFT>
582 error_code ELFObjectFile<ELFT>::getRelocationNext(DataRefImpl Rel,
583 RelocationRef &Result) const {
585 Result = RelocationRef(Rel, this);
586 return object_error::success;
589 template <class ELFT>
591 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
593 const Elf_Shdr *sec = getRelSection(Rel);
594 switch (sec->sh_type) {
596 report_fatal_error("Invalid section type in Rel!");
598 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
601 case ELF::SHT_RELA: {
602 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
607 return end_symbols();
609 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
611 DataRefImpl SymbolData;
612 switch (SymSec->sh_type) {
614 report_fatal_error("Invalid symbol table section type!");
615 case ELF::SHT_SYMTAB:
616 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
618 case ELF::SHT_DYNSYM:
619 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
623 return symbol_iterator(SymbolRef(SymbolData, this));
626 template <class ELFT>
627 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
628 uint64_t &Result) const {
629 Result = getROffset(Rel);
630 return object_error::success;
633 template <class ELFT>
634 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
635 uint64_t &Result) const {
636 Result = getROffset(Rel);
637 return object_error::success;
640 template <class ELFT>
641 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
642 const Elf_Shdr *sec = getRelSection(Rel);
643 switch (sec->sh_type) {
645 report_fatal_error("Invalid section type in Rel!");
647 return getRel(Rel)->r_offset;
649 return getRela(Rel)->r_offset;
653 template <class ELFT>
654 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
655 uint64_t &Result) const {
656 const Elf_Shdr *sec = getRelSection(Rel);
657 switch (sec->sh_type) {
659 report_fatal_error("Invalid section type in Rel!");
661 Result = getRel(Rel)->getType(EF.isMips64EL());
664 case ELF::SHT_RELA: {
665 Result = getRela(Rel)->getType(EF.isMips64EL());
669 return object_error::success;
672 template <class ELFT>
673 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
674 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
677 template <class ELFT>
678 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
679 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
680 const Elf_Shdr *sec = getRelSection(Rel);
682 switch (sec->sh_type) {
684 return object_error::parse_failed;
686 type = getRel(Rel)->getType(EF.isMips64EL());
689 case ELF::SHT_RELA: {
690 type = getRela(Rel)->getType(EF.isMips64EL());
695 EF.getRelocationTypeName(type, Result);
696 return object_error::success;
699 template <class ELFT>
700 error_code ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
701 int64_t &Result) const {
702 const Elf_Shdr *sec = getRelSection(Rel);
703 switch (sec->sh_type) {
705 report_fatal_error("Invalid section type in Rel!");
708 return object_error::success;
710 case ELF::SHT_RELA: {
711 Result = getRela(Rel)->r_addend;
712 return object_error::success;
717 template <class ELFT>
718 error_code ELFObjectFile<ELFT>::getRelocationValueString(
719 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
720 const Elf_Shdr *sec = getRelSection(Rel);
724 uint16_t symbol_index = 0;
725 switch (sec->sh_type) {
727 return object_error::parse_failed;
729 type = getRel(Rel)->getType(EF.isMips64EL());
730 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
731 // TODO: Read implicit addend from section data.
734 case ELF::SHT_RELA: {
735 type = getRela(Rel)->getType(EF.isMips64EL());
736 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
737 addend = getRela(Rel)->r_addend;
741 const Elf_Sym *symb =
742 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
743 ErrorOr<StringRef> SymName =
744 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
746 return SymName.getError();
747 switch (EF.getHeader()->e_machine) {
750 case ELF::R_X86_64_PC8:
751 case ELF::R_X86_64_PC16:
752 case ELF::R_X86_64_PC32: {
754 raw_string_ostream fmt(fmtbuf);
755 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
757 Result.append(fmtbuf.begin(), fmtbuf.end());
759 case ELF::R_X86_64_8:
760 case ELF::R_X86_64_16:
761 case ELF::R_X86_64_32:
762 case ELF::R_X86_64_32S:
763 case ELF::R_X86_64_64: {
765 raw_string_ostream fmt(fmtbuf);
766 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
768 Result.append(fmtbuf.begin(), fmtbuf.end());
774 case ELF::EM_AARCH64: {
776 raw_string_ostream fmt(fmtbuf);
779 fmt << (addend < 0 ? "" : "+") << addend;
781 Result.append(fmtbuf.begin(), fmtbuf.end());
785 case ELF::EM_HEXAGON:
792 Result.append(res.begin(), res.end());
793 return object_error::success;
796 template <class ELFT>
797 const typename ELFFile<ELFT>::Elf_Sym *
798 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
799 return &*toELFSymIter(Symb);
802 template <class ELFT>
803 const typename ELFObjectFile<ELFT>::Elf_Rel *
804 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
805 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
808 template <class ELFT>
809 const typename ELFObjectFile<ELFT>::Elf_Rela *
810 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
811 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
814 template <class ELFT>
815 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
816 : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
822 template <class ELFT>
823 symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const {
824 return symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
827 template <class ELFT>
828 symbol_iterator ELFObjectFile<ELFT>::end_symbols() const {
829 return symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
832 template <class ELFT>
833 symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const {
834 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
837 template <class ELFT>
838 symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const {
839 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
842 template <class ELFT>
843 section_iterator ELFObjectFile<ELFT>::begin_sections() const {
844 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
847 template <class ELFT>
848 section_iterator ELFObjectFile<ELFT>::end_sections() const {
849 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
852 template <class ELFT>
853 StringRef ELFObjectFile<ELFT>::getLoadName() const {
854 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
855 Elf_Dyn_Iter DE = EF.end_dynamic_table();
857 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
861 return EF.getDynamicString(DI->getVal());
865 template <class ELFT>
866 library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const {
867 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
868 Elf_Dyn_Iter DE = EF.end_dynamic_table();
870 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
873 return library_iterator(LibraryRef(toDRI(DI), this));
876 template <class ELFT>
877 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
878 LibraryRef &Result) const {
879 Elf_Dyn_Iter DI = toELFDynIter(Data);
880 Elf_Dyn_Iter DE = EF.end_dynamic_table();
882 // Skip to the next DT_NEEDED entry.
885 while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
887 Result = LibraryRef(toDRI(DI), this);
888 return object_error::success;
891 template <class ELFT>
892 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
893 StringRef &Res) const {
894 Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
895 return object_error::success;
898 template <class ELFT>
899 library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const {
900 return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
903 template <class ELFT>
904 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
905 return ELFT::Is64Bits ? 8 : 4;
908 template <class ELFT>
909 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
910 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
911 case ELF::ELFCLASS32:
912 switch (EF.getHeader()->e_machine) {
916 return "ELF32-x86-64";
919 case ELF::EM_HEXAGON:
920 return "ELF32-hexagon";
926 return "ELF32-unknown";
928 case ELF::ELFCLASS64:
929 switch (EF.getHeader()->e_machine) {
933 return "ELF64-x86-64";
934 case ELF::EM_AARCH64:
935 return "ELF64-aarch64";
937 return "ELF64-ppc64";
941 return "ELF64-unknown";
944 // FIXME: Proper error handling.
945 report_fatal_error("Invalid ELFCLASS!");
949 template <class ELFT>
950 unsigned ELFObjectFile<ELFT>::getArch() const {
951 switch (EF.getHeader()->e_machine) {
955 return Triple::x86_64;
956 case ELF::EM_AARCH64:
957 return Triple::aarch64;
960 case ELF::EM_HEXAGON:
961 return Triple::hexagon;
963 return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
966 return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
969 return Triple::systemz;
971 return Triple::UnknownArch;
975 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
976 /// and make these member functions?
977 static inline error_code getELFRelocationAddend(const RelocationRef R,
979 const ObjectFile *Obj = R.getObjectFile();
980 DataRefImpl DRI = R.getRawDataRefImpl();
981 // Little-endian 32-bit
982 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
983 return ELFObj->getRelocationAddend(DRI, Addend);
986 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
987 return ELFObj->getRelocationAddend(DRI, Addend);
989 // Little-endian 64-bit
990 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
991 return ELFObj->getRelocationAddend(DRI, Addend);
994 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
995 return ELFObj->getRelocationAddend(DRI, Addend);
997 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
1000 /// This is a generic interface for retrieving GNU symbol version
1001 /// information from an ELFObjectFile.
1002 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
1003 const SymbolRef &Sym,
1006 // Little-endian 32-bit
1007 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1008 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1010 // Big-endian 32-bit
1011 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1012 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1014 // Little-endian 64-bit
1015 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1016 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1018 // Big-endian 64-bit
1019 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1020 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1022 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");