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