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