Add section_size_type and section_offset_type, use them to replace a
[external/binutils.git] / gold / merge.h
1 // merge.h -- handle section merging for gold  -*- C++ -*-
2
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
23 #ifndef GOLD_MERGE_H
24 #define GOLD_MERGE_H
25
26 #include <climits>
27
28 #include "stringpool.h"
29 #include "output.h"
30
31 namespace gold
32 {
33
34 // This class manages mappings from input sections to offsets in an
35 // output section.  This is used where input sections are merged.  The
36 // actual data is stored in fields in Object.
37
38 class Merge_map
39 {
40  public:
41   Merge_map()
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,
50               section_offset_type offset, section_size_type length,
51               section_offset_type output_offset);
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
59   get_output_offset(const Relobj* object, unsigned int shndx,
60                     section_offset_type offset,
61                     section_offset_type *output_offset) const;
62 };
63
64 // A general class for SHF_MERGE data, to hold functions shared by
65 // fixed-size constant data and string data.
66
67 class Output_merge_base : public Output_section_data
68 {
69  public:
70   Output_merge_base(uint64_t entsize, uint64_t addralign)
71     : Output_section_data(addralign), merge_map_(), entsize_(entsize)
72   { }
73
74   // Return the output offset for an input offset.
75   bool
76   do_output_offset(const Relobj* object, unsigned int shndx,
77                    section_offset_type offset,
78                    section_offset_type* poutput) const;
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
89   add_mapping(Relobj* object, unsigned int shndx, section_offset_type offset,
90               section_size_type length, section_offset_type output_offset)
91   {
92     this->merge_map_.add_mapping(object, shndx, offset, length, output_offset);
93   }
94
95  private:
96   // A mapping from input object/section/offset to offset in output
97   // section.
98   Merge_map merge_map_;
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
106 class Output_merge_data : public Output_merge_base
107 {
108  public:
109   Output_merge_data(uint64_t entsize, uint64_t addralign)
110     : Output_merge_base(entsize, addralign), p_(NULL), len_(0), alc_(0),
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
120   set_final_data_size();
121
122   // Write the data to the file.
123   void
124   do_write(Output_file*);
125
126   // Write the data to a buffer.
127   void
128   do_write_to_buffer(unsigned char*);
129
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.
136   typedef section_offset_type Merge_data_key;
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   {
186     gold_assert(k >= 0 && k < static_cast<section_offset_type>(this->len_));
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.
197   section_size_type len_;
198   // The size of the allocated buffer.
199   section_size_type alc_;
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
207 template<typename Char_type>
208 class Output_merge_string : public Output_merge_base
209 {
210  public:
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   }
218
219  protected:
220   // Add an input section.
221   bool
222   do_add_input_section(Relobj* object, unsigned int shndx);
223
224   // Do all the final processing after the input sections are read in.
225   // Returns the final data size.
226   section_size_type
227   finalize_merged_data();
228
229   // Set the final data size.
230   void
231   set_final_data_size();
232
233   // Write the data to the file.
234   void
235   do_write(Output_file*);
236
237   // Write the data to a buffer.
238   void
239   do_write_to_buffer(unsigned char*);
240
241   // Writes the stringpool to a buffer.
242   void
243   stringpool_to_buffer(unsigned char* buffer, section_size_type buffer_size)
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()
249   { this->stringpool_.clear(); }
250
251  private:
252   // As we see input sections, we build a mapping from object, section
253   // index and offset to strings.
254   struct Merged_string
255   {
256     // The input object where the string was found.
257     Relobj* object;
258     // The input section in the input object.
259     unsigned int shndx;
260     // The offset in the input section.
261     section_offset_type offset;
262     // The string itself, a pointer into a Stringpool.
263     const Char_type* string;
264     // The length of the string in bytes, including the null terminator.
265     size_t length;
266
267     Merged_string(Relobj *objecta, unsigned int shndxa,
268                   section_offset_type offseta, const Char_type* stringa,
269                   size_t lengtha)
270       : object(objecta), shndx(shndxa), offset(offseta), string(stringa),
271         length(lengtha)
272     { }
273   };
274
275   typedef std::vector<Merged_string> Merged_strings;
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.
281   Merged_strings merged_strings_;
282 };
283
284 } // End namespace gold.
285
286 #endif // !defined(GOLD_MERGE_H)