2010-05-23 Doug Kwan <dougkwan@google.com>
[external/binutils.git] / gold / merge.cc
1 // merge.cc -- handle section merging for gold
2
3 // Copyright 2006, 2007, 2008 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
30 namespace gold
31 {
32
33 // Class Object_merge_map.
34
35 // Destructor.
36
37 Object_merge_map::~Object_merge_map()
38 {
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
47 Object_merge_map::Input_merge_map*
48 Object_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
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)
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
71       // must be done with the same Merge_map.
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)
78     {
79       this->first_shnum_ = shndx;
80       this->first_map_.merge_map = merge_map;
81       return &this->first_map_;
82     }
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
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)
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
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
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)
118         {
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;
123         }
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))
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
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)
148 {
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;
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
174   if (input_offset - p->input_offset
175       >= static_cast<section_offset_type>(p->length))
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;
182 }
183
184 // Return whether this is the merge map for section SHNDX.
185
186 inline bool
187 Object_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
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)
203 {
204   Input_merge_map* map = this->get_input_merge_map(shndx);
205   gold_assert(map != NULL);
206
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
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
233 // Class Merge_map.
234
235 // Add a mapping for the bytes from OFFSET to OFFSET + LENGTH in input
236 // section SHNDX in object OBJECT to an OUTPUT_OFFSET in merged data
237 // in an output section.
238
239 void
240 Merge_map::add_mapping(Relobj* object, unsigned int shndx,
241                        section_offset_type offset, section_size_type length,
242                        section_offset_type output_offset)
243 {
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);
252 }
253
254 // Return the output offset for an input address.  The input address
255 // is at offset OFFSET in section SHNDX in OBJECT.  This sets
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.
259
260 bool
261 Merge_map::get_output_offset(const Relobj* object, unsigned int shndx,
262                              section_offset_type offset,
263                              section_offset_type* output_offset) const
264 {
265   Object_merge_map* object_merge_map = object->merge_map();
266   if (object_merge_map == NULL)
267     return false;
268   return object_merge_map->get_output_offset(this, shndx, offset,
269                                              output_offset);
270 }
271
272 // Return whether this is the merge section for SHNDX in OBJECT.
273
274 bool
275 Merge_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
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
289 bool
290 Output_merge_base::do_output_offset(const Relobj* object,
291                                     unsigned int shndx,
292                                     section_offset_type offset,
293                                     section_offset_type* poutput) const
294 {
295   return this->merge_map_.get_output_offset(object, shndx, offset, poutput);
296 }
297
298 // Return whether this is the merge section for SHNDX in OBJECT.
299
300 bool
301 Output_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
307 // Record a merged input section for script processing.
308
309 void
310 Output_merge_base::record_input_section(Relobj* relobj, unsigned int shndx)
311 {
312   gold_assert(this->keeps_input_sections_ && relobj != NULL);
313   // If this is the first input section, record it.  We need do this because
314   // this->input_sections_ is unordered.
315   if (this->first_relobj_ == NULL)
316     {
317       this->first_relobj_ = relobj;
318       this->first_shndx_ = shndx;
319     }
320
321   std::pair<Input_sections::iterator, bool> result =
322     this->input_sections_.insert(Section_id(relobj, shndx));
323   // We should insert a merge section once only.
324   gold_assert(result.second);
325 }
326
327 // Class Output_merge_data.
328
329 // Compute the hash code for a fixed-size constant.
330
331 size_t
332 Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
333 {
334   const unsigned char* p = this->pomd_->constant(k);
335   section_size_type entsize =
336     convert_to_section_size_type(this->pomd_->entsize());
337
338   // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
339   if (sizeof(size_t) == 8)
340     {
341       size_t result = static_cast<size_t>(14695981039346656037ULL);
342       for (section_size_type i = 0; i < entsize; ++i)
343         {
344           result &= (size_t) *p++;
345           result *= 1099511628211ULL;
346         }
347       return result;
348     }
349   else
350     {
351       size_t result = 2166136261UL;
352       for (section_size_type i = 0; i < entsize; ++i)
353         {
354           result ^= (size_t) *p++;
355           result *= 16777619UL;
356         }
357       return result;
358     }
359 }
360
361 // Return whether one hash table key equals another.
362
363 bool
364 Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
365                                              Merge_data_key k2) const
366 {
367   const unsigned char* p1 = this->pomd_->constant(k1);
368   const unsigned char* p2 = this->pomd_->constant(k2);
369   return memcmp(p1, p2, this->pomd_->entsize()) == 0;
370 }
371
372 // Add a constant to the end of the section contents.
373
374 void
375 Output_merge_data::add_constant(const unsigned char* p)
376 {
377   section_size_type entsize = convert_to_section_size_type(this->entsize());
378   section_size_type addralign =
379     convert_to_section_size_type(this->addralign());
380   section_size_type addsize = std::max(entsize, addralign);
381   if (this->len_ + addsize > this->alc_)
382     {
383       if (this->alc_ == 0)
384         this->alc_ = 128 * addsize;
385       else
386         this->alc_ *= 2;
387       this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
388       if (this->p_ == NULL)
389         gold_nomem();
390     }
391
392   memcpy(this->p_ + this->len_, p, entsize);
393   if (addsize > entsize)
394     memset(this->p_ + this->len_ + entsize, 0, addsize - entsize);
395   this->len_ += addsize;
396 }
397
398 // Add the input section SHNDX in OBJECT to a merged output section
399 // which holds fixed length constants.  Return whether we were able to
400 // handle the section; if not, it will be linked as usual without
401 // constant merging.
402
403 bool
404 Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
405 {
406   section_size_type len;
407   const unsigned char* p = object->section_contents(shndx, &len, false);
408
409   section_size_type entsize = convert_to_section_size_type(this->entsize());
410
411   if (len % entsize != 0)
412     return false;
413
414   this->input_count_ += len / entsize;
415
416   for (section_size_type i = 0; i < len; i += entsize, p += entsize)
417     {
418       // Add the constant to the section contents.  If we find that it
419       // is already in the hash table, we will remove it again.
420       Merge_data_key k = this->len_;
421       this->add_constant(p);
422
423       std::pair<Merge_data_hashtable::iterator, bool> ins =
424         this->hashtable_.insert(k);
425
426       if (!ins.second)
427         {
428           // Key was already present.  Remove the copy we just added.
429           this->len_ -= entsize;
430           k = *ins.first;
431         }
432
433       // Record the offset of this constant in the output section.
434       this->add_mapping(object, shndx, i, entsize, k);
435     }
436
437   // For script processing, we keep the input sections.
438   if (this->keeps_input_sections())
439     record_input_section(object, shndx);
440
441   return true;
442 }
443
444 // Set the final data size in a merged output section with fixed size
445 // constants.
446
447 void
448 Output_merge_data::set_final_data_size()
449 {
450   // Release the memory we don't need.
451   this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
452   // An Output_merge_data object may be empty and realloc is allowed
453   // to return a NULL pointer in this case.  An Output_merge_data is empty
454   // if all its input sections have sizes that are not multiples of entsize.
455   gold_assert(this->p_ != NULL || this->len_ == 0);
456   this->set_data_size(this->len_);
457 }
458
459 // Write the data of a merged output section with fixed size constants
460 // to the file.
461
462 void
463 Output_merge_data::do_write(Output_file* of)
464 {
465   of->write(this->offset(), this->p_, this->len_);
466 }
467
468 // Write the data to a buffer.
469
470 void
471 Output_merge_data::do_write_to_buffer(unsigned char* buffer)
472 {
473   memcpy(buffer, this->p_, this->len_);
474 }
475
476 // Print merge stats to stderr.
477
478 void
479 Output_merge_data::do_print_merge_stats(const char* section_name)
480 {
481   fprintf(stderr,
482           _("%s: %s merged constants size: %lu; input: %zu; output: %zu\n"),
483           program_name, section_name,
484           static_cast<unsigned long>(this->entsize()),
485           this->input_count_, this->hashtable_.size());
486 }
487
488 // Class Output_merge_string.
489
490 // Add an input section to a merged string section.
491
492 template<typename Char_type>
493 bool
494 Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
495                                                      unsigned int shndx)
496 {
497   section_size_type len;
498   const unsigned char* pdata = object->section_contents(shndx, &len, false);
499
500   const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
501   const Char_type* pend = p + len / sizeof(Char_type);
502
503   if (len % sizeof(Char_type) != 0)
504     {
505       object->error(_("mergeable string section length not multiple of "
506                       "character size"));
507       return false;
508     }
509
510   size_t count = 0;
511
512   // The index I is in bytes, not characters.
513   section_size_type i = 0;
514   while (i < len)
515     {
516       const Char_type* pl;
517       for (pl = p; *pl != 0; ++pl)
518         {
519           if (pl >= pend)
520             {
521               gold_warning(_("%s: last entry in mergeable string section '%s' "
522                              "not null terminated"),
523                            object->name().c_str(),
524                            object->section_name(shndx).c_str());
525               break;
526             }
527         }
528
529       Stringpool::Key key;
530       const Char_type* str = this->stringpool_.add_with_length(p, pl - p, true,
531                                                                &key);
532
533       section_size_type bytelen_with_null = ((pl - p) + 1) * sizeof(Char_type);
534       this->merged_strings_.push_back(Merged_string(object, shndx, i, str,
535                                                     bytelen_with_null, key));
536
537       p = pl + 1;
538       i += bytelen_with_null;
539       ++count;
540     }
541
542   this->input_count_ += count;
543
544   // For script processing, we keep the input sections.
545   if (this->keeps_input_sections())
546     record_input_section(object, shndx);
547
548   return true;
549 }
550
551 // Finalize the mappings from the input sections to the output
552 // section, and return the final data size.
553
554 template<typename Char_type>
555 section_size_type
556 Output_merge_string<Char_type>::finalize_merged_data()
557 {
558   this->stringpool_.set_string_offsets();
559
560   for (typename Merged_strings::const_iterator p =
561          this->merged_strings_.begin();
562        p != this->merged_strings_.end();
563        ++p)
564     {
565       section_offset_type offset =
566         this->stringpool_.get_offset_from_key(p->stringpool_key);
567       this->add_mapping(p->object, p->shndx, p->offset, p->length, offset);
568     }
569
570   // Save some memory.  This also ensures that this function will work
571   // if called twice, as may happen if Layout::set_segment_offsets
572   // finds a better alignment.
573   this->merged_strings_.clear();
574
575   return this->stringpool_.get_strtab_size();
576 }
577
578 template<typename Char_type>
579 void
580 Output_merge_string<Char_type>::set_final_data_size()
581 {
582   const off_t final_data_size = this->finalize_merged_data();
583   this->set_data_size(final_data_size);
584 }
585
586 // Write out a merged string section.
587
588 template<typename Char_type>
589 void
590 Output_merge_string<Char_type>::do_write(Output_file* of)
591 {
592   this->stringpool_.write(of, this->offset());
593 }
594
595 // Write a merged string section to a buffer.
596
597 template<typename Char_type>
598 void
599 Output_merge_string<Char_type>::do_write_to_buffer(unsigned char* buffer)
600 {
601   this->stringpool_.write_to_buffer(buffer, this->data_size());
602 }
603
604 // Return the name of the types of string to use with
605 // do_print_merge_stats.
606
607 template<typename Char_type>
608 const char*
609 Output_merge_string<Char_type>::string_name()
610 {
611   gold_unreachable();
612   return NULL;
613 }
614
615 template<>
616 const char*
617 Output_merge_string<char>::string_name()
618 {
619   return "strings";
620 }
621
622 template<>
623 const char*
624 Output_merge_string<uint16_t>::string_name()
625 {
626   return "16-bit strings";
627 }
628
629 template<>
630 const char*
631 Output_merge_string<uint32_t>::string_name()
632 {
633   return "32-bit strings";
634 }
635
636 // Print merge stats to stderr.
637
638 template<typename Char_type>
639 void
640 Output_merge_string<Char_type>::do_print_merge_stats(const char* section_name)
641 {
642   char buf[200];
643   snprintf(buf, sizeof buf, "%s merged %s", section_name, this->string_name());
644   fprintf(stderr, _("%s: %s input: %zu\n"),
645           program_name, buf, this->input_count_);
646   this->stringpool_.print_stats(buf);
647 }
648
649 // Instantiate the templates we need.
650
651 template
652 class Output_merge_string<char>;
653
654 template
655 class Output_merge_string<uint16_t>;
656
657 template
658 class Output_merge_string<uint32_t>;
659
660 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
661 template
662 void
663 Object_merge_map::initialize_input_to_output_map<32>(
664     unsigned int shndx,
665     elfcpp::Elf_types<32>::Elf_Addr starting_address,
666     Unordered_map<section_offset_type, elfcpp::Elf_types<32>::Elf_Addr>*);
667 #endif
668
669 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
670 template
671 void
672 Object_merge_map::initialize_input_to_output_map<64>(
673     unsigned int shndx,
674     elfcpp::Elf_types<64>::Elf_Addr starting_address,
675     Unordered_map<section_offset_type, elfcpp::Elf_types<64>::Elf_Addr>*);
676 #endif
677
678 } // End namespace gold.