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