summaryrefslogtreecommitdiff
path: root/lib/Target/Mips/MipsRelocator.cpp
blob: 8f1c5e2d66ee9634d6211d35ddbc8f973723bc90 (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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
//===- MipsRelocator.cpp  -----------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "MipsRelocator.h"
#include "MipsRelocationFunctions.h"

#include <mcld/IRBuilder.h>
#include <mcld/LinkerConfig.h>
#include <mcld/Object/ObjectBuilder.h>
#include <mcld/Support/MsgHandling.h>
#include <mcld/Target/OutputRelocSection.h>
#include <mcld/LD/ELFFileFormat.h>

#include <llvm/ADT/Twine.h>
#include <llvm/Support/ELF.h>

namespace llvm {
namespace ELF {

// FIXME: Consider upstream these relocation types to LLVM.
enum {
  R_MIPS_LA25_LUI = 200,
  R_MIPS_LA25_J   = 201,
  R_MIPS_LA25_ADD = 202,
  R_MIPS_PC32     = 248,
};

} // end namespace ELF
} // end namespace llvm

using namespace mcld;

//===----------------------------------------------------------------------===//
// MipsRelocationInfo
//===----------------------------------------------------------------------===//
class mcld::MipsRelocationInfo
{
public:
  static bool HasSubType(const Relocation& pParent, Relocation::Type pType)
  {
    if (llvm::ELF::R_MIPS_NONE == pType)
      return true;

    for (Relocation::Type type = pParent.type();
         llvm::ELF::R_MIPS_NONE != (type & 0xff); type >>= 8) {
      if ((type & 0xff) == pType)
        return true;
    }

    return false;
  }

  MipsRelocationInfo(Relocation& pParent, bool pIsRel)
    : m_Parent(&pParent),
      m_Type(pParent.type()),
      m_Addend(0),
      m_Symbol(pParent.symValue()),
      m_Result(pParent.target())
  {
    if (pIsRel && (type() < llvm::ELF::R_MIPS_LA25_LUI ||
                   type() > llvm::ELF::R_MIPS_LA25_ADD))
      m_Addend = pParent.target();
    else
      m_Addend = pParent.addend();
  }

  bool isNone() const
  {
    return llvm::ELF::R_MIPS_NONE == type();
  }

  bool isLast() const
  {
    return llvm::ELF::R_MIPS_NONE == (m_Type >> 8);
  }

  MipsRelocationInfo next() const
  {
    return MipsRelocationInfo(*m_Parent, m_Type >> 8, result(), result(), 0);
  }

  const Relocation& parent() const
  {
    return *m_Parent;
  }

  Relocation& parent()
  {
    return *m_Parent;
  }

  Relocation::Type type() const
  {
    return m_Type & 0xff;
  }

  Relocation::DWord A() const
  {
    return m_Addend;
  }

  Relocation::DWord S() const
  {
    return m_Symbol;
  }

  Relocation::DWord P() const
  {
    return parent().place();
  }

  Relocation::DWord result() const
  {
    return m_Result;
  }

  Relocation::DWord& result()
  {
    return m_Result;
  }

private:
  Relocation* m_Parent;
  Relocation::Type m_Type;
  Relocation::DWord m_Addend;
  Relocation::DWord m_Symbol;
  Relocation::DWord m_Result;

  MipsRelocationInfo(Relocation& pParent, Relocation::Type pType,
                     Relocation::DWord pResult,
                     Relocation::DWord pAddend, Relocation::DWord pSymbol)
    : m_Parent(&pParent),
      m_Type(pType),
      m_Addend(pAddend),
      m_Symbol(pSymbol),
      m_Result(pResult)
  {}

  bool isFirst() const {
    return m_Type == parent().type();
  }
};

//===----------------------------------------------------------------------===//
// Relocation Functions and Tables
//===----------------------------------------------------------------------===//
DECL_MIPS_APPLY_RELOC_FUNCS

/// the prototype of applying function
typedef Relocator::Result (*ApplyFunctionType)(MipsRelocationInfo&,
                                               MipsRelocator& pParent);


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

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

//===----------------------------------------------------------------------===//
// MipsRelocator
//===----------------------------------------------------------------------===//
MipsRelocator::MipsRelocator(MipsGNULDBackend& pParent,
                             const LinkerConfig& pConfig)
  : Relocator(pConfig),
    m_Target(pParent),
    m_pApplyingInput(NULL),
    m_CurrentLo16Reloc(NULL)
{
}

Relocator::Result
MipsRelocator::applyRelocation(Relocation& pReloc)
{
  // If m_CurrentLo16Reloc is not NULL we are processing
  // postponed relocation. Otherwise check relocation type
  // and postpone it for later handling.
  if (NULL == m_CurrentLo16Reloc && isPostponed(pReloc)) {
    postponeRelocation(pReloc);
    return OK;
  }

  for (MipsRelocationInfo info(pReloc, isRel());
       !info.isNone(); info = info.next()) {
    if (info.type() >= sizeof(ApplyFunctions) / sizeof(ApplyFunctions[0]))
      return Unknown;

    const ApplyFunctionTriple & triple = ApplyFunctions[info.type()];

    Result res = triple.func(info, *this);
    if (OK != res)
      return res;

    if (info.isLast()) {
      uint64_t mask = 0xFFFFFFFFFFFFFFFFULL >> (64 - triple.size);
      pReloc.target() &= ~mask;
      pReloc.target() |= info.result() & mask;
    }
  }

  return OK;
}

const char* MipsRelocator::getName(Relocation::Type pType) const
{
  return ApplyFunctions[pType & 0xff].name;
}

Relocator::Size MipsRelocator::getSize(Relocation::Type pType) const
{
  return ApplyFunctions[pType & 0xff].size;
}

void MipsRelocator::scanRelocation(Relocation& pReloc,
                                   IRBuilder& pBuilder,
                                   Module& pModule,
                                   LDSection& pSection,
                                   Input& pInput)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();
  assert(NULL != rsym && "ResolveInfo of relocation not set while scanRelocation");

  // Skip relocation against _gp_disp
  if (NULL != getTarget().getGpDispSymbol() &&
      rsym == getTarget().getGpDispSymbol()->resolveInfo())
    return;

  assert(NULL != pSection.getLink());
  if (0 == (pSection.getLink()->flag() & llvm::ELF::SHF_ALLOC))
    return;

  for (MipsRelocationInfo info(pReloc, isRel());
       !info.isNone(); info = info.next()) {
    // We test isLocal or if pInputSym is not a dynamic symbol
    // We assume -Bsymbolic to bind all symbols internaly via !rsym->isDyn()
    // Don't put undef symbols into local entries.
    if (isLocalReloc(*rsym))
      scanLocalReloc(info, pBuilder, pSection);
    else
      scanGlobalReloc(info, pBuilder, pSection);

    if (getTarget().needsLA25Stub(info.type(), info.parent().symInfo()))
      getTarget().addNonPICBranchSym(pReloc.symInfo());
  }

  // Check if we should issue undefined reference
  // for the relocation target symbol.
  if (rsym->isUndef() && !rsym->isDyn() && !rsym->isWeak() && !rsym->isNull())
    issueUndefRef(pReloc, pSection, pInput);
}

