1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010 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.
27 #ifdef HAVE_BYTESWAP_H
32 #include "workqueue.h"
37 class General_options;
40 class Read_relocs_data;
49 template<int size, bool big_endian>
50 class Sized_relobj_file;
55 template<int sh_type, bool dynamic, int size, bool big_endian>
56 class Output_data_reloc;
58 // A class to read the relocations for an object file, and then queue
59 // up a task to see if they require any GOT/PLT/COPY relocations in
62 class Read_relocs : public Task
65 // THIS_BLOCKER and NEXT_BLOCKER are passed along to a Scan_relocs
66 // or Gc_process_relocs task, so that they run in a deterministic
68 Read_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
69 Task_token* this_blocker, Task_token* next_blocker)
70 : symtab_(symtab), layout_(layout), object_(object),
71 this_blocker_(this_blocker), next_blocker_(next_blocker)
74 // The standard Task methods.
89 Symbol_table* symtab_;
92 Task_token* this_blocker_;
93 Task_token* next_blocker_;
96 // Process the relocs to figure out which sections are garbage.
97 // Very similar to scan relocs.
99 class Gc_process_relocs : public Task
102 // THIS_BLOCKER prevents this task from running until the previous
103 // one is finished. NEXT_BLOCKER prevents the next task from
105 Gc_process_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
106 Read_relocs_data* rd, Task_token* this_blocker,
107 Task_token* next_blocker)
108 : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
109 this_blocker_(this_blocker), next_blocker_(next_blocker)
112 ~Gc_process_relocs();
114 // The standard Task methods.
129 Symbol_table* symtab_;
132 Read_relocs_data* rd_;
133 Task_token* this_blocker_;
134 Task_token* next_blocker_;
137 // Scan the relocations for an object to see if they require any
138 // GOT/PLT/COPY relocations.
140 class Scan_relocs : public Task
143 // THIS_BLOCKER prevents this task from running until the previous
144 // one is finished. NEXT_BLOCKER prevents the next task from
146 Scan_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
147 Read_relocs_data* rd, Task_token* this_blocker,
148 Task_token* next_blocker)
149 : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
150 this_blocker_(this_blocker), next_blocker_(next_blocker)
155 // The standard Task methods.
170 Symbol_table* symtab_;
173 Read_relocs_data* rd_;
174 Task_token* this_blocker_;
175 Task_token* next_blocker_;
178 // A class to perform all the relocations for an object file.
180 class Relocate_task : public Task
183 Relocate_task(const Symbol_table* symtab, const Layout* layout,
184 Relobj* object, Output_file* of,
185 Task_token* input_sections_blocker,
186 Task_token* output_sections_blocker, Task_token* final_blocker)
187 : symtab_(symtab), layout_(layout), object_(object), of_(of),
188 input_sections_blocker_(input_sections_blocker),
189 output_sections_blocker_(output_sections_blocker),
190 final_blocker_(final_blocker)
193 // The standard Task methods.
208 const Symbol_table* symtab_;
209 const Layout* layout_;
212 Task_token* input_sections_blocker_;
213 Task_token* output_sections_blocker_;
214 Task_token* final_blocker_;
217 // During a relocatable link, this class records how relocations
218 // should be handled for a single input reloc section. An instance of
219 // this class is created while scanning relocs, and it is used while
220 // processing relocs.
222 class Relocatable_relocs
225 // We use a vector of unsigned char to indicate how the input relocs
226 // should be handled. Each element is one of the following values.
227 // We create this vector when we initially scan the relocations.
230 // Copy the input reloc. Don't modify it other than updating the
231 // r_offset field and the r_sym part of the r_info field.
233 // Copy the input reloc which is against an STT_SECTION symbol.
234 // Update the r_offset and r_sym part of the r_info field. Adjust
235 // the addend by subtracting the value of the old local symbol and
236 // adding the value of the new local symbol. The addend is in the
237 // SHT_RELA reloc and the contents of the data section do not need
239 RELOC_ADJUST_FOR_SECTION_RELA,
240 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
242 RELOC_ADJUST_FOR_SECTION_0,
243 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
244 // section need to be changed. The number indicates the number of
245 // bytes in the addend in the section contents.
246 RELOC_ADJUST_FOR_SECTION_1,
247 RELOC_ADJUST_FOR_SECTION_2,
248 RELOC_ADJUST_FOR_SECTION_4,
249 RELOC_ADJUST_FOR_SECTION_8,
250 // Like RELOC_ADJUST_FOR_SECTION_4 but for unaligned relocs.
251 RELOC_ADJUST_FOR_SECTION_4_UNALIGNED,
252 // Discard the input reloc--process it completely when relocating
253 // the data section contents.
255 // An input reloc which is not discarded, but which requires
256 // target specific processing in order to update it.
261 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
264 // Record the number of relocs.
266 set_reloc_count(size_t reloc_count)
267 { this->reloc_strategies_.reserve(reloc_count); }
269 // Record what to do for the next reloc.
271 set_next_reloc_strategy(Reloc_strategy strategy)
273 this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
274 if (strategy != RELOC_DISCARD)
275 ++this->output_reloc_count_;
278 // Record the Output_data associated with this reloc section.
280 set_output_data(Output_data* posd)
282 gold_assert(this->posd_ == NULL);
286 // Return the Output_data associated with this reloc section.
289 { return this->posd_; }
291 // Return what to do for reloc I.
293 strategy(unsigned int i) const
295 gold_assert(i < this->reloc_strategies_.size());
296 return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
299 // Return the number of relocations to create in the output file.
301 output_reloc_count() const
302 { return this->output_reloc_count_; }
305 typedef std::vector<unsigned char> Reloc_strategies;
307 // The strategies for the input reloc. There is one entry in this
308 // vector for each relocation in the input section.
309 Reloc_strategies reloc_strategies_;
310 // The number of relocations to be created in the output file.
311 size_t output_reloc_count_;
312 // The output data structure associated with this relocation.
316 // Standard relocation routines which are used on many targets. Here
317 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
319 template<int size, bool big_endian>
320 class Relocate_functions
323 // Do a simple relocation with the addend in the section contents.
324 // VALSIZE is the size of the value.
325 template<int valsize>
327 rel(unsigned char* view,
328 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
330 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
331 Valtype* wv = reinterpret_cast<Valtype*>(view);
332 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
333 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
336 // Do a simple relocation using a Symbol_value with the addend in
337 // the section contents. VALSIZE is the size of the value to
339 template<int valsize>
341 rel(unsigned char* view,
342 const Sized_relobj_file<size, big_endian>* object,
343 const Symbol_value<size>* psymval)
345 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
346 Valtype* wv = reinterpret_cast<Valtype*>(view);
347 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
348 x = psymval->value(object, x);
349 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
352 // Like the above but for relocs at unaligned addresses.
353 template<int valsize>
355 rel_unaligned(unsigned char* view,
356 const Sized_relobj_file<size, big_endian>* object,
357 const Symbol_value<size>* psymval)
359 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
361 Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
362 x = psymval->value(object, x);
363 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x);
366 // Do a simple relocation with the addend in the relocation.
367 // VALSIZE is the size of the value.
368 template<int valsize>
370 rela(unsigned char* view,
371 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
372 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
374 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
375 Valtype* wv = reinterpret_cast<Valtype*>(view);
376 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
379 // Do a simple relocation using a symbol value with the addend in
380 // the relocation. VALSIZE is the size of the value.
381 template<int valsize>
383 rela(unsigned char* view,
384 const Sized_relobj_file<size, big_endian>* object,
385 const Symbol_value<size>* psymval,
386 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
388 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
389 Valtype* wv = reinterpret_cast<Valtype*>(view);
390 Valtype x = psymval->value(object, addend);
391 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
394 // Do a simple PC relative relocation with the addend in the section
395 // contents. VALSIZE is the size of the value.
396 template<int valsize>
398 pcrel(unsigned char* view,
399 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
400 typename elfcpp::Elf_types<size>::Elf_Addr address)
402 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
403 Valtype* wv = reinterpret_cast<Valtype*>(view);
404 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
405 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
408 // Do a simple PC relative relocation with a Symbol_value with the
409 // addend in the section contents. VALSIZE is the size of the
411 template<int valsize>
413 pcrel(unsigned char* view,
414 const Sized_relobj_file<size, big_endian>* object,
415 const Symbol_value<size>* psymval,
416 typename elfcpp::Elf_types<size>::Elf_Addr address)
418 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
419 Valtype* wv = reinterpret_cast<Valtype*>(view);
420 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
421 x = psymval->value(object, x);
422 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
425 // Do a simple PC relative relocation with the addend in the
426 // relocation. VALSIZE is the size of the value.
427 template<int valsize>
429 pcrela(unsigned char* view,
430 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
431 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
432 typename elfcpp::Elf_types<size>::Elf_Addr address)
434 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
435 Valtype* wv = reinterpret_cast<Valtype*>(view);
436 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
439 // Do a simple PC relative relocation with a Symbol_value with the
440 // addend in the relocation. VALSIZE is the size of the value.
441 template<int valsize>
443 pcrela(unsigned char* view,
444 const Sized_relobj_file<size, big_endian>* object,
445 const Symbol_value<size>* psymval,
446 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
447 typename elfcpp::Elf_types<size>::Elf_Addr address)
449 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
450 Valtype* wv = reinterpret_cast<Valtype*>(view);
451 Valtype x = psymval->value(object, addend);
452 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
455 typedef Relocate_functions<size, big_endian> This;
458 // Do a simple 8-bit REL relocation with the addend in the section
461 rel8(unsigned char* view, unsigned char value)
462 { This::template rel<8>(view, value); }
465 rel8(unsigned char* view,
466 const Sized_relobj_file<size, big_endian>* object,
467 const Symbol_value<size>* psymval)
468 { This::template rel<8>(view, object, psymval); }
470 // Do an 8-bit RELA relocation with the addend in the relocation.
472 rela8(unsigned char* view, unsigned char value, unsigned char addend)
473 { This::template rela<8>(view, value, addend); }
476 rela8(unsigned char* view,
477 const Sized_relobj_file<size, big_endian>* object,
478 const Symbol_value<size>* psymval,
479 unsigned char addend)
480 { This::template rela<8>(view, object, psymval, addend); }
482 // Do a simple 8-bit PC relative relocation with the addend in the
485 pcrel8(unsigned char* view, unsigned char value,
486 typename elfcpp::Elf_types<size>::Elf_Addr address)
487 { This::template pcrel<8>(view, value, address); }
490 pcrel8(unsigned char* view,
491 const Sized_relobj_file<size, big_endian>* object,
492 const Symbol_value<size>* psymval,
493 typename elfcpp::Elf_types<size>::Elf_Addr address)
494 { This::template pcrel<8>(view, object, psymval, address); }
496 // Do a simple 8-bit PC relative RELA relocation with the addend in
499 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
500 typename elfcpp::Elf_types<size>::Elf_Addr address)
501 { This::template pcrela<8>(view, value, addend, address); }
504 pcrela8(unsigned char* view,
505 const Sized_relobj_file<size, big_endian>* object,
506 const Symbol_value<size>* psymval,
507 unsigned char addend,
508 typename elfcpp::Elf_types<size>::Elf_Addr address)
509 { This::template pcrela<8>(view, object, psymval, addend, address); }
511 // Do a simple 16-bit REL relocation with the addend in the section
514 rel16(unsigned char* view, elfcpp::Elf_Half value)
515 { This::template rel<16>(view, value); }
518 rel16(unsigned char* view,
519 const Sized_relobj_file<size, big_endian>* object,
520 const Symbol_value<size>* psymval)
521 { This::template rel<16>(view, object, psymval); }
523 // Do an 16-bit RELA relocation with the addend in the relocation.
525 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
526 { This::template rela<16>(view, value, addend); }
529 rela16(unsigned char* view,
530 const Sized_relobj_file<size, big_endian>* object,
531 const Symbol_value<size>* psymval,
532 elfcpp::Elf_Half addend)
533 { This::template rela<16>(view, object, psymval, addend); }
535 // Do a simple 16-bit PC relative REL relocation with the addend in
536 // the section contents.
538 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
539 typename elfcpp::Elf_types<size>::Elf_Addr address)
540 { This::template pcrel<16>(view, value, address); }
543 pcrel16(unsigned char* view,
544 const Sized_relobj_file<size, big_endian>* object,
545 const Symbol_value<size>* psymval,
546 typename elfcpp::Elf_types<size>::Elf_Addr address)
547 { This::template pcrel<16>(view, object, psymval, address); }
549 // Do a simple 16-bit PC relative RELA relocation with the addend in
552 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
553 elfcpp::Elf_Half addend,
554 typename elfcpp::Elf_types<size>::Elf_Addr address)
555 { This::template pcrela<16>(view, value, addend, address); }
558 pcrela16(unsigned char* view,
559 const Sized_relobj_file<size, big_endian>* object,
560 const Symbol_value<size>* psymval,
561 elfcpp::Elf_Half addend,
562 typename elfcpp::Elf_types<size>::Elf_Addr address)
563 { This::template pcrela<16>(view, object, psymval, addend, address); }
565 // Do a simple 32-bit REL relocation with the addend in the section
568 rel32(unsigned char* view, elfcpp::Elf_Word value)
569 { This::template rel<32>(view, value); }
572 rel32(unsigned char* view,
573 const Sized_relobj_file<size, big_endian>* object,
574 const Symbol_value<size>* psymval)
575 { This::template rel<32>(view, object, psymval); }
577 // Like above but for relocs at unaligned addresses.
579 rel32_unaligned(unsigned char* view,
580 const Sized_relobj_file<size, big_endian>* object,
581 const Symbol_value<size>* psymval)
582 { This::template rel_unaligned<32>(view, object, psymval); }
584 // Do an 32-bit RELA relocation with the addend in the relocation.
586 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
587 { This::template rela<32>(view, value, addend); }
590 rela32(unsigned char* view,
591 const Sized_relobj_file<size, big_endian>* object,
592 const Symbol_value<size>* psymval,
593 elfcpp::Elf_Word addend)
594 { This::template rela<32>(view, object, psymval, addend); }
596 // Do a simple 32-bit PC relative REL relocation with the addend in
597 // the section contents.
599 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
600 typename elfcpp::Elf_types<size>::Elf_Addr address)
601 { This::template pcrel<32>(view, value, address); }
604 pcrel32(unsigned char* view,
605 const Sized_relobj_file<size, big_endian>* object,
606 const Symbol_value<size>* psymval,
607 typename elfcpp::Elf_types<size>::Elf_Addr address)
608 { This::template pcrel<32>(view, object, psymval, address); }
610 // Do a simple 32-bit PC relative RELA relocation with the addend in
613 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
614 elfcpp::Elf_Word addend,
615 typename elfcpp::Elf_types<size>::Elf_Addr address)
616 { This::template pcrela<32>(view, value, addend, address); }
619 pcrela32(unsigned char* view,
620 const Sized_relobj_file<size, big_endian>* object,
621 const Symbol_value<size>* psymval,
622 elfcpp::Elf_Word addend,
623 typename elfcpp::Elf_types<size>::Elf_Addr address)
624 { This::template pcrela<32>(view, object, psymval, addend, address); }
626 // Do a simple 64-bit REL relocation with the addend in the section
629 rel64(unsigned char* view, elfcpp::Elf_Xword value)
630 { This::template rel<64>(view, value); }
633 rel64(unsigned char* view,
634 const Sized_relobj_file<size, big_endian>* object,
635 const Symbol_value<size>* psymval)
636 { This::template rel<64>(view, object, psymval); }
638 // Do a 64-bit RELA relocation with the addend in the relocation.
640 rela64(unsigned char* view, elfcpp::Elf_Xword value,
641 elfcpp::Elf_Xword addend)
642 { This::template rela<64>(view, value, addend); }
645 rela64(unsigned char* view,
646 const Sized_relobj_file<size, big_endian>* object,
647 const Symbol_value<size>* psymval,
648 elfcpp::Elf_Xword addend)
649 { This::template rela<64>(view, object, psymval, addend); }
651 // Do a simple 64-bit PC relative REL relocation with the addend in
652 // the section contents.
654 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
655 typename elfcpp::Elf_types<size>::Elf_Addr address)
656 { This::template pcrel<64>(view, value, address); }
659 pcrel64(unsigned char* view,
660 const Sized_relobj_file<size, big_endian>* object,
661 const Symbol_value<size>* psymval,
662 typename elfcpp::Elf_types<size>::Elf_Addr address)
663 { This::template pcrel<64>(view, object, psymval, address); }
665 // Do a simple 64-bit PC relative RELA relocation with the addend in
668 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
669 elfcpp::Elf_Xword addend,
670 typename elfcpp::Elf_types<size>::Elf_Addr address)
671 { This::template pcrela<64>(view, value, addend, address); }
674 pcrela64(unsigned char* view,
675 const Sized_relobj_file<size, big_endian>* object,
676 const Symbol_value<size>* psymval,
677 elfcpp::Elf_Xword addend,
678 typename elfcpp::Elf_types<size>::Elf_Addr address)
679 { This::template pcrela<64>(view, object, psymval, addend, address); }
682 // Track relocations while reading a section. This lets you ask for
683 // the relocation at a certain offset, and see how relocs occur
684 // between points of interest.
686 template<int size, bool big_endian>
691 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
694 // Initialize the Track_relocs object. OBJECT is the object holding
695 // the reloc section, RELOC_SHNDX is the section index of the reloc
696 // section, and RELOC_TYPE is the type of the reloc section
697 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
698 // something went wrong.
700 initialize(Object* object, unsigned int reloc_shndx,
701 unsigned int reloc_type);
703 // Return the offset in the data section to which the next reloc
704 // applies. This returns -1 if there is no next reloc.
708 // Return the symbol index of the next reloc. This returns -1U if
709 // there is no next reloc.
713 // Return the addend of the next reloc. This returns 0 if there is
718 // Advance to OFFSET within the data section, and return the number
719 // of relocs which would be skipped.
721 advance(off_t offset);
724 // The contents of the input object's reloc section.
725 const unsigned char* prelocs_;
726 // The length of the reloc section.
727 section_size_type len_;
728 // Our current position in the reloc section.
729 section_size_type pos_;
730 // The size of the relocs in the section.
734 } // End namespace gold.
736 #endif // !defined(GOLD_RELOC_H)