daily update
[binutils.git] / gold / stringpool.h
blob80e53bef52bf0623cb9bba9a908d71896fc1ade1
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>
26 #ifndef GOLD_STRINGPOOL_H
27 #define GOLD_STRINGPOOL_H
29 namespace gold
32 class Output_file;
34 // A Stringpool is a pool of unique strings. It provides the
35 // following features:
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.
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.
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.
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.
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".
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.
73 template<typename Stringpool_char>
74 class Stringpool_template
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;
82 // Create a Stringpool.
83 Stringpool_template();
85 ~Stringpool_template();
87 // Indicate that we should not reserve offset 0 to hold the empty
88 // string when converting the stringpool to a string table. This
89 // should not be called for a proper ELF SHT_STRTAB section.
90 void
91 set_no_zero_null()
92 { this->zero_null_ = false; }
94 // Add the string S to the pool. This returns a canonical permanent
95 // pointer to the string in the pool. If COPY is true, the string
96 // is copied into permanent storage. If PKEY is not NULL, this sets
97 // *PKEY to the key for the string.
98 const Stringpool_char*
99 add(const Stringpool_char* s, bool copy, Key* pkey);
101 // Add the prefix of length LEN of string S to the pool.
102 const Stringpool_char*
103 add_prefix(const Stringpool_char* s, size_t len, Key* pkey);
105 // If the string S is present in the pool, return the canonical
106 // string pointer. Otherwise, return NULL. If PKEY is not NULL,
107 // set *PKEY to the key.
108 const Stringpool_char*
109 find(const Stringpool_char* s, Key* pkey) const;
111 // Turn the stringpool into a string table: determine the offsets of
112 // all the strings. After this is called, no more strings may be
113 // added to the stringpool.
114 void
115 set_string_offsets();
117 // Get the offset of the string S in the string table. This returns
118 // the offset in bytes, not in units of Stringpool_char. This may
119 // only be called after set_string_offsets has been called.
120 off_t
121 get_offset(const Stringpool_char* s) const;
123 // Get the offset of the string S in the string table.
124 off_t
125 get_offset(const std::basic_string<Stringpool_char>& s) const
126 { return this->get_offset(s.c_str()); }
128 // Get the size of the string table. This returns the number of
129 // bytes, not in units of Stringpool_char.
130 off_t
131 get_strtab_size() const
133 gold_assert(this->strtab_size_ != 0);
134 return this->strtab_size_;
137 // Write the string table into the output file at the specified
138 // offset.
139 void
140 write(Output_file*, off_t offset);
142 private:
143 Stringpool_template(const Stringpool_template&);
144 Stringpool_template& operator=(const Stringpool_template&);
146 // Return the length of a string in units of Stringpool_char.
147 static size_t
148 string_length(const Stringpool_char*);
150 // We store the actual data in a list of these buffers.
151 struct Stringdata
153 // Length of data in buffer.
154 size_t len;
155 // Allocated size of buffer.
156 size_t alc;
157 // Buffer index.
158 unsigned int index;
159 // Buffer.
160 char data[1];
163 // Copy a string into the buffers, returning a canonical string.
164 const Stringpool_char*
165 add_string(const Stringpool_char*, Key*);
167 // Hash function.
168 struct Stringpool_hash
170 size_t
171 operator()(const Stringpool_char*) const;
174 // Equality comparison function for hash table.
175 struct Stringpool_eq
177 bool
178 operator()(const Stringpool_char* p1, const Stringpool_char* p2) const;
181 // Return whether s1 is a suffix of s2.
182 static bool
183 is_suffix(const Stringpool_char* s1, size_t len1,
184 const Stringpool_char* s2, size_t len2);
186 // The hash table is a map from string names to a pair of Key and
187 // string table offsets. We only use the offsets if we turn this
188 // into an string table section.
190 typedef std::pair<Key, off_t> Val;
192 #ifdef HAVE_TR1_UNORDERED_SET
193 typedef Unordered_map<const Stringpool_char*, Val, Stringpool_hash,
194 Stringpool_eq,
195 std::allocator<std::pair<const Stringpool_char* const,
196 Val> >,
197 true> String_set_type;
198 #else
199 typedef Unordered_map<const Stringpool_char*, Val, Stringpool_hash,
200 Stringpool_eq> String_set_type;
201 #endif
203 // Comparison routine used when sorting into a string table. We
204 // store string-sizes in the sort-vector so we don't have to
205 // recompute them log(n) times as we sort.
206 struct Stringpool_sort_info
208 typename String_set_type::iterator it;
209 size_t string_length;
210 Stringpool_sort_info(typename String_set_type::iterator i, size_t s)
211 : it(i), string_length(s)
215 struct Stringpool_sort_comparison
217 bool
218 operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const;
221 // List of Stringdata structures.
222 typedef std::list<Stringdata*> Stringdata_list;
224 // Mapping from const char* to namepool entry.
225 String_set_type string_set_;
226 // List of buffers.
227 Stringdata_list strings_;
228 // Size of string table.
229 off_t strtab_size_;
230 // Next Stringdata index.
231 unsigned int next_index_;
232 // Next key value for a string we don't copy.
233 int next_uncopied_key_;
234 // Whether to reserve offset 0 to hold the null string.
235 bool zero_null_;
238 // The most common type of Stringpool.
239 typedef Stringpool_template<char> Stringpool;
241 } // End namespace gold.
243 #endif // !defined(GOLD_STRINGPOOL_H)