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();
399 for (uint64_t I
= 0; I
!= NumRelocsInGroup
; ++I
) {
401 Offset
+= GroupedByOffsetDelta
? GroupOffsetDelta
: ReadSLEB();
403 R
.r_info
= GroupedByInfo
? GroupRInfo
: ReadSLEB();
404 if (GroupHasAddend
&& !GroupedByAddend
)
405 Addend
+= ReadSLEB();
410 return createError(ErrStr
);
414 return createError(ErrStr
);
420 template <class ELFT
>
421 const char *ELFFile
<ELFT
>::getDynamicTagAsString(unsigned Arch
,
422 uint64_t Type
) const {
423 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
427 #define DYNAMIC_TAG(n, v)
429 case ELF::EM_HEXAGON
:
431 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
432 #include "llvm/BinaryFormat/DynamicTags.def"
433 #undef HEXAGON_DYNAMIC_TAG
438 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
439 #include "llvm/BinaryFormat/DynamicTags.def"
440 #undef MIPS_DYNAMIC_TAG
445 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
446 #include "llvm/BinaryFormat/DynamicTags.def"
447 #undef PPC64_DYNAMIC_TAG
452 // Now handle all dynamic tags except the architecture specific ones
453 #define MIPS_DYNAMIC_TAG(name, value)
454 #define HEXAGON_DYNAMIC_TAG(name, value)
455 #define PPC64_DYNAMIC_TAG(name, value)
456 // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
457 #define DYNAMIC_TAG_MARKER(name, value)
458 #define DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
459 #include "llvm/BinaryFormat/DynamicTags.def"
461 #undef MIPS_DYNAMIC_TAG
462 #undef HEXAGON_DYNAMIC_TAG
463 #undef PPC64_DYNAMIC_TAG
464 #undef DYNAMIC_TAG_MARKER
465 #undef DYNAMIC_STRINGIFY_ENUM
471 template <class ELFT
>
472 const char *ELFFile
<ELFT
>::getDynamicTagAsString(uint64_t Type
) const {
473 return getDynamicTagAsString(getHeader()->e_machine
, Type
);
476 template <class ELFT
>
477 Expected
<typename
ELFT::DynRange
> ELFFile
<ELFT
>::dynamicEntries() const {
478 ArrayRef
<Elf_Dyn
> Dyn
;
479 size_t DynSecSize
= 0;
481 auto ProgramHeadersOrError
= program_headers();
482 if (!ProgramHeadersOrError
)
483 return ProgramHeadersOrError
.takeError();
485 for (const Elf_Phdr
&Phdr
: *ProgramHeadersOrError
) {
486 if (Phdr
.p_type
== ELF::PT_DYNAMIC
) {
488 reinterpret_cast<const Elf_Dyn
*>(base() + Phdr
.p_offset
),
489 Phdr
.p_filesz
/ sizeof(Elf_Dyn
));
490 DynSecSize
= Phdr
.p_filesz
;
495 // If we can't find the dynamic section in the program headers, we just fall
496 // back on the sections.
498 auto SectionsOrError
= sections();
499 if (!SectionsOrError
)
500 return SectionsOrError
.takeError();
502 for (const Elf_Shdr
&Sec
: *SectionsOrError
) {
503 if (Sec
.sh_type
== ELF::SHT_DYNAMIC
) {
504 Expected
<ArrayRef
<Elf_Dyn
>> DynOrError
=
505 getSectionContentsAsArray
<Elf_Dyn
>(&Sec
);
507 return DynOrError
.takeError();
509 DynSecSize
= Sec
.sh_size
;
515 return ArrayRef
<Elf_Dyn
>();
519 return createError("invalid empty dynamic section");
521 if (DynSecSize
% sizeof(Elf_Dyn
) != 0)
522 return createError("malformed dynamic section");
524 if (Dyn
.back().d_tag
!= ELF::DT_NULL
)
525 return createError("dynamic sections must be DT_NULL terminated");
530 template <class ELFT
>
531 Expected
<const uint8_t *> ELFFile
<ELFT
>::toMappedAddr(uint64_t VAddr
) const {
532 auto ProgramHeadersOrError
= program_headers();
533 if (!ProgramHeadersOrError
)
534 return ProgramHeadersOrError
.takeError();
536 llvm::SmallVector
<Elf_Phdr
*, 4> LoadSegments
;
538 for (const Elf_Phdr
&Phdr
: *ProgramHeadersOrError
)
539 if (Phdr
.p_type
== ELF::PT_LOAD
)
540 LoadSegments
.push_back(const_cast<Elf_Phdr
*>(&Phdr
));
542 const Elf_Phdr
*const *I
=
543 std::upper_bound(LoadSegments
.begin(), LoadSegments
.end(), VAddr
,
544 [](uint64_t VAddr
, const Elf_Phdr_Impl
<ELFT
> *Phdr
) {
545 return VAddr
< Phdr
->p_vaddr
;
548 if (I
== LoadSegments
.begin())
549 return createError("Virtual address is not in any segment");
551 const Elf_Phdr
&Phdr
= **I
;
552 uint64_t Delta
= VAddr
- Phdr
.p_vaddr
;
553 if (Delta
>= Phdr
.p_filesz
)
554 return createError("Virtual address is not in any segment");
555 return base() + Phdr
.p_offset
+ Delta
;
558 template class llvm::object::ELFFile
<ELF32LE
>;
559 template class llvm::object::ELFFile
<ELF32BE
>;
560 template class llvm::object::ELFFile
<ELF64LE
>;
561 template class llvm::object::ELFFile
<ELF64BE
>;