Update comments about output offsets and merged input sections.
[deliverable/binutils-gdb.git] / gold / merge.h
1 // merge.h -- handle section merging for gold -*- C++ -*-
2
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
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 // This class manages mappings from input sections to offsets in an
35 // output section. This is used where input sections are merged. The
36 // actual data is stored in fields in Object.
37
38 class Merge_map
39 {
40 public:
41 Merge_map()
42 { }
43
44 // Add a mapping for the bytes from OFFSET to OFFSET + LENGTH in the
45 // input section SHNDX in object OBJECT to OUTPUT_OFFSET in the
46 // output section. An OUTPUT_OFFSET of -1 means that the bytes are
47 // discarded. OUTPUT_OFFSET is not the offset from the start of the
48 // output section, it is the offset from the start of the merged
49 // data within the output section.
50 void
51 add_mapping(Relobj* object, unsigned int shndx,
52 section_offset_type offset, section_size_type length,
53 section_offset_type output_offset);
54
55 // Return the output offset for an input address. The input address
56 // is at offset OFFSET in section SHNDX in OBJECT. This sets
57 // *OUTPUT_OFFSET to the offset in the output section; this will be
58 // -1 if the bytes are not being copied to the output. This returns
59 // true if the mapping is known, false otherwise. This returns the
60 // value stored by add_mapping, namely the offset from the start of
61 // the merged data within the output section.
62 bool
63 get_output_offset(const Relobj* object, unsigned int shndx,
64 section_offset_type offset,
65 section_offset_type *output_offset) const;
66 };
67
68 // A general class for SHF_MERGE data, to hold functions shared by
69 // fixed-size constant data and string data.
70
71 class Output_merge_base : public Output_section_data
72 {
73 public:
74 Output_merge_base(uint64_t entsize, uint64_t addralign)
75 : Output_section_data(addralign), merge_map_(), entsize_(entsize)
76 { }
77
78 // Return the output offset for an input offset.
79 bool
80 do_output_offset(const Relobj* object, unsigned int shndx,
81 section_offset_type offset,
82 section_offset_type* poutput) const;
83
84 protected:
85 // Return the entry size.
86 uint64_t
87 entsize() const
88 { return this->entsize_; }
89
90 // Add a mapping from an OFFSET in input section SHNDX in object
91 // OBJECT to an OUTPUT_OFFSET in the output section. OUTPUT_OFFSET
92 // is the offset from the start of the merged data in the output
93 // section.
94 void
95 add_mapping(Relobj* object, unsigned int shndx, section_offset_type offset,
96 section_size_type length, section_offset_type output_offset)
97 {
98 this->merge_map_.add_mapping(object, shndx, offset, length, output_offset);
99 }
100
101 private:
102 // A mapping from input object/section/offset to offset in output
103 // section.
104 Merge_map merge_map_;
105 // The entry size. For fixed-size constants, this is the size of
106 // the constants. For strings, this is the size of a character.
107 uint64_t entsize_;
108 };
109
110 // Handle SHF_MERGE sections with fixed-size constant data.
111
112 class Output_merge_data : public Output_merge_base
113 {
114 public:
115 Output_merge_data(uint64_t entsize, uint64_t addralign)
116 : Output_merge_base(entsize, addralign), p_(NULL), len_(0), alc_(0),
117 input_count_(0),
118 hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
119 { }
120
121 protected:
122 // Add an input section.
123 bool
124 do_add_input_section(Relobj* object, unsigned int shndx);
125
126 // Set the final data size.
127 void
128 set_final_data_size();
129
130 // Write the data to the file.
131 void
132 do_write(Output_file*);
133
134 // Write the data to a buffer.
135 void
136 do_write_to_buffer(unsigned char*);
137
138 // Print merge stats to stderr.
139 void
140 do_print_merge_stats(const char* section_name);
141
142 private:
143 // We build a hash table of the fixed-size constants. Each constant
144 // is stored as a pointer into the section data we are accumulating.
145
146 // A key in the hash table. This is an offset in the section
147 // contents we are building.
148 typedef section_offset_type Merge_data_key;
149
150 // Compute the hash code. To do this we need a pointer back to the
151 // object holding the data.
152 class Merge_data_hash
153 {
154 public:
155 Merge_data_hash(const Output_merge_data* pomd)
156 : pomd_(pomd)
157 { }
158
159 size_t
160 operator()(Merge_data_key) const;
161
162 private:
163 const Output_merge_data* pomd_;
164 };
165
166 friend class Merge_data_hash;
167
168 // Compare two entries in the hash table for equality. To do this
169 // we need a pointer back to the object holding the data. Note that
170 // we now have a pointer to the object stored in two places in the
171 // hash table. Fixing this would require specializing the hash
172 // table, which would be hard to do portably.
173 class Merge_data_eq
174 {
175 public:
176 Merge_data_eq(const Output_merge_data* pomd)
177 : pomd_(pomd)
178 { }
179
180 bool
181 operator()(Merge_data_key k1, Merge_data_key k2) const;
182
183 private:
184 const Output_merge_data* pomd_;
185 };
186
187 friend class Merge_data_eq;
188
189 // The type of the hash table.
190 typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
191 Merge_data_hashtable;
192
193 // Given a hash table key, which is just an offset into the section
194 // data, return a pointer to the corresponding constant.
195 const unsigned char*
196 constant(Merge_data_key k) const
197 {
198 gold_assert(k >= 0 && k < static_cast<section_offset_type>(this->len_));
199 return this->p_ + k;
200 }
201
202 // Add a constant to the output.
203 void
204 add_constant(const unsigned char*);
205
206 // The accumulated data.
207 unsigned char* p_;
208 // The length of the accumulated data.
209 section_size_type len_;
210 // The size of the allocated buffer.
211 section_size_type alc_;
212 // The number of entries seen in input files.
213 size_t input_count_;
214 // The hash table.
215 Merge_data_hashtable hashtable_;
216 };
217
218 // Handle SHF_MERGE sections with string data. This is a template
219 // based on the type of the characters in the string.
220
221 template<typename Char_type>
222 class Output_merge_string : public Output_merge_base
223 {
224 public:
225 Output_merge_string(uint64_t addralign)
226 : Output_merge_base(sizeof(Char_type), addralign), stringpool_(),
227 merged_strings_(), input_count_(0)
228 {
229 gold_assert(addralign <= sizeof(Char_type));
230 this->stringpool_.set_no_zero_null();
231 }
232
233 protected:
234 // Add an input section.
235 bool
236 do_add_input_section(Relobj* object, unsigned int shndx);
237
238 // Do all the final processing after the input sections are read in.
239 // Returns the final data size.
240 section_size_type
241 finalize_merged_data();
242
243 // Set the final data size.
244 void
245 set_final_data_size();
246
247 // Write the data to the file.
248 void
249 do_write(Output_file*);
250
251 // Write the data to a buffer.
252 void
253 do_write_to_buffer(unsigned char*);
254
255 // Print merge stats to stderr.
256 void
257 do_print_merge_stats(const char* section_name);
258
259 // Writes the stringpool to a buffer.
260 void
261 stringpool_to_buffer(unsigned char* buffer, section_size_type buffer_size)
262 { this->stringpool_.write_to_buffer(buffer, buffer_size); }
263
264 // Clears all the data in the stringpool, to save on memory.
265 void
266 clear_stringpool()
267 { this->stringpool_.clear(); }
268
269 private:
270 // The name of the string type, for stats.
271 const char*
272 string_name();
273
274 // As we see input sections, we build a mapping from object, section
275 // index and offset to strings.
276 struct Merged_string
277 {
278 // The input object where the string was found.
279 Relobj* object;
280 // The input section in the input object.
281 unsigned int shndx;
282 // The offset in the input section.
283 section_offset_type offset;
284 // The string itself, a pointer into a Stringpool.
285 const Char_type* string;
286 // The length of the string in bytes, including the null terminator.
287 size_t length;
288 // The key in the Stringpool.
289 Stringpool::Key stringpool_key;
290
291 Merged_string(Relobj *objecta, unsigned int shndxa,
292 section_offset_type offseta, const Char_type* stringa,
293 size_t lengtha, Stringpool::Key stringpool_keya)
294 : object(objecta), shndx(shndxa), offset(offseta), string(stringa),
295 length(lengtha), stringpool_key(stringpool_keya)
296 { }
297 };
298
299 typedef std::vector<Merged_string> Merged_strings;
300
301 // As we see the strings, we add them to a Stringpool.
302 Stringpool_template<Char_type> stringpool_;
303 // Map from a location in an input object to an entry in the
304 // Stringpool.
305 Merged_strings merged_strings_;
306 // The number of entries seen in input files.
307 size_t input_count_;
308 };
309
310 } // End namespace gold.
311
312 #endif // !defined(GOLD_MERGE_H)
This page took 0.037095 seconds and 5 git commands to generate.