* options.cc: Include "demangle.h".
[deliverable/binutils-gdb.git] / gold / output.cc
index f0f4de26fdf2cff749b94646b5e78357d709ea6b..85cc2b257be06181fb9fe2bdbf0ea8a6a8012beb 100644 (file)
@@ -1,6 +1,6 @@
 // output.cc -- manage the output file for gold
 
-// Copyright 2006, 2007 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of gold.
@@ -61,7 +61,8 @@ Output_data::~Output_data()
 uint64_t
 Output_data::default_alignment()
 {
-  return Output_data::default_alignment_for_size(parameters->get_size());
+  return Output_data::default_alignment_for_size(
+      parameters->target().get_size());
 }
 
 // Return the default alignment for a size--32 or 64.
@@ -83,23 +84,36 @@ Output_data::default_alignment_for_size(int size)
 Output_section_headers::Output_section_headers(
     const Layout* layout,
     const Layout::Segment_list* segment_list,
+    const Layout::Section_list* section_list,
     const Layout::Section_list* unattached_section_list,
     const Stringpool* secnamepool)
   : layout_(layout),
     segment_list_(segment_list),
+    section_list_(section_list),
     unattached_section_list_(unattached_section_list),
     secnamepool_(secnamepool)
 {
   // Count all the sections.  Start with 1 for the null section.
   off_t count = 1;
-  for (Layout::Segment_list::const_iterator p = segment_list->begin();
-       p != segment_list->end();
-       ++p)
-    if ((*p)->type() == elfcpp::PT_LOAD)
-      count += (*p)->output_section_count();
+  if (!parameters->options().relocatable())
+    {
+      for (Layout::Segment_list::const_iterator p = segment_list->begin();
+          p != segment_list->end();
+          ++p)
+       if ((*p)->type() == elfcpp::PT_LOAD)
+         count += (*p)->output_section_count();
+    }
+  else
+    {
+      for (Layout::Section_list::const_iterator p = section_list->begin();
+          p != section_list->end();
+          ++p)
+       if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
+         ++count;
+    }
   count += unattached_section_list->size();
 
-  const int size = parameters->get_size();
+  const int size = parameters->target().get_size();
   int shdr_size;
   if (size == 32)
     shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
@@ -116,46 +130,31 @@ Output_section_headers::Output_section_headers(
 void
 Output_section_headers::do_write(Output_file* of)
 {
-  if (parameters->get_size() == 32)
+  switch (parameters->size_and_endianness())
     {
-      if (parameters->is_big_endian())
-       {
-#ifdef HAVE_TARGET_32_BIG
-         this->do_sized_write<32, true>(of);
-#else
-         gold_unreachable();
-#endif
-       }
-      else
-       {
 #ifdef HAVE_TARGET_32_LITTLE
-         this->do_sized_write<32, false>(of);
-#else
-         gold_unreachable();
+    case Parameters::TARGET_32_LITTLE:
+      this->do_sized_write<32, false>(of);
+      break;
 #endif
-       }
-    }
-  else if (parameters->get_size() == 64)
-    {
-      if (parameters->is_big_endian())
-       {
-#ifdef HAVE_TARGET_64_BIG
-         this->do_sized_write<64, true>(of);
-#else
-         gold_unreachable();
+#ifdef HAVE_TARGET_32_BIG
+    case Parameters::TARGET_32_BIG:
+      this->do_sized_write<32, true>(of);
+      break;
 #endif
-       }
-      else
-       {
 #ifdef HAVE_TARGET_64_LITTLE
-         this->do_sized_write<64, false>(of);
-#else
-         gold_unreachable();
+    case Parameters::TARGET_64_LITTLE:
+      this->do_sized_write<64, false>(of);
+      break;
 #endif
-       }
+#ifdef HAVE_TARGET_64_BIG
+    case Parameters::TARGET_64_BIG:
+      this->do_sized_write<64, true>(of);
+      break;
+#endif
+    default:
+      gold_unreachable();
     }
-  else
-    gold_unreachable();
 }
 
 template<int size, bool big_endian>
@@ -184,18 +183,48 @@ Output_section_headers::do_sized_write(Output_file* of)
 
   v += shdr_size;
 
-  unsigned shndx = 1;
-  for (Layout::Segment_list::const_iterator p = this->segment_list_->begin();
-       p != this->segment_list_->end();
-       ++p)
-    v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
-           this->layout_, this->secnamepool_, v, &shndx
-           SELECT_SIZE_ENDIAN(size, big_endian));
+  unsigned int shndx = 1;
+  if (!parameters->options().relocatable())
+    {
+      for (Layout::Segment_list::const_iterator p =
+            this->segment_list_->begin();
+          p != this->segment_list_->end();
+          ++p)
+       v = (*p)->write_section_headers<size, big_endian>(this->layout_,
+                                                         this->secnamepool_,
+                                                         v,
+                                                         &shndx);
+    }
+  else
+    {
+      for (Layout::Section_list::const_iterator p =
+            this->section_list_->begin();
+          p != this->section_list_->end();
+          ++p)
+       {
+         // We do unallocated sections below, except that group
+         // sections have to come first.
+         if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
+             && (*p)->type() != elfcpp::SHT_GROUP)
+           continue;
+         gold_assert(shndx == (*p)->out_shndx());
+         elfcpp::Shdr_write<size, big_endian> oshdr(v);
+         (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
+         v += shdr_size;
+         ++shndx;
+       }
+    }
+
   for (Layout::Section_list::const_iterator p =
         this->unattached_section_list_->begin();
        p != this->unattached_section_list_->end();
        ++p)
     {
+      // For a relocatable link, we did unallocated group sections
+      // above, since they have to come first.
+      if ((*p)->type() == elfcpp::SHT_GROUP
+         && parameters->options().relocatable())
+       continue;
       gold_assert(shndx == (*p)->out_shndx());
       elfcpp::Shdr_write<size, big_endian> oshdr(v);
       (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
@@ -212,7 +241,7 @@ Output_segment_headers::Output_segment_headers(
     const Layout::Segment_list& segment_list)
   : segment_list_(segment_list)
 {
-  const int size = parameters->get_size();
+  const int size = parameters->target().get_size();
   int phdr_size;
   if (size == 32)
     phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
@@ -227,46 +256,31 @@ Output_segment_headers::Output_segment_headers(
 void
 Output_segment_headers::do_write(Output_file* of)
 {
-  if (parameters->get_size() == 32)
+  switch (parameters->size_and_endianness())
     {
-      if (parameters->is_big_endian())
-       {
-#ifdef HAVE_TARGET_32_BIG
-         this->do_sized_write<32, true>(of);
-#else
-         gold_unreachable();
-#endif
-       }
-      else
-       {
 #ifdef HAVE_TARGET_32_LITTLE
-       this->do_sized_write<32, false>(of);
-#else
-       gold_unreachable();
+    case Parameters::TARGET_32_LITTLE:
+      this->do_sized_write<32, false>(of);
+      break;
 #endif
-       }
-    }
-  else if (parameters->get_size() == 64)
-    {
-      if (parameters->is_big_endian())
-       {
-#ifdef HAVE_TARGET_64_BIG
-         this->do_sized_write<64, true>(of);
-#else
-         gold_unreachable();
+#ifdef HAVE_TARGET_32_BIG
+    case Parameters::TARGET_32_BIG:
+      this->do_sized_write<32, true>(of);
+      break;
 #endif
-       }
-      else
-       {
 #ifdef HAVE_TARGET_64_LITTLE
-         this->do_sized_write<64, false>(of);
-#else
-         gold_unreachable();
+    case Parameters::TARGET_64_LITTLE:
+      this->do_sized_write<64, false>(of);
+      break;
 #endif
-       }
+#ifdef HAVE_TARGET_64_BIG
+    case Parameters::TARGET_64_BIG:
+      this->do_sized_write<64, true>(of);
+      break;
+#endif
+    default:
+      gold_unreachable();
     }
-  else
-    gold_unreachable();
 }
 
 template<int size, bool big_endian>
@@ -306,7 +320,7 @@ Output_file_header::Output_file_header(const Target* target,
     shstrtab_(NULL),
     entry_(entry)
 {
-  const int size = parameters->get_size();
+  const int size = parameters->target().get_size();
   int ehdr_size;
   if (size == 32)
     ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
@@ -335,46 +349,31 @@ Output_file_header::do_write(Output_file* of)
 {
   gold_assert(this->offset() == 0);
 
-  if (parameters->get_size() == 32)
+  switch (parameters->size_and_endianness())
     {
-      if (parameters->is_big_endian())
-       {
-#ifdef HAVE_TARGET_32_BIG
-         this->do_sized_write<32, true>(of);
-#else
-         gold_unreachable();
-#endif
-       }
-      else
-       {
 #ifdef HAVE_TARGET_32_LITTLE
-         this->do_sized_write<32, false>(of);
-#else
-         gold_unreachable();
+    case Parameters::TARGET_32_LITTLE:
+      this->do_sized_write<32, false>(of);
+      break;
 #endif
-       }
-    }
-  else if (parameters->get_size() == 64)
-    {
-      if (parameters->is_big_endian())
-       {
-#ifdef HAVE_TARGET_64_BIG
-         this->do_sized_write<64, true>(of);
-#else
-         gold_unreachable();
+#ifdef HAVE_TARGET_32_BIG
+    case Parameters::TARGET_32_BIG:
+      this->do_sized_write<32, true>(of);
+      break;
 #endif
-       }
-      else
-       {
 #ifdef HAVE_TARGET_64_LITTLE
-         this->do_sized_write<64, false>(of);
-#else
-         gold_unreachable();
+    case Parameters::TARGET_64_LITTLE:
+      this->do_sized_write<64, false>(of);
+      break;
 #endif
-       }
+#ifdef HAVE_TARGET_64_BIG
+    case Parameters::TARGET_64_BIG:
+      this->do_sized_write<64, true>(of);
+      break;
+#endif
+    default:
+      gold_unreachable();
     }
-  else
-    gold_unreachable();
 }
 
 // Write out the file header with appropriate size and endianess.
@@ -409,9 +408,9 @@ Output_file_header::do_sized_write(Output_file* of)
   oehdr.put_e_ident(e_ident);
 
   elfcpp::ET e_type;
-  if (parameters->output_is_object())
+  if (parameters->options().relocatable())
     e_type = elfcpp::ET_REL;
-  else if (parameters->output_is_shared())
+  else if (parameters->options().shared())
     e_type = elfcpp::ET_DYN;
   else
     e_type = elfcpp::ET_EXEC;
@@ -422,16 +421,30 @@ Output_file_header::do_sized_write(Output_file* of)
 
   oehdr.put_e_entry(this->entry<size>());
 
-  oehdr.put_e_phoff(this->segment_header_->offset());
+  if (this->segment_header_ == NULL)
+    oehdr.put_e_phoff(0);
+  else
+    oehdr.put_e_phoff(this->segment_header_->offset());
+
   oehdr.put_e_shoff(this->section_header_->offset());
 
   // FIXME: The target needs to set the flags.
   oehdr.put_e_flags(0);
 
   oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
-  oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
-  oehdr.put_e_phnum(this->segment_header_->data_size()
-                    / elfcpp::Elf_sizes<size>::phdr_size);
+
+  if (this->segment_header_ == NULL)
+    {
+      oehdr.put_e_phentsize(0);
+      oehdr.put_e_phnum(0);
+    }
+  else
+    {
+      oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
+      oehdr.put_e_phnum(this->segment_header_->data_size()
+                       / elfcpp::Elf_sizes<size>::phdr_size);
+    }
+
   oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
   oehdr.put_e_shnum(this->section_header_->data_size()
                     / elfcpp::Elf_sizes<size>::shdr_size);
@@ -448,7 +461,8 @@ typename elfcpp::Elf_types<size>::Elf_Addr
 Output_file_header::entry()
 {
   const bool should_issue_warning = (this->entry_ != NULL
-                                    && parameters->output_is_executable());
+                                    && !parameters->options().relocatable()
+                                     && !parameters->options().shared());
 
   // FIXME: Need to support target specific entry symbol.
   const char* entry = this->entry_;
@@ -551,12 +565,14 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
     Address address,
     bool is_relative)
   : address_(address), local_sym_index_(GSYM_CODE), type_(type),
-    is_relative_(is_relative), shndx_(INVALID_CODE)
+    is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
 {
+  // this->type_ is a bitfield; make sure TYPE fits.
+  gold_assert(this->type_ == type);
   this->u1_.gsym = gsym;
   this->u2_.od = od;
-  if (dynamic && !is_relative)
-    gsym->set_needs_dynsym_entry();
+  if (dynamic)
+    this->set_needs_dynsym_index();
 }
 
 template<bool dynamic, int size, bool big_endian>
@@ -568,13 +584,15 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
     Address address,
     bool is_relative)
   : address_(address), local_sym_index_(GSYM_CODE), type_(type),
-    is_relative_(is_relative), shndx_(shndx)
+    is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
 {
   gold_assert(shndx != INVALID_CODE);
+  // this->type_ is a bitfield; make sure TYPE fits.
+  gold_assert(this->type_ == type);
   this->u1_.gsym = gsym;
   this->u2_.relobj = relobj;
-  if (dynamic && !is_relative)
-    gsym->set_needs_dynsym_entry();
+  if (dynamic)
+    this->set_needs_dynsym_index();
 }
 
 // A reloc against a local symbol.
@@ -586,16 +604,20 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
     unsigned int type,
     Output_data* od,
     Address address,
-    bool is_relative)
+    bool is_relative,
+    bool is_section_symbol)
   : address_(address), local_sym_index_(local_sym_index), type_(type),
-    is_relative_(is_relative), shndx_(INVALID_CODE)
+    is_relative_(is_relative), is_section_symbol_(is_section_symbol),
+    shndx_(INVALID_CODE)
 {
   gold_assert(local_sym_index != GSYM_CODE
               && local_sym_index != INVALID_CODE);
+  // this->type_ is a bitfield; make sure TYPE fits.
+  gold_assert(this->type_ == type);
   this->u1_.relobj = relobj;
   this->u2_.od = od;
-  if (dynamic && !is_relative)
-    relobj->set_needs_output_dynsym_entry(local_sym_index);
+  if (dynamic)
+    this->set_needs_dynsym_index();
 }
 
 template<bool dynamic, int size, bool big_endian>
@@ -605,17 +627,21 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
     unsigned int type,
     unsigned int shndx,
     Address address,
-    bool is_relative)
+    bool is_relative,
+    bool is_section_symbol)
   : address_(address), local_sym_index_(local_sym_index), type_(type),
-    is_relative_(is_relative), shndx_(shndx)
+    is_relative_(is_relative), is_section_symbol_(is_section_symbol),
+    shndx_(shndx)
 {
   gold_assert(local_sym_index != GSYM_CODE
               && local_sym_index != INVALID_CODE);
   gold_assert(shndx != INVALID_CODE);
+  // this->type_ is a bitfield; make sure TYPE fits.
+  gold_assert(this->type_ == type);
   this->u1_.relobj = relobj;
   this->u2_.relobj = relobj;
-  if (dynamic && !is_relative)
-    relobj->set_needs_output_dynsym_entry(local_sym_index);
+  if (dynamic)
+    this->set_needs_dynsym_index();
 }
 
 // A reloc against the STT_SECTION symbol of an output section.
@@ -627,12 +653,16 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
     Output_data* od,
     Address address)
   : address_(address), local_sym_index_(SECTION_CODE), type_(type),
