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