2007-08-06 Paul Brook <paul@codesourcery.com>
[binutils.git] / gold / merge.h
blobdd97bf2d39e227056ae016b9bb9dcc562d5ac1f9
1 // merge.h -- handle section merging for gold -*- C++ -*-
3 #ifndef GOLD_MERGE_H
4 #define GOLD_MERGE_H
6 #include <climits>
8 #include "stringpool.h"
9 #include "output.h"
11 namespace gold
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
19 public:
20 Output_merge_base(uint64_t entsize)
21 : Output_section_data(1), merge_map_(), entsize_(entsize)
22 { }
24 // Return the output address for an input address.
25 bool
26 do_output_address(const Relobj* object, unsigned int shndx, off_t offset,
27 uint64_t output_section_address, uint64_t* poutput) const;
29 protected:
30 // Return the entry size.
31 uint64_t
32 entsize() const
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.
37 void
38 add_mapping(Relobj* object, unsigned int shndx, off_t offset,
39 off_t output_offset);
41 private:
42 // We build a mapping from OBJECT/SHNDX/OFFSET to an offset in the
43 // output section.
44 struct Merge_key
46 const Relobj* object;
47 unsigned int shndx;
48 off_t offset;
51 struct Merge_key_less
53 bool
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
60 // section.
61 Merge_map merge_map_;
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.
65 uint64_t entsize_;
68 // Handle SHF_MERGE sections with fixed-size constant data.
70 class Output_merge_data : public Output_merge_base
72 public:
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))
76 { }
78 // Add an input section.
79 bool
80 do_add_input_section(Relobj* object, unsigned int shndx);
82 // Set the final data size.
83 void
84 do_set_address(uint64_t, off_t);
86 // Write the data to the file.
87 void
88 do_write(Output_file*);
90 private:
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
102 public:
103 Merge_data_hash(const Output_merge_data* pomd)
104 : pomd_(pomd)
107 size_t
108 operator()(Merge_data_key) const;
110 private:
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.
121 class Merge_data_eq
123 public:
124 Merge_data_eq(const Output_merge_data* pomd)
125 : pomd_(pomd)
128 bool
129 operator()(Merge_data_key k1, Merge_data_key k2) const;
131 private:
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.
143 const unsigned char*
144 constant(Merge_data_key k) const
146 gold_assert(k >= 0 && k < this->len_);
147 return this->p_ + k;
150 // Add a constant to the output.
151 void
152 add_constant(const unsigned char*);
154 // The accumulated data.
155 unsigned char* p_;
156 // The length of the accumulated data.
157 off_t len_;
158 // The size of the allocated buffer.
159 size_t alc_;
160 // The hash table.
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
170 public:
171 Output_merge_string()
172 : Output_merge_base(sizeof(Char_type)), stringpool_(false), hashtable_()
175 // Add an input section.
176 bool
177 do_add_input_section(Relobj* object, unsigned int shndx);
179 // Set the final data size.
180 void
181 do_set_address(uint64_t, off_t);
183 // Write the data to the file.
184 void
185 do_write(Output_file*);
187 private:
188 // As we see input sections, we build a mapping from object, section
189 // index and offset to strings.
190 struct Merge_string_key
192 Relobj* object;
193 unsigned int shndx;
194 off_t offset;
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
203 size_t
204 operator()(const Merge_string_key&) const;
207 struct Merge_string_key_eq
209 bool
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
220 // Stringpool.
221 Merge_string_hashtable hashtable_;
224 } // End namespace gold.
226 #endif // !defined(GOLD_MERGE_H)