1 // icf.h -- Identical Code Folding
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Sriraman Tallam <tmsriram@google.com>.
6 // This file is part of gold.
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.
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.
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.
38 typedef std::pair<Object*, unsigned int> Section_id;
43 struct Section_id_hash
45 size_t operator()(const Section_id& loc) const
46 { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
49 typedef std::vector<Section_id> Sections_reachable_list;
50 typedef std::vector<Symbol*> Symbol_info;
51 typedef std::vector<std::pair<long long, long long> > Addend_info;
52 typedef Unordered_map<Section_id,
53 Sections_reachable_list,
54 Section_id_hash> Section_list;
55 typedef Unordered_map<Section_id, Symbol_info, Section_id_hash> Symbol_list;
56 typedef Unordered_map<Section_id, Addend_info, Section_id_hash> Addend_list;
57 typedef Unordered_map<Section_id,
59 Section_id_hash> Uniq_secn_id_map;
62 : id_section_(), section_id_(), kept_section_id_(),
63 num_tracked_relocs(NULL), icf_ready_(false),
64 section_reloc_list_(), symbol_reloc_list_(),
68 // Returns the kept folded identical section corresponding to
69 // dup_obj and dup_shndx.
71 get_folded_section(Object* dup_obj, unsigned int dup_shndx);
73 // Forms groups of identical sections where the first member
74 // of each group is the kept section during folding.
76 find_identical_sections(const Input_objects* input_objects,
77 Symbol_table* symtab);
79 // This is set when ICF has been run and the groups of
80 // identical sections have been formed.
83 { this->icf_ready_ = true; }
85 // Returns true if ICF has been run.
88 { return this->icf_ready_; }
90 // Unfolds the section denoted by OBJ and SHNDX if folded.
92 unfold_section(Object* obj, unsigned int shndx);
94 // Returns the kept section corresponding to the
97 is_section_folded(Object* obj, unsigned int shndx);
99 // Returns a map of a section to a list of all sections referenced
100 // by its relocations.
103 { return this->section_reloc_list_; }
105 // Returns a map of a section to a list of all symbols referenced
106 // by its relocations.
109 { return this->symbol_reloc_list_; }
111 // Returns a maps of a section to a list of symbol values and addends
112 // of its relocations.
115 { return this->addend_reloc_list_; }
117 // Returns a mapping of each section to a unique integer.
120 { return this->section_id_; }
124 // Maps integers to sections.
125 std::vector<Section_id> id_section_;
127 Uniq_secn_id_map section_id_;
128 // Given a section id, this maps it to the id of the kept
129 // section. If the id's are the same then this section is
131 std::vector<unsigned int> kept_section_id_;
132 unsigned int* num_tracked_relocs;
133 // Flag to indicate if ICF has been run.
136 // These lists are populated by gc_process_relocs in gc.h.
137 Section_list section_reloc_list_;
138 Symbol_list symbol_reloc_list_;
139 Addend_list addend_reloc_list_;
142 // This function returns true if this section corresponds to a function that
143 // should be considered by icf as a possible candidate for folding. Some
144 // earlier gcc versions, like 4.0.3, put constructors and destructors in
145 // .gnu.linkonce.t sections and hence should be included too.
147 is_section_foldable_candidate(const char* section_name)
149 return (is_prefix_of(".text", section_name)
150 || is_prefix_of(".gnu.linkonce.t", section_name));
153 } // End of namespace gold.