1 // stringpool.cc -- a string pool for gold
10 #include "stringpool.h"
15 Stringpool::Stringpool()
16 : string_set_(), strings_(), strtab_size_(0), next_index_(1)
20 Stringpool::~Stringpool()
22 for (std::list
<Stringdata
*>::iterator p
= this->strings_
.begin();
23 p
!= this->strings_
.end();
25 delete[] reinterpret_cast<char*>(*p
);
31 Stringpool::Stringpool_hash::operator()(const char* s
) const
33 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
34 if (sizeof(size_t) == 8)
36 size_t result
= static_cast<size_t>(14695981039346656037ULL);
39 result
&= (size_t) *s
++;
40 result
*= 1099511628211ULL;
46 size_t result
= 2166136261UL;
49 result
^= (size_t) *s
++;
56 // Add a string to the list of canonical strings. Return a pointer to
57 // the canonical string. If PKEY is not NULL, set *PKEY to the key.
60 Stringpool::add_string(const char* s
, Key
* pkey
)
62 // We are in trouble if we've already computed the string offsets.
63 gold_assert(this->strtab_size_
== 0);
65 // The size we allocate for a new Stringdata.
66 const size_t buffer_size
= 1000;
67 // The amount we multiply the Stringdata index when calculating the
69 const size_t key_mult
= 1024;
70 gold_assert(key_mult
>= buffer_size
);
72 size_t len
= strlen(s
);
76 if (len
>= buffer_size
)
78 alc
= sizeof(Stringdata
) + len
;
81 else if (this->strings_
.empty())
82 alc
= sizeof(Stringdata
) + buffer_size
;
85 Stringdata
*psd
= this->strings_
.front();
86 if (len
>= psd
->alc
- psd
->len
)
87 alc
= sizeof(Stringdata
) + buffer_size
;
90 char* ret
= psd
->data
+ psd
->len
;
91 memcpy(ret
, s
, len
+ 1);
94 *pkey
= psd
->index
* key_mult
+ psd
->len
;
102 Stringdata
*psd
= reinterpret_cast<Stringdata
*>(new char[alc
]);
103 psd
->alc
= alc
- sizeof(Stringdata
);
104 memcpy(psd
->data
, s
, len
+ 1);
106 psd
->index
= this->next_index_
;
110 *pkey
= psd
->index
* key_mult
;
113 this->strings_
.push_front(psd
);
115 this->strings_
.push_back(psd
);
120 // Add a string to a string pool.
123 Stringpool::add(const char* s
, Key
* pkey
)
125 // FIXME: This will look up the entry twice in the hash table. The
126 // problem is that we can't insert S before we canonicalize it. I
127 // don't think there is a way to handle this correctly with
128 // unordered_map, so this should be replaced with custom code to do
129 // what we need, which is to return the empty slot.
131 String_set_type::const_iterator p
= this->string_set_
.find(s
);
132 if (p
!= this->string_set_
.end())
135 *pkey
= p
->second
.first
;
140 const char* ret
= this->add_string(s
, &k
);
142 const off_t ozero
= 0;
143 std::pair
<const char*, Val
> element(ret
, std::make_pair(k
, ozero
));
144 std::pair
<String_set_type::iterator
, bool> ins
=
145 this->string_set_
.insert(element
);
146 gold_assert(ins
.second
);
154 // Add a prefix of a string to a string pool.
157 Stringpool::add(const char* s
, size_t len
, Key
* pkey
)
159 // FIXME: This implementation should be rewritten when we rewrite
160 // the hash table to avoid copying.
161 std::string
st(s
, len
);
162 return this->add(st
, pkey
);
166 Stringpool::find(const char* s
, Key
* pkey
) const
168 String_set_type::const_iterator p
= this->string_set_
.find(s
);
169 if (p
== this->string_set_
.end())
173 *pkey
= p
->second
.first
;
178 // Comparison routine used when sorting into an ELF strtab. We want
179 // to sort this so that when one string is a suffix of another, we
180 // always see the shorter string immediately after the longer string.
181 // For example, we want to see these strings in this order:
185 // When strings are not suffixes, we don't care what order they are
186 // in, but we need to ensure that suffixes wind up next to each other.
187 // So we do a reversed lexicographic sort on the reversed string.
190 Stringpool::Stringpool_sort_comparison::operator()(
191 String_set_type::iterator it1
,
192 String_set_type::iterator it2
) const
194 const char* s1
= it1
->first
;
195 const char* s2
= it2
->first
;
196 int len1
= strlen(s1
);
197 int len2
= strlen(s2
);
198 int minlen
= len1
< len2
? len1
: len2
;
199 const char* p1
= s1
+ len1
- 1;
200 const char* p2
= s2
+ len2
- 1;
201 for (int i
= minlen
- 1; i
>= 0; --i
, --p1
, --p2
)
209 // Return whether s1 is a suffix of s2.
212 Stringpool::is_suffix(const char* s1
, const char* s2
)
214 size_t len1
= strlen(s1
);
215 size_t len2
= strlen(s2
);
218 return strcmp(s1
, s2
+ len2
- len1
) == 0;
221 // Turn the stringpool into an ELF strtab: determine the offsets of
222 // each string in the table.
225 Stringpool::set_string_offsets()
227 if (this->strtab_size_
!= 0)
229 // We've already computed the offsets.
233 size_t count
= this->string_set_
.size();
235 std::vector
<String_set_type::iterator
> v
;
238 for (String_set_type::iterator p
= this->string_set_
.begin();
239 p
!= this->string_set_
.end();
243 std::sort(v
.begin(), v
.end(), Stringpool_sort_comparison());
245 // Offset 0 is reserved for the empty string.
247 for (size_t i
= 0; i
< count
; ++i
)
249 if (v
[i
]->first
[0] == '\0')
250 v
[i
]->second
.second
= 0;
251 else if (i
> 0 && Stringpool::is_suffix(v
[i
]->first
, v
[i
- 1]->first
))
252 v
[i
]->second
.second
= (v
[i
- 1]->second
.second
253 + strlen(v
[i
- 1]->first
)
254 - strlen(v
[i
]->first
));
257 v
[i
]->second
.second
= offset
;
258 offset
+= strlen(v
[i
]->first
) + 1;
262 this->strtab_size_
= offset
;
265 // Get the offset of a string in the ELF strtab. The string must
269 Stringpool::get_offset(const char* s
) const
271 gold_assert(this->strtab_size_
!= 0);
272 String_set_type::const_iterator p
= this->string_set_
.find(s
);
273 if (p
!= this->string_set_
.end())
274 return p
->second
.second
;
278 // Write the ELF strtab into the output file at the specified offset.
281 Stringpool::write(Output_file
* of
, off_t offset
)
283 gold_assert(this->strtab_size_
!= 0);
284 unsigned char* viewu
= of
->get_output_view(offset
, this->strtab_size_
);
285 char* view
= reinterpret_cast<char*>(viewu
);
287 for (String_set_type::const_iterator p
= this->string_set_
.begin();
288 p
!= this->string_set_
.end();
290 strcpy(view
+ p
->second
.second
, p
->first
);
291 of
->write_output_view(offset
, this->strtab_size_
, viewu
);
294 } // End namespace gold.