Rev 5197 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5197 | Rev 6324 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /* Compressed section support (intended for debug sections). |
1 | /* Compressed section support (intended for debug sections). |
2 | Copyright 2008, 2010, 2011, 2012 |
- | |
3 | Free Software Foundation, Inc. |
2 | Copyright (C) 2008-2015 Free Software Foundation, Inc. |
Line 4... | Line 3... | ||
4 | 3 | ||
Line 5... | Line 4... | ||
5 | This file is part of BFD, the Binary File Descriptor library. |
4 | This file is part of BFD, the Binary File Descriptor library. |
6 | 5 | ||
Line 18... | Line 17... | ||
18 | along with this program; if not, write to the Free Software |
17 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ |
19 | MA 02110-1301, USA. */ |
Line 21... | Line 20... | ||
21 | 20 | ||
- | 21 | #include "sysdep.h" |
|
22 | #include "sysdep.h" |
22 | #include |
23 | #include "bfd.h" |
23 | #include "bfd.h" |
24 | #include "libbfd.h" |
- | |
25 | #ifdef HAVE_ZLIB_H |
24 | #include "libbfd.h" |
26 | #include |
25 | #include "safe-ctype.h" |
- | 26 | ||
Line 27... | Line -... | ||
27 | #endif |
- | |
28 | 27 | #define MAX_COMPRESSION_HEADER_SIZE 24 |
|
29 | #ifdef HAVE_ZLIB_H |
28 | |
30 | static bfd_boolean |
29 | static bfd_boolean |
31 | decompress_contents (bfd_byte *compressed_buffer, |
30 | decompress_contents (bfd_byte *compressed_buffer, |
32 | bfd_size_type compressed_size, |
31 | bfd_size_type compressed_size, |
Line 36... | Line 35... | ||
36 | z_stream strm; |
35 | z_stream strm; |
37 | int rc; |
36 | int rc; |
Line 38... | Line 37... | ||
38 | 37 | ||
39 | /* It is possible the section consists of several compressed |
38 | /* It is possible the section consists of several compressed |
- | 39 | buffers concatenated together, so we uncompress in a loop. */ |
|
- | 40 | /* PR 18313: The state field in the z_stream structure is supposed |
|
40 | buffers concatenated together, so we uncompress in a loop. */ |
41 | to be invisible to the user (ie us), but some compilers will |
- | 42 | still complain about it being used without initialisation. So |
|
41 | strm.zalloc = NULL; |
43 | we first zero the entire z_stream structure and then set the fields |
42 | strm.zfree = NULL; |
44 | that we need. */ |
43 | strm.opaque = NULL; |
45 | memset (& strm, 0, sizeof strm); |
44 | strm.avail_in = compressed_size - 12; |
46 | strm.avail_in = compressed_size; |
45 | strm.next_in = (Bytef*) compressed_buffer + 12; |
47 | strm.next_in = (Bytef*) compressed_buffer; |
Line 46... | Line 48... | ||
46 | strm.avail_out = uncompressed_size; |
48 | strm.avail_out = uncompressed_size; |
47 | 49 | ||
48 | BFD_ASSERT (Z_OK == 0); |
50 | BFD_ASSERT (Z_OK == 0); |
Line 59... | Line 61... | ||
59 | rc = inflateReset (&strm); |
61 | rc = inflateReset (&strm); |
60 | } |
62 | } |
61 | rc |= inflateEnd (&strm); |
63 | rc |= inflateEnd (&strm); |
62 | return rc == Z_OK && strm.avail_out == 0; |
64 | return rc == Z_OK && strm.avail_out == 0; |
63 | } |
65 | } |
64 | #endif |
- | |
65 | - | ||
66 | /* |
- | |
67 | FUNCTION |
- | |
68 | bfd_compress_section_contents |
- | |
69 | - | ||
70 | SYNOPSIS |
- | |
71 | bfd_boolean bfd_compress_section_contents |
- | |
72 | (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer, |
- | |
73 | bfd_size_type uncompressed_size); |
- | |
74 | - | ||
75 | DESCRIPTION |
- | |
Line 76... | Line 66... | ||
76 | 66 | ||
77 | Compress data of the size specified in @var{uncompressed_size} |
67 | /* Compress data of the size specified in @var{uncompressed_size} |
78 | and pointed to by @var{uncompressed_buffer} using zlib and store |
68 | and pointed to by @var{uncompressed_buffer} using zlib and store |
79 | as the contents field. This function assumes the contents |
69 | as the contents field. This function assumes the contents |
80 | field was allocated using bfd_malloc() or equivalent. If zlib |
- | |
Line 81... | Line 70... | ||
81 | is not installed on this machine, the input is unmodified. |
70 | field was allocated using bfd_malloc() or equivalent. |
82 | 71 | ||
83 | Return @code{TRUE} if the full section contents is compressed |
- | |
Line 84... | Line 72... | ||
84 | successfully. |
72 | Return the uncompressed size if the full section contents is |
85 | */ |
73 | compressed successfully. Otherwise return 0. */ |
86 | - | ||
87 | bfd_boolean |
74 | |
88 | bfd_compress_section_contents (bfd *abfd ATTRIBUTE_UNUSED, |
75 | static bfd_size_type |
89 | sec_ptr sec ATTRIBUTE_UNUSED, |
76 | bfd_compress_section_contents (bfd *abfd, sec_ptr sec, |
90 | bfd_byte *uncompressed_buffer ATTRIBUTE_UNUSED, |
- | |
91 | bfd_size_type uncompressed_size ATTRIBUTE_UNUSED) |
- | |
92 | { |
- | |
93 | #ifndef HAVE_ZLIB_H |
- | |
94 | bfd_set_error (bfd_error_invalid_operation); |
77 | bfd_byte *uncompressed_buffer, |
95 | return FALSE; |
78 | bfd_size_type uncompressed_size) |
- | 79 | { |
|
- | 80 | uLong compressed_size; |
|
- | 81 | bfd_byte *buffer; |
|
- | 82 | bfd_size_type buffer_size; |
|
- | 83 | bfd_boolean decompress; |
|
- | 84 | int zlib_size = 0; |
|
- | 85 | int orig_compression_header_size; |
|
- | 86 | bfd_size_type orig_uncompressed_size; |
|
- | 87 | int header_size = bfd_get_compression_header_size (abfd, NULL); |
|
- | 88 | bfd_boolean compressed |
|
- | 89 | = bfd_is_section_compressed_with_header (abfd, sec, |
|
- | 90 | &orig_compression_header_size, |
|
- | 91 | &orig_uncompressed_size); |
|
- | 92 | ||
- | 93 | /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size, |
|
Line -... | Line 94... | ||
- | 94 | overhead in .zdebug* section. */ |
|
- | 95 | if (!header_size) |
|
96 | #else |
96 | header_size = 12; |
97 | uLong compressed_size; |
97 | |
- | 98 | if (compressed) |
|
Line -... | Line 99... | ||
- | 99 | { |
|
- | 100 | /* We shouldn't decompress unsupported compressed section. */ |
|
98 | bfd_byte *compressed_buffer; |
101 | if (orig_compression_header_size < 0) |
- | 102 | abort (); |
|
- | 103 | ||
- | 104 | /* Different compression schemes. Just move the compressed section |
|
- | 105 | contents to the right position. */ |
|
- | 106 | if (orig_compression_header_size == 0) |
|
- | 107 | { |
|
- | 108 | /* Convert it from .zdebug* section. Get the uncompressed |
|
- | 109 | size first. We need to substract the 12-byte overhead in |
|
99 | 110 | .zdebug* section. Set orig_compression_header_size to |
|
- | 111 | the 12-bye overhead. */ |
|
- | 112 | orig_compression_header_size = 12; |
|
- | 113 | zlib_size = uncompressed_size - 12; |
|
- | 114 | } |
|
Line -... | Line 115... | ||
- | 115 | else |
|
- | 116 | { |
|
- | 117 | /* Convert it to .zdebug* section. */ |
|
- | 118 | zlib_size = uncompressed_size - orig_compression_header_size; |
|
- | 119 | } |
|
- | 120 | ||
- | 121 | /* Add the header size. */ |
|
- | 122 | compressed_size = zlib_size + header_size; |
|
- | 123 | } |
|
- | 124 | else |
|
- | 125 | compressed_size = compressBound (uncompressed_size) + header_size; |
|
- | 126 | ||
- | 127 | /* Uncompress if it leads to smaller size. */ |
|
- | 128 | if (compressed && compressed_size > orig_uncompressed_size) |
|
- | 129 | { |
|
- | 130 | decompress = TRUE; |
|
- | 131 | buffer_size = orig_uncompressed_size; |
|
- | 132 | } |
|
- | 133 | else |
|
- | 134 | { |
|
- | 135 | decompress = FALSE; |
|
- | 136 | buffer_size = compressed_size; |
|
- | 137 | } |
|
- | 138 | buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size); |
|
- | 139 | if (buffer == NULL) |
|
- | 140 | return 0; |
|
100 | compressed_size = compressBound (uncompressed_size) + 12; |
141 | |
- | 142 | if (compressed) |
|
- | 143 | { |
|
- | 144 | sec->size = orig_uncompressed_size; |
|
- | 145 | if (decompress) |
|
- | 146 | { |
|
- | 147 | if (!decompress_contents (uncompressed_buffer |
|
- | 148 | + orig_compression_header_size, |
|
- | 149 | zlib_size, buffer, buffer_size)) |
|
- | 150 | { |
|
- | 151 | bfd_set_error (bfd_error_bad_value); |
|
- | 152 | bfd_release (abfd, buffer); |
|
- | 153 | return 0; |
|
- | 154 | } |
|
- | 155 | free (uncompressed_buffer); |
|
- | 156 | sec->contents = buffer; |
|
- | 157 | sec->compress_status = COMPRESS_SECTION_DONE; |
|
- | 158 | return orig_uncompressed_size; |
|
- | 159 | } |
|
- | 160 | else |
|
- | 161 | { |
|
- | 162 | bfd_update_compression_header (abfd, buffer, sec); |
|
- | 163 | memmove (buffer + header_size, |
|
- | 164 | uncompressed_buffer + orig_compression_header_size, |
|
101 | compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size); |
165 | zlib_size); |
102 | 166 | } |
|
103 | if (compressed_buffer == NULL) |
167 | } |
104 | return FALSE; |
168 | else |
105 | 169 | { |
|
106 | if (compress ((Bytef*) compressed_buffer + 12, |
170 | if (compress ((Bytef*) buffer + header_size, |
107 | &compressed_size, |
171 | &compressed_size, |
108 | (const Bytef*) uncompressed_buffer, |
172 | (const Bytef*) uncompressed_buffer, |
Line 109... | Line 173... | ||
109 | uncompressed_size) != Z_OK) |
173 | uncompressed_size) != Z_OK) |
110 | { |
174 | { |
111 | free (compressed_buffer); |
175 | bfd_release (abfd, buffer); |
112 | bfd_set_error (bfd_error_bad_value); |
176 | bfd_set_error (bfd_error_bad_value); |
113 | return FALSE; |
177 | return 0; |
- | 178 | } |
|
- | 179 | ||
114 | } |
180 | compressed_size += header_size; |
115 | 181 | /* PR binutils/18087: If compression didn't make the section smaller, |
|
116 | /* Write the zlib header. In this case, it should be "ZLIB" followed |
182 | just keep it uncompressed. */ |
117 | by the uncompressed section size, 8 bytes in big-endian order. */ |
183 | if (compressed_size < uncompressed_size) |
118 | memcpy (compressed_buffer, "ZLIB", 4); |
184 | bfd_update_compression_header (abfd, buffer, sec); |
119 | compressed_buffer[11] = uncompressed_size; uncompressed_size >>= 8; |
185 | else |
- | 186 | { |
|
120 | compressed_buffer[10] = uncompressed_size; uncompressed_size >>= 8; |
187 | /* NOTE: There is a small memory leak here since |
Line 121... | Line -... | ||
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; |
188 | uncompressed_buffer is malloced and won't be freed. */ |
124 | compressed_buffer[6] = uncompressed_size; uncompressed_size >>= 8; |
- | |
125 | compressed_buffer[5] = uncompressed_size; uncompressed_size >>= 8; |
189 | bfd_release (abfd, buffer); |
126 | compressed_buffer[4] = uncompressed_size; |
190 | sec->contents = uncompressed_buffer; |
127 | compressed_size += 12; |
191 | sec->compress_status = COMPRESS_SECTION_NONE; |
Line 128... | Line 192... | ||
128 | 192 | return uncompressed_size; |
|
129 | /* Free the uncompressed contents if we compress in place. */ |
- | |
130 | if (uncompressed_buffer == sec->contents) |
193 | } |
Line 131... | Line 194... | ||
131 | free (uncompressed_buffer); |
194 | } |
132 | 195 | ||
133 | sec->contents = compressed_buffer; |
196 | free (uncompressed_buffer); |
Line 150... | Line 213... | ||
150 | Read all data from @var{section} in BFD @var{abfd}, decompress |
213 | Read all data from @var{section} in BFD @var{abfd}, decompress |
151 | if needed, and store in @var{*ptr}. If @var{*ptr} is NULL, |
214 | if needed, and store in @var{*ptr}. If @var{*ptr} is NULL, |
152 | return @var{*ptr} with memory malloc'd by this function. |
215 | return @var{*ptr} with memory malloc'd by this function. |
Line 153... | Line 216... | ||
153 | 216 | ||
154 | Return @code{TRUE} if the full section contents is retrieved |
217 | Return @code{TRUE} if the full section contents is retrieved |
- | 218 | successfully. If the section has no contents then this function |
|
155 | successfully. |
219 | returns @code{TRUE} but @var{*ptr} is set to NULL. |
Line 156... | Line 220... | ||
156 | */ |
220 | */ |
157 | 221 | ||
158 | bfd_boolean |
222 | bfd_boolean |
159 | bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr) |
223 | bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr) |
160 | { |
224 | { |
161 | bfd_size_type sz; |
- | |
162 | bfd_byte *p = *ptr; |
225 | bfd_size_type sz; |
163 | #ifdef HAVE_ZLIB_H |
226 | bfd_byte *p = *ptr; |
164 | bfd_boolean ret; |
227 | bfd_boolean ret; |
165 | bfd_size_type save_size; |
228 | bfd_size_type save_size; |
166 | bfd_size_type save_rawsize; |
229 | bfd_size_type save_rawsize; |
Line 167... | Line 230... | ||
167 | bfd_byte *compressed_buffer; |
230 | bfd_byte *compressed_buffer; |
168 | #endif |
231 | unsigned int compression_header_size; |
169 | 232 | ||
170 | if (abfd->direction != write_direction && sec->rawsize != 0) |
233 | if (abfd->direction != write_direction && sec->rawsize != 0) |
171 | sz = sec->rawsize; |
234 | sz = sec->rawsize; |
- | 235 | else |
|
- | 236 | sz = sec->size; |
|
172 | else |
237 | if (sz == 0) |
- | 238 | { |
|
Line 173... | Line 239... | ||
173 | sz = sec->size; |
239 | *ptr = NULL; |
174 | if (sz == 0) |
240 | return TRUE; |
175 | return TRUE; |
241 | } |
176 | 242 | ||
177 | switch (sec->compress_status) |
243 | switch (sec->compress_status) |
178 | { |
244 | { |
179 | case COMPRESS_SECTION_NONE: |
245 | case COMPRESS_SECTION_NONE: |
180 | if (p == NULL) |
246 | if (p == NULL) |
181 | { |
247 | { |
- | 248 | p = (bfd_byte *) bfd_malloc (sz); |
|
182 | p = (bfd_byte *) bfd_malloc (sz); |
249 | if (p == NULL) |
183 | if (p == NULL) |
250 | return FALSE; |
184 | return FALSE; |
251 | } |
185 | } |
252 | |
186 | if (!bfd_get_section_contents (abfd, sec, p, 0, sz)) |
253 | if (!bfd_get_section_contents (abfd, sec, p, 0, sz)) |
187 | { |
254 | { |
188 | if (*ptr != p) |
255 | if (*ptr != p) |
189 | free (p); |
256 | free (p); |
Line 190... | Line 257... | ||
190 | return FALSE; |
257 | return FALSE; |
191 | } |
- | |
192 | *ptr = p; |
- | |
193 | return TRUE; |
- | |
194 | - | ||
195 | case DECOMPRESS_SECTION_SIZED: |
258 | } |
196 | #ifndef HAVE_ZLIB_H |
259 | *ptr = p; |
197 | bfd_set_error (bfd_error_invalid_operation); |
260 | return TRUE; |
198 | return FALSE; |
261 | |
199 | #else |
262 | case DECOMPRESS_SECTION_SIZED: |
Line 221... | Line 284... | ||
221 | if (p == NULL) |
284 | if (p == NULL) |
222 | p = (bfd_byte *) bfd_malloc (sz); |
285 | p = (bfd_byte *) bfd_malloc (sz); |
223 | if (p == NULL) |
286 | if (p == NULL) |
224 | goto fail_compressed; |
287 | goto fail_compressed; |
Line -... | Line 288... | ||
- | 288 | ||
- | 289 | compression_header_size = bfd_get_compression_header_size (abfd, sec); |
|
- | 290 | if (compression_header_size == 0) |
|
- | 291 | /* Set header size to the zlib header size if it is a |
|
- | 292 | SHF_COMPRESSED section. */ |
|
225 | 293 | compression_header_size = 12; |
|
- | 294 | if (!decompress_contents (compressed_buffer + compression_header_size, |
|
226 | if (!decompress_contents (compressed_buffer, sec->compressed_size, p, sz)) |
295 | sec->compressed_size, p, sz)) |
227 | { |
296 | { |
228 | bfd_set_error (bfd_error_bad_value); |
297 | bfd_set_error (bfd_error_bad_value); |
229 | if (p != *ptr) |
298 | if (p != *ptr) |
230 | free (p); |
299 | free (p); |
Line 234... | Line 303... | ||
234 | } |
303 | } |
Line 235... | Line 304... | ||
235 | 304 | ||
236 | free (compressed_buffer); |
305 | free (compressed_buffer); |
237 | *ptr = p; |
306 | *ptr = p; |
238 | return TRUE; |
- | |
Line 239... | Line 307... | ||
239 | #endif |
307 | return TRUE; |
- | 308 | ||
- | 309 | case COMPRESS_SECTION_DONE: |
|
240 | 310 | if (sec->contents == NULL) |
|
241 | case COMPRESS_SECTION_DONE: |
311 | return FALSE; |
242 | if (p == NULL) |
312 | if (p == NULL) |
243 | { |
313 | { |
244 | p = (bfd_byte *) bfd_malloc (sz); |
314 | p = (bfd_byte *) bfd_malloc (sz); |
245 | if (p == NULL) |
315 | if (p == NULL) |
246 | return FALSE; |
316 | return FALSE; |
- | 317 | *ptr = p; |
|
- | 318 | } |
|
247 | *ptr = p; |
319 | /* PR 17512; file: 5bc29788. */ |
248 | } |
320 | if (p != sec->contents) |
Line 249... | Line 321... | ||
249 | memcpy (p, sec->contents, sz); |
321 | memcpy (p, sec->contents, sz); |
250 | return TRUE; |
322 | return TRUE; |
Line 274... | Line 346... | ||
274 | sec->compress_status = COMPRESS_SECTION_DONE; |
346 | sec->compress_status = COMPRESS_SECTION_DONE; |
275 | sec->contents = contents; |
347 | sec->contents = contents; |
276 | sec->flags |= SEC_IN_MEMORY; |
348 | sec->flags |= SEC_IN_MEMORY; |
277 | } |
349 | } |
Line 278... | Line -... | ||
278 | - | ||
279 | 350 | ||
280 | /* |
351 | /* |
281 | FUNCTION |
352 | FUNCTION |
Line 282... | Line 353... | ||
282 | bfd_is_section_compressed |
353 | bfd_is_section_compressed_with_header |
283 | 354 | ||
284 | SYNOPSIS |
355 | SYNOPSIS |
- | 356 | bfd_boolean bfd_is_section_compressed_with_header |
|
- | 357 | (bfd *abfd, asection *section, |
|
Line 285... | Line 358... | ||
285 | bfd_boolean bfd_is_section_compressed |
358 | int *compression_header_size_p, |
286 | (bfd *abfd, asection *section); |
359 | bfd_size_type *uncompressed_size_p); |
- | 360 | ||
- | 361 | DESCRIPTION |
|
- | 362 | Return @code{TRUE} if @var{section} is compressed. Compression |
|
- | 363 | header size is returned in @var{compression_header_size_p} and |
|
287 | 364 | uncompressed size is returned in @var{uncompressed_size_p}. If |
|
Line 288... | Line 365... | ||
288 | DESCRIPTION |
365 | compression is unsupported, compression header size is returned |
289 | Return @code{TRUE} if @var{section} is compressed. |
366 | with -1 and uncompressed size is returned with 0. |
- | 367 | */ |
|
- | 368 | ||
290 | */ |
369 | bfd_boolean |
- | 370 | bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec, |
|
291 | 371 | int *compression_header_size_p, |
|
- | 372 | bfd_size_type *uncompressed_size_p) |
|
292 | bfd_boolean |
373 | { |
293 | bfd_is_section_compressed (bfd *abfd, sec_ptr sec) |
374 | bfd_byte header[MAX_COMPRESSION_HEADER_SIZE]; |
Line -... | Line 375... | ||
- | 375 | int compression_header_size; |
|
- | 376 | int header_size; |
|
- | 377 | unsigned int saved = sec->compress_status; |
|
- | 378 | bfd_boolean compressed; |
|
- | 379 | ||
294 | { |
380 | compression_header_size = bfd_get_compression_header_size (abfd, sec); |
295 | bfd_byte compressed_buffer [12]; |
381 | if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE) |
Line -... | Line 382... | ||
- | 382 | abort (); |
|
- | 383 | header_size = compression_header_size ? compression_header_size : 12; |
|
- | 384 | ||
- | 385 | /* Don't decompress the section. */ |
|
296 | unsigned int saved = sec->compress_status; |
386 | sec->compress_status = COMPRESS_SECTION_NONE; |
297 | bfd_boolean compressed; |
387 | |
- | 388 | /* Read the header. */ |
|
- | 389 | if (bfd_get_section_contents (abfd, sec, header, 0, header_size)) |
|
- | 390 | { |
|
- | 391 | if (compression_header_size == 0) |
|
- | 392 | /* In this case, it should be "ZLIB" followed by the uncompressed |
|
- | 393 | section size, 8 bytes in big-endian order. */ |
|
- | 394 | compressed = CONST_STRNEQ ((char*) header , "ZLIB"); |
|
- | 395 | else |
|
- | 396 | compressed = TRUE; |
|
- | 397 | } |
|
- | 398 | else |
|
- | 399 | compressed = FALSE; |
|
- | 400 | ||
- | 401 | *uncompressed_size_p = sec->size; |
|
- | 402 | if (compressed) |
|
- | 403 | { |
|
- | 404 | if (compression_header_size != 0) |
|
- | 405 | { |
|
298 | 406 | if (!bfd_check_compression_header (abfd, header, sec, |
|
- | 407 | uncompressed_size_p)) |
|
299 | /* Don't decompress the section. */ |
408 | compression_header_size = -1; |
- | 409 | } |
|
- | 410 | /* Check for the pathalogical case of a debug string section that |
|
- | 411 | contains the string ZLIB.... as the first entry. We assume that |
|
- | 412 | no uncompressed .debug_str section would ever be big enough to |
|
- | 413 | have the first byte of its (big-endian) size be non-zero. */ |
|
Line 300... | Line 414... | ||
300 | sec->compress_status = COMPRESS_SECTION_NONE; |
414 | else if (strcmp (sec->name, ".debug_str") == 0 |
301 | 415 | && ISPRINT (header[4])) |
|
- | 416 | compressed = FALSE; |
|
302 | /* Read the zlib header. In this case, it should be "ZLIB" followed |
417 | else |
303 | by the uncompressed section size, 8 bytes in big-endian order. */ |
418 | *uncompressed_size_p = bfd_getb64 (header + 4); |
Line 304... | Line 419... | ||
304 | compressed = (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12) |
419 | } |
305 | && CONST_STRNEQ ((char*) compressed_buffer, "ZLIB")); |
420 | |
- | 421 | /* Restore compress_status. */ |
|
- | 422 | sec->compress_status = saved; |
|
- | 423 | *compression_header_size_p = compression_header_size; |
|
- | 424 | return compressed; |
|
- | 425 | } |
|
- | 426 | ||
- | 427 | /* |
|
- | 428 | FUNCTION |
|
- | 429 | bfd_is_section_compressed |
|
- | 430 | ||
- | 431 | SYNOPSIS |
|
- | 432 | bfd_boolean bfd_is_section_compressed |
|
- | 433 | (bfd *abfd, asection *section); |
|
- | 434 | ||
- | 435 | DESCRIPTION |
|
- | 436 | Return @code{TRUE} if @var{section} is compressed. |
|
- | 437 | */ |
|
- | 438 | ||
- | 439 | bfd_boolean |
|
- | 440 | bfd_is_section_compressed (bfd *abfd, sec_ptr sec) |
|
- | 441 | { |
|
- | 442 | int compression_header_size; |
|
- | 443 | bfd_size_type uncompressed_size; |
|
- | 444 | return (bfd_is_section_compressed_with_header (abfd, sec, |
|
306 | 445 | &compression_header_size, |
|
Line 307... | Line 446... | ||
307 | /* Restore compress_status. */ |
446 | &uncompressed_size) |
308 | sec->compress_status = saved; |
447 | && compression_header_size >= 0 |
309 | return compressed; |
448 | && uncompressed_size > 0); |
Line 321... | Line 460... | ||
321 | Record compressed section size, update section size with |
460 | Record compressed section size, update section size with |
322 | decompressed size and set compress_status to |
461 | decompressed size and set compress_status to |
323 | DECOMPRESS_SECTION_SIZED. |
462 | DECOMPRESS_SECTION_SIZED. |
Line 324... | Line 463... | ||
324 | 463 | ||
325 | Return @code{FALSE} if the section is not a valid compressed |
- | |
326 | section or zlib is not installed on this machine. Otherwise, |
464 | Return @code{FALSE} if the section is not a valid compressed |
327 | return @code{TRUE}. |
465 | section. Otherwise, return @code{TRUE}. |
Line 328... | Line 466... | ||
328 | */ |
466 | */ |
329 | 467 | ||
330 | bfd_boolean |
- | |
331 | bfd_init_section_decompress_status (bfd *abfd ATTRIBUTE_UNUSED, |
468 | bfd_boolean |
332 | sec_ptr sec ATTRIBUTE_UNUSED) |
- | |
333 | { |
469 | bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec) |
334 | #ifndef HAVE_ZLIB_H |
470 | { |
335 | bfd_set_error (bfd_error_invalid_operation); |
- | |
336 | return FALSE; |
471 | bfd_byte header[MAX_COMPRESSION_HEADER_SIZE]; |
337 | #else |
472 | int compression_header_size; |
Line -... | Line 473... | ||
- | 473 | int header_size; |
|
- | 474 | bfd_size_type uncompressed_size; |
|
- | 475 | ||
- | 476 | compression_header_size = bfd_get_compression_header_size (abfd, sec); |
|
- | 477 | if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE) |
|
- | 478 | abort (); |
|
338 | bfd_byte compressed_buffer [12]; |
479 | header_size = compression_header_size ? compression_header_size : 12; |
339 | bfd_size_type uncompressed_size; |
480 | |
340 | 481 | /* Read the header. */ |
|
341 | if (sec->rawsize != 0 |
482 | if (sec->rawsize != 0 |
342 | || sec->contents != NULL |
483 | || sec->contents != NULL |
343 | || sec->compress_status != COMPRESS_SECTION_NONE |
484 | || sec->compress_status != COMPRESS_SECTION_NONE |
344 | || !bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12)) |
485 | || !bfd_get_section_contents (abfd, sec, header, 0, header_size)) |
345 | { |
486 | { |
Line -... | Line 487... | ||
- | 487 | bfd_set_error (bfd_error_invalid_operation); |
|
- | 488 | return FALSE; |
|
346 | bfd_set_error (bfd_error_invalid_operation); |
489 | } |
347 | return FALSE; |
490 | |
348 | } |
491 | if (compression_header_size == 0) |
- | 492 | { |
|
- | 493 | /* In this case, it should be "ZLIB" followed by the uncompressed |
|
- | 494 | section size, 8 bytes in big-endian order. */ |
|
- | 495 | if (! CONST_STRNEQ ((char*) header, "ZLIB")) |
|
- | 496 | { |
|
- | 497 | bfd_set_error (bfd_error_wrong_format); |
|
- | 498 | return FALSE; |
|
- | 499 | } |
|
349 | 500 | uncompressed_size = bfd_getb64 (header + 4); |
|
350 | /* Read the zlib header. In this case, it should be "ZLIB" followed |
501 | } |
351 | by the uncompressed section size, 8 bytes in big-endian order. */ |
502 | else if (!bfd_check_compression_header (abfd, header, sec, |
352 | if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB")) |
503 | &uncompressed_size)) |
353 | { |
- | |
354 | bfd_set_error (bfd_error_wrong_format); |
- | |
355 | return FALSE; |
- | |
356 | } |
- | |
357 | - | ||
358 | uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8; |
- | |
359 | uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8; |
- | |
360 | uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8; |
- | |
361 | uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8; |
- | |
Line 362... | Line 504... | ||
362 | uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8; |
504 | { |
363 | uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8; |
505 | bfd_set_error (bfd_error_wrong_format); |
364 | uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8; |
506 | return FALSE; |
Line 365... | Line 507... | ||
365 | uncompressed_size += compressed_buffer[11]; |
507 | } |
366 | - | ||
367 | sec->compressed_size = sec->size; |
508 | |
Line 368... | Line 509... | ||
368 | sec->size = uncompressed_size; |
509 | sec->compressed_size = sec->size; |
369 | sec->compress_status = DECOMPRESS_SECTION_SIZED; |
510 | sec->size = uncompressed_size; |
370 | 511 | sec->compress_status = DECOMPRESS_SECTION_SIZED; |
|
Line 383... | Line 524... | ||
383 | DESCRIPTION |
524 | DESCRIPTION |
384 | If open for read, compress section, update section size with |
525 | If open for read, compress section, update section size with |
385 | compressed size and set compress_status to COMPRESS_SECTION_DONE. |
526 | compressed size and set compress_status to COMPRESS_SECTION_DONE. |
Line 386... | Line 527... | ||
386 | 527 | ||
387 | Return @code{FALSE} if the section is not a valid compressed |
- | |
388 | section or zlib is not installed on this machine. Otherwise, |
528 | Return @code{FALSE} if the section is not a valid compressed |
389 | return @code{TRUE}. |
529 | section. Otherwise, return @code{TRUE}. |
Line 390... | Line 530... | ||
390 | */ |
530 | */ |
391 | 531 | ||
392 | bfd_boolean |
- | |
393 | bfd_init_section_compress_status (bfd *abfd ATTRIBUTE_UNUSED, |
532 | bfd_boolean |
394 | sec_ptr sec ATTRIBUTE_UNUSED) |
- | |
395 | { |
- | |
396 | #ifndef HAVE_ZLIB_H |
- | |
397 | bfd_set_error (bfd_error_invalid_operation); |
- | |
398 | return FALSE; |
533 | bfd_init_section_compress_status (bfd *abfd, sec_ptr sec) |
399 | #else |
534 | { |
400 | bfd_size_type uncompressed_size; |
535 | bfd_size_type uncompressed_size; |
Line 401... | Line 536... | ||
401 | bfd_byte *uncompressed_buffer; |
536 | bfd_byte *uncompressed_buffer; |
Line 417... | Line 552... | ||
417 | uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size); |
552 | uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size); |
418 | if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer, |
553 | if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer, |
419 | 0, uncompressed_size)) |
554 | 0, uncompressed_size)) |
420 | ret = FALSE; |
555 | ret = FALSE; |
421 | else |
556 | else |
- | 557 | { |
|
422 | ret = bfd_compress_section_contents (abfd, sec, |
558 | uncompressed_size = bfd_compress_section_contents (abfd, sec, |
423 | uncompressed_buffer, |
559 | uncompressed_buffer, |
424 | uncompressed_size); |
560 | uncompressed_size); |
- | 561 | ret = uncompressed_size != 0; |
|
- | 562 | } |
|
Line 425... | Line -... | ||
425 | - | ||
426 | free (uncompressed_buffer); |
563 | |
- | 564 | return ret; |
|
- | 565 | } |
|
- | 566 | ||
- | 567 | /* |
|
- | 568 | FUNCTION |
|
- | 569 | bfd_compress_section |
|
- | 570 | ||
- | 571 | SYNOPSIS |
|
- | 572 | bfd_boolean bfd_compress_section |
|
- | 573 | (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer); |
|
- | 574 | ||
- | 575 | DESCRIPTION |
|
- | 576 | If open for write, compress section, update section size with |
|
- | 577 | compressed size and set compress_status to COMPRESS_SECTION_DONE. |
|
- | 578 | ||
- | 579 | Return @code{FALSE} if compression fail. Otherwise, return |
|
- | 580 | @code{TRUE}. |
|
- | 581 | */ |
|
- | 582 | ||
- | 583 | bfd_boolean |
|
- | 584 | bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer) |
|
- | 585 | { |
|
- | 586 | bfd_size_type uncompressed_size = sec->size; |
|
- | 587 | ||
- | 588 | /* Error if not opened for write. */ |
|
- | 589 | if (abfd->direction != write_direction |
|
- | 590 | || uncompressed_size == 0 |
|
- | 591 | || uncompressed_buffer == NULL |
|
- | 592 | || sec->contents != NULL |
|
- | 593 | || sec->compressed_size != 0 |
|
- | 594 | || sec->compress_status != COMPRESS_SECTION_NONE) |
|
- | 595 | { |
|
- | 596 | bfd_set_error (bfd_error_invalid_operation); |
|
427 | return ret; |
597 | return FALSE; |
- | 598 | } |
|
- | 599 | ||
- | 600 | /* Compress it. */ |
|
- | 601 | return bfd_compress_section_contents (abfd, sec, uncompressed_buffer, |
|
428 | #endif |
602 | uncompressed_size) != 0; |