1 // stringpool.h -- a string pool for gold -*- C++ -*-
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.
27 #ifndef GOLD_STRINGPOOL_H
28 #define GOLD_STRINGPOOL_H
35 // A Stringpool is a pool of unique strings. It provides the
36 // following features:
38 // Every string in the pool is unique. Thus, if you have two strings
39 // in the Stringpool, you can compare them for equality by using
40 // pointer comparison rather than string comparison.
42 // There is a key associated with every string in the pool. If you
43 // add strings to the Stringpool in the same order, then the key for
44 // each string will always be the same for any run of the linker.
45 // This is not true of the string pointers themselves, as they may
46 // change due to address space randomization. Some parts of the
47 // linker (e.g., the symbol table) use the key value instead of the
48 // string pointer so that repeated runs of the linker will generate
49 // precisely the same output.
51 // When you add a string to a Stringpool, Stringpool will optionally
52 // make a copy of it. Thus there is no requirement to keep a copy
55 // A Stringpool can be turned into a string table, a sequential series
56 // of null terminated strings. The first string may optionally be a
57 // single zero byte, as required for SHT_STRTAB sections. This
58 // conversion is only permitted after all strings have been added to
59 // the Stringpool. After doing this conversion, you can ask for the
60 // offset of any string (or any key) in the stringpool in the string
61 // table, and you can write the resulting string table to an output
64 // When a Stringpool is turned into a string table, then as an
65 // optimization it will reuse string suffixes to avoid duplicating
66 // strings. That is, given the strings "abc" and "bc", only the
67 // string "abc" will be stored, and "bc" will be represented by an
68 // offset into the middle of the string "abc".
71 // A simple chunked vector class--this is a subset of std::vector
72 // which stores memory in chunks. We don't provide iterators, because
73 // we don't need them.
75 template<typename Element
>
83 // Clear the elements.
87 this->chunks_
.clear();
93 reserve(unsigned int n
)
95 if (n
> this->chunks_
.size() * chunk_size
)
97 this->chunks_
.resize((n
+ chunk_size
- 1) / chunk_size
);
98 // We need to call reserve() of all chunks since changing
99 // this->chunks_ casues Element_vectors to be copied. The
100 // reserved capacity of an Element_vector may be lost in copying.
101 for (size_t i
= 0; i
< this->chunks_
.size(); ++i
)
102 this->chunks_
[i
].reserve(chunk_size
);
106 // Get the number of elements.
109 { return this->size_
; }
111 // Push a new element on the back of the vector.
113 push_back(const Element
& element
)
115 size_t chunk_index
= this->size_
/ chunk_size
;
116 if (chunk_index
>= this->chunks_
.size())
118 this->chunks_
.push_back(Element_vector());
119 this->chunks_
.back().reserve(chunk_size
);
120 gold_assert(chunk_index
< this->chunks_
.size());
122 this->chunks_
[chunk_index
].push_back(element
);
126 // Return a reference to an entry in the vector.
129 { return this->chunks_
[i
/ chunk_size
][i
% chunk_size
]; }
132 operator[](size_t i
) const
133 { return this->chunks_
[i
/ chunk_size
][i
% chunk_size
]; }
136 static const unsigned int chunk_size
= 8192;
138 typedef std::vector
<Element
> Element_vector
;
139 typedef std::vector
<Element_vector
> Chunk_vector
;
141 Chunk_vector chunks_
;
146 // Stringpools are implemented in terms of Stringpool_template, which
147 // is generalized on the type of character used for the strings. Most
148 // uses will want the Stringpool type which uses char. Other cases
149 // are used for merging wide string constants.
151 template<typename Stringpool_char
>
152 class Stringpool_template
155 // The type of a key into the stringpool. As described above, a key
156 // value will always be the same during any run of the linker. Zero
157 // is never a valid key value.
160 // Create a Stringpool.
161 Stringpool_template();
163 ~Stringpool_template();
165 // Clear all the data from the stringpool.
169 // Hint to the stringpool class that you intend to insert n additional
170 // elements. The stringpool class can use this info however it likes;
171 // in practice it will resize its internal hashtables to make room.
173 reserve(unsigned int n
);
175 // Indicate that we should not reserve offset 0 to hold the empty
176 // string when converting the stringpool to a string table. This
177 // should not be called for a proper ELF SHT_STRTAB section.
181 gold_assert(this->string_set_
.empty()
182 && this->offset_
== sizeof(Stringpool_char
));
183 this->zero_null_
= false;
187 // Indicate that this string pool should be optimized, even if not
191 { this->optimize_
= true; }
193 // Add the string S to the pool. This returns a canonical permanent
194 // pointer to the string in the pool. If COPY is true, the string
195 // is copied into permanent storage. If PKEY is not NULL, this sets
196 // *PKEY to the key for the string.
197 const Stringpool_char
*
198 add(const Stringpool_char
* s
, bool copy
, Key
* pkey
);
200 // Add string S of length LEN characters to the pool. If COPY is
201 // true, S need not be null terminated.
202 const Stringpool_char
*
203 add_with_length(const Stringpool_char
* s
, size_t len
, bool copy
, Key
* pkey
);
205 // If the string S is present in the pool, return the canonical
206 // string pointer. Otherwise, return NULL. If PKEY is not NULL,
207 // set *PKEY to the key.
208 const Stringpool_char
*
209 find(const Stringpool_char
* s
, Key
* pkey
) const;
211 // Turn the stringpool into a string table: determine the offsets of
212 // all the strings. After this is called, no more strings may be
213 // added to the stringpool.
215 set_string_offsets();
217 // Get the offset of the string S in the string table. This returns
218 // the offset in bytes, not in units of Stringpool_char. This may
219 // only be called after set_string_offsets has been called.
221 get_offset(const Stringpool_char
* s
) const;
223 // Get the offset of the string S in the string table.
225 get_offset(const std::basic_string
<Stringpool_char
>& s
) const
226 { return this->get_offset_with_length(s
.c_str(), s
.size()); }
228 // Get the offset of string S, with length LENGTH characters, in the
231 get_offset_with_length(const Stringpool_char
* s
, size_t length
) const;
233 // Get the offset of the string with key K.
235 get_offset_from_key(Key k
) const
237 gold_assert(k
<= this->key_to_offset_
.size());
238 return this->key_to_offset_
[k
- 1];
241 // Get the size of the string table. This returns the number of
242 // bytes, not in units of Stringpool_char.
244 get_strtab_size() const
246 gold_assert(this->strtab_size_
!= 0);
247 return this->strtab_size_
;
250 // Write the string table into the output file at the specified
253 write(Output_file
*, off_t offset
);
255 // Write the string table into the specified buffer, of the
256 // specified size. buffer_size should be at least
257 // get_strtab_size().
259 write_to_buffer(unsigned char* buffer
, section_size_type buffer_size
);
261 // Dump statistical information to stderr.
263 print_stats(const char*) const;
266 Stringpool_template(const Stringpool_template
&);
267 Stringpool_template
& operator=(const Stringpool_template
&);
269 // Return the length of a string in units of Stringpool_char.
271 string_length(const Stringpool_char
*);
273 // Return whether two strings are equal.
275 string_equal(const Stringpool_char
*, const Stringpool_char
*);
277 // Compute a hash code for a string. LENGTH is the length of the
278 // string in characters.
280 string_hash(const Stringpool_char
*, size_t length
);
282 // We store the actual data in a list of these buffers.
285 // Length of data in buffer.
287 // Allocated size of buffer.
293 // Add a new key offset entry.
295 new_key_offset(size_t);
297 // Copy a string into the buffers, returning a canonical string.
298 const Stringpool_char
*
299 add_string(const Stringpool_char
*, size_t);
301 // Return whether s1 is a suffix of s2.
303 is_suffix(const Stringpool_char
* s1
, size_t len1
,
304 const Stringpool_char
* s2
, size_t len2
);
306 // The hash table key includes the string, the length of the string,
307 // and the hash code for the string. We put the hash code
308 // explicitly into the key so that we can do a find()/insert()
309 // sequence without having to recompute the hash. Computing the
310 // hash code is a significant user of CPU time in the linker.
313 const Stringpool_char
* string
;
314 // Length is in characters, not bytes.
318 // This goes in an STL container, so we need a default
321 : string(NULL
), length(0), hash_code(0)
324 // Note that these constructors are relatively expensive, because
325 // they compute the hash code.
326 explicit Hashkey(const Stringpool_char
* s
)
327 : string(s
), length(string_length(s
)), hash_code(string_hash(s
, length
))
330 Hashkey(const Stringpool_char
* s
, size_t len
)
331 : string(s
), length(len
), hash_code(string_hash(s
, len
))
335 // Hash function. This is trivial, since we have already computed
337 struct Stringpool_hash
340 operator()(const Hashkey
& hk
) const
341 { return hk
.hash_code
; }
344 // Equality comparison function for hash table.
348 operator()(const Hashkey
&, const Hashkey
&) const;
351 // The hash table is a map from strings to Keys.
355 typedef Unordered_map
<Hashkey
, Hashval
, Stringpool_hash
,
356 Stringpool_eq
> String_set_type
;
358 // Comparison routine used when sorting into a string table.
360 typedef typename
String_set_type::iterator Stringpool_sort_info
;
362 struct Stringpool_sort_comparison
365 operator()(const Stringpool_sort_info
&, const Stringpool_sort_info
&) const;
368 // Keys map to offsets via a Chunked_vector. We only use the
369 // offsets if we turn this into an string table section.
370 typedef Chunked_vector
<section_offset_type
> Key_to_offset
;
372 // List of Stringdata structures.
373 typedef std::list
<Stringdata
*> Stringdata_list
;
375 // Mapping from const char* to namepool entry.
376 String_set_type string_set_
;
377 // Mapping from Key to string table offset.
378 Key_to_offset key_to_offset_
;
380 Stringdata_list strings_
;
381 // Size of string table.
382 section_size_type strtab_size_
;
383 // Whether to reserve offset 0 to hold the null string.
385 // Whether to optimize the string table.
387 // offset of the next string.
388 section_offset_type offset_
;
391 // The most common type of Stringpool.
392 typedef Stringpool_template
<char> Stringpool
;
394 } // End namespace gold.
396 #endif // !defined(GOLD_STRINGPOOL_H)