Add -Wshadow to the gcc command line options used when compiling the binutils.
[binutils.git] / gold / reloc.h
blob30a55ff2da0e67732ef32126ad552fef0ba7bc28
1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@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.
23 #ifndef GOLD_RELOC_H
24 #define GOLD_RELOC_H
26 #include <vector>
27 #ifdef HAVE_BYTESWAP_H
28 #include <byteswap.h>
29 #endif
31 #include "elfcpp.h"
32 #include "workqueue.h"
34 namespace gold
37 class General_options;
38 class Object;
39 class Relobj;
40 class Read_relocs_data;
41 class Symbol;
42 class Layout;
43 class Output_data;
44 class Output_section;
46 template<int size>
47 class Sized_symbol;
49 template<int size, bool big_endian>
50 class Sized_relobj;
52 template<int size>
53 class Symbol_value;
55 template<int sh_type, bool dynamic, int size, bool big_endian>
56 class Output_data_reloc;
58 // A class to read the relocations for an object file, and then queue
59 // up a task to see if they require any GOT/PLT/COPY relocations in
60 // the symbol table.
62 class Read_relocs : public Task
64 public:
65 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
66 // unblocked when the Scan_relocs task completes.
67 Read_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
68 Task_token* symtab_lock, Task_token* blocker)
69 : symtab_(symtab), layout_(layout), object_(object),
70 symtab_lock_(symtab_lock), blocker_(blocker)
71 { }
73 // The standard Task methods.
75 Task_token*
76 is_runnable();
78 void
79 locks(Task_locker*);
81 void
82 run(Workqueue*);
84 std::string
85 get_name() const;
87 private:
88 Symbol_table* symtab_;
89 Layout* layout_;
90 Relobj* object_;
91 Task_token* symtab_lock_;
92 Task_token* blocker_;
95 // Process the relocs to figure out which sections are garbage.
96 // Very similar to scan relocs.
98 class Gc_process_relocs : public Task
100 public:
101 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
102 // unblocked when the task completes.
103 Gc_process_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
104 Read_relocs_data* rd, Task_token* symtab_lock,
105 Task_token* blocker)
106 : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
107 symtab_lock_(symtab_lock), blocker_(blocker)
110 // The standard Task methods.
112 Task_token*
113 is_runnable();
115 void
116 locks(Task_locker*);
118 void
119 run(Workqueue*);
121 std::string
122 get_name() const;
124 private:
125 Symbol_table* symtab_;
126 Layout* layout_;
127 Relobj* object_;
128 Read_relocs_data* rd_;
129 Task_token* symtab_lock_;
130 Task_token* blocker_;
133 // Scan the relocations for an object to see if they require any
134 // GOT/PLT/COPY relocations.
136 class Scan_relocs : public Task
138 public:
139 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
140 // unblocked when the task completes.
141 Scan_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
142 Read_relocs_data* rd, Task_token* symtab_lock,
143 Task_token* blocker)
144 : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
145 symtab_lock_(symtab_lock), blocker_(blocker)
148 // The standard Task methods.
150 Task_token*
151 is_runnable();
153 void
154 locks(Task_locker*);
156 void
157 run(Workqueue*);
159 std::string
160 get_name() const;
162 private:
163 Symbol_table* symtab_;
164 Layout* layout_;
165 Relobj* object_;
166 Read_relocs_data* rd_;
167 Task_token* symtab_lock_;
168 Task_token* blocker_;
171 // A class to perform all the relocations for an object file.
173 class Relocate_task : public Task
175 public:
176 Relocate_task(const Symbol_table* symtab, const Layout* layout,
177 Relobj* object, Output_file* of,
178 Task_token* input_sections_blocker,
179 Task_token* output_sections_blocker, Task_token* final_blocker)
180 : symtab_(symtab), layout_(layout), object_(object), of_(of),
181 input_sections_blocker_(input_sections_blocker),
182 output_sections_blocker_(output_sections_blocker),
183 final_blocker_(final_blocker)
186 // The standard Task methods.
188 Task_token*
189 is_runnable();
191 void
192 locks(Task_locker*);
194 void
195 run(Workqueue*);
197 std::string
198 get_name() const;
200 private:
201 const Symbol_table* symtab_;
202 const Layout* layout_;
203 Relobj* object_;
204 Output_file* of_;
205 Task_token* input_sections_blocker_;
206 Task_token* output_sections_blocker_;
207 Task_token* final_blocker_;
210 // During a relocatable link, this class records how relocations
211 // should be handled for a single input reloc section. An instance of
212 // this class is created while scanning relocs, and it is used while
213 // processing relocs.
215 class Relocatable_relocs
217 public:
218 // We use a vector of unsigned char to indicate how the input relocs
219 // should be handled. Each element is one of the following values.
220 // We create this vector when we initially scan the relocations.
221 enum Reloc_strategy
223 // Copy the input reloc. Don't modify it other than updating the
224 // r_offset field and the r_sym part of the r_info field.
225 RELOC_COPY,
226 // Copy the input reloc which is against an STT_SECTION symbol.
227 // Update the r_offset and r_sym part of the r_info field. Adjust
228 // the addend by subtracting the value of the old local symbol and
229 // adding the value of the new local symbol. The addend is in the
230 // SHT_RELA reloc and the contents of the data section do not need
231 // to be changed.
232 RELOC_ADJUST_FOR_SECTION_RELA,
233 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
234 // adjusted.
235 RELOC_ADJUST_FOR_SECTION_0,
236 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
237 // section need to be changed. The number indicates the number of
238 // bytes in the addend in the section contents.
239 RELOC_ADJUST_FOR_SECTION_1,
240 RELOC_ADJUST_FOR_SECTION_2,
241 RELOC_ADJUST_FOR_SECTION_4,
242 RELOC_ADJUST_FOR_SECTION_8,
243 // Discard the input reloc--process it completely when relocating
244 // the data section contents.
245 RELOC_DISCARD,
246 // An input reloc which is not discarded, but which requires
247 // target specific processing in order to update it.
248 RELOC_SPECIAL
251 Relocatable_relocs()
252 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
255 // Record the number of relocs.
256 void
257 set_reloc_count(size_t reloc_count)
258 { this->reloc_strategies_.reserve(reloc_count); }
260 // Record what to do for the next reloc.
261 void
262 set_next_reloc_strategy(Reloc_strategy astrategy)
264 this->reloc_strategies_.push_back(static_cast<unsigned char>(astrategy));
265 if (astrategy != RELOC_DISCARD)
266 ++this->output_reloc_count_;
269 // Record the Output_data associated with this reloc section.
270 void
271 set_output_data(Output_data* posd)
273 gold_assert(this->posd_ == NULL);
274 this->posd_ = posd;
277 // Return the Output_data associated with this reloc section.
278 Output_data*
279 output_data() const
280 { return this->posd_; }
282 // Return what to do for reloc I.
283 Reloc_strategy
284 strategy(unsigned int i) const
286 gold_assert(i < this->reloc_strategies_.size());
287 return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
290 // Return the number of relocations to create in the output file.
291 size_t
292 output_reloc_count() const
293 { return this->output_reloc_count_; }
295 private:
296 typedef std::vector<unsigned char> Reloc_strategies;
298 // The strategies for the input reloc. There is one entry in this
299 // vector for each relocation in the input section.
300 Reloc_strategies reloc_strategies_;
301 // The number of relocations to be created in the output file.
302 size_t output_reloc_count_;
303 // The output data structure associated with this relocation.
304 Output_data* posd_;
307 // Standard relocation routines which are used on many targets. Here
308 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
310 template<int size, bool big_endian>
311 class Relocate_functions
313 private:
314 // Do a simple relocation with the addend in the section contents.
315 // VALSIZE is the size of the value.
316 template<int valsize>
317 static inline void
318 rel(unsigned char* view,
319 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
321 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
322 Valtype* wv = reinterpret_cast<Valtype*>(view);
323 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
324 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
327 // Do a simple relocation using a Symbol_value with the addend in
328 // the section contents. VALSIZE is the size of the value to
329 // relocate.
330 template<int valsize>
331 static inline void
332 rel(unsigned char* view,
333 const Sized_relobj<size, big_endian>* object,
334 const Symbol_value<size>* psymval)
336 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
337 Valtype* wv = reinterpret_cast<Valtype*>(view);
338 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
339 x = psymval->value(object, x);
340 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
343 // Do a simple relocation with the addend in the relocation.
344 // VALSIZE is the size of the value.
345 template<int valsize>
346 static inline void
347 rela(unsigned char* view,
348 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
349 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
351 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
352 Valtype* wv = reinterpret_cast<Valtype*>(view);
353 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
356 // Do a simple relocation using a symbol value with the addend in
357 // the relocation. VALSIZE is the size of the value.
358 template<int valsize>
359 static inline void
360 rela(unsigned char* view,
361 const Sized_relobj<size, big_endian>* object,
362 const Symbol_value<size>* psymval,
363 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
365 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
366 Valtype* wv = reinterpret_cast<Valtype*>(view);
367 Valtype x = psymval->value(object, addend);
368 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
371 // Do a simple PC relative relocation with the addend in the section
372 // contents. VALSIZE is the size of the value.
373 template<int valsize>
374 static inline void
375 pcrel(unsigned char* view,
376 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
377 typename elfcpp::Elf_types<size>::Elf_Addr address)
379 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
380 Valtype* wv = reinterpret_cast<Valtype*>(view);
381 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
382 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
385 // Do a simple PC relative relocation with a Symbol_value with the
386 // addend in the section contents. VALSIZE is the size of the
387 // value.
388 template<int valsize>
389 static inline void
390 pcrel(unsigned char* view,
391 const Sized_relobj<size, big_endian>* object,
392 const Symbol_value<size>* psymval,
393 typename elfcpp::Elf_types<size>::Elf_Addr address)
395 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
396 Valtype* wv = reinterpret_cast<Valtype*>(view);
397 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
398 x = psymval->value(object, x);
399 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
402 // Do a simple PC relative relocation with the addend in the
403 // relocation. VALSIZE is the size of the value.
404 template<int valsize>
405 static inline void
406 pcrela(unsigned char* view,
407 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
408 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
409 typename elfcpp::Elf_types<size>::Elf_Addr address)
411 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
412 Valtype* wv = reinterpret_cast<Valtype*>(view);
413 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
416 // Do a simple PC relative relocation with a Symbol_value with the
417 // addend in the relocation. VALSIZE is the size of the value.
418 template<int valsize>
419 static inline void
420 pcrela(unsigned char* view,
421 const Sized_relobj<size, big_endian>* object,
422 const Symbol_value<size>* psymval,
423 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
424 typename elfcpp::Elf_types<size>::Elf_Addr address)
426 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
427 Valtype* wv = reinterpret_cast<Valtype*>(view);
428 Valtype x = psymval->value(object, addend);
429 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
432 typedef Relocate_functions<size, big_endian> This;
434 public:
435 // Do a simple 8-bit REL relocation with the addend in the section
436 // contents.
437 static inline void
438 rel8(unsigned char* view, unsigned char value)
439 { This::template rel<8>(view, value); }
441 static inline void
442 rel8(unsigned char* view,
443 const Sized_relobj<size, big_endian>* object,
444 const Symbol_value<size>* psymval)
445 { This::template rel<8>(view, object, psymval); }
447 // Do an 8-bit RELA relocation with the addend in the relocation.
448 static inline void
449 rela8(unsigned char* view, unsigned char value, unsigned char addend)
450 { This::template rela<8>(view, value, addend); }
452 static inline void
453 rela8(unsigned char* view,
454 const Sized_relobj<size, big_endian>* object,
455 const Symbol_value<size>* psymval,
456 unsigned char addend)
457 { This::template rela<8>(view, object, psymval, addend); }
459 // Do a simple 8-bit PC relative relocation with the addend in the
460 // section contents.
461 static inline void
462 pcrel8(unsigned char* view, unsigned char value,
463 typename elfcpp::Elf_types<size>::Elf_Addr address)
464 { This::template pcrel<8>(view, value, address); }
466 static inline void
467 pcrel8(unsigned char* view,
468 const Sized_relobj<size, big_endian>* object,
469 const Symbol_value<size>* psymval,
470 typename elfcpp::Elf_types<size>::Elf_Addr address)
471 { This::template pcrel<8>(view, object, psymval, address); }
473 // Do a simple 8-bit PC relative RELA relocation with the addend in
474 // the reloc.
475 static inline void
476 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
477 typename elfcpp::Elf_types<size>::Elf_Addr address)
478 { This::template pcrela<8>(view, value, addend, address); }
480 static inline void
481 pcrela8(unsigned char* view,
482 const Sized_relobj<size, big_endian>* object,
483 const Symbol_value<size>* psymval,
484 unsigned char addend,
485 typename elfcpp::Elf_types<size>::Elf_Addr address)
486 { This::template pcrela<8>(view, object, psymval, addend, address); }
488 // Do a simple 16-bit REL relocation with the addend in the section
489 // contents.
490 static inline void
491 rel16(unsigned char* view, elfcpp::Elf_Half value)
492 { This::template rel<16>(view, value); }
494 static inline void
495 rel16(unsigned char* view,
496 const Sized_relobj<size, big_endian>* object,
497 const Symbol_value<size>* psymval)
498 { This::template rel<16>(view, object, psymval); }
500 // Do an 16-bit RELA relocation with the addend in the relocation.
501 static inline void
502 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
503 { This::template rela<16>(view, value, addend); }
505 static inline void
506 rela16(unsigned char* view,
507 const Sized_relobj<size, big_endian>* object,
508 const Symbol_value<size>* psymval,
509 elfcpp::Elf_Half addend)
510 { This::template rela<16>(view, object, psymval, addend); }
512 // Do a simple 16-bit PC relative REL relocation with the addend in
513 // the section contents.
514 static inline void
515 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
516 typename elfcpp::Elf_types<size>::Elf_Addr address)
517 { This::template pcrel<16>(view, value, address); }
519 static inline void
520 pcrel16(unsigned char* view,
521 const Sized_relobj<size, big_endian>* object,
522 const Symbol_value<size>* psymval,
523 typename elfcpp::Elf_types<size>::Elf_Addr address)
524 { This::template pcrel<16>(view, object, psymval, address); }
526 // Do a simple 16-bit PC relative RELA relocation with the addend in
527 // the reloc.
528 static inline void
529 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
530 elfcpp::Elf_Half addend,
531 typename elfcpp::Elf_types<size>::Elf_Addr address)
532 { This::template pcrela<16>(view, value, addend, address); }
534 static inline void
535 pcrela16(unsigned char* view,
536 const Sized_relobj<size, big_endian>* object,
537 const Symbol_value<size>* psymval,
538 elfcpp::Elf_Half addend,
539 typename elfcpp::Elf_types<size>::Elf_Addr address)
540 { This::template pcrela<16>(view, object, psymval, addend, address); }
542 // Do a simple 32-bit REL relocation with the addend in the section
543 // contents.
544 static inline void
545 rel32(unsigned char* view, elfcpp::Elf_Word value)
546 { This::template rel<32>(view, value); }
548 static inline void
549 rel32(unsigned char* view,
550 const Sized_relobj<size, big_endian>* object,
551 const Symbol_value<size>* psymval)
552 { This::template rel<32>(view, object, psymval); }
554 // Do an 32-bit RELA relocation with the addend in the relocation.
555 static inline void
556 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
557 { This::template rela<32>(view, value, addend); }
559 static inline void
560 rela32(unsigned char* view,
561 const Sized_relobj<size, big_endian>* object,
562 const Symbol_value<size>* psymval,
563 elfcpp::Elf_Word addend)
564 { This::template rela<32>(view, object, psymval, addend); }
566 // Do a simple 32-bit PC relative REL relocation with the addend in
567 // the section contents.
568 static inline void
569 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
570 typename elfcpp::Elf_types<size>::Elf_Addr address)
571 { This::template pcrel<32>(view, value, address); }
573 static inline void
574 pcrel32(unsigned char* view,
575 const Sized_relobj<size, big_endian>* object,
576 const Symbol_value<size>* psymval,
577 typename elfcpp::Elf_types<size>::Elf_Addr address)
578 { This::template pcrel<32>(view, object, psymval, address); }
580 // Do a simple 32-bit PC relative RELA relocation with the addend in
581 // the relocation.
582 static inline void
583 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
584 elfcpp::Elf_Word addend,
585 typename elfcpp::Elf_types<size>::Elf_Addr address)
586 { This::template pcrela<32>(view, value, addend, address); }
588 static inline void
589 pcrela32(unsigned char* view,
590 const Sized_relobj<size, big_endian>* object,
591 const Symbol_value<size>* psymval,
592 elfcpp::Elf_Word addend,
593 typename elfcpp::Elf_types<size>::Elf_Addr address)
594 { This::template pcrela<32>(view, object, psymval, addend, address); }
596 // Do a simple 64-bit REL relocation with the addend in the section
597 // contents.
598 static inline void
599 rel64(unsigned char* view, elfcpp::Elf_Xword value)
600 { This::template rel<64>(view, value); }
602 static inline void
603 rel64(unsigned char* view,
604 const Sized_relobj<size, big_endian>* object,
605 const Symbol_value<size>* psymval)
606 { This::template rel<64>(view, object, psymval); }
608 // Do a 64-bit RELA relocation with the addend in the relocation.
609 static inline void
610 rela64(unsigned char* view, elfcpp::Elf_Xword value,
611 elfcpp::Elf_Xword addend)
612 { This::template rela<64>(view, value, addend); }
614 static inline void
615 rela64(unsigned char* view,
616 const Sized_relobj<size, big_endian>* object,
617 const Symbol_value<size>* psymval,
618 elfcpp::Elf_Xword addend)
619 { This::template rela<64>(view, object, psymval, addend); }
621 // Do a simple 64-bit PC relative REL relocation with the addend in
622 // the section contents.
623 static inline void
624 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
625 typename elfcpp::Elf_types<size>::Elf_Addr address)
626 { This::template pcrel<64>(view, value, address); }
628 static inline void
629 pcrel64(unsigned char* view,
630 const Sized_relobj<size, big_endian>* object,
631 const Symbol_value<size>* psymval,
632 typename elfcpp::Elf_types<size>::Elf_Addr address)
633 { This::template pcrel<64>(view, object, psymval, address); }
635 // Do a simple 64-bit PC relative RELA relocation with the addend in
636 // the relocation.
637 static inline void
638 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
639 elfcpp::Elf_Xword addend,
640 typename elfcpp::Elf_types<size>::Elf_Addr address)
641 { This::template pcrela<64>(view, value, addend, address); }
643 static inline void
644 pcrela64(unsigned char* view,
645 const Sized_relobj<size, big_endian>* object,
646 const Symbol_value<size>* psymval,
647 elfcpp::Elf_Xword addend,
648 typename elfcpp::Elf_types<size>::Elf_Addr address)
649 { This::template pcrela<64>(view, object, psymval, addend, address); }
652 // Track relocations while reading a section. This lets you ask for
653 // the relocation at a certain offset, and see how relocs occur
654 // between points of interest.
656 template<int size, bool big_endian>
657 class Track_relocs
659 public:
660 Track_relocs()
661 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
664 // Initialize the Track_relocs object. OBJECT is the object holding
665 // the reloc section, RELOC_SHNDX is the section index of the reloc
666 // section, and RELOC_TYPE is the type of the reloc section
667 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
668 // something went wrong.
669 bool
670 initialize(Object* object, unsigned int reloc_shndx,
671 unsigned int reloc_type);
673 // Return the offset in the data section to which the next reloc
674 // applies. THis returns -1 if there is no next reloc.
675 off_t
676 next_offset() const;
678 // Return the symbol index of the next reloc. This returns -1U if
679 // there is no next reloc.
680 unsigned int
681 next_symndx() const;
683 // Advance to OFFSET within the data section, and return the number
684 // of relocs which would be skipped.
686 advance(off_t offset);
688 private:
689 // The contents of the input object's reloc section.
690 const unsigned char* prelocs_;
691 // The length of the reloc section.
692 section_size_type len_;
693 // Our current position in the reloc section.
694 section_size_type pos_;
695 // The size of the relocs in the section.
696 int reloc_size_;
699 } // End namespace gold.
701 #endif // !defined(GOLD_RELOC_H)