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//===----------------------------------------------------------------------===//
1437b74a387bb3993387029859c2d9d051c41c724eStephen Hines#ifndef MCLD_IRBUILDER_H_
1537b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_IRBUILDER_H_
1637b74a387bb3993387029859c2d9d051c41c724eStephen Hines
1737b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Fragment/FillFragment.h"
1837b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Fragment/Fragment.h"
1937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Fragment/FragmentRef.h"
2037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Fragment/RegionFragment.h"
2137b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Fragment/Relocation.h"
2237b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/LD/EhFrame.h"
2337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/LD/LDSection.h"
2437b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/LD/LDSymbol.h"
2537b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/MC/Input.h"
2637b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/MC/InputBuilder.h"
2737b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/FileHandle.h"
2837b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/Path.h"
2922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaonamespace mcld {
3122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass InputTree;
3337b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass LinkerConfig;
3437b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass Module;
3522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/** \class IRBuilder
3722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  \brief IRBuilder provides an uniform API for creating sections and
3822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  inserting them into a input file.
3922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *
4022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  Ahead-of-time virtual machines (VM) usually compiles an intermediate
4122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  language into a system-dependent binary.  IRBuilder helps such kind of VMs
4222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  to emit binaries in native object format, such as ELF or MachO.
4322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao */
4437b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass IRBuilder {
4537b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
4637b74a387bb3993387029859c2d9d051c41c724eStephen Hines  enum ObjectFormat { ELF, MachO, COFF };
4737b74a387bb3993387029859c2d9d051c41c724eStephen Hines
4837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  enum SymbolDefinePolicy { Force, AsReferred };
4937b74a387bb3993387029859c2d9d051c41c724eStephen Hines
5037b74a387bb3993387029859c2d9d051c41c724eStephen Hines  enum SymbolResolvePolicy { Unresolve, Resolve };
5137b74a387bb3993387029859c2d9d051c41c724eStephen Hines
5237b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
5322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  IRBuilder(Module& pModule, const LinkerConfig& pConfig);
5422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
5522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ~IRBuilder();
5622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
5722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const InputBuilder& getInputBuilder() const { return m_InputBuilder; }
5837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  InputBuilder& getInputBuilder() { return m_InputBuilder; }
59f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  const Module& getModule() const { return m_Module; }
6037b74a387bb3993387029859c2d9d051c41c724eStephen Hines  Module& getModule() { return m_Module; }
6122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
6237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @}
6337b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @name Input Files On The Command Line
6437b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @{
6522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
6622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateInput - To create an input file and append it to the input tree.
6722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is like to add an input file in the command line.
6822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
6922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// There are four types of the input files:
7022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///   - relocatable objects,
7122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///   - shared objects,
7222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///   - archives,
7322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///   - and user-defined objects.
7422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
7522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// If Input::Unknown type is given, MCLinker will automatically
7622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// open and read the input file, and create sections of the input. Otherwise,
7722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// users need to manually create sections by IRBuilder.
7822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
7922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @see mcld::Input
8022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
8122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pName [in] The name of the input file.
8222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pPath [in] The path of the input file.
8322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pType [in] The type of the input file. MCLinker will parse the
8422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                   input file to create sections only if pType is
8522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                   Input::Unknown.
8622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return the created mcld::Input.
8722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* CreateInput(const std::string& pName,
8822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                     const sys::fs::Path& pPath,
8922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                     Input::Type pType);
9022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
9122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// ReadInput - To read an input file and append it to the input tree.
9222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is like to add an input file in the command line.
9322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
9422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This funciton is equal to call
9522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///   @ref IRBuilder::CreateInput(pName, pPath, Input::Unknown);
9622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
9722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker will automatically open and read the input file, and create
9822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// sections of the input.
9922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
10022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @see mcld::Input
10122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
10222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pName [in] The name of the input file.
10322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pPath [in] The path of the input file.
10422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return the created mcld::Input.
10522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* ReadInput(const std::string& pName, const sys::fs::Path& pPath);
10622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
10722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// ReadInput - To read an input file and append it to the input tree.
10822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
10922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to -l option. This function tells MCLinker to
11022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// search for lib[pNameSpec].so or lib[pNameSpec].a in the search path.
11122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
11222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pNameSpec [in] The namespec of the input file.
11322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return the created mcld::Input.
11422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* ReadInput(const std::string& pNameSpec);
11522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
11622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// ReadInput - To read an input file and append it to the input tree.
11722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// Another way to open file manually. Use MCLinker's mcld::FileHandle.
11822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* ReadInput(FileHandle& pFileHandle);
11922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
12022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// ReadInput - To read an input file and append it to the input tree.
12122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
12222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is like to add an input in the command line.
12322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
12422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function tells MCLinker to read pRawMemory as an image of an object
12522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// file. So far, MCLinekr only supports ELF object format, but it will
12622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// support various object formats in the future. MCLinker relies triple to
12722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// know the object format of pRawMemory.
12822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in] pName      The name of the input file
12922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in] pRawMemory An image of object file
13022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in] pSize      The size of the memory
13122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The created mcld::Input
13222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* ReadInput(const std::string& pName, void* pRawMemory, size_t pSize);
13322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
13422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// StartGroup - Add an opening tag of group.
13522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
13622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --start-group option. This function tells
13722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to create a new archive group and to add the following archives
13822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// in the created group. The archives in a group are searched repeatedly
13922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// until no new undefined references are created.
14022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  bool StartGroup();
14122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
14222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// EndGroup - Add a closing tag of group.
14322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
14422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --end-group option. This function tells
14522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to stop adding following archives in the created group.
14622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  bool EndGroup();
14722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
14837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @}
14937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @name Positional Options On The Command Line
15037b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @{
15122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
15222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// WholeArchive - Append a --whole-archive option on the command line
15322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
15422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --whole-archive option. This function tells
15522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to include every object files in the following archives.
15622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void WholeArchive();
15722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
15822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// NoWholeArchive - Append a --no-whole-archive option on the command line.
15922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
16022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --no-whole-archive option. This function tells
16122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to stop including every object files in the following archives.
16222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// Only used object files in the following archives are included.
16322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void NoWholeArchive();
16422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
16522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AsNeeded - Append a --as-needed option on the command line.
16622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
16722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --as-needed option. This function tells
16822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to not add a DT_NEEDED tag in .dynamic sections for the
16922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// following shared objects that are not really used. MCLinker will add tags
17022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  //  only for the following shared objects which is really used.
17122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void AsNeeded();
17222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
17322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// NoAsNeeded - Append a --no-as-needed option on the command line.
17422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
17522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --no-as-needed option. This function tells
17622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to add a DT_NEEDED tag in .dynamic section for every shared
17722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// objects that is created after this option.
17822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void NoAsNeeded();
17922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
18022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CopyDTNeeded - Append a --add-needed option on the command line.
18122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
18222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --add-needed option. This function tells
18322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// NCLinker to copy all DT_NEEDED tags of every following shared objects
18422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// to the output file.
18522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void CopyDTNeeded();
18622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
18722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// NoCopyDTNeeded - Append a --no-add-needed option on the command line.
18822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
18922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to --no-add-needed option. This function tells
19022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// MCLinker to stop copying all DT_NEEDS tags in the following shared
19122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// objects to the output file.
19222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void NoCopyDTNeeded();
19322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
19422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AgainstShared - Append a -Bdynamic option on the command line.
19522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
19622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to -Bdynamic option. This function tells MCLinker
19722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// to search shared objects before archives for the following namespec.
19822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void AgainstShared();
19922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
20022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AgainstStatic - Append a -static option on the command line.
20122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
20222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function is equal to -static option. This function tells MCLinker to
20322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// search archives before shared objects for the following namespec.
20422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void AgainstStatic();
20522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
20637b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @}
20737b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @name Input Methods
20837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @{
20922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
21022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateELFHeader - To create and append a section header in the input file
21122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
21222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param OF     [in]      The file format. @see ObjectFormat
21322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pInput [in, out] The input file.
21422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pName  [in]      The name of the section.
21522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pType  [in]      The meaning of the content in the section. The
21622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                         value is format-dependent. In ELF, the value is
21722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                         SHT_* in normal.
21822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pFlag  [in]      The format-dependent flag. In ELF, the value is
21922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                         SHF_* in normal.
22022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pAlign [in]      The alignment constraint of the section
22122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The created section header.
22222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static LDSection* CreateELFHeader(Input& pInput,
22322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                    const std::string& pName,
22422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                    uint32_t pType,
22522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                    uint32_t pFlag,
22622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                                    uint32_t pAlign);
22722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
22822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateSectionData - To create a section data for given pSection.
22922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pSection The given LDSection. It can be in either an
23022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         input or the output.
23122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         pSection.getSectionData() is set to a valid section data.
23222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The created section data. If the pSection already has section
23322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         data, or if the pSection's type should not have a section data
23422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         (.eh_frame or relocation data), then an assertion occurs.
23522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static SectionData* CreateSectionData(LDSection& pSection);
23622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
23722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateRelocData - To create a relocation data for given pSection.
23822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pSection The given LDSection. It can be in either an
23922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         input or the output.
24022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         pSection.getRelocData() is set to a valid relocation data.
24122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The created relocation data. If the pSection already has
24222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         relocation data, or if the pSection's type is not
24322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         LDFileFormat::Relocation, then an assertion occurs.
24437b74a387bb3993387029859c2d9d051c41c724eStephen Hines  static RelocData* CreateRelocData(LDSection& pSection);
24522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
24622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateEhFrame - To create a eh_frame for given pSection
24722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pSection The given LDSection. It can be in either an
24822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         input or the output.
24922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         pSection.getEhFrame() is set to a valid eh_frame.
25022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The created eh_frame. If the pSection already has eh_frame data,
25122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         or if the pSection's type is not LDFileFormat::EhFrame, then an
25222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         assertion occurs.
25322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static EhFrame* CreateEhFrame(LDSection& pSection);
25422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
25537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// CreateDebugString - To create a debug_str for given pSection
25637b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @param  pSection The given LDSection. It should be the output
25737b74a387bb3993387029859c2d9d051c41c724eStephen Hines  ///         .debug_str section
25837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  ///         pSection.getDebugString() is set to a valid eh_frame.
25937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @return The created DebugString
26037b74a387bb3993387029859c2d9d051c41c724eStephen Hines  static DebugString* CreateDebugString(LDSection& pSection);
26137b74a387bb3993387029859c2d9d051c41c724eStephen Hines
26222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateBSS - To create a bss section for given pSection
26322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pSection The given LDSection. It can be in either an
26422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         input or the output.
26522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         pSection.getSectionData() is set to a valid section data and
26622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         contains a fillment fragment whose size is pSection.size().
26722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return The create section data. It the pSection already has a section
26822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         data, or if the pSection's type is not LDFileFormat::BSS, then
26922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         an assertion occurs.
27022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static SectionData* CreateBSS(LDSection& pSection);
27122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
27222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateRegion - To create a region fragment in the input file.
27322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function tells MCLinker to read a piece of data from the input
27422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// file, and to create a region fragment that carries the data. The data
27522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// will be deallocated automatically when pInput is destroyed.
27622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
27722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pInput  [in, out] The input file.
27822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pOffset [in]      The starting file offset of the data
27922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pLength [in]      The number of bytes of the data
28022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return If pLength is zero or failing to request a region, return a
28122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         FillFragment.
28222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static Fragment* CreateRegion(Input& pInput, size_t pOffset, size_t pLength);
28322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
28422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// CreateRegion - To create a region fragment wrapping the given memory.
28522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function tells MCLinker to create a region fragment by the data
28622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// directly. Since the data is given from outside, not read from the input
28722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// file, users should deallocated the data manually.
28822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
28922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pMemory [in] The start address of the given data
29022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pLength [in] The number of bytes of the data
29122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return If pLength is zero or failing to request a region, return a
29222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///         FillFragment.
29322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static Fragment* CreateRegion(void* pMemory, size_t pLength);
29422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
29522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AppendFragment - To append pFrag to the given SectionData pSD.
29622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function tells MCLinker to append a fragment to section data, and
29722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// update size of the section header.
29822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
29922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note In order to keep the alignment of pFrag, This function inserts an
30022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AlignFragment before pFrag if the section header's alignment is larger
30122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// than 1.
30222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note This function does not update offset of section headers.
30322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
30422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pFrag [in, out] The appended fragment. Its offset is set as the
30522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                        section offset in pSD.
30622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pSD   [in, out] The section data. Size of the header is also
30722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///                        updated.
30822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return Total size of the inserted fragments.
30922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static uint64_t AppendFragment(Fragment& pFrag, SectionData& pSD);
31022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
31122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AppendRelocation - To append a relocation to a relocation data.
31222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function tells MCLinker to add a general relocation to the
31322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// relocation data. This function does not update offset and size of section
31422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// headers.
31522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
31622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pReloc [in]      The appended relocation.
31722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pRD    [in, out] The relocation data being appended.
31822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static void AppendRelocation(Relocation& pRelocation, RelocData& pRD);
31922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
32022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AppendEhFrame - To append a fragment to a EhFrame.
32122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note In order to keep the alignment of pFrag, This function inserts an
32222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AlignFragment before pFrag if the section header's alignment is larger
32322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// than 1.
32422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note This function also update size of the section header, but does not
32522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// update header's offset.
32622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
32722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pFrag    [in, out] The appended fragment.
32822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pEhFrame [in, out] The EhFrame.
32922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return Total size of the inserted fragments.
33022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static uint64_t AppendEhFrame(Fragment& pFrag, EhFrame& pEhFrame);
33122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
33222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AppendEhFrame - To append a FDE to the given EhFrame pEhFram.
33322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note In order to keep the alignment of pFrag, This function inserts an
33422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AlignFragment before pFrag if the section header's alignment is larger
33522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// than 1.
33622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note This function also update size of the section header, but does not
33722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// update header's offset.
33822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
33922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pFDE The appended FDE entry.
34022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pEhFrame The eh_frame being appended.
34122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return Total size of the inserted fragments.
34222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static uint64_t AppendEhFrame(EhFrame::FDE& pFDE, EhFrame& pEhFrame);
34322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
34422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AppendEhFrame - To append a CIE to the given EhFrame pEhFram.
34522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note In order to keep the alignment of pFrag, This function inserts an
34622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// AlignFragment before pFrag if the section header's alignment is larger
34722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// than 1.
34822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @note This function also update size of the section header, but does not
34922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// update header's offset.
35022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
35122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pCIE The appended CIE entry.
35222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param [in, out] pEhFrame The eh_frame being appended.
35322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @return Total size of the inserted fragments.
35422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static uint64_t AppendEhFrame(EhFrame::CIE& pCIE, EhFrame& pEhFrame);
35522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
356b0d0eb206527b43c771933602e147bbd7b471082Stephen Hines  /// CreateLocalSymbol - Create a local symbol at the given FragmentRef.
357b0d0eb206527b43c771933602e147bbd7b471082Stephen Hines  ResolveInfo* CreateLocalSymbol(FragmentRef& pFragRef);
358b0d0eb206527b43c771933602e147bbd7b471082Stephen Hines
3596f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// AddSymbol - To add a symbol to the input file.
3606f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// This function create a new symbol and insert it into the input file. If
3616f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// mcld::Module has another symbol with the same name, then this function
3626f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// resolves these two symbols and keeps one in mcld::Module by their
3636f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// attributes.
364d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///
365d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// This is a general method for all kinds of symbol.
366d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///
367d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in, out] pInput   The input file. Either a relocatable or dynamic
368d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           object
369d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pName    The name of the symbol
3706f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @param [in]      pType    What the symbol refers to. May be a object,
3716f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///                           function, no-type and so on. @see ResolveInfo
372d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pDesc    { Undefined, Define, Common, Indirect }
373d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pBind    { Global, Weak, Local, Absolute }
374d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pSize    The size of the symbol. Bigger common symbols
375d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           overrides the smaller common symbols.
376d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pValue   Common symbols' value are alignment constraints
377d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           Undefined symbols don't have value.
378d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           The rest symbols' value are relative section
379d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           offset.
380d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in]      pSection Absolute, undefined, common symbols do not have
381d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                           pSection. Keep their pSection be NULL.
382d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @oaram [in]      pVis     The visibility of the symbol
3836f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
3846f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @return The added symbol. If the insertion fails due to the resoluction,
3856f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// return NULL.
386d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  LDSymbol* AddSymbol(Input& pInput,
387d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      const std::string& pName,
388d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      ResolveInfo::Type pType,
389d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      ResolveInfo::Desc pDesc,
390d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      ResolveInfo::Binding pBind,
391d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      ResolveInfo::SizeType pSize,
392d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      LDSymbol::ValueType pValue = 0x0,
393d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      LDSection* pSection = NULL,
394d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                      ResolveInfo::Visibility pVis = ResolveInfo::Default);
395d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
3966f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// AddSymbol - To add a symbol in mcld::Module
3976f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// This function create a new symbol and insert it into mcld::Module.
3986f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
3996f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @tparam POLICY idicate the condition to define or not to define the
4006f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// symbol.
4016f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///   - AsRefered
4026f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///     - Define a symbol only if mcld::Module contains a symbol with
4036f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///       identical name. If mcld::Module does not have any symbol with
4046f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///       the same name, this function returns NULL.
4056f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4066f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///   - Force
4076f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///     - Define a symbol no matter mcld::Module has a symbol with identical
4086f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///       name or not.
4096f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4106f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @tparam RESOLVE indicate the method to define a symbol. If we must define
4116f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// a symbol in mcld::Module, then how to define it.
4126f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4136f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///   - Resolve
4146f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///      - Follow the symbol resolution rule to bind the symbol references.
4156f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///        Resolution of the symbols with idential name depends on their
4166f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///        attributes.
4176f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4186f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///   - Unresolve
4196f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///      - Forcefully override the symbol in mcld::Module.  With this
4206f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///        argument, AddSymbol function turns a blind eye to symbol
4216f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///        resolution rules.
4226f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4236f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @param [in] pName    The name of the symbol
4246f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @param [in] pType    The type of the symbol
4256f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @param [in] pDesc    The description of the symbol, Could be one of
4266f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///                      { Undefined, Define, Common, Indirect }
4276f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @param [in] pBinding The binding of the symbol. Could be one of
4286f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///                      { Global, Weak, Local, Absolute }
4296f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  ///
4306f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// @return The symbol kept in mcld::Module.
43137b74a387bb3993387029859c2d9d051c41c724eStephen Hines  template <SymbolDefinePolicy POLICY, SymbolResolvePolicy RESOLVE>
43237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  LDSymbol* AddSymbol(
43337b74a387bb3993387029859c2d9d051c41c724eStephen Hines      const llvm::StringRef& pName,
43437b74a387bb3993387029859c2d9d051c41c724eStephen Hines      ResolveInfo::Type pType,
43537b74a387bb3993387029859c2d9d051c41c724eStephen Hines      ResolveInfo::Desc pDesc,
43637b74a387bb3993387029859c2d9d051c41c724eStephen Hines      ResolveInfo::Binding pBinding,
43737b74a387bb3993387029859c2d9d051c41c724eStephen Hines      ResolveInfo::SizeType pSize = 0,
43837b74a387bb3993387029859c2d9d051c41c724eStephen Hines      LDSymbol::ValueType pValue = 0x0,
43937b74a387bb3993387029859c2d9d051c41c724eStephen Hines      FragmentRef * pFragmentRef = FragmentRef::Null(),
44037b74a387bb3993387029859c2d9d051c41c724eStephen Hines      ResolveInfo::Visibility pVisibility = ResolveInfo::Default);
4416f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
442d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// AddRelocation - To add a relocation entry
443d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///
44437b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// @param [in] pSection The relocation section. pSection's link should point
44537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  /// to
446d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  ///                      the target section.
447d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in] pType    The type of the relocation (target dependent)
448d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in] pSym     The symbol should be the symbol in the input file.
449d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in] pOffset  The offset of target section.
450d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param [in] pAddend  Tthe addend value for applying relocation
451d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  static Relocation* AddRelocation(LDSection& pSection,
452d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                   Relocation::Type pType,
453d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                   LDSymbol& pSym,
454d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                   uint32_t pOffset,
455d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                   Relocation::Address pAddend = 0);
456d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
457f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  /// shouldForceLocal - The helper function for AddSymbol to check if the
458f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  /// symbols should be force to local symbols
459f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  bool shouldForceLocal(const ResolveInfo& pInfo, const LinkerConfig& pConfig);
460f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines
46137b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
462d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  LDSymbol* addSymbolFromObject(const std::string& pName,
463d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Type pType,
464d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Desc pDesc,
465d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Binding pBinding,
466d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::SizeType pSize,
467d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                LDSymbol::ValueType pValue,
468d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                FragmentRef* pFragmentRef,
469d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Visibility pVisibility);
470d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
4716f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  LDSymbol* addSymbolFromDynObj(Input& pInput,
4726f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines                                const std::string& pName,
473d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Type pType,
474d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Desc pDesc,
475d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Binding pBinding,
476d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::SizeType pSize,
477d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                LDSymbol::ValueType pValue,
478d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                                ResolveInfo::Visibility pVisibility);
479d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
48037b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
48122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Module& m_Module;
48222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const LinkerConfig& m_Config;
48322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
48422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputBuilder m_InputBuilder;
48522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao};
48622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
48737b74a387bb3993387029859c2d9d051c41c724eStephen Hinestemplate <>
48837b74a387bb3993387029859c2d9d051c41c724eStephen HinesLDSymbol* IRBuilder::AddSymbol<IRBuilder::Force, IRBuilder::Unresolve>(
48937b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const llvm::StringRef& pName,
49037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Type pType,
49137b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Desc pDesc,
49237b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Binding pBinding,
49337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::SizeType pSize,
49437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    LDSymbol::ValueType pValue,
49537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    FragmentRef* pFragmentRef,
49637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Visibility pVisibility);
49737b74a387bb3993387029859c2d9d051c41c724eStephen Hines
49837b74a387bb3993387029859c2d9d051c41c724eStephen Hinestemplate <>
49937b74a387bb3993387029859c2d9d051c41c724eStephen HinesLDSymbol* IRBuilder::AddSymbol<IRBuilder::AsReferred, IRBuilder::Unresolve>(
50037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const llvm::StringRef& pName,
50137b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Type pType,
50237b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Desc pDesc,
50337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Binding pBinding,
50437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::SizeType pSize,
50537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    LDSymbol::ValueType pValue,
50637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    FragmentRef* pFragmentRef,
50737b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Visibility pVisibility);
50837b74a387bb3993387029859c2d9d051c41c724eStephen Hines
50937b74a387bb3993387029859c2d9d051c41c724eStephen Hinestemplate <>
51037b74a387bb3993387029859c2d9d051c41c724eStephen HinesLDSymbol* IRBuilder::AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
51137b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const llvm::StringRef& pName,
51237b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Type pType,
51337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Desc pDesc,
51437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Binding pBinding,
51537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::SizeType pSize,
51637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    LDSymbol::ValueType pValue,
51737b74a387bb3993387029859c2d9d051c41c724eStephen Hines    FragmentRef* pFragmentRef,
51837b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Visibility pVisibility);
51937b74a387bb3993387029859c2d9d051c41c724eStephen Hines
52037b74a387bb3993387029859c2d9d051c41c724eStephen Hinestemplate <>
52137b74a387bb3993387029859c2d9d051c41c724eStephen HinesLDSymbol* IRBuilder::AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
52237b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const llvm::StringRef& pName,
52337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Type pType,
52437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Desc pDesc,
52537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Binding pBinding,
52637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::SizeType pSize,
52737b74a387bb3993387029859c2d9d051c41c724eStephen Hines    LDSymbol::ValueType pValue,
52837b74a387bb3993387029859c2d9d051c41c724eStephen Hines    FragmentRef* pFragmentRef,
52937b74a387bb3993387029859c2d9d051c41c724eStephen Hines    ResolveInfo::Visibility pVisibility);
53037b74a387bb3993387029859c2d9d051c41c724eStephen Hines
53137b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // end of namespace mcld
53237b74a387bb3993387029859c2d9d051c41c724eStephen Hines
53337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif  // MCLD_IRBUILDER_H_
534