1 // reloc.cc -- relocate input files for gold.
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.
27 #include "workqueue.h"
37 // Read_relocs methods.
39 // These tasks just read the relocation information from the file.
40 // After reading it, the start another task to process the
41 // information. These tasks requires access to the file.
44 Read_relocs::is_runnable()
46 return this->object_
->is_locked() ? this->object_
->token() : NULL
;
52 Read_relocs::locks(Task_locker
* tl
)
54 tl
->add(this, this->object_
->token());
57 // Read the relocations and then start a Scan_relocs_task.
60 Read_relocs::run(Workqueue
* workqueue
)
62 Read_relocs_data
*rd
= new Read_relocs_data
;
63 this->object_
->read_relocs(rd
);
64 this->object_
->release();
66 workqueue
->queue_front(new Scan_relocs(this->options_
, this->symtab_
,
67 this->layout_
, this->object_
, rd
,
68 this->symtab_lock_
, this->blocker_
));
71 // Return a debugging name for the task.
74 Read_relocs::get_name() const
76 return "Read_relocs " + this->object_
->name();
79 // Scan_relocs methods.
81 // These tasks scan the relocations read by Read_relocs and mark up
82 // the symbol table to indicate which relocations are required. We
83 // use a lock on the symbol table to keep them from interfering with
87 Scan_relocs::is_runnable()
89 if (!this->symtab_lock_
->is_writable())
90 return this->symtab_lock_
;
91 if (this->object_
->is_locked())
92 return this->object_
->token();
96 // Return the locks we hold: one on the file, one on the symbol table
100 Scan_relocs::locks(Task_locker
* tl
)
102 tl
->add(this, this->object_
->token());
103 tl
->add(this, this->symtab_lock_
);
104 tl
->add(this, this->blocker_
);
110 Scan_relocs::run(Workqueue
*)
112 this->object_
->scan_relocs(this->options_
, this->symtab_
, this->layout_
,
114 this->object_
->release();
119 // Return a debugging name for the task.
122 Scan_relocs::get_name() const
124 return "Scan_relocs " + this->object_
->name();
127 // Relocate_task methods.
129 // We may have to wait for the output sections to be written.
132 Relocate_task::is_runnable()
134 if (this->object_
->relocs_must_follow_section_writes()
135 && this->output_sections_blocker_
->is_blocked())
136 return this->output_sections_blocker_
;
138 if (this->object_
->is_locked())
139 return this->object_
->token();
144 // We want to lock the file while we run. We want to unblock
145 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
146 // INPUT_SECTIONS_BLOCKER may be NULL.
149 Relocate_task::locks(Task_locker
* tl
)
151 if (this->input_sections_blocker_
!= NULL
)
152 tl
->add(this, this->input_sections_blocker_
);
153 tl
->add(this, this->final_blocker_
);
154 tl
->add(this, this->object_
->token());
160 Relocate_task::run(Workqueue
*)
162 this->object_
->relocate(this->options_
, this->symtab_
, this->layout_
,
165 // This is normally the last thing we will do with an object, so
166 // uncache all views.
167 this->object_
->clear_view_cache_marks();
169 this->object_
->release();
172 // Return a debugging name for the task.
175 Relocate_task::get_name() const
177 return "Relocate_task " + this->object_
->name();
180 // Read the relocs and local symbols from the object file and store
181 // the information in RD.
183 template<int size
, bool big_endian
>
185 Sized_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
189 unsigned int shnum
= this->shnum();
193 rd
->relocs
.reserve(shnum
/ 2);
195 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
197 const unsigned char *pshdrs
= this->get_view(this->elf_file_
.shoff(),
198 shnum
* This::shdr_size
,
200 // Skip the first, dummy, section.
201 const unsigned char *ps
= pshdrs
+ This::shdr_size
;
202 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= This::shdr_size
)
204 typename
This::Shdr
shdr(ps
);
206 unsigned int sh_type
= shdr
.get_sh_type();
207 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
210 unsigned int shndx
= shdr
.get_sh_info();
213 this->error(_("relocation section %u has bad info %u"),
218 Output_section
* os
= map_sections
[shndx
].output_section
;
222 // We are scanning relocations in order to fill out the GOT and
223 // PLT sections. Relocations for sections which are not
224 // allocated (typically debugging sections) should not add new
225 // GOT and PLT entries. So we skip them unless this is a
227 if (!parameters
->output_is_object())
229 typename
This::Shdr
secshdr(pshdrs
+ shndx
* This::shdr_size
);
230 if ((secshdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
234 if (shdr
.get_sh_link() != this->symtab_shndx_
)
236 this->error(_("relocation section %u uses unexpected "
238 i
, shdr
.get_sh_link());
242 off_t sh_size
= shdr
.get_sh_size();
244 unsigned int reloc_size
;
245 if (sh_type
== elfcpp::SHT_REL
)
246 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
248 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
249 if (reloc_size
!= shdr
.get_sh_entsize())
251 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
252 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
257 size_t reloc_count
= sh_size
/ reloc_size
;
258 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
260 this->error(_("reloc section %u size %lu uneven"),
261 i
, static_cast<unsigned long>(sh_size
));
265 rd
->relocs
.push_back(Section_relocs());
266 Section_relocs
& sr(rd
->relocs
.back());
268 sr
.data_shndx
= shndx
;
269 sr
.contents
= this->get_lasting_view(shdr
.get_sh_offset(), sh_size
,
271 sr
.sh_type
= sh_type
;
272 sr
.reloc_count
= reloc_count
;
273 sr
.output_section
= os
;
274 sr
.needs_special_offset_handling
= map_sections
[shndx
].offset
== -1;
277 // Read the local symbols.
278 gold_assert(this->symtab_shndx_
!= -1U);
279 if (this->symtab_shndx_
== 0 || this->local_symbol_count_
== 0)
280 rd
->local_symbols
= NULL
;
283 typename
This::Shdr
symtabshdr(pshdrs
284 + this->symtab_shndx_
* This::shdr_size
);
285 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
286 const int sym_size
= This::sym_size
;
287 const unsigned int loccount
= this->local_symbol_count_
;
288 gold_assert(loccount
== symtabshdr
.get_sh_info());
289 off_t locsize
= loccount
* sym_size
;
290 rd
->local_symbols
= this->get_lasting_view(symtabshdr
.get_sh_offset(),
295 // Scan the relocs and adjust the symbol table. This looks for
296 // relocations which require GOT/PLT/COPY relocations.
298 template<int size
, bool big_endian
>
300 Sized_relobj
<size
, big_endian
>::do_scan_relocs(const General_options
& options
,
301 Symbol_table
* symtab
,
303 Read_relocs_data
* rd
)
305 Sized_target
<size
, big_endian
>* target
= this->sized_target();
307 const unsigned char* local_symbols
;
308 if (rd
->local_symbols
== NULL
)
309 local_symbols
= NULL
;
311 local_symbols
= rd
->local_symbols
->data();
313 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
314 p
!= rd
->relocs
.end();
317 if (!parameters
->output_is_object())
318 target
->scan_relocs(options
, symtab
, layout
, this, p
->data_shndx
,
319 p
->sh_type
, p
->contents
->data(), p
->reloc_count
,
321 p
->needs_special_offset_handling
,
322 this->local_symbol_count_
,
326 Relocatable_relocs
* rr
= this->relocatable_relocs(p
->reloc_shndx
);
327 gold_assert(rr
!= NULL
);
328 rr
->set_reloc_count(p
->reloc_count
);
329 target
->scan_relocatable_relocs(options
, symtab
, layout
, this,
330 p
->data_shndx
, p
->sh_type
,
334 p
->needs_special_offset_handling
,
335 this->local_symbol_count_
,
344 if (rd
->local_symbols
!= NULL
)
346 delete rd
->local_symbols
;
347 rd
->local_symbols
= NULL
;
351 // Relocate the input sections and write out the local symbols.
353 template<int size
, bool big_endian
>
355 Sized_relobj
<size
, big_endian
>::do_relocate(const General_options
& options
,
356 const Symbol_table
* symtab
,
357 const Layout
* layout
,
360 unsigned int shnum
= this->shnum();
362 // Read the section headers.
363 const unsigned char* pshdrs
= this->get_view(this->elf_file_
.shoff(),
364 shnum
* This::shdr_size
,
370 // Make two passes over the sections. The first one copies the
371 // section data to the output file. The second one applies
374 this->write_sections(pshdrs
, of
, &views
);
376 // To speed up relocations, we set up hash tables for fast lookup of
377 // input offsets to output addresses.
378 this->initialize_input_to_output_maps();
380 // Apply relocations.
382 this->relocate_sections(options
, symtab
, layout
, pshdrs
, &views
);
384 // After we've done the relocations, we release the hash tables,
385 // since we no longer need them.
386 this->free_input_to_output_maps();
388 // Write out the accumulated views.
389 for (unsigned int i
= 1; i
< shnum
; ++i
)
391 if (views
[i
].view
!= NULL
)
393 if (!views
[i
].is_postprocessing_view
)
395 if (views
[i
].is_input_output_view
)
396 of
->write_input_output_view(views
[i
].offset
,
400 of
->write_output_view(views
[i
].offset
, views
[i
].view_size
,
406 // Write out the local symbols.
407 this->write_local_symbols(of
, layout
->sympool(), layout
->dynpool());
409 // We should no longer need the local symbol values.
410 this->clear_local_symbols();
413 // Sort a Read_multiple vector by file offset.
414 struct Read_multiple_compare
417 operator()(const File_read::Read_multiple_entry
& rme1
,
418 const File_read::Read_multiple_entry
& rme2
) const
419 { return rme1
.file_offset
< rme2
.file_offset
; }
422 // Write section data to the output file. PSHDRS points to the
423 // section headers. Record the views in *PVIEWS for use when
426 template<int size
, bool big_endian
>
428 Sized_relobj
<size
, big_endian
>::write_sections(const unsigned char* pshdrs
,
432 unsigned int shnum
= this->shnum();
433 const std::vector
<Map_to_output
>& map_sections(this->map_to_output());
435 File_read::Read_multiple rm
;
436 bool is_sorted
= true;
438 const unsigned char* p
= pshdrs
+ This::shdr_size
;
439 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
441 View_size
* pvs
= &(*pviews
)[i
];
445 const Output_section
* os
= map_sections
[i
].output_section
;
448 off_t output_offset
= map_sections
[i
].offset
;
450 typename
This::Shdr
shdr(p
);
452 if (shdr
.get_sh_type() == elfcpp::SHT_NOBITS
)
455 if (parameters
->output_is_object()
456 && (shdr
.get_sh_type() == elfcpp::SHT_REL
457 || shdr
.get_sh_type() == elfcpp::SHT_RELA
)
458 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
460 // This is a reloc section in a relocatable link. We don't
461 // need to read the input file. The size and file offset
462 // are stored in the Relocatable_relocs structure.
463 Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
464 gold_assert(rr
!= NULL
);
465 Output_data
* posd
= rr
->output_data();
466 gold_assert(posd
!= NULL
);
468 pvs
->offset
= posd
->offset();
469 pvs
->view_size
= posd
->data_size();
470 pvs
->view
= of
->get_output_view(pvs
->offset
, pvs
->view_size
);
471 pvs
->address
= posd
->address();
472 pvs
->is_input_output_view
= false;
473 pvs
->is_postprocessing_view
= false;
478 // In the normal case, this input section is simply mapped to
479 // the output section at offset OUTPUT_OFFSET.
481 // However, if OUTPUT_OFFSET == -1, then input data is handled
482 // specially--e.g., a .eh_frame section. The relocation
483 // routines need to check for each reloc where it should be
484 // applied. For this case, we need an input/output view for the
485 // entire contents of the section in the output file. We don't
486 // want to copy the contents of the input section to the output
487 // section; the output section contents were already written,
488 // and we waited for them in Relocate_task::is_runnable because
489 // relocs_must_follow_section_writes is set for the object.
491 // Regardless of which of the above cases is true, we have to
492 // check requires_postprocessing of the output section. If that
493 // is false, then we work with views of the output file
494 // directly. If it is true, then we work with a separate
495 // buffer, and the output section is responsible for writing the
496 // final data to the output file.
498 off_t output_section_offset
;
499 off_t output_section_size
;
500 if (!os
->requires_postprocessing())
502 output_section_offset
= os
->offset();
503 output_section_size
= os
->data_size();
507 output_section_offset
= 0;
508 output_section_size
= os
->postprocessing_buffer_size();
512 section_size_type view_size
;
513 if (output_offset
!= -1)
515 view_start
= output_section_offset
+ output_offset
;
516 view_size
= convert_to_section_size_type(shdr
.get_sh_size());
520 view_start
= output_section_offset
;
521 view_size
= convert_to_section_size_type(output_section_size
);
527 gold_assert(output_offset
== -1
528 || (output_offset
>= 0
529 && (output_offset
+ static_cast<off_t
>(view_size
)
530 <= output_section_size
)));
533 if (os
->requires_postprocessing())
535 unsigned char* buffer
= os
->postprocessing_buffer();
536 view
= buffer
+ view_start
;
537 if (output_offset
!= -1)
539 off_t sh_offset
= shdr
.get_sh_offset();
540 if (!rm
.empty() && rm
.back().file_offset
> sh_offset
)
542 rm
.push_back(File_read::Read_multiple_entry(sh_offset
,
548 if (output_offset
== -1)
549 view
= of
->get_input_output_view(view_start
, view_size
);
552 view
= of
->get_output_view(view_start
, view_size
);
553 off_t sh_offset
= shdr
.get_sh_offset();
554 if (!rm
.empty() && rm
.back().file_offset
> sh_offset
)
556 rm
.push_back(File_read::Read_multiple_entry(sh_offset
,
562 pvs
->address
= os
->address();
563 if (output_offset
!= -1)
564 pvs
->address
+= output_offset
;
565 pvs
->offset
= view_start
;
566 pvs
->view_size
= view_size
;
567 pvs
->is_input_output_view
= output_offset
== -1;
568 pvs
->is_postprocessing_view
= os
->requires_postprocessing();
571 // Actually read the data.
575 std::sort(rm
.begin(), rm
.end(), Read_multiple_compare());
576 this->read_multiple(rm
);
580 // Relocate section data. VIEWS points to the section data as views
581 // in the output file.
583 template<int size
, bool big_endian
>
585 Sized_relobj
<size
, big_endian
>::relocate_sections(
586 const General_options
& options
,
587 const Symbol_table
* symtab
,
588 const Layout
* layout
,
589 const unsigned char* pshdrs
,
592 unsigned int shnum
= this->shnum();
593 Sized_target
<size
, big_endian
>* target
= this->sized_target();
595 const std::vector
<Map_to_output
>& map_sections(this->map_to_output());
597 Relocate_info
<size
, big_endian
> relinfo
;
598 relinfo
.options
= &options
;
599 relinfo
.symtab
= symtab
;
600 relinfo
.layout
= layout
;
601 relinfo
.object
= this;
603 const unsigned char* p
= pshdrs
+ This::shdr_size
;
604 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
606 typename
This::Shdr
shdr(p
);
608 unsigned int sh_type
= shdr
.get_sh_type();
609 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
612 unsigned int index
= shdr
.get_sh_info();
613 if (index
>= this->shnum())
615 this->error(_("relocation section %u has bad info %u"),
620 Output_section
* os
= map_sections
[index
].output_section
;
623 // This relocation section is against a section which we
627 off_t output_offset
= map_sections
[index
].offset
;
629 gold_assert((*pviews
)[index
].view
!= NULL
);
630 if (parameters
->output_is_object())
631 gold_assert((*pviews
)[i
].view
!= NULL
);
633 if (shdr
.get_sh_link() != this->symtab_shndx_
)
635 gold_error(_("relocation section %u uses unexpected "
637 i
, shdr
.get_sh_link());
641 off_t sh_size
= shdr
.get_sh_size();
642 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
645 unsigned int reloc_size
;
646 if (sh_type
== elfcpp::SHT_REL
)
647 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
649 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
651 if (reloc_size
!= shdr
.get_sh_entsize())
653 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
654 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
659 size_t reloc_count
= sh_size
/ reloc_size
;
660 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
662 gold_error(_("reloc section %u size %lu uneven"),
663 i
, static_cast<unsigned long>(sh_size
));
667 gold_assert(output_offset
!= -1
668 || this->relocs_must_follow_section_writes());
670 relinfo
.reloc_shndx
= i
;
671 relinfo
.data_shndx
= index
;
672 if (!parameters
->output_is_object())
673 target
->relocate_section(&relinfo
,
679 (*pviews
)[index
].view
,
680 (*pviews
)[index
].address
,
681 (*pviews
)[index
].view_size
);
684 Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
685 target
->relocate_for_relocatable(&relinfo
,
692 (*pviews
)[index
].view
,
693 (*pviews
)[index
].address
,
694 (*pviews
)[index
].view_size
,
696 (*pviews
)[i
].view_size
);
701 // Create merge hash tables for the local symbols. These are used to
702 // speed up relocations.
704 template<int size
, bool big_endian
>
706 Sized_relobj
<size
, big_endian
>::initialize_input_to_output_maps()
708 const unsigned int loccount
= this->local_symbol_count_
;
709 for (unsigned int i
= 1; i
< loccount
; ++i
)
711 Symbol_value
<size
>& lv(this->local_values_
[i
]);
712 lv
.initialize_input_to_output_map(this);
716 // Free merge hash tables for the local symbols.
718 template<int size
, bool big_endian
>
720 Sized_relobj
<size
, big_endian
>::free_input_to_output_maps()
722 const unsigned int loccount
= this->local_symbol_count_
;
723 for (unsigned int i
= 1; i
< loccount
; ++i
)
725 Symbol_value
<size
>& lv(this->local_values_
[i
]);
726 lv
.free_input_to_output_map();
730 // Class Merged_symbol_value.
734 Merged_symbol_value
<size
>::initialize_input_to_output_map(
735 const Relobj
* object
,
736 unsigned int input_shndx
)
738 Object_merge_map
* map
= object
->merge_map();
739 map
->initialize_input_to_output_map
<size
>(input_shndx
,
740 this->output_start_address_
,
741 &this->output_addresses_
);
744 // Get the output value corresponding to an input offset if we
745 // couldn't find it in the hash table.
748 typename
elfcpp::Elf_types
<size
>::Elf_Addr
749 Merged_symbol_value
<size
>::value_from_output_section(
750 const Relobj
* object
,
751 unsigned int input_shndx
,
752 typename
elfcpp::Elf_types
<size
>::Elf_Addr input_offset
) const
754 section_offset_type output_offset
;
755 bool found
= object
->merge_map()->get_output_offset(NULL
, input_shndx
,
759 // If this assertion fails, it means that some relocation was
760 // against a portion of an input merge section which we didn't map
761 // to the output file and we didn't explicitly discard. We should
762 // always map all portions of input merge sections.
765 if (output_offset
== -1)
768 return this->output_start_address_
+ output_offset
;
771 // Copy_relocs::Copy_reloc_entry methods.
773 // Return whether we should emit this reloc. We should emit it if the
774 // symbol is still defined in a dynamic object. If we should not emit
775 // it, we clear it, to save ourselves the test next time.
777 template<int size
, bool big_endian
>
779 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::should_emit()
781 if (this->sym_
== NULL
)
783 if (this->sym_
->is_from_dynobj())
789 // Emit a reloc into a SHT_REL section.
791 template<int size
, bool big_endian
>
793 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::emit(
794 Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>* reloc_data
)
796 this->sym_
->set_needs_dynsym_entry();
797 reloc_data
->add_global(this->sym_
, this->reloc_type_
, this->output_section_
,
798 this->relobj_
, this->shndx_
, this->address_
);
801 // Emit a reloc into a SHT_RELA section.
803 template<int size
, bool big_endian
>
805 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::emit(
806 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>* reloc_data
)
808 this->sym_
->set_needs_dynsym_entry();
809 reloc_data
->add_global(this->sym_
, this->reloc_type_
, this->output_section_
,
810 this->relobj_
, this->shndx_
, this->address_
,
814 // Copy_relocs methods.
816 // Return whether we need a COPY reloc for a relocation against GSYM.
817 // The relocation is being applied to section SHNDX in OBJECT.
819 template<int size
, bool big_endian
>
821 Copy_relocs
<size
, big_endian
>::need_copy_reloc(
822 const General_options
*,
825 Sized_symbol
<size
>* sym
)
827 // FIXME: Handle -z nocopyrelocs.
829 if (sym
->symsize() == 0)
832 // If this is a readonly section, then we need a COPY reloc.
833 // Otherwise we can use a dynamic reloc.
834 if ((object
->section_flags(shndx
) & elfcpp::SHF_WRITE
) == 0)
842 template<int size
, bool big_endian
>
844 Copy_relocs
<size
, big_endian
>::save(
848 Output_section
* output_section
,
849 const elfcpp::Rel
<size
, big_endian
>& rel
)
851 unsigned int reloc_type
= elfcpp::elf_r_type
<size
>(rel
.get_r_info());
852 this->entries_
.push_back(Copy_reloc_entry(sym
, reloc_type
, relobj
, shndx
,
854 rel
.get_r_offset(), 0));
857 // Save a Rela reloc.
859 template<int size
, bool big_endian
>
861 Copy_relocs
<size
, big_endian
>::save(
865 Output_section
* output_section
,
866 const elfcpp::Rela
<size
, big_endian
>& rela
)
868 unsigned int reloc_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
869 this->entries_
.push_back(Copy_reloc_entry(sym
, reloc_type
, relobj
, shndx
,
872 rela
.get_r_addend()));
875 // Return whether there are any relocs to emit. We don't want to emit
876 // a reloc if the symbol is no longer defined in a dynamic object.
878 template<int size
, bool big_endian
>
880 Copy_relocs
<size
, big_endian
>::any_to_emit()
882 for (typename
Copy_reloc_entries::iterator p
= this->entries_
.begin();
883 p
!= this->entries_
.end();
886 if (p
->should_emit())
894 template<int size
, bool big_endian
>
895 template<int sh_type
>
897 Copy_relocs
<size
, big_endian
>::emit(
898 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_data
)
900 for (typename
Copy_reloc_entries::iterator p
= this->entries_
.begin();
901 p
!= this->entries_
.end();
904 if (p
->should_emit())
909 // Track_relocs methods.
911 // Initialize the class to track the relocs. This gets the object,
912 // the reloc section index, and the type of the relocs. This returns
913 // false if something goes wrong.
915 template<int size
, bool big_endian
>
917 Track_relocs
<size
, big_endian
>::initialize(
919 unsigned int reloc_shndx
,
920 unsigned int reloc_type
)
922 // If RELOC_SHNDX is -1U, it means there is more than one reloc
923 // section for the .eh_frame section. We can't handle that case.
924 if (reloc_shndx
== -1U)
927 // If RELOC_SHNDX is 0, there is no reloc section.
928 if (reloc_shndx
== 0)
931 // Get the contents of the reloc section.
932 this->prelocs_
= object
->section_contents(reloc_shndx
, &this->len_
, false);
934 if (reloc_type
== elfcpp::SHT_REL
)
935 this->reloc_size_
= elfcpp::Elf_sizes
<size
>::rel_size
;
936 else if (reloc_type
== elfcpp::SHT_RELA
)
937 this->reloc_size_
= elfcpp::Elf_sizes
<size
>::rela_size
;
941 if (this->len_
% this->reloc_size_
!= 0)
943 object
->error(_("reloc section size %zu is not a multiple of "
945 static_cast<size_t>(this->len_
),
953 // Return the offset of the next reloc, or -1 if there isn't one.
955 template<int size
, bool big_endian
>
957 Track_relocs
<size
, big_endian
>::next_offset() const
959 if (this->pos_
>= this->len_
)
962 // Rel and Rela start out the same, so we can always use Rel to find
963 // the r_offset value.
964 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
965 return rel
.get_r_offset();
968 // Return the index of the symbol referenced by the next reloc, or -1U
969 // if there aren't any more relocs.
971 template<int size
, bool big_endian
>
973 Track_relocs
<size
, big_endian
>::next_symndx() const
975 if (this->pos_
>= this->len_
)
978 // Rel and Rela start out the same, so we can use Rel to find the
980 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
981 return elfcpp::elf_r_sym
<size
>(rel
.get_r_info());
984 // Advance to the next reloc whose r_offset is greater than or equal
985 // to OFFSET. Return the number of relocs we skip.
987 template<int size
, bool big_endian
>
989 Track_relocs
<size
, big_endian
>::advance(off_t offset
)
992 while (this->pos_
< this->len_
)
994 // Rel and Rela start out the same, so we can always use Rel to
995 // find the r_offset value.
996 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
997 if (static_cast<off_t
>(rel
.get_r_offset()) >= offset
)
1000 this->pos_
+= this->reloc_size_
;
1005 // Instantiate the templates we need. We could use the configure
1006 // script to restrict this to only the ones for implemented targets.
1008 #ifdef HAVE_TARGET_32_LITTLE
1011 Sized_relobj
<32, false>::do_read_relocs(Read_relocs_data
* rd
);
1014 #ifdef HAVE_TARGET_32_BIG
1017 Sized_relobj
<32, true>::do_read_relocs(Read_relocs_data
* rd
);
1020 #ifdef HAVE_TARGET_64_LITTLE
1023 Sized_relobj
<64, false>::do_read_relocs(Read_relocs_data
* rd
);
1026 #ifdef HAVE_TARGET_64_BIG
1029 Sized_relobj
<64, true>::do_read_relocs(Read_relocs_data
* rd
);
1032 #ifdef HAVE_TARGET_32_LITTLE
1035 Sized_relobj
<32, false>::do_scan_relocs(const General_options
& options
,
1036 Symbol_table
* symtab
,
1038 Read_relocs_data
* rd
);
1041 #ifdef HAVE_TARGET_32_BIG
1044 Sized_relobj
<32, true>::do_scan_relocs(const General_options
& options
,
1045 Symbol_table
* symtab
,
1047 Read_relocs_data
* rd
);
1050 #ifdef HAVE_TARGET_64_LITTLE
1053 Sized_relobj
<64, false>::do_scan_relocs(const General_options
& options
,
1054 Symbol_table
* symtab
,
1056 Read_relocs_data
* rd
);
1059 #ifdef HAVE_TARGET_64_BIG
1062 Sized_relobj
<64, true>::do_scan_relocs(const General_options
& options
,
1063 Symbol_table
* symtab
,
1065 Read_relocs_data
* rd
);
1068 #ifdef HAVE_TARGET_32_LITTLE
1071 Sized_relobj
<32, false>::do_relocate(const General_options
& options
,
1072 const Symbol_table
* symtab
,
1073 const Layout
* layout
,
1077 #ifdef HAVE_TARGET_32_BIG
1080 Sized_relobj
<32, true>::do_relocate(const General_options
& options
,
1081 const Symbol_table
* symtab
,
1082 const Layout
* layout
,
1086 #ifdef HAVE_TARGET_64_LITTLE
1089 Sized_relobj
<64, false>::do_relocate(const General_options
& options
,
1090 const Symbol_table
* symtab
,
1091 const Layout
* layout
,
1095 #ifdef HAVE_TARGET_64_BIG
1098 Sized_relobj
<64, true>::do_relocate(const General_options
& options
,
1099 const Symbol_table
* symtab
,
1100 const Layout
* layout
,
1104 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1106 class Merged_symbol_value
<32>;
1109 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1111 class Merged_symbol_value
<64>;
1114 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1116 class Symbol_value
<32>;
1119 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1121 class Symbol_value
<64>;
1124 #ifdef HAVE_TARGET_32_LITTLE
1126 class Copy_relocs
<32, false>;
1129 #ifdef HAVE_TARGET_32_BIG
1131 class Copy_relocs
<32, true>;
1134 #ifdef HAVE_TARGET_64_LITTLE
1136 class Copy_relocs
<64, false>;
1139 #ifdef HAVE_TARGET_64_BIG
1141 class Copy_relocs
<64, true>;
1144 #ifdef HAVE_TARGET_32_LITTLE
1147 Copy_relocs
<32, false>::emit
<elfcpp::SHT_REL
>(
1148 Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>*);
1151 #ifdef HAVE_TARGET_32_BIG
1154 Copy_relocs
<32, true>::emit
<elfcpp::SHT_REL
>(
1155 Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>*);
1158 #ifdef HAVE_TARGET_64_LITTLE
1161 Copy_relocs
<64, false>::emit
<elfcpp::SHT_REL
>(
1162 Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>*);
1165 #ifdef HAVE_TARGET_64_BIG
1168 Copy_relocs
<64, true>::emit
<elfcpp::SHT_REL
>(
1169 Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>*);
1172 #ifdef HAVE_TARGET_32_LITTLE
1175 Copy_relocs
<32, false>::emit
<elfcpp::SHT_RELA
>(
1176 Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>*);
1179 #ifdef HAVE_TARGET_32_BIG
1182 Copy_relocs
<32, true>::emit
<elfcpp::SHT_RELA
>(
1183 Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>*);
1186 #ifdef HAVE_TARGET_64_LITTLE
1189 Copy_relocs
<64, false>::emit
<elfcpp::SHT_RELA
>(
1190 Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>*);
1193 #ifdef HAVE_TARGET_64_BIG
1196 Copy_relocs
<64, true>::emit
<elfcpp::SHT_RELA
>(
1197 Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>*);
1200 #ifdef HAVE_TARGET_32_LITTLE
1202 class Track_relocs
<32, false>;
1205 #ifdef HAVE_TARGET_32_BIG
1207 class Track_relocs
<32, true>;
1210 #ifdef HAVE_TARGET_64_LITTLE
1212 class Track_relocs
<64, false>;
1215 #ifdef HAVE_TARGET_64_BIG
1217 class Track_relocs
<64, true>;
1220 } // End namespace gold.