*** empty log message ***
[deliverable/binutils-gdb.git] / gold / icf.h
CommitLineData
ef15dade
ST
1// icf.h -- Identical Code Folding
2
55a2bb35 3// Copyright 2009, 2010 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
32namespace gold
33{
34
35class Object;
36class Input_objects;
37class Symbol_table;
38
ef15dade
ST
39class 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;
ef15dade
ST
46 typedef Unordered_map<Section_id,
47 unsigned int,
48 Section_id_hash> Uniq_secn_id_map;
21bb3914 49 typedef Unordered_set<Section_id, Section_id_hash> Secn_fptr_taken_set;
ef15dade 50
b487ad64
ST
51 typedef struct
52 {
53 // This stores the section corresponding to the reloc.
54 Sections_reachable_info section_info;
55 // This stores the symbol corresponding to the reloc.
56 Symbol_info symbol_info;
57 // This stores the symbol value and the addend for a reloc.
58 Addend_info addend_info;
59 Offset_info offset_info;
60 } Reloc_info;
61
62 typedef Unordered_map<Section_id, Reloc_info,
63 Section_id_hash> Reloc_info_list;
64
ef15dade
ST
65 Icf()
66 : id_section_(), section_id_(), kept_section_id_(),
21bb3914 67 fptr_section_id_(),
ef15dade 68 num_tracked_relocs(NULL), icf_ready_(false),
b487ad64 69 reloc_info_list_()
ef15dade
ST
70 { }
71
72 // Returns the kept folded identical section corresponding to
73 // dup_obj and dup_shndx.
74 Section_id
75 get_folded_section(Object* dup_obj, unsigned int dup_shndx);
76
77 // Forms groups of identical sections where the first member
78 // of each group is the kept section during folding.
79 void
80 find_identical_sections(const Input_objects* input_objects,
81 Symbol_table* symtab);
82
83 // This is set when ICF has been run and the groups of
84 // identical sections have been formed.
85 void
86 icf_ready()
87 { this->icf_ready_ = true; }
88
89 // Returns true if ICF has been run.
90 bool
91 is_icf_ready()
92 { return this->icf_ready_; }
93
48c187ce
ST
94 // Unfolds the section denoted by OBJ and SHNDX if folded.
95 void
96 unfold_section(Object* obj, unsigned int shndx);
97
ef15dade
ST
98 // Returns the kept section corresponding to the
99 // given section.
100 bool
101 is_section_folded(Object* obj, unsigned int shndx);
21bb3914
ST
102
103 // Given an object and a section index, this returns true if the
104 // pointer of the function defined in this section is taken.
105 bool
106 section_has_function_pointers(Object *obj, unsigned int shndx)
107 {
108 return (this->fptr_section_id_.find(Section_id(obj, shndx))
109 != this->fptr_section_id_.end());
110 }
111
112 // Records that a pointer of the function defined in this section
113 // is taken.
114 void
115 set_section_has_function_pointers(Object *obj, unsigned int shndx)
116 {
117 this->fptr_section_id_.insert(Section_id(obj, shndx));
118 }
119
120 // Checks if the section_name should be searched for relocs
121 // corresponding to taken function pointers. Ignores eh_frame
122 // and vtable sections.
123 inline bool
124 check_section_for_function_pointers(std::string section_name,
125 Target* target)
126 {
127 return (parameters->options().icf_safe_folding()
128 && target->can_check_for_function_pointers()
129 && !is_prefix_of(".rodata._ZTV", section_name.c_str())
0897ed3b 130 && !is_prefix_of(".data.rel.ro._ZTV", section_name.c_str())
21bb3914
ST
131 && !is_prefix_of(".eh_frame", section_name.c_str()));
132 }
133
b487ad64
ST
134 // Returns a map of a section to info (Reloc_info) about its relocations.
135 Reloc_info_list&
136 reloc_info_list()
137 { return this->reloc_info_list_; }
ef15dade
ST
138
139 // Returns a mapping of each section to a unique integer.
140 Uniq_secn_id_map&
141 section_to_int_map()
142 { return this->section_id_; }
143
144 private:
145
146 // Maps integers to sections.
147 std::vector<Section_id> id_section_;
148 // Does the reverse.
149 Uniq_secn_id_map section_id_;
150 // Given a section id, this maps it to the id of the kept
151 // section. If the id's are the same then this section is
152 // not folded.
153 std::vector<unsigned int> kept_section_id_;
21bb3914
ST
154 // Given a section id, this says if the pointer to this
155 // function is taken in which case it is dangerous to fold
156 // this function.
157 Secn_fptr_taken_set fptr_section_id_;
ef15dade
ST
158 unsigned int* num_tracked_relocs;
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.
169inline bool
170is_section_foldable_candidate(const char* section_name)
171{
172 return (is_prefix_of(".text", section_name)
173 || is_prefix_of(".gnu.linkonce.t", section_name));
174}
175
ef15dade
ST
176} // End of namespace gold.
177
178#endif
This page took 0.073997 seconds and 4 git commands to generate.