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