Display --interleave[=<number>]
[deliverable/binutils-gdb.git] / gold / compressed_output.cc
index a0f8ed181ace455f9db37188f9fa3a1822561ed4..8c9fc1e96f73b5d7c01277035fe0d0fce963a38f 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, 2008 Free Software Foundation, Inc.
+// Copyright (C) 2007-2015 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of gold.
 // MA 02110-1301, USA.
 
 #include "gold.h"
-
-#ifdef HAVE_ZLIB_H
 #include <zlib.h>
-#endif
-
 #include "parameters.h"
 #include "options.h"
 #include "compressed_output.h"
@@ -42,8 +38,6 @@ namespace gold
 // "ZLIB", and 8 bytes indicating the uncompressed size, in big-endian
 // order.
 
-#ifdef HAVE_ZLIB_H
-
 static bool
 zlib_compress(const unsigned char* uncompressed_data,
               unsigned long uncompressed_size,
@@ -81,16 +75,95 @@ zlib_compress(const unsigned char* uncompressed_data,
     }
 }
 
-#else // !defined(HAVE_ZLIB_H)
+// 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_compress(const unsigned char*, unsigned long,
-              unsigned char**, unsigned long*)
+zlib_decompress(const unsigned char* compressed_data,
+               unsigned long compressed_size,
+               unsigned char* uncompressed_data,
+               unsigned long uncompressed_size)
 {
-  return false;
+  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;
 }
 
-#endif // !defined(HAVE_ZLIB_H)
+// Read the compression header of a compressed debug section and return
+// the uncompressed size.
+
+uint64_t
+get_uncompressed_size(const unsigned char* compressed_data,
+                     section_size_type compressed_size)
+{
+  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;
+}
+
+// Decompress a compressed debug section directly into the output file.
+
+bool
+decompress_input_section(const unsigned char* compressed_data,
+                        unsigned long compressed_size,
+                        unsigned char* uncompressed_data,
+                        unsigned long uncompressed_size)
+{
+  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.
 
This page took 0.02543 seconds and 4 git commands to generate.