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"
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/Support/DataExtractor.h"
13
14 using namespace llvm;
15 using 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
getELFRelocationTypeName(uint32_t Machine,uint32_t Type)23 StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
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;
69 case ELF::EM_ARC_COMPACT:
70 case ELF::EM_ARC_COMPACT2:
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:
127 case ELF::EM_SPARC32PLUS:
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;
170 case ELF::EM_LOONGARCH:
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
getELFRelativeRelocationType(uint32_t Machine)192 uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
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;
205 case ELF::EM_ARC_COMPACT:
206 case ELF::EM_ARC_COMPACT2:
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:
223 case ELF::EM_SPARC32PLUS:
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;
234 case ELF::EM_LOONGARCH:
235 return ELF::R_LARCH_RELATIVE;
236 default:
237 break;
238 }
239 return 0;
240 }
241
getELFSectionTypeName(uint32_t Machine,unsigned Type)242 StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
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:
260 case ELF::EM_MIPS_RS3_LE:
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
328 template <class ELFT>
329 std::vector<typename ELFT::Rel>
decode_relrs(Elf_Relr_Range relrs) const330 ELFFile<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
392 template <class ELFT>
393 Expected<std::vector<typename ELFT::Rela>>
android_relas(const Elf_Shdr & Sec) const394 ELFFile<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
461 template <class ELFT>
getDynamicTagAsString(unsigned Arch,uint64_t Type) const462 std::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
545 template <class ELFT>
getDynamicTagAsString(uint64_t Type) const546 std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
547 return getDynamicTagAsString(getHeader().e_machine, Type);
548 }
549
550 template <class ELFT>
dynamicEntries() const551 Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
552 ArrayRef<Elf_Dyn> Dyn;
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
597 template <class ELFT>
598 Expected<const uint8_t *>
toMappedAddr(uint64_t VAddr,WarningHandler WarnHandler) const599 ELFFile<ELFT>::toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler) const {
600 auto ProgramHeadersOrError = program_headers();
601 if (!ProgramHeadersOrError)
602 return ProgramHeadersOrError.takeError();
603
604 llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
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.
654 template <typename IntTy, std::enable_if_t<std::is_unsigned_v<IntTy>, int> = 0>
readULEB128As(DataExtractor & Data,DataExtractor::Cursor & Cur,Error & ULEBSizeErr)655 static IntTy readULEB128As(DataExtractor &Data, DataExtractor::Cursor &Cur,
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
672 template <typename ELFT>
673 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)674 decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
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.");
688 Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas = EF.relas(*RelaSec);
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 Expected<ArrayRef<uint8_t>> ContentsOrErr = EF.getSectionContents(Sec);
697 if (!ContentsOrErr)
698 return ContentsOrErr.takeError();
699 ArrayRef<uint8_t> Content = *ContentsOrErr;
700 DataExtractor Data(Content, EF.isLE(), ELFT::Is64Bits ? 8 : 4);
701 std::vector<BBAddrMap> FunctionEntries;
702
703 DataExtractor::Cursor Cur(0);
704 Error ULEBSizeErr = Error::success();
705 Error MetadataDecodeErr = Error::success();
706
707 uint8_t Version = 0;
708 uint8_t Feature = 0;
709 PGOAnalysisMap::Features FeatEnable{};
710 while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
711 Cur.tell() < Content.size()) {
712 if (Sec.sh_type == ELF::SHT_LLVM_BB_ADDR_MAP) {
713 Version = Data.getU8(Cur);
714 if (!Cur)
715 break;
716 if (Version > 2)
717 return createError("unsupported SHT_LLVM_BB_ADDR_MAP version: " +
718 Twine(static_cast<int>(Version)));
719 Feature = Data.getU8(Cur); // Feature byte
720 if (!Cur)
721 break;
722 auto FeatEnableOrErr = PGOAnalysisMap::Features::decode(Feature);
723 if (!FeatEnableOrErr)
724 return FeatEnableOrErr.takeError();
725 FeatEnable =
726 FeatEnableOrErr ? *FeatEnableOrErr : PGOAnalysisMap::Features{};
727 if (Feature != 0 && Version < 2 && Cur)
728 return createError(
729 "version should be >= 2 for SHT_LLVM_BB_ADDR_MAP when "
730 "PGO features are enabled: version = " +
731 Twine(static_cast<int>(Version)) +
732 " feature = " + Twine(static_cast<int>(Feature)));
733 }
734 uint64_t SectionOffset = Cur.tell();
735 auto Address =
736 static_cast<typename ELFFile<ELFT>::uintX_t>(Data.getAddress(Cur));
737 if (!Cur)
738 return Cur.takeError();
739 if (IsRelocatable) {
740 assert(Address == 0);
741 auto FOTIterator = FunctionOffsetTranslations.find(SectionOffset);
742 if (FOTIterator == FunctionOffsetTranslations.end()) {
743 return createError("failed to get relocation data for offset: " +
744 Twine::utohexstr(SectionOffset) + " in section " +
745 describe(EF, Sec));
746 }
747 Address = FOTIterator->second;
748 }
749 uint32_t NumBlocks = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
750
751 std::vector<BBAddrMap::BBEntry> BBEntries;
752 uint32_t PrevBBEndOffset = 0;
753 for (uint32_t BlockIndex = 0;
754 !MetadataDecodeErr && !ULEBSizeErr && Cur && (BlockIndex < NumBlocks);
755 ++BlockIndex) {
756 uint32_t ID = Version >= 2
757 ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
758 : BlockIndex;
759 uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
760 uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
761 uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
762 if (Version >= 1) {
763 // Offset is calculated relative to the end of the previous BB.
764 Offset += PrevBBEndOffset;
765 PrevBBEndOffset = Offset + Size;
766 }
767 Expected<BBAddrMap::BBEntry::Metadata> MetadataOrErr =
768 BBAddrMap::BBEntry::Metadata::decode(MD);
769 if (!MetadataOrErr) {
770 MetadataDecodeErr = MetadataOrErr.takeError();
771 break;
772 }
773 BBEntries.push_back({ID, Offset, Size, *MetadataOrErr});
774 }
775 FunctionEntries.emplace_back(Address, std::move(BBEntries));
776
777 if (PGOAnalyses || FeatEnable.anyEnabled()) {
778 // Function entry count
779 uint64_t FuncEntryCount =
780 FeatEnable.FuncEntryCount
781 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
782 : 0;
783
784 std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
785 for (uint32_t BlockIndex = 0;
786 (FeatEnable.BBFreq || FeatEnable.BrProb) && !MetadataDecodeErr &&
787 !ULEBSizeErr && Cur && (BlockIndex < NumBlocks);
788 ++BlockIndex) {
789 // Block frequency
790 uint64_t BBF = FeatEnable.BBFreq
791 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
792 : 0;
793
794 // Branch probability
795 llvm::SmallVector<PGOAnalysisMap::PGOBBEntry::SuccessorEntry, 2>
796 Successors;
797 if (FeatEnable.BrProb) {
798 auto SuccCount = readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr);
799 for (uint64_t I = 0; I < SuccCount; ++I) {
800 uint32_t BBID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
801 uint32_t BrProb = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
802 if (PGOAnalyses)
803 Successors.push_back({BBID, BranchProbability::getRaw(BrProb)});
804 }
805 }
806
807 if (PGOAnalyses)
808 PGOBBEntries.push_back({BlockFrequency(BBF), std::move(Successors)});
809 }
810
811 if (PGOAnalyses)
812 PGOAnalyses->push_back(
813 {FuncEntryCount, std::move(PGOBBEntries), FeatEnable});
814 }
815 }
816 // Either Cur is in the error state, or we have an error in ULEBSizeErr or
817 // MetadataDecodeErr (but not both), but we join all errors here to be safe.
818 if (!Cur || ULEBSizeErr || MetadataDecodeErr)
819 return joinErrors(joinErrors(Cur.takeError(), std::move(ULEBSizeErr)),
820 std::move(MetadataDecodeErr));
821 return FunctionEntries;
822 }
823
824 template <class ELFT>
825 Expected<std::vector<BBAddrMap>>
decodeBBAddrMap(const Elf_Shdr & Sec,const Elf_Shdr * RelaSec,std::vector<PGOAnalysisMap> * PGOAnalyses) const826 ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec,
827 std::vector<PGOAnalysisMap> *PGOAnalyses) const {
828 size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
829 auto AddrMapsOrErr = decodeBBAddrMapImpl(*this, Sec, RelaSec, PGOAnalyses);
830 // remove new analyses when an error occurs
831 if (!AddrMapsOrErr && PGOAnalyses)
832 PGOAnalyses->resize(OriginalPGOSize);
833 return std::move(AddrMapsOrErr);
834 }
835
836 template <class ELFT>
837 Expected<
838 MapVector<const typename ELFT::Shdr *, const typename ELFT::Shdr *>>
getSectionAndRelocations(std::function<Expected<bool> (const Elf_Shdr &)> IsMatch) const839 ELFFile<ELFT>::getSectionAndRelocations(
840 std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const {
841 MapVector<const Elf_Shdr *, const Elf_Shdr *> SecToRelocMap;
842 Error Errors = Error::success();
843 for (const Elf_Shdr &Sec : cantFail(this->sections())) {
844 Expected<bool> DoesSectionMatch = IsMatch(Sec);
845 if (!DoesSectionMatch) {
846 Errors = joinErrors(std::move(Errors), DoesSectionMatch.takeError());
847 continue;
848 }
849 if (*DoesSectionMatch) {
850 if (SecToRelocMap.insert(std::make_pair(&Sec, (const Elf_Shdr *)nullptr))
851 .second)
852 continue;
853 }
854
855 if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL)
856 continue;
857
858 Expected<const Elf_Shdr *> RelSecOrErr = this->getSection(Sec.sh_info);
859 if (!RelSecOrErr) {
860 Errors = joinErrors(std::move(Errors),
861 createError(describe(*this, Sec) +
862 ": failed to get a relocated section: " +
863 toString(RelSecOrErr.takeError())));
864 continue;
865 }
866 const Elf_Shdr *ContentsSec = *RelSecOrErr;
867 Expected<bool> DoesRelTargetMatch = IsMatch(*ContentsSec);
868 if (!DoesRelTargetMatch) {
869 Errors = joinErrors(std::move(Errors), DoesRelTargetMatch.takeError());
870 continue;
871 }
872 if (*DoesRelTargetMatch)
873 SecToRelocMap[ContentsSec] = &Sec;
874 }
875 if(Errors)
876 return std::move(Errors);
877 return SecToRelocMap;
878 }
879
880 template class llvm::object::ELFFile<ELF32LE>;
881 template class llvm::object::ELFFile<ELF32BE>;
882 template class llvm::object::ELFFile<ELF64LE>;
883 template class llvm::object::ELFFile<ELF64BE>;
884