1 // gc.h -- garbage collection of unused sections
3 // Copyright (C) 2009-2014 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.
39 template<int size
, bool big_endian
>
40 class Sized_relobj_file
;
42 template<int sh_type
, int size
, bool big_endian
>
46 class General_options
;
49 class Garbage_collection
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
;
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
;
62 : is_worklist_ready_(false)
65 // Accessor methods for the private members.
69 { return referenced_list_
; }
73 { return this->section_reloc_map_
; }
77 { return this->work_list_
; }
81 { return this->is_worklist_ready_
; }
85 { this->is_worklist_ready_
= true; }
88 do_transitive_closure();
91 is_section_garbage(Object
* obj
, unsigned int shndx
)
92 { return (this->referenced_list().find(Section_id(obj
, shndx
))
93 == this->referenced_list().end()); }
97 { return &cident_sections_
; }
100 add_cident_section(std::string section_name
,
102 { this->cident_sections_
[section_name
].insert(secn
); }
104 // Add a reference from the SRC_SHNDX-th section of SRC_OBJECT to
105 // DST_SHNDX-th section of DST_OBJECT.
107 add_reference(Object
* src_object
, unsigned int src_shndx
,
108 Object
* dst_object
, unsigned int dst_shndx
)
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
);
116 p
->second
.insert(dst_id
);
121 Worklist_type work_list_
;
122 bool is_worklist_ready_
;
123 Section_ref section_reloc_map_
;
124 Sections_reachable referenced_list_
;
125 Cident_section_map cident_sections_
;
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.
135 unsigned char* section_headers_data
;
137 unsigned char* section_names_data
;
138 // Size of section name data in bytes.
139 section_size_type section_names_size
;
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
;
149 unsigned char* symbol_names_data
;
150 // Size of symbol name data in bytes.
151 section_size_type symbol_names_size
;
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.
158 template<typename Classify_reloc
>
160 get_embedded_addend_size(int sh_type
, int r_type
, Relobj
* obj
)
162 if (sh_type
!= elfcpp::SHT_REL
)
164 Classify_reloc classify_reloc
;
165 return classify_reloc
.get_size_for_reloc(r_type
, obj
);
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
174 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
175 typename Scan
, typename Classify_reloc
>
178 Symbol_table
* symtab
,
181 Sized_relobj_file
<size
, big_endian
>* src_obj
,
182 unsigned int src_indx
,
183 const unsigned char* prelocs
,
188 const unsigned char* plocal_syms
)
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
;
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
;
200 Icf::Reloc_addend_size_info
* reloc_addend_size_vec
= NULL
;
201 bool is_icf_tracked
= false;
202 const char* cident_section_name
= NULL
;
204 std::string src_section_name
= (parameters
->options().icf_enabled()
205 ? src_obj
->section_name(src_indx
)
208 bool check_section_for_function_pointers
= false;
210 if (parameters
->options().icf_enabled()
211 && is_section_foldable_candidate(src_section_name
.c_str()))
213 is_icf_tracked
= true;
214 Section_id
src_id(src_obj
, src_indx
);
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
;
221 reloc_addend_size_vec
= &reloc_info
->reloc_addend_size_info
;
224 check_section_for_function_pointers
=
225 symtab
->icf()->check_section_for_function_pointers(src_section_name
,
228 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
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
);
233 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
234 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
=
235 Reloc_types
<sh_type
, size
, big_endian
>::get_reloc_addend_noerror(&reloc
);
237 unsigned int dst_indx
;
238 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
241 if (r_sym
< local_count
)
243 gold_assert(plocal_syms
!= NULL
);
244 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
246 dst_indx
= lsym
.get_st_shndx();
248 dst_indx
= src_obj
->adjust_sym_shndx(r_sym
, dst_indx
, &is_ordinary
);
250 dst_off
= lsym
.get_st_value() + addend
;
254 Address symvalue
= dst_off
- addend
;
256 (*secvec
).push_back(Section_id(dst_obj
, dst_indx
));
258 (*secvec
).push_back(Section_id(NULL
, 0));
259 (*symvec
).push_back(NULL
);
260 (*addendvec
).push_back(std::make_pair(
261 static_cast<long long>(symvalue
),
262 static_cast<long long>(addend
)));
263 uint64_t reloc_offset
=
264 convert_to_section_size_type(reloc
.get_r_offset());
265 (*offsetvec
).push_back(reloc_offset
);
266 (*reloc_addend_size_vec
).push_back(
267 get_embedded_addend_size
<Classify_reloc
>(sh_type
, r_type
,
271 // When doing safe folding, check to see if this relocation is that
272 // of a function pointer being taken.
274 && check_section_for_function_pointers
275 && lsym
.get_st_type() != elfcpp::STT_OBJECT
276 && scan
.local_reloc_may_be_function_pointer(symtab
, NULL
, NULL
,
280 symtab
->icf()->set_section_has_function_pointers(
281 src_obj
, lsym
.get_st_shndx());
283 if (!is_ordinary
|| dst_indx
== src_indx
)
288 Symbol
* gsym
= src_obj
->global_symbol(r_sym
);
289 gold_assert(gsym
!= NULL
);
290 if (gsym
->is_forwarder())
291 gsym
= symtab
->resolve_forwards(gsym
);
295 bool is_ordinary
= false;
296 if (gsym
->source() == Symbol::FROM_OBJECT
)
298 dst_obj
= gsym
->object();
299 dst_indx
= gsym
->shndx(&is_ordinary
);
301 dst_off
= static_cast<const Sized_symbol
<size
>*>(gsym
)->value();
304 // When doing safe folding, check to see if this relocation is that
305 // of a function pointer being taken.
306 if (gsym
->source() == Symbol::FROM_OBJECT
307 && check_section_for_function_pointers
308 && gsym
->type() != elfcpp::STT_OBJECT
310 || scan
.global_reloc_may_be_function_pointer(
311 symtab
, NULL
, NULL
, src_obj
, src_indx
, NULL
, reloc
,
313 symtab
->icf()->set_section_has_function_pointers(dst_obj
, dst_indx
);
315 // If the symbol name matches '__start_XXX' then the section with
316 // the C identifier like name 'XXX' should not be garbage collected.
317 // A similar treatment to symbols with the name '__stop_XXX'.
318 if (is_prefix_of(cident_section_start_prefix
, gsym
->name()))
320 cident_section_name
= (gsym
->name()
321 + strlen(cident_section_start_prefix
));
323 else if (is_prefix_of(cident_section_stop_prefix
, gsym
->name()))
325 cident_section_name
= (gsym
->name()
326 + strlen(cident_section_stop_prefix
));
330 Address symvalue
= dst_off
- addend
;
331 if (is_ordinary
&& gsym
->source() == Symbol::FROM_OBJECT
)
332 (*secvec
).push_back(Section_id(dst_obj
, dst_indx
));
334 (*secvec
).push_back(Section_id(NULL
, 0));
335 (*symvec
).push_back(gsym
);
336 (*addendvec
).push_back(std::make_pair(
337 static_cast<long long>(symvalue
),
338 static_cast<long long>(addend
)));
339 uint64_t reloc_offset
=
340 convert_to_section_size_type(reloc
.get_r_offset());
341 (*offsetvec
).push_back(reloc_offset
);
342 (*reloc_addend_size_vec
).push_back(
343 get_embedded_addend_size
<Classify_reloc
>(sh_type
, r_type
,
347 if (gsym
->source() != Symbol::FROM_OBJECT
)
352 if (parameters
->options().gc_sections())
354 symtab
->gc()->add_reference(src_obj
, src_indx
, dst_obj
, dst_indx
);
355 parameters
->sized_target
<size
, big_endian
>()
356 ->gc_add_reference(symtab
, src_obj
, src_indx
,
357 dst_obj
, dst_indx
, dst_off
);
358 if (cident_section_name
!= NULL
)
360 Garbage_collection::Cident_section_map::iterator ele
=
361 symtab
->gc()->cident_sections()->find(std::string(cident_section_name
));
362 if (ele
== symtab
->gc()->cident_sections()->end())
364 Section_id
src_id(src_obj
, src_indx
);
365 Garbage_collection::Sections_reachable
&
366 v(symtab
->gc()->section_reloc_map()[src_id
]);
367 Garbage_collection::Sections_reachable
& cident_secn(ele
->second
);
368 for (Garbage_collection::Sections_reachable::iterator it_v
369 = cident_secn
.begin();
370 it_v
!= cident_secn
.end();
381 } // End of namespace gold.