-    is_relative_(false), shndx_(INVALID_CODE)
+    is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
 {
+  // this->type_ is a bitfield; make sure TYPE fits.
+  gold_assert(this->type_ == type);
   this->u1_.os = os;
   this->u2_.od = od;
   if (dynamic)
-    os->set_needs_dynsym_index();
+    this->set_needs_dynsym_index();
+  else
+    os->set_needs_symtab_index();
 }
 
 template<bool dynamic, int size, bool big_endian>
@@ -643,13 +673,59 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
     unsigned int shndx,
     Address address)
   : address_(address), local_sym_index_(SECTION_CODE), type_(type),
-    is_relative_(false), shndx_(shndx)
+    is_relative_(false), is_section_symbol_(true), shndx_(shndx)
 {
   gold_assert(shndx != INVALID_CODE);
+  // this->type_ is a bitfield; make sure TYPE fits.
+  gold_assert(this->type_ == type);
   this->u1_.os = os;
   this->u2_.relobj = relobj;
   if (dynamic)
-    os->set_needs_dynsym_index();
+    this->set_needs_dynsym_index();
+  else
+    os->set_needs_symtab_index();
+}
+
+// Record that we need a dynamic symbol index for this relocation.
+
+template<bool dynamic, int size, bool big_endian>
+void
+Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
+set_needs_dynsym_index()
+{
+  if (this->is_relative_)
+    return;
+  switch (this->local_sym_index_)
+    {
+    case INVALID_CODE:
+      gold_unreachable();
+
+    case GSYM_CODE:
+      this->u1_.gsym->set_needs_dynsym_entry();
+      break;
+
+    case SECTION_CODE:
+      this->u1_.os->set_needs_dynsym_index();
+      break;
+
+    case 0:
+      break;
+
+    default:
+      {
+        const unsigned int lsi = this->local_sym_index_;
+        if (!this->is_section_symbol_)
+          this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
+        else
+          {
+            section_offset_type dummy;
+            Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
+            gold_assert(os != NULL);
+            os->set_needs_dynsym_index();
+          }
+      }
+      break;
+    }
 }
 
 // Get the symbol index of a relocation.
