Blame tests/scnnames.c

Packit 032894
/* Copyright (C) 1998, 1999, 2000, 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 <stdlib.h>
Packit 032894
#include <stdio.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
  size_t strndx;
Packit 032894
  Elf_Scn *scn;
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
  strndx = ehdr.e_shstrndx;
Packit 032894
Packit 032894
  scn = NULL;
Packit 032894
  while ((scn = elf_nextscn (elf, scn)) != NULL)
Packit 032894
    {
Packit 032894
      char *name = NULL;
Packit 032894
      GElf_Shdr shdr;
Packit 032894
Packit 032894
      if (gelf_getshdr (scn, &shdr) != NULL)
Packit 032894
	name = elf_strptr (elf, strndx, (size_t) shdr.sh_name);
Packit 032894
Packit 032894
      printf ("section: `%s'\n", name);
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
}