Add RELA versions of the relocation routines. Original patch from
[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<size, big_endian>::Valtype Sizetype;
232     Valtype* wv = reinterpret_cast<Valtype*>(view);
233     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
234     // Fancy formula to sign-extend x to size.
235     const Sizetype mask = 1U << (sizeof(valsize) * 8 - 1);
236     Sizetype sign_extended_x = x;
237     sign_extended_x = (sign_extended_x ^ mask) - mask;
238     x = psymval->value(object, sign_extended_x);
239     elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
240   }
241
242   // Do a simple PC relative relocation with the addend in the section
243   // contents.  VALSIZE is the size of the value.
244   template<int valsize>
245   static inline void
246   pcrel(unsigned char* view,
247         typename elfcpp::Swap<valsize, big_endian>::Valtype value,
248         typename elfcpp::Elf_types<size>::Elf_Addr address)
249   {
250     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
251     Valtype* wv = reinterpret_cast<Valtype*>(view);
252     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
253     elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
254   }
255
256   // Do a simple PC relative relocation with a Symbol_value with the
257   // addend in the section contents.  VALSIZE is the size of the
258   // value.
259   template<int valsize>
260   static inline void
261   pcrel(unsigned char* view,
262         const Sized_relobj<size, big_endian>* object,
263         const Symbol_value<size>* psymval,
264         typename elfcpp::Elf_types<size>::Elf_Addr address)
265   {
266     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
267     Valtype* wv = reinterpret_cast<Valtype*>(view);
268     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
269     x = psymval->value(object, x);
270     elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
271   }
272
273   // Do a simple PC relative relocation with the addend in the
274   // relocation.  VALSIZE is the size of the value.
275   template<int valsize>
276   static inline void
277   pcrela(unsigned char* view,
278          typename elfcpp::Swap<valsize, big_endian>::Valtype value,
279          typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
280          typename elfcpp::Elf_types<size>::Elf_Addr address)
281   {
282     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
283     Valtype* wv = reinterpret_cast<Valtype*>(view);
284     elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
285   }
286
287   // Do a simple PC relative relocation with a Symbol_value with the
288   // addend in the relocation.  VALSIZE is the size of the value.
289   template<int valsize>
290   static inline void
291   pcrela(unsigned char* view,
292          const Sized_relobj<size, big_endian>* object,
293          const Symbol_value<size>* psymval,
294          typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
295          typename elfcpp::Elf_types<size>::Elf_Addr address)
296   {
297     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
298     Valtype* wv = reinterpret_cast<Valtype*>(view);
299     Valtype x = psymval->value(object, addend);
300     elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
301   }
302
303   typedef Relocate_functions<size, big_endian> This;
304
305 public:
306   // Do a simple 8-bit REL relocation with the addend in the section
307   // contents.
308   static inline void
309   rel8(unsigned char* view, unsigned char value)
310   { This::template rel<8>(view, value); }
311
312   static inline void
313   rel8(unsigned char* view,
314        const Sized_relobj<size, big_endian>* object,
315        const Symbol_value<size>* psymval)
316   { This::template rel<8>(view, object, psymval); }
317
318   // Do an 8-bit RELA relocation with the addend in the relocation.
319   static inline void
320   rel8a(unsigned char* view, unsigned char value, unsigned char addend)
321   { This::template rela<8>(view, value, addend); }
322
323   static inline void
324   rel8a(unsigned char* view,
325         const Sized_relobj<size, big_endian>* object,
326         const Symbol_value<size>* psymval,
327         unsigned char addend)
328   { This::template rela<8>(view, object, psymval, addend); }
329
330   // Do an 8-bit REL relocation, sign extending the addend to SIZE.
331   static inline void
332   rel8s(unsigned char* view,
333          const Sized_relobj<size, big_endian>* object,
334          const Symbol_value<size>* psymval)
335   { This::template signedrel<8>(view, object, psymval); }
336
337   // Do a simple 8-bit PC relative relocation with the addend in the
338   // section contents.
339   static inline void
340   pcrel8(unsigned char* view, unsigned char value,
341          typename elfcpp::Elf_types<size>::Elf_Addr address)
342   { This::template pcrel<8>(view, value, address); }
343
344   static inline void
345   pcrel8(unsigned char* view,
346          const Sized_relobj<size, big_endian>* object,
347          const Symbol_value<size>* psymval,
348          typename elfcpp::Elf_types<size>::Elf_Addr address)
349   { This::template pcrel<8>(view, object, psymval, address); }
350
351   // Do a simple 8-bit PC relative RELA relocation with the addend in
352   // the reloc.
353   static inline void
354   pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
355           typename elfcpp::Elf_types<size>::Elf_Addr address)
356   { This::template pcrela<8>(view, value, addend, address); }
357
358   static inline void
359   pcrela8(unsigned char* view,
360           const Sized_relobj<size, big_endian>* object,
361           const Symbol_value<size>* psymval,
362           unsigned char addend,
363           typename elfcpp::Elf_types<size>::Elf_Addr address)
364   { This::template pcrela<8>(view, object, psymval, addend, address); }
365
366   // Do a simple 16-bit REL relocation with the addend in the section
367   // contents.
368   static inline void
369   rel16(unsigned char* view, elfcpp::Elf_Half value)
370   { This::template rel<16>(view, value); }
371
372   static inline void
373   rel16(unsigned char* view,
374         const Sized_relobj<size, big_endian>* object,
375         const Symbol_value<size>* psymval)
376   { This::template rel<16>(view, object, psymval); }
377
378   // Do an 16-bit RELA relocation with the addend in the relocation.
379   static inline void
380   rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
381   { This::template rela<16>(view, value, addend); }
382
383   static inline void
384   rela16(unsigned char* view,
385          const Sized_relobj<size, big_endian>* object,
386          const Symbol_value<size>* psymval,
387          elfcpp::Elf_Half addend)
388   { This::template rela<16>(view, object, psymval, addend); }
389
390   // Do a 16-bit REL relocation, sign extending the addend to SIZE.
391   static inline void
392   rel16s(unsigned char* view,
393          const Sized_relobj<size, big_endian>* object,
394          const Symbol_value<size>* psymval)
395   { This::template signedrel<16>(view, object, psymval); }
396
397   // Do a simple 16-bit PC relative REL relocation with the addend in
398   // the section contents.
399   static inline void
400   pcrel16(unsigned char* view, elfcpp::Elf_Half value,
401           typename elfcpp::Elf_types<size>::Elf_Addr address)
402   { This::template pcrel<16>(view, value, address); }
403
404   static inline void
405   pcrel16(unsigned char* view,
406           const Sized_relobj<size, big_endian>* object,
407           const Symbol_value<size>* psymval,
408           typename elfcpp::Elf_types<size>::Elf_Addr address)
409   { This::template pcrel<16>(view, object, psymval, address); }
410
411   // Do a simple 16-bit PC relative RELA relocation with the addend in
412   // the reloc.
413   static inline void
414   pcrela16(unsigned char* view, elfcpp::Elf_Half value,
415            elfcpp::Elf_Half addend,
416            typename elfcpp::Elf_types<size>::Elf_Addr address)
417   { This::template pcrela<16>(view, value, addend, address); }
418
419   static inline void
420   pcrela16(unsigned char* view,
421            const Sized_relobj<size, big_endian>* object,
422            const Symbol_value<size>* psymval,
423            elfcpp::Elf_Half addend,
424            typename elfcpp::Elf_types<size>::Elf_Addr address)
425   { This::template pcrela<16>(view, object, psymval, addend, address); }
426
427   // Do a simple 32-bit REL relocation with the addend in the section
428   // contents.
429   static inline void
430   rel32(unsigned char* view, elfcpp::Elf_Word value)
431   { This::template rel<32>(view, value); }
432
433   static inline void
434   rel32(unsigned char* view,
435         const Sized_relobj<size, big_endian>* object,
436         const Symbol_value<size>* psymval)
437   { This::template rel<32>(view, object, psymval); }
438
439   // Do an 32-bit RELA relocation with the addend in the relocation.
440   static inline void
441   rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
442   { This::template rela<32>(view, value, addend); }
443
444   static inline void
445   rela32(unsigned char* view,
446          const Sized_relobj<size, big_endian>* object,
447          const Symbol_value<size>* psymval,
448          elfcpp::Elf_Word addend)
449   { This::template rela<32>(view, object, psymval, addend); }
450
451   // Do a 32-bit REL relocation, sign extending the addend to SIZE.
452   static inline void
453   rel32s(unsigned char* view,
454          const Sized_relobj<size, big_endian>* object,
455          const Symbol_value<size>* psymval)
456   { This::template signedrel<32>(view, object, psymval); }
457
458   // Do a simple 32-bit PC relative REL relocation with the addend in
459   // the section contents.
460   static inline void
461   pcrel32(unsigned char* view, elfcpp::Elf_Word value,
462           typename elfcpp::Elf_types<size>::Elf_Addr address)
463   { This::template pcrel<32>(view, value, address); }
464
465   static inline void
466   pcrel32(unsigned char* view,
467           const Sized_relobj<size, big_endian>* object,
468           const Symbol_value<size>* psymval,
469           typename elfcpp::Elf_types<size>::Elf_Addr address)
470   { This::template pcrel<32>(view, object, psymval, address); }
471
472   // Do a simple 32-bit PC relative RELA relocation with the addend in
473   // the relocation.
474   static inline void
475   pcrela32(unsigned char* view, elfcpp::Elf_Word value,
476            elfcpp::Elf_Word addend,
477            typename elfcpp::Elf_types<size>::Elf_Addr address)
478   { This::template pcrela<32>(view, value, addend, address); }
479
480   static inline void
481   pcrela32(unsigned char* view,
482            const Sized_relobj<size, big_endian>* object,
483            const Symbol_value<size>* psymval,
484            elfcpp::Elf_Word addend,
485            typename elfcpp::Elf_types<size>::Elf_Addr address)
486   { This::template pcrela<32>(view, object, psymval, addend, address); }
487
488   // Do a simple 64-bit REL relocation with the addend in the section
489   // contents.
490   static inline void
491   rel64(unsigned char* view, elfcpp::Elf_Xword value)
492   { This::template rel<64>(view, value); }
493
494   static inline void
495   rel64(unsigned char* view,
496         const Sized_relobj<size, big_endian>* object,
497         const Symbol_value<size>* psymval)
498   { This::template rel<64>(view, object, psymval); }
499
500   // Do a 64-bit RELA relocation with the addend in the relocation.
501   static inline void
502   rela64(unsigned char* view, elfcpp::Elf_Xword value,
503          elfcpp::Elf_Xword addend)
504   { This::template rela<64>(view, value, addend); }
505
506   static inline void
507   rela64(unsigned char* view,
508          const Sized_relobj<size, big_endian>* object,
509          const Symbol_value<size>* psymval,
510          elfcpp::Elf_Xword addend)
511   { This::template rela<64>(view, object, psymval, addend); }
512
513   // Do a simple 64-bit PC relative REL relocation with the addend in
514   // the section contents.
515   static inline void
516   pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
517           typename elfcpp::Elf_types<size>::Elf_Addr address)
518   { This::template pcrel<64>(view, value, address); }
519
520   static inline void
521   pcrel64(unsigned char* view,
522           const Sized_relobj<size, big_endian>* object,
523           const Symbol_value<size>* psymval,
524           typename elfcpp::Elf_types<size>::Elf_Addr address)
525   { This::template pcrel<64>(view, object, psymval, address); }
526
527   // Do a simple 64-bit PC relative RELA relocation with the addend in
528   // the relocation.
529   static inline void
530   pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
531            elfcpp::Elf_Xword addend,
532            typename elfcpp::Elf_types<size>::Elf_Addr address)
533   { This::template pcrela<64>(view, value, addend, address); }
534
535   static inline void
536   pcrela64(unsigned char* view,
537            const Sized_relobj<size, big_endian>* object,
538            const Symbol_value<size>* psymval,
539            elfcpp::Elf_Xword addend,
540            typename elfcpp::Elf_types<size>::Elf_Addr address)
541   { This::template pcrela<64>(view, object, psymval, addend, address); }
542 };
543
544 // We try to avoid COPY relocations when possible.  A COPY relocation
545 // may be required when an executable refers to a variable defined in
546 // a shared library.  COPY relocations are problematic because they
547 // tie the executable to the exact size of the variable in the shared
548 // library.  We can avoid them if all the references to the variable
549 // are in a writeable section.  In that case we can simply use dynamic
550 // relocations.  However, when scanning relocs, we don't know when we
551 // see the relocation whether we will be forced to use a COPY
552 // relocation or not.  So we have to save the relocation during the
553 // reloc scanning, and then emit it as a dynamic relocation if
554 // necessary.  This class implements that.  It is used by the target
555 // specific code.
556
557 template<int size, bool big_endian>
558 class Copy_relocs
559 {
560  public:
561   Copy_relocs()
562     : entries_()
563   { }
564
565   // Return whether we need a COPY reloc for a reloc against GSYM,
566   // which is being applied to section SHNDX in OBJECT.
567   static bool
568   need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
569                   Sized_symbol<size>* gsym);
570
571   // Save a Rel against SYM for possible emission later.  SHNDX is the
572   // index of the section to which the reloc is being applied.
573   void
574   save(Symbol* sym, Relobj*, unsigned int shndx,
575        const elfcpp::Rel<size, big_endian>&);
576
577   // Save a Rela against SYM for possible emission later.
578   void
579   save(Symbol* sym, Relobj*, unsigned int shndx,
580        const elfcpp::Rela<size, big_endian>&);
581
582   // Return whether there are any relocs to emit.  This also discards
583   // entries which need not be emitted.
584   bool
585   any_to_emit();
586
587   // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
588   // is still defined in the dynamic object).
589   template<int sh_type>
590   void
591   emit(Output_data_reloc<sh_type, true, size, big_endian>*);
592
593  private:
594   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
595   typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
596
597   // This POD class holds the entries we are saving.
598   class Copy_reloc_entry
599   {
600    public:
601     Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
602                      Relobj* relobj, unsigned int shndx,
603                      Address address, Addend addend)
604       : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
605         shndx_(shndx), address_(address), addend_(addend)
606     { }
607
608     // Return whether we should emit this reloc.  If we should not
609     // emit, we clear it.
610     bool
611     should_emit();
612
613     // Emit this reloc.
614
615     void
616     emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
617
618     void
619     emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
620
621    private:
622     Symbol* sym_;
623     unsigned int reloc_type_;
624     Relobj* relobj_;
625     unsigned int shndx_;
626     Address address_;
627     Addend addend_;
628   };
629
630   // A list of relocs to be saved.
631   typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
632
633   // The list of relocs we are saving.
634   Copy_reloc_entries entries_;
635 };
636
637 } // End namespace gold.
638
639 #endif // !defined(GOLD_RELOC_H)