1//===- SectionMerger.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_SECTION_MERGER_H
10#define MCLD_SECTION_MERGER_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14
15#include <vector>
16#include <string>
17
18#include <mcld/LD/LDSection.h>
19#include <mcld/LD/LDContext.h>
20#include <mcld/LD/SectionMap.h>
21
22namespace mcld
23{
24class MCLinker;
25
26/** \class SectionMerger
27 *  \brief maintain the mappings of substr of input section name to associated
28 *         output section (data)
29 */
30class SectionMerger
31{
32public:
33  struct Mapping {
34    std::string inputSubStr;
35    LDSection* outputSection;
36  };
37  typedef std::vector<Mapping> LDSectionMapTy;
38
39  typedef LDSectionMapTy::iterator iterator;
40  typedef LDSectionMapTy::const_iterator const_iterator;
41
42public:
43  SectionMerger(SectionMap& pSectionMap, LDContext& pContext);
44  ~SectionMerger();
45
46  /// getOutputSectHdr - return a associated output section header
47  LDSection* getOutputSectHdr(const std::string& pName);
48
49  /// getOutputSectData - return a associated output section data
50  SectionData* getOutputSectData(const std::string& pName);
51
52  /// addMapping - add a mapping as creating one new output LDSection
53  /// @param pName - a input section name
54  /// @param pSection - the output LDSection*
55  bool addMapping(const std::string& pName, LDSection* pSection);
56
57  // -----  observers  ----- //
58  bool empty() const
59  { return m_LDSectionMap.empty(); }
60
61  size_t size() const
62  { return m_LDSectionMap.size(); }
63
64  size_t capacity () const
65  { return m_LDSectionMap.capacity(); }
66
67  // -----  iterators  ----- //
68  iterator find(const std::string& pName);
69
70  iterator begin()
71  { return m_LDSectionMap.begin(); }
72
73  iterator end()
74  { return m_LDSectionMap.end(); }
75
76  const_iterator begin() const
77  { return m_LDSectionMap.begin(); }
78
79  const_iterator end() const
80  { return m_LDSectionMap.end(); }
81
82private:
83  /// initOutputSectMap - initialize the map from input substr to associated
84  /// output LDSection*
85  void initOutputSectMap();
86
87private:
88  SectionMap& m_SectionNameMap;
89
90  LDContext& m_Output;
91
92  LDSectionMapTy m_LDSectionMap;
93};
94
95} // namespace of mcld
96
97#endif
98
99