1 /* Copyright (C) 2002, 2005 Red Hat, Inc.
2    This file is part of elfutils.
3    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
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 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <gelf.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include "system.h"
28 
29 int
main(int argc,char * argv[])30 main (int argc, char *argv[])
31 {
32   if (argc < 3)
33     error (EXIT_FAILURE, 0, "usage: %s FROMNAME TONAME", argv[0]);
34 
35   elf_version (EV_CURRENT);
36 
37   int infd = open (argv[1], O_RDONLY);
38   if (infd == -1)
39     error (EXIT_FAILURE, errno, "cannot open input file '%s'", argv[1]);
40 
41   Elf *inelf = elf_begin (infd, ELF_C_READ, NULL);
42   if (inelf == NULL)
43     error (EXIT_FAILURE, 0, "problems opening '%s' as ELF file: %s",
44 	   argv[1], elf_errmsg (-1));
45 
46   int outfd = creat (argv[2], 0666);
47   if (outfd == -1)
48     error (EXIT_FAILURE, errno, "cannot open output file '%s'", argv[2]);
49 
50   Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL);
51   if (outelf == NULL)
52     error (EXIT_FAILURE, 0, "problems opening '%s' as ELF file: %s",
53 	   argv[2], elf_errmsg (-1));
54 
55   gelf_newehdr (outelf, gelf_getclass (inelf));
56 
57   GElf_Ehdr ehdr_mem;
58   GElf_Ehdr *ehdr;
59   gelf_update_ehdr (outelf, (ehdr = gelf_getehdr (inelf, &ehdr_mem)));
60 
61   if (ehdr->e_phnum > 0)
62     {
63       int cnt;
64 
65       if (gelf_newphdr (outelf, ehdr->e_phnum) == 0)
66 	error (EXIT_FAILURE, 0, "cannot create program header: %s",
67 	       elf_errmsg (-1));
68 
69       for (cnt = 0; cnt < ehdr->e_phnum; ++cnt)
70 	{
71 	  GElf_Phdr phdr_mem;
72 
73 	  gelf_update_phdr (outelf, cnt, gelf_getphdr (inelf, cnt, &phdr_mem));
74 	}
75     }
76 
77   Elf_Scn *scn = NULL;
78   while ((scn = elf_nextscn (inelf, scn)) != NULL)
79     {
80       Elf_Scn *newscn = elf_newscn (outelf);
81 
82       GElf_Shdr shdr_mem;
83       gelf_update_shdr (newscn, gelf_getshdr (scn, &shdr_mem));
84 
85       *elf_newdata (newscn) = *elf_getdata (scn, NULL);
86     }
87 
88   elf_flagelf (outelf, ELF_C_SET, ELF_F_LAYOUT);
89 
90   if (elf_update (outelf, ELF_C_WRITE) == -1)
91     error (EXIT_FAILURE, 0, "elf_update failed: %s", elf_errmsg (-1));
92 
93   elf_end (outelf);
94   close (outfd);
95 
96   elf_end (inelf);
97 
98   return 0;
99 }
100