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