1 // reloc.h -- relocate input files for gold   -*- C++ -*-
2 
3 // Copyright (C) 2006-2014 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.
Read_relocs(Symbol_table * symtab,Layout * layout,Relobj * object,Task_token * this_blocker,Task_token * next_blocker)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.
Gc_process_relocs(Symbol_table * symtab,Layout * layout,Relobj * object,Read_relocs_data * rd,Task_token * this_blocker,Task_token * next_blocker)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.
Scan_relocs(Symbol_table * symtab,Layout * layout,Relobj * object,Read_relocs_data * rd,Task_token * this_blocker,Task_token * next_blocker)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:
Relocate_task(const Symbol_table * symtab,const Layout * layout,Relobj * object,Output_file * of,Task_token * input_sections_blocker,Task_token * output_sections_blocker,Task_token * final_blocker)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 
Relocatable_relocs()260   Relocatable_relocs()
261     : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
262   { }
263 
264   // Record the number of relocs.
265   void
set_reloc_count(size_t reloc_count)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
set_next_reloc_strategy(Reloc_strategy strategy)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
set_output_data(Output_data * posd)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*
output_data()288   output_data() const
289   { return this->posd_; }
290 
291   // Return what to do for reloc I.
292   Reloc_strategy
strategy(unsigned int i)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   // Return the number of relocations to create in the output file.
300   size_t
output_reloc_count()301   output_reloc_count() const
302   { return this->output_reloc_count_; }
303 
304  private:
305   typedef std::vector<unsigned char> Reloc_strategies;
306 
307   // The strategies for the input reloc.  There is one entry in this
308   // vector for each relocation in the input section.
309   Reloc_strategies reloc_strategies_;
310   // The number of relocations to be created in the output file.
311   size_t output_reloc_count_;
312   // The output data structure associated with this relocation.
313   Output_data* posd_;
314 };
315 
316 // Standard relocation routines which are used on many targets.  Here
317 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
318 
319 template<int size, bool big_endian>
320 class Relocate_functions
321 {
322 private:
323   // Do a simple relocation with the addend in the section contents.
324   // VALSIZE is the size of the value.
325   template<int valsize>
326   static inline void
rel(unsigned char * view,typename elfcpp::Swap<valsize,big_endian>::Valtype value)327   rel(unsigned char* view,
328       typename elfcpp::Swap<valsize, big_endian>::Valtype value)
329   {
330     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
331     Valtype* wv = reinterpret_cast<Valtype*>(view);
332     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
333     elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
334   }
335 
336   // Like the above but for relocs at unaligned addresses.
337   template<int valsize>
338   static inline void
rel_unaligned(unsigned char * view,typename elfcpp::Swap<valsize,big_endian>::Valtype value)339   rel_unaligned(unsigned char* view,
340 	        typename elfcpp::Swap<valsize, big_endian>::Valtype value)
341   {
342     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
343 	Valtype;
344     Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
345     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x + value);
346   }
347 
348   // Do a simple relocation using a Symbol_value with the addend in
349   // the section contents.  VALSIZE is the size of the value to
350   // relocate.
351   template<int valsize>
352   static inline void
rel(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval)353   rel(unsigned char* view,
354       const Sized_relobj_file<size, big_endian>* object,
355       const Symbol_value<size>* psymval)
356   {
357     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
358     Valtype* wv = reinterpret_cast<Valtype*>(view);
359     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
360     x = psymval->value(object, x);
361     elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
362   }
363 
364   // Like the above but for relocs at unaligned addresses.
365   template<int valsize>
366   static inline void
rel_unaligned(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval)367   rel_unaligned(unsigned char* view,
368                 const Sized_relobj_file<size, big_endian>* object,
369                 const Symbol_value<size>* psymval)
370   {
371     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
372         Valtype;
373     Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
374     x = psymval->value(object, x);
375     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x);
376   }
377 
378   // Do a simple relocation with the addend in the relocation.
379   // VALSIZE is the size of the value.
380   template<int valsize>
381   static inline void
rela(unsigned char * view,typename elfcpp::Swap<valsize,big_endian>::Valtype value,typename elfcpp::Swap<valsize,big_endian>::Valtype addend)382   rela(unsigned char* view,
383        typename elfcpp::Swap<valsize, big_endian>::Valtype value,
384        typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
385   {
386     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
387     Valtype* wv = reinterpret_cast<Valtype*>(view);
388     elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
389   }
390 
391   // Do a simple relocation using a symbol value with the addend in
392   // the relocation.  VALSIZE is the size of the value.
393   template<int valsize>
394   static inline void
rela(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Swap<valsize,big_endian>::Valtype addend)395   rela(unsigned char* view,
396        const Sized_relobj_file<size, big_endian>* object,
397        const Symbol_value<size>* psymval,
398        typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
399   {
400     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
401     Valtype* wv = reinterpret_cast<Valtype*>(view);
402     Valtype x = psymval->value(object, addend);
403     elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
404   }
405 
406   // Do a simple PC relative relocation with the addend in the section
407   // contents.  VALSIZE is the size of the value.
408   template<int valsize>
409   static inline void
pcrel(unsigned char * view,typename elfcpp::Swap<valsize,big_endian>::Valtype value,typename elfcpp::Elf_types<size>::Elf_Addr address)410   pcrel(unsigned char* view,
411 	typename elfcpp::Swap<valsize, big_endian>::Valtype value,
412 	typename elfcpp::Elf_types<size>::Elf_Addr address)
413   {
414     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
415     Valtype* wv = reinterpret_cast<Valtype*>(view);
416     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
417     elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
418   }
419 
420   // Like the above but for relocs at unaligned addresses.
421   template<int valsize>
422   static inline void
pcrel_unaligned(unsigned char * view,typename elfcpp::Swap<valsize,big_endian>::Valtype value,typename elfcpp::Elf_types<size>::Elf_Addr address)423   pcrel_unaligned(unsigned char* view,
424 		  typename elfcpp::Swap<valsize, big_endian>::Valtype value,
425 		  typename elfcpp::Elf_types<size>::Elf_Addr address)
426   {
427     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
428     Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
429     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
430 							  x + value - address);
431   }
432 
433   // Do a simple PC relative relocation with a Symbol_value with the
434   // addend in the section contents.  VALSIZE is the size of the
435   // value.
436   template<int valsize>
437   static inline void
pcrel(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr address)438   pcrel(unsigned char* view,
439 	const Sized_relobj_file<size, big_endian>* object,
440 	const Symbol_value<size>* psymval,
441 	typename elfcpp::Elf_types<size>::Elf_Addr address)
442   {
443     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
444     Valtype* wv = reinterpret_cast<Valtype*>(view);
445     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
446     x = psymval->value(object, x);
447     elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
448   }
449 
450   // Do a simple PC relative relocation with the addend in the
451   // relocation.  VALSIZE is the size of the value.
452   template<int valsize>
453   static inline void
pcrela(unsigned char * view,typename elfcpp::Swap<valsize,big_endian>::Valtype value,typename elfcpp::Swap<valsize,big_endian>::Valtype addend,typename elfcpp::Elf_types<size>::Elf_Addr address)454   pcrela(unsigned char* view,
455 	 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
456 	 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
457 	 typename elfcpp::Elf_types<size>::Elf_Addr address)
458   {
459     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
460     Valtype* wv = reinterpret_cast<Valtype*>(view);
461     elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
462   }
463 
464   // Do a simple PC relative relocation with a Symbol_value with the
465   // addend in the relocation.  VALSIZE is the size of the value.
466   template<int valsize>
467   static inline void
pcrela(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Swap<valsize,big_endian>::Valtype addend,typename elfcpp::Elf_types<size>::Elf_Addr address)468   pcrela(unsigned char* view,
469 	 const Sized_relobj_file<size, big_endian>* object,
470 	 const Symbol_value<size>* psymval,
471 	 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
472 	 typename elfcpp::Elf_types<size>::Elf_Addr address)
473   {
474     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
475     Valtype* wv = reinterpret_cast<Valtype*>(view);
476     Valtype x = psymval->value(object, addend);
477     elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
478   }
479 
480   typedef Relocate_functions<size, big_endian> This;
481 
482 public:
483   // Do a simple 8-bit REL relocation with the addend in the section
484   // contents.
485   static inline void
rel8(unsigned char * view,unsigned char value)486   rel8(unsigned char* view, unsigned char value)
487   { This::template rel<8>(view, value); }
488 
489   static inline void
rel8(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval)490   rel8(unsigned char* view,
491        const Sized_relobj_file<size, big_endian>* object,
492        const Symbol_value<size>* psymval)
493   { This::template rel<8>(view, object, psymval); }
494 
495   // Do an 8-bit RELA relocation with the addend in the relocation.
496   static inline void
rela8(unsigned char * view,unsigned char value,unsigned char addend)497   rela8(unsigned char* view, unsigned char value, unsigned char addend)
498   { This::template rela<8>(view, value, addend); }
499 
500   static inline void
rela8(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,unsigned char addend)501   rela8(unsigned char* view,
502 	const Sized_relobj_file<size, big_endian>* object,
503 	const Symbol_value<size>* psymval,
504 	unsigned char addend)
505   { This::template rela<8>(view, object, psymval, addend); }
506 
507   // Do a simple 8-bit PC relative relocation with the addend in the
508   // section contents.
509   static inline void
pcrel8(unsigned char * view,unsigned char value,typename elfcpp::Elf_types<size>::Elf_Addr address)510   pcrel8(unsigned char* view, unsigned char value,
511 	 typename elfcpp::Elf_types<size>::Elf_Addr address)
512   { This::template pcrel<8>(view, value, address); }
513 
514   static inline void
pcrel8(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr address)515   pcrel8(unsigned char* view,
516 	 const Sized_relobj_file<size, big_endian>* object,
517 	 const Symbol_value<size>* psymval,
518 	 typename elfcpp::Elf_types<size>::Elf_Addr address)
519   { This::template pcrel<8>(view, object, psymval, address); }
520 
521   // Do a simple 8-bit PC relative RELA relocation with the addend in
522   // the reloc.
523   static inline void
pcrela8(unsigned char * view,unsigned char value,unsigned char addend,typename elfcpp::Elf_types<size>::Elf_Addr address)524   pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
525 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
526   { This::template pcrela<8>(view, value, addend, address); }
527 
528   static inline void
pcrela8(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,unsigned char addend,typename elfcpp::Elf_types<size>::Elf_Addr address)529   pcrela8(unsigned char* view,
530 	  const Sized_relobj_file<size, big_endian>* object,
531 	  const Symbol_value<size>* psymval,
532 	  unsigned char addend,
533 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
534   { This::template pcrela<8>(view, object, psymval, addend, address); }
535 
536   // Do a simple 16-bit REL relocation with the addend in the section
537   // contents.
538   static inline void
rel16(unsigned char * view,elfcpp::Elf_Half value)539   rel16(unsigned char* view, elfcpp::Elf_Half value)
540   { This::template rel<16>(view, value); }
541 
542   static inline void
rel16(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval)543   rel16(unsigned char* view,
544 	const Sized_relobj_file<size, big_endian>* object,
545 	const Symbol_value<size>* psymval)
546   { This::template rel<16>(view, object, psymval); }
547 
548   // Do an 16-bit RELA relocation with the addend in the relocation.
549   static inline void
rela16(unsigned char * view,elfcpp::Elf_Half value,elfcpp::Elf_Half addend)550   rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
551   { This::template rela<16>(view, value, addend); }
552 
553   static inline void
rela16(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,elfcpp::Elf_Half addend)554   rela16(unsigned char* view,
555 	 const Sized_relobj_file<size, big_endian>* object,
556 	 const Symbol_value<size>* psymval,
557 	 elfcpp::Elf_Half addend)
558   { This::template rela<16>(view, object, psymval, addend); }
559 
560   // Do a simple 16-bit PC relative REL relocation with the addend in
561   // the section contents.
562   static inline void
pcrel16(unsigned char * view,elfcpp::Elf_Half value,typename elfcpp::Elf_types<size>::Elf_Addr address)563   pcrel16(unsigned char* view, elfcpp::Elf_Half value,
564 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
565   { This::template pcrel<16>(view, value, address); }
566 
567   static inline void
pcrel16(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr address)568   pcrel16(unsigned char* view,
569 	  const Sized_relobj_file<size, big_endian>* object,
570 	  const Symbol_value<size>* psymval,
571 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
572   { This::template pcrel<16>(view, object, psymval, address); }
573 
574   // Do a simple 16-bit PC relative RELA relocation with the addend in
575   // the reloc.
576   static inline void
pcrela16(unsigned char * view,elfcpp::Elf_Half value,elfcpp::Elf_Half addend,typename elfcpp::Elf_types<size>::Elf_Addr address)577   pcrela16(unsigned char* view, elfcpp::Elf_Half value,
578 	   elfcpp::Elf_Half addend,
579            typename elfcpp::Elf_types<size>::Elf_Addr address)
580   { This::template pcrela<16>(view, value, addend, address); }
581 
582   static inline void
pcrela16(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,elfcpp::Elf_Half addend,typename elfcpp::Elf_types<size>::Elf_Addr address)583   pcrela16(unsigned char* view,
584 	   const Sized_relobj_file<size, big_endian>* object,
585 	   const Symbol_value<size>* psymval,
586 	   elfcpp::Elf_Half addend,
587 	   typename elfcpp::Elf_types<size>::Elf_Addr address)
588   { This::template pcrela<16>(view, object, psymval, addend, address); }
589 
590   // Do a simple 32-bit REL relocation with the addend in the section
591   // contents.
592   static inline void
rel32(unsigned char * view,elfcpp::Elf_Word value)593   rel32(unsigned char* view, elfcpp::Elf_Word value)
594   { This::template rel<32>(view, value); }
595 
596   // Like above but for relocs at unaligned addresses.
597   static inline void
rel32_unaligned(unsigned char * view,elfcpp::Elf_Word value)598   rel32_unaligned(unsigned char* view, elfcpp::Elf_Word value)
599   { This::template rel_unaligned<32>(view, value); }
600 
601   static inline void
rel32(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval)602   rel32(unsigned char* view,
603 	const Sized_relobj_file<size, big_endian>* object,
604 	const Symbol_value<size>* psymval)
605   { This::template rel<32>(view, object, psymval); }
606 
607   // Like above but for relocs at unaligned addresses.
608   static inline void
rel32_unaligned(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval)609   rel32_unaligned(unsigned char* view,
610 	          const Sized_relobj_file<size, big_endian>* object,
611 	          const Symbol_value<size>* psymval)
612   { This::template rel_unaligned<32>(view, object, psymval); }
613 
614   // Do an 32-bit RELA relocation with the addend in the relocation.
615   static inline void
rela32(unsigned char * view,elfcpp::Elf_Word value,elfcpp::Elf_Word addend)616   rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
617   { This::template rela<32>(view, value, addend); }
618 
619   static inline void
rela32(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,elfcpp::Elf_Word addend)620   rela32(unsigned char* view,
621 	 const Sized_relobj_file<size, big_endian>* object,
622 	 const Symbol_value<size>* psymval,
623 	 elfcpp::Elf_Word addend)
624   { This::template rela<32>(view, object, psymval, addend); }
625 
626   // Do a simple 32-bit PC relative REL relocation with the addend in
627   // the section contents.
628   static inline void
pcrel32(unsigned char * view,elfcpp::Elf_Word value,typename elfcpp::Elf_types<size>::Elf_Addr address)629   pcrel32(unsigned char* view, elfcpp::Elf_Word value,
630 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
631   { This::template pcrel<32>(view, value, address); }
632 
633   // Unaligned version of the above.
634   static inline void
pcrel32_unaligned(unsigned char * view,elfcpp::Elf_Word value,typename elfcpp::Elf_types<size>::Elf_Addr address)635   pcrel32_unaligned(unsigned char* view, elfcpp::Elf_Word value,
636 		    typename elfcpp::Elf_types<size>::Elf_Addr address)
637   { This::template pcrel_unaligned<32>(view, value, address); }
638 
639   static inline void
pcrel32(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr address)640   pcrel32(unsigned char* view,
641 	  const Sized_relobj_file<size, big_endian>* object,
642 	  const Symbol_value<size>* psymval,
643 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
644   { This::template pcrel<32>(view, object, psymval, address); }
645 
646   // Do a simple 32-bit PC relative RELA relocation with the addend in
647   // the relocation.
648   static inline void
pcrela32(unsigned char * view,elfcpp::Elf_Word value,elfcpp::Elf_Word addend,typename elfcpp::Elf_types<size>::Elf_Addr address)649   pcrela32(unsigned char* view, elfcpp::Elf_Word value,
650            elfcpp::Elf_Word addend,
651            typename elfcpp::Elf_types<size>::Elf_Addr address)
652   { This::template pcrela<32>(view, value, addend, address); }
653 
654   static inline void
pcrela32(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,elfcpp::Elf_Word addend,typename elfcpp::Elf_types<size>::Elf_Addr address)655   pcrela32(unsigned char* view,
656 	   const Sized_relobj_file<size, big_endian>* object,
657 	   const Symbol_value<size>* psymval,
658 	   elfcpp::Elf_Word addend,
659 	   typename elfcpp::Elf_types<size>::Elf_Addr address)
660   { This::template pcrela<32>(view, object, psymval, addend, address); }
661 
662   // Do a simple 64-bit REL relocation with the addend in the section
663   // contents.
664   static inline void
rel64(unsigned char * view,elfcpp::Elf_Xword value)665   rel64(unsigned char* view, elfcpp::Elf_Xword value)
666   { This::template rel<64>(view, value); }
667 
668   static inline void
rel64(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval)669   rel64(unsigned char* view,
670 	const Sized_relobj_file<size, big_endian>* object,
671 	const Symbol_value<size>* psymval)
672   { This::template rel<64>(view, object, psymval); }
673 
674   // Do a 64-bit RELA relocation with the addend in the relocation.
675   static inline void
rela64(unsigned char * view,elfcpp::Elf_Xword value,elfcpp::Elf_Xword addend)676   rela64(unsigned char* view, elfcpp::Elf_Xword value,
677          elfcpp::Elf_Xword addend)
678   { This::template rela<64>(view, value, addend); }
679 
680   static inline void
rela64(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,elfcpp::Elf_Xword addend)681   rela64(unsigned char* view,
682 	 const Sized_relobj_file<size, big_endian>* object,
683 	 const Symbol_value<size>* psymval,
684 	 elfcpp::Elf_Xword addend)
685   { This::template rela<64>(view, object, psymval, addend); }
686 
687   // Do a simple 64-bit PC relative REL relocation with the addend in
688   // the section contents.
689   static inline void
pcrel64(unsigned char * view,elfcpp::Elf_Xword value,typename elfcpp::Elf_types<size>::Elf_Addr address)690   pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
691 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
692   { This::template pcrel<64>(view, value, address); }
693 
694   static inline void
pcrel64(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr address)695   pcrel64(unsigned char* view,
696 	  const Sized_relobj_file<size, big_endian>* object,
697 	  const Symbol_value<size>* psymval,
698 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
699   { This::template pcrel<64>(view, object, psymval, address); }
700 
701   // Do a simple 64-bit PC relative RELA relocation with the addend in
702   // the relocation.
703   static inline void
pcrela64(unsigned char * view,elfcpp::Elf_Xword value,elfcpp::Elf_Xword addend,typename elfcpp::Elf_types<size>::Elf_Addr address)704   pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
705            elfcpp::Elf_Xword addend,
706            typename elfcpp::Elf_types<size>::Elf_Addr address)
707   { This::template pcrela<64>(view, value, addend, address); }
708 
709   static inline void
pcrela64(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,elfcpp::Elf_Xword addend,typename elfcpp::Elf_types<size>::Elf_Addr address)710   pcrela64(unsigned char* view,
711 	   const Sized_relobj_file<size, big_endian>* object,
712 	   const Symbol_value<size>* psymval,
713 	   elfcpp::Elf_Xword addend,
714 	   typename elfcpp::Elf_types<size>::Elf_Addr address)
715   { This::template pcrela<64>(view, object, psymval, addend, address); }
716 };
717 
718 // Integer manipulation functions used by various targets when
719 // performing relocations.
720 
721 template<int bits>
722 class Bits
723 {
724  public:
725   // Sign extend an n-bit unsigned integer stored in a uint32_t into
726   // an int32_t.  BITS must be between 1 and 32.
727   static inline int32_t
sign_extend32(uint32_t val)728   sign_extend32(uint32_t val)
729   {
730     gold_assert(bits > 0 && bits <= 32);
731     if (bits == 32)
732       return static_cast<int32_t>(val);
733     uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - bits);
734     val &= mask;
735     uint32_t top_bit = 1U << (bits - 1);
736     int32_t as_signed = static_cast<int32_t>(val);
737     if ((val & top_bit) != 0)
738       as_signed -= static_cast<int32_t>(top_bit * 2);
739     return as_signed;
740   }
741 
742   // Return true if VAL (stored in a uint32_t) has overflowed a signed
743   // value with BITS bits.
744   static inline bool
has_overflow32(uint32_t val)745   has_overflow32(uint32_t val)
746   {
747     gold_assert(bits > 0 && bits <= 32);
748     if (bits == 32)
749       return false;
750     int32_t max = (1 << (bits - 1)) - 1;
751     int32_t min = -(1 << (bits - 1));
752     int32_t as_signed = static_cast<int32_t>(val);
753     return as_signed > max || as_signed < min;
754   }
755 
756   // Return true if VAL (stored in a uint32_t) has overflowed both a
757   // signed and an unsigned value.  E.g.,
758   // Bits<8>::has_signed_unsigned_overflow32 would check -128 <= VAL <
759   // 255.
760   static inline bool
has_signed_unsigned_overflow32(uint32_t val)761   has_signed_unsigned_overflow32(uint32_t val)
762   {
763     gold_assert(bits > 0 && bits <= 32);
764     if (bits == 32)
765       return false;
766     int32_t max = static_cast<int32_t>((1U << bits) - 1);
767     int32_t min = -(1 << (bits - 1));
768     int32_t as_signed = static_cast<int32_t>(val);
769     return as_signed > max || as_signed < min;
770   }
771 
772   // Select bits from A and B using bits in MASK.  For each n in
773   // [0..31], the n-th bit in the result is chosen from the n-th bits
774   // of A and B.  A zero selects A and a one selects B.
775   static inline uint32_t
bit_select32(uint32_t a,uint32_t b,uint32_t mask)776   bit_select32(uint32_t a, uint32_t b, uint32_t mask)
777   { return (a & ~mask) | (b & mask); }
778 
779   // Sign extend an n-bit unsigned integer stored in a uint64_t into
780   // an int64_t.  BITS must be between 1 and 64.
781   static inline int64_t
sign_extend(uint64_t val)782   sign_extend(uint64_t val)
783   {
784     gold_assert(bits > 0 && bits <= 64);
785     if (bits == 64)
786       return static_cast<int64_t>(val);
787     uint64_t mask = (~static_cast<uint64_t>(0)) >> (64 - bits);
788     val &= mask;
789     uint64_t top_bit = static_cast<uint64_t>(1) << (bits - 1);
790     int64_t as_signed = static_cast<int64_t>(val);
791     if ((val & top_bit) != 0)
792       as_signed -= static_cast<int64_t>(top_bit * 2);
793     return as_signed;
794   }
795 
796   // Return true if VAL (stored in a uint64_t) has overflowed a signed
797   // value with BITS bits.
798   static inline bool
has_overflow(uint64_t val)799   has_overflow(uint64_t val)
800   {
801     gold_assert(bits > 0 && bits <= 64);
802     if (bits == 64)
803       return false;
804     int64_t max = (static_cast<int64_t>(1) << (bits - 1)) - 1;
805     int64_t min = -(static_cast<int64_t>(1) << (bits - 1));
806     int64_t as_signed = static_cast<int64_t>(val);
807     return as_signed > max || as_signed < min;
808   }
809 
810   // Return true if VAL (stored in a uint64_t) has overflowed both a
811   // signed and an unsigned value.  E.g.,
812   // Bits<8>::has_signed_unsigned_overflow would check -128 <= VAL <
813   // 255.
814   static inline bool
has_signed_unsigned_overflow64(uint64_t val)815   has_signed_unsigned_overflow64(uint64_t val)
816   {
817     gold_assert(bits > 0 && bits <= 64);
818     if (bits == 64)
819       return false;
820     int64_t max = static_cast<int64_t>((static_cast<uint64_t>(1) << bits) - 1);
821     int64_t min = -(static_cast<int64_t>(1) << (bits - 1));
822     int64_t as_signed = static_cast<int64_t>(val);
823     return as_signed > max || as_signed < min;
824   }
825 
826   // Select bits from A and B using bits in MASK.  For each n in
827   // [0..31], the n-th bit in the result is chosen from the n-th bits
828   // of A and B.  A zero selects A and a one selects B.
829   static inline uint64_t
bit_select64(uint64_t a,uint64_t b,uint64_t mask)830   bit_select64(uint64_t a, uint64_t b, uint64_t mask)
831   { return (a & ~mask) | (b & mask); }
832 };
833 
834 // Track relocations while reading a section.  This lets you ask for
835 // the relocation at a certain offset, and see how relocs occur
836 // between points of interest.
837 
838 template<int size, bool big_endian>
839 class Track_relocs
840 {
841  public:
Track_relocs()842   Track_relocs()
843     : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
844   { }
845 
846   // Initialize the Track_relocs object.  OBJECT is the object holding
847   // the reloc section, RELOC_SHNDX is the section index of the reloc
848   // section, and RELOC_TYPE is the type of the reloc section
849   // (elfcpp::SHT_REL or elfcpp::SHT_RELA).  This returns false if
850   // something went wrong.
851   bool
852   initialize(Object* object, unsigned int reloc_shndx,
853 	     unsigned int reloc_type);
854 
855   // Return the offset in the data section to which the next reloc
856   // applies.  This returns -1 if there is no next reloc.
857   off_t
858   next_offset() const;
859 
860   // Return the symbol index of the next reloc.  This returns -1U if
861   // there is no next reloc.
862   unsigned int
863   next_symndx() const;
864 
865   // Return the addend of the next reloc.  This returns 0 if there is
866   // no next reloc.
867   uint64_t
868   next_addend() const;
869 
870   // Advance to OFFSET within the data section, and return the number
871   // of relocs which would be skipped.
872   int
873   advance(off_t offset);
874 
875   // Checkpoint the current position in the reloc section.
876   section_size_type
checkpoint()877   checkpoint() const
878   { return this->pos_; }
879 
880   // Reset the position to CHECKPOINT.
881   void
reset(section_size_type checkpoint)882   reset(section_size_type checkpoint)
883   { this->pos_ = checkpoint; }
884 
885  private:
886   // The contents of the input object's reloc section.
887   const unsigned char* prelocs_;
888   // The length of the reloc section.
889   section_size_type len_;
890   // Our current position in the reloc section.
891   section_size_type pos_;
892   // The size of the relocs in the section.
893   int reloc_size_;
894 };
895 
896 } // End namespace gold.
897 
898 #endif // !defined(GOLD_RELOC_H)
899