Blame tests/newfile.c

Packit 032894
/* Copyright (C) 1999, 2000, 2001, 2002, 2005 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 1999.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of the GNU General Public License as published by
Packit 032894
   the Free Software Foundation; either version 3 of the License, or
Packit 032894
   (at your option) any later version.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 032894
   GNU General Public License for more details.
Packit 032894
Packit 032894
   You should have received a copy of the GNU General Public License
Packit 032894
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#include <config.h>
Packit 032894
Packit 032894
#include <libelf.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
static void
Packit 032894
print_ehdr (Elf32_Ehdr *ehdr)
Packit 032894
{
Packit 032894
  int n;
Packit 032894
Packit 032894
  for (n = 0; n < EI_NIDENT; ++n)
Packit 032894
    printf (" %02x", ehdr->e_ident[n]);
Packit 032894
Packit 032894
  printf ("\ntype = %d\nmachine = %d\nversion = %d\nentry = %d\n"
Packit 032894
          "phoff = %d\nshoff = %d\nflags = %d\nehsize = %d\n"
Packit 032894
          "phentsize = %d\nphnum = %d\nshentsize = %d\nshnum = %d\n"
Packit 032894
          "shstrndx = %d\n",
Packit 032894
          ehdr->e_type,
Packit 032894
          ehdr->e_machine,
Packit 032894
          ehdr->e_version,
Packit 032894
          ehdr->e_entry,
Packit 032894
          ehdr->e_phoff,
Packit 032894
          ehdr->e_shoff,
Packit 032894
          ehdr->e_flags,
Packit 032894
          ehdr->e_ehsize,
Packit 032894
          ehdr->e_phentsize,
Packit 032894
          ehdr->e_phnum,
Packit 032894
          ehdr->e_shentsize,
Packit 032894
          ehdr->e_shnum,
Packit 032894
          ehdr->e_shstrndx);
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[] __attribute__ ((unused)))
Packit 032894
{
Packit 032894
  Elf *elf;
Packit 032894
  int result = 0;
Packit 032894
  int fd;
Packit 032894
  char fname[] = "newfile-XXXXXX";
Packit 032894
Packit 032894
  fd = mkstemp (fname);
Packit 032894
  if (fd == -1)
Packit 032894
    {
Packit 032894
      printf ("cannot create temporary file: %m\n");
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
  /* Remove the file when we exit.  */
Packit 032894
  unlink (fname);
Packit 032894
Packit 032894
  elf_version (EV_CURRENT);
Packit 032894
  elf = elf_begin (fd, ELF_C_WRITE, NULL);
Packit 032894
  if (elf == NULL)
Packit 032894
    {
Packit 032894
      printf ("elf_begin: %s\n", elf_errmsg (-1));
Packit 032894
      result = 1;
Packit 032894
    }
Packit 032894
  else
Packit 032894
    {
Packit 032894
      if (elf32_newehdr (elf) == NULL)
Packit 032894
	{
Packit 032894
	  printf ("elf32_newehdr: %s\n", elf_errmsg (-1));
Packit 032894
	  result = 1;
Packit 032894
	}
Packit 032894
      else
Packit 032894
        {
Packit 032894
	  Elf32_Ehdr *ehdr = elf32_getehdr (elf);
Packit 032894
Packit 032894
	  if (ehdr == NULL)
Packit 032894
	    {
Packit 032894
	      printf ("elf32_getehdr: %s\n", elf_errmsg (-1));
Packit 032894
	      result = 1;
Packit 032894
	    }
Packit 032894
	  else
Packit 032894
	    {
Packit 032894
	      int i;
Packit 032894
Packit 032894
	      if (argc > 1)
Packit 032894
		/* Use argc as a debugging flag.  */
Packit 032894
		print_ehdr (ehdr);
Packit 032894
Packit 032894
	      /* Some tests.  */
Packit 032894
	      for (i = 0; i < EI_NIDENT; ++i)
Packit 032894
		if (ehdr->e_ident[i] != 0)
Packit 032894
		  {
Packit 032894
		    printf ("ehdr->e_ident[%d] != 0\n", i);
Packit 032894
		    result = 1;
Packit 032894
		    break;
Packit 032894
		  }
Packit 032894
Packit 032894
#define VALUE_TEST(name, val) \
Packit 032894
	      if (ehdr->name != val)					      \
Packit 032894
	        {							      \
Packit 032894
		  printf ("ehdr->%s != %d\n", #name, val);		      \
Packit 032894
		  result = 1;						      \
Packit 032894
		}
Packit 032894
#define ZERO_TEST(name) VALUE_TEST (name, 0)
Packit 032894
	      ZERO_TEST (e_type);
Packit 032894
	      ZERO_TEST (e_machine);
Packit 032894
	      ZERO_TEST (e_version);
Packit 032894
	      ZERO_TEST (e_entry);
Packit 032894
	      ZERO_TEST (e_phoff);
Packit 032894
	      ZERO_TEST (e_shoff);
Packit 032894
	      ZERO_TEST (e_flags);
Packit 032894
	      ZERO_TEST (e_ehsize);
Packit 032894
	      ZERO_TEST (e_phentsize);
Packit 032894
	      ZERO_TEST (e_phnum);
Packit 032894
	      ZERO_TEST (e_shentsize);
Packit 032894
	      ZERO_TEST (e_shnum);
Packit 032894
	      ZERO_TEST (e_shstrndx);
Packit 032894
Packit 032894
	      if (elf32_newphdr (elf, 10) == NULL)
Packit 032894
		{
Packit 032894
		  printf ("elf32_newphdr: %s\n", elf_errmsg (-1));
Packit 032894
		  result = 1;
Packit 032894
		}
Packit 032894
	      else
Packit 032894
		{
Packit 032894
		  if (argc > 1)
Packit 032894
		    print_ehdr (ehdr);
Packit 032894
Packit 032894
		  ehdr = elf32_getehdr (elf);
Packit 032894
		  if (ehdr == NULL)
Packit 032894
		    {
Packit 032894
		      printf ("elf32_getehdr (#2): %s\n", elf_errmsg (-1));
Packit 032894
		      result = 1;
Packit 032894
		    }
Packit 032894
		  else
Packit 032894
		    {
Packit 032894
		      ZERO_TEST (e_type);
Packit 032894
		      ZERO_TEST (e_machine);
Packit 032894
		      ZERO_TEST (e_version);
Packit 032894
		      ZERO_TEST (e_entry);
Packit 032894
		      ZERO_TEST (e_phoff);
Packit 032894
		      ZERO_TEST (e_shoff);
Packit 032894
		      ZERO_TEST (e_flags);
Packit 032894
		      ZERO_TEST (e_ehsize);
Packit 032894
		      VALUE_TEST (e_phentsize, (int) sizeof (Elf32_Phdr));
Packit 032894
		      VALUE_TEST (e_phnum, 10);
Packit 032894
		      ZERO_TEST (e_shentsize);
Packit 032894
		      ZERO_TEST (e_shnum);
Packit 032894
		      ZERO_TEST (e_shstrndx);
Packit 032894
		    }
Packit 032894
		}
Packit 032894
	    }
Packit 032894
        }
Packit 032894
Packit 032894
      (void) elf_end (elf);
Packit 032894
    }
Packit 032894
Packit 032894
  return result;
Packit 032894
}