aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/lib/Object/RelocationResolver.cpp
blob: 13a7a98511370b33ee1138bbd5303d31075573bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
//===- RelocationResolver.cpp ------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines utilities to resolve relocations in object files.
//
//===----------------------------------------------------------------------===//

#include "llvm/Object/RelocationResolver.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ADT/Twine.h"
#include "llvm/BinaryFormat/COFF.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/BinaryFormat/Wasm.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Object/ELFTypes.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/SymbolicFile.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include <cassert>
#include <vector>

namespace llvm {
namespace object {

static int64_t getELFAddend(RelocationRef R) {
  Expected<int64_t> AddendOrErr = ELFRelocationRef(R).getAddend();
  handleAllErrors(AddendOrErr.takeError(), [](const ErrorInfoBase &EI) {
    report_fatal_error(Twine(EI.message()));
  });
  return *AddendOrErr;
}

static bool supportsX86_64(uint64_t Type) {
  switch (Type) {
  case ELF::R_X86_64_NONE:
  case ELF::R_X86_64_64:
  case ELF::R_X86_64_DTPOFF32:
  case ELF::R_X86_64_DTPOFF64:
  case ELF::R_X86_64_PC32:
  case ELF::R_X86_64_PC64:
  case ELF::R_X86_64_32:
  case ELF::R_X86_64_32S:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
                              uint64_t LocData, int64_t Addend) {
  switch (Type) {
  case ELF::R_X86_64_NONE:
    return LocData;
  case ELF::R_X86_64_64:
  case ELF::R_X86_64_DTPOFF32:
  case ELF::R_X86_64_DTPOFF64:
    return S + Addend;
  case ELF::R_X86_64_PC32:
  case ELF::R_X86_64_PC64:
    return S + Addend - Offset;
  case ELF::R_X86_64_32:
  case ELF::R_X86_64_32S:
    return (S + Addend) & 0xFFFFFFFF;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsAArch64(uint64_t Type) {
  switch (Type) {
  case ELF::R_AARCH64_ABS32:
  case ELF::R_AARCH64_ABS64:
  case ELF::R_AARCH64_PREL16:
  case ELF::R_AARCH64_PREL32:
  case ELF::R_AARCH64_PREL64:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveAArch64(uint64_t Type, uint64_t Offset, uint64_t S,
                               uint64_t /*LocData*/, int64_t Addend) {
  switch (Type) {
  case ELF::R_AARCH64_ABS32:
    return (S + Addend) & 0xFFFFFFFF;
  case ELF::R_AARCH64_ABS64:
    return S + Addend;
  case ELF::R_AARCH64_PREL16:
    return (S + Addend - Offset) & 0xFFFF;
  case ELF::R_AARCH64_PREL32:
    return (S + Addend - Offset) & 0xFFFFFFFF;
  case ELF::R_AARCH64_PREL64:
    return S + Addend - Offset;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsBPF(uint64_t Type) {
  switch (Type) {
  case ELF::R_BPF_64_ABS32:
  case ELF::R_BPF_64_ABS64:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveBPF(uint64_t Type, uint64_t Offset, uint64_t S,
                           uint64_t LocData, int64_t /*Addend*/) {
  switch (Type) {
  case ELF::R_BPF_64_ABS32:
    return (S + LocData) & 0xFFFFFFFF;
  case ELF::R_BPF_64_ABS64:
    return S + LocData;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsMips64(uint64_t Type) {
  switch (Type) {
  case ELF::R_MIPS_32:
  case ELF::R_MIPS_64:
  case ELF::R_MIPS_TLS_DTPREL64:
  case ELF::R_MIPS_PC32:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveMips64(uint64_t Type, uint64_t Offset, uint64_t S,
                              uint64_t /*LocData*/, int64_t Addend) {
  switch (Type) {
  case ELF::R_MIPS_32:
    return (S + Addend) & 0xFFFFFFFF;
  case ELF::R_MIPS_64:
    return S + Addend;
  case ELF::R_MIPS_TLS_DTPREL64:
    return S + Addend - 0x8000;
  case ELF::R_MIPS_PC32:
    return S + Addend - Offset;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsMSP430(uint64_t Type) {
  switch (Type) {
  case ELF::R_MSP430_32:
  case ELF::R_MSP430_16_BYTE:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveMSP430(uint64_t Type, uint64_t Offset, uint64_t S,
                              uint64_t /*LocData*/, int64_t Addend) {
  switch (Type) {
  case ELF::R_MSP430_32:
    return (S + Addend) & 0xFFFFFFFF;
  case ELF::R_MSP430_16_BYTE:
    return (S + Addend) & 0xFFFF;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsPPC64(uint64_t Type) {
  switch (Type) {
  case ELF::R_PPC64_ADDR32:
  case ELF::R_PPC64_ADDR64:
  case ELF::R_PPC64_REL32:
  case ELF::R_PPC64_REL64:
    return true;
  default:
    return false;
  }
}

static uint64_t resolvePPC64(uint64_t Type, uint64_t Offset, uint64_t S,
                             uint64_t /*LocData*/, int64_t Addend) {
  switch (Type) {
  case ELF::R_PPC64_ADDR32:
    return (S + Addend) & 0xFFFFFFFF;
  case ELF::R_PPC64_ADDR64:
    return S + Addend;
  case ELF::R_PPC64_REL32:
    return (S + Addend - Offset) & 0xFFFFFFFF;
  case ELF::R_PPC64_REL64:
    return S + Addend - Offset;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsSystemZ(uint64_t Type) {
  switch (Type) {
  case ELF::R_390_32:
  case ELF::R_390_64:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveSystemZ(uint64_t Type, uint64_t Offset, uint64_t S,
                               uint64_t /*LocData*/, int64_t Addend) {
  switch (Type) {
  case ELF::R_390_32:
    return (S + Addend) & 0xFFFFFFFF;
  case ELF::R_390_64:
    return S + Addend;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsSparc64(uint64_t Type) {
  switch (Type) {
  case ELF::R_SPARC_32:
  case ELF::R_SPARC_64:
  case ELF::R_SPARC_UA32:
  case ELF::R_SPARC_UA64:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveSparc64(uint64_t Type, uint64_t Offset, uint64_t S,
                               uint64_t /*LocData*/, int64_t Addend) {
  switch (Type) {
  case ELF::R_SPARC_32:
  case ELF::R_SPARC_64:
  case ELF::R_SPARC_UA32:
  case ELF::R_SPARC_UA64:
    return S + Addend;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsAmdgpu(uint64_t Type) {
  switch (Type) {
  case ELF::R_AMDGPU_ABS32:
  case ELF::R_AMDGPU_ABS64:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveAmdgpu(uint64_t Type, uint64_t Offset, uint64_t S,
                              uint64_t /*LocData*/, int64_t Addend) {
  switch (Type) {
  case ELF::R_AMDGPU_ABS32:
  case ELF::R_AMDGPU_ABS64:
    return S + Addend;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsX86(uint64_t Type) {
  switch (Type) {
  case ELF::R_386_NONE:
  case ELF::R_386_32:
  case ELF::R_386_PC32:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveX86(uint64_t Type, uint64_t Offset, uint64_t S,
                           uint64_t LocData, int64_t /*Addend*/) {
  switch (Type) {
  case ELF::R_386_NONE:
    return LocData;
  case ELF::R_386_32:
    return S + LocData;
  case ELF::R_386_PC32:
    return S - Offset + LocData;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsPPC32(uint64_t Type) {
  switch (Type) {
  case ELF::R_PPC_ADDR32:
  case ELF::R_PPC_REL32:
    return true;
  default:
    return false;
  }
}

static uint64_t resolvePPC32(uint64_t Type, uint64_t Offset, uint64_t S,
                             uint64_t /*LocData*/, int64_t Addend) {
  switch (Type) {
  case ELF::R_PPC_ADDR32:
    return (S + Addend) & 0xFFFFFFFF;
  case ELF::R_PPC_REL32:
    return (S + Addend - Offset) & 0xFFFFFFFF;
  }
  llvm_unreachable("Invalid relocation type");
}

static bool supportsARM(uint64_t Type) {
  switch (Type) {
  case ELF::R_ARM_ABS32:
  case ELF::R_ARM_REL32:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveARM(uint64_t Type, uint64_t Offset, uint64_t S,
                           uint64_t LocData, int64_t Addend) {
  // Support both RELA and REL relocations. The caller is responsible
  // for supplying the correct values for LocData and Addend, i.e.
  // Addend == 0 for REL and LocData == 0 for RELA.
  assert((LocData == 0 || Addend == 0) &&
         "one of LocData and Addend must be 0");
  switch (Type) {
  case ELF::R_ARM_ABS32:
    return (S + LocData + Addend) & 0xFFFFFFFF;
  case ELF::R_ARM_REL32:
    return (S + LocData + Addend - Offset) & 0xFFFFFFFF;
  }
  llvm_unreachable("Invalid relocation type");
}

static bool supportsAVR(uint64_t Type) {
  switch (Type) {
  case ELF::R_AVR_16:
  case ELF::R_AVR_32:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveAVR(uint64_t Type, uint64_t Offset, uint64_t S,
                           uint64_t /*LocData*/, int64_t Addend) {
  switch (Type) {
  case ELF::R_AVR_16:
    return (S + Addend) & 0xFFFF;
  case ELF::R_AVR_32:
    return (S + Addend) & 0xFFFFFFFF;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsLanai(uint64_t Type) {
  return Type == ELF::R_LANAI_32;
}

static uint64_t resolveLanai(uint64_t Type, uint64_t Offset, uint64_t S,
                             uint64_t /*LocData*/, int64_t Addend) {
  if (Type == ELF::R_LANAI_32)
    return (S + Addend) & 0xFFFFFFFF;
  llvm_unreachable("Invalid relocation type");
}

static bool supportsMips32(uint64_t Type) {
  switch (Type) {
  case ELF::R_MIPS_32:
  case ELF::R_MIPS_TLS_DTPREL32:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveMips32(uint64_t Type, uint64_t Offset, uint64_t S,
                              uint64_t LocData, int64_t /*Addend*/) {
  // FIXME: Take in account implicit addends to get correct results.
  if (Type == ELF::R_MIPS_32)
    return (S + LocData) & 0xFFFFFFFF;
  if (Type == ELF::R_MIPS_TLS_DTPREL32)
    return (S + LocData) & 0xFFFFFFFF;
  llvm_unreachable("Invalid relocation type");
}

static bool supportsSparc32(uint64_t Type) {
  switch (Type) {
  case ELF::R_SPARC_32:
  case ELF::R_SPARC_UA32:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveSparc32(uint64_t Type, uint64_t Offset, uint64_t S,
                               uint64_t LocData, int64_t Addend) {
  if (Type == ELF::R_SPARC_32 || Type == ELF::R_SPARC_UA32)
    return S + Addend;
  return LocData;
}

static bool supportsHexagon(uint64_t Type) {
  return Type == ELF::R_HEX_32;
}

static uint64_t resolveHexagon(uint64_t Type, uint64_t Offset, uint64_t S,
                               uint64_t /*LocData*/, int64_t Addend) {
  if (Type == ELF::R_HEX_32)
    return S + Addend;
  llvm_unreachable("Invalid relocation type");
}

static bool supportsRISCV(uint64_t Type) {
  switch (Type) {
  case ELF::R_RISCV_NONE:
  case ELF::R_RISCV_32:
  case ELF::R_RISCV_32_PCREL:
  case ELF::R_RISCV_64:
  case ELF::R_RISCV_SET6:
  case ELF::R_RISCV_SET8:
  case ELF::R_RISCV_SUB6:
  case ELF::R_RISCV_ADD8:
  case ELF::R_RISCV_SUB8:
  case ELF::R_RISCV_SET16:
  case ELF::R_RISCV_ADD16:
  case ELF::R_RISCV_SUB16:
  case ELF::R_RISCV_SET32:
  case ELF::R_RISCV_ADD32:
  case ELF::R_RISCV_SUB32:
  case ELF::R_RISCV_ADD64:
  case ELF::R_RISCV_SUB64:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveRISCV(uint64_t Type, uint64_t Offset, uint64_t S,
                             uint64_t LocData, int64_t Addend) {
  int64_t RA = Addend;
  uint64_t A = LocData;
  switch (Type) {
  case ELF::R_RISCV_NONE:
    return LocData;
  case ELF::R_RISCV_32:
    return (S + RA) & 0xFFFFFFFF;
  case ELF::R_RISCV_32_PCREL:
    return (S + RA - Offset) & 0xFFFFFFFF;
  case ELF::R_RISCV_64:
    return S + RA;
  case ELF::R_RISCV_SET6:
    return (A & 0xC0) | ((S + RA) & 0x3F);
  case ELF::R_RISCV_SUB6:
    return (A & 0xC0) | (((A & 0x3F) - (S + RA)) & 0x3F);
  case ELF::R_RISCV_SET8:
    return (S + RA) & 0xFF;
  case ELF::R_RISCV_ADD8:
    return (A + (S + RA)) & 0xFF;
  case ELF::R_RISCV_SUB8:
    return (A - (S + RA)) & 0xFF;
  case ELF::R_RISCV_SET16:
    return (S + RA) & 0xFFFF;
  case ELF::R_RISCV_ADD16:
    return (A + (S + RA)) & 0xFFFF;
  case ELF::R_RISCV_SUB16:
    return (A - (S + RA)) & 0xFFFF;
  case ELF::R_RISCV_SET32:
    return (S + RA) & 0xFFFFFFFF;
  case ELF::R_RISCV_ADD32:
    return (A + (S + RA)) & 0xFFFFFFFF;
  case ELF::R_RISCV_SUB32:
    return (A - (S + RA)) & 0xFFFFFFFF;
  case ELF::R_RISCV_ADD64:
    return (A + (S + RA));
  case ELF::R_RISCV_SUB64:
    return (A - (S + RA));
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsCSKY(uint64_t Type) {
  switch (Type) {
  case ELF::R_CKCORE_NONE:
  case ELF::R_CKCORE_ADDR32:
  case ELF::R_CKCORE_PCREL32:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveCSKY(uint64_t Type, uint64_t Offset, uint64_t S,
                            uint64_t LocData, int64_t Addend) {
  switch (Type) {
  case ELF::R_CKCORE_NONE:
    return LocData;
  case ELF::R_CKCORE_ADDR32:
    return (S + Addend) & 0xFFFFFFFF;
  case ELF::R_CKCORE_PCREL32:
    return (S + Addend - Offset) & 0xFFFFFFFF;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsLoongArch(uint64_t Type) {
  switch (Type) {
  case ELF::R_LARCH_NONE:
  case ELF::R_LARCH_32:
  case ELF::R_LARCH_32_PCREL:
  case ELF::R_LARCH_64:
  case ELF::R_LARCH_ADD8:
  case ELF::R_LARCH_SUB8:
  case ELF::R_LARCH_ADD16:
  case ELF::R_LARCH_SUB16:
  case ELF::R_LARCH_ADD32:
  case ELF::R_LARCH_SUB32:
  case ELF::R_LARCH_ADD64:
  case ELF::R_LARCH_SUB64:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveLoongArch(uint64_t Type, uint64_t Offset, uint64_t S,
                                 uint64_t LocData, int64_t Addend) {
  switch (Type) {
  case ELF::R_LARCH_NONE:
    return LocData;
  case ELF::R_LARCH_32:
    return (S + Addend) & 0xFFFFFFFF;
  case ELF::R_LARCH_32_PCREL:
    return (S + Addend - Offset) & 0xFFFFFFFF;
  case ELF::R_LARCH_64:
    return S + Addend;
  case ELF::R_LARCH_ADD8:
    return (LocData + (S + Addend)) & 0xFF;
  case ELF::R_LARCH_SUB8:
    return (LocData - (S + Addend)) & 0xFF;
  case ELF::R_LARCH_ADD16:
    return (LocData + (S + Addend)) & 0xFFFF;
  case ELF::R_LARCH_SUB16:
    return (LocData - (S + Addend)) & 0xFFFF;
  case ELF::R_LARCH_ADD32:
    return (LocData + (S + Addend)) & 0xFFFFFFFF;
  case ELF::R_LARCH_SUB32:
    return (LocData - (S + Addend)) & 0xFFFFFFFF;
  case ELF::R_LARCH_ADD64:
    return (LocData + (S + Addend));
  case ELF::R_LARCH_SUB64:
    return (LocData - (S + Addend));
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsCOFFX86(uint64_t Type) {
  switch (Type) {
  case COFF::IMAGE_REL_I386_SECREL:
  case COFF::IMAGE_REL_I386_DIR32:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveCOFFX86(uint64_t Type, uint64_t Offset, uint64_t S,
                               uint64_t LocData, int64_t /*Addend*/) {
  switch (Type) {
  case COFF::IMAGE_REL_I386_SECREL:
  case COFF::IMAGE_REL_I386_DIR32:
    return (S + LocData) & 0xFFFFFFFF;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsCOFFX86_64(uint64_t Type) {
  switch (Type) {
  case COFF::IMAGE_REL_AMD64_SECREL:
  case COFF::IMAGE_REL_AMD64_ADDR64:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveCOFFX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
                                  uint64_t LocData, int64_t /*Addend*/) {
  switch (Type) {
  case COFF::IMAGE_REL_AMD64_SECREL:
    return (S + LocData) & 0xFFFFFFFF;
  case COFF::IMAGE_REL_AMD64_ADDR64:
    return S + LocData;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsCOFFARM(uint64_t Type) {
  switch (Type) {
  case COFF::IMAGE_REL_ARM_SECREL:
  case COFF::IMAGE_REL_ARM_ADDR32:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveCOFFARM(uint64_t Type, uint64_t Offset, uint64_t S,
                               uint64_t LocData, int64_t /*Addend*/) {
  switch (Type) {
  case COFF::IMAGE_REL_ARM_SECREL:
  case COFF::IMAGE_REL_ARM_ADDR32:
    return (S + LocData) & 0xFFFFFFFF;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsCOFFARM64(uint64_t Type) {
  switch (Type) {
  case COFF::IMAGE_REL_ARM64_SECREL:
  case COFF::IMAGE_REL_ARM64_ADDR64:
    return true;
  default:
    return false;
  }
}

static uint64_t resolveCOFFARM64(uint64_t Type, uint64_t Offset, uint64_t S,
                                 uint64_t LocData, int64_t /*Addend*/) {
  switch (Type) {
  case COFF::IMAGE_REL_ARM64_SECREL:
    return (S + LocData) & 0xFFFFFFFF;
  case COFF::IMAGE_REL_ARM64_ADDR64:
    return S + LocData;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static bool supportsMachOX86_64(uint64_t Type) {
  return Type == MachO::X86_64_RELOC_UNSIGNED;
}

static uint64_t resolveMachOX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
                                   uint64_t LocData, int64_t /*Addend*/) {
  if (Type == MachO::X86_64_RELOC_UNSIGNED)
    return S;
  llvm_unreachable("Invalid relocation type");
}

static bool supportsWasm32(uint64_t Type) {
  switch (Type) {
  case wasm::R_WASM_FUNCTION_INDEX_LEB:
  case wasm::R_WASM_TABLE_INDEX_SLEB:
  case wasm::R_WASM_TABLE_INDEX_I32:
  case wasm::R_WASM_MEMORY_ADDR_LEB:
  case wasm::R_WASM_MEMORY_ADDR_SLEB:
  case wasm::R_WASM_MEMORY_ADDR_I32:
  case wasm::R_WASM_TYPE_INDEX_LEB:
  case wasm::R_WASM_GLOBAL_INDEX_LEB:
  case wasm::R_WASM_FUNCTION_OFFSET_I32:
  case wasm::R_WASM_SECTION_OFFSET_I32:
  case wasm::R_WASM_TAG_INDEX_LEB:
  case wasm::R_WASM_GLOBAL_INDEX_I32:
  case wasm::R_WASM_TABLE_NUMBER_LEB:
  case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
    return true;
  default:
    return false;
  }
}

static bool supportsWasm64(uint64_t Type) {
  switch (Type) {
  case wasm::R_WASM_MEMORY_ADDR_LEB64:
  case wasm::R_WASM_MEMORY_ADDR_SLEB64:
  case wasm::R_WASM_MEMORY_ADDR_I64:
  case wasm::R_WASM_TABLE_INDEX_SLEB64:
  case wasm::R_WASM_TABLE_INDEX_I64:
  case wasm::R_WASM_FUNCTION_OFFSET_I64:
    return true;
  default:
    return supportsWasm32(Type);
  }
}

static uint64_t resolveWasm32(uint64_t Type, uint64_t Offset, uint64_t S,
                              uint64_t LocData, int64_t /*Addend*/) {
  switch (Type) {
  case wasm::R_WASM_FUNCTION_INDEX_LEB:
  case wasm::R_WASM_TABLE_INDEX_SLEB:
  case wasm::R_WASM_TABLE_INDEX_I32:
  case wasm::R_WASM_MEMORY_ADDR_LEB:
  case wasm::R_WASM_MEMORY_ADDR_SLEB:
  case wasm::R_WASM_MEMORY_ADDR_I32:
  case wasm::R_WASM_TYPE_INDEX_LEB:
  case wasm::R_WASM_GLOBAL_INDEX_LEB:
  case wasm::R_WASM_FUNCTION_OFFSET_I32:
  case wasm::R_WASM_SECTION_OFFSET_I32:
  case wasm::R_WASM_TAG_INDEX_LEB:
  case wasm::R_WASM_GLOBAL_INDEX_I32:
  case wasm::R_WASM_TABLE_NUMBER_LEB:
  case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
    // For wasm section, its offset at 0 -- ignoring Value
    return LocData;
  default:
    llvm_unreachable("Invalid relocation type");
  }
}

static uint64_t resolveWasm64(uint64_t Type, uint64_t Offset, uint64_t S,
                              uint64_t LocData, int64_t Addend) {
  switch (Type) {
  case wasm::R_WASM_MEMORY_ADDR_LEB64:
  case wasm::R_WASM_MEMORY_ADDR_SLEB64:
  case wasm::R_WASM_MEMORY_ADDR_I64:
  case wasm::R_WASM_TABLE_INDEX_SLEB64:
  case wasm::R_WASM_TABLE_INDEX_I64:
  case wasm::R_WASM_FUNCTION_OFFSET_I64:
    // For wasm section, its offset at 0 -- ignoring Value
    return LocData;
  default:
    return resolveWasm32(Type, Offset, S, LocData, Addend);
  }
}

std::pair<SupportsRelocation, RelocationResolver>
getRelocationResolver(const ObjectFile &Obj) {
  if (Obj.isCOFF()) {
    switch (Obj.getArch()) {
    case Triple::x86_64:
      return {supportsCOFFX86_64, resolveCOFFX86_64};
    case Triple::x86:
      return {supportsCOFFX86, resolveCOFFX86};
    case Triple::arm:
    case Triple::thumb:
      return {supportsCOFFARM, resolveCOFFARM};
    case Triple::aarch64:
      return {supportsCOFFARM64, resolveCOFFARM64};
    default:
      return {nullptr, nullptr};
    }
  } else if (Obj.isELF()) {
    if (Obj.getBytesInAddress() == 8) {
      switch (Obj.getArch()) {
      case Triple::x86_64:
        return {supportsX86_64, resolveX86_64};
      case Triple::aarch64:
      case Triple::aarch64_be:
        return {supportsAArch64, resolveAArch64};
      case Triple::bpfel:
      case Triple::bpfeb:
        return {supportsBPF, resolveBPF};
      case Triple::loongarch64:
        return {supportsLoongArch, resolveLoongArch};
      case Triple::mips64el:
      case Triple::mips64:
        return {supportsMips64, resolveMips64};
      case Triple::ppc64le:
      case Triple::ppc64:
        return {supportsPPC64, resolvePPC64};
      case Triple::systemz:
        return {supportsSystemZ, resolveSystemZ};
      case Triple::sparcv9:
        return {supportsSparc64, resolveSparc64};
      case Triple::amdgcn:
        return {supportsAmdgpu, resolveAmdgpu};
      case Triple::riscv64:
        return {supportsRISCV, resolveRISCV};
      default:
        return {nullptr, nullptr};
      }
    }

    // 32-bit object file
    assert(Obj.getBytesInAddress() == 4 &&
           "Invalid word size in object file");

    switch (Obj.getArch()) {
    case Triple::x86:
      return {supportsX86, resolveX86};
    case Triple::ppcle:
    case Triple::ppc:
      return {supportsPPC32, resolvePPC32};
    case Triple::arm:
    case Triple::armeb:
      return {supportsARM, resolveARM};
    case Triple::avr:
      return {supportsAVR, resolveAVR};
    case Triple::lanai:
      return {supportsLanai, resolveLanai};
    case Triple::loongarch32:
      return {supportsLoongArch, resolveLoongArch};
    case Triple::mipsel:
    case Triple::mips:
      return {supportsMips32, resolveMips32};
    case Triple::msp430:
      return {supportsMSP430, resolveMSP430};
    case Triple::sparc:
      return {supportsSparc32, resolveSparc32};
    case Triple::hexagon:
      return {supportsHexagon, resolveHexagon};
    case Triple::riscv32:
      return {supportsRISCV, resolveRISCV};
    case Triple::csky:
      return {supportsCSKY, resolveCSKY};
    default:
      return {nullptr, nullptr};
    }
  } else if (Obj.isMachO()) {
    if (Obj.getArch() == Triple::x86_64)
      return {supportsMachOX86_64, resolveMachOX86_64};
    return {nullptr, nullptr};
  } else if (Obj.isWasm()) {
    if (Obj.getArch() == Triple::wasm32)
      return {supportsWasm32, resolveWasm32};
    if (Obj.getArch() == Triple::wasm64)
      return {supportsWasm64, resolveWasm64};
    return {nullptr, nullptr};
  }

  llvm_unreachable("Invalid object file");
}

uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R,
                           uint64_t S, uint64_t LocData) {
  if (const ObjectFile *Obj = R.getObject()) {
    int64_t Addend = 0;
    if (Obj->isELF()) {
      auto GetRelSectionType = [&]() -> unsigned {
        if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
          return Elf32LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
        if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
          return Elf64LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
        if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
          return Elf32BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
        auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj);
        return Elf64BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
      };

      if (GetRelSectionType() == ELF::SHT_RELA) {
        Addend = getELFAddend(R);
        // RISCV relocations use both LocData and Addend.
        if (Obj->getArch() != Triple::riscv32 &&
            Obj->getArch() != Triple::riscv64)
          LocData = 0;
      }
    }

    return Resolver(R.getType(), R.getOffset(), S, LocData, Addend);
  }

  // Sometimes the caller might want to use its own specific implementation of
  // the resolver function. E.g. this is used by LLD when it resolves debug
  // relocations and assumes that all of them have the same computation (S + A).
  // The relocation R has no owner object in this case and we don't need to
  // provide Type and Offset fields. It is also assumed the DataRefImpl.p
  // contains the addend, provided by the caller.
  return Resolver(/*Type=*/0, /*Offset=*/0, S, LocData,
                  R.getRawDataRefImpl().p);
}

} // namespace object
} // namespace llvm