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/LinkerScript.h>
13#include <mcld/Module.h>
14#include <mcld/InputTree.h>
15#include <mcld/IRBuilder.h>
16#include <mcld/LD/LDSection.h>
17#include <mcld/LD/LDContext.h>
18#include <mcld/LD/Archive.h>
19#include <mcld/LD/ArchiveReader.h>
20#include <mcld/LD/ObjectReader.h>
21#include <mcld/LD/DynObjReader.h>
22#include <mcld/LD/GroupReader.h>
23#include <mcld/LD/BinaryReader.h>
24#include <mcld/LD/GarbageCollection.h>
25#include <mcld/LD/IdenticalCodeFolding.h>
26#include <mcld/LD/ObjectWriter.h>
27#include <mcld/LD/ResolveInfo.h>
28#include <mcld/LD/RelocData.h>
29#include <mcld/LD/Relocator.h>
30#include <mcld/LD/SectionData.h>
31#include <mcld/LD/BranchIslandFactory.h>
32#include <mcld/Script/ScriptFile.h>
33#include <mcld/Script/ScriptReader.h>
34#include <mcld/Script/Assignment.h>
35#include <mcld/Script/Operand.h>
36#include <mcld/Script/RpnEvaluator.h>
37#include <mcld/Support/RealPath.h>
38#include <mcld/Support/FileOutputBuffer.h>
39#include <mcld/Support/MsgHandling.h>
40#include <mcld/Target/TargetLDBackend.h>
41#include <mcld/Fragment/Relocation.h>
42#include <mcld/Object/ObjectBuilder.h>
43
44#include <llvm/Support/Casting.h>
45#include <llvm/Support/Host.h>
46#include <system_error>
47
48using namespace llvm;
49using namespace mcld;
50
51//===----------------------------------------------------------------------===//
52// ObjectLinker
53//===----------------------------------------------------------------------===//
54ObjectLinker::ObjectLinker(const LinkerConfig& pConfig,
55                           TargetLDBackend& pLDBackend)
56  : m_Config(pConfig),
57    m_pModule(NULL),
58    m_pBuilder(NULL),
59    m_LDBackend(pLDBackend),
60    m_pObjectReader(NULL),
61    m_pDynObjReader(NULL),
62    m_pArchiveReader(NULL),
63    m_pGroupReader(NULL),
64    m_pBinaryReader(NULL),
65    m_pScriptReader(NULL),
66    m_pWriter(NULL) {
67}
68
69ObjectLinker::~ObjectLinker()
70{
71  delete m_pObjectReader;
72  delete m_pDynObjReader;
73  delete m_pArchiveReader;
74  delete m_pGroupReader;
75  delete m_pBinaryReader;
76  delete m_pScriptReader;
77  delete m_pWriter;
78}
79
80bool ObjectLinker::initialize(Module& pModule, IRBuilder& pBuilder)
81{
82  m_pModule = &pModule;
83  m_pBuilder = &pBuilder;
84
85  // initialize the readers and writers
86  m_pObjectReader  = m_LDBackend.createObjectReader(*m_pBuilder);
87  m_pArchiveReader = m_LDBackend.createArchiveReader(*m_pModule);
88  m_pDynObjReader  = m_LDBackend.createDynObjReader(*m_pBuilder);
89  m_pBinaryReader  = m_LDBackend.createBinaryReader(*m_pBuilder);
90  m_pGroupReader   = new GroupReader(*m_pModule, *m_pObjectReader,
91                         *m_pDynObjReader, *m_pArchiveReader, *m_pBinaryReader);
92  m_pScriptReader  = new ScriptReader(*m_pGroupReader);
93  m_pWriter        = m_LDBackend.createWriter();
94
95  // initialize Relocator
96  m_LDBackend.initRelocator();
97
98  return true;
99}
100
101/// initStdSections - initialize standard sections
102bool ObjectLinker::initStdSections()
103{
104  ObjectBuilder builder(*m_pModule);
105
106  // initialize standard sections
107  if (!m_LDBackend.initStdSections(builder))
108    return false;
109
110  // initialize target-dependent sections
111  m_LDBackend.initTargetSections(*m_pModule, builder);
112
113  return true;
114}
115
116void ObjectLinker::addUndefinedSymbols()
117{
118  // Add the symbol set by -u as an undefind global symbol into symbol pool
119  GeneralOptions::const_undef_sym_iterator usym;
120  GeneralOptions::const_undef_sym_iterator usymEnd =
121                                             m_Config.options().undef_sym_end();
122  for (usym = m_Config.options().undef_sym_begin(); usym != usymEnd; ++usym) {
123    Resolver::Result result;
124    m_pModule->getNamePool().insertSymbol(*usym, // name
125                                          false, // isDyn
126                                          ResolveInfo::NoType,
127                                          ResolveInfo::Undefined,
128                                          ResolveInfo::Global,
129                                          0x0, // size
130                                          0x0, // value
131                                          ResolveInfo::Default,
132                                          NULL,
133                                          result);
134
135    LDSymbol* output_sym = result.info->outSymbol();
136    bool has_output_sym = (NULL != output_sym);
137
138    // create the output symbol if it dose not have one
139    if (!result.existent || !has_output_sym) {
140      output_sym = LDSymbol::Create(*result.info);
141      result.info->setSymPtr(output_sym);
142      output_sym->setFragmentRef(FragmentRef::Null());
143    }
144  }
145}
146
147void ObjectLinker::normalize()
148{
149  // -----  set up inputs  ----- //
150  Module::input_iterator input, inEnd = m_pModule->input_end();
151  for (input = m_pModule->input_begin(); input!=inEnd; ++input) {
152    // is a group node
153    if (isGroup(input)) {
154      getGroupReader()->readGroup(input, inEnd, m_pBuilder->getInputBuilder(),
155                                  m_Config);
156      continue;
157    }
158
159    // already got type - for example, bitcode or external OIR (object
160    // intermediate representation)
161    if ((*input)->type() == Input::Script ||
162        (*input)->type() == Input::Archive ||
163        (*input)->type() == Input::External)
164      continue;
165
166    if (Input::Object == (*input)->type()) {
167      m_pModule->getObjectList().push_back(*input);
168      continue;
169    }
170
171    if (Input::DynObj == (*input)->type()) {
172      m_pModule->getLibraryList().push_back(*input);
173      continue;
174    }
175
176    bool doContinue = false;
177    // read input as a binary file
178    if (getBinaryReader()->isMyFormat(**input, doContinue)) {
179      (*input)->setType(Input::Object);
180      getBinaryReader()->readBinary(**input);
181      m_pModule->getObjectList().push_back(*input);
182    }
183    // is a relocatable object file
184    else if (doContinue && getObjectReader()->isMyFormat(**input, doContinue)) {
185      (*input)->setType(Input::Object);
186      getObjectReader()->readHeader(**input);
187      getObjectReader()->readSections(**input);
188      getObjectReader()->readSymbols(**input);
189      m_pModule->getObjectList().push_back(*input);
190    }
191    // is a shared object file
192    else if (doContinue && getDynObjReader()->isMyFormat(**input, doContinue)) {
193      (*input)->setType(Input::DynObj);
194      getDynObjReader()->readHeader(**input);
195      getDynObjReader()->readSymbols(**input);
196      m_pModule->getLibraryList().push_back(*input);
197    }
198    // is an archive
199    else if (doContinue && getArchiveReader()->isMyFormat(**input, doContinue)) {
200      (*input)->setType(Input::Archive);
201      if (m_Config.options().isInExcludeLIBS(**input)) {
202        (*input)->setNoExport();
203      }
204      Archive archive(**input, m_pBuilder->getInputBuilder());
205      getArchiveReader()->readArchive(m_Config, archive);
206      if(archive.numOfObjectMember() > 0) {
207        m_pModule->getInputTree().merge<InputTree::Inclusive>(input,
208                                                              archive.inputs());
209      }
210    }
211    // try to parse input as a linker script
212    else if (doContinue && getScriptReader()->isMyFormat(**input, doContinue)) {
213      ScriptFile script(ScriptFile::LDScript, **input,
214                        m_pBuilder->getInputBuilder());
215      if (getScriptReader()->readScript(m_Config, script)) {
216        (*input)->setType(Input::Script);
217        script.activate(*m_pModule);
218        if (script.inputs().size() > 0) {
219          m_pModule->getInputTree().merge<InputTree::Inclusive>(input,
220            script.inputs());
221        }
222      }
223    }
224    else {
225      if (m_Config.options().warnMismatch())
226        warning(diag::warn_unrecognized_input_file) << (*input)->path()
227          << m_Config.targets().triple().str();
228    }
229  } // end of for
230}
231
232bool ObjectLinker::linkable() const
233{
234  // check we have input and output files
235  if (m_pModule->getInputTree().empty()) {
236    error(diag::err_no_inputs);
237    return false;
238  }
239
240  // can not mix -static with shared objects
241  Module::const_lib_iterator lib, libEnd = m_pModule->lib_end();
242  for (lib = m_pModule->lib_begin(); lib != libEnd; ++lib) {
243    if((*lib)->attribute()->isStatic()) {
244      error(diag::err_mixed_shared_static_objects)
245                                      << (*lib)->name() << (*lib)->path();
246      return false;
247    }
248  }
249
250  // --nmagic and --omagic options lead to static executable program.
251  // These options turn off page alignment of sections. Because the
252  // sections are not aligned to pages, these sections can not contain any
253  // exported functions. Also, because the two options disable linking
254  // against shared libraries, the output absolutely does not call outside
255  // functions.
256  if (m_Config.options().nmagic() && !m_Config.isCodeStatic()) {
257    error(diag::err_nmagic_not_static);
258    return false;
259  }
260  if (m_Config.options().omagic() && !m_Config.isCodeStatic()) {
261    error(diag::err_omagic_not_static);
262    return false;
263  }
264
265  return true;
266}
267
268void ObjectLinker::dataStrippingOpt()
269{
270  // Garbege collection
271  if (m_Config.options().GCSections()) {
272    GarbageCollection GC(m_Config, m_LDBackend, *m_pModule);
273    GC.run();
274  }
275
276  // Identical code folding
277  if (m_Config.options().getICFMode() != GeneralOptions::ICF_None) {
278    IdenticalCodeFolding icf(m_Config, m_LDBackend, *m_pModule);
279    icf.foldIdenticalCode();
280  }
281  return;
282}
283
284/// readRelocations - read all relocation entries
285///
286/// All symbols should be read and resolved before this function.
287bool ObjectLinker::readRelocations()
288{
289  // Bitcode is read by the other path. This function reads relocation sections
290  // in object files.
291  mcld::InputTree::bfs_iterator input, inEnd = m_pModule->getInputTree().bfs_end();
292  for (input=m_pModule->getInputTree().bfs_begin(); input!=inEnd; ++input) {
293    if ((*input)->type() == Input::Object && (*input)->hasMemArea()) {
294      if (!getObjectReader()->readRelocations(**input))
295        return false;
296    }
297    // ignore the other kinds of files.
298  }
299  return true;
300}
301
302/// mergeSections - put allinput sections into output sections
303bool ObjectLinker::mergeSections()
304{
305  ObjectBuilder builder(*m_pModule);
306  Module::obj_iterator obj, objEnd = m_pModule->obj_end();
307  for (obj = m_pModule->obj_begin(); obj != objEnd; ++obj) {
308    LDContext::sect_iterator sect, sectEnd = (*obj)->context()->sectEnd();
309    for (sect = (*obj)->context()->sectBegin(); sect != sectEnd; ++sect) {
310      switch ((*sect)->kind()) {
311        // Some *INPUT sections should not be merged.
312        case LDFileFormat::Folded:
313        case LDFileFormat::Ignore:
314        case LDFileFormat::Null:
315        case LDFileFormat::NamePool:
316        case LDFileFormat::Group:
317        case LDFileFormat::StackNote:
318          // skip
319          continue;
320        case LDFileFormat::Relocation: {
321          if (!(*sect)->hasRelocData())
322            continue; // skip
323
324          if ((*sect)->getLink()->kind() == LDFileFormat::Ignore ||
325              (*sect)->getLink()->kind() == LDFileFormat::Folded)
326            (*sect)->setKind(LDFileFormat::Ignore);
327          break;
328        }
329        case LDFileFormat::Target:
330          if (!m_LDBackend.mergeSection(*m_pModule, **obj, **sect)) {
331            error(diag::err_cannot_merge_section) << (*sect)->name()
332                                                  << (*obj)->name();
333            return false;
334          }
335          break;
336        case LDFileFormat::EhFrame: {
337          if (!(*sect)->hasEhFrame())
338            continue; // skip
339
340          LDSection* out_sect = NULL;
341          if (NULL != (out_sect = builder.MergeSection(**obj, **sect))) {
342            if (!m_LDBackend.updateSectionFlags(*out_sect, **sect)) {
343              error(diag::err_cannot_merge_section) << (*sect)->name()
344                                                    << (*obj)->name();
345              return false;
346            }
347          }
348          break;
349        }
350        default: {
351          if (!(*sect)->hasSectionData())
352            continue; // skip
353
354          LDSection* out_sect = NULL;
355          if (NULL != (out_sect = builder.MergeSection(**obj, **sect))) {
356            if (!m_LDBackend.updateSectionFlags(*out_sect, **sect)) {
357              error(diag::err_cannot_merge_section) << (*sect)->name()
358                                                    << (*obj)->name();
359              return false;
360            }
361          }
362          break;
363        }
364      } // end of switch
365    } // for each section
366  } // for each obj
367
368  RpnEvaluator evaluator(*m_pModule, m_LDBackend);
369  SectionMap::iterator out, outBegin, outEnd;
370  outBegin = m_pModule->getScript().sectionMap().begin();
371  outEnd = m_pModule->getScript().sectionMap().end();
372  for (out = outBegin; out != outEnd; ++out) {
373    uint64_t out_align = 0x0, in_align = 0x0;
374    LDSection* out_sect = (*out)->getSection();
375    SectionMap::Output::iterator in, inBegin, inEnd;
376    inBegin = (*out)->begin();
377    inEnd = (*out)->end();
378
379    // force input alignment from ldscript if any
380    if ((*out)->prolog().hasSubAlign()) {
381      evaluator.eval((*out)->prolog().subAlign(), in_align);
382    }
383
384    for (in = inBegin; in != inEnd; ++in) {
385      LDSection* in_sect = (*in)->getSection();
386      if ((*out)->prolog().hasSubAlign())
387        in_sect->setAlign(in_align);
388
389      if (builder.MoveSectionData(*in_sect->getSectionData(),
390                                  *out_sect->getSectionData())) {
391        builder.UpdateSectionAlign(*out_sect, *in_sect);
392        m_LDBackend.updateSectionFlags(*out_sect, *in_sect);
393      }
394    } // for each input section description
395
396    // force output alignment from ldscript if any
397    if ((*out)->prolog().hasAlign()) {
398      evaluator.eval((*out)->prolog().align(), out_align);
399      out_sect->setAlign(out_align);
400    }
401
402    if ((*out)->hasContent()) {
403      LDSection* target = m_pModule->getSection((*out)->name());
404      assert(target != NULL && target->hasSectionData());
405      if (builder.MoveSectionData(*out_sect->getSectionData(),
406                                  *target->getSectionData())) {
407        builder.UpdateSectionAlign(*target, *out_sect);
408        m_LDBackend.updateSectionFlags(*target, *out_sect);
409      }
410    }
411  } // for each output section description
412
413  return true;
414}
415
416void ObjectLinker::addSymbolToOutput(ResolveInfo& pInfo, Module& pModule)
417{
418  // section symbols will be defined by linker later, we should not add section
419  // symbols to output here
420  if (ResolveInfo::Section == pInfo.type() || NULL == pInfo.outSymbol())
421    return;
422
423  // if the symbols defined in the Ignore sections (e.g. discared by GC), then
424  // not to put them to output
425  if (pInfo.outSymbol()->hasFragRef() && LDFileFormat::Ignore ==
426        pInfo.outSymbol()->fragRef()->frag()->getParent()->getSection().kind())
427    return;
428
429  if (pInfo.shouldForceLocal(m_Config))
430    pModule.getSymbolTable().forceLocal(*pInfo.outSymbol());
431  else
432    pModule.getSymbolTable().add(*pInfo.outSymbol());
433}
434
435void ObjectLinker::addSymbolsToOutput(Module& pModule)
436{
437  // Traverse all the free ResolveInfo and add the output symobols to output
438  NamePool::freeinfo_iterator free_it,
439                              free_end = pModule.getNamePool().freeinfo_end();
440  for (free_it = pModule.getNamePool().freeinfo_begin(); free_it != free_end;
441                                                                      ++free_it)
442    addSymbolToOutput(**free_it, pModule);
443
444
445  // Traverse all the resolveInfo and add the output symbol to output
446  NamePool::syminfo_iterator info_it,
447                             info_end = pModule.getNamePool().syminfo_end();
448  for (info_it = pModule.getNamePool().syminfo_begin(); info_it != info_end;
449                                                                      ++info_it)
450    addSymbolToOutput(*info_it.getEntry(), pModule);
451}
452
453
454/// addStandardSymbols - shared object and executable files need some
455/// standard symbols
456///   @return if there are some input symbols with the same name to the
457///   standard symbols, return false
458bool ObjectLinker::addStandardSymbols()
459{
460  // create and add section symbols for each output section
461  Module::iterator iter, iterEnd = m_pModule->end();
462  for (iter = m_pModule->begin(); iter != iterEnd; ++iter) {
463    m_pModule->getSectionSymbolSet().add(**iter, m_pModule->getNamePool());
464  }
465
466  return m_LDBackend.initStandardSymbols(*m_pBuilder, *m_pModule);
467}
468
469/// addTargetSymbols - some targets, such as MIPS and ARM, need some
470/// target-dependent symbols
471///   @return if there are some input symbols with the same name to the
472///   target symbols, return false
473bool ObjectLinker::addTargetSymbols()
474{
475  m_LDBackend.initTargetSymbols(*m_pBuilder, *m_pModule);
476  return true;
477}
478
479/// addScriptSymbols - define symbols from the command line option or linker
480/// scripts.
481bool ObjectLinker::addScriptSymbols()
482{
483  LinkerScript& script = m_pModule->getScript();
484  LinkerScript::Assignments::iterator it, ie = script.assignments().end();
485  // go through the entire symbol assignments
486  for (it = script.assignments().begin(); it != ie; ++it) {
487    LDSymbol* symbol = NULL;
488    assert((*it).second.symbol().type() == Operand::SYMBOL);
489    const llvm::StringRef symName =  (*it).second.symbol().name();
490    ResolveInfo::Type       type = ResolveInfo::NoType;
491    ResolveInfo::Visibility vis  = ResolveInfo::Default;
492    size_t size = 0;
493    ResolveInfo* old_info = m_pModule->getNamePool().findInfo(symName);
494    // if the symbol does not exist, we can set type to NOTYPE
495    // else we retain its type, same goes for size - 0 or retain old value
496    // and visibility - Default or retain
497    if (old_info != NULL) {
498      type = static_cast<ResolveInfo::Type>(old_info->type());
499      vis = old_info->visibility();
500      size = old_info->size();
501    }
502
503    // Add symbol and refine the visibility if needed
504    // FIXME: bfd linker would change the binding instead, but currently
505    //        ABS is also a kind of Binding in ResolveInfo.
506    switch ((*it).second.type()) {
507    case Assignment::HIDDEN:
508      vis = ResolveInfo::Hidden;
509      // Fall through
510    case Assignment::DEFAULT:
511      symbol =
512        m_pBuilder->AddSymbol<IRBuilder::Force,
513                              IRBuilder::Unresolve>(symName,
514                                                    type,
515                                                    ResolveInfo::Define,
516                                                    ResolveInfo::Absolute,
517                                                    size,
518                                                    0x0,
519                                                    FragmentRef::Null(),
520                                                    vis);
521      break;
522    case Assignment::PROVIDE_HIDDEN:
523      vis = ResolveInfo::Hidden;
524      // Fall through
525    case Assignment::PROVIDE:
526      symbol =
527        m_pBuilder->AddSymbol<IRBuilder::AsReferred,
528                              IRBuilder::Unresolve>(symName,
529                                                    type,
530                                                    ResolveInfo::Define,
531                                                    ResolveInfo::Absolute,
532                                                    size,
533                                                    0x0,
534                                                    FragmentRef::Null(),
535                                                    vis);
536      break;
537    }
538    // Set symbol of this assignment.
539    (*it).first = symbol;
540  }
541  return true;
542}
543
544bool ObjectLinker::scanRelocations()
545{
546  // apply all relocations of all inputs
547  Module::obj_iterator input, inEnd = m_pModule->obj_end();
548  for (input = m_pModule->obj_begin(); input != inEnd; ++input) {
549    m_LDBackend.getRelocator()->initializeScan(**input);
550    LDContext::sect_iterator rs, rsEnd = (*input)->context()->relocSectEnd();
551    for (rs = (*input)->context()->relocSectBegin(); rs != rsEnd; ++rs) {
552      // bypass the reloc section if
553      // 1. its section kind is changed to Ignore. (The target section is a
554      // discarded group section.)
555      // 2. it has no reloc data. (All symbols in the input relocs are in the
556      // discarded group sections)
557      if (LDFileFormat::Ignore == (*rs)->kind() || !(*rs)->hasRelocData())
558        continue;
559      RelocData::iterator reloc, rEnd = (*rs)->getRelocData()->end();
560      for (reloc = (*rs)->getRelocData()->begin(); reloc != rEnd; ++reloc) {
561        Relocation* relocation = llvm::cast<Relocation>(reloc);
562
563        // bypass the reloc if the symbol is in the discarded input section
564        ResolveInfo* info = relocation->symInfo();
565        if (!info->outSymbol()->hasFragRef() &&
566            ResolveInfo::Section == info->type() &&
567            ResolveInfo::Undefined == info->desc())
568           continue;
569
570        // scan relocation
571        if (LinkerConfig::Object != m_Config.codeGenType())
572          m_LDBackend.getRelocator()->scanRelocation(
573                                    *relocation, *m_pBuilder, *m_pModule, **rs, **input);
574        else
575          m_LDBackend.getRelocator()->partialScanRelocation(
576                                                 *relocation, *m_pModule, **rs);
577      } // for all relocations
578    } // for all relocation section
579    m_LDBackend.getRelocator()->finalizeScan(**input);
580  } // for all inputs
581  return true;
582}
583
584/// initStubs - initialize stub-related stuff.
585bool ObjectLinker::initStubs()
586{
587  // initialize BranchIslandFactory
588  m_LDBackend.initBRIslandFactory();
589
590  // initialize StubFactory
591  m_LDBackend.initStubFactory();
592
593  // initialize target stubs
594  m_LDBackend.initTargetStubs();
595  return true;
596}
597
598/// allocateCommonSymobols - allocate fragments for common symbols to the
599/// corresponding sections
600bool ObjectLinker::allocateCommonSymbols()
601{
602  if (LinkerConfig::Object != m_Config.codeGenType() ||
603      m_Config.options().isDefineCommon())
604    return m_LDBackend.allocateCommonSymbols(*m_pModule);
605  return true;
606}
607
608/// prelayout - help backend to do some modification before layout
609bool ObjectLinker::prelayout()
610{
611  // finalize the section symbols, set their fragment reference and push them
612  // into output symbol table
613  Module::iterator sect, sEnd = m_pModule->end();
614  for (sect = m_pModule->begin(); sect != sEnd; ++sect) {
615    m_pModule->getSectionSymbolSet().finalize(**sect,
616        m_pModule->getSymbolTable(),
617        m_Config.codeGenType() == LinkerConfig::Object);
618  }
619
620  m_LDBackend.preLayout(*m_pModule, *m_pBuilder);
621
622  /// check program interpreter - computer the name size of the runtime dyld
623  if (!m_Config.isCodeStatic() &&
624      (LinkerConfig::Exec == m_Config.codeGenType() ||
625       m_Config.options().isPIE() ||
626       m_Config.options().hasDyld()))
627    m_LDBackend.sizeInterp();
628
629  /// measure NamePools - compute the size of name pool sections
630  /// In ELF, will compute  the size of.symtab, .strtab, .dynsym, .dynstr,
631  /// .hash and .shstrtab sections.
632  ///
633  /// dump all symbols and strings from ObjectLinker and build the format-dependent
634  /// hash table.
635  /// @note sizeNamePools replies on LinkerConfig::CodePosition. Must determine
636  /// code position model before calling GNULDBackend::sizeNamePools()
637  m_LDBackend.sizeNamePools(*m_pModule);
638
639  // Do this after backend prelayout since it may add eh_frame entries.
640  LDSection* eh_frame_sect = m_pModule->getSection(".eh_frame");
641  if (eh_frame_sect && eh_frame_sect->hasEhFrame())
642    eh_frame_sect->getEhFrame()->computeOffsetSize();
643  m_LDBackend.createAndSizeEhFrameHdr(*m_pModule);
644
645  return true;
646}
647
648/// layout - linearly layout all output sections and reserve some space
649/// for GOT/PLT
650///   Because we do not support instruction relaxing in this early version,
651///   if there is a branch can not jump to its target, we return false
652///   directly
653bool ObjectLinker::layout()
654{
655  m_LDBackend.layout(*m_pModule);
656  return true;
657}
658
659/// prelayout - help backend to do some modification after layout
660bool ObjectLinker::postlayout()
661{
662  m_LDBackend.postLayout(*m_pModule, *m_pBuilder);
663  return true;
664}
665
666/// finalizeSymbolValue - finalize the resolved symbol value.
667///   Before relocate(), after layout(), ObjectLinker should correct value of all
668///   symbol.
669bool ObjectLinker::finalizeSymbolValue()
670{
671  Module::sym_iterator symbol, symEnd = m_pModule->sym_end();
672  for (symbol = m_pModule->sym_begin(); symbol != symEnd; ++symbol) {
673
674    if ((*symbol)->resolveInfo()->isAbsolute() ||
675        (*symbol)->resolveInfo()->type() == ResolveInfo::File) {
676      // absolute symbols should just use its value directly (i.e., the result
677      // of symbol resolution)
678      continue;
679    }
680
681    if ((*symbol)->resolveInfo()->type() == ResolveInfo::ThreadLocal) {
682      m_LDBackend.finalizeTLSSymbol(**symbol);
683      continue;
684    }
685
686    if ((*symbol)->hasFragRef()) {
687      // set the virtual address of the symbol. If the output file is
688      // relocatable object file, the section's virtual address becomes zero.
689      // And the symbol's value become section relative offset.
690      uint64_t value = (*symbol)->fragRef()->getOutputOffset();
691      assert(NULL != (*symbol)->fragRef()->frag());
692      uint64_t addr =
693        (*symbol)->fragRef()->frag()->getParent()->getSection().addr();
694      (*symbol)->setValue(value + addr);
695      continue;
696    }
697  }
698
699  RpnEvaluator evaluator(*m_pModule, m_LDBackend);
700  bool finalized = m_LDBackend.finalizeSymbols();
701  bool scriptSymsFinalized = true;
702  LinkerScript& script = m_pModule->getScript();
703  LinkerScript::Assignments::iterator assign, assignEnd;
704  assignEnd = script.assignments().end();
705  for (assign = script.assignments().begin(); assign != assignEnd; ++assign) {
706    LDSymbol* symbol = (*assign).first;
707    Assignment& assignment = (*assign).second;
708
709    if (symbol == NULL)
710      continue;
711
712    scriptSymsFinalized &= assignment.assign(evaluator);
713    if (!scriptSymsFinalized)
714      break;
715
716    symbol->setValue(assignment.symbol().value());
717  } // for each script symbol assignment
718
719  bool assertionsPassed = true;
720  LinkerScript::Assertions::iterator assert, assertEnd;
721  assertEnd = script.assertions().end();
722  for (assert = script.assertions().begin(); assert != assertEnd; ++assert) {
723    uint64_t res = 0x0;
724    evaluator.eval((*assert).getRpnExpr(), res);
725    if (res == 0x0)
726      fatal(diag::err_assert_failed) << (*assert).message();
727  } // for each assertion in ldscript
728
729  return finalized && scriptSymsFinalized && assertionsPassed;
730}
731
732/// relocate - applying relocation entries and create relocation
733/// section in the output files
734/// Create relocation section, asking TargetLDBackend to
735/// read the relocation information into RelocationEntry
736/// and push_back into the relocation section
737bool ObjectLinker::relocation()
738{
739  // when producing relocatables, no need to apply relocation
740  if (LinkerConfig::Object == m_Config.codeGenType())
741    return true;
742
743  // apply all relocations of all inputs
744  Module::obj_iterator input, inEnd = m_pModule->obj_end();
745  for (input = m_pModule->obj_begin(); input != inEnd; ++input) {
746    m_LDBackend.getRelocator()->initializeApply(**input);
747    LDContext::sect_iterator rs, rsEnd = (*input)->context()->relocSectEnd();
748    for (rs = (*input)->context()->relocSectBegin(); rs != rsEnd; ++rs) {
749      // bypass the reloc section if
750      // 1. its section kind is changed to Ignore. (The target section is a
751      // discarded group section.)
752      // 2. it has no reloc data. (All symbols in the input relocs are in the
753      // discarded group sections)
754      if (LDFileFormat::Ignore == (*rs)->kind() || !(*rs)->hasRelocData())
755        continue;
756      RelocData::iterator reloc, rEnd = (*rs)->getRelocData()->end();
757      for (reloc = (*rs)->getRelocData()->begin(); reloc != rEnd; ++reloc) {
758        Relocation* relocation = llvm::cast<Relocation>(reloc);
759
760        // bypass the reloc if the symbol is in the discarded input section
761        ResolveInfo* info = relocation->symInfo();
762        if (!info->outSymbol()->hasFragRef() &&
763            ResolveInfo::Section == info->type() &&
764            ResolveInfo::Undefined == info->desc())
765          continue;
766
767        relocation->apply(*m_LDBackend.getRelocator());
768      } // for all relocations
769    } // for all relocation section
770    m_LDBackend.getRelocator()->finalizeApply(**input);
771  } // for all inputs
772
773  // apply relocations created by relaxation
774  BranchIslandFactory* br_factory = m_LDBackend.getBRIslandFactory();
775  BranchIslandFactory::iterator facIter, facEnd = br_factory->end();
776  for (facIter = br_factory->begin(); facIter != facEnd; ++facIter) {
777    BranchIsland& island = *facIter;
778    BranchIsland::reloc_iterator iter, iterEnd = island.reloc_end();
779    for (iter = island.reloc_begin(); iter != iterEnd; ++iter)
780      (*iter)->apply(*m_LDBackend.getRelocator());
781  }
782  return true;
783}
784
785/// emitOutput - emit the output file.
786bool ObjectLinker::emitOutput(FileOutputBuffer& pOutput)
787{
788  return std::error_code() == getWriter()->writeObject(*m_pModule, pOutput);
789}
790
791
792/// postProcessing - do modification after all processes
793bool ObjectLinker::postProcessing(FileOutputBuffer& pOutput)
794{
795  if (LinkerConfig::Object != m_Config.codeGenType())
796    normalSyncRelocationResult(pOutput);
797  else
798    partialSyncRelocationResult(pOutput);
799
800  // emit .eh_frame_hdr
801  // eh_frame_hdr should be emitted after syncRelocation, because eh_frame_hdr
802  // needs FDE PC value, which will be corrected at syncRelocation
803  m_LDBackend.postProcessing(pOutput);
804  return true;
805}
806
807void ObjectLinker::normalSyncRelocationResult(FileOutputBuffer& pOutput)
808{
809  uint8_t* data = pOutput.getBufferStart();
810
811  // sync all relocations of all inputs
812  Module::obj_iterator input, inEnd = m_pModule->obj_end();
813  for (input = m_pModule->obj_begin(); input != inEnd; ++input) {
814    LDContext::sect_iterator rs, rsEnd = (*input)->context()->relocSectEnd();
815    for (rs = (*input)->context()->relocSectBegin(); rs != rsEnd; ++rs) {
816      // bypass the reloc section if
817      // 1. its section kind is changed to Ignore. (The target section is a
818      // discarded group section.)
819      // 2. it has no reloc data. (All symbols in the input relocs are in the
820      // discarded group sections)
821      if (LDFileFormat::Ignore == (*rs)->kind() || !(*rs)->hasRelocData())
822        continue;
823      RelocData::iterator reloc, rEnd = (*rs)->getRelocData()->end();
824      for (reloc = (*rs)->getRelocData()->begin(); reloc != rEnd; ++reloc) {
825        Relocation* relocation = llvm::cast<Relocation>(reloc);
826
827        // bypass the reloc if the symbol is in the discarded input section
828        ResolveInfo* info = relocation->symInfo();
829        if (!info->outSymbol()->hasFragRef() &&
830            ResolveInfo::Section == info->type() &&
831            ResolveInfo::Undefined == info->desc())
832          continue;
833
834        // bypass the relocation with NONE type. This is to avoid overwrite the
835        // target result by NONE type relocation if there is a place which has
836        // two relocations to apply to, and one of it is NONE type. The result
837        // we want is the value of the other relocation result. For example,
838        // in .exidx, there are usually an R_ARM_NONE and R_ARM_PREL31 apply to
839        // the same place
840        if (0x0 == relocation->type())
841          continue;
842        writeRelocationResult(*relocation, data);
843      } // for all relocations
844    } // for all relocation section
845  } // for all inputs
846
847  // sync relocations created by relaxation
848  BranchIslandFactory* br_factory = m_LDBackend.getBRIslandFactory();
849  BranchIslandFactory::iterator facIter, facEnd = br_factory->end();
850  for (facIter = br_factory->begin(); facIter != facEnd; ++facIter) {
851    BranchIsland& island = *facIter;
852    BranchIsland::reloc_iterator iter, iterEnd = island.reloc_end();
853    for (iter = island.reloc_begin(); iter != iterEnd; ++iter) {
854      Relocation* reloc = *iter;
855      writeRelocationResult(*reloc, data);
856    }
857  }
858}
859
860void ObjectLinker::partialSyncRelocationResult(FileOutputBuffer& pOutput)
861{
862  uint8_t* data = pOutput.getBufferStart();
863
864  // traverse outputs' LDSection to get RelocData
865  Module::iterator sectIter, sectEnd = m_pModule->end();
866  for (sectIter = m_pModule->begin(); sectIter != sectEnd; ++sectIter) {
867    if (LDFileFormat::Relocation != (*sectIter)->kind())
868      continue;
869
870    RelocData* reloc_data = (*sectIter)->getRelocData();
871    RelocData::iterator relocIter, relocEnd = reloc_data->end();
872    for (relocIter = reloc_data->begin(); relocIter != relocEnd; ++relocIter) {
873      Relocation* reloc = llvm::cast<Relocation>(relocIter);
874
875      // bypass the relocation with NONE type. This is to avoid overwrite the
876      // target result by NONE type relocation if there is a place which has
877      // two relocations to apply to, and one of it is NONE type. The result
878      // we want is the value of the other relocation result. For example,
879      // in .exidx, there are usually an R_ARM_NONE and R_ARM_PREL31 apply to
880      // the same place
881      if (0x0 == reloc->type())
882        continue;
883      writeRelocationResult(*reloc, data);
884    }
885  }
886}
887
888void ObjectLinker::writeRelocationResult(Relocation& pReloc, uint8_t* pOutput)
889{
890  // get output file offset
891  size_t out_offset =
892                 pReloc.targetRef().frag()->getParent()->getSection().offset() +
893                 pReloc.targetRef().getOutputOffset();
894
895  uint8_t* target_addr = pOutput + out_offset;
896  // byte swapping if target and host has different endian, and then write back
897  if(llvm::sys::IsLittleEndianHost != m_Config.targets().isLittleEndian()) {
898     uint64_t tmp_data = 0;
899
900     switch(pReloc.size(*m_LDBackend.getRelocator())) {
901       case 8u:
902         std::memcpy(target_addr, &pReloc.target(), 1);
903         break;
904
905       case 16u:
906         tmp_data = mcld::bswap16(pReloc.target());
907         std::memcpy(target_addr, &tmp_data, 2);
908         break;
909
910       case 32u:
911         tmp_data = mcld::bswap32(pReloc.target());
912         std::memcpy(target_addr, &tmp_data, 4);
913         break;
914
915       case 64u:
916         tmp_data = mcld::bswap64(pReloc.target());
917         std::memcpy(target_addr, &tmp_data, 8);
918         break;
919
920       default:
921         break;
922    }
923  }
924  else
925    std::memcpy(target_addr, &pReloc.target(),
926                                      pReloc.size(*m_LDBackend.getRelocator())/8);
927}
928
929