* section.c (bfd_get_section_contents): Detect and handle the case
[binutils.git] / gold / merge.cc
blob25ecc40c0e17c14b7ef6605a98ad6c3f7065912a
1 // merge.cc -- handle section merging for gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <cstdlib>
26 #include <algorithm>
28 #include "merge.h"
30 namespace gold
33 // Class Object_merge_map.
35 // Destructor.
37 Object_merge_map::~Object_merge_map()
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;
45 // Get the Input_merge_map to use for an input section, or NULL.
47 Object_merge_map::Input_merge_map*
48 Object_merge_map::get_input_merge_map(unsigned int shndx)
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;
61 // Get or create the Input_merge_map to use for an input section.
63 Object_merge_map::Input_merge_map*
64 Object_merge_map::get_or_make_input_merge_map(const Merge_map* merge_map,
65 unsigned int shndx)
67 Input_merge_map* map = this->get_input_merge_map(shndx);
68 if (map != NULL)
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->merge_map == merge_map);
73 return map;
76 // We need to create a new entry.
77 if (this->first_shnum_ == -1U)
79 this->first_shnum_ = shndx;
80 this->first_map_.merge_map = merge_map;
81 return &this->first_map_;
83 if (this->second_shnum_ == -1U)
85 this->second_shnum_ = shndx;
86 this->second_map_.merge_map = merge_map;
87 return &this->second_map_;
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;
96 // Add a mapping.
98 void
99 Object_merge_map::add_mapping(const Merge_map* merge_map, unsigned int shndx,
100 section_offset_type input_offset,
101 section_size_type length,
102 section_offset_type output_offset)
104 Input_merge_map* map = this->get_or_make_input_merge_map(merge_map, shndx);
106 // Try to merge the new entry in the last one we saw.
107 if (!map->entries.empty())
109 Input_merge_entry& entry(map->entries.back());
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;
115 // If this entry is not in order, we need to sort the vector
116 // before looking anything up.
117 if (input_offset_u < entry.input_offset + entry.length)
119 gold_assert(input_offset < entry.input_offset);
120 gold_assert(input_offset_u + length
121 <= static_cast<section_size_type>(entry.input_offset));
122 map->sorted = false;
124 else if (entry.input_offset + entry.length == input_offset_u
125 && (output_offset == -1
126 ? entry.output_offset == -1
127 : entry.output_offset + entry.length == output_offset_u))
129 entry.length += length;
130 return;
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);
141 // Get the output offset for an input address.
143 bool
144 Object_merge_map::get_output_offset(const Merge_map* merge_map,
145 unsigned int shndx,
146 section_offset_type input_offset,
147 section_offset_type *output_offset)
149 Input_merge_map* map = this->get_input_merge_map(shndx);
150 if (map == NULL
151 || (merge_map != NULL && map->merge_map != merge_map))
152 return false;
154 if (!map->sorted)
156 std::sort(map->entries.begin(), map->entries.end(),
157 Input_merge_compare());
158 map->sorted = true;
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)
168 if (p == map->entries.begin())
169 return false;
170 --p;
171 gold_assert(p->input_offset <= input_offset);
174 if (input_offset - p->input_offset
175 >= static_cast<section_offset_type>(p->length))
176 return false;
178 *output_offset = p->output_offset;
179 if (*output_offset != -1)
180 *output_offset += (input_offset - p->input_offset);
181 return true;
184 // Return whether this is the merge map for section SHNDX.
186 inline bool
187 Object_merge_map::is_merge_section_for(const Merge_map* merge_map,
188 unsigned int shndx)
190 Input_merge_map* map = this->get_input_merge_map(shndx);
191 return map != NULL && map->merge_map == merge_map;
194 // Initialize a mapping from input offsets to output addresses.
196 template<int size>
197 void
198 Object_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)
204 Input_merge_map* map = this->get_input_merge_map(shndx);
205 gold_assert(map != NULL);
207 for (Input_merge_map::Entries::const_iterator p = map->entries.begin();
208 p != map->entries.end();
209 ++p)
211 section_offset_type output_offset = p->output_offset;
212 if (output_offset != -1)
213 output_offset += starting_address;
214 else
216 // If we see a relocation against an address we have chosen
217 // to discard, we relocate to zero. FIXME: We could also
218 // issue a warning in this case; that would require
219 // reporting this somehow and checking it in the routines in
220 // reloc.h.
221 output_offset = 0;
223 initialize_map->insert(std::make_pair(p->input_offset, output_offset));
227 // Class Merge_map.
229 // Add a mapping for the bytes from OFFSET to OFFSET + LENGTH in input
230 // section SHNDX in object OBJECT to an OUTPUT_OFFSET in merged data
231 // in an output section.
233 void
234 Merge_map::add_mapping(Relobj* object, unsigned int shndx,
235 section_offset_type offset, section_size_type length,
236 section_offset_type output_offset)
238 Object_merge_map* object_merge_map = object->merge_map();
239 if (object_merge_map == NULL)
241 object_merge_map = new Object_merge_map();
242 object->set_merge_map(object_merge_map);
245 object_merge_map->add_mapping(this, shndx, offset, length, output_offset);
248 // Return the output offset for an input address. The input address
249 // is at offset OFFSET in section SHNDX in OBJECT. This sets
250 // *OUTPUT_OFFSET to the offset in the merged data in the output
251 // section. This returns true if the mapping is known, false
252 // otherwise.
254 bool
255 Merge_map::get_output_offset(const Relobj* object, unsigned int shndx,
256 section_offset_type offset,
257 section_offset_type* output_offset) const
259 Object_merge_map* object_merge_map = object->merge_map();
260 if (object_merge_map == NULL)
261 return false;
262 return object_merge_map->get_output_offset(this, shndx, offset,
263 output_offset);
266 // Return whether this is the merge section for SHNDX in OBJECT.
268 bool
269 Merge_map::is_merge_section_for(const Relobj* object, unsigned int shndx) const
271 Object_merge_map* object_merge_map = object->merge_map();
272 if (object_merge_map == NULL)
273 return false;
274 return object_merge_map->is_merge_section_for(this, shndx);
277 // Class Output_merge_base.
279 // Return the output offset for an input offset. The input address is
280 // at offset OFFSET in section SHNDX in OBJECT. If we know the
281 // offset, set *POUTPUT and return true. Otherwise return false.
283 bool
284 Output_merge_base::do_output_offset(const Relobj* object,
285 unsigned int shndx,
286 section_offset_type offset,
287 section_offset_type* poutput) const
289 return this->merge_map_.get_output_offset(object, shndx, offset, poutput);
292 // Return whether this is the merge section for SHNDX in OBJECT.
294 bool
295 Output_merge_base::do_is_merge_section_for(const Relobj* object,
296 unsigned int shndx) const
298 return this->merge_map_.is_merge_section_for(object, shndx);
301 // Class Output_merge_data.
303 // Compute the hash code for a fixed-size constant.
305 size_t
306 Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
308 const unsigned char* p = this->pomd_->constant(k);
309 section_size_type entsize =
310 convert_to_section_size_type(this->pomd_->entsize());
312 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
313 if (sizeof(size_t) == 8)
315 size_t result = static_cast<size_t>(14695981039346656037ULL);
316 for (section_size_type i = 0; i < entsize; ++i)
318 result &= (size_t) *p++;
319 result *= 1099511628211ULL;
321 return result;
323 else
325 size_t result = 2166136261UL;
326 for (section_size_type i = 0; i < entsize; ++i)
328 result ^= (size_t) *p++;
329 result *= 16777619UL;
331 return result;
335 // Return whether one hash table key equals another.
337 bool
338 Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
339 Merge_data_key k2) const
341 const unsigned char* p1 = this->pomd_->constant(k1);
342 const unsigned char* p2 = this->pomd_->constant(k2);
343 return memcmp(p1, p2, this->pomd_->entsize()) == 0;
346 // Add a constant to the end of the section contents.
348 void
349 Output_merge_data::add_constant(const unsigned char* p)
351 section_size_type entsize = convert_to_section_size_type(this->entsize());
352 section_size_type addralign =
353 convert_to_section_size_type(this->addralign());
354 section_size_type addsize = std::max(entsize, addralign);
355 if (this->len_ + addsize > this->alc_)
357 if (this->alc_ == 0)
358 this->alc_ = 128 * addsize;
359 else
360 this->alc_ *= 2;
361 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
362 if (this->p_ == NULL)
363 gold_nomem();
366 memcpy(this->p_ + this->len_, p, entsize);
367 if (addsize > entsize)
368 memset(this->p_ + this->len_ + entsize, 0, addsize - entsize);
369 this->len_ += addsize;
372 // Add the input section SHNDX in OBJECT to a merged output section
373 // which holds fixed length constants. Return whether we were able to
374 // handle the section; if not, it will be linked as usual without
375 // constant merging.
377 bool
378 Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
380 section_size_type len;
381 const unsigned char* p = object->section_contents(shndx, &len, false);
383 section_size_type entsize = convert_to_section_size_type(this->entsize());
385 if (len % entsize != 0)
386 return false;
388 this->input_count_ += len / entsize;
390 for (section_size_type i = 0; i < len; i += entsize, p += entsize)
392 // Add the constant to the section contents. If we find that it
393 // is already in the hash table, we will remove it again.
394 Merge_data_key k = this->len_;
395 this->add_constant(p);
397 std::pair<Merge_data_hashtable::iterator, bool> ins =
398 this->hashtable_.insert(k);
400 if (!ins.second)
402 // Key was already present. Remove the copy we just added.
403 this->len_ -= entsize;
404 k = *ins.first;
407 // Record the offset of this constant in the output section.
408 this->add_mapping(object, shndx, i, entsize, k);
411 return true;
414 // Set the final data size in a merged output section with fixed size
415 // constants.
417 void
418 Output_merge_data::set_final_data_size()
420 // Release the memory we don't need.
421 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
422 gold_assert(this->p_ != NULL);
423 this->set_data_size(this->len_);
426 // Write the data of a merged output section with fixed size constants
427 // to the file.
429 void
430 Output_merge_data::do_write(Output_file* of)
432 of->write(this->offset(), this->p_, this->len_);
435 // Write the data to a buffer.
437 void
438 Output_merge_data::do_write_to_buffer(unsigned char* buffer)
440 memcpy(buffer, this->p_, this->len_);
443 // Print merge stats to stderr.
445 void
446 Output_merge_data::do_print_merge_stats(const char* section_name)
448 fprintf(stderr,
449 _("%s: %s merged constants size: %lu; input: %zu; output: %zu\n"),
450 program_name, section_name,
451 static_cast<unsigned long>(this->entsize()),
452 this->input_count_, this->hashtable_.size());
455 // Class Output_merge_string.
457 // Add an input section to a merged string section.
459 template<typename Char_type>
460 bool
461 Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
462 unsigned int shndx)
464 section_size_type len;
465 const unsigned char* pdata = object->section_contents(shndx, &len, false);
467 const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
468 const Char_type* pend = p + len / sizeof(Char_type);
470 if (len % sizeof(Char_type) != 0)
472 object->error(_("mergeable string section length not multiple of "
473 "character size"));
474 return false;
477 size_t count = 0;
479 // The index I is in bytes, not characters.
480 section_size_type i = 0;
481 while (i < len)
483 const Char_type* pl;
484 for (pl = p; *pl != 0; ++pl)
486 if (pl >= pend)
488 gold_warning(_("%s: last entry in mergeable string section '%s' "
489 "not null terminated"),
490 object->name().c_str(),
491 object->section_name(shndx).c_str());
492 break;
496 Stringpool::Key key;
497 const Char_type* str = this->stringpool_.add_with_length(p, pl - p, true,
498 &key);
500 section_size_type bytelen_with_null = ((pl - p) + 1) * sizeof(Char_type);
501 this->merged_strings_.push_back(Merged_string(object, shndx, i, str,
502 bytelen_with_null, key));
504 p = pl + 1;
505 i += bytelen_with_null;
506 ++count;
509 this->input_count_ += count;
511 return true;
514 // Finalize the mappings from the input sections to the output
515 // section, and return the final data size.
517 template<typename Char_type>
518 section_size_type
519 Output_merge_string<Char_type>::finalize_merged_data()
521 this->stringpool_.set_string_offsets();
523 for (typename Merged_strings::const_iterator p =
524 this->merged_strings_.begin();
525 p != this->merged_strings_.end();
526 ++p)
528 section_offset_type offset =
529 this->stringpool_.get_offset_from_key(p->stringpool_key);
530 this->add_mapping(p->object, p->shndx, p->offset, p->length, offset);
533 // Save some memory. This also ensures that this function will work
534 // if called twice, as may happen if Layout::set_segment_offsets
535 // finds a better alignment.
536 this->merged_strings_.clear();
538 return this->stringpool_.get_strtab_size();
541 template<typename Char_type>
542 void
543 Output_merge_string<Char_type>::set_final_data_size()
545 const off_t final_data_size = this->finalize_merged_data();
546 this->set_data_size(final_data_size);
549 // Write out a merged string section.
551 template<typename Char_type>
552 void
553 Output_merge_string<Char_type>::do_write(Output_file* of)
555 this->stringpool_.write(of, this->offset());
558 // Write a merged string section to a buffer.
560 template<typename Char_type>
561 void
562 Output_merge_string<Char_type>::do_write_to_buffer(unsigned char* buffer)
564 this->stringpool_.write_to_buffer(buffer, this->data_size());
567 // Return the name of the types of string to use with
568 // do_print_merge_stats.
570 template<typename Char_type>
571 const char*
572 Output_merge_string<Char_type>::string_name()
574 gold_unreachable();
575 return NULL;
578 template<>
579 const char*
580 Output_merge_string<char>::string_name()
582 return "strings";
585 template<>
586 const char*
587 Output_merge_string<uint16_t>::string_name()
589 return "16-bit strings";
592 template<>
593 const char*
594 Output_merge_string<uint32_t>::string_name()
596 return "32-bit strings";
599 // Print merge stats to stderr.
601 template<typename Char_type>
602 void
603 Output_merge_string<Char_type>::do_print_merge_stats(const char* section_name)
605 char buf[200];
606 snprintf(buf, sizeof buf, "%s merged %s", section_name, this->string_name());
607 fprintf(stderr, _("%s: %s input: %zu\n"),
608 program_name, buf, this->input_count_);
609 this->stringpool_.print_stats(buf);
612 // Instantiate the templates we need.
614 template
615 class Output_merge_string<char>;
617 template
618 class Output_merge_string<uint16_t>;
620 template
621 class Output_merge_string<uint32_t>;
623 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
624 template
625 void
626 Object_merge_map::initialize_input_to_output_map<32>(
627 unsigned int shndx,
628 elfcpp::Elf_types<32>::Elf_Addr starting_address,
629 Unordered_map<section_offset_type, elfcpp::Elf_types<32>::Elf_Addr>*);
630 #endif
632 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
633 template
634 void
635 Object_merge_map::initialize_input_to_output_map<64>(
636 unsigned int shndx,
637 elfcpp::Elf_types<64>::Elf_Addr starting_address,
638 Unordered_map<section_offset_type, elfcpp::Elf_types<64>::Elf_Addr>*);
639 #endif
641 } // End namespace gold.