1 /* Copyright (C) 1999, 2000, 2001, 2002, 2005 Red Hat, Inc.
2    This file is part of elfutils.
3    Written by Ulrich Drepper <drepper@redhat.com>, 1999.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    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
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include <config.h>
19 
20 #include <libelf.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 
25 static void
print_ehdr(Elf32_Ehdr * ehdr)26 print_ehdr (Elf32_Ehdr *ehdr)
27 {
28   int n;
29 
30   for (n = 0; n < EI_NIDENT; ++n)
31     printf (" %02x", ehdr->e_ident[n]);
32 
33   printf ("\ntype = %d\nmachine = %d\nversion = %d\nentry = %d\n"
34           "phoff = %d\nshoff = %d\nflags = %d\nehsize = %d\n"
35           "phentsize = %d\nphnum = %d\nshentsize = %d\nshnum = %d\n"
36           "shstrndx = %d\n",
37           ehdr->e_type,
38           ehdr->e_machine,
39           ehdr->e_version,
40           ehdr->e_entry,
41           ehdr->e_phoff,
42           ehdr->e_shoff,
43           ehdr->e_flags,
44           ehdr->e_ehsize,
45           ehdr->e_phentsize,
46           ehdr->e_phnum,
47           ehdr->e_shentsize,
48           ehdr->e_shnum,
49           ehdr->e_shstrndx);
50 }
51 
52 int
main(int argc,char * argv[])53 main (int argc, char *argv[] __attribute__ ((unused)))
54 {
55   Elf *elf;
56   int result = 0;
57   int fd;
58   char fname[] = "newfile-XXXXXX";
59 
60   fd = mkstemp (fname);
61   if (fd == -1)
62     {
63       printf ("cannot create temporary file: %m\n");
64       exit (1);
65     }
66   /* Remove the file when we exit.  */
67   unlink (fname);
68 
69   elf_version (EV_CURRENT);
70   elf = elf_begin (fd, ELF_C_WRITE, NULL);
71   if (elf == NULL)
72     {
73       printf ("elf_begin: %s\n", elf_errmsg (-1));
74       result = 1;
75     }
76   else
77     {
78       if (elf32_newehdr (elf) == NULL)
79 	{
80 	  printf ("elf32_newehdr: %s\n", elf_errmsg (-1));
81 	  result = 1;
82 	}
83       else
84         {
85 	  Elf32_Ehdr *ehdr = elf32_getehdr (elf);
86 
87 	  if (ehdr == NULL)
88 	    {
89 	      printf ("elf32_getehdr: %s\n", elf_errmsg (-1));
90 	      result = 1;
91 	    }
92 	  else
93 	    {
94 	      int i;
95 
96 	      if (argc > 1)
97 		/* Use argc as a debugging flag.  */
98 		print_ehdr (ehdr);
99 
100 	      /* Some tests.  */
101 	      for (i = 0; i < EI_NIDENT; ++i)
102 		if (ehdr->e_ident[i] != 0)
103 		  {
104 		    printf ("ehdr->e_ident[%d] != 0\n", i);
105 		    result = 1;
106 		    break;
107 		  }
108 
109 #define VALUE_TEST(name, val) \
110 	      if (ehdr->name != val)					      \
111 	        {							      \
112 		  printf ("ehdr->%s != %d\n", #name, val);		      \
113 		  result = 1;						      \
114 		}
115 #define ZERO_TEST(name) VALUE_TEST (name, 0)
116 	      ZERO_TEST (e_type);
117 	      ZERO_TEST (e_machine);
118 	      ZERO_TEST (e_version);
119 	      ZERO_TEST (e_entry);
120 	      ZERO_TEST (e_phoff);
121 	      ZERO_TEST (e_shoff);
122 	      ZERO_TEST (e_flags);
123 	      ZERO_TEST (e_ehsize);
124 	      ZERO_TEST (e_phentsize);
125 	      ZERO_TEST (e_phnum);
126 	      ZERO_TEST (e_shentsize);
127 	      ZERO_TEST (e_shnum);
128 	      ZERO_TEST (e_shstrndx);
129 
130 	      if (elf32_newphdr (elf, 10) == NULL)
131 		{
132 		  printf ("elf32_newphdr: %s\n", elf_errmsg (-1));
133 		  result = 1;
134 		}
135 	      else
136 		{
137 		  if (argc > 1)
138 		    print_ehdr (ehdr);
139 
140 		  ehdr = elf32_getehdr (elf);
141 		  if (ehdr == NULL)
142 		    {
143 		      printf ("elf32_getehdr (#2): %s\n", elf_errmsg (-1));
144 		      result = 1;
145 		    }
146 		  else
147 		    {
148 		      ZERO_TEST (e_type);
149 		      ZERO_TEST (e_machine);
150 		      ZERO_TEST (e_version);
151 		      ZERO_TEST (e_entry);
152 		      ZERO_TEST (e_phoff);
153 		      ZERO_TEST (e_shoff);
154 		      ZERO_TEST (e_flags);
155 		      ZERO_TEST (e_ehsize);
156 		      VALUE_TEST (e_phentsize, (int) sizeof (Elf32_Phdr));
157 		      VALUE_TEST (e_phnum, 10);
158 		      ZERO_TEST (e_shentsize);
159 		      ZERO_TEST (e_shnum);
160 		      ZERO_TEST (e_shstrndx);
161 		    }
162 		}
163 	    }
164         }
165 
166       (void) elf_end (elf);
167     }
168 
169   return result;
170 }
171