1 // gc.h -- garbage collection of unused sections
3 // Copyright (C) 2009-2018 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.
38 template<int size, bool big_endian>
39 class Sized_relobj_file;
42 class General_options;
45 class Garbage_collection
49 typedef Unordered_set<Section_id, Section_id_hash> Sections_reachable;
50 typedef std::map<Section_id, Sections_reachable> Section_ref;
51 typedef std::vector<Section_id> Worklist_type;
52 // This maps the name of the section which can be represented as a C
53 // identifier (cident) to the list of sections that have that name.
54 // Different object files can have cident sections with the same name.
55 typedef std::map<std::string, Sections_reachable> Cident_section_map;
58 : is_worklist_ready_(false)
61 // Accessor methods for the private members.
65 { return referenced_list_; }
69 { return this->section_reloc_map_; }
73 { return this->work_list_; }
77 { return this->is_worklist_ready_; }
81 { this->is_worklist_ready_ = true; }
84 do_transitive_closure();
87 is_section_garbage(Relobj* obj, unsigned int shndx)
88 { return (this->referenced_list().find(Section_id(obj, shndx))
89 == this->referenced_list().end()); }
93 { return &cident_sections_; }
96 add_cident_section(std::string section_name,
98 { this->cident_sections_[section_name].insert(secn); }
100 // Add a reference from the SRC_SHNDX-th section of SRC_OBJECT to
101 // DST_SHNDX-th section of DST_OBJECT.
103 add_reference(Relobj* src_object, unsigned int src_shndx,
104 Relobj* dst_object, unsigned int dst_shndx)
106 Section_id src_id(src_object, src_shndx);
107 Section_id dst_id(dst_object, dst_shndx);
108 Sections_reachable& reachable = this->section_reloc_map_[src_id];
109 reachable.insert(dst_id);
114 Worklist_type work_list_;
115 bool is_worklist_ready_;
116 Section_ref section_reloc_map_;
117 Sections_reachable referenced_list_;
118 Cident_section_map cident_sections_;
121 // Data to pass between successive invocations of do_layout
122 // in object.cc while garbage collecting. This data structure
123 // is filled by using the data from Read_symbols_data.
128 unsigned char* section_headers_data;
130 unsigned char* section_names_data;
131 // Size of section name data in bytes.
132 section_size_type section_names_size;
134 unsigned char* symbols_data;
135 // Size of symbol data in bytes.
136 section_size_type symbols_size;
137 // Offset of external symbols within symbol data. This structure
138 // sometimes contains only external symbols, in which case this will
139 // be zero. Sometimes it contains all symbols.
140 section_offset_type external_symbols_offset;
142 unsigned char* symbol_names_data;
143 // Size of symbol name data in bytes.
144 section_size_type symbol_names_size;
147 // Relocations of type SHT_REL store the addend value in their bytes.
148 // This function returns the size of the embedded addend which is
149 // nothing but the size of the relocation.
151 template<typename Classify_reloc>
153 get_embedded_addend_size(int r_type, Relobj* obj)
155 if (Classify_reloc::sh_type == elfcpp::SHT_REL)
156 return Classify_reloc::get_size_for_reloc(r_type, obj);
160 // This function implements the generic part of reloc
161 // processing to map a section to all the sections it
162 // references through relocs. It is called only during
163 // garbage collection (--gc-sections) and identical code
166 template<int size, bool big_endian, typename Target_type,
167 typename Scan, typename Classify_reloc>
170 Symbol_table* symtab,
173 Sized_relobj_file<size, big_endian>* src_obj,
174 unsigned int src_indx,
175 const unsigned char* prelocs,
180 const unsigned char* plocal_syms)
184 typedef typename Classify_reloc::Reltype Reltype;
185 const int reloc_size = Classify_reloc::reloc_size;
186 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
188 Icf::Sections_reachable_info* secvec = NULL;
189 Icf::Symbol_info* symvec = NULL;
190 Icf::Addend_info* addendvec = NULL;
191 Icf::Offset_info* offsetvec = NULL;
192 Icf::Reloc_addend_size_info* reloc_addend_size_vec = NULL;
193 bool is_icf_tracked = false;
194 const char* cident_section_name = NULL;
196 std::string src_section_name = (parameters->options().icf_enabled()
197 ? src_obj->section_name(src_indx)
200 bool check_section_for_function_pointers = false;
202 if (parameters->options().icf_enabled()
203 && is_section_foldable_candidate(src_section_name.c_str()))
205 is_icf_tracked = true;
206 Section_id src_id(src_obj, src_indx);
207 Icf::Reloc_info* reloc_info =
208 &symtab->icf()->reloc_info_list()[src_id];
209 secvec = &reloc_info->section_info;
210 symvec = &reloc_info->symbol_info;
211 addendvec = &reloc_info->addend_info;
212 offsetvec = &reloc_info->offset_info;
213 reloc_addend_size_vec = &reloc_info->reloc_addend_size_info;
216 check_section_for_function_pointers =
217 symtab->icf()->check_section_for_function_pointers(src_section_name,
220 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
222 Reltype reloc(prelocs);
223 unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
224 unsigned int r_type = Classify_reloc::get_r_type(&reloc);
225 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
226 Classify_reloc::get_r_addend(&reloc);
228 unsigned int dst_indx;
229 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
232 if (r_sym < local_count)
234 gold_assert(plocal_syms != NULL);
235 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
237 dst_indx = lsym.get_st_shndx();
239 dst_indx = src_obj->adjust_sym_shndx(r_sym, dst_indx, &is_ordinary);
241 dst_off = lsym.get_st_value() + addend;
245 Address symvalue = dst_off - addend;
247 (*secvec).push_back(Section_id(src_obj, dst_indx));
249 (*secvec).push_back(Section_id(NULL, 0));
250 // If the target of the relocation is an STT_SECTION symbol,
251 // make a note of that by storing -1 in the symbol vector.
252 if (lsym.get_st_type() == elfcpp::STT_SECTION)
253 (*symvec).push_back(reinterpret_cast<Symbol*>(-1));
255 (*symvec).push_back(NULL);
256 (*addendvec).push_back(std::make_pair(
257 static_cast<long long>(symvalue),
258 static_cast<long long>(addend)));
259 uint64_t reloc_offset =
260 convert_to_section_size_type(reloc.get_r_offset());
261 (*offsetvec).push_back(reloc_offset);
262 (*reloc_addend_size_vec).push_back(
263 get_embedded_addend_size<Classify_reloc>(r_type, src_obj));
266 // When doing safe folding, check to see if this relocation is that
267 // of a function pointer being taken.
269 && check_section_for_function_pointers
270 && lsym.get_st_type() != elfcpp::STT_OBJECT
271 && scan.local_reloc_may_be_function_pointer(symtab, NULL, target,
275 symtab->icf()->set_section_has_function_pointers(
276 src_obj, lsym.get_st_shndx());
278 if (!is_ordinary || dst_indx == src_indx)
283 Symbol* gsym = src_obj->global_symbol(r_sym);
284 gold_assert(gsym != NULL);
285 if (gsym->is_forwarder())
286 gsym = symtab->resolve_forwards(gsym);
290 bool is_ordinary = false;
291 if (gsym->source() == Symbol::FROM_OBJECT
292 && !gsym->object()->is_dynamic())
294 dst_obj = static_cast<Relobj*>(gsym->object());
295 dst_indx = gsym->shndx(&is_ordinary);
297 dst_off = static_cast<const Sized_symbol<size>*>(gsym)->value();
300 // When doing safe folding, check to see if this relocation is that
301 // of a function pointer being taken.
302 if (gsym->source() == Symbol::FROM_OBJECT
303 && gsym->type() == elfcpp::STT_FUNC
304 && check_section_for_function_pointers
307 || scan.global_reloc_may_be_function_pointer(
308 symtab, NULL, target, src_obj, src_indx, NULL, reloc,
310 symtab->icf()->set_section_has_function_pointers(dst_obj, dst_indx);
312 // If the symbol name matches '__start_XXX' then the section with
313 // the C identifier like name 'XXX' should not be garbage collected.
314 // A similar treatment to symbols with the name '__stop_XXX'.
315 if (is_prefix_of(cident_section_start_prefix, gsym->name()))
317 cident_section_name = (gsym->name()
318 + strlen(cident_section_start_prefix));
320 else if (is_prefix_of(cident_section_stop_prefix, gsym->name()))
322 cident_section_name = (gsym->name()
323 + strlen(cident_section_stop_prefix));
327 Address symvalue = dst_off - addend;
328 if (is_ordinary && dst_obj != NULL)
329 (*secvec).push_back(Section_id(dst_obj, dst_indx));
331 (*secvec).push_back(Section_id(NULL, 0));
332 (*symvec).push_back(gsym);
333 (*addendvec).push_back(std::make_pair(
334 static_cast<long long>(symvalue),
335 static_cast<long long>(addend)));
336 uint64_t reloc_offset =
337 convert_to_section_size_type(reloc.get_r_offset());
338 (*offsetvec).push_back(reloc_offset);
339 (*reloc_addend_size_vec).push_back(
340 get_embedded_addend_size<Classify_reloc>(r_type, src_obj));
348 if (parameters->options().gc_sections())
350 symtab->gc()->add_reference(src_obj, src_indx, dst_obj, dst_indx);
351 parameters->sized_target<size, big_endian>()
352 ->gc_add_reference(symtab, src_obj, src_indx, dst_obj, dst_indx,
354 if (cident_section_name != NULL)
356 Garbage_collection::Cident_section_map::iterator ele =
357 symtab->gc()->cident_sections()->find(std::string(cident_section_name));
358 if (ele == symtab->gc()->cident_sections()->end())
360 Section_id src_id(src_obj, src_indx);
361 Garbage_collection::Sections_reachable&
362 v(symtab->gc()->section_reloc_map()[src_id]);
363 Garbage_collection::Sections_reachable& cident_secn(ele->second);
364 for (Garbage_collection::Sections_reachable::iterator it_v
365 = cident_secn.begin();
366 it_v != cident_secn.end();
377 } // End of namespace gold.