]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/merge.h
Avoid some warnings which showed up in 64-bit mode.
[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
47 // discarded.
48 void
8383303e
ILT
49 add_mapping(Relobj* object, unsigned int shndx,
50 section_offset_type offset, section_size_type length,
51 section_offset_type output_offset);
730cdc88
ILT
52
53 // Return the output offset for an input address. The input address
54 // is at offset OFFSET in section SHNDX in OBJECT. This sets
55 // *OUTPUT_OFFSET to the offset in the output section; this will be
56 // -1 if the bytes are not being copied to the output. This returns
57 // true if the mapping is known, false otherwise.
58 bool
8383303e
ILT
59 get_output_offset(const Relobj* object, unsigned int shndx,
60 section_offset_type offset,
61 section_offset_type *output_offset) const;
730cdc88
ILT
62};
63
b8e6aad9
ILT
64// A general class for SHF_MERGE data, to hold functions shared by
65// fixed-size constant data and string data.
66
67class Output_merge_base : public Output_section_data
68{
69 public:
87f95776
ILT
70 Output_merge_base(uint64_t entsize, uint64_t addralign)
71 : Output_section_data(addralign), merge_map_(), entsize_(entsize)
b8e6aad9
ILT
72 { }
73
730cdc88 74 // Return the output offset for an input offset.
b8e6aad9 75 bool
8383303e
ILT
76 do_output_offset(const Relobj* object, unsigned int shndx,
77 section_offset_type offset,
78 section_offset_type* poutput) const;
b8e6aad9
ILT
79
80 protected:
81 // Return the entry size.
82 uint64_t
83 entsize() const
84 { return this->entsize_; }
85
86 // Add a mapping from an OFFSET in input section SHNDX in object
87 // OBJECT to an OUTPUT_OFFSET in the output section.
88 void
8383303e
ILT
89 add_mapping(Relobj* object, unsigned int shndx, section_offset_type offset,
90 section_size_type length, section_offset_type output_offset)
b8e6aad9 91 {
730cdc88
ILT
92 this->merge_map_.add_mapping(object, shndx, offset, length, output_offset);
93 }
b8e6aad9 94
730cdc88 95 private:
b8e6aad9
ILT
96 // A mapping from input object/section/offset to offset in output
97 // section.
98 Merge_map merge_map_;
b8e6aad9
ILT
99 // The entry size. For fixed-size constants, this is the size of
100 // the constants. For strings, this is the size of a character.
101 uint64_t entsize_;
102};
103
104// Handle SHF_MERGE sections with fixed-size constant data.
105
106class Output_merge_data : public Output_merge_base
107{
108 public:
87f95776
ILT
109 Output_merge_data(uint64_t entsize, uint64_t addralign)
110 : Output_merge_base(entsize, addralign), p_(NULL), len_(0), alc_(0),
38c5e8b4 111 input_count_(0),
b8e6aad9
ILT
112 hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
113 { }
114
38c5e8b4 115 protected:
b8e6aad9
ILT
116 // Add an input section.
117 bool
118 do_add_input_section(Relobj* object, unsigned int shndx);
119
120 // Set the final data size.
121 void
27bc2bce 122 set_final_data_size();
b8e6aad9
ILT
123
124 // Write the data to the file.
125 void
126 do_write(Output_file*);
127
96803768
ILT
128 // Write the data to a buffer.
129 void
130 do_write_to_buffer(unsigned char*);
131
38c5e8b4
ILT
132 // Print merge stats to stderr.
133 void
134 do_print_merge_stats(const char* section_name);
135
b8e6aad9
ILT
136 private:
137 // We build a hash table of the fixed-size constants. Each constant
138 // is stored as a pointer into the section data we are accumulating.
139
140 // A key in the hash table. This is an offset in the section
141 // contents we are building.
8383303e 142 typedef section_offset_type Merge_data_key;
b8e6aad9
ILT
143
144 // Compute the hash code. To do this we need a pointer back to the
145 // object holding the data.
146 class Merge_data_hash
147 {
148 public:
149 Merge_data_hash(const Output_merge_data* pomd)
150 : pomd_(pomd)
151 { }
152
153 size_t
154 operator()(Merge_data_key) const;
155
156 private:
157 const Output_merge_data* pomd_;
158 };
159
160 friend class Merge_data_hash;
161
162 // Compare two entries in the hash table for equality. To do this
163 // we need a pointer back to the object holding the data. Note that
164 // we now have a pointer to the object stored in two places in the
165 // hash table. Fixing this would require specializing the hash
166 // table, which would be hard to do portably.
167 class Merge_data_eq
168 {
169 public:
170 Merge_data_eq(const Output_merge_data* pomd)
171 : pomd_(pomd)
172 { }
173
174 bool
175 operator()(Merge_data_key k1, Merge_data_key k2) const;
176
177 private:
178 const Output_merge_data* pomd_;
179 };
180
181 friend class Merge_data_eq;
182
183 // The type of the hash table.
184 typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
185 Merge_data_hashtable;
186
187 // Given a hash table key, which is just an offset into the section
188 // data, return a pointer to the corresponding constant.
189 const unsigned char*
190 constant(Merge_data_key k) const
191 {
8383303e 192 gold_assert(k >= 0 && k < static_cast<section_offset_type>(this->len_));
b8e6aad9
ILT
193 return this->p_ + k;
194 }
195
196 // Add a constant to the output.
197 void
198 add_constant(const unsigned char*);
199
200 // The accumulated data.
201 unsigned char* p_;
202 // The length of the accumulated data.
8383303e 203 section_size_type len_;
b8e6aad9 204 // The size of the allocated buffer.
8383303e 205 section_size_type alc_;
38c5e8b4
ILT
206 // The number of entries seen in input files.
207 size_t input_count_;
b8e6aad9
ILT
208 // The hash table.
209 Merge_data_hashtable hashtable_;
210};
211
212// Handle SHF_MERGE sections with string data. This is a template
213// based on the type of the characters in the string.
214
215template<typename Char_type>
216class Output_merge_string : public Output_merge_base
217{
218 public:
87f95776
ILT
219 Output_merge_string(uint64_t addralign)
220 : Output_merge_base(sizeof(Char_type), addralign), stringpool_(),
38c5e8b4 221 merged_strings_(), input_count_(0)
87f95776
ILT
222 {
223 gold_assert(addralign <= sizeof(Char_type));
224 this->stringpool_.set_no_zero_null();
225 }
b8e6aad9 226
9a0910c3 227 protected:
b8e6aad9
ILT
228 // Add an input section.
229 bool
230 do_add_input_section(Relobj* object, unsigned int shndx);
231
9a0910c3
ILT
232 // Do all the final processing after the input sections are read in.
233 // Returns the final data size.
8383303e 234 section_size_type
9a0910c3
ILT
235 finalize_merged_data();
236
b8e6aad9
ILT
237 // Set the final data size.
238 void
27bc2bce 239 set_final_data_size();
b8e6aad9
ILT
240
241 // Write the data to the file.
242 void
243 do_write(Output_file*);
244
96803768
ILT
245 // Write the data to a buffer.
246 void
247 do_write_to_buffer(unsigned char*);
248
38c5e8b4
ILT
249 // Print merge stats to stderr.
250 void
251 do_print_merge_stats(const char* section_name);
252
9a0910c3
ILT
253 // Writes the stringpool to a buffer.
254 void
8383303e 255 stringpool_to_buffer(unsigned char* buffer, section_size_type buffer_size)
9a0910c3
ILT
256 { this->stringpool_.write_to_buffer(buffer, buffer_size); }
257
258 // Clears all the data in the stringpool, to save on memory.
259 void
260 clear_stringpool()
bc2c67ff 261 { this->stringpool_.clear(); }
9a0910c3 262
b8e6aad9 263 private:
38c5e8b4
ILT
264 // The name of the string type, for stats.
265 const char*
266 string_name();
267
b8e6aad9
ILT
268 // As we see input sections, we build a mapping from object, section
269 // index and offset to strings.
42e3fe0d 270 struct Merged_string
b8e6aad9 271 {
42e3fe0d 272 // The input object where the string was found.
b8e6aad9 273 Relobj* object;
42e3fe0d 274 // The input section in the input object.
b8e6aad9 275 unsigned int shndx;
42e3fe0d 276 // The offset in the input section.
8383303e 277 section_offset_type offset;
42e3fe0d
ILT
278 // The string itself, a pointer into a Stringpool.
279 const Char_type* string;
730cdc88
ILT
280 // The length of the string in bytes, including the null terminator.
281 size_t length;
b8e6aad9 282
8383303e
ILT
283 Merged_string(Relobj *objecta, unsigned int shndxa,
284 section_offset_type offseta, const Char_type* stringa,
285 size_t lengtha)
730cdc88
ILT
286 : object(objecta), shndx(shndxa), offset(offseta), string(stringa),
287 length(lengtha)
b8e6aad9
ILT
288 { }
289 };
290
42e3fe0d 291 typedef std::vector<Merged_string> Merged_strings;
b8e6aad9
ILT
292
293 // As we see the strings, we add them to a Stringpool.
294 Stringpool_template<Char_type> stringpool_;
295 // Map from a location in an input object to an entry in the
296 // Stringpool.
42e3fe0d 297 Merged_strings merged_strings_;
38c5e8b4
ILT
298 // The number of entries seen in input files.
299 size_t input_count_;
b8e6aad9
ILT
300};
301
302} // End namespace gold.
303
304#endif // !defined(GOLD_MERGE_H)