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 // A general class for SHF_MERGE data, to hold functions shared by
35 // fixed-size constant data and string data.
37 class Output_merge_base
: public Output_section_data
40 Output_merge_base(uint64_t entsize
, uint64_t addralign
)
41 : Output_section_data(addralign
), merge_map_(), entsize_(entsize
)
44 // Return the output address for an input address.
46 do_output_address(const Relobj
* object
, unsigned int shndx
, off_t offset
,
47 uint64_t output_section_address
, uint64_t* poutput
) const;
50 // Return the entry size.
53 { return this->entsize_
; }
55 // Add a mapping from an OFFSET in input section SHNDX in object
56 // OBJECT to an OUTPUT_OFFSET in the output section.
58 add_mapping(Relobj
* object
, unsigned int shndx
, off_t offset
,
62 // We build a mapping from OBJECT/SHNDX/OFFSET to an offset in the
74 operator()(const Merge_key
&, const Merge_key
&) const;
77 typedef std::map
<Merge_key
, off_t
, Merge_key_less
> Merge_map
;
79 // A mapping from input object/section/offset to offset in output
83 // The entry size. For fixed-size constants, this is the size of
84 // the constants. For strings, this is the size of a character.
88 // Handle SHF_MERGE sections with fixed-size constant data.
90 class Output_merge_data
: public Output_merge_base
93 Output_merge_data(uint64_t entsize
, uint64_t addralign
)
94 : Output_merge_base(entsize
, addralign
), p_(NULL
), len_(0), alc_(0),
95 hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
98 // Add an input section.
100 do_add_input_section(Relobj
* object
, unsigned int shndx
);
102 // Set the final data size.
104 do_set_address(uint64_t, off_t
);
106 // Write the data to the file.
108 do_write(Output_file
*);
111 // We build a hash table of the fixed-size constants. Each constant
112 // is stored as a pointer into the section data we are accumulating.
114 // A key in the hash table. This is an offset in the section
115 // contents we are building.
116 typedef off_t Merge_data_key
;
118 // Compute the hash code. To do this we need a pointer back to the
119 // object holding the data.
120 class Merge_data_hash
123 Merge_data_hash(const Output_merge_data
* pomd
)
128 operator()(Merge_data_key
) const;
131 const Output_merge_data
* pomd_
;
134 friend class Merge_data_hash
;
136 // Compare two entries in the hash table for equality. To do this
137 // we need a pointer back to the object holding the data. Note that
138 // we now have a pointer to the object stored in two places in the
139 // hash table. Fixing this would require specializing the hash
140 // table, which would be hard to do portably.
144 Merge_data_eq(const Output_merge_data
* pomd
)
149 operator()(Merge_data_key k1
, Merge_data_key k2
) const;
152 const Output_merge_data
* pomd_
;
155 friend class Merge_data_eq
;
157 // The type of the hash table.
158 typedef Unordered_set
<Merge_data_key
, Merge_data_hash
, Merge_data_eq
>
159 Merge_data_hashtable
;
161 // Given a hash table key, which is just an offset into the section
162 // data, return a pointer to the corresponding constant.
164 constant(Merge_data_key k
) const
166 gold_assert(k
>= 0 && k
< this->len_
);
170 // Add a constant to the output.
172 add_constant(const unsigned char*);
174 // The accumulated data.
176 // The length of the accumulated data.
178 // The size of the allocated buffer.
181 Merge_data_hashtable hashtable_
;
184 // Handle SHF_MERGE sections with string data. This is a template
185 // based on the type of the characters in the string.
187 template<typename Char_type
>
188 class Output_merge_string
: public Output_merge_base
191 Output_merge_string(uint64_t addralign
)
192 : Output_merge_base(sizeof(Char_type
), addralign
), stringpool_(),
195 gold_assert(addralign
<= sizeof(Char_type
));
196 this->stringpool_
.set_no_zero_null();
199 // Add an input section.
201 do_add_input_section(Relobj
* object
, unsigned int shndx
);
203 // Set the final data size.
205 do_set_address(uint64_t, off_t
);
207 // Write the data to the file.
209 do_write(Output_file
*);
212 // As we see input sections, we build a mapping from object, section
213 // index and offset to strings.
216 // The input object where the string was found.
218 // The input section in the input object.
220 // The offset in the input section.
222 // The string itself, a pointer into a Stringpool.
223 const Char_type
* string
;
225 Merged_string(Relobj
*objecta
, unsigned int shndxa
, off_t offseta
,
226 const Char_type
* stringa
)
227 : object(objecta
), shndx(shndxa
), offset(offseta
), string(stringa
)
231 typedef std::vector
<Merged_string
> Merged_strings
;
233 // As we see the strings, we add them to a Stringpool.
234 Stringpool_template
<Char_type
> stringpool_
;
235 // Map from a location in an input object to an entry in the
237 Merged_strings merged_strings_
;
240 } // End namespace gold.
242 #endif // !defined(GOLD_MERGE_H)