bool MipsRelocator::initializeScan(Input& pInput)
{
  if (LinkerConfig::Object != config().codeGenType())
    getTarget().getGOT().initializeScan(pInput);
  return true;
}

bool MipsRelocator::finalizeScan(Input& pInput)
{
  if (LinkerConfig::Object != config().codeGenType())
    getTarget().getGOT().finalizeScan(pInput);
  return true;
}

bool MipsRelocator::initializeApply(Input& pInput)
{
  m_pApplyingInput = &pInput;
  return true;
}

bool MipsRelocator::finalizeApply(Input& pInput)
{
  m_pApplyingInput = NULL;
  return true;
}

void MipsRelocator::scanLocalReloc(MipsRelocationInfo& pReloc,
                                   IRBuilder& pBuilder,
                                   const LDSection& pSection)
{
  ResolveInfo* rsym = pReloc.parent().symInfo();

  switch (pReloc.type()){
    case llvm::ELF::R_MIPS_NONE:
    case llvm::ELF::R_MIPS_16:
      break;
    case llvm::ELF::R_MIPS_32:
    case llvm::ELF::R_MIPS_64:
      if (LinkerConfig::DynObj == config().codeGenType()) {
        // TODO: (simon) The gold linker does not create an entry in .rel.dyn
        // section if the symbol section flags contains SHF_EXECINSTR.
        // 1. Find the reason of this condition.
        // 2. Check this condition here.
        getTarget().getRelDyn().reserveEntry();
        rsym->setReserved(rsym->reserved() | ReserveRel);
        getTarget().checkAndSetHasTextRel(*pSection.getLink());
      }
      break;
    case llvm::ELF::R_MIPS_REL32:
    case llvm::ELF::R_MIPS_26:
    case llvm::ELF::R_MIPS_HI16:
    case llvm::ELF::R_MIPS_LO16:
    case llvm::ELF::R_MIPS_PC16:
    case llvm::ELF::R_MIPS_SHIFT5:
    case llvm::ELF::R_MIPS_SHIFT6:
    case llvm::ELF::R_MIPS_SUB:
    case llvm::ELF::R_MIPS_INSERT_A:
    case llvm::ELF::R_MIPS_INSERT_B:
    case llvm::ELF::R_MIPS_DELETE:
    case llvm::ELF::R_MIPS_HIGHER:
    case llvm::ELF::R_MIPS_HIGHEST:
    case llvm::ELF::R_MIPS_SCN_DISP:
    case llvm::ELF::R_MIPS_REL16:
    case llvm::ELF::R_MIPS_ADD_IMMEDIATE:
    case llvm::ELF::R_MIPS_PJUMP:
    case llvm::ELF::R_MIPS_RELGOT:
    case llvm::ELF::R_MIPS_JALR:
    case llvm::ELF::R_MIPS_GLOB_DAT:
    case llvm::ELF::R_MIPS_COPY:
    case llvm::ELF::R_MIPS_JUMP_SLOT:
      break;
    case llvm::ELF::R_MIPS_GOT16:
    case llvm::ELF::R_MIPS_CALL16:
    case llvm::ELF::R_MIPS_GOT_HI16:
    case llvm::ELF::R_MIPS_CALL_HI16:
    case llvm::ELF::R_MIPS_GOT_LO16:
    case llvm::ELF::R_MIPS_CALL_LO16:
    case llvm::ELF::R_MIPS_GOT_DISP:
    case llvm::ELF::R_MIPS_GOT_PAGE:
    case llvm::ELF::R_MIPS_GOT_OFST:
      if (getTarget().getGOT().reserveLocalEntry(*rsym,
                                                 pReloc.type(), pReloc.A())) {
        if (getTarget().getGOT().hasMultipleGOT())
          getTarget().checkAndSetHasTextRel(*pSection.getLink());
      }
      break;
    case llvm::ELF::R_MIPS_GPREL32:
    case llvm::ELF::R_MIPS_GPREL16:
    case llvm::ELF::R_MIPS_LITERAL:
      break;
    case llvm::ELF::R_MIPS_TLS_DTPMOD32:
    case llvm::ELF::R_MIPS_TLS_DTPREL32:
    case llvm::ELF::R_MIPS_TLS_DTPMOD64:
    case llvm::ELF::R_MIPS_TLS_DTPREL64:
    case llvm::ELF::R_MIPS_TLS_GD:
    case llvm::ELF::R_MIPS_TLS_LDM:
    case llvm::ELF::R_MIPS_TLS_DTPREL_HI16:
    case llvm::ELF::R_MIPS_TLS_DTPREL_LO16:
    case llvm::ELF::R_MIPS_TLS_GOTTPREL:
    case llvm::ELF::R_MIPS_TLS_TPREL32:
    case llvm::ELF::R_MIPS_TLS_TPREL64:
    case llvm::ELF::R_MIPS_TLS_TPREL_HI16:
    case llvm::ELF::R_MIPS_TLS_TPREL_LO16:
      break;
    case llvm::ELF::R_MIPS_PC32:
      break;
    default:
      fatal(diag::unknown_relocation) << (int)pReloc.type() << rsym->name();
  }
}

