Blame tests/next_cfi.c

Packit 032894
/* Test program for dwarf_next_cfi
Packit 032894
   Copyright (C) 2018 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
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
#include <assert.h>
Packit 032894
#include <inttypes.h>
Packit 032894
#include ELFUTILS_HEADER(dw)
Packit 032894
#include <dwarf.h>
Packit 032894
#include <argp.h>
Packit 032894
#include <stdbool.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <sys/types.h>
Packit 032894
#include <sys/stat.h>
Packit 032894
#include <fcntl.h>
Packit 032894
#include <locale.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include <unistd.h>
Packit 032894
#include "system.h"
Packit 032894
Packit 032894
void
Packit 032894
handle_section (char *name, const unsigned char e_ident[],
Packit 032894
		Elf_Scn *scn, const bool is_eh)
Packit 032894
{
Packit 032894
  if (is_eh)
Packit 032894
    printf (".eh_frame\n");
Packit 032894
  else
Packit 032894
    printf (".debug_frame\n");
Packit 032894
Packit 032894
  GElf_Shdr mem;
Packit 032894
  GElf_Shdr *shdr = gelf_getshdr (scn, &mem;;
Packit 032894
  if (shdr == NULL)
Packit 032894
    error (EXIT_FAILURE, 0, "Couldn't get section header: %s",
Packit 032894
	   elf_errmsg (-1));
Packit 032894
  if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
Packit 032894
    {
Packit 032894
      if (elf_compress (scn, 0, 0) < 0)
Packit 032894
	error (EXIT_FAILURE, 0, "Couldn't decompress section: %s",
Packit 032894
	       elf_errmsg (-1));
Packit 032894
    }
Packit 032894
  else if (name[0] == '.' && name[1] == 'z')
Packit 032894
    {
Packit 032894
      if (elf_compress_gnu (scn, 0, 0) < 0)
Packit 032894
	error (EXIT_FAILURE, 0, "Couldn't decompress section: %s",
Packit 032894
	       elf_errmsg (-1));
Packit 032894
    }
Packit 032894
Packit 032894
  Elf_Data *data = elf_getdata (scn, NULL);
Packit 032894
  if (data == NULL || data->d_buf == NULL)
Packit 032894
    error (EXIT_FAILURE, 0, "no section data");
Packit 032894
Packit 032894
  int res;
Packit 032894
  Dwarf_Off off;
Packit 032894
  Dwarf_Off next_off = 0;
Packit 032894
  Dwarf_CFI_Entry entry;
Packit 032894
  while ((res = dwarf_next_cfi (e_ident, data, is_eh, off = next_off,
Packit 032894
				&next_off, &entry)) == 0)
Packit 032894
    {
Packit 032894
      printf ("[%" PRId64 "] ", off);
Packit 032894
      if (dwarf_cfi_cie_p (&entry))
Packit 032894
	printf ("CIE augmentation=\"%s\"\n", entry.cie.augmentation);
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  printf ("FDE cie=[%" PRId64 "]\n", entry.fde.CIE_pointer);
Packit 032894
Packit 032894
	  Dwarf_Off cie_off = entry.fde.CIE_pointer;
Packit 032894
	  Dwarf_Off cie_off_next;
Packit 032894
	  Dwarf_CFI_Entry cie_entry;
Packit 032894
	  if (dwarf_next_cfi (e_ident, data, is_eh, cie_off, &cie_off_next,
Packit 032894
			      &cie_entry) != 0
Packit 032894
	      || !dwarf_cfi_cie_p (&cie_entry))
Packit 032894
	    error (EXIT_FAILURE, 0, "FDE doesn't point to CIE");
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  if (res < 0)
Packit 032894
    error (EXIT_FAILURE, 0, "dwarf_next_cfi failed: %s\n",
Packit 032894
	   dwarf_errmsg (-1));
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  if (argc != 2)
Packit 032894
    error (EXIT_FAILURE, 0, "need file name argument");
Packit 032894
Packit 032894
  const char *file = argv[1];
Packit 032894
  printf ("%s\n", file);
Packit 032894
Packit 032894
  int fd = open (file, O_RDONLY);
Packit 032894
  if (fd == -1)
Packit 032894
    error (EXIT_FAILURE, errno, "cannot open input file `%s'", file);
Packit 032894
Packit 032894
  elf_version (EV_CURRENT);
Packit 032894
Packit 032894
  Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
Packit 032894
  if (elf == NULL)
Packit 032894
    error (EXIT_FAILURE, 0, "cannot create ELF descriptor: %s",
Packit 032894
	   elf_errmsg (-1));
Packit 032894
Packit 032894
  size_t esize;
Packit 032894
  const unsigned char *ident = (const unsigned char *) elf_getident (elf,
Packit 032894
								     &esize);
Packit 032894
  if (ident == NULL || esize < EI_NIDENT)
Packit 032894
    error (EXIT_FAILURE, 0, "no, or too small, ELF ident");
Packit 032894
Packit 032894
  GElf_Ehdr ehdr;
Packit 032894
  if (gelf_getehdr (elf, &ehdr) == NULL)
Packit 032894
    error (EXIT_FAILURE, 0, "cannot get the ELF header: %s\n",
Packit 032894
	   elf_errmsg (-1));
Packit 032894
Packit 032894
  size_t strndx = ehdr.e_shstrndx;
Packit 032894
Packit 032894
  Elf_Scn *scn = NULL;
Packit 032894
  while ((scn = elf_nextscn (elf, scn)) != NULL)
Packit 032894
    {
Packit 032894
      GElf_Shdr shdr;
Packit 032894
      if (gelf_getshdr (scn, &shdr) != NULL)
Packit 032894
	{
Packit 032894
	  char *name = elf_strptr (elf, strndx, (size_t) shdr.sh_name);
Packit 032894
	  if (name != NULL && shdr.sh_type == SHT_PROGBITS)
Packit 032894
	    {
Packit 032894
	      if (strcmp (name, ".eh_frame") == 0)
Packit 032894
		handle_section (name, ident, scn, true);
Packit 032894
	      if (strcmp (name, ".debug_frame") == 0
Packit 032894
		  || strcmp (name, ".zdebug_frame") == 0)
Packit 032894
		handle_section (name, ident, scn, false);
Packit 032894
	    }
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  elf_end (elf);
Packit 032894
  close (fd);
Packit 032894
Packit 032894
  return 0;
Packit 032894
}