@@ -687,16 +763,47 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
       break;
 
     default:
-      if (dynamic)
-        index = this->u1_.relobj->dynsym_index(this->local_sym_index_);
-      else
-       index = this->u1_.relobj->symtab_index(this->local_sym_index_);
+      {
+        const unsigned int lsi = this->local_sym_index_;
+        if (!this->is_section_symbol_)
+          {
+            if (dynamic)
+              index = this->u1_.relobj->dynsym_index(lsi);
+            else
+              index = this->u1_.relobj->symtab_index(lsi);
+          }
+        else
+          {
+            section_offset_type dummy;
+            Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
+            gold_assert(os != NULL);
+            if (dynamic)
+              index = os->dynsym_index();
+            else
+              index = os->symtab_index();
+          }
+      }
       break;
     }
   gold_assert(index != -1U);
   return index;
 }
 
+// For a local section symbol, get the section offset of the input
+// section within the output section.
+
+template<bool dynamic, int size, bool big_endian>
+section_offset_type
+Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
+  local_section_offset() const
+{
+  const unsigned int lsi = this->local_sym_index_;
+  section_offset_type offset;
+  Output_section* os = this->u1_.relobj->output_section(lsi, &offset);
+  gold_assert(os != NULL && offset != -1);
+  return offset;
+}
+
 // Write out the offset and info fields of a Rel or Rela relocation
 // entry.
 
