Automatic date update in version.in
[deliverable/binutils-gdb.git] / gold / merge.h
CommitLineData
b8e6aad9
ILT
1// merge.h -- handle section merging for gold -*- C++ -*-
2
b90efa5b 3// Copyright (C) 2006-2015 Free Software Foundation, Inc.
6cb15b7f
ILT
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>
a9a60db6
ILT
27#include <map>
28#include <vector>
b8e6aad9
ILT
29
30#include "stringpool.h"
31#include "output.h"
32
33namespace gold
34{
35
a9a60db6
ILT
36// For each object with merge sections, we store an Object_merge_map.
37// This is used to map locations in input sections to a merged output
38// section. The output section itself is not recorded here--it can be
ef9beddf 39// found in the output_sections_ field of the Object.
a9a60db6
ILT
40
41class Object_merge_map
42{
43 public:
44 Object_merge_map()
45 : first_shnum_(-1U), first_map_(),
46 second_shnum_(-1U), second_map_(),
47 section_merge_maps_()
48 { }
49
50 ~Object_merge_map();
51
52 // Add a mapping for MERGE_MAP, for the bytes from OFFSET to OFFSET
53 // + LENGTH in the input section SHNDX to OUTPUT_OFFSET in the
54 // output section. An OUTPUT_OFFSET of -1 means that the bytes are
55 // discarded. OUTPUT_OFFSET is relative to the start of the merged
56 // data in the output section.
57 void
dbe40a88
RÁE
58 add_mapping(const Output_section_data*, unsigned int shndx,
59 section_offset_type offset, section_size_type length,
60 section_offset_type output_offset);
a9a60db6
ILT
61
62 // Get the output offset for an input address. MERGE_MAP is the map
63 // we are looking for, or NULL if we don't care. The input address
64 // is at offset OFFSET in section SHNDX. This sets *OUTPUT_OFFSET
65 // to the offset in the output section; this will be -1 if the bytes
66 // are not being copied to the output. This returns true if the
67 // mapping is known, false otherwise. *OUTPUT_OFFSET is relative to
68 // the start of the merged data in the output section.
69 bool
dbe40a88 70 get_output_offset(unsigned int shndx,
a9a60db6 71 section_offset_type offset,
ca09d69a 72 section_offset_type* output_offset);
a9a60db6 73
67f95b96
RÁE
74 const Output_section_data*
75 find_merge_section(unsigned int shndx) const;
a9a60db6
ILT
76
77 // Initialize an mapping from input offsets to output addresses for
78 // section SHNDX. STARTING_ADDRESS is the output address of the
79 // merged section.
80 template<int size>
81 void
82 initialize_input_to_output_map(
83 unsigned int shndx,
84 typename elfcpp::Elf_types<size>::Elf_Addr starting_address,
85 Unordered_map<section_offset_type,
86 typename elfcpp::Elf_types<size>::Elf_Addr>*);
87
a9a60db6
ILT
88 // Map input section offsets to a length and an output section
89 // offset. An output section offset of -1 means that this part of
90 // the input section is being discarded.
91 struct Input_merge_entry
92 {
93 // The offset in the input section.
94 section_offset_type input_offset;
95 // The length.
96 section_size_type length;
97 // The offset in the output section.
98 section_offset_type output_offset;
99 };
100
a9a60db6
ILT
101 // A list of entries for a particular input section.
102 struct Input_merge_map
103 {
0916f9e7
RÁE
104 void add_mapping(section_offset_type input_offset, section_size_type length,
105 section_offset_type output_offset);
106
a9a60db6
ILT
107 typedef std::vector<Input_merge_entry> Entries;
108
109 // We store these with the Relobj, and we look them up by input
110 // section. It is possible to have two different merge maps
111 // associated with a single output section. For example, this
112 // happens routinely with .rodata, when merged string constants
113 // and merged fixed size constants are both put into .rodata. The
114 // output offset that we store is not the offset from the start of
115 // the output section; it is the offset from the start of the
116 // merged data in the output section. That means that the caller
117 // is going to add the offset of the merged data within the output
118 // section, which means that the caller needs to know which set of
119 // merged data it found the entry in. So it's not enough to find
120 // this data based on the input section and the output section; we
121 // also have to find it based on a set of merged data in the
122 // output section. In order to verify that we are looking at the
123 // right data, we store a pointer to the Merge_map here, and we
124 // pass in a pointer when looking at the data. If we are asked to
125 // look up information for a different Merge_map, we report that
126 // we don't have it, rather than trying a lookup and returning an
127 // answer which will receive the wrong offset.
dbe40a88 128 const Output_section_data* output_data;
a9a60db6
ILT
129 // The list of mappings.
130 Entries entries;
131 // Whether the ENTRIES field is sorted by input_offset.
132 bool sorted;
133
134 Input_merge_map()
dbe40a88 135 : output_data(NULL), entries(), sorted(true)
a9a60db6
ILT
136 { }
137 };
138
0916f9e7
RÁE
139 // Get or make the Input_merge_map to use for the section SHNDX
140 // with MERGE_MAP.
141 Input_merge_map*
142 get_or_make_input_merge_map(const Output_section_data* merge_map,
143 unsigned int shndx);
144
145 private:
146 // A less-than comparison routine for Input_merge_entry.
147 struct Input_merge_compare
148 {
149 bool
150 operator()(const Input_merge_entry& i1, const Input_merge_entry& i2) const
151 { return i1.input_offset < i2.input_offset; }
152 };
153
a9a60db6
ILT
154 // Map input section indices to merge maps.
155 typedef std::map<unsigned int, Input_merge_map*> Section_merge_maps;
156
157 // Return a pointer to the Input_merge_map to use for the input
158 // section SHNDX, or NULL.
67f95b96
RÁE
159 const Input_merge_map*
160 get_input_merge_map(unsigned int shndx) const;
161
162 Input_merge_map *
163 get_input_merge_map(unsigned int shndx) {
164 return const_cast<Input_merge_map *>(static_cast<const Object_merge_map *>(
165 this)->get_input_merge_map(shndx));
166 }
a9a60db6 167
a9a60db6
ILT
168 // Any given object file will normally only have a couple of input
169 // sections with mergeable contents. So we keep the first two input
170 // section numbers inline, and push any further ones into a map. A
171 // value of -1U in first_shnum_ or second_shnum_ means that we don't
172 // have a corresponding entry.
173 unsigned int first_shnum_;
174 Input_merge_map first_map_;
175 unsigned int second_shnum_;
176 Input_merge_map second_map_;
177 Section_merge_maps section_merge_maps_;
178};
179
b8e6aad9
ILT
180// A general class for SHF_MERGE data, to hold functions shared by
181// fixed-size constant data and string data.
182
183class Output_merge_base : public Output_section_data
184{
185 public:
2ea97941 186 Output_merge_base(uint64_t entsize, uint64_t addralign)
dbe40a88 187 : Output_section_data(addralign), entsize_(entsize),
0439c796
DK
188 keeps_input_sections_(false), first_relobj_(NULL), first_shndx_(-1),
189 input_sections_()
b8e6aad9
ILT
190 { }
191
c0a62865
DK
192 // Return the entry size.
193 uint64_t
194 entsize() const
195 { return this->entsize_; }
196
197 // Whether this is a merge string section. This is only true of
198 // Output_merge_string.
199 bool
200 is_string()
201 { return this->do_is_string(); }
202
0439c796
DK
203 // Whether this keeps input sections.
204 bool
205 keeps_input_sections() const
206 { return this->keeps_input_sections_; }
207
208 // Set the keeps-input-sections flag. This is virtual so that sub-classes
209 // can perform additional checks.
210 void
211 set_keeps_input_sections()
212 { this->do_set_keeps_input_sections(); }
213
214 // Return the object of the first merged input section. This used
215 // for script processing. This is NULL if merge section is empty.
216 Relobj*
217 first_relobj() const
218 { return this->first_relobj_; }
219
220 // Return the section index of the first merged input section. This
221 // is used for script processing. This is valid only if merge section
222 // is not valid.
223 unsigned int
224 first_shndx() const
225 {
226 gold_assert(this->first_relobj_ != NULL);
227 return this->first_shndx_;
228 }
229
230 // Set of merged input sections.
231 typedef Unordered_set<Section_id, Section_id_hash> Input_sections;
232
233 // Beginning of merged input sections.
234 Input_sections::const_iterator
235 input_sections_begin() const
236 {
237 gold_assert(this->keeps_input_sections_);
238 return this->input_sections_.begin();
239 }
240
241 // Beginning of merged input sections.
242 Input_sections::const_iterator
243 input_sections_end() const
244 {
245 gold_assert(this->keeps_input_sections_);
246 return this->input_sections_.end();
247 }
248
a9a60db6 249 protected:
730cdc88 250 // Return the output offset for an input offset.
b8e6aad9 251 bool
8383303e
ILT
252 do_output_offset(const Relobj* object, unsigned int shndx,
253 section_offset_type offset,
254 section_offset_type* poutput) const;
b8e6aad9 255
9b547ce6 256 // This may be overridden by the child class.
c0a62865
DK
257 virtual bool
258 do_is_string()
259 { return false; }
260
0439c796
DK
261 // This may be overridden by the child class.
262 virtual void
263 do_set_keeps_input_sections()
264 { this->keeps_input_sections_ = true; }
265
266 // Record the merged input section for script processing.
267 void
268 record_input_section(Relobj* relobj, unsigned int shndx);
269
730cdc88 270 private:
b8e6aad9
ILT
271 // The entry size. For fixed-size constants, this is the size of
272 // the constants. For strings, this is the size of a character.
273 uint64_t entsize_;
0439c796
DK
274 // Whether we keep input sections.
275 bool keeps_input_sections_;
276 // Object of the first merged input section. We use this for script
277 // processing.
278 Relobj* first_relobj_;
279 // Section index of the first merged input section.
280 unsigned int first_shndx_;
281 // Input sections. We only keep them is keeps_input_sections_ is true.
282 Input_sections input_sections_;
b8e6aad9
ILT
283};
284
285// Handle SHF_MERGE sections with fixed-size constant data.
286
287class Output_merge_data : public Output_merge_base
288{
289 public:
2ea97941
ILT
290 Output_merge_data(uint64_t entsize, uint64_t addralign)
291 : Output_merge_base(entsize, addralign), p_(NULL), len_(0), alc_(0),
38c5e8b4 292 input_count_(0),
b8e6aad9
ILT
293 hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
294 { }
295
38c5e8b4 296 protected:
b8e6aad9
ILT
297 // Add an input section.
298 bool
299 do_add_input_section(Relobj* object, unsigned int shndx);
300
301 // Set the final data size.
302 void
27bc2bce 303 set_final_data_size();
b8e6aad9
ILT
304
305 // Write the data to the file.
306 void
307 do_write(Output_file*);
308
96803768
ILT
309 // Write the data to a buffer.
310 void
311 do_write_to_buffer(unsigned char*);
312
7d9e3d98
ILT
313 // Write to a map file.
314 void
315 do_print_to_mapfile(Mapfile* mapfile) const
316 { mapfile->print_output_data(this, _("** merge constants")); }
317
38c5e8b4
ILT
318 // Print merge stats to stderr.
319 void
320 do_print_merge_stats(const char* section_name);
321
0439c796
DK
322 // Set keeps-input-sections flag.
323 void
324 do_set_keeps_input_sections()
325 {
326 gold_assert(this->input_count_ == 0);
327 Output_merge_base::do_set_keeps_input_sections();
328 }
329
b8e6aad9
ILT
330 private:
331 // We build a hash table of the fixed-size constants. Each constant
332 // is stored as a pointer into the section data we are accumulating.
333
334 // A key in the hash table. This is an offset in the section
335 // contents we are building.
8383303e 336 typedef section_offset_type Merge_data_key;
b8e6aad9
ILT
337
338 // Compute the hash code. To do this we need a pointer back to the
339 // object holding the data.
340 class Merge_data_hash
341 {
342 public:
343 Merge_data_hash(const Output_merge_data* pomd)
344 : pomd_(pomd)
345 { }
346
347 size_t
348 operator()(Merge_data_key) const;
349
350 private:
351 const Output_merge_data* pomd_;
352 };
353
354 friend class Merge_data_hash;
355
356 // Compare two entries in the hash table for equality. To do this
357 // we need a pointer back to the object holding the data. Note that
358 // we now have a pointer to the object stored in two places in the
359 // hash table. Fixing this would require specializing the hash
360 // table, which would be hard to do portably.
361 class Merge_data_eq
362 {
363 public:
364 Merge_data_eq(const Output_merge_data* pomd)
365 : pomd_(pomd)
366 { }
367
368 bool
369 operator()(Merge_data_key k1, Merge_data_key k2) const;
370
371 private:
372 const Output_merge_data* pomd_;
373 };
374
375 friend class Merge_data_eq;
376
377 // The type of the hash table.
378 typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
379 Merge_data_hashtable;
380
381 // Given a hash table key, which is just an offset into the section
382 // data, return a pointer to the corresponding constant.
383 const unsigned char*
384 constant(Merge_data_key k) const
385 {
8383303e 386 gold_assert(k >= 0 && k < static_cast<section_offset_type>(this->len_));
b8e6aad9
ILT
387 return this->p_ + k;
388 }
389
390 // Add a constant to the output.
391 void
392 add_constant(const unsigned char*);
393
394 // The accumulated data.
395 unsigned char* p_;
396 // The length of the accumulated data.
8383303e 397 section_size_type len_;
b8e6aad9 398 // The size of the allocated buffer.
8383303e 399 section_size_type alc_;
38c5e8b4
ILT
400 // The number of entries seen in input files.
401 size_t input_count_;
b8e6aad9
ILT
402 // The hash table.
403 Merge_data_hashtable hashtable_;
404};
405
406// Handle SHF_MERGE sections with string data. This is a template
407// based on the type of the characters in the string.
408
409template<typename Char_type>
410class Output_merge_string : public Output_merge_base
411{
412 public:
2ea97941 413 Output_merge_string(uint64_t addralign)
e31908b6 414 : Output_merge_base(sizeof(Char_type), addralign), stringpool_(addralign),
fef830db 415 merged_strings_lists_(), input_count_(0), input_size_(0)
87f95776 416 {
87f95776
ILT
417 this->stringpool_.set_no_zero_null();
418 }
b8e6aad9 419
9a0910c3 420 protected:
b8e6aad9
ILT
421 // Add an input section.
422 bool
423 do_add_input_section(Relobj* object, unsigned int shndx);
424
9a0910c3
ILT
425 // Do all the final processing after the input sections are read in.
426 // Returns the final data size.
8383303e 427 section_size_type
9a0910c3
ILT
428 finalize_merged_data();
429
b8e6aad9
ILT
430 // Set the final data size.
431 void
27bc2bce 432 set_final_data_size();
b8e6aad9
ILT
433
434 // Write the data to the file.
435 void
436 do_write(Output_file*);
437
96803768
ILT
438 // Write the data to a buffer.
439 void
440 do_write_to_buffer(unsigned char*);
441
7d9e3d98
ILT
442 // Write to a map file.
443 void
444 do_print_to_mapfile(Mapfile* mapfile) const
445 { mapfile->print_output_data(this, _("** merge strings")); }
446
38c5e8b4
ILT
447 // Print merge stats to stderr.
448 void
449 do_print_merge_stats(const char* section_name);
450
9a0910c3
ILT
451 // Writes the stringpool to a buffer.
452 void
8383303e 453 stringpool_to_buffer(unsigned char* buffer, section_size_type buffer_size)
9a0910c3
ILT
454 { this->stringpool_.write_to_buffer(buffer, buffer_size); }
455
456 // Clears all the data in the stringpool, to save on memory.
457 void
458 clear_stringpool()
bc2c67ff 459 { this->stringpool_.clear(); }
9a0910c3 460
c0a62865
DK
461 // Whether this is a merge string section.
462 virtual bool
463 do_is_string()
464 { return true; }
465
0439c796
DK
466 // Set keeps-input-sections flag.
467 void
468 do_set_keeps_input_sections()
469 {
470 gold_assert(this->input_count_ == 0);
471 Output_merge_base::do_set_keeps_input_sections();
472 }
473
b8e6aad9 474 private:
38c5e8b4
ILT
475 // The name of the string type, for stats.
476 const char*
477 string_name();
478
b8e6aad9
ILT
479 // As we see input sections, we build a mapping from object, section
480 // index and offset to strings.
42e3fe0d 481 struct Merged_string
b8e6aad9 482 {
42e3fe0d 483 // The offset in the input section.
8383303e 484 section_offset_type offset;
2030fba0
ILT
485 // The key in the Stringpool.
486 Stringpool::Key stringpool_key;
b8e6aad9 487
76897331
CC
488 Merged_string(section_offset_type offseta, Stringpool::Key stringpool_keya)
489 : offset(offseta), stringpool_key(stringpool_keya)
b8e6aad9
ILT
490 { }
491 };
492
42e3fe0d 493 typedef std::vector<Merged_string> Merged_strings;
b8e6aad9 494
76897331
CC
495 struct Merged_strings_list
496 {
497 // The input object where the strings were found.
498 Relobj* object;
499 // The input section in the input object.
500 unsigned int shndx;
501 // The list of merged strings.
502 Merged_strings merged_strings;
503
504 Merged_strings_list(Relobj* objecta, unsigned int shndxa)
505 : object(objecta), shndx(shndxa), merged_strings()
506 { }
507 };
508
509 typedef std::vector<Merged_strings_list*> Merged_strings_lists;
510
b8e6aad9
ILT
511 // As we see the strings, we add them to a Stringpool.
512 Stringpool_template<Char_type> stringpool_;
513 // Map from a location in an input object to an entry in the
514 // Stringpool.
76897331 515 Merged_strings_lists merged_strings_lists_;
38c5e8b4
ILT
516 // The number of entries seen in input files.
517 size_t input_count_;
fef830db
CC
518 // The total size of input sections.
519 size_t input_size_;
b8e6aad9
ILT
520};
521
522} // End namespace gold.
523
524#endif // !defined(GOLD_MERGE_H)
This page took 0.373477 seconds and 4 git commands to generate.