Commit | Line | Data |
---|---|---|
ef15dade ST |
1 | // icf.cc -- Identical Code Folding. |
2 | // | |
b90efa5b | 3 | // Copyright (C) 2009-2015 Free Software Foundation, Inc. |
ef15dade ST |
4 | // Written by Sriraman Tallam <tmsriram@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 | ||
23 | // Identical Code Folding Algorithm | |
24 | // ---------------------------------- | |
25 | // Detecting identical functions is done here and the basic algorithm | |
55a2bb35 | 26 | // is as follows. A checksum is computed on each foldable section using |
ef15dade ST |
27 | // its contents and relocations. If the symbol name corresponding to |
28 | // a relocation is known it is used to compute the checksum. If the | |
29 | // symbol name is not known the stringified name of the object and the | |
30 | // section number pointed to by the relocation is used. The checksums | |
31 | // are stored as keys in a hash map and a section is identical to some | |
32 | // other section if its checksum is already present in the hash map. | |
33 | // Checksum collisions are handled by using a multimap and explicitly | |
34 | // checking the contents when two sections have the same checksum. | |
35 | // | |
36 | // However, two functions A and B with identical text but with | |
55a2bb35 ST |
37 | // relocations pointing to different foldable sections can be identical if |
38 | // the corresponding foldable sections to which their relocations point to | |
ef15dade ST |
39 | // turn out to be identical. Hence, this checksumming process must be |
40 | // done repeatedly until convergence is obtained. Here is an example for | |
41 | // the following case : | |
42 | // | |
43 | // int funcA () int funcB () | |
44 | // { { | |
45 | // return foo(); return goo(); | |
46 | // } } | |
47 | // | |
48 | // The functions funcA and funcB are identical if functions foo() and | |
49 | // goo() are identical. | |
50 | // | |
51 | // Hence, as described above, we repeatedly do the checksumming, | |
52 | // assigning identical functions to the same group, until convergence is | |
53 | // obtained. Now, we have two different ways to do this depending on how | |
54 | // we initialize. | |
55 | // | |
56 | // Algorithm I : | |
57 | // ----------- | |
58 | // We can start with marking all functions as different and repeatedly do | |
59 | // the checksumming. This has the advantage that we do not need to wait | |
60 | // for convergence. We can stop at any point and correctness will be | |
61 | // guaranteed although not all cases would have been found. However, this | |
62 | // has a problem that some cases can never be found even if it is run until | |
63 | // convergence. Here is an example with mutually recursive functions : | |
64 | // | |
65 | // int funcA (int a) int funcB (int a) | |
66 | // { { | |
67 | // if (a == 1) if (a == 1) | |
68 | // return 1; return 1; | |
69 | // return 1 + funcB(a - 1); return 1 + funcA(a - 1); | |
70 | // } } | |
71 | // | |
72 | // In this example funcA and funcB are identical and one of them could be | |
73 | // folded into the other. However, if we start with assuming that funcA | |
74 | // and funcB are not identical, the algorithm, even after it is run to | |
75 | // convergence, cannot detect that they are identical. It should be noted | |
76 | // that even if the functions were self-recursive, Algorithm I cannot catch | |
77 | // that they are identical, at least as is. | |
78 | // | |
79 | // Algorithm II : | |
80 | // ------------ | |
81 | // Here we start with marking all functions as identical and then repeat | |
82 | // the checksumming until convergence. This can detect the above case | |
83 | // mentioned above. It can detect all cases that Algorithm I can and more. | |
84 | // However, the caveat is that it has to be run to convergence. It cannot | |
85 | // be stopped arbitrarily like Algorithm I as correctness cannot be | |
86 | // guaranteed. Algorithm II is not implemented. | |
87 | // | |
88 | // Algorithm I is used because experiments show that about three | |
89 | // iterations are more than enough to achieve convergence. Algorithm I can | |
90 | // handle recursive calls if it is changed to use a special common symbol | |
91 | // for recursive relocs. This seems to be the most common case that | |
92 | // Algorithm I could not catch as is. Mutually recursive calls are not | |
93 | // frequent and Algorithm I wins because of its ability to be stopped | |
94 | // arbitrarily. | |
95 | // | |
96 | // Caveat with using function pointers : | |
97 | // ------------------------------------ | |
98 | // | |
99 | // Programs using function pointer comparisons/checks should use function | |
100 | // folding with caution as the result of such comparisons could be different | |
101 | // when folding takes place. This could lead to unexpected run-time | |
102 | // behaviour. | |
103 | // | |
21bb3914 ST |
104 | // Safe Folding : |
105 | // ------------ | |
106 | // | |
107 | // ICF in safe mode folds only ctors and dtors if their function pointers can | |
108 | // never be taken. Also, for X86-64, safe folding uses the relocation | |
109 | // type to determine if a function's pointer is taken or not and only folds | |
110 | // functions whose pointers are definitely not taken. | |
111 | // | |
112 | // Caveat with safe folding : | |
113 | // ------------------------ | |
114 | // | |
115 | // This applies only to x86_64. | |
116 | // | |
117 | // Position independent executables are created from PIC objects (compiled | |
118 | // with -fPIC) and/or PIE objects (compiled with -fPIE). For PIE objects, the | |
119 | // relocation types for function pointer taken and a call are the same. | |
120 | // Now, it is not always possible to tell if an object used in the link of | |
121 | // a pie executable is a PIC object or a PIE object. Hence, for pie | |
122 | // executables, using relocation types to disambiguate function pointers is | |
123 | // currently disabled. | |
124 | // | |
125 | // Further, it is not correct to use safe folding to build non-pie | |
126 | // executables using PIC/PIE objects. PIC/PIE objects have different | |
127 | // relocation types for function pointers than non-PIC objects, and the | |
128 | // current implementation of safe folding does not handle those relocation | |
129 | // types. Hence, if used, functions whose pointers are taken could still be | |
130 | // folded causing unpredictable run-time behaviour if the pointers were used | |
131 | // in comparisons. | |
132 | // | |
133 | // | |
ef15dade | 134 | // |
55a2bb35 | 135 | // How to run : --icf=[safe|all|none] |
ef15dade ST |
136 | // Optional parameters : --icf-iterations <num> --print-icf-sections |
137 | // | |
138 | // Performance : Less than 20 % link-time overhead on industry strength | |
139 | // applications. Up to 6 % text size reductions. | |
140 | ||
141 | #include "gold.h" | |
142 | #include "object.h" | |
143 | #include "gc.h" | |
144 | #include "icf.h" | |
145 | #include "symtab.h" | |
146 | #include "libiberty.h" | |
032ce4e9 | 147 | #include "demangle.h" |
41cbeecc ST |
148 | #include "elfcpp.h" |
149 | #include "int_encoding.h" | |
ef15dade ST |
150 | |
151 | namespace gold | |
152 | { | |
153 | ||
154 | // This function determines if a section or a group of identical | |
155 | // sections has unique contents. Such unique sections or groups can be | |
156 | // declared final and need not be processed any further. | |
157 | // Parameters : | |
158 | // ID_SECTION : Vector mapping a section index to a Section_id pair. | |
159 | // IS_SECN_OR_GROUP_UNIQUE : To check if a section or a group of identical | |
160 | // sections is already known to be unique. | |
161 | // SECTION_CONTENTS : Contains the section's text and relocs to sections | |
162 | // that cannot be folded. SECTION_CONTENTS are NULL | |
163 | // implies that this function is being called for the | |
164 | // first time before the first iteration of icf. | |
165 | ||
166 | static void | |
167 | preprocess_for_unique_sections(const std::vector<Section_id>& id_section, | |
168 | std::vector<bool>* is_secn_or_group_unique, | |
169 | std::vector<std::string>* section_contents) | |
170 | { | |
171 | Unordered_map<uint32_t, unsigned int> uniq_map; | |
172 | std::pair<Unordered_map<uint32_t, unsigned int>::iterator, bool> | |
173 | uniq_map_insert; | |
174 | ||
175 | for (unsigned int i = 0; i < id_section.size(); i++) | |
176 | { | |
177 | if ((*is_secn_or_group_unique)[i]) | |
178 | continue; | |
179 | ||
180 | uint32_t cksum; | |
181 | Section_id secn = id_section[i]; | |
182 | section_size_type plen; | |
183 | if (section_contents == NULL) | |
184 | { | |
5f9bcf58 CC |
185 | // Lock the object so we can read from it. This is only called |
186 | // single-threaded from queue_middle_tasks, so it is OK to lock. | |
187 | // Unfortunately we have no way to pass in a Task token. | |
188 | const Task* dummy_task = reinterpret_cast<const Task*>(-1); | |
189 | Task_lock_obj<Object> tl(dummy_task, secn.first); | |
ef15dade ST |
190 | const unsigned char* contents; |
191 | contents = secn.first->section_contents(secn.second, | |
192 | &plen, | |
193 | false); | |
194 | cksum = xcrc32(contents, plen, 0xffffffff); | |
195 | } | |
196 | else | |
197 | { | |
198 | const unsigned char* contents_array = reinterpret_cast | |
199 | <const unsigned char*>((*section_contents)[i].c_str()); | |
200 | cksum = xcrc32(contents_array, (*section_contents)[i].length(), | |
201 | 0xffffffff); | |
202 | } | |
203 | uniq_map_insert = uniq_map.insert(std::make_pair(cksum, i)); | |
204 | if (uniq_map_insert.second) | |
205 | { | |
206 | (*is_secn_or_group_unique)[i] = true; | |
207 | } | |
208 | else | |
209 | { | |
210 | (*is_secn_or_group_unique)[i] = false; | |
211 | (*is_secn_or_group_unique)[uniq_map_insert.first->second] = false; | |
212 | } | |
213 | } | |
214 | } | |
215 | ||
216 | // This returns the buffer containing the section's contents, both | |
217 | // text and relocs. Relocs are differentiated as those pointing to | |
218 | // sections that could be folded and those that cannot. Only relocs | |
219 | // pointing to sections that could be folded are recomputed on | |
220 | // subsequent invocations of this function. | |
221 | // Parameters : | |
222 | // FIRST_ITERATION : true if it is the first invocation. | |
223 | // SECN : Section for which contents are desired. | |
224 | // SECTION_NUM : Unique section number of this section. | |
225 | // NUM_TRACKED_RELOCS : Vector reference to store the number of relocs | |
226 | // to ICF sections. | |
227 | // KEPT_SECTION_ID : Vector which maps folded sections to kept sections. | |
228 | // SECTION_CONTENTS : Store the section's text and relocs to non-ICF | |
229 | // sections. | |
230 | ||
231 | static std::string | |
232 | get_section_contents(bool first_iteration, | |
233 | const Section_id& secn, | |
234 | unsigned int section_num, | |
235 | unsigned int* num_tracked_relocs, | |
236 | Symbol_table* symtab, | |
237 | const std::vector<unsigned int>& kept_section_id, | |
238 | std::vector<std::string>* section_contents) | |
239 | { | |
880473a6 DK |
240 | // Lock the object so we can read from it. This is only called |
241 | // single-threaded from queue_middle_tasks, so it is OK to lock. | |
242 | // Unfortunately we have no way to pass in a Task token. | |
243 | const Task* dummy_task = reinterpret_cast<const Task*>(-1); | |
244 | Task_lock_obj<Object> tl(dummy_task, secn.first); | |
245 | ||
ef15dade ST |
246 | section_size_type plen; |
247 | const unsigned char* contents = NULL; | |
ef15dade | 248 | if (first_iteration) |
880473a6 | 249 | contents = secn.first->section_contents(secn.second, &plen, false); |
ef15dade ST |
250 | |
251 | // The buffer to hold all the contents including relocs. A checksum | |
252 | // is then computed on this buffer. | |
253 | std::string buffer; | |
254 | std::string icf_reloc_buffer; | |
255 | ||
256 | if (num_tracked_relocs) | |
257 | *num_tracked_relocs = 0; | |
258 | ||
b487ad64 ST |
259 | Icf::Reloc_info_list& reloc_info_list = |
260 | symtab->icf()->reloc_info_list(); | |
ef15dade | 261 | |
b487ad64 ST |
262 | Icf::Reloc_info_list::iterator it_reloc_info_list = |
263 | reloc_info_list.find(secn); | |
ef15dade ST |
264 | |
265 | buffer.clear(); | |
266 | icf_reloc_buffer.clear(); | |
267 | ||
268 | // Process relocs and put them into the buffer. | |
269 | ||
b487ad64 | 270 | if (it_reloc_info_list != reloc_info_list.end()) |
ef15dade | 271 | { |
c4eb27e1 | 272 | Icf::Sections_reachable_info &v = |
b487ad64 | 273 | (it_reloc_info_list->second).section_info; |
ef38fd8a | 274 | // Stores the information of the symbol pointed to by the reloc. |
c4eb27e1 | 275 | const Icf::Symbol_info &s = (it_reloc_info_list->second).symbol_info; |
ef38fd8a | 276 | // Stores the addend and the symbol value. |
c4eb27e1 | 277 | Icf::Addend_info &a = (it_reloc_info_list->second).addend_info; |
ef38fd8a | 278 | // Stores the offset of the reloc. |
c4eb27e1 ST |
279 | const Icf::Offset_info &o = (it_reloc_info_list->second).offset_info; |
280 | const Icf::Reloc_addend_size_info &reloc_addend_size_info = | |
41cbeecc | 281 | (it_reloc_info_list->second).reloc_addend_size_info; |
b487ad64 | 282 | Icf::Sections_reachable_info::iterator it_v = v.begin(); |
c4eb27e1 | 283 | Icf::Symbol_info::const_iterator it_s = s.begin(); |
ef15dade | 284 | Icf::Addend_info::iterator it_a = a.begin(); |
c4eb27e1 ST |
285 | Icf::Offset_info::const_iterator it_o = o.begin(); |
286 | Icf::Reloc_addend_size_info::const_iterator it_addend_size = | |
41cbeecc | 287 | reloc_addend_size_info.begin(); |
ef15dade | 288 | |
41cbeecc | 289 | for (; it_v != v.end(); ++it_v, ++it_s, ++it_a, ++it_o, ++it_addend_size) |
ef15dade | 290 | { |
ad3d8a2f AM |
291 | if (first_iteration |
292 | && it_v->first != NULL) | |
293 | { | |
294 | Symbol_location loc; | |
295 | loc.object = it_v->first; | |
296 | loc.shndx = it_v->second; | |
297 | loc.offset = convert_types<off_t, long long>(it_a->first | |
298 | + it_a->second); | |
299 | // Look through function descriptors | |
300 | parameters->target().function_location(&loc); | |
301 | if (loc.shndx != it_v->second) | |
302 | { | |
303 | it_v->second = loc.shndx; | |
304 | // Modify symvalue/addend to the code entry. | |
305 | it_a->first = loc.offset; | |
306 | it_a->second = 0; | |
307 | } | |
308 | } | |
309 | ||
b487ad64 | 310 | // ADDEND_STR stores the symbol value and addend and offset, |
9b547ce6 | 311 | // each at most 16 hex digits long. it_a points to a pair |
ef15dade ST |
312 | // where first is the symbol value and second is the |
313 | // addend. | |
b487ad64 | 314 | char addend_str[50]; |
bb0bfe4f DK |
315 | |
316 | // It would be nice if we could use format macros in inttypes.h | |
317 | // here but there are not in ISO/IEC C++ 1998. | |
318 | snprintf(addend_str, sizeof(addend_str), "%llx %llx %llux", | |
319 | static_cast<long long>((*it_a).first), | |
320 | static_cast<long long>((*it_a).second), | |
321 | static_cast<unsigned long long>(*it_o)); | |
ef38fd8a ST |
322 | |
323 | // If the symbol pointed to by the reloc is not in an ordinary | |
324 | // section or if the symbol type is not FROM_OBJECT, then the | |
325 | // object is NULL. | |
326 | if (it_v->first == NULL) | |
327 | { | |
328 | if (first_iteration) | |
329 | { | |
330 | // If the symbol name is available, use it. | |
331 | if ((*it_s) != NULL) | |
332 | buffer.append((*it_s)->name()); | |
333 | // Append the addend. | |
334 | buffer.append(addend_str); | |
335 | buffer.append("@"); | |
336 | } | |
337 | continue; | |
338 | } | |
339 | ||
ef15dade ST |
340 | Section_id reloc_secn(it_v->first, it_v->second); |
341 | ||
342 | // If this reloc turns back and points to the same section, | |
343 | // like a recursive call, use a special symbol to mark this. | |
344 | if (reloc_secn.first == secn.first | |
345 | && reloc_secn.second == secn.second) | |
346 | { | |
347 | if (first_iteration) | |
348 | { | |
349 | buffer.append("R"); | |
350 | buffer.append(addend_str); | |
351 | buffer.append("@"); | |
352 | } | |
353 | continue; | |
354 | } | |
355 | Icf::Uniq_secn_id_map& section_id_map = | |
356 | symtab->icf()->section_to_int_map(); | |
357 | Icf::Uniq_secn_id_map::iterator section_id_map_it = | |
358 | section_id_map.find(reloc_secn); | |
ce97fa81 ST |
359 | bool is_sym_preemptible = (*it_s != NULL |
360 | && !(*it_s)->is_from_dynobj() | |
361 | && !(*it_s)->is_undefined() | |
362 | && (*it_s)->is_preemptible()); | |
363 | if (!is_sym_preemptible | |
364 | && section_id_map_it != section_id_map.end()) | |
ef15dade ST |
365 | { |
366 | // This is a reloc to a section that might be folded. | |
367 | if (num_tracked_relocs) | |
368 | (*num_tracked_relocs)++; | |
369 | ||
370 | char kept_section_str[10]; | |
371 | unsigned int secn_id = section_id_map_it->second; | |
372 | snprintf(kept_section_str, sizeof(kept_section_str), "%u", | |
373 | kept_section_id[secn_id]); | |
374 | if (first_iteration) | |
375 | { | |
376 | buffer.append("ICF_R"); | |
377 | buffer.append(addend_str); | |
378 | } | |
379 | icf_reloc_buffer.append(kept_section_str); | |
380 | // Append the addend. | |
381 | icf_reloc_buffer.append(addend_str); | |
382 | icf_reloc_buffer.append("@"); | |
383 | } | |
384 | else | |
385 | { | |
386 | // This is a reloc to a section that cannot be folded. | |
387 | // Process it only in the first iteration. | |
388 | if (!first_iteration) | |
389 | continue; | |
390 | ||
391 | uint64_t secn_flags = (it_v->first)->section_flags(it_v->second); | |
392 | // This reloc points to a merge section. Hash the | |
393 | // contents of this section. | |
c95e9f27 | 394 | if ((secn_flags & elfcpp::SHF_MERGE) != 0 |
b3ce541e | 395 | && parameters->target().can_icf_inline_merge_sections()) |
ef15dade ST |
396 | { |
397 | uint64_t entsize = | |
398 | (it_v->first)->section_entsize(it_v->second); | |
ce97fa81 ST |
399 | long long offset = it_a->first; |
400 | ||
401 | unsigned long long addend = it_a->second; | |
402 | // Ignoring the addend when it is a negative value. See the | |
403 | // comments in Merged_symbol_value::Value in object.h. | |
404 | if (addend < 0xffffff00) | |
405 | offset = offset + addend; | |
406 | ||
41cbeecc ST |
407 | // For SHT_REL relocation sections, the addend is stored in the |
408 | // text section at the relocation offset. | |
409 | uint64_t reloc_addend_value = 0; | |
410 | const unsigned char* reloc_addend_ptr = | |
411 | contents + static_cast<unsigned long long>(*it_o); | |
412 | switch(*it_addend_size) | |
413 | { | |
414 | case 0: | |
415 | { | |
416 | break; | |
417 | } | |
418 | case 1: | |
419 | { | |
420 | reloc_addend_value = | |
421 | read_from_pointer<8>(reloc_addend_ptr); | |
422 | break; | |
423 | } | |
424 | case 2: | |
425 | { | |
426 | reloc_addend_value = | |
427 | read_from_pointer<16>(reloc_addend_ptr); | |
428 | break; | |
429 | } | |
430 | case 4: | |
431 | { | |
432 | reloc_addend_value = | |
433 | read_from_pointer<32>(reloc_addend_ptr); | |
434 | break; | |
435 | } | |
436 | case 8: | |
437 | { | |
438 | reloc_addend_value = | |
439 | read_from_pointer<64>(reloc_addend_ptr); | |
440 | break; | |
441 | } | |
442 | default: | |
443 | gold_unreachable(); | |
444 | } | |
445 | offset = offset + reloc_addend_value; | |
446 | ||
ef15dade ST |
447 | section_size_type secn_len; |
448 | const unsigned char* str_contents = | |
449 | (it_v->first)->section_contents(it_v->second, | |
450 | &secn_len, | |
451 | false) + offset; | |
452 | if ((secn_flags & elfcpp::SHF_STRINGS) != 0) | |
453 | { | |
454 | // String merge section. | |
455 | const char* str_char = | |
456 | reinterpret_cast<const char*>(str_contents); | |
457 | switch(entsize) | |
458 | { | |
459 | case 1: | |
460 | { | |
461 | buffer.append(str_char); | |
462 | break; | |
463 | } | |
464 | case 2: | |
465 | { | |
466 | const uint16_t* ptr_16 = | |
467 | reinterpret_cast<const uint16_t*>(str_char); | |
468 | unsigned int strlen_16 = 0; | |
469 | // Find the NULL character. | |
470 | while(*(ptr_16 + strlen_16) != 0) | |
471 | strlen_16++; | |
472 | buffer.append(str_char, strlen_16 * 2); | |
473 | } | |
474 | break; | |
475 | case 4: | |
476 | { | |
477 | const uint32_t* ptr_32 = | |
478 | reinterpret_cast<const uint32_t*>(str_char); | |
479 | unsigned int strlen_32 = 0; | |
480 | // Find the NULL character. | |
481 | while(*(ptr_32 + strlen_32) != 0) | |
482 | strlen_32++; | |
483 | buffer.append(str_char, strlen_32 * 4); | |
484 | } | |
485 | break; | |
486 | default: | |
487 | gold_unreachable(); | |
488 | } | |
489 | } | |
490 | else | |
491 | { | |
492 | // Use the entsize to determine the length. | |
493 | buffer.append(reinterpret_cast<const | |
494 | char*>(str_contents), | |
495 | entsize); | |
496 | } | |
d62d0f5f | 497 | buffer.append("@"); |
ef15dade ST |
498 | } |
499 | else if ((*it_s) != NULL) | |
500 | { | |
501 | // If symbol name is available use that. | |
ef38fd8a | 502 | buffer.append((*it_s)->name()); |
ef15dade ST |
503 | // Append the addend. |
504 | buffer.append(addend_str); | |
505 | buffer.append("@"); | |
506 | } | |
507 | else | |
508 | { | |
509 | // Symbol name is not available, like for a local symbol, | |
510 | // use object and section id. | |
511 | buffer.append(it_v->first->name()); | |
512 | char secn_id[10]; | |
513 | snprintf(secn_id, sizeof(secn_id), "%u",it_v->second); | |
514 | buffer.append(secn_id); | |
515 | // Append the addend. | |
516 | buffer.append(addend_str); | |
517 | buffer.append("@"); | |
518 | } | |
519 | } | |
520 | } | |
521 | } | |
522 | ||
523 | if (first_iteration) | |
524 | { | |
525 | buffer.append("Contents = "); | |
526 | buffer.append(reinterpret_cast<const char*>(contents), plen); | |
527 | // Store the section contents that dont change to avoid recomputing | |
528 | // during the next call to this function. | |
529 | (*section_contents)[section_num] = buffer; | |
530 | } | |
531 | else | |
532 | { | |
533 | gold_assert(buffer.empty()); | |
534 | // Reuse the contents computed in the previous iteration. | |
535 | buffer.append((*section_contents)[section_num]); | |
536 | } | |
537 | ||
538 | buffer.append(icf_reloc_buffer); | |
539 | return buffer; | |
540 | } | |
541 | ||
542 | // This function computes a checksum on each section to detect and form | |
543 | // groups of identical sections. The first iteration does this for all | |
544 | // sections. | |
545 | // Further iterations do this only for the kept sections from each group to | |
546 | // determine if larger groups of identical sections could be formed. The | |
547 | // first section in each group is the kept section for that group. | |
548 | // | |
549 | // CRC32 is the checksumming algorithm and can have collisions. That is, | |
550 | // two sections with different contents can have the same checksum. Hence, | |
551 | // a multimap is used to maintain more than one group of checksum | |
552 | // identical sections. A section is added to a group only after its | |
553 | // contents are explicitly compared with the kept section of the group. | |
554 | // | |
555 | // Parameters : | |
556 | // ITERATION_NUM : Invocation instance of this function. | |
557 | // NUM_TRACKED_RELOCS : Vector reference to store the number of relocs | |
558 | // to ICF sections. | |
559 | // KEPT_SECTION_ID : Vector which maps folded sections to kept sections. | |
560 | // ID_SECTION : Vector mapping a section to an unique integer. | |
561 | // IS_SECN_OR_GROUP_UNIQUE : To check if a section or a group of identical | |
9b547ce6 | 562 | // sections is already known to be unique. |
ef15dade ST |
563 | // SECTION_CONTENTS : Store the section's text and relocs to non-ICF |
564 | // sections. | |
565 | ||
566 | static bool | |
567 | match_sections(unsigned int iteration_num, | |
568 | Symbol_table* symtab, | |
569 | std::vector<unsigned int>* num_tracked_relocs, | |
570 | std::vector<unsigned int>* kept_section_id, | |
571 | const std::vector<Section_id>& id_section, | |
572 | std::vector<bool>* is_secn_or_group_unique, | |
573 | std::vector<std::string>* section_contents) | |
574 | { | |
575 | Unordered_multimap<uint32_t, unsigned int> section_cksum; | |
576 | std::pair<Unordered_multimap<uint32_t, unsigned int>::iterator, | |
577 | Unordered_multimap<uint32_t, unsigned int>::iterator> key_range; | |
578 | bool converged = true; | |
579 | ||
580 | if (iteration_num == 1) | |
581 | preprocess_for_unique_sections(id_section, | |
582 | is_secn_or_group_unique, | |
583 | NULL); | |
584 | else | |
585 | preprocess_for_unique_sections(id_section, | |
586 | is_secn_or_group_unique, | |
587 | section_contents); | |
588 | ||
589 | std::vector<std::string> full_section_contents; | |
590 | ||
591 | for (unsigned int i = 0; i < id_section.size(); i++) | |
592 | { | |
593 | full_section_contents.push_back(""); | |
594 | if ((*is_secn_or_group_unique)[i]) | |
595 | continue; | |
596 | ||
597 | Section_id secn = id_section[i]; | |
598 | std::string this_secn_contents; | |
599 | uint32_t cksum; | |
600 | if (iteration_num == 1) | |
601 | { | |
602 | unsigned int num_relocs = 0; | |
603 | this_secn_contents = get_section_contents(true, secn, i, &num_relocs, | |
604 | symtab, (*kept_section_id), | |
605 | section_contents); | |
606 | (*num_tracked_relocs)[i] = num_relocs; | |
607 | } | |
608 | else | |
609 | { | |
610 | if ((*kept_section_id)[i] != i) | |
611 | { | |
612 | // This section is already folded into something. See | |
613 | // if it should point to a different kept section. | |
614 | unsigned int kept_section = (*kept_section_id)[i]; | |
615 | if (kept_section != (*kept_section_id)[kept_section]) | |
616 | { | |
617 | (*kept_section_id)[i] = (*kept_section_id)[kept_section]; | |
618 | } | |
619 | continue; | |
620 | } | |
621 | this_secn_contents = get_section_contents(false, secn, i, NULL, | |
622 | symtab, (*kept_section_id), | |
623 | section_contents); | |
624 | } | |
625 | ||
626 | const unsigned char* this_secn_contents_array = | |
627 | reinterpret_cast<const unsigned char*>(this_secn_contents.c_str()); | |
628 | cksum = xcrc32(this_secn_contents_array, this_secn_contents.length(), | |
629 | 0xffffffff); | |
630 | size_t count = section_cksum.count(cksum); | |
631 | ||
632 | if (count == 0) | |
633 | { | |
634 | // Start a group with this cksum. | |
635 | section_cksum.insert(std::make_pair(cksum, i)); | |
636 | full_section_contents[i] = this_secn_contents; | |
637 | } | |
638 | else | |
639 | { | |
640 | key_range = section_cksum.equal_range(cksum); | |
641 | Unordered_multimap<uint32_t, unsigned int>::iterator it; | |
642 | // Search all the groups with this cksum for a match. | |
643 | for (it = key_range.first; it != key_range.second; ++it) | |
644 | { | |
645 | unsigned int kept_section = it->second; | |
646 | if (full_section_contents[kept_section].length() | |
647 | != this_secn_contents.length()) | |
648 | continue; | |
649 | if (memcmp(full_section_contents[kept_section].c_str(), | |
650 | this_secn_contents.c_str(), | |
651 | this_secn_contents.length()) != 0) | |
652 | continue; | |
653 | (*kept_section_id)[i] = kept_section; | |
654 | converged = false; | |
655 | break; | |
656 | } | |
657 | if (it == key_range.second) | |
658 | { | |
659 | // Create a new group for this cksum. | |
660 | section_cksum.insert(std::make_pair(cksum, i)); | |
661 | full_section_contents[i] = this_secn_contents; | |
662 | } | |
663 | } | |
664 | // If there are no relocs to foldable sections do not process | |
665 | // this section any further. | |
666 | if (iteration_num == 1 && (*num_tracked_relocs)[i] == 0) | |
667 | (*is_secn_or_group_unique)[i] = true; | |
668 | } | |
669 | ||
670 | return converged; | |
671 | } | |
672 | ||
032ce4e9 | 673 | // During safe icf (--icf=safe), only fold functions that are ctors or dtors. |
4e271fff | 674 | // This function returns true if the section name is that of a ctor or a dtor. |
032ce4e9 ST |
675 | |
676 | static bool | |
4e271fff | 677 | is_function_ctor_or_dtor(const std::string& section_name) |
032ce4e9 | 678 | { |
4e271fff ST |
679 | const char* mangled_func_name = strrchr(section_name.c_str(), '.'); |
680 | gold_assert(mangled_func_name != NULL); | |
681 | if ((is_prefix_of("._ZN", mangled_func_name) | |
682 | || is_prefix_of("._ZZ", mangled_func_name)) | |
683 | && (is_gnu_v3_mangled_ctor(mangled_func_name + 1) | |
684 | || is_gnu_v3_mangled_dtor(mangled_func_name + 1))) | |
032ce4e9 ST |
685 | { |
686 | return true; | |
687 | } | |
688 | return false; | |
689 | } | |
ef15dade ST |
690 | |
691 | // This is the main ICF function called in gold.cc. This does the | |
692 | // initialization and calls match_sections repeatedly (twice by default) | |
693 | // which computes the crc checksums and detects identical functions. | |
694 | ||
695 | void | |
696 | Icf::find_identical_sections(const Input_objects* input_objects, | |
697 | Symbol_table* symtab) | |
698 | { | |
699 | unsigned int section_num = 0; | |
2ea97941 | 700 | std::vector<unsigned int> num_tracked_relocs; |
ef15dade ST |
701 | std::vector<bool> is_secn_or_group_unique; |
702 | std::vector<std::string> section_contents; | |
21bb3914 | 703 | const Target& target = parameters->target(); |
ef15dade ST |
704 | |
705 | // Decide which sections are possible candidates first. | |
706 | ||
707 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
708 | p != input_objects->relobj_end(); | |
709 | ++p) | |
710 | { | |
5f9bcf58 CC |
711 | // Lock the object so we can read from it. This is only called |
712 | // single-threaded from queue_middle_tasks, so it is OK to lock. | |
713 | // Unfortunately we have no way to pass in a Task token. | |
714 | const Task* dummy_task = reinterpret_cast<const Task*>(-1); | |
715 | Task_lock_obj<Object> tl(dummy_task, *p); | |
716 | ||
ef15dade ST |
717 | for (unsigned int i = 0;i < (*p)->shnum(); ++i) |
718 | { | |
4e271fff | 719 | const std::string section_name = (*p)->section_name(i); |
55a2bb35 | 720 | if (!is_section_foldable_candidate(section_name)) |
ef15dade ST |
721 | continue; |
722 | if (!(*p)->is_section_included(i)) | |
723 | continue; | |
724 | if (parameters->options().gc_sections() | |
725 | && symtab->gc()->is_section_garbage(*p, i)) | |
726 | continue; | |
55a2bb35 ST |
727 | // With --icf=safe, check if the mangled function name is a ctor |
728 | // or a dtor. The mangled function name can be obtained from the | |
729 | // section name by stripping the section prefix. | |
032ce4e9 | 730 | if (parameters->options().icf_safe_folding() |
4e271fff | 731 | && !is_function_ctor_or_dtor(section_name) |
21bb3914 ST |
732 | && (!target.can_check_for_function_pointers() |
733 | || section_has_function_pointers(*p, i))) | |
734 | { | |
735 | continue; | |
736 | } | |
ef15dade ST |
737 | this->id_section_.push_back(Section_id(*p, i)); |
738 | this->section_id_[Section_id(*p, i)] = section_num; | |
739 | this->kept_section_id_.push_back(section_num); | |
2ea97941 | 740 | num_tracked_relocs.push_back(0); |
ef15dade ST |
741 | is_secn_or_group_unique.push_back(false); |
742 | section_contents.push_back(""); | |
743 | section_num++; | |
744 | } | |
745 | } | |
746 | ||
747 | unsigned int num_iterations = 0; | |
748 | ||
749 | // Default number of iterations to run ICF is 2. | |
750 | unsigned int max_iterations = (parameters->options().icf_iterations() > 0) | |
751 | ? parameters->options().icf_iterations() | |
752 | : 2; | |
753 | ||
754 | bool converged = false; | |
755 | ||
756 | while (!converged && (num_iterations < max_iterations)) | |
757 | { | |
758 | num_iterations++; | |
759 | converged = match_sections(num_iterations, symtab, | |
2ea97941 | 760 | &num_tracked_relocs, &this->kept_section_id_, |
ef15dade ST |
761 | this->id_section_, &is_secn_or_group_unique, |
762 | §ion_contents); | |
763 | } | |
764 | ||
765 | if (parameters->options().print_icf_sections()) | |
766 | { | |
767 | if (converged) | |
768 | gold_info(_("%s: ICF Converged after %u iteration(s)"), | |
769 | program_name, num_iterations); | |
770 | else | |
771 | gold_info(_("%s: ICF stopped after %u iteration(s)"), | |
772 | program_name, num_iterations); | |
773 | } | |
774 | ||
48c187ce ST |
775 | // Unfold --keep-unique symbols. |
776 | for (options::String_set::const_iterator p = | |
777 | parameters->options().keep_unique_begin(); | |
778 | p != parameters->options().keep_unique_end(); | |
779 | ++p) | |
780 | { | |
781 | const char* name = p->c_str(); | |
782 | Symbol* sym = symtab->lookup(name); | |
ef5e0cb1 ST |
783 | if (sym == NULL) |
784 | { | |
785 | gold_warning(_("Could not find symbol %s to unfold\n"), name); | |
786 | } | |
787 | else if (sym->source() == Symbol::FROM_OBJECT | |
788 | && !sym->object()->is_dynamic()) | |
48c187ce ST |
789 | { |
790 | Object* obj = sym->object(); | |
791 | bool is_ordinary; | |
792 | unsigned int shndx = sym->shndx(&is_ordinary); | |
793 | if (is_ordinary) | |
794 | { | |
795 | this->unfold_section(obj, shndx); | |
796 | } | |
797 | } | |
798 | ||
799 | } | |
800 | ||
ef15dade ST |
801 | this->icf_ready(); |
802 | } | |
803 | ||
48c187ce ST |
804 | // Unfolds the section denoted by OBJ and SHNDX if folded. |
805 | ||
806 | void | |
807 | Icf::unfold_section(Object* obj, unsigned int shndx) | |
808 | { | |
809 | Section_id secn(obj, shndx); | |
810 | Uniq_secn_id_map::iterator it = this->section_id_.find(secn); | |
811 | if (it == this->section_id_.end()) | |
812 | return; | |
813 | unsigned int section_num = it->second; | |
814 | unsigned int kept_section_id = this->kept_section_id_[section_num]; | |
815 | if (kept_section_id != section_num) | |
816 | this->kept_section_id_[section_num] = section_num; | |
817 | } | |
818 | ||
ef15dade ST |
819 | // This function determines if the section corresponding to the |
820 | // given object and index is folded based on if the kept section | |
821 | // is different from this section. | |
822 | ||
823 | bool | |
824 | Icf::is_section_folded(Object* obj, unsigned int shndx) | |
825 | { | |
826 | Section_id secn(obj, shndx); | |
827 | Uniq_secn_id_map::iterator it = this->section_id_.find(secn); | |
828 | if (it == this->section_id_.end()) | |
829 | return false; | |
830 | unsigned int section_num = it->second; | |
831 | unsigned int kept_section_id = this->kept_section_id_[section_num]; | |
832 | return kept_section_id != section_num; | |
833 | } | |
834 | ||
835 | // This function returns the folded section for the given section. | |
836 | ||
837 | Section_id | |
838 | Icf::get_folded_section(Object* dup_obj, unsigned int dup_shndx) | |
839 | { | |
840 | Section_id dup_secn(dup_obj, dup_shndx); | |
841 | Uniq_secn_id_map::iterator it = this->section_id_.find(dup_secn); | |
842 | gold_assert(it != this->section_id_.end()); | |
843 | unsigned int section_num = it->second; | |
844 | unsigned int kept_section_id = this->kept_section_id_[section_num]; | |
845 | Section_id folded_section = this->id_section_[kept_section_id]; | |
846 | return folded_section; | |
847 | } | |
848 | ||
849 | } // End of namespace gold. |