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