* Makefile.am ($(srcdir)/ia64-asmtab.c): Remove line continuation.
[deliverable/binutils-gdb.git] / gold / output.cc
index 7a76f79377c129743980d547b6fb8d6cf68f1a93..145fca159df5f00e0fb4bc7c3e6af3c592b9d5ac 100644 (file)
@@ -37,6 +37,7 @@
 #include "symtab.h"
 #include "reloc.h"
 #include "merge.h"
+#include "descriptors.h"
 #include "output.h"
 
 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
@@ -87,12 +88,14 @@ Output_section_headers::Output_section_headers(
     const Layout::Segment_list* segment_list,
     const Layout::Section_list* section_list,
     const Layout::Section_list* unattached_section_list,
-    const Stringpool* secnamepool)
+    const Stringpool* secnamepool,
+    const Output_section* shstrtab_section)
   : layout_(layout),
     segment_list_(segment_list),
     section_list_(section_list),
     unattached_section_list_(unattached_section_list),
-    secnamepool_(secnamepool)
+    secnamepool_(secnamepool),
+    shstrtab_section_(shstrtab_section)
 {
   // Count all the sections.  Start with 1 for the null section.
   off_t count = 1;
@@ -175,8 +178,20 @@ Output_section_headers::do_sized_write(Output_file* of)
     oshdr.put_sh_flags(0);
     oshdr.put_sh_addr(0);
     oshdr.put_sh_offset(0);
-    oshdr.put_sh_size(0);
-    oshdr.put_sh_link(0);
+
+    size_t section_count = (this->data_size()
+                           / elfcpp::Elf_sizes<size>::shdr_size);
+    if (section_count < elfcpp::SHN_LORESERVE)
+      oshdr.put_sh_size(0);
+    else
+      oshdr.put_sh_size(section_count);
+
+    unsigned int shstrndx = this->shstrtab_section_->out_shndx();
+    if (shstrndx < elfcpp::SHN_LORESERVE)
+      oshdr.put_sh_link(0);
+    else
+      oshdr.put_sh_link(shstrndx);
+
     oshdr.put_sh_info(0);
     oshdr.put_sh_addralign(0);
     oshdr.put_sh_entsize(0);
@@ -447,9 +462,20 @@ Output_file_header::do_sized_write(Output_file* of)
     }
 
   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);
-  oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
+  size_t section_count = (this->section_header_->data_size()
+                         / elfcpp::Elf_sizes<size>::shdr_size);
+
+  if (section_count < elfcpp::SHN_LORESERVE)
+    oehdr.put_e_shnum(this->section_header_->data_size()
+                     / elfcpp::Elf_sizes<size>::shdr_size);
+  else
+    oehdr.put_e_shnum(0);
+
+  unsigned int shstrndx = this->shstrtab_->out_shndx();
+  if (shstrndx < elfcpp::SHN_LORESERVE)
+    oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
+  else
+    oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
 
   of->write_output_view(0, ehdr_size, view);
 }
@@ -535,6 +561,18 @@ Output_section_data::do_out_shndx() const
   return this->output_section_->out_shndx();
 }
 
+// Set the alignment, which means we may need to update the alignment
+// of the output section.
+
+void
+Output_section_data::set_addralign(uint64_t addralign)
+{
+  this->addralign_ = addralign;
+  if (this->output_section_ != NULL
+      && this->output_section_->addralign() < addralign)
+    this->output_section_->set_addralign(addralign);
+}
+
 // Output_data_strtab methods.
 
 // Set the final data size.
@@ -580,7 +618,7 @@ template<bool dynamic, int size, bool big_endian>
 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
     Symbol* gsym,
     unsigned int type,
-    Relobj* relobj,
+    Sized_relobj<size, big_endian>* relobj,
     unsigned int shndx,
     Address address,
     bool is_relative)
@@ -670,7 +708,7 @@ template<bool dynamic, int size, bool big_endian>
 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
     Output_section* os,
     unsigned int type,
-    Relobj* relobj,
+    Sized_relobj<size, big_endian>* relobj,
     unsigned int shndx,
     Address address)
   : address_(address), local_sym_index_(SECTION_CODE), type_(type),
@@ -718,12 +756,7 @@ set_needs_dynsym_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();
-          }
+          this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
       }
       break;
     }