void MipsRelocator::scanGlobalReloc(MipsRelocationInfo& pReloc,
                                    IRBuilder& pBuilder,
                                    const LDSection& pSection)
{
  ResolveInfo* rsym = pReloc.parent().symInfo();

  switch (pReloc.type()){
    case llvm::ELF::R_MIPS_NONE:
    case llvm::ELF::R_MIPS_INSERT_A:
    case llvm::ELF::R_MIPS_INSERT_B:
    case llvm::ELF::R_MIPS_DELETE:
    case llvm::ELF::R_MIPS_TLS_DTPMOD64:
    case llvm::ELF::R_MIPS_TLS_DTPREL64:
    case llvm::ELF::R_MIPS_REL16:
    case llvm::ELF::R_MIPS_ADD_IMMEDIATE:
    case llvm::ELF::R_MIPS_PJUMP:
    case llvm::ELF::R_MIPS_RELGOT:
    case llvm::ELF::R_MIPS_TLS_TPREL64:
      break;
    case llvm::ELF::R_MIPS_32:
    case llvm::ELF::R_MIPS_64:
    case llvm::ELF::R_MIPS_HI16:
    case llvm::ELF::R_MIPS_LO16:
      if (getTarget().symbolNeedsDynRel(*rsym, false, true)) {
        getTarget().getRelDyn().reserveEntry();
        if (getTarget().symbolNeedsCopyReloc(pReloc.parent(), *rsym)) {
          LDSymbol& cpySym = defineSymbolforCopyReloc(pBuilder, *rsym);
          addCopyReloc(*cpySym.resolveInfo());
        }
        else {
          // set Rel bit
          rsym->setReserved(rsym->reserved() | ReserveRel);
          getTarget().checkAndSetHasTextRel(*pSection.getLink());
        }
      }
      break;
    case llvm::ELF::R_MIPS_GOT16:
    case llvm::ELF::R_MIPS_CALL16:
    case llvm::ELF::R_MIPS_GOT_DISP:
    case llvm::ELF::R_MIPS_GOT_HI16:
    case llvm::ELF::R_MIPS_CALL_HI16:
    case llvm::ELF::R_MIPS_GOT_LO16:
    case llvm::ELF::R_MIPS_CALL_LO16:
    case llvm::ELF::R_MIPS_GOT_PAGE:
    case llvm::ELF::R_MIPS_GOT_OFST:
      if (getTarget().getGOT().reserveGlobalEntry(*rsym)) {
        if (getTarget().getGOT().hasMultipleGOT())
          getTarget().checkAndSetHasTextRel(*pSection.getLink());
      }
      break;
    case llvm::ELF::R_MIPS_LITERAL:
    case llvm::ELF::R_MIPS_GPREL32:
      fatal(diag::invalid_global_relocation) << (int)pReloc.type()
                                             << rsym->name();
      break;
    case llvm::ELF::R_MIPS_GPREL16:
      break;
    case llvm::ELF::R_MIPS_26:
      // Create a PLT entry if the symbol requires it and does not have it.
      if (getTarget().symbolNeedsPLT(*rsym) &&
          !(rsym->reserved() & ReservePLT)) {
        getTarget().getPLT().reserveEntry();
        getTarget().getGOTPLT().reserve();
        getTarget().getRelPLT().reserveEntry();
        rsym->setReserved(rsym->reserved() | ReservePLT);
      }
      break;
    case llvm::ELF::R_MIPS_PC16:
      break;
    case llvm::ELF::R_MIPS_16:
    case llvm::ELF::R_MIPS_SHIFT5:
    case llvm::ELF::R_MIPS_SHIFT6:
    case llvm::ELF::R_MIPS_SUB:
    case llvm::ELF::R_MIPS_HIGHER:
    case llvm::ELF::R_MIPS_HIGHEST:
    case llvm::ELF::R_MIPS_SCN_DISP:
      break;
    case llvm::ELF::R_MIPS_TLS_DTPREL32:
    case llvm::ELF::R_MIPS_TLS_GD:
    case llvm::ELF::R_MIPS_TLS_LDM:
    case llvm::ELF::R_MIPS_TLS_DTPREL_HI16:
    case llvm::ELF::R_MIPS_TLS_DTPREL_LO16:
    case llvm::ELF::R_MIPS_TLS_GOTTPREL:
    case llvm::ELF::R_MIPS_TLS_TPREL32:
    case llvm::ELF::R_MIPS_TLS_TPREL_HI16:
    case llvm::ELF::R_MIPS_TLS_TPREL_LO16:
      break;
    case llvm::ELF::R_MIPS_REL32:
    case llvm::ELF::R_MIPS_JALR:
    case llvm::ELF::R_MIPS_PC32:
      break;
    case llvm::ELF::R_MIPS_COPY:
    case llvm::ELF::R_MIPS_GLOB_DAT:
    case llvm::ELF::R_MIPS_JUMP_SLOT:
      fatal(diag::dynamic_relocation) << (int)pReloc.type();
      break;
    default:
      fatal(diag::unknown_relocation) << (int)pReloc.type() << rsym->name();
  }
}

