1 // reloc.cc -- relocate input files for gold.
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
27 #include "workqueue.h"
32 #include "target-reloc.h"
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->options_,
84 workqueue->queue_next(new Scan_relocs(this->options_, this->symtab_,
85 this->layout_, this->object_, rd,
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 // These tasks process the relocations read by Read_relocs and
102 // determine which sections are referenced and which are garbage.
103 // This task is done only when --gc-sections is used.
106 Gc_process_relocs::is_runnable()
108 if (this->object_->is_locked())
109 return this->object_->token();
114 Gc_process_relocs::locks(Task_locker* tl)
116 tl->add(this, this->object_->token());
117 tl->add(this, this->blocker_);
121 Gc_process_relocs::run(Workqueue*)
123 this->object_->gc_process_relocs(this->options_, this->symtab_, this->layout_,
125 this->object_->release();
128 // Return a debugging name for the task.
131 Gc_process_relocs::get_name() const
133 return "Gc_process_relocs " + this->object_->name();
136 // Scan_relocs methods.
138 // These tasks scan the relocations read by Read_relocs and mark up
139 // the symbol table to indicate which relocations are required. We
140 // use a lock on the symbol table to keep them from interfering with
144 Scan_relocs::is_runnable()
146 if (!this->symtab_lock_->is_writable())
147 return this->symtab_lock_;
148 if (this->object_->is_locked())
149 return this->object_->token();
153 // Return the locks we hold: one on the file, one on the symbol table
157 Scan_relocs::locks(Task_locker* tl)
159 tl->add(this, this->object_->token());
160 tl->add(this, this->symtab_lock_);
161 tl->add(this, this->blocker_);
167 Scan_relocs::run(Workqueue*)
169 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
171 this->object_->release();
176 // Return a debugging name for the task.
179 Scan_relocs::get_name() const
181 return "Scan_relocs " + this->object_->name();
184 // Relocate_task methods.
186 // We may have to wait for the output sections to be written.
189 Relocate_task::is_runnable()
191 if (this->object_->relocs_must_follow_section_writes()
192 && this->output_sections_blocker_->is_blocked())
193 return this->output_sections_blocker_;
195 if (this->object_->is_locked())
196 return this->object_->token();
201 // We want to lock the file while we run. We want to unblock
202 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
203 // INPUT_SECTIONS_BLOCKER may be NULL.
206 Relocate_task::locks(Task_locker* tl)
208 if (this->input_sections_blocker_ != NULL)
209 tl->add(this, this->input_sections_blocker_);
210 tl->add(this, this->final_blocker_);
211 tl->add(this, this->object_->token());
217 Relocate_task::run(Workqueue*)
219 this->object_->relocate(this->options_, this->symtab_, this->layout_,
222 // This is normally the last thing we will do with an object, so
223 // uncache all views.
224 this->object_->clear_view_cache_marks();
226 this->object_->release();
229 // Return a debugging name for the task.
232 Relocate_task::get_name() const
234 return "Relocate_task " + this->object_->name();
237 // Read the relocs and local symbols from the object file and store
238 // the information in RD.
240 template<int size, bool big_endian>
242 Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
246 unsigned int shnum = this->shnum();
250 rd->relocs.reserve(shnum / 2);
252 const Output_sections& out_sections(this->output_sections());
253 const std::vector<Address>& out_offsets(this->section_offsets_);
255 const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
256 shnum * This::shdr_size,
258 // Skip the first, dummy, section.
259 const unsigned char *ps = pshdrs + This::shdr_size;
260 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
262 typename This::Shdr shdr(ps);
264 unsigned int sh_type = shdr.get_sh_type();
265 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
268 unsigned int shndx = this->adjust_shndx(shdr.get_sh_info());
271 this->error(_("relocation section %u has bad info %u"),
276 Output_section* os = out_sections[shndx];
280 // We are scanning relocations in order to fill out the GOT and
281 // PLT sections. Relocations for sections which are not
282 // allocated (typically debugging sections) should not add new
283 // GOT and PLT entries. So we skip them unless this is a
284 // relocatable link or we need to emit relocations. FIXME: What
285 // should we do if a linker script maps a section with SHF_ALLOC
286 // clear to a section with SHF_ALLOC set?
287 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
288 bool is_section_allocated = ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC)
290 if (!is_section_allocated
291 && !parameters->options().relocatable()
292 && !parameters->options().emit_relocs())
295 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
297 this->error(_("relocation section %u uses unexpected "
299 i, this->adjust_shndx(shdr.get_sh_link()));
303 off_t sh_size = shdr.get_sh_size();
305 unsigned int reloc_size;
306 if (sh_type == elfcpp::SHT_REL)
307 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
309 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
310 if (reloc_size != shdr.get_sh_entsize())
312 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
313 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
318 size_t reloc_count = sh_size / reloc_size;
319 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
321 this->error(_("reloc section %u size %lu uneven"),
322 i, static_cast<unsigned long>(sh_size));
326 rd->relocs.push_back(Section_relocs());
327 Section_relocs& sr(rd->relocs.back());
329 sr.data_shndx = shndx;
330 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
332 sr.sh_type = sh_type;
333 sr.reloc_count = reloc_count;
334 sr.output_section = os;
335 sr.needs_special_offset_handling = out_offsets[shndx] == invalid_address;
336 sr.is_data_section_allocated = is_section_allocated;
339 // Read the local symbols.
340 gold_assert(this->symtab_shndx_ != -1U);
341 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
342 rd->local_symbols = NULL;
345 typename This::Shdr symtabshdr(pshdrs
346 + this->symtab_shndx_ * This::shdr_size);
347 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
348 const int sym_size = This::sym_size;
349 const unsigned int loccount = this->local_symbol_count_;
350 gold_assert(loccount == symtabshdr.get_sh_info());
351 off_t locsize = loccount * sym_size;
352 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
353 locsize, true, true);
357 // Process the relocs to generate mappings from source sections to referenced
358 // sections. This is used during garbage colletion to determine garbage
361 template<int size, bool big_endian>
363 Sized_relobj<size, big_endian>::do_gc_process_relocs(const General_options& options,
364 Symbol_table* symtab,
366 Read_relocs_data* rd)
368 Sized_target<size, big_endian>* target =
369 parameters->sized_target<size, big_endian>();
371 const unsigned char* local_symbols;
372 if (rd->local_symbols == NULL)
373 local_symbols = NULL;
375 local_symbols = rd->local_symbols->data();
377 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
378 p != rd->relocs.end();
381 if (!parameters->options().relocatable())
383 // As noted above, when not generating an object file, we
384 // only scan allocated sections. We may see a non-allocated
385 // section here if we are emitting relocs.
386 if (p->is_data_section_allocated)
387 target->gc_process_relocs(options, symtab, layout, this,
388 p->data_shndx, p->sh_type,
389 p->contents->data(), p->reloc_count,
391 p->needs_special_offset_handling,
392 this->local_symbol_count_,
399 // Scan the relocs and adjust the symbol table. This looks for
400 // relocations which require GOT/PLT/COPY relocations.
402 template<int size, bool big_endian>
404 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
405 Symbol_table* symtab,
407 Read_relocs_data* rd)
409 Sized_target<size, big_endian>* target =
410 parameters->sized_target<size, big_endian>();
412 const unsigned char* local_symbols;
413 if (rd->local_symbols == NULL)
414 local_symbols = NULL;
416 local_symbols = rd->local_symbols->data();
418 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
419 p != rd->relocs.end();
422 // When garbage collection is on, unreferenced sections are not included
423 // in the link that would have been included normally. This is known only
424 // after Read_relocs hence this check has to be done again.
425 if (parameters->options().gc_sections()
426 || parameters->options().icf_enabled())
428 if (p->output_section == NULL)
431 if (!parameters->options().relocatable())
433 // As noted above, when not generating an object file, we
434 // only scan allocated sections. We may see a non-allocated
435 // section here if we are emitting relocs.
436 if (p->is_data_section_allocated)
437 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
438 p->sh_type, p->contents->data(),
439 p->reloc_count, p->output_section,
440 p->needs_special_offset_handling,
441 this->local_symbol_count_,
443 if (parameters->options().emit_relocs())
444 this->emit_relocs_scan(options, symtab, layout, local_symbols, p);
448 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
449 gold_assert(rr != NULL);
450 rr->set_reloc_count(p->reloc_count);
451 target->scan_relocatable_relocs(options, symtab, layout, this,
452 p->data_shndx, p->sh_type,
456 p->needs_special_offset_handling,
457 this->local_symbol_count_,
466 if (rd->local_symbols != NULL)
468 delete rd->local_symbols;
469 rd->local_symbols = NULL;
473 // This is a strategy class we use when scanning for --emit-relocs.
475 template<int sh_type>
476 class Emit_relocs_strategy
479 // A local non-section symbol.
480 inline Relocatable_relocs::Reloc_strategy
481 local_non_section_strategy(unsigned int, Relobj*, unsigned int)
482 { return Relocatable_relocs::RELOC_COPY; }
484 // A local section symbol.
485 inline Relocatable_relocs::Reloc_strategy
486 local_section_strategy(unsigned int, Relobj*)
488 if (sh_type == elfcpp::SHT_RELA)
489 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
492 // The addend is stored in the section contents. Since this
493 // is not a relocatable link, we are going to apply the
494 // relocation contents to the section as usual. This means
495 // that we have no way to record the original addend. If the
496 // original addend is not zero, there is basically no way for
497 // the user to handle this correctly. Caveat emptor.
498 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
503 inline Relocatable_relocs::Reloc_strategy
504 global_strategy(unsigned int, Relobj*, unsigned int)
505 { return Relocatable_relocs::RELOC_COPY; }
508 // Scan the input relocations for --emit-relocs.
510 template<int size, bool big_endian>
512 Sized_relobj<size, big_endian>::emit_relocs_scan(
513 const General_options& options,
514 Symbol_table* symtab,
516 const unsigned char* plocal_syms,
517 const Read_relocs_data::Relocs_list::iterator& p)
519 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
520 gold_assert(rr != NULL);
521 rr->set_reloc_count(p->reloc_count);
523 if (p->sh_type == elfcpp::SHT_REL)
524 this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(options, symtab, layout,
528 gold_assert(p->sh_type == elfcpp::SHT_RELA);
529 this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(options, symtab,
530 layout, plocal_syms, p,
535 // Scan the input relocation for --emit-relocs, templatized on the
536 // type of the relocation section.
538 template<int size, bool big_endian>
539 template<int sh_type>
541 Sized_relobj<size, big_endian>::emit_relocs_scan_reltype(
542 const General_options& options,
543 Symbol_table* symtab,
545 const unsigned char* plocal_syms,
546 const Read_relocs_data::Relocs_list::iterator& p,
547 Relocatable_relocs* rr)
549 scan_relocatable_relocs<size, big_endian, sh_type,
550 Emit_relocs_strategy<sh_type> >(
559 p->needs_special_offset_handling,
560 this->local_symbol_count_,
565 // Relocate the input sections and write out the local symbols.
567 template<int size, bool big_endian>
569 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
570 const Symbol_table* symtab,
571 const Layout* layout,
574 unsigned int shnum = this->shnum();
576 // Read the section headers.
577 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
578 shnum * This::shdr_size,
584 // Make two passes over the sections. The first one copies the
585 // section data to the output file. The second one applies
588 this->write_sections(pshdrs, of, &views);
590 // To speed up relocations, we set up hash tables for fast lookup of
591 // input offsets to output addresses.
592 this->initialize_input_to_output_maps();
594 // Apply relocations.
596 this->relocate_sections(options, symtab, layout, pshdrs, &views);
598 // After we've done the relocations, we release the hash tables,
599 // since we no longer need them.
600 this->free_input_to_output_maps();
602 // Write out the accumulated views.
603 for (unsigned int i = 1; i < shnum; ++i)
605 if (views[i].view != NULL)
607 if (!views[i].is_postprocessing_view)
609 if (views[i].is_input_output_view)
610 of->write_input_output_view(views[i].offset,
614 of->write_output_view(views[i].offset, views[i].view_size,
620 // Write out the local symbols.
621 this->write_local_symbols(of, layout->sympool(), layout->dynpool(),
622 layout->symtab_xindex(), layout->dynsym_xindex());
624 // We should no longer need the local symbol values.
625 this->clear_local_symbols();
628 // Sort a Read_multiple vector by file offset.
629 struct Read_multiple_compare
632 operator()(const File_read::Read_multiple_entry& rme1,
633 const File_read::Read_multiple_entry& rme2) const
634 { return rme1.file_offset < rme2.file_offset; }
637 // Write section data to the output file. PSHDRS points to the
638 // section headers. Record the views in *PVIEWS for use when
641 template<int size, bool big_endian>
643 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
647 unsigned int shnum = this->shnum();
648 const Output_sections& out_sections(this->output_sections());
649 const std::vector<Address>& out_offsets(this->section_offsets_);
651 File_read::Read_multiple rm;
652 bool is_sorted = true;
654 const unsigned char* p = pshdrs + This::shdr_size;
655 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
657 View_size* pvs = &(*pviews)[i];
661 const Output_section* os = out_sections[i];
664 Address output_offset = out_offsets[i];
666 typename This::Shdr shdr(p);
668 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
671 if ((parameters->options().relocatable()
672 || parameters->options().emit_relocs())
673 && (shdr.get_sh_type() == elfcpp::SHT_REL
674 || shdr.get_sh_type() == elfcpp::SHT_RELA)
675 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
677 // This is a reloc section in a relocatable link or when
678 // emitting relocs. We don't need to read the input file.
679 // The size and file offset are stored in the
680 // Relocatable_relocs structure.
681 Relocatable_relocs* rr = this->relocatable_relocs(i);
682 gold_assert(rr != NULL);
683 Output_data* posd = rr->output_data();
684 gold_assert(posd != NULL);
686 pvs->offset = posd->offset();
687 pvs->view_size = posd->data_size();
688 pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
689 pvs->address = posd->address();
690 pvs->is_input_output_view = false;
691 pvs->is_postprocessing_view = false;
696 // In the normal case, this input section is simply mapped to
697 // the output section at offset OUTPUT_OFFSET.
699 // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
700 // handled specially--e.g., a .eh_frame section. The relocation
701 // routines need to check for each reloc where it should be
702 // applied. For this case, we need an input/output view for the
703 // entire contents of the section in the output file. We don't
704 // want to copy the contents of the input section to the output
705 // section; the output section contents were already written,
706 // and we waited for them in Relocate_task::is_runnable because
707 // relocs_must_follow_section_writes is set for the object.
709 // Regardless of which of the above cases is true, we have to
710 // check requires_postprocessing of the output section. If that
711 // is false, then we work with views of the output file
712 // directly. If it is true, then we work with a separate
713 // buffer, and the output section is responsible for writing the
714 // final data to the output file.
716 off_t output_section_offset;
717 Address output_section_size;
718 if (!os->requires_postprocessing())
720 output_section_offset = os->offset();
721 output_section_size = convert_types<Address, off_t>(os->data_size());
725 output_section_offset = 0;
726 output_section_size =
727 convert_types<Address, off_t>(os->postprocessing_buffer_size());
731 section_size_type view_size;
732 if (output_offset != invalid_address)
734 view_start = output_section_offset + output_offset;
735 view_size = convert_to_section_size_type(shdr.get_sh_size());
739 view_start = output_section_offset;
740 view_size = convert_to_section_size_type(output_section_size);
746 gold_assert(output_offset == invalid_address
747 || output_offset + view_size <= output_section_size);
750 if (os->requires_postprocessing())
752 unsigned char* buffer = os->postprocessing_buffer();
753 view = buffer + view_start;
754 if (output_offset != invalid_address)
756 off_t sh_offset = shdr.get_sh_offset();
757 if (!rm.empty() && rm.back().file_offset > sh_offset)
759 rm.push_back(File_read::Read_multiple_entry(sh_offset,
765 if (output_offset == invalid_address)
766 view = of->get_input_output_view(view_start, view_size);
769 view = of->get_output_view(view_start, view_size);
770 off_t sh_offset = shdr.get_sh_offset();
771 if (!rm.empty() && rm.back().file_offset > sh_offset)
773 rm.push_back(File_read::Read_multiple_entry(sh_offset,
779 pvs->address = os->address();
780 if (output_offset != invalid_address)
781 pvs->address += output_offset;
782 pvs->offset = view_start;
783 pvs->view_size = view_size;
784 pvs->is_input_output_view = output_offset == invalid_address;
785 pvs->is_postprocessing_view = os->requires_postprocessing();
788 // Actually read the data.
792 std::sort(rm.begin(), rm.end(), Read_multiple_compare());
793 this->read_multiple(rm);
797 // Relocate section data. VIEWS points to the section data as views
798 // in the output file.
800 template<int size, bool big_endian>
802 Sized_relobj<size, big_endian>::relocate_sections(
803 const General_options& options,
804 const Symbol_table* symtab,
805 const Layout* layout,
806 const unsigned char* pshdrs,
809 unsigned int shnum = this->shnum();
810 Sized_target<size, big_endian>* target =
811 parameters->sized_target<size, big_endian>();
813 const Output_sections& out_sections(this->output_sections());
814 const std::vector<Address>& out_offsets(this->section_offsets_);
816 Relocate_info<size, big_endian> relinfo;
817 relinfo.options = &options;
818 relinfo.symtab = symtab;
819 relinfo.layout = layout;
820 relinfo.object = this;
822 const unsigned char* p = pshdrs + This::shdr_size;
823 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
825 typename This::Shdr shdr(p);
827 unsigned int sh_type = shdr.get_sh_type();
828 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
831 off_t sh_size = shdr.get_sh_size();
835 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
836 if (index >= this->shnum())
838 this->error(_("relocation section %u has bad info %u"),
843 Output_section* os = out_sections[index];
846 // This relocation section is against a section which we
850 Address output_offset = out_offsets[index];
852 gold_assert((*pviews)[index].view != NULL);
853 if (parameters->options().relocatable())
854 gold_assert((*pviews)[i].view != NULL);
856 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
858 gold_error(_("relocation section %u uses unexpected "
860 i, this->adjust_shndx(shdr.get_sh_link()));
864 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
865 sh_size, true, false);
867 unsigned int reloc_size;
868 if (sh_type == elfcpp::SHT_REL)
869 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
871 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
873 if (reloc_size != shdr.get_sh_entsize())
875 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
876 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
881 size_t reloc_count = sh_size / reloc_size;
882 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
884 gold_error(_("reloc section %u size %lu uneven"),
885 i, static_cast<unsigned long>(sh_size));
889 gold_assert(output_offset != invalid_address
890 || this->relocs_must_follow_section_writes());
892 relinfo.reloc_shndx = i;
893 relinfo.data_shndx = index;
894 unsigned char* view = (*pviews)[index].view;
895 Address address = (*pviews)[index].address;
896 section_size_type view_size = (*pviews)[index].view_size;
898 Reloc_symbol_changes* reloc_map = NULL;
899 if (this->uses_split_stack() && output_offset != invalid_address)
901 typename This::Shdr data_shdr(pshdrs + index * This::shdr_size);
902 if ((data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
903 this->split_stack_adjust(symtab, pshdrs, sh_type, index,
904 prelocs, reloc_count, view, view_size,
908 if (!parameters->options().relocatable())
910 target->relocate_section(&relinfo, sh_type, prelocs, reloc_count, os,
911 output_offset == invalid_address,
912 view, address, view_size, reloc_map);
913 if (parameters->options().emit_relocs())
914 this->emit_relocs(&relinfo, i, sh_type, prelocs, reloc_count,
915 os, output_offset, view, address, view_size,
916 (*pviews)[i].view, (*pviews)[i].view_size);
920 Relocatable_relocs* rr = this->relocatable_relocs(i);
921 target->relocate_for_relocatable(&relinfo, sh_type, prelocs,
922 reloc_count, os, output_offset, rr,
923 view, address, view_size,
925 (*pviews)[i].view_size);
930 // Emit the relocs for --emit-relocs.
932 template<int size, bool big_endian>
934 Sized_relobj<size, big_endian>::emit_relocs(
935 const Relocate_info<size, big_endian>* relinfo,
937 unsigned int sh_type,
938 const unsigned char* prelocs,
940 Output_section* output_section,
941 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
943 typename elfcpp::Elf_types<size>::Elf_Addr address,
944 section_size_type view_size,
945 unsigned char* reloc_view,
946 section_size_type reloc_view_size)
948 if (sh_type == elfcpp::SHT_REL)
949 this->emit_relocs_reltype<elfcpp::SHT_REL>(relinfo, i, prelocs,
950 reloc_count, output_section,
951 offset_in_output_section,
952 view, address, view_size,
953 reloc_view, reloc_view_size);
956 gold_assert(sh_type == elfcpp::SHT_RELA);
957 this->emit_relocs_reltype<elfcpp::SHT_RELA>(relinfo, i, prelocs,
958 reloc_count, output_section,
959 offset_in_output_section,
960 view, address, view_size,
961 reloc_view, reloc_view_size);
965 // Emit the relocs for --emit-relocs, templatized on the type of the
966 // relocation section.
968 template<int size, bool big_endian>
969 template<int sh_type>
971 Sized_relobj<size, big_endian>::emit_relocs_reltype(
972 const Relocate_info<size, big_endian>* relinfo,
974 const unsigned char* prelocs,
976 Output_section* output_section,
977 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
979 typename elfcpp::Elf_types<size>::Elf_Addr address,
980 section_size_type view_size,
981 unsigned char* reloc_view,
982 section_size_type reloc_view_size)
984 const Relocatable_relocs* rr = this->relocatable_relocs(i);
985 relocate_for_relocatable<size, big_endian, sh_type>(
990 offset_in_output_section,
999 // Create merge hash tables for the local symbols. These are used to
1000 // speed up relocations.
1002 template<int size, bool big_endian>
1004 Sized_relobj<size, big_endian>::initialize_input_to_output_maps()
1006 const unsigned int loccount = this->local_symbol_count_;
1007 for (unsigned int i = 1; i < loccount; ++i)
1009 Symbol_value<size>& lv(this->local_values_[i]);
1010 lv.initialize_input_to_output_map(this);
1014 // Free merge hash tables for the local symbols.
1016 template<int size, bool big_endian>
1018 Sized_relobj<size, big_endian>::free_input_to_output_maps()
1020 const unsigned int loccount = this->local_symbol_count_;
1021 for (unsigned int i = 1; i < loccount; ++i)
1023 Symbol_value<size>& lv(this->local_values_[i]);
1024 lv.free_input_to_output_map();
1028 // If an object was compiled with -fsplit-stack, this is called to
1029 // check whether any relocations refer to functions defined in objects
1030 // which were not compiled with -fsplit-stack. If they were, then we
1031 // need to apply some target-specific adjustments to request
1032 // additional stack space.
1034 template<int size, bool big_endian>
1036 Sized_relobj<size, big_endian>::split_stack_adjust(
1037 const Symbol_table* symtab,
1038 const unsigned char* pshdrs,
1039 unsigned int sh_type,
1041 const unsigned char* prelocs,
1043 unsigned char* view,
1044 section_size_type view_size,
1045 Reloc_symbol_changes** reloc_map)
1047 if (sh_type == elfcpp::SHT_REL)
1048 this->split_stack_adjust_reltype<elfcpp::SHT_REL>(symtab, pshdrs, shndx,
1049 prelocs, reloc_count,
1054 gold_assert(sh_type == elfcpp::SHT_RELA);
1055 this->split_stack_adjust_reltype<elfcpp::SHT_RELA>(symtab, pshdrs, shndx,
1056 prelocs, reloc_count,
1062 // Adjust for -fsplit-stack, templatized on the type of the relocation
1065 template<int size, bool big_endian>
1066 template<int sh_type>
1068 Sized_relobj<size, big_endian>::split_stack_adjust_reltype(
1069 const Symbol_table* symtab,
1070 const unsigned char* pshdrs,
1072 const unsigned char* prelocs,
1074 unsigned char* view,
1075 section_size_type view_size,
1076 Reloc_symbol_changes** reloc_map)
1078 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
1079 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
1081 size_t local_count = this->local_symbol_count();
1083 std::vector<section_offset_type> non_split_refs;
1085 const unsigned char* pr = prelocs;
1086 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
1090 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
1091 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1092 if (r_sym < local_count)
1095 const Symbol* gsym = this->global_symbol(r_sym);
1096 gold_assert(gsym != NULL);
1097 if (gsym->is_forwarder())
1098 gsym = symtab->resolve_forwards(gsym);
1100 // See if this relocation refers to a function defined in an
1101 // object compiled without -fsplit-stack. Note that we don't
1102 // care about the type of relocation--this means that in some
1103 // cases we will ask for a large stack unnecessarily, but this
1104 // is not fatal. FIXME: Some targets have symbols which are
1105 // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC.
1106 if (gsym->type() == elfcpp::STT_FUNC
1107 && !gsym->is_undefined()
1108 && gsym->source() == Symbol::FROM_OBJECT
1109 && !gsym->object()->uses_split_stack())
1111 section_offset_type offset =
1112 convert_to_section_size_type(reloc.get_r_offset());
1113 non_split_refs.push_back(offset);
1117 if (non_split_refs.empty())
1120 // At this point, every entry in NON_SPLIT_REFS indicates a
1121 // relocation which refers to a function in an object compiled
1122 // without -fsplit-stack. We now have to convert that list into a
1123 // set of offsets to functions. First, we find all the functions.
1125 Function_offsets function_offsets;
1126 this->find_functions(pshdrs, shndx, &function_offsets);
1127 if (function_offsets.empty())
1130 // Now get a list of the function with references to non split-stack
1133 Function_offsets calls_non_split;
1134 for (std::vector<section_offset_type>::const_iterator p
1135 = non_split_refs.begin();
1136 p != non_split_refs.end();
1139 Function_offsets::const_iterator low = function_offsets.lower_bound(*p);
1140 if (low == function_offsets.end())
1142 else if (low->first == *p)
1144 else if (low == function_offsets.begin())
1149 calls_non_split.insert(*low);
1151 if (calls_non_split.empty())
1154 // Now we have a set of functions to adjust. The adjustments are
1155 // target specific. Besides changing the output section view
1156 // however, it likes, the target may request a relocation change
1157 // from one global symbol name to another.
1159 for (Function_offsets::const_iterator p = calls_non_split.begin();
1160 p != calls_non_split.end();
1165 parameters->target().calls_non_split(this, shndx, p->first, p->second,
1166 view, view_size, &from, &to);
1169 gold_assert(!to.empty());
1170 Symbol* tosym = NULL;
1172 // Find relocations in the relevant function which are for
1175 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
1179 typename elfcpp::Elf_types<size>::Elf_WXword r_info =
1181 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1182 if (r_sym < local_count)
1185 section_offset_type offset =
1186 convert_to_section_size_type(reloc.get_r_offset());
1187 if (offset < p->first
1190 + static_cast<section_offset_type>(p->second))))
1193 const Symbol* gsym = this->global_symbol(r_sym);
1194 if (from == gsym->name())
1198 tosym = symtab->lookup(to.c_str());
1201 this->error(_("could not convert call "
1203 from.c_str(), to.c_str());
1208 if (*reloc_map == NULL)
1209 *reloc_map = new Reloc_symbol_changes(reloc_count);
1210 (*reloc_map)->set(i, tosym);
1217 // Find all the function in this object defined in section SHNDX.
1218 // Store their offsets in the section in FUNCTION_OFFSETS.
1220 template<int size, bool big_endian>
1222 Sized_relobj<size, big_endian>::find_functions(
1223 const unsigned char* pshdrs,
1225 Sized_relobj<size, big_endian>::Function_offsets* function_offsets)
1227 // We need to read the symbols to find the functions. If we wanted
1228 // to, we could cache reading the symbols across all sections in the
1230 const unsigned int symtab_shndx = this->symtab_shndx_;
1231 typename This::Shdr symtabshdr(pshdrs + symtab_shndx * This::shdr_size);
1232 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1234 typename elfcpp::Elf_types<size>::Elf_WXword sh_size =
1235 symtabshdr.get_sh_size();
1236 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1237 sh_size, true, true);
1239 const int sym_size = This::sym_size;
1240 const unsigned int symcount = sh_size / sym_size;
1241 for (unsigned int i = 0; i < symcount; ++i, psyms += sym_size)
1243 typename elfcpp::Sym<size, big_endian> isym(psyms);
1245 // FIXME: Some targets can have functions which do not have type
1246 // STT_FUNC, e.g., STT_ARM_TFUNC.
1247 if (isym.get_st_type() != elfcpp::STT_FUNC
1248 || isym.get_st_size() == 0)
1252 unsigned int sym_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1254 if (!is_ordinary || sym_shndx != shndx)
1257 section_offset_type value =
1258 convert_to_section_size_type(isym.get_st_value());
1259 section_size_type fnsize =
1260 convert_to_section_size_type(isym.get_st_size());
1262 (*function_offsets)[value] = fnsize;
1266 // Class Merged_symbol_value.
1270 Merged_symbol_value<size>::initialize_input_to_output_map(
1271 const Relobj* object,
1272 unsigned int input_shndx)
1274 Object_merge_map* map = object->merge_map();
1275 map->initialize_input_to_output_map<size>(input_shndx,
1276 this->output_start_address_,
1277 &this->output_addresses_);
1280 // Get the output value corresponding to an input offset if we
1281 // couldn't find it in the hash table.
1284 typename elfcpp::Elf_types<size>::Elf_Addr
1285 Merged_symbol_value<size>::value_from_output_section(
1286 const Relobj* object,
1287 unsigned int input_shndx,
1288 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
1290 section_offset_type output_offset;
1291 bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
1295 // If this assertion fails, it means that some relocation was
1296 // against a portion of an input merge section which we didn't map
1297 // to the output file and we didn't explicitly discard. We should
1298 // always map all portions of input merge sections.
1301 if (output_offset == -1)
1304 return this->output_start_address_ + output_offset;
1307 // Track_relocs methods.
1309 // Initialize the class to track the relocs. This gets the object,
1310 // the reloc section index, and the type of the relocs. This returns
1311 // false if something goes wrong.
1313 template<int size, bool big_endian>
1315 Track_relocs<size, big_endian>::initialize(
1317 unsigned int reloc_shndx,
1318 unsigned int reloc_type)
1320 // If RELOC_SHNDX is -1U, it means there is more than one reloc
1321 // section for the .eh_frame section. We can't handle that case.
1322 if (reloc_shndx == -1U)
1325 // If RELOC_SHNDX is 0, there is no reloc section.
1326 if (reloc_shndx == 0)
1329 // Get the contents of the reloc section.
1330 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
1332 if (reloc_type == elfcpp::SHT_REL)
1333 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
1334 else if (reloc_type == elfcpp::SHT_RELA)
1335 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
1339 if (this->len_ % this->reloc_size_ != 0)
1341 object->error(_("reloc section size %zu is not a multiple of "
1343 static_cast<size_t>(this->len_),
1351 // Return the offset of the next reloc, or -1 if there isn't one.
1353 template<int size, bool big_endian>
1355 Track_relocs<size, big_endian>::next_offset() const
1357 if (this->pos_ >= this->len_)
1360 // Rel and Rela start out the same, so we can always use Rel to find
1361 // the r_offset value.
1362 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1363 return rel.get_r_offset();
1366 // Return the index of the symbol referenced by the next reloc, or -1U
1367 // if there aren't any more relocs.
1369 template<int size, bool big_endian>
1371 Track_relocs<size, big_endian>::next_symndx() const
1373 if (this->pos_ >= this->len_)
1376 // Rel and Rela start out the same, so we can use Rel to find the
1378 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1379 return elfcpp::elf_r_sym<size>(rel.get_r_info());
1382 // Advance to the next reloc whose r_offset is greater than or equal
1383 // to OFFSET. Return the number of relocs we skip.
1385 template<int size, bool big_endian>
1387 Track_relocs<size, big_endian>::advance(off_t offset)
1390 while (this->pos_ < this->len_)
1392 // Rel and Rela start out the same, so we can always use Rel to
1393 // find the r_offset value.
1394 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1395 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
1398 this->pos_ += this->reloc_size_;
1403 // Instantiate the templates we need.
1405 #ifdef HAVE_TARGET_32_LITTLE
1408 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
1411 #ifdef HAVE_TARGET_32_BIG
1414 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
1417 #ifdef HAVE_TARGET_64_LITTLE
1420 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
1423 #ifdef HAVE_TARGET_64_BIG
1426 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
1429 #ifdef HAVE_TARGET_32_LITTLE
1432 Sized_relobj<32, false>::do_gc_process_relocs(const General_options& options,
1433 Symbol_table* symtab,
1435 Read_relocs_data* rd);
1438 #ifdef HAVE_TARGET_32_BIG
1441 Sized_relobj<32, true>::do_gc_process_relocs(const General_options& options,
1442 Symbol_table* symtab,
1444 Read_relocs_data* rd);
1447 #ifdef HAVE_TARGET_64_LITTLE
1450 Sized_relobj<64, false>::do_gc_process_relocs(const General_options& options,
1451 Symbol_table* symtab,
1453 Read_relocs_data* rd);
1456 #ifdef HAVE_TARGET_64_BIG
1459 Sized_relobj<64, true>::do_gc_process_relocs(const General_options& options,
1460 Symbol_table* symtab,
1462 Read_relocs_data* rd);
1465 #ifdef HAVE_TARGET_32_LITTLE
1468 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
1469 Symbol_table* symtab,
1471 Read_relocs_data* rd);
1474 #ifdef HAVE_TARGET_32_BIG
1477 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
1478 Symbol_table* symtab,
1480 Read_relocs_data* rd);
1483 #ifdef HAVE_TARGET_64_LITTLE
1486 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
1487 Symbol_table* symtab,
1489 Read_relocs_data* rd);
1492 #ifdef HAVE_TARGET_64_BIG
1495 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
1496 Symbol_table* symtab,
1498 Read_relocs_data* rd);
1501 #ifdef HAVE_TARGET_32_LITTLE
1504 Sized_relobj<32, false>::do_relocate(const General_options& options,
1505 const Symbol_table* symtab,
1506 const Layout* layout,
1510 #ifdef HAVE_TARGET_32_BIG
1513 Sized_relobj<32, true>::do_relocate(const General_options& options,
1514 const Symbol_table* symtab,
1515 const Layout* layout,
1519 #ifdef HAVE_TARGET_64_LITTLE
1522 Sized_relobj<64, false>::do_relocate(const General_options& options,
1523 const Symbol_table* symtab,
1524 const Layout* layout,
1528 #ifdef HAVE_TARGET_64_BIG
1531 Sized_relobj<64, true>::do_relocate(const General_options& options,
1532 const Symbol_table* symtab,
1533 const Layout* layout,
1537 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1539 class Merged_symbol_value<32>;
1542 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1544 class Merged_symbol_value<64>;
1547 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1549 class Symbol_value<32>;
1552 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1554 class Symbol_value<64>;
1557 #ifdef HAVE_TARGET_32_LITTLE
1559 class Track_relocs<32, false>;
1562 #ifdef HAVE_TARGET_32_BIG
1564 class Track_relocs<32, true>;
1567 #ifdef HAVE_TARGET_64_LITTLE
1569 class Track_relocs<64, false>;
1572 #ifdef HAVE_TARGET_64_BIG
1574 class Track_relocs<64, true>;
1577 } // End namespace gold.