1 // merge.h -- handle section merging for gold -*- C++ -*-
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.
28 #include "stringpool.h"
34 // This class manages mappings from input sections to offsets in an
35 // output section. This is used where input sections are merged. The
36 // actual data is stored in fields in Object.
44 // Add a mapping for the bytes from OFFSET to OFFSET + LENGTH in the
45 // input section SHNDX in object OBJECT to OUTPUT_OFFSET in the
46 // output section. An OUTPUT_OFFSET of -1 means that the bytes are
47 // discarded. OUTPUT_OFFSET is not the offset from the start of the
48 // output section, it is the offset from the start of the merged
49 // data within the output section.
51 add_mapping(Relobj
* object
, unsigned int shndx
,
52 section_offset_type offset
, section_size_type length
,
53 section_offset_type output_offset
);
55 // Return the output offset for an input address. The input address
56 // is at offset OFFSET in section SHNDX in OBJECT. This sets
57 // *OUTPUT_OFFSET to the offset in the output section; this will be
58 // -1 if the bytes are not being copied to the output. This returns
59 // true if the mapping is known, false otherwise. This returns the
60 // value stored by add_mapping, namely the offset from the start of
61 // the merged data within the output section.
63 get_output_offset(const Relobj
* object
, unsigned int shndx
,
64 section_offset_type offset
,
65 section_offset_type
*output_offset
) const;
68 // A general class for SHF_MERGE data, to hold functions shared by
69 // fixed-size constant data and string data.
71 class Output_merge_base
: public Output_section_data
74 Output_merge_base(uint64_t entsize
, uint64_t addralign
)
75 : Output_section_data(addralign
), merge_map_(), entsize_(entsize
)
78 // Return the output offset for an input offset.
80 do_output_offset(const Relobj
* object
, unsigned int shndx
,
81 section_offset_type offset
,
82 section_offset_type
* poutput
) const;
85 // Return the entry size.
88 { return this->entsize_
; }
90 // Add a mapping from an OFFSET in input section SHNDX in object
91 // OBJECT to an OUTPUT_OFFSET in the output section. OUTPUT_OFFSET
92 // is the offset from the start of the merged data in the output
95 add_mapping(Relobj
* object
, unsigned int shndx
, section_offset_type offset
,
96 section_size_type length
, section_offset_type output_offset
)
98 this->merge_map_
.add_mapping(object
, shndx
, offset
, length
, output_offset
);
102 // A mapping from input object/section/offset to offset in output
104 Merge_map merge_map_
;
105 // The entry size. For fixed-size constants, this is the size of
106 // the constants. For strings, this is the size of a character.
110 // Handle SHF_MERGE sections with fixed-size constant data.
112 class Output_merge_data
: public Output_merge_base
115 Output_merge_data(uint64_t entsize
, uint64_t addralign
)
116 : Output_merge_base(entsize
, addralign
), p_(NULL
), len_(0), alc_(0),
118 hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
122 // Add an input section.
124 do_add_input_section(Relobj
* object
, unsigned int shndx
);
126 // Set the final data size.
128 set_final_data_size();
130 // Write the data to the file.
132 do_write(Output_file
*);
134 // Write the data to a buffer.
136 do_write_to_buffer(unsigned char*);
138 // Print merge stats to stderr.
140 do_print_merge_stats(const char* section_name
);
143 // We build a hash table of the fixed-size constants. Each constant
144 // is stored as a pointer into the section data we are accumulating.
146 // A key in the hash table. This is an offset in the section
147 // contents we are building.
148 typedef section_offset_type Merge_data_key
;
150 // Compute the hash code. To do this we need a pointer back to the
151 // object holding the data.
152 class Merge_data_hash
155 Merge_data_hash(const Output_merge_data
* pomd
)
160 operator()(Merge_data_key
) const;
163 const Output_merge_data
* pomd_
;
166 friend class Merge_data_hash
;
168 // Compare two entries in the hash table for equality. To do this
169 // we need a pointer back to the object holding the data. Note that
170 // we now have a pointer to the object stored in two places in the
171 // hash table. Fixing this would require specializing the hash
172 // table, which would be hard to do portably.
176 Merge_data_eq(const Output_merge_data
* pomd
)
181 operator()(Merge_data_key k1
, Merge_data_key k2
) const;
184 const Output_merge_data
* pomd_
;
187 friend class Merge_data_eq
;
189 // The type of the hash table.
190 typedef Unordered_set
<Merge_data_key
, Merge_data_hash
, Merge_data_eq
>
191 Merge_data_hashtable
;
193 // Given a hash table key, which is just an offset into the section
194 // data, return a pointer to the corresponding constant.
196 constant(Merge_data_key k
) const
198 gold_assert(k
>= 0 && k
< static_cast<section_offset_type
>(this->len_
));
202 // Add a constant to the output.
204 add_constant(const unsigned char*);
206 // The accumulated data.
208 // The length of the accumulated data.
209 section_size_type len_
;
210 // The size of the allocated buffer.
211 section_size_type alc_
;
212 // The number of entries seen in input files.
215 Merge_data_hashtable hashtable_
;
218 // Handle SHF_MERGE sections with string data. This is a template
219 // based on the type of the characters in the string.
221 template<typename Char_type
>
222 class Output_merge_string
: public Output_merge_base
225 Output_merge_string(uint64_t addralign
)
226 : Output_merge_base(sizeof(Char_type
), addralign
), stringpool_(),
227 merged_strings_(), input_count_(0)
229 gold_assert(addralign
<= sizeof(Char_type
));
230 this->stringpool_
.set_no_zero_null();
234 // Add an input section.
236 do_add_input_section(Relobj
* object
, unsigned int shndx
);
238 // Do all the final processing after the input sections are read in.
239 // Returns the final data size.
241 finalize_merged_data();
243 // Set the final data size.
245 set_final_data_size();
247 // Write the data to the file.
249 do_write(Output_file
*);
251 // Write the data to a buffer.
253 do_write_to_buffer(unsigned char*);
255 // Print merge stats to stderr.
257 do_print_merge_stats(const char* section_name
);
259 // Writes the stringpool to a buffer.
261 stringpool_to_buffer(unsigned char* buffer
, section_size_type buffer_size
)
262 { this->stringpool_
.write_to_buffer(buffer
, buffer_size
); }
264 // Clears all the data in the stringpool, to save on memory.
267 { this->stringpool_
.clear(); }
270 // The name of the string type, for stats.
274 // As we see input sections, we build a mapping from object, section
275 // index and offset to strings.
278 // The input object where the string was found.
280 // The input section in the input object.
282 // The offset in the input section.
283 section_offset_type offset
;
284 // The string itself, a pointer into a Stringpool.
285 const Char_type
* string
;
286 // The length of the string in bytes, including the null terminator.
288 // The key in the Stringpool.
289 Stringpool::Key stringpool_key
;
291 Merged_string(Relobj
*objecta
, unsigned int shndxa
,
292 section_offset_type offseta
, const Char_type
* stringa
,
293 size_t lengtha
, Stringpool::Key stringpool_keya
)
294 : object(objecta
), shndx(shndxa
), offset(offseta
), string(stringa
),
295 length(lengtha
), stringpool_key(stringpool_keya
)
299 typedef std::vector
<Merged_string
> Merged_strings
;
301 // As we see the strings, we add them to a Stringpool.
302 Stringpool_template
<Char_type
> stringpool_
;
303 // Map from a location in an input object to an entry in the
305 Merged_strings merged_strings_
;
306 // The number of entries seen in input files.
310 } // End namespace gold.
312 #endif // !defined(GOLD_MERGE_H)