| 1 | // icf.h -- Identical Code Folding |
| 2 | |
| 3 | // Copyright 2009 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 | |
| 31 | namespace gold |
| 32 | { |
| 33 | |
| 34 | class Object; |
| 35 | class Input_objects; |
| 36 | class Symbol_table; |
| 37 | |
| 38 | typedef std::pair<Object*, unsigned int> Section_id; |
| 39 | |
| 40 | class Icf |
| 41 | { |
| 42 | public: |
| 43 | struct Section_id_hash |
| 44 | { |
| 45 | size_t operator()(const Section_id& loc) const |
| 46 | { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; } |
| 47 | }; |
| 48 | |
| 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, |
| 58 | unsigned int, |
| 59 | Section_id_hash> Uniq_secn_id_map; |
| 60 | |
| 61 | Icf() |
| 62 | : id_section_(), section_id_(), kept_section_id_(), |
| 63 | num_tracked_relocs(NULL), icf_ready_(false), |
| 64 | section_reloc_list_(), symbol_reloc_list_(), |
| 65 | addend_reloc_list_() |
| 66 | { } |
| 67 | |
| 68 | // Returns the kept folded identical section corresponding to |
| 69 | // dup_obj and dup_shndx. |
| 70 | Section_id |
| 71 | get_folded_section(Object* dup_obj, unsigned int dup_shndx); |
| 72 | |
| 73 | // Forms groups of identical sections where the first member |
| 74 | // of each group is the kept section during folding. |
| 75 | void |
| 76 | find_identical_sections(const Input_objects* input_objects, |
| 77 | Symbol_table* symtab); |
| 78 | |
| 79 | // This is set when ICF has been run and the groups of |
| 80 | // identical sections have been formed. |
| 81 | void |
| 82 | icf_ready() |
| 83 | { this->icf_ready_ = true; } |
| 84 | |
| 85 | // Returns true if ICF has been run. |
| 86 | bool |
| 87 | is_icf_ready() |
| 88 | { return this->icf_ready_; } |
| 89 | |
| 90 | // Unfolds the section denoted by OBJ and SHNDX if folded. |
| 91 | void |
| 92 | unfold_section(Object* obj, unsigned int shndx); |
| 93 | |
| 94 | // Returns the kept section corresponding to the |
| 95 | // given section. |
| 96 | bool |
| 97 | is_section_folded(Object* obj, unsigned int shndx); |
| 98 | |
| 99 | // Returns a map of a section to a list of all sections referenced |
| 100 | // by its relocations. |
| 101 | Section_list& |
| 102 | section_reloc_list() |
| 103 | { return this->section_reloc_list_; } |
| 104 | |
| 105 | // Returns a map of a section to a list of all symbols referenced |
| 106 | // by its relocations. |
| 107 | Symbol_list& |
| 108 | symbol_reloc_list() |
| 109 | { return this->symbol_reloc_list_; } |
| 110 | |
| 111 | // Returns a maps of a section to a list of symbol values and addends |
| 112 | // of its relocations. |
| 113 | Addend_list& |
| 114 | addend_reloc_list() |
| 115 | { return this->addend_reloc_list_; } |
| 116 | |
| 117 | // Returns a mapping of each section to a unique integer. |
| 118 | Uniq_secn_id_map& |
| 119 | section_to_int_map() |
| 120 | { return this->section_id_; } |
| 121 | |
| 122 | private: |
| 123 | |
| 124 | // Maps integers to sections. |
| 125 | std::vector<Section_id> id_section_; |
| 126 | // Does the reverse. |
| 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 |
| 130 | // not folded. |
| 131 | std::vector<unsigned int> kept_section_id_; |
| 132 | unsigned int* num_tracked_relocs; |
| 133 | // Flag to indicate if ICF has been run. |
| 134 | bool icf_ready_; |
| 135 | |
| 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_; |
| 140 | }; |
| 141 | |
| 142 | } // End of namespace gold. |
| 143 | |
| 144 | #endif |