* linux-low.c (linux_supports_tracefork_flag): Document.
[deliverable/binutils-gdb.git] / gold / gc.h
CommitLineData
6d03d481
ST
1// gc.h -- garbage collection of unused sections
2
55a2bb35 3// Copyright 2009, 2010 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>
40class Sized_relobj;
41
42template<int sh_type, int size, bool big_endian>
43class Reloc_types;
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
ef15dade
ST
154// This function implements the generic part of reloc
155// processing to map a section to all the sections it
156// references through relocs. It is called only during
157// garbage collection (--gc-sections) and identical code
158// folding (--icf).
6d03d481
ST
159
160template<int size, bool big_endian, typename Target_type, int sh_type,
161 typename Scan>
162inline void
163gc_process_relocs(
6d03d481
ST
164 Symbol_table* symtab,
165 Layout*,
166 Target_type* ,
ef15dade
ST
167 Sized_relobj<size, big_endian>* src_obj,
168 unsigned int src_indx,
6d03d481
ST
169 const unsigned char* prelocs,
170 size_t reloc_count,
171 Output_section*,
172 bool ,
173 size_t local_count,
174 const unsigned char* plocal_syms)
175{
ef15dade
ST
176 Object *dst_obj;
177 unsigned int dst_indx;
6d03d481 178
6d03d481
ST
179 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
180 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
181 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
182
ef15dade
ST
183 std::vector<Section_id>* secvec = NULL;
184 std::vector<Symbol*>* symvec = NULL;
185 std::vector<std::pair<long long, long long> >* addendvec = NULL;
186 bool is_icf_tracked = false;
f1ec9ded 187 const char* cident_section_name = NULL;
ef15dade 188
032ce4e9 189 if (parameters->options().icf_enabled()
55a2bb35 190 && is_section_foldable_candidate(src_obj->section_name(src_indx).c_str()))
ef15dade
ST
191 {
192 is_icf_tracked = true;
193 Section_id src_id(src_obj, src_indx);
194 secvec = &symtab->icf()->section_reloc_list()[src_id];
195 symvec = &symtab->icf()->symbol_reloc_list()[src_id];
196 addendvec = &symtab->icf()->addend_reloc_list()[src_id];
197 }
198
6d03d481
ST
199 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
200 {
201 Reltype reloc(prelocs);
202 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
203 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
ef15dade
ST
204 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
205 Reloc_types<sh_type, size, big_endian>::get_reloc_addend_noerror(&reloc);
206
6d03d481
ST
207 if (r_sym < local_count)
208 {
209 gold_assert(plocal_syms != NULL);
210 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
211 + r_sym * sym_size);
212 unsigned int shndx = lsym.get_st_shndx();
213 bool is_ordinary;
ef15dade
ST
214 shndx = src_obj->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
215 if (!is_ordinary)
6d03d481
ST
216 continue;
217 dst_obj = src_obj;
6d03d481 218 dst_indx = shndx;
ef15dade
ST
219 Section_id dst_id(dst_obj, dst_indx);
220 if (is_icf_tracked)
221 {
222 (*secvec).push_back(dst_id);
223 (*symvec).push_back(NULL);
224 long long symvalue = static_cast<long long>(lsym.get_st_value());
225 (*addendvec).push_back(std::make_pair(symvalue,
226 static_cast<long long>(addend)));
227 }
228 if (shndx == src_indx)
229 continue;
6d03d481
ST
230 }
231 else
232 {
ef15dade 233 Symbol* gsym = src_obj->global_symbol(r_sym);
6d03d481
ST
234 gold_assert(gsym != NULL);
235 if (gsym->is_forwarder())
236 gsym = symtab->resolve_forwards(gsym);
237 if (gsym->source() != Symbol::FROM_OBJECT)
238 continue;
239 bool is_ordinary;
240 dst_obj = gsym->object();
241 dst_indx = gsym->shndx(&is_ordinary);
242 if (!is_ordinary)
243 continue;
ef15dade 244 Section_id dst_id(dst_obj, dst_indx);
f1ec9ded
ST
245 // If the symbol name matches '__start_XXX' then the section with
246 // the C identifier like name 'XXX' should not be garbage collected.
247 // A similar treatment to symbols with the name '__stop_XXX'.
248 if (is_prefix_of(cident_section_start_prefix, gsym->name()))
249 {
250 cident_section_name = (gsym->name()
251 + strlen(cident_section_start_prefix));
252 }
253 else if (is_prefix_of(cident_section_stop_prefix, gsym->name()))
254 {
255 cident_section_name = (gsym->name()
256 + strlen(cident_section_stop_prefix));
257 }
ef15dade
ST
258 if (is_icf_tracked)
259 {
260 (*secvec).push_back(dst_id);
261 (*symvec).push_back(gsym);
262 Sized_symbol<size>* sized_gsym =
263 static_cast<Sized_symbol<size>* >(gsym);
264 long long symvalue =
265 static_cast<long long>(sized_gsym->value());
266 (*addendvec).push_back(std::make_pair(symvalue,
267 static_cast<long long>(addend)));
268 }
6d03d481 269 }
ef15dade 270 if (parameters->options().gc_sections())
6d03d481 271 {
99e5bff2 272 symtab->gc()->add_reference(src_obj, src_indx, dst_obj, dst_indx);
f1ec9ded
ST
273 if (cident_section_name != NULL)
274 {
275 Garbage_collection::Cident_section_map::iterator ele =
276 symtab->gc()->cident_sections()->find(std::string(cident_section_name));
277 if (ele == symtab->gc()->cident_sections()->end())
278 continue;
99e5bff2 279 Section_id src_id(src_obj, src_indx);
f1ec9ded
ST
280 Garbage_collection::Sections_reachable&
281 v(symtab->gc()->section_reloc_map()[src_id]);
282 Garbage_collection::Sections_reachable& cident_secn(ele->second);
283 for (Garbage_collection::Sections_reachable::iterator it_v
284 = cident_secn.begin();
285 it_v != cident_secn.end();
286 ++it_v)
287 {
288 v.insert(*it_v);
289 }
290 }
6d03d481
ST
291 }
292 }
293 return;
294}
295
296} // End of namespace gold.
297
298#endif
This page took 0.095579 seconds and 4 git commands to generate.