summaryrefslogtreecommitdiff
path: root/lib/Target/ARM/ARMRelocator.cpp
blob: d613d6667936d6095f1590dcae6e98b0e2bfeef8 (plain)
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
//===- ARMRelocator.cpp  ----------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===--------------------------------------------------------------------===//

#include <llvm/ADT/Twine.h>
#include <llvm/Support/DataTypes.h>
#include <llvm/Support/ELF.h>
#include <llvm/Support/Host.h>
#include <mcld/Fragment/FragmentLinker.h>
#include <mcld/Support/MsgHandling.h>
#include "ARMRelocator.h"
#include "ARMRelocationFunctions.h"

using namespace mcld;

//===--------------------------------------------------------------------===//
// Relocation Functions and Tables
//===--------------------------------------------------------------------===//
DECL_ARM_APPLY_RELOC_FUNCS

/// the prototype of applying function
typedef Relocator::Result (*ApplyFunctionType)(Relocation& pReloc,
                                               ARMRelocator& pParent);

// the table entry of applying functions
struct ApplyFunctionTriple
{
  ApplyFunctionType func;
  unsigned int type;
  const char* name;
};

// declare the table of applying functions
static const ApplyFunctionTriple ApplyFunctions[] = {
  DECL_ARM_APPLY_RELOC_FUNC_PTRS
};

//===--------------------------------------------------------------------===//
// ARMRelocator
//===--------------------------------------------------------------------===//
ARMRelocator::ARMRelocator(ARMGNULDBackend& pParent)
  : Relocator(),
    m_Target(pParent) {
}

ARMRelocator::~ARMRelocator()
{
}

Relocator::Result
ARMRelocator::applyRelocation(Relocation& pRelocation)
{
  Relocation::Type type = pRelocation.type();
  if (type > 130) { // 131-255 doesn't noted in ARM spec
    return Relocator::Unknown;
  }

  return ApplyFunctions[type].func(pRelocation, *this);
}

const char* ARMRelocator::getName(Relocator::Type pType) const
{
  return ApplyFunctions[pType].name;
}

//===--------------------------------------------------------------------===//
// non-member functions
static Relocator::DWord getThumbBit(const Relocation& pReloc)
{
  // Set thumb bit if
  // - symbol has type of STT_FUNC, is defined and with bit 0 of its value set
  Relocator::DWord thumbBit =
       ((!pReloc.symInfo()->isUndef() || pReloc.symInfo()->isDyn()) &&
        (pReloc.symInfo()->type() == ResolveInfo::Function) &&
        ((pReloc.symValue() & 0x1) != 0))?
        1:0;
  return thumbBit;
}




//=========================================//
// Relocation helper function              //
//=========================================//

// Using uint64_t to make sure those complicate operations won't cause
// undefined behavior.
static
uint64_t helper_sign_extend(uint64_t pVal, uint64_t pOri_width)
{
  assert(pOri_width <= 64);
  uint64_t sign_bit = 1 << (pOri_width - 1);
  return (pVal ^ sign_bit) - sign_bit;
  // Reverse sign bit, then subtract sign bit.
}

static
uint64_t helper_bit_select(uint64_t pA, uint64_t pB, uint64_t pMask)
{
  return (pA & ~pMask) | (pB & pMask) ;
}

// Check if symbol can use relocation R_ARM_RELATIVE
static bool
helper_use_relative_reloc(const ResolveInfo& pSym,
                          const ARMRelocator& pFactory)
{
  // if symbol is dynamic or undefine or preemptible
  if (pSym.isDyn() ||
      pSym.isUndef() ||
      pFactory.getTarget().isSymbolPreemptible(pSym))
    return false;
  return true;
}

