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.
30 #include "workqueue.h"
35 class General_options;
38 class Read_relocs_data;
47 template<int size, bool big_endian>
53 template<int sh_type, bool dynamic, int size, bool big_endian>
54 class Output_data_reloc;
56 // A class to read the relocations for an object file, and then queue
57 // up a task to see if they require any GOT/PLT/COPY relocations in
60 class Read_relocs : public Task
63 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
64 // unblocked when the Scan_relocs task completes.
65 Read_relocs(const General_options& options, Symbol_table* symtab,
66 Layout* layout, Relobj* object, Task_token* symtab_lock,
68 : options_(options), symtab_(symtab), layout_(layout), object_(object),
69 symtab_lock_(symtab_lock), blocker_(blocker)
72 // The standard Task methods.
87 const General_options& options_;
88 Symbol_table* symtab_;
91 Task_token* symtab_lock_;
95 // Scan the relocations for an object to see if they require any
96 // GOT/PLT/COPY relocations.
98 class Scan_relocs : public Task
101 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
102 // unblocked when the task completes.
103 Scan_relocs(const General_options& options, Symbol_table* symtab,
104 Layout* layout, Relobj* object, Read_relocs_data* rd,
105 Task_token* symtab_lock, Task_token* blocker)
106 : options_(options), symtab_(symtab), layout_(layout), object_(object),
107 rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker)
110 // The standard Task methods.
125 const General_options& options_;
126 Symbol_table* symtab_;
129 Read_relocs_data* rd_;
130 Task_token* symtab_lock_;
131 Task_token* blocker_;
134 // A class to perform all the relocations for an object file.
136 class Relocate_task : public Task
139 Relocate_task(const General_options& options, const Symbol_table* symtab,
140 const Layout* layout, Relobj* object, Output_file* of,
141 Task_token* input_sections_blocker,
142 Task_token* output_sections_blocker, Task_token* final_blocker)
143 : options_(options), symtab_(symtab), layout_(layout), object_(object),
144 of_(of), input_sections_blocker_(input_sections_blocker),
145 output_sections_blocker_(output_sections_blocker),
146 final_blocker_(final_blocker)
149 // The standard Task methods.
164 const General_options& options_;
165 const Symbol_table* symtab_;
166 const Layout* layout_;
169 Task_token* input_sections_blocker_;
170 Task_token* output_sections_blocker_;
171 Task_token* final_blocker_;
174 // During a relocatable link, this class records how relocations
175 // should be handled for a single input reloc section. An instance of
176 // this class is created while scanning relocs, and it is used while
177 // processing relocs.
179 class Relocatable_relocs
182 // We use a vector of unsigned char to indicate how the input relocs
183 // should be handled. Each element is one of the following values.
184 // We create this vector when we initially scan the relocations.
187 // Copy the input reloc. Don't modify it other than updating the
188 // r_offset field and the r_sym part of the r_info field.
190 // Copy the input reloc which is against an STT_SECTION symbol.
191 // Update the r_offset and r_sym part of the r_info field. Adjust
192 // the addend by subtracting the value of the old local symbol and
193 // adding the value of the new local symbol. The addend is in the
194 // SHT_RELA reloc and the contents of the data section do not need
196 RELOC_ADJUST_FOR_SECTION_RELA,
197 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
199 RELOC_ADJUST_FOR_SECTION_0,
200 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
201 // section need to be changed. The number indicates the number of
202 // bytes in the addend in the section contents.
203 RELOC_ADJUST_FOR_SECTION_1,
204 RELOC_ADJUST_FOR_SECTION_2,
205 RELOC_ADJUST_FOR_SECTION_4,
206 RELOC_ADJUST_FOR_SECTION_8,
207 // Discard the input reloc--process it completely when relocating
208 // the data section contents.
210 // An input reloc which is not discarded, but which requires
211 // target specific processing in order to update it.
216 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
219 // Record the number of relocs.
221 set_reloc_count(size_t reloc_count)
222 { this->reloc_strategies_.reserve(reloc_count); }
224 // Record what to do for the next reloc.
226 set_next_reloc_strategy(Reloc_strategy strategy)
228 this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
229 if (strategy != RELOC_DISCARD)
230 ++this->output_reloc_count_;
233 // Record the Output_data associated with this reloc section.
235 set_output_data(Output_data* posd)
237 gold_assert(this->posd_ == NULL);
241 // Return the Output_data associated with this reloc section.
244 { return this->posd_; }
246 // Return what to do for reloc I.
248 strategy(unsigned int i) const
250 gold_assert(i < this->reloc_strategies_.size());
251 return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
254 // Return the number of relocations to create in the output file.
256 output_reloc_count() const
257 { return this->output_reloc_count_; }
260 typedef std::vector<unsigned char> Reloc_strategies;
262 // The strategies for the input reloc. There is one entry in this
263 // vector for each relocation in the input section.
264 Reloc_strategies reloc_strategies_;
265 // The number of relocations to be created in the output file.
266 size_t output_reloc_count_;
267 // The output data structure associated with this relocation.
271 // Standard relocation routines which are used on many targets. Here
272 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
274 template<int size, bool big_endian>
275 class Relocate_functions
278 // Do a simple relocation with the addend in the section contents.
279 // VALSIZE is the size of the value.
280 template<int valsize>
282 rel(unsigned char* view,
283 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
285 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
286 Valtype* wv = reinterpret_cast<Valtype*>(view);
287 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
288 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
291 // Do a simple relocation using a Symbol_value with the addend in
292 // the section contents. VALSIZE is the size of the value to
294 template<int valsize>
296 rel(unsigned char* view,
297 const Sized_relobj<size, big_endian>* object,
298 const Symbol_value<size>* psymval)
300 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
301 Valtype* wv = reinterpret_cast<Valtype*>(view);
302 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
303 x = psymval->value(object, x);
304 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
307 // Do a simple relocation with the addend in the relocation.
308 // VALSIZE is the size of the value.
309 template<int valsize>
311 rela(unsigned char* view,
312 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
313 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
315 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
316 Valtype* wv = reinterpret_cast<Valtype*>(view);
317 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
320 // Do a simple relocation using a symbol value with the addend in
321 // the relocation. VALSIZE is the size of the value.
322 template<int valsize>
324 rela(unsigned char* view,
325 const Sized_relobj<size, big_endian>* object,
326 const Symbol_value<size>* psymval,
327 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
329 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
330 Valtype* wv = reinterpret_cast<Valtype*>(view);
331 Valtype x = psymval->value(object, addend);
332 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
335 // Do a simple PC relative relocation with the addend in the section
336 // contents. VALSIZE is the size of the value.
337 template<int valsize>
339 pcrel(unsigned char* view,
340 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
341 typename elfcpp::Elf_types<size>::Elf_Addr address)
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 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
349 // Do a simple PC relative relocation with a Symbol_value with the
350 // addend in the section contents. VALSIZE is the size of the
352 template<int valsize>
354 pcrel(unsigned char* view,
355 const Sized_relobj<size, big_endian>* object,
356 const Symbol_value<size>* psymval,
357 typename elfcpp::Elf_types<size>::Elf_Addr address)
359 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
360 Valtype* wv = reinterpret_cast<Valtype*>(view);
361 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
362 x = psymval->value(object, x);
363 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
366 // Do a simple PC relative relocation with the addend in the
367 // relocation. VALSIZE is the size of the value.
368 template<int valsize>
370 pcrela(unsigned char* view,
371 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
372 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
373 typename elfcpp::Elf_types<size>::Elf_Addr address)
375 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
376 Valtype* wv = reinterpret_cast<Valtype*>(view);
377 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
380 // Do a simple PC relative relocation with a Symbol_value with the
381 // addend in the relocation. VALSIZE is the size of the value.
382 template<int valsize>
384 pcrela(unsigned char* view,
385 const Sized_relobj<size, big_endian>* object,
386 const Symbol_value<size>* psymval,
387 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
388 typename elfcpp::Elf_types<size>::Elf_Addr address)
390 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
391 Valtype* wv = reinterpret_cast<Valtype*>(view);
392 Valtype x = psymval->value(object, addend);
393 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
396 typedef Relocate_functions<size, big_endian> This;
399 // Do a simple 8-bit REL relocation with the addend in the section
402 rel8(unsigned char* view, unsigned char value)
403 { This::template rel<8>(view, value); }
406 rel8(unsigned char* view,
407 const Sized_relobj<size, big_endian>* object,
408 const Symbol_value<size>* psymval)
409 { This::template rel<8>(view, object, psymval); }
411 // Do an 8-bit RELA relocation with the addend in the relocation.
413 rela8(unsigned char* view, unsigned char value, unsigned char addend)
414 { This::template rela<8>(view, value, addend); }
417 rela8(unsigned char* view,
418 const Sized_relobj<size, big_endian>* object,
419 const Symbol_value<size>* psymval,
420 unsigned char addend)
421 { This::template rela<8>(view, object, psymval, addend); }
423 // Do a simple 8-bit PC relative relocation with the addend in the
426 pcrel8(unsigned char* view, unsigned char value,
427 typename elfcpp::Elf_types<size>::Elf_Addr address)
428 { This::template pcrel<8>(view, value, address); }
431 pcrel8(unsigned char* view,
432 const Sized_relobj<size, big_endian>* object,
433 const Symbol_value<size>* psymval,
434 typename elfcpp::Elf_types<size>::Elf_Addr address)
435 { This::template pcrel<8>(view, object, psymval, address); }
437 // Do a simple 8-bit PC relative RELA relocation with the addend in
440 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
441 typename elfcpp::Elf_types<size>::Elf_Addr address)
442 { This::template pcrela<8>(view, value, addend, address); }
445 pcrela8(unsigned char* view,
446 const Sized_relobj<size, big_endian>* object,
447 const Symbol_value<size>* psymval,
448 unsigned char addend,
449 typename elfcpp::Elf_types<size>::Elf_Addr address)
450 { This::template pcrela<8>(view, object, psymval, addend, address); }
452 // Do a simple 16-bit REL relocation with the addend in the section
455 rel16(unsigned char* view, elfcpp::Elf_Half value)
456 { This::template rel<16>(view, value); }
459 rel16(unsigned char* view,
460 const Sized_relobj<size, big_endian>* object,
461 const Symbol_value<size>* psymval)
462 { This::template rel<16>(view, object, psymval); }
464 // Do an 16-bit RELA relocation with the addend in the relocation.
466 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
467 { This::template rela<16>(view, value, addend); }
470 rela16(unsigned char* view,
471 const Sized_relobj<size, big_endian>* object,
472 const Symbol_value<size>* psymval,
473 elfcpp::Elf_Half addend)
474 { This::template rela<16>(view, object, psymval, addend); }
476 // Do a simple 16-bit PC relative REL relocation with the addend in
477 // the section contents.
479 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
480 typename elfcpp::Elf_types<size>::Elf_Addr address)
481 { This::template pcrel<16>(view, value, address); }
484 pcrel16(unsigned char* view,
485 const Sized_relobj<size, big_endian>* object,
486 const Symbol_value<size>* psymval,
487 typename elfcpp::Elf_types<size>::Elf_Addr address)
488 { This::template pcrel<16>(view, object, psymval, address); }
490 // Do a simple 16-bit PC relative RELA relocation with the addend in
493 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
494 elfcpp::Elf_Half addend,
495 typename elfcpp::Elf_types<size>::Elf_Addr address)
496 { This::template pcrela<16>(view, value, addend, address); }
499 pcrela16(unsigned char* view,
500 const Sized_relobj<size, big_endian>* object,
501 const Symbol_value<size>* psymval,
502 elfcpp::Elf_Half addend,
503 typename elfcpp::Elf_types<size>::Elf_Addr address)
504 { This::template pcrela<16>(view, object, psymval, addend, address); }
506 // Do a simple 32-bit REL relocation with the addend in the section
509 rel32(unsigned char* view, elfcpp::Elf_Word value)
510 { This::template rel<32>(view, value); }
513 rel32(unsigned char* view,
514 const Sized_relobj<size, big_endian>* object,
515 const Symbol_value<size>* psymval)
516 { This::template rel<32>(view, object, psymval); }
518 // Do an 32-bit RELA relocation with the addend in the relocation.
520 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
521 { This::template rela<32>(view, value, addend); }
524 rela32(unsigned char* view,
525 const Sized_relobj<size, big_endian>* object,
526 const Symbol_value<size>* psymval,
527 elfcpp::Elf_Word addend)
528 { This::template rela<32>(view, object, psymval, addend); }
530 // Do a simple 32-bit PC relative REL relocation with the addend in
531 // the section contents.
533 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
534 typename elfcpp::Elf_types<size>::Elf_Addr address)
535 { This::template pcrel<32>(view, value, address); }
538 pcrel32(unsigned char* view,
539 const Sized_relobj<size, big_endian>* object,
540 const Symbol_value<size>* psymval,
541 typename elfcpp::Elf_types<size>::Elf_Addr address)
542 { This::template pcrel<32>(view, object, psymval, address); }
544 // Do a simple 32-bit PC relative RELA relocation with the addend in
547 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
548 elfcpp::Elf_Word addend,
549 typename elfcpp::Elf_types<size>::Elf_Addr address)
550 { This::template pcrela<32>(view, value, addend, address); }
553 pcrela32(unsigned char* view,
554 const Sized_relobj<size, big_endian>* object,
555 const Symbol_value<size>* psymval,
556 elfcpp::Elf_Word addend,
557 typename elfcpp::Elf_types<size>::Elf_Addr address)
558 { This::template pcrela<32>(view, object, psymval, addend, address); }
560 // Do a simple 64-bit REL relocation with the addend in the section
563 rel64(unsigned char* view, elfcpp::Elf_Xword value)
564 { This::template rel<64>(view, value); }
567 rel64(unsigned char* view,
568 const Sized_relobj<size, big_endian>* object,
569 const Symbol_value<size>* psymval)
570 { This::template rel<64>(view, object, psymval); }
572 // Do a 64-bit RELA relocation with the addend in the relocation.
574 rela64(unsigned char* view, elfcpp::Elf_Xword value,
575 elfcpp::Elf_Xword addend)
576 { This::template rela<64>(view, value, addend); }
579 rela64(unsigned char* view,
580 const Sized_relobj<size, big_endian>* object,
581 const Symbol_value<size>* psymval,
582 elfcpp::Elf_Xword addend)
583 { This::template rela<64>(view, object, psymval, addend); }
585 // Do a simple 64-bit PC relative REL relocation with the addend in
586 // the section contents.
588 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
589 typename elfcpp::Elf_types<size>::Elf_Addr address)
590 { This::template pcrel<64>(view, value, address); }
593 pcrel64(unsigned char* view,
594 const Sized_relobj<size, big_endian>* object,
595 const Symbol_value<size>* psymval,
596 typename elfcpp::Elf_types<size>::Elf_Addr address)
597 { This::template pcrel<64>(view, object, psymval, address); }
599 // Do a simple 64-bit PC relative RELA relocation with the addend in
602 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
603 elfcpp::Elf_Xword addend,
604 typename elfcpp::Elf_types<size>::Elf_Addr address)
605 { This::template pcrela<64>(view, value, addend, address); }
608 pcrela64(unsigned char* view,
609 const Sized_relobj<size, big_endian>* object,
610 const Symbol_value<size>* psymval,
611 elfcpp::Elf_Xword addend,
612 typename elfcpp::Elf_types<size>::Elf_Addr address)
613 { This::template pcrela<64>(view, object, psymval, addend, address); }
616 // We try to avoid COPY relocations when possible. A COPY relocation
617 // may be required when an executable refers to a variable defined in
618 // a shared library. COPY relocations are problematic because they
619 // tie the executable to the exact size of the variable in the shared
620 // library. We can avoid them if all the references to the variable
621 // are in a writeable section. In that case we can simply use dynamic
622 // relocations. However, when scanning relocs, we don't know when we
623 // see the relocation whether we will be forced to use a COPY
624 // relocation or not. So we have to save the relocation during the
625 // reloc scanning, and then emit it as a dynamic relocation if
626 // necessary. This class implements that. It is used by the target
629 template<int size, bool big_endian>
637 // Return whether we need a COPY reloc for a reloc against GSYM,
638 // which is being applied to section SHNDX in OBJECT.
640 need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
641 Sized_symbol<size>* gsym);
643 // Save a Rel against SYM for possible emission later. SHNDX is the
644 // index of the section to which the reloc is being applied.
646 save(Symbol* sym, Relobj*, unsigned int shndx,
647 Output_section* output_section, const elfcpp::Rel<size, big_endian>&);
649 // Save a Rela against SYM for possible emission later.
651 save(Symbol* sym, Relobj*, unsigned int shndx,
652 Output_section* output_section, const elfcpp::Rela<size, big_endian>&);
654 // Return whether there are any relocs to emit. This also discards
655 // entries which need not be emitted.
659 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
660 // is still defined in the dynamic object).
661 template<int sh_type>
663 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
666 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
667 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
669 // This POD class holds the entries we are saving.
670 class Copy_reloc_entry
673 Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
674 Relobj* relobj, unsigned int shndx,
675 Output_section* output_section,
676 Address address, Addend addend)
677 : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
678 shndx_(shndx), output_section_(output_section),
679 address_(address), addend_(addend)
682 // Return whether we should emit this reloc. If we should not
683 // emit, we clear it.
690 emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
693 emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
697 unsigned int reloc_type_;
700 Output_section* output_section_;
705 // A list of relocs to be saved.
706 typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
708 // The list of relocs we are saving.
709 Copy_reloc_entries entries_;
712 // Track relocations while reading a section. This lets you ask for
713 // the relocation at a certain offset, and see how relocs occur
714 // between points of interest.
716 template<int size, bool big_endian>
721 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
724 // Initialize the Track_relocs object. OBJECT is the object holding
725 // the reloc section, RELOC_SHNDX is the section index of the reloc
726 // section, and RELOC_TYPE is the type of the reloc section
727 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
728 // something went wrong.
730 initialize(Object* object, unsigned int reloc_shndx,
731 unsigned int reloc_type);
733 // Return the offset in the data section to which the next reloc
734 // applies. THis returns -1 if there is no next reloc.
738 // Return the symbol index of the next reloc. This returns -1U if
739 // there is no next reloc.
743 // Advance to OFFSET within the data section, and return the number
744 // of relocs which would be skipped.
746 advance(off_t offset);
749 // The contents of the input object's reloc section.
750 const unsigned char* prelocs_;
751 // The length of the reloc section.
752 section_size_type len_;
753 // Our current position in the reloc section.
754 section_size_type pos_;
755 // The size of the relocs in the section.
759 } // End namespace gold.
761 #endif // !defined(GOLD_RELOC_H)