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