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 std::error_code getSymbolName(DataRefImpl Symb,
61 StringRef &Res) const override;
62 std::error_code getSymbolAddress(DataRefImpl Symb,
63 uint64_t &Res) const override;
64 std::error_code getSymbolAlignment(DataRefImpl Symb,
65 uint32_t &Res) const override;
66 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
67 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
68 std::error_code getSymbolType(DataRefImpl Symb,
69 SymbolRef::Type &Res) const override;
70 std::error_code getSymbolSection(DataRefImpl Symb,
71 section_iterator &Res) const override;
73 std::error_code getLibraryNext(DataRefImpl Data,
74 LibraryRef &Result) const override;
75 std::error_code getLibraryPath(DataRefImpl Data,
76 StringRef &Res) const override;
78 void moveSectionNext(DataRefImpl &Sec) const override;
79 std::error_code getSectionName(DataRefImpl Sec,
80 StringRef &Res) const override;
81 std::error_code getSectionAddress(DataRefImpl Sec,
82 uint64_t &Res) const override;
83 std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
84 std::error_code getSectionContents(DataRefImpl Sec,
85 StringRef &Res) const override;
86 std::error_code getSectionAlignment(DataRefImpl Sec,
87 uint64_t &Res) const override;
88 std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
89 std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
90 std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
91 std::error_code isSectionRequiredForExecution(DataRefImpl Sec,
92 bool &Res) const override;
93 std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
94 std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
95 std::error_code isSectionReadOnlyData(DataRefImpl Sec,
96 bool &Res) const override;
97 std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
98 bool &Result) const override;
99 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
100 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
101 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
103 void moveRelocationNext(DataRefImpl &Rel) const override;
104 std::error_code getRelocationAddress(DataRefImpl Rel,
105 uint64_t &Res) const override;
106 std::error_code getRelocationOffset(DataRefImpl Rel,
107 uint64_t &Res) const override;
108 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
109 std::error_code getRelocationType(DataRefImpl Rel,
110 uint64_t &Res) const override;
112 getRelocationTypeName(DataRefImpl Rel,
113 SmallVectorImpl<char> &Result) const override;
115 getRelocationValueString(DataRefImpl Rel,
116 SmallVectorImpl<char> &Result) const override;
118 uint64_t getROffset(DataRefImpl Rel) const;
119 StringRef getRelocationTypeName(uint32_t Type) const;
121 /// \brief Get the relocation section that contains \a Rel.
122 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
123 return EF.getSection(Rel.d.a);
126 const Elf_Rel *getRel(DataRefImpl Rel) const;
127 const Elf_Rela *getRela(DataRefImpl Rela) const;
129 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
130 bool IsDynamic = Symb.p & 1;
133 EF.begin_dynamic_symbols().getEntSize(),
134 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
135 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
136 reinterpret_cast<const char *>(Symb.p), IsDynamic);
139 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
141 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
142 static_cast<uintptr_t>(Symb.isDynamic());
146 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
147 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
148 reinterpret_cast<const char *>(Sec.p));
151 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
153 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
157 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
159 DRI.p = reinterpret_cast<uintptr_t>(Sec);
163 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
164 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
165 reinterpret_cast<const char *>(Dyn.p));
168 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
170 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
174 // This flag is used for classof, to distinguish ELFObjectFile from
175 // its subclass. If more subclasses will be created, this flag will
176 // have to become an enum.
177 bool isDyldELFObject;
180 ELFObjectFile(std::unique_ptr<MemoryBuffer> Object, std::error_code &EC);
182 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
184 basic_symbol_iterator symbol_begin_impl() const override;
185 basic_symbol_iterator symbol_end_impl() const override;
187 symbol_iterator dynamic_symbol_begin() const;
188 symbol_iterator dynamic_symbol_end() const;
190 section_iterator section_begin() const override;
191 section_iterator section_end() const override;
193 library_iterator needed_library_begin() const override;
194 library_iterator needed_library_end() const override;
196 std::error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
197 std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
198 bool &IsDefault) const;
200 uint8_t getBytesInAddress() const override;
201 StringRef getFileFormatName() const override;
202 unsigned getArch() const override;
203 StringRef getLoadName() const 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 std::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 std::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 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
252 uint64_t &Result) const {
253 const Elf_Sym *ESym = getSymbol(Symb);
254 switch (EF.getSymbolTableIndex(ESym)) {
255 case ELF::SHN_COMMON:
257 Result = UnknownAddressOrSize;
258 return object_error::success;
260 Result = ESym->st_value;
261 return object_error::success;
266 const Elf_Ehdr *Header = EF.getHeader();
267 Result = ESym->st_value;
269 // Clear the ARM/Thumb indicator flag.
270 if (Header->e_machine == ELF::EM_ARM && ESym->getType() == ELF::STT_FUNC)
273 if (Header->e_type == ELF::ET_REL)
274 Result += EF.getSection(ESym)->sh_addr;
276 return object_error::success;
279 template <class ELFT>
280 std::error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
281 uint32_t &Res) const {
282 Elf_Sym_Iter Sym = toELFSymIter(Symb);
283 if (Sym->st_shndx == ELF::SHN_COMMON)
287 return object_error::success;
290 template <class ELFT>
291 std::error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
292 uint64_t &Result) const {
293 Result = toELFSymIter(Symb)->st_size;
294 return object_error::success;
297 template <class ELFT>
299 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
300 SymbolRef::Type &Result) const {
301 const Elf_Sym *ESym = getSymbol(Symb);
303 switch (ESym->getType()) {
304 case ELF::STT_NOTYPE:
305 Result = SymbolRef::ST_Unknown;
307 case ELF::STT_SECTION:
308 Result = SymbolRef::ST_Debug;
311 Result = SymbolRef::ST_File;
314 Result = SymbolRef::ST_Function;
316 case ELF::STT_OBJECT:
317 case ELF::STT_COMMON:
319 Result = SymbolRef::ST_Data;
322 Result = SymbolRef::ST_Other;
325 return object_error::success;
328 template <class ELFT>
329 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
330 Elf_Sym_Iter EIter = toELFSymIter(Symb);
331 const Elf_Sym *ESym = &*EIter;
333 uint32_t Result = SymbolRef::SF_None;
335 if (ESym->getBinding() != ELF::STB_LOCAL)
336 Result |= SymbolRef::SF_Global;
338 if (ESym->getBinding() == ELF::STB_WEAK)
339 Result |= SymbolRef::SF_Weak;
341 if (ESym->st_shndx == ELF::SHN_ABS)
342 Result |= SymbolRef::SF_Absolute;
344 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
345 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
346 Result |= SymbolRef::SF_FormatSpecific;
348 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
349 Result |= SymbolRef::SF_Undefined;
351 if (ESym->getType() == ELF::STT_COMMON ||
352 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
353 Result |= SymbolRef::SF_Common;
358 template <class ELFT>
360 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
361 section_iterator &Res) const {
362 const Elf_Sym *ESym = getSymbol(Symb);
363 const Elf_Shdr *ESec = EF.getSection(ESym);
368 Sec.p = reinterpret_cast<intptr_t>(ESec);
369 Res = section_iterator(SectionRef(Sec, this));
371 return object_error::success;
374 template <class ELFT>
375 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
376 Sec = toDRI(++toELFShdrIter(Sec));
379 template <class ELFT>
380 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
381 StringRef &Result) const {
382 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
384 return Name.getError();
386 return object_error::success;
389 template <class ELFT>
390 std::error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
391 uint64_t &Result) const {
392 Result = toELFShdrIter(Sec)->sh_addr;
393 return object_error::success;
396 template <class ELFT>
397 std::error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
398 uint64_t &Result) const {
399 Result = toELFShdrIter(Sec)->sh_size;
400 return object_error::success;
403 template <class ELFT>
405 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
406 StringRef &Result) const {
407 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
408 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
409 return object_error::success;
412 template <class ELFT>
414 ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
415 uint64_t &Result) const {
416 Result = toELFShdrIter(Sec)->sh_addralign;
417 return object_error::success;
420 template <class ELFT>
421 std::error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
422 bool &Result) const {
423 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
424 return object_error::success;
427 template <class ELFT>
428 std::error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
429 bool &Result) const {
430 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
431 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
432 EShdr->sh_type == ELF::SHT_PROGBITS;
433 return object_error::success;
436 template <class ELFT>
437 std::error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
438 bool &Result) const {
439 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
440 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
441 EShdr->sh_type == ELF::SHT_NOBITS;
442 return object_error::success;
445 template <class ELFT>
447 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
448 bool &Result) const {
449 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
450 return object_error::success;
453 template <class ELFT>
454 std::error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
455 bool &Result) const {
456 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
457 return object_error::success;
460 template <class ELFT>
461 std::error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
462 bool &Result) const {
463 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
464 return object_error::success;
467 template <class ELFT>
468 std::error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
469 bool &Result) const {
470 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
471 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
472 return object_error::success;
475 template <class ELFT>
476 std::error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
478 bool &Result) const {
479 Elf_Sym_Iter ESym = toELFSymIter(Symb);
481 uintX_t Index = ESym->st_shndx;
482 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
484 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
485 return object_error::success;
488 template <class ELFT>
490 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
492 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
493 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
495 return relocation_iterator(RelocationRef(RelData, this));
498 template <class ELFT>
500 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
502 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
503 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
504 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
505 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
508 RelData.d.b = S->sh_size / S->sh_entsize;
510 return relocation_iterator(RelocationRef(RelData, this));
513 template <class ELFT>
515 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
516 if (EF.getHeader()->e_type != ELF::ET_REL)
517 return section_end();
519 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
520 uintX_t Type = EShdr->sh_type;
521 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
522 return section_end();
524 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
525 return section_iterator(SectionRef(toDRI(R), this));
529 template <class ELFT>
530 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
534 template <class ELFT>
536 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
538 const Elf_Shdr *sec = getRelSection(Rel);
539 switch (sec->sh_type) {
541 report_fatal_error("Invalid section type in Rel!");
543 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
546 case ELF::SHT_RELA: {
547 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
554 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
556 DataRefImpl SymbolData;
557 switch (SymSec->sh_type) {
559 report_fatal_error("Invalid symbol table section type!");
560 case ELF::SHT_SYMTAB:
561 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
563 case ELF::SHT_DYNSYM:
564 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
568 return symbol_iterator(SymbolRef(SymbolData, this));
571 template <class ELFT>
573 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
574 uint64_t &Result) const {
575 uint64_t ROffset = getROffset(Rel);
576 const Elf_Ehdr *Header = EF.getHeader();
578 if (Header->e_type == ELF::ET_REL) {
579 const Elf_Shdr *RelocationSec = getRelSection(Rel);
580 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
581 Result = ROffset + RelocatedSec->sh_addr;
586 return object_error::success;
589 template <class ELFT>
591 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
592 uint64_t &Result) const {
593 assert(EF.getHeader()->e_type == ELF::ET_REL &&
594 "Only relocatable object files have relocation offsets");
595 Result = getROffset(Rel);
596 return object_error::success;
599 template <class ELFT>
600 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
601 const Elf_Shdr *sec = getRelSection(Rel);
602 switch (sec->sh_type) {
604 report_fatal_error("Invalid section type in Rel!");
606 return getRel(Rel)->r_offset;
608 return getRela(Rel)->r_offset;
612 template <class ELFT>
613 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
614 uint64_t &Result) const {
615 const Elf_Shdr *sec = getRelSection(Rel);
616 switch (sec->sh_type) {
618 report_fatal_error("Invalid section type in Rel!");
620 Result = getRel(Rel)->getType(EF.isMips64EL());
623 case ELF::SHT_RELA: {
624 Result = getRela(Rel)->getType(EF.isMips64EL());
628 return object_error::success;
631 template <class ELFT>
632 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
633 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
636 template <class ELFT>
637 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
638 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
639 const Elf_Shdr *sec = getRelSection(Rel);
641 switch (sec->sh_type) {
643 return object_error::parse_failed;
645 type = getRel(Rel)->getType(EF.isMips64EL());
648 case ELF::SHT_RELA: {
649 type = getRela(Rel)->getType(EF.isMips64EL());
654 EF.getRelocationTypeName(type, Result);
655 return object_error::success;
658 template <class ELFT>
660 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
661 int64_t &Result) const {
662 const Elf_Shdr *sec = getRelSection(Rel);
663 switch (sec->sh_type) {
665 report_fatal_error("Invalid section type in Rel!");
668 return object_error::success;
670 case ELF::SHT_RELA: {
671 Result = getRela(Rel)->r_addend;
672 return object_error::success;
677 template <class ELFT>
678 std::error_code ELFObjectFile<ELFT>::getRelocationValueString(
679 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
680 const Elf_Shdr *sec = getRelSection(Rel);
684 uint16_t symbol_index = 0;
685 switch (sec->sh_type) {
687 return object_error::parse_failed;
689 type = getRel(Rel)->getType(EF.isMips64EL());
690 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
691 // TODO: Read implicit addend from section data.
694 case ELF::SHT_RELA: {
695 type = getRela(Rel)->getType(EF.isMips64EL());
696 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
697 addend = getRela(Rel)->r_addend;
701 const Elf_Sym *symb =
702 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
703 ErrorOr<StringRef> SymName =
704 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
706 return SymName.getError();
707 switch (EF.getHeader()->e_machine) {
710 case ELF::R_X86_64_PC8:
711 case ELF::R_X86_64_PC16:
712 case ELF::R_X86_64_PC32: {
714 raw_string_ostream fmt(fmtbuf);
715 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
717 Result.append(fmtbuf.begin(), fmtbuf.end());
719 case ELF::R_X86_64_8:
720 case ELF::R_X86_64_16:
721 case ELF::R_X86_64_32:
722 case ELF::R_X86_64_32S:
723 case ELF::R_X86_64_64: {
725 raw_string_ostream fmt(fmtbuf);
726 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
728 Result.append(fmtbuf.begin(), fmtbuf.end());
734 case ELF::EM_AARCH64: {
736 raw_string_ostream fmt(fmtbuf);
739 fmt << (addend < 0 ? "" : "+") << addend;
741 Result.append(fmtbuf.begin(), fmtbuf.end());
745 case ELF::EM_HEXAGON:
753 Result.append(res.begin(), res.end());
754 return object_error::success;
757 template <class ELFT>
758 const typename ELFFile<ELFT>::Elf_Sym *
759 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
760 return &*toELFSymIter(Symb);
763 template <class ELFT>
764 const typename ELFObjectFile<ELFT>::Elf_Rel *
765 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
766 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
769 template <class ELFT>
770 const typename ELFObjectFile<ELFT>::Elf_Rela *
771 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
772 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
775 template <class ELFT>
776 ELFObjectFile<ELFT>::ELFObjectFile(std::unique_ptr<MemoryBuffer> Object,
778 : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
782 EF(Data.get(), EC) {}
784 template <class ELFT>
785 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
786 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
789 template <class ELFT>
790 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
791 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
794 template <class ELFT>
795 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
796 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
799 template <class ELFT>
800 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
801 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
804 template <class ELFT>
805 section_iterator ELFObjectFile<ELFT>::section_begin() const {
806 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
809 template <class ELFT>
810 section_iterator ELFObjectFile<ELFT>::section_end() const {
811 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
814 template <class ELFT>
815 StringRef ELFObjectFile<ELFT>::getLoadName() const {
816 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
817 Elf_Dyn_Iter DE = EF.end_dynamic_table();
819 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
823 return EF.getDynamicString(DI->getVal());
827 template <class ELFT>
828 library_iterator ELFObjectFile<ELFT>::needed_library_begin() const {
829 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
830 Elf_Dyn_Iter DE = EF.end_dynamic_table();
832 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
835 return library_iterator(LibraryRef(toDRI(DI), this));
838 template <class ELFT>
839 std::error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
840 LibraryRef &Result) const {
841 Elf_Dyn_Iter DI = toELFDynIter(Data);
842 Elf_Dyn_Iter DE = EF.end_dynamic_table();
844 // Skip to the next DT_NEEDED entry.
847 while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
849 Result = LibraryRef(toDRI(DI), this);
850 return object_error::success;
853 template <class ELFT>
854 std::error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
855 StringRef &Res) const {
856 Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
857 return object_error::success;
860 template <class ELFT>
861 library_iterator ELFObjectFile<ELFT>::needed_library_end() const {
862 return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
865 template <class ELFT>
866 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
867 return ELFT::Is64Bits ? 8 : 4;
870 template <class ELFT>
871 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
872 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
873 case ELF::ELFCLASS32:
874 switch (EF.getHeader()->e_machine) {
878 return "ELF32-x86-64";
881 case ELF::EM_HEXAGON:
882 return "ELF32-hexagon";
888 case ELF::EM_SPARC32PLUS:
889 return "ELF32-sparc";
891 return "ELF32-unknown";
893 case ELF::ELFCLASS64:
894 switch (EF.getHeader()->e_machine) {
898 return "ELF64-x86-64";
899 case ELF::EM_AARCH64:
900 return "ELF64-aarch64";
902 return "ELF64-ppc64";
905 case ELF::EM_SPARCV9:
906 return "ELF64-sparc";
910 return "ELF64-unknown";
913 // FIXME: Proper error handling.
914 report_fatal_error("Invalid ELFCLASS!");
918 template <class ELFT>
919 unsigned ELFObjectFile<ELFT>::getArch() const {
920 switch (EF.getHeader()->e_machine) {
924 return Triple::x86_64;
925 case ELF::EM_AARCH64:
926 return Triple::aarch64;
929 case ELF::EM_HEXAGON:
930 return Triple::hexagon;
932 return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
935 return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
938 return Triple::systemz;
941 case ELF::EM_SPARC32PLUS:
942 return Triple::sparc;
943 case ELF::EM_SPARCV9:
944 return Triple::sparcv9;
947 return Triple::UnknownArch;
951 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
952 /// and make these member functions?
953 inline std::error_code getELFRelocationAddend(const RelocationRef R,
955 const ObjectFile *Obj = R.getObjectFile();
956 DataRefImpl DRI = R.getRawDataRefImpl();
957 // Little-endian 32-bit
958 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
959 return ELFObj->getRelocationAddend(DRI, Addend);
962 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
963 return ELFObj->getRelocationAddend(DRI, Addend);
965 // Little-endian 64-bit
966 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
967 return ELFObj->getRelocationAddend(DRI, Addend);
970 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
971 return ELFObj->getRelocationAddend(DRI, Addend);
973 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
976 inline std::pair<symbol_iterator, symbol_iterator>
977 getELFDynamicSymbolIterators(SymbolicFile *Obj) {
978 if (const ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
979 return std::make_pair(ELF->dynamic_symbol_begin(),
980 ELF->dynamic_symbol_end());
981 if (const ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
982 return std::make_pair(ELF->dynamic_symbol_begin(),
983 ELF->dynamic_symbol_end());
984 if (const ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
985 return std::make_pair(ELF->dynamic_symbol_begin(),
986 ELF->dynamic_symbol_end());
987 if (const ELF64BEObjectFile *ELF = cast<ELF64BEObjectFile>(Obj))
988 return std::make_pair(ELF->dynamic_symbol_begin(),
989 ELF->dynamic_symbol_end());
992 "Object passed to getELFDynamicSymbolIterators() is not ELF");
995 /// This is a generic interface for retrieving GNU symbol version
996 /// information from an ELFObjectFile.
997 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
998 const SymbolRef &Sym,
1001 // Little-endian 32-bit
1002 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1003 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1005 // Big-endian 32-bit
1006 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1007 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1009 // Little-endian 64-bit
1010 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1011 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1013 // Big-endian 64-bit
1014 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1015 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1017 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");