LLVM 24.0.0git
RelocationResolver.cpp
Go to the documentation of this file.
1//===- RelocationResolver.cpp ------------------------------------*- C++ -*-===//
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// This file defines utilities to resolve relocations in object files.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/Twine.h"
23#include "llvm/Support/Error.h"
26#include <cassert>
27
28namespace llvm {
29namespace object {
30
31static int64_t getELFAddend(RelocationRef R) {
33 handleAllErrors(AddendOrErr.takeError(), [](const ErrorInfoBase &EI) {
34 report_fatal_error(Twine(EI.message()));
35 });
36 return *AddendOrErr;
37}
38
40 switch (Type) {
41 case ELF::R_X86_64_NONE:
42 case ELF::R_X86_64_64:
43 case ELF::R_X86_64_DTPOFF32:
44 case ELF::R_X86_64_DTPOFF64:
45 case ELF::R_X86_64_PC32:
46 case ELF::R_X86_64_PC64:
47 case ELF::R_X86_64_32:
48 case ELF::R_X86_64_32S:
49 return true;
50 default:
51 return false;
52 }
53}
54
56 uint64_t LocData, int64_t Addend) {
57 switch (Type) {
58 case ELF::R_X86_64_NONE:
59 return LocData;
60 case ELF::R_X86_64_64:
61 case ELF::R_X86_64_DTPOFF32:
62 case ELF::R_X86_64_DTPOFF64:
63 return S + Addend;
64 case ELF::R_X86_64_PC32:
65 case ELF::R_X86_64_PC64:
66 return S + Addend - Offset;
67 case ELF::R_X86_64_32:
68 case ELF::R_X86_64_32S:
69 return (S + Addend) & 0xFFFFFFFF;
70 default:
71 llvm_unreachable("Invalid relocation type");
72 }
73}
74
76 switch (Type) {
77 case ELF::R_AARCH64_ABS32:
78 case ELF::R_AARCH64_ABS64:
79 case ELF::R_AARCH64_PREL16:
80 case ELF::R_AARCH64_PREL32:
81 case ELF::R_AARCH64_PREL64:
82 case ELF::R_AARCH64_TLS_DTPREL64:
83 return true;
84 default:
85 return false;
86 }
87}
88
90 uint64_t /*LocData*/, int64_t Addend) {
91 switch (Type) {
92 case ELF::R_AARCH64_ABS32:
93 return (S + Addend) & 0xFFFFFFFF;
94 case ELF::R_AARCH64_ABS64:
95 case ELF::R_AARCH64_TLS_DTPREL64:
96 return S + Addend;
97 case ELF::R_AARCH64_PREL16:
98 return (S + Addend - Offset) & 0xFFFF;
99 case ELF::R_AARCH64_PREL32:
100 return (S + Addend - Offset) & 0xFFFFFFFF;
101 case ELF::R_AARCH64_PREL64:
102 return S + Addend - Offset;
103 default:
104 llvm_unreachable("Invalid relocation type");
105 }
106}
107
109 switch (Type) {
110 case ELF::R_BPF_64_ABS32:
111 case ELF::R_BPF_64_ABS64:
112 return true;
113 default:
114 return false;
115 }
116}
117
119 uint64_t LocData, int64_t /*Addend*/) {
120 switch (Type) {
121 case ELF::R_BPF_64_ABS32:
122 return (S + LocData) & 0xFFFFFFFF;
123 case ELF::R_BPF_64_ABS64:
124 return S + LocData;
125 default:
126 llvm_unreachable("Invalid relocation type");
127 }
128}
129
131 switch (Type) {
132 case ELF::R_MIPS_32:
133 case ELF::R_MIPS_64:
134 case ELF::R_MIPS_TLS_DTPREL64:
135 case ELF::R_MIPS_PC32:
136 return true;
137 default:
138 return false;
139 }
140}
141
143 uint64_t /*LocData*/, int64_t Addend) {
144 switch (Type) {
145 case ELF::R_MIPS_32:
146 return (S + Addend) & 0xFFFFFFFF;
147 case ELF::R_MIPS_64:
148 return S + Addend;
149 case ELF::R_MIPS_TLS_DTPREL64:
150 return S + Addend - 0x8000;
151 case ELF::R_MIPS_PC32:
152 return S + Addend - Offset;
153 default:
154 llvm_unreachable("Invalid relocation type");
155 }
156}
157
159 switch (Type) {
160 case ELF::R_MSP430_32:
161 case ELF::R_MSP430_16_BYTE:
162 return true;
163 default:
164 return false;
165 }
166}
167
169 uint64_t /*LocData*/, int64_t Addend) {
170 switch (Type) {
171 case ELF::R_MSP430_32:
172 return (S + Addend) & 0xFFFFFFFF;
173 case ELF::R_MSP430_16_BYTE:
174 return (S + Addend) & 0xFFFF;
175 default:
176 llvm_unreachable("Invalid relocation type");
177 }
178}
179
181 switch (Type) {
182 case ELF::R_PPC64_ADDR32:
183 case ELF::R_PPC64_ADDR64:
184 case ELF::R_PPC64_REL32:
185 case ELF::R_PPC64_REL64:
186 return true;
187 default:
188 return false;
189 }
190}
191
193 uint64_t /*LocData*/, int64_t Addend) {
194 switch (Type) {
195 case ELF::R_PPC64_ADDR32:
196 return (S + Addend) & 0xFFFFFFFF;
197 case ELF::R_PPC64_ADDR64:
198 return S + Addend;
199 case ELF::R_PPC64_REL32:
200 return (S + Addend - Offset) & 0xFFFFFFFF;
201 case ELF::R_PPC64_REL64:
202 return S + Addend - Offset;
203 default:
204 llvm_unreachable("Invalid relocation type");
205 }
206}
207
209 switch (Type) {
210 case ELF::R_390_32:
211 case ELF::R_390_64:
212 return true;
213 default:
214 return false;
215 }
216}
217
219 uint64_t /*LocData*/, int64_t Addend) {
220 switch (Type) {
221 case ELF::R_390_32:
222 return (S + Addend) & 0xFFFFFFFF;
223 case ELF::R_390_64:
224 return S + Addend;
225 default:
226 llvm_unreachable("Invalid relocation type");
227 }
228}
229
231 switch (Type) {
232 case ELF::R_SPARC_32:
233 case ELF::R_SPARC_64:
234 case ELF::R_SPARC_UA32:
235 case ELF::R_SPARC_UA64:
236 case ELF::R_SPARC_TLS_DTPOFF32:
237 case ELF::R_SPARC_TLS_DTPOFF64:
238 return true;
239 default:
240 return false;
241 }
242}
243
245 uint64_t /*LocData*/, int64_t Addend) {
246 switch (Type) {
247 case ELF::R_SPARC_32:
248 case ELF::R_SPARC_64:
249 case ELF::R_SPARC_UA32:
250 case ELF::R_SPARC_UA64:
251 case ELF::R_SPARC_TLS_DTPOFF32:
252 case ELF::R_SPARC_TLS_DTPOFF64:
253 return S + Addend;
254 default:
255 llvm_unreachable("Invalid relocation type");
256 }
257}
258
259/// Returns true if \c Obj is an AMDGPU code object based solely on the value
260/// of e_machine.
261///
262/// AMDGPU code objects with an e_machine of EF_AMDGPU_MACH_NONE do not
263/// identify their arch as either r600 or amdgcn, but we can still handle
264/// their relocations. When we identify an ELF object with an UnknownArch,
265/// we use isAMDGPU to check for this case.
266static bool isAMDGPU(const ObjectFile &Obj) {
267 if (const auto *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj))
268 return ELFObj->getEMachine() == ELF::EM_AMDGPU;
269 return false;
270}
271
273 switch (Type) {
274 case ELF::R_AMDGPU_ABS32:
275 case ELF::R_AMDGPU_ABS64:
276 return true;
277 default:
278 return false;
279 }
280}
281
283 uint64_t LocData, int64_t Addend) {
284 assert((LocData == 0 || Addend == 0) &&
285 "one of LocData and Addend must be 0");
286 switch (Type) {
287 case ELF::R_AMDGPU_ABS32:
288 case ELF::R_AMDGPU_ABS64:
289 return S + LocData + Addend;
290 default:
291 llvm_unreachable("Invalid relocation type");
292 }
293}
294
296 switch (Type) {
297 case ELF::R_386_NONE:
298 case ELF::R_386_32:
299 case ELF::R_386_PC32:
300 return true;
301 default:
302 return false;
303 }
304}
305
307 uint64_t LocData, int64_t /*Addend*/) {
308 switch (Type) {
309 case ELF::R_386_NONE:
310 return LocData;
311 case ELF::R_386_32:
312 return S + LocData;
313 case ELF::R_386_PC32:
314 return S - Offset + LocData;
315 default:
316 llvm_unreachable("Invalid relocation type");
317 }
318}
319
321 switch (Type) {
322 case ELF::R_PPC_ADDR32:
323 case ELF::R_PPC_REL32:
324 return true;
325 default:
326 return false;
327 }
328}
329
331 uint64_t /*LocData*/, int64_t Addend) {
332 switch (Type) {
333 case ELF::R_PPC_ADDR32:
334 return (S + Addend) & 0xFFFFFFFF;
335 case ELF::R_PPC_REL32:
336 return (S + Addend - Offset) & 0xFFFFFFFF;
337 }
338 llvm_unreachable("Invalid relocation type");
339}
340
342 switch (Type) {
343 case ELF::R_ARM_ABS32:
344 case ELF::R_ARM_REL32:
345 return true;
346 default:
347 return false;
348 }
349}
350
352 uint64_t LocData, int64_t Addend) {
353 // Support both RELA and REL relocations. The caller is responsible
354 // for supplying the correct values for LocData and Addend, i.e.
355 // Addend == 0 for REL and LocData == 0 for RELA.
356 assert((LocData == 0 || Addend == 0) &&
357 "one of LocData and Addend must be 0");
358 switch (Type) {
359 case ELF::R_ARM_ABS32:
360 return (S + LocData + Addend) & 0xFFFFFFFF;
361 case ELF::R_ARM_REL32:
362 return (S + LocData + Addend - Offset) & 0xFFFFFFFF;
363 }
364 llvm_unreachable("Invalid relocation type");
365}
366
368 switch (Type) {
369 case ELF::R_AVR_16:
370 case ELF::R_AVR_32:
371 return true;
372 default:
373 return false;
374 }
375}
376
378 uint64_t /*LocData*/, int64_t Addend) {
379 switch (Type) {
380 case ELF::R_AVR_16:
381 return (S + Addend) & 0xFFFF;
382 case ELF::R_AVR_32:
383 return (S + Addend) & 0xFFFFFFFF;
384 default:
385 llvm_unreachable("Invalid relocation type");
386 }
387}
388
390 return Type == ELF::R_LANAI_32;
391}
392
394 uint64_t /*LocData*/, int64_t Addend) {
395 if (Type == ELF::R_LANAI_32)
396 return (S + Addend) & 0xFFFFFFFF;
397 llvm_unreachable("Invalid relocation type");
398}
399
401 switch (Type) {
402 case ELF::R_MIPS_32:
403 case ELF::R_MIPS_TLS_DTPREL32:
404 return true;
405 default:
406 return false;
407 }
408}
409
411 uint64_t LocData, int64_t /*Addend*/) {
412 // FIXME: Take in account implicit addends to get correct results.
413 if (Type == ELF::R_MIPS_32)
414 return (S + LocData) & 0xFFFFFFFF;
415 if (Type == ELF::R_MIPS_TLS_DTPREL32)
416 return (S + LocData) & 0xFFFFFFFF;
417 llvm_unreachable("Invalid relocation type");
418}
419
421 switch (Type) {
422 case ELF::R_SPARC_32:
423 case ELF::R_SPARC_UA32:
424 case ELF::R_SPARC_TLS_DTPOFF32:
425 return true;
426 default:
427 return false;
428 }
429}
430
432 uint64_t LocData, int64_t Addend) {
433 if (Type == ELF::R_SPARC_32 || Type == ELF::R_SPARC_UA32 ||
434 Type == ELF::R_SPARC_TLS_DTPOFF32)
435 return S + Addend;
436 return LocData;
437}
438
440 return Type == ELF::R_HEX_32;
441}
442
444 uint64_t /*LocData*/, int64_t Addend) {
445 if (Type == ELF::R_HEX_32)
446 return S + Addend;
447 llvm_unreachable("Invalid relocation type");
448}
449
451 switch (Type) {
452 case ELF::R_RISCV_NONE:
453 case ELF::R_RISCV_32:
454 case ELF::R_RISCV_32_PCREL:
455 case ELF::R_RISCV_64:
456 case ELF::R_RISCV_SET6:
457 case ELF::R_RISCV_SET8:
458 case ELF::R_RISCV_SUB6:
459 case ELF::R_RISCV_ADD8:
460 case ELF::R_RISCV_SUB8:
461 case ELF::R_RISCV_SET16:
462 case ELF::R_RISCV_ADD16:
463 case ELF::R_RISCV_SUB16:
464 case ELF::R_RISCV_SET32:
465 case ELF::R_RISCV_ADD32:
466 case ELF::R_RISCV_SUB32:
467 case ELF::R_RISCV_ADD64:
468 case ELF::R_RISCV_SUB64:
469 // Because the unrelocated value generated by .uleb128 A-B (used by
470 // loclists/rnglists) is meaningful, DebugInfoDWARF does not inspect the
471 // relocations. We declare support for the two relocation types without an
472 // (unreachable) implementation.
473 case ELF::R_RISCV_SET_ULEB128:
474 case ELF::R_RISCV_SUB_ULEB128:
475 return true;
476 default:
477 return false;
478 }
479}
480
482 uint64_t LocData, int64_t Addend) {
483 int64_t RA = Addend;
484 uint64_t A = LocData;
485 switch (Type) {
486 case ELF::R_RISCV_NONE:
487 return LocData;
488 case ELF::R_RISCV_32:
489 return (S + RA) & 0xFFFFFFFF;
490 case ELF::R_RISCV_32_PCREL:
491 return (S + RA - Offset) & 0xFFFFFFFF;
492 case ELF::R_RISCV_64:
493 return S + RA;
494 case ELF::R_RISCV_SET6:
495 return (A & 0xC0) | ((S + RA) & 0x3F);
496 case ELF::R_RISCV_SUB6:
497 return (A & 0xC0) | (((A & 0x3F) - (S + RA)) & 0x3F);
498 case ELF::R_RISCV_SET8:
499 return (S + RA) & 0xFF;
500 case ELF::R_RISCV_ADD8:
501 return (A + (S + RA)) & 0xFF;
502 case ELF::R_RISCV_SUB8:
503 return (A - (S + RA)) & 0xFF;
504 case ELF::R_RISCV_SET16:
505 return (S + RA) & 0xFFFF;
506 case ELF::R_RISCV_ADD16:
507 return (A + (S + RA)) & 0xFFFF;
508 case ELF::R_RISCV_SUB16:
509 return (A - (S + RA)) & 0xFFFF;
510 case ELF::R_RISCV_SET32:
511 return (S + RA) & 0xFFFFFFFF;
512 case ELF::R_RISCV_ADD32:
513 return (A + (S + RA)) & 0xFFFFFFFF;
514 case ELF::R_RISCV_SUB32:
515 return (A - (S + RA)) & 0xFFFFFFFF;
516 case ELF::R_RISCV_ADD64:
517 return (A + (S + RA));
518 case ELF::R_RISCV_SUB64:
519 return (A - (S + RA));
520 default:
521 llvm_unreachable("Invalid relocation type");
522 }
523}
524
526 switch (Type) {
527 case ELF::R_CKCORE_NONE:
528 case ELF::R_CKCORE_ADDR32:
529 case ELF::R_CKCORE_PCREL32:
530 return true;
531 default:
532 return false;
533 }
534}
535
537 uint64_t LocData, int64_t Addend) {
538 switch (Type) {
539 case ELF::R_CKCORE_NONE:
540 return LocData;
541 case ELF::R_CKCORE_ADDR32:
542 return (S + Addend) & 0xFFFFFFFF;
543 case ELF::R_CKCORE_PCREL32:
544 return (S + Addend - Offset) & 0xFFFFFFFF;
545 default:
546 llvm_unreachable("Invalid relocation type");
547 }
548}
549
551 switch (Type) {
552 case ELF::R_LARCH_NONE:
553 case ELF::R_LARCH_32:
554 case ELF::R_LARCH_32_PCREL:
555 case ELF::R_LARCH_64:
556 case ELF::R_LARCH_ADD6:
557 case ELF::R_LARCH_SUB6:
558 case ELF::R_LARCH_ADD8:
559 case ELF::R_LARCH_SUB8:
560 case ELF::R_LARCH_ADD16:
561 case ELF::R_LARCH_SUB16:
562 case ELF::R_LARCH_ADD32:
563 case ELF::R_LARCH_SUB32:
564 case ELF::R_LARCH_ADD64:
565 case ELF::R_LARCH_SUB64:
566 return true;
567 default:
568 return false;
569 }
570}
571
573 uint64_t LocData, int64_t Addend) {
574 switch (Type) {
575 case ELF::R_LARCH_NONE:
576 return LocData;
577 case ELF::R_LARCH_32:
578 return (S + Addend) & 0xFFFFFFFF;
579 case ELF::R_LARCH_32_PCREL:
580 return (S + Addend - Offset) & 0xFFFFFFFF;
581 case ELF::R_LARCH_64:
582 return S + Addend;
583 case ELF::R_LARCH_ADD6:
584 return (LocData & 0xC0) | ((LocData + S + Addend) & 0x3F);
585 case ELF::R_LARCH_SUB6:
586 return (LocData & 0xC0) | ((LocData - (S + Addend)) & 0x3F);
587 case ELF::R_LARCH_ADD8:
588 return (LocData + (S + Addend)) & 0xFF;
589 case ELF::R_LARCH_SUB8:
590 return (LocData - (S + Addend)) & 0xFF;
591 case ELF::R_LARCH_ADD16:
592 return (LocData + (S + Addend)) & 0xFFFF;
593 case ELF::R_LARCH_SUB16:
594 return (LocData - (S + Addend)) & 0xFFFF;
595 case ELF::R_LARCH_ADD32:
596 return (LocData + (S + Addend)) & 0xFFFFFFFF;
597 case ELF::R_LARCH_SUB32:
598 return (LocData - (S + Addend)) & 0xFFFFFFFF;
599 case ELF::R_LARCH_ADD64:
600 return (LocData + (S + Addend));
601 case ELF::R_LARCH_SUB64:
602 return (LocData - (S + Addend));
603 default:
604 llvm_unreachable("Invalid relocation type");
605 }
606}
607
609 switch (Type) {
612 return true;
613 default:
614 return false;
615 }
616}
617
619 uint64_t LocData, int64_t /*Addend*/) {
620 switch (Type) {
623 return (S + LocData) & 0xFFFFFFFF;
624 default:
625 llvm_unreachable("Invalid relocation type");
626 }
627}
628
630 switch (Type) {
633 return true;
634 default:
635 return false;
636 }
637}
638
640 uint64_t LocData, int64_t /*Addend*/) {
641 switch (Type) {
643 return (S + LocData) & 0xFFFFFFFF;
645 return S + LocData;
646 default:
647 llvm_unreachable("Invalid relocation type");
648 }
649}
650
652 switch (Type) {
655 return true;
656 default:
657 return false;
658 }
659}
660
662 uint64_t LocData, int64_t /*Addend*/) {
663 switch (Type) {
666 return (S + LocData) & 0xFFFFFFFF;
667 default:
668 llvm_unreachable("Invalid relocation type");
669 }
670}
671
673 switch (Type) {
676 return true;
677 default:
678 return false;
679 }
680}
681
683 uint64_t LocData, int64_t /*Addend*/) {
684 switch (Type) {
686 return (S + LocData) & 0xFFFFFFFF;
688 return S + LocData;
689 default:
690 llvm_unreachable("Invalid relocation type");
691 }
692}
693
695 switch (Type) {
698 return true;
699 default:
700 return false;
701 }
702}
703
705 uint64_t LocData, int64_t /*Addend*/) {
706 switch (Type) {
709 return (S + LocData) & 0xFFFFFFFF;
710 default:
711 llvm_unreachable("Invalid relocation type");
712 }
713}
714
718
720 uint64_t LocData, int64_t /*Addend*/) {
722 return S;
723 llvm_unreachable("Invalid relocation type");
724}
725
727 switch (Type) {
728 case wasm::R_WASM_FUNCTION_INDEX_LEB:
729 case wasm::R_WASM_TABLE_INDEX_SLEB:
730 case wasm::R_WASM_TABLE_INDEX_I32:
731 case wasm::R_WASM_MEMORY_ADDR_LEB:
732 case wasm::R_WASM_MEMORY_ADDR_SLEB:
733 case wasm::R_WASM_MEMORY_ADDR_I32:
734 case wasm::R_WASM_TYPE_INDEX_LEB:
735 case wasm::R_WASM_GLOBAL_INDEX_LEB:
736 case wasm::R_WASM_FUNCTION_OFFSET_I32:
737 case wasm::R_WASM_SECTION_OFFSET_I32:
738 case wasm::R_WASM_TAG_INDEX_LEB:
739 case wasm::R_WASM_GLOBAL_INDEX_I32:
740 case wasm::R_WASM_TABLE_NUMBER_LEB:
741 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
742 return true;
743 default:
744 return false;
745 }
746}
747
749 switch (Type) {
750 case wasm::R_WASM_MEMORY_ADDR_LEB64:
751 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
752 case wasm::R_WASM_MEMORY_ADDR_I64:
753 case wasm::R_WASM_TABLE_INDEX_SLEB64:
754 case wasm::R_WASM_TABLE_INDEX_I64:
755 case wasm::R_WASM_FUNCTION_OFFSET_I64:
756 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I64:
757 return true;
758 default:
759 return supportsWasm32(Type);
760 }
761}
762
764 uint64_t LocData, int64_t /*Addend*/) {
765 switch (Type) {
766 case wasm::R_WASM_FUNCTION_INDEX_LEB:
767 case wasm::R_WASM_TABLE_INDEX_SLEB:
768 case wasm::R_WASM_TABLE_INDEX_I32:
769 case wasm::R_WASM_MEMORY_ADDR_LEB:
770 case wasm::R_WASM_MEMORY_ADDR_SLEB:
771 case wasm::R_WASM_MEMORY_ADDR_I32:
772 case wasm::R_WASM_TYPE_INDEX_LEB:
773 case wasm::R_WASM_GLOBAL_INDEX_LEB:
774 case wasm::R_WASM_FUNCTION_OFFSET_I32:
775 case wasm::R_WASM_SECTION_OFFSET_I32:
776 case wasm::R_WASM_TAG_INDEX_LEB:
777 case wasm::R_WASM_GLOBAL_INDEX_I32:
778 case wasm::R_WASM_TABLE_NUMBER_LEB:
779 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
780 // For wasm section, its offset at 0 -- ignoring Value
781 return LocData;
782 default:
783 llvm_unreachable("Invalid relocation type");
784 }
785}
786
788 uint64_t LocData, int64_t Addend) {
789 switch (Type) {
790 case wasm::R_WASM_MEMORY_ADDR_LEB64:
791 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
792 case wasm::R_WASM_MEMORY_ADDR_I64:
793 case wasm::R_WASM_TABLE_INDEX_SLEB64:
794 case wasm::R_WASM_TABLE_INDEX_I64:
795 case wasm::R_WASM_FUNCTION_OFFSET_I64:
796 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I64:
797 // For wasm section, its offset at 0 -- ignoring Value
798 return LocData;
799 default:
800 return resolveWasm32(Type, Offset, S, LocData, Addend);
801 }
802}
803
804std::pair<SupportsRelocation, RelocationResolver>
806 if (Obj.isCOFF()) {
807 switch (Obj.getArch()) {
808 case Triple::x86_64:
810 case Triple::x86:
812 case Triple::arm:
813 case Triple::thumb:
815 case Triple::aarch64:
817 case Triple::mipsel:
819 default:
820 return {nullptr, nullptr};
821 }
822 } else if (Obj.isELF()) {
823 if (Obj.getBytesInAddress() == 8) {
824 switch (Obj.getArch()) {
825 case Triple::x86_64:
827 case Triple::aarch64:
830 case Triple::bpfel:
831 case Triple::bpfeb:
832 return {supportsBPF, resolveBPF};
835 case Triple::mips64el:
836 case Triple::mips64:
838 case Triple::ppc64le:
839 case Triple::ppc64:
840 return {supportsPPC64, resolvePPC64};
841 case Triple::systemz:
843 case Triple::sparcv9:
845 case Triple::amdgpu:
847 case Triple::riscv64:
849 return {supportsRISCV, resolveRISCV};
850 default:
851 if (isAMDGPU(Obj))
853 return {nullptr, nullptr};
854 }
855 }
856
857 // 32-bit object file
858 assert(Obj.getBytesInAddress() == 4 &&
859 "Invalid word size in object file");
860
861 switch (Obj.getArch()) {
862 case Triple::x86:
863 return {supportsX86, resolveX86};
864 case Triple::ppcle:
865 case Triple::ppc:
866 return {supportsPPC32, resolvePPC32};
867 case Triple::arm:
868 case Triple::armeb:
869 return {supportsARM, resolveARM};
870 case Triple::avr:
871 return {supportsAVR, resolveAVR};
872 case Triple::lanai:
873 return {supportsLanai, resolveLanai};
876 case Triple::mipsel:
877 case Triple::mips:
879 case Triple::msp430:
881 case Triple::sparc:
883 case Triple::hexagon:
885 case Triple::r600:
887 case Triple::riscv32:
889 return {supportsRISCV, resolveRISCV};
890 case Triple::csky:
891 return {supportsCSKY, resolveCSKY};
892 default:
893 if (isAMDGPU(Obj))
895 return {nullptr, nullptr};
896 }
897 } else if (Obj.isMachO()) {
898 if (Obj.getArch() == Triple::x86_64)
900 return {nullptr, nullptr};
901 } else if (Obj.isWasm()) {
902 if (Obj.getArch() == Triple::wasm32)
904 if (Obj.getArch() == Triple::wasm64)
906 return {nullptr, nullptr};
907 }
908
909 llvm_unreachable("Invalid object file");
910}
911
913 uint64_t S, uint64_t LocData) {
914 if (const ObjectFile *Obj = R.getObject()) {
915 int64_t Addend = 0;
916 if (Obj->isELF()) {
917 auto GetRelSectionType = [&]() -> unsigned {
918 if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
919 return Elf32LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
920 if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
921 return Elf64LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
922 if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
923 return Elf32BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
924 auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj);
925 return Elf64BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
926 };
927
928 if (GetRelSectionType() == ELF::SHT_RELA ||
929 GetRelSectionType() == ELF::SHT_CREL) {
930 Addend = getELFAddend(R);
931 // LoongArch and RISCV relocations use both LocData and Addend.
932 if (Obj->getArch() != Triple::loongarch32 &&
933 Obj->getArch() != Triple::loongarch64 &&
934 Obj->getArch() != Triple::riscv32 &&
935 Obj->getArch() != Triple::riscv64 &&
936 Obj->getArch() != Triple::riscv32be &&
937 Obj->getArch() != Triple::riscv64be)
938 LocData = 0;
939 }
940 }
941
942 return Resolver(R.getType(), R.getOffset(), S, LocData, Addend);
943 }
944
945 // Sometimes the caller might want to use its own specific implementation of
946 // the resolver function. E.g. this is used by LLD when it resolves debug
947 // relocations and assumes that all of them have the same computation (S + A).
948 // The relocation R has no owner object in this case and we don't need to
949 // provide Type and Offset fields. It is also assumed the DataRefImpl.p
950 // contains the addend, provided by the caller.
951 return Resolver(/*Type=*/0, /*Offset=*/0, S, LocData,
952 R.getRawDataRefImpl().p);
953}
954
955} // namespace object
956} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
SI optimize exec mask operations pre RA
Base class for error info classes.
Definition Error.h:44
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
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition Record.h:2295
@ loongarch32
Definition Triple.h:65
@ loongarch64
Definition Triple.h:66
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
Expected< int64_t > getAddend() const
This class is the base class for all object file types.
Definition ObjectFile.h:231
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition ObjectFile.h:54
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ IMAGE_REL_MIPS_REFWORD
Definition COFF.h:424
@ IMAGE_REL_MIPS_SECREL
Definition COFF.h:431
@ IMAGE_REL_ARM64_SECREL
Definition COFF.h:409
@ IMAGE_REL_ARM64_ADDR64
Definition COFF.h:415
@ IMAGE_REL_AMD64_ADDR64
Definition COFF.h:362
@ IMAGE_REL_AMD64_SECREL
Definition COFF.h:372
@ IMAGE_REL_ARM_ADDR32
Definition COFF.h:382
@ IMAGE_REL_ARM_SECREL
Definition COFF.h:391
@ IMAGE_REL_I386_SECREL
Definition COFF.h:354
@ IMAGE_REL_I386_DIR32
Definition COFF.h:350
@ SHT_CREL
Definition ELF.h:1017
@ SHT_RELA
Definition ELF.h:999
@ X86_64_RELOC_UNSIGNED
Definition MachO.h:536
static uint64_t resolveCOFFARM(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t)
static bool supportsRISCV(uint64_t Type)
static bool supportsCOFFARM(uint64_t Type)
static uint64_t resolveCOFFARM64(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t)
static uint64_t resolveMips64(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t, int64_t Addend)
static bool supportsCSKY(uint64_t Type)
static bool supportsLanai(uint64_t Type)
static uint64_t resolvePPC64(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t, int64_t Addend)
static bool supportsMSP430(uint64_t Type)
static bool supportsLoongArch(uint64_t Type)
static uint64_t resolveCSKY(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend)
static int64_t getELFAddend(RelocationRef R)
static uint64_t resolveAArch64(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t, int64_t Addend)
static bool supportsWasm32(uint64_t Type)
LLVM_ABI uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, uint64_t S, uint64_t LocData)
static uint64_t resolveX86(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t)
static uint64_t resolveWasm64(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend)
static bool supportsSystemZ(uint64_t Type)
static uint64_t resolveARM(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend)
static bool supportsWasm64(uint64_t Type)
static bool supportsSparc64(uint64_t Type)
static bool supportsMips64(uint64_t Type)
uint64_t(*)(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend) RelocationResolver
static bool supportsMachOX86_64(uint64_t Type)
static uint64_t resolveLanai(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t, int64_t Addend)
static uint64_t resolveCOFFX86(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t)
static uint64_t resolveX86_64(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend)
static uint64_t resolveMSP430(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t, int64_t Addend)
static bool supportsAmdgpu(uint64_t Type)
static bool supportsMips32(uint64_t Type)
static bool isAMDGPU(const ObjectFile &Obj)
Returns true if Obj is an AMDGPU code object based solely on the value of e_machine.
static bool supportsBPF(uint64_t Type)
static uint64_t resolvePPC32(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t, int64_t Addend)
static uint64_t resolveLoongArch(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend)
static uint64_t resolveAmdgpu(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend)
static uint64_t resolveHexagon(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t, int64_t Addend)
LLVM_ABI std::pair< SupportsRelocation, RelocationResolver > getRelocationResolver(const ObjectFile &Obj)
static bool supportsPPC32(uint64_t Type)
static uint64_t resolveSparc64(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t, int64_t Addend)
static uint64_t resolveAVR(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t, int64_t Addend)
static bool supportsCOFFX86_64(uint64_t Type)
static bool supportsARM(uint64_t Type)
static uint64_t resolveBPF(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t)
static bool supportsAVR(uint64_t Type)
static uint64_t resolveWasm32(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t)
static bool supportsX86(uint64_t Type)
static uint64_t resolveCOFFX86_64(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t)
static uint64_t resolveCOFFMIPS(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t)
static uint64_t resolveRISCV(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend)
static bool supportsHexagon(uint64_t Type)
static uint64_t resolveMachOX86_64(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t)
static bool supportsAArch64(uint64_t Type)
static bool supportsX86_64(uint64_t Type)
static uint64_t resolveSparc32(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend)
static bool supportsPPC64(uint64_t Type)
static bool supportsCOFFMIPS(uint64_t Type)
static bool supportsCOFFARM64(uint64_t Type)
static bool supportsCOFFX86(uint64_t Type)
static uint64_t resolveMips32(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t)
static bool supportsSparc32(uint64_t Type)
static uint64_t resolveSystemZ(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t, int64_t Addend)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition Error.h:1013
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559