summaryrefslogtreecommitdiff
path: root/lib/Target/X86/X86Relocator.cpp
blob: 6e3234fcc6666e205d649ac1237f9ecddd61f185 (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
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
//===- X86Relocator.cpp -------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "X86Relocator.h"
#include "X86RelocationFunctions.h"

#include <mcld/LinkerConfig.h>
#include <mcld/IRBuilder.h>
#include <mcld/Support/MsgHandling.h>
#include <mcld/LD/LDSymbol.h>
#include <mcld/LD/ELFFileFormat.h>
#include <mcld/LD/ELFSegmentFactory.h>
#include <mcld/LD/ELFSegment.h>
#include <mcld/Object/ObjectBuilder.h>

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

using namespace mcld;

//===--------------------------------------------------------------------===//
// X86_32 Relocation helper function
//===--------------------------------------------------------------------===//
/// helper_DynRel - Get an relocation entry in .rel.dyn
static
Relocation& helper_DynRel_init(ResolveInfo* pSym,
                               Fragment& pFrag,
                               uint64_t pOffset,
                               X86Relocator::Type pType,
                               X86_32Relocator& pParent)
{
  X86_32GNULDBackend& ld_backend = pParent.getTarget();
  Relocation& rel_entry = *ld_backend.getRelDyn().create();
  rel_entry.setType(pType);
  rel_entry.targetRef().assign(pFrag, pOffset);
  if (pType == llvm::ELF::R_386_RELATIVE || NULL == pSym)
    rel_entry.setSymInfo(NULL);
  else
    rel_entry.setSymInfo(pSym);

  return rel_entry;
}

/// helper_use_relative_reloc - Check if symbol ceuse relocation
/// R_386_RELATIVE
static bool
helper_use_relative_reloc(const ResolveInfo& pSym,
                          const X86_32Relocator& pFactory)

{
  // if symbol is dynamic or undefine or preemptible
  if (pSym.isDyn() ||
      pSym.isUndef() ||
      pFactory.getTarget().isSymbolPreemptible(pSym))
    return false;
  return true;
}

static
X86_32GOTEntry& helper_GOT_init(Relocation& pReloc,
                                bool pHasRel,
					                      X86_32Relocator& pParent)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();
  X86_32GNULDBackend& ld_backend = pParent.getTarget();
  assert(NULL == pParent.getSymGOTMap().lookUp(*rsym));

  X86_32GOTEntry* got_entry = ld_backend.getGOT().create();
  pParent.getSymGOTMap().record(*rsym, *got_entry);

  if (!pHasRel) {
    // No corresponding dynamic relocation, initialize to the symbol value.
    got_entry->setValue(X86Relocator::SymVal);
  }
  else {
    // Initialize got_entry content and the corresponding dynamic relocation.
    if (helper_use_relative_reloc(*rsym, pParent)) {
      helper_DynRel_init(rsym, *got_entry, 0x0, llvm::ELF::R_386_RELATIVE,
                                                                       pParent);
      got_entry->setValue(X86Relocator::SymVal);
    }
    else {
      helper_DynRel_init(rsym, *got_entry, 0x0, llvm::ELF::R_386_GLOB_DAT,
                                                                       pParent);
      got_entry->setValue(0x0);
    }
  }
  return *got_entry;
}

static
X86Relocator::Address helper_GOT_ORG(X86_32Relocator& pParent)
{
  return pParent.getTarget().getGOTPLT().addr();
}

static
X86Relocator::Address helper_get_GOT_address(Relocation& pReloc,
                                             X86_32Relocator& pParent)
{
  X86_32GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*pReloc.symInfo());
  assert(NULL != got_entry);
  return pParent.getTarget().getGOT().addr() + got_entry->getOffset();
}

static
PLTEntryBase& helper_PLT_init(Relocation& pReloc, X86_32Relocator& pParent)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();
  X86_32GNULDBackend& ld_backend = pParent.getTarget();
  assert(NULL == pParent.getSymPLTMap().lookUp(*rsym));

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

  // initialize plt and the corresponding gotplt and dyn rel entry.
  assert(NULL == pParent.getSymGOTPLTMap().lookUp(*rsym) &&
         "PLT entry not exist, but GOTPLT entry exist!");
  X86_32GOTEntry* gotplt_entry = ld_backend.getGOTPLT().create();
  pParent.getSymGOTPLTMap().record(*rsym, *gotplt_entry);

  // init the corresponding rel entry in .rel.plt
  Relocation& rel_entry = *ld_backend.getRelPLT().create();
  rel_entry.setType(llvm::ELF::R_386_JUMP_SLOT);
  rel_entry.targetRef().assign(*gotplt_entry);
  rel_entry.setSymInfo(rsym);
  return *plt_entry;
}

static
X86Relocator::Address helper_get_PLT_address(ResolveInfo& pSym, X86_32Relocator& pParent)
{
  PLTEntryBase* plt_entry = pParent.getSymPLTMap().lookUp(pSym);
  assert(NULL != plt_entry);
  return pParent.getTarget().getPLT().addr() + plt_entry->getOffset();
}

//===--------------------------------------------------------------------===//
// X86_32 Relocation Functions and Tables
//===--------------------------------------------------------------------===//
DECL_X86_32_APPLY_RELOC_FUNCS

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

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

// declare the table of applying functions
static const X86_32ApplyFunctionTriple X86_32ApplyFunctions[] = {
  DECL_X86_32_APPLY_RELOC_FUNC_PTRS
};

//===--------------------------------------------------------------------===//
// X86Relocator
//===--------------------------------------------------------------------===//
X86Relocator::X86Relocator(const LinkerConfig& pConfig)
  : Relocator(pConfig) {
}

X86Relocator::~X86Relocator()
{
}

void X86Relocator::scanRelocation(Relocation& pReloc,
                                  IRBuilder& pLinker,
                                  Module& pModule,
                                  LDSection& pSection,
                                  Input& pInput)
{
  if (LinkerConfig::Object == config().codeGenType())
    return;
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();
  assert(NULL != rsym &&
         "ResolveInfo of relocation not set while scanRelocation");

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

  // Scan relocation type to determine if the GOT/PLT/Dynamic Relocation
  // entries should be created.
  if (rsym->isLocal()) // rsym is local
    scanLocalReloc(pReloc, pLinker, pModule, pSection);
  else // rsym is external
    scanGlobalReloc(pReloc, pLinker, pModule, pSection);

  // 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);
}

void X86Relocator::addCopyReloc(ResolveInfo& pSym, X86GNULDBackend& pTarget)
{
  Relocation& rel_entry = *pTarget.getRelDyn().create();
  rel_entry.setType(pTarget.getCopyRelType());
  assert(pSym.outSymbol()->hasFragRef());
  rel_entry.targetRef().assign(*pSym.outSymbol()->fragRef());
  rel_entry.setSymInfo(&pSym);
}

