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 // Equality comparison function.
85 template<typename Stringpool_char>
87 Stringpool_template<Stringpool_char>::Stringpool_eq::operator()(
88 const Stringpool_char* s1,
89 const Stringpool_char* s2) const
97 // Specialize equality comparison for char.
101 Stringpool_template<char>::Stringpool_eq::operator()(const char* s1,
102 const char* s2) const
104 return strcmp(s1, s2) == 0;
109 template<typename Stringpool_char>
111 Stringpool_template<Stringpool_char>::Stringpool_hash::operator()(
112 const Stringpool_char* s) const
114 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
115 if (sizeof(size_t) > 4)
117 size_t result = static_cast<size_t>(14695981039346656037ULL);
120 const char* p = reinterpret_cast<const char*>(s);
121 for (size_t i = 0; i < sizeof(Stringpool_char); ++i)
123 result ^= (size_t) *p++;
124 result *= 1099511628211ULL;
132 size_t result = 2166136261UL;
135 const char* p = reinterpret_cast<const char*>(s);
136 for (size_t i = 0; i < sizeof(Stringpool_char); ++i)
138 result ^= (size_t) *p++;
139 result *= 16777619UL;
147 // Add a string to the list of canonical strings. Return a pointer to
148 // the canonical string. If PKEY is not NULL, set *PKEY to the key.
150 template<typename Stringpool_char>
151 const Stringpool_char*
152 Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
155 // We are in trouble if we've already computed the string offsets.
156 gold_assert(this->strtab_size_ == 0);
158 // The size we allocate for a new Stringdata.
159 const size_t buffer_size = 1000;
160 // The amount we multiply the Stringdata index when calculating the
162 const size_t key_mult = 1024;
163 gold_assert(key_mult >= buffer_size);
165 size_t len = (string_length(s) + 1) * sizeof(Stringpool_char);
169 if (len > buffer_size)
171 alc = sizeof(Stringdata) + len;
174 else if (this->strings_.empty())
175 alc = sizeof(Stringdata) + buffer_size;
178 Stringdata *psd = this->strings_.front();
179 if (len > psd->alc - psd->len)
180 alc = sizeof(Stringdata) + buffer_size;
183 char* ret = psd->data + psd->len;
187 *pkey = psd->index * key_mult + psd->len;
191 return reinterpret_cast<const Stringpool_char*>(ret);
195 Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]);
196 psd->alc = alc - sizeof(Stringdata);
197 memcpy(psd->data, s, len);
199 psd->index = this->next_index_;
203 *pkey = psd->index * key_mult;
206 this->strings_.push_front(psd);
208 this->strings_.push_back(psd);
210 return reinterpret_cast<const Stringpool_char*>(psd->data);
213 // Add a string to a string pool.
215 template<typename Stringpool_char>
216 const Stringpool_char*
217 Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, bool copy,
220 // FIXME: This will look up the entry twice in the hash table. The
221 // problem is that we can't insert S before we canonicalize it. I
222 // don't think there is a way to handle this correctly with
223 // unordered_map, so this should be replaced with custom code to do
224 // what we need, which is to return the empty slot.
226 typename String_set_type::const_iterator p = this->string_set_.find(s);
227 if (p != this->string_set_.end())
230 *pkey = p->second.first;
235 const Stringpool_char* ret;
237 ret = this->add_string(s, &k);
241 k = this->next_uncopied_key_;
242 --this->next_uncopied_key_;
245 const off_t ozero = 0;
246 std::pair<const Stringpool_char*, Val> element(ret,
247 std::make_pair(k, ozero));
248 std::pair<typename String_set_type::iterator, bool> ins =
249 this->string_set_.insert(element);
250 gold_assert(ins.second);
258 // Add a prefix of a string to a string pool.
260 template<typename Stringpool_char>
261 const Stringpool_char*
262 Stringpool_template<Stringpool_char>::add_prefix(const Stringpool_char* s,
266 // FIXME: This implementation should be rewritten when we rewrite
267 // the hash table to avoid copying.
268 std::basic_string<Stringpool_char> st(s, len);
269 return this->add(st.c_str(), true, pkey);
272 template<typename Stringpool_char>
273 const Stringpool_char*
274 Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
277 typename String_set_type::const_iterator p = this->string_set_.find(s);
278 if (p == this->string_set_.end())
282 *pkey = p->second.first;
287 // Comparison routine used when sorting into an ELF strtab. We want
288 // to sort this so that when one string is a suffix of another, we
289 // always see the shorter string immediately after the longer string.
290 // For example, we want to see these strings in this order:
294 // When strings are not suffixes, we don't care what order they are
295 // in, but we need to ensure that suffixes wind up next to each other.
296 // So we do a reversed lexicographic sort on the reversed string.
298 template<typename Stringpool_char>
300 Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
301 const Stringpool_sort_info& sort_info1,
302 const Stringpool_sort_info& sort_info2) const
304 const Stringpool_char* s1 = sort_info1.it->first;
305 const Stringpool_char* s2 = sort_info2.it->first;
306 const size_t len1 = sort_info1.string_length;
307 const size_t len2 = sort_info2.string_length;
308 const size_t minlen = len1 < len2 ? len1 : len2;
309 const Stringpool_char* p1 = s1 + len1 - 1;
310 const Stringpool_char* p2 = s2 + len2 - 1;
311 for (size_t i = minlen; i > 0; --i, --p1, --p2)
319 // Return whether s1 is a suffix of s2.
321 template<typename Stringpool_char>
323 Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
325 const Stringpool_char* s2,
330 return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
333 // Turn the stringpool into an ELF strtab: determine the offsets of
334 // each string in the table.
336 template<typename Stringpool_char>
338 Stringpool_template<Stringpool_char>::set_string_offsets()
340 if (this->strtab_size_ != 0)
342 // We've already computed the offsets.
346 const size_t charsize = sizeof(Stringpool_char);
348 // Offset 0 may be reserved for the empty string.
349 off_t offset = this->zero_null_ ? charsize : 0;
351 // Sorting to find suffixes can take over 25% of the total CPU time
352 // used by the linker. Since it's merely an optimization to reduce
353 // the strtab size, and gives a relatively small benefit (it's
354 // typically rare for a symbol to be a suffix of another), we only
355 // take the time to sort when the user asks for heavy optimization.
356 if (parameters->optimization_level() < 2)
358 for (typename String_set_type::iterator curr = this->string_set_.begin();
359 curr != this->string_set_.end();
362 if (this->zero_null_ && curr->first[0] == 0)
363 curr->second.second = 0;
366 curr->second.second = offset;
367 offset += (string_length(curr->first) + 1) * charsize;
373 size_t count = this->string_set_.size();
375 std::vector<Stringpool_sort_info> v;
378 for (typename String_set_type::iterator p = this->string_set_.begin();
379 p != this->string_set_.end();
381 v.push_back(Stringpool_sort_info(p, string_length(p->first)));
383 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
385 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
390 if (this->zero_null_ && curr->it->first[0] == 0)
391 curr->it->second.second = 0;
392 else if (last != v.end()
393 && is_suffix(curr->it->first, curr->string_length,
394 last->it->first, last->string_length))
395 curr->it->second.second = (last->it->second.second
396 + ((last->string_length
397 - curr->string_length)
401 curr->it->second.second = offset;
402 offset += (curr->string_length + 1) * charsize;
407 this->strtab_size_ = offset;
410 // Get the offset of a string in the ELF strtab. The string must
413 template<typename Stringpool_char>
415 Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
418 gold_assert(this->strtab_size_ != 0);
419 typename String_set_type::const_iterator p = this->string_set_.find(s);
420 if (p != this->string_set_.end())
421 return p->second.second;
425 // Write the ELF strtab into the buffer.
427 template<typename Stringpool_char>
429 Stringpool_template<Stringpool_char>::write_to_buffer(unsigned char* buffer,
432 gold_assert(this->strtab_size_ != 0);
433 // Quiet the compiler in opt mode.
434 if (bufsize < static_cast<size_t>(this->strtab_size_))
435 gold_assert(bufsize >= static_cast<size_t>(this->strtab_size_));
436 if (this->zero_null_)
438 for (typename String_set_type::const_iterator p = this->string_set_.begin();
439 p != this->string_set_.end();
442 const int len = (string_length(p->first) + 1) * sizeof(Stringpool_char);
443 gold_assert(p->second.second + len <= this->strtab_size_);
444 memcpy(buffer + p->second.second, p->first, len);
448 // Write the ELF strtab into the output file at the specified offset.
450 template<typename Stringpool_char>
452 Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
454 gold_assert(this->strtab_size_ != 0);
455 unsigned char* view = of->get_output_view(offset, this->strtab_size_);
456 this->write_to_buffer(view, this->strtab_size_);
457 of->write_output_view(offset, this->strtab_size_, view);
460 // Instantiate the templates we need.
463 class Stringpool_template<char>;
466 class Stringpool_template<uint16_t>;
469 class Stringpool_template<uint32_t>;
471 } // End namespace gold.