Linker.cpp revision 37b74a387bb3993387029859c2d9d051c41c724e
1//===- Linker.cpp ---------------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "mcld/Linker.h"
10
11#include "mcld/IRBuilder.h"
12#include "mcld/LinkerConfig.h"
13#include "mcld/Module.h"
14#include "mcld/Fragment/FragmentRef.h"
15#include "mcld/Fragment/Relocation.h"
16#include "mcld/LD/LDSection.h"
17#include "mcld/LD/LDSymbol.h"
18#include "mcld/LD/ObjectWriter.h"
19#include "mcld/LD/RelocData.h"
20#include "mcld/LD/SectionData.h"
21#include "mcld/MC/InputBuilder.h"
22#include "mcld/Object/ObjectLinker.h"
23#include "mcld/Support/FileHandle.h"
24#include "mcld/Support/FileOutputBuffer.h"
25#include "mcld/Support/MsgHandling.h"
26#include "mcld/Support/TargetRegistry.h"
27#include "mcld/Support/raw_ostream.h"
28#include "mcld/Target/TargetLDBackend.h"
29
30#include <cassert>
31
32namespace mcld {
33
34Linker::Linker()
35    : m_pConfig(NULL),
36      m_pIRBuilder(NULL),
37      m_pTarget(NULL),
38      m_pBackend(NULL),
39      m_pObjLinker(NULL) {
40}
41
42Linker::~Linker() {
43  reset();
44}
45
46/// emulate - To set up target-dependent options and default linker script.
47/// Follow GNU ld quirks.
48bool Linker::emulate(LinkerScript& pScript, LinkerConfig& pConfig) {
49  m_pConfig = &pConfig;
50
51  if (!initTarget())
52    return false;
53
54  if (!initBackend())
55    return false;
56
57  if (!initOStream())
58    return false;
59
60  if (!initEmulator(pScript))
61    return false;
62
63  return true;
64}
65
66bool Linker::link(Module& pModule, IRBuilder& pBuilder) {
67  if (!normalize(pModule, pBuilder))
68    return false;
69
70  if (!resolve(pModule))
71    return false;
72
73  return layout();
74}
75
76/// normalize - to convert the command line language to the input tree.
77bool Linker::normalize(Module& pModule, IRBuilder& pBuilder) {
78  assert(m_pConfig != NULL);
79
80  m_pIRBuilder = &pBuilder;
81
82  m_pObjLinker = new ObjectLinker(*m_pConfig, *m_pBackend);
83
84  // 2. - initialize ObjectLinker
85  if (!m_pObjLinker->initialize(pModule, pBuilder))
86    return false;
87
88  // 3. - initialize output's standard sections
89  if (!m_pObjLinker->initStdSections())
90    return false;
91
92  if (!Diagnose())
93    return false;
94
95  // 4.a - add undefined symbols
96  //   before reading the inputs, we should add undefined symbols set by -u to
97  //   ensure that correspoding objects (e.g. in an archive) will be included
98  m_pObjLinker->addUndefinedSymbols();
99
100  // 4.b - normalize the input tree
101  //   read out sections and symbol/string tables (from the files) and
102  //   set them in Module. When reading out the symbol, resolve their symbols
103  //   immediately and set their ResolveInfo (i.e., Symbol Resolution).
104  m_pObjLinker->normalize();
105
106  if (m_pConfig->options().trace()) {
107    static int counter = 0;
108    mcld::outs() << "** name\ttype\tpath\tsize ("
109                 << pModule.getInputTree().size() << ")\n";
110
111    InputTree::const_dfs_iterator input,
112        inEnd = pModule.getInputTree().dfs_end();
113    for (input = pModule.getInputTree().dfs_begin(); input != inEnd; ++input) {
114      mcld::outs() << counter++ << " *  " << (*input)->name();
115      switch ((*input)->type()) {
116        case Input::Archive:
117          mcld::outs() << "\tarchive\t(";
118          break;
119        case Input::Object:
120          mcld::outs() << "\tobject\t(";
121          break;
122        case Input::DynObj:
123          mcld::outs() << "\tshared\t(";
124          break;
125        case Input::Script:
126          mcld::outs() << "\tscript\t(";
127          break;
128        case Input::External:
129          mcld::outs() << "\textern\t(";
130          break;
131        default:
132          unreachable(diag::err_cannot_trace_file)
133              << (*input)->type() << (*input)->name() << (*input)->path();
134      }
135      mcld::outs() << (*input)->path() << ")\n";
136    }
137  }
138
139  // 5. - set up code position
140  if (LinkerConfig::DynObj == m_pConfig->codeGenType() ||
141      m_pConfig->options().isPIE()) {
142    m_pConfig->setCodePosition(LinkerConfig::Independent);
143  } else if (pModule.getLibraryList().empty()) {
144    // If the output is dependent on its loaded address, and it does not need
145    // to call outside functions, then we can treat the output static dependent
146    // and perform better optimizations.
147    m_pConfig->setCodePosition(LinkerConfig::StaticDependent);
148
149    if (LinkerConfig::Exec == m_pConfig->codeGenType()) {
150      // Since the output is static dependent, there should not have any
151      // undefined
152      // references in the output module.
153      m_pConfig->options().setNoUndefined();
154    }
155  } else {
156    m_pConfig->setCodePosition(LinkerConfig::DynamicDependent);
157  }
158
159  if (!m_pObjLinker->linkable())
160    return Diagnose();
161
162  return true;
163}
164
165bool Linker::resolve(Module& pModule) {
166  assert(m_pConfig != NULL);
167  assert(m_pObjLinker != NULL);
168
169  // 6. - read all relocation entries from input files
170  //   For all relocation sections of each input file (in the tree),
171  //   read out reloc entry info from the object file and accordingly
172  //   initiate their reloc entries in SectOrRelocData of LDSection.
173  //
174  //   To collect all edges in the reference graph.
175  m_pObjLinker->readRelocations();
176
177  // 7. - data stripping optimizations
178  m_pObjLinker->dataStrippingOpt();
179
180  // 8. - merge all sections
181  //   Push sections into Module's SectionTable.
182  //   Merge sections that have the same name.
183  //   Maintain them as fragments in the section.
184  //
185  //   To merge nodes of the reference graph.
186  if (!m_pObjLinker->mergeSections())
187    return false;
188
189  // 9.a - add symbols to output
190  //  After all input symbols have been resolved, add them to output symbol
191  //  table at once
192  m_pObjLinker->addSymbolsToOutput(pModule);
193
194  // 9.b - allocateCommonSymbols
195  //   Allocate fragments for common symbols to the corresponding sections.
196  if (!m_pObjLinker->allocateCommonSymbols())
197    return false;
198
199  return true;
200}
201
202bool Linker::layout() {
203  assert(m_pConfig != NULL && m_pObjLinker != NULL);
204
205  // 10. - add standard symbols, target-dependent symbols and script symbols
206  if (!m_pObjLinker->addStandardSymbols() ||
207      !m_pObjLinker->addTargetSymbols() || !m_pObjLinker->addScriptSymbols())
208    return false;
209
210  // 11. - scan all relocation entries by output symbols.
211  //   reserve GOT space for layout.
212  //   the space info is needed by pre-layout to compute the section size
213  m_pObjLinker->scanRelocations();
214
215  // 12.a - init relaxation stuff.
216  m_pObjLinker->initStubs();
217
218  // 12.b - pre-layout
219  m_pObjLinker->prelayout();
220
221  // 12.c - linear layout
222  //   Decide which sections will be left in. Sort the sections according to
223  //   a given order. Then, create program header accordingly.
224  //   Finally, set the offset for sections (@ref LDSection)
225  //   according to the new order.
226  m_pObjLinker->layout();
227
228  // 12.d - post-layout (create segment, instruction relaxing)
229  m_pObjLinker->postlayout();
230
231  // 13. - finalize symbol value
232  m_pObjLinker->finalizeSymbolValue();
233
234  // 14. - apply relocations
235  m_pObjLinker->relocation();
236
237  if (!Diagnose())
238    return false;
239  return true;
240}
241
242bool Linker::emit(FileOutputBuffer& pOutput) {
243  // 15. - write out output
244  m_pObjLinker->emitOutput(pOutput);
245
246  // 16. - post processing
247  m_pObjLinker->postProcessing(pOutput);
248
249  if (!Diagnose())
250    return false;
251
252  return true;
253}
254
255bool Linker::emit(const Module& pModule, const std::string& pPath) {
256  FileHandle file;
257  FileHandle::OpenMode open_mode(
258      FileHandle::ReadWrite | FileHandle::Truncate | FileHandle::Create);
259  FileHandle::Permission permission;
260  switch (m_pConfig->codeGenType()) {
261    case mcld::LinkerConfig::Unknown:
262    case mcld::LinkerConfig::Object:
263      permission = FileHandle::Permission(0x644);
264      break;
265    case mcld::LinkerConfig::DynObj:
266    case mcld::LinkerConfig::Exec:
267    case mcld::LinkerConfig::Binary:
268      permission = FileHandle::Permission(0x755);
269      break;
270    default:
271      assert(0 && "Unknown file type");
272  }
273
274  bool result = file.open(sys::fs::Path(pPath), open_mode, permission);
275  if (!result) {
276    error(diag::err_cannot_open_output_file) << "Linker::emit()" << pPath;
277    return false;
278  }
279
280  std::unique_ptr<FileOutputBuffer> output;
281  FileOutputBuffer::create(
282      file, m_pObjLinker->getWriter()->getOutputSize(pModule), output);
283
284  result = emit(*output.get());
285  file.close();
286  return result;
287}
288
289bool Linker::emit(const Module& pModule, int pFileDescriptor) {
290  FileHandle file;
291  file.delegate(pFileDescriptor);
292
293  std::unique_ptr<FileOutputBuffer> output;
294  FileOutputBuffer::create(
295      file, m_pObjLinker->getWriter()->getOutputSize(pModule), output);
296
297  return emit(*output.get());
298}
299
300bool Linker::reset() {
301  m_pConfig = NULL;
302  m_pIRBuilder = NULL;
303  m_pTarget = NULL;
304
305  // Because llvm::iplist will touch the removed node, we must clear
306  // RelocData before deleting target backend.
307  RelocData::Clear();
308  SectionData::Clear();
309  EhFrame::Clear();
310
311  delete m_pBackend;
312  m_pBackend = NULL;
313
314  delete m_pObjLinker;
315  m_pObjLinker = NULL;
316
317  LDSection::Clear();
318  LDSymbol::Clear();
319  FragmentRef::Clear();
320  Relocation::Clear();
321  return true;
322}
323
324bool Linker::initTarget() {
325  assert(m_pConfig != NULL);
326
327  std::string error;
328  llvm::Triple triple(m_pConfig->targets().triple());
329
330  m_pTarget = mcld::TargetRegistry::lookupTarget(
331      m_pConfig->targets().getArch(), triple, error);
332  m_pConfig->targets().setTriple(triple);
333
334  if (m_pTarget == NULL) {
335    fatal(diag::fatal_cannot_init_target) << triple.str() << error;
336    return false;
337  }
338  return true;
339}
340
341bool Linker::initBackend() {
342  assert(m_pTarget != NULL);
343  m_pBackend = m_pTarget->createLDBackend(*m_pConfig);
344  if (m_pBackend == NULL) {
345    fatal(diag::fatal_cannot_init_backend)
346        << m_pConfig->targets().triple().str();
347    return false;
348  }
349  return true;
350}
351
352bool Linker::initOStream() {
353  assert(m_pConfig != NULL);
354
355  mcld::outs().setColor(m_pConfig->options().color());
356  mcld::errs().setColor(m_pConfig->options().color());
357
358  return true;
359}
360
361bool Linker::initEmulator(LinkerScript& pScript) {
362  assert(m_pTarget != NULL && m_pConfig != NULL);
363  return m_pTarget->emulate(pScript, *m_pConfig);
364}
365
366}  // namespace mcld
367