gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gold / stringpool.cc
CommitLineData
14bfc3f5
ILT
1// stringpool.cc -- a string pool for gold
2
b3adc24a 3// Copyright (C) 2006-2020 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{
1c582fe7
CC
42 if (parameters->options_valid()
43 && parameters->options().optimize() >= 2
44 && addralign <= sizeof(Stringpool_char))
f7c8a183 45 this->optimize_ = true;
14bfc3f5
ILT
46}
47
b8e6aad9 48template<typename Stringpool_char>
9a0910c3
ILT
49void
50Stringpool_template<Stringpool_char>::clear()
14bfc3f5 51{
b8e6aad9 52 for (typename std::list<Stringdata*>::iterator p = this->strings_.begin();
14bfc3f5
ILT
53 p != this->strings_.end();
54 ++p)
55 delete[] reinterpret_cast<char*>(*p);
9a0910c3 56 this->strings_.clear();
2030fba0 57 this->key_to_offset_.clear();
9a0910c3
ILT
58 this->string_set_.clear();
59}
60
61template<typename Stringpool_char>
62Stringpool_template<Stringpool_char>::~Stringpool_template()
63{
64 this->clear();
14bfc3f5
ILT
65}
66
6d013333
ILT
67// Resize the internal hashtable with the expectation we'll get n new
68// elements. Note that the hashtable constructor takes a "number of
69// buckets you'd like," rather than "number of elements you'd like,"
70// but that's the best we can do.
71
72template<typename Stringpool_char>
73void
74Stringpool_template<Stringpool_char>::reserve(unsigned int n)
75{
2030fba0
ILT
76 this->key_to_offset_.reserve(n);
77
6c09fb0b
ILT
78#if defined(HAVE_UNORDERED_MAP)
79 this->string_set_.rehash(this->string_set_.size() + n);
80 return;
81#elif defined(HAVE_TR1_UNORDERED_MAP)
6d013333
ILT
82 // rehash() implementation is broken in gcc 4.0.3's stl
83 //this->string_set_.rehash(this->string_set_.size() + n);
84 //return;
85#elif defined(HAVE_EXT_HASH_MAP)
86 this->string_set_.resize(this->string_set_.size() + n);
87 return;
88#endif
89
90 // This is the generic "reserve" code, if no #ifdef above triggers.
91 String_set_type new_string_set(this->string_set_.size() + n);
92 new_string_set.insert(this->string_set_.begin(), this->string_set_.end());
93 this->string_set_.swap(new_string_set);
94}
95
987cc251 96// Compare two strings of arbitrary character type for equality.
b8e6aad9
ILT
97
98template<typename Stringpool_char>
99bool
987cc251
ILT
100Stringpool_template<Stringpool_char>::string_equal(const Stringpool_char* s1,
101 const Stringpool_char* s2)
b8e6aad9
ILT
102{
103 while (*s1 != 0)
104 if (*s1++ != *s2++)
105 return false;
106 return *s2 == 0;
107}
108
987cc251 109// Specialize string_equal for char.
b8e6aad9
ILT
110
111template<>
987cc251
ILT
112inline bool
113Stringpool_template<char>::string_equal(const char* s1, const char* s2)
b8e6aad9
ILT
114{
115 return strcmp(s1, s2) == 0;
116}
117
987cc251
ILT
118// Equality comparison function for the hash table.
119
120template<typename Stringpool_char>
121inline bool
122Stringpool_template<Stringpool_char>::Stringpool_eq::operator()(
123 const Hashkey& h1,
124 const Hashkey& h2) const
125{
126 return (h1.hash_code == h2.hash_code
127 && h1.length == h2.length
c0873094
ILT
128 && (h1.string == h2.string
129 || memcmp(h1.string, h2.string,
130 h1.length * sizeof(Stringpool_char)) == 0));
987cc251
ILT
131}
132
133// Hash function. The length is in characters, not bytes.
14bfc3f5 134
b8e6aad9 135template<typename Stringpool_char>
14bfc3f5 136size_t
987cc251
ILT
137Stringpool_template<Stringpool_char>::string_hash(const Stringpool_char* s,
138 size_t length)
14bfc3f5 139{
b569affa 140 return gold::string_hash<Stringpool_char>(s, length);
14bfc3f5
ILT
141}
142
987cc251
ILT
143// Add the string S to the list of canonical strings. Return a
144// pointer to the canonical string. If PKEY is not NULL, set *PKEY to
145// the key. LENGTH is the length of S in characters. Note that S may
146// not be NUL terminated.
14bfc3f5 147
b8e6aad9
ILT
148template<typename Stringpool_char>
149const Stringpool_char*
150Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
2030fba0 151 size_t len)
14bfc3f5 152{
a3ad94ed
ILT
153 // We are in trouble if we've already computed the string offsets.
154 gold_assert(this->strtab_size_ == 0);
155
f0641a0b 156 // The size we allocate for a new Stringdata.
14bfc3f5 157 const size_t buffer_size = 1000;
f0641a0b
ILT
158 // The amount we multiply the Stringdata index when calculating the
159 // key.
160 const size_t key_mult = 1024;
a3ad94ed 161 gold_assert(key_mult >= buffer_size);
f0641a0b 162
987cc251
ILT
163 // Convert len to the number of bytes we need to allocate, including
164 // the null character.
165 len = (len + 1) * sizeof(Stringpool_char);
14bfc3f5
ILT
166
167 size_t alc;
168 bool front = true;
b8e6aad9 169 if (len > buffer_size)
14bfc3f5 170 {
61ba1cf9 171 alc = sizeof(Stringdata) + len;
14bfc3f5
ILT
172 front = false;
173 }
174 else if (this->strings_.empty())
61ba1cf9 175 alc = sizeof(Stringdata) + buffer_size;
14bfc3f5
ILT
176 else
177 {
ca09d69a 178 Stringdata* psd = this->strings_.front();
b8e6aad9 179 if (len > psd->alc - psd->len)
61ba1cf9 180 alc = sizeof(Stringdata) + buffer_size;
14bfc3f5
ILT
181 else
182 {
183 char* ret = psd->data + psd->len;
987cc251
ILT
184 memcpy(ret, s, len - sizeof(Stringpool_char));
185 memset(ret + len - sizeof(Stringpool_char), 0,
186 sizeof(Stringpool_char));
f0641a0b 187
b8e6aad9 188 psd->len += len;
f0641a0b 189
b8e6aad9 190 return reinterpret_cast<const Stringpool_char*>(ret);
14bfc3f5
ILT
191 }
192 }
193
ca09d69a 194 Stringdata* psd = reinterpret_cast<Stringdata*>(new char[alc]);
61ba1cf9 195 psd->alc = alc - sizeof(Stringdata);
987cc251
ILT
196 memcpy(psd->data, s, len - sizeof(Stringpool_char));
197 memset(psd->data + len - sizeof(Stringpool_char), 0,
198 sizeof(Stringpool_char));
b8e6aad9 199 psd->len = len;
f0641a0b 200
14bfc3f5
ILT
201 if (front)
202 this->strings_.push_front(psd);
203 else
204 this->strings_.push_back(psd);
f0641a0b 205
b8e6aad9 206 return reinterpret_cast<const Stringpool_char*>(psd->data);
14bfc3f5
ILT
207}
208
209// Add a string to a string pool.
210
b8e6aad9
ILT
211template<typename Stringpool_char>
212const Stringpool_char*
cfd73a4e
ILT
213Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, bool copy,
214 Key* pkey)
c0873094
ILT
215{
216 return this->add_with_length(s, string_length(s), copy, pkey);
217}
218
1aa37384
DK
219// Add a new key offset entry.
220
221template<typename Stringpool_char>
222void
223Stringpool_template<Stringpool_char>::new_key_offset(size_t length)
224{
1aa37384
DK
225 section_offset_type offset;
226 if (this->zero_null_ && length == 0)
227 offset = 0;
228 else
229 {
6ad3daba 230 offset = this->offset_;
a817ff49
MK
231 // Align strings.
232 offset = align_address(offset, this->addralign_);
e31908b6 233 this->offset_ = offset + (length + 1) * sizeof(Stringpool_char);
1aa37384
DK
234 }
235 this->key_to_offset_.push_back(offset);
236}
237
c0873094
ILT
238template<typename Stringpool_char>
239const Stringpool_char*
240Stringpool_template<Stringpool_char>::add_with_length(const Stringpool_char* s,
241 size_t length,
242 bool copy,
243 Key* pkey)
14bfc3f5 244{
987cc251 245 typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
14bfc3f5 246
5778530e
ILT
247 // We add 1 so that 0 is always invalid.
248 const Key k = this->key_to_offset_.size() + 1;
2030fba0 249
987cc251 250 if (!copy)
f0641a0b 251 {
987cc251
ILT
252 // When we don't need to copy the string, we can call insert
253 // directly.
14bfc3f5 254
2030fba0 255 std::pair<Hashkey, Hashval> element(Hashkey(s, length), k);
f0641a0b 256
987cc251 257 Insert_type ins = this->string_set_.insert(element);
f0641a0b 258
987cc251
ILT
259 typename String_set_type::const_iterator p = ins.first;
260
261 if (ins.second)
262 {
263 // We just added the string. The key value has now been
264 // used.
1aa37384 265 this->new_key_offset(length);
987cc251
ILT
266 }
267 else
268 {
2030fba0 269 gold_assert(k != p->second);
987cc251
ILT
270 }
271
272 if (pkey != NULL)
2030fba0 273 *pkey = p->second;
987cc251
ILT
274 return p->first.string;
275 }
f0641a0b 276
c0873094 277 // When we have to copy the string, we look it up twice in the hash
987cc251
ILT
278 // table. The problem is that we can't insert S before we
279 // canonicalize it by copying it into the canonical list. The hash
c0873094 280 // code will only be computed once.
987cc251 281
c0873094 282 Hashkey hk(s, length);
987cc251
ILT
283 typename String_set_type::const_iterator p = this->string_set_.find(hk);
284 if (p != this->string_set_.end())
285 {
286 if (pkey != NULL)
2030fba0 287 *pkey = p->second;
987cc251
ILT
288 return p->first.string;
289 }
290
1aa37384 291 this->new_key_offset(length);
2030fba0
ILT
292
293 hk.string = this->add_string(s, length);
987cc251
ILT
294 // The contents of the string stay the same, so we don't need to
295 // adjust hk.hash_code or hk.length.
296
2030fba0 297 std::pair<Hashkey, Hashval> element(hk, k);
987cc251 298
987cc251
ILT
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)
2030fba0 318 *pkey = p->second;
f0641a0b 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.
8383303e 387 section_offset_type 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.
f7c8a183 394 if (!this->optimize_)
61ba1cf9 395 {
1aa37384
DK
396 // If we are not optimizing, the offsets are already assigned.
397 offset = this->offset_;
377caf49
ILT
398 }
399 else
400 {
401 size_t count = this->string_set_.size();
402
403 std::vector<Stringpool_sort_info> v;
404 v.reserve(count);
405
406 for (typename String_set_type::iterator p = this->string_set_.begin();
407 p != this->string_set_.end();
408 ++p)
987cc251 409 v.push_back(Stringpool_sort_info(p));
377caf49
ILT
410
411 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
412
2030fba0 413 section_offset_type last_offset = -1;
377caf49
ILT
414 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
415 curr = v.begin();
416 curr != v.end();
417 last = curr++)
418 {
2030fba0 419 section_offset_type this_offset;
987cc251 420 if (this->zero_null_ && (*curr)->first.string[0] == 0)
2030fba0 421 this_offset = 0;
377caf49 422 else if (last != v.end()
a817ff49
MK
423 && ((((*curr)->first.length - (*last)->first.length)
424 % this->addralign_) == 0)
987cc251
ILT
425 && is_suffix((*curr)->first.string,
426 (*curr)->first.length,
427 (*last)->first.string,
428 (*last)->first.length))
2030fba0
ILT
429 this_offset = (last_offset
430 + (((*last)->first.length - (*curr)->first.length)
431 * charsize));
377caf49
ILT
432 else
433 {
e31908b6
CC
434 this_offset = align_address(offset, this->addralign_);
435 offset = this_offset + ((*curr)->first.length + 1) * charsize;
377caf49 436 }
5778530e 437 this->key_to_offset_[(*curr)->second - 1] = this_offset;
2030fba0 438 last_offset = this_offset;
377caf49 439 }
61ba1cf9
ILT
440 }
441
442 this->strtab_size_ = offset;
443}
444
445// Get the offset of a string in the ELF strtab. The string must
446// exist.
447
b8e6aad9 448template<typename Stringpool_char>
8383303e 449section_offset_type
b8e6aad9
ILT
450Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
451 const
c0873094
ILT
452{
453 return this->get_offset_with_length(s, string_length(s));
454}
455
456template<typename Stringpool_char>
457section_offset_type
458Stringpool_template<Stringpool_char>::get_offset_with_length(
459 const Stringpool_char* s,
460 size_t length) const
61ba1cf9 461{
a3ad94ed 462 gold_assert(this->strtab_size_ != 0);
c0873094
ILT
463 Hashkey hk(s, length);
464 typename String_set_type::const_iterator p = this->string_set_.find(hk);
61ba1cf9 465 if (p != this->string_set_.end())
5778530e 466 return this->key_to_offset_[p->second - 1];
a3ad94ed 467 gold_unreachable();
61ba1cf9
ILT
468}
469
9a0910c3 470// Write the ELF strtab into the buffer.
61ba1cf9 471
b8e6aad9 472template<typename Stringpool_char>
61ba1cf9 473void
8383303e
ILT
474Stringpool_template<Stringpool_char>::write_to_buffer(
475 unsigned char* buffer,
476 section_size_type bufsize)
61ba1cf9 477{
a3ad94ed 478 gold_assert(this->strtab_size_ != 0);
8383303e 479 gold_assert(bufsize >= this->strtab_size_);
b8e6aad9 480 if (this->zero_null_)
9a0910c3 481 buffer[0] = '\0';
b8e6aad9 482 for (typename String_set_type::const_iterator p = this->string_set_.begin();
61ba1cf9
ILT
483 p != this->string_set_.end();
484 ++p)
9a0910c3 485 {
987cc251 486 const int len = (p->first.length + 1) * sizeof(Stringpool_char);
5778530e 487 const section_offset_type offset = this->key_to_offset_[p->second - 1];
2030fba0 488 gold_assert(static_cast<section_size_type>(offset) + len
8383303e 489 <= this->strtab_size_);
2030fba0 490 memcpy(buffer + offset, p->first.string, len);
9a0910c3
ILT
491 }
492}
493
494// Write the ELF strtab into the output file at the specified offset.
495
496template<typename Stringpool_char>
497void
498Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
499{
500 gold_assert(this->strtab_size_ != 0);
501 unsigned char* view = of->get_output_view(offset, this->strtab_size_);
96803768 502 this->write_to_buffer(view, this->strtab_size_);
9a0910c3 503 of->write_output_view(offset, this->strtab_size_, view);
61ba1cf9
ILT
504}
505
ad8f37d1
ILT
506// Print statistical information to stderr. This is used for --stats.
507
508template<typename Stringpool_char>
509void
510Stringpool_template<Stringpool_char>::print_stats(const char* name) const
511{
6c09fb0b 512#if defined(HAVE_UNORDERED_MAP) || defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
ad8f37d1
ILT
513 fprintf(stderr, _("%s: %s entries: %zu; buckets: %zu\n"),
514 program_name, name, this->string_set_.size(),
515 this->string_set_.bucket_count());
516#else
517 fprintf(stderr, _("%s: %s entries: %zu\n"),
518 program_name, name, this->table_.size());
519#endif
520 fprintf(stderr, _("%s: %s Stringdata structures: %zu\n"),
521 program_name, name, this->strings_.size());
522}
523
b8e6aad9
ILT
524// Instantiate the templates we need.
525
526template
527class Stringpool_template<char>;
528
529template
530class Stringpool_template<uint16_t>;
531
532template
533class Stringpool_template<uint32_t>;
534
14bfc3f5 535} // End namespace gold.
This page took 0.578887 seconds and 4 git commands to generate.