1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 // Copyright (C) 2006-2016 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.
27 #ifdef HAVE_BYTESWAP_H
32 #include "workqueue.h"
37 class General_options;
40 struct Read_relocs_data;
49 template<int size, bool big_endian>
50 class Sized_relobj_file;
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
62 class Read_relocs : public Task
65 // THIS_BLOCKER and NEXT_BLOCKER are passed along to a Scan_relocs
66 // or Gc_process_relocs task, so that they run in a deterministic
68 Read_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
69 Task_token* this_blocker, Task_token* next_blocker)
70 : symtab_(symtab), layout_(layout), object_(object),
71 this_blocker_(this_blocker), next_blocker_(next_blocker)
74 // The standard Task methods.
89 Symbol_table* symtab_;
92 Task_token* this_blocker_;
93 Task_token* next_blocker_;
96 // Process the relocs to figure out which sections are garbage.
97 // Very similar to scan relocs.
99 class Gc_process_relocs : public Task
102 // THIS_BLOCKER prevents this task from running until the previous
103 // one is finished. NEXT_BLOCKER prevents the next task from
105 Gc_process_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
106 Read_relocs_data* rd, Task_token* this_blocker,
107 Task_token* next_blocker)
108 : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
109 this_blocker_(this_blocker), next_blocker_(next_blocker)
112 ~Gc_process_relocs();
114 // The standard Task methods.
129 Symbol_table* symtab_;
132 Read_relocs_data* rd_;
133 Task_token* this_blocker_;
134 Task_token* next_blocker_;
137 // Scan the relocations for an object to see if they require any
138 // GOT/PLT/COPY relocations.
140 class Scan_relocs : public Task
143 // THIS_BLOCKER prevents this task from running until the previous
144 // one is finished. NEXT_BLOCKER prevents the next task from
146 Scan_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
147 Read_relocs_data* rd, Task_token* this_blocker,
148 Task_token* next_blocker)
149 : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
150 this_blocker_(this_blocker), next_blocker_(next_blocker)
155 // The standard Task methods.
170 Symbol_table* symtab_;
173 Read_relocs_data* rd_;
174 Task_token* this_blocker_;
175 Task_token* next_blocker_;
178 // A class to perform all the relocations for an object file.
180 class Relocate_task : public Task
183 Relocate_task(const Symbol_table* symtab, const Layout* layout,
184 Relobj* object, Output_file* of,
185 Task_token* input_sections_blocker,
186 Task_token* output_sections_blocker, Task_token* final_blocker)
187 : symtab_(symtab), layout_(layout), object_(object), of_(of),
188 input_sections_blocker_(input_sections_blocker),
189 output_sections_blocker_(output_sections_blocker),
190 final_blocker_(final_blocker)
193 // The standard Task methods.
208 const Symbol_table* symtab_;
209 const Layout* layout_;
212 Task_token* input_sections_blocker_;
213 Task_token* output_sections_blocker_;
214 Task_token* final_blocker_;
217 // During a relocatable link, this class records how relocations
218 // should be handled for a single input reloc section. An instance of
219 // this class is created while scanning relocs, and it is used while
220 // processing relocs.
222 class Relocatable_relocs
225 // We use a vector of unsigned char to indicate how the input relocs
226 // should be handled. Each element is one of the following values.
227 // We create this vector when we initially scan the relocations.
230 // Copy the input reloc. Don't modify it other than updating the
231 // r_offset field and the r_sym part of the r_info field.
233 // Copy the input reloc which is against an STT_SECTION symbol.
234 // Update the r_offset and r_sym part of the r_info field. Adjust
235 // the addend by subtracting the value of the old local symbol and
236 // adding the value of the new local symbol. The addend is in the
237 // SHT_RELA reloc and the contents of the data section do not need
239 RELOC_ADJUST_FOR_SECTION_RELA,
240 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
242 RELOC_ADJUST_FOR_SECTION_0,
243 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
244 // section need to be changed. The number indicates the number of
245 // bytes in the addend in the section contents.
246 RELOC_ADJUST_FOR_SECTION_1,
247 RELOC_ADJUST_FOR_SECTION_2,
248 RELOC_ADJUST_FOR_SECTION_4,
249 RELOC_ADJUST_FOR_SECTION_8,
250 // Like RELOC_ADJUST_FOR_SECTION_4 but for unaligned relocs.
251 RELOC_ADJUST_FOR_SECTION_4_UNALIGNED,
252 // Discard the input reloc--process it completely when relocating
253 // the data section contents.
255 // An input reloc which is not discarded, but which requires
256 // target specific processing in order to update it.
261 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
264 // Record the number of relocs.
266 set_reloc_count(size_t reloc_count)
267 { this->reloc_strategies_.reserve(reloc_count); }
269 // Record what to do for the next reloc.
271 set_next_reloc_strategy(Reloc_strategy strategy)
273 this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
274 if (strategy != RELOC_DISCARD)
275 ++this->output_reloc_count_;
278 // Record the Output_data associated with this reloc section.
280 set_output_data(Output_data* posd)
282 gold_assert(this->posd_ == NULL);
286 // Return the Output_data associated with this reloc section.
289 { return this->posd_; }
291 // Return what to do for reloc I.
293 strategy(unsigned int i) const
295 gold_assert(i < this->reloc_strategies_.size());
296 return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
299 // Set the strategy for reloc I.
301 set_strategy(unsigned int i, Reloc_strategy strategy)
303 gold_assert(i < this->reloc_strategies_.size());
304 this->reloc_strategies_[i] = strategy;
307 // Return the number of relocations to create in the output file.
309 output_reloc_count() const
310 { return this->output_reloc_count_; }
313 typedef std::vector<unsigned char> Reloc_strategies;
315 // The strategies for the input reloc. There is one entry in this
316 // vector for each relocation in the input section.
317 Reloc_strategies reloc_strategies_;
318 // The number of relocations to be created in the output file.
319 size_t output_reloc_count_;
320 // The output data structure associated with this relocation.
324 template<int valsize>
327 // Standard relocation routines which are used on many targets. Here
328 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
330 template<int size, bool big_endian>
331 class Relocate_functions
334 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
335 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addendtype;
342 CHECK_SIGNED_OR_UNSIGNED
352 // Check for overflow.
353 template<int valsize>
354 static inline Reloc_status
355 check_overflow(Address value, Overflow_check check)
361 return (Bits<valsize>::has_overflow32(value)
365 return (Bits<valsize>::has_overflow(value)
370 return (Bits<valsize>::has_unsigned_overflow32(value)
374 return (Bits<valsize>::has_unsigned_overflow(value)
377 case CHECK_SIGNED_OR_UNSIGNED:
379 return (Bits<valsize>::has_signed_unsigned_overflow32(value)
383 return (Bits<valsize>::has_signed_unsigned_overflow64(value)
392 // Do a simple relocation with the addend in the section contents.
393 // VALSIZE is the size of the value.
394 template<int valsize>
395 static inline Reloc_status
396 rel(unsigned char* view, Address value, Overflow_check check)
398 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
399 Valtype* wv = reinterpret_cast<Valtype*>(view);
400 Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
402 elfcpp::Swap<valsize, big_endian>::
403 writeval(wv, static_cast<Valtype>(value));
404 return check_overflow<valsize>(value, check);
407 // Like the above but for relocs at unaligned addresses.
408 template<int valsize>
409 static inline Reloc_status
410 rel_unaligned(unsigned char* view, Address value, Overflow_check check)
412 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
414 Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
416 elfcpp::Swap_unaligned<valsize, big_endian>::
417 writeval(view, static_cast<Valtype>(value));
418 return check_overflow<valsize>(value, check);
421 // Do a simple relocation using a Symbol_value with the addend in
422 // the section contents. VALSIZE is the size of the value to
424 template<int valsize>
425 static inline Reloc_status
426 rel(unsigned char* view,
427 const Sized_relobj_file<size, big_endian>* object,
428 const Symbol_value<size>* psymval,
429 Overflow_check check)
431 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
432 Valtype* wv = reinterpret_cast<Valtype*>(view);
433 Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
434 Address value = psymval->value(object, addend);
435 elfcpp::Swap<valsize, big_endian>::
436 writeval(wv, static_cast<Valtype>(value));
437 return check_overflow<valsize>(value, check);
440 // Like the above but for relocs at unaligned addresses.
441 template<int valsize>
442 static inline Reloc_status
443 rel_unaligned(unsigned char* view,
444 const Sized_relobj_file<size, big_endian>* object,
445 const Symbol_value<size>* psymval,
446 Overflow_check check)
448 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
450 Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
451 Address value = psymval->value(object, addend);
452 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
453 return check_overflow<valsize>(value, check);
456 // Do a simple relocation with the addend in the relocation.
457 // VALSIZE is the size of the value.
458 template<int valsize>
459 static inline Reloc_status
460 rela(unsigned char* view, Address value, Addendtype addend,
461 Overflow_check check)
463 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
464 Valtype* wv = reinterpret_cast<Valtype*>(view);
466 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
467 return check_overflow<valsize>(value, check);
470 // Do a simple relocation using a symbol value with the addend in
471 // the relocation. VALSIZE is the size of the value.
472 template<int valsize>
473 static inline Reloc_status
474 rela(unsigned char* view,
475 const Sized_relobj_file<size, big_endian>* object,
476 const Symbol_value<size>* psymval,
478 Overflow_check check)
480 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
481 Valtype* wv = reinterpret_cast<Valtype*>(view);
482 Address value = psymval->value(object, addend);
483 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
484 return check_overflow<valsize>(value, check);
487 // Do a simple PC relative relocation with the addend in the section
488 // contents. VALSIZE is the size of the value.
489 template<int valsize>
490 static inline Reloc_status
491 pcrel(unsigned char* view, Address value, Address address,
492 Overflow_check check)
494 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
495 Valtype* wv = reinterpret_cast<Valtype*>(view);
496 Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
497 value = value + addend - address;
498 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
499 return check_overflow<valsize>(value, check);
502 // Like the above but for relocs at unaligned addresses.
503 template<int valsize>
504 static inline Reloc_status
505 pcrel_unaligned(unsigned char* view, Address value, Address address,
506 Overflow_check check)
508 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
509 Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
510 value = value + addend - address;
511 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
512 return check_overflow<valsize>(value, check);
515 // Do a simple PC relative relocation with a Symbol_value with the
516 // addend in the section contents. VALSIZE is the size of the
518 template<int valsize>
519 static inline Reloc_status
520 pcrel(unsigned char* view,
521 const Sized_relobj_file<size, big_endian>* object,
522 const Symbol_value<size>* psymval,
524 Overflow_check check)
526 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
527 Valtype* wv = reinterpret_cast<Valtype*>(view);
528 Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
529 Address value = psymval->value(object, addend) - address;
530 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
531 return check_overflow<valsize>(value, check);
534 // Do a simple PC relative relocation with the addend in the
535 // relocation. VALSIZE is the size of the value.
536 template<int valsize>
537 static inline Reloc_status
538 pcrela(unsigned char* view, Address value, Addendtype addend, Address address,
539 Overflow_check check)
541 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
542 Valtype* wv = reinterpret_cast<Valtype*>(view);
543 value = value + addend - address;
544 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
545 return check_overflow<valsize>(value, check);
548 // Do a simple PC relative relocation with a Symbol_value with the
549 // addend in the relocation. VALSIZE is the size of the value.
550 template<int valsize>
551 static inline Reloc_status
552 pcrela(unsigned char* view,
553 const Sized_relobj_file<size, big_endian>* object,
554 const Symbol_value<size>* psymval,
557 Overflow_check check)
559 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
560 Valtype* wv = reinterpret_cast<Valtype*>(view);
561 Address value = psymval->value(object, addend) - address;
562 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
563 return check_overflow<valsize>(value, check);
566 typedef Relocate_functions<size, big_endian> This;
569 // Do a simple 8-bit REL relocation with the addend in the section
572 rel8(unsigned char* view, Address value)
573 { This::template rel<8>(view, value, CHECK_NONE); }
575 static inline Reloc_status
576 rel8_check(unsigned char* view, Address value, Overflow_check check)
577 { return This::template rel<8>(view, value, check); }
580 rel8(unsigned char* view,
581 const Sized_relobj_file<size, big_endian>* object,
582 const Symbol_value<size>* psymval)
583 { This::template rel<8>(view, object, psymval, CHECK_NONE); }
585 static inline Reloc_status
586 rel8_check(unsigned char* view,
587 const Sized_relobj_file<size, big_endian>* object,
588 const Symbol_value<size>* psymval,
589 Overflow_check check)
590 { return This::template rel<8>(view, object, psymval, check); }
592 // Do an 8-bit RELA relocation with the addend in the relocation.
594 rela8(unsigned char* view, Address value, Addendtype addend)
595 { This::template rela<8>(view, value, addend, CHECK_NONE); }
597 static inline Reloc_status
598 rela8_check(unsigned char* view, Address value, Addendtype addend,
599 Overflow_check check)
600 { return This::template rela<8>(view, value, addend, check); }
603 rela8(unsigned char* view,
604 const Sized_relobj_file<size, big_endian>* object,
605 const Symbol_value<size>* psymval,
607 { This::template rela<8>(view, object, psymval, addend, CHECK_NONE); }
609 static inline Reloc_status
610 rela8_check(unsigned char* view,
611 const Sized_relobj_file<size, big_endian>* object,
612 const Symbol_value<size>* psymval,
614 Overflow_check check)
615 { return This::template rela<8>(view, object, psymval, addend, check); }
617 // Do a simple 8-bit PC relative relocation with the addend in the
620 pcrel8(unsigned char* view, unsigned char value, Address address)
621 { This::template pcrel<8>(view, value, address, CHECK_NONE); }
623 static inline Reloc_status
624 pcrel8_check(unsigned char* view, unsigned char value, Address address,
625 Overflow_check check)
626 { return This::template pcrel<8>(view, value, address, check); }
629 pcrel8(unsigned char* view,
630 const Sized_relobj_file<size, big_endian>* object,
631 const Symbol_value<size>* psymval,
633 { This::template pcrel<8>(view, object, psymval, address, CHECK_NONE); }
635 static inline Reloc_status
636 pcrel8_check(unsigned char* view,
637 const Sized_relobj_file<size, big_endian>* object,
638 const Symbol_value<size>* psymval,
640 Overflow_check check)
641 { return This::template pcrel<8>(view, object, psymval, address, check); }
643 // Do a simple 8-bit PC relative RELA relocation with the addend in
646 pcrela8(unsigned char* view, Address value, Addendtype addend,
648 { This::template pcrela<8>(view, value, addend, address, CHECK_NONE); }
650 static inline Reloc_status
651 pcrela8_check(unsigned char* view, Address value, Addendtype addend,
652 Address address, Overflow_check check)
653 { return This::template pcrela<8>(view, value, addend, address, check); }
656 pcrela8(unsigned char* view,
657 const Sized_relobj_file<size, big_endian>* object,
658 const Symbol_value<size>* psymval,
661 { This::template pcrela<8>(view, object, psymval, addend, address,
664 static inline Reloc_status
665 pcrela8_check(unsigned char* view,
666 const Sized_relobj_file<size, big_endian>* object,
667 const Symbol_value<size>* psymval,
670 Overflow_check check)
671 { return This::template pcrela<8>(view, object, psymval, addend, address,
674 // Do a simple 16-bit REL relocation with the addend in the section
677 rel16(unsigned char* view, Address value)
678 { This::template rel<16>(view, value, CHECK_NONE); }
680 static inline Reloc_status
681 rel16_check(unsigned char* view, Address value, Overflow_check check)
682 { return This::template rel<16>(view, value, check); }
685 rel16(unsigned char* view,
686 const Sized_relobj_file<size, big_endian>* object,
687 const Symbol_value<size>* psymval)
688 { This::template rel<16>(view, object, psymval, CHECK_NONE); }
690 static inline Reloc_status
691 rel16_check(unsigned char* view,
692 const Sized_relobj_file<size, big_endian>* object,
693 const Symbol_value<size>* psymval,
694 Overflow_check check)
695 { return This::template rel<16>(view, object, psymval, check); }
697 // Do an 16-bit RELA relocation with the addend in the relocation.
699 rela16(unsigned char* view, Address value, Addendtype addend)
700 { This::template rela<16>(view, value, addend, CHECK_NONE); }
702 static inline Reloc_status
703 rela16_check(unsigned char* view, Address value, Addendtype addend,
704 Overflow_check check)
705 { return This::template rela<16>(view, value, addend, check); }
708 rela16(unsigned char* view,
709 const Sized_relobj_file<size, big_endian>* object,
710 const Symbol_value<size>* psymval,
712 { This::template rela<16>(view, object, psymval, addend, CHECK_NONE); }
714 static inline Reloc_status
715 rela16_check(unsigned char* view,
716 const Sized_relobj_file<size, big_endian>* object,
717 const Symbol_value<size>* psymval,
719 Overflow_check check)
720 { return This::template rela<16>(view, object, psymval, addend, check); }
722 // Do a simple 16-bit PC relative REL relocation with the addend in
723 // the section contents.
725 pcrel16(unsigned char* view, Address value, Address address)
726 { This::template pcrel<16>(view, value, address, CHECK_NONE); }
728 static inline Reloc_status
729 pcrel16_check(unsigned char* view, Address value, Address address,
730 Overflow_check check)
731 { return This::template pcrel<16>(view, value, address, check); }
734 pcrel16(unsigned char* view,
735 const Sized_relobj_file<size, big_endian>* object,
736 const Symbol_value<size>* psymval,
738 { This::template pcrel<16>(view, object, psymval, address, CHECK_NONE); }
740 static inline Reloc_status
741 pcrel16_check(unsigned char* view,
742 const Sized_relobj_file<size, big_endian>* object,
743 const Symbol_value<size>* psymval,
745 Overflow_check check)
746 { return This::template pcrel<16>(view, object, psymval, address, check); }
748 // Do a simple 16-bit PC relative RELA relocation with the addend in
751 pcrela16(unsigned char* view, Address value, Addendtype addend,
753 { This::template pcrela<16>(view, value, addend, address, CHECK_NONE); }
755 static inline Reloc_status
756 pcrela16_check(unsigned char* view, Address value, Addendtype addend,
757 Address address, Overflow_check check)
758 { return This::template pcrela<16>(view, value, addend, address, check); }
761 pcrela16(unsigned char* view,
762 const Sized_relobj_file<size, big_endian>* object,
763 const Symbol_value<size>* psymval,
766 { This::template pcrela<16>(view, object, psymval, addend, address,
769 static inline Reloc_status
770 pcrela16_check(unsigned char* view,
771 const Sized_relobj_file<size, big_endian>* object,
772 const Symbol_value<size>* psymval,
775 Overflow_check check)
776 { return This::template pcrela<16>(view, object, psymval, addend, address,
779 // Do a simple 32-bit REL relocation with the addend in the section
782 rel32(unsigned char* view, Address value)
783 { This::template rel<32>(view, value, CHECK_NONE); }
785 static inline Reloc_status
786 rel32_check(unsigned char* view, Address value, Overflow_check check)
787 { return This::template rel<32>(view, value, check); }
789 // Like above but for relocs at unaligned addresses.
791 rel32_unaligned(unsigned char* view, Address value)
792 { This::template rel_unaligned<32>(view, value, CHECK_NONE); }
794 static inline Reloc_status
795 rel32_unaligned_check(unsigned char* view, Address value,
796 Overflow_check check)
797 { return This::template rel_unaligned<32>(view, value, check); }
800 rel32(unsigned char* view,
801 const Sized_relobj_file<size, big_endian>* object,
802 const Symbol_value<size>* psymval)
803 { This::template rel<32>(view, object, psymval, CHECK_NONE); }
805 static inline Reloc_status
806 rel32_check(unsigned char* view,
807 const Sized_relobj_file<size, big_endian>* object,
808 const Symbol_value<size>* psymval,
809 Overflow_check check)
810 { return This::template rel<32>(view, object, psymval, check); }
812 // Like above but for relocs at unaligned addresses.
814 rel32_unaligned(unsigned char* view,
815 const Sized_relobj_file<size, big_endian>* object,
816 const Symbol_value<size>* psymval)
817 { This::template rel_unaligned<32>(view, object, psymval, CHECK_NONE); }
819 static inline Reloc_status
820 rel32_unaligned_check(unsigned char* view,
821 const Sized_relobj_file<size, big_endian>* object,
822 const Symbol_value<size>* psymval,
823 Overflow_check check)
824 { return This::template rel_unaligned<32>(view, object, psymval, check); }
826 // Do a 32-bit RELA relocation with the addend in the relocation.
828 rela32(unsigned char* view, Address value, Addendtype addend)
829 { This::template rela<32>(view, value, addend, CHECK_NONE); }
831 static inline Reloc_status
832 rela32(unsigned char* view, Address value, Addendtype addend,
833 Overflow_check check)
834 { return This::template rela<32>(view, value, addend, check); }
837 rela32(unsigned char* view,
838 const Sized_relobj_file<size, big_endian>* object,
839 const Symbol_value<size>* psymval,
841 { This::template rela<32>(view, object, psymval, addend, CHECK_NONE); }
843 static inline Reloc_status
844 rela32_check(unsigned char* view,
845 const Sized_relobj_file<size, big_endian>* object,
846 const Symbol_value<size>* psymval,
848 Overflow_check check)
849 { return This::template rela<32>(view, object, psymval, addend, check); }
851 // Do a simple 32-bit PC relative REL relocation with the addend in
852 // the section contents.
854 pcrel32(unsigned char* view, Address value, Address address)
855 { This::template pcrel<32>(view, value, address, CHECK_NONE); }
857 static inline Reloc_status
858 pcrel32_check(unsigned char* view, Address value, Address address,
859 Overflow_check check)
860 { return This::template pcrel<32>(view, value, address, check); }
862 // Unaligned version of the above.
864 pcrel32_unaligned(unsigned char* view, Address value, Address address)
865 { This::template pcrel_unaligned<32>(view, value, address, CHECK_NONE); }
867 static inline Reloc_status
868 pcrel32_unaligned_check(unsigned char* view, Address value, Address address,
869 Overflow_check check)
870 { return This::template pcrel_unaligned<32>(view, value, address, check); }
873 pcrel32(unsigned char* view,
874 const Sized_relobj_file<size, big_endian>* object,
875 const Symbol_value<size>* psymval,
877 { This::template pcrel<32>(view, object, psymval, address, CHECK_NONE); }
879 static inline Reloc_status
880 pcrel32_check(unsigned char* view,
881 const Sized_relobj_file<size, big_endian>* object,
882 const Symbol_value<size>* psymval,
884 Overflow_check check)
885 { return This::template pcrel<32>(view, object, psymval, address, check); }
887 // Do a simple 32-bit PC relative RELA relocation with the addend in
890 pcrela32(unsigned char* view, Address value, Addendtype addend,
892 { This::template pcrela<32>(view, value, addend, address, CHECK_NONE); }
894 static inline Reloc_status
895 pcrela32_check(unsigned char* view, Address value, Addendtype addend,
896 Address address, Overflow_check check)
897 { return This::template pcrela<32>(view, value, addend, address, check); }
900 pcrela32(unsigned char* view,
901 const Sized_relobj_file<size, big_endian>* object,
902 const Symbol_value<size>* psymval,
905 { This::template pcrela<32>(view, object, psymval, addend, address,
908 static inline Reloc_status
909 pcrela32_check(unsigned char* view,
910 const Sized_relobj_file<size, big_endian>* object,
911 const Symbol_value<size>* psymval,
914 Overflow_check check)
915 { return This::template pcrela<32>(view, object, psymval, addend, address,
918 // Do a simple 64-bit REL relocation with the addend in the section
921 rel64(unsigned char* view, Address value)
922 { This::template rel<64>(view, value, CHECK_NONE); }
925 rel64(unsigned char* view,
926 const Sized_relobj_file<size, big_endian>* object,
927 const Symbol_value<size>* psymval)
928 { This::template rel<64>(view, object, psymval, CHECK_NONE); }
930 // Do a 64-bit RELA relocation with the addend in the relocation.
932 rela64(unsigned char* view, Address value, Addendtype addend)
933 { This::template rela<64>(view, value, addend, CHECK_NONE); }
936 rela64(unsigned char* view,
937 const Sized_relobj_file<size, big_endian>* object,
938 const Symbol_value<size>* psymval,
940 { This::template rela<64>(view, object, psymval, addend, CHECK_NONE); }
942 // Do a simple 64-bit PC relative REL relocation with the addend in
943 // the section contents.
945 pcrel64(unsigned char* view, Address value, Address address)
946 { This::template pcrel<64>(view, value, address, CHECK_NONE); }
949 pcrel64(unsigned char* view,
950 const Sized_relobj_file<size, big_endian>* object,
951 const Symbol_value<size>* psymval,
953 { This::template pcrel<64>(view, object, psymval, address, CHECK_NONE); }
955 // Do a simple 64-bit PC relative RELA relocation with the addend in
958 pcrela64(unsigned char* view, Address value, Addendtype addend,
960 { This::template pcrela<64>(view, value, addend, address, CHECK_NONE); }
963 pcrela64(unsigned char* view,
964 const Sized_relobj_file<size, big_endian>* object,
965 const Symbol_value<size>* psymval,
968 { This::template pcrela<64>(view, object, psymval, addend, address,
972 // Integer manipulation functions used by various targets when
973 // performing relocations.
979 // Sign extend an n-bit unsigned integer stored in a uint32_t into
980 // an int32_t. BITS must be between 1 and 32.
981 static inline int32_t
982 sign_extend32(uint32_t val)
984 gold_assert(bits > 0 && bits <= 32);
986 return static_cast<int32_t>(val);
987 uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - bits);
989 uint32_t top_bit = 1U << (bits - 1);
990 int32_t as_signed = static_cast<int32_t>(val);
991 if ((val & top_bit) != 0)
992 as_signed -= static_cast<int32_t>(top_bit * 2);
996 // Return true if VAL (stored in a uint32_t) has overflowed a signed
997 // value with BITS bits.
999 has_overflow32(uint32_t val)
1001 gold_assert(bits > 0 && bits <= 32);
1004 int32_t max = (1 << (bits - 1)) - 1;
1005 int32_t min = -(1 << (bits - 1));
1006 int32_t as_signed = static_cast<int32_t>(val);
1007 return as_signed > max || as_signed < min;
1010 // Return true if VAL (stored in a uint32_t) has overflowed an unsigned
1011 // value with BITS bits.
1013 has_unsigned_overflow32(uint32_t val)
1015 gold_assert(bits > 0 && bits <= 32);
1018 int32_t max = static_cast<int32_t>((1U << bits) - 1);
1022 // Return true if VAL (stored in a uint32_t) has overflowed both a
1023 // signed and an unsigned value. E.g.,
1024 // Bits<8>::has_signed_unsigned_overflow32 would check -128 <= VAL <
1027 has_signed_unsigned_overflow32(uint32_t val)
1029 gold_assert(bits > 0 && bits <= 32);
1032 int32_t max = static_cast<int32_t>((1U << bits) - 1);
1033 int32_t min = -(1 << (bits - 1));
1034 int32_t as_signed = static_cast<int32_t>(val);
1035 return as_signed > max || as_signed < min;
1038 // Select bits from A and B using bits in MASK. For each n in
1039 // [0..31], the n-th bit in the result is chosen from the n-th bits
1040 // of A and B. A zero selects A and a one selects B.
1041 static inline uint32_t
1042 bit_select32(uint32_t a, uint32_t b, uint32_t mask)
1043 { return (a & ~mask) | (b & mask); }
1045 // Sign extend an n-bit unsigned integer stored in a uint64_t into
1046 // an int64_t. BITS must be between 1 and 64.
1047 static inline int64_t
1048 sign_extend(uint64_t val)
1050 gold_assert(bits > 0 && bits <= 64);
1052 return static_cast<int64_t>(val);
1053 uint64_t mask = (~static_cast<uint64_t>(0)) >> (64 - bits);
1055 uint64_t top_bit = static_cast<uint64_t>(1) << (bits - 1);
1056 int64_t as_signed = static_cast<int64_t>(val);
1057 if ((val & top_bit) != 0)
1058 as_signed -= static_cast<int64_t>(top_bit * 2);
1062 // Return true if VAL (stored in a uint64_t) has overflowed a signed
1063 // value with BITS bits.
1065 has_overflow(uint64_t val)
1067 gold_assert(bits > 0 && bits <= 64);
1070 int64_t max = (static_cast<int64_t>(1) << (bits - 1)) - 1;
1071 int64_t min = -(static_cast<int64_t>(1) << (bits - 1));
1072 int64_t as_signed = static_cast<int64_t>(val);
1073 return as_signed > max || as_signed < min;
1076 // Return true if VAL (stored in a uint64_t) has overflowed an unsigned
1077 // value with BITS bits.
1079 has_unsigned_overflow(uint64_t val)
1081 gold_assert(bits > 0 && bits <= 64);
1084 int64_t max = static_cast<int64_t>((static_cast<uint64_t>(1) << bits) - 1);
1088 // Return true if VAL (stored in a uint64_t) has overflowed both a
1089 // signed and an unsigned value. E.g.,
1090 // Bits<8>::has_signed_unsigned_overflow would check -128 <= VAL <
1093 has_signed_unsigned_overflow64(uint64_t val)
1095 gold_assert(bits > 0 && bits <= 64);
1098 int64_t max = static_cast<int64_t>((static_cast<uint64_t>(1) << bits) - 1);
1099 int64_t min = -(static_cast<int64_t>(1) << (bits - 1));
1100 int64_t as_signed = static_cast<int64_t>(val);
1101 return as_signed > max || as_signed < min;
1104 // Select bits from A and B using bits in MASK. For each n in
1105 // [0..31], the n-th bit in the result is chosen from the n-th bits
1106 // of A and B. A zero selects A and a one selects B.
1107 static inline uint64_t
1108 bit_select64(uint64_t a, uint64_t b, uint64_t mask)
1109 { return (a & ~mask) | (b & mask); }
1112 // Track relocations while reading a section. This lets you ask for
1113 // the relocation at a certain offset, and see how relocs occur
1114 // between points of interest.
1116 template<int size, bool big_endian>
1121 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
1124 // Initialize the Track_relocs object. OBJECT is the object holding
1125 // the reloc section, RELOC_SHNDX is the section index of the reloc
1126 // section, and RELOC_TYPE is the type of the reloc section
1127 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
1128 // something went wrong.
1130 initialize(Object* object, unsigned int reloc_shndx,
1131 unsigned int reloc_type);
1133 // Return the offset in the data section to which the next reloc
1134 // applies. This returns -1 if there is no next reloc.
1136 next_offset() const;
1138 // Return the symbol index of the next reloc. This returns -1U if
1139 // there is no next reloc.
1141 next_symndx() const;
1143 // Return the addend of the next reloc. This returns 0 if there is
1146 next_addend() const;
1148 // Advance to OFFSET within the data section, and return the number
1149 // of relocs which would be skipped.
1151 advance(off_t offset);
1153 // Checkpoint the current position in the reloc section.
1156 { return this->pos_; }
1158 // Reset the position to CHECKPOINT.
1160 reset(section_size_type checkpoint)
1161 { this->pos_ = checkpoint; }
1164 // The contents of the input object's reloc section.
1165 const unsigned char* prelocs_;
1166 // The length of the reloc section.
1167 section_size_type len_;
1168 // Our current position in the reloc section.
1169 section_size_type pos_;
1170 // The size of the relocs in the section.
1174 } // End namespace gold.
1176 #endif // !defined(GOLD_RELOC_H)