[gdb/testsuite] Fix untested in gdb.base/frame-view.exp
[binutils-gdb.git] / gold / reloc.h
blob46cd7c1666e2b095a784df1a4c65bf0f3780e18b
1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 // Copyright (C) 2006-2023 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 struct 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_file;
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 // 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
67 // order.
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)
72 { }
74 // The standard Task methods.
76 Task_token*
77 is_runnable();
79 void
80 locks(Task_locker*);
82 void
83 run(Workqueue*);
85 std::string
86 get_name() const;
88 private:
89 Symbol_table* symtab_;
90 Layout* layout_;
91 Relobj* object_;
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
101 public:
102 // THIS_BLOCKER prevents this task from running until the previous
103 // one is finished. NEXT_BLOCKER prevents the next task from
104 // running.
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.
116 Task_token*
117 is_runnable();
119 void
120 locks(Task_locker*);
122 void
123 run(Workqueue*);
125 std::string
126 get_name() const;
128 private:
129 Symbol_table* symtab_;
130 Layout* layout_;
131 Relobj* object_;
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
142 public:
143 // THIS_BLOCKER prevents this task from running until the previous
144 // one is finished. NEXT_BLOCKER prevents the next task from
145 // running.
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)
153 ~Scan_relocs();
155 // The standard Task methods.
157 Task_token*
158 is_runnable();
160 void
161 locks(Task_locker*);
163 void
164 run(Workqueue*);
166 std::string
167 get_name() const;
169 private:
170 Symbol_table* symtab_;
171 Layout* layout_;
172 Relobj* object_;
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
182 public:
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.
195 Task_token*
196 is_runnable();
198 void
199 locks(Task_locker*);
201 void
202 run(Workqueue*);
204 std::string
205 get_name() const;
207 private:
208 const Symbol_table* symtab_;
209 const Layout* layout_;
210 Relobj* object_;
211 Output_file* of_;
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
224 public:
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.
228 enum Reloc_strategy
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.
232 RELOC_COPY,
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
238 // to be changed.
239 RELOC_ADJUST_FOR_SECTION_RELA,
240 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
241 // adjusted.
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.
254 RELOC_DISCARD,
255 // An input reloc which is not discarded, but which requires
256 // target specific processing in order to update it.
257 RELOC_SPECIAL
260 Relocatable_relocs()
261 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
264 // Record the number of relocs.
265 void
266 set_reloc_count(size_t reloc_count)
267 { this->reloc_strategies_.reserve(reloc_count); }
269 // Record what to do for the next reloc.
270 void
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.
279 void
280 set_output_data(Output_data* posd)
282 gold_assert(this->posd_ == NULL);
283 this->posd_ = posd;
286 // Return the Output_data associated with this reloc section.
287 Output_data*
288 output_data() const
289 { return this->posd_; }
291 // Return what to do for reloc I.
292 Reloc_strategy
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.
300 void
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.
308 size_t
309 output_reloc_count() const
310 { return this->output_reloc_count_; }
312 private:
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.
321 Output_data* posd_;
324 template<int valsize>
325 class Bits;
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
333 public:
334 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
335 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addendtype;
337 enum Overflow_check
339 // No overflow checking.
340 CHECK_NONE,
341 // Check for overflow of a signed value.
342 CHECK_SIGNED,
343 // Check for overflow of an unsigned value.
344 CHECK_UNSIGNED,
345 // Check for overflow of a signed or unsigned value.
346 // (i.e., no error if either signed or unsigned fits.)
347 CHECK_SIGNED_OR_UNSIGNED
350 enum Reloc_status
352 RELOC_OK,
353 RELOC_OVERFLOW
356 private:
357 // Check for overflow.
358 template<int valsize>
359 static inline Reloc_status
360 check_overflow(Address value, Overflow_check check)
362 switch (check)
364 case CHECK_SIGNED:
365 if (size == 32)
366 return (Bits<valsize>::has_overflow32(value)
367 ? RELOC_OVERFLOW
368 : RELOC_OK);
369 else
370 return (Bits<valsize>::has_overflow(value)
371 ? RELOC_OVERFLOW
372 : RELOC_OK);
373 case CHECK_UNSIGNED:
374 if (size == 32)
375 return (Bits<valsize>::has_unsigned_overflow32(value)
376 ? RELOC_OVERFLOW
377 : RELOC_OK);
378 else
379 return (Bits<valsize>::has_unsigned_overflow(value)
380 ? RELOC_OVERFLOW
381 : RELOC_OK);
382 case CHECK_SIGNED_OR_UNSIGNED:
383 if (size == 32)
384 return (Bits<valsize>::has_signed_unsigned_overflow32(value)
385 ? RELOC_OVERFLOW
386 : RELOC_OK);
387 else
388 return (Bits<valsize>::has_signed_unsigned_overflow64(value)
389 ? RELOC_OVERFLOW
390 : RELOC_OK);
391 case CHECK_NONE:
392 default:
393 return RELOC_OK;
397 // Do a simple relocation with the addend in the section contents.
398 // VALSIZE is the size of the value.
399 template<int valsize>
400 static inline Reloc_status
401 rel(unsigned char* view, Address value, Overflow_check check)
403 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
404 Valtype* wv = reinterpret_cast<Valtype*>(view);
405 Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
406 value += addend;
407 elfcpp::Swap<valsize, big_endian>::
408 writeval(wv, static_cast<Valtype>(value));
409 return check_overflow<valsize>(value, check);
412 // Like the above but for relocs at unaligned addresses.
413 template<int valsize>
414 static inline Reloc_status
415 rel_unaligned(unsigned char* view, Address value, Overflow_check check)
417 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
418 Valtype;
419 Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
420 value += addend;
421 elfcpp::Swap_unaligned<valsize, big_endian>::
422 writeval(view, static_cast<Valtype>(value));
423 return check_overflow<valsize>(value, check);
426 // Do a simple relocation using a Symbol_value with the addend in
427 // the section contents. VALSIZE is the size of the value to
428 // relocate.
429 template<int valsize>
430 static inline Reloc_status
431 rel(unsigned char* view,
432 const Sized_relobj_file<size, big_endian>* object,
433 const Symbol_value<size>* psymval,
434 Overflow_check check)
436 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
437 Valtype* wv = reinterpret_cast<Valtype*>(view);
438 Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
439 Address value = psymval->value(object, addend);
440 elfcpp::Swap<valsize, big_endian>::
441 writeval(wv, static_cast<Valtype>(value));
442 return check_overflow<valsize>(value, check);
445 // Like the above but for relocs at unaligned addresses.
446 template<int valsize>
447 static inline Reloc_status
448 rel_unaligned(unsigned char* view,
449 const Sized_relobj_file<size, big_endian>* object,
450 const Symbol_value<size>* psymval,
451 Overflow_check check)
453 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
454 Valtype;
455 Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
456 Address value = psymval->value(object, addend);
457 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
458 return check_overflow<valsize>(value, check);
461 // Do a simple relocation with the addend in the relocation.
462 // VALSIZE is the size of the value.
463 template<int valsize>
464 static inline Reloc_status
465 rela(unsigned char* view, Address value, Addendtype addend,
466 Overflow_check check)
468 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
469 Valtype* wv = reinterpret_cast<Valtype*>(view);
470 value += addend;
471 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
472 return check_overflow<valsize>(value, check);
475 // Do a simple relocation using a symbol value with the addend in
476 // the relocation. VALSIZE is the size of the value.
477 template<int valsize>
478 static inline Reloc_status
479 rela(unsigned char* view,
480 const Sized_relobj_file<size, big_endian>* object,
481 const Symbol_value<size>* psymval,
482 Addendtype addend,
483 Overflow_check check)
485 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
486 Valtype* wv = reinterpret_cast<Valtype*>(view);
487 Address value = psymval->value(object, addend);
488 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
489 return check_overflow<valsize>(value, check);
492 // Do a simple PC relative relocation with the addend in the section
493 // contents. VALSIZE is the size of the value.
494 template<int valsize>
495 static inline Reloc_status
496 pcrel(unsigned char* view, Address value, Address address,
497 Overflow_check check)
499 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
500 Valtype* wv = reinterpret_cast<Valtype*>(view);
501 Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
502 value = value + addend - address;
503 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
504 return check_overflow<valsize>(value, check);
507 // Like the above but for relocs at unaligned addresses.
508 template<int valsize>
509 static inline Reloc_status
510 pcrel_unaligned(unsigned char* view, Address value, Address address,
511 Overflow_check check)
513 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
514 Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
515 value = value + addend - address;
516 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
517 return check_overflow<valsize>(value, check);
520 // Do a simple PC relative relocation with a Symbol_value with the
521 // addend in the section contents. VALSIZE is the size of the
522 // value.
523 template<int valsize>
524 static inline Reloc_status
525 pcrel(unsigned char* view,
526 const Sized_relobj_file<size, big_endian>* object,
527 const Symbol_value<size>* psymval,
528 Address address,
529 Overflow_check check)
531 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
532 Valtype* wv = reinterpret_cast<Valtype*>(view);
533 Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
534 Address value = psymval->value(object, addend) - address;
535 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
536 return check_overflow<valsize>(value, check);
539 // Do a simple PC relative relocation with the addend in the
540 // relocation. VALSIZE is the size of the value.
541 template<int valsize>
542 static inline Reloc_status
543 pcrela(unsigned char* view, Address value, Addendtype addend, Address address,
544 Overflow_check check)
546 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
547 Valtype* wv = reinterpret_cast<Valtype*>(view);
548 value = value + addend - address;
549 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
550 return check_overflow<valsize>(value, check);
553 // Do a simple PC relative relocation with a Symbol_value with the
554 // addend in the relocation. VALSIZE is the size of the value.
555 template<int valsize>
556 static inline Reloc_status
557 pcrela(unsigned char* view,
558 const Sized_relobj_file<size, big_endian>* object,
559 const Symbol_value<size>* psymval,
560 Addendtype addend,
561 Address address,
562 Overflow_check check)
564 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
565 Valtype* wv = reinterpret_cast<Valtype*>(view);
566 Address value = psymval->value(object, addend) - address;
567 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
568 return check_overflow<valsize>(value, check);
571 typedef Relocate_functions<size, big_endian> This;
573 public:
574 // Do a simple 8-bit REL relocation with the addend in the section
575 // contents.
576 static inline void
577 rel8(unsigned char* view, Address value)
578 { This::template rel<8>(view, value, CHECK_NONE); }
580 static inline Reloc_status
581 rel8_check(unsigned char* view, Address value, Overflow_check check)
582 { return This::template rel<8>(view, value, check); }
584 static inline void
585 rel8(unsigned char* view,
586 const Sized_relobj_file<size, big_endian>* object,
587 const Symbol_value<size>* psymval)
588 { This::template rel<8>(view, object, psymval, CHECK_NONE); }
590 static inline Reloc_status
591 rel8_check(unsigned char* view,
592 const Sized_relobj_file<size, big_endian>* object,
593 const Symbol_value<size>* psymval,
594 Overflow_check check)
595 { return This::template rel<8>(view, object, psymval, check); }
597 // Do an 8-bit RELA relocation with the addend in the relocation.
598 static inline void
599 rela8(unsigned char* view, Address value, Addendtype addend)
600 { This::template rela<8>(view, value, addend, CHECK_NONE); }
602 static inline Reloc_status
603 rela8_check(unsigned char* view, Address value, Addendtype addend,
604 Overflow_check check)
605 { return This::template rela<8>(view, value, addend, check); }
607 static inline void
608 rela8(unsigned char* view,
609 const Sized_relobj_file<size, big_endian>* object,
610 const Symbol_value<size>* psymval,
611 Addendtype addend)
612 { This::template rela<8>(view, object, psymval, addend, CHECK_NONE); }
614 static inline Reloc_status
615 rela8_check(unsigned char* view,
616 const Sized_relobj_file<size, big_endian>* object,
617 const Symbol_value<size>* psymval,
618 Addendtype addend,
619 Overflow_check check)
620 { return This::template rela<8>(view, object, psymval, addend, check); }
622 // Do a simple 8-bit PC relative relocation with the addend in the
623 // section contents.
624 static inline void
625 pcrel8(unsigned char* view, unsigned char value, Address address)
626 { This::template pcrel<8>(view, value, address, CHECK_NONE); }
628 static inline Reloc_status
629 pcrel8_check(unsigned char* view, unsigned char value, Address address,
630 Overflow_check check)
631 { return This::template pcrel<8>(view, value, address, check); }
633 static inline void
634 pcrel8(unsigned char* view,
635 const Sized_relobj_file<size, big_endian>* object,
636 const Symbol_value<size>* psymval,
637 Address address)
638 { This::template pcrel<8>(view, object, psymval, address, CHECK_NONE); }
640 static inline Reloc_status
641 pcrel8_check(unsigned char* view,
642 const Sized_relobj_file<size, big_endian>* object,
643 const Symbol_value<size>* psymval,
644 Address address,
645 Overflow_check check)
646 { return This::template pcrel<8>(view, object, psymval, address, check); }
648 // Do a simple 8-bit PC relative RELA relocation with the addend in
649 // the reloc.
650 static inline void
651 pcrela8(unsigned char* view, Address value, Addendtype addend,
652 Address address)
653 { This::template pcrela<8>(view, value, addend, address, CHECK_NONE); }
655 static inline Reloc_status
656 pcrela8_check(unsigned char* view, Address value, Addendtype addend,
657 Address address, Overflow_check check)
658 { return This::template pcrela<8>(view, value, addend, address, check); }
660 static inline void
661 pcrela8(unsigned char* view,
662 const Sized_relobj_file<size, big_endian>* object,
663 const Symbol_value<size>* psymval,
664 Addendtype addend,
665 Address address)
666 { This::template pcrela<8>(view, object, psymval, addend, address,
667 CHECK_NONE); }
669 static inline Reloc_status
670 pcrela8_check(unsigned char* view,
671 const Sized_relobj_file<size, big_endian>* object,
672 const Symbol_value<size>* psymval,
673 Addendtype addend,
674 Address address,
675 Overflow_check check)
676 { return This::template pcrela<8>(view, object, psymval, addend, address,
677 check); }
679 // Do a simple 16-bit REL relocation with the addend in the section
680 // contents.
681 static inline void
682 rel16(unsigned char* view, Address value)
683 { This::template rel<16>(view, value, CHECK_NONE); }
685 static inline Reloc_status
686 rel16_check(unsigned char* view, Address value, Overflow_check check)
687 { return This::template rel<16>(view, value, check); }
689 static inline void
690 rel16(unsigned char* view,
691 const Sized_relobj_file<size, big_endian>* object,
692 const Symbol_value<size>* psymval)
693 { This::template rel<16>(view, object, psymval, CHECK_NONE); }
695 static inline Reloc_status
696 rel16_check(unsigned char* view,
697 const Sized_relobj_file<size, big_endian>* object,
698 const Symbol_value<size>* psymval,
699 Overflow_check check)
700 { return This::template rel<16>(view, object, psymval, check); }
702 // Do an 16-bit RELA relocation with the addend in the relocation.
703 static inline void
704 rela16(unsigned char* view, Address value, Addendtype addend)
705 { This::template rela<16>(view, value, addend, CHECK_NONE); }
707 static inline Reloc_status
708 rela16_check(unsigned char* view, Address value, Addendtype addend,
709 Overflow_check check)
710 { return This::template rela<16>(view, value, addend, check); }
712 static inline void
713 rela16(unsigned char* view,
714 const Sized_relobj_file<size, big_endian>* object,
715 const Symbol_value<size>* psymval,
716 Addendtype addend)
717 { This::template rela<16>(view, object, psymval, addend, CHECK_NONE); }
719 static inline Reloc_status
720 rela16_check(unsigned char* view,
721 const Sized_relobj_file<size, big_endian>* object,
722 const Symbol_value<size>* psymval,
723 Addendtype addend,
724 Overflow_check check)
725 { return This::template rela<16>(view, object, psymval, addend, check); }
727 // Do a simple 16-bit PC relative REL relocation with the addend in
728 // the section contents.
729 static inline void
730 pcrel16(unsigned char* view, Address value, Address address)
731 { This::template pcrel<16>(view, value, address, CHECK_NONE); }
733 static inline Reloc_status
734 pcrel16_check(unsigned char* view, Address value, Address address,
735 Overflow_check check)
736 { return This::template pcrel<16>(view, value, address, check); }
738 static inline void
739 pcrel16(unsigned char* view,
740 const Sized_relobj_file<size, big_endian>* object,
741 const Symbol_value<size>* psymval,
742 Address address)
743 { This::template pcrel<16>(view, object, psymval, address, CHECK_NONE); }
745 static inline Reloc_status
746 pcrel16_check(unsigned char* view,
747 const Sized_relobj_file<size, big_endian>* object,
748 const Symbol_value<size>* psymval,
749 Address address,
750 Overflow_check check)
751 { return This::template pcrel<16>(view, object, psymval, address, check); }
753 // Do a simple 16-bit PC relative RELA relocation with the addend in
754 // the reloc.
755 static inline void
756 pcrela16(unsigned char* view, Address value, Addendtype addend,
757 Address address)
758 { This::template pcrela<16>(view, value, addend, address, CHECK_NONE); }
760 static inline Reloc_status
761 pcrela16_check(unsigned char* view, Address value, Addendtype addend,
762 Address address, Overflow_check check)
763 { return This::template pcrela<16>(view, value, addend, address, check); }
765 static inline void
766 pcrela16(unsigned char* view,
767 const Sized_relobj_file<size, big_endian>* object,
768 const Symbol_value<size>* psymval,
769 Addendtype addend,
770 Address address)
771 { This::template pcrela<16>(view, object, psymval, addend, address,
772 CHECK_NONE); }
774 static inline Reloc_status
775 pcrela16_check(unsigned char* view,
776 const Sized_relobj_file<size, big_endian>* object,
777 const Symbol_value<size>* psymval,
778 Addendtype addend,
779 Address address,
780 Overflow_check check)
781 { return This::template pcrela<16>(view, object, psymval, addend, address,
782 check); }
784 // Do a simple 32-bit REL relocation with the addend in the section
785 // contents.
786 static inline void
787 rel32(unsigned char* view, Address value)
788 { This::template rel<32>(view, value, CHECK_NONE); }
790 static inline Reloc_status
791 rel32_check(unsigned char* view, Address value, Overflow_check check)
792 { return This::template rel<32>(view, value, check); }
794 // Like above but for relocs at unaligned addresses.
795 static inline void
796 rel32_unaligned(unsigned char* view, Address value)
797 { This::template rel_unaligned<32>(view, value, CHECK_NONE); }
799 static inline Reloc_status
800 rel32_unaligned_check(unsigned char* view, Address value,
801 Overflow_check check)
802 { return This::template rel_unaligned<32>(view, value, check); }
804 static inline void
805 rel32(unsigned char* view,
806 const Sized_relobj_file<size, big_endian>* object,
807 const Symbol_value<size>* psymval)
808 { This::template rel<32>(view, object, psymval, CHECK_NONE); }
810 static inline Reloc_status
811 rel32_check(unsigned char* view,
812 const Sized_relobj_file<size, big_endian>* object,
813 const Symbol_value<size>* psymval,
814 Overflow_check check)
815 { return This::template rel<32>(view, object, psymval, check); }
817 // Like above but for relocs at unaligned addresses.
818 static inline void
819 rel32_unaligned(unsigned char* view,
820 const Sized_relobj_file<size, big_endian>* object,
821 const Symbol_value<size>* psymval)
822 { This::template rel_unaligned<32>(view, object, psymval, CHECK_NONE); }
824 static inline Reloc_status
825 rel32_unaligned_check(unsigned char* view,
826 const Sized_relobj_file<size, big_endian>* object,
827 const Symbol_value<size>* psymval,
828 Overflow_check check)
829 { return This::template rel_unaligned<32>(view, object, psymval, check); }
831 // Do a 32-bit RELA relocation with the addend in the relocation.
832 static inline void
833 rela32(unsigned char* view, Address value, Addendtype addend)
834 { This::template rela<32>(view, value, addend, CHECK_NONE); }
836 static inline Reloc_status
837 rela32(unsigned char* view, Address value, Addendtype addend,
838 Overflow_check check)
839 { return This::template rela<32>(view, value, addend, check); }
841 static inline void
842 rela32(unsigned char* view,
843 const Sized_relobj_file<size, big_endian>* object,
844 const Symbol_value<size>* psymval,
845 Addendtype addend)
846 { This::template rela<32>(view, object, psymval, addend, CHECK_NONE); }
848 static inline Reloc_status
849 rela32_check(unsigned char* view,
850 const Sized_relobj_file<size, big_endian>* object,
851 const Symbol_value<size>* psymval,
852 Addendtype addend,
853 Overflow_check check)
854 { return This::template rela<32>(view, object, psymval, addend, check); }
856 // Do a simple 32-bit PC relative REL relocation with the addend in
857 // the section contents.
858 static inline void
859 pcrel32(unsigned char* view, Address value, Address address)
860 { This::template pcrel<32>(view, value, address, CHECK_NONE); }
862 static inline Reloc_status
863 pcrel32_check(unsigned char* view, Address value, Address address,
864 Overflow_check check)
865 { return This::template pcrel<32>(view, value, address, check); }
867 // Unaligned version of the above.
868 static inline void
869 pcrel32_unaligned(unsigned char* view, Address value, Address address)
870 { This::template pcrel_unaligned<32>(view, value, address, CHECK_NONE); }
872 static inline Reloc_status
873 pcrel32_unaligned_check(unsigned char* view, Address value, Address address,
874 Overflow_check check)
875 { return This::template pcrel_unaligned<32>(view, value, address, check); }
877 static inline void
878 pcrel32(unsigned char* view,
879 const Sized_relobj_file<size, big_endian>* object,
880 const Symbol_value<size>* psymval,
881 Address address)
882 { This::template pcrel<32>(view, object, psymval, address, CHECK_NONE); }
884 static inline Reloc_status
885 pcrel32_check(unsigned char* view,
886 const Sized_relobj_file<size, big_endian>* object,
887 const Symbol_value<size>* psymval,
888 Address address,
889 Overflow_check check)
890 { return This::template pcrel<32>(view, object, psymval, address, check); }
892 // Do a simple 32-bit PC relative RELA relocation with the addend in
893 // the relocation.
894 static inline void
895 pcrela32(unsigned char* view, Address value, Addendtype addend,
896 Address address)
897 { This::template pcrela<32>(view, value, addend, address, CHECK_NONE); }
899 static inline Reloc_status
900 pcrela32_check(unsigned char* view, Address value, Addendtype addend,
901 Address address, Overflow_check check)
902 { return This::template pcrela<32>(view, value, addend, address, check); }
904 static inline void
905 pcrela32(unsigned char* view,
906 const Sized_relobj_file<size, big_endian>* object,
907 const Symbol_value<size>* psymval,
908 Addendtype addend,
909 Address address)
910 { This::template pcrela<32>(view, object, psymval, addend, address,
911 CHECK_NONE); }
913 static inline Reloc_status
914 pcrela32_check(unsigned char* view,
915 const Sized_relobj_file<size, big_endian>* object,
916 const Symbol_value<size>* psymval,
917 Addendtype addend,
918 Address address,
919 Overflow_check check)
920 { return This::template pcrela<32>(view, object, psymval, addend, address,
921 check); }
923 // Do a simple 64-bit REL relocation with the addend in the section
924 // contents.
925 static inline void
926 rel64(unsigned char* view, Address value)
927 { This::template rel<64>(view, value, CHECK_NONE); }
929 static inline void
930 rel64(unsigned char* view,
931 const Sized_relobj_file<size, big_endian>* object,
932 const Symbol_value<size>* psymval)
933 { This::template rel<64>(view, object, psymval, CHECK_NONE); }
935 // Do a 64-bit RELA relocation with the addend in the relocation.
936 static inline void
937 rela64(unsigned char* view, Address value, Addendtype addend)
938 { This::template rela<64>(view, value, addend, CHECK_NONE); }
940 static inline void
941 rela64(unsigned char* view,
942 const Sized_relobj_file<size, big_endian>* object,
943 const Symbol_value<size>* psymval,
944 Addendtype addend)
945 { This::template rela<64>(view, object, psymval, addend, CHECK_NONE); }
947 // Do a simple 64-bit PC relative REL relocation with the addend in
948 // the section contents.
949 static inline void
950 pcrel64(unsigned char* view, Address value, Address address)
951 { This::template pcrel<64>(view, value, address, CHECK_NONE); }
953 static inline void
954 pcrel64(unsigned char* view,
955 const Sized_relobj_file<size, big_endian>* object,
956 const Symbol_value<size>* psymval,
957 Address address)
958 { This::template pcrel<64>(view, object, psymval, address, CHECK_NONE); }
960 // Do a simple 64-bit PC relative RELA relocation with the addend in
961 // the relocation.
962 static inline void
963 pcrela64(unsigned char* view, Address value, Addendtype addend,
964 Address address)
965 { This::template pcrela<64>(view, value, addend, address, CHECK_NONE); }
967 static inline void
968 pcrela64(unsigned char* view,
969 const Sized_relobj_file<size, big_endian>* object,
970 const Symbol_value<size>* psymval,
971 Addendtype addend,
972 Address address)
973 { This::template pcrela<64>(view, object, psymval, addend, address,
974 CHECK_NONE); }
977 // Convenience class for min and max values of a given BITS length.
979 template<int bits>
980 class Limits
982 public:
983 static const uint64_t MAX_UNSIGNED = (1ULL << bits) - 1;
984 static const int64_t MAX_SIGNED = MAX_UNSIGNED >> 1;
985 static const int64_t MIN_SIGNED = -MAX_SIGNED - 1;
988 template<>
989 class Limits<64>
991 public:
992 static const uint64_t MAX_UNSIGNED = ~0ULL;
993 static const int64_t MAX_SIGNED = MAX_UNSIGNED >> 1;
994 static const int64_t MIN_SIGNED = -MAX_SIGNED - 1;
997 // Integer manipulation functions used by various targets when
998 // performing relocations.
1000 template<int bits>
1001 class Bits
1003 public:
1004 // Sign extend an n-bit unsigned integer stored in a uint32_t into
1005 // an int32_t. BITS must be between 1 and 32.
1006 static inline int32_t
1007 sign_extend32(uint32_t val)
1009 gold_assert(bits > 0 && bits <= 32);
1010 if (bits == 32)
1011 return static_cast<int32_t>(val);
1012 uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - bits);
1013 val &= mask;
1014 uint32_t top_bit = 1U << (bits - 1);
1015 int32_t as_signed = static_cast<int32_t>(val);
1016 if ((val & top_bit) != 0)
1017 as_signed -= static_cast<int32_t>(top_bit * 2);
1018 return as_signed;
1021 // Return true if VAL (stored in a uint32_t) has overflowed a signed
1022 // value with BITS bits.
1023 static inline bool
1024 has_overflow32(uint32_t val)
1026 gold_assert(bits > 0 && bits <= 32);
1027 if (bits == 32)
1028 return false;
1029 const int32_t max = static_cast<int32_t>(Limits<bits>::MAX_SIGNED);
1030 const int32_t min = static_cast<int32_t>(Limits<bits>::MIN_SIGNED);
1031 int32_t as_signed = static_cast<int32_t>(val);
1032 return as_signed > max || as_signed < min;
1035 // Return true if VAL (stored in a uint32_t) has overflowed an unsigned
1036 // value with BITS bits.
1037 static inline bool
1038 has_unsigned_overflow32(uint32_t val)
1040 gold_assert(bits > 0 && bits <= 32);
1041 if (bits == 32)
1042 return false;
1043 const uint32_t max = static_cast<uint32_t>(Limits<bits>::MAX_UNSIGNED);
1044 return val > max;
1047 // Return true if VAL (stored in a uint32_t) has overflowed both a
1048 // signed and an unsigned value. E.g.,
1049 // Bits<8>::has_signed_unsigned_overflow32 would check -128 <= VAL <
1050 // 255.
1051 static inline bool
1052 has_signed_unsigned_overflow32(uint32_t val)
1054 gold_assert(bits > 0 && bits <= 32);
1055 if (bits == 32)
1056 return false;
1057 const int32_t max = static_cast<int32_t>(Limits<bits>::MAX_UNSIGNED);
1058 const int32_t min = static_cast<int32_t>(Limits<bits>::MIN_SIGNED);
1059 int32_t as_signed = static_cast<int32_t>(val);
1060 return as_signed > max || as_signed < min;
1063 // Select bits from A and B using bits in MASK. For each n in
1064 // [0..31], the n-th bit in the result is chosen from the n-th bits
1065 // of A and B. A zero selects A and a one selects B.
1066 static inline uint32_t
1067 bit_select32(uint32_t a, uint32_t b, uint32_t mask)
1068 { return (a & ~mask) | (b & mask); }
1070 // Sign extend an n-bit unsigned integer stored in a uint64_t into
1071 // an int64_t. BITS must be between 1 and 64.
1072 static inline int64_t
1073 sign_extend(uint64_t val)
1075 gold_assert(bits > 0 && bits <= 64);
1076 if (bits == 64)
1077 return static_cast<int64_t>(val);
1078 uint64_t mask = (~static_cast<uint64_t>(0)) >> (64 - bits);
1079 val &= mask;
1080 uint64_t top_bit = static_cast<uint64_t>(1) << (bits - 1);
1081 int64_t as_signed = static_cast<int64_t>(val);
1082 if ((val & top_bit) != 0)
1083 as_signed -= static_cast<int64_t>(top_bit * 2);
1084 return as_signed;
1087 // Return true if VAL (stored in a uint64_t) has overflowed a signed
1088 // value with BITS bits.
1089 static inline bool
1090 has_overflow(uint64_t val)
1092 gold_assert(bits > 0 && bits <= 64);
1093 if (bits == 64)
1094 return false;
1095 const int64_t max = Limits<bits>::MAX_SIGNED;
1096 const int64_t min = Limits<bits>::MIN_SIGNED;
1097 int64_t as_signed = static_cast<int64_t>(val);
1098 return as_signed > max || as_signed < min;
1101 // Return true if VAL (stored in a uint64_t) has overflowed an unsigned
1102 // value with BITS bits.
1103 static inline bool
1104 has_unsigned_overflow(uint64_t val)
1106 gold_assert(bits > 0 && bits <= 64);
1107 if (bits == 64)
1108 return false;
1109 const uint64_t max = Limits<bits>::MAX_UNSIGNED;
1110 return val > max;
1113 // Return true if VAL (stored in a uint64_t) has overflowed both a
1114 // signed and an unsigned value. E.g.,
1115 // Bits<8>::has_signed_unsigned_overflow would check -128 <= VAL <
1116 // 255.
1117 static inline bool
1118 has_signed_unsigned_overflow64(uint64_t val)
1120 gold_assert(bits > 0 && bits <= 64);
1121 if (bits == 64)
1122 return false;
1123 const int64_t max = static_cast<int64_t>(Limits<bits>::MAX_UNSIGNED);
1124 const int64_t min = Limits<bits>::MIN_SIGNED;
1125 int64_t as_signed = static_cast<int64_t>(val);
1126 return as_signed > max || as_signed < min;
1129 // Select bits from A and B using bits in MASK. For each n in
1130 // [0..31], the n-th bit in the result is chosen from the n-th bits
1131 // of A and B. A zero selects A and a one selects B.
1132 static inline uint64_t
1133 bit_select64(uint64_t a, uint64_t b, uint64_t mask)
1134 { return (a & ~mask) | (b & mask); }
1137 // Track relocations while reading a section. This lets you ask for
1138 // the relocation at a certain offset, and see how relocs occur
1139 // between points of interest.
1141 template<int size, bool big_endian>
1142 class Track_relocs
1144 public:
1145 Track_relocs()
1146 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
1149 // Initialize the Track_relocs object. OBJECT is the object holding
1150 // the reloc section, RELOC_SHNDX is the section index of the reloc
1151 // section, and RELOC_TYPE is the type of the reloc section
1152 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
1153 // something went wrong.
1154 bool
1155 initialize(Object* object, unsigned int reloc_shndx,
1156 unsigned int reloc_type);
1158 // Return the offset in the data section to which the next reloc
1159 // applies. This returns -1 if there is no next reloc.
1160 off_t
1161 next_offset() const;
1163 // Return the symbol index of the next reloc. This returns -1U if
1164 // there is no next reloc.
1165 unsigned int
1166 next_symndx() const;
1168 // Return the addend of the next reloc. This returns 0 if there is
1169 // no next reloc.
1170 uint64_t
1171 next_addend() const;
1173 // Advance to OFFSET within the data section, and return the number
1174 // of relocs which would be skipped, excluding r_info==0 relocs.
1176 advance(off_t offset);
1178 // Checkpoint the current position in the reloc section.
1179 section_size_type
1180 checkpoint() const
1181 { return this->pos_; }
1183 // Reset the position to CHECKPOINT.
1184 void
1185 reset(section_size_type checkpoint)
1186 { this->pos_ = checkpoint; }
1188 private:
1189 // The contents of the input object's reloc section.
1190 const unsigned char* prelocs_;
1191 // The length of the reloc section.
1192 section_size_type len_;
1193 // Our current position in the reloc section.
1194 section_size_type pos_;
1195 // The size of the relocs in the section.
1196 int reloc_size_;
1199 } // End namespace gold.
1201 #endif // !defined(GOLD_RELOC_H)