• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- SectionMap.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_MAP_H
10 #define MCLD_SECTION_MAP_H
11 #ifdef ENABLE_UNITTEST
12 #include <gtest.h>
13 #endif
14 
15 #include <vector>
16 #include <string>
17 
18 #include <llvm/Support/DataTypes.h>
19 
20 namespace mcld {
21 
22 /** \class SectionMap
23  *  \brief descirbe the mappings of input section's name (or prefix) to
24  *         its associated output section's name and offset
25  */
26 class SectionMap
27 {
28 public:
29   // a mapping in SectionMap is the triple of
30   // {input substr, output section's name, output section's offset}
31   struct NamePair
32   {
33   public:
34     NamePair();
35     NamePair(const std::string& pFrom, const std::string& pTo);
36 
37     bool isNull() const;
38 
39   public:
40     unsigned int hash;
41     std::string from;
42     std::string to;
43   };
44 
45   typedef std::vector<NamePair> NamePairList;
46   typedef NamePairList::iterator iterator;
47   typedef NamePairList::const_iterator const_iterator;
48 
49   /// NullName - the null object of NamePair
50   static NamePair NullName;
51 
52 public:
53   // get the possible output section name based on the mapping table
54   // return NullPair if not found
55   const NamePair& find(const std::string& pFrom) const;
56   NamePair&       find(const std::string& pFrom);
57 
58   const NamePair& find(const std::string& pFrom, unsigned int pHash) const;
59   NamePair&       find(const std::string& pFrom, unsigned int pHash);
60 
61   // add a mapping from input sub-string to output name.
62   // @param [in]  pFrom  the given input sub-string
63   // @param [in]  pTo    the mapped output string
64   // @param [out] pExist does pFrom exist?
65   NamePair& append(const std::string& pFrom,
66                    const std::string& pTo,
67                    bool& pExist);
68 
begin()69   const_iterator begin() const { return m_NamePairList.begin(); }
begin()70   iterator       begin()       { return m_NamePairList.begin(); }
end()71   const_iterator end  () const { return m_NamePairList.end(); }
end()72   iterator       end  ()       { return m_NamePairList.end(); }
73 
empty()74   bool           empty() const { return m_NamePairList.empty(); }
size()75   size_t         size () const { return m_NamePairList.size(); }
76 
77   static unsigned int hash(const std::string& pString);
78 
79 private:
80   bool matched(const NamePair& pNamePair,
81                const std::string& pInput,
82                unsigned int pHash) const;
83 private:
84   NamePairList m_NamePairList;
85 };
86 
87 } // namespace of mcld
88 
89 #endif
90 
91