3885d1a23a8d3f4763263c4009e5456385b236ed
[platform/upstream/binutils.git] / gold / gc.h
1 // gc.h -- garbage collection of unused sections
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_GC_H
24 #define GOLD_GC_H
25
26 #include <queue>
27 #include <vector>
28
29 #include "elfcpp.h"
30 #include "symtab.h"
31 #include "icf.h"
32
33 namespace gold
34 {
35
36 class Object;
37
38 template<int size, bool big_endian>
39 class Sized_relobj;
40
41 template<int sh_type, int size, bool big_endian>
42 class Reloc_types;
43
44 class Output_section;
45 class General_options;
46 class Layout;
47
48 typedef std::pair<Object *, unsigned int> Section_id;
49
50 class Garbage_collection
51 {
52   struct Section_id_hash
53   {
54     size_t operator()(const Section_id& loc) const
55     { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
56   };
57
58  public:
59
60   typedef Unordered_set<Section_id, Section_id_hash> Sections_reachable;
61   typedef std::map<Section_id, Sections_reachable> Section_ref;
62   typedef std::queue<Section_id> Worklist_type;
63   // This maps the name of the section which can be represented as a C
64   // identifier (cident) to the list of sections that have that name.
65   // Different object files can have cident sections with the same name.
66   typedef std::map<std::string, Sections_reachable> Cident_section_map;
67
68   Garbage_collection()
69   : is_worklist_ready_(false)
70   { }
71
72   // Accessor methods for the private members.
73
74   Sections_reachable&
75   referenced_list()
76   { return referenced_list_; }
77
78   Section_ref&
79   section_reloc_map()
80   { return this->section_reloc_map_; }
81
82   Worklist_type&
83   worklist()
84   { return this->work_list_; }
85
86   bool
87   is_worklist_ready()
88   { return this->is_worklist_ready_; }
89
90   void
91   worklist_ready()
92   { this->is_worklist_ready_ = true; }
93
94   void
95   do_transitive_closure();
96
97   bool
98   is_section_garbage(Object* obj, unsigned int shndx)
99   { return (this->referenced_list().find(Section_id(obj, shndx))
100             == this->referenced_list().end()); }
101
102   Cident_section_map*
103   cident_sections()
104   { return &cident_sections_; }
105
106   void
107   add_cident_section(std::string section_name,
108                      Section_id secn)
109   { this->cident_sections_[section_name].insert(secn); }
110
111   // Add a reference from the SRC_SHNDX-th section of SRC_OBJECT to
112   // DST_SHNDX-th section of DST_OBJECT.
113   void
114   add_reference(Object* src_object, unsigned int src_shndx,
115                 Object* dst_object, unsigned int dst_shndx)
116   {
117     Section_id src_id(src_object, src_shndx);
118     Section_id dst_id(dst_object, dst_shndx);
119     Section_ref::iterator p = this->section_reloc_map_.find(src_id);
120     if (p == this->section_reloc_map_.end())
121       this->section_reloc_map_[src_id].insert(dst_id);
122     else
123       p->second.insert(dst_id);
124   }
125
126  private:
127
128   Worklist_type work_list_;
129   bool is_worklist_ready_;
130   Section_ref section_reloc_map_;
131   Sections_reachable referenced_list_;
132   Cident_section_map cident_sections_;
133 };
134
135 // Data to pass between successive invocations of do_layout
136 // in object.cc while garbage collecting.  This data structure
137 // is filled by using the data from Read_symbols_data.
138
139 struct Symbols_data
140 {
141   // Section headers.
142   unsigned char* section_headers_data;
143   // Section names.
144   unsigned char* section_names_data;
145   // Size of section name data in bytes.
146   section_size_type section_names_size;
147   // Symbol data.
148   unsigned char* symbols_data;
149   // Size of symbol data in bytes.
150   section_size_type symbols_size;
151   // Offset of external symbols within symbol data.  This structure
152   // sometimes contains only external symbols, in which case this will
153   // be zero.  Sometimes it contains all symbols.
154   section_offset_type external_symbols_offset;
155   // Symbol names.
156   unsigned char* symbol_names_data;
157   // Size of symbol name data in bytes.
158   section_size_type symbol_names_size;
159 };
160
161 // This function implements the generic part of reloc
162 // processing to map a section to all the sections it
163 // references through relocs.  It is called only during
164 // garbage collection (--gc-sections) and identical code
165 // folding (--icf).
166
167 template<int size, bool big_endian, typename Target_type, int sh_type,
168          typename Scan>
169 inline void
170 gc_process_relocs(
171     Symbol_table* symtab,
172     Layout*,
173     Target_type* ,
174     Sized_relobj<size, big_endian>* src_obj,
175     unsigned int src_indx,
176     const unsigned char* prelocs,
177     size_t reloc_count,
178     Output_section*,
179     bool ,
180     size_t local_count,
181     const unsigned char* plocal_syms)
182 {
183   Object *dst_obj;
184   unsigned int dst_indx;
185
186   typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
187   const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
188   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
189
190   std::vector<Section_id>* secvec = NULL;
191   std::vector<Symbol*>* symvec = NULL;
192   std::vector<std::pair<long long, long long> >* addendvec = NULL;
193   bool is_icf_tracked = false;
194   const char* cident_section_name = NULL;
195
196   if (parameters->options().icf_enabled()
197       && is_section_foldable_candidate(src_obj->section_name(src_indx).c_str()))
198     {
199       is_icf_tracked = true;
200       Section_id src_id(src_obj, src_indx);
201       secvec = &symtab->icf()->section_reloc_list()[src_id];
202       symvec = &symtab->icf()->symbol_reloc_list()[src_id];
203       addendvec = &symtab->icf()->addend_reloc_list()[src_id];
204     }
205
206   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
207     {
208       Reltype reloc(prelocs);
209       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
210       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
211       typename elfcpp::Elf_types<size>::Elf_Swxword addend =
212       Reloc_types<sh_type, size, big_endian>::get_reloc_addend_noerror(&reloc);
213
214       if (r_sym < local_count)
215         {
216           gold_assert(plocal_syms != NULL);
217           typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
218                                                       + r_sym * sym_size);
219           unsigned int shndx = lsym.get_st_shndx();
220           bool is_ordinary;
221           shndx = src_obj->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
222           if (!is_ordinary)
223             continue;
224           dst_obj = src_obj;
225           dst_indx = shndx;
226           Section_id dst_id(dst_obj, dst_indx);
227           if (is_icf_tracked)
228             {
229               (*secvec).push_back(dst_id);
230               (*symvec).push_back(NULL);
231               long long symvalue = static_cast<long long>(lsym.get_st_value());
232               (*addendvec).push_back(std::make_pair(symvalue,
233                                               static_cast<long long>(addend)));
234             }
235           if (shndx == src_indx)
236             continue;
237         }
238       else
239         {
240           Symbol* gsym = src_obj->global_symbol(r_sym);
241           gold_assert(gsym != NULL);
242           if (gsym->is_forwarder())
243             gsym = symtab->resolve_forwards(gsym);
244           if (gsym->source() != Symbol::FROM_OBJECT)
245             continue;
246           bool is_ordinary;
247           dst_obj = gsym->object();
248           dst_indx = gsym->shndx(&is_ordinary);
249           if (!is_ordinary)
250             continue;
251           Section_id dst_id(dst_obj, dst_indx);
252           // If the symbol name matches '__start_XXX' then the section with
253           // the C identifier like name 'XXX' should not be garbage collected.
254           // A similar treatment to symbols with the name '__stop_XXX'.
255           if (is_prefix_of(cident_section_start_prefix, gsym->name()))
256             {
257               cident_section_name = (gsym->name() 
258                                      + strlen(cident_section_start_prefix));
259             }
260           else if (is_prefix_of(cident_section_stop_prefix, gsym->name()))
261             {
262               cident_section_name = (gsym->name() 
263                                      + strlen(cident_section_stop_prefix));
264             }
265           if (is_icf_tracked)
266             {
267               (*secvec).push_back(dst_id);
268               (*symvec).push_back(gsym);
269               Sized_symbol<size>* sized_gsym =
270                         static_cast<Sized_symbol<size>* >(gsym);
271               long long symvalue =
272                         static_cast<long long>(sized_gsym->value());
273               (*addendvec).push_back(std::make_pair(symvalue,
274                                         static_cast<long long>(addend)));
275             }
276         }
277       if (parameters->options().gc_sections())
278         {
279           symtab->gc()->add_reference(src_obj, src_indx, dst_obj, dst_indx);
280           if (cident_section_name != NULL)
281             {
282               Garbage_collection::Cident_section_map::iterator ele =
283                 symtab->gc()->cident_sections()->find(std::string(cident_section_name));
284               if (ele == symtab->gc()->cident_sections()->end())
285                 continue;
286               Section_id src_id(src_obj, src_indx);
287               Garbage_collection::Sections_reachable&
288                 v(symtab->gc()->section_reloc_map()[src_id]);
289               Garbage_collection::Sections_reachable& cident_secn(ele->second);
290               for (Garbage_collection::Sections_reachable::iterator it_v
291                      = cident_secn.begin();
292                    it_v != cident_secn.end();
293                    ++it_v)
294                 {
295                   v.insert(*it_v);
296                 }
297             }
298         }
299     }
300   return;
301 }
302
303 } // End of namespace gold.
304
305 #endif