static
ARMGOTEntry& helper_get_GOT_and_init(Relocation& pReloc,
                                     ARMRelocator& pParent)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();
  ARMGNULDBackend& ld_backend = pParent.getTarget();

  ARMGOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*rsym);
  if (NULL == got_entry) {
    got_entry = ld_backend.getGOT().consumeGOT();
    pParent.getSymGOTMap().record(*rsym, *got_entry);
    // If we first get this GOT entry, we should initialize it.
    if (rsym->reserved() & ARMGNULDBackend::ReserveGOT) {
      // No corresponding dynamic relocation, initialize to the symbol value.
      got_entry->setValue(pReloc.symValue());
    }
    else if (rsym->reserved() & ARMGNULDBackend::GOTRel) {

      // Initialize corresponding dynamic relocation.
      Relocation& rel_entry = *ld_backend.getRelDyn().consumeEntry();
      if ( rsym->isLocal() ||
          helper_use_relative_reloc(*rsym, pParent)) {
        // Initialize got entry to target symbol address
        got_entry->setValue(pReloc.symValue());
        rel_entry.setType(llvm::ELF::R_ARM_RELATIVE);
        rel_entry.setSymInfo(0);
      }
      else {
        // Initialize got entry to 0 for corresponding dynamic relocation.
        got_entry->setValue(0);
        rel_entry.setType(llvm::ELF::R_ARM_GLOB_DAT);
        rel_entry.setSymInfo(rsym);
      }
      rel_entry.targetRef().assign(*got_entry);
    }
    else {
      fatal(diag::reserve_entry_number_mismatch_got);
    }
  }
  return *got_entry;
}

static
ARMRelocator::Address helper_GOT_ORG(ARMRelocator& pParent)
{
  return pParent.getTarget().getGOT().addr();
}


static
ARMRelocator::Address helper_GOT(Relocation& pReloc, ARMRelocator& pParent)
{
  ARMGOTEntry& got_entry = helper_get_GOT_and_init(pReloc, pParent);
  return helper_GOT_ORG(pParent) + got_entry.getOffset();
}


static
ARMPLT1& helper_get_PLT_and_init(Relocation& pReloc, ARMRelocator& pParent)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();
  ARMGNULDBackend& ld_backend = pParent.getTarget();

  ARMPLT1* plt_entry = pParent.getSymPLTMap().lookUp(*rsym);
  if (NULL != plt_entry)
    return *plt_entry;

  plt_entry = ld_backend.getPLT().consume();
  pParent.getSymPLTMap().record(*rsym, *plt_entry);

  // If we first get this PLT entry, we should initialize it.
  if (rsym->reserved() & ARMGNULDBackend::ReservePLT) {
    ARMGOTEntry* gotplt_entry = pParent.getSymGOTPLTMap().lookUp(*rsym);
    assert(NULL == gotplt_entry && "PLT entry not exist, but DynRel entry exist!");
    gotplt_entry = ld_backend.getGOT().consumeGOTPLT();
    pParent.getSymGOTPLTMap().record(*rsym, *gotplt_entry);

    // Initialize corresponding dynamic relocation.
    Relocation& rel_entry = *ld_backend.getRelPLT().consumeEntry();
    rel_entry.setType(llvm::ELF::R_ARM_JUMP_SLOT);
    rel_entry.targetRef().assign(*gotplt_entry);
    rel_entry.setSymInfo(rsym);
  }
  else {
    fatal(diag::reserve_entry_number_mismatch_plt);
  }

  return *plt_entry;
}

static
ARMRelocator::Address helper_PLT_ORG(ARMRelocator& pParent)
{
  return pParent.getTarget().getPLT().addr();
}


static
ARMRelocator::Address helper_PLT(Relocation& pReloc, ARMRelocator& pParent)
{
  ARMPLT1& plt_entry = helper_get_PLT_and_init(pReloc, pParent);
  return helper_PLT_ORG(pParent) + plt_entry.getOffset();
}

// Get an relocation entry in .rel.dyn and set its type to pType,
// its FragmentRef to pReloc->targetFrag() and its ResolveInfo to
// pReloc->symInfo()
static
void helper_DynRel(Relocation& pReloc,
                   ARMRelocator::Type pType,
                   ARMRelocator& pParent)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();
  ARMGNULDBackend& ld_backend = pParent.getTarget();

  Relocation& rel_entry = *ld_backend.getRelDyn().consumeEntry();
  rel_entry.setType(pType);
  rel_entry.targetRef() = pReloc.targetRef();

  if (pType == llvm::ELF::R_ARM_RELATIVE)
    rel_entry.setSymInfo(0);
  else
    rel_entry.setSymInfo(rsym);
}

static ARMRelocator::DWord
helper_extract_movw_movt_addend(ARMRelocator::DWord pTarget)
{
  // imm16: [19-16][11-0]
  return helper_sign_extend((((pTarget >> 4)) & 0xf000U) | (pTarget & 0xfffU),
                            16);
}

