1//===- ObjectLinker.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/Object/ObjectLinker.h>
10
11#include <mcld/LinkerConfig.h>
12#include <mcld/Module.h>
13#include <mcld/InputTree.h>
14#include <mcld/IRBuilder.h>
15#include <mcld/LD/LDSection.h>
16#include <mcld/LD/LDContext.h>
17#include <mcld/LD/Archive.h>
18#include <mcld/LD/ArchiveReader.h>
19#include <mcld/LD/ObjectReader.h>
20#include <mcld/LD/DynObjReader.h>
21#include <mcld/LD/GroupReader.h>
22#include <mcld/LD/BinaryReader.h>
23#include <mcld/LD/ObjectWriter.h>
24#include <mcld/LD/ResolveInfo.h>
25#include <mcld/LD/RelocData.h>
26#include <mcld/LD/Relocator.h>
27#include <mcld/Support/RealPath.h>
28#include <mcld/Support/MemoryArea.h>
29#include <mcld/Support/MsgHandling.h>
30#include <mcld/Support/DefSymParser.h>
31#include <mcld/Target/TargetLDBackend.h>
32#include <mcld/Fragment/FragmentLinker.h>
33#include <mcld/Object/ObjectBuilder.h>
34
35#include <llvm/Support/Casting.h>
36
37
38using namespace llvm;
39using namespace mcld;
40ObjectLinker::ObjectLinker(const LinkerConfig& pConfig,
41                           TargetLDBackend& pLDBackend)
42  : m_Config(pConfig),
43    m_pLinker(NULL),
44    m_pModule(NULL),
45    m_pBuilder(NULL),
46    m_LDBackend(pLDBackend),
47    m_pObjectReader(NULL),
48    m_pDynObjReader(NULL),
49    m_pArchiveReader(NULL),
50    m_pGroupReader(NULL),
51    m_pBinaryReader(NULL),
52    m_pWriter(NULL) {
53}
54
55ObjectLinker::~ObjectLinker()
56{
57  delete m_pLinker;
58  delete m_pObjectReader;
59  delete m_pDynObjReader;
60  delete m_pArchiveReader;
61  delete m_pGroupReader;
62  delete m_pBinaryReader;
63  delete m_pWriter;
64}
65
66void ObjectLinker::setup(Module& pModule, IRBuilder& pBuilder)
67{
68  m_pModule = &pModule;
69  m_pBuilder = &pBuilder;
70
71  // set up soname
72  if (!m_Config.options().soname().empty()) {
73    m_pModule->setName(m_Config.options().soname());
74  }
75}
76
77/// initFragmentLinker - initialize FragmentLinker
78///  Connect all components with FragmentLinker
79bool ObjectLinker::initFragmentLinker()
80{
81  if (NULL == m_pLinker) {
82    m_pLinker = new FragmentLinker(m_Config,
83                                   *m_pModule,
84                                   m_LDBackend);
85  }
86
87  // initialize the readers and writers
88  // Because constructor can not be failed, we initalize all readers and
89  // writers outside the FragmentLinker constructors.
90  m_pObjectReader  = m_LDBackend.createObjectReader(*m_pBuilder);
91  m_pArchiveReader = m_LDBackend.createArchiveReader(*m_pModule);
92  m_pDynObjReader  = m_LDBackend.createDynObjReader(*m_pBuilder);
93  m_pBinaryReader  = m_LDBackend.createBinaryReader(*m_pBuilder);
94  m_pGroupReader   = new GroupReader(*m_pModule, *m_pObjectReader,
95                         *m_pDynObjReader, *m_pArchiveReader, *m_pBinaryReader);
96  m_pWriter        = m_LDBackend.createWriter();
97
98  // initialize Relocator
99  m_LDBackend.initRelocator();
100  return true;
101}
102
103/// initStdSections - initialize standard sections
104bool ObjectLinker::initStdSections()
105{
106  ObjectBuilder builder(m_Config, *m_pModule);
107
108  // initialize standard sections
109  if (!m_LDBackend.initStdSections(builder))
110    return false;
111
112  // initialize target-dependent sections
113  m_LDBackend.initTargetSections(*m_pModule, builder);
114
115  return true;
116}
117
118void ObjectLinker::normalize()
119{
120  // -----  set up inputs  ----- //
121  Module::input_iterator input, inEnd = m_pModule->input_end();
122  for (input = m_pModule->input_begin(); input!=inEnd; ++input) {
123    // is a group node
124    if (isGroup(input)) {
125      getGroupReader()->readGroup(input, m_pBuilder->getInputBuilder(), m_Config);
126      continue;
127    }
128
129    // already got type - for example, bitcode or external OIR (object
130    // intermediate representation)
131    if ((*input)->type() == Input::Script ||
132        (*input)->type() == Input::Archive ||
133        (*input)->type() == Input::External)
134      continue;
135
136    if (Input::Object == (*input)->type()) {
137      m_pModule->getObjectList().push_back(*input);
138      continue;
139    }
140
141    if (Input::DynObj == (*input)->type()) {
142      m_pModule->getLibraryList().push_back(*input);
143      continue;
144    }
145
146    // read input as a binary file
147    if (m_Config.options().isBinaryInput()) {
148      (*input)->setType(Input::Object);
149      getBinaryReader()->readBinary(**input);
150      m_pModule->getObjectList().push_back(*input);
151    }
152    // is a relocatable object file
153    else if (getObjectReader()->isMyFormat(**input)) {
154      (*input)->setType(Input::Object);
155      getObjectReader()->readHeader(**input);
156      getObjectReader()->readSections(**input);
157      getObjectReader()->readSymbols(**input);
158      m_pModule->getObjectList().push_back(*input);
159    }
160    // is a shared object file
161    else if (getDynObjReader()->isMyFormat(**input)) {
162      (*input)->setType(Input::DynObj);
163      getDynObjReader()->readHeader(**input);
164      getDynObjReader()->readSymbols(**input);
165      m_pModule->getLibraryList().push_back(*input);
166    }
167    // is an archive
168    else if (getArchiveReader()->isMyFormat(**input)) {
169      (*input)->setType(Input::Archive);
170      Archive archive(**input, m_pBuilder->getInputBuilder());
171      getArchiveReader()->readArchive(archive);
172      if(archive.numOfObjectMember() > 0) {
173        m_pModule->getInputTree().merge<InputTree::Inclusive>(input,
174                                                            archive.inputs());
175      }
176    }
177    else {
178      fatal(diag::err_unrecognized_input_file) << (*input)->path()
179                                          << m_Config.targets().triple().str();
180    }
181  } // end of for
182}
183
184bool ObjectLinker::linkable() const
185{
186  // check we have input and output files
187  if (m_pModule->getInputTree().empty()) {
188    error(diag::err_no_inputs);
189    return false;
190  }
191
192  // can not mix -static with shared objects
193  Module::const_lib_iterator lib, libEnd = m_pModule->lib_end();
194  for (lib = m_pModule->lib_begin(); lib != libEnd; ++lib) {
195    if((*lib)->attribute()->isStatic()) {
196      error(diag::err_mixed_shared_static_objects)
197                                      << (*lib)->name() << (*lib)->path();
198      return false;
199    }
200  }
201
202  // --nmagic and --omagic options lead to static executable program.
203  // These options turn off page alignment of sections. Because the
204  // sections are not aligned to pages, these sections can not contain any
205  // exported functions. Also, because the two options disable linking
206  // against shared libraries, the output absolutely does not call outside
207  // functions.
208  if (m_Config.options().nmagic() && !m_Config.isCodeStatic()) {
209    error(diag::err_nmagic_not_static);
210    return false;
211  }
212  if (m_Config.options().omagic() && !m_Config.isCodeStatic()) {
213    error(diag::err_omagic_not_static);
214    return false;
215  }
216
217  return true;
218}
219
220/// readRelocations - read all relocation entries
221///
222/// All symbols should be read and resolved before this function.
223bool ObjectLinker::readRelocations()
224{
225  // Bitcode is read by the other path. This function reads relocation sections
226  // in object files.
227  mcld::InputTree::bfs_iterator input, inEnd = m_pModule->getInputTree().bfs_end();
228  for (input=m_pModule->getInputTree().bfs_begin(); input!=inEnd; ++input) {
229    if ((*input)->type() == Input::Object && (*input)->hasMemArea()) {
230      if (!getObjectReader()->readRelocations(**input))
231        return false;
232    }
233    // ignore the other kinds of files.
234  }
235  return true;
236}
237
238/// mergeSections - put allinput sections into output sections
239bool ObjectLinker::mergeSections()
240{
241  ObjectBuilder builder(m_Config, *m_pModule);
242  Module::obj_iterator obj, objEnd = m_pModule->obj_end();
243  for (obj = m_pModule->obj_begin(); obj != objEnd; ++obj) {
244    LDContext::sect_iterator sect, sectEnd = (*obj)->context()->sectEnd();
245    for (sect = (*obj)->context()->sectBegin(); sect != sectEnd; ++sect) {
246      switch ((*sect)->kind()) {
247        // Some *INPUT sections should not be merged.
248        case LDFileFormat::Ignore:
249        case LDFileFormat::Null:
250        case LDFileFormat::Relocation:
251        case LDFileFormat::NamePool:
252        case LDFileFormat::Group:
253        case LDFileFormat::StackNote:
254          // skip
255          continue;
256        case LDFileFormat::Target:
257          if (!m_LDBackend.mergeSection(*m_pModule, **sect)) {
258            error(diag::err_cannot_merge_section) << (*sect)->name()
259                                                  << (*obj)->name();
260            return false;
261          }
262          break;
263        case LDFileFormat::EhFrame: {
264          if (!(*sect)->hasEhFrame())
265            continue; // skip
266
267          LDSection* out_sect = NULL;
268          if (NULL == (out_sect = builder.MergeSection(**sect))) {
269            error(diag::err_cannot_merge_section) << (*sect)->name()
270                                                  << (*obj)->name();
271            return false;
272          }
273
274          if (!m_LDBackend.updateSectionFlags(*out_sect, **sect)) {
275            error(diag::err_cannot_merge_section) << (*sect)->name()
276                                                  << (*obj)->name();
277            return false;
278          }
279          break;
280        }
281        default: {
282          if (!(*sect)->hasSectionData())
283            continue; // skip
284
285          LDSection* out_sect = NULL;
286          if (NULL == (out_sect = builder.MergeSection(**sect))) {
287            error(diag::err_cannot_merge_section) << (*sect)->name()
288                                                  << (*obj)->name();
289            return false;
290          }
291
292          if (!m_LDBackend.updateSectionFlags(*out_sect, **sect)) {
293            error(diag::err_cannot_merge_section) << (*sect)->name()
294                                                  << (*obj)->name();
295            return false;
296          }
297          break;
298        }
299      } // end of switch
300    } // for each section
301  } // for each obj
302  return true;
303}
304
305/// addStandardSymbols - shared object and executable files need some
306/// standard symbols
307///   @return if there are some input symbols with the same name to the
308///   standard symbols, return false
309bool ObjectLinker::addStandardSymbols()
310{
311  // create and add section symbols for each output section
312  Module::iterator iter, iterEnd = m_pModule->end();
313  for (iter = m_pModule->begin(); iter != iterEnd; ++iter) {
314    m_pModule->getSectionSymbolSet().add(**iter, m_pModule->getNamePool());
315  }
316
317  return m_LDBackend.initStandardSymbols(*m_pBuilder, *m_pModule);
318}
319
320/// addTargetSymbols - some targets, such as MIPS and ARM, need some
321/// target-dependent symbols
322///   @return if there are some input symbols with the same name to the
323///   target symbols, return false
324bool ObjectLinker::addTargetSymbols()
325{
326  m_LDBackend.initTargetSymbols(*m_pBuilder, *m_pModule);
327  return true;
328}
329
330/// addScriptSymbols - define symbols from the command line option or linker
331/// scripts.
332bool ObjectLinker::addScriptSymbols()
333{
334  const LinkerScript& script = m_pModule->getScript();
335  LinkerScript::DefSymMap::const_entry_iterator it;
336  LinkerScript::DefSymMap::const_entry_iterator ie = script.defSymMap().end();
337  // go through the entire defSymMap
338  for (it = script.defSymMap().begin(); it != ie; ++it) {
339    const llvm::StringRef sym =  it.getEntry()->key();
340    ResolveInfo* old_info = m_pModule->getNamePool().findInfo(sym);
341    // if the symbol does not exist, we can set type to NOTYPE
342    // else we retain its type, same goes for size - 0 or retain old value
343    // and visibility - Default or retain
344    if (old_info != NULL) {
345      if(!m_pBuilder->AddSymbol<IRBuilder::Force, IRBuilder::Unresolve>(
346                             sym,
347                             static_cast<ResolveInfo::Type>(old_info->type()),
348                             ResolveInfo::Define,
349                             ResolveInfo::Absolute,
350                             old_info->size(),
351                             0x0,
352                             FragmentRef::Null(),
353                             old_info->visibility()))
354        return false;
355    }
356    else {
357      if (!m_pBuilder->AddSymbol<IRBuilder::Force, IRBuilder::Unresolve>(
358                             sym,
359                             ResolveInfo::NoType,
360                             ResolveInfo::Define,
361                             ResolveInfo::Absolute,
362                             0x0,
363                             0x0,
364                             FragmentRef::Null(),
365                             ResolveInfo::Default))
366        return false;
367    }
368  }
369  return true;
370}
371
372bool ObjectLinker::scanRelocations()
373{
374  // apply all relocations of all inputs
375  Module::obj_iterator input, inEnd = m_pModule->obj_end();
376  for (input = m_pModule->obj_begin(); input != inEnd; ++input) {
377    m_LDBackend.getRelocator()->initializeScan(**input);
378    LDContext::sect_iterator rs, rsEnd = (*input)->context()->relocSectEnd();
379    for (rs = (*input)->context()->relocSectBegin(); rs != rsEnd; ++rs) {
380      // bypass the reloc section if
381      // 1. its section kind is changed to Ignore. (The target section is a
382      // discarded group section.)
383      // 2. it has no reloc data. (All symbols in the input relocs are in the
384      // discarded group sections)
385      if (LDFileFormat::Ignore == (*rs)->kind() || !(*rs)->hasRelocData())
386        continue;
387      RelocData::iterator reloc, rEnd = (*rs)->getRelocData()->end();
388      for (reloc = (*rs)->getRelocData()->begin(); reloc != rEnd; ++reloc) {
389        Relocation* relocation = llvm::cast<Relocation>(reloc);
390        // scan relocation
391        if (LinkerConfig::Object != m_Config.codeGenType())
392          m_LDBackend.getRelocator()->scanRelocation(
393                                    *relocation, *m_pBuilder, *m_pModule, **rs);
394        else
395          m_LDBackend.getRelocator()->partialScanRelocation(
396                                                 *relocation, *m_pModule, **rs);
397      } // for all relocations
398    } // for all relocation section
399    m_LDBackend.getRelocator()->finalizeScan(**input);
400  } // for all inputs
401  return true;
402}
403
404/// initStubs - initialize stub-related stuff.
405bool ObjectLinker::initStubs()
406{
407  // initialize BranchIslandFactory
408  m_LDBackend.initBRIslandFactory();
409
410  // initialize StubFactory
411  m_LDBackend.initStubFactory();
412
413  // initialize target stubs
414  m_LDBackend.initTargetStubs();
415  return true;
416}
417
418/// allocateCommonSymobols - allocate fragments for common symbols to the
419/// corresponding sections
420bool ObjectLinker::allocateCommonSymbols()
421{
422  if (LinkerConfig::Object != m_Config.codeGenType() ||
423      m_Config.options().isDefineCommon())
424    return m_LDBackend.allocateCommonSymbols(*m_pModule);
425  return true;
426}
427
428/// prelayout - help backend to do some modification before layout
429bool ObjectLinker::prelayout()
430{
431  // finalize the section symbols, set their fragment reference and push them
432  // into output symbol table
433  Module::iterator sect, sEnd = m_pModule->end();
434  for (sect = m_pModule->begin(); sect != sEnd; ++sect) {
435    m_pModule->getSectionSymbolSet().finalize(**sect,
436        m_pModule->getSymbolTable(),
437        m_Config.codeGenType() == LinkerConfig::Object);
438  }
439
440  m_LDBackend.preLayout(*m_pModule, *m_pBuilder);
441
442  /// check program interpreter - computer the name size of the runtime dyld
443  if (!m_Config.isCodeStatic() &&
444      (LinkerConfig::Exec == m_Config.codeGenType() ||
445       m_Config.options().isPIE() ||
446       m_Config.options().hasDyld()))
447    m_LDBackend.sizeInterp();
448
449  /// measure NamePools - compute the size of name pool sections
450  /// In ELF, will compute  the size of.symtab, .strtab, .dynsym, .dynstr,
451  /// .hash and .shstrtab sections.
452  ///
453  /// dump all symbols and strings from FragmentLinker and build the format-dependent
454  /// hash table.
455  /// @note sizeNamePools replies on LinkerConfig::CodePosition. Must determine
456  /// code position model before calling GNULDBackend::sizeNamePools()
457  m_LDBackend.sizeNamePools(*m_pModule);
458
459  return true;
460}
461
462/// layout - linearly layout all output sections and reserve some space
463/// for GOT/PLT
464///   Because we do not support instruction relaxing in this early version,
465///   if there is a branch can not jump to its target, we return false
466///   directly
467bool ObjectLinker::layout()
468{
469  m_LDBackend.layout(*m_pModule);
470  return true;
471}
472
473/// prelayout - help backend to do some modification after layout
474bool ObjectLinker::postlayout()
475{
476  m_LDBackend.postLayout(*m_pModule, *m_pBuilder);
477  return true;
478}
479
480/// finalizeSymbolValue - finalize the resolved symbol value.
481///   Before relocate(), after layout(), FragmentLinker should correct value of all
482///   symbol.
483bool ObjectLinker::finalizeSymbolValue()
484{
485  bool finalized = m_pLinker->finalizeSymbols() && m_LDBackend.finalizeSymbols();
486  bool scriptSymsAdded = true;
487  uint64_t symVal;
488  const LinkerScript& script = m_pModule->getScript();
489  LinkerScript::DefSymMap::const_entry_iterator it;
490  LinkerScript::DefSymMap::const_entry_iterator ie = script.defSymMap().end();
491
492  DefSymParser parser(*m_pModule);
493  for (it = script.defSymMap().begin(); it != ie; ++it) {
494    llvm::StringRef symName =  it.getEntry()->key();
495    llvm::StringRef expr =  it.getEntry()->value();
496
497    LDSymbol* symbol = m_pModule->getNamePool().findSymbol(symName);
498    assert(NULL != symbol && "--defsym symbol should be in the name pool");
499    scriptSymsAdded &= parser.parse(expr, symVal);
500    if (!scriptSymsAdded)
501      break;
502    symbol->setValue(symVal);
503  }
504  return finalized && scriptSymsAdded ;
505}
506
507/// relocate - applying relocation entries and create relocation
508/// section in the output files
509/// Create relocation section, asking TargetLDBackend to
510/// read the relocation information into RelocationEntry
511/// and push_back into the relocation section
512bool ObjectLinker::relocation()
513{
514  return m_pLinker->applyRelocations();
515}
516
517/// emitOutput - emit the output file.
518bool ObjectLinker::emitOutput(MemoryArea& pOutput)
519{
520  return llvm::errc::success == getWriter()->writeObject(*m_pModule, pOutput);
521}
522
523
524/// postProcessing - do modification after all processes
525bool ObjectLinker::postProcessing(MemoryArea& pOutput)
526{
527  m_pLinker->syncRelocationResult(pOutput);
528
529  // emit .eh_frame_hdr
530  // eh_frame_hdr should be emitted after syncRelocation, because eh_frame_hdr
531  // needs FDE PC value, which will be corrected at syncRelocation
532  m_LDBackend.postProcessing(pOutput);
533  return true;
534}
535
536