1 /* Copyright (C) 2002, 2005 Red Hat, Inc.
2 This file is part of elfutils.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
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/>. */
26 main (int argc, char *argv[])
29 error (EXIT_FAILURE, 0, "usage: %s FROMNAME TONAME", argv[0]);
31 elf_version (EV_CURRENT);
33 int infd = open (argv[1], O_RDONLY);
35 error (EXIT_FAILURE, errno, "cannot open input file '%s'", argv[1]);
37 Elf *inelf = elf_begin (infd, ELF_C_READ, NULL);
39 error (EXIT_FAILURE, 0, "problems opening '%s' as ELF file: %s",
40 argv[1], elf_errmsg (-1));
42 int outfd = creat (argv[2], 0666);
44 error (EXIT_FAILURE, errno, "cannot open output file '%s'", argv[2]);
46 Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL);
48 error (EXIT_FAILURE, 0, "problems opening '%s' as ELF file: %s",
49 argv[2], elf_errmsg (-1));
51 gelf_newehdr (outelf, gelf_getclass (inelf));
55 gelf_update_ehdr (outelf, (ehdr = gelf_getehdr (inelf, &ehdr_mem)));
57 if (ehdr->e_phnum > 0)
61 if (gelf_newphdr (outelf, ehdr->e_phnum) == 0)
62 error (EXIT_FAILURE, 0, "cannot create program header: %s",
65 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt)
69 gelf_update_phdr (outelf, cnt, gelf_getphdr (inelf, cnt, &phdr_mem));
74 while ((scn = elf_nextscn (inelf, scn)) != NULL)
76 Elf_Scn *newscn = elf_newscn (outelf);
79 gelf_update_shdr (newscn, gelf_getshdr (scn, &shdr_mem));
81 *elf_newdata (newscn) = *elf_getdata (scn, NULL);
84 elf_flagelf (outelf, ELF_C_SET, ELF_F_LAYOUT);
86 if (elf_update (outelf, ELF_C_WRITE) == -1)
87 error (EXIT_FAILURE, 0, "elf_update failed: %s", elf_errmsg (-1));