1 // merge.h -- handle section merging for gold -*- C++ -*-
8 #include "stringpool.h"
14 // A general class for SHF_MERGE data, to hold functions shared by
15 // fixed-size constant data and string data.
17 class Output_merge_base : public Output_section_data
20 Output_merge_base(uint64_t entsize)
21 : Output_section_data(1), merge_map_(), entsize_(entsize)
24 // Return the output address for an input address.
26 do_output_address(const Relobj* object, unsigned int shndx, off_t offset,
27 uint64_t output_section_address, uint64_t* poutput) const;
30 // Return the entry size.
33 { return this->entsize_; }
35 // Add a mapping from an OFFSET in input section SHNDX in object
36 // OBJECT to an OUTPUT_OFFSET in the output section.
38 add_mapping(Relobj* object, unsigned int shndx, off_t offset,
42 // We build a mapping from OBJECT/SHNDX/OFFSET to an offset in the
54 operator()(const Merge_key&, const Merge_key&) const;
57 typedef std::map<Merge_key, off_t, Merge_key_less> Merge_map;
59 // A mapping from input object/section/offset to offset in output
63 // The entry size. For fixed-size constants, this is the size of
64 // the constants. For strings, this is the size of a character.
68 // Handle SHF_MERGE sections with fixed-size constant data.
70 class Output_merge_data : public Output_merge_base
73 Output_merge_data(uint64_t entsize)
74 : Output_merge_base(entsize), p_(NULL), len_(0), alc_(0),
75 hashtable_(128, Merge_data_hash(this), Merge_data_eq(this))
78 // Add an input section.
80 do_add_input_section(Relobj* object, unsigned int shndx);
82 // Set the final data size.
84 do_set_address(uint64_t, off_t);
86 // Write the data to the file.
88 do_write(Output_file*);
91 // We build a hash table of the fixed-size constants. Each constant
92 // is stored as a pointer into the section data we are accumulating.
94 // A key in the hash table. This is an offset in the section
95 // contents we are building.
96 typedef off_t Merge_data_key;
98 // Compute the hash code. To do this we need a pointer back to the
99 // object holding the data.
100 class Merge_data_hash
103 Merge_data_hash(const Output_merge_data* pomd)
108 operator()(Merge_data_key) const;
111 const Output_merge_data* pomd_;
114 friend class Merge_data_hash;
116 // Compare two entries in the hash table for equality. To do this
117 // we need a pointer back to the object holding the data. Note that
118 // we now have a pointer to the object stored in two places in the
119 // hash table. Fixing this would require specializing the hash
120 // table, which would be hard to do portably.
124 Merge_data_eq(const Output_merge_data* pomd)
129 operator()(Merge_data_key k1, Merge_data_key k2) const;
132 const Output_merge_data* pomd_;
135 friend class Merge_data_eq;
137 // The type of the hash table.
138 typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
139 Merge_data_hashtable;
141 // Given a hash table key, which is just an offset into the section
142 // data, return a pointer to the corresponding constant.
144 constant(Merge_data_key k) const
146 gold_assert(k >= 0 && k < this->len_);
150 // Add a constant to the output.
152 add_constant(const unsigned char*);
154 // The accumulated data.
156 // The length of the accumulated data.
158 // The size of the allocated buffer.
161 Merge_data_hashtable hashtable_;
164 // Handle SHF_MERGE sections with string data. This is a template
165 // based on the type of the characters in the string.
167 template<typename Char_type>
168 class Output_merge_string : public Output_merge_base
171 Output_merge_string()
172 : Output_merge_base(sizeof(Char_type)), stringpool_(false), hashtable_()
175 // Add an input section.
177 do_add_input_section(Relobj* object, unsigned int shndx);
179 // Set the final data size.
181 do_set_address(uint64_t, off_t);
183 // Write the data to the file.
185 do_write(Output_file*);
188 // As we see input sections, we build a mapping from object, section
189 // index and offset to strings.
190 struct Merge_string_key
196 Merge_string_key(Relobj *objecta, unsigned int shndxa, off_t offseta)
197 : object(objecta), shndx(shndxa), offset(offseta)
201 struct Merge_string_key_hash
204 operator()(const Merge_string_key&) const;
207 struct Merge_string_key_eq
210 operator()(const Merge_string_key&, const Merge_string_key&) const;
213 typedef Unordered_map<Merge_string_key, const Char_type*,
214 Merge_string_key_hash, Merge_string_key_eq>
215 Merge_string_hashtable;
217 // As we see the strings, we add them to a Stringpool.
218 Stringpool_template<Char_type> stringpool_;
219 // Map from a location in an input object to an entry in the
221 Merge_string_hashtable hashtable_;
224 } // End namespace gold.
226 #endif // !defined(GOLD_MERGE_H)