1 // stringpool.cc -- a string pool for gold
10 #include "parameters.h"
11 #include "stringpool.h"
16 template<typename Stringpool_char
>
17 Stringpool_template
<Stringpool_char
>::Stringpool_template()
18 : string_set_(), strings_(), strtab_size_(0), next_index_(1),
23 template<typename Stringpool_char
>
24 Stringpool_template
<Stringpool_char
>::~Stringpool_template()
26 for (typename
std::list
<Stringdata
*>::iterator p
= this->strings_
.begin();
27 p
!= this->strings_
.end();
29 delete[] reinterpret_cast<char*>(*p
);
32 // Return the length of a string of arbitrary character type.
34 template<typename Stringpool_char
>
36 Stringpool_template
<Stringpool_char
>::string_length(const Stringpool_char
* p
)
44 // Specialize string_length for char. Maybe we could just use
45 // std::char_traits<>::length?
49 Stringpool_template
<char>::string_length(const char* p
)
54 // Equality comparison function.
56 template<typename Stringpool_char
>
58 Stringpool_template
<Stringpool_char
>::Stringpool_eq::operator()(
59 const Stringpool_char
* s1
,
60 const Stringpool_char
* s2
) const
68 // Specialize equality comparison for char.
72 Stringpool_template
<char>::Stringpool_eq::operator()(const char* s1
,
75 return strcmp(s1
, s2
) == 0;
80 template<typename Stringpool_char
>
82 Stringpool_template
<Stringpool_char
>::Stringpool_hash::operator()(
83 const Stringpool_char
* s
) const
85 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
86 if (sizeof(size_t) == 8)
88 size_t result
= static_cast<size_t>(14695981039346656037ULL);
91 const char* p
= reinterpret_cast<const char*>(s
);
92 for (size_t i
= 0; i
< sizeof(Stringpool_char
); ++i
)
94 result
&= (size_t) *p
++;
95 result
*= 1099511628211ULL;
103 size_t result
= 2166136261UL;
106 const char* p
= reinterpret_cast<const char*>(s
);
107 for (size_t i
= 0; i
< sizeof(Stringpool_char
); ++i
)
109 result
^= (size_t) *p
++;
110 result
*= 16777619UL;
118 // Add a string to the list of canonical strings. Return a pointer to
119 // the canonical string. If PKEY is not NULL, set *PKEY to the key.
121 template<typename Stringpool_char
>
122 const Stringpool_char
*
123 Stringpool_template
<Stringpool_char
>::add_string(const Stringpool_char
* s
,
126 // We are in trouble if we've already computed the string offsets.
127 gold_assert(this->strtab_size_
== 0);
129 // The size we allocate for a new Stringdata.
130 const size_t buffer_size
= 1000;
131 // The amount we multiply the Stringdata index when calculating the
133 const size_t key_mult
= 1024;
134 gold_assert(key_mult
>= buffer_size
);
136 size_t len
= (string_length(s
) + 1) * sizeof(Stringpool_char
);
140 if (len
> buffer_size
)
142 alc
= sizeof(Stringdata
) + len
;
145 else if (this->strings_
.empty())
146 alc
= sizeof(Stringdata
) + buffer_size
;
149 Stringdata
*psd
= this->strings_
.front();
150 if (len
> psd
->alc
- psd
->len
)
151 alc
= sizeof(Stringdata
) + buffer_size
;
154 char* ret
= psd
->data
+ psd
->len
;
158 *pkey
= psd
->index
* key_mult
+ psd
->len
;
162 return reinterpret_cast<const Stringpool_char
*>(ret
);
166 Stringdata
*psd
= reinterpret_cast<Stringdata
*>(new char[alc
]);
167 psd
->alc
= alc
- sizeof(Stringdata
);
168 memcpy(psd
->data
, s
, len
);
170 psd
->index
= this->next_index_
;
174 *pkey
= psd
->index
* key_mult
;
177 this->strings_
.push_front(psd
);
179 this->strings_
.push_back(psd
);
181 return reinterpret_cast<const Stringpool_char
*>(psd
->data
);
184 // Add a string to a string pool.
186 template<typename Stringpool_char
>
187 const Stringpool_char
*
188 Stringpool_template
<Stringpool_char
>::add(const Stringpool_char
* s
, Key
* pkey
)
190 // FIXME: This will look up the entry twice in the hash table. The
191 // problem is that we can't insert S before we canonicalize it. I
192 // don't think there is a way to handle this correctly with
193 // unordered_map, so this should be replaced with custom code to do
194 // what we need, which is to return the empty slot.
196 typename
String_set_type::const_iterator p
= this->string_set_
.find(s
);
197 if (p
!= this->string_set_
.end())
200 *pkey
= p
->second
.first
;
205 const Stringpool_char
* ret
= this->add_string(s
, &k
);
207 const off_t ozero
= 0;
208 std::pair
<const Stringpool_char
*, Val
> element(ret
,
209 std::make_pair(k
, ozero
));
210 std::pair
<typename
String_set_type::iterator
, bool> ins
=
211 this->string_set_
.insert(element
);
212 gold_assert(ins
.second
);
220 // Add a prefix of a string to a string pool.
222 template<typename Stringpool_char
>
223 const Stringpool_char
*
224 Stringpool_template
<Stringpool_char
>::add(const Stringpool_char
* s
, size_t len
,
227 // FIXME: This implementation should be rewritten when we rewrite
228 // the hash table to avoid copying.
229 std::basic_string
<Stringpool_char
> st(s
, len
);
230 return this->add(st
, pkey
);
233 template<typename Stringpool_char
>
234 const Stringpool_char
*
235 Stringpool_template
<Stringpool_char
>::find(const Stringpool_char
* s
,
238 typename
String_set_type::const_iterator p
= this->string_set_
.find(s
);
239 if (p
== this->string_set_
.end())
243 *pkey
= p
->second
.first
;
248 // Comparison routine used when sorting into an ELF strtab. We want
249 // to sort this so that when one string is a suffix of another, we
250 // always see the shorter string immediately after the longer string.
251 // For example, we want to see these strings in this order:
255 // When strings are not suffixes, we don't care what order they are
256 // in, but we need to ensure that suffixes wind up next to each other.
257 // So we do a reversed lexicographic sort on the reversed string.
259 template<typename Stringpool_char
>
261 Stringpool_template
<Stringpool_char
>::Stringpool_sort_comparison::operator()(
262 const Stringpool_sort_info
& sort_info1
,
263 const Stringpool_sort_info
& sort_info2
) const
265 const Stringpool_char
* s1
= sort_info1
.it
->first
;
266 const Stringpool_char
* s2
= sort_info2
.it
->first
;
267 const size_t len1
= sort_info1
.string_length
;
268 const size_t len2
= sort_info2
.string_length
;
269 const size_t minlen
= len1
< len2
? len1
: len2
;
270 const Stringpool_char
* p1
= s1
+ len1
- 1;
271 const Stringpool_char
* p2
= s2
+ len2
- 1;
272 for (size_t i
= minlen
; i
> 0; --i
, --p1
, --p2
)
280 // Return whether s1 is a suffix of s2.
282 template<typename Stringpool_char
>
284 Stringpool_template
<Stringpool_char
>::is_suffix(const Stringpool_char
* s1
,
286 const Stringpool_char
* s2
,
291 return memcmp(s1
, s2
+ len2
- len1
, len1
* sizeof(Stringpool_char
)) == 0;
294 // Turn the stringpool into an ELF strtab: determine the offsets of
295 // each string in the table.
297 template<typename Stringpool_char
>
299 Stringpool_template
<Stringpool_char
>::set_string_offsets()
301 if (this->strtab_size_
!= 0)
303 // We've already computed the offsets.
307 const size_t charsize
= sizeof(Stringpool_char
);
309 // Offset 0 may be reserved for the empty string.
310 off_t offset
= this->zero_null_
? charsize
: 0;
312 // Sorting to find suffixes can take over 25% of the total CPU time
313 // used by the linker. Since it's merely an optimization to reduce
314 // the strtab size, and gives a relatively small benefit (it's
315 // typically rare for a symbol to be a suffix of another), we only
316 // take the time to sort when the user asks for heavy optimization.
317 if (parameters
->optimization_level() < 2)
319 for (typename
String_set_type::iterator curr
= this->string_set_
.begin();
320 curr
!= this->string_set_
.end();
323 if (this->zero_null_
&& curr
->first
[0] == 0)
324 curr
->second
.second
= 0;
327 curr
->second
.second
= offset
;
328 offset
+= (string_length(curr
->first
) + 1) * charsize
;
334 size_t count
= this->string_set_
.size();
336 std::vector
<Stringpool_sort_info
> v
;
339 for (typename
String_set_type::iterator p
= this->string_set_
.begin();
340 p
!= this->string_set_
.end();
342 v
.push_back(Stringpool_sort_info(p
, string_length(p
->first
)));
344 std::sort(v
.begin(), v
.end(), Stringpool_sort_comparison());
346 for (typename
std::vector
<Stringpool_sort_info
>::iterator last
= v
.end(),
351 if (this->zero_null_
&& curr
->it
->first
[0] == 0)
352 curr
->it
->second
.second
= 0;
353 else if (last
!= v
.end()
354 && is_suffix(curr
->it
->first
, curr
->string_length
,
355 last
->it
->first
, last
->string_length
))
356 curr
->it
->second
.second
= (last
->it
->second
.second
357 + ((last
->string_length
358 - curr
->string_length
)
362 curr
->it
->second
.second
= offset
;
363 offset
+= (curr
->string_length
+ 1) * charsize
;
368 this->strtab_size_
= offset
;
371 // Get the offset of a string in the ELF strtab. The string must
374 template<typename Stringpool_char
>
376 Stringpool_template
<Stringpool_char
>::get_offset(const Stringpool_char
* s
)
379 gold_assert(this->strtab_size_
!= 0);
380 typename
String_set_type::const_iterator p
= this->string_set_
.find(s
);
381 if (p
!= this->string_set_
.end())
382 return p
->second
.second
;
386 // Write the ELF strtab into the output file at the specified offset.
388 template<typename Stringpool_char
>
390 Stringpool_template
<Stringpool_char
>::write(Output_file
* of
, off_t offset
)
392 gold_assert(this->strtab_size_
!= 0);
393 unsigned char* viewu
= of
->get_output_view(offset
, this->strtab_size_
);
394 char* view
= reinterpret_cast<char*>(viewu
);
395 if (this->zero_null_
)
397 for (typename
String_set_type::const_iterator p
= this->string_set_
.begin();
398 p
!= this->string_set_
.end();
400 memcpy(view
+ p
->second
.second
, p
->first
,
401 (string_length(p
->first
) + 1) * sizeof(Stringpool_char
));
402 of
->write_output_view(offset
, this->strtab_size_
, viewu
);
405 // Instantiate the templates we need.
408 class Stringpool_template
<char>;
411 class Stringpool_template
<uint16_t>;
414 class Stringpool_template
<uint32_t>;
416 } // End namespace gold.