1 /* Test program for copying a whole ELF file using libelf.
2 Copyright (C) 2018 Red Hat, Inc.
3 This file is part of elfutils.
4
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <inttypes.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33
34 #include ELFUTILS_HEADER(elf)
35 #include <gelf.h>
36
37
38 /* shstrndx is special, might overflow into section zero header sh_link. */
39 static int
setshstrndx(Elf * elf,size_t ndx)40 setshstrndx (Elf *elf, size_t ndx)
41 {
42 printf ("setshstrndx: %zd\n", ndx);
43
44 GElf_Ehdr ehdr_mem;
45 GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
46 if (ehdr == NULL)
47 return -1;
48
49 if (ndx < SHN_LORESERVE)
50 ehdr->e_shstrndx = ndx;
51 else
52 {
53 ehdr->e_shstrndx = SHN_XINDEX;
54 Elf_Scn *zscn = elf_getscn (elf, 0);
55 GElf_Shdr zshdr_mem;
56 GElf_Shdr *zshdr = gelf_getshdr (zscn, &zshdr_mem);
57 if (zshdr == NULL)
58 return -1;
59 zshdr->sh_link = ndx;
60 if (gelf_update_shdr (zscn, zshdr) == 0)
61 return -1;
62 }
63
64 if (gelf_update_ehdr (elf, ehdr) == 0)
65 return -1;
66
67 return 0;
68 }
69
70 /* Copies all elements of an ELF file either using mmap or read. */
71 static void
copy_elf(const char * in,const char * out,bool use_mmap,bool reverse_offs)72 copy_elf (const char *in, const char *out, bool use_mmap, bool reverse_offs)
73 {
74 printf ("\ncopy_elf: %s -> %s (%s,%s)\n", in, out,
75 use_mmap ? "mmap" : "read",
76 reverse_offs ? "reverse" : "same");
77
78 /* Existing ELF file. */
79 int fda = open (in, O_RDONLY);
80 if (fda < 0)
81 {
82 fprintf (stderr, "Couldn't open file '%s': %s\n",
83 in, strerror (errno));
84 exit (1);
85 }
86
87 Elf *elfa = elf_begin (fda, use_mmap ? ELF_C_READ_MMAP : ELF_C_READ, NULL);
88 if (elfa == NULL)
89 {
90 fprintf (stderr, "Couldn't open ELF file '%s': %s\n",
91 in, elf_errmsg (-1));
92 exit (1);
93 }
94
95 /* Open new file. */
96 int fdb = open (out, O_RDWR | O_CREAT | O_TRUNC, 0644);
97 if (fdb < 0)
98 {
99 fprintf (stderr, "Couldn't create file '%s': %s\n",
100 out, strerror (errno));
101 exit (1);
102 }
103
104 Elf *elfb = elf_begin (fdb, use_mmap ? ELF_C_WRITE_MMAP : ELF_C_WRITE, NULL);
105 if (elfb == NULL)
106 {
107 fprintf (stderr, "Couldn't create ELF file '%s': %s\n",
108 out, elf_errmsg (-1));
109 exit (1);
110 }
111
112 // Copy ELF header.
113 GElf_Ehdr ehdr_mema;
114 GElf_Ehdr *ehdra = gelf_getehdr (elfa, &ehdr_mema);
115 if (ehdra == NULL)
116 {
117 printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
118 exit (1);
119 }
120
121 int class = gelf_getclass (elfa);
122 // Create an ELF header.
123 if (gelf_newehdr (elfb, class) == 0)
124 {
125 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
126 exit (1);
127 }
128
129 /* New elf header is an exact copy. */
130 GElf_Ehdr ehdr_memb;
131 GElf_Ehdr *ehdrb = &ehdr_memb;
132 *ehdrb = *ehdra;
133 if (gelf_update_ehdr (elfb, ehdrb) == 0)
134 {
135 printf ("cannot update ELF header: %s\n", elf_errmsg (-1));
136 exit (1);
137 }
138
139 /* shstrndx is special. (Technically phdrnum and shdrnum are also
140 special, but they are handled by libelf.) */
141 size_t shstrndx;
142 if (elf_getshdrstrndx (elfa, &shstrndx) < 0)
143 {
144 printf ("cannot get shstrndx: %s\n", elf_errmsg (-1));
145 exit (1);
146 }
147 if (setshstrndx (elfb, shstrndx) < 0)
148 {
149 printf ("cannot set shstrndx: %s\n", elf_errmsg (-1));
150 exit (1);
151 }
152
153 /* If there are phdrs, copy them over. */
154 size_t phnum;
155 if (elf_getphdrnum (elfa, &phnum) != 0)
156 {
157 printf ("cannot get phdrs: %s\n", elf_errmsg (-1));
158 exit (1);
159 }
160
161 if (phnum > 0)
162 {
163 if (gelf_newphdr (elfb, phnum) == 0)
164 {
165 printf ("cannot create phdrs: %s\n", elf_errmsg (-1));
166 exit (1);
167 }
168
169 for (size_t cnt = 0; cnt < phnum; ++cnt)
170 {
171 GElf_Phdr phdr_mem;
172 GElf_Phdr *phdr = gelf_getphdr (elfa, cnt, &phdr_mem);
173 if (phdr == NULL)
174 {
175 printf ("couldn't get phdr %zd: %s\n", cnt, elf_errmsg (-1));
176 exit (1);
177 }
178
179 if (gelf_update_phdr (elfb, cnt, phdr) == 0)
180 {
181 printf ("couldn't update phdr %zd: %s\n", cnt, elf_errmsg (-1));
182 exit (1);
183 }
184 }
185 }
186
187 GElf_Off *offs = NULL;
188 size_t shnum;
189 if (reverse_offs)
190 {
191 if (elf_getshdrnum (elfa, &shnum) < 0)
192 {
193 printf ("couldn't get shdrnum: %s\n", elf_errmsg (-1));
194 exit (1);
195 }
196
197 offs = (GElf_Off *) malloc (shnum * sizeof (GElf_Off));
198 if (offs == NULL)
199 {
200 printf ("couldn't allocate memory for offs\n");
201 exit (1);
202 }
203 }
204
205 /* Copy all sections, headers and data. */
206 Elf_Scn *scn = NULL;
207 size_t last_off = 0;
208 GElf_Shdr last_shdr = { .sh_type = SHT_NULL };
209 while ((scn = elf_nextscn (elfa, scn)) != NULL)
210 {
211 /* Get the header. */
212 GElf_Shdr shdr;
213 if (gelf_getshdr (scn, &shdr) == NULL)
214 {
215 printf ("couldn't get shdr: %s\n", elf_errmsg (-1));
216 exit (1);
217 }
218
219 if (reverse_offs)
220 {
221 offs[last_off] = shdr.sh_offset;
222
223 if (last_shdr.sh_type != SHT_NULL
224 && last_shdr.sh_addralign == shdr.sh_addralign
225 && shdr.sh_addralign == 1
226 && last_shdr.sh_type != SHT_NOBITS
227 && shdr.sh_type != SHT_NOBITS
228 && last_shdr.sh_offset + last_shdr.sh_size == shdr.sh_offset
229 && (phnum == 0
230 || ((shdr.sh_flags & SHF_ALLOC) == 0
231 && (last_shdr.sh_flags & SHF_ALLOC) == 0)))
232 {
233 printf ("Swapping offsets of section %zd and %zd\n",
234 last_off, last_off + 1);
235 GElf_Word off = offs[last_off - 1];
236 offs[last_off - 1] = off + shdr.sh_size;
237 offs[last_off] = off;
238 last_shdr.sh_type = SHT_NULL;
239 }
240 else
241 {
242 last_shdr = shdr;
243 offs[last_off] = shdr.sh_offset;
244 }
245 last_off++;
246 }
247
248 /* Create new section. */
249 Elf_Scn *new_scn = elf_newscn (elfb);
250 if (new_scn == NULL)
251 {
252 printf ("couldn't create new section: %s\n", elf_errmsg (-1));
253 exit (1);
254 }
255
256 if (gelf_update_shdr (new_scn, &shdr) == 0)
257 {
258 printf ("couldn't update shdr: %s\n", elf_errmsg (-1));
259 exit (1);
260 }
261
262 /* Copy over section data. */
263 Elf_Data *data = NULL;
264 while ((data = elf_getdata (scn, data)) != NULL)
265 {
266 Elf_Data *new_data = elf_newdata (new_scn);
267 if (new_data == NULL)
268 {
269 printf ("couldn't create new section data: %s\n",
270 elf_errmsg (-1));
271 exit (1);
272 }
273 *new_data = *data;
274 }
275 }
276
277 if (reverse_offs)
278 {
279 last_off = 0;
280 scn = NULL;
281 while ((scn = elf_nextscn (elfb, scn)) != NULL)
282 {
283 GElf_Shdr shdr;
284 if (gelf_getshdr (scn, &shdr) == NULL)
285 {
286 printf ("couldn't get shdr for updating: %s\n", elf_errmsg (-1));
287 exit (1);
288 }
289
290 shdr.sh_offset = offs[last_off++];
291
292 if (gelf_update_shdr (scn, &shdr) == 0)
293 {
294 printf ("couldn't update shdr sh_off: %s\n", elf_errmsg (-1));
295 exit (1);
296 }
297 }
298 free (offs);
299 }
300
301 /* Write everything to disk. If there are any phdrs, or we want to
302 update the offsets, then we want the exact same layout. Do we
303 want ELF_F_PERMISSIVE? */
304 if (phnum > 0 || reverse_offs)
305 elf_flagelf (elfb, ELF_C_SET, ELF_F_LAYOUT);
306 if (elf_update (elfb, ELF_C_WRITE) < 0)
307 {
308 printf ("failure in elf_update: %s\n", elf_errmsg (-1));
309 exit (1);
310 }
311
312 if (elf_end (elfa) != 0)
313 {
314 printf ("couldn't cleanup elf '%s': %s\n", in, elf_errmsg (-1));
315 exit (1);
316 }
317
318 if (close (fda) != 0)
319 {
320 printf ("couldn't close '%s': %s\n", in, strerror (errno));
321 exit (1);
322 }
323
324 if (elf_end (elfb) != 0)
325 {
326 printf ("couldn't cleanup elf '%s': %s\n", out, elf_errmsg (-1));
327 exit (1);
328 }
329
330 if (close (fdb) != 0)
331 {
332 printf ("couldn't close '%s': %s\n", out, strerror (errno));
333 exit (1);
334 }
335 }
336
337 int
main(int argc,const char * argv[])338 main (int argc, const char *argv[])
339 {
340 elf_version (EV_CURRENT);
341
342 /* Takes the given file, and create a new identical one. */
343 if (argc < 3 || argc > 5)
344 {
345 fprintf (stderr, "elfcopy [--mmap] [--reverse-offs] in.elf out.elf\n");
346 exit (1);
347 }
348
349 int argn = 1;
350 bool use_mmap = false;
351 if (strcmp (argv[argn], "--mmap") == 0)
352 {
353 use_mmap = true;
354 argn++;
355 }
356
357 bool reverse_offs = false;
358 if (strcmp (argv[argn], "--reverse-offs") == 0)
359 {
360 reverse_offs = true;
361 argn++;
362 }
363
364 const char *in = argv[argn++];
365 const char *out = argv[argn];
366 copy_elf (in, out, use_mmap, reverse_offs);
367
368 return 0;
369 }
370