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 void moveSymbolNext(DataRefImpl &Symb) const LLVM_OVERRIDE;
59 error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const
61 error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const
63 error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const
65 error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const
67 error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const LLVM_OVERRIDE;
68 uint32_t getSymbolFlags(DataRefImpl Symb) const LLVM_OVERRIDE;
69 error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const
71 error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const
73 error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
75 error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const
77 error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const
80 void moveSectionNext(DataRefImpl &Sec) const LLVM_OVERRIDE;
81 error_code getSectionName(DataRefImpl Sec, StringRef &Res) const
83 error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const
85 error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const LLVM_OVERRIDE;
86 error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
87 error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const
89 error_code isSectionText(DataRefImpl Sec, bool &Res) const LLVM_OVERRIDE;
90 error_code isSectionData(DataRefImpl Sec, bool &Res) const;
91 error_code isSectionBSS(DataRefImpl Sec, bool &Res) const LLVM_OVERRIDE;
92 error_code isSectionRequiredForExecution(DataRefImpl Sec, bool &Res) const
94 error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
95 error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const LLVM_OVERRIDE;
96 error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
97 error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
98 bool &Result) const LLVM_OVERRIDE;
99 relocation_iterator section_rel_begin(DataRefImpl Sec) const;
100 relocation_iterator section_rel_end(DataRefImpl Sec) const LLVM_OVERRIDE;
101 section_iterator getRelocatedSection(DataRefImpl Sec) const LLVM_OVERRIDE;
103 void moveRelocationNext(DataRefImpl &Rel) const LLVM_OVERRIDE;
104 error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const
106 error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const
108 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const LLVM_OVERRIDE;
109 error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
110 error_code getRelocationTypeName(DataRefImpl Rel,
111 SmallVectorImpl<char> &Result) const
113 error_code getRelocationValueString(DataRefImpl Rel,
114 SmallVectorImpl<char> &Result) const
117 uint64_t getROffset(DataRefImpl Rel) const;
118 StringRef getRelocationTypeName(uint32_t Type) const;
120 /// \brief Get the relocation section that contains \a Rel.
121 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
122 return EF.getSection(Rel.d.a);
125 const Elf_Rel *getRel(DataRefImpl Rel) const;
126 const Elf_Rela *getRela(DataRefImpl Rela) const;
128 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
129 bool IsDynamic = Symb.p & 1;
132 EF.begin_dynamic_symbols().getEntSize(),
133 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
134 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
135 reinterpret_cast<const char *>(Symb.p), IsDynamic);
138 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
140 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
141 static_cast<uintptr_t>(Symb.isDynamic());
145 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
146 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
147 reinterpret_cast<const char *>(Sec.p));
150 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
152 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
156 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
158 DRI.p = reinterpret_cast<uintptr_t>(Sec);
162 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
163 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
164 reinterpret_cast<const char *>(Dyn.p));
167 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
169 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
173 // This flag is used for classof, to distinguish ELFObjectFile from
174 // its subclass. If more subclasses will be created, this flag will
175 // have to become an enum.
176 bool isDyldELFObject;
179 ELFObjectFile(MemoryBuffer *Object, error_code &EC, bool BufferOwned = true);
181 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
183 symbol_iterator begin_symbols() const LLVM_OVERRIDE;
184 symbol_iterator end_symbols() const LLVM_OVERRIDE;
186 symbol_iterator begin_dynamic_symbols() const;
187 symbol_iterator end_dynamic_symbols() const;
189 section_iterator begin_sections() const LLVM_OVERRIDE;
190 section_iterator end_sections() const LLVM_OVERRIDE;
192 library_iterator begin_libraries_needed() const LLVM_OVERRIDE;
193 library_iterator end_libraries_needed() const LLVM_OVERRIDE;
195 error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
196 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
197 bool &IsDefault) const;
200 uint8_t getBytesInAddress() const LLVM_OVERRIDE;
201 StringRef getFileFormatName() const LLVM_OVERRIDE;
202 unsigned getArch() const LLVM_OVERRIDE;
203 StringRef getLoadName() const LLVM_OVERRIDE;
205 const ELFFile<ELFT> *getELFFile() const { return &EF; }
207 bool isDyldType() const { return isDyldELFObject; }
208 static inline bool classof(const Binary *v) {
209 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
214 // Use an alignment of 2 for the typedefs since that is the worst case for
215 // ELF files in archives.
216 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
217 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
218 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
219 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
221 template <class ELFT>
222 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
223 Symb = toDRI(++toELFSymIter(Symb));
226 template <class ELFT>
227 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
228 StringRef &Result) const {
229 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
231 return Name.getError();
233 return object_error::success;
236 template <class ELFT>
237 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
239 bool &IsDefault) const {
240 DataRefImpl Symb = SymRef.getRawDataRefImpl();
241 const Elf_Sym *symb = getSymbol(Symb);
242 ErrorOr<StringRef> Ver =
243 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
245 return Ver.getError();
247 return object_error::success;
250 template <class ELFT>
251 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
252 uint64_t &Result) const {
253 const Elf_Sym *ESym = getSymbol(Symb);
254 const Elf_Shdr *ESec;
255 switch (EF.getSymbolTableIndex(ESym)) {
256 case ELF::SHN_COMMON:
257 // Unintialized symbols have no offset in the object file
259 Result = UnknownAddressOrSize;
260 return object_error::success;
262 Result = ESym->st_value;
263 return object_error::success;
265 ESec = EF.getSection(ESym);
268 switch (ESym->getType()) {
269 case ELF::STT_SECTION:
270 Result = ESec ? ESec->sh_offset : UnknownAddressOrSize;
271 return object_error::success;
273 case ELF::STT_OBJECT:
274 case ELF::STT_NOTYPE:
275 Result = ESym->st_value + (ESec ? ESec->sh_offset : 0);
276 return object_error::success;
278 Result = UnknownAddressOrSize;
279 return object_error::success;
283 template <class ELFT>
284 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
285 uint64_t &Result) const {
286 const Elf_Sym *ESym = getSymbol(Symb);
287 const Elf_Shdr *ESec;
288 switch (EF.getSymbolTableIndex(ESym)) {
289 case ELF::SHN_COMMON:
291 Result = UnknownAddressOrSize;
292 return object_error::success;
294 Result = ESym->st_value;
295 return object_error::success;
297 ESec = EF.getSection(ESym);
300 switch (ESym->getType()) {
301 case ELF::STT_SECTION:
302 Result = ESec ? ESec->sh_addr : UnknownAddressOrSize;
303 return object_error::success;
305 case ELF::STT_OBJECT:
306 case ELF::STT_NOTYPE:
309 switch (EF.getHeader()->e_type) {
312 IsRelocatable = false;
315 IsRelocatable = true;
317 Result = ESym->st_value;
319 // Clear the ARM/Thumb indicator flag.
320 if (EF.getHeader()->e_machine == ELF::EM_ARM)
323 if (IsRelocatable && ESec != 0)
324 Result += ESec->sh_addr;
325 return object_error::success;
327 Result = UnknownAddressOrSize;
328 return object_error::success;
332 template <class ELFT>
333 error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
334 uint32_t &Res) const {
335 Elf_Sym_Iter Sym = toELFSymIter(Symb);
336 if (Sym->st_shndx == ELF::SHN_COMMON)
340 return object_error::success;
343 template <class ELFT>
344 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
345 uint64_t &Result) const {
346 Result = toELFSymIter(Symb)->st_size;
347 return object_error::success;
350 template <class ELFT>
351 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
352 SymbolRef::Type &Result) const {
353 const Elf_Sym *ESym = getSymbol(Symb);
355 switch (ESym->getType()) {
356 case ELF::STT_NOTYPE:
357 Result = SymbolRef::ST_Unknown;
359 case ELF::STT_SECTION:
360 Result = SymbolRef::ST_Debug;
363 Result = SymbolRef::ST_File;
366 Result = SymbolRef::ST_Function;
368 case ELF::STT_OBJECT:
369 case ELF::STT_COMMON:
371 Result = SymbolRef::ST_Data;
374 Result = SymbolRef::ST_Other;
377 return object_error::success;
380 template <class ELFT>
381 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
382 const Elf_Sym *ESym = getSymbol(Symb);
384 uint32_t Result = SymbolRef::SF_None;
386 if (ESym->getBinding() != ELF::STB_LOCAL)
387 Result |= SymbolRef::SF_Global;
389 if (ESym->getBinding() == ELF::STB_WEAK)
390 Result |= SymbolRef::SF_Weak;
392 if (ESym->st_shndx == ELF::SHN_ABS)
393 Result |= SymbolRef::SF_Absolute;
395 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
396 ESym == &*EF.begin_symbols())
397 Result |= SymbolRef::SF_FormatSpecific;
399 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
400 Result |= SymbolRef::SF_Undefined;
402 if (ESym->getType() == ELF::STT_COMMON ||
403 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
404 Result |= SymbolRef::SF_Common;
406 if (ESym->getType() == ELF::STT_TLS)
407 Result |= SymbolRef::SF_ThreadLocal;
412 template <class ELFT>
413 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
414 section_iterator &Res) const {
415 const Elf_Sym *ESym = getSymbol(Symb);
416 const Elf_Shdr *ESec = EF.getSection(ESym);
418 Res = end_sections();
421 Sec.p = reinterpret_cast<intptr_t>(ESec);
422 Res = section_iterator(SectionRef(Sec, this));
424 return object_error::success;
427 template <class ELFT>
428 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
429 uint64_t &Val) const {
430 const Elf_Sym *ESym = getSymbol(Symb);
431 Val = ESym->st_value;
432 return object_error::success;
435 template <class ELFT>
436 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
437 Sec = toDRI(++toELFShdrIter(Sec));
440 template <class ELFT>
441 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
442 StringRef &Result) const {
443 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
445 return Name.getError();
447 return object_error::success;
450 template <class ELFT>
451 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
452 uint64_t &Result) const {
453 Result = toELFShdrIter(Sec)->sh_addr;
454 return object_error::success;
457 template <class ELFT>
458 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
459 uint64_t &Result) const {
460 Result = toELFShdrIter(Sec)->sh_size;
461 return object_error::success;
464 template <class ELFT>
465 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
466 StringRef &Result) const {
467 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
468 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
469 return object_error::success;
472 template <class ELFT>
473 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
474 uint64_t &Result) const {
475 Result = toELFShdrIter(Sec)->sh_addralign;
476 return object_error::success;
479 template <class ELFT>
480 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
481 bool &Result) const {
482 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
483 return object_error::success;
486 template <class ELFT>
487 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
488 bool &Result) const {
489 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
490 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
491 EShdr->sh_type == ELF::SHT_PROGBITS;
492 return object_error::success;
495 template <class ELFT>
496 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
497 bool &Result) const {
498 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
499 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
500 EShdr->sh_type == ELF::SHT_NOBITS;
501 return object_error::success;
504 template <class ELFT>
506 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
507 bool &Result) const {
508 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
509 return object_error::success;
512 template <class ELFT>
513 error_code ELFObjectFile<ELFT>::isSectionVirtual(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>::isSectionZeroInit(DataRefImpl Sec,
521 bool &Result) const {
522 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
523 return object_error::success;
526 template <class ELFT>
527 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
528 bool &Result) const {
529 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
530 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
531 return object_error::success;
534 template <class ELFT>
535 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
537 bool &Result) const {
538 Elf_Sym_Iter ESym = toELFSymIter(Symb);
540 uintX_t Index = ESym->st_shndx;
541 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
543 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
544 return object_error::success;
547 template <class ELFT>
549 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
551 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
552 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
554 return relocation_iterator(RelocationRef(RelData, this));
557 template <class ELFT>
559 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
561 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
562 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
563 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
564 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
567 RelData.d.b = S->sh_size / S->sh_entsize;
569 return relocation_iterator(RelocationRef(RelData, this));
572 template <class ELFT>
574 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
575 if (EF.getHeader()->e_type != ELF::ET_REL)
576 return end_sections();
578 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
579 uintX_t Type = EShdr->sh_type;
580 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
581 return end_sections();
583 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
584 return section_iterator(SectionRef(toDRI(R), this));
588 template <class ELFT>
589 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
593 template <class ELFT>
595 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
597 const Elf_Shdr *sec = getRelSection(Rel);
598 switch (sec->sh_type) {
600 report_fatal_error("Invalid section type in Rel!");
602 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
605 case ELF::SHT_RELA: {
606 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
611 return end_symbols();
613 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
615 DataRefImpl SymbolData;
616 switch (SymSec->sh_type) {
618 report_fatal_error("Invalid symbol table section type!");
619 case ELF::SHT_SYMTAB:
620 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
622 case ELF::SHT_DYNSYM:
623 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
627 return symbol_iterator(SymbolRef(SymbolData, this));
630 template <class ELFT>
631 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
632 uint64_t &Result) const {
633 Result = getROffset(Rel);
634 return object_error::success;
637 template <class ELFT>
638 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
639 uint64_t &Result) const {
640 Result = getROffset(Rel);
641 return object_error::success;
644 template <class ELFT>
645 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
646 const Elf_Shdr *sec = getRelSection(Rel);
647 switch (sec->sh_type) {
649 report_fatal_error("Invalid section type in Rel!");
651 return getRel(Rel)->r_offset;
653 return getRela(Rel)->r_offset;
657 template <class ELFT>
658 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
659 uint64_t &Result) const {
660 const Elf_Shdr *sec = getRelSection(Rel);
661 switch (sec->sh_type) {
663 report_fatal_error("Invalid section type in Rel!");
665 Result = getRel(Rel)->getType(EF.isMips64EL());
668 case ELF::SHT_RELA: {
669 Result = getRela(Rel)->getType(EF.isMips64EL());
673 return object_error::success;
676 template <class ELFT>
677 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
678 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
681 template <class ELFT>
682 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
683 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
684 const Elf_Shdr *sec = getRelSection(Rel);
686 switch (sec->sh_type) {
688 return object_error::parse_failed;
690 type = getRel(Rel)->getType(EF.isMips64EL());
693 case ELF::SHT_RELA: {
694 type = getRela(Rel)->getType(EF.isMips64EL());
699 EF.getRelocationTypeName(type, Result);
700 return object_error::success;
703 template <class ELFT>
704 error_code ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
705 int64_t &Result) const {
706 const Elf_Shdr *sec = getRelSection(Rel);
707 switch (sec->sh_type) {
709 report_fatal_error("Invalid section type in Rel!");
712 return object_error::success;
714 case ELF::SHT_RELA: {
715 Result = getRela(Rel)->r_addend;
716 return object_error::success;
721 template <class ELFT>
722 error_code ELFObjectFile<ELFT>::getRelocationValueString(
723 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
724 const Elf_Shdr *sec = getRelSection(Rel);
728 uint16_t symbol_index = 0;
729 switch (sec->sh_type) {
731 return object_error::parse_failed;
733 type = getRel(Rel)->getType(EF.isMips64EL());
734 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
735 // TODO: Read implicit addend from section data.
738 case ELF::SHT_RELA: {
739 type = getRela(Rel)->getType(EF.isMips64EL());
740 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
741 addend = getRela(Rel)->r_addend;
745 const Elf_Sym *symb =
746 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
747 ErrorOr<StringRef> SymName =
748 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
750 return SymName.getError();
751 switch (EF.getHeader()->e_machine) {
754 case ELF::R_X86_64_PC8:
755 case ELF::R_X86_64_PC16:
756 case ELF::R_X86_64_PC32: {
758 raw_string_ostream fmt(fmtbuf);
759 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
761 Result.append(fmtbuf.begin(), fmtbuf.end());
763 case ELF::R_X86_64_8:
764 case ELF::R_X86_64_16:
765 case ELF::R_X86_64_32:
766 case ELF::R_X86_64_32S:
767 case ELF::R_X86_64_64: {
769 raw_string_ostream fmt(fmtbuf);
770 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
772 Result.append(fmtbuf.begin(), fmtbuf.end());
778 case ELF::EM_AARCH64: {
780 raw_string_ostream fmt(fmtbuf);
783 fmt << (addend < 0 ? "" : "+") << addend;
785 Result.append(fmtbuf.begin(), fmtbuf.end());
789 case ELF::EM_HEXAGON:
797 Result.append(res.begin(), res.end());
798 return object_error::success;
801 template <class ELFT>
802 const typename ELFFile<ELFT>::Elf_Sym *
803 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
804 return &*toELFSymIter(Symb);
807 template <class ELFT>
808 const typename ELFObjectFile<ELFT>::Elf_Rel *
809 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
810 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
813 template <class ELFT>
814 const typename ELFObjectFile<ELFT>::Elf_Rela *
815 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
816 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
819 template <class ELFT>
820 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec,
822 : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
825 Object, BufferOwned),
828 template <class ELFT>
829 symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const {
830 return symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
833 template <class ELFT>
834 symbol_iterator ELFObjectFile<ELFT>::end_symbols() const {
835 return symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
838 template <class ELFT>
839 symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const {
840 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
843 template <class ELFT>
844 symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const {
845 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
848 template <class ELFT>
849 section_iterator ELFObjectFile<ELFT>::begin_sections() const {
850 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
853 template <class ELFT>
854 section_iterator ELFObjectFile<ELFT>::end_sections() const {
855 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
858 template <class ELFT>
859 StringRef ELFObjectFile<ELFT>::getLoadName() const {
860 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
861 Elf_Dyn_Iter DE = EF.end_dynamic_table();
863 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
867 return EF.getDynamicString(DI->getVal());
871 template <class ELFT>
872 library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const {
873 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
874 Elf_Dyn_Iter DE = EF.end_dynamic_table();
876 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
879 return library_iterator(LibraryRef(toDRI(DI), this));
882 template <class ELFT>
883 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
884 LibraryRef &Result) const {
885 Elf_Dyn_Iter DI = toELFDynIter(Data);
886 Elf_Dyn_Iter DE = EF.end_dynamic_table();
888 // Skip to the next DT_NEEDED entry.
891 while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
893 Result = LibraryRef(toDRI(DI), this);
894 return object_error::success;
897 template <class ELFT>
898 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
899 StringRef &Res) const {
900 Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
901 return object_error::success;
904 template <class ELFT>
905 library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const {
906 return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
909 template <class ELFT>
910 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
911 return ELFT::Is64Bits ? 8 : 4;
914 template <class ELFT>
915 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
916 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
917 case ELF::ELFCLASS32:
918 switch (EF.getHeader()->e_machine) {
922 return "ELF32-x86-64";
925 case ELF::EM_HEXAGON:
926 return "ELF32-hexagon";
932 case ELF::EM_SPARC32PLUS:
933 return "ELF32-sparc";
935 return "ELF32-unknown";
937 case ELF::ELFCLASS64:
938 switch (EF.getHeader()->e_machine) {
942 return "ELF64-x86-64";
943 case ELF::EM_AARCH64:
944 return "ELF64-aarch64";
946 return "ELF64-ppc64";
949 case ELF::EM_SPARCV9:
950 return "ELF64-sparc";
952 return "ELF64-unknown";
955 // FIXME: Proper error handling.
956 report_fatal_error("Invalid ELFCLASS!");
960 template <class ELFT>
961 unsigned ELFObjectFile<ELFT>::getArch() const {
962 switch (EF.getHeader()->e_machine) {
966 return Triple::x86_64;
967 case ELF::EM_AARCH64:
968 return Triple::aarch64;
971 case ELF::EM_HEXAGON:
972 return Triple::hexagon;
974 return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
977 return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
980 return Triple::systemz;
983 case ELF::EM_SPARC32PLUS:
984 return Triple::sparc;
985 case ELF::EM_SPARCV9:
986 return Triple::sparcv9;
989 return Triple::UnknownArch;
993 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
994 /// and make these member functions?
995 static inline error_code getELFRelocationAddend(const RelocationRef R,
997 const ObjectFile *Obj = R.getObjectFile();
998 DataRefImpl DRI = R.getRawDataRefImpl();
999 // Little-endian 32-bit
1000 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1001 return ELFObj->getRelocationAddend(DRI, Addend);
1003 // Big-endian 32-bit
1004 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1005 return ELFObj->getRelocationAddend(DRI, Addend);
1007 // Little-endian 64-bit
1008 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1009 return ELFObj->getRelocationAddend(DRI, Addend);
1011 // Big-endian 64-bit
1012 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1013 return ELFObj->getRelocationAddend(DRI, Addend);
1015 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
1018 /// This is a generic interface for retrieving GNU symbol version
1019 /// information from an ELFObjectFile.
1020 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
1021 const SymbolRef &Sym,
1024 // Little-endian 32-bit
1025 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1026 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1028 // Big-endian 32-bit
1029 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1030 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1032 // Little-endian 64-bit
1033 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1034 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1036 // Big-endian 64-bit
1037 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1038 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1040 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");