]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/stringpool.cc
PR 10030
[thirdparty/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
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 153{
cf88803a
ILT
154 // This is the hash function used by the dynamic linker for
155 // DT_GNU_HASH entries. I compared this to a Fowler/Noll/Vo hash
156 // for a C++ program with 385,775 global symbols. This hash
157 // function was very slightly worse. However, it is much faster to
158 // compute. Overall wall clock time was a win.
987cc251 159 const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
cf88803a
ILT
160 size_t h = 5381;
161 for (size_t i = 0; i < length * sizeof(Stringpool_char); ++i)
162 h = h * 33 + *p++;
163 return h;
14bfc3f5
ILT
164}
165
987cc251
ILT
166// Add the string S to the list of canonical strings. Return a
167// pointer to the canonical string. If PKEY is not NULL, set *PKEY to
168// the key. LENGTH is the length of S in characters. Note that S may
169// not be NUL terminated.
14bfc3f5 170
b8e6aad9
ILT
171template<typename Stringpool_char>
172const Stringpool_char*
173Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
2030fba0 174 size_t len)
14bfc3f5 175{
a3ad94ed
ILT
176 // We are in trouble if we've already computed the string offsets.
177 gold_assert(this->strtab_size_ == 0);
178
f0641a0b 179 // The size we allocate for a new Stringdata.
14bfc3f5 180 const size_t buffer_size = 1000;
f0641a0b
ILT
181 // The amount we multiply the Stringdata index when calculating the
182 // key.
183 const size_t key_mult = 1024;
a3ad94ed 184 gold_assert(key_mult >= buffer_size);
f0641a0b 185
987cc251
ILT
186 // Convert len to the number of bytes we need to allocate, including
187 // the null character.
188 len = (len + 1) * sizeof(Stringpool_char);
14bfc3f5
ILT
189
190 size_t alc;
191 bool front = true;
b8e6aad9 192 if (len > buffer_size)
14bfc3f5 193 {
61ba1cf9 194 alc = sizeof(Stringdata) + len;
14bfc3f5
ILT
195 front = false;
196 }
197 else if (this->strings_.empty())
61ba1cf9 198 alc = sizeof(Stringdata) + buffer_size;
14bfc3f5
ILT
199 else
200 {
61ba1cf9 201 Stringdata *psd = this->strings_.front();
b8e6aad9 202 if (len > psd->alc - psd->len)
61ba1cf9 203 alc = sizeof(Stringdata) + buffer_size;
14bfc3f5
ILT
204 else
205 {
206 char* ret = psd->data + psd->len;
987cc251
ILT
207 memcpy(ret, s, len - sizeof(Stringpool_char));
208 memset(ret + len - sizeof(Stringpool_char), 0,
209 sizeof(Stringpool_char));
f0641a0b 210
b8e6aad9 211 psd->len += len;
f0641a0b 212
b8e6aad9 213 return reinterpret_cast<const Stringpool_char*>(ret);
14bfc3f5
ILT
214 }
215 }
216
61ba1cf9
ILT
217 Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]);
218 psd->alc = alc - sizeof(Stringdata);
987cc251
ILT
219 memcpy(psd->data, s, len - sizeof(Stringpool_char));
220 memset(psd->data + len - sizeof(Stringpool_char), 0,
221 sizeof(Stringpool_char));
b8e6aad9 222 psd->len = len;
f0641a0b 223
14bfc3f5
ILT
224 if (front)
225 this->strings_.push_front(psd);
226 else
227 this->strings_.push_back(psd);
f0641a0b 228
b8e6aad9 229 return reinterpret_cast<const Stringpool_char*>(psd->data);
14bfc3f5
ILT
230}
231
232// Add a string to a string pool.
233
b8e6aad9
ILT
234template<typename Stringpool_char>
235const Stringpool_char*
cfd73a4e
ILT
236Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, bool copy,
237 Key* pkey)
c0873094
ILT
238{
239 return this->add_with_length(s, string_length(s), copy, pkey);
240}
241
242template<typename Stringpool_char>
243const Stringpool_char*
244Stringpool_template<Stringpool_char>::add_with_length(const Stringpool_char* s,
245 size_t length,
246 bool copy,
247 Key* pkey)
14bfc3f5 248{
987cc251 249 typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
14bfc3f5 250
5778530e
ILT
251 // We add 1 so that 0 is always invalid.
252 const Key k = this->key_to_offset_.size() + 1;
2030fba0 253
987cc251 254 if (!copy)
f0641a0b 255 {
987cc251
ILT
256 // When we don't need to copy the string, we can call insert
257 // directly.
14bfc3f5 258
2030fba0 259 std::pair<Hashkey, Hashval> element(Hashkey(s, length), k);
f0641a0b 260
987cc251 261 Insert_type ins = this->string_set_.insert(element);
f0641a0b 262
987cc251
ILT
263 typename String_set_type::const_iterator p = ins.first;
264
265 if (ins.second)
266 {
267 // We just added the string. The key value has now been
268 // used.
2030fba0 269 this->key_to_offset_.push_back(0);
987cc251
ILT
270 }
271 else
272 {
2030fba0 273 gold_assert(k != p->second);
987cc251
ILT
274 }
275
276 if (pkey != NULL)
2030fba0 277 *pkey = p->second;
987cc251
ILT
278 return p->first.string;
279 }
f0641a0b 280
c0873094 281 // When we have to copy the string, we look it up twice in the hash
987cc251
ILT
282 // table. The problem is that we can't insert S before we
283 // canonicalize it by copying it into the canonical list. The hash
c0873094 284 // code will only be computed once.
987cc251 285
c0873094 286 Hashkey hk(s, length);
987cc251
ILT
287 typename String_set_type::const_iterator p = this->string_set_.find(hk);
288 if (p != this->string_set_.end())
289 {
290 if (pkey != NULL)
2030fba0 291 *pkey = p->second;
987cc251
ILT
292 return p->first.string;
293 }
294
2030fba0
ILT
295 this->key_to_offset_.push_back(0);
296
297 hk.string = this->add_string(s, length);
987cc251
ILT
298 // The contents of the string stay the same, so we don't need to
299 // adjust hk.hash_code or hk.length.
300
2030fba0 301 std::pair<Hashkey, Hashval> element(hk, k);
987cc251 302
987cc251
ILT
303 Insert_type ins = this->string_set_.insert(element);
304 gold_assert(ins.second);
305
306 if (pkey != NULL)
307 *pkey = k;
308 return hk.string;
14bfc3f5
ILT
309}
310
b8e6aad9
ILT
311template<typename Stringpool_char>
312const Stringpool_char*
313Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
314 Key* pkey) const
61ba1cf9 315{
987cc251
ILT
316 Hashkey hk(s);
317 typename String_set_type::const_iterator p = this->string_set_.find(hk);
61ba1cf9
ILT
318 if (p == this->string_set_.end())
319 return NULL;
f0641a0b
ILT
320
321 if (pkey != NULL)
2030fba0 322 *pkey = p->second;
f0641a0b 323
987cc251 324 return p->first.string;
61ba1cf9
ILT
325}
326
327// Comparison routine used when sorting into an ELF strtab. We want
328// to sort this so that when one string is a suffix of another, we
329// always see the shorter string immediately after the longer string.
330// For example, we want to see these strings in this order:
331// abcd
332// cd
333// d
334// When strings are not suffixes, we don't care what order they are
335// in, but we need to ensure that suffixes wind up next to each other.
336// So we do a reversed lexicographic sort on the reversed string.
337
b8e6aad9 338template<typename Stringpool_char>
61ba1cf9 339bool
b8e6aad9 340Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
614f30a2
ILT
341 const Stringpool_sort_info& sort_info1,
342 const Stringpool_sort_info& sort_info2) const
61ba1cf9 343{
987cc251
ILT
344 const Hashkey& h1(sort_info1->first);
345 const Hashkey& h2(sort_info2->first);
346 const Stringpool_char* s1 = h1.string;
347 const Stringpool_char* s2 = h2.string;
348 const size_t len1 = h1.length;
349 const size_t len2 = h2.length;
614f30a2 350 const size_t minlen = len1 < len2 ? len1 : len2;
b8e6aad9
ILT
351 const Stringpool_char* p1 = s1 + len1 - 1;
352 const Stringpool_char* p2 = s2 + len2 - 1;
353 for (size_t i = minlen; i > 0; --i, --p1, --p2)
61ba1cf9
ILT
354 {
355 if (*p1 != *p2)
356 return *p1 > *p2;
357 }
358 return len1 > len2;
359}
360
361// Return whether s1 is a suffix of s2.
362
b8e6aad9 363template<typename Stringpool_char>
61ba1cf9 364bool
b8e6aad9 365Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
614f30a2
ILT
366 size_t len1,
367 const Stringpool_char* s2,
368 size_t len2)
61ba1cf9 369{
61ba1cf9
ILT
370 if (len1 > len2)
371 return false;
b8e6aad9 372 return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
61ba1cf9
ILT
373}
374
375// Turn the stringpool into an ELF strtab: determine the offsets of
376// each string in the table.
377
b8e6aad9 378template<typename Stringpool_char>
61ba1cf9 379void
b8e6aad9 380Stringpool_template<Stringpool_char>::set_string_offsets()
61ba1cf9 381{
a3ad94ed
ILT
382 if (this->strtab_size_ != 0)
383 {
384 // We've already computed the offsets.
385 return;
386 }
387
b8e6aad9
ILT
388 const size_t charsize = sizeof(Stringpool_char);
389
390 // Offset 0 may be reserved for the empty string.
8383303e 391 section_offset_type offset = this->zero_null_ ? charsize : 0;
614f30a2 392
377caf49
ILT
393 // Sorting to find suffixes can take over 25% of the total CPU time
394 // used by the linker. Since it's merely an optimization to reduce
395 // the strtab size, and gives a relatively small benefit (it's
396 // typically rare for a symbol to be a suffix of another), we only
397 // take the time to sort when the user asks for heavy optimization.
8851ecca 398 if (parameters->options().optimize() < 2)
61ba1cf9 399 {
377caf49
ILT
400 for (typename String_set_type::iterator curr = this->string_set_.begin();
401 curr != this->string_set_.end();
402 curr++)
403 {
5778530e 404 section_offset_type* poff = &this->key_to_offset_[curr->second - 1];
987cc251 405 if (this->zero_null_ && curr->first.string[0] == 0)
2030fba0 406 *poff = 0;
377caf49
ILT
407 else
408 {
2030fba0 409 *poff = offset;
987cc251 410 offset += (curr->first.length + 1) * charsize;
377caf49
ILT
411 }
412 }
413 }
414 else
415 {
416 size_t count = this->string_set_.size();
417
418 std::vector<Stringpool_sort_info> v;
419 v.reserve(count);
420
421 for (typename String_set_type::iterator p = this->string_set_.begin();
422 p != this->string_set_.end();
423 ++p)
987cc251 424 v.push_back(Stringpool_sort_info(p));
377caf49
ILT
425
426 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
427
2030fba0 428 section_offset_type last_offset = -1;
377caf49
ILT
429 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
430 curr = v.begin();
431 curr != v.end();
432 last = curr++)
433 {
2030fba0 434 section_offset_type this_offset;
987cc251 435 if (this->zero_null_ && (*curr)->first.string[0] == 0)
2030fba0 436 this_offset = 0;
377caf49 437 else if (last != v.end()
987cc251
ILT
438 && is_suffix((*curr)->first.string,
439 (*curr)->first.length,
440 (*last)->first.string,
441 (*last)->first.length))
2030fba0
ILT
442 this_offset = (last_offset
443 + (((*last)->first.length - (*curr)->first.length)
444 * charsize));
377caf49
ILT
445 else
446 {
2030fba0 447 this_offset = offset;
987cc251 448 offset += ((*curr)->first.length + 1) * charsize;
377caf49 449 }
5778530e 450 this->key_to_offset_[(*curr)->second - 1] = this_offset;
2030fba0 451 last_offset = this_offset;
377caf49 452 }
61ba1cf9
ILT
453 }
454
455 this->strtab_size_ = offset;
456}
457
458// Get the offset of a string in the ELF strtab. The string must
459// exist.
460
b8e6aad9 461template<typename Stringpool_char>
8383303e 462section_offset_type
b8e6aad9
ILT
463Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
464 const
c0873094
ILT
465{
466 return this->get_offset_with_length(s, string_length(s));
467}
468
469template<typename Stringpool_char>
470section_offset_type
471Stringpool_template<Stringpool_char>::get_offset_with_length(
472 const Stringpool_char* s,
473 size_t length) const
61ba1cf9 474{
a3ad94ed 475 gold_assert(this->strtab_size_ != 0);
c0873094
ILT
476 Hashkey hk(s, length);
477 typename String_set_type::const_iterator p = this->string_set_.find(hk);
61ba1cf9 478 if (p != this->string_set_.end())
5778530e 479 return this->key_to_offset_[p->second - 1];
a3ad94ed 480 gold_unreachable();
61ba1cf9
ILT
481}
482
9a0910c3 483// Write the ELF strtab into the buffer.
61ba1cf9 484
b8e6aad9 485template<typename Stringpool_char>
61ba1cf9 486void
8383303e
ILT
487Stringpool_template<Stringpool_char>::write_to_buffer(
488 unsigned char* buffer,
489 section_size_type bufsize)
61ba1cf9 490{
a3ad94ed 491 gold_assert(this->strtab_size_ != 0);
8383303e 492 gold_assert(bufsize >= this->strtab_size_);
b8e6aad9 493 if (this->zero_null_)
9a0910c3 494 buffer[0] = '\0';
b8e6aad9 495 for (typename String_set_type::const_iterator p = this->string_set_.begin();
61ba1cf9
ILT
496 p != this->string_set_.end();
497 ++p)
9a0910c3 498 {
987cc251 499 const int len = (p->first.length + 1) * sizeof(Stringpool_char);
5778530e 500 const section_offset_type offset = this->key_to_offset_[p->second - 1];
2030fba0 501 gold_assert(static_cast<section_size_type>(offset) + len
8383303e 502 <= this->strtab_size_);
2030fba0 503 memcpy(buffer + offset, p->first.string, len);
9a0910c3
ILT
504 }
505}
506
507// Write the ELF strtab into the output file at the specified offset.
508
509template<typename Stringpool_char>
510void
511Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
512{
513 gold_assert(this->strtab_size_ != 0);
514 unsigned char* view = of->get_output_view(offset, this->strtab_size_);
96803768 515 this->write_to_buffer(view, this->strtab_size_);
9a0910c3 516 of->write_output_view(offset, this->strtab_size_, view);
61ba1cf9
ILT
517}
518
ad8f37d1
ILT
519// Print statistical information to stderr. This is used for --stats.
520
521template<typename Stringpool_char>
522void
523Stringpool_template<Stringpool_char>::print_stats(const char* name) const
524{
525#if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
526 fprintf(stderr, _("%s: %s entries: %zu; buckets: %zu\n"),
527 program_name, name, this->string_set_.size(),
528 this->string_set_.bucket_count());
529#else
530 fprintf(stderr, _("%s: %s entries: %zu\n"),
531 program_name, name, this->table_.size());
532#endif
533 fprintf(stderr, _("%s: %s Stringdata structures: %zu\n"),
534 program_name, name, this->strings_.size());
535}
536
b8e6aad9
ILT
537// Instantiate the templates we need.
538
539template
540class Stringpool_template<char>;
541
542template
543class Stringpool_template<uint16_t>;
544
545template
546class Stringpool_template<uint32_t>;
547
14bfc3f5 548} // End namespace gold.