1 // merge.cc -- handle section merging for gold
3 // Copyright 2006, 2007 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 // For each object with merge sections, we store an Object_merge_map.
34 // This is used to map locations in input sections to a merged output
35 // section. The output section itself is not recorded here--it can be
36 // found in the map_to_output_ field of the Object.
38 class Object_merge_map
42 : first_shnum_(-1U), first_map_(),
43 second_shnum_(-1U), second_map_(),
49 // Add a mapping for MERGE_MAP, for the bytes from OFFSET to OFFSET
50 // + LENGTH in the input section SHNDX to OUTPUT_OFFSET in the
51 // output section. An OUTPUT_OFFSET of -1 means that the bytes are
52 // discarded. OUTPUT_OFFSET is relative to the start of the merged
53 // data in the output section.
55 add_mapping(const Merge_map
*, unsigned int shndx
, section_offset_type offset
,
56 section_size_type length
, section_offset_type output_offset
);
58 // Get the output offset for an input address in MERGE_MAP. The
59 // input address is at offset OFFSET in section SHNDX. This sets
60 // *OUTPUT_OFFSET to the offset in the output section; this will be
61 // -1 if the bytes are not being copied to the output. This returns
62 // true if the mapping is known, false otherwise. *OUTPUT_OFFSET is
63 // relative to the start of the merged data in the output section.
65 get_output_offset(const Merge_map
*, unsigned int shndx
,
66 section_offset_type offset
,
67 section_offset_type
*output_offset
);
70 // Map input section offsets to a length and an output section
71 // offset. An output section offset of -1 means that this part of
72 // the input section is being discarded.
73 struct Input_merge_entry
75 // The offset in the input section.
76 section_offset_type input_offset
;
78 section_size_type length
;
79 // The offset in the output section.
80 section_offset_type output_offset
;
83 // A less-than comparison routine for Input_merge_entry.
84 struct Input_merge_compare
87 operator()(const Input_merge_entry
& i1
, const Input_merge_entry
& i2
) const
88 { return i1
.input_offset
< i2
.input_offset
; }
91 // A list of entries for a particular input section.
92 struct Input_merge_map
94 // We store these with the Relobj, and we look them up by input
95 // section. It is possible to have two different merge maps
96 // associated with a single output section. For example, this
97 // happens routinely with .rodata, when merged string constants
98 // and merged fixed size constants are both put into .rodata. The
99 // output offset that we store is not the offset from the start of
100 // the output section; it is the offset from the start of the
101 // merged data in the output section. That means that the caller
102 // is going to add the offset of the merged data within the output
103 // section, which means that the caller needs to know which set of
104 // merged data it found the entry in. So it's not enough to find
105 // this data based on the input section and the output section; we
106 // also have to find it based on a set of merged data in the
107 // output section. In order to verify that we are looking at the
108 // right data, we store a pointer to the Merge_map here, and we
109 // pass in a pointer when looking at the data. If we are asked to
110 // look up information for a different Merge_map, we report that
111 // we don't have it, rather than trying a lookup and returning an
112 // answer which will receive the wrong offset.
113 const Merge_map
* merge_map
;
114 // The list of mappings.
115 std::vector
<Input_merge_entry
> entries
;
116 // Whether the ENTRIES field is sorted by input_offset.
120 : merge_map(NULL
), entries(), sorted(true)
124 // Map input section indices to merge maps.
125 typedef std::map
<unsigned int, Input_merge_map
*> Section_merge_maps
;
127 // Return a pointer to the Input_merge_map to use for the input
128 // section SHNDX, or NULL.
130 get_input_merge_map(unsigned int shndx
);
132 // Get or make the the Input_merge_map to use for the section SHNDX
135 get_or_make_input_merge_map(const Merge_map
* merge_map
, unsigned int shndx
);
137 // Any given object file will normally only have a couple of input
138 // sections with mergeable contents. So we keep the first two input
139 // section numbers inline, and push any further ones into a map. A
140 // value of -1U in first_shnum_ or second_shnum_ means that we don't
141 // have a corresponding entry.
142 unsigned int first_shnum_
;
143 Input_merge_map first_map_
;
144 unsigned int second_shnum_
;
145 Input_merge_map second_map_
;
146 Section_merge_maps section_merge_maps_
;
151 Object_merge_map::~Object_merge_map()
153 for (Section_merge_maps::iterator p
= this->section_merge_maps_
.begin();
154 p
!= this->section_merge_maps_
.end();
159 // Get the Input_merge_map to use for an input section, or NULL.
161 Object_merge_map::Input_merge_map
*
162 Object_merge_map::get_input_merge_map(unsigned int shndx
)
164 gold_assert(shndx
!= -1U);
165 if (shndx
== this->first_shnum_
)
166 return &this->first_map_
;
167 if (shndx
== this->second_shnum_
)
168 return &this->second_map_
;
169 Section_merge_maps::const_iterator p
= this->section_merge_maps_
.find(shndx
);
170 if (p
!= this->section_merge_maps_
.end())
175 // Get or create the Input_merge_map to use for an input section.
177 Object_merge_map::Input_merge_map
*
178 Object_merge_map::get_or_make_input_merge_map(const Merge_map
* merge_map
,
181 Input_merge_map
* map
= this->get_input_merge_map(shndx
);
184 // For a given input section in a given object, every mapping
185 // must be done with the same Merge_map.
186 gold_assert(map
->merge_map
== merge_map
);
190 // We need to create a new entry.
191 if (this->first_shnum_
== -1U)
193 this->first_shnum_
= shndx
;
194 this->first_map_
.merge_map
= merge_map
;
195 return &this->first_map_
;
197 if (this->second_shnum_
== -1U)
199 this->second_shnum_
= shndx
;
200 this->second_map_
.merge_map
= merge_map
;
201 return &this->second_map_
;
204 Input_merge_map
* new_map
= new Input_merge_map
;
205 new_map
->merge_map
= merge_map
;
206 this->section_merge_maps_
[shndx
] = new_map
;
213 Object_merge_map::add_mapping(const Merge_map
* merge_map
, unsigned int shndx
,
214 section_offset_type input_offset
,
215 section_size_type length
,
216 section_offset_type output_offset
)
218 Input_merge_map
* map
= this->get_or_make_input_merge_map(merge_map
, shndx
);
220 // Try to merge the new entry in the last one we saw.
221 if (!map
->entries
.empty())
223 Input_merge_entry
& entry(map
->entries
.back());
225 // Use section_size_type to avoid signed/unsigned warnings.
226 section_size_type input_offset_u
= input_offset
;
227 section_size_type output_offset_u
= output_offset
;
229 // If this entry is not in order, we need to sort the vector
230 // before looking anything up.
231 if (input_offset_u
< entry
.input_offset
+ entry
.length
)
233 gold_assert(input_offset
< entry
.input_offset
);
234 gold_assert(input_offset_u
+ length
235 <= static_cast<section_size_type
>(entry
.input_offset
));
238 else if (entry
.input_offset
+ entry
.length
== input_offset_u
239 && (output_offset
== -1
240 ? entry
.output_offset
== -1
241 : entry
.output_offset
+ entry
.length
== output_offset_u
))
243 entry
.length
+= length
;
248 Input_merge_entry entry
;
249 entry
.input_offset
= input_offset
;
250 entry
.length
= length
;
251 entry
.output_offset
= output_offset
;
252 map
->entries
.push_back(entry
);
255 // Get the output offset for an input address.
258 Object_merge_map::get_output_offset(const Merge_map
* merge_map
,
260 section_offset_type input_offset
,
261 section_offset_type
*output_offset
)
263 Input_merge_map
* map
= this->get_input_merge_map(shndx
);
264 if (map
== NULL
|| map
->merge_map
!= merge_map
)
269 std::sort(map
->entries
.begin(), map
->entries
.end(),
270 Input_merge_compare());
274 Input_merge_entry entry
;
275 entry
.input_offset
= input_offset
;
276 std::vector
<Input_merge_entry
>::const_iterator p
=
277 std::lower_bound(map
->entries
.begin(), map
->entries
.end(),
278 entry
, Input_merge_compare());
279 if (p
== map
->entries
.end() || p
->input_offset
> input_offset
)
281 if (p
== map
->entries
.begin())
284 gold_assert(p
->input_offset
<= input_offset
);
287 if (input_offset
- p
->input_offset
288 >= static_cast<section_offset_type
>(p
->length
))
291 *output_offset
= p
->output_offset
;
292 if (*output_offset
!= -1)
293 *output_offset
+= (input_offset
- p
->input_offset
);
299 // Add a mapping for the bytes from OFFSET to OFFSET + LENGTH in input
300 // section SHNDX in object OBJECT to an OUTPUT_OFFSET in merged data
301 // in an output section.
304 Merge_map::add_mapping(Relobj
* object
, unsigned int shndx
,
305 section_offset_type offset
, section_size_type length
,
306 section_offset_type output_offset
)
308 Object_merge_map
* object_merge_map
= object
->merge_map();
309 if (object_merge_map
== NULL
)
311 object_merge_map
= new Object_merge_map();
312 object
->set_merge_map(object_merge_map
);
315 object_merge_map
->add_mapping(this, shndx
, offset
, length
, output_offset
);
318 // Return the output offset for an input address. The input address
319 // is at offset OFFSET in section SHNDX in OBJECT. This sets
320 // *OUTPUT_OFFSET to the offset in the merged data in the output
321 // section. This returns true if the mapping is known, false
325 Merge_map::get_output_offset(const Relobj
* object
, unsigned int shndx
,
326 section_offset_type offset
,
327 section_offset_type
* output_offset
) const
329 Object_merge_map
* object_merge_map
= object
->merge_map();
330 if (object_merge_map
== NULL
)
332 return object_merge_map
->get_output_offset(this, shndx
, offset
,
336 // Class Output_merge_base.
338 // Return the output offset for an input offset. The input address is
339 // at offset OFFSET in section SHNDX in OBJECT. If we know the
340 // offset, set *POUTPUT and return true. Otherwise return false.
343 Output_merge_base::do_output_offset(const Relobj
* object
,
345 section_offset_type offset
,
346 section_offset_type
* poutput
) const
348 return this->merge_map_
.get_output_offset(object
, shndx
, offset
, poutput
);
351 // Class Output_merge_data.
353 // Compute the hash code for a fixed-size constant.
356 Output_merge_data::Merge_data_hash::operator()(Merge_data_key k
) const
358 const unsigned char* p
= this->pomd_
->constant(k
);
359 section_size_type entsize
=
360 convert_to_section_size_type(this->pomd_
->entsize());
362 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
363 if (sizeof(size_t) == 8)
365 size_t result
= static_cast<size_t>(14695981039346656037ULL);
366 for (section_size_type i
= 0; i
< entsize
; ++i
)
368 result
&= (size_t) *p
++;
369 result
*= 1099511628211ULL;
375 size_t result
= 2166136261UL;
376 for (section_size_type i
= 0; i
< entsize
; ++i
)
378 result
^= (size_t) *p
++;
379 result
*= 16777619UL;
385 // Return whether one hash table key equals another.
388 Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1
,
389 Merge_data_key k2
) const
391 const unsigned char* p1
= this->pomd_
->constant(k1
);
392 const unsigned char* p2
= this->pomd_
->constant(k2
);
393 return memcmp(p1
, p2
, this->pomd_
->entsize()) == 0;
396 // Add a constant to the end of the section contents.
399 Output_merge_data::add_constant(const unsigned char* p
)
401 section_size_type entsize
= convert_to_section_size_type(this->entsize());
402 section_size_type addralign
=
403 convert_to_section_size_type(this->addralign());
404 section_size_type addsize
= std::max(entsize
, addralign
);
405 if (this->len_
+ addsize
> this->alc_
)
408 this->alc_
= 128 * addsize
;
411 this->p_
= static_cast<unsigned char*>(realloc(this->p_
, this->alc_
));
412 if (this->p_
== NULL
)
416 memcpy(this->p_
+ this->len_
, p
, entsize
);
417 if (addsize
> entsize
)
418 memset(this->p_
+ this->len_
+ entsize
, 0, addsize
- entsize
);
419 this->len_
+= addsize
;
422 // Add the input section SHNDX in OBJECT to a merged output section
423 // which holds fixed length constants. Return whether we were able to
424 // handle the section; if not, it will be linked as usual without
428 Output_merge_data::do_add_input_section(Relobj
* object
, unsigned int shndx
)
430 section_size_type len
;
431 const unsigned char* p
= object
->section_contents(shndx
, &len
, false);
433 section_size_type entsize
= convert_to_section_size_type(this->entsize());
435 if (len
% entsize
!= 0)
438 this->input_count_
+= len
/ entsize
;
440 for (section_size_type i
= 0; i
< len
; i
+= entsize
, p
+= entsize
)
442 // Add the constant to the section contents. If we find that it
443 // is already in the hash table, we will remove it again.
444 Merge_data_key k
= this->len_
;
445 this->add_constant(p
);
447 std::pair
<Merge_data_hashtable::iterator
, bool> ins
=
448 this->hashtable_
.insert(k
);
452 // Key was already present. Remove the copy we just added.
453 this->len_
-= entsize
;
457 // Record the offset of this constant in the output section.
458 this->add_mapping(object
, shndx
, i
, entsize
, k
);
464 // Set the final data size in a merged output section with fixed size
468 Output_merge_data::set_final_data_size()
470 // Release the memory we don't need.
471 this->p_
= static_cast<unsigned char*>(realloc(this->p_
, this->len_
));
472 gold_assert(this->p_
!= NULL
);
473 this->set_data_size(this->len_
);
476 // Write the data of a merged output section with fixed size constants
480 Output_merge_data::do_write(Output_file
* of
)
482 of
->write(this->offset(), this->p_
, this->len_
);
485 // Write the data to a buffer.
488 Output_merge_data::do_write_to_buffer(unsigned char* buffer
)
490 memcpy(buffer
, this->p_
, this->len_
);
493 // Print merge stats to stderr.
496 Output_merge_data::do_print_merge_stats(const char* section_name
)
499 _("%s: %s merged constants size: %lu; input: %zu; output: %zu\n"),
500 program_name
, section_name
,
501 static_cast<unsigned long>(this->entsize()),
502 this->input_count_
, this->hashtable_
.size());
505 // Class Output_merge_string.
507 // Add an input section to a merged string section.
509 template<typename Char_type
>
511 Output_merge_string
<Char_type
>::do_add_input_section(Relobj
* object
,
514 section_size_type len
;
515 const unsigned char* pdata
= object
->section_contents(shndx
, &len
, false);
517 const Char_type
* p
= reinterpret_cast<const Char_type
*>(pdata
);
518 const Char_type
* pend
= p
+ len
;
520 if (len
% sizeof(Char_type
) != 0)
522 object
->error(_("mergeable string section length not multiple of "
529 // The index I is in bytes, not characters.
530 section_size_type i
= 0;
534 for (pl
= p
; *pl
!= 0; ++pl
)
538 object
->error(_("entry in mergeable string section "
539 "not null terminated"));
545 const Char_type
* str
= this->stringpool_
.add_with_length(p
, pl
- p
, true,
548 section_size_type bytelen_with_null
= ((pl
- p
) + 1) * sizeof(Char_type
);
549 this->merged_strings_
.push_back(Merged_string(object
, shndx
, i
, str
,
550 bytelen_with_null
, key
));
553 i
+= bytelen_with_null
;
557 this->input_count_
+= count
;
562 // Finalize the mappings from the input sections to the output
563 // section, and return the final data size.
565 template<typename Char_type
>
567 Output_merge_string
<Char_type
>::finalize_merged_data()
569 this->stringpool_
.set_string_offsets();
571 for (typename
Merged_strings::const_iterator p
=
572 this->merged_strings_
.begin();
573 p
!= this->merged_strings_
.end();
576 section_offset_type offset
=
577 this->stringpool_
.get_offset_from_key(p
->stringpool_key
);
578 this->add_mapping(p
->object
, p
->shndx
, p
->offset
, p
->length
, offset
);
582 this->merged_strings_
.clear();
584 return this->stringpool_
.get_strtab_size();
587 template<typename Char_type
>
589 Output_merge_string
<Char_type
>::set_final_data_size()
591 const off_t final_data_size
= this->finalize_merged_data();
592 this->set_data_size(final_data_size
);
595 // Write out a merged string section.
597 template<typename Char_type
>
599 Output_merge_string
<Char_type
>::do_write(Output_file
* of
)
601 this->stringpool_
.write(of
, this->offset());
604 // Write a merged string section to a buffer.
606 template<typename Char_type
>
608 Output_merge_string
<Char_type
>::do_write_to_buffer(unsigned char* buffer
)
610 this->stringpool_
.write_to_buffer(buffer
, this->data_size());
613 // Return the name of the types of string to use with
614 // do_print_merge_stats.
616 template<typename Char_type
>
618 Output_merge_string
<Char_type
>::string_name()
626 Output_merge_string
<char>::string_name()
633 Output_merge_string
<uint16_t>::string_name()
635 return "16-bit strings";
640 Output_merge_string
<uint32_t>::string_name()
642 return "32-bit strings";
645 // Print merge stats to stderr.
647 template<typename Char_type
>
649 Output_merge_string
<Char_type
>::do_print_merge_stats(const char* section_name
)
652 snprintf(buf
, sizeof buf
, "%s merged %s", section_name
, this->string_name());
653 fprintf(stderr
, _("%s: %s input: %zu\n"),
654 program_name
, buf
, this->input_count_
);
655 this->stringpool_
.print_stats(buf
);
658 // Instantiate the templates we need.
661 class Output_merge_string
<char>;
664 class Output_merge_string
<uint16_t>;
667 class Output_merge_string
<uint32_t>;
669 } // End namespace gold.