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