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 // Discard the input reloc--process it completely when relocating
251 // the data section contents.
253 // An input reloc which is not discarded, but which requires
254 // target specific processing in order to update it.
259 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
262 // Record the number of relocs.
264 set_reloc_count(size_t reloc_count)
265 { this->reloc_strategies_.reserve(reloc_count); }
267 // Record what to do for the next reloc.
269 set_next_reloc_strategy(Reloc_strategy strategy)
271 this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
272 if (strategy != RELOC_DISCARD)
273 ++this->output_reloc_count_;
276 // Record the Output_data associated with this reloc section.
278 set_output_data(Output_data* posd)
280 gold_assert(this->posd_ == NULL);
284 // Return the Output_data associated with this reloc section.
287 { return this->posd_; }
289 // Return what to do for reloc I.
291 strategy(unsigned int i) const
293 gold_assert(i < this->reloc_strategies_.size());
294 return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
297 // Return the number of relocations to create in the output file.
299 output_reloc_count() const
300 { return this->output_reloc_count_; }
303 typedef std::vector<unsigned char> Reloc_strategies;
305 // The strategies for the input reloc. There is one entry in this
306 // vector for each relocation in the input section.
307 Reloc_strategies reloc_strategies_;
308 // The number of relocations to be created in the output file.
309 size_t output_reloc_count_;
310 // The output data structure associated with this relocation.
314 // Standard relocation routines which are used on many targets. Here
315 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
317 template<int size, bool big_endian>
318 class Relocate_functions
321 // Do a simple relocation with the addend in the section contents.
322 // VALSIZE is the size of the value.
323 template<int valsize>
325 rel(unsigned char* view,
326 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
328 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
329 Valtype* wv = reinterpret_cast<Valtype*>(view);
330 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
331 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
334 // Do a simple relocation using a Symbol_value with the addend in
335 // the section contents. VALSIZE is the size of the value to
337 template<int valsize>
339 rel(unsigned char* view,
340 const Sized_relobj_file<size, big_endian>* object,
341 const Symbol_value<size>* psymval)
343 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
344 Valtype* wv = reinterpret_cast<Valtype*>(view);
345 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
346 x = psymval->value(object, x);
347 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
350 // Do a simple relocation with the addend in the relocation.
351 // VALSIZE is the size of the value.
352 template<int valsize>
354 rela(unsigned char* view,
355 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
356 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
358 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
359 Valtype* wv = reinterpret_cast<Valtype*>(view);
360 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
363 // Do a simple relocation using a symbol value with the addend in
364 // the relocation. VALSIZE is the size of the value.
365 template<int valsize>
367 rela(unsigned char* view,
368 const Sized_relobj_file<size, big_endian>* object,
369 const Symbol_value<size>* psymval,
370 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
372 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
373 Valtype* wv = reinterpret_cast<Valtype*>(view);
374 Valtype x = psymval->value(object, addend);
375 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
378 // Do a simple PC relative relocation with the addend in the section
379 // contents. VALSIZE is the size of the value.
380 template<int valsize>
382 pcrel(unsigned char* view,
383 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
384 typename elfcpp::Elf_types<size>::Elf_Addr address)
386 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
387 Valtype* wv = reinterpret_cast<Valtype*>(view);
388 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
389 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
392 // Do a simple PC relative relocation with a Symbol_value with the
393 // addend in the section contents. VALSIZE is the size of the
395 template<int valsize>
397 pcrel(unsigned char* view,
398 const Sized_relobj_file<size, big_endian>* object,
399 const Symbol_value<size>* psymval,
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 x = psymval->value(object, x);
406 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
409 // Do a simple PC relative relocation with the addend in the
410 // relocation. VALSIZE is the size of the value.
411 template<int valsize>
413 pcrela(unsigned char* view,
414 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
415 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
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 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
423 // Do a simple PC relative relocation with a Symbol_value with the
424 // addend in the relocation. VALSIZE is the size of the value.
425 template<int valsize>
427 pcrela(unsigned char* view,
428 const Sized_relobj_file<size, big_endian>* object,
429 const Symbol_value<size>* psymval,
430 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
431 typename elfcpp::Elf_types<size>::Elf_Addr address)
433 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
434 Valtype* wv = reinterpret_cast<Valtype*>(view);
435 Valtype x = psymval->value(object, addend);
436 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
439 typedef Relocate_functions<size, big_endian> This;
442 // Do a simple 8-bit REL relocation with the addend in the section
445 rel8(unsigned char* view, unsigned char value)
446 { This::template rel<8>(view, value); }
449 rel8(unsigned char* view,
450 const Sized_relobj_file<size, big_endian>* object,
451 const Symbol_value<size>* psymval)
452 { This::template rel<8>(view, object, psymval); }
454 // Do an 8-bit RELA relocation with the addend in the relocation.
456 rela8(unsigned char* view, unsigned char value, unsigned char addend)
457 { This::template rela<8>(view, value, addend); }
460 rela8(unsigned char* view,
461 const Sized_relobj_file<size, big_endian>* object,
462 const Symbol_value<size>* psymval,
463 unsigned char addend)
464 { This::template rela<8>(view, object, psymval, addend); }
466 // Do a simple 8-bit PC relative relocation with the addend in the
469 pcrel8(unsigned char* view, unsigned char value,
470 typename elfcpp::Elf_types<size>::Elf_Addr address)
471 { This::template pcrel<8>(view, value, address); }
474 pcrel8(unsigned char* view,
475 const Sized_relobj_file<size, big_endian>* object,
476 const Symbol_value<size>* psymval,
477 typename elfcpp::Elf_types<size>::Elf_Addr address)
478 { This::template pcrel<8>(view, object, psymval, address); }
480 // Do a simple 8-bit PC relative RELA relocation with the addend in
483 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
484 typename elfcpp::Elf_types<size>::Elf_Addr address)
485 { This::template pcrela<8>(view, value, addend, address); }
488 pcrela8(unsigned char* view,
489 const Sized_relobj_file<size, big_endian>* object,
490 const Symbol_value<size>* psymval,
491 unsigned char addend,
492 typename elfcpp::Elf_types<size>::Elf_Addr address)
493 { This::template pcrela<8>(view, object, psymval, addend, address); }
495 // Do a simple 16-bit REL relocation with the addend in the section
498 rel16(unsigned char* view, elfcpp::Elf_Half value)
499 { This::template rel<16>(view, value); }
502 rel16(unsigned char* view,
503 const Sized_relobj_file<size, big_endian>* object,
504 const Symbol_value<size>* psymval)
505 { This::template rel<16>(view, object, psymval); }
507 // Do an 16-bit RELA relocation with the addend in the relocation.
509 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
510 { This::template rela<16>(view, value, addend); }
513 rela16(unsigned char* view,
514 const Sized_relobj_file<size, big_endian>* object,
515 const Symbol_value<size>* psymval,
516 elfcpp::Elf_Half addend)
517 { This::template rela<16>(view, object, psymval, addend); }
519 // Do a simple 16-bit PC relative REL relocation with the addend in
520 // the section contents.
522 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
523 typename elfcpp::Elf_types<size>::Elf_Addr address)
524 { This::template pcrel<16>(view, value, address); }
527 pcrel16(unsigned char* view,
528 const Sized_relobj_file<size, big_endian>* object,
529 const Symbol_value<size>* psymval,
530 typename elfcpp::Elf_types<size>::Elf_Addr address)
531 { This::template pcrel<16>(view, object, psymval, address); }
533 // Do a simple 16-bit PC relative RELA relocation with the addend in
536 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
537 elfcpp::Elf_Half addend,
538 typename elfcpp::Elf_types<size>::Elf_Addr address)
539 { This::template pcrela<16>(view, value, addend, address); }
542 pcrela16(unsigned char* view,
543 const Sized_relobj_file<size, big_endian>* object,
544 const Symbol_value<size>* psymval,
545 elfcpp::Elf_Half addend,
546 typename elfcpp::Elf_types<size>::Elf_Addr address)
547 { This::template pcrela<16>(view, object, psymval, addend, address); }
549 // Do a simple 32-bit REL relocation with the addend in the section
552 rel32(unsigned char* view, elfcpp::Elf_Word value)
553 { This::template rel<32>(view, value); }
556 rel32(unsigned char* view,
557 const Sized_relobj_file<size, big_endian>* object,
558 const Symbol_value<size>* psymval)
559 { This::template rel<32>(view, object, psymval); }
561 // Do an 32-bit RELA relocation with the addend in the relocation.
563 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
564 { This::template rela<32>(view, value, addend); }
567 rela32(unsigned char* view,
568 const Sized_relobj_file<size, big_endian>* object,
569 const Symbol_value<size>* psymval,
570 elfcpp::Elf_Word addend)
571 { This::template rela<32>(view, object, psymval, addend); }
573 // Do a simple 32-bit PC relative REL relocation with the addend in
574 // the section contents.
576 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
577 typename elfcpp::Elf_types<size>::Elf_Addr address)
578 { This::template pcrel<32>(view, value, address); }
581 pcrel32(unsigned char* view,
582 const Sized_relobj_file<size, big_endian>* object,
583 const Symbol_value<size>* psymval,
584 typename elfcpp::Elf_types<size>::Elf_Addr address)
585 { This::template pcrel<32>(view, object, psymval, address); }
587 // Do a simple 32-bit PC relative RELA relocation with the addend in
590 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
591 elfcpp::Elf_Word addend,
592 typename elfcpp::Elf_types<size>::Elf_Addr address)
593 { This::template pcrela<32>(view, value, addend, address); }
596 pcrela32(unsigned char* view,
597 const Sized_relobj_file<size, big_endian>* object,
598 const Symbol_value<size>* psymval,
599 elfcpp::Elf_Word addend,
600 typename elfcpp::Elf_types<size>::Elf_Addr address)
601 { This::template pcrela<32>(view, object, psymval, addend, address); }
603 // Do a simple 64-bit REL relocation with the addend in the section
606 rel64(unsigned char* view, elfcpp::Elf_Xword value)
607 { This::template rel<64>(view, value); }
610 rel64(unsigned char* view,
611 const Sized_relobj_file<size, big_endian>* object,
612 const Symbol_value<size>* psymval)
613 { This::template rel<64>(view, object, psymval); }
615 // Do a 64-bit RELA relocation with the addend in the relocation.
617 rela64(unsigned char* view, elfcpp::Elf_Xword value,
618 elfcpp::Elf_Xword addend)
619 { This::template rela<64>(view, value, addend); }
622 rela64(unsigned char* view,
623 const Sized_relobj_file<size, big_endian>* object,
624 const Symbol_value<size>* psymval,
625 elfcpp::Elf_Xword addend)
626 { This::template rela<64>(view, object, psymval, addend); }
628 // Do a simple 64-bit PC relative REL relocation with the addend in
629 // the section contents.
631 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
632 typename elfcpp::Elf_types<size>::Elf_Addr address)
633 { This::template pcrel<64>(view, value, address); }
636 pcrel64(unsigned char* view,
637 const Sized_relobj_file<size, big_endian>* object,
638 const Symbol_value<size>* psymval,
639 typename elfcpp::Elf_types<size>::Elf_Addr address)
640 { This::template pcrel<64>(view, object, psymval, address); }
642 // Do a simple 64-bit PC relative RELA relocation with the addend in
645 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
646 elfcpp::Elf_Xword addend,
647 typename elfcpp::Elf_types<size>::Elf_Addr address)
648 { This::template pcrela<64>(view, value, addend, address); }
651 pcrela64(unsigned char* view,
652 const Sized_relobj_file<size, big_endian>* object,
653 const Symbol_value<size>* psymval,
654 elfcpp::Elf_Xword addend,
655 typename elfcpp::Elf_types<size>::Elf_Addr address)
656 { This::template pcrela<64>(view, object, psymval, addend, address); }
659 // Track relocations while reading a section. This lets you ask for
660 // the relocation at a certain offset, and see how relocs occur
661 // between points of interest.
663 template<int size, bool big_endian>
668 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
671 // Initialize the Track_relocs object. OBJECT is the object holding
672 // the reloc section, RELOC_SHNDX is the section index of the reloc
673 // section, and RELOC_TYPE is the type of the reloc section
674 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
675 // something went wrong.
677 initialize(Object* object, unsigned int reloc_shndx,
678 unsigned int reloc_type);
680 // Return the offset in the data section to which the next reloc
681 // applies. This returns -1 if there is no next reloc.
685 // Return the symbol index of the next reloc. This returns -1U if
686 // there is no next reloc.
690 // Return the addend of the next reloc. This returns 0 if there is
695 // Advance to OFFSET within the data section, and return the number
696 // of relocs which would be skipped.
698 advance(off_t offset);
701 // The contents of the input object's reloc section.
702 const unsigned char* prelocs_;
703 // The length of the reloc section.
704 section_size_type len_;
705 // Our current position in the reloc section.
706 section_size_type pos_;
707 // The size of the relocs in the section.
711 } // End namespace gold.
713 #endif // !defined(GOLD_RELOC_H)