1 // merge.cc -- handle section merging for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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.
29 #include "compressed_output.h"
34 // Class Object_merge_map.
38 Object_merge_map::~Object_merge_map()
40 for (Section_merge_maps::iterator p
= this->section_merge_maps_
.begin();
41 p
!= this->section_merge_maps_
.end();
46 // Get the Input_merge_map to use for an input section, or NULL.
48 Object_merge_map::Input_merge_map
*
49 Object_merge_map::get_input_merge_map(unsigned int shndx
)
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())
62 // Get or create the Input_merge_map to use for an input section.
64 Object_merge_map::Input_merge_map
*
65 Object_merge_map::get_or_make_input_merge_map(const Merge_map
* merge_map
,
68 Input_merge_map
* map
= this->get_input_merge_map(shndx
);
71 // For a given input section in a given object, every mapping
72 // must be done with the same Merge_map.
73 gold_assert(map
->merge_map
== merge_map
);
77 // We need to create a new entry.
78 if (this->first_shnum_
== -1U)
80 this->first_shnum_
= shndx
;
81 this->first_map_
.merge_map
= merge_map
;
82 return &this->first_map_
;
84 if (this->second_shnum_
== -1U)
86 this->second_shnum_
= shndx
;
87 this->second_map_
.merge_map
= merge_map
;
88 return &this->second_map_
;
91 Input_merge_map
* new_map
= new Input_merge_map
;
92 new_map
->merge_map
= merge_map
;
93 this->section_merge_maps_
[shndx
] = new_map
;
100 Object_merge_map::add_mapping(const Merge_map
* merge_map
, unsigned int shndx
,
101 section_offset_type input_offset
,
102 section_size_type length
,
103 section_offset_type output_offset
)
105 Input_merge_map
* map
= this->get_or_make_input_merge_map(merge_map
, shndx
);
107 // Try to merge the new entry in the last one we saw.
108 if (!map
->entries
.empty())
110 Input_merge_entry
& entry(map
->entries
.back());
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
;
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
)
120 gold_assert(input_offset
< entry
.input_offset
);
121 gold_assert(input_offset_u
+ length
122 <= static_cast<section_size_type
>(entry
.input_offset
));
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
))
130 entry
.length
+= length
;
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
);
142 // Get the output offset for an input address.
145 Object_merge_map::get_output_offset(const Merge_map
* merge_map
,
147 section_offset_type input_offset
,
148 section_offset_type
* output_offset
)
150 Input_merge_map
* map
= this->get_input_merge_map(shndx
);
152 || (merge_map
!= NULL
&& map
->merge_map
!= merge_map
))
157 std::sort(map
->entries
.begin(), map
->entries
.end(),
158 Input_merge_compare());
162 Input_merge_entry entry
;
163 entry
.input_offset
= input_offset
;
164 std::vector
<Input_merge_entry
>::const_iterator p
=
165 std::lower_bound(map
->entries
.begin(), map
->entries
.end(),
166 entry
, Input_merge_compare());
167 if (p
== map
->entries
.end() || p
->input_offset
> input_offset
)
169 if (p
== map
->entries
.begin())
172 gold_assert(p
->input_offset
<= input_offset
);
175 if (input_offset
- p
->input_offset
176 >= static_cast<section_offset_type
>(p
->length
))
179 *output_offset
= p
->output_offset
;
180 if (*output_offset
!= -1)
181 *output_offset
+= (input_offset
- p
->input_offset
);
185 // Return whether this is the merge map for section SHNDX.
188 Object_merge_map::is_merge_section_for(const Merge_map
* merge_map
,
191 Input_merge_map
* map
= this->get_input_merge_map(shndx
);
192 return map
!= NULL
&& map
->merge_map
== merge_map
;
195 // Initialize a mapping from input offsets to output addresses.
199 Object_merge_map::initialize_input_to_output_map(
201 typename
elfcpp::Elf_types
<size
>::Elf_Addr starting_address
,
202 Unordered_map
<section_offset_type
,
203 typename
elfcpp::Elf_types
<size
>::Elf_Addr
>* initialize_map
)
205 Input_merge_map
* map
= this->get_input_merge_map(shndx
);
206 gold_assert(map
!= NULL
);
208 gold_assert(initialize_map
->empty());
209 // We know how many entries we are going to add.
210 // reserve_unordered_map takes an expected count of buckets, not a
211 // count of elements, so double it to try to reduce collisions.
212 reserve_unordered_map(initialize_map
, map
->entries
.size() * 2);
214 for (Input_merge_map::Entries::const_iterator p
= map
->entries
.begin();
215 p
!= map
->entries
.end();
218 section_offset_type output_offset
= p
->output_offset
;
219 if (output_offset
!= -1)
220 output_offset
+= starting_address
;
223 // If we see a relocation against an address we have chosen
224 // to discard, we relocate to zero. FIXME: We could also
225 // issue a warning in this case; that would require
226 // reporting this somehow and checking it in the routines in
230 initialize_map
->insert(std::make_pair(p
->input_offset
, output_offset
));
236 // Add a mapping for the bytes from OFFSET to OFFSET + LENGTH in input
237 // section SHNDX in object OBJECT to an OUTPUT_OFFSET in merged data
238 // in an output section.
241 Merge_map::add_mapping(Relobj
* object
, unsigned int shndx
,
242 section_offset_type offset
, section_size_type length
,
243 section_offset_type output_offset
)
245 gold_assert(object
!= NULL
);
246 Object_merge_map
* object_merge_map
= object
->merge_map();
247 if (object_merge_map
== NULL
)
249 object_merge_map
= new Object_merge_map();
250 object
->set_merge_map(object_merge_map
);
253 object_merge_map
->add_mapping(this, shndx
, offset
, length
, output_offset
);
256 // Return the output offset for an input address. The input address
257 // is at offset OFFSET in section SHNDX in OBJECT. This sets
258 // *OUTPUT_OFFSET to the offset in the merged data in the output
259 // section. This returns true if the mapping is known, false
263 Merge_map::get_output_offset(const Relobj
* object
, unsigned int shndx
,
264 section_offset_type offset
,
265 section_offset_type
* output_offset
) const
267 Object_merge_map
* object_merge_map
= object
->merge_map();
268 if (object_merge_map
== NULL
)
270 return object_merge_map
->get_output_offset(this, shndx
, offset
,
274 // Return whether this is the merge section for SHNDX in OBJECT.
277 Merge_map::is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
279 Object_merge_map
* object_merge_map
= object
->merge_map();
280 if (object_merge_map
== NULL
)
282 return object_merge_map
->is_merge_section_for(this, shndx
);
285 // Class Output_merge_base.
287 // Return the output offset for an input offset. The input address is
288 // at offset OFFSET in section SHNDX in OBJECT. If we know the
289 // offset, set *POUTPUT and return true. Otherwise return false.
292 Output_merge_base::do_output_offset(const Relobj
* object
,
294 section_offset_type offset
,
295 section_offset_type
* poutput
) const
297 return this->merge_map_
.get_output_offset(object
, shndx
, offset
, poutput
);
300 // Return whether this is the merge section for SHNDX in OBJECT.
303 Output_merge_base::do_is_merge_section_for(const Relobj
* object
,
304 unsigned int shndx
) const
306 return this->merge_map_
.is_merge_section_for(object
, shndx
);
309 // Record a merged input section for script processing.
312 Output_merge_base::record_input_section(Relobj
* relobj
, unsigned int shndx
)
314 gold_assert(this->keeps_input_sections_
&& relobj
!= NULL
);
315 // If this is the first input section, record it. We need do this because
316 // this->input_sections_ is unordered.
317 if (this->first_relobj_
== NULL
)
319 this->first_relobj_
= relobj
;
320 this->first_shndx_
= shndx
;
323 std::pair
<Input_sections::iterator
, bool> result
=
324 this->input_sections_
.insert(Section_id(relobj
, shndx
));
325 // We should insert a merge section once only.
326 gold_assert(result
.second
);
329 // Class Output_merge_data.
331 // Compute the hash code for a fixed-size constant.
334 Output_merge_data::Merge_data_hash::operator()(Merge_data_key k
) const
336 const unsigned char* p
= this->pomd_
->constant(k
);
337 section_size_type entsize
=
338 convert_to_section_size_type(this->pomd_
->entsize());
340 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
341 if (sizeof(size_t) == 8)
343 size_t result
= static_cast<size_t>(14695981039346656037ULL);
344 for (section_size_type i
= 0; i
< entsize
; ++i
)
346 result
&= (size_t) *p
++;
347 result
*= 1099511628211ULL;
353 size_t result
= 2166136261UL;
354 for (section_size_type i
= 0; i
< entsize
; ++i
)
356 result
^= (size_t) *p
++;
357 result
*= 16777619UL;
363 // Return whether one hash table key equals another.
366 Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1
,
367 Merge_data_key k2
) const
369 const unsigned char* p1
= this->pomd_
->constant(k1
);
370 const unsigned char* p2
= this->pomd_
->constant(k2
);
371 return memcmp(p1
, p2
, this->pomd_
->entsize()) == 0;
374 // Add a constant to the end of the section contents.
377 Output_merge_data::add_constant(const unsigned char* p
)
379 section_size_type entsize
= convert_to_section_size_type(this->entsize());
380 section_size_type addralign
=
381 convert_to_section_size_type(this->addralign());
382 section_size_type addsize
= std::max(entsize
, addralign
);
383 if (this->len_
+ addsize
> this->alc_
)
386 this->alc_
= 128 * addsize
;
389 this->p_
= static_cast<unsigned char*>(realloc(this->p_
, this->alc_
));
390 if (this->p_
== NULL
)
394 memcpy(this->p_
+ this->len_
, p
, entsize
);
395 if (addsize
> entsize
)
396 memset(this->p_
+ this->len_
+ entsize
, 0, addsize
- entsize
);
397 this->len_
+= addsize
;
400 // Add the input section SHNDX in OBJECT to a merged output section
401 // which holds fixed length constants. Return whether we were able to
402 // handle the section; if not, it will be linked as usual without
406 Output_merge_data::do_add_input_section(Relobj
* object
, unsigned int shndx
)
408 section_size_type len
;
409 section_size_type uncompressed_size
= 0;
410 unsigned char* uncompressed_data
= NULL
;
411 const unsigned char* p
= object
->section_contents(shndx
, &len
, false);
413 if (object
->section_is_compressed(shndx
, &uncompressed_size
))
415 uncompressed_data
= new unsigned char[uncompressed_size
];
416 if (!decompress_input_section(p
, len
, uncompressed_data
,
418 object
->error(_("could not decompress section %s"),
419 object
->section_name(shndx
).c_str());
420 p
= uncompressed_data
;
421 len
= uncompressed_size
;
424 section_size_type entsize
= convert_to_section_size_type(this->entsize());
426 if (len
% entsize
!= 0)
428 if (uncompressed_data
!= NULL
)
429 delete[] uncompressed_data
;
433 this->input_count_
+= len
/ entsize
;
435 for (section_size_type i
= 0; i
< len
; i
+= entsize
, p
+= entsize
)
437 // Add the constant to the section contents. If we find that it
438 // is already in the hash table, we will remove it again.
439 Merge_data_key k
= this->len_
;
440 this->add_constant(p
);
442 std::pair
<Merge_data_hashtable::iterator
, bool> ins
=
443 this->hashtable_
.insert(k
);
447 // Key was already present. Remove the copy we just added.
448 this->len_
-= entsize
;
452 // Record the offset of this constant in the output section.
453 this->add_mapping(object
, shndx
, i
, entsize
, k
);
456 // For script processing, we keep the input sections.
457 if (this->keeps_input_sections())
458 record_input_section(object
, shndx
);
460 if (uncompressed_data
!= NULL
)
461 delete[] uncompressed_data
;
466 // Set the final data size in a merged output section with fixed size
470 Output_merge_data::set_final_data_size()
472 // Release the memory we don't need.
473 this->p_
= static_cast<unsigned char*>(realloc(this->p_
, this->len_
));
474 // An Output_merge_data object may be empty and realloc is allowed
475 // to return a NULL pointer in this case. An Output_merge_data is empty
476 // if all its input sections have sizes that are not multiples of entsize.
477 gold_assert(this->p_
!= NULL
|| this->len_
== 0);
478 this->set_data_size(this->len_
);
481 // Write the data of a merged output section with fixed size constants
485 Output_merge_data::do_write(Output_file
* of
)
487 of
->write(this->offset(), this->p_
, this->len_
);
490 // Write the data to a buffer.
493 Output_merge_data::do_write_to_buffer(unsigned char* buffer
)
495 memcpy(buffer
, this->p_
, this->len_
);
498 // Print merge stats to stderr.
501 Output_merge_data::do_print_merge_stats(const char* section_name
)
504 _("%s: %s merged constants size: %lu; input: %zu; output: %zu\n"),
505 program_name
, section_name
,
506 static_cast<unsigned long>(this->entsize()),
507 this->input_count_
, this->hashtable_
.size());
510 // Class Output_merge_string.
512 // Add an input section to a merged string section.
514 template<typename Char_type
>
516 Output_merge_string
<Char_type
>::do_add_input_section(Relobj
* object
,
519 section_size_type len
;
520 section_size_type uncompressed_size
= 0;
521 unsigned char* uncompressed_data
= NULL
;
522 const unsigned char* pdata
= object
->section_contents(shndx
, &len
, false);
524 if (object
->section_is_compressed(shndx
, &uncompressed_size
))
526 uncompressed_data
= new unsigned char[uncompressed_size
];
527 if (!decompress_input_section(pdata
, len
, uncompressed_data
,
529 object
->error(_("could not decompress section %s"),
530 object
->section_name(shndx
).c_str());
531 pdata
= uncompressed_data
;
532 len
= uncompressed_size
;
535 const Char_type
* p
= reinterpret_cast<const Char_type
*>(pdata
);
536 const Char_type
* pend
= p
+ len
/ sizeof(Char_type
);
537 const Char_type
* pend0
= pend
;
539 if (len
% sizeof(Char_type
) != 0)
541 object
->error(_("mergeable string section length not multiple of "
543 if (uncompressed_data
!= NULL
)
544 delete[] uncompressed_data
;
550 gold_warning(_("%s: last entry in mergeable string section '%s' "
551 "not null terminated"),
552 object
->name().c_str(),
553 object
->section_name(shndx
).c_str());
554 // Find the end of the last NULL-terminated string in the buffer.
555 while (pend0
> p
&& pend0
[-1] != 0)
559 Merged_strings_list
* merged_strings_list
=
560 new Merged_strings_list(object
, shndx
);
561 this->merged_strings_lists_
.push_back(merged_strings_list
);
562 Merged_strings
& merged_strings
= merged_strings_list
->merged_strings
;
564 // Count the number of strings in the section and size the list.
566 for (const Char_type
* pt
= p
; pt
< pend0
; pt
+= string_length(pt
) + 1)
570 merged_strings
.reserve(count
+ 1);
572 // The index I is in bytes, not characters.
573 section_size_type i
= 0;
576 size_t len
= string_length(p
);
579 this->stringpool_
.add_with_length(p
, len
, true, &key
);
581 merged_strings
.push_back(Merged_string(i
, key
));
584 i
+= (len
+ 1) * sizeof(Char_type
);
588 size_t len
= pend
- p
;
591 this->stringpool_
.add_with_length(p
, len
, true, &key
);
593 merged_strings
.push_back(Merged_string(i
, key
));
595 i
+= (len
+ 1) * sizeof(Char_type
);
598 // Record the last offset in the input section so that we can
599 // compute the length of the last string.
600 merged_strings
.push_back(Merged_string(i
, 0));
602 this->input_count_
+= count
;
603 this->input_size_
+= len
;
605 // For script processing, we keep the input sections.
606 if (this->keeps_input_sections())
607 record_input_section(object
, shndx
);
609 if (uncompressed_data
!= NULL
)
610 delete[] uncompressed_data
;
615 // Finalize the mappings from the input sections to the output
616 // section, and return the final data size.
618 template<typename Char_type
>
620 Output_merge_string
<Char_type
>::finalize_merged_data()
622 this->stringpool_
.set_string_offsets();
624 for (typename
Merged_strings_lists::const_iterator l
=
625 this->merged_strings_lists_
.begin();
626 l
!= this->merged_strings_lists_
.end();
629 section_offset_type last_input_offset
= 0;
630 section_offset_type last_output_offset
= 0;
631 for (typename
Merged_strings::const_iterator p
=
632 (*l
)->merged_strings
.begin();
633 p
!= (*l
)->merged_strings
.end();
636 section_size_type length
= p
->offset
- last_input_offset
;
638 this->add_mapping((*l
)->object
, (*l
)->shndx
, last_input_offset
,
639 length
, last_output_offset
);
640 last_input_offset
= p
->offset
;
641 if (p
->stringpool_key
!= 0)
643 this->stringpool_
.get_offset_from_key(p
->stringpool_key
);
648 // Save some memory. This also ensures that this function will work
649 // if called twice, as may happen if Layout::set_segment_offsets
650 // finds a better alignment.
651 this->merged_strings_lists_
.clear();
653 return this->stringpool_
.get_strtab_size();
656 template<typename Char_type
>
658 Output_merge_string
<Char_type
>::set_final_data_size()
660 const off_t final_data_size
= this->finalize_merged_data();
661 this->set_data_size(final_data_size
);
664 // Write out a merged string section.
666 template<typename Char_type
>
668 Output_merge_string
<Char_type
>::do_write(Output_file
* of
)
670 this->stringpool_
.write(of
, this->offset());
673 // Write a merged string section to a buffer.
675 template<typename Char_type
>
677 Output_merge_string
<Char_type
>::do_write_to_buffer(unsigned char* buffer
)
679 this->stringpool_
.write_to_buffer(buffer
, this->data_size());
682 // Return the name of the types of string to use with
683 // do_print_merge_stats.
685 template<typename Char_type
>
687 Output_merge_string
<Char_type
>::string_name()
695 Output_merge_string
<char>::string_name()
702 Output_merge_string
<uint16_t>::string_name()
704 return "16-bit strings";
709 Output_merge_string
<uint32_t>::string_name()
711 return "32-bit strings";
714 // Print merge stats to stderr.
716 template<typename Char_type
>
718 Output_merge_string
<Char_type
>::do_print_merge_stats(const char* section_name
)
721 snprintf(buf
, sizeof buf
, "%s merged %s", section_name
, this->string_name());
722 fprintf(stderr
, _("%s: %s input bytes: %zu\n"),
723 program_name
, buf
, this->input_size_
);
724 fprintf(stderr
, _("%s: %s input strings: %zu\n"),
725 program_name
, buf
, this->input_count_
);
726 this->stringpool_
.print_stats(buf
);
729 // Instantiate the templates we need.
732 class Output_merge_string
<char>;
735 class Output_merge_string
<uint16_t>;
738 class Output_merge_string
<uint32_t>;
740 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
743 Object_merge_map::initialize_input_to_output_map
<32>(
745 elfcpp::Elf_types
<32>::Elf_Addr starting_address
,
746 Unordered_map
<section_offset_type
, elfcpp::Elf_types
<32>::Elf_Addr
>*);
749 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
752 Object_merge_map::initialize_input_to_output_map
<64>(
754 elfcpp::Elf_types
<64>::Elf_Addr starting_address
,
755 Unordered_map
<section_offset_type
, elfcpp::Elf_types
<64>::Elf_Addr
>*);
758 } // End namespace gold.