Home | History | Annotate | Download | only in tests
      1 /* Copyright (C) 2002, 2005 Red Hat, Inc.
      2    This file is part of Red Hat elfutils.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      4 
      5    Red Hat elfutils is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by the
      7    Free Software Foundation; version 2 of the License.
      8 
      9    Red Hat 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 GNU
     12    General Public License for more details.
     13 
     14    You should have received a copy of the GNU General Public License along
     15    with Red Hat elfutils; if not, write to the Free Software Foundation,
     16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     17 
     18    Red Hat elfutils is an included package of the Open Invention Network.
     19    An included package of the Open Invention Network is a package for which
     20    Open Invention Network licensees cross-license their patents.  No patent
     21    license is granted, either expressly or impliedly, by designation as an
     22    included package.  Should you wish to participate in the Open Invention
     23    Network licensing program, please visit www.openinventionnetwork.com
     24    <http://www.openinventionnetwork.com>.  */
     25 
     26 #include <errno.h>
     27 #include <error.h>
     28 #include <fcntl.h>
     29 #include <gelf.h>
     30 #include <stdlib.h>
     31 #include <unistd.h>
     32 
     33 int
     34 main (int argc, char *argv[])
     35 {
     36   if (argc < 3)
     37     error (EXIT_FAILURE, 0, "usage: %s FROMNAME TONAME", argv[0]);
     38 
     39   elf_version (EV_CURRENT);
     40 
     41   int infd = open (argv[1], O_RDONLY);
     42   if (infd == -1)
     43     error (EXIT_FAILURE, errno, "cannot open input file '%s'", argv[1]);
     44 
     45   Elf *inelf = elf_begin (infd, ELF_C_READ, NULL);
     46   if (inelf == NULL)
     47     error (EXIT_FAILURE, 0, "problems opening '%s' as ELF file: %s",
     48 	   argv[1], elf_errmsg (-1));
     49 
     50   int outfd = creat (argv[2], 0666);
     51   if (outfd == -1)
     52     error (EXIT_FAILURE, errno, "cannot open output file '%s'", argv[2]);
     53 
     54   Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL);
     55   if (outelf == NULL)
     56     error (EXIT_FAILURE, 0, "problems opening '%s' as ELF file: %s",
     57 	   argv[2], elf_errmsg (-1));
     58 
     59   gelf_newehdr (outelf, gelf_getclass (inelf));
     60 
     61   GElf_Ehdr ehdr_mem;
     62   GElf_Ehdr *ehdr;
     63   gelf_update_ehdr (outelf, (ehdr = gelf_getehdr (inelf, &ehdr_mem)));
     64 
     65   if (ehdr->e_phnum > 0)
     66     {
     67       int cnt;
     68 
     69       if (gelf_newphdr (outelf, ehdr->e_phnum) == 0)
     70 	error (EXIT_FAILURE, 0, "cannot create program header: %s",
     71 	       elf_errmsg (-1));
     72 
     73       for (cnt = 0; cnt < ehdr->e_phnum; ++cnt)
     74 	{
     75 	  GElf_Phdr phdr_mem;
     76 
     77 	  gelf_update_phdr (outelf, cnt, gelf_getphdr (inelf, cnt, &phdr_mem));
     78 	}
     79     }
     80 
     81   Elf_Scn *scn = NULL;
     82   while ((scn = elf_nextscn (inelf, scn)) != NULL)
     83     {
     84       Elf_Scn *newscn = elf_newscn (outelf);
     85 
     86       GElf_Shdr shdr_mem;
     87       gelf_update_shdr (newscn, gelf_getshdr (scn, &shdr_mem));
     88 
     89       *elf_newdata (newscn) = *elf_getdata (scn, NULL);
     90     }
     91 
     92   elf_flagelf (outelf, ELF_C_SET, ELF_F_LAYOUT);
     93 
     94   if (elf_update (outelf, ELF_C_WRITE) == -1)
     95     error (EXIT_FAILURE, 0, "elf_update failed: %s", elf_errmsg (-1));
     96 
     97   elf_end (outelf);
     98   close (outfd);
     99 
    100   elf_end (inelf);
    101 
    102   return 0;
    103 }
    104