/// defineSymbolforCopyReloc
/// For a symbol needing copy relocation, define a copy symbol in the BSS
/// section and all other reference to this symbol should refer to this
/// copy.
/// @note This is executed at `scan relocation' stage.
LDSymbol& X86Relocator::defineSymbolforCopyReloc(IRBuilder& pBuilder,
                                                 const ResolveInfo& pSym,
                                                 X86GNULDBackend& pTarget)
{
  // get or create corresponding BSS LDSection
  LDSection* bss_sect_hdr = NULL;
  ELFFileFormat* file_format = pTarget.getOutputFormat();
  if (ResolveInfo::ThreadLocal == pSym.type())
    bss_sect_hdr = &file_format->getTBSS();
  else
    bss_sect_hdr = &file_format->getBSS();

  // get or create corresponding BSS SectionData
  assert(NULL != bss_sect_hdr);
  SectionData* bss_section = NULL;
  if (bss_sect_hdr->hasSectionData())
    bss_section = bss_sect_hdr->getSectionData();
  else
    bss_section = IRBuilder::CreateSectionData(*bss_sect_hdr);

  // 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,
                                                *bss_section,
                                                addralign);
  bss_sect_hdr->setSize(bss_sect_hdr->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* cpy_sym = 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 &pModule = pBuilder.getModule();
  Module::AliasList* alias_list = pModule.getAliasList(pSym);
  if (NULL!=alias_list) {
    Module::alias_iterator it, it_e=alias_list->end();
    for (it=alias_list->begin(); it!=it_e; ++it) {
      const ResolveInfo* alias = *it;
      if (alias!=&pSym && alias->isDyn()) {
        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 *cpy_sym;
}

//===--------------------------------------------------------------------===//
// X86_32Relocator
//===--------------------------------------------------------------------===//
X86_32Relocator::X86_32Relocator(X86_32GNULDBackend& pParent,
                                 const LinkerConfig& pConfig)
  : X86Relocator(pConfig), m_Target(pParent) {
}

Relocator::Result
X86_32Relocator::applyRelocation(Relocation& pRelocation)
{
  Relocation::Type type = pRelocation.type();

  if (type >= sizeof (X86_32ApplyFunctions) / sizeof (X86_32ApplyFunctions[0]) ) {
    return Unknown;
  }

  // apply the relocation
  return X86_32ApplyFunctions[type].func(pRelocation, *this);
}

const char* X86_32Relocator::getName(Relocation::Type pType) const
{
  return X86_32ApplyFunctions[pType].name;
}

Relocator::Size X86_32Relocator::getSize(Relocation::Type pType) const
{
  return X86_32ApplyFunctions[pType].size;;
}

void X86_32Relocator::scanLocalReloc(Relocation& pReloc,
                                     IRBuilder& pBuilder,
                                     Module& pModule,
                                     LDSection& pSection)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();

  switch(pReloc.type()){

    case llvm::ELF::R_386_32:
      // If buiding PIC object (shared library or PIC executable),
      // a dynamic relocations with RELATIVE type to this location is needed.
      // Reserve an entry in .rel.dyn
      if (config().isCodeIndep()) {
        // set Rel bit
        rsym->setReserved(rsym->reserved() | ReserveRel);
        getTarget().checkAndSetHasTextRel(*pSection.getLink());
        // set up the dyn rel directly
        helper_DynRel_init(rsym,
                           *pReloc.targetRef().frag(),
                           pReloc.targetRef().offset(),
                           llvm::ELF::R_386_RELATIVE,
                           *this);
      }
      return;

    case llvm::ELF::R_386_16:
    case llvm::ELF::R_386_8:
      // If buiding PIC object (shared library or PIC executable),
      // a dynamic relocations with RELATIVE type to this location is needed.
      // Reserve an entry in .rel.dyn
      if (config().isCodeIndep()) {
        // set up the dyn rel directly
        helper_DynRel_init(rsym, *pReloc.targetRef().frag(),
                            pReloc.targetRef().offset(), pReloc.type(), *this);
        // set Rel bit
        rsym->setReserved(rsym->reserved() | ReserveRel);
        getTarget().checkAndSetHasTextRel(*pSection.getLink());
      }
      return;

    case llvm::ELF::R_386_PLT32:
      return;

    case llvm::ELF::R_386_GOTOFF:
    case llvm::ELF::R_386_GOTPC:
      // FIXME: A GOT section is needed
      return;

    case llvm::ELF::R_386_GOT32:
      // Symbol needs GOT entry, reserve entry in .got
      // return if we already create GOT for this symbol
      if (rsym->reserved() & ReserveGOT)
        return;

      // FIXME: check STT_GNU_IFUNC symbol

      // If building shared object or the symbol is undefined, a dynamic
      // relocation is needed to relocate this GOT entry. Reserve an
      // entry in .rel.dyn
      if (LinkerConfig::DynObj ==
                   config().codeGenType() || rsym->isUndef() || rsym->isDyn()) {
        helper_GOT_init(pReloc, true, *this);
        // set GOT bit
        rsym->setReserved(rsym->reserved() | ReserveGOT);
        return;
      }

      // elsewhere if the GOT is used in statically linked binaries,
      // the GOT entry is enough and no relocation is needed.
      helper_GOT_init(pReloc, false, *this);
      rsym->setReserved(rsym->reserved() | ReserveGOT);
      return;

    case llvm::ELF::R_386_PC32:
    case llvm::ELF::R_386_PC16:
    case llvm::ELF::R_386_PC8:
      return;

    case llvm::ELF::R_386_TLS_GD: {
      // FIXME: no linker optimization for TLS relocation
      if (rsym->reserved() & ReserveGOT)
        return;

      // define the section symbol for .tdata or .tbss
      // the target symbol of the created dynamic relocation should be the
      // section symbol of the section which this symbol defined. so we
      // need to define that section symbol here
      ELFFileFormat* file_format = getTarget().getOutputFormat();
      const LDSection* sym_sect =
               &rsym->outSymbol()->fragRef()->frag()->getParent()->getSection();
      LDSymbol* sect_sym = NULL;
      if (&file_format->getTData() == sym_sect) {
        if (!getTarget().hasTDATASymbol()) {
          sect_sym = pModule.getSectionSymbolSet().get(*sym_sect);
          getTarget().setTDATASymbol(*sect_sym);
        }
      }
      else if (&file_format->getTBSS() == sym_sect || rsym->isCommon()) {
        if (!getTarget().hasTBSSSymbol()) {
          sect_sym = pModule.getSectionSymbolSet().get(*sym_sect);
          getTarget().setTBSSSymbol(*sect_sym);
        }
      }
      else
        error(diag::invalid_tls) << rsym->name() << sym_sect->name();

      // set up a pair of got entries and a dyn rel
      // set GOT bit
      rsym->setReserved(rsym->reserved() | ReserveGOT);
      X86_32GOTEntry* got_entry1 = getTarget().getGOT().create();
      X86_32GOTEntry* got_entry2 = getTarget().getGOT().create();
      getSymGOTMap().record(*rsym, *got_entry1, *got_entry2);
      // set up value of got entries, the value of got_entry2 should be the
      // symbol value, which has to be set during apply relocation
      got_entry1->setValue(0x0);

      // setup dyn rel for got_entry1
      Relocation& rel_entry1 = helper_DynRel_init(rsym, *got_entry1, 0x0,
                                        llvm::ELF::R_386_TLS_DTPMOD32, *this);
      // for local tls symbol, add rel entry against the section symbol this
      // symbol belong to (.tdata or .tbss)
      rel_entry1.setSymInfo(sect_sym->resolveInfo());
      return;
    }
    case llvm::ELF::R_386_TLS_LDM:
      getTLSModuleID();
      return;

    case llvm::ELF::R_386_TLS_LDO_32:
      return;

    case llvm::ELF::R_386_TLS_IE: {
      getTarget().setHasStaticTLS();

      // if building shared object, a RELATIVE dynamic relocation is needed
      if (LinkerConfig::DynObj == config().codeGenType()) {
         helper_DynRel_init(rsym, *pReloc.targetRef().frag(),
                                              pReloc.targetRef().offset(),
                                              llvm::ELF::R_386_RELATIVE, *this);
        rsym->setReserved(rsym->reserved() | ReserveRel);
        getTarget().checkAndSetHasTextRel(*pSection.getLink());
      }
      else {
        // for local sym, we can convert ie to le if not building shared object
        convertTLSIEtoLE(pReloc, pSection);
        return;
      }

      if (rsym->reserved() & ReserveGOT)
        return;

      // set up the got and the corresponding rel entry
      X86_32GOTEntry* got_entry = getTarget().getGOT().create();
      getSymGOTMap().record(*rsym, *got_entry);
      got_entry->setValue(0x0);
      helper_DynRel_init(rsym, *got_entry, 0x0, llvm::ELF::R_386_TLS_TPOFF,
                                                                         *this);
      // set GOT bit
      rsym->setReserved(rsym->reserved() | ReserveGOT);
      // add symbol to dyn sym table
      getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
      return;
    }

    case llvm::ELF::R_386_TLS_GOTIE: {
      getTarget().setHasStaticTLS();
      if (rsym->reserved() & ReserveGOT)
        return;
      // set up the got and the corresponding dyn rel
      X86_32GOTEntry* got_entry = getTarget().getGOT().create();
      getSymGOTMap().record(*rsym, *got_entry);
      got_entry->setValue(0x0);
      helper_DynRel_init(rsym, *got_entry, 0x0, llvm::ELF::R_386_TLS_TPOFF,
                                                                        *this);
      // set GOT bit
      rsym->setReserved(rsym->reserved() | ReserveGOT);
      getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
      return;
    }

    case llvm::ELF::R_386_TLS_LE:
    case llvm::ELF::R_386_TLS_LE_32:
      getTarget().setHasStaticTLS();
      // if buildint shared object, a dynamic relocation is needed
      if (LinkerConfig::DynObj == config().codeGenType()) {
        helper_DynRel_init(rsym,
                           *pReloc.targetRef().frag(),
                           pReloc.targetRef().offset(),
                           llvm::ELF::R_386_TLS_TPOFF,
                           *this);
        rsym->setReserved(rsym->reserved() | ReserveRel);
        getTarget().checkAndSetHasTextRel(*pSection.getLink());
        // the target symbol of the dynamic relocation is rsym, so we need to
        // emit it into .dynsym
        assert(NULL != rsym->outSymbol());
        getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
      }
      return;

    default:
      fatal(diag::unsupported_relocation) << (int)pReloc.type()
                                          << "mclinker@googlegroups.com";
      break;
  } // end switch
}

void X86_32Relocator::scanGlobalReloc(Relocation& pReloc,
                                      IRBuilder& pBuilder,
                                      Module& pModule,
                                      LDSection& pSection)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();

  switch(pReloc.type()) {
    case llvm::ELF::R_386_32:
    case llvm::ELF::R_386_16:
    case llvm::ELF::R_386_8:
      // Absolute relocation type, symbol may needs PLT entry or
      // dynamic relocation entry
      if (getTarget().symbolNeedsPLT(*rsym)) {
        // create plt for this symbol if it does not have one
        if (!(rsym->reserved() & ReservePLT)){
          // Symbol needs PLT entry, we need a PLT entry
          // and the corresponding GOT and dynamic relocation entry
          // in .got and .rel.plt.
          helper_PLT_init(pReloc, *this);
          // set PLT bit
          rsym->setReserved(rsym->reserved() | ReservePLT);
        }
      }

      if (getTarget().symbolNeedsDynRel(*rsym, (rsym->reserved() & ReservePLT),
                                                                        true)) {
        // symbol needs dynamic relocation entry, set up the dynrel entry
        if (getTarget().symbolNeedsCopyReloc(pReloc, *rsym)) {
          LDSymbol& cpy_sym = defineSymbolforCopyReloc(pBuilder, *rsym, getTarget());
          addCopyReloc(*cpy_sym.resolveInfo(), getTarget());
        }
        else {
          // set Rel bit and the dyn rel
          rsym->setReserved(rsym->reserved() | ReserveRel);
          getTarget().checkAndSetHasTextRel(*pSection.getLink());
          if (llvm::ELF::R_386_32 == pReloc.type() &&
              helper_use_relative_reloc(*rsym, *this))
            helper_DynRel_init(rsym,
                               *pReloc.targetRef().frag(),
                               pReloc.targetRef().offset(),
                               llvm::ELF::R_386_RELATIVE,
                               *this);
          else
            helper_DynRel_init(rsym,
                               *pReloc.targetRef().frag(),
                               pReloc.targetRef().offset(),
                               pReloc.type(),
                               *this);
        }
      }
      return;

    case llvm::ELF::R_386_GOTOFF:
    case llvm::ELF::R_386_GOTPC: {
      // FIXME: A GOT section is needed
      return;
    }

    case llvm::ELF::R_386_PLT32:
      // A PLT entry is needed when building shared library

      // return if we already create plt for this symbol
      if (rsym->reserved() & ReservePLT)
        return;

      // if the symbol's value can be decided at link time, then no need plt
      if (getTarget().symbolFinalValueIsKnown(*rsym))
        return;

      // if symbol is defined in the ouput file and it's not
      // preemptible, no need plt
      if (rsym->isDefine() && !rsym->isDyn() &&
          !getTarget().isSymbolPreemptible(*rsym))
        return;

      // Symbol needs PLT entry, we need a PLT entry
      // and the corresponding GOT and dynamic relocation entry
      // in .got and .rel.plt
      helper_PLT_init(pReloc, *this);
      // set PLT bit
      rsym->setReserved(rsym->reserved() | ReservePLT);
      return;

    case llvm::ELF::R_386_GOT32:
      // Symbol needs GOT entry, reserve entry in .got
      // return if we already create GOT for this symbol
      if (rsym->reserved() & ReserveGOT)
        return;
      // If building shared object or the symbol is undefined, a dynamic
      // relocation is needed to relocate this GOT entry. Reserve an
      // entry in .rel.dyn
      if (LinkerConfig::DynObj ==
                   config().codeGenType() || rsym->isUndef() || rsym->isDyn())
        helper_GOT_init(pReloc, true, *this);
      else
        helper_GOT_init(pReloc, false, *this);
      // set GOT bit
      rsym->setReserved(rsym->reserved() | ReserveGOT);
      return;

    case llvm::ELF::R_386_PC32:
    case llvm::ELF::R_386_PC16:
    case llvm::ELF::R_386_PC8:

      if (getTarget().symbolNeedsPLT(*rsym) &&
          LinkerConfig::DynObj != config().codeGenType()) {
        // create plt for this symbol if it does not have one
        if (!(rsym->reserved() & ReservePLT)){
          // Symbol needs PLT entry, we need a PLT entry
          // and the corresponding GOT and dynamic relocation entry
          // in .got and .rel.plt.
          // set PLT bit
          helper_PLT_init(pReloc, *this);
          rsym->setReserved(rsym->reserved() | ReservePLT);
        }
      }

      if (getTarget().symbolNeedsDynRel(*rsym, (rsym->reserved() & ReservePLT),
                                                                       false)) {
        // symbol needs dynamic relocation entry, setup an entry in .rel.dyn
        if (getTarget().symbolNeedsCopyReloc(pReloc, *rsym)) {
          LDSymbol& cpy_sym = defineSymbolforCopyReloc(pBuilder, *rsym,
                                                                   getTarget());
          addCopyReloc(*cpy_sym.resolveInfo(), getTarget());
        }
        else {
          // set Rel bit and the dyn rel
          rsym->setReserved(rsym->reserved() | ReserveRel);
          getTarget().checkAndSetHasTextRel(*pSection.getLink());
          if (llvm::ELF::R_386_32 == pReloc.type() &&
              helper_use_relative_reloc(*rsym, *this))
            helper_DynRel_init(rsym,
                               *pReloc.targetRef().frag(),
                               pReloc.targetRef().offset(),
                               llvm::ELF::R_386_RELATIVE,
                               *this);
          else
            helper_DynRel_init(rsym,
                               *pReloc.targetRef().frag(),
                               pReloc.targetRef().offset(),
                               pReloc.type(),
                               *this);
        }
      }
      return;

    case llvm::ELF::R_386_TLS_GD: {
      // FIXME: no linker optimization for TLS relocation
      if (rsym->reserved() & ReserveGOT)
        return;

      // set up a pair of got entries and a pair of dyn rel
      X86_32GOTEntry* got_entry1 = getTarget().getGOT().create();
      X86_32GOTEntry* got_entry2 = getTarget().getGOT().create();
      getSymGOTMap().record(*rsym, *got_entry1, *got_entry2);
      got_entry1->setValue(0x0);
      got_entry2->setValue(0x0);
      // setup dyn rel for got entries against rsym
      helper_DynRel_init(rsym, *got_entry1, 0x0,
                                        llvm::ELF::R_386_TLS_DTPMOD32, *this);
      helper_DynRel_init(rsym, *got_entry2, 0x0,
                                        llvm::ELF::R_386_TLS_DTPOFF32, *this);

      // add the rsym to dynamic symbol table
      getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
      // set GOT bit
      rsym->setReserved(rsym->reserved() | ReserveGOT);
      return;
    }

    case llvm::ELF::R_386_TLS_LDM:
      getTLSModuleID();
      return;

    case llvm::ELF::R_386_TLS_LDO_32:
      return;

    case llvm::ELF::R_386_TLS_IE: {
      getTarget().setHasStaticTLS();
      // if buildint shared object, a RELATIVE dynamic relocation is needed
      if (LinkerConfig::DynObj == config().codeGenType()) {
        helper_DynRel_init(rsym, *pReloc.targetRef().frag(),
                                               pReloc.targetRef().offset(),
                                               llvm::ELF::R_386_RELATIVE, *this);
        rsym->setReserved(rsym->reserved() | ReserveRel);
        getTarget().checkAndSetHasTextRel(*pSection.getLink());
      } else {
        // for global sym, we can convert ie to le if its final value is known
        if (getTarget().symbolFinalValueIsKnown(*rsym)) {
          convertTLSIEtoLE(pReloc, pSection);
          return;
        }
      }
      if (rsym->reserved() & ReserveGOT)
        return;
      // set up the got and the corresponding rel entry
      X86_32GOTEntry* got_entry = getTarget().getGOT().create();
      getSymGOTMap().record(*rsym, *got_entry);
      got_entry->setValue(0x0);
      helper_DynRel_init(rsym, *got_entry, 0x0, llvm::ELF::R_386_TLS_TPOFF,
                                                                         *this);
      // set GOT bit
      rsym->setReserved(rsym->reserved() | ReserveGOT);
      return;
    }

    case llvm::ELF::R_386_TLS_GOTIE: {
      getTarget().setHasStaticTLS();
      if (rsym->reserved() & ReserveGOT)
        return;
      // set up the got and the corresponding dyn rel
      X86_32GOTEntry* got_entry = getTarget().getGOT().create();
      getSymGOTMap().record(*rsym, *got_entry);
      got_entry->setValue(0x0);
      helper_DynRel_init(rsym, *got_entry, 0x0, llvm::ELF::R_386_TLS_TPOFF,
                                                                        *this);
      getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
      // set GOT bit
      rsym->setReserved(rsym->reserved() | ReserveGOT);
      return;
    }

    case llvm::ELF::R_386_TLS_LE:
    case llvm::ELF::R_386_TLS_LE_32:
      getTarget().setHasStaticTLS();
      // if buildint shared object, a dynamic relocation is needed
      if (LinkerConfig::DynObj == config().codeGenType()) {
        helper_DynRel_init(rsym,
                           *pReloc.targetRef().frag(),
                           pReloc.targetRef().offset(),
                           llvm::ELF::R_386_TLS_TPOFF,
                           *this);
        getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
        rsym->setReserved(rsym->reserved() | ReserveRel);
        getTarget().checkAndSetHasTextRel(*pSection.getLink());
      }
      return;

    default: {
      fatal(diag::unsupported_relocation) << (int)pReloc.type()
                                          << "mclinker@googlegroups.com";
      break;
    }
  } // end switch
}

// Create a GOT entry for the TLS module index
X86_32GOTEntry& X86_32Relocator::getTLSModuleID()
{
  static X86_32GOTEntry* got_entry = NULL;
  if (NULL != got_entry)
    return *got_entry;

  // Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
  got_entry = getTarget().getGOT().create();
  getTarget().getGOT().create()->setValue(0x0);

  helper_DynRel_init(NULL, *got_entry, 0x0, llvm::ELF::R_386_TLS_DTPMOD32,
                                                                          *this);
  return *got_entry;
}

/// convert R_386_TLS_IE to R_386_TLS_LE
void X86_32Relocator::convertTLSIEtoLE(Relocation& pReloc,
                                       LDSection& pSection)
{
  assert(pReloc.type() == llvm::ELF::R_386_TLS_IE);
  assert(NULL != pReloc.targetRef().frag());

  // 1. create the fragment references and new relocs
  uint64_t off = pReloc.targetRef().offset();
  if (off >= 4)
    off -= 4;
  else
    off = 0;

  FragmentRef* fragref = FragmentRef::Create(*pReloc.targetRef().frag(), off);
  Relocation* reloc = Relocation::Create(X86_32Relocator::R_386_TLS_OPT,
                                         *fragref,
                                         0x0);
  // FIXME: should we create a special symbol for the tls opt instead?
  reloc->setSymInfo(pReloc.symInfo());

  // 2. modify the opcodes to the appropriate ones
  uint8_t* op =  (reinterpret_cast<uint8_t*>(&reloc->target()));
  off = pReloc.targetRef().offset() - reloc->targetRef().offset() - 1;
  if (op[off] == 0xa1) {
    op[off] = 0xb8;
  } else {
    switch (op[off - 1]) {
      case 0x8b:
        assert((op[off] & 0xc7) == 0x05);
        op[off - 1] = 0xc7;
        op[off]     = 0xc0 | ((op[off] >> 3) & 7);
        break;
      case 0x03:
        assert((op[off] & 0xc7) == 0x05);
        op[off - 1] = 0x81;
        op[off]     = 0xc0 | ((op[off] >> 3) & 7);
        break;
      default:
        assert(0);
        break;
    }
  }

  // 3. insert the new relocs "BEFORE" the original reloc.
  pSection.getRelocData()->getRelocationList().insert(
    RelocData::iterator(pReloc), reloc);

  // 4. change the type of the original reloc
  pReloc.setType(llvm::ELF::R_386_TLS_LE);
}

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

// R_386_NONE
X86Relocator::Result none(Relocation& pReloc, X86_32Relocator& pParent)
{
  return X86Relocator::OK;
}

// R_386_32: S + A
// R_386_16
// R_386_8
X86Relocator::Result abs(Relocation& pReloc, X86_32Relocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  Relocator::DWord A = pReloc.target() + pReloc.addend();
  Relocator::DWord S = pReloc.symValue();
  bool has_dyn_rel = pParent.getTarget().symbolNeedsDynRel(
                                  *rsym,
                                  (rsym->reserved() & X86Relocator::ReservePLT),
                                  true);


  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;
    return X86Relocator::OK;
  }

  // An external symbol may need PLT and dynamic relocation
  if (!rsym->isLocal()) {
    if (rsym->reserved() & X86Relocator::ReservePLT) {
      S = helper_get_PLT_address(*rsym, pParent);
    }
    // If we generate a dynamic relocation (except R_386_RELATIVE)
    // for a place, we should not perform static relocation on it
    // in order to keep the addend store in the place correct.
    if (has_dyn_rel)
      if (llvm::ELF::R_386_32 != pReloc.type() ||
          (!helper_use_relative_reloc(*rsym, pParent)))
        return X86Relocator::OK;
  }

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

// R_386_PC32: S + A - P
// R_386_PC16
// R_386_PC8
X86Relocator::Result rel(Relocation& pReloc, X86_32Relocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  Relocator::DWord A = pReloc.target() + pReloc.addend();
  Relocator::DWord S = pReloc.symValue();
  Relocator::DWord P = pReloc.place();
  bool has_dyn_rel = pParent.getTarget().symbolNeedsDynRel(
                                  *rsym,
                                  (rsym->reserved() & X86Relocator::ReservePLT),
                                  true);

  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 - P;
    return X86Relocator::OK;
  }

  // An external symbol may need PLT and dynamic relocation
  if (!rsym->isLocal()) {
    if (rsym->reserved() & X86Relocator::ReservePLT) {
      S = helper_get_PLT_address(*rsym, pParent);
      pReloc.target() = S + A - P;
    }
    if (has_dyn_rel)
      if (!helper_use_relative_reloc(*rsym, pParent))
        return X86Relocator::OK;
  }

   // perform static relocation
  pReloc.target() = S + A - P;
  return X86Relocator::OK;
}

