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