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"
35 #include "compressed_output.h"
40 // Read_relocs methods.
42 // These tasks just read the relocation information from the file.
43 // After reading it, the start another task to process the
44 // information. These tasks requires access to the file.
47 Read_relocs::is_runnable()
49 return this->object_
->is_locked() ? this->object_
->token() : NULL
;
55 Read_relocs::locks(Task_locker
* tl
)
57 tl
->add(this, this->object_
->token());
60 // Read the relocations and then start a Scan_relocs_task.
63 Read_relocs::run(Workqueue
* workqueue
)
65 Read_relocs_data
*rd
= new Read_relocs_data
;
66 this->object_
->read_relocs(rd
);
67 this->object_
->set_relocs_data(rd
);
68 this->object_
->release();
70 // If garbage collection or identical comdat folding is desired, we
71 // process the relocs first before scanning them. Scanning of relocs is
72 // done only after garbage or identical sections is identified.
73 if (parameters
->options().gc_sections()
74 || parameters
->options().icf_enabled())
76 workqueue
->queue_next(new Gc_process_relocs(this->symtab_
,
80 this->next_blocker_
));
84 workqueue
->queue_next(new Scan_relocs(this->symtab_
, this->layout_
,
87 this->next_blocker_
));
91 // Return a debugging name for the task.
94 Read_relocs::get_name() const
96 return "Read_relocs " + this->object_
->name();
99 // Gc_process_relocs methods.
101 Gc_process_relocs::~Gc_process_relocs()
103 if (this->this_blocker_
!= NULL
)
104 delete this->this_blocker_
;
107 // These tasks process the relocations read by Read_relocs and
108 // determine which sections are referenced and which are garbage.
109 // This task is done only when --gc-sections is used. This is blocked
110 // by THIS_BLOCKER_. It unblocks NEXT_BLOCKER_.
113 Gc_process_relocs::is_runnable()
115 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
116 return this->this_blocker_
;
117 if (this->object_
->is_locked())
118 return this->object_
->token();
123 Gc_process_relocs::locks(Task_locker
* tl
)
125 tl
->add(this, this->object_
->token());
126 tl
->add(this, this->next_blocker_
);
130 Gc_process_relocs::run(Workqueue
*)
132 this->object_
->gc_process_relocs(this->symtab_
, this->layout_
, this->rd_
);
133 this->object_
->release();
136 // Return a debugging name for the task.
139 Gc_process_relocs::get_name() const
141 return "Gc_process_relocs " + this->object_
->name();
144 // Scan_relocs methods.
146 Scan_relocs::~Scan_relocs()
148 if (this->this_blocker_
!= NULL
)
149 delete this->this_blocker_
;
152 // These tasks scan the relocations read by Read_relocs and mark up
153 // the symbol table to indicate which relocations are required. We
154 // use a lock on the symbol table to keep them from interfering with
158 Scan_relocs::is_runnable()
160 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
161 return this->this_blocker_
;
162 if (this->object_
->is_locked())
163 return this->object_
->token();
167 // Return the locks we hold: one on the file, one on the symbol table
171 Scan_relocs::locks(Task_locker
* tl
)
173 tl
->add(this, this->object_
->token());
174 tl
->add(this, this->next_blocker_
);
180 Scan_relocs::run(Workqueue
*)
182 this->object_
->scan_relocs(this->symtab_
, this->layout_
, this->rd_
);
185 this->object_
->release();
188 // Return a debugging name for the task.
191 Scan_relocs::get_name() const
193 return "Scan_relocs " + this->object_
->name();
196 // Relocate_task methods.
198 // We may have to wait for the output sections to be written.
201 Relocate_task::is_runnable()
203 if (this->object_
->relocs_must_follow_section_writes()
204 && this->output_sections_blocker_
->is_blocked())
205 return this->output_sections_blocker_
;
207 if (this->object_
->is_locked())
208 return this->object_
->token();
213 // We want to lock the file while we run. We want to unblock
214 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
215 // INPUT_SECTIONS_BLOCKER may be NULL.
218 Relocate_task::locks(Task_locker
* tl
)
220 if (this->input_sections_blocker_
!= NULL
)
221 tl
->add(this, this->input_sections_blocker_
);
222 tl
->add(this, this->final_blocker_
);
223 tl
->add(this, this->object_
->token());
229 Relocate_task::run(Workqueue
*)
231 this->object_
->relocate(this->symtab_
, this->layout_
, this->of_
);
233 // This is normally the last thing we will do with an object, so
234 // uncache all views.
235 this->object_
->clear_view_cache_marks();
237 this->object_
->release();
240 // Return a debugging name for the task.
243 Relocate_task::get_name() const
245 return "Relocate_task " + this->object_
->name();
248 // Read the relocs and local symbols from the object file and store
249 // the information in RD.
251 template<int size
, bool big_endian
>
253 Sized_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
257 unsigned int shnum
= this->shnum();
261 rd
->relocs
.reserve(shnum
/ 2);
263 const Output_sections
& out_sections(this->output_sections());
264 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
266 const unsigned char *pshdrs
= this->get_view(this->elf_file_
.shoff(),
267 shnum
* This::shdr_size
,
269 // Skip the first, dummy, section.
270 const unsigned char *ps
= pshdrs
+ This::shdr_size
;
271 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= This::shdr_size
)
273 typename
This::Shdr
shdr(ps
);
275 unsigned int sh_type
= shdr
.get_sh_type();
276 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
279 unsigned int shndx
= this->adjust_shndx(shdr
.get_sh_info());
282 this->error(_("relocation section %u has bad info %u"),
287 Output_section
* os
= out_sections
[shndx
];
291 // We are scanning relocations in order to fill out the GOT and
292 // PLT sections. Relocations for sections which are not
293 // allocated (typically debugging sections) should not add new
294 // GOT and PLT entries. So we skip them unless this is a
295 // relocatable link or we need to emit relocations. FIXME: What
296 // should we do if a linker script maps a section with SHF_ALLOC
297 // clear to a section with SHF_ALLOC set?
298 typename
This::Shdr
secshdr(pshdrs
+ shndx
* This::shdr_size
);
299 bool is_section_allocated
= ((secshdr
.get_sh_flags() & elfcpp::SHF_ALLOC
)
301 if (!is_section_allocated
302 && !parameters
->options().relocatable()
303 && !parameters
->options().emit_relocs())
306 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx_
)
308 this->error(_("relocation section %u uses unexpected "
310 i
, this->adjust_shndx(shdr
.get_sh_link()));
314 off_t sh_size
= shdr
.get_sh_size();
316 unsigned int reloc_size
;
317 if (sh_type
== elfcpp::SHT_REL
)
318 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
320 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
321 if (reloc_size
!= shdr
.get_sh_entsize())
323 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
324 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
329 size_t reloc_count
= sh_size
/ reloc_size
;
330 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
332 this->error(_("reloc section %u size %lu uneven"),
333 i
, static_cast<unsigned long>(sh_size
));
337 rd
->relocs
.push_back(Section_relocs());
338 Section_relocs
& sr(rd
->relocs
.back());
340 sr
.data_shndx
= shndx
;
341 sr
.contents
= this->get_lasting_view(shdr
.get_sh_offset(), sh_size
,
343 sr
.sh_type
= sh_type
;
344 sr
.reloc_count
= reloc_count
;
345 sr
.output_section
= os
;
346 sr
.needs_special_offset_handling
= out_offsets
[shndx
] == invalid_address
;
347 sr
.is_data_section_allocated
= is_section_allocated
;
350 // Read the local symbols.
351 gold_assert(this->symtab_shndx_
!= -1U);
352 if (this->symtab_shndx_
== 0 || this->local_symbol_count_
== 0)
353 rd
->local_symbols
= NULL
;
356 typename
This::Shdr
symtabshdr(pshdrs
357 + this->symtab_shndx_
* This::shdr_size
);
358 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
359 const int sym_size
= This::sym_size
;
360 const unsigned int loccount
= this->local_symbol_count_
;
361 gold_assert(loccount
== symtabshdr
.get_sh_info());
362 off_t locsize
= loccount
* sym_size
;
363 rd
->local_symbols
= this->get_lasting_view(symtabshdr
.get_sh_offset(),
364 locsize
, true, true);
368 // Process the relocs to generate mappings from source sections to referenced
369 // sections. This is used during garbage colletion to determine garbage
372 template<int size
, bool big_endian
>
374 Sized_relobj
<size
, big_endian
>::do_gc_process_relocs(Symbol_table
* symtab
,
376 Read_relocs_data
* rd
)
378 Sized_target
<size
, big_endian
>* target
=
379 parameters
->sized_target
<size
, big_endian
>();
381 const unsigned char* local_symbols
;
382 if (rd
->local_symbols
== NULL
)
383 local_symbols
= NULL
;
385 local_symbols
= rd
->local_symbols
->data();
387 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
388 p
!= rd
->relocs
.end();
391 if (!parameters
->options().relocatable())
393 // As noted above, when not generating an object file, we
394 // only scan allocated sections. We may see a non-allocated
395 // section here if we are emitting relocs.
396 if (p
->is_data_section_allocated
)
397 target
->gc_process_relocs(symtab
, layout
, this,
398 p
->data_shndx
, p
->sh_type
,
399 p
->contents
->data(), p
->reloc_count
,
401 p
->needs_special_offset_handling
,
402 this->local_symbol_count_
,
409 // Scan the relocs and adjust the symbol table. This looks for
410 // relocations which require GOT/PLT/COPY relocations.
412 template<int size
, bool big_endian
>
414 Sized_relobj
<size
, big_endian
>::do_scan_relocs(Symbol_table
* symtab
,
416 Read_relocs_data
* rd
)
418 Sized_target
<size
, big_endian
>* target
=
419 parameters
->sized_target
<size
, big_endian
>();
421 const unsigned char* local_symbols
;
422 if (rd
->local_symbols
== NULL
)
423 local_symbols
= NULL
;
425 local_symbols
= rd
->local_symbols
->data();
427 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
428 p
!= rd
->relocs
.end();
431 // When garbage collection is on, unreferenced sections are not included
432 // in the link that would have been included normally. This is known only
433 // after Read_relocs hence this check has to be done again.
434 if (parameters
->options().gc_sections()
435 || parameters
->options().icf_enabled())
437 if (p
->output_section
== NULL
)
440 if (!parameters
->options().relocatable())
442 // As noted above, when not generating an object file, we
443 // only scan allocated sections. We may see a non-allocated
444 // section here if we are emitting relocs.
445 if (p
->is_data_section_allocated
)
446 target
->scan_relocs(symtab
, layout
, this, p
->data_shndx
,
447 p
->sh_type
, p
->contents
->data(),
448 p
->reloc_count
, p
->output_section
,
449 p
->needs_special_offset_handling
,
450 this->local_symbol_count_
,
452 if (parameters
->options().emit_relocs())
453 this->emit_relocs_scan(symtab
, layout
, local_symbols
, p
);
457 Relocatable_relocs
* rr
= this->relocatable_relocs(p
->reloc_shndx
);
458 gold_assert(rr
!= NULL
);
459 rr
->set_reloc_count(p
->reloc_count
);
460 target
->scan_relocatable_relocs(symtab
, layout
, this,
461 p
->data_shndx
, p
->sh_type
,
465 p
->needs_special_offset_handling
,
466 this->local_symbol_count_
,
475 if (rd
->local_symbols
!= NULL
)
477 delete rd
->local_symbols
;
478 rd
->local_symbols
= NULL
;
482 // This is a strategy class we use when scanning for --emit-relocs.
484 template<int sh_type
>
485 class Emit_relocs_strategy
488 // A local non-section symbol.
489 inline Relocatable_relocs::Reloc_strategy
490 local_non_section_strategy(unsigned int, Relobj
*, unsigned int)
491 { return Relocatable_relocs::RELOC_COPY
; }
493 // A local section symbol.
494 inline Relocatable_relocs::Reloc_strategy
495 local_section_strategy(unsigned int, Relobj
*)
497 if (sh_type
== elfcpp::SHT_RELA
)
498 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
501 // The addend is stored in the section contents. Since this
502 // is not a relocatable link, we are going to apply the
503 // relocation contents to the section as usual. This means
504 // that we have no way to record the original addend. If the
505 // original addend is not zero, there is basically no way for
506 // the user to handle this correctly. Caveat emptor.
507 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
512 inline Relocatable_relocs::Reloc_strategy
513 global_strategy(unsigned int, Relobj
*, unsigned int)
514 { return Relocatable_relocs::RELOC_COPY
; }
517 // Scan the input relocations for --emit-relocs.
519 template<int size
, bool big_endian
>
521 Sized_relobj
<size
, big_endian
>::emit_relocs_scan(
522 Symbol_table
* symtab
,
524 const unsigned char* plocal_syms
,
525 const Read_relocs_data::Relocs_list::iterator
& p
)
527 Relocatable_relocs
* rr
= this->relocatable_relocs(p
->reloc_shndx
);
528 gold_assert(rr
!= NULL
);
529 rr
->set_reloc_count(p
->reloc_count
);
531 if (p
->sh_type
== elfcpp::SHT_REL
)
532 this->emit_relocs_scan_reltype
<elfcpp::SHT_REL
>(symtab
, layout
,
536 gold_assert(p
->sh_type
== elfcpp::SHT_RELA
);
537 this->emit_relocs_scan_reltype
<elfcpp::SHT_RELA
>(symtab
, layout
,
542 // Scan the input relocation for --emit-relocs, templatized on the
543 // type of the relocation section.
545 template<int size
, bool big_endian
>
546 template<int sh_type
>
548 Sized_relobj
<size
, big_endian
>::emit_relocs_scan_reltype(
549 Symbol_table
* symtab
,
551 const unsigned char* plocal_syms
,
552 const Read_relocs_data::Relocs_list::iterator
& p
,
553 Relocatable_relocs
* rr
)
555 scan_relocatable_relocs
<size
, big_endian
, sh_type
,
556 Emit_relocs_strategy
<sh_type
> >(
564 p
->needs_special_offset_handling
,
565 this->local_symbol_count_
,
570 // Relocate the input sections and write out the local symbols.
572 template<int size
, bool big_endian
>
574 Sized_relobj
<size
, big_endian
>::do_relocate(const Symbol_table
* symtab
,
575 const Layout
* layout
,
578 unsigned int shnum
= this->shnum();
580 // Read the section headers.
581 const unsigned char* pshdrs
= this->get_view(this->elf_file_
.shoff(),
582 shnum
* This::shdr_size
,
588 // Make two passes over the sections. The first one copies the
589 // section data to the output file. The second one applies
592 this->write_sections(pshdrs
, of
, &views
);
594 // To speed up relocations, we set up hash tables for fast lookup of
595 // input offsets to output addresses.
596 this->initialize_input_to_output_maps();
598 // Apply relocations.
600 this->relocate_sections(symtab
, layout
, pshdrs
, &views
);
602 // After we've done the relocations, we release the hash tables,
603 // since we no longer need them.
604 this->free_input_to_output_maps();
606 // Write out the accumulated views.
607 for (unsigned int i
= 1; i
< shnum
; ++i
)
609 if (views
[i
].view
!= NULL
)
611 if (!views
[i
].is_postprocessing_view
)
613 if (views
[i
].is_input_output_view
)
614 of
->write_input_output_view(views
[i
].offset
,
618 of
->write_output_view(views
[i
].offset
, views
[i
].view_size
,
624 // Write out the local symbols.
625 this->write_local_symbols(of
, layout
->sympool(), layout
->dynpool(),
626 layout
->symtab_xindex(), layout
->dynsym_xindex());
628 // We should no longer need the local symbol values.
629 this->clear_local_symbols();
632 // Sort a Read_multiple vector by file offset.
633 struct Read_multiple_compare
636 operator()(const File_read::Read_multiple_entry
& rme1
,
637 const File_read::Read_multiple_entry
& rme2
) const
638 { return rme1
.file_offset
< rme2
.file_offset
; }
641 // Write section data to the output file. PSHDRS points to the
642 // section headers. Record the views in *PVIEWS for use when
645 template<int size
, bool big_endian
>
647 Sized_relobj
<size
, big_endian
>::write_sections(const unsigned char* pshdrs
,
651 unsigned int shnum
= this->shnum();
652 const Output_sections
& out_sections(this->output_sections());
653 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
655 File_read::Read_multiple rm
;
656 bool is_sorted
= true;
658 const unsigned char* p
= pshdrs
+ This::shdr_size
;
659 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
661 View_size
* pvs
= &(*pviews
)[i
];
665 const Output_section
* os
= out_sections
[i
];
668 Address output_offset
= out_offsets
[i
];
670 typename
This::Shdr
shdr(p
);
672 if (shdr
.get_sh_type() == elfcpp::SHT_NOBITS
)
675 if ((parameters
->options().relocatable()
676 || parameters
->options().emit_relocs())
677 && (shdr
.get_sh_type() == elfcpp::SHT_REL
678 || shdr
.get_sh_type() == elfcpp::SHT_RELA
)
679 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
681 // This is a reloc section in a relocatable link or when
682 // emitting relocs. We don't need to read the input file.
683 // The size and file offset are stored in the
684 // Relocatable_relocs structure.
685 Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
686 gold_assert(rr
!= NULL
);
687 Output_data
* posd
= rr
->output_data();
688 gold_assert(posd
!= NULL
);
690 pvs
->offset
= posd
->offset();
691 pvs
->view_size
= posd
->data_size();
692 pvs
->view
= of
->get_output_view(pvs
->offset
, pvs
->view_size
);
693 pvs
->address
= posd
->address();
694 pvs
->is_input_output_view
= false;
695 pvs
->is_postprocessing_view
= false;
700 // In the normal case, this input section is simply mapped to
701 // the output section at offset OUTPUT_OFFSET.
703 // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
704 // handled specially--e.g., a .eh_frame section. The relocation
705 // routines need to check for each reloc where it should be
706 // applied. For this case, we need an input/output view for the
707 // entire contents of the section in the output file. We don't
708 // want to copy the contents of the input section to the output
709 // section; the output section contents were already written,
710 // and we waited for them in Relocate_task::is_runnable because
711 // relocs_must_follow_section_writes is set for the object.
713 // Regardless of which of the above cases is true, we have to
714 // check requires_postprocessing of the output section. If that
715 // is false, then we work with views of the output file
716 // directly. If it is true, then we work with a separate
717 // buffer, and the output section is responsible for writing the
718 // final data to the output file.
720 off_t output_section_offset
;
721 Address output_section_size
;
722 if (!os
->requires_postprocessing())
724 output_section_offset
= os
->offset();
725 output_section_size
= convert_types
<Address
, off_t
>(os
->data_size());
729 output_section_offset
= 0;
730 output_section_size
=
731 convert_types
<Address
, off_t
>(os
->postprocessing_buffer_size());
735 section_size_type view_size
;
736 bool must_decompress
= false;
737 if (output_offset
!= invalid_address
)
739 view_start
= output_section_offset
+ output_offset
;
740 view_size
= convert_to_section_size_type(shdr
.get_sh_size());
741 section_size_type uncompressed_size
;
742 if (this->section_is_compressed(i
, &uncompressed_size
))
744 view_size
= uncompressed_size
;
745 must_decompress
= true;
750 view_start
= output_section_offset
;
751 view_size
= convert_to_section_size_type(output_section_size
);
757 gold_assert(output_offset
== invalid_address
758 || output_offset
+ view_size
<= output_section_size
);
761 if (os
->requires_postprocessing())
763 unsigned char* buffer
= os
->postprocessing_buffer();
764 view
= buffer
+ view_start
;
765 if (output_offset
!= invalid_address
&& !must_decompress
)
767 off_t sh_offset
= shdr
.get_sh_offset();
768 if (!rm
.empty() && rm
.back().file_offset
> sh_offset
)
770 rm
.push_back(File_read::Read_multiple_entry(sh_offset
,
776 if (output_offset
== invalid_address
)
777 view
= of
->get_input_output_view(view_start
, view_size
);
780 view
= of
->get_output_view(view_start
, view_size
);
781 if (!must_decompress
)
783 off_t sh_offset
= shdr
.get_sh_offset();
784 if (!rm
.empty() && rm
.back().file_offset
> sh_offset
)
786 rm
.push_back(File_read::Read_multiple_entry(sh_offset
,
794 // Read and decompress the section.
795 section_size_type len
;
796 const unsigned char* p
= this->section_contents(i
, &len
, false);
797 if (!decompress_input_section(p
, len
, view
, view_size
))
798 this->error(_("could not decompress section %s"),
799 this->section_name(i
).c_str());
803 pvs
->address
= os
->address();
804 if (output_offset
!= invalid_address
)
805 pvs
->address
+= output_offset
;
806 pvs
->offset
= view_start
;
807 pvs
->view_size
= view_size
;
808 pvs
->is_input_output_view
= output_offset
== invalid_address
;
809 pvs
->is_postprocessing_view
= os
->requires_postprocessing();
812 // Actually read the data.
816 std::sort(rm
.begin(), rm
.end(), Read_multiple_compare());
817 this->read_multiple(rm
);
821 // Relocate section data. VIEWS points to the section data as views
822 // in the output file.
824 template<int size
, bool big_endian
>
826 Sized_relobj
<size
, big_endian
>::do_relocate_sections(
827 const Symbol_table
* symtab
,
828 const Layout
* layout
,
829 const unsigned char* pshdrs
,
832 unsigned int shnum
= this->shnum();
833 Sized_target
<size
, big_endian
>* target
=
834 parameters
->sized_target
<size
, big_endian
>();
836 const Output_sections
& out_sections(this->output_sections());
837 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
839 Relocate_info
<size
, big_endian
> relinfo
;
840 relinfo
.symtab
= symtab
;
841 relinfo
.layout
= layout
;
842 relinfo
.object
= this;
844 const unsigned char* p
= pshdrs
+ This::shdr_size
;
845 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
847 typename
This::Shdr
shdr(p
);
849 unsigned int sh_type
= shdr
.get_sh_type();
850 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
853 off_t sh_size
= shdr
.get_sh_size();
857 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
858 if (index
>= this->shnum())
860 this->error(_("relocation section %u has bad info %u"),
865 Output_section
* os
= out_sections
[index
];
868 // This relocation section is against a section which we
872 Address output_offset
= out_offsets
[index
];
874 gold_assert((*pviews
)[index
].view
!= NULL
);
875 if (parameters
->options().relocatable())
876 gold_assert((*pviews
)[i
].view
!= NULL
);
878 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx_
)
880 gold_error(_("relocation section %u uses unexpected "
882 i
, this->adjust_shndx(shdr
.get_sh_link()));
886 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
887 sh_size
, true, false);
889 unsigned int reloc_size
;
890 if (sh_type
== elfcpp::SHT_REL
)
891 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
893 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
895 if (reloc_size
!= shdr
.get_sh_entsize())
897 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
898 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
903 size_t reloc_count
= sh_size
/ reloc_size
;
904 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
906 gold_error(_("reloc section %u size %lu uneven"),
907 i
, static_cast<unsigned long>(sh_size
));
911 gold_assert(output_offset
!= invalid_address
912 || this->relocs_must_follow_section_writes());
914 relinfo
.reloc_shndx
= i
;
915 relinfo
.reloc_shdr
= p
;
916 relinfo
.data_shndx
= index
;
917 relinfo
.data_shdr
= pshdrs
+ index
* This::shdr_size
;
918 unsigned char* view
= (*pviews
)[index
].view
;
919 Address address
= (*pviews
)[index
].address
;
920 section_size_type view_size
= (*pviews
)[index
].view_size
;
922 Reloc_symbol_changes
* reloc_map
= NULL
;
923 if (this->uses_split_stack() && output_offset
!= invalid_address
)
925 typename
This::Shdr
data_shdr(pshdrs
+ index
* This::shdr_size
);
926 if ((data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
927 this->split_stack_adjust(symtab
, pshdrs
, sh_type
, index
,
928 prelocs
, reloc_count
, view
, view_size
,
932 if (!parameters
->options().relocatable())
934 target
->relocate_section(&relinfo
, sh_type
, prelocs
, reloc_count
, os
,
935 output_offset
== invalid_address
,
936 view
, address
, view_size
, reloc_map
);
937 if (parameters
->options().emit_relocs())
938 this->emit_relocs(&relinfo
, i
, sh_type
, prelocs
, reloc_count
,
939 os
, output_offset
, view
, address
, view_size
,
940 (*pviews
)[i
].view
, (*pviews
)[i
].view_size
);
944 Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
945 target
->relocate_for_relocatable(&relinfo
, sh_type
, prelocs
,
946 reloc_count
, os
, output_offset
, rr
,
947 view
, address
, view_size
,
949 (*pviews
)[i
].view_size
);
954 // Emit the relocs for --emit-relocs.
956 template<int size
, bool big_endian
>
958 Sized_relobj
<size
, big_endian
>::emit_relocs(
959 const Relocate_info
<size
, big_endian
>* relinfo
,
961 unsigned int sh_type
,
962 const unsigned char* prelocs
,
964 Output_section
* output_section
,
965 typename
elfcpp::Elf_types
<size
>::Elf_Addr offset_in_output_section
,
967 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
968 section_size_type view_size
,
969 unsigned char* reloc_view
,
970 section_size_type reloc_view_size
)
972 if (sh_type
== elfcpp::SHT_REL
)
973 this->emit_relocs_reltype
<elfcpp::SHT_REL
>(relinfo
, i
, prelocs
,
974 reloc_count
, output_section
,
975 offset_in_output_section
,
976 view
, address
, view_size
,
977 reloc_view
, reloc_view_size
);
980 gold_assert(sh_type
== elfcpp::SHT_RELA
);
981 this->emit_relocs_reltype
<elfcpp::SHT_RELA
>(relinfo
, i
, prelocs
,
982 reloc_count
, output_section
,
983 offset_in_output_section
,
984 view
, address
, view_size
,
985 reloc_view
, reloc_view_size
);
989 // Emit the relocs for --emit-relocs, templatized on the type of the
990 // relocation section.
992 template<int size
, bool big_endian
>
993 template<int sh_type
>
995 Sized_relobj
<size
, big_endian
>::emit_relocs_reltype(
996 const Relocate_info
<size
, big_endian
>* relinfo
,
998 const unsigned char* prelocs
,
1000 Output_section
* output_section
,
1001 typename
elfcpp::Elf_types
<size
>::Elf_Addr offset_in_output_section
,
1002 unsigned char* view
,
1003 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1004 section_size_type view_size
,
1005 unsigned char* reloc_view
,
1006 section_size_type reloc_view_size
)
1008 const Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
1009 relocate_for_relocatable
<size
, big_endian
, sh_type
>(
1014 offset_in_output_section
,
1023 // Create merge hash tables for the local symbols. These are used to
1024 // speed up relocations.
1026 template<int size
, bool big_endian
>
1028 Sized_relobj
<size
, big_endian
>::initialize_input_to_output_maps()
1030 const unsigned int loccount
= this->local_symbol_count_
;
1031 for (unsigned int i
= 1; i
< loccount
; ++i
)
1033 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1034 lv
.initialize_input_to_output_map(this);
1038 // Free merge hash tables for the local symbols.
1040 template<int size
, bool big_endian
>
1042 Sized_relobj
<size
, big_endian
>::free_input_to_output_maps()
1044 const unsigned int loccount
= this->local_symbol_count_
;
1045 for (unsigned int i
= 1; i
< loccount
; ++i
)
1047 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1048 lv
.free_input_to_output_map();
1052 // If an object was compiled with -fsplit-stack, this is called to
1053 // check whether any relocations refer to functions defined in objects
1054 // which were not compiled with -fsplit-stack. If they were, then we
1055 // need to apply some target-specific adjustments to request
1056 // additional stack space.
1058 template<int size
, bool big_endian
>
1060 Sized_relobj
<size
, big_endian
>::split_stack_adjust(
1061 const Symbol_table
* symtab
,
1062 const unsigned char* pshdrs
,
1063 unsigned int sh_type
,
1065 const unsigned char* prelocs
,
1067 unsigned char* view
,
1068 section_size_type view_size
,
1069 Reloc_symbol_changes
** reloc_map
)
1071 if (sh_type
== elfcpp::SHT_REL
)
1072 this->split_stack_adjust_reltype
<elfcpp::SHT_REL
>(symtab
, pshdrs
, shndx
,
1073 prelocs
, reloc_count
,
1078 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1079 this->split_stack_adjust_reltype
<elfcpp::SHT_RELA
>(symtab
, pshdrs
, shndx
,
1080 prelocs
, reloc_count
,
1086 // Adjust for -fsplit-stack, templatized on the type of the relocation
1089 template<int size
, bool big_endian
>
1090 template<int sh_type
>
1092 Sized_relobj
<size
, big_endian
>::split_stack_adjust_reltype(
1093 const Symbol_table
* symtab
,
1094 const unsigned char* pshdrs
,
1096 const unsigned char* prelocs
,
1098 unsigned char* view
,
1099 section_size_type view_size
,
1100 Reloc_symbol_changes
** reloc_map
)
1102 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
1103 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1105 size_t local_count
= this->local_symbol_count();
1107 std::vector
<section_offset_type
> non_split_refs
;
1109 const unsigned char* pr
= prelocs
;
1110 for (size_t i
= 0; i
< reloc_count
; ++i
, pr
+= reloc_size
)
1114 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
1115 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1116 if (r_sym
< local_count
)
1119 const Symbol
* gsym
= this->global_symbol(r_sym
);
1120 gold_assert(gsym
!= NULL
);
1121 if (gsym
->is_forwarder())
1122 gsym
= symtab
->resolve_forwards(gsym
);
1124 // See if this relocation refers to a function defined in an
1125 // object compiled without -fsplit-stack. Note that we don't
1126 // care about the type of relocation--this means that in some
1127 // cases we will ask for a large stack unnecessarily, but this
1128 // is not fatal. FIXME: Some targets have symbols which are
1129 // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC.
1130 if (!gsym
->is_undefined()
1131 && gsym
->source() == Symbol::FROM_OBJECT
1132 && !gsym
->object()->uses_split_stack())
1134 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1135 if (parameters
->target().is_call_to_non_split(gsym
, r_type
))
1137 section_offset_type offset
=
1138 convert_to_section_size_type(reloc
.get_r_offset());
1139 non_split_refs
.push_back(offset
);
1144 if (non_split_refs
.empty())
1147 // At this point, every entry in NON_SPLIT_REFS indicates a
1148 // relocation which refers to a function in an object compiled
1149 // without -fsplit-stack. We now have to convert that list into a
1150 // set of offsets to functions. First, we find all the functions.
1152 Function_offsets function_offsets
;
1153 this->find_functions(pshdrs
, shndx
, &function_offsets
);
1154 if (function_offsets
.empty())
1157 // Now get a list of the function with references to non split-stack
1160 Function_offsets calls_non_split
;
1161 for (std::vector
<section_offset_type
>::const_iterator p
1162 = non_split_refs
.begin();
1163 p
!= non_split_refs
.end();
1166 Function_offsets::const_iterator low
= function_offsets
.lower_bound(*p
);
1167 if (low
== function_offsets
.end())
1169 else if (low
->first
== *p
)
1171 else if (low
== function_offsets
.begin())
1176 calls_non_split
.insert(*low
);
1178 if (calls_non_split
.empty())
1181 // Now we have a set of functions to adjust. The adjustments are
1182 // target specific. Besides changing the output section view
1183 // however, it likes, the target may request a relocation change
1184 // from one global symbol name to another.
1186 for (Function_offsets::const_iterator p
= calls_non_split
.begin();
1187 p
!= calls_non_split
.end();
1192 parameters
->target().calls_non_split(this, shndx
, p
->first
, p
->second
,
1193 view
, view_size
, &from
, &to
);
1196 gold_assert(!to
.empty());
1197 Symbol
* tosym
= NULL
;
1199 // Find relocations in the relevant function which are for
1202 for (size_t i
= 0; i
< reloc_count
; ++i
, pr
+= reloc_size
)
1206 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
=
1208 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1209 if (r_sym
< local_count
)
1212 section_offset_type offset
=
1213 convert_to_section_size_type(reloc
.get_r_offset());
1214 if (offset
< p
->first
1217 + static_cast<section_offset_type
>(p
->second
))))
1220 const Symbol
* gsym
= this->global_symbol(r_sym
);
1221 if (from
== gsym
->name())
1225 tosym
= symtab
->lookup(to
.c_str());
1228 this->error(_("could not convert call "
1230 from
.c_str(), to
.c_str());
1235 if (*reloc_map
== NULL
)
1236 *reloc_map
= new Reloc_symbol_changes(reloc_count
);
1237 (*reloc_map
)->set(i
, tosym
);
1244 // Find all the function in this object defined in section SHNDX.
1245 // Store their offsets in the section in FUNCTION_OFFSETS.
1247 template<int size
, bool big_endian
>
1249 Sized_relobj
<size
, big_endian
>::find_functions(
1250 const unsigned char* pshdrs
,
1252 Sized_relobj
<size
, big_endian
>::Function_offsets
* function_offsets
)
1254 // We need to read the symbols to find the functions. If we wanted
1255 // to, we could cache reading the symbols across all sections in the
1257 const unsigned int symtab_shndx
= this->symtab_shndx_
;
1258 typename
This::Shdr
symtabshdr(pshdrs
+ symtab_shndx
* This::shdr_size
);
1259 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
1261 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_size
=
1262 symtabshdr
.get_sh_size();
1263 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
1264 sh_size
, true, true);
1266 const int sym_size
= This::sym_size
;
1267 const unsigned int symcount
= sh_size
/ sym_size
;
1268 for (unsigned int i
= 0; i
< symcount
; ++i
, psyms
+= sym_size
)
1270 typename
elfcpp::Sym
<size
, big_endian
> isym(psyms
);
1272 // FIXME: Some targets can have functions which do not have type
1273 // STT_FUNC, e.g., STT_ARM_TFUNC.
1274 if (isym
.get_st_type() != elfcpp::STT_FUNC
1275 || isym
.get_st_size() == 0)
1279 unsigned int sym_shndx
= this->adjust_sym_shndx(i
, isym
.get_st_shndx(),
1281 if (!is_ordinary
|| sym_shndx
!= shndx
)
1284 section_offset_type value
=
1285 convert_to_section_size_type(isym
.get_st_value());
1286 section_size_type fnsize
=
1287 convert_to_section_size_type(isym
.get_st_size());
1289 (*function_offsets
)[value
] = fnsize
;
1293 // Class Merged_symbol_value.
1297 Merged_symbol_value
<size
>::initialize_input_to_output_map(
1298 const Relobj
* object
,
1299 unsigned int input_shndx
)
1301 Object_merge_map
* map
= object
->merge_map();
1302 map
->initialize_input_to_output_map
<size
>(input_shndx
,
1303 this->output_start_address_
,
1304 &this->output_addresses_
);
1307 // Get the output value corresponding to an input offset if we
1308 // couldn't find it in the hash table.
1311 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1312 Merged_symbol_value
<size
>::value_from_output_section(
1313 const Relobj
* object
,
1314 unsigned int input_shndx
,
1315 typename
elfcpp::Elf_types
<size
>::Elf_Addr input_offset
) const
1317 section_offset_type output_offset
;
1318 bool found
= object
->merge_map()->get_output_offset(NULL
, input_shndx
,
1322 // If this assertion fails, it means that some relocation was
1323 // against a portion of an input merge section which we didn't map
1324 // to the output file and we didn't explicitly discard. We should
1325 // always map all portions of input merge sections.
1328 if (output_offset
== -1)
1331 return this->output_start_address_
+ output_offset
;
1334 // Track_relocs methods.
1336 // Initialize the class to track the relocs. This gets the object,
1337 // the reloc section index, and the type of the relocs. This returns
1338 // false if something goes wrong.
1340 template<int size
, bool big_endian
>
1342 Track_relocs
<size
, big_endian
>::initialize(
1344 unsigned int reloc_shndx
,
1345 unsigned int reloc_type
)
1347 // If RELOC_SHNDX is -1U, it means there is more than one reloc
1348 // section for the .eh_frame section. We can't handle that case.
1349 if (reloc_shndx
== -1U)
1352 // If RELOC_SHNDX is 0, there is no reloc section.
1353 if (reloc_shndx
== 0)
1356 // Get the contents of the reloc section.
1357 this->prelocs_
= object
->section_contents(reloc_shndx
, &this->len_
, false);
1359 if (reloc_type
== elfcpp::SHT_REL
)
1360 this->reloc_size_
= elfcpp::Elf_sizes
<size
>::rel_size
;
1361 else if (reloc_type
== elfcpp::SHT_RELA
)
1362 this->reloc_size_
= elfcpp::Elf_sizes
<size
>::rela_size
;
1366 if (this->len_
% this->reloc_size_
!= 0)
1368 object
->error(_("reloc section size %zu is not a multiple of "
1370 static_cast<size_t>(this->len_
),
1378 // Return the offset of the next reloc, or -1 if there isn't one.
1380 template<int size
, bool big_endian
>
1382 Track_relocs
<size
, big_endian
>::next_offset() const
1384 if (this->pos_
>= this->len_
)
1387 // Rel and Rela start out the same, so we can always use Rel to find
1388 // the r_offset value.
1389 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1390 return rel
.get_r_offset();
1393 // Return the index of the symbol referenced by the next reloc, or -1U
1394 // if there aren't any more relocs.
1396 template<int size
, bool big_endian
>
1398 Track_relocs
<size
, big_endian
>::next_symndx() const
1400 if (this->pos_
>= this->len_
)
1403 // Rel and Rela start out the same, so we can use Rel to find the
1405 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1406 return elfcpp::elf_r_sym
<size
>(rel
.get_r_info());
1409 // Advance to the next reloc whose r_offset is greater than or equal
1410 // to OFFSET. Return the number of relocs we skip.
1412 template<int size
, bool big_endian
>
1414 Track_relocs
<size
, big_endian
>::advance(off_t offset
)
1417 while (this->pos_
< this->len_
)
1419 // Rel and Rela start out the same, so we can always use Rel to
1420 // find the r_offset value.
1421 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1422 if (static_cast<off_t
>(rel
.get_r_offset()) >= offset
)
1425 this->pos_
+= this->reloc_size_
;
1430 // Instantiate the templates we need.
1432 #ifdef HAVE_TARGET_32_LITTLE
1435 Sized_relobj
<32, false>::do_read_relocs(Read_relocs_data
* rd
);
1438 #ifdef HAVE_TARGET_32_BIG
1441 Sized_relobj
<32, true>::do_read_relocs(Read_relocs_data
* rd
);
1444 #ifdef HAVE_TARGET_64_LITTLE
1447 Sized_relobj
<64, false>::do_read_relocs(Read_relocs_data
* rd
);
1450 #ifdef HAVE_TARGET_64_BIG
1453 Sized_relobj
<64, true>::do_read_relocs(Read_relocs_data
* rd
);
1456 #ifdef HAVE_TARGET_32_LITTLE
1459 Sized_relobj
<32, false>::do_gc_process_relocs(Symbol_table
* symtab
,
1461 Read_relocs_data
* rd
);
1464 #ifdef HAVE_TARGET_32_BIG
1467 Sized_relobj
<32, true>::do_gc_process_relocs(Symbol_table
* symtab
,
1469 Read_relocs_data
* rd
);
1472 #ifdef HAVE_TARGET_64_LITTLE
1475 Sized_relobj
<64, false>::do_gc_process_relocs(Symbol_table
* symtab
,
1477 Read_relocs_data
* rd
);
1480 #ifdef HAVE_TARGET_64_BIG
1483 Sized_relobj
<64, true>::do_gc_process_relocs(Symbol_table
* symtab
,
1485 Read_relocs_data
* rd
);
1488 #ifdef HAVE_TARGET_32_LITTLE
1491 Sized_relobj
<32, false>::do_scan_relocs(Symbol_table
* symtab
,
1493 Read_relocs_data
* rd
);
1496 #ifdef HAVE_TARGET_32_BIG
1499 Sized_relobj
<32, true>::do_scan_relocs(Symbol_table
* symtab
,
1501 Read_relocs_data
* rd
);
1504 #ifdef HAVE_TARGET_64_LITTLE
1507 Sized_relobj
<64, false>::do_scan_relocs(Symbol_table
* symtab
,
1509 Read_relocs_data
* rd
);
1512 #ifdef HAVE_TARGET_64_BIG
1515 Sized_relobj
<64, true>::do_scan_relocs(Symbol_table
* symtab
,
1517 Read_relocs_data
* rd
);
1520 #ifdef HAVE_TARGET_32_LITTLE
1523 Sized_relobj
<32, false>::do_relocate(const Symbol_table
* symtab
,
1524 const Layout
* layout
,
1528 #ifdef HAVE_TARGET_32_BIG
1531 Sized_relobj
<32, true>::do_relocate(const Symbol_table
* symtab
,
1532 const Layout
* layout
,
1536 #ifdef HAVE_TARGET_64_LITTLE
1539 Sized_relobj
<64, false>::do_relocate(const Symbol_table
* symtab
,
1540 const Layout
* layout
,
1544 #ifdef HAVE_TARGET_64_BIG
1547 Sized_relobj
<64, true>::do_relocate(const Symbol_table
* symtab
,
1548 const Layout
* layout
,
1552 #ifdef HAVE_TARGET_32_LITTLE
1555 Sized_relobj
<32, false>::do_relocate_sections(
1556 const Symbol_table
* symtab
,
1557 const Layout
* layout
,
1558 const unsigned char* pshdrs
,
1562 #ifdef HAVE_TARGET_32_BIG
1565 Sized_relobj
<32, true>::do_relocate_sections(
1566 const Symbol_table
* symtab
,
1567 const Layout
* layout
,
1568 const unsigned char* pshdrs
,
1572 #ifdef HAVE_TARGET_64_LITTLE
1575 Sized_relobj
<64, false>::do_relocate_sections(
1576 const Symbol_table
* symtab
,
1577 const Layout
* layout
,
1578 const unsigned char* pshdrs
,
1582 #ifdef HAVE_TARGET_64_BIG
1585 Sized_relobj
<64, true>::do_relocate_sections(
1586 const Symbol_table
* symtab
,
1587 const Layout
* layout
,
1588 const unsigned char* pshdrs
,
1592 #ifdef HAVE_TARGET_32_LITTLE
1595 Sized_relobj
<32, false>::initialize_input_to_output_maps();
1599 Sized_relobj
<32, false>::free_input_to_output_maps();
1602 #ifdef HAVE_TARGET_32_BIG
1605 Sized_relobj
<32, true>::initialize_input_to_output_maps();
1609 Sized_relobj
<32, true>::free_input_to_output_maps();
1612 #ifdef HAVE_TARGET_64_LITTLE
1615 Sized_relobj
<64, false>::initialize_input_to_output_maps();
1619 Sized_relobj
<64, false>::free_input_to_output_maps();
1622 #ifdef HAVE_TARGET_64_BIG
1625 Sized_relobj
<64, true>::initialize_input_to_output_maps();
1629 Sized_relobj
<64, true>::free_input_to_output_maps();
1632 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1634 class Merged_symbol_value
<32>;
1637 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1639 class Merged_symbol_value
<64>;
1642 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1644 class Symbol_value
<32>;
1647 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1649 class Symbol_value
<64>;
1652 #ifdef HAVE_TARGET_32_LITTLE
1654 class Track_relocs
<32, false>;
1657 #ifdef HAVE_TARGET_32_BIG
1659 class Track_relocs
<32, true>;
1662 #ifdef HAVE_TARGET_64_LITTLE
1664 class Track_relocs
<64, false>;
1667 #ifdef HAVE_TARGET_64_BIG
1669 class Track_relocs
<64, true>;
1672 } // End namespace gold.