@@ -744,18 +851,21 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
 
 template<bool dynamic, int size, bool big_endian>
 typename elfcpp::Elf_types<size>::Elf_Addr
-Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value() const
+Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
+    Address addend) const
 {
   if (this->local_sym_index_ == GSYM_CODE)
     {
       const Sized_symbol<size>* sym;
       sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
-      return sym->value();
+      return sym->value() + addend;
     }
   gold_assert(this->local_sym_index_ != SECTION_CODE
-              && this->local_sym_index_ != INVALID_CODE);
-  const Sized_relobj<size, big_endian>* relobj = this->u1_.relobj;
-  return relobj->local_symbol_value(this->local_sym_index_);
+              && this->local_sym_index_ != INVALID_CODE
+              && !this->is_section_symbol_);
+  const unsigned int lsi = this->local_sym_index_;
+  const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
+  return symval->value(this->u1_.relobj, addend);
 }
 
 // Write out a Rela relocation.
@@ -768,8 +878,10 @@ Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
   elfcpp::Rela_write<size, big_endian> orel(pov);
   this->rel_.write_rel(&orel);
   Addend addend = this->addend_;
-  if (rel_.is_relative())
-    addend += rel_.symbol_value();
+  if (this->rel_.is_relative())
+    addend = this->rel_.symbol_value(addend);
+  else if (this->rel_.is_local_section_symbol())
+    addend += this->rel_.local_section_offset();
   orel.put_r_addend(addend);
 }
 
