1 /* Compressed section support (intended for debug sections).
2    Copyright (C) 2008-2021 Free Software Foundation, Inc.
3 
4    This file is part of BFD, the Binary File Descriptor library.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #include "sysdep.h"
22 #include <zlib.h>
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "safe-ctype.h"
26 
27 #define MAX_COMPRESSION_HEADER_SIZE 24
28 
29 static bool
decompress_contents(bfd_byte * compressed_buffer,bfd_size_type compressed_size,bfd_byte * uncompressed_buffer,bfd_size_type uncompressed_size)30 decompress_contents (bfd_byte *compressed_buffer,
31 		     bfd_size_type compressed_size,
32 		     bfd_byte *uncompressed_buffer,
33 		     bfd_size_type uncompressed_size)
34 {
35   z_stream strm;
36   int rc;
37 
38   /* It is possible the section consists of several compressed
39      buffers concatenated together, so we uncompress in a loop.  */
40   /* PR 18313: The state field in the z_stream structure is supposed
41      to be invisible to the user (ie us), but some compilers will
42      still complain about it being used without initialisation.  So
43      we first zero the entire z_stream structure and then set the fields
44      that we need.  */
45   memset (& strm, 0, sizeof strm);
46   strm.avail_in = compressed_size;
47   strm.next_in = (Bytef*) compressed_buffer;
48   strm.avail_out = uncompressed_size;
49 
50   BFD_ASSERT (Z_OK == 0);
51   rc = inflateInit (&strm);
52   while (strm.avail_in > 0 && strm.avail_out > 0)
53     {
54       if (rc != Z_OK)
55 	break;
56       strm.next_out = ((Bytef*) uncompressed_buffer
57 		       + (uncompressed_size - strm.avail_out));
58       rc = inflate (&strm, Z_FINISH);
59       if (rc != Z_STREAM_END)
60 	break;
61       rc = inflateReset (&strm);
62     }
63   return inflateEnd (&strm) == Z_OK && rc == Z_OK && strm.avail_out == 0;
64 }
65 
66 /* Compress data of the size specified in @var{uncompressed_size}
67    and pointed to by @var{uncompressed_buffer} using zlib and store
68    as the contents field.  This function assumes the contents
69    field was allocated using bfd_malloc() or equivalent.
70 
71    Return the uncompressed size if the full section contents is
72    compressed successfully.  Otherwise return 0.  */
73 
74 static bfd_size_type
bfd_compress_section_contents(bfd * abfd,sec_ptr sec,bfd_byte * uncompressed_buffer,bfd_size_type uncompressed_size)75 bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
76 			       bfd_byte *uncompressed_buffer,
77 			       bfd_size_type uncompressed_size)
78 {
79   uLong compressed_size;
80   bfd_byte *buffer;
81   bfd_size_type buffer_size;
82   bool decompress;
83   int zlib_size = 0;
84   int orig_compression_header_size;
85   bfd_size_type orig_uncompressed_size;
86   unsigned int orig_uncompressed_alignment_pow;
87   int header_size = bfd_get_compression_header_size (abfd, NULL);
88   bool compressed
89     = bfd_is_section_compressed_with_header (abfd, sec,
90 					     &orig_compression_header_size,
91 					     &orig_uncompressed_size,
92 					     &orig_uncompressed_alignment_pow);
93 
94   /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
95      overhead in .zdebug* section.  */
96   if (!header_size)
97      header_size = 12;
98 
99   if (compressed)
100     {
101       /* We shouldn't decompress unsupported compressed section.  */
102       if (orig_compression_header_size < 0)
103 	abort ();
104 
105       /* Different compression schemes.  Just move the compressed section
106 	 contents to the right position. */
107       if (orig_compression_header_size == 0)
108 	{
109 	  /* Convert it from .zdebug* section.  Get the uncompressed
110 	     size first.  We need to subtract the 12-byte overhead in
111 	     .zdebug* section.  Set orig_compression_header_size to
112 	     the 12-bye overhead.  */
113 	  orig_compression_header_size = 12;
114 	  zlib_size = uncompressed_size - 12;
115 	}
116       else
117 	{
118 	  /* Convert it to .zdebug* section.  */
119 	  zlib_size = uncompressed_size - orig_compression_header_size;
120 	}
121 
122       /* Add the header size.  */
123       compressed_size = zlib_size + header_size;
124     }
125   else
126     compressed_size = compressBound (uncompressed_size) + header_size;
127 
128   /* Uncompress if it leads to smaller size.  */
129   if (compressed && compressed_size > orig_uncompressed_size)
130     {
131       decompress = true;
132       buffer_size = orig_uncompressed_size;
133     }
134   else
135     {
136       decompress = false;
137       buffer_size = compressed_size;
138     }
139   buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size);
140   if (buffer == NULL)
141     return 0;
142 
143   if (compressed)
144     {
145       sec->size = orig_uncompressed_size;
146       if (decompress)
147 	{
148 	  if (!decompress_contents (uncompressed_buffer
149 				    + orig_compression_header_size,
150 				    zlib_size, buffer, buffer_size))
151 	    {
152 	      bfd_set_error (bfd_error_bad_value);
153 	      bfd_release (abfd, buffer);
154 	      return 0;
155 	    }
156 	  free (uncompressed_buffer);
157 	  bfd_set_section_alignment (sec, orig_uncompressed_alignment_pow);
158 
159 	  sec->contents = buffer;
160 	  sec->compress_status = COMPRESS_SECTION_DONE;
161 	  return orig_uncompressed_size;
162 	}
163       else
164 	{
165 	  bfd_update_compression_header (abfd, buffer, sec);
166 	  memmove (buffer + header_size,
167 		   uncompressed_buffer + orig_compression_header_size,
168 		   zlib_size);
169 	}
170     }
171   else
172     {
173       if (compress ((Bytef*) buffer + header_size,
174 		    &compressed_size,
175 		    (const Bytef*) uncompressed_buffer,
176 		    uncompressed_size) != Z_OK)
177 	{
178 	  bfd_release (abfd, buffer);
179 	  bfd_set_error (bfd_error_bad_value);
180 	  return 0;
181 	}
182 
183       compressed_size += header_size;
184       /* PR binutils/18087: If compression didn't make the section smaller,
185 	 just keep it uncompressed.  */
186       if (compressed_size < uncompressed_size)
187 	bfd_update_compression_header (abfd, buffer, sec);
188       else
189 	{
190 	  /* NOTE: There is a small memory leak here since
191 	     uncompressed_buffer is malloced and won't be freed.  */
192 	  bfd_release (abfd, buffer);
193 	  sec->contents = uncompressed_buffer;
194 	  sec->compress_status = COMPRESS_SECTION_NONE;
195 	  return uncompressed_size;
196 	}
197     }
198 
199   free (uncompressed_buffer);
200   sec->contents = buffer;
201   sec->size = compressed_size;
202   sec->compress_status = COMPRESS_SECTION_DONE;
203 
204   return uncompressed_size;
205 }
206 
207 /*
208 FUNCTION
209 	bfd_get_full_section_contents
210 
211 SYNOPSIS
212 	bool bfd_get_full_section_contents
213 	  (bfd *abfd, asection *section, bfd_byte **ptr);
214 
215 DESCRIPTION
216 	Read all data from @var{section} in BFD @var{abfd}, decompress
217 	if needed, and store in @var{*ptr}.  If @var{*ptr} is NULL,
218 	return @var{*ptr} with memory malloc'd by this function.
219 
220 	Return @code{TRUE} if the full section contents is retrieved
221 	successfully.  If the section has no contents then this function
222 	returns @code{TRUE} but @var{*ptr} is set to NULL.
223 */
224 
225 bool
bfd_get_full_section_contents(bfd * abfd,sec_ptr sec,bfd_byte ** ptr)226 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
227 {
228   bfd_size_type sz;
229   bfd_byte *p = *ptr;
230   bool ret;
231   bfd_size_type save_size;
232   bfd_size_type save_rawsize;
233   bfd_byte *compressed_buffer;
234   unsigned int compression_header_size;
235 
236   if (abfd->direction != write_direction && sec->rawsize != 0)
237     sz = sec->rawsize;
238   else
239     sz = sec->size;
240   if (sz == 0)
241     {
242       *ptr = NULL;
243       return true;
244     }
245 
246   switch (sec->compress_status)
247     {
248     case COMPRESS_SECTION_NONE:
249       if (p == NULL)
250 	{
251 	  ufile_ptr filesize = bfd_get_file_size (abfd);
252 	  if (filesize > 0
253 	      && filesize < sz
254 	      /* PR 24753: Linker created sections can be larger than
255 		 the file size, eg if they are being used to hold stubs.  */
256 	      && (bfd_section_flags (sec) & SEC_LINKER_CREATED) == 0
257 	      /* PR 24753: Sections which have no content should also be
258 		 excluded as they contain no size on disk.  */
259 	      && (bfd_section_flags (sec) & SEC_HAS_CONTENTS) != 0
260 	      /* The MMO file format supports its own special compression
261 		 technique, but it uses COMPRESS_SECTION_NONE when loading
262 		 a section's contents.  */
263 	      && bfd_get_flavour (abfd) != bfd_target_mmo_flavour)
264 	    {
265 	      /* PR 24708: Avoid attempts to allocate a ridiculous amount
266 		 of memory.  */
267 	      bfd_set_error (bfd_error_no_memory);
268 	      _bfd_error_handler
269 		/* xgettext:c-format */
270 		(_("error: %pB(%pA) section size (%#" PRIx64 " bytes) is larger than file size (%#" PRIx64 " bytes)"),
271 		 abfd, sec, (uint64_t) sz, (uint64_t) filesize);
272 	      return false;
273 	    }
274 	  p = (bfd_byte *) bfd_malloc (sz);
275 	  if (p == NULL)
276 	    {
277 	      /* PR 20801: Provide a more helpful error message.  */
278 	      if (bfd_get_error () == bfd_error_no_memory)
279 		_bfd_error_handler
280 		  /* xgettext:c-format */
281 		  (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
282 		  abfd, sec, (uint64_t) sz);
283 	      return false;
284 	    }
285 	}
286 
287       if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
288 	{
289 	  if (*ptr != p)
290 	    free (p);
291 	  return false;
292 	}
293       *ptr = p;
294       return true;
295 
296     case DECOMPRESS_SECTION_SIZED:
297       /* Read in the full compressed section contents.  */
298       compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
299       if (compressed_buffer == NULL)
300 	return false;
301       save_rawsize = sec->rawsize;
302       save_size = sec->size;
303       /* Clear rawsize, set size to compressed size and set compress_status
304 	 to COMPRESS_SECTION_NONE.  If the compressed size is bigger than
305 	 the uncompressed size, bfd_get_section_contents will fail.  */
306       sec->rawsize = 0;
307       sec->size = sec->compressed_size;
308       sec->compress_status = COMPRESS_SECTION_NONE;
309       ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
310 				      0, sec->compressed_size);
311       /* Restore rawsize and size.  */
312       sec->rawsize = save_rawsize;
313       sec->size = save_size;
314       sec->compress_status = DECOMPRESS_SECTION_SIZED;
315       if (!ret)
316 	goto fail_compressed;
317 
318       if (p == NULL)
319 	p = (bfd_byte *) bfd_malloc (sz);
320       if (p == NULL)
321 	goto fail_compressed;
322 
323       compression_header_size = bfd_get_compression_header_size (abfd, sec);
324       if (compression_header_size == 0)
325 	/* Set header size to the zlib header size if it is a
326 	   SHF_COMPRESSED section.  */
327 	compression_header_size = 12;
328       if (!decompress_contents (compressed_buffer + compression_header_size,
329 				sec->compressed_size - compression_header_size, p, sz))
330 	{
331 	  bfd_set_error (bfd_error_bad_value);
332 	  if (p != *ptr)
333 	    free (p);
334 	fail_compressed:
335 	  free (compressed_buffer);
336 	  return false;
337 	}
338 
339       free (compressed_buffer);
340       *ptr = p;
341       return true;
342 
343     case COMPRESS_SECTION_DONE:
344       if (sec->contents == NULL)
345 	return false;
346       if (p == NULL)
347 	{
348 	  p = (bfd_byte *) bfd_malloc (sz);
349 	  if (p == NULL)
350 	    return false;
351 	  *ptr = p;
352 	}
353       /* PR 17512; file: 5bc29788.  */
354       if (p != sec->contents)
355 	memcpy (p, sec->contents, sz);
356       return true;
357 
358     default:
359       abort ();
360     }
361 }
362 
363 /*
364 FUNCTION
365 	bfd_cache_section_contents
366 
367 SYNOPSIS
368 	void bfd_cache_section_contents
369 	  (asection *sec, void *contents);
370 
371 DESCRIPTION
372 	Stash @var(contents) so any following reads of @var(sec) do
373 	not need to decompress again.
374 */
375 
376 void
bfd_cache_section_contents(asection * sec,void * contents)377 bfd_cache_section_contents (asection *sec, void *contents)
378 {
379   if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
380     sec->compress_status = COMPRESS_SECTION_DONE;
381   sec->contents = contents;
382   sec->flags |= SEC_IN_MEMORY;
383 }
384 
385 /*
386 FUNCTION
387 	bfd_is_section_compressed_with_header
388 
389 SYNOPSIS
390 	bool bfd_is_section_compressed_with_header
391 	  (bfd *abfd, asection *section,
392 	  int *compression_header_size_p,
393 	  bfd_size_type *uncompressed_size_p,
394 	  unsigned int *uncompressed_alignment_power_p);
395 
396 DESCRIPTION
397 	Return @code{TRUE} if @var{section} is compressed.  Compression
398 	header size is returned in @var{compression_header_size_p},
399 	uncompressed size is returned in @var{uncompressed_size_p}
400 	and the uncompressed data alignement power is returned in
401 	@var{uncompressed_align_pow_p}.  If compression is
402 	unsupported, compression header size is returned with -1
403 	and uncompressed size is returned with 0.
404 */
405 
406 bool
bfd_is_section_compressed_with_header(bfd * abfd,sec_ptr sec,int * compression_header_size_p,bfd_size_type * uncompressed_size_p,unsigned int * uncompressed_align_pow_p)407 bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
408 				       int *compression_header_size_p,
409 				       bfd_size_type *uncompressed_size_p,
410 				       unsigned int *uncompressed_align_pow_p)
411 {
412   bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
413   int compression_header_size;
414   int header_size;
415   unsigned int saved = sec->compress_status;
416   bool compressed;
417 
418   *uncompressed_align_pow_p = 0;
419 
420   compression_header_size = bfd_get_compression_header_size (abfd, sec);
421   if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
422     abort ();
423   header_size = compression_header_size ? compression_header_size : 12;
424 
425   /* Don't decompress the section.  */
426   sec->compress_status = COMPRESS_SECTION_NONE;
427 
428   /* Read the header.  */
429   if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
430     {
431       if (compression_header_size == 0)
432 	/* In this case, it should be "ZLIB" followed by the uncompressed
433 	   section size, 8 bytes in big-endian order.  */
434 	compressed = startswith ((char*) header , "ZLIB");
435       else
436 	compressed = true;
437     }
438   else
439     compressed = false;
440 
441   *uncompressed_size_p = sec->size;
442   if (compressed)
443     {
444       if (compression_header_size != 0)
445 	{
446 	  if (!bfd_check_compression_header (abfd, header, sec,
447 					     uncompressed_size_p,
448 					     uncompressed_align_pow_p))
449 	    compression_header_size = -1;
450 	}
451       /* Check for the pathalogical case of a debug string section that
452 	 contains the string ZLIB.... as the first entry.  We assume that
453 	 no uncompressed .debug_str section would ever be big enough to
454 	 have the first byte of its (big-endian) size be non-zero.  */
455       else if (strcmp (sec->name, ".debug_str") == 0
456 	       && ISPRINT (header[4]))
457 	compressed = false;
458       else
459 	*uncompressed_size_p = bfd_getb64 (header + 4);
460     }
461 
462   /* Restore compress_status.  */
463   sec->compress_status = saved;
464   *compression_header_size_p = compression_header_size;
465   return compressed;
466 }
467 
468 /*
469 FUNCTION
470 	bfd_is_section_compressed
471 
472 SYNOPSIS
473 	bool bfd_is_section_compressed
474 	  (bfd *abfd, asection *section);
475 
476 DESCRIPTION
477 	Return @code{TRUE} if @var{section} is compressed.
478 */
479 
480 bool
bfd_is_section_compressed(bfd * abfd,sec_ptr sec)481 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
482 {
483   int compression_header_size;
484   bfd_size_type uncompressed_size;
485   unsigned int uncompressed_align_power;
486   return (bfd_is_section_compressed_with_header (abfd, sec,
487 						 &compression_header_size,
488 						 &uncompressed_size,
489 						 &uncompressed_align_power)
490 	  && compression_header_size >= 0
491 	  && uncompressed_size > 0);
492 }
493 
494 /*
495 FUNCTION
496 	bfd_init_section_decompress_status
497 
498 SYNOPSIS
499 	bool bfd_init_section_decompress_status
500 	  (bfd *abfd, asection *section);
501 
502 DESCRIPTION
503 	Record compressed section size, update section size with
504 	decompressed size and set compress_status to
505 	DECOMPRESS_SECTION_SIZED.
506 
507 	Return @code{FALSE} if the section is not a valid compressed
508 	section.  Otherwise, return @code{TRUE}.
509 */
510 
511 bool
bfd_init_section_decompress_status(bfd * abfd,sec_ptr sec)512 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
513 {
514   bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
515   int compression_header_size;
516   int header_size;
517   bfd_size_type uncompressed_size;
518   unsigned int uncompressed_alignment_power = 0;
519 
520   compression_header_size = bfd_get_compression_header_size (abfd, sec);
521   if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
522     abort ();
523   header_size = compression_header_size ? compression_header_size : 12;
524 
525   /* Read the header.  */
526   if (sec->rawsize != 0
527       || sec->contents != NULL
528       || sec->compress_status != COMPRESS_SECTION_NONE
529       || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
530     {
531       bfd_set_error (bfd_error_invalid_operation);
532       return false;
533     }
534 
535   if (compression_header_size == 0)
536     {
537       /* In this case, it should be "ZLIB" followed by the uncompressed
538 	 section size, 8 bytes in big-endian order.  */
539       if (! startswith ((char*) header, "ZLIB"))
540 	{
541 	  bfd_set_error (bfd_error_wrong_format);
542 	  return false;
543 	}
544       uncompressed_size = bfd_getb64 (header + 4);
545     }
546   else if (!bfd_check_compression_header (abfd, header, sec,
547 					  &uncompressed_size,
548 					  &uncompressed_alignment_power))
549     {
550       bfd_set_error (bfd_error_wrong_format);
551       return false;
552     }
553 
554   sec->compressed_size = sec->size;
555   sec->size = uncompressed_size;
556   bfd_set_section_alignment (sec, uncompressed_alignment_power);
557   sec->compress_status = DECOMPRESS_SECTION_SIZED;
558 
559   return true;
560 }
561 
562 /*
563 FUNCTION
564 	bfd_init_section_compress_status
565 
566 SYNOPSIS
567 	bool bfd_init_section_compress_status
568 	  (bfd *abfd, asection *section);
569 
570 DESCRIPTION
571 	If open for read, compress section, update section size with
572 	compressed size and set compress_status to COMPRESS_SECTION_DONE.
573 
574 	Return @code{FALSE} if the section is not a valid compressed
575 	section.  Otherwise, return @code{TRUE}.
576 */
577 
578 bool
bfd_init_section_compress_status(bfd * abfd,sec_ptr sec)579 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
580 {
581   bfd_size_type uncompressed_size;
582   bfd_byte *uncompressed_buffer;
583 
584   /* Error if not opened for read.  */
585   if (abfd->direction != read_direction
586       || sec->size == 0
587       || sec->rawsize != 0
588       || sec->contents != NULL
589       || sec->compress_status != COMPRESS_SECTION_NONE)
590     {
591       bfd_set_error (bfd_error_invalid_operation);
592       return false;
593     }
594 
595   /* Read in the full section contents and compress it.  */
596   uncompressed_size = sec->size;
597   uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
598   /* PR 21431 */
599   if (uncompressed_buffer == NULL)
600     return false;
601 
602   if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
603 				 0, uncompressed_size))
604     return false;
605 
606   uncompressed_size = bfd_compress_section_contents (abfd, sec,
607 						     uncompressed_buffer,
608 						     uncompressed_size);
609   return uncompressed_size != 0;
610 }
611 
612 /*
613 FUNCTION
614 	bfd_compress_section
615 
616 SYNOPSIS
617 	bool bfd_compress_section
618 	  (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
619 
620 DESCRIPTION
621 	If open for write, compress section, update section size with
622 	compressed size and set compress_status to COMPRESS_SECTION_DONE.
623 
624 	Return @code{FALSE} if compression fail.  Otherwise, return
625 	@code{TRUE}.
626 */
627 
628 bool
bfd_compress_section(bfd * abfd,sec_ptr sec,bfd_byte * uncompressed_buffer)629 bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
630 {
631   bfd_size_type uncompressed_size = sec->size;
632 
633   /* Error if not opened for write.  */
634   if (abfd->direction != write_direction
635       || uncompressed_size == 0
636       || uncompressed_buffer == NULL
637       || sec->contents != NULL
638       || sec->compressed_size != 0
639       || sec->compress_status != COMPRESS_SECTION_NONE)
640     {
641       bfd_set_error (bfd_error_invalid_operation);
642       return false;
643     }
644 
645   /* Compress it.  */
646   return bfd_compress_section_contents (abfd, sec, uncompressed_buffer,
647 					uncompressed_size) != 0;
648 }
649