icf.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // icf.h -- Identical Code Folding
  2. // Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. // Written by Sriraman Tallam <tmsriram@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #ifndef GOLD_ICF_H
  18. #define GOLD_ICF_H
  19. #include <vector>
  20. #include "elfcpp.h"
  21. #include "symtab.h"
  22. #include "object.h"
  23. namespace gold
  24. {
  25. class Object;
  26. class Input_objects;
  27. class Symbol_table;
  28. class Icf
  29. {
  30. public:
  31. typedef std::vector<Section_id> Sections_reachable_info;
  32. typedef std::vector<Symbol*> Symbol_info;
  33. typedef std::vector<std::pair<long long, long long> > Addend_info;
  34. typedef std::vector<uint64_t> Offset_info;
  35. typedef std::vector<unsigned int> Reloc_addend_size_info;
  36. typedef Unordered_map<Section_id,
  37. unsigned int,
  38. Section_id_hash> Uniq_secn_id_map;
  39. typedef Unordered_set<Section_id, Section_id_hash> Secn_fptr_taken_set;
  40. typedef struct
  41. {
  42. // This stores the section corresponding to the reloc.
  43. Sections_reachable_info section_info;
  44. // This stores the symbol corresponding to the reloc.
  45. Symbol_info symbol_info;
  46. // This stores the symbol value and the addend for a reloc.
  47. Addend_info addend_info;
  48. Offset_info offset_info;
  49. Reloc_addend_size_info reloc_addend_size_info;
  50. } Reloc_info;
  51. typedef Unordered_map<Section_id, Reloc_info,
  52. Section_id_hash> Reloc_info_list;
  53. // A region of some other section that should be considered part of
  54. // a section for ICF purposes. This is used to avoid folding sections
  55. // that have identical text and relocations but different .eh_frame
  56. // information.
  57. typedef struct
  58. {
  59. Section_id section;
  60. section_offset_type offset;
  61. section_size_type length;
  62. } Extra_identity_info;
  63. typedef std::multimap<Section_id, Extra_identity_info> Extra_identity_list;
  64. Icf()
  65. : id_section_(), section_id_(), kept_section_id_(),
  66. fptr_section_id_(),
  67. icf_ready_(false),
  68. reloc_info_list_()
  69. { }
  70. // Returns the kept folded identical section corresponding to
  71. // dup_obj and dup_shndx.
  72. Section_id
  73. get_folded_section(Relobj* dup_obj, unsigned int dup_shndx);
  74. // Forms groups of identical sections where the first member
  75. // of each group is the kept section during folding.
  76. void
  77. find_identical_sections(const Input_objects* input_objects,
  78. Symbol_table* symtab);
  79. // This is set when ICF has been run and the groups of
  80. // identical sections have been formed.
  81. void
  82. icf_ready()
  83. { this->icf_ready_ = true; }
  84. // Returns true if ICF has been run.
  85. bool
  86. is_icf_ready()
  87. { return this->icf_ready_; }
  88. // Unfolds the section denoted by OBJ and SHNDX if folded.
  89. void
  90. unfold_section(Relobj* obj, unsigned int shndx);
  91. // Returns the kept section corresponding to the
  92. // given section.
  93. bool
  94. is_section_folded(Relobj* obj, unsigned int shndx);
  95. // Given an object and a section index, this returns true if the
  96. // pointer of the function defined in this section is taken.
  97. bool
  98. section_has_function_pointers(Relobj* obj, unsigned int shndx)
  99. {
  100. return (this->fptr_section_id_.find(Section_id(obj, shndx))
  101. != this->fptr_section_id_.end());
  102. }
  103. // Records that a pointer of the function defined in this section
  104. // is taken.
  105. void
  106. set_section_has_function_pointers(Relobj* obj, unsigned int shndx)
  107. {
  108. this->fptr_section_id_.insert(Section_id(obj, shndx));
  109. }
  110. // Checks if the section_name should be searched for relocs
  111. // corresponding to taken function pointers. Ignores eh_frame
  112. // and vtable sections.
  113. inline bool
  114. check_section_for_function_pointers(const std::string& section_name,
  115. Target* target)
  116. {
  117. return (parameters->options().icf_safe_folding()
  118. && target->can_check_for_function_pointers()
  119. && target->section_may_have_icf_unsafe_pointers(
  120. section_name.c_str()));
  121. }
  122. // Returns a map of a section to info (Reloc_info) about its relocations.
  123. Reloc_info_list&
  124. reloc_info_list()
  125. { return this->reloc_info_list_; }
  126. // Returns a map from section to region of a different section that should
  127. // be considered part of the key section for ICF purposes.
  128. Extra_identity_list&
  129. extra_identity_list()
  130. { return this->extra_identity_list_; }
  131. // Returns a mapping of each section to a unique integer.
  132. Uniq_secn_id_map&
  133. section_to_int_map()
  134. { return this->section_id_; }
  135. private:
  136. bool
  137. add_ehframe_links(Relobj* object, unsigned int ehframe_shndx,
  138. Reloc_info& ehframe_relocs);
  139. // Maps integers to sections.
  140. std::vector<Section_id> id_section_;
  141. // Does the reverse.
  142. Uniq_secn_id_map section_id_;
  143. // Given a section id, this maps it to the id of the kept
  144. // section. If the id's are the same then this section is
  145. // not folded.
  146. std::vector<unsigned int> kept_section_id_;
  147. // Given a section id, this says if the pointer to this
  148. // function is taken in which case it is dangerous to fold
  149. // this function.
  150. Secn_fptr_taken_set fptr_section_id_;
  151. // Flag to indicate if ICF has been run.
  152. bool icf_ready_;
  153. // This list is populated by gc_process_relocs in gc.h.
  154. Reloc_info_list reloc_info_list_;
  155. // Regions of other sections that should be considered part of
  156. // each section for ICF purposes.
  157. Extra_identity_list extra_identity_list_;
  158. };
  159. // This function returns true if this section corresponds to a function that
  160. // should be considered by icf as a possible candidate for folding. Some
  161. // earlier gcc versions, like 4.0.3, put constructors and destructors in
  162. // .gnu.linkonce.t sections and hence should be included too.
  163. // The mechanism used to safely fold functions referenced by .eh_frame
  164. // requires folding .gcc_except_table sections as well; see "Notes regarding
  165. // C++ exception handling" at the top of icf.cc for an explanation why.
  166. inline bool
  167. is_section_foldable_candidate(const std::string& section_name)
  168. {
  169. const char* section_name_cstr = section_name.c_str();
  170. return (is_prefix_of(".text", section_name_cstr)
  171. || is_prefix_of(".gcc_except_table", section_name_cstr)
  172. || is_prefix_of(".gnu.linkonce.t", section_name_cstr));
  173. }
  174. } // End of namespace gold.
  175. #endif