ObjectBuilder.h revision 87f34658dec9097d987d254a990ea7f311bfc95f
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/// @}
80/// @name Fragment Methods
81/// @{
82  /// AppendFragment - To append pFrag to the given SectionData pSD.
83  /// In order to keep the alignment of pFrag, This function inserts an
84  /// AlignFragment before pFrag if pAlignConstraint is larger than 1.
85  ///
86  /// @note This function does not update the alignment constraint of LDSection.
87  ///
88  /// @param [in, out] pFrag The appended fragment. The offset of the appended
89  ///        pFrag is set to the offset in pSD.
90  /// @param [in, out] pSD The section data being appended.
91  /// @param [in] pAlignConstraint The alignment constraint.
92  /// @return Total size of the inserted fragments.
93  static uint64_t AppendFragment(Fragment& pFrag, SectionData& pSD,
94                                 uint32_t pAlignConstraint = 1);
95
96private:
97  const LinkerConfig& m_Config;
98  Module& m_Module;
99};
100
101} // namespace of mcld
102
103#endif
104
105