blob: 38a6859e8b2b71474b4c783451495f33aae4760b [file] [log] [blame]
Roland McGrathd804e722005-12-08 01:35:42 +00001/* Copyright (C) 2002, 2005 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02002 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00003 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
4
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Mark Wielaardde2ed972012-06-05 17:15:16 +020010 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000011 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000014
Mark Wielaardde2ed972012-06-05 17:15:16 +020015 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000017
Josh Stone34254542015-10-09 10:10:37 -070018#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000022#include <errno.h>
23#include <error.h>
24#include <fcntl.h>
25#include <gelf.h>
26#include <stdlib.h>
27#include <unistd.h>
28
29int
30main (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
Roland McGrathd804e722005-12-08 01:35:42 +000093 elf_end (outelf);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000094 close (outfd);
95
Roland McGrathd804e722005-12-08 01:35:42 +000096 elf_end (inelf);
97
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000098 return 0;
99}