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