756d0159d98791cc5d3720e7014fd116d760971f
[platform/upstream/elfutils.git] / tests / update3.c
1 /* Test program for elf_update function.
2    Copyright (C) 2000, 2002, 2005 Red Hat, Inc.
3    This file is part of Red Hat elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
5
6    Red Hat elfutils is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by the
8    Free Software Foundation; version 2 of the License.
9
10    Red Hat 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 GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with Red Hat elfutils; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
19    Red Hat elfutils is an included package of the Open Invention Network.
20    An included package of the Open Invention Network is a package for which
21    Open Invention Network licensees cross-license their patents.  No patent
22    license is granted, either expressly or impliedly, by designation as an
23    included package.  Should you wish to participate in the Open Invention
24    Network licensing program, please visit www.openinventionnetwork.com
25    <http://www.openinventionnetwork.com>.  */
26
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <libelf.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include ELFUTILS_HEADER(ebl)
39
40
41 int
42 main (int argc, char *argv[] __attribute__ ((unused)))
43 {
44   const char *fname = "xxx";
45   int fd;
46   Elf *elf;
47   Elf32_Ehdr *ehdr;
48   Elf32_Phdr *phdr;
49   Elf_Scn *scn;
50   Elf32_Shdr *shdr;
51   Elf_Data *data;
52   struct Ebl_Strtab *shst;
53   struct Ebl_Strent *shstrtabse;
54   int i;
55
56   fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
57   if (fd == -1)
58     {
59       printf ("cannot open `%s': %s\n", fname, strerror (errno));
60       exit (1);
61     }
62
63   elf_version (EV_CURRENT);
64
65   elf_fill (0x42);
66
67   elf = elf_begin (fd, ELF_C_WRITE, NULL);
68   if (elf == NULL)
69     {
70       printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
71       exit (1);
72     }
73
74   /* Create an ELF header.  */
75   ehdr = elf32_newehdr (elf);
76   if (ehdr == NULL)
77     {
78       printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
79       exit (1);
80     }
81
82   /* Print the ELF header values.  */
83   if (argc > 1)
84     {
85       for (i = 0; i < EI_NIDENT; ++i)
86         printf (" %02x", ehdr->e_ident[i]);
87       printf ("\
88 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
89               "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
90               "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
91               ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
92               ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
93               ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
94               ehdr->e_shnum, ehdr->e_shstrndx);
95     }
96
97   ehdr->e_ident[0] = 42;
98   ehdr->e_ident[4] = 1;
99   ehdr->e_ident[5] = 1;
100   ehdr->e_ident[6] = 2;
101   ehdr->e_type = ET_EXEC;
102   ehdr->e_version = 1;
103   ehdr->e_ehsize = 1;
104   elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
105
106   /* Create the program header.  */
107   phdr = elf32_newphdr (elf, 1);
108   if (phdr == NULL)
109     {
110       printf ("cannot create program header: %s\n", elf_errmsg (-1));
111       exit (1);
112     }
113
114   phdr[0].p_type = PT_PHDR;
115   elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
116
117   shst = ebl_strtabinit (true);
118
119   scn = elf_newscn (elf);
120   if (scn == NULL)
121     {
122       printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1));
123       exit (1);
124     }
125   shdr = elf32_getshdr (scn);
126   if (shdr == NULL)
127     {
128       printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1));
129       exit (1);
130     }
131
132   shstrtabse = ebl_strtabadd (shst, ".shstrtab", 0);
133
134   shdr->sh_type = SHT_STRTAB;
135   shdr->sh_flags = 0;
136   shdr->sh_addr = 0;
137   shdr->sh_link = SHN_UNDEF;
138   shdr->sh_info = SHN_UNDEF;
139   shdr->sh_addralign = 1;
140   shdr->sh_entsize = 0;
141
142   /* We have to store the section index in the ELF header.  */
143   ehdr->e_shstrndx = elf_ndxscn (scn);
144
145   data = elf_newdata (scn);
146   if (data == NULL)
147     {
148       printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1));
149       exit (1);
150     }
151
152   /* No more sections, finalize the section header string table.  */
153   ebl_strtabfinalize (shst, data);
154
155   shdr->sh_name = ebl_strtaboffset (shstrtabse);
156
157   /* Let the library compute the internal structure information.  */
158   if (elf_update (elf, ELF_C_NULL) < 0)
159     {
160       printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
161       exit (1);
162     }
163
164   ehdr = elf32_getehdr (elf);
165
166   phdr[0].p_offset = ehdr->e_phoff;
167   phdr[0].p_offset = ehdr->e_phoff;
168   phdr[0].p_vaddr = ehdr->e_phoff;
169   phdr[0].p_paddr = ehdr->e_phoff;
170   phdr[0].p_flags = PF_R | PF_X;
171   phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
172   phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
173   phdr[0].p_align = sizeof (Elf32_Word);
174
175   /* Write out the file.  */
176   if (elf_update (elf, ELF_C_WRITE) < 0)
177     {
178       printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
179       exit (1);
180     }
181
182   /* We don't need the string table anymore.  */
183   ebl_strtabfree (shst);
184
185   /* And the data allocated in the .shstrtab section.  */
186   free (data->d_buf);
187
188   /* Print the ELF header values.  */
189   if (argc > 1)
190     {
191       for (i = 0; i < EI_NIDENT; ++i)
192         printf (" %02x", ehdr->e_ident[i]);
193       printf ("\
194 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
195               "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
196               "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
197               ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
198               ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
199               ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
200               ehdr->e_shnum, ehdr->e_shstrndx);
201     }
202
203   if (elf_end (elf) != 0)
204     {
205       printf ("failure in elf_end: %s\n", elf_errmsg (-1));
206       exit (1);
207     }
208
209   return 0;
210 }