// R_386_GOTOFF: S + A - GOT_ORG
X86Relocator::Result gotoff32(Relocation& pReloc, X86_32Relocator& pParent)
{
  Relocator::DWord      A = pReloc.target() + pReloc.addend();
  X86Relocator::Address GOT_ORG = helper_GOT_ORG(pParent);
  X86Relocator::Address S = pReloc.symValue();

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

// R_386_GOTPC: GOT_ORG + A - P
X86Relocator::Result gotpc32(Relocation& pReloc, X86_32Relocator& pParent)
{
  Relocator::DWord      A       = pReloc.target() + pReloc.addend();
  X86Relocator::Address GOT_ORG = helper_GOT_ORG(pParent);
  // Apply relocation.
  pReloc.target() = GOT_ORG + A - pReloc.place();
  return X86Relocator::OK;
}

// R_386_GOT32: GOT(S) + A - GOT_ORG
X86Relocator::Result got32(Relocation& pReloc, X86_32Relocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  if (!(rsym->reserved() & (X86Relocator::ReserveGOT)))
    return X86Relocator::BadReloc;

  // set up got entry value if the got has no dyn rel or
  // the dyn rel is RELATIVE
  X86_32GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*pReloc.symInfo());
  assert(NULL != got_entry);
  if (got_entry->getValue() == X86Relocator::SymVal)
    got_entry->setValue(pReloc.symValue());

  X86Relocator::Address GOT_S   = helper_get_GOT_address(pReloc, pParent);
  Relocator::DWord      A       = pReloc.target() + pReloc.addend();
  X86Relocator::Address GOT_ORG = helper_GOT_ORG(pParent);
  // Apply relocation.
  pReloc.target() = GOT_S + A - GOT_ORG;
  return X86Relocator::OK;
}

