]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/stringpool.cc
Use a simpler hash function.
[thirdparty/binutils-gdb.git] / gold / stringpool.cc
1 // stringpool.cc -- a string pool for gold
2
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
23 #include "gold.h"
24
25 #include <cstring>
26 #include <algorithm>
27 #include <vector>
28
29 #include "output.h"
30 #include "parameters.h"
31 #include "stringpool.h"
32
33 namespace gold
34 {
35
36 template<typename Stringpool_char>
37 Stringpool_template<Stringpool_char>::Stringpool_template()
38 : string_set_(), key_to_offset_(), strings_(), strtab_size_(0),
39 zero_null_(true)
40 {
41 }
42
43 template<typename Stringpool_char>
44 void
45 Stringpool_template<Stringpool_char>::clear()
46 {
47 for (typename std::list<Stringdata*>::iterator p = this->strings_.begin();
48 p != this->strings_.end();
49 ++p)
50 delete[] reinterpret_cast<char*>(*p);
51 this->strings_.clear();
52 this->key_to_offset_.clear();
53 this->string_set_.clear();
54 }
55
56 template<typename Stringpool_char>
57 Stringpool_template<Stringpool_char>::~Stringpool_template()
58 {
59 this->clear();
60 }
61
62 // Resize the internal hashtable with the expectation we'll get n new
63 // elements. Note that the hashtable constructor takes a "number of
64 // buckets you'd like," rather than "number of elements you'd like,"
65 // but that's the best we can do.
66
67 template<typename Stringpool_char>
68 void
69 Stringpool_template<Stringpool_char>::reserve(unsigned int n)
70 {
71 this->key_to_offset_.reserve(n);
72
73 #if defined(HAVE_TR1_UNORDERED_MAP)
74 // rehash() implementation is broken in gcc 4.0.3's stl
75 //this->string_set_.rehash(this->string_set_.size() + n);
76 //return;
77 #elif defined(HAVE_EXT_HASH_MAP)
78 this->string_set_.resize(this->string_set_.size() + n);
79 return;
80 #endif
81
82 // This is the generic "reserve" code, if no #ifdef above triggers.
83 String_set_type new_string_set(this->string_set_.size() + n);
84 new_string_set.insert(this->string_set_.begin(), this->string_set_.end());
85 this->string_set_.swap(new_string_set);
86 }
87
88 // Return the length of a string of arbitrary character type.
89
90 template<typename Stringpool_char>
91 size_t
92 Stringpool_template<Stringpool_char>::string_length(const Stringpool_char* p)
93 {
94 size_t len = 0;
95 for (; *p != 0; ++p)
96 ++len;
97 return len;
98 }
99
100 // Specialize string_length for char. Maybe we could just use
101 // std::char_traits<>::length?
102
103 template<>
104 inline size_t
105 Stringpool_template<char>::string_length(const char* p)
106 {
107 return strlen(p);
108 }
109
110 // Compare two strings of arbitrary character type for equality.
111
112 template<typename Stringpool_char>
113 bool
114 Stringpool_template<Stringpool_char>::string_equal(const Stringpool_char* s1,
115 const Stringpool_char* s2)
116 {
117 while (*s1 != 0)
118 if (*s1++ != *s2++)
119 return false;
120 return *s2 == 0;
121 }
122
123 // Specialize string_equal for char.
124
125 template<>
126 inline bool
127 Stringpool_template<char>::string_equal(const char* s1, const char* s2)
128 {
129 return strcmp(s1, s2) == 0;
130 }
131
132 // Equality comparison function for the hash table.
133
134 template<typename Stringpool_char>
135 inline bool
136 Stringpool_template<Stringpool_char>::Stringpool_eq::operator()(
137 const Hashkey& h1,
138 const Hashkey& h2) const
139 {
140 return (h1.hash_code == h2.hash_code
141 && h1.length == h2.length
142 && (h1.string == h2.string
143 || memcmp(h1.string, h2.string,
144 h1.length * sizeof(Stringpool_char)) == 0));
145 }
146
147 // Hash function. The length is in characters, not bytes.
148
149 template<typename Stringpool_char>
150 size_t
151 Stringpool_template<Stringpool_char>::string_hash(const Stringpool_char* s,
152 size_t length)
153 {
154 // This is the hash function used by the dynamic linker for
155 // DT_GNU_HASH entries. I compared this to a Fowler/Noll/Vo hash
156 // for a C++ program with 385,775 global symbols. This hash
157 // function was very slightly worse. However, it is much faster to
158 // compute. Overall wall clock time was a win.
159 const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
160 size_t h = 5381;
161 for (size_t i = 0; i < length * sizeof(Stringpool_char); ++i)
162 h = h * 33 + *p++;
163 return h;
164 }
165
166 // Add the string S to the list of canonical strings. Return a
167 // pointer to the canonical string. If PKEY is not NULL, set *PKEY to
168 // the key. LENGTH is the length of S in characters. Note that S may
169 // not be NUL terminated.
170
171 template<typename Stringpool_char>
172 const Stringpool_char*
173 Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
174 size_t len)
175 {
176 // We are in trouble if we've already computed the string offsets.
177 gold_assert(this->strtab_size_ == 0);
178
179 // The size we allocate for a new Stringdata.
180 const size_t buffer_size = 1000;
181 // The amount we multiply the Stringdata index when calculating the
182 // key.
183 const size_t key_mult = 1024;
184 gold_assert(key_mult >= buffer_size);
185
186 // Convert len to the number of bytes we need to allocate, including
187 // the null character.
188 len = (len + 1) * sizeof(Stringpool_char);
189
190 size_t alc;
191 bool front = true;
192 if (len > buffer_size)
193 {
194 alc = sizeof(Stringdata) + len;
195 front = false;
196 }
197 else if (this->strings_.empty())
198 alc = sizeof(Stringdata) + buffer_size;
199 else
200 {
201 Stringdata *psd = this->strings_.front();
202 if (len > psd->alc - psd->len)
203 alc = sizeof(Stringdata) + buffer_size;
204 else
205 {
206 char* ret = psd->data + psd->len;
207 memcpy(ret, s, len - sizeof(Stringpool_char));
208 memset(ret + len - sizeof(Stringpool_char), 0,
209 sizeof(Stringpool_char));
210
211 psd->len += len;
212
213 return reinterpret_cast<const Stringpool_char*>(ret);
214 }
215 }
216
217 Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]);
218 psd->alc = alc - sizeof(Stringdata);
219 memcpy(psd->data, s, len - sizeof(Stringpool_char));
220 memset(psd->data + len - sizeof(Stringpool_char), 0,
221 sizeof(Stringpool_char));
222 psd->len = len;
223
224 if (front)
225 this->strings_.push_front(psd);
226 else
227 this->strings_.push_back(psd);
228
229 return reinterpret_cast<const Stringpool_char*>(psd->data);
230 }
231
232 // Add a string to a string pool.
233
234 template<typename Stringpool_char>
235 const Stringpool_char*
236 Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, bool copy,
237 Key* pkey)
238 {
239 return this->add_with_length(s, string_length(s), copy, pkey);
240 }
241
242 template<typename Stringpool_char>
243 const Stringpool_char*
244 Stringpool_template<Stringpool_char>::add_with_length(const Stringpool_char* s,
245 size_t length,
246 bool copy,
247 Key* pkey)
248 {
249 typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
250
251 const Key k = this->key_to_offset_.size();
252
253 if (!copy)
254 {
255 // When we don't need to copy the string, we can call insert
256 // directly.
257
258 std::pair<Hashkey, Hashval> element(Hashkey(s, length), k);
259
260 Insert_type ins = this->string_set_.insert(element);
261
262 typename String_set_type::const_iterator p = ins.first;
263
264 if (ins.second)
265 {
266 // We just added the string. The key value has now been
267 // used.
268 this->key_to_offset_.push_back(0);
269 }
270 else
271 {
272 gold_assert(k != p->second);
273 }
274
275 if (pkey != NULL)
276 *pkey = p->second;
277 return p->first.string;
278 }
279
280 // When we have to copy the string, we look it up twice in the hash
281 // table. The problem is that we can't insert S before we
282 // canonicalize it by copying it into the canonical list. The hash
283 // code will only be computed once.
284
285 Hashkey hk(s, length);
286 typename String_set_type::const_iterator p = this->string_set_.find(hk);
287 if (p != this->string_set_.end())
288 {
289 if (pkey != NULL)
290 *pkey = p->second;
291 return p->first.string;
292 }
293
294 this->key_to_offset_.push_back(0);
295
296 hk.string = this->add_string(s, length);
297 // The contents of the string stay the same, so we don't need to
298 // adjust hk.hash_code or hk.length.
299
300 std::pair<Hashkey, Hashval> element(hk, k);
301
302 Insert_type ins = this->string_set_.insert(element);
303 gold_assert(ins.second);
304
305 if (pkey != NULL)
306 *pkey = k;
307 return hk.string;
308 }
309
310 template<typename Stringpool_char>
311 const Stringpool_char*
312 Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
313 Key* pkey) const
314 {
315 Hashkey hk(s);
316 typename String_set_type::const_iterator p = this->string_set_.find(hk);
317 if (p == this->string_set_.end())
318 return NULL;
319
320 if (pkey != NULL)
321 *pkey = p->second;
322
323 return p->first.string;
324 }
325
326 // Comparison routine used when sorting into an ELF strtab. We want
327 // to sort this so that when one string is a suffix of another, we
328 // always see the shorter string immediately after the longer string.
329 // For example, we want to see these strings in this order:
330 // abcd
331 // cd
332 // d
333 // When strings are not suffixes, we don't care what order they are
334 // in, but we need to ensure that suffixes wind up next to each other.
335 // So we do a reversed lexicographic sort on the reversed string.
336
337 template<typename Stringpool_char>
338 bool
339 Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
340 const Stringpool_sort_info& sort_info1,
341 const Stringpool_sort_info& sort_info2) const
342 {
343 const Hashkey& h1(sort_info1->first);
344 const Hashkey& h2(sort_info2->first);
345 const Stringpool_char* s1 = h1.string;
346 const Stringpool_char* s2 = h2.string;
347 const size_t len1 = h1.length;
348 const size_t len2 = h2.length;
349 const size_t minlen = len1 < len2 ? len1 : len2;
350 const Stringpool_char* p1 = s1 + len1 - 1;
351 const Stringpool_char* p2 = s2 + len2 - 1;
352 for (size_t i = minlen; i > 0; --i, --p1, --p2)
353 {
354 if (*p1 != *p2)
355 return *p1 > *p2;
356 }
357 return len1 > len2;
358 }
359
360 // Return whether s1 is a suffix of s2.
361
362 template<typename Stringpool_char>
363 bool
364 Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
365 size_t len1,
366 const Stringpool_char* s2,
367 size_t len2)
368 {
369 if (len1 > len2)
370 return false;
371 return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
372 }
373
374 // Turn the stringpool into an ELF strtab: determine the offsets of
375 // each string in the table.
376
377 template<typename Stringpool_char>
378 void
379 Stringpool_template<Stringpool_char>::set_string_offsets()
380 {
381 if (this->strtab_size_ != 0)
382 {
383 // We've already computed the offsets.
384 return;
385 }
386
387 const size_t charsize = sizeof(Stringpool_char);
388
389 // Offset 0 may be reserved for the empty string.
390 section_offset_type offset = this->zero_null_ ? charsize : 0;
391
392 // Sorting to find suffixes can take over 25% of the total CPU time
393 // used by the linker. Since it's merely an optimization to reduce
394 // the strtab size, and gives a relatively small benefit (it's
395 // typically rare for a symbol to be a suffix of another), we only
396 // take the time to sort when the user asks for heavy optimization.
397 if (parameters->optimization_level() < 2)
398 {
399 for (typename String_set_type::iterator curr = this->string_set_.begin();
400 curr != this->string_set_.end();
401 curr++)
402 {
403 section_offset_type* poff = &this->key_to_offset_[curr->second];
404 if (this->zero_null_ && curr->first.string[0] == 0)
405 *poff = 0;
406 else
407 {
408 *poff = offset;
409 offset += (curr->first.length + 1) * charsize;
410 }
411 }
412 }
413 else
414 {
415 size_t count = this->string_set_.size();
416
417 std::vector<Stringpool_sort_info> v;
418 v.reserve(count);
419
420 for (typename String_set_type::iterator p = this->string_set_.begin();
421 p != this->string_set_.end();
422 ++p)
423 v.push_back(Stringpool_sort_info(p));
424
425 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
426
427 section_offset_type last_offset = -1;
428 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
429 curr = v.begin();
430 curr != v.end();
431 last = curr++)
432 {
433 section_offset_type this_offset;
434 if (this->zero_null_ && (*curr)->first.string[0] == 0)
435 this_offset = 0;
436 else if (last != v.end()
437 && is_suffix((*curr)->first.string,
438 (*curr)->first.length,
439 (*last)->first.string,
440 (*last)->first.length))
441 this_offset = (last_offset
442 + (((*last)->first.length - (*curr)->first.length)
443 * charsize));
444 else
445 {
446 this_offset = offset;
447 offset += ((*curr)->first.length + 1) * charsize;
448 }
449 this->key_to_offset_[(*curr)->second] = this_offset;
450 last_offset = this_offset;
451 }
452 }
453
454 this->strtab_size_ = offset;
455 }
456
457 // Get the offset of a string in the ELF strtab. The string must
458 // exist.
459
460 template<typename Stringpool_char>
461 section_offset_type
462 Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
463 const
464 {
465 return this->get_offset_with_length(s, string_length(s));
466 }
467
468 template<typename Stringpool_char>
469 section_offset_type
470 Stringpool_template<Stringpool_char>::get_offset_with_length(
471 const Stringpool_char* s,
472 size_t length) const
473 {
474 gold_assert(this->strtab_size_ != 0);
475 Hashkey hk(s, length);
476 typename String_set_type::const_iterator p = this->string_set_.find(hk);
477 if (p != this->string_set_.end())
478 return this->key_to_offset_[p->second];
479 gold_unreachable();
480 }
481
482 // Write the ELF strtab into the buffer.
483
484 template<typename Stringpool_char>
485 void
486 Stringpool_template<Stringpool_char>::write_to_buffer(
487 unsigned char* buffer,
488 section_size_type bufsize)
489 {
490 gold_assert(this->strtab_size_ != 0);
491 gold_assert(bufsize >= this->strtab_size_);
492 if (this->zero_null_)
493 buffer[0] = '\0';
494 for (typename String_set_type::const_iterator p = this->string_set_.begin();
495 p != this->string_set_.end();
496 ++p)
497 {
498 const int len = (p->first.length + 1) * sizeof(Stringpool_char);
499 const section_offset_type offset = this->key_to_offset_[p->second];
500 gold_assert(static_cast<section_size_type>(offset) + len
501 <= this->strtab_size_);
502 memcpy(buffer + offset, p->first.string, len);
503 }
504 }
505
506 // Write the ELF strtab into the output file at the specified offset.
507
508 template<typename Stringpool_char>
509 void
510 Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
511 {
512 gold_assert(this->strtab_size_ != 0);
513 unsigned char* view = of->get_output_view(offset, this->strtab_size_);
514 this->write_to_buffer(view, this->strtab_size_);
515 of->write_output_view(offset, this->strtab_size_, view);
516 }
517
518 // Print statistical information to stderr. This is used for --stats.
519
520 template<typename Stringpool_char>
521 void
522 Stringpool_template<Stringpool_char>::print_stats(const char* name) const
523 {
524 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
525 fprintf(stderr, _("%s: %s entries: %zu; buckets: %zu\n"),
526 program_name, name, this->string_set_.size(),
527 this->string_set_.bucket_count());
528 #else
529 fprintf(stderr, _("%s: %s entries: %zu\n"),
530 program_name, name, this->table_.size());
531 #endif
532 fprintf(stderr, _("%s: %s Stringdata structures: %zu\n"),
533 program_name, name, this->strings_.size());
534 }
535
536 // Instantiate the templates we need.
537
538 template
539 class Stringpool_template<char>;
540
541 template
542 class Stringpool_template<uint16_t>;
543
544 template
545 class Stringpool_template<uint32_t>;
546
547 } // End namespace gold.