1 /* Compress or decompress a section.
2 Copyright (C) 2015 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
38 elf_compress_gnu (Elf_Scn *scn, int inflate, unsigned int flags)
43 if ((flags & ~ELF_CHF_FORCE) != 0)
45 __libelf_seterrno (ELF_E_INVALID_OPERAND);
49 bool force = (flags & ELF_CHF_FORCE) != 0;
53 if (gelf_getehdr (elf, &ehdr) == NULL)
56 int elfclass = elf->class;
57 int elfdata = ehdr.e_ident[EI_DATA];
61 Elf64_Xword sh_addralign;
62 if (elfclass == ELFCLASS32)
64 Elf32_Shdr *shdr = elf32_getshdr (scn);
68 sh_flags = shdr->sh_flags;
69 sh_type = shdr->sh_type;
70 sh_addralign = shdr->sh_addralign;
74 Elf64_Shdr *shdr = elf64_getshdr (scn);
78 sh_flags = shdr->sh_flags;
79 sh_type = shdr->sh_type;
80 sh_addralign = shdr->sh_addralign;
83 /* Allocated sections, or sections that are already are compressed
84 cannot (also) be GNU compressed. */
85 if ((sh_flags & SHF_ALLOC) != 0 || (sh_flags & SHF_COMPRESSED))
87 __libelf_seterrno (ELF_E_INVALID_SECTION_FLAGS);
91 if (sh_type == SHT_NULL || sh_type == SHT_NOBITS)
93 __libelf_seterrno (ELF_E_INVALID_SECTION_TYPE);
97 /* For GNU compression we cannot really know whether the section is
98 already compressed or not. Just try and see what happens... */
99 // int compressed = (sh_flags & SHF_COMPRESSED);
102 size_t hsize = 4 + 8; /* GNU "ZLIB" + 8 byte size. */
103 size_t orig_size, new_size, orig_addralign;
104 void *out_buf = __libelf_compress (scn, hsize, elfdata,
105 &orig_size, &orig_addralign,
107 /* use_zstd */ false);
109 /* Compression would make section larger, don't change anything. */
110 if (out_buf == (void *) -1)
113 /* Compression failed, return error. */
117 uint64_t be64_size = htobe64 (orig_size);
118 memmove (out_buf, "ZLIB", 4);
119 memmove (out_buf + 4, &be64_size, sizeof (be64_size));
121 /* We don't know anything about sh_entsize, sh_addralign and
122 sh_flags won't have a SHF_COMPRESSED hint in the GNU format.
123 Just adjust the sh_size. */
124 if (elfclass == ELFCLASS32)
126 Elf32_Shdr *shdr = elf32_getshdr (scn);
127 shdr->sh_size = new_size;
131 Elf64_Shdr *shdr = elf64_getshdr (scn);
132 shdr->sh_size = new_size;
135 __libelf_reset_rawdata (scn, out_buf, new_size, 1, ELF_T_BYTE);
137 /* The section is now compressed, we could keep the uncompressed
138 data around, but since that might have been multiple Elf_Data
139 buffers let the user uncompress it explicitly again if they
140 want it to simplify bookkeeping. */
141 scn->zdata_base = NULL;
145 else if (inflate == 0)
147 /* In theory the user could have constructed a compressed section
148 by hand. And in practice they do. For example when copying
149 a section from one file to another using elf_newdata. So we
150 have to use elf_getdata (not elf_rawdata). */
151 Elf_Data *data = elf_getdata (scn, NULL);
155 size_t hsize = 4 + 8; /* GNU "ZLIB" + 8 byte size. */
156 if (data->d_size < hsize || memcmp (data->d_buf, "ZLIB", 4) != 0)
158 __libelf_seterrno (ELF_E_NOT_COMPRESSED);
162 /* There is a 12-byte header of "ZLIB" followed by
163 an 8-byte big-endian size. There is only one type and
164 Alignment isn't preserved separately. */
166 memcpy (&gsize, data->d_buf + 4, sizeof gsize);
167 gsize = be64toh (gsize);
169 /* One more sanity check, size should be bigger than original
170 data size plus some overhead (4 chars ZLIB + 8 bytes size + 6
171 bytes zlib stream overhead + 5 bytes overhead max for one 16K
172 block) and should fit into a size_t. */
173 if (gsize + 4 + 8 + 6 + 5 < data->d_size || gsize > SIZE_MAX)
175 __libelf_seterrno (ELF_E_NOT_COMPRESSED);
180 size_t size_in = data->d_size - hsize;
181 void *buf_in = data->d_buf + hsize;
182 void *buf_out = __libelf_decompress (ELFCOMPRESS_ZLIB, buf_in, size_in, size);
186 /* We don't know anything about sh_entsize, sh_addralign and
187 sh_flags won't have a SHF_COMPRESSED hint in the GNU format.
188 Just adjust the sh_size. */
189 if (elfclass == ELFCLASS32)
191 Elf32_Shdr *shdr = elf32_getshdr (scn);
192 shdr->sh_size = size;
196 Elf64_Shdr *shdr = elf64_getshdr (scn);
197 shdr->sh_size = size;
200 __libelf_reset_rawdata (scn, buf_out, size, sh_addralign,
201 __libelf_data_type (&ehdr, sh_type,
204 scn->zdata_base = buf_out;
210 __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE);