daily update
[binutils.git] / gold / stringpool.h
blob0257894a869ab110eecdf7e5069bb139e98bb45d
1 // stringpool.h -- a string pool for gold -*- C++ -*-
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.
23 #include <string>
24 #include <list>
25 #include <vector>
27 #ifndef GOLD_STRINGPOOL_H
28 #define GOLD_STRINGPOOL_H
30 namespace gold
33 class Output_file;
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
53 // elsewhere.
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
62 // file.
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>
76 class Chunked_vector
78 public:
79 Chunked_vector()
80 : chunks_()
81 { }
83 // Clear the elements.
84 void
85 clear()
86 { this->chunks_.clear(); }
88 // Reserve elements.
89 void
90 reserve(unsigned int n)
92 n += chunk_size - 1;
93 while (n >= chunk_size)
95 this->chunks_.push_back(Element_vector());
96 this->chunks_.back().reserve(chunk_size);
97 n -= chunk_size;
101 // Get the number of elements.
102 size_t
103 size() const
105 if (this->chunks_.empty())
106 return 0;
107 else
108 return ((this->chunks_.size() - 1) * chunk_size
109 + this->chunks_.back().size());
112 // Push a new element on the back of the vector.
113 void
114 push_back(const Element& element)
116 if (this->chunks_.empty() || this->chunks_.back().size() == chunk_size)
118 this->chunks_.push_back(Element_vector());
119 this->chunks_.back().reserve(chunk_size);
121 this->chunks_.back().push_back(element);
124 // Return a reference to an entry in the vector.
125 Element&
126 operator[](size_t i)
127 { return this->chunks_[i / chunk_size][i % chunk_size]; }
129 const Element&
130 operator[](size_t i) const
131 { return this->chunks_[i / chunk_size][i % chunk_size]; }
133 private:
134 static const unsigned int chunk_size = 8192;
136 typedef std::vector<Element> Element_vector;
137 typedef std::vector<Element_vector> Chunk_vector;
139 Chunk_vector chunks_;
143 // Stringpools are implemented in terms of Stringpool_template, which
144 // is generalized on the type of character used for the strings. Most
145 // uses will want the Stringpool type which uses char. Other cases
146 // are used for merging wide string constants.
148 template<typename Stringpool_char>
149 class Stringpool_template
151 public:
152 // The type of a key into the stringpool. As described above, a key
153 // value will always be the same during any run of the linker. Zero
154 // is never a valid key value.
155 typedef size_t Key;
157 // Create a Stringpool.
158 Stringpool_template();
160 ~Stringpool_template();
162 // Clear all the data from the stringpool.
163 void
164 clear();
166 // Hint to the stringpool class that you intend to insert n additional
167 // elements. The stringpool class can use this info however it likes;
168 // in practice it will resize its internal hashtables to make room.
169 void
170 reserve(unsigned int n);
172 // Indicate that we should not reserve offset 0 to hold the empty
173 // string when converting the stringpool to a string table. This
174 // should not be called for a proper ELF SHT_STRTAB section.
175 void
176 set_no_zero_null()
177 { this->zero_null_ = false; }
179 // Add the string S to the pool. This returns a canonical permanent
180 // pointer to the string in the pool. If COPY is true, the string
181 // is copied into permanent storage. If PKEY is not NULL, this sets
182 // *PKEY to the key for the string.
183 const Stringpool_char*
184 add(const Stringpool_char* s, bool copy, Key* pkey);
186 // Add string S of length LEN characters to the pool. If COPY is
187 // true, S need not be null terminated.
188 const Stringpool_char*
189 add_with_length(const Stringpool_char* s, size_t len, bool copy, Key* pkey);
191 // If the string S is present in the pool, return the canonical
192 // string pointer. Otherwise, return NULL. If PKEY is not NULL,
193 // set *PKEY to the key.
194 const Stringpool_char*
195 find(const Stringpool_char* s, Key* pkey) const;
197 // Turn the stringpool into a string table: determine the offsets of
198 // all the strings. After this is called, no more strings may be
199 // added to the stringpool.
200 void
201 set_string_offsets();
203 // Get the offset of the string S in the string table. This returns
204 // the offset in bytes, not in units of Stringpool_char. This may
205 // only be called after set_string_offsets has been called.
206 section_offset_type
207 get_offset(const Stringpool_char* s) const;
209 // Get the offset of the string S in the string table.
210 section_offset_type
211 get_offset(const std::basic_string<Stringpool_char>& s) const
212 { return this->get_offset_with_length(s.c_str(), s.size()); }
214 // Get the offset of string S, with length LENGTH characters, in the
215 // string table.
216 section_offset_type
217 get_offset_with_length(const Stringpool_char* s, size_t length) const;
219 // Get the offset of the string with key K.
220 section_offset_type
221 get_offset_from_key(Key k) const
223 gold_assert(k < this->key_to_offset_.size());
224 return this->key_to_offset_[k];
227 // Get the size of the string table. This returns the number of
228 // bytes, not in units of Stringpool_char.
229 section_size_type
230 get_strtab_size() const
232 gold_assert(this->strtab_size_ != 0);
233 return this->strtab_size_;
236 // Write the string table into the output file at the specified
237 // offset.
238 void
239 write(Output_file*, off_t offset);
241 // Write the string table into the specified buffer, of the
242 // specified size. buffer_size should be at least
243 // get_strtab_size().
244 void
245 write_to_buffer(unsigned char* buffer, section_size_type buffer_size);
247 // Dump statistical information to stderr.
248 void
249 print_stats(const char*) const;
251 private:
252 Stringpool_template(const Stringpool_template&);
253 Stringpool_template& operator=(const Stringpool_template&);
255 // Return the length of a string in units of Stringpool_char.
256 static size_t
257 string_length(const Stringpool_char*);
259 // Return whether two strings are equal.
260 static bool
261 string_equal(const Stringpool_char*, const Stringpool_char*);
263 // Compute a hash code for a string. LENGTH is the length of the
264 // string in characters.
265 static size_t
266 string_hash(const Stringpool_char*, size_t length);
268 // We store the actual data in a list of these buffers.
269 struct Stringdata
271 // Length of data in buffer.
272 size_t len;
273 // Allocated size of buffer.
274 size_t alc;
275 // Buffer.
276 char data[1];
279 // Copy a string into the buffers, returning a canonical string.
280 const Stringpool_char*
281 add_string(const Stringpool_char*, size_t);
283 // Return whether s1 is a suffix of s2.
284 static bool
285 is_suffix(const Stringpool_char* s1, size_t len1,
286 const Stringpool_char* s2, size_t len2);
288 // The hash table key includes the string, the length of the string,
289 // and the hash code for the string. We put the hash code
290 // explicitly into the key so that we can do a find()/insert()
291 // sequence without having to recompute the hash. Computing the
292 // hash code is a significant user of CPU time in the linker.
293 struct Hashkey
295 const Stringpool_char* string;
296 // Length is in characters, not bytes.
297 size_t length;
298 size_t hash_code;
300 // This goes in an STL container, so we need a default
301 // constructor.
302 Hashkey()
303 : string(NULL), length(0), hash_code(0)
306 // Note that these constructors are relatively expensive, because
307 // they compute the hash code.
308 explicit Hashkey(const Stringpool_char* s)
309 : string(s), length(string_length(s)), hash_code(string_hash(s, length))
312 Hashkey(const Stringpool_char* s, size_t len)
313 : string(s), length(len), hash_code(string_hash(s, len))
317 // Hash function. This is trivial, since we have already computed
318 // the hash.
319 struct Stringpool_hash
321 size_t
322 operator()(const Hashkey& hk) const
323 { return hk.hash_code; }
326 // Equality comparison function for hash table.
327 struct Stringpool_eq
329 bool
330 operator()(const Hashkey&, const Hashkey&) const;
333 // The hash table is a map from strings to Keys.
335 typedef Key Hashval;
337 typedef Unordered_map<Hashkey, Hashval, Stringpool_hash,
338 Stringpool_eq> String_set_type;
340 // Comparison routine used when sorting into a string table.
342 typedef typename String_set_type::iterator Stringpool_sort_info;
344 struct Stringpool_sort_comparison
346 bool
347 operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const;
350 // Keys map to offsets via a Chunked_vector. We only use the
351 // offsets if we turn this into an string table section.
352 typedef Chunked_vector<section_offset_type> Key_to_offset;
354 // List of Stringdata structures.
355 typedef std::list<Stringdata*> Stringdata_list;
357 // Mapping from const char* to namepool entry.
358 String_set_type string_set_;
359 // Mapping from Key to string table offset.
360 Key_to_offset key_to_offset_;
361 // List of buffers.
362 Stringdata_list strings_;
363 // Size of string table.
364 section_size_type strtab_size_;
365 // Whether to reserve offset 0 to hold the null string.
366 bool zero_null_;
369 // The most common type of Stringpool.
370 typedef Stringpool_template<char> Stringpool;
372 } // End namespace gold.
374 #endif // !defined(GOLD_STRINGPOOL_H)