Blame tests/saridx.c

Packit 032894
/* Copyright (C) 1998, 1999, 2000, 2002 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 <fcntl.h>
Packit 032894
#include <gelf.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include <time.h>
Packit 032894
#include <unistd.h>
Packit 032894
#include "system.h"
Packit 032894
Packit 032894
Packit 032894
static const char *machines[] =
Packit 032894
{
Packit 032894
#define MACHINE(name) [name] = #name
Packit 032894
  MACHINE (EM_NONE),
Packit 032894
  MACHINE (EM_M32),
Packit 032894
  MACHINE (EM_SPARC),
Packit 032894
  MACHINE (EM_386),
Packit 032894
  MACHINE (EM_68K),
Packit 032894
  MACHINE (EM_88K),
Packit 032894
  MACHINE (EM_860),
Packit 032894
  MACHINE (EM_MIPS),
Packit 032894
  MACHINE (EM_MIPS_RS3_LE),
Packit 032894
  MACHINE (EM_PARISC),
Packit 032894
  MACHINE (EM_VPP500),
Packit 032894
  MACHINE (EM_SPARC32PLUS),
Packit 032894
  MACHINE (EM_960),
Packit 032894
  MACHINE (EM_PPC),
Packit 032894
  MACHINE (EM_PPC64),
Packit 032894
  MACHINE (EM_V800),
Packit 032894
  MACHINE (EM_FR20),
Packit 032894
  MACHINE (EM_RH32),
Packit 032894
  MACHINE (EM_RCE),
Packit 032894
  MACHINE (EM_ARM),
Packit 032894
  MACHINE (EM_FAKE_ALPHA),
Packit 032894
  MACHINE (EM_SH),
Packit 032894
  MACHINE (EM_SPARCV9),
Packit 032894
  MACHINE (EM_TRICORE),
Packit 032894
  MACHINE (EM_ARC),
Packit 032894
  MACHINE (EM_H8_300),
Packit 032894
  MACHINE (EM_H8_300H),
Packit 032894
  MACHINE (EM_H8S),
Packit 032894
  MACHINE (EM_H8_500),
Packit 032894
  MACHINE (EM_IA_64),
Packit 032894
  MACHINE (EM_MIPS_X),
Packit 032894
  MACHINE (EM_COLDFIRE),
Packit 032894
  MACHINE (EM_68HC12),
Packit 032894
  MACHINE (EM_MMA),
Packit 032894
  MACHINE (EM_PCP),
Packit 032894
  MACHINE (EM_NCPU),
Packit 032894
  MACHINE (EM_NDR1),
Packit 032894
  MACHINE (EM_STARCORE),
Packit 032894
  MACHINE (EM_ME16),
Packit 032894
  MACHINE (EM_ST100),
Packit 032894
  MACHINE (EM_TINYJ),
Packit 032894
  MACHINE (EM_FX66),
Packit 032894
  MACHINE (EM_ST9PLUS),
Packit 032894
  MACHINE (EM_ST7),
Packit 032894
  MACHINE (EM_68HC16),
Packit 032894
  MACHINE (EM_68HC11),
Packit 032894
  MACHINE (EM_68HC08),
Packit 032894
  MACHINE (EM_68HC05),
Packit 032894
  MACHINE (EM_SVX),
Packit 032894
  MACHINE (EM_ST19),
Packit 032894
  MACHINE (EM_VAX)
Packit 032894
};
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  int fd;
Packit 032894
  Elf *elf;
Packit 032894
  Elf_Cmd cmd;
Packit 032894
  size_t n;
Packit 032894
  int arg = 1;
Packit 032894
  int verbose = 0;
Packit 032894
Packit 032894
  /* Recognize optional verbosity flag.  */
Packit 032894
  if (arg < argc && strcmp (argv[arg], "-v") == 0)
