From Craig Silverstein: Only sort for suffixes with -O2.
[deliverable/binutils-gdb.git] / gold / stringpool.cc
1 // stringpool.cc -- a string pool for gold
2
3 #include "gold.h"
4
5 #include <cstring>
6 #include <algorithm>
7 #include <vector>
8
9 #include "output.h"
10 #include "parameters.h"
11 #include "stringpool.h"
12
13 namespace gold
14 {
15
16 template<typename Stringpool_char>
17 Stringpool_template<Stringpool_char>::Stringpool_template()
18 : string_set_(), strings_(), strtab_size_(0), next_index_(1),
19 zero_null_(true)
20 {
21 }
22
23 template<typename Stringpool_char>
24 Stringpool_template<Stringpool_char>::~Stringpool_template()
25 {
26 for (typename std::list<Stringdata*>::iterator p = this->strings_.begin();
27 p != this->strings_.end();
28 ++p)
29 delete[] reinterpret_cast<char*>(*p);
30 }
31
32 // Return the length of a string of arbitrary character type.
33
34 template<typename Stringpool_char>
35 size_t
36 Stringpool_template<Stringpool_char>::string_length(const Stringpool_char* p)
37 {
38 size_t len = 0;
39 for (; *p != 0; ++p)
40 ++len;
41 return len;
42 }
43
44 // Specialize string_length for char. Maybe we could just use
45 // std::char_traits<>::length?
46
47 template<>
48 inline size_t
49 Stringpool_template<char>::string_length(const char* p)
50 {
51 return strlen(p);
52 }
53
54 // Equality comparison function.
55
56 template<typename Stringpool_char>
57 bool
58 Stringpool_template<Stringpool_char>::Stringpool_eq::operator()(
59 const Stringpool_char* s1,
60 const Stringpool_char* s2) const
61 {
62 while (*s1 != 0)
63 if (*s1++ != *s2++)
64 return false;
65 return *s2 == 0;
66 }
67
68 // Specialize equality comparison for char.
69
70 template<>
71 bool
72 Stringpool_template<char>::Stringpool_eq::operator()(const char* s1,
73 const char* s2) const
74 {
75 return strcmp(s1, s2) == 0;
76 }
77
78 // Hash function.
79
80 template<typename Stringpool_char>
81 size_t
82 Stringpool_template<Stringpool_char>::Stringpool_hash::operator()(
83 const Stringpool_char* s) const
84 {
85 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
86 if (sizeof(size_t) == 8)
87 {
88 size_t result = static_cast<size_t>(14695981039346656037ULL);
89 while (*s != 0)
90 {
91 const char* p = reinterpret_cast<const char*>(s);
92 for (size_t i = 0; i < sizeof(Stringpool_char); ++i)
93 {
94 result &= (size_t) *p++;
95 result *= 1099511628211ULL;
96 }
97 ++s;
98 }
99 return result;
100 }
101 else
102 {
103 size_t result = 2166136261UL;
104 while (*s != 0)
105 {
106 const char* p = reinterpret_cast<const char*>(s);
107 for (size_t i = 0; i < sizeof(Stringpool_char); ++i)
108 {
109 result ^= (size_t) *p++;
110 result *= 16777619UL;
111 }
112 ++s;
113 }
114 return result;
115 }
116 }
117
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.
120
121 template<typename Stringpool_char>
122 const Stringpool_char*
123 Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
124 Key* pkey)
125 {
126 // We are in trouble if we've already computed the string offsets.
127 gold_assert(this->strtab_size_ == 0);
128
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
132 // key.
133 const size_t key_mult = 1024;
134 gold_assert(key_mult >= buffer_size);
135
136 size_t len = (string_length(s) + 1) * sizeof(Stringpool_char);
137
138 size_t alc;
139 bool front = true;
140 if (len > buffer_size)
141 {
142 alc = sizeof(Stringdata) + len;
143 front = false;
144 }
145 else if (this->strings_.empty())
146 alc = sizeof(Stringdata) + buffer_size;
147 else
148 {
149 Stringdata *psd = this->strings_.front();
150 if (len > psd->alc - psd->len)
151 alc = sizeof(Stringdata) + buffer_size;
152 else
153 {
154 char* ret = psd->data + psd->len;
155 memcpy(ret, s, len);
156
157 if (pkey != NULL)
158 *pkey = psd->index * key_mult + psd->len;
159
160 psd->len += len;
161
162 return reinterpret_cast<const Stringpool_char*>(ret);
163 }
164 }
165
166 Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]);
167 psd->alc = alc - sizeof(Stringdata);
168 memcpy(psd->data, s, len);
169 psd->len = len;
170 psd->index = this->next_index_;
171 ++this->next_index_;
172
173 if (pkey != NULL)
174 *pkey = psd->index * key_mult;
175
176 if (front)
177 this->strings_.push_front(psd);
178 else
179 this->strings_.push_back(psd);
180
181 return reinterpret_cast<const Stringpool_char*>(psd->data);
182 }
183
184 // Add a string to a string pool.
185
186 template<typename Stringpool_char>
187 const Stringpool_char*
188 Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, Key* pkey)
189 {
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.
195
196 typename String_set_type::const_iterator p = this->string_set_.find(s);
197 if (p != this->string_set_.end())
198 {
199 if (pkey != NULL)
200 *pkey = p->second.first;
201 return p->first;
202 }
203
204 Key k;
205 const Stringpool_char* ret = this->add_string(s, &k);
206
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);
213
214 if (pkey != NULL)
215 *pkey = k;
216
217 return ret;
218 }
219
220 // Add a prefix of a string to a string pool.
221
222 template<typename Stringpool_char>
223 const Stringpool_char*
224 Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, size_t len,
225 Key* pkey)
226 {
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);
231 }
232
233 template<typename Stringpool_char>
234 const Stringpool_char*
235 Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
236 Key* pkey) const
237 {
238 typename String_set_type::const_iterator p = this->string_set_.find(s);
239 if (p == this->string_set_.end())
240 return NULL;
241
242 if (pkey != NULL)
243 *pkey = p->second.first;
244
245 return p->first;
246 }
247
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:
252 // abcd
253 // cd
254 // d
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.
258
259 template<typename Stringpool_char>
260 bool
261 Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
262 const Stringpool_sort_info& sort_info1,
263 const Stringpool_sort_info& sort_info2) const
264 {
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)
273 {
274 if (*p1 != *p2)
275 return *p1 > *p2;
276 }
277 return len1 > len2;
278 }
279
280 // Return whether s1 is a suffix of s2.
281
282 template<typename Stringpool_char>
283 bool
284 Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
285 size_t len1,
286 const Stringpool_char* s2,
287 size_t len2)
288 {
289 if (len1 > len2)
290 return false;
291 return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
292 }
293
294 // Turn the stringpool into an ELF strtab: determine the offsets of
295 // each string in the table.
296
297 template<typename Stringpool_char>
298 void
299 Stringpool_template<Stringpool_char>::set_string_offsets()
300 {
301 if (this->strtab_size_ != 0)
302 {
303 // We've already computed the offsets.
304 return;
305 }
306
307 const size_t charsize = sizeof(Stringpool_char);
308
309 // Offset 0 may be reserved for the empty string.
310 off_t offset = this->zero_null_ ? charsize : 0;
311
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)
318 {
319 for (typename String_set_type::iterator curr = this->string_set_.begin();
320 curr != this->string_set_.end();
321 curr++)
322 {
323 if (this->zero_null_ && curr->first[0] == 0)
324 curr->second.second = 0;
325 else
326 {
327 curr->second.second = offset;
328 offset += (string_length(curr->first) + 1) * charsize;
329 }
330 }
331 }
332 else
333 {
334 size_t count = this->string_set_.size();
335
336 std::vector<Stringpool_sort_info> v;
337 v.reserve(count);
338
339 for (typename String_set_type::iterator p = this->string_set_.begin();
340 p != this->string_set_.end();
341 ++p)
342 v.push_back(Stringpool_sort_info(p, string_length(p->first)));
343
344 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
345
346 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
347 curr = v.begin();
348 curr != v.end();
349 last = curr++)
350 {
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)
359 * charsize));
360 else
361 {
362 curr->it->second.second = offset;
363 offset += (curr->string_length + 1) * charsize;
364 }
365 }
366 }
367
368 this->strtab_size_ = offset;
369 }
370
371 // Get the offset of a string in the ELF strtab. The string must
372 // exist.
373
374 template<typename Stringpool_char>
375 off_t
376 Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
377 const
378 {
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;
383 gold_unreachable();
384 }
385
386 // Write the ELF strtab into the output file at the specified offset.
387
388 template<typename Stringpool_char>
389 void
390 Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
391 {
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_)
396 view[0] = '\0';
397 for (typename String_set_type::const_iterator p = this->string_set_.begin();
398 p != this->string_set_.end();
399 ++p)
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);
403 }
404
405 // Instantiate the templates we need.
406
407 template
408 class Stringpool_template<char>;
409
410 template
411 class Stringpool_template<uint16_t>;
412
413 template
414 class Stringpool_template<uint32_t>;
415
416 } // End namespace gold.
This page took 0.044275 seconds and 5 git commands to generate.