// R_386_PLT32: PLT(S) + A - P
X86Relocator::Result plt32(Relocation& pReloc, X86_32Relocator& pParent)
{
  // PLT_S depends on if there is a PLT entry.
  X86Relocator::Address PLT_S;
  if ((pReloc.symInfo()->reserved() & X86Relocator::ReservePLT))
    PLT_S = helper_get_PLT_address(*pReloc.symInfo(), pParent);
  else
    PLT_S = pReloc.symValue();
  Relocator::DWord      A = pReloc.target() + pReloc.addend();
  X86Relocator::Address P = pReloc.place();
  pReloc.target() = PLT_S + A - P;
  return X86Relocator::OK;
}

// R_386_TLS_GD:
X86Relocator::Result tls_gd(Relocation& pReloc, X86_32Relocator& pParent)
{
  // global-dynamic
  ResolveInfo* rsym = pReloc.symInfo();
  // must reserve two pairs of got and dynamic relocation
  if (!(rsym->reserved() & X86Relocator::ReserveGOT))
    return X86Relocator::BadReloc;

  ELFFileFormat* file_format = pParent.getTarget().getOutputFormat();
  // setup corresponding got and dynamic relocatio entries:
  // get first got entry, if there is already a got entry for rsym, then apply
  // this relocation to the got entry directly. If not, setup the corresponding
  // got and dyn relocation entries
  X86_32GOTEntry* got_entry1 = pParent.getSymGOTMap().lookUpFirstEntry(*rsym);

  // set the got_entry2 value to symbol value
  if (rsym->isLocal())
     pParent.getSymGOTMap().lookUpSecondEntry(*rsym)->setValue(pReloc.symValue());

  // perform relocation to the first got entry
  Relocator::DWord A = pReloc.target() + pReloc.addend();
  // GOT_OFF - the offset between the got_entry1 and _GLOBAL_OFFSET_TABLE (the
  // .got.plt section)
  X86Relocator::Address GOT_OFF =
     file_format->getGOT().addr() +
     got_entry1->getOffset() -
     file_format->getGOTPLT().addr();
  pReloc.target() = GOT_OFF + A;
  return X86Relocator::OK;
}

