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