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.
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.
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.
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/>. */
26 print_ehdr (Elf32_Ehdr *ehdr)
30 for (n = 0; n < EI_NIDENT; ++n)
31 printf (" %02x", ehdr->e_ident[n]);
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"
53 main (int argc, char *argv[] __attribute__ ((unused)))
58 char fname[] = "newfile-XXXXXX";
63 printf ("cannot create temporary file: %m\n");
66 /* Remove the file when we exit. */
69 elf_version (EV_CURRENT);
70 elf = elf_begin (fd, ELF_C_WRITE, NULL);
73 printf ("elf_begin: %s\n", elf_errmsg (-1));
78 if (elf32_newehdr (elf) == NULL)
80 printf ("elf32_newehdr: %s\n", elf_errmsg (-1));
85 Elf32_Ehdr *ehdr = elf32_getehdr (elf);
89 printf ("elf32_getehdr: %s\n", elf_errmsg (-1));
97 /* Use argc as a debugging flag. */
101 for (i = 0; i < EI_NIDENT; ++i)
102 if (ehdr->e_ident[i] != 0)
104 printf ("ehdr->e_ident[%d] != 0\n", i);
109 #define VALUE_TEST(name, val) \
110 if (ehdr->name != val) \
112 printf ("ehdr->%s != %d\n", #name, val); \
115 #define ZERO_TEST(name) VALUE_TEST (name, 0)
117 ZERO_TEST (e_machine);
118 ZERO_TEST (e_version);
123 ZERO_TEST (e_ehsize);
124 ZERO_TEST (e_phentsize);
126 ZERO_TEST (e_shentsize);
128 ZERO_TEST (e_shstrndx);
130 if (elf32_newphdr (elf, 10) == NULL)
132 printf ("elf32_newphdr: %s\n", elf_errmsg (-1));
140 ehdr = elf32_getehdr (elf);
143 printf ("elf32_getehdr (#2): %s\n", elf_errmsg (-1));
149 ZERO_TEST (e_machine);
150 ZERO_TEST (e_version);
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);
160 ZERO_TEST (e_shstrndx);
166 (void) elf_end (elf);