*** empty log message ***
[deliverable/binutils-gdb.git] / gold / gc.h
CommitLineData
6d03d481
ST
1// gc.h -- garbage collection of unused sections
2
3// Copyright 2009 Free Software Foundation, Inc.
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"
31
32namespace gold
33{
34
35class Object;
36
37template<int size, bool big_endian>
38class Sized_relobj;
39
40template<int sh_type, int size, bool big_endian>
41class Reloc_types;
42
43class Output_section;
44class General_options;
45class Layout;
46
47typedef std::pair<Object *, unsigned int> Section_id;
48
49class Garbage_collection
50{
51 struct Section_id_hash
52 {
53 size_t operator()(const Section_id& loc) const
54 { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
55 };
56
ef15dade
ST
57 public:
58
6d03d481
ST
59 typedef Unordered_set<Section_id, Section_id_hash> Sections_reachable;
60 typedef std::map<Section_id, Sections_reachable> Section_ref;
61 typedef std::queue<Section_id> Worklist_type;
62
ef15dade
ST
63 Garbage_collection()
64 : is_worklist_ready_(false)
65 { }
66
67 // Accessor methods for the private members.
68
69 Sections_reachable&
70 referenced_list()
71 { return referenced_list_; }
72
73 Section_ref&
74 section_reloc_map()
75 { return this->section_reloc_map_; }
76
77 Worklist_type&
78 worklist()
79 { return this->work_list_; }
80
81 bool
82 is_worklist_ready()
83 { return this->is_worklist_ready_; }
84
85 void
86 worklist_ready()
87 { this->is_worklist_ready_ = true; }
88
89 void
90 do_transitive_closure();
91
92 bool
93 is_section_garbage(Object* obj, unsigned int shndx)
94 { return (this->referenced_list().find(Section_id(obj, shndx))
95 == this->referenced_list().end()); }
96 private:
97
98 Worklist_type work_list_;
99 bool is_worklist_ready_;
100 Section_ref section_reloc_map_;
101 Sections_reachable referenced_list_;
6d03d481
ST
102};
103
104// Data to pass between successive invocations of do_layout
105// in object.cc while garbage collecting. This data structure
106// is filled by using the data from Read_symbols_data.
107
108struct Symbols_data
109{
110 // Section headers.
111 unsigned char* section_headers_data;
112 // Section names.
113 unsigned char* section_names_data;
114 // Size of section name data in bytes.
115 section_size_type section_names_size;
116 // Symbol data.
117 unsigned char* symbols_data;
118 // Size of symbol data in bytes.
119 section_size_type symbols_size;
120 // Offset of external symbols within symbol data. This structure
121 // sometimes contains only external symbols, in which case this will
122 // be zero. Sometimes it contains all symbols.
123 section_offset_type external_symbols_offset;
124 // Symbol names.
125 unsigned char* symbol_names_data;
126 // Size of symbol name data in bytes.
127 section_size_type symbol_names_size;
128};
129
ef15dade
ST
130// This function implements the generic part of reloc
131// processing to map a section to all the sections it
132// references through relocs. It is called only during
133// garbage collection (--gc-sections) and identical code
134// folding (--icf).
6d03d481
ST
135
136template<int size, bool big_endian, typename Target_type, int sh_type,
137 typename Scan>
138inline void
139gc_process_relocs(
140 const General_options& ,
141 Symbol_table* symtab,
142 Layout*,
143 Target_type* ,
ef15dade
ST
144 Sized_relobj<size, big_endian>* src_obj,
145 unsigned int src_indx,
6d03d481
ST
146 const unsigned char* prelocs,
147 size_t reloc_count,
148 Output_section*,
149 bool ,
150 size_t local_count,
151 const unsigned char* plocal_syms)
152{
ef15dade
ST
153 Object *dst_obj;
154 unsigned int dst_indx;
6d03d481 155
6d03d481
ST
156 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
157 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
158 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
159
ef15dade
ST
160 std::vector<Section_id>* secvec = NULL;
161 std::vector<Symbol*>* symvec = NULL;
162 std::vector<std::pair<long long, long long> >* addendvec = NULL;
163 bool is_icf_tracked = false;
164
165 if (parameters->options().icf()
166 && is_prefix_of(".text.", (src_obj)->section_name(src_indx).c_str()))
167 {
168 is_icf_tracked = true;
169 Section_id src_id(src_obj, src_indx);
170 secvec = &symtab->icf()->section_reloc_list()[src_id];
171 symvec = &symtab->icf()->symbol_reloc_list()[src_id];
172 addendvec = &symtab->icf()->addend_reloc_list()[src_id];
173 }
174
6d03d481
ST
175 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
176 {
177 Reltype reloc(prelocs);
178 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
179 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
ef15dade
ST
180 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
181 Reloc_types<sh_type, size, big_endian>::get_reloc_addend_noerror(&reloc);
182
6d03d481
ST
183 if (r_sym < local_count)
184 {
185 gold_assert(plocal_syms != NULL);
186 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
187 + r_sym * sym_size);
188 unsigned int shndx = lsym.get_st_shndx();
189 bool is_ordinary;
ef15dade
ST
190 shndx = src_obj->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
191 if (!is_ordinary)
6d03d481
ST
192 continue;
193 dst_obj = src_obj;
6d03d481 194 dst_indx = shndx;
ef15dade
ST
195 Section_id dst_id(dst_obj, dst_indx);
196 if (is_icf_tracked)
197 {
198 (*secvec).push_back(dst_id);
199 (*symvec).push_back(NULL);
200 long long symvalue = static_cast<long long>(lsym.get_st_value());
201 (*addendvec).push_back(std::make_pair(symvalue,
202 static_cast<long long>(addend)));
203 }
204 if (shndx == src_indx)
205 continue;
6d03d481
ST
206 }
207 else
208 {
ef15dade 209 Symbol* gsym = src_obj->global_symbol(r_sym);
6d03d481
ST
210 gold_assert(gsym != NULL);
211 if (gsym->is_forwarder())
212 gsym = symtab->resolve_forwards(gsym);
213 if (gsym->source() != Symbol::FROM_OBJECT)
214 continue;
215 bool is_ordinary;
216 dst_obj = gsym->object();
217 dst_indx = gsym->shndx(&is_ordinary);
218 if (!is_ordinary)
219 continue;
ef15dade
ST
220 Section_id dst_id(dst_obj, dst_indx);
221 if (is_icf_tracked)
222 {
223 (*secvec).push_back(dst_id);
224 (*symvec).push_back(gsym);
225 Sized_symbol<size>* sized_gsym =
226 static_cast<Sized_symbol<size>* >(gsym);
227 long long symvalue =
228 static_cast<long long>(sized_gsym->value());
229 (*addendvec).push_back(std::make_pair(symvalue,
230 static_cast<long long>(addend)));
231 }
6d03d481 232 }
ef15dade 233 if (parameters->options().gc_sections())
6d03d481 234 {
ef15dade
ST
235 Section_id src_id(src_obj, src_indx);
236 Section_id dst_id(dst_obj, dst_indx);
237 Garbage_collection::Section_ref::iterator map_it;
238 map_it = symtab->gc()->section_reloc_map().find(src_id);
239 if (map_it == symtab->gc()->section_reloc_map().end())
240 {
241 symtab->gc()->section_reloc_map()[src_id].insert(dst_id);
242 }
243 else
244 {
245 Garbage_collection::Sections_reachable& v(map_it->second);
246 v.insert(dst_id);
247 }
6d03d481
ST
248 }
249 }
250 return;
251}
252
253} // End of namespace gold.
254
255#endif
This page took 0.068045 seconds and 4 git commands to generate.