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
13using namespace llvm;
14using 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
22StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
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;
61  case ELF::EM_ARC_COMPACT:
62  case ELF::EM_ARC_COMPACT2:
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  case ELF::EM_VE:
149    switch (Type) {
150#include "llvm/BinaryFormat/ELFRelocs/VE.def"
151    default:
152      break;
153    }
154    break;
155  default:
156    break;
157  }
158  return "Unknown";
159}
160
161#undef ELF_RELOC
162
163uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
164  switch (Machine) {
165  case ELF::EM_X86_64:
166    return ELF::R_X86_64_RELATIVE;
167  case ELF::EM_386:
168  case ELF::EM_IAMCU:
169    return ELF::R_386_RELATIVE;
170  case ELF::EM_MIPS:
171    break;
172  case ELF::EM_AARCH64:
173    return ELF::R_AARCH64_RELATIVE;
174  case ELF::EM_ARM:
175    return ELF::R_ARM_RELATIVE;
176  case ELF::EM_ARC_COMPACT:
177  case ELF::EM_ARC_COMPACT2:
178    return ELF::R_ARC_RELATIVE;
179  case ELF::EM_AVR:
180    break;
181  case ELF::EM_HEXAGON:
182    return ELF::R_HEX_RELATIVE;
183  case ELF::EM_LANAI:
184    break;
185  case ELF::EM_PPC:
186    break;
187  case ELF::EM_PPC64:
188    return ELF::R_PPC64_RELATIVE;
189  case ELF::EM_RISCV:
190    return ELF::R_RISCV_RELATIVE;
191  case ELF::EM_S390:
192    return ELF::R_390_RELATIVE;
193  case ELF::EM_SPARC:
194  case ELF::EM_SPARC32PLUS:
195  case ELF::EM_SPARCV9:
196    return ELF::R_SPARC_RELATIVE;
197  case ELF::EM_AMDGPU:
198    break;
199  case ELF::EM_BPF:
200    break;
201  default:
202    break;
203  }
204  return 0;
205}
206
207StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
208  switch (Machine) {
209  case ELF::EM_ARM:
210    switch (Type) {
211      STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
212      STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
213      STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
214      STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
215      STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
216    }
217    break;
218  case ELF::EM_HEXAGON:
219    switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
220    break;
221  case ELF::EM_X86_64:
222    switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
223    break;
224  case ELF::EM_MIPS:
225  case ELF::EM_MIPS_RS3_LE:
226    switch (Type) {
227      STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
228      STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
229      STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
230      STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
231    }
232    break;
233  case ELF::EM_RISCV:
234    switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
235    break;
236  default:
237    break;
238  }
239
240  switch (Type) {
241    STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
242    STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
243    STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
244    STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
245    STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
246    STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
247    STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
248    STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
249    STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
250    STRINGIFY_ENUM_CASE(ELF, SHT_REL);
251    STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
252    STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
253    STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
254    STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
255    STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
256    STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
257    STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
258    STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
259    STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
260    STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
261    STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
262    STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
263    STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
264    STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
265    STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
266    STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
267    STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
268    STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
269    STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
270    STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
271    STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
272    STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
273    STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
274    STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
275  default:
276    return "Unknown";
277  }
278}
279
280template <class ELFT>
281Expected<std::vector<typename ELFT::Rela>>
282ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
283  // This function decodes the contents of an SHT_RELR packed relocation
284  // section.
285  //
286  // Proposal for adding SHT_RELR sections to generic-abi is here:
287  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
288  //
289  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
290  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
291  //
292  // i.e. start with an address, followed by any number of bitmaps. The address
293  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
294  // relocations each, at subsequent offsets following the last address entry.
295  //
296  // The bitmap entries must have 1 in the least significant bit. The assumption
297  // here is that an address cannot have 1 in lsb. Odd addresses are not
298  // supported.
299  //
300  // Excluding the least significant bit in the bitmap, each non-zero bit in
301  // the bitmap represents a relocation to be applied to a corresponding machine
302  // word that follows the base address word. The second least significant bit
303  // represents the machine word immediately following the initial address, and
304  // each bit that follows represents the next word, in linear order. As such,
305  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
306  // 63 relocations in a 64-bit object.
307  //
308  // This encoding has a couple of interesting properties:
309  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
310  //    even means address, odd means bitmap.
311  // 2. Just a simple list of addresses is a valid encoding.
312
313  Elf_Rela Rela;
314  Rela.r_info = 0;
315  Rela.r_addend = 0;
316  Rela.setType(getRelativeRelocationType(), false);
317  std::vector<Elf_Rela> Relocs;
318
319  // Word type: uint32_t for Elf32, and uint64_t for Elf64.
320  typedef typename ELFT::uint Word;
321
322  // Word size in number of bytes.
323  const size_t WordSize = sizeof(Word);
324
325  // Number of bits used for the relocation offsets bitmap.
326  // These many relative relocations can be encoded in a single entry.
327  const size_t NBits = 8*WordSize - 1;
328
329  Word Base = 0;
330  for (const Elf_Relr &R : relrs) {
331    Word Entry = R;
332    if ((Entry&1) == 0) {
333      // Even entry: encodes the offset for next relocation.
334      Rela.r_offset = Entry;
335      Relocs.push_back(Rela);
336      // Set base offset for subsequent bitmap entries.
337      Base = Entry + WordSize;
338      continue;
339    }
340
341    // Odd entry: encodes bitmap for relocations starting at base.
342    Word Offset = Base;
343    while (Entry != 0) {
344      Entry >>= 1;
345      if ((Entry&1) != 0) {
346        Rela.r_offset = Offset;
347        Relocs.push_back(Rela);
348      }
349      Offset += WordSize;
350    }
351
352    // Advance base offset by NBits words.
353    Base += NBits * WordSize;
354  }
355
356  return Relocs;
357}
358
359template <class ELFT>
360Expected<std::vector<typename ELFT::Rela>>
361ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
362  // This function reads relocations in Android's packed relocation format,
363  // which is based on SLEB128 and delta encoding.
364  Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
365  if (!ContentsOrErr)
366    return ContentsOrErr.takeError();
367  const uint8_t *Cur = ContentsOrErr->begin();
368  const uint8_t *End = ContentsOrErr->end();
369  if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
370      Cur[2] != 'S' || Cur[3] != '2')
371    return createError("invalid packed relocation header");
372  Cur += 4;
373
374  const char *ErrStr = nullptr;
375  auto ReadSLEB = [&]() -> int64_t {
376    if (ErrStr)
377      return 0;
378    unsigned Len;
379    int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
380    Cur += Len;
381    return Result;
382  };
383
384  uint64_t NumRelocs = ReadSLEB();
385  uint64_t Offset = ReadSLEB();
386  uint64_t Addend = 0;
387
388  if (ErrStr)
389    return createError(ErrStr);
390
391  std::vector<Elf_Rela> Relocs;
392  Relocs.reserve(NumRelocs);
393  while (NumRelocs) {
394    uint64_t NumRelocsInGroup = ReadSLEB();
395    if (NumRelocsInGroup > NumRelocs)
396      return createError("relocation group unexpectedly large");
397    NumRelocs -= NumRelocsInGroup;
398
399    uint64_t GroupFlags = ReadSLEB();
400    bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
401    bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
402    bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
403    bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
404
405    uint64_t GroupOffsetDelta;
406    if (GroupedByOffsetDelta)
407      GroupOffsetDelta = ReadSLEB();
408
409    uint64_t GroupRInfo;
410    if (GroupedByInfo)
411      GroupRInfo = ReadSLEB();
412
413    if (GroupedByAddend && GroupHasAddend)
414      Addend += ReadSLEB();
415
416    if (!GroupHasAddend)
417      Addend = 0;
418
419    for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
420      Elf_Rela R;
421      Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
422      R.r_offset = Offset;
423      R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
424      if (GroupHasAddend && !GroupedByAddend)
425        Addend += ReadSLEB();
426      R.r_addend = Addend;
427      Relocs.push_back(R);
428
429      if (ErrStr)
430        return createError(ErrStr);
431    }
432
433    if (ErrStr)
434      return createError(ErrStr);
435  }
436
437  return Relocs;
438}
439
440template <class ELFT>
441std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
442                                                 uint64_t Type) const {
443#define DYNAMIC_STRINGIFY_ENUM(tag, value)                                     \
444  case value:                                                                  \
445    return #tag;
446
447#define DYNAMIC_TAG(n, v)
448  switch (Arch) {
449  case ELF::EM_AARCH64:
450    switch (Type) {
451#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
452#include "llvm/BinaryFormat/DynamicTags.def"
453#undef AARCH64_DYNAMIC_TAG
454    }
455    break;
456
457  case ELF::EM_HEXAGON:
458    switch (Type) {
459#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
460#include "llvm/BinaryFormat/DynamicTags.def"
461#undef HEXAGON_DYNAMIC_TAG
462    }
463    break;
464
465  case ELF::EM_MIPS:
466    switch (Type) {
467#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
468#include "llvm/BinaryFormat/DynamicTags.def"
469#undef MIPS_DYNAMIC_TAG
470    }
471    break;
472
473  case ELF::EM_PPC64:
474    switch (Type) {
475#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
476#include "llvm/BinaryFormat/DynamicTags.def"
477#undef PPC64_DYNAMIC_TAG
478    }
479    break;
480  }
481#undef DYNAMIC_TAG
482  switch (Type) {
483// Now handle all dynamic tags except the architecture specific ones
484#define AARCH64_DYNAMIC_TAG(name, value)
485#define MIPS_DYNAMIC_TAG(name, value)
486#define HEXAGON_DYNAMIC_TAG(name, value)
487#define PPC64_DYNAMIC_TAG(name, value)
488// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
489#define DYNAMIC_TAG_MARKER(name, value)
490#define DYNAMIC_TAG(name, value) case value: return #name;
491#include "llvm/BinaryFormat/DynamicTags.def"
492#undef DYNAMIC_TAG
493#undef AARCH64_DYNAMIC_TAG
494#undef MIPS_DYNAMIC_TAG
495#undef HEXAGON_DYNAMIC_TAG
496#undef PPC64_DYNAMIC_TAG
497#undef DYNAMIC_TAG_MARKER
498#undef DYNAMIC_STRINGIFY_ENUM
499  default:
500    return "<unknown:>0x" + utohexstr(Type, true);
501  }
502}
503
504template <class ELFT>
505std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
506  return getDynamicTagAsString(getHeader()->e_machine, Type);
507}
508
509template <class ELFT>
510Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
511  ArrayRef<Elf_Dyn> Dyn;
512
513  auto ProgramHeadersOrError = program_headers();
514  if (!ProgramHeadersOrError)
515    return ProgramHeadersOrError.takeError();
516
517  for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
518    if (Phdr.p_type == ELF::PT_DYNAMIC) {
519      Dyn = makeArrayRef(
520          reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
521          Phdr.p_filesz / sizeof(Elf_Dyn));
522      break;
523    }
524  }
525
526  // If we can't find the dynamic section in the program headers, we just fall
527  // back on the sections.
528  if (Dyn.empty()) {
529    auto SectionsOrError = sections();
530    if (!SectionsOrError)
531      return SectionsOrError.takeError();
532
533    for (const Elf_Shdr &Sec : *SectionsOrError) {
534      if (Sec.sh_type == ELF::SHT_DYNAMIC) {
535        Expected<ArrayRef<Elf_Dyn>> DynOrError =
536            getSectionContentsAsArray<Elf_Dyn>(&Sec);
537        if (!DynOrError)
538          return DynOrError.takeError();
539        Dyn = *DynOrError;
540        break;
541      }
542    }
543
544    if (!Dyn.data())
545      return ArrayRef<Elf_Dyn>();
546  }
547
548  if (Dyn.empty())
549    // TODO: this error is untested.
550    return createError("invalid empty dynamic section");
551
552  if (Dyn.back().d_tag != ELF::DT_NULL)
553    // TODO: this error is untested.
554    return createError("dynamic sections must be DT_NULL terminated");
555
556  return Dyn;
557}
558
559template <class ELFT>
560Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
561  auto ProgramHeadersOrError = program_headers();
562  if (!ProgramHeadersOrError)
563    return ProgramHeadersOrError.takeError();
564
565  llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
566
567  for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
568    if (Phdr.p_type == ELF::PT_LOAD)
569      LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
570
571  const Elf_Phdr *const *I =
572      std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
573                       [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
574                         return VAddr < Phdr->p_vaddr;
575                       });
576
577  if (I == LoadSegments.begin())
578    return createError("virtual address is not in any segment: 0x" +
579                       Twine::utohexstr(VAddr));
580  --I;
581  const Elf_Phdr &Phdr = **I;
582  uint64_t Delta = VAddr - Phdr.p_vaddr;
583  if (Delta >= Phdr.p_filesz)
584    return createError("virtual address is not in any segment: 0x" +
585                       Twine::utohexstr(VAddr));
586
587  uint64_t Offset = Phdr.p_offset + Delta;
588  if (Offset >= getBufSize())
589    return createError("can't map virtual address 0x" +
590                       Twine::utohexstr(VAddr) + " to the segment with index " +
591                       Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
592                       ": the segment ends at 0x" +
593                       Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
594                       ", which is greater than the file size (0x" +
595                       Twine::utohexstr(getBufSize()) + ")");
596
597  return base() + Offset;
598}
599
600template class llvm::object::ELFFile<ELF32LE>;
601template class llvm::object::ELFFile<ELF32BE>;
602template class llvm::object::ELFFile<ELF64LE>;
603template class llvm::object::ELFFile<ELF64BE>;
604