Fix build gdbserver build errors on arm, mips, aarch64.
[deliverable/binutils-gdb.git] / bfd / compress.c
CommitLineData
0acf065b 1/* Compressed section support (intended for debug sections).
b90efa5b 2 Copyright (C) 2008-2015 Free Software Foundation, Inc.
1b315056
CS
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
1b315056 21#include "sysdep.h"
243340ad 22#include <zlib.h>
1b315056
CS
23#include "bfd.h"
24#include "libbfd.h"
a953eec9 25#include "safe-ctype.h"
1b315056 26
151411f8
L
27#define MAX_COMPRESSION_HEADER_SIZE 24
28
4a114e3e
L
29static bfd_boolean
30decompress_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. */
de13ef81
NC
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);
4a114e3e
L
46 strm.avail_in = compressed_size - 12;
47 strm.next_in = (Bytef*) compressed_buffer + 12;
48 strm.avail_out = uncompressed_size;
49
a253d456 50 BFD_ASSERT (Z_OK == 0);
4a114e3e 51 rc = inflateInit (&strm);
a29a8af8 52 while (strm.avail_in > 0 && strm.avail_out > 0)
4a114e3e
L
53 {
54 if (rc != Z_OK)
a253d456 55 break;
4a114e3e
L
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)
a253d456 60 break;
4a114e3e
L
61 rc = inflateReset (&strm);
62 }
a253d456 63 rc |= inflateEnd (&strm);
82c6068a 64 return rc == Z_OK && strm.avail_out == 0;
4a114e3e 65}
4a114e3e 66
0b0732e1
L
67/* Compress data of the size specified in @var{uncompressed_size}
68 and pointed to by @var{uncompressed_buffer} using zlib and store
69 as the contents field. This function assumes the contents
18ece1de 70 field was allocated using bfd_malloc() or equivalent.
1b315056 71
151411f8
L
72 Return the uncompressed size if the full section contents is
73 compressed successfully. Otherwise return 0. */
1b315056 74
151411f8
L
75static bfd_size_type
76bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
243340ad 77 bfd_byte *uncompressed_buffer,
3e19fb8f 78 bfd_size_type uncompressed_size)
1b315056 79{
ae6a0217 80 uLong compressed_size;
151411f8
L
81 bfd_byte *buffer;
82 bfd_size_type buffer_size;
83 bfd_boolean decompress;
51509926
L
84#if defined(__GNUC__) && GCC_VERSION < 4007
85 /* Work around a GCC uninitialized warning bug fixed in GCC 4.7. */
86 int zlib_size = 0;
87#else
151411f8 88 int zlib_size;
51509926 89#endif
151411f8
L
90 int orig_compression_header_size;
91 int compression_header_size
92 = bfd_get_compression_header_size (abfd, NULL);
93 bfd_boolean compressed
94 = bfd_is_section_compressed_with_header (abfd, sec,
95 &orig_compression_header_size);
96
97 if (compressed)
98 {
99 /* We shouldn't decompress unsupported compressed section. */
100 if (orig_compression_header_size < 0)
101 abort ();
4a114e3e 102
151411f8
L
103 /* Different compression schemes. Just move the compressed section
104 contents to the right position. */
105 if (orig_compression_header_size == 0)
106 {
107 /* Convert it from .zdebug* section. Get the uncompressed
108 size first. */
109 zlib_size = uncompressed_size;
110 compressed_size = zlib_size + compression_header_size;
111 uncompressed_size = bfd_getb64 (uncompressed_buffer + 4);
112 }
113 else
114 {
115 /* Convert it to .zdebug* section. */
116 zlib_size = uncompressed_size - orig_compression_header_size;
117 compressed_size = zlib_size;
118 }
119 }
120 else
121 compressed_size = compressBound (uncompressed_size) + 12;
4281caad 122
151411f8
L
123 /* When converting from .zdebug* section, uncompress if it leads to
124 smaller size. */
125 if (compressed
126 && orig_compression_header_size == 0
127 && compressed_size > uncompressed_size)
4a114e3e 128 {
151411f8
L
129 decompress = TRUE;
130 buffer_size = uncompressed_size;
4a114e3e 131 }
151411f8
L
132 else
133 {
134 decompress = FALSE;
135 buffer_size = compressed_size + compression_header_size;
136 }
030aeb75 137 buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size);
151411f8
L
138 if (buffer == NULL)
139 return 0;
4a114e3e 140
151411f8 141 if (compressed)
8d001214 142 {
151411f8
L
143 sec->size = uncompressed_size;
144 if (decompress)
145 {
146 if (!decompress_contents (uncompressed_buffer, zlib_size,
147 buffer, uncompressed_size))
148 {
149 bfd_set_error (bfd_error_bad_value);
030aeb75 150 bfd_release (abfd, buffer);
151411f8
L
151 return 0;
152 }
153 free (uncompressed_buffer);
154 sec->contents = buffer;
155 sec->compress_status = COMPRESS_SECTION_DONE;
156 return uncompressed_size;
157 }
158 else
159 {
160 bfd_update_compression_header (abfd, buffer, sec);
161 memmove (buffer + compression_header_size,
162 uncompressed_buffer + orig_compression_header_size,
163 zlib_size);
164 }
8d001214
L
165 }
166 else
167 {
151411f8
L
168 bfd_size_type size = uncompressed_size;
169 int header_size = 12 + compression_header_size;
170 if (compress ((Bytef*) buffer + header_size,
171 &compressed_size,
172 (const Bytef*) uncompressed_buffer,
173 uncompressed_size) != Z_OK)
174 {
030aeb75 175 bfd_release (abfd, buffer);
151411f8
L
176 bfd_set_error (bfd_error_bad_value);
177 return 0;
178 }
179
180 compressed_size += header_size;
181 /* PR binutils/18087: If compression didn't make the section smaller,
3e19fb8f
L
182 just keep it uncompressed. */
183 if (compressed_size < uncompressed_size)
151411f8
L
184 {
185 bfd_update_compression_header (abfd, buffer, sec);
186
187 /* Write the zlib header. In this case, it should be "ZLIB"
188 followed by the uncompressed section size, 8 bytes in
189 big-endian order. */
190 memcpy (buffer + compression_header_size, "ZLIB", 4);
191 bfd_putb64 (size, buffer + compression_header_size + 4);
192 }
193 else
194 {
030aeb75
L
195 /* NOTE: There is a small memory leak here since
196 uncompressed_buffer is malloced and won't be freed. */
197 bfd_release (abfd, buffer);
151411f8
L
198 sec->contents = uncompressed_buffer;
199 sec->compress_status = COMPRESS_SECTION_NONE;
200 return uncompressed_size;
201 }
8d001214 202 }
4a114e3e 203
151411f8
L
204 free (uncompressed_buffer);
205 sec->contents = buffer;
206 sec->size = compressed_size;
207 sec->compress_status = COMPRESS_SECTION_DONE;
208
209 return uncompressed_size;
4a114e3e
L
210}
211
212/*
213FUNCTION
214 bfd_get_full_section_contents
215
216SYNOPSIS
217 bfd_boolean bfd_get_full_section_contents
218 (bfd *abfd, asection *section, bfd_byte **ptr);
219
220DESCRIPTION
221 Read all data from @var{section} in BFD @var{abfd}, decompress
222 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
68ffbac6 223 return @var{*ptr} with memory malloc'd by this function.
4a114e3e
L
224
225 Return @code{TRUE} if the full section contents is retrieved
06614111
NC
226 successfully. If the section has no contents then this function
227 returns @code{TRUE} but @var{*ptr} is set to NULL.
4a114e3e
L
228*/
229
230bfd_boolean
231bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
232{
e57278ef 233 bfd_size_type sz;
4a114e3e 234 bfd_byte *p = *ptr;
82c6068a 235 bfd_boolean ret;
38b774d2
AM
236 bfd_size_type save_size;
237 bfd_size_type save_rawsize;
4a114e3e 238 bfd_byte *compressed_buffer;
151411f8 239 unsigned int compression_header_size;
4a114e3e 240
e57278ef
AM
241 if (abfd->direction != write_direction && sec->rawsize != 0)
242 sz = sec->rawsize;
243 else
244 sz = sec->size;
4a114e3e 245 if (sz == 0)
06614111
NC
246 {
247 *ptr = NULL;
248 return TRUE;
249 }
4a114e3e
L
250
251 switch (sec->compress_status)
252 {
253 case COMPRESS_SECTION_NONE:
254 if (p == NULL)
255 {
0d13c96b 256 p = (bfd_byte *) bfd_malloc (sz);
4a114e3e
L
257 if (p == NULL)
258 return FALSE;
4a114e3e 259 }
06614111 260
82c6068a
AM
261 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
262 {
263 if (*ptr != p)
264 free (p);
265 return FALSE;
266 }
267 *ptr = p;
4a114e3e
L
268 return TRUE;
269
270 case DECOMPRESS_SECTION_SIZED:
82c6068a 271 /* Read in the full compressed section contents. */
38b774d2 272 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
82c6068a
AM
273 if (compressed_buffer == NULL)
274 return FALSE;
38b774d2
AM
275 save_rawsize = sec->rawsize;
276 save_size = sec->size;
82c6068a
AM
277 /* Clear rawsize, set size to compressed size and set compress_status
278 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
279 the uncompressed size, bfd_get_section_contents will fail. */
280 sec->rawsize = 0;
38b774d2 281 sec->size = sec->compressed_size;
82c6068a
AM
282 sec->compress_status = COMPRESS_SECTION_NONE;
283 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
38b774d2 284 0, sec->compressed_size);
82c6068a 285 /* Restore rawsize and size. */
38b774d2
AM
286 sec->rawsize = save_rawsize;
287 sec->size = save_size;
4a114e3e 288 sec->compress_status = DECOMPRESS_SECTION_SIZED;
82c6068a
AM
289 if (!ret)
290 goto fail_compressed;
4a114e3e 291
38b774d2
AM
292 if (p == NULL)
293 p = (bfd_byte *) bfd_malloc (sz);
294 if (p == NULL)
4a114e3e 295 goto fail_compressed;
4a114e3e 296
151411f8
L
297 compression_header_size = bfd_get_compression_header_size (abfd, sec);
298 if (!decompress_contents (compressed_buffer + compression_header_size,
299 sec->compressed_size, p, sz))
82c6068a
AM
300 {
301 bfd_set_error (bfd_error_bad_value);
38b774d2
AM
302 if (p != *ptr)
303 free (p);
82c6068a
AM
304 fail_compressed:
305 free (compressed_buffer);
306 return FALSE;
307 }
4a114e3e 308
82c6068a 309 free (compressed_buffer);
38b774d2
AM
310 *ptr = p;
311 return TRUE;
4a114e3e 312
82c6068a 313 case COMPRESS_SECTION_DONE:
db6b071a
NC
314 if (sec->contents == NULL)
315 return FALSE;
82c6068a
AM
316 if (p == NULL)
317 {
318 p = (bfd_byte *) bfd_malloc (sz);
319 if (p == NULL)
320 return FALSE;
321 *ptr = p;
322 }
06614111
NC
323 /* PR 17512; file: 5bc29788. */
324 if (p != sec->contents)
325 memcpy (p, sec->contents, sz);
82c6068a 326 return TRUE;
4a114e3e 327
82c6068a
AM
328 default:
329 abort ();
330 }
4a114e3e
L
331}
332
8a72cc6e
AM
333/*
334FUNCTION
335 bfd_cache_section_contents
336
337SYNOPSIS
338 void bfd_cache_section_contents
339 (asection *sec, void *contents);
340
341DESCRIPTION
342 Stash @var(contents) so any following reads of @var(sec) do
343 not need to decompress again.
344*/
345
346void
347bfd_cache_section_contents (asection *sec, void *contents)
348{
349 if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
350 sec->compress_status = COMPRESS_SECTION_DONE;
351 sec->contents = contents;
352 sec->flags |= SEC_IN_MEMORY;
353}
354
4a114e3e
L
355/*
356FUNCTION
151411f8 357 bfd_is_section_compressed_with_header
4a114e3e
L
358
359SYNOPSIS
151411f8
L
360 bfd_boolean bfd_is_section_compressed_with_header
361 (bfd *abfd, asection *section,
362 int *compression_header_size_p);
4a114e3e
L
363
364DESCRIPTION
151411f8
L
365 Return @code{TRUE} if @var{section} is compressed. Compression
366 header size is returned in @var{compression_header_size_p}. If
367 compression is unsupported, compression header size is returned
368 with -1.
4a114e3e
L
369*/
370
371bfd_boolean
151411f8
L
372bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
373 int *compression_header_size_p)
4a114e3e 374{
151411f8
L
375 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
376 int compression_header_size;
377 int header_size = 12;
64f40162
L
378 unsigned int saved = sec->compress_status;
379 bfd_boolean compressed;
380
151411f8
L
381 compression_header_size = bfd_get_compression_header_size (abfd, sec);
382 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
383 abort ();
384 header_size += compression_header_size;
385
64f40162
L
386 /* Don't decompress the section. */
387 sec->compress_status = COMPRESS_SECTION_NONE;
4a114e3e
L
388
389 /* Read the zlib header. In this case, it should be "ZLIB" followed
390 by the uncompressed section size, 8 bytes in big-endian order. */
151411f8
L
391 compressed = bfd_get_section_contents (abfd, sec, header, 0,
392 header_size)
393 && CONST_STRNEQ ((char*) header + compression_header_size,
394 "ZLIB");
64f40162 395
151411f8
L
396 if (compressed)
397 {
398 if (compression_header_size != 0)
399 {
400 bfd_size_type uncompressed_size
401 = bfd_getb64 ((bfd_byte *) header
402 + compression_header_size + 4);
403 if (!bfd_check_compression_header (abfd, header, sec,
404 uncompressed_size))
405 compression_header_size = -1;
406 }
407 /* Check for the pathalogical case of a debug string section that
408 contains the string ZLIB.... as the first entry. We assume that
409 no uncompressed .debug_str section would ever be big enough to
410 have the first byte of its (big-endian) size be non-zero. */
411 else if (strcmp (sec->name, ".debug_str") == 0
412 && ISPRINT (header[compression_header_size + 4]))
413 compressed = FALSE;
414 }
a953eec9 415
64f40162
L
416 /* Restore compress_status. */
417 sec->compress_status = saved;
151411f8 418 *compression_header_size_p = compression_header_size;
64f40162 419 return compressed;
4a114e3e
L
420}
421
151411f8
L
422/*
423FUNCTION
424 bfd_is_section_compressed
425
426SYNOPSIS
427 bfd_boolean bfd_is_section_compressed
428 (bfd *abfd, asection *section);
429
430DESCRIPTION
431 Return @code{TRUE} if @var{section} is compressed.
432*/
433
434bfd_boolean
435bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
436{
437 int compression_header_size;
438 return (bfd_is_section_compressed_with_header (abfd, sec,
439 &compression_header_size)
440 && compression_header_size >= 0);
441}
442
4a114e3e
L
443/*
444FUNCTION
445 bfd_init_section_decompress_status
446
447SYNOPSIS
448 bfd_boolean bfd_init_section_decompress_status
449 (bfd *abfd, asection *section);
450
451DESCRIPTION
452 Record compressed section size, update section size with
453 decompressed size and set compress_status to
454 DECOMPRESS_SECTION_SIZED.
455
456 Return @code{FALSE} if the section is not a valid compressed
18ece1de 457 section. Otherwise, return @code{TRUE}.
4a114e3e
L
458*/
459
460bfd_boolean
243340ad 461bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
4a114e3e 462{
151411f8
L
463 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
464 int compression_header_size;
465 int header_size = 12;
4a114e3e
L
466 bfd_size_type uncompressed_size;
467
151411f8
L
468 compression_header_size = bfd_get_compression_header_size (abfd, sec);
469 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
470 abort ();
471 header_size += compression_header_size;
472
4a114e3e
L
473 if (sec->rawsize != 0
474 || sec->contents != NULL
475 || sec->compress_status != COMPRESS_SECTION_NONE
151411f8 476 || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
4a114e3e
L
477 {
478 bfd_set_error (bfd_error_invalid_operation);
479 return FALSE;
480 }
1b315056
CS
481
482 /* Read the zlib header. In this case, it should be "ZLIB" followed
483 by the uncompressed section size, 8 bytes in big-endian order. */
151411f8 484 if (! CONST_STRNEQ ((char*) header + compression_header_size, "ZLIB"))
4a114e3e
L
485 {
486 bfd_set_error (bfd_error_wrong_format);
487 return FALSE;
488 }
489
151411f8
L
490 uncompressed_size = bfd_getb64 (header + compression_header_size + 4);
491 if (compression_header_size != 0
492 && !bfd_check_compression_header (abfd, header, sec,
493 uncompressed_size))
494 {
495 bfd_set_error (bfd_error_wrong_format);
496 return FALSE;
497 }
4a114e3e
L
498 sec->compressed_size = sec->size;
499 sec->size = uncompressed_size;
500 sec->compress_status = DECOMPRESS_SECTION_SIZED;
1b315056 501
4a114e3e 502 return TRUE;
4a114e3e
L
503}
504
505/*
506FUNCTION
507 bfd_init_section_compress_status
508
509SYNOPSIS
510 bfd_boolean bfd_init_section_compress_status
511 (bfd *abfd, asection *section);
512
513DESCRIPTION
514 If open for read, compress section, update section size with
515 compressed size and set compress_status to COMPRESS_SECTION_DONE.
516
517 Return @code{FALSE} if the section is not a valid compressed
18ece1de 518 section. Otherwise, return @code{TRUE}.
4a114e3e
L
519*/
520
521bfd_boolean
243340ad 522bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
4a114e3e 523{
4a114e3e
L
524 bfd_size_type uncompressed_size;
525 bfd_byte *uncompressed_buffer;
526 bfd_boolean ret;
527
528 /* Error if not opened for read. */
529 if (abfd->direction != read_direction
530 || sec->size == 0
531 || sec->rawsize != 0
532 || sec->contents != NULL
533 || sec->compress_status != COMPRESS_SECTION_NONE)
1b315056 534 {
4a114e3e
L
535 bfd_set_error (bfd_error_invalid_operation);
536 return FALSE;
1b315056 537 }
1b315056 538
4a114e3e
L
539 /* Read in the full section contents and compress it. */
540 uncompressed_size = sec->size;
541 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
542 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
543 0, uncompressed_size))
544 ret = FALSE;
545 else
151411f8
L
546 {
547 uncompressed_size = bfd_compress_section_contents (abfd, sec,
548 uncompressed_buffer,
3e19fb8f 549 uncompressed_size);
151411f8
L
550 ret = uncompressed_size != 0;
551 }
1b315056 552
4a114e3e 553 return ret;
1b315056 554}
0ce398f1
L
555
556/*
557FUNCTION
558 bfd_compress_section
559
560SYNOPSIS
561 bfd_boolean bfd_compress_section
562 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
563
564DESCRIPTION
565 If open for write, compress section, update section size with
566 compressed size and set compress_status to COMPRESS_SECTION_DONE.
567
568 Return @code{FALSE} if compression fail. Otherwise, return
569 @code{TRUE}.
570*/
571
572bfd_boolean
573bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
574{
575 bfd_size_type uncompressed_size = sec->size;
576
577 /* Error if not opened for write. */
578 if (abfd->direction != write_direction
579 || uncompressed_size == 0
580 || uncompressed_buffer == NULL
581 || sec->contents != NULL
582 || sec->compressed_size != 0
583 || sec->compress_status != COMPRESS_SECTION_NONE)
584 {
585 bfd_set_error (bfd_error_invalid_operation);
586 return FALSE;
587 }
588
589 /* Compress it. */
590 return bfd_compress_section_contents (abfd, sec, uncompressed_buffer,
3e19fb8f 591 uncompressed_size) != 0;
0ce398f1 592}
This page took 0.375906 seconds and 4 git commands to generate.