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