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"
150 uint32_t llvm::object::getELFRelrRelocationType(uint32_t Machine) {
153 return ELF::R_X86_64_RELATIVE;
156 return ELF::R_386_RELATIVE;
159 case ELF::EM_AARCH64:
160 return ELF::R_AARCH64_RELATIVE;
162 return ELF::R_ARM_RELATIVE;
163 case ELF::EM_ARC_COMPACT:
164 case ELF::EM_ARC_COMPACT2:
165 return ELF::R_ARC_RELATIVE;
168 case ELF::EM_HEXAGON:
169 return ELF::R_HEX_RELATIVE;
175 return ELF::R_PPC64_RELATIVE;
177 return ELF::R_RISCV_RELATIVE;
179 return ELF::R_390_RELATIVE;
181 case ELF::EM_SPARC32PLUS:
182 case ELF::EM_SPARCV9:
183 return ELF::R_SPARC_RELATIVE;
194 StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
198 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
199 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
200 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
201 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
202 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
205 case ELF::EM_HEXAGON:
206 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
209 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
212 case ELF::EM_MIPS_RS3_LE:
214 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
215 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
216 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
217 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
225 STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
226 STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
227 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
228 STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
229 STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
230 STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
231 STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
232 STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
233 STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
234 STRINGIFY_ENUM_CASE(ELF, SHT_REL);
235 STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
236 STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
237 STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
238 STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
239 STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
240 STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
241 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
242 STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
243 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
244 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
245 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
246 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
247 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
248 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
249 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
250 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
251 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
252 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
253 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
254 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
260 template <class ELFT>
261 Expected<std::vector<typename ELFT::Rela>>
262 ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
263 // This function decodes the contents of an SHT_RELR packed relocation
266 // Proposal for adding SHT_RELR sections to generic-abi is here:
267 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
269 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
270 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
272 // i.e. start with an address, followed by any number of bitmaps. The address
273 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
274 // relocations each, at subsequent offsets following the last address entry.
276 // The bitmap entries must have 1 in the least significant bit. The assumption
277 // here is that an address cannot have 1 in lsb. Odd addresses are not
280 // Excluding the least significant bit in the bitmap, each non-zero bit in
281 // the bitmap represents a relocation to be applied to a corresponding machine
282 // word that follows the base address word. The second least significant bit
283 // represents the machine word immediately following the initial address, and
284 // each bit that follows represents the next word, in linear order. As such,
285 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
286 // 63 relocations in a 64-bit object.
288 // This encoding has a couple of interesting properties:
289 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
290 // even means address, odd means bitmap.
291 // 2. Just a simple list of addresses is a valid encoding.
296 Rela.setType(getRelrRelocationType(), false);
297 std::vector<Elf_Rela> Relocs;
299 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
300 typedef typename ELFT::uint Word;
302 // Word size in number of bytes.
303 const size_t WordSize = sizeof(Word);
305 // Number of bits used for the relocation offsets bitmap.
306 // These many relative relocations can be encoded in a single entry.
307 const size_t NBits = 8*WordSize - 1;
310 for (const Elf_Relr &R : relrs) {
312 if ((Entry&1) == 0) {
313 // Even entry: encodes the offset for next relocation.
314 Rela.r_offset = Entry;
315 Relocs.push_back(Rela);
316 // Set base offset for subsequent bitmap entries.
317 Base = Entry + WordSize;
321 // Odd entry: encodes bitmap for relocations starting at base.
325 if ((Entry&1) != 0) {
326 Rela.r_offset = Offset;
327 Relocs.push_back(Rela);
332 // Advance base offset by NBits words.
333 Base += NBits * WordSize;
339 template <class ELFT>
340 Expected<std::vector<typename ELFT::Rela>>
341 ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
342 // This function reads relocations in Android's packed relocation format,
343 // which is based on SLEB128 and delta encoding.
344 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
346 return ContentsOrErr.takeError();
347 const uint8_t *Cur = ContentsOrErr->begin();
348 const uint8_t *End = ContentsOrErr->end();
349 if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
350 Cur[2] != 'S' || Cur[3] != '2')
351 return createError("invalid packed relocation header");
354 const char *ErrStr = nullptr;
355 auto ReadSLEB = [&]() -> int64_t {
359 int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
364 uint64_t NumRelocs = ReadSLEB();
365 uint64_t Offset = ReadSLEB();
369 return createError(ErrStr);
371 std::vector<Elf_Rela> Relocs;
372 Relocs.reserve(NumRelocs);
374 uint64_t NumRelocsInGroup = ReadSLEB();
375 if (NumRelocsInGroup > NumRelocs)
376 return createError("relocation group unexpectedly large");
377 NumRelocs -= NumRelocsInGroup;
379 uint64_t GroupFlags = ReadSLEB();
380 bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
381 bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
382 bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
383 bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
385 uint64_t GroupOffsetDelta;
386 if (GroupedByOffsetDelta)
387 GroupOffsetDelta = ReadSLEB();
391 GroupRInfo = ReadSLEB();
393 if (GroupedByAddend && GroupHasAddend)
394 Addend += ReadSLEB();
396 for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
398 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
400 R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
402 if (GroupHasAddend) {
403 if (!GroupedByAddend)
404 Addend += ReadSLEB();
413 return createError(ErrStr);
417 return createError(ErrStr);
423 template <class ELFT>
424 const char *ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
425 uint64_t Type) const {
426 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
430 #define DYNAMIC_TAG(n, v)
432 case ELF::EM_HEXAGON:
434 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
435 #include "llvm/BinaryFormat/DynamicTags.def"
436 #undef HEXAGON_DYNAMIC_TAG
441 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
442 #include "llvm/BinaryFormat/DynamicTags.def"
443 #undef MIPS_DYNAMIC_TAG
448 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
449 #include "llvm/BinaryFormat/DynamicTags.def"
450 #undef PPC64_DYNAMIC_TAG
455 // Now handle all dynamic tags except the architecture specific ones
456 #define MIPS_DYNAMIC_TAG(name, value)
457 #define HEXAGON_DYNAMIC_TAG(name, value)
458 #define PPC64_DYNAMIC_TAG(name, value)
459 // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
460 #define DYNAMIC_TAG_MARKER(name, value)
461 #define DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
462 #include "llvm/BinaryFormat/DynamicTags.def"
464 #undef MIPS_DYNAMIC_TAG
465 #undef HEXAGON_DYNAMIC_TAG
466 #undef PPC64_DYNAMIC_TAG
467 #undef DYNAMIC_TAG_MARKER
468 #undef DYNAMIC_STRINGIFY_ENUM
474 template <class ELFT>
475 const char *ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
476 return getDynamicTagAsString(getHeader()->e_machine, Type);
479 template <class ELFT>
480 Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
481 ArrayRef<Elf_Dyn> Dyn;
482 size_t DynSecSize = 0;
484 auto ProgramHeadersOrError = program_headers();
485 if (!ProgramHeadersOrError)
486 return ProgramHeadersOrError.takeError();
488 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
489 if (Phdr.p_type == ELF::PT_DYNAMIC) {
491 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
492 Phdr.p_filesz / sizeof(Elf_Dyn));
493 DynSecSize = Phdr.p_filesz;
498 // If we can't find the dynamic section in the program headers, we just fall
499 // back on the sections.
501 auto SectionsOrError = sections();
502 if (!SectionsOrError)
503 return SectionsOrError.takeError();
505 for (const Elf_Shdr &Sec : *SectionsOrError) {
506 if (Sec.sh_type == ELF::SHT_DYNAMIC) {
507 Expected<ArrayRef<Elf_Dyn>> DynOrError =
508 getSectionContentsAsArray<Elf_Dyn>(&Sec);
510 return DynOrError.takeError();
512 DynSecSize = Sec.sh_size;
518 return ArrayRef<Elf_Dyn>();
522 return createError("invalid empty dynamic section");
524 if (DynSecSize % sizeof(Elf_Dyn) != 0)
525 return createError("malformed dynamic section");
527 if (Dyn.back().d_tag != ELF::DT_NULL)
528 return createError("dynamic sections must be DT_NULL terminated");
533 template <class ELFT>
534 Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
535 auto ProgramHeadersOrError = program_headers();
536 if (!ProgramHeadersOrError)
537 return ProgramHeadersOrError.takeError();
539 llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
541 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
542 if (Phdr.p_type == ELF::PT_LOAD)
543 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
545 const Elf_Phdr *const *I =
546 std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
547 [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
548 return VAddr < Phdr->p_vaddr;
551 if (I == LoadSegments.begin())
552 return createError("Virtual address is not in any segment");
554 const Elf_Phdr &Phdr = **I;
555 uint64_t Delta = VAddr - Phdr.p_vaddr;
556 if (Delta >= Phdr.p_filesz)
557 return createError("Virtual address is not in any segment");
558 return base() + Phdr.p_offset + Delta;
561 template class llvm::object::ELFFile<ELF32LE>;
562 template class llvm::object::ELFFile<ELF32BE>;
563 template class llvm::object::ELFFile<ELF64LE>;
564 template class llvm::object::ELFFile<ELF64BE>;