// R_386_TLS_LDM
X86Relocator::Result tls_ldm(Relocation& pReloc, X86_32Relocator& pParent)
{
  // FIXME: no linker optimization for TLS relocation
  const X86_32GOTEntry& got_entry = pParent.getTLSModuleID();

  // All GOT offsets are relative to the end of the GOT.
  X86Relocator::SWord GOT_S = got_entry.getOffset() -
                                      (pParent.getTarget().getGOTPLT().addr() -
                                       pParent.getTarget().getGOT().addr());
  Relocator::DWord A = pReloc.target() + pReloc.addend();
  pReloc.target() = GOT_S + A;

  return X86Relocator::OK;
}

// R_386_TLS_LDO_32
X86Relocator::Result tls_ldo_32(Relocation& pReloc, X86_32Relocator& pParent)
{
  // FIXME: no linker optimization for TLS relocation
  Relocator::DWord A = pReloc.target() + pReloc.addend();
  X86Relocator::Address S = pReloc.symValue();
  pReloc.target() = S + A;
  return X86Relocator::OK;
}

// R_X86_TLS_IE
X86Relocator::Result tls_ie(Relocation& pReloc, X86_32Relocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  if (!(rsym->reserved() & X86Relocator::ReserveGOT)) {
     return X86Relocator::BadReloc;
  }

  // set up the got and dynamic relocation entries if not exist
  X86_32GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*rsym);
  assert(NULL != got_entry);
  // perform relocation to the absolute address of got_entry
  X86Relocator::Address GOT_S =
                 pParent.getTarget().getGOT().addr() + got_entry->getOffset();

  Relocator::DWord A = pReloc.target() + pReloc.addend();
  pReloc.target() = GOT_S + A;

  return X86Relocator::OK;
}

