Commit | Line | Data |
---|---|---|
b8e6aad9 ILT |
1 | // merge.h -- handle section merging for gold -*- C++ -*- |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. | |
22 | ||
b8e6aad9 ILT |
23 | #ifndef GOLD_MERGE_H |
24 | #define GOLD_MERGE_H | |
25 | ||
26 | #include <climits> | |
27 | ||
28 | #include "stringpool.h" | |
29 | #include "output.h" | |
30 | ||
31 | namespace gold | |
32 | { | |
33 | ||
34 | // A general class for SHF_MERGE data, to hold functions shared by | |
35 | // fixed-size constant data and string data. | |
36 | ||
37 | class Output_merge_base : public Output_section_data | |
38 | { | |
39 | public: | |
87f95776 ILT |
40 | Output_merge_base(uint64_t entsize, uint64_t addralign) |
41 | : Output_section_data(addralign), merge_map_(), entsize_(entsize) | |
b8e6aad9 ILT |
42 | { } |
43 | ||
44 | // Return the output address for an input address. | |
45 | bool | |
46 | do_output_address(const Relobj* object, unsigned int shndx, off_t offset, | |
47 | uint64_t output_section_address, uint64_t* poutput) const; | |
48 | ||
49 | protected: | |
50 | // Return the entry size. | |
51 | uint64_t | |
52 | entsize() const | |
53 | { return this->entsize_; } | |
54 | ||
55 | // Add a mapping from an OFFSET in input section SHNDX in object | |
56 | // OBJECT to an OUTPUT_OFFSET in the output section. | |
57 | void | |
58 | add_mapping(Relobj* object, unsigned int shndx, off_t offset, | |
59 | off_t output_offset); | |
60 | ||
61 | private: | |
62 | // We build a mapping from OBJECT/SHNDX/OFFSET to an offset in the | |
63 | // output section. | |
64 | struct Merge_key | |
65 | { | |
66 | const Relobj* object; | |
67 | unsigned int shndx; | |
68 | off_t offset; | |
69 | }; | |
70 | ||
71 | struct Merge_key_less | |
72 | { | |
73 | bool | |
74 | operator()(const Merge_key&, const Merge_key&) const; | |
75 | }; | |
76 | ||
77 | typedef std::map<Merge_key, off_t, Merge_key_less> Merge_map; | |
78 | ||
79 | // A mapping from input object/section/offset to offset in output | |
80 | // section. | |
81 | Merge_map merge_map_; | |
82 | ||
83 | // The entry size. For fixed-size constants, this is the size of | |
84 | // the constants. For strings, this is the size of a character. | |
85 | uint64_t entsize_; | |
86 | }; | |
87 | ||
88 | // Handle SHF_MERGE sections with fixed-size constant data. | |
89 | ||
90 | class Output_merge_data : public Output_merge_base | |
91 | { | |
92 | public: | |
87f95776 ILT |
93 | Output_merge_data(uint64_t entsize, uint64_t addralign) |
94 | : Output_merge_base(entsize, addralign), p_(NULL), len_(0), alc_(0), | |
b8e6aad9 ILT |
95 | hashtable_(128, Merge_data_hash(this), Merge_data_eq(this)) |
96 | { } | |
97 | ||
98 | // Add an input section. | |
99 | bool | |
100 | do_add_input_section(Relobj* object, unsigned int shndx); | |
101 | ||
102 | // Set the final data size. | |
103 | void | |
104 | do_set_address(uint64_t, off_t); | |
105 | ||
106 | // Write the data to the file. | |
107 | void | |
108 | do_write(Output_file*); | |
109 | ||
110 | private: | |
111 | // We build a hash table of the fixed-size constants. Each constant | |
112 | // is stored as a pointer into the section data we are accumulating. | |
113 | ||
114 | // A key in the hash table. This is an offset in the section | |
115 | // contents we are building. | |
116 | typedef off_t Merge_data_key; | |
117 | ||
118 | // Compute the hash code. To do this we need a pointer back to the | |
119 | // object holding the data. | |
120 | class Merge_data_hash | |
121 | { | |
122 | public: | |
123 | Merge_data_hash(const Output_merge_data* pomd) | |
124 | : pomd_(pomd) | |
125 | { } | |
126 | ||
127 | size_t | |
128 | operator()(Merge_data_key) const; | |
129 | ||
130 | private: | |
131 | const Output_merge_data* pomd_; | |
132 | }; | |
133 | ||
134 | friend class Merge_data_hash; | |
135 | ||
136 | // Compare two entries in the hash table for equality. To do this | |
137 | // we need a pointer back to the object holding the data. Note that | |
138 | // we now have a pointer to the object stored in two places in the | |
139 | // hash table. Fixing this would require specializing the hash | |
140 | // table, which would be hard to do portably. | |
141 | class Merge_data_eq | |
142 | { | |
143 | public: | |
144 | Merge_data_eq(const Output_merge_data* pomd) | |
145 | : pomd_(pomd) | |
146 | { } | |
147 | ||
148 | bool | |
149 | operator()(Merge_data_key k1, Merge_data_key k2) const; | |
150 | ||
151 | private: | |
152 | const Output_merge_data* pomd_; | |
153 | }; | |
154 | ||
155 | friend class Merge_data_eq; | |
156 | ||
157 | // The type of the hash table. | |
158 | typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq> | |
159 | Merge_data_hashtable; | |
160 | ||
161 | // Given a hash table key, which is just an offset into the section | |
162 | // data, return a pointer to the corresponding constant. | |
163 | const unsigned char* | |
164 | constant(Merge_data_key k) const | |
165 | { | |
166 | gold_assert(k >= 0 && k < this->len_); | |
167 | return this->p_ + k; | |
168 | } | |
169 | ||
170 | // Add a constant to the output. | |
171 | void | |
172 | add_constant(const unsigned char*); | |
173 | ||
174 | // The accumulated data. | |
175 | unsigned char* p_; | |
176 | // The length of the accumulated data. | |
177 | off_t len_; | |
178 | // The size of the allocated buffer. | |
179 | size_t alc_; | |
180 | // The hash table. | |
181 | Merge_data_hashtable hashtable_; | |
182 | }; | |
183 | ||
184 | // Handle SHF_MERGE sections with string data. This is a template | |
185 | // based on the type of the characters in the string. | |
186 | ||
187 | template<typename Char_type> | |
188 | class Output_merge_string : public Output_merge_base | |
189 | { | |
190 | public: | |
87f95776 ILT |
191 | Output_merge_string(uint64_t addralign) |
192 | : Output_merge_base(sizeof(Char_type), addralign), stringpool_(), | |
193 | merged_strings_() | |
194 | { | |
195 | gold_assert(addralign <= sizeof(Char_type)); | |
196 | this->stringpool_.set_no_zero_null(); | |
197 | } | |
b8e6aad9 ILT |
198 | |
199 | // Add an input section. | |
200 | bool | |
201 | do_add_input_section(Relobj* object, unsigned int shndx); | |
202 | ||
203 | // Set the final data size. | |
204 | void | |
205 | do_set_address(uint64_t, off_t); | |
206 | ||
207 | // Write the data to the file. | |
208 | void | |
209 | do_write(Output_file*); | |
210 | ||
211 | private: | |
212 | // As we see input sections, we build a mapping from object, section | |
213 | // index and offset to strings. | |
42e3fe0d | 214 | struct Merged_string |
b8e6aad9 | 215 | { |
42e3fe0d | 216 | // The input object where the string was found. |
b8e6aad9 | 217 | Relobj* object; |
42e3fe0d | 218 | // The input section in the input object. |
b8e6aad9 | 219 | unsigned int shndx; |
42e3fe0d | 220 | // The offset in the input section. |
b8e6aad9 | 221 | off_t offset; |
42e3fe0d ILT |
222 | // The string itself, a pointer into a Stringpool. |
223 | const Char_type* string; | |
b8e6aad9 | 224 | |
42e3fe0d ILT |
225 | Merged_string(Relobj *objecta, unsigned int shndxa, off_t offseta, |
226 | const Char_type* stringa) | |
227 | : object(objecta), shndx(shndxa), offset(offseta), string(stringa) | |
b8e6aad9 ILT |
228 | { } |
229 | }; | |
230 | ||
42e3fe0d | 231 | typedef std::vector<Merged_string> Merged_strings; |
b8e6aad9 ILT |
232 | |
233 | // As we see the strings, we add them to a Stringpool. | |
234 | Stringpool_template<Char_type> stringpool_; | |
235 | // Map from a location in an input object to an entry in the | |
236 | // Stringpool. | |
42e3fe0d | 237 | Merged_strings merged_strings_; |
b8e6aad9 ILT |
238 | }; |
239 | ||
240 | } // End namespace gold. | |
241 | ||
242 | #endif // !defined(GOLD_MERGE_H) |