PR ld/10515
[binutils-gdb.git] / gold / output.cc
index ce29b53ccbb143b3adacf1a3da05ac574dbe02e0..64fcb37187b5cbc2a004e803d6a721f00e8afe8d 100644 (file)
@@ -1,6 +1,6 @@
 // output.cc -- manage the output file for gold
 
-// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of gold.
@@ -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
 # define MAP_ANONYMOUS  MAP_ANON
 #endif
 
+#ifndef HAVE_POSIX_FALLOCATE
+// A dummy, non general, version of posix_fallocate.  Here we just set
+// the file size and hope that there is enough disk space.  FIXME: We
+// could allocate disk space by walking block by block and writing a
+// zero byte into each block.
+static int
+posix_fallocate(int o, off_t offset, off_t len)
+{
+  return ftruncate(o, offset + len);
+}
+#endif // !defined(HAVE_POSIX_FALLOCATE)
+
 namespace gold
 {
 
@@ -419,7 +432,6 @@ Output_file_header::do_sized_write(Output_file* of)
                              ? elfcpp::ELFDATA2MSB
                              : elfcpp::ELFDATA2LSB);
   e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
-  // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
   oehdr.put_e_ident(e_ident);
 
   elfcpp::ET e_type;
@@ -476,6 +488,10 @@ Output_file_header::do_sized_write(Output_file* of)
   else
     oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
 
+  // Let the target adjust the ELF header, e.g., to set EI_OSABI in
+  // the e_ident field.
+  parameters->target().adjust_elf_header(view, ehdr_size);
+
   of->write_output_view(0, ehdr_size, view);
 }
 
@@ -617,7 +633,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)
@@ -707,7 +723,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),
@@ -755,12 +771,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;
     }
@@ -812,8 +823,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();
@@ -831,7 +841,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
 {
@@ -840,14 +850,14 @@ 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 != invalid_address)
     return offset + addend;
   // This is a merge section.
   offset = os->output_address(this->u1_.relobj, lsi, addend);
-  gold_assert(offset != -1);
+  gold_assert(offset != invalid_address);
   return offset;
 }
 
@@ -860,17 +870,16 @@ 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 != invalid_address)
        address += os->address() + off;
       else
        {
          address = os->output_address(this->u2_.relobj, this->shndx_,
                                       address);
-         gold_assert(address != -1U);
+         gold_assert(address != invalid_address);
        }
     }
   else if (this->u2_.od != NULL)
@@ -1094,8 +1103,7 @@ Output_data_group<size, big_endian>::do_write(Output_file* of)
        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)
@@ -1358,8 +1366,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));
@@ -1389,8 +1396,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));
@@ -1701,6 +1707,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.
@@ -1737,6 +1762,10 @@ 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),
+    is_small_section_(false),
+    is_large_section_(false),
     tls_offset_(0)
 {
   // An unallocated section has no address.  Forcing this means that
@@ -1808,9 +1837,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))
@@ -1857,7 +1888,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));
@@ -1963,6 +1995,24 @@ Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
   return true;
 }
 
+// Update the output section flags based on input section flags.
+
+void
+Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
+{
+  // If we created the section with SHF_ALLOC clear, we set the
+  // address.  If we are now setting the SHF_ALLOC flag, we need to
+  // undo that.
+  if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
+      && (flags & elfcpp::SHF_ALLOC) != 0)
+    this->mark_address_invalid();
+
+  this->flags_ |= (flags
+                  & (elfcpp::SHF_WRITE
+                     | elfcpp::SHF_ALLOC
+                     | elfcpp::SHF_EXECINSTR));
+}
+
 // Given an address OFFSET relative to the start of input section
 // SHNDX in OBJECT, return whether this address is being included in
 // the final link.  This should only be called if SHNDX in OBJECT has
@@ -1973,8 +2023,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)
@@ -1999,7 +2047,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());
 
