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, off_t offset, off_t length,
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.
58 get_output_offset(const Relobj* object, unsigned int shndx, off_t offset,
59 off_t *output_offset) const;
62 // A general class for SHF_MERGE data, to hold functions shared by
63 // fixed-size constant data and string data.
65 class Output_merge_base : public Output_section_data
68 Output_merge_base(uint64_t entsize, uint64_t addralign)
69 : Output_section_data(addralign), merge_map_(), entsize_(entsize)
72 // Return the output offset for an input offset.
74 do_output_offset(const Relobj* object, unsigned int shndx, off_t offset,
75 off_t* poutput) const;
78 // Return the entry size.
81 { return this->entsize_; }
83 // Add a mapping from an OFFSET in input section SHNDX in object
84 // OBJECT to an OUTPUT_OFFSET in the output section.
86 add_mapping(Relobj* object, unsigned int shndx, off_t offset,
87 off_t length, off_t output_offset)
89 this->merge_map_.add_mapping(object, shndx, offset, length, output_offset);
93 // A mapping from input object/section/offset to offset in output
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.
101 // Handle SHF_MERGE sections with fixed-size constant data.
103 class Output_merge_data : public Output_merge_base
106 Output_merge_data(uint64_t entsize, uint64_t addralign)
107 : Output_merge_base(entsize, addralign), p_(NULL), len_(0), alc_(0),
108 hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
111 // Add an input section.
113 do_add_input_section(Relobj* object, unsigned int shndx);
115 // Set the final data size.
117 set_final_data_size();
119 // Write the data to the file.
121 do_write(Output_file*);
124 // We build a hash table of the fixed-size constants. Each constant
125 // is stored as a pointer into the section data we are accumulating.
127 // A key in the hash table. This is an offset in the section
128 // contents we are building.
129 typedef off_t Merge_data_key;
131 // Compute the hash code. To do this we need a pointer back to the
132 // object holding the data.
133 class Merge_data_hash
136 Merge_data_hash(const Output_merge_data* pomd)
141 operator()(Merge_data_key) const;
144 const Output_merge_data* pomd_;
147 friend class Merge_data_hash;
149 // Compare two entries in the hash table for equality. To do this
150 // we need a pointer back to the object holding the data. Note that
151 // we now have a pointer to the object stored in two places in the
152 // hash table. Fixing this would require specializing the hash
153 // table, which would be hard to do portably.
157 Merge_data_eq(const Output_merge_data* pomd)
162 operator()(Merge_data_key k1, Merge_data_key k2) const;
165 const Output_merge_data* pomd_;
168 friend class Merge_data_eq;
170 // The type of the hash table.
171 typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
172 Merge_data_hashtable;
174 // Given a hash table key, which is just an offset into the section
175 // data, return a pointer to the corresponding constant.
177 constant(Merge_data_key k) const
179 gold_assert(k >= 0 && k < this->len_);
183 // Add a constant to the output.
185 add_constant(const unsigned char*);
187 // The accumulated data.
189 // The length of the accumulated data.
191 // The size of the allocated buffer.
194 Merge_data_hashtable hashtable_;
197 // Handle SHF_MERGE sections with string data. This is a template
198 // based on the type of the characters in the string.
200 template<typename Char_type>
201 class Output_merge_string : public Output_merge_base
204 Output_merge_string(uint64_t addralign)
205 : Output_merge_base(sizeof(Char_type), addralign), stringpool_(),
208 gold_assert(addralign <= sizeof(Char_type));
209 this->stringpool_.set_no_zero_null();
213 // Add an input section.
215 do_add_input_section(Relobj* object, unsigned int shndx);
217 // Do all the final processing after the input sections are read in.
218 // Returns the final data size.
220 finalize_merged_data();
222 // Set the final data size.
224 set_final_data_size();
226 // Write the data to the file.
228 do_write(Output_file*);
230 // Writes the stringpool to a buffer.
232 stringpool_to_buffer(char* buffer, size_t buffer_size)
233 { this->stringpool_.write_to_buffer(buffer, buffer_size); }
235 // Clears all the data in the stringpool, to save on memory.
238 { stringpool_.clear(); }
241 // As we see input sections, we build a mapping from object, section
242 // index and offset to strings.
245 // The input object where the string was found.
247 // The input section in the input object.
249 // The offset in the input section.
251 // The string itself, a pointer into a Stringpool.
252 const Char_type* string;
253 // The length of the string in bytes, including the null terminator.
256 Merged_string(Relobj *objecta, unsigned int shndxa, off_t offseta,
257 const Char_type* stringa, size_t lengtha)
258 : object(objecta), shndx(shndxa), offset(offseta), string(stringa),
263 typedef std::vector<Merged_string> Merged_strings;
265 // As we see the strings, we add them to a Stringpool.
266 Stringpool_template<Char_type> stringpool_;
267 // Map from a location in an input object to an entry in the
269 Merged_strings merged_strings_;
272 } // End namespace gold.
274 #endif // !defined(GOLD_MERGE_H)