1 // stringpool.cc -- a string pool for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
30 #include "parameters.h"
31 #include "stringpool.h"
36 template<typename Stringpool_char
>
37 Stringpool_template
<Stringpool_char
>::Stringpool_template()
38 : string_set_(), key_to_offset_(), strings_(), strtab_size_(0),
43 template<typename Stringpool_char
>
45 Stringpool_template
<Stringpool_char
>::clear()
47 for (typename
std::list
<Stringdata
*>::iterator p
= this->strings_
.begin();
48 p
!= this->strings_
.end();
50 delete[] reinterpret_cast<char*>(*p
);
51 this->strings_
.clear();
52 this->key_to_offset_
.clear();
53 this->string_set_
.clear();
56 template<typename Stringpool_char
>
57 Stringpool_template
<Stringpool_char
>::~Stringpool_template()
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.
67 template<typename Stringpool_char
>
69 Stringpool_template
<Stringpool_char
>::reserve(unsigned int n
)
71 this->key_to_offset_
.reserve(n
);
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);
77 #elif defined(HAVE_EXT_HASH_MAP)
78 this->string_set_
.resize(this->string_set_
.size() + n
);
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
);
88 // Return the length of a string of arbitrary character type.
90 template<typename Stringpool_char
>
92 Stringpool_template
<Stringpool_char
>::string_length(const Stringpool_char
* p
)
100 // Specialize string_length for char. Maybe we could just use
101 // std::char_traits<>::length?
105 Stringpool_template
<char>::string_length(const char* p
)
110 // Compare two strings of arbitrary character type for equality.
112 template<typename Stringpool_char
>
114 Stringpool_template
<Stringpool_char
>::string_equal(const Stringpool_char
* s1
,
115 const Stringpool_char
* s2
)
123 // Specialize string_equal for char.
127 Stringpool_template
<char>::string_equal(const char* s1
, const char* s2
)
129 return strcmp(s1
, s2
) == 0;
132 // Equality comparison function for the hash table.
134 template<typename Stringpool_char
>
136 Stringpool_template
<Stringpool_char
>::Stringpool_eq::operator()(
138 const Hashkey
& h2
) const
140 return (h1
.hash_code
== h2
.hash_code
141 && h1
.length
== h2
.length
142 && (h1
.string
== h2
.string
143 || memcmp(h1
.string
, h2
.string
,
144 h1
.length
* sizeof(Stringpool_char
)) == 0));
147 // Hash function. The length is in characters, not bytes.
149 template<typename Stringpool_char
>
151 Stringpool_template
<Stringpool_char
>::string_hash(const Stringpool_char
* s
,
154 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
155 const unsigned char* p
= reinterpret_cast<const unsigned char*>(s
);
156 if (sizeof(size_t) > 4)
158 size_t result
= static_cast<size_t>(14695981039346656037ULL);
159 for (size_t i
= 0; i
< length
* sizeof(Stringpool_char
); ++i
)
161 result
^= static_cast<size_t>(*p
++);
162 result
*= 1099511628211ULL;
168 size_t result
= 2166136261UL;
169 for (size_t i
= 0; i
< length
* sizeof(Stringpool_char
); ++i
)
171 result
^= static_cast<size_t>(*p
++);
172 result
*= 16777619UL;
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.
183 template<typename Stringpool_char
>
184 const Stringpool_char
*
185 Stringpool_template
<Stringpool_char
>::add_string(const Stringpool_char
* s
,
188 // We are in trouble if we've already computed the string offsets.
189 gold_assert(this->strtab_size_
== 0);
191 // The size we allocate for a new Stringdata.
192 const size_t buffer_size
= 1000;
193 // The amount we multiply the Stringdata index when calculating the
195 const size_t key_mult
= 1024;
196 gold_assert(key_mult
>= buffer_size
);
198 // Convert len to the number of bytes we need to allocate, including
199 // the null character.
200 len
= (len
+ 1) * sizeof(Stringpool_char
);
204 if (len
> buffer_size
)
206 alc
= sizeof(Stringdata
) + len
;
209 else if (this->strings_
.empty())
210 alc
= sizeof(Stringdata
) + buffer_size
;
213 Stringdata
*psd
= this->strings_
.front();
214 if (len
> psd
->alc
- psd
->len
)
215 alc
= sizeof(Stringdata
) + buffer_size
;
218 char* ret
= psd
->data
+ psd
->len
;
219 memcpy(ret
, s
, len
- sizeof(Stringpool_char
));
220 memset(ret
+ len
- sizeof(Stringpool_char
), 0,
221 sizeof(Stringpool_char
));
225 return reinterpret_cast<const Stringpool_char
*>(ret
);
229 Stringdata
*psd
= reinterpret_cast<Stringdata
*>(new char[alc
]);
230 psd
->alc
= alc
- sizeof(Stringdata
);
231 memcpy(psd
->data
, s
, len
- sizeof(Stringpool_char
));
232 memset(psd
->data
+ len
- sizeof(Stringpool_char
), 0,
233 sizeof(Stringpool_char
));
237 this->strings_
.push_front(psd
);
239 this->strings_
.push_back(psd
);
241 return reinterpret_cast<const Stringpool_char
*>(psd
->data
);
244 // Add a string to a string pool.
246 template<typename Stringpool_char
>
247 const Stringpool_char
*
248 Stringpool_template
<Stringpool_char
>::add(const Stringpool_char
* s
, bool copy
,
251 return this->add_with_length(s
, string_length(s
), copy
, pkey
);
254 template<typename Stringpool_char
>
255 const Stringpool_char
*
256 Stringpool_template
<Stringpool_char
>::add_with_length(const Stringpool_char
* s
,
261 typedef std::pair
<typename
String_set_type::iterator
, bool> Insert_type
;
263 const Key k
= this->key_to_offset_
.size();
267 // When we don't need to copy the string, we can call insert
270 std::pair
<Hashkey
, Hashval
> element(Hashkey(s
, length
), k
);
272 Insert_type ins
= this->string_set_
.insert(element
);
274 typename
String_set_type::const_iterator p
= ins
.first
;
278 // We just added the string. The key value has now been
280 this->key_to_offset_
.push_back(0);
284 gold_assert(k
!= p
->second
);
289 return p
->first
.string
;
292 // When we have to copy the string, we look it up twice in the hash
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
295 // code will only be computed once.
297 Hashkey
hk(s
, length
);
298 typename
String_set_type::const_iterator p
= this->string_set_
.find(hk
);
299 if (p
!= this->string_set_
.end())
303 return p
->first
.string
;
306 this->key_to_offset_
.push_back(0);
308 hk
.string
= this->add_string(s
, length
);
309 // The contents of the string stay the same, so we don't need to
310 // adjust hk.hash_code or hk.length.
312 std::pair
<Hashkey
, Hashval
> element(hk
, k
);
314 Insert_type ins
= this->string_set_
.insert(element
);
315 gold_assert(ins
.second
);
322 template<typename Stringpool_char
>
323 const Stringpool_char
*
324 Stringpool_template
<Stringpool_char
>::find(const Stringpool_char
* s
,
328 typename
String_set_type::const_iterator p
= this->string_set_
.find(hk
);
329 if (p
== this->string_set_
.end())
335 return p
->first
.string
;
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:
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.
349 template<typename Stringpool_char
>
351 Stringpool_template
<Stringpool_char
>::Stringpool_sort_comparison::operator()(
352 const Stringpool_sort_info
& sort_info1
,
353 const Stringpool_sort_info
& sort_info2
) const
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
;
361 const size_t minlen
= len1
< len2
? len1
: len2
;
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
)
372 // Return whether s1 is a suffix of s2.
374 template<typename Stringpool_char
>
376 Stringpool_template
<Stringpool_char
>::is_suffix(const Stringpool_char
* s1
,
378 const Stringpool_char
* s2
,
383 return memcmp(s1
, s2
+ len2
- len1
, len1
* sizeof(Stringpool_char
)) == 0;
386 // Turn the stringpool into an ELF strtab: determine the offsets of
387 // each string in the table.
389 template<typename Stringpool_char
>
391 Stringpool_template
<Stringpool_char
>::set_string_offsets()
393 if (this->strtab_size_
!= 0)
395 // We've already computed the offsets.
399 const size_t charsize
= sizeof(Stringpool_char
);
401 // Offset 0 may be reserved for the empty string.
402 section_offset_type offset
= this->zero_null_
? charsize
: 0;
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)
411 for (typename
String_set_type::iterator curr
= this->string_set_
.begin();
412 curr
!= this->string_set_
.end();
415 section_offset_type
* poff
= &this->key_to_offset_
[curr
->second
];
416 if (this->zero_null_
&& curr
->first
.string
[0] == 0)
421 offset
+= (curr
->first
.length
+ 1) * charsize
;
427 size_t count
= this->string_set_
.size();
429 std::vector
<Stringpool_sort_info
> v
;
432 for (typename
String_set_type::iterator p
= this->string_set_
.begin();
433 p
!= this->string_set_
.end();
435 v
.push_back(Stringpool_sort_info(p
));
437 std::sort(v
.begin(), v
.end(), Stringpool_sort_comparison());
439 section_offset_type last_offset
= -1;
440 for (typename
std::vector
<Stringpool_sort_info
>::iterator last
= v
.end(),
445 section_offset_type this_offset
;
446 if (this->zero_null_
&& (*curr
)->first
.string
[0] == 0)
448 else if (last
!= v
.end()
449 && is_suffix((*curr
)->first
.string
,
450 (*curr
)->first
.length
,
451 (*last
)->first
.string
,
452 (*last
)->first
.length
))
453 this_offset
= (last_offset
454 + (((*last
)->first
.length
- (*curr
)->first
.length
)
458 this_offset
= offset
;
459 offset
+= ((*curr
)->first
.length
+ 1) * charsize
;
461 this->key_to_offset_
[(*curr
)->second
] = this_offset
;
462 last_offset
= this_offset
;
466 this->strtab_size_
= offset
;
469 // Get the offset of a string in the ELF strtab. The string must
472 template<typename Stringpool_char
>
474 Stringpool_template
<Stringpool_char
>::get_offset(const Stringpool_char
* s
)
477 return this->get_offset_with_length(s
, string_length(s
));
480 template<typename Stringpool_char
>
482 Stringpool_template
<Stringpool_char
>::get_offset_with_length(
483 const Stringpool_char
* s
,
486 gold_assert(this->strtab_size_
!= 0);
487 Hashkey
hk(s
, length
);
488 typename
String_set_type::const_iterator p
= this->string_set_
.find(hk
);
489 if (p
!= this->string_set_
.end())
490 return this->key_to_offset_
[p
->second
];
494 // Write the ELF strtab into the buffer.
496 template<typename Stringpool_char
>
498 Stringpool_template
<Stringpool_char
>::write_to_buffer(
499 unsigned char* buffer
,
500 section_size_type bufsize
)
502 gold_assert(this->strtab_size_
!= 0);
503 gold_assert(bufsize
>= this->strtab_size_
);
504 if (this->zero_null_
)
506 for (typename
String_set_type::const_iterator p
= this->string_set_
.begin();
507 p
!= this->string_set_
.end();
510 const int len
= (p
->first
.length
+ 1) * sizeof(Stringpool_char
);
511 const section_offset_type offset
= this->key_to_offset_
[p
->second
];
512 gold_assert(static_cast<section_size_type
>(offset
) + len
513 <= this->strtab_size_
);
514 memcpy(buffer
+ offset
, p
->first
.string
, len
);
518 // Write the ELF strtab into the output file at the specified offset.
520 template<typename Stringpool_char
>
522 Stringpool_template
<Stringpool_char
>::write(Output_file
* of
, off_t offset
)
524 gold_assert(this->strtab_size_
!= 0);
525 unsigned char* view
= of
->get_output_view(offset
, this->strtab_size_
);
526 this->write_to_buffer(view
, this->strtab_size_
);
527 of
->write_output_view(offset
, this->strtab_size_
, view
);
530 // Print statistical information to stderr. This is used for --stats.
532 template<typename Stringpool_char
>
534 Stringpool_template
<Stringpool_char
>::print_stats(const char* name
) const
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());
541 fprintf(stderr
, _("%s: %s entries: %zu\n"),
542 program_name
, name
, this->table_
.size());
544 fprintf(stderr
, _("%s: %s Stringdata structures: %zu\n"),
545 program_name
, name
, this->strings_
.size());
548 // Instantiate the templates we need.
551 class Stringpool_template
<char>;
554 class Stringpool_template
<uint16_t>;
557 class Stringpool_template
<uint32_t>;
559 } // End namespace gold.