@@ -2021,8 +2068,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();
@@ -2033,7 +2078,7 @@ Output_section::output_address(const Relobj* object, unsigned int shndx,
       if (p->output_offset(object, shndx, offset, &output_offset))
        {
          if (output_offset == -1)
-           return -1U;
+           return -1ULL;
          return addr + output_offset;
        }
       addr += p->data_size();
@@ -2047,15 +2092,14 @@ Output_section::output_address(const Relobj* object, unsigned int shndx,
   gold_unreachable();
 }
 
-// Return the output address of the start of the merged section for
+// Find the output address of the start of the merged section for
 // input section SHNDX in object OBJECT.
 
-uint64_t
-Output_section::starting_output_address(const Relobj* object,
-                                       unsigned int shndx) const
+bool
+Output_section::find_starting_output_address(const Relobj* object,
+                                            unsigned int shndx,
+                                            uint64_t* paddr) 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();
@@ -2068,11 +2112,16 @@ Output_section::starting_output_address(const Relobj* object,
       // Unfortunately we don't know for sure that input offset 0 is
       // mapped at all.
       if (p->is_merge_section_for(object, shndx))
-       return addr;
+       {
+         *paddr = addr;
+         return true;
+       }
 
       addr += p->data_size();
     }
-  gold_unreachable();
+
+  // We couldn't find a merge output section for this input section.
+  return false;
 }
 
 // Set the data size of an Output_section.  This is where we handle
@@ -2544,6 +2593,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
@@ -2571,7 +2633,8 @@ Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
     type_(type),
     flags_(flags),
     is_max_align_known_(false),
-    are_addresses_set_(false)
+    are_addresses_set_(false),
+    is_large_data_segment_(false)
 {
 }
 
@@ -2579,11 +2642,11 @@ 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_);
+  gold_assert(os->is_large_data_section() == this->is_large_data_segment());
 
   // Update the segment flags.
   this->flags_ |= seg_flags;
@@ -2615,7 +2678,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;
@@ -2632,8 +2694,7 @@ Output_segment::add_output_section(Output_section* os,
   // and the PT_TLS segment -- we do this grouping only for the
   // PT_LOAD segment.
   if (this->type_ != elfcpp::PT_TLS
-      && (os->flags() & elfcpp::SHF_TLS) != 0
-      && !this->output_data_.empty())
+      && (os->flags() & elfcpp::SHF_TLS) != 0)
     {
       pdl = &this->output_data_;
       bool nobits = os->type() == elfcpp::SHT_NOBITS;
@@ -2647,7 +2708,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);
            }
@@ -2660,7 +2721,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;
@@ -2672,10 +2732,92 @@ 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;
+    }
+
+  // Small data sections go at the end of the list of data sections.
+  // If OS is not small, and there are small sections, we have to
+  // insert it before the first small section.
+  if (os->type() != elfcpp::SHT_NOBITS
+      && !os->is_small_section()
+      && !pdl->empty()
+      && pdl->back()->is_section()
+      && pdl->back()->output_section()->is_small_section())
+    {
+      for (Output_segment::Output_data_list::iterator p = pdl->begin();
+          p != pdl->end();
+          ++p)
+       {
+         if ((*p)->is_section()
+             && (*p)->output_section()->is_small_section())
+           {
+             pdl->insert(p, os);
+             return;
+           }
+       }
+      gold_unreachable();
+    }
+
+  // A small BSS section goes at the start of the BSS sections, after
+  // other small BSS sections.
+  if (os->type() == elfcpp::SHT_NOBITS && os->is_small_section())
+    {
+      for (Output_segment::Output_data_list::iterator p = pdl->begin();
+          p != pdl->end();
+          ++p)
+       {
+         if (!(*p)->is_section()
+             || !(*p)->output_section()->is_small_section())
+           {
+             pdl->insert(p, os);
+             return;
+           }
+       }
+    }
+
+  // A large BSS section goes at the end of the BSS sections, which
+  // means that one that is not large must come before the first large
+  // one.
+  if (os->type() == elfcpp::SHT_NOBITS
+      && !os->is_large_section()
+      && !pdl->empty()
+      && pdl->back()->is_section()
+      && pdl->back()->output_section()->is_large_section())
+    {
+      for (Output_segment::Output_data_list::iterator p = pdl->begin();
+          p != pdl->end();
+          ++p)
+       {
+         if ((*p)->is_section()
+             && (*p)->output_section()->is_large_section())
+           {
+             pdl->insert(p, os);
+             return;
+           }
+       }
+      gold_unreachable();
+    }
+
+  pdl->push_back(os);
 }
 
 // Remove an Output_section from this segment.  It is an error if it