static ARMRelocator::DWord
helper_insert_val_movw_movt_inst(ARMRelocator::DWord pTarget,
                                 ARMRelocator::DWord pImm)
{
  // imm16: [19-16][11-0]
  pTarget &= 0xfff0f000U;
  pTarget |= pImm & 0x0fffU;
  pTarget |= (pImm & 0xf000U) << 4;
  return pTarget;
}

static ARMRelocator::DWord
helper_extract_thumb_movw_movt_addend(ARMRelocator::DWord pValue)
{
  // imm16: [19-16][26][14-12][7-0]
  return helper_sign_extend((((pValue >> 4) & 0xf000U) |
                             ((pValue >> 15) & 0x0800U) |
                             ((pValue >> 4) & 0x0700U) |
                             (pValue& 0x00ffU)),
                            16);
}

static ARMRelocator::DWord
helper_insert_val_thumb_movw_movt_inst(ARMRelocator::DWord pValue,
                                       ARMRelocator::DWord pImm)
{
  // imm16: [19-16][26][14-12][7-0]
  pValue &= 0xfbf08f00U;
  pValue |= (pImm & 0xf000U) << 4;
  pValue |= (pImm & 0x0800U) << 15;
  pValue |= (pImm & 0x0700U) << 4;
  pValue |= (pImm & 0x00ffU);
  return pValue;
}

static ARMRelocator::DWord
helper_thumb32_branch_offset(ARMRelocator::DWord pUpper16,
                             ARMRelocator::DWord pLower16)
{
  ARMRelocator::DWord s = (pUpper16 & (1U << 10)) >> 10,        // 26 bit
                           u  = pUpper16 & 0x3ffU,              // 25-16
                           l  = pLower16 & 0x7ffU,              // 10-0
                           j1 = (pLower16 & (1U << 13)) >> 13,  // 13
                           j2 = (pLower16 & (1U << 11)) >> 11;  // 11
  ARMRelocator::DWord i1 = j1 ^ s? 0: 1,
                              i2 = j2 ^ s? 0: 1;

  // [31-25][24][23][22][21-12][11-1][0]
  //      0   s  i1  i2      u     l  0
  return helper_sign_extend((s << 24) | (i1 << 23) | (i2 << 22) |
                            (u << 12) | (l << 1),
                            25);
}

static ARMRelocator::DWord
helper_thumb32_branch_upper(ARMRelocator::DWord pUpper16,
                            ARMRelocator::DWord pOffset)
{
  uint32_t sign = ((pOffset & 0x80000000U) >> 31);
  return (pUpper16 & ~0x7ffU) | ((pOffset >> 12) & 0x3ffU) | (sign << 10);
}

static ARMRelocator::DWord
helper_thumb32_branch_lower(ARMRelocator::DWord pLower16,
                            ARMRelocator::DWord pOffset)
{
  uint32_t sign = ((pOffset & 0x80000000U) >> 31);
  return ((pLower16 & ~0x2fffU) |
          ((((pOffset >> 23) & 1) ^ !sign) << 13) |
          ((((pOffset >> 22) & 1) ^ !sign) << 11) |
          ((pOffset >> 1) & 0x7ffU));
}

// Return true if overflow
static bool
helper_check_signed_overflow(ARMRelocator::DWord pValue,
                             unsigned bits)
{
  int32_t signed_val = static_cast<int32_t>(pValue);
  int32_t max = (1 << (bits - 1)) - 1;
  int32_t min = -(1 << (bits - 1));
  if (signed_val > max || signed_val < min) {
    return true;
  } else {
    return false;
  }
}


//=========================================//
// Each relocation function implementation //
//=========================================//

// R_ARM_NONE
ARMRelocator::Result none(Relocation& pReloc, ARMRelocator& pParent)
{
  return ARMRelocator::OK;
}

