Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // stringpool.h -- a string pool for gold -*- C++ -*- |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
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> | |
25 | ||
14bfc3f5 ILT |
26 | #ifndef GOLD_STRINGPOOL_H |
27 | #define GOLD_STRINGPOOL_H | |
28 | ||
29 | namespace gold | |
30 | { | |
31 | ||
61ba1cf9 ILT |
32 | class Output_file; |
33 | ||
835965e6 ILT |
34 | // A Stringpool is a pool of unique strings. It provides the |
35 | // following features: | |
36 | ||
37 | // Every string in the pool is unique. Thus, if you have two strings | |
38 | // in the Stringpool, you can compare them for equality by using | |
39 | // pointer comparison rather than string comparison. | |
40 | ||
41 | // There is a key associated with every string in the pool. If you | |
42 | // add strings to the Stringpool in the same order, then the key for | |
43 | // each string will always be the same for any run of the linker. | |
44 | // This is not true of the string pointers themselves, as they may | |
45 | // change due to address space randomization. Some parts of the | |
46 | // linker (e.g., the symbol table) use the key value instead of the | |
47 | // string pointer so that repeated runs of the linker will generate | |
48 | // precisely the same output. | |
49 | ||
cfd73a4e ILT |
50 | // When you add a string to a Stringpool, Stringpool will optionally |
51 | // make a copy of it. Thus there is no requirement to keep a copy | |
52 | // elsewhere. | |
bc56b3fb | 53 | |
835965e6 ILT |
54 | // A Stringpool can be turned into a string table, a sequential series |
55 | // of null terminated strings. The first string may optionally be a | |
56 | // single zero byte, as required for SHT_STRTAB sections. This | |
57 | // conversion is only permitted after all strings have been added to | |
58 | // the Stringpool. After doing this conversion, you can ask for the | |
59 | // offset of any string in the stringpool in the string table, and you | |
60 | // can write the resulting string table to an output file. | |
61 | ||
62 | // When a Stringpool is turned into a string table, then as an | |
63 | // optimization it will reuse string suffixes to avoid duplicating | |
64 | // strings. That is, given the strings "abc" and "bc", only the | |
65 | // string "abc" will be stored, and "bc" will be represented by an | |
66 | // offset into the middle of the string "abc". | |
67 | ||
68 | // Stringpools are implemented in terms of Stringpool_template, which | |
69 | // is generalized on the type of character used for the strings. Most | |
70 | // uses will want the Stringpool type which uses char. Other cases | |
71 | // are used for merging wide string constants. | |
72 | ||
b8e6aad9 ILT |
73 | template<typename Stringpool_char> |
74 | class Stringpool_template | |
14bfc3f5 ILT |
75 | { |
76 | public: | |
835965e6 ILT |
77 | // The type of a key into the stringpool. As described above, a key |
78 | // value will always be the same during any run of the linker. Zero | |
79 | // is never a valid key value. | |
f0641a0b ILT |
80 | typedef size_t Key; |
81 | ||
a8b2552e ILT |
82 | // Create a Stringpool. |
83 | Stringpool_template(); | |
14bfc3f5 | 84 | |
b8e6aad9 | 85 | ~Stringpool_template(); |
14bfc3f5 | 86 | |
9a0910c3 ILT |
87 | // Clear all the data from the stringpool. |
88 | void | |
89 | clear(); | |
90 | ||
6d013333 ILT |
91 | // Hint to the stringpool class that you intend to insert n additional |
92 | // elements. The stringpool class can use this info however it likes; | |
93 | // in practice it will resize its internal hashtables to make room. | |
94 | void | |
95 | reserve(unsigned int n); | |
96 | ||
a8b2552e ILT |
97 | // Indicate that we should not reserve offset 0 to hold the empty |
98 | // string when converting the stringpool to a string table. This | |
99 | // should not be called for a proper ELF SHT_STRTAB section. | |
100 | void | |
101 | set_no_zero_null() | |
102 | { this->zero_null_ = false; } | |
103 | ||
835965e6 | 104 | // Add the string S to the pool. This returns a canonical permanent |
cfd73a4e ILT |
105 | // pointer to the string in the pool. If COPY is true, the string |
106 | // is copied into permanent storage. If PKEY is not NULL, this sets | |
107 | // *PKEY to the key for the string. | |
b8e6aad9 | 108 | const Stringpool_char* |
cfd73a4e | 109 | add(const Stringpool_char* s, bool copy, Key* pkey); |
14bfc3f5 | 110 | |
c0873094 ILT |
111 | // Add string S of length LEN characters to the pool. If COPY is |
112 | // true, S need not be null terminated. | |
b8e6aad9 | 113 | const Stringpool_char* |
c0873094 | 114 | add_with_length(const Stringpool_char* s, size_t len, bool copy, Key* pkey); |
61ba1cf9 | 115 | |
835965e6 ILT |
116 | // If the string S is present in the pool, return the canonical |
117 | // string pointer. Otherwise, return NULL. If PKEY is not NULL, | |
118 | // set *PKEY to the key. | |
b8e6aad9 | 119 | const Stringpool_char* |
835965e6 | 120 | find(const Stringpool_char* s, Key* pkey) const; |
61ba1cf9 | 121 | |
835965e6 ILT |
122 | // Turn the stringpool into a string table: determine the offsets of |
123 | // all the strings. After this is called, no more strings may be | |
124 | // added to the stringpool. | |
61ba1cf9 ILT |
125 | void |
126 | set_string_offsets(); | |
127 | ||
835965e6 ILT |
128 | // Get the offset of the string S in the string table. This returns |
129 | // the offset in bytes, not in units of Stringpool_char. This may | |
130 | // only be called after set_string_offsets has been called. | |
8383303e | 131 | section_offset_type |
835965e6 | 132 | get_offset(const Stringpool_char* s) const; |
61ba1cf9 | 133 | |
835965e6 | 134 | // Get the offset of the string S in the string table. |
8383303e | 135 | section_offset_type |
b8e6aad9 | 136 | get_offset(const std::basic_string<Stringpool_char>& s) const |
c0873094 ILT |
137 | { return this->get_offset_with_length(s.c_str(), s.size()); } |
138 | ||
139 | // Get the offset of string S, with length LENGTH characters, in the | |
140 | // string table. | |
141 | section_offset_type | |
142 | get_offset_with_length(const Stringpool_char* s, size_t length) const; | |
61ba1cf9 | 143 | |
835965e6 ILT |
144 | // Get the size of the string table. This returns the number of |
145 | // bytes, not in units of Stringpool_char. | |
8383303e | 146 | section_size_type |
61ba1cf9 | 147 | get_strtab_size() const |
a3ad94ed ILT |
148 | { |
149 | gold_assert(this->strtab_size_ != 0); | |
150 | return this->strtab_size_; | |
151 | } | |
61ba1cf9 | 152 | |
835965e6 ILT |
153 | // Write the string table into the output file at the specified |
154 | // offset. | |
61ba1cf9 ILT |
155 | void |
156 | write(Output_file*, off_t offset); | |
14bfc3f5 | 157 | |
9a0910c3 ILT |
158 | // Write the string table into the specified buffer, of the |
159 | // specified size. buffer_size should be at least | |
160 | // get_strtab_size(). | |
161 | void | |
8383303e | 162 | write_to_buffer(unsigned char* buffer, section_size_type buffer_size); |
9a0910c3 | 163 | |
ad8f37d1 ILT |
164 | // Dump statistical information to stderr. |
165 | void | |
166 | print_stats(const char*) const; | |
167 | ||
14bfc3f5 | 168 | private: |
b8e6aad9 ILT |
169 | Stringpool_template(const Stringpool_template&); |
170 | Stringpool_template& operator=(const Stringpool_template&); | |
171 | ||
835965e6 | 172 | // Return the length of a string in units of Stringpool_char. |
b8e6aad9 ILT |
173 | static size_t |
174 | string_length(const Stringpool_char*); | |
14bfc3f5 | 175 | |
987cc251 ILT |
176 | // Return whether two strings are equal. |
177 | static bool | |
178 | string_equal(const Stringpool_char*, const Stringpool_char*); | |
179 | ||
180 | // Compute a hash code for a string. LENGTH is the length of the | |
181 | // string in characters. | |
182 | static size_t | |
183 | string_hash(const Stringpool_char*, size_t length); | |
184 | ||
61ba1cf9 ILT |
185 | // We store the actual data in a list of these buffers. |
186 | struct Stringdata | |
14bfc3f5 ILT |
187 | { |
188 | // Length of data in buffer. | |
189 | size_t len; | |
190 | // Allocated size of buffer. | |
191 | size_t alc; | |
f0641a0b ILT |
192 | // Buffer index. |
193 | unsigned int index; | |
14bfc3f5 ILT |
194 | // Buffer. |
195 | char data[1]; | |
196 | }; | |
197 | ||
61ba1cf9 | 198 | // Copy a string into the buffers, returning a canonical string. |
b8e6aad9 | 199 | const Stringpool_char* |
987cc251 ILT |
200 | add_string(const Stringpool_char*, size_t, Key*); |
201 | ||
202 | // Return whether s1 is a suffix of s2. | |
203 | static bool | |
204 | is_suffix(const Stringpool_char* s1, size_t len1, | |
205 | const Stringpool_char* s2, size_t len2); | |
206 | ||
207 | // The hash table key includes the string, the length of the string, | |
208 | // and the hash code for the string. We put the hash code | |
209 | // explicitly into the key so that we can do a find()/insert() | |
210 | // sequence without having to recompute the hash. Computing the | |
211 | // hash code is a significant user of CPU time in the linker. | |
212 | struct Hashkey | |
213 | { | |
214 | const Stringpool_char* string; | |
215 | // Length is in characters, not bytes. | |
216 | size_t length; | |
217 | size_t hash_code; | |
218 | ||
219 | // This goes in an STL container, so we need a default | |
220 | // constructor. | |
221 | Hashkey() | |
222 | : string(NULL), length(0), hash_code(0) | |
223 | { } | |
224 | ||
225 | // Note that these constructors are relatively expensive, because | |
226 | // they compute the hash code. | |
c0873094 | 227 | explicit Hashkey(const Stringpool_char* s) |
987cc251 ILT |
228 | : string(s), length(string_length(s)), hash_code(string_hash(s, length)) |
229 | { } | |
230 | ||
231 | Hashkey(const Stringpool_char* s, size_t len) | |
232 | : string(s), length(len), hash_code(string_hash(s, len)) | |
233 | { } | |
234 | }; | |
14bfc3f5 | 235 | |
987cc251 ILT |
236 | // Hash function. This is trivial, since we have already computed |
237 | // the hash. | |
14bfc3f5 ILT |
238 | struct Stringpool_hash |
239 | { | |
240 | size_t | |
987cc251 ILT |
241 | operator()(const Hashkey& hk) const |
242 | { return hk.hash_code; } | |
14bfc3f5 ILT |
243 | }; |
244 | ||
835965e6 | 245 | // Equality comparison function for hash table. |
14bfc3f5 ILT |
246 | struct Stringpool_eq |
247 | { | |
248 | bool | |
987cc251 | 249 | operator()(const Hashkey&, const Hashkey&) const; |
14bfc3f5 ILT |
250 | }; |
251 | ||
987cc251 ILT |
252 | // The hash table is a map from strings to a pair of Key and string |
253 | // table offsets. We only use the offsets if we turn this into an | |
254 | // string table section. | |
f0641a0b | 255 | |
8383303e | 256 | typedef std::pair<Key, section_offset_type> Hashval; |
61ba1cf9 | 257 | |
987cc251 | 258 | typedef Unordered_map<Hashkey, Hashval, Stringpool_hash, |
61ba1cf9 | 259 | Stringpool_eq> String_set_type; |
274e99f9 | 260 | |
987cc251 ILT |
261 | // Comparison routine used when sorting into a string table. |
262 | ||
263 | typedef typename String_set_type::iterator Stringpool_sort_info; | |
61ba1cf9 ILT |
264 | |
265 | struct Stringpool_sort_comparison | |
266 | { | |
267 | bool | |
614f30a2 | 268 | operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const; |
61ba1cf9 ILT |
269 | }; |
270 | ||
f0641a0b ILT |
271 | // List of Stringdata structures. |
272 | typedef std::list<Stringdata*> Stringdata_list; | |
273 | ||
274 | // Mapping from const char* to namepool entry. | |
14bfc3f5 | 275 | String_set_type string_set_; |
f0641a0b ILT |
276 | // List of buffers. |
277 | Stringdata_list strings_; | |
835965e6 | 278 | // Size of string table. |
8383303e | 279 | section_size_type strtab_size_; |
f0641a0b ILT |
280 | // Next Stringdata index. |
281 | unsigned int next_index_; | |
cfd73a4e ILT |
282 | // Next key value for a string we don't copy. |
283 | int next_uncopied_key_; | |
b8e6aad9 ILT |
284 | // Whether to reserve offset 0 to hold the null string. |
285 | bool zero_null_; | |
14bfc3f5 ILT |
286 | }; |
287 | ||
b8e6aad9 ILT |
288 | // The most common type of Stringpool. |
289 | typedef Stringpool_template<char> Stringpool; | |
290 | ||
14bfc3f5 ILT |
291 | } // End namespace gold. |
292 | ||
293 | #endif // !defined(GOLD_STRINGPOOL_H) |