OSDN Git Service

Rework Stringpool to not compute the hash code twice when adding a new
[pf3gnuchains/pf3gnuchains3x.git] / gold / stringpool.h
1 // stringpool.h -- a string pool for gold    -*- C++ -*-
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include <string>
24 #include <list>
25
26 #ifndef GOLD_STRINGPOOL_H
27 #define GOLD_STRINGPOOL_H
28
29 namespace gold
30 {
31
32 class Output_file;
33
34 // A Stringpool is a pool of unique strings.  It provides the
35 // following features:
36
37 // Every string in the pool is unique.  Thus, if you have two strings
38 // in the Stringpool, you can compare them for equality by using
39 // pointer comparison rather than string comparison.
40
41 // There is a key associated with every string in the pool.  If you
42 // add strings to the Stringpool in the same order, then the key for
43 // each string will always be the same for any run of the linker.
44 // This is not true of the string pointers themselves, as they may
45 // change due to address space randomization.  Some parts of the
46 // linker (e.g., the symbol table) use the key value instead of the
47 // string pointer so that repeated runs of the linker will generate
48 // precisely the same output.
49
50 // When you add a string to a Stringpool, Stringpool will optionally
51 // make a copy of it.  Thus there is no requirement to keep a copy
52 // elsewhere.
53
54 // A Stringpool can be turned into a string table, a sequential series
55 // of null terminated strings.  The first string may optionally be a
56 // single zero byte, as required for SHT_STRTAB sections.  This
57 // conversion is only permitted after all strings have been added to
58 // the Stringpool.  After doing this conversion, you can ask for the
59 // offset of any string in the stringpool in the string table, and you
60 // can write the resulting string table to an output file.
61
62 // When a Stringpool is turned into a string table, then as an
63 // optimization it will reuse string suffixes to avoid duplicating
64 // strings.  That is, given the strings "abc" and "bc", only the
65 // string "abc" will be stored, and "bc" will be represented by an
66 // offset into the middle of the string "abc".
67
68 // Stringpools are implemented in terms of Stringpool_template, which
69 // is generalized on the type of character used for the strings.  Most
70 // uses will want the Stringpool type which uses char.  Other cases
71 // are used for merging wide string constants.
72
73 template<typename Stringpool_char>
74 class Stringpool_template
75 {
76  public:
77   // The type of a key into the stringpool.  As described above, a key
78   // value will always be the same during any run of the linker.  Zero
79   // is never a valid key value.
80   typedef size_t Key;
81
82   // Create a Stringpool.
83   Stringpool_template();
84
85   ~Stringpool_template();
86
87   // Clear all the data from the stringpool.
88   void
89   clear();
90
91   // Indicate that we should not reserve offset 0 to hold the empty
92   // string when converting the stringpool to a string table.  This
93   // should not be called for a proper ELF SHT_STRTAB section.
94   void
95   set_no_zero_null()
96   { this->zero_null_ = false; }
97
98   // Add the string S to the pool.  This returns a canonical permanent
99   // pointer to the string in the pool.  If COPY is true, the string
100   // is copied into permanent storage.  If PKEY is not NULL, this sets
101   // *PKEY to the key for the string.
102   const Stringpool_char*
103   add(const Stringpool_char* s, bool copy, Key* pkey);
104
105   // Add the prefix of length LEN of string S to the pool.
106   const Stringpool_char*
107   add_prefix(const Stringpool_char* s, size_t len, Key* pkey);
108
109   // If the string S is present in the pool, return the canonical
110   // string pointer.  Otherwise, return NULL.  If PKEY is not NULL,
111   // set *PKEY to the key.
112   const Stringpool_char*
113   find(const Stringpool_char* s, Key* pkey) const;
114
115   // Turn the stringpool into a string table: determine the offsets of
116   // all the strings.  After this is called, no more strings may be
117   // added to the stringpool.
118   void
119   set_string_offsets();
120
121   // Get the offset of the string S in the string table.  This returns
122   // the offset in bytes, not in units of Stringpool_char.  This may
123   // only be called after set_string_offsets has been called.
124   off_t
125   get_offset(const Stringpool_char* s) const;
126
127   // Get the offset of the string S in the string table.
128   off_t
129   get_offset(const std::basic_string<Stringpool_char>& s) const
130   { return this->get_offset(s.c_str()); }
131
132   // Get the size of the string table.  This returns the number of
133   // bytes, not in units of Stringpool_char.
134   off_t
135   get_strtab_size() const
136   {
137     gold_assert(this->strtab_size_ != 0);
138     return this->strtab_size_;
139   }
140
141   // Write the string table into the output file at the specified
142   // offset.
143   void
144   write(Output_file*, off_t offset);
145
146   // Write the string table into the specified buffer, of the
147   // specified size.  buffer_size should be at least
148   // get_strtab_size().
149   void
150   write_to_buffer(unsigned char* buffer, size_t buffer_size);
151
152   // Dump statistical information to stderr.
153   void
154   print_stats(const char*) const;
155
156  private:
157   Stringpool_template(const Stringpool_template&);
158   Stringpool_template& operator=(const Stringpool_template&);
159
160   // Return the length of a string in units of Stringpool_char.
161   static size_t
162   string_length(const Stringpool_char*);
163
164   // Return whether two strings are equal.
165   static bool
166   string_equal(const Stringpool_char*, const Stringpool_char*);
167
168   // Compute a hash code for a string.  LENGTH is the length of the
169   // string in characters.
170   static size_t
171   string_hash(const Stringpool_char*, size_t length);
172
173   // We store the actual data in a list of these buffers.
174   struct Stringdata
175   {
176     // Length of data in buffer.
177     size_t len;
178     // Allocated size of buffer.
179     size_t alc;
180     // Buffer index.
181     unsigned int index;
182     // Buffer.
183     char data[1];
184   };
185
186   // Copy a string into the buffers, returning a canonical string.
187   const Stringpool_char*
188   add_string(const Stringpool_char*, size_t, Key*);
189
190   // Return whether s1 is a suffix of s2.
191   static bool
192   is_suffix(const Stringpool_char* s1, size_t len1,
193             const Stringpool_char* s2, size_t len2);
194
195   // The hash table key includes the string, the length of the string,
196   // and the hash code for the string.  We put the hash code
197   // explicitly into the key so that we can do a find()/insert()
198   // sequence without having to recompute the hash.  Computing the
199   // hash code is a significant user of CPU time in the linker.
200   struct Hashkey
201   {
202     const Stringpool_char* string;
203     // Length is in characters, not bytes.
204     size_t length;
205     size_t hash_code;
206
207     // This goes in an STL container, so we need a default
208     // constructor.
209     Hashkey()
210       : string(NULL), length(0), hash_code(0)
211     { }
212
213     // Note that these constructors are relatively expensive, because
214     // they compute the hash code.
215     Hashkey(const Stringpool_char* s)
216       : string(s), length(string_length(s)), hash_code(string_hash(s, length))
217     { }
218
219     Hashkey(const Stringpool_char* s, size_t len)
220       : string(s), length(len), hash_code(string_hash(s, len))
221     { }
222   };
223
224   // Hash function.  This is trivial, since we have already computed
225   // the hash.
226   struct Stringpool_hash
227   {
228     size_t
229     operator()(const Hashkey& hk) const
230     { return hk.hash_code; }
231   };
232
233   // Equality comparison function for hash table.
234   struct Stringpool_eq
235   {
236     bool
237     operator()(const Hashkey&, const Hashkey&) const;
238   };
239
240   // The hash table is a map from strings to a pair of Key and string
241   // table offsets.  We only use the offsets if we turn this into an
242   // string table section.
243
244   typedef std::pair<Key, off_t> Hashval;
245
246   typedef Unordered_map<Hashkey, Hashval, Stringpool_hash,
247                         Stringpool_eq> String_set_type;
248
249   // Comparison routine used when sorting into a string table.
250
251   typedef typename String_set_type::iterator Stringpool_sort_info;
252
253   struct Stringpool_sort_comparison
254   {
255     bool
256     operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const;
257   };
258
259   // List of Stringdata structures.
260   typedef std::list<Stringdata*> Stringdata_list;
261
262   // Mapping from const char* to namepool entry.
263   String_set_type string_set_;
264   // List of buffers.
265   Stringdata_list strings_;
266   // Size of string table.
267   off_t strtab_size_;
268   // Next Stringdata index.
269   unsigned int next_index_;
270   // Next key value for a string we don't copy.
271   int next_uncopied_key_;
272   // Whether to reserve offset 0 to hold the null string.
273   bool zero_null_;
274 };
275
276 // The most common type of Stringpool.
277 typedef Stringpool_template<char> Stringpool;
278
279 } // End namespace gold.
280
281 #endif // !defined(GOLD_STRINGPOOL_H)