From Andrew Chatham: add signedrel<>, rel32s and friends.
[deliverable/binutils-gdb.git] / gold / object.cc
index 1bfd9698e06f3c98827bef16ce3048522b3299d1..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,7 +147,7 @@ Sized_relobj<size, big_endian>::Sized_relobj(
     output_local_symbol_count_(0),
     symbols_(NULL),
     local_symbol_offset_(0),
-    values_(NULL)
+    local_values_()
 {
 }
 
@@ -209,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;
@@ -220,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();
@@ -242,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;
@@ -266,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);
 
@@ -295,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.
@@ -387,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)
 {
@@ -428,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;
        }
 
@@ -440,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;
@@ -476,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;
     }
 
@@ -494,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;
@@ -505,23 +525,24 @@ 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->VALUES_.  Return the new file offset.
+// 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>
-off_t
-Sized_relobj<size, big_endian>::do_finalize_local_symbols(off_t off,
+unsigned int
+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 off;
+      return index;
     }
 
-  off = align_address(off, size >> 3);
+  gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
 
   this->local_symbol_offset_ = off;
 
@@ -529,28 +550,29 @@ Sized_relobj<size, big_endian>::do_finalize_local_symbols(off_t off,
   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->values_ = new typename elfcpp::Elf_types<size>::Elf_Addr[loccount];
+  this->local_values_.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.
 
-  std::vector<Map_to_output>& mo(this->map_to_output());
+  const std::vector<Map_to_output>& mo(this->map_to_output());
   unsigned int shnum = this->shnum();
   unsigned int count = 0;
   // Skip the first, dummy, symbol.
@@ -559,12 +581,15 @@ Sized_relobj<size, big_endian>::do_finalize_local_symbols(off_t off,
     {
       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->values_[i] = sym.get_st_value();
+           lv.set_output_value(sym.get_st_value());
          else
            {
              // FIXME: Handle SHN_XINDEX.
@@ -586,39 +611,69 @@ Sized_relobj<size, big_endian>::do_finalize_local_symbols(off_t off,
              gold_exit(false);
            }
 
-         if (mo[shndx].output_section == NULL)
+         Output_section* os = mo[shndx].output_section;
+
+         if (os == NULL)
            {
-             this->values_[i] = 0;
+             lv.set_output_value(0);
+             lv.set_no_output_symtab_entry();
              continue;
            }
 
-         this->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());
        }
 
-      if (sym.get_st_type() != elfcpp::STT_SECTION)
+      // Decide whether this symbol should go into the output file.
+
+      if (sym.get_st_type() == elfcpp::STT_SECTION)
        {
-         if (sym.get_st_name() >= strtab_size)
-           {
-             fprintf(stderr,
-                     _("%s: %s: local symbol %u section name "
-                       "out of range: %u >= %u\n"),
-                     program_name, this->name().c_str(),
-                     i, sym.get_st_name(),
-                     static_cast<unsigned int>(strtab_size));
-             gold_exit(false);
-           }
+         lv.set_no_output_symtab_entry();
+         continue;
+       }
 
-         pool->add(pnames + sym.get_st_name(), NULL);
-         off += sym_size;
-         ++count;
+      if (sym.get_st_name() >= strtab_size)
+       {
+         fprintf(stderr,
+                 _("%s: %s: local symbol %u section name "
+                   "out of range: %u >= %u\n"),
+                 program_name, this->name().c_str(),
+                 i, sym.get_st_name(),
+                 static_cast<unsigned int>(strtab_size));
+         gold_exit(false);
        }
+
+      const char* name = pnames + sym.get_st_name();
+      pool->add(name, NULL);
+      lv.set_output_symtab_index(index);
+      ++index;
+      ++count;
     }
 
   this->output_local_symbol_count_ = count;
 
-  return off;
+  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.
@@ -628,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.
@@ -639,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.
@@ -661,21 +717,23 @@ Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
   unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
                                             output_size);
 
-  std::vector<Map_to_output>& mo(this->map_to_output());
+  const std::vector<Map_to_output>& mo(this->map_to_output());
+
+  gold_assert(this->local_values_.size() == loccount);
 
-  psyms += sym_size;
   unsigned char* ov = oview;
+  psyms += sym_size;
   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
     {
       elfcpp::Sym<size, big_endian> isym(psyms);
 
-      if (isym.get_st_type() == elfcpp::STT_SECTION)
+      if (!this->local_values_[i].needs_output_symtab_entry())
        continue;
 
       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();
@@ -683,9 +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);
-      osym.put_st_name(sympool->get_offset(pnames + isym.get_st_name()));
-      osym.put_st_value(this->values_[i]);
+      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].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());
@@ -694,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)
@@ -720,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.
@@ -857,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
@@ -878,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
        }
     }
 }
@@ -894,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.029808 seconds and 4 git commands to generate.