bool MipsRelocator::isPostponed(const Relocation& pReloc) const
{
  if (MipsRelocationInfo::HasSubType(pReloc, llvm::ELF::R_MIPS_HI16))
    return true;

  if (MipsRelocationInfo::HasSubType(pReloc, llvm::ELF::R_MIPS_GOT16) &&
      pReloc.symInfo()->isLocal())
    return true;

  return false;
}

void MipsRelocator::addCopyReloc(ResolveInfo& pSym)
{
  Relocation& relEntry = *getTarget().getRelDyn().consumeEntry();
  relEntry.setType(llvm::ELF::R_MIPS_COPY);
  assert(pSym.outSymbol()->hasFragRef());
  relEntry.targetRef().assign(*pSym.outSymbol()->fragRef());
  relEntry.setSymInfo(&pSym);
}

LDSymbol& MipsRelocator::defineSymbolforCopyReloc(IRBuilder& pBuilder,
                                                  const ResolveInfo& pSym)
{
  // Get or create corresponding BSS LDSection
  ELFFileFormat* fileFormat = getTarget().getOutputFormat();
  LDSection* bssSectHdr =
    ResolveInfo::ThreadLocal == pSym.type() ? &fileFormat->getTBSS()
                                            : &fileFormat->getBSS();

  // Get or create corresponding BSS SectionData
  SectionData* bssData =
    bssSectHdr->hasSectionData() ? bssSectHdr->getSectionData()
                                 : IRBuilder::CreateSectionData(*bssSectHdr);

  // Determine the alignment by the symbol value
  // FIXME: here we use the largest alignment
  uint32_t addrAlign = config().targets().bitclass() / 8;

  // Allocate space in BSS for the copy symbol
  Fragment* frag = new FillFragment(0x0, 1, pSym.size());
  uint64_t size = ObjectBuilder::AppendFragment(*frag, *bssData, addrAlign);
  bssSectHdr->setSize(bssSectHdr->size() + size);

  // Change symbol binding to Global if it's a weak symbol
  ResolveInfo::Binding binding = (ResolveInfo::Binding)pSym.binding();
  if (binding == ResolveInfo::Weak)
    binding = ResolveInfo::Global;

  // Define the copy symbol in the bss section and resolve it
  LDSymbol* cpySym = pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
                      pSym.name(),
                      (ResolveInfo::Type)pSym.type(),
                      ResolveInfo::Define,
                      binding,
                      pSym.size(),  // size
                      0x0,          // value
                      FragmentRef::Create(*frag, 0x0),
                      (ResolveInfo::Visibility)pSym.other());

  // Output all other alias symbols if any
  Module::AliasList* alias_list = pBuilder.getModule().getAliasList(pSym);
  if (NULL == alias_list)
    return *cpySym;

  for (Module::alias_iterator it = alias_list->begin(), ie = alias_list->end();
       it != ie; ++it) {
    const ResolveInfo* alias = *it;
    if (alias == &pSym || !alias->isDyn())
      continue;

    pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
        alias->name(),
        (ResolveInfo::Type)alias->type(),
        ResolveInfo::Define,
        binding,
        alias->size(),  // size
        0x0,            // value
        FragmentRef::Create(*frag, 0x0),
        (ResolveInfo::Visibility)alias->other());
  }

  return *cpySym;
}

