ObjectBuilder.h revision 22add6ff3426df1a85089fe6a6e1597ee3b6f300
1//===- ObjectBuilder.h ----------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef MCLD_OBJECT_OBJECT_BUILDER_H
10#define MCLD_OBJECT_OBJECT_BUILDER_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14#include <mcld/LD/LDFileFormat.h>
15#include <mcld/LD/EhFrame.h>
16
17#include <llvm/Support/DataTypes.h>
18
19#include <string>
20
21namespace mcld {
22
23class LinkerConfig;
24class Module;
25class LDSection;
26class SectionData;
27class RelocData;
28class Fragment;
29class Relocation;
30
31/** \class ObjectBuilder
32 *  \brief ObjectBuilder recieve ObjectAction and build the mcld::Module.
33 */
34class ObjectBuilder
35{
36public:
37  ObjectBuilder(const LinkerConfig& pConfig,
38                Module& pTheModule);
39
40/// @}
41/// @name Section Methods
42/// @{
43  /// CreateSection - To create an output LDSection in mcld::Module.
44  /// Link scripts and command line options define some SECTIONS commands that
45  /// specify where input sections are placed into output sections. This function
46  /// checks SECTIONS commands to change given name to the output section name.
47  /// This function creates a new LDSection and push the created LDSection into
48  /// @ref mcld::Module.
49  ///
50  /// To create an input LDSection in mcld::LDContext, use @ref LDSection::Create().
51  ///
52  /// @see SectionMap
53  ///
54  /// @param [in] pName The given name. Returned LDSection used the changed name
55  ///                   by SectionMap.
56  LDSection* CreateSection(const std::string& pInputName,
57                           LDFileFormat::Kind pKind,
58                           uint32_t pType,
59                           uint32_t pFlag,
60                           uint32_t pAlign = 0x0);
61
62  /// MergeSection - merge the pInput section to mcld::Module.
63  /// This function moves all fragments in pInputSection to the corresponding
64  /// output section of mcld::Module.
65  ///
66  /// @see SectionMap
67  /// @param [in] pInputSection The merged input section.
68  /// @return If the corresponding output sections is not defined, return false.
69  bool MergeSection(LDSection& pInputSection);
70
71  /// MoveSectionData - move the fragment of pFrom to pTo section data.
72  static bool MoveSectionData(SectionData& pFrom, SectionData& pTo);
73
74/// @}
75/// @name Fragment Methods
76/// @{
77  /// AppendFragment - To append pFrag to the given SectionData pSD.
78  /// In order to keep the alignment of pFrag, This function inserts an
79  /// AlignFragment before pFrag if pAlignConstraint is larger than 1.
80  ///
81  /// @note This function does not update the alignment constraint of LDSection.
82  ///
83  /// @param [in, out] pFrag The appended fragment. The offset of the appended
84  ///        pFrag is set to the offset in pSD.
85  /// @param [in, out] pSD The section data being appended.
86  /// @param [in] pAlignConstraint The alignment constraint.
87  /// @return Total size of the inserted fragments.
88  static uint64_t AppendFragment(Fragment& pFrag, SectionData& pSD,
89                                 uint32_t pAlignConstraint = 1);
90
91private:
92  const LinkerConfig& m_Config;
93  Module& m_Module;
94};
95
96} // namespace of mcld
97
98#endif
99
100