Imported Upstream version 0.155
[platform/upstream/elfutils.git] / tests / update2.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   Elf32_Phdr *phdr;
39   int i;
40
41   fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
42   if (fd == -1)
43     {
44       printf ("cannot open `%s': %s\n", fname, strerror (errno));
45       exit (1);
46     }
47
48   elf_version (EV_CURRENT);
49
50   elf = elf_begin (fd, ELF_C_WRITE, NULL);
51   if (elf == NULL)
52     {
53       printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
54       exit (1);
55     }
56
57   /* Create an ELF header.  */
58   ehdr = elf32_newehdr (elf);
59   if (ehdr == NULL)
60     {
61       printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
62       exit (1);
63     }
64
65   /* Print the ELF header values.  */
66   if (argc > 1)
67     {
68       for (i = 0; i < EI_NIDENT; ++i)
69         printf (" %02x", ehdr->e_ident[i]);
70       printf ("\
71 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
72               "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
73               "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
74               ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
75               ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
76               ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
77               ehdr->e_shnum, ehdr->e_shstrndx);
78     }
79
80   ehdr->e_ident[0] = 42;
81   ehdr->e_ident[4] = 1;
82   ehdr->e_ident[5] = 1;
83   ehdr->e_ident[6] = 2;
84   ehdr->e_type = ET_EXEC;
85   ehdr->e_version = 1;
86   ehdr->e_ehsize = 1;
87   elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
88
89   /* Create the program header.  */
90   phdr = elf32_newphdr (elf, 1);
91   if (phdr == NULL)
92     {
93       printf ("cannot create program header: %s\n", elf_errmsg (-1));
94       exit (1);
95     }
96
97   phdr[0].p_type = PT_PHDR;
98   elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
99
100   /* Let the library compute the internal structure information.  */
101   if (elf_update (elf, ELF_C_NULL) < 0)
102     {
103       printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
104       exit (1);
105     }
106
107   ehdr = elf32_getehdr (elf);
108
109   phdr[0].p_offset = ehdr->e_phoff;
110   phdr[0].p_offset = ehdr->e_phoff;
111   phdr[0].p_vaddr = ehdr->e_phoff;
112   phdr[0].p_paddr = ehdr->e_phoff;
113   phdr[0].p_flags = PF_R | PF_X;
114   phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
115   phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
116   phdr[0].p_align = sizeof (Elf32_Word);
117
118   /* Write out the file.  */
119   if (elf_update (elf, ELF_C_WRITE) < 0)
120     {
121       printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
122       exit (1);
123     }
124
125   /* Print the ELF header values.  */
126   if (argc > 1)
127     {
128       for (i = 0; i < EI_NIDENT; ++i)
129         printf (" %02x", ehdr->e_ident[i]);
130       printf ("\
131 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
132               "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
133               "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
134               ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
135               ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
136               ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
137               ehdr->e_shnum, ehdr->e_shstrndx);
138     }
139
140   if (elf_end (elf) != 0)
141     {
142       printf ("failure in elf_end: %s\n", elf_errmsg (-1));
143       exit (1);
144     }
145
146   return 0;
147 }