1 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the ELFObjectFile template class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_H
16
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/BinaryFormat/ELF.h"
24 #include "llvm/MC/SubtargetFeature.h"
25 #include "llvm/Object/Binary.h"
26 #include "llvm/Object/ELF.h"
27 #include "llvm/Object/ELFTypes.h"
28 #include "llvm/Object/Error.h"
29 #include "llvm/Object/ObjectFile.h"
30 #include "llvm/Object/SymbolicFile.h"
31 #include "llvm/Support/ARMAttributeParser.h"
32 #include "llvm/Support/ARMBuildAttributes.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/Error.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include <cassert>
39 #include <cstdint>
40 #include <system_error>
41
42 namespace llvm {
43 namespace object {
44
45 class elf_symbol_iterator;
46
47 class ELFObjectFileBase : public ObjectFile {
48 friend class ELFRelocationRef;
49 friend class ELFSectionRef;
50 friend class ELFSymbolRef;
51
52 protected:
53 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
54
55 virtual uint16_t getEMachine() const = 0;
56 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
57 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
58 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
59
60 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
61 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
62 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
63
64 virtual Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
65
66 public:
67 using elf_symbol_iterator_range = iterator_range<elf_symbol_iterator>;
68
69 virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
70
71 /// Returns platform-specific object flags, if any.
72 virtual unsigned getPlatformFlags() const = 0;
73
74 elf_symbol_iterator_range symbols() const;
75
classof(const Binary * v)76 static bool classof(const Binary *v) { return v->isELF(); }
77
78 SubtargetFeatures getFeatures() const override;
79
80 SubtargetFeatures getMIPSFeatures() const;
81
82 SubtargetFeatures getARMFeatures() const;
83
84 SubtargetFeatures getRISCVFeatures() const;
85
86 void setARMSubArch(Triple &TheTriple) const override;
87
88 virtual uint16_t getEType() const = 0;
89
90 std::vector<std::pair<DataRefImpl, uint64_t>> getPltAddresses() const;
91 };
92
93 class ELFSectionRef : public SectionRef {
94 public:
ELFSectionRef(const SectionRef & B)95 ELFSectionRef(const SectionRef &B) : SectionRef(B) {
96 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
97 }
98
getObject()99 const ELFObjectFileBase *getObject() const {
100 return cast<ELFObjectFileBase>(SectionRef::getObject());
101 }
102
getType()103 uint32_t getType() const {
104 return getObject()->getSectionType(getRawDataRefImpl());
105 }
106
getFlags()107 uint64_t getFlags() const {
108 return getObject()->getSectionFlags(getRawDataRefImpl());
109 }
110
getOffset()111 uint64_t getOffset() const {
112 return getObject()->getSectionOffset(getRawDataRefImpl());
113 }
114 };
115
116 class elf_section_iterator : public section_iterator {
117 public:
elf_section_iterator(const section_iterator & B)118 elf_section_iterator(const section_iterator &B) : section_iterator(B) {
119 assert(isa<ELFObjectFileBase>(B->getObject()));
120 }
121
122 const ELFSectionRef *operator->() const {
123 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
124 }
125
126 const ELFSectionRef &operator*() const {
127 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
128 }
129 };
130
131 class ELFSymbolRef : public SymbolRef {
132 public:
ELFSymbolRef(const SymbolRef & B)133 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
134 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
135 }
136
getObject()137 const ELFObjectFileBase *getObject() const {
138 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
139 }
140
getSize()141 uint64_t getSize() const {
142 return getObject()->getSymbolSize(getRawDataRefImpl());
143 }
144
getOther()145 uint8_t getOther() const {
146 return getObject()->getSymbolOther(getRawDataRefImpl());
147 }
148
getELFType()149 uint8_t getELFType() const {
150 return getObject()->getSymbolELFType(getRawDataRefImpl());
151 }
152 };
153
154 class elf_symbol_iterator : public symbol_iterator {
155 public:
elf_symbol_iterator(const basic_symbol_iterator & B)156 elf_symbol_iterator(const basic_symbol_iterator &B)
157 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
158 cast<ELFObjectFileBase>(B->getObject()))) {}
159
160 const ELFSymbolRef *operator->() const {
161 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
162 }
163
164 const ELFSymbolRef &operator*() const {
165 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
166 }
167 };
168
169 class ELFRelocationRef : public RelocationRef {
170 public:
ELFRelocationRef(const RelocationRef & B)171 ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) {
172 assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
173 }
174
getObject()175 const ELFObjectFileBase *getObject() const {
176 return cast<ELFObjectFileBase>(RelocationRef::getObject());
177 }
178
getAddend()179 Expected<int64_t> getAddend() const {
180 return getObject()->getRelocationAddend(getRawDataRefImpl());
181 }
182 };
183
184 class elf_relocation_iterator : public relocation_iterator {
185 public:
elf_relocation_iterator(const relocation_iterator & B)186 elf_relocation_iterator(const relocation_iterator &B)
187 : relocation_iterator(RelocationRef(
188 B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
189
190 const ELFRelocationRef *operator->() const {
191 return static_cast<const ELFRelocationRef *>(
192 relocation_iterator::operator->());
193 }
194
195 const ELFRelocationRef &operator*() const {
196 return static_cast<const ELFRelocationRef &>(
197 relocation_iterator::operator*());
198 }
199 };
200
201 inline ELFObjectFileBase::elf_symbol_iterator_range
symbols()202 ELFObjectFileBase::symbols() const {
203 return elf_symbol_iterator_range(symbol_begin(), symbol_end());
204 }
205
206 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
207 uint16_t getEMachine() const override;
208 uint16_t getEType() const override;
209 uint64_t getSymbolSize(DataRefImpl Sym) const override;
210
211 public:
212 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
213
214 using uintX_t = typename ELFT::uint;
215
216 using Elf_Sym = typename ELFT::Sym;
217 using Elf_Shdr = typename ELFT::Shdr;
218 using Elf_Ehdr = typename ELFT::Ehdr;
219 using Elf_Rel = typename ELFT::Rel;
220 using Elf_Rela = typename ELFT::Rela;
221 using Elf_Dyn = typename ELFT::Dyn;
222
223 private:
224 ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
225 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
226 ArrayRef<Elf_Word> ShndxTable);
227
228 protected:
229 ELFFile<ELFT> EF;
230
231 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
232 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
233 ArrayRef<Elf_Word> ShndxTable;
234
235 void moveSymbolNext(DataRefImpl &Symb) const override;
236 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
237 Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
238 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
239 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
240 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
241 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
242 uint8_t getSymbolOther(DataRefImpl Symb) const override;
243 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
244 Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
245 Expected<section_iterator> getSymbolSection(const Elf_Sym *Symb,
246 const Elf_Shdr *SymTab) const;
247 Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
248
249 void moveSectionNext(DataRefImpl &Sec) const override;
250 std::error_code getSectionName(DataRefImpl Sec,
251 StringRef &Res) const override;
252 uint64_t getSectionAddress(DataRefImpl Sec) const override;
253 uint64_t getSectionIndex(DataRefImpl Sec) const override;
254 uint64_t getSectionSize(DataRefImpl Sec) const override;
255 std::error_code getSectionContents(DataRefImpl Sec,
256 StringRef &Res) const override;
257 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
258 bool isSectionCompressed(DataRefImpl Sec) const override;
259 bool isSectionText(DataRefImpl Sec) const override;
260 bool isSectionData(DataRefImpl Sec) const override;
261 bool isSectionBSS(DataRefImpl Sec) const override;
262 bool isSectionVirtual(DataRefImpl Sec) const override;
263 bool isBerkeleyText(DataRefImpl Sec) const override;
264 bool isBerkeleyData(DataRefImpl Sec) const override;
265 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
266 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
267 std::vector<SectionRef> dynamic_relocation_sections() const override;
268 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
269
270 void moveRelocationNext(DataRefImpl &Rel) const override;
271 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
272 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
273 uint64_t getRelocationType(DataRefImpl Rel) const override;
274 void getRelocationTypeName(DataRefImpl Rel,
275 SmallVectorImpl<char> &Result) const override;
276
277 uint32_t getSectionType(DataRefImpl Sec) const override;
278 uint64_t getSectionFlags(DataRefImpl Sec) const override;
279 uint64_t getSectionOffset(DataRefImpl Sec) const override;
280 StringRef getRelocationTypeName(uint32_t Type) const;
281
282 /// Get the relocation section that contains \a Rel.
getRelSection(DataRefImpl Rel)283 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
284 auto RelSecOrErr = EF.getSection(Rel.d.a);
285 if (!RelSecOrErr)
286 report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message());
287 return *RelSecOrErr;
288 }
289
toDRI(const Elf_Shdr * SymTable,unsigned SymbolNum)290 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
291 DataRefImpl DRI;
292 if (!SymTable) {
293 DRI.d.a = 0;
294 DRI.d.b = 0;
295 return DRI;
296 }
297 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
298 SymTable->sh_type == ELF::SHT_DYNSYM);
299
300 auto SectionsOrErr = EF.sections();
301 if (!SectionsOrErr) {
302 DRI.d.a = 0;
303 DRI.d.b = 0;
304 return DRI;
305 }
306 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
307 unsigned SymTableIndex =
308 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
309
310 DRI.d.a = SymTableIndex;
311 DRI.d.b = SymbolNum;
312 return DRI;
313 }
314
toELFShdrIter(DataRefImpl Sec)315 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
316 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
317 }
318
toDRI(const Elf_Shdr * Sec)319 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
320 DataRefImpl DRI;
321 DRI.p = reinterpret_cast<uintptr_t>(Sec);
322 return DRI;
323 }
324
toDRI(const Elf_Dyn * Dyn)325 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
326 DataRefImpl DRI;
327 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
328 return DRI;
329 }
330
isExportedToOtherDSO(const Elf_Sym * ESym)331 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
332 unsigned char Binding = ESym->getBinding();
333 unsigned char Visibility = ESym->getVisibility();
334
335 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
336 // visibility is either DEFAULT or PROTECTED. All other symbols are not
337 // exported.
338 return (
339 (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
340 Binding == ELF::STB_GNU_UNIQUE) &&
341 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
342 }
343
344 // This flag is used for classof, to distinguish ELFObjectFile from
345 // its subclass. If more subclasses will be created, this flag will
346 // have to become an enum.
347 bool isDyldELFObject;
348
349 public:
350 ELFObjectFile(ELFObjectFile<ELFT> &&Other);
351 static Expected<ELFObjectFile<ELFT>> create(MemoryBufferRef Object);
352
353 const Elf_Rel *getRel(DataRefImpl Rel) const;
354 const Elf_Rela *getRela(DataRefImpl Rela) const;
355
getSymbol(DataRefImpl Sym)356 const Elf_Sym *getSymbol(DataRefImpl Sym) const {
357 auto Ret = EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
358 if (!Ret)
359 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
360 return *Ret;
361 }
362
getSection(DataRefImpl Sec)363 const Elf_Shdr *getSection(DataRefImpl Sec) const {
364 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
365 }
366
367 basic_symbol_iterator symbol_begin() const override;
368 basic_symbol_iterator symbol_end() const override;
369
370 elf_symbol_iterator dynamic_symbol_begin() const;
371 elf_symbol_iterator dynamic_symbol_end() const;
372
373 section_iterator section_begin() const override;
374 section_iterator section_end() const override;
375
376 Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
377
378 uint8_t getBytesInAddress() const override;
379 StringRef getFileFormatName() const override;
380 Triple::ArchType getArch() const override;
381 Expected<uint64_t> getStartAddress() const override;
382
getPlatformFlags()383 unsigned getPlatformFlags() const override { return EF.getHeader()->e_flags; }
384
getBuildAttributes(ARMAttributeParser & Attributes)385 std::error_code getBuildAttributes(ARMAttributeParser &Attributes) const override {
386 auto SectionsOrErr = EF.sections();
387 if (!SectionsOrErr)
388 return errorToErrorCode(SectionsOrErr.takeError());
389
390 for (const Elf_Shdr &Sec : *SectionsOrErr) {
391 if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES) {
392 auto ErrorOrContents = EF.getSectionContents(&Sec);
393 if (!ErrorOrContents)
394 return errorToErrorCode(ErrorOrContents.takeError());
395
396 auto Contents = ErrorOrContents.get();
397 if (Contents[0] != ARMBuildAttrs::Format_Version || Contents.size() == 1)
398 return std::error_code();
399
400 Attributes.Parse(Contents, ELFT::TargetEndianness == support::little);
401 break;
402 }
403 }
404 return std::error_code();
405 }
406
getELFFile()407 const ELFFile<ELFT> *getELFFile() const { return &EF; }
408
isDyldType()409 bool isDyldType() const { return isDyldELFObject; }
classof(const Binary * v)410 static bool classof(const Binary *v) {
411 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
412 ELFT::Is64Bits);
413 }
414
415 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
416
417 bool isRelocatableObject() const override;
418 };
419
420 using ELF32LEObjectFile = ELFObjectFile<ELF32LE>;
421 using ELF64LEObjectFile = ELFObjectFile<ELF64LE>;
422 using ELF32BEObjectFile = ELFObjectFile<ELF32BE>;
423 using ELF64BEObjectFile = ELFObjectFile<ELF64BE>;
424
425 template <class ELFT>
moveSymbolNext(DataRefImpl & Sym)426 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
427 ++Sym.d.b;
428 }
429
430 template <class ELFT>
getSymbolName(DataRefImpl Sym)431 Expected<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
432 const Elf_Sym *ESym = getSymbol(Sym);
433 auto SymTabOrErr = EF.getSection(Sym.d.a);
434 if (!SymTabOrErr)
435 return SymTabOrErr.takeError();
436 const Elf_Shdr *SymTableSec = *SymTabOrErr;
437 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
438 if (!StrTabOrErr)
439 return StrTabOrErr.takeError();
440 const Elf_Shdr *StringTableSec = *StrTabOrErr;
441 auto SymStrTabOrErr = EF.getStringTable(StringTableSec);
442 if (!SymStrTabOrErr)
443 return SymStrTabOrErr.takeError();
444 return ESym->getName(*SymStrTabOrErr);
445 }
446
447 template <class ELFT>
getSectionFlags(DataRefImpl Sec)448 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
449 return getSection(Sec)->sh_flags;
450 }
451
452 template <class ELFT>
getSectionType(DataRefImpl Sec)453 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
454 return getSection(Sec)->sh_type;
455 }
456
457 template <class ELFT>
getSectionOffset(DataRefImpl Sec)458 uint64_t ELFObjectFile<ELFT>::getSectionOffset(DataRefImpl Sec) const {
459 return getSection(Sec)->sh_offset;
460 }
461
462 template <class ELFT>
getSymbolValueImpl(DataRefImpl Symb)463 uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
464 const Elf_Sym *ESym = getSymbol(Symb);
465 uint64_t Ret = ESym->st_value;
466 if (ESym->st_shndx == ELF::SHN_ABS)
467 return Ret;
468
469 const Elf_Ehdr *Header = EF.getHeader();
470 // Clear the ARM/Thumb or microMIPS indicator flag.
471 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
472 ESym->getType() == ELF::STT_FUNC)
473 Ret &= ~1;
474
475 return Ret;
476 }
477
478 template <class ELFT>
479 Expected<uint64_t>
getSymbolAddress(DataRefImpl Symb)480 ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
481 uint64_t Result = getSymbolValue(Symb);
482 const Elf_Sym *ESym = getSymbol(Symb);
483 switch (ESym->st_shndx) {
484 case ELF::SHN_COMMON:
485 case ELF::SHN_UNDEF:
486 case ELF::SHN_ABS:
487 return Result;
488 }
489
490 const Elf_Ehdr *Header = EF.getHeader();
491 auto SymTabOrErr = EF.getSection(Symb.d.a);
492 if (!SymTabOrErr)
493 return SymTabOrErr.takeError();
494 const Elf_Shdr *SymTab = *SymTabOrErr;
495
496 if (Header->e_type == ELF::ET_REL) {
497 auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
498 if (!SectionOrErr)
499 return SectionOrErr.takeError();
500 const Elf_Shdr *Section = *SectionOrErr;
501 if (Section)
502 Result += Section->sh_addr;
503 }
504
505 return Result;
506 }
507
508 template <class ELFT>
getSymbolAlignment(DataRefImpl Symb)509 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
510 const Elf_Sym *Sym = getSymbol(Symb);
511 if (Sym->st_shndx == ELF::SHN_COMMON)
512 return Sym->st_value;
513 return 0;
514 }
515
516 template <class ELFT>
getEMachine()517 uint16_t ELFObjectFile<ELFT>::getEMachine() const {
518 return EF.getHeader()->e_machine;
519 }
520
getEType()521 template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
522 return EF.getHeader()->e_type;
523 }
524
525 template <class ELFT>
getSymbolSize(DataRefImpl Sym)526 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
527 return getSymbol(Sym)->st_size;
528 }
529
530 template <class ELFT>
getCommonSymbolSizeImpl(DataRefImpl Symb)531 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
532 return getSymbol(Symb)->st_size;
533 }
534
535 template <class ELFT>
getSymbolOther(DataRefImpl Symb)536 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
537 return getSymbol(Symb)->st_other;
538 }
539
540 template <class ELFT>
getSymbolELFType(DataRefImpl Symb)541 uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
542 return getSymbol(Symb)->getType();
543 }
544
545 template <class ELFT>
546 Expected<SymbolRef::Type>
getSymbolType(DataRefImpl Symb)547 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
548 const Elf_Sym *ESym = getSymbol(Symb);
549
550 switch (ESym->getType()) {
551 case ELF::STT_NOTYPE:
552 return SymbolRef::ST_Unknown;
553 case ELF::STT_SECTION:
554 return SymbolRef::ST_Debug;
555 case ELF::STT_FILE:
556 return SymbolRef::ST_File;
557 case ELF::STT_FUNC:
558 return SymbolRef::ST_Function;
559 case ELF::STT_OBJECT:
560 case ELF::STT_COMMON:
561 case ELF::STT_TLS:
562 return SymbolRef::ST_Data;
563 default:
564 return SymbolRef::ST_Other;
565 }
566 }
567
568 template <class ELFT>
getSymbolFlags(DataRefImpl Sym)569 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
570 const Elf_Sym *ESym = getSymbol(Sym);
571
572 uint32_t Result = SymbolRef::SF_None;
573
574 if (ESym->getBinding() != ELF::STB_LOCAL)
575 Result |= SymbolRef::SF_Global;
576
577 if (ESym->getBinding() == ELF::STB_WEAK)
578 Result |= SymbolRef::SF_Weak;
579
580 if (ESym->st_shndx == ELF::SHN_ABS)
581 Result |= SymbolRef::SF_Absolute;
582
583 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
584 Result |= SymbolRef::SF_FormatSpecific;
585
586 auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
587 if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
588 Result |= SymbolRef::SF_FormatSpecific;
589 auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
590 if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
591 Result |= SymbolRef::SF_FormatSpecific;
592
593 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
594 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
595 StringRef Name = *NameOrErr;
596 if (Name.startswith("$d") || Name.startswith("$t") ||
597 Name.startswith("$a"))
598 Result |= SymbolRef::SF_FormatSpecific;
599 } else {
600 // TODO: Actually report errors helpfully.
601 consumeError(NameOrErr.takeError());
602 }
603 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
604 Result |= SymbolRef::SF_Thumb;
605 }
606
607 if (ESym->st_shndx == ELF::SHN_UNDEF)
608 Result |= SymbolRef::SF_Undefined;
609
610 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
611 Result |= SymbolRef::SF_Common;
612
613 if (isExportedToOtherDSO(ESym))
614 Result |= SymbolRef::SF_Exported;
615
616 if (ESym->getVisibility() == ELF::STV_HIDDEN)
617 Result |= SymbolRef::SF_Hidden;
618
619 return Result;
620 }
621
622 template <class ELFT>
623 Expected<section_iterator>
getSymbolSection(const Elf_Sym * ESym,const Elf_Shdr * SymTab)624 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym,
625 const Elf_Shdr *SymTab) const {
626 auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
627 if (!ESecOrErr)
628 return ESecOrErr.takeError();
629
630 const Elf_Shdr *ESec = *ESecOrErr;
631 if (!ESec)
632 return section_end();
633
634 DataRefImpl Sec;
635 Sec.p = reinterpret_cast<intptr_t>(ESec);
636 return section_iterator(SectionRef(Sec, this));
637 }
638
639 template <class ELFT>
640 Expected<section_iterator>
getSymbolSection(DataRefImpl Symb)641 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const {
642 const Elf_Sym *Sym = getSymbol(Symb);
643 auto SymTabOrErr = EF.getSection(Symb.d.a);
644 if (!SymTabOrErr)
645 return SymTabOrErr.takeError();
646 const Elf_Shdr *SymTab = *SymTabOrErr;
647 return getSymbolSection(Sym, SymTab);
648 }
649
650 template <class ELFT>
moveSectionNext(DataRefImpl & Sec)651 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
652 const Elf_Shdr *ESec = getSection(Sec);
653 Sec = toDRI(++ESec);
654 }
655
656 template <class ELFT>
getSectionName(DataRefImpl Sec,StringRef & Result)657 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
658 StringRef &Result) const {
659 auto Name = EF.getSectionName(&*getSection(Sec));
660 if (!Name)
661 return errorToErrorCode(Name.takeError());
662 Result = *Name;
663 return std::error_code();
664 }
665
666 template <class ELFT>
getSectionAddress(DataRefImpl Sec)667 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
668 return getSection(Sec)->sh_addr;
669 }
670
671 template <class ELFT>
getSectionIndex(DataRefImpl Sec)672 uint64_t ELFObjectFile<ELFT>::getSectionIndex(DataRefImpl Sec) const {
673 auto SectionsOrErr = EF.sections();
674 handleAllErrors(std::move(SectionsOrErr.takeError()),
675 [](const ErrorInfoBase &) {
676 llvm_unreachable("unable to get section index");
677 });
678 const Elf_Shdr *First = SectionsOrErr->begin();
679 return getSection(Sec) - First;
680 }
681
682 template <class ELFT>
getSectionSize(DataRefImpl Sec)683 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
684 return getSection(Sec)->sh_size;
685 }
686
687 template <class ELFT>
688 std::error_code
getSectionContents(DataRefImpl Sec,StringRef & Result)689 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
690 StringRef &Result) const {
691 const Elf_Shdr *EShdr = getSection(Sec);
692 if (std::error_code EC =
693 checkOffset(getMemoryBufferRef(),
694 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
695 return EC;
696 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
697 return std::error_code();
698 }
699
700 template <class ELFT>
getSectionAlignment(DataRefImpl Sec)701 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
702 return getSection(Sec)->sh_addralign;
703 }
704
705 template <class ELFT>
isSectionCompressed(DataRefImpl Sec)706 bool ELFObjectFile<ELFT>::isSectionCompressed(DataRefImpl Sec) const {
707 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
708 }
709
710 template <class ELFT>
isSectionText(DataRefImpl Sec)711 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
712 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
713 }
714
715 template <class ELFT>
isSectionData(DataRefImpl Sec)716 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
717 const Elf_Shdr *EShdr = getSection(Sec);
718 return EShdr->sh_type == ELF::SHT_PROGBITS &&
719 EShdr->sh_flags & ELF::SHF_ALLOC &&
720 !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
721 }
722
723 template <class ELFT>
isSectionBSS(DataRefImpl Sec)724 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
725 const Elf_Shdr *EShdr = getSection(Sec);
726 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
727 EShdr->sh_type == ELF::SHT_NOBITS;
728 }
729
730 template <class ELFT>
731 std::vector<SectionRef>
dynamic_relocation_sections()732 ELFObjectFile<ELFT>::dynamic_relocation_sections() const {
733 std::vector<SectionRef> Res;
734 std::vector<uintptr_t> Offsets;
735
736 auto SectionsOrErr = EF.sections();
737 if (!SectionsOrErr)
738 return Res;
739
740 for (const Elf_Shdr &Sec : *SectionsOrErr) {
741 if (Sec.sh_type != ELF::SHT_DYNAMIC)
742 continue;
743 Elf_Dyn *Dynamic =
744 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
745 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
746 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
747 Dynamic->d_tag == ELF::DT_JMPREL) {
748 Offsets.push_back(Dynamic->d_un.d_val);
749 }
750 }
751 }
752 for (const Elf_Shdr &Sec : *SectionsOrErr) {
753 if (is_contained(Offsets, Sec.sh_offset))
754 Res.emplace_back(toDRI(&Sec), this);
755 }
756 return Res;
757 }
758
759 template <class ELFT>
isSectionVirtual(DataRefImpl Sec)760 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
761 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
762 }
763
764 template <class ELFT>
isBerkeleyText(DataRefImpl Sec)765 bool ELFObjectFile<ELFT>::isBerkeleyText(DataRefImpl Sec) const {
766 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
767 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
768 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
769 }
770
771 template <class ELFT>
isBerkeleyData(DataRefImpl Sec)772 bool ELFObjectFile<ELFT>::isBerkeleyData(DataRefImpl Sec) const {
773 const Elf_Shdr *EShdr = getSection(Sec);
774 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
775 EShdr->sh_flags & ELF::SHF_ALLOC;
776 }
777
778 template <class ELFT>
779 relocation_iterator
section_rel_begin(DataRefImpl Sec)780 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
781 DataRefImpl RelData;
782 auto SectionsOrErr = EF.sections();
783 if (!SectionsOrErr)
784 return relocation_iterator(RelocationRef());
785 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
786 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
787 RelData.d.b = 0;
788 return relocation_iterator(RelocationRef(RelData, this));
789 }
790
791 template <class ELFT>
792 relocation_iterator
section_rel_end(DataRefImpl Sec)793 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
794 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
795 relocation_iterator Begin = section_rel_begin(Sec);
796 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
797 return Begin;
798 DataRefImpl RelData = Begin->getRawDataRefImpl();
799 const Elf_Shdr *RelSec = getRelSection(RelData);
800
801 // Error check sh_link here so that getRelocationSymbol can just use it.
802 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
803 if (!SymSecOrErr)
804 report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
805
806 RelData.d.b += S->sh_size / S->sh_entsize;
807 return relocation_iterator(RelocationRef(RelData, this));
808 }
809
810 template <class ELFT>
811 section_iterator
getRelocatedSection(DataRefImpl Sec)812 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
813 if (EF.getHeader()->e_type != ELF::ET_REL)
814 return section_end();
815
816 const Elf_Shdr *EShdr = getSection(Sec);
817 uintX_t Type = EShdr->sh_type;
818 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
819 return section_end();
820
821 auto R = EF.getSection(EShdr->sh_info);
822 if (!R)
823 report_fatal_error(errorToErrorCode(R.takeError()).message());
824 return section_iterator(SectionRef(toDRI(*R), this));
825 }
826
827 // Relocations
828 template <class ELFT>
moveRelocationNext(DataRefImpl & Rel)829 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
830 ++Rel.d.b;
831 }
832
833 template <class ELFT>
834 symbol_iterator
getRelocationSymbol(DataRefImpl Rel)835 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
836 uint32_t symbolIdx;
837 const Elf_Shdr *sec = getRelSection(Rel);
838 if (sec->sh_type == ELF::SHT_REL)
839 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
840 else
841 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
842 if (!symbolIdx)
843 return symbol_end();
844
845 // FIXME: error check symbolIdx
846 DataRefImpl SymbolData;
847 SymbolData.d.a = sec->sh_link;
848 SymbolData.d.b = symbolIdx;
849 return symbol_iterator(SymbolRef(SymbolData, this));
850 }
851
852 template <class ELFT>
getRelocationOffset(DataRefImpl Rel)853 uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
854 const Elf_Shdr *sec = getRelSection(Rel);
855 if (sec->sh_type == ELF::SHT_REL)
856 return getRel(Rel)->r_offset;
857
858 return getRela(Rel)->r_offset;
859 }
860
861 template <class ELFT>
getRelocationType(DataRefImpl Rel)862 uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
863 const Elf_Shdr *sec = getRelSection(Rel);
864 if (sec->sh_type == ELF::SHT_REL)
865 return getRel(Rel)->getType(EF.isMips64EL());
866 else
867 return getRela(Rel)->getType(EF.isMips64EL());
868 }
869
870 template <class ELFT>
getRelocationTypeName(uint32_t Type)871 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
872 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
873 }
874
875 template <class ELFT>
getRelocationTypeName(DataRefImpl Rel,SmallVectorImpl<char> & Result)876 void ELFObjectFile<ELFT>::getRelocationTypeName(
877 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
878 uint32_t type = getRelocationType(Rel);
879 EF.getRelocationTypeName(type, Result);
880 }
881
882 template <class ELFT>
883 Expected<int64_t>
getRelocationAddend(DataRefImpl Rel)884 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
885 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
886 return createError("Section is not SHT_RELA");
887 return (int64_t)getRela(Rel)->r_addend;
888 }
889
890 template <class ELFT>
891 const typename ELFObjectFile<ELFT>::Elf_Rel *
getRel(DataRefImpl Rel)892 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
893 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
894 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
895 if (!Ret)
896 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
897 return *Ret;
898 }
899
900 template <class ELFT>
901 const typename ELFObjectFile<ELFT>::Elf_Rela *
getRela(DataRefImpl Rela)902 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
903 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
904 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
905 if (!Ret)
906 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
907 return *Ret;
908 }
909
910 template <class ELFT>
911 Expected<ELFObjectFile<ELFT>>
create(MemoryBufferRef Object)912 ELFObjectFile<ELFT>::create(MemoryBufferRef Object) {
913 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
914 if (Error E = EFOrErr.takeError())
915 return std::move(E);
916 auto EF = std::move(*EFOrErr);
917
918 auto SectionsOrErr = EF.sections();
919 if (!SectionsOrErr)
920 return SectionsOrErr.takeError();
921
922 const Elf_Shdr *DotDynSymSec = nullptr;
923 const Elf_Shdr *DotSymtabSec = nullptr;
924 ArrayRef<Elf_Word> ShndxTable;
925 for (const Elf_Shdr &Sec : *SectionsOrErr) {
926 switch (Sec.sh_type) {
927 case ELF::SHT_DYNSYM: {
928 if (DotDynSymSec)
929 return createError("More than one dynamic symbol table!");
930 DotDynSymSec = &Sec;
931 break;
932 }
933 case ELF::SHT_SYMTAB: {
934 if (DotSymtabSec)
935 return createError("More than one static symbol table!");
936 DotSymtabSec = &Sec;
937 break;
938 }
939 case ELF::SHT_SYMTAB_SHNDX: {
940 auto TableOrErr = EF.getSHNDXTable(Sec);
941 if (!TableOrErr)
942 return TableOrErr.takeError();
943 ShndxTable = *TableOrErr;
944 break;
945 }
946 }
947 }
948 return ELFObjectFile<ELFT>(Object, EF, DotDynSymSec, DotSymtabSec,
949 ShndxTable);
950 }
951
952 template <class ELFT>
ELFObjectFile(MemoryBufferRef Object,ELFFile<ELFT> EF,const Elf_Shdr * DotDynSymSec,const Elf_Shdr * DotSymtabSec,ArrayRef<Elf_Word> ShndxTable)953 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
954 const Elf_Shdr *DotDynSymSec,
955 const Elf_Shdr *DotSymtabSec,
956 ArrayRef<Elf_Word> ShndxTable)
957 : ELFObjectFileBase(
958 getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
959 Object),
960 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
961 ShndxTable(ShndxTable) {}
962
963 template <class ELFT>
ELFObjectFile(ELFObjectFile<ELFT> && Other)964 ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
965 : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
966 Other.DotSymtabSec, Other.ShndxTable) {}
967
968 template <class ELFT>
symbol_begin()969 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin() const {
970 DataRefImpl Sym = toDRI(DotSymtabSec, 0);
971 return basic_symbol_iterator(SymbolRef(Sym, this));
972 }
973
974 template <class ELFT>
symbol_end()975 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end() const {
976 const Elf_Shdr *SymTab = DotSymtabSec;
977 if (!SymTab)
978 return symbol_begin();
979 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
980 return basic_symbol_iterator(SymbolRef(Sym, this));
981 }
982
983 template <class ELFT>
dynamic_symbol_begin()984 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
985 DataRefImpl Sym = toDRI(DotDynSymSec, 0);
986 return symbol_iterator(SymbolRef(Sym, this));
987 }
988
989 template <class ELFT>
dynamic_symbol_end()990 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
991 const Elf_Shdr *SymTab = DotDynSymSec;
992 if (!SymTab)
993 return dynamic_symbol_begin();
994 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
995 return basic_symbol_iterator(SymbolRef(Sym, this));
996 }
997
998 template <class ELFT>
section_begin()999 section_iterator ELFObjectFile<ELFT>::section_begin() const {
1000 auto SectionsOrErr = EF.sections();
1001 if (!SectionsOrErr)
1002 return section_iterator(SectionRef());
1003 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1004 }
1005
1006 template <class ELFT>
section_end()1007 section_iterator ELFObjectFile<ELFT>::section_end() const {
1008 auto SectionsOrErr = EF.sections();
1009 if (!SectionsOrErr)
1010 return section_iterator(SectionRef());
1011 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1012 }
1013
1014 template <class ELFT>
getBytesInAddress()1015 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
1016 return ELFT::Is64Bits ? 8 : 4;
1017 }
1018
1019 template <class ELFT>
getFileFormatName()1020 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
1021 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1022 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1023 case ELF::ELFCLASS32:
1024 switch (EF.getHeader()->e_machine) {
1025 case ELF::EM_386:
1026 return "ELF32-i386";
1027 case ELF::EM_IAMCU:
1028 return "ELF32-iamcu";
1029 case ELF::EM_X86_64:
1030 return "ELF32-x86-64";
1031 case ELF::EM_ARM:
1032 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
1033 case ELF::EM_AVR:
1034 return "ELF32-avr";
1035 case ELF::EM_HEXAGON:
1036 return "ELF32-hexagon";
1037 case ELF::EM_LANAI:
1038 return "ELF32-lanai";
1039 case ELF::EM_MIPS:
1040 return "ELF32-mips";
1041 case ELF::EM_MSP430:
1042 return "ELF32-msp430";
1043 case ELF::EM_PPC:
1044 return "ELF32-ppc";
1045 case ELF::EM_RISCV:
1046 return "ELF32-riscv";
1047 case ELF::EM_SPARC:
1048 case ELF::EM_SPARC32PLUS:
1049 return "ELF32-sparc";
1050 case ELF::EM_AMDGPU:
1051 return "ELF32-amdgpu";
1052 default:
1053 return "ELF32-unknown";
1054 }
1055 case ELF::ELFCLASS64:
1056 switch (EF.getHeader()->e_machine) {
1057 case ELF::EM_386:
1058 return "ELF64-i386";
1059 case ELF::EM_X86_64:
1060 return "ELF64-x86-64";
1061 case ELF::EM_AARCH64:
1062 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
1063 case ELF::EM_PPC64:
1064 return "ELF64-ppc64";
1065 case ELF::EM_RISCV:
1066 return "ELF64-riscv";
1067 case ELF::EM_S390:
1068 return "ELF64-s390";
1069 case ELF::EM_SPARCV9:
1070 return "ELF64-sparc";
1071 case ELF::EM_MIPS:
1072 return "ELF64-mips";
1073 case ELF::EM_AMDGPU:
1074 return "ELF64-amdgpu";
1075 case ELF::EM_BPF:
1076 return "ELF64-BPF";
1077 default:
1078 return "ELF64-unknown";
1079 }
1080 default:
1081 // FIXME: Proper error handling.
1082 report_fatal_error("Invalid ELFCLASS!");
1083 }
1084 }
1085
getArch()1086 template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1087 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1088 switch (EF.getHeader()->e_machine) {
1089 case ELF::EM_386:
1090 case ELF::EM_IAMCU:
1091 return Triple::x86;
1092 case ELF::EM_X86_64:
1093 return Triple::x86_64;
1094 case ELF::EM_AARCH64:
1095 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1096 case ELF::EM_ARM:
1097 return Triple::arm;
1098 case ELF::EM_AVR:
1099 return Triple::avr;
1100 case ELF::EM_HEXAGON:
1101 return Triple::hexagon;
1102 case ELF::EM_LANAI:
1103 return Triple::lanai;
1104 case ELF::EM_MIPS:
1105 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1106 case ELF::ELFCLASS32:
1107 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1108 case ELF::ELFCLASS64:
1109 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1110 default:
1111 report_fatal_error("Invalid ELFCLASS!");
1112 }
1113 case ELF::EM_MSP430:
1114 return Triple::msp430;
1115 case ELF::EM_PPC:
1116 return Triple::ppc;
1117 case ELF::EM_PPC64:
1118 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1119 case ELF::EM_RISCV:
1120 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1121 case ELF::ELFCLASS32:
1122 return Triple::riscv32;
1123 case ELF::ELFCLASS64:
1124 return Triple::riscv64;
1125 default:
1126 report_fatal_error("Invalid ELFCLASS!");
1127 }
1128 case ELF::EM_S390:
1129 return Triple::systemz;
1130
1131 case ELF::EM_SPARC:
1132 case ELF::EM_SPARC32PLUS:
1133 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1134 case ELF::EM_SPARCV9:
1135 return Triple::sparcv9;
1136
1137 case ELF::EM_AMDGPU: {
1138 if (!IsLittleEndian)
1139 return Triple::UnknownArch;
1140
1141 unsigned MACH = EF.getHeader()->e_flags & ELF::EF_AMDGPU_MACH;
1142 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1143 MACH <= ELF::EF_AMDGPU_MACH_R600_LAST)
1144 return Triple::r600;
1145 if (MACH >= ELF::EF_AMDGPU_MACH_AMDGCN_FIRST &&
1146 MACH <= ELF::EF_AMDGPU_MACH_AMDGCN_LAST)
1147 return Triple::amdgcn;
1148
1149 return Triple::UnknownArch;
1150 }
1151
1152 case ELF::EM_BPF:
1153 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1154
1155 default:
1156 return Triple::UnknownArch;
1157 }
1158 }
1159
1160 template <class ELFT>
getStartAddress()1161 Expected<uint64_t> ELFObjectFile<ELFT>::getStartAddress() const {
1162 return EF.getHeader()->e_entry;
1163 }
1164
1165 template <class ELFT>
1166 ELFObjectFileBase::elf_symbol_iterator_range
getDynamicSymbolIterators()1167 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
1168 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1169 }
1170
isRelocatableObject()1171 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1172 return EF.getHeader()->e_type == ELF::ET_REL;
1173 }
1174
1175 } // end namespace object
1176 } // end namespace llvm
1177
1178 #endif // LLVM_OBJECT_ELFOBJECTFILE_H
1179