Compress all debug sections.
[platform/upstream/binutils.git] / gold / output.cc
index 5e9758b..59658c7 100644 (file)
@@ -43,7 +43,7 @@ namespace gold
 
 // Output_data variables.
 
-bool Output_data::sizes_are_fixed;
+bool Output_data::allocated_sizes_are_fixed;
 
 // Output_data methods.
 
@@ -51,18 +51,6 @@ Output_data::~Output_data()
 {
 }
 
-// Set the address and offset.
-
-void
-Output_data::set_address(uint64_t addr, off_t off)
-{
-  this->address_ = addr;
-  this->offset_ = off;
-
-  // Let the child class know.
-  this->do_set_address(addr, off);
-}
-
 // Return the default alignment for the target size.
 
 uint64_t
@@ -335,6 +323,8 @@ Output_file_header::set_section_info(const Output_section_headers* shdrs,
 void
 Output_file_header::do_write(Output_file* of)
 {
+  gold_assert(this->offset() == 0);
+
   if (parameters->get_size() == 32)
     {
       if (parameters->is_big_endian())
@@ -491,11 +481,10 @@ Output_section_data::do_out_shndx() const
 
 // Output_data_strtab methods.
 
-// Set the address.  We don't actually care about the address, but we
-// do set our final size.
+// Set the final data size.
 
 void
-Output_data_strtab::do_set_address(uint64_t, off_t)
+Output_data_strtab::set_final_data_size()
 {
   this->strtab_->set_string_offsets();
   this->set_data_size(this->strtab_->get_strtab_size());
@@ -890,7 +879,7 @@ Output_data_dynamic::do_adjust_output_section(Output_section* os)
 // Set the final data size.
 
 void
-Output_data_dynamic::do_set_address(uint64_t, off_t)
+Output_data_dynamic::set_final_data_size()
 {
   // Add the terminating entry.
   this->add_constant(elfcpp::DT_NULL, 0);
@@ -997,13 +986,25 @@ Output_section::Input_section::data_size() const
 // Set the address and file offset.
 
 void
-Output_section::Input_section::set_address(uint64_t addr, off_t off,
-                                          off_t secoff)
+Output_section::Input_section::set_address_and_file_offset(
+    uint64_t address,
+    off_t file_offset,
+    off_t section_file_offset)
 {
   if (this->is_input_section())
-    this->u2_.object->set_section_offset(this->shndx_, off - secoff);
+    this->u2_.object->set_section_offset(this->shndx_,
+                                        file_offset - section_file_offset);
   else
-    this->u2_.posd->set_address(addr, off);
+    this->u2_.posd->set_address_and_file_offset(address, file_offset);
+}
+
+// Finalize the data size.
+
+void
+Output_section::Input_section::finalize_data_size()
+{
+  if (!this->is_input_section())
+    this->u2_.posd->finalize_data_size();
 }
 
 // Try to turn an input offset into an output offset.
@@ -1040,6 +1041,16 @@ Output_section::Input_section::write(Output_file* of)
     this->u2_.posd->write(of);
 }
 
+// Write the data to a buffer.  As for write(), we don't have to do
+// anything for an input section.
+
+void
+Output_section::Input_section::write_to_buffer(unsigned char* buffer)
+{
+  if (!this->is_input_section())
+    this->u2_.posd->write_to_buffer(buffer);
+}
+
 // Output_section methods.
 
 // Construct an Output_section.  NAME will point into a Stringpool.
@@ -1061,12 +1072,19 @@ Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
     input_sections_(),
     first_input_offset_(0),
     fills_(),
+    postprocessing_buffer_(NULL),
     needs_symtab_index_(false),
     needs_dynsym_index_(false),
     should_link_to_symtab_(false),
     should_link_to_dynsym_(false),
-    after_input_sections_(false)
+    after_input_sections_(false),
+    requires_postprocessing_(false)
 {
+  // An unallocated section has no address.  Forcing this means that
+  // we don't need special treatment for symbols defined in debug
+  // sections.
+  if ((flags & elfcpp::SHF_ALLOC) == 0)
+    this->set_address(0);
 }
 
 Output_section::~Output_section()
@@ -1114,11 +1132,15 @@ 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();
+  uint64_t entsize = shdr.get_sh_entsize();
 
   // .debug_str is a mergeable string section, but is not always so
   // marked by compilers.  Mark manually here so we can optimize.
   if (strcmp(secname, ".debug_str") == 0)
-    sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
+    {
+      sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
+      entsize = 1;
+    }
 
   // 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
@@ -1127,8 +1149,7 @@ Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
       && reloc_shndx == 0)
     {
       if (this->add_merge_input_section(object, shndx, sh_flags,
-                                       shdr.get_sh_entsize(),
-                                       addralign))
+                                       entsize, addralign))
        {
          // Tell the relocation routines that they need to call the
          // output_offset method to determine the final address.
@@ -1136,7 +1157,7 @@ Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
        }
     }
 
-  off_t offset_in_section = this->data_size();
+  off_t offset_in_section = this->current_data_size_for_child();
   off_t aligned_offset_in_section = align_address(offset_in_section,
                                                   addralign);
 
@@ -1160,7 +1181,8 @@ Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
         }
     }
 
