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