xref: /llvm-project/llvm/lib/Object/RelocationResolver.cpp (revision 48e894a536417b07fc68093e1e6fa52b768e3753)
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 
13 #include "llvm/Object/RelocationResolver.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/BinaryFormat/COFF.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/BinaryFormat/MachO.h"
19 #include "llvm/BinaryFormat/Wasm.h"
20 #include "llvm/Object/ELFObjectFile.h"
21 #include "llvm/Object/ELFTypes.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Object/SymbolicFile.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include <cassert>
28 #include <vector>
29 
30 namespace llvm {
31 namespace object {
32 
33 static int64_t getELFAddend(RelocationRef R) {
34   Expected<int64_t> AddendOrErr = ELFRelocationRef(R).getAddend();
35   handleAllErrors(AddendOrErr.takeError(), [](const ErrorInfoBase &EI) {
36     report_fatal_error(Twine(EI.message()));
37   });
38   return *AddendOrErr;
39 }
40 
41 static bool supportsX86_64(uint64_t Type) {
42   switch (Type) {
43   case ELF::R_X86_64_NONE:
44   case ELF::R_X86_64_64:
45   case ELF::R_X86_64_DTPOFF32:
46   case ELF::R_X86_64_DTPOFF64:
47   case ELF::R_X86_64_PC32:
48   case ELF::R_X86_64_PC64:
49   case ELF::R_X86_64_32:
50   case ELF::R_X86_64_32S:
51     return true;
52   default:
53     return false;
54   }
55 }
56 
57 static uint64_t resolveX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
58                               uint64_t LocData, int64_t Addend) {
59   switch (Type) {
60   case ELF::R_X86_64_NONE:
61     return LocData;
62   case ELF::R_X86_64_64:
63   case ELF::R_X86_64_DTPOFF32:
64   case ELF::R_X86_64_DTPOFF64:
65     return S + Addend;
66   case ELF::R_X86_64_PC32:
67   case ELF::R_X86_64_PC64:
68     return S + Addend - Offset;
69   case ELF::R_X86_64_32:
70   case ELF::R_X86_64_32S:
71     return (S + Addend) & 0xFFFFFFFF;
72   default:
73     llvm_unreachable("Invalid relocation type");
74   }
75 }
76 
77 static bool supportsAArch64(uint64_t Type) {
78   switch (Type) {
79   case ELF::R_AARCH64_ABS32:
80   case ELF::R_AARCH64_ABS64:
81   case ELF::R_AARCH64_PREL16:
82   case ELF::R_AARCH64_PREL32:
83   case ELF::R_AARCH64_PREL64:
84     return true;
85   default:
86     return false;
87   }
88 }
89 
90 static uint64_t resolveAArch64(uint64_t Type, uint64_t Offset, uint64_t S,
91                                uint64_t /*LocData*/, int64_t Addend) {
92   switch (Type) {
93   case ELF::R_AARCH64_ABS32:
94     return (S + Addend) & 0xFFFFFFFF;
95   case ELF::R_AARCH64_ABS64:
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 
108 static bool supportsBPF(uint64_t Type) {
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 
118 static uint64_t resolveBPF(uint64_t Type, uint64_t Offset, uint64_t S,
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 
130 static bool supportsMips64(uint64_t Type) {
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 
142 static uint64_t resolveMips64(uint64_t Type, uint64_t Offset, uint64_t S,
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 
158 static bool supportsMSP430(uint64_t Type) {
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 
168 static uint64_t resolveMSP430(uint64_t Type, uint64_t Offset, uint64_t S,
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 
180 static bool supportsPPC64(uint64_t Type) {
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 
192 static uint64_t resolvePPC64(uint64_t Type, uint64_t Offset, uint64_t S,
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 
208 static bool supportsSystemZ(uint64_t Type) {
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 
218 static uint64_t resolveSystemZ(uint64_t Type, uint64_t Offset, uint64_t S,
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 
230 static bool supportsSparc64(uint64_t Type) {
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     return true;
237   default:
238     return false;
239   }
240 }
241 
242 static uint64_t resolveSparc64(uint64_t Type, uint64_t Offset, uint64_t S,
243                                uint64_t /*LocData*/, int64_t Addend) {
244   switch (Type) {
245   case ELF::R_SPARC_32:
246   case ELF::R_SPARC_64:
247   case ELF::R_SPARC_UA32:
248   case ELF::R_SPARC_UA64:
249     return S + Addend;
250   default:
251     llvm_unreachable("Invalid relocation type");
252   }
253 }
254 
255 static bool supportsAmdgpu(uint64_t Type) {
256   switch (Type) {
257   case ELF::R_AMDGPU_ABS32:
258   case ELF::R_AMDGPU_ABS64:
259     return true;
260   default:
261     return false;
262   }
263 }
264 
265 static uint64_t resolveAmdgpu(uint64_t Type, uint64_t Offset, uint64_t S,
266                               uint64_t /*LocData*/, int64_t Addend) {
267   switch (Type) {
268   case ELF::R_AMDGPU_ABS32:
269   case ELF::R_AMDGPU_ABS64:
270     return S + Addend;
271   default:
272     llvm_unreachable("Invalid relocation type");
273   }
274 }
275 
276 static bool supportsX86(uint64_t Type) {
277   switch (Type) {
278   case ELF::R_386_NONE:
279   case ELF::R_386_32:
280   case ELF::R_386_PC32:
281     return true;
282   default:
283     return false;
284   }
285 }
286 
287 static uint64_t resolveX86(uint64_t Type, uint64_t Offset, uint64_t S,
288                            uint64_t LocData, int64_t /*Addend*/) {
289   switch (Type) {
290   case ELF::R_386_NONE:
291     return LocData;
292   case ELF::R_386_32:
293     return S + LocData;
294   case ELF::R_386_PC32:
295     return S - Offset + LocData;
296   default:
297     llvm_unreachable("Invalid relocation type");
298   }
299 }
300 
301 static bool supportsPPC32(uint64_t Type) {
302   switch (Type) {
303   case ELF::R_PPC_ADDR32:
304   case ELF::R_PPC_REL32:
305     return true;
306   default:
307     return false;
308   }
309 }
310 
311 static uint64_t resolvePPC32(uint64_t Type, uint64_t Offset, uint64_t S,
312                              uint64_t /*LocData*/, int64_t Addend) {
313   switch (Type) {
314   case ELF::R_PPC_ADDR32:
315     return (S + Addend) & 0xFFFFFFFF;
316   case ELF::R_PPC_REL32:
317     return (S + Addend - Offset) & 0xFFFFFFFF;
318   }
319   llvm_unreachable("Invalid relocation type");
320 }
321 
322 static bool supportsARM(uint64_t Type) {
323   switch (Type) {
324   case ELF::R_ARM_ABS32:
325   case ELF::R_ARM_REL32:
326     return true;
327   default:
328     return false;
329   }
330 }
331 
332 static uint64_t resolveARM(uint64_t Type, uint64_t Offset, uint64_t S,
333                            uint64_t LocData, int64_t Addend) {
334   // Support both RELA and REL relocations. The caller is responsible
335   // for supplying the correct values for LocData and Addend, i.e.
336   // Addend == 0 for REL and LocData == 0 for RELA.
337   assert((LocData == 0 || Addend == 0) &&
338          "one of LocData and Addend must be 0");
339   switch (Type) {
340   case ELF::R_ARM_ABS32:
341     return (S + LocData + Addend) & 0xFFFFFFFF;
342   case ELF::R_ARM_REL32:
343     return (S + LocData + Addend - Offset) & 0xFFFFFFFF;
344   }
345   llvm_unreachable("Invalid relocation type");
346 }
347 
348 static bool supportsAVR(uint64_t Type) {
349   switch (Type) {
350   case ELF::R_AVR_16:
351   case ELF::R_AVR_32:
352     return true;
353   default:
354     return false;
355   }
356 }
357 
358 static uint64_t resolveAVR(uint64_t Type, uint64_t Offset, uint64_t S,
359                            uint64_t /*LocData*/, int64_t Addend) {
360   switch (Type) {
361   case ELF::R_AVR_16:
362     return (S + Addend) & 0xFFFF;
363   case ELF::R_AVR_32:
364     return (S + Addend) & 0xFFFFFFFF;
365   default:
366     llvm_unreachable("Invalid relocation type");
367   }
368 }
369 
370 static bool supportsLanai(uint64_t Type) {
371   return Type == ELF::R_LANAI_32;
372 }
373 
374 static uint64_t resolveLanai(uint64_t Type, uint64_t Offset, uint64_t S,
375                              uint64_t /*LocData*/, int64_t Addend) {
376   if (Type == ELF::R_LANAI_32)
377     return (S + Addend) & 0xFFFFFFFF;
378   llvm_unreachable("Invalid relocation type");
379 }
380 
381 static bool supportsMips32(uint64_t Type) {
382   switch (Type) {
383   case ELF::R_MIPS_32:
384   case ELF::R_MIPS_TLS_DTPREL32:
385     return true;
386   default:
387     return false;
388   }
389 }
390 
391 static uint64_t resolveMips32(uint64_t Type, uint64_t Offset, uint64_t S,
392                               uint64_t LocData, int64_t /*Addend*/) {
393   // FIXME: Take in account implicit addends to get correct results.
394   if (Type == ELF::R_MIPS_32)
395     return (S + LocData) & 0xFFFFFFFF;
396   if (Type == ELF::R_MIPS_TLS_DTPREL32)
397     return (S + LocData) & 0xFFFFFFFF;
398   llvm_unreachable("Invalid relocation type");
399 }
400 
401 static bool supportsSparc32(uint64_t Type) {
402   switch (Type) {
403   case ELF::R_SPARC_32:
404   case ELF::R_SPARC_UA32:
405     return true;
406   default:
407     return false;
408   }
409 }
410 
411 static uint64_t resolveSparc32(uint64_t Type, uint64_t Offset, uint64_t S,
412                                uint64_t LocData, int64_t Addend) {
413   if (Type == ELF::R_SPARC_32 || Type == ELF::R_SPARC_UA32)
414     return S + Addend;
415   return LocData;
416 }
417 
418 static bool supportsHexagon(uint64_t Type) {
419   return Type == ELF::R_HEX_32;
420 }
421 
422 static uint64_t resolveHexagon(uint64_t Type, uint64_t Offset, uint64_t S,
423                                uint64_t /*LocData*/, int64_t Addend) {
424   if (Type == ELF::R_HEX_32)
425     return S + Addend;
426   llvm_unreachable("Invalid relocation type");
427 }
428 
429 static bool supportsRISCV(uint64_t Type) {
430   switch (Type) {
431   case ELF::R_RISCV_NONE:
432   case ELF::R_RISCV_32:
433   case ELF::R_RISCV_32_PCREL:
434   case ELF::R_RISCV_64:
435   case ELF::R_RISCV_SET6:
436   case ELF::R_RISCV_SUB6:
437   case ELF::R_RISCV_ADD8:
438   case ELF::R_RISCV_SUB8:
439   case ELF::R_RISCV_ADD16:
440   case ELF::R_RISCV_SUB16:
441   case ELF::R_RISCV_ADD32:
442   case ELF::R_RISCV_SUB32:
443   case ELF::R_RISCV_ADD64:
444   case ELF::R_RISCV_SUB64:
445     return true;
446   default:
447     return false;
448   }
449 }
450 
451 static uint64_t resolveRISCV(uint64_t Type, uint64_t Offset, uint64_t S,
452                              uint64_t LocData, int64_t Addend) {
453   int64_t RA = Addend;
454   uint64_t A = LocData;
455   switch (Type) {
456   case ELF::R_RISCV_NONE:
457     return LocData;
458   case ELF::R_RISCV_32:
459     return (S + RA) & 0xFFFFFFFF;
460   case ELF::R_RISCV_32_PCREL:
461     return (S + RA - Offset) & 0xFFFFFFFF;
462   case ELF::R_RISCV_64:
463     return S + RA;
464   case ELF::R_RISCV_SET6:
465     return (A & 0xC0) | ((S + RA) & 0x3F);
466   case ELF::R_RISCV_SUB6:
467     return (A & 0xC0) | (((A & 0x3F) - (S + RA)) & 0x3F);
468   case ELF::R_RISCV_ADD8:
469     return (A + (S + RA)) & 0xFF;
470   case ELF::R_RISCV_SUB8:
471     return (A - (S + RA)) & 0xFF;
472   case ELF::R_RISCV_ADD16:
473     return (A + (S + RA)) & 0xFFFF;
474   case ELF::R_RISCV_SUB16:
475     return (A - (S + RA)) & 0xFFFF;
476   case ELF::R_RISCV_ADD32:
477     return (A + (S + RA)) & 0xFFFFFFFF;
478   case ELF::R_RISCV_SUB32:
479     return (A - (S + RA)) & 0xFFFFFFFF;
480   case ELF::R_RISCV_ADD64:
481     return (A + (S + RA));
482   case ELF::R_RISCV_SUB64:
483     return (A - (S + RA));
484   default:
485     llvm_unreachable("Invalid relocation type");
486   }
487 }
488 
489 static bool supportsCOFFX86(uint64_t Type) {
490   switch (Type) {
491   case COFF::IMAGE_REL_I386_SECREL:
492   case COFF::IMAGE_REL_I386_DIR32:
493     return true;
494   default:
495     return false;
496   }
497 }
498 
499 static uint64_t resolveCOFFX86(uint64_t Type, uint64_t Offset, uint64_t S,
500                                uint64_t LocData, int64_t /*Addend*/) {
501   switch (Type) {
502   case COFF::IMAGE_REL_I386_SECREL:
503   case COFF::IMAGE_REL_I386_DIR32:
504     return (S + LocData) & 0xFFFFFFFF;
505   default:
506     llvm_unreachable("Invalid relocation type");
507   }
508 }
509 
510 static bool supportsCOFFX86_64(uint64_t Type) {
511   switch (Type) {
512   case COFF::IMAGE_REL_AMD64_SECREL:
513   case COFF::IMAGE_REL_AMD64_ADDR64:
514     return true;
515   default:
516     return false;
517   }
518 }
519 
520 static uint64_t resolveCOFFX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
521                                   uint64_t LocData, int64_t /*Addend*/) {
522   switch (Type) {
523   case COFF::IMAGE_REL_AMD64_SECREL:
524     return (S + LocData) & 0xFFFFFFFF;
525   case COFF::IMAGE_REL_AMD64_ADDR64:
526     return S + LocData;
527   default:
528     llvm_unreachable("Invalid relocation type");
529   }
530 }
531 
532 static bool supportsCOFFARM(uint64_t Type) {
533   switch (Type) {
534   case COFF::IMAGE_REL_ARM_SECREL:
535   case COFF::IMAGE_REL_ARM_ADDR32:
536     return true;
537   default:
538     return false;
539   }
540 }
541 
542 static uint64_t resolveCOFFARM(uint64_t Type, uint64_t Offset, uint64_t S,
543                                uint64_t LocData, int64_t /*Addend*/) {
544   switch (Type) {
545   case COFF::IMAGE_REL_ARM_SECREL:
546   case COFF::IMAGE_REL_ARM_ADDR32:
547     return (S + LocData) & 0xFFFFFFFF;
548   default:
549     llvm_unreachable("Invalid relocation type");
550   }
551 }
552 
553 static bool supportsCOFFARM64(uint64_t Type) {
554   switch (Type) {
555   case COFF::IMAGE_REL_ARM64_SECREL:
556   case COFF::IMAGE_REL_ARM64_ADDR64:
557     return true;
558   default:
559     return false;
560   }
561 }
562 
563 static uint64_t resolveCOFFARM64(uint64_t Type, uint64_t Offset, uint64_t S,
564                                  uint64_t LocData, int64_t /*Addend*/) {
565   switch (Type) {
566   case COFF::IMAGE_REL_ARM64_SECREL:
567     return (S + LocData) & 0xFFFFFFFF;
568   case COFF::IMAGE_REL_ARM64_ADDR64:
569     return S + LocData;
570   default:
571     llvm_unreachable("Invalid relocation type");
572   }
573 }
574 
575 static bool supportsMachOX86_64(uint64_t Type) {
576   return Type == MachO::X86_64_RELOC_UNSIGNED;
577 }
578 
579 static uint64_t resolveMachOX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
580                                    uint64_t LocData, int64_t /*Addend*/) {
581   if (Type == MachO::X86_64_RELOC_UNSIGNED)
582     return S;
583   llvm_unreachable("Invalid relocation type");
584 }
585 
586 static bool supportsWasm32(uint64_t Type) {
587   switch (Type) {
588   case wasm::R_WASM_FUNCTION_INDEX_LEB:
589   case wasm::R_WASM_TABLE_INDEX_SLEB:
590   case wasm::R_WASM_TABLE_INDEX_I32:
591   case wasm::R_WASM_MEMORY_ADDR_LEB:
592   case wasm::R_WASM_MEMORY_ADDR_SLEB:
593   case wasm::R_WASM_MEMORY_ADDR_I32:
594   case wasm::R_WASM_TYPE_INDEX_LEB:
595   case wasm::R_WASM_GLOBAL_INDEX_LEB:
596   case wasm::R_WASM_FUNCTION_OFFSET_I32:
597   case wasm::R_WASM_SECTION_OFFSET_I32:
598   case wasm::R_WASM_TAG_INDEX_LEB:
599   case wasm::R_WASM_GLOBAL_INDEX_I32:
600   case wasm::R_WASM_TABLE_NUMBER_LEB:
601   case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
602     return true;
603   default:
604     return false;
605   }
606 }
607 
608 static bool supportsWasm64(uint64_t Type) {
609   switch (Type) {
610   case wasm::R_WASM_MEMORY_ADDR_LEB64:
611   case wasm::R_WASM_MEMORY_ADDR_SLEB64:
612   case wasm::R_WASM_MEMORY_ADDR_I64:
613   case wasm::R_WASM_TABLE_INDEX_SLEB64:
614   case wasm::R_WASM_TABLE_INDEX_I64:
615   case wasm::R_WASM_FUNCTION_OFFSET_I64:
616     return true;
617   default:
618     return supportsWasm32(Type);
619   }
620 }
621 
622 static uint64_t resolveWasm32(uint64_t Type, uint64_t Offset, uint64_t S,
623                               uint64_t LocData, int64_t /*Addend*/) {
624   switch (Type) {
625   case wasm::R_WASM_FUNCTION_INDEX_LEB:
626   case wasm::R_WASM_TABLE_INDEX_SLEB:
627   case wasm::R_WASM_TABLE_INDEX_I32:
628   case wasm::R_WASM_MEMORY_ADDR_LEB:
629   case wasm::R_WASM_MEMORY_ADDR_SLEB:
630   case wasm::R_WASM_MEMORY_ADDR_I32:
631   case wasm::R_WASM_TYPE_INDEX_LEB:
632   case wasm::R_WASM_GLOBAL_INDEX_LEB:
633   case wasm::R_WASM_FUNCTION_OFFSET_I32:
634   case wasm::R_WASM_SECTION_OFFSET_I32:
635   case wasm::R_WASM_TAG_INDEX_LEB:
636   case wasm::R_WASM_GLOBAL_INDEX_I32:
637   case wasm::R_WASM_TABLE_NUMBER_LEB:
638   case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
639     // For wasm section, its offset at 0 -- ignoring Value
640     return LocData;
641   default:
642     llvm_unreachable("Invalid relocation type");
643   }
644 }
645 
646 static uint64_t resolveWasm64(uint64_t Type, uint64_t Offset, uint64_t S,
647                               uint64_t LocData, int64_t Addend) {
648   switch (Type) {
649   case wasm::R_WASM_MEMORY_ADDR_LEB64:
650   case wasm::R_WASM_MEMORY_ADDR_SLEB64:
651   case wasm::R_WASM_MEMORY_ADDR_I64:
652   case wasm::R_WASM_TABLE_INDEX_SLEB64:
653   case wasm::R_WASM_TABLE_INDEX_I64:
654   case wasm::R_WASM_FUNCTION_OFFSET_I64:
655     // For wasm section, its offset at 0 -- ignoring Value
656     return LocData;
657   default:
658     return resolveWasm32(Type, Offset, S, LocData, Addend);
659   }
660 }
661 
662 std::pair<SupportsRelocation, RelocationResolver>
663 getRelocationResolver(const ObjectFile &Obj) {
664   if (Obj.isCOFF()) {
665     switch (Obj.getArch()) {
666     case Triple::x86_64:
667       return {supportsCOFFX86_64, resolveCOFFX86_64};
668     case Triple::x86:
669       return {supportsCOFFX86, resolveCOFFX86};
670     case Triple::arm:
671     case Triple::thumb:
672       return {supportsCOFFARM, resolveCOFFARM};
673     case Triple::aarch64:
674       return {supportsCOFFARM64, resolveCOFFARM64};
675     default:
676       return {nullptr, nullptr};
677     }
678   } else if (Obj.isELF()) {
679     if (Obj.getBytesInAddress() == 8) {
680       switch (Obj.getArch()) {
681       case Triple::x86_64:
682         return {supportsX86_64, resolveX86_64};
683       case Triple::aarch64:
684       case Triple::aarch64_be:
685         return {supportsAArch64, resolveAArch64};
686       case Triple::bpfel:
687       case Triple::bpfeb:
688         return {supportsBPF, resolveBPF};
689       case Triple::mips64el:
690       case Triple::mips64:
691         return {supportsMips64, resolveMips64};
692       case Triple::ppc64le:
693       case Triple::ppc64:
694         return {supportsPPC64, resolvePPC64};
695       case Triple::systemz:
696         return {supportsSystemZ, resolveSystemZ};
697       case Triple::sparcv9:
698         return {supportsSparc64, resolveSparc64};
699       case Triple::amdgcn:
700         return {supportsAmdgpu, resolveAmdgpu};
701       case Triple::riscv64:
702         return {supportsRISCV, resolveRISCV};
703       default:
704         return {nullptr, nullptr};
705       }
706     }
707 
708     // 32-bit object file
709     assert(Obj.getBytesInAddress() == 4 &&
710            "Invalid word size in object file");
711 
712     switch (Obj.getArch()) {
713     case Triple::x86:
714       return {supportsX86, resolveX86};
715     case Triple::ppcle:
716     case Triple::ppc:
717       return {supportsPPC32, resolvePPC32};
718     case Triple::arm:
719     case Triple::armeb:
720       return {supportsARM, resolveARM};
721     case Triple::avr:
722       return {supportsAVR, resolveAVR};
723     case Triple::lanai:
724       return {supportsLanai, resolveLanai};
725     case Triple::mipsel:
726     case Triple::mips:
727       return {supportsMips32, resolveMips32};
728     case Triple::msp430:
729       return {supportsMSP430, resolveMSP430};
730     case Triple::sparc:
731       return {supportsSparc32, resolveSparc32};
732     case Triple::hexagon:
733       return {supportsHexagon, resolveHexagon};
734     case Triple::riscv32:
735       return {supportsRISCV, resolveRISCV};
736     default:
737       return {nullptr, nullptr};
738     }
739   } else if (Obj.isMachO()) {
740     if (Obj.getArch() == Triple::x86_64)
741       return {supportsMachOX86_64, resolveMachOX86_64};
742     return {nullptr, nullptr};
743   } else if (Obj.isWasm()) {
744     if (Obj.getArch() == Triple::wasm32)
745       return {supportsWasm32, resolveWasm32};
746     if (Obj.getArch() == Triple::wasm64)
747       return {supportsWasm64, resolveWasm64};
748     return {nullptr, nullptr};
749   }
750 
751   llvm_unreachable("Invalid object file");
752 }
753 
754 uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R,
755                            uint64_t S, uint64_t LocData) {
756   if (const ObjectFile *Obj = R.getObject()) {
757     int64_t Addend = 0;
758     if (Obj->isELF()) {
759       auto GetRelSectionType = [&]() -> unsigned {
760         if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
761           return Elf32LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
762         if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
763           return Elf64LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
764         if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
765           return Elf32BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
766         auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj);
767         return Elf64BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
768       };
769 
770       if (GetRelSectionType() == ELF::SHT_RELA) {
771         Addend = getELFAddend(R);
772         // RISCV relocations use both LocData and Addend.
773         if (Obj->getArch() != Triple::riscv32 &&
774             Obj->getArch() != Triple::riscv64)
775           LocData = 0;
776       }
777     }
778 
779     return Resolver(R.getType(), R.getOffset(), S, LocData, Addend);
780   }
781 
782   // Sometimes the caller might want to use its own specific implementation of
783   // the resolver function. E.g. this is used by LLD when it resolves debug
784   // relocations and assumes that all of them have the same computation (S + A).
785   // The relocation R has no owner object in this case and we don't need to
786   // provide Type and Offset fields. It is also assumed the DataRefImpl.p
787   // contains the addend, provided by the caller.
788   return Resolver(/*Type=*/0, /*Offset=*/0, S, LocData,
789                   R.getRawDataRefImpl().p);
790 }
791 
792 } // namespace object
793 } // namespace llvm
794