From Andrew Chatham: add signedrel<>, rel32s and friends.
[deliverable/binutils-gdb.git] / gold / object.cc
index 74c1347712ee63e0d3c8c92a994606c3f63f563c..305190923c5f37f91fec16f7a7519f1a2410fc04 100644 (file)
@@ -1,10 +1,29 @@
 // object.cc -- support for an object file for linking in 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 <cerrno>
 #include <cstring>
-#include <cassert>
 #include <cstdarg>
 
 #include "target-select.h"
@@ -54,11 +73,11 @@ Object::error(const char* format, ...)
 // Return a view of the contents of a section.
 
 const unsigned char*
-Object::section_contents(unsigned int shndx, off_t* plen)
+Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
 {
   Location loc(this->do_section_contents(shndx));
   *plen = loc.data_size;
-  return this->get_view(loc.file_offset, loc.data_size);
+  return this->get_view(loc.file_offset, loc.data_size, cache);
 }
 
 // Read the section data into SD.  This is code common to Sized_relobj
@@ -74,7 +93,7 @@ Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
   // Read the section headers.
   const off_t shoff = elf_file->shoff();
   const unsigned int shnum = this->shnum();
-  sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size);
+  sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, true);
 
   // Read the section names.
   const unsigned char* pshdrs = sd->section_headers->data();
@@ -92,7 +111,7 @@ Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
 
   sd->section_names_size = shdrnames.get_sh_size();
   sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
-                                            sd->section_names_size);
+                                            sd->section_names_size, false);
 }
 
 // If NAME is the name of a special .gnu.warning section, arrange for
@@ -128,8 +147,7 @@ Sized_relobj<size, big_endian>::Sized_relobj(
     output_local_symbol_count_(0),
     symbols_(NULL),
     local_symbol_offset_(0),
-    local_values_(),
-    local_indexes_()
+    local_values_()
 {
 }
 
@@ -210,7 +228,7 @@ Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
   // Get the symbol table section header.
   typename This::Shdr symtabshdr(pshdrs
                                 + this->symtab_shndx_ * This::shdr_size);
-  assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
+  gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
 
   // We only need the external symbols.
   const int sym_size = This::sym_size;
@@ -221,7 +239,7 @@ Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
   off_t extsize = symtabshdr.get_sh_size() - locsize;
 
   // Read the symbol table.
-  File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
+  File_view* fvsymtab = this->get_lasting_view(extoff, extsize, false);
 
   // Read the section header for the symbol names.
   unsigned int strtab_shndx = symtabshdr.get_sh_link();
@@ -243,7 +261,7 @@ Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
 
   // Read the symbol names.
   File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
-                                              strtabshdr.get_sh_size());
+                                              strtabshdr.get_sh_size(), true);
 
   sd->symbols = fvsymtab;
   sd->symbols_size = extsize;
@@ -267,7 +285,7 @@ Sized_relobj<size, big_endian>::include_section_group(
 {
   // Read the section contents.
   const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
-                                            shdr.get_sh_size());
+                                            shdr.get_sh_size(), false);
   const elfcpp::Elf_Word* pword =
     reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
 
@@ -296,13 +314,14 @@ Sized_relobj<size, big_endian>::include_section_group(
       gold_exit(false);
     }
   off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
-  const unsigned char* psym = this->get_view(symoff, This::sym_size);
+  const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
   elfcpp::Sym<size, big_endian> sym(psym);
 
   // Read the symbol table names.
   off_t symnamelen;
   const unsigned char* psymnamesu;
-  psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen);
+  psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
+                                     true);
   const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
 
   // Get the section group signature.
