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_(), strings_(), strtab_size_(0), next_index_(1),
39 next_uncopied_key_(-1), zero_null_(true)
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->string_set_
.clear();
55 template<typename Stringpool_char
>
56 Stringpool_template
<Stringpool_char
>::~Stringpool_template()
61 // Return the length of a string of arbitrary character type.
63 template<typename Stringpool_char
>
65 Stringpool_template
<Stringpool_char
>::string_length(const Stringpool_char
* p
)
73 // Specialize string_length for char. Maybe we could just use
74 // std::char_traits<>::length?
78 Stringpool_template
<char>::string_length(const char* p
)
83 // Compare two strings of arbitrary character type for equality.
85 template<typename Stringpool_char
>
87 Stringpool_template
<Stringpool_char
>::string_equal(const Stringpool_char
* s1
,
88 const Stringpool_char
* s2
)
96 // Specialize string_equal for char.
100 Stringpool_template
<char>::string_equal(const char* s1
, const char* s2
)
102 return strcmp(s1
, s2
) == 0;
105 // Equality comparison function for the hash table.
107 template<typename Stringpool_char
>
109 Stringpool_template
<Stringpool_char
>::Stringpool_eq::operator()(
111 const Hashkey
& h2
) const
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);
119 // Hash function. The length is in characters, not bytes.
121 template<typename Stringpool_char
>
123 Stringpool_template
<Stringpool_char
>::string_hash(const Stringpool_char
* s
,
126 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
127 const unsigned char* p
= reinterpret_cast<const unsigned char*>(s
);
128 if (sizeof(size_t) > 4)
130 size_t result
= static_cast<size_t>(14695981039346656037ULL);
131 for (size_t i
= 0; i
< length
* sizeof(Stringpool_char
); ++i
)
133 result
^= static_cast<size_t>(*p
++);
134 result
*= 1099511628211ULL;
140 size_t result
= 2166136261UL;
141 for (size_t i
= 0; i
< length
* sizeof(Stringpool_char
); ++i
)
143 result
^= static_cast<size_t>(*p
++);
144 result
*= 16777619UL;
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.
155 template<typename Stringpool_char
>
156 const Stringpool_char
*
157 Stringpool_template
<Stringpool_char
>::add_string(const Stringpool_char
* s
,
161 // We are in trouble if we've already computed the string offsets.
162 gold_assert(this->strtab_size_
== 0);
164 // The size we allocate for a new Stringdata.
165 const size_t buffer_size
= 1000;
166 // The amount we multiply the Stringdata index when calculating the
168 const size_t key_mult
= 1024;
169 gold_assert(key_mult
>= buffer_size
);
171 // Convert len to the number of bytes we need to allocate, including
172 // the null character.
173 len
= (len
+ 1) * sizeof(Stringpool_char
);
177 if (len
> buffer_size
)
179 alc
= sizeof(Stringdata
) + len
;
182 else if (this->strings_
.empty())
183 alc
= sizeof(Stringdata
) + buffer_size
;
186 Stringdata
*psd
= this->strings_
.front();
187 if (len
> psd
->alc
- psd
->len
)
188 alc
= sizeof(Stringdata
) + buffer_size
;
191 char* ret
= psd
->data
+ psd
->len
;
192 memcpy(ret
, s
, len
- sizeof(Stringpool_char
));
193 memset(ret
+ len
- sizeof(Stringpool_char
), 0,
194 sizeof(Stringpool_char
));
197 *pkey
= psd
->index
* key_mult
+ psd
->len
;
201 return reinterpret_cast<const Stringpool_char
*>(ret
);
205 Stringdata
*psd
= reinterpret_cast<Stringdata
*>(new char[alc
]);
206 psd
->alc
= alc
- sizeof(Stringdata
);
207 memcpy(psd
->data
, s
, len
- sizeof(Stringpool_char
));
208 memset(psd
->data
+ len
- sizeof(Stringpool_char
), 0,
209 sizeof(Stringpool_char
));
211 psd
->index
= this->next_index_
;
215 *pkey
= psd
->index
* key_mult
;
218 this->strings_
.push_front(psd
);
220 this->strings_
.push_back(psd
);
222 return reinterpret_cast<const Stringpool_char
*>(psd
->data
);
225 // Add a string to a string pool.
227 template<typename Stringpool_char
>
228 const Stringpool_char
*
229 Stringpool_template
<Stringpool_char
>::add(const Stringpool_char
* s
, bool copy
,
232 typedef std::pair
<typename
String_set_type::iterator
, bool> Insert_type
;
236 // When we don't need to copy the string, we can call insert
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
));
244 Insert_type ins
= this->string_set_
.insert(element
);
246 typename
String_set_type::const_iterator p
= ins
.first
;
250 // We just added the string. The key value has now been
252 --this->next_uncopied_key_
;
256 gold_assert(k
!= p
->second
.first
);
260 *pkey
= p
->second
.first
;
261 return p
->first
.string
;
264 return this->add_prefix(s
, string_length(s
), pkey
);
267 // Add a prefix of a string to a string pool.
269 template<typename Stringpool_char
>
270 const Stringpool_char
*
271 Stringpool_template
<Stringpool_char
>::add_prefix(const Stringpool_char
* s
,
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
282 typename
String_set_type::const_iterator p
= this->string_set_
.find(hk
);
283 if (p
!= this->string_set_
.end())
286 *pkey
= p
->second
.first
;
287 return p
->first
.string
;
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.
295 const off_t ozero
= 0;
296 std::pair
<Hashkey
, Hashval
> element(hk
, std::make_pair(k
, ozero
));
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
);
307 template<typename Stringpool_char
>
308 const Stringpool_char
*
309 Stringpool_template
<Stringpool_char
>::find(const Stringpool_char
* s
,
313 typename
String_set_type::const_iterator p
= this->string_set_
.find(hk
);
314 if (p
== this->string_set_
.end())
318 *pkey
= p
->second
.first
;
320 return p
->first
.string
;
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:
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.
334 template<typename Stringpool_char
>
336 Stringpool_template
<Stringpool_char
>::Stringpool_sort_comparison::operator()(
337 const Stringpool_sort_info
& sort_info1
,
338 const Stringpool_sort_info
& sort_info2
) const
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
;
346 const size_t minlen
= len1
< len2
? len1
: len2
;
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
)
357 // Return whether s1 is a suffix of s2.
359 template<typename Stringpool_char
>
361 Stringpool_template
<Stringpool_char
>::is_suffix(const Stringpool_char
* s1
,
363 const Stringpool_char
* s2
,
368 return memcmp(s1
, s2
+ len2
- len1
, len1
* sizeof(Stringpool_char
)) == 0;
371 // Turn the stringpool into an ELF strtab: determine the offsets of
372 // each string in the table.
374 template<typename Stringpool_char
>
376 Stringpool_template
<Stringpool_char
>::set_string_offsets()
378 if (this->strtab_size_
!= 0)
380 // We've already computed the offsets.
384 const size_t charsize
= sizeof(Stringpool_char
);
386 // Offset 0 may be reserved for the empty string.
387 off_t offset
= this->zero_null_
? charsize
: 0;
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)
396 for (typename
String_set_type::iterator curr
= this->string_set_
.begin();
397 curr
!= this->string_set_
.end();
400 if (this->zero_null_
&& curr
->first
.string
[0] == 0)
401 curr
->second
.second
= 0;
404 curr
->second
.second
= offset
;
405 offset
+= (curr
->first
.length
+ 1) * charsize
;
411 size_t count
= this->string_set_
.size();
413 std::vector
<Stringpool_sort_info
> v
;
416 for (typename
String_set_type::iterator p
= this->string_set_
.begin();
417 p
!= this->string_set_
.end();
419 v
.push_back(Stringpool_sort_info(p
));
421 std::sort(v
.begin(), v
.end(), Stringpool_sort_comparison());
423 for (typename
std::vector
<Stringpool_sort_info
>::iterator last
= v
.end(),
428 if (this->zero_null_
&& (*curr
)->first
.string
[0] == 0)
429 (*curr
)->second
.second
= 0;
430 else if (last
!= v
.end()
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
)
441 (*curr
)->second
.second
= offset
;
442 offset
+= ((*curr
)->first
.length
+ 1) * charsize
;
447 this->strtab_size_
= offset
;
450 // Get the offset of a string in the ELF strtab. The string must
453 template<typename Stringpool_char
>
455 Stringpool_template
<Stringpool_char
>::get_offset(const Stringpool_char
* s
)
458 gold_assert(this->strtab_size_
!= 0);
459 typename
String_set_type::const_iterator p
= this->string_set_
.find(s
);
460 if (p
!= this->string_set_
.end())
461 return p
->second
.second
;
465 // Write the ELF strtab into the buffer.
467 template<typename Stringpool_char
>
469 Stringpool_template
<Stringpool_char
>::write_to_buffer(unsigned char* buffer
,
472 gold_assert(this->strtab_size_
!= 0);
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_
));
476 if (this->zero_null_
)
478 for (typename
String_set_type::const_iterator p
= this->string_set_
.begin();
479 p
!= this->string_set_
.end();
482 const int len
= (p
->first
.length
+ 1) * sizeof(Stringpool_char
);
483 gold_assert(p
->second
.second
+ len
<= this->strtab_size_
);
484 memcpy(buffer
+ p
->second
.second
, p
->first
.string
, len
);
488 // Write the ELF strtab into the output file at the specified offset.
490 template<typename Stringpool_char
>
492 Stringpool_template
<Stringpool_char
>::write(Output_file
* of
, off_t offset
)
494 gold_assert(this->strtab_size_
!= 0);
495 unsigned char* view
= of
->get_output_view(offset
, this->strtab_size_
);
496 this->write_to_buffer(view
, this->strtab_size_
);
497 of
->write_output_view(offset
, this->strtab_size_
, view
);
500 // Print statistical information to stderr. This is used for --stats.
502 template<typename Stringpool_char
>
504 Stringpool_template
<Stringpool_char
>::print_stats(const char* name
) const
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());
511 fprintf(stderr
, _("%s: %s entries: %zu\n"),
512 program_name
, name
, this->table_
.size());
514 fprintf(stderr
, _("%s: %s Stringdata structures: %zu\n"),
515 program_name
, name
, this->strings_
.size());
518 // Instantiate the templates we need.
521 class Stringpool_template
<char>;
524 class Stringpool_template
<uint16_t>;
527 class Stringpool_template
<uint32_t>;
529 } // End namespace gold.
This page took 0.042628 seconds and 4 git commands to generate.