1 /* Copyright (C) 2015 Red Hat, Inc.
2    This file is part of elfutils.
3 
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.
8 
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.
13 
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/>.  */
16 
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <libelf.h>
26 #include <gelf.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 
34 int
main(int argc,char * argv[])35 main (int argc, char *argv[])
36 {
37   int result = 0;
38   int cnt;
39 
40   if (argc < 3
41       || (strcmp (argv[1], "elf") != 0
42 	  && strcmp (argv[1], "gnu") != 0))
43     {
44       printf ("Usage: (elf|gnu) files...\n");
45       return -1;
46     }
47 
48   int gnu;
49   if (strcmp (argv[1], "gnu") == 0)
50     gnu = 1;
51   else
52     gnu = 0;
53 
54   elf_version (EV_CURRENT);
55 
56   for (cnt = 2; cnt < argc; ++cnt)
57     {
58       int fd = open (argv[cnt], O_RDONLY);
59 
60       Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
61       if (elf == NULL)
62 	{
63 	  printf ("%s not usable %s\n", argv[cnt], elf_errmsg (-1));
64 	  result = 1;
65 	  close (fd);
66 	  continue;
67 	}
68 
69       /* To get the section names.  */
70       size_t strndx;
71       elf_getshdrstrndx (elf, &strndx);
72 
73       Elf_Scn *scn = NULL;
74       while ((scn = elf_nextscn (elf, scn)) != NULL)
75 	{
76 	  size_t idx = elf_ndxscn (scn);
77 	  GElf_Shdr mem;
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)
82 	    {
83 	      printf ("Cannot compress %zd %s\n", idx, name);
84 	    }
85 	  else if ((shdr->sh_flags & SHF_COMPRESSED) != 0
86 		   || strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
87 	    {
88 	      printf ("Already compressed %zd %s\n", idx, name);
89 	    }
90 	  else
91 	    {
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);
96 	      if (d == NULL)
97 		{
98 		  printf ("Couldn't get orig data for section %zd\n", idx);
99 		  return -1;
100 		}
101 	      /* Make a copy so we can compare after
102 		 compression/decompression.  */
103 	      if (d->d_size != orig_size)
104 		{
105 		  printf ("Unexpected data size for orig section %zd\n", idx);
106 		  return -1;
107 		}
108 	      char *orig_buf = NULL;
109 	      if (orig_size > 0)
110 		{
111 		  orig_buf = malloc (d->d_size);
112 		  if (orig_buf == NULL)
113 		    {
114 		      printf ("No memory to copy section %zd data\n", idx);
115 		      return -1;
116 		    }
117 		  memcpy (orig_buf, d->d_buf, orig_size);
118 		}
119 
120 	      bool forced = false;
121 	      if (gnu)
122 		{
123 		  int res = elf_compress_gnu (scn, 1, 0);
124 		  if (res == 0)
125 		    {
126 		      forced = true;
127 		      res = elf_compress_gnu (scn, 1, ELF_CHF_FORCE);
128 		    }
129 		  if (res < 0)
130 		    {
131 		      printf ("elf_compress_gnu%sfailed for section %zd: %s\n",
132 			      forced ? " (forced) " : " ",
133 			      idx, elf_errmsg (-1));
134 		      return -1;
135 		    }
136 		}
137 	      else
138 		{
139 		  int res = elf_compress (scn, ELFCOMPRESS_ZLIB, 0);
140 		  if (res == 0)
141 		    {
142 		      forced = true;
143 		      res = elf_compress (scn, ELFCOMPRESS_ZLIB, ELF_CHF_FORCE);
144 		    }
145 		  if (res < 0)
146 		    {
147 		      printf ("elf_compress%sfailed for section %zd: %s\n",
148 			      forced ? " (forced) " : " ",
149 			      idx, elf_errmsg (-1));
150 		      return -1;
151 		    }
152 		}
153 	      GElf_Shdr newmem;
154 	      GElf_Shdr *newshdr = gelf_getshdr (scn, &newmem);
155 	      size_t new_size = newshdr->sh_size;
156 	      d = elf_getdata (scn, NULL);
157 	      // Don't check this, might depend on zlib implementation.
158 	      // fprintf (stderr, "  new_size: %zd\n", new_size);
159 	      if (d->d_size != new_size)
160 		{
161 		  printf ("Unexpected data size for compressed section %zd\n",
162 			  idx);
163 		  return -1;
164 		}
165 
166 	      if (forced && new_size < orig_size)
167 		{
168 		  printf ("section %zd forced to compress, but size smaller\n",
169 			  idx);
170 		  return -1;
171 		}
172 
173 	      if (! forced && new_size >= orig_size)
174 		{
175 		  printf ("section %zd compressed to bigger size\n",
176 			  idx);
177 		  return -1;
178 		}
179 
180 	      if (new_size == orig_size
181 		  && (orig_buf == NULL
182 		      || memcmp (orig_buf, d->d_buf, orig_size) == 0))
183 		{
184 		  printf ("section %zd didn't compress\n", idx);
185 		  return -1;
186 		}
187 
188 	      if (gnu)
189 		{
190 		  if (elf_compress_gnu (scn, 0, 0) < 0)
191 		    {
192 		      printf ("elf_[un]compress_gnu failed for section %zd: %s\n",
193 			      idx, elf_errmsg (-1));
194 		      return -1;
195 		    }
196 		}
197 	      else
198 		{
199 		  if (elf_compress (scn, 0, 0) < 0)
200 		    {
201 		      printf ("elf_[un]compress failed for section %zd: %s\n",
202 			      idx, elf_errmsg (-1));
203 		      return -1;
204 		    }
205 		}
206 	      GElf_Shdr newermem;
207 	      GElf_Shdr *newershdr = gelf_getshdr (scn, &newermem);
208 	      size_t newer_size = newershdr->sh_size;
209 	      d = elf_getdata (scn, NULL);
210 	      // fprintf (stderr, "  newer_size: %zd\n", newer_size);
211 	      if (d->d_size != newer_size)
212 		{
213 		  printf ("Unexpected data size for compressed section %zd\n",
214 			  idx);
215 		  return -1;
216 		}
217 	      if (newer_size != orig_size
218 		  && (orig_buf == NULL
219 		      || memcmp (orig_buf, d->d_buf, orig_size) != 0))
220 		{
221 		  printf ("section %zd didn't correctly uncompress\n", idx);
222 		  return -1;
223 		}
224 	      free (orig_buf);
225 	      // Recompress the string table, just to make sure
226 	      // everything keeps working. See elf_strptr above.
227 	      if (! gnu && idx == strndx
228 		  && elf_compress (scn, ELFCOMPRESS_ZLIB, 0) < 0)
229 		{
230 		  printf ("couldn't recompress section header strings: %s\n",
231 			  elf_errmsg (-1));
232 		  return -1;
233 		}
234 	    }
235 	}
236 
237       elf_end (elf);
238       close (fd);
239     }
240 
241   return result;
242 }
243