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 (Header->e_machine == ELF::EM_ARM)
298 if (Header->e_type == ELF::ET_REL)
299 Result += EF.getSection(ESym)->sh_addr;
301 return object_error::success;
304 template <class ELFT>
305 error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
306 uint32_t &Res) const {
307 Elf_Sym_Iter Sym = toELFSymIter(Symb);
308 if (Sym->st_shndx == ELF::SHN_COMMON)
312 return object_error::success;
315 template <class ELFT>
316 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
317 uint64_t &Result) const {
318 Result = toELFSymIter(Symb)->st_size;
319 return object_error::success;
322 template <class ELFT>
323 error_code 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>
383 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
384 section_iterator &Res) const {
385 const Elf_Sym *ESym = getSymbol(Symb);
386 const Elf_Shdr *ESec = EF.getSection(ESym);
391 Sec.p = reinterpret_cast<intptr_t>(ESec);
392 Res = section_iterator(SectionRef(Sec, this));
394 return object_error::success;
397 template <class ELFT>
398 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
399 uint64_t &Val) const {
400 const Elf_Sym *ESym = getSymbol(Symb);
401 Val = ESym->st_value;
402 return object_error::success;
405 template <class ELFT>
406 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
407 Sec = toDRI(++toELFShdrIter(Sec));
410 template <class ELFT>
411 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
412 StringRef &Result) const {
413 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
415 return Name.getError();
417 return object_error::success;
420 template <class ELFT>
421 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
422 uint64_t &Result) const {
423 Result = toELFShdrIter(Sec)->sh_addr;
424 return object_error::success;
427 template <class ELFT>
428 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
429 uint64_t &Result) const {
430 Result = toELFShdrIter(Sec)->sh_size;
431 return object_error::success;
434 template <class ELFT>
435 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
436 StringRef &Result) const {
437 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
438 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
439 return object_error::success;
442 template <class ELFT>
443 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
444 uint64_t &Result) const {
445 Result = toELFShdrIter(Sec)->sh_addralign;
446 return object_error::success;
449 template <class ELFT>
450 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
451 bool &Result) const {
452 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
453 return object_error::success;
456 template <class ELFT>
457 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
458 bool &Result) const {
459 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
460 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
461 EShdr->sh_type == ELF::SHT_PROGBITS;
462 return object_error::success;
465 template <class ELFT>
466 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
467 bool &Result) const {
468 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
469 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
470 EShdr->sh_type == ELF::SHT_NOBITS;
471 return object_error::success;
474 template <class ELFT>
476 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
477 bool &Result) const {
478 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
479 return object_error::success;
482 template <class ELFT>
483 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
484 bool &Result) const {
485 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
486 return object_error::success;
489 template <class ELFT>
490 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
491 bool &Result) const {
492 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
493 return object_error::success;
496 template <class ELFT>
497 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
498 bool &Result) const {
499 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
500 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
501 return object_error::success;
504 template <class ELFT>
505 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
507 bool &Result) const {
508 Elf_Sym_Iter ESym = toELFSymIter(Symb);
510 uintX_t Index = ESym->st_shndx;
511 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
513 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
514 return object_error::success;
517 template <class ELFT>
519 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
521 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
522 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
524 return relocation_iterator(RelocationRef(RelData, this));
527 template <class ELFT>
529 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
531 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
532 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
533 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
534 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
537 RelData.d.b = S->sh_size / S->sh_entsize;
539 return relocation_iterator(RelocationRef(RelData, this));
542 template <class ELFT>
543 bool ELFObjectFile<ELFT>::section_rel_empty(DataRefImpl Sec) const {
544 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
545 return S->sh_size == 0;
548 template <class ELFT>
550 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
551 if (EF.getHeader()->e_type != ELF::ET_REL)
552 return section_end();
554 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
555 uintX_t Type = EShdr->sh_type;
556 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
557 return section_end();
559 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
560 return section_iterator(SectionRef(toDRI(R), this));
564 template <class ELFT>
565 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
569 template <class ELFT>
571 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
573 const Elf_Shdr *sec = getRelSection(Rel);
574 switch (sec->sh_type) {
576 report_fatal_error("Invalid section type in Rel!");
578 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
581 case ELF::SHT_RELA: {
582 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
589 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
591 DataRefImpl SymbolData;
592 switch (SymSec->sh_type) {
594 report_fatal_error("Invalid symbol table section type!");
595 case ELF::SHT_SYMTAB:
596 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
598 case ELF::SHT_DYNSYM:
599 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
603 return symbol_iterator(SymbolRef(SymbolData, this));
606 template <class ELFT>
607 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
608 uint64_t &Result) const {
609 assert((EF.getHeader()->e_type == ELF::ET_EXEC ||
610 EF.getHeader()->e_type == ELF::ET_DYN) &&
611 "Only executable and shared objects files have relocation addresses");
612 Result = getROffset(Rel);
613 return object_error::success;
616 template <class ELFT>
617 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
618 uint64_t &Result) const {
619 assert(EF.getHeader()->e_type == ELF::ET_REL &&
620 "Only relocatable object files have relocation offsets");
621 Result = getROffset(Rel);
622 return object_error::success;
625 template <class ELFT>
626 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
627 const Elf_Shdr *sec = getRelSection(Rel);
628 switch (sec->sh_type) {
630 report_fatal_error("Invalid section type in Rel!");
632 return getRel(Rel)->r_offset;
634 return getRela(Rel)->r_offset;
638 template <class ELFT>
639 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
640 uint64_t &Result) const {
641 const Elf_Shdr *sec = getRelSection(Rel);
642 switch (sec->sh_type) {
644 report_fatal_error("Invalid section type in Rel!");
646 Result = getRel(Rel)->getType(EF.isMips64EL());
649 case ELF::SHT_RELA: {
650 Result = getRela(Rel)->getType(EF.isMips64EL());
654 return object_error::success;
657 template <class ELFT>
658 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
659 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
662 template <class ELFT>
663 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
664 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
665 const Elf_Shdr *sec = getRelSection(Rel);
667 switch (sec->sh_type) {
669 return object_error::parse_failed;
671 type = getRel(Rel)->getType(EF.isMips64EL());
674 case ELF::SHT_RELA: {
675 type = getRela(Rel)->getType(EF.isMips64EL());
680 EF.getRelocationTypeName(type, Result);
681 return object_error::success;
684 template <class ELFT>
685 error_code ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
686 int64_t &Result) const {
687 const Elf_Shdr *sec = getRelSection(Rel);
688 switch (sec->sh_type) {
690 report_fatal_error("Invalid section type in Rel!");
693 return object_error::success;
695 case ELF::SHT_RELA: {
696 Result = getRela(Rel)->r_addend;
697 return object_error::success;
702 template <class ELFT>
703 error_code ELFObjectFile<ELFT>::getRelocationValueString(
704 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
705 const Elf_Shdr *sec = getRelSection(Rel);
709 uint16_t symbol_index = 0;
710 switch (sec->sh_type) {
712 return object_error::parse_failed;
714 type = getRel(Rel)->getType(EF.isMips64EL());
715 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
716 // TODO: Read implicit addend from section data.
719 case ELF::SHT_RELA: {
720 type = getRela(Rel)->getType(EF.isMips64EL());
721 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
722 addend = getRela(Rel)->r_addend;
726 const Elf_Sym *symb =
727 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
728 ErrorOr<StringRef> SymName =
729 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
731 return SymName.getError();
732 switch (EF.getHeader()->e_machine) {
735 case ELF::R_X86_64_PC8:
736 case ELF::R_X86_64_PC16:
737 case ELF::R_X86_64_PC32: {
739 raw_string_ostream fmt(fmtbuf);
740 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
742 Result.append(fmtbuf.begin(), fmtbuf.end());
744 case ELF::R_X86_64_8:
745 case ELF::R_X86_64_16:
746 case ELF::R_X86_64_32:
747 case ELF::R_X86_64_32S:
748 case ELF::R_X86_64_64: {
750 raw_string_ostream fmt(fmtbuf);
751 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
753 Result.append(fmtbuf.begin(), fmtbuf.end());
759 case ELF::EM_AARCH64: {
761 raw_string_ostream fmt(fmtbuf);
764 fmt << (addend < 0 ? "" : "+") << addend;
766 Result.append(fmtbuf.begin(), fmtbuf.end());
770 case ELF::EM_HEXAGON:
778 Result.append(res.begin(), res.end());
779 return object_error::success;
782 template <class ELFT>
783 const typename ELFFile<ELFT>::Elf_Sym *
784 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
785 return &*toELFSymIter(Symb);
788 template <class ELFT>
789 const typename ELFObjectFile<ELFT>::Elf_Rel *
790 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
791 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
794 template <class ELFT>
795 const typename ELFObjectFile<ELFT>::Elf_Rela *
796 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
797 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
800 template <class ELFT>
801 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec,
803 : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
806 Object, BufferOwned),
809 template <class ELFT>
810 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
811 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
814 template <class ELFT>
815 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
816 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
819 template <class ELFT>
820 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
821 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
824 template <class ELFT>
825 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
826 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
829 template <class ELFT>
830 section_iterator ELFObjectFile<ELFT>::section_begin() const {
831 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
834 template <class ELFT>
835 section_iterator ELFObjectFile<ELFT>::section_end() const {
836 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
839 template <class ELFT>
840 StringRef ELFObjectFile<ELFT>::getLoadName() const {
841 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
842 Elf_Dyn_Iter DE = EF.end_dynamic_table();
844 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
848 return EF.getDynamicString(DI->getVal());
852 template <class ELFT>
853 library_iterator ELFObjectFile<ELFT>::needed_library_begin() 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)
860 return library_iterator(LibraryRef(toDRI(DI), this));
863 template <class ELFT>
864 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
865 LibraryRef &Result) const {
866 Elf_Dyn_Iter DI = toELFDynIter(Data);
867 Elf_Dyn_Iter DE = EF.end_dynamic_table();
869 // Skip to the next DT_NEEDED entry.
872 while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
874 Result = LibraryRef(toDRI(DI), this);
875 return object_error::success;
878 template <class ELFT>
879 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
880 StringRef &Res) const {
881 Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
882 return object_error::success;
885 template <class ELFT>
886 library_iterator ELFObjectFile<ELFT>::needed_library_end() const {
887 return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
890 template <class ELFT>
891 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
892 return ELFT::Is64Bits ? 8 : 4;
895 template <class ELFT>
896 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
897 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
898 case ELF::ELFCLASS32:
899 switch (EF.getHeader()->e_machine) {
903 return "ELF32-x86-64";
906 case ELF::EM_HEXAGON:
907 return "ELF32-hexagon";
913 case ELF::EM_SPARC32PLUS:
914 return "ELF32-sparc";
916 return "ELF32-unknown";
918 case ELF::ELFCLASS64:
919 switch (EF.getHeader()->e_machine) {
923 return "ELF64-x86-64";
924 case ELF::EM_AARCH64:
925 return "ELF64-aarch64";
927 return "ELF64-ppc64";
930 case ELF::EM_SPARCV9:
931 return "ELF64-sparc";
935 return "ELF64-unknown";
938 // FIXME: Proper error handling.
939 report_fatal_error("Invalid ELFCLASS!");
943 template <class ELFT>
944 unsigned ELFObjectFile<ELFT>::getArch() const {
945 switch (EF.getHeader()->e_machine) {
949 return Triple::x86_64;
950 case ELF::EM_AARCH64:
951 return Triple::aarch64;
954 case ELF::EM_HEXAGON:
955 return Triple::hexagon;
957 return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
960 return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
963 return Triple::systemz;
966 case ELF::EM_SPARC32PLUS:
967 return Triple::sparc;
968 case ELF::EM_SPARCV9:
969 return Triple::sparcv9;
972 return Triple::UnknownArch;
976 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
977 /// and make these member functions?
978 inline error_code getELFRelocationAddend(const RelocationRef R,
980 const ObjectFile *Obj = R.getObjectFile();
981 DataRefImpl DRI = R.getRawDataRefImpl();
982 // Little-endian 32-bit
983 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
984 return ELFObj->getRelocationAddend(DRI, Addend);
987 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
988 return ELFObj->getRelocationAddend(DRI, Addend);
990 // Little-endian 64-bit
991 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
992 return ELFObj->getRelocationAddend(DRI, Addend);
995 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
996 return ELFObj->getRelocationAddend(DRI, Addend);
998 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
1001 inline std::pair<symbol_iterator, symbol_iterator>
1002 getELFDynamicSymbolIterators(SymbolicFile *Obj) {
1003 if (const ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
1004 return std::make_pair(ELF->dynamic_symbol_begin(),
1005 ELF->dynamic_symbol_end());
1006 if (const ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
1007 return std::make_pair(ELF->dynamic_symbol_begin(),
1008 ELF->dynamic_symbol_end());
1009 if (const ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
1010 return std::make_pair(ELF->dynamic_symbol_begin(),
1011 ELF->dynamic_symbol_end());
1012 if (const ELF64BEObjectFile *ELF = cast<ELF64BEObjectFile>(Obj))
1013 return std::make_pair(ELF->dynamic_symbol_begin(),
1014 ELF->dynamic_symbol_end());
1017 "Object passed to getELFDynamicSymbolIterators() is not ELF");
1020 /// This is a generic interface for retrieving GNU symbol version
1021 /// information from an ELFObjectFile.
1022 inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
1023 const SymbolRef &Sym, StringRef &Version,
1025 // Little-endian 32-bit
1026 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1027 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1029 // Big-endian 32-bit
1030 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1031 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1033 // Little-endian 64-bit
1034 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1035 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1037 // Big-endian 64-bit
1038 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1039 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1041 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");