// R_ARM_ABS32: (S + A) | T
ARMRelocator::Result abs32(Relocation& pReloc, ARMRelocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  ARMRelocator::DWord T = getThumbBit(pReloc);
  ARMRelocator::DWord A = pReloc.target() + pReloc.addend();
  ARMRelocator::DWord S = pReloc.symValue();

  LDSection& target_sect = pReloc.targetRef().frag()->getParent()->getSection();

  // If the flag of target section is not ALLOC, we will not scan this relocation
  // but perform static relocation. (e.g., applying .debug section)
  if (0x0 == (llvm::ELF::SHF_ALLOC & target_sect.flag())) {
    pReloc.target() = (S + A) | T;
    return ARMRelocator::OK;
  }

  // A local symbol may need REL Type dynamic relocation
  if (rsym->isLocal() && (rsym->reserved() & ARMGNULDBackend::ReserveRel)) {
    helper_DynRel(pReloc, llvm::ELF::R_ARM_RELATIVE, pParent);
    pReloc.target() = (S + A) | T ;
    return ARMRelocator::OK;
  }

  // An external symbol may need PLT and dynamic relocation
  if (!rsym->isLocal()) {
    if (rsym->reserved() & ARMGNULDBackend::ReservePLT) {
      S = helper_PLT(pReloc, pParent);
      T = 0 ; // PLT is not thumb
    }
    // If we generate a dynamic relocation (except R_ARM_RELATIVE)
    // for a place, we should not perform static relocation on it
    // in order to keep the addend store in the place correct.
    if (rsym->reserved() & ARMGNULDBackend::ReserveRel) {
      if (helper_use_relative_reloc(*rsym, pParent)) {
        helper_DynRel(pReloc, llvm::ELF::R_ARM_RELATIVE, pParent);
      }
      else {
        helper_DynRel(pReloc, pReloc.type(), pParent);
        return ARMRelocator::OK;
      }
    }
  }


  // perform static relocation
  pReloc.target() = (S + A) | T;
  return ARMRelocator::OK;
}

// R_ARM_REL32: ((S + A) | T) - P
ARMRelocator::Result rel32(Relocation& pReloc, ARMRelocator& pParent)
{
  // perform static relocation
  ARMRelocator::Address S = pReloc.symValue();
  ARMRelocator::DWord   T = getThumbBit(pReloc);
  ARMRelocator::DWord   A = pReloc.target() + pReloc.addend();

  // An external symbol may need PLT (this reloc is from stub)
  if (!pReloc.symInfo()->isLocal()) {
    if (pReloc.symInfo()->reserved() & ARMGNULDBackend::ReservePLT) {
      S = helper_PLT(pReloc, pParent);
      T = 0;  // PLT is not thumb.
    }
  }

  // perform relocation
  pReloc.target() = ((S + A) | T) - pReloc.place();

  return ARMRelocator::OK;
}

// R_ARM_BASE_PREL: B(S) + A - P
ARMRelocator::Result base_prel(Relocation& pReloc, ARMRelocator& pParent)
{
  // perform static relocation
  ARMRelocator::DWord A = pReloc.target() + pReloc.addend();
  pReloc.target() = pReloc.symValue() + A - pReloc.place();
  return ARMRelocator::OK;
}

// R_ARM_GOTOFF32: ((S + A) | T) - GOT_ORG
ARMRelocator::Result gotoff32(Relocation& pReloc, ARMRelocator& pParent)
{
  ARMRelocator::DWord T = getThumbBit(pReloc);
  ARMRelocator::DWord A = pReloc.target() + pReloc.addend();
  ARMRelocator::Address GOT_ORG = helper_GOT_ORG(pParent);
  ARMRelocator::Address S = pReloc.symValue();

  pReloc.target() = ((S + A) | T) - GOT_ORG;
  return ARMRelocator::OK;
}

// R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
ARMRelocator::Result got_brel(Relocation& pReloc, ARMRelocator& pParent)
{
  if (!(pReloc.symInfo()->reserved() &
      (ARMGNULDBackend::ReserveGOT | ARMGNULDBackend::GOTRel))) {
    return ARMRelocator::BadReloc;
  }
  ARMRelocator::Address GOT_S   = helper_GOT(pReloc, pParent);
  ARMRelocator::DWord   A       = pReloc.target() + pReloc.addend();
  ARMRelocator::Address GOT_ORG = helper_GOT_ORG(pParent);
  // Apply relocation.
  pReloc.target() = GOT_S + A - GOT_ORG;
  return ARMRelocator::OK;
}

