Move Stringpool offsets into a chunked_vector indexed by keys.
[deliverable/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 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
155 const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
156 if (sizeof(size_t) > 4)
157 {
158 size_t result = static_cast<size_t>(14695981039346656037ULL);
159 for (size_t i = 0; i < length * sizeof(Stringpool_char); ++i)
160 {
161 result ^= static_cast<size_t>(*p++);
162 result *= 1099511628211ULL;
163 }
164 return result;
165 }
166 else
167 {
168 size_t result = 2166136261UL;
169 for (size_t i = 0; i < length * sizeof(Stringpool_char); ++i)
170 {
171 result ^= static_cast<size_t>(*p++);
172 result *= 16777619UL;
173 }
174 return result;
175 }
176 }
177
178 // Add the string S to the list of canonical strings. Return a
179 // pointer to the canonical string. If PKEY is not NULL, set *PKEY to
180 // the key. LENGTH is the length of S in characters. Note that S may
181 // not be NUL terminated.
182
183 template<typename Stringpool_char>
184 const Stringpool_char*
185 Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
186 size_t len)
187 {
188 // We are in trouble if we've already computed the string offsets.
189 gold_assert(this->strtab_size_ == 0);
190
191 // The size we allocate for a new Stringdata.
192 const size_t buffer_size = 1000;
193 // The amount we multiply the Stringdata index when calculating the
194 // key.
195 const size_t key_mult = 1024;
196 gold_assert(key_mult >= buffer_size);
197
198 // Convert len to the number of bytes we need to allocate, including
199 // the null character.
200 len = (len + 1) * sizeof(Stringpool_char);
201
202 size_t alc;
203 bool front = true;
204 if (len > buffer_size)
205 {
206 alc = sizeof(Stringdata) + len;
207 front = false;
208 }
209 else if (this->strings_.empty())
210 alc = sizeof(Stringdata) + buffer_size;
211 else
212 {
213 Stringdata *psd = this->strings_.front();
214 if (len > psd->alc - psd->len)
215 alc = sizeof(Stringdata) + buffer_size;
216 else
217 {
218 char* ret = psd->data + psd->len;
219 memcpy(ret, s, len - sizeof(Stringpool_char));
220 memset(ret + len - sizeof(Stringpool_char), 0,
221 sizeof(Stringpool_char));
222
223 psd->len += len;
224
225 return reinterpret_cast<const Stringpool_char*>(ret);
226 }
227 }
228
229 Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]);
230 psd->alc = alc - sizeof(Stringdata);
231 memcpy(psd->data, s, len - sizeof(Stringpool_char));
232 memset(psd->data + len - sizeof(Stringpool_char), 0,
233 sizeof(Stringpool_char));
234 psd->len = len;
235
236 if (front)
237 this->strings_.push_front(psd);
238 else
239 this->strings_.push_back(psd);
240
241 return reinterpret_cast<const Stringpool_char*>(psd->data);
242 }
243
244 // Add a string to a string pool.
245
246 template<typename Stringpool_char>
247 const Stringpool_char*
248 Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, bool copy,
249 Key* pkey)
250 {
251 return this->add_with_length(s, string_length(s), copy, pkey);
252 }
253
254 template<typename Stringpool_char>
255 const Stringpool_char*
256 Stringpool_template<Stringpool_char>::add_with_length(const Stringpool_char* s,
257 size_t length,
258 bool copy,
259 Key* pkey)
260 {
261 typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
262
263 const Key k = this->key_to_offset_.size();
264
265 if (!copy)
266 {
267 // When we don't need to copy the string, we can call insert
268 // directly.
269
270 std::pair<Hashkey, Hashval> element(Hashkey(s, length), k);
271
272 Insert_type ins = this->string_set_.insert(element);
273
274 typename String_set_type::const_iterator p = ins.first;
275
276 if (ins.second)
277 {
278 // We just added the string. The key value has now been
279 // used.
280 this->key_to_offset_.push_back(0);
281 }
282 else
283 {
284 gold_assert(k != p->second);
285 }
286
287 if (pkey != NULL)
288 *pkey = p->second;
289 return p->first.string;
290 }
291
292 // When we have to copy the string, we look it up twice in the hash
293 // table. The problem is that we can't insert S before we
294 // canonicalize it by copying it into the canonical list. The hash
295 // code will only be computed once.
296
297 Hashkey hk(s, length);
298 typename String_set_type::const_iterator p = this->string_set_.find(hk);
299 if (p != this->string_set_.end())
300 {
301 if (pkey != NULL)
302 *pkey = p->second;
303 return p->first.string;
304 }
305
306 this->key_to_offset_.push_back(0);
307
308 hk.string = this->add_string(s, length);
309 // The contents of the string stay the same, so we don't need to
310 // adjust hk.hash_code or hk.length.
311
312 std::pair<Hashkey, Hashval> element(hk, k);
313
314 Insert_type ins = this->string_set_.insert(element);
315 gold_assert(ins.second);
316
317 if (pkey != NULL)
318 *pkey = k;
319 return hk.string;
320 }
321
322 template<typename Stringpool_char>
323 const Stringpool_char*
324 Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
325 Key* pkey) const
326 {
327 Hashkey hk(s);
328 typename String_set_type::const_iterator p = this->string_set_.find(hk);
329 if (p == this->string_set_.end())
330 return NULL;
331
332 if (pkey != NULL)
333 *pkey = p->second;
334
335 return p->first.string;
336 }
337
338 // Comparison routine used when sorting into an ELF strtab. We want
339 // to sort this so that when one string is a suffix of another, we
340 // always see the shorter string immediately after the longer string.
341 // For example, we want to see these strings in this order:
342 // abcd
343 // cd
344 // d
345 // When strings are not suffixes, we don't care what order they are
346 // in, but we need to ensure that suffixes wind up next to each other.
347 // So we do a reversed lexicographic sort on the reversed string.
348
349 template<typename Stringpool_char>
350 bool
351 Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
352 const Stringpool_sort_info& sort_info1,
353 const Stringpool_sort_info& sort_info2) const
354 {
355 const Hashkey& h1(sort_info1->first);
356 const Hashkey& h2(sort_info2->first);
357 const Stringpool_char* s1 = h1.string;
358 const Stringpool_char* s2 = h2.string;
359 const size_t len1 = h1.length;
360 const size_t len2 = h2.length;
361 const size_t minlen = len1 < len2 ? len1 : len2;
362 const Stringpool_char* p1 = s1 + len1 - 1;
363 const Stringpool_char* p2 = s2 + len2 - 1;
364 for (size_t i = minlen; i > 0; --i, --p1, --p2)
365 {
366 if (*p1 != *p2)
367 return *p1 > *p2;
368 }
369 return len1 > len2;
370 }
371
372 // Return whether s1 is a suffix of s2.
373
374 template<typename Stringpool_char>
375 bool
376 Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
377 size_t len1,
378 const Stringpool_char* s2,
379 size_t len2)
380 {
381 if (len1 > len2)
382 return false;
383 return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
384 }
385
386 // Turn the stringpool into an ELF strtab: determine the offsets of
387 // each string in the table.
388
389 template<typename Stringpool_char>
390 void
391 Stringpool_template<Stringpool_char>::set_string_offsets()
392 {
393 if (this->strtab_size_ != 0)
394 {
395 // We've already computed the offsets.
396 return;
397 }
398
399 const size_t charsize = sizeof(Stringpool_char);
400
401 // Offset 0 may be reserved for the empty string.
402 section_offset_type offset = this->zero_null_ ? charsize : 0;
403
404 // Sorting to find suffixes can take over 25% of the total CPU time
405 // used by the linker. Since it's merely an optimization to reduce
406 // the strtab size, and gives a relatively small benefit (it's
407 // typically rare for a symbol to be a suffix of another), we only
408 // take the time to sort when the user asks for heavy optimization.
409 if (parameters->optimization_level() < 2)
410 {
411 for (typename String_set_type::iterator curr = this->string_set_.begin();
412 curr != this->string_set_.end();
413 curr++)
414 {
415 section_offset_type* poff = &this->key_to_offset_[curr->second];
416 if (this->zero_null_ && curr->first.string[0] == 0)
417 *poff = 0;
418 else
419 {
420 *poff = offset;
421 offset += (curr->first.length + 1) * charsize;
422 }
423 }
424 }
425 else
426 {
427 size_t count = this->string_set_.size();
428
429 std::vector<Stringpool_sort_info> v;
430 v.reserve(count);
431
432 for (typename String_set_type::iterator p = this->string_set_.begin();
433 p != this->string_set_.end();
434 ++p)
435 v.push_back(Stringpool_sort_info(p));
436
437 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
438
439 section_offset_type last_offset = -1;
440 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
441 curr = v.begin();
442 curr != v.end();
443 last = curr++)
444 {
445 section_offset_type this_offset;
446 if (this->zero_null_ && (*curr)->first.string[0] == 0)
447 this_offset = 0;
448 else if (last != v.end()
449 && is_suffix((*curr)->first.string,
450 (*curr)->first.length,
451 (*last)->first.string,
452 (*last)->first.length))
453 this_offset = (last_offset
454 + (((*last)->first.length - (*curr)->first.length)
455 * charsize));
456 else
457 {
458 this_offset = offset;
459 offset += ((*curr)->first.length + 1) * charsize;
460 }
461 this->key_to_offset_[(*curr)->second] = this_offset;
462 last_offset = this_offset;
463 }
464 }
465
466 this->strtab_size_ = offset;
467 }
468
469 // Get the offset of a string in the ELF strtab. The string must
470 // exist.
471
472 template<typename Stringpool_char>
473 section_offset_type
474 Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
475 const
476 {
477 return this->get_offset_with_length(s, string_length(s));
478 }
479
480 template<typename Stringpool_char>
481 section_offset_type
482 Stringpool_template<Stringpool_char>::get_offset_with_length(
483 const Stringpool_char* s,
484 size_t length) const
485 {
486 gold_assert(this->strtab_size_ != 0);
487 Hashkey hk(s, length);
488 typename String_set_type::const_iterator p = this->string_set_.find(hk);
489 if (p != this->string_set_.end())
490 return this->key_to_offset_[p->second];
491 gold_unreachable();
492 }
493
494 // Write the ELF strtab into the buffer.
495
496 template<typename Stringpool_char>
497 void
498 Stringpool_template<Stringpool_char>::write_to_buffer(
499 unsigned char* buffer,
500 section_size_type bufsize)
501 {
502 gold_assert(this->strtab_size_ != 0);
503 gold_assert(bufsize >= this->strtab_size_);
504 if (this->zero_null_)
505 buffer[0] = '\0';
506 for (typename String_set_type::const_iterator p = this->string_set_.begin();
507 p != this->string_set_.end();
508 ++p)
509 {
510 const int len = (p->first.length + 1) * sizeof(Stringpool_char);
511 const section_offset_type offset = this->key_to_offset_[p->second];
512 gold_assert(static_cast<section_size_type>(offset) + len
513 <= this->strtab_size_);
514 memcpy(buffer + offset, p->first.string, len);
515 }
516 }
517
518 // Write the ELF strtab into the output file at the specified offset.
519
520 template<typename Stringpool_char>
521 void
522 Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
523 {
524 gold_assert(this->strtab_size_ != 0);
525 unsigned char* view = of->get_output_view(offset, this->strtab_size_);
526 this->write_to_buffer(view, this->strtab_size_);
527 of->write_output_view(offset, this->strtab_size_, view);
528 }
529
530 // Print statistical information to stderr. This is used for --stats.
531
532 template<typename Stringpool_char>
533 void
534 Stringpool_template<Stringpool_char>::print_stats(const char* name) const
535 {
536 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
537 fprintf(stderr, _("%s: %s entries: %zu; buckets: %zu\n"),
538 program_name, name, this->string_set_.size(),
539 this->string_set_.bucket_count());
540 #else
541 fprintf(stderr, _("%s: %s entries: %zu\n"),
542 program_name, name, this->table_.size());
543 #endif
544 fprintf(stderr, _("%s: %s Stringdata structures: %zu\n"),
545 program_name, name, this->strings_.size());
546 }
547
548 // Instantiate the templates we need.
549
550 template
551 class Stringpool_template<char>;
552
553 template
554 class Stringpool_template<uint16_t>;
555
556 template
557 class Stringpool_template<uint32_t>;
558
559 } // End namespace gold.
This page took 0.041604 seconds and 5 git commands to generate.