daily update
[binutils.git] / gold / reloc.h
blob39ffe4ac229eef971dc8c780d4becd899cc11147
1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_RELOC_H
24 #define GOLD_RELOC_H
26 #include <vector>
27 #include <byteswap.h>
29 #include "elfcpp.h"
30 #include "workqueue.h"
32 namespace gold
35 class General_options;
36 class Object;
37 class Relobj;
38 class Read_relocs_data;
39 class Symbol;
40 class Layout;
41 class Output_section;
43 template<int size>
44 class Sized_symbol;
46 template<int size, bool big_endian>
47 class Sized_relobj;
49 template<int size>
50 class Symbol_value;
52 template<int sh_type, bool dynamic, int size, bool big_endian>
53 class Output_data_reloc;
55 // A class to read the relocations for an object file, and then queue
56 // up a task to see if they require any GOT/PLT/COPY relocations in
57 // the symbol table.
59 class Read_relocs : public Task
61 public:
62 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
63 // unblocked when the Scan_relocs task completes.
64 Read_relocs(const General_options& options, Symbol_table* symtab,
65 Layout* layout, Relobj* object, Task_token* symtab_lock,
66 Task_token* blocker)
67 : options_(options), symtab_(symtab), layout_(layout), object_(object),
68 symtab_lock_(symtab_lock), blocker_(blocker)
69 { }
71 // The standard Task methods.
73 Task_token*
74 is_runnable();
76 void
77 locks(Task_locker*);
79 void
80 run(Workqueue*);
82 std::string
83 get_name() const;
85 private:
86 const General_options& options_;
87 Symbol_table* symtab_;
88 Layout* layout_;
89 Relobj* object_;
90 Task_token* symtab_lock_;
91 Task_token* blocker_;
94 // Scan the relocations for an object to see if they require any
95 // GOT/PLT/COPY relocations.
97 class Scan_relocs : public Task
99 public:
100 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
101 // unblocked when the task completes.
102 Scan_relocs(const General_options& options, Symbol_table* symtab,
103 Layout* layout, Relobj* object, Read_relocs_data* rd,
104 Task_token* symtab_lock, Task_token* blocker)
105 : options_(options), symtab_(symtab), layout_(layout), object_(object),
106 rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker)
109 // The standard Task methods.
111 Task_token*
112 is_runnable();
114 void
115 locks(Task_locker*);
117 void
118 run(Workqueue*);
120 std::string
121 get_name() const;
123 private:
124 const General_options& options_;
125 Symbol_table* symtab_;
126 Layout* layout_;
127 Relobj* object_;
128 Read_relocs_data* rd_;
129 Task_token* symtab_lock_;
130 Task_token* blocker_;
133 // A class to perform all the relocations for an object file.
135 class Relocate_task : public Task
137 public:
138 Relocate_task(const General_options& options, const Symbol_table* symtab,
139 const Layout* layout, Relobj* object, Output_file* of,
140 Task_token* input_sections_blocker,
141 Task_token* output_sections_blocker, Task_token* final_blocker)
142 : options_(options), symtab_(symtab), layout_(layout), object_(object),
143 of_(of), input_sections_blocker_(input_sections_blocker),
144 output_sections_blocker_(output_sections_blocker),
145 final_blocker_(final_blocker)
148 // The standard Task methods.
150 Task_token*
151 is_runnable();
153 void
154 locks(Task_locker*);
156 void
157 run(Workqueue*);
159 std::string
160 get_name() const;
162 private:
163 const General_options& options_;
164 const Symbol_table* symtab_;
165 const Layout* layout_;
166 Relobj* object_;
167 Output_file* of_;
168 Task_token* input_sections_blocker_;
169 Task_token* output_sections_blocker_;
170 Task_token* final_blocker_;
173 // Standard relocation routines which are used on many targets. Here
174 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
176 template<int size, bool big_endian>
177 class Relocate_functions
179 private:
180 // Do a simple relocation with the addend in the section contents.
181 // VALSIZE is the size of the value.
182 template<int valsize>
183 static inline void
184 rel(unsigned char* view,
185 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
187 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
188 Valtype* wv = reinterpret_cast<Valtype*>(view);
189 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
190 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
193 // Do a simple relocation using a Symbol_value with the addend in
194 // the section contents. VALSIZE is the size of the value to
195 // relocate.
196 template<int valsize>
197 static inline void
198 rel(unsigned char* view,
199 const Sized_relobj<size, big_endian>* object,
200 const Symbol_value<size>* psymval)
202 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
203 Valtype* wv = reinterpret_cast<Valtype*>(view);
204 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
205 x = psymval->value(object, x);
206 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
209 // Do a simple relocation with the addend in the relocation.
210 // VALSIZE is the size of the value.
211 template<int valsize>
212 static inline void
213 rela(unsigned char* view,
214 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
215 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
217 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
218 Valtype* wv = reinterpret_cast<Valtype*>(view);
219 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
222 // Do a simple relocation using a symbol value with the addend in
223 // the relocation. VALSIZE is the size of the value.
224 template<int valsize>
225 static inline void
226 rela(unsigned char* view,
227 const Sized_relobj<size, big_endian>* object,
228 const Symbol_value<size>* psymval,
229 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
231 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
232 Valtype* wv = reinterpret_cast<Valtype*>(view);
233 Valtype x = psymval->value(object, addend);
234 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
237 // Do a simple PC relative relocation with the addend in the section
238 // contents. VALSIZE is the size of the value.
239 template<int valsize>
240 static inline void
241 pcrel(unsigned char* view,
242 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
243 typename elfcpp::Elf_types<size>::Elf_Addr address)
245 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
246 Valtype* wv = reinterpret_cast<Valtype*>(view);
247 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
248 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
251 // Do a simple PC relative relocation with a Symbol_value with the
252 // addend in the section contents. VALSIZE is the size of the
253 // value.
254 template<int valsize>
255 static inline void
256 pcrel(unsigned char* view,
257 const Sized_relobj<size, big_endian>* object,
258 const Symbol_value<size>* psymval,
259 typename elfcpp::Elf_types<size>::Elf_Addr address)
261 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
262 Valtype* wv = reinterpret_cast<Valtype*>(view);
263 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
264 x = psymval->value(object, x);
265 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
268 // Do a simple PC relative relocation with the addend in the
269 // relocation. VALSIZE is the size of the value.
270 template<int valsize>
271 static inline void
272 pcrela(unsigned char* view,
273 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
274 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
275 typename elfcpp::Elf_types<size>::Elf_Addr address)
277 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
278 Valtype* wv = reinterpret_cast<Valtype*>(view);
279 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
282 // Do a simple PC relative relocation with a Symbol_value with the
283 // addend in the relocation. VALSIZE is the size of the value.
284 template<int valsize>
285 static inline void
286 pcrela(unsigned char* view,
287 const Sized_relobj<size, big_endian>* object,
288 const Symbol_value<size>* psymval,
289 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
290 typename elfcpp::Elf_types<size>::Elf_Addr address)
292 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
293 Valtype* wv = reinterpret_cast<Valtype*>(view);
294 Valtype x = psymval->value(object, addend);
295 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
298 typedef Relocate_functions<size, big_endian> This;
300 public:
301 // Do a simple 8-bit REL relocation with the addend in the section
302 // contents.
303 static inline void
304 rel8(unsigned char* view, unsigned char value)
305 { This::template rel<8>(view, value); }
307 static inline void
308 rel8(unsigned char* view,
309 const Sized_relobj<size, big_endian>* object,
310 const Symbol_value<size>* psymval)
311 { This::template rel<8>(view, object, psymval); }
313 // Do an 8-bit RELA relocation with the addend in the relocation.
314 static inline void
315 rela8(unsigned char* view, unsigned char value, unsigned char addend)
316 { This::template rela<8>(view, value, addend); }
318 static inline void
319 rela8(unsigned char* view,
320 const Sized_relobj<size, big_endian>* object,
321 const Symbol_value<size>* psymval,
322 unsigned char addend)
323 { This::template rela<8>(view, object, psymval, addend); }
325 // Do a simple 8-bit PC relative relocation with the addend in the
326 // section contents.
327 static inline void
328 pcrel8(unsigned char* view, unsigned char value,
329 typename elfcpp::Elf_types<size>::Elf_Addr address)
330 { This::template pcrel<8>(view, value, address); }
332 static inline void
333 pcrel8(unsigned char* view,
334 const Sized_relobj<size, big_endian>* object,
335 const Symbol_value<size>* psymval,
336 typename elfcpp::Elf_types<size>::Elf_Addr address)
337 { This::template pcrel<8>(view, object, psymval, address); }
339 // Do a simple 8-bit PC relative RELA relocation with the addend in
340 // the reloc.
341 static inline void
342 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
343 typename elfcpp::Elf_types<size>::Elf_Addr address)
344 { This::template pcrela<8>(view, value, addend, address); }
346 static inline void
347 pcrela8(unsigned char* view,
348 const Sized_relobj<size, big_endian>* object,
349 const Symbol_value<size>* psymval,
350 unsigned char addend,
351 typename elfcpp::Elf_types<size>::Elf_Addr address)
352 { This::template pcrela<8>(view, object, psymval, addend, address); }
354 // Do a simple 16-bit REL relocation with the addend in the section
355 // contents.
356 static inline void
357 rel16(unsigned char* view, elfcpp::Elf_Half value)
358 { This::template rel<16>(view, value); }
360 static inline void
361 rel16(unsigned char* view,
362 const Sized_relobj<size, big_endian>* object,
363 const Symbol_value<size>* psymval)
364 { This::template rel<16>(view, object, psymval); }
366 // Do an 16-bit RELA relocation with the addend in the relocation.
367 static inline void
368 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
369 { This::template rela<16>(view, value, addend); }
371 static inline void
372 rela16(unsigned char* view,
373 const Sized_relobj<size, big_endian>* object,
374 const Symbol_value<size>* psymval,
375 elfcpp::Elf_Half addend)
376 { This::template rela<16>(view, object, psymval, addend); }
378 // Do a simple 16-bit PC relative REL relocation with the addend in
379 // the section contents.
380 static inline void
381 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
382 typename elfcpp::Elf_types<size>::Elf_Addr address)
383 { This::template pcrel<16>(view, value, address); }
385 static inline void
386 pcrel16(unsigned char* view,
387 const Sized_relobj<size, big_endian>* object,
388 const Symbol_value<size>* psymval,
389 typename elfcpp::Elf_types<size>::Elf_Addr address)
390 { This::template pcrel<16>(view, object, psymval, address); }
392 // Do a simple 16-bit PC relative RELA relocation with the addend in
393 // the reloc.
394 static inline void
395 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
396 elfcpp::Elf_Half addend,
397 typename elfcpp::Elf_types<size>::Elf_Addr address)
398 { This::template pcrela<16>(view, value, addend, address); }
400 static inline void
401 pcrela16(unsigned char* view,
402 const Sized_relobj<size, big_endian>* object,
403 const Symbol_value<size>* psymval,
404 elfcpp::Elf_Half addend,
405 typename elfcpp::Elf_types<size>::Elf_Addr address)
406 { This::template pcrela<16>(view, object, psymval, addend, address); }
408 // Do a simple 32-bit REL relocation with the addend in the section
409 // contents.
410 static inline void
411 rel32(unsigned char* view, elfcpp::Elf_Word value)
412 { This::template rel<32>(view, value); }
414 static inline void
415 rel32(unsigned char* view,
416 const Sized_relobj<size, big_endian>* object,
417 const Symbol_value<size>* psymval)
418 { This::template rel<32>(view, object, psymval); }
420 // Do an 32-bit RELA relocation with the addend in the relocation.
421 static inline void
422 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
423 { This::template rela<32>(view, value, addend); }
425 static inline void
426 rela32(unsigned char* view,
427 const Sized_relobj<size, big_endian>* object,
428 const Symbol_value<size>* psymval,
429 elfcpp::Elf_Word addend)
430 { This::template rela<32>(view, object, psymval, addend); }
432 // Do a simple 32-bit PC relative REL relocation with the addend in
433 // the section contents.
434 static inline void
435 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
436 typename elfcpp::Elf_types<size>::Elf_Addr address)
437 { This::template pcrel<32>(view, value, address); }
439 static inline void
440 pcrel32(unsigned char* view,
441 const Sized_relobj<size, big_endian>* object,
442 const Symbol_value<size>* psymval,
443 typename elfcpp::Elf_types<size>::Elf_Addr address)
444 { This::template pcrel<32>(view, object, psymval, address); }
446 // Do a simple 32-bit PC relative RELA relocation with the addend in
447 // the relocation.
448 static inline void
449 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
450 elfcpp::Elf_Word addend,
451 typename elfcpp::Elf_types<size>::Elf_Addr address)
452 { This::template pcrela<32>(view, value, addend, address); }
454 static inline void
455 pcrela32(unsigned char* view,
456 const Sized_relobj<size, big_endian>* object,
457 const Symbol_value<size>* psymval,
458 elfcpp::Elf_Word addend,
459 typename elfcpp::Elf_types<size>::Elf_Addr address)
460 { This::template pcrela<32>(view, object, psymval, addend, address); }
462 // Do a simple 64-bit REL relocation with the addend in the section
463 // contents.
464 static inline void
465 rel64(unsigned char* view, elfcpp::Elf_Xword value)
466 { This::template rel<64>(view, value); }
468 static inline void
469 rel64(unsigned char* view,
470 const Sized_relobj<size, big_endian>* object,
471 const Symbol_value<size>* psymval)
472 { This::template rel<64>(view, object, psymval); }
474 // Do a 64-bit RELA relocation with the addend in the relocation.
475 static inline void
476 rela64(unsigned char* view, elfcpp::Elf_Xword value,
477 elfcpp::Elf_Xword addend)
478 { This::template rela<64>(view, value, addend); }
480 static inline void
481 rela64(unsigned char* view,
482 const Sized_relobj<size, big_endian>* object,
483 const Symbol_value<size>* psymval,
484 elfcpp::Elf_Xword addend)
485 { This::template rela<64>(view, object, psymval, addend); }
487 // Do a simple 64-bit PC relative REL relocation with the addend in
488 // the section contents.
489 static inline void
490 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
491 typename elfcpp::Elf_types<size>::Elf_Addr address)
492 { This::template pcrel<64>(view, value, address); }
494 static inline void
495 pcrel64(unsigned char* view,
496 const Sized_relobj<size, big_endian>* object,
497 const Symbol_value<size>* psymval,
498 typename elfcpp::Elf_types<size>::Elf_Addr address)
499 { This::template pcrel<64>(view, object, psymval, address); }
501 // Do a simple 64-bit PC relative RELA relocation with the addend in
502 // the relocation.
503 static inline void
504 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
505 elfcpp::Elf_Xword addend,
506 typename elfcpp::Elf_types<size>::Elf_Addr address)
507 { This::template pcrela<64>(view, value, addend, address); }
509 static inline void
510 pcrela64(unsigned char* view,
511 const Sized_relobj<size, big_endian>* object,
512 const Symbol_value<size>* psymval,
513 elfcpp::Elf_Xword addend,
514 typename elfcpp::Elf_types<size>::Elf_Addr address)
515 { This::template pcrela<64>(view, object, psymval, addend, address); }
518 // We try to avoid COPY relocations when possible. A COPY relocation
519 // may be required when an executable refers to a variable defined in
520 // a shared library. COPY relocations are problematic because they
521 // tie the executable to the exact size of the variable in the shared
522 // library. We can avoid them if all the references to the variable
523 // are in a writeable section. In that case we can simply use dynamic
524 // relocations. However, when scanning relocs, we don't know when we
525 // see the relocation whether we will be forced to use a COPY
526 // relocation or not. So we have to save the relocation during the
527 // reloc scanning, and then emit it as a dynamic relocation if
528 // necessary. This class implements that. It is used by the target
529 // specific code.
531 template<int size, bool big_endian>
532 class Copy_relocs
534 public:
535 Copy_relocs()
536 : entries_()
539 // Return whether we need a COPY reloc for a reloc against GSYM,
540 // which is being applied to section SHNDX in OBJECT.
541 static bool
542 need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
543 Sized_symbol<size>* gsym);
545 // Save a Rel against SYM for possible emission later. SHNDX is the
546 // index of the section to which the reloc is being applied.
547 void
548 save(Symbol* sym, Relobj*, unsigned int shndx,
549 Output_section* output_section, const elfcpp::Rel<size, big_endian>&);
551 // Save a Rela against SYM for possible emission later.
552 void
553 save(Symbol* sym, Relobj*, unsigned int shndx,
554 Output_section* output_section, const elfcpp::Rela<size, big_endian>&);
556 // Return whether there are any relocs to emit. This also discards
557 // entries which need not be emitted.
558 bool
559 any_to_emit();
561 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
562 // is still defined in the dynamic object).
563 template<int sh_type>
564 void
565 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
567 private:
568 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
569 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
571 // This POD class holds the entries we are saving.
572 class Copy_reloc_entry
574 public:
575 Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
576 Relobj* relobj, unsigned int shndx,
577 Output_section* output_section,
578 Address address, Addend addend)
579 : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
580 shndx_(shndx), output_section_(output_section),
581 address_(address), addend_(addend)
584 // Return whether we should emit this reloc. If we should not
585 // emit, we clear it.
586 bool
587 should_emit();
589 // Emit this reloc.
591 void
592 emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
594 void
595 emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
597 private:
598 Symbol* sym_;
599 unsigned int reloc_type_;
600 Relobj* relobj_;
601 unsigned int shndx_;
602 Output_section* output_section_;
603 Address address_;
604 Addend addend_;
607 // A list of relocs to be saved.
608 typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
610 // The list of relocs we are saving.
611 Copy_reloc_entries entries_;
614 // Track relocations while reading a section. This lets you ask for
615 // the relocation at a certain offset, and see how relocs occur
616 // between points of interest.
618 template<int size, bool big_endian>
619 class Track_relocs
621 public:
622 Track_relocs()
623 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
626 // Initialize the Track_relocs object. OBJECT is the object holding
627 // the reloc section, RELOC_SHNDX is the section index of the reloc
628 // section, and RELOC_TYPE is the type of the reloc section
629 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
630 // something went wrong.
631 bool
632 initialize(Object* object, unsigned int reloc_shndx,
633 unsigned int reloc_type);
635 // Return the offset in the data section to which the next reloc
636 // applies. THis returns -1 if there is no next reloc.
637 off_t
638 next_offset() const;
640 // Return the symbol index of the next reloc. This returns -1U if
641 // there is no next reloc.
642 unsigned int
643 next_symndx() const;
645 // Advance to OFFSET within the data section, and return the number
646 // of relocs which would be skipped.
648 advance(off_t offset);
650 private:
651 // The contents of the input object's reloc section.
652 const unsigned char* prelocs_;
653 // The length of the reloc section.
654 section_size_type len_;
655 // Our current position in the reloc section.
656 section_size_type pos_;
657 // The size of the relocs in the section.
658 int reloc_size_;
661 } // End namespace gold.
663 #endif // !defined(GOLD_RELOC_H)