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