122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===- ObjectLinker.cpp ---------------------------------------------------===//
222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//
322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//                     The MCLinker Project
422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//
522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao// This file is distributed under the University of Illinois Open Source
622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao// License. See LICENSE.TXT for details.
722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//
822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===----------------------------------------------------------------------===//
922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Object/ObjectLinker.h>
1022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
1122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LinkerConfig.h>
1222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Module.h>
1322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/InputTree.h>
14d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <mcld/IRBuilder.h>
1522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/LDSection.h>
1622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/LDContext.h>
1722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/Archive.h>
1822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/ArchiveReader.h>
1922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/ObjectReader.h>
2022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/DynObjReader.h>
2122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/GroupReader.h>
22d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <mcld/LD/BinaryReader.h>
2322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/ObjectWriter.h>
2422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/ResolveInfo.h>
2522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/RelocData.h>
2622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Support/RealPath.h>
2722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Support/MemoryArea.h>
2822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Support/MsgHandling.h>
2922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Target/TargetLDBackend.h>
3022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Fragment/FragmentLinker.h>
3122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Object/ObjectBuilder.h>
3222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <llvm/Support/Casting.h>
3422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
356f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
3622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaousing namespace llvm;
3722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaousing namespace mcld;
3822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei LiaoObjectLinker::ObjectLinker(const LinkerConfig& pConfig,
4022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                           TargetLDBackend& pLDBackend)
4122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  : m_Config(pConfig),
4222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    m_pLinker(NULL),
436f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    m_pModule(NULL),
446f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    m_pBuilder(NULL),
4522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    m_LDBackend(pLDBackend),
4622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    m_pObjectReader(NULL),
4722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    m_pDynObjReader(NULL),
4822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    m_pArchiveReader(NULL),
49d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    m_pGroupReader(NULL),
50d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    m_pBinaryReader(NULL),
516f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    m_pWriter(NULL) {
5222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
5322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
5422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei LiaoObjectLinker::~ObjectLinker()
5522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
5622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  delete m_pLinker;
5722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  delete m_pObjectReader;
5822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  delete m_pDynObjReader;
5922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  delete m_pArchiveReader;
60d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  delete m_pGroupReader;
61d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  delete m_pBinaryReader;
626f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  delete m_pWriter;
636f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines}
646f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
656f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hinesvoid ObjectLinker::setup(Module& pModule, IRBuilder& pBuilder)
666f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines{
676f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_pModule = &pModule;
686f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_pBuilder = &pBuilder;
696f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  // set up soname
706f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  if (!m_Config.options().soname().empty()) {
716f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    m_pModule->setName(m_Config.options().soname());
726f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  }
7322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
7422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// initFragmentLinker - initialize FragmentLinker
7622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///  Connect all components with FragmentLinker
7722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::initFragmentLinker()
7822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
7922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  if (NULL == m_pLinker) {
8022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    m_pLinker = new FragmentLinker(m_Config,
816f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                                   *m_pModule,
8222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                   m_LDBackend);
8322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  }
8422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
8522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // initialize the readers and writers
8622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // Because constructor can not be failed, we initalize all readers and
8722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // writers outside the FragmentLinker constructors.
886f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_pObjectReader  = m_LDBackend.createObjectReader(*m_pBuilder);
896f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_pArchiveReader = m_LDBackend.createArchiveReader(*m_pModule);
906f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_pDynObjReader  = m_LDBackend.createDynObjReader(*m_pBuilder);
916f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_pGroupReader   = new GroupReader(*m_pModule, *m_pObjectReader,
9222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                     *m_pDynObjReader, *m_pArchiveReader);
936f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_pBinaryReader  = m_LDBackend.createBinaryReader(*m_pBuilder);
946f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_pWriter        = m_LDBackend.createWriter();
9522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
96d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  // initialize Relocator
976f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.initRelocator();
9822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
9922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
10022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
10122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// initStdSections - initialize standard sections
10222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::initStdSections()
10322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
1046f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ObjectBuilder builder(m_Config, *m_pModule);
10522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
10622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // initialize standard sections
10722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  if (!m_LDBackend.initStdSections(builder))
10822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    return false;
10922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
11022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // initialize target-dependent sections
1116f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.initTargetSections(*m_pModule, builder);
11222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
11322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
11422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
11522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
11622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaovoid ObjectLinker::normalize()
11722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
11822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // -----  set up inputs  ----- //
1196f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  Module::input_iterator input, inEnd = m_pModule->input_end();
1206f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  for (input = m_pModule->input_begin(); input!=inEnd; ++input) {
12122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // is a group node
12222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    if (isGroup(input)) {
1236f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines      getGroupReader()->readGroup(input, m_pBuilder->getInputBuilder(), m_Config);
12422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      continue;
12522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    }
12622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
12722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // already got type - for example, bitcode or external OIR (object
12822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // intermediate representation)
12922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    if ((*input)->type() == Input::Script ||
13022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        (*input)->type() == Input::Archive ||
13122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        (*input)->type() == Input::External)
13222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      continue;
13322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
13422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    if (Input::Object == (*input)->type()) {
1356f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines      m_pModule->getObjectList().push_back(*input);
13622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      continue;
13722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    }
13822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
13922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    if (Input::DynObj == (*input)->type()) {
1406f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines      m_pModule->getLibraryList().push_back(*input);
14122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      continue;
14222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    }
14322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
144d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    // read input as a binary file
145d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    if (m_Config.options().isBinaryInput()) {
146d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao      (*input)->setType(Input::Object);
147d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao      getBinaryReader()->readBinary(**input);
1486f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines      m_pModule->getObjectList().push_back(*input);
149d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    }
15022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // is a relocatable object file
151d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    else if (getObjectReader()->isMyFormat(**input)) {
15222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      (*input)->setType(Input::Object);
15322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      getObjectReader()->readHeader(**input);
15422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      getObjectReader()->readSections(**input);
15522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      getObjectReader()->readSymbols(**input);
1566f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines      m_pModule->getObjectList().push_back(*input);
15722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    }
15822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // is a shared object file
15922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    else if (getDynObjReader()->isMyFormat(**input)) {
16022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      (*input)->setType(Input::DynObj);
16122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      getDynObjReader()->readHeader(**input);
16222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      getDynObjReader()->readSymbols(**input);
1636f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines      m_pModule->getLibraryList().push_back(*input);
16422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    }
16522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // is an archive
16622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    else if (getArchiveReader()->isMyFormat(**input)) {
16722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      (*input)->setType(Input::Archive);
1686f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines      Archive archive(**input, m_pBuilder->getInputBuilder());
16922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      getArchiveReader()->readArchive(archive);
17022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      if(archive.numOfObjectMember() > 0) {
1716f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines        m_pModule->getInputTree().merge<InputTree::Inclusive>(input,
17222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                                            archive.inputs());
17322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      }
17422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    }
17522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    else {
17622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      fatal(diag::err_unrecognized_input_file) << (*input)->path()
177d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                          << m_Config.targets().triple().str();
17822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    }
17922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  } // end of for
18022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
18122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
18222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::linkable() const
18322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
18422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // check we have input and output files
1856f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  if (m_pModule->getInputTree().empty()) {
18622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    error(diag::err_no_inputs);
18722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    return false;
18822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  }
18922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
19022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // can not mix -static with shared objects
1916f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  Module::const_lib_iterator lib, libEnd = m_pModule->lib_end();
1926f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  for (lib = m_pModule->lib_begin(); lib != libEnd; ++lib) {
19322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    if((*lib)->attribute()->isStatic()) {
19422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      error(diag::err_mixed_shared_static_objects)
19522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                      << (*lib)->name() << (*lib)->path();
19622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      return false;
19722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    }
19822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  }
19922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2006f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  // --nmagic and --omagic options lead to static executable program.
2016f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  // These options turn off page alignment of sections. Because the
2026f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  // sections are not aligned to pages, these sections can not contain any
2036f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  // exported functions. Also, because the two options disable linking
2046f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  // against shared libraries, the output absolutely does not call outside
2056f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  // functions.
2066f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  if (m_Config.options().nmagic() && !m_Config.isCodeStatic()) {
2076f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    error(diag::err_nmagic_not_static);
2086f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    return false;
2096f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  }
2106f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  if (m_Config.options().omagic() && !m_Config.isCodeStatic()) {
2116f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    error(diag::err_omagic_not_static);
2126f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    return false;
2136f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  }
2146f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
21522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
21622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
21722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
21822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// readRelocations - read all relocation entries
21922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///
22022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// All symbols should be read and resolved before this function.
22122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::readRelocations()
22222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
22322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // Bitcode is read by the other path. This function reads relocation sections
22422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // in object files.
2256f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  mcld::InputTree::bfs_iterator input, inEnd = m_pModule->getInputTree().bfs_end();
2266f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  for (input=m_pModule->getInputTree().bfs_begin(); input!=inEnd; ++input) {
22722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    if ((*input)->type() == Input::Object && (*input)->hasMemArea()) {
22822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      if (!getObjectReader()->readRelocations(**input))
22922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        return false;
23022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    }
23122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // ignore the other kinds of files.
23222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  }
23322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
23422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
23522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
23622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// mergeSections - put allinput sections into output sections
23722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::mergeSections()
23822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
2396f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ObjectBuilder builder(m_Config, *m_pModule);
2406f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  Module::obj_iterator obj, objEnd = m_pModule->obj_end();
2416f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  for (obj = m_pModule->obj_begin(); obj != objEnd; ++obj) {
24222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    LDContext::sect_iterator sect, sectEnd = (*obj)->context()->sectEnd();
24322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    for (sect = (*obj)->context()->sectBegin(); sect != sectEnd; ++sect) {
24422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      switch ((*sect)->kind()) {
24522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        // Some *INPUT sections should not be merged.
24622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        case LDFileFormat::Ignore:
24722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        case LDFileFormat::Null:
24822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        case LDFileFormat::Relocation:
24922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        case LDFileFormat::NamePool:
25022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        case LDFileFormat::Group:
25122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        case LDFileFormat::StackNote:
25222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao          // skip
25322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao          continue;
25422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        case LDFileFormat::Target:
2556f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines          if (!m_LDBackend.mergeSection(*m_pModule, **sect)) {
25622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            error(diag::err_cannot_merge_section) << (*sect)->name()
25722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                                  << (*obj)->name();
25822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            return false;
25922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao          }
26022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao          break;
26122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        case LDFileFormat::EhFrame: {
26222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao          if (!(*sect)->hasEhFrame())
26322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            continue; // skip
26422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2656f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines          if (NULL == builder.MergeSection(**sect)) {
26622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            error(diag::err_cannot_merge_section) << (*sect)->name()
26722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                                  << (*obj)->name();
26822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            return false;
26922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao          }
27022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao          break;
27122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        }
27222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        default: {
27322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao          if (!(*sect)->hasSectionData())
27422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            continue; // skip
27522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2766f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines          LDSection* out_sect = builder.MergeSection(**sect);
2776f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines          if (NULL != out_sect) {
2786f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines            if (!m_LDBackend.updateSectionFlags(*out_sect, **sect)) {
2796f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines              error(diag::err_cannot_merge_section) << (*sect)->name()
2806f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                                                    << (*obj)->name();
2816f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines              return false;
2826f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines            }
2836f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines          }
2846f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines          else {
28522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            error(diag::err_cannot_merge_section) << (*sect)->name()
28622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                                  << (*obj)->name();
28722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao            return false;
28822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao          }
28922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao          break;
29022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        }
29122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      } // end of switch
29222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    } // for each section
29322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  } // for each obj
29422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
29522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
29622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
29722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// addStandardSymbols - shared object and executable files need some
29822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// standard symbols
29922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///   @return if there are some input symbols with the same name to the
30022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///   standard symbols, return false
30122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::addStandardSymbols()
30222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
30322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // create and add section symbols for each output section
3046f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  Module::iterator iter, iterEnd = m_pModule->end();
3056f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  for (iter = m_pModule->begin(); iter != iterEnd; ++iter) {
3066f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    m_pModule->getSectionSymbolSet().add(**iter, m_pModule->getNamePool());
30722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  }
30822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3096f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  return m_LDBackend.initStandardSymbols(*m_pBuilder, *m_pModule);
31022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
31122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
31222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// addTargetSymbols - some targets, such as MIPS and ARM, need some
31322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// target-dependent symbols
31422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///   @return if there are some input symbols with the same name to the
31522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///   target symbols, return false
31622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::addTargetSymbols()
31722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
3186f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.initTargetSymbols(*m_pBuilder, *m_pModule);
3196f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  return true;
3206f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines}
3216f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
3226f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines/// addScriptSymbols - define symbols from the command line option or linker
3236f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines/// scripts.
3246f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines///   @return if there are some existing symbols with identical name to the
3256f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines///   script symbols, return false.
3266f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hinesbool ObjectLinker::addScriptSymbols()
3276f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines{
32822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
32922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
33022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
33122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::scanRelocations()
33222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
33322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // apply all relocations of all inputs
3346f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  Module::obj_iterator input, inEnd = m_pModule->obj_end();
3356f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  for (input = m_pModule->obj_begin(); input != inEnd; ++input) {
33622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    LDContext::sect_iterator rs, rsEnd = (*input)->context()->relocSectEnd();
33722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    for (rs = (*input)->context()->relocSectBegin(); rs != rsEnd; ++rs) {
33822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      // bypass the reloc section if
33922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      // 1. its section kind is changed to Ignore. (The target section is a
34022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      // discarded group section.)
34122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      // 2. it has no reloc data. (All symbols in the input relocs are in the
34222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      // discarded group sections)
34322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      if (LDFileFormat::Ignore == (*rs)->kind() || !(*rs)->hasRelocData())
34422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        continue;
34522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      RelocData::iterator reloc, rEnd = (*rs)->getRelocData()->end();
34622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      for (reloc = (*rs)->getRelocData()->begin(); reloc != rEnd; ++reloc) {
34722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        Relocation* relocation = llvm::cast<Relocation>(reloc);
34822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao        // scan relocation
3496f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines        if (LinkerConfig::Object != m_Config.codeGenType())
3506f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines          m_LDBackend.scanRelocation(*relocation, *m_pBuilder, *m_pModule, **rs);
3516f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines        else
3526f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines          m_LDBackend.partialScanRelocation(*relocation, *m_pModule, **rs);
35322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      } // for all relocations
35422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    } // for all relocation section
35522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  } // for all inputs
35622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
35722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
35822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3596f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines/// initStubs - initialize stub-related stuff.
3606f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hinesbool ObjectLinker::initStubs()
3616f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines{
3626f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  // initialize BranchIslandFactory
3636f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.initBRIslandFactory();
3646f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
3656f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  // initialize StubFactory
3666f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.initStubFactory();
3676f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
3686f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  // initialize target stubs
3696f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.initTargetStubs();
3706f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  return true;
3716f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines}
3726f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
3736f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines/// allocateCommonSymobols - allocate fragments for common symbols to the
3746f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines/// corresponding sections
3756f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hinesbool ObjectLinker::allocateCommonSymbols()
3766f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines{
3776f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  if (LinkerConfig::Object != m_Config.codeGenType() ||
3786f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines      m_Config.options().isDefineCommon())
3796f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    return m_LDBackend.allocateCommonSymbols(*m_pModule);
3806f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  return true;
3816f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines}
3826f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
38322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// prelayout - help backend to do some modification before layout
38422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::prelayout()
38522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
38622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // finalize the section symbols, set their fragment reference and push them
38722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // into output symbol table
3886f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  Module::iterator sect, sEnd = m_pModule->end();
3896f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  for (sect = m_pModule->begin(); sect != sEnd; ++sect) {
3906f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    m_pModule->getSectionSymbolSet().finalize(**sect, m_pModule->getSymbolTable());
39122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  }
39222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3936f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.preLayout(*m_pModule, *m_pBuilder);
39422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
39522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// check program interpreter - computer the name size of the runtime dyld
3966f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  if (!m_Config.isCodeStatic() &&
39722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao      (LinkerConfig::Exec == m_Config.codeGenType() ||
39822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao       m_Config.options().isPIE() ||
39922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao       m_Config.options().hasDyld()))
40022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    m_LDBackend.sizeInterp();
40122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
40222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// measure NamePools - compute the size of name pool sections
40322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// In ELF, will compute  the size of.symtab, .strtab, .dynsym, .dynstr,
40422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// .hash and .shstrtab sections.
40522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
40622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// dump all symbols and strings from FragmentLinker and build the format-dependent
40722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// hash table.
4086f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.sizeNamePools(*m_pModule, m_Config.isCodeStatic());
40922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
41022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
41122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
41222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
41322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// layout - linearly layout all output sections and reserve some space
41422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// for GOT/PLT
41522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///   Because we do not support instruction relaxing in this early version,
41622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///   if there is a branch can not jump to its target, we return false
41722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///   directly
41822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::layout()
41922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
4206f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.layout(*m_pModule);
421d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  return true;
42222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
42322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
42422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// prelayout - help backend to do some modification after layout
42522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::postlayout()
42622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
4276f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.postLayout(*m_pModule, *m_pBuilder);
42822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
42922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
43022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
43122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// finalizeSymbolValue - finalize the resolved symbol value.
43222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///   Before relocate(), after layout(), FragmentLinker should correct value of all
43322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao///   symbol.
43422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::finalizeSymbolValue()
43522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
4366f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  return (m_pLinker->finalizeSymbols() && m_LDBackend.finalizeSymbols());
43722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
43822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
43922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// relocate - applying relocation entries and create relocation
44022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// section in the output files
44122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// Create relocation section, asking TargetLDBackend to
44222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// read the relocation information into RelocationEntry
44322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// and push_back into the relocation section
44422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::relocation()
44522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
44622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return m_pLinker->applyRelocations();
44722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
44822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
44922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// emitOutput - emit the output file.
45022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::emitOutput(MemoryArea& pOutput)
45122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
4526f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  return llvm::errc::success == getWriter()->writeObject(*m_pModule, pOutput);
45322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
45422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
45522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// postProcessing - do modification after all processes
45622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool ObjectLinker::postProcessing(MemoryArea& pOutput)
45722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
45822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  m_pLinker->syncRelocationResult(pOutput);
45922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
46022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // emit .eh_frame_hdr
46122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // eh_frame_hdr should be emitted after syncRelocation, because eh_frame_hdr
46222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // needs FDE PC value, which will be corrected at syncRelocation
4636f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  m_LDBackend.postProcessing(pOutput);
46422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
46522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
46622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
467