void MipsRelocator::postponeRelocation(Relocation& pReloc)
{
  ResolveInfo* rsym = pReloc.symInfo();
  m_PostponedRelocs[rsym].insert(&pReloc);
}

void MipsRelocator::applyPostponedRelocations(MipsRelocationInfo& pLo16Reloc)
{
  m_CurrentLo16Reloc = &pLo16Reloc;

  ResolveInfo* rsym = pLo16Reloc.parent().symInfo();

  RelocationSet & relocs = m_PostponedRelocs[rsym];
  for (RelocationSet::iterator it = relocs.begin(); it != relocs.end(); ++it)
    (*it)->apply(*this);

  m_PostponedRelocs.erase(rsym);

  m_CurrentLo16Reloc = NULL;
}

bool MipsRelocator::isGpDisp(const Relocation& pReloc) const
{
  return 0 == strcmp("_gp_disp", pReloc.symInfo()->name());
}

bool MipsRelocator::isRel() const
{
  return config().targets().is32Bits();
}

bool MipsRelocator::isLocalReloc(ResolveInfo& pSym) const
{
  if (pSym.isUndef())
    return false;

  return pSym.isLocal() ||
         !getTarget().isDynamicSymbol(pSym) ||
         !pSym.isDyn();
}

Relocator::Address MipsRelocator::getGPAddress()
{
  return getTarget().getGOT().getGPAddr(getApplyingInput());
}

Relocator::Address MipsRelocator::getGP0()
{
  return getTarget().getGP0(getApplyingInput());
}

Fragment& MipsRelocator::getLocalGOTEntry(MipsRelocationInfo& pReloc,
                                          Relocation::DWord entryValue)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.parent().symInfo();
  MipsGOT& got = getTarget().getGOT();

  assert(isLocalReloc(*rsym) &&
         "Attempt to get a global GOT entry for the local relocation");

  Fragment* got_entry = got.lookupLocalEntry(rsym, entryValue);

  // Found a mapping, then return the mapped entry immediately.
  if (NULL != got_entry)
    return *got_entry;

  // Not found.
  got_entry = got.consumeLocal();

  if (got.isPrimaryGOTConsumed())
    setupRelDynEntry(*FragmentRef::Create(*got_entry, 0), NULL);
  else
    got.setEntryValue(got_entry, entryValue);

  got.recordLocalEntry(rsym, entryValue, got_entry);

  return *got_entry;
}