Packit 032894
    {
Packit 032894
      verbose = 1;
Packit 032894
      ++arg;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Any more arguments available.  */
Packit 032894
  if (arg >= argc)
Packit 032894
    error (EXIT_FAILURE, 0, "No input file given");
Packit 032894
Packit 032894
  /* Open the input file.  */
Packit 032894
  fd = open (argv[arg], O_RDONLY);
Packit 032894
  if (fd == -1)
Packit 032894
    {
Packit 032894
      perror ("cannot open input file");
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Set the ELF version we are using here.  */
Packit 032894
  if (elf_version (EV_CURRENT) == EV_NONE)
Packit 032894
    {
Packit 032894
      puts ("ELF library too old");
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Start reading the file.  */
Packit 032894
  cmd = ELF_C_READ;
Packit 032894
  elf = elf_begin (fd, cmd, NULL);
Packit 032894
  if (elf == NULL)
Packit 032894
    {
Packit 032894
      printf ("elf_begin: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* If it is no archive punt.  */
Packit 032894
  if (elf_kind (elf) != ELF_K_AR)
Packit 032894
    {
Packit 032894
      printf ("%s is not an archive\n", argv[1]);
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  if (verbose)
Packit 032894
    {
Packit 032894
      /* The verbose variant.  We print a lot of information.  */
Packit 032894
      Elf *subelf;
Packit 032894
      char buf[100];
Packit 032894
      time_t t;
Packit 032894
Packit 032894
      /* Get the elements of the archive one after the other.  */
Packit 032894
      while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
Packit 032894
	{
Packit 032894
	  /* The the header for this element.  */
Packit 032894
	  Elf_Arhdr *arhdr = elf_getarhdr (subelf);
Packit 032894
Packit 032894
	  if (arhdr == NULL)
Packit 032894
	    {
Packit 032894
	      printf ("cannot get arhdr: %s\n", elf_errmsg (-1));
Packit 032894
	      break;
Packit 032894
	    }
Packit 032894
Packit 032894
	  switch (elf_kind (subelf))
Packit 032894
	    {
Packit 032894
	    case ELF_K_ELF:
Packit 032894
	      fputs ("ELF file:\n", stdout);
Packit 032894
	      break;
Packit 032894
Packit 032894
	    case ELF_K_AR:
Packit 032894
	      fputs ("archive:\n", stdout);
Packit 032894
	      break;
Packit 032894
Packit 032894
	    default:
Packit 032894
	      fputs ("unknown file:\n", stdout);
Packit 032894
	      break;
Packit 032894
	    }
Packit 032894
Packit 032894
	  /* Print general information.  */
Packit 032894
	  t = arhdr->ar_date;
Packit 032894
	  strftime (buf, sizeof buf, "%Y-%m-%dT%H:%M:%S%z", gmtime (&t);;
Packit 032894
	  printf ("  name         : \"%s\"\n"
Packit 032894
		  "  time         : %s\n"
Packit 032894
		  "  uid          : %ld\n"
Packit 032894
		  "  gid          : %ld\n"
Packit 032894
		  "  mode         : %o\n"
Packit 032894
		  "  size         : %ld\n"
Packit 032894
		  "  rawname      : \"%s\"\n",
Packit 032894
		  arhdr->ar_name,
Packit 032894
		  buf,
Packit 032894
		  (long int) arhdr->ar_uid,
Packit 032894
		  (long int) arhdr->ar_gid,
Packit 032894
		  arhdr->ar_mode,
Packit 032894
		  (long int) arhdr->ar_size,
Packit 032894
		  arhdr->ar_rawname);
Packit 032894
Packit 032894
	  /* For ELF files we can provide some more information.  */
Packit 032894
	  if (elf_kind (subelf) == ELF_K_ELF)
Packit 032894
	    {
Packit 032894
	      GElf_Ehdr ehdr;
Packit 032894
Packit 032894
	      /* Get the ELF header.  */
Packit 032894
	      if (gelf_getehdr (subelf, &ehdr) == NULL)
Packit 032894
		printf ("  *** cannot get ELF header: %s\n", elf_errmsg (-1));
Packit 032894
	      else
Packit 032894
		{
Packit 032894
		  printf ("  binary class : %s\n",
Packit 032894
			  ehdr.e_ident[EI_CLASS] == ELFCLASS32
Packit 032894
			  ? "ELFCLASS32" : "ELFCLASS64");
Packit 032894
		  printf ("  data encoding: %s\n",
Packit 032894
			  ehdr.e_ident[EI_DATA] == ELFDATA2LSB
Packit 032894
			  ? "ELFDATA2LSB" : "ELFDATA2MSB");
Packit 032894
		  printf ("  binary type  : %s\n",
Packit 032894
			  ehdr.e_type == ET_REL
Packit 032894
			  ? "relocatable"
Packit 032894
			  : (ehdr.e_type == ET_EXEC
Packit 032894
			     ? "executable"
Packit 032894
			     : (ehdr.e_type == ET_DYN
Packit 032894
				? "dynamic"
Packit 032894
				: "core file")));
Packit 032894
		  printf ("  machine      : %s\n",
Packit 032894
			  (ehdr.e_machine >= (sizeof (machines)
Packit 032894
					      / sizeof (machines[0]))
Packit 032894
			   || machines[ehdr.e_machine] == NULL)
Packit 032894
			  ? "???"
Packit 032894
			  : machines[ehdr.e_machine]);
Packit 032894
		}
Packit 032894
	    }
Packit 032894
Packit 032894
	  /* Get next archive element.  */
Packit 032894
	  cmd = elf_next (subelf);
Packit 032894
	  if (elf_end (subelf) != 0)
Packit 032894
	    printf ("error while freeing sub-ELF descriptor: %s\n",
Packit 032894
		    elf_errmsg (-1));
Packit 032894
	}
Packit 032894
    }
Packit 032894
  else
Packit 032894
    {
Packit 032894
      /* The simple version.  Only print a bit of information.  */
Packit 032894
      Elf_Arsym *arsym = elf_getarsym (elf, &n);
Packit 032894
Packit 032894
      if (n == 0)
Packit 032894
	printf ("no symbol table in archive: %s\n", elf_errmsg (-1));
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  --n;
Packit 032894
Packit 032894
	  while (n-- > 0)
Packit 032894
	    printf ("name = \"%s\", offset = %ld, hash = %lx\n",
Packit 032894
		    arsym[n].as_name, (long int) arsym[n].as_off,
Packit 032894
		    arsym[n].as_hash);
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  /* Free the ELF handle.  */
Packit 032894
  if (elf_end (elf) != 0)
Packit 032894
    printf ("error while freeing ELF descriptor: %s\n", elf_errmsg (-1));
Packit 032894
Packit 032894
  /* Close the underlying file.  */
Packit 032894
  close (fd);
Packit 032894
Packit 032894
  return 0;
Packit 032894
}