1 // reloc.cc -- relocate input files for gold.
3 // Copyright 2006, 2007, 2008, 2009 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 #include "workqueue.h"
32 #include "target-reloc.h"
38 // Read_relocs methods.
40 // These tasks just read the relocation information from the file.
41 // After reading it, the start another task to process the
42 // information. These tasks requires access to the file.
45 Read_relocs::is_runnable()
47 return this->object_
->is_locked() ? this->object_
->token() : NULL
;
53 Read_relocs::locks(Task_locker
* tl
)
55 tl
->add(this, this->object_
->token());
58 // Read the relocations and then start a Scan_relocs_task.
61 Read_relocs::run(Workqueue
* workqueue
)
63 Read_relocs_data
*rd
= new Read_relocs_data
;
64 this->object_
->read_relocs(rd
);
65 this->object_
->set_relocs_data(rd
);
66 this->object_
->release();
68 // If garbage collection is desired, we must process the relocs
69 // instead of scanning the relocs as reloc processing is necessary
70 // to determine unused sections.
71 if (parameters
->options().gc_sections())
73 workqueue
->queue_next(new Gc_process_relocs(this->options_
,
82 workqueue
->queue_next(new Scan_relocs(this->options_
, this->symtab_
,
83 this->layout_
, this->object_
, rd
,
89 // Return a debugging name for the task.
92 Read_relocs::get_name() const
94 return "Read_relocs " + this->object_
->name();
97 // Gc_process_relocs methods.
99 // These tasks process the relocations read by Read_relocs and
100 // determine which sections are referenced and which are garbage.
101 // This task is done only when --gc-sections is used.
104 Gc_process_relocs::is_runnable()
106 if (this->object_
->is_locked())
107 return this->object_
->token();
112 Gc_process_relocs::locks(Task_locker
* tl
)
114 tl
->add(this, this->object_
->token());
115 tl
->add(this, this->blocker_
);
119 Gc_process_relocs::run(Workqueue
*)
121 this->object_
->gc_process_relocs(this->options_
, this->symtab_
, this->layout_
,
123 this->object_
->release();
126 // Return a debugging name for the task.
129 Gc_process_relocs::get_name() const
131 return "Gc_process_relocs " + this->object_
->name();
134 // Scan_relocs methods.
136 // These tasks scan the relocations read by Read_relocs and mark up
137 // the symbol table to indicate which relocations are required. We
138 // use a lock on the symbol table to keep them from interfering with
142 Scan_relocs::is_runnable()
144 if (!this->symtab_lock_
->is_writable())
145 return this->symtab_lock_
;
146 if (this->object_
->is_locked())
147 return this->object_
->token();
151 // Return the locks we hold: one on the file, one on the symbol table
155 Scan_relocs::locks(Task_locker
* tl
)
157 tl
->add(this, this->object_
->token());
158 tl
->add(this, this->symtab_lock_
);
159 tl
->add(this, this->blocker_
);
165 Scan_relocs::run(Workqueue
*)
167 this->object_
->scan_relocs(this->options_
, this->symtab_
, this->layout_
,
169 this->object_
->release();
174 // Return a debugging name for the task.
177 Scan_relocs::get_name() const
179 return "Scan_relocs " + this->object_
->name();
182 // Relocate_task methods.
184 // We may have to wait for the output sections to be written.
187 Relocate_task::is_runnable()
189 if (this->object_
->relocs_must_follow_section_writes()
190 && this->output_sections_blocker_
->is_blocked())
191 return this->output_sections_blocker_
;
193 if (this->object_
->is_locked())
194 return this->object_
->token();
199 // We want to lock the file while we run. We want to unblock
200 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
201 // INPUT_SECTIONS_BLOCKER may be NULL.
204 Relocate_task::locks(Task_locker
* tl
)
206 if (this->input_sections_blocker_
!= NULL
)
207 tl
->add(this, this->input_sections_blocker_
);
208 tl
->add(this, this->final_blocker_
);
209 tl
->add(this, this->object_
->token());
215 Relocate_task::run(Workqueue
*)
217 this->object_
->relocate(this->options_
, this->symtab_
, this->layout_
,
220 // This is normally the last thing we will do with an object, so
221 // uncache all views.
222 this->object_
->clear_view_cache_marks();
224 this->object_
->release();
227 // Return a debugging name for the task.
230 Relocate_task::get_name() const
232 return "Relocate_task " + this->object_
->name();
235 // Read the relocs and local symbols from the object file and store
236 // the information in RD.
238 template<int size
, bool big_endian
>
240 Sized_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
244 unsigned int shnum
= this->shnum();
248 rd
->relocs
.reserve(shnum
/ 2);
250 const Output_sections
& out_sections(this->output_sections());
251 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
253 const unsigned char *pshdrs
= this->get_view(this->elf_file_
.shoff(),
254 shnum
* This::shdr_size
,
256 // Skip the first, dummy, section.
257 const unsigned char *ps
= pshdrs
+ This::shdr_size
;
258 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= This::shdr_size
)
260 typename
This::Shdr
shdr(ps
);
262 unsigned int sh_type
= shdr
.get_sh_type();
263 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
266 unsigned int shndx
= this->adjust_shndx(shdr
.get_sh_info());
269 this->error(_("relocation section %u has bad info %u"),
274 Output_section
* os
= out_sections
[shndx
];
278 // We are scanning relocations in order to fill out the GOT and
279 // PLT sections. Relocations for sections which are not
280 // allocated (typically debugging sections) should not add new
281 // GOT and PLT entries. So we skip them unless this is a
282 // relocatable link or we need to emit relocations. FIXME: What
283 // should we do if a linker script maps a section with SHF_ALLOC
284 // clear to a section with SHF_ALLOC set?
285 typename
This::Shdr
secshdr(pshdrs
+ shndx
* This::shdr_size
);
286 bool is_section_allocated
= ((secshdr
.get_sh_flags() & elfcpp::SHF_ALLOC
)
288 if (!is_section_allocated
289 && !parameters
->options().relocatable()
290 && !parameters
->options().emit_relocs())
293 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx_
)
295 this->error(_("relocation section %u uses unexpected "
297 i
, this->adjust_shndx(shdr
.get_sh_link()));
301 off_t sh_size
= shdr
.get_sh_size();
303 unsigned int reloc_size
;
304 if (sh_type
== elfcpp::SHT_REL
)
305 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
307 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
308 if (reloc_size
!= shdr
.get_sh_entsize())
310 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
311 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
316 size_t reloc_count
= sh_size
/ reloc_size
;
317 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
319 this->error(_("reloc section %u size %lu uneven"),
320 i
, static_cast<unsigned long>(sh_size
));
324 rd
->relocs
.push_back(Section_relocs());
325 Section_relocs
& sr(rd
->relocs
.back());
327 sr
.data_shndx
= shndx
;
328 sr
.contents
= this->get_lasting_view(shdr
.get_sh_offset(), sh_size
,
330 sr
.sh_type
= sh_type
;
331 sr
.reloc_count
= reloc_count
;
332 sr
.output_section
= os
;
333 sr
.needs_special_offset_handling
= out_offsets
[shndx
] == invalid_address
;
334 sr
.is_data_section_allocated
= is_section_allocated
;
337 // Read the local symbols.
338 gold_assert(this->symtab_shndx_
!= -1U);
339 if (this->symtab_shndx_
== 0 || this->local_symbol_count_
== 0)
340 rd
->local_symbols
= NULL
;
343 typename
This::Shdr
symtabshdr(pshdrs
344 + this->symtab_shndx_
* This::shdr_size
);
345 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
346 const int sym_size
= This::sym_size
;
347 const unsigned int loccount
= this->local_symbol_count_
;
348 gold_assert(loccount
== symtabshdr
.get_sh_info());
349 off_t locsize
= loccount
* sym_size
;
350 rd
->local_symbols
= this->get_lasting_view(symtabshdr
.get_sh_offset(),
351 locsize
, true, true);
355 // Process the relocs to generate mappings from source sections to referenced
356 // sections. This is used during garbage colletion to determine garbage
359 template<int size
, bool big_endian
>
361 Sized_relobj
<size
, big_endian
>::do_gc_process_relocs(const General_options
& options
,
362 Symbol_table
* symtab
,
364 Read_relocs_data
* rd
)
366 Sized_target
<size
, big_endian
>* target
= this->sized_target();
368 const unsigned char* local_symbols
;
369 if (rd
->local_symbols
== NULL
)
370 local_symbols
= NULL
;
372 local_symbols
= rd
->local_symbols
->data();
374 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
375 p
!= rd
->relocs
.end();
378 if (!parameters
->options().relocatable())
380 // As noted above, when not generating an object file, we
381 // only scan allocated sections. We may see a non-allocated
382 // section here if we are emitting relocs.
383 if (p
->is_data_section_allocated
)
384 target
->gc_process_relocs(options
, symtab
, layout
, this,
385 p
->data_shndx
, p
->sh_type
,
386 p
->contents
->data(), p
->reloc_count
,
388 p
->needs_special_offset_handling
,
389 this->local_symbol_count_
,
396 // Scan the relocs and adjust the symbol table. This looks for
397 // relocations which require GOT/PLT/COPY relocations.
399 template<int size
, bool big_endian
>
401 Sized_relobj
<size
, big_endian
>::do_scan_relocs(const General_options
& options
,
402 Symbol_table
* symtab
,
404 Read_relocs_data
* rd
)
406 Sized_target
<size
, big_endian
>* target
= this->sized_target();
408 const unsigned char* local_symbols
;
409 if (rd
->local_symbols
== NULL
)
410 local_symbols
= NULL
;
412 local_symbols
= rd
->local_symbols
->data();
414 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
415 p
!= rd
->relocs
.end();
418 // When garbage collection is on, unreferenced sections are not included
419 // in the link that would have been included normally. This is known only
420 // after Read_relocs hence this check has to be done again.
421 if (parameters
->options().gc_sections())
423 if (p
->output_section
== NULL
)
426 if (!parameters
->options().relocatable())
428 // As noted above, when not generating an object file, we
429 // only scan allocated sections. We may see a non-allocated
430 // section here if we are emitting relocs.
431 if (p
->is_data_section_allocated
)
432 target
->scan_relocs(options
, symtab
, layout
, this, p
->data_shndx
,
433 p
->sh_type
, p
->contents
->data(),
434 p
->reloc_count
, p
->output_section
,
435 p
->needs_special_offset_handling
,
436 this->local_symbol_count_
,
438 if (parameters
->options().emit_relocs())
439 this->emit_relocs_scan(options
, symtab
, layout
, local_symbols
, p
);
443 Relocatable_relocs
* rr
= this->relocatable_relocs(p
->reloc_shndx
);
444 gold_assert(rr
!= NULL
);
445 rr
->set_reloc_count(p
->reloc_count
);
446 target
->scan_relocatable_relocs(options
, symtab
, layout
, this,
447 p
->data_shndx
, p
->sh_type
,
451 p
->needs_special_offset_handling
,
452 this->local_symbol_count_
,
461 if (rd
->local_symbols
!= NULL
)
463 delete rd
->local_symbols
;
464 rd
->local_symbols
= NULL
;
468 // This is a strategy class we use when scanning for --emit-relocs.
470 template<int sh_type
>
471 class Emit_relocs_strategy
474 // A local non-section symbol.
475 inline Relocatable_relocs::Reloc_strategy
476 local_non_section_strategy(unsigned int, Relobj
*, unsigned int)
477 { return Relocatable_relocs::RELOC_COPY
; }
479 // A local section symbol.
480 inline Relocatable_relocs::Reloc_strategy
481 local_section_strategy(unsigned int, Relobj
*)
483 if (sh_type
== elfcpp::SHT_RELA
)
484 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
487 // The addend is stored in the section contents. Since this
488 // is not a relocatable link, we are going to apply the
489 // relocation contents to the section as usual. This means
490 // that we have no way to record the original addend. If the
491 // original addend is not zero, there is basically no way for
492 // the user to handle this correctly. Caveat emptor.
493 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
498 inline Relocatable_relocs::Reloc_strategy
499 global_strategy(unsigned int, Relobj
*, unsigned int)
500 { return Relocatable_relocs::RELOC_COPY
; }
503 // Scan the input relocations for --emit-relocs.
505 template<int size
, bool big_endian
>
507 Sized_relobj
<size
, big_endian
>::emit_relocs_scan(
508 const General_options
& options
,
509 Symbol_table
* symtab
,
511 const unsigned char* plocal_syms
,
512 const Read_relocs_data::Relocs_list::iterator
& p
)
514 Relocatable_relocs
* rr
= this->relocatable_relocs(p
->reloc_shndx
);
515 gold_assert(rr
!= NULL
);
516 rr
->set_reloc_count(p
->reloc_count
);
518 if (p
->sh_type
== elfcpp::SHT_REL
)
519 this->emit_relocs_scan_reltype
<elfcpp::SHT_REL
>(options
, symtab
, layout
,
523 gold_assert(p
->sh_type
== elfcpp::SHT_RELA
);
524 this->emit_relocs_scan_reltype
<elfcpp::SHT_RELA
>(options
, symtab
,
525 layout
, plocal_syms
, p
,
530 // Scan the input relocation for --emit-relocs, templatized on the
531 // type of the relocation section.
533 template<int size
, bool big_endian
>
534 template<int sh_type
>
536 Sized_relobj
<size
, big_endian
>::emit_relocs_scan_reltype(
537 const General_options
& options
,
538 Symbol_table
* symtab
,
540 const unsigned char* plocal_syms
,
541 const Read_relocs_data::Relocs_list::iterator
& p
,
542 Relocatable_relocs
* rr
)
544 scan_relocatable_relocs
<size
, big_endian
, sh_type
,
545 Emit_relocs_strategy
<sh_type
> >(
554 p
->needs_special_offset_handling
,
555 this->local_symbol_count_
,
560 // Relocate the input sections and write out the local symbols.
562 template<int size
, bool big_endian
>
564 Sized_relobj
<size
, big_endian
>::do_relocate(const General_options
& options
,
565 const Symbol_table
* symtab
,
566 const Layout
* layout
,
569 unsigned int shnum
= this->shnum();
571 // Read the section headers.
572 const unsigned char* pshdrs
= this->get_view(this->elf_file_
.shoff(),
573 shnum
* This::shdr_size
,
579 // Make two passes over the sections. The first one copies the
580 // section data to the output file. The second one applies
583 this->write_sections(pshdrs
, of
, &views
);
585 // To speed up relocations, we set up hash tables for fast lookup of
586 // input offsets to output addresses.
587 this->initialize_input_to_output_maps();
589 // Apply relocations.
591 this->relocate_sections(options
, symtab
, layout
, pshdrs
, &views
);
593 // After we've done the relocations, we release the hash tables,
594 // since we no longer need them.
595 this->free_input_to_output_maps();
597 // Write out the accumulated views.
598 for (unsigned int i
= 1; i
< shnum
; ++i
)
600 if (views
[i
].view
!= NULL
)
602 if (!views
[i
].is_postprocessing_view
)
604 if (views
[i
].is_input_output_view
)
605 of
->write_input_output_view(views
[i
].offset
,
609 of
->write_output_view(views
[i
].offset
, views
[i
].view_size
,
615 // Write out the local symbols.
616 this->write_local_symbols(of
, layout
->sympool(), layout
->dynpool(),
617 layout
->symtab_xindex(), layout
->dynsym_xindex());
619 // We should no longer need the local symbol values.
620 this->clear_local_symbols();
623 // Sort a Read_multiple vector by file offset.
624 struct Read_multiple_compare
627 operator()(const File_read::Read_multiple_entry
& rme1
,
628 const File_read::Read_multiple_entry
& rme2
) const
629 { return rme1
.file_offset
< rme2
.file_offset
; }
632 // Write section data to the output file. PSHDRS points to the
633 // section headers. Record the views in *PVIEWS for use when
636 template<int size
, bool big_endian
>
638 Sized_relobj
<size
, big_endian
>::write_sections(const unsigned char* pshdrs
,
642 unsigned int shnum
= this->shnum();
643 const Output_sections
& out_sections(this->output_sections());
644 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
646 File_read::Read_multiple rm
;
647 bool is_sorted
= true;
649 const unsigned char* p
= pshdrs
+ This::shdr_size
;
650 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
652 View_size
* pvs
= &(*pviews
)[i
];
656 const Output_section
* os
= out_sections
[i
];
659 Address output_offset
= out_offsets
[i
];
661 typename
This::Shdr
shdr(p
);
663 if (shdr
.get_sh_type() == elfcpp::SHT_NOBITS
)
666 if ((parameters
->options().relocatable()
667 || parameters
->options().emit_relocs())
668 && (shdr
.get_sh_type() == elfcpp::SHT_REL
669 || shdr
.get_sh_type() == elfcpp::SHT_RELA
)
670 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
672 // This is a reloc section in a relocatable link or when
673 // emitting relocs. We don't need to read the input file.
674 // The size and file offset are stored in the
675 // Relocatable_relocs structure.
676 Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
677 gold_assert(rr
!= NULL
);
678 Output_data
* posd
= rr
->output_data();
679 gold_assert(posd
!= NULL
);
681 pvs
->offset
= posd
->offset();
682 pvs
->view_size
= posd
->data_size();
683 pvs
->view
= of
->get_output_view(pvs
->offset
, pvs
->view_size
);
684 pvs
->address
= posd
->address();
685 pvs
->is_input_output_view
= false;
686 pvs
->is_postprocessing_view
= false;
691 // In the normal case, this input section is simply mapped to
692 // the output section at offset OUTPUT_OFFSET.
694 // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
695 // handled specially--e.g., a .eh_frame section. The relocation
696 // routines need to check for each reloc where it should be
697 // applied. For this case, we need an input/output view for the
698 // entire contents of the section in the output file. We don't
699 // want to copy the contents of the input section to the output
700 // section; the output section contents were already written,
701 // and we waited for them in Relocate_task::is_runnable because
702 // relocs_must_follow_section_writes is set for the object.
704 // Regardless of which of the above cases is true, we have to
705 // check requires_postprocessing of the output section. If that
706 // is false, then we work with views of the output file
707 // directly. If it is true, then we work with a separate
708 // buffer, and the output section is responsible for writing the
709 // final data to the output file.
711 off_t output_section_offset
;
712 Address output_section_size
;
713 if (!os
->requires_postprocessing())
715 output_section_offset
= os
->offset();
716 output_section_size
= convert_types
<Address
, off_t
>(os
->data_size());
720 output_section_offset
= 0;
721 output_section_size
=
722 convert_types
<Address
, off_t
>(os
->postprocessing_buffer_size());
726 section_size_type view_size
;
727 if (output_offset
!= invalid_address
)
729 view_start
= output_section_offset
+ output_offset
;
730 view_size
= convert_to_section_size_type(shdr
.get_sh_size());
734 view_start
= output_section_offset
;
735 view_size
= convert_to_section_size_type(output_section_size
);
741 gold_assert(output_offset
== invalid_address
742 || output_offset
+ view_size
<= output_section_size
);
745 if (os
->requires_postprocessing())
747 unsigned char* buffer
= os
->postprocessing_buffer();
748 view
= buffer
+ view_start
;
749 if (output_offset
!= invalid_address
)
751 off_t sh_offset
= shdr
.get_sh_offset();
752 if (!rm
.empty() && rm
.back().file_offset
> sh_offset
)
754 rm
.push_back(File_read::Read_multiple_entry(sh_offset
,
760 if (output_offset
== invalid_address
)
761 view
= of
->get_input_output_view(view_start
, view_size
);
764 view
= of
->get_output_view(view_start
, view_size
);
765 off_t sh_offset
= shdr
.get_sh_offset();
766 if (!rm
.empty() && rm
.back().file_offset
> sh_offset
)
768 rm
.push_back(File_read::Read_multiple_entry(sh_offset
,
774 pvs
->address
= os
->address();
775 if (output_offset
!= invalid_address
)
776 pvs
->address
+= output_offset
;
777 pvs
->offset
= view_start
;
778 pvs
->view_size
= view_size
;
779 pvs
->is_input_output_view
= output_offset
== invalid_address
;
780 pvs
->is_postprocessing_view
= os
->requires_postprocessing();
783 // Actually read the data.
787 std::sort(rm
.begin(), rm
.end(), Read_multiple_compare());
788 this->read_multiple(rm
);
792 // Relocate section data. VIEWS points to the section data as views
793 // in the output file.
795 template<int size
, bool big_endian
>
797 Sized_relobj
<size
, big_endian
>::relocate_sections(
798 const General_options
& options
,
799 const Symbol_table
* symtab
,
800 const Layout
* layout
,
801 const unsigned char* pshdrs
,
804 unsigned int shnum
= this->shnum();
805 Sized_target
<size
, big_endian
>* target
= this->sized_target();
807 const Output_sections
& out_sections(this->output_sections());
808 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
810 Relocate_info
<size
, big_endian
> relinfo
;
811 relinfo
.options
= &options
;
812 relinfo
.symtab
= symtab
;
813 relinfo
.layout
= layout
;
814 relinfo
.object
= this;
816 const unsigned char* p
= pshdrs
+ This::shdr_size
;
817 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
819 typename
This::Shdr
shdr(p
);
821 unsigned int sh_type
= shdr
.get_sh_type();
822 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
825 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
826 if (index
>= this->shnum())
828 this->error(_("relocation section %u has bad info %u"),
833 Output_section
* os
= out_sections
[index
];
836 // This relocation section is against a section which we
840 Address output_offset
= out_offsets
[index
];
842 gold_assert((*pviews
)[index
].view
!= NULL
);
843 if (parameters
->options().relocatable())
844 gold_assert((*pviews
)[i
].view
!= NULL
);
846 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx_
)
848 gold_error(_("relocation section %u uses unexpected "
850 i
, this->adjust_shndx(shdr
.get_sh_link()));
854 off_t sh_size
= shdr
.get_sh_size();
855 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
856 sh_size
, true, false);
858 unsigned int reloc_size
;
859 if (sh_type
== elfcpp::SHT_REL
)
860 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
862 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
864 if (reloc_size
!= shdr
.get_sh_entsize())
866 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
867 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
872 size_t reloc_count
= sh_size
/ reloc_size
;
873 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
875 gold_error(_("reloc section %u size %lu uneven"),
876 i
, static_cast<unsigned long>(sh_size
));
880 gold_assert(output_offset
!= invalid_address
881 || this->relocs_must_follow_section_writes());
883 relinfo
.reloc_shndx
= i
;
884 relinfo
.data_shndx
= index
;
885 if (!parameters
->options().relocatable())
887 target
->relocate_section(&relinfo
,
892 output_offset
== invalid_address
,
893 (*pviews
)[index
].view
,
894 (*pviews
)[index
].address
,
895 (*pviews
)[index
].view_size
);
896 if (parameters
->options().emit_relocs())
897 this->emit_relocs(&relinfo
, i
, sh_type
, prelocs
, reloc_count
,
899 (*pviews
)[index
].view
,
900 (*pviews
)[index
].address
,
901 (*pviews
)[index
].view_size
,
903 (*pviews
)[i
].view_size
);
907 Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
908 target
->relocate_for_relocatable(&relinfo
,
915 (*pviews
)[index
].view
,
916 (*pviews
)[index
].address
,
917 (*pviews
)[index
].view_size
,
919 (*pviews
)[i
].view_size
);
924 // Emit the relocs for --emit-relocs.
926 template<int size
, bool big_endian
>
928 Sized_relobj
<size
, big_endian
>::emit_relocs(
929 const Relocate_info
<size
, big_endian
>* relinfo
,
931 unsigned int sh_type
,
932 const unsigned char* prelocs
,
934 Output_section
* output_section
,
935 typename
elfcpp::Elf_types
<size
>::Elf_Addr offset_in_output_section
,
937 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
938 section_size_type view_size
,
939 unsigned char* reloc_view
,
940 section_size_type reloc_view_size
)
942 if (sh_type
== elfcpp::SHT_REL
)
943 this->emit_relocs_reltype
<elfcpp::SHT_REL
>(relinfo
, i
, prelocs
,
944 reloc_count
, output_section
,
945 offset_in_output_section
,
946 view
, address
, view_size
,
947 reloc_view
, reloc_view_size
);
950 gold_assert(sh_type
== elfcpp::SHT_RELA
);
951 this->emit_relocs_reltype
<elfcpp::SHT_RELA
>(relinfo
, i
, prelocs
,
952 reloc_count
, output_section
,
953 offset_in_output_section
,
954 view
, address
, view_size
,
955 reloc_view
, reloc_view_size
);
959 // Emit the relocs for --emit-relocs, templatized on the type of the
960 // relocation section.
962 template<int size
, bool big_endian
>
963 template<int sh_type
>
965 Sized_relobj
<size
, big_endian
>::emit_relocs_reltype(
966 const Relocate_info
<size
, big_endian
>* relinfo
,
968 const unsigned char* prelocs
,
970 Output_section
* output_section
,
971 typename
elfcpp::Elf_types
<size
>::Elf_Addr offset_in_output_section
,
973 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
974 section_size_type view_size
,
975 unsigned char* reloc_view
,
976 section_size_type reloc_view_size
)
978 const Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
979 relocate_for_relocatable
<size
, big_endian
, sh_type
>(
984 offset_in_output_section
,
993 // Create merge hash tables for the local symbols. These are used to
994 // speed up relocations.
996 template<int size
, bool big_endian
>
998 Sized_relobj
<size
, big_endian
>::initialize_input_to_output_maps()
1000 const unsigned int loccount
= this->local_symbol_count_
;
1001 for (unsigned int i
= 1; i
< loccount
; ++i
)
1003 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1004 lv
.initialize_input_to_output_map(this);
1008 // Free merge hash tables for the local symbols.
1010 template<int size
, bool big_endian
>
1012 Sized_relobj
<size
, big_endian
>::free_input_to_output_maps()
1014 const unsigned int loccount
= this->local_symbol_count_
;
1015 for (unsigned int i
= 1; i
< loccount
; ++i
)
1017 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1018 lv
.free_input_to_output_map();
1022 // Class Merged_symbol_value.
1026 Merged_symbol_value
<size
>::initialize_input_to_output_map(
1027 const Relobj
* object
,
1028 unsigned int input_shndx
)
1030 Object_merge_map
* map
= object
->merge_map();
1031 map
->initialize_input_to_output_map
<size
>(input_shndx
,
1032 this->output_start_address_
,
1033 &this->output_addresses_
);
1036 // Get the output value corresponding to an input offset if we
1037 // couldn't find it in the hash table.
1040 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1041 Merged_symbol_value
<size
>::value_from_output_section(
1042 const Relobj
* object
,
1043 unsigned int input_shndx
,
1044 typename
elfcpp::Elf_types
<size
>::Elf_Addr input_offset
) const
1046 section_offset_type output_offset
;
1047 bool found
= object
->merge_map()->get_output_offset(NULL
, input_shndx
,
1051 // If this assertion fails, it means that some relocation was
1052 // against a portion of an input merge section which we didn't map
1053 // to the output file and we didn't explicitly discard. We should
1054 // always map all portions of input merge sections.
1057 if (output_offset
== -1)
1060 return this->output_start_address_
+ output_offset
;
1063 // Track_relocs methods.
1065 // Initialize the class to track the relocs. This gets the object,
1066 // the reloc section index, and the type of the relocs. This returns
1067 // false if something goes wrong.
1069 template<int size
, bool big_endian
>
1071 Track_relocs
<size
, big_endian
>::initialize(
1073 unsigned int reloc_shndx
,
1074 unsigned int reloc_type
)
1076 // If RELOC_SHNDX is -1U, it means there is more than one reloc
1077 // section for the .eh_frame section. We can't handle that case.
1078 if (reloc_shndx
== -1U)
1081 // If RELOC_SHNDX is 0, there is no reloc section.
1082 if (reloc_shndx
== 0)
1085 // Get the contents of the reloc section.
1086 this->prelocs_
= object
->section_contents(reloc_shndx
, &this->len_
, false);
1088 if (reloc_type
== elfcpp::SHT_REL
)
1089 this->reloc_size_
= elfcpp::Elf_sizes
<size
>::rel_size
;
1090 else if (reloc_type
== elfcpp::SHT_RELA
)
1091 this->reloc_size_
= elfcpp::Elf_sizes
<size
>::rela_size
;
1095 if (this->len_
% this->reloc_size_
!= 0)
1097 object
->error(_("reloc section size %zu is not a multiple of "
1099 static_cast<size_t>(this->len_
),
1107 // Return the offset of the next reloc, or -1 if there isn't one.
1109 template<int size
, bool big_endian
>
1111 Track_relocs
<size
, big_endian
>::next_offset() const
1113 if (this->pos_
>= this->len_
)
1116 // Rel and Rela start out the same, so we can always use Rel to find
1117 // the r_offset value.
1118 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1119 return rel
.get_r_offset();
1122 // Return the index of the symbol referenced by the next reloc, or -1U
1123 // if there aren't any more relocs.
1125 template<int size
, bool big_endian
>
1127 Track_relocs
<size
, big_endian
>::next_symndx() const
1129 if (this->pos_
>= this->len_
)
1132 // Rel and Rela start out the same, so we can use Rel to find the
1134 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1135 return elfcpp::elf_r_sym
<size
>(rel
.get_r_info());
1138 // Advance to the next reloc whose r_offset is greater than or equal
1139 // to OFFSET. Return the number of relocs we skip.
1141 template<int size
, bool big_endian
>
1143 Track_relocs
<size
, big_endian
>::advance(off_t offset
)
1146 while (this->pos_
< this->len_
)
1148 // Rel and Rela start out the same, so we can always use Rel to
1149 // find the r_offset value.
1150 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1151 if (static_cast<off_t
>(rel
.get_r_offset()) >= offset
)
1154 this->pos_
+= this->reloc_size_
;
1159 // Instantiate the templates we need.
1161 #ifdef HAVE_TARGET_32_LITTLE
1164 Sized_relobj
<32, false>::do_read_relocs(Read_relocs_data
* rd
);
1167 #ifdef HAVE_TARGET_32_BIG
1170 Sized_relobj
<32, true>::do_read_relocs(Read_relocs_data
* rd
);
1173 #ifdef HAVE_TARGET_64_LITTLE
1176 Sized_relobj
<64, false>::do_read_relocs(Read_relocs_data
* rd
);
1179 #ifdef HAVE_TARGET_64_BIG
1182 Sized_relobj
<64, true>::do_read_relocs(Read_relocs_data
* rd
);
1185 #ifdef HAVE_TARGET_32_LITTLE
1188 Sized_relobj
<32, false>::do_gc_process_relocs(const General_options
& options
,
1189 Symbol_table
* symtab
,
1191 Read_relocs_data
* rd
);
1194 #ifdef HAVE_TARGET_32_BIG
1197 Sized_relobj
<32, true>::do_gc_process_relocs(const General_options
& options
,
1198 Symbol_table
* symtab
,
1200 Read_relocs_data
* rd
);
1203 #ifdef HAVE_TARGET_64_LITTLE
1206 Sized_relobj
<64, false>::do_gc_process_relocs(const General_options
& options
,
1207 Symbol_table
* symtab
,
1209 Read_relocs_data
* rd
);
1212 #ifdef HAVE_TARGET_64_BIG
1215 Sized_relobj
<64, true>::do_gc_process_relocs(const General_options
& options
,
1216 Symbol_table
* symtab
,
1218 Read_relocs_data
* rd
);
1221 #ifdef HAVE_TARGET_32_LITTLE
1224 Sized_relobj
<32, false>::do_scan_relocs(const General_options
& options
,
1225 Symbol_table
* symtab
,
1227 Read_relocs_data
* rd
);
1230 #ifdef HAVE_TARGET_32_BIG
1233 Sized_relobj
<32, true>::do_scan_relocs(const General_options
& options
,
1234 Symbol_table
* symtab
,
1236 Read_relocs_data
* rd
);
1239 #ifdef HAVE_TARGET_64_LITTLE
1242 Sized_relobj
<64, false>::do_scan_relocs(const General_options
& options
,
1243 Symbol_table
* symtab
,
1245 Read_relocs_data
* rd
);
1248 #ifdef HAVE_TARGET_64_BIG
1251 Sized_relobj
<64, true>::do_scan_relocs(const General_options
& options
,
1252 Symbol_table
* symtab
,
1254 Read_relocs_data
* rd
);
1257 #ifdef HAVE_TARGET_32_LITTLE
1260 Sized_relobj
<32, false>::do_relocate(const General_options
& options
,
1261 const Symbol_table
* symtab
,
1262 const Layout
* layout
,
1266 #ifdef HAVE_TARGET_32_BIG
1269 Sized_relobj
<32, true>::do_relocate(const General_options
& options
,
1270 const Symbol_table
* symtab
,
1271 const Layout
* layout
,
1275 #ifdef HAVE_TARGET_64_LITTLE
1278 Sized_relobj
<64, false>::do_relocate(const General_options
& options
,
1279 const Symbol_table
* symtab
,
1280 const Layout
* layout
,
1284 #ifdef HAVE_TARGET_64_BIG
1287 Sized_relobj
<64, true>::do_relocate(const General_options
& options
,
1288 const Symbol_table
* symtab
,
1289 const Layout
* layout
,
1293 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1295 class Merged_symbol_value
<32>;
1298 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1300 class Merged_symbol_value
<64>;
1303 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1305 class Symbol_value
<32>;
1308 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1310 class Symbol_value
<64>;
1313 #ifdef HAVE_TARGET_32_LITTLE
1315 class Track_relocs
<32, false>;
1318 #ifdef HAVE_TARGET_32_BIG
1320 class Track_relocs
<32, true>;
1323 #ifdef HAVE_TARGET_64_LITTLE
1325 class Track_relocs
<64, false>;
1328 #ifdef HAVE_TARGET_64_BIG
1330 class Track_relocs
<64, true>;
1333 } // End namespace gold.