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),
43 template<typename Stringpool_char
>
44 Stringpool_template
<Stringpool_char
>::~Stringpool_template()
46 for (typename
std::list
<Stringdata
*>::iterator p
= this->strings_
.begin();
47 p
!= this->strings_
.end();
49 delete[] reinterpret_cast<char*>(*p
);
52 // Return the length of a string of arbitrary character type.
54 template<typename Stringpool_char
>
56 Stringpool_template
<Stringpool_char
>::string_length(const Stringpool_char
* p
)
64 // Specialize string_length for char. Maybe we could just use
65 // std::char_traits<>::length?
69 Stringpool_template
<char>::string_length(const char* p
)
74 // Equality comparison function.
76 template<typename Stringpool_char
>
78 Stringpool_template
<Stringpool_char
>::Stringpool_eq::operator()(
79 const Stringpool_char
* s1
,
80 const Stringpool_char
* s2
) const
88 // Specialize equality comparison for char.
92 Stringpool_template
<char>::Stringpool_eq::operator()(const char* s1
,
95 return strcmp(s1
, s2
) == 0;
100 template<typename Stringpool_char
>
102 Stringpool_template
<Stringpool_char
>::Stringpool_hash::operator()(
103 const Stringpool_char
* s
) const
105 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
106 if (sizeof(size_t) == 8)
108 size_t result
= static_cast<size_t>(14695981039346656037ULL);
111 const char* p
= reinterpret_cast<const char*>(s
);
112 for (size_t i
= 0; i
< sizeof(Stringpool_char
); ++i
)
114 result
&= (size_t) *p
++;
115 result
*= 1099511628211ULL;
123 size_t result
= 2166136261UL;
126 const char* p
= reinterpret_cast<const char*>(s
);
127 for (size_t i
= 0; i
< sizeof(Stringpool_char
); ++i
)
129 result
^= (size_t) *p
++;
130 result
*= 16777619UL;
138 // Add a string to the list of canonical strings. Return a pointer to
139 // the canonical string. If PKEY is not NULL, set *PKEY to the key.
141 template<typename Stringpool_char
>
142 const Stringpool_char
*
143 Stringpool_template
<Stringpool_char
>::add_string(const Stringpool_char
* s
,
146 // We are in trouble if we've already computed the string offsets.
147 gold_assert(this->strtab_size_
== 0);
149 // The size we allocate for a new Stringdata.
150 const size_t buffer_size
= 1000;
151 // The amount we multiply the Stringdata index when calculating the
153 const size_t key_mult
= 1024;
154 gold_assert(key_mult
>= buffer_size
);
156 size_t len
= (string_length(s
) + 1) * sizeof(Stringpool_char
);
160 if (len
> buffer_size
)
162 alc
= sizeof(Stringdata
) + len
;
165 else if (this->strings_
.empty())
166 alc
= sizeof(Stringdata
) + buffer_size
;
169 Stringdata
*psd
= this->strings_
.front();
170 if (len
> psd
->alc
- psd
->len
)
171 alc
= sizeof(Stringdata
) + buffer_size
;
174 char* ret
= psd
->data
+ psd
->len
;
178 *pkey
= psd
->index
* key_mult
+ psd
->len
;
182 return reinterpret_cast<const Stringpool_char
*>(ret
);
186 Stringdata
*psd
= reinterpret_cast<Stringdata
*>(new char[alc
]);
187 psd
->alc
= alc
- sizeof(Stringdata
);
188 memcpy(psd
->data
, s
, len
);
190 psd
->index
= this->next_index_
;
194 *pkey
= psd
->index
* key_mult
;
197 this->strings_
.push_front(psd
);
199 this->strings_
.push_back(psd
);
201 return reinterpret_cast<const Stringpool_char
*>(psd
->data
);
204 // Add a string to a string pool.
206 template<typename Stringpool_char
>
207 const Stringpool_char
*
208 Stringpool_template
<Stringpool_char
>::add(const Stringpool_char
* s
, Key
* pkey
)
210 // FIXME: This will look up the entry twice in the hash table. The
211 // problem is that we can't insert S before we canonicalize it. I
212 // don't think there is a way to handle this correctly with
213 // unordered_map, so this should be replaced with custom code to do
214 // what we need, which is to return the empty slot.
216 typename
String_set_type::const_iterator p
= this->string_set_
.find(s
);
217 if (p
!= this->string_set_
.end())
220 *pkey
= p
->second
.first
;
225 const Stringpool_char
* ret
= this->add_string(s
, &k
);
227 const off_t ozero
= 0;
228 std::pair
<const Stringpool_char
*, Val
> element(ret
,
229 std::make_pair(k
, ozero
));
230 std::pair
<typename
String_set_type::iterator
, bool> ins
=
231 this->string_set_
.insert(element
);
232 gold_assert(ins
.second
);
240 // Add a prefix of a string to a string pool.
242 template<typename Stringpool_char
>
243 const Stringpool_char
*
244 Stringpool_template
<Stringpool_char
>::add(const Stringpool_char
* s
, size_t len
,
247 // FIXME: This implementation should be rewritten when we rewrite
248 // the hash table to avoid copying.
249 std::basic_string
<Stringpool_char
> st(s
, len
);
250 return this->add(st
, pkey
);
253 template<typename Stringpool_char
>
254 const Stringpool_char
*
255 Stringpool_template
<Stringpool_char
>::find(const Stringpool_char
* s
,
258 typename
String_set_type::const_iterator p
= this->string_set_
.find(s
);
259 if (p
== this->string_set_
.end())
263 *pkey
= p
->second
.first
;
268 // Comparison routine used when sorting into an ELF strtab. We want
269 // to sort this so that when one string is a suffix of another, we
270 // always see the shorter string immediately after the longer string.
271 // For example, we want to see these strings in this order:
275 // When strings are not suffixes, we don't care what order they are
276 // in, but we need to ensure that suffixes wind up next to each other.
277 // So we do a reversed lexicographic sort on the reversed string.
279 template<typename Stringpool_char
>
281 Stringpool_template
<Stringpool_char
>::Stringpool_sort_comparison::operator()(
282 const Stringpool_sort_info
& sort_info1
,
283 const Stringpool_sort_info
& sort_info2
) const
285 const Stringpool_char
* s1
= sort_info1
.it
->first
;
286 const Stringpool_char
* s2
= sort_info2
.it
->first
;
287 const size_t len1
= sort_info1
.string_length
;
288 const size_t len2
= sort_info2
.string_length
;
289 const size_t minlen
= len1
< len2
? len1
: len2
;
290 const Stringpool_char
* p1
= s1
+ len1
- 1;
291 const Stringpool_char
* p2
= s2
+ len2
- 1;
292 for (size_t i
= minlen
; i
> 0; --i
, --p1
, --p2
)
300 // Return whether s1 is a suffix of s2.
302 template<typename Stringpool_char
>
304 Stringpool_template
<Stringpool_char
>::is_suffix(const Stringpool_char
* s1
,
306 const Stringpool_char
* s2
,
311 return memcmp(s1
, s2
+ len2
- len1
, len1
* sizeof(Stringpool_char
)) == 0;
314 // Turn the stringpool into an ELF strtab: determine the offsets of
315 // each string in the table.
317 template<typename Stringpool_char
>
319 Stringpool_template
<Stringpool_char
>::set_string_offsets()
321 if (this->strtab_size_
!= 0)
323 // We've already computed the offsets.
327 const size_t charsize
= sizeof(Stringpool_char
);
329 // Offset 0 may be reserved for the empty string.
330 off_t offset
= this->zero_null_
? charsize
: 0;
332 // Sorting to find suffixes can take over 25% of the total CPU time
333 // used by the linker. Since it's merely an optimization to reduce
334 // the strtab size, and gives a relatively small benefit (it's
335 // typically rare for a symbol to be a suffix of another), we only
336 // take the time to sort when the user asks for heavy optimization.
337 if (parameters
->optimization_level() < 2)
339 for (typename
String_set_type::iterator curr
= this->string_set_
.begin();
340 curr
!= this->string_set_
.end();
343 if (this->zero_null_
&& curr
->first
[0] == 0)
344 curr
->second
.second
= 0;
347 curr
->second
.second
= offset
;
348 offset
+= (string_length(curr
->first
) + 1) * charsize
;
354 size_t count
= this->string_set_
.size();
356 std::vector
<Stringpool_sort_info
> v
;
359 for (typename
String_set_type::iterator p
= this->string_set_
.begin();
360 p
!= this->string_set_
.end();
362 v
.push_back(Stringpool_sort_info(p
, string_length(p
->first
)));
364 std::sort(v
.begin(), v
.end(), Stringpool_sort_comparison());
366 for (typename
std::vector
<Stringpool_sort_info
>::iterator last
= v
.end(),
371 if (this->zero_null_
&& curr
->it
->first
[0] == 0)
372 curr
->it
->second
.second
= 0;
373 else if (last
!= v
.end()
374 && is_suffix(curr
->it
->first
, curr
->string_length
,
375 last
->it
->first
, last
->string_length
))
376 curr
->it
->second
.second
= (last
->it
->second
.second
377 + ((last
->string_length
378 - curr
->string_length
)
382 curr
->it
->second
.second
= offset
;
383 offset
+= (curr
->string_length
+ 1) * charsize
;
388 this->strtab_size_
= offset
;
391 // Get the offset of a string in the ELF strtab. The string must
394 template<typename Stringpool_char
>
396 Stringpool_template
<Stringpool_char
>::get_offset(const Stringpool_char
* s
)
399 gold_assert(this->strtab_size_
!= 0);
400 typename
String_set_type::const_iterator p
= this->string_set_
.find(s
);
401 if (p
!= this->string_set_
.end())
402 return p
->second
.second
;
406 // Write the ELF strtab into the output file at the specified offset.
408 template<typename Stringpool_char
>
410 Stringpool_template
<Stringpool_char
>::write(Output_file
* of
, off_t offset
)
412 gold_assert(this->strtab_size_
!= 0);
413 unsigned char* viewu
= of
->get_output_view(offset
, this->strtab_size_
);
414 char* view
= reinterpret_cast<char*>(viewu
);
415 if (this->zero_null_
)
417 for (typename
String_set_type::const_iterator p
= this->string_set_
.begin();
418 p
!= this->string_set_
.end();
420 memcpy(view
+ p
->second
.second
, p
->first
,
421 (string_length(p
->first
) + 1) * sizeof(Stringpool_char
));
422 of
->write_output_view(offset
, this->strtab_size_
, viewu
);
425 // Instantiate the templates we need.
428 class Stringpool_template
<char>;
431 class Stringpool_template
<uint16_t>;
434 class Stringpool_template
<uint32_t>;
436 } // End namespace gold.