gdb/
[deliverable/binutils-gdb.git] / gold / gc.h
CommitLineData
6d03d481
ST
1// gc.h -- garbage collection of unused sections
2
2c54b4f4 3// Copyright 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
6d03d481
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_GC_H
24#define GOLD_GC_H
25
26#include <queue>
ef15dade 27#include <vector>
6d03d481
ST
28
29#include "elfcpp.h"
30#include "symtab.h"
5ac169d4 31#include "object.h"
f345227a 32#include "icf.h"
6d03d481
ST
33
34namespace gold
35{
36
37class Object;
38
39template<int size, bool big_endian>
6fa2a40b 40class Sized_relobj_file;
6d03d481
ST
41
42template<int sh_type, int size, bool big_endian>
2c54b4f4 43struct Reloc_types;
6d03d481
ST
44
45class Output_section;
46class General_options;
47class Layout;
48
6d03d481
ST
49class Garbage_collection
50{
ef15dade
ST
51 public:
52
6d03d481
ST
53 typedef Unordered_set<Section_id, Section_id_hash> Sections_reachable;
54 typedef std::map<Section_id, Sections_reachable> Section_ref;
55 typedef std::queue<Section_id> Worklist_type;
f1ec9ded
ST
56 // This maps the name of the section which can be represented as a C
57 // identifier (cident) to the list of sections that have that name.
58 // Different object files can have cident sections with the same name.
59 typedef std::map<std::string, Sections_reachable> Cident_section_map;
6d03d481 60
ef15dade
ST
61 Garbage_collection()
62 : is_worklist_ready_(false)
63 { }
64
65 // Accessor methods for the private members.
66
67 Sections_reachable&
68 referenced_list()
69 { return referenced_list_; }
70
71 Section_ref&
72 section_reloc_map()
73 { return this->section_reloc_map_; }
74
75 Worklist_type&
76 worklist()
77 { return this->work_list_; }
78
79 bool
80 is_worklist_ready()
81 { return this->is_worklist_ready_; }
82
83 void
84 worklist_ready()
85 { this->is_worklist_ready_ = true; }
86
87 void
88 do_transitive_closure();
89
90 bool
91 is_section_garbage(Object* obj, unsigned int shndx)
92 { return (this->referenced_list().find(Section_id(obj, shndx))
93 == this->referenced_list().end()); }
f1ec9ded
ST
94
95 Cident_section_map*
96 cident_sections()
97 { return &cident_sections_; }
98
99 void
100 add_cident_section(std::string section_name,
101 Section_id secn)
102 { this->cident_sections_[section_name].insert(secn); }
103
99e5bff2
DK
104 // Add a reference from the SRC_SHNDX-th section of SRC_OBJECT to
105 // DST_SHNDX-th section of DST_OBJECT.
106 void
107 add_reference(Object* src_object, unsigned int src_shndx,
108 Object* dst_object, unsigned int dst_shndx)
109 {
110 Section_id src_id(src_object, src_shndx);
111 Section_id dst_id(dst_object, dst_shndx);
112 Section_ref::iterator p = this->section_reloc_map_.find(src_id);
113 if (p == this->section_reloc_map_.end())
114 this->section_reloc_map_[src_id].insert(dst_id);
115 else
116 p->second.insert(dst_id);
117 }
118
ef15dade
ST
119 private:
120
121 Worklist_type work_list_;
122 bool is_worklist_ready_;
123 Section_ref section_reloc_map_;
124 Sections_reachable referenced_list_;
f1ec9ded 125 Cident_section_map cident_sections_;
6d03d481
ST
126};
127
128// Data to pass between successive invocations of do_layout
129// in object.cc while garbage collecting. This data structure
130// is filled by using the data from Read_symbols_data.
131
132struct Symbols_data
133{
134 // Section headers.
135 unsigned char* section_headers_data;
136 // Section names.
137 unsigned char* section_names_data;
138 // Size of section name data in bytes.
139 section_size_type section_names_size;
140 // Symbol data.
141 unsigned char* symbols_data;
142 // Size of symbol data in bytes.
143 section_size_type symbols_size;
144 // Offset of external symbols within symbol data. This structure
145 // sometimes contains only external symbols, in which case this will
146 // be zero. Sometimes it contains all symbols.
147 section_offset_type external_symbols_offset;
148 // Symbol names.
149 unsigned char* symbol_names_data;
150 // Size of symbol name data in bytes.
151 section_size_type symbol_names_size;
152};
153
41cbeecc
ST
154// Relocations of type SHT_REL store the addend value in their bytes.
155// This function returns the size of the embedded addend which is
156// nothing but the size of the relocation.
157
158template<typename Classify_reloc>
159inline unsigned int
160get_embedded_addend_size(int sh_type, int r_type, Relobj* obj)
161{
162 if (sh_type != elfcpp::SHT_REL)
163 return 0;
164 Classify_reloc classify_reloc;
165 return classify_reloc.get_size_for_reloc(r_type, obj);
166}
167
ef15dade
ST
168// This function implements the generic part of reloc
169// processing to map a section to all the sections it
170// references through relocs. It is called only during
171// garbage collection (--gc-sections) and identical code
172// folding (--icf).
6d03d481
ST
173
174template<int size, bool big_endian, typename Target_type, int sh_type,
41cbeecc 175 typename Scan, typename Classify_reloc>
6d03d481
ST
176inline void
177gc_process_relocs(
6d03d481
ST
178 Symbol_table* symtab,
179 Layout*,
21bb3914 180 Target_type* target,
6fa2a40b 181 Sized_relobj_file<size, big_endian>* src_obj,
ef15dade 182 unsigned int src_indx,
6d03d481
ST
183 const unsigned char* prelocs,
184 size_t reloc_count,
185 Output_section*,
21bb3914 186 bool,
6d03d481
ST
187 size_t local_count,
188 const unsigned char* plocal_syms)
189{
21bb3914 190 Scan scan;
6d03d481 191
6d03d481
ST
192 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
193 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
194 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
195
b487ad64
ST
196 Icf::Sections_reachable_info* secvec = NULL;
197 Icf::Symbol_info* symvec = NULL;
198 Icf::Addend_info* addendvec = NULL;
199 Icf::Offset_info* offsetvec = NULL;
41cbeecc 200 Icf::Reloc_addend_size_info* reloc_addend_size_vec = NULL;
ef15dade 201 bool is_icf_tracked = false;
f1ec9ded 202 const char* cident_section_name = NULL;
ef15dade 203
21bb3914
ST
204 std::string src_section_name = (parameters->options().icf_enabled()
205 ? src_obj->section_name(src_indx)
206 : "");
207
208 bool check_section_for_function_pointers = false;
209
032ce4e9 210 if (parameters->options().icf_enabled()
21bb3914 211 && is_section_foldable_candidate(src_section_name.c_str()))
ef15dade
ST
212 {
213 is_icf_tracked = true;
214 Section_id src_id(src_obj, src_indx);
b487ad64
ST
215 Icf::Reloc_info* reloc_info =
216 &symtab->icf()->reloc_info_list()[src_id];
217 secvec = &reloc_info->section_info;
218 symvec = &reloc_info->symbol_info;
219 addendvec = &reloc_info->addend_info;
220 offsetvec = &reloc_info->offset_info;
41cbeecc 221 reloc_addend_size_vec = &reloc_info->reloc_addend_size_info;
ef15dade
ST
222 }
223
21bb3914
ST
224 check_section_for_function_pointers =
225 symtab->icf()->check_section_for_function_pointers(src_section_name,
226 target);
227
6d03d481
ST
228 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
229 {
230 Reltype reloc(prelocs);
231 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
232 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
21bb3914 233 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
ef15dade
ST
234 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
235 Reloc_types<sh_type, size, big_endian>::get_reloc_addend_noerror(&reloc);
e81fea4d
AM
236 Object* dst_obj;
237 unsigned int dst_indx;
57420c20
AM
238 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
239 Address dst_off;
ef15dade 240
6d03d481
ST
241 if (r_sym < local_count)
242 {
243 gold_assert(plocal_syms != NULL);
244 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
245 + r_sym * sym_size);
57420c20 246 dst_indx = lsym.get_st_shndx();
6d03d481 247 bool is_ordinary;
57420c20 248 dst_indx = src_obj->adjust_sym_shndx(r_sym, dst_indx, &is_ordinary);
6d03d481 249 dst_obj = src_obj;
57420c20 250 dst_off = lsym.get_st_value() + addend;
e81fea4d 251
ef15dade
ST
252 if (is_icf_tracked)
253 {
57420c20 254 Address symvalue = dst_off - addend;
ef38fd8a 255 if (is_ordinary)
57420c20
AM
256 {
257 Symbol_location loc;
258 loc.object = dst_obj;
259 loc.shndx = dst_indx;
260 loc.offset = convert_types<off_t, Address>(dst_off);
261 // Look through function descriptors.
262 parameters->target().function_location(&loc);
263 if (loc.shndx != dst_indx)
264 {
265 // Modify symvalue/addend to the code entry.
266 symvalue = loc.offset;
267 addend = 0;
268 }
269 (*secvec).push_back(Section_id(loc.object, loc.shndx));
270 }
ef38fd8a
ST
271 else
272 (*secvec).push_back(Section_id(NULL, 0));
ef15dade 273 (*symvec).push_back(NULL);
57420c20
AM
274 (*addendvec).push_back(std::make_pair(
275 static_cast<long long>(symvalue),
276 static_cast<long long>(addend)));
b487ad64
ST
277 uint64_t reloc_offset =
278 convert_to_section_size_type(reloc.get_r_offset());
279 (*offsetvec).push_back(reloc_offset);
41cbeecc
ST
280 (*reloc_addend_size_vec).push_back(
281 get_embedded_addend_size<Classify_reloc>(sh_type, r_type,
282 src_obj));
ef15dade 283 }
21bb3914
ST
284
285 // When doing safe folding, check to see if this relocation is that
286 // of a function pointer being taken.
ef38fd8a
ST
287 if (is_ordinary
288 && check_section_for_function_pointers
21bb3914
ST
289 && lsym.get_st_type() != elfcpp::STT_OBJECT
290 && scan.local_reloc_may_be_function_pointer(symtab, NULL, NULL,
291 src_obj, src_indx,
292 NULL, reloc, r_type,
293 lsym))
294 symtab->icf()->set_section_has_function_pointers(
295 src_obj, lsym.get_st_shndx());
296
57420c20 297 if (!is_ordinary || dst_indx == src_indx)
ef15dade 298 continue;
6d03d481
ST
299 }
300 else
301 {
ef15dade 302 Symbol* gsym = src_obj->global_symbol(r_sym);
6d03d481
ST
303 gold_assert(gsym != NULL);
304 if (gsym->is_forwarder())
305 gsym = symtab->resolve_forwards(gsym);
ef38fd8a
ST
306
307 dst_obj = NULL;
308 dst_indx = 0;
309 bool is_ordinary = false;
310 if (gsym->source() == Symbol::FROM_OBJECT)
311 {
312 dst_obj = gsym->object();
313 dst_indx = gsym->shndx(&is_ordinary);
314 }
57420c20
AM
315 dst_off = static_cast<const Sized_symbol<size>*>(gsym)->value();
316 dst_off += addend;
21bb3914
ST
317
318 // When doing safe folding, check to see if this relocation is that
319 // of a function pointer being taken.
ef38fd8a
ST
320 if (gsym->source() == Symbol::FROM_OBJECT
321 && check_section_for_function_pointers
21bb3914
ST
322 && gsym->type() != elfcpp::STT_OBJECT
323 && (!is_ordinary
324 || scan.global_reloc_may_be_function_pointer(
325 symtab, NULL, NULL, src_obj, src_indx, NULL, reloc,
326 r_type, gsym)))
327 symtab->icf()->set_section_has_function_pointers(dst_obj, dst_indx);
328
f1ec9ded
ST
329 // If the symbol name matches '__start_XXX' then the section with
330 // the C identifier like name 'XXX' should not be garbage collected.
331 // A similar treatment to symbols with the name '__stop_XXX'.
332 if (is_prefix_of(cident_section_start_prefix, gsym->name()))
333 {
334 cident_section_name = (gsym->name()
335 + strlen(cident_section_start_prefix));
336 }
337 else if (is_prefix_of(cident_section_stop_prefix, gsym->name()))
338 {
339 cident_section_name = (gsym->name()
340 + strlen(cident_section_stop_prefix));
341 }
ef15dade
ST
342 if (is_icf_tracked)
343 {
57420c20 344 Address symvalue = dst_off - addend;
ef38fd8a 345 if (is_ordinary && gsym->source() == Symbol::FROM_OBJECT)
57420c20
AM
346 {
347 Symbol_location loc;
348 loc.object = dst_obj;
349 loc.shndx = dst_indx;
350 loc.offset = convert_types<off_t, Address>(dst_off);
351 // Look through function descriptors.
352 parameters->target().function_location(&loc);
353 if (loc.shndx != dst_indx)
354 {
355 // Modify symvalue/addend to the code entry.
356 symvalue = loc.offset;
357 addend = 0;
358 }
359 (*secvec).push_back(Section_id(loc.object, loc.shndx));
360 }
ef38fd8a
ST
361 else
362 (*secvec).push_back(Section_id(NULL, 0));
ef15dade 363 (*symvec).push_back(gsym);
57420c20
AM
364 (*addendvec).push_back(std::make_pair(
365 static_cast<long long>(symvalue),
366 static_cast<long long>(addend)));
b487ad64
ST
367 uint64_t reloc_offset =
368 convert_to_section_size_type(reloc.get_r_offset());
369 (*offsetvec).push_back(reloc_offset);
41cbeecc
ST
370 (*reloc_addend_size_vec).push_back(
371 get_embedded_addend_size<Classify_reloc>(sh_type, r_type,
372 src_obj));
21bb3914 373 }
ef38fd8a
ST
374
375 if (gsym->source() != Symbol::FROM_OBJECT)
376 continue;
377 if (!is_ordinary)
378 continue;
6d03d481 379 }
ef15dade 380 if (parameters->options().gc_sections())
6d03d481 381 {
99e5bff2 382 symtab->gc()->add_reference(src_obj, src_indx, dst_obj, dst_indx);
e81fea4d
AM
383 parameters->sized_target<size, big_endian>()
384 ->gc_add_reference(symtab, src_obj, src_indx,
385 dst_obj, dst_indx, dst_off);
f1ec9ded
ST
386 if (cident_section_name != NULL)
387 {
388 Garbage_collection::Cident_section_map::iterator ele =
389 symtab->gc()->cident_sections()->find(std::string(cident_section_name));
390 if (ele == symtab->gc()->cident_sections()->end())
391 continue;
99e5bff2 392 Section_id src_id(src_obj, src_indx);
f1ec9ded
ST
393 Garbage_collection::Sections_reachable&
394 v(symtab->gc()->section_reloc_map()[src_id]);
395 Garbage_collection::Sections_reachable& cident_secn(ele->second);
396 for (Garbage_collection::Sections_reachable::iterator it_v
397 = cident_secn.begin();
398 it_v != cident_secn.end();
399 ++it_v)
400 {
401 v.insert(*it_v);
402 }
403 }
6d03d481
ST
404 }
405 }
406 return;
407}
408
409} // End of namespace gold.
410
411#endif
This page took 0.214955 seconds and 4 git commands to generate.