Framework for relocation scanning. Implement simple static TLS
[external/binutils.git] / gold / reloc.h
1 // reloc.h -- relocate input files for gold   -*- C++ -*-
2
3 #ifndef GOLD_RELOC_H
4 #define GOLD_RELOC_H
5
6 #include <byteswap.h>
7
8 #include "workqueue.h"
9
10 namespace gold
11 {
12
13 class Object;
14 class Read_relocs_data;
15 class Stringpool;
16
17 // A class to read the relocations for an object file, and then queue
18 // up a task to see if they require any GOT/PLT/COPY relocations in
19 // the symbol table.
20
21 class Read_relocs : public Task
22 {
23  public:
24   // SYMTAB_LOCK is used to lock the symbol table.  BLOCKER should be
25   // unblocked when the Scan_relocs task completes.
26   Read_relocs(const General_options& options, Symbol_table* symtab,
27               Object* object, Task_token* symtab_lock,
28               Task_token* blocker)
29     : options_(options), symtab_(symtab), object_(object),
30       symtab_lock_(symtab_lock), blocker_(blocker)
31   { }
32
33   // The standard Task methods.
34
35   Is_runnable_type
36   is_runnable(Workqueue*);
37
38   Task_locker*
39   locks(Workqueue*);
40
41   void
42   run(Workqueue*);
43
44  private:
45   const General_options& options_;
46   Symbol_table* symtab_;
47   Object* object_;
48   Task_token* symtab_lock_;
49   Task_token* blocker_;
50 };
51
52 // Scan the relocations for an object to see if they require any
53 // GOT/PLT/COPY relocations.
54
55 class Scan_relocs : public Task
56 {
57  public:
58   // SYMTAB_LOCK is used to lock the symbol table.  BLOCKER should be
59   // unblocked when the task completes.
60   Scan_relocs(const General_options& options, Symbol_table* symtab,
61               Object* object, Read_relocs_data* rd, Task_token* symtab_lock,
62               Task_token* blocker)
63     : options_(options), symtab_(symtab), object_(object), rd_(rd),
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   class Scan_relocs_locker;
80
81   const General_options& options_;
82   Symbol_table* symtab_;
83   Object* object_;
84   Read_relocs_data* rd_;
85   Task_token* symtab_lock_;
86   Task_token* blocker_;
87 };
88
89 // A class to perform all the relocations for an object file.
90
91 class Relocate_task : public Task
92 {
93  public:
94   Relocate_task(const General_options& options, const Symbol_table* symtab,
95                 const Layout* layout, Object* object, Output_file* of,
96                 Task_token* final_blocker)
97     : options_(options), symtab_(symtab), layout_(layout), object_(object),
98       of_(of), final_blocker_(final_blocker)
99   { }
100
101   // The standard Task methods.
102
103   Is_runnable_type
104   is_runnable(Workqueue*);
105
106   Task_locker*
107   locks(Workqueue*);
108
109   void
110   run(Workqueue*);
111
112  private:
113   class Relocate_locker;
114
115   const General_options& options_;
116   const Symbol_table* symtab_;
117   const Layout* layout_;
118   Object* object_;
119   Output_file* of_;
120   Task_token* final_blocker_;
121 };
122
123 // Integer swapping routines used by relocation functions.  FIXME:
124 // Maybe these should be more general, and/or shared with elfcpp.
125
126 // Endian simply indicates whether the host is big endian or not,
127 // based on the results of the configure script.
128
129 struct Endian
130 {
131  public:
132   // Used for template specializations.
133 #ifdef WORDS_BIGENDIAN
134   static const bool host_big_endian = true;
135 #else
136   static const bool host_big_endian = false;
137 #endif
138 };
139
140 // Valtype_base is a template based on size (8, 16, 32, 64) which
141 // defines a typedef Valtype for the unsigned integer of the specified
142 // size.
143
144 template<int size>
145 struct Valtype_base;
146
147 template<>
148 struct Valtype_base<8>
149 {
150   typedef unsigned char Valtype;
151 };
152
153 template<>
154 struct Valtype_base<16>
155 {
156   typedef uint16_t Valtype;
157 };
158
159 template<>
160 struct Valtype_base<32>
161 {
162   typedef uint32_t Valtype;
163 };
164
165 template<>
166 struct Valtype_base<64>
167 {
168   typedef uint64_t Valtype;
169 };
170
171 // Convert_host is a template based on size and on whether the host
172 // and target have the same endianness.  It defines the type Valtype,
173 // and defines a function convert_host which takes an argument of type
174 // Valtype and swaps it if the host and target have different
175 // endianness.
176
177 template<int size, bool same_endian>
178 struct Convert_host;
179
180 template<int size>
181 struct Convert_host<size, true>
182 {
183   typedef typename Valtype_base<size>::Valtype Valtype;
184
185   static inline Valtype
186   convert_host(Valtype v)
187   { return v; }
188 };
189
190 template<>
191 struct Convert_host<8, false>
192 {
193   typedef Valtype_base<8>::Valtype Valtype;
194
195   static inline Valtype
196   convert_host(Valtype v)
197   { return v; }
198 };
199
200 template<>
201 struct Convert_host<16, false>
202 {
203   typedef Valtype_base<16>::Valtype Valtype;
204
205   static inline Valtype
206   convert_host(Valtype v)
207   { return bswap_16(v); }
208 };
209
210 template<>
211 struct Convert_host<32, false>
212 {
213   typedef Valtype_base<32>::Valtype Valtype;
214
215   static inline Valtype
216   convert_host(Valtype v)
217   { return bswap_32(v); }
218 };
219
220 template<>
221 struct Convert_host<64, false>
222 {
223   typedef Valtype_base<64>::Valtype Valtype;
224
225   static inline Valtype
226   convert_host(Valtype v)
227   { return bswap_64(v); }
228 };
229
230 // Convert is a template based on size and on whether we have a big
231 // endian target.  It defines Valtype and convert_host like
232 // Convert_host.  That is, it is just like Convert_host except in the
233 // meaning of the second template parameter.
234
235 template<int size, bool big_endian>
236 struct Convert
237 {
238   typedef typename Valtype_base<size>::Valtype Valtype;
239
240   static inline Valtype
241   convert_host(Valtype v)
242   { return Convert_host<size, big_endian == Endian::host_big_endian>
243       ::convert_host(v); }
244 };
245
246 // Swap is a template based on size and on whether the target is big
247 // endian.  It defines the type Valtype and the functions readval and
248 // writeval.  The functions read and write values of the appropriate
249 // size out of buffers, swapping them if necessary.
250
251 template<int size, bool big_endian>
252 struct Swap
253 {
254   typedef typename Valtype_base<size>::Valtype Valtype;
255
256   static inline Valtype
257   readval(const Valtype* wv)
258   { return Convert<size, big_endian>::convert_host(*wv); }
259
260   static inline void
261   writeval(Valtype* wv, Valtype v)
262   { *wv = Convert<size, big_endian>::convert_host(v); }
263 };
264
265 // Swap_unaligned is a template based on size and on whether the
266 // target is big endian.  It defines the type Valtype and the
267 // functions readval_unaligned and writeval_unaligned.  The functions
268 // read and write values of the appropriate size out of buffers which
269 // may be misaligned.
270
271 template<int size, bool big_endian>
272 class Swap_unaligned;
273
274 template<bool big_endian>
275 class Swap_unaligned<8, big_endian>
276 {
277 public:
278   typedef typename Valtype_base<8>::Valtype Valtype;
279
280   static inline Valtype
281   readval_unaligned(const unsigned char* wv)
282   { return *wv; }
283
284   static inline void
285   writeval_unaligned(unsigned char* wv, Valtype v)
286   { *wv = v; }
287 };
288
289 template<>
290 class Swap_unaligned<16, false>
291 {
292 public:
293   typedef Valtype_base<16>::Valtype Valtype;
294
295   static inline Valtype
296   readval_unaligned(const unsigned char* wv)
297   {
298     return (wv[1] << 8) | wv[0];
299   }
300
301   static inline void
302   writeval_unaligned(unsigned char* wv, Valtype v)
303   {
304     wv[1] = v >> 8;
305     wv[0] = v;
306   }
307 };
308
309 template<>
310 class Swap_unaligned<16, true>
311 {
312 public:
313   typedef Valtype_base<16>::Valtype Valtype;
314
315   static inline Valtype
316   readval_unaligned(const unsigned char* wv)
317   {
318     return (wv[0] << 8) | wv[1];
319   }
320
321   static inline void
322   writeval_unaligned(unsigned char* wv, Valtype v)
323   {
324     wv[0] = v >> 8;
325     wv[1] = v;
326   }
327 };
328
329 template<>
330 class Swap_unaligned<32, false>
331 {
332 public:
333   typedef Valtype_base<32>::Valtype Valtype;
334
335   static inline Valtype
336   readval_unaligned(const unsigned char* wv)
337   {
338     return (wv[3] << 24) | (wv[2] << 16) | (wv[1] << 8) | wv[0];
339   }
340
341   static inline void
342   writeval_unaligned(unsigned char* wv, Valtype v)
343   {
344     wv[3] = v >> 24;
345     wv[2] = v >> 16;
346     wv[1] = v >> 8;
347     wv[0] = v;
348   }
349 };
350
351 template<>
352 class Swap_unaligned<32, true>
353 {
354 public:
355   typedef Valtype_base<32>::Valtype Valtype;
356
357   static inline Valtype
358   readval_unaligned(const unsigned char* wv)
359   {
360     return (wv[0] << 24) | (wv[1] << 16) | (wv[2] << 8) | wv[3];
361   }
362
363   static inline void
364   writeval_unaligned(unsigned char* wv, Valtype v)
365   {
366     wv[0] = v >> 24;
367     wv[1] = v >> 16;
368     wv[2] = v >> 8;
369     wv[3] = v;
370   }
371 };
372
373 template<>
374 class Swap_unaligned<64, false>
375 {
376 public:
377   typedef Valtype_base<64>::Valtype Valtype;
378
379   static inline Valtype
380   readval_unaligned(const unsigned char* wv)
381   {
382     return ((static_cast<Valtype>(wv[7]) << 56)
383             | (static_cast<Valtype>(wv[6]) << 48)
384             | (static_cast<Valtype>(wv[5]) << 40)
385             | (static_cast<Valtype>(wv[4]) << 32)
386             | (static_cast<Valtype>(wv[3]) << 24)
387             | (static_cast<Valtype>(wv[2]) << 16)
388             | (static_cast<Valtype>(wv[1]) << 8)
389             | static_cast<Valtype>(wv[0]));
390   }
391
392   static inline void
393   writeval_unaligned(unsigned char* wv, Valtype v)
394   {
395     wv[7] = v >> 56;
396     wv[6] = v >> 48;
397     wv[5] = v >> 40;
398     wv[4] = v >> 32;
399     wv[3] = v >> 24;
400     wv[2] = v >> 16;
401     wv[1] = v >> 8;
402     wv[0] = v;
403   }
404 };
405
406 template<>
407 class Swap_unaligned<64, true>
408 {
409 public:
410   typedef Valtype_base<64>::Valtype Valtype;
411
412   static inline Valtype
413   readval_unaligned(const unsigned char* wv)
414   {
415     return ((static_cast<Valtype>(wv[0]) << 56)
416             | (static_cast<Valtype>(wv[1]) << 48)
417             | (static_cast<Valtype>(wv[2]) << 40)
418             | (static_cast<Valtype>(wv[3]) << 32)
419             | (static_cast<Valtype>(wv[4]) << 24)
420             | (static_cast<Valtype>(wv[5]) << 16)
421             | (static_cast<Valtype>(wv[6]) << 8)
422             | static_cast<Valtype>(wv[7]));
423   }
424
425   static inline void
426   writeval_unaligned(unsigned char* wv, Valtype v)
427   {
428     wv[7] = v >> 56;
429     wv[6] = v >> 48;
430     wv[5] = v >> 40;
431     wv[4] = v >> 32;
432     wv[3] = v >> 24;
433     wv[2] = v >> 16;
434     wv[1] = v >> 8;
435     wv[0] = v;
436   }
437 };
438
439 // Standard relocation routines which are used on many targets.  Here
440 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
441
442 template<int size, bool big_endian>
443 class Relocate_functions
444 {
445 private:
446   // Do a simple relocation with the addend in the section contents.
447   // VALSIZE is the size of the value.
448   template<int valsize>
449   static inline void
450   rel(unsigned char* view, typename Swap<valsize, big_endian>::Valtype value)
451   {
452     typedef typename Swap<valsize, big_endian>::Valtype Valtype;
453     Valtype* wv = reinterpret_cast<Valtype*>(view);
454     Valtype x = Swap<valsize, big_endian>::readval(wv);
455     Swap<valsize, big_endian>::writeval(wv, x + value);
456   }
457
458   // Do a simple PC relative relocation with the addend in the section
459   // contents.  VALSIZE is the size of the value.
460   template<int valsize>
461   static inline void
462   pcrel(unsigned char* view, typename Swap<valsize, big_endian>::Valtype value,
463         typename elfcpp::Elf_types<size>::Elf_Addr address)
464   {
465     typedef typename Swap<valsize, big_endian>::Valtype Valtype;
466     Valtype* wv = reinterpret_cast<Valtype*>(view);
467     Valtype x = Swap<valsize, big_endian>::readval(wv);
468     Swap<valsize, big_endian>::writeval(wv, x + value - address);
469   }
470
471   typedef Relocate_functions<size, big_endian> This;
472
473 public:
474   // Do a simple 8-bit REL relocation with the addend in the object
475   // file data.
476   static inline void
477   rel8(unsigned char* view, unsigned char value)
478   {
479     This::template rel<8>(view, value);
480   }
481
482   // Do a simple 8-bit PC relative relocation with the addend in the
483   // object file data.
484   static inline void
485   pcrel8(unsigned char* view, unsigned char value,
486          typename elfcpp::Elf_types<size>::Elf_Addr address)
487   {
488     This::template pcrel<8>(view, value, address);
489   }
490
491   // Do a simple 16-bit REL relocation with the addend in the object
492   // file data.
493   static inline void
494   rel16(unsigned char* view, elfcpp::Elf_Half value)
495   {
496     This::template rel<16>(view, value);
497   }
498
499   // Do a simple 32-bit PC relative REL relocation with the addend in
500   // the object file data.
501   static inline void
502   pcrel16(unsigned char* view, elfcpp::Elf_Word value,
503           typename elfcpp::Elf_types<size>::Elf_Addr address)
504   {
505     This::template pcrel<16>(view, value, address);
506   }
507
508   // Do a simple 32-bit REL relocation with the addend in the section
509   // contents.
510   static inline void
511   rel32(unsigned char* view, elfcpp::Elf_Word value)
512   {
513     This::template rel<32>(view, value);
514   }
515
516   // Do a simple 32-bit PC relative REL relocation with the addend in
517   // the section contents.
518   static inline void
519   pcrel32(unsigned char* view, elfcpp::Elf_Word value,
520           typename elfcpp::Elf_types<size>::Elf_Addr address)
521   {
522     This::template pcrel<32>(view, value, address);
523   }
524
525   // Do a simple 64-bit REL relocation with the addend in the section
526   // contents.
527   static inline void
528   rel64(unsigned char* view, elfcpp::Elf_Word value)
529   {
530     This::template rel<64>(view, value);
531   }
532
533   // Do a simple 64-bit PC relative REL relocation with the addend in
534   // the section contents.
535   static inline void
536   pcrel64(unsigned char* view, elfcpp::Elf_Word value,
537           typename elfcpp::Elf_types<size>::Elf_Addr address)
538   {
539     This::template pcrel<64>(view, value, address);
540   }
541 };
542
543 } // End namespace gold.
544
545 #endif // !defined(GOLD_RELOC_H)