OSDN Git Service

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