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/BinaryFormat/ELF.h"
11 #include "llvm/Support/LEB128.h"
12
13 using namespace llvm;
14 using namespace object;
15
16 #define STRINGIFY_ENUM_CASE(ns, name) \
17 case ns::name: \
18 return #name;
19
20 #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
21
getELFRelocationTypeName(uint32_t Machine,uint32_t Type)22 StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
23 uint32_t Type) {
24 switch (Machine) {
25 case ELF::EM_X86_64:
26 switch (Type) {
27 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
28 default:
29 break;
30 }
31 break;
32 case ELF::EM_386:
33 case ELF::EM_IAMCU:
34 switch (Type) {
35 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
36 default:
37 break;
38 }
39 break;
40 case ELF::EM_MIPS:
41 switch (Type) {
42 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
43 default:
44 break;
45 }
46 break;
47 case ELF::EM_AARCH64:
48 switch (Type) {
49 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
50 default:
51 break;
52 }
53 break;
54 case ELF::EM_ARM:
55 switch (Type) {
56 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
57 default:
58 break;
59 }
60 break;
61 case ELF::EM_ARC_COMPACT:
62 case ELF::EM_ARC_COMPACT2:
63 switch (Type) {
64 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
65 default:
66 break;
67 }
68 break;
69 case ELF::EM_AVR:
70 switch (Type) {
71 #include "llvm/BinaryFormat/ELFRelocs/AVR.def"
72 default:
73 break;
74 }
75 break;
76 case ELF::EM_HEXAGON:
77 switch (Type) {
78 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
79 default:
80 break;
81 }
82 break;
83 case ELF::EM_LANAI:
84 switch (Type) {
85 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
86 default:
87 break;
88 }
89 break;
90 case ELF::EM_PPC:
91 switch (Type) {
92 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
93 default:
94 break;
95 }
96 break;
97 case ELF::EM_PPC64:
98 switch (Type) {
99 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
100 default:
101 break;
102 }
103 break;
104 case ELF::EM_RISCV:
105 switch (Type) {
106 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
107 default:
108 break;
109 }
110 break;
111 case ELF::EM_S390:
112 switch (Type) {
113 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
114 default:
115 break;
116 }
117 break;
118 case ELF::EM_SPARC:
119 case ELF::EM_SPARC32PLUS:
120 case ELF::EM_SPARCV9:
121 switch (Type) {
122 #include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
123 default:
124 break;
125 }
126 break;
127 case ELF::EM_AMDGPU:
128 switch (Type) {
129 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
130 default:
131 break;
132 }
133 break;
134 case ELF::EM_BPF:
135 switch (Type) {
136 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
137 default:
138 break;
139 }
140 break;
141 case ELF::EM_MSP430:
142 switch (Type) {
143 #include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
144 default:
145 break;
146 }
147 break;
148 case ELF::EM_VE:
149 switch (Type) {
150 #include "llvm/BinaryFormat/ELFRelocs/VE.def"
151 default:
152 break;
153 }
154 break;
155 case ELF::EM_CSKY:
156 switch (Type) {
157 #include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
158 default:
159 break;
160 }
161 break;
162 default:
163 break;
164 }
165 return "Unknown";
166 }
167
168 #undef ELF_RELOC
169
getELFRelativeRelocationType(uint32_t Machine)170 uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
171 switch (Machine) {
172 case ELF::EM_X86_64:
173 return ELF::R_X86_64_RELATIVE;
174 case ELF::EM_386:
175 case ELF::EM_IAMCU:
176 return ELF::R_386_RELATIVE;
177 case ELF::EM_MIPS:
178 break;
179 case ELF::EM_AARCH64:
180 return ELF::R_AARCH64_RELATIVE;
181 case ELF::EM_ARM:
182 return ELF::R_ARM_RELATIVE;
183 case ELF::EM_ARC_COMPACT:
184 case ELF::EM_ARC_COMPACT2:
185 return ELF::R_ARC_RELATIVE;
186 case ELF::EM_AVR:
187 break;
188 case ELF::EM_HEXAGON:
189 return ELF::R_HEX_RELATIVE;
190 case ELF::EM_LANAI:
191 break;
192 case ELF::EM_PPC:
193 break;
194 case ELF::EM_PPC64:
195 return ELF::R_PPC64_RELATIVE;
196 case ELF::EM_RISCV:
197 return ELF::R_RISCV_RELATIVE;
198 case ELF::EM_S390:
199 return ELF::R_390_RELATIVE;
200 case ELF::EM_SPARC:
201 case ELF::EM_SPARC32PLUS:
202 case ELF::EM_SPARCV9:
203 return ELF::R_SPARC_RELATIVE;
204 case ELF::EM_CSKY:
205 return ELF::R_CKCORE_RELATIVE;
206 case ELF::EM_AMDGPU:
207 break;
208 case ELF::EM_BPF:
209 break;
210 default:
211 break;
212 }
213 return 0;
214 }
215
getELFSectionTypeName(uint32_t Machine,unsigned Type)216 StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
217 switch (Machine) {
218 case ELF::EM_ARM:
219 switch (Type) {
220 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
221 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
222 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
223 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
224 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
225 }
226 break;
227 case ELF::EM_HEXAGON:
228 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
229 break;
230 case ELF::EM_X86_64:
231 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
232 break;
233 case ELF::EM_MIPS:
234 case ELF::EM_MIPS_RS3_LE:
235 switch (Type) {
236 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
237 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
238 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
239 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
240 }
241 break;
242 case ELF::EM_RISCV:
243 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
244 break;
245 default:
246 break;
247 }
248
249 switch (Type) {
250 STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
251 STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
252 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
253 STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
254 STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
255 STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
256 STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
257 STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
258 STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
259 STRINGIFY_ENUM_CASE(ELF, SHT_REL);
260 STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
261 STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
262 STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
263 STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
264 STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
265 STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
266 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
267 STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
268 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
269 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
270 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
271 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
272 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
273 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
274 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
275 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
276 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
277 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
278 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
279 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP);
280 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
281 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
282 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
283 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
284 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
285 default:
286 return "Unknown";
287 }
288 }
289
290 template <class ELFT>
291 std::vector<typename ELFT::Rel>
decode_relrs(Elf_Relr_Range relrs) const292 ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
293 // This function decodes the contents of an SHT_RELR packed relocation
294 // section.
295 //
296 // Proposal for adding SHT_RELR sections to generic-abi is here:
297 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
298 //
299 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
300 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
301 //
302 // i.e. start with an address, followed by any number of bitmaps. The address
303 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
304 // relocations each, at subsequent offsets following the last address entry.
305 //
306 // The bitmap entries must have 1 in the least significant bit. The assumption
307 // here is that an address cannot have 1 in lsb. Odd addresses are not
308 // supported.
309 //
310 // Excluding the least significant bit in the bitmap, each non-zero bit in
311 // the bitmap represents a relocation to be applied to a corresponding machine
312 // word that follows the base address word. The second least significant bit
313 // represents the machine word immediately following the initial address, and
314 // each bit that follows represents the next word, in linear order. As such,
315 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
316 // 63 relocations in a 64-bit object.
317 //
318 // This encoding has a couple of interesting properties:
319 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
320 // even means address, odd means bitmap.
321 // 2. Just a simple list of addresses is a valid encoding.
322
323 Elf_Rel Rel;
324 Rel.r_info = 0;
325 Rel.setType(getRelativeRelocationType(), false);
326 std::vector<Elf_Rel> Relocs;
327
328 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
329 typedef typename ELFT::uint Word;
330
331 // Word size in number of bytes.
332 const size_t WordSize = sizeof(Word);
333
334 // Number of bits used for the relocation offsets bitmap.
335 // These many relative relocations can be encoded in a single entry.
336 const size_t NBits = 8*WordSize - 1;
337
338 Word Base = 0;
339 for (const Elf_Relr &R : relrs) {
340 Word Entry = R;
341 if ((Entry&1) == 0) {
342 // Even entry: encodes the offset for next relocation.
343 Rel.r_offset = Entry;
344 Relocs.push_back(Rel);
345 // Set base offset for subsequent bitmap entries.
346 Base = Entry + WordSize;
347 continue;
348 }
349
350 // Odd entry: encodes bitmap for relocations starting at base.
351 Word Offset = Base;
352 while (Entry != 0) {
353 Entry >>= 1;
354 if ((Entry&1) != 0) {
355 Rel.r_offset = Offset;
356 Relocs.push_back(Rel);
357 }
358 Offset += WordSize;
359 }
360
361 // Advance base offset by NBits words.
362 Base += NBits * WordSize;
363 }
364
365 return Relocs;
366 }
367
368 template <class ELFT>
369 Expected<std::vector<typename ELFT::Rela>>
android_relas(const Elf_Shdr & Sec) const370 ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
371 // This function reads relocations in Android's packed relocation format,
372 // which is based on SLEB128 and delta encoding.
373 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
374 if (!ContentsOrErr)
375 return ContentsOrErr.takeError();
376 const uint8_t *Cur = ContentsOrErr->begin();
377 const uint8_t *End = ContentsOrErr->end();
378 if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
379 Cur[2] != 'S' || Cur[3] != '2')
380 return createError("invalid packed relocation header");
381 Cur += 4;
382
383 const char *ErrStr = nullptr;
384 auto ReadSLEB = [&]() -> int64_t {
385 if (ErrStr)
386 return 0;
387 unsigned Len;
388 int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
389 Cur += Len;
390 return Result;
391 };
392
393 uint64_t NumRelocs = ReadSLEB();
394 uint64_t Offset = ReadSLEB();
395 uint64_t Addend = 0;
396
397 if (ErrStr)
398 return createError(ErrStr);
399
400 std::vector<Elf_Rela> Relocs;
401 Relocs.reserve(NumRelocs);
402 while (NumRelocs) {
403 uint64_t NumRelocsInGroup = ReadSLEB();
404 if (NumRelocsInGroup > NumRelocs)
405 return createError("relocation group unexpectedly large");
406 NumRelocs -= NumRelocsInGroup;
407
408 uint64_t GroupFlags = ReadSLEB();
409 bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
410 bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
411 bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
412 bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
413
414 uint64_t GroupOffsetDelta;
415 if (GroupedByOffsetDelta)
416 GroupOffsetDelta = ReadSLEB();
417
418 uint64_t GroupRInfo;
419 if (GroupedByInfo)
420 GroupRInfo = ReadSLEB();
421
422 if (GroupedByAddend && GroupHasAddend)
423 Addend += ReadSLEB();
424
425 if (!GroupHasAddend)
426 Addend = 0;
427
428 for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
429 Elf_Rela R;
430 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
431 R.r_offset = Offset;
432 R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
433 if (GroupHasAddend && !GroupedByAddend)
434 Addend += ReadSLEB();
435 R.r_addend = Addend;
436 Relocs.push_back(R);
437
438 if (ErrStr)
439 return createError(ErrStr);
440 }
441
442 if (ErrStr)
443 return createError(ErrStr);
444 }
445
446 return Relocs;
447 }
448
449 template <class ELFT>
getDynamicTagAsString(unsigned Arch,uint64_t Type) const450 std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
451 uint64_t Type) const {
452 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
453 case value: \
454 return #tag;
455
456 #define DYNAMIC_TAG(n, v)
457 switch (Arch) {
458 case ELF::EM_AARCH64:
459 switch (Type) {
460 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
461 #include "llvm/BinaryFormat/DynamicTags.def"
462 #undef AARCH64_DYNAMIC_TAG
463 }
464 break;
465
466 case ELF::EM_HEXAGON:
467 switch (Type) {
468 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
469 #include "llvm/BinaryFormat/DynamicTags.def"
470 #undef HEXAGON_DYNAMIC_TAG
471 }
472 break;
473
474 case ELF::EM_MIPS:
475 switch (Type) {
476 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
477 #include "llvm/BinaryFormat/DynamicTags.def"
478 #undef MIPS_DYNAMIC_TAG
479 }
480 break;
481
482 case ELF::EM_PPC64:
483 switch (Type) {
484 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
485 #include "llvm/BinaryFormat/DynamicTags.def"
486 #undef PPC64_DYNAMIC_TAG
487 }
488 break;
489 }
490 #undef DYNAMIC_TAG
491 switch (Type) {
492 // Now handle all dynamic tags except the architecture specific ones
493 #define AARCH64_DYNAMIC_TAG(name, value)
494 #define MIPS_DYNAMIC_TAG(name, value)
495 #define HEXAGON_DYNAMIC_TAG(name, value)
496 #define PPC64_DYNAMIC_TAG(name, value)
497 // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
498 #define DYNAMIC_TAG_MARKER(name, value)
499 #define DYNAMIC_TAG(name, value) case value: return #name;
500 #include "llvm/BinaryFormat/DynamicTags.def"
501 #undef DYNAMIC_TAG
502 #undef AARCH64_DYNAMIC_TAG
503 #undef MIPS_DYNAMIC_TAG
504 #undef HEXAGON_DYNAMIC_TAG
505 #undef PPC64_DYNAMIC_TAG
506 #undef DYNAMIC_TAG_MARKER
507 #undef DYNAMIC_STRINGIFY_ENUM
508 default:
509 return "<unknown:>0x" + utohexstr(Type, true);
510 }
511 }
512
513 template <class ELFT>
getDynamicTagAsString(uint64_t Type) const514 std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
515 return getDynamicTagAsString(getHeader().e_machine, Type);
516 }
517
518 template <class ELFT>
dynamicEntries() const519 Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
520 ArrayRef<Elf_Dyn> Dyn;
521
522 auto ProgramHeadersOrError = program_headers();
523 if (!ProgramHeadersOrError)
524 return ProgramHeadersOrError.takeError();
525
526 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
527 if (Phdr.p_type == ELF::PT_DYNAMIC) {
528 Dyn = makeArrayRef(
529 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
530 Phdr.p_filesz / sizeof(Elf_Dyn));
531 break;
532 }
533 }
534
535 // If we can't find the dynamic section in the program headers, we just fall
536 // back on the sections.
537 if (Dyn.empty()) {
538 auto SectionsOrError = sections();
539 if (!SectionsOrError)
540 return SectionsOrError.takeError();
541
542 for (const Elf_Shdr &Sec : *SectionsOrError) {
543 if (Sec.sh_type == ELF::SHT_DYNAMIC) {
544 Expected<ArrayRef<Elf_Dyn>> DynOrError =
545 getSectionContentsAsArray<Elf_Dyn>(Sec);
546 if (!DynOrError)
547 return DynOrError.takeError();
548 Dyn = *DynOrError;
549 break;
550 }
551 }
552
553 if (!Dyn.data())
554 return ArrayRef<Elf_Dyn>();
555 }
556
557 if (Dyn.empty())
558 // TODO: this error is untested.
559 return createError("invalid empty dynamic section");
560
561 if (Dyn.back().d_tag != ELF::DT_NULL)
562 // TODO: this error is untested.
563 return createError("dynamic sections must be DT_NULL terminated");
564
565 return Dyn;
566 }
567
568 template <class ELFT>
569 Expected<const uint8_t *>
toMappedAddr(uint64_t VAddr,WarningHandler WarnHandler) const570 ELFFile<ELFT>::toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler) const {
571 auto ProgramHeadersOrError = program_headers();
572 if (!ProgramHeadersOrError)
573 return ProgramHeadersOrError.takeError();
574
575 llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
576
577 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
578 if (Phdr.p_type == ELF::PT_LOAD)
579 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
580
581 auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A,
582 const Elf_Phdr_Impl<ELFT> *B) {
583 return A->p_vaddr < B->p_vaddr;
584 };
585 if (!llvm::is_sorted(LoadSegments, SortPred)) {
586 if (Error E =
587 WarnHandler("loadable segments are unsorted by virtual address"))
588 return std::move(E);
589 llvm::stable_sort(LoadSegments, SortPred);
590 }
591
592 const Elf_Phdr *const *I = llvm::upper_bound(
593 LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
594 return VAddr < Phdr->p_vaddr;
595 });
596
597 if (I == LoadSegments.begin())
598 return createError("virtual address is not in any segment: 0x" +
599 Twine::utohexstr(VAddr));
600 --I;
601 const Elf_Phdr &Phdr = **I;
602 uint64_t Delta = VAddr - Phdr.p_vaddr;
603 if (Delta >= Phdr.p_filesz)
604 return createError("virtual address is not in any segment: 0x" +
605 Twine::utohexstr(VAddr));
606
607 uint64_t Offset = Phdr.p_offset + Delta;
608 if (Offset >= getBufSize())
609 return createError("can't map virtual address 0x" +
610 Twine::utohexstr(VAddr) + " to the segment with index " +
611 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
612 ": the segment ends at 0x" +
613 Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
614 ", which is greater than the file size (0x" +
615 Twine::utohexstr(getBufSize()) + ")");
616
617 return base() + Offset;
618 }
619
620 template class llvm::object::ELFFile<ELF32LE>;
621 template class llvm::object::ELFFile<ELF32BE>;
622 template class llvm::object::ELFFile<ELF64LE>;
623 template class llvm::object::ELFFile<ELF64BE>;
624