Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // stringpool.h -- a string pool for gold -*- C++ -*- |
2 | ||
ebdbb458 | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
14bfc3f5 ILT |
23 | #include <string> |
24 | #include <list> | |
2030fba0 | 25 | #include <vector> |
14bfc3f5 | 26 | |
14bfc3f5 ILT |
27 | #ifndef GOLD_STRINGPOOL_H |
28 | #define GOLD_STRINGPOOL_H | |
29 | ||
30 | namespace gold | |
31 | { | |
32 | ||
61ba1cf9 ILT |
33 | class Output_file; |
34 | ||
835965e6 ILT |
35 | // A Stringpool is a pool of unique strings. It provides the |
36 | // following features: | |
37 | ||
38 | // Every string in the pool is unique. Thus, if you have two strings | |
39 | // in the Stringpool, you can compare them for equality by using | |
40 | // pointer comparison rather than string comparison. | |
41 | ||
42 | // There is a key associated with every string in the pool. If you | |
43 | // add strings to the Stringpool in the same order, then the key for | |
44 | // each string will always be the same for any run of the linker. | |
45 | // This is not true of the string pointers themselves, as they may | |
46 | // change due to address space randomization. Some parts of the | |
47 | // linker (e.g., the symbol table) use the key value instead of the | |
48 | // string pointer so that repeated runs of the linker will generate | |
49 | // precisely the same output. | |
50 | ||
cfd73a4e ILT |
51 | // When you add a string to a Stringpool, Stringpool will optionally |
52 | // make a copy of it. Thus there is no requirement to keep a copy | |
53 | // elsewhere. | |
bc56b3fb | 54 | |
835965e6 ILT |
55 | // A Stringpool can be turned into a string table, a sequential series |
56 | // of null terminated strings. The first string may optionally be a | |
57 | // single zero byte, as required for SHT_STRTAB sections. This | |
58 | // conversion is only permitted after all strings have been added to | |
59 | // the Stringpool. After doing this conversion, you can ask for the | |
2030fba0 ILT |
60 | // offset of any string (or any key) in the stringpool in the string |
61 | // table, and you can write the resulting string table to an output | |
62 | // file. | |
835965e6 ILT |
63 | |
64 | // When a Stringpool is turned into a string table, then as an | |
65 | // optimization it will reuse string suffixes to avoid duplicating | |
66 | // strings. That is, given the strings "abc" and "bc", only the | |
67 | // string "abc" will be stored, and "bc" will be represented by an | |
68 | // offset into the middle of the string "abc". | |
69 | ||
2030fba0 ILT |
70 | |
71 | // A simple chunked vector class--this is a subset of std::vector | |
72 | // which stores memory in chunks. We don't provide iterators, because | |
73 | // we don't need them. | |
74 | ||
75 | template<typename Element> | |
76 | class Chunked_vector | |
77 | { | |
78 | public: | |
79 | Chunked_vector() | |
1aa37384 | 80 | : chunks_(), size_(0) |
2030fba0 ILT |
81 | { } |
82 | ||
83 | // Clear the elements. | |
84 | void | |
85 | clear() | |
1aa37384 DK |
86 | { |
87 | this->chunks_.clear(); | |
88 | this->size_ = 0; | |
89 | } | |
2030fba0 ILT |
90 | |
91 | // Reserve elements. | |
92 | void | |
93 | reserve(unsigned int n) | |
94 | { | |
1aa37384 | 95 | if (n > this->chunks_.size() * chunk_size) |
2030fba0 | 96 | { |
1aa37384 DK |
97 | this->chunks_.resize((n + chunk_size - 1) / chunk_size); |
98 | // We need to call reserve() of all chunks since changing | |
99 | // this->chunks_ casues Element_vectors to be copied. The | |
100 | // reserved capacity of an Element_vector may be lost in copying. | |
101 | for (size_t i = 0; i < this->chunks_.size(); ++i) | |
102 | this->chunks_[i].reserve(chunk_size); | |
2030fba0 ILT |
103 | } |
104 | } | |
105 | ||
106 | // Get the number of elements. | |
107 | size_t | |
108 | size() const | |
1aa37384 | 109 | { return this->size_; } |
2030fba0 ILT |
110 | |
111 | // Push a new element on the back of the vector. | |
112 | void | |
113 | push_back(const Element& element) | |
114 | { | |
1aa37384 DK |
115 | size_t chunk_index = this->size_ / chunk_size; |
116 | if (chunk_index >= this->chunks_.size()) | |
2030fba0 ILT |
117 | { |
118 | this->chunks_.push_back(Element_vector()); | |
119 | this->chunks_.back().reserve(chunk_size); | |
1aa37384 | 120 | gold_assert(chunk_index < this->chunks_.size()); |
2030fba0 | 121 | } |
1aa37384 DK |
122 | this->chunks_[chunk_index].push_back(element); |
123 | this->size_++; | |
2030fba0 ILT |
124 | } |
125 | ||
126 | // Return a reference to an entry in the vector. | |
127 | Element& | |
128 | operator[](size_t i) | |
129 | { return this->chunks_[i / chunk_size][i % chunk_size]; } | |
130 | ||
131 | const Element& | |
132 | operator[](size_t i) const | |
133 | { return this->chunks_[i / chunk_size][i % chunk_size]; } | |
134 | ||
135 | private: | |
136 | static const unsigned int chunk_size = 8192; | |
137 | ||
138 | typedef std::vector<Element> Element_vector; | |
139 | typedef std::vector<Element_vector> Chunk_vector; | |
140 | ||
141 | Chunk_vector chunks_; | |
1aa37384 | 142 | size_t size_; |
2030fba0 ILT |
143 | }; |
144 | ||
145 | ||
835965e6 ILT |
146 | // Stringpools are implemented in terms of Stringpool_template, which |
147 | // is generalized on the type of character used for the strings. Most | |
148 | // uses will want the Stringpool type which uses char. Other cases | |
149 | // are used for merging wide string constants. | |
150 | ||
b8e6aad9 ILT |
151 | template<typename Stringpool_char> |
152 | class Stringpool_template | |
14bfc3f5 ILT |
153 | { |
154 | public: | |
835965e6 ILT |
155 | // The type of a key into the stringpool. As described above, a key |
156 | // value will always be the same during any run of the linker. Zero | |
157 | // is never a valid key value. | |
f0641a0b ILT |
158 | typedef size_t Key; |
159 | ||
a8b2552e ILT |
160 | // Create a Stringpool. |
161 | Stringpool_template(); | |
14bfc3f5 | 162 | |
b8e6aad9 | 163 | ~Stringpool_template(); |
14bfc3f5 | 164 | |
9a0910c3 ILT |
165 | // Clear all the data from the stringpool. |
166 | void | |
167 | clear(); | |
168 | ||
6d013333 ILT |
169 | // Hint to the stringpool class that you intend to insert n additional |
170 | // elements. The stringpool class can use this info however it likes; | |
171 | // in practice it will resize its internal hashtables to make room. | |
172 | void | |
173 | reserve(unsigned int n); | |
174 | ||
a8b2552e ILT |
175 | // Indicate that we should not reserve offset 0 to hold the empty |
176 | // string when converting the stringpool to a string table. This | |
177 | // should not be called for a proper ELF SHT_STRTAB section. | |
178 | void | |
179 | set_no_zero_null() | |
1aa37384 DK |
180 | { |
181 | gold_assert(this->string_set_.empty()); | |
182 | this->zero_null_ = false; | |
183 | } | |
a8b2552e | 184 | |
f7c8a183 ILT |
185 | // Indicate that this string pool should be optimized, even if not |
186 | // running with -O2. | |
187 | void | |
188 | set_optimize() | |
189 | { this->optimize_ = true; } | |
190 | ||
835965e6 | 191 | // Add the string S to the pool. This returns a canonical permanent |
cfd73a4e ILT |
192 | // pointer to the string in the pool. If COPY is true, the string |
193 | // is copied into permanent storage. If PKEY is not NULL, this sets | |
194 | // *PKEY to the key for the string. | |
b8e6aad9 | 195 | const Stringpool_char* |
cfd73a4e | 196 | add(const Stringpool_char* s, bool copy, Key* pkey); |
14bfc3f5 | 197 | |
c0873094 ILT |
198 | // Add string S of length LEN characters to the pool. If COPY is |
199 | // true, S need not be null terminated. | |
b8e6aad9 | 200 | const Stringpool_char* |
c0873094 | 201 | add_with_length(const Stringpool_char* s, size_t len, bool copy, Key* pkey); |
61ba1cf9 | 202 | |
835965e6 ILT |
203 | // If the string S is present in the pool, return the canonical |
204 | // string pointer. Otherwise, return NULL. If PKEY is not NULL, | |
205 | // set *PKEY to the key. | |
b8e6aad9 | 206 | const Stringpool_char* |
835965e6 | 207 | find(const Stringpool_char* s, Key* pkey) const; |
61ba1cf9 | 208 | |
835965e6 ILT |
209 | // Turn the stringpool into a string table: determine the offsets of |
210 | // all the strings. After this is called, no more strings may be | |
211 | // added to the stringpool. | |
61ba1cf9 ILT |
212 | void |
213 | set_string_offsets(); | |
214 | ||
835965e6 ILT |
215 | // Get the offset of the string S in the string table. This returns |
216 | // the offset in bytes, not in units of Stringpool_char. This may | |
217 | // only be called after set_string_offsets has been called. | |
8383303e | 218 | section_offset_type |
835965e6 | 219 | get_offset(const Stringpool_char* s) const; |
61ba1cf9 | 220 | |
835965e6 | 221 | // Get the offset of the string S in the string table. |
8383303e | 222 | section_offset_type |
b8e6aad9 | 223 | get_offset(const std::basic_string<Stringpool_char>& s) const |
c0873094 ILT |
224 | { return this->get_offset_with_length(s.c_str(), s.size()); } |
225 | ||
226 | // Get the offset of string S, with length LENGTH characters, in the | |
227 | // string table. | |
228 | section_offset_type | |
229 | get_offset_with_length(const Stringpool_char* s, size_t length) const; | |
61ba1cf9 | 230 | |
2030fba0 ILT |
231 | // Get the offset of the string with key K. |
232 | section_offset_type | |
233 | get_offset_from_key(Key k) const | |
234 | { | |
5778530e ILT |
235 | gold_assert(k <= this->key_to_offset_.size()); |
236 | return this->key_to_offset_[k - 1]; | |
2030fba0 ILT |
237 | } |
238 | ||
835965e6 ILT |
239 | // Get the size of the string table. This returns the number of |
240 | // bytes, not in units of Stringpool_char. | |
8383303e | 241 | section_size_type |
61ba1cf9 | 242 | get_strtab_size() const |
a3ad94ed ILT |
243 | { |
244 | gold_assert(this->strtab_size_ != 0); | |
245 | return this->strtab_size_; | |
246 | } | |
61ba1cf9 | 247 | |
835965e6 ILT |
248 | // Write the string table into the output file at the specified |
249 | // offset. | |
61ba1cf9 ILT |
250 | void |
251 | write(Output_file*, off_t offset); | |
14bfc3f5 | 252 | |
9a0910c3 ILT |
253 | // Write the string table into the specified buffer, of the |
254 | // specified size. buffer_size should be at least | |
255 | // get_strtab_size(). | |
256 | void | |
8383303e | 257 | write_to_buffer(unsigned char* buffer, section_size_type buffer_size); |
9a0910c3 | 258 | |
ad8f37d1 ILT |
259 | // Dump statistical information to stderr. |
260 | void | |
261 | print_stats(const char*) const; | |
262 | ||
14bfc3f5 | 263 | private: |
b8e6aad9 ILT |
264 | Stringpool_template(const Stringpool_template&); |
265 | Stringpool_template& operator=(const Stringpool_template&); | |
266 | ||
835965e6 | 267 | // Return the length of a string in units of Stringpool_char. |
b8e6aad9 ILT |
268 | static size_t |
269 | string_length(const Stringpool_char*); | |
14bfc3f5 | 270 | |
987cc251 ILT |
271 | // Return whether two strings are equal. |
272 | static bool | |
273 | string_equal(const Stringpool_char*, const Stringpool_char*); | |
274 | ||
275 | // Compute a hash code for a string. LENGTH is the length of the | |
276 | // string in characters. | |
277 | static size_t | |
278 | string_hash(const Stringpool_char*, size_t length); | |
279 | ||
61ba1cf9 ILT |
280 | // We store the actual data in a list of these buffers. |
281 | struct Stringdata | |
14bfc3f5 ILT |
282 | { |
283 | // Length of data in buffer. | |
284 | size_t len; | |
285 | // Allocated size of buffer. | |
286 | size_t alc; | |
287 | // Buffer. | |
288 | char data[1]; | |
289 | }; | |
290 | ||
1aa37384 DK |
291 | // Add a new key offset entry. |
292 | void | |
293 | new_key_offset(size_t); | |
294 | ||
61ba1cf9 | 295 | // Copy a string into the buffers, returning a canonical string. |
b8e6aad9 | 296 | const Stringpool_char* |
2030fba0 | 297 | add_string(const Stringpool_char*, size_t); |
987cc251 ILT |
298 | |
299 | // Return whether s1 is a suffix of s2. | |
300 | static bool | |
301 | is_suffix(const Stringpool_char* s1, size_t len1, | |
302 | const Stringpool_char* s2, size_t len2); | |
303 | ||
304 | // The hash table key includes the string, the length of the string, | |
305 | // and the hash code for the string. We put the hash code | |
306 | // explicitly into the key so that we can do a find()/insert() | |
307 | // sequence without having to recompute the hash. Computing the | |
308 | // hash code is a significant user of CPU time in the linker. | |
309 | struct Hashkey | |
310 | { | |
311 | const Stringpool_char* string; | |
312 | // Length is in characters, not bytes. | |
313 | size_t length; | |
314 | size_t hash_code; | |
315 | ||
316 | // This goes in an STL container, so we need a default | |
317 | // constructor. | |
318 | Hashkey() | |
319 | : string(NULL), length(0), hash_code(0) | |
320 | { } | |
321 | ||
322 | // Note that these constructors are relatively expensive, because | |
323 | // they compute the hash code. | |
c0873094 | 324 | explicit Hashkey(const Stringpool_char* s) |
987cc251 ILT |
325 | : string(s), length(string_length(s)), hash_code(string_hash(s, length)) |
326 | { } | |
327 | ||
328 | Hashkey(const Stringpool_char* s, size_t len) | |
329 | : string(s), length(len), hash_code(string_hash(s, len)) | |
330 | { } | |
331 | }; | |
14bfc3f5 | 332 | |
987cc251 ILT |
333 | // Hash function. This is trivial, since we have already computed |
334 | // the hash. | |
14bfc3f5 ILT |
335 | struct Stringpool_hash |
336 | { | |
337 | size_t | |
987cc251 ILT |
338 | operator()(const Hashkey& hk) const |
339 | { return hk.hash_code; } | |
14bfc3f5 ILT |
340 | }; |
341 | ||
835965e6 | 342 | // Equality comparison function for hash table. |
14bfc3f5 ILT |
343 | struct Stringpool_eq |
344 | { | |
345 | bool | |
987cc251 | 346 | operator()(const Hashkey&, const Hashkey&) const; |
14bfc3f5 ILT |
347 | }; |
348 | ||
2030fba0 | 349 | // The hash table is a map from strings to Keys. |
f0641a0b | 350 | |
2030fba0 | 351 | typedef Key Hashval; |
61ba1cf9 | 352 | |
987cc251 | 353 | typedef Unordered_map<Hashkey, Hashval, Stringpool_hash, |
61ba1cf9 | 354 | Stringpool_eq> String_set_type; |
274e99f9 | 355 | |
987cc251 ILT |
356 | // Comparison routine used when sorting into a string table. |
357 | ||
358 | typedef typename String_set_type::iterator Stringpool_sort_info; | |
61ba1cf9 ILT |
359 | |
360 | struct Stringpool_sort_comparison | |
361 | { | |
362 | bool | |
614f30a2 | 363 | operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const; |
61ba1cf9 ILT |
364 | }; |
365 | ||
2030fba0 ILT |
366 | // Keys map to offsets via a Chunked_vector. We only use the |
367 | // offsets if we turn this into an string table section. | |
368 | typedef Chunked_vector<section_offset_type> Key_to_offset; | |
369 | ||
f0641a0b ILT |
370 | // List of Stringdata structures. |
371 | typedef std::list<Stringdata*> Stringdata_list; | |
372 | ||
373 | // Mapping from const char* to namepool entry. | |
14bfc3f5 | 374 | String_set_type string_set_; |
2030fba0 ILT |
375 | // Mapping from Key to string table offset. |
376 | Key_to_offset key_to_offset_; | |
f0641a0b ILT |
377 | // List of buffers. |
378 | Stringdata_list strings_; | |
835965e6 | 379 | // Size of string table. |
8383303e | 380 | section_size_type strtab_size_; |
b8e6aad9 ILT |
381 | // Whether to reserve offset 0 to hold the null string. |
382 | bool zero_null_; | |
f7c8a183 ILT |
383 | // Whether to optimize the string table. |
384 | bool optimize_; | |
1aa37384 DK |
385 | // offset of the next string. |
386 | section_offset_type offset_; | |
14bfc3f5 ILT |
387 | }; |
388 | ||
b8e6aad9 ILT |
389 | // The most common type of Stringpool. |
390 | typedef Stringpool_template<char> Stringpool; | |
391 | ||
14bfc3f5 ILT |
392 | } // End namespace gold. |
393 | ||
394 | #endif // !defined(GOLD_STRINGPOOL_H) |