This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Craig Silverstein] PATCH: new format for compressed sections


I committed this patch for Craig.

Date: Thu,  3 Apr 2008 00:19:15 -0700 (PDT)
From: csilvers@google.com (Craig Silverstein)
Lines: 206
Xref: localhost.localdomain mail.misc:169600

This patch goes along with the patch I've sent to the gdb folks, to
change the format of compressed debugging sections.  I now store the
uncompressed length in the section proper, rather than in the section
name, so files will have dependable section names.

In the process of doing this, I noticed some brokenness in the
testsuite Makefiles for testing this feature.

craig

--cut here--

2008-04-03  Craig Silverstein  <csilvers@google.com>

	* compressed_output.h (class Output_compressed_section): Use
        unsigned buffer.
	* compressed_output.cc (zlib_compress): Use unsigned buffers,
        add zlib header.
	(zlib_compressed_suffix): Removed.
	(Output_compressed_section::set_final_data_size): Use unsigned
        buffers.
	* testsuite/Makefile.am (flagstest_compress_debug_sections):
        Fix linker invocation.
	(flagstest_o_specialfile_and_compress_debug_sections):
        Likewise.
	* testsuite/Makefile.in: Regenerated.

Index: compressed_output.cc
===================================================================
RCS file: /cvs/src/src/gold/compressed_output.cc,v
retrieving revision 1.8
diff -p -u -r1.8 compressed_output.cc
--- compressed_output.cc	13 Mar 2008 21:04:21 -0000	1.8
+++ compressed_output.cc	3 Apr 2008 14:14:54 -0000
@@ -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_d
   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_d
 #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_dat
 
   // (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
@@ -117,8 +118,8 @@ Output_compressed_section::set_final_dat
                             &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);
     }
Index: compressed_output.h
===================================================================
RCS file: /cvs/src/src/gold/compressed_output.h,v
retrieving revision 1.3
diff -p -u -r1.3 compressed_output.h
--- compressed_output.h	13 Mar 2008 21:04:21 -0000	1.3
+++ compressed_output.h	3 Apr 2008 14:14:54 -0000
@@ -64,7 +64,7 @@ class Output_compressed_section : public
   // The options--this includes the compression type.
   const General_options* options_;
   // The compressed data.
-  char* data_;
+  unsigned char* data_;
   // The new section name if we do compress.
   std::string new_section_name_;
 };
Index: testsuite/Makefile.am
===================================================================
RCS file: /cvs/src/src/gold/testsuite/Makefile.am,v
retrieving revision 1.53
diff -p -u -r1.53 Makefile.am
--- testsuite/Makefile.am	29 Mar 2008 08:39:26 -0000	1.53
+++ testsuite/Makefile.am	3 Apr 2008 14:14:54 -0000
@@ -507,7 +507,7 @@ undef_symbol.err: undef_symbol_main.o un
 # Test --compress-debug-sections.  FIXME: check we actually compress.
 check_PROGRAMS += flagstest_compress_debug_sections
 flagstest_compress_debug_sections: flagstest_debug.o gcctestdir/ld
-	$(CXXLINK) -Bgcctestdir/ -o $@ $< --compress-debug-sections=zlib
+	$(CXXLINK) -Bgcctestdir/ -o $@ $< -Wl,--compress-debug-sections=zlib
 	test -s $@
 
 
@@ -523,7 +523,7 @@ flagstest_o_specialfile: flagstest_debug
 check_PROGRAMS += flagstest_o_specialfile_and_compress_debug_sections
 flagstest_o_specialfile_and_compress_debug_sections: flagstest_debug.o \
 		gcctestdir/ld
-	$(CXXLINK) -Bgcctestdir/ -o /dev/stdout $< --compress-debug-sections=zlib 2>&1 | cat > $@
+	$(CXXLINK) -Bgcctestdir/ -o /dev/stdout $< -Wl,--compress-debug-sections=zlib 2>&1 | cat > $@
 	chmod a+x $@
 	test -s $@
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]