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