* elfxx-mips.c (_bfd_mips_elf_finish_dynamic_sections): Make sure .got
[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
6// Stringpool
7// Manage a pool of unique strings.
8
9#ifndef GOLD_STRINGPOOL_H
10#define GOLD_STRINGPOOL_H
11
12namespace gold
13{
14
61ba1cf9
ILT
15class Output_file;
16
b8e6aad9
ILT
17template<typename Stringpool_char>
18class Stringpool_template
14bfc3f5
ILT
19{
20 public:
f0641a0b
ILT
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.
26 typedef size_t Key;
27
b8e6aad9
ILT
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);
14bfc3f5 31
b8e6aad9 32 ~Stringpool_template();
14bfc3f5
ILT
33
34 // Add a string to the pool. This returns a canonical permanent
f0641a0b
ILT
35 // pointer to the string. If PKEY is not NULL, this sets *PKEY to
36 // the key for the string.
b8e6aad9
ILT
37 const Stringpool_char*
38 add(const Stringpool_char*, Key* pkey);
14bfc3f5 39
b8e6aad9
ILT
40 const Stringpool_char*
41 add(const std::basic_string<Stringpool_char>& s, Key* pkey)
f0641a0b 42 { return this->add(s.c_str(), pkey); }
14bfc3f5
ILT
43
44 // Add the prefix of a string to the pool.
b8e6aad9
ILT
45 const Stringpool_char*
46 add(const Stringpool_char*, size_t, Key* pkey);
61ba1cf9
ILT
47
48 // If a string is present, return the canonical string. Otherwise,
f0641a0b 49 // return NULL. If PKEY is not NULL, set *PKEY to the key.
b8e6aad9
ILT
50 const Stringpool_char*
51 find(const Stringpool_char*, Key* pkey) const;
61ba1cf9
ILT
52
53 // Turn the stringpool into an ELF strtab: determine the offsets of
54 // all the strings.
55 void
56 set_string_offsets();
57
b8e6aad9
ILT
58 // Get the offset of a string in an ELF strtab. This returns the
59 // offset in bytes, not characters.
61ba1cf9 60 off_t
b8e6aad9 61 get_offset(const Stringpool_char*) const;
61ba1cf9
ILT
62
63 off_t
b8e6aad9 64 get_offset(const std::basic_string<Stringpool_char>& s) const
61ba1cf9
ILT
65 { return this->get_offset(s.c_str()); }
66
b8e6aad9
ILT
67 // Get the size of the ELF strtab. This returns the number of
68 // bytes, not characters.
61ba1cf9
ILT
69 off_t
70 get_strtab_size() const
a3ad94ed
ILT
71 {
72 gold_assert(this->strtab_size_ != 0);
73 return this->strtab_size_;
74 }
61ba1cf9
ILT
75
76 // Write the strtab into the output file at the specified offset.
77 void
78 write(Output_file*, off_t offset);
14bfc3f5
ILT
79
80 private:
b8e6aad9
ILT
81 Stringpool_template(const Stringpool_template&);
82 Stringpool_template& operator=(const Stringpool_template&);
83
84 // Return the length of a string.
85 static size_t
86 string_length(const Stringpool_char*);
14bfc3f5 87
61ba1cf9
ILT
88 // We store the actual data in a list of these buffers.
89 struct Stringdata
14bfc3f5
ILT
90 {
91 // Length of data in buffer.
92 size_t len;
93 // Allocated size of buffer.
94 size_t alc;
f0641a0b
ILT
95 // Buffer index.
96 unsigned int index;
14bfc3f5
ILT
97 // Buffer.
98 char data[1];
99 };
100
61ba1cf9 101 // Copy a string into the buffers, returning a canonical string.
b8e6aad9
ILT
102 const Stringpool_char*
103 add_string(const Stringpool_char*, Key*);
14bfc3f5
ILT
104
105 struct Stringpool_hash
106 {
107 size_t
b8e6aad9 108 operator()(const Stringpool_char*) const;
14bfc3f5
ILT
109 };
110
111 struct Stringpool_eq
112 {
113 bool
b8e6aad9 114 operator()(const Stringpool_char* p1, const Stringpool_char* p2) const;
14bfc3f5
ILT
115 };
116
61ba1cf9 117 // Return whether s1 is a suffix of s2.
b8e6aad9 118 static bool
614f30a2
ILT
119 is_suffix(const Stringpool_char* s1, size_t len1,
120 const Stringpool_char* s2, size_t len2);
61ba1cf9 121
f0641a0b
ILT
122 // The hash table is a map from string names to a pair of Key and
123 // ELF strtab offsets. We only use the offsets if we turn this into
124 // an ELF strtab section.
125
126 typedef std::pair<Key, off_t> Val;
61ba1cf9 127
274e99f9 128#ifdef HAVE_TR1_UNORDERED_SET
b8e6aad9 129 typedef Unordered_map<const Stringpool_char*, Val, Stringpool_hash,
61ba1cf9 130 Stringpool_eq,
b8e6aad9
ILT
131 std::allocator<std::pair<const Stringpool_char* const,
132 Val> >,
14bfc3f5 133 true> String_set_type;
274e99f9 134#else
b8e6aad9 135 typedef Unordered_map<const Stringpool_char*, Val, Stringpool_hash,
61ba1cf9 136 Stringpool_eq> String_set_type;
274e99f9
ILT
137#endif
138
614f30a2
ILT
139 // Comparison routine used when sorting into an ELF strtab. We
140 // store string-sizes in the sort-vector so we don't have to
141 // recompute them log(n) times as we sort.
142 struct Stringpool_sort_info
143 {
144 typename String_set_type::iterator it;
145 size_t string_length;
146 Stringpool_sort_info(typename String_set_type::iterator i, size_t s)
147 : it(i), string_length(s)
148 { }
149 };
61ba1cf9
ILT
150
151 struct Stringpool_sort_comparison
152 {
153 bool
614f30a2 154 operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const;
61ba1cf9
ILT
155 };
156
f0641a0b
ILT
157 // List of Stringdata structures.
158 typedef std::list<Stringdata*> Stringdata_list;
159
160 // Mapping from const char* to namepool entry.
14bfc3f5 161 String_set_type string_set_;
f0641a0b
ILT
162 // List of buffers.
163 Stringdata_list strings_;
164 // Size of ELF strtab.
61ba1cf9 165 off_t strtab_size_;
f0641a0b
ILT
166 // Next Stringdata index.
167 unsigned int next_index_;
b8e6aad9
ILT
168 // Whether to reserve offset 0 to hold the null string.
169 bool zero_null_;
14bfc3f5
ILT
170};
171
b8e6aad9
ILT
172// The most common type of Stringpool.
173typedef Stringpool_template<char> Stringpool;
174
14bfc3f5
ILT
175} // End namespace gold.
176
177#endif // !defined(GOLD_STRINGPOOL_H)
This page took 0.06342 seconds and 4 git commands to generate.