Make Merge_key_less operator() inline.
[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.
36
37 class Merge_map
38 {
39  public:
40   Merge_map()
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, 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;
60
61  private:
62   // We build a mapping from OBJECT/SHNDX/OFFSET to an offset and
63   // length in the output section.
64   struct Merge_key
65   {
66     const Relobj* object;
67     unsigned int shndx;
68     off_t offset;
69   };
70
71   struct Merge_key_less
72   {
73     inline bool
74     operator()(const Merge_key&, const Merge_key&) const;
75   };
76
77   struct Merge_value
78   {
79     off_t length;
80     off_t output_offset;
81   };
82
83   typedef std::map<Merge_key, Merge_value, Merge_key_less> Merge_mapping;
84
85   // A mapping from input object/section/offset to offset in output
86   // section.
87   Merge_mapping merge_map_;
88 };
89
90 // A general class for SHF_MERGE data, to hold functions shared by
91 // fixed-size constant data and string data.
92
93 class Output_merge_base : public Output_section_data
94 {
95  public:
96   Output_merge_base(uint64_t entsize, uint64_t addralign)
97     : Output_section_data(addralign), merge_map_(), entsize_(entsize)
98   { }
99
100   // Return the output offset for an input offset.
101   bool
102   do_output_offset(const Relobj* object, unsigned int shndx, off_t offset,
103                    off_t* poutput) const;
104
105  protected:
106   // Return the entry size.
107   uint64_t
108   entsize() const
109   { return this->entsize_; }
110
111   // Add a mapping from an OFFSET in input section SHNDX in object
112   // OBJECT to an OUTPUT_OFFSET in the output section.
113   void
114   add_mapping(Relobj* object, unsigned int shndx, off_t offset,
115               off_t length, off_t output_offset)
116   {
117     this->merge_map_.add_mapping(object, shndx, offset, length, output_offset);
118   }
119
120  private:
121   // A mapping from input object/section/offset to offset in output
122   // section.
123   Merge_map merge_map_;
124   // The entry size.  For fixed-size constants, this is the size of
125   // the constants.  For strings, this is the size of a character.
126   uint64_t entsize_;
127 };
128
129 // Handle SHF_MERGE sections with fixed-size constant data.
130
131 class Output_merge_data : public Output_merge_base
132 {
133  public:
134   Output_merge_data(uint64_t entsize, uint64_t addralign)
135     : Output_merge_base(entsize, addralign), p_(NULL), len_(0), alc_(0),
136       hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
137   { }
138
139   // Add an input section.
140   bool
141   do_add_input_section(Relobj* object, unsigned int shndx);
142
143   // Set the final data size.
144   void
145   do_set_address(uint64_t, off_t);
146
147   // Write the data to the file.
148   void
149   do_write(Output_file*);
150
151  private:
152   // We build a hash table of the fixed-size constants.  Each constant
153   // is stored as a pointer into the section data we are accumulating.
154
155   // A key in the hash table.  This is an offset in the section
156   // contents we are building.
157   typedef off_t Merge_data_key;
158
159   // Compute the hash code.  To do this we need a pointer back to the
160   // object holding the data.
161   class Merge_data_hash
162   {
163    public:
164     Merge_data_hash(const Output_merge_data* pomd)
165       : pomd_(pomd)
166     { }
167
168     size_t
169     operator()(Merge_data_key) const;
170
171    private:
172     const Output_merge_data* pomd_;
173   };
174
175   friend class Merge_data_hash;
176
177   // Compare two entries in the hash table for equality.  To do this
178   // we need a pointer back to the object holding the data.  Note that
179   // we now have a pointer to the object stored in two places in the
180   // hash table.  Fixing this would require specializing the hash
181   // table, which would be hard to do portably.
182   class Merge_data_eq
183   {
184    public:
185     Merge_data_eq(const Output_merge_data* pomd)
186       : pomd_(pomd)
187     { }
188
189     bool
190     operator()(Merge_data_key k1, Merge_data_key k2) const;
191
192    private:
193     const Output_merge_data* pomd_;
194   };
195
196   friend class Merge_data_eq;
197
198   // The type of the hash table.
199   typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
200     Merge_data_hashtable;
201
202   // Given a hash table key, which is just an offset into the section
203   // data, return a pointer to the corresponding constant.
204   const unsigned char*
205   constant(Merge_data_key k) const
206   {
207     gold_assert(k >= 0 && k < this->len_);
208     return this->p_ + k;
209   }
210
211   // Add a constant to the output.
212   void
213   add_constant(const unsigned char*);
214
215   // The accumulated data.
216   unsigned char* p_;
217   // The length of the accumulated data.
218   off_t len_;
219   // The size of the allocated buffer.
220   size_t alc_;
221   // The hash table.
222   Merge_data_hashtable hashtable_;
223 };
224
225 // Handle SHF_MERGE sections with string data.  This is a template
226 // based on the type of the characters in the string.
227
228 template<typename Char_type>
229 class Output_merge_string : public Output_merge_base
230 {
231  public:
232   Output_merge_string(uint64_t addralign)
233     : Output_merge_base(sizeof(Char_type), addralign), stringpool_(),
234       merged_strings_()
235   {
236     gold_assert(addralign <= sizeof(Char_type));
237     this->stringpool_.set_no_zero_null();
238   }
239
240   // Add an input section.
241   bool
242   do_add_input_section(Relobj* object, unsigned int shndx);
243
244   // Set the final data size.
245   void
246   do_set_address(uint64_t, off_t);
247
248   // Write the data to the file.
249   void
250   do_write(Output_file*);
251
252  private:
253   // As we see input sections, we build a mapping from object, section
254   // index and offset to strings.
255   struct Merged_string
256   {
257     // The input object where the string was found.
258     Relobj* object;
259     // The input section in the input object.
260     unsigned int shndx;
261     // The offset in the input section.
262     off_t offset;
263     // The string itself, a pointer into a Stringpool.
264     const Char_type* string;
265     // The length of the string in bytes, including the null terminator.
266     size_t length;
267
268     Merged_string(Relobj *objecta, unsigned int shndxa, off_t offseta,
269                   const Char_type* stringa, 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)