From Craig Silverstein: Add -O option.
[deliverable/binutils-gdb.git] / gold / stringpool.cc
CommitLineData
14bfc3f5
ILT
1// stringpool.cc -- a string pool for gold
2
3#include "gold.h"
4
14bfc3f5 5#include <cstring>
61ba1cf9
ILT
6#include <algorithm>
7#include <vector>
14bfc3f5 8
61ba1cf9 9#include "output.h"
14bfc3f5
ILT
10#include "stringpool.h"
11
12namespace gold
13{
14
b8e6aad9
ILT
15template<typename Stringpool_char>
16Stringpool_template<Stringpool_char>::Stringpool_template(bool zero_null)
17 : string_set_(), strings_(), strtab_size_(0), next_index_(1),
18 zero_null_(zero_null)
14bfc3f5
ILT
19{
20}
21
b8e6aad9
ILT
22template<typename Stringpool_char>
23Stringpool_template<Stringpool_char>::~Stringpool_template()
14bfc3f5 24{
b8e6aad9 25 for (typename std::list<Stringdata*>::iterator p = this->strings_.begin();
14bfc3f5
ILT
26 p != this->strings_.end();
27 ++p)
28 delete[] reinterpret_cast<char*>(*p);
29}
30
b8e6aad9
ILT
31// Return the length of a string of arbitrary character type.
32
33template<typename Stringpool_char>
34size_t
35Stringpool_template<Stringpool_char>::string_length(const Stringpool_char* p)
36{
37 size_t len = 0;
38 for (; *p != 0; ++p)
39 ++len;
40 return len;
41}
42
43// Specialize string_length for char. Maybe we could just use
44// std::char_traits<>::length?
45
46template<>
47inline size_t
48Stringpool_template<char>::string_length(const char* p)
49{
50 return strlen(p);
51}
52
53// Equality comparison function.
54
55template<typename Stringpool_char>
56bool
57Stringpool_template<Stringpool_char>::Stringpool_eq::operator()(
58 const Stringpool_char* s1,
59 const Stringpool_char* s2) const
60{
61 while (*s1 != 0)
62 if (*s1++ != *s2++)
63 return false;
64 return *s2 == 0;
65}
66
67// Specialize equality comparison for char.
68
69template<>
70bool
71Stringpool_template<char>::Stringpool_eq::operator()(const char* s1,
72 const char* s2) const
73{
74 return strcmp(s1, s2) == 0;
75}
76
14bfc3f5
ILT
77// Hash function.
78
b8e6aad9 79template<typename Stringpool_char>
14bfc3f5 80size_t
b8e6aad9
ILT
81Stringpool_template<Stringpool_char>::Stringpool_hash::operator()(
82 const Stringpool_char* s) const
14bfc3f5
ILT
83{
84 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
85 if (sizeof(size_t) == 8)
86 {
b3168e9d 87 size_t result = static_cast<size_t>(14695981039346656037ULL);
b8e6aad9 88 while (*s != 0)
14bfc3f5 89 {
b8e6aad9
ILT
90 const char* p = reinterpret_cast<const char*>(s);
91 for (size_t i = 0; i < sizeof(Stringpool_char); ++i)
92 {
93 result &= (size_t) *p++;
94 result *= 1099511628211ULL;
95 }
96 ++s;
14bfc3f5
ILT
97 }
98 return result;
99 }
100 else
101 {
102 size_t result = 2166136261UL;
b8e6aad9 103 while (*s != 0)
14bfc3f5 104 {
b8e6aad9
ILT
105 const char* p = reinterpret_cast<const char*>(s);
106 for (size_t i = 0; i < sizeof(Stringpool_char); ++i)
107 {
108 result ^= (size_t) *p++;
109 result *= 16777619UL;
110 }
111 ++s;
14bfc3f5
ILT
112 }
113 return result;
114 }
115}
116
117// Add a string to the list of canonical strings. Return a pointer to
f0641a0b 118// the canonical string. If PKEY is not NULL, set *PKEY to the key.
14bfc3f5 119
b8e6aad9
ILT
120template<typename Stringpool_char>
121const Stringpool_char*
122Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
123 Key* pkey)
14bfc3f5 124{
a3ad94ed
ILT
125 // We are in trouble if we've already computed the string offsets.
126 gold_assert(this->strtab_size_ == 0);
127
f0641a0b 128 // The size we allocate for a new Stringdata.
14bfc3f5 129 const size_t buffer_size = 1000;
f0641a0b
ILT
130 // The amount we multiply the Stringdata index when calculating the
131 // key.
132 const size_t key_mult = 1024;
a3ad94ed 133 gold_assert(key_mult >= buffer_size);
f0641a0b 134
b8e6aad9 135 size_t len = (string_length(s) + 1) * sizeof(Stringpool_char);
14bfc3f5
ILT
136
137 size_t alc;
138 bool front = true;
b8e6aad9 139 if (len > buffer_size)
14bfc3f5 140 {
61ba1cf9 141 alc = sizeof(Stringdata) + len;
14bfc3f5
ILT
142 front = false;
143 }
144 else if (this->strings_.empty())
61ba1cf9 145 alc = sizeof(Stringdata) + buffer_size;
14bfc3f5
ILT
146 else
147 {
61ba1cf9 148 Stringdata *psd = this->strings_.front();
b8e6aad9 149 if (len > psd->alc - psd->len)
61ba1cf9 150 alc = sizeof(Stringdata) + buffer_size;
14bfc3f5
ILT
151 else
152 {
153 char* ret = psd->data + psd->len;
b8e6aad9 154 memcpy(ret, s, len);
f0641a0b
ILT
155
156 if (pkey != NULL)
157 *pkey = psd->index * key_mult + psd->len;
158
b8e6aad9 159 psd->len += len;
f0641a0b 160
b8e6aad9 161 return reinterpret_cast<const Stringpool_char*>(ret);
14bfc3f5
ILT
162 }
163 }
164
61ba1cf9
ILT
165 Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]);
166 psd->alc = alc - sizeof(Stringdata);
b8e6aad9
ILT
167 memcpy(psd->data, s, len);
168 psd->len = len;
f0641a0b
ILT
169 psd->index = this->next_index_;
170 ++this->next_index_;
171
172 if (pkey != NULL)
173 *pkey = psd->index * key_mult;
174
14bfc3f5
ILT
175 if (front)
176 this->strings_.push_front(psd);
177 else
178 this->strings_.push_back(psd);
f0641a0b 179
b8e6aad9 180 return reinterpret_cast<const Stringpool_char*>(psd->data);
14bfc3f5
ILT
181}
182
183// Add a string to a string pool.
184
b8e6aad9
ILT
185template<typename Stringpool_char>
186const Stringpool_char*
187Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, Key* pkey)
14bfc3f5
ILT
188{
189 // FIXME: This will look up the entry twice in the hash table. The
190 // problem is that we can't insert S before we canonicalize it. I
a2fb1b05 191 // don't think there is a way to handle this correctly with
61ba1cf9 192 // unordered_map, so this should be replaced with custom code to do
14bfc3f5
ILT
193 // what we need, which is to return the empty slot.
194
b8e6aad9 195 typename String_set_type::const_iterator p = this->string_set_.find(s);
14bfc3f5 196 if (p != this->string_set_.end())
f0641a0b
ILT
197 {
198 if (pkey != NULL)
199 *pkey = p->second.first;
200 return p->first;
201 }
14bfc3f5 202
f0641a0b 203 Key k;
b8e6aad9 204 const Stringpool_char* ret = this->add_string(s, &k);
f0641a0b
ILT
205
206 const off_t ozero = 0;
b8e6aad9
ILT
207 std::pair<const Stringpool_char*, Val> element(ret,
208 std::make_pair(k, ozero));
209 std::pair<typename String_set_type::iterator, bool> ins =
f0641a0b 210 this->string_set_.insert(element);
a3ad94ed 211 gold_assert(ins.second);
f0641a0b
ILT
212
213 if (pkey != NULL)
214 *pkey = k;
215
14bfc3f5
ILT
216 return ret;
217}
218
219// Add a prefix of a string to a string pool.
220
b8e6aad9
ILT
221template<typename Stringpool_char>
222const Stringpool_char*
223Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, size_t len,
224 Key* pkey)
14bfc3f5
ILT
225{
226 // FIXME: This implementation should be rewritten when we rewrite
227 // the hash table to avoid copying.
b8e6aad9 228 std::basic_string<Stringpool_char> st(s, len);
f0641a0b 229 return this->add(st, pkey);
14bfc3f5
ILT
230}
231
b8e6aad9
ILT
232template<typename Stringpool_char>
233const Stringpool_char*
234Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
235 Key* pkey) const
61ba1cf9 236{
b8e6aad9 237 typename String_set_type::const_iterator p = this->string_set_.find(s);
61ba1cf9
ILT
238 if (p == this->string_set_.end())
239 return NULL;
f0641a0b
ILT
240
241 if (pkey != NULL)
242 *pkey = p->second.first;
243
61ba1cf9
ILT
244 return p->first;
245}
246
247// Comparison routine used when sorting into an ELF strtab. We want
248// to sort this so that when one string is a suffix of another, we
249// always see the shorter string immediately after the longer string.
250// For example, we want to see these strings in this order:
251// abcd
252// cd
253// d
254// When strings are not suffixes, we don't care what order they are
255// in, but we need to ensure that suffixes wind up next to each other.
256// So we do a reversed lexicographic sort on the reversed string.
257
b8e6aad9 258template<typename Stringpool_char>
61ba1cf9 259bool
b8e6aad9 260Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
614f30a2
ILT
261 const Stringpool_sort_info& sort_info1,
262 const Stringpool_sort_info& sort_info2) const
61ba1cf9 263{
614f30a2
ILT
264 const Stringpool_char* s1 = sort_info1.it->first;
265 const Stringpool_char* s2 = sort_info2.it->first;
266 const size_t len1 = sort_info1.string_length;
267 const size_t len2 = sort_info2.string_length;
268 const size_t minlen = len1 < len2 ? len1 : len2;
b8e6aad9
ILT
269 const Stringpool_char* p1 = s1 + len1 - 1;
270 const Stringpool_char* p2 = s2 + len2 - 1;
271 for (size_t i = minlen; i > 0; --i, --p1, --p2)
61ba1cf9
ILT
272 {
273 if (*p1 != *p2)
274 return *p1 > *p2;
275 }
276 return len1 > len2;
277}
278
279// Return whether s1 is a suffix of s2.
280
b8e6aad9 281template<typename Stringpool_char>
61ba1cf9 282bool
b8e6aad9 283Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
614f30a2
ILT
284 size_t len1,
285 const Stringpool_char* s2,
286 size_t len2)
61ba1cf9 287{
61ba1cf9
ILT
288 if (len1 > len2)
289 return false;
b8e6aad9 290 return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
61ba1cf9
ILT
291}
292
293// Turn the stringpool into an ELF strtab: determine the offsets of
294// each string in the table.
295
b8e6aad9 296template<typename Stringpool_char>
61ba1cf9 297void
b8e6aad9 298Stringpool_template<Stringpool_char>::set_string_offsets()
61ba1cf9 299{
a3ad94ed
ILT
300 if (this->strtab_size_ != 0)
301 {
302 // We've already computed the offsets.
303 return;
304 }
305
61ba1cf9
ILT
306 size_t count = this->string_set_.size();
307
614f30a2 308 std::vector<Stringpool_sort_info> v;
61ba1cf9
ILT
309 v.reserve(count);
310
b8e6aad9 311 for (typename String_set_type::iterator p = this->string_set_.begin();
61ba1cf9
ILT
312 p != this->string_set_.end();
313 ++p)
614f30a2 314 v.push_back(Stringpool_sort_info(p, string_length(p->first)));
61ba1cf9
ILT
315
316 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
317
b8e6aad9
ILT
318 const size_t charsize = sizeof(Stringpool_char);
319
320 // Offset 0 may be reserved for the empty string.
321 off_t offset = this->zero_null_ ? charsize : 0;
614f30a2
ILT
322
323 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
324 curr = v.begin();
325 curr != v.end();
326 last = curr++)
61ba1cf9 327 {
614f30a2
ILT
328 if (this->zero_null_ && curr->it->first[0] == 0)
329 curr->it->second.second = 0;
330 else if (last != v.end()
331 && is_suffix(curr->it->first, curr->string_length,
332 last->it->first, last->string_length))
333 curr->it->second.second = (last->it->second.second
334 + ((last->string_length
335 - curr->string_length)
336 * charsize));
61ba1cf9
ILT
337 else
338 {
614f30a2
ILT
339 curr->it->second.second = offset;
340 offset += (curr->string_length + 1) * charsize;
61ba1cf9
ILT
341 }
342 }
343
344 this->strtab_size_ = offset;
345}
346
347// Get the offset of a string in the ELF strtab. The string must
348// exist.
349
b8e6aad9 350template<typename Stringpool_char>
61ba1cf9 351off_t
b8e6aad9
ILT
352Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
353 const
61ba1cf9 354{
a3ad94ed 355 gold_assert(this->strtab_size_ != 0);
b8e6aad9 356 typename String_set_type::const_iterator p = this->string_set_.find(s);
61ba1cf9 357 if (p != this->string_set_.end())
f0641a0b 358 return p->second.second;
a3ad94ed 359 gold_unreachable();
61ba1cf9
ILT
360}
361
362// Write the ELF strtab into the output file at the specified offset.
363
b8e6aad9 364template<typename Stringpool_char>
61ba1cf9 365void
b8e6aad9 366Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
61ba1cf9 367{
a3ad94ed 368 gold_assert(this->strtab_size_ != 0);
61ba1cf9
ILT
369 unsigned char* viewu = of->get_output_view(offset, this->strtab_size_);
370 char* view = reinterpret_cast<char*>(viewu);
b8e6aad9
ILT
371 if (this->zero_null_)
372 view[0] = '\0';
373 for (typename String_set_type::const_iterator p = this->string_set_.begin();
61ba1cf9
ILT
374 p != this->string_set_.end();
375 ++p)
b8e6aad9
ILT
376 memcpy(view + p->second.second, p->first,
377 (string_length(p->first) + 1) * sizeof(Stringpool_char));
61ba1cf9
ILT
378 of->write_output_view(offset, this->strtab_size_, viewu);
379}
380
b8e6aad9
ILT
381// Instantiate the templates we need.
382
383template
384class Stringpool_template<char>;
385
386template
387class Stringpool_template<uint16_t>;
388
389template
390class Stringpool_template<uint32_t>;
391
14bfc3f5 392} // End namespace gold.
This page took 0.071885 seconds and 4 git commands to generate.