1 // gc.h -- garbage collection of unused sections
3 // Copyright 2009, 2010 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
>
41 template<int sh_type
, int size
, bool big_endian
>
45 class General_options
;
48 typedef std::pair
<Object
*, unsigned int> Section_id
;
50 class Garbage_collection
52 struct Section_id_hash
54 size_t operator()(const Section_id
& loc
) const
55 { return reinterpret_cast<uintptr_t>(loc
.first
) ^ loc
.second
; }
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
;
69 : is_worklist_ready_(false)
72 // Accessor methods for the private members.
76 { return referenced_list_
; }
80 { return this->section_reloc_map_
; }
84 { return this->work_list_
; }
88 { return this->is_worklist_ready_
; }
92 { this->is_worklist_ready_
= true; }
95 do_transitive_closure();
98 is_section_garbage(Object
* obj
, unsigned int shndx
)
99 { return (this->referenced_list().find(Section_id(obj
, shndx
))
100 == this->referenced_list().end()); }
104 { return &cident_sections_
; }
107 add_cident_section(std::string section_name
,
109 { this->cident_sections_
[section_name
].insert(secn
); }
113 Worklist_type work_list_
;
114 bool is_worklist_ready_
;
115 Section_ref section_reloc_map_
;
116 Sections_reachable referenced_list_
;
117 Cident_section_map cident_sections_
;
120 // Data to pass between successive invocations of do_layout
121 // in object.cc while garbage collecting. This data structure
122 // is filled by using the data from Read_symbols_data.
127 unsigned char* section_headers_data
;
129 unsigned char* section_names_data
;
130 // Size of section name data in bytes.
131 section_size_type section_names_size
;
133 unsigned char* symbols_data
;
134 // Size of symbol data in bytes.
135 section_size_type symbols_size
;
136 // Offset of external symbols within symbol data. This structure
137 // sometimes contains only external symbols, in which case this will
138 // be zero. Sometimes it contains all symbols.
139 section_offset_type external_symbols_offset
;
141 unsigned char* symbol_names_data
;
142 // Size of symbol name data in bytes.
143 section_size_type symbol_names_size
;
146 // This function implements the generic part of reloc
147 // processing to map a section to all the sections it
148 // references through relocs. It is called only during
149 // garbage collection (--gc-sections) and identical code
152 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
156 Symbol_table
* symtab
,
159 Sized_relobj
<size
, big_endian
>* src_obj
,
160 unsigned int src_indx
,
161 const unsigned char* prelocs
,
166 const unsigned char* plocal_syms
)
169 unsigned int dst_indx
;
171 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
172 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
173 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
175 std::vector
<Section_id
>* secvec
= NULL
;
176 std::vector
<Symbol
*>* symvec
= NULL
;
177 std::vector
<std::pair
<long long, long long> >* addendvec
= NULL
;
178 bool is_icf_tracked
= false;
179 const char* cident_section_name
= NULL
;
181 if (parameters
->options().icf_enabled()
182 && is_section_foldable_candidate(src_obj
->section_name(src_indx
).c_str()))
184 is_icf_tracked
= true;
185 Section_id
src_id(src_obj
, src_indx
);
186 secvec
= &symtab
->icf()->section_reloc_list()[src_id
];
187 symvec
= &symtab
->icf()->symbol_reloc_list()[src_id
];
188 addendvec
= &symtab
->icf()->addend_reloc_list()[src_id
];
191 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
193 Reltype
reloc(prelocs
);
194 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
195 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
196 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
=
197 Reloc_types
<sh_type
, size
, big_endian
>::get_reloc_addend_noerror(&reloc
);
199 if (r_sym
< local_count
)
201 gold_assert(plocal_syms
!= NULL
);
202 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
204 unsigned int shndx
= lsym
.get_st_shndx();
206 shndx
= src_obj
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
211 Section_id
dst_id(dst_obj
, dst_indx
);
214 (*secvec
).push_back(dst_id
);
215 (*symvec
).push_back(NULL
);
216 long long symvalue
= static_cast<long long>(lsym
.get_st_value());
217 (*addendvec
).push_back(std::make_pair(symvalue
,
218 static_cast<long long>(addend
)));
220 if (shndx
== src_indx
)
225 Symbol
* gsym
= src_obj
->global_symbol(r_sym
);
226 gold_assert(gsym
!= NULL
);
227 if (gsym
->is_forwarder())
228 gsym
= symtab
->resolve_forwards(gsym
);
229 if (gsym
->source() != Symbol::FROM_OBJECT
)
232 dst_obj
= gsym
->object();
233 dst_indx
= gsym
->shndx(&is_ordinary
);
236 Section_id
dst_id(dst_obj
, dst_indx
);
237 // If the symbol name matches '__start_XXX' then the section with
238 // the C identifier like name 'XXX' should not be garbage collected.
239 // A similar treatment to symbols with the name '__stop_XXX'.
240 if (is_prefix_of(cident_section_start_prefix
, gsym
->name()))
242 cident_section_name
= (gsym
->name()
243 + strlen(cident_section_start_prefix
));
245 else if (is_prefix_of(cident_section_stop_prefix
, gsym
->name()))
247 cident_section_name
= (gsym
->name()
248 + strlen(cident_section_stop_prefix
));
252 (*secvec
).push_back(dst_id
);
253 (*symvec
).push_back(gsym
);
254 Sized_symbol
<size
>* sized_gsym
=
255 static_cast<Sized_symbol
<size
>* >(gsym
);
257 static_cast<long long>(sized_gsym
->value());
258 (*addendvec
).push_back(std::make_pair(symvalue
,
259 static_cast<long long>(addend
)));
262 if (parameters
->options().gc_sections())
264 Section_id
src_id(src_obj
, src_indx
);
265 Section_id
dst_id(dst_obj
, dst_indx
);
266 Garbage_collection::Section_ref::iterator map_it
;
267 map_it
= symtab
->gc()->section_reloc_map().find(src_id
);
268 if (map_it
== symtab
->gc()->section_reloc_map().end())
270 symtab
->gc()->section_reloc_map()[src_id
].insert(dst_id
);
274 Garbage_collection::Sections_reachable
& v(map_it
->second
);
277 if (cident_section_name
!= NULL
)
279 Garbage_collection::Cident_section_map::iterator ele
=
280 symtab
->gc()->cident_sections()->find(std::string(cident_section_name
));
281 if (ele
== symtab
->gc()->cident_sections()->end())
283 Garbage_collection::Sections_reachable
&
284 v(symtab
->gc()->section_reloc_map()[src_id
]);
285 Garbage_collection::Sections_reachable
& cident_secn(ele
->second
);
286 for (Garbage_collection::Sections_reachable::iterator it_v
287 = cident_secn
.begin();
288 it_v
!= cident_secn
.end();
299 } // End of namespace gold.