]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/merge.h
From Andrew Chatham: Make File_read::View::data_ a const pointer.
[thirdparty/binutils-gdb.git] / gold / merge.h
CommitLineData
b8e6aad9
ILT
1// merge.h -- handle section merging for gold -*- C++ -*-
2
3#ifndef GOLD_MERGE_H
4#define GOLD_MERGE_H
5
6#include <climits>
7
8#include "stringpool.h"
9#include "output.h"
10
11namespace gold
12{
13
14// A general class for SHF_MERGE data, to hold functions shared by
15// fixed-size constant data and string data.
16
17class Output_merge_base : public Output_section_data
18{
19 public:
20 Output_merge_base(uint64_t entsize)
21 : Output_section_data(1), merge_map_(), entsize_(entsize)
22 { }
23
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;
28
29 protected:
30 // Return the entry size.
31 uint64_t
32 entsize() const
33 { return this->entsize_; }
34
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);
40
41 private:
42 // We build a mapping from OBJECT/SHNDX/OFFSET to an offset in the
43 // output section.
44 struct Merge_key
45 {
46 const Relobj* object;
47 unsigned int shndx;
48 off_t offset;
49 };
50
51 struct Merge_key_less
52 {
53 bool
54 operator()(const Merge_key&, const Merge_key&) const;
55 };
56
57 typedef std::map<Merge_key, off_t, Merge_key_less> Merge_map;
58
59 // A mapping from input object/section/offset to offset in output
60 // section.
61 Merge_map merge_map_;
62
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_;
66};
67
68// Handle SHF_MERGE sections with fixed-size constant data.
69
70class Output_merge_data : public Output_merge_base
71{
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 { }
77
78 // Add an input section.
79 bool
80 do_add_input_section(Relobj* object, unsigned int shndx);
81
82 // Set the final data size.
83 void
84 do_set_address(uint64_t, off_t);
85
86 // Write the data to the file.
87 void
88 do_write(Output_file*);
89
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.
93
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;
97
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
101 {
102 public:
103 Merge_data_hash(const Output_merge_data* pomd)
104 : pomd_(pomd)
105 { }
106
107 size_t
108 operator()(Merge_data_key) const;
109
110 private:
111 const Output_merge_data* pomd_;
112 };
113
114 friend class Merge_data_hash;
115
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
122 {
123 public:
124 Merge_data_eq(const Output_merge_data* pomd)
125 : pomd_(pomd)
126 { }
127
128 bool
129 operator()(Merge_data_key k1, Merge_data_key k2) const;
130
131 private:
132 const Output_merge_data* pomd_;
133 };
134
135 friend class Merge_data_eq;
136
137 // The type of the hash table.
138 typedef Unordered_set<Merge_data_key, Merge_data_hash, Merge_data_eq>
139 Merge_data_hashtable;
140
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
145 {
146 gold_assert(k >= 0 && k < this->len_);
147 return this->p_ + k;
148 }
149
150 // Add a constant to the output.
151 void
152 add_constant(const unsigned char*);
153
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_;
162};
163
164// Handle SHF_MERGE sections with string data. This is a template
165// based on the type of the characters in the string.
166
167template<typename Char_type>
168class Output_merge_string : public Output_merge_base
169{
170 public:
171 Output_merge_string()
a8b2552e
ILT
172 : Output_merge_base(sizeof(Char_type)), stringpool_(), hashtable_()
173 { this->stringpool_.set_no_zero_null(); }
b8e6aad9
ILT
174
175 // Add an input section.
176 bool
177 do_add_input_section(Relobj* object, unsigned int shndx);
178
179 // Set the final data size.
180 void
181 do_set_address(uint64_t, off_t);
182
183 // Write the data to the file.
184 void
185 do_write(Output_file*);
186
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
191 {
192 Relobj* object;
193 unsigned int shndx;
194 off_t offset;
195
196 Merge_string_key(Relobj *objecta, unsigned int shndxa, off_t offseta)
197 : object(objecta), shndx(shndxa), offset(offseta)
198 { }
199 };
200
201 struct Merge_string_key_hash
202 {
203 size_t
204 operator()(const Merge_string_key&) const;
205 };
206
207 struct Merge_string_key_eq
208 {
209 bool
210 operator()(const Merge_string_key&, const Merge_string_key&) const;
211 };
212
213 typedef Unordered_map<Merge_string_key, const Char_type*,
214 Merge_string_key_hash, Merge_string_key_eq>
215 Merge_string_hashtable;
216
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_;
222};
223
224} // End namespace gold.
225
226#endif // !defined(GOLD_MERGE_H)