// R_386_TLS_GOTIE
X86Relocator::Result tls_gotie(Relocation& pReloc, X86_32Relocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  if (!(rsym->reserved() & X86Relocator::ReserveGOT)) {
     return X86Relocator::BadReloc;
  }

  // set up the got and dynamic relocation entries if not exist
  X86_32GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*rsym);
  assert(NULL != got_entry);

  // All GOT offsets are relative to the end of the GOT.
  X86Relocator::SWord GOT_S = got_entry->getOffset() -
    (pParent.getTarget().getGOTPLT().addr() - pParent.getTarget().getGOT().addr());
  Relocator::DWord A = pReloc.target() + pReloc.addend();
  pReloc.target() = GOT_S + A;

  return X86Relocator::OK;
}

// R_X86_TLS_LE
X86Relocator::Result tls_le(Relocation& pReloc, X86_32Relocator& pParent)
{
  if (pReloc.symInfo()->reserved() & X86Relocator::ReserveRel)
    return X86Relocator::OK;

  // perform static relocation
  // get TLS segment
  ELFSegmentFactory::const_iterator tls_seg =
    pParent.getTarget().elfSegmentTable().find(llvm::ELF::PT_TLS,
                                               llvm::ELF::PF_R,
                                               0x0);
  assert(tls_seg != pParent.getTarget().elfSegmentTable().end());
  Relocator::DWord A = pReloc.target() + pReloc.addend();
  X86Relocator::Address S = pReloc.symValue();
  pReloc.target() = S + A - (*tls_seg)->memsz();
  return X86Relocator::OK;
}

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

//===--------------------------------------------------------------------===//
// X86_64 Relocation helper function
//===--------------------------------------------------------------------===//
/// helper_DynRel - Get an relocation entry in .rela.dyn
static
Relocation& helper_DynRel_init(ResolveInfo* pSym,
                               Fragment& pFrag,
                               uint64_t pOffset,
                               X86Relocator::Type pType,
                               X86_64Relocator& pParent)
{
  X86_64GNULDBackend& ld_backend = pParent.getTarget();
  Relocation& rel_entry = *ld_backend.getRelDyn().create();
  rel_entry.setType(pType);
  rel_entry.targetRef().assign(pFrag, pOffset);
  if (pType == llvm::ELF::R_X86_64_RELATIVE || NULL == pSym)
    rel_entry.setSymInfo(NULL);
  else
    rel_entry.setSymInfo(pSym);

  return rel_entry;
}


/// helper_use_relative_reloc - Check if symbol can use relocation
/// R_X86_64_RELATIVE
static bool
helper_use_relative_reloc(const ResolveInfo& pSym,
                          const X86_64Relocator& pFactory)

{
  // if symbol is dynamic or undefine or preemptible
  if (pSym.isDyn() ||
      pSym.isUndef() ||
      pFactory.getTarget().isSymbolPreemptible(pSym))
    return false;
  return true;
}

static
X86_64GOTEntry& helper_GOT_init(Relocation& pReloc,
                                bool pHasRel,
                                X86_64Relocator& pParent)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();
  X86_64GNULDBackend& ld_backend = pParent.getTarget();
  assert(NULL == pParent.getSymGOTMap().lookUp(*rsym));

  X86_64GOTEntry* got_entry = ld_backend.getGOT().create();
  pParent.getSymGOTMap().record(*rsym, *got_entry);

  // If we first get this GOT entry, we should initialize it.
  if (!pHasRel) {
    // No corresponding dynamic relocation, initialize to the symbol value.
    got_entry->setValue(X86Relocator::SymVal);
  }
  else {
    // Initialize got_entry content and the corresponding dynamic relocation.
    if (helper_use_relative_reloc(*rsym, pParent)) {
       Relocation& rel_entry = helper_DynRel_init(rsym, *got_entry, 0x0,
                                         llvm::ELF::R_X86_64_RELATIVE, pParent);
       rel_entry.setAddend(X86Relocator::SymVal);
       pParent.getRelRelMap().record(pReloc, rel_entry);
    }
    else {
       helper_DynRel_init(rsym, *got_entry, 0x0, llvm::ELF::R_X86_64_GLOB_DAT,
                                                                       pParent);
    }
    got_entry->setValue(0);
  }
  return *got_entry;
}

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

static
X86Relocator::Address helper_get_GOT_address(Relocation& pReloc,
                                             X86_64Relocator& pParent)
{
  X86_64GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*pReloc.symInfo());
  assert(NULL != got_entry);
  return got_entry->getOffset();
}

static
X86Relocator::Address helper_get_PLT_address(ResolveInfo& pSym,
                                             X86_64Relocator& pParent)
{
  PLTEntryBase* plt_entry = pParent.getSymPLTMap().lookUp(pSym);
  assert(NULL != plt_entry);
  return pParent.getTarget().getPLT().addr() + plt_entry->getOffset();
}

static
PLTEntryBase& helper_PLT_init(Relocation& pReloc, X86_64Relocator& pParent)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();
  X86_64GNULDBackend& ld_backend = pParent.getTarget();
  assert(NULL == pParent.getSymPLTMap().lookUp(*rsym));

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

  // initialize plt and the corresponding gotplt and dyn rel entry.
  assert(NULL == pParent.getSymGOTPLTMap().lookUp(*rsym) &&
         "PLT entry not exist, but DynRel entry exist!");
  X86_64GOTEntry* gotplt_entry = ld_backend.getGOTPLT().create();
  pParent.getSymGOTPLTMap().record(*rsym, *gotplt_entry);

  // init the corresponding rel entry in .rel.plt
  Relocation& rel_entry = *ld_backend.getRelPLT().create();
  rel_entry.setType(llvm::ELF::R_X86_64_JUMP_SLOT);
  rel_entry.targetRef().assign(*gotplt_entry);
  rel_entry.setSymInfo(rsym);
  return *plt_entry;
}

//===--------------------------------------------------------------------===//
// X86_64 Relocation Functions and Tables
//===--------------------------------------------------------------------===//
DECL_X86_64_APPLY_RELOC_FUNCS

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

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

// declare the table of applying functions
static const X86_64ApplyFunctionTriple X86_64ApplyFunctions[] = {
  DECL_X86_64_APPLY_RELOC_FUNC_PTRS
};

//===--------------------------------------------------------------------===//
// X86_64Relocator
//===--------------------------------------------------------------------===//
X86_64Relocator::X86_64Relocator(X86_64GNULDBackend& pParent,
                                 const LinkerConfig& pConfig)
  : X86Relocator(pConfig), m_Target(pParent) {
}

Relocator::Result
X86_64Relocator::applyRelocation(Relocation& pRelocation)
{
  Relocation::Type type = pRelocation.type();

  if (type >= sizeof (X86_64ApplyFunctions) / sizeof (X86_64ApplyFunctions[0]) ) {
    return Unknown;
  }

  // apply the relocation
  return X86_64ApplyFunctions[type].func(pRelocation, *this);
}