@@ -822,6 +934,80 @@ Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
   this->relocs_.clear();
 }
 
+// Class Output_relocatable_relocs.
+
+template<int sh_type, int size, bool big_endian>
+void
+Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
+{
+  this->set_data_size(this->rr_->output_reloc_count()
+                     * Reloc_types<sh_type, size, big_endian>::reloc_size);
+}
+
+// class Output_data_group.
+
+template<int size, bool big_endian>
+Output_data_group<size, big_endian>::Output_data_group(
+    Sized_relobj<size, big_endian>* relobj,
+    section_size_type entry_count,
+    const elfcpp::Elf_Word* contents)
+  : Output_section_data(entry_count * 4, 4),
+    relobj_(relobj)
+{
+  this->flags_ = elfcpp::Swap<32, big_endian>::readval(contents);
+  for (section_size_type i = 1; i < entry_count; ++i)
+    {
+      unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
+      this->input_sections_.push_back(shndx);
+    }
+}
+
+// Write out the section group, which means translating the section
+// indexes to apply to the output file.
+
+template<int size, bool big_endian>
+void
+Output_data_group<size, big_endian>::do_write(Output_file* of)
+{
+  const off_t off = this->offset();
+  const section_size_type oview_size =
+    convert_to_section_size_type(this->data_size());
+  unsigned char* const oview = of->get_output_view(off, oview_size);
+
+  elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
+  elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
+  ++contents;
+
+  for (std::vector<unsigned int>::const_iterator p =
+        this->input_sections_.begin();
+       p != this->input_sections_.end();
+       ++p, ++contents)
+    {
+      section_offset_type dummy;
+      Output_section* os = this->relobj_->output_section(*p, &dummy);
+
+      unsigned int output_shndx;
+      if (os != NULL)
+       output_shndx = os->out_shndx();
+      else
+       {
+         this->relobj_->error(_("section group retained but "
+                                "group element discarded"));
+         output_shndx = 0;
+       }
+
+      elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
+    }
+
+  size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
+  gold_assert(wrote == oview_size);
+
+  of->write_output_view(off, oview_size, oview);
+
+  // We no longer need this information.
+  this->input_sections_.clear();
+}
+
 // Output_data_got::Got_entry methods.
 
 // Write out the entry.
@@ -854,7 +1040,11 @@ Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
       break;
 
     default:
-      val = this->u_.object->local_symbol_value(this->local_sym_index_);
+      {
+        const unsigned int lsi = this->local_sym_index_;
+        const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
+        val = symval->value(this->u_.object, 0);
+      }
       break;
     }
 
