xref: /netbsd-src/external/gpl3/binutils/dist/gold/compressed_output.cc (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 // compressed_output.cc -- manage compressed debug sections for gold
2 
3 // Copyright (C) 2007-2024 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5 
6 // This file is part of gold.
7 
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12 
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22 
23 #include "gold.h"
24 #include <zlib.h>
25 #ifdef HAVE_ZSTD
26 #include <zstd.h>
27 #endif
28 #include "parameters.h"
29 #include "options.h"
30 #include "compressed_output.h"
31 
32 namespace gold
33 {
34 
35 // Compress UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE.  Returns true
36 // if it successfully compressed, false if it failed for any reason
37 // (including not having zlib support in the library).  If it returns
38 // true, it allocates memory for the compressed data using new, and
39 // sets *COMPRESSED_DATA and *COMPRESSED_SIZE to appropriate values.
40 // It also writes a header before COMPRESSED_DATA: 4 bytes saying
41 // "ZLIB", and 8 bytes indicating the uncompressed size, in big-endian
42 // order.
43 
44 static bool
zlib_compress(int header_size,const unsigned char * uncompressed_data,unsigned long uncompressed_size,unsigned char ** compressed_data,unsigned long * compressed_size)45 zlib_compress(int header_size,
46               const unsigned char* uncompressed_data,
47               unsigned long uncompressed_size,
48               unsigned char** compressed_data,
49               unsigned long* compressed_size)
50 {
51   *compressed_size = uncompressed_size + uncompressed_size / 1000 + 128;
52   *compressed_data = new unsigned char[*compressed_size + header_size];
53 
54   int compress_level;
55   if (parameters->options().optimize() >= 1)
56     compress_level = 9;
57   else
58     compress_level = 1;
59 
60   int rc = compress2(reinterpret_cast<Bytef*>(*compressed_data) + header_size,
61                      compressed_size,
62                      reinterpret_cast<const Bytef*>(uncompressed_data),
63                      uncompressed_size,
64                      compress_level);
65   if (rc == Z_OK)
66     {
67       *compressed_size += header_size;
68       return true;
69     }
70   else
71     {
72       delete[] *compressed_data;
73       *compressed_data = NULL;
74       return false;
75     }
76 }
77 
78 #if HAVE_ZSTD
79 static bool
zstd_compress(int header_size,const unsigned char * uncompressed_data,unsigned long uncompressed_size,unsigned char ** compressed_data,unsigned long * compressed_size)80 zstd_compress(int header_size, const unsigned char *uncompressed_data,
81 	      unsigned long uncompressed_size,
82 	      unsigned char **compressed_data, unsigned long *compressed_size)
83 {
84   size_t size = ZSTD_compressBound(uncompressed_size);
85   *compressed_data = new unsigned char[size + header_size];
86   size = ZSTD_compress(*compressed_data + header_size, size, uncompressed_data,
87 		       uncompressed_size, ZSTD_CLEVEL_DEFAULT);
88   if (ZSTD_isError(size))
89     {
90       delete[] *compressed_data;
91       return false;
92     }
93   *compressed_size = header_size + size;
94   return true;
95 }
96 #endif
97 
98 // Decompress COMPRESSED_DATA of size COMPRESSED_SIZE, into a buffer
99 // UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE.  Returns TRUE if it
100 // decompressed successfully, false if it failed.  The buffer, of
101 // appropriate size, is provided by the caller, and is typically part
102 // of the memory-mapped output file.
103 
104 static bool
zlib_decompress(const unsigned char * compressed_data,unsigned long compressed_size,unsigned char * uncompressed_data,unsigned long uncompressed_size)105 zlib_decompress(const unsigned char* compressed_data,
106 		unsigned long compressed_size,
107 		unsigned char* uncompressed_data,
108 		unsigned long uncompressed_size)
109 {
110   z_stream strm;
111   int rc;
112 
113   /* It is possible the section consists of several compressed
114      buffers concatenated together, so we uncompress in a loop.  */
115   strm.zalloc = NULL;
116   strm.zfree = NULL;
117   strm.opaque = NULL;
118   strm.avail_in = compressed_size;
119   strm.next_in = const_cast<Bytef*>(compressed_data);
120   strm.avail_out = uncompressed_size;
121 
122   rc = inflateInit(&strm);
123   while (strm.avail_in > 0)
124     {
125       if (rc != Z_OK)
126         return false;
127       strm.next_out = ((Bytef*) uncompressed_data
128                        + (uncompressed_size - strm.avail_out));
129       rc = inflate(&strm, Z_FINISH);
130       if (rc != Z_STREAM_END)
131         return false;
132       rc = inflateReset(&strm);
133     }
134   rc = inflateEnd(&strm);
135   if (rc != Z_OK || strm.avail_out != 0)
136     return false;
137 
138   return true;
139 }
140 
141 // Read the compression header of a compressed debug section and return
142 // the uncompressed size.
143 
144 uint64_t
get_uncompressed_size(const unsigned char * compressed_data,section_size_type compressed_size)145 get_uncompressed_size(const unsigned char* compressed_data,
146 		      section_size_type compressed_size)
147 {
148   const unsigned int zlib_header_size = 12;
149 
150   /* Verify the compression header.  Currently, we support only zlib
151      compression, so it should be "ZLIB" followed by the uncompressed
152      section size, 8 bytes in big-endian order.  */
153   if (compressed_size >= zlib_header_size
154       && strncmp(reinterpret_cast<const char*>(compressed_data),
155 		 "ZLIB", 4) == 0)
156     return elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4);
157   return -1ULL;
158 }
159 
160 // Decompress a compressed debug section directly into the output file.
161 
162 bool
decompress_input_section(const unsigned char * compressed_data,unsigned long compressed_size,unsigned char * uncompressed_data,unsigned long uncompressed_size,int size,bool big_endian,elfcpp::Elf_Xword sh_flags)163 decompress_input_section(const unsigned char* compressed_data,
164 			 unsigned long compressed_size,
165 			 unsigned char* uncompressed_data,
166 			 unsigned long uncompressed_size,
167 			 int size,
168 			 bool big_endian,
169 			 elfcpp::Elf_Xword sh_flags)
170 {
171   if ((sh_flags & elfcpp::SHF_COMPRESSED) != 0)
172     {
173       unsigned int compression_header_size;
174       unsigned int ch_type;
175       if (size == 32)
176 	{
177 	  compression_header_size = elfcpp::Elf_sizes<32>::chdr_size;
178 	  if (big_endian)
179 	    ch_type = elfcpp::Chdr<32, true> (compressed_data).get_ch_type();
180 	  else
181 	    ch_type = elfcpp::Chdr<32, false>(compressed_data).get_ch_type();
182 	}
183       else if (size == 64)
184 	{
185 	  compression_header_size = elfcpp::Elf_sizes<64>::chdr_size;
186 	  if (big_endian)
187 	    ch_type = elfcpp::Chdr<64, true>(compressed_data).get_ch_type();
188 	  else
189 	    ch_type = elfcpp::Chdr<64, false>(compressed_data).get_ch_type();
190 	}
191       else
192 	gold_unreachable();
193 
194 #ifdef HAVE_ZSTD
195       if (ch_type == elfcpp::ELFCOMPRESS_ZSTD)
196 	return !ZSTD_isError(
197 	    ZSTD_decompress(uncompressed_data, uncompressed_size,
198 			    compressed_data + compression_header_size,
199 			    compressed_size - compression_header_size));
200 #endif
201       if (ch_type == elfcpp::ELFCOMPRESS_ZLIB)
202 	return zlib_decompress(compressed_data + compression_header_size,
203 			       compressed_size - compression_header_size,
204 			       uncompressed_data, uncompressed_size);
205       return false;
206     }
207 
208   const unsigned int zlib_header_size = 12;
209 
210   /* Verify the compression header.  Currently, we support only zlib
211      compression, so it should be "ZLIB" followed by the uncompressed
212      section size, 8 bytes in big-endian order.  */
213   if (compressed_size >= zlib_header_size
214       && strncmp(reinterpret_cast<const char*>(compressed_data),
215 		 "ZLIB", 4) == 0)
216     {
217       unsigned long uncompressed_size_check =
218 	  elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4);
219       gold_assert(uncompressed_size_check == uncompressed_size);
220       return zlib_decompress(compressed_data + zlib_header_size,
221 			     compressed_size - zlib_header_size,
222 			     uncompressed_data,
223 			     uncompressed_size);
224     }
225   return false;
226 }
227 
228 // Class Output_compressed_section.
229 
230 // Set the final data size of a compressed section.  This is where
231 // we actually compress the section data.
232 
233 void
set_final_data_size()234 Output_compressed_section::set_final_data_size()
235 {
236   off_t uncompressed_size = this->postprocessing_buffer_size();
237 
238   // (Try to) compress the data.
239   unsigned long compressed_size;
240   unsigned char* uncompressed_data = this->postprocessing_buffer();
241 
242   // At this point the contents of all regular input sections will
243   // have been copied into the postprocessing buffer, and relocations
244   // will have been applied.  Now we need to copy in the contents of
245   // anything other than a regular input section.
246   this->write_to_postprocessing_buffer();
247 
248   bool success = false;
249   enum { none, gnu_zlib, gabi_zlib, zstd } compress;
250   int compression_header_size = 12;
251   const int size = parameters->target().get_size();
252   if (strcmp(this->options_->compress_debug_sections(), "zlib-gnu") == 0)
253     compress = gnu_zlib;
254   else if (strcmp(this->options_->compress_debug_sections(), "none") == 0)
255     compress = none;
256   else
257     {
258       if (strcmp(this->options_->compress_debug_sections(), "zstd") == 0)
259 	compress = zstd;
260       else
261 	compress = gabi_zlib;
262       if (size == 32)
263 	compression_header_size = elfcpp::Elf_sizes<32>::chdr_size;
264       else if (size == 64)
265 	compression_header_size = elfcpp::Elf_sizes<64>::chdr_size;
266       else
267 	gold_unreachable();
268     }
269   if (compress == gnu_zlib || compress == gabi_zlib)
270     success = zlib_compress(compression_header_size, uncompressed_data,
271 			    uncompressed_size, &this->data_,
272 			    &compressed_size);
273 #if HAVE_ZSTD
274   else if (compress == zstd)
275     success = zstd_compress(compression_header_size, uncompressed_data,
276 			    uncompressed_size, &this->data_,
277 			    &compressed_size);
278 #endif
279   if (success)
280     {
281       elfcpp::Elf_Xword flags = this->flags();
282       if (compress == gabi_zlib || compress == zstd)
283 	{
284 	  // Set the SHF_COMPRESSED bit.
285 	  flags |= elfcpp::SHF_COMPRESSED;
286 	  const bool is_big_endian = parameters->target().is_big_endian();
287 	  const unsigned int ch_type = compress == zstd
288 					   ? elfcpp::ELFCOMPRESS_ZSTD
289 					   : elfcpp::ELFCOMPRESS_ZLIB;
290 	  uint64_t addralign = this->addralign ();
291 	  if (size == 32)
292 	    {
293 	      if (is_big_endian)
294 		{
295 		  elfcpp::Chdr_write<32, true> chdr(this->data_);
296 		  chdr.put_ch_type(ch_type);
297 		  chdr.put_ch_size(uncompressed_size);
298 		  chdr.put_ch_addralign(addralign);
299 		}
300 	      else
301 		{
302 		  elfcpp::Chdr_write<32, false> chdr(this->data_);
303 		  chdr.put_ch_type(ch_type);
304 		  chdr.put_ch_size(uncompressed_size);
305 		  chdr.put_ch_addralign(addralign);
306 		}
307 	    }
308 	  else if (size == 64)
309 	    {
310 	      if (is_big_endian)
311 		{
312 		  elfcpp::Chdr_write<64, true> chdr(this->data_);
313 		  chdr.put_ch_type(ch_type);
314 		  chdr.put_ch_size(uncompressed_size);
315 		  chdr.put_ch_addralign(addralign);
316 		  // Clear the reserved field.
317 		  chdr.put_ch_reserved(0);
318 		}
319 	      else
320 		{
321 		  elfcpp::Chdr_write<64, false> chdr(this->data_);
322 		  chdr.put_ch_type(ch_type);
323 		  chdr.put_ch_size(uncompressed_size);
324 		  chdr.put_ch_addralign(addralign);
325 		  // Clear the reserved field.
326 		  chdr.put_ch_reserved(0);
327 		}
328 	    }
329 	  else
330 	    gold_unreachable();
331 	}
332       else
333 	{
334 	  // Write out the zlib header.
335 	  memcpy(this->data_, "ZLIB", 4);
336 	  elfcpp::Swap_unaligned<64, true>::writeval(this->data_ + 4,
337 						     uncompressed_size);
338 	  // This converts .debug_foo to .zdebug_foo
339 	  this->new_section_name_ = std::string(".z") + (this->name() + 1);
340 	  this->set_name(this->new_section_name_.c_str());
341 	}
342       this->set_flags(flags);
343       this->set_data_size(compressed_size);
344     }
345   else
346     {
347       gold_warning(_("not compressing section data: zlib error"));
348       gold_assert(this->data_ == NULL);
349       this->set_data_size(uncompressed_size);
350     }
351 }
352 
353 // Write out a compressed section.  If we couldn't compress, we just
354 // write it out as normal, uncompressed data.
355 
356 void
do_write(Output_file * of)357 Output_compressed_section::do_write(Output_file* of)
358 {
359   off_t offset = this->offset();
360   off_t data_size = this->data_size();
361   unsigned char* view = of->get_output_view(offset, data_size);
362   if (this->data_ == NULL)
363     memcpy(view, this->postprocessing_buffer(), data_size);
364   else
365     memcpy(view, this->data_, data_size);
366   of->write_output_view(offset, data_size, view);
367 }
368 
369 } // End namespace gold.
370