1 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the ELFObjectFile template class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_H
16
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Object/ELF.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <algorithm>
31 #include <cctype>
32 #include <limits>
33 #include <utility>
34
35 namespace llvm {
36 namespace object {
37
38 class ELFObjectFileBase : public ObjectFile {
39 protected:
40 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
41
42 public:
43 virtual std::error_code getRelocationAddend(DataRefImpl Rel,
44 int64_t &Res) const = 0;
45 virtual std::pair<symbol_iterator, symbol_iterator>
46 getELFDynamicSymbolIterators() const = 0;
47
48 virtual std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
49 bool &IsDefault) const = 0;
50
51 virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
52 virtual uint32_t getSectionType(SectionRef Sec) const = 0;
53
classof(const Binary * v)54 static inline bool classof(const Binary *v) { return v->isELF(); }
55 };
56
57 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
58 public:
59 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
60
61 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
62
63 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
64 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
65 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
66 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
67 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
68 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
69
70 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
71 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
72 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
73
74 protected:
75 ELFFile<ELFT> EF;
76
77 void moveSymbolNext(DataRefImpl &Symb) const override;
78 std::error_code getSymbolName(DataRefImpl Symb,
79 StringRef &Res) const override;
80 std::error_code getSymbolAddress(DataRefImpl Symb,
81 uint64_t &Res) const override;
82 std::error_code getSymbolAlignment(DataRefImpl Symb,
83 uint32_t &Res) const override;
84 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
85 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
86 std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
87 std::error_code getSymbolType(DataRefImpl Symb,
88 SymbolRef::Type &Res) const override;
89 std::error_code getSymbolSection(DataRefImpl Symb,
90 section_iterator &Res) const override;
91
92 void moveSectionNext(DataRefImpl &Sec) const override;
93 std::error_code getSectionName(DataRefImpl Sec,
94 StringRef &Res) const override;
95 uint64_t getSectionAddress(DataRefImpl Sec) const override;
96 uint64_t getSectionSize(DataRefImpl Sec) const override;
97 std::error_code getSectionContents(DataRefImpl Sec,
98 StringRef &Res) const override;
99 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
100 bool isSectionText(DataRefImpl Sec) const override;
101 bool isSectionData(DataRefImpl Sec) const override;
102 bool isSectionBSS(DataRefImpl Sec) const override;
103 bool isSectionVirtual(DataRefImpl Sec) const override;
104 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
105 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
106 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
107 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
108
109 void moveRelocationNext(DataRefImpl &Rel) const override;
110 std::error_code getRelocationAddress(DataRefImpl Rel,
111 uint64_t &Res) const override;
112 std::error_code getRelocationOffset(DataRefImpl Rel,
113 uint64_t &Res) const override;
114 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
115 std::error_code getRelocationType(DataRefImpl Rel,
116 uint64_t &Res) const override;
117 std::error_code
118 getRelocationTypeName(DataRefImpl Rel,
119 SmallVectorImpl<char> &Result) const override;
120 std::error_code
121 getRelocationValueString(DataRefImpl Rel,
122 SmallVectorImpl<char> &Result) const override;
123
124 uint64_t getROffset(DataRefImpl Rel) const;
125 StringRef getRelocationTypeName(uint32_t Type) const;
126
127 /// \brief Get the relocation section that contains \a Rel.
getRelSection(DataRefImpl Rel)128 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
129 return EF.getSection(Rel.d.a);
130 }
131
132 const Elf_Rel *getRel(DataRefImpl Rel) const;
133 const Elf_Rela *getRela(DataRefImpl Rela) const;
134
toELFSymIter(DataRefImpl Symb)135 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
136 bool IsDynamic = Symb.p & 1;
137 if (IsDynamic)
138 return Elf_Sym_Iter(
139 EF.begin_dynamic_symbols().getEntSize(),
140 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
141 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
142 reinterpret_cast<const char *>(Symb.p), IsDynamic);
143 }
144
toDRI(Elf_Sym_Iter Symb)145 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
146 DataRefImpl DRI;
147 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
148 static_cast<uintptr_t>(Symb.isDynamic());
149 return DRI;
150 }
151
toELFShdrIter(DataRefImpl Sec)152 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
153 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
154 reinterpret_cast<const char *>(Sec.p));
155 }
156
toDRI(Elf_Shdr_Iter Sec)157 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
158 DataRefImpl DRI;
159 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
160 return DRI;
161 }
162
toDRI(const Elf_Shdr * Sec)163 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
164 DataRefImpl DRI;
165 DRI.p = reinterpret_cast<uintptr_t>(Sec);
166 return DRI;
167 }
168
toELFDynIter(DataRefImpl Dyn)169 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
170 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
171 reinterpret_cast<const char *>(Dyn.p));
172 }
173
toDRI(Elf_Dyn_Iter Dyn)174 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
175 DataRefImpl DRI;
176 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
177 return DRI;
178 }
179
180 // This flag is used for classof, to distinguish ELFObjectFile from
181 // its subclass. If more subclasses will be created, this flag will
182 // have to become an enum.
183 bool isDyldELFObject;
184
185 public:
186 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
187
188 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
189
190 basic_symbol_iterator symbol_begin_impl() const override;
191 basic_symbol_iterator symbol_end_impl() const override;
192
193 symbol_iterator dynamic_symbol_begin() const;
194 symbol_iterator dynamic_symbol_end() const;
195
196 section_iterator section_begin() const override;
197 section_iterator section_end() const override;
198
199 std::error_code getRelocationAddend(DataRefImpl Rel,
200 int64_t &Res) const override;
201 std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
202 bool &IsDefault) const override;
203
204 uint64_t getSectionFlags(SectionRef Sec) const override;
205 uint32_t getSectionType(SectionRef Sec) const override;
206
207 uint8_t getBytesInAddress() const override;
208 StringRef getFileFormatName() const override;
209 unsigned getArch() const override;
210 StringRef getLoadName() const;
211
getPlatformFlags(unsigned & Result)212 std::error_code getPlatformFlags(unsigned &Result) const override {
213 Result = EF.getHeader()->e_flags;
214 return object_error::success;
215 }
216
getELFFile()217 const ELFFile<ELFT> *getELFFile() const { return &EF; }
218
isDyldType()219 bool isDyldType() const { return isDyldELFObject; }
classof(const Binary * v)220 static inline bool classof(const Binary *v) {
221 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
222 ELFT::Is64Bits);
223 }
224
225 std::pair<symbol_iterator, symbol_iterator>
226 getELFDynamicSymbolIterators() const override;
227
228 bool isRelocatableObject() const override;
229 };
230
231 // Use an alignment of 2 for the typedefs since that is the worst case for
232 // ELF files in archives.
233 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
234 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
235 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
236 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
237
238 template <class ELFT>
moveSymbolNext(DataRefImpl & Symb)239 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
240 Symb = toDRI(++toELFSymIter(Symb));
241 }
242
243 template <class ELFT>
getSymbolName(DataRefImpl Symb,StringRef & Result)244 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
245 StringRef &Result) const {
246 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
247 if (!Name)
248 return Name.getError();
249 Result = *Name;
250 return object_error::success;
251 }
252
253 template <class ELFT>
getSymbolVersion(SymbolRef SymRef,StringRef & Version,bool & IsDefault)254 std::error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
255 StringRef &Version,
256 bool &IsDefault) const {
257 DataRefImpl Symb = SymRef.getRawDataRefImpl();
258 const Elf_Sym *symb = getSymbol(Symb);
259 ErrorOr<StringRef> Ver =
260 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
261 if (!Ver)
262 return Ver.getError();
263 Version = *Ver;
264 return object_error::success;
265 }
266
267 template <class ELFT>
getSectionFlags(SectionRef Sec)268 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
269 DataRefImpl DRI = Sec.getRawDataRefImpl();
270 return toELFShdrIter(DRI)->sh_flags;
271 }
272
273 template <class ELFT>
getSectionType(SectionRef Sec)274 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
275 DataRefImpl DRI = Sec.getRawDataRefImpl();
276 return toELFShdrIter(DRI)->sh_type;
277 }
278
279 template <class ELFT>
getSymbolAddress(DataRefImpl Symb,uint64_t & Result)280 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
281 uint64_t &Result) const {
282 const Elf_Sym *ESym = getSymbol(Symb);
283 switch (EF.getSymbolTableIndex(ESym)) {
284 case ELF::SHN_COMMON:
285 case ELF::SHN_UNDEF:
286 Result = UnknownAddressOrSize;
287 return object_error::success;
288 case ELF::SHN_ABS:
289 Result = ESym->st_value;
290 return object_error::success;
291 default:
292 break;
293 }
294
295 const Elf_Ehdr *Header = EF.getHeader();
296 Result = ESym->st_value;
297
298 // Clear the ARM/Thumb or microMIPS indicator flag.
299 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
300 ESym->getType() == ELF::STT_FUNC)
301 Result &= ~1;
302
303 if (Header->e_type == ELF::ET_REL)
304 Result += EF.getSection(ESym)->sh_addr;
305
306 return object_error::success;
307 }
308
309 template <class ELFT>
getSymbolAlignment(DataRefImpl Symb,uint32_t & Res)310 std::error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
311 uint32_t &Res) const {
312 Elf_Sym_Iter Sym = toELFSymIter(Symb);
313 if (Sym->st_shndx == ELF::SHN_COMMON)
314 Res = Sym->st_value;
315 else
316 Res = 0;
317 return object_error::success;
318 }
319
320 template <class ELFT>
getSymbolSize(DataRefImpl Symb,uint64_t & Result)321 std::error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
322 uint64_t &Result) const {
323 Result = toELFSymIter(Symb)->st_size;
324 return object_error::success;
325 }
326
327 template <class ELFT>
getSymbolOther(DataRefImpl Symb,uint8_t & Result)328 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
329 uint8_t &Result) const {
330 Result = toELFSymIter(Symb)->st_other;
331 return object_error::success;
332 }
333
334 template <class ELFT>
335 std::error_code
getSymbolType(DataRefImpl Symb,SymbolRef::Type & Result)336 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
337 SymbolRef::Type &Result) const {
338 const Elf_Sym *ESym = getSymbol(Symb);
339
340 switch (ESym->getType()) {
341 case ELF::STT_NOTYPE:
342 Result = SymbolRef::ST_Unknown;
343 break;
344 case ELF::STT_SECTION:
345 Result = SymbolRef::ST_Debug;
346 break;
347 case ELF::STT_FILE:
348 Result = SymbolRef::ST_File;
349 break;
350 case ELF::STT_FUNC:
351 Result = SymbolRef::ST_Function;
352 break;
353 case ELF::STT_OBJECT:
354 case ELF::STT_COMMON:
355 case ELF::STT_TLS:
356 Result = SymbolRef::ST_Data;
357 break;
358 default:
359 Result = SymbolRef::ST_Other;
360 break;
361 }
362 return object_error::success;
363 }
364
365 template <class ELFT>
getSymbolFlags(DataRefImpl Symb)366 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
367 Elf_Sym_Iter EIter = toELFSymIter(Symb);
368 const Elf_Sym *ESym = &*EIter;
369
370 uint32_t Result = SymbolRef::SF_None;
371
372 if (ESym->getBinding() != ELF::STB_LOCAL)
373 Result |= SymbolRef::SF_Global;
374
375 if (ESym->getBinding() == ELF::STB_WEAK)
376 Result |= SymbolRef::SF_Weak;
377
378 if (ESym->st_shndx == ELF::SHN_ABS)
379 Result |= SymbolRef::SF_Absolute;
380
381 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
382 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
383 Result |= SymbolRef::SF_FormatSpecific;
384
385 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
386 Result |= SymbolRef::SF_Undefined;
387
388 if (ESym->getType() == ELF::STT_COMMON ||
389 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
390 Result |= SymbolRef::SF_Common;
391
392 return Result;
393 }
394
395 template <class ELFT>
396 std::error_code
getSymbolSection(DataRefImpl Symb,section_iterator & Res)397 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
398 section_iterator &Res) const {
399 const Elf_Sym *ESym = getSymbol(Symb);
400 const Elf_Shdr *ESec = EF.getSection(ESym);
401 if (!ESec)
402 Res = section_end();
403 else {
404 DataRefImpl Sec;
405 Sec.p = reinterpret_cast<intptr_t>(ESec);
406 Res = section_iterator(SectionRef(Sec, this));
407 }
408 return object_error::success;
409 }
410
411 template <class ELFT>
moveSectionNext(DataRefImpl & Sec)412 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
413 Sec = toDRI(++toELFShdrIter(Sec));
414 }
415
416 template <class ELFT>
getSectionName(DataRefImpl Sec,StringRef & Result)417 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
418 StringRef &Result) const {
419 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
420 if (!Name)
421 return Name.getError();
422 Result = *Name;
423 return object_error::success;
424 }
425
426 template <class ELFT>
getSectionAddress(DataRefImpl Sec)427 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
428 return toELFShdrIter(Sec)->sh_addr;
429 }
430
431 template <class ELFT>
getSectionSize(DataRefImpl Sec)432 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
433 return toELFShdrIter(Sec)->sh_size;
434 }
435
436 template <class ELFT>
437 std::error_code
getSectionContents(DataRefImpl Sec,StringRef & Result)438 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
439 StringRef &Result) const {
440 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
441 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
442 return object_error::success;
443 }
444
445 template <class ELFT>
getSectionAlignment(DataRefImpl Sec)446 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
447 return toELFShdrIter(Sec)->sh_addralign;
448 }
449
450 template <class ELFT>
isSectionText(DataRefImpl Sec)451 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
452 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
453 }
454
455 template <class ELFT>
isSectionData(DataRefImpl Sec)456 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
457 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
458 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
459 EShdr->sh_type == ELF::SHT_PROGBITS;
460 }
461
462 template <class ELFT>
isSectionBSS(DataRefImpl Sec)463 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
464 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
465 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
466 EShdr->sh_type == ELF::SHT_NOBITS;
467 }
468
469 template <class ELFT>
isSectionVirtual(DataRefImpl Sec)470 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
471 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
472 }
473
474 template <class ELFT>
sectionContainsSymbol(DataRefImpl Sec,DataRefImpl Symb)475 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
476 DataRefImpl Symb) const {
477 Elf_Sym_Iter ESym = toELFSymIter(Symb);
478
479 uintX_t Index = ESym->st_shndx;
480 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
481
482 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
483 }
484
485 template <class ELFT>
486 relocation_iterator
section_rel_begin(DataRefImpl Sec)487 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
488 DataRefImpl RelData;
489 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
490 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
491 RelData.d.b = 0;
492 return relocation_iterator(RelocationRef(RelData, this));
493 }
494
495 template <class ELFT>
496 relocation_iterator
section_rel_end(DataRefImpl Sec)497 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
498 DataRefImpl RelData;
499 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
500 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
501 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
502 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
503 RelData.d.b = 0;
504 else
505 RelData.d.b = S->sh_size / S->sh_entsize;
506
507 return relocation_iterator(RelocationRef(RelData, this));
508 }
509
510 template <class ELFT>
511 section_iterator
getRelocatedSection(DataRefImpl Sec)512 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
513 if (EF.getHeader()->e_type != ELF::ET_REL)
514 return section_end();
515
516 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
517 uintX_t Type = EShdr->sh_type;
518 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
519 return section_end();
520
521 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
522 return section_iterator(SectionRef(toDRI(R), this));
523 }
524
525 // Relocations
526 template <class ELFT>
moveRelocationNext(DataRefImpl & Rel)527 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
528 ++Rel.d.b;
529 }
530
531 template <class ELFT>
532 symbol_iterator
getRelocationSymbol(DataRefImpl Rel)533 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
534 uint32_t symbolIdx;
535 const Elf_Shdr *sec = getRelSection(Rel);
536 switch (sec->sh_type) {
537 default:
538 report_fatal_error("Invalid section type in Rel!");
539 case ELF::SHT_REL: {
540 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
541 break;
542 }
543 case ELF::SHT_RELA: {
544 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
545 break;
546 }
547 }
548 if (!symbolIdx)
549 return symbol_end();
550
551 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
552
553 DataRefImpl SymbolData;
554 switch (SymSec->sh_type) {
555 default:
556 report_fatal_error("Invalid symbol table section type!");
557 case ELF::SHT_SYMTAB:
558 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
559 break;
560 case ELF::SHT_DYNSYM:
561 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
562 break;
563 }
564
565 return symbol_iterator(SymbolRef(SymbolData, this));
566 }
567
568 template <class ELFT>
569 std::error_code
getRelocationAddress(DataRefImpl Rel,uint64_t & Result)570 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
571 uint64_t &Result) const {
572 uint64_t ROffset = getROffset(Rel);
573 const Elf_Ehdr *Header = EF.getHeader();
574
575 if (Header->e_type == ELF::ET_REL) {
576 const Elf_Shdr *RelocationSec = getRelSection(Rel);
577 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
578 Result = ROffset + RelocatedSec->sh_addr;
579 } else {
580 Result = ROffset;
581 }
582
583 return object_error::success;
584 }
585
586 template <class ELFT>
587 std::error_code
getRelocationOffset(DataRefImpl Rel,uint64_t & Result)588 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
589 uint64_t &Result) const {
590 assert(EF.getHeader()->e_type == ELF::ET_REL &&
591 "Only relocatable object files have relocation offsets");
592 Result = getROffset(Rel);
593 return object_error::success;
594 }
595
596 template <class ELFT>
getROffset(DataRefImpl Rel)597 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
598 const Elf_Shdr *sec = getRelSection(Rel);
599 switch (sec->sh_type) {
600 default:
601 report_fatal_error("Invalid section type in Rel!");
602 case ELF::SHT_REL:
603 return getRel(Rel)->r_offset;
604 case ELF::SHT_RELA:
605 return getRela(Rel)->r_offset;
606 }
607 }
608
609 template <class ELFT>
getRelocationType(DataRefImpl Rel,uint64_t & Result)610 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
611 uint64_t &Result) const {
612 const Elf_Shdr *sec = getRelSection(Rel);
613 switch (sec->sh_type) {
614 default:
615 report_fatal_error("Invalid section type in Rel!");
616 case ELF::SHT_REL: {
617 Result = getRel(Rel)->getType(EF.isMips64EL());
618 break;
619 }
620 case ELF::SHT_RELA: {
621 Result = getRela(Rel)->getType(EF.isMips64EL());
622 break;
623 }
624 }
625 return object_error::success;
626 }
627
628 template <class ELFT>
getRelocationTypeName(uint32_t Type)629 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
630 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
631 }
632
633 template <class ELFT>
getRelocationTypeName(DataRefImpl Rel,SmallVectorImpl<char> & Result)634 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
635 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
636 const Elf_Shdr *sec = getRelSection(Rel);
637 uint32_t type;
638 switch (sec->sh_type) {
639 default:
640 return object_error::parse_failed;
641 case ELF::SHT_REL: {
642 type = getRel(Rel)->getType(EF.isMips64EL());
643 break;
644 }
645 case ELF::SHT_RELA: {
646 type = getRela(Rel)->getType(EF.isMips64EL());
647 break;
648 }
649 }
650
651 EF.getRelocationTypeName(type, Result);
652 return object_error::success;
653 }
654
655 template <class ELFT>
656 std::error_code
getRelocationAddend(DataRefImpl Rel,int64_t & Result)657 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
658 int64_t &Result) const {
659 const Elf_Shdr *sec = getRelSection(Rel);
660 switch (sec->sh_type) {
661 default:
662 report_fatal_error("Invalid section type in Rel!");
663 case ELF::SHT_REL: {
664 Result = 0;
665 return object_error::success;
666 }
667 case ELF::SHT_RELA: {
668 Result = getRela(Rel)->r_addend;
669 return object_error::success;
670 }
671 }
672 }
673
674 template <class ELFT>
getRelocationValueString(DataRefImpl Rel,SmallVectorImpl<char> & Result)675 std::error_code ELFObjectFile<ELFT>::getRelocationValueString(
676 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
677 const Elf_Shdr *sec = getRelSection(Rel);
678 uint8_t type;
679 StringRef res;
680 int64_t addend = 0;
681 uint16_t symbol_index = 0;
682 switch (sec->sh_type) {
683 default:
684 return object_error::parse_failed;
685 case ELF::SHT_REL: {
686 type = getRel(Rel)->getType(EF.isMips64EL());
687 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
688 // TODO: Read implicit addend from section data.
689 break;
690 }
691 case ELF::SHT_RELA: {
692 type = getRela(Rel)->getType(EF.isMips64EL());
693 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
694 addend = getRela(Rel)->r_addend;
695 break;
696 }
697 }
698 const Elf_Sym *symb =
699 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
700 ErrorOr<StringRef> SymName =
701 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
702 if (!SymName)
703 return SymName.getError();
704 switch (EF.getHeader()->e_machine) {
705 case ELF::EM_X86_64:
706 switch (type) {
707 case ELF::R_X86_64_PC8:
708 case ELF::R_X86_64_PC16:
709 case ELF::R_X86_64_PC32: {
710 std::string fmtbuf;
711 raw_string_ostream fmt(fmtbuf);
712 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
713 fmt.flush();
714 Result.append(fmtbuf.begin(), fmtbuf.end());
715 } break;
716 case ELF::R_X86_64_8:
717 case ELF::R_X86_64_16:
718 case ELF::R_X86_64_32:
719 case ELF::R_X86_64_32S:
720 case ELF::R_X86_64_64: {
721 std::string fmtbuf;
722 raw_string_ostream fmt(fmtbuf);
723 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
724 fmt.flush();
725 Result.append(fmtbuf.begin(), fmtbuf.end());
726 } break;
727 default:
728 res = "Unknown";
729 }
730 break;
731 case ELF::EM_AARCH64: {
732 std::string fmtbuf;
733 raw_string_ostream fmt(fmtbuf);
734 fmt << *SymName;
735 if (addend != 0)
736 fmt << (addend < 0 ? "" : "+") << addend;
737 fmt.flush();
738 Result.append(fmtbuf.begin(), fmtbuf.end());
739 break;
740 }
741 case ELF::EM_386:
742 case ELF::EM_ARM:
743 case ELF::EM_HEXAGON:
744 case ELF::EM_MIPS:
745 res = *SymName;
746 break;
747 default:
748 res = "Unknown";
749 }
750 if (Result.empty())
751 Result.append(res.begin(), res.end());
752 return object_error::success;
753 }
754
755 template <class ELFT>
756 const typename ELFFile<ELFT>::Elf_Sym *
getSymbol(DataRefImpl Symb)757 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
758 return &*toELFSymIter(Symb);
759 }
760
761 template <class ELFT>
762 const typename ELFObjectFile<ELFT>::Elf_Rel *
getRel(DataRefImpl Rel)763 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
764 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
765 }
766
767 template <class ELFT>
768 const typename ELFObjectFile<ELFT>::Elf_Rela *
getRela(DataRefImpl Rela)769 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
770 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
771 }
772
773 template <class ELFT>
ELFObjectFile(MemoryBufferRef Object,std::error_code & EC)774 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
775 : ELFObjectFileBase(
776 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
777 support::little,
778 ELFT::Is64Bits),
779 Object),
780 EF(Data.getBuffer(), EC) {}
781
782 template <class ELFT>
symbol_begin_impl()783 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
784 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
785 }
786
787 template <class ELFT>
symbol_end_impl()788 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
789 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
790 }
791
792 template <class ELFT>
dynamic_symbol_begin()793 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
794 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
795 }
796
797 template <class ELFT>
dynamic_symbol_end()798 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
799 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
800 }
801
802 template <class ELFT>
section_begin()803 section_iterator ELFObjectFile<ELFT>::section_begin() const {
804 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
805 }
806
807 template <class ELFT>
section_end()808 section_iterator ELFObjectFile<ELFT>::section_end() const {
809 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
810 }
811
812 template <class ELFT>
getLoadName()813 StringRef ELFObjectFile<ELFT>::getLoadName() const {
814 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
815 Elf_Dyn_Iter DE = EF.end_dynamic_table();
816
817 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
818 ++DI;
819
820 if (DI != DE)
821 return EF.getDynamicString(DI->getVal());
822 return "";
823 }
824
825 template <class ELFT>
getBytesInAddress()826 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
827 return ELFT::Is64Bits ? 8 : 4;
828 }
829
830 template <class ELFT>
getFileFormatName()831 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
832 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
833 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
834 case ELF::ELFCLASS32:
835 switch (EF.getHeader()->e_machine) {
836 case ELF::EM_386:
837 return "ELF32-i386";
838 case ELF::EM_X86_64:
839 return "ELF32-x86-64";
840 case ELF::EM_ARM:
841 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
842 case ELF::EM_HEXAGON:
843 return "ELF32-hexagon";
844 case ELF::EM_MIPS:
845 return "ELF32-mips";
846 case ELF::EM_PPC:
847 return "ELF32-ppc";
848 case ELF::EM_SPARC:
849 case ELF::EM_SPARC32PLUS:
850 return "ELF32-sparc";
851 default:
852 return "ELF32-unknown";
853 }
854 case ELF::ELFCLASS64:
855 switch (EF.getHeader()->e_machine) {
856 case ELF::EM_386:
857 return "ELF64-i386";
858 case ELF::EM_X86_64:
859 return "ELF64-x86-64";
860 case ELF::EM_AARCH64:
861 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
862 case ELF::EM_PPC64:
863 return "ELF64-ppc64";
864 case ELF::EM_S390:
865 return "ELF64-s390";
866 case ELF::EM_SPARCV9:
867 return "ELF64-sparc";
868 case ELF::EM_MIPS:
869 return "ELF64-mips";
870 default:
871 return "ELF64-unknown";
872 }
873 default:
874 // FIXME: Proper error handling.
875 report_fatal_error("Invalid ELFCLASS!");
876 }
877 }
878
879 template <class ELFT>
getArch()880 unsigned ELFObjectFile<ELFT>::getArch() const {
881 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
882 switch (EF.getHeader()->e_machine) {
883 case ELF::EM_386:
884 return Triple::x86;
885 case ELF::EM_X86_64:
886 return Triple::x86_64;
887 case ELF::EM_AARCH64:
888 return Triple::aarch64;
889 case ELF::EM_ARM:
890 return Triple::arm;
891 case ELF::EM_HEXAGON:
892 return Triple::hexagon;
893 case ELF::EM_MIPS:
894 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
895 case ELF::ELFCLASS32:
896 return IsLittleEndian ? Triple::mipsel : Triple::mips;
897 case ELF::ELFCLASS64:
898 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
899 default:
900 report_fatal_error("Invalid ELFCLASS!");
901 }
902 case ELF::EM_PPC:
903 return Triple::ppc;
904 case ELF::EM_PPC64:
905 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
906 case ELF::EM_S390:
907 return Triple::systemz;
908
909 case ELF::EM_SPARC:
910 case ELF::EM_SPARC32PLUS:
911 return Triple::sparc;
912 case ELF::EM_SPARCV9:
913 return Triple::sparcv9;
914
915 default:
916 return Triple::UnknownArch;
917 }
918 }
919
920 template <class ELFT>
921 std::pair<symbol_iterator, symbol_iterator>
getELFDynamicSymbolIterators()922 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
923 return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
924 }
925
isRelocatableObject()926 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
927 return EF.getHeader()->e_type == ELF::ET_REL;
928 }
929
getELFRelocationAddend(const RelocationRef R,int64_t & Addend)930 inline std::error_code getELFRelocationAddend(const RelocationRef R,
931 int64_t &Addend) {
932 const ObjectFile *Obj = R.getObjectFile();
933 DataRefImpl DRI = R.getRawDataRefImpl();
934 return cast<ELFObjectFileBase>(Obj)->getRelocationAddend(DRI, Addend);
935 }
936
937 inline std::pair<symbol_iterator, symbol_iterator>
getELFDynamicSymbolIterators(const SymbolicFile * Obj)938 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
939 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
940 }
941
GetELFSymbolVersion(const ObjectFile * Obj,const SymbolRef & Sym,StringRef & Version,bool & IsDefault)942 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
943 const SymbolRef &Sym,
944 StringRef &Version,
945 bool &IsDefault) {
946 return cast<ELFObjectFileBase>(Obj)
947 ->getSymbolVersion(Sym, Version, IsDefault);
948 }
949 }
950 }
951
952 #endif
953