1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
28 #ifdef HAVE_BYTESWAP_H
33 #include "workqueue.h"
38 class General_options;
41 struct Read_relocs_data;
50 template<int size, bool big_endian>
51 class Sized_relobj_file;
56 template<int sh_type, bool dynamic, int size, bool big_endian>
57 class Output_data_reloc;
59 // A class to read the relocations for an object file, and then queue
60 // up a task to see if they require any GOT/PLT/COPY relocations in
63 class Read_relocs : public Task
66 // THIS_BLOCKER and NEXT_BLOCKER are passed along to a Scan_relocs
67 // or Gc_process_relocs task, so that they run in a deterministic
69 Read_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
70 Task_token* this_blocker, Task_token* next_blocker)
71 : symtab_(symtab), layout_(layout), object_(object),
72 this_blocker_(this_blocker), next_blocker_(next_blocker)
75 // The standard Task methods.
90 Symbol_table* symtab_;
93 Task_token* this_blocker_;
94 Task_token* next_blocker_;
97 // Process the relocs to figure out which sections are garbage.
98 // Very similar to scan relocs.
100 class Gc_process_relocs : public Task
103 // THIS_BLOCKER prevents this task from running until the previous
104 // one is finished. NEXT_BLOCKER prevents the next task from
106 Gc_process_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
107 Read_relocs_data* rd, Task_token* this_blocker,
108 Task_token* next_blocker)
109 : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
110 this_blocker_(this_blocker), next_blocker_(next_blocker)
113 ~Gc_process_relocs();
115 // The standard Task methods.
130 Symbol_table* symtab_;
133 Read_relocs_data* rd_;
134 Task_token* this_blocker_;
135 Task_token* next_blocker_;
138 // Scan the relocations for an object to see if they require any
139 // GOT/PLT/COPY relocations.
141 class Scan_relocs : public Task
144 // THIS_BLOCKER prevents this task from running until the previous
145 // one is finished. NEXT_BLOCKER prevents the next task from
147 Scan_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
148 Read_relocs_data* rd, Task_token* this_blocker,
149 Task_token* next_blocker)
150 : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
151 this_blocker_(this_blocker), next_blocker_(next_blocker)
156 // The standard Task methods.
171 Symbol_table* symtab_;
174 Read_relocs_data* rd_;
175 Task_token* this_blocker_;
176 Task_token* next_blocker_;
179 // A class to perform all the relocations for an object file.
181 class Relocate_task : public Task
184 Relocate_task(const Symbol_table* symtab, const Layout* layout,
185 Relobj* object, Output_file* of,
186 Task_token* input_sections_blocker,
187 Task_token* output_sections_blocker, Task_token* final_blocker)
188 : symtab_(symtab), layout_(layout), object_(object), of_(of),
189 input_sections_blocker_(input_sections_blocker),
190 output_sections_blocker_(output_sections_blocker),
191 final_blocker_(final_blocker)
194 // The standard Task methods.
209 const Symbol_table* symtab_;
210 const Layout* layout_;
213 Task_token* input_sections_blocker_;
214 Task_token* output_sections_blocker_;
215 Task_token* final_blocker_;
218 // During a relocatable link, this class records how relocations
219 // should be handled for a single input reloc section. An instance of
220 // this class is created while scanning relocs, and it is used while
221 // processing relocs.
223 class Relocatable_relocs
226 // We use a vector of unsigned char to indicate how the input relocs
227 // should be handled. Each element is one of the following values.
228 // We create this vector when we initially scan the relocations.
231 // Copy the input reloc. Don't modify it other than updating the
232 // r_offset field and the r_sym part of the r_info field.
234 // Copy the input reloc which is against an STT_SECTION symbol.
235 // Update the r_offset and r_sym part of the r_info field. Adjust
236 // the addend by subtracting the value of the old local symbol and
237 // adding the value of the new local symbol. The addend is in the
238 // SHT_RELA reloc and the contents of the data section do not need
240 RELOC_ADJUST_FOR_SECTION_RELA,
241 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
243 RELOC_ADJUST_FOR_SECTION_0,
244 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
245 // section need to be changed. The number indicates the number of
246 // bytes in the addend in the section contents.
247 RELOC_ADJUST_FOR_SECTION_1,
248 RELOC_ADJUST_FOR_SECTION_2,
249 RELOC_ADJUST_FOR_SECTION_4,
250 RELOC_ADJUST_FOR_SECTION_8,
251 // Like RELOC_ADJUST_FOR_SECTION_4 but for unaligned relocs.
252 RELOC_ADJUST_FOR_SECTION_4_UNALIGNED,
253 // Discard the input reloc--process it completely when relocating
254 // the data section contents.
256 // An input reloc which is not discarded, but which requires
257 // target specific processing in order to update it.
262 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
265 // Record the number of relocs.
267 set_reloc_count(size_t reloc_count)
268 { this->reloc_strategies_.reserve(reloc_count); }
270 // Record what to do for the next reloc.
272 set_next_reloc_strategy(Reloc_strategy strategy)
274 this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
275 if (strategy != RELOC_DISCARD)
276 ++this->output_reloc_count_;
279 // Record the Output_data associated with this reloc section.
281 set_output_data(Output_data* posd)
283 gold_assert(this->posd_ == NULL);
287 // Return the Output_data associated with this reloc section.
290 { return this->posd_; }
292 // Return what to do for reloc I.
294 strategy(unsigned int i) const
296 gold_assert(i < this->reloc_strategies_.size());
297 return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
300 // Return the number of relocations to create in the output file.
302 output_reloc_count() const
303 { return this->output_reloc_count_; }
306 typedef std::vector<unsigned char> Reloc_strategies;
308 // The strategies for the input reloc. There is one entry in this
309 // vector for each relocation in the input section.
310 Reloc_strategies reloc_strategies_;
311 // The number of relocations to be created in the output file.
312 size_t output_reloc_count_;
313 // The output data structure associated with this relocation.
317 // Standard relocation routines which are used on many targets. Here
318 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
320 template<int size, bool big_endian>
321 class Relocate_functions
324 // Do a simple relocation with the addend in the section contents.
325 // VALSIZE is the size of the value.
326 template<int valsize>
328 rel(unsigned char* view,
329 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
331 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
332 Valtype* wv = reinterpret_cast<Valtype*>(view);
333 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
334 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
337 // Like the above but for relocs at unaligned addresses.
338 template<int valsize>
340 rel_unaligned(unsigned char* view,
341 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
343 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
345 Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
346 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x + value);
349 // Do a simple relocation using a Symbol_value with the addend in
350 // the section contents. VALSIZE is the size of the value to
352 template<int valsize>
354 rel(unsigned char* view,
355 const Sized_relobj_file<size, big_endian>* object,
356 const Symbol_value<size>* psymval)
358 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
359 Valtype* wv = reinterpret_cast<Valtype*>(view);
360 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
361 x = psymval->value(object, x);
362 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
365 // Like the above but for relocs at unaligned addresses.
366 template<int valsize>
368 rel_unaligned(unsigned char* view,
369 const Sized_relobj_file<size, big_endian>* object,
370 const Symbol_value<size>* psymval)
372 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
374 Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
375 x = psymval->value(object, x);
376 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x);
379 // Do a simple relocation with the addend in the relocation.
380 // VALSIZE is the size of the value.
381 template<int valsize>
383 rela(unsigned char* view,
384 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
385 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
387 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
388 Valtype* wv = reinterpret_cast<Valtype*>(view);
389 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
392 // Do a simple relocation using a symbol value with the addend in
393 // the relocation. VALSIZE is the size of the value.
394 template<int valsize>
396 rela(unsigned char* view,
397 const Sized_relobj_file<size, big_endian>* object,
398 const Symbol_value<size>* psymval,
399 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
401 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
402 Valtype* wv = reinterpret_cast<Valtype*>(view);
403 Valtype x = psymval->value(object, addend);
404 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
407 // Do a simple PC relative relocation with the addend in the section
408 // contents. VALSIZE is the size of the value.
409 template<int valsize>
411 pcrel(unsigned char* view,
412 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
413 typename elfcpp::Elf_types<size>::Elf_Addr address)
415 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
416 Valtype* wv = reinterpret_cast<Valtype*>(view);
417 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
418 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
421 // Like the above but for relocs at unaligned addresses.
422 template<int valsize>
424 pcrel_unaligned(unsigned char* view,
425 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
426 typename elfcpp::Elf_types<size>::Elf_Addr address)
428 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
429 Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
430 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
431 x + value - address);
434 // Do a simple PC relative relocation with a Symbol_value with the
435 // addend in the section contents. VALSIZE is the size of the
437 template<int valsize>
439 pcrel(unsigned char* view,
440 const Sized_relobj_file<size, big_endian>* object,
441 const Symbol_value<size>* psymval,
442 typename elfcpp::Elf_types<size>::Elf_Addr address)
444 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
445 Valtype* wv = reinterpret_cast<Valtype*>(view);
446 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
447 x = psymval->value(object, x);
448 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
451 // Do a simple PC relative relocation with the addend in the
452 // relocation. VALSIZE is the size of the value.
453 template<int valsize>
455 pcrela(unsigned char* view,
456 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
457 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
458 typename elfcpp::Elf_types<size>::Elf_Addr address)
460 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
461 Valtype* wv = reinterpret_cast<Valtype*>(view);
462 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
465 // Do a simple PC relative relocation with a Symbol_value with the
466 // addend in the relocation. VALSIZE is the size of the value.
467 template<int valsize>
469 pcrela(unsigned char* view,
470 const Sized_relobj_file<size, big_endian>* object,
471 const Symbol_value<size>* psymval,
472 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
473 typename elfcpp::Elf_types<size>::Elf_Addr address)
475 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
476 Valtype* wv = reinterpret_cast<Valtype*>(view);
477 Valtype x = psymval->value(object, addend);
478 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
481 typedef Relocate_functions<size, big_endian> This;
484 // Do a simple 8-bit REL relocation with the addend in the section
487 rel8(unsigned char* view, unsigned char value)
488 { This::template rel<8>(view, value); }
491 rel8(unsigned char* view,
492 const Sized_relobj_file<size, big_endian>* object,
493 const Symbol_value<size>* psymval)
494 { This::template rel<8>(view, object, psymval); }
496 // Do an 8-bit RELA relocation with the addend in the relocation.
498 rela8(unsigned char* view, unsigned char value, unsigned char addend)
499 { This::template rela<8>(view, value, addend); }
502 rela8(unsigned char* view,
503 const Sized_relobj_file<size, big_endian>* object,
504 const Symbol_value<size>* psymval,
505 unsigned char addend)
506 { This::template rela<8>(view, object, psymval, addend); }
508 // Do a simple 8-bit PC relative relocation with the addend in the
511 pcrel8(unsigned char* view, unsigned char value,
512 typename elfcpp::Elf_types<size>::Elf_Addr address)
513 { This::template pcrel<8>(view, value, address); }
516 pcrel8(unsigned char* view,
517 const Sized_relobj_file<size, big_endian>* object,
518 const Symbol_value<size>* psymval,
519 typename elfcpp::Elf_types<size>::Elf_Addr address)
520 { This::template pcrel<8>(view, object, psymval, address); }
522 // Do a simple 8-bit PC relative RELA relocation with the addend in
525 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
526 typename elfcpp::Elf_types<size>::Elf_Addr address)
527 { This::template pcrela<8>(view, value, addend, address); }
530 pcrela8(unsigned char* view,
531 const Sized_relobj_file<size, big_endian>* object,
532 const Symbol_value<size>* psymval,
533 unsigned char addend,
534 typename elfcpp::Elf_types<size>::Elf_Addr address)
535 { This::template pcrela<8>(view, object, psymval, addend, address); }
537 // Do a simple 16-bit REL relocation with the addend in the section
540 rel16(unsigned char* view, elfcpp::Elf_Half value)
541 { This::template rel<16>(view, value); }
544 rel16(unsigned char* view,
545 const Sized_relobj_file<size, big_endian>* object,
546 const Symbol_value<size>* psymval)
547 { This::template rel<16>(view, object, psymval); }
549 // Do an 16-bit RELA relocation with the addend in the relocation.
551 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
552 { This::template rela<16>(view, value, addend); }
555 rela16(unsigned char* view,
556 const Sized_relobj_file<size, big_endian>* object,
557 const Symbol_value<size>* psymval,
558 elfcpp::Elf_Half addend)
559 { This::template rela<16>(view, object, psymval, addend); }
561 // Do a simple 16-bit PC relative REL relocation with the addend in
562 // the section contents.
564 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
565 typename elfcpp::Elf_types<size>::Elf_Addr address)
566 { This::template pcrel<16>(view, value, address); }
569 pcrel16(unsigned char* view,
570 const Sized_relobj_file<size, big_endian>* object,
571 const Symbol_value<size>* psymval,
572 typename elfcpp::Elf_types<size>::Elf_Addr address)
573 { This::template pcrel<16>(view, object, psymval, address); }
575 // Do a simple 16-bit PC relative RELA relocation with the addend in
578 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
579 elfcpp::Elf_Half addend,
580 typename elfcpp::Elf_types<size>::Elf_Addr address)
581 { This::template pcrela<16>(view, value, addend, address); }
584 pcrela16(unsigned char* view,
585 const Sized_relobj_file<size, big_endian>* object,
586 const Symbol_value<size>* psymval,
587 elfcpp::Elf_Half addend,
588 typename elfcpp::Elf_types<size>::Elf_Addr address)
589 { This::template pcrela<16>(view, object, psymval, addend, address); }
591 // Do a simple 32-bit REL relocation with the addend in the section
594 rel32(unsigned char* view, elfcpp::Elf_Word value)
595 { This::template rel<32>(view, value); }
597 // Like above but for relocs at unaligned addresses.
599 rel32_unaligned(unsigned char* view, elfcpp::Elf_Word value)
600 { This::template rel_unaligned<32>(view, value); }
603 rel32(unsigned char* view,
604 const Sized_relobj_file<size, big_endian>* object,
605 const Symbol_value<size>* psymval)
606 { This::template rel<32>(view, object, psymval); }
608 // Like above but for relocs at unaligned addresses.
610 rel32_unaligned(unsigned char* view,
611 const Sized_relobj_file<size, big_endian>* object,
612 const Symbol_value<size>* psymval)
613 { This::template rel_unaligned<32>(view, object, psymval); }
615 // Do an 32-bit RELA relocation with the addend in the relocation.
617 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
618 { This::template rela<32>(view, value, addend); }
621 rela32(unsigned char* view,
622 const Sized_relobj_file<size, big_endian>* object,
623 const Symbol_value<size>* psymval,
624 elfcpp::Elf_Word addend)
625 { This::template rela<32>(view, object, psymval, addend); }
627 // Do a simple 32-bit PC relative REL relocation with the addend in
628 // the section contents.
630 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
631 typename elfcpp::Elf_types<size>::Elf_Addr address)
632 { This::template pcrel<32>(view, value, address); }
634 // Unaligned version of the above.
636 pcrel32_unaligned(unsigned char* view, elfcpp::Elf_Word value,
637 typename elfcpp::Elf_types<size>::Elf_Addr address)
638 { This::template pcrel_unaligned<32>(view, value, address); }
641 pcrel32(unsigned char* view,
642 const Sized_relobj_file<size, big_endian>* object,
643 const Symbol_value<size>* psymval,
644 typename elfcpp::Elf_types<size>::Elf_Addr address)
645 { This::template pcrel<32>(view, object, psymval, address); }
647 // Do a simple 32-bit PC relative RELA relocation with the addend in
650 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
651 elfcpp::Elf_Word addend,
652 typename elfcpp::Elf_types<size>::Elf_Addr address)
653 { This::template pcrela<32>(view, value, addend, address); }
656 pcrela32(unsigned char* view,
657 const Sized_relobj_file<size, big_endian>* object,
658 const Symbol_value<size>* psymval,
659 elfcpp::Elf_Word addend,
660 typename elfcpp::Elf_types<size>::Elf_Addr address)
661 { This::template pcrela<32>(view, object, psymval, addend, address); }
663 // Do a simple 64-bit REL relocation with the addend in the section
666 rel64(unsigned char* view, elfcpp::Elf_Xword value)
667 { This::template rel<64>(view, value); }
670 rel64(unsigned char* view,
671 const Sized_relobj_file<size, big_endian>* object,
672 const Symbol_value<size>* psymval)
673 { This::template rel<64>(view, object, psymval); }
675 // Do a 64-bit RELA relocation with the addend in the relocation.
677 rela64(unsigned char* view, elfcpp::Elf_Xword value,
678 elfcpp::Elf_Xword addend)
679 { This::template rela<64>(view, value, addend); }
682 rela64(unsigned char* view,
683 const Sized_relobj_file<size, big_endian>* object,
684 const Symbol_value<size>* psymval,
685 elfcpp::Elf_Xword addend)
686 { This::template rela<64>(view, object, psymval, addend); }
688 // Do a simple 64-bit PC relative REL relocation with the addend in
689 // the section contents.
691 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
692 typename elfcpp::Elf_types<size>::Elf_Addr address)
693 { This::template pcrel<64>(view, value, address); }
696 pcrel64(unsigned char* view,
697 const Sized_relobj_file<size, big_endian>* object,
698 const Symbol_value<size>* psymval,
699 typename elfcpp::Elf_types<size>::Elf_Addr address)
700 { This::template pcrel<64>(view, object, psymval, address); }
702 // Do a simple 64-bit PC relative RELA relocation with the addend in
705 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
706 elfcpp::Elf_Xword addend,
707 typename elfcpp::Elf_types<size>::Elf_Addr address)
708 { This::template pcrela<64>(view, value, addend, address); }
711 pcrela64(unsigned char* view,
712 const Sized_relobj_file<size, big_endian>* object,
713 const Symbol_value<size>* psymval,
714 elfcpp::Elf_Xword addend,
715 typename elfcpp::Elf_types<size>::Elf_Addr address)
716 { This::template pcrela<64>(view, object, psymval, addend, address); }
719 // Integer manipulation functions used by various targets when
720 // performing relocations.
726 // Sign extend an n-bit unsigned integer stored in a uint32_t into
727 // an int32_t. BITS must be between 1 and 32.
728 static inline int32_t
729 sign_extend32(uint32_t val)
731 gold_assert(bits > 0 && bits <= 32);
733 return static_cast<int32_t>(val);
734 uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - bits);
736 uint32_t top_bit = 1U << (bits - 1);
737 int32_t as_signed = static_cast<int32_t>(val);
738 if ((val & top_bit) != 0)
739 as_signed -= static_cast<int32_t>(top_bit * 2);
743 // Return true if VAL (stored in a uint32_t) has overflowed a signed
744 // value with BITS bits.
746 has_overflow32(uint32_t val)
748 gold_assert(bits > 0 && bits <= 32);
751 int32_t max = (1 << (bits - 1)) - 1;
752 int32_t min = -(1 << (bits - 1));
753 int32_t as_signed = static_cast<int32_t>(val);
754 return as_signed > max || as_signed < min;
757 // Return true if VAL (stored in a uint32_t) has overflowed both a
758 // signed and an unsigned value. E.g.,
759 // Bits<8>::has_signed_unsigned_overflow32 would check -128 <= VAL <
762 has_signed_unsigned_overflow32(uint32_t val)
764 gold_assert(bits > 0 && bits <= 32);
767 int32_t max = static_cast<int32_t>((1U << bits) - 1);
768 int32_t min = -(1 << (bits - 1));
769 int32_t as_signed = static_cast<int32_t>(val);
770 return as_signed > max || as_signed < min;
773 // Select bits from A and B using bits in MASK. For each n in
774 // [0..31], the n-th bit in the result is chosen from the n-th bits
775 // of A and B. A zero selects A and a one selects B.
776 static inline uint32_t
777 bit_select32(uint32_t a, uint32_t b, uint32_t mask)
778 { return (a & ~mask) | (b & mask); }
780 // Sign extend an n-bit unsigned integer stored in a uint64_t into
781 // an int64_t. BITS must be between 1 and 64.
782 static inline int64_t
783 sign_extend(uint64_t val)
785 gold_assert(bits > 0 && bits <= 64);
787 return static_cast<int64_t>(val);
788 uint64_t mask = (~static_cast<uint64_t>(0)) >> (64 - bits);
790 uint64_t top_bit = static_cast<uint64_t>(1) << (bits - 1);
791 int64_t as_signed = static_cast<int64_t>(val);
792 if ((val & top_bit) != 0)
793 as_signed -= static_cast<int64_t>(top_bit * 2);
797 // Return true if VAL (stored in a uint64_t) has overflowed a signed
798 // value with BITS bits.
800 has_overflow(uint64_t val)
802 gold_assert(bits > 0 && bits <= 64);
805 int64_t max = (static_cast<int64_t>(1) << (bits - 1)) - 1;
806 int64_t min = -(static_cast<int64_t>(1) << (bits - 1));
807 int64_t as_signed = static_cast<int64_t>(val);
808 return as_signed > max || as_signed < min;
811 // Return true if VAL (stored in a uint64_t) has overflowed both a
812 // signed and an unsigned value. E.g.,
813 // Bits<8>::has_signed_unsigned_overflow would check -128 <= VAL <
816 has_signed_unsigned_overflow64(uint64_t val)
818 gold_assert(bits > 0 && bits <= 64);
821 int64_t max = static_cast<int64_t>((static_cast<uint64_t>(1) << bits) - 1);
822 int64_t min = -(static_cast<int64_t>(1) << (bits - 1));
823 int64_t as_signed = static_cast<int64_t>(val);
824 return as_signed > max || as_signed < min;
827 // Select bits from A and B using bits in MASK. For each n in
828 // [0..31], the n-th bit in the result is chosen from the n-th bits
829 // of A and B. A zero selects A and a one selects B.
830 static inline uint64_t
831 bit_select64(uint64_t a, uint64_t b, uint64_t mask)
832 { return (a & ~mask) | (b & mask); }
835 // Track relocations while reading a section. This lets you ask for
836 // the relocation at a certain offset, and see how relocs occur
837 // between points of interest.
839 template<int size, bool big_endian>
844 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
847 // Initialize the Track_relocs object. OBJECT is the object holding
848 // the reloc section, RELOC_SHNDX is the section index of the reloc
849 // section, and RELOC_TYPE is the type of the reloc section
850 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
851 // something went wrong.
853 initialize(Object* object, unsigned int reloc_shndx,
854 unsigned int reloc_type);
856 // Return the offset in the data section to which the next reloc
857 // applies. This returns -1 if there is no next reloc.
861 // Return the symbol index of the next reloc. This returns -1U if
862 // there is no next reloc.
866 // Return the addend of the next reloc. This returns 0 if there is
871 // Advance to OFFSET within the data section, and return the number
872 // of relocs which would be skipped.
874 advance(off_t offset);
876 // Checkpoint the current position in the reloc section.
879 { return this->pos_; }
881 // Reset the position to CHECKPOINT.
883 reset(section_size_type checkpoint)
884 { this->pos_ = checkpoint; }
887 // The contents of the input object's reloc section.
888 const unsigned char* prelocs_;
889 // The length of the reloc section.
890 section_size_type len_;
891 // Our current position in the reloc section.
892 section_size_type pos_;
893 // The size of the relocs in the section.
897 } // End namespace gold.
899 #endif // !defined(GOLD_RELOC_H)