Fix soname for library found in search path.
[binutils-gdb.git] / gold / reloc.cc
index 7233266fa3f3c22f6418a3b6e55eb04182eec14a..bf4b2d66ead92a6dbf4c1fcec9db20b8c45d0e20 100644 (file)
@@ -1,9 +1,30 @@
 // reloc.cc -- relocate input files for gold.
 
+// Copyright 2006, 2007 Free Software Foundation, Inc.
+// Written by Ian Lance Taylor <iant@google.com>.
+
+// This file is part of gold.
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+// MA 02110-1301, USA.
+
 #include "gold.h"
 
 #include "workqueue.h"
 #include "object.h"
+#include "symtab.h"
 #include "output.h"
 #include "reloc.h"
 
@@ -150,8 +171,9 @@ Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
 
   rd->relocs.reserve(shnum / 2);
 
-  const unsigned char *pshdrs = this->get_view(this->shoff_,
-                                              shnum * This::shdr_size);
+  const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
+                                              shnum * This::shdr_size,
+                                              true);
   // Skip the first, dummy, section.
   const unsigned char *ps = pshdrs + This::shdr_size;
   for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
@@ -181,7 +203,7 @@ Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
       if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
        continue;
 
-      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 "
@@ -209,7 +231,7 @@ Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
        }
 
       size_t reloc_count = sh_size / reloc_size;
-      if (reloc_count * reloc_size != sh_size)
+      if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
        {
          fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"),
                  program_name, this->name().c_str(), i,
@@ -221,25 +243,27 @@ Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
       Section_relocs& sr(rd->relocs.back());
       sr.reloc_shndx = i;
       sr.data_shndx = shndx;
-      sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size);
+      sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
+                                          true);
       sr.sh_type = sh_type;
       sr.reloc_count = reloc_count;
     }
 
   // 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);
+                                                locsize, true);
     }
 }
 
@@ -265,8 +289,8 @@ Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
        p != rd->relocs.end();
        ++p)
     {
-      target->scan_relocs(options, symtab, layout, 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_);
@@ -293,8 +317,9 @@ Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
   unsigned int shnum = this->shnum();
 
   // Read the section headers.
-  const unsigned char* pshdrs = this->get_view(this->shoff_,
-                                              shnum * This::shdr_size);
+  const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
+                                              shnum * This::shdr_size,
+                                              true);
 
   Views views;
   views.resize(shnum);
@@ -341,6 +366,9 @@ Sized_relobj<size, big_endian>::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;
@@ -356,8 +384,8 @@ Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
       if (sh_size == 0)
        continue;
 
-      assert(map_sections[i].offset >= 0
-            && map_sections[i].offset + sh_size <= os->data_size());
+      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);
@@ -390,7 +418,7 @@ Sized_relobj<size, big_endian>::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;
@@ -417,9 +445,9 @@ Sized_relobj<size, big_endian>::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 "
@@ -430,7 +458,7 @@ Sized_relobj<size, big_endian>::relocate_sections(
 
       off_t sh_size = shdr.get_sh_size();
       const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
-                                                   sh_size);
+                                                   sh_size, false);
 
       unsigned int reloc_size;
       if (sh_type == elfcpp::SHT_REL)
@@ -450,7 +478,7 @@ Sized_relobj<size, big_endian>::relocate_sections(
        }
 
       size_t reloc_count = sh_size / reloc_size;
