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