icf.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // icf.h -- Identical Code Folding
  2. // Copyright (C) 2009-2015 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. Icf()
  54. : id_section_(), section_id_(), kept_section_id_(),
  55. fptr_section_id_(),
  56. icf_ready_(false),
  57. reloc_info_list_()
  58. { }
  59. // Returns the kept folded identical section corresponding to
  60. // dup_obj and dup_shndx.
  61. Section_id
  62. get_folded_section(Relobj* dup_obj, unsigned int dup_shndx);
  63. // Forms groups of identical sections where the first member
  64. // of each group is the kept section during folding.
  65. void
  66. find_identical_sections(const Input_objects* input_objects,
  67. Symbol_table* symtab);
  68. // This is set when ICF has been run and the groups of
  69. // identical sections have been formed.
  70. void
  71. icf_ready()
  72. { this->icf_ready_ = true; }
  73. // Returns true if ICF has been run.
  74. bool
  75. is_icf_ready()
  76. { return this->icf_ready_; }
  77. // Unfolds the section denoted by OBJ and SHNDX if folded.
  78. void
  79. unfold_section(Relobj* obj, unsigned int shndx);
  80. // Returns the kept section corresponding to the
  81. // given section.
  82. bool
  83. is_section_folded(Relobj* obj, unsigned int shndx);
  84. // Given an object and a section index, this returns true if the
  85. // pointer of the function defined in this section is taken.
  86. bool
  87. section_has_function_pointers(Relobj* obj, unsigned int shndx)
  88. {
  89. return (this->fptr_section_id_.find(Section_id(obj, shndx))
  90. != this->fptr_section_id_.end());
  91. }
  92. // Records that a pointer of the function defined in this section
  93. // is taken.
  94. void
  95. set_section_has_function_pointers(Relobj* obj, unsigned int shndx)
  96. {
  97. this->fptr_section_id_.insert(Section_id(obj, shndx));
  98. }
  99. // Checks if the section_name should be searched for relocs
  100. // corresponding to taken function pointers. Ignores eh_frame
  101. // and vtable sections.
  102. inline bool
  103. check_section_for_function_pointers(const std::string& section_name,
  104. Target* target)
  105. {
  106. return (parameters->options().icf_safe_folding()
  107. && target->can_check_for_function_pointers()
  108. && target->section_may_have_icf_unsafe_pointers(
  109. section_name.c_str()));
  110. }
  111. // Returns a map of a section to info (Reloc_info) about its relocations.
  112. Reloc_info_list&
  113. reloc_info_list()
  114. { return this->reloc_info_list_; }
  115. // Returns a mapping of each section to a unique integer.
  116. Uniq_secn_id_map&
  117. section_to_int_map()
  118. { return this->section_id_; }
  119. private:
  120. // Maps integers to sections.
  121. std::vector<Section_id> id_section_;
  122. // Does the reverse.
  123. Uniq_secn_id_map section_id_;
  124. // Given a section id, this maps it to the id of the kept
  125. // section. If the id's are the same then this section is
  126. // not folded.
  127. std::vector<unsigned int> kept_section_id_;
  128. // Given a section id, this says if the pointer to this
  129. // function is taken in which case it is dangerous to fold
  130. // this function.
  131. Secn_fptr_taken_set fptr_section_id_;
  132. // Flag to indicate if ICF has been run.
  133. bool icf_ready_;
  134. // This list is populated by gc_process_relocs in gc.h.
  135. Reloc_info_list reloc_info_list_;
  136. };
  137. // This function returns true if this section corresponds to a function that
  138. // should be considered by icf as a possible candidate for folding. Some
  139. // earlier gcc versions, like 4.0.3, put constructors and destructors in
  140. // .gnu.linkonce.t sections and hence should be included too.
  141. inline bool
  142. is_section_foldable_candidate(const std::string& section_name)
  143. {
  144. const char* section_name_cstr = section_name.c_str();
  145. return (is_prefix_of(".text", section_name_cstr)
  146. || is_prefix_of(".gnu.linkonce.t", section_name_cstr));
  147. }
  148. } // End of namespace gold.
  149. #endif