xref: /llvm-project/llvm/lib/Object/RelocationResolver.cpp (revision d22ea5be51c41d9d03c3226fe5e3445de0c5b6ac)
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_SET8:
437   case ELF::R_RISCV_SUB6:
438   case ELF::R_RISCV_ADD8:
439   case ELF::R_RISCV_SUB8:
440   case ELF::R_RISCV_ADD16:
441   case ELF::R_RISCV_SUB16:
442   case ELF::R_RISCV_ADD32:
443   case ELF::R_RISCV_SUB32:
444   case ELF::R_RISCV_ADD64:
445   case ELF::R_RISCV_SUB64:
446     return true;
447   default:
448     return false;
449   }
450 }
451 
452 static uint64_t resolveRISCV(uint64_t Type, uint64_t Offset, uint64_t S,
453                              uint64_t LocData, int64_t Addend) {
454   int64_t RA = Addend;
455   uint64_t A = LocData;
456   switch (Type) {
457   case ELF::R_RISCV_NONE:
458     return LocData;
459   case ELF::R_RISCV_32:
460     return (S + RA) & 0xFFFFFFFF;
461   case ELF::R_RISCV_32_PCREL:
462     return (S + RA - Offset) & 0xFFFFFFFF;
463   case ELF::R_RISCV_64:
464     return S + RA;
465   case ELF::R_RISCV_SET6:
466     return (A & 0xC0) | ((S + RA) & 0x3F);
467   case ELF::R_RISCV_SUB6:
468     return (A & 0xC0) | (((A & 0x3F) - (S + RA)) & 0x3F);
469   case ELF::R_RISCV_SET8:
470     return (S + RA) & 0xFF;
471   case ELF::R_RISCV_ADD8:
472     return (A + (S + RA)) & 0xFF;
473   case ELF::R_RISCV_SUB8:
474     return (A - (S + RA)) & 0xFF;
475   case ELF::R_RISCV_ADD16:
476     return (A + (S + RA)) & 0xFFFF;
477   case ELF::R_RISCV_SUB16:
478     return (A - (S + RA)) & 0xFFFF;
479   case ELF::R_RISCV_ADD32:
480     return (A + (S + RA)) & 0xFFFFFFFF;
481   case ELF::R_RISCV_SUB32:
482     return (A - (S + RA)) & 0xFFFFFFFF;
483   case ELF::R_RISCV_ADD64:
484     return (A + (S + RA));
485   case ELF::R_RISCV_SUB64:
486     return (A - (S + RA));
487   default:
488     llvm_unreachable("Invalid relocation type");
489   }
490 }
491 
492 static bool supportsCSKY(uint64_t Type) {
493   switch (Type) {
494   case ELF::R_CKCORE_NONE:
495   case ELF::R_CKCORE_ADDR32:
496   case ELF::R_CKCORE_PCREL32:
497     return true;
498   default:
499     return false;
500   }
501 }
502 
503 static uint64_t resolveCSKY(uint64_t Type, uint64_t Offset, uint64_t S,
504                             uint64_t LocData, int64_t Addend) {
505   switch (Type) {
506   case ELF::R_CKCORE_NONE:
507     return LocData;
508   case ELF::R_CKCORE_ADDR32:
509     return (S + Addend) & 0xFFFFFFFF;
510   case ELF::R_CKCORE_PCREL32:
511     return (S + Addend - Offset) & 0xFFFFFFFF;
512   default:
513     llvm_unreachable("Invalid relocation type");
514   }
515 }
516 
517 static bool supportsLoongArch(uint64_t Type) {
518   switch (Type) {
519   case ELF::R_LARCH_NONE:
520   case ELF::R_LARCH_32:
521   case ELF::R_LARCH_32_PCREL:
522   case ELF::R_LARCH_64:
523   case ELF::R_LARCH_ADD8:
524   case ELF::R_LARCH_SUB8:
525   case ELF::R_LARCH_ADD16:
526   case ELF::R_LARCH_SUB16:
527   case ELF::R_LARCH_ADD32:
528   case ELF::R_LARCH_SUB32:
529   case ELF::R_LARCH_ADD64:
530   case ELF::R_LARCH_SUB64:
531     return true;
532   default:
533     return false;
534   }
535 }
536 
537 static uint64_t resolveLoongArch(uint64_t Type, uint64_t Offset, uint64_t S,
538                                  uint64_t LocData, int64_t Addend) {
539   switch (Type) {
540   case ELF::R_LARCH_NONE:
541     return LocData;
542   case ELF::R_LARCH_32:
543     return (S + Addend) & 0xFFFFFFFF;
544   case ELF::R_LARCH_32_PCREL:
545     return (S + Addend - Offset) & 0xFFFFFFFF;
546   case ELF::R_LARCH_64:
547     return S + Addend;
548   case ELF::R_LARCH_ADD8:
549     return (LocData + (S + Addend)) & 0xFF;
550   case ELF::R_LARCH_SUB8:
551     return (LocData - (S + Addend)) & 0xFF;
552   case ELF::R_LARCH_ADD16:
553     return (LocData + (S + Addend)) & 0xFFFF;
554   case ELF::R_LARCH_SUB16:
555     return (LocData - (S + Addend)) & 0xFFFF;
556   case ELF::R_LARCH_ADD32:
557     return (LocData + (S + Addend)) & 0xFFFFFFFF;
558   case ELF::R_LARCH_SUB32:
559     return (LocData - (S + Addend)) & 0xFFFFFFFF;
560   case ELF::R_LARCH_ADD64:
561     return (LocData + (S + Addend));
562   case ELF::R_LARCH_SUB64:
563     return (LocData - (S + Addend));
564   default:
565     llvm_unreachable("Invalid relocation type");
566   }
567 }
568 
569 static bool supportsCOFFX86(uint64_t Type) {
570   switch (Type) {
571   case COFF::IMAGE_REL_I386_SECREL:
572   case COFF::IMAGE_REL_I386_DIR32:
573     return true;
574   default:
575     return false;
576   }
577 }
578 
579 static uint64_t resolveCOFFX86(uint64_t Type, uint64_t Offset, uint64_t S,
580                                uint64_t LocData, int64_t /*Addend*/) {
581   switch (Type) {
582   case COFF::IMAGE_REL_I386_SECREL:
583   case COFF::IMAGE_REL_I386_DIR32:
584     return (S + LocData) & 0xFFFFFFFF;
585   default:
586     llvm_unreachable("Invalid relocation type");
587   }
588 }
589 
590 static bool supportsCOFFX86_64(uint64_t Type) {
591   switch (Type) {
592   case COFF::IMAGE_REL_AMD64_SECREL:
593   case COFF::IMAGE_REL_AMD64_ADDR64:
594     return true;
595   default:
596     return false;
597   }
598 }
599 
600 static uint64_t resolveCOFFX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
601                                   uint64_t LocData, int64_t /*Addend*/) {
602   switch (Type) {
603   case COFF::IMAGE_REL_AMD64_SECREL:
604     return (S + LocData) & 0xFFFFFFFF;
605   case COFF::IMAGE_REL_AMD64_ADDR64:
606     return S + LocData;
607   default:
608     llvm_unreachable("Invalid relocation type");
609   }
610 }
611 
612 static bool supportsCOFFARM(uint64_t Type) {
613   switch (Type) {
614   case COFF::IMAGE_REL_ARM_SECREL:
615   case COFF::IMAGE_REL_ARM_ADDR32:
616     return true;
617   default:
618     return false;
619   }
620 }
621 
622 static uint64_t resolveCOFFARM(uint64_t Type, uint64_t Offset, uint64_t S,
623                                uint64_t LocData, int64_t /*Addend*/) {
624   switch (Type) {
625   case COFF::IMAGE_REL_ARM_SECREL:
626   case COFF::IMAGE_REL_ARM_ADDR32:
627     return (S + LocData) & 0xFFFFFFFF;
628   default:
629     llvm_unreachable("Invalid relocation type");
630   }
631 }
632 
633 static bool supportsCOFFARM64(uint64_t Type) {
634   switch (Type) {
635   case COFF::IMAGE_REL_ARM64_SECREL:
636   case COFF::IMAGE_REL_ARM64_ADDR64:
637     return true;
638   default:
639     return false;
640   }
641 }
642 
643 static uint64_t resolveCOFFARM64(uint64_t Type, uint64_t Offset, uint64_t S,
644                                  uint64_t LocData, int64_t /*Addend*/) {
645   switch (Type) {
646   case COFF::IMAGE_REL_ARM64_SECREL:
647     return (S + LocData) & 0xFFFFFFFF;
648   case COFF::IMAGE_REL_ARM64_ADDR64:
649     return S + LocData;
650   default:
651     llvm_unreachable("Invalid relocation type");
652   }
653 }
654 
655 static bool supportsMachOX86_64(uint64_t Type) {
656   return Type == MachO::X86_64_RELOC_UNSIGNED;
657 }
658 
659 static uint64_t resolveMachOX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
660                                    uint64_t LocData, int64_t /*Addend*/) {
661   if (Type == MachO::X86_64_RELOC_UNSIGNED)
662     return S;
663   llvm_unreachable("Invalid relocation type");
664 }
665 
666 static bool supportsWasm32(uint64_t Type) {
667   switch (Type) {
668   case wasm::R_WASM_FUNCTION_INDEX_LEB:
669   case wasm::R_WASM_TABLE_INDEX_SLEB:
670   case wasm::R_WASM_TABLE_INDEX_I32:
671   case wasm::R_WASM_MEMORY_ADDR_LEB:
672   case wasm::R_WASM_MEMORY_ADDR_SLEB:
673   case wasm::R_WASM_MEMORY_ADDR_I32:
674   case wasm::R_WASM_TYPE_INDEX_LEB:
675   case wasm::R_WASM_GLOBAL_INDEX_LEB:
676   case wasm::R_WASM_FUNCTION_OFFSET_I32:
677   case wasm::R_WASM_SECTION_OFFSET_I32:
678   case wasm::R_WASM_TAG_INDEX_LEB:
679   case wasm::R_WASM_GLOBAL_INDEX_I32:
680   case wasm::R_WASM_TABLE_NUMBER_LEB:
681   case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
682     return true;
683   default:
684     return false;
685   }
686 }
687 
688 static bool supportsWasm64(uint64_t Type) {
689   switch (Type) {
690   case wasm::R_WASM_MEMORY_ADDR_LEB64:
691   case wasm::R_WASM_MEMORY_ADDR_SLEB64:
692   case wasm::R_WASM_MEMORY_ADDR_I64:
693   case wasm::R_WASM_TABLE_INDEX_SLEB64:
694   case wasm::R_WASM_TABLE_INDEX_I64:
695   case wasm::R_WASM_FUNCTION_OFFSET_I64:
696     return true;
697   default:
698     return supportsWasm32(Type);
699   }
700 }
701 
702 static uint64_t resolveWasm32(uint64_t Type, uint64_t Offset, uint64_t S,
703                               uint64_t LocData, int64_t /*Addend*/) {
704   switch (Type) {
705   case wasm::R_WASM_FUNCTION_INDEX_LEB:
706   case wasm::R_WASM_TABLE_INDEX_SLEB:
707   case wasm::R_WASM_TABLE_INDEX_I32:
708   case wasm::R_WASM_MEMORY_ADDR_LEB:
709   case wasm::R_WASM_MEMORY_ADDR_SLEB:
710   case wasm::R_WASM_MEMORY_ADDR_I32:
711   case wasm::R_WASM_TYPE_INDEX_LEB:
712   case wasm::R_WASM_GLOBAL_INDEX_LEB:
713   case wasm::R_WASM_FUNCTION_OFFSET_I32:
714   case wasm::R_WASM_SECTION_OFFSET_I32:
715   case wasm::R_WASM_TAG_INDEX_LEB:
716   case wasm::R_WASM_GLOBAL_INDEX_I32:
717   case wasm::R_WASM_TABLE_NUMBER_LEB:
718   case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
719     // For wasm section, its offset at 0 -- ignoring Value
720     return LocData;
721   default:
722     llvm_unreachable("Invalid relocation type");
723   }
724 }
725 
726 static uint64_t resolveWasm64(uint64_t Type, uint64_t Offset, uint64_t S,
727                               uint64_t LocData, int64_t Addend) {
728   switch (Type) {
729   case wasm::R_WASM_MEMORY_ADDR_LEB64:
730   case wasm::R_WASM_MEMORY_ADDR_SLEB64:
731   case wasm::R_WASM_MEMORY_ADDR_I64:
732   case wasm::R_WASM_TABLE_INDEX_SLEB64:
733   case wasm::R_WASM_TABLE_INDEX_I64:
734   case wasm::R_WASM_FUNCTION_OFFSET_I64:
735     // For wasm section, its offset at 0 -- ignoring Value
736     return LocData;
737   default:
738     return resolveWasm32(Type, Offset, S, LocData, Addend);
739   }
740 }
741 
742 std::pair<SupportsRelocation, RelocationResolver>
743 getRelocationResolver(const ObjectFile &Obj) {
744   if (Obj.isCOFF()) {
745     switch (Obj.getArch()) {
746     case Triple::x86_64:
747       return {supportsCOFFX86_64, resolveCOFFX86_64};
748     case Triple::x86:
749       return {supportsCOFFX86, resolveCOFFX86};
750     case Triple::arm:
751     case Triple::thumb:
752       return {supportsCOFFARM, resolveCOFFARM};
753     case Triple::aarch64:
754       return {supportsCOFFARM64, resolveCOFFARM64};
755     default:
756       return {nullptr, nullptr};
757     }
758   } else if (Obj.isELF()) {
759     if (Obj.getBytesInAddress() == 8) {
760       switch (Obj.getArch()) {
761       case Triple::x86_64:
762         return {supportsX86_64, resolveX86_64};
763       case Triple::aarch64:
764       case Triple::aarch64_be:
765         return {supportsAArch64, resolveAArch64};
766       case Triple::bpfel:
767       case Triple::bpfeb:
768         return {supportsBPF, resolveBPF};
769       case Triple::loongarch64:
770         return {supportsLoongArch, resolveLoongArch};
771       case Triple::mips64el:
772       case Triple::mips64:
773         return {supportsMips64, resolveMips64};
774       case Triple::ppc64le:
775       case Triple::ppc64:
776         return {supportsPPC64, resolvePPC64};
777       case Triple::systemz:
778         return {supportsSystemZ, resolveSystemZ};
779       case Triple::sparcv9:
780         return {supportsSparc64, resolveSparc64};
781       case Triple::amdgcn:
782         return {supportsAmdgpu, resolveAmdgpu};
783       case Triple::riscv64:
784         return {supportsRISCV, resolveRISCV};
785       default:
786         return {nullptr, nullptr};
787       }
788     }
789 
790     // 32-bit object file
791     assert(Obj.getBytesInAddress() == 4 &&
792            "Invalid word size in object file");
793 
794     switch (Obj.getArch()) {
795     case Triple::x86:
796       return {supportsX86, resolveX86};
797     case Triple::ppcle:
798     case Triple::ppc:
799       return {supportsPPC32, resolvePPC32};
800     case Triple::arm:
801     case Triple::armeb:
802       return {supportsARM, resolveARM};
803     case Triple::avr:
804       return {supportsAVR, resolveAVR};
805     case Triple::lanai:
806       return {supportsLanai, resolveLanai};
807     case Triple::loongarch32:
808       return {supportsLoongArch, resolveLoongArch};
809     case Triple::mipsel:
810     case Triple::mips:
811       return {supportsMips32, resolveMips32};
812     case Triple::msp430:
813       return {supportsMSP430, resolveMSP430};
814     case Triple::sparc:
815       return {supportsSparc32, resolveSparc32};
816     case Triple::hexagon:
817       return {supportsHexagon, resolveHexagon};
818     case Triple::riscv32:
819       return {supportsRISCV, resolveRISCV};
820     case Triple::csky:
821       return {supportsCSKY, resolveCSKY};
822     default:
823       return {nullptr, nullptr};
824     }
825   } else if (Obj.isMachO()) {
826     if (Obj.getArch() == Triple::x86_64)
827       return {supportsMachOX86_64, resolveMachOX86_64};
828     return {nullptr, nullptr};
829   } else if (Obj.isWasm()) {
830     if (Obj.getArch() == Triple::wasm32)
831       return {supportsWasm32, resolveWasm32};
832     if (Obj.getArch() == Triple::wasm64)
833       return {supportsWasm64, resolveWasm64};
834     return {nullptr, nullptr};
835   }
836 
837   llvm_unreachable("Invalid object file");
838 }
839 
840 uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R,
841                            uint64_t S, uint64_t LocData) {
842   if (const ObjectFile *Obj = R.getObject()) {
843     int64_t Addend = 0;
844     if (Obj->isELF()) {
845       auto GetRelSectionType = [&]() -> unsigned {
846         if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
847           return Elf32LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
848         if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
849           return Elf64LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
850         if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
851           return Elf32BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
852         auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj);
853         return Elf64BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
854       };
855 
856       if (GetRelSectionType() == ELF::SHT_RELA) {
857         Addend = getELFAddend(R);
858         // RISCV relocations use both LocData and Addend.
859         if (Obj->getArch() != Triple::riscv32 &&
860             Obj->getArch() != Triple::riscv64)
861           LocData = 0;
862       }
863     }
864 
865     return Resolver(R.getType(), R.getOffset(), S, LocData, Addend);
866   }
867 
868   // Sometimes the caller might want to use its own specific implementation of
869   // the resolver function. E.g. this is used by LLD when it resolves debug
870   // relocations and assumes that all of them have the same computation (S + A).
871   // The relocation R has no owner object in this case and we don't need to
872   // provide Type and Offset fields. It is also assumed the DataRefImpl.p
873   // contains the addend, provided by the caller.
874   return Resolver(/*Type=*/0, /*Offset=*/0, S, LocData,
875                   R.getRawDataRefImpl().p);
876 }
877 
878 } // namespace object
879 } // namespace llvm
880