Note that a Stringpool keeps a copy of a string.
[deliverable/binutils-gdb.git] / gold / stringpool.h
1 // stringpool.h -- a string pool for gold -*- C++ -*-
2
3 #include <string>
4 #include <list>
5
6 #ifndef GOLD_STRINGPOOL_H
7 #define GOLD_STRINGPOOL_H
8
9 namespace gold
10 {
11
12 class Output_file;
13
14 // A Stringpool is a pool of unique strings. It provides the
15 // following features:
16
17 // Every string in the pool is unique. Thus, if you have two strings
18 // in the Stringpool, you can compare them for equality by using
19 // pointer comparison rather than string comparison.
20
21 // There is a key associated with every string in the pool. If you
22 // add strings to the Stringpool in the same order, then the key for
23 // each string will always be the same for any run of the linker.
24 // This is not true of the string pointers themselves, as they may
25 // change due to address space randomization. Some parts of the
26 // linker (e.g., the symbol table) use the key value instead of the
27 // string pointer so that repeated runs of the linker will generate
28 // precisely the same output.
29
30 // When you add a string to a Stringpool, Stringpool will make a copy
31 // of it. Thus there is no need to keep a copy elsewhere.
32
33 // A Stringpool can be turned into a string table, a sequential series
34 // of null terminated strings. The first string may optionally be a
35 // single zero byte, as required for SHT_STRTAB sections. This
36 // conversion is only permitted after all strings have been added to
37 // the Stringpool. After doing this conversion, you can ask for the
38 // offset of any string in the stringpool in the string table, and you
39 // can write the resulting string table to an output file.
40
41 // When a Stringpool is turned into a string table, then as an
42 // optimization it will reuse string suffixes to avoid duplicating
43 // strings. That is, given the strings "abc" and "bc", only the
44 // string "abc" will be stored, and "bc" will be represented by an
45 // offset into the middle of the string "abc".
46
47 // Stringpools are implemented in terms of Stringpool_template, which
48 // is generalized on the type of character used for the strings. Most
49 // uses will want the Stringpool type which uses char. Other cases
50 // are used for merging wide string constants.
51
52 template<typename Stringpool_char>
53 class Stringpool_template
54 {
55 public:
56 // The type of a key into the stringpool. As described above, a key
57 // value will always be the same during any run of the linker. Zero
58 // is never a valid key value.
59 typedef size_t Key;
60
61 // Create a Stringpool. ZERO_NULL is true if we should reserve
62 // offset 0 to hold the empty string when converting the stringpool
63 // to a string table. ZERO_NULL should be true if you want a proper
64 // ELF SHT_STRTAB section.
65 Stringpool_template(bool zero_null = true);
66
67 ~Stringpool_template();
68
69 // Add the string S to the pool. This returns a canonical permanent
70 // pointer to the string in the pool. If PKEY is not NULL, this
71 // sets *PKEY to the key for the string.
72 const Stringpool_char*
73 add(const Stringpool_char* s, Key* pkey);
74
75 // Add the string S to the pool.
76 const Stringpool_char*
77 add(const std::basic_string<Stringpool_char>& s, Key* pkey)
78 { return this->add(s.c_str(), pkey); }
79
80 // Add the prefix of length LEN of string S to the pool.
81 const Stringpool_char*
82 add(const Stringpool_char* s, size_t len, Key* pkey);
83
84 // If the string S is present in the pool, return the canonical
85 // string pointer. Otherwise, return NULL. If PKEY is not NULL,
86 // set *PKEY to the key.
87 const Stringpool_char*
88 find(const Stringpool_char* s, Key* pkey) const;
89
90 // Turn the stringpool into a string table: determine the offsets of
91 // all the strings. After this is called, no more strings may be
92 // added to the stringpool.
93 void
94 set_string_offsets();
95
96 // Get the offset of the string S in the string table. This returns
97 // the offset in bytes, not in units of Stringpool_char. This may
98 // only be called after set_string_offsets has been called.
99 off_t
100 get_offset(const Stringpool_char* s) const;
101
102 // Get the offset of the string S in the string table.
103 off_t
104 get_offset(const std::basic_string<Stringpool_char>& s) const
105 { return this->get_offset(s.c_str()); }
106
107 // Get the size of the string table. This returns the number of
108 // bytes, not in units of Stringpool_char.
109 off_t
110 get_strtab_size() const
111 {
112 gold_assert(this->strtab_size_ != 0);
113 return this->strtab_size_;
114 }
115
116 // Write the string table into the output file at the specified
117 // offset.
118 void
119 write(Output_file*, off_t offset);
120
121 private:
122 Stringpool_template(const Stringpool_template&);
123 Stringpool_template& operator=(const Stringpool_template&);
124
125 // Return the length of a string in units of Stringpool_char.
126 static size_t
127 string_length(const Stringpool_char*);
128
129 // We store the actual data in a list of these buffers.
130 struct Stringdata
131 {
132 // Length of data in buffer.
133 size_t len;
134 // Allocated size of buffer.
135 size_t alc;
136 // Buffer index.
137 unsigned int index;
138 // Buffer.
139 char data[1];
140 };
141
142 // Copy a string into the buffers, returning a canonical string.
143 const Stringpool_char*
144 add_string(const Stringpool_char*, Key*);
145
146 // Hash function.
147 struct Stringpool_hash
148 {
149 size_t
150 operator()(const Stringpool_char*) const;
151 };
152
153 // Equality comparison function for hash table.
154 struct Stringpool_eq
155 {
156 bool
157 operator()(const Stringpool_char* p1, const Stringpool_char* p2) const;
158 };
159
160 // Return whether s1 is a suffix of s2.
161 static bool
162 is_suffix(const Stringpool_char* s1, size_t len1,
163 const Stringpool_char* s2, size_t len2);
164
165 // The hash table is a map from string names to a pair of Key and
166 // string table offsets. We only use the offsets if we turn this
167 // into an string table section.
168
169 typedef std::pair<Key, off_t> Val;
170
171 #ifdef HAVE_TR1_UNORDERED_SET
172 typedef Unordered_map<const Stringpool_char*, Val, Stringpool_hash,
173 Stringpool_eq,
174 std::allocator<std::pair<const Stringpool_char* const,
175 Val> >,
176 true> String_set_type;
177 #else
178 typedef Unordered_map<const Stringpool_char*, Val, Stringpool_hash,
179 Stringpool_eq> String_set_type;
180 #endif
181
182 // Comparison routine used when sorting into a string table. We
183 // store string-sizes in the sort-vector so we don't have to
184 // recompute them log(n) times as we sort.
185 struct Stringpool_sort_info
186 {
187 typename String_set_type::iterator it;
188 size_t string_length;
189 Stringpool_sort_info(typename String_set_type::iterator i, size_t s)
190 : it(i), string_length(s)
191 { }
192 };
193
194 struct Stringpool_sort_comparison
195 {
196 bool
197 operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const;
198 };
199
200 // List of Stringdata structures.
201 typedef std::list<Stringdata*> Stringdata_list;
202
203 // Mapping from const char* to namepool entry.
204 String_set_type string_set_;
205 // List of buffers.
206 Stringdata_list strings_;
207 // Size of string table.
208 off_t strtab_size_;
209 // Next Stringdata index.
210 unsigned int next_index_;
211 // Whether to reserve offset 0 to hold the null string.
212 bool zero_null_;
213 };
214
215 // The most common type of Stringpool.
216 typedef Stringpool_template<char> Stringpool;
217
218 } // End namespace gold.
219
220 #endif // !defined(GOLD_STRINGPOOL_H)
This page took 0.034094 seconds and 5 git commands to generate.