Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // stringpool.cc -- a string pool for gold |
2 | ||
3 | #include "gold.h" | |
4 | ||
14bfc3f5 | 5 | #include <cstring> |
61ba1cf9 ILT |
6 | #include <algorithm> |
7 | #include <vector> | |
14bfc3f5 | 8 | |
61ba1cf9 | 9 | #include "output.h" |
377caf49 | 10 | #include "parameters.h" |
14bfc3f5 ILT |
11 | #include "stringpool.h" |
12 | ||
13 | namespace gold | |
14 | { | |
15 | ||
b8e6aad9 | 16 | template<typename Stringpool_char> |
a8b2552e | 17 | Stringpool_template<Stringpool_char>::Stringpool_template() |
b8e6aad9 | 18 | : string_set_(), strings_(), strtab_size_(0), next_index_(1), |
a8b2552e | 19 | zero_null_(true) |
14bfc3f5 ILT |
20 | { |
21 | } | |
22 | ||
b8e6aad9 ILT |
23 | template<typename Stringpool_char> |
24 | Stringpool_template<Stringpool_char>::~Stringpool_template() | |
14bfc3f5 | 25 | { |
b8e6aad9 | 26 | for (typename std::list<Stringdata*>::iterator p = this->strings_.begin(); |
14bfc3f5 ILT |
27 | p != this->strings_.end(); |
28 | ++p) | |
29 | delete[] reinterpret_cast<char*>(*p); | |
30 | } | |
31 | ||
b8e6aad9 ILT |
32 | // Return the length of a string of arbitrary character type. |
33 | ||
34 | template<typename Stringpool_char> | |
35 | size_t | |
36 | Stringpool_template<Stringpool_char>::string_length(const Stringpool_char* p) | |
37 | { | |
38 | size_t len = 0; | |
39 | for (; *p != 0; ++p) | |
40 | ++len; | |
41 | return len; | |
42 | } | |
43 | ||
44 | // Specialize string_length for char. Maybe we could just use | |
45 | // std::char_traits<>::length? | |
46 | ||
47 | template<> | |
48 | inline size_t | |
49 | Stringpool_template<char>::string_length(const char* p) | |
50 | { | |
51 | return strlen(p); | |
52 | } | |
53 | ||
54 | // Equality comparison function. | |
55 | ||
56 | template<typename Stringpool_char> | |
57 | bool | |
58 | Stringpool_template<Stringpool_char>::Stringpool_eq::operator()( | |
59 | const Stringpool_char* s1, | |
60 | const Stringpool_char* s2) const | |
61 | { | |
62 | while (*s1 != 0) | |
63 | if (*s1++ != *s2++) | |
64 | return false; | |
65 | return *s2 == 0; | |
66 | } | |
67 | ||
68 | // Specialize equality comparison for char. | |
69 | ||
70 | template<> | |
71 | bool | |
72 | Stringpool_template<char>::Stringpool_eq::operator()(const char* s1, | |
73 | const char* s2) const | |
74 | { | |
75 | return strcmp(s1, s2) == 0; | |
76 | } | |
77 | ||
14bfc3f5 ILT |
78 | // Hash function. |
79 | ||
b8e6aad9 | 80 | template<typename Stringpool_char> |
14bfc3f5 | 81 | size_t |
b8e6aad9 ILT |
82 | Stringpool_template<Stringpool_char>::Stringpool_hash::operator()( |
83 | const Stringpool_char* s) const | |
14bfc3f5 ILT |
84 | { |
85 | // Fowler/Noll/Vo (FNV) hash (type FNV-1a). | |
86 | if (sizeof(size_t) == 8) | |
87 | { | |
b3168e9d | 88 | size_t result = static_cast<size_t>(14695981039346656037ULL); |
b8e6aad9 | 89 | while (*s != 0) |
14bfc3f5 | 90 | { |
b8e6aad9 ILT |
91 | const char* p = reinterpret_cast<const char*>(s); |
92 | for (size_t i = 0; i < sizeof(Stringpool_char); ++i) | |
93 | { | |
94 | result &= (size_t) *p++; | |
95 | result *= 1099511628211ULL; | |
96 | } | |
97 | ++s; | |
14bfc3f5 ILT |
98 | } |
99 | return result; | |
100 | } | |
101 | else | |
102 | { | |
103 | size_t result = 2166136261UL; | |
b8e6aad9 | 104 | while (*s != 0) |
14bfc3f5 | 105 | { |
b8e6aad9 ILT |
106 | const char* p = reinterpret_cast<const char*>(s); |
107 | for (size_t i = 0; i < sizeof(Stringpool_char); ++i) | |
108 | { | |
109 | result ^= (size_t) *p++; | |
110 | result *= 16777619UL; | |
111 | } | |
112 | ++s; | |
14bfc3f5 ILT |
113 | } |
114 | return result; | |
115 | } | |
116 | } | |
117 | ||
118 | // Add a string to the list of canonical strings. Return a pointer to | |
f0641a0b | 119 | // the canonical string. If PKEY is not NULL, set *PKEY to the key. |
14bfc3f5 | 120 | |
b8e6aad9 ILT |
121 | template<typename Stringpool_char> |
122 | const Stringpool_char* | |
123 | Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s, | |
124 | Key* pkey) | |
14bfc3f5 | 125 | { |
a3ad94ed ILT |
126 | // We are in trouble if we've already computed the string offsets. |
127 | gold_assert(this->strtab_size_ == 0); | |
128 | ||
f0641a0b | 129 | // The size we allocate for a new Stringdata. |
14bfc3f5 | 130 | const size_t buffer_size = 1000; |
f0641a0b ILT |
131 | // The amount we multiply the Stringdata index when calculating the |
132 | // key. | |
133 | const size_t key_mult = 1024; | |
a3ad94ed | 134 | gold_assert(key_mult >= buffer_size); |
f0641a0b | 135 | |
b8e6aad9 | 136 | size_t len = (string_length(s) + 1) * sizeof(Stringpool_char); |
14bfc3f5 ILT |
137 | |
138 | size_t alc; | |
139 | bool front = true; | |
b8e6aad9 | 140 | if (len > buffer_size) |
14bfc3f5 | 141 | { |
61ba1cf9 | 142 | alc = sizeof(Stringdata) + len; |
14bfc3f5 ILT |
143 | front = false; |
144 | } | |
145 | else if (this->strings_.empty()) | |
61ba1cf9 | 146 | alc = sizeof(Stringdata) + buffer_size; |
14bfc3f5 ILT |
147 | else |
148 | { | |
61ba1cf9 | 149 | Stringdata *psd = this->strings_.front(); |
b8e6aad9 | 150 | if (len > psd->alc - psd->len) |
61ba1cf9 | 151 | alc = sizeof(Stringdata) + buffer_size; |
14bfc3f5 ILT |
152 | else |
153 | { | |
154 | char* ret = psd->data + psd->len; | |
b8e6aad9 | 155 | memcpy(ret, s, len); |
f0641a0b ILT |
156 | |
157 | if (pkey != NULL) | |
158 | *pkey = psd->index * key_mult + psd->len; | |
159 | ||
b8e6aad9 | 160 | psd->len += len; |
f0641a0b | 161 | |
b8e6aad9 | 162 | return reinterpret_cast<const Stringpool_char*>(ret); |
14bfc3f5 ILT |
163 | } |
164 | } | |
165 | ||
61ba1cf9 ILT |
166 | Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]); |
167 | psd->alc = alc - sizeof(Stringdata); | |
b8e6aad9 ILT |
168 | memcpy(psd->data, s, len); |
169 | psd->len = len; | |
f0641a0b ILT |
170 | psd->index = this->next_index_; |
171 | ++this->next_index_; | |
172 | ||
173 | if (pkey != NULL) | |
174 | *pkey = psd->index * key_mult; | |
175 | ||
14bfc3f5 ILT |
176 | if (front) |
177 | this->strings_.push_front(psd); | |
178 | else | |
179 | this->strings_.push_back(psd); | |
f0641a0b | 180 | |
b8e6aad9 | 181 | return reinterpret_cast<const Stringpool_char*>(psd->data); |
14bfc3f5 ILT |
182 | } |
183 | ||
184 | // Add a string to a string pool. | |
185 | ||
b8e6aad9 ILT |
186 | template<typename Stringpool_char> |
187 | const Stringpool_char* | |
188 | Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, Key* pkey) | |
14bfc3f5 ILT |
189 | { |
190 | // FIXME: This will look up the entry twice in the hash table. The | |
191 | // problem is that we can't insert S before we canonicalize it. I | |
a2fb1b05 | 192 | // don't think there is a way to handle this correctly with |
61ba1cf9 | 193 | // unordered_map, so this should be replaced with custom code to do |
14bfc3f5 ILT |
194 | // what we need, which is to return the empty slot. |
195 | ||
b8e6aad9 | 196 | typename String_set_type::const_iterator p = this->string_set_.find(s); |
14bfc3f5 | 197 | if (p != this->string_set_.end()) |
f0641a0b ILT |
198 | { |
199 | if (pkey != NULL) | |
200 | *pkey = p->second.first; | |
201 | return p->first; | |
202 | } | |
14bfc3f5 | 203 | |
f0641a0b | 204 | Key k; |
b8e6aad9 | 205 | const Stringpool_char* ret = this->add_string(s, &k); |
f0641a0b ILT |
206 | |
207 | const off_t ozero = 0; | |
b8e6aad9 ILT |
208 | std::pair<const Stringpool_char*, Val> element(ret, |
209 | std::make_pair(k, ozero)); | |
210 | std::pair<typename String_set_type::iterator, bool> ins = | |
f0641a0b | 211 | this->string_set_.insert(element); |
a3ad94ed | 212 | gold_assert(ins.second); |
f0641a0b ILT |
213 | |
214 | if (pkey != NULL) | |
215 | *pkey = k; | |
216 | ||
14bfc3f5 ILT |
217 | return ret; |
218 | } | |
219 | ||
220 | // Add a prefix of a string to a string pool. | |
221 | ||
b8e6aad9 ILT |
222 | template<typename Stringpool_char> |
223 | const Stringpool_char* | |
224 | Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, size_t len, | |
225 | Key* pkey) | |
14bfc3f5 ILT |
226 | { |
227 | // FIXME: This implementation should be rewritten when we rewrite | |
228 | // the hash table to avoid copying. | |
b8e6aad9 | 229 | std::basic_string<Stringpool_char> st(s, len); |
f0641a0b | 230 | return this->add(st, pkey); |
14bfc3f5 ILT |
231 | } |
232 | ||
b8e6aad9 ILT |
233 | template<typename Stringpool_char> |
234 | const Stringpool_char* | |
235 | Stringpool_template<Stringpool_char>::find(const Stringpool_char* s, | |
236 | Key* pkey) const | |
61ba1cf9 | 237 | { |
b8e6aad9 | 238 | typename String_set_type::const_iterator p = this->string_set_.find(s); |
61ba1cf9 ILT |
239 | if (p == this->string_set_.end()) |
240 | return NULL; | |
f0641a0b ILT |
241 | |
242 | if (pkey != NULL) | |
243 | *pkey = p->second.first; | |
244 | ||
61ba1cf9 ILT |
245 | return p->first; |
246 | } | |
247 | ||
248 | // Comparison routine used when sorting into an ELF strtab. We want | |
249 | // to sort this so that when one string is a suffix of another, we | |
250 | // always see the shorter string immediately after the longer string. | |
251 | // For example, we want to see these strings in this order: | |
252 | // abcd | |
253 | // cd | |
254 | // d | |
255 | // When strings are not suffixes, we don't care what order they are | |
256 | // in, but we need to ensure that suffixes wind up next to each other. | |
257 | // So we do a reversed lexicographic sort on the reversed string. | |
258 | ||
b8e6aad9 | 259 | template<typename Stringpool_char> |
61ba1cf9 | 260 | bool |
b8e6aad9 | 261 | Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()( |
614f30a2 ILT |
262 | const Stringpool_sort_info& sort_info1, |
263 | const Stringpool_sort_info& sort_info2) const | |
61ba1cf9 | 264 | { |
614f30a2 ILT |
265 | const Stringpool_char* s1 = sort_info1.it->first; |
266 | const Stringpool_char* s2 = sort_info2.it->first; | |
267 | const size_t len1 = sort_info1.string_length; | |
268 | const size_t len2 = sort_info2.string_length; | |
269 | const size_t minlen = len1 < len2 ? len1 : len2; | |
b8e6aad9 ILT |
270 | const Stringpool_char* p1 = s1 + len1 - 1; |
271 | const Stringpool_char* p2 = s2 + len2 - 1; | |
272 | for (size_t i = minlen; i > 0; --i, --p1, --p2) | |
61ba1cf9 ILT |
273 | { |
274 | if (*p1 != *p2) | |
275 | return *p1 > *p2; | |
276 | } | |
277 | return len1 > len2; | |
278 | } | |
279 | ||
280 | // Return whether s1 is a suffix of s2. | |
281 | ||
b8e6aad9 | 282 | template<typename Stringpool_char> |
61ba1cf9 | 283 | bool |
b8e6aad9 | 284 | Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1, |
614f30a2 ILT |
285 | size_t len1, |
286 | const Stringpool_char* s2, | |
287 | size_t len2) | |
61ba1cf9 | 288 | { |
61ba1cf9 ILT |
289 | if (len1 > len2) |
290 | return false; | |
b8e6aad9 | 291 | return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0; |
61ba1cf9 ILT |
292 | } |
293 | ||
294 | // Turn the stringpool into an ELF strtab: determine the offsets of | |
295 | // each string in the table. | |
296 | ||
b8e6aad9 | 297 | template<typename Stringpool_char> |
61ba1cf9 | 298 | void |
b8e6aad9 | 299 | Stringpool_template<Stringpool_char>::set_string_offsets() |
61ba1cf9 | 300 | { |
a3ad94ed ILT |
301 | if (this->strtab_size_ != 0) |
302 | { | |
303 | // We've already computed the offsets. | |
304 | return; | |
305 | } | |
306 | ||
b8e6aad9 ILT |
307 | const size_t charsize = sizeof(Stringpool_char); |
308 | ||
309 | // Offset 0 may be reserved for the empty string. | |
310 | off_t offset = this->zero_null_ ? charsize : 0; | |
614f30a2 | 311 | |
377caf49 ILT |
312 | // Sorting to find suffixes can take over 25% of the total CPU time |
313 | // used by the linker. Since it's merely an optimization to reduce | |
314 | // the strtab size, and gives a relatively small benefit (it's | |
315 | // typically rare for a symbol to be a suffix of another), we only | |
316 | // take the time to sort when the user asks for heavy optimization. | |
317 | if (parameters->optimization_level() < 2) | |
61ba1cf9 | 318 | { |
377caf49 ILT |
319 | for (typename String_set_type::iterator curr = this->string_set_.begin(); |
320 | curr != this->string_set_.end(); | |
321 | curr++) | |
322 | { | |
323 | if (this->zero_null_ && curr->first[0] == 0) | |
324 | curr->second.second = 0; | |
325 | else | |
326 | { | |
327 | curr->second.second = offset; | |
328 | offset += (string_length(curr->first) + 1) * charsize; | |
329 | } | |
330 | } | |
331 | } | |
332 | else | |
333 | { | |
334 | size_t count = this->string_set_.size(); | |
335 | ||
336 | std::vector<Stringpool_sort_info> v; | |
337 | v.reserve(count); | |
338 | ||
339 | for (typename String_set_type::iterator p = this->string_set_.begin(); | |
340 | p != this->string_set_.end(); | |
341 | ++p) | |
342 | v.push_back(Stringpool_sort_info(p, string_length(p->first))); | |
343 | ||
344 | std::sort(v.begin(), v.end(), Stringpool_sort_comparison()); | |
345 | ||
346 | for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(), | |
347 | curr = v.begin(); | |
348 | curr != v.end(); | |
349 | last = curr++) | |
350 | { | |
351 | if (this->zero_null_ && curr->it->first[0] == 0) | |
352 | curr->it->second.second = 0; | |
353 | else if (last != v.end() | |
354 | && is_suffix(curr->it->first, curr->string_length, | |
355 | last->it->first, last->string_length)) | |
356 | curr->it->second.second = (last->it->second.second | |
357 | + ((last->string_length | |
358 | - curr->string_length) | |
359 | * charsize)); | |
360 | else | |
361 | { | |
362 | curr->it->second.second = offset; | |
363 | offset += (curr->string_length + 1) * charsize; | |
364 | } | |
365 | } | |
61ba1cf9 ILT |
366 | } |
367 | ||
368 | this->strtab_size_ = offset; | |
369 | } | |
370 | ||
371 | // Get the offset of a string in the ELF strtab. The string must | |
372 | // exist. | |
373 | ||
b8e6aad9 | 374 | template<typename Stringpool_char> |
61ba1cf9 | 375 | off_t |
b8e6aad9 ILT |
376 | Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s) |
377 | const | |
61ba1cf9 | 378 | { |
a3ad94ed | 379 | gold_assert(this->strtab_size_ != 0); |
b8e6aad9 | 380 | typename String_set_type::const_iterator p = this->string_set_.find(s); |
61ba1cf9 | 381 | if (p != this->string_set_.end()) |
f0641a0b | 382 | return p->second.second; |
a3ad94ed | 383 | gold_unreachable(); |
61ba1cf9 ILT |
384 | } |
385 | ||
386 | // Write the ELF strtab into the output file at the specified offset. | |
387 | ||
b8e6aad9 | 388 | template<typename Stringpool_char> |
61ba1cf9 | 389 | void |
b8e6aad9 | 390 | Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset) |
61ba1cf9 | 391 | { |
a3ad94ed | 392 | gold_assert(this->strtab_size_ != 0); |
61ba1cf9 ILT |
393 | unsigned char* viewu = of->get_output_view(offset, this->strtab_size_); |
394 | char* view = reinterpret_cast<char*>(viewu); | |
b8e6aad9 ILT |
395 | if (this->zero_null_) |
396 | view[0] = '\0'; | |
397 | for (typename String_set_type::const_iterator p = this->string_set_.begin(); | |
61ba1cf9 ILT |
398 | p != this->string_set_.end(); |
399 | ++p) | |
b8e6aad9 ILT |
400 | memcpy(view + p->second.second, p->first, |
401 | (string_length(p->first) + 1) * sizeof(Stringpool_char)); | |
61ba1cf9 ILT |
402 | of->write_output_view(offset, this->strtab_size_, viewu); |
403 | } | |
404 | ||
b8e6aad9 ILT |
405 | // Instantiate the templates we need. |
406 | ||
407 | template | |
408 | class Stringpool_template<char>; | |
409 | ||
410 | template | |
411 | class Stringpool_template<uint16_t>; | |
412 | ||
413 | template | |
414 | class Stringpool_template<uint32_t>; | |
415 | ||
14bfc3f5 | 416 | } // End namespace gold. |