*** empty log message ***
[deliverable/binutils-gdb.git] / gold / merge.cc
CommitLineData
b8e6aad9
ILT
1// merge.cc -- handle section merging for gold
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#include "gold.h"
24
25#include <cstdlib>
87f95776 26#include <algorithm>
b8e6aad9
ILT
27
28#include "merge.h"
29
30namespace gold
31{
32
4625f782
ILT
33// For each object with merge sections, we store an Object_merge_map.
34// This is used to map locations in input sections to a merged output
35// section. The output section itself is not recorded here--it can be
36// found in the map_to_output_ field of the Object.
730cdc88 37
4625f782
ILT
38class Object_merge_map
39{
40 public:
41 Object_merge_map()
42 : first_shnum_(-1U), first_map_(),
43 second_shnum_(-1U), second_map_(),
44 section_merge_maps_()
45 { }
46
47 ~Object_merge_map();
48
49 // Add a mapping for MERGE_MAP, for the bytes from OFFSET to OFFSET
50 // + LENGTH in the input section SHNDX to OUTPUT_OFFSET in the
51 // output section. An OUTPUT_OFFSET of -1 means that the bytes are
52 // discarded.
53 void
8383303e
ILT
54 add_mapping(const Merge_map*, unsigned int shndx, section_offset_type offset,
55 section_size_type length, section_offset_type output_offset);
4625f782
ILT
56
57 // Get the output offset for an input address in MERGE_MAP. The
58 // input address is at offset OFFSET in section SHNDX. This sets
59 // *OUTPUT_OFFSET to the offset in the output section; this will be
60 // -1 if the bytes are not being copied to the output. This returns
61 // true if the mapping is known, false otherwise.
62 bool
8383303e
ILT
63 get_output_offset(const Merge_map*, unsigned int shndx,
64 section_offset_type offset,
65 section_offset_type *output_offset);
4625f782
ILT
66
67 private:
68 // Map input section offsets to a length and an output section
69 // offset. An output section offset of -1 means that this part of
70 // the input section is being discarded.
71 struct Input_merge_entry
72 {
73 // The offset in the input section.
8383303e 74 section_offset_type input_offset;
4625f782 75 // The length.
8383303e 76 section_size_type length;
4625f782 77 // The offset in the output section.
8383303e 78 section_offset_type output_offset;
4625f782
ILT
79 };
80
81 // A less-than comparison routine for Input_merge_entry.
82 struct Input_merge_compare
83 {
84 bool
85 operator()(const Input_merge_entry& i1, const Input_merge_entry& i2) const
86 { return i1.input_offset < i2.input_offset; }
87 };
88
89 // A list of entries for a particular section.
90 struct Input_merge_map
91 {
92 // The Merge_map for this section.
93 const Merge_map* merge_map;
94 // The list of mappings.
95 std::vector<Input_merge_entry> entries;
96 // Whether the ENTRIES field is sorted by input_offset.
97 bool sorted;
98
99 Input_merge_map()
100 : merge_map(NULL), entries(), sorted(true)
101 { }
102 };
103
104 // Map input section indices to merge maps.
105 typedef std::map<unsigned int, Input_merge_map*> Section_merge_maps;
106
107 // Return a pointer to the Input_merge_map to use for the input
108 // section SHNDX, or NULL.
109 Input_merge_map*
110 get_input_merge_map(unsigned int shndx);
b8e6aad9 111
4625f782
ILT
112 // Get or make the the Input_merge_map to use for the section SHNDX
113 // with MERGE_MAP.
114 Input_merge_map*
115 get_or_make_input_merge_map(const Merge_map* merge_map, unsigned int shndx);
116
117 // Any given object file will normally only have a couple of input
118 // sections with mergeable contents. So we keep the first two input
119 // section numbers inline, and push any further ones into a map. A
120 // value of -1U in first_shnum_ or second_shnum_ means that we don't
121 // have a corresponding entry.
122 unsigned int first_shnum_;
123 Input_merge_map first_map_;
124 unsigned int second_shnum_;
125 Input_merge_map second_map_;
126 Section_merge_maps section_merge_maps_;
127};
128
129// Destructor.
130
131Object_merge_map::~Object_merge_map()
b8e6aad9 132{
4625f782
ILT
133 for (Section_merge_maps::iterator p = this->section_merge_maps_.begin();
134 p != this->section_merge_maps_.end();
135 ++p)
136 delete p->second;
137}
138
139// Get the Input_merge_map to use for an input section, or NULL.
140
141Object_merge_map::Input_merge_map*
142Object_merge_map::get_input_merge_map(unsigned int shndx)
143{
144 gold_assert(shndx != -1U);
145 if (shndx == this->first_shnum_)
146 return &this->first_map_;
147 if (shndx == this->second_shnum_)
148 return &this->second_map_;
149 Section_merge_maps::const_iterator p = this->section_merge_maps_.find(shndx);
150 if (p != this->section_merge_maps_.end())
151 return p->second;
152 return NULL;
153}
154
155// Get or create the Input_merge_map to use for an input section.
156
157Object_merge_map::Input_merge_map*
158Object_merge_map::get_or_make_input_merge_map(const Merge_map* merge_map,
159 unsigned int shndx)
160{
161 Input_merge_map* map = this->get_input_merge_map(shndx);
162 if (map != NULL)
163 {
164 // For a given input section in a given object, every mapping
165 // must be donw with the same Merge_map.
166 gold_assert(map->merge_map == merge_map);
167 return map;
168 }
169
170 // We need to create a new entry.
171 if (this->first_shnum_ == -1U)
cec9d2f3 172 {
4625f782
ILT
173 this->first_shnum_ = shndx;
174 this->first_map_.merge_map = merge_map;
175 return &this->first_map_;
cec9d2f3 176 }
4625f782
ILT
177 if (this->second_shnum_ == -1U)
178 {
179 this->second_shnum_ = shndx;
180 this->second_map_.merge_map = merge_map;
181 return &this->second_map_;
182 }
183
184 Input_merge_map* new_map = new Input_merge_map;
185 new_map->merge_map = merge_map;
186 this->section_merge_maps_[shndx] = new_map;
187 return new_map;
188}
189
190// Add a mapping.
191
192void
193Object_merge_map::add_mapping(const Merge_map* merge_map, unsigned int shndx,
8383303e
ILT
194 section_offset_type input_offset,
195 section_size_type length,
196 section_offset_type output_offset)
4625f782
ILT
197{
198 Input_merge_map* map = this->get_or_make_input_merge_map(merge_map, shndx);
199
200 // Try to merge the new entry in the last one we saw.
201 if (!map->entries.empty())
202 {
203 Input_merge_entry& entry(map->entries.back());
204
8383303e
ILT
205 // Use section_size_type to avoid signed/unsigned warnings.
206 section_size_type input_offset_u = input_offset;
207 section_size_type output_offset_u = output_offset;
208
4625f782
ILT
209 // If this entry is not in order, we need to sort the vector
210 // before looking anything up.
8383303e 211 if (input_offset_u < entry.input_offset + entry.length)
4625f782 212 {
8383303e
ILT
213 gold_assert(input_offset < entry.input_offset);
214 gold_assert(input_offset_u + length
215 <= static_cast<section_size_type>(entry.input_offset));
4625f782
ILT
216 map->sorted = false;
217 }
8383303e 218 else if (entry.input_offset + entry.length == input_offset_u
4625f782
ILT
219 && (output_offset == -1
220 ? entry.output_offset == -1
8383303e 221 : entry.output_offset + entry.length == output_offset_u))
4625f782
ILT
222 {
223 entry.length += length;
224 return;
225 }
226 }
227
228 Input_merge_entry entry;
229 entry.input_offset = input_offset;
230 entry.length = length;
231 entry.output_offset = output_offset;
232 map->entries.push_back(entry);
233}
234
235// Get the output offset for an input address.
236
237bool
238Object_merge_map::get_output_offset(const Merge_map* merge_map,
8383303e
ILT
239 unsigned int shndx,
240 section_offset_type input_offset,
241 section_offset_type *output_offset)
4625f782
ILT
242{
243 Input_merge_map* map = this->get_input_merge_map(shndx);
244 if (map == NULL || map->merge_map != merge_map)
245 return false;
246
247 if (!map->sorted)
248 {
249 std::sort(map->entries.begin(), map->entries.end(),
250 Input_merge_compare());
251 map->sorted = true;
252 }
253
254 Input_merge_entry entry;
255 entry.input_offset = input_offset;
256 std::vector<Input_merge_entry>::const_iterator p =
257 std::lower_bound(map->entries.begin(), map->entries.end(),
258 entry, Input_merge_compare());
259 if (p == map->entries.end() || p->input_offset > input_offset)
260 {
261 if (p == map->entries.begin())
262 return false;
263 --p;
264 gold_assert(p->input_offset <= input_offset);
265 }
266
8383303e
ILT
267 if (input_offset - p->input_offset
268 >= static_cast<section_offset_type>(p->length))
4625f782
ILT
269 return false;
270
271 *output_offset = p->output_offset;
272 if (*output_offset != -1)
273 *output_offset += (input_offset - p->input_offset);
274 return true;
b8e6aad9
ILT
275}
276
730cdc88
ILT
277// Class Merge_map.
278
279// Add a mapping for the bytes from OFFSET to OFFSET + LENGTH in input
280// section SHNDX in object OBJECT to an OUTPUT_OFFSET in a merged
281// output section.
b8e6aad9
ILT
282
283void
730cdc88 284Merge_map::add_mapping(Relobj* object, unsigned int shndx,
8383303e
ILT
285 section_offset_type offset, section_size_type length,
286 section_offset_type output_offset)
b8e6aad9 287{
4625f782
ILT
288 Object_merge_map* object_merge_map = object->merge_map();
289 if (object_merge_map == NULL)
290 {
291 object_merge_map = new Object_merge_map();
292 object->set_merge_map(object_merge_map);
293 }
294
295 object_merge_map->add_mapping(this, shndx, offset, length, output_offset);
b8e6aad9
ILT
296}
297
730cdc88
ILT
298// Return the output offset for an input address. The input address
299// is at offset OFFSET in section SHNDX in OBJECT. This sets
300// *OUTPUT_OFFSET to the offset in the output section. This returns
301// true if the mapping is known, false otherwise.
b8e6aad9
ILT
302
303bool
730cdc88 304Merge_map::get_output_offset(const Relobj* object, unsigned int shndx,
8383303e
ILT
305 section_offset_type offset,
306 section_offset_type* output_offset) const
b8e6aad9 307{
4625f782
ILT
308 Object_merge_map* object_merge_map = object->merge_map();
309 if (object_merge_map == NULL)
b8e6aad9 310 return false;
4625f782
ILT
311 return object_merge_map->get_output_offset(this, shndx, offset,
312 output_offset);
b8e6aad9
ILT
313}
314
730cdc88
ILT
315// Class Output_merge_base.
316
317// Return the output offset for an input offset. The input address is
318// at offset OFFSET in section SHNDX in OBJECT. If we know the
319// offset, set *POUTPUT and return true. Otherwise return false.
320
321bool
322Output_merge_base::do_output_offset(const Relobj* object,
323 unsigned int shndx,
8383303e
ILT
324 section_offset_type offset,
325 section_offset_type* poutput) const
730cdc88
ILT
326{
327 return this->merge_map_.get_output_offset(object, shndx, offset, poutput);
328}
329
330// Class Output_merge_data.
331
b8e6aad9
ILT
332// Compute the hash code for a fixed-size constant.
333
334size_t
335Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
336{
337 const unsigned char* p = this->pomd_->constant(k);
8383303e
ILT
338 section_size_type entsize =
339 convert_to_section_size_type(this->pomd_->entsize());
b8e6aad9
ILT
340
341 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
342 if (sizeof(size_t) == 8)
343 {
344 size_t result = static_cast<size_t>(14695981039346656037ULL);
8383303e 345 for (section_size_type i = 0; i < entsize; ++i)
b8e6aad9
ILT
346 {
347 result &= (size_t) *p++;
348 result *= 1099511628211ULL;
349 }
350 return result;
351 }
352 else
353 {
354 size_t result = 2166136261UL;
8383303e 355 for (section_size_type i = 0; i < entsize; ++i)
b8e6aad9
ILT
356 {
357 result ^= (size_t) *p++;
358 result *= 16777619UL;
359 }
360 return result;
361 }
362}
363
364// Return whether one hash table key equals another.
365
366bool
367Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
368 Merge_data_key k2) const
369{
370 const unsigned char* p1 = this->pomd_->constant(k1);
371 const unsigned char* p2 = this->pomd_->constant(k2);
372 return memcmp(p1, p2, this->pomd_->entsize()) == 0;
373}
374
375// Add a constant to the end of the section contents.
376
377void
378Output_merge_data::add_constant(const unsigned char* p)
379{
8383303e
ILT
380 section_size_type entsize = convert_to_section_size_type(this->entsize());
381 section_size_type addralign =
382 convert_to_section_size_type(this->addralign());
383 section_size_type addsize = std::max(entsize, addralign);
87f95776 384 if (this->len_ + addsize > this->alc_)
b8e6aad9
ILT
385 {
386 if (this->alc_ == 0)
87f95776 387 this->alc_ = 128 * addsize;
b8e6aad9
ILT
388 else
389 this->alc_ *= 2;
390 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
391 if (this->p_ == NULL)
75f2446e 392 gold_nomem();
b8e6aad9
ILT
393 }
394
395 memcpy(this->p_ + this->len_, p, entsize);
87f95776
ILT
396 if (addsize > entsize)
397 memset(this->p_ + this->len_ + entsize, 0, addsize - entsize);
398 this->len_ += addsize;
b8e6aad9
ILT
399}
400
401// Add the input section SHNDX in OBJECT to a merged output section
402// which holds fixed length constants. Return whether we were able to
403// handle the section; if not, it will be linked as usual without
404// constant merging.
405
406bool
407Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
408{
8383303e 409 section_size_type len;
9eb9fa57 410 const unsigned char* p = object->section_contents(shndx, &len, false);
b8e6aad9 411
8383303e 412 section_size_type entsize = convert_to_section_size_type(this->entsize());
b8e6aad9
ILT
413
414 if (len % entsize != 0)
415 return false;
416
38c5e8b4
ILT
417 this->input_count_ += len / entsize;
418
8383303e 419 for (section_size_type i = 0; i < len; i += entsize, p += entsize)
b8e6aad9
ILT
420 {
421 // Add the constant to the section contents. If we find that it
422 // is already in the hash table, we will remove it again.
423 Merge_data_key k = this->len_;
424 this->add_constant(p);
425
426 std::pair<Merge_data_hashtable::iterator, bool> ins =
427 this->hashtable_.insert(k);
428
429 if (!ins.second)
430 {
431 // Key was already present. Remove the copy we just added.
432 this->len_ -= entsize;
433 k = *ins.first;
434 }
435
436 // Record the offset of this constant in the output section.
730cdc88 437 this->add_mapping(object, shndx, i, entsize, k);
b8e6aad9
ILT
438 }
439
440 return true;
441}
442
443// Set the final data size in a merged output section with fixed size
444// constants.
445
446void
27bc2bce 447Output_merge_data::set_final_data_size()
b8e6aad9
ILT
448{
449 // Release the memory we don't need.
450 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
451 gold_assert(this->p_ != NULL);
452 this->set_data_size(this->len_);
453}
454
455// Write the data of a merged output section with fixed size constants
456// to the file.
457
458void
459Output_merge_data::do_write(Output_file* of)
460{
461 of->write(this->offset(), this->p_, this->len_);
462}
463
96803768
ILT
464// Write the data to a buffer.
465
466void
467Output_merge_data::do_write_to_buffer(unsigned char* buffer)
468{
469 memcpy(buffer, this->p_, this->len_);
470}
471
38c5e8b4
ILT
472// Print merge stats to stderr.
473
474void
475Output_merge_data::do_print_merge_stats(const char* section_name)
476{
477 fprintf(stderr,
478 _("%s: %s merged constants size: %lu; input: %zu; output: %zu\n"),
479 program_name, section_name,
480 static_cast<unsigned long>(this->entsize()),
481 this->input_count_, this->hashtable_.size());
482}
483
730cdc88
ILT
484// Class Output_merge_string.
485
b8e6aad9
ILT
486// Add an input section to a merged string section.
487
488template<typename Char_type>
489bool
490Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
491 unsigned int shndx)
492{
8383303e 493 section_size_type len;
9eb9fa57 494 const unsigned char* pdata = object->section_contents(shndx, &len, false);
b8e6aad9
ILT
495
496 const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
281b8327 497 const Char_type* pend = p + len;
b8e6aad9
ILT
498
499 if (len % sizeof(Char_type) != 0)
500 {
75f2446e
ILT
501 object->error(_("mergeable string section length not multiple of "
502 "character size"));
503 return false;
b8e6aad9 504 }
b8e6aad9 505
38c5e8b4
ILT
506 size_t count = 0;
507
fa1bd4fb 508 // The index I is in bytes, not characters.
8383303e 509 section_size_type i = 0;
b8e6aad9
ILT
510 while (i < len)
511 {
281b8327
ILT
512 const Char_type* pl;
513 for (pl = p; *pl != 0; ++pl)
b8e6aad9 514 {
281b8327 515 if (pl >= pend)
b8e6aad9 516 {
75f2446e
ILT
517 object->error(_("entry in mergeable string section "
518 "not null terminated"));
519 break;
b8e6aad9
ILT
520 }
521 }
522
281b8327 523 const Char_type* str = this->stringpool_.add_prefix(p, pl - p, NULL);
b8e6aad9 524
8383303e 525 section_size_type bytelen_with_null = ((pl - p) + 1) * sizeof(Char_type);
730cdc88
ILT
526 this->merged_strings_.push_back(Merged_string(object, shndx, i, str,
527 bytelen_with_null));
b8e6aad9 528
281b8327 529 p = pl + 1;
730cdc88 530 i += bytelen_with_null;
38c5e8b4 531 ++count;
b8e6aad9
ILT
532 }
533
38c5e8b4
ILT
534 this->input_count_ += count;
535
b8e6aad9
ILT
536 return true;
537}
538
9a0910c3
ILT
539// Finalize the mappings from the input sections to the output
540// section, and return the final data size.
b8e6aad9
ILT
541
542template<typename Char_type>
8383303e 543section_size_type
9a0910c3 544Output_merge_string<Char_type>::finalize_merged_data()
b8e6aad9
ILT
545{
546 this->stringpool_.set_string_offsets();
547
42e3fe0d
ILT
548 for (typename Merged_strings::const_iterator p =
549 this->merged_strings_.begin();
550 p != this->merged_strings_.end();
b8e6aad9 551 ++p)
730cdc88 552 this->add_mapping(p->object, p->shndx, p->offset, p->length,
42e3fe0d 553 this->stringpool_.get_offset(p->string));
b8e6aad9 554
b8e6aad9 555 // Save some memory.
42e3fe0d 556 this->merged_strings_.clear();
9a0910c3
ILT
557
558 return this->stringpool_.get_strtab_size();
559}
560
561template<typename Char_type>
562void
563Output_merge_string<Char_type>::set_final_data_size()
564{
565 const off_t final_data_size = this->finalize_merged_data();
566 this->set_data_size(final_data_size);
b8e6aad9
ILT
567}
568
569// Write out a merged string section.
570
571template<typename Char_type>
572void
573Output_merge_string<Char_type>::do_write(Output_file* of)
574{
575 this->stringpool_.write(of, this->offset());
576}
577
96803768
ILT
578// Write a merged string section to a buffer.
579
580template<typename Char_type>
581void
582Output_merge_string<Char_type>::do_write_to_buffer(unsigned char* buffer)
583{
584 this->stringpool_.write_to_buffer(buffer, this->data_size());
585}
586
38c5e8b4
ILT
587// Return the name of the types of string to use with
588// do_print_merge_stats.
589
590template<typename Char_type>
591const char*
592Output_merge_string<Char_type>::string_name()
593{
594 gold_unreachable();
595 return NULL;
596}
597
598template<>
599const char*
600Output_merge_string<char>::string_name()
601{
602 return "strings";
603}
604
605template<>
606const char*
607Output_merge_string<uint16_t>::string_name()
608{
609 return "16-bit strings";
610}
611
612template<>
613const char*
614Output_merge_string<uint32_t>::string_name()
615{
616 return "32-bit strings";
617}
618
619// Print merge stats to stderr.
620
621template<typename Char_type>
622void
623Output_merge_string<Char_type>::do_print_merge_stats(const char* section_name)
624{
625 char buf[200];
626 snprintf(buf, sizeof buf, "%s merged %s", section_name, this->string_name());
627 fprintf(stderr, _("%s: %s input: %zu\n"),
628 program_name, buf, this->input_count_);
629 this->stringpool_.print_stats(buf);
630}
631
b8e6aad9
ILT
632// Instantiate the templates we need.
633
634template
635class Output_merge_string<char>;
636
637template
638class Output_merge_string<uint16_t>;
639
640template
641class Output_merge_string<uint32_t>;
642
643} // End namespace gold.
This page took 0.080234 seconds and 4 git commands to generate.