gas/
[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 PC relative relocation with the addend in the section
196   // contents.  VALSIZE is the size of the value.
197   template<int valsize>
198   static inline void
199   pcrel(unsigned char* view,
200         typename elfcpp::Swap<valsize, big_endian>::Valtype value,
201         typename elfcpp::Elf_types<size>::Elf_Addr address)
202   {
203     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
204     Valtype* wv = reinterpret_cast<Valtype*>(view);
205     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
206     elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
207   }
208
209   // Do a simple PC relative relocation with a Symbol_value with the
210   // addend in the section contents.  VALSIZE is the size of the
211   // value.
212   template<int valsize>
213   static inline void
214   pcrel(unsigned char* view,
215         const Sized_relobj<size, big_endian>* object,
216         const Symbol_value<size>* psymval,
217         typename elfcpp::Elf_types<size>::Elf_Addr address)
218   {
219     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
220     Valtype* wv = reinterpret_cast<Valtype*>(view);
221     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
222     x = psymval->value(object, x);
223     elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
224   }
225
226   typedef Relocate_functions<size, big_endian> This;
227
228 public:
229   // Do a simple 8-bit REL relocation with the addend in the section
230   // contents.
231   static inline void
232   rel8(unsigned char* view, unsigned char value)
233   { This::template rel<8>(view, value); }
234
235   static inline void
236   rel8(unsigned char* view,
237        const Sized_relobj<size, big_endian>* object,
238        const Symbol_value<size>* psymval)
239   { This::template rel<8>(view, object, psymval); }
240
241   // Do a simple 8-bit PC relative relocation with the addend in the
242   // section contents.
243   static inline void
244   pcrel8(unsigned char* view, unsigned char value,
245          typename elfcpp::Elf_types<size>::Elf_Addr address)
246   { This::template pcrel<8>(view, value, address); }
247
248   static inline void
249   pcrel8(unsigned char* view,
250          const Sized_relobj<size, big_endian>* object,
251          const Symbol_value<size>* psymval,
252          typename elfcpp::Elf_types<size>::Elf_Addr address)
253   { This::template pcrel<8>(view, object, psymval, address); }
254
255   // Do a simple 16-bit REL relocation with the addend in the section
256   // contents.
257   static inline void
258   rel16(unsigned char* view, elfcpp::Elf_Half value)
259   { This::template rel<16>(view, value); }
260
261   static inline void
262   rel16(unsigned char* view,
263         const Sized_relobj<size, big_endian>* object,
264         const Symbol_value<size>* psymval)
265   { This::template rel<16>(view, object, psymval); }
266
267   // Do a simple 32-bit PC relative REL relocation with the addend in
268   // the section contents.
269   static inline void
270   pcrel16(unsigned char* view, elfcpp::Elf_Word value,
271           typename elfcpp::Elf_types<size>::Elf_Addr address)
272   { This::template pcrel<16>(view, value, address); }
273
274   static inline void
275   pcrel16(unsigned char* view,
276           const Sized_relobj<size, big_endian>* object,
277           const Symbol_value<size>* psymval,
278           typename elfcpp::Elf_types<size>::Elf_Addr address)
279   { This::template pcrel<16>(view, object, psymval, address); }
280
281   // Do a simple 32-bit REL relocation with the addend in the section
282   // contents.
283   static inline void
284   rel32(unsigned char* view, elfcpp::Elf_Word value)
285   { This::template rel<32>(view, value); }
286
287   static inline void
288   rel32(unsigned char* view,
289         const Sized_relobj<size, big_endian>* object,
290         const Symbol_value<size>* psymval)
291   { This::template rel<32>(view, object, psymval); }
292
293   // Do a simple 32-bit PC relative REL relocation with the addend in
294   // the section contents.
295   static inline void
296   pcrel32(unsigned char* view, elfcpp::Elf_Word value,
297           typename elfcpp::Elf_types<size>::Elf_Addr address)
298   { This::template pcrel<32>(view, value, address); }
299
300   static inline void
301   pcrel32(unsigned char* view,
302           const Sized_relobj<size, big_endian>* object,
303           const Symbol_value<size>* psymval,
304           typename elfcpp::Elf_types<size>::Elf_Addr address)
305   { This::template pcrel<32>(view, object, psymval, address); }
306
307   // Do a simple 64-bit REL relocation with the addend in the section
308   // contents.
309   static inline void
310   rel64(unsigned char* view, elfcpp::Elf_Xword value)
311   { This::template rel<64>(view, value); }
312
313   static inline void
314   rel64(unsigned char* view,
315         const Sized_relobj<size, big_endian>* object,
316         const Symbol_value<size>* psymval)
317   { This::template rel<64>(view, object, psymval); }
318
319   // Do a simple 64-bit PC relative REL relocation with the addend in
320   // the section contents.
321   static inline void
322   pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
323           typename elfcpp::Elf_types<size>::Elf_Addr address)
324   { This::template pcrel<64>(view, value, address); }
325
326   static inline void
327   pcrel64(unsigned char* view,
328           const Sized_relobj<size, big_endian>* object,
329           const Symbol_value<size>* psymval,
330           typename elfcpp::Elf_types<size>::Elf_Addr address)
331   { This::template pcrel<64>(view, object, psymval, address); }
332 };
333
334 // We try to avoid COPY relocations when possible.  A COPY relocation
335 // may be required when an executable refers to a variable defined in
336 // a shared library.  COPY relocations are problematic because they
337 // tie the executable to the exact size of the variable in the shared
338 // library.  We can avoid them if all the references to the variable
339 // are in a writeable section.  In that case we can simply use dynamic
340 // relocations.  However, when scanning relocs, we don't know when we
341 // see the relocation whether we will be forced to use a COPY
342 // relocation or not.  So we have to save the relocation during the
343 // reloc scanning, and then emit it as a dynamic relocation if
344 // necessary.  This class implements that.  It is used by the target
345 // specific code.
346
347 template<int size, bool big_endian>
348 class Copy_relocs
349 {
350  public:
351   Copy_relocs()
352     : entries_()
353   { }
354
355   // Return whether we need a COPY reloc for a reloc against GSYM,
356   // which is being applied to section SHNDX in OBJECT.
357   static bool
358   need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
359                   Sized_symbol<size>* gsym);
360
361   // Save a Rel against SYM for possible emission later.  SHNDX is the
362   // index of the section to which the reloc is being applied.
363   void
364   save(Symbol* sym, Relobj*, unsigned int shndx,
365        const elfcpp::Rel<size, big_endian>&);
366
367   // Save a Rela against SYM for possible emission later.
368   void
369   save(Symbol* sym, Relobj*, unsigned int shndx,
370        const elfcpp::Rela<size, big_endian>&);
371
372   // Return whether there are any relocs to emit.  This also discards
373   // entries which need not be emitted.
374   bool
375   any_to_emit();
376
377   // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
378   // is still defined in the dynamic object).
379   template<int sh_type>
380   void
381   emit(Output_data_reloc<sh_type, true, size, big_endian>*);
382
383  private:
384   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
385   typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
386
387   // This POD class holds the entries we are saving.
388   class Copy_reloc_entry
389   {
390    public:
391     Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
392                      Relobj* relobj, unsigned int shndx,
393                      Address address, Addend addend)
394       : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
395         shndx_(shndx), address_(address), addend_(addend)
396     { }
397
398     // Return whether we should emit this reloc.  If we should not
399     // emit, we clear it.
400     bool
401     should_emit();
402
403     // Emit this reloc.
404
405     void
406     emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
407
408     void
409     emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
410
411    private:
412     Symbol* sym_;
413     unsigned int reloc_type_;
414     Relobj* relobj_;
415     unsigned int shndx_;
416     Address address_;
417     Addend addend_;
418   };
419
420   // A list of relocs to be saved.
421   typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
422
423   // The list of relocs we are saving.
424   Copy_reloc_entries entries_;
425 };
426
427 } // End namespace gold.
428
429 #endif // !defined(GOLD_RELOC_H)