Commit | Line | Data |
---|---|---|
b8e6aad9 ILT |
1 | // merge.cc -- handle section merging for gold |
2 | ||
b90efa5b | 3 | // Copyright (C) 2006-2015 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
b8e6aad9 ILT |
23 | #include "gold.h" |
24 | ||
25 | #include <cstdlib> | |
87f95776 | 26 | #include <algorithm> |
b8e6aad9 ILT |
27 | |
28 | #include "merge.h" | |
a2e47362 | 29 | #include "compressed_output.h" |
b8e6aad9 ILT |
30 | |
31 | namespace gold | |
32 | { | |
33 | ||
a9a60db6 | 34 | // Class Object_merge_map. |
4625f782 ILT |
35 | |
36 | // Destructor. | |
37 | ||
38 | Object_merge_map::~Object_merge_map() | |
b8e6aad9 | 39 | { |
4625f782 ILT |
40 | for (Section_merge_maps::iterator p = this->section_merge_maps_.begin(); |
41 | p != this->section_merge_maps_.end(); | |
42 | ++p) | |
43 | delete p->second; | |
44 | } | |
45 | ||
46 | // Get the Input_merge_map to use for an input section, or NULL. | |
47 | ||
67f95b96 RÁE |
48 | const Object_merge_map::Input_merge_map* |
49 | Object_merge_map::get_input_merge_map(unsigned int shndx) const | |
4625f782 ILT |
50 | { |
51 | gold_assert(shndx != -1U); | |
52 | if (shndx == this->first_shnum_) | |
53 | return &this->first_map_; | |
54 | if (shndx == this->second_shnum_) | |
55 | return &this->second_map_; | |
56 | Section_merge_maps::const_iterator p = this->section_merge_maps_.find(shndx); | |
57 | if (p != this->section_merge_maps_.end()) | |
58 | return p->second; | |
59 | return NULL; | |
60 | } | |
61 | ||
62 | // Get or create the Input_merge_map to use for an input section. | |
63 | ||
64 | Object_merge_map::Input_merge_map* | |
dbe40a88 RÁE |
65 | Object_merge_map::get_or_make_input_merge_map( |
66 | const Output_section_data* output_data, unsigned int shndx) { | |
4625f782 ILT |
67 | Input_merge_map* map = this->get_input_merge_map(shndx); |
68 | if (map != NULL) | |
69 | { | |
70 | // For a given input section in a given object, every mapping | |
1e983657 | 71 | // must be done with the same Merge_map. |
dbe40a88 | 72 | gold_assert(map->output_data == output_data); |
4625f782 ILT |
73 | return map; |
74 | } | |
75 | ||
76 | // We need to create a new entry. | |
77 | if (this->first_shnum_ == -1U) | |
cec9d2f3 | 78 | { |
4625f782 | 79 | this->first_shnum_ = shndx; |
dbe40a88 | 80 | this->first_map_.output_data = output_data; |
4625f782 | 81 | return &this->first_map_; |
cec9d2f3 | 82 | } |
4625f782 ILT |
83 | if (this->second_shnum_ == -1U) |
84 | { | |
85 | this->second_shnum_ = shndx; | |
dbe40a88 | 86 | this->second_map_.output_data = output_data; |
4625f782 ILT |
87 | return &this->second_map_; |
88 | } | |
89 | ||
90 | Input_merge_map* new_map = new Input_merge_map; | |
dbe40a88 | 91 | new_map->output_data = output_data; |
4625f782 ILT |
92 | this->section_merge_maps_[shndx] = new_map; |
93 | return new_map; | |
94 | } | |
95 | ||
96 | // Add a mapping. | |
97 | ||
98 | void | |
dbe40a88 RÁE |
99 | Object_merge_map::add_mapping(const Output_section_data* output_data, |
100 | unsigned int shndx, | |
8383303e ILT |
101 | section_offset_type input_offset, |
102 | section_size_type length, | |
103 | section_offset_type output_offset) | |
4625f782 | 104 | { |
dbe40a88 | 105 | Input_merge_map* map = this->get_or_make_input_merge_map(output_data, shndx); |
4625f782 ILT |
106 | |
107 | // Try to merge the new entry in the last one we saw. | |
108 | if (!map->entries.empty()) | |
109 | { | |
110 | Input_merge_entry& entry(map->entries.back()); | |
111 | ||
8383303e ILT |
112 | // Use section_size_type to avoid signed/unsigned warnings. |
113 | section_size_type input_offset_u = input_offset; | |
114 | section_size_type output_offset_u = output_offset; | |
115 | ||
4625f782 ILT |
116 | // If this entry is not in order, we need to sort the vector |
117 | // before looking anything up. | |
8383303e | 118 | if (input_offset_u < entry.input_offset + entry.length) |
4625f782 | 119 | { |
8383303e ILT |
120 | gold_assert(input_offset < entry.input_offset); |
121 | gold_assert(input_offset_u + length | |
122 | <= static_cast<section_size_type>(entry.input_offset)); | |
4625f782 ILT |
123 | map->sorted = false; |
124 | } | |
8383303e | 125 | else if (entry.input_offset + entry.length == input_offset_u |
4625f782 ILT |
126 | && (output_offset == -1 |
127 | ? entry.output_offset == -1 | |
8383303e | 128 | : entry.output_offset + entry.length == output_offset_u)) |
4625f782 ILT |
129 | { |
130 | entry.length += length; | |
131 | return; | |
132 | } | |
133 | } | |
134 | ||
135 | Input_merge_entry entry; | |
136 | entry.input_offset = input_offset; | |
137 | entry.length = length; | |
138 | entry.output_offset = output_offset; | |
139 | map->entries.push_back(entry); | |
140 | } | |
141 | ||
142 | // Get the output offset for an input address. | |
143 | ||
780e49c5 | 144 | bool |
dbe40a88 | 145 | Object_merge_map::get_output_offset(unsigned int shndx, |
8383303e | 146 | section_offset_type input_offset, |
ca09d69a | 147 | section_offset_type* output_offset) |
4625f782 ILT |
148 | { |
149 | Input_merge_map* map = this->get_input_merge_map(shndx); | |
dbe40a88 | 150 | if (map == NULL) |
4625f782 ILT |
151 | return false; |
152 | ||
153 | if (!map->sorted) | |
154 | { | |
155 | std::sort(map->entries.begin(), map->entries.end(), | |
156 | Input_merge_compare()); | |
157 | map->sorted = true; | |
158 | } | |
159 | ||
160 | Input_merge_entry entry; | |
161 | entry.input_offset = input_offset; | |
162 | std::vector<Input_merge_entry>::const_iterator p = | |
45a4fb1a | 163 | std::upper_bound(map->entries.begin(), map->entries.end(), |
4625f782 | 164 | entry, Input_merge_compare()); |
45a4fb1a RÁE |
165 | if (p == map->entries.begin()) |
166 | return false; | |
167 | --p; | |
168 | gold_assert(p->input_offset <= input_offset); | |
4625f782 | 169 | |
8383303e ILT |
170 | if (input_offset - p->input_offset |
171 | >= static_cast<section_offset_type>(p->length)) | |
4625f782 ILT |
172 | return false; |
173 | ||
174 | *output_offset = p->output_offset; | |
175 | if (*output_offset != -1) | |
176 | *output_offset += (input_offset - p->input_offset); | |
177 | return true; | |
b8e6aad9 ILT |
178 | } |
179 | ||
a9a60db6 ILT |
180 | // Return whether this is the merge map for section SHNDX. |
181 | ||
67f95b96 RÁE |
182 | const Output_section_data* |
183 | Object_merge_map::find_merge_section(unsigned int shndx) const { | |
184 | const Object_merge_map::Input_merge_map* map = | |
185 | this->get_input_merge_map(shndx); | |
186 | if (map == NULL) | |
187 | return NULL; | |
188 | return map->output_data; | |
a9a60db6 ILT |
189 | } |
190 | ||
191 | // Initialize a mapping from input offsets to output addresses. | |
192 | ||
193 | template<int size> | |
194 | void | |
195 | Object_merge_map::initialize_input_to_output_map( | |
196 | unsigned int shndx, | |
197 | typename elfcpp::Elf_types<size>::Elf_Addr starting_address, | |
198 | Unordered_map<section_offset_type, | |
199 | typename elfcpp::Elf_types<size>::Elf_Addr>* initialize_map) | |
200 | { | |
201 | Input_merge_map* map = this->get_input_merge_map(shndx); | |
202 | gold_assert(map != NULL); | |
203 | ||
37c3b7b0 ILT |
204 | gold_assert(initialize_map->empty()); |
205 | // We know how many entries we are going to add. | |
206 | // reserve_unordered_map takes an expected count of buckets, not a | |
207 | // count of elements, so double it to try to reduce collisions. | |
208 | reserve_unordered_map(initialize_map, map->entries.size() * 2); | |
209 | ||
a9a60db6 ILT |
210 | for (Input_merge_map::Entries::const_iterator p = map->entries.begin(); |
211 | p != map->entries.end(); | |
212 | ++p) | |
213 | { | |
214 | section_offset_type output_offset = p->output_offset; | |
215 | if (output_offset != -1) | |
216 | output_offset += starting_address; | |
217 | else | |
218 | { | |
219 | // If we see a relocation against an address we have chosen | |
220 | // to discard, we relocate to zero. FIXME: We could also | |
221 | // issue a warning in this case; that would require | |
222 | // reporting this somehow and checking it in the routines in | |
223 | // reloc.h. | |
224 | output_offset = 0; | |
225 | } | |
226 | initialize_map->insert(std::make_pair(p->input_offset, output_offset)); | |
227 | } | |
228 | } | |
229 | ||
730cdc88 ILT |
230 | // Class Output_merge_base. |
231 | ||
232 | // Return the output offset for an input offset. The input address is | |
233 | // at offset OFFSET in section SHNDX in OBJECT. If we know the | |
234 | // offset, set *POUTPUT and return true. Otherwise return false. | |
235 | ||
236 | bool | |
237 | Output_merge_base::do_output_offset(const Relobj* object, | |
238 | unsigned int shndx, | |
2ea97941 | 239 | section_offset_type offset, |
8383303e | 240 | section_offset_type* poutput) const |
730cdc88 | 241 | { |
dbe40a88 | 242 | return object->merge_output_offset(shndx, offset, poutput); |
a9a60db6 ILT |
243 | } |
244 | ||
0439c796 DK |
245 | // Record a merged input section for script processing. |
246 | ||
247 | void | |
248 | Output_merge_base::record_input_section(Relobj* relobj, unsigned int shndx) | |
249 | { | |
250 | gold_assert(this->keeps_input_sections_ && relobj != NULL); | |
251 | // If this is the first input section, record it. We need do this because | |
252 | // this->input_sections_ is unordered. | |
253 | if (this->first_relobj_ == NULL) | |
254 | { | |
255 | this->first_relobj_ = relobj; | |
256 | this->first_shndx_ = shndx; | |
257 | } | |
258 | ||
259 | std::pair<Input_sections::iterator, bool> result = | |
260 | this->input_sections_.insert(Section_id(relobj, shndx)); | |
261 | // We should insert a merge section once only. | |
262 | gold_assert(result.second); | |
263 | } | |
264 | ||
730cdc88 ILT |
265 | // Class Output_merge_data. |
266 | ||
b8e6aad9 ILT |
267 | // Compute the hash code for a fixed-size constant. |
268 | ||
269 | size_t | |
270 | Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const | |
271 | { | |
272 | const unsigned char* p = this->pomd_->constant(k); | |
8383303e ILT |
273 | section_size_type entsize = |
274 | convert_to_section_size_type(this->pomd_->entsize()); | |
b8e6aad9 ILT |
275 | |
276 | // Fowler/Noll/Vo (FNV) hash (type FNV-1a). | |
277 | if (sizeof(size_t) == 8) | |
278 | { | |
279 | size_t result = static_cast<size_t>(14695981039346656037ULL); | |
8383303e | 280 | for (section_size_type i = 0; i < entsize; ++i) |
b8e6aad9 ILT |
281 | { |
282 | result &= (size_t) *p++; | |
283 | result *= 1099511628211ULL; | |
284 | } | |
285 | return result; | |
286 | } | |
287 | else | |
288 | { | |
289 | size_t result = 2166136261UL; | |
8383303e | 290 | for (section_size_type i = 0; i < entsize; ++i) |
b8e6aad9 ILT |
291 | { |
292 | result ^= (size_t) *p++; | |
293 | result *= 16777619UL; | |
294 | } | |
295 | return result; | |
296 | } | |
297 | } | |
298 | ||
299 | // Return whether one hash table key equals another. | |
300 | ||
301 | bool | |
302 | Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1, | |
303 | Merge_data_key k2) const | |
304 | { | |
305 | const unsigned char* p1 = this->pomd_->constant(k1); | |
306 | const unsigned char* p2 = this->pomd_->constant(k2); | |
307 | return memcmp(p1, p2, this->pomd_->entsize()) == 0; | |
308 | } | |
309 | ||
310 | // Add a constant to the end of the section contents. | |
311 | ||
312 | void | |
313 | Output_merge_data::add_constant(const unsigned char* p) | |
314 | { | |
2ea97941 ILT |
315 | section_size_type entsize = convert_to_section_size_type(this->entsize()); |
316 | section_size_type addralign = | |
8383303e | 317 | convert_to_section_size_type(this->addralign()); |
2ea97941 | 318 | section_size_type addsize = std::max(entsize, addralign); |
87f95776 | 319 | if (this->len_ + addsize > this->alc_) |
b8e6aad9 ILT |
320 | { |
321 | if (this->alc_ == 0) | |
87f95776 | 322 | this->alc_ = 128 * addsize; |
b8e6aad9 ILT |
323 | else |
324 | this->alc_ *= 2; | |
325 | this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_)); | |
326 | if (this->p_ == NULL) | |
75f2446e | 327 | gold_nomem(); |
b8e6aad9 ILT |
328 | } |
329 | ||
2ea97941 ILT |
330 | memcpy(this->p_ + this->len_, p, entsize); |
331 | if (addsize > entsize) | |
332 | memset(this->p_ + this->len_ + entsize, 0, addsize - entsize); | |
87f95776 | 333 | this->len_ += addsize; |
b8e6aad9 ILT |
334 | } |
335 | ||
336 | // Add the input section SHNDX in OBJECT to a merged output section | |
337 | // which holds fixed length constants. Return whether we were able to | |
338 | // handle the section; if not, it will be linked as usual without | |
339 | // constant merging. | |
340 | ||
341 | bool | |
342 | Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx) | |
343 | { | |
8383303e | 344 | section_size_type len; |
5dd8762a CC |
345 | bool is_new; |
346 | const unsigned char* p = object->decompressed_section_contents(shndx, &len, | |
347 | &is_new); | |
a2e47362 | 348 | |
2ea97941 | 349 | section_size_type entsize = convert_to_section_size_type(this->entsize()); |
b8e6aad9 | 350 | |
2ea97941 | 351 | if (len % entsize != 0) |
a2e47362 | 352 | { |
5dd8762a CC |
353 | if (is_new) |
354 | delete[] p; | |
a2e47362 CC |
355 | return false; |
356 | } | |
b8e6aad9 | 357 | |
2ea97941 | 358 | this->input_count_ += len / entsize; |
38c5e8b4 | 359 | |
2ea97941 | 360 | for (section_size_type i = 0; i < len; i += entsize, p += entsize) |
b8e6aad9 ILT |
361 | { |
362 | // Add the constant to the section contents. If we find that it | |
363 | // is already in the hash table, we will remove it again. | |
364 | Merge_data_key k = this->len_; | |
365 | this->add_constant(p); | |
366 | ||
367 | std::pair<Merge_data_hashtable::iterator, bool> ins = | |
368 | this->hashtable_.insert(k); | |
369 | ||
370 | if (!ins.second) | |
371 | { | |
372 | // Key was already present. Remove the copy we just added. | |
2ea97941 | 373 | this->len_ -= entsize; |
b8e6aad9 ILT |
374 | k = *ins.first; |
375 | } | |
376 | ||
377 | // Record the offset of this constant in the output section. | |
dbe40a88 | 378 | object->add_merge_mapping(this, shndx, i, entsize, k); |
b8e6aad9 ILT |
379 | } |
380 | ||
0439c796 DK |
381 | // For script processing, we keep the input sections. |
382 | if (this->keeps_input_sections()) | |
383 | record_input_section(object, shndx); | |
384 | ||
5dd8762a CC |
385 | if (is_new) |
386 | delete[] p; | |
a2e47362 | 387 | |
b8e6aad9 ILT |
388 | return true; |
389 | } | |
390 | ||
391 | // Set the final data size in a merged output section with fixed size | |
392 | // constants. | |
393 | ||
394 | void | |
27bc2bce | 395 | Output_merge_data::set_final_data_size() |
b8e6aad9 ILT |
396 | { |
397 | // Release the memory we don't need. | |
398 | this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_)); | |
6bf924b0 DK |
399 | // An Output_merge_data object may be empty and realloc is allowed |
400 | // to return a NULL pointer in this case. An Output_merge_data is empty | |
401 | // if all its input sections have sizes that are not multiples of entsize. | |
402 | gold_assert(this->p_ != NULL || this->len_ == 0); | |
b8e6aad9 ILT |
403 | this->set_data_size(this->len_); |
404 | } | |
405 | ||
406 | // Write the data of a merged output section with fixed size constants | |
407 | // to the file. | |
408 | ||
409 | void | |
410 | Output_merge_data::do_write(Output_file* of) | |
411 | { | |
412 | of->write(this->offset(), this->p_, this->len_); | |
413 | } | |
414 | ||
96803768 ILT |
415 | // Write the data to a buffer. |
416 | ||
417 | void | |
418 | Output_merge_data::do_write_to_buffer(unsigned char* buffer) | |
419 | { | |
420 | memcpy(buffer, this->p_, this->len_); | |
421 | } | |
422 | ||
38c5e8b4 ILT |
423 | // Print merge stats to stderr. |
424 | ||
425 | void | |
426 | Output_merge_data::do_print_merge_stats(const char* section_name) | |
427 | { | |
428 | fprintf(stderr, | |
429 | _("%s: %s merged constants size: %lu; input: %zu; output: %zu\n"), | |
430 | program_name, section_name, | |
431 | static_cast<unsigned long>(this->entsize()), | |
432 | this->input_count_, this->hashtable_.size()); | |
433 | } | |
434 | ||
730cdc88 ILT |
435 | // Class Output_merge_string. |
436 | ||
b8e6aad9 ILT |
437 | // Add an input section to a merged string section. |
438 | ||
439 | template<typename Char_type> | |
440 | bool | |
441 | Output_merge_string<Char_type>::do_add_input_section(Relobj* object, | |
442 | unsigned int shndx) | |
443 | { | |
f31ae5b8 | 444 | section_size_type sec_len; |
5dd8762a CC |
445 | bool is_new; |
446 | const unsigned char* pdata = object->decompressed_section_contents(shndx, | |
f31ae5b8 | 447 | &sec_len, |
5dd8762a | 448 | &is_new); |
a2e47362 | 449 | |
b8e6aad9 | 450 | const Char_type* p = reinterpret_cast<const Char_type*>(pdata); |
f31ae5b8 | 451 | const Char_type* pend = p + sec_len / sizeof(Char_type); |
fef830db | 452 | const Char_type* pend0 = pend; |
b8e6aad9 | 453 | |
f31ae5b8 | 454 | if (sec_len % sizeof(Char_type) != 0) |
b8e6aad9 | 455 | { |
75f2446e ILT |
456 | object->error(_("mergeable string section length not multiple of " |
457 | "character size")); | |
5dd8762a CC |
458 | if (is_new) |
459 | delete[] pdata; | |
75f2446e | 460 | return false; |
b8e6aad9 | 461 | } |
b8e6aad9 | 462 | |
fef830db CC |
463 | if (pend[-1] != 0) |
464 | { | |
465 | gold_warning(_("%s: last entry in mergeable string section '%s' " | |
466 | "not null terminated"), | |
467 | object->name().c_str(), | |
468 | object->section_name(shndx).c_str()); | |
469 | // Find the end of the last NULL-terminated string in the buffer. | |
470 | while (pend0 > p && pend0[-1] != 0) | |
471 | --pend0; | |
472 | } | |
38c5e8b4 | 473 | |
76897331 CC |
474 | Merged_strings_list* merged_strings_list = |
475 | new Merged_strings_list(object, shndx); | |
476 | this->merged_strings_lists_.push_back(merged_strings_list); | |
477 | Merged_strings& merged_strings = merged_strings_list->merged_strings; | |
478 | ||
e31908b6 | 479 | // Count the number of non-null strings in the section and size the list. |
fef830db | 480 | size_t count = 0; |
f31ae5b8 AM |
481 | const Char_type* pt = p; |
482 | while (pt < pend0) | |
483 | { | |
484 | size_t len = string_length(pt); | |
485 | if (len != 0) | |
486 | ++count; | |
487 | pt += len + 1; | |
488 | } | |
fef830db CC |
489 | if (pend0 < pend) |
490 | ++count; | |
491 | merged_strings.reserve(count + 1); | |
492 | ||
fa1bd4fb | 493 | // The index I is in bytes, not characters. |
8383303e | 494 | section_size_type i = 0; |
e31908b6 CC |
495 | |
496 | // We assume here that the beginning of the section is correctly | |
497 | // aligned, so each string within the section must retain the same | |
498 | // modulo. | |
499 | uintptr_t init_align_modulo = (reinterpret_cast<uintptr_t>(pdata) | |
500 | & (this->addralign() - 1)); | |
501 | bool has_misaligned_strings = false; | |
502 | ||
b39b8b9d | 503 | while (p < pend) |
b8e6aad9 | 504 | { |
b39b8b9d | 505 | size_t len = p < pend0 ? string_length(p) : pend - p; |
fef830db | 506 | |
d3a7cd45 L |
507 | // Within merge input section each string must be aligned. |
508 | if (len != 0 | |
509 | && ((reinterpret_cast<uintptr_t>(p) & (this->addralign() - 1)) | |
510 | != init_align_modulo)) | |
511 | has_misaligned_strings = true; | |
fef830db | 512 | |
d3a7cd45 L |
513 | Stringpool::Key key; |
514 | this->stringpool_.add_with_length(p, len, true, &key); | |
fef830db | 515 | |
d3a7cd45 | 516 | merged_strings.push_back(Merged_string(i, key)); |
fef830db CC |
517 | p += len + 1; |
518 | i += (len + 1) * sizeof(Char_type); | |
519 | } | |
b8e6aad9 | 520 | |
76897331 CC |
521 | // Record the last offset in the input section so that we can |
522 | // compute the length of the last string. | |
523 | merged_strings.push_back(Merged_string(i, 0)); | |
524 | ||
38c5e8b4 | 525 | this->input_count_ += count; |
f31ae5b8 | 526 | this->input_size_ += i; |
38c5e8b4 | 527 | |
e31908b6 CC |
528 | if (has_misaligned_strings) |
529 | gold_warning(_("%s: section %s contains incorrectly aligned strings;" | |
530 | " the alignment of those strings won't be preserved"), | |
531 | object->name().c_str(), | |
532 | object->section_name(shndx).c_str()); | |
533 | ||
0439c796 DK |
534 | // For script processing, we keep the input sections. |
535 | if (this->keeps_input_sections()) | |
536 | record_input_section(object, shndx); | |
537 | ||
5dd8762a CC |
538 | if (is_new) |
539 | delete[] pdata; | |
a2e47362 | 540 | |
b8e6aad9 ILT |
541 | return true; |
542 | } | |
543 | ||
9a0910c3 ILT |
544 | // Finalize the mappings from the input sections to the output |
545 | // section, and return the final data size. | |
b8e6aad9 ILT |
546 | |
547 | template<typename Char_type> | |
8383303e | 548 | section_size_type |
9a0910c3 | 549 | Output_merge_string<Char_type>::finalize_merged_data() |
b8e6aad9 ILT |
550 | { |
551 | this->stringpool_.set_string_offsets(); | |
552 | ||
76897331 CC |
553 | for (typename Merged_strings_lists::const_iterator l = |
554 | this->merged_strings_lists_.begin(); | |
555 | l != this->merged_strings_lists_.end(); | |
556 | ++l) | |
c0873094 | 557 | { |
76897331 CC |
558 | section_offset_type last_input_offset = 0; |
559 | section_offset_type last_output_offset = 0; | |
560 | for (typename Merged_strings::const_iterator p = | |
561 | (*l)->merged_strings.begin(); | |
562 | p != (*l)->merged_strings.end(); | |
563 | ++p) | |
564 | { | |
565 | section_size_type length = p->offset - last_input_offset; | |
566 | if (length > 0) | |
dbe40a88 RÁE |
567 | (*l)->object->add_merge_mapping(this, (*l)->shndx, |
568 | last_input_offset, length, last_output_offset); | |
76897331 CC |
569 | last_input_offset = p->offset; |
570 | if (p->stringpool_key != 0) | |
571 | last_output_offset = | |
572 | this->stringpool_.get_offset_from_key(p->stringpool_key); | |
573 | } | |
574 | delete *l; | |
c0873094 | 575 | } |
b8e6aad9 | 576 | |
1d6531cf ILT |
577 | // Save some memory. This also ensures that this function will work |
578 | // if called twice, as may happen if Layout::set_segment_offsets | |
579 | // finds a better alignment. | |
76897331 | 580 | this->merged_strings_lists_.clear(); |
9a0910c3 ILT |
581 | |
582 | return this->stringpool_.get_strtab_size(); | |
583 | } | |
584 | ||
585 | template<typename Char_type> | |
586 | void | |
587 | Output_merge_string<Char_type>::set_final_data_size() | |
588 | { | |
589 | const off_t final_data_size = this->finalize_merged_data(); | |
590 | this->set_data_size(final_data_size); | |
b8e6aad9 ILT |
591 | } |
592 | ||
593 | // Write out a merged string section. | |
594 | ||
595 | template<typename Char_type> | |
596 | void | |
597 | Output_merge_string<Char_type>::do_write(Output_file* of) | |
598 | { | |
599 | this->stringpool_.write(of, this->offset()); | |
600 | } | |
601 | ||
96803768 ILT |
602 | // Write a merged string section to a buffer. |
603 | ||
604 | template<typename Char_type> | |
605 | void | |
606 | Output_merge_string<Char_type>::do_write_to_buffer(unsigned char* buffer) | |
607 | { | |
608 | this->stringpool_.write_to_buffer(buffer, this->data_size()); | |
609 | } | |
610 | ||
38c5e8b4 ILT |
611 | // Return the name of the types of string to use with |
612 | // do_print_merge_stats. | |
613 | ||
614 | template<typename Char_type> | |
615 | const char* | |
616 | Output_merge_string<Char_type>::string_name() | |
617 | { | |
618 | gold_unreachable(); | |
619 | return NULL; | |
620 | } | |
621 | ||
622 | template<> | |
623 | const char* | |
624 | Output_merge_string<char>::string_name() | |
625 | { | |
626 | return "strings"; | |
627 | } | |
628 | ||
629 | template<> | |
630 | const char* | |
631 | Output_merge_string<uint16_t>::string_name() | |
632 | { | |
633 | return "16-bit strings"; | |
634 | } | |
635 | ||
636 | template<> | |
637 | const char* | |
638 | Output_merge_string<uint32_t>::string_name() | |
639 | { | |
640 | return "32-bit strings"; | |
641 | } | |
642 | ||
643 | // Print merge stats to stderr. | |
644 | ||
645 | template<typename Char_type> | |
646 | void | |
647 | Output_merge_string<Char_type>::do_print_merge_stats(const char* section_name) | |
648 | { | |
649 | char buf[200]; | |
650 | snprintf(buf, sizeof buf, "%s merged %s", section_name, this->string_name()); | |
fef830db CC |
651 | fprintf(stderr, _("%s: %s input bytes: %zu\n"), |
652 | program_name, buf, this->input_size_); | |
653 | fprintf(stderr, _("%s: %s input strings: %zu\n"), | |
38c5e8b4 ILT |
654 | program_name, buf, this->input_count_); |
655 | this->stringpool_.print_stats(buf); | |
656 | } | |
657 | ||
b8e6aad9 ILT |
658 | // Instantiate the templates we need. |
659 | ||
660 | template | |
661 | class Output_merge_string<char>; | |
662 | ||
663 | template | |
664 | class Output_merge_string<uint16_t>; | |
665 | ||
666 | template | |
667 | class Output_merge_string<uint32_t>; | |
668 | ||
a9a60db6 ILT |
669 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
670 | template | |
671 | void | |
672 | Object_merge_map::initialize_input_to_output_map<32>( | |
673 | unsigned int shndx, | |
674 | elfcpp::Elf_types<32>::Elf_Addr starting_address, | |
675 | Unordered_map<section_offset_type, elfcpp::Elf_types<32>::Elf_Addr>*); | |
676 | #endif | |
677 | ||
678 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
679 | template | |
680 | void | |
681 | Object_merge_map::initialize_input_to_output_map<64>( | |
682 | unsigned int shndx, | |
683 | elfcpp::Elf_types<64>::Elf_Addr starting_address, | |
684 | Unordered_map<section_offset_type, elfcpp::Elf_types<64>::Elf_Addr>*); | |
685 | #endif | |
686 | ||
b8e6aad9 | 687 | } // End namespace gold. |