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.
33 // Class Object_merge_map.
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();
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())
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
,
67 Input_merge_map
* map
= this->get_input_merge_map(shndx
);
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
);
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
;
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
));
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
;
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.
144 Object_merge_map::get_output_offset(const Merge_map
* merge_map
,
146 section_offset_type input_offset
,
147 section_offset_type
*output_offset
)
149 Input_merge_map
* map
= this->get_input_merge_map(shndx
);
151 || (merge_map
!= NULL
&& map
->merge_map
!= merge_map
))
156 std::sort(map
->entries
.begin(), map
->entries
.end(),
157 Input_merge_compare());
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())
171 gold_assert(p
->input_offset
<= input_offset
);
174 if (input_offset
- p
->input_offset
175 >= static_cast<section_offset_type
>(p
->length
))
178 *output_offset
= p
->output_offset
;
179 if (*output_offset
!= -1)
180 *output_offset
+= (input_offset
- p
->input_offset
);
184 // Return whether this is the merge map for section SHNDX.
187 Object_merge_map::is_merge_section_for(const Merge_map
* merge_map
,
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.
198 Object_merge_map::initialize_input_to_output_map(
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 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);
213 for (Input_merge_map::Entries::const_iterator p
= map
->entries
.begin();
214 p
!= map
->entries
.end();
217 section_offset_type output_offset
= p
->output_offset
;
218 if (output_offset
!= -1)
219 output_offset
+= starting_address
;
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
229 initialize_map
->insert(std::make_pair(p
->input_offset
, output_offset
));
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.
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
)
244 Object_merge_map
* object_merge_map
= object
->merge_map();
245 if (object_merge_map
== NULL
)
247 object_merge_map
= new Object_merge_map();
248 object
->set_merge_map(object_merge_map
);
251 object_merge_map
->add_mapping(this, shndx
, offset
, length
, output_offset
);
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
261 Merge_map::get_output_offset(const Relobj
* object
, unsigned int shndx
,
262 section_offset_type offset
,
263 section_offset_type
* output_offset
) const
265 Object_merge_map
* object_merge_map
= object
->merge_map();
266 if (object_merge_map
== NULL
)
268 return object_merge_map
->get_output_offset(this, shndx
, offset
,
272 // Return whether this is the merge section for SHNDX in OBJECT.
275 Merge_map::is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
277 Object_merge_map
* object_merge_map
= object
->merge_map();
278 if (object_merge_map
== NULL
)
280 return object_merge_map
->is_merge_section_for(this, shndx
);
283 // Class Output_merge_base.
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.
290 Output_merge_base::do_output_offset(const Relobj
* object
,
292 section_offset_type offset
,
293 section_offset_type
* poutput
) const
295 return this->merge_map_
.get_output_offset(object
, shndx
, offset
, poutput
);
298 // Return whether this is the merge section for SHNDX in OBJECT.
301 Output_merge_base::do_is_merge_section_for(const Relobj
* object
,
302 unsigned int shndx
) const
304 return this->merge_map_
.is_merge_section_for(object
, shndx
);
307 // Class Output_merge_data.
309 // Compute the hash code for a fixed-size constant.
312 Output_merge_data::Merge_data_hash::operator()(Merge_data_key k
) const
314 const unsigned char* p
= this->pomd_
->constant(k
);
315 section_size_type entsize
=
316 convert_to_section_size_type(this->pomd_
->entsize());
318 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
319 if (sizeof(size_t) == 8)
321 size_t result
= static_cast<size_t>(14695981039346656037ULL);
322 for (section_size_type i
= 0; i
< entsize
; ++i
)
324 result
&= (size_t) *p
++;
325 result
*= 1099511628211ULL;
331 size_t result
= 2166136261UL;
332 for (section_size_type i
= 0; i
< entsize
; ++i
)
334 result
^= (size_t) *p
++;
335 result
*= 16777619UL;
341 // Return whether one hash table key equals another.
344 Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1
,
345 Merge_data_key k2
) const
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;
352 // Add a constant to the end of the section contents.
355 Output_merge_data::add_constant(const unsigned char* p
)
357 section_size_type entsize
= convert_to_section_size_type(this->entsize());
358 section_size_type addralign
=
359 convert_to_section_size_type(this->addralign());
360 section_size_type addsize
= std::max(entsize
, addralign
);
361 if (this->len_
+ addsize
> this->alc_
)
364 this->alc_
= 128 * addsize
;
367 this->p_
= static_cast<unsigned char*>(realloc(this->p_
, this->alc_
));
368 if (this->p_
== NULL
)
372 memcpy(this->p_
+ this->len_
, p
, entsize
);
373 if (addsize
> entsize
)
374 memset(this->p_
+ this->len_
+ entsize
, 0, addsize
- entsize
);
375 this->len_
+= addsize
;
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
384 Output_merge_data::do_add_input_section(Relobj
* object
, unsigned int shndx
)
386 section_size_type len
;
387 const unsigned char* p
= object
->section_contents(shndx
, &len
, false);
389 section_size_type entsize
= convert_to_section_size_type(this->entsize());
391 if (len
% entsize
!= 0)
394 this->input_count_
+= len
/ entsize
;
396 for (section_size_type i
= 0; i
< len
; i
+= entsize
, p
+= entsize
)
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
);
403 std::pair
<Merge_data_hashtable::iterator
, bool> ins
=
404 this->hashtable_
.insert(k
);
408 // Key was already present. Remove the copy we just added.
409 this->len_
-= entsize
;
413 // Record the offset of this constant in the output section.
414 this->add_mapping(object
, shndx
, i
, entsize
, k
);
420 // Set the final data size in a merged output section with fixed size
424 Output_merge_data::set_final_data_size()
426 // Release the memory we don't need.
427 this->p_
= static_cast<unsigned char*>(realloc(this->p_
, this->len_
));
428 // An Output_merge_data object may be empty and realloc is allowed
429 // to return a NULL pointer in this case. An Output_merge_data is empty
430 // if all its input sections have sizes that are not multiples of entsize.
431 gold_assert(this->p_
!= NULL
|| this->len_
== 0);
432 this->set_data_size(this->len_
);
435 // Write the data of a merged output section with fixed size constants
439 Output_merge_data::do_write(Output_file
* of
)
441 of
->write(this->offset(), this->p_
, this->len_
);
444 // Write the data to a buffer.
447 Output_merge_data::do_write_to_buffer(unsigned char* buffer
)
449 memcpy(buffer
, this->p_
, this->len_
);
452 // Print merge stats to stderr.
455 Output_merge_data::do_print_merge_stats(const char* section_name
)
458 _("%s: %s merged constants size: %lu; input: %zu; output: %zu\n"),
459 program_name
, section_name
,
460 static_cast<unsigned long>(this->entsize()),
461 this->input_count_
, this->hashtable_
.size());
464 // Class Output_merge_string.
466 // Add an input section to a merged string section.
468 template<typename Char_type
>
470 Output_merge_string
<Char_type
>::do_add_input_section(Relobj
* object
,
473 section_size_type len
;
474 const unsigned char* pdata
= object
->section_contents(shndx
, &len
, false);
476 const Char_type
* p
= reinterpret_cast<const Char_type
*>(pdata
);
477 const Char_type
* pend
= p
+ len
/ sizeof(Char_type
);
479 if (len
% sizeof(Char_type
) != 0)
481 object
->error(_("mergeable string section length not multiple of "
488 // The index I is in bytes, not characters.
489 section_size_type i
= 0;
493 for (pl
= p
; *pl
!= 0; ++pl
)
497 gold_warning(_("%s: last entry in mergeable string section '%s' "
498 "not null terminated"),
499 object
->name().c_str(),
500 object
->section_name(shndx
).c_str());
506 const Char_type
* str
= this->stringpool_
.add_with_length(p
, pl
- p
, true,
509 section_size_type bytelen_with_null
= ((pl
- p
) + 1) * sizeof(Char_type
);
510 this->merged_strings_
.push_back(Merged_string(object
, shndx
, i
, str
,
511 bytelen_with_null
, key
));
514 i
+= bytelen_with_null
;
518 this->input_count_
+= count
;
523 // Finalize the mappings from the input sections to the output
524 // section, and return the final data size.
526 template<typename Char_type
>
528 Output_merge_string
<Char_type
>::finalize_merged_data()
530 this->stringpool_
.set_string_offsets();
532 for (typename
Merged_strings::const_iterator p
=
533 this->merged_strings_
.begin();
534 p
!= this->merged_strings_
.end();
537 section_offset_type offset
=
538 this->stringpool_
.get_offset_from_key(p
->stringpool_key
);
539 this->add_mapping(p
->object
, p
->shndx
, p
->offset
, p
->length
, offset
);
542 // Save some memory. This also ensures that this function will work
543 // if called twice, as may happen if Layout::set_segment_offsets
544 // finds a better alignment.
545 this->merged_strings_
.clear();
547 return this->stringpool_
.get_strtab_size();
550 template<typename Char_type
>
552 Output_merge_string
<Char_type
>::set_final_data_size()
554 const off_t final_data_size
= this->finalize_merged_data();
555 this->set_data_size(final_data_size
);
558 // Write out a merged string section.
560 template<typename Char_type
>
562 Output_merge_string
<Char_type
>::do_write(Output_file
* of
)
564 this->stringpool_
.write(of
, this->offset());
567 // Write a merged string section to a buffer.
569 template<typename Char_type
>
571 Output_merge_string
<Char_type
>::do_write_to_buffer(unsigned char* buffer
)
573 this->stringpool_
.write_to_buffer(buffer
, this->data_size());
576 // Return the name of the types of string to use with
577 // do_print_merge_stats.
579 template<typename Char_type
>
581 Output_merge_string
<Char_type
>::string_name()
589 Output_merge_string
<char>::string_name()
596 Output_merge_string
<uint16_t>::string_name()
598 return "16-bit strings";
603 Output_merge_string
<uint32_t>::string_name()
605 return "32-bit strings";
608 // Print merge stats to stderr.
610 template<typename Char_type
>
612 Output_merge_string
<Char_type
>::do_print_merge_stats(const char* section_name
)
615 snprintf(buf
, sizeof buf
, "%s merged %s", section_name
, this->string_name());
616 fprintf(stderr
, _("%s: %s input: %zu\n"),
617 program_name
, buf
, this->input_count_
);
618 this->stringpool_
.print_stats(buf
);
621 // Instantiate the templates we need.
624 class Output_merge_string
<char>;
627 class Output_merge_string
<uint16_t>;
630 class Output_merge_string
<uint32_t>;
632 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
635 Object_merge_map::initialize_input_to_output_map
<32>(
637 elfcpp::Elf_types
<32>::Elf_Addr starting_address
,
638 Unordered_map
<section_offset_type
, elfcpp::Elf_types
<32>::Elf_Addr
>*);
641 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
644 Object_merge_map::initialize_input_to_output_map
<64>(
646 elfcpp::Elf_types
<64>::Elf_Addr starting_address
,
647 Unordered_map
<section_offset_type
, elfcpp::Elf_types
<64>::Elf_Addr
>*);
650 } // End namespace gold.