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