• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test program for elf_update function.
2    Copyright (C) 2000, 2002 Red Hat, Inc.
3    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
4 
5    This program is Open Source software; you can redistribute it and/or
6    modify it under the terms of the Open Software License version 1.0 as
7    published by the Open Source Initiative.
8 
9    You should have received a copy of the Open Software License along
10    with this program; if not, you may obtain a copy of the Open Software
11    License version 1.0 from http://www.opensource.org/licenses/osl.php or
12    by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
13    3001 King Ranch Road, Ukiah, CA 95482.   */
14 
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <libelf.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 
27 int
main(int argc,char * argv[])28 main (int argc, char *argv[])
29 {
30   const char *fname = "xxx";
31   int fd;
32   Elf *elf;
33   Elf32_Ehdr *ehdr;
34   int i;
35 
36   fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
37   if (fd == -1)
38     {
39       printf ("cannot open `%s': %s\n", fname, strerror (errno));
40       exit (1);
41     }
42 
43   elf_version (EV_CURRENT);
44 
45   elf = elf_begin (fd, ELF_C_WRITE, NULL);
46   if (elf == NULL)
47     {
48       printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
49       exit (1);
50     }
51 
52   /* Create an ELF header.  */
53   ehdr = elf32_newehdr (elf);
54   if (ehdr == NULL)
55     {
56       printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
57       exit (1);
58     }
59 
60   /* Print the ELF header values.  */
61   if (argc > 1)
62     {
63       for (i = 0; i < EI_NIDENT; ++i)
64 	printf (" %02x", ehdr->e_ident[i]);
65       printf ("\
66 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
67 	      "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
68 	      "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
69 	      ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
70 	      ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
71 	      ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
72 	      ehdr->e_shnum, ehdr->e_shstrndx);
73     }
74 
75   ehdr->e_ident[0] = 42;
76   ehdr->e_ident[4] = 1;
77   ehdr->e_ident[5] = 1;
78   ehdr->e_ident[6] = 2;
79   ehdr->e_ident[9] = 2;
80   ehdr->e_version = 1;
81   ehdr->e_ehsize = 1;
82 
83   /* Write out the file.  */
84   if (elf_update (elf, ELF_C_WRITE) < 0)
85     {
86       printf ("failure in elf_update: %s\n", elf_errmsg (-1));
87       exit (1);
88     }
89 
90   /* Create an ELF header.  */
91   ehdr = elf32_newehdr (elf);
92   if (ehdr == NULL)
93     {
94       printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
95       exit (1);
96     }
97 
98   /* Print the ELF header values.  */
99   if (argc > 1)
100     {
101       for (i = 0; i < EI_NIDENT; ++i)
102 	printf (" %02x", ehdr->e_ident[i]);
103       printf ("\
104 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
105 	      "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
106 	      "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
107 	      ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
108 	      ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
109 	      ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
110 	      ehdr->e_shnum, ehdr->e_shstrndx);
111     }
112 
113   if (elf_end (elf) != 0)
114     {
115       printf ("failure in elf_end: %s\n", elf_errmsg (-1));
116       exit (1);
117     }
118 
119   return 0;
120 }
121