ObjectBuilder.h revision 37b74a387bb3993387029859c2d9d051c41c724e
108fa40c5cb5229b7969b2a5146855a337870f45aJim Miller//===- ObjectBuilder.h ----------------------------------------------------===//
208fa40c5cb5229b7969b2a5146855a337870f45aJim Miller//
308fa40c5cb5229b7969b2a5146855a337870f45aJim Miller//                     The MCLinker Project
408fa40c5cb5229b7969b2a5146855a337870f45aJim Miller//
508fa40c5cb5229b7969b2a5146855a337870f45aJim Miller// This file is distributed under the University of Illinois Open Source
608fa40c5cb5229b7969b2a5146855a337870f45aJim Miller// License. See LICENSE.TXT for details.
708fa40c5cb5229b7969b2a5146855a337870f45aJim Miller//
808fa40c5cb5229b7969b2a5146855a337870f45aJim Miller//===----------------------------------------------------------------------===//
908fa40c5cb5229b7969b2a5146855a337870f45aJim Miller#ifndef MCLD_OBJECT_OBJECTBUILDER_H_
1008fa40c5cb5229b7969b2a5146855a337870f45aJim Miller#define MCLD_OBJECT_OBJECTBUILDER_H_
1108fa40c5cb5229b7969b2a5146855a337870f45aJim Miller#include "mcld/LD/EhFrame.h"
1208fa40c5cb5229b7969b2a5146855a337870f45aJim Miller#include "mcld/LD/LDFileFormat.h"
1308fa40c5cb5229b7969b2a5146855a337870f45aJim Miller
1408fa40c5cb5229b7969b2a5146855a337870f45aJim Miller#include <llvm/Support/DataTypes.h>
1508fa40c5cb5229b7969b2a5146855a337870f45aJim Miller
1608fa40c5cb5229b7969b2a5146855a337870f45aJim Miller#include <string>
17ebbf205bc6e8292f74d8fc4652c70274a445f907Jim Miller
1808fa40c5cb5229b7969b2a5146855a337870f45aJim Millernamespace mcld {
19ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller
20ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Millerclass Fragment;
21f501b58de8f467a80fef49c704555781bc61ea6fJim Millerclass Input;
22dc589ac82b5fe2063f4cfd94c8ae26d43d5420a0Sudheer Shankaclass LDSection;
2308fa40c5cb5229b7969b2a5146855a337870f45aJim Millerclass Module;
24a7596147b43940cad3f76c53ed154ef088b9269bJim Millerclass SectionData;
259f0753f5a378fc80da86305b33244acc6fc53f01Jim Miller
26ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller/** \class ObjectBuilder
2708fa40c5cb5229b7969b2a5146855a337870f45aJim Miller *  \brief ObjectBuilder recieve ObjectAction and build the mcld::Module.
2808fa40c5cb5229b7969b2a5146855a337870f45aJim Miller */
2957e2f4db3535fb059ac278499018951deddc4c82Jorim Jaggiclass ObjectBuilder {
30f501b58de8f467a80fef49c704555781bc61ea6fJim Miller public:
315e354223d817477efac9a6a2e3ce3d9161e046a4Jorim Jaggi  explicit ObjectBuilder(Module& pTheModule);
3208fa40c5cb5229b7969b2a5146855a337870f45aJim Miller
3308fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  /// @}
34dcdaf87ed0aa99073638bcfe645949f130f0c7adAlex Klyubin  /// @name Section Methods
3508fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  /// @{
36a7596147b43940cad3f76c53ed154ef088b9269bJim Miller  /// CreateSection - To create an output LDSection in mcld::Module.
3708fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  /// Link scripts and command line options define some SECTIONS commands that
389f0753f5a378fc80da86305b33244acc6fc53f01Jim Miller  /// specify where input sections are placed into output sections. This
39ba67aee02cf864793129976cd8a8a46e60c60577Jim Miller  /// function
40ba67aee02cf864793129976cd8a8a46e60c60577Jim Miller  /// checks SECTIONS commands to change given name to the output section name.
419f0753f5a378fc80da86305b33244acc6fc53f01Jim Miller  /// This function creates a new LDSection and push the created LDSection into
42b62dc82b0c7208f106077b46fc7118da6baa6e13Jim Miller  /// @ref mcld::Module.
439f0753f5a378fc80da86305b33244acc6fc53f01Jim Miller  ///
4433fd3cf2d90ca877b139e7f47824683b774f122aClara Bayarri  /// To create an input LDSection in mcld::LDContext, use @ref
45f501b58de8f467a80fef49c704555781bc61ea6fJim Miller  /// LDSection::Create().
46f501b58de8f467a80fef49c704555781bc61ea6fJim Miller  ///
47f501b58de8f467a80fef49c704555781bc61ea6fJim Miller  /// @see SectionMap
4808fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  ///
4908fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  /// @param [in] pName The given name. Returned LDSection used the changed name
50ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  ///                   by SectionMap.
51ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  LDSection* CreateSection(const std::string& pInputName,
52ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller                           LDFileFormat::Kind pKind,
53ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller                           uint32_t pType,
5408fa40c5cb5229b7969b2a5146855a337870f45aJim Miller                           uint32_t pFlag,
5508fa40c5cb5229b7969b2a5146855a337870f45aJim Miller                           uint32_t pAlign = 0x0);
5608fa40c5cb5229b7969b2a5146855a337870f45aJim Miller
5708fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  /// MergeSection - merge the pInput section to mcld::Module.
58d08c2aceb238b02d8348518a2c87693054c6ce37Jim Miller  /// This function moves all fragments in pInputSection to the corresponding
5908fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  /// output section of mcld::Module.
60a7596147b43940cad3f76c53ed154ef088b9269bJim Miller  ///
61f501b58de8f467a80fef49c704555781bc61ea6fJim Miller  /// @see SectionMap
62f501b58de8f467a80fef49c704555781bc61ea6fJim Miller  /// @param [in] pInputSection The merged input section.
63f501b58de8f467a80fef49c704555781bc61ea6fJim Miller  /// @return The merged output section. If the corresponding output sections
64f501b58de8f467a80fef49c704555781bc61ea6fJim Miller  /// is not defined, return NULL.
6508fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  LDSection* MergeSection(const Input& pInputFile, LDSection& pInputSection);
66ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller
67ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// MoveSectionData - move the fragment of pFrom to pTo section data.
68ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  static bool MoveSectionData(SectionData& pFrom, SectionData& pTo);
69ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller
7008fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  /// UpdateSectionAlign - update alignment for input section
71ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  static void UpdateSectionAlign(LDSection& pTo, const LDSection& pFrom);
72ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller
73ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// UpdateSectionAlign - update alignment for the section
7408fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  static void UpdateSectionAlign(LDSection& pSection,
75ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller                                 uint32_t pAlignConstraint);
76ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller
77ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// @}
78ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// @name Fragment Methods
79a7596147b43940cad3f76c53ed154ef088b9269bJim Miller  /// @{
80ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// AppendFragment - To append pFrag to the given SectionData pSD.
81ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// In order to keep the alignment of pFrag, This function inserts an
82ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// AlignFragment before pFrag if pAlignConstraint is larger than 1.
83ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  ///
84ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// @note This function does not update the alignment constraint of LDSection.
85ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  ///
8608fa40c5cb5229b7969b2a5146855a337870f45aJim Miller  /// @param [in, out] pFrag The appended fragment. The offset of the appended
87ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  ///        pFrag is set to the offset in pSD.
88ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// @param [in, out] pSD The section data being appended.
89ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// @param [in] pAlignConstraint The alignment constraint.
90ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  /// @return Total size of the inserted fragments.
91ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  static uint64_t AppendFragment(Fragment& pFrag,
9208fa40c5cb5229b7969b2a5146855a337870f45aJim Miller                                 SectionData& pSD,
93ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller                                 uint32_t pAlignConstraint = 1);
94ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller
95ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller private:
96ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller  Module& m_Module;
97ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller};
98ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller
999f0753f5a378fc80da86305b33244acc6fc53f01Jim Miller}  // namespace mcld
100ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller
101ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller#endif  // MCLD_OBJECT_OBJECTBUILDER_H_
102ce7eb6daf06a88129da365eb2112537ce0bb1b75Jim Miller