]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/merge.h
2007-12-21 H.J. Lu <hongjiu.lu@intel.com>
[thirdparty/binutils-gdb.git] / gold / merge.h
CommitLineData
b8e6aad9
ILT
1// merge.h -- handle section merging for gold -*- C++ -*-
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
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.
12
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.
17
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.
22
b8e6aad9
ILT
23#ifndef GOLD_MERGE_H
24#define GOLD_MERGE_H
25
26#include <climits>
27
28#include "stringpool.h"
29#include "output.h"
30
31namespace gold
32{
33
730cdc88 34// This class manages mappings from input sections to offsets in an
4625f782
ILT
35// output section. This is used where input sections are merged. The
36// actual data is stored in fields in Object.
730cdc88
ILT
37
38class Merge_map
39{
40 public:
41 Merge_map()
730cdc88
ILT
42 { }
43
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
1e983657
ILT
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.
730cdc88 50 void
8383303e
ILT
51 add_mapping(Relobj* object, unsigned int shndx,
52 section_offset_type offset, section_size_type length,
53 section_offset_type output_offset);
730cdc88
ILT
54
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
1e983657
ILT
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.
730cdc88 62 bool
8383303e
ILT
63 get_output_offset(const Relobj* object, unsigned int shndx,
64 section_offset_type offset,
65 section_offset_type *output_offset) const;
730cdc88
ILT
66};
67
b8e6aad9
ILT
68// A general class for SHF_MERGE data, to hold functions shared by
69// fixed-size constant data and string data.
70
71class Output_merge_base : public Output_section_data
72{
73 public:
87f95776
ILT
74 Output_merge_base(uint64_t entsize, uint64_t addralign)
75 : Output_section_data(addralign), merge_map_(), entsize_(entsize)
b8e6aad9
ILT
76 { }
77
730cdc88 78 // Return the output offset for an input offset.
b8e6aad9 79 bool
8383303e
ILT
80 do_output_offset(const Relobj* object, unsigned int shndx,
81 section_offset_type offset,
82 section_offset_type* poutput) const;
b8e6aad9
ILT
83
84 protected:
85 // Return the entry size.
86 uint64_t
87 entsize() const
88 { return this->entsize_; }
89
90 // Add a mapping from an OFFSET in input section SHNDX in object
1e983657
ILT
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
93 // section.
b8e6aad9 94 void
8383303e
ILT
95 add_mapping(Relobj* object, unsigned int shndx, section_offset_type offset,
96 section_size_type length, section_offset_type output_offset)
b8e6aad9 97 {
730cdc88
ILT
98 this->merge_map_.add_mapping(object, shndx, offset, length, output_offset);
99 }
b8e6aad9 100
730cdc88 101 private:
b8e6aad9
ILT
102 // A mapping from input object/section/offset to offset in output
103 // section.
104 Merge_map merge_map_;
b8e6aad9
ILT
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.
107 uint64_t entsize_;
108};
109
110// Handle SHF_MERGE sections with fixed-size constant data.
111
112class Output_merge_data : public Output_merge_base
113{
114 public:
87f95776
ILT
115 Output_merge_data(uint64_t entsize, uint64_t addralign)
116 : Output_merge_base(entsize, addralign), p_(NULL), len_(0), alc_(0),
38c5e8b4 117 input_count_(0),
b8e6aad9
ILT
118 hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
119 { }
120
38c5e8b4 121 protected:
b8e6aad9
ILT
122 // Add an input section.
123 bool
124 do_add_input_section(Relobj* object, unsigned int shndx);
125
126 // Set the final data size.
127 void
27bc2bce 128 set_final_data_size();
b8e6aad9
ILT
129
130 // Write the data to the file.
131 void
132 do_write(Output_file*);
133
96803768
ILT
134 // Write the data to a buffer.
135 void
136 do_write_to_buffer(unsigned char*);
137
38c5e8b4
ILT
138 // Print merge stats to stderr.
139 void
140 do_print_merge_stats(const char* section_name);
141
b8e6aad9
ILT
142 private:
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.
145
146 // A key in the hash table. This is an offset in the section
147 // contents we are building.
8383303e 148 typedef section_offset_type Merge_data_key;
b8e6aad9
ILT
149
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
153 {
154 public:
155 Merge_data_hash(const Output_merge_data* pomd)
156 : pomd_(pomd)
157 { }
158
159 size_t
160 operator()(Merge_data_key) const;
161
162 private:
163 const Output_merge_data* pomd_;
164 };
165
166 friend class Merge_data_hash;
167
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.
173 class Merge_data_eq
174 {
175 public:
176 Merge_data_eq(const Output_merge_data* pomd)
177 : pomd_(pomd)
178 { }
179
180 bool
181 operator()(Merge_data_key k1, Merge_data_key k2) const;
182
183 private:
184 const Output_merge_data* pomd_;
185 };
186
187 friend class Merge_data_eq;
188
189 // The type of the hash table.
190 typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
191 Merge_data_hashtable;
192
193 // Given a hash table key, which is just an offset into the section
194 // data, return a pointer to the corresponding constant.
195 const unsigned char*
196 constant(Merge_data_key k) const
197 {
8383303e 198 gold_assert(k >= 0 && k < static_cast<section_offset_type>(this->len_));
b8e6aad9
ILT
199 return this->p_ + k;
200 }
201
202 // Add a constant to the output.
203 void
204 add_constant(const unsigned char*);
205
206 // The accumulated data.
207 unsigned char* p_;
208 // The length of the accumulated data.
8383303e 209 section_size_type len_;
b8e6aad9 210 // The size of the allocated buffer.
8383303e 211 section_size_type alc_;
38c5e8b4
ILT
212 // The number of entries seen in input files.
213 size_t input_count_;
b8e6aad9
ILT
214 // The hash table.
215 Merge_data_hashtable hashtable_;
216};
217
218// Handle SHF_MERGE sections with string data. This is a template
219// based on the type of the characters in the string.
220
221template<typename Char_type>
222class Output_merge_string : public Output_merge_base
223{
224 public:
87f95776
ILT
225 Output_merge_string(uint64_t addralign)
226 : Output_merge_base(sizeof(Char_type), addralign), stringpool_(),
38c5e8b4 227 merged_strings_(), input_count_(0)
87f95776
ILT
228 {
229 gold_assert(addralign <= sizeof(Char_type));
230 this->stringpool_.set_no_zero_null();
231 }
b8e6aad9 232
9a0910c3 233 protected:
b8e6aad9
ILT
234 // Add an input section.
235 bool
236 do_add_input_section(Relobj* object, unsigned int shndx);
237
9a0910c3
ILT
238 // Do all the final processing after the input sections are read in.
239 // Returns the final data size.
8383303e 240 section_size_type
9a0910c3
ILT
241 finalize_merged_data();
242
b8e6aad9
ILT
243 // Set the final data size.
244 void
27bc2bce 245 set_final_data_size();
b8e6aad9
ILT
246
247 // Write the data to the file.
248 void
249 do_write(Output_file*);
250
96803768
ILT
251 // Write the data to a buffer.
252 void
253 do_write_to_buffer(unsigned char*);
254
38c5e8b4
ILT
255 // Print merge stats to stderr.
256 void
257 do_print_merge_stats(const char* section_name);
258
9a0910c3
ILT
259 // Writes the stringpool to a buffer.
260 void
8383303e 261 stringpool_to_buffer(unsigned char* buffer, section_size_type buffer_size)
9a0910c3
ILT
262 { this->stringpool_.write_to_buffer(buffer, buffer_size); }
263
264 // Clears all the data in the stringpool, to save on memory.
265 void
266 clear_stringpool()
bc2c67ff 267 { this->stringpool_.clear(); }
9a0910c3 268
b8e6aad9 269 private:
38c5e8b4
ILT
270 // The name of the string type, for stats.
271 const char*
272 string_name();
273
b8e6aad9
ILT
274 // As we see input sections, we build a mapping from object, section
275 // index and offset to strings.
42e3fe0d 276 struct Merged_string
b8e6aad9 277 {
42e3fe0d 278 // The input object where the string was found.
b8e6aad9 279 Relobj* object;
42e3fe0d 280 // The input section in the input object.
b8e6aad9 281 unsigned int shndx;
42e3fe0d 282 // The offset in the input section.
8383303e 283 section_offset_type offset;
42e3fe0d
ILT
284 // The string itself, a pointer into a Stringpool.
285 const Char_type* string;
730cdc88
ILT
286 // The length of the string in bytes, including the null terminator.
287 size_t length;
2030fba0
ILT
288 // The key in the Stringpool.
289 Stringpool::Key stringpool_key;
b8e6aad9 290
8383303e
ILT
291 Merged_string(Relobj *objecta, unsigned int shndxa,
292 section_offset_type offseta, const Char_type* stringa,
2030fba0 293 size_t lengtha, Stringpool::Key stringpool_keya)
730cdc88 294 : object(objecta), shndx(shndxa), offset(offseta), string(stringa),
2030fba0 295 length(lengtha), stringpool_key(stringpool_keya)
b8e6aad9
ILT
296 { }
297 };
298
42e3fe0d 299 typedef std::vector<Merged_string> Merged_strings;
b8e6aad9
ILT
300
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
304 // Stringpool.
42e3fe0d 305 Merged_strings merged_strings_;
38c5e8b4
ILT
306 // The number of entries seen in input files.
307 size_t input_count_;
b8e6aad9
ILT
308};
309
310} // End namespace gold.
311
312#endif // !defined(GOLD_MERGE_H)