We don't need a hash table mapping input locations to strings.
[deliverable/binutils-gdb.git] / gold / merge.h
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
11 namespace 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
17 class 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
70 class 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
167 template<typename Char_type>
168 class Output_merge_string : public Output_merge_base
169 {
170 public:
171 Output_merge_string()
172 : Output_merge_base(sizeof(Char_type)), stringpool_(), merged_strings_()
173 { this->stringpool_.set_no_zero_null(); }
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 Merged_string
191 {
192 // The input object where the string was found.
193 Relobj* object;
194 // The input section in the input object.
195 unsigned int shndx;
196 // The offset in the input section.
197 off_t offset;
198 // The string itself, a pointer into a Stringpool.
199 const Char_type* string;
200
201 Merged_string(Relobj *objecta, unsigned int shndxa, off_t offseta,
202 const Char_type* stringa)
203 : object(objecta), shndx(shndxa), offset(offseta), string(stringa)
204 { }
205 };
206
207 typedef std::vector<Merged_string> Merged_strings;
208
209 // As we see the strings, we add them to a Stringpool.
210 Stringpool_template<Char_type> stringpool_;
211 // Map from a location in an input object to an entry in the
212 // Stringpool.
213 Merged_strings merged_strings_;
214 };
215
216 } // End namespace gold.
217
218 #endif // !defined(GOLD_MERGE_H)
This page took 0.05406 seconds and 5 git commands to generate.