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