icf.cc 33 KB

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