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 contents of the
198 // section need to be changed. The number indicates the number of
199 // bytes in the addend in the section contents.
200 RELOC_ADJUST_FOR_SECTION_1,
201 RELOC_ADJUST_FOR_SECTION_2,
202 RELOC_ADJUST_FOR_SECTION_4,
203 RELOC_ADJUST_FOR_SECTION_8,
204 // Discard the input reloc--process it completely when relocating
205 // the data section contents.
207 // An input reloc which is not discarded, but which requires
208 // target specific processing in order to update it.
213 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
216 // Record the number of relocs.
218 set_reloc_count(size_t reloc_count)
219 { this->reloc_strategies_.reserve(reloc_count); }
221 // Record what to do for the next reloc.
223 set_next_reloc_strategy(Reloc_strategy strategy)
225 this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
226 if (strategy != RELOC_DISCARD)
227 ++this->output_reloc_count_;
230 // Record the Output_data associated with this reloc section.
232 set_output_data(Output_data* posd)
234 gold_assert(this->posd_ == NULL);
238 // Return the Output_data associated with this reloc section.
241 { return this->posd_; }
243 // Return what to do for reloc I.
245 strategy(unsigned int i) const
247 gold_assert(i < this->reloc_strategies_.size());
248 return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
251 // Return the number of relocations to create in the output file.
253 output_reloc_count() const
254 { return this->output_reloc_count_; }
257 typedef std::vector<unsigned char> Reloc_strategies;
259 // The strategies for the input reloc. There is one entry in this
260 // vector for each relocation in the input section.
261 Reloc_strategies reloc_strategies_;
262 // The number of relocations to be created in the output file.
263 size_t output_reloc_count_;
264 // The output data structure associated with this relocation.
268 // Standard relocation routines which are used on many targets. Here
269 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
271 template<int size, bool big_endian>
272 class Relocate_functions
275 // Do a simple relocation with the addend in the section contents.
276 // VALSIZE is the size of the value.
277 template<int valsize>
279 rel(unsigned char* view,
280 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
282 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
283 Valtype* wv = reinterpret_cast<Valtype*>(view);
284 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
285 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
288 // Do a simple relocation using a Symbol_value with the addend in
289 // the section contents. VALSIZE is the size of the value to
291 template<int valsize>
293 rel(unsigned char* view,
294 const Sized_relobj<size, big_endian>* object,
295 const Symbol_value<size>* psymval)
297 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
298 Valtype* wv = reinterpret_cast<Valtype*>(view);
299 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
300 x = psymval->value(object, x);
301 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
304 // Do a simple relocation with the addend in the relocation.
305 // VALSIZE is the size of the value.
306 template<int valsize>
308 rela(unsigned char* view,
309 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
310 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
312 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
313 Valtype* wv = reinterpret_cast<Valtype*>(view);
314 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
317 // Do a simple relocation using a symbol value with the addend in
318 // the relocation. VALSIZE is the size of the value.
319 template<int valsize>
321 rela(unsigned char* view,
322 const Sized_relobj<size, big_endian>* object,
323 const Symbol_value<size>* psymval,
324 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
326 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
327 Valtype* wv = reinterpret_cast<Valtype*>(view);
328 Valtype x = psymval->value(object, addend);
329 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
332 // Do a simple PC relative relocation with the addend in the section
333 // contents. VALSIZE is the size of the value.
334 template<int valsize>
336 pcrel(unsigned char* view,
337 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
338 typename elfcpp::Elf_types<size>::Elf_Addr address)
340 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
341 Valtype* wv = reinterpret_cast<Valtype*>(view);
342 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
343 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
346 // Do a simple PC relative relocation with a Symbol_value with the
347 // addend in the section contents. VALSIZE is the size of the
349 template<int valsize>
351 pcrel(unsigned char* view,
352 const Sized_relobj<size, big_endian>* object,
353 const Symbol_value<size>* psymval,
354 typename elfcpp::Elf_types<size>::Elf_Addr address)
356 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
357 Valtype* wv = reinterpret_cast<Valtype*>(view);
358 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
359 x = psymval->value(object, x);
360 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
363 // Do a simple PC relative relocation with the addend in the
364 // relocation. VALSIZE is the size of the value.
365 template<int valsize>
367 pcrela(unsigned char* view,
368 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
369 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
370 typename elfcpp::Elf_types<size>::Elf_Addr address)
372 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
373 Valtype* wv = reinterpret_cast<Valtype*>(view);
374 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
377 // Do a simple PC relative relocation with a Symbol_value with the
378 // addend in the relocation. VALSIZE is the size of the value.
379 template<int valsize>
381 pcrela(unsigned char* view,
382 const Sized_relobj<size, big_endian>* object,
383 const Symbol_value<size>* psymval,
384 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
385 typename elfcpp::Elf_types<size>::Elf_Addr address)
387 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
388 Valtype* wv = reinterpret_cast<Valtype*>(view);
389 Valtype x = psymval->value(object, addend);
390 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
393 typedef Relocate_functions<size, big_endian> This;
396 // Do a simple 8-bit REL relocation with the addend in the section
399 rel8(unsigned char* view, unsigned char value)
400 { This::template rel<8>(view, value); }
403 rel8(unsigned char* view,
404 const Sized_relobj<size, big_endian>* object,
405 const Symbol_value<size>* psymval)
406 { This::template rel<8>(view, object, psymval); }
408 // Do an 8-bit RELA relocation with the addend in the relocation.
410 rela8(unsigned char* view, unsigned char value, unsigned char addend)
411 { This::template rela<8>(view, value, addend); }
414 rela8(unsigned char* view,
415 const Sized_relobj<size, big_endian>* object,
416 const Symbol_value<size>* psymval,
417 unsigned char addend)
418 { This::template rela<8>(view, object, psymval, addend); }
420 // Do a simple 8-bit PC relative relocation with the addend in the
423 pcrel8(unsigned char* view, unsigned char value,
424 typename elfcpp::Elf_types<size>::Elf_Addr address)
425 { This::template pcrel<8>(view, value, address); }
428 pcrel8(unsigned char* view,
429 const Sized_relobj<size, big_endian>* object,
430 const Symbol_value<size>* psymval,
431 typename elfcpp::Elf_types<size>::Elf_Addr address)
432 { This::template pcrel<8>(view, object, psymval, address); }
434 // Do a simple 8-bit PC relative RELA relocation with the addend in
437 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
438 typename elfcpp::Elf_types<size>::Elf_Addr address)
439 { This::template pcrela<8>(view, value, addend, address); }
442 pcrela8(unsigned char* view,
443 const Sized_relobj<size, big_endian>* object,
444 const Symbol_value<size>* psymval,
445 unsigned char addend,
446 typename elfcpp::Elf_types<size>::Elf_Addr address)
447 { This::template pcrela<8>(view, object, psymval, addend, address); }
449 // Do a simple 16-bit REL relocation with the addend in the section
452 rel16(unsigned char* view, elfcpp::Elf_Half value)
453 { This::template rel<16>(view, value); }
456 rel16(unsigned char* view,
457 const Sized_relobj<size, big_endian>* object,
458 const Symbol_value<size>* psymval)
459 { This::template rel<16>(view, object, psymval); }
461 // Do an 16-bit RELA relocation with the addend in the relocation.
463 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
464 { This::template rela<16>(view, value, addend); }
467 rela16(unsigned char* view,
468 const Sized_relobj<size, big_endian>* object,
469 const Symbol_value<size>* psymval,
470 elfcpp::Elf_Half addend)
471 { This::template rela<16>(view, object, psymval, addend); }
473 // Do a simple 16-bit PC relative REL relocation with the addend in
474 // the section contents.
476 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
477 typename elfcpp::Elf_types<size>::Elf_Addr address)
478 { This::template pcrel<16>(view, value, address); }
481 pcrel16(unsigned char* view,
482 const Sized_relobj<size, big_endian>* object,
483 const Symbol_value<size>* psymval,
484 typename elfcpp::Elf_types<size>::Elf_Addr address)
485 { This::template pcrel<16>(view, object, psymval, address); }
487 // Do a simple 16-bit PC relative RELA relocation with the addend in
490 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
491 elfcpp::Elf_Half addend,
492 typename elfcpp::Elf_types<size>::Elf_Addr address)
493 { This::template pcrela<16>(view, value, addend, address); }
496 pcrela16(unsigned char* view,
497 const Sized_relobj<size, big_endian>* object,
498 const Symbol_value<size>* psymval,
499 elfcpp::Elf_Half addend,
500 typename elfcpp::Elf_types<size>::Elf_Addr address)
501 { This::template pcrela<16>(view, object, psymval, addend, address); }
503 // Do a simple 32-bit REL relocation with the addend in the section
506 rel32(unsigned char* view, elfcpp::Elf_Word value)
507 { This::template rel<32>(view, value); }
510 rel32(unsigned char* view,
511 const Sized_relobj<size, big_endian>* object,
512 const Symbol_value<size>* psymval)
513 { This::template rel<32>(view, object, psymval); }
515 // Do an 32-bit RELA relocation with the addend in the relocation.
517 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
518 { This::template rela<32>(view, value, addend); }
521 rela32(unsigned char* view,
522 const Sized_relobj<size, big_endian>* object,
523 const Symbol_value<size>* psymval,
524 elfcpp::Elf_Word addend)
525 { This::template rela<32>(view, object, psymval, addend); }
527 // Do a simple 32-bit PC relative REL relocation with the addend in
528 // the section contents.
530 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
531 typename elfcpp::Elf_types<size>::Elf_Addr address)
532 { This::template pcrel<32>(view, value, address); }
535 pcrel32(unsigned char* view,
536 const Sized_relobj<size, big_endian>* object,
537 const Symbol_value<size>* psymval,
538 typename elfcpp::Elf_types<size>::Elf_Addr address)
539 { This::template pcrel<32>(view, object, psymval, address); }
541 // Do a simple 32-bit PC relative RELA relocation with the addend in
544 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
545 elfcpp::Elf_Word addend,
546 typename elfcpp::Elf_types<size>::Elf_Addr address)
547 { This::template pcrela<32>(view, value, addend, address); }
550 pcrela32(unsigned char* view,
551 const Sized_relobj<size, big_endian>* object,
552 const Symbol_value<size>* psymval,
553 elfcpp::Elf_Word addend,
554 typename elfcpp::Elf_types<size>::Elf_Addr address)
555 { This::template pcrela<32>(view, object, psymval, addend, address); }
557 // Do a simple 64-bit REL relocation with the addend in the section
560 rel64(unsigned char* view, elfcpp::Elf_Xword value)
561 { This::template rel<64>(view, value); }
564 rel64(unsigned char* view,
565 const Sized_relobj<size, big_endian>* object,
566 const Symbol_value<size>* psymval)
567 { This::template rel<64>(view, object, psymval); }
569 // Do a 64-bit RELA relocation with the addend in the relocation.
571 rela64(unsigned char* view, elfcpp::Elf_Xword value,
572 elfcpp::Elf_Xword addend)
573 { This::template rela<64>(view, value, addend); }
576 rela64(unsigned char* view,
577 const Sized_relobj<size, big_endian>* object,
578 const Symbol_value<size>* psymval,
579 elfcpp::Elf_Xword addend)
580 { This::template rela<64>(view, object, psymval, addend); }
582 // Do a simple 64-bit PC relative REL relocation with the addend in
583 // the section contents.
585 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
586 typename elfcpp::Elf_types<size>::Elf_Addr address)
587 { This::template pcrel<64>(view, value, address); }
590 pcrel64(unsigned char* view,
591 const Sized_relobj<size, big_endian>* object,
592 const Symbol_value<size>* psymval,
593 typename elfcpp::Elf_types<size>::Elf_Addr address)
594 { This::template pcrel<64>(view, object, psymval, address); }
596 // Do a simple 64-bit PC relative RELA relocation with the addend in
599 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
600 elfcpp::Elf_Xword addend,
601 typename elfcpp::Elf_types<size>::Elf_Addr address)
602 { This::template pcrela<64>(view, value, addend, address); }
605 pcrela64(unsigned char* view,
606 const Sized_relobj<size, big_endian>* object,
607 const Symbol_value<size>* psymval,
608 elfcpp::Elf_Xword addend,
609 typename elfcpp::Elf_types<size>::Elf_Addr address)
610 { This::template pcrela<64>(view, object, psymval, addend, address); }
613 // We try to avoid COPY relocations when possible. A COPY relocation
614 // may be required when an executable refers to a variable defined in
615 // a shared library. COPY relocations are problematic because they
616 // tie the executable to the exact size of the variable in the shared
617 // library. We can avoid them if all the references to the variable
618 // are in a writeable section. In that case we can simply use dynamic
619 // relocations. However, when scanning relocs, we don't know when we
620 // see the relocation whether we will be forced to use a COPY
621 // relocation or not. So we have to save the relocation during the
622 // reloc scanning, and then emit it as a dynamic relocation if
623 // necessary. This class implements that. It is used by the target
626 template<int size, bool big_endian>
634 // Return whether we need a COPY reloc for a reloc against GSYM,
635 // which is being applied to section SHNDX in OBJECT.
637 need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
638 Sized_symbol<size>* gsym);
640 // Save a Rel against SYM for possible emission later. SHNDX is the
641 // index of the section to which the reloc is being applied.
643 save(Symbol* sym, Relobj*, unsigned int shndx,
644 Output_section* output_section, const elfcpp::Rel<size, big_endian>&);
646 // Save a Rela against SYM for possible emission later.
648 save(Symbol* sym, Relobj*, unsigned int shndx,
649 Output_section* output_section, const elfcpp::Rela<size, big_endian>&);
651 // Return whether there are any relocs to emit. This also discards
652 // entries which need not be emitted.
656 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
657 // is still defined in the dynamic object).
658 template<int sh_type>
660 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
663 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
664 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
666 // This POD class holds the entries we are saving.
667 class Copy_reloc_entry
670 Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
671 Relobj* relobj, unsigned int shndx,
672 Output_section* output_section,
673 Address address, Addend addend)
674 : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
675 shndx_(shndx), output_section_(output_section),
676 address_(address), addend_(addend)
679 // Return whether we should emit this reloc. If we should not
680 // emit, we clear it.
687 emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
690 emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
694 unsigned int reloc_type_;
697 Output_section* output_section_;
702 // A list of relocs to be saved.
703 typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
705 // The list of relocs we are saving.
706 Copy_reloc_entries entries_;
709 // Track relocations while reading a section. This lets you ask for
710 // the relocation at a certain offset, and see how relocs occur
711 // between points of interest.
713 template<int size, bool big_endian>
718 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
721 // Initialize the Track_relocs object. OBJECT is the object holding
722 // the reloc section, RELOC_SHNDX is the section index of the reloc
723 // section, and RELOC_TYPE is the type of the reloc section
724 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
725 // something went wrong.
727 initialize(Object* object, unsigned int reloc_shndx,
728 unsigned int reloc_type);
730 // Return the offset in the data section to which the next reloc
731 // applies. THis returns -1 if there is no next reloc.
735 // Return the symbol index of the next reloc. This returns -1U if
736 // there is no next reloc.
740 // Advance to OFFSET within the data section, and return the number
741 // of relocs which would be skipped.
743 advance(off_t offset);
746 // The contents of the input object's reloc section.
747 const unsigned char* prelocs_;
748 // The length of the reloc section.
749 section_size_type len_;
750 // Our current position in the reloc section.
751 section_size_type pos_;
752 // The size of the relocs in the section.
756 } // End namespace gold.
758 #endif // !defined(GOLD_RELOC_H)