-  this->set_data_size(aligned_offset_in_section + shdr.get_sh_size());
+  this->set_current_data_size_for_child(aligned_offset_in_section
+                                       + shdr.get_sh_size());
 
   // We need to keep track of this section if we are already keeping
   // track of sections, or if we are relaxing.  FIXME: Add test for
@@ -1188,7 +1210,7 @@ void
 Output_section::add_output_section_data(Input_section* inp)
 {
   if (this->input_sections_.empty())
-    this->first_input_offset_ = this->data_size();
+    this->first_input_offset_ = this->current_data_size_for_child();
 
   this->input_sections_.push_back(*inp);
 
@@ -1228,30 +1250,37 @@ Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
        p != this->input_sections_.end();
        ++p)
     if (p->is_merge_section(is_string, entsize, addralign))
-      break;
+      {
+        p->add_input_section(object, shndx);
+        return true;
+      }
 
   // We handle the actual constant merging in Output_merge_data or
   // Output_merge_string_data.
-  if (p != this->input_sections_.end())
-    p->add_input_section(object, shndx);
+  Output_section_data* posd;
+  if (!is_string)
+    posd = new Output_merge_data(entsize, addralign);
   else
     {
-      Output_section_data* posd;
-      if (!is_string)
-       posd = new Output_merge_data(entsize, addralign);
-      else if (entsize == 1)
-       posd = new Output_merge_string<char>(addralign);
-      else if (entsize == 2)
-       posd = new Output_merge_string<uint16_t>(addralign);
-      else if (entsize == 4)
-       posd = new Output_merge_string<uint32_t>(addralign);
-      else
-       return false;
-
-      this->add_output_merge_section(posd, is_string, entsize);
-      posd->add_input_section(object, shndx);
+      switch (entsize)
+       {
+        case 1:
+         posd = new Output_merge_string<char>(addralign);
+         break;
+        case 2:
+         posd = new Output_merge_string<uint16_t>(addralign);
+         break;
+        case 4:
+         posd = new Output_merge_string<uint32_t>(addralign);
+         break;
+        default:
+         return false;
+       }
     }
 
+  this->add_output_merge_section(posd, is_string, entsize);
+  posd->add_input_section(object, shndx);
+
   return true;
 }
 
@@ -1341,22 +1370,28 @@ Output_section::output_address(const Relobj* object, unsigned int shndx,
   gold_unreachable();
 }
 
-// Set the address of an Output_section.  This is where we handle
+// Set the data size of an Output_section.  This is where we handle
 // setting the addresses of any Output_section_data objects.
 
 void