@@ -388,8 +407,7 @@ Sized_relobj<size, big_endian>::include_linkonce_section(
 
 template<int size, bool big_endian>
 void
-Sized_relobj<size, big_endian>::do_layout(const General_options& options,
-                                         Symbol_table* symtab,
+Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
                                          Layout* layout,
                                          Read_symbols_data* sd)
 {
@@ -429,7 +447,7 @@ Sized_relobj<size, big_endian>::do_layout(const General_options& options,
 
       if (this->handle_gnu_warning_section(name, i, symtab))
        {
-         if (!options.is_relocatable())
+         if (!parameters->output_is_object())
            omit[i] = true;
        }
 
@@ -441,7 +459,8 @@ Sized_relobj<size, big_endian>::do_layout(const General_options& options,
              if (!this->include_section_group(layout, i, shdr, &omit))
                discard = true;
            }
-         else if (Layout::is_linkonce(name))
+          else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
+                   && Layout::is_linkonce(name))
            {
              if (!this->include_linkonce_section(layout, name, shdr))
                discard = true;
@@ -477,7 +496,7 @@ Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
 {
   if (sd->symbols == NULL)
     {
-      assert(sd->symbol_names == NULL);
+      gold_assert(sd->symbol_names == NULL);
       return;
     }
 
@@ -495,7 +514,7 @@ Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
 
   const char* sym_names =
     reinterpret_cast<const char*>(sd->symbol_names->data());
-  symtab->add_from_relobj(this, sd->symbols->data(), symcount, sym_names, 
+  symtab->add_from_relobj(this, sd->symbols->data(), symcount, sym_names,
                          sd->symbol_names_size, this->symbols_);
 
   delete sd->symbols;
@@ -506,10 +525,9 @@ Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
 
 // Finalize the local symbols.  Here we record the file offset at
 // which they should be output, we add their names to *POOL, and we
-// add their values to THIS->LOCAL_VALUES_ and their indexes in the
-// output symbol table to THIS->LOCAL_INDEXES_.  Return the symbol
-// index.  This function is always called from the main thread.  The
-// actual output of the local symbols will occur in a separate task.
+// add their values to THIS->LOCAL_VALUES_.  Return the symbol index.
+// This function is always called from the main thread.  The actual
+// output of the local symbols will occur in a separate task.
 
 template<int size, bool big_endian>
 unsigned int
@@ -517,14 +535,14 @@ Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
                                                          off_t off,
                                                          Stringpool* pool)
 {
-  assert(this->symtab_shndx_ != -1U);
+  gold_assert(this->symtab_shndx_ != -1U);
   if (this->symtab_shndx_ == 0)
     {
       // This object has no symbols.  Weird but legal.
       return index;
     }
 
-  assert(off == static_cast<off_t>(align_address(off, size >> 3)));
+  gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
 
   this->local_symbol_offset_ = off;
 
@@ -532,24 +550,24 @@ Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
   const unsigned int symtab_shndx = this->symtab_shndx_;
   typename This::Shdr symtabshdr(this,
                                 this->elf_file_.section_header(symtab_shndx));
-  assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
+  gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
 
   // Read the local symbols.
   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;
   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
-                                             locsize);
+                                             locsize, true);
 
   this->local_values_.resize(loccount);
-  this->local_indexes_.resize(loccount);
 
   // Read the symbol names.
   const unsigned int strtab_shndx = symtabshdr.get_sh_link();
   off_t strtab_size;
   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
-                                                       &strtab_size);
+                                                       &strtab_size,
+                                                       true);
   const char* pnames = reinterpret_cast<const char*>(pnamesu);
 
   // Loop over the local symbols.
@@ -563,12 +581,15 @@ Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
     {
       elfcpp::Sym<size, big_endian> sym(psyms);
 
+      Symbol_value<size>& lv(this->local_values_[i]);
+
       unsigned int shndx = sym.get_st_shndx();
+      lv.set_input_shndx(shndx);
 
       if (shndx >= elfcpp::SHN_LORESERVE)
        {
          if (shndx == elfcpp::SHN_ABS)
-           this->local_values_[i] = sym.get_st_value();
+           lv.set_output_value(sym.get_st_value());
          else
            {
              // FIXME: Handle SHN_XINDEX.
@@ -590,23 +611,28 @@ Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
              gold_exit(false);
            }
 
-         if (mo[shndx].output_section == NULL)
+         Output_section* os = mo[shndx].output_section;
+
+         if (os == NULL)
            {
-             this->local_values_[i] = 0;
-             this->local_indexes_[i] = -1U;
+             lv.set_output_value(0);
+             lv.set_no_output_symtab_entry();
              continue;
            }
 
-         this->local_values_[i] = (mo[shndx].output_section->address()
-                                   + mo[shndx].offset
-                                   + sym.get_st_value());
+         if (mo[shndx].offset == -1)
+           lv.set_input_value(sym.get_st_value());
+         else
+           lv.set_output_value(mo[shndx].output_section->address()
+                               + mo[shndx].offset
+                               + sym.get_st_value());
        }
 
       // Decide whether this symbol should go into the output file.
 
       if (sym.get_st_type() == elfcpp::STT_SECTION)
        {
-         this->local_indexes_[i] = -1U;
+         lv.set_no_output_symtab_entry();
          continue;
        }
 
@@ -623,9 +649,8 @@ Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
 
       const char* name = pnames + sym.get_st_name();
       pool->add(name, NULL);
-      this->local_indexes_[i] = index;
+      lv.set_output_symtab_index(index);
       ++index;
-      off += sym_size;
       ++count;
     }
 
@@ -634,6 +659,23 @@ Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
   return index;
 }
 
