1 // stringpool.cc -- a string pool for gold
3 // Copyright 2006, 2007, 2008 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_(), key_to_offset_(), strings_(), strtab_size_(0),
39 zero_null_(true), optimize_(false), offset_(sizeof(Stringpool_char))
41 if (parameters->options_valid() && parameters->options().optimize() >= 2)
42 this->optimize_ = true;
45 template<typename Stringpool_char>
47 Stringpool_template<Stringpool_char>::clear()
49 for (typename std::list<Stringdata*>::iterator p = this->strings_.begin();
50 p != this->strings_.end();
52 delete[] reinterpret_cast<char*>(*p);
53 this->strings_.clear();
54 this->key_to_offset_.clear();
55 this->string_set_.clear();
58 template<typename Stringpool_char>
59 Stringpool_template<Stringpool_char>::~Stringpool_template()
64 // Resize the internal hashtable with the expectation we'll get n new
65 // elements. Note that the hashtable constructor takes a "number of
66 // buckets you'd like," rather than "number of elements you'd like,"
67 // but that's the best we can do.
69 template<typename Stringpool_char>
71 Stringpool_template<Stringpool_char>::reserve(unsigned int n)
73 this->key_to_offset_.reserve(n);
75 #if defined(HAVE_TR1_UNORDERED_MAP)
76 // rehash() implementation is broken in gcc 4.0.3's stl
77 //this->string_set_.rehash(this->string_set_.size() + n);
79 #elif defined(HAVE_EXT_HASH_MAP)
80 this->string_set_.resize(this->string_set_.size() + n);
84 // This is the generic "reserve" code, if no #ifdef above triggers.
85 String_set_type new_string_set(this->string_set_.size() + n);
86 new_string_set.insert(this->string_set_.begin(), this->string_set_.end());
87 this->string_set_.swap(new_string_set);
90 // Return the length of a string of arbitrary character type.
92 template<typename Stringpool_char>
94 Stringpool_template<Stringpool_char>::string_length(const Stringpool_char* p)
102 // Specialize string_length for char. Maybe we could just use
103 // std::char_traits<>::length?
107 Stringpool_template<char>::string_length(const char* p)
112 // Compare two strings of arbitrary character type for equality.
114 template<typename Stringpool_char>
116 Stringpool_template<Stringpool_char>::string_equal(const Stringpool_char* s1,
117 const Stringpool_char* s2)
125 // Specialize string_equal for char.
129 Stringpool_template<char>::string_equal(const char* s1, const char* s2)
131 return strcmp(s1, s2) == 0;
134 // Equality comparison function for the hash table.
136 template<typename Stringpool_char>
138 Stringpool_template<Stringpool_char>::Stringpool_eq::operator()(
140 const Hashkey& h2) const
142 return (h1.hash_code == h2.hash_code
143 && h1.length == h2.length
144 && (h1.string == h2.string
145 || memcmp(h1.string, h2.string,
146 h1.length * sizeof(Stringpool_char)) == 0));
149 // Hash function. The length is in characters, not bytes.
151 template<typename Stringpool_char>
153 Stringpool_template<Stringpool_char>::string_hash(const Stringpool_char* s,
156 return gold::string_hash<Stringpool_char>(s, length);
159 // Add the string S to the list of canonical strings. Return a
160 // pointer to the canonical string. If PKEY is not NULL, set *PKEY to
161 // the key. LENGTH is the length of S in characters. Note that S may
162 // not be NUL terminated.
164 template<typename Stringpool_char>
165 const Stringpool_char*
166 Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
169 // We are in trouble if we've already computed the string offsets.
170 gold_assert(this->strtab_size_ == 0);
172 // The size we allocate for a new Stringdata.
173 const size_t buffer_size = 1000;
174 // The amount we multiply the Stringdata index when calculating the
176 const size_t key_mult = 1024;
177 gold_assert(key_mult >= buffer_size);
179 // Convert len to the number of bytes we need to allocate, including
180 // the null character.
181 len = (len + 1) * sizeof(Stringpool_char);
185 if (len > buffer_size)
187 alc = sizeof(Stringdata) + len;
190 else if (this->strings_.empty())
191 alc = sizeof(Stringdata) + buffer_size;
194 Stringdata *psd = this->strings_.front();
195 if (len > psd->alc - psd->len)
196 alc = sizeof(Stringdata) + buffer_size;
199 char* ret = psd->data + psd->len;
200 memcpy(ret, s, len - sizeof(Stringpool_char));
201 memset(ret + len - sizeof(Stringpool_char), 0,
202 sizeof(Stringpool_char));
206 return reinterpret_cast<const Stringpool_char*>(ret);
210 Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]);
211 psd->alc = alc - sizeof(Stringdata);
212 memcpy(psd->data, s, len - sizeof(Stringpool_char));
213 memset(psd->data + len - sizeof(Stringpool_char), 0,
214 sizeof(Stringpool_char));
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 return this->add_with_length(s, string_length(s), copy, pkey);
235 // Add a new key offset entry.
237 template<typename Stringpool_char>
239 Stringpool_template<Stringpool_char>::new_key_offset(size_t length)
241 section_offset_type offset;
242 if (this->zero_null_ && length == 0)
246 offset = this->offset_;
247 this->offset_ += (length + 1) * sizeof(Stringpool_char);
249 this->key_to_offset_.push_back(offset);
252 template<typename Stringpool_char>
253 const Stringpool_char*
254 Stringpool_template<Stringpool_char>::add_with_length(const Stringpool_char* s,
259 typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
261 // We add 1 so that 0 is always invalid.
262 const Key k = this->key_to_offset_.size() + 1;
266 // When we don't need to copy the string, we can call insert
269 std::pair<Hashkey, Hashval> element(Hashkey(s, length), k);
271 Insert_type ins = this->string_set_.insert(element);
273 typename String_set_type::const_iterator p = ins.first;
277 // We just added the string. The key value has now been
279 this->new_key_offset(length);
283 gold_assert(k != p->second);
288 return p->first.string;
291 // When we have to copy the string, we look it up twice in the hash
292 // table. The problem is that we can't insert S before we
293 // canonicalize it by copying it into the canonical list. The hash
294 // code will only be computed once.
296 Hashkey hk(s, length);
297 typename String_set_type::const_iterator p = this->string_set_.find(hk);
298 if (p != this->string_set_.end())
302 return p->first.string;
305 this->new_key_offset(length);
307 hk.string = this->add_string(s, length);
308 // The contents of the string stay the same, so we don't need to
309 // adjust hk.hash_code or hk.length.
311 std::pair<Hashkey, Hashval> element(hk, k);
313 Insert_type ins = this->string_set_.insert(element);
314 gold_assert(ins.second);
321 template<typename Stringpool_char>
322 const Stringpool_char*
323 Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
327 typename String_set_type::const_iterator p = this->string_set_.find(hk);
328 if (p == this->string_set_.end())
334 return p->first.string;
337 // Comparison routine used when sorting into an ELF strtab. We want
338 // to sort this so that when one string is a suffix of another, we
339 // always see the shorter string immediately after the longer string.
340 // For example, we want to see these strings in this order:
344 // When strings are not suffixes, we don't care what order they are
345 // in, but we need to ensure that suffixes wind up next to each other.
346 // So we do a reversed lexicographic sort on the reversed string.
348 template<typename Stringpool_char>
350 Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
351 const Stringpool_sort_info& sort_info1,
352 const Stringpool_sort_info& sort_info2) const
354 const Hashkey& h1(sort_info1->first);
355 const Hashkey& h2(sort_info2->first);
356 const Stringpool_char* s1 = h1.string;
357 const Stringpool_char* s2 = h2.string;
358 const size_t len1 = h1.length;
359 const size_t len2 = h2.length;
360 const size_t minlen = len1 < len2 ? len1 : len2;
361 const Stringpool_char* p1 = s1 + len1 - 1;
362 const Stringpool_char* p2 = s2 + len2 - 1;
363 for (size_t i = minlen; i > 0; --i, --p1, --p2)
371 // Return whether s1 is a suffix of s2.
373 template<typename Stringpool_char>
375 Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
377 const Stringpool_char* s2,
382 return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
385 // Turn the stringpool into an ELF strtab: determine the offsets of
386 // each string in the table.
388 template<typename Stringpool_char>
390 Stringpool_template<Stringpool_char>::set_string_offsets()
392 if (this->strtab_size_ != 0)
394 // We've already computed the offsets.
398 const size_t charsize = sizeof(Stringpool_char);
400 // Offset 0 may be reserved for the empty string.
401 section_offset_type offset = this->zero_null_ ? charsize : 0;
403 // Sorting to find suffixes can take over 25% of the total CPU time
404 // used by the linker. Since it's merely an optimization to reduce
405 // the strtab size, and gives a relatively small benefit (it's
406 // typically rare for a symbol to be a suffix of another), we only
407 // take the time to sort when the user asks for heavy optimization.
408 if (!this->optimize_)
410 // If we are not optimizing, the offsets are already assigned.
411 offset = this->offset_;
415 size_t count = this->string_set_.size();
417 std::vector<Stringpool_sort_info> v;
420 for (typename String_set_type::iterator p = this->string_set_.begin();
421 p != this->string_set_.end();
423 v.push_back(Stringpool_sort_info(p));
425 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
427 section_offset_type last_offset = -1;
428 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
433 section_offset_type this_offset;
434 if (this->zero_null_ && (*curr)->first.string[0] == 0)
436 else if (last != v.end()
437 && is_suffix((*curr)->first.string,
438 (*curr)->first.length,
439 (*last)->first.string,
440 (*last)->first.length))
441 this_offset = (last_offset
442 + (((*last)->first.length - (*curr)->first.length)
446 this_offset = offset;
447 offset += ((*curr)->first.length + 1) * charsize;
449 this->key_to_offset_[(*curr)->second - 1] = this_offset;
450 last_offset = this_offset;
454 this->strtab_size_ = offset;
457 // Get the offset of a string in the ELF strtab. The string must
460 template<typename Stringpool_char>
462 Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
465 return this->get_offset_with_length(s, string_length(s));
468 template<typename Stringpool_char>
470 Stringpool_template<Stringpool_char>::get_offset_with_length(
471 const Stringpool_char* s,
474 gold_assert(this->strtab_size_ != 0);
475 Hashkey hk(s, length);
476 typename String_set_type::const_iterator p = this->string_set_.find(hk);
477 if (p != this->string_set_.end())
478 return this->key_to_offset_[p->second - 1];
482 // Write the ELF strtab into the buffer.
484 template<typename Stringpool_char>
486 Stringpool_template<Stringpool_char>::write_to_buffer(
487 unsigned char* buffer,
488 section_size_type bufsize)
490 gold_assert(this->strtab_size_ != 0);
491 gold_assert(bufsize >= this->strtab_size_);
492 if (this->zero_null_)
494 for (typename String_set_type::const_iterator p = this->string_set_.begin();
495 p != this->string_set_.end();
498 const int len = (p->first.length + 1) * sizeof(Stringpool_char);
499 const section_offset_type offset = this->key_to_offset_[p->second - 1];
500 gold_assert(static_cast<section_size_type>(offset) + len
501 <= this->strtab_size_);
502 memcpy(buffer + offset, p->first.string, len);
506 // Write the ELF strtab into the output file at the specified offset.
508 template<typename Stringpool_char>
510 Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
512 gold_assert(this->strtab_size_ != 0);
513 unsigned char* view = of->get_output_view(offset, this->strtab_size_);
514 this->write_to_buffer(view, this->strtab_size_);
515 of->write_output_view(offset, this->strtab_size_, view);
518 // Print statistical information to stderr. This is used for --stats.
520 template<typename Stringpool_char>
522 Stringpool_template<Stringpool_char>::print_stats(const char* name) const
524 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
525 fprintf(stderr, _("%s: %s entries: %zu; buckets: %zu\n"),
526 program_name, name, this->string_set_.size(),
527 this->string_set_.bucket_count());
529 fprintf(stderr, _("%s: %s entries: %zu\n"),
530 program_name, name, this->table_.size());
532 fprintf(stderr, _("%s: %s Stringdata structures: %zu\n"),
533 program_name, name, this->strings_.size());
536 // Instantiate the templates we need.
539 class Stringpool_template<char>;
542 class Stringpool_template<uint16_t>;
545 class Stringpool_template<uint32_t>;
547 } // End namespace gold.