1*a9fa9459Szrj // stringpool.h -- a string pool for gold -*- C++ -*-
2*a9fa9459Szrj
3*a9fa9459Szrj // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*a9fa9459Szrj
6*a9fa9459Szrj // This file is part of gold.
7*a9fa9459Szrj
8*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
9*a9fa9459Szrj // it under the terms of the GNU General Public License as published by
10*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
11*a9fa9459Szrj // (at your option) any later version.
12*a9fa9459Szrj
13*a9fa9459Szrj // This program is distributed in the hope that it will be useful,
14*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*a9fa9459Szrj // GNU General Public License for more details.
17*a9fa9459Szrj
18*a9fa9459Szrj // You should have received a copy of the GNU General Public License
19*a9fa9459Szrj // along with this program; if not, write to the Free Software
20*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*a9fa9459Szrj // MA 02110-1301, USA.
22*a9fa9459Szrj
23*a9fa9459Szrj #include <string>
24*a9fa9459Szrj #include <list>
25*a9fa9459Szrj #include <vector>
26*a9fa9459Szrj
27*a9fa9459Szrj #ifndef GOLD_STRINGPOOL_H
28*a9fa9459Szrj #define GOLD_STRINGPOOL_H
29*a9fa9459Szrj
30*a9fa9459Szrj namespace gold
31*a9fa9459Szrj {
32*a9fa9459Szrj
33*a9fa9459Szrj class Output_file;
34*a9fa9459Szrj
35*a9fa9459Szrj // Return the length of a string in units of Char_type.
36*a9fa9459Szrj
37*a9fa9459Szrj template<typename Char_type>
38*a9fa9459Szrj inline size_t
string_length(const Char_type * p)39*a9fa9459Szrj string_length(const Char_type* p)
40*a9fa9459Szrj {
41*a9fa9459Szrj size_t len = 0;
42*a9fa9459Szrj for (; *p != 0; ++p)
43*a9fa9459Szrj ++len;
44*a9fa9459Szrj return len;
45*a9fa9459Szrj }
46*a9fa9459Szrj
47*a9fa9459Szrj // Specialize string_length for char. Maybe we could just use
48*a9fa9459Szrj // std::char_traits<>::length?
49*a9fa9459Szrj
50*a9fa9459Szrj template<>
51*a9fa9459Szrj inline size_t
string_length(const char * p)52*a9fa9459Szrj string_length(const char* p)
53*a9fa9459Szrj {
54*a9fa9459Szrj return strlen(p);
55*a9fa9459Szrj }
56*a9fa9459Szrj
57*a9fa9459Szrj // A Stringpool is a pool of unique strings. It provides the
58*a9fa9459Szrj // following features:
59*a9fa9459Szrj
60*a9fa9459Szrj // Every string in the pool is unique. Thus, if you have two strings
61*a9fa9459Szrj // in the Stringpool, you can compare them for equality by using
62*a9fa9459Szrj // pointer comparison rather than string comparison.
63*a9fa9459Szrj
64*a9fa9459Szrj // There is a key associated with every string in the pool. If you
65*a9fa9459Szrj // add strings to the Stringpool in the same order, then the key for
66*a9fa9459Szrj // each string will always be the same for any run of the linker.
67*a9fa9459Szrj // This is not true of the string pointers themselves, as they may
68*a9fa9459Szrj // change due to address space randomization. Some parts of the
69*a9fa9459Szrj // linker (e.g., the symbol table) use the key value instead of the
70*a9fa9459Szrj // string pointer so that repeated runs of the linker will generate
71*a9fa9459Szrj // precisely the same output.
72*a9fa9459Szrj
73*a9fa9459Szrj // When you add a string to a Stringpool, Stringpool will optionally
74*a9fa9459Szrj // make a copy of it. Thus there is no requirement to keep a copy
75*a9fa9459Szrj // elsewhere.
76*a9fa9459Szrj
77*a9fa9459Szrj // A Stringpool can be turned into a string table, a sequential series
78*a9fa9459Szrj // of null terminated strings. The first string may optionally be a
79*a9fa9459Szrj // single zero byte, as required for SHT_STRTAB sections. This
80*a9fa9459Szrj // conversion is only permitted after all strings have been added to
81*a9fa9459Szrj // the Stringpool. After doing this conversion, you can ask for the
82*a9fa9459Szrj // offset of any string (or any key) in the stringpool in the string
83*a9fa9459Szrj // table, and you can write the resulting string table to an output
84*a9fa9459Szrj // file.
85*a9fa9459Szrj
86*a9fa9459Szrj // When a Stringpool is turned into a string table, then as an
87*a9fa9459Szrj // optimization it will reuse string suffixes to avoid duplicating
88*a9fa9459Szrj // strings. That is, given the strings "abc" and "bc", only the
89*a9fa9459Szrj // string "abc" will be stored, and "bc" will be represented by an
90*a9fa9459Szrj // offset into the middle of the string "abc".
91*a9fa9459Szrj
92*a9fa9459Szrj
93*a9fa9459Szrj // A simple chunked vector class--this is a subset of std::vector
94*a9fa9459Szrj // which stores memory in chunks. We don't provide iterators, because
95*a9fa9459Szrj // we don't need them.
96*a9fa9459Szrj
97*a9fa9459Szrj template<typename Element>
98*a9fa9459Szrj class Chunked_vector
99*a9fa9459Szrj {
100*a9fa9459Szrj public:
Chunked_vector()101*a9fa9459Szrj Chunked_vector()
102*a9fa9459Szrj : chunks_(), size_(0)
103*a9fa9459Szrj { }
104*a9fa9459Szrj
105*a9fa9459Szrj // Clear the elements.
106*a9fa9459Szrj void
clear()107*a9fa9459Szrj clear()
108*a9fa9459Szrj {
109*a9fa9459Szrj this->chunks_.clear();
110*a9fa9459Szrj this->size_ = 0;
111*a9fa9459Szrj }
112*a9fa9459Szrj
113*a9fa9459Szrj // Reserve elements.
114*a9fa9459Szrj void
reserve(unsigned int n)115*a9fa9459Szrj reserve(unsigned int n)
116*a9fa9459Szrj {
117*a9fa9459Szrj if (n > this->chunks_.size() * chunk_size)
118*a9fa9459Szrj {
119*a9fa9459Szrj this->chunks_.resize((n + chunk_size - 1) / chunk_size);
120*a9fa9459Szrj // We need to call reserve() of all chunks since changing
121*a9fa9459Szrj // this->chunks_ casues Element_vectors to be copied. The
122*a9fa9459Szrj // reserved capacity of an Element_vector may be lost in copying.
123*a9fa9459Szrj for (size_t i = 0; i < this->chunks_.size(); ++i)
124*a9fa9459Szrj this->chunks_[i].reserve(chunk_size);
125*a9fa9459Szrj }
126*a9fa9459Szrj }
127*a9fa9459Szrj
128*a9fa9459Szrj // Get the number of elements.
129*a9fa9459Szrj size_t
size()130*a9fa9459Szrj size() const
131*a9fa9459Szrj { return this->size_; }
132*a9fa9459Szrj
133*a9fa9459Szrj // Push a new element on the back of the vector.
134*a9fa9459Szrj void
push_back(const Element & element)135*a9fa9459Szrj push_back(const Element& element)
136*a9fa9459Szrj {
137*a9fa9459Szrj size_t chunk_index = this->size_ / chunk_size;
138*a9fa9459Szrj if (chunk_index >= this->chunks_.size())
139*a9fa9459Szrj {
140*a9fa9459Szrj this->chunks_.push_back(Element_vector());
141*a9fa9459Szrj this->chunks_.back().reserve(chunk_size);
142*a9fa9459Szrj gold_assert(chunk_index < this->chunks_.size());
143*a9fa9459Szrj }
144*a9fa9459Szrj this->chunks_[chunk_index].push_back(element);
145*a9fa9459Szrj this->size_++;
146*a9fa9459Szrj }
147*a9fa9459Szrj
148*a9fa9459Szrj // Return a reference to an entry in the vector.
149*a9fa9459Szrj Element&
150*a9fa9459Szrj operator[](size_t i)
151*a9fa9459Szrj { return this->chunks_[i / chunk_size][i % chunk_size]; }
152*a9fa9459Szrj
153*a9fa9459Szrj const Element&
154*a9fa9459Szrj operator[](size_t i) const
155*a9fa9459Szrj { return this->chunks_[i / chunk_size][i % chunk_size]; }
156*a9fa9459Szrj
157*a9fa9459Szrj private:
158*a9fa9459Szrj static const unsigned int chunk_size = 8192;
159*a9fa9459Szrj
160*a9fa9459Szrj typedef std::vector<Element> Element_vector;
161*a9fa9459Szrj typedef std::vector<Element_vector> Chunk_vector;
162*a9fa9459Szrj
163*a9fa9459Szrj Chunk_vector chunks_;
164*a9fa9459Szrj size_t size_;
165*a9fa9459Szrj };
166*a9fa9459Szrj
167*a9fa9459Szrj
168*a9fa9459Szrj // Stringpools are implemented in terms of Stringpool_template, which
169*a9fa9459Szrj // is generalized on the type of character used for the strings. Most
170*a9fa9459Szrj // uses will want the Stringpool type which uses char. Other cases
171*a9fa9459Szrj // are used for merging wide string constants.
172*a9fa9459Szrj
173*a9fa9459Szrj template<typename Stringpool_char>
174*a9fa9459Szrj class Stringpool_template
175*a9fa9459Szrj {
176*a9fa9459Szrj public:
177*a9fa9459Szrj // The type of a key into the stringpool. As described above, a key
178*a9fa9459Szrj // value will always be the same during any run of the linker. Zero
179*a9fa9459Szrj // is never a valid key value.
180*a9fa9459Szrj typedef size_t Key;
181*a9fa9459Szrj
182*a9fa9459Szrj // Create a Stringpool.
183*a9fa9459Szrj Stringpool_template(uint64_t addralign = 1);
184*a9fa9459Szrj
185*a9fa9459Szrj ~Stringpool_template();
186*a9fa9459Szrj
187*a9fa9459Szrj // Clear all the data from the stringpool.
188*a9fa9459Szrj void
189*a9fa9459Szrj clear();
190*a9fa9459Szrj
191*a9fa9459Szrj // Hint to the stringpool class that you intend to insert n additional
192*a9fa9459Szrj // elements. The stringpool class can use this info however it likes;
193*a9fa9459Szrj // in practice it will resize its internal hashtables to make room.
194*a9fa9459Szrj void
195*a9fa9459Szrj reserve(unsigned int n);
196*a9fa9459Szrj
197*a9fa9459Szrj // Indicate that we should not reserve offset 0 to hold the empty
198*a9fa9459Szrj // string when converting the stringpool to a string table. This
199*a9fa9459Szrj // should not be called for a proper ELF SHT_STRTAB section.
200*a9fa9459Szrj void
set_no_zero_null()201*a9fa9459Szrj set_no_zero_null()
202*a9fa9459Szrj {
203*a9fa9459Szrj gold_assert(this->string_set_.empty()
204*a9fa9459Szrj && this->offset_ == sizeof(Stringpool_char));
205*a9fa9459Szrj this->zero_null_ = false;
206*a9fa9459Szrj this->offset_ = 0;
207*a9fa9459Szrj }
208*a9fa9459Szrj
209*a9fa9459Szrj // Indicate that this string pool should be optimized, even if not
210*a9fa9459Szrj // running with -O2.
211*a9fa9459Szrj void
set_optimize()212*a9fa9459Szrj set_optimize()
213*a9fa9459Szrj { this->optimize_ = true; }
214*a9fa9459Szrj
215*a9fa9459Szrj // Add the string S to the pool. This returns a canonical permanent
216*a9fa9459Szrj // pointer to the string in the pool. If COPY is true, the string
217*a9fa9459Szrj // is copied into permanent storage. If PKEY is not NULL, this sets
218*a9fa9459Szrj // *PKEY to the key for the string.
219*a9fa9459Szrj const Stringpool_char*
220*a9fa9459Szrj add(const Stringpool_char* s, bool copy, Key* pkey);
221*a9fa9459Szrj
222*a9fa9459Szrj // Add the string S to the pool.
223*a9fa9459Szrj const Stringpool_char*
add(const std::basic_string<Stringpool_char> & s,bool copy,Key * pkey)224*a9fa9459Szrj add(const std::basic_string<Stringpool_char>& s, bool copy, Key* pkey)
225*a9fa9459Szrj { return this->add_with_length(s.data(), s.size(), copy, pkey); }
226*a9fa9459Szrj
227*a9fa9459Szrj // Add string S of length LEN characters to the pool. If COPY is
228*a9fa9459Szrj // true, S need not be null terminated.
229*a9fa9459Szrj const Stringpool_char*
230*a9fa9459Szrj add_with_length(const Stringpool_char* s, size_t len, bool copy, Key* pkey);
231*a9fa9459Szrj
232*a9fa9459Szrj // If the string S is present in the pool, return the canonical
233*a9fa9459Szrj // string pointer. Otherwise, return NULL. If PKEY is not NULL,
234*a9fa9459Szrj // set *PKEY to the key.
235*a9fa9459Szrj const Stringpool_char*
236*a9fa9459Szrj find(const Stringpool_char* s, Key* pkey) const;
237*a9fa9459Szrj
238*a9fa9459Szrj // Turn the stringpool into a string table: determine the offsets of
239*a9fa9459Szrj // all the strings. After this is called, no more strings may be
240*a9fa9459Szrj // added to the stringpool.
241*a9fa9459Szrj void
242*a9fa9459Szrj set_string_offsets();
243*a9fa9459Szrj
244*a9fa9459Szrj // Get the offset of the string S in the string table. This returns
245*a9fa9459Szrj // the offset in bytes, not in units of Stringpool_char. This may
246*a9fa9459Szrj // only be called after set_string_offsets has been called.
247*a9fa9459Szrj section_offset_type
248*a9fa9459Szrj get_offset(const Stringpool_char* s) const;
249*a9fa9459Szrj
250*a9fa9459Szrj // Get the offset of the string S in the string table.
251*a9fa9459Szrj section_offset_type
get_offset(const std::basic_string<Stringpool_char> & s)252*a9fa9459Szrj get_offset(const std::basic_string<Stringpool_char>& s) const
253*a9fa9459Szrj { return this->get_offset_with_length(s.c_str(), s.size()); }
254*a9fa9459Szrj
255*a9fa9459Szrj // Get the offset of string S, with length LENGTH characters, in the
256*a9fa9459Szrj // string table.
257*a9fa9459Szrj section_offset_type
258*a9fa9459Szrj get_offset_with_length(const Stringpool_char* s, size_t length) const;
259*a9fa9459Szrj
260*a9fa9459Szrj // Get the offset of the string with key K.
261*a9fa9459Szrj section_offset_type
get_offset_from_key(Key k)262*a9fa9459Szrj get_offset_from_key(Key k) const
263*a9fa9459Szrj {
264*a9fa9459Szrj gold_assert(k <= this->key_to_offset_.size());
265*a9fa9459Szrj return this->key_to_offset_[k - 1];
266*a9fa9459Szrj }
267*a9fa9459Szrj
268*a9fa9459Szrj // Get the size of the string table. This returns the number of
269*a9fa9459Szrj // bytes, not in units of Stringpool_char.
270*a9fa9459Szrj section_size_type
get_strtab_size()271*a9fa9459Szrj get_strtab_size() const
272*a9fa9459Szrj {
273*a9fa9459Szrj gold_assert(this->strtab_size_ != 0);
274*a9fa9459Szrj return this->strtab_size_;
275*a9fa9459Szrj }
276*a9fa9459Szrj
277*a9fa9459Szrj // Write the string table into the output file at the specified
278*a9fa9459Szrj // offset.
279*a9fa9459Szrj void
280*a9fa9459Szrj write(Output_file*, off_t offset);
281*a9fa9459Szrj
282*a9fa9459Szrj // Write the string table into the specified buffer, of the
283*a9fa9459Szrj // specified size. buffer_size should be at least
284*a9fa9459Szrj // get_strtab_size().
285*a9fa9459Szrj void
286*a9fa9459Szrj write_to_buffer(unsigned char* buffer, section_size_type buffer_size);
287*a9fa9459Szrj
288*a9fa9459Szrj // Dump statistical information to stderr.
289*a9fa9459Szrj void
290*a9fa9459Szrj print_stats(const char*) const;
291*a9fa9459Szrj
292*a9fa9459Szrj private:
293*a9fa9459Szrj Stringpool_template(const Stringpool_template&);
294*a9fa9459Szrj Stringpool_template& operator=(const Stringpool_template&);
295*a9fa9459Szrj
296*a9fa9459Szrj // Return whether two strings are equal.
297*a9fa9459Szrj static bool
298*a9fa9459Szrj string_equal(const Stringpool_char*, const Stringpool_char*);
299*a9fa9459Szrj
300*a9fa9459Szrj // Compute a hash code for a string. LENGTH is the length of the
301*a9fa9459Szrj // string in characters.
302*a9fa9459Szrj static size_t
303*a9fa9459Szrj string_hash(const Stringpool_char*, size_t length);
304*a9fa9459Szrj
305*a9fa9459Szrj // We store the actual data in a list of these buffers.
306*a9fa9459Szrj struct Stringdata
307*a9fa9459Szrj {
308*a9fa9459Szrj // Length of data in buffer.
309*a9fa9459Szrj size_t len;
310*a9fa9459Szrj // Allocated size of buffer.
311*a9fa9459Szrj size_t alc;
312*a9fa9459Szrj // Buffer.
313*a9fa9459Szrj char data[1];
314*a9fa9459Szrj };
315*a9fa9459Szrj
316*a9fa9459Szrj // Add a new key offset entry.
317*a9fa9459Szrj void
318*a9fa9459Szrj new_key_offset(size_t);
319*a9fa9459Szrj
320*a9fa9459Szrj // Copy a string into the buffers, returning a canonical string.
321*a9fa9459Szrj const Stringpool_char*
322*a9fa9459Szrj add_string(const Stringpool_char*, size_t);
323*a9fa9459Szrj
324*a9fa9459Szrj // Return whether s1 is a suffix of s2.
325*a9fa9459Szrj static bool
326*a9fa9459Szrj is_suffix(const Stringpool_char* s1, size_t len1,
327*a9fa9459Szrj const Stringpool_char* s2, size_t len2);
328*a9fa9459Szrj
329*a9fa9459Szrj // The hash table key includes the string, the length of the string,
330*a9fa9459Szrj // and the hash code for the string. We put the hash code
331*a9fa9459Szrj // explicitly into the key so that we can do a find()/insert()
332*a9fa9459Szrj // sequence without having to recompute the hash. Computing the
333*a9fa9459Szrj // hash code is a significant user of CPU time in the linker.
334*a9fa9459Szrj struct Hashkey
335*a9fa9459Szrj {
336*a9fa9459Szrj const Stringpool_char* string;
337*a9fa9459Szrj // Length is in characters, not bytes.
338*a9fa9459Szrj size_t length;
339*a9fa9459Szrj size_t hash_code;
340*a9fa9459Szrj
341*a9fa9459Szrj // This goes in an STL container, so we need a default
342*a9fa9459Szrj // constructor.
HashkeyHashkey343*a9fa9459Szrj Hashkey()
344*a9fa9459Szrj : string(NULL), length(0), hash_code(0)
345*a9fa9459Szrj { }
346*a9fa9459Szrj
347*a9fa9459Szrj // Note that these constructors are relatively expensive, because
348*a9fa9459Szrj // they compute the hash code.
HashkeyHashkey349*a9fa9459Szrj explicit Hashkey(const Stringpool_char* s)
350*a9fa9459Szrj : string(s), length(string_length(s)), hash_code(string_hash(s, length))
351*a9fa9459Szrj { }
352*a9fa9459Szrj
HashkeyHashkey353*a9fa9459Szrj Hashkey(const Stringpool_char* s, size_t len)
354*a9fa9459Szrj : string(s), length(len), hash_code(string_hash(s, len))
355*a9fa9459Szrj { }
356*a9fa9459Szrj };
357*a9fa9459Szrj
358*a9fa9459Szrj // Hash function. This is trivial, since we have already computed
359*a9fa9459Szrj // the hash.
360*a9fa9459Szrj struct Stringpool_hash
361*a9fa9459Szrj {
362*a9fa9459Szrj size_t
operatorStringpool_hash363*a9fa9459Szrj operator()(const Hashkey& hk) const
364*a9fa9459Szrj { return hk.hash_code; }
365*a9fa9459Szrj };
366*a9fa9459Szrj
367*a9fa9459Szrj // Equality comparison function for hash table.
368*a9fa9459Szrj struct Stringpool_eq
369*a9fa9459Szrj {
370*a9fa9459Szrj bool
371*a9fa9459Szrj operator()(const Hashkey&, const Hashkey&) const;
372*a9fa9459Szrj };
373*a9fa9459Szrj
374*a9fa9459Szrj // The hash table is a map from strings to Keys.
375*a9fa9459Szrj
376*a9fa9459Szrj typedef Key Hashval;
377*a9fa9459Szrj
378*a9fa9459Szrj typedef Unordered_map<Hashkey, Hashval, Stringpool_hash,
379*a9fa9459Szrj Stringpool_eq> String_set_type;
380*a9fa9459Szrj
381*a9fa9459Szrj // Comparison routine used when sorting into a string table.
382*a9fa9459Szrj
383*a9fa9459Szrj typedef typename String_set_type::iterator Stringpool_sort_info;
384*a9fa9459Szrj
385*a9fa9459Szrj struct Stringpool_sort_comparison
386*a9fa9459Szrj {
387*a9fa9459Szrj bool
388*a9fa9459Szrj operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const;
389*a9fa9459Szrj };
390*a9fa9459Szrj
391*a9fa9459Szrj // Keys map to offsets via a Chunked_vector. We only use the
392*a9fa9459Szrj // offsets if we turn this into an string table section.
393*a9fa9459Szrj typedef Chunked_vector<section_offset_type> Key_to_offset;
394*a9fa9459Szrj
395*a9fa9459Szrj // List of Stringdata structures.
396*a9fa9459Szrj typedef std::list<Stringdata*> Stringdata_list;
397*a9fa9459Szrj
398*a9fa9459Szrj // Mapping from const char* to namepool entry.
399*a9fa9459Szrj String_set_type string_set_;
400*a9fa9459Szrj // Mapping from Key to string table offset.
401*a9fa9459Szrj Key_to_offset key_to_offset_;
402*a9fa9459Szrj // List of buffers.
403*a9fa9459Szrj Stringdata_list strings_;
404*a9fa9459Szrj // Size of string table.
405*a9fa9459Szrj section_size_type strtab_size_;
406*a9fa9459Szrj // Whether to reserve offset 0 to hold the null string.
407*a9fa9459Szrj bool zero_null_;
408*a9fa9459Szrj // Whether to optimize the string table.
409*a9fa9459Szrj bool optimize_;
410*a9fa9459Szrj // offset of the next string.
411*a9fa9459Szrj section_offset_type offset_;
412*a9fa9459Szrj // The alignment of strings in the stringpool.
413*a9fa9459Szrj uint64_t addralign_;
414*a9fa9459Szrj };
415*a9fa9459Szrj
416*a9fa9459Szrj // The most common type of Stringpool.
417*a9fa9459Szrj typedef Stringpool_template<char> Stringpool;
418*a9fa9459Szrj
419*a9fa9459Szrj } // End namespace gold.
420*a9fa9459Szrj
421*a9fa9459Szrj #endif // !defined(GOLD_STRINGPOOL_H)
422