LLVM  11.0.0git
ELF.cpp
Go to the documentation of this file.
1 //===- ELF.cpp - ELF object file implementation ---------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Object/ELF.h"
10 #include "llvm/BinaryFormat/ELF.h"
11 #include "llvm/Support/LEB128.h"
12 
13 using namespace llvm;
14 using namespace object;
15 
16 #define STRINGIFY_ENUM_CASE(ns, name) \
17  case ns::name: \
18  return #name;
19 
20 #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
21 
23  uint32_t Type) {
24  switch (Machine) {
25  case ELF::EM_X86_64:
26  switch (Type) {
27 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
28  default:
29  break;
30  }
31  break;
32  case ELF::EM_386:
33  case ELF::EM_IAMCU:
34  switch (Type) {
35 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
36  default:
37  break;
38  }
39  break;
40  case ELF::EM_MIPS:
41  switch (Type) {
42 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
43  default:
44  break;
45  }
46  break;
47  case ELF::EM_AARCH64:
48  switch (Type) {
49 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
50  default:
51  break;
52  }
53  break;
54  case ELF::EM_ARM:
55  switch (Type) {
56 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
57  default:
58  break;
59  }
60  break;
63  switch (Type) {
64 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
65  default:
66  break;
67  }
68  break;
69  case ELF::EM_AVR:
70  switch (Type) {
71 #include "llvm/BinaryFormat/ELFRelocs/AVR.def"
72  default:
73  break;
74  }
75  break;
76  case ELF::EM_HEXAGON:
77  switch (Type) {
78 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
79  default:
80  break;
81  }
82  break;
83  case ELF::EM_LANAI:
84  switch (Type) {
85 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
86  default:
87  break;
88  }
89  break;
90  case ELF::EM_PPC:
91  switch (Type) {
92 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
93  default:
94  break;
95  }
96  break;
97  case ELF::EM_PPC64:
98  switch (Type) {
99 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
100  default:
101  break;
102  }
103  break;
104  case ELF::EM_RISCV:
105  switch (Type) {
106 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
107  default:
108  break;
109  }
110  break;
111  case ELF::EM_S390:
112  switch (Type) {
113 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
114  default:
115  break;
116  }
117  break;
118  case ELF::EM_SPARC:
119  case ELF::EM_SPARC32PLUS:
120  case ELF::EM_SPARCV9:
121  switch (Type) {
122 #include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
123  default:
124  break;
125  }
126  break;
127  case ELF::EM_AMDGPU:
128  switch (Type) {
129 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
130  default:
131  break;
132  }
133  break;
134  case ELF::EM_BPF:
135  switch (Type) {
136 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
137  default:
138  break;
139  }
140  break;
141  case ELF::EM_MSP430:
142  switch (Type) {
143 #include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
144  default:
145  break;
146  }
147  break;
148  default:
149  break;
150  }
151  return "Unknown";
152 }
153 
154 #undef ELF_RELOC
155 
157  switch (Machine) {
158  case ELF::EM_X86_64:
159  return ELF::R_X86_64_RELATIVE;
160  case ELF::EM_386:
161  case ELF::EM_IAMCU:
162  return ELF::R_386_RELATIVE;
163  case ELF::EM_MIPS:
164  break;
165  case ELF::EM_AARCH64:
166  return ELF::R_AARCH64_RELATIVE;
167  case ELF::EM_ARM:
168  return ELF::R_ARM_RELATIVE;
169  case ELF::EM_ARC_COMPACT:
171  return ELF::R_ARC_RELATIVE;
172  case ELF::EM_AVR:
173  break;
174  case ELF::EM_HEXAGON:
175  return ELF::R_HEX_RELATIVE;
176  case ELF::EM_LANAI:
177  break;
178  case ELF::EM_PPC:
179  break;
180  case ELF::EM_PPC64:
181  return ELF::R_PPC64_RELATIVE;
182  case ELF::EM_RISCV:
183  return ELF::R_RISCV_RELATIVE;
184  case ELF::EM_S390:
185  return ELF::R_390_RELATIVE;
186  case ELF::EM_SPARC:
187  case ELF::EM_SPARC32PLUS:
188  case ELF::EM_SPARCV9:
189  return ELF::R_SPARC_RELATIVE;
190  case ELF::EM_AMDGPU:
191  break;
192  case ELF::EM_BPF:
193  break;
194  default:
195  break;
196  }
197  return 0;
198 }
199 
201  switch (Machine) {
202  case ELF::EM_ARM:
203  switch (Type) {
209  }
210  break;
211  case ELF::EM_HEXAGON:
212  switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
213  break;
214  case ELF::EM_X86_64:
215  switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
216  break;
217  case ELF::EM_MIPS:
218  case ELF::EM_MIPS_RS3_LE:
219  switch (Type) {
224  }
225  break;
226  case ELF::EM_RISCV:
227  switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
228  break;
229  default:
230  break;
231  }
232 
233  switch (Type) {
268  default:
269  return "Unknown";
270  }
271 }
272 
273 template <class ELFT>
276  // This function decodes the contents of an SHT_RELR packed relocation
277  // section.
278  //
279  // Proposal for adding SHT_RELR sections to generic-abi is here:
280  // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
281  //
282  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
283  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
284  //
285  // i.e. start with an address, followed by any number of bitmaps. The address
286  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
287  // relocations each, at subsequent offsets following the last address entry.
288  //
289  // The bitmap entries must have 1 in the least significant bit. The assumption
290  // here is that an address cannot have 1 in lsb. Odd addresses are not
291  // supported.
292  //
293  // Excluding the least significant bit in the bitmap, each non-zero bit in
294  // the bitmap represents a relocation to be applied to a corresponding machine
295  // word that follows the base address word. The second least significant bit
296  // represents the machine word immediately following the initial address, and
297  // each bit that follows represents the next word, in linear order. As such,
298  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
299  // 63 relocations in a 64-bit object.
300  //
301  // This encoding has a couple of interesting properties:
302  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
303  // even means address, odd means bitmap.
304  // 2. Just a simple list of addresses is a valid encoding.
305 
306  Elf_Rela Rela;
307  Rela.r_info = 0;
308  Rela.r_addend = 0;
309  Rela.setType(getRelativeRelocationType(), false);
310  std::vector<Elf_Rela> Relocs;
311 
312  // Word type: uint32_t for Elf32, and uint64_t for Elf64.
313  typedef typename ELFT::uint Word;
314 
315  // Word size in number of bytes.
316  const size_t WordSize = sizeof(Word);
317 
318  // Number of bits used for the relocation offsets bitmap.
319  // These many relative relocations can be encoded in a single entry.
320  const size_t NBits = 8*WordSize - 1;
321 
322  Word Base = 0;
323  for (const Elf_Relr &R : relrs) {
324  Word Entry = R;
325  if ((Entry&1) == 0) {
326  // Even entry: encodes the offset for next relocation.
327  Rela.r_offset = Entry;
328  Relocs.push_back(Rela);
329  // Set base offset for subsequent bitmap entries.
330  Base = Entry + WordSize;
331  continue;
332  }
333 
334  // Odd entry: encodes bitmap for relocations starting at base.
335  Word Offset = Base;
336  while (Entry != 0) {
337  Entry >>= 1;
338  if ((Entry&1) != 0) {
339  Rela.r_offset = Offset;
340  Relocs.push_back(Rela);
341  }
342  Offset += WordSize;
343  }
344 
345  // Advance base offset by NBits words.
346  Base += NBits * WordSize;
347  }
348 
349  return Relocs;
350 }
351 
352 template <class ELFT>
355  // This function reads relocations in Android's packed relocation format,
356  // which is based on SLEB128 and delta encoding.
357  Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
358  if (!ContentsOrErr)
359  return ContentsOrErr.takeError();
360  const uint8_t *Cur = ContentsOrErr->begin();
361  const uint8_t *End = ContentsOrErr->end();
362  if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
363  Cur[2] != 'S' || Cur[3] != '2')
364  return createError("invalid packed relocation header");
365  Cur += 4;
366 
367  const char *ErrStr = nullptr;
368  auto ReadSLEB = [&]() -> int64_t {
369  if (ErrStr)
370  return 0;
371  unsigned Len;
372  int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
373  Cur += Len;
374  return Result;
375  };
376 
377  uint64_t NumRelocs = ReadSLEB();
378  uint64_t Offset = ReadSLEB();
379  uint64_t Addend = 0;
380 
381  if (ErrStr)
382  return createError(ErrStr);
383 
384  std::vector<Elf_Rela> Relocs;
385  Relocs.reserve(NumRelocs);
386  while (NumRelocs) {
387  uint64_t NumRelocsInGroup = ReadSLEB();
388  if (NumRelocsInGroup > NumRelocs)
389  return createError("relocation group unexpectedly large");
390  NumRelocs -= NumRelocsInGroup;
391 
392  uint64_t GroupFlags = ReadSLEB();
393  bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
394  bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
395  bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
396  bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
397 
398  uint64_t GroupOffsetDelta;
399  if (GroupedByOffsetDelta)
400  GroupOffsetDelta = ReadSLEB();
401 
402  uint64_t GroupRInfo;
403  if (GroupedByInfo)
404  GroupRInfo = ReadSLEB();
405 
406  if (GroupedByAddend && GroupHasAddend)
407  Addend += ReadSLEB();
408 
409  if (!GroupHasAddend)
410  Addend = 0;
411 
412  for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
413  Elf_Rela R;
414  Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
415  R.r_offset = Offset;
416  R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
417  if (GroupHasAddend && !GroupedByAddend)
418  Addend += ReadSLEB();
419  R.r_addend = Addend;
420  Relocs.push_back(R);
421 
422  if (ErrStr)
423  return createError(ErrStr);
424  }
425 
426  if (ErrStr)
427  return createError(ErrStr);
428  }
429 
430  return Relocs;
431 }
432 
433 template <class ELFT>
434 std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
435  uint64_t Type) const {
436 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
437  case value: \
438  return #tag;
439 
440 #define DYNAMIC_TAG(n, v)
441  switch (Arch) {
442  case ELF::EM_AARCH64:
443  switch (Type) {
444 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
445 #include "llvm/BinaryFormat/DynamicTags.def"
446 #undef AARCH64_DYNAMIC_TAG
447  }
448  break;
449 
450  case ELF::EM_HEXAGON:
451  switch (Type) {
452 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
453 #include "llvm/BinaryFormat/DynamicTags.def"
454 #undef HEXAGON_DYNAMIC_TAG
455  }
456  break;
457 
458  case ELF::EM_MIPS:
459  switch (Type) {
460 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
461 #include "llvm/BinaryFormat/DynamicTags.def"
462 #undef MIPS_DYNAMIC_TAG
463  }
464  break;
465 
466  case ELF::EM_PPC64:
467  switch (Type) {
468 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
469 #include "llvm/BinaryFormat/DynamicTags.def"
470 #undef PPC64_DYNAMIC_TAG
471  }
472  break;
473  }
474 #undef DYNAMIC_TAG
475  switch (Type) {
476 // Now handle all dynamic tags except the architecture specific ones
477 #define AARCH64_DYNAMIC_TAG(name, value)
478 #define MIPS_DYNAMIC_TAG(name, value)
479 #define HEXAGON_DYNAMIC_TAG(name, value)
480 #define PPC64_DYNAMIC_TAG(name, value)
481 // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
482 #define DYNAMIC_TAG_MARKER(name, value)
483 #define DYNAMIC_TAG(name, value) case value: return #name;
484 #include "llvm/BinaryFormat/DynamicTags.def"
485 #undef DYNAMIC_TAG
486 #undef AARCH64_DYNAMIC_TAG
487 #undef MIPS_DYNAMIC_TAG
488 #undef HEXAGON_DYNAMIC_TAG
489 #undef PPC64_DYNAMIC_TAG
490 #undef DYNAMIC_TAG_MARKER
491 #undef DYNAMIC_STRINGIFY_ENUM
492  default:
493  return "<unknown:>0x" + utohexstr(Type, true);
494  }
495 }
496 
497 template <class ELFT>
498 std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
499  return getDynamicTagAsString(getHeader()->e_machine, Type);
500 }
501 
502 template <class ELFT>
504  ArrayRef<Elf_Dyn> Dyn;
505 
506  auto ProgramHeadersOrError = program_headers();
507  if (!ProgramHeadersOrError)
508  return ProgramHeadersOrError.takeError();
509 
510  for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
511  if (Phdr.p_type == ELF::PT_DYNAMIC) {
512  Dyn = makeArrayRef(
513  reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
514  Phdr.p_filesz / sizeof(Elf_Dyn));
515  break;
516  }
517  }
518 
519  // If we can't find the dynamic section in the program headers, we just fall
520  // back on the sections.
521  if (Dyn.empty()) {
522  auto SectionsOrError = sections();
523  if (!SectionsOrError)
524  return SectionsOrError.takeError();
525 
526  for (const Elf_Shdr &Sec : *SectionsOrError) {
527  if (Sec.sh_type == ELF::SHT_DYNAMIC) {
528  Expected<ArrayRef<Elf_Dyn>> DynOrError =
529  getSectionContentsAsArray<Elf_Dyn>(&Sec);
530  if (!DynOrError)
531  return DynOrError.takeError();
532  Dyn = *DynOrError;
533  break;
534  }
535  }
536 
537  if (!Dyn.data())
538  return ArrayRef<Elf_Dyn>();
539  }
540 
541  if (Dyn.empty())
542  // TODO: this error is untested.
543  return createError("invalid empty dynamic section");
544 
545  if (Dyn.back().d_tag != ELF::DT_NULL)
546  // TODO: this error is untested.
547  return createError("dynamic sections must be DT_NULL terminated");
548 
549  return Dyn;
550 }
551 
552 template <class ELFT>
554  auto ProgramHeadersOrError = program_headers();
555  if (!ProgramHeadersOrError)
556  return ProgramHeadersOrError.takeError();
557 
559 
560  for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
561  if (Phdr.p_type == ELF::PT_LOAD)
562  LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
563 
564  const Elf_Phdr *const *I =
565  std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
566  [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
567  return VAddr < Phdr->p_vaddr;
568  });
569 
570  if (I == LoadSegments.begin())
571  return createError("virtual address is not in any segment: 0x" +
572  Twine::utohexstr(VAddr));
573  --I;
574  const Elf_Phdr &Phdr = **I;
575  uint64_t Delta = VAddr - Phdr.p_vaddr;
576  if (Delta >= Phdr.p_filesz)
577  return createError("virtual address is not in any segment: 0x" +
578  Twine::utohexstr(VAddr));
579 
580  uint64_t Offset = Phdr.p_offset + Delta;
581  if (Offset >= getBufSize())
582  return createError("can't map virtual address 0x" +
583  Twine::utohexstr(VAddr) + " to the segment with index " +
584  Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
585  ": the segment ends at 0x" +
586  Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
587  ", which is greater than the file size (0x" +
588  Twine::utohexstr(getBufSize()) + ")");
589 
590  return base() + Offset;
591 }
592 
593 template class llvm::object::ELFFile<ELF32LE>;
594 template class llvm::object::ELFFile<ELF32BE>;
595 template class llvm::object::ELFFile<ELF64LE>;
596 template class llvm::object::ELFFile<ELF64BE>;
typename ELFT::Dyn Elf_Dyn
Definition: ELF.h:79
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:165
This class represents lattice values for constants.
Definition: AllocatorList.h:23
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
Expected< std::vector< Elf_Rela > > decode_relrs(Elf_Relr_Range relrs) const
Definition: ELF.cpp:275
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr) const
Definition: ELF.cpp:553
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:458
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
Definition: LEB128.h:161
support::ulittle32_t Word
Definition: IRSymtab.h:51
typename ELFT::Phdr Elf_Phdr
Definition: ELF.h:80
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
uint32_t getELFRelativeRelocationType(uint32_t Machine)
Definition: ELF.cpp:156
static Error createError(const Twine &Err)
Definition: ELF.h:47
typename ELFT::Relr Elf_Relr
Definition: ELF.h:83
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1599
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:365
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
Definition: ELF.cpp:434
typename ELFT::RelrRange Elf_Relr_Range
Definition: ELF.h:99
typename ELFT::Rela Elf_Rela
Definition: ELF.h:82
const T * data() const
Definition: ArrayRef.h:153
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:22
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
#define STRINGIFY_ENUM_CASE(ns, name)
Definition: ELF.cpp:16
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:387
Expected< std::vector< Elf_Rela > > android_relas(const Elf_Shdr *Sec) const
Definition: ELF.cpp:354
#define I(x, y, z)
Definition: MD5.cpp:59
Expected< Elf_Dyn_Range > dynamicEntries() const
Definition: ELF.cpp:503
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
typename ELFT::Shdr Elf_Shdr
Definition: ELF.h:77
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:151
std::string utohexstr(uint64_t X, bool LowerCase=false)
Definition: StringExtras.h:132