// R_ARM_GOT_PREL: GOT(S) + A - P
ARMRelocator::Result got_prel(Relocation& pReloc, ARMRelocator& pParent)
{
  if (!(pReloc.symInfo()->reserved() &
      (ARMGNULDBackend::ReserveGOT | ARMGNULDBackend::GOTRel))) {
    return ARMRelocator::BadReloc;
  }
  ARMRelocator::Address GOT_S   = helper_GOT(pReloc, pParent);
  ARMRelocator::DWord   A       = pReloc.target() + pReloc.addend();
  ARMRelocator::Address P = pReloc.place();

  // Apply relocation.
  pReloc.target() = GOT_S + A - P;
  return ARMRelocator::OK;
}

// R_ARM_PLT32: ((S + A) | T) - P
// R_ARM_JUMP24: ((S + A) | T) - P
// R_ARM_CALL: ((S + A) | T) - P
ARMRelocator::Result call(Relocation& pReloc, ARMRelocator& pParent)
{
  // If target is undefined weak symbol, we only need to jump to the
  // next instruction unless it has PLT entry. Rewrite instruction
  // to NOP.
  if (pReloc.symInfo()->isWeak() &&
      pReloc.symInfo()->isUndef() &&
      !pReloc.symInfo()->isDyn() &&
      !(pReloc.symInfo()->reserved() & ARMGNULDBackend::ReservePLT)) {
    // change target to NOP : mov r0, r0
    pReloc.target() = (pReloc.target() & 0xf0000000U) | 0x01a00000;
    return ARMRelocator::OK;
  }

  ARMRelocator::Address S; // S depends on PLT exists or not.
  ARMRelocator::DWord   T = getThumbBit(pReloc);
  ARMRelocator::DWord   A =
    helper_sign_extend((pReloc.target() & 0x00FFFFFFu) << 2, 26)
    + pReloc.addend();
  ARMRelocator::Address P = pReloc.place();

  S = pReloc.symValue();
  if (pReloc.symInfo()->reserved() & ARMGNULDBackend::ReservePLT) {
    S = helper_PLT(pReloc, pParent);
    T = 0;  // PLT is not thumb.
  }

  // At this moment (after relaxation), if the jump target is thumb instruction,
  // switch mode is needed, rewrite the instruction to BLX
  // FIXME: check if we can use BLX instruction (check from .ARM.attribute
  // CPU ARCH TAG, which should be ARMv5 or above)
  if (T != 0) {
    // cannot rewrite to blx for R_ARM_JUMP24
    if (pReloc.type() == llvm::ELF::R_ARM_JUMP24)
      return ARMRelocator::BadReloc;

    pReloc.target() = (pReloc.target() & 0xffffff) |
                      0xfa000000 |
                      (((S + A - P) & 2) << 23);
  }

  ARMRelocator::DWord X = ((S + A) | T) - P;
  // Check X is 24bit sign int. If not, we should use stub or PLT before apply.
  if (helper_check_signed_overflow(X, 26))
    return ARMRelocator::Overflow;
  //                    Make sure the Imm is 0.          Result Mask.
  pReloc.target() = (pReloc.target() & 0xFF000000u) | ((X & 0x03FFFFFEu) >> 2);
  return ARMRelocator::OK;
}