+// Return the value of a local symbol defined in input section SHNDX,
+// with value VALUE, adding addend ADDEND.  This handles SHF_MERGE
+// sections.
+template<int size, bool big_endian>
+typename elfcpp::Elf_types<size>::Elf_Addr
+Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
+                                           Address value,
+                                           Address addend) const
+{
+  const std::vector<Map_to_output>& mo(this->map_to_output());
+  Output_section* os = mo[shndx].output_section;
+  if (os == NULL)
+    return addend;
+  gold_assert(mo[shndx].offset == -1);
+  return os->output_address(this, shndx, value + addend);
+}
+
 // Write out the local symbols.
 
 template<int size, bool big_endian>
@@ -641,7 +683,7 @@ void
 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
                                                    const Stringpool* sympool)
 {
-  assert(this->symtab_shndx_ != -1U);
+  gold_assert(this->symtab_shndx_ != -1U);
   if (this->symtab_shndx_ == 0)
     {
       // This object has no symbols.  Weird but legal.
@@ -652,21 +694,22 @@ Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
   const unsigned int symtab_shndx = this->symtab_shndx_;
   typename This::Shdr symtabshdr(this,
                                 this->elf_file_.section_header(symtab_shndx));
-  assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
+  gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
   const unsigned int loccount = this->local_symbol_count_;
-  assert(loccount == symtabshdr.get_sh_info());
+  gold_assert(loccount == symtabshdr.get_sh_info());
 
   // Read the local symbols.
   const int sym_size = This::sym_size;
   off_t locsize = loccount * sym_size;
   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
-                                             locsize);
+                                             locsize, false);
 
   // Read the symbol names.
   const unsigned int strtab_shndx = symtabshdr.get_sh_link();
   off_t strtab_size;
   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
-                                                       &strtab_size);
+                                                       &strtab_size,
+                                                       true);
   const char* pnames = reinterpret_cast<const char*>(pnamesu);
 
   // Get a view into the output file.
@@ -676,8 +719,7 @@ Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
 
   const std::vector<Map_to_output>& mo(this->map_to_output());
 
-  assert(this->local_values_.size() == loccount);
-  assert(this->local_indexes_.size() == loccount);
+  gold_assert(this->local_values_.size() == loccount);
 
   unsigned char* ov = oview;
   psyms += sym_size;
@@ -685,14 +727,13 @@ Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
     {
       elfcpp::Sym<size, big_endian> isym(psyms);
 
-      if (this->local_indexes_[i] == -1U)
+      if (!this->local_values_[i].needs_output_symtab_entry())
        continue;
-      assert(this->local_indexes_[i] != 0);
 
       unsigned int st_shndx = isym.get_st_shndx();
       if (st_shndx < elfcpp::SHN_LORESERVE)
        {
-         assert(st_shndx < mo.size());
+         gold_assert(st_shndx < mo.size());
          if (mo[st_shndx].output_section == NULL)
            continue;
          st_shndx = mo[st_shndx].output_section->out_shndx();
@@ -700,10 +741,10 @@ Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
 
       elfcpp::Sym_write<size, big_endian> osym(ov);
 
-      assert(isym.get_st_name() < strtab_size);
+      gold_assert(isym.get_st_name() < strtab_size);
       const char* name = pnames + isym.get_st_name();
       osym.put_st_name(sympool->get_offset(name));
-      osym.put_st_value(this->local_values_[i]);
+      osym.put_st_value(this->local_values_[i].value(this, 0));
       osym.put_st_size(isym.get_st_size());
       osym.put_st_info(isym.get_st_info());
       osym.put_st_other(isym.get_st_other());
@@ -712,22 +753,36 @@ Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
       ov += sym_size;
     }
 
-  assert(ov - oview == output_size);
+  gold_assert(ov - oview == output_size);
 
   of->write_output_view(this->local_symbol_offset_, output_size, oview);
 }
 
 // Input_objects methods.
 
