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