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_Ehdr Elf_Ehdr;
48 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
49 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
50 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
52 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
53 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
54 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
59 void moveSymbolNext(DataRefImpl &Symb) const override;
60 error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const override;
61 error_code getSymbolFileOffset(DataRefImpl Symb,
62 uint64_t &Res) const override;
63 error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const override;
64 error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const override;
65 error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
66 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
67 error_code getSymbolType(DataRefImpl Symb,
68 SymbolRef::Type &Res) const override;
69 error_code getSymbolSection(DataRefImpl Symb,
70 section_iterator &Res) const override;
71 error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const override;
73 error_code getLibraryNext(DataRefImpl Data,
74 LibraryRef &Result) const override;
75 error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const override;
77 void moveSectionNext(DataRefImpl &Sec) const override;
78 error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override;
79 error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const override;
80 error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
81 error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override;
82 error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const override;
83 error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
84 error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
85 error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
86 error_code isSectionRequiredForExecution(DataRefImpl Sec,
87 bool &Res) const override;
88 error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
89 error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
90 error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const override;
91 error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
92 bool &Result) const override;
93 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
94 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
95 bool section_rel_empty(DataRefImpl Sec) const override;
96 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
98 void moveRelocationNext(DataRefImpl &Rel) const override;
99 error_code getRelocationAddress(DataRefImpl Rel,
100 uint64_t &Res) const override;
101 error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const override;
102 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
103 error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const override;
104 error_code getRelocationTypeName(DataRefImpl Rel,
105 SmallVectorImpl<char> &Result) const override;
106 error_code getRelocationValueString(DataRefImpl Rel,
107 SmallVectorImpl<char> &Result) const override;
109 uint64_t getROffset(DataRefImpl Rel) const;
110 StringRef getRelocationTypeName(uint32_t Type) const;
112 /// \brief Get the relocation section that contains \a Rel.
113 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
114 return EF.getSection(Rel.d.a);
117 const Elf_Rel *getRel(DataRefImpl Rel) const;
118 const Elf_Rela *getRela(DataRefImpl Rela) const;
120 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
121 bool IsDynamic = Symb.p & 1;
124 EF.begin_dynamic_symbols().getEntSize(),
125 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
126 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
127 reinterpret_cast<const char *>(Symb.p), IsDynamic);
130 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
132 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
133 static_cast<uintptr_t>(Symb.isDynamic());
137 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
138 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
139 reinterpret_cast<const char *>(Sec.p));
142 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
144 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
148 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
150 DRI.p = reinterpret_cast<uintptr_t>(Sec);
154 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
155 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
156 reinterpret_cast<const char *>(Dyn.p));
159 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
161 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
165 // This flag is used for classof, to distinguish ELFObjectFile from
166 // its subclass. If more subclasses will be created, this flag will
167 // have to become an enum.
168 bool isDyldELFObject;
171 ELFObjectFile(MemoryBuffer *Object, error_code &EC, bool BufferOwned = true);
173 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
175 basic_symbol_iterator symbol_begin_impl() const override;
176 basic_symbol_iterator symbol_end_impl() const override;
178 symbol_iterator dynamic_symbol_begin() const;
179 symbol_iterator dynamic_symbol_end() const;
181 section_iterator section_begin() const override;
182 section_iterator section_end() const override;
184 library_iterator needed_library_begin() const override;
185 library_iterator needed_library_end() const override;
187 error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
188 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
189 bool &IsDefault) const;
192 uint8_t getBytesInAddress() const override;
193 StringRef getFileFormatName() const override;
194 unsigned getArch() const override;
195 StringRef getLoadName() const override;
197 const ELFFile<ELFT> *getELFFile() const { return &EF; }
199 bool isDyldType() const { return isDyldELFObject; }
200 static inline bool classof(const Binary *v) {
201 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
206 // Use an alignment of 2 for the typedefs since that is the worst case for
207 // ELF files in archives.
208 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
209 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
210 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
211 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
213 template <class ELFT>
214 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
215 Symb = toDRI(++toELFSymIter(Symb));
218 template <class ELFT>
219 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
220 StringRef &Result) const {
221 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
223 return Name.getError();
225 return object_error::success;
228 template <class ELFT>
229 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
231 bool &IsDefault) const {
232 DataRefImpl Symb = SymRef.getRawDataRefImpl();
233 const Elf_Sym *symb = getSymbol(Symb);
234 ErrorOr<StringRef> Ver =
235 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
237 return Ver.getError();
239 return object_error::success;
242 template <class ELFT>
243 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
244 uint64_t &Result) const {
245 const Elf_Sym *ESym = getSymbol(Symb);
246 const Elf_Shdr *ESec;
247 switch (EF.getSymbolTableIndex(ESym)) {
248 case ELF::SHN_COMMON:
249 // Unintialized symbols have no offset in the object file
251 Result = UnknownAddressOrSize;
252 return object_error::success;
254 Result = ESym->st_value;
255 return object_error::success;
257 ESec = EF.getSection(ESym);
260 switch (ESym->getType()) {
261 case ELF::STT_SECTION:
262 Result = ESec ? ESec->sh_offset : UnknownAddressOrSize;
263 return object_error::success;
265 case ELF::STT_OBJECT:
266 case ELF::STT_NOTYPE:
267 Result = ESym->st_value + (ESec ? ESec->sh_offset : 0);
268 return object_error::success;
270 Result = UnknownAddressOrSize;
271 return object_error::success;
275 template <class ELFT>
276 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
277 uint64_t &Result) const {
278 const Elf_Sym *ESym = getSymbol(Symb);
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;
291 const Elf_Ehdr *Header = EF.getHeader();
292 Result = ESym->st_value;
294 // Clear the ARM/Thumb indicator flag.
295 if (EF.getHeader()->e_machine == ELF::EM_ARM &&
296 ESym->getType() == ELF::STT_FUNC)
299 if (Header->e_type == ELF::ET_REL)
300 Result += EF.getSection(ESym)->sh_addr;
302 return object_error::success;
305 template <class ELFT>
306 error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
307 uint32_t &Res) const {
308 Elf_Sym_Iter Sym = toELFSymIter(Symb);
309 if (Sym->st_shndx == ELF::SHN_COMMON)
313 return object_error::success;
316 template <class ELFT>
317 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
318 uint64_t &Result) const {
319 Result = toELFSymIter(Symb)->st_size;
320 return object_error::success;
323 template <class ELFT>
324 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
325 SymbolRef::Type &Result) const {
326 const Elf_Sym *ESym = getSymbol(Symb);
328 switch (ESym->getType()) {
329 case ELF::STT_NOTYPE:
330 Result = SymbolRef::ST_Unknown;
332 case ELF::STT_SECTION:
333 Result = SymbolRef::ST_Debug;
336 Result = SymbolRef::ST_File;
339 Result = SymbolRef::ST_Function;
341 case ELF::STT_OBJECT:
342 case ELF::STT_COMMON:
344 Result = SymbolRef::ST_Data;
347 Result = SymbolRef::ST_Other;
350 return object_error::success;
353 template <class ELFT>
354 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
355 Elf_Sym_Iter EIter = toELFSymIter(Symb);
356 const Elf_Sym *ESym = &*EIter;
358 uint32_t Result = SymbolRef::SF_None;
360 if (ESym->getBinding() != ELF::STB_LOCAL)
361 Result |= SymbolRef::SF_Global;
363 if (ESym->getBinding() == ELF::STB_WEAK)
364 Result |= SymbolRef::SF_Weak;
366 if (ESym->st_shndx == ELF::SHN_ABS)
367 Result |= SymbolRef::SF_Absolute;
369 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
370 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
371 Result |= SymbolRef::SF_FormatSpecific;
373 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
374 Result |= SymbolRef::SF_Undefined;
376 if (ESym->getType() == ELF::STT_COMMON ||
377 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
378 Result |= SymbolRef::SF_Common;
383 template <class ELFT>
384 error_code 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 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
400 uint64_t &Val) const {
401 const Elf_Sym *ESym = getSymbol(Symb);
402 Val = ESym->st_value;
403 return object_error::success;
406 template <class ELFT>
407 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
408 Sec = toDRI(++toELFShdrIter(Sec));
411 template <class ELFT>
412 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
413 StringRef &Result) const {
414 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
416 return Name.getError();
418 return object_error::success;
421 template <class ELFT>
422 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
423 uint64_t &Result) const {
424 Result = toELFShdrIter(Sec)->sh_addr;
425 return object_error::success;
428 template <class ELFT>
429 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
430 uint64_t &Result) const {
431 Result = toELFShdrIter(Sec)->sh_size;
432 return object_error::success;
435 template <class ELFT>
436 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
437 StringRef &Result) const {
438 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
439 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
440 return object_error::success;
443 template <class ELFT>
444 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
445 uint64_t &Result) const {
446 Result = toELFShdrIter(Sec)->sh_addralign;
447 return object_error::success;
450 template <class ELFT>
451 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
452 bool &Result) const {
453 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
454 return object_error::success;
457 template <class ELFT>
458 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
459 bool &Result) const {
460 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
461 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
462 EShdr->sh_type == ELF::SHT_PROGBITS;
463 return object_error::success;
466 template <class ELFT>
467 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
468 bool &Result) const {
469 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
470 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
471 EShdr->sh_type == ELF::SHT_NOBITS;
472 return object_error::success;
475 template <class ELFT>
477 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
478 bool &Result) const {
479 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
480 return object_error::success;
483 template <class ELFT>
484 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
485 bool &Result) const {
486 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
487 return object_error::success;
490 template <class ELFT>
491 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
492 bool &Result) const {
493 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
494 return object_error::success;
497 template <class ELFT>
498 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
499 bool &Result) const {
500 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
501 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
502 return object_error::success;
505 template <class ELFT>
506 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
508 bool &Result) const {
509 Elf_Sym_Iter ESym = toELFSymIter(Symb);
511 uintX_t Index = ESym->st_shndx;
512 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
514 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
515 return object_error::success;
518 template <class ELFT>
520 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
522 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
523 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
525 return relocation_iterator(RelocationRef(RelData, this));
528 template <class ELFT>
530 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
532 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
533 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
534 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
535 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
538 RelData.d.b = S->sh_size / S->sh_entsize;
540 return relocation_iterator(RelocationRef(RelData, this));
543 template <class ELFT>
544 bool ELFObjectFile<ELFT>::section_rel_empty(DataRefImpl Sec) const {
545 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
546 return S->sh_size == 0;
549 template <class ELFT>
551 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
552 if (EF.getHeader()->e_type != ELF::ET_REL)
553 return section_end();
555 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
556 uintX_t Type = EShdr->sh_type;
557 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
558 return section_end();
560 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
561 return section_iterator(SectionRef(toDRI(R), this));
565 template <class ELFT>
566 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
570 template <class ELFT>
572 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
574 const Elf_Shdr *sec = getRelSection(Rel);
575 switch (sec->sh_type) {
577 report_fatal_error("Invalid section type in Rel!");
579 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
582 case ELF::SHT_RELA: {
583 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
590 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
592 DataRefImpl SymbolData;
593 switch (SymSec->sh_type) {
595 report_fatal_error("Invalid symbol table section type!");
596 case ELF::SHT_SYMTAB:
597 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
599 case ELF::SHT_DYNSYM:
600 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
604 return symbol_iterator(SymbolRef(SymbolData, this));
607 template <class ELFT>
608 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
609 uint64_t &Result) const {
610 assert((EF.getHeader()->e_type == ELF::ET_EXEC ||
611 EF.getHeader()->e_type == ELF::ET_DYN) &&
612 "Only executable and shared objects files have relocation addresses");
613 Result = getROffset(Rel);
614 return object_error::success;
617 template <class ELFT>
618 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
619 uint64_t &Result) const {
620 assert(EF.getHeader()->e_type == ELF::ET_REL &&
621 "Only relocatable object files have relocation offsets");
622 Result = getROffset(Rel);
623 return object_error::success;
626 template <class ELFT>
627 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
628 const Elf_Shdr *sec = getRelSection(Rel);
629 switch (sec->sh_type) {
631 report_fatal_error("Invalid section type in Rel!");
633 return getRel(Rel)->r_offset;
635 return getRela(Rel)->r_offset;
639 template <class ELFT>
640 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
641 uint64_t &Result) const {
642 const Elf_Shdr *sec = getRelSection(Rel);
643 switch (sec->sh_type) {
645 report_fatal_error("Invalid section type in Rel!");
647 Result = getRel(Rel)->getType(EF.isMips64EL());
650 case ELF::SHT_RELA: {
651 Result = getRela(Rel)->getType(EF.isMips64EL());
655 return object_error::success;
658 template <class ELFT>
659 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
660 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
663 template <class ELFT>
664 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
665 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
666 const Elf_Shdr *sec = getRelSection(Rel);
668 switch (sec->sh_type) {
670 return object_error::parse_failed;
672 type = getRel(Rel)->getType(EF.isMips64EL());
675 case ELF::SHT_RELA: {
676 type = getRela(Rel)->getType(EF.isMips64EL());
681 EF.getRelocationTypeName(type, Result);
682 return object_error::success;
685 template <class ELFT>
686 error_code ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
687 int64_t &Result) const {
688 const Elf_Shdr *sec = getRelSection(Rel);
689 switch (sec->sh_type) {
691 report_fatal_error("Invalid section type in Rel!");
694 return object_error::success;
696 case ELF::SHT_RELA: {
697 Result = getRela(Rel)->r_addend;
698 return object_error::success;
703 template <class ELFT>
704 error_code ELFObjectFile<ELFT>::getRelocationValueString(
705 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
706 const Elf_Shdr *sec = getRelSection(Rel);
710 uint16_t symbol_index = 0;
711 switch (sec->sh_type) {
713 return object_error::parse_failed;
715 type = getRel(Rel)->getType(EF.isMips64EL());
716 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
717 // TODO: Read implicit addend from section data.
720 case ELF::SHT_RELA: {
721 type = getRela(Rel)->getType(EF.isMips64EL());
722 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
723 addend = getRela(Rel)->r_addend;
727 const Elf_Sym *symb =
728 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
729 ErrorOr<StringRef> SymName =
730 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
732 return SymName.getError();
733 switch (EF.getHeader()->e_machine) {
736 case ELF::R_X86_64_PC8:
737 case ELF::R_X86_64_PC16:
738 case ELF::R_X86_64_PC32: {
740 raw_string_ostream fmt(fmtbuf);
741 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
743 Result.append(fmtbuf.begin(), fmtbuf.end());
745 case ELF::R_X86_64_8:
746 case ELF::R_X86_64_16:
747 case ELF::R_X86_64_32:
748 case ELF::R_X86_64_32S:
749 case ELF::R_X86_64_64: {
751 raw_string_ostream fmt(fmtbuf);
752 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
754 Result.append(fmtbuf.begin(), fmtbuf.end());
760 case ELF::EM_AARCH64: {
762 raw_string_ostream fmt(fmtbuf);
765 fmt << (addend < 0 ? "" : "+") << addend;
767 Result.append(fmtbuf.begin(), fmtbuf.end());
771 case ELF::EM_HEXAGON:
779 Result.append(res.begin(), res.end());
780 return object_error::success;
783 template <class ELFT>
784 const typename ELFFile<ELFT>::Elf_Sym *
785 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
786 return &*toELFSymIter(Symb);
789 template <class ELFT>
790 const typename ELFObjectFile<ELFT>::Elf_Rel *
791 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
792 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
795 template <class ELFT>
796 const typename ELFObjectFile<ELFT>::Elf_Rela *
797 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
798 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
801 template <class ELFT>
802 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec,
804 : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
807 Object, BufferOwned),
810 template <class ELFT>
811 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
812 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
815 template <class ELFT>
816 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
817 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
820 template <class ELFT>
821 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
822 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
825 template <class ELFT>
826 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
827 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
830 template <class ELFT>
831 section_iterator ELFObjectFile<ELFT>::section_begin() const {
832 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
835 template <class ELFT>
836 section_iterator ELFObjectFile<ELFT>::section_end() const {
837 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
840 template <class ELFT>
841 StringRef ELFObjectFile<ELFT>::getLoadName() const {
842 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
843 Elf_Dyn_Iter DE = EF.end_dynamic_table();
845 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
849 return EF.getDynamicString(DI->getVal());
853 template <class ELFT>
854 library_iterator ELFObjectFile<ELFT>::needed_library_begin() const {
855 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
856 Elf_Dyn_Iter DE = EF.end_dynamic_table();
858 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
861 return library_iterator(LibraryRef(toDRI(DI), this));
864 template <class ELFT>
865 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
866 LibraryRef &Result) const {
867 Elf_Dyn_Iter DI = toELFDynIter(Data);
868 Elf_Dyn_Iter DE = EF.end_dynamic_table();
870 // Skip to the next DT_NEEDED entry.
873 while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
875 Result = LibraryRef(toDRI(DI), this);
876 return object_error::success;
879 template <class ELFT>
880 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
881 StringRef &Res) const {
882 Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
883 return object_error::success;
886 template <class ELFT>
887 library_iterator ELFObjectFile<ELFT>::needed_library_end() const {
888 return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
891 template <class ELFT>
892 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
893 return ELFT::Is64Bits ? 8 : 4;
896 template <class ELFT>
897 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
898 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
899 case ELF::ELFCLASS32:
900 switch (EF.getHeader()->e_machine) {
904 return "ELF32-x86-64";
907 case ELF::EM_HEXAGON:
908 return "ELF32-hexagon";
914 case ELF::EM_SPARC32PLUS:
915 return "ELF32-sparc";
917 return "ELF32-unknown";
919 case ELF::ELFCLASS64:
920 switch (EF.getHeader()->e_machine) {
924 return "ELF64-x86-64";
925 case ELF::EM_AARCH64:
926 return "ELF64-aarch64";
928 return "ELF64-ppc64";
931 case ELF::EM_SPARCV9:
932 return "ELF64-sparc";
936 return "ELF64-unknown";
939 // FIXME: Proper error handling.
940 report_fatal_error("Invalid ELFCLASS!");
944 template <class ELFT>
945 unsigned ELFObjectFile<ELFT>::getArch() const {
946 switch (EF.getHeader()->e_machine) {
950 return Triple::x86_64;
951 case ELF::EM_AARCH64:
952 return Triple::aarch64;
955 case ELF::EM_HEXAGON:
956 return Triple::hexagon;
958 return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
961 return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
964 return Triple::systemz;
967 case ELF::EM_SPARC32PLUS:
968 return Triple::sparc;
969 case ELF::EM_SPARCV9:
970 return Triple::sparcv9;
973 return Triple::UnknownArch;
977 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
978 /// and make these member functions?
979 inline error_code getELFRelocationAddend(const RelocationRef R,
981 const ObjectFile *Obj = R.getObjectFile();
982 DataRefImpl DRI = R.getRawDataRefImpl();
983 // Little-endian 32-bit
984 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
985 return ELFObj->getRelocationAddend(DRI, Addend);
988 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
989 return ELFObj->getRelocationAddend(DRI, Addend);
991 // Little-endian 64-bit
992 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
993 return ELFObj->getRelocationAddend(DRI, Addend);
996 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
997 return ELFObj->getRelocationAddend(DRI, Addend);
999 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
1002 inline std::pair<symbol_iterator, symbol_iterator>
1003 getELFDynamicSymbolIterators(SymbolicFile *Obj) {
1004 if (const ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
1005 return std::make_pair(ELF->dynamic_symbol_begin(),
1006 ELF->dynamic_symbol_end());
1007 if (const ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
1008 return std::make_pair(ELF->dynamic_symbol_begin(),
1009 ELF->dynamic_symbol_end());
1010 if (const ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
1011 return std::make_pair(ELF->dynamic_symbol_begin(),
1012 ELF->dynamic_symbol_end());
1013 if (const ELF64BEObjectFile *ELF = cast<ELF64BEObjectFile>(Obj))
1014 return std::make_pair(ELF->dynamic_symbol_begin(),
1015 ELF->dynamic_symbol_end());
1018 "Object passed to getELFDynamicSymbolIterators() is not ELF");
1021 /// This is a generic interface for retrieving GNU symbol version
1022 /// information from an ELFObjectFile.
1023 inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
1024 const SymbolRef &Sym, StringRef &Version,
1026 // Little-endian 32-bit
1027 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1028 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1030 // Big-endian 32-bit
1031 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1032 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1034 // Little-endian 64-bit
1035 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1036 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1038 // Big-endian 64-bit
1039 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1040 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1042 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");