@@ -2709,6 +2851,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
@@ -2726,6 +2878,19 @@ 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 (this->type_ == elfcpp::PT_LOAD
+         && 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;
     }
 
@@ -2798,11 +2963,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;
@@ -2810,7 +2979,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
@@ -2821,6 +2990,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
@@ -2838,7 +3015,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;
 
@@ -2889,6 +3066,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);
        }
@@ -2982,6 +3172,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.
@@ -3162,6 +3362,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)
@@ -3206,7 +3429,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;
@@ -3241,6 +3465,17 @@ Output_file::resize(off_t file_size)
     }
 }
 
+// Map a block of memory which will later be written to the file.
+// Return a pointer to the memory.
+
+void*
+Output_file::map_anonymous()
+{
+  this->map_is_anonymous_ = true;
+  return ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
+               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+}
+
 // Map the file into memory.
 
 void
@@ -3257,27 +3492,37 @@ Output_file::map()
       || ::fstat(o, &statbuf) != 0
       || !S_ISREG(statbuf.st_mode)
       || this->is_temporary_)
-    {
-      this->map_is_anonymous_ = true;
-      base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
-                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
-    }
+    base = this->map_anonymous();
   else
     {
-      // Write out one byte to make the file the right size.
-      if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
-        gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
-      char b = 0;
-      if (::write(o, &b, 1) != 1)
-        gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
+      // Ensure that we have disk space available for the file.  If we
+      // don't do this, it is possible that we will call munmap,
+      // close, and exit with dirty buffers still in the cache with no
+      // assigned disk blocks.  If the disk is out of space at that
+      // point, the output file will wind up incomplete, but we will
+      // have already exited.  The alternative to fallocate would be
+      // to use fdatasync, but that would be a more significant
+      // performance hit.
+      if (::posix_fallocate(o, 0, this->file_size_) < 0)
+       gold_fatal(_("%s: %s"), this->name_, strerror(errno));
 
       // Map the file into memory.
       this->map_is_anonymous_ = false;
       base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
                     MAP_SHARED, o, 0);
+
+      // The mmap call might fail because of file system issues: the
+      // file system might not support mmap at all, or it might not
+      // support mmap with PROT_WRITE.  I'm not sure which errno
+      // values we will see in all cases, so if the mmap fails for any
+      // reason try for an anonymous map.
+      if (base == MAP_FAILED)
+       base = this->map_anonymous();
     }
   if (base == MAP_FAILED)
-    gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
+    gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
+              this->name_, static_cast<unsigned long>(this->file_size_),
+              strerror(errno));
   this->base_ = static_cast<unsigned char*>(base);
 }
 
@@ -3300,15 +3545,20 @@ Output_file::close()
   if (this->map_is_anonymous_ && !this->is_temporary_)
     {
       size_t bytes_to_write = this->file_size_;
+      size_t offset = 0;
       while (bytes_to_write > 0)
         {
-          ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
+          ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
+                                          bytes_to_write);
           if (bytes_written == 0)
             gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
           else if (bytes_written < 0)
             gold_error(_("%s: write: %s"), this->name_, strerror(errno));
           else
-            bytes_to_write -= bytes_written;
+            {
+              bytes_to_write -= bytes_written;
+              offset += bytes_written;
+            }
         }
     }
   this->unmap();
@@ -3373,6 +3623,86 @@ Output_section::add_input_section<64, true>(
     bool have_sections_script);
 #endif
 
+#ifdef HAVE_TARGET_32_LITTLE
+template
+class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
+#endif
+
+#ifdef HAVE_TARGET_32_LITTLE
+template
+class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
+#endif
+
+#ifdef HAVE_TARGET_32_LITTLE
+template
+class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
+#endif
+
+#ifdef HAVE_TARGET_32_LITTLE
+template
+class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
+#endif
+
 #ifdef HAVE_TARGET_32_LITTLE
 template
 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;