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