summaryrefslogtreecommitdiff
path: root/tests/newfile.c
blob: 5eabdcb70a4385c6b579dbe49e015d2a08d2687b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (C) 1999, 2000, 2001, 2002, 2005 Red Hat, Inc.
   This file is part of elfutils.
   Written by Ulrich Drepper <drepper@redhat.com>, 1999.

   This file is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>

#include <libelf.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void
print_ehdr (Elf32_Ehdr *ehdr)
{
  int n;

  for (n = 0; n < EI_NIDENT; ++n)
    printf (" %02x", ehdr->e_ident[n]);

  printf ("\ntype = %d\nmachine = %d\nversion = %d\nentry = %d\n"
          "phoff = %d\nshoff = %d\nflags = %d\nehsize = %d\n"
          "phentsize = %d\nphnum = %d\nshentsize = %d\nshnum = %d\n"
          "shstrndx = %d\n",
          ehdr->e_type,
          ehdr->e_machine,
          ehdr->e_version,
          ehdr->e_entry,
          ehdr->e_phoff,
          ehdr->e_shoff,
          ehdr->e_flags,
          ehdr->e_ehsize,
          ehdr->e_phentsize,
          ehdr->e_phnum,
          ehdr->e_shentsize,
          ehdr->e_shnum,
          ehdr->e_shstrndx);
}

int
main (int argc, char *argv[] __attribute__ ((unused)))
{
  Elf *elf;
  int result = 0;
  int fd;
  char fname[] = "newfile-XXXXXX";

  fd = mkstemp (fname);
  if (fd == -1)
    {
      printf ("cannot create temporary file: %m\n");
      exit (1);
    }
  /* Remove the file when we exit.  */
  unlink (fname);

  elf_version (EV_CURRENT);
  elf = elf_begin (fd, ELF_C_WRITE, NULL);
  if (elf == NULL)
    {
      printf ("elf_begin: %s\n", elf_errmsg (-1));
      result = 1;
    }
  else
    {
      if (elf32_newehdr (elf) == NULL)
	{
	  printf ("elf32_newehdr: %s\n", elf_errmsg (-1));
	  result = 1;
	}
      else
        {
	  Elf32_Ehdr *ehdr = elf32_getehdr (elf);

	  if (ehdr == NULL)
	    {
	      printf ("elf32_getehdr: %s\n", elf_errmsg (-1));
	      result = 1;
	    }
	  else
	    {
	      int i;

	      if (argc > 1)
		/* Use argc as a debugging flag.  */
		print_ehdr (ehdr);

	      /* Some tests.  */
	      for (i = 0; i < EI_NIDENT; ++i)
		if (ehdr->e_ident[i] != 0)
		  {
		    printf ("ehdr->e_ident[%d] != 0\n", i);
		    result = 1;
		    break;
		  }

#define VALUE_TEST(name, val) \
	      if (ehdr->name != val)					      \
	        {							      \
		  printf ("ehdr->%s != %d\n", #name, val);		      \
		  result = 1;						      \
		}
#define ZERO_TEST(name) VALUE_TEST (name, 0)
	      ZERO_TEST (e_type);
	      ZERO_TEST (e_machine);
	      ZERO_TEST (e_version);
	      ZERO_TEST (e_entry);
	      ZERO_TEST (e_phoff);
	      ZERO_TEST (e_shoff);
	      ZERO_TEST (e_flags);
	      ZERO_TEST (e_ehsize);
	      ZERO_TEST (e_phentsize);
	      ZERO_TEST (e_phnum);
	      ZERO_TEST (e_shentsize);
	      ZERO_TEST (e_shnum);
	      ZERO_TEST (e_shstrndx);

	      if (elf32_newphdr (elf, 10) == NULL)
		{
		  printf ("elf32_newphdr: %s\n", elf_errmsg (-1));
		  result = 1;
		}
	      else
		{
		  if (argc > 1)
		    print_ehdr (ehdr);

		  ehdr = elf32_getehdr (elf);
		  if (ehdr == NULL)
		    {
		      printf ("elf32_getehdr (#2): %s\n", elf_errmsg (-1));
		      result = 1;
		    }
		  else
		    {
		      ZERO_TEST (e_type);
		      ZERO_TEST (e_machine);
		      ZERO_TEST (e_version);
		      ZERO_TEST (e_entry);
		      ZERO_TEST (e_phoff);
		      ZERO_TEST (e_shoff);
		      ZERO_TEST (e_flags);
		      ZERO_TEST (e_ehsize);
		      VALUE_TEST (e_phentsize, (int) sizeof (Elf32_Phdr));
		      VALUE_TEST (e_phnum, 10);
		      ZERO_TEST (e_shentsize);
		      ZERO_TEST (e_shnum);
		      ZERO_TEST (e_shstrndx);
		    }
		}
	    }
        }

      (void) elf_end (elf);
    }

  return result;
}