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;
72 error_code getLibraryNext(DataRefImpl Data,
73 LibraryRef &Result) const override;
74 error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const override;
76 void moveSectionNext(DataRefImpl &Sec) const override;
77 error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override;
78 error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const override;
79 error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
80 error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override;
81 error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const override;
82 error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
83 error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
84 error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
85 error_code isSectionRequiredForExecution(DataRefImpl Sec,
86 bool &Res) const override;
87 error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
88 error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
89 error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const override;
90 error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
91 bool &Result) const override;
92 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
93 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
94 bool section_rel_empty(DataRefImpl Sec) const override;
95 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
97 void moveRelocationNext(DataRefImpl &Rel) const override;
98 error_code getRelocationAddress(DataRefImpl Rel,
99 uint64_t &Res) const override;
100 error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const override;
101 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
102 error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const override;
103 error_code getRelocationTypeName(DataRefImpl Rel,
104 SmallVectorImpl<char> &Result) const override;
105 error_code getRelocationValueString(DataRefImpl Rel,
106 SmallVectorImpl<char> &Result) const override;
108 uint64_t getROffset(DataRefImpl Rel) const;
109 StringRef getRelocationTypeName(uint32_t Type) const;
111 /// \brief Get the relocation section that contains \a Rel.
112 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
113 return EF.getSection(Rel.d.a);
116 const Elf_Rel *getRel(DataRefImpl Rel) const;
117 const Elf_Rela *getRela(DataRefImpl Rela) const;
119 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
120 bool IsDynamic = Symb.p & 1;
123 EF.begin_dynamic_symbols().getEntSize(),
124 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
125 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
126 reinterpret_cast<const char *>(Symb.p), IsDynamic);
129 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
131 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
132 static_cast<uintptr_t>(Symb.isDynamic());
136 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
137 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
138 reinterpret_cast<const char *>(Sec.p));
141 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
143 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
147 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
149 DRI.p = reinterpret_cast<uintptr_t>(Sec);
153 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
154 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
155 reinterpret_cast<const char *>(Dyn.p));
158 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
160 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
164 // This flag is used for classof, to distinguish ELFObjectFile from
165 // its subclass. If more subclasses will be created, this flag will
166 // have to become an enum.
167 bool isDyldELFObject;
170 ELFObjectFile(MemoryBuffer *Object, error_code &EC, bool BufferOwned = true);
172 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
174 basic_symbol_iterator symbol_begin_impl() const override;
175 basic_symbol_iterator symbol_end_impl() const override;
177 symbol_iterator dynamic_symbol_begin() const;
178 symbol_iterator dynamic_symbol_end() const;
180 section_iterator section_begin() const override;
181 section_iterator section_end() const override;
183 library_iterator needed_library_begin() const override;
184 library_iterator needed_library_end() const override;
186 error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
187 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
188 bool &IsDefault) const;
191 uint8_t getBytesInAddress() const override;
192 StringRef getFileFormatName() const override;
193 unsigned getArch() const override;
194 StringRef getLoadName() const override;
196 const ELFFile<ELFT> *getELFFile() const { return &EF; }
198 bool isDyldType() const { return isDyldELFObject; }
199 static inline bool classof(const Binary *v) {
200 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
205 // Use an alignment of 2 for the typedefs since that is the worst case for
206 // ELF files in archives.
207 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
208 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
209 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
210 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
212 template <class ELFT>
213 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
214 Symb = toDRI(++toELFSymIter(Symb));
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 switch (EF.getSymbolTableIndex(ESym)) {
279 case ELF::SHN_COMMON:
281 Result = UnknownAddressOrSize;
282 return object_error::success;
284 Result = ESym->st_value;
285 return object_error::success;
290 const Elf_Ehdr *Header = EF.getHeader();
291 Result = ESym->st_value;
293 // Clear the ARM/Thumb indicator flag.
294 if (EF.getHeader()->e_machine == ELF::EM_ARM &&
295 ESym->getType() == ELF::STT_FUNC)
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 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
399 Sec = toDRI(++toELFShdrIter(Sec));
402 template <class ELFT>
403 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
404 StringRef &Result) const {
405 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
407 return Name.getError();
409 return object_error::success;
412 template <class ELFT>
413 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
414 uint64_t &Result) const {
415 Result = toELFShdrIter(Sec)->sh_addr;
416 return object_error::success;
419 template <class ELFT>
420 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
421 uint64_t &Result) const {
422 Result = toELFShdrIter(Sec)->sh_size;
423 return object_error::success;
426 template <class ELFT>
427 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
428 StringRef &Result) const {
429 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
430 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
431 return object_error::success;
434 template <class ELFT>
435 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
436 uint64_t &Result) const {
437 Result = toELFShdrIter(Sec)->sh_addralign;
438 return object_error::success;
441 template <class ELFT>
442 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
443 bool &Result) const {
444 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
445 return object_error::success;
448 template <class ELFT>
449 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
450 bool &Result) const {
451 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
452 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
453 EShdr->sh_type == ELF::SHT_PROGBITS;
454 return object_error::success;
457 template <class ELFT>
458 error_code ELFObjectFile<ELFT>::isSectionBSS(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_NOBITS;
463 return object_error::success;
466 template <class ELFT>
468 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
469 bool &Result) const {
470 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
471 return object_error::success;
474 template <class ELFT>
475 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
476 bool &Result) const {
477 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
478 return object_error::success;
481 template <class ELFT>
482 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
483 bool &Result) const {
484 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
485 return object_error::success;
488 template <class ELFT>
489 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
490 bool &Result) const {
491 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
492 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
493 return object_error::success;
496 template <class ELFT>
497 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
499 bool &Result) const {
500 Elf_Sym_Iter ESym = toELFSymIter(Symb);
502 uintX_t Index = ESym->st_shndx;
503 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
505 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
506 return object_error::success;
509 template <class ELFT>
511 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
513 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
514 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
516 return relocation_iterator(RelocationRef(RelData, this));
519 template <class ELFT>
521 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
523 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
524 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
525 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
526 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
529 RelData.d.b = S->sh_size / S->sh_entsize;
531 return relocation_iterator(RelocationRef(RelData, this));
534 template <class ELFT>
535 bool ELFObjectFile<ELFT>::section_rel_empty(DataRefImpl Sec) const {
536 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
537 return S->sh_size == 0;
540 template <class ELFT>
542 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
543 if (EF.getHeader()->e_type != ELF::ET_REL)
544 return section_end();
546 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
547 uintX_t Type = EShdr->sh_type;
548 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
549 return section_end();
551 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
552 return section_iterator(SectionRef(toDRI(R), this));
556 template <class ELFT>
557 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
561 template <class ELFT>
563 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
565 const Elf_Shdr *sec = getRelSection(Rel);
566 switch (sec->sh_type) {
568 report_fatal_error("Invalid section type in Rel!");
570 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
573 case ELF::SHT_RELA: {
574 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
581 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
583 DataRefImpl SymbolData;
584 switch (SymSec->sh_type) {
586 report_fatal_error("Invalid symbol table section type!");
587 case ELF::SHT_SYMTAB:
588 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
590 case ELF::SHT_DYNSYM:
591 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
595 return symbol_iterator(SymbolRef(SymbolData, this));
598 template <class ELFT>
599 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
600 uint64_t &Result) const {
601 assert((EF.getHeader()->e_type == ELF::ET_EXEC ||
602 EF.getHeader()->e_type == ELF::ET_DYN) &&
603 "Only executable and shared objects files have relocation addresses");
604 Result = getROffset(Rel);
605 return object_error::success;
608 template <class ELFT>
609 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
610 uint64_t &Result) const {
611 assert(EF.getHeader()->e_type == ELF::ET_REL &&
612 "Only relocatable object files have relocation offsets");
613 Result = getROffset(Rel);
614 return object_error::success;
617 template <class ELFT>
618 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
619 const Elf_Shdr *sec = getRelSection(Rel);
620 switch (sec->sh_type) {
622 report_fatal_error("Invalid section type in Rel!");
624 return getRel(Rel)->r_offset;
626 return getRela(Rel)->r_offset;
630 template <class ELFT>
631 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
632 uint64_t &Result) const {
633 const Elf_Shdr *sec = getRelSection(Rel);
634 switch (sec->sh_type) {
636 report_fatal_error("Invalid section type in Rel!");
638 Result = getRel(Rel)->getType(EF.isMips64EL());
641 case ELF::SHT_RELA: {
642 Result = getRela(Rel)->getType(EF.isMips64EL());
646 return object_error::success;
649 template <class ELFT>
650 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
651 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
654 template <class ELFT>
655 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
656 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
657 const Elf_Shdr *sec = getRelSection(Rel);
659 switch (sec->sh_type) {
661 return object_error::parse_failed;
663 type = getRel(Rel)->getType(EF.isMips64EL());
666 case ELF::SHT_RELA: {
667 type = getRela(Rel)->getType(EF.isMips64EL());
672 EF.getRelocationTypeName(type, Result);
673 return object_error::success;
676 template <class ELFT>
677 error_code ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
678 int64_t &Result) const {
679 const Elf_Shdr *sec = getRelSection(Rel);
680 switch (sec->sh_type) {
682 report_fatal_error("Invalid section type in Rel!");
685 return object_error::success;
687 case ELF::SHT_RELA: {
688 Result = getRela(Rel)->r_addend;
689 return object_error::success;
694 template <class ELFT>
695 error_code ELFObjectFile<ELFT>::getRelocationValueString(
696 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
697 const Elf_Shdr *sec = getRelSection(Rel);
701 uint16_t symbol_index = 0;
702 switch (sec->sh_type) {
704 return object_error::parse_failed;
706 type = getRel(Rel)->getType(EF.isMips64EL());
707 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
708 // TODO: Read implicit addend from section data.
711 case ELF::SHT_RELA: {
712 type = getRela(Rel)->getType(EF.isMips64EL());
713 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
714 addend = getRela(Rel)->r_addend;
718 const Elf_Sym *symb =
719 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
720 ErrorOr<StringRef> SymName =
721 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
723 return SymName.getError();
724 switch (EF.getHeader()->e_machine) {
727 case ELF::R_X86_64_PC8:
728 case ELF::R_X86_64_PC16:
729 case ELF::R_X86_64_PC32: {
731 raw_string_ostream fmt(fmtbuf);
732 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
734 Result.append(fmtbuf.begin(), fmtbuf.end());
736 case ELF::R_X86_64_8:
737 case ELF::R_X86_64_16:
738 case ELF::R_X86_64_32:
739 case ELF::R_X86_64_32S:
740 case ELF::R_X86_64_64: {
742 raw_string_ostream fmt(fmtbuf);
743 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
745 Result.append(fmtbuf.begin(), fmtbuf.end());
751 case ELF::EM_AARCH64: {
753 raw_string_ostream fmt(fmtbuf);
756 fmt << (addend < 0 ? "" : "+") << addend;
758 Result.append(fmtbuf.begin(), fmtbuf.end());
762 case ELF::EM_HEXAGON:
770 Result.append(res.begin(), res.end());
771 return object_error::success;
774 template <class ELFT>
775 const typename ELFFile<ELFT>::Elf_Sym *
776 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
777 return &*toELFSymIter(Symb);
780 template <class ELFT>
781 const typename ELFObjectFile<ELFT>::Elf_Rel *
782 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
783 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
786 template <class ELFT>
787 const typename ELFObjectFile<ELFT>::Elf_Rela *
788 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
789 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
792 template <class ELFT>
793 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec,
795 : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
798 Object, BufferOwned),
801 template <class ELFT>
802 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
803 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
806 template <class ELFT>
807 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
808 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
811 template <class ELFT>
812 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
813 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
816 template <class ELFT>
817 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
818 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
821 template <class ELFT>
822 section_iterator ELFObjectFile<ELFT>::section_begin() const {
823 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
826 template <class ELFT>
827 section_iterator ELFObjectFile<ELFT>::section_end() const {
828 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
831 template <class ELFT>
832 StringRef ELFObjectFile<ELFT>::getLoadName() const {
833 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
834 Elf_Dyn_Iter DE = EF.end_dynamic_table();
836 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
840 return EF.getDynamicString(DI->getVal());
844 template <class ELFT>
845 library_iterator ELFObjectFile<ELFT>::needed_library_begin() const {
846 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
847 Elf_Dyn_Iter DE = EF.end_dynamic_table();
849 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
852 return library_iterator(LibraryRef(toDRI(DI), this));
855 template <class ELFT>
856 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
857 LibraryRef &Result) const {
858 Elf_Dyn_Iter DI = toELFDynIter(Data);
859 Elf_Dyn_Iter DE = EF.end_dynamic_table();
861 // Skip to the next DT_NEEDED entry.
864 while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
866 Result = LibraryRef(toDRI(DI), this);
867 return object_error::success;
870 template <class ELFT>
871 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
872 StringRef &Res) const {
873 Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
874 return object_error::success;
877 template <class ELFT>
878 library_iterator ELFObjectFile<ELFT>::needed_library_end() const {
879 return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
882 template <class ELFT>
883 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
884 return ELFT::Is64Bits ? 8 : 4;
887 template <class ELFT>
888 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
889 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
890 case ELF::ELFCLASS32:
891 switch (EF.getHeader()->e_machine) {
895 return "ELF32-x86-64";
898 case ELF::EM_HEXAGON:
899 return "ELF32-hexagon";
905 case ELF::EM_SPARC32PLUS:
906 return "ELF32-sparc";
908 return "ELF32-unknown";
910 case ELF::ELFCLASS64:
911 switch (EF.getHeader()->e_machine) {
915 return "ELF64-x86-64";
916 case ELF::EM_AARCH64:
917 return "ELF64-aarch64";
919 return "ELF64-ppc64";
922 case ELF::EM_SPARCV9:
923 return "ELF64-sparc";
927 return "ELF64-unknown";
930 // FIXME: Proper error handling.
931 report_fatal_error("Invalid ELFCLASS!");
935 template <class ELFT>
936 unsigned ELFObjectFile<ELFT>::getArch() const {
937 switch (EF.getHeader()->e_machine) {
941 return Triple::x86_64;
942 case ELF::EM_AARCH64:
943 return Triple::aarch64;
946 case ELF::EM_HEXAGON:
947 return Triple::hexagon;
949 return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
952 return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
955 return Triple::systemz;
958 case ELF::EM_SPARC32PLUS:
959 return Triple::sparc;
960 case ELF::EM_SPARCV9:
961 return Triple::sparcv9;
964 return Triple::UnknownArch;
968 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
969 /// and make these member functions?
970 inline error_code getELFRelocationAddend(const RelocationRef R,
972 const ObjectFile *Obj = R.getObjectFile();
973 DataRefImpl DRI = R.getRawDataRefImpl();
974 // Little-endian 32-bit
975 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
976 return ELFObj->getRelocationAddend(DRI, Addend);
979 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
980 return ELFObj->getRelocationAddend(DRI, Addend);
982 // Little-endian 64-bit
983 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
984 return ELFObj->getRelocationAddend(DRI, Addend);
987 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
988 return ELFObj->getRelocationAddend(DRI, Addend);
990 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
993 inline std::pair<symbol_iterator, symbol_iterator>
994 getELFDynamicSymbolIterators(SymbolicFile *Obj) {
995 if (const ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
996 return std::make_pair(ELF->dynamic_symbol_begin(),
997 ELF->dynamic_symbol_end());
998 if (const ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
999 return std::make_pair(ELF->dynamic_symbol_begin(),
1000 ELF->dynamic_symbol_end());
1001 if (const ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
1002 return std::make_pair(ELF->dynamic_symbol_begin(),
1003 ELF->dynamic_symbol_end());
1004 if (const ELF64BEObjectFile *ELF = cast<ELF64BEObjectFile>(Obj))
1005 return std::make_pair(ELF->dynamic_symbol_begin(),
1006 ELF->dynamic_symbol_end());
1009 "Object passed to getELFDynamicSymbolIterators() is not ELF");
1012 /// This is a generic interface for retrieving GNU symbol version
1013 /// information from an ELFObjectFile.
1014 inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
1015 const SymbolRef &Sym, StringRef &Version,
1017 // Little-endian 32-bit
1018 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1019 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1021 // Big-endian 32-bit
1022 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1023 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1025 // Little-endian 64-bit
1026 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1027 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1029 // Big-endian 64-bit
1030 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1031 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1033 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");