122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===- IRBuilder.h --------------------------------------------------------===//
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//
1022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao// IRBuilder is a class used as a convenient way to create MCLinker sections
1122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao// with a consistent and simplified interface.
1222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//
1322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===----------------------------------------------------------------------===//
1422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#ifndef MCLD_IRBUILDER_H
1522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#define MCLD_IRBUILDER_H
1622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
17f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines#include <mcld/MC/Input.h>
1822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/MC/InputBuilder.h>
1922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/LDSection.h>
2122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/EhFrame.h>
22d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <mcld/LD/LDSymbol.h>
2322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Fragment/Fragment.h>
2522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Fragment/Relocation.h>
2622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Fragment/RegionFragment.h>
2722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Fragment/FillFragment.h>
286f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines#include <mcld/Fragment/FragmentRef.h>
2922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Support/Path.h>
3122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Support/FileHandle.h>
3222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaonamespace mcld {
3422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass Module;
3622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass LinkerConfig;
3722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass InputTree;
3822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/** \class IRBuilder
4022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  \brief IRBuilder provides an uniform API for creating sections and
4122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  inserting them into a input file.
4222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *
4322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  Ahead-of-time virtual machines (VM) usually compiles an intermediate
4422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  language into a system-dependent binary.  IRBuilder helps such kind of VMs
4522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  to emit binaries in native object format, such as ELF or MachO.
4622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao */
4722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass IRBuilder
4822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
4922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaopublic:
5022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  enum ObjectFormat {
5122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    ELF,
5222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    MachO,
5322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    COFF
5422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  };
5522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
566f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  enum SymbolDefinePolicy {
576f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    Force,
586f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    AsReferred
596f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  };
606f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
616f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  enum SymbolResolvePolicy {
626f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    Unresolve,
636f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines    Resolve
646f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  };
656f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
6622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaopublic:
6722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  IRBuilder(Module& pModule, const LinkerConfig& pConfig);
6822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
6922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ~IRBuilder();
7022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const InputBuilder& getInputBuilder() const { return m_InputBuilder; }
7222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputBuilder&       getInputBuilder()       { return m_InputBuilder; }
73f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  const Module& getModule() const { return m_Module; }
74f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  Module&       getModule()       { return m_Module; }
7522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// @}
7722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// @name Input Files On The Command Line
7822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// @{
7922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
8022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateInput - To create an input file and append it to the input tree.
8122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is like to add an input file in the command line.
8222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
8322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// There are four types of the input files:
8422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///   - relocatable objects,
8522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///   - shared objects,
8622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///   - archives,
8722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///   - and user-defined objects.
8822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
8922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// If Input::Unknown type is given, MCLinker will automatically
9022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// open and read the input file, and create sections of the input. Otherwise,
9122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// users need to manually create sections by IRBuilder.
9222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
9322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @see mcld::Input
9422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
9522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pName [in] The name of the input file.
9622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pPath [in] The path of the input file.
9722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pType [in] The type of the input file. MCLinker will parse the
9822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                   input file to create sections only if pType is
9922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                   Input::Unknown.
10022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return the created mcld::Input.
10122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* CreateInput(const std::string& pName,
10222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                     const sys::fs::Path& pPath,
10322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                     Input::Type pType);
10422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
10522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// ReadInput - To read an input file and append it to the input tree.
10622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is like to add an input file in the command line.
10722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
10822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This funciton is equal to call
10922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///   @ref IRBuilder::CreateInput(pName, pPath, Input::Unknown);
11022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
11122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker will automatically open and read the input file, and create
11222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// sections of the input.
11322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
11422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @see mcld::Input
11522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
11622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pName [in] The name of the input file.
11722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pPath [in] The path of the input file.
11822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return the created mcld::Input.
11922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* ReadInput(const std::string& pName, const sys::fs::Path& pPath);
12022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
12122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// ReadInput - To read an input file and append it to the input tree.
12222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
12322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to -l option. This function tells MCLinker to
12422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// search for lib[pNameSpec].so or lib[pNameSpec].a in the search path.
12522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
12622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pNameSpec [in] The namespec of the input file.
12722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return the created mcld::Input.
12822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* ReadInput(const std::string& pNameSpec);
12922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
13022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// ReadInput - To read an input file and append it to the input tree.
13122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// Another way to open file manually. Use MCLinker's mcld::FileHandle.
13222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* ReadInput(FileHandle& pFileHandle);
13322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
13422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// ReadInput - To read an input file and append it to the input tree.
13522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
13622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is like to add an input in the command line.
13722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
13822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function tells MCLinker to read pRawMemory as an image of an object
13922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// file. So far, MCLinekr only supports ELF object format, but it will
14022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// support various object formats in the future. MCLinker relies triple to
14122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// know the object format of pRawMemory.
14222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in] pName      The name of the input file
14322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in] pRawMemory An image of object file
14422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in] pSize      The size of the memory
14522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The created mcld::Input
14622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* ReadInput(const std::string& pName, void* pRawMemory, size_t pSize);
14722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
14822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// StartGroup - Add an opening tag of group.
14922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
15022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --start-group option. This function tells
15122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to create a new archive group and to add the following archives
15222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// in the created group. The archives in a group are searched repeatedly
15322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// until no new undefined references are created.
15422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  bool StartGroup();
15522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
15622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// EndGroup - Add a closing tag of group.
15722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
15822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --end-group option. This function tells
15922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to stop adding following archives in the created group.
16022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  bool EndGroup();
16122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
16222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// @}
16322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// @name Positional Options On The Command Line
16422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// @{
16522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
16622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// WholeArchive - Append a --whole-archive option on the command line
16722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
16822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --whole-archive option. This function tells
16922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to include every object files in the following archives.
17022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void WholeArchive();
17122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
17222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// NoWholeArchive - Append a --no-whole-archive option on the command line.
17322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
17422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --no-whole-archive option. This function tells
17522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to stop including every object files in the following archives.
17622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// Only used object files in the following archives are included.
17722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void NoWholeArchive();
17822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
17922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AsNeeded - Append a --as-needed option on the command line.
18022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
18122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --as-needed option. This function tells
18222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to not add a DT_NEEDED tag in .dynamic sections for the
18322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// following shared objects that are not really used. MCLinker will add tags
18422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  //  only for the following shared objects which is really used.
18522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void AsNeeded();
18622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
18722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// NoAsNeeded - Append a --no-as-needed option on the command line.
18822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
18922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --no-as-needed option. This function tells
19022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to add a DT_NEEDED tag in .dynamic section for every shared
19122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// objects that is created after this option.
19222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void NoAsNeeded();
19322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
19422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CopyDTNeeded - Append a --add-needed option on the command line.
19522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
19622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --add-needed option. This function tells
19722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// NCLinker to copy all DT_NEEDED tags of every following shared objects
19822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// to the output file.
19922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void CopyDTNeeded();
20022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
20122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// NoCopyDTNeeded - Append a --no-add-needed option on the command line.
20222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
20322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --no-add-needed option. This function tells
20422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to stop copying all DT_NEEDS tags in the following shared
20522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// objects to the output file.
20622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void NoCopyDTNeeded();
20722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
20822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AgainstShared - Append a -Bdynamic option on the command line.
20922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
21022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to -Bdynamic option. This function tells MCLinker
21122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// to search shared objects before archives for the following namespec.
21222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void AgainstShared();
21322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
21422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AgainstStatic - Append a -static option on the command line.
21522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
21622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to -static option. This function tells MCLinker to
21722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// search archives before shared objects for the following namespec.
21822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void AgainstStatic();
21922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
22022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// @}
22122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// @name Input Methods
22222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/// @{
22322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
22422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateELFHeader - To create and append a section header in the input file
22522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
22622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param OF     [in]      The file format. @see ObjectFormat
22722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pInput [in, out] The input file.
22822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pName  [in]      The name of the section.
22922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pType  [in]      The meaning of the content in the section. The
23022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                         value is format-dependent. In ELF, the value is
23122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                         SHT_* in normal.
23222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pFlag  [in]      The format-dependent flag. In ELF, the value is
23322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                         SHF_* in normal.
23422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pAlign [in]      The alignment constraint of the section
23522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The created section header.
23622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static LDSection* CreateELFHeader(Input& pInput,
23722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                    const std::string& pName,
23822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                    uint32_t pType,
23922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                    uint32_t pFlag,
24022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                    uint32_t pAlign);
24122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
24222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateSectionData - To create a section data for given pSection.
24322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pSection The given LDSection. It can be in either an
24422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         input or the output.
24522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         pSection.getSectionData() is set to a valid section data.
24622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The created section data. If the pSection already has section
24722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         data, or if the pSection's type should not have a section data
24822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         (.eh_frame or relocation data), then an assertion occurs.
24922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static SectionData* CreateSectionData(LDSection& pSection);
25022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
25122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateRelocData - To create a relocation data for given pSection.
25222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pSection The given LDSection. It can be in either an
25322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         input or the output.
25422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         pSection.getRelocData() is set to a valid relocation data.
25522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The created relocation data. If the pSection already has
25622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         relocation data, or if the pSection's type is not
25722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         LDFileFormat::Relocation, then an assertion occurs.
25822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static RelocData* CreateRelocData(LDSection &pSection);
25922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
26022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateEhFrame - To create a eh_frame for given pSection
26122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pSection The given LDSection. It can be in either an
26222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         input or the output.
26322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         pSection.getEhFrame() is set to a valid eh_frame.
26422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The created eh_frame. If the pSection already has eh_frame data,
26522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         or if the pSection's type is not LDFileFormat::EhFrame, then an
26622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         assertion occurs.
26722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static EhFrame* CreateEhFrame(LDSection& pSection);
26822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
26922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateBSS - To create a bss section for given pSection
27022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pSection The given LDSection. It can be in either an
27122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         input or the output.
27222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         pSection.getSectionData() is set to a valid section data and
27322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         contains a fillment fragment whose size is pSection.size().
27422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The create section data. It the pSection already has a section
27522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         data, or if the pSection's type is not LDFileFormat::BSS, then
27622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         an assertion occurs.
27722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static SectionData* CreateBSS(LDSection& pSection);
27822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
27922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateRegion - To create a region fragment in the input file.
28022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function tells MCLinker to read a piece of data from the input
28122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// file, and to create a region fragment that carries the data. The data
28222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// will be deallocated automatically when pInput is destroyed.
28322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
28422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pInput  [in, out] The input file.
28522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pOffset [in]      The starting file offset of the data
28622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pLength [in]      The number of bytes of the data
28722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return If pLength is zero or failing to request a region, return a
28822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         FillFragment.
28922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static Fragment* CreateRegion(Input& pInput, size_t pOffset, size_t pLength);
29022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
29122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateRegion - To create a region fragment wrapping the given memory.
29222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function tells MCLinker to create a region fragment by the data
29322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// directly. Since the data is given from outside, not read from the input
29422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// file, users should deallocated the data manually.
29522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
29622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pMemory [in] The start address of the given data
29722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pLength [in] The number of bytes of the data
29822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return If pLength is zero or failing to request a region, return a
29922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         FillFragment.
30022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static Fragment* CreateRegion(void* pMemory, size_t pLength);
30122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
30222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AppendFragment - To append pFrag to the given SectionData pSD.
30322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function tells MCLinker to append a fragment to section data, and
30422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// update size of the section header.
30522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
30622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note In order to keep the alignment of pFrag, This function inserts an
30722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AlignFragment before pFrag if the section header's alignment is larger
30822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// than 1.
30922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note This function does not update offset of section headers.
31022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
31122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pFrag [in, out] The appended fragment. Its offset is set as the
31222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                        section offset in pSD.
31322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pSD   [in, out] The section data. Size of the header is also
31422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                        updated.
31522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return Total size of the inserted fragments.
31622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static uint64_t AppendFragment(Fragment& pFrag, SectionData& pSD);
31722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
31822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AppendRelocation - To append a relocation to a relocation data.
31922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function tells MCLinker to add a general relocation to the
32022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// relocation data. This function does not update offset and size of section
32122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// headers.
32222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
32322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pReloc [in]      The appended relocation.
32422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pRD    [in, out] The relocation data being appended.
32522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static void AppendRelocation(Relocation& pRelocation, RelocData& pRD);
32622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
32722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AppendEhFrame - To append a fragment to a EhFrame.
32822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note In order to keep the alignment of pFrag, This function inserts an
32922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AlignFragment before pFrag if the section header's alignment is larger
33022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// than 1.
33122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note This function also update size of the section header, but does not
33222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// update header's offset.
33322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
33422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pFrag    [in, out] The appended fragment.
33522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pEhFrame [in, out] The EhFrame.
33622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return Total size of the inserted fragments.
33722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static uint64_t AppendEhFrame(Fragment& pFrag, EhFrame& pEhFrame);
33822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
33922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AppendEhFrame - To append a FDE to the given EhFrame pEhFram.
34022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note In order to keep the alignment of pFrag, This function inserts an
34122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AlignFragment before pFrag if the section header's alignment is larger
34222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// than 1.
34322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note This function also update size of the section header, but does not
34422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// update header's offset.
34522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
34622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pFDE The appended FDE entry.
34722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pEhFrame The eh_frame being appended.
34822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return Total size of the inserted fragments.
34922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static uint64_t AppendEhFrame(EhFrame::FDE& pFDE, EhFrame& pEhFrame);
35022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
35122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AppendEhFrame - To append a CIE to the given EhFrame pEhFram.
35222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note In order to keep the alignment of pFrag, This function inserts an
35322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AlignFragment before pFrag if the section header's alignment is larger
35422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// than 1.
35522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note This function also update size of the section header, but does not
35622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// update header's offset.
35722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
35822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pCIE The appended CIE entry.
35922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pEhFrame The eh_frame being appended.
36022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return Total size of the inserted fragments.
36122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static uint64_t AppendEhFrame(EhFrame::CIE& pCIE, EhFrame& pEhFrame);
36222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3636f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// AddSymbol - To add a symbol to the input file.
3646f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// This function create a new symbol and insert it into the input file. If
3656f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// mcld::Module has another symbol with the same name, then this function
3666f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// resolves these two symbols and keeps one in mcld::Module by their
3676f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// attributes.
368d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///
369d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// This is a general method for all kinds of symbol.
370d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///
371d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in, out] pInput   The input file. Either a relocatable or dynamic
372d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           object
373d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pName    The name of the symbol
3746f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @param [in]      pType    What the symbol refers to. May be a object,
3756f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///                           function, no-type and so on. @see ResolveInfo
376d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pDesc    { Undefined, Define, Common, Indirect }
377d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pBind    { Global, Weak, Local, Absolute }
378d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pSize    The size of the symbol. Bigger common symbols
379d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           overrides the smaller common symbols.
380d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pValue   Common symbols' value are alignment constraints
381d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           Undefined symbols don't have value.
382d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           The rest symbols' value are relative section
383d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           offset.
384d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pSection Absolute, undefined, common symbols do not have
385d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           pSection. Keep their pSection be NULL.
386d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @oaram [in]      pVis     The visibility of the symbol
3876f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
3886f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @return The added symbol. If the insertion fails due to the resoluction,
3896f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// return NULL.
390d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  LDSymbol* AddSymbol(Input& pInput,
391d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      const std::string& pName,
392d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      ResolveInfo::Type pType,
393d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      ResolveInfo::Desc pDesc,
394d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      ResolveInfo::Binding pBind,
395d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      ResolveInfo::SizeType pSize,
396d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      LDSymbol::ValueType pValue = 0x0,
397d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      LDSection* pSection = NULL,
398d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      ResolveInfo::Visibility pVis = ResolveInfo::Default);
399d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
4006f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// AddSymbol - To add a symbol in mcld::Module
4016f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// This function create a new symbol and insert it into mcld::Module.
4026f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4036f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @tparam POLICY idicate the condition to define or not to define the
4046f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// symbol.
4056f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///   - AsRefered
4066f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///     - Define a symbol only if mcld::Module contains a symbol with
4076f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///       identical name. If mcld::Module does not have any symbol with
4086f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///       the same name, this function returns NULL.
4096f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4106f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///   - Force
4116f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///     - Define a symbol no matter mcld::Module has a symbol with identical
4126f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///       name or not.
4136f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4146f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @tparam RESOLVE indicate the method to define a symbol. If we must define
4156f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// a symbol in mcld::Module, then how to define it.
4166f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4176f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///   - Resolve
4186f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///      - Follow the symbol resolution rule to bind the symbol references.
4196f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///        Resolution of the symbols with idential name depends on their
4206f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///        attributes.
4216f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4226f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///   - Unresolve
4236f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///      - Forcefully override the symbol in mcld::Module.  With this
4246f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///        argument, AddSymbol function turns a blind eye to symbol
4256f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///        resolution rules.
4266f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4276f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @param [in] pName    The name of the symbol
4286f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @param [in] pType    The type of the symbol
4296f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @param [in] pDesc    The description of the symbol, Could be one of
4306f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///                      { Undefined, Define, Common, Indirect }
4316f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @param [in] pBinding The binding of the symbol. Could be one of
4326f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///                      { Global, Weak, Local, Absolute }
4336f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4346f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @return The symbol kept in mcld::Module.
4356f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  template<SymbolDefinePolicy POLICY, SymbolResolvePolicy RESOLVE>
4366f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  LDSymbol* AddSymbol(const llvm::StringRef& pName,
4376f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                      ResolveInfo::Type pType,
4386f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                      ResolveInfo::Desc pDesc,
4396f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                      ResolveInfo::Binding pBinding,
4406f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                      ResolveInfo::SizeType pSize = 0,
4416f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                      LDSymbol::ValueType pValue = 0x0,
4426f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                      FragmentRef* pFragmentRef = FragmentRef::Null(),
4436f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                      ResolveInfo::Visibility pVisibility = ResolveInfo::Default);
4446f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
445d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// AddRelocation - To add a relocation entry
446d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///
447d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in] pSection The relocation section. pSection's link should point to
448d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                      the target section.
449d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in] pType    The type of the relocation (target dependent)
450d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in] pSym     The symbol should be the symbol in the input file.
451d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in] pOffset  The offset of target section.
452d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in] pAddend  Tthe addend value for applying relocation
453d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  static Relocation* AddRelocation(LDSection& pSection,
454d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                   Relocation::Type pType,
455d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                   LDSymbol& pSym,
456d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                   uint32_t pOffset,
457d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                   Relocation::Address pAddend = 0);
458d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
459f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  /// shouldForceLocal - The helper function for AddSymbol to check if the
460f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  /// symbols should be force to local symbols
461f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  bool shouldForceLocal(const ResolveInfo& pInfo, const LinkerConfig& pConfig);
462f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines
463d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaoprivate:
464d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  LDSymbol* addSymbolFromObject(const std::string& pName,
465d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Type pType,
466d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Desc pDesc,
467d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Binding pBinding,
468d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::SizeType pSize,
469d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                LDSymbol::ValueType pValue,
470d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                FragmentRef* pFragmentRef,
471d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Visibility pVisibility);
472d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
4736f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  LDSymbol* addSymbolFromDynObj(Input& pInput,
4746f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                                const std::string& pName,
475d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Type pType,
476d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Desc pDesc,
477d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Binding pBinding,
478d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::SizeType pSize,
479d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                LDSymbol::ValueType pValue,
480d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Visibility pVisibility);
481d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
48222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoprivate:
48322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Module& m_Module;
48422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const LinkerConfig& m_Config;
48522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
48622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputBuilder m_InputBuilder;
48722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao};
48822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
4896f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hinestemplate<> LDSymbol*
4906f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen HinesIRBuilder::AddSymbol<IRBuilder::Force, IRBuilder::Unresolve>(
4916f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         const llvm::StringRef& pName,
4926f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Type pType,
4936f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Desc pDesc,
4946f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Binding pBinding,
4956f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::SizeType pSize,
4966f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         LDSymbol::ValueType pValue,
4976f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         FragmentRef* pFragmentRef,
4986f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Visibility pVisibility);
4996f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
5006f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hinestemplate<> LDSymbol*
5016f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen HinesIRBuilder::AddSymbol<IRBuilder::AsReferred, IRBuilder::Unresolve>(
5026f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         const llvm::StringRef& pName,
5036f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Type pType,
5046f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Desc pDesc,
5056f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Binding pBinding,
5066f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::SizeType pSize,
5076f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         LDSymbol::ValueType pValue,
5086f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         FragmentRef* pFragmentRef,
5096f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Visibility pVisibility);
5106f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
5116f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hinestemplate<> LDSymbol*
5126f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen HinesIRBuilder::AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
5136f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         const llvm::StringRef& pName,
5146f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Type pType,
5156f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Desc pDesc,
5166f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Binding pBinding,
5176f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::SizeType pSize,
5186f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         LDSymbol::ValueType pValue,
5196f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         FragmentRef* pFragmentRef,
5206f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Visibility pVisibility);
5216f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
5226f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hinestemplate<> LDSymbol*
5236f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen HinesIRBuilder::AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
5246f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         const llvm::StringRef& pName,
5256f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Type pType,
5266f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Desc pDesc,
5276f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Binding pBinding,
5286f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::SizeType pSize,
5296f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         LDSymbol::ValueType pValue,
5306f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         FragmentRef* pFragmentRef,
5316f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                         ResolveInfo::Visibility pVisibility);
5326f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
53322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao} // end of namespace mcld
53422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
53522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#endif
536