1 /* Compressed section support (intended for debug sections).
2 Copyright (C) 2008-2014 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 #include "safe-ctype.h"
31 decompress_contents (bfd_byte *compressed_buffer,
32 bfd_size_type compressed_size,
33 bfd_byte *uncompressed_buffer,
34 bfd_size_type uncompressed_size)
39 /* It is possible the section consists of several compressed
40 buffers concatenated together, so we uncompress in a loop. */
44 strm.avail_in = compressed_size - 12;
45 strm.next_in = (Bytef*) compressed_buffer + 12;
46 strm.avail_out = uncompressed_size;
48 BFD_ASSERT (Z_OK == 0);
49 rc = inflateInit (&strm);
50 while (strm.avail_in > 0 && strm.avail_out > 0)
54 strm.next_out = ((Bytef*) uncompressed_buffer
55 + (uncompressed_size - strm.avail_out));
56 rc = inflate (&strm, Z_FINISH);
57 if (rc != Z_STREAM_END)
59 rc = inflateReset (&strm);
61 rc |= inflateEnd (&strm);
62 return rc == Z_OK && strm.avail_out == 0;
68 bfd_compress_section_contents
71 bfd_boolean bfd_compress_section_contents
72 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer,
73 bfd_size_type uncompressed_size);
77 Compress data of the size specified in @var{uncompressed_size}
78 and pointed to by @var{uncompressed_buffer} using zlib and store
79 as the contents field. This function assumes the contents
80 field was allocated using bfd_malloc() or equivalent. If zlib
81 is not installed on this machine, the input is unmodified.
83 Return @code{TRUE} if the full section contents is compressed
88 bfd_compress_section_contents (bfd *abfd ATTRIBUTE_UNUSED,
89 sec_ptr sec ATTRIBUTE_UNUSED,
90 bfd_byte *uncompressed_buffer ATTRIBUTE_UNUSED,
91 bfd_size_type uncompressed_size ATTRIBUTE_UNUSED)
94 bfd_set_error (bfd_error_invalid_operation);
97 uLong compressed_size;
98 bfd_byte *compressed_buffer;
100 compressed_size = compressBound (uncompressed_size) + 12;
101 compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size);
103 if (compressed_buffer == NULL)
106 if (compress ((Bytef*) compressed_buffer + 12,
108 (const Bytef*) uncompressed_buffer,
109 uncompressed_size) != Z_OK)
111 free (compressed_buffer);
112 bfd_set_error (bfd_error_bad_value);
116 /* Write the zlib header. In this case, it should be "ZLIB" followed
117 by the uncompressed section size, 8 bytes in big-endian order. */
118 memcpy (compressed_buffer, "ZLIB", 4);
119 compressed_buffer[11] = uncompressed_size; uncompressed_size >>= 8;
120 compressed_buffer[10] = uncompressed_size; uncompressed_size >>= 8;
121 compressed_buffer[9] = uncompressed_size; uncompressed_size >>= 8;
122 compressed_buffer[8] = uncompressed_size; uncompressed_size >>= 8;
123 compressed_buffer[7] = uncompressed_size; uncompressed_size >>= 8;
124 compressed_buffer[6] = uncompressed_size; uncompressed_size >>= 8;
125 compressed_buffer[5] = uncompressed_size; uncompressed_size >>= 8;
126 compressed_buffer[4] = uncompressed_size;
127 compressed_size += 12;
129 /* Free the uncompressed contents if we compress in place. */
130 if (uncompressed_buffer == sec->contents)
131 free (uncompressed_buffer);
133 sec->contents = compressed_buffer;
134 sec->size = compressed_size;
135 sec->compress_status = COMPRESS_SECTION_DONE;
138 #endif /* HAVE_ZLIB_H */
143 bfd_get_full_section_contents
146 bfd_boolean bfd_get_full_section_contents
147 (bfd *abfd, asection *section, bfd_byte **ptr);
150 Read all data from @var{section} in BFD @var{abfd}, decompress
151 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
152 return @var{*ptr} with memory malloc'd by this function.
154 Return @code{TRUE} if the full section contents is retrieved
159 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
165 bfd_size_type save_size;
166 bfd_size_type save_rawsize;
167 bfd_byte *compressed_buffer;
170 if (abfd->direction != write_direction && sec->rawsize != 0)
177 switch (sec->compress_status)
179 case COMPRESS_SECTION_NONE:
180 /* PR binutils/17512: Avoid malloc or file reading errors due to
181 ridiculous section sizes. But ignore linker created objects
182 with no contents (yet). */
183 if (bfd_get_size (abfd) > 0
184 && sz > (bfd_size_type) bfd_get_size (abfd))
189 p = (bfd_byte *) bfd_malloc (sz);
193 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
202 case DECOMPRESS_SECTION_SIZED:
204 bfd_set_error (bfd_error_invalid_operation);
207 /* Read in the full compressed section contents. */
208 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
209 if (compressed_buffer == NULL)
211 save_rawsize = sec->rawsize;
212 save_size = sec->size;
213 /* Clear rawsize, set size to compressed size and set compress_status
214 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
215 the uncompressed size, bfd_get_section_contents will fail. */
217 sec->size = sec->compressed_size;
218 sec->compress_status = COMPRESS_SECTION_NONE;
219 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
220 0, sec->compressed_size);
221 /* Restore rawsize and size. */
222 sec->rawsize = save_rawsize;
223 sec->size = save_size;
224 sec->compress_status = DECOMPRESS_SECTION_SIZED;
226 goto fail_compressed;
229 p = (bfd_byte *) bfd_malloc (sz);
231 goto fail_compressed;
233 if (!decompress_contents (compressed_buffer, sec->compressed_size, p, sz))
235 bfd_set_error (bfd_error_bad_value);
239 free (compressed_buffer);
243 free (compressed_buffer);
248 case COMPRESS_SECTION_DONE:
251 p = (bfd_byte *) bfd_malloc (sz);
256 memcpy (p, sec->contents, sz);
266 bfd_cache_section_contents
269 void bfd_cache_section_contents
270 (asection *sec, void *contents);
273 Stash @var(contents) so any following reads of @var(sec) do
274 not need to decompress again.
278 bfd_cache_section_contents (asection *sec, void *contents)
280 if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
281 sec->compress_status = COMPRESS_SECTION_DONE;
282 sec->contents = contents;
283 sec->flags |= SEC_IN_MEMORY;
289 bfd_is_section_compressed
292 bfd_boolean bfd_is_section_compressed
293 (bfd *abfd, asection *section);
296 Return @code{TRUE} if @var{section} is compressed.
300 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
302 bfd_byte compressed_buffer [12];
303 unsigned int saved = sec->compress_status;
304 bfd_boolean compressed;
306 /* Don't decompress the section. */
307 sec->compress_status = COMPRESS_SECTION_NONE;
309 /* Read the zlib header. In this case, it should be "ZLIB" followed
310 by the uncompressed section size, 8 bytes in big-endian order. */
311 compressed = (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12)
312 && CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"));
314 /* Check for the pathalogical case of a debug string section that
315 contains the string ZLIB.... as the first entry. We assume that
316 no uncompressed .debug_str section would ever be big enough to
317 have the first byte of its (big-endian) size be non-zero. */
319 && strcmp (sec->name, ".debug_str") == 0
320 && ISPRINT (compressed_buffer[4]))
323 /* Restore compress_status. */
324 sec->compress_status = saved;
330 bfd_init_section_decompress_status
333 bfd_boolean bfd_init_section_decompress_status
334 (bfd *abfd, asection *section);
337 Record compressed section size, update section size with
338 decompressed size and set compress_status to
339 DECOMPRESS_SECTION_SIZED.
341 Return @code{FALSE} if the section is not a valid compressed
342 section or zlib is not installed on this machine. Otherwise,
347 bfd_init_section_decompress_status (bfd *abfd ATTRIBUTE_UNUSED,
348 sec_ptr sec ATTRIBUTE_UNUSED)
351 bfd_set_error (bfd_error_invalid_operation);
354 bfd_byte compressed_buffer [12];
355 bfd_size_type uncompressed_size;
357 if (sec->rawsize != 0
358 || sec->contents != NULL
359 || sec->compress_status != COMPRESS_SECTION_NONE
360 || !bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12))
362 bfd_set_error (bfd_error_invalid_operation);
366 /* Read the zlib header. In this case, it should be "ZLIB" followed
367 by the uncompressed section size, 8 bytes in big-endian order. */
368 if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"))
370 bfd_set_error (bfd_error_wrong_format);
374 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
375 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
376 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
377 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
378 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
379 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
380 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
381 uncompressed_size += compressed_buffer[11];
383 sec->compressed_size = sec->size;
384 sec->size = uncompressed_size;
385 sec->compress_status = DECOMPRESS_SECTION_SIZED;
393 bfd_init_section_compress_status
396 bfd_boolean bfd_init_section_compress_status
397 (bfd *abfd, asection *section);
400 If open for read, compress section, update section size with
401 compressed size and set compress_status to COMPRESS_SECTION_DONE.
403 Return @code{FALSE} if the section is not a valid compressed
404 section or zlib is not installed on this machine. Otherwise,
409 bfd_init_section_compress_status (bfd *abfd ATTRIBUTE_UNUSED,
410 sec_ptr sec ATTRIBUTE_UNUSED)
413 bfd_set_error (bfd_error_invalid_operation);
416 bfd_size_type uncompressed_size;
417 bfd_byte *uncompressed_buffer;
420 /* Error if not opened for read. */
421 if (abfd->direction != read_direction
424 || sec->contents != NULL
425 || sec->compress_status != COMPRESS_SECTION_NONE)
427 bfd_set_error (bfd_error_invalid_operation);
431 /* Read in the full section contents and compress it. */
432 uncompressed_size = sec->size;
433 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
434 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
435 0, uncompressed_size))
438 ret = bfd_compress_section_contents (abfd, sec,
442 free (uncompressed_buffer);