// R_ARM_THM_CALL: ((S + A) | T) - P
// R_ARM_THM_JUMP24: (((S + A) | T) - P)
ARMRelocator::Result thm_call(Relocation& pReloc, ARMRelocator& pParent)
{
  // If target is undefined weak symbol, we only need to jump to the
  // next instruction unless it has PLT entry. Rewrite instruction
  // to NOP.
  if (pReloc.symInfo()->isWeak() &&
      pReloc.symInfo()->isUndef() &&
      !pReloc.symInfo()->isDyn() &&
      !(pReloc.symInfo()->reserved() & ARMGNULDBackend::ReservePLT)) {
    pReloc.target() = (0xe000U << 16) | 0xbf00U;
    return ARMRelocator::OK;
  }

  // get lower and upper 16 bit instructions from relocation targetData
  uint16_t upper_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()));
  uint16_t lower_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1);

  ARMRelocator::DWord T = getThumbBit(pReloc);
  ARMRelocator::DWord A = helper_thumb32_branch_offset(upper_inst,
                                                               lower_inst);
  ARMRelocator::Address P = pReloc.place();
  ARMRelocator::Address S;

  // if symbol has plt
  if (pReloc.symInfo()->reserved() & ARMGNULDBackend::ReservePLT) {
    S = helper_PLT(pReloc, pParent);
    T = 0;  // PLT is not thumb.
  }
  else {
    S = pReloc.symValue();
  }

  S = S + A;

  // At this moment (after relaxation), if the jump target is arm
  // instruction, switch mode is needed, rewrite the instruction to BLX
  // FIXME: check if we can use BLX instruction (check from .ARM.attribute
  // CPU ARCH TAG, which should be ARMv5 or above)
  if (T == 0) {
    // cannot rewrite to blx for R_ARM_THM_JUMP24
    if (pReloc.type() == llvm::ELF::R_ARM_THM_JUMP24)
      return ARMRelocator::BadReloc;

    // for BLX, select bit 1 from relocation base address to jump target
    // address
    S = helper_bit_select(S, P, 0x2);
    // rewrite instruction to BLX
    lower_inst &= ~0x1000U;
  }
  else {
    // otherwise, the instruction should be BL
    lower_inst |= 0x1000U;
  }

  ARMRelocator::DWord X = (S | T) - P;

  // FIXME: Check bit size is 24(thumb2) or 22?
  if (helper_check_signed_overflow(X, 25)) {
    return ARMRelocator::Overflow;
  }

  upper_inst = helper_thumb32_branch_upper(upper_inst, X);
  lower_inst = helper_thumb32_branch_lower(lower_inst, X);

  *(reinterpret_cast<uint16_t*>(&pReloc.target())) = upper_inst;
  *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1) = lower_inst;

  return ARMRelocator::OK;
}

// R_ARM_MOVW_ABS_NC: (S + A) | T
ARMRelocator::Result movw_abs_nc(Relocation& pReloc, ARMRelocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  ARMRelocator::Address S = pReloc.symValue();
  ARMRelocator::DWord T = getThumbBit(pReloc);
  ARMRelocator::DWord A =
      helper_extract_movw_movt_addend(pReloc.target()) + pReloc.addend();
  ARMRelocator::DWord X;

  LDSection& target_sect = pReloc.targetRef().frag()->getParent()->getSection();

  // If the flag of target section is not ALLOC, we will not scan this
  // relocation but perform static relocation. (e.g., applying .debug section)
  if (0x0 != (llvm::ELF::SHF_ALLOC & target_sect.flag())) {
    // use plt
    if (rsym->reserved() & ARMGNULDBackend::ReservePLT) {
      S = helper_PLT(pReloc, pParent);
      T = 0 ; // PLT is not thumb
    }
  }

  // perform static relocation
  X = (S + A) | T;
  pReloc.target() = helper_insert_val_movw_movt_inst(
                                         pReloc.target() + pReloc.addend(), X);
  return ARMRelocator::OK;
}

// R_ARM_MOVW_PREL_NC: ((S + A) | T) - P
ARMRelocator::Result movw_prel_nc(Relocation& pReloc, ARMRelocator& pParent)
{
  ARMRelocator::Address S = pReloc.symValue();
  ARMRelocator::DWord T = getThumbBit(pReloc);
  ARMRelocator::DWord P = pReloc.place();
  ARMRelocator::DWord A =
      helper_extract_movw_movt_addend(pReloc.target()) + pReloc.addend();
  ARMRelocator::DWord X;

  X = ((S + A) | T) - P;

  if (helper_check_signed_overflow(X, 16)) {
    return ARMRelocator::Overflow;
  } else {
    pReloc.target() = helper_insert_val_movw_movt_inst(pReloc.target(), X);
    return ARMRelocator::OK;
  }
}

// R_ARM_MOVT_ABS: S + A
ARMRelocator::Result movt_abs(Relocation& pReloc, ARMRelocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  ARMRelocator::Address S = pReloc.symValue();
  ARMRelocator::DWord A =
    helper_extract_movw_movt_addend(pReloc.target()) + pReloc.addend();
  ARMRelocator::DWord X;

  LDSection& target_sect = pReloc.targetRef().frag()->getParent()->getSection();

  // If the flag of target section is not ALLOC, we will not scan this relocation
  // but perform static relocation. (e.g., applying .debug section)
  if (0x0 != (llvm::ELF::SHF_ALLOC & target_sect.flag())) {
    // use plt
    if (rsym->reserved() & ARMGNULDBackend::ReservePLT) {
      S = helper_PLT(pReloc, pParent);
    }
  }

  X = S + A;
  X >>= 16;
  // perform static relocation
  pReloc.target() = helper_insert_val_movw_movt_inst(pReloc.target(), X);
  return ARMRelocator::OK;
}

