Snapshot. Now able to produce a minimal executable which actually
[external/binutils.git] / gold / stringpool.h
1 // stringpool.h -- a string pool for gold    -*- C++ -*-
2
3 #include <string>
4 #include <list>
5
6 // Stringpool
7 //   Manage a pool of unique strings.
8
9 #ifndef GOLD_STRINGPOOL_H
10 #define GOLD_STRINGPOOL_H
11
12 namespace gold
13 {
14
15 class Output_file;
16
17 class Stringpool
18 {
19  public:
20   Stringpool();
21
22   ~Stringpool();
23
24   // Add a string to the pool.  This returns a canonical permanent
25   // pointer to the string.
26   const char*
27   add(const char*);
28
29   const char*
30   add(const std::string& s)
31   { return this->add(s.c_str()); }
32
33   // Add the prefix of a string to the pool.
34   const char*
35   add(const char *, size_t);
36
37   // If a string is present, return the canonical string.  Otherwise,
38   // return NULL.
39   const char*
40   find(const char*) const;
41
42   // Turn the stringpool into an ELF strtab: determine the offsets of
43   // all the strings.
44   void
45   set_string_offsets();
46
47   // Get the offset of a string.
48   off_t
49   get_offset(const char*) const;
50
51   off_t
52   get_offset(const std::string& s) const
53   { return this->get_offset(s.c_str()); }
54
55   // Get the size of the ELF strtab.
56   off_t
57   get_strtab_size() const
58   { return this->strtab_size_; }
59
60   // Write the strtab into the output file at the specified offset.
61   void
62   write(Output_file*, off_t offset);
63
64  private:
65   Stringpool(const Stringpool&);
66   Stringpool& operator=(const Stringpool&);
67
68   // We store the actual data in a list of these buffers.
69   struct Stringdata
70   {
71     // Length of data in buffer.
72     size_t len;
73     // Allocated size of buffer.
74     size_t alc;
75     // Buffer.
76     char data[1];
77   };
78
79   // Copy a string into the buffers, returning a canonical string.
80   const char*
81   add_string(const char*);
82
83   struct Stringpool_hash
84   {
85     size_t
86     operator()(const char*) const;
87   };
88
89   struct Stringpool_eq
90   {
91     bool
92     operator()(const char* p1, const char* p2) const
93     { return strcmp(p1, p2) == 0; }
94   };
95
96   // Return whether s1 is a suffix of s2.
97   static bool is_suffix(const char* s1, const char* s2);
98
99   // The hash table is a map from string names to offsets.  We only
100   // use the offsets if we turn this into an ELF strtab section.
101
102 #ifdef HAVE_TR1_UNORDERED_SET
103   typedef Unordered_map<const char*, off_t, Stringpool_hash,
104                         Stringpool_eq,
105                         std::allocator<std::pair<const char* const, off_t> >,
106                         true> String_set_type;
107 #else
108   typedef Unordered_map<const char*, off_t, Stringpool_hash,
109                         Stringpool_eq> String_set_type;
110 #endif
111
112   // Comparison routine used when sorting into an ELF strtab.
113
114   struct Stringpool_sort_comparison
115   {
116     bool
117     operator()(String_set_type::iterator,
118                String_set_type::iterator) const;
119   };
120
121   String_set_type string_set_;
122   std::list<Stringdata*> strings_;
123   off_t strtab_size_;
124 };
125
126 } // End namespace gold.
127
128 #endif // !defined(GOLD_STRINGPOOL_H)