LLVM 19.0.0git
ELF.cpp
Go to the documentation of this file.
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"
13
14using namespace llvm;
15using namespace object;
16
17#define STRINGIFY_ENUM_CASE(ns, name) \
18 case ns::name: \
19 return #name;
20
21#define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
22
24 uint32_t Type) {
25 switch (Machine) {
26 case ELF::EM_68K:
27 switch (Type) {
28#include "llvm/BinaryFormat/ELFRelocs/M68k.def"
29 default:
30 break;
31 }
32 break;
33 case ELF::EM_X86_64:
34 switch (Type) {
35#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
36 default:
37 break;
38 }
39 break;
40 case ELF::EM_386:
41 case ELF::EM_IAMCU:
42 switch (Type) {
43#include "llvm/BinaryFormat/ELFRelocs/i386.def"
44 default:
45 break;
46 }
47 break;
48 case ELF::EM_MIPS:
49 switch (Type) {
50#include "llvm/BinaryFormat/ELFRelocs/Mips.def"
51 default:
52 break;
53 }
54 break;
55 case ELF::EM_AARCH64:
56 switch (Type) {
57#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
58 default:
59 break;
60 }
61 break;
62 case ELF::EM_ARM:
63 switch (Type) {
64#include "llvm/BinaryFormat/ELFRelocs/ARM.def"
65 default:
66 break;
67 }
68 break;
71 switch (Type) {
72#include "llvm/BinaryFormat/ELFRelocs/ARC.def"
73 default:
74 break;
75 }
76 break;
77 case ELF::EM_AVR:
78 switch (Type) {
79#include "llvm/BinaryFormat/ELFRelocs/AVR.def"
80 default:
81 break;
82 }
83 break;
84 case ELF::EM_HEXAGON:
85 switch (Type) {
86#include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
87 default:
88 break;
89 }
90 break;
91 case ELF::EM_LANAI:
92 switch (Type) {
93#include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
94 default:
95 break;
96 }
97 break;
98 case ELF::EM_PPC:
99 switch (Type) {
100#include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
101 default:
102 break;
103 }
104 break;
105 case ELF::EM_PPC64:
106 switch (Type) {
107#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
108 default:
109 break;
110 }
111 break;
112 case ELF::EM_RISCV:
113 switch (Type) {
114#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
115 default:
116 break;
117 }
118 break;
119 case ELF::EM_S390:
120 switch (Type) {
121#include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
122 default:
123 break;
124 }
125 break;
126 case ELF::EM_SPARC:
128 case ELF::EM_SPARCV9:
129 switch (Type) {
130#include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
131 default:
132 break;
133 }
134 break;
135 case ELF::EM_AMDGPU:
136 switch (Type) {
137#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
138 default:
139 break;
140 }
141 break;
142 case ELF::EM_BPF:
143 switch (Type) {
144#include "llvm/BinaryFormat/ELFRelocs/BPF.def"
145 default:
146 break;
147 }
148 break;
149 case ELF::EM_MSP430:
150 switch (Type) {
151#include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
152 default:
153 break;
154 }
155 break;
156 case ELF::EM_VE:
157 switch (Type) {
158#include "llvm/BinaryFormat/ELFRelocs/VE.def"
159 default:
160 break;
161 }
162 break;
163 case ELF::EM_CSKY:
164 switch (Type) {
165#include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
166 default:
167 break;
168 }
169 break;
171 switch (Type) {
172#include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
173 default:
174 break;
175 }
176 break;
177 case ELF::EM_XTENSA:
178 switch (Type) {
179#include "llvm/BinaryFormat/ELFRelocs/Xtensa.def"
180 default:
181 break;
182 }
183 break;
184 default:
185 break;
186 }
187 return "Unknown";
188}
189
190#undef ELF_RELOC
191
193 switch (Machine) {
194 case ELF::EM_X86_64:
195 return ELF::R_X86_64_RELATIVE;
196 case ELF::EM_386:
197 case ELF::EM_IAMCU:
198 return ELF::R_386_RELATIVE;
199 case ELF::EM_MIPS:
200 break;
201 case ELF::EM_AARCH64:
202 return ELF::R_AARCH64_RELATIVE;
203 case ELF::EM_ARM:
204 return ELF::R_ARM_RELATIVE;
207 return ELF::R_ARC_RELATIVE;
208 case ELF::EM_AVR:
209 break;
210 case ELF::EM_HEXAGON:
211 return ELF::R_HEX_RELATIVE;
212 case ELF::EM_LANAI:
213 break;
214 case ELF::EM_PPC:
215 break;
216 case ELF::EM_PPC64:
217 return ELF::R_PPC64_RELATIVE;
218 case ELF::EM_RISCV:
219 return ELF::R_RISCV_RELATIVE;
220 case ELF::EM_S390:
221 return ELF::R_390_RELATIVE;
222 case ELF::EM_SPARC:
224 case ELF::EM_SPARCV9:
225 return ELF::R_SPARC_RELATIVE;
226 case ELF::EM_CSKY:
227 return ELF::R_CKCORE_RELATIVE;
228 case ELF::EM_VE:
229 return ELF::R_VE_RELATIVE;
230 case ELF::EM_AMDGPU:
231 break;
232 case ELF::EM_BPF:
233 break;
235 return ELF::R_LARCH_RELATIVE;
236 default:
237 break;
238 }
239 return 0;
240}
241
243 switch (Machine) {
244 case ELF::EM_ARM:
245 switch (Type) {
246 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
247 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
248 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
249 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
250 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
251 }
252 break;
253 case ELF::EM_HEXAGON:
254 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
255 break;
256 case ELF::EM_X86_64:
257 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
258 break;
259 case ELF::EM_MIPS:
261 switch (Type) {
262 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
263 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
264 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
265 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
266 }
267 break;
268 case ELF::EM_MSP430:
269 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_MSP430_ATTRIBUTES); }
270 break;
271 case ELF::EM_RISCV:
272 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
273 break;
274 case ELF::EM_AARCH64:
275 switch (Type) {
276 STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_AUTH_RELR);
277 STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC);
278 STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_STATIC);
279 }
280 default:
281 break;
282 }
283
284 switch (Type) {
285 STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
286 STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
287 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
288 STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
289 STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
290 STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
291 STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
292 STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
293 STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
294 STRINGIFY_ENUM_CASE(ELF, SHT_REL);
295 STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
296 STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
297 STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
298 STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
299 STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
300 STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
301 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
302 STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
303 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
304 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
305 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
306 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
307 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
308 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
309 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
310 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
311 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
312 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
313 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
314 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP_V0);
315 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP);
316 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_OFFLOADING);
317 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LTO);
318 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
319 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
320 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
321 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
322 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
323 default:
324 return "Unknown";
325 }
326}
327
328template <class ELFT>
329std::vector<typename ELFT::Rel>
330ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
331 // This function decodes the contents of an SHT_RELR packed relocation
332 // section.
333 //
334 // Proposal for adding SHT_RELR sections to generic-abi is here:
335 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
336 //
337 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
338 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
339 //
340 // i.e. start with an address, followed by any number of bitmaps. The address
341 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
342 // relocations each, at subsequent offsets following the last address entry.
343 //
344 // The bitmap entries must have 1 in the least significant bit. The assumption
345 // here is that an address cannot have 1 in lsb. Odd addresses are not
346 // supported.
347 //
348 // Excluding the least significant bit in the bitmap, each non-zero bit in
349 // the bitmap represents a relocation to be applied to a corresponding machine
350 // word that follows the base address word. The second least significant bit
351 // represents the machine word immediately following the initial address, and
352 // each bit that follows represents the next word, in linear order. As such,
353 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
354 // 63 relocations in a 64-bit object.
355 //
356 // This encoding has a couple of interesting properties:
357 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
358 // even means address, odd means bitmap.
359 // 2. Just a simple list of addresses is a valid encoding.
360
361 Elf_Rel Rel;
362 Rel.r_info = 0;
363 Rel.setType(getRelativeRelocationType(), false);
364 std::vector<Elf_Rel> Relocs;
365
366 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
367 using Addr = typename ELFT::uint;
368
369 Addr Base = 0;
370 for (Elf_Relr R : relrs) {
371 typename ELFT::uint Entry = R;
372 if ((Entry & 1) == 0) {
373 // Even entry: encodes the offset for next relocation.
374 Rel.r_offset = Entry;
375 Relocs.push_back(Rel);
376 // Set base offset for subsequent bitmap entries.
377 Base = Entry + sizeof(Addr);
378 } else {
379 // Odd entry: encodes bitmap for relocations starting at base.
380 for (Addr Offset = Base; (Entry >>= 1) != 0; Offset += sizeof(Addr))
381 if ((Entry & 1) != 0) {
382 Rel.r_offset = Offset;
383 Relocs.push_back(Rel);
384 }
385 Base += (CHAR_BIT * sizeof(Entry) - 1) * sizeof(Addr);
386 }
387 }
388
389 return Relocs;
390}
391
392template <class ELFT>
394ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
395 // This function reads relocations in Android's packed relocation format,
396 // which is based on SLEB128 and delta encoding.
397 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
398 if (!ContentsOrErr)
399 return ContentsOrErr.takeError();
400 ArrayRef<uint8_t> Content = *ContentsOrErr;
401 if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' ||
402 Content[2] != 'S' || Content[3] != '2')
403 return createError("invalid packed relocation header");
404 DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
405 DataExtractor::Cursor Cur(/*Offset=*/4);
406
407 uint64_t NumRelocs = Data.getSLEB128(Cur);
408 uint64_t Offset = Data.getSLEB128(Cur);
409 uint64_t Addend = 0;
410
411 if (!Cur)
412 return std::move(Cur.takeError());
413
414 std::vector<Elf_Rela> Relocs;
415 Relocs.reserve(NumRelocs);
416 while (NumRelocs) {
417 uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
418 if (!Cur)
419 return std::move(Cur.takeError());
420 if (NumRelocsInGroup > NumRelocs)
421 return createError("relocation group unexpectedly large");
422 NumRelocs -= NumRelocsInGroup;
423
424 uint64_t GroupFlags = Data.getSLEB128(Cur);
425 bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
426 bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
427 bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
428 bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
429
430 uint64_t GroupOffsetDelta;
431 if (GroupedByOffsetDelta)
432 GroupOffsetDelta = Data.getSLEB128(Cur);
433
434 uint64_t GroupRInfo;
435 if (GroupedByInfo)
436 GroupRInfo = Data.getSLEB128(Cur);
437
438 if (GroupedByAddend && GroupHasAddend)
439 Addend += Data.getSLEB128(Cur);
440
441 if (!GroupHasAddend)
442 Addend = 0;
443
444 for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) {
445 Elf_Rela R;
446 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
447 R.r_offset = Offset;
448 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
449 if (GroupHasAddend && !GroupedByAddend)
450 Addend += Data.getSLEB128(Cur);
451 R.r_addend = Addend;
452 Relocs.push_back(R);
453 }
454 if (!Cur)
455 return std::move(Cur.takeError());
456 }
457
458 return Relocs;
459}
460
461template <class ELFT>
462std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
463 uint64_t Type) const {
464#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
465 case value: \
466 return #tag;
467
468#define DYNAMIC_TAG(n, v)
469 switch (Arch) {
470 case ELF::EM_AARCH64:
471 switch (Type) {
472#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
473#include "llvm/BinaryFormat/DynamicTags.def"
474#undef AARCH64_DYNAMIC_TAG
475 }
476 break;
477
478 case ELF::EM_HEXAGON:
479 switch (Type) {
480#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
481#include "llvm/BinaryFormat/DynamicTags.def"
482#undef HEXAGON_DYNAMIC_TAG
483 }
484 break;
485
486 case ELF::EM_MIPS:
487 switch (Type) {
488#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
489#include "llvm/BinaryFormat/DynamicTags.def"
490#undef MIPS_DYNAMIC_TAG
491 }
492 break;
493
494 case ELF::EM_PPC:
495 switch (Type) {
496#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
497#include "llvm/BinaryFormat/DynamicTags.def"
498#undef PPC_DYNAMIC_TAG
499 }
500 break;
501
502 case ELF::EM_PPC64:
503 switch (Type) {
504#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
505#include "llvm/BinaryFormat/DynamicTags.def"
506#undef PPC64_DYNAMIC_TAG
507 }
508 break;
509
510 case ELF::EM_RISCV:
511 switch (Type) {
512#define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
513#include "llvm/BinaryFormat/DynamicTags.def"
514#undef RISCV_DYNAMIC_TAG
515 }
516 break;
517 }
518#undef DYNAMIC_TAG
519 switch (Type) {
520// Now handle all dynamic tags except the architecture specific ones
521#define AARCH64_DYNAMIC_TAG(name, value)
522#define MIPS_DYNAMIC_TAG(name, value)
523#define HEXAGON_DYNAMIC_TAG(name, value)
524#define PPC_DYNAMIC_TAG(name, value)
525#define PPC64_DYNAMIC_TAG(name, value)
526#define RISCV_DYNAMIC_TAG(name, value)
527// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
528#define DYNAMIC_TAG_MARKER(name, value)
529#define DYNAMIC_TAG(name, value) case value: return #name;
530#include "llvm/BinaryFormat/DynamicTags.def"
531#undef DYNAMIC_TAG
532#undef AARCH64_DYNAMIC_TAG
533#undef MIPS_DYNAMIC_TAG
534#undef HEXAGON_DYNAMIC_TAG
535#undef PPC_DYNAMIC_TAG
536#undef PPC64_DYNAMIC_TAG
537#undef RISCV_DYNAMIC_TAG
538#undef DYNAMIC_TAG_MARKER
539#undef DYNAMIC_STRINGIFY_ENUM
540 default:
541 return "<unknown:>0x" + utohexstr(Type, true);
542 }
543}
544
545template <class ELFT>
547 return getDynamicTagAsString(getHeader().e_machine, Type);
548}
549
550template <class ELFT>
553
554 auto ProgramHeadersOrError = program_headers();
555 if (!ProgramHeadersOrError)
556 return ProgramHeadersOrError.takeError();
557
558 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
559 if (Phdr.p_type == ELF::PT_DYNAMIC) {
560 Dyn = ArrayRef(reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
561 Phdr.p_filesz / sizeof(Elf_Dyn));
562 break;
563 }
564 }
565
566 // If we can't find the dynamic section in the program headers, we just fall
567 // back on the sections.
568 if (Dyn.empty()) {
569 auto SectionsOrError = sections();
570 if (!SectionsOrError)
571 return SectionsOrError.takeError();
572
573 for (const Elf_Shdr &Sec : *SectionsOrError) {
574 if (Sec.sh_type == ELF::SHT_DYNAMIC) {
575 Expected<ArrayRef<Elf_Dyn>> DynOrError =
576 getSectionContentsAsArray<Elf_Dyn>(Sec);
577 if (!DynOrError)
578 return DynOrError.takeError();
579 Dyn = *DynOrError;
580 break;
581 }
582 }
583
584 if (!Dyn.data())
585 return ArrayRef<Elf_Dyn>();
586 }
587
588 if (Dyn.empty())
589 return createError("invalid empty dynamic section");
590
591 if (Dyn.back().d_tag != ELF::DT_NULL)
592 return createError("dynamic sections must be DT_NULL terminated");
593
594 return Dyn;
595}
596
597template <class ELFT>
600 auto ProgramHeadersOrError = program_headers();
601 if (!ProgramHeadersOrError)
602 return ProgramHeadersOrError.takeError();
603
605
606 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
607 if (Phdr.p_type == ELF::PT_LOAD)
608 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
609
610 auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A,
611 const Elf_Phdr_Impl<ELFT> *B) {
612 return A->p_vaddr < B->p_vaddr;
613 };
614 if (!llvm::is_sorted(LoadSegments, SortPred)) {
615 if (Error E =
616 WarnHandler("loadable segments are unsorted by virtual address"))
617 return std::move(E);
618 llvm::stable_sort(LoadSegments, SortPred);
619 }
620
621 const Elf_Phdr *const *I = llvm::upper_bound(
622 LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
623 return VAddr < Phdr->p_vaddr;
624 });
625
626 if (I == LoadSegments.begin())
627 return createError("virtual address is not in any segment: 0x" +
628 Twine::utohexstr(VAddr));
629 --I;
630 const Elf_Phdr &Phdr = **I;
631 uint64_t Delta = VAddr - Phdr.p_vaddr;
632 if (Delta >= Phdr.p_filesz)
633 return createError("virtual address is not in any segment: 0x" +
634 Twine::utohexstr(VAddr));
635
636 uint64_t Offset = Phdr.p_offset + Delta;
637 if (Offset >= getBufSize())
638 return createError("can't map virtual address 0x" +
639 Twine::utohexstr(VAddr) + " to the segment with index " +
640 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
641 ": the segment ends at 0x" +
642 Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
643 ", which is greater than the file size (0x" +
644 Twine::utohexstr(getBufSize()) + ")");
645
646 return base() + Offset;
647}
648
649// Helper to extract and decode the next ULEB128 value as unsigned int.
650// Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the unsigned
651// int limit.
652// Also returns zero if ULEBSizeErr is already in an error state.
653// ULEBSizeErr is an out variable if an error occurs.
654template <typename IntTy, std::enable_if_t<std::is_unsigned_v<IntTy>, int> = 0>
656 Error &ULEBSizeErr) {
657 // Bail out and do not extract data if ULEBSizeErr is already set.
658 if (ULEBSizeErr)
659 return 0;
660 uint64_t Offset = Cur.tell();
661 uint64_t Value = Data.getULEB128(Cur);
662 if (Value > std::numeric_limits<IntTy>::max()) {
663 ULEBSizeErr = createError("ULEB128 value at offset 0x" +
664 Twine::utohexstr(Offset) + " exceeds UINT" +
665 Twine(std::numeric_limits<IntTy>::digits) +
666 "_MAX (0x" + Twine::utohexstr(Value) + ")");
667 return 0;
668 }
669 return static_cast<IntTy>(Value);
670}
671
672template <typename ELFT>
675 const typename ELFFile<ELFT>::Elf_Shdr &Sec,
676 const typename ELFFile<ELFT>::Elf_Shdr *RelaSec,
677 std::vector<PGOAnalysisMap> *PGOAnalyses) {
678 bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL;
679
680 // This DenseMap maps the offset of each function (the location of the
681 // reference to the function in the SHT_LLVM_BB_ADDR_MAP section) to the
682 // addend (the location of the function in the text section).
683 llvm::DenseMap<uint64_t, uint64_t> FunctionOffsetTranslations;
684 if (IsRelocatable && RelaSec) {
685 assert(RelaSec &&
686 "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
687 "object file without providing a relocation section.");
689 if (!Relas)
690 return createError("unable to read relocations for section " +
691 describe(EF, Sec) + ": " +
692 toString(Relas.takeError()));
693 for (typename ELFFile<ELFT>::Elf_Rela Rela : *Relas)
694 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
695 }
696 auto GetAddressForRelocation =
697 [&](unsigned RelocationOffsetInSection) -> Expected<unsigned> {
698 auto FOTIterator =
699 FunctionOffsetTranslations.find(RelocationOffsetInSection);
700 if (FOTIterator == FunctionOffsetTranslations.end()) {
701 return createError("failed to get relocation data for offset: " +
702 Twine::utohexstr(RelocationOffsetInSection) +
703 " in section " + describe(EF, Sec));
704 }
705 return FOTIterator->second;
706 };
707 Expected<ArrayRef<uint8_t>> ContentsOrErr = EF.getSectionContents(Sec);
708 if (!ContentsOrErr)
709 return ContentsOrErr.takeError();
710 ArrayRef<uint8_t> Content = *ContentsOrErr;
711 DataExtractor Data(Content, EF.isLE(), ELFT::Is64Bits ? 8 : 4);
712 std::vector<BBAddrMap> FunctionEntries;
713
715 Error ULEBSizeErr = Error::success();
716 Error MetadataDecodeErr = Error::success();
717
718 // Helper lampda to extract the (possiblly relocatable) address stored at Cur.
719 auto ExtractAddress = [&]() -> Expected<typename ELFFile<ELFT>::uintX_t> {
720 uint64_t RelocationOffsetInSection = Cur.tell();
721 auto Address =
722 static_cast<typename ELFFile<ELFT>::uintX_t>(Data.getAddress(Cur));
723 if (!Cur)
724 return Cur.takeError();
725 if (!IsRelocatable)
726 return Address;
727 assert(Address == 0);
728 Expected<unsigned> AddressOrErr =
729 GetAddressForRelocation(RelocationOffsetInSection);
730 if (!AddressOrErr)
731 return AddressOrErr.takeError();
732 return *AddressOrErr;
733 };
734
735 uint8_t Version = 0;
736 uint8_t Feature = 0;
737 BBAddrMap::Features FeatEnable{};
738 while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
739 Cur.tell() < Content.size()) {
740 if (Sec.sh_type == ELF::SHT_LLVM_BB_ADDR_MAP) {
741 Version = Data.getU8(Cur);
742 if (!Cur)
743 break;
744 if (Version > 2)
745 return createError("unsupported SHT_LLVM_BB_ADDR_MAP version: " +
746 Twine(static_cast<int>(Version)));
747 Feature = Data.getU8(Cur); // Feature byte
748 if (!Cur)
749 break;
750 auto FeatEnableOrErr = BBAddrMap::Features::decode(Feature);
751 if (!FeatEnableOrErr)
752 return FeatEnableOrErr.takeError();
753 FeatEnable = *FeatEnableOrErr;
754 if (Feature != 0 && Version < 2 && Cur)
755 return createError(
756 "version should be >= 2 for SHT_LLVM_BB_ADDR_MAP when "
757 "PGO features are enabled: version = " +
758 Twine(static_cast<int>(Version)) +
759 " feature = " + Twine(static_cast<int>(Feature)));
760 }
761 uint32_t NumBlocksInBBRange = 0;
762 uint32_t NumBBRanges = 1;
763 typename ELFFile<ELFT>::uintX_t RangeBaseAddress = 0;
764 std::vector<BBAddrMap::BBEntry> BBEntries;
765 if (FeatEnable.MultiBBRange) {
766 NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
767 if (!Cur || ULEBSizeErr)
768 break;
769 if (!NumBBRanges)
770 return createError("invalid zero number of BB ranges at offset " +
771 Twine::utohexstr(Cur.tell()) + " in " +
772 describe(EF, Sec));
773 } else {
774 auto AddressOrErr = ExtractAddress();
775 if (!AddressOrErr)
776 return AddressOrErr.takeError();
777 RangeBaseAddress = *AddressOrErr;
778 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
779 }
780 std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
781 uint32_t TotalNumBlocks = 0;
782 for (uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
783 ++BBRangeIndex) {
784 uint32_t PrevBBEndOffset = 0;
785 if (FeatEnable.MultiBBRange) {
786 auto AddressOrErr = ExtractAddress();
787 if (!AddressOrErr)
788 return AddressOrErr.takeError();
789 RangeBaseAddress = *AddressOrErr;
790 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
791 }
792 for (uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr && Cur &&
793 (BlockIndex < NumBlocksInBBRange);
794 ++BlockIndex) {
795 uint32_t ID = Version >= 2
796 ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
797 : BlockIndex;
798 uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
799 uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
800 uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
801 if (Version >= 1) {
802 // Offset is calculated relative to the end of the previous BB.
803 Offset += PrevBBEndOffset;
804 PrevBBEndOffset = Offset + Size;
805 }
808 if (!MetadataOrErr) {
809 MetadataDecodeErr = MetadataOrErr.takeError();
810 break;
811 }
812 BBEntries.push_back({ID, Offset, Size, *MetadataOrErr});
813 }
814 TotalNumBlocks += BBEntries.size();
815 BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
816 }
817 FunctionEntries.push_back({std::move(BBRangeEntries)});
818
819 if (PGOAnalyses || FeatEnable.hasPGOAnalysis()) {
820 // Function entry count
822 FeatEnable.FuncEntryCount
823 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
824 : 0;
825
826 std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
827 for (uint32_t BlockIndex = 0;
828 FeatEnable.hasPGOAnalysisBBData() && !MetadataDecodeErr &&
829 !ULEBSizeErr && Cur && (BlockIndex < TotalNumBlocks);
830 ++BlockIndex) {
831 // Block frequency
832 uint64_t BBF = FeatEnable.BBFreq
833 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
834 : 0;
835
836 // Branch probability
838 Successors;
839 if (FeatEnable.BrProb) {
840 auto SuccCount = readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr);
841 for (uint64_t I = 0; I < SuccCount; ++I) {
842 uint32_t BBID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
843 uint32_t BrProb = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
844 if (PGOAnalyses)
845 Successors.push_back({BBID, BranchProbability::getRaw(BrProb)});
846 }
847 }
848
849 if (PGOAnalyses)
850 PGOBBEntries.push_back({BlockFrequency(BBF), std::move(Successors)});
851 }
852
853 if (PGOAnalyses)
854 PGOAnalyses->push_back(
855 {FuncEntryCount, std::move(PGOBBEntries), FeatEnable});
856 }
857 }
858 // Either Cur is in the error state, or we have an error in ULEBSizeErr or
859 // MetadataDecodeErr (but not both), but we join all errors here to be safe.
860 if (!Cur || ULEBSizeErr || MetadataDecodeErr)
861 return joinErrors(joinErrors(Cur.takeError(), std::move(ULEBSizeErr)),
862 std::move(MetadataDecodeErr));
863 return FunctionEntries;
864}
865
866template <class ELFT>
868ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec,
869 std::vector<PGOAnalysisMap> *PGOAnalyses) const {
870 size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
871 auto AddrMapsOrErr = decodeBBAddrMapImpl(*this, Sec, RelaSec, PGOAnalyses);
872 // remove new analyses when an error occurs
873 if (!AddrMapsOrErr && PGOAnalyses)
874 PGOAnalyses->resize(OriginalPGOSize);
875 return std::move(AddrMapsOrErr);
876}
877
878template <class ELFT>
882 std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const {
884 Error Errors = Error::success();
885 for (const Elf_Shdr &Sec : cantFail(this->sections())) {
886 Expected<bool> DoesSectionMatch = IsMatch(Sec);
887 if (!DoesSectionMatch) {
888 Errors = joinErrors(std::move(Errors), DoesSectionMatch.takeError());
889 continue;
890 }
891 if (*DoesSectionMatch) {
892 if (SecToRelocMap.insert(std::make_pair(&Sec, (const Elf_Shdr *)nullptr))
893 .second)
894 continue;
895 }
896
897 if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL)
898 continue;
899
900 Expected<const Elf_Shdr *> RelSecOrErr = this->getSection(Sec.sh_info);
901 if (!RelSecOrErr) {
902 Errors = joinErrors(std::move(Errors),
903 createError(describe(*this, Sec) +
904 ": failed to get a relocated section: " +
905 toString(RelSecOrErr.takeError())));
906 continue;
907 }
908 const Elf_Shdr *ContentsSec = *RelSecOrErr;
909 Expected<bool> DoesRelTargetMatch = IsMatch(*ContentsSec);
910 if (!DoesRelTargetMatch) {
911 Errors = joinErrors(std::move(Errors), DoesRelTargetMatch.takeError());
912 continue;
913 }
914 if (*DoesRelTargetMatch)
915 SecToRelocMap[ContentsSec] = &Sec;
916 }
917 if(Errors)
918 return std::move(Errors);
919 return SecToRelocMap;
920}
921
922template class llvm::object::ELFFile<ELF32LE>;
923template class llvm::object::ELFFile<ELF32BE>;
924template class llvm::object::ELFFile<ELF64LE>;
925template class llvm::object::ELFFile<ELF64BE>;
bbsections Prepares for basic block sections
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:371
T Content
uint64_t Addr
uint64_t Size
#define I(x, y, z)
Definition: MD5.cpp:58
static IntTy readULEB128As(DataExtractor &Data, DataExtractor::Cursor &Cur, Error &ULEBSizeErr)
Definition: ELF.cpp:655
#define STRINGIFY_ENUM_CASE(ns, name)
Definition: ELF.cpp:17
static Expected< std::vector< BBAddrMap > > decodeBBAddrMapImpl(const ELFFile< ELFT > &EF, const typename ELFFile< ELFT >::Elf_Shdr &Sec, const typename ELFFile< ELFT >::Elf_Shdr *RelaSec, std::vector< PGOAnalysisMap > *PGOAnalyses)
Definition: ELF.cpp:674
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some functions that are useful when dealing with strings.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:174
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
const T * data() const
Definition: ArrayRef.h:162
static BranchProbability getRaw(uint32_t N)
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
uint64_t tell() const
Return the current position of this Cursor.
Definition: DataExtractor.h:71
Error takeError()
Return error contained inside this Cursor, if any.
Definition: DataExtractor.h:78
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
iterator end()
Definition: DenseMap.h:84
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:141
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:416
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
An efficient, type-erasing, non-owning reference to a callable.
const Elf_Ehdr & getHeader() const
Definition: ELF.h:235
Expected< std::vector< Elf_Rela > > android_relas(const Elf_Shdr &Sec) const
Definition: ELF.cpp:394
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
Definition: ELF.cpp:462
Expected< std::vector< BBAddrMap > > decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec=nullptr, std::vector< PGOAnalysisMap > *PGOAnalyses=nullptr) const
Returns a vector of BBAddrMap structs corresponding to each function within the text section that the...
Definition: ELF.cpp:868
Expected< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr &Sec) const
Definition: ELF.h:623
Expected< Elf_Rela_Range > relas(const Elf_Shdr &Sec) const
Definition: ELF.h:310
Expected< Elf_Dyn_Range > dynamicEntries() const
Definition: ELF.cpp:551
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.cpp:599
Expected< MapVector< const Elf_Shdr *, const Elf_Shdr * > > getSectionAndRelocations(std::function< Expected< bool >(const Elf_Shdr &)> IsMatch) const
Returns a map from every section matching IsMatch to its relocation section, or nullptr if it has no ...
Definition: ELF.cpp:881
bool isLE() const
Definition: ELF.h:285
std::vector< Elf_Rel > decode_relrs(Elf_Relr_Range relrs) const
Definition: ELF.cpp:330
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ EM_MSP430
Definition: ELF.h:222
@ EM_S390
Definition: ELF.h:150
@ EM_PPC64
Definition: ELF.h:149
@ EM_SPARC
Definition: ELF.h:135
@ EM_CSKY
Definition: ELF.h:321
@ EM_SPARC32PLUS
Definition: ELF.h:146
@ EM_MIPS_RS3_LE
Definition: ELF.h:143
@ EM_68K
Definition: ELF.h:137
@ EM_386
Definition: ELF.h:136
@ EM_LOONGARCH
Definition: ELF.h:322
@ EM_BPF
Definition: ELF.h:319
@ EM_PPC
Definition: ELF.h:148
@ EM_X86_64
Definition: ELF.h:178
@ EM_HEXAGON
Definition: ELF.h:257
@ EM_LANAI
Definition: ELF.h:318
@ EM_MIPS
Definition: ELF.h:141
@ EM_SPARCV9
Definition: ELF.h:159
@ EM_AARCH64
Definition: ELF.h:280
@ EM_XTENSA
Definition: ELF.h:211
@ EM_ARC_COMPACT2
Definition: ELF.h:291
@ EM_RISCV
Definition: ELF.h:317
@ EM_ARC_COMPACT
Definition: ELF.h:209
@ EM_ARM
Definition: ELF.h:156
@ EM_VE
Definition: ELF.h:320
@ EM_IAMCU
Definition: ELF.h:139
@ EM_AMDGPU
Definition: ELF.h:316
@ EM_AVR
Definition: ELF.h:199
@ SHT_REL
Definition: ELF.h:1071
@ SHT_DYNAMIC
Definition: ELF.h:1068
@ SHT_LLVM_BB_ADDR_MAP
Definition: ELF.h:1100
@ SHT_RELA
Definition: ELF.h:1066
@ RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG
Definition: ELF.h:1874
@ RELOCATION_GROUPED_BY_INFO_FLAG
Definition: ELF.h:1873
@ RELOCATION_GROUPED_BY_ADDEND_FLAG
Definition: ELF.h:1875
@ RELOCATION_GROUP_HAS_ADDEND_FLAG
Definition: ELF.h:1876
@ PT_LOAD
Definition: ELF.h:1454
@ PT_DYNAMIC
Definition: ELF.h:1455
@ ET_REL
Definition: ELF.h:116
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:485
Error createError(const Twine &Err)
Definition: Error.h:84
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:23
uint32_t getELFRelativeRelocationType(uint32_t Machine)
Definition: ELF.cpp:192
static std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
Definition: ELF.h:142
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
void stable_sort(R &&Range)
Definition: STLExtras.h:1975
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1963
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:431
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
Definition: STLExtras.h:1911
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:749
static Expected< Metadata > decode(uint32_t V)
Definition: ELFTypes.h:868
static Expected< Features > decode(uint8_t Val)
Definition: ELFTypes.h:822