Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // reloc.h -- relocate input files for gold -*- C++ -*- |
2 | ||
3 | #ifndef GOLD_RELOC_H | |
4 | #define GOLD_RELOC_H | |
5 | ||
92e059d8 ILT |
6 | #include <byteswap.h> |
7 | ||
61ba1cf9 ILT |
8 | #include "workqueue.h" |
9 | ||
10 | namespace gold | |
11 | { | |
12 | ||
a3ad94ed | 13 | class General_options; |
f6ce93d6 | 14 | class Relobj; |
92e059d8 | 15 | class Read_relocs_data; |
a3ad94ed | 16 | class Symbol; |
ead1e424 | 17 | class Layout; |
92e059d8 | 18 | |
5a6f7e2d ILT |
19 | template<int size> |
20 | class Sized_symbol; | |
21 | ||
b8e6aad9 ILT |
22 | template<int size, bool big_endian> |
23 | class Sized_relobj; | |
24 | ||
25 | template<int size> | |
26 | class Symbol_value; | |
27 | ||
5a6f7e2d ILT |
28 | template<int sh_type, bool dynamic, int size, bool big_endian> |
29 | class Output_data_reloc; | |
30 | ||
92e059d8 ILT |
31 | // A class to read the relocations for an object file, and then queue |
32 | // up a task to see if they require any GOT/PLT/COPY relocations in | |
33 | // the symbol table. | |
34 | ||
35 | class Read_relocs : public Task | |
36 | { | |
37 | public: | |
38 | // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be | |
39 | // unblocked when the Scan_relocs task completes. | |
40 | Read_relocs(const General_options& options, Symbol_table* symtab, | |
f6ce93d6 | 41 | Layout* layout, Relobj* object, Task_token* symtab_lock, |
92e059d8 | 42 | Task_token* blocker) |
ead1e424 | 43 | : options_(options), symtab_(symtab), layout_(layout), object_(object), |
92e059d8 ILT |
44 | symtab_lock_(symtab_lock), blocker_(blocker) |
45 | { } | |
46 | ||
47 | // The standard Task methods. | |
48 | ||
49 | Is_runnable_type | |
50 | is_runnable(Workqueue*); | |
51 | ||
52 | Task_locker* | |
53 | locks(Workqueue*); | |
54 | ||
55 | void | |
56 | run(Workqueue*); | |
57 | ||
58 | private: | |
59 | const General_options& options_; | |
60 | Symbol_table* symtab_; | |
ead1e424 | 61 | Layout* layout_; |
f6ce93d6 | 62 | Relobj* object_; |
92e059d8 ILT |
63 | Task_token* symtab_lock_; |
64 | Task_token* blocker_; | |
65 | }; | |
66 | ||
67 | // Scan the relocations for an object to see if they require any | |
68 | // GOT/PLT/COPY relocations. | |
69 | ||
70 | class Scan_relocs : public Task | |
71 | { | |
72 | public: | |
73 | // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be | |
74 | // unblocked when the task completes. | |
75 | Scan_relocs(const General_options& options, Symbol_table* symtab, | |
f6ce93d6 | 76 | Layout* layout, Relobj* object, Read_relocs_data* rd, |
ead1e424 ILT |
77 | Task_token* symtab_lock, Task_token* blocker) |
78 | : options_(options), symtab_(symtab), layout_(layout), object_(object), | |
79 | rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker) | |
92e059d8 ILT |
80 | { } |
81 | ||
82 | // The standard Task methods. | |
83 | ||
84 | Is_runnable_type | |
85 | is_runnable(Workqueue*); | |
86 | ||
87 | Task_locker* | |
88 | locks(Workqueue*); | |
89 | ||
90 | void | |
91 | run(Workqueue*); | |
92 | ||
93 | private: | |
94 | class Scan_relocs_locker; | |
95 | ||
96 | const General_options& options_; | |
97 | Symbol_table* symtab_; | |
ead1e424 | 98 | Layout* layout_; |
f6ce93d6 | 99 | Relobj* object_; |
92e059d8 ILT |
100 | Read_relocs_data* rd_; |
101 | Task_token* symtab_lock_; | |
102 | Task_token* blocker_; | |
103 | }; | |
104 | ||
105 | // A class to perform all the relocations for an object file. | |
106 | ||
61ba1cf9 ILT |
107 | class Relocate_task : public Task |
108 | { | |
109 | public: | |
110 | Relocate_task(const General_options& options, const Symbol_table* symtab, | |
f6ce93d6 | 111 | const Layout* layout, Relobj* object, Output_file* of, |
61ba1cf9 | 112 | Task_token* final_blocker) |
92e059d8 | 113 | : options_(options), symtab_(symtab), layout_(layout), object_(object), |
61ba1cf9 ILT |
114 | of_(of), final_blocker_(final_blocker) |
115 | { } | |
116 | ||
117 | // The standard Task methods. | |
118 | ||
119 | Is_runnable_type | |
120 | is_runnable(Workqueue*); | |
121 | ||
122 | Task_locker* | |
123 | locks(Workqueue*); | |
124 | ||
125 | void | |
126 | run(Workqueue*); | |
127 | ||
128 | private: | |
129 | class Relocate_locker; | |
130 | ||
131 | const General_options& options_; | |
132 | const Symbol_table* symtab_; | |
92e059d8 | 133 | const Layout* layout_; |
f6ce93d6 | 134 | Relobj* object_; |
61ba1cf9 ILT |
135 | Output_file* of_; |
136 | Task_token* final_blocker_; | |
137 | }; | |
138 | ||
92e059d8 ILT |
139 | // Standard relocation routines which are used on many targets. Here |
140 | // SIZE and BIG_ENDIAN refer to the target, not the relocation type. | |
141 | ||
142 | template<int size, bool big_endian> | |
143 | class Relocate_functions | |
144 | { | |
145 | private: | |
146 | // Do a simple relocation with the addend in the section contents. | |
147 | // VALSIZE is the size of the value. | |
148 | template<int valsize> | |
149 | static inline void | |
f6ce93d6 ILT |
150 | rel(unsigned char* view, |
151 | typename elfcpp::Swap<valsize, big_endian>::Valtype value) | |
92e059d8 | 152 | { |
f6ce93d6 | 153 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; |
92e059d8 | 154 | Valtype* wv = reinterpret_cast<Valtype*>(view); |
f6ce93d6 ILT |
155 | Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv); |
156 | elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value); | |
92e059d8 ILT |
157 | } |
158 | ||
b8e6aad9 ILT |
159 | // Do a simple relocation using a Symbol_value with the addend in |
160 | // the section contents. VALSIZE is the size of the value to | |
161 | // relocate. | |
162 | template<int valsize> | |
163 | static inline void | |
164 | rel(unsigned char* view, | |
165 | const Sized_relobj<size, big_endian>* object, | |
166 | const Symbol_value<size>* psymval) | |
167 | { | |
168 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
169 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
170 | Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv); | |
171 | x = psymval->value(object, x); | |
172 | elfcpp::Swap<valsize, big_endian>::writeval(wv, x); | |
173 | } | |
174 | ||
92e059d8 ILT |
175 | // Do a simple PC relative relocation with the addend in the section |
176 | // contents. VALSIZE is the size of the value. | |
177 | template<int valsize> | |
178 | static inline void | |
f6ce93d6 ILT |
179 | pcrel(unsigned char* view, |
180 | typename elfcpp::Swap<valsize, big_endian>::Valtype value, | |
92e059d8 ILT |
181 | typename elfcpp::Elf_types<size>::Elf_Addr address) |
182 | { | |
f6ce93d6 | 183 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; |
92e059d8 | 184 | Valtype* wv = reinterpret_cast<Valtype*>(view); |
f6ce93d6 ILT |
185 | Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv); |
186 | elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address); | |
92e059d8 ILT |
187 | } |
188 | ||
b8e6aad9 ILT |
189 | // Do a simple PC relative relocation with a Symbol_value with the |
190 | // addend in the section contents. VALSIZE is the size of the | |
191 | // value. | |
192 | template<int valsize> | |
193 | static inline void | |
194 | pcrel(unsigned char* view, | |
195 | const Sized_relobj<size, big_endian>* object, | |
196 | const Symbol_value<size>* psymval, | |
197 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
198 | { | |
199 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
200 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
201 | Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv); | |
202 | x = psymval->value(object, x); | |
203 | elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address); | |
204 | } | |
205 | ||
92e059d8 ILT |
206 | typedef Relocate_functions<size, big_endian> This; |
207 | ||
208 | public: | |
b8e6aad9 ILT |
209 | // Do a simple 8-bit REL relocation with the addend in the section |
210 | // contents. | |
92e059d8 ILT |
211 | static inline void |
212 | rel8(unsigned char* view, unsigned char value) | |
b8e6aad9 ILT |
213 | { This::template rel<8>(view, value); } |
214 | ||
215 | static inline void | |
216 | rel8(unsigned char* view, | |
217 | const Sized_relobj<size, big_endian>* object, | |
218 | const Symbol_value<size>* psymval) | |
219 | { This::template rel<8>(view, object, psymval); } | |
92e059d8 ILT |
220 | |
221 | // Do a simple 8-bit PC relative relocation with the addend in the | |
b8e6aad9 | 222 | // section contents. |
92e059d8 ILT |
223 | static inline void |
224 | pcrel8(unsigned char* view, unsigned char value, | |
225 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
b8e6aad9 ILT |
226 | { This::template pcrel<8>(view, value, address); } |
227 | ||
228 | static inline void | |
229 | pcrel8(unsigned char* view, | |
230 | const Sized_relobj<size, big_endian>* object, | |
231 | const Symbol_value<size>* psymval, | |
232 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
233 | { This::template pcrel<8>(view, object, psymval, address); } | |
92e059d8 | 234 | |
b8e6aad9 ILT |
235 | // Do a simple 16-bit REL relocation with the addend in the section |
236 | // contents. | |
92e059d8 ILT |
237 | static inline void |
238 | rel16(unsigned char* view, elfcpp::Elf_Half value) | |
b8e6aad9 ILT |
239 | { This::template rel<16>(view, value); } |
240 | ||
241 | static inline void | |
242 | rel16(unsigned char* view, | |
243 | const Sized_relobj<size, big_endian>* object, | |
244 | const Symbol_value<size>* psymval) | |
245 | { This::template rel<16>(view, object, psymval); } | |
92e059d8 ILT |
246 | |
247 | // Do a simple 32-bit PC relative REL relocation with the addend in | |
b8e6aad9 | 248 | // the section contents. |
92e059d8 ILT |
249 | static inline void |
250 | pcrel16(unsigned char* view, elfcpp::Elf_Word value, | |
251 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
b8e6aad9 ILT |
252 | { This::template pcrel<16>(view, value, address); } |
253 | ||
254 | static inline void | |
255 | pcrel16(unsigned char* view, | |
256 | const Sized_relobj<size, big_endian>* object, | |
257 | const Symbol_value<size>* psymval, | |
258 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
259 | { This::template pcrel<16>(view, object, psymval, address); } | |
92e059d8 ILT |
260 | |
261 | // Do a simple 32-bit REL relocation with the addend in the section | |
262 | // contents. | |
263 | static inline void | |
264 | rel32(unsigned char* view, elfcpp::Elf_Word value) | |
b8e6aad9 ILT |
265 | { This::template rel<32>(view, value); } |
266 | ||
267 | static inline void | |
268 | rel32(unsigned char* view, | |
269 | const Sized_relobj<size, big_endian>* object, | |
270 | const Symbol_value<size>* psymval) | |
271 | { This::template rel<32>(view, object, psymval); } | |
92e059d8 ILT |
272 | |
273 | // Do a simple 32-bit PC relative REL relocation with the addend in | |
274 | // the section contents. | |
275 | static inline void | |
276 | pcrel32(unsigned char* view, elfcpp::Elf_Word value, | |
277 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
b8e6aad9 ILT |
278 | { This::template pcrel<32>(view, value, address); } |
279 | ||
280 | static inline void | |
281 | pcrel32(unsigned char* view, | |
282 | const Sized_relobj<size, big_endian>* object, | |
283 | const Symbol_value<size>* psymval, | |
284 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
285 | { This::template pcrel<32>(view, object, psymval, address); } | |
92e059d8 ILT |
286 | |
287 | // Do a simple 64-bit REL relocation with the addend in the section | |
288 | // contents. | |
289 | static inline void | |
f6ce93d6 | 290 | rel64(unsigned char* view, elfcpp::Elf_Xword value) |
b8e6aad9 ILT |
291 | { This::template rel<64>(view, value); } |
292 | ||
293 | static inline void | |
294 | rel64(unsigned char* view, | |
295 | const Sized_relobj<size, big_endian>* object, | |
296 | const Symbol_value<size>* psymval) | |
297 | { This::template rel<64>(view, object, psymval); } | |
92e059d8 ILT |
298 | |
299 | // Do a simple 64-bit PC relative REL relocation with the addend in | |
300 | // the section contents. | |
301 | static inline void | |
f6ce93d6 | 302 | pcrel64(unsigned char* view, elfcpp::Elf_Xword value, |
92e059d8 | 303 | typename elfcpp::Elf_types<size>::Elf_Addr address) |
b8e6aad9 ILT |
304 | { This::template pcrel<64>(view, value, address); } |
305 | ||
306 | static inline void | |
307 | pcrel64(unsigned char* view, | |
308 | const Sized_relobj<size, big_endian>* object, | |
309 | const Symbol_value<size>* psymval, | |
310 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
311 | { This::template pcrel<64>(view, object, psymval, address); } | |
5a6f7e2d ILT |
312 | }; |
313 | ||
314 | // We try to avoid COPY relocations when possible. A COPY relocation | |
315 | // may be required when an executable refers to a variable defined in | |
316 | // a shared library. COPY relocations are problematic because they | |
317 | // tie the executable to the exact size of the variable in the shared | |
318 | // library. We can avoid them if all the references to the variable | |
319 | // are in a writeable section. In that case we can simply use dynamic | |
320 | // relocations. However, when scanning relocs, we don't know when we | |
321 | // see the relocation whether we will be forced to use a COPY | |
322 | // relocation or not. So we have to save the relocation during the | |
323 | // reloc scanning, and then emit it as a dynamic relocation if | |
324 | // necessary. This class implements that. It is used by the target | |
325 | // specific code. | |
326 | ||
327 | template<int size, bool big_endian> | |
328 | class Copy_relocs | |
329 | { | |
330 | public: | |
331 | Copy_relocs() | |
332 | : entries_() | |
333 | { } | |
a3ad94ed ILT |
334 | |
335 | // Return whether we need a COPY reloc for a reloc against GSYM, | |
336 | // which is being applied to section SHNDX in OBJECT. | |
337 | static bool | |
338 | need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx, | |
5a6f7e2d ILT |
339 | Sized_symbol<size>* gsym); |
340 | ||
341 | // Save a Rel against SYM for possible emission later. SHNDX is the | |
342 | // index of the section to which the reloc is being applied. | |
343 | void | |
344 | save(Symbol* sym, Relobj*, unsigned int shndx, | |
345 | const elfcpp::Rel<size, big_endian>&); | |
346 | ||
347 | // Save a Rela against SYM for possible emission later. | |
348 | void | |
349 | save(Symbol* sym, Relobj*, unsigned int shndx, | |
350 | const elfcpp::Rela<size, big_endian>&); | |
351 | ||
352 | // Return whether there are any relocs to emit. This also discards | |
353 | // entries which need not be emitted. | |
354 | bool | |
355 | any_to_emit(); | |
356 | ||
357 | // Emit relocs for each symbol which did not get a COPY reloc (i.e., | |
358 | // is still defined in the dynamic object). | |
359 | template<int sh_type> | |
360 | void | |
361 | emit(Output_data_reloc<sh_type, true, size, big_endian>*); | |
362 | ||
363 | private: | |
364 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
365 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend; | |
366 | ||
367 | // This POD class holds the entries we are saving. | |
368 | class Copy_reloc_entry | |
369 | { | |
370 | public: | |
371 | Copy_reloc_entry(Symbol* sym, unsigned int reloc_type, | |
372 | Relobj* relobj, unsigned int shndx, | |
373 | Address address, Addend addend) | |
374 | : sym_(sym), reloc_type_(reloc_type), relobj_(relobj), | |
375 | shndx_(shndx), address_(address), addend_(addend) | |
376 | { } | |
377 | ||
378 | // Return whether we should emit this reloc. If we should not | |
379 | // emit, we clear it. | |
380 | bool | |
381 | should_emit(); | |
382 | ||
383 | // Emit this reloc. | |
384 | ||
385 | void | |
386 | emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*); | |
387 | ||
388 | void | |
389 | emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*); | |
390 | ||
391 | private: | |
392 | Symbol* sym_; | |
393 | unsigned int reloc_type_; | |
394 | Relobj* relobj_; | |
395 | unsigned int shndx_; | |
396 | Address address_; | |
397 | Addend addend_; | |
398 | }; | |
399 | ||
400 | // A list of relocs to be saved. | |
401 | typedef std::vector<Copy_reloc_entry> Copy_reloc_entries; | |
402 | ||
403 | // The list of relocs we are saving. | |
404 | Copy_reloc_entries entries_; | |
92e059d8 ILT |
405 | }; |
406 | ||
61ba1cf9 ILT |
407 | } // End namespace gold. |
408 | ||
409 | #endif // !defined(GOLD_RELOC_H) |