* elfxx-mips.c (mips_elf_merge_gots): Always use maxcnt.
[binutils.git] / gold / reloc.h
bloba2e9d54729a6f00b28472bbee623d689782cd948
1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 #ifndef GOLD_RELOC_H
4 #define GOLD_RELOC_H
6 #include <byteswap.h>
8 #include "workqueue.h"
10 namespace gold
13 class Object;
14 class Read_relocs_data;
15 class Stringpool;
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.
21 class Read_relocs : public Task
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 { }
33 // The standard Task methods.
35 Is_runnable_type
36 is_runnable(Workqueue*);
38 Task_locker*
39 locks(Workqueue*);
41 void
42 run(Workqueue*);
44 private:
45 const General_options& options_;
46 Symbol_table* symtab_;
47 Object* object_;
48 Task_token* symtab_lock_;
49 Task_token* blocker_;
52 // Scan the relocations for an object to see if they require any
53 // GOT/PLT/COPY relocations.
55 class Scan_relocs : public Task
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 { }
67 // The standard Task methods.
69 Is_runnable_type
70 is_runnable(Workqueue*);
72 Task_locker*
73 locks(Workqueue*);
75 void
76 run(Workqueue*);
78 private:
79 class Scan_relocs_locker;
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_;
89 // A class to perform all the relocations for an object file.
91 class Relocate_task : public Task
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 { }
101 // The standard Task methods.
103 Is_runnable_type
104 is_runnable(Workqueue*);
106 Task_locker*
107 locks(Workqueue*);
109 void
110 run(Workqueue*);
112 private:
113 class Relocate_locker;
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_;
123 // Integer swapping routines used by relocation functions. FIXME:
124 // Maybe these should be more general, and/or shared with elfcpp.
126 // Endian simply indicates whether the host is big endian or not,
127 // based on the results of the configure script.
129 struct Endian
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
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.
144 template<int size>
145 struct Valtype_base;
147 template<>
148 struct Valtype_base<8>
150 typedef unsigned char Valtype;
153 template<>
154 struct Valtype_base<16>
156 typedef uint16_t Valtype;
159 template<>
160 struct Valtype_base<32>
162 typedef uint32_t Valtype;
165 template<>
166 struct Valtype_base<64>
168 typedef uint64_t Valtype;
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.
177 template<int size, bool same_endian>
178 struct Convert_host;
180 template<int size>
181 struct Convert_host<size, true>
183 typedef typename Valtype_base<size>::Valtype Valtype;
185 static inline Valtype
186 convert_host(Valtype v)
187 { return v; }
190 template<>
191 struct Convert_host<8, false>
193 typedef Valtype_base<8>::Valtype Valtype;
195 static inline Valtype
196 convert_host(Valtype v)
197 { return v; }
200 template<>
201 struct Convert_host<16, false>
203 typedef Valtype_base<16>::Valtype Valtype;
205 static inline Valtype
206 convert_host(Valtype v)
207 { return bswap_16(v); }
210 template<>
211 struct Convert_host<32, false>
213 typedef Valtype_base<32>::Valtype Valtype;
215 static inline Valtype
216 convert_host(Valtype v)
217 { return bswap_32(v); }
220 template<>
221 struct Convert_host<64, false>
223 typedef Valtype_base<64>::Valtype Valtype;
225 static inline Valtype
226 convert_host(Valtype v)
227 { return bswap_64(v); }
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.
235 template<int size, bool big_endian>
236 struct Convert
238 typedef typename Valtype_base<size>::Valtype Valtype;
240 static inline Valtype
241 convert_host(Valtype v)
242 { return Convert_host<size, big_endian == Endian::host_big_endian>
243 ::convert_host(v); }
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.
251 template<int size, bool big_endian>
252 struct Swap
254 typedef typename Valtype_base<size>::Valtype Valtype;
256 static inline Valtype
257 readval(const Valtype* wv)
258 { return Convert<size, big_endian>::convert_host(*wv); }
260 static inline void
261 writeval(Valtype* wv, Valtype v)
262 { *wv = Convert<size, big_endian>::convert_host(v); }
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.
271 template<int size, bool big_endian>
272 class Swap_unaligned;
274 template<bool big_endian>
275 class Swap_unaligned<8, big_endian>
277 public:
278 typedef typename Valtype_base<8>::Valtype Valtype;
280 static inline Valtype
281 readval_unaligned(const unsigned char* wv)
282 { return *wv; }
284 static inline void
285 writeval_unaligned(unsigned char* wv, Valtype v)
286 { *wv = v; }
289 template<>
290 class Swap_unaligned<16, false>
292 public:
293 typedef Valtype_base<16>::Valtype Valtype;
295 static inline Valtype
296 readval_unaligned(const unsigned char* wv)
298 return (wv[1] << 8) | wv[0];
301 static inline void
302 writeval_unaligned(unsigned char* wv, Valtype v)
304 wv[1] = v >> 8;
305 wv[0] = v;
309 template<>
310 class Swap_unaligned<16, true>
312 public:
313 typedef Valtype_base<16>::Valtype Valtype;
315 static inline Valtype
316 readval_unaligned(const unsigned char* wv)
318 return (wv[0] << 8) | wv[1];
321 static inline void
322 writeval_unaligned(unsigned char* wv, Valtype v)
324 wv[0] = v >> 8;
325 wv[1] = v;
329 template<>
330 class Swap_unaligned<32, false>
332 public:
333 typedef Valtype_base<32>::Valtype Valtype;
335 static inline Valtype
336 readval_unaligned(const unsigned char* wv)
338 return (wv[3] << 24) | (wv[2] << 16) | (wv[1] << 8) | wv[0];
341 static inline void
342 writeval_unaligned(unsigned char* wv, Valtype v)
344 wv[3] = v >> 24;
345 wv[2] = v >> 16;
346 wv[1] = v >> 8;
347 wv[0] = v;
351 template<>
352 class Swap_unaligned<32, true>
354 public:
355 typedef Valtype_base<32>::Valtype Valtype;
357 static inline Valtype
358 readval_unaligned(const unsigned char* wv)
360 return (wv[0] << 24) | (wv[1] << 16) | (wv[2] << 8) | wv[3];
363 static inline void
364 writeval_unaligned(unsigned char* wv, Valtype v)
366 wv[0] = v >> 24;
367 wv[1] = v >> 16;
368 wv[2] = v >> 8;
369 wv[3] = v;
373 template<>
374 class Swap_unaligned<64, false>
376 public:
377 typedef Valtype_base<64>::Valtype Valtype;
379 static inline Valtype
380 readval_unaligned(const unsigned char* wv)
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]));
392 static inline void
393 writeval_unaligned(unsigned char* wv, Valtype v)
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;
406 template<>
407 class Swap_unaligned<64, true>
409 public:
410 typedef Valtype_base<64>::Valtype Valtype;
412 static inline Valtype
413 readval_unaligned(const unsigned char* wv)
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]));
425 static inline void
426 writeval_unaligned(unsigned char* wv, Valtype v)
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;
439 // Standard relocation routines which are used on many targets. Here
440 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
442 template<int size, bool big_endian>
443 class Relocate_functions
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)
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);
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)
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);
471 typedef Relocate_functions<size, big_endian> This;
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)
479 This::template rel<8>(view, value);
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)
488 This::template pcrel<8>(view, value, address);
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)
496 This::template rel<16>(view, value);
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)
505 This::template pcrel<16>(view, value, address);
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)
513 This::template rel<32>(view, value);
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)
522 This::template pcrel<32>(view, value, address);
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)
530 This::template rel<64>(view, value);
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)
539 This::template pcrel<64>(view, value, address);
543 } // End namespace gold.
545 #endif // !defined(GOLD_RELOC_H)