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