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