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