blob: fed53eef68c3de42fc45e770ef05b8b214ae1e56 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010017#include "llvm/ADT/STLExtras.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010018#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"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031#include "llvm/Support/Casting.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020032#include "llvm/Support/ELFAttributes.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010033#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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020044constexpr int NumElfSymbolTypes = 16;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010045extern const llvm::EnumEntry<unsigned> ElfSymbolTypes[NumElfSymbolTypes];
46
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010047class elf_symbol_iterator;
48
49class ELFObjectFileBase : public ObjectFile {
50 friend class ELFRelocationRef;
51 friend class ELFSectionRef;
52 friend class ELFSymbolRef;
53
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020054 SubtargetFeatures getMIPSFeatures() const;
55 SubtargetFeatures getARMFeatures() const;
56 SubtargetFeatures getRISCVFeatures() const;
57
58 StringRef getAMDGPUCPUName() const;
59
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060protected:
61 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
62
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010063 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010064 virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010065 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;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020073 virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074
75public:
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
85 static bool classof(const Binary *v) { return v->isELF(); }
86
87 SubtargetFeatures getFeatures() const override;
88
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020089 Optional<StringRef> tryGetCPUName() const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010090
91 void setARMSubArch(Triple &TheTriple) const override;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010092
93 virtual uint16_t getEType() const = 0;
94
Andrew Walbran3d2c1972020-04-07 12:24:26 +010095 virtual uint16_t getEMachine() const = 0;
96
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020097 std::vector<std::pair<Optional<DataRefImpl>, uint64_t>>
98 getPltAddresses() const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010099};
100
101class ELFSectionRef : public SectionRef {
102public:
103 ELFSectionRef(const SectionRef &B) : SectionRef(B) {
104 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
105 }
106
107 const ELFObjectFileBase *getObject() const {
108 return cast<ELFObjectFileBase>(SectionRef::getObject());
109 }
110
111 uint32_t getType() const {
112 return getObject()->getSectionType(getRawDataRefImpl());
113 }
114
115 uint64_t getFlags() const {
116 return getObject()->getSectionFlags(getRawDataRefImpl());
117 }
118
119 uint64_t getOffset() const {
120 return getObject()->getSectionOffset(getRawDataRefImpl());
121 }
122};
123
124class elf_section_iterator : public section_iterator {
125public:
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
139class ELFSymbolRef : public SymbolRef {
140public:
141 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
142 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
143 }
144
145 const ELFObjectFileBase *getObject() const {
146 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
147 }
148
149 uint64_t getSize() const {
150 return getObject()->getSymbolSize(getRawDataRefImpl());
151 }
152
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100153 uint8_t getBinding() const {
154 return getObject()->getSymbolBinding(getRawDataRefImpl());
155 }
156
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100157 uint8_t getOther() const {
158 return getObject()->getSymbolOther(getRawDataRefImpl());
159 }
160
161 uint8_t getELFType() const {
162 return getObject()->getSymbolELFType(getRawDataRefImpl());
163 }
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100164
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 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100174};
175
176class elf_symbol_iterator : public symbol_iterator {
177public:
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
191class ELFRelocationRef : public RelocationRef {
192public:
193 ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) {
194 assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
195 }
196
197 const ELFObjectFileBase *getObject() const {
198 return cast<ELFObjectFileBase>(RelocationRef::getObject());
199 }
200
201 Expected<int64_t> getAddend() const {
202 return getObject()->getRelocationAddend(getRawDataRefImpl());
203 }
204};
205
206class elf_relocation_iterator : public relocation_iterator {
207public:
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
223inline ELFObjectFileBase::elf_symbol_iterator_range
224ELFObjectFileBase::symbols() const {
225 return elf_symbol_iterator_range(symbol_begin(), symbol_end());
226}
227
228template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
229 uint16_t getEMachine() const override;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100230 uint16_t getEType() const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100231 uint64_t getSymbolSize(DataRefImpl Sym) const override;
232
233public:
234 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
235
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200236 SectionRef toSectionRef(const Elf_Shdr *Sec) const {
237 return SectionRef(toDRI(Sec), this);
238 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100239
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200240 ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
241 return ELFSymbolRef({toDRI(SymTable, SymbolNum), this});
242 }
243
244 bool IsContentValid() const { return ContentValid; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100245
246private:
247 ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
248 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200249 const Elf_Shdr *DotSymtabShndxSec);
250
251 bool ContentValid = false;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100252
253protected:
254 ELFFile<ELFT> EF;
255
256 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
257 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200258 const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section.
259
260 Error initContent() override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100261
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;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200268 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100269 uint8_t getSymbolBinding(DataRefImpl Symb) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100270 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;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100278 Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100279 uint64_t getSectionAddress(DataRefImpl Sec) const override;
280 uint64_t getSectionIndex(DataRefImpl Sec) const override;
281 uint64_t getSectionSize(DataRefImpl Sec) const override;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100282 Expected<ArrayRef<uint8_t>>
283 getSectionContents(DataRefImpl Sec) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100284 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;
Andrew Walbran16937d02019-10-22 13:54:20 +0100290 bool isBerkeleyText(DataRefImpl Sec) const override;
291 bool isBerkeleyData(DataRefImpl Sec) const override;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200292 bool isDebugSection(StringRef SectionName) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100293 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
294 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100295 std::vector<SectionRef> dynamic_relocation_sections() const override;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200296 Expected<section_iterator>
297 getRelocatedSection(DataRefImpl Sec) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100298
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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100311 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
336 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
337 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
338 }
339
340 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
341 DataRefImpl DRI;
342 DRI.p = reinterpret_cast<uintptr_t>(Sec);
343 return DRI;
344 }
345
346 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
347 DataRefImpl DRI;
348 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
349 return DRI;
350 }
351
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.
Andrew Walbran16937d02019-10-22 13:54:20 +0100359 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));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100363 }
364
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200365 Error getBuildAttributes(ELFAttributeParser &Attributes) const override {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100366 auto SectionsOrErr = EF.sections();
367 if (!SectionsOrErr)
368 return SectionsOrErr.takeError();
369
370 for (const Elf_Shdr &Sec : *SectionsOrErr) {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200371 if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES ||
372 Sec.sh_type == ELF::SHT_RISCV_ATTRIBUTES) {
373 auto ErrorOrContents = EF.getSectionContents(Sec);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100374 if (!ErrorOrContents)
375 return ErrorOrContents.takeError();
376
377 auto Contents = ErrorOrContents.get();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200378 if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100379 return Error::success();
380
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200381 if (Error E = Attributes.parse(Contents, ELFT::TargetEndianness))
382 return E;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100383 break;
384 }
385 }
386 return Error::success();
387 }
388
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100389 // 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
394public:
395 ELFObjectFile(ELFObjectFile<ELFT> &&Other);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200396 static Expected<ELFObjectFile<ELFT>> create(MemoryBufferRef Object,
397 bool InitContent = true);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100398
399 const Elf_Rel *getRel(DataRefImpl Rel) const;
400 const Elf_Rela *getRela(DataRefImpl Rela) const;
401
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200402 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.
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;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100412 }
413
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;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100432 Expected<uint64_t> getStartAddress() const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100433
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200434 unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100435
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200436 const ELFFile<ELFT> &getELFFile() const { return EF; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100437
438 bool isDyldType() const { return isDyldELFObject; }
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
449using ELF32LEObjectFile = ELFObjectFile<ELF32LE>;
450using ELF64LEObjectFile = ELFObjectFile<ELF64LE>;
451using ELF32BEObjectFile = ELFObjectFile<ELF32BE>;
452using ELF64BEObjectFile = ELFObjectFile<ELF64BE>;
453
454template <class ELFT>
455void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
456 ++Sym.d.b;
457}
458
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200459template <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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100488template <class ELFT>
489Expected<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200490 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
491 if (!SymOrErr)
492 return SymOrErr.takeError();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100493 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;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200501 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100502 if (!SymStrTabOrErr)
503 return SymStrTabOrErr.takeError();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200504 Expected<StringRef> Name = (*SymOrErr)->getName(*SymStrTabOrErr);
505 if (Name && !Name->empty())
506 return Name;
Andrew Walbran16937d02019-10-22 13:54:20 +0100507
508 // If the symbol name is empty use the section name.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200509 if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
510 if (Expected<section_iterator> SecOrErr = getSymbolSection(Sym)) {
511 consumeError(Name.takeError());
512 return (*SecOrErr)->getName();
513 }
Andrew Walbran16937d02019-10-22 13:54:20 +0100514 }
515 return Name;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100516}
517
518template <class ELFT>
519uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
520 return getSection(Sec)->sh_flags;
521}
522
523template <class ELFT>
524uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
525 return getSection(Sec)->sh_type;
526}
527
528template <class ELFT>
529uint64_t ELFObjectFile<ELFT>::getSectionOffset(DataRefImpl Sec) const {
530 return getSection(Sec)->sh_offset;
531}
532
533template <class ELFT>
534uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200535 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)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100541 return Ret;
542
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200543 const Elf_Ehdr &Header = EF.getHeader();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100544 // Clear the ARM/Thumb or microMIPS indicator flag.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200545 if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) &&
546 (*SymOrErr)->getType() == ELF::STT_FUNC)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100547 Ret &= ~1;
548
549 return Ret;
550}
551
552template <class ELFT>
553Expected<uint64_t>
554ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200555 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) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100566 case ELF::SHN_COMMON:
567 case ELF::SHN_UNDEF:
568 case ELF::SHN_ABS:
569 return Result;
570 }
571
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100572 auto SymTabOrErr = EF.getSection(Symb.d.a);
573 if (!SymTabOrErr)
574 return SymTabOrErr.takeError();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100575
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200576 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);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100589 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
599template <class ELFT>
600uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200601 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;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100606 return 0;
607}
608
609template <class ELFT>
610uint16_t ELFObjectFile<ELFT>::getEMachine() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200611 return EF.getHeader().e_machine;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100612}
613
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100614template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200615 return EF.getHeader().e_type;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100616}
617
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100618template <class ELFT>
619uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200620 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
621 if (!SymOrErr)
622 report_fatal_error(SymOrErr.takeError());
623 return (*SymOrErr)->st_size;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100624}
625
626template <class ELFT>
627uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200628 return getSymbolSize(Symb);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100629}
630
631template <class ELFT>
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100632uint8_t ELFObjectFile<ELFT>::getSymbolBinding(DataRefImpl Symb) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200633 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
634 if (!SymOrErr)
635 report_fatal_error(SymOrErr.takeError());
636 return (*SymOrErr)->getBinding();
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100637}
638
639template <class ELFT>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100640uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200641 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
642 if (!SymOrErr)
643 report_fatal_error(SymOrErr.takeError());
644 return (*SymOrErr)->st_other;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100645}
646
647template <class ELFT>
648uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200649 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
650 if (!SymOrErr)
651 report_fatal_error(SymOrErr.takeError());
652 return (*SymOrErr)->getType();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100653}
654
655template <class ELFT>
656Expected<SymbolRef::Type>
657ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200658 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
659 if (!SymOrErr)
660 return SymOrErr.takeError();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100661
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200662 switch ((*SymOrErr)->getType()) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100663 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 case ELF::STT_TLS:
674 return SymbolRef::ST_Data;
675 default:
676 return SymbolRef::ST_Other;
677 }
678}
679
680template <class ELFT>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200681Expected<uint32_t> ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
682 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
683 if (!SymOrErr)
684 return SymOrErr.takeError();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100685
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200686 const Elf_Sym *ESym = *SymOrErr;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100687 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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200701 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();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100709
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200710 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_ARM) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100720 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
721 StringRef Name = *NameOrErr;
722 if (Name.startswith("$d") || Name.startswith("$t") ||
723 Name.startswith("$a"))
724 Result |= SymbolRef::SF_FormatSpecific;
725 } else {
726 // TODO: Actually report errors helpfully.
727 consumeError(NameOrErr.takeError());
728 }
729 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
730 Result |= SymbolRef::SF_Thumb;
731 }
732
733 if (ESym->st_shndx == ELF::SHN_UNDEF)
734 Result |= SymbolRef::SF_Undefined;
735
736 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
737 Result |= SymbolRef::SF_Common;
738
739 if (isExportedToOtherDSO(ESym))
740 Result |= SymbolRef::SF_Exported;
741
742 if (ESym->getVisibility() == ELF::STV_HIDDEN)
743 Result |= SymbolRef::SF_Hidden;
744
745 return Result;
746}
747
748template <class ELFT>
749Expected<section_iterator>
750ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym,
751 const Elf_Shdr *SymTab) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200752 ArrayRef<Elf_Word> ShndxTable;
753 if (DotSymtabShndxSec) {
754 // TODO: Test this error.
755 Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
756 EF.getSHNDXTable(*DotSymtabShndxSec);
757 if (!ShndxTableOrErr)
758 return ShndxTableOrErr.takeError();
759 ShndxTable = *ShndxTableOrErr;
760 }
761
762 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100763 if (!ESecOrErr)
764 return ESecOrErr.takeError();
765
766 const Elf_Shdr *ESec = *ESecOrErr;
767 if (!ESec)
768 return section_end();
769
770 DataRefImpl Sec;
771 Sec.p = reinterpret_cast<intptr_t>(ESec);
772 return section_iterator(SectionRef(Sec, this));
773}
774
775template <class ELFT>
776Expected<section_iterator>
777ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200778 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
779 if (!SymOrErr)
780 return SymOrErr.takeError();
781
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100782 auto SymTabOrErr = EF.getSection(Symb.d.a);
783 if (!SymTabOrErr)
784 return SymTabOrErr.takeError();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200785 return getSymbolSection(*SymOrErr, *SymTabOrErr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100786}
787
788template <class ELFT>
789void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
790 const Elf_Shdr *ESec = getSection(Sec);
791 Sec = toDRI(++ESec);
792}
793
794template <class ELFT>
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100795Expected<StringRef> ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200796 return EF.getSectionName(*getSection(Sec));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100797}
798
799template <class ELFT>
800uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
801 return getSection(Sec)->sh_addr;
802}
803
804template <class ELFT>
805uint64_t ELFObjectFile<ELFT>::getSectionIndex(DataRefImpl Sec) const {
806 auto SectionsOrErr = EF.sections();
807 handleAllErrors(std::move(SectionsOrErr.takeError()),
808 [](const ErrorInfoBase &) {
809 llvm_unreachable("unable to get section index");
810 });
811 const Elf_Shdr *First = SectionsOrErr->begin();
812 return getSection(Sec) - First;
813}
814
815template <class ELFT>
816uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
817 return getSection(Sec)->sh_size;
818}
819
820template <class ELFT>
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100821Expected<ArrayRef<uint8_t>>
822ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100823 const Elf_Shdr *EShdr = getSection(Sec);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200824 if (EShdr->sh_type == ELF::SHT_NOBITS)
825 return makeArrayRef((const uint8_t *)base(), 0);
826 if (Error E =
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100827 checkOffset(getMemoryBufferRef(),
828 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200829 return std::move(E);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100830 return makeArrayRef((const uint8_t *)base() + EShdr->sh_offset,
831 EShdr->sh_size);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100832}
833
834template <class ELFT>
835uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
836 return getSection(Sec)->sh_addralign;
837}
838
839template <class ELFT>
840bool ELFObjectFile<ELFT>::isSectionCompressed(DataRefImpl Sec) const {
841 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
842}
843
844template <class ELFT>
845bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
846 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
847}
848
849template <class ELFT>
850bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
851 const Elf_Shdr *EShdr = getSection(Sec);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100852 return EShdr->sh_type == ELF::SHT_PROGBITS &&
853 EShdr->sh_flags & ELF::SHF_ALLOC &&
854 !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100855}
856
857template <class ELFT>
858bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
859 const Elf_Shdr *EShdr = getSection(Sec);
860 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
861 EShdr->sh_type == ELF::SHT_NOBITS;
862}
863
864template <class ELFT>
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100865std::vector<SectionRef>
866ELFObjectFile<ELFT>::dynamic_relocation_sections() const {
867 std::vector<SectionRef> Res;
868 std::vector<uintptr_t> Offsets;
869
870 auto SectionsOrErr = EF.sections();
871 if (!SectionsOrErr)
872 return Res;
873
874 for (const Elf_Shdr &Sec : *SectionsOrErr) {
875 if (Sec.sh_type != ELF::SHT_DYNAMIC)
876 continue;
877 Elf_Dyn *Dynamic =
878 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
879 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
880 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
881 Dynamic->d_tag == ELF::DT_JMPREL) {
882 Offsets.push_back(Dynamic->d_un.d_val);
883 }
884 }
885 }
886 for (const Elf_Shdr &Sec : *SectionsOrErr) {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100887 if (is_contained(Offsets, Sec.sh_addr))
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100888 Res.emplace_back(toDRI(&Sec), this);
889 }
890 return Res;
891}
892
893template <class ELFT>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100894bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
895 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
896}
897
898template <class ELFT>
Andrew Walbran16937d02019-10-22 13:54:20 +0100899bool ELFObjectFile<ELFT>::isBerkeleyText(DataRefImpl Sec) const {
900 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
901 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
902 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
903}
904
905template <class ELFT>
906bool ELFObjectFile<ELFT>::isBerkeleyData(DataRefImpl Sec) const {
907 const Elf_Shdr *EShdr = getSection(Sec);
908 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
909 EShdr->sh_flags & ELF::SHF_ALLOC;
910}
911
912template <class ELFT>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200913bool ELFObjectFile<ELFT>::isDebugSection(StringRef SectionName) const {
914 return SectionName.startswith(".debug") ||
915 SectionName.startswith(".zdebug") || SectionName == ".gdb_index";
916}
917
918template <class ELFT>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100919relocation_iterator
920ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
921 DataRefImpl RelData;
922 auto SectionsOrErr = EF.sections();
923 if (!SectionsOrErr)
924 return relocation_iterator(RelocationRef());
925 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200926 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100927 RelData.d.b = 0;
928 return relocation_iterator(RelocationRef(RelData, this));
929}
930
931template <class ELFT>
932relocation_iterator
933ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
934 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
935 relocation_iterator Begin = section_rel_begin(Sec);
936 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
937 return Begin;
938 DataRefImpl RelData = Begin->getRawDataRefImpl();
939 const Elf_Shdr *RelSec = getRelSection(RelData);
940
941 // Error check sh_link here so that getRelocationSymbol can just use it.
942 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
943 if (!SymSecOrErr)
944 report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
945
946 RelData.d.b += S->sh_size / S->sh_entsize;
947 return relocation_iterator(RelocationRef(RelData, this));
948}
949
950template <class ELFT>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200951Expected<section_iterator>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100952ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200953 if (EF.getHeader().e_type != ELF::ET_REL)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100954 return section_end();
955
956 const Elf_Shdr *EShdr = getSection(Sec);
957 uintX_t Type = EShdr->sh_type;
958 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
959 return section_end();
960
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200961 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
962 if (!SecOrErr)
963 return SecOrErr.takeError();
964 return section_iterator(SectionRef(toDRI(*SecOrErr), this));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100965}
966
967// Relocations
968template <class ELFT>
969void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
970 ++Rel.d.b;
971}
972
973template <class ELFT>
974symbol_iterator
975ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
976 uint32_t symbolIdx;
977 const Elf_Shdr *sec = getRelSection(Rel);
978 if (sec->sh_type == ELF::SHT_REL)
979 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
980 else
981 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
982 if (!symbolIdx)
983 return symbol_end();
984
985 // FIXME: error check symbolIdx
986 DataRefImpl SymbolData;
987 SymbolData.d.a = sec->sh_link;
988 SymbolData.d.b = symbolIdx;
989 return symbol_iterator(SymbolRef(SymbolData, this));
990}
991
992template <class ELFT>
993uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100994 const Elf_Shdr *sec = getRelSection(Rel);
995 if (sec->sh_type == ELF::SHT_REL)
996 return getRel(Rel)->r_offset;
997
998 return getRela(Rel)->r_offset;
999}
1000
1001template <class ELFT>
1002uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
1003 const Elf_Shdr *sec = getRelSection(Rel);
1004 if (sec->sh_type == ELF::SHT_REL)
1005 return getRel(Rel)->getType(EF.isMips64EL());
1006 else
1007 return getRela(Rel)->getType(EF.isMips64EL());
1008}
1009
1010template <class ELFT>
1011StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001012 return getELFRelocationTypeName(EF.getHeader().e_machine, Type);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001013}
1014
1015template <class ELFT>
1016void ELFObjectFile<ELFT>::getRelocationTypeName(
1017 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1018 uint32_t type = getRelocationType(Rel);
1019 EF.getRelocationTypeName(type, Result);
1020}
1021
1022template <class ELFT>
1023Expected<int64_t>
1024ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
1025 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
1026 return createError("Section is not SHT_RELA");
1027 return (int64_t)getRela(Rel)->r_addend;
1028}
1029
1030template <class ELFT>
1031const typename ELFObjectFile<ELFT>::Elf_Rel *
1032ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
1033 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
1034 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
1035 if (!Ret)
1036 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
1037 return *Ret;
1038}
1039
1040template <class ELFT>
1041const typename ELFObjectFile<ELFT>::Elf_Rela *
1042ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
1043 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
1044 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
1045 if (!Ret)
1046 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
1047 return *Ret;
1048}
1049
1050template <class ELFT>
1051Expected<ELFObjectFile<ELFT>>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001052ELFObjectFile<ELFT>::create(MemoryBufferRef Object, bool InitContent) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001053 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
1054 if (Error E = EFOrErr.takeError())
1055 return std::move(E);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001056
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001057 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr,
1058 nullptr};
1059 if (InitContent)
1060 if (Error E = Obj.initContent())
1061 return std::move(E);
1062 return std::move(Obj);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001063}
1064
1065template <class ELFT>
1066ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
1067 const Elf_Shdr *DotDynSymSec,
1068 const Elf_Shdr *DotSymtabSec,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001069 const Elf_Shdr *DotSymtabShndx)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001070 : ELFObjectFileBase(
1071 getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
1072 Object),
1073 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001074 DotSymtabShndxSec(DotSymtabShndx) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001075
1076template <class ELFT>
1077ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
1078 : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001079 Other.DotSymtabSec, Other.DotSymtabShndxSec) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001080
1081template <class ELFT>
1082basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin() const {
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001083 DataRefImpl Sym =
1084 toDRI(DotSymtabSec,
1085 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001086 return basic_symbol_iterator(SymbolRef(Sym, this));
1087}
1088
1089template <class ELFT>
1090basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end() const {
1091 const Elf_Shdr *SymTab = DotSymtabSec;
1092 if (!SymTab)
1093 return symbol_begin();
1094 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1095 return basic_symbol_iterator(SymbolRef(Sym, this));
1096}
1097
1098template <class ELFT>
1099elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001100 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
1101 // Ignore errors here where the dynsym is empty or sh_size less than the
1102 // size of one symbol. These should be handled elsewhere.
1103 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
1104 // Skip 0-index NULL symbol.
1105 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001106}
1107
1108template <class ELFT>
1109elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
1110 const Elf_Shdr *SymTab = DotDynSymSec;
1111 if (!SymTab)
1112 return dynamic_symbol_begin();
1113 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1114 return basic_symbol_iterator(SymbolRef(Sym, this));
1115}
1116
1117template <class ELFT>
1118section_iterator ELFObjectFile<ELFT>::section_begin() const {
1119 auto SectionsOrErr = EF.sections();
1120 if (!SectionsOrErr)
1121 return section_iterator(SectionRef());
1122 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1123}
1124
1125template <class ELFT>
1126section_iterator ELFObjectFile<ELFT>::section_end() const {
1127 auto SectionsOrErr = EF.sections();
1128 if (!SectionsOrErr)
1129 return section_iterator(SectionRef());
1130 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1131}
1132
1133template <class ELFT>
1134uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
1135 return ELFT::Is64Bits ? 8 : 4;
1136}
1137
1138template <class ELFT>
1139StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
1140 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001141 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001142 case ELF::ELFCLASS32:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001143 switch (EF.getHeader().e_machine) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001144 case ELF::EM_386:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001145 return "elf32-i386";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001146 case ELF::EM_IAMCU:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001147 return "elf32-iamcu";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001148 case ELF::EM_X86_64:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001149 return "elf32-x86-64";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001150 case ELF::EM_ARM:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001151 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001152 case ELF::EM_AVR:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001153 return "elf32-avr";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001154 case ELF::EM_HEXAGON:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001155 return "elf32-hexagon";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001156 case ELF::EM_LANAI:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001157 return "elf32-lanai";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001158 case ELF::EM_MIPS:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001159 return "elf32-mips";
Andrew Walbran16937d02019-10-22 13:54:20 +01001160 case ELF::EM_MSP430:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001161 return "elf32-msp430";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001162 case ELF::EM_PPC:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001163 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001164 case ELF::EM_RISCV:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001165 return "elf32-littleriscv";
1166 case ELF::EM_CSKY:
1167 return "elf32-csky";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001168 case ELF::EM_SPARC:
1169 case ELF::EM_SPARC32PLUS:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001170 return "elf32-sparc";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001171 case ELF::EM_AMDGPU:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001172 return "elf32-amdgpu";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001173 default:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001174 return "elf32-unknown";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001175 }
1176 case ELF::ELFCLASS64:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001177 switch (EF.getHeader().e_machine) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001178 case ELF::EM_386:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001179 return "elf64-i386";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001180 case ELF::EM_X86_64:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001181 return "elf64-x86-64";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001182 case ELF::EM_AARCH64:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001183 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001184 case ELF::EM_PPC64:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001185 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001186 case ELF::EM_RISCV:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001187 return "elf64-littleriscv";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001188 case ELF::EM_S390:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001189 return "elf64-s390";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001190 case ELF::EM_SPARCV9:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001191 return "elf64-sparc";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001192 case ELF::EM_MIPS:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001193 return "elf64-mips";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001194 case ELF::EM_AMDGPU:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001195 return "elf64-amdgpu";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001196 case ELF::EM_BPF:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001197 return "elf64-bpf";
1198 case ELF::EM_VE:
1199 return "elf64-ve";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001200 default:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001201 return "elf64-unknown";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001202 }
1203 default:
1204 // FIXME: Proper error handling.
1205 report_fatal_error("Invalid ELFCLASS!");
1206 }
1207}
1208
1209template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1210 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001211 switch (EF.getHeader().e_machine) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001212 case ELF::EM_386:
1213 case ELF::EM_IAMCU:
1214 return Triple::x86;
1215 case ELF::EM_X86_64:
1216 return Triple::x86_64;
1217 case ELF::EM_AARCH64:
1218 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1219 case ELF::EM_ARM:
1220 return Triple::arm;
1221 case ELF::EM_AVR:
1222 return Triple::avr;
1223 case ELF::EM_HEXAGON:
1224 return Triple::hexagon;
1225 case ELF::EM_LANAI:
1226 return Triple::lanai;
1227 case ELF::EM_MIPS:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001228 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001229 case ELF::ELFCLASS32:
1230 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1231 case ELF::ELFCLASS64:
1232 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1233 default:
1234 report_fatal_error("Invalid ELFCLASS!");
1235 }
Andrew Walbran16937d02019-10-22 13:54:20 +01001236 case ELF::EM_MSP430:
1237 return Triple::msp430;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001238 case ELF::EM_PPC:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001239 return IsLittleEndian ? Triple::ppcle : Triple::ppc;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001240 case ELF::EM_PPC64:
1241 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1242 case ELF::EM_RISCV:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001243 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001244 case ELF::ELFCLASS32:
1245 return Triple::riscv32;
1246 case ELF::ELFCLASS64:
1247 return Triple::riscv64;
1248 default:
1249 report_fatal_error("Invalid ELFCLASS!");
1250 }
1251 case ELF::EM_S390:
1252 return Triple::systemz;
1253
1254 case ELF::EM_SPARC:
1255 case ELF::EM_SPARC32PLUS:
1256 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1257 case ELF::EM_SPARCV9:
1258 return Triple::sparcv9;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001259
1260 case ELF::EM_AMDGPU: {
1261 if (!IsLittleEndian)
1262 return Triple::UnknownArch;
1263
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001264 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001265 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1266 MACH <= ELF::EF_AMDGPU_MACH_R600_LAST)
1267 return Triple::r600;
1268 if (MACH >= ELF::EF_AMDGPU_MACH_AMDGCN_FIRST &&
1269 MACH <= ELF::EF_AMDGPU_MACH_AMDGCN_LAST)
1270 return Triple::amdgcn;
1271
1272 return Triple::UnknownArch;
1273 }
1274
1275 case ELF::EM_BPF:
1276 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1277
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001278 case ELF::EM_VE:
1279 return Triple::ve;
1280 case ELF::EM_CSKY:
1281 return Triple::csky;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001282 default:
1283 return Triple::UnknownArch;
1284 }
1285}
1286
1287template <class ELFT>
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001288Expected<uint64_t> ELFObjectFile<ELFT>::getStartAddress() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001289 return EF.getHeader().e_entry;
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001290}
1291
1292template <class ELFT>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001293ELFObjectFileBase::elf_symbol_iterator_range
1294ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
1295 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1296}
1297
1298template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001299 return EF.getHeader().e_type == ELF::ET_REL;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001300}
1301
1302} // end namespace object
1303} // end namespace llvm
1304
1305#endif // LLVM_OBJECT_ELFOBJECTFILE_H