gdb/
[deliverable/binutils-gdb.git] / gold / compressed_output.cc
index bf5f6407d3b7cefd2ffae5432e8e6f799fa1528e..9c7c7dd7e76c3789e3b39968b3e8133ed0d8b5ce 100644 (file)
@@ -1,6 +1,6 @@
-// compressed_output.cc -- manage compressed output sections for gold
+// compressed_output.cc -- manage compressed debug sections for gold
 
-// Copyright 2007 Free Software Foundation, Inc.
+// Copyright 2007, 2008, 2010 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of gold.
 #include <zlib.h>
 #endif
 
-#include "compressed_output.h"
 #include "parameters.h"
+#include "options.h"
+#include "compressed_output.h"
 
 namespace gold
 {
 
+#ifdef HAVE_ZLIB_H
+
 // Compress UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE.  Returns true
 // if it successfully compressed, false if it failed for any reason
 // (including not having zlib support in the library).  If it returns
 // true, it allocates memory for the compressed data using new, and
 // sets *COMPRESSED_DATA and *COMPRESSED_SIZE to appropriate values.
-
-#ifdef HAVE_ZLIB_H
+// It also writes a header before COMPRESSED_DATA: 4 bytes saying
+// "ZLIB", and 8 bytes indicating the uncompressed size, in big-endian
+// order.
 
 static bool
-zlib_compress(const char* uncompressed_data, unsigned long uncompressed_size,
-              char** compressed_data, unsigned long* compressed_size)
+zlib_compress(const unsigned char* uncompressed_data,
+              unsigned long uncompressed_size,
+              unsigned char** compressed_data,
+              unsigned long* compressed_size)
 {
+  const int header_size = 12;
   *compressed_size = uncompressed_size + uncompressed_size / 1000 + 128;
-  *compressed_data = new char[*compressed_size];
+  *compressed_data = new unsigned char[*compressed_size + header_size];
 
   int compress_level;
-  if (parameters->optimization_level() >= 1)
+  if (parameters->options().optimize() >= 1)
     compress_level = 9;
   else
     compress_level = 1;
 
-  int rc = compress2(reinterpret_cast<Bytef*>(*compressed_data),
+  int rc = compress2(reinterpret_cast<Bytef*>(*compressed_data) + header_size,
                      compressed_size,
                      reinterpret_cast<const Bytef*>(uncompressed_data),
                      uncompressed_size,
                      compress_level);
   if (rc == Z_OK)
-    return true;
+    {
+      memcpy(*compressed_data, "ZLIB", 4);
+      elfcpp::Swap_unaligned<64, true>::writeval(*compressed_data + 4,
+                                                uncompressed_size);
+      *compressed_size += header_size;
+      return true;
+    }
   else
     {
       delete[] *compressed_data;
@@ -68,208 +81,167 @@ zlib_compress(const char* uncompressed_data, unsigned long uncompressed_size,
     }
 }
 
+// Decompress COMPRESSED_DATA of size COMPRESSED_SIZE, into a buffer
+// UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE.  Returns TRUE if it
+// decompressed successfully, false if it failed.  The buffer, of
+// appropriate size, is provided by the caller, and is typically part
+// of the memory-mapped output file.
+
+static bool
+zlib_decompress(const unsigned char* compressed_data,
+               unsigned long compressed_size,
+               unsigned char* uncompressed_data,
+               unsigned long uncompressed_size)
+{
+  z_stream strm;
+  int rc;
+
+  /* It is possible the section consists of several compressed
+     buffers concatenated together, so we uncompress in a loop.  */
+  strm.zalloc = NULL;
+  strm.zfree = NULL;
+  strm.opaque = NULL;
+  strm.avail_in = compressed_size;
+  strm.next_in = const_cast<Bytef*>(compressed_data);
+  strm.avail_out = uncompressed_size;
+
+  rc = inflateInit(&strm);
+  while (strm.avail_in > 0)
+    {
+      if (rc != Z_OK)
+        return false;
+      strm.next_out = ((Bytef*) uncompressed_data
+                       + (uncompressed_size - strm.avail_out));
+      rc = inflate(&strm, Z_FINISH);
+      if (rc != Z_STREAM_END)
+        return false;
+      rc = inflateReset(&strm);
+    }
+  rc = inflateEnd(&strm);
+  if (rc != Z_OK || strm.avail_out != 0)
+    return false;
+
+  return true;
+}
+
 #else // !defined(HAVE_ZLIB_H)
 
 static bool
-zlib_compress(const char*, unsigned long, char**, unsigned long*)
+zlib_compress(const unsigned char*, unsigned long,
+              unsigned char**, unsigned long*)
+{
+  return false;
+}
+
+static bool
+zlib_decompress(const unsigned char*, unsigned long,
+               unsigned char*, unsigned long)
 {
   return false;
 }
 
 #endif // !defined(HAVE_ZLIB_H)
 
-// After compressing an output section, we rename it from foo to
-// foo.zlib.nnnn, where nnnn is the uncompressed size of the section.
+// Read the compression header of a compressed debug section and return
+// the uncompressed size.
 
-static std::string
-zlib_compressed_suffix(unsigned long uncompressed_size)
+uint64_t
+get_uncompressed_size(const unsigned char* compressed_data,
+                     section_size_type compressed_size)
 {
-  char size_string[64];
-  snprintf(size_string, sizeof(size_string), "%lu", uncompressed_size);
-  return std::string(".zlib.") + size_string;
+  const unsigned int zlib_header_size = 12;
+
+  /* Verify the compression header.  Currently, we support only zlib
+     compression, so it should be "ZLIB" followed by the uncompressed
+     section size, 8 bytes in big-endian order.  */
+  if (compressed_size >= zlib_header_size
+      && strncmp(reinterpret_cast<const char*>(compressed_data),
+                "ZLIB", 4) == 0)
+    return elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4);
+  return -1ULL;
 }
 
-// Class Output_compressed_section_data.
-
-// Add an input section.  In this case, we just keep track of the sections.
+// Decompress a compressed debug section directly into the output file.
 
 bool
-Output_compressed_section_data::do_add_input_section(Relobj* obj,
-                                                     unsigned int shndx)
+decompress_input_section(const unsigned char* compressed_data,
+                        unsigned long compressed_size,
+                        unsigned char* uncompressed_data,
+                        unsigned long uncompressed_size)
 {
-  this->objects_.push_back(Object_entry(obj, shndx));
-  return true;
+  const unsigned int zlib_header_size = 12;
+
+  /* Verify the compression header.  Currently, we support only zlib
+     compression, so it should be "ZLIB" followed by the uncompressed
+     section size, 8 bytes in big-endian order.  */
+  if (compressed_size >= zlib_header_size
+      && strncmp(reinterpret_cast<const char*>(compressed_data),
+                "ZLIB", 4) == 0)
+    {
+      unsigned long uncompressed_size_check =
+         elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4);
+      gold_assert(uncompressed_size_check == uncompressed_size);
+      return zlib_decompress(compressed_data + zlib_header_size,
+                            compressed_size - zlib_header_size,
+                            uncompressed_data,
+                            uncompressed_size);
+    }
+  return false;
 }
 
+// Class Output_compressed_section.
+
 // Set the final data size of a compressed section.  This is where
 // we actually compress the section data.
 
 void
-Output_compressed_section_data::set_final_data_size()
+Output_compressed_section::set_final_data_size()
 {
-  // FIXME: assert that relocations have already been applied.
-
-  off_t uncompressed_size = 0;
-  for (std::vector<Object_entry>::iterator it = this->objects_.begin();
-       it != this->objects_.end();
-       ++it)
-    {
-      it->contents
-        = it->object->section_contents(it->shndx, &it->length, false);
-      uncompressed_size += it->length;
-    }
+  off_t uncompressed_size = this->postprocessing_buffer_size();
 
   // (Try to) compress the data.
   unsigned long compressed_size;
-  char* uncompressed_data = new char[uncompressed_size];
-  off_t pos = 0;
-  for (std::vector<Object_entry>::const_iterator it = this->objects_.begin();
-       it != this->objects_.end();
-       ++it)
-    {
-      memcpy(uncompressed_data + pos,
-             reinterpret_cast<const char*>(it->contents),
-             it->length);
-      pos += it->length;
-    }
+  unsigned char* uncompressed_data = this->postprocessing_buffer();
+
+  // At this point the contents of all regular input sections will
+  // have been copied into the postprocessing buffer, and relocations
+  // will have been applied.  Now we need to copy in the contents of
+  // anything other than a regular input section.
+  this->write_to_postprocessing_buffer();
 
   bool success = false;
-  if (options_.zlib_compress_debug_sections())
+  if (strcmp(this->options_->compress_debug_sections(), "zlib") == 0)
     success = zlib_compress(uncompressed_data, uncompressed_size,
                             &this->data_, &compressed_size);
   if (success)
     {
-      delete[] uncompressed_data;
+      // This converts .debug_foo to .zdebug_foo
+      this->new_section_name_ = std::string(".z") + (this->name() + 1);
+      this->set_name(this->new_section_name_.c_str());
       this->set_data_size(compressed_size);
-      this->new_section_name_ = zlib_compressed_suffix(uncompressed_size);
     }
   else
     {
-      gold_warning(_("Not compressing section data: zlib error"));
+      gold_warning(_("not compressing section data: zlib error"));
       gold_assert(this->data_ == NULL);
-      this->data_ = uncompressed_data;
       this->set_data_size(uncompressed_size);
     }
 }
 
-// Change the name of the output section to reflect it's compressed.
-// The layout routines call into this right before finalizing the
-// shstrtab.
-
-const char*
-Output_compressed_section_data::do_modified_output_section_name(
-  const char* name)
-{
-  // This mean we never compressed the data.
-  if (this->new_section_name_.empty())
-    return NULL;
-  this->new_section_name_ = std::string(name) + this->new_section_name_;
-  return this->new_section_name_.c_str();
-}
-
 // Write out a compressed section.  If we couldn't compress, we just
 // write it out as normal, uncompressed data.
 
 void
-Output_compressed_section_data::do_write(Output_file* of)
+Output_compressed_section::do_write(Output_file* of)
 {
-  unsigned char* uview = of->get_output_view(this->offset(),
-                                             this->data_size());
-  char* view = reinterpret_cast<char*>(uview);
-  memcpy(view, this->data_, this->data_size());
-  of->write_output_view(this->offset(), this->data_size(), uview);
-}
-
-// Class Output_compressed_string.
-
-// Add an input section.  We don't do anything special here.
-
-template<typename Char_type>
-bool
-Output_compressed_string<Char_type>::do_add_input_section(Relobj* object,
-                                                          unsigned int shndx)
-{
-  return Output_merge_string<Char_type>::do_add_input_section(object, shndx);
-}
-
-// Set the final data size of a compressed section.  This is where
-// we actually compress the section data.
-
-template<typename Char_type>
-void
-Output_compressed_string<Char_type>::set_final_data_size()
-{
-  // First let the superclass finalize all its data, then write it to
-  // a buffer.
-  unsigned long uncompressed_size = this->finalize_merged_data();
-  char* uncompressed_data = new char[uncompressed_size];
-  this->stringpool_to_buffer(uncompressed_data, uncompressed_size);
-
-  // (Try to) compress the data.
-  unsigned long compressed_size;
-  if (options_.zlib_compress_debug_sections()
-      && zlib_compress(uncompressed_data, uncompressed_size,
-                       &this->compressed_data_, &compressed_size))
-    {
-      this->set_data_size(compressed_size);
-      // Save some memory.
-      this->clear_stringpool();
-      // We will be renaming the section to name.zlib.uncompressed_size.
-      this->new_section_name_ = zlib_compressed_suffix(uncompressed_size);
-    }
-  else
-    {
-      this->compressed_data_ = NULL;
-      this->set_data_size(uncompressed_size);
-    }
-
-  delete[] uncompressed_data;
-}
-
-// Change the name of the output section to reflect it's compressed.
-// The layout routines call into this right before finalizing the
-// shstrtab.
-
-template<typename Char_type>
-const char*
-Output_compressed_string<Char_type>::do_modified_output_section_name(
-  const char* name)
-{
-  // This mean we never compressed the data
-  if (this->new_section_name_.empty())
-    return NULL;
-  this->new_section_name_ = std::string(name) + this->new_section_name_;
-  return this->new_section_name_.c_str();
-}
-
-// Write out a compressed string section.  If we couldn't compress,
-// we just write out the normal string section.
-
-template<typename Char_type>
-void
-Output_compressed_string<Char_type>::do_write(Output_file* of)
-{
-  if (this->compressed_data_ == NULL)
-    Output_merge_string<Char_type>::do_write(of);
+  off_t offset = this->offset();
+  off_t data_size = this->data_size();
+  unsigned char* view = of->get_output_view(offset, data_size);
+  if (this->data_ == NULL)
+    memcpy(view, this->postprocessing_buffer(), data_size);
   else
-    {
-      unsigned char* uview = of->get_output_view(this->offset(),
-                                                 this->data_size());
-      char* view = reinterpret_cast<char*>(uview);
-      memcpy(view, this->compressed_data_, this->data_size());
-      of->write_output_view(this->offset(), this->data_size(), uview);
-    }
+    memcpy(view, this->data_, data_size);
+  of->write_output_view(offset, data_size, view);
 }
 
-// Instantiate the templates we need.
-
-template
-class Output_compressed_string<char>;
-
-template
-class Output_compressed_string<uint16_t>;
-
-template
-class Output_compressed_string<uint32_t>;
-
 } // End namespace gold.
This page took 0.046308 seconds and 4 git commands to generate.