2010-02-12 Sriraman Tallam <tmsriram@google.com>
[deliverable/binutils-gdb.git] / gold / icf.h
1 // icf.h -- Identical Code Folding
2
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
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"
30 #include "object.h"
31
32 namespace gold
33 {
34
35 class Object;
36 class Input_objects;
37 class Symbol_table;
38
39 class Icf
40 {
41 public:
42 typedef std::vector<Section_id> Sections_reachable_list;
43 typedef std::vector<Symbol*> Symbol_info;
44 typedef std::vector<std::pair<long long, long long> > Addend_info;
45 typedef Unordered_map<Section_id,
46 Sections_reachable_list,
47 Section_id_hash> Section_list;
48 typedef Unordered_map<Section_id, Symbol_info, Section_id_hash> Symbol_list;
49 typedef Unordered_map<Section_id, Addend_info, Section_id_hash> Addend_list;
50 typedef Unordered_map<Section_id,
51 unsigned int,
52 Section_id_hash> Uniq_secn_id_map;
53 typedef Unordered_set<Section_id, Section_id_hash> Secn_fptr_taken_set;
54
55 Icf()
56 : id_section_(), section_id_(), kept_section_id_(),
57 fptr_section_id_(),
58 num_tracked_relocs(NULL), icf_ready_(false),
59 section_reloc_list_(), symbol_reloc_list_(),
60 addend_reloc_list_()
61 { }
62
63 // Returns the kept folded identical section corresponding to
64 // dup_obj and dup_shndx.
65 Section_id
66 get_folded_section(Object* dup_obj, unsigned int dup_shndx);
67
68 // Forms groups of identical sections where the first member
69 // of each group is the kept section during folding.
70 void
71 find_identical_sections(const Input_objects* input_objects,
72 Symbol_table* symtab);
73
74 // This is set when ICF has been run and the groups of
75 // identical sections have been formed.
76 void
77 icf_ready()
78 { this->icf_ready_ = true; }
79
80 // Returns true if ICF has been run.
81 bool
82 is_icf_ready()
83 { return this->icf_ready_; }
84
85 // Unfolds the section denoted by OBJ and SHNDX if folded.
86 void
87 unfold_section(Object* obj, unsigned int shndx);
88
89 // Returns the kept section corresponding to the
90 // given section.
91 bool
92 is_section_folded(Object* obj, unsigned int shndx);
93
94 // Given an object and a section index, this returns true if the
95 // pointer of the function defined in this section is taken.
96 bool
97 section_has_function_pointers(Object *obj, unsigned int shndx)
98 {
99 return (this->fptr_section_id_.find(Section_id(obj, shndx))
100 != this->fptr_section_id_.end());
101 }
102
103 // Records that a pointer of the function defined in this section
104 // is taken.
105 void
106 set_section_has_function_pointers(Object *obj, unsigned int shndx)
107 {
108 this->fptr_section_id_.insert(Section_id(obj, shndx));
109 }
110
111 // Checks if the section_name should be searched for relocs
112 // corresponding to taken function pointers. Ignores eh_frame
113 // and vtable sections.
114 inline bool
115 check_section_for_function_pointers(std::string section_name,
116 Target* target)
117 {
118 return (parameters->options().icf_safe_folding()
119 && target->can_check_for_function_pointers()
120 && !is_prefix_of(".rodata._ZTV", section_name.c_str())
121 && !is_prefix_of(".eh_frame", section_name.c_str()));
122 }
123
124 // Returns a map of a section to a list of all sections referenced
125 // by its relocations.
126 Section_list&
127 section_reloc_list()
128 { return this->section_reloc_list_; }
129
130 // Returns a map of a section to a list of all symbols referenced
131 // by its relocations.
132 Symbol_list&
133 symbol_reloc_list()
134 { return this->symbol_reloc_list_; }
135
136 // Returns a maps of a section to a list of symbol values and addends
137 // of its relocations.
138 Addend_list&
139 addend_reloc_list()
140 { return this->addend_reloc_list_; }
141
142 // Returns a mapping of each section to a unique integer.
143 Uniq_secn_id_map&
144 section_to_int_map()
145 { return this->section_id_; }
146
147 private:
148
149 // Maps integers to sections.
150 std::vector<Section_id> id_section_;
151 // Does the reverse.
152 Uniq_secn_id_map section_id_;
153 // Given a section id, this maps it to the id of the kept
154 // section. If the id's are the same then this section is
155 // not folded.
156 std::vector<unsigned int> kept_section_id_;
157 // Given a section id, this says if the pointer to this
158 // function is taken in which case it is dangerous to fold
159 // this function.
160 Secn_fptr_taken_set fptr_section_id_;
161 unsigned int* num_tracked_relocs;
162 // Flag to indicate if ICF has been run.
163 bool icf_ready_;
164
165 // These lists are populated by gc_process_relocs in gc.h.
166 Section_list section_reloc_list_;
167 Symbol_list symbol_reloc_list_;
168 Addend_list addend_reloc_list_;
169 };
170
171 // This function returns true if this section corresponds to a function that
172 // should be considered by icf as a possible candidate for folding. Some
173 // earlier gcc versions, like 4.0.3, put constructors and destructors in
174 // .gnu.linkonce.t sections and hence should be included too.
175 inline bool
176 is_section_foldable_candidate(const char* section_name)
177 {
178 return (is_prefix_of(".text", section_name)
179 || is_prefix_of(".gnu.linkonce.t", section_name));
180 }
181
182 } // End of namespace gold.
183
184 #endif
This page took 0.033629 seconds and 5 git commands to generate.