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