1 //===- MergedStringTable.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_LD_MERGEDSTRINGTABLE_H_
10 #define MCLD_LD_MERGEDSTRINGTABLE_H_
11 
12 #include "mcld/Support/MemoryRegion.h"
13 
14 #include <llvm/ADT/StringMap.h>
15 #include <llvm/ADT/StringRef.h>
16 
17 namespace mcld {
18 
19 /** \class MergedStringTable
20  *  \brief MergedStringTable represents the mergeable string table. The sections
21  *  with flag SHF_MERGED and SHF_STRING are mergeable. Every string in
22  *  MergedStringTable is unique.
23  */
24 class MergedStringTable {
25  public:
26   typedef llvm::StringMap<size_t> StringMapTy;
27 
28  public:
MergedStringTable()29   MergedStringTable() {}
30 
31   /// insertString - insert a string to the string table
32   /// @return false if the string already exists in the map.
33   bool insertString(llvm::StringRef pString);
34 
35   /// finalizeOffset - finalize the output offset of strings. After this
36   /// function been called, any string should not be added to this table
37   /// @return the section size
38   uint64_t finalizeOffset();
39 
40   /// emit - emit the string table
41   void emit(MemoryRegion& pRegion);
42 
43   /// ----- observers -----///
44   /// getOutputOffset - get the output offset of the string. This should be
45   /// called after finalizeOffset.
46   size_t getOutputOffset(llvm::StringRef pStr);
47 
48  private:
49   typedef StringMapTy::iterator string_map_iterator;
50   typedef StringMapTy::const_iterator const_string_map_iterator;
51 
52  private:
53   /// m_StringMap - the string pool of this section. It maps the string to the
54   /// output offset. The key of this map is the string, and the value is output
55   /// offset
56   StringMapTy m_StringMap;
57 };
58 
59 }  // namespace mcld
60 
61 #endif  // MCLD_LD_MERGEDSTRINGTABLE_H_
62 
63