@@ -775,8 +808,7 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
           }
         else
           {
-            section_offset_type dummy;
-            Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
+            Output_section* os = this->u1_.relobj->output_section(lsi);
             gold_assert(os != NULL);
             if (dynamic)
               index = os->dynsym_index();
@@ -794,7 +826,7 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
 // within the input section.
 
 template<bool dynamic, int size, bool big_endian>
-section_offset_type
+typename elfcpp::Elf_types<size>::Elf_Addr
 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
   local_section_offset(Addend addend) const
 {
@@ -803,34 +835,30 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
               && this->local_sym_index_ != INVALID_CODE
               && this->is_section_symbol_);
   const unsigned int lsi = this->local_sym_index_;
-  section_offset_type offset;
-  Output_section* os = this->u1_.relobj->output_section(lsi, &offset);
+  Output_section* os = this->u1_.relobj->output_section(lsi);
   gold_assert(os != NULL);
-  if (offset != -1)
+  Address offset = this->u1_.relobj->get_output_section_offset(lsi);
+  if (offset != -1U)
     return offset + addend;
   // This is a merge section.
   offset = os->output_address(this->u1_.relobj, lsi, addend);
-  gold_assert(offset != -1);
+  gold_assert(offset != -1U);
   return offset;
 }
 
-// Write out the offset and info fields of a Rel or Rela relocation
-// entry.
+// Get the output address of a relocation.
 
 template<bool dynamic, int size, bool big_endian>
-template<typename Write_rel>
-void
-Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
-    Write_rel* wr) const
+typename elfcpp::Elf_types<size>::Elf_Addr
+Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
 {
   Address address = this->address_;
   if (this->shndx_ != INVALID_CODE)
     {
-      section_offset_type off;
-      Output_section* os = this->u2_.relobj->output_section(this->shndx_,
-                                                           &off);
+      Output_section* os = this->u2_.relobj->output_section(this->shndx_);
       gold_assert(os != NULL);
-      if (off != -1)
+      Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
+      if (off != -1U)
        address += os->address() + off;
       else
        {
@@ -841,7 +869,19 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
     }
   else if (this->u2_.od != NULL)
     address += this->u2_.od->address();
-  wr->put_r_offset(address);
+  return address;
+}
+
+// Write out the offset and info fields of a Rel or Rela relocation
+// entry.
+
+template<bool dynamic, int size, bool big_endian>
+template<typename Write_rel>
+void
+Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
+    Write_rel* wr) const
+{
+  wr->put_r_offset(this->get_address());
   unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
   wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
 }
@@ -878,6 +918,57 @@ Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
   return symval->value(this->u1_.relobj, addend);
 }
 
+// Reloc comparison.  This function sorts the dynamic relocs for the
+// benefit of the dynamic linker.  First we sort all relative relocs
+// to the front.  Among relative relocs, we sort by output address.
+// Among non-relative relocs, we sort by symbol index, then by output
+// address.
+
+template<bool dynamic, int size, bool big_endian>
+int
+Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
+  compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
+    const
+{
+  if (this->is_relative_)
+    {
+      if (!r2.is_relative_)
+       return -1;
+      // Otherwise sort by reloc address below.
+    }
+  else if (r2.is_relative_)
+    return 1;
+  else
+    {
+      unsigned int sym1 = this->get_symbol_index();
+      unsigned int sym2 = r2.get_symbol_index();
+      if (sym1 < sym2)
+       return -1;
+      else if (sym1 > sym2)
+       return 1;
+      // Otherwise sort by reloc address.
+    }
+
+  section_offset_type addr1 = this->get_address();
+  section_offset_type addr2 = r2.get_address();
+  if (addr1 < addr2)
+    return -1;
+  else if (addr1 > addr2)
+    return 1;
+
+  // Final tie breaker, in order to generate the same output on any
+  // host: reloc type.
+  unsigned int type1 = this->type_;
+  unsigned int type2 = r2.type_;
+  if (type1 < type2)
+    return -1;
+  else if (type1 > type2)
+    return 1;
+
+  // These relocs appear to be exactly the same.
+  return 0;
+}
+
 // Write out a Rela relocation.
 
 template<bool dynamic, int size, bool big_endian>
