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