9e40686e5382769fc5654db8848108771e8c10d6
[platform/upstream/elfutils.git] / tests / update1.c
1 /* Test program for elf_update function.
2    Copyright (C) 2000, 2002, 2005 Red Hat, Inc.
3    This file is part of elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
5
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    elfutils is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <libelf.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30
31 int
32 main (int argc, char *argv[] __attribute__ ((unused)))
33 {
34   const char *fname = "xxx";
35   int fd;
36   Elf *elf;
37   Elf32_Ehdr *ehdr;
38   int i;
39
40   fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
41   if (fd == -1)
42     {
43       printf ("cannot open `%s': %s\n", fname, strerror (errno));
44       exit (1);
45     }
46
47   elf_version (EV_CURRENT);
48
49   elf = elf_begin (fd, ELF_C_WRITE, NULL);
50   if (elf == NULL)
51     {
52       printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
53       exit (1);
54     }
55
56   /* Create an ELF header.  */
57   ehdr = elf32_newehdr (elf);
58   if (ehdr == NULL)
59     {
60       printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
61       exit (1);
62     }
63
64   /* Print the ELF header values.  */
65   if (argc > 1)
66     {
67       for (i = 0; i < EI_NIDENT; ++i)
68         printf (" %02x", ehdr->e_ident[i]);
69       printf ("\
70 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
71               "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
72               "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
73               ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
74               ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
75               ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
76               ehdr->e_shnum, ehdr->e_shstrndx);
77     }
78
79   ehdr->e_ident[0] = 42;
80   ehdr->e_ident[4] = 1;
81   ehdr->e_ident[5] = 1;
82   ehdr->e_ident[6] = 2;
83   ehdr->e_ident[9] = 2;
84   ehdr->e_version = 1;
85   ehdr->e_ehsize = 1;
86
87   /* Write out the file.  */
88   if (elf_update (elf, ELF_C_WRITE) < 0)
89     {
90       printf ("failure in elf_update: %s\n", elf_errmsg (-1));
91       exit (1);
92     }
93
94   /* Create an ELF header.  */
95   ehdr = elf32_newehdr (elf);
96   if (ehdr == NULL)
97     {
98       printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
99       exit (1);
100     }
101
102   /* Print the ELF header values.  */
103   if (argc > 1)
104     {
105       for (i = 0; i < EI_NIDENT; ++i)
106         printf (" %02x", ehdr->e_ident[i]);
107       printf ("\
108 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
109               "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
110               "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
111               ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
112               ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
113               ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
114               ehdr->e_shnum, ehdr->e_shstrndx);
115     }
116
117   if (elf_end (elf) != 0)
118     {
119       printf ("failure in elf_end: %s\n", elf_errmsg (-1));
120       exit (1);
121     }
122
123   return 0;
124 }