Add Signed_valtype and use it for sign extension. Fix names of rela8.
[external/binutils.git] / gold / reloc.h
1 // reloc.h -- relocate input files for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007 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 <byteswap.h>
27
28 #include "workqueue.h"
29
30 namespace gold
31 {
32
33 class General_options;
34 class Relobj;
35 class Read_relocs_data;
36 class Symbol;
37 class Layout;
38
39 template<int size>
40 class Sized_symbol;
41
42 template<int size, bool big_endian>
43 class Sized_relobj;
44
45 template<int size>
46 class Symbol_value;
47
48 template<int sh_type, bool dynamic, int size, bool big_endian>
49 class Output_data_reloc;
50
51 // A class to read the relocations for an object file, and then queue
52 // up a task to see if they require any GOT/PLT/COPY relocations in
53 // the symbol table.
54
55 class Read_relocs : public Task
56 {
57  public:
58   // SYMTAB_LOCK is used to lock the symbol table.  BLOCKER should be
59   // unblocked when the Scan_relocs task completes.
60   Read_relocs(const General_options& options, Symbol_table* symtab,
61               Layout* layout, Relobj* object, Task_token* symtab_lock,
62               Task_token* blocker)
63     : options_(options), symtab_(symtab), layout_(layout), object_(object),
64       symtab_lock_(symtab_lock), blocker_(blocker)
65   { }
66
67   // The standard Task methods.
68
69   Is_runnable_type
70   is_runnable(Workqueue*);
71
72   Task_locker*
73   locks(Workqueue*);
74
75   void
76   run(Workqueue*);
77
78  private:
79   const General_options& options_;
80   Symbol_table* symtab_;
81   Layout* layout_;
82   Relobj* object_;
83   Task_token* symtab_lock_;
84   Task_token* blocker_;
85 };
86
87 // Scan the relocations for an object to see if they require any
88 // GOT/PLT/COPY relocations.
89
90 class Scan_relocs : public Task
91 {
92  public:
93   // SYMTAB_LOCK is used to lock the symbol table.  BLOCKER should be
94   // unblocked when the task completes.
95   Scan_relocs(const General_options& options, Symbol_table* symtab,
96               Layout* layout, Relobj* object, Read_relocs_data* rd,
97               Task_token* symtab_lock, Task_token* blocker)
98     : options_(options), symtab_(symtab), layout_(layout), object_(object),
99       rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker)
100   { }
101
102   // The standard Task methods.
103
104   Is_runnable_type
105   is_runnable(Workqueue*);
106
107   Task_locker*
108   locks(Workqueue*);
109
110   void
111   run(Workqueue*);
112
113  private:
114   class Scan_relocs_locker;
115
116   const General_options& options_;
117   Symbol_table* symtab_;
118   Layout* layout_;
119   Relobj* object_;
120   Read_relocs_data* rd_;
121   Task_token* symtab_lock_;
122   Task_token* blocker_;
123 };
124
125 // A class to perform all the relocations for an object file.
126
127 class Relocate_task : public Task
128 {
129  public:
130   Relocate_task(const General_options& options, const Symbol_table* symtab,
131                 const Layout* layout, Relobj* object, Output_file* of,
132                 Task_token* final_blocker)
133     : options_(options), symtab_(symtab), layout_(layout), object_(object),
134       of_(of), final_blocker_(final_blocker)
135   { }
136
137   // The standard Task methods.
138
139   Is_runnable_type
140   is_runnable(Workqueue*);
141
142   Task_locker*
143   locks(Workqueue*);
144
145   void
146   run(Workqueue*);
147
148  private:
149   class Relocate_locker;
150
151   const General_options& options_;
152   const Symbol_table* symtab_;
153   const Layout* layout_;
154   Relobj* object_;
155   Output_file* of_;
156   Task_token* final_blocker_;
157 };
158
159 // Standard relocation routines which are used on many targets.  Here
160 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
161
162 template<int size, bool big_endian>
163 class Relocate_functions
164 {
165 private:
166   // Do a simple relocation with the addend in the section contents.
167   // VALSIZE is the size of the value.
168   template<int valsize>
169   static inline void
170   rel(unsigned char* view,
171       typename elfcpp::Swap<valsize, big_endian>::Valtype value)
172   {
173     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
174     Valtype* wv = reinterpret_cast<Valtype*>(view);
175     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
176     elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
177   }
178
179   // Do a simple relocation using a Symbol_value with the addend in
180   // the section contents.  VALSIZE is the size of the value to
181   // relocate.
182   template<int valsize>
183   static inline void
184   rel(unsigned char* view,
185       const Sized_relobj<size, big_endian>* object,
186       const Symbol_value<size>* psymval)
187   {
188     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
189     Valtype* wv = reinterpret_cast<Valtype*>(view);
190     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
191     x = psymval->value(object, x);
192     elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
193   }
194
195   // Do a simple relocation with the addend in the relocation.
196   // VALSIZE is the size of the value.
197   template<int valsize>
198   static inline void
199   rela(unsigned char* view,
200        typename elfcpp::Swap<valsize, big_endian>::Valtype value,
201        typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
202   {
203     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
204     Valtype* wv = reinterpret_cast<Valtype*>(view);
205     elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
206   }
207
208   // Do a simple relocation using a symbol value with the addend in
209   // the relocation.  VALSIZE is the size of the value.
210   template<int valsize>
211   static inline void
212   rela(unsigned char* view,
213        const Sized_relobj<size, big_endian>* object,
214        const Symbol_value<size>* psymval,
215        typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
216   {
217     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
218     Valtype* wv = reinterpret_cast<Valtype*>(view);
219     Valtype x = psymval->value(object, addend);
220     elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
221   }
222
223   // Like rel(), but sign-extends the value to SIZE.
224   template<int valsize>
225   static inline void
226   signedrel(unsigned char* view,
227             const Sized_relobj<size, big_endian>* object,
228             const Symbol_value<size>* psymval)
229   {
230     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
231     typedef typename elfcpp::Swap<valsize, big_endian>::Signed_valtype
232       Signed_valtype;
233     typedef typename elfcpp::Swap<size, big_endian>::Valtype Sizetype;
234     typedef typename elfcpp::Swap<size, big_endian>::Signed_valtype
235       Signed_sizetype;
236     Valtype* wv = reinterpret_cast<Valtype*>(view);
237     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
238
239     // Sign extend the value.
240     Signed_valtype signed_x = static_cast<Signed_valtype>(x);
241     Signed_sizetype signed_extended_x = static_cast<Signed_sizetype>(signed_x);
242     Sizetype unsigned_extended_x = static_cast<Sizetype>(signed_extended_x);
243
244     x = psymval->value(object, unsigned_extended_x);
245     elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
246   }
247
248   // Do a simple PC relative relocation with the addend in the section
249   // contents.  VALSIZE is the size of the value.
250   template<int valsize>
251   static inline void
252   pcrel(unsigned char* view,
253         typename elfcpp::Swap<valsize, big_endian>::Valtype value,
254         typename elfcpp::Elf_types<size>::Elf_Addr address)
255   {
256     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
257     Valtype* wv = reinterpret_cast<Valtype*>(view);
258     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
259     elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
260   }
261
262   // Do a simple PC relative relocation with a Symbol_value with the
263   // addend in the section contents.  VALSIZE is the size of the
264   // value.
265   template<int valsize>
266   static inline void
267   pcrel(unsigned char* view,
268         const Sized_relobj<size, big_endian>* object,
269         const Symbol_value<size>* psymval,
270         typename elfcpp::Elf_types<size>::Elf_Addr address)
271   {
272     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
273     Valtype* wv = reinterpret_cast<Valtype*>(view);
274     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
275     x = psymval->value(object, x);
276     elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
277   }
278
279   // Do a simple PC relative relocation with the addend in the
280   // relocation.  VALSIZE is the size of the value.
281   template<int valsize>
282   static inline void
283   pcrela(unsigned char* view,
284          typename elfcpp::Swap<valsize, big_endian>::Valtype value,
285          typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
286          typename elfcpp::Elf_types<size>::Elf_Addr address)
287   {
288     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
289     Valtype* wv = reinterpret_cast<Valtype*>(view);
290     elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
291   }
292
293   // Do a simple PC relative relocation with a Symbol_value with the
294   // addend in the relocation.  VALSIZE is the size of the value.
295   template<int valsize>
296   static inline void
297   pcrela(unsigned char* view,
298          const Sized_relobj<size, big_endian>* object,
299          const Symbol_value<size>* psymval,
300          typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
301          typename elfcpp::Elf_types<size>::Elf_Addr address)
302   {
303     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
304     Valtype* wv = reinterpret_cast<Valtype*>(view);
305     Valtype x = psymval->value(object, addend);
306     elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
307   }
308
309   typedef Relocate_functions<size, big_endian> This;
310
311 public:
312   // Do a simple 8-bit REL relocation with the addend in the section
313   // contents.
314   static inline void
315   rel8(unsigned char* view, unsigned char value)
316   { This::template rel<8>(view, value); }
317
318   static inline void
319   rel8(unsigned char* view,
320        const Sized_relobj<size, big_endian>* object,
321        const Symbol_value<size>* psymval)
322   { This::template rel<8>(view, object, psymval); }
323
324   // Do an 8-bit RELA relocation with the addend in the relocation.
325   static inline void
326   rela8(unsigned char* view, unsigned char value, unsigned char addend)
327   { This::template rela<8>(view, value, addend); }
328
329   static inline void
330   rela8(unsigned char* view,
331         const Sized_relobj<size, big_endian>* object,
332         const Symbol_value<size>* psymval,
333         unsigned char addend)
334   { This::template rela<8>(view, object, psymval, addend); }
335
336   // Do an 8-bit REL relocation, sign extending the addend to SIZE.
337   static inline void
338   rel8s(unsigned char* view,
339          const Sized_relobj<size, big_endian>* object,
340          const Symbol_value<size>* psymval)
341   { This::template signedrel<8>(view, object, psymval); }
342
343   // Do a simple 8-bit PC relative relocation with the addend in the
344   // section contents.
345   static inline void
346   pcrel8(unsigned char* view, unsigned char value,
347          typename elfcpp::Elf_types<size>::Elf_Addr address)
348   { This::template pcrel<8>(view, value, address); }
349
350   static inline void
351   pcrel8(unsigned char* view,
352          const Sized_relobj<size, big_endian>* object,
353          const Symbol_value<size>* psymval,
354          typename elfcpp::Elf_types<size>::Elf_Addr address)
355   { This::template pcrel<8>(view, object, psymval, address); }
356
357   // Do a simple 8-bit PC relative RELA relocation with the addend in
358   // the reloc.
359   static inline void
360   pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
361           typename elfcpp::Elf_types<size>::Elf_Addr address)
362   { This::template pcrela<8>(view, value, addend, address); }
363
364   static inline void
365   pcrela8(unsigned char* view,
366           const Sized_relobj<size, big_endian>* object,
367           const Symbol_value<size>* psymval,
368           unsigned char addend,
369           typename elfcpp::Elf_types<size>::Elf_Addr address)
370   { This::template pcrela<8>(view, object, psymval, addend, address); }
371
372   // Do a simple 16-bit REL relocation with the addend in the section
373   // contents.
374   static inline void
375   rel16(unsigned char* view, elfcpp::Elf_Half value)
376   { This::template rel<16>(view, value); }
377
378   static inline void
379   rel16(unsigned char* view,
380         const Sized_relobj<size, big_endian>* object,
381         const Symbol_value<size>* psymval)
382   { This::template rel<16>(view, object, psymval); }
383
384   // Do an 16-bit RELA relocation with the addend in the relocation.
385   static inline void
386   rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
387   { This::template rela<16>(view, value, addend); }
388
389   static inline void
390   rela16(unsigned char* view,
391          const Sized_relobj<size, big_endian>* object,
392          const Symbol_value<size>* psymval,
393          elfcpp::Elf_Half addend)
394   { This::template rela<16>(view, object, psymval, addend); }
395
396   // Do a 16-bit REL relocation, sign extending the addend to SIZE.
397   static inline void
398   rel16s(unsigned char* view,
399          const Sized_relobj<size, big_endian>* object,
400          const Symbol_value<size>* psymval)
401   { This::template signedrel<16>(view, object, psymval); }
402
403   // Do a simple 16-bit PC relative REL relocation with the addend in
404   // the section contents.
405   static inline void
406   pcrel16(unsigned char* view, elfcpp::Elf_Half value,
407           typename elfcpp::Elf_types<size>::Elf_Addr address)
408   { This::template pcrel<16>(view, value, address); }
409
410   static inline void
411   pcrel16(unsigned char* view,
412           const Sized_relobj<size, big_endian>* object,
413           const Symbol_value<size>* psymval,
414           typename elfcpp::Elf_types<size>::Elf_Addr address)
415   { This::template pcrel<16>(view, object, psymval, address); }
416
417   // Do a simple 16-bit PC relative RELA relocation with the addend in
418   // the reloc.
419   static inline void
420   pcrela16(unsigned char* view, elfcpp::Elf_Half value,
421            elfcpp::Elf_Half addend,
422            typename elfcpp::Elf_types<size>::Elf_Addr address)
423   { This::template pcrela<16>(view, value, addend, address); }
424
425   static inline void
426   pcrela16(unsigned char* view,
427            const Sized_relobj<size, big_endian>* object,
428            const Symbol_value<size>* psymval,
429            elfcpp::Elf_Half addend,
430            typename elfcpp::Elf_types<size>::Elf_Addr address)
431   { This::template pcrela<16>(view, object, psymval, addend, address); }
432
433   // Do a simple 32-bit REL relocation with the addend in the section
434   // contents.
435   static inline void
436   rel32(unsigned char* view, elfcpp::Elf_Word value)
437   { This::template rel<32>(view, value); }
438
439   static inline void
440   rel32(unsigned char* view,
441         const Sized_relobj<size, big_endian>* object,
442         const Symbol_value<size>* psymval)
443   { This::template rel<32>(view, object, psymval); }
444
445   // Do an 32-bit RELA relocation with the addend in the relocation.
446   static inline void
447   rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
448   { This::template rela<32>(view, value, addend); }
449
450   static inline void
451   rela32(unsigned char* view,
452          const Sized_relobj<size, big_endian>* object,
453          const Symbol_value<size>* psymval,
454          elfcpp::Elf_Word addend)
455   { This::template rela<32>(view, object, psymval, addend); }
456
457   // Do a 32-bit REL relocation, sign extending the addend to SIZE.
458   static inline void
459   rel32s(unsigned char* view,
460          const Sized_relobj<size, big_endian>* object,
461          const Symbol_value<size>* psymval)
462   { This::template signedrel<32>(view, object, psymval); }
463
464   // Do a simple 32-bit PC relative REL relocation with the addend in
465   // the section contents.
466   static inline void
467   pcrel32(unsigned char* view, elfcpp::Elf_Word value,
468           typename elfcpp::Elf_types<size>::Elf_Addr address)
469   { This::template pcrel<32>(view, value, address); }
470
471   static inline void
472   pcrel32(unsigned char* view,
473           const Sized_relobj<size, big_endian>* object,
474           const Symbol_value<size>* psymval,
475           typename elfcpp::Elf_types<size>::Elf_Addr address)
476   { This::template pcrel<32>(view, object, psymval, address); }
477
478   // Do a simple 32-bit PC relative RELA relocation with the addend in
479   // the relocation.
480   static inline void
481   pcrela32(unsigned char* view, elfcpp::Elf_Word value,
482            elfcpp::Elf_Word addend,
483            typename elfcpp::Elf_types<size>::Elf_Addr address)
484   { This::template pcrela<32>(view, value, addend, address); }
485
486   static inline void
487   pcrela32(unsigned char* view,
488            const Sized_relobj<size, big_endian>* object,
489            const Symbol_value<size>* psymval,
490            elfcpp::Elf_Word addend,
491            typename elfcpp::Elf_types<size>::Elf_Addr address)
492   { This::template pcrela<32>(view, object, psymval, addend, address); }
493
494   // Do a simple 64-bit REL relocation with the addend in the section
495   // contents.
496   static inline void
497   rel64(unsigned char* view, elfcpp::Elf_Xword value)
498   { This::template rel<64>(view, value); }
499
500   static inline void
501   rel64(unsigned char* view,
502         const Sized_relobj<size, big_endian>* object,
503         const Symbol_value<size>* psymval)
504   { This::template rel<64>(view, object, psymval); }
505
506   // Do a 64-bit RELA relocation with the addend in the relocation.
507   static inline void
508   rela64(unsigned char* view, elfcpp::Elf_Xword value,
509          elfcpp::Elf_Xword addend)
510   { This::template rela<64>(view, value, addend); }
511
512   static inline void
513   rela64(unsigned char* view,
514          const Sized_relobj<size, big_endian>* object,
515          const Symbol_value<size>* psymval,
516          elfcpp::Elf_Xword addend)
517   { This::template rela<64>(view, object, psymval, addend); }
518
519   // Do a simple 64-bit PC relative REL relocation with the addend in
520   // the section contents.
521   static inline void
522   pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
523           typename elfcpp::Elf_types<size>::Elf_Addr address)
524   { This::template pcrel<64>(view, value, address); }
525
526   static inline void
527   pcrel64(unsigned char* view,
528           const Sized_relobj<size, big_endian>* object,
529           const Symbol_value<size>* psymval,
530           typename elfcpp::Elf_types<size>::Elf_Addr address)
531   { This::template pcrel<64>(view, object, psymval, address); }
532
533   // Do a simple 64-bit PC relative RELA relocation with the addend in
534   // the relocation.
535   static inline void
536   pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
537            elfcpp::Elf_Xword addend,
538            typename elfcpp::Elf_types<size>::Elf_Addr address)
539   { This::template pcrela<64>(view, value, addend, address); }
540
541   static inline void
542   pcrela64(unsigned char* view,
543            const Sized_relobj<size, big_endian>* object,
544            const Symbol_value<size>* psymval,
545            elfcpp::Elf_Xword addend,
546            typename elfcpp::Elf_types<size>::Elf_Addr address)
547   { This::template pcrela<64>(view, object, psymval, addend, address); }
548 };
549
550 // We try to avoid COPY relocations when possible.  A COPY relocation
551 // may be required when an executable refers to a variable defined in
552 // a shared library.  COPY relocations are problematic because they
553 // tie the executable to the exact size of the variable in the shared
554 // library.  We can avoid them if all the references to the variable
555 // are in a writeable section.  In that case we can simply use dynamic
556 // relocations.  However, when scanning relocs, we don't know when we
557 // see the relocation whether we will be forced to use a COPY
558 // relocation or not.  So we have to save the relocation during the
559 // reloc scanning, and then emit it as a dynamic relocation if
560 // necessary.  This class implements that.  It is used by the target
561 // specific code.
562
563 template<int size, bool big_endian>
564 class Copy_relocs
565 {
566  public:
567   Copy_relocs()
568     : entries_()
569   { }
570
571   // Return whether we need a COPY reloc for a reloc against GSYM,
572   // which is being applied to section SHNDX in OBJECT.
573   static bool
574   need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
575                   Sized_symbol<size>* gsym);
576
577   // Save a Rel against SYM for possible emission later.  SHNDX is the
578   // index of the section to which the reloc is being applied.
579   void
580   save(Symbol* sym, Relobj*, unsigned int shndx,
581        const elfcpp::Rel<size, big_endian>&);
582
583   // Save a Rela against SYM for possible emission later.
584   void
585   save(Symbol* sym, Relobj*, unsigned int shndx,
586        const elfcpp::Rela<size, big_endian>&);
587
588   // Return whether there are any relocs to emit.  This also discards
589   // entries which need not be emitted.
590   bool
591   any_to_emit();
592
593   // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
594   // is still defined in the dynamic object).
595   template<int sh_type>
596   void
597   emit(Output_data_reloc<sh_type, true, size, big_endian>*);
598
599  private:
600   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
601   typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
602
603   // This POD class holds the entries we are saving.
604   class Copy_reloc_entry
605   {
606    public:
607     Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
608                      Relobj* relobj, unsigned int shndx,
609                      Address address, Addend addend)
610       : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
611         shndx_(shndx), address_(address), addend_(addend)
612     { }
613
614     // Return whether we should emit this reloc.  If we should not
615     // emit, we clear it.
616     bool
617     should_emit();
618
619     // Emit this reloc.
620
621     void
622     emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
623
624     void
625     emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
626
627    private:
628     Symbol* sym_;
629     unsigned int reloc_type_;
630     Relobj* relobj_;
631     unsigned int shndx_;
632     Address address_;
633     Addend addend_;
634   };
635
636   // A list of relocs to be saved.
637   typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
638
639   // The list of relocs we are saving.
640   Copy_reloc_entries entries_;
641 };
642
643 } // End namespace gold.
644
645 #endif // !defined(GOLD_RELOC_H)