@@ -1198,8 +1388,7 @@ template<int size, bool big_endian>
 void
 Output_data_dynamic::Dynamic_entry::write(
     unsigned char* pov,
-    const Stringpool* pool
-    ACCEPT_SIZE_ENDIAN) const
+    const Stringpool* pool) const
 {
   typename elfcpp::Elf_types<size>::Elf_WXword val;
   switch (this->classification_)
@@ -1244,9 +1433,9 @@ Output_data_dynamic::Dynamic_entry::write(
 void
 Output_data_dynamic::do_adjust_output_section(Output_section* os)
 {
-  if (parameters->get_size() == 32)
+  if (parameters->target().get_size() == 32)
     os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
-  else if (parameters->get_size() == 64)
+  else if (parameters->target().get_size() == 64)
     os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
   else
     gold_unreachable();
@@ -1261,9 +1450,9 @@ Output_data_dynamic::set_final_data_size()
   this->add_constant(elfcpp::DT_NULL, 0);
 
   int dyn_size;
-  if (parameters->get_size() == 32)
+  if (parameters->target().get_size() == 32)
     dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
-  else if (parameters->get_size() == 64)
+  else if (parameters->target().get_size() == 64)
     dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
   else
     gold_unreachable();
@@ -1275,46 +1464,31 @@ Output_data_dynamic::set_final_data_size()
 void
 Output_data_dynamic::do_write(Output_file* of)
 {
-  if (parameters->get_size() == 32)
+  switch (parameters->size_and_endianness())
     {
-      if (parameters->is_big_endian())
-       {
-#ifdef HAVE_TARGET_32_BIG
-         this->sized_write<32, true>(of);
-#else
-         gold_unreachable();
-#endif
-       }
-      else
-       {
 #ifdef HAVE_TARGET_32_LITTLE
-         this->sized_write<32, false>(of);
-#else
-         gold_unreachable();
+    case Parameters::TARGET_32_LITTLE:
+      this->sized_write<32, false>(of);
+      break;
 #endif
-       }
-    }
-  else if (parameters->get_size() == 64)
-    {
-      if (parameters->is_big_endian())
-       {
-#ifdef HAVE_TARGET_64_BIG
-         this->sized_write<64, true>(of);
-#else
-         gold_unreachable();
+#ifdef HAVE_TARGET_32_BIG
+    case Parameters::TARGET_32_BIG:
+      this->sized_write<32, true>(of);
+      break;
 #endif
-       }
-      else
-       {
 #ifdef HAVE_TARGET_64_LITTLE
-         this->sized_write<64, false>(of);
-#else
-         gold_unreachable();
+    case Parameters::TARGET_64_LITTLE:
+      this->sized_write<64, false>(of);
+      break;
 #endif
-       }
+#ifdef HAVE_TARGET_64_BIG
+    case Parameters::TARGET_64_BIG:
+      this->sized_write<64, true>(of);
+      break;
+#endif
+    default:
+      gold_unreachable();
     }
-  else
-    gold_unreachable();
 }
 
 template<int size, bool big_endian>
@@ -1332,8 +1506,7 @@ Output_data_dynamic::sized_write(Output_file* of)
        p != this->entries_.end();
        ++p)
     {
-      p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
-          pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
+      p->write<size, big_endian>(pov, this->pool_);
       pov += dyn_size;
     }
 
@@ -1460,6 +1633,7 @@ Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
     link_section_(NULL),
     link_(0),
     info_section_(NULL),
+    info_symndx_(NULL),
     info_(0),
     type_(type),
     flags_(flags),
@@ -1478,6 +1652,7 @@ Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
     requires_postprocessing_(false),
     found_in_sections_clause_(false),
     has_load_address_(false),
+    info_uses_section_index_(false),
     tls_offset_(0)
 {
   // An unallocated section has no address.  Forcing this means that
@@ -1871,7 +2046,12 @@ Output_section::write_header(const Layout* layout,
 {
   oshdr->put_sh_name(secnamepool->get_offset(this->name_));
   oshdr->put_sh_type(this->type_);
-  oshdr->put_sh_flags(this->flags_);
+
+  elfcpp::Elf_Xword flags = this->flags_;
+  if (this->info_section_ != NULL && this->info_uses_section_index_)
+    flags |= elfcpp::SHF_INFO_LINK;
+  oshdr->put_sh_flags(flags);
+
   oshdr->put_sh_addr(this->address());
   oshdr->put_sh_offset(this->offset());
   oshdr->put_sh_size(this->data_size());
@@ -1883,10 +2063,21 @@ Output_section::write_header(const Layout* layout,
     oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
   else
     oshdr->put_sh_link(this->link_);
+
+  elfcpp::Elf_Word info;
   if (this->info_section_ != NULL)
-    oshdr->put_sh_info(this->info_section_->out_shndx());
+    {
+      if (this->info_uses_section_index_)
+       info = this->info_section_->out_shndx();
+      else
+       info = this->info_section_->symtab_index();
+    }
+  else if (this->info_symndx_ != NULL)
+    info = this->info_symndx_->symtab_index();
   else
-    oshdr->put_sh_info(this->info_);
+    info = this->info_;
+  oshdr->put_sh_info(info);
+
   oshdr->put_sh_addralign(this->addralign_);
   oshdr->put_sh_entsize(this->entsize_);
 }
@@ -1905,7 +2096,7 @@ Output_section::do_write(Output_file* of)
        p != this->fills_.end();
        ++p)
     {
-      std::string fill_data(parameters->target()->code_fill(p->length()));
+      std::string fill_data(parameters->target().code_fill(p->length()));
       of->write(output_section_file_offset + p->section_offset(),
                fill_data.data(), fill_data.size());
     }
@@ -1954,13 +2145,12 @@ Output_section::write_to_postprocessing_buffer()
 {
   gold_assert(this->requires_postprocessing());
 
-  Target* target = parameters->target();
   unsigned char* buffer = this->postprocessing_buffer();
   for (Fill_list::iterator p = this->fills_.begin();
        p != this->fills_.end();
        ++p)
     {
-      std::string fill_data(target->code_fill(p->length()));
+      std::string fill_data(parameters->target().code_fill(p->length()));
       memcpy(buffer + p->section_offset(), fill_data.data(),
             fill_data.size());
     }
@@ -2193,6 +2383,27 @@ Output_segment::add_output_section(Output_section* os,
     pdl->push_back(os);
 }
 
+// Remove an Output_section from this segment.  It is an error if it
+// is not present.
+
+void
+Output_segment::remove_output_section(Output_section* os)
+{
+  // We only need this for SHT_PROGBITS.
+  gold_assert(os->type() == elfcpp::SHT_PROGBITS);
+  for (Output_data_list::iterator p = this->output_data_.begin();
+       p != this->output_data_.end();
+       ++p)
+   {
+     if (*p == os)
+       {
+         this->output_data_.erase(p);
+         return;
+       }
+   }
+  gold_unreachable();
+}
+
 // Add an Output_data (which is not an Output_section) to the start of
 // a segment.
 
@@ -2272,7 +2483,8 @@ Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
 // *POFF and *PSHNDX.
 
 uint64_t
-Output_segment::set_section_addresses(bool reset, uint64_t addr, off_t* poff,
+Output_segment::set_section_addresses(const Layout* layout, bool reset,
+                                      uint64_t addr, off_t* poff,
                                      unsigned int* pshndx)
 {
   gold_assert(this->type_ == elfcpp::PT_LOAD);
@@ -2289,17 +2501,31 @@ Output_segment::set_section_addresses(bool reset, uint64_t addr, off_t* poff,
       this->are_addresses_set_ = true;
     }
 
+  bool in_tls = false;
+
   off_t orig_off = *poff;
   this->offset_ = orig_off;
 
-  addr = this->set_section_list_addresses(reset, &this->output_data_,
-                                         addr, poff, pshndx);
+  addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
+                                         addr, poff, pshndx, &in_tls);
   this->filesz_ = *poff - orig_off;
 
   off_t off = *poff;
 
-  uint64_t ret = this->set_section_list_addresses(reset, &this->output_bss_,
-                                                 addr, poff, pshndx);
+  uint64_t ret = this->set_section_list_addresses(layout, reset,
+                                                  &this->output_bss_,
+                                                 addr, poff, pshndx,
+                                                  &in_tls);
+
+  // If the last section was a TLS section, align upward to the
+  // alignment of the TLS segment, so that the overall size of the TLS
+  // segment is aligned.
+  if (in_tls)
+    {
+      uint64_t segment_align = layout->tls_segment()->maximum_alignment();
+      *poff = align_address(*poff, segment_align);
+    }
+
   this->memsz_ = *poff - orig_off;
 
   // Ignore the file offset adjustments made by the BSS Output_data
@@ -2313,9 +2539,11 @@ Output_segment::set_section_addresses(bool reset, uint64_t addr, off_t* poff,
 // structures.
 
 uint64_t
-Output_segment::set_section_list_addresses(bool reset, Output_data_list* pdl,
+Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
+                                           Output_data_list* pdl,
                                           uint64_t addr, off_t* poff,
-                                          unsigned int* pshndx)
+                                          unsigned int* pshndx,
+                                           bool* in_tls)
 {
   off_t startoff = *poff;
 
@@ -2331,7 +2559,42 @@ Output_segment::set_section_list_addresses(bool reset, Output_data_list* pdl,
       // already have an address.
       if (!(*p)->is_address_valid())
        {
-         off = align_address(off, (*p)->addralign());
+          uint64_t align = (*p)->addralign();
+
+          if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
+            {
+              // Give the first TLS section the alignment of the
+              // entire TLS segment.  Otherwise the TLS segment as a
+              // whole may be misaligned.
+              if (!*in_tls)
+                {
+                  Output_segment* tls_segment = layout->tls_segment();
+                  gold_assert(tls_segment != NULL);
+                  uint64_t segment_align = tls_segment->maximum_alignment();
+                  gold_assert(segment_align >= align);
+                  align = segment_align;
+
+                  *in_tls = true;
+                }
+            }
+          else
+            {
+              // If this is the first section after the TLS segment,
+              // align it to at least the alignment of the TLS
+              // segment, so that the size of the overall TLS segment
+              // is aligned.
+              if (*in_tls)
+                {
+                  uint64_t segment_align =
+                      layout->tls_segment()->maximum_alignment();
+                  if (segment_align > align)
+                    align = segment_align;
+
+                  *in_tls = false;
+                }
+            }
+
+         off = align_address(off, align);
          (*p)->set_address_and_file_offset(addr + (off - startoff), off);
        }
       else
@@ -2344,11 +2607,10 @@ Output_segment::set_section_list_addresses(bool reset, Output_data_list* pdl,
          (*p)->finalize_data_size();
        }
 
-      // Unless this is a PT_TLS segment, we want to ignore the size
-      // of a SHF_TLS/SHT_NOBITS section.  Such a section does not
-      // affect the size of a PT_LOAD segment.
-      if (this->type_ == elfcpp::PT_TLS
-         || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
+      // We want to ignore the size of a SHF_TLS or SHT_NOBITS
+      // section.  Such a section does not affect the size of a
+      // PT_LOAD segment.
+      if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
          || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
        off += (*p)->data_size();
 
@@ -2415,6 +2677,16 @@ Output_segment::set_offset()
   this->memsz_ = (last->address()
                  + last->data_size()
                  - this->vaddr_);
+
+  // If this is a TLS segment, align the memory size.  The code in
+  // set_section_list ensures that the section after the TLS segment
+  // is aligned to give us room.
+  if (this->type_ == elfcpp::PT_TLS)
+    {
+      uint64_t segment_align = this->maximum_alignment();
+      gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
+      this->memsz_ = align_address(this->memsz_, segment_align);
+    }
 }
 
 // Set the TLS offsets of the sections in the PT_TLS segment.
@@ -2552,8 +2824,7 @@ unsigned char*
 Output_segment::write_section_headers(const Layout* layout,
                                      const Stringpool* secnamepool,
                                      unsigned char* v,
-                                     unsigned int *pshndx
-                                      ACCEPT_SIZE_ENDIAN) const
+                                     unsigned int *pshndx) const
 {
   // Every section that is attached to a segment must be attached to a
   // PT_LOAD segment, so we only write out section headers for PT_LOAD
@@ -2561,14 +2832,12 @@ Output_segment::write_section_headers(const Layout* layout,
   if (this->type_ != elfcpp::PT_LOAD)
     return v;
 
-  v = this->write_section_headers_list
-      SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
-         layout, secnamepool, &this->output_data_, v, pshndx
-          SELECT_SIZE_ENDIAN(size, big_endian));
-  v = this->write_section_headers_list
-      SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
-          layout, secnamepool, &this->output_bss_, v, pshndx
-          SELECT_SIZE_ENDIAN(size, big_endian));
+  v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
+                                                        &this->output_data_,
+                                                        v, pshndx);
+  v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
+                                                        &this->output_bss_,
+                                                        v, pshndx);
   return v;
 }
 
@@ -2578,8 +2847,7 @@ Output_segment::write_section_headers_list(const Layout* layout,
                                           const Stringpool* secnamepool,
                                           const Output_data_list* pdl,
                                           unsigned char* v,
-                                          unsigned int* pshndx
-                                           ACCEPT_SIZE_ENDIAN) const
+                                          unsigned int* pshndx) const
 {
   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
   for (Output_data_list::const_iterator p = pdl->begin();
@@ -2606,7 +2874,8 @@ Output_file::Output_file(const char* name)
     o_(-1),
     file_size_(0),
     base_(NULL),
-    map_is_anonymous_(false)
+    map_is_anonymous_(false),
+    is_temporary_(false)
 {
 }
 
@@ -2631,19 +2900,22 @@ Output_file::open(off_t file_size)
   // to improve the odds for open().
 
   // We let the name "-" mean "stdout"
-  if (strcmp(this->name_, "-") == 0)
-    this->o_ = STDOUT_FILENO;
-  else
+  if (!this->is_temporary_)
     {
-      struct stat s;
-      if (::stat(this->name_, &s) == 0 && s.st_size != 0)
-        unlink_if_ordinary(this->name_);
-
-      int mode = parameters->output_is_object() ? 0666 : 0777;
-      int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
-      if (o < 0)
-        gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
-      this->o_ = o;
+      if (strcmp(this->name_, "-") == 0)
+       this->o_ = STDOUT_FILENO;
+      else
+       {
+         struct stat s;
+         if (::stat(this->name_, &s) == 0 && s.st_size != 0)
+           unlink_if_ordinary(this->name_);
+
+         int mode = parameters->options().relocatable() ? 0666 : 0777;
+         int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
+         if (o < 0)
+           gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
+         this->o_ = o;
+       }
     }
 
   this->map();
@@ -2688,7 +2960,8 @@ Output_file::map()
   struct stat statbuf;
   if (o == STDOUT_FILENO || o == STDERR_FILENO
       || ::fstat(o, &statbuf) != 0
-      || !S_ISREG(statbuf.st_mode))
+      || !S_ISREG(statbuf.st_mode)
+      || this->is_temporary_)
     {
       this->map_is_anonymous_ = true;
       base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
@@ -2729,7 +3002,7 @@ void
 Output_file::close()
 {
   // If the map isn't file-backed, we need to write it now.
-  if (this->map_is_anonymous_)
+  if (this->map_is_anonymous_ && !this->is_temporary_)
     {
       size_t bytes_to_write = this->file_size_;
       while (bytes_to_write > 0)
@@ -2746,7 +3019,9 @@ Output_file::close()
   this->unmap();
 
   // We don't close stdout or stderr
-  if (this->o_ != STDOUT_FILENO && this->o_ != STDERR_FILENO)
+  if (this->o_ != STDOUT_FILENO
+      && this->o_ != STDERR_FILENO
+      && !this->is_temporary_)
     if (::close(this->o_) < 0)
       gold_error(_("%s: close: %s"), this->name_, strerror(errno));
   this->o_ = -1;
@@ -2883,6 +3158,66 @@ template
 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
 #endif
 
+#ifdef HAVE_TARGET_32_LITTLE
+template
+class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
+#endif
+
+#ifdef HAVE_TARGET_32_LITTLE
+template
+class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
+#endif
+
+#ifdef HAVE_TARGET_32_LITTLE
+template
+class Output_data_group<32, false>;
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+class Output_data_group<32, true>;
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+class Output_data_group<64, false>;
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+class Output_data_group<64, true>;
+#endif
+
 #ifdef HAVE_TARGET_32_LITTLE
 template
 class Output_data_got<32, false>;
This page took 0.037116 seconds and 4 git commands to generate.