1 //===- RelocationResolver.cpp ------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines utilities to resolve relocations in object files.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Object/RelocationResolver.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/BinaryFormat/COFF.h"
16 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/BinaryFormat/MachO.h"
18 #include "llvm/BinaryFormat/Wasm.h"
19 #include "llvm/Object/ELFObjectFile.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Object/SymbolicFile.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Error.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/TargetParser/Triple.h"
31 static int64_t getELFAddend(RelocationRef R
) {
32 Expected
<int64_t> AddendOrErr
= ELFRelocationRef(R
).getAddend();
33 handleAllErrors(AddendOrErr
.takeError(), [](const ErrorInfoBase
&EI
) {
34 report_fatal_error(Twine(EI
.message()));
39 static bool supportsX86_64(uint64_t 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
:
55 static uint64_t resolveX86_64(uint64_t Type
, uint64_t Offset
, uint64_t S
,
56 uint64_t LocData
, int64_t Addend
) {
58 case ELF::R_X86_64_NONE
:
60 case ELF::R_X86_64_64
:
61 case ELF::R_X86_64_DTPOFF32
:
62 case ELF::R_X86_64_DTPOFF64
:
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;
71 llvm_unreachable("Invalid relocation type");
75 static bool supportsAArch64(uint64_t 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
:
88 static uint64_t resolveAArch64(uint64_t Type
, uint64_t Offset
, uint64_t S
,
89 uint64_t /*LocData*/, int64_t Addend
) {
91 case ELF::R_AARCH64_ABS32
:
92 return (S
+ Addend
) & 0xFFFFFFFF;
93 case ELF::R_AARCH64_ABS64
:
95 case ELF::R_AARCH64_PREL16
:
96 return (S
+ Addend
- Offset
) & 0xFFFF;
97 case ELF::R_AARCH64_PREL32
:
98 return (S
+ Addend
- Offset
) & 0xFFFFFFFF;
99 case ELF::R_AARCH64_PREL64
:
100 return S
+ Addend
- Offset
;
102 llvm_unreachable("Invalid relocation type");
106 static bool supportsBPF(uint64_t Type
) {
108 case ELF::R_BPF_64_ABS32
:
109 case ELF::R_BPF_64_ABS64
:
116 static uint64_t resolveBPF(uint64_t Type
, uint64_t Offset
, uint64_t S
,
117 uint64_t LocData
, int64_t /*Addend*/) {
119 case ELF::R_BPF_64_ABS32
:
120 return (S
+ LocData
) & 0xFFFFFFFF;
121 case ELF::R_BPF_64_ABS64
:
124 llvm_unreachable("Invalid relocation type");
128 static bool supportsMips64(uint64_t Type
) {
132 case ELF::R_MIPS_TLS_DTPREL64
:
133 case ELF::R_MIPS_PC32
:
140 static uint64_t resolveMips64(uint64_t Type
, uint64_t Offset
, uint64_t S
,
141 uint64_t /*LocData*/, int64_t Addend
) {
144 return (S
+ Addend
) & 0xFFFFFFFF;
147 case ELF::R_MIPS_TLS_DTPREL64
:
148 return S
+ Addend
- 0x8000;
149 case ELF::R_MIPS_PC32
:
150 return S
+ Addend
- Offset
;
152 llvm_unreachable("Invalid relocation type");
156 static bool supportsMSP430(uint64_t Type
) {
158 case ELF::R_MSP430_32
:
159 case ELF::R_MSP430_16_BYTE
:
166 static uint64_t resolveMSP430(uint64_t Type
, uint64_t Offset
, uint64_t S
,
167 uint64_t /*LocData*/, int64_t Addend
) {
169 case ELF::R_MSP430_32
:
170 return (S
+ Addend
) & 0xFFFFFFFF;
171 case ELF::R_MSP430_16_BYTE
:
172 return (S
+ Addend
) & 0xFFFF;
174 llvm_unreachable("Invalid relocation type");
178 static bool supportsPPC64(uint64_t Type
) {
180 case ELF::R_PPC64_ADDR32
:
181 case ELF::R_PPC64_ADDR64
:
182 case ELF::R_PPC64_REL32
:
183 case ELF::R_PPC64_REL64
:
190 static uint64_t resolvePPC64(uint64_t Type
, uint64_t Offset
, uint64_t S
,
191 uint64_t /*LocData*/, int64_t Addend
) {
193 case ELF::R_PPC64_ADDR32
:
194 return (S
+ Addend
) & 0xFFFFFFFF;
195 case ELF::R_PPC64_ADDR64
:
197 case ELF::R_PPC64_REL32
:
198 return (S
+ Addend
- Offset
) & 0xFFFFFFFF;
199 case ELF::R_PPC64_REL64
:
200 return S
+ Addend
- Offset
;
202 llvm_unreachable("Invalid relocation type");
206 static bool supportsSystemZ(uint64_t Type
) {
216 static uint64_t resolveSystemZ(uint64_t Type
, uint64_t Offset
, uint64_t S
,
217 uint64_t /*LocData*/, int64_t Addend
) {
220 return (S
+ Addend
) & 0xFFFFFFFF;
224 llvm_unreachable("Invalid relocation type");
228 static bool supportsSparc64(uint64_t Type
) {
230 case ELF::R_SPARC_32
:
231 case ELF::R_SPARC_64
:
232 case ELF::R_SPARC_UA32
:
233 case ELF::R_SPARC_UA64
:
240 static uint64_t resolveSparc64(uint64_t Type
, uint64_t Offset
, uint64_t S
,
241 uint64_t /*LocData*/, int64_t Addend
) {
243 case ELF::R_SPARC_32
:
244 case ELF::R_SPARC_64
:
245 case ELF::R_SPARC_UA32
:
246 case ELF::R_SPARC_UA64
:
249 llvm_unreachable("Invalid relocation type");
253 /// Returns true if \c Obj is an AMDGPU code object based solely on the value
256 /// AMDGPU code objects with an e_machine of EF_AMDGPU_MACH_NONE do not
257 /// identify their arch as either r600 or amdgcn, but we can still handle
258 /// their relocations. When we identify an ELF object with an UnknownArch,
259 /// we use isAMDGPU to check for this case.
260 static bool isAMDGPU(const ObjectFile
&Obj
) {
261 if (const auto *ELFObj
= dyn_cast
<ELFObjectFileBase
>(&Obj
))
262 return ELFObj
->getEMachine() == ELF::EM_AMDGPU
;
266 static bool supportsAmdgpu(uint64_t Type
) {
268 case ELF::R_AMDGPU_ABS32
:
269 case ELF::R_AMDGPU_ABS64
:
276 static uint64_t resolveAmdgpu(uint64_t Type
, uint64_t Offset
, uint64_t S
,
277 uint64_t /*LocData*/, int64_t Addend
) {
279 case ELF::R_AMDGPU_ABS32
:
280 case ELF::R_AMDGPU_ABS64
:
283 llvm_unreachable("Invalid relocation type");
287 static bool supportsX86(uint64_t Type
) {
289 case ELF::R_386_NONE
:
291 case ELF::R_386_PC32
:
298 static uint64_t resolveX86(uint64_t Type
, uint64_t Offset
, uint64_t S
,
299 uint64_t LocData
, int64_t /*Addend*/) {
301 case ELF::R_386_NONE
:
305 case ELF::R_386_PC32
:
306 return S
- Offset
+ LocData
;
308 llvm_unreachable("Invalid relocation type");
312 static bool supportsPPC32(uint64_t Type
) {
314 case ELF::R_PPC_ADDR32
:
315 case ELF::R_PPC_REL32
:
322 static uint64_t resolvePPC32(uint64_t Type
, uint64_t Offset
, uint64_t S
,
323 uint64_t /*LocData*/, int64_t Addend
) {
325 case ELF::R_PPC_ADDR32
:
326 return (S
+ Addend
) & 0xFFFFFFFF;
327 case ELF::R_PPC_REL32
:
328 return (S
+ Addend
- Offset
) & 0xFFFFFFFF;
330 llvm_unreachable("Invalid relocation type");
333 static bool supportsARM(uint64_t Type
) {
335 case ELF::R_ARM_ABS32
:
336 case ELF::R_ARM_REL32
:
343 static uint64_t resolveARM(uint64_t Type
, uint64_t Offset
, uint64_t S
,
344 uint64_t LocData
, int64_t Addend
) {
345 // Support both RELA and REL relocations. The caller is responsible
346 // for supplying the correct values for LocData and Addend, i.e.
347 // Addend == 0 for REL and LocData == 0 for RELA.
348 assert((LocData
== 0 || Addend
== 0) &&
349 "one of LocData and Addend must be 0");
351 case ELF::R_ARM_ABS32
:
352 return (S
+ LocData
+ Addend
) & 0xFFFFFFFF;
353 case ELF::R_ARM_REL32
:
354 return (S
+ LocData
+ Addend
- Offset
) & 0xFFFFFFFF;
356 llvm_unreachable("Invalid relocation type");
359 static bool supportsAVR(uint64_t Type
) {
369 static uint64_t resolveAVR(uint64_t Type
, uint64_t Offset
, uint64_t S
,
370 uint64_t /*LocData*/, int64_t Addend
) {
373 return (S
+ Addend
) & 0xFFFF;
375 return (S
+ Addend
) & 0xFFFFFFFF;
377 llvm_unreachable("Invalid relocation type");
381 static bool supportsLanai(uint64_t Type
) {
382 return Type
== ELF::R_LANAI_32
;
385 static uint64_t resolveLanai(uint64_t Type
, uint64_t Offset
, uint64_t S
,
386 uint64_t /*LocData*/, int64_t Addend
) {
387 if (Type
== ELF::R_LANAI_32
)
388 return (S
+ Addend
) & 0xFFFFFFFF;
389 llvm_unreachable("Invalid relocation type");
392 static bool supportsMips32(uint64_t Type
) {
395 case ELF::R_MIPS_TLS_DTPREL32
:
402 static uint64_t resolveMips32(uint64_t Type
, uint64_t Offset
, uint64_t S
,
403 uint64_t LocData
, int64_t /*Addend*/) {
404 // FIXME: Take in account implicit addends to get correct results.
405 if (Type
== ELF::R_MIPS_32
)
406 return (S
+ LocData
) & 0xFFFFFFFF;
407 if (Type
== ELF::R_MIPS_TLS_DTPREL32
)
408 return (S
+ LocData
) & 0xFFFFFFFF;
409 llvm_unreachable("Invalid relocation type");
412 static bool supportsSparc32(uint64_t Type
) {
414 case ELF::R_SPARC_32
:
415 case ELF::R_SPARC_UA32
:
422 static uint64_t resolveSparc32(uint64_t Type
, uint64_t Offset
, uint64_t S
,
423 uint64_t LocData
, int64_t Addend
) {
424 if (Type
== ELF::R_SPARC_32
|| Type
== ELF::R_SPARC_UA32
)
429 static bool supportsHexagon(uint64_t Type
) {
430 return Type
== ELF::R_HEX_32
;
433 static uint64_t resolveHexagon(uint64_t Type
, uint64_t Offset
, uint64_t S
,
434 uint64_t /*LocData*/, int64_t Addend
) {
435 if (Type
== ELF::R_HEX_32
)
437 llvm_unreachable("Invalid relocation type");
440 static bool supportsRISCV(uint64_t Type
) {
442 case ELF::R_RISCV_NONE
:
443 case ELF::R_RISCV_32
:
444 case ELF::R_RISCV_32_PCREL
:
445 case ELF::R_RISCV_64
:
446 case ELF::R_RISCV_SET6
:
447 case ELF::R_RISCV_SET8
:
448 case ELF::R_RISCV_SUB6
:
449 case ELF::R_RISCV_ADD8
:
450 case ELF::R_RISCV_SUB8
:
451 case ELF::R_RISCV_SET16
:
452 case ELF::R_RISCV_ADD16
:
453 case ELF::R_RISCV_SUB16
:
454 case ELF::R_RISCV_SET32
:
455 case ELF::R_RISCV_ADD32
:
456 case ELF::R_RISCV_SUB32
:
457 case ELF::R_RISCV_ADD64
:
458 case ELF::R_RISCV_SUB64
:
459 // Because the unrelocated value generated by .uleb128 A-B (used by
460 // loclists/rnglists) is meaningful, DebugInfoDWARF does not inspect the
461 // relocations. We declare support for the two relocation types without an
462 // (unreachable) implementation.
463 case ELF::R_RISCV_SET_ULEB128
:
464 case ELF::R_RISCV_SUB_ULEB128
:
471 static uint64_t resolveRISCV(uint64_t Type
, uint64_t Offset
, uint64_t S
,
472 uint64_t LocData
, int64_t Addend
) {
474 uint64_t A
= LocData
;
476 case ELF::R_RISCV_NONE
:
478 case ELF::R_RISCV_32
:
479 return (S
+ RA
) & 0xFFFFFFFF;
480 case ELF::R_RISCV_32_PCREL
:
481 return (S
+ RA
- Offset
) & 0xFFFFFFFF;
482 case ELF::R_RISCV_64
:
484 case ELF::R_RISCV_SET6
:
485 return (A
& 0xC0) | ((S
+ RA
) & 0x3F);
486 case ELF::R_RISCV_SUB6
:
487 return (A
& 0xC0) | (((A
& 0x3F) - (S
+ RA
)) & 0x3F);
488 case ELF::R_RISCV_SET8
:
489 return (S
+ RA
) & 0xFF;
490 case ELF::R_RISCV_ADD8
:
491 return (A
+ (S
+ RA
)) & 0xFF;
492 case ELF::R_RISCV_SUB8
:
493 return (A
- (S
+ RA
)) & 0xFF;
494 case ELF::R_RISCV_SET16
:
495 return (S
+ RA
) & 0xFFFF;
496 case ELF::R_RISCV_ADD16
:
497 return (A
+ (S
+ RA
)) & 0xFFFF;
498 case ELF::R_RISCV_SUB16
:
499 return (A
- (S
+ RA
)) & 0xFFFF;
500 case ELF::R_RISCV_SET32
:
501 return (S
+ RA
) & 0xFFFFFFFF;
502 case ELF::R_RISCV_ADD32
:
503 return (A
+ (S
+ RA
)) & 0xFFFFFFFF;
504 case ELF::R_RISCV_SUB32
:
505 return (A
- (S
+ RA
)) & 0xFFFFFFFF;
506 case ELF::R_RISCV_ADD64
:
507 return (A
+ (S
+ RA
));
508 case ELF::R_RISCV_SUB64
:
509 return (A
- (S
+ RA
));
511 llvm_unreachable("Invalid relocation type");
515 static bool supportsCSKY(uint64_t Type
) {
517 case ELF::R_CKCORE_NONE
:
518 case ELF::R_CKCORE_ADDR32
:
519 case ELF::R_CKCORE_PCREL32
:
526 static uint64_t resolveCSKY(uint64_t Type
, uint64_t Offset
, uint64_t S
,
527 uint64_t LocData
, int64_t Addend
) {
529 case ELF::R_CKCORE_NONE
:
531 case ELF::R_CKCORE_ADDR32
:
532 return (S
+ Addend
) & 0xFFFFFFFF;
533 case ELF::R_CKCORE_PCREL32
:
534 return (S
+ Addend
- Offset
) & 0xFFFFFFFF;
536 llvm_unreachable("Invalid relocation type");
540 static bool supportsLoongArch(uint64_t Type
) {
542 case ELF::R_LARCH_NONE
:
543 case ELF::R_LARCH_32
:
544 case ELF::R_LARCH_32_PCREL
:
545 case ELF::R_LARCH_64
:
546 case ELF::R_LARCH_ADD6
:
547 case ELF::R_LARCH_SUB6
:
548 case ELF::R_LARCH_ADD8
:
549 case ELF::R_LARCH_SUB8
:
550 case ELF::R_LARCH_ADD16
:
551 case ELF::R_LARCH_SUB16
:
552 case ELF::R_LARCH_ADD32
:
553 case ELF::R_LARCH_SUB32
:
554 case ELF::R_LARCH_ADD64
:
555 case ELF::R_LARCH_SUB64
:
562 static uint64_t resolveLoongArch(uint64_t Type
, uint64_t Offset
, uint64_t S
,
563 uint64_t LocData
, int64_t Addend
) {
565 case ELF::R_LARCH_NONE
:
567 case ELF::R_LARCH_32
:
568 return (S
+ Addend
) & 0xFFFFFFFF;
569 case ELF::R_LARCH_32_PCREL
:
570 return (S
+ Addend
- Offset
) & 0xFFFFFFFF;
571 case ELF::R_LARCH_64
:
573 case ELF::R_LARCH_ADD6
:
574 return (LocData
& 0xC0) | ((LocData
+ S
+ Addend
) & 0x3F);
575 case ELF::R_LARCH_SUB6
:
576 return (LocData
& 0xC0) | ((LocData
- (S
+ Addend
)) & 0x3F);
577 case ELF::R_LARCH_ADD8
:
578 return (LocData
+ (S
+ Addend
)) & 0xFF;
579 case ELF::R_LARCH_SUB8
:
580 return (LocData
- (S
+ Addend
)) & 0xFF;
581 case ELF::R_LARCH_ADD16
:
582 return (LocData
+ (S
+ Addend
)) & 0xFFFF;
583 case ELF::R_LARCH_SUB16
:
584 return (LocData
- (S
+ Addend
)) & 0xFFFF;
585 case ELF::R_LARCH_ADD32
:
586 return (LocData
+ (S
+ Addend
)) & 0xFFFFFFFF;
587 case ELF::R_LARCH_SUB32
:
588 return (LocData
- (S
+ Addend
)) & 0xFFFFFFFF;
589 case ELF::R_LARCH_ADD64
:
590 return (LocData
+ (S
+ Addend
));
591 case ELF::R_LARCH_SUB64
:
592 return (LocData
- (S
+ Addend
));
594 llvm_unreachable("Invalid relocation type");
598 static bool supportsCOFFX86(uint64_t Type
) {
600 case COFF::IMAGE_REL_I386_SECREL
:
601 case COFF::IMAGE_REL_I386_DIR32
:
608 static uint64_t resolveCOFFX86(uint64_t Type
, uint64_t Offset
, uint64_t S
,
609 uint64_t LocData
, int64_t /*Addend*/) {
611 case COFF::IMAGE_REL_I386_SECREL
:
612 case COFF::IMAGE_REL_I386_DIR32
:
613 return (S
+ LocData
) & 0xFFFFFFFF;
615 llvm_unreachable("Invalid relocation type");
619 static bool supportsCOFFX86_64(uint64_t Type
) {
621 case COFF::IMAGE_REL_AMD64_SECREL
:
622 case COFF::IMAGE_REL_AMD64_ADDR64
:
629 static uint64_t resolveCOFFX86_64(uint64_t Type
, uint64_t Offset
, uint64_t S
,
630 uint64_t LocData
, int64_t /*Addend*/) {
632 case COFF::IMAGE_REL_AMD64_SECREL
:
633 return (S
+ LocData
) & 0xFFFFFFFF;
634 case COFF::IMAGE_REL_AMD64_ADDR64
:
637 llvm_unreachable("Invalid relocation type");
641 static bool supportsCOFFARM(uint64_t Type
) {
643 case COFF::IMAGE_REL_ARM_SECREL
:
644 case COFF::IMAGE_REL_ARM_ADDR32
:
651 static uint64_t resolveCOFFARM(uint64_t Type
, uint64_t Offset
, uint64_t S
,
652 uint64_t LocData
, int64_t /*Addend*/) {
654 case COFF::IMAGE_REL_ARM_SECREL
:
655 case COFF::IMAGE_REL_ARM_ADDR32
:
656 return (S
+ LocData
) & 0xFFFFFFFF;
658 llvm_unreachable("Invalid relocation type");
662 static bool supportsCOFFARM64(uint64_t Type
) {
664 case COFF::IMAGE_REL_ARM64_SECREL
:
665 case COFF::IMAGE_REL_ARM64_ADDR64
:
672 static uint64_t resolveCOFFARM64(uint64_t Type
, uint64_t Offset
, uint64_t S
,
673 uint64_t LocData
, int64_t /*Addend*/) {
675 case COFF::IMAGE_REL_ARM64_SECREL
:
676 return (S
+ LocData
) & 0xFFFFFFFF;
677 case COFF::IMAGE_REL_ARM64_ADDR64
:
680 llvm_unreachable("Invalid relocation type");
684 static bool supportsMachOX86_64(uint64_t Type
) {
685 return Type
== MachO::X86_64_RELOC_UNSIGNED
;
688 static uint64_t resolveMachOX86_64(uint64_t Type
, uint64_t Offset
, uint64_t S
,
689 uint64_t LocData
, int64_t /*Addend*/) {
690 if (Type
== MachO::X86_64_RELOC_UNSIGNED
)
692 llvm_unreachable("Invalid relocation type");
695 static bool supportsWasm32(uint64_t Type
) {
697 case wasm::R_WASM_FUNCTION_INDEX_LEB
:
698 case wasm::R_WASM_TABLE_INDEX_SLEB
:
699 case wasm::R_WASM_TABLE_INDEX_I32
:
700 case wasm::R_WASM_MEMORY_ADDR_LEB
:
701 case wasm::R_WASM_MEMORY_ADDR_SLEB
:
702 case wasm::R_WASM_MEMORY_ADDR_I32
:
703 case wasm::R_WASM_TYPE_INDEX_LEB
:
704 case wasm::R_WASM_GLOBAL_INDEX_LEB
:
705 case wasm::R_WASM_FUNCTION_OFFSET_I32
:
706 case wasm::R_WASM_SECTION_OFFSET_I32
:
707 case wasm::R_WASM_TAG_INDEX_LEB
:
708 case wasm::R_WASM_GLOBAL_INDEX_I32
:
709 case wasm::R_WASM_TABLE_NUMBER_LEB
:
710 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32
:
717 static bool supportsWasm64(uint64_t Type
) {
719 case wasm::R_WASM_MEMORY_ADDR_LEB64
:
720 case wasm::R_WASM_MEMORY_ADDR_SLEB64
:
721 case wasm::R_WASM_MEMORY_ADDR_I64
:
722 case wasm::R_WASM_TABLE_INDEX_SLEB64
:
723 case wasm::R_WASM_TABLE_INDEX_I64
:
724 case wasm::R_WASM_FUNCTION_OFFSET_I64
:
727 return supportsWasm32(Type
);
731 static uint64_t resolveWasm32(uint64_t Type
, uint64_t Offset
, uint64_t S
,
732 uint64_t LocData
, int64_t /*Addend*/) {
734 case wasm::R_WASM_FUNCTION_INDEX_LEB
:
735 case wasm::R_WASM_TABLE_INDEX_SLEB
:
736 case wasm::R_WASM_TABLE_INDEX_I32
:
737 case wasm::R_WASM_MEMORY_ADDR_LEB
:
738 case wasm::R_WASM_MEMORY_ADDR_SLEB
:
739 case wasm::R_WASM_MEMORY_ADDR_I32
:
740 case wasm::R_WASM_TYPE_INDEX_LEB
:
741 case wasm::R_WASM_GLOBAL_INDEX_LEB
:
742 case wasm::R_WASM_FUNCTION_OFFSET_I32
:
743 case wasm::R_WASM_SECTION_OFFSET_I32
:
744 case wasm::R_WASM_TAG_INDEX_LEB
:
745 case wasm::R_WASM_GLOBAL_INDEX_I32
:
746 case wasm::R_WASM_TABLE_NUMBER_LEB
:
747 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32
:
748 // For wasm section, its offset at 0 -- ignoring Value
751 llvm_unreachable("Invalid relocation type");
755 static uint64_t resolveWasm64(uint64_t Type
, uint64_t Offset
, uint64_t S
,
756 uint64_t LocData
, int64_t Addend
) {
758 case wasm::R_WASM_MEMORY_ADDR_LEB64
:
759 case wasm::R_WASM_MEMORY_ADDR_SLEB64
:
760 case wasm::R_WASM_MEMORY_ADDR_I64
:
761 case wasm::R_WASM_TABLE_INDEX_SLEB64
:
762 case wasm::R_WASM_TABLE_INDEX_I64
:
763 case wasm::R_WASM_FUNCTION_OFFSET_I64
:
764 // For wasm section, its offset at 0 -- ignoring Value
767 return resolveWasm32(Type
, Offset
, S
, LocData
, Addend
);
771 std::pair
<SupportsRelocation
, RelocationResolver
>
772 getRelocationResolver(const ObjectFile
&Obj
) {
774 switch (Obj
.getArch()) {
776 return {supportsCOFFX86_64
, resolveCOFFX86_64
};
778 return {supportsCOFFX86
, resolveCOFFX86
};
781 return {supportsCOFFARM
, resolveCOFFARM
};
782 case Triple::aarch64
:
783 return {supportsCOFFARM64
, resolveCOFFARM64
};
785 return {nullptr, nullptr};
787 } else if (Obj
.isELF()) {
788 if (Obj
.getBytesInAddress() == 8) {
789 switch (Obj
.getArch()) {
791 return {supportsX86_64
, resolveX86_64
};
792 case Triple::aarch64
:
793 case Triple::aarch64_be
:
794 return {supportsAArch64
, resolveAArch64
};
797 return {supportsBPF
, resolveBPF
};
798 case Triple::loongarch64
:
799 return {supportsLoongArch
, resolveLoongArch
};
800 case Triple::mips64el
:
802 return {supportsMips64
, resolveMips64
};
803 case Triple::ppc64le
:
805 return {supportsPPC64
, resolvePPC64
};
806 case Triple::systemz
:
807 return {supportsSystemZ
, resolveSystemZ
};
808 case Triple::sparcv9
:
809 return {supportsSparc64
, resolveSparc64
};
811 return {supportsAmdgpu
, resolveAmdgpu
};
812 case Triple::riscv64
:
813 return {supportsRISCV
, resolveRISCV
};
816 return {supportsAmdgpu
, resolveAmdgpu
};
817 return {nullptr, nullptr};
821 // 32-bit object file
822 assert(Obj
.getBytesInAddress() == 4 &&
823 "Invalid word size in object file");
825 switch (Obj
.getArch()) {
827 return {supportsX86
, resolveX86
};
830 return {supportsPPC32
, resolvePPC32
};
833 return {supportsARM
, resolveARM
};
835 return {supportsAVR
, resolveAVR
};
837 return {supportsLanai
, resolveLanai
};
838 case Triple::loongarch32
:
839 return {supportsLoongArch
, resolveLoongArch
};
842 return {supportsMips32
, resolveMips32
};
844 return {supportsMSP430
, resolveMSP430
};
846 return {supportsSparc32
, resolveSparc32
};
847 case Triple::hexagon
:
848 return {supportsHexagon
, resolveHexagon
};
850 return {supportsAmdgpu
, resolveAmdgpu
};
851 case Triple::riscv32
:
852 return {supportsRISCV
, resolveRISCV
};
854 return {supportsCSKY
, resolveCSKY
};
857 return {supportsAmdgpu
, resolveAmdgpu
};
858 return {nullptr, nullptr};
860 } else if (Obj
.isMachO()) {
861 if (Obj
.getArch() == Triple::x86_64
)
862 return {supportsMachOX86_64
, resolveMachOX86_64
};
863 return {nullptr, nullptr};
864 } else if (Obj
.isWasm()) {
865 if (Obj
.getArch() == Triple::wasm32
)
866 return {supportsWasm32
, resolveWasm32
};
867 if (Obj
.getArch() == Triple::wasm64
)
868 return {supportsWasm64
, resolveWasm64
};
869 return {nullptr, nullptr};
872 llvm_unreachable("Invalid object file");
875 uint64_t resolveRelocation(RelocationResolver Resolver
, const RelocationRef
&R
,
876 uint64_t S
, uint64_t LocData
) {
877 if (const ObjectFile
*Obj
= R
.getObject()) {
880 auto GetRelSectionType
= [&]() -> unsigned {
881 if (auto *Elf32LEObj
= dyn_cast
<ELF32LEObjectFile
>(Obj
))
882 return Elf32LEObj
->getRelSection(R
.getRawDataRefImpl())->sh_type
;
883 if (auto *Elf64LEObj
= dyn_cast
<ELF64LEObjectFile
>(Obj
))
884 return Elf64LEObj
->getRelSection(R
.getRawDataRefImpl())->sh_type
;
885 if (auto *Elf32BEObj
= dyn_cast
<ELF32BEObjectFile
>(Obj
))
886 return Elf32BEObj
->getRelSection(R
.getRawDataRefImpl())->sh_type
;
887 auto *Elf64BEObj
= cast
<ELF64BEObjectFile
>(Obj
);
888 return Elf64BEObj
->getRelSection(R
.getRawDataRefImpl())->sh_type
;
891 if (GetRelSectionType() == ELF::SHT_RELA
) {
892 Addend
= getELFAddend(R
);
893 // LoongArch and RISCV relocations use both LocData and Addend.
894 if (Obj
->getArch() != Triple::loongarch32
&&
895 Obj
->getArch() != Triple::loongarch64
&&
896 Obj
->getArch() != Triple::riscv32
&&
897 Obj
->getArch() != Triple::riscv64
)
902 return Resolver(R
.getType(), R
.getOffset(), S
, LocData
, Addend
);
905 // Sometimes the caller might want to use its own specific implementation of
906 // the resolver function. E.g. this is used by LLD when it resolves debug
907 // relocations and assumes that all of them have the same computation (S + A).
908 // The relocation R has no owner object in this case and we don't need to
909 // provide Type and Offset fields. It is also assumed the DataRefImpl.p
910 // contains the addend, provided by the caller.
911 return Resolver(/*Type=*/0, /*Offset=*/0, S
, LocData
,
912 R
.getRawDataRefImpl().p
);
915 } // namespace object