@@ -927,6 +1018,13 @@ Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
   const off_t oview_size = this->data_size();
   unsigned char* const oview = of->get_output_view(off, oview_size);
 
+  if (this->sort_relocs_)
+    {
+      gold_assert(dynamic);
+      std::sort(this->relocs_.begin(), this->relocs_.end(),
+               Sort_relocs_comparison());
+    }
+
   unsigned char* pov = oview;
   for (typename Relocs::const_iterator p = this->relocs_.begin();
        p != this->relocs_.end();
@@ -960,16 +1058,13 @@ 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)
+    elfcpp::Elf_Word flags,
+    std::vector<unsigned int>* input_shndxes)
   : Output_section_data(entry_count * 4, 4),
-    relobj_(relobj)
+    relobj_(relobj),
+    flags_(flags)
 {
-  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);
-    }
+  this->input_shndxes_.swap(*input_shndxes);
 }
 
 // Write out the section group, which means translating the section
@@ -989,12 +1084,11 @@ Output_data_group<size, big_endian>::do_write(Output_file* of)
   ++contents;
 
   for (std::vector<unsigned int>::const_iterator p =
-        this->input_sections_.begin();
-       p != this->input_sections_.end();
+        this->input_shndxes_.begin();
+       p != this->input_shndxes_.end();
        ++p, ++contents)
     {
-      section_offset_type dummy;
-      Output_section* os = this->relobj_->output_section(*p, &dummy);
+      Output_section* os = this->relobj_->output_section(*p);
 
       unsigned int output_shndx;
       if (os != NULL)
@@ -1015,7 +1109,7 @@ Output_data_group<size, big_endian>::do_write(Output_file* of)
   of->write_output_view(off, oview_size, oview);
 
   // We no longer need this information.
-  this->input_sections_.clear();
+  this->input_shndxes_.clear();
 }
 
 // Output_data_got::Got_entry methods.
