1 /* Copyright (C) 2015 Red Hat, Inc.
2 This file is part of elfutils.
4 This file is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include <sys/types.h>
35 main (int argc, char *argv[])
41 || (strcmp (argv[1], "elf") != 0
42 && strcmp (argv[1], "gnu") != 0))
44 printf ("Usage: (elf|gnu) files...\n");
49 if (strcmp (argv[1], "gnu") == 0)
54 elf_version (EV_CURRENT);
56 for (cnt = 2; cnt < argc; ++cnt)
58 int fd = open (argv[cnt], O_RDONLY);
60 Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
63 printf ("%s not usable %s\n", argv[cnt], elf_errmsg (-1));
69 /* To get the section names. */
71 elf_getshdrstrndx (elf, &strndx);
74 while ((scn = elf_nextscn (elf, scn)) != NULL)
76 size_t idx = elf_ndxscn (scn);
78 GElf_Shdr *shdr = gelf_getshdr (scn, &mem);
79 const char *name = elf_strptr (elf, strndx, shdr->sh_name);
80 if (shdr->sh_type == SHT_NOBITS
81 || (shdr->sh_flags & SHF_ALLOC) != 0)
83 printf ("Cannot compress %zd %s\n", idx, name);
85 else if ((shdr->sh_flags & SHF_COMPRESSED) != 0
86 || strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
88 printf ("Already compressed %zd %s\n", idx, name);
92 size_t orig_size = shdr->sh_size;
93 printf ("Lets compress %zd %s, size: %" PRId64 "\n",
94 idx, name, shdr->sh_size);
95 Elf_Data *d = elf_getdata (scn, NULL);
98 printf ("Couldn't get orig data for section %zd\n", idx);
101 /* Make a copy so we can compare after
102 compression/decompression. */
103 if (d->d_size != orig_size)
105 printf ("Unexpected data size for orig section %zd\n", idx);
108 char *orig_buf = malloc (d->d_size);
109 if (orig_size > 0 && orig_buf == NULL)
111 printf ("No memory to copy section %zd data\n", idx);
115 memcpy (orig_buf, d->d_buf, orig_size);
120 int res = elf_compress_gnu (scn, 1, 0);
124 res = elf_compress_gnu (scn, 1, ELF_CHF_FORCE);
128 printf ("elf_compress_gnu%sfailed for section %zd: %s\n",
129 forced ? " (forced) " : " ",
130 idx, elf_errmsg (-1));
136 int res = elf_compress (scn, ELFCOMPRESS_ZLIB, 0);
140 res = elf_compress (scn, ELFCOMPRESS_ZLIB, ELF_CHF_FORCE);
144 printf ("elf_compress%sfailed for section %zd: %s\n",
145 forced ? " (forced) " : " ",
146 idx, elf_errmsg (-1));
151 GElf_Shdr *newshdr = gelf_getshdr (scn, &newmem);
152 size_t new_size = newshdr->sh_size;
153 d = elf_getdata (scn, NULL);
154 // Don't check this, might depend on zlib implementation.
155 // fprintf (stderr, " new_size: %zd\n", new_size);
156 if (d->d_size != new_size)
158 printf ("Unexpected data size for compressed section %zd\n",
163 if (forced && new_size < orig_size)
165 printf ("section %zd forced to compress, but size smaller\n",
170 if (! forced && new_size >= orig_size)
172 printf ("section %zd compressed to bigger size\n",
177 if (new_size == orig_size
178 && memcmp (orig_buf, d->d_buf, orig_size) == 0)
180 printf ("section %zd didn't compress\n", idx);
186 if (elf_compress_gnu (scn, 0, 0) < 0)
188 printf ("elf_[un]compress_gnu failed for section %zd: %s\n",
189 idx, elf_errmsg (-1));
195 if (elf_compress (scn, 0, 0) < 0)
197 printf ("elf_[un]compress failed for section %zd: %s\n",
198 idx, elf_errmsg (-1));
203 GElf_Shdr *newershdr = gelf_getshdr (scn, &newermem);
204 size_t newer_size = newershdr->sh_size;
205 d = elf_getdata (scn, NULL);
206 // fprintf (stderr, " newer_size: %zd\n", newer_size);
207 if (d->d_size != newer_size)
209 printf ("Unexpected data size for compressed section %zd\n",
213 if (newer_size != orig_size
214 && memcmp (orig_buf, d->d_buf, orig_size) != 0)
216 printf ("section %zd didn't correctly uncompress\n", idx);
220 // Recompress the string table, just to make sure
221 // everything keeps working. See elf_strptr above.
222 if (! gnu && idx == strndx
223 && elf_compress (scn, ELFCOMPRESS_ZLIB, 0) < 0)
225 printf ("couldn't recompress section header strings: %s\n",