Remove empty class Merge_map.
[deliverable/binutils-gdb.git] / gold / merge.cc
1 // merge.cc -- handle section merging for gold
2
3 // Copyright (C) 2006-2015 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 #include "gold.h"
24
25 #include <cstdlib>
26 #include <algorithm>
27
28 #include "merge.h"
29 #include "compressed_output.h"
30
31 namespace gold
32 {
33
34 // Class Object_merge_map.
35
36 // Destructor.
37
38 Object_merge_map::~Object_merge_map()
39 {
40 for (Section_merge_maps::iterator p = this->section_merge_maps_.begin();
41 p != this->section_merge_maps_.end();
42 ++p)
43 delete p->second;
44 }
45
46 // Get the Input_merge_map to use for an input section, or NULL.
47
48 Object_merge_map::Input_merge_map*
49 Object_merge_map::get_input_merge_map(unsigned int shndx)
50 {
51 gold_assert(shndx != -1U);
52 if (shndx == this->first_shnum_)
53 return &this->first_map_;
54 if (shndx == this->second_shnum_)
55 return &this->second_map_;
56 Section_merge_maps::const_iterator p = this->section_merge_maps_.find(shndx);
57 if (p != this->section_merge_maps_.end())
58 return p->second;
59 return NULL;
60 }
61
62 // Get or create the Input_merge_map to use for an input section.
63
64 Object_merge_map::Input_merge_map*
65 Object_merge_map::get_or_make_input_merge_map(
66 const Output_section_data* output_data, unsigned int shndx) {
67 Input_merge_map* map = this->get_input_merge_map(shndx);
68 if (map != NULL)
69 {
70 // For a given input section in a given object, every mapping
71 // must be done with the same Merge_map.
72 gold_assert(map->output_data == output_data);
73 return map;
74 }
75
76 // We need to create a new entry.
77 if (this->first_shnum_ == -1U)
78 {
79 this->first_shnum_ = shndx;
80 this->first_map_.output_data = output_data;
81 return &this->first_map_;
82 }
83 if (this->second_shnum_ == -1U)
84 {
85 this->second_shnum_ = shndx;
86 this->second_map_.output_data = output_data;
87 return &this->second_map_;
88 }
89
90 Input_merge_map* new_map = new Input_merge_map;
91 new_map->output_data = output_data;
92 this->section_merge_maps_[shndx] = new_map;
93 return new_map;
94 }
95
96 // Add a mapping.
97
98 void
99 Object_merge_map::add_mapping(const Output_section_data* output_data,
100 unsigned int shndx,
101 section_offset_type input_offset,
102 section_size_type length,
103 section_offset_type output_offset)
104 {
105 Input_merge_map* map = this->get_or_make_input_merge_map(output_data, shndx);
106
107 // Try to merge the new entry in the last one we saw.
108 if (!map->entries.empty())
109 {
110 Input_merge_entry& entry(map->entries.back());
111
112 // Use section_size_type to avoid signed/unsigned warnings.
113 section_size_type input_offset_u = input_offset;
114 section_size_type output_offset_u = output_offset;
115
116 // If this entry is not in order, we need to sort the vector
117 // before looking anything up.
118 if (input_offset_u < entry.input_offset + entry.length)
119 {
120 gold_assert(input_offset < entry.input_offset);
121 gold_assert(input_offset_u + length
122 <= static_cast<section_size_type>(entry.input_offset));
123 map->sorted = false;
124 }
125 else if (entry.input_offset + entry.length == input_offset_u
126 && (output_offset == -1
127 ? entry.output_offset == -1
128 : entry.output_offset + entry.length == output_offset_u))
129 {
130 entry.length += length;
131 return;
132 }
133 }
134
135 Input_merge_entry entry;
136 entry.input_offset = input_offset;
137 entry.length = length;
138 entry.output_offset = output_offset;
139 map->entries.push_back(entry);
140 }
141
142 // Get the output offset for an input address.
143
144 bool
145 Object_merge_map::get_output_offset(unsigned int shndx,
146 section_offset_type input_offset,
147 section_offset_type* output_offset)
148 {
149 Input_merge_map* map = this->get_input_merge_map(shndx);
150 if (map == NULL)
151 return false;
152
153 if (!map->sorted)
154 {
155 std::sort(map->entries.begin(), map->entries.end(),
156 Input_merge_compare());
157 map->sorted = true;
158 }
159
160 Input_merge_entry entry;
161 entry.input_offset = input_offset;
162 std::vector<Input_merge_entry>::const_iterator p =
163 std::upper_bound(map->entries.begin(), map->entries.end(),
164 entry, Input_merge_compare());
165 if (p == map->entries.begin())
166 return false;
167 --p;
168 gold_assert(p->input_offset <= input_offset);
169
170 if (input_offset - p->input_offset
171 >= static_cast<section_offset_type>(p->length))
172 return false;
173
174 *output_offset = p->output_offset;
175 if (*output_offset != -1)
176 *output_offset += (input_offset - p->input_offset);
177 return true;
178 }
179
180 // Return whether this is the merge map for section SHNDX.
181
182 bool
183 Object_merge_map::is_merge_section_for(const Output_section_data* output_data,
184 unsigned int shndx)
185 {
186 Input_merge_map* map = this->get_input_merge_map(shndx);
187 return map != NULL && map->output_data == output_data;
188 }
189
190 // Initialize a mapping from input offsets to output addresses.
191
192 template<int size>
193 void
194 Object_merge_map::initialize_input_to_output_map(
195 unsigned int shndx,
196 typename elfcpp::Elf_types<size>::Elf_Addr starting_address,
197 Unordered_map<section_offset_type,
198 typename elfcpp::Elf_types<size>::Elf_Addr>* initialize_map)
199 {
200 Input_merge_map* map = this->get_input_merge_map(shndx);
201 gold_assert(map != NULL);
202
203 gold_assert(initialize_map->empty());
204 // We know how many entries we are going to add.
205 // reserve_unordered_map takes an expected count of buckets, not a
206 // count of elements, so double it to try to reduce collisions.
207 reserve_unordered_map(initialize_map, map->entries.size() * 2);
208
209 for (Input_merge_map::Entries::const_iterator p = map->entries.begin();
210 p != map->entries.end();
211 ++p)
212 {
213 section_offset_type output_offset = p->output_offset;
214 if (output_offset != -1)
215 output_offset += starting_address;
216 else
217 {
218 // If we see a relocation against an address we have chosen
219 // to discard, we relocate to zero. FIXME: We could also
220 // issue a warning in this case; that would require
221 // reporting this somehow and checking it in the routines in
222 // reloc.h.
223 output_offset = 0;
224 }
225 initialize_map->insert(std::make_pair(p->input_offset, output_offset));
226 }
227 }
228
229 // Class Output_merge_base.
230
231 // Return the output offset for an input offset. The input address is
232 // at offset OFFSET in section SHNDX in OBJECT. If we know the
233 // offset, set *POUTPUT and return true. Otherwise return false.
234
235 bool
236 Output_merge_base::do_output_offset(const Relobj* object,
237 unsigned int shndx,
238 section_offset_type offset,
239 section_offset_type* poutput) const
240 {
241 return object->merge_output_offset(shndx, offset, poutput);
242 }
243
244 // Record a merged input section for script processing.
245
246 void
247 Output_merge_base::record_input_section(Relobj* relobj, unsigned int shndx)
248 {
249 gold_assert(this->keeps_input_sections_ && relobj != NULL);
250 // If this is the first input section, record it. We need do this because
251 // this->input_sections_ is unordered.
252 if (this->first_relobj_ == NULL)
253 {
254 this->first_relobj_ = relobj;
255 this->first_shndx_ = shndx;
256 }
257
258 std::pair<Input_sections::iterator, bool> result =
259 this->input_sections_.insert(Section_id(relobj, shndx));
260 // We should insert a merge section once only.
261 gold_assert(result.second);
262 }
263
264 // Class Output_merge_data.
265
266 // Compute the hash code for a fixed-size constant.
267
268 size_t
269 Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
270 {
271 const unsigned char* p = this->pomd_->constant(k);
272 section_size_type entsize =
273 convert_to_section_size_type(this->pomd_->entsize());
274
275 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
276 if (sizeof(size_t) == 8)
277 {
278 size_t result = static_cast<size_t>(14695981039346656037ULL);
279 for (section_size_type i = 0; i < entsize; ++i)
280 {
281 result &= (size_t) *p++;
282 result *= 1099511628211ULL;
283 }
284 return result;
285 }
286 else
287 {
288 size_t result = 2166136261UL;
289 for (section_size_type i = 0; i < entsize; ++i)
290 {
291 result ^= (size_t) *p++;
292 result *= 16777619UL;
293 }
294 return result;
295 }
296 }
297
298 // Return whether one hash table key equals another.
299
300 bool
301 Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
302 Merge_data_key k2) const
303 {
304 const unsigned char* p1 = this->pomd_->constant(k1);
305 const unsigned char* p2 = this->pomd_->constant(k2);
306 return memcmp(p1, p2, this->pomd_->entsize()) == 0;
307 }
308
309 // Add a constant to the end of the section contents.
310
311 void
312 Output_merge_data::add_constant(const unsigned char* p)
313 {
314 section_size_type entsize = convert_to_section_size_type(this->entsize());
315 section_size_type addralign =
316 convert_to_section_size_type(this->addralign());
317 section_size_type addsize = std::max(entsize, addralign);
318 if (this->len_ + addsize > this->alc_)
319 {
320 if (this->alc_ == 0)
321 this->alc_ = 128 * addsize;
322 else
323 this->alc_ *= 2;
324 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
325 if (this->p_ == NULL)
326 gold_nomem();
327 }
328
329 memcpy(this->p_ + this->len_, p, entsize);
330 if (addsize > entsize)
331 memset(this->p_ + this->len_ + entsize, 0, addsize - entsize);
332 this->len_ += addsize;
333 }
334
335 // Add the input section SHNDX in OBJECT to a merged output section
336 // which holds fixed length constants. Return whether we were able to
337 // handle the section; if not, it will be linked as usual without
338 // constant merging.
339
340 bool
341 Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
342 {
343 section_size_type len;
344 bool is_new;
345 const unsigned char* p = object->decompressed_section_contents(shndx, &len,
346 &is_new);
347
348 section_size_type entsize = convert_to_section_size_type(this->entsize());
349
350 if (len % entsize != 0)
351 {
352 if (is_new)
353 delete[] p;
354 return false;
355 }
356
357 this->input_count_ += len / entsize;
358
359 for (section_size_type i = 0; i < len; i += entsize, p += entsize)
360 {
361 // Add the constant to the section contents. If we find that it
362 // is already in the hash table, we will remove it again.
363 Merge_data_key k = this->len_;
364 this->add_constant(p);
365
366 std::pair<Merge_data_hashtable::iterator, bool> ins =
367 this->hashtable_.insert(k);
368
369 if (!ins.second)
370 {
371 // Key was already present. Remove the copy we just added.
372 this->len_ -= entsize;
373 k = *ins.first;
374 }
375
376 // Record the offset of this constant in the output section.
377 object->add_merge_mapping(this, shndx, i, entsize, k);
378 }
379
380 // For script processing, we keep the input sections.
381 if (this->keeps_input_sections())
382 record_input_section(object, shndx);
383
384 if (is_new)
385 delete[] p;
386
387 return true;
388 }
389
390 // Set the final data size in a merged output section with fixed size
391 // constants.
392
393 void
394 Output_merge_data::set_final_data_size()
395 {
396 // Release the memory we don't need.
397 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
398 // An Output_merge_data object may be empty and realloc is allowed
399 // to return a NULL pointer in this case. An Output_merge_data is empty
400 // if all its input sections have sizes that are not multiples of entsize.
401 gold_assert(this->p_ != NULL || this->len_ == 0);
402 this->set_data_size(this->len_);
403 }
404
405 // Write the data of a merged output section with fixed size constants
406 // to the file.
407
408 void
409 Output_merge_data::do_write(Output_file* of)
410 {
411 of->write(this->offset(), this->p_, this->len_);
412 }
413
414 // Write the data to a buffer.
415
416 void
417 Output_merge_data::do_write_to_buffer(unsigned char* buffer)
418 {
419 memcpy(buffer, this->p_, this->len_);
420 }
421
422 // Print merge stats to stderr.
423
424 void
425 Output_merge_data::do_print_merge_stats(const char* section_name)
426 {
427 fprintf(stderr,
428 _("%s: %s merged constants size: %lu; input: %zu; output: %zu\n"),
429 program_name, section_name,
430 static_cast<unsigned long>(this->entsize()),
431 this->input_count_, this->hashtable_.size());
432 }
433
434 // Class Output_merge_string.
435
436 // Add an input section to a merged string section.
437
438 template<typename Char_type>
439 bool
440 Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
441 unsigned int shndx)
442 {
443 section_size_type sec_len;
444 bool is_new;
445 const unsigned char* pdata = object->decompressed_section_contents(shndx,
446 &sec_len,
447 &is_new);
448
449 const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
450 const Char_type* pend = p + sec_len / sizeof(Char_type);
451 const Char_type* pend0 = pend;
452
453 if (sec_len % sizeof(Char_type) != 0)
454 {
455 object->error(_("mergeable string section length not multiple of "
456 "character size"));
457 if (is_new)
458 delete[] pdata;
459 return false;
460 }
461
462 if (pend[-1] != 0)
463 {
464 gold_warning(_("%s: last entry in mergeable string section '%s' "
465 "not null terminated"),
466 object->name().c_str(),
467 object->section_name(shndx).c_str());
468 // Find the end of the last NULL-terminated string in the buffer.
469 while (pend0 > p && pend0[-1] != 0)
470 --pend0;
471 }
472
473 Merged_strings_list* merged_strings_list =
474 new Merged_strings_list(object, shndx);
475 this->merged_strings_lists_.push_back(merged_strings_list);
476 Merged_strings& merged_strings = merged_strings_list->merged_strings;
477
478 // Count the number of non-null strings in the section and size the list.
479 size_t count = 0;
480 const Char_type* pt = p;
481 while (pt < pend0)
482 {
483 size_t len = string_length(pt);
484 if (len != 0)
485 ++count;
486 pt += len + 1;
487 }
488 if (pend0 < pend)
489 ++count;
490 merged_strings.reserve(count + 1);
491
492 // The index I is in bytes, not characters.
493 section_size_type i = 0;
494
495 // We assume here that the beginning of the section is correctly
496 // aligned, so each string within the section must retain the same
497 // modulo.
498 uintptr_t init_align_modulo = (reinterpret_cast<uintptr_t>(pdata)
499 & (this->addralign() - 1));
500 bool has_misaligned_strings = false;
501
502 while (p < pend)
503 {
504 size_t len = p < pend0 ? string_length(p) : pend - p;
505
506 // Within merge input section each string must be aligned.
507 if (len != 0
508 && ((reinterpret_cast<uintptr_t>(p) & (this->addralign() - 1))
509 != init_align_modulo))
510 has_misaligned_strings = true;
511
512 Stringpool::Key key;
513 this->stringpool_.add_with_length(p, len, true, &key);
514
515 merged_strings.push_back(Merged_string(i, key));
516 p += len + 1;
517 i += (len + 1) * sizeof(Char_type);
518 }
519
520 // Record the last offset in the input section so that we can
521 // compute the length of the last string.
522 merged_strings.push_back(Merged_string(i, 0));
523
524 this->input_count_ += count;
525 this->input_size_ += i;
526
527 if (has_misaligned_strings)
528 gold_warning(_("%s: section %s contains incorrectly aligned strings;"
529 " the alignment of those strings won't be preserved"),
530 object->name().c_str(),
531 object->section_name(shndx).c_str());
532
533 // For script processing, we keep the input sections.
534 if (this->keeps_input_sections())
535 record_input_section(object, shndx);
536
537 if (is_new)
538 delete[] pdata;
539
540 return true;
541 }
542
543 // Finalize the mappings from the input sections to the output
544 // section, and return the final data size.
545
546 template<typename Char_type>
547 section_size_type
548 Output_merge_string<Char_type>::finalize_merged_data()
549 {
550 this->stringpool_.set_string_offsets();
551
552 for (typename Merged_strings_lists::const_iterator l =
553 this->merged_strings_lists_.begin();
554 l != this->merged_strings_lists_.end();
555 ++l)
556 {
557 section_offset_type last_input_offset = 0;
558 section_offset_type last_output_offset = 0;
559 for (typename Merged_strings::const_iterator p =
560 (*l)->merged_strings.begin();
561 p != (*l)->merged_strings.end();
562 ++p)
563 {
564 section_size_type length = p->offset - last_input_offset;
565 if (length > 0)
566 (*l)->object->add_merge_mapping(this, (*l)->shndx,
567 last_input_offset, length, last_output_offset);
568 last_input_offset = p->offset;
569 if (p->stringpool_key != 0)
570 last_output_offset =
571 this->stringpool_.get_offset_from_key(p->stringpool_key);
572 }
573 delete *l;
574 }
575
576 // Save some memory. This also ensures that this function will work
577 // if called twice, as may happen if Layout::set_segment_offsets
578 // finds a better alignment.
579 this->merged_strings_lists_.clear();
580
581 return this->stringpool_.get_strtab_size();
582 }
583
584 template<typename Char_type>
585 void
586 Output_merge_string<Char_type>::set_final_data_size()
587 {
588 const off_t final_data_size = this->finalize_merged_data();
589 this->set_data_size(final_data_size);
590 }
591
592 // Write out a merged string section.
593
594 template<typename Char_type>
595 void
596 Output_merge_string<Char_type>::do_write(Output_file* of)
597 {
598 this->stringpool_.write(of, this->offset());
599 }
600
601 // Write a merged string section to a buffer.
602
603 template<typename Char_type>
604 void
605 Output_merge_string<Char_type>::do_write_to_buffer(unsigned char* buffer)
606 {
607 this->stringpool_.write_to_buffer(buffer, this->data_size());
608 }
609
610 // Return the name of the types of string to use with
611 // do_print_merge_stats.
612
613 template<typename Char_type>
614 const char*
615 Output_merge_string<Char_type>::string_name()
616 {
617 gold_unreachable();
618 return NULL;
619 }
620
621 template<>
622 const char*
623 Output_merge_string<char>::string_name()
624 {
625 return "strings";
626 }
627
628 template<>
629 const char*
630 Output_merge_string<uint16_t>::string_name()
631 {
632 return "16-bit strings";
633 }
634
635 template<>
636 const char*
637 Output_merge_string<uint32_t>::string_name()
638 {
639 return "32-bit strings";
640 }
641
642 // Print merge stats to stderr.
643
644 template<typename Char_type>
645 void
646 Output_merge_string<Char_type>::do_print_merge_stats(const char* section_name)
647 {
648 char buf[200];
649 snprintf(buf, sizeof buf, "%s merged %s", section_name, this->string_name());
650 fprintf(stderr, _("%s: %s input bytes: %zu\n"),
651 program_name, buf, this->input_size_);
652 fprintf(stderr, _("%s: %s input strings: %zu\n"),
653 program_name, buf, this->input_count_);
654 this->stringpool_.print_stats(buf);
655 }
656
657 // Instantiate the templates we need.
658
659 template
660 class Output_merge_string<char>;
661
662 template
663 class Output_merge_string<uint16_t>;
664
665 template
666 class Output_merge_string<uint32_t>;
667
668 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
669 template
670 void
671 Object_merge_map::initialize_input_to_output_map<32>(
672 unsigned int shndx,
673 elfcpp::Elf_types<32>::Elf_Addr starting_address,
674 Unordered_map<section_offset_type, elfcpp::Elf_types<32>::Elf_Addr>*);
675 #endif
676
677 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
678 template
679 void
680 Object_merge_map::initialize_input_to_output_map<64>(
681 unsigned int shndx,
682 elfcpp::Elf_types<64>::Elf_Addr starting_address,
683 Unordered_map<section_offset_type, elfcpp::Elf_types<64>::Elf_Addr>*);
684 #endif
685
686 } // End namespace gold.
This page took 0.05927 seconds and 5 git commands to generate.