@@ -1257,8 +1351,7 @@ Output_data_got<size, big_endian>::add_local_pair_with_rel(
   this->entries_.push_back(Got_entry());
   unsigned int got_offset = this->last_got_offset();
   object->set_local_got_offset(symndx, got_type, got_offset);
-  section_offset_type off;
-  Output_section* os = object->output_section(shndx, &off);
+  Output_section* os = object->output_section(shndx);
   rel_dyn->add_output_section(os, r_type_1, this, got_offset);
 
   this->entries_.push_back(Got_entry(object, symndx));
@@ -1288,8 +1381,7 @@ Output_data_got<size, big_endian>::add_local_pair_with_rela(
   this->entries_.push_back(Got_entry());
   unsigned int got_offset = this->last_got_offset();
   object->set_local_got_offset(symndx, got_type, got_offset);
-  section_offset_type off;
-  Output_section* os = object->output_section(shndx, &off);
+  Output_section* os = object->output_section(shndx);
   rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
 
   this->entries_.push_back(Got_entry(object, symndx));
@@ -1342,16 +1434,12 @@ Output_data_dynamic::Dynamic_entry::write(
     const Stringpool* pool) const
 {
   typename elfcpp::Elf_types<size>::Elf_WXword val;
-  switch (this->classification_)
+  switch (this->offset_)
     {
     case DYNAMIC_NUMBER:
       val = this->u_.val;
       break;
 
-    case DYNAMIC_SECTION_ADDRESS:
-      val = this->u_.od->address();
-      break;
-
     case DYNAMIC_SECTION_SIZE:
       val = this->u_.od->data_size();
       break;
@@ -1369,7 +1457,8 @@ Output_data_dynamic::Dynamic_entry::write(
       break;
 
     default:
-      gold_unreachable();
+      val = this->u_.od->address() + this->offset_;
+      break;
     }
 
   elfcpp::Dyn_write<size, big_endian> dw(pov);
@@ -1469,6 +1558,38 @@ Output_data_dynamic::sized_write(Output_file* of)
   this->entries_.clear();
 }
 
+// Class Output_symtab_xindex.
+
+void
+Output_symtab_xindex::do_write(Output_file* of)
+{
+  const off_t offset = this->offset();
+  const off_t oview_size = this->data_size();
+  unsigned char* const oview = of->get_output_view(offset, oview_size);
+
+  memset(oview, 0, oview_size);
+
+  if (parameters->target().is_big_endian())
+    this->endian_do_write<true>(oview);
+  else
+    this->endian_do_write<false>(oview);
+
+  of->write_output_view(offset, oview_size, oview);
+
+  // We no longer need the data.
+  this->entries_.clear();
+}
+
+template<bool big_endian>
+void
+Output_symtab_xindex::endian_do_write(unsigned char* const oview)
+{
+  for (Xindex_entries::const_iterator p = this->entries_.begin();
+       p != this->entries_.end();
+       ++p)
+    elfcpp::Swap<32, big_endian>::writeval(oview + p->first * 4, p->second);
+}
+
 // Output_section::Input_section methods.
 
 // Return the data size.  For an input section we store the size here.
@@ -1571,6 +1692,25 @@ Output_section::Input_section::write_to_buffer(unsigned char* buffer)
     this->u2_.posd->write_to_buffer(buffer);
 }
 
+// Print to a map file.
+
+void
+Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
+{
+  switch (this->shndx_)
+    {
+    case OUTPUT_SECTION_CODE:
+    case MERGE_DATA_SECTION_CODE:
+    case MERGE_STRING_SECTION_CODE:
+      this->u2_.posd->print_to_mapfile(mapfile);
+      break;
+
+    default:
+      mapfile->print_input_section(this->u2_.object, this->shndx_);
+      break;
+    }
+}
+
 // Output_section methods.
 
 // Construct an Output_section.  NAME will point into a Stringpool.
@@ -1607,6 +1747,8 @@ Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
     may_sort_attached_input_sections_(false),
     must_sort_attached_input_sections_(false),
     attached_input_sections_are_sorted_(false),
+    is_relro_(false),
+    is_relro_local_(false),
     tls_offset_(0)
 {
   // An unallocated section has no address.  Forcing this means that
@@ -1664,10 +1806,7 @@ Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
     this->addralign_ = addralign;
 
   typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
-  this->flags_ |= (sh_flags
-                  & (elfcpp::SHF_WRITE
-                     | elfcpp::SHF_ALLOC
-                     | elfcpp::SHF_EXECINSTR));
+  this->update_flags_for_input_section(sh_flags);
 
   uint64_t entsize = shdr.get_sh_entsize();
 
@@ -1681,9 +1820,11 @@ Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
 
   // If this is a SHF_MERGE section, we pass all the input sections to
   // a Output_data_merge.  We don't try to handle relocations for such
-  // a section.
+  // a section.  We don't try to handle empty merge sections--they
+  // mess up the mappings, and are useless anyhow.
   if ((sh_flags & elfcpp::SHF_MERGE) != 0
-      && reloc_shndx == 0)
+      && reloc_shndx == 0
+      && shdr.get_sh_size() > 0)
     {
       if (this->add_merge_input_section(object, shndx, sh_flags,
                                        entsize, addralign))
@@ -1730,7 +1871,8 @@ Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
   if (have_sections_script
       || !this->input_sections_.empty()
       || this->may_sort_attached_input_sections()
-      || this->must_sort_attached_input_sections())
+      || this->must_sort_attached_input_sections()
+      || parameters->options().user_set_Map())
     this->input_sections_.push_back(Input_section(object, shndx,
                                                  shdr.get_sh_size(),
                                                  addralign));
@@ -1846,8 +1988,6 @@ Output_section::is_input_address_mapped(const Relobj* object,
                                        unsigned int shndx,
                                        off_t offset) const
 {
-  gold_assert(object->is_section_specially_mapped(shndx));
-
   for (Input_section_list::const_iterator p = this->input_sections_.begin();
        p != this->input_sections_.end();
        ++p)
@@ -1872,7 +2012,6 @@ section_offset_type
 Output_section::output_offset(const Relobj* object, unsigned int shndx,
                              section_offset_type offset) const
 {
-  gold_assert(object->is_section_specially_mapped(shndx));
   // This can only be called meaningfully when layout is complete.
   gold_assert(Output_data::is_layout_complete());
 
@@ -1894,8 +2033,6 @@ uint64_t
 Output_section::output_address(const Relobj* object, unsigned int shndx,
                               off_t offset) const
 {
-  gold_assert(object->is_section_specially_mapped(shndx));
-
   uint64_t addr = this->address() + this->first_input_offset_;
   for (Input_section_list::const_iterator p = this->input_sections_.begin();
        p != this->input_sections_.end();
@@ -1927,8 +2064,6 @@ uint64_t
 Output_section::starting_output_address(const Relobj* object,
                                        unsigned int shndx) const
 {
-  gold_assert(object->is_section_specially_mapped(shndx));
-
   uint64_t addr = this->address() + this->first_input_offset_;
   for (Input_section_list::const_iterator p = this->input_sections_.begin();
        p != this->input_sections_.end();
@@ -2417,6 +2552,19 @@ Output_section::add_input_section_for_script(Relobj* object,
                                                data_size, addralign));
 }
 
+// Print to the map file.
+
+void
+Output_section::do_print_to_mapfile(Mapfile* mapfile) const
+{
+  mapfile->print_output_section(this);
+
+  for (Input_section_list::const_iterator p = this->input_sections_.begin();
+       p != this->input_sections_.end();
+       ++p)
+    p->print_to_mapfile(mapfile);
+}
+
 // Print stats for merge sections to stderr.
 
 void
@@ -2452,8 +2600,7 @@ Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
 
 void
 Output_segment::add_output_section(Output_section* os,
-                                  elfcpp::Elf_Word seg_flags,
-                                  bool front)
+                                  elfcpp::Elf_Word seg_flags)
 {
   gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
   gold_assert(!this->is_max_align_known_);
@@ -2488,7 +2635,6 @@ Output_segment::add_output_section(Output_section* os,
          --p;
          if ((*p)->is_section_type(elfcpp::SHT_NOTE))
            {
-             // We don't worry about the FRONT parameter.
              ++p;
              pdl->insert(p, os);
              return;
@@ -2520,7 +2666,7 @@ Output_segment::add_output_section(Output_section* os,
            {
              sawtls = true;
              // Put a NOBITS section after the first TLS section.
-             // But a PROGBITS section after the first TLS/PROGBITS
+             // Put a PROGBITS section after the first TLS/PROGBITS
              // section.
              insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
            }
@@ -2533,7 +2679,6 @@ Output_segment::add_output_section(Output_section* os,
 
          if (insert)
            {
-             // We don't worry about the FRONT parameter.
              ++p;
              pdl->insert(p, os);
              return;
@@ -2545,10 +2690,29 @@ Output_segment::add_output_section(Output_section* os,
       // location in the section list.
     }
 
-  if (front)
-    pdl->push_front(os);
-  else
-    pdl->push_back(os);
+  // For the PT_GNU_RELRO segment, we need to group relro sections,
+  // and we need to put them before any non-relro sections.  Also,
+  // relro local sections go before relro non-local sections.
+  if (parameters->options().relro() && os->is_relro())
+    {
+      gold_assert(pdl == &this->output_data_);
+      Output_segment::Output_data_list::iterator p;
+      for (p = pdl->begin(); p != pdl->end(); ++p)
+       {
+         if (!(*p)->is_section())
+           break;
+
+         Output_section* pos = (*p)->output_section();
+         if (!pos->is_relro()
+             || (os->is_relro_local() && !pos->is_relro_local()))
+           break;
+       }
+
+      pdl->insert(p, os);
+      return;
+    }
+
+  pdl->push_back(os);
 }
 
 // Remove an Output_section from this segment.  It is an error if it
@@ -2582,6 +2746,16 @@ Output_segment::add_initial_output_data(Output_data* od)
   this->output_data_.push_front(od);
 }
 
+// Return whether the first data section is a relro section.
+
+bool
+Output_segment::is_first_section_relro() const
+{
+  return (!this->output_data_.empty()
+         && this->output_data_.front()->is_section()
+         && this->output_data_.front()->output_section()->is_relro());
+}
+
 // Return the maximum alignment of the Output_data in Output_segment.
 
 uint64_t
@@ -2599,6 +2773,17 @@ Output_segment::maximum_alignment()
       if (addralign > this->max_align_)
        this->max_align_ = addralign;
 
+      // If -z relro is in effect, and the first section in this
+      // segment is a relro section, then the segment must be aligned
+      // to at least the common page size.  This ensures that the
+      // PT_GNU_RELRO segment will start at a page boundary.
+      if (parameters->options().relro() && this->is_first_section_relro())
+       {
+         addralign = parameters->target().common_pagesize();
+         if (addralign > this->max_align_)
+           this->max_align_ = addralign;
+       }
+
       this->is_max_align_known_ = true;
     }
 
@@ -2671,11 +2856,15 @@ Output_segment::set_section_addresses(const Layout* layout, bool reset,
 
   bool in_tls = false;
 
+  bool in_relro = (parameters->options().relro()
+                  && this->is_first_section_relro());
+
   off_t orig_off = *poff;
   this->offset_ = orig_off;
 
   addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
-                                         addr, poff, pshndx, &in_tls);
+                                         addr, poff, pshndx, &in_tls,
+                                         &in_relro);
   this->filesz_ = *poff - orig_off;
 
   off_t off = *poff;
@@ -2683,7 +2872,7 @@ Output_segment::set_section_addresses(const Layout* layout, bool reset,
   uint64_t ret = this->set_section_list_addresses(layout, reset,
                                                   &this->output_bss_,
                                                  addr, poff, pshndx,
-                                                  &in_tls);
+                                                  &in_tls, &in_relro);
 
   // 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
@@ -2694,6 +2883,14 @@ Output_segment::set_section_addresses(const Layout* layout, bool reset,
       *poff = align_address(*poff, segment_align);
     }
 
+  // If all the sections were relro sections, align upward to the
+  // common page size.
+  if (in_relro)
+    {
+      uint64_t page_align = parameters->target().common_pagesize();
+      *poff = align_address(*poff, page_align);
+    }
+
   this->memsz_ = *poff - orig_off;
 
   // Ignore the file offset adjustments made by the BSS Output_data
@@ -2711,7 +2908,7 @@ Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
                                            Output_data_list* pdl,
                                           uint64_t addr, off_t* poff,
                                           unsigned int* pshndx,
-                                           bool* in_tls)
+                                           bool* in_tls, bool* in_relro)
 {
   off_t startoff = *poff;
 
@@ -2762,6 +2959,19 @@ Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
                 }
             }
 
+         // If this is a non-relro section after a relro section,
+         // align it to a common page boundary so that the dynamic
+         // linker has a page to mark as read-only.
+         if (*in_relro
+             && (!(*p)->is_section()
+                 || !(*p)->output_section()->is_relro()))
+           {
+             uint64_t page_align = parameters->target().common_pagesize();
+             if (page_align > align)
+               align = page_align;
+             *in_relro = false;
+           }
+
          off = align_address(off, align);
          (*p)->set_address_and_file_offset(addr + (off - startoff), off);
        }
@@ -2855,6 +3065,16 @@ Output_segment::set_offset()
       gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
       this->memsz_ = align_address(this->memsz_, segment_align);
     }
+
+  // If this is a RELRO segment, align the memory size.  The code in
+  // set_section_list ensures that the section after the RELRO segment
+  // is aligned to give us room.
+  if (this->type_ == elfcpp::PT_GNU_RELRO)
+    {
+      uint64_t page_align = parameters->target().common_pagesize();
+      gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align));
+      this->memsz_ = align_address(this->memsz_, page_align);
+    }
 }
 
 // Set the TLS offsets of the sections in the PT_TLS segment.
@@ -3035,6 +3255,29 @@ Output_segment::write_section_headers_list(const Layout* layout,
   return v;
 }
 
+// Print the output sections to the map file.
+
+void
+Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
+{
+  if (this->type() != elfcpp::PT_LOAD)
+    return;
+  this->print_section_list_to_mapfile(mapfile, &this->output_data_);
+  this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
+}
+
+// Print an output section list to the map file.
+
+void
+Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
+                                             const Output_data_list* pdl) const
+{
+  for (Output_data_list::const_iterator p = pdl->begin();
+       p != pdl->end();
+       ++p)
+    (*p)->print_to_mapfile(mapfile);
+}
+
 // Output_file methods.
 
 Output_file::Output_file(const char* name)
@@ -3079,7 +3322,8 @@ Output_file::open(off_t file_size)
            unlink_if_ordinary(this->name_);
 
          int mode = parameters->options().relocatable() ? 0666 : 0777;
-         int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
+         int o = open_descriptor(-1, 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 page took 0.034251 seconds and 4 git commands to generate.