Blame tests/showptable.c

Packit 032894
/* Copyright (C) 1998, 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>, 1998.
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 <errno.h>
Packit 032894
#include <fcntl.h>
Packit 032894
#include <gelf.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  Elf *elf;
Packit 032894
  int fd;
Packit 032894
  GElf_Ehdr ehdr;
Packit 032894
  int cnt;
Packit 032894
Packit 032894
  if (argc < 2)
Packit 032894
    {
Packit 032894
      puts ("missing parameter");
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  fd = open (argv[1], O_RDONLY);
Packit 032894
  if (fd == -1)
Packit 032894
    {
Packit 032894
      printf ("cannot open \"%s\": %s\n", argv[1], strerror (errno));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  elf_version (EV_CURRENT);
Packit 032894
Packit 032894
  elf = elf_begin (fd, ELF_C_READ, NULL);
Packit 032894
  if (elf == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot open ELF file: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  if (elf_kind (elf) != ELF_K_ELF)
Packit 032894
    {
Packit 032894
      printf ("\"%s\" is not an ELF file\n", argv[1]);
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  if (gelf_getehdr (elf, &ehdr) == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot get the ELF header: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  printf ("idx type    %*s %*s %*s %*s %*s  align flags\n",
Packit 032894
	  gelf_getclass (elf) == ELFCLASS32 ? 9 : 17, "offset",
Packit 032894
	  gelf_getclass (elf) == ELFCLASS32 ? 10 : 18, "vaddr",
Packit 032894
	  gelf_getclass (elf) == ELFCLASS32 ? 10 : 18, "paddr",
Packit 032894
	  gelf_getclass (elf) == ELFCLASS32 ? 9 : 12, "filesz",
Packit 032894
	  gelf_getclass (elf) == ELFCLASS32 ? 9 : 12, "memsz");
Packit 032894
Packit 032894
  for (cnt = 0; cnt < ehdr.e_phnum; ++cnt)
Packit 032894
    {
Packit 032894
      static const char *typenames[] =
Packit 032894
      {
Packit 032894
	[PT_NULL] = "NULL",
Packit 032894
	[PT_LOAD] = "LOAD",
Packit 032894
	[PT_DYNAMIC] = "DYNAMIC",
Packit 032894
	[PT_INTERP] = "INTERP",
Packit 032894
	[PT_NOTE] = "NOTE",
Packit 032894
	[PT_SHLIB] = "SHLIB",
Packit 032894
	[PT_PHDR] = "PHDR"
Packit 032894
      };
Packit 032894
      GElf_Phdr mem;
Packit 032894
      GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &mem;;
Packit 032894
      char buf[19];
Packit 032894
      const char *p_type = typenames[phdr->p_type];
Packit 032894
Packit 032894
      /* If we don't know the name of the type we use the number value.  */
Packit 032894
      if (phdr->p_type >= PT_NUM)
Packit 032894
	{
Packit 032894
	  snprintf (buf, sizeof (buf), "%x", phdr->p_type);
Packit 032894
	  p_type = buf;
Packit 032894
	}
Packit 032894
Packit 032894
      printf ("%3d %-7s %#0*llx %#0*llx %#0*llx %#0*llx %#0*llx %#6llx ",
Packit 032894
	      cnt, p_type,
Packit 032894
	      gelf_getclass (elf) == ELFCLASS32 ? 9 : 17,
Packit 032894
	      (unsigned long long int) phdr->p_offset,
Packit 032894
	      gelf_getclass (elf) == ELFCLASS32 ? 10 : 18,
Packit 032894
	      (unsigned long long int) phdr->p_vaddr,
Packit 032894
	      gelf_getclass (elf) == ELFCLASS32 ? 10 : 18,
Packit 032894
	      (unsigned long long int) phdr->p_paddr,
Packit 032894
	      gelf_getclass (elf) == ELFCLASS32 ? 9 : 12,
Packit 032894
	      (unsigned long long int) phdr->p_filesz,
Packit 032894
	      gelf_getclass (elf) == ELFCLASS32 ? 9 : 12,
Packit 032894
	      (unsigned long long int) phdr->p_memsz,
Packit 032894
	      (unsigned long long int) phdr->p_align);
Packit 032894
Packit 032894
      putc_unlocked ((phdr->p_flags & PF_X) ? 'X' : ' ', stdout);
Packit 032894
      putc_unlocked ((phdr->p_flags & PF_W) ? 'W' : ' ', stdout);
Packit 032894
      putc_unlocked ((phdr->p_flags & PF_R) ? 'R' : ' ', stdout);
Packit 032894
Packit 032894
      putc_unlocked ('\n', stdout);
Packit 032894
Packit 032894
      if (phdr->p_type == PT_INTERP)
Packit 032894
	{
Packit 032894
	  /* We can show the user the name of the interpreter.  */
Packit 032894
	  size_t maxsize;
Packit 032894
	  char *filedata = elf_rawfile (elf, &maxsize);
Packit 032894
Packit 032894
	  if (filedata != NULL && phdr->p_offset < maxsize)
Packit 032894
	    printf ("\t[Requesting program interpreter: %s]\n",
Packit 032894
		    filedata + phdr->p_offset);
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  if (elf_end (elf) != 0)
Packit 032894
    {
Packit 032894
      printf ("error while freeing ELF descriptor: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  return 0;
Packit 032894
}