2010-02-20 Sriraman Tallam <tmsriram@google.com>
[external/binutils.git] / gold / icf.cc
1 // icf.cc -- Identical Code Folding.
2 //
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
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
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.
35 //
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 :
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 //
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 //
134 //
135 // How to run  : --icf=[safe|all|none]
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"
147 #include "demangle.h"
148
149 namespace gold
150 {
151
152 // This function determines if a section or a group of identical
153 // sections has unique contents.  Such unique sections or groups can be
154 // declared final and need not be processed any further.
155 // Parameters :
156 // ID_SECTION : Vector mapping a section index to a Section_id pair.
157 // IS_SECN_OR_GROUP_UNIQUE : To check if a section or a group of identical
158 //                            sections is already known to be unique.
159 // SECTION_CONTENTS : Contains the section's text and relocs to sections
160 //                    that cannot be folded.   SECTION_CONTENTS are NULL
161 //                    implies that this function is being called for the
162 //                    first time before the first iteration of icf.
163
164 static void
165 preprocess_for_unique_sections(const std::vector<Section_id>& id_section,
166                                std::vector<bool>* is_secn_or_group_unique,
167                                std::vector<std::string>* section_contents)
168 {
169   Unordered_map<uint32_t, unsigned int> uniq_map;
170   std::pair<Unordered_map<uint32_t, unsigned int>::iterator, bool>
171     uniq_map_insert;
172
173   for (unsigned int i = 0; i < id_section.size(); i++)
174     {
175       if ((*is_secn_or_group_unique)[i])
176         continue;
177
178       uint32_t cksum;
179       Section_id secn = id_section[i];
180       section_size_type plen;
181       if (section_contents == NULL)
182         {
183           const unsigned char* contents;
184           contents = secn.first->section_contents(secn.second,
185                                                   &plen,
186                                                   false);
187           cksum = xcrc32(contents, plen, 0xffffffff);
188         }
189       else
190         {
191           const unsigned char* contents_array = reinterpret_cast
192             <const unsigned char*>((*section_contents)[i].c_str());
193           cksum = xcrc32(contents_array, (*section_contents)[i].length(),
194                          0xffffffff);
195         }
196       uniq_map_insert = uniq_map.insert(std::make_pair(cksum, i));
197       if (uniq_map_insert.second)
198         {
199           (*is_secn_or_group_unique)[i] = true;
200         }
201       else
202         {
203           (*is_secn_or_group_unique)[i] = false;
204           (*is_secn_or_group_unique)[uniq_map_insert.first->second] = false;
205         }
206     }
207 }
208
209 // This returns the buffer containing the section's contents, both
210 // text and relocs.  Relocs are differentiated as those pointing to
211 // sections that could be folded and those that cannot.  Only relocs
212 // pointing to sections that could be folded are recomputed on
213 // subsequent invocations of this function.
214 // Parameters  :
215 // FIRST_ITERATION    : true if it is the first invocation.
216 // SECN               : Section for which contents are desired.
217 // SECTION_NUM        : Unique section number of this section.
218 // NUM_TRACKED_RELOCS : Vector reference to store the number of relocs
219 //                      to ICF sections.
220 // KEPT_SECTION_ID    : Vector which maps folded sections to kept sections.
221 // SECTION_CONTENTS   : Store the section's text and relocs to non-ICF
222 //                      sections.
223
224 static std::string
225 get_section_contents(bool first_iteration,
226                      const Section_id& secn,
227                      unsigned int section_num,
228                      unsigned int* num_tracked_relocs,
229                      Symbol_table* symtab,
230                      const std::vector<unsigned int>& kept_section_id,
231                      std::vector<std::string>* section_contents)
232 {
233   section_size_type plen;
234   const unsigned char* contents = NULL;
235
236   if (first_iteration)
237     {
238       contents = secn.first->section_contents(secn.second,
239                                               &plen,
240                                               false);
241     }
242
243   // The buffer to hold all the contents including relocs.  A checksum
244   // is then computed on this buffer.
245   std::string buffer;
246   std::string icf_reloc_buffer;
247
248   if (num_tracked_relocs)
249     *num_tracked_relocs = 0;
250
251   Icf::Reloc_info_list& reloc_info_list = 
252     symtab->icf()->reloc_info_list();
253
254   Icf::Reloc_info_list::iterator it_reloc_info_list =
255     reloc_info_list.find(secn);
256
257   buffer.clear();
258   icf_reloc_buffer.clear();
259
260   // Process relocs and put them into the buffer.
261
262   if (it_reloc_info_list != reloc_info_list.end())
263     {
264       Icf::Sections_reachable_info v =
265         (it_reloc_info_list->second).section_info;
266       Icf::Symbol_info s = (it_reloc_info_list->second).symbol_info;
267       Icf::Addend_info a = (it_reloc_info_list->second).addend_info;
268       Icf::Offset_info o = (it_reloc_info_list->second).offset_info;
269       Icf::Sections_reachable_info::iterator it_v = v.begin();
270       Icf::Symbol_info::iterator it_s = s.begin();
271       Icf::Addend_info::iterator it_a = a.begin();
272       Icf::Offset_info::iterator it_o = o.begin();
273
274       for (; it_v != v.end(); ++it_v, ++it_s, ++it_a, ++it_o)
275         {
276           // ADDEND_STR stores the symbol value and addend and offset,
277           // each atmost 16 hex digits long.  it_a points to a pair
278           // where first is the symbol value and second is the
279           // addend.
280           char addend_str[50];
281           snprintf(addend_str, sizeof(addend_str), "%llx %llx %lux",
282                    (*it_a).first, (*it_a).second, (*it_o));
283           Section_id reloc_secn(it_v->first, it_v->second);
284
285           // If this reloc turns back and points to the same section,
286           // like a recursive call, use a special symbol to mark this.
287           if (reloc_secn.first == secn.first
288               && reloc_secn.second == secn.second)
289             {
290               if (first_iteration)
291                 {
292                   buffer.append("R");
293                   buffer.append(addend_str);
294                   buffer.append("@");
295                 }
296               continue;
297             }
298           Icf::Uniq_secn_id_map& section_id_map =
299             symtab->icf()->section_to_int_map();
300           Icf::Uniq_secn_id_map::iterator section_id_map_it =
301             section_id_map.find(reloc_secn);
302           if (section_id_map_it != section_id_map.end())
303             {
304               // This is a reloc to a section that might be folded.
305               if (num_tracked_relocs)
306                 (*num_tracked_relocs)++;
307
308               char kept_section_str[10];
309               unsigned int secn_id = section_id_map_it->second;
310               snprintf(kept_section_str, sizeof(kept_section_str), "%u",
311                        kept_section_id[secn_id]);
312               if (first_iteration)
313                 {
314                   buffer.append("ICF_R");
315                   buffer.append(addend_str);
316                 }
317               icf_reloc_buffer.append(kept_section_str);
318               // Append the addend.
319               icf_reloc_buffer.append(addend_str);
320               icf_reloc_buffer.append("@");
321             }
322           else
323             {
324               // This is a reloc to a section that cannot be folded.
325               // Process it only in the first iteration.
326               if (!first_iteration)
327                 continue;
328
329               uint64_t secn_flags = (it_v->first)->section_flags(it_v->second);
330               // This reloc points to a merge section.  Hash the
331               // contents of this section.
332               if ((secn_flags & elfcpp::SHF_MERGE) != 0)
333                 {
334                   uint64_t entsize =
335                     (it_v->first)->section_entsize(it_v->second);
336                   long long offset = it_a->first + it_a->second;
337                   section_size_type secn_len;
338                   const unsigned char* str_contents =
339                   (it_v->first)->section_contents(it_v->second,
340                                                   &secn_len,
341                                                   false) + offset;
342                   if ((secn_flags & elfcpp::SHF_STRINGS) != 0)
343                     {
344                       // String merge section.
345                       const char* str_char =
346                         reinterpret_cast<const char*>(str_contents);
347                       switch(entsize)
348                         {
349                         case 1:
350                           {
351                             buffer.append(str_char);
352                             break;
353                           }
354                         case 2:
355                           {
356                             const uint16_t* ptr_16 =
357                               reinterpret_cast<const uint16_t*>(str_char);
358                             unsigned int strlen_16 = 0;
359                             // Find the NULL character.
360                             while(*(ptr_16 + strlen_16) != 0)
361                                 strlen_16++;
362                             buffer.append(str_char, strlen_16 * 2);
363                           }
364                           break;
365                         case 4:
366                           {
367                             const uint32_t* ptr_32 =
368                               reinterpret_cast<const uint32_t*>(str_char);
369                             unsigned int strlen_32 = 0;
370                             // Find the NULL character.
371                             while(*(ptr_32 + strlen_32) != 0)
372                                 strlen_32++;
373                             buffer.append(str_char, strlen_32 * 4);
374                           }
375                           break;
376                         default:
377                           gold_unreachable();
378                         }
379                     }
380                   else
381                     {
382                       // Use the entsize to determine the length.
383                       buffer.append(reinterpret_cast<const 
384                                                      char*>(str_contents),
385                                     entsize);
386                     }
387                 }
388               else if ((*it_s) != NULL)
389                 {
390                   // If symbol name is available use that.
391                   const char *sym_name = (*it_s)->name();
392                   buffer.append(sym_name);
393                   // Append the addend.
394                   buffer.append(addend_str);
395                   buffer.append("@");
396                 }
397               else
398                 {
399                   // Symbol name is not available, like for a local symbol,
400                   // use object and section id.
401                   buffer.append(it_v->first->name());
402                   char secn_id[10];
403                   snprintf(secn_id, sizeof(secn_id), "%u",it_v->second);
404                   buffer.append(secn_id);
405                   // Append the addend.
406                   buffer.append(addend_str);
407                   buffer.append("@");
408                 }
409             }
410         }
411     }
412
413   if (first_iteration)
414     {
415       buffer.append("Contents = ");
416       buffer.append(reinterpret_cast<const char*>(contents), plen);
417       // Store the section contents that dont change to avoid recomputing
418       // during the next call to this function.
419       (*section_contents)[section_num] = buffer;
420     }
421   else
422     {
423       gold_assert(buffer.empty());
424       // Reuse the contents computed in the previous iteration.
425       buffer.append((*section_contents)[section_num]);
426     }
427
428   buffer.append(icf_reloc_buffer);
429   return buffer;
430 }
431
432 // This function computes a checksum on each section to detect and form
433 // groups of identical sections.  The first iteration does this for all 
434 // sections.
435 // Further iterations do this only for the kept sections from each group to
436 // determine if larger groups of identical sections could be formed.  The
437 // first section in each group is the kept section for that group.
438 //
439 // CRC32 is the checksumming algorithm and can have collisions.  That is,
440 // two sections with different contents can have the same checksum. Hence,
441 // a multimap is used to maintain more than one group of checksum
442 // identical sections.  A section is added to a group only after its
443 // contents are explicitly compared with the kept section of the group.
444 //
445 // Parameters  :
446 // ITERATION_NUM           : Invocation instance of this function.
447 // NUM_TRACKED_RELOCS : Vector reference to store the number of relocs
448 //                      to ICF sections.
449 // KEPT_SECTION_ID    : Vector which maps folded sections to kept sections.
450 // ID_SECTION         : Vector mapping a section to an unique integer.
451 // IS_SECN_OR_GROUP_UNIQUE : To check if a section or a group of identical
452 //                            sectionsis already known to be unique.
453 // SECTION_CONTENTS   : Store the section's text and relocs to non-ICF
454 //                      sections.
455
456 static bool
457 match_sections(unsigned int iteration_num,
458                Symbol_table* symtab,
459                std::vector<unsigned int>* num_tracked_relocs,
460                std::vector<unsigned int>* kept_section_id,
461                const std::vector<Section_id>& id_section,
462                std::vector<bool>* is_secn_or_group_unique,
463                std::vector<std::string>* section_contents)
464 {
465   Unordered_multimap<uint32_t, unsigned int> section_cksum;
466   std::pair<Unordered_multimap<uint32_t, unsigned int>::iterator,
467             Unordered_multimap<uint32_t, unsigned int>::iterator> key_range;
468   bool converged = true;
469
470   if (iteration_num == 1)
471     preprocess_for_unique_sections(id_section,
472                                    is_secn_or_group_unique,
473                                    NULL);
474   else
475     preprocess_for_unique_sections(id_section,
476                                    is_secn_or_group_unique,
477                                    section_contents);
478
479   std::vector<std::string> full_section_contents;
480
481   for (unsigned int i = 0; i < id_section.size(); i++)
482     {
483       full_section_contents.push_back("");
484       if ((*is_secn_or_group_unique)[i])
485         continue;
486
487       Section_id secn = id_section[i];
488       std::string this_secn_contents;
489       uint32_t cksum;
490       if (iteration_num == 1)
491         {
492           unsigned int num_relocs = 0;
493           this_secn_contents = get_section_contents(true, secn, i, &num_relocs,
494                                                     symtab, (*kept_section_id),
495                                                     section_contents);
496           (*num_tracked_relocs)[i] = num_relocs;
497         }
498       else
499         {
500           if ((*kept_section_id)[i] != i)
501             {
502               // This section is already folded into something.  See
503               // if it should point to a different kept section.
504               unsigned int kept_section = (*kept_section_id)[i];
505               if (kept_section != (*kept_section_id)[kept_section])
506                 {
507                   (*kept_section_id)[i] = (*kept_section_id)[kept_section];
508                 }
509               continue;
510             }
511           this_secn_contents = get_section_contents(false, secn, i, NULL,
512                                                     symtab, (*kept_section_id),
513                                                     section_contents);
514         }
515
516       const unsigned char* this_secn_contents_array =
517             reinterpret_cast<const unsigned char*>(this_secn_contents.c_str());
518       cksum = xcrc32(this_secn_contents_array, this_secn_contents.length(),
519                      0xffffffff);
520       size_t count = section_cksum.count(cksum);
521
522       if (count == 0)
523         {
524           // Start a group with this cksum.
525           section_cksum.insert(std::make_pair(cksum, i));
526           full_section_contents[i] = this_secn_contents;
527         }
528       else
529         {
530           key_range = section_cksum.equal_range(cksum);
531           Unordered_multimap<uint32_t, unsigned int>::iterator it;
532           // Search all the groups with this cksum for a match.
533           for (it = key_range.first; it != key_range.second; ++it)
534             {
535               unsigned int kept_section = it->second;
536               if (full_section_contents[kept_section].length()
537                   != this_secn_contents.length())
538                   continue;
539               if (memcmp(full_section_contents[kept_section].c_str(),
540                          this_secn_contents.c_str(),
541                          this_secn_contents.length()) != 0)
542                   continue;
543               (*kept_section_id)[i] = kept_section;
544               converged = false;
545               break;
546             }
547           if (it == key_range.second)
548             {
549               // Create a new group for this cksum.
550               section_cksum.insert(std::make_pair(cksum, i));
551               full_section_contents[i] = this_secn_contents;
552             }
553         }
554       // If there are no relocs to foldable sections do not process
555       // this section any further.
556       if (iteration_num == 1 && (*num_tracked_relocs)[i] == 0)
557         (*is_secn_or_group_unique)[i] = true;
558     }
559
560   return converged;
561 }
562
563 // During safe icf (--icf=safe), only fold functions that are ctors or dtors.
564 // This function returns true if the mangled function name is a ctor or a
565 // dtor.
566
567 static bool
568 is_function_ctor_or_dtor(const char* mangled_func_name)
569 {
570   if ((is_prefix_of("_ZN", mangled_func_name)
571        || is_prefix_of("_ZZ", mangled_func_name))
572       && (is_gnu_v3_mangled_ctor(mangled_func_name)
573           || is_gnu_v3_mangled_dtor(mangled_func_name)))
574     {
575       return true;
576     }
577   return false;
578 }
579
580 // This is the main ICF function called in gold.cc.  This does the
581 // initialization and calls match_sections repeatedly (twice by default)
582 // which computes the crc checksums and detects identical functions.
583
584 void
585 Icf::find_identical_sections(const Input_objects* input_objects,
586                              Symbol_table* symtab)
587 {
588   unsigned int section_num = 0;
589   std::vector<unsigned int> num_tracked_relocs;
590   std::vector<bool> is_secn_or_group_unique;
591   std::vector<std::string> section_contents;
592   const Target& target = parameters->target();
593
594   // Decide which sections are possible candidates first.
595
596   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
597        p != input_objects->relobj_end();
598        ++p)
599     {
600       for (unsigned int i = 0;i < (*p)->shnum(); ++i)
601         {
602           const char* section_name = (*p)->section_name(i).c_str();
603           if (!is_section_foldable_candidate(section_name))
604             continue;
605           if (!(*p)->is_section_included(i))
606             continue;
607           if (parameters->options().gc_sections()
608               && symtab->gc()->is_section_garbage(*p, i))
609               continue;
610           const char* mangled_func_name = strrchr(section_name, '.');
611           gold_assert(mangled_func_name != NULL);
612           // With --icf=safe, check if the mangled function name is a ctor
613           // or a dtor.  The mangled function name can be obtained from the
614           // section name by stripping the section prefix.
615           if (parameters->options().icf_safe_folding()
616               && !is_function_ctor_or_dtor(mangled_func_name + 1)
617               && (!target.can_check_for_function_pointers()
618                   || section_has_function_pointers(*p, i)))
619             {
620               continue;
621             }
622           this->id_section_.push_back(Section_id(*p, i));
623           this->section_id_[Section_id(*p, i)] = section_num;
624           this->kept_section_id_.push_back(section_num);
625           num_tracked_relocs.push_back(0);
626           is_secn_or_group_unique.push_back(false);
627           section_contents.push_back("");
628           section_num++;
629         }
630     }
631
632   unsigned int num_iterations = 0;
633
634   // Default number of iterations to run ICF is 2.
635   unsigned int max_iterations = (parameters->options().icf_iterations() > 0)
636                             ? parameters->options().icf_iterations()
637                             : 2;
638
639   bool converged = false;
640
641   while (!converged && (num_iterations < max_iterations))
642     {
643       num_iterations++;
644       converged = match_sections(num_iterations, symtab,
645                                  &num_tracked_relocs, &this->kept_section_id_,
646                                  this->id_section_, &is_secn_or_group_unique,
647                                  &section_contents);
648     }
649
650   if (parameters->options().print_icf_sections())
651     {
652       if (converged)
653         gold_info(_("%s: ICF Converged after %u iteration(s)"),
654                   program_name, num_iterations);
655       else
656         gold_info(_("%s: ICF stopped after %u iteration(s)"),
657                   program_name, num_iterations);
658     }
659
660   // Unfold --keep-unique symbols.
661   for (options::String_set::const_iterator p =
662          parameters->options().keep_unique_begin();
663        p != parameters->options().keep_unique_end();
664        ++p)
665     {
666       const char* name = p->c_str();
667       Symbol* sym = symtab->lookup(name);
668       if (sym == NULL)
669         {
670           gold_warning(_("Could not find symbol %s to unfold\n"), name);
671         }
672       else if (sym->source() == Symbol::FROM_OBJECT 
673                && !sym->object()->is_dynamic())
674         {
675           Object* obj = sym->object();
676           bool is_ordinary;
677           unsigned int shndx = sym->shndx(&is_ordinary);
678           if (is_ordinary)
679             {
680               this->unfold_section(obj, shndx);
681             }
682         }
683
684     }
685
686   this->icf_ready();
687 }
688
689 // Unfolds the section denoted by OBJ and SHNDX if folded.
690
691 void
692 Icf::unfold_section(Object* obj, unsigned int shndx)
693 {
694   Section_id secn(obj, shndx);
695   Uniq_secn_id_map::iterator it = this->section_id_.find(secn);
696   if (it == this->section_id_.end())
697     return;
698   unsigned int section_num = it->second;
699   unsigned int kept_section_id = this->kept_section_id_[section_num];
700   if (kept_section_id != section_num)
701     this->kept_section_id_[section_num] = section_num;
702 }
703
704 // This function determines if the section corresponding to the
705 // given object and index is folded based on if the kept section
706 // is different from this section.
707
708 bool
709 Icf::is_section_folded(Object* obj, unsigned int shndx)
710 {
711   Section_id secn(obj, shndx);
712   Uniq_secn_id_map::iterator it = this->section_id_.find(secn);
713   if (it == this->section_id_.end())
714     return false;
715   unsigned int section_num = it->second;
716   unsigned int kept_section_id = this->kept_section_id_[section_num];
717   return kept_section_id != section_num;
718 }
719
720 // This function returns the folded section for the given section.
721
722 Section_id
723 Icf::get_folded_section(Object* dup_obj, unsigned int dup_shndx)
724 {
725   Section_id dup_secn(dup_obj, dup_shndx);
726   Uniq_secn_id_map::iterator it = this->section_id_.find(dup_secn);
727   gold_assert(it != this->section_id_.end());
728   unsigned int section_num = it->second;
729   unsigned int kept_section_id = this->kept_section_id_[section_num];
730   Section_id folded_section = this->id_section_[kept_section_id];
731   return folded_section;
732 }
733
734 } // End of namespace gold.