Blame tests/dwarf-die-addr-die.c

Packit 032894
/* Test program for dwarf_die_addr_die.
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 ELFUTILS_HEADER(dw)
Packit 032894
#include <dwarf.h>
Packit 032894
#include <sys/types.h>
Packit 032894
#include <sys/stat.h>
Packit 032894
#include <fcntl.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdint.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <assert.h>
Packit 032894
#include <inttypes.h>
Packit 032894
#include <string.h>
Packit 032894
#include <errno.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
/* The main Dwarf file.  */
Packit 032894
static Dwarf *dwarf;
Packit 032894
Packit 032894
int
Packit 032894
check_die (Dwarf_Die *die)
Packit 032894
{
Packit 032894
  if (dwarf_tag (die) == DW_TAG_invalid)
Packit 032894
    {
Packit 032894
      printf ("Invalid die\n");
Packit 032894
      return -1;
Packit 032894
    }
Packit 032894
Packit 032894
  int res = 0;
Packit 032894
  void *addr = die->addr;
Packit 032894
  Dwarf_Die die2;
Packit 032894
  if (dwarf_die_addr_die (dwarf, addr, &die2) == NULL)
Packit 032894
    {
Packit 032894
      printf ("Bad die addr die at offset %" PRIx64 "\n",
Packit 032894
	      dwarf_dieoffset (die));
Packit 032894
      res = -1;
Packit 032894
    }
Packit 032894
Packit 032894
  if (dwarf_tag (die) != dwarf_tag (&die2))
Packit 032894
    {
Packit 032894
      printf ("Tags differ for die at offset %" PRIx64 "\n",
Packit 032894
	      dwarf_dieoffset (die));
Packit 032894
      res = -1;
Packit 032894
    }
Packit 032894
Packit 032894
  if (dwarf_cuoffset (die) != dwarf_cuoffset (&die2))
Packit 032894
    {
Packit 032894
      printf ("CU offsets differ for die at offset %" PRIx64 "\n",
Packit 032894
	      dwarf_dieoffset (die));
Packit 032894
      res = -1;
Packit 032894
    }
Packit 032894
Packit 032894
  Dwarf_Die child;
Packit 032894
  if (dwarf_child (die, &child) == 0)
Packit 032894
    res |= check_die (&child);
Packit 032894
Packit 032894
  Dwarf_Die sibling;
Packit 032894
  if (dwarf_siblingof (die, &sibling) == 0)
Packit 032894
    res |= check_die (&sibling);
Packit 032894
Packit 032894
  return res;
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
check_dbg (Dwarf *dbg)
Packit 032894
{
Packit 032894
  int res = 0;
Packit 032894
  Dwarf_Off off = 0;
Packit 032894
  Dwarf_Off old_off = 0;
Packit 032894
  size_t hsize;
Packit 032894
  Dwarf_Off abbrev;
Packit 032894
  uint8_t addresssize;
Packit 032894
  uint8_t offsetsize;
Packit 032894
  while (dwarf_nextcu (dbg, off, &off, &hsize, &abbrev, &addresssize,
Packit 032894
                       &offsetsize) == 0)
Packit 032894
    {
Packit 032894
      Dwarf_Die die;
Packit 032894
      if (dwarf_offdie (dbg, old_off + hsize, &die) != NULL)
Packit 032894
	{
Packit 032894
	  printf ("checking CU at %" PRIx64 "\n", old_off);
Packit 032894
	  res |= check_die (&die);
Packit 032894
	}
Packit 032894
Packit 032894
      old_off = off;
Packit 032894
    }
Packit 032894
Packit 032894
  // Same for type...
Packit 032894
  Dwarf_Half version;
Packit 032894
  uint64_t typesig;
Packit 032894
  Dwarf_Off typeoff;
Packit 032894
  off = 0;
Packit 032894
  old_off = 0;
Packit 032894
  while (dwarf_next_unit (dbg, off, &off, &hsize, &version, &abbrev,
Packit 032894
			  &addresssize, &offsetsize, &typesig, &typeoff) == 0)
Packit 032894
    {
Packit 032894
      Dwarf_Die die;
Packit 032894
      if (dwarf_offdie_types (dbg, old_off + hsize, &die) != NULL)
Packit 032894
	{
Packit 032894
	  printf ("checking TU at %" PRIx64 "\n", old_off);
Packit 032894
	  res |= check_die (&die);
Packit 032894
	}
Packit 032894
Packit 032894
      // We should have seen this already, but double check...
Packit 032894
      if (dwarf_offdie_types (dbg, old_off + typeoff, &die) != NULL)
Packit 032894
	{
Packit 032894
	  printf ("checking Type DIE at %" PRIx64 "\n",
Packit 032894
		  old_off + hsize + typeoff);
Packit 032894
	  res |= check_die (&die);
Packit 032894
	}
Packit 032894
Packit 032894
      old_off = off;
Packit 032894
    }
Packit 032894
Packit 032894
  Dwarf *alt = dwarf_getalt (dbg);
Packit 032894
  if (alt != NULL)
Packit 032894
    {
Packit 032894
      printf ("checking alt debug\n");
Packit 032894
      res |= check_dbg (alt);
Packit 032894
    }
Packit 032894
Packit 032894
  // Split or Type Dwarf_Dies gotten through dwarf_get_units.
Packit 032894
  Dwarf_CU *cu = NULL;
Packit 032894
  Dwarf_Die subdie;
Packit 032894
  uint8_t unit_type;
Packit 032894
  while (dwarf_get_units (dbg, cu, &cu, NULL,
Packit 032894
                          &unit_type, NULL, &subdie) == 0)
Packit 032894
    {
Packit 032894
      if (dwarf_tag (&subdie) != DW_TAG_invalid)
Packit 032894
        {
Packit 032894
	  printf ("checking %" PRIx8 " subdie\n", unit_type);
Packit 032894
	  res |= check_die (&subdie);
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  return res;
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  if (argc < 2)
Packit 032894
    {
Packit 032894
      printf ("No file given.\n");
Packit 032894
      return -1;
Packit 032894
    }
Packit 032894
Packit 032894
  const char *name = argv[1];
Packit 032894
  int fd = open (name, O_RDONLY);
Packit 032894
  if (fd < 0)
Packit 032894
    {
Packit 032894
      printf ("Cannnot open '%s': %s\n", name, strerror (errno));
Packit 032894
      return -1;
Packit 032894
    }
Packit 032894
Packit 032894
  dwarf = dwarf_begin (fd, DWARF_C_READ);
Packit 032894
  if (dwarf == NULL)
Packit 032894
    {
Packit 032894
      printf ("Not a Dwarf file '%s': %s\n", name, dwarf_errmsg (-1));
Packit 032894
      close (fd);
Packit 032894
      return -1;
Packit 032894
    }
Packit 032894
Packit 032894
  printf ("checking %s\n", name);
Packit 032894
  int res = check_dbg (dwarf);
Packit 032894
Packit 032894
  dwarf_end (dwarf);
Packit 032894
  close (fd);
Packit 032894
Packit 032894
  return res;
Packit 032894
}