Overhaul relocation framework to support overflow checking.
[external/binutils.git] / gold / reloc.h
1 // reloc.h -- relocate input files for gold   -*- C++ -*-
2
3 // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #ifndef GOLD_RELOC_H
24 #define GOLD_RELOC_H
25
26 #include <vector>
27 #ifdef HAVE_BYTESWAP_H
28 #include <byteswap.h>
29 #endif
30
31 #include "elfcpp.h"
32 #include "workqueue.h"
33
34 namespace gold
35 {
36
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;
45
46 template<int size>
47 class Sized_symbol;
48
49 template<int size, bool big_endian>
50 class Sized_relobj_file;
51
52 template<int size>
53 class Symbol_value;
54
55 template<int sh_type, bool dynamic, int size, bool big_endian>
56 class Output_data_reloc;
57
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.
61
62 class Read_relocs : public Task
63 {
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   { }
73
74   // The standard Task methods.
75
76   Task_token*
77   is_runnable();
78
79   void
80   locks(Task_locker*);
81
82   void
83   run(Workqueue*);
84
85   std::string
86   get_name() const;
87
88  private:
89   Symbol_table* symtab_;
90   Layout* layout_;
91   Relobj* object_;
92   Task_token* this_blocker_;
93   Task_token* next_blocker_;
94 };
95
96 // Process the relocs to figure out which sections are garbage.
97 // Very similar to scan relocs.
98
99 class Gc_process_relocs : public Task
100 {
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)
110   { }
111
112   ~Gc_process_relocs();
113
114   // The standard Task methods.
115
116   Task_token*
117   is_runnable();
118
119   void
120   locks(Task_locker*);
121
122   void
123   run(Workqueue*);
124
125   std::string
126   get_name() const;
127
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_;
135 };
136
137 // Scan the relocations for an object to see if they require any
138 // GOT/PLT/COPY relocations.
139
140 class Scan_relocs : public Task
141 {
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)
151   { }
152
153   ~Scan_relocs();
154
155   // The standard Task methods.
156
157   Task_token*
158   is_runnable();
159
160   void
161   locks(Task_locker*);
162
163   void
164   run(Workqueue*);
165
166   std::string
167   get_name() const;
168
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_;
176 };
177
178 // A class to perform all the relocations for an object file.
179
180 class Relocate_task : public Task
181 {
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)
191   { }
192
193   // The standard Task methods.
194
195   Task_token*
196   is_runnable();
197
198   void
199   locks(Task_locker*);
200
201   void
202   run(Workqueue*);
203
204   std::string
205   get_name() const;
206
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_;
215 };
216
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.
221
222 class Relocatable_relocs
223 {
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
229   {
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
258   };
259
260   Relocatable_relocs()
261     : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
262   { }
263
264   // Record the number of relocs.
265   void
266   set_reloc_count(size_t reloc_count)
267   { this->reloc_strategies_.reserve(reloc_count); }
268
269   // Record what to do for the next reloc.
270   void
271   set_next_reloc_strategy(Reloc_strategy strategy)
272   {
273     this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
274     if (strategy != RELOC_DISCARD)
275       ++this->output_reloc_count_;
276   }
277
278   // Record the Output_data associated with this reloc section.
279   void
280   set_output_data(Output_data* posd)
281   {
282     gold_assert(this->posd_ == NULL);
283     this->posd_ = posd;
284   }
285
286   // Return the Output_data associated with this reloc section.
287   Output_data*
288   output_data() const
289   { return this->posd_; }
290
291   // Return what to do for reloc I.
292   Reloc_strategy
293   strategy(unsigned int i) const
294   {
295     gold_assert(i < this->reloc_strategies_.size());
296     return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
297   }
298
299   // Set the strategy for reloc I.
300   void
301   set_strategy(unsigned int i, Reloc_strategy strategy)
302   {
303     gold_assert(i < this->reloc_strategies_.size());
304     this->reloc_strategies_[i] = strategy;
305   }
306
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_; }
311
312  private:
313   typedef std::vector<unsigned char> Reloc_strategies;
314
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_;
322 };
323
324 template<int valsize>
325 class Bits;
326
327 // Standard relocation routines which are used on many targets.  Here
328 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
329
330 template<int size, bool big_endian>
331 class Relocate_functions
332 {
333  public:
334   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
335   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addendtype;
336
337   enum Overflow_check
338   {
339     CHECK_NONE,
340     CHECK_SIGNED,
341     CHECK_UNSIGNED,
342     CHECK_SIGNED_OR_UNSIGNED
343   };
344
345   enum Reloc_status
346   {
347     RELOC_OK,
348     RELOC_OVERFLOW
349   };
350
351  private:
352   // Check for overflow.
353   template<int valsize>
354   static inline Reloc_status
355   check_overflow(Address value, Overflow_check check)
356   {
357     switch (check)
358       {
359       case CHECK_SIGNED:
360         if (size == 32)
361           return (Bits<valsize>::has_overflow32(value)
362                   ? RELOC_OVERFLOW
363                   : RELOC_OK);
364         else
365           return (Bits<valsize>::has_overflow(value)
366                   ? RELOC_OVERFLOW
367                   : RELOC_OK);
368       case CHECK_UNSIGNED:
369         if (size == 32)
370           return (Bits<valsize>::has_unsigned_overflow32(value)
371                   ? RELOC_OVERFLOW
372                   : RELOC_OK);
373         else
374           return (Bits<valsize>::has_unsigned_overflow(value)
375                   ? RELOC_OVERFLOW
376                   : RELOC_OK);
377       case CHECK_SIGNED_OR_UNSIGNED:
378         if (size == 32)
379           return (Bits<valsize>::has_signed_unsigned_overflow32(value)
380                   ? RELOC_OVERFLOW
381                   : RELOC_OK);
382         else
383           return (Bits<valsize>::has_signed_unsigned_overflow64(value)
384                   ? RELOC_OVERFLOW
385                   : RELOC_OK);
386       case CHECK_NONE:
387       default:
388         return RELOC_OK;
389       }
390   }
391
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)
397   {
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);
401     value += addend;
402     elfcpp::Swap<valsize, big_endian>::
403         writeval(wv, static_cast<Valtype>(value));
404     return check_overflow<valsize>(value, check);
405   }
406
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)
411   {
412     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
413         Valtype;
414     Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
415     value += addend;
416     elfcpp::Swap_unaligned<valsize, big_endian>::
417         writeval(view, static_cast<Valtype>(value));
418     return check_overflow<valsize>(value, check);
419   }
420
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
423   // relocate.
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)
430   {
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);
438   }
439
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)
447   {
448     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
449         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);
454   }
455
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)
462   {
463     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
464     Valtype* wv = reinterpret_cast<Valtype*>(view);
465     value += addend;
466     elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
467     return check_overflow<valsize>(value, check);
468   }
469
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,
477        Addendtype addend,
478        Overflow_check check)
479   {
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);
485   }
486
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)
493   {
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);
500   }
501
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)
507   {
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);
513   }
514
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
517   // value.
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,
523         Address address,
524         Overflow_check check)
525   {
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);
532   }
533
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)
540   {
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);
546   }
547
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,
555          Addendtype addend,
556          Address address,
557          Overflow_check check)
558   {
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);
564   }
565
566   typedef Relocate_functions<size, big_endian> This;
567
568  public:
569   // Do a simple 8-bit REL relocation with the addend in the section
570   // contents.
571   static inline void
572   rel8(unsigned char* view, Address value)
573   { This::template rel<8>(view, value, CHECK_NONE); }
574
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); }
578
579   static inline void
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); }
584
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); }
591
592   // Do an 8-bit RELA relocation with the addend in the relocation.
593   static inline void
594   rela8(unsigned char* view, Address value, Addendtype addend)
595   { This::template rela<8>(view, value, addend, CHECK_NONE); }
596
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); }
601
602   static inline void
603   rela8(unsigned char* view,
604         const Sized_relobj_file<size, big_endian>* object,
605         const Symbol_value<size>* psymval,
606         Addendtype addend)
607   { This::template rela<8>(view, object, psymval, addend, CHECK_NONE); }
608
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,
613               Addendtype addend,
614               Overflow_check check)
615   { return This::template rela<8>(view, object, psymval, addend, check); }
616
617   // Do a simple 8-bit PC relative relocation with the addend in the
618   // section contents.
619   static inline void
620   pcrel8(unsigned char* view, unsigned char value, Address address)
621   { This::template pcrel<8>(view, value, address, CHECK_NONE); }
622
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); }
627
628   static inline void
629   pcrel8(unsigned char* view,
630          const Sized_relobj_file<size, big_endian>* object,
631          const Symbol_value<size>* psymval,
632          Address address)
633   { This::template pcrel<8>(view, object, psymval, address, CHECK_NONE); }
634
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,
639                Address address,
640                Overflow_check check)
641   { return This::template pcrel<8>(view, object, psymval, address, check); }
642
643   // Do a simple 8-bit PC relative RELA relocation with the addend in
644   // the reloc.
645   static inline void
646   pcrela8(unsigned char* view, Address value, Addendtype addend,
647           Address address)
648   { This::template pcrela<8>(view, value, addend, address, CHECK_NONE); }
649
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); }
654
655   static inline void
656   pcrela8(unsigned char* view,
657           const Sized_relobj_file<size, big_endian>* object,
658           const Symbol_value<size>* psymval,
659           Addendtype addend,
660           Address address)
661   { This::template pcrela<8>(view, object, psymval, addend, address,
662                              CHECK_NONE); }
663
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,
668                 Addendtype addend,
669                 Address address,
670                 Overflow_check check)
671   { return This::template pcrela<8>(view, object, psymval, addend, address,
672                                     check); }
673
674   // Do a simple 16-bit REL relocation with the addend in the section
675   // contents.
676   static inline void
677   rel16(unsigned char* view, Address value)
678   { This::template rel<16>(view, value, CHECK_NONE); }
679
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); }
683
684   static inline void
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); }
689
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); }
696
697   // Do an 16-bit RELA relocation with the addend in the relocation.
698   static inline void
699   rela16(unsigned char* view, Address value, Addendtype addend)
700   { This::template rela<16>(view, value, addend, CHECK_NONE); }
701
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); }
706
707   static inline void
708   rela16(unsigned char* view,
709          const Sized_relobj_file<size, big_endian>* object,
710          const Symbol_value<size>* psymval,
711          Addendtype addend)
712   { This::template rela<16>(view, object, psymval, addend, CHECK_NONE); }
713
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,
718                Addendtype addend,
719                Overflow_check check)
720   { return This::template rela<16>(view, object, psymval, addend, check); }
721
722   // Do a simple 16-bit PC relative REL relocation with the addend in
723   // the section contents.
724   static inline void
725   pcrel16(unsigned char* view, Address value, Address address)
726   { This::template pcrel<16>(view, value, address, CHECK_NONE); }
727
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); }
732
733   static inline void
734   pcrel16(unsigned char* view,
735           const Sized_relobj_file<size, big_endian>* object,
736           const Symbol_value<size>* psymval,
737           Address address)
738   { This::template pcrel<16>(view, object, psymval, address, CHECK_NONE); }
739
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,
744                 Address address,
745                 Overflow_check check)
746   { return This::template pcrel<16>(view, object, psymval, address, check); }
747
748   // Do a simple 16-bit PC relative RELA relocation with the addend in
749   // the reloc.
750   static inline void
751   pcrela16(unsigned char* view, Address value, Addendtype addend,
752            Address address)
753   { This::template pcrela<16>(view, value, addend, address, CHECK_NONE); }
754
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); }
759
760   static inline void
761   pcrela16(unsigned char* view,
762            const Sized_relobj_file<size, big_endian>* object,
763            const Symbol_value<size>* psymval,
764            Addendtype addend,
765            Address address)
766   { This::template pcrela<16>(view, object, psymval, addend, address,
767                               CHECK_NONE); }
768
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,
773                  Addendtype addend,
774                  Address address,
775                  Overflow_check check)
776   { return This::template pcrela<16>(view, object, psymval, addend, address,
777                                      check); }
778
779   // Do a simple 32-bit REL relocation with the addend in the section
780   // contents.
781   static inline void
782   rel32(unsigned char* view, Address value)
783   { This::template rel<32>(view, value, CHECK_NONE); }
784
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); }
788
789   // Like above but for relocs at unaligned addresses.
790   static inline void
791   rel32_unaligned(unsigned char* view, Address value)
792   { This::template rel_unaligned<32>(view, value, CHECK_NONE); }
793
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); }
798
799   static inline void
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); }
804
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); }
811
812   // Like above but for relocs at unaligned addresses.
813   static inline void
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); }
818
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); }
825
826   // Do a 32-bit RELA relocation with the addend in the relocation.
827   static inline void
828   rela32(unsigned char* view, Address value, Addendtype addend)
829   { This::template rela<32>(view, value, addend, CHECK_NONE); }
830
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); }
835
836   static inline void
837   rela32(unsigned char* view,
838          const Sized_relobj_file<size, big_endian>* object,
839          const Symbol_value<size>* psymval,
840          Addendtype addend)
841   { This::template rela<32>(view, object, psymval, addend, CHECK_NONE); }
842
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,
847                Addendtype addend,
848                Overflow_check check)
849   { return This::template rela<32>(view, object, psymval, addend, check); }
850
851   // Do a simple 32-bit PC relative REL relocation with the addend in
852   // the section contents.
853   static inline void
854   pcrel32(unsigned char* view, Address value, Address address)
855   { This::template pcrel<32>(view, value, address, CHECK_NONE); }
856
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); }
861
862   // Unaligned version of the above.
863   static inline void
864   pcrel32_unaligned(unsigned char* view, Address value, Address address)
865   { This::template pcrel_unaligned<32>(view, value, address, CHECK_NONE); }
866
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); }
871
872   static inline void
873   pcrel32(unsigned char* view,
874           const Sized_relobj_file<size, big_endian>* object,
875           const Symbol_value<size>* psymval,
876           Address address)
877   { This::template pcrel<32>(view, object, psymval, address, CHECK_NONE); }
878
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,
883                 Address address,
884                 Overflow_check check)
885   { return This::template pcrel<32>(view, object, psymval, address, check); }
886
887   // Do a simple 32-bit PC relative RELA relocation with the addend in
888   // the relocation.
889   static inline void
890   pcrela32(unsigned char* view, Address value, Addendtype addend,
891            Address address)
892   { This::template pcrela<32>(view, value, addend, address, CHECK_NONE); }
893
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); }
898
899   static inline void
900   pcrela32(unsigned char* view,
901            const Sized_relobj_file<size, big_endian>* object,
902            const Symbol_value<size>* psymval,
903            Addendtype addend,
904            Address address)
905   { This::template pcrela<32>(view, object, psymval, addend, address,
906                               CHECK_NONE); }
907
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,
912            Addendtype addend,
913            Address address,
914            Overflow_check check)
915   { return This::template pcrela<32>(view, object, psymval, addend, address,
916                                      check); }
917
918   // Do a simple 64-bit REL relocation with the addend in the section
919   // contents.
920   static inline void
921   rel64(unsigned char* view, Address value)
922   { This::template rel<64>(view, value, CHECK_NONE); }
923
924   static inline void
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); }
929
930   // Do a 64-bit RELA relocation with the addend in the relocation.
931   static inline void
932   rela64(unsigned char* view, Address value, Addendtype addend)
933   { This::template rela<64>(view, value, addend, CHECK_NONE); }
934
935   static inline void
936   rela64(unsigned char* view,
937          const Sized_relobj_file<size, big_endian>* object,
938          const Symbol_value<size>* psymval,
939          Addendtype addend)
940   { This::template rela<64>(view, object, psymval, addend, CHECK_NONE); }
941
942   // Do a simple 64-bit PC relative REL relocation with the addend in
943   // the section contents.
944   static inline void
945   pcrel64(unsigned char* view, Address value, Address address)
946   { This::template pcrel<64>(view, value, address, CHECK_NONE); }
947
948   static inline void
949   pcrel64(unsigned char* view,
950           const Sized_relobj_file<size, big_endian>* object,
951           const Symbol_value<size>* psymval,
952           Address address)
953   { This::template pcrel<64>(view, object, psymval, address, CHECK_NONE); }
954
955   // Do a simple 64-bit PC relative RELA relocation with the addend in
956   // the relocation.
957   static inline void
958   pcrela64(unsigned char* view, Address value, Addendtype addend,
959            Address address)
960   { This::template pcrela<64>(view, value, addend, address, CHECK_NONE); }
961
962   static inline void
963   pcrela64(unsigned char* view,
964            const Sized_relobj_file<size, big_endian>* object,
965            const Symbol_value<size>* psymval,
966            Addendtype addend,
967            Address address)
968   { This::template pcrela<64>(view, object, psymval, addend, address,
969                               CHECK_NONE); }
970 };
971
972 // Integer manipulation functions used by various targets when
973 // performing relocations.
974
975 template<int bits>
976 class Bits
977 {
978  public:
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)
983   {
984     gold_assert(bits > 0 && bits <= 32);
985     if (bits == 32)
986       return static_cast<int32_t>(val);
987     uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - bits);
988     val &= mask;
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);
993     return as_signed;    
994   }
995
996   // Return true if VAL (stored in a uint32_t) has overflowed a signed
997   // value with BITS bits.
998   static inline bool
999   has_overflow32(uint32_t val)
1000   {
1001     gold_assert(bits > 0 && bits <= 32);
1002     if (bits == 32)
1003       return false;
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;
1008   }
1009
1010   // Return true if VAL (stored in a uint32_t) has overflowed an unsigned
1011   // value with BITS bits.
1012   static inline bool
1013   has_unsigned_overflow32(uint32_t val)
1014   {
1015     gold_assert(bits > 0 && bits <= 32);
1016     if (bits == 32)
1017       return false;
1018     int32_t max = static_cast<int32_t>((1U << bits) - 1);
1019     return val > max;
1020   }
1021
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 <
1025   // 255.
1026   static inline bool
1027   has_signed_unsigned_overflow32(uint32_t val)
1028   {
1029     gold_assert(bits > 0 && bits <= 32);
1030     if (bits == 32)
1031       return false;
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;
1036   }
1037
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); }
1044
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)
1049   {
1050     gold_assert(bits > 0 && bits <= 64);
1051     if (bits == 64)
1052       return static_cast<int64_t>(val);
1053     uint64_t mask = (~static_cast<uint64_t>(0)) >> (64 - bits);
1054     val &= mask;
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);
1059     return as_signed;    
1060   }
1061
1062   // Return true if VAL (stored in a uint64_t) has overflowed a signed
1063   // value with BITS bits.
1064   static inline bool
1065   has_overflow(uint64_t val)
1066   {
1067     gold_assert(bits > 0 && bits <= 64);
1068     if (bits == 64)
1069       return false;
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;
1074   }
1075
1076   // Return true if VAL (stored in a uint64_t) has overflowed an unsigned
1077   // value with BITS bits.
1078   static inline bool
1079   has_unsigned_overflow(uint64_t val)
1080   {
1081     gold_assert(bits > 0 && bits <= 64);
1082     if (bits == 64)
1083       return false;
1084     int64_t max = static_cast<int64_t>((static_cast<uint64_t>(1) << bits) - 1);
1085     return val > max;
1086   }
1087
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 <
1091   // 255.
1092   static inline bool
1093   has_signed_unsigned_overflow64(uint64_t val)
1094   {
1095     gold_assert(bits > 0 && bits <= 64);
1096     if (bits == 64)
1097       return false;
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;
1102   }
1103
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); }
1110 };
1111
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.
1115
1116 template<int size, bool big_endian>
1117 class Track_relocs
1118 {
1119  public:
1120   Track_relocs()
1121     : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
1122   { }
1123
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.
1129   bool
1130   initialize(Object* object, unsigned int reloc_shndx,
1131              unsigned int reloc_type);
1132
1133   // Return the offset in the data section to which the next reloc
1134   // applies.  This returns -1 if there is no next reloc.
1135   off_t
1136   next_offset() const;
1137
1138   // Return the symbol index of the next reloc.  This returns -1U if
1139   // there is no next reloc.
1140   unsigned int
1141   next_symndx() const;
1142
1143   // Return the addend of the next reloc.  This returns 0 if there is
1144   // no next reloc.
1145   uint64_t
1146   next_addend() const;
1147
1148   // Advance to OFFSET within the data section, and return the number
1149   // of relocs which would be skipped.
1150   int
1151   advance(off_t offset);
1152
1153   // Checkpoint the current position in the reloc section.
1154   section_size_type
1155   checkpoint() const
1156   { return this->pos_; }
1157
1158   // Reset the position to CHECKPOINT.
1159   void
1160   reset(section_size_type checkpoint)
1161   { this->pos_ = checkpoint; }
1162
1163  private:
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.
1171   int reloc_size_;
1172 };
1173
1174 } // End namespace gold.
1175
1176 #endif // !defined(GOLD_RELOC_H)