-Output_section::do_set_address(uint64_t address, off_t startoff)
+Output_section::set_final_data_size()
 {
   if (this->input_sections_.empty())
-    return;
+    {
+      this->set_data_size(this->current_data_size_for_child());
+      return;
+    }
 
+  uint64_t address = this->address();
+  off_t startoff = this->offset();
   off_t off = startoff + this->first_input_offset_;
   for (Input_section_list::iterator p = this->input_sections_.begin();
        p != this->input_sections_.end();
        ++p)
     {
       off = align_address(off, p->addralign());
-      p->set_address(address + (off - startoff), off, startoff);
+      p->set_address_and_file_offset(address + (off - startoff), off,
+                                    startoff);
       off += p->data_size();
     }
 
@@ -1400,6 +1435,8 @@ Output_section::write_header(const Layout* layout,
 void
 Output_section::do_write(Output_file* of)
 {
+  gold_assert(!this->requires_postprocessing());
+
   off_t output_section_file_offset = this->offset();
   for (Fill_list::iterator p = this->fills_.begin();
        p != this->fills_.end();
@@ -1416,6 +1453,63 @@ Output_section::do_write(Output_file* of)
     p->write(of);
 }
 
+// If a section requires postprocessing, create the buffer to use.
+
+void
+Output_section::create_postprocessing_buffer()
+{
+  gold_assert(this->requires_postprocessing());
+  gold_assert(this->postprocessing_buffer_ == NULL);
+
+  if (!this->input_sections_.empty())
+    {
+      off_t off = this->first_input_offset_;
+      for (Input_section_list::iterator p = this->input_sections_.begin();
+          p != this->input_sections_.end();
+          ++p)
+       {
+         off = align_address(off, p->addralign());
+         p->finalize_data_size();
+         off += p->data_size();
+       }
+      this->set_current_data_size_for_child(off);
+    }
+
+  off_t buffer_size = this->current_data_size_for_child();
+  this->postprocessing_buffer_ = new unsigned char[buffer_size];
+}
+
+// Write all the data of an Output_section into the postprocessing
+// buffer.  This is used for sections which require postprocessing,
+// such as compression.  Input sections are handled by
+// Object::Relocate.
+
+void
+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()));
+      memcpy(buffer + p->section_offset(), fill_data.data(), fill_data.size());
+    }
+
+  off_t off = this->first_input_offset_;
+  for (Input_section_list::iterator p = this->input_sections_.begin();
+       p != this->input_sections_.end();
+       ++p)
+    {
+      off = align_address(off, p->addralign());
+      p->write_to_buffer(buffer + off);
+      off += p->data_size();
+    }
+}
+
 // Output segment methods.
 
 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
@@ -1661,7 +1755,7 @@ Output_segment::set_section_list_addresses(Output_data_list* pdl,
        ++p)
     {
       off = align_address(off, (*p)->addralign());
-      (*p)->set_address(addr + (off - startoff), off);
+      (*p)->set_address_and_file_offset(addr + (off - startoff), off);
 
       // 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
@@ -1867,15 +1961,36 @@ Output_file::open(off_t file_size)
     gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
   this->o_ = o;
 
+  this->map();
+}
+
+// Resize the output file.
+
+void
+Output_file::resize(off_t file_size)
+{
+  if (::munmap(this->base_, this->file_size_) < 0)
+    gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
+  this->file_size_ = file_size;
+  this->map();
+}
+
+// Map the file into memory.
+
+void
+Output_file::map()
+{
+  int o = this->o_;
+
   // Write out one byte to make the file the right size.
-  if (::lseek(o, file_size - 1, SEEK_SET) < 0)
+  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));
 
   // Map the file into memory.
-  void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
+  void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
                      MAP_SHARED, o, 0);
   if (base == MAP_FAILED)
     gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));