// R_ARM_MOVT_PREL: S + A - P
ARMRelocator::Result movt_prel(Relocation& pReloc, ARMRelocator& pParent)
{
  ARMRelocator::Address S = pReloc.symValue();
  ARMRelocator::DWord P = pReloc.place();
  ARMRelocator::DWord A =
            helper_extract_movw_movt_addend(pReloc.target()) + pReloc.addend();
  ARMRelocator::DWord X;

  X = S + A - P;
  X >>= 16;

  pReloc.target() = helper_insert_val_movw_movt_inst(pReloc.target(), X);
  return ARMRelocator::OK;
}

// R_ARM_THM_MOVW_ABS_NC: (S + A) | T
ARMRelocator::Result thm_movw_abs_nc(Relocation& pReloc, ARMRelocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  ARMRelocator::Address S = pReloc.symValue();
  ARMRelocator::DWord T = getThumbBit(pReloc);

  // get lower and upper 16 bit instructions from relocation targetData
  uint16_t upper_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()));
  uint16_t lower_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1);
  ARMRelocator::DWord val = ((upper_inst) << 16) | (lower_inst);
  ARMRelocator::DWord A =
      helper_extract_thumb_movw_movt_addend(val) + pReloc.addend();
  ARMRelocator::DWord X;

  LDSection& target_sect = pReloc.targetRef().frag()->getParent()->getSection();
  // If the flag of target section is not ALLOC, we will not scan this relocation
  // but perform static relocation. (e.g., applying .debug section)
  if (0x0 != (llvm::ELF::SHF_ALLOC & target_sect.flag())) {
    // use plt
    if (rsym->reserved() & ARMGNULDBackend::ReservePLT) {
      S = helper_PLT(pReloc, pParent);
      T = 0; // PLT is not thumb
    }
  }
  X = (S + A) | T;

  val = helper_insert_val_thumb_movw_movt_inst(val, X);
  *(reinterpret_cast<uint16_t*>(&pReloc.target())) = upper_inst;
  *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1) = lower_inst;

  return ARMRelocator::OK;
}

// R_ARM_THM_MOVW_PREL_NC: ((S + A) | T) - P
ARMRelocator::Result thm_movw_prel_nc(Relocation& pReloc, ARMRelocator& pParent)
{
  ARMRelocator::Address S = pReloc.symValue();
  ARMRelocator::DWord T = getThumbBit(pReloc);
  ARMRelocator::DWord P = pReloc.place();

  // get lower and upper 16 bit instructions from relocation targetData
  uint16_t upper_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()));
  uint16_t lower_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1);
  ARMRelocator::DWord val = ((upper_inst) << 16) | (lower_inst);
  ARMRelocator::DWord A =
      helper_extract_thumb_movw_movt_addend(val) + pReloc.addend();
  ARMRelocator::DWord X;

  X = ((S + A) | T) - P;

  val = helper_insert_val_thumb_movw_movt_inst(val, X);
  *(reinterpret_cast<uint16_t*>(&pReloc.target())) = upper_inst;
  *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1) = lower_inst;

  return ARMRelocator::OK;
}

// R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
// R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
ARMRelocator::Result thm_movw_brel(Relocation& pReloc, ARMRelocator& pParent)
{
  ARMRelocator::Address S = pReloc.symValue();
  ARMRelocator::DWord T = getThumbBit(pReloc);
  ARMRelocator::DWord P = pReloc.place();

  // get lower and upper 16 bit instructions from relocation targetData
  uint16_t upper_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()));
  uint16_t lower_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1);
  ARMRelocator::DWord val = ((upper_inst) << 16) | (lower_inst);
  ARMRelocator::DWord A =
      helper_extract_thumb_movw_movt_addend(val) + pReloc.addend();
  ARMRelocator::DWord X;

  X = ((S + A) | T) - P;

  val = helper_insert_val_thumb_movw_movt_inst(val, X);
  *(reinterpret_cast<uint16_t*>(&pReloc.target())) = upper_inst;
  *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1) = lower_inst;

  return ARMRelocator::OK;
}

