ObjectBuilder.h revision 551ae4ebd3e9d137ea668fb83ae4a55b8cfba451
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_OBJECTBUILDER_H
10#define MCLD_OBJECT_OBJECTBUILDER_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;
30class Input;
31
32/** \class ObjectBuilder
33 *  \brief ObjectBuilder recieve ObjectAction and build the mcld::Module.
34 */
35class ObjectBuilder
36{
37public:
38  ObjectBuilder(const LinkerConfig& pConfig,
39                Module& pTheModule);
40
41/// @}
42/// @name Section Methods
43/// @{
44  /// CreateSection - To create an output LDSection in mcld::Module.
45  /// Link scripts and command line options define some SECTIONS commands that
46  /// specify where input sections are placed into output sections. This function
47  /// checks SECTIONS commands to change given name to the output section name.
48  /// This function creates a new LDSection and push the created LDSection into
49  /// @ref mcld::Module.
50  ///
51  /// To create an input LDSection in mcld::LDContext, use @ref LDSection::Create().
52  ///
53  /// @see SectionMap
54  ///
55  /// @param [in] pName The given name. Returned LDSection used the changed name
56  ///                   by SectionMap.
57  LDSection* CreateSection(const std::string& pInputName,
58                           LDFileFormat::Kind pKind,
59                           uint32_t pType,
60                           uint32_t pFlag,
61                           uint32_t pAlign = 0x0);
62
63  /// MergeSection - merge the pInput section to mcld::Module.
64  /// This function moves all fragments in pInputSection to the corresponding
65  /// output section of mcld::Module.
66  ///
67  /// @see SectionMap
68  /// @param [in] pInputSection The merged input section.
69  /// @return The merged output section. If the corresponding output sections
70  /// is not defined, return NULL.
71  LDSection* MergeSection(const Input& pInputFile, LDSection& pInputSection);
72
73  /// MoveSectionData - move the fragment of pFrom to pTo section data.
74  static bool MoveSectionData(SectionData& pFrom, SectionData& pTo);
75
76  /// UpdateSectionAlign - update alignment for input section
77  static void UpdateSectionAlign(LDSection& pTo, const LDSection& pFrom);
78
79  /// UpdateSectionAlign - update alignment for the section
80  static void UpdateSectionAlign(LDSection& pSection,
81                                 uint32_t pAlignConstraint);
82
83/// @}
84/// @name Fragment Methods
85/// @{
86  /// AppendFragment - To append pFrag to the given SectionData pSD.
87  /// In order to keep the alignment of pFrag, This function inserts an
88  /// AlignFragment before pFrag if pAlignConstraint is larger than 1.
89  ///
90  /// @note This function does not update the alignment constraint of LDSection.
91  ///
92  /// @param [in, out] pFrag The appended fragment. The offset of the appended
93  ///        pFrag is set to the offset in pSD.
94  /// @param [in, out] pSD The section data being appended.
95  /// @param [in] pAlignConstraint The alignment constraint.
96  /// @return Total size of the inserted fragments.
97  static uint64_t AppendFragment(Fragment& pFrag, SectionData& pSD,
98                                 uint32_t pAlignConstraint = 1);
99
100private:
101  const LinkerConfig& m_Config;
102  Module& m_Module;
103};
104
105} // namespace of mcld
106
107#endif
108
109