const char* X86_64Relocator::getName(Relocation::Type pType) const
{
  return X86_64ApplyFunctions[pType].name;
}

Relocator::Size X86_64Relocator::getSize(Relocation::Type pType) const
{
  return X86_64ApplyFunctions[pType].size;
}

void X86_64Relocator::scanLocalReloc(Relocation& pReloc,
                                     IRBuilder& pBuilder,
                                     Module& pModule,
                                     LDSection& pSection)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();

  switch(pReloc.type()){
    case llvm::ELF::R_X86_64_64:
      // If buiding PIC object (shared library or PIC executable),
      // a dynamic relocations with RELATIVE type to this location is needed.
      // Reserve an entry in .rela.dyn
      if (config().isCodeIndep()) {
        Relocation& reloc = helper_DynRel_init(rsym,
                                               *pReloc.targetRef().frag(),
                                               pReloc.targetRef().offset(),
                                               llvm::ELF::R_X86_64_RELATIVE,
                                               *this);
        getRelRelMap().record(pReloc, reloc);
        // set Rel bit
        rsym->setReserved(rsym->reserved() | ReserveRel);
        getTarget().checkAndSetHasTextRel(*pSection.getLink());
      }
      return;

    case llvm::ELF::R_X86_64_32:
    case llvm::ELF::R_X86_64_16:
    case llvm::ELF::R_X86_64_8:
    case llvm::ELF::R_X86_64_32S:
      // If buiding PIC object (shared library or PIC executable),
      // a dynamic relocations with RELATIVE type to this location is needed.
      // Reserve an entry in .rela.dyn
      if (config().isCodeIndep()) {
        Relocation& reloc = helper_DynRel_init(rsym,
                                               *pReloc.targetRef().frag(),
                                               pReloc.targetRef().offset(),
                                               pReloc.type(),
                                               *this);
        getRelRelMap().record(pReloc, reloc);
        // set Rel bit
        rsym->setReserved(rsym->reserved() | ReserveRel);
        getTarget().checkAndSetHasTextRel(*pSection.getLink());
      }
      return;

    case llvm::ELF::R_X86_64_PC32:
    case llvm::ELF::R_X86_64_PC16:
    case llvm::ELF::R_X86_64_PC8:
      return;

    case llvm::ELF::R_X86_64_GOTPCREL:
      // Symbol needs GOT entry, reserve entry in .got
      // return if we already create GOT for this symbol
      if (rsym->reserved() & ReserveGOT)
        return;

      // If building shared object or the symbol is undefined, a dynamic
      // relocation is needed to relocate this GOT entry. Reserve an
      // entry in .rela.dyn
      if (LinkerConfig::DynObj ==
                   config().codeGenType() || rsym->isUndef() || rsym->isDyn())
        helper_GOT_init(pReloc, true, *this);
      else
        helper_GOT_init(pReloc, false, *this);
      rsym->setReserved(rsym->reserved() | ReserveGOT);
      return;

    default:
      fatal(diag::unsupported_relocation) << (int)pReloc.type()
                                          << "mclinker@googlegroups.com";
      break;
  } // end switch
}

void X86_64Relocator::scanGlobalReloc(Relocation& pReloc,
                                      IRBuilder& pBuilder,
                                      Module& pModule,
                                      LDSection& pSection)
{
  // rsym - The relocation target symbol
  ResolveInfo* rsym = pReloc.symInfo();

  switch(pReloc.type()) {
    case llvm::ELF::R_X86_64_64:
    case llvm::ELF::R_X86_64_32:
    case llvm::ELF::R_X86_64_16:
    case llvm::ELF::R_X86_64_8:
    case llvm::ELF::R_X86_64_32S:
      // Absolute relocation type, symbol may needs PLT entry or
      // dynamic relocation entry
      if (getTarget().symbolNeedsPLT(*rsym)) {
        // create plt for this symbol if it does not have one
        if (!(rsym->reserved() & ReservePLT)){
          // Symbol needs PLT entry, we need to reserve a PLT entry
          // and the corresponding GOT and dynamic relocation entry
          // in .got and .rela.plt.
          helper_PLT_init(pReloc, *this);
          // set PLT bit
          rsym->setReserved(rsym->reserved() | ReservePLT);
        }
      }

      if (getTarget().symbolNeedsDynRel(*rsym, (rsym->reserved() & ReservePLT),
                                                                        true)) {
        // symbol needs dynamic relocation entry, set up the dynrel entry
        if (getTarget().symbolNeedsCopyReloc(pReloc, *rsym)) {
          LDSymbol& cpy_sym = defineSymbolforCopyReloc(pBuilder, *rsym, getTarget());
          addCopyReloc(*cpy_sym.resolveInfo(), getTarget());
        }
        else {
          // set Rel bit and the dyn rel
          rsym->setReserved(rsym->reserved() | ReserveRel);
          getTarget().checkAndSetHasTextRel(*pSection.getLink());
          if (llvm::ELF::R_386_32 == pReloc.type() &&
              helper_use_relative_reloc(*rsym, *this)) {
            Relocation& reloc = helper_DynRel_init(rsym,
                                                   *pReloc.targetRef().frag(),
                                                   pReloc.targetRef().offset(),
                                                   llvm::ELF::R_X86_64_RELATIVE,
                                                   *this);
            getRelRelMap().record(pReloc, reloc);
          }
          else {
            Relocation& reloc = helper_DynRel_init(rsym,
                                                   *pReloc.targetRef().frag(),
                                                   pReloc.targetRef().offset(),
                                                   pReloc.type(),
                                                   *this);
            getRelRelMap().record(pReloc, reloc);
          }
	        getTarget().checkAndSetHasTextRel(*pSection.getLink());
        }
      }
      return;

    case llvm::ELF::R_X86_64_GOTPCREL:
      // Symbol needs GOT entry, reserve entry in .got
      // return if we already create GOT for this symbol
      if (rsym->reserved() & ReserveGOT)
        return;

      // If building shared object or the symbol is undefined, a dynamic
      // relocation is needed to relocate this GOT entry. Reserve an
      // entry in .rela.dyn
      if (LinkerConfig::DynObj ==
                   config().codeGenType() || rsym->isUndef() || rsym->isDyn())
        helper_GOT_init(pReloc, true, *this);
      else
        helper_GOT_init(pReloc, false, *this);
      // set GOT bit
      rsym->setReserved(rsym->reserved() | ReserveGOT);
      return;

    case llvm::ELF::R_X86_64_PLT32:
      // A PLT entry is needed when building shared library

      // return if we already create plt for this symbol
      if (rsym->reserved() & ReservePLT)
        return;

      // if the symbol's value can be decided at link time, then no need plt
      if (getTarget().symbolFinalValueIsKnown(*rsym))
        return;

      // if symbol is defined in the ouput file and it's not
      // preemptible, no need plt
      if (rsym->isDefine() && !rsym->isDyn() &&
         !getTarget().isSymbolPreemptible(*rsym)) {
        return;
      }

      // Symbol needs PLT entry, we need a PLT entry
      // and the corresponding GOT and dynamic relocation entry
      // in .got and .rel.plt.
      helper_PLT_init(pReloc, *this);
      // set PLT bit
      rsym->setReserved(rsym->reserved() | ReservePLT);
      return;

    case llvm::ELF::R_X86_64_PC32:
    case llvm::ELF::R_X86_64_PC16:
    case llvm::ELF::R_X86_64_PC8:
      if (getTarget().symbolNeedsPLT(*rsym) &&
          LinkerConfig::DynObj != config().codeGenType()) {
        // create plt for this symbol if it does not have one
        if (!(rsym->reserved() & ReservePLT)){
          // Symbol needs PLT entry, we need a PLT entry
          // and the corresponding GOT and dynamic relocation entry
          // in .got and .rel.plt.
          helper_PLT_init(pReloc, *this);
          // set PLT bit
          rsym->setReserved(rsym->reserved() | ReservePLT);
        }
      }

      // Only PC relative relocation against dynamic symbol needs a
      // dynamic relocation.  Only dynamic copy relocation is allowed
      // and PC relative relocation will be resolved to the local copy.
      // All other dynamic relocations may lead to run-time relocation
      // overflow.
      if (getTarget().isDynamicSymbol(*rsym) &&
	        getTarget().symbolNeedsDynRel(*rsym,
                                        (rsym->reserved() & ReservePLT),
                                        false) &&
	        getTarget().symbolNeedsCopyReloc(pReloc, *rsym)) {
   	    LDSymbol& cpy_sym = defineSymbolforCopyReloc(pBuilder, *rsym, getTarget());
	      addCopyReloc(*cpy_sym.resolveInfo(), getTarget());
      }
      return;

    default:
      fatal(diag::unsupported_relocation) << (int)pReloc.type()
                                          << "mclinker@googlegroups.com";
      break;
  } // end switch
}