// R_ARM_THM_MOVT_ABS: S + A
ARMRelocator::Result thm_movt_abs(Relocation& pReloc, ARMRelocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  ARMRelocator::Address S = pReloc.symValue();

  // get lower and upper 16 bit instructions from relocation targetData
  uint16_t upper_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()));
  uint16_t lower_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1);
  ARMRelocator::DWord val = ((upper_inst) << 16) | (lower_inst);
  ARMRelocator::DWord A =
      helper_extract_thumb_movw_movt_addend(val) + pReloc.addend();
  ARMRelocator::DWord X;

  LDSection& target_sect = pReloc.targetRef().frag()->getParent()->getSection();
  // If the flag of target section is not ALLOC, we will not scan this relocation
  // but perform static relocation. (e.g., applying .debug section)
  if (0x0 != (llvm::ELF::SHF_ALLOC & target_sect.flag())) {
    // use plt
    if (rsym->reserved() & ARMGNULDBackend::ReservePLT) {
      S = helper_PLT(pReloc, pParent);
    }
  }

  X = S + A;
  X >>= 16;

  // check 16-bit overflow
  if (helper_check_signed_overflow(X, 16)) {
    return ARMRelocator::Overflow;
  } else {
    val = helper_insert_val_thumb_movw_movt_inst(val, X);
    *(reinterpret_cast<uint16_t*>(&pReloc.target())) = upper_inst;
    *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1) = lower_inst;
    return ARMRelocator::OK;
  }
}

// R_ARM_THM_MOVT_PREL: S + A - P
// R_ARM_THM_MOVT_BREL: S + A - B(S)
ARMRelocator::Result thm_movt_prel(Relocation& pReloc, ARMRelocator& pParent)
{
  ARMRelocator::Address S = pReloc.symValue();
  ARMRelocator::DWord P = pReloc.place();

  // get lower and upper 16 bit instructions from relocation targetData
  uint16_t upper_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()));
  uint16_t lower_inst = *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1);
  ARMRelocator::DWord val = ((upper_inst) << 16) | (lower_inst);
  ARMRelocator::DWord A =
      helper_extract_thumb_movw_movt_addend(val) + pReloc.addend();
  ARMRelocator::DWord X;

  X = S + A - P;
  X >>= 16;

  val = helper_insert_val_thumb_movw_movt_inst(val, X);
  *(reinterpret_cast<uint16_t*>(&pReloc.target())) = upper_inst;
  *(reinterpret_cast<uint16_t*>(&pReloc.target()) + 1) = lower_inst;

  return ARMRelocator::OK;
}

// R_ARM_PREL31: ((S + A) | T) - P
ARMRelocator::Result prel31(Relocation& pReloc, ARMRelocator& pParent)
{
  ARMRelocator::DWord target = pReloc.target();
  ARMRelocator::DWord T = getThumbBit(pReloc);
  ARMRelocator::DWord A = helper_sign_extend(target, 31) +
                                  pReloc.addend();
  ARMRelocator::DWord P = pReloc.place();
  ARMRelocator::Address S;

  S = pReloc.symValue();
  // if symbol has plt
  if ( pReloc.symInfo()->reserved() & ARMGNULDBackend::ReservePLT) {
    S = helper_PLT(pReloc, pParent);
    T = 0;  // PLT is not thumb.
  }

  ARMRelocator::DWord X = ((S + A) | T) - P;
  pReloc.target() = helper_bit_select(target, X, 0x7fffffffU);
  if (helper_check_signed_overflow(X, 31))
    return ARMRelocator::Overflow;
  return ARMRelocator::OK;
}

// R_ARM_TLS_GD32: GOT(S) + A - P
// R_ARM_TLS_IE32: GOT(S) + A - P
// R_ARM_TLS_LE32: S + A - tp
ARMRelocator::Result tls(Relocation& pReloc, ARMRelocator& pParent)
{
  return ARMRelocator::Unsupport;
}

ARMRelocator::Result unsupport(Relocation& pReloc, ARMRelocator& pParent)
{
  return ARMRelocator::Unsupport;
}