Branch data Line data Source code
1 : : /* Compress or decompress a section.
2 : : Copyright (C) 2015, 2016 Red Hat, Inc.
3 : : Copyright (C) 2023, Mark J. Wielaard <mark@klomp.org>
4 : : This file is part of elfutils.
5 : :
6 : : This file is free software; you can redistribute it and/or modify
7 : : it under the terms of either
8 : :
9 : : * the GNU Lesser General Public License as published by the Free
10 : : Software Foundation; either version 3 of the License, or (at
11 : : your option) any later version
12 : :
13 : : or
14 : :
15 : : * the GNU General Public License as published by the Free
16 : : Software Foundation; either version 2 of the License, or (at
17 : : your option) any later version
18 : :
19 : : or both in parallel, as here.
20 : :
21 : : elfutils is distributed in the hope that it will be useful, but
22 : : WITHOUT ANY WARRANTY; without even the implied warranty of
23 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : : General Public License for more details.
25 : :
26 : : You should have received copies of the GNU General Public License and
27 : : the GNU Lesser General Public License along with this program. If
28 : : not, see <http://www.gnu.org/licenses/>. */
29 : :
30 : : #ifdef HAVE_CONFIG_H
31 : : # include <config.h>
32 : : #endif
33 : :
34 : : #include <libelf.h>
35 : : #include "libelfP.h"
36 : : #include "common.h"
37 : :
38 : : #include <stddef.h>
39 : : #include <stdlib.h>
40 : : #include <string.h>
41 : : #include <zlib.h>
42 : :
43 : : #ifdef USE_ZSTD
44 : : #include <zstd.h>
45 : : #endif
46 : :
47 : : /* Cleanup and return result. Don't leak memory. */
48 : : static void *
49 : 142 : do_deflate_cleanup (void *result, z_stream *z, void *out_buf,
50 : : Elf_Data *cdatap)
51 : : {
52 : 142 : deflateEnd (z);
53 : 142 : free (out_buf);
54 [ + + ]: 142 : if (cdatap != NULL)
55 : 60 : free (cdatap->d_buf);
56 : 142 : return result;
57 : : }
58 : :
59 : : #define deflate_cleanup(result, cdata) \
60 : : do_deflate_cleanup(result, &z, out_buf, cdata)
61 : :
62 : : static
63 : : void *
64 : 3134 : __libelf_compress_zlib (Elf_Scn *scn, size_t hsize, int ei_data,
65 : : size_t *orig_size, size_t *orig_addralign,
66 : : size_t *new_size, bool force,
67 : : Elf_Data *data, Elf_Data *next_data,
68 : : void *out_buf, size_t out_size, size_t block)
69 : : {
70 : : /* Caller gets to fill in the header at the start. Just skip it here. */
71 : 3134 : size_t used = hsize;
72 : :
73 : 3134 : z_stream z;
74 : 3134 : z.zalloc = Z_NULL;
75 : 3134 : z.zfree = Z_NULL;
76 : 3134 : z.opaque = Z_NULL;
77 : 3134 : int zrc = deflateInit (&z, Z_BEST_COMPRESSION);
78 [ - + ]: 3134 : if (zrc != Z_OK)
79 : : {
80 : 0 : __libelf_seterrno (ELF_E_COMPRESS_ERROR);
81 : 0 : return deflate_cleanup(NULL, NULL);
82 : : }
83 : :
84 : 3134 : Elf_Data cdata;
85 : 3134 : cdata.d_buf = NULL;
86 : :
87 : : /* Loop over data buffers. */
88 : 3134 : int flush = Z_NO_FLUSH;
89 : 3150 : do
90 : : {
91 : : /* Convert to raw if different endianness. */
92 : 3150 : cdata = *data;
93 [ + + + - ]: 3150 : bool convert = ei_data != MY_ELFDATA && data->d_size > 0;
94 : 682 : if (convert)
95 : : {
96 : : /* Don't do this conversion in place, we might want to keep
97 : : the original data around, caller decides. */
98 : 682 : cdata.d_buf = malloc (data->d_size);
99 [ - + ]: 682 : if (cdata.d_buf == NULL)
100 : : {
101 : 0 : __libelf_seterrno (ELF_E_NOMEM);
102 : 0 : return deflate_cleanup (NULL, NULL);
103 : : }
104 [ - + ]: 682 : if (gelf_xlatetof (scn->elf, &cdata, data, ei_data) == NULL)
105 : 0 : return deflate_cleanup (NULL, &cdata);
106 : : }
107 : :
108 : 3150 : z.avail_in = cdata.d_size;
109 : 3150 : z.next_in = cdata.d_buf;
110 : :
111 : : /* Get next buffer to see if this is the last one. */
112 : 3150 : data = next_data;
113 [ + + ]: 3150 : if (data != NULL)
114 : : {
115 : 16 : *orig_addralign = MAX (*orig_addralign, data->d_align);
116 : 16 : *orig_size += data->d_size;
117 : 16 : next_data = elf_getdata (scn, data);
118 : : }
119 : : else
120 : 3150 : flush = Z_FINISH;
121 : :
122 : : /* Flush one data buffer. */
123 : 6620 : do
124 : : {
125 : 6620 : z.avail_out = out_size - used;
126 : 6620 : z.next_out = out_buf + used;
127 : 6620 : zrc = deflate (&z, flush);
128 [ - + ]: 6620 : if (zrc == Z_STREAM_ERROR)
129 : : {
130 : 0 : __libelf_seterrno (ELF_E_COMPRESS_ERROR);
131 [ # # ]: 0 : return deflate_cleanup (NULL, convert ? &cdata : NULL);
132 : : }
133 : 6620 : used += (out_size - used) - z.avail_out;
134 : :
135 : : /* Bail out if we are sure the user doesn't want the
136 : : compression forced and we are using more compressed data
137 : : than original data. */
138 [ + + + + ]: 6620 : if (!force && flush == Z_FINISH && used >= *orig_size)
139 [ + + ]: 224 : return deflate_cleanup ((void *) -1, convert ? &cdata : NULL);
140 : :
141 [ + + ]: 6478 : if (z.avail_out == 0)
142 : : {
143 : 3470 : void *bigger = realloc (out_buf, out_size + block);
144 [ - + ]: 3470 : if (bigger == NULL)
145 : : {
146 : 0 : __libelf_seterrno (ELF_E_NOMEM);
147 [ # # ]: 0 : return deflate_cleanup (NULL, convert ? &cdata : NULL);
148 : : }
149 : : out_buf = bigger;
150 : : out_size += block;
151 : : }
152 : : }
153 [ + + ]: 6478 : while (z.avail_out == 0); /* Need more output buffer. */
154 : :
155 [ + + ]: 3008 : if (convert)
156 : : {
157 : 622 : free (cdata.d_buf);
158 : 622 : cdata.d_buf = NULL;
159 : : }
160 : : }
161 [ + + ]: 3008 : while (flush != Z_FINISH); /* More data blocks. */
162 : :
163 [ - + ]: 2992 : if (zrc != Z_STREAM_END)
164 : : {
165 : 0 : __libelf_seterrno (ELF_E_COMPRESS_ERROR);
166 : 0 : return deflate_cleanup (NULL, NULL);
167 : : }
168 : :
169 : 2992 : deflateEnd (&z);
170 : 2992 : *new_size = used;
171 : 2992 : return out_buf;
172 : : }
173 : :
174 : : #ifdef USE_ZSTD_COMPRESS
175 : : /* Cleanup and return result. Don't leak memory. */
176 : : static void *
177 : 104 : do_zstd_cleanup (void *result, ZSTD_CCtx * const cctx, void *out_buf,
178 : : Elf_Data *cdatap)
179 : : {
180 : 104 : ZSTD_freeCCtx (cctx);
181 : 104 : free (out_buf);
182 [ + + ]: 104 : if (cdatap != NULL)
183 : 48 : free (cdatap->d_buf);
184 : 104 : return result;
185 : : }
186 : :
187 : : #define zstd_cleanup(result, cdata) \
188 : : do_zstd_cleanup(result, cctx, out_buf, cdata)
189 : :
190 : : static
191 : : void *
192 : 464 : __libelf_compress_zstd (Elf_Scn *scn, size_t hsize, int ei_data,
193 : : size_t *orig_size, size_t *orig_addralign,
194 : : size_t *new_size, bool force,
195 : : Elf_Data *data, Elf_Data *next_data,
196 : : void *out_buf, size_t out_size, size_t block)
197 : : {
198 : : /* Caller gets to fill in the header at the start. Just skip it here. */
199 : 464 : size_t used = hsize;
200 : :
201 : 464 : ZSTD_CCtx* const cctx = ZSTD_createCCtx();
202 : 464 : Elf_Data cdata;
203 : 464 : cdata.d_buf = NULL;
204 : :
205 : : /* Loop over data buffers. */
206 : 464 : ZSTD_EndDirective mode = ZSTD_e_continue;
207 : :
208 : 464 : do
209 : : {
210 : : /* Convert to raw if different endianness. */
211 : 464 : cdata = *data;
212 [ + + + - ]: 464 : bool convert = ei_data != MY_ELFDATA && data->d_size > 0;
213 : 240 : if (convert)
214 : : {
215 : : /* Don't do this conversion in place, we might want to keep
216 : : the original data around, caller decides. */
217 : 240 : cdata.d_buf = malloc (data->d_size);
218 [ - + ]: 240 : if (cdata.d_buf == NULL)
219 : : {
220 : 0 : __libelf_seterrno (ELF_E_NOMEM);
221 : 104 : return zstd_cleanup (NULL, NULL);
222 : : }
223 [ - + ]: 240 : if (gelf_xlatetof (scn->elf, &cdata, data, ei_data) == NULL)
224 : 0 : return zstd_cleanup (NULL, &cdata);
225 : : }
226 : :
227 : 464 : ZSTD_inBuffer ib = { cdata.d_buf, cdata.d_size, 0 };
228 : :
229 : : /* Get next buffer to see if this is the last one. */
230 : 464 : data = next_data;
231 [ - + ]: 464 : if (data != NULL)
232 : : {
233 : 0 : *orig_addralign = MAX (*orig_addralign, data->d_align);
234 : 0 : *orig_size += data->d_size;
235 : 0 : next_data = elf_getdata (scn, data);
236 : : }
237 : : else
238 : 464 : mode = ZSTD_e_end;
239 : :
240 : : /* Flush one data buffer. */
241 : 1856 : for (;;)
242 : 696 : {
243 : 1160 : ZSTD_outBuffer ob = { out_buf + used, out_size - used, 0 };
244 : 1160 : size_t ret = ZSTD_compressStream2 (cctx, &ob, &ib, mode);
245 [ - + ]: 1160 : if (ZSTD_isError (ret))
246 : : {
247 : 0 : __libelf_seterrno (ELF_E_COMPRESS_ERROR);
248 [ - - ]: 104 : return zstd_cleanup (NULL, convert ? &cdata : NULL);
249 : : }
250 : 1160 : used += ob.pos;
251 : :
252 : : /* Bail out if we are sure the user doesn't want the
253 : : compression forced and we are using more compressed data
254 : : than original data. */
255 [ + - + + ]: 1160 : if (!force && mode == ZSTD_e_end && used >= *orig_size)
256 [ + + ]: 160 : return zstd_cleanup ((void *) -1, convert ? &cdata : NULL);
257 : :
258 [ + + ]: 1056 : if (ret > 0)
259 : : {
260 : 696 : void *bigger = realloc (out_buf, out_size + block);
261 [ - + ]: 696 : if (bigger == NULL)
262 : : {
263 : 0 : __libelf_seterrno (ELF_E_NOMEM);
264 [ # # ]: 0 : return zstd_cleanup (NULL, convert ? &cdata : NULL);
265 : : }
266 : 696 : out_buf = bigger;
267 : 696 : out_size += block;
268 : : }
269 : : else
270 : : break;
271 : : }
272 : :
273 [ + + ]: 360 : if (convert)
274 : : {
275 : 192 : free (cdata.d_buf);
276 : 192 : cdata.d_buf = NULL;
277 : : }
278 : : }
279 [ - + ]: 360 : while (mode != ZSTD_e_end); /* More data blocks. */
280 : :
281 : 360 : ZSTD_freeCCtx (cctx);
282 : 360 : *new_size = used;
283 : 360 : return out_buf;
284 : : }
285 : : #endif
286 : :
287 : : /* Given a section, uses the (in-memory) Elf_Data to extract the
288 : : original data size (including the given header size) and data
289 : : alignment. Returns a buffer that has at least hsize bytes (for the
290 : : caller to fill in with a header) plus zlib compressed date. Also
291 : : returns the new buffer size in new_size (hsize + compressed data
292 : : size). Returns (void *) -1 when FORCE is false and the compressed
293 : : data would be bigger than the original data. */
294 : : void *
295 : : internal_function
296 : 3668 : __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data,
297 : : size_t *orig_size, size_t *orig_addralign,
298 : : size_t *new_size, bool force, bool use_zstd)
299 : : {
300 : : /* The compressed data is the on-disk data. We simplify the
301 : : implementation a bit by asking for the (converted) in-memory
302 : : data (which might be all there is if the user created it with
303 : : elf_newdata) and then convert back to raw if needed before
304 : : compressing. Should be made a bit more clever to directly
305 : : use raw if that is directly available. */
306 : 3668 : Elf_Data *data = elf_getdata (scn, NULL);
307 [ + - ]: 3668 : if (data == NULL)
308 : : return NULL;
309 : :
310 : : /* When not forced and we immediately know we would use more data by
311 : : compressing, because of the header plus zlib overhead (five bytes
312 : : per 16 KB block, plus a one-time overhead of six bytes for the
313 : : entire stream), don't do anything.
314 : : Size estimation for ZSTD compression would be similar. */
315 : 3668 : Elf_Data *next_data = elf_getdata (scn, data);
316 [ + + ]: 3668 : if (next_data == NULL && !force
317 [ + + ]: 1602 : && data->d_size <= hsize + 5 + 6)
318 : : return (void *) -1;
319 : :
320 : 3598 : *orig_addralign = data->d_align;
321 : 3598 : *orig_size = data->d_size;
322 : :
323 : : /* Guess an output block size. 1/8th of the original Elf_Data plus
324 : : hsize. Make the first chunk twice that size (25%), then increase
325 : : by a block (12.5%) when necessary. */
326 : 3598 : size_t block = (data->d_size / 8) + hsize;
327 : 3598 : size_t out_size = 2 * block;
328 : 3598 : void *out_buf = malloc (out_size);
329 [ - + ]: 3598 : if (out_buf == NULL)
330 : : {
331 : 0 : __libelf_seterrno (ELF_E_NOMEM);
332 : 0 : return NULL;
333 : : }
334 : :
335 [ + + ]: 3598 : if (use_zstd)
336 : : {
337 : : #ifdef USE_ZSTD_COMPRESS
338 : 464 : return __libelf_compress_zstd (scn, hsize, ei_data, orig_size,
339 : : orig_addralign, new_size, force,
340 : : data, next_data, out_buf, out_size,
341 : : block);
342 : : #else
343 : : __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE);
344 : : return NULL;
345 : : #endif
346 : : }
347 : : else
348 : 3134 : return __libelf_compress_zlib (scn, hsize, ei_data, orig_size,
349 : : orig_addralign, new_size, force,
350 : : data, next_data, out_buf, out_size,
351 : : block);
352 : : }
353 : :
354 : : void *
355 : : internal_function
356 : 4188 : __libelf_decompress_zlib (void *buf_in, size_t size_in, size_t size_out)
357 : : {
358 : : /* Catch highly unlikely compression ratios so we don't allocate
359 : : some giant amount of memory for nothing. The max compression
360 : : factor 1032:1 comes from http://www.zlib.net/zlib_tech.html */
361 [ - + ]: 4188 : if (unlikely (size_out / 1032 > size_in))
362 : : {
363 : 0 : __libelf_seterrno (ELF_E_INVALID_DATA);
364 : 0 : return NULL;
365 : : }
366 : :
367 : : /* Malloc might return NULL when requesting zero size. This is highly
368 : : unlikely, it would only happen when the compression was forced.
369 : : But we do need a non-NULL buffer to return and set as result.
370 : : Just make sure to always allocate at least 1 byte. */
371 [ + + ]: 4188 : void *buf_out = malloc (size_out ?: 1);
372 [ - + ]: 4188 : if (unlikely (buf_out == NULL))
373 : : {
374 : 0 : __libelf_seterrno (ELF_E_NOMEM);
375 : 0 : return NULL;
376 : : }
377 : :
378 : 4188 : z_stream z =
379 : : {
380 : : .next_in = buf_in,
381 : : .avail_in = size_in,
382 : : .next_out = buf_out,
383 : : .avail_out = size_out
384 : : };
385 : 4188 : int zrc = inflateInit (&z);
386 [ + + + - ]: 8376 : while (z.avail_in > 0 && likely (zrc == Z_OK))
387 : : {
388 : 4188 : z.next_out = buf_out + (size_out - z.avail_out);
389 : 4188 : zrc = inflate (&z, Z_FINISH);
390 [ + - ]: 4188 : if (unlikely (zrc != Z_STREAM_END))
391 : : {
392 : : zrc = Z_DATA_ERROR;
393 : : break;
394 : : }
395 : 4188 : zrc = inflateReset (&z);
396 : : }
397 : :
398 [ + - - + ]: 4188 : if (unlikely (zrc != Z_OK) || unlikely (z.avail_out != 0))
399 : : {
400 : 0 : free (buf_out);
401 : 0 : buf_out = NULL;
402 : 0 : __libelf_seterrno (ELF_E_DECOMPRESS_ERROR);
403 : : }
404 : :
405 : 4188 : inflateEnd(&z);
406 : 4188 : return buf_out;
407 : : }
408 : :
409 : : #ifdef USE_ZSTD
410 : : static void *
411 : 180 : __libelf_decompress_zstd (void *buf_in, size_t size_in, size_t size_out)
412 : : {
413 : : /* Catch highly unlikely compression ratios so we don't allocate
414 : : some giant amount of memory for nothing. The smallest zstd block
415 : : is a 3-byte block header followed by a 1-byte RLE payload, and a
416 : : single block expands to at most ZSTD_BLOCKSIZE_MAX bytes, so no
417 : : valid frame can expand by more than ZSTD_BLOCKSIZE_MAX / 4
418 : : (32768:1). See doc/zstd_compression_format.md in the zstd
419 : : sources. */
420 : 180 : const size_t zstd_min_block = 3 + 1;
421 : 180 : const size_t zstd_max_ratio = ZSTD_BLOCKSIZE_MAX / zstd_min_block;
422 [ - + ]: 180 : if (unlikely (size_out / zstd_max_ratio > size_in))
423 : : {
424 : 0 : __libelf_seterrno (ELF_E_INVALID_DATA);
425 : 0 : return NULL;
426 : : }
427 : :
428 : : /* Malloc might return NULL when requesting zero size. This is highly
429 : : unlikely, it would only happen when the compression was forced.
430 : : But we do need a non-NULL buffer to return and set as result.
431 : : Just make sure to always allocate at least 1 byte. */
432 [ - + ]: 180 : void *buf_out = malloc (size_out ?: 1);
433 [ - + ]: 180 : if (unlikely (buf_out == NULL))
434 : : {
435 : 0 : __libelf_seterrno (ELF_E_NOMEM);
436 : 0 : return NULL;
437 : : }
438 : :
439 : 180 : size_t ret = ZSTD_decompress (buf_out, size_out, buf_in, size_in);
440 [ + - - + ]: 180 : if (unlikely (ZSTD_isError (ret)) || unlikely (ret != size_out))
441 : : {
442 : 0 : free (buf_out);
443 : 0 : __libelf_seterrno (ELF_E_DECOMPRESS_ERROR);
444 : 0 : return NULL;
445 : : }
446 : : else
447 : : return buf_out;
448 : : }
449 : : #endif
450 : :
451 : : void *
452 : : internal_function
453 : 4368 : __libelf_decompress (int chtype, void *buf_in, size_t size_in, size_t size_out)
454 : : {
455 [ + + ]: 4368 : if (chtype == ELFCOMPRESS_ZLIB)
456 : 4188 : return __libelf_decompress_zlib (buf_in, size_in, size_out);
457 : : else
458 : : {
459 : : #ifdef USE_ZSTD
460 : 180 : return __libelf_decompress_zstd (buf_in, size_in, size_out);
461 : : #else
462 : : __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE);
463 : : return NULL;
464 : : #endif
465 : : }
466 : : }
467 : :
468 : : static void *
469 : 3726 : decompress_elf (Elf_Scn *scn, size_t *size_out, size_t *addralign,
470 : : bool wrlocked)
471 : : {
472 : 3726 : GElf_Chdr chdr;
473 : :
474 [ + + ]: 3726 : if (scn->elf->class == ELFCLASS32)
475 : : {
476 : 596 : Elf32_Chdr *c
477 [ + + ]: 596 : = wrlocked ? __elf32_getchdr_wrlock (scn) : elf32_getchdr (scn);
478 [ + - ]: 596 : if (c == NULL)
479 : : return NULL;
480 : 596 : chdr.ch_type = c->ch_type;
481 : 596 : chdr.ch_size = c->ch_size;
482 : 596 : chdr.ch_addralign = c->ch_addralign;
483 : : }
484 : : else
485 : : {
486 : 3130 : Elf64_Chdr *c
487 [ + + ]: 3130 : = wrlocked ? __elf64_getchdr_wrlock (scn) : elf64_getchdr (scn);
488 [ + - ]: 3130 : if (c == NULL)
489 : : return NULL;
490 : 3130 : chdr = *c;
491 : : }
492 : :
493 : 3726 : bool unknown_compression = false;
494 [ + + ]: 3726 : if (chdr.ch_type != ELFCOMPRESS_ZLIB)
495 : : {
496 [ - + ]: 180 : if (chdr.ch_type != ELFCOMPRESS_ZSTD)
497 : 0 : unknown_compression = true;
498 : :
499 : : #ifndef USE_ZSTD
500 : : if (chdr.ch_type == ELFCOMPRESS_ZSTD)
501 : : unknown_compression = true;
502 : : #endif
503 : : }
504 : :
505 : 0 : if (unknown_compression)
506 : : {
507 : 0 : __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE);
508 : 0 : return NULL;
509 : : }
510 : :
511 [ - + ]: 3726 : if (! powerof2 (chdr.ch_addralign))
512 : : {
513 : 0 : __libelf_seterrno (ELF_E_INVALID_ALIGN);
514 : 0 : return NULL;
515 : : }
516 : :
517 : : /* Take the in-memory representation, so we can even handle a
518 : : section that has just been constructed (maybe it was copied
519 : : over from some other ELF file first with elf_newdata). This
520 : : is slightly inefficient when the raw data needs to be
521 : : converted since then we'll be converting the whole buffer and
522 : : not just Chdr. */
523 : 3726 : Elf_Data *data
524 [ + + ]: 3726 : = wrlocked ? __elf_getdata_wrlock (scn, NULL) : elf_getdata (scn, NULL);
525 [ + - ]: 3726 : if (data == NULL)
526 : : return NULL;
527 : :
528 : 3726 : int elfclass = scn->elf->class;
529 : 3130 : size_t hsize = (elfclass == ELFCLASS32
530 [ + + ]: 3726 : ? sizeof (Elf32_Chdr) : sizeof (Elf64_Chdr));
531 : 3726 : size_t size_in = data->d_size - hsize;
532 : 3726 : void *buf_in = data->d_buf + hsize;
533 : 3726 : void *buf_out
534 : 3726 : = __libelf_decompress (chdr.ch_type, buf_in, size_in, chdr.ch_size);
535 : :
536 : 3726 : *size_out = chdr.ch_size;
537 : 3726 : *addralign = chdr.ch_addralign;
538 : 3726 : return buf_out;
539 : : }
540 : :
541 : : void *
542 : : internal_function
543 : 12 : __libelf_decompress_elf_wrlock (Elf_Scn *scn, size_t *size_out,
544 : : size_t *addralign)
545 : : {
546 : 12 : return decompress_elf (scn, size_out, addralign, true);
547 : : }
548 : :
549 : : void *
550 : : internal_function
551 : 3714 : __libelf_decompress_elf (Elf_Scn *scn, size_t *size_out, size_t *addralign)
552 : : {
553 : 3714 : return decompress_elf (scn, size_out, addralign, false);
554 : : }
555 : :
556 : : /* Assumes buf is a malloced buffer. */
557 : : void
558 : : internal_function
559 : 7712 : __libelf_reset_rawdata (Elf_Scn *scn, void *buf, size_t size, size_t align,
560 : : Elf_Type type)
561 : : {
562 : : /* This is the new raw data, replace and possibly free old data. */
563 : 7712 : scn->rawdata.d.d_off = 0;
564 : 7712 : scn->rawdata.d.d_version = EV_CURRENT;
565 : 7712 : scn->rawdata.d.d_buf = buf;
566 : 7712 : scn->rawdata.d.d_size = size;
567 : 7712 : scn->rawdata.d.d_align = align;
568 : 7712 : scn->rawdata.d.d_type = type;
569 : :
570 : : /* Remove the old data. */
571 : 7712 : Elf_Data_List *runp = scn->data_list.next;
572 [ + + ]: 7728 : while (runp != NULL)
573 : : {
574 : 16 : Elf_Data_List *oldp = runp;
575 : 16 : runp = runp->next;
576 [ + - ]: 16 : if ((oldp->flags & ELF_F_MALLOCED) != 0)
577 : 16 : free (oldp);
578 : : }
579 : : /* Existing existing data is no longer valid. */
580 : 7712 : scn->data_list.next = NULL;
581 : 7712 : scn->data_list_rear = NULL;
582 [ + + ]: 7712 : if (scn->data_base != scn->rawdata_base)
583 : 1216 : free (scn->data_base);
584 : 7712 : scn->data_base = NULL;
585 [ + + ]: 7712 : if (scn->zdata_base != buf
586 [ + + ]: 3994 : && scn->zdata_base != scn->rawdata_base)
587 : : {
588 : 3966 : free (scn->zdata_base);
589 : 3966 : scn->zdata_base = NULL;
590 : : }
591 [ + + ]: 7712 : if (scn->elf->map_address == NULL
592 [ + - ]: 2452 : || scn->rawdata_base == scn->zdata_base
593 [ + + ]: 2452 : || (scn->flags & ELF_F_MALLOCED) != 0)
594 : : {
595 : 5264 : free (scn->rawdata_base);
596 : 5264 : scn->rawdata_base = NULL;
597 : 5264 : scn->zdata_base = NULL;
598 : : }
599 : :
600 : 7712 : scn->rawdata_base = buf;
601 : 7712 : scn->flags |= ELF_F_MALLOCED;
602 : :
603 : : /* Pretend we (tried to) read the data from the file and setup the
604 : : data (might have to convert the Chdr to native format). */
605 : 7712 : scn->data_read = 1;
606 : 7712 : scn->flags |= ELF_F_FILEDATA;
607 : 7712 : __libelf_set_data_list_rdlock (scn, 1);
608 : 7712 : }
609 : :
610 : : int
611 : 816766 : elf_compress (Elf_Scn *scn, int type, unsigned int flags)
612 : : {
613 [ + - ]: 816766 : if (scn == NULL)
614 : : return -1;
615 : :
616 [ - + ]: 816766 : if ((flags & ~ELF_CHF_FORCE) != 0)
617 : : {
618 : 0 : __libelf_seterrno (ELF_E_INVALID_OPERAND);
619 : 0 : return -1;
620 : : }
621 : :
622 : 816766 : bool force = (flags & ELF_CHF_FORCE) != 0;
623 : :
624 : 816766 : Elf *elf = scn->elf;
625 : 816766 : GElf_Ehdr ehdr;
626 [ + - ]: 816766 : if (gelf_getehdr (elf, &ehdr) == NULL)
627 : : return -1;
628 : :
629 : 816766 : int elfclass = elf->class;
630 : 816766 : int elfdata = ehdr.e_ident[EI_DATA];
631 : :
632 : 816766 : Elf64_Xword sh_flags;
633 : 816766 : Elf64_Word sh_type;
634 : 816766 : union shdr
635 : : {
636 : : Elf32_Shdr *s32;
637 : : Elf64_Shdr *s64;
638 : : } shdr;
639 [ + + ]: 816766 : if (elfclass == ELFCLASS32)
640 : : {
641 : 794112 : shdr.s32 = elf32_getshdr (scn);
642 [ + - ]: 794112 : if (shdr.s32 == NULL)
643 : : return -1;
644 : :
645 : 794112 : sh_flags = shdr.s32->sh_flags;
646 : 794112 : sh_type = shdr.s32->sh_type;
647 : : }
648 : : else
649 : : {
650 : 22654 : shdr.s64 = elf64_getshdr (scn);
651 [ + - ]: 22654 : if (shdr.s64 == NULL)
652 : : return -1;
653 : :
654 : 22654 : sh_flags = shdr.s64->sh_flags;
655 : 22654 : sh_type = shdr.s64->sh_type;
656 : : }
657 : :
658 [ + + ]: 816766 : if ((sh_flags & SHF_ALLOC) != 0)
659 : : {
660 : 314862 : __libelf_seterrno (ELF_E_INVALID_SECTION_FLAGS);
661 : 314862 : return -1;
662 : : }
663 : :
664 [ + + ]: 501904 : if (sh_type == SHT_NULL || sh_type == SHT_NOBITS)
665 : : {
666 : 131160 : __libelf_seterrno (ELF_E_INVALID_SECTION_TYPE);
667 : 131160 : return -1;
668 : : }
669 : :
670 : 370744 : int compressed = (sh_flags & SHF_COMPRESSED);
671 [ + + ]: 370744 : if (type == ELFCOMPRESS_ZLIB || type == ELFCOMPRESS_ZSTD)
672 : : {
673 : : /* Compress/Deflate. */
674 : 2776 : if (compressed == 1)
675 : : {
676 : : __libelf_seterrno (ELF_E_ALREADY_COMPRESSED);
677 : : return -1;
678 : : }
679 : :
680 : 2294 : size_t hsize = (elfclass == ELFCLASS32
681 [ + + ]: 2776 : ? sizeof (Elf32_Chdr) : sizeof (Elf64_Chdr));
682 : 2776 : size_t orig_size, orig_addralign, new_size;
683 : 2776 : void *out_buf = __libelf_compress (scn, hsize, elfdata,
684 : : &orig_size, &orig_addralign,
685 : : &new_size, force,
686 : : type == ELFCOMPRESS_ZSTD);
687 : :
688 : : /* Compression would make section larger, don't change anything. */
689 [ + + ]: 2776 : if (out_buf == (void *) -1)
690 : : return 0;
691 : :
692 : : /* Compression failed, return error. */
693 [ + - ]: 2574 : if (out_buf == NULL)
694 : : return -1;
695 : :
696 : : /* Put the header in front of the data. */
697 [ + + ]: 2574 : if (elfclass == ELFCLASS32)
698 : : {
699 : 390 : Elf32_Chdr chdr;
700 : 390 : chdr.ch_type = type;
701 : 390 : chdr.ch_size = orig_size;
702 : 390 : chdr.ch_addralign = orig_addralign;
703 [ + + ]: 390 : if (elfdata != MY_ELFDATA)
704 : : {
705 : 220 : CONVERT (chdr.ch_type);
706 : 220 : CONVERT (chdr.ch_size);
707 : 220 : CONVERT (chdr.ch_addralign);
708 : : }
709 : 390 : memcpy (out_buf, &chdr, sizeof (Elf32_Chdr));
710 : : }
711 : : else
712 : : {
713 : 2184 : Elf64_Chdr chdr;
714 : 2184 : chdr.ch_type = type;
715 : 2184 : chdr.ch_reserved = 0;
716 : 2184 : chdr.ch_size = orig_size;
717 : 2184 : chdr.ch_addralign = orig_addralign;
718 [ + + ]: 2184 : if (elfdata != MY_ELFDATA)
719 : : {
720 : 218 : CONVERT (chdr.ch_type);
721 : 218 : CONVERT (chdr.ch_reserved);
722 : 218 : CONVERT (chdr.ch_size);
723 : 218 : CONVERT (chdr.ch_addralign);
724 : : }
725 : 2184 : memcpy (out_buf, &chdr, sizeof (Elf64_Chdr));
726 : : }
727 : :
728 : : /* Note we keep the sh_entsize as is, we assume it is setup
729 : : correctly and ignored when SHF_COMPRESSED is set. */
730 [ + + ]: 2574 : if (elfclass == ELFCLASS32)
731 : : {
732 : 390 : shdr.s32->sh_size = new_size;
733 [ + - ]: 390 : shdr.s32->sh_addralign = __libelf_type_align (ELFCLASS32,
734 : : ELF_T_CHDR);
735 : 390 : shdr.s32->sh_flags |= SHF_COMPRESSED;
736 : : }
737 : : else
738 : : {
739 : 2184 : shdr.s64->sh_size = new_size;
740 [ + - ]: 2184 : shdr.s64->sh_addralign = __libelf_type_align (ELFCLASS64,
741 : : ELF_T_CHDR);
742 : 2184 : shdr.s64->sh_flags |= SHF_COMPRESSED;
743 : : }
744 : :
745 : 2574 : __libelf_reset_rawdata (scn, out_buf, new_size, 1, ELF_T_CHDR);
746 : :
747 : : /* The section is now compressed, we could keep the uncompressed
748 : : data around, but since that might have been multiple Elf_Data
749 : : buffers let the user uncompress it explicitly again if they
750 : : want it to simplify bookkeeping. */
751 : 2574 : free (scn->zdata_base);
752 : 2574 : scn->zdata_base = NULL;
753 : :
754 : 2574 : return 1;
755 : : }
756 [ + - ]: 367968 : else if (type == 0)
757 : : {
758 : : /* Decompress/Inflate. */
759 [ + + ]: 367968 : if (compressed == 0)
760 : : {
761 : 364250 : __libelf_seterrno (ELF_E_NOT_COMPRESSED);
762 : 364250 : return -1;
763 : : }
764 : :
765 : : /* If the data is already decompressed (by elf_strptr), then we
766 : : only need to setup the rawdata and section header. XXX what
767 : : about elf_newdata? */
768 [ + + ]: 3718 : if (scn->zdata_base == NULL)
769 : : {
770 : 3714 : size_t size_out, addralign;
771 : 3714 : void *buf_out = __libelf_decompress_elf (scn, &size_out, &addralign);
772 [ - + ]: 3714 : if (buf_out == NULL)
773 : 0 : return -1;
774 : :
775 : 3714 : scn->zdata_base = buf_out;
776 : 3714 : scn->zdata_size = size_out;
777 : 3714 : scn->zdata_align = addralign;
778 : : }
779 : :
780 : : /* Note we keep the sh_entsize as is, we assume it is setup
781 : : correctly and ignored when SHF_COMPRESSED is set. */
782 [ + + ]: 3718 : if (elfclass == ELFCLASS32)
783 : : {
784 : 592 : shdr.s32->sh_size = scn->zdata_size;
785 : 592 : shdr.s32->sh_addralign = scn->zdata_align;
786 : 592 : shdr.s32->sh_flags &= ~SHF_COMPRESSED;
787 : : }
788 : : else
789 : : {
790 : 3126 : shdr.s64->sh_size = scn->zdata_size;
791 : 3126 : shdr.s64->sh_addralign = scn->zdata_align;
792 : 3126 : shdr.s64->sh_flags &= ~SHF_COMPRESSED;
793 : : }
794 : :
795 : 7436 : __libelf_reset_rawdata (scn, scn->zdata_base,
796 : : scn->zdata_size, scn->zdata_align,
797 : : __libelf_data_type (&ehdr, sh_type,
798 : 3718 : scn->zdata_align));
799 : :
800 : 3718 : return 1;
801 : : }
802 : : else
803 : : {
804 : 0 : __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE);
805 : 0 : return -1;
806 : : }
807 : : }
|