1 // reloc.cc -- relocate input files for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
27 #include "workqueue.h"
32 #include "target-reloc.h"
39 // Read_relocs methods.
41 // These tasks just read the relocation information from the file.
42 // After reading it, the start another task to process the
43 // information. These tasks requires access to the file.
46 Read_relocs::is_runnable()
48 return this->object_
->is_locked() ? this->object_
->token() : NULL
;
54 Read_relocs::locks(Task_locker
* tl
)
56 tl
->add(this, this->object_
->token());
59 // Read the relocations and then start a Scan_relocs_task.
62 Read_relocs::run(Workqueue
* workqueue
)
64 Read_relocs_data
*rd
= new Read_relocs_data
;
65 this->object_
->read_relocs(rd
);
66 this->object_
->set_relocs_data(rd
);
67 this->object_
->release();
69 // If garbage collection or identical comdat folding is desired, we
70 // process the relocs first before scanning them. Scanning of relocs is
71 // done only after garbage or identical sections is identified.
72 if (parameters
->options().gc_sections()
73 || parameters
->options().icf_enabled())
75 workqueue
->queue_next(new Gc_process_relocs(this->symtab_
,
79 this->next_blocker_
));
83 workqueue
->queue_next(new Scan_relocs(this->symtab_
, this->layout_
,
86 this->next_blocker_
));
90 // Return a debugging name for the task.
93 Read_relocs::get_name() const
95 return "Read_relocs " + this->object_
->name();
98 // Gc_process_relocs methods.
100 Gc_process_relocs::~Gc_process_relocs()
102 if (this->this_blocker_
!= NULL
)
103 delete this->this_blocker_
;
106 // These tasks process the relocations read by Read_relocs and
107 // determine which sections are referenced and which are garbage.
108 // This task is done only when --gc-sections is used. This is blocked
109 // by THIS_BLOCKER_. It unblocks NEXT_BLOCKER_.
112 Gc_process_relocs::is_runnable()
114 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
115 return this->this_blocker_
;
116 if (this->object_
->is_locked())
117 return this->object_
->token();
122 Gc_process_relocs::locks(Task_locker
* tl
)
124 tl
->add(this, this->object_
->token());
125 tl
->add(this, this->next_blocker_
);
129 Gc_process_relocs::run(Workqueue
*)
131 this->object_
->gc_process_relocs(this->symtab_
, this->layout_
, this->rd_
);
132 this->object_
->release();
135 // Return a debugging name for the task.
138 Gc_process_relocs::get_name() const
140 return "Gc_process_relocs " + this->object_
->name();
143 // Scan_relocs methods.
145 Scan_relocs::~Scan_relocs()
147 if (this->this_blocker_
!= NULL
)
148 delete this->this_blocker_
;
151 // These tasks scan the relocations read by Read_relocs and mark up
152 // the symbol table to indicate which relocations are required. We
153 // use a lock on the symbol table to keep them from interfering with
157 Scan_relocs::is_runnable()
159 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
160 return this->this_blocker_
;
161 if (this->object_
->is_locked())
162 return this->object_
->token();
166 // Return the locks we hold: one on the file, one on the symbol table
170 Scan_relocs::locks(Task_locker
* tl
)
172 tl
->add(this, this->object_
->token());
173 tl
->add(this, this->next_blocker_
);
179 Scan_relocs::run(Workqueue
*)
181 this->object_
->scan_relocs(this->symtab_
, this->layout_
, this->rd_
);
182 this->object_
->release();
187 // Return a debugging name for the task.
190 Scan_relocs::get_name() const
192 return "Scan_relocs " + this->object_
->name();
195 // Relocate_task methods.
197 // We may have to wait for the output sections to be written.
200 Relocate_task::is_runnable()
202 if (this->object_
->relocs_must_follow_section_writes()
203 && this->output_sections_blocker_
->is_blocked())
204 return this->output_sections_blocker_
;
206 if (this->object_
->is_locked())
207 return this->object_
->token();
212 // We want to lock the file while we run. We want to unblock
213 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
214 // INPUT_SECTIONS_BLOCKER may be NULL.
217 Relocate_task::locks(Task_locker
* tl
)
219 if (this->input_sections_blocker_
!= NULL
)
220 tl
->add(this, this->input_sections_blocker_
);
221 tl
->add(this, this->final_blocker_
);
222 tl
->add(this, this->object_
->token());
228 Relocate_task::run(Workqueue
*)
230 this->object_
->relocate(this->symtab_
, this->layout_
, this->of_
);
232 // This is normally the last thing we will do with an object, so
233 // uncache all views.
234 this->object_
->clear_view_cache_marks();
236 this->object_
->release();
239 // Return a debugging name for the task.
242 Relocate_task::get_name() const
244 return "Relocate_task " + this->object_
->name();
247 // Read the relocs and local symbols from the object file and store
248 // the information in RD.
250 template<int size
, bool big_endian
>
252 Sized_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
256 unsigned int shnum
= this->shnum();
260 rd
->relocs
.reserve(shnum
/ 2);
262 const Output_sections
& out_sections(this->output_sections());
263 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
265 const unsigned char *pshdrs
= this->get_view(this->elf_file_
.shoff(),
266 shnum
* This::shdr_size
,
268 // Skip the first, dummy, section.
269 const unsigned char *ps
= pshdrs
+ This::shdr_size
;
270 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= This::shdr_size
)
272 typename
This::Shdr
shdr(ps
);
274 unsigned int sh_type
= shdr
.get_sh_type();
275 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
278 unsigned int shndx
= this->adjust_shndx(shdr
.get_sh_info());
281 this->error(_("relocation section %u has bad info %u"),
286 Output_section
* os
= out_sections
[shndx
];
290 // We are scanning relocations in order to fill out the GOT and
291 // PLT sections. Relocations for sections which are not
292 // allocated (typically debugging sections) should not add new
293 // GOT and PLT entries. So we skip them unless this is a
294 // relocatable link or we need to emit relocations. FIXME: What
295 // should we do if a linker script maps a section with SHF_ALLOC
296 // clear to a section with SHF_ALLOC set?
297 typename
This::Shdr
secshdr(pshdrs
+ shndx
* This::shdr_size
);
298 bool is_section_allocated
= ((secshdr
.get_sh_flags() & elfcpp::SHF_ALLOC
)
300 if (!is_section_allocated
301 && !parameters
->options().relocatable()
302 && !parameters
->options().emit_relocs())
305 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx_
)
307 this->error(_("relocation section %u uses unexpected "
309 i
, this->adjust_shndx(shdr
.get_sh_link()));
313 off_t sh_size
= shdr
.get_sh_size();
315 unsigned int reloc_size
;
316 if (sh_type
== elfcpp::SHT_REL
)
317 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
319 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
320 if (reloc_size
!= shdr
.get_sh_entsize())
322 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
323 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
328 size_t reloc_count
= sh_size
/ reloc_size
;
329 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
331 this->error(_("reloc section %u size %lu uneven"),
332 i
, static_cast<unsigned long>(sh_size
));
336 rd
->relocs
.push_back(Section_relocs());
337 Section_relocs
& sr(rd
->relocs
.back());
339 sr
.data_shndx
= shndx
;
340 sr
.contents
= this->get_lasting_view(shdr
.get_sh_offset(), sh_size
,
342 sr
.sh_type
= sh_type
;
343 sr
.reloc_count
= reloc_count
;
344 sr
.output_section
= os
;
345 sr
.needs_special_offset_handling
= out_offsets
[shndx
] == invalid_address
;
346 sr
.is_data_section_allocated
= is_section_allocated
;
349 // Read the local symbols.
350 gold_assert(this->symtab_shndx_
!= -1U);
351 if (this->symtab_shndx_
== 0 || this->local_symbol_count_
== 0)
352 rd
->local_symbols
= NULL
;
355 typename
This::Shdr
symtabshdr(pshdrs
356 + this->symtab_shndx_
* This::shdr_size
);
357 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
358 const int sym_size
= This::sym_size
;
359 const unsigned int loccount
= this->local_symbol_count_
;
360 gold_assert(loccount
== symtabshdr
.get_sh_info());
361 off_t locsize
= loccount
* sym_size
;
362 rd
->local_symbols
= this->get_lasting_view(symtabshdr
.get_sh_offset(),
363 locsize
, true, true);
367 // Process the relocs to generate mappings from source sections to referenced
368 // sections. This is used during garbage colletion to determine garbage
371 template<int size
, bool big_endian
>
373 Sized_relobj
<size
, big_endian
>::do_gc_process_relocs(Symbol_table
* symtab
,
375 Read_relocs_data
* rd
)
377 Sized_target
<size
, big_endian
>* target
=
378 parameters
->sized_target
<size
, big_endian
>();
380 const unsigned char* local_symbols
;
381 if (rd
->local_symbols
== NULL
)
382 local_symbols
= NULL
;
384 local_symbols
= rd
->local_symbols
->data();
386 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
387 p
!= rd
->relocs
.end();
390 if (!parameters
->options().relocatable())
392 // As noted above, when not generating an object file, we
393 // only scan allocated sections. We may see a non-allocated
394 // section here if we are emitting relocs.
395 if (p
->is_data_section_allocated
)
396 target
->gc_process_relocs(symtab
, layout
, this,
397 p
->data_shndx
, p
->sh_type
,
398 p
->contents
->data(), p
->reloc_count
,
400 p
->needs_special_offset_handling
,
401 this->local_symbol_count_
,
408 // Scan the relocs and adjust the symbol table. This looks for
409 // relocations which require GOT/PLT/COPY relocations.
411 template<int size
, bool big_endian
>
413 Sized_relobj
<size
, big_endian
>::do_scan_relocs(Symbol_table
* symtab
,
415 Read_relocs_data
* rd
)
417 Sized_target
<size
, big_endian
>* target
=
418 parameters
->sized_target
<size
, big_endian
>();
420 const unsigned char* local_symbols
;
421 if (rd
->local_symbols
== NULL
)
422 local_symbols
= NULL
;
424 local_symbols
= rd
->local_symbols
->data();
426 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
427 p
!= rd
->relocs
.end();
430 // When garbage collection is on, unreferenced sections are not included
431 // in the link that would have been included normally. This is known only
432 // after Read_relocs hence this check has to be done again.
433 if (parameters
->options().gc_sections()
434 || parameters
->options().icf_enabled())
436 if (p
->output_section
== NULL
)
439 if (!parameters
->options().relocatable())
441 // As noted above, when not generating an object file, we
442 // only scan allocated sections. We may see a non-allocated
443 // section here if we are emitting relocs.
444 if (p
->is_data_section_allocated
)
445 target
->scan_relocs(symtab
, layout
, this, p
->data_shndx
,
446 p
->sh_type
, p
->contents
->data(),
447 p
->reloc_count
, p
->output_section
,
448 p
->needs_special_offset_handling
,
449 this->local_symbol_count_
,
451 if (parameters
->options().emit_relocs())
452 this->emit_relocs_scan(symtab
, layout
, local_symbols
, p
);
456 Relocatable_relocs
* rr
= this->relocatable_relocs(p
->reloc_shndx
);
457 gold_assert(rr
!= NULL
);
458 rr
->set_reloc_count(p
->reloc_count
);
459 target
->scan_relocatable_relocs(symtab
, layout
, this,
460 p
->data_shndx
, p
->sh_type
,
464 p
->needs_special_offset_handling
,
465 this->local_symbol_count_
,
474 if (rd
->local_symbols
!= NULL
)
476 delete rd
->local_symbols
;
477 rd
->local_symbols
= NULL
;
481 // This is a strategy class we use when scanning for --emit-relocs.
483 template<int sh_type
>
484 class Emit_relocs_strategy
487 // A local non-section symbol.
488 inline Relocatable_relocs::Reloc_strategy
489 local_non_section_strategy(unsigned int, Relobj
*, unsigned int)
490 { return Relocatable_relocs::RELOC_COPY
; }
492 // A local section symbol.
493 inline Relocatable_relocs::Reloc_strategy
494 local_section_strategy(unsigned int, Relobj
*)
496 if (sh_type
== elfcpp::SHT_RELA
)
497 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
500 // The addend is stored in the section contents. Since this
501 // is not a relocatable link, we are going to apply the
502 // relocation contents to the section as usual. This means
503 // that we have no way to record the original addend. If the
504 // original addend is not zero, there is basically no way for
505 // the user to handle this correctly. Caveat emptor.
506 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
511 inline Relocatable_relocs::Reloc_strategy
512 global_strategy(unsigned int, Relobj
*, unsigned int)
513 { return Relocatable_relocs::RELOC_COPY
; }
516 // Scan the input relocations for --emit-relocs.
518 template<int size
, bool big_endian
>
520 Sized_relobj
<size
, big_endian
>::emit_relocs_scan(
521 Symbol_table
* symtab
,
523 const unsigned char* plocal_syms
,
524 const Read_relocs_data::Relocs_list::iterator
& p
)
526 Relocatable_relocs
* rr
= this->relocatable_relocs(p
->reloc_shndx
);
527 gold_assert(rr
!= NULL
);
528 rr
->set_reloc_count(p
->reloc_count
);
530 if (p
->sh_type
== elfcpp::SHT_REL
)
531 this->emit_relocs_scan_reltype
<elfcpp::SHT_REL
>(symtab
, layout
,
535 gold_assert(p
->sh_type
== elfcpp::SHT_RELA
);
536 this->emit_relocs_scan_reltype
<elfcpp::SHT_RELA
>(symtab
, layout
,
541 // Scan the input relocation for --emit-relocs, templatized on the
542 // type of the relocation section.
544 template<int size
, bool big_endian
>
545 template<int sh_type
>
547 Sized_relobj
<size
, big_endian
>::emit_relocs_scan_reltype(
548 Symbol_table
* symtab
,
550 const unsigned char* plocal_syms
,
551 const Read_relocs_data::Relocs_list::iterator
& p
,
552 Relocatable_relocs
* rr
)
554 scan_relocatable_relocs
<size
, big_endian
, sh_type
,
555 Emit_relocs_strategy
<sh_type
> >(
563 p
->needs_special_offset_handling
,
564 this->local_symbol_count_
,
569 // Relocate the input sections and write out the local symbols.
571 template<int size
, bool big_endian
>
573 Sized_relobj
<size
, big_endian
>::do_relocate(const Symbol_table
* symtab
,
574 const Layout
* layout
,
577 unsigned int shnum
= this->shnum();
579 // Read the section headers.
580 const unsigned char* pshdrs
= this->get_view(this->elf_file_
.shoff(),
581 shnum
* This::shdr_size
,
587 // Make two passes over the sections. The first one copies the
588 // section data to the output file. The second one applies
591 this->write_sections(pshdrs
, of
, &views
);
593 // To speed up relocations, we set up hash tables for fast lookup of
594 // input offsets to output addresses.
595 this->initialize_input_to_output_maps();
597 // Apply relocations.
599 this->relocate_sections(symtab
, layout
, pshdrs
, &views
);
601 // After we've done the relocations, we release the hash tables,
602 // since we no longer need them.
603 this->free_input_to_output_maps();
605 // Write out the accumulated views.
606 for (unsigned int i
= 1; i
< shnum
; ++i
)
608 if (views
[i
].view
!= NULL
)
610 if (!views
[i
].is_postprocessing_view
)
612 if (views
[i
].is_input_output_view
)
613 of
->write_input_output_view(views
[i
].offset
,
617 of
->write_output_view(views
[i
].offset
, views
[i
].view_size
,
623 // Write out the local symbols.
624 this->write_local_symbols(of
, layout
->sympool(), layout
->dynpool(),
625 layout
->symtab_xindex(), layout
->dynsym_xindex());
627 // We should no longer need the local symbol values.
628 this->clear_local_symbols();
631 // Sort a Read_multiple vector by file offset.
632 struct Read_multiple_compare
635 operator()(const File_read::Read_multiple_entry
& rme1
,
636 const File_read::Read_multiple_entry
& rme2
) const
637 { return rme1
.file_offset
< rme2
.file_offset
; }
640 // Write section data to the output file. PSHDRS points to the
641 // section headers. Record the views in *PVIEWS for use when
644 template<int size
, bool big_endian
>
646 Sized_relobj
<size
, big_endian
>::write_sections(const unsigned char* pshdrs
,
650 unsigned int shnum
= this->shnum();
651 const Output_sections
& out_sections(this->output_sections());
652 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
654 File_read::Read_multiple rm
;
655 bool is_sorted
= true;
657 const unsigned char* p
= pshdrs
+ This::shdr_size
;
658 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
660 View_size
* pvs
= &(*pviews
)[i
];
664 const Output_section
* os
= out_sections
[i
];
667 Address output_offset
= out_offsets
[i
];
669 typename
This::Shdr
shdr(p
);
671 if (shdr
.get_sh_type() == elfcpp::SHT_NOBITS
)
674 if ((parameters
->options().relocatable()
675 || parameters
->options().emit_relocs())
676 && (shdr
.get_sh_type() == elfcpp::SHT_REL
677 || shdr
.get_sh_type() == elfcpp::SHT_RELA
)
678 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
680 // This is a reloc section in a relocatable link or when
681 // emitting relocs. We don't need to read the input file.
682 // The size and file offset are stored in the
683 // Relocatable_relocs structure.
684 Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
685 gold_assert(rr
!= NULL
);
686 Output_data
* posd
= rr
->output_data();
687 gold_assert(posd
!= NULL
);
689 pvs
->offset
= posd
->offset();
690 pvs
->view_size
= posd
->data_size();
691 pvs
->view
= of
->get_output_view(pvs
->offset
, pvs
->view_size
);
692 pvs
->address
= posd
->address();
693 pvs
->is_input_output_view
= false;
694 pvs
->is_postprocessing_view
= false;
699 // In the normal case, this input section is simply mapped to
700 // the output section at offset OUTPUT_OFFSET.
702 // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
703 // handled specially--e.g., a .eh_frame section. The relocation
704 // routines need to check for each reloc where it should be
705 // applied. For this case, we need an input/output view for the
706 // entire contents of the section in the output file. We don't
707 // want to copy the contents of the input section to the output
708 // section; the output section contents were already written,
709 // and we waited for them in Relocate_task::is_runnable because
710 // relocs_must_follow_section_writes is set for the object.
712 // Regardless of which of the above cases is true, we have to
713 // check requires_postprocessing of the output section. If that
714 // is false, then we work with views of the output file
715 // directly. If it is true, then we work with a separate
716 // buffer, and the output section is responsible for writing the
717 // final data to the output file.
719 off_t output_section_offset
;
720 Address output_section_size
;
721 if (!os
->requires_postprocessing())
723 output_section_offset
= os
->offset();
724 output_section_size
= convert_types
<Address
, off_t
>(os
->data_size());
728 output_section_offset
= 0;
729 output_section_size
=
730 convert_types
<Address
, off_t
>(os
->postprocessing_buffer_size());
734 section_size_type view_size
;
735 if (output_offset
!= invalid_address
)
737 view_start
= output_section_offset
+ output_offset
;
738 view_size
= convert_to_section_size_type(shdr
.get_sh_size());
742 view_start
= output_section_offset
;
743 view_size
= convert_to_section_size_type(output_section_size
);
749 gold_assert(output_offset
== invalid_address
750 || output_offset
+ view_size
<= output_section_size
);
753 if (os
->requires_postprocessing())
755 unsigned char* buffer
= os
->postprocessing_buffer();
756 view
= buffer
+ view_start
;
757 if (output_offset
!= invalid_address
)
759 off_t sh_offset
= shdr
.get_sh_offset();
760 if (!rm
.empty() && rm
.back().file_offset
> sh_offset
)
762 rm
.push_back(File_read::Read_multiple_entry(sh_offset
,
768 if (output_offset
== invalid_address
)
769 view
= of
->get_input_output_view(view_start
, view_size
);
772 view
= of
->get_output_view(view_start
, view_size
);
773 off_t sh_offset
= shdr
.get_sh_offset();
774 if (!rm
.empty() && rm
.back().file_offset
> sh_offset
)
776 rm
.push_back(File_read::Read_multiple_entry(sh_offset
,
782 pvs
->address
= os
->address();
783 if (output_offset
!= invalid_address
)
784 pvs
->address
+= output_offset
;
785 pvs
->offset
= view_start
;
786 pvs
->view_size
= view_size
;
787 pvs
->is_input_output_view
= output_offset
== invalid_address
;
788 pvs
->is_postprocessing_view
= os
->requires_postprocessing();
791 // Actually read the data.
795 std::sort(rm
.begin(), rm
.end(), Read_multiple_compare());
796 this->read_multiple(rm
);
800 // Relocate section data. VIEWS points to the section data as views
801 // in the output file.
803 template<int size
, bool big_endian
>
805 Sized_relobj
<size
, big_endian
>::do_relocate_sections(
806 const Symbol_table
* symtab
,
807 const Layout
* layout
,
808 const unsigned char* pshdrs
,
811 unsigned int shnum
= this->shnum();
812 Sized_target
<size
, big_endian
>* target
=
813 parameters
->sized_target
<size
, big_endian
>();
815 const Output_sections
& out_sections(this->output_sections());
816 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
818 Relocate_info
<size
, big_endian
> relinfo
;
819 relinfo
.symtab
= symtab
;
820 relinfo
.layout
= layout
;
821 relinfo
.object
= this;
823 const unsigned char* p
= pshdrs
+ This::shdr_size
;
824 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
826 typename
This::Shdr
shdr(p
);
828 unsigned int sh_type
= shdr
.get_sh_type();
829 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
832 off_t sh_size
= shdr
.get_sh_size();
836 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
837 if (index
>= this->shnum())
839 this->error(_("relocation section %u has bad info %u"),
844 Output_section
* os
= out_sections
[index
];
847 // This relocation section is against a section which we
851 Address output_offset
= out_offsets
[index
];
853 gold_assert((*pviews
)[index
].view
!= NULL
);
854 if (parameters
->options().relocatable())
855 gold_assert((*pviews
)[i
].view
!= NULL
);
857 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx_
)
859 gold_error(_("relocation section %u uses unexpected "
861 i
, this->adjust_shndx(shdr
.get_sh_link()));
865 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
866 sh_size
, true, false);
868 unsigned int reloc_size
;
869 if (sh_type
== elfcpp::SHT_REL
)
870 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
872 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
874 if (reloc_size
!= shdr
.get_sh_entsize())
876 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
877 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
882 size_t reloc_count
= sh_size
/ reloc_size
;
883 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
885 gold_error(_("reloc section %u size %lu uneven"),
886 i
, static_cast<unsigned long>(sh_size
));
890 gold_assert(output_offset
!= invalid_address
891 || this->relocs_must_follow_section_writes());
893 relinfo
.reloc_shndx
= i
;
894 relinfo
.reloc_shdr
= p
;
895 relinfo
.data_shndx
= index
;
896 relinfo
.data_shdr
= pshdrs
+ index
* This::shdr_size
;
897 unsigned char* view
= (*pviews
)[index
].view
;
898 Address address
= (*pviews
)[index
].address
;
899 section_size_type view_size
= (*pviews
)[index
].view_size
;
901 Reloc_symbol_changes
* reloc_map
= NULL
;
902 if (this->uses_split_stack() && output_offset
!= invalid_address
)
904 typename
This::Shdr
data_shdr(pshdrs
+ index
* This::shdr_size
);
905 if ((data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
906 this->split_stack_adjust(symtab
, pshdrs
, sh_type
, index
,
907 prelocs
, reloc_count
, view
, view_size
,
911 if (!parameters
->options().relocatable())
913 target
->relocate_section(&relinfo
, sh_type
, prelocs
, reloc_count
, os
,
914 output_offset
== invalid_address
,
915 view
, address
, view_size
, reloc_map
);
916 if (parameters
->options().emit_relocs())
917 this->emit_relocs(&relinfo
, i
, sh_type
, prelocs
, reloc_count
,
918 os
, output_offset
, view
, address
, view_size
,
919 (*pviews
)[i
].view
, (*pviews
)[i
].view_size
);
923 Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
924 target
->relocate_for_relocatable(&relinfo
, sh_type
, prelocs
,
925 reloc_count
, os
, output_offset
, rr
,
926 view
, address
, view_size
,
928 (*pviews
)[i
].view_size
);
933 // Emit the relocs for --emit-relocs.
935 template<int size
, bool big_endian
>
937 Sized_relobj
<size
, big_endian
>::emit_relocs(
938 const Relocate_info
<size
, big_endian
>* relinfo
,
940 unsigned int sh_type
,
941 const unsigned char* prelocs
,
943 Output_section
* output_section
,
944 typename
elfcpp::Elf_types
<size
>::Elf_Addr offset_in_output_section
,
946 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
947 section_size_type view_size
,
948 unsigned char* reloc_view
,
949 section_size_type reloc_view_size
)
951 if (sh_type
== elfcpp::SHT_REL
)
952 this->emit_relocs_reltype
<elfcpp::SHT_REL
>(relinfo
, i
, prelocs
,
953 reloc_count
, output_section
,
954 offset_in_output_section
,
955 view
, address
, view_size
,
956 reloc_view
, reloc_view_size
);
959 gold_assert(sh_type
== elfcpp::SHT_RELA
);
960 this->emit_relocs_reltype
<elfcpp::SHT_RELA
>(relinfo
, i
, prelocs
,
961 reloc_count
, output_section
,
962 offset_in_output_section
,
963 view
, address
, view_size
,
964 reloc_view
, reloc_view_size
);
968 // Emit the relocs for --emit-relocs, templatized on the type of the
969 // relocation section.
971 template<int size
, bool big_endian
>
972 template<int sh_type
>
974 Sized_relobj
<size
, big_endian
>::emit_relocs_reltype(
975 const Relocate_info
<size
, big_endian
>* relinfo
,
977 const unsigned char* prelocs
,
979 Output_section
* output_section
,
980 typename
elfcpp::Elf_types
<size
>::Elf_Addr offset_in_output_section
,
982 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
983 section_size_type view_size
,
984 unsigned char* reloc_view
,
985 section_size_type reloc_view_size
)
987 const Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
988 relocate_for_relocatable
<size
, big_endian
, sh_type
>(
993 offset_in_output_section
,
1002 // Create merge hash tables for the local symbols. These are used to
1003 // speed up relocations.
1005 template<int size
, bool big_endian
>
1007 Sized_relobj
<size
, big_endian
>::initialize_input_to_output_maps()
1009 const unsigned int loccount
= this->local_symbol_count_
;
1010 for (unsigned int i
= 1; i
< loccount
; ++i
)
1012 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1013 lv
.initialize_input_to_output_map(this);
1017 // Free merge hash tables for the local symbols.
1019 template<int size
, bool big_endian
>
1021 Sized_relobj
<size
, big_endian
>::free_input_to_output_maps()
1023 const unsigned int loccount
= this->local_symbol_count_
;
1024 for (unsigned int i
= 1; i
< loccount
; ++i
)
1026 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1027 lv
.free_input_to_output_map();
1031 // If an object was compiled with -fsplit-stack, this is called to
1032 // check whether any relocations refer to functions defined in objects
1033 // which were not compiled with -fsplit-stack. If they were, then we
1034 // need to apply some target-specific adjustments to request
1035 // additional stack space.
1037 template<int size
, bool big_endian
>
1039 Sized_relobj
<size
, big_endian
>::split_stack_adjust(
1040 const Symbol_table
* symtab
,
1041 const unsigned char* pshdrs
,
1042 unsigned int sh_type
,
1044 const unsigned char* prelocs
,
1046 unsigned char* view
,
1047 section_size_type view_size
,
1048 Reloc_symbol_changes
** reloc_map
)
1050 if (sh_type
== elfcpp::SHT_REL
)
1051 this->split_stack_adjust_reltype
<elfcpp::SHT_REL
>(symtab
, pshdrs
, shndx
,
1052 prelocs
, reloc_count
,
1057 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1058 this->split_stack_adjust_reltype
<elfcpp::SHT_RELA
>(symtab
, pshdrs
, shndx
,
1059 prelocs
, reloc_count
,
1065 // Adjust for -fsplit-stack, templatized on the type of the relocation
1068 template<int size
, bool big_endian
>
1069 template<int sh_type
>
1071 Sized_relobj
<size
, big_endian
>::split_stack_adjust_reltype(
1072 const Symbol_table
* symtab
,
1073 const unsigned char* pshdrs
,
1075 const unsigned char* prelocs
,
1077 unsigned char* view
,
1078 section_size_type view_size
,
1079 Reloc_symbol_changes
** reloc_map
)
1081 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
1082 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1084 size_t local_count
= this->local_symbol_count();
1086 std::vector
<section_offset_type
> non_split_refs
;
1088 const unsigned char* pr
= prelocs
;
1089 for (size_t i
= 0; i
< reloc_count
; ++i
, pr
+= reloc_size
)
1093 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
1094 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1095 if (r_sym
< local_count
)
1098 const Symbol
* gsym
= this->global_symbol(r_sym
);
1099 gold_assert(gsym
!= NULL
);
1100 if (gsym
->is_forwarder())
1101 gsym
= symtab
->resolve_forwards(gsym
);
1103 // See if this relocation refers to a function defined in an
1104 // object compiled without -fsplit-stack. Note that we don't
1105 // care about the type of relocation--this means that in some
1106 // cases we will ask for a large stack unnecessarily, but this
1107 // is not fatal. FIXME: Some targets have symbols which are
1108 // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC.
1109 if (gsym
->type() == elfcpp::STT_FUNC
1110 && !gsym
->is_undefined()
1111 && gsym
->source() == Symbol::FROM_OBJECT
1112 && !gsym
->object()->uses_split_stack())
1114 section_offset_type offset
=
1115 convert_to_section_size_type(reloc
.get_r_offset());
1116 non_split_refs
.push_back(offset
);
1120 if (non_split_refs
.empty())
1123 // At this point, every entry in NON_SPLIT_REFS indicates a
1124 // relocation which refers to a function in an object compiled
1125 // without -fsplit-stack. We now have to convert that list into a
1126 // set of offsets to functions. First, we find all the functions.
1128 Function_offsets function_offsets
;
1129 this->find_functions(pshdrs
, shndx
, &function_offsets
);
1130 if (function_offsets
.empty())
1133 // Now get a list of the function with references to non split-stack
1136 Function_offsets calls_non_split
;
1137 for (std::vector
<section_offset_type
>::const_iterator p
1138 = non_split_refs
.begin();
1139 p
!= non_split_refs
.end();
1142 Function_offsets::const_iterator low
= function_offsets
.lower_bound(*p
);
1143 if (low
== function_offsets
.end())
1145 else if (low
->first
== *p
)
1147 else if (low
== function_offsets
.begin())
1152 calls_non_split
.insert(*low
);
1154 if (calls_non_split
.empty())
1157 // Now we have a set of functions to adjust. The adjustments are
1158 // target specific. Besides changing the output section view
1159 // however, it likes, the target may request a relocation change
1160 // from one global symbol name to another.
1162 for (Function_offsets::const_iterator p
= calls_non_split
.begin();
1163 p
!= calls_non_split
.end();
1168 parameters
->target().calls_non_split(this, shndx
, p
->first
, p
->second
,
1169 view
, view_size
, &from
, &to
);
1172 gold_assert(!to
.empty());
1173 Symbol
* tosym
= NULL
;
1175 // Find relocations in the relevant function which are for
1178 for (size_t i
= 0; i
< reloc_count
; ++i
, pr
+= reloc_size
)
1182 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
=
1184 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1185 if (r_sym
< local_count
)
1188 section_offset_type offset
=
1189 convert_to_section_size_type(reloc
.get_r_offset());
1190 if (offset
< p
->first
1193 + static_cast<section_offset_type
>(p
->second
))))
1196 const Symbol
* gsym
= this->global_symbol(r_sym
);
1197 if (from
== gsym
->name())
1201 tosym
= symtab
->lookup(to
.c_str());
1204 this->error(_("could not convert call "
1206 from
.c_str(), to
.c_str());
1211 if (*reloc_map
== NULL
)
1212 *reloc_map
= new Reloc_symbol_changes(reloc_count
);
1213 (*reloc_map
)->set(i
, tosym
);
1220 // Find all the function in this object defined in section SHNDX.
1221 // Store their offsets in the section in FUNCTION_OFFSETS.
1223 template<int size
, bool big_endian
>
1225 Sized_relobj
<size
, big_endian
>::find_functions(
1226 const unsigned char* pshdrs
,
1228 Sized_relobj
<size
, big_endian
>::Function_offsets
* function_offsets
)
1230 // We need to read the symbols to find the functions. If we wanted
1231 // to, we could cache reading the symbols across all sections in the
1233 const unsigned int symtab_shndx
= this->symtab_shndx_
;
1234 typename
This::Shdr
symtabshdr(pshdrs
+ symtab_shndx
* This::shdr_size
);
1235 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
1237 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_size
=
1238 symtabshdr
.get_sh_size();
1239 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
1240 sh_size
, true, true);
1242 const int sym_size
= This::sym_size
;
1243 const unsigned int symcount
= sh_size
/ sym_size
;
1244 for (unsigned int i
= 0; i
< symcount
; ++i
, psyms
+= sym_size
)
1246 typename
elfcpp::Sym
<size
, big_endian
> isym(psyms
);
1248 // FIXME: Some targets can have functions which do not have type
1249 // STT_FUNC, e.g., STT_ARM_TFUNC.
1250 if (isym
.get_st_type() != elfcpp::STT_FUNC
1251 || isym
.get_st_size() == 0)
1255 unsigned int sym_shndx
= this->adjust_sym_shndx(i
, isym
.get_st_shndx(),
1257 if (!is_ordinary
|| sym_shndx
!= shndx
)
1260 section_offset_type value
=
1261 convert_to_section_size_type(isym
.get_st_value());
1262 section_size_type fnsize
=
1263 convert_to_section_size_type(isym
.get_st_size());
1265 (*function_offsets
)[value
] = fnsize
;
1269 // Class Merged_symbol_value.
1273 Merged_symbol_value
<size
>::initialize_input_to_output_map(
1274 const Relobj
* object
,
1275 unsigned int input_shndx
)
1277 Object_merge_map
* map
= object
->merge_map();
1278 map
->initialize_input_to_output_map
<size
>(input_shndx
,
1279 this->output_start_address_
,
1280 &this->output_addresses_
);
1283 // Get the output value corresponding to an input offset if we
1284 // couldn't find it in the hash table.
1287 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1288 Merged_symbol_value
<size
>::value_from_output_section(
1289 const Relobj
* object
,
1290 unsigned int input_shndx
,
1291 typename
elfcpp::Elf_types
<size
>::Elf_Addr input_offset
) const
1293 section_offset_type output_offset
;
1294 bool found
= object
->merge_map()->get_output_offset(NULL
, input_shndx
,
1298 // If this assertion fails, it means that some relocation was
1299 // against a portion of an input merge section which we didn't map
1300 // to the output file and we didn't explicitly discard. We should
1301 // always map all portions of input merge sections.
1304 if (output_offset
== -1)
1307 return this->output_start_address_
+ output_offset
;
1310 // Track_relocs methods.
1312 // Initialize the class to track the relocs. This gets the object,
1313 // the reloc section index, and the type of the relocs. This returns
1314 // false if something goes wrong.
1316 template<int size
, bool big_endian
>
1318 Track_relocs
<size
, big_endian
>::initialize(
1320 unsigned int reloc_shndx
,
1321 unsigned int reloc_type
)
1323 // If RELOC_SHNDX is -1U, it means there is more than one reloc
1324 // section for the .eh_frame section. We can't handle that case.
1325 if (reloc_shndx
== -1U)
1328 // If RELOC_SHNDX is 0, there is no reloc section.
1329 if (reloc_shndx
== 0)
1332 // Get the contents of the reloc section.
1333 this->prelocs_
= object
->section_contents(reloc_shndx
, &this->len_
, false);
1335 if (reloc_type
== elfcpp::SHT_REL
)
1336 this->reloc_size_
= elfcpp::Elf_sizes
<size
>::rel_size
;
1337 else if (reloc_type
== elfcpp::SHT_RELA
)
1338 this->reloc_size_
= elfcpp::Elf_sizes
<size
>::rela_size
;
1342 if (this->len_
% this->reloc_size_
!= 0)
1344 object
->error(_("reloc section size %zu is not a multiple of "
1346 static_cast<size_t>(this->len_
),
1354 // Return the offset of the next reloc, or -1 if there isn't one.
1356 template<int size
, bool big_endian
>
1358 Track_relocs
<size
, big_endian
>::next_offset() const
1360 if (this->pos_
>= this->len_
)
1363 // Rel and Rela start out the same, so we can always use Rel to find
1364 // the r_offset value.
1365 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1366 return rel
.get_r_offset();
1369 // Return the index of the symbol referenced by the next reloc, or -1U
1370 // if there aren't any more relocs.
1372 template<int size
, bool big_endian
>
1374 Track_relocs
<size
, big_endian
>::next_symndx() const
1376 if (this->pos_
>= this->len_
)
1379 // Rel and Rela start out the same, so we can use Rel to find the
1381 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1382 return elfcpp::elf_r_sym
<size
>(rel
.get_r_info());
1385 // Advance to the next reloc whose r_offset is greater than or equal
1386 // to OFFSET. Return the number of relocs we skip.
1388 template<int size
, bool big_endian
>
1390 Track_relocs
<size
, big_endian
>::advance(off_t offset
)
1393 while (this->pos_
< this->len_
)
1395 // Rel and Rela start out the same, so we can always use Rel to
1396 // find the r_offset value.
1397 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1398 if (static_cast<off_t
>(rel
.get_r_offset()) >= offset
)
1401 this->pos_
+= this->reloc_size_
;
1406 // Instantiate the templates we need.
1408 #ifdef HAVE_TARGET_32_LITTLE
1411 Sized_relobj
<32, false>::do_read_relocs(Read_relocs_data
* rd
);
1414 #ifdef HAVE_TARGET_32_BIG
1417 Sized_relobj
<32, true>::do_read_relocs(Read_relocs_data
* rd
);
1420 #ifdef HAVE_TARGET_64_LITTLE
1423 Sized_relobj
<64, false>::do_read_relocs(Read_relocs_data
* rd
);
1426 #ifdef HAVE_TARGET_64_BIG
1429 Sized_relobj
<64, true>::do_read_relocs(Read_relocs_data
* rd
);
1432 #ifdef HAVE_TARGET_32_LITTLE
1435 Sized_relobj
<32, false>::do_gc_process_relocs(Symbol_table
* symtab
,
1437 Read_relocs_data
* rd
);
1440 #ifdef HAVE_TARGET_32_BIG
1443 Sized_relobj
<32, true>::do_gc_process_relocs(Symbol_table
* symtab
,
1445 Read_relocs_data
* rd
);
1448 #ifdef HAVE_TARGET_64_LITTLE
1451 Sized_relobj
<64, false>::do_gc_process_relocs(Symbol_table
* symtab
,
1453 Read_relocs_data
* rd
);
1456 #ifdef HAVE_TARGET_64_BIG
1459 Sized_relobj
<64, true>::do_gc_process_relocs(Symbol_table
* symtab
,
1461 Read_relocs_data
* rd
);
1464 #ifdef HAVE_TARGET_32_LITTLE
1467 Sized_relobj
<32, false>::do_scan_relocs(Symbol_table
* symtab
,
1469 Read_relocs_data
* rd
);
1472 #ifdef HAVE_TARGET_32_BIG
1475 Sized_relobj
<32, true>::do_scan_relocs(Symbol_table
* symtab
,
1477 Read_relocs_data
* rd
);
1480 #ifdef HAVE_TARGET_64_LITTLE
1483 Sized_relobj
<64, false>::do_scan_relocs(Symbol_table
* symtab
,
1485 Read_relocs_data
* rd
);
1488 #ifdef HAVE_TARGET_64_BIG
1491 Sized_relobj
<64, true>::do_scan_relocs(Symbol_table
* symtab
,
1493 Read_relocs_data
* rd
);
1496 #ifdef HAVE_TARGET_32_LITTLE
1499 Sized_relobj
<32, false>::do_relocate(const Symbol_table
* symtab
,
1500 const Layout
* layout
,
1504 #ifdef HAVE_TARGET_32_BIG
1507 Sized_relobj
<32, true>::do_relocate(const Symbol_table
* symtab
,
1508 const Layout
* layout
,
1512 #ifdef HAVE_TARGET_64_LITTLE
1515 Sized_relobj
<64, false>::do_relocate(const Symbol_table
* symtab
,
1516 const Layout
* layout
,
1520 #ifdef HAVE_TARGET_64_BIG
1523 Sized_relobj
<64, true>::do_relocate(const Symbol_table
* symtab
,
1524 const Layout
* layout
,
1528 #ifdef HAVE_TARGET_32_LITTLE
1531 Sized_relobj
<32, false>::do_relocate_sections(
1532 const Symbol_table
* symtab
,
1533 const Layout
* layout
,
1534 const unsigned char* pshdrs
,
1538 #ifdef HAVE_TARGET_32_BIG
1541 Sized_relobj
<32, true>::do_relocate_sections(
1542 const Symbol_table
* symtab
,
1543 const Layout
* layout
,
1544 const unsigned char* pshdrs
,
1548 #ifdef HAVE_TARGET_64_LITTLE
1551 Sized_relobj
<64, false>::do_relocate_sections(
1552 const Symbol_table
* symtab
,
1553 const Layout
* layout
,
1554 const unsigned char* pshdrs
,
1558 #ifdef HAVE_TARGET_64_BIG
1561 Sized_relobj
<64, true>::do_relocate_sections(
1562 const Symbol_table
* symtab
,
1563 const Layout
* layout
,
1564 const unsigned char* pshdrs
,
1568 #ifdef HAVE_TARGET_32_LITTLE
1571 Sized_relobj
<32, false>::initialize_input_to_output_maps();
1575 Sized_relobj
<32, false>::free_input_to_output_maps();
1578 #ifdef HAVE_TARGET_32_BIG
1581 Sized_relobj
<32, true>::initialize_input_to_output_maps();
1585 Sized_relobj
<32, true>::free_input_to_output_maps();
1588 #ifdef HAVE_TARGET_64_LITTLE
1591 Sized_relobj
<64, false>::initialize_input_to_output_maps();
1595 Sized_relobj
<64, false>::free_input_to_output_maps();
1598 #ifdef HAVE_TARGET_64_BIG
1601 Sized_relobj
<64, true>::initialize_input_to_output_maps();
1605 Sized_relobj
<64, true>::free_input_to_output_maps();
1608 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1610 class Merged_symbol_value
<32>;
1613 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1615 class Merged_symbol_value
<64>;
1618 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1620 class Symbol_value
<32>;
1623 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1625 class Symbol_value
<64>;
1628 #ifdef HAVE_TARGET_32_LITTLE
1630 class Track_relocs
<32, false>;
1633 #ifdef HAVE_TARGET_32_BIG
1635 class Track_relocs
<32, true>;
1638 #ifdef HAVE_TARGET_64_LITTLE
1640 class Track_relocs
<64, false>;
1643 #ifdef HAVE_TARGET_64_BIG
1645 class Track_relocs
<64, true>;
1648 } // End namespace gold.