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