1 // stringpool.h -- a string pool for gold -*- C++ -*-
7 // Manage a pool of unique strings.
9 #ifndef GOLD_STRINGPOOL_H
10 #define GOLD_STRINGPOOL_H
17 template<typename Stringpool_char
>
18 class Stringpool_template
21 // The type of a key into the stringpool. A key value will always
22 // be the same during any run of the linker. The string pointers
23 // may change when using address space randomization. We use key
24 // values in order to get repeatable runs when the value is inserted
25 // into an unordered hash table. Zero is never a valid key.
28 // Create a Stringpool. ZERO_NULL is true if we should reserve
29 // offset 0 to hold the empty string.
30 Stringpool_template(bool zero_null
= true);
32 ~Stringpool_template();
34 // Add a string to the pool. This returns a canonical permanent
35 // pointer to the string. If PKEY is not NULL, this sets *PKEY to
36 // the key for the string.
37 const Stringpool_char
*
38 add(const Stringpool_char
*, Key
* pkey
);
40 const Stringpool_char
*
41 add(const std::basic_string
<Stringpool_char
>& s
, Key
* pkey
)
42 { return this->add(s
.c_str(), pkey
); }
44 // Add the prefix of a string to the pool.
45 const Stringpool_char
*
46 add(const Stringpool_char
*, size_t, Key
* pkey
);
48 // If a string is present, return the canonical string. Otherwise,
49 // return NULL. If PKEY is not NULL, set *PKEY to the key.
50 const Stringpool_char
*
51 find(const Stringpool_char
*, Key
* pkey
) const;
53 // Turn the stringpool into an ELF strtab: determine the offsets of
58 // Get the offset of a string in an ELF strtab. This returns the
59 // offset in bytes, not characters.
61 get_offset(const Stringpool_char
*) const;
64 get_offset(const std::basic_string
<Stringpool_char
>& s
) const
65 { return this->get_offset(s
.c_str()); }
67 // Get the size of the ELF strtab. This returns the number of
68 // bytes, not characters.
70 get_strtab_size() const
72 gold_assert(this->strtab_size_
!= 0);
73 return this->strtab_size_
;
76 // Write the strtab into the output file at the specified offset.
78 write(Output_file
*, off_t offset
);
81 Stringpool_template(const Stringpool_template
&);
82 Stringpool_template
& operator=(const Stringpool_template
&);
84 // Return the length of a string.
86 string_length(const Stringpool_char
*);
88 // We store the actual data in a list of these buffers.
91 // Length of data in buffer.
93 // Allocated size of buffer.
101 // Copy a string into the buffers, returning a canonical string.
102 const Stringpool_char
*
103 add_string(const Stringpool_char
*, Key
*);
105 struct Stringpool_hash
108 operator()(const Stringpool_char
*) const;
114 operator()(const Stringpool_char
* p1
, const Stringpool_char
* p2
) const;
117 // Return whether s1 is a suffix of s2.
119 is_suffix(const Stringpool_char
* s1
, const Stringpool_char
* s2
);
121 // The hash table is a map from string names to a pair of Key and
122 // ELF strtab offsets. We only use the offsets if we turn this into
123 // an ELF strtab section.
125 typedef std::pair
<Key
, off_t
> Val
;
127 #ifdef HAVE_TR1_UNORDERED_SET
128 typedef Unordered_map
<const Stringpool_char
*, Val
, Stringpool_hash
,
130 std::allocator
<std::pair
<const Stringpool_char
* const,
132 true> String_set_type
;
134 typedef Unordered_map
<const Stringpool_char
*, Val
, Stringpool_hash
,
135 Stringpool_eq
> String_set_type
;
138 // Comparison routine used when sorting into an ELF strtab.
140 struct Stringpool_sort_comparison
143 operator()(typename
String_set_type::iterator
,
144 typename
String_set_type::iterator
) const;
147 // List of Stringdata structures.
148 typedef std::list
<Stringdata
*> Stringdata_list
;
150 // Mapping from const char* to namepool entry.
151 String_set_type string_set_
;
153 Stringdata_list strings_
;
154 // Size of ELF strtab.
156 // Next Stringdata index.
157 unsigned int next_index_
;
158 // Whether to reserve offset 0 to hold the null string.
162 // The most common type of Stringpool.
163 typedef Stringpool_template
<char> Stringpool
;
165 } // End namespace gold.
167 #endif // !defined(GOLD_STRINGPOOL_H)