* section.c (BFD_FAKE_SECTION): Update.
[deliverable/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),
b8e6aad9
ILT
111 hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
112 { }
113
114 // Add an input section.
115 bool
116 do_add_input_section(Relobj* object, unsigned int shndx);
117
118 // Set the final data size.
119 void
27bc2bce 120 set_final_data_size();
b8e6aad9
ILT
121
122 // Write the data to the file.
123 void
124 do_write(Output_file*);
125
96803768
ILT
126 // Write the data to a buffer.
127 void
128 do_write_to_buffer(unsigned char*);
129
b8e6aad9
ILT
130 private:
131 // We build a hash table of the fixed-size constants. Each constant
132 // is stored as a pointer into the section data we are accumulating.
133
134 // A key in the hash table. This is an offset in the section
135 // contents we are building.
8383303e 136 typedef section_offset_type Merge_data_key;
b8e6aad9
ILT
137
138 // Compute the hash code. To do this we need a pointer back to the
139 // object holding the data.
140 class Merge_data_hash
141 {
142 public:
143 Merge_data_hash(const Output_merge_data* pomd)
144 : pomd_(pomd)
145 { }
146
147 size_t
148 operator()(Merge_data_key) const;
149
150 private:
151 const Output_merge_data* pomd_;
152 };
153
154 friend class Merge_data_hash;
155
156 // Compare two entries in the hash table for equality. To do this
157 // we need a pointer back to the object holding the data. Note that
158 // we now have a pointer to the object stored in two places in the
159 // hash table. Fixing this would require specializing the hash
160 // table, which would be hard to do portably.
161 class Merge_data_eq
162 {
163 public:
164 Merge_data_eq(const Output_merge_data* pomd)
165 : pomd_(pomd)
166 { }
167
168 bool
169 operator()(Merge_data_key k1, Merge_data_key k2) const;
170
171 private:
172 const Output_merge_data* pomd_;
173 };
174
175 friend class Merge_data_eq;
176
177 // The type of the hash table.
178 typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
179 Merge_data_hashtable;
180
181 // Given a hash table key, which is just an offset into the section
182 // data, return a pointer to the corresponding constant.
183 const unsigned char*
184 constant(Merge_data_key k) const
185 {
8383303e 186 gold_assert(k >= 0 && k < static_cast<section_offset_type>(this->len_));
b8e6aad9
ILT
187 return this->p_ + k;
188 }
189
190 // Add a constant to the output.
191 void
192 add_constant(const unsigned char*);
193
194 // The accumulated data.
195 unsigned char* p_;
196 // The length of the accumulated data.
8383303e 197 section_size_type len_;
b8e6aad9 198 // The size of the allocated buffer.
8383303e 199 section_size_type alc_;
b8e6aad9
ILT
200 // The hash table.
201 Merge_data_hashtable hashtable_;
202};
203
204// Handle SHF_MERGE sections with string data. This is a template
205// based on the type of the characters in the string.
206
207template<typename Char_type>
208class Output_merge_string : public Output_merge_base
209{
210 public:
87f95776
ILT
211 Output_merge_string(uint64_t addralign)
212 : Output_merge_base(sizeof(Char_type), addralign), stringpool_(),
213 merged_strings_()
214 {
215 gold_assert(addralign <= sizeof(Char_type));
216 this->stringpool_.set_no_zero_null();
217 }
b8e6aad9 218
9a0910c3 219 protected:
b8e6aad9
ILT
220 // Add an input section.
221 bool
222 do_add_input_section(Relobj* object, unsigned int shndx);
223
9a0910c3
ILT
224 // Do all the final processing after the input sections are read in.
225 // Returns the final data size.
8383303e 226 section_size_type
9a0910c3
ILT
227 finalize_merged_data();
228
b8e6aad9
ILT
229 // Set the final data size.
230 void
27bc2bce 231 set_final_data_size();
b8e6aad9
ILT
232
233 // Write the data to the file.
234 void
235 do_write(Output_file*);
236
96803768
ILT
237 // Write the data to a buffer.
238 void
239 do_write_to_buffer(unsigned char*);
240
9a0910c3
ILT
241 // Writes the stringpool to a buffer.
242 void
8383303e 243 stringpool_to_buffer(unsigned char* buffer, section_size_type buffer_size)
9a0910c3
ILT
244 { this->stringpool_.write_to_buffer(buffer, buffer_size); }
245
246 // Clears all the data in the stringpool, to save on memory.
247 void
248 clear_stringpool()
bc2c67ff 249 { this->stringpool_.clear(); }
9a0910c3 250
b8e6aad9
ILT
251 private:
252 // As we see input sections, we build a mapping from object, section
253 // index and offset to strings.
42e3fe0d 254 struct Merged_string
b8e6aad9 255 {
42e3fe0d 256 // The input object where the string was found.
b8e6aad9 257 Relobj* object;
42e3fe0d 258 // The input section in the input object.
b8e6aad9 259 unsigned int shndx;
42e3fe0d 260 // The offset in the input section.
8383303e 261 section_offset_type offset;
42e3fe0d
ILT
262 // The string itself, a pointer into a Stringpool.
263 const Char_type* string;
730cdc88
ILT
264 // The length of the string in bytes, including the null terminator.
265 size_t length;
b8e6aad9 266
8383303e
ILT
267 Merged_string(Relobj *objecta, unsigned int shndxa,
268 section_offset_type offseta, const Char_type* stringa,
269 size_t lengtha)
730cdc88
ILT
270 : object(objecta), shndx(shndxa), offset(offseta), string(stringa),
271 length(lengtha)
b8e6aad9
ILT
272 { }
273 };
274
42e3fe0d 275 typedef std::vector<Merged_string> Merged_strings;
b8e6aad9
ILT
276
277 // As we see the strings, we add them to a Stringpool.
278 Stringpool_template<Char_type> stringpool_;
279 // Map from a location in an input object to an entry in the
280 // Stringpool.
42e3fe0d 281 Merged_strings merged_strings_;
b8e6aad9
ILT
282};
283
284} // End namespace gold.
285
286#endif // !defined(GOLD_MERGE_H)
This page took 0.065066 seconds and 4 git commands to generate.