-      if (reloc_count * reloc_size != sh_size)
+      if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
        {
          fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"),
                  program_name, this->name().c_str(), i,
@@ -470,80 +498,312 @@ Sized_relobj<size, big_endian>::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<int size, bool big_endian>
+bool
+Copy_relocs<size, big_endian>::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<int size, bool big_endian>
+void
+Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
+    Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* 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<int size, bool big_endian>
+void
+Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
+    Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* 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<int size, bool big_endian>
+bool
+Copy_relocs<size, big_endian>::need_copy_reloc(
+    const General_options*,
+    Relobj* object,
+    unsigned int shndx,
+    Sized_symbol<size>* 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<int size, bool big_endian>
+void
+Copy_relocs<size, big_endian>::save(
+    Symbol* sym,
+    Relobj* relobj,
+    unsigned int shndx,
+    const elfcpp::Rel<size, big_endian>& rel)
+{
+  unsigned int reloc_type = elfcpp::elf_r_type<size>(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<int size, bool big_endian>
+void
+Copy_relocs<size, big_endian>::save(
+    Symbol* sym,
+    Relobj* relobj,
+    unsigned int shndx,
+    const elfcpp::Rela<size, big_endian>& rela)
+{
+  unsigned int reloc_type = elfcpp::elf_r_type<size>(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<int size, bool big_endian>
+bool
+Copy_relocs<size, big_endian>::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<int size, bool big_endian>
+template<int sh_type>
+void
+Copy_relocs<size, big_endian>::emit(
+    Output_data_reloc<sh_type, true, size, big_endian>* 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.
 
+#ifdef HAVE_TARGET_32_LITTLE
 template
 void
 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
+#endif
 
+#ifdef HAVE_TARGET_32_BIG
 template
 void
 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
+#endif
 
+#ifdef HAVE_TARGET_64_LITTLE
 template
 void
 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
+#endif
 
+#ifdef HAVE_TARGET_64_BIG
 template
 void
 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
+#endif
 
+#ifdef HAVE_TARGET_32_LITTLE
 template
 void
 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
                                        Symbol_table* symtab,
                                        Layout* layout,
                                        Read_relocs_data* rd);
+#endif
 
+#ifdef HAVE_TARGET_32_BIG
 template
 void
 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
                                       Symbol_table* symtab,
                                       Layout* layout,
                                       Read_relocs_data* rd);
+#endif
 
+#ifdef HAVE_TARGET_64_LITTLE
 template
 void
 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
                                        Symbol_table* symtab,
                                        Layout* layout,
                                        Read_relocs_data* rd);
+#endif
 
+#ifdef HAVE_TARGET_64_BIG
 template
 void
 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
                                       Symbol_table* symtab,
                                       Layout* layout,
                                       Read_relocs_data* rd);
+#endif
 
+#ifdef HAVE_TARGET_32_LITTLE
 template
 void
 Sized_relobj<32, false>::do_relocate(const General_options& options,
                                     const Symbol_table* symtab,
                                     const Layout* layout,
                                     Output_file* of);
+#endif
 
+#ifdef HAVE_TARGET_32_BIG
 template
 void
 Sized_relobj<32, true>::do_relocate(const General_options& options,
                                    const Symbol_table* symtab,
                                    const Layout* layout,
                                    Output_file* of);
+#endif
 
+#ifdef HAVE_TARGET_64_LITTLE
 template
 void
 Sized_relobj<64, false>::do_relocate(const General_options& options,
                                     const Symbol_table* symtab,
                                     const Layout* layout,
                                     Output_file* of);
+#endif
 
+#ifdef HAVE_TARGET_64_BIG
 template
 void
 Sized_relobj<64, true>::do_relocate(const General_options& options,
                                    const Symbol_table* symtab,
                                    const Layout* layout,
                                    Output_file* of);
+#endif
+
+#ifdef HAVE_TARGET_32_LITTLE
+template
+class Copy_relocs<32, false>;
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+class Copy_relocs<32, true>;
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+class Copy_relocs<64, false>;
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+class Copy_relocs<64, true>;
+#endif
 
+#ifdef HAVE_TARGET_32_LITTLE
+template
+void
+Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
+    Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+void
+Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
+    Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+void
+Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
+    Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+void
+Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
+    Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
+#endif
+
+#ifdef HAVE_TARGET_32_LITTLE
+template
+void
+Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
+    Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+void
+Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
+    Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+void
+Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
+    Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+void
+Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
+    Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
+#endif
 
 } // End namespace gold.