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 supportsCSKY(uint64_t Type) {
490   switch (Type) {
491   case ELF::R_CKCORE_NONE:
492   case ELF::R_CKCORE_ADDR32:
493   case ELF::R_CKCORE_PCREL32:
494     return true;
495   default:
496     return false;
497   }
498 }
499 
500 static uint64_t resolveCSKY(uint64_t Type, uint64_t Offset, uint64_t S,
501                             uint64_t LocData, int64_t Addend) {
502   switch (Type) {
503   case ELF::R_CKCORE_NONE:
504     return LocData;
505   case ELF::R_CKCORE_ADDR32:
506     return (S + Addend) & 0xFFFFFFFF;
507   case ELF::R_CKCORE_PCREL32:
508     return (S + Addend - Offset) & 0xFFFFFFFF;
509   default:
510     llvm_unreachable("Invalid relocation type");
511   }
512 }
513 
514 static bool supportsCOFFX86(uint64_t Type) {
515   switch (Type) {
516   case COFF::IMAGE_REL_I386_SECREL:
517   case COFF::IMAGE_REL_I386_DIR32:
518     return true;
519   default:
520     return false;
521   }
522 }
523 
524 static uint64_t resolveCOFFX86(uint64_t Type, uint64_t Offset, uint64_t S,
525                                uint64_t LocData, int64_t /*Addend*/) {
526   switch (Type) {
527   case COFF::IMAGE_REL_I386_SECREL:
528   case COFF::IMAGE_REL_I386_DIR32:
529     return (S + LocData) & 0xFFFFFFFF;
530   default:
531     llvm_unreachable("Invalid relocation type");
532   }
533 }
534 
535 static bool supportsCOFFX86_64(uint64_t Type) {
536   switch (Type) {
537   case COFF::IMAGE_REL_AMD64_SECREL:
538   case COFF::IMAGE_REL_AMD64_ADDR64:
539     return true;
540   default:
541     return false;
542   }
543 }
544 
545 static uint64_t resolveCOFFX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
546                                   uint64_t LocData, int64_t /*Addend*/) {
547   switch (Type) {
548   case COFF::IMAGE_REL_AMD64_SECREL:
549     return (S + LocData) & 0xFFFFFFFF;
550   case COFF::IMAGE_REL_AMD64_ADDR64:
551     return S + LocData;
552   default:
553     llvm_unreachable("Invalid relocation type");
554   }
555 }
556 
557 static bool supportsCOFFARM(uint64_t Type) {
558   switch (Type) {
559   case COFF::IMAGE_REL_ARM_SECREL:
560   case COFF::IMAGE_REL_ARM_ADDR32:
561     return true;
562   default:
563     return false;
564   }
565 }
566 
567 static uint64_t resolveCOFFARM(uint64_t Type, uint64_t Offset, uint64_t S,
568                                uint64_t LocData, int64_t /*Addend*/) {
569   switch (Type) {
570   case COFF::IMAGE_REL_ARM_SECREL:
571   case COFF::IMAGE_REL_ARM_ADDR32:
572     return (S + LocData) & 0xFFFFFFFF;
573   default:
574     llvm_unreachable("Invalid relocation type");
575   }
576 }
577 
578 static bool supportsCOFFARM64(uint64_t Type) {
579   switch (Type) {
580   case COFF::IMAGE_REL_ARM64_SECREL:
581   case COFF::IMAGE_REL_ARM64_ADDR64:
582     return true;
583   default:
584     return false;
585   }
586 }
587 
588 static uint64_t resolveCOFFARM64(uint64_t Type, uint64_t Offset, uint64_t S,
589                                  uint64_t LocData, int64_t /*Addend*/) {
590   switch (Type) {
591   case COFF::IMAGE_REL_ARM64_SECREL:
592     return (S + LocData) & 0xFFFFFFFF;
593   case COFF::IMAGE_REL_ARM64_ADDR64:
594     return S + LocData;
595   default:
596     llvm_unreachable("Invalid relocation type");
597   }
598 }
599 
600 static bool supportsMachOX86_64(uint64_t Type) {
601   return Type == MachO::X86_64_RELOC_UNSIGNED;
602 }
603 
604 static uint64_t resolveMachOX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
605                                    uint64_t LocData, int64_t /*Addend*/) {
606   if (Type == MachO::X86_64_RELOC_UNSIGNED)
607     return S;
608   llvm_unreachable("Invalid relocation type");
609 }
610 
611 static bool supportsWasm32(uint64_t Type) {
612   switch (Type) {
613   case wasm::R_WASM_FUNCTION_INDEX_LEB:
614   case wasm::R_WASM_TABLE_INDEX_SLEB:
615   case wasm::R_WASM_TABLE_INDEX_I32:
616   case wasm::R_WASM_MEMORY_ADDR_LEB:
617   case wasm::R_WASM_MEMORY_ADDR_SLEB:
618   case wasm::R_WASM_MEMORY_ADDR_I32:
619   case wasm::R_WASM_TYPE_INDEX_LEB:
620   case wasm::R_WASM_GLOBAL_INDEX_LEB:
621   case wasm::R_WASM_FUNCTION_OFFSET_I32:
622   case wasm::R_WASM_SECTION_OFFSET_I32:
623   case wasm::R_WASM_TAG_INDEX_LEB:
624   case wasm::R_WASM_GLOBAL_INDEX_I32:
625   case wasm::R_WASM_TABLE_NUMBER_LEB:
626   case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
627     return true;
628   default:
629     return false;
630   }
631 }
632 
633 static bool supportsWasm64(uint64_t Type) {
634   switch (Type) {
635   case wasm::R_WASM_MEMORY_ADDR_LEB64:
636   case wasm::R_WASM_MEMORY_ADDR_SLEB64:
637   case wasm::R_WASM_MEMORY_ADDR_I64:
638   case wasm::R_WASM_TABLE_INDEX_SLEB64:
639   case wasm::R_WASM_TABLE_INDEX_I64:
640   case wasm::R_WASM_FUNCTION_OFFSET_I64:
641     return true;
642   default:
643     return supportsWasm32(Type);
644   }
645 }
646 
647 static uint64_t resolveWasm32(uint64_t Type, uint64_t Offset, uint64_t S,
648                               uint64_t LocData, int64_t /*Addend*/) {
649   switch (Type) {
650   case wasm::R_WASM_FUNCTION_INDEX_LEB:
651   case wasm::R_WASM_TABLE_INDEX_SLEB:
652   case wasm::R_WASM_TABLE_INDEX_I32:
653   case wasm::R_WASM_MEMORY_ADDR_LEB:
654   case wasm::R_WASM_MEMORY_ADDR_SLEB:
655   case wasm::R_WASM_MEMORY_ADDR_I32:
656   case wasm::R_WASM_TYPE_INDEX_LEB:
657   case wasm::R_WASM_GLOBAL_INDEX_LEB:
658   case wasm::R_WASM_FUNCTION_OFFSET_I32:
659   case wasm::R_WASM_SECTION_OFFSET_I32:
660   case wasm::R_WASM_TAG_INDEX_LEB:
661   case wasm::R_WASM_GLOBAL_INDEX_I32:
662   case wasm::R_WASM_TABLE_NUMBER_LEB:
663   case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
664     // For wasm section, its offset at 0 -- ignoring Value
665     return LocData;
666   default:
667     llvm_unreachable("Invalid relocation type");
668   }
669 }
670 
671 static uint64_t resolveWasm64(uint64_t Type, uint64_t Offset, uint64_t S,
672                               uint64_t LocData, int64_t Addend) {
673   switch (Type) {
674   case wasm::R_WASM_MEMORY_ADDR_LEB64:
675   case wasm::R_WASM_MEMORY_ADDR_SLEB64:
676   case wasm::R_WASM_MEMORY_ADDR_I64:
677   case wasm::R_WASM_TABLE_INDEX_SLEB64:
678   case wasm::R_WASM_TABLE_INDEX_I64:
679   case wasm::R_WASM_FUNCTION_OFFSET_I64:
680     // For wasm section, its offset at 0 -- ignoring Value
681     return LocData;
682   default:
683     return resolveWasm32(Type, Offset, S, LocData, Addend);
684   }
685 }
686 
687 std::pair<SupportsRelocation, RelocationResolver>
688 getRelocationResolver(const ObjectFile &Obj) {
689   if (Obj.isCOFF()) {
690     switch (Obj.getArch()) {
691     case Triple::x86_64:
692       return {supportsCOFFX86_64, resolveCOFFX86_64};
693     case Triple::x86:
694       return {supportsCOFFX86, resolveCOFFX86};
695     case Triple::arm:
696     case Triple::thumb:
697       return {supportsCOFFARM, resolveCOFFARM};
698     case Triple::aarch64:
699       return {supportsCOFFARM64, resolveCOFFARM64};
700     default:
701       return {nullptr, nullptr};
702     }
703   } else if (Obj.isELF()) {
704     if (Obj.getBytesInAddress() == 8) {
705       switch (Obj.getArch()) {
706       case Triple::x86_64:
707         return {supportsX86_64, resolveX86_64};
708       case Triple::aarch64:
709       case Triple::aarch64_be:
710         return {supportsAArch64, resolveAArch64};
711       case Triple::bpfel:
712       case Triple::bpfeb:
713         return {supportsBPF, resolveBPF};
714       case Triple::mips64el:
715       case Triple::mips64:
716         return {supportsMips64, resolveMips64};
717       case Triple::ppc64le:
718       case Triple::ppc64:
719         return {supportsPPC64, resolvePPC64};
720       case Triple::systemz:
721         return {supportsSystemZ, resolveSystemZ};
722       case Triple::sparcv9:
723         return {supportsSparc64, resolveSparc64};
724       case Triple::amdgcn:
725         return {supportsAmdgpu, resolveAmdgpu};
726       case Triple::riscv64:
727         return {supportsRISCV, resolveRISCV};
728       default:
729         return {nullptr, nullptr};
730       }
731     }
732 
733     // 32-bit object file
734     assert(Obj.getBytesInAddress() == 4 &&
735            "Invalid word size in object file");
736 
737     switch (Obj.getArch()) {
738     case Triple::x86:
739       return {supportsX86, resolveX86};
740     case Triple::ppcle:
741     case Triple::ppc:
742       return {supportsPPC32, resolvePPC32};
743     case Triple::arm:
744     case Triple::armeb:
745       return {supportsARM, resolveARM};
746     case Triple::avr:
747       return {supportsAVR, resolveAVR};
748     case Triple::lanai:
749       return {supportsLanai, resolveLanai};
750     case Triple::mipsel:
751     case Triple::mips:
752       return {supportsMips32, resolveMips32};
753     case Triple::msp430:
754       return {supportsMSP430, resolveMSP430};
755     case Triple::sparc:
756       return {supportsSparc32, resolveSparc32};
757     case Triple::hexagon:
758       return {supportsHexagon, resolveHexagon};
759     case Triple::riscv32:
760       return {supportsRISCV, resolveRISCV};
761     case Triple::csky:
762       return {supportsCSKY, resolveCSKY};
763     default:
764       return {nullptr, nullptr};
765     }
766   } else if (Obj.isMachO()) {
767     if (Obj.getArch() == Triple::x86_64)
768       return {supportsMachOX86_64, resolveMachOX86_64};
769     return {nullptr, nullptr};
770   } else if (Obj.isWasm()) {
771     if (Obj.getArch() == Triple::wasm32)
772       return {supportsWasm32, resolveWasm32};
773     if (Obj.getArch() == Triple::wasm64)
774       return {supportsWasm64, resolveWasm64};
775     return {nullptr, nullptr};
776   }
777 
778   llvm_unreachable("Invalid object file");
779 }
780 
781 uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R,
782                            uint64_t S, uint64_t LocData) {
783   if (const ObjectFile *Obj = R.getObject()) {
784     int64_t Addend = 0;
785     if (Obj->isELF()) {
786       auto GetRelSectionType = [&]() -> unsigned {
787         if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
788           return Elf32LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
789         if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
790           return Elf64LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
791         if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
792           return Elf32BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
793         auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj);
794         return Elf64BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
795       };
796 
797       if (GetRelSectionType() == ELF::SHT_RELA) {
798         Addend = getELFAddend(R);
799         // RISCV relocations use both LocData and Addend.
800         if (Obj->getArch() != Triple::riscv32 &&
801             Obj->getArch() != Triple::riscv64)
802           LocData = 0;
803       }
804     }
805 
806     return Resolver(R.getType(), R.getOffset(), S, LocData, Addend);
807   }
808 
809   // Sometimes the caller might want to use its own specific implementation of
810   // the resolver function. E.g. this is used by LLD when it resolves debug
811   // relocations and assumes that all of them have the same computation (S + A).
812   // The relocation R has no owner object in this case and we don't need to
813   // provide Type and Offset fields. It is also assumed the DataRefImpl.p
814   // contains the addend, provided by the caller.
815   return Resolver(/*Type=*/0, /*Offset=*/0, S, LocData,
816                   R.getRawDataRefImpl().p);
817 }
818 
819 } // namespace object
820 } // namespace llvm
821