]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/reloc.h
2ff49d84bf0cc6c69f5bdbb26249c859a91ac8e5
[thirdparty/binutils-gdb.git] / gold / reloc.h
1 // reloc.h -- relocate input files for gold -*- C++ -*-
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@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_RELOC_H
24 #define GOLD_RELOC_H
25
26 #include <byteswap.h>
27
28 #include "elfcpp.h"
29 #include "workqueue.h"
30
31 namespace gold
32 {
33
34 class General_options;
35 class Object;
36 class Relobj;
37 class Read_relocs_data;
38 class Symbol;
39 class Layout;
40 class Output_section;
41
42 template<int size>
43 class Sized_symbol;
44
45 template<int size, bool big_endian>
46 class Sized_relobj;
47
48 template<int size>
49 class Symbol_value;
50
51 template<int sh_type, bool dynamic, int size, bool big_endian>
52 class Output_data_reloc;
53
54 // A class to read the relocations for an object file, and then queue
55 // up a task to see if they require any GOT/PLT/COPY relocations in
56 // the symbol table.
57
58 class Read_relocs : public Task
59 {
60 public:
61 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
62 // unblocked when the Scan_relocs task completes.
63 Read_relocs(const General_options& options, Symbol_table* symtab,
64 Layout* layout, Relobj* object, Task_token* symtab_lock,
65 Task_token* blocker)
66 : options_(options), symtab_(symtab), layout_(layout), object_(object),
67 symtab_lock_(symtab_lock), blocker_(blocker)
68 { }
69
70 // The standard Task methods.
71
72 Is_runnable_type
73 is_runnable(Workqueue*);
74
75 Task_locker*
76 locks(Workqueue*);
77
78 void
79 run(Workqueue*);
80
81 private:
82 const General_options& options_;
83 Symbol_table* symtab_;
84 Layout* layout_;
85 Relobj* object_;
86 Task_token* symtab_lock_;
87 Task_token* blocker_;
88 };
89
90 // Scan the relocations for an object to see if they require any
91 // GOT/PLT/COPY relocations.
92
93 class Scan_relocs : public Task
94 {
95 public:
96 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
97 // unblocked when the task completes.
98 Scan_relocs(const General_options& options, Symbol_table* symtab,
99 Layout* layout, Relobj* object, Read_relocs_data* rd,
100 Task_token* symtab_lock, Task_token* blocker)
101 : options_(options), symtab_(symtab), layout_(layout), object_(object),
102 rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker)
103 { }
104
105 // The standard Task methods.
106
107 Is_runnable_type
108 is_runnable(Workqueue*);
109
110 Task_locker*
111 locks(Workqueue*);
112
113 void
114 run(Workqueue*);
115
116 private:
117 class Scan_relocs_locker;
118
119 const General_options& options_;
120 Symbol_table* symtab_;
121 Layout* layout_;
122 Relobj* object_;
123 Read_relocs_data* rd_;
124 Task_token* symtab_lock_;
125 Task_token* blocker_;
126 };
127
128 // A class to perform all the relocations for an object file.
129
130 class Relocate_task : public Task
131 {
132 public:
133 Relocate_task(const General_options& options, const Symbol_table* symtab,
134 const Layout* layout, Relobj* object, Output_file* of,
135 Task_token* input_sections_blocker,
136 Task_token* output_sections_blocker, Task_token* final_blocker)
137 : options_(options), symtab_(symtab), layout_(layout), object_(object),
138 of_(of), input_sections_blocker_(input_sections_blocker),
139 output_sections_blocker_(output_sections_blocker),
140 final_blocker_(final_blocker)
141 { }
142
143 // The standard Task methods.
144
145 Is_runnable_type
146 is_runnable(Workqueue*);
147
148 Task_locker*
149 locks(Workqueue*);
150
151 void
152 run(Workqueue*);
153
154 private:
155 class Relocate_locker;
156
157 const General_options& options_;
158 const Symbol_table* symtab_;
159 const Layout* layout_;
160 Relobj* object_;
161 Output_file* of_;
162 Task_token* input_sections_blocker_;
163 Task_token* output_sections_blocker_;
164 Task_token* final_blocker_;
165 };
166
167 // Standard relocation routines which are used on many targets. Here
168 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
169
170 template<int size, bool big_endian>
171 class Relocate_functions
172 {
173 private:
174 // Do a simple relocation with the addend in the section contents.
175 // VALSIZE is the size of the value.
176 template<int valsize>
177 static inline void
178 rel(unsigned char* view,
179 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
180 {
181 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
182 Valtype* wv = reinterpret_cast<Valtype*>(view);
183 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
184 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
185 }
186
187 // Do a simple relocation using a Symbol_value with the addend in
188 // the section contents. VALSIZE is the size of the value to
189 // relocate.
190 template<int valsize>
191 static inline void
192 rel(unsigned char* view,
193 const Sized_relobj<size, big_endian>* object,
194 const Symbol_value<size>* psymval)
195 {
196 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
197 Valtype* wv = reinterpret_cast<Valtype*>(view);
198 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
199 x = psymval->value(object, x);
200 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
201 }
202
203 // Do a simple relocation with the addend in the relocation.
204 // VALSIZE is the size of the value.
205 template<int valsize>
206 static inline void
207 rela(unsigned char* view,
208 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
209 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
210 {
211 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
212 Valtype* wv = reinterpret_cast<Valtype*>(view);
213 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
214 }
215
216 // Do a simple relocation using a symbol value with the addend in
217 // the relocation. VALSIZE is the size of the value.
218 template<int valsize>
219 static inline void
220 rela(unsigned char* view,
221 const Sized_relobj<size, big_endian>* object,
222 const Symbol_value<size>* psymval,
223 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
224 {
225 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
226 Valtype* wv = reinterpret_cast<Valtype*>(view);
227 Valtype x = psymval->value(object, addend);
228 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
229 }
230
231 // Do a simple PC relative relocation with the addend in the section
232 // contents. VALSIZE is the size of the value.
233 template<int valsize>
234 static inline void
235 pcrel(unsigned char* view,
236 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
237 typename elfcpp::Elf_types<size>::Elf_Addr address)
238 {
239 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
240 Valtype* wv = reinterpret_cast<Valtype*>(view);
241 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
242 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
243 }
244
245 // Do a simple PC relative relocation with a Symbol_value with the
246 // addend in the section contents. VALSIZE is the size of the
247 // value.
248 template<int valsize>
249 static inline void
250 pcrel(unsigned char* view,
251 const Sized_relobj<size, big_endian>* object,
252 const Symbol_value<size>* psymval,
253 typename elfcpp::Elf_types<size>::Elf_Addr address)
254 {
255 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
256 Valtype* wv = reinterpret_cast<Valtype*>(view);
257 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
258 x = psymval->value(object, x);
259 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
260 }
261
262 // Do a simple PC relative relocation with the addend in the
263 // relocation. VALSIZE is the size of the value.
264 template<int valsize>
265 static inline void
266 pcrela(unsigned char* view,
267 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
268 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
269 typename elfcpp::Elf_types<size>::Elf_Addr address)
270 {
271 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
272 Valtype* wv = reinterpret_cast<Valtype*>(view);
273 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
274 }
275
276 // Do a simple PC relative relocation with a Symbol_value with the
277 // addend in the relocation. VALSIZE is the size of the value.
278 template<int valsize>
279 static inline void
280 pcrela(unsigned char* view,
281 const Sized_relobj<size, big_endian>* object,
282 const Symbol_value<size>* psymval,
283 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
284 typename elfcpp::Elf_types<size>::Elf_Addr address)
285 {
286 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
287 Valtype* wv = reinterpret_cast<Valtype*>(view);
288 Valtype x = psymval->value(object, addend);
289 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
290 }
291
292 typedef Relocate_functions<size, big_endian> This;
293
294 public:
295 // Do a simple 8-bit REL relocation with the addend in the section
296 // contents.
297 static inline void
298 rel8(unsigned char* view, unsigned char value)
299 { This::template rel<8>(view, value); }
300
301 static inline void
302 rel8(unsigned char* view,
303 const Sized_relobj<size, big_endian>* object,
304 const Symbol_value<size>* psymval)
305 { This::template rel<8>(view, object, psymval); }
306
307 // Do an 8-bit RELA relocation with the addend in the relocation.
308 static inline void
309 rela8(unsigned char* view, unsigned char value, unsigned char addend)
310 { This::template rela<8>(view, value, addend); }
311
312 static inline void
313 rela8(unsigned char* view,
314 const Sized_relobj<size, big_endian>* object,
315 const Symbol_value<size>* psymval,
316 unsigned char addend)
317 { This::template rela<8>(view, object, psymval, addend); }
318
319 // Do a simple 8-bit PC relative relocation with the addend in the
320 // section contents.
321 static inline void
322 pcrel8(unsigned char* view, unsigned char value,
323 typename elfcpp::Elf_types<size>::Elf_Addr address)
324 { This::template pcrel<8>(view, value, address); }
325
326 static inline void
327 pcrel8(unsigned char* view,
328 const Sized_relobj<size, big_endian>* object,
329 const Symbol_value<size>* psymval,
330 typename elfcpp::Elf_types<size>::Elf_Addr address)
331 { This::template pcrel<8>(view, object, psymval, address); }
332
333 // Do a simple 8-bit PC relative RELA relocation with the addend in
334 // the reloc.
335 static inline void
336 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
337 typename elfcpp::Elf_types<size>::Elf_Addr address)
338 { This::template pcrela<8>(view, value, addend, address); }
339
340 static inline void
341 pcrela8(unsigned char* view,
342 const Sized_relobj<size, big_endian>* object,
343 const Symbol_value<size>* psymval,
344 unsigned char addend,
345 typename elfcpp::Elf_types<size>::Elf_Addr address)
346 { This::template pcrela<8>(view, object, psymval, addend, address); }
347
348 // Do a simple 16-bit REL relocation with the addend in the section
349 // contents.
350 static inline void
351 rel16(unsigned char* view, elfcpp::Elf_Half value)
352 { This::template rel<16>(view, value); }
353
354 static inline void
355 rel16(unsigned char* view,
356 const Sized_relobj<size, big_endian>* object,
357 const Symbol_value<size>* psymval)
358 { This::template rel<16>(view, object, psymval); }
359
360 // Do an 16-bit RELA relocation with the addend in the relocation.
361 static inline void
362 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
363 { This::template rela<16>(view, value, addend); }
364
365 static inline void
366 rela16(unsigned char* view,
367 const Sized_relobj<size, big_endian>* object,
368 const Symbol_value<size>* psymval,
369 elfcpp::Elf_Half addend)
370 { This::template rela<16>(view, object, psymval, addend); }
371
372 // Do a simple 16-bit PC relative REL relocation with the addend in
373 // the section contents.
374 static inline void
375 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
376 typename elfcpp::Elf_types<size>::Elf_Addr address)
377 { This::template pcrel<16>(view, value, address); }
378
379 static inline void
380 pcrel16(unsigned char* view,
381 const Sized_relobj<size, big_endian>* object,
382 const Symbol_value<size>* psymval,
383 typename elfcpp::Elf_types<size>::Elf_Addr address)
384 { This::template pcrel<16>(view, object, psymval, address); }
385
386 // Do a simple 16-bit PC relative RELA relocation with the addend in
387 // the reloc.
388 static inline void
389 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
390 elfcpp::Elf_Half addend,
391 typename elfcpp::Elf_types<size>::Elf_Addr address)
392 { This::template pcrela<16>(view, value, addend, address); }
393
394 static inline void
395 pcrela16(unsigned char* view,
396 const Sized_relobj<size, big_endian>* object,
397 const Symbol_value<size>* psymval,
398 elfcpp::Elf_Half addend,
399 typename elfcpp::Elf_types<size>::Elf_Addr address)
400 { This::template pcrela<16>(view, object, psymval, addend, address); }
401
402 // Do a simple 32-bit REL relocation with the addend in the section
403 // contents.
404 static inline void
405 rel32(unsigned char* view, elfcpp::Elf_Word value)
406 { This::template rel<32>(view, value); }
407
408 static inline void
409 rel32(unsigned char* view,
410 const Sized_relobj<size, big_endian>* object,
411 const Symbol_value<size>* psymval)
412 { This::template rel<32>(view, object, psymval); }
413
414 // Do an 32-bit RELA relocation with the addend in the relocation.
415 static inline void
416 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
417 { This::template rela<32>(view, value, addend); }
418
419 static inline void
420 rela32(unsigned char* view,
421 const Sized_relobj<size, big_endian>* object,
422 const Symbol_value<size>* psymval,
423 elfcpp::Elf_Word addend)
424 { This::template rela<32>(view, object, psymval, addend); }
425
426 // Do a simple 32-bit PC relative REL relocation with the addend in
427 // the section contents.
428 static inline void
429 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
430 typename elfcpp::Elf_types<size>::Elf_Addr address)
431 { This::template pcrel<32>(view, value, address); }
432
433 static inline void
434 pcrel32(unsigned char* view,
435 const Sized_relobj<size, big_endian>* object,
436 const Symbol_value<size>* psymval,
437 typename elfcpp::Elf_types<size>::Elf_Addr address)
438 { This::template pcrel<32>(view, object, psymval, address); }
439
440 // Do a simple 32-bit PC relative RELA relocation with the addend in
441 // the relocation.
442 static inline void
443 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
444 elfcpp::Elf_Word addend,
445 typename elfcpp::Elf_types<size>::Elf_Addr address)
446 { This::template pcrela<32>(view, value, addend, address); }
447
448 static inline void
449 pcrela32(unsigned char* view,
450 const Sized_relobj<size, big_endian>* object,
451 const Symbol_value<size>* psymval,
452 elfcpp::Elf_Word addend,
453 typename elfcpp::Elf_types<size>::Elf_Addr address)
454 { This::template pcrela<32>(view, object, psymval, addend, address); }
455
456 // Do a simple 64-bit REL relocation with the addend in the section
457 // contents.
458 static inline void
459 rel64(unsigned char* view, elfcpp::Elf_Xword value)
460 { This::template rel<64>(view, value); }
461
462 static inline void
463 rel64(unsigned char* view,
464 const Sized_relobj<size, big_endian>* object,
465 const Symbol_value<size>* psymval)
466 { This::template rel<64>(view, object, psymval); }
467
468 // Do a 64-bit RELA relocation with the addend in the relocation.
469 static inline void
470 rela64(unsigned char* view, elfcpp::Elf_Xword value,
471 elfcpp::Elf_Xword addend)
472 { This::template rela<64>(view, value, addend); }
473
474 static inline void
475 rela64(unsigned char* view,
476 const Sized_relobj<size, big_endian>* object,
477 const Symbol_value<size>* psymval,
478 elfcpp::Elf_Xword addend)
479 { This::template rela<64>(view, object, psymval, addend); }
480
481 // Do a simple 64-bit PC relative REL relocation with the addend in
482 // the section contents.
483 static inline void
484 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
485 typename elfcpp::Elf_types<size>::Elf_Addr address)
486 { This::template pcrel<64>(view, value, address); }
487
488 static inline void
489 pcrel64(unsigned char* view,
490 const Sized_relobj<size, big_endian>* object,
491 const Symbol_value<size>* psymval,
492 typename elfcpp::Elf_types<size>::Elf_Addr address)
493 { This::template pcrel<64>(view, object, psymval, address); }
494
495 // Do a simple 64-bit PC relative RELA relocation with the addend in
496 // the relocation.
497 static inline void
498 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
499 elfcpp::Elf_Xword addend,
500 typename elfcpp::Elf_types<size>::Elf_Addr address)
501 { This::template pcrela<64>(view, value, addend, address); }
502
503 static inline void
504 pcrela64(unsigned char* view,
505 const Sized_relobj<size, big_endian>* object,
506 const Symbol_value<size>* psymval,
507 elfcpp::Elf_Xword addend,
508 typename elfcpp::Elf_types<size>::Elf_Addr address)
509 { This::template pcrela<64>(view, object, psymval, addend, address); }
510 };
511
512 // We try to avoid COPY relocations when possible. A COPY relocation
513 // may be required when an executable refers to a variable defined in
514 // a shared library. COPY relocations are problematic because they
515 // tie the executable to the exact size of the variable in the shared
516 // library. We can avoid them if all the references to the variable
517 // are in a writeable section. In that case we can simply use dynamic
518 // relocations. However, when scanning relocs, we don't know when we
519 // see the relocation whether we will be forced to use a COPY
520 // relocation or not. So we have to save the relocation during the
521 // reloc scanning, and then emit it as a dynamic relocation if
522 // necessary. This class implements that. It is used by the target
523 // specific code.
524
525 template<int size, bool big_endian>
526 class Copy_relocs
527 {
528 public:
529 Copy_relocs()
530 : entries_()
531 { }
532
533 // Return whether we need a COPY reloc for a reloc against GSYM,
534 // which is being applied to section SHNDX in OBJECT.
535 static bool
536 need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
537 Sized_symbol<size>* gsym);
538
539 // Save a Rel against SYM for possible emission later. SHNDX is the
540 // index of the section to which the reloc is being applied.
541 void
542 save(Symbol* sym, Relobj*, unsigned int shndx,
543 Output_section* output_section, const elfcpp::Rel<size, big_endian>&);
544
545 // Save a Rela against SYM for possible emission later.
546 void
547 save(Symbol* sym, Relobj*, unsigned int shndx,
548 Output_section* output_section, const elfcpp::Rela<size, big_endian>&);
549
550 // Return whether there are any relocs to emit. This also discards
551 // entries which need not be emitted.
552 bool
553 any_to_emit();
554
555 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
556 // is still defined in the dynamic object).
557 template<int sh_type>
558 void
559 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
560
561 private:
562 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
563 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
564
565 // This POD class holds the entries we are saving.
566 class Copy_reloc_entry
567 {
568 public:
569 Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
570 Relobj* relobj, unsigned int shndx,
571 Output_section* output_section,
572 Address address, Addend addend)
573 : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
574 shndx_(shndx), output_section_(output_section),
575 address_(address), addend_(addend)
576 { }
577
578 // Return whether we should emit this reloc. If we should not
579 // emit, we clear it.
580 bool
581 should_emit();
582
583 // Emit this reloc.
584
585 void
586 emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
587
588 void
589 emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
590
591 private:
592 Symbol* sym_;
593 unsigned int reloc_type_;
594 Relobj* relobj_;
595 unsigned int shndx_;
596 Output_section* output_section_;
597 Address address_;
598 Addend addend_;
599 };
600
601 // A list of relocs to be saved.
602 typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
603
604 // The list of relocs we are saving.
605 Copy_reloc_entries entries_;
606 };
607
608 // Track relocations while reading a section. This lets you ask for
609 // the relocation at a certain offset, and see how relocs occur
610 // between points of interest.
611
612 template<int size, bool big_endian>
613 class Track_relocs
614 {
615 public:
616 Track_relocs()
617 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
618 { }
619
620 // Initialize the Track_relocs object. OBJECT is the object holding
621 // the reloc section, RELOC_SHNDX is the section index of the reloc
622 // section, and RELOC_TYPE is the type of the reloc section
623 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
624 // something went wrong.
625 bool
626 initialize(Object* object, unsigned int reloc_shndx,
627 unsigned int reloc_type);
628
629 // Return the offset in the data section to which the next reloc
630 // applies. THis returns -1 if there is no next reloc.
631 off_t
632 next_offset() const;
633
634 // Return the symbol index of the next reloc. This returns -1U if
635 // there is no next reloc.
636 unsigned int
637 next_symndx() const;
638
639 // Advance to OFFSET within the data section, and return the number
640 // of relocs which would be skipped.
641 int
642 advance(off_t offset);
643
644 private:
645 // The contents of the input object's reloc section.
646 const unsigned char* prelocs_;
647 // The length of the reloc section.
648 off_t len_;
649 // Our current position in the reloc section.
650 off_t pos_;
651 // The size of the relocs in the section.
652 int reloc_size_;
653 };
654
655 } // End namespace gold.
656
657 #endif // !defined(GOLD_RELOC_H)