X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=gold%2Freloc.cc;h=76ab196139530d218cc4622fb81c306cc77fe49b;hb=32866df75ece22ec1fd88e99e3c5effe9238e072;hp=bb672e4e100f9b97ce3c7a7557057fff79f2fe6d;hpb=92e059d8dc78c3f65e29e48e368f6e47ea0ab671;p=deliverable%2Fbinutils-gdb.git diff --git a/gold/reloc.cc b/gold/reloc.cc index bb672e4e10..76ab196139 100644 --- a/gold/reloc.cc +++ b/gold/reloc.cc @@ -4,6 +4,7 @@ #include "workqueue.h" #include "object.h" +#include "symtab.h" #include "output.h" #include "reloc.h" @@ -38,8 +39,8 @@ Read_relocs::run(Workqueue* workqueue) Read_relocs_data *rd = new Read_relocs_data; this->object_->read_relocs(rd); workqueue->queue_front(new Scan_relocs(this->options_, this->symtab_, - this->object_, rd, this->symtab_lock_, - this->blocker_)); + this->layout_, this->object_, rd, + this->symtab_lock_, this->blocker_)); } // Scan_relocs methods. @@ -52,7 +53,9 @@ Read_relocs::run(Workqueue* workqueue) Task::Is_runnable_type Scan_relocs::is_runnable(Workqueue*) { - return this->symtab_lock_->is_writable() ? IS_RUNNABLE : IS_LOCKED; + if (!this->symtab_lock_->is_writable() || this->object_->is_locked()) + return IS_LOCKED; + return IS_RUNNABLE; } // Return the locks we hold: one on the file, one on the symbol table @@ -85,7 +88,8 @@ Scan_relocs::locks(Workqueue* workqueue) void Scan_relocs::run(Workqueue*) { - this->object_->scan_relocs(this->options_, this->symtab_, this->rd_); + this->object_->scan_relocs(this->options_, this->symtab_, this->layout_, + this->rd_); delete this->rd_; this->rd_ = NULL; } @@ -137,7 +141,7 @@ Relocate_task::run(Workqueue*) template void -Sized_object::do_read_relocs(Read_relocs_data* rd) +Sized_relobj::do_read_relocs(Read_relocs_data* rd) { rd->relocs.clear(); @@ -147,7 +151,7 @@ Sized_object::do_read_relocs(Read_relocs_data* rd) rd->relocs.reserve(shnum / 2); - const unsigned char *pshdrs = this->get_view(this->shoff_, + const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(), shnum * This::shdr_size); // Skip the first, dummy, section. const unsigned char *ps = pshdrs + This::shdr_size; @@ -170,7 +174,15 @@ Sized_object::do_read_relocs(Read_relocs_data* rd) if (!this->is_section_included(shndx)) continue; - if (shdr.get_sh_link() != this->symtab_shnum_) + // We are scanning relocations in order to fill out the GOT and + // PLT sections. Relocations for sections which are not + // allocated (typically debugging sections) should not add new + // GOT and PLT entries. So we skip them. + typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size); + if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) + continue; + + if (shdr.get_sh_link() != this->symtab_shndx_) { fprintf(stderr, _("%s: %s: relocation section %u uses unexpected " @@ -216,16 +228,17 @@ Sized_object::do_read_relocs(Read_relocs_data* rd) } // Read the local symbols. - if (this->symtab_shnum_ == 0 || this->local_symbol_count_ == 0) + gold_assert(this->symtab_shndx_ != -1U); + if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0) rd->local_symbols = NULL; else { typename This::Shdr symtabshdr(pshdrs - + this->symtab_shnum_ * This::shdr_size); - assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); + + this->symtab_shndx_ * This::shdr_size); + gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); const int sym_size = This::sym_size; const unsigned int loccount = this->local_symbol_count_; - assert(loccount == symtabshdr.get_sh_info()); + gold_assert(loccount == symtabshdr.get_sh_info()); off_t locsize = loccount * sym_size; rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(), locsize); @@ -237,8 +250,9 @@ Sized_object::do_read_relocs(Read_relocs_data* rd) template void -Sized_object::do_scan_relocs(const General_options& options, +Sized_relobj::do_scan_relocs(const General_options& options, Symbol_table* symtab, + Layout* layout, Read_relocs_data* rd) { Sized_target* target = this->sized_target(); @@ -253,8 +267,8 @@ Sized_object::do_scan_relocs(const General_options& options, p != rd->relocs.end(); ++p) { - target->scan_relocs(options, symtab, this, p->sh_type, - p->contents->data(), p->reloc_count, + target->scan_relocs(options, symtab, layout, this, p->data_shndx, + p->sh_type, p->contents->data(), p->reloc_count, this->local_symbol_count_, local_symbols, this->symbols_); @@ -273,7 +287,7 @@ Sized_object::do_scan_relocs(const General_options& options, template void -Sized_object::do_relocate(const General_options& options, +Sized_relobj::do_relocate(const General_options& options, const Symbol_table* symtab, const Layout* layout, Output_file* of) @@ -281,7 +295,7 @@ Sized_object::do_relocate(const General_options& options, unsigned int shnum = this->shnum(); // Read the section headers. - const unsigned char* pshdrs = this->get_view(this->shoff_, + const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(), shnum * This::shdr_size); Views views; @@ -315,7 +329,7 @@ Sized_object::do_relocate(const General_options& options, template void -Sized_object::write_sections(const unsigned char* pshdrs, +Sized_relobj::write_sections(const unsigned char* pshdrs, Output_file* of, Views* pviews) { @@ -329,6 +343,9 @@ Sized_object::write_sections(const unsigned char* pshdrs, pvs->view = NULL; + if (map_sections[i].offset == -1) + continue; + const Output_section* os = map_sections[i].output_section; if (os == NULL) continue; @@ -338,11 +355,15 @@ Sized_object::write_sections(const unsigned char* pshdrs, if (shdr.get_sh_type() == elfcpp::SHT_NOBITS) continue; - assert(map_sections[i].offset >= 0 - && map_sections[i].offset < os->data_size()); off_t start = os->offset() + map_sections[i].offset; off_t sh_size = shdr.get_sh_size(); + if (sh_size == 0) + continue; + + gold_assert(map_sections[i].offset >= 0 + && map_sections[i].offset + sh_size <= os->data_size()); + unsigned char* view = of->get_output_view(start, sh_size); this->read(shdr.get_sh_offset(), sh_size, view); @@ -358,7 +379,7 @@ Sized_object::write_sections(const unsigned char* pshdrs, template void -Sized_object::relocate_sections( +Sized_relobj::relocate_sections( const General_options& options, const Symbol_table* symtab, const Layout* layout, @@ -374,7 +395,7 @@ Sized_object::relocate_sections( relinfo.layout = layout; relinfo.object = this; relinfo.local_symbol_count = this->local_symbol_count_; - relinfo.values = this->values_; + relinfo.local_values = &this->local_values_; relinfo.symbols = this->symbols_; const unsigned char* p = pshdrs + This::shdr_size; @@ -401,9 +422,9 @@ Sized_object::relocate_sections( continue; } - assert((*pviews)[index].view != NULL); + gold_assert((*pviews)[index].view != NULL); - if (shdr.get_sh_link() != this->symtab_shnum_) + if (shdr.get_sh_link() != this->symtab_shndx_) { fprintf(stderr, _("%s: %s: relocation section %u uses unexpected " @@ -454,76 +475,264 @@ Sized_object::relocate_sections( } } +// Copy_relocs::Copy_reloc_entry methods. + +// Return whether we should emit this reloc. We should emit it if the +// symbol is still defined in a dynamic object. If we should not emit +// it, we clear it, to save ourselves the test next time. + +template +bool +Copy_relocs::Copy_reloc_entry::should_emit() +{ + if (this->sym_ == NULL) + return false; + if (this->sym_->is_from_dynobj()) + return true; + this->sym_ = NULL; + return false; +} + +// Emit a reloc into a SHT_REL section. + +template +void +Copy_relocs::Copy_reloc_entry::emit( + Output_data_reloc* reloc_data) +{ + this->sym_->set_needs_dynsym_entry(); + reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_, + this->shndx_, this->address_); +} + +// Emit a reloc into a SHT_RELA section. + +template +void +Copy_relocs::Copy_reloc_entry::emit( + Output_data_reloc* reloc_data) +{ + this->sym_->set_needs_dynsym_entry(); + reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_, + this->shndx_, this->address_, this->addend_); +} + +// Copy_relocs methods. + +// Return whether we need a COPY reloc for a relocation against GSYM. +// The relocation is being applied to section SHNDX in OBJECT. + +template +bool +Copy_relocs::need_copy_reloc( + const General_options*, + Relobj* object, + unsigned int shndx, + Sized_symbol* sym) +{ + // FIXME: Handle -z nocopyrelocs. + + if (sym->symsize() == 0) + return false; + + // If this is a readonly section, then we need a COPY reloc. + // Otherwise we can use a dynamic reloc. + if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0) + return true; + + return false; +} + +// Save a Rel reloc. + +template +void +Copy_relocs::save( + Symbol* sym, + Relobj* relobj, + unsigned int shndx, + const elfcpp::Rel& rel) +{ + unsigned int reloc_type = elfcpp::elf_r_type(rel.get_r_info()); + this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx, + rel.get_r_offset(), 0)); +} + +// Save a Rela reloc. + +template +void +Copy_relocs::save( + Symbol* sym, + Relobj* relobj, + unsigned int shndx, + const elfcpp::Rela& rela) +{ + unsigned int reloc_type = elfcpp::elf_r_type(rela.get_r_info()); + this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx, + rela.get_r_offset(), + rela.get_r_addend())); +} + +// Return whether there are any relocs to emit. We don't want to emit +// a reloc if the symbol is no longer defined in a dynamic object. + +template +bool +Copy_relocs::any_to_emit() +{ + for (typename Copy_reloc_entries::iterator p = this->entries_.begin(); + p != this->entries_.end(); + ++p) + { + if (p->should_emit()) + return true; + } + return false; +} + +// Emit relocs. + +template +template +void +Copy_relocs::emit( + Output_data_reloc* reloc_data) +{ + for (typename Copy_reloc_entries::iterator p = this->entries_.begin(); + p != this->entries_.end(); + ++p) + { + if (p->should_emit()) + p->emit(reloc_data); + } +} + // Instantiate the templates we need. We could use the configure // script to restrict this to only the ones for implemented targets. template void -Sized_object<32, false>::do_read_relocs(Read_relocs_data* rd); +Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd); template void -Sized_object<32, true>::do_read_relocs(Read_relocs_data* rd); +Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd); template void -Sized_object<64, false>::do_read_relocs(Read_relocs_data* rd); +Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd); template void -Sized_object<64, true>::do_read_relocs(Read_relocs_data* rd); +Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd); template void -Sized_object<32, false>::do_scan_relocs(const General_options& options, +Sized_relobj<32, false>::do_scan_relocs(const General_options& options, Symbol_table* symtab, + Layout* layout, Read_relocs_data* rd); template void -Sized_object<32, true>::do_scan_relocs(const General_options& options, +Sized_relobj<32, true>::do_scan_relocs(const General_options& options, Symbol_table* symtab, + Layout* layout, Read_relocs_data* rd); template void -Sized_object<64, false>::do_scan_relocs(const General_options& options, +Sized_relobj<64, false>::do_scan_relocs(const General_options& options, Symbol_table* symtab, + Layout* layout, Read_relocs_data* rd); template void -Sized_object<64, true>::do_scan_relocs(const General_options& options, +Sized_relobj<64, true>::do_scan_relocs(const General_options& options, Symbol_table* symtab, + Layout* layout, Read_relocs_data* rd); template void -Sized_object<32, false>::do_relocate(const General_options& options, +Sized_relobj<32, false>::do_relocate(const General_options& options, const Symbol_table* symtab, const Layout* layout, Output_file* of); template void -Sized_object<32, true>::do_relocate(const General_options& options, +Sized_relobj<32, true>::do_relocate(const General_options& options, const Symbol_table* symtab, const Layout* layout, Output_file* of); template void -Sized_object<64, false>::do_relocate(const General_options& options, +Sized_relobj<64, false>::do_relocate(const General_options& options, const Symbol_table* symtab, const Layout* layout, Output_file* of); template void -Sized_object<64, true>::do_relocate(const General_options& options, +Sized_relobj<64, true>::do_relocate(const General_options& options, const Symbol_table* symtab, const Layout* layout, Output_file* of); +template +class Copy_relocs<32, false>; + +template +class Copy_relocs<32, true>; + +template +class Copy_relocs<64, false>; + +template +class Copy_relocs<64, true>; + +template +void +Copy_relocs<32, false>::emit( + Output_data_reloc*); + +template +void +Copy_relocs<32, true>::emit( + Output_data_reloc*); + +template +void +Copy_relocs<64, false>::emit( + Output_data_reloc*); + +template +void +Copy_relocs<64, true>::emit( + Output_data_reloc*); + +template +void +Copy_relocs<32, false>::emit( + Output_data_reloc*); + +template +void +Copy_relocs<32, true>::emit( + Output_data_reloc*); + +template +void +Copy_relocs<64, false>::emit( + Output_data_reloc*); + +template +void +Copy_relocs<64, true>::emit( + Output_data_reloc*); } // End namespace gold.