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