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