1 // merge.h -- handle section merging for gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
28 #include "stringpool.h"
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.
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
49 add_mapping(Relobj* object, unsigned int shndx,
50 section_offset_type offset, section_size_type length,
51 section_offset_type output_offset);
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.
59 get_output_offset(const Relobj* object, unsigned int shndx,
60 section_offset_type offset,
61 section_offset_type *output_offset) const;
64 // A general class for SHF_MERGE data, to hold functions shared by
65 // fixed-size constant data and string data.
67 class Output_merge_base : public Output_section_data
70 Output_merge_base(uint64_t entsize, uint64_t addralign)
71 : Output_section_data(addralign), merge_map_(), entsize_(entsize)
74 // Return the output offset for an input offset.
76 do_output_offset(const Relobj* object, unsigned int shndx,
77 section_offset_type offset,
78 section_offset_type* poutput) const;
81 // Return the entry size.
84 { return this->entsize_; }
86 // Add a mapping from an OFFSET in input section SHNDX in object
87 // OBJECT to an OUTPUT_OFFSET in the output section.
89 add_mapping(Relobj* object, unsigned int shndx, section_offset_type offset,
90 section_size_type length, section_offset_type output_offset)
92 this->merge_map_.add_mapping(object, shndx, offset, length, output_offset);
96 // A mapping from input object/section/offset to offset in output
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.
104 // Handle SHF_MERGE sections with fixed-size constant data.
106 class Output_merge_data : public Output_merge_base
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))
114 // Add an input section.
116 do_add_input_section(Relobj* object, unsigned int shndx);
118 // Set the final data size.
120 set_final_data_size();
122 // Write the data to the file.
124 do_write(Output_file*);
126 // Write the data to a buffer.
128 do_write_to_buffer(unsigned char*);
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.
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;
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
143 Merge_data_hash(const Output_merge_data* pomd)
148 operator()(Merge_data_key) const;
151 const Output_merge_data* pomd_;
154 friend class Merge_data_hash;
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.
164 Merge_data_eq(const Output_merge_data* pomd)
169 operator()(Merge_data_key k1, Merge_data_key k2) const;
172 const Output_merge_data* pomd_;
175 friend class Merge_data_eq;
177 // The type of the hash table.
178 typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
179 Merge_data_hashtable;
181 // Given a hash table key, which is just an offset into the section
182 // data, return a pointer to the corresponding constant.
184 constant(Merge_data_key k) const
186 gold_assert(k >= 0 && k < static_cast<section_offset_type>(this->len_));
190 // Add a constant to the output.
192 add_constant(const unsigned char*);
194 // The accumulated data.
196 // The length of the accumulated data.
197 section_size_type len_;
198 // The size of the allocated buffer.
199 section_size_type alc_;
201 Merge_data_hashtable hashtable_;
204 // Handle SHF_MERGE sections with string data. This is a template
205 // based on the type of the characters in the string.
207 template<typename Char_type>
208 class Output_merge_string : public Output_merge_base
211 Output_merge_string(uint64_t addralign)
212 : Output_merge_base(sizeof(Char_type), addralign), stringpool_(),
215 gold_assert(addralign <= sizeof(Char_type));
216 this->stringpool_.set_no_zero_null();
220 // Add an input section.
222 do_add_input_section(Relobj* object, unsigned int shndx);
224 // Do all the final processing after the input sections are read in.
225 // Returns the final data size.
227 finalize_merged_data();
229 // Set the final data size.
231 set_final_data_size();
233 // Write the data to the file.
235 do_write(Output_file*);
237 // Write the data to a buffer.
239 do_write_to_buffer(unsigned char*);
241 // Writes the stringpool to a buffer.
243 stringpool_to_buffer(unsigned char* buffer, section_size_type buffer_size)
244 { this->stringpool_.write_to_buffer(buffer, buffer_size); }
246 // Clears all the data in the stringpool, to save on memory.
249 { this->stringpool_.clear(); }
252 // As we see input sections, we build a mapping from object, section
253 // index and offset to strings.
256 // The input object where the string was found.
258 // The input section in the input object.
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.
267 Merged_string(Relobj *objecta, unsigned int shndxa,
268 section_offset_type offseta, const Char_type* stringa,
270 : object(objecta), shndx(shndxa), offset(offseta), string(stringa),
275 typedef std::vector<Merged_string> Merged_strings;
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
281 Merged_strings merged_strings_;
284 } // End namespace gold.
286 #endif // !defined(GOLD_MERGE_H)