PR ld/11931
[binutils.git] / gold / icf.cc
blob31312bbff9587a92ddbbd3d83535efc936be89d5
1 // icf.cc -- Identical Code Folding.
2 //
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Sriraman Tallam <tmsriram@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 // Identical Code Folding Algorithm
24 // ----------------------------------
25 // Detecting identical functions is done here and the basic algorithm
26 // is as follows. A checksum is computed on each foldable section using
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.
36 // However, two functions A and B with identical text but with
37 // relocations pointing to different foldable sections can be identical if
38 // the corresponding foldable sections to which their relocations point to
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 :
43 // int funcA () int funcB ()
44 // { {
45 // return foo(); return goo();
46 // } }
48 // The functions funcA and funcB are identical if functions foo() and
49 // goo() are identical.
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.
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 :
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 // } }
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.
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.
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.
96 // Caveat with using function pointers :
97 // ------------------------------------
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.
104 // Safe Folding :
105 // ------------
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.
112 // Caveat with safe folding :
113 // ------------------------
115 // This applies only to x86_64.
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.
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.
135 // How to run : --icf=[safe|all|none]
136 // Optional parameters : --icf-iterations <num> --print-icf-sections
138 // Performance : Less than 20 % link-time overhead on industry strength
139 // applications. Up to 6 % text size reductions.
141 #include "gold.h"
142 #include "object.h"
143 #include "gc.h"
144 #include "icf.h"
145 #include "symtab.h"
146 #include "libiberty.h"
147 #include "demangle.h"
148 #include "elfcpp.h"
149 #include "int_encoding.h"
151 namespace gold
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.
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)
171 Unordered_map<uint32_t, unsigned int> uniq_map;
172 std::pair<Unordered_map<uint32_t, unsigned int>::iterator, bool>
173 uniq_map_insert;
175 for (unsigned int i = 0; i < id_section.size(); i++)
177 if ((*is_secn_or_group_unique)[i])
178 continue;
180 uint32_t cksum;
181 Section_id secn = id_section[i];
182 section_size_type plen;
183 if (section_contents == NULL)
185 const unsigned char* contents;
186 contents = secn.first->section_contents(secn.second,
187 &plen,
188 false);
189 cksum = xcrc32(contents, plen, 0xffffffff);
191 else
193 const unsigned char* contents_array = reinterpret_cast
194 <const unsigned char*>((*section_contents)[i].c_str());
195 cksum = xcrc32(contents_array, (*section_contents)[i].length(),
196 0xffffffff);
198 uniq_map_insert = uniq_map.insert(std::make_pair(cksum, i));
199 if (uniq_map_insert.second)
201 (*is_secn_or_group_unique)[i] = true;
203 else
205 (*is_secn_or_group_unique)[i] = false;
206 (*is_secn_or_group_unique)[uniq_map_insert.first->second] = false;
211 // This returns the buffer containing the section's contents, both
212 // text and relocs. Relocs are differentiated as those pointing to
213 // sections that could be folded and those that cannot. Only relocs
214 // pointing to sections that could be folded are recomputed on
215 // subsequent invocations of this function.
216 // Parameters :
217 // FIRST_ITERATION : true if it is the first invocation.
218 // SECN : Section for which contents are desired.
219 // SECTION_NUM : Unique section number of this section.
220 // NUM_TRACKED_RELOCS : Vector reference to store the number of relocs
221 // to ICF sections.
222 // KEPT_SECTION_ID : Vector which maps folded sections to kept sections.
223 // SECTION_CONTENTS : Store the section's text and relocs to non-ICF
224 // sections.
226 static std::string
227 get_section_contents(bool first_iteration,
228 const Section_id& secn,
229 unsigned int section_num,
230 unsigned int* num_tracked_relocs,
231 Symbol_table* symtab,
232 const std::vector<unsigned int>& kept_section_id,
233 std::vector<std::string>* section_contents)
235 section_size_type plen;
236 const unsigned char* contents = NULL;
238 if (first_iteration)
240 contents = secn.first->section_contents(secn.second,
241 &plen,
242 false);
245 // The buffer to hold all the contents including relocs. A checksum
246 // is then computed on this buffer.
247 std::string buffer;
248 std::string icf_reloc_buffer;
250 if (num_tracked_relocs)
251 *num_tracked_relocs = 0;
253 Icf::Reloc_info_list& reloc_info_list =
254 symtab->icf()->reloc_info_list();
256 Icf::Reloc_info_list::iterator it_reloc_info_list =
257 reloc_info_list.find(secn);
259 buffer.clear();
260 icf_reloc_buffer.clear();
262 // Process relocs and put them into the buffer.
264 if (it_reloc_info_list != reloc_info_list.end())
266 Icf::Sections_reachable_info v =
267 (it_reloc_info_list->second).section_info;
268 // Stores the information of the symbol pointed to by the reloc.
269 Icf::Symbol_info s = (it_reloc_info_list->second).symbol_info;
270 // Stores the addend and the symbol value.
271 Icf::Addend_info a = (it_reloc_info_list->second).addend_info;
272 // Stores the offset of the reloc.
273 Icf::Offset_info o = (it_reloc_info_list->second).offset_info;
274 Icf::Reloc_addend_size_info reloc_addend_size_info =
275 (it_reloc_info_list->second).reloc_addend_size_info;
276 Icf::Sections_reachable_info::iterator it_v = v.begin();
277 Icf::Symbol_info::iterator it_s = s.begin();
278 Icf::Addend_info::iterator it_a = a.begin();
279 Icf::Offset_info::iterator it_o = o.begin();
280 Icf::Reloc_addend_size_info::iterator it_addend_size =
281 reloc_addend_size_info.begin();
283 for (; it_v != v.end(); ++it_v, ++it_s, ++it_a, ++it_o, ++it_addend_size)
285 // ADDEND_STR stores the symbol value and addend and offset,
286 // each atmost 16 hex digits long. it_a points to a pair
287 // where first is the symbol value and second is the
288 // addend.
289 char addend_str[50];
291 // It would be nice if we could use format macros in inttypes.h
292 // here but there are not in ISO/IEC C++ 1998.
293 snprintf(addend_str, sizeof(addend_str), "%llx %llx %llux",
294 static_cast<long long>((*it_a).first),
295 static_cast<long long>((*it_a).second),
296 static_cast<unsigned long long>(*it_o));
298 // If the symbol pointed to by the reloc is not in an ordinary
299 // section or if the symbol type is not FROM_OBJECT, then the
300 // object is NULL.
301 if (it_v->first == NULL)
303 if (first_iteration)
305 // If the symbol name is available, use it.
306 if ((*it_s) != NULL)
307 buffer.append((*it_s)->name());
308 // Append the addend.
309 buffer.append(addend_str);
310 buffer.append("@");
312 continue;
315 Section_id reloc_secn(it_v->first, it_v->second);
317 // If this reloc turns back and points to the same section,
318 // like a recursive call, use a special symbol to mark this.
319 if (reloc_secn.first == secn.first
320 && reloc_secn.second == secn.second)
322 if (first_iteration)
324 buffer.append("R");
325 buffer.append(addend_str);
326 buffer.append("@");
328 continue;
330 Icf::Uniq_secn_id_map& section_id_map =
331 symtab->icf()->section_to_int_map();
332 Icf::Uniq_secn_id_map::iterator section_id_map_it =
333 section_id_map.find(reloc_secn);
334 bool is_sym_preemptible = (*it_s != NULL
335 && !(*it_s)->is_from_dynobj()
336 && !(*it_s)->is_undefined()
337 && (*it_s)->is_preemptible());
338 if (!is_sym_preemptible
339 && section_id_map_it != section_id_map.end())
341 // This is a reloc to a section that might be folded.
342 if (num_tracked_relocs)
343 (*num_tracked_relocs)++;
345 char kept_section_str[10];
346 unsigned int secn_id = section_id_map_it->second;
347 snprintf(kept_section_str, sizeof(kept_section_str), "%u",
348 kept_section_id[secn_id]);
349 if (first_iteration)
351 buffer.append("ICF_R");
352 buffer.append(addend_str);
354 icf_reloc_buffer.append(kept_section_str);
355 // Append the addend.
356 icf_reloc_buffer.append(addend_str);
357 icf_reloc_buffer.append("@");
359 else
361 // This is a reloc to a section that cannot be folded.
362 // Process it only in the first iteration.
363 if (!first_iteration)
364 continue;
366 uint64_t secn_flags = (it_v->first)->section_flags(it_v->second);
367 // This reloc points to a merge section. Hash the
368 // contents of this section.
369 if ((secn_flags & elfcpp::SHF_MERGE) != 0)
371 uint64_t entsize =
372 (it_v->first)->section_entsize(it_v->second);
373 long long offset = it_a->first;
375 unsigned long long addend = it_a->second;
376 // Ignoring the addend when it is a negative value. See the
377 // comments in Merged_symbol_value::Value in object.h.
378 if (addend < 0xffffff00)
379 offset = offset + addend;
381 // For SHT_REL relocation sections, the addend is stored in the
382 // text section at the relocation offset.
383 uint64_t reloc_addend_value = 0;
384 const unsigned char* reloc_addend_ptr =
385 contents + static_cast<unsigned long long>(*it_o);
386 switch(*it_addend_size)
388 case 0:
390 break;
392 case 1:
394 reloc_addend_value =
395 read_from_pointer<8>(reloc_addend_ptr);
396 break;
398 case 2:
400 reloc_addend_value =
401 read_from_pointer<16>(reloc_addend_ptr);
402 break;
404 case 4:
406 reloc_addend_value =
407 read_from_pointer<32>(reloc_addend_ptr);
408 break;
410 case 8:
412 reloc_addend_value =
413 read_from_pointer<64>(reloc_addend_ptr);
414 break;
416 default:
417 gold_unreachable();
419 offset = offset + reloc_addend_value;
421 section_size_type secn_len;
422 const unsigned char* str_contents =
423 (it_v->first)->section_contents(it_v->second,
424 &secn_len,
425 false) + offset;
426 if ((secn_flags & elfcpp::SHF_STRINGS) != 0)
428 // String merge section.
429 const char* str_char =
430 reinterpret_cast<const char*>(str_contents);
431 switch(entsize)
433 case 1:
435 buffer.append(str_char);
436 break;
438 case 2:
440 const uint16_t* ptr_16 =
441 reinterpret_cast<const uint16_t*>(str_char);
442 unsigned int strlen_16 = 0;
443 // Find the NULL character.
444 while(*(ptr_16 + strlen_16) != 0)
445 strlen_16++;
446 buffer.append(str_char, strlen_16 * 2);
448 break;
449 case 4:
451 const uint32_t* ptr_32 =
452 reinterpret_cast<const uint32_t*>(str_char);
453 unsigned int strlen_32 = 0;
454 // Find the NULL character.
455 while(*(ptr_32 + strlen_32) != 0)
456 strlen_32++;
457 buffer.append(str_char, strlen_32 * 4);
459 break;
460 default:
461 gold_unreachable();
464 else
466 // Use the entsize to determine the length.
467 buffer.append(reinterpret_cast<const
468 char*>(str_contents),
469 entsize);
471 buffer.append("@");
473 else if ((*it_s) != NULL)
475 // If symbol name is available use that.
476 buffer.append((*it_s)->name());
477 // Append the addend.
478 buffer.append(addend_str);
479 buffer.append("@");
481 else
483 // Symbol name is not available, like for a local symbol,
484 // use object and section id.
485 buffer.append(it_v->first->name());
486 char secn_id[10];
487 snprintf(secn_id, sizeof(secn_id), "%u",it_v->second);
488 buffer.append(secn_id);
489 // Append the addend.
490 buffer.append(addend_str);
491 buffer.append("@");
497 if (first_iteration)
499 buffer.append("Contents = ");
500 buffer.append(reinterpret_cast<const char*>(contents), plen);
501 // Store the section contents that dont change to avoid recomputing
502 // during the next call to this function.
503 (*section_contents)[section_num] = buffer;
505 else
507 gold_assert(buffer.empty());
508 // Reuse the contents computed in the previous iteration.
509 buffer.append((*section_contents)[section_num]);
512 buffer.append(icf_reloc_buffer);
513 return buffer;
516 // This function computes a checksum on each section to detect and form
517 // groups of identical sections. The first iteration does this for all
518 // sections.
519 // Further iterations do this only for the kept sections from each group to
520 // determine if larger groups of identical sections could be formed. The
521 // first section in each group is the kept section for that group.
523 // CRC32 is the checksumming algorithm and can have collisions. That is,
524 // two sections with different contents can have the same checksum. Hence,
525 // a multimap is used to maintain more than one group of checksum
526 // identical sections. A section is added to a group only after its
527 // contents are explicitly compared with the kept section of the group.
529 // Parameters :
530 // ITERATION_NUM : Invocation instance of this function.
531 // NUM_TRACKED_RELOCS : Vector reference to store the number of relocs
532 // to ICF sections.
533 // KEPT_SECTION_ID : Vector which maps folded sections to kept sections.
534 // ID_SECTION : Vector mapping a section to an unique integer.
535 // IS_SECN_OR_GROUP_UNIQUE : To check if a section or a group of identical
536 // sectionsis already known to be unique.
537 // SECTION_CONTENTS : Store the section's text and relocs to non-ICF
538 // sections.
540 static bool
541 match_sections(unsigned int iteration_num,
542 Symbol_table* symtab,
543 std::vector<unsigned int>* num_tracked_relocs,
544 std::vector<unsigned int>* kept_section_id,
545 const std::vector<Section_id>& id_section,
546 std::vector<bool>* is_secn_or_group_unique,
547 std::vector<std::string>* section_contents)
549 Unordered_multimap<uint32_t, unsigned int> section_cksum;
550 std::pair<Unordered_multimap<uint32_t, unsigned int>::iterator,
551 Unordered_multimap<uint32_t, unsigned int>::iterator> key_range;
552 bool converged = true;
554 if (iteration_num == 1)
555 preprocess_for_unique_sections(id_section,
556 is_secn_or_group_unique,
557 NULL);
558 else
559 preprocess_for_unique_sections(id_section,
560 is_secn_or_group_unique,
561 section_contents);
563 std::vector<std::string> full_section_contents;
565 for (unsigned int i = 0; i < id_section.size(); i++)
567 full_section_contents.push_back("");
568 if ((*is_secn_or_group_unique)[i])
569 continue;
571 Section_id secn = id_section[i];
572 std::string this_secn_contents;
573 uint32_t cksum;
574 if (iteration_num == 1)
576 unsigned int num_relocs = 0;
577 this_secn_contents = get_section_contents(true, secn, i, &num_relocs,
578 symtab, (*kept_section_id),
579 section_contents);
580 (*num_tracked_relocs)[i] = num_relocs;
582 else
584 if ((*kept_section_id)[i] != i)
586 // This section is already folded into something. See
587 // if it should point to a different kept section.
588 unsigned int kept_section = (*kept_section_id)[i];
589 if (kept_section != (*kept_section_id)[kept_section])
591 (*kept_section_id)[i] = (*kept_section_id)[kept_section];
593 continue;
595 this_secn_contents = get_section_contents(false, secn, i, NULL,
596 symtab, (*kept_section_id),
597 section_contents);
600 const unsigned char* this_secn_contents_array =
601 reinterpret_cast<const unsigned char*>(this_secn_contents.c_str());
602 cksum = xcrc32(this_secn_contents_array, this_secn_contents.length(),
603 0xffffffff);
604 size_t count = section_cksum.count(cksum);
606 if (count == 0)
608 // Start a group with this cksum.
609 section_cksum.insert(std::make_pair(cksum, i));
610 full_section_contents[i] = this_secn_contents;
612 else
614 key_range = section_cksum.equal_range(cksum);
615 Unordered_multimap<uint32_t, unsigned int>::iterator it;
616 // Search all the groups with this cksum for a match.
617 for (it = key_range.first; it != key_range.second; ++it)
619 unsigned int kept_section = it->second;
620 if (full_section_contents[kept_section].length()
621 != this_secn_contents.length())
622 continue;
623 if (memcmp(full_section_contents[kept_section].c_str(),
624 this_secn_contents.c_str(),
625 this_secn_contents.length()) != 0)
626 continue;
627 (*kept_section_id)[i] = kept_section;
628 converged = false;
629 break;
631 if (it == key_range.second)
633 // Create a new group for this cksum.
634 section_cksum.insert(std::make_pair(cksum, i));
635 full_section_contents[i] = this_secn_contents;
638 // If there are no relocs to foldable sections do not process
639 // this section any further.
640 if (iteration_num == 1 && (*num_tracked_relocs)[i] == 0)
641 (*is_secn_or_group_unique)[i] = true;
644 return converged;
647 // During safe icf (--icf=safe), only fold functions that are ctors or dtors.
648 // This function returns true if the mangled function name is a ctor or a
649 // dtor.
651 static bool
652 is_function_ctor_or_dtor(const char* mangled_func_name)
654 if ((is_prefix_of("_ZN", mangled_func_name)
655 || is_prefix_of("_ZZ", mangled_func_name))
656 && (is_gnu_v3_mangled_ctor(mangled_func_name)
657 || is_gnu_v3_mangled_dtor(mangled_func_name)))
659 return true;
661 return false;
664 // This is the main ICF function called in gold.cc. This does the
665 // initialization and calls match_sections repeatedly (twice by default)
666 // which computes the crc checksums and detects identical functions.
668 void
669 Icf::find_identical_sections(const Input_objects* input_objects,
670 Symbol_table* symtab)
672 unsigned int section_num = 0;
673 std::vector<unsigned int> num_tracked_relocs;
674 std::vector<bool> is_secn_or_group_unique;
675 std::vector<std::string> section_contents;
676 const Target& target = parameters->target();
678 // Decide which sections are possible candidates first.
680 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
681 p != input_objects->relobj_end();
682 ++p)
684 for (unsigned int i = 0;i < (*p)->shnum(); ++i)
686 const char* section_name = (*p)->section_name(i).c_str();
687 if (!is_section_foldable_candidate(section_name))
688 continue;
689 if (!(*p)->is_section_included(i))
690 continue;
691 if (parameters->options().gc_sections()
692 && symtab->gc()->is_section_garbage(*p, i))
693 continue;
694 const char* mangled_func_name = strrchr(section_name, '.');
695 gold_assert(mangled_func_name != NULL);
696 // With --icf=safe, check if the mangled function name is a ctor
697 // or a dtor. The mangled function name can be obtained from the
698 // section name by stripping the section prefix.
699 if (parameters->options().icf_safe_folding()
700 && !is_function_ctor_or_dtor(mangled_func_name + 1)
701 && (!target.can_check_for_function_pointers()
702 || section_has_function_pointers(*p, i)))
704 continue;
706 this->id_section_.push_back(Section_id(*p, i));
707 this->section_id_[Section_id(*p, i)] = section_num;
708 this->kept_section_id_.push_back(section_num);
709 num_tracked_relocs.push_back(0);
710 is_secn_or_group_unique.push_back(false);
711 section_contents.push_back("");
712 section_num++;
716 unsigned int num_iterations = 0;
718 // Default number of iterations to run ICF is 2.
719 unsigned int max_iterations = (parameters->options().icf_iterations() > 0)
720 ? parameters->options().icf_iterations()
721 : 2;
723 bool converged = false;
725 while (!converged && (num_iterations < max_iterations))
727 num_iterations++;
728 converged = match_sections(num_iterations, symtab,
729 &num_tracked_relocs, &this->kept_section_id_,
730 this->id_section_, &is_secn_or_group_unique,
731 &section_contents);
734 if (parameters->options().print_icf_sections())
736 if (converged)
737 gold_info(_("%s: ICF Converged after %u iteration(s)"),
738 program_name, num_iterations);
739 else
740 gold_info(_("%s: ICF stopped after %u iteration(s)"),
741 program_name, num_iterations);
744 // Unfold --keep-unique symbols.
745 for (options::String_set::const_iterator p =
746 parameters->options().keep_unique_begin();
747 p != parameters->options().keep_unique_end();
748 ++p)
750 const char* name = p->c_str();
751 Symbol* sym = symtab->lookup(name);
752 if (sym == NULL)
754 gold_warning(_("Could not find symbol %s to unfold\n"), name);
756 else if (sym->source() == Symbol::FROM_OBJECT
757 && !sym->object()->is_dynamic())
759 Object* obj = sym->object();
760 bool is_ordinary;
761 unsigned int shndx = sym->shndx(&is_ordinary);
762 if (is_ordinary)
764 this->unfold_section(obj, shndx);
770 this->icf_ready();
773 // Unfolds the section denoted by OBJ and SHNDX if folded.
775 void
776 Icf::unfold_section(Object* obj, unsigned int shndx)
778 Section_id secn(obj, shndx);
779 Uniq_secn_id_map::iterator it = this->section_id_.find(secn);
780 if (it == this->section_id_.end())
781 return;
782 unsigned int section_num = it->second;
783 unsigned int kept_section_id = this->kept_section_id_[section_num];
784 if (kept_section_id != section_num)
785 this->kept_section_id_[section_num] = section_num;
788 // This function determines if the section corresponding to the
789 // given object and index is folded based on if the kept section
790 // is different from this section.
792 bool
793 Icf::is_section_folded(Object* obj, unsigned int shndx)
795 Section_id secn(obj, shndx);
796 Uniq_secn_id_map::iterator it = this->section_id_.find(secn);
797 if (it == this->section_id_.end())
798 return false;
799 unsigned int section_num = it->second;
800 unsigned int kept_section_id = this->kept_section_id_[section_num];
801 return kept_section_id != section_num;
804 // This function returns the folded section for the given section.
806 Section_id
807 Icf::get_folded_section(Object* dup_obj, unsigned int dup_shndx)
809 Section_id dup_secn(dup_obj, dup_shndx);
810 Uniq_secn_id_map::iterator it = this->section_id_.find(dup_secn);
811 gold_assert(it != this->section_id_.end());
812 unsigned int section_num = it->second;
813 unsigned int kept_section_id = this->kept_section_id_[section_num];
814 Section_id folded_section = this->id_section_[kept_section_id];
815 return folded_section;
818 } // End of namespace gold.