-// Add a regular relocatable object to the list.
+// Add a regular relocatable object to the list.  Return false if this
+// object should be ignored.
 
-void
+bool
 Input_objects::add_object(Object* obj)
 {
-  if (obj->is_dynamic())
-    this->dynobj_list_.push_back(static_cast<Dynobj*>(obj));
-  else
+  if (!obj->is_dynamic())
     this->relobj_list_.push_back(static_cast<Relobj*>(obj));
+  else
+    {
+      // See if this is a duplicate SONAME.
+      Dynobj* dynobj = static_cast<Dynobj*>(obj);
+
+      std::pair<Unordered_set<std::string>::iterator, bool> ins =
+       this->sonames_.insert(dynobj->soname());
+      if (!ins.second)
+       {
+         // We have already seen a dynamic object with this soname.
+         return false;
+       }
+
+      this->dynobj_list_.push_back(dynobj);
+    }
 
   Target* target = obj->target();
   if (this->target_ == NULL)
@@ -738,6 +793,11 @@ Input_objects::add_object(Object* obj)
              program_name, obj->name().c_str());
       gold_exit(false);
     }
+
+  set_parameters_size_and_endianness(target->get_size(),
+                                    target->is_big_endian());
+
+  return true;
 }
 
 // Relocate_info methods.
@@ -875,15 +935,29 @@ make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
        }
       if (big_endian)
        {
+#ifdef HAVE_TARGET_32_BIG
          elfcpp::Ehdr<32, true> ehdr(p);
          return make_elf_sized_object<32, true>(name, input_file,
                                                 offset, ehdr);
+#else
+          fprintf(stderr,
+                  _("%s: %s: not configured to support 32-bit big-endian object\n"),
+                  program_name, name.c_str());
+          gold_exit(false);
+#endif
        }
       else
        {
+#ifdef HAVE_TARGET_32_LITTLE
          elfcpp::Ehdr<32, false> ehdr(p);
          return make_elf_sized_object<32, false>(name, input_file,
                                                  offset, ehdr);
+#else
+          fprintf(stderr,
+                  _("%s: %s: not configured to support 32-bit little-endian object\n"),
+                  program_name, name.c_str());
+          gold_exit(false);
+#endif
        }
     }
   else
@@ -896,15 +970,29 @@ make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
        }
       if (big_endian)
        {
+#ifdef HAVE_TARGET_64_BIG
          elfcpp::Ehdr<64, true> ehdr(p);
          return make_elf_sized_object<64, true>(name, input_file,
                                                 offset, ehdr);
+#else
+          fprintf(stderr,
+                  _("%s: %s: not configured to support 64-bit big-endian object\n"),
+                  program_name, name.c_str());
+          gold_exit(false);
+#endif
        }
       else
        {
+#ifdef HAVE_TARGET_64_LITTLE
          elfcpp::Ehdr<64, false> ehdr(p);
          return make_elf_sized_object<64, false>(name, input_file,
                                                  offset, ehdr);
+#else
+          fprintf(stderr,
+                  _("%s: %s: not configured to support 64-bit little-endian object\n"),
+                  program_name, name.c_str());
+          gold_exit(false);
+#endif
        }
     }
 }
@@ -912,28 +1000,44 @@ make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
 // 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
 class Sized_relobj<32, false>;
+#endif
 
+#ifdef HAVE_TARGET_32_BIG
 template
 class Sized_relobj<32, true>;
+#endif
 
+#ifdef HAVE_TARGET_64_LITTLE
 template
 class Sized_relobj<64, false>;
+#endif
 
+#ifdef HAVE_TARGET_64_BIG
 template
 class Sized_relobj<64, true>;
+#endif
 
+#ifdef HAVE_TARGET_32_LITTLE
 template
 struct Relocate_info<32, false>;
+#endif
 
+#ifdef HAVE_TARGET_32_BIG
 template
 struct Relocate_info<32, true>;
+#endif
 
+#ifdef HAVE_TARGET_64_LITTLE
 template
 struct Relocate_info<64, false>;
+#endif
 
+#ifdef HAVE_TARGET_64_BIG
 template
 struct Relocate_info<64, true>;
+#endif
 
 } // End namespace gold.
This page took 0.03025 seconds and 4 git commands to generate.