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