Add -Wshadow to the gcc command line options used when compiling the binutils.
[deliverable/binutils-gdb.git] / gold / compressed_output.cc
index 1eca021e716934c72b93974cfbd17ae6bc5b92a7..814bd250aac52042a7f541d82315ff403443bae3 100644 (file)
@@ -1,6 +1,6 @@
 // compressed_output.cc -- manage compressed output sections for gold
 
-// Copyright 2007 Free Software Foundation, Inc.
+// Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of gold.
@@ -38,15 +38,21 @@ namespace gold
 // (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.
+// It also writes a header before COMPRESSED_DATA: 4 bytes saying
+// "ZLIB", and 8 bytes indicating the uncompressed size, in big-endian
+// order.
 
 #ifdef HAVE_ZLIB_H
 
 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->options().optimize() >= 1)
@@ -54,13 +60,19 @@ zlib_compress(const char* uncompressed_data, unsigned long uncompressed_size,
   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;
@@ -72,24 +84,14 @@ zlib_compress(const char* uncompressed_data, unsigned long uncompressed_size,
 #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;
 }
 
 #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.
-
-static std::string
-zlib_compressed_suffix(unsigned long uncompressed_size)
-{
-  char size_string[64];
-  snprintf(size_string, sizeof(size_string), "%lu", uncompressed_size);
-  return std::string(".zlib.") + size_string;
-}
-
 // Class Output_compressed_section.
 
 // Set the final data size of a compressed section.  This is where
@@ -102,8 +104,7 @@ Output_compressed_section::set_final_data_size()
 
   // (Try to) compress the data.
   unsigned long compressed_size;
-  unsigned char* u_uncompressed_data = this->postprocessing_buffer();
-  char* uncompressed_data = reinterpret_cast<char*>(u_uncompressed_data);
+  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
@@ -112,13 +113,13 @@ Output_compressed_section::set_final_data_size()
   this->write_to_postprocessing_buffer();
 
   bool success = false;
-  if (this->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)
     {
-      std::string suffix(zlib_compressed_suffix(uncompressed_size));
-      this->new_section_name_ = std::string(this->name()) + suffix;
+      // 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);
     }
@@ -136,14 +137,14 @@ Output_compressed_section::set_final_data_size()
 void
 Output_compressed_section::do_write(Output_file* of)
 {
-  off_t offset = this->offset();
-  off_t data_size = this->data_size();
-  unsigned char* view = of->get_output_view(offset, data_size);
+  off_t off = this->offset();
+  off_t datasize = this->data_size();
+  unsigned char* view = of->get_output_view(off, datasize);
   if (this->data_ == NULL)
-    memcpy(view, this->postprocessing_buffer(), data_size);
+    memcpy(view, this->postprocessing_buffer(), datasize);
   else
-    memcpy(view, this->data_, data_size);
-  of->write_output_view(offset, data_size, view);
+    memcpy(view, this->data_, datasize);
+  of->write_output_view(off, datasize, view);
 }
 
 } // End namespace gold.
This page took 0.029965 seconds and 4 git commands to generate.