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