Fragment& MipsRelocator::getGlobalGOTEntry(MipsRelocationInfo& pReloc)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.parent().symInfo();
  MipsGOT& got = getTarget().getGOT();

  assert(!isLocalReloc(*rsym) &&
         "Attempt to get a local GOT entry for the global relocation");

  Fragment* got_entry = got.lookupGlobalEntry(rsym);

  // Found a mapping, then return the mapped entry immediately.
  if (NULL != got_entry)
    return *got_entry;

  // Not found.
  got_entry = got.consumeGlobal();

  if (got.isPrimaryGOTConsumed())
    setupRelDynEntry(*FragmentRef::Create(*got_entry, 0), rsym);
  else
    got.setEntryValue(got_entry, pReloc.parent().symValue());

  got.recordGlobalEntry(rsym, got_entry);

  return *got_entry;
}

Relocator::Address MipsRelocator::getGOTOffset(MipsRelocationInfo& pReloc)
{
  ResolveInfo* rsym = pReloc.parent().symInfo();
  MipsGOT& got = getTarget().getGOT();

  if (isLocalReloc(*rsym)) {
    uint64_t value = pReloc.S();

    if (ResolveInfo::Section == rsym->type())
      value += pReloc.A();

    return got.getGPRelOffset(getApplyingInput(),
                              getLocalGOTEntry(pReloc, value));
  }
  else {
    return got.getGPRelOffset(getApplyingInput(), getGlobalGOTEntry(pReloc));
  }
}

void MipsRelocator::createDynRel(MipsRelocationInfo& pReloc)
{
  Relocator::DWord A = pReloc.A();
  Relocator::DWord S = pReloc.S();

  ResolveInfo* rsym = pReloc.parent().symInfo();

  if (isLocalReloc(*rsym)) {
    setupRelDynEntry(pReloc.parent().targetRef(), NULL);
    pReloc.result() = A + S;
  }
  else {
    setupRelDynEntry(pReloc.parent().targetRef(), rsym);
    // Don't add symbol value that will be resolved by the dynamic linker.
    pReloc.result() = A;
  }
}

uint64_t MipsRelocator::calcAHL(const MipsRelocationInfo& pHiReloc)
{
  assert(NULL != m_CurrentLo16Reloc &&
         "There is no saved R_MIPS_LO16 relocation");

  uint64_t AHI = pHiReloc.A() & 0xFFFF;
  uint64_t ALO = m_CurrentLo16Reloc->A() & 0xFFFF;
  uint64_t AHL = (AHI << 16) + int16_t(ALO);

  return AHL;
}

bool MipsRelocator::isN64ABI() const
{
  return config().targets().is64Bits();
}

uint64_t MipsRelocator::getPLTAddress(ResolveInfo& rsym)
{
  assert((rsym.reserved() & MipsRelocator::ReservePLT) &&
         "Symbol does not require a PLT entry");

  SymPLTMap::const_iterator it = m_SymPLTMap.find(&rsym);

  Fragment* plt;

  if (it != m_SymPLTMap.end()) {
    plt = it->second.first;
  }
  else {
    plt = getTarget().getPLT().consume();

    Fragment* got = getTarget().getGOTPLT().consume();
    Relocation* rel = getTarget().getRelPLT().consumeEntry();

    rel->setType(llvm::ELF::R_MIPS_JUMP_SLOT);
    rel->targetRef().assign(*got);
    rel->setSymInfo(&rsym);

    m_SymPLTMap[&rsym] = PLTDescriptor(plt, got);
  }

  return getTarget().getPLT().addr() + plt->getOffset();
}

//===----------------------------------------------------------------------===//
// Mips32Relocator
//===----------------------------------------------------------------------===//
Mips32Relocator::Mips32Relocator(Mips32GNULDBackend& pParent,
                                 const LinkerConfig& pConfig)
  : MipsRelocator(pParent, pConfig)
{}

void Mips32Relocator::setupRelDynEntry(FragmentRef& pFragRef, ResolveInfo* pSym)
{
  Relocation& relEntry = *getTarget().getRelDyn().consumeEntry();
  relEntry.setType(llvm::ELF::R_MIPS_REL32);
  relEntry.targetRef() = pFragRef;
  relEntry.setSymInfo(pSym);
}

