Fix --thread-count-middle and --thread-count-final.
[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()
b8e6aad9 38 : string_set_(), strings_(), strtab_size_(0), next_index_(1),
cfd73a4e 39 next_uncopied_key_(-1), 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
ILT
51 this->strings_.clear();
52 this->string_set_.clear();
53}
54
55template<typename Stringpool_char>
56Stringpool_template<Stringpool_char>::~Stringpool_template()
57{
58 this->clear();
14bfc3f5
ILT
59}
60
b8e6aad9
ILT
61// Return the length of a string of arbitrary character type.
62
63template<typename Stringpool_char>
64size_t
65Stringpool_template<Stringpool_char>::string_length(const Stringpool_char* p)
66{
67 size_t len = 0;
68 for (; *p != 0; ++p)
69 ++len;
70 return len;
71}
72
73// Specialize string_length for char. Maybe we could just use
74// std::char_traits<>::length?
75
76template<>
77inline size_t
78Stringpool_template<char>::string_length(const char* p)
79{
80 return strlen(p);
81}
82
987cc251 83// Compare two strings of arbitrary character type for equality.
b8e6aad9
ILT
84
85template<typename Stringpool_char>
86bool
987cc251
ILT
87Stringpool_template<Stringpool_char>::string_equal(const Stringpool_char* s1,
88 const Stringpool_char* s2)
b8e6aad9
ILT
89{
90 while (*s1 != 0)
91 if (*s1++ != *s2++)
92 return false;
93 return *s2 == 0;
94}
95
987cc251 96// Specialize string_equal for char.
b8e6aad9
ILT
97
98template<>
987cc251
ILT
99inline bool
100Stringpool_template<char>::string_equal(const char* s1, const char* s2)
b8e6aad9
ILT
101{
102 return strcmp(s1, s2) == 0;
103}
104
987cc251
ILT
105// Equality comparison function for the hash table.
106
107template<typename Stringpool_char>
108inline bool
109Stringpool_template<Stringpool_char>::Stringpool_eq::operator()(
110 const Hashkey& h1,
111 const Hashkey& h2) const
112{
113 return (h1.hash_code == h2.hash_code
114 && h1.length == h2.length
115 && memcmp(h1.string, h2.string,
116 h1.length * sizeof(Stringpool_char)) == 0);
117}
118
119// Hash function. The length is in characters, not bytes.
14bfc3f5 120
b8e6aad9 121template<typename Stringpool_char>
14bfc3f5 122size_t
987cc251
ILT
123Stringpool_template<Stringpool_char>::string_hash(const Stringpool_char* s,
124 size_t length)
14bfc3f5
ILT
125{
126 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
987cc251 127 const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
730cdc88 128 if (sizeof(size_t) > 4)
14bfc3f5 129 {
b3168e9d 130 size_t result = static_cast<size_t>(14695981039346656037ULL);
987cc251 131 for (size_t i = 0; i < length * sizeof(Stringpool_char); ++i)
14bfc3f5 132 {
987cc251
ILT
133 result ^= static_cast<size_t>(*p++);
134 result *= 1099511628211ULL;
14bfc3f5
ILT
135 }
136 return result;
137 }
138 else
139 {
140 size_t result = 2166136261UL;
987cc251 141 for (size_t i = 0; i < length * sizeof(Stringpool_char); ++i)
14bfc3f5 142 {
987cc251
ILT
143 result ^= static_cast<size_t>(*p++);
144 result *= 16777619UL;
14bfc3f5
ILT
145 }
146 return result;
147 }
148}
149
987cc251
ILT
150// Add the string S to the list of canonical strings. Return a
151// pointer to the canonical string. If PKEY is not NULL, set *PKEY to
152// the key. LENGTH is the length of S in characters. Note that S may
153// not be NUL terminated.
14bfc3f5 154
b8e6aad9
ILT
155template<typename Stringpool_char>
156const Stringpool_char*
157Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
987cc251 158 size_t len,
b8e6aad9 159 Key* pkey)
14bfc3f5 160{
a3ad94ed
ILT
161 // We are in trouble if we've already computed the string offsets.
162 gold_assert(this->strtab_size_ == 0);
163
f0641a0b 164 // The size we allocate for a new Stringdata.
14bfc3f5 165 const size_t buffer_size = 1000;
f0641a0b
ILT
166 // The amount we multiply the Stringdata index when calculating the
167 // key.
168 const size_t key_mult = 1024;
a3ad94ed 169 gold_assert(key_mult >= buffer_size);
f0641a0b 170
987cc251
ILT
171 // Convert len to the number of bytes we need to allocate, including
172 // the null character.
173 len = (len + 1) * sizeof(Stringpool_char);
14bfc3f5
ILT
174
175 size_t alc;
176 bool front = true;
b8e6aad9 177 if (len > buffer_size)
14bfc3f5 178 {
61ba1cf9 179 alc = sizeof(Stringdata) + len;
14bfc3f5
ILT
180 front = false;
181 }
182 else if (this->strings_.empty())
61ba1cf9 183 alc = sizeof(Stringdata) + buffer_size;
14bfc3f5
ILT
184 else
185 {
61ba1cf9 186 Stringdata *psd = this->strings_.front();
b8e6aad9 187 if (len > psd->alc - psd->len)
61ba1cf9 188 alc = sizeof(Stringdata) + buffer_size;
14bfc3f5
ILT
189 else
190 {
191 char* ret = psd->data + psd->len;
987cc251
ILT
192 memcpy(ret, s, len - sizeof(Stringpool_char));
193 memset(ret + len - sizeof(Stringpool_char), 0,
194 sizeof(Stringpool_char));
f0641a0b
ILT
195
196 if (pkey != NULL)
197 *pkey = psd->index * key_mult + psd->len;
198
b8e6aad9 199 psd->len += len;
f0641a0b 200
b8e6aad9 201 return reinterpret_cast<const Stringpool_char*>(ret);
14bfc3f5
ILT
202 }
203 }
204
61ba1cf9
ILT
205 Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]);
206 psd->alc = alc - sizeof(Stringdata);
987cc251
ILT
207 memcpy(psd->data, s, len - sizeof(Stringpool_char));
208 memset(psd->data + len - sizeof(Stringpool_char), 0,
209 sizeof(Stringpool_char));
b8e6aad9 210 psd->len = len;
f0641a0b
ILT
211 psd->index = this->next_index_;
212 ++this->next_index_;
213
214 if (pkey != NULL)
215 *pkey = psd->index * key_mult;
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)
14bfc3f5 231{
987cc251 232 typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
14bfc3f5 233
987cc251 234 if (!copy)
f0641a0b 235 {
987cc251
ILT
236 // When we don't need to copy the string, we can call insert
237 // directly.
14bfc3f5 238
987cc251
ILT
239 const Key k = this->next_uncopied_key_;
240 const off_t ozero = 0;
241 std::pair<Hashkey, Hashval> element(Hashkey(s),
242 std::make_pair(k, ozero));
f0641a0b 243
987cc251 244 Insert_type ins = this->string_set_.insert(element);
f0641a0b 245
987cc251
ILT
246 typename String_set_type::const_iterator p = ins.first;
247
248 if (ins.second)
249 {
250 // We just added the string. The key value has now been
251 // used.
252 --this->next_uncopied_key_;
253 }
254 else
255 {
256 gold_assert(k != p->second.first);
257 }
258
259 if (pkey != NULL)
260 *pkey = p->second.first;
261 return p->first.string;
262 }
f0641a0b 263
987cc251 264 return this->add_prefix(s, string_length(s), pkey);
14bfc3f5
ILT
265}
266
267// Add a prefix of a string to a string pool.
268
b8e6aad9
ILT
269template<typename Stringpool_char>
270const Stringpool_char*
cfd73a4e
ILT
271Stringpool_template<Stringpool_char>::add_prefix(const Stringpool_char* s,
272 size_t len,
273 Key* pkey)
14bfc3f5 274{
987cc251
ILT
275 // When adding an entry, this will look it up twice in the hash
276 // table. The problem is that we can't insert S before we
277 // canonicalize it by copying it into the canonical list. The hash
278 // code will only be computed once, so this isn't all that
279 // expensive.
280
281 Hashkey hk(s, len);
282 typename String_set_type::const_iterator p = this->string_set_.find(hk);
283 if (p != this->string_set_.end())
284 {
285 if (pkey != NULL)
286 *pkey = p->second.first;
287 return p->first.string;
288 }
289
290 Key k;
291 hk.string = this->add_string(s, len, &k);
292 // The contents of the string stay the same, so we don't need to
293 // adjust hk.hash_code or hk.length.
294
295 const off_t ozero = 0;
296 std::pair<Hashkey, Hashval> element(hk, std::make_pair(k, ozero));
297
298 typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
299 Insert_type ins = this->string_set_.insert(element);
300 gold_assert(ins.second);
301
302 if (pkey != NULL)
303 *pkey = k;
304 return hk.string;
14bfc3f5
ILT
305}
306
b8e6aad9
ILT
307template<typename Stringpool_char>
308const Stringpool_char*
309Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
310 Key* pkey) const
61ba1cf9 311{
987cc251
ILT
312 Hashkey hk(s);
313 typename String_set_type::const_iterator p = this->string_set_.find(hk);
61ba1cf9
ILT
314 if (p == this->string_set_.end())
315 return NULL;
f0641a0b
ILT
316
317 if (pkey != NULL)
318 *pkey = p->second.first;
319
987cc251 320 return p->first.string;
61ba1cf9
ILT
321}
322
323// Comparison routine used when sorting into an ELF strtab. We want
324// to sort this so that when one string is a suffix of another, we
325// always see the shorter string immediately after the longer string.
326// For example, we want to see these strings in this order:
327// abcd
328// cd
329// d
330// When strings are not suffixes, we don't care what order they are
331// in, but we need to ensure that suffixes wind up next to each other.
332// So we do a reversed lexicographic sort on the reversed string.
333
b8e6aad9 334template<typename Stringpool_char>
61ba1cf9 335bool
b8e6aad9 336Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
614f30a2
ILT
337 const Stringpool_sort_info& sort_info1,
338 const Stringpool_sort_info& sort_info2) const
61ba1cf9 339{
987cc251
ILT
340 const Hashkey& h1(sort_info1->first);
341 const Hashkey& h2(sort_info2->first);
342 const Stringpool_char* s1 = h1.string;
343 const Stringpool_char* s2 = h2.string;
344 const size_t len1 = h1.length;
345 const size_t len2 = h2.length;
614f30a2 346 const size_t minlen = len1 < len2 ? len1 : len2;
b8e6aad9
ILT
347 const Stringpool_char* p1 = s1 + len1 - 1;
348 const Stringpool_char* p2 = s2 + len2 - 1;
349 for (size_t i = minlen; i > 0; --i, --p1, --p2)
61ba1cf9
ILT
350 {
351 if (*p1 != *p2)
352 return *p1 > *p2;
353 }
354 return len1 > len2;
355}
356
357// Return whether s1 is a suffix of s2.
358
b8e6aad9 359template<typename Stringpool_char>
61ba1cf9 360bool
b8e6aad9 361Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
614f30a2
ILT
362 size_t len1,
363 const Stringpool_char* s2,
364 size_t len2)
61ba1cf9 365{
61ba1cf9
ILT
366 if (len1 > len2)
367 return false;
b8e6aad9 368 return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
61ba1cf9
ILT
369}
370
371// Turn the stringpool into an ELF strtab: determine the offsets of
372// each string in the table.
373
b8e6aad9 374template<typename Stringpool_char>
61ba1cf9 375void
b8e6aad9 376Stringpool_template<Stringpool_char>::set_string_offsets()
61ba1cf9 377{
a3ad94ed
ILT
378 if (this->strtab_size_ != 0)
379 {
380 // We've already computed the offsets.
381 return;
382 }
383
b8e6aad9
ILT
384 const size_t charsize = sizeof(Stringpool_char);
385
386 // Offset 0 may be reserved for the empty string.
387 off_t offset = this->zero_null_ ? charsize : 0;
614f30a2 388
377caf49
ILT
389 // Sorting to find suffixes can take over 25% of the total CPU time
390 // used by the linker. Since it's merely an optimization to reduce
391 // the strtab size, and gives a relatively small benefit (it's
392 // typically rare for a symbol to be a suffix of another), we only
393 // take the time to sort when the user asks for heavy optimization.
394 if (parameters->optimization_level() < 2)
61ba1cf9 395 {
377caf49
ILT
396 for (typename String_set_type::iterator curr = this->string_set_.begin();
397 curr != this->string_set_.end();
398 curr++)
399 {
987cc251 400 if (this->zero_null_ && curr->first.string[0] == 0)
377caf49
ILT
401 curr->second.second = 0;
402 else
403 {
404 curr->second.second = offset;
987cc251 405 offset += (curr->first.length + 1) * charsize;
377caf49
ILT
406 }
407 }
408 }
409 else
410 {
411 size_t count = this->string_set_.size();
412
413 std::vector<Stringpool_sort_info> v;
414 v.reserve(count);
415
416 for (typename String_set_type::iterator p = this->string_set_.begin();
417 p != this->string_set_.end();
418 ++p)
987cc251 419 v.push_back(Stringpool_sort_info(p));
377caf49
ILT
420
421 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
422
423 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
424 curr = v.begin();
425 curr != v.end();
426 last = curr++)
427 {
987cc251
ILT
428 if (this->zero_null_ && (*curr)->first.string[0] == 0)
429 (*curr)->second.second = 0;
377caf49 430 else if (last != v.end()
987cc251
ILT
431 && is_suffix((*curr)->first.string,
432 (*curr)->first.length,
433 (*last)->first.string,
434 (*last)->first.length))
435 (*curr)->second.second = ((*last)->second.second
436 + (((*last)->first.length
437 - (*curr)->first.length)
438 * charsize));
377caf49
ILT
439 else
440 {
987cc251
ILT
441 (*curr)->second.second = offset;
442 offset += ((*curr)->first.length + 1) * charsize;
377caf49
ILT
443 }
444 }
61ba1cf9
ILT
445 }
446
447 this->strtab_size_ = offset;
448}
449
450// Get the offset of a string in the ELF strtab. The string must
451// exist.
452
b8e6aad9 453template<typename Stringpool_char>
61ba1cf9 454off_t
b8e6aad9
ILT
455Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
456 const
61ba1cf9 457{
a3ad94ed 458 gold_assert(this->strtab_size_ != 0);
b8e6aad9 459 typename String_set_type::const_iterator p = this->string_set_.find(s);
61ba1cf9 460 if (p != this->string_set_.end())
f0641a0b 461 return p->second.second;
a3ad94ed 462 gold_unreachable();
61ba1cf9
ILT
463}
464
9a0910c3 465// Write the ELF strtab into the buffer.
61ba1cf9 466
b8e6aad9 467template<typename Stringpool_char>
61ba1cf9 468void
96803768 469Stringpool_template<Stringpool_char>::write_to_buffer(unsigned char* buffer,
9a0910c3 470 size_t bufsize)
61ba1cf9 471{
a3ad94ed 472 gold_assert(this->strtab_size_ != 0);
ae3e2ab1
ILT
473 // Quiet the compiler in opt mode.
474 if (bufsize < static_cast<size_t>(this->strtab_size_))
475 gold_assert(bufsize >= static_cast<size_t>(this->strtab_size_));
b8e6aad9 476 if (this->zero_null_)
9a0910c3 477 buffer[0] = '\0';
b8e6aad9 478 for (typename String_set_type::const_iterator p = this->string_set_.begin();
61ba1cf9
ILT
479 p != this->string_set_.end();
480 ++p)
9a0910c3 481 {
987cc251 482 const int len = (p->first.length + 1) * sizeof(Stringpool_char);
9a0910c3 483 gold_assert(p->second.second + len <= this->strtab_size_);
987cc251 484 memcpy(buffer + p->second.second, 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.090068 seconds and 4 git commands to generate.