Blame tests/scnnames.c

Packit Service 97d2fb
/* Copyright (C) 1998, 1999, 2000, 2002, 2005 Red Hat, Inc.
Packit Service 97d2fb
   This file is part of elfutils.
Packit Service 97d2fb
   Written by Ulrich Drepper <drepper@redhat.com>, 1998.
Packit Service 97d2fb
Packit Service 97d2fb
   This file is free software; you can redistribute it and/or modify
Packit Service 97d2fb
   it under the terms of the GNU General Public License as published by
Packit Service 97d2fb
   the Free Software Foundation; either version 3 of the License, or
Packit Service 97d2fb
   (at your option) any later version.
Packit Service 97d2fb
Packit Service 97d2fb
   elfutils is distributed in the hope that it will be useful, but
Packit Service 97d2fb
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 97d2fb
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 97d2fb
   GNU General Public License for more details.
Packit Service 97d2fb
Packit Service 97d2fb
   You should have received a copy of the GNU General Public License
Packit Service 97d2fb
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service 97d2fb
Packit Service 97d2fb
#include <config.h>
Packit Service 97d2fb
Packit Service 97d2fb
#include <errno.h>
Packit Service 97d2fb
#include <fcntl.h>
Packit Service 97d2fb
#include <gelf.h>
Packit Service 97d2fb
#include <stdlib.h>
Packit Service 97d2fb
#include <stdio.h>
Packit Service 97d2fb
#include <string.h>
Packit Service 97d2fb
Packit Service 97d2fb
int
Packit Service 97d2fb
main (int argc, char *argv[])
Packit Service 97d2fb
{
Packit Service 97d2fb
  Elf *elf;
Packit Service 97d2fb
  int fd;
Packit Service 97d2fb
  GElf_Ehdr ehdr;
Packit Service 97d2fb
  size_t strndx;
Packit Service 97d2fb
  Elf_Scn *scn;
Packit Service 97d2fb
Packit Service 97d2fb
  if (argc < 2)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      puts ("missing parameter");
Packit Service 97d2fb
      exit (1);
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  fd = open (argv[1], O_RDONLY);
Packit Service 97d2fb
  if (fd == -1)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      printf ("cannot open \"%s\": %s\n", argv[1], strerror (errno));
Packit Service 97d2fb
      exit (1);
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  elf_version (EV_CURRENT);
Packit Service 97d2fb
Packit Service 97d2fb
  elf = elf_begin (fd, ELF_C_READ, NULL);
Packit Service 97d2fb
  if (elf == NULL)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      printf ("cannot open ELF file: %s\n", elf_errmsg (-1));
Packit Service 97d2fb
      exit (1);
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  if (elf_kind (elf) != ELF_K_ELF)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      printf ("\"%s\" is not an ELF file\n", argv[1]);
Packit Service 97d2fb
      exit (1);
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  if (gelf_getehdr (elf, &ehdr) == NULL)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      printf ("cannot get the ELF header: %s\n", elf_errmsg (-1));
Packit Service 97d2fb
      exit (1);
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  strndx = ehdr.e_shstrndx;
Packit Service 97d2fb
Packit Service 97d2fb
  scn = NULL;
Packit Service 97d2fb
  while ((scn = elf_nextscn (elf, scn)) != NULL)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      char *name = NULL;
Packit Service 97d2fb
      GElf_Shdr shdr;
Packit Service 97d2fb
Packit Service 97d2fb
      if (gelf_getshdr (scn, &shdr) != NULL)
Packit Service 97d2fb
	name = elf_strptr (elf, strndx, (size_t) shdr.sh_name);
Packit Service 97d2fb
Packit Service 97d2fb
      printf ("section: `%s'\n", name);
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  if (elf_end (elf) != 0)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      printf ("error while freeing ELF descriptor: %s\n", elf_errmsg (-1));
Packit Service 97d2fb
      exit (1);
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  return 0;
Packit Service 97d2fb
}