//===----------------------------------------------------------------------===//
// Mips64Relocator
//===----------------------------------------------------------------------===//
Mips64Relocator::Mips64Relocator(Mips64GNULDBackend& pParent,
                                 const LinkerConfig& pConfig)
  : MipsRelocator(pParent, pConfig)
{}

void Mips64Relocator::setupRelDynEntry(FragmentRef& pFragRef, ResolveInfo* pSym)
{
  Relocation::Type type = llvm::ELF::R_MIPS_REL32 |
                          llvm::ELF::R_MIPS_64 << 8;
  // FIXME (simon): Fix dynamic relocations.
  type = llvm::ELF::R_MIPS_NONE;

  Relocation& relEntry = *getTarget().getRelDyn().consumeEntry();
  relEntry.setType(type);
  relEntry.targetRef() = pFragRef;
  relEntry.setSymInfo(pSym);
}

//=========================================//
// Relocation functions implementation     //
//=========================================//

// R_MIPS_NONE and those unsupported/deprecated relocation type
static
MipsRelocator::Result none(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  return Relocator::OK;
}

// R_MIPS_32: S + A
static
MipsRelocator::Result abs32(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  ResolveInfo* rsym = pReloc.parent().symInfo();

  Relocator::DWord A = pReloc.A();
  Relocator::DWord S = pReloc.S();

  LDSection& target_sect =
    pReloc.parent().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.result() = S + A;
    return Relocator::OK;
  }

  if (rsym->reserved() & MipsRelocator::ReserveRel) {
    pParent.createDynRel(pReloc);
    return Relocator::OK;
  }

  pReloc.result() = S + A;

  return Relocator::OK;
}

// R_MIPS_26:
//   local   : ((A | ((P + 4) & 0x3F000000)) + S) >> 2
//   external: (sign–extend(A) + S) >> 2
static
MipsRelocator::Result rel26(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  ResolveInfo* rsym = pReloc.parent().symInfo();

  int32_t A = ((pReloc.parent().target() & 0x03FFFFFF) << 2);
  int32_t P = pReloc.P();
  int32_t S = rsym->reserved() & MipsRelocator::ReservePLT
                  ? pParent.getPLTAddress(*rsym)
                  : pReloc.S();

  if (rsym->isLocal())
    pReloc.result() = A | ((P + 4) & 0x3F000000);
  else
    pReloc.result() = mcld::signExtend<28>(A);

  pReloc.result() = (pReloc.result() + S) >> 2;

  return Relocator::OK;
}

// R_MIPS_HI16:
//   local/external: ((AHL + S) - (short)(AHL + S)) >> 16
//   _gp_disp      : ((AHL + GP - P) - (short)(AHL + GP - P)) >> 16
static
MipsRelocator::Result hi16(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  uint64_t AHL = pParent.calcAHL(pReloc);

  if (pParent.isGpDisp(pReloc.parent())) {
    int32_t P = pReloc.P();
    int32_t GP = pParent.getGPAddress();
    pReloc.result() = ((AHL + GP - P) - (int16_t)(AHL + GP - P)) >> 16;
  }
  else {
    int32_t S = pReloc.S();
    if (pParent.isN64ABI())
      pReloc.result() = (pReloc.A() + S + 0x8000ull) >> 16;
    else
      pReloc.result() = ((AHL + S) - (int16_t)(AHL + S)) >> 16;
  }

  return Relocator::OK;
}

// R_MIPS_LO16:
//   local/external: AHL + S
//   _gp_disp      : AHL + GP - P + 4
static
MipsRelocator::Result lo16(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  // AHL is a combination of HI16 and LO16 addends. But R_MIPS_LO16
  // uses low 16 bits of the AHL. That is why we do not need R_MIPS_HI16
  // addend here.
  int32_t AHL = (pReloc.A() & 0xFFFF);

  if (pParent.isGpDisp(pReloc.parent())) {
    int32_t P = pReloc.P();
    int32_t GP = pParent.getGPAddress();
    pReloc.result() = AHL + GP - P + 4;
  }
  else {
    int32_t S = pReloc.S();
    pReloc.result() = AHL + S;
  }

  pParent.applyPostponedRelocations(pReloc);

  return Relocator::OK;
}

