]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/merge.cc
Remove unnecessary elfcpp_config.h file.
[thirdparty/binutils-gdb.git] / gold / merge.cc
CommitLineData
b8e6aad9
ILT
1// merge.cc -- handle section merging for gold
2
3#include "gold.h"
4
5#include <cstdlib>
6
7#include "merge.h"
8
9namespace gold
10{
11
12// Sort the entries in a merge mapping. The key is an input object, a
13// section index in that object, and an offset in that section.
14
15bool
16Output_merge_base::Merge_key_less::operator()(const Merge_key& mk1,
17 const Merge_key& mk2) const
18{
19 // The order of different objects and different sections doesn't
20 // matter. We want to get consistent results across links so we
21 // don't use pointer comparison.
22 if (mk1.object != mk2.object)
23 return mk1.object->name() < mk2.object->name();
24 if (mk1.shndx != mk2.shndx)
25 return mk1.shndx < mk2.shndx;
26 return mk1.offset < mk2.offset;
27}
28
29// Add a mapping from an OFFSET in input section SHNDX in object
30// OBJECT to an OUTPUT_OFFSET in a merged output section. This
31// manages the mapping used to resolve relocations against merged
32// sections.
33
34void
35Output_merge_base::add_mapping(Relobj* object, unsigned int shndx,
36 off_t offset, off_t output_offset)
37{
38 Merge_key mk;
39 mk.object = object;
40 mk.shndx = shndx;
41 mk.offset = offset;
42 std::pair<Merge_map::iterator, bool> ins =
43 this->merge_map_.insert(std::make_pair(mk, output_offset));
44 gold_assert(ins.second);
45}
46
47// Return the output address for an input address. The input address
48// is at offset OFFSET in section SHNDX in OBJECT.
49// OUTPUT_SECTION_ADDRESS is the address of the output section. If we
50// know the address, set *POUTPUT and return true. Otherwise return
51// false.
52
53bool
54Output_merge_base::do_output_address(const Relobj* object, unsigned int shndx,
55 off_t offset,
56 uint64_t output_section_address,
57 uint64_t* poutput) const
58{
59 gold_assert(output_section_address == this->address());
60
61 Merge_key mk;
62 mk.object = object;
63 mk.shndx = shndx;
64 mk.offset = offset;
65 Merge_map::const_iterator p = this->merge_map_.lower_bound(mk);
66
67 // If MK is not in the map, lower_bound returns the next iterator
68 // larger than it.
69 if (p->first.object != object
70 || p->first.shndx != shndx
71 || p->first.offset != offset)
72 {
73 if (p == this->merge_map_.begin())
74 return false;
75 --p;
76 }
77
78 if (p->first.object != object || p->first.shndx != shndx)
79 return false;
80
81 // Any input section is fully mapped: we don't need to know the size
82 // of the range starting at P->FIRST.OFFSET.
83 *poutput = output_section_address + p->second + (offset - p->first.offset);
84 return true;
85}
86
87// Compute the hash code for a fixed-size constant.
88
89size_t
90Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
91{
92 const unsigned char* p = this->pomd_->constant(k);
93 uint64_t entsize = this->pomd_->entsize();
94
95 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
96 if (sizeof(size_t) == 8)
97 {
98 size_t result = static_cast<size_t>(14695981039346656037ULL);
99 for (uint64_t i = 0; i < entsize; ++i)
100 {
101 result &= (size_t) *p++;
102 result *= 1099511628211ULL;
103 }
104 return result;
105 }
106 else
107 {
108 size_t result = 2166136261UL;
109 for (uint64_t i = 0; i < entsize; ++i)
110 {
111 result ^= (size_t) *p++;
112 result *= 16777619UL;
113 }
114 return result;
115 }
116}
117
118// Return whether one hash table key equals another.
119
120bool
121Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
122 Merge_data_key k2) const
123{
124 const unsigned char* p1 = this->pomd_->constant(k1);
125 const unsigned char* p2 = this->pomd_->constant(k2);
126 return memcmp(p1, p2, this->pomd_->entsize()) == 0;
127}
128
129// Add a constant to the end of the section contents.
130
131void
132Output_merge_data::add_constant(const unsigned char* p)
133{
134 uint64_t entsize = this->entsize();
135 if (this->len_ + entsize > this->alc_)
136 {
137 if (this->alc_ == 0)
138 this->alc_ = 128 * entsize;
139 else
140 this->alc_ *= 2;
141 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
142 if (this->p_ == NULL)
143 gold_fatal("out of memory", true);
144 }
145
146 memcpy(this->p_ + this->len_, p, entsize);
147 this->len_ += entsize;
148}
149
150// Add the input section SHNDX in OBJECT to a merged output section
151// which holds fixed length constants. Return whether we were able to
152// handle the section; if not, it will be linked as usual without
153// constant merging.
154
155bool
156Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
157{
158 off_t len;
159 const unsigned char* p = object->section_contents(shndx, &len);
160
161 uint64_t entsize = this->entsize();
162
163 if (len % entsize != 0)
164 return false;
165
166 for (off_t i = 0; i < len; i += entsize, p += entsize)
167 {
168 // Add the constant to the section contents. If we find that it
169 // is already in the hash table, we will remove it again.
170 Merge_data_key k = this->len_;
171 this->add_constant(p);
172
173 std::pair<Merge_data_hashtable::iterator, bool> ins =
174 this->hashtable_.insert(k);
175
176 if (!ins.second)
177 {
178 // Key was already present. Remove the copy we just added.
179 this->len_ -= entsize;
180 k = *ins.first;
181 }
182
183 // Record the offset of this constant in the output section.
184 this->add_mapping(object, shndx, i, k);
185 }
186
187 return true;
188}
189
190// Set the final data size in a merged output section with fixed size
191// constants.
192
193void
194Output_merge_data::do_set_address(uint64_t, off_t)
195{
196 // Release the memory we don't need.
197 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
198 gold_assert(this->p_ != NULL);
199 this->set_data_size(this->len_);
200}
201
202// Write the data of a merged output section with fixed size constants
203// to the file.
204
205void
206Output_merge_data::do_write(Output_file* of)
207{
208 of->write(this->offset(), this->p_, this->len_);
209}
210
b8e6aad9
ILT
211// Add an input section to a merged string section.
212
213template<typename Char_type>
214bool
215Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
216 unsigned int shndx)
217{
218 off_t len;
219 const unsigned char* pdata = object->section_contents(shndx, &len);
220
221 const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
222
223 if (len % sizeof(Char_type) != 0)
224 {
225 fprintf(stderr,
226 _("%s: %s: mergeable string section length not multiple of "
227 "character size\n"),
228 program_name, object->name().c_str());
229 gold_exit(false);
230 }
231 len /= sizeof(Char_type);
232
233 off_t i = 0;
234 while (i < len)
235 {
236 off_t plen = 0;
237 for (const Char_type* pl = p; *pl != 0; ++pl)
238 {
239 ++plen;
240 if (i + plen >= len)
241 {
242 fprintf(stderr,
243 _("%s: %s: entry in mergeable string section "
244 "not null terminated\n"),
245 program_name, object->name().c_str());
246 gold_exit(false);
247 }
248 }
249
250 const Char_type* str = this->stringpool_.add(p, NULL);
251
42e3fe0d 252 this->merged_strings_.push_back(Merged_string(object, shndx, i, str));
b8e6aad9
ILT
253
254 p += plen + 1;
255 i += plen + 1;
256 }
257
258 return true;
259}
260
261// Set the final data size of a merged string section. This is where
262// we finalize the mappings from the input sections to the output
263// section.
264
265template<typename Char_type>
266void
267Output_merge_string<Char_type>::do_set_address(uint64_t, off_t)
268{
269 this->stringpool_.set_string_offsets();
270
42e3fe0d
ILT
271 for (typename Merged_strings::const_iterator p =
272 this->merged_strings_.begin();
273 p != this->merged_strings_.end();
b8e6aad9 274 ++p)
42e3fe0d
ILT
275 this->add_mapping(p->object, p->shndx, p->offset,
276 this->stringpool_.get_offset(p->string));
b8e6aad9
ILT
277
278 this->set_data_size(this->stringpool_.get_strtab_size());
279
280 // Save some memory.
42e3fe0d 281 this->merged_strings_.clear();
b8e6aad9
ILT
282}
283
284// Write out a merged string section.
285
286template<typename Char_type>
287void
288Output_merge_string<Char_type>::do_write(Output_file* of)
289{
290 this->stringpool_.write(of, this->offset());
291}
292
293// Instantiate the templates we need.
294
295template
296class Output_merge_string<char>;
297
298template
299class Output_merge_string<uint16_t>;
300
301template
302class Output_merge_string<uint32_t>;
303
304} // End namespace gold.