X86Relocator.cpp revision fda4bbd63841e8ae3418637b24b7416f6bf56ca6
1baf412994612ae72a3db9698b1d216078297b48aChris Craik//===- X86Relocator.cpp -------------------------------------------===//
2baf412994612ae72a3db9698b1d216078297b48aChris Craik//
3baf412994612ae72a3db9698b1d216078297b48aChris Craik//                     The MCLinker Project
4baf412994612ae72a3db9698b1d216078297b48aChris Craik//
5baf412994612ae72a3db9698b1d216078297b48aChris Craik// This file is distributed under the University of Illinois Open Source
6baf412994612ae72a3db9698b1d216078297b48aChris Craik// License. See LICENSE.TXT for details.
7baf412994612ae72a3db9698b1d216078297b48aChris Craik//
8baf412994612ae72a3db9698b1d216078297b48aChris Craik//===----------------------------------------------------------------------===//
9baf412994612ae72a3db9698b1d216078297b48aChris Craik#include "X86Relocator.h"
10baf412994612ae72a3db9698b1d216078297b48aChris Craik#include "X86RelocationFunctions.h"
11baf412994612ae72a3db9698b1d216078297b48aChris Craik
12baf412994612ae72a3db9698b1d216078297b48aChris Craik#include "mcld/IRBuilder.h"
13baf412994612ae72a3db9698b1d216078297b48aChris Craik#include "mcld/LinkerConfig.h"
14baf412994612ae72a3db9698b1d216078297b48aChris Craik#include "mcld/LD/ELFFileFormat.h"
15baf412994612ae72a3db9698b1d216078297b48aChris Craik#include "mcld/LD/ELFSegmentFactory.h"
16baf412994612ae72a3db9698b1d216078297b48aChris Craik#include "mcld/LD/ELFSegment.h"
17baf412994612ae72a3db9698b1d216078297b48aChris Craik#include "mcld/LD/LDSymbol.h"
18baf412994612ae72a3db9698b1d216078297b48aChris Craik#include "mcld/Object/ObjectBuilder.h"
19baf412994612ae72a3db9698b1d216078297b48aChris Craik#include "mcld/Support/MsgHandling.h"
20baf412994612ae72a3db9698b1d216078297b48aChris Craik
21baf412994612ae72a3db9698b1d216078297b48aChris Craik#include <llvm/ADT/Twine.h>
22baf412994612ae72a3db9698b1d216078297b48aChris Craik#include <llvm/Support/DataTypes.h>
23baf412994612ae72a3db9698b1d216078297b48aChris Craik#include <llvm/Support/ELF.h>
24baf412994612ae72a3db9698b1d216078297b48aChris Craik
25baf412994612ae72a3db9698b1d216078297b48aChris Craiknamespace mcld {
26baf412994612ae72a3db9698b1d216078297b48aChris Craik
27baf412994612ae72a3db9698b1d216078297b48aChris Craik//===--------------------------------------------------------------------===//
28baf412994612ae72a3db9698b1d216078297b48aChris Craik// X86_32 Relocation helper function
29baf412994612ae72a3db9698b1d216078297b48aChris Craik//===--------------------------------------------------------------------===//
30baf412994612ae72a3db9698b1d216078297b48aChris Craik/// helper_DynRel - Get an relocation entry in .rel.dyn
31baf412994612ae72a3db9698b1d216078297b48aChris Craikstatic Relocation& helper_DynRel_init(ResolveInfo* pSym,
32baf412994612ae72a3db9698b1d216078297b48aChris Craik                                      Fragment& pFrag,
33baf412994612ae72a3db9698b1d216078297b48aChris Craik                                      uint64_t pOffset,
34baf412994612ae72a3db9698b1d216078297b48aChris Craik                                      Relocator::Type pType,
35baf412994612ae72a3db9698b1d216078297b48aChris Craik                                      X86_32Relocator& pParent) {
36baf412994612ae72a3db9698b1d216078297b48aChris Craik  X86_32GNULDBackend& ld_backend = pParent.getTarget();
37baf412994612ae72a3db9698b1d216078297b48aChris Craik  Relocation& rel_entry = *ld_backend.getRelDyn().create();
38baf412994612ae72a3db9698b1d216078297b48aChris Craik  rel_entry.setType(pType);
39baf412994612ae72a3db9698b1d216078297b48aChris Craik  rel_entry.targetRef().assign(pFrag, pOffset);
40baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (pType == llvm::ELF::R_386_RELATIVE || pSym == NULL)
41baf412994612ae72a3db9698b1d216078297b48aChris Craik    rel_entry.setSymInfo(NULL);
42baf412994612ae72a3db9698b1d216078297b48aChris Craik  else
43baf412994612ae72a3db9698b1d216078297b48aChris Craik    rel_entry.setSymInfo(pSym);
44baf412994612ae72a3db9698b1d216078297b48aChris Craik
45baf412994612ae72a3db9698b1d216078297b48aChris Craik  return rel_entry;
46baf412994612ae72a3db9698b1d216078297b48aChris Craik}
47baf412994612ae72a3db9698b1d216078297b48aChris Craik
48baf412994612ae72a3db9698b1d216078297b48aChris Craik/// helper_use_relative_reloc - Check if symbol ceuse relocation
49baf412994612ae72a3db9698b1d216078297b48aChris Craik/// R_386_RELATIVE
50baf412994612ae72a3db9698b1d216078297b48aChris Craikstatic bool helper_use_relative_reloc(const ResolveInfo& pSym,
51baf412994612ae72a3db9698b1d216078297b48aChris Craik                                      const X86_32Relocator& pFactory) {
52baf412994612ae72a3db9698b1d216078297b48aChris Craik  // if symbol is dynamic or undefine or preemptible
53baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (pSym.isDyn() || pSym.isUndef() ||
54baf412994612ae72a3db9698b1d216078297b48aChris Craik      pFactory.getTarget().isSymbolPreemptible(pSym))
55baf412994612ae72a3db9698b1d216078297b48aChris Craik    return false;
56baf412994612ae72a3db9698b1d216078297b48aChris Craik  return true;
57baf412994612ae72a3db9698b1d216078297b48aChris Craik}
58baf412994612ae72a3db9698b1d216078297b48aChris Craik
59baf412994612ae72a3db9698b1d216078297b48aChris Craikstatic X86_32GOTEntry& helper_GOT_init(Relocation& pReloc,
60baf412994612ae72a3db9698b1d216078297b48aChris Craik                                       bool pHasRel,
61baf412994612ae72a3db9698b1d216078297b48aChris Craik                                       X86_32Relocator& pParent) {
62baf412994612ae72a3db9698b1d216078297b48aChris Craik  // rsym - The relocation target symbol
63baf412994612ae72a3db9698b1d216078297b48aChris Craik  ResolveInfo* rsym = pReloc.symInfo();
64baf412994612ae72a3db9698b1d216078297b48aChris Craik  X86_32GNULDBackend& ld_backend = pParent.getTarget();
65baf412994612ae72a3db9698b1d216078297b48aChris Craik  assert(pParent.getSymGOTMap().lookUp(*rsym) == NULL);
66baf412994612ae72a3db9698b1d216078297b48aChris Craik
67baf412994612ae72a3db9698b1d216078297b48aChris Craik  X86_32GOTEntry* got_entry = ld_backend.getGOT().create();
68baf412994612ae72a3db9698b1d216078297b48aChris Craik  pParent.getSymGOTMap().record(*rsym, *got_entry);
69baf412994612ae72a3db9698b1d216078297b48aChris Craik
70baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (!pHasRel) {
71baf412994612ae72a3db9698b1d216078297b48aChris Craik    // No corresponding dynamic relocation, initialize to the symbol value.
72baf412994612ae72a3db9698b1d216078297b48aChris Craik    got_entry->setValue(X86Relocator::SymVal);
73baf412994612ae72a3db9698b1d216078297b48aChris Craik  } else {
74baf412994612ae72a3db9698b1d216078297b48aChris Craik    // Initialize got_entry content and the corresponding dynamic relocation.
75baf412994612ae72a3db9698b1d216078297b48aChris Craik    if (helper_use_relative_reloc(*rsym, pParent)) {
76baf412994612ae72a3db9698b1d216078297b48aChris Craik      helper_DynRel_init(
77baf412994612ae72a3db9698b1d216078297b48aChris Craik          rsym, *got_entry, 0x0, llvm::ELF::R_386_RELATIVE, pParent);
78baf412994612ae72a3db9698b1d216078297b48aChris Craik      got_entry->setValue(X86Relocator::SymVal);
79baf412994612ae72a3db9698b1d216078297b48aChris Craik    } else {
80baf412994612ae72a3db9698b1d216078297b48aChris Craik      helper_DynRel_init(
81baf412994612ae72a3db9698b1d216078297b48aChris Craik          rsym, *got_entry, 0x0, llvm::ELF::R_386_GLOB_DAT, pParent);
82baf412994612ae72a3db9698b1d216078297b48aChris Craik      got_entry->setValue(0x0);
83baf412994612ae72a3db9698b1d216078297b48aChris Craik    }
84baf412994612ae72a3db9698b1d216078297b48aChris Craik  }
85baf412994612ae72a3db9698b1d216078297b48aChris Craik  return *got_entry;
86baf412994612ae72a3db9698b1d216078297b48aChris Craik}
87baf412994612ae72a3db9698b1d216078297b48aChris Craik
88baf412994612ae72a3db9698b1d216078297b48aChris Craikstatic Relocator::Address helper_GOT_ORG(X86_32Relocator& pParent) {
89baf412994612ae72a3db9698b1d216078297b48aChris Craik  return pParent.getTarget().getGOTPLT().addr();
90baf412994612ae72a3db9698b1d216078297b48aChris Craik}
91baf412994612ae72a3db9698b1d216078297b48aChris Craik
92baf412994612ae72a3db9698b1d216078297b48aChris Craikstatic Relocator::Address helper_get_GOT_address(Relocation& pReloc,
93baf412994612ae72a3db9698b1d216078297b48aChris Craik                                                 X86_32Relocator& pParent) {
94baf412994612ae72a3db9698b1d216078297b48aChris Craik  X86_32GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*pReloc.symInfo());
95baf412994612ae72a3db9698b1d216078297b48aChris Craik  assert(got_entry != NULL);
96baf412994612ae72a3db9698b1d216078297b48aChris Craik  return pParent.getTarget().getGOT().addr() + got_entry->getOffset();
97baf412994612ae72a3db9698b1d216078297b48aChris Craik}
98baf412994612ae72a3db9698b1d216078297b48aChris Craik
99baf412994612ae72a3db9698b1d216078297b48aChris Craikstatic PLTEntryBase& helper_PLT_init(Relocation& pReloc,
100baf412994612ae72a3db9698b1d216078297b48aChris Craik                                     X86_32Relocator& pParent) {
101baf412994612ae72a3db9698b1d216078297b48aChris Craik  // rsym - The relocation target symbol
102baf412994612ae72a3db9698b1d216078297b48aChris Craik  ResolveInfo* rsym = pReloc.symInfo();
103baf412994612ae72a3db9698b1d216078297b48aChris Craik  X86_32GNULDBackend& ld_backend = pParent.getTarget();
104baf412994612ae72a3db9698b1d216078297b48aChris Craik  assert(pParent.getSymPLTMap().lookUp(*rsym) == NULL);
105baf412994612ae72a3db9698b1d216078297b48aChris Craik
106baf412994612ae72a3db9698b1d216078297b48aChris Craik  PLTEntryBase* plt_entry = ld_backend.getPLT().create();
107baf412994612ae72a3db9698b1d216078297b48aChris Craik  pParent.getSymPLTMap().record(*rsym, *plt_entry);
108baf412994612ae72a3db9698b1d216078297b48aChris Craik
109baf412994612ae72a3db9698b1d216078297b48aChris Craik  // initialize plt and the corresponding gotplt and dyn rel entry.
110baf412994612ae72a3db9698b1d216078297b48aChris Craik  assert(pParent.getSymGOTPLTMap().lookUp(*rsym) == NULL &&
111baf412994612ae72a3db9698b1d216078297b48aChris Craik         "PLT entry not exist, but GOTPLT entry exist!");
112baf412994612ae72a3db9698b1d216078297b48aChris Craik  X86_32GOTEntry* gotplt_entry = ld_backend.getGOTPLT().create();
113baf412994612ae72a3db9698b1d216078297b48aChris Craik  pParent.getSymGOTPLTMap().record(*rsym, *gotplt_entry);
114baf412994612ae72a3db9698b1d216078297b48aChris Craik
115baf412994612ae72a3db9698b1d216078297b48aChris Craik  // init the corresponding rel entry in .rel.plt
116baf412994612ae72a3db9698b1d216078297b48aChris Craik  Relocation& rel_entry = *ld_backend.getRelPLT().create();
117baf412994612ae72a3db9698b1d216078297b48aChris Craik  rel_entry.setType(llvm::ELF::R_386_JUMP_SLOT);
118baf412994612ae72a3db9698b1d216078297b48aChris Craik  rel_entry.targetRef().assign(*gotplt_entry);
119baf412994612ae72a3db9698b1d216078297b48aChris Craik  rel_entry.setSymInfo(rsym);
120baf412994612ae72a3db9698b1d216078297b48aChris Craik  return *plt_entry;
121baf412994612ae72a3db9698b1d216078297b48aChris Craik}
122baf412994612ae72a3db9698b1d216078297b48aChris Craik
123baf412994612ae72a3db9698b1d216078297b48aChris Craikstatic Relocator::Address helper_get_PLT_address(ResolveInfo& pSym,
124baf412994612ae72a3db9698b1d216078297b48aChris Craik                                                 X86_32Relocator& pParent) {
125baf412994612ae72a3db9698b1d216078297b48aChris Craik  PLTEntryBase* plt_entry = pParent.getSymPLTMap().lookUp(pSym);
126baf412994612ae72a3db9698b1d216078297b48aChris Craik  assert(plt_entry != NULL);
127baf412994612ae72a3db9698b1d216078297b48aChris Craik  return pParent.getTarget().getPLT().addr() + plt_entry->getOffset();
128baf412994612ae72a3db9698b1d216078297b48aChris Craik}
129baf412994612ae72a3db9698b1d216078297b48aChris Craik
130baf412994612ae72a3db9698b1d216078297b48aChris Craik//===--------------------------------------------------------------------===//
131baf412994612ae72a3db9698b1d216078297b48aChris Craik// X86_32 Relocation Functions and Tables
132baf412994612ae72a3db9698b1d216078297b48aChris Craik//===--------------------------------------------------------------------===//
133baf412994612ae72a3db9698b1d216078297b48aChris CraikDECL_X86_32_APPLY_RELOC_FUNCS
134baf412994612ae72a3db9698b1d216078297b48aChris Craik
135baf412994612ae72a3db9698b1d216078297b48aChris Craik/// the prototype of applying function
136baf412994612ae72a3db9698b1d216078297b48aChris Craiktypedef Relocator::Result (*X86_32ApplyFunctionType)(Relocation& pReloc,
137baf412994612ae72a3db9698b1d216078297b48aChris Craik                                                     X86_32Relocator& pParent);
138baf412994612ae72a3db9698b1d216078297b48aChris Craik
139baf412994612ae72a3db9698b1d216078297b48aChris Craik// the table entry of applying functions
140baf412994612ae72a3db9698b1d216078297b48aChris Craikstruct X86_32ApplyFunctionTriple {
141baf412994612ae72a3db9698b1d216078297b48aChris Craik  X86_32ApplyFunctionType func;
142baf412994612ae72a3db9698b1d216078297b48aChris Craik  unsigned int type;
143baf412994612ae72a3db9698b1d216078297b48aChris Craik  const char* name;
144baf412994612ae72a3db9698b1d216078297b48aChris Craik  unsigned int size;
145baf412994612ae72a3db9698b1d216078297b48aChris Craik};
146baf412994612ae72a3db9698b1d216078297b48aChris Craik
147baf412994612ae72a3db9698b1d216078297b48aChris Craik// declare the table of applying functions
148baf412994612ae72a3db9698b1d216078297b48aChris Craikstatic const X86_32ApplyFunctionTriple X86_32ApplyFunctions[] = {
149baf412994612ae72a3db9698b1d216078297b48aChris Craik    DECL_X86_32_APPLY_RELOC_FUNC_PTRS};
150baf412994612ae72a3db9698b1d216078297b48aChris Craik
151baf412994612ae72a3db9698b1d216078297b48aChris Craik//===--------------------------------------------------------------------===//
152baf412994612ae72a3db9698b1d216078297b48aChris Craik// X86Relocator
153baf412994612ae72a3db9698b1d216078297b48aChris Craik//===--------------------------------------------------------------------===//
154baf412994612ae72a3db9698b1d216078297b48aChris CraikX86Relocator::X86Relocator(const LinkerConfig& pConfig) : Relocator(pConfig) {
155baf412994612ae72a3db9698b1d216078297b48aChris Craik}
156baf412994612ae72a3db9698b1d216078297b48aChris Craik
157baf412994612ae72a3db9698b1d216078297b48aChris CraikX86Relocator::~X86Relocator() {
158baf412994612ae72a3db9698b1d216078297b48aChris Craik}
159baf412994612ae72a3db9698b1d216078297b48aChris Craik
160baf412994612ae72a3db9698b1d216078297b48aChris Craikvoid X86Relocator::scanRelocation(Relocation& pReloc,
161baf412994612ae72a3db9698b1d216078297b48aChris Craik                                  IRBuilder& pLinker,
162baf412994612ae72a3db9698b1d216078297b48aChris Craik                                  Module& pModule,
163baf412994612ae72a3db9698b1d216078297b48aChris Craik                                  LDSection& pSection,
164baf412994612ae72a3db9698b1d216078297b48aChris Craik                                  Input& pInput) {
165baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (LinkerConfig::Object == config().codeGenType())
166baf412994612ae72a3db9698b1d216078297b48aChris Craik    return;
167baf412994612ae72a3db9698b1d216078297b48aChris Craik  // rsym - The relocation target symbol
168baf412994612ae72a3db9698b1d216078297b48aChris Craik  ResolveInfo* rsym = pReloc.symInfo();
169baf412994612ae72a3db9698b1d216078297b48aChris Craik  assert(rsym != NULL &&
170baf412994612ae72a3db9698b1d216078297b48aChris Craik         "ResolveInfo of relocation not set while scanRelocation");
171baf412994612ae72a3db9698b1d216078297b48aChris Craik
172baf412994612ae72a3db9698b1d216078297b48aChris Craik  assert(pSection.getLink() != NULL);
173baf412994612ae72a3db9698b1d216078297b48aChris Craik  if ((pSection.getLink()->flag() & llvm::ELF::SHF_ALLOC) == 0)
174baf412994612ae72a3db9698b1d216078297b48aChris Craik    return;
175baf412994612ae72a3db9698b1d216078297b48aChris Craik
176baf412994612ae72a3db9698b1d216078297b48aChris Craik  // Scan relocation type to determine if the GOT/PLT/Dynamic Relocation
177baf412994612ae72a3db9698b1d216078297b48aChris Craik  // entries should be created.
178baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (rsym->isLocal())  // rsym is local
179baf412994612ae72a3db9698b1d216078297b48aChris Craik    scanLocalReloc(pReloc, pLinker, pModule, pSection);
180baf412994612ae72a3db9698b1d216078297b48aChris Craik  else  // rsym is external
181baf412994612ae72a3db9698b1d216078297b48aChris Craik    scanGlobalReloc(pReloc, pLinker, pModule, pSection);
182baf412994612ae72a3db9698b1d216078297b48aChris Craik
183baf412994612ae72a3db9698b1d216078297b48aChris Craik  // check if we should issue undefined reference for the relocation target
184baf412994612ae72a3db9698b1d216078297b48aChris Craik  // symbol
185baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (rsym->isUndef() && !rsym->isDyn() && !rsym->isWeak() && !rsym->isNull())
186baf412994612ae72a3db9698b1d216078297b48aChris Craik    issueUndefRef(pReloc, pSection, pInput);
187baf412994612ae72a3db9698b1d216078297b48aChris Craik}
188baf412994612ae72a3db9698b1d216078297b48aChris Craik
189baf412994612ae72a3db9698b1d216078297b48aChris Craikvoid X86Relocator::addCopyReloc(ResolveInfo& pSym, X86GNULDBackend& pTarget) {
190baf412994612ae72a3db9698b1d216078297b48aChris Craik  Relocation& rel_entry = *pTarget.getRelDyn().create();
191baf412994612ae72a3db9698b1d216078297b48aChris Craik  rel_entry.setType(pTarget.getCopyRelType());
192baf412994612ae72a3db9698b1d216078297b48aChris Craik  assert(pSym.outSymbol()->hasFragRef());
193baf412994612ae72a3db9698b1d216078297b48aChris Craik  rel_entry.targetRef().assign(*pSym.outSymbol()->fragRef());
194baf412994612ae72a3db9698b1d216078297b48aChris Craik  rel_entry.setSymInfo(&pSym);
195baf412994612ae72a3db9698b1d216078297b48aChris Craik}
196baf412994612ae72a3db9698b1d216078297b48aChris Craik
197baf412994612ae72a3db9698b1d216078297b48aChris Craik/// defineSymbolforCopyReloc
198baf412994612ae72a3db9698b1d216078297b48aChris Craik/// For a symbol needing copy relocation, define a copy symbol in the BSS
199baf412994612ae72a3db9698b1d216078297b48aChris Craik/// section and all other reference to this symbol should refer to this
200baf412994612ae72a3db9698b1d216078297b48aChris Craik/// copy.
201baf412994612ae72a3db9698b1d216078297b48aChris Craik/// @note This is executed at `scan relocation' stage.
202baf412994612ae72a3db9698b1d216078297b48aChris CraikLDSymbol& X86Relocator::defineSymbolforCopyReloc(IRBuilder& pBuilder,
203baf412994612ae72a3db9698b1d216078297b48aChris Craik                                                 const ResolveInfo& pSym,
204baf412994612ae72a3db9698b1d216078297b48aChris Craik                                                 X86GNULDBackend& pTarget) {
205baf412994612ae72a3db9698b1d216078297b48aChris Craik  // get or create corresponding BSS LDSection
206baf412994612ae72a3db9698b1d216078297b48aChris Craik  LDSection* bss_sect_hdr = NULL;
207baf412994612ae72a3db9698b1d216078297b48aChris Craik  ELFFileFormat* file_format = pTarget.getOutputFormat();
208baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (ResolveInfo::ThreadLocal == pSym.type())
209baf412994612ae72a3db9698b1d216078297b48aChris Craik    bss_sect_hdr = &file_format->getTBSS();
210baf412994612ae72a3db9698b1d216078297b48aChris Craik  else
211baf412994612ae72a3db9698b1d216078297b48aChris Craik    bss_sect_hdr = &file_format->getBSS();
212baf412994612ae72a3db9698b1d216078297b48aChris Craik
213baf412994612ae72a3db9698b1d216078297b48aChris Craik  // get or create corresponding BSS SectionData
214baf412994612ae72a3db9698b1d216078297b48aChris Craik  assert(bss_sect_hdr != NULL);
215baf412994612ae72a3db9698b1d216078297b48aChris Craik  SectionData* bss_section = NULL;
216baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (bss_sect_hdr->hasSectionData())
217baf412994612ae72a3db9698b1d216078297b48aChris Craik    bss_section = bss_sect_hdr->getSectionData();
218baf412994612ae72a3db9698b1d216078297b48aChris Craik  else
219baf412994612ae72a3db9698b1d216078297b48aChris Craik    bss_section = IRBuilder::CreateSectionData(*bss_sect_hdr);
220baf412994612ae72a3db9698b1d216078297b48aChris Craik
221baf412994612ae72a3db9698b1d216078297b48aChris Craik  // Determine the alignment by the symbol value
222baf412994612ae72a3db9698b1d216078297b48aChris Craik  // FIXME: here we use the largest alignment
223baf412994612ae72a3db9698b1d216078297b48aChris Craik  uint32_t addralign = config().targets().bitclass() / 8;
224baf412994612ae72a3db9698b1d216078297b48aChris Craik
225baf412994612ae72a3db9698b1d216078297b48aChris Craik  // allocate space in BSS for the copy symbol
226baf412994612ae72a3db9698b1d216078297b48aChris Craik  Fragment* frag = new FillFragment(0x0, 1, pSym.size());
227baf412994612ae72a3db9698b1d216078297b48aChris Craik  uint64_t size = ObjectBuilder::AppendFragment(*frag, *bss_section, addralign);
228baf412994612ae72a3db9698b1d216078297b48aChris Craik  bss_sect_hdr->setSize(bss_sect_hdr->size() + size);
229baf412994612ae72a3db9698b1d216078297b48aChris Craik
230baf412994612ae72a3db9698b1d216078297b48aChris Craik  // change symbol binding to Global if it's a weak symbol
231baf412994612ae72a3db9698b1d216078297b48aChris Craik  ResolveInfo::Binding binding = (ResolveInfo::Binding)pSym.binding();
232baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (binding == ResolveInfo::Weak)
233baf412994612ae72a3db9698b1d216078297b48aChris Craik    binding = ResolveInfo::Global;
234baf412994612ae72a3db9698b1d216078297b48aChris Craik
235baf412994612ae72a3db9698b1d216078297b48aChris Craik  // Define the copy symbol in the bss section and resolve it
236baf412994612ae72a3db9698b1d216078297b48aChris Craik  LDSymbol* cpy_sym = pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
237baf412994612ae72a3db9698b1d216078297b48aChris Craik      pSym.name(),
238baf412994612ae72a3db9698b1d216078297b48aChris Craik      (ResolveInfo::Type)pSym.type(),
239baf412994612ae72a3db9698b1d216078297b48aChris Craik      ResolveInfo::Define,
240baf412994612ae72a3db9698b1d216078297b48aChris Craik      binding,
241baf412994612ae72a3db9698b1d216078297b48aChris Craik      pSym.size(),  // size
242baf412994612ae72a3db9698b1d216078297b48aChris Craik      0x0,          // value
243baf412994612ae72a3db9698b1d216078297b48aChris Craik      FragmentRef::Create(*frag, 0x0),
244baf412994612ae72a3db9698b1d216078297b48aChris Craik      (ResolveInfo::Visibility)pSym.other());
245baf412994612ae72a3db9698b1d216078297b48aChris Craik
246baf412994612ae72a3db9698b1d216078297b48aChris Craik  // output all other alias symbols if any
247baf412994612ae72a3db9698b1d216078297b48aChris Craik  Module& pModule = pBuilder.getModule();
248baf412994612ae72a3db9698b1d216078297b48aChris Craik  Module::AliasList* alias_list = pModule.getAliasList(pSym);
249baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (alias_list != NULL) {
250baf412994612ae72a3db9698b1d216078297b48aChris Craik    Module::alias_iterator it, it_e = alias_list->end();
251baf412994612ae72a3db9698b1d216078297b48aChris Craik    for (it = alias_list->begin(); it != it_e; ++it) {
252baf412994612ae72a3db9698b1d216078297b48aChris Craik      const ResolveInfo* alias = *it;
253baf412994612ae72a3db9698b1d216078297b48aChris Craik      if (alias != &pSym && alias->isDyn()) {
254baf412994612ae72a3db9698b1d216078297b48aChris Craik        pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
255baf412994612ae72a3db9698b1d216078297b48aChris Craik            alias->name(),
256baf412994612ae72a3db9698b1d216078297b48aChris Craik            (ResolveInfo::Type)alias->type(),
257baf412994612ae72a3db9698b1d216078297b48aChris Craik            ResolveInfo::Define,
258baf412994612ae72a3db9698b1d216078297b48aChris Craik            binding,
259baf412994612ae72a3db9698b1d216078297b48aChris Craik            alias->size(),  // size
260baf412994612ae72a3db9698b1d216078297b48aChris Craik            0x0,            // value
261baf412994612ae72a3db9698b1d216078297b48aChris Craik            FragmentRef::Create(*frag, 0x0),
262baf412994612ae72a3db9698b1d216078297b48aChris Craik            (ResolveInfo::Visibility)alias->other());
263baf412994612ae72a3db9698b1d216078297b48aChris Craik      }
264baf412994612ae72a3db9698b1d216078297b48aChris Craik    }
265baf412994612ae72a3db9698b1d216078297b48aChris Craik  }
266baf412994612ae72a3db9698b1d216078297b48aChris Craik
267baf412994612ae72a3db9698b1d216078297b48aChris Craik  return *cpy_sym;
268baf412994612ae72a3db9698b1d216078297b48aChris Craik}
269baf412994612ae72a3db9698b1d216078297b48aChris Craik
270baf412994612ae72a3db9698b1d216078297b48aChris Craik//===----------------------------------------------------------------------===//
271baf412994612ae72a3db9698b1d216078297b48aChris Craik// X86_32Relocator
272baf412994612ae72a3db9698b1d216078297b48aChris Craik//===----------------------------------------------------------------------===//
273baf412994612ae72a3db9698b1d216078297b48aChris CraikX86_32Relocator::X86_32Relocator(X86_32GNULDBackend& pParent,
274baf412994612ae72a3db9698b1d216078297b48aChris Craik                                 const LinkerConfig& pConfig)
275baf412994612ae72a3db9698b1d216078297b48aChris Craik    : X86Relocator(pConfig), m_Target(pParent) {
276baf412994612ae72a3db9698b1d216078297b48aChris Craik}
277baf412994612ae72a3db9698b1d216078297b48aChris Craik
278baf412994612ae72a3db9698b1d216078297b48aChris CraikRelocator::Result X86_32Relocator::applyRelocation(Relocation& pRelocation) {
279baf412994612ae72a3db9698b1d216078297b48aChris Craik  Relocation::Type type = pRelocation.type();
280baf412994612ae72a3db9698b1d216078297b48aChris Craik
281baf412994612ae72a3db9698b1d216078297b48aChris Craik  if (type >= sizeof(X86_32ApplyFunctions) / sizeof(X86_32ApplyFunctions[0])) {
282baf412994612ae72a3db9698b1d216078297b48aChris Craik    return Unknown;
283baf412994612ae72a3db9698b1d216078297b48aChris Craik  }
284baf412994612ae72a3db9698b1d216078297b48aChris Craik
285baf412994612ae72a3db9698b1d216078297b48aChris Craik  // apply the relocation
286baf412994612ae72a3db9698b1d216078297b48aChris Craik  return X86_32ApplyFunctions[type].func(pRelocation, *this);
287baf412994612ae72a3db9698b1d216078297b48aChris Craik}
288baf412994612ae72a3db9698b1d216078297b48aChris Craik
289baf412994612ae72a3db9698b1d216078297b48aChris Craikconst char* X86_32Relocator::getName(Relocation::Type pType) const {
290baf412994612ae72a3db9698b1d216078297b48aChris Craik  return X86_32ApplyFunctions[pType].name;
291baf412994612ae72a3db9698b1d216078297b48aChris Craik}
292baf412994612ae72a3db9698b1d216078297b48aChris Craik
293baf412994612ae72a3db9698b1d216078297b48aChris CraikRelocator::Size X86_32Relocator::getSize(Relocation::Type pType) const {
294baf412994612ae72a3db9698b1d216078297b48aChris Craik  return X86_32ApplyFunctions[pType].size;
295baf412994612ae72a3db9698b1d216078297b48aChris Craik}
296baf412994612ae72a3db9698b1d216078297b48aChris Craik
297baf412994612ae72a3db9698b1d216078297b48aChris Craikbool X86_32Relocator::mayHaveFunctionPointerAccess(
298baf412994612ae72a3db9698b1d216078297b48aChris Craik    const Relocation& pReloc) const {
299baf412994612ae72a3db9698b1d216078297b48aChris Craik  switch (pReloc.type()) {
300baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_32:
301baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_16:
302baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_8:
303baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_GOTOFF:
304baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_GOT32: {
305baf412994612ae72a3db9698b1d216078297b48aChris Craik      return true;
306baf412994612ae72a3db9698b1d216078297b48aChris Craik    }
307baf412994612ae72a3db9698b1d216078297b48aChris Craik    default: { return false; }
308baf412994612ae72a3db9698b1d216078297b48aChris Craik  }
309baf412994612ae72a3db9698b1d216078297b48aChris Craik}
310baf412994612ae72a3db9698b1d216078297b48aChris Craik
311baf412994612ae72a3db9698b1d216078297b48aChris Craikvoid X86_32Relocator::scanLocalReloc(Relocation& pReloc,
312baf412994612ae72a3db9698b1d216078297b48aChris Craik                                     IRBuilder& pBuilder,
313baf412994612ae72a3db9698b1d216078297b48aChris Craik                                     Module& pModule,
314baf412994612ae72a3db9698b1d216078297b48aChris Craik                                     LDSection& pSection) {
315baf412994612ae72a3db9698b1d216078297b48aChris Craik  // rsym - The relocation target symbol
316baf412994612ae72a3db9698b1d216078297b48aChris Craik  ResolveInfo* rsym = pReloc.symInfo();
317baf412994612ae72a3db9698b1d216078297b48aChris Craik
318baf412994612ae72a3db9698b1d216078297b48aChris Craik  switch (pReloc.type()) {
319baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_32:
320baf412994612ae72a3db9698b1d216078297b48aChris Craik      // If buiding PIC object (shared library or PIC executable),
321baf412994612ae72a3db9698b1d216078297b48aChris Craik      // a dynamic relocations with RELATIVE type to this location is needed.
322baf412994612ae72a3db9698b1d216078297b48aChris Craik      // Reserve an entry in .rel.dyn
323baf412994612ae72a3db9698b1d216078297b48aChris Craik      if (config().isCodeIndep()) {
324baf412994612ae72a3db9698b1d216078297b48aChris Craik        // set Rel bit
325baf412994612ae72a3db9698b1d216078297b48aChris Craik        rsym->setReserved(rsym->reserved() | ReserveRel);
326baf412994612ae72a3db9698b1d216078297b48aChris Craik        getTarget().checkAndSetHasTextRel(*pSection.getLink());
327baf412994612ae72a3db9698b1d216078297b48aChris Craik        // set up the dyn rel directly
328baf412994612ae72a3db9698b1d216078297b48aChris Craik        helper_DynRel_init(rsym,
329baf412994612ae72a3db9698b1d216078297b48aChris Craik                           *pReloc.targetRef().frag(),
330baf412994612ae72a3db9698b1d216078297b48aChris Craik                           pReloc.targetRef().offset(),
331baf412994612ae72a3db9698b1d216078297b48aChris Craik                           llvm::ELF::R_386_RELATIVE,
332baf412994612ae72a3db9698b1d216078297b48aChris Craik                           *this);
333baf412994612ae72a3db9698b1d216078297b48aChris Craik      }
334baf412994612ae72a3db9698b1d216078297b48aChris Craik      return;
335baf412994612ae72a3db9698b1d216078297b48aChris Craik
336baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_16:
337baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_8:
338baf412994612ae72a3db9698b1d216078297b48aChris Craik      // If buiding PIC object (shared library or PIC executable),
339baf412994612ae72a3db9698b1d216078297b48aChris Craik      // a dynamic relocations with RELATIVE type to this location is needed.
340baf412994612ae72a3db9698b1d216078297b48aChris Craik      // Reserve an entry in .rel.dyn
341baf412994612ae72a3db9698b1d216078297b48aChris Craik      if (config().isCodeIndep()) {
342baf412994612ae72a3db9698b1d216078297b48aChris Craik        // set up the dyn rel directly
343baf412994612ae72a3db9698b1d216078297b48aChris Craik        helper_DynRel_init(rsym,
344baf412994612ae72a3db9698b1d216078297b48aChris Craik                           *pReloc.targetRef().frag(),
345baf412994612ae72a3db9698b1d216078297b48aChris Craik                           pReloc.targetRef().offset(),
346baf412994612ae72a3db9698b1d216078297b48aChris Craik                           pReloc.type(),
347baf412994612ae72a3db9698b1d216078297b48aChris Craik                           *this);
348baf412994612ae72a3db9698b1d216078297b48aChris Craik        // set Rel bit
349baf412994612ae72a3db9698b1d216078297b48aChris Craik        rsym->setReserved(rsym->reserved() | ReserveRel);
350baf412994612ae72a3db9698b1d216078297b48aChris Craik        getTarget().checkAndSetHasTextRel(*pSection.getLink());
351baf412994612ae72a3db9698b1d216078297b48aChris Craik      }
352baf412994612ae72a3db9698b1d216078297b48aChris Craik      return;
353baf412994612ae72a3db9698b1d216078297b48aChris Craik
354baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_PLT32:
355baf412994612ae72a3db9698b1d216078297b48aChris Craik      return;
356baf412994612ae72a3db9698b1d216078297b48aChris Craik
357baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_GOTOFF:
358baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_GOTPC:
359baf412994612ae72a3db9698b1d216078297b48aChris Craik      // FIXME: A GOT section is needed
360baf412994612ae72a3db9698b1d216078297b48aChris Craik      return;
361baf412994612ae72a3db9698b1d216078297b48aChris Craik
362baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_GOT32:
363baf412994612ae72a3db9698b1d216078297b48aChris Craik      // Symbol needs GOT entry, reserve entry in .got
364baf412994612ae72a3db9698b1d216078297b48aChris Craik      // return if we already create GOT for this symbol
365baf412994612ae72a3db9698b1d216078297b48aChris Craik      if (rsym->reserved() & ReserveGOT)
366baf412994612ae72a3db9698b1d216078297b48aChris Craik        return;
367baf412994612ae72a3db9698b1d216078297b48aChris Craik
368baf412994612ae72a3db9698b1d216078297b48aChris Craik      // FIXME: check STT_GNU_IFUNC symbol
369baf412994612ae72a3db9698b1d216078297b48aChris Craik
370baf412994612ae72a3db9698b1d216078297b48aChris Craik      // If building PIC object, a dynamic relocation with
371baf412994612ae72a3db9698b1d216078297b48aChris Craik      // type RELATIVE is needed to relocate this GOT entry.
372baf412994612ae72a3db9698b1d216078297b48aChris Craik      if (config().isCodeIndep())
373baf412994612ae72a3db9698b1d216078297b48aChris Craik        helper_GOT_init(pReloc, true, *this);
374baf412994612ae72a3db9698b1d216078297b48aChris Craik      else
375baf412994612ae72a3db9698b1d216078297b48aChris Craik        helper_GOT_init(pReloc, false, *this);
376baf412994612ae72a3db9698b1d216078297b48aChris Craik
377baf412994612ae72a3db9698b1d216078297b48aChris Craik      // set GOT bit
378baf412994612ae72a3db9698b1d216078297b48aChris Craik      rsym->setReserved(rsym->reserved() | ReserveGOT);
379baf412994612ae72a3db9698b1d216078297b48aChris Craik      return;
380baf412994612ae72a3db9698b1d216078297b48aChris Craik
381baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_PC32:
382baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_PC16:
383baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_PC8:
384baf412994612ae72a3db9698b1d216078297b48aChris Craik      return;
385baf412994612ae72a3db9698b1d216078297b48aChris Craik
386baf412994612ae72a3db9698b1d216078297b48aChris Craik    case llvm::ELF::R_386_TLS_GD: {
387baf412994612ae72a3db9698b1d216078297b48aChris Craik      // FIXME: no linker optimization for TLS relocation
388baf412994612ae72a3db9698b1d216078297b48aChris Craik      if (rsym->reserved() & ReserveGOT)
389baf412994612ae72a3db9698b1d216078297b48aChris Craik        return;
390
391      // define the section symbol for .tdata or .tbss
392      // the target symbol of the created dynamic relocation should be the
393      // section symbol of the section which this symbol defined. so we
394      // need to define that section symbol here
395      ELFFileFormat* file_format = getTarget().getOutputFormat();
396      const LDSection* sym_sect =
397          &rsym->outSymbol()->fragRef()->frag()->getParent()->getSection();
398      LDSymbol* sect_sym = NULL;
399      if (&file_format->getTData() == sym_sect) {
400        if (!getTarget().hasTDATASymbol()) {
401          sect_sym = pModule.getSectionSymbolSet().get(*sym_sect);
402          getTarget().setTDATASymbol(*sect_sym);
403        }
404      } else if (&file_format->getTBSS() == sym_sect || rsym->isCommon()) {
405        if (!getTarget().hasTBSSSymbol()) {
406          sect_sym = pModule.getSectionSymbolSet().get(*sym_sect);
407          getTarget().setTBSSSymbol(*sect_sym);
408        }
409      } else {
410        error(diag::invalid_tls) << rsym->name() << sym_sect->name();
411      }
412
413      // set up a pair of got entries and a dyn rel
414      // set GOT bit
415      rsym->setReserved(rsym->reserved() | ReserveGOT);
416      X86_32GOTEntry* got_entry1 = getTarget().getGOT().create();
417      X86_32GOTEntry* got_entry2 = getTarget().getGOT().create();
418      getSymGOTMap().record(*rsym, *got_entry1, *got_entry2);
419      // set up value of got entries, the value of got_entry2 should be the
420      // symbol value, which has to be set during apply relocation
421      got_entry1->setValue(0x0);
422
423      // setup dyn rel for got_entry1
424      Relocation& rel_entry1 = helper_DynRel_init(
425          rsym, *got_entry1, 0x0, llvm::ELF::R_386_TLS_DTPMOD32, *this);
426      // for local tls symbol, add rel entry against the section symbol this
427      // symbol belong to (.tdata or .tbss)
428      rel_entry1.setSymInfo(sect_sym->resolveInfo());
429      return;
430    }
431    case llvm::ELF::R_386_TLS_LDM:
432      getTLSModuleID();
433      return;
434
435    case llvm::ELF::R_386_TLS_LDO_32:
436      return;
437
438    case llvm::ELF::R_386_TLS_IE: {
439      getTarget().setHasStaticTLS();
440
441      // if building shared object, a RELATIVE dynamic relocation is needed
442      if (LinkerConfig::DynObj == config().codeGenType()) {
443        helper_DynRel_init(rsym,
444                           *pReloc.targetRef().frag(),
445                           pReloc.targetRef().offset(),
446                           llvm::ELF::R_386_RELATIVE,
447                           *this);
448        rsym->setReserved(rsym->reserved() | ReserveRel);
449        getTarget().checkAndSetHasTextRel(*pSection.getLink());
450      } else {
451        // for local sym, we can convert ie to le if not building shared object
452        convertTLSIEtoLE(pReloc, pSection);
453        return;
454      }
455
456      if (rsym->reserved() & ReserveGOT)
457        return;
458
459      // set up the got and the corresponding rel entry
460      X86_32GOTEntry* got_entry = getTarget().getGOT().create();
461      getSymGOTMap().record(*rsym, *got_entry);
462      got_entry->setValue(0x0);
463      helper_DynRel_init(
464          rsym, *got_entry, 0x0, llvm::ELF::R_386_TLS_TPOFF, *this);
465      // set GOT bit
466      rsym->setReserved(rsym->reserved() | ReserveGOT);
467      // add symbol to dyn sym table
468      getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
469      return;
470    }
471
472    case llvm::ELF::R_386_TLS_GOTIE: {
473      getTarget().setHasStaticTLS();
474      if (rsym->reserved() & ReserveGOT)
475        return;
476      // set up the got and the corresponding dyn rel
477      X86_32GOTEntry* got_entry = getTarget().getGOT().create();
478      getSymGOTMap().record(*rsym, *got_entry);
479      got_entry->setValue(0x0);
480      helper_DynRel_init(
481          rsym, *got_entry, 0x0, llvm::ELF::R_386_TLS_TPOFF, *this);
482      // set GOT bit
483      rsym->setReserved(rsym->reserved() | ReserveGOT);
484      getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
485      return;
486    }
487
488    case llvm::ELF::R_386_TLS_LE:
489    case llvm::ELF::R_386_TLS_LE_32:
490      getTarget().setHasStaticTLS();
491      // if buildint shared object, a dynamic relocation is needed
492      if (LinkerConfig::DynObj == config().codeGenType()) {
493        helper_DynRel_init(rsym,
494                           *pReloc.targetRef().frag(),
495                           pReloc.targetRef().offset(),
496                           llvm::ELF::R_386_TLS_TPOFF,
497                           *this);
498        rsym->setReserved(rsym->reserved() | ReserveRel);
499        getTarget().checkAndSetHasTextRel(*pSection.getLink());
500        // the target symbol of the dynamic relocation is rsym, so we need to
501        // emit it into .dynsym
502        assert(rsym->outSymbol() != NULL);
503        getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
504      }
505      return;
506
507    default:
508      fatal(diag::unsupported_relocation) << static_cast<int>(pReloc.type())
509                                          << "mclinker@googlegroups.com";
510      break;
511  }  // end switch
512}
513
514void X86_32Relocator::scanGlobalReloc(Relocation& pReloc,
515                                      IRBuilder& pBuilder,
516                                      Module& pModule,
517                                      LDSection& pSection) {
518  // rsym - The relocation target symbol
519  ResolveInfo* rsym = pReloc.symInfo();
520
521  switch (pReloc.type()) {
522    case llvm::ELF::R_386_32:
523    case llvm::ELF::R_386_16:
524    case llvm::ELF::R_386_8:
525      // Absolute relocation type, symbol may needs PLT entry or
526      // dynamic relocation entry
527      if (getTarget().symbolNeedsPLT(*rsym)) {
528        // create plt for this symbol if it does not have one
529        if (!(rsym->reserved() & ReservePLT)) {
530          // Symbol needs PLT entry, we need a PLT entry
531          // and the corresponding GOT and dynamic relocation entry
532          // in .got and .rel.plt.
533          helper_PLT_init(pReloc, *this);
534          // set PLT bit
535          rsym->setReserved(rsym->reserved() | ReservePLT);
536        }
537      }
538
539      if (getTarget()
540              .symbolNeedsDynRel(
541                  *rsym, (rsym->reserved() & ReservePLT), true)) {
542        // symbol needs dynamic relocation entry, set up the dynrel entry
543        if (getTarget().symbolNeedsCopyReloc(pReloc, *rsym)) {
544          LDSymbol& cpy_sym =
545              defineSymbolforCopyReloc(pBuilder, *rsym, getTarget());
546          addCopyReloc(*cpy_sym.resolveInfo(), getTarget());
547        } else {
548          // set Rel bit and the dyn rel
549          rsym->setReserved(rsym->reserved() | ReserveRel);
550          getTarget().checkAndSetHasTextRel(*pSection.getLink());
551          if (llvm::ELF::R_386_32 == pReloc.type() &&
552              helper_use_relative_reloc(*rsym, *this)) {
553            helper_DynRel_init(rsym,
554                               *pReloc.targetRef().frag(),
555                               pReloc.targetRef().offset(),
556                               llvm::ELF::R_386_RELATIVE,
557                               *this);
558          } else {
559            helper_DynRel_init(rsym,
560                               *pReloc.targetRef().frag(),
561                               pReloc.targetRef().offset(),
562                               pReloc.type(),
563                               *this);
564          }
565        }
566      }
567      return;
568
569    case llvm::ELF::R_386_GOTOFF:
570    case llvm::ELF::R_386_GOTPC: {
571      // FIXME: A GOT section is needed
572      return;
573    }
574
575    case llvm::ELF::R_386_PLT32:
576      // A PLT entry is needed when building shared library
577
578      // return if we already create plt for this symbol
579      if (rsym->reserved() & ReservePLT)
580        return;
581
582      // if the symbol's value can be decided at link time, then no need plt
583      if (getTarget().symbolFinalValueIsKnown(*rsym))
584        return;
585
586      // if symbol is defined in the ouput file and it's not
587      // preemptible, no need plt
588      if (rsym->isDefine() && !rsym->isDyn() &&
589          !getTarget().isSymbolPreemptible(*rsym))
590        return;
591
592      // Symbol needs PLT entry, we need a PLT entry
593      // and the corresponding GOT and dynamic relocation entry
594      // in .got and .rel.plt
595      helper_PLT_init(pReloc, *this);
596      // set PLT bit
597      rsym->setReserved(rsym->reserved() | ReservePLT);
598      return;
599
600    case llvm::ELF::R_386_GOT32:
601      // Symbol needs GOT entry, reserve entry in .got
602      // return if we already create GOT for this symbol
603      if (rsym->reserved() & ReserveGOT)
604        return;
605      // if the symbol cannot be fully resolved at link time, then we need a
606      // dynamic relocation
607      if (!getTarget().symbolFinalValueIsKnown(*rsym))
608        helper_GOT_init(pReloc, true, *this);
609      else
610        helper_GOT_init(pReloc, false, *this);
611      // set GOT bit
612      rsym->setReserved(rsym->reserved() | ReserveGOT);
613      return;
614
615    case llvm::ELF::R_386_PC32:
616    case llvm::ELF::R_386_PC16:
617    case llvm::ELF::R_386_PC8:
618
619      if (getTarget().symbolNeedsPLT(*rsym) &&
620          LinkerConfig::DynObj != config().codeGenType()) {
621        // create plt for this symbol if it does not have one
622        if (!(rsym->reserved() & ReservePLT)) {
623          // Symbol needs PLT entry, we need a PLT entry
624          // and the corresponding GOT and dynamic relocation entry
625          // in .got and .rel.plt.
626          // set PLT bit
627          helper_PLT_init(pReloc, *this);
628          rsym->setReserved(rsym->reserved() | ReservePLT);
629        }
630      }
631
632      if (getTarget()
633              .symbolNeedsDynRel(
634                  *rsym, (rsym->reserved() & ReservePLT), false)) {
635        // symbol needs dynamic relocation entry, setup an entry in .rel.dyn
636        if (getTarget().symbolNeedsCopyReloc(pReloc, *rsym)) {
637          LDSymbol& cpy_sym =
638              defineSymbolforCopyReloc(pBuilder, *rsym, getTarget());
639          addCopyReloc(*cpy_sym.resolveInfo(), getTarget());
640        } else {
641          // set Rel bit and the dyn rel
642          rsym->setReserved(rsym->reserved() | ReserveRel);
643          getTarget().checkAndSetHasTextRel(*pSection.getLink());
644          if (llvm::ELF::R_386_32 == pReloc.type() &&
645              helper_use_relative_reloc(*rsym, *this)) {
646            helper_DynRel_init(rsym,
647                               *pReloc.targetRef().frag(),
648                               pReloc.targetRef().offset(),
649                               llvm::ELF::R_386_RELATIVE,
650                               *this);
651          } else {
652            helper_DynRel_init(rsym,
653                               *pReloc.targetRef().frag(),
654                               pReloc.targetRef().offset(),
655                               pReloc.type(),
656                               *this);
657          }
658        }
659      }
660      return;
661
662    case llvm::ELF::R_386_TLS_GD: {
663      // FIXME: no linker optimization for TLS relocation
664      if (rsym->reserved() & ReserveGOT)
665        return;
666
667      // set up a pair of got entries and a pair of dyn rel
668      X86_32GOTEntry* got_entry1 = getTarget().getGOT().create();
669      X86_32GOTEntry* got_entry2 = getTarget().getGOT().create();
670      getSymGOTMap().record(*rsym, *got_entry1, *got_entry2);
671      got_entry1->setValue(0x0);
672      got_entry2->setValue(0x0);
673      // setup dyn rel for got entries against rsym
674      helper_DynRel_init(
675          rsym, *got_entry1, 0x0, llvm::ELF::R_386_TLS_DTPMOD32, *this);
676      helper_DynRel_init(
677          rsym, *got_entry2, 0x0, llvm::ELF::R_386_TLS_DTPOFF32, *this);
678
679      // add the rsym to dynamic symbol table
680      getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
681      // set GOT bit
682      rsym->setReserved(rsym->reserved() | ReserveGOT);
683      return;
684    }
685
686    case llvm::ELF::R_386_TLS_LDM:
687      getTLSModuleID();
688      return;
689
690    case llvm::ELF::R_386_TLS_LDO_32:
691      return;
692
693    case llvm::ELF::R_386_TLS_IE: {
694      getTarget().setHasStaticTLS();
695      // if buildint shared object, a RELATIVE dynamic relocation is needed
696      if (LinkerConfig::DynObj == config().codeGenType()) {
697        helper_DynRel_init(rsym,
698                           *pReloc.targetRef().frag(),
699                           pReloc.targetRef().offset(),
700                           llvm::ELF::R_386_RELATIVE,
701                           *this);
702        rsym->setReserved(rsym->reserved() | ReserveRel);
703        getTarget().checkAndSetHasTextRel(*pSection.getLink());
704      } else {
705        // for global sym, we can convert ie to le if its final value is known
706        if (getTarget().symbolFinalValueIsKnown(*rsym)) {
707          convertTLSIEtoLE(pReloc, pSection);
708          return;
709        }
710      }
711      if (rsym->reserved() & ReserveGOT)
712        return;
713      // set up the got and the corresponding rel entry
714      X86_32GOTEntry* got_entry = getTarget().getGOT().create();
715      getSymGOTMap().record(*rsym, *got_entry);
716      got_entry->setValue(0x0);
717      helper_DynRel_init(
718          rsym, *got_entry, 0x0, llvm::ELF::R_386_TLS_TPOFF, *this);
719      // set GOT bit
720      rsym->setReserved(rsym->reserved() | ReserveGOT);
721      return;
722    }
723
724    case llvm::ELF::R_386_TLS_GOTIE: {
725      getTarget().setHasStaticTLS();
726      if (rsym->reserved() & ReserveGOT)
727        return;
728      // set up the got and the corresponding dyn rel
729      X86_32GOTEntry* got_entry = getTarget().getGOT().create();
730      getSymGOTMap().record(*rsym, *got_entry);
731      got_entry->setValue(0x0);
732      helper_DynRel_init(
733          rsym, *got_entry, 0x0, llvm::ELF::R_386_TLS_TPOFF, *this);
734      getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
735      // set GOT bit
736      rsym->setReserved(rsym->reserved() | ReserveGOT);
737      return;
738    }
739
740    case llvm::ELF::R_386_TLS_LE:
741    case llvm::ELF::R_386_TLS_LE_32:
742      getTarget().setHasStaticTLS();
743      // if buildint shared object, a dynamic relocation is needed
744      if (LinkerConfig::DynObj == config().codeGenType()) {
745        helper_DynRel_init(rsym,
746                           *pReloc.targetRef().frag(),
747                           pReloc.targetRef().offset(),
748                           llvm::ELF::R_386_TLS_TPOFF,
749                           *this);
750        getTarget().getRelDyn().addSymbolToDynSym(*rsym->outSymbol());
751        rsym->setReserved(rsym->reserved() | ReserveRel);
752        getTarget().checkAndSetHasTextRel(*pSection.getLink());
753      }
754      return;
755
756    default: {
757      fatal(diag::unsupported_relocation) << static_cast<int>(pReloc.type())
758                                          << "mclinker@googlegroups.com";
759      break;
760    }
761  }  // end switch
762}
763
764// Create a GOT entry for the TLS module index
765X86_32GOTEntry& X86_32Relocator::getTLSModuleID() {
766  static X86_32GOTEntry* got_entry = NULL;
767  if (got_entry != NULL)
768    return *got_entry;
769
770  // Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
771  got_entry = getTarget().getGOT().create();
772  getTarget().getGOT().create()->setValue(0x0);
773
774  helper_DynRel_init(
775      NULL, *got_entry, 0x0, llvm::ELF::R_386_TLS_DTPMOD32, *this);
776  return *got_entry;
777}
778
779/// convert R_386_TLS_IE to R_386_TLS_LE
780void X86_32Relocator::convertTLSIEtoLE(Relocation& pReloc,
781                                       LDSection& pSection) {
782  assert(pReloc.type() == llvm::ELF::R_386_TLS_IE);
783  assert(pReloc.targetRef().frag() != NULL);
784
785  // 1. create the new relocs
786  Relocation* reloc =
787      Relocation::Create(X86_32Relocator::R_386_TLS_OPT,
788                         *FragmentRef::Create(*pReloc.targetRef().frag(),
789                                              pReloc.targetRef().offset() - 1),
790                         0x0);
791  // FIXME: should we create a special symbol for the tls opt instead?
792  reloc->setSymInfo(pReloc.symInfo());
793
794  // 2. modify the opcodes to the appropriate ones
795  uint8_t* op = (reinterpret_cast<uint8_t*>(&reloc->target()));
796  if (op[0] == 0xa1) {
797    op[0] = 0xb8;
798  } else {
799    // create the new reloc (move 1 byte forward).
800    reloc = Relocation::Create(
801        X86_32Relocator::R_386_TLS_OPT,
802        *FragmentRef::Create(*pReloc.targetRef().frag(),
803                             pReloc.targetRef().offset() - 2),
804        0x0);
805    reloc->setSymInfo(pReloc.symInfo());
806    op = (reinterpret_cast<uint8_t*>(&reloc->target()));
807    switch (op[0]) {
808      case 0x8b:
809        assert((op[1] & 0xc7) == 0x05);
810        op[0] = 0xc7;
811        op[1] = 0xc0 | ((op[1] >> 3) & 7);
812        break;
813      case 0x03:
814        assert((op[1] & 0xc7) == 0x05);
815        op[0] = 0x81;
816        op[1] = 0xc0 | ((op[1] >> 3) & 7);
817        break;
818      default:
819        assert(0);
820        break;
821    }
822  }
823
824  // 3. insert the new relocs "BEFORE" the original reloc.
825  assert(reloc != NULL);
826  pSection.getRelocData()->getRelocationList().insert(
827      RelocData::iterator(pReloc), reloc);
828
829  // 4. change the type of the original reloc
830  pReloc.setType(llvm::ELF::R_386_TLS_LE);
831}
832
833uint32_t X86_32Relocator::getDebugStringOffset(Relocation& pReloc) const {
834  if (pReloc.type() != llvm::ELF::R_386_32)
835    error(diag::unsupport_reloc_for_debug_string)
836        << getName(pReloc.type()) << "mclinker@googlegroups.com";
837
838  if (pReloc.symInfo()->type() == ResolveInfo::Section)
839    return pReloc.target() + pReloc.addend();
840  else
841    return pReloc.symInfo()->outSymbol()->fragRef()->offset() +
842               pReloc.target() + pReloc.addend();
843}
844
845void X86_32Relocator::applyDebugStringOffset(Relocation& pReloc,
846                                             uint32_t pOffset) {
847  pReloc.target() = pOffset;
848}
849
850//================================================//
851// X86_32 Each relocation function implementation //
852//================================================//
853
854// R_386_NONE
855Relocator::Result none(Relocation& pReloc, X86_32Relocator& pParent) {
856  return Relocator::OK;
857}
858
859// R_386_32: S + A
860// R_386_16
861// R_386_8
862Relocator::Result abs(Relocation& pReloc, X86_32Relocator& pParent) {
863  ResolveInfo* rsym = pReloc.symInfo();
864  Relocator::DWord A = pReloc.target() + pReloc.addend();
865  Relocator::DWord S = pReloc.symValue();
866  bool has_dyn_rel = pParent.getTarget().symbolNeedsDynRel(
867      *rsym, (rsym->reserved() & X86Relocator::ReservePLT), true);
868
869  LDSection& target_sect = pReloc.targetRef().frag()->getParent()->getSection();
870  // If the flag of target section is not ALLOC, we will not scan this
871  // relocation but perform static relocation. (e.g., applying .debug section)
872  if ((llvm::ELF::SHF_ALLOC & target_sect.flag()) == 0x0) {
873    pReloc.target() = S + A;
874    return Relocator::OK;
875  }
876
877  // An external symbol may need PLT and dynamic relocation
878  if (!rsym->isLocal()) {
879    if (rsym->reserved() & X86Relocator::ReservePLT) {
880      S = helper_get_PLT_address(*rsym, pParent);
881    }
882    // If we generate a dynamic relocation (except R_386_RELATIVE)
883    // for a place, we should not perform static relocation on it
884    // in order to keep the addend store in the place correct.
885    if (has_dyn_rel)
886      if (llvm::ELF::R_386_32 != pReloc.type() ||
887          (!helper_use_relative_reloc(*rsym, pParent)))
888        return Relocator::OK;
889  }
890
891  // perform static relocation
892  pReloc.target() = S + A;
893  return Relocator::OK;
894}
895
896// R_386_PC32: S + A - P
897// R_386_PC16
898// R_386_PC8
899Relocator::Result rel(Relocation& pReloc, X86_32Relocator& pParent) {
900  ResolveInfo* rsym = pReloc.symInfo();
901  Relocator::DWord A = pReloc.target() + pReloc.addend();
902  Relocator::DWord S = pReloc.symValue();
903  Relocator::DWord P = pReloc.place();
904  bool has_dyn_rel = pParent.getTarget().symbolNeedsDynRel(
905      *rsym, (rsym->reserved() & X86Relocator::ReservePLT), true);
906
907  LDSection& target_sect = pReloc.targetRef().frag()->getParent()->getSection();
908  // If the flag of target section is not ALLOC, we will not scan this
909  // relocation
910  // but perform static relocation. (e.g., applying .debug section)
911  if ((llvm::ELF::SHF_ALLOC & target_sect.flag()) == 0x0) {
912    pReloc.target() = S + A - P;
913    return Relocator::OK;
914  }
915
916  // An external symbol may need PLT and dynamic relocation
917  if (!rsym->isLocal()) {
918    if (rsym->reserved() & X86Relocator::ReservePLT) {
919      S = helper_get_PLT_address(*rsym, pParent);
920      pReloc.target() = S + A - P;
921    }
922    if (has_dyn_rel)
923      if (!helper_use_relative_reloc(*rsym, pParent))
924        return Relocator::OK;
925  }
926
927  // perform static relocation
928  pReloc.target() = S + A - P;
929  return Relocator::OK;
930}
931
932// R_386_GOTOFF: S + A - GOT_ORG
933Relocator::Result gotoff32(Relocation& pReloc, X86_32Relocator& pParent) {
934  Relocator::DWord A = pReloc.target() + pReloc.addend();
935  Relocator::Address GOT_ORG = helper_GOT_ORG(pParent);
936  Relocator::Address S = pReloc.symValue();
937
938  pReloc.target() = S + A - GOT_ORG;
939  return Relocator::OK;
940}
941
942// R_386_GOTPC: GOT_ORG + A - P
943Relocator::Result gotpc32(Relocation& pReloc, X86_32Relocator& pParent) {
944  Relocator::DWord A = pReloc.target() + pReloc.addend();
945  Relocator::Address GOT_ORG = helper_GOT_ORG(pParent);
946  // Apply relocation.
947  pReloc.target() = GOT_ORG + A - pReloc.place();
948  return Relocator::OK;
949}
950
951// R_386_GOT32: GOT(S) + A - GOT_ORG
952Relocator::Result got32(Relocation& pReloc, X86_32Relocator& pParent) {
953  ResolveInfo* rsym = pReloc.symInfo();
954  if (!(rsym->reserved() & (X86Relocator::ReserveGOT)))
955    return Relocator::BadReloc;
956
957  // set up got entry value if the got has no dyn rel or
958  // the dyn rel is RELATIVE
959  X86_32GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*pReloc.symInfo());
960  assert(got_entry != NULL);
961  if (got_entry->getValue() == X86Relocator::SymVal)
962    got_entry->setValue(pReloc.symValue());
963
964  Relocator::Address GOT_S = helper_get_GOT_address(pReloc, pParent);
965  Relocator::DWord A = pReloc.target() + pReloc.addend();
966  Relocator::Address GOT_ORG = helper_GOT_ORG(pParent);
967  // Apply relocation.
968  pReloc.target() = GOT_S + A - GOT_ORG;
969  return Relocator::OK;
970}
971
972// R_386_PLT32: PLT(S) + A - P
973Relocator::Result plt32(Relocation& pReloc, X86_32Relocator& pParent) {
974  // PLT_S depends on if there is a PLT entry.
975  Relocator::Address PLT_S;
976  if ((pReloc.symInfo()->reserved() & X86Relocator::ReservePLT))
977    PLT_S = helper_get_PLT_address(*pReloc.symInfo(), pParent);
978  else
979    PLT_S = pReloc.symValue();
980  Relocator::DWord A = pReloc.target() + pReloc.addend();
981  Relocator::Address P = pReloc.place();
982  pReloc.target() = PLT_S + A - P;
983  return Relocator::OK;
984}
985
986// R_386_TLS_GD:
987Relocator::Result tls_gd(Relocation& pReloc, X86_32Relocator& pParent) {
988  // global-dynamic
989  ResolveInfo* rsym = pReloc.symInfo();
990  // must reserve two pairs of got and dynamic relocation
991  if (!(rsym->reserved() & X86Relocator::ReserveGOT))
992    return Relocator::BadReloc;
993
994  ELFFileFormat* file_format = pParent.getTarget().getOutputFormat();
995  // setup corresponding got and dynamic relocatio entries:
996  // get first got entry, if there is already a got entry for rsym, then apply
997  // this relocation to the got entry directly. If not, setup the corresponding
998  // got and dyn relocation entries
999  X86_32GOTEntry* got_entry1 = pParent.getSymGOTMap().lookUpFirstEntry(*rsym);
1000
1001  // set the got_entry2 value to symbol value
1002  if (rsym->isLocal())
1003    pParent.getSymGOTMap().lookUpSecondEntry(*rsym)->setValue(
1004        pReloc.symValue());
1005
1006  // perform relocation to the first got entry
1007  Relocator::DWord A = pReloc.target() + pReloc.addend();
1008  // GOT_OFF - the offset between the got_entry1 and _GLOBAL_OFFSET_TABLE (the
1009  // .got.plt section)
1010  Relocator::Address GOT_OFF = file_format->getGOT().addr() +
1011                               got_entry1->getOffset() -
1012                               file_format->getGOTPLT().addr();
1013  pReloc.target() = GOT_OFF + A;
1014  return Relocator::OK;
1015}
1016
1017// R_386_TLS_LDM
1018Relocator::Result tls_ldm(Relocation& pReloc, X86_32Relocator& pParent) {
1019  // FIXME: no linker optimization for TLS relocation
1020  const X86_32GOTEntry& got_entry = pParent.getTLSModuleID();
1021
1022  // All GOT offsets are relative to the end of the GOT.
1023  X86Relocator::SWord GOT_S =
1024      got_entry.getOffset() - (pParent.getTarget().getGOTPLT().addr() -
1025                               pParent.getTarget().getGOT().addr());
1026  Relocator::DWord A = pReloc.target() + pReloc.addend();
1027  pReloc.target() = GOT_S + A;
1028
1029  return Relocator::OK;
1030}
1031
1032// R_386_TLS_LDO_32
1033Relocator::Result tls_ldo_32(Relocation& pReloc, X86_32Relocator& pParent) {
1034  // FIXME: no linker optimization for TLS relocation
1035  Relocator::DWord A = pReloc.target() + pReloc.addend();
1036  Relocator::Address S = pReloc.symValue();
1037  pReloc.target() = S + A;
1038  return Relocator::OK;
1039}
1040
1041// R_X86_TLS_IE
1042Relocator::Result tls_ie(Relocation& pReloc, X86_32Relocator& pParent) {
1043  ResolveInfo* rsym = pReloc.symInfo();
1044  if (!(rsym->reserved() & X86Relocator::ReserveGOT)) {
1045    return Relocator::BadReloc;
1046  }
1047
1048  // set up the got and dynamic relocation entries if not exist
1049  X86_32GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*rsym);
1050  assert(got_entry != NULL);
1051  // perform relocation to the absolute address of got_entry
1052  Relocator::Address GOT_S =
1053      pParent.getTarget().getGOT().addr() + got_entry->getOffset();
1054
1055  Relocator::DWord A = pReloc.target() + pReloc.addend();
1056  pReloc.target() = GOT_S + A;
1057
1058  return Relocator::OK;
1059}
1060
1061// R_386_TLS_GOTIE
1062Relocator::Result tls_gotie(Relocation& pReloc, X86_32Relocator& pParent) {
1063  ResolveInfo* rsym = pReloc.symInfo();
1064  if (!(rsym->reserved() & X86Relocator::ReserveGOT)) {
1065    return Relocator::BadReloc;
1066  }
1067
1068  // set up the got and dynamic relocation entries if not exist
1069  X86_32GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*rsym);
1070  assert(got_entry != NULL);
1071
1072  // All GOT offsets are relative to the end of the GOT.
1073  X86Relocator::SWord GOT_S =
1074      got_entry->getOffset() - (pParent.getTarget().getGOTPLT().addr() -
1075                                pParent.getTarget().getGOT().addr());
1076  Relocator::DWord A = pReloc.target() + pReloc.addend();
1077  pReloc.target() = GOT_S + A;
1078
1079  return Relocator::OK;
1080}
1081
1082// R_X86_TLS_LE
1083Relocator::Result tls_le(Relocation& pReloc, X86_32Relocator& pParent) {
1084  if (pReloc.symInfo()->reserved() & X86Relocator::ReserveRel)
1085    return Relocator::OK;
1086
1087  // perform static relocation
1088  // get TLS segment
1089  ELFSegmentFactory::const_iterator tls_seg =
1090      pParent.getTarget().elfSegmentTable().find(
1091          llvm::ELF::PT_TLS, llvm::ELF::PF_R, 0x0);
1092  assert(tls_seg != pParent.getTarget().elfSegmentTable().end());
1093  Relocator::DWord A = pReloc.target() + pReloc.addend();
1094  Relocator::Address S = pReloc.symValue();
1095  pReloc.target() = S + A - (*tls_seg)->memsz();
1096  return Relocator::OK;
1097}
1098
1099Relocator::Result unsupported(Relocation& pReloc, X86_32Relocator& pParent) {
1100  return Relocator::Unsupported;
1101}
1102
1103//===--------------------------------------------------------------------===//
1104// X86_64 Relocation helper function
1105//===--------------------------------------------------------------------===//
1106/// helper_DynRel - Get an relocation entry in .rela.dyn
1107static Relocation& helper_DynRel_init(ResolveInfo* pSym,
1108                                      Fragment& pFrag,
1109                                      uint64_t pOffset,
1110                                      Relocator::Type pType,
1111                                      X86_64Relocator& pParent) {
1112  X86_64GNULDBackend& ld_backend = pParent.getTarget();
1113  Relocation& rel_entry = *ld_backend.getRelDyn().create();
1114  rel_entry.setType(pType);
1115  rel_entry.targetRef().assign(pFrag, pOffset);
1116  if (pType == llvm::ELF::R_X86_64_RELATIVE || pSym == NULL)
1117    rel_entry.setSymInfo(NULL);
1118  else
1119    rel_entry.setSymInfo(pSym);
1120
1121  return rel_entry;
1122}
1123
1124/// helper_use_relative_reloc - Check if symbol can use relocation
1125/// R_X86_64_RELATIVE
1126static bool helper_use_relative_reloc(const ResolveInfo& pSym,
1127                                      const X86_64Relocator& pFactory) {
1128  // if symbol is dynamic or undefine or preemptible
1129  if (pSym.isDyn() || pSym.isUndef() ||
1130      pFactory.getTarget().isSymbolPreemptible(pSym))
1131    return false;
1132  return true;
1133}
1134
1135static X86_64GOTEntry& helper_GOT_init(Relocation& pReloc,
1136                                       bool pHasRel,
1137                                       X86_64Relocator& pParent) {
1138  // rsym - The relocation target symbol
1139  ResolveInfo* rsym = pReloc.symInfo();
1140  X86_64GNULDBackend& ld_backend = pParent.getTarget();
1141  assert(pParent.getSymGOTMap().lookUp(*rsym) == NULL);
1142
1143  X86_64GOTEntry* got_entry = ld_backend.getGOT().create();
1144  pParent.getSymGOTMap().record(*rsym, *got_entry);
1145
1146  // If we first get this GOT entry, we should initialize it.
1147  if (!pHasRel) {
1148    // No corresponding dynamic relocation, initialize to the symbol value.
1149    got_entry->setValue(X86Relocator::SymVal);
1150  } else {
1151    // Initialize got_entry content and the corresponding dynamic relocation.
1152    if (helper_use_relative_reloc(*rsym, pParent)) {
1153      Relocation& rel_entry = helper_DynRel_init(
1154          rsym, *got_entry, 0x0, llvm::ELF::R_X86_64_RELATIVE, pParent);
1155      rel_entry.setAddend(X86Relocator::SymVal);
1156      pParent.getRelRelMap().record(pReloc, rel_entry);
1157    } else {
1158      helper_DynRel_init(
1159          rsym, *got_entry, 0x0, llvm::ELF::R_X86_64_GLOB_DAT, pParent);
1160    }
1161    got_entry->setValue(0);
1162  }
1163  return *got_entry;
1164}
1165
1166static Relocator::Address helper_GOT_ORG(X86_64Relocator& pParent) {
1167  return pParent.getTarget().getGOT().addr();
1168}
1169
1170static Relocator::Address helper_get_GOT_address(Relocation& pReloc,
1171                                                 X86_64Relocator& pParent) {
1172  X86_64GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*pReloc.symInfo());
1173  assert(got_entry != NULL);
1174  return got_entry->getOffset();
1175}
1176
1177static Relocator::Address helper_get_PLT_address(ResolveInfo& pSym,
1178                                                 X86_64Relocator& pParent) {
1179  PLTEntryBase* plt_entry = pParent.getSymPLTMap().lookUp(pSym);
1180  assert(plt_entry != NULL);
1181  return pParent.getTarget().getPLT().addr() + plt_entry->getOffset();
1182}
1183
1184static PLTEntryBase& helper_PLT_init(Relocation& pReloc,
1185                                     X86_64Relocator& pParent) {
1186  // rsym - The relocation target symbol
1187  ResolveInfo* rsym = pReloc.symInfo();
1188  X86_64GNULDBackend& ld_backend = pParent.getTarget();
1189  assert(pParent.getSymPLTMap().lookUp(*rsym) == NULL);
1190
1191  PLTEntryBase* plt_entry = ld_backend.getPLT().create();
1192  pParent.getSymPLTMap().record(*rsym, *plt_entry);
1193
1194  // initialize plt and the corresponding gotplt and dyn rel entry.
1195  assert(pParent.getSymGOTPLTMap().lookUp(*rsym) == NULL &&
1196         "PLT entry not exist, but DynRel entry exist!");
1197  X86_64GOTEntry* gotplt_entry = ld_backend.getGOTPLT().create();
1198  pParent.getSymGOTPLTMap().record(*rsym, *gotplt_entry);
1199
1200  // init the corresponding rel entry in .rel.plt
1201  Relocation& rel_entry = *ld_backend.getRelPLT().create();
1202  rel_entry.setType(llvm::ELF::R_X86_64_JUMP_SLOT);
1203  rel_entry.targetRef().assign(*gotplt_entry);
1204  rel_entry.setSymInfo(rsym);
1205  return *plt_entry;
1206}
1207
1208//===----------------------------------------------------------------------===//
1209// X86_64 Relocation Functions and Tables
1210//===----------------------------------------------------------------------===//
1211DECL_X86_64_APPLY_RELOC_FUNCS
1212
1213/// the prototype of applying function
1214typedef Relocator::Result (*X86_64ApplyFunctionType)(Relocation& pReloc,
1215                                                     X86_64Relocator& pParent);
1216
1217// the table entry of applying functions
1218struct X86_64ApplyFunctionTriple {
1219  X86_64ApplyFunctionType func;
1220  unsigned int type;
1221  const char* name;
1222  unsigned int size;
1223};
1224
1225// declare the table of applying functions
1226static const X86_64ApplyFunctionTriple X86_64ApplyFunctions[] = {
1227    DECL_X86_64_APPLY_RELOC_FUNC_PTRS};
1228
1229//===--------------------------------------------------------------------===//
1230// X86_64Relocator
1231//===--------------------------------------------------------------------===//
1232X86_64Relocator::X86_64Relocator(X86_64GNULDBackend& pParent,
1233                                 const LinkerConfig& pConfig)
1234    : X86Relocator(pConfig), m_Target(pParent) {
1235}
1236
1237Relocator::Result X86_64Relocator::applyRelocation(Relocation& pRelocation) {
1238  Relocation::Type type = pRelocation.type();
1239
1240  if (type >= sizeof(X86_64ApplyFunctions) / sizeof(X86_64ApplyFunctions[0])) {
1241    return Unknown;
1242  }
1243
1244  // apply the relocation
1245  return X86_64ApplyFunctions[type].func(pRelocation, *this);
1246}
1247
1248const char* X86_64Relocator::getName(Relocation::Type pType) const {
1249  return X86_64ApplyFunctions[pType].name;
1250}
1251
1252Relocator::Size X86_64Relocator::getSize(Relocation::Type pType) const {
1253  return X86_64ApplyFunctions[pType].size;
1254}
1255
1256bool X86_64Relocator::mayHaveFunctionPointerAccess(
1257    const Relocation& pReloc) const {
1258  bool possible_funcptr_reloc = false;
1259  switch (pReloc.type()) {
1260    case llvm::ELF::R_X86_64_64:
1261    case llvm::ELF::R_X86_64_32:
1262    case llvm::ELF::R_X86_64_32S:
1263    case llvm::ELF::R_X86_64_16:
1264    case llvm::ELF::R_X86_64_8:
1265    case llvm::ELF::R_X86_64_GOT64:
1266    case llvm::ELF::R_X86_64_GOT32:
1267    case llvm::ELF::R_X86_64_GOTPCREL64:
1268    case llvm::ELF::R_X86_64_GOTPCREL:
1269    case llvm::ELF::R_X86_64_GOTPLT64: {
1270      possible_funcptr_reloc = true;
1271      break;
1272    }
1273    default: {
1274      possible_funcptr_reloc = false;
1275      break;
1276    }
1277  }
1278
1279  if (pReloc.symInfo()->isGlobal()) {
1280    return (config().codeGenType() == LinkerConfig::DynObj) &&
1281           ((pReloc.symInfo()->visibility() != ResolveInfo::Default) ||
1282            possible_funcptr_reloc);
1283  } else {
1284    return (config().codeGenType() == LinkerConfig::DynObj) ||
1285           possible_funcptr_reloc;
1286  }
1287}
1288
1289void X86_64Relocator::scanLocalReloc(Relocation& pReloc,
1290                                     IRBuilder& pBuilder,
1291                                     Module& pModule,
1292                                     LDSection& pSection) {
1293  // rsym - The relocation target symbol
1294  ResolveInfo* rsym = pReloc.symInfo();
1295
1296  switch (pReloc.type()) {
1297    case llvm::ELF::R_X86_64_64:
1298      // If buiding PIC object (shared library or PIC executable),
1299      // a dynamic relocations with RELATIVE type to this location is needed.
1300      // Reserve an entry in .rela.dyn
1301      if (config().isCodeIndep()) {
1302        Relocation& reloc = helper_DynRel_init(rsym,
1303                                               *pReloc.targetRef().frag(),
1304                                               pReloc.targetRef().offset(),
1305                                               llvm::ELF::R_X86_64_RELATIVE,
1306                                               *this);
1307        getRelRelMap().record(pReloc, reloc);
1308        // set Rel bit
1309        rsym->setReserved(rsym->reserved() | ReserveRel);
1310        getTarget().checkAndSetHasTextRel(*pSection.getLink());
1311      }
1312      return;
1313
1314    case llvm::ELF::R_X86_64_32:
1315    case llvm::ELF::R_X86_64_16:
1316    case llvm::ELF::R_X86_64_8:
1317    case llvm::ELF::R_X86_64_32S:
1318      // If buiding PIC object (shared library or PIC executable),
1319      // a dynamic relocations with RELATIVE type to this location is needed.
1320      // Reserve an entry in .rela.dyn
1321      if (config().isCodeIndep()) {
1322        Relocation& reloc = helper_DynRel_init(rsym,
1323                                               *pReloc.targetRef().frag(),
1324                                               pReloc.targetRef().offset(),
1325                                               pReloc.type(),
1326                                               *this);
1327        getRelRelMap().record(pReloc, reloc);
1328        // set Rel bit
1329        rsym->setReserved(rsym->reserved() | ReserveRel);
1330        getTarget().checkAndSetHasTextRel(*pSection.getLink());
1331      }
1332      return;
1333
1334    case llvm::ELF::R_X86_64_PC32:
1335    case llvm::ELF::R_X86_64_PC16:
1336    case llvm::ELF::R_X86_64_PC8:
1337      return;
1338
1339    case llvm::ELF::R_X86_64_GOTPCREL:
1340      // Symbol needs GOT entry, reserve entry in .got
1341      // return if we already create GOT for this symbol
1342      if (rsym->reserved() & ReserveGOT)
1343        return;
1344
1345      // If building PIC object, a dynamic relocation with
1346      // type RELATIVE is needed to relocate this GOT entry.
1347      if (config().isCodeIndep())
1348        helper_GOT_init(pReloc, true, *this);
1349      else
1350        helper_GOT_init(pReloc, false, *this);
1351      rsym->setReserved(rsym->reserved() | ReserveGOT);
1352      return;
1353
1354    default:
1355      fatal(diag::unsupported_relocation) << static_cast<int>(pReloc.type())
1356                                          << "mclinker@googlegroups.com";
1357      break;
1358  }  // end switch
1359}
1360
1361void X86_64Relocator::scanGlobalReloc(Relocation& pReloc,
1362                                      IRBuilder& pBuilder,
1363                                      Module& pModule,
1364                                      LDSection& pSection) {
1365  // rsym - The relocation target symbol
1366  ResolveInfo* rsym = pReloc.symInfo();
1367
1368  switch (pReloc.type()) {
1369    case llvm::ELF::R_X86_64_64:
1370    case llvm::ELF::R_X86_64_32:
1371    case llvm::ELF::R_X86_64_16:
1372    case llvm::ELF::R_X86_64_8:
1373    case llvm::ELF::R_X86_64_32S:
1374      // Absolute relocation type, symbol may needs PLT entry or
1375      // dynamic relocation entry
1376      if (getTarget().symbolNeedsPLT(*rsym)) {
1377        // create plt for this symbol if it does not have one
1378        if (!(rsym->reserved() & ReservePLT)) {
1379          // Symbol needs PLT entry, we need to reserve a PLT entry
1380          // and the corresponding GOT and dynamic relocation entry
1381          // in .got and .rela.plt.
1382          helper_PLT_init(pReloc, *this);
1383          // set PLT bit
1384          rsym->setReserved(rsym->reserved() | ReservePLT);
1385        }
1386      }
1387
1388      if (getTarget()
1389              .symbolNeedsDynRel(
1390                  *rsym, (rsym->reserved() & ReservePLT), true)) {
1391        // symbol needs dynamic relocation entry, set up the dynrel entry
1392        if (getTarget().symbolNeedsCopyReloc(pReloc, *rsym)) {
1393          LDSymbol& cpy_sym =
1394              defineSymbolforCopyReloc(pBuilder, *rsym, getTarget());
1395          addCopyReloc(*cpy_sym.resolveInfo(), getTarget());
1396        } else {
1397          // set Rel bit and the dyn rel
1398          rsym->setReserved(rsym->reserved() | ReserveRel);
1399          getTarget().checkAndSetHasTextRel(*pSection.getLink());
1400          if (llvm::ELF::R_386_32 == pReloc.type() &&
1401              helper_use_relative_reloc(*rsym, *this)) {
1402            Relocation& reloc = helper_DynRel_init(rsym,
1403                                                   *pReloc.targetRef().frag(),
1404                                                   pReloc.targetRef().offset(),
1405                                                   llvm::ELF::R_X86_64_RELATIVE,
1406                                                   *this);
1407            getRelRelMap().record(pReloc, reloc);
1408          } else {
1409            Relocation& reloc = helper_DynRel_init(rsym,
1410                                                   *pReloc.targetRef().frag(),
1411                                                   pReloc.targetRef().offset(),
1412                                                   pReloc.type(),
1413                                                   *this);
1414            getRelRelMap().record(pReloc, reloc);
1415          }
1416          getTarget().checkAndSetHasTextRel(*pSection.getLink());
1417        }
1418      }
1419      return;
1420
1421    case llvm::ELF::R_X86_64_GOTPCREL:
1422      // Symbol needs GOT entry, reserve entry in .got
1423      // return if we already create GOT for this symbol
1424      if (rsym->reserved() & ReserveGOT)
1425        return;
1426
1427      // if the symbol cannot be fully resolved at link time, then we need a
1428      // dynamic relocation
1429      if (!getTarget().symbolFinalValueIsKnown(*rsym))
1430        helper_GOT_init(pReloc, true, *this);
1431      else
1432        helper_GOT_init(pReloc, false, *this);
1433      // set GOT bit
1434      rsym->setReserved(rsym->reserved() | ReserveGOT);
1435      return;
1436
1437    case llvm::ELF::R_X86_64_PLT32:
1438      // A PLT entry is needed when building shared library
1439
1440      // return if we already create plt for this symbol
1441      if (rsym->reserved() & ReservePLT)
1442        return;
1443
1444      // if the symbol's value can be decided at link time, then no need plt
1445      if (getTarget().symbolFinalValueIsKnown(*rsym))
1446        return;
1447
1448      // if symbol is defined in the ouput file and it's not
1449      // preemptible, no need plt
1450      if (rsym->isDefine() && !rsym->isDyn() &&
1451          !getTarget().isSymbolPreemptible(*rsym)) {
1452        return;
1453      }
1454
1455      // Symbol needs PLT entry, we need a PLT entry
1456      // and the corresponding GOT and dynamic relocation entry
1457      // in .got and .rel.plt.
1458      helper_PLT_init(pReloc, *this);
1459      // set PLT bit
1460      rsym->setReserved(rsym->reserved() | ReservePLT);
1461      return;
1462
1463    case llvm::ELF::R_X86_64_PC32:
1464    case llvm::ELF::R_X86_64_PC16:
1465    case llvm::ELF::R_X86_64_PC8:
1466      if (getTarget().symbolNeedsPLT(*rsym) &&
1467          LinkerConfig::DynObj != config().codeGenType()) {
1468        // create plt for this symbol if it does not have one
1469        if (!(rsym->reserved() & ReservePLT)) {
1470          // Symbol needs PLT entry, we need a PLT entry
1471          // and the corresponding GOT and dynamic relocation entry
1472          // in .got and .rel.plt.
1473          helper_PLT_init(pReloc, *this);
1474          // set PLT bit
1475          rsym->setReserved(rsym->reserved() | ReservePLT);
1476        }
1477      }
1478
1479      // Only PC relative relocation against dynamic symbol needs a
1480      // dynamic relocation.  Only dynamic copy relocation is allowed
1481      // and PC relative relocation will be resolved to the local copy.
1482      // All other dynamic relocations may lead to run-time relocation
1483      // overflow.
1484      if (getTarget().isDynamicSymbol(*rsym) &&
1485          getTarget()
1486              .symbolNeedsDynRel(
1487                  *rsym, (rsym->reserved() & ReservePLT), false) &&
1488          getTarget().symbolNeedsCopyReloc(pReloc, *rsym)) {
1489        LDSymbol& cpy_sym =
1490            defineSymbolforCopyReloc(pBuilder, *rsym, getTarget());
1491        addCopyReloc(*cpy_sym.resolveInfo(), getTarget());
1492      }
1493      return;
1494
1495    default:
1496      fatal(diag::unsupported_relocation) << static_cast<int>(pReloc.type())
1497                                          << "mclinker@googlegroups.com";
1498      break;
1499  }  // end switch
1500}
1501
1502uint32_t X86_64Relocator::getDebugStringOffset(Relocation& pReloc) const {
1503  if (pReloc.type() != llvm::ELF::R_X86_64_32)
1504    error(diag::unsupport_reloc_for_debug_string)
1505        << getName(pReloc.type()) << "mclinker@googlegroups.com";
1506
1507  if (pReloc.symInfo()->type() == ResolveInfo::Section)
1508    return pReloc.target() + pReloc.addend();
1509  else
1510    return pReloc.symInfo()->outSymbol()->fragRef()->offset() +
1511               pReloc.target() + pReloc.addend();
1512}
1513
1514void X86_64Relocator::applyDebugStringOffset(Relocation& pReloc,
1515                                             uint32_t pOffset) {
1516  pReloc.target() = pOffset;
1517}
1518
1519//------------------------------------------------//
1520// X86_64 Each relocation function implementation //
1521//------------------------------------------------//
1522// R_X86_64_NONE
1523Relocator::Result none(Relocation& pReloc, X86_64Relocator& pParent) {
1524  return Relocator::OK;
1525}
1526
1527// R_X86_64_64: S + A
1528// R_X86_64_32:
1529// R_X86_64_16:
1530// R_X86_64_8
1531Relocator::Result abs(Relocation& pReloc, X86_64Relocator& pParent) {
1532  ResolveInfo* rsym = pReloc.symInfo();
1533  Relocator::DWord A = pReloc.target() + pReloc.addend();
1534  Relocator::DWord S = pReloc.symValue();
1535  Relocation* dyn_rel = pParent.getRelRelMap().lookUp(pReloc);
1536  bool has_dyn_rel = (dyn_rel != NULL);
1537
1538  LDSection& target_sect = pReloc.targetRef().frag()->getParent()->getSection();
1539  // If the flag of target section is not ALLOC, we will not scan this
1540  // relocation but perform static relocation. (e.g., applying .debug section)
1541  if ((llvm::ELF::SHF_ALLOC & target_sect.flag()) == 0x0) {
1542    pReloc.target() = S + A;
1543    return Relocator::OK;
1544  }
1545
1546  // A local symbol may need RELA Type dynamic relocation
1547  if (rsym->isLocal() && has_dyn_rel) {
1548    dyn_rel->setAddend(S + A);
1549    return Relocator::OK;
1550  }
1551
1552  // An external symbol may need PLT and dynamic relocation
1553  if (!rsym->isLocal()) {
1554    if (rsym->reserved() & X86Relocator::ReservePLT) {
1555      S = helper_get_PLT_address(*rsym, pParent);
1556    }
1557    // If we generate a dynamic relocation (except R_X86_64_RELATIVE)
1558    // for a place, we should not perform static relocation on it
1559    // in order to keep the addend store in the place correct.
1560    if (has_dyn_rel) {
1561      if (llvm::ELF::R_X86_64_64 == pReloc.type() &&
1562          helper_use_relative_reloc(*rsym, pParent)) {
1563        dyn_rel->setAddend(S + A);
1564      } else {
1565        dyn_rel->setAddend(A);
1566        return Relocator::OK;
1567      }
1568    }
1569  }
1570
1571  // perform static relocation
1572  pReloc.target() = S + A;
1573  return Relocator::OK;
1574}
1575
1576// R_X86_64_32S: S + A
1577Relocator::Result signed32(Relocation& pReloc, X86_64Relocator& pParent) {
1578  ResolveInfo* rsym = pReloc.symInfo();
1579  Relocator::DWord A = pReloc.target() + pReloc.addend();
1580  Relocator::DWord S = pReloc.symValue();
1581
1582  // There should be no dynamic relocations for R_X86_64_32S.
1583  if (pParent.getRelRelMap().lookUp(pReloc) != NULL)
1584    return Relocator::BadReloc;
1585
1586  LDSection& target_sect = pReloc.targetRef().frag()->getParent()->getSection();
1587  // If the flag of target section is not ALLOC, we will not scan this
1588  // relocation but perform static relocation. (e.g., applying .debug section)
1589  // An external symbol may need PLT and dynamic relocation
1590  if ((llvm::ELF::SHF_ALLOC & target_sect.flag()) != 0x0 && !rsym->isLocal() &&
1591      rsym->reserved() & X86Relocator::ReservePLT)
1592    S = helper_get_PLT_address(*rsym, pParent);
1593
1594#if notyet
1595  // Check 32-bit signed overflow.
1596  Relocator::SWord V = S + A;
1597  if (V > INT64_C(0x7fffffff) || V < INT64_C(-0x80000000))
1598    return Relocator::Overflow;
1599#endif
1600
1601  // perform static relocation
1602  pReloc.target() = S + A;
1603  return Relocator::OK;
1604}
1605
1606// R_X86_64_GOTPCREL: GOT(S) + GOT_ORG + A - P
1607Relocator::Result gotpcrel(Relocation& pReloc, X86_64Relocator& pParent) {
1608  if (!(pReloc.symInfo()->reserved() & X86Relocator::ReserveGOT)) {
1609    return Relocator::BadReloc;
1610  }
1611
1612  // set symbol value of the got entry if needed
1613  X86_64GOTEntry* got_entry = pParent.getSymGOTMap().lookUp(*pReloc.symInfo());
1614  if (X86Relocator::SymVal == got_entry->getValue())
1615    got_entry->setValue(pReloc.symValue());
1616
1617  // setup relocation addend if needed
1618  Relocation* dyn_rel = pParent.getRelRelMap().lookUp(pReloc);
1619  if ((dyn_rel != NULL) && (X86Relocator::SymVal == dyn_rel->addend())) {
1620    dyn_rel->setAddend(pReloc.symValue());
1621  }
1622
1623  Relocator::Address GOT_S = helper_get_GOT_address(pReloc, pParent);
1624  Relocator::DWord A = pReloc.target() + pReloc.addend();
1625  Relocator::Address GOT_ORG = helper_GOT_ORG(pParent);
1626  // Apply relocation.
1627  pReloc.target() = GOT_S + GOT_ORG + A - pReloc.place();
1628  return Relocator::OK;
1629}
1630
1631// R_X86_64_PLT32: PLT(S) + A - P
1632Relocator::Result plt32(Relocation& pReloc, X86_64Relocator& pParent) {
1633  // PLT_S depends on if there is a PLT entry.
1634  Relocator::Address PLT_S;
1635  if ((pReloc.symInfo()->reserved() & X86Relocator::ReservePLT))
1636    PLT_S = helper_get_PLT_address(*pReloc.symInfo(), pParent);
1637  else
1638    PLT_S = pReloc.symValue();
1639  Relocator::DWord A = pReloc.target() + pReloc.addend();
1640  Relocator::Address P = pReloc.place();
1641  pReloc.target() = PLT_S + A - P;
1642  return Relocator::OK;
1643}
1644
1645// R_X86_64_PC32: S + A - P
1646// R_X86_64_PC16
1647// R_X86_64_PC8
1648Relocator::Result rel(Relocation& pReloc, X86_64Relocator& pParent) {
1649  ResolveInfo* rsym = pReloc.symInfo();
1650  Relocator::DWord A = pReloc.target() + pReloc.addend();
1651  Relocator::DWord S = pReloc.symValue();
1652  Relocator::DWord P = pReloc.place();
1653
1654  LDSection& target_sect = pReloc.targetRef().frag()->getParent()->getSection();
1655  // If the flag of target section is not ALLOC, we will not scan this
1656  // relocation but perform static relocation. (e.g., applying .debug section)
1657  if ((llvm::ELF::SHF_ALLOC & target_sect.flag()) == 0x0) {
1658    pReloc.target() = S + A - P;
1659    return Relocator::OK;
1660  }
1661
1662  // setup relocation addend if needed
1663  Relocation* dyn_rel = pParent.getRelRelMap().lookUp(pReloc);
1664  if ((dyn_rel != NULL) && (X86Relocator::SymVal == dyn_rel->addend())) {
1665    dyn_rel->setAddend(S);
1666  }
1667
1668  // An external symbol may need PLT and dynamic relocation
1669  if (!rsym->isLocal()) {
1670    if (rsym->reserved() & X86Relocator::ReservePLT) {
1671      S = helper_get_PLT_address(*rsym, pParent);
1672    }
1673    if (pParent.getTarget()
1674            .symbolNeedsDynRel(
1675                *rsym, (rsym->reserved() & X86Relocator::ReservePLT), false)) {
1676      return Relocator::Overflow;
1677    }
1678  }
1679
1680  // perform static relocation
1681  pReloc.target() = S + A - P;
1682  return Relocator::OK;
1683}
1684
1685Relocator::Result unsupported(Relocation& pReloc, X86_64Relocator& pParent) {
1686  return Relocator::Unsupported;
1687}
1688
1689}  // namespace mcld
1690