1 // reloc.cc -- relocate input files for gold.
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
25 #include "workqueue.h"
34 // Read_relocs methods.
36 // These tasks just read the relocation information from the file.
37 // After reading it, the start another task to process the
38 // information. These tasks requires access to the file.
40 Task::Is_runnable_type
41 Read_relocs::is_runnable(Workqueue*)
43 return this->object_->is_locked() ? IS_LOCKED : IS_RUNNABLE;
49 Read_relocs::locks(Workqueue*)
51 return new Task_locker_obj<Object>(*this->object_);
54 // Read the relocations and then start a Scan_relocs_task.
57 Read_relocs::run(Workqueue* workqueue)
59 Read_relocs_data *rd = new Read_relocs_data;
60 this->object_->read_relocs(rd);
61 workqueue->queue_front(new Scan_relocs(this->options_, this->symtab_,
62 this->layout_, this->object_, rd,
63 this->symtab_lock_, this->blocker_));
66 // Scan_relocs methods.
68 // These tasks scan the relocations read by Read_relocs and mark up
69 // the symbol table to indicate which relocations are required. We
70 // use a lock on the symbol table to keep them from interfering with
73 Task::Is_runnable_type
74 Scan_relocs::is_runnable(Workqueue*)
76 if (!this->symtab_lock_->is_writable() || this->object_->is_locked())
81 // Return the locks we hold: one on the file, one on the symbol table
84 class Scan_relocs::Scan_relocs_locker : public Task_locker
87 Scan_relocs_locker(Object* object, Task_token& symtab_lock, Task* task,
88 Task_token& blocker, Workqueue* workqueue)
89 : objlock_(*object), symtab_locker_(symtab_lock, task),
90 blocker_(blocker, workqueue)
94 Task_locker_obj<Object> objlock_;
95 Task_locker_write symtab_locker_;
96 Task_locker_block blocker_;
100 Scan_relocs::locks(Workqueue* workqueue)
102 return new Scan_relocs_locker(this->object_, *this->symtab_lock_, this,
103 *this->blocker_, workqueue);
109 Scan_relocs::run(Workqueue*)
111 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
117 // Relocate_task methods.
119 // We may have to wait for the output sections to be written.
121 Task::Is_runnable_type
122 Relocate_task::is_runnable(Workqueue*)
124 if (this->object_->relocs_must_follow_section_writes()
125 && this->output_sections_blocker_->is_blocked())
131 // We want to lock the file while we run. We want to unblock
132 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
134 class Relocate_task::Relocate_locker : public Task_locker
137 Relocate_locker(Task_token& input_sections_blocker,
138 Task_token& final_blocker, Workqueue* workqueue,
140 : input_sections_blocker_(input_sections_blocker, workqueue),
141 final_blocker_(final_blocker, workqueue),
146 Task_block_token input_sections_blocker_;
147 Task_block_token final_blocker_;
148 Task_locker_obj<Object> objlock_;
152 Relocate_task::locks(Workqueue* workqueue)
154 return new Relocate_locker(*this->input_sections_blocker_,
155 *this->final_blocker_,
163 Relocate_task::run(Workqueue*)
165 this->object_->relocate(this->options_, this->symtab_, this->layout_,
169 // Read the relocs and local symbols from the object file and store
170 // the information in RD.
172 template<int size, bool big_endian>
174 Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
178 unsigned int shnum = this->shnum();
182 rd->relocs.reserve(shnum / 2);
184 std::vector<Map_to_output>& map_sections(this->map_to_output());
186 const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
187 shnum * This::shdr_size,
189 // Skip the first, dummy, section.
190 const unsigned char *ps = pshdrs + This::shdr_size;
191 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
193 typename This::Shdr shdr(ps);
195 unsigned int sh_type = shdr.get_sh_type();
196 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
199 unsigned int shndx = shdr.get_sh_info();
202 this->error(_("relocation section %u has bad info %u"),
207 Output_section* os = map_sections[shndx].output_section;
211 // We are scanning relocations in order to fill out the GOT and
212 // PLT sections. Relocations for sections which are not
213 // allocated (typically debugging sections) should not add new
214 // GOT and PLT entries. So we skip them.
215 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
216 if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
219 if (shdr.get_sh_link() != this->symtab_shndx_)
221 this->error(_("relocation section %u uses unexpected "
223 i, shdr.get_sh_link());
227 off_t sh_size = shdr.get_sh_size();
229 unsigned int reloc_size;
230 if (sh_type == elfcpp::SHT_REL)
231 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
233 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
234 if (reloc_size != shdr.get_sh_entsize())
236 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
237 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
242 size_t reloc_count = sh_size / reloc_size;
243 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
245 this->error(_("reloc section %u size %lu uneven"),
246 i, static_cast<unsigned long>(sh_size));
250 rd->relocs.push_back(Section_relocs());
251 Section_relocs& sr(rd->relocs.back());
253 sr.data_shndx = shndx;
254 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
256 sr.sh_type = sh_type;
257 sr.reloc_count = reloc_count;
258 sr.output_section = os;
259 sr.needs_special_offset_handling = map_sections[shndx].offset == -1;
262 // Read the local symbols.
263 gold_assert(this->symtab_shndx_ != -1U);
264 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
265 rd->local_symbols = NULL;
268 typename This::Shdr symtabshdr(pshdrs
269 + this->symtab_shndx_ * This::shdr_size);
270 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
271 const int sym_size = This::sym_size;
272 const unsigned int loccount = this->local_symbol_count_;
273 gold_assert(loccount == symtabshdr.get_sh_info());
274 off_t locsize = loccount * sym_size;
275 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
280 // Scan the relocs and adjust the symbol table. This looks for
281 // relocations which require GOT/PLT/COPY relocations.
283 template<int size, bool big_endian>
285 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
286 Symbol_table* symtab,
288 Read_relocs_data* rd)
290 Sized_target<size, big_endian>* target = this->sized_target();
292 const unsigned char* local_symbols;
293 if (rd->local_symbols == NULL)
294 local_symbols = NULL;
296 local_symbols = rd->local_symbols->data();
298 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
299 p != rd->relocs.end();
302 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
303 p->sh_type, p->contents->data(), p->reloc_count,
304 p->output_section, p->needs_special_offset_handling,
305 this->local_symbol_count_,
311 if (rd->local_symbols != NULL)
313 delete rd->local_symbols;
314 rd->local_symbols = NULL;
318 // Relocate the input sections and write out the local symbols.
320 template<int size, bool big_endian>
322 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
323 const Symbol_table* symtab,
324 const Layout* layout,
327 unsigned int shnum = this->shnum();
329 // Read the section headers.
330 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
331 shnum * This::shdr_size,
337 // Make two passes over the sections. The first one copies the
338 // section data to the output file. The second one applies
341 this->write_sections(pshdrs, of, &views);
343 // Apply relocations.
345 this->relocate_sections(options, symtab, layout, pshdrs, &views);
347 // Write out the accumulated views.
348 for (unsigned int i = 1; i < shnum; ++i)
350 if (views[i].view != NULL)
352 if (views[i].is_input_output_view)
353 of->write_input_output_view(views[i].offset, views[i].view_size,
356 of->write_output_view(views[i].offset, views[i].view_size,
361 // Write out the local symbols.
362 this->write_local_symbols(of, layout->sympool());
365 // Write section data to the output file. PSHDRS points to the
366 // section headers. Record the views in *PVIEWS for use when
369 template<int size, bool big_endian>
371 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
375 unsigned int shnum = this->shnum();
376 std::vector<Map_to_output>& map_sections(this->map_to_output());
378 const unsigned char* p = pshdrs + This::shdr_size;
379 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
381 View_size* pvs = &(*pviews)[i];
385 const Output_section* os = map_sections[i].output_section;
388 off_t output_offset = map_sections[i].offset;
390 typename This::Shdr shdr(p);
392 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
397 if (output_offset != -1)
399 view_start = os->offset() + output_offset;
400 view_size = shdr.get_sh_size();
404 view_start = os->offset();
405 view_size = os->data_size();
411 gold_assert(output_offset == -1
412 || (output_offset >= 0
413 && output_offset + view_size <= os->data_size()));
416 if (output_offset == -1)
417 view = of->get_input_output_view(view_start, view_size);
420 view = of->get_output_view(view_start, view_size);
421 this->read(shdr.get_sh_offset(), view_size, view);
425 pvs->address = os->address();
426 if (output_offset != -1)
427 pvs->address += output_offset;
428 pvs->offset = view_start;
429 pvs->view_size = view_size;
430 pvs->is_input_output_view = output_offset == -1;
434 // Relocate section data. VIEWS points to the section data as views
435 // in the output file.
437 template<int size, bool big_endian>
439 Sized_relobj<size, big_endian>::relocate_sections(
440 const General_options& options,
441 const Symbol_table* symtab,
442 const Layout* layout,
443 const unsigned char* pshdrs,
446 unsigned int shnum = this->shnum();
447 Sized_target<size, big_endian>* target = this->sized_target();
449 std::vector<Map_to_output>& map_sections(this->map_to_output());
451 Relocate_info<size, big_endian> relinfo;
452 relinfo.options = &options;
453 relinfo.symtab = symtab;
454 relinfo.layout = layout;
455 relinfo.object = this;
457 const unsigned char* p = pshdrs + This::shdr_size;
458 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
460 typename This::Shdr shdr(p);
462 unsigned int sh_type = shdr.get_sh_type();
463 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
466 unsigned int index = shdr.get_sh_info();
467 if (index >= this->shnum())
469 this->error(_("relocation section %u has bad info %u"),
474 Output_section* os = map_sections[index].output_section;
477 // This relocation section is against a section which we
481 off_t output_offset = map_sections[index].offset;
483 gold_assert((*pviews)[index].view != NULL);
485 if (shdr.get_sh_link() != this->symtab_shndx_)
487 gold_error(_("relocation section %u uses unexpected "
489 i, shdr.get_sh_link());
493 off_t sh_size = shdr.get_sh_size();
494 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
497 unsigned int reloc_size;
498 if (sh_type == elfcpp::SHT_REL)
499 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
501 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
503 if (reloc_size != shdr.get_sh_entsize())
505 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
506 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
511 size_t reloc_count = sh_size / reloc_size;
512 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
514 gold_error(_("reloc section %u size %lu uneven"),
515 i, static_cast<unsigned long>(sh_size));
519 relinfo.reloc_shndx = i;
520 relinfo.data_shndx = index;
521 target->relocate_section(&relinfo,
527 (*pviews)[index].view,
528 (*pviews)[index].address,
529 (*pviews)[index].view_size);
533 // Copy_relocs::Copy_reloc_entry methods.
535 // Return whether we should emit this reloc. We should emit it if the
536 // symbol is still defined in a dynamic object. If we should not emit
537 // it, we clear it, to save ourselves the test next time.
539 template<int size, bool big_endian>
541 Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
543 if (this->sym_ == NULL)
545 if (this->sym_->is_from_dynobj())
551 // Emit a reloc into a SHT_REL section.
553 template<int size, bool big_endian>
555 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
556 Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
558 this->sym_->set_needs_dynsym_entry();
559 reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
560 this->relobj_, this->shndx_, this->address_);
563 // Emit a reloc into a SHT_RELA section.
565 template<int size, bool big_endian>
567 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
568 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
570 this->sym_->set_needs_dynsym_entry();
571 reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
572 this->relobj_, this->shndx_, this->address_,
576 // Copy_relocs methods.
578 // Return whether we need a COPY reloc for a relocation against GSYM.
579 // The relocation is being applied to section SHNDX in OBJECT.
581 template<int size, bool big_endian>
583 Copy_relocs<size, big_endian>::need_copy_reloc(
584 const General_options*,
587 Sized_symbol<size>* sym)
589 // FIXME: Handle -z nocopyrelocs.
591 if (sym->symsize() == 0)
594 // If this is a readonly section, then we need a COPY reloc.
595 // Otherwise we can use a dynamic reloc.
596 if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
604 template<int size, bool big_endian>
606 Copy_relocs<size, big_endian>::save(
610 Output_section* output_section,
611 const elfcpp::Rel<size, big_endian>& rel)
613 unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
614 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
616 rel.get_r_offset(), 0));
619 // Save a Rela reloc.
621 template<int size, bool big_endian>
623 Copy_relocs<size, big_endian>::save(
627 Output_section* output_section,
628 const elfcpp::Rela<size, big_endian>& rela)
630 unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
631 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
634 rela.get_r_addend()));
637 // Return whether there are any relocs to emit. We don't want to emit
638 // a reloc if the symbol is no longer defined in a dynamic object.
640 template<int size, bool big_endian>
642 Copy_relocs<size, big_endian>::any_to_emit()
644 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
645 p != this->entries_.end();
648 if (p->should_emit())
656 template<int size, bool big_endian>
657 template<int sh_type>
659 Copy_relocs<size, big_endian>::emit(
660 Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
662 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
663 p != this->entries_.end();
666 if (p->should_emit())
671 // Track_relocs methods.
673 // Initialize the class to track the relocs. This gets the object,
674 // the reloc section index, and the type of the relocs. This returns
675 // false if something goes wrong.
677 template<int size, bool big_endian>
679 Track_relocs<size, big_endian>::initialize(
681 unsigned int reloc_shndx,
682 unsigned int reloc_type)
684 // If RELOC_SHNDX is -1U, it means there is more than one reloc
685 // section for the .eh_frame section. We can't handle that case.
686 if (reloc_shndx == -1U)
689 // If RELOC_SHNDX is 0, there is no reloc section.
690 if (reloc_shndx == 0)
693 // Get the contents of the reloc section.
694 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
696 if (reloc_type == elfcpp::SHT_REL)
697 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
698 else if (reloc_type == elfcpp::SHT_RELA)
699 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
703 if (this->len_ % this->reloc_size_ != 0)
705 object->error(_("reloc section size %zu is not a multiple of "
707 static_cast<size_t>(this->len_),
715 // Return the offset of the next reloc, or -1 if there isn't one.
717 template<int size, bool big_endian>
719 Track_relocs<size, big_endian>::next_offset() const
721 if (this->pos_ >= this->len_)
724 // Rel and Rela start out the same, so we can always use Rel to find
725 // the r_offset value.
726 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
727 return rel.get_r_offset();
730 // Return the index of the symbol referenced by the next reloc, or -1U
731 // if there aren't any more relocs.
733 template<int size, bool big_endian>
735 Track_relocs<size, big_endian>::next_symndx() const
737 if (this->pos_ >= this->len_)
740 // Rel and Rela start out the same, so we can use Rel to find the
742 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
743 return elfcpp::elf_r_sym<size>(rel.get_r_info());
746 // Advance to the next reloc whose r_offset is greater than or equal
747 // to OFFSET. Return the number of relocs we skip.
749 template<int size, bool big_endian>
751 Track_relocs<size, big_endian>::advance(off_t offset)
754 while (this->pos_ < this->len_)
756 // Rel and Rela start out the same, so we can always use Rel to
757 // find the r_offset value.
758 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
759 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
762 this->pos_ += this->reloc_size_;
767 // Instantiate the templates we need. We could use the configure
768 // script to restrict this to only the ones for implemented targets.
770 #ifdef HAVE_TARGET_32_LITTLE
773 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
776 #ifdef HAVE_TARGET_32_BIG
779 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
782 #ifdef HAVE_TARGET_64_LITTLE
785 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
788 #ifdef HAVE_TARGET_64_BIG
791 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
794 #ifdef HAVE_TARGET_32_LITTLE
797 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
798 Symbol_table* symtab,
800 Read_relocs_data* rd);
803 #ifdef HAVE_TARGET_32_BIG
806 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
807 Symbol_table* symtab,
809 Read_relocs_data* rd);
812 #ifdef HAVE_TARGET_64_LITTLE
815 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
816 Symbol_table* symtab,
818 Read_relocs_data* rd);
821 #ifdef HAVE_TARGET_64_BIG
824 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
825 Symbol_table* symtab,
827 Read_relocs_data* rd);
830 #ifdef HAVE_TARGET_32_LITTLE
833 Sized_relobj<32, false>::do_relocate(const General_options& options,
834 const Symbol_table* symtab,
835 const Layout* layout,
839 #ifdef HAVE_TARGET_32_BIG
842 Sized_relobj<32, true>::do_relocate(const General_options& options,
843 const Symbol_table* symtab,
844 const Layout* layout,
848 #ifdef HAVE_TARGET_64_LITTLE
851 Sized_relobj<64, false>::do_relocate(const General_options& options,
852 const Symbol_table* symtab,
853 const Layout* layout,
857 #ifdef HAVE_TARGET_64_BIG
860 Sized_relobj<64, true>::do_relocate(const General_options& options,
861 const Symbol_table* symtab,
862 const Layout* layout,
866 #ifdef HAVE_TARGET_32_LITTLE
868 class Copy_relocs<32, false>;
871 #ifdef HAVE_TARGET_32_BIG
873 class Copy_relocs<32, true>;
876 #ifdef HAVE_TARGET_64_LITTLE
878 class Copy_relocs<64, false>;
881 #ifdef HAVE_TARGET_64_BIG
883 class Copy_relocs<64, true>;
886 #ifdef HAVE_TARGET_32_LITTLE
889 Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
890 Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
893 #ifdef HAVE_TARGET_32_BIG
896 Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
897 Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
900 #ifdef HAVE_TARGET_64_LITTLE
903 Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
904 Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
907 #ifdef HAVE_TARGET_64_BIG
910 Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
911 Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
914 #ifdef HAVE_TARGET_32_LITTLE
917 Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
918 Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
921 #ifdef HAVE_TARGET_32_BIG
924 Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
925 Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
928 #ifdef HAVE_TARGET_64_LITTLE
931 Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
932 Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
935 #ifdef HAVE_TARGET_64_BIG
938 Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
939 Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
942 #ifdef HAVE_TARGET_32_LITTLE
944 class Track_relocs<32, false>;
947 #ifdef HAVE_TARGET_32_BIG
949 class Track_relocs<32, true>;
952 #ifdef HAVE_TARGET_64_LITTLE
954 class Track_relocs<64, false>;
957 #ifdef HAVE_TARGET_64_BIG
959 class Track_relocs<64, true>;
962 } // End namespace gold.