1 //===- ELF.cpp - ELF object file implementation ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Object/ELF.h"
10 #include "llvm/BinaryFormat/ELF.h"
11 #include "llvm/Support/LEB128.h"
14 using namespace object
;
16 #define STRINGIFY_ENUM_CASE(ns, name) \
20 #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
22 StringRef
llvm::object::getELFRelocationTypeName(uint32_t Machine
,
27 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
35 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
42 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
49 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
56 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
61 case ELF::EM_ARC_COMPACT
:
62 case ELF::EM_ARC_COMPACT2
:
64 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
71 #include "llvm/BinaryFormat/ELFRelocs/AVR.def"
78 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
85 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
92 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
99 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
106 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
113 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
119 case ELF::EM_SPARC32PLUS
:
120 case ELF::EM_SPARCV9
:
122 #include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
129 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
136 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
143 #include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
156 uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine
) {
159 return ELF::R_X86_64_RELATIVE
;
162 return ELF::R_386_RELATIVE
;
165 case ELF::EM_AARCH64
:
166 return ELF::R_AARCH64_RELATIVE
;
168 return ELF::R_ARM_RELATIVE
;
169 case ELF::EM_ARC_COMPACT
:
170 case ELF::EM_ARC_COMPACT2
:
171 return ELF::R_ARC_RELATIVE
;
174 case ELF::EM_HEXAGON
:
175 return ELF::R_HEX_RELATIVE
;
181 return ELF::R_PPC64_RELATIVE
;
183 return ELF::R_RISCV_RELATIVE
;
185 return ELF::R_390_RELATIVE
;
187 case ELF::EM_SPARC32PLUS
:
188 case ELF::EM_SPARCV9
:
189 return ELF::R_SPARC_RELATIVE
;
200 StringRef
llvm::object::getELFSectionTypeName(uint32_t Machine
, unsigned Type
) {
204 STRINGIFY_ENUM_CASE(ELF
, SHT_ARM_EXIDX
);
205 STRINGIFY_ENUM_CASE(ELF
, SHT_ARM_PREEMPTMAP
);
206 STRINGIFY_ENUM_CASE(ELF
, SHT_ARM_ATTRIBUTES
);
207 STRINGIFY_ENUM_CASE(ELF
, SHT_ARM_DEBUGOVERLAY
);
208 STRINGIFY_ENUM_CASE(ELF
, SHT_ARM_OVERLAYSECTION
);
211 case ELF::EM_HEXAGON
:
212 switch (Type
) { STRINGIFY_ENUM_CASE(ELF
, SHT_HEX_ORDERED
); }
215 switch (Type
) { STRINGIFY_ENUM_CASE(ELF
, SHT_X86_64_UNWIND
); }
218 case ELF::EM_MIPS_RS3_LE
:
220 STRINGIFY_ENUM_CASE(ELF
, SHT_MIPS_REGINFO
);
221 STRINGIFY_ENUM_CASE(ELF
, SHT_MIPS_OPTIONS
);
222 STRINGIFY_ENUM_CASE(ELF
, SHT_MIPS_DWARF
);
223 STRINGIFY_ENUM_CASE(ELF
, SHT_MIPS_ABIFLAGS
);
231 STRINGIFY_ENUM_CASE(ELF
, SHT_NULL
);
232 STRINGIFY_ENUM_CASE(ELF
, SHT_PROGBITS
);
233 STRINGIFY_ENUM_CASE(ELF
, SHT_SYMTAB
);
234 STRINGIFY_ENUM_CASE(ELF
, SHT_STRTAB
);
235 STRINGIFY_ENUM_CASE(ELF
, SHT_RELA
);
236 STRINGIFY_ENUM_CASE(ELF
, SHT_HASH
);
237 STRINGIFY_ENUM_CASE(ELF
, SHT_DYNAMIC
);
238 STRINGIFY_ENUM_CASE(ELF
, SHT_NOTE
);
239 STRINGIFY_ENUM_CASE(ELF
, SHT_NOBITS
);
240 STRINGIFY_ENUM_CASE(ELF
, SHT_REL
);
241 STRINGIFY_ENUM_CASE(ELF
, SHT_SHLIB
);
242 STRINGIFY_ENUM_CASE(ELF
, SHT_DYNSYM
);
243 STRINGIFY_ENUM_CASE(ELF
, SHT_INIT_ARRAY
);
244 STRINGIFY_ENUM_CASE(ELF
, SHT_FINI_ARRAY
);
245 STRINGIFY_ENUM_CASE(ELF
, SHT_PREINIT_ARRAY
);
246 STRINGIFY_ENUM_CASE(ELF
, SHT_GROUP
);
247 STRINGIFY_ENUM_CASE(ELF
, SHT_SYMTAB_SHNDX
);
248 STRINGIFY_ENUM_CASE(ELF
, SHT_RELR
);
249 STRINGIFY_ENUM_CASE(ELF
, SHT_ANDROID_REL
);
250 STRINGIFY_ENUM_CASE(ELF
, SHT_ANDROID_RELA
);
251 STRINGIFY_ENUM_CASE(ELF
, SHT_ANDROID_RELR
);
252 STRINGIFY_ENUM_CASE(ELF
, SHT_LLVM_ODRTAB
);
253 STRINGIFY_ENUM_CASE(ELF
, SHT_LLVM_LINKER_OPTIONS
);
254 STRINGIFY_ENUM_CASE(ELF
, SHT_LLVM_CALL_GRAPH_PROFILE
);
255 STRINGIFY_ENUM_CASE(ELF
, SHT_LLVM_ADDRSIG
);
256 STRINGIFY_ENUM_CASE(ELF
, SHT_LLVM_DEPENDENT_LIBRARIES
);
257 STRINGIFY_ENUM_CASE(ELF
, SHT_LLVM_SYMPART
);
258 STRINGIFY_ENUM_CASE(ELF
, SHT_GNU_ATTRIBUTES
);
259 STRINGIFY_ENUM_CASE(ELF
, SHT_GNU_HASH
);
260 STRINGIFY_ENUM_CASE(ELF
, SHT_GNU_verdef
);
261 STRINGIFY_ENUM_CASE(ELF
, SHT_GNU_verneed
);
262 STRINGIFY_ENUM_CASE(ELF
, SHT_GNU_versym
);
268 template <class ELFT
>
269 Expected
<std::vector
<typename
ELFT::Rela
>>
270 ELFFile
<ELFT
>::decode_relrs(Elf_Relr_Range relrs
) const {
271 // This function decodes the contents of an SHT_RELR packed relocation
274 // Proposal for adding SHT_RELR sections to generic-abi is here:
275 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
277 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
278 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
280 // i.e. start with an address, followed by any number of bitmaps. The address
281 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
282 // relocations each, at subsequent offsets following the last address entry.
284 // The bitmap entries must have 1 in the least significant bit. The assumption
285 // here is that an address cannot have 1 in lsb. Odd addresses are not
288 // Excluding the least significant bit in the bitmap, each non-zero bit in
289 // the bitmap represents a relocation to be applied to a corresponding machine
290 // word that follows the base address word. The second least significant bit
291 // represents the machine word immediately following the initial address, and
292 // each bit that follows represents the next word, in linear order. As such,
293 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
294 // 63 relocations in a 64-bit object.
296 // This encoding has a couple of interesting properties:
297 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
298 // even means address, odd means bitmap.
299 // 2. Just a simple list of addresses is a valid encoding.
304 Rela
.setType(getRelativeRelocationType(), false);
305 std::vector
<Elf_Rela
> Relocs
;
307 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
308 typedef typename
ELFT::uint Word
;
310 // Word size in number of bytes.
311 const size_t WordSize
= sizeof(Word
);
313 // Number of bits used for the relocation offsets bitmap.
314 // These many relative relocations can be encoded in a single entry.
315 const size_t NBits
= 8*WordSize
- 1;
318 for (const Elf_Relr
&R
: relrs
) {
320 if ((Entry
&1) == 0) {
321 // Even entry: encodes the offset for next relocation.
322 Rela
.r_offset
= Entry
;
323 Relocs
.push_back(Rela
);
324 // Set base offset for subsequent bitmap entries.
325 Base
= Entry
+ WordSize
;
329 // Odd entry: encodes bitmap for relocations starting at base.
333 if ((Entry
&1) != 0) {
334 Rela
.r_offset
= Offset
;
335 Relocs
.push_back(Rela
);
340 // Advance base offset by NBits words.
341 Base
+= NBits
* WordSize
;
347 template <class ELFT
>
348 Expected
<std::vector
<typename
ELFT::Rela
>>
349 ELFFile
<ELFT
>::android_relas(const Elf_Shdr
*Sec
) const {
350 // This function reads relocations in Android's packed relocation format,
351 // which is based on SLEB128 and delta encoding.
352 Expected
<ArrayRef
<uint8_t>> ContentsOrErr
= getSectionContents(Sec
);
354 return ContentsOrErr
.takeError();
355 const uint8_t *Cur
= ContentsOrErr
->begin();
356 const uint8_t *End
= ContentsOrErr
->end();
357 if (ContentsOrErr
->size() < 4 || Cur
[0] != 'A' || Cur
[1] != 'P' ||
358 Cur
[2] != 'S' || Cur
[3] != '2')
359 return createError("invalid packed relocation header");
362 const char *ErrStr
= nullptr;
363 auto ReadSLEB
= [&]() -> int64_t {
367 int64_t Result
= decodeSLEB128(Cur
, &Len
, End
, &ErrStr
);
372 uint64_t NumRelocs
= ReadSLEB();
373 uint64_t Offset
= ReadSLEB();
377 return createError(ErrStr
);
379 std::vector
<Elf_Rela
> Relocs
;
380 Relocs
.reserve(NumRelocs
);
382 uint64_t NumRelocsInGroup
= ReadSLEB();
383 if (NumRelocsInGroup
> NumRelocs
)
384 return createError("relocation group unexpectedly large");
385 NumRelocs
-= NumRelocsInGroup
;
387 uint64_t GroupFlags
= ReadSLEB();
388 bool GroupedByInfo
= GroupFlags
& ELF::RELOCATION_GROUPED_BY_INFO_FLAG
;
389 bool GroupedByOffsetDelta
= GroupFlags
& ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG
;
390 bool GroupedByAddend
= GroupFlags
& ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG
;
391 bool GroupHasAddend
= GroupFlags
& ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG
;
393 uint64_t GroupOffsetDelta
;
394 if (GroupedByOffsetDelta
)
395 GroupOffsetDelta
= ReadSLEB();
399 GroupRInfo
= ReadSLEB();
401 if (GroupedByAddend
&& GroupHasAddend
)
402 Addend
+= ReadSLEB();
407 for (uint64_t I
= 0; I
!= NumRelocsInGroup
; ++I
) {
409 Offset
+= GroupedByOffsetDelta
? GroupOffsetDelta
: ReadSLEB();
411 R
.r_info
= GroupedByInfo
? GroupRInfo
: ReadSLEB();
412 if (GroupHasAddend
&& !GroupedByAddend
)
413 Addend
+= ReadSLEB();
418 return createError(ErrStr
);
422 return createError(ErrStr
);
428 template <class ELFT
>
429 std::string ELFFile
<ELFT
>::getDynamicTagAsString(unsigned Arch
,
430 uint64_t Type
) const {
431 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
435 #define DYNAMIC_TAG(n, v)
437 case ELF::EM_AARCH64
:
439 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
440 #include "llvm/BinaryFormat/DynamicTags.def"
441 #undef AARCH64_DYNAMIC_TAG
445 case ELF::EM_HEXAGON
:
447 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
448 #include "llvm/BinaryFormat/DynamicTags.def"
449 #undef HEXAGON_DYNAMIC_TAG
455 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
456 #include "llvm/BinaryFormat/DynamicTags.def"
457 #undef MIPS_DYNAMIC_TAG
463 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
464 #include "llvm/BinaryFormat/DynamicTags.def"
465 #undef PPC64_DYNAMIC_TAG
471 // Now handle all dynamic tags except the architecture specific ones
472 #define AARCH64_DYNAMIC_TAG(name, value)
473 #define MIPS_DYNAMIC_TAG(name, value)
474 #define HEXAGON_DYNAMIC_TAG(name, value)
475 #define PPC64_DYNAMIC_TAG(name, value)
476 // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
477 #define DYNAMIC_TAG_MARKER(name, value)
478 #define DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
479 #include "llvm/BinaryFormat/DynamicTags.def"
481 #undef AARCH64_DYNAMIC_TAG
482 #undef MIPS_DYNAMIC_TAG
483 #undef HEXAGON_DYNAMIC_TAG
484 #undef PPC64_DYNAMIC_TAG
485 #undef DYNAMIC_TAG_MARKER
486 #undef DYNAMIC_STRINGIFY_ENUM
488 return "<unknown:>0x" + utohexstr(Type
, true);
492 template <class ELFT
>
493 std::string ELFFile
<ELFT
>::getDynamicTagAsString(uint64_t Type
) const {
494 return getDynamicTagAsString(getHeader()->e_machine
, Type
);
497 template <class ELFT
>
498 Expected
<typename
ELFT::DynRange
> ELFFile
<ELFT
>::dynamicEntries() const {
499 ArrayRef
<Elf_Dyn
> Dyn
;
500 size_t DynSecSize
= 0;
502 auto ProgramHeadersOrError
= program_headers();
503 if (!ProgramHeadersOrError
)
504 return ProgramHeadersOrError
.takeError();
506 for (const Elf_Phdr
&Phdr
: *ProgramHeadersOrError
) {
507 if (Phdr
.p_type
== ELF::PT_DYNAMIC
) {
509 reinterpret_cast<const Elf_Dyn
*>(base() + Phdr
.p_offset
),
510 Phdr
.p_filesz
/ sizeof(Elf_Dyn
));
511 DynSecSize
= Phdr
.p_filesz
;
516 // If we can't find the dynamic section in the program headers, we just fall
517 // back on the sections.
519 auto SectionsOrError
= sections();
520 if (!SectionsOrError
)
521 return SectionsOrError
.takeError();
523 for (const Elf_Shdr
&Sec
: *SectionsOrError
) {
524 if (Sec
.sh_type
== ELF::SHT_DYNAMIC
) {
525 Expected
<ArrayRef
<Elf_Dyn
>> DynOrError
=
526 getSectionContentsAsArray
<Elf_Dyn
>(&Sec
);
528 return DynOrError
.takeError();
530 DynSecSize
= Sec
.sh_size
;
536 return ArrayRef
<Elf_Dyn
>();
540 // TODO: this error is untested.
541 return createError("invalid empty dynamic section");
543 if (DynSecSize
% sizeof(Elf_Dyn
) != 0)
544 // TODO: this error is untested.
545 return createError("malformed dynamic section");
547 if (Dyn
.back().d_tag
!= ELF::DT_NULL
)
548 // TODO: this error is untested.
549 return createError("dynamic sections must be DT_NULL terminated");
554 template <class ELFT
>
555 Expected
<const uint8_t *> ELFFile
<ELFT
>::toMappedAddr(uint64_t VAddr
) const {
556 auto ProgramHeadersOrError
= program_headers();
557 if (!ProgramHeadersOrError
)
558 return ProgramHeadersOrError
.takeError();
560 llvm::SmallVector
<Elf_Phdr
*, 4> LoadSegments
;
562 for (const Elf_Phdr
&Phdr
: *ProgramHeadersOrError
)
563 if (Phdr
.p_type
== ELF::PT_LOAD
)
564 LoadSegments
.push_back(const_cast<Elf_Phdr
*>(&Phdr
));
566 const Elf_Phdr
*const *I
=
567 std::upper_bound(LoadSegments
.begin(), LoadSegments
.end(), VAddr
,
568 [](uint64_t VAddr
, const Elf_Phdr_Impl
<ELFT
> *Phdr
) {
569 return VAddr
< Phdr
->p_vaddr
;
572 if (I
== LoadSegments
.begin())
573 return createError("virtual address is not in any segment: 0x" +
574 Twine::utohexstr(VAddr
));
576 const Elf_Phdr
&Phdr
= **I
;
577 uint64_t Delta
= VAddr
- Phdr
.p_vaddr
;
578 if (Delta
>= Phdr
.p_filesz
)
579 return createError("virtual address is not in any segment: 0x" +
580 Twine::utohexstr(VAddr
));
581 return base() + Phdr
.p_offset
+ Delta
;
584 template class llvm::object::ELFFile
<ELF32LE
>;
585 template class llvm::object::ELFFile
<ELF32BE
>;
586 template class llvm::object::ELFFile
<ELF64LE
>;
587 template class llvm::object::ELFFile
<ELF64BE
>;