// R_MIPS_GPREL16:
//   external: sign–extend(A) + S - GP
//   local   : sign–extend(A) + S + GP0 – GP
static
MipsRelocator::Result gprel16(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  // Remember to add the section offset to A.
  uint64_t A = pReloc.A();
  uint64_t S = pReloc.S();
  uint64_t GP0 = pParent.getGP0();
  uint64_t GP = pParent.getGPAddress();

  ResolveInfo* rsym = pReloc.parent().symInfo();
  if (rsym->isLocal())
    pReloc.result() = A + S + GP0 - GP;
  else
    pReloc.result() = A + S - GP;

  return Relocator::OK;
}

// R_MIPS_GOT16:
//   local   : G (calculate AHL and put high 16 bit to GOT)
//   external: G
static
MipsRelocator::Result got16(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  if (pReloc.parent().symInfo()->isLocal()) {
    int32_t AHL = pParent.calcAHL(pReloc);
    int32_t S = pReloc.S();
    int32_t res = (AHL + S + 0x8000) & 0xFFFF0000;

    MipsGOT& got = pParent.getTarget().getGOT();

    Fragment& got_entry = pParent.getLocalGOTEntry(pReloc, res);

    pReloc.result() = got.getGPRelOffset(pParent.getApplyingInput(), got_entry);
  }
  else {
    pReloc.result() = pParent.getGOTOffset(pReloc);
  }

  return Relocator::OK;
}

// R_MIPS_GOTHI16:
//   external: (G - (short)G) >> 16 + A
static
MipsRelocator::Result gothi16(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  Relocator::Address G = pParent.getGOTOffset(pReloc);
  int32_t A = pReloc.A();

  pReloc.result() = (G - (int16_t)G) >> (16 + A);

  return Relocator::OK;
}

// R_MIPS_GOTLO16:
//   external: G & 0xffff
static
MipsRelocator::Result gotlo16(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  pReloc.result() = pParent.getGOTOffset(pReloc) & 0xffff;

  return Relocator::OK;
}

// R_MIPS_SUB:
//   external/local: S - A
static
MipsRelocator::Result sub(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  uint64_t S = pReloc.S();
  uint64_t A = pReloc.A();

  pReloc.result() = S - A;

  return Relocator::OK;
}

// R_MIPS_CALL16: G
static
MipsRelocator::Result call16(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  pReloc.result() = pParent.getGOTOffset(pReloc);

  return Relocator::OK;
}

// R_MIPS_GPREL32: A + S + GP0 - GP
static
MipsRelocator::Result gprel32(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  // Remember to add the section offset to A.
  uint64_t A = pReloc.A();
  uint64_t S = pReloc.S();
  uint64_t GP0 = pParent.getGP0();
  uint64_t GP = pParent.getGPAddress();

  pReloc.result() = A + S + GP0 - GP;

  return Relocator::OK;
}

// R_MIPS_64: S + A
static
MipsRelocator::Result abs64(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  // FIXME (simon): Consider to merge with abs32() or use the same function
  // but with another mask size.
  ResolveInfo* rsym = pReloc.parent().symInfo();

  Relocator::DWord A = pReloc.A();
  Relocator::DWord S = pReloc.S();

  LDSection& target_sect =
    pReloc.parent().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.result() = S + A;
    return Relocator::OK;
  }

  if (rsym->reserved() & MipsRelocator::ReserveRel) {
    pParent.createDynRel(pReloc);
    return Relocator::OK;
  }

  pReloc.result() = S + A;

  return Relocator::OK;
}

// R_MIPS_GOT_DISP / R_MIPS_GOT_PAGE: G
static
MipsRelocator::Result gotdisp(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  pReloc.result() = pParent.getGOTOffset(pReloc);

  return Relocator::OK;
}

// R_MIPS_GOT_OFST:
static
MipsRelocator::Result gotoff(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  // FIXME (simon): Needs to be implemented.
  return Relocator::OK;
}

// R_MIPS_JALR:
static
MipsRelocator::Result jalr(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  return Relocator::OK;
}

// R_MIPS_LA25_LUI
static
MipsRelocator::Result la25lui(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  int32_t S = pReloc.S();

  pReloc.result() = (S + 0x8000) >> 16;

  return Relocator::OK;
}

// R_MIPS_LA25_J
static
MipsRelocator::Result la25j(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  int32_t S = pReloc.S();

  pReloc.result() = S >> 2;

  return Relocator::OK;
}

// R_MIPS_LA25_ADD
static
MipsRelocator::Result la25add(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  pReloc.result() = pReloc.S();

  return Relocator::OK;
}

// R_MIPS_PC32:
static
MipsRelocator::Result pc32(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  return Relocator::OK;
}

static
MipsRelocator::Result unsupport(MipsRelocationInfo& pReloc, MipsRelocator& pParent)
{
  return Relocator::Unsupport;
}