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