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) {
255 STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED);
256 STRINGIFY_ENUM_CASE(ELF, SHT_HEXAGON_ATTRIBUTES);
257 }
258 break;
259 case ELF::EM_X86_64:
260 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
261 break;
262 case ELF::EM_MIPS:
264 switch (Type) {
265 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
266 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
267 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
268 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
269 }
270 break;
271 case ELF::EM_MSP430:
272 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_MSP430_ATTRIBUTES); }
273 break;
274 case ELF::EM_RISCV:
275 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
276 break;
277 case ELF::EM_AARCH64:
278 switch (Type) {
279 STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_AUTH_RELR);
280 STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC);
281 STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_STATIC);
282 }
283 default:
284 break;
285 }
286
287 switch (Type) {
288 STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
289 STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
290 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
291 STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
292 STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
293 STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
294 STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
295 STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
296 STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
297 STRINGIFY_ENUM_CASE(ELF, SHT_REL);
298 STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
299 STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
300 STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
301 STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
302 STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
303 STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
304 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
305 STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
306 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
307 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
308 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
309 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
310 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
311 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
312 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
313 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
314 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
315 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
316 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
317 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP_V0);
318 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP);
319 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_OFFLOADING);
320 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LTO);
321 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
322 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
323 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
324 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
325 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
326 default:
327 return "Unknown";
328 }
329}
330
331template <class ELFT>
332std::vector<typename ELFT::Rel>
333ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
334 // This function decodes the contents of an SHT_RELR packed relocation
335 // section.
336 //
337 // Proposal for adding SHT_RELR sections to generic-abi is here:
338 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
339 //
340 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
341 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
342 //
343 // i.e. start with an address, followed by any number of bitmaps. The address
344 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
345 // relocations each, at subsequent offsets following the last address entry.
346 //
347 // The bitmap entries must have 1 in the least significant bit. The assumption
348 // here is that an address cannot have 1 in lsb. Odd addresses are not
349 // supported.
350 //
351 // Excluding the least significant bit in the bitmap, each non-zero bit in
352 // the bitmap represents a relocation to be applied to a corresponding machine
353 // word that follows the base address word. The second least significant bit
354 // represents the machine word immediately following the initial address, and
355 // each bit that follows represents the next word, in linear order. As such,
356 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
357 // 63 relocations in a 64-bit object.
358 //
359 // This encoding has a couple of interesting properties:
360 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
361 // even means address, odd means bitmap.
362 // 2. Just a simple list of addresses is a valid encoding.
363
364 Elf_Rel Rel;
365 Rel.r_info = 0;
366 Rel.setType(getRelativeRelocationType(), false);
367 std::vector<Elf_Rel> Relocs;
368
369 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
370 using Addr = typename ELFT::uint;
371
372 Addr Base = 0;
373 for (Elf_Relr R : relrs) {
374 typename ELFT::uint Entry = R;
375 if ((Entry & 1) == 0) {
376 // Even entry: encodes the offset for next relocation.
377 Rel.r_offset = Entry;
378 Relocs.push_back(Rel);
379 // Set base offset for subsequent bitmap entries.
380 Base = Entry + sizeof(Addr);
381 } else {
382 // Odd entry: encodes bitmap for relocations starting at base.
383 for (Addr Offset = Base; (Entry >>= 1) != 0; Offset += sizeof(Addr))
384 if ((Entry & 1) != 0) {
385 Rel.r_offset = Offset;
386 Relocs.push_back(Rel);
387 }
388 Base += (CHAR_BIT * sizeof(Entry) - 1) * sizeof(Addr);
389 }
390 }
391
392 return Relocs;
393}
394
395template <class ELFT>
397ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
398 // This function reads relocations in Android's packed relocation format,
399 // which is based on SLEB128 and delta encoding.
400 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
401 if (!ContentsOrErr)
402 return ContentsOrErr.takeError();
403 ArrayRef<uint8_t> Content = *ContentsOrErr;
404 if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' ||
405 Content[2] != 'S' || Content[3] != '2')
406 return createError("invalid packed relocation header");
407 DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
408 DataExtractor::Cursor Cur(/*Offset=*/4);
409
410 uint64_t NumRelocs = Data.getSLEB128(Cur);
411 uint64_t Offset = Data.getSLEB128(Cur);
412 uint64_t Addend = 0;
413
414 if (!Cur)
415 return std::move(Cur.takeError());
416
417 std::vector<Elf_Rela> Relocs;
418 Relocs.reserve(NumRelocs);
419 while (NumRelocs) {
420 uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
421 if (!Cur)
422 return std::move(Cur.takeError());
423 if (NumRelocsInGroup > NumRelocs)
424 return createError("relocation group unexpectedly large");
425 NumRelocs -= NumRelocsInGroup;
426
427 uint64_t GroupFlags = Data.getSLEB128(Cur);
428 bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
429 bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
430 bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
431 bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
432
433 uint64_t GroupOffsetDelta;
434 if (GroupedByOffsetDelta)
435 GroupOffsetDelta = Data.getSLEB128(Cur);
436
437 uint64_t GroupRInfo;
438 if (GroupedByInfo)
439 GroupRInfo = Data.getSLEB128(Cur);
440
441 if (GroupedByAddend && GroupHasAddend)
442 Addend += Data.getSLEB128(Cur);
443
444 if (!GroupHasAddend)
445 Addend = 0;
446
447 for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) {
448 Elf_Rela R;
449 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
450 R.r_offset = Offset;
451 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
452 if (GroupHasAddend && !GroupedByAddend)
453 Addend += Data.getSLEB128(Cur);
454 R.r_addend = Addend;
455 Relocs.push_back(R);
456 }
457 if (!Cur)
458 return std::move(Cur.takeError());
459 }
460
461 return Relocs;
462}
463
464template <class ELFT>
465std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
466 uint64_t Type) const {
467#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
468 case value: \
469 return #tag;
470
471#define DYNAMIC_TAG(n, v)
472 switch (Arch) {
473 case ELF::EM_AARCH64:
474 switch (Type) {
475#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
476#include "llvm/BinaryFormat/DynamicTags.def"
477#undef AARCH64_DYNAMIC_TAG
478 }
479 break;
480
481 case ELF::EM_HEXAGON:
482 switch (Type) {
483#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
484#include "llvm/BinaryFormat/DynamicTags.def"
485#undef HEXAGON_DYNAMIC_TAG
486 }
487 break;
488
489 case ELF::EM_MIPS:
490 switch (Type) {
491#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
492#include "llvm/BinaryFormat/DynamicTags.def"
493#undef MIPS_DYNAMIC_TAG
494 }
495 break;
496
497 case ELF::EM_PPC:
498 switch (Type) {
499#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
500#include "llvm/BinaryFormat/DynamicTags.def"
501#undef PPC_DYNAMIC_TAG
502 }
503 break;
504
505 case ELF::EM_PPC64:
506 switch (Type) {
507#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
508#include "llvm/BinaryFormat/DynamicTags.def"
509#undef PPC64_DYNAMIC_TAG
510 }
511 break;
512
513 case ELF::EM_RISCV:
514 switch (Type) {
515#define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
516#include "llvm/BinaryFormat/DynamicTags.def"
517#undef RISCV_DYNAMIC_TAG
518 }
519 break;
520 }
521#undef DYNAMIC_TAG
522 switch (Type) {
523// Now handle all dynamic tags except the architecture specific ones
524#define AARCH64_DYNAMIC_TAG(name, value)
525#define MIPS_DYNAMIC_TAG(name, value)
526#define HEXAGON_DYNAMIC_TAG(name, value)
527#define PPC_DYNAMIC_TAG(name, value)
528#define PPC64_DYNAMIC_TAG(name, value)
529#define RISCV_DYNAMIC_TAG(name, value)
530// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
531#define DYNAMIC_TAG_MARKER(name, value)
532#define DYNAMIC_TAG(name, value) case value: return #name;
533#include "llvm/BinaryFormat/DynamicTags.def"
534#undef DYNAMIC_TAG
535#undef AARCH64_DYNAMIC_TAG
536#undef MIPS_DYNAMIC_TAG
537#undef HEXAGON_DYNAMIC_TAG
538#undef PPC_DYNAMIC_TAG
539#undef PPC64_DYNAMIC_TAG
540#undef RISCV_DYNAMIC_TAG
541#undef DYNAMIC_TAG_MARKER
542#undef DYNAMIC_STRINGIFY_ENUM
543 default:
544 return "<unknown:>0x" + utohexstr(Type, true);
545 }
546}
547
548template <class ELFT>
550 return getDynamicTagAsString(getHeader().e_machine, Type);
551}
552
553template <class ELFT>
556
557 auto ProgramHeadersOrError = program_headers();
558 if (!ProgramHeadersOrError)
559 return ProgramHeadersOrError.takeError();
560
561 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
562 if (Phdr.p_type == ELF::PT_DYNAMIC) {
563 const uint8_t *DynOffset = base() + Phdr.p_offset;
564 if (DynOffset > end())
565 return createError(
566 "dynamic section offset past file size: corrupted ELF");
567 Dyn = ArrayRef(reinterpret_cast<const Elf_Dyn *>(DynOffset),
568 Phdr.p_filesz / sizeof(Elf_Dyn));
569 break;
570 }
571 }
572
573 // If we can't find the dynamic section in the program headers, we just fall
574 // back on the sections.
575 if (Dyn.empty()) {
576 auto SectionsOrError = sections();
577 if (!SectionsOrError)
578 return SectionsOrError.takeError();
579
580 for (const Elf_Shdr &Sec : *SectionsOrError) {
581 if (Sec.sh_type == ELF::SHT_DYNAMIC) {
582 Expected<ArrayRef<Elf_Dyn>> DynOrError =
583 getSectionContentsAsArray<Elf_Dyn>(Sec);
584 if (!DynOrError)
585 return DynOrError.takeError();
586 Dyn = *DynOrError;
587 break;
588 }
589 }
590
591 if (!Dyn.data())
592 return ArrayRef<Elf_Dyn>();
593 }
594
595 if (Dyn.empty())
596 return createError("invalid empty dynamic section");
597
598 if (Dyn.back().d_tag != ELF::DT_NULL)
599 return createError("dynamic sections must be DT_NULL terminated");
600
601 return Dyn;
602}
603
604template <class ELFT>
607 auto ProgramHeadersOrError = program_headers();
608 if (!ProgramHeadersOrError)
609 return ProgramHeadersOrError.takeError();
610
612
613 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
614 if (Phdr.p_type == ELF::PT_LOAD)
615 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
616
617 auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A,
618 const Elf_Phdr_Impl<ELFT> *B) {
619 return A->p_vaddr < B->p_vaddr;
620 };
621 if (!llvm::is_sorted(LoadSegments, SortPred)) {
622 if (Error E =
623 WarnHandler("loadable segments are unsorted by virtual address"))
624 return std::move(E);
625 llvm::stable_sort(LoadSegments, SortPred);
626 }
627
628 const Elf_Phdr *const *I = llvm::upper_bound(
629 LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
630 return VAddr < Phdr->p_vaddr;
631 });
632
633 if (I == LoadSegments.begin())
634 return createError("virtual address is not in any segment: 0x" +
635 Twine::utohexstr(VAddr));
636 --I;
637 const Elf_Phdr &Phdr = **I;
638 uint64_t Delta = VAddr - Phdr.p_vaddr;
639 if (Delta >= Phdr.p_filesz)
640 return createError("virtual address is not in any segment: 0x" +
641 Twine::utohexstr(VAddr));
642
643 uint64_t Offset = Phdr.p_offset + Delta;
644 if (Offset >= getBufSize())
645 return createError("can't map virtual address 0x" +
646 Twine::utohexstr(VAddr) + " to the segment with index " +
647 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
648 ": the segment ends at 0x" +
649 Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
650 ", which is greater than the file size (0x" +
651 Twine::utohexstr(getBufSize()) + ")");
652
653 return base() + Offset;
654}
655
656// Helper to extract and decode the next ULEB128 value as unsigned int.
657// Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the unsigned
658// int limit.
659// Also returns zero if ULEBSizeErr is already in an error state.
660// ULEBSizeErr is an out variable if an error occurs.
661template <typename IntTy, std::enable_if_t<std::is_unsigned_v<IntTy>, int> = 0>
663 Error &ULEBSizeErr) {
664 // Bail out and do not extract data if ULEBSizeErr is already set.
665 if (ULEBSizeErr)
666 return 0;
667 uint64_t Offset = Cur.tell();
668 uint64_t Value = Data.getULEB128(Cur);
669 if (Value > std::numeric_limits<IntTy>::max()) {
670 ULEBSizeErr = createError("ULEB128 value at offset 0x" +
671 Twine::utohexstr(Offset) + " exceeds UINT" +
672 Twine(std::numeric_limits<IntTy>::digits) +
673 "_MAX (0x" + Twine::utohexstr(Value) + ")");
674 return 0;
675 }
676 return static_cast<IntTy>(Value);
677}
678
679template <typename ELFT>
682 const typename ELFFile<ELFT>::Elf_Shdr &Sec,
683 const typename ELFFile<ELFT>::Elf_Shdr *RelaSec,
684 std::vector<PGOAnalysisMap> *PGOAnalyses) {
685 bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL;
686
687 // This DenseMap maps the offset of each function (the location of the
688 // reference to the function in the SHT_LLVM_BB_ADDR_MAP section) to the
689 // addend (the location of the function in the text section).
690 llvm::DenseMap<uint64_t, uint64_t> FunctionOffsetTranslations;
691 if (IsRelocatable && RelaSec) {
692 assert(RelaSec &&
693 "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
694 "object file without providing a relocation section.");
696 if (!Relas)
697 return createError("unable to read relocations for section " +
698 describe(EF, Sec) + ": " +
699 toString(Relas.takeError()));
700 for (typename ELFFile<ELFT>::Elf_Rela Rela : *Relas)
701 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
702 }
703 auto GetAddressForRelocation =
704 [&](unsigned RelocationOffsetInSection) -> Expected<unsigned> {
705 auto FOTIterator =
706 FunctionOffsetTranslations.find(RelocationOffsetInSection);
707 if (FOTIterator == FunctionOffsetTranslations.end()) {
708 return createError("failed to get relocation data for offset: " +
709 Twine::utohexstr(RelocationOffsetInSection) +
710 " in section " + describe(EF, Sec));
711 }
712 return FOTIterator->second;
713 };
714 Expected<ArrayRef<uint8_t>> ContentsOrErr = EF.getSectionContents(Sec);
715 if (!ContentsOrErr)
716 return ContentsOrErr.takeError();
717 ArrayRef<uint8_t> Content = *ContentsOrErr;
718 DataExtractor Data(Content, EF.isLE(), ELFT::Is64Bits ? 8 : 4);
719 std::vector<BBAddrMap> FunctionEntries;
720
722 Error ULEBSizeErr = Error::success();
723 Error MetadataDecodeErr = Error::success();
724
725 // Helper lampda to extract the (possiblly relocatable) address stored at Cur.
726 auto ExtractAddress = [&]() -> Expected<typename ELFFile<ELFT>::uintX_t> {
727 uint64_t RelocationOffsetInSection = Cur.tell();
728 auto Address =
729 static_cast<typename ELFFile<ELFT>::uintX_t>(Data.getAddress(Cur));
730 if (!Cur)
731 return Cur.takeError();
732 if (!IsRelocatable)
733 return Address;
734 assert(Address == 0);
735 Expected<unsigned> AddressOrErr =
736 GetAddressForRelocation(RelocationOffsetInSection);
737 if (!AddressOrErr)
738 return AddressOrErr.takeError();
739 return *AddressOrErr;
740 };
741
742 uint8_t Version = 0;
743 uint8_t Feature = 0;
744 BBAddrMap::Features FeatEnable{};
745 while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
746 Cur.tell() < Content.size()) {
747 if (Sec.sh_type == ELF::SHT_LLVM_BB_ADDR_MAP) {
748 Version = Data.getU8(Cur);
749 if (!Cur)
750 break;
751 if (Version > 2)
752 return createError("unsupported SHT_LLVM_BB_ADDR_MAP version: " +
753 Twine(static_cast<int>(Version)));
754 Feature = Data.getU8(Cur); // Feature byte
755 if (!Cur)
756 break;
757 auto FeatEnableOrErr = BBAddrMap::Features::decode(Feature);
758 if (!FeatEnableOrErr)
759 return FeatEnableOrErr.takeError();
760 FeatEnable = *FeatEnableOrErr;
761 if (Feature != 0 && Version < 2 && Cur)
762 return createError(
763 "version should be >= 2 for SHT_LLVM_BB_ADDR_MAP when "
764 "PGO features are enabled: version = " +
765 Twine(static_cast<int>(Version)) +
766 " feature = " + Twine(static_cast<int>(Feature)));
767 }
768 uint32_t NumBlocksInBBRange = 0;
769 uint32_t NumBBRanges = 1;
770 typename ELFFile<ELFT>::uintX_t RangeBaseAddress = 0;
771 std::vector<BBAddrMap::BBEntry> BBEntries;
772 if (FeatEnable.MultiBBRange) {
773 NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
774 if (!Cur || ULEBSizeErr)
775 break;
776 if (!NumBBRanges)
777 return createError("invalid zero number of BB ranges at offset " +
778 Twine::utohexstr(Cur.tell()) + " in " +
779 describe(EF, Sec));
780 } else {
781 auto AddressOrErr = ExtractAddress();
782 if (!AddressOrErr)
783 return AddressOrErr.takeError();
784 RangeBaseAddress = *AddressOrErr;
785 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
786 }
787 std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
788 uint32_t TotalNumBlocks = 0;
789 for (uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
790 ++BBRangeIndex) {
791 uint32_t PrevBBEndOffset = 0;
792 if (FeatEnable.MultiBBRange) {
793 auto AddressOrErr = ExtractAddress();
794 if (!AddressOrErr)
795 return AddressOrErr.takeError();
796 RangeBaseAddress = *AddressOrErr;
797 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
798 }
799 for (uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr && Cur &&
800 (BlockIndex < NumBlocksInBBRange);
801 ++BlockIndex) {
802 uint32_t ID = Version >= 2
803 ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
804 : BlockIndex;
805 uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
806 uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
807 uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
808 if (Version >= 1) {
809 // Offset is calculated relative to the end of the previous BB.
810 Offset += PrevBBEndOffset;
811 PrevBBEndOffset = Offset + Size;
812 }
815 if (!MetadataOrErr) {
816 MetadataDecodeErr = MetadataOrErr.takeError();
817 break;
818 }
819 BBEntries.push_back({ID, Offset, Size, *MetadataOrErr});
820 }
821 TotalNumBlocks += BBEntries.size();
822 BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
823 }
824 FunctionEntries.push_back({std::move(BBRangeEntries)});
825
826 if (PGOAnalyses || FeatEnable.hasPGOAnalysis()) {
827 // Function entry count
829 FeatEnable.FuncEntryCount
830 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
831 : 0;
832
833 std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
834 for (uint32_t BlockIndex = 0;
835 FeatEnable.hasPGOAnalysisBBData() && !MetadataDecodeErr &&
836 !ULEBSizeErr && Cur && (BlockIndex < TotalNumBlocks);
837 ++BlockIndex) {
838 // Block frequency
839 uint64_t BBF = FeatEnable.BBFreq
840 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
841 : 0;
842
843 // Branch probability
845 Successors;
846 if (FeatEnable.BrProb) {
847 auto SuccCount = readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr);
848 for (uint64_t I = 0; I < SuccCount; ++I) {
849 uint32_t BBID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
850 uint32_t BrProb = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
851 if (PGOAnalyses)
852 Successors.push_back({BBID, BranchProbability::getRaw(BrProb)});
853 }
854 }
855
856 if (PGOAnalyses)
857 PGOBBEntries.push_back({BlockFrequency(BBF), std::move(Successors)});
858 }
859
860 if (PGOAnalyses)
861 PGOAnalyses->push_back(
862 {FuncEntryCount, std::move(PGOBBEntries), FeatEnable});
863 }
864 }
865 // Either Cur is in the error state, or we have an error in ULEBSizeErr or
866 // MetadataDecodeErr (but not both), but we join all errors here to be safe.
867 if (!Cur || ULEBSizeErr || MetadataDecodeErr)
868 return joinErrors(joinErrors(Cur.takeError(), std::move(ULEBSizeErr)),
869 std::move(MetadataDecodeErr));
870 return FunctionEntries;
871}
872
873template <class ELFT>
875ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec,
876 std::vector<PGOAnalysisMap> *PGOAnalyses) const {
877 size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
878 auto AddrMapsOrErr = decodeBBAddrMapImpl(*this, Sec, RelaSec, PGOAnalyses);
879 // remove new analyses when an error occurs
880 if (!AddrMapsOrErr && PGOAnalyses)
881 PGOAnalyses->resize(OriginalPGOSize);
882 return std::move(AddrMapsOrErr);
883}
884
885template <class ELFT>
889 std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const {
891 Error Errors = Error::success();
892 for (const Elf_Shdr &Sec : cantFail(this->sections())) {
893 Expected<bool> DoesSectionMatch = IsMatch(Sec);
894 if (!DoesSectionMatch) {
895 Errors = joinErrors(std::move(Errors), DoesSectionMatch.takeError());
896 continue;
897 }
898 if (*DoesSectionMatch) {
899 if (SecToRelocMap.insert(std::make_pair(&Sec, (const Elf_Shdr *)nullptr))
900 .second)
901 continue;
902 }
903
904 if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL)
905 continue;
906
907 Expected<const Elf_Shdr *> RelSecOrErr = this->getSection(Sec.sh_info);
908 if (!RelSecOrErr) {
909 Errors = joinErrors(std::move(Errors),
910 createError(describe(*this, Sec) +
911 ": failed to get a relocated section: " +
912 toString(RelSecOrErr.takeError())));
913 continue;
914 }
915 const Elf_Shdr *ContentsSec = *RelSecOrErr;
916 Expected<bool> DoesRelTargetMatch = IsMatch(*ContentsSec);
917 if (!DoesRelTargetMatch) {
918 Errors = joinErrors(std::move(Errors), DoesRelTargetMatch.takeError());
919 continue;
920 }
921 if (*DoesRelTargetMatch)
922 SecToRelocMap[ContentsSec] = &Sec;
923 }
924 if(Errors)
925 return std::move(Errors);
926 return SecToRelocMap;
927}
928
929template class llvm::object::ELFFile<ELF32LE>;
930template class llvm::object::ELFFile<ELF32BE>;
931template class llvm::object::ELFFile<ELF64LE>;
932template 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")
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:662
#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:681
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:397
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
Definition: ELF.cpp:465
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:875
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:554
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition: ELF.cpp:606
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:888
bool isLE() const
Definition: ELF.h:285
std::vector< Elf_Rel > decode_relrs(Elf_Relr_Range relrs) const
Definition: ELF.cpp:333
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:1892
@ RELOCATION_GROUPED_BY_INFO_FLAG
Definition: ELF.h:1891
@ RELOCATION_GROUPED_BY_ADDEND_FLAG
Definition: ELF.h:1893
@ RELOCATION_GROUP_HAS_ADDEND_FLAG
Definition: ELF.h:1894
@ PT_LOAD
Definition: ELF.h:1456
@ PT_DYNAMIC
Definition: ELF.h:1457
@ 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:1995
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:1967
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:1902
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