// ===
//
// ===
// R_X86_64_NONE
X86Relocator::Result none(Relocation& pReloc, X86_64Relocator& pParent)
{
  return X86Relocator::OK;
}

// R_X86_64_64: S + A
// R_X86_64_32:
// R_X86_64_16:
// R_X86_64_8
X86Relocator::Result abs(Relocation& pReloc, X86_64Relocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  Relocator::DWord A = pReloc.target() + pReloc.addend();
  Relocator::DWord S = pReloc.symValue();
  Relocation* dyn_rel = pParent.getRelRelMap().lookUp(pReloc);
  bool has_dyn_rel = (NULL != dyn_rel);

  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;
    return X86Relocator::OK;
  }

  // A local symbol may need RELA Type dynamic relocation
  if (rsym->isLocal() && has_dyn_rel) {
    dyn_rel->setAddend(S + A);
    return X86Relocator::OK;
  }

  // An external symbol may need PLT and dynamic relocation
  if (!rsym->isLocal()) {
    if (rsym->reserved() & X86Relocator::ReservePLT) {
      S = helper_get_PLT_address(*rsym, pParent);
    }
    // If we generate a dynamic relocation (except R_X86_64_RELATIVE)
    // for a place, we should not perform static relocation on it
    // in order to keep the addend store in the place correct.
    if (has_dyn_rel) {
      if (llvm::ELF::R_X86_64_64 == pReloc.type() &&
          helper_use_relative_reloc(*rsym, pParent)) {
        dyn_rel->setAddend(S + A);
      }
      else {
        dyn_rel->setAddend(A);
        return X86Relocator::OK;
      }
    }
  }

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

// R_X86_64_32S: S + A
X86Relocator::Result signed32(Relocation& pReloc, X86_64Relocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  Relocator::DWord A = pReloc.target() + pReloc.addend();
  Relocator::DWord S = pReloc.symValue();

  // There should be no dynamic relocations for R_X86_64_32S.
  if (NULL != pParent.getRelRelMap().lookUp(pReloc))
    return X86Relocator::BadReloc;

  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)
  // An external symbol may need PLT and dynamic relocation
  if (0x0 != (llvm::ELF::SHF_ALLOC & target_sect.flag()) &&
      !rsym->isLocal() && rsym->reserved() & X86Relocator::ReservePLT)
    S = helper_get_PLT_address(*rsym, pParent);

#if notyet
  // Check 32-bit signed overflow.
  Relocator::SWord V = S + A;
  if (V > INT64_C(0x7fffffff) || V < INT64_C(-0x80000000))
    return X86Relocator::Overflow;
#endif

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

// R_X86_64_GOTPCREL: GOT(S) + GOT_ORG + A - P
X86Relocator::Result gotpcrel(Relocation& pReloc, X86_64Relocator& pParent)
{
  if (!(pReloc.symInfo()->reserved() & X86Relocator::ReserveGOT)) {
    return X86Relocator::BadReloc;
  }

  // set symbol value of the got entry if needed
  X86_64GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*pReloc.symInfo());
  if (X86Relocator::SymVal == got_entry->getValue())
    got_entry->setValue(pReloc.symValue());

  // setup relocation addend if needed
  Relocation* dyn_rel = pParent.getRelRelMap().lookUp(pReloc);
  if ((NULL != dyn_rel) && (X86Relocator::SymVal == dyn_rel->addend())) {
    dyn_rel->setAddend(pReloc.symValue());
  }

  X86Relocator::Address GOT_S   = helper_get_GOT_address(pReloc, pParent);
  Relocator::DWord      A       = pReloc.target() + pReloc.addend();
  X86Relocator::Address GOT_ORG = helper_GOT_ORG(pParent);
  // Apply relocation.
  pReloc.target() = GOT_S + GOT_ORG + A - pReloc.place();
  return X86Relocator::OK;
}

// R_X86_64_PLT32: PLT(S) + A - P
X86Relocator::Result plt32(Relocation& pReloc, X86_64Relocator& pParent)
{
  // PLT_S depends on if there is a PLT entry.
  X86Relocator::Address PLT_S;
  if ((pReloc.symInfo()->reserved() & X86Relocator::ReservePLT))
    PLT_S = helper_get_PLT_address(*pReloc.symInfo(), pParent);
  else
    PLT_S = pReloc.symValue();
  Relocator::DWord      A = pReloc.target() + pReloc.addend();
  X86Relocator::Address P = pReloc.place();
  pReloc.target() = PLT_S + A - P;
  return X86Relocator::OK;
}

// R_X86_64_PC32: S + A - P
// R_X86_64_PC16
// R_X86_64_PC8
X86Relocator::Result rel(Relocation& pReloc, X86_64Relocator& pParent)
{
  ResolveInfo* rsym = pReloc.symInfo();
  Relocator::DWord A = pReloc.target() + pReloc.addend();
  Relocator::DWord S = pReloc.symValue();
  Relocator::DWord P = pReloc.place();

  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 - P;
    return X86Relocator::OK;
  }

  // setup relocation addend if needed
  Relocation* dyn_rel = pParent.getRelRelMap().lookUp(pReloc);
  if ((NULL != dyn_rel) && (X86Relocator::SymVal == dyn_rel->addend())) {
    dyn_rel->setAddend(S);
  }

  // An external symbol may need PLT and dynamic relocation
  if (!rsym->isLocal()) {
    if (rsym->reserved() & X86Relocator::ReservePLT) {
       S = helper_get_PLT_address(*rsym, pParent);
    }
    if (pParent.getTarget().symbolNeedsDynRel(
                              *rsym,
                              (rsym->reserved() & X86Relocator::ReservePLT),
                              false)) {
      return X86Relocator::Overflow;
    }
  }

   // perform static relocation
  pReloc.target() = S + A - P;
  return X86Relocator::OK;
}

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