1 //===- ELF.cpp - ELF object file implementation ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Object/ELF.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/Support/LEB128.h"
15 using namespace object;
17 #define STRINGIFY_ENUM_CASE(ns, name) \
21 #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
23 StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
28 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
36 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
43 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
50 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
57 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
62 case ELF::EM_ARC_COMPACT:
63 case ELF::EM_ARC_COMPACT2:
65 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
72 #include "llvm/BinaryFormat/ELFRelocs/AVR.def"
79 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
86 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
93 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
100 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
107 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
114 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
120 case ELF::EM_SPARC32PLUS:
121 case ELF::EM_SPARCV9:
123 #include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
130 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
137 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
144 #include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
157 uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
160 return ELF::R_X86_64_RELATIVE;
163 return ELF::R_386_RELATIVE;
166 case ELF::EM_AARCH64:
167 return ELF::R_AARCH64_RELATIVE;
169 return ELF::R_ARM_RELATIVE;
170 case ELF::EM_ARC_COMPACT:
171 case ELF::EM_ARC_COMPACT2:
172 return ELF::R_ARC_RELATIVE;
175 case ELF::EM_HEXAGON:
176 return ELF::R_HEX_RELATIVE;
182 return ELF::R_PPC64_RELATIVE;
184 return ELF::R_RISCV_RELATIVE;
186 return ELF::R_390_RELATIVE;
188 case ELF::EM_SPARC32PLUS:
189 case ELF::EM_SPARCV9:
190 return ELF::R_SPARC_RELATIVE;
201 StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
205 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
206 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
207 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
208 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
209 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
212 case ELF::EM_HEXAGON:
213 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
216 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
219 case ELF::EM_MIPS_RS3_LE:
221 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
222 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
223 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
224 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
232 STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
233 STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
234 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
235 STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
236 STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
237 STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
238 STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
239 STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
240 STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
241 STRINGIFY_ENUM_CASE(ELF, SHT_REL);
242 STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
243 STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
244 STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
245 STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
246 STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
247 STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
248 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
249 STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
250 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
251 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
252 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
253 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
254 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
255 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
256 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
257 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
258 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
259 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
260 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
261 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
267 template <class ELFT>
268 Expected<std::vector<typename ELFT::Rela>>
269 ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
270 // This function decodes the contents of an SHT_RELR packed relocation
273 // Proposal for adding SHT_RELR sections to generic-abi is here:
274 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
276 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
277 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
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.
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
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.
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.
303 Rela.setType(getRelativeRelocationType(), false);
304 std::vector<Elf_Rela> Relocs;
306 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
307 typedef typename ELFT::uint Word;
309 // Word size in number of bytes.
310 const size_t WordSize = sizeof(Word);
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;
317 for (const Elf_Relr &R : relrs) {
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;
328 // Odd entry: encodes bitmap for relocations starting at base.
332 if ((Entry&1) != 0) {
333 Rela.r_offset = Offset;
334 Relocs.push_back(Rela);
339 // Advance base offset by NBits words.
340 Base += NBits * WordSize;
346 template <class ELFT>
347 Expected<std::vector<typename ELFT::Rela>>
348 ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
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);
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");
361 const char *ErrStr = nullptr;
362 auto ReadSLEB = [&]() -> int64_t {
366 int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
371 uint64_t NumRelocs = ReadSLEB();
372 uint64_t Offset = ReadSLEB();
376 return createError(ErrStr);
378 std::vector<Elf_Rela> Relocs;
379 Relocs.reserve(NumRelocs);
381 uint64_t NumRelocsInGroup = ReadSLEB();
382 if (NumRelocsInGroup > NumRelocs)
383 return createError("relocation group unexpectedly large");
384 NumRelocs -= NumRelocsInGroup;
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;
392 uint64_t GroupOffsetDelta;
393 if (GroupedByOffsetDelta)
394 GroupOffsetDelta = ReadSLEB();
398 GroupRInfo = ReadSLEB();
400 if (GroupedByAddend && GroupHasAddend)
401 Addend += ReadSLEB();
406 for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
408 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
410 R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
411 if (GroupHasAddend && !GroupedByAddend)
412 Addend += ReadSLEB();
417 return createError(ErrStr);
421 return createError(ErrStr);
427 template <class ELFT>
428 const char *ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
429 uint64_t Type) const {
430 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
434 #define DYNAMIC_TAG(n, v)
436 case ELF::EM_HEXAGON:
438 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
439 #include "llvm/BinaryFormat/DynamicTags.def"
440 #undef HEXAGON_DYNAMIC_TAG
445 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
446 #include "llvm/BinaryFormat/DynamicTags.def"
447 #undef MIPS_DYNAMIC_TAG
452 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
453 #include "llvm/BinaryFormat/DynamicTags.def"
454 #undef PPC64_DYNAMIC_TAG
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"
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
478 template <class ELFT>
479 const char *ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
480 return getDynamicTagAsString(getHeader()->e_machine, Type);
483 template <class ELFT>
484 Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
485 ArrayRef<Elf_Dyn> Dyn;
486 size_t DynSecSize = 0;
488 auto ProgramHeadersOrError = program_headers();
489 if (!ProgramHeadersOrError)
490 return ProgramHeadersOrError.takeError();
492 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
493 if (Phdr.p_type == ELF::PT_DYNAMIC) {
495 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
496 Phdr.p_filesz / sizeof(Elf_Dyn));
497 DynSecSize = Phdr.p_filesz;
502 // If we can't find the dynamic section in the program headers, we just fall
503 // back on the sections.
505 auto SectionsOrError = sections();
506 if (!SectionsOrError)
507 return SectionsOrError.takeError();
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);
514 return DynOrError.takeError();
516 DynSecSize = Sec.sh_size;
522 return ArrayRef<Elf_Dyn>();
526 return createError("invalid empty dynamic section");
528 if (DynSecSize % sizeof(Elf_Dyn) != 0)
529 return createError("malformed dynamic section");
531 if (Dyn.back().d_tag != ELF::DT_NULL)
532 return createError("dynamic sections must be DT_NULL terminated");
537 template <class ELFT>
538 Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
539 auto ProgramHeadersOrError = program_headers();
540 if (!ProgramHeadersOrError)
541 return ProgramHeadersOrError.takeError();
543 llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
545 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
546 if (Phdr.p_type == ELF::PT_LOAD)
547 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
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;
555 if (I == LoadSegments.begin())
556 return createError("Virtual address is not in any segment");
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;
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>;