Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // stringpool.h -- a string pool for gold -*- C++ -*- |
2 | ||
6cb15b7f ILT |
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 | ||
14bfc3f5 ILT |
23 | #include <string> |
24 | #include <list> | |
25 | ||
14bfc3f5 ILT |
26 | #ifndef GOLD_STRINGPOOL_H |
27 | #define GOLD_STRINGPOOL_H | |
28 | ||
29 | namespace gold | |
30 | { | |
31 | ||
61ba1cf9 ILT |
32 | class Output_file; |
33 | ||
835965e6 ILT |
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 | ||
bc56b3fb ILT |
50 | // When you add a string to a Stringpool, Stringpool will make a copy |
51 | // of it. Thus there is no need to keep a copy elsewhere. | |
52 | ||
835965e6 ILT |
53 | // A Stringpool can be turned into a string table, a sequential series |
54 | // of null terminated strings. The first string may optionally be a | |
55 | // single zero byte, as required for SHT_STRTAB sections. This | |
56 | // conversion is only permitted after all strings have been added to | |
57 | // the Stringpool. After doing this conversion, you can ask for the | |
58 | // offset of any string in the stringpool in the string table, and you | |
59 | // can write the resulting string table to an output file. | |
60 | ||
61 | // When a Stringpool is turned into a string table, then as an | |
62 | // optimization it will reuse string suffixes to avoid duplicating | |
63 | // strings. That is, given the strings "abc" and "bc", only the | |
64 | // string "abc" will be stored, and "bc" will be represented by an | |
65 | // offset into the middle of the string "abc". | |
66 | ||
67 | // Stringpools are implemented in terms of Stringpool_template, which | |
68 | // is generalized on the type of character used for the strings. Most | |
69 | // uses will want the Stringpool type which uses char. Other cases | |
70 | // are used for merging wide string constants. | |
71 | ||
b8e6aad9 ILT |
72 | template<typename Stringpool_char> |
73 | class Stringpool_template | |
14bfc3f5 ILT |
74 | { |
75 | public: | |
835965e6 ILT |
76 | // The type of a key into the stringpool. As described above, a key |
77 | // value will always be the same during any run of the linker. Zero | |
78 | // is never a valid key value. | |
f0641a0b ILT |
79 | typedef size_t Key; |
80 | ||
a8b2552e ILT |
81 | // Create a Stringpool. |
82 | Stringpool_template(); | |
14bfc3f5 | 83 | |
b8e6aad9 | 84 | ~Stringpool_template(); |
14bfc3f5 | 85 | |
a8b2552e ILT |
86 | // Indicate that we should not reserve offset 0 to hold the empty |
87 | // string when converting the stringpool to a string table. This | |
88 | // should not be called for a proper ELF SHT_STRTAB section. | |
89 | void | |
90 | set_no_zero_null() | |
91 | { this->zero_null_ = false; } | |
92 | ||
835965e6 ILT |
93 | // Add the string S to the pool. This returns a canonical permanent |
94 | // pointer to the string in the pool. If PKEY is not NULL, this | |
95 | // sets *PKEY to the key for the string. | |
b8e6aad9 | 96 | const Stringpool_char* |
835965e6 | 97 | add(const Stringpool_char* s, Key* pkey); |
14bfc3f5 | 98 | |
835965e6 | 99 | // Add the string S to the pool. |
b8e6aad9 ILT |
100 | const Stringpool_char* |
101 | add(const std::basic_string<Stringpool_char>& s, Key* pkey) | |
f0641a0b | 102 | { return this->add(s.c_str(), pkey); } |
14bfc3f5 | 103 | |
835965e6 | 104 | // Add the prefix of length LEN of string S to the pool. |
b8e6aad9 | 105 | const Stringpool_char* |
835965e6 | 106 | add(const Stringpool_char* s, size_t len, Key* pkey); |
61ba1cf9 | 107 | |
835965e6 ILT |
108 | // If the string S is present in the pool, return the canonical |
109 | // string pointer. Otherwise, return NULL. If PKEY is not NULL, | |
110 | // set *PKEY to the key. | |
b8e6aad9 | 111 | const Stringpool_char* |
835965e6 | 112 | find(const Stringpool_char* s, Key* pkey) const; |
61ba1cf9 | 113 | |
835965e6 ILT |
114 | // Turn the stringpool into a string table: determine the offsets of |
115 | // all the strings. After this is called, no more strings may be | |
116 | // added to the stringpool. | |
61ba1cf9 ILT |
117 | void |
118 | set_string_offsets(); | |
119 | ||
835965e6 ILT |
120 | // Get the offset of the string S in the string table. This returns |
121 | // the offset in bytes, not in units of Stringpool_char. This may | |
122 | // only be called after set_string_offsets has been called. | |
61ba1cf9 | 123 | off_t |
835965e6 | 124 | get_offset(const Stringpool_char* s) const; |
61ba1cf9 | 125 | |
835965e6 | 126 | // Get the offset of the string S in the string table. |
61ba1cf9 | 127 | off_t |
b8e6aad9 | 128 | get_offset(const std::basic_string<Stringpool_char>& s) const |
61ba1cf9 ILT |
129 | { return this->get_offset(s.c_str()); } |
130 | ||
835965e6 ILT |
131 | // Get the size of the string table. This returns the number of |
132 | // bytes, not in units of Stringpool_char. | |
61ba1cf9 ILT |
133 | off_t |
134 | get_strtab_size() const | |
a3ad94ed ILT |
135 | { |
136 | gold_assert(this->strtab_size_ != 0); | |
137 | return this->strtab_size_; | |
138 | } | |
61ba1cf9 | 139 | |
835965e6 ILT |
140 | // Write the string table into the output file at the specified |
141 | // offset. | |
61ba1cf9 ILT |
142 | void |
143 | write(Output_file*, off_t offset); | |
14bfc3f5 ILT |
144 | |
145 | private: | |
b8e6aad9 ILT |
146 | Stringpool_template(const Stringpool_template&); |
147 | Stringpool_template& operator=(const Stringpool_template&); | |
148 | ||
835965e6 | 149 | // Return the length of a string in units of Stringpool_char. |
b8e6aad9 ILT |
150 | static size_t |
151 | string_length(const Stringpool_char*); | |
14bfc3f5 | 152 | |
61ba1cf9 ILT |
153 | // We store the actual data in a list of these buffers. |
154 | struct Stringdata | |
14bfc3f5 ILT |
155 | { |
156 | // Length of data in buffer. | |
157 | size_t len; | |
158 | // Allocated size of buffer. | |
159 | size_t alc; | |
f0641a0b ILT |
160 | // Buffer index. |
161 | unsigned int index; | |
14bfc3f5 ILT |
162 | // Buffer. |
163 | char data[1]; | |
164 | }; | |
165 | ||
61ba1cf9 | 166 | // Copy a string into the buffers, returning a canonical string. |
b8e6aad9 ILT |
167 | const Stringpool_char* |
168 | add_string(const Stringpool_char*, Key*); | |
14bfc3f5 | 169 | |
835965e6 | 170 | // Hash function. |
14bfc3f5 ILT |
171 | struct Stringpool_hash |
172 | { | |
173 | size_t | |
b8e6aad9 | 174 | operator()(const Stringpool_char*) const; |
14bfc3f5 ILT |
175 | }; |
176 | ||
835965e6 | 177 | // Equality comparison function for hash table. |
14bfc3f5 ILT |
178 | struct Stringpool_eq |
179 | { | |
180 | bool | |
b8e6aad9 | 181 | operator()(const Stringpool_char* p1, const Stringpool_char* p2) const; |
14bfc3f5 ILT |
182 | }; |
183 | ||
61ba1cf9 | 184 | // Return whether s1 is a suffix of s2. |
b8e6aad9 | 185 | static bool |
614f30a2 ILT |
186 | is_suffix(const Stringpool_char* s1, size_t len1, |
187 | const Stringpool_char* s2, size_t len2); | |
61ba1cf9 | 188 | |
f0641a0b | 189 | // The hash table is a map from string names to a pair of Key and |
835965e6 ILT |
190 | // string table offsets. We only use the offsets if we turn this |
191 | // into an string table section. | |
f0641a0b ILT |
192 | |
193 | typedef std::pair<Key, off_t> Val; | |
61ba1cf9 | 194 | |
274e99f9 | 195 | #ifdef HAVE_TR1_UNORDERED_SET |
b8e6aad9 | 196 | typedef Unordered_map<const Stringpool_char*, Val, Stringpool_hash, |
61ba1cf9 | 197 | Stringpool_eq, |
b8e6aad9 ILT |
198 | std::allocator<std::pair<const Stringpool_char* const, |
199 | Val> >, | |
14bfc3f5 | 200 | true> String_set_type; |
274e99f9 | 201 | #else |
b8e6aad9 | 202 | typedef Unordered_map<const Stringpool_char*, Val, Stringpool_hash, |
61ba1cf9 | 203 | Stringpool_eq> String_set_type; |
274e99f9 ILT |
204 | #endif |
205 | ||
835965e6 | 206 | // Comparison routine used when sorting into a string table. We |
614f30a2 ILT |
207 | // store string-sizes in the sort-vector so we don't have to |
208 | // recompute them log(n) times as we sort. | |
209 | struct Stringpool_sort_info | |
210 | { | |
211 | typename String_set_type::iterator it; | |
212 | size_t string_length; | |
213 | Stringpool_sort_info(typename String_set_type::iterator i, size_t s) | |
214 | : it(i), string_length(s) | |
215 | { } | |
216 | }; | |
61ba1cf9 ILT |
217 | |
218 | struct Stringpool_sort_comparison | |
219 | { | |
220 | bool | |
614f30a2 | 221 | operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const; |
61ba1cf9 ILT |
222 | }; |
223 | ||
f0641a0b ILT |
224 | // List of Stringdata structures. |
225 | typedef std::list<Stringdata*> Stringdata_list; | |
226 | ||
227 | // Mapping from const char* to namepool entry. | |
14bfc3f5 | 228 | String_set_type string_set_; |
f0641a0b ILT |
229 | // List of buffers. |
230 | Stringdata_list strings_; | |
835965e6 | 231 | // Size of string table. |
61ba1cf9 | 232 | off_t strtab_size_; |
f0641a0b ILT |
233 | // Next Stringdata index. |
234 | unsigned int next_index_; | |
b8e6aad9 ILT |
235 | // Whether to reserve offset 0 to hold the null string. |
236 | bool zero_null_; | |
14bfc3f5 ILT |
237 | }; |
238 | ||
b8e6aad9 ILT |
239 | // The most common type of Stringpool. |
240 | typedef Stringpool_template<char> Stringpool; | |
241 | ||
14bfc3f5 ILT |
242 | } // End namespace gold. |
243 | ||
244 | #endif // !defined(GOLD_STRINGPOOL_H) |