Blame tests/low_high_pc.c

Packit 032894
/* Test program for dwarf_lowpc and dwarf_highpc
Packit 032894
   Copyright (C) 2012, 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(dwfl)
Packit 032894
#include <dwarf.h>
Packit 032894
#include <argp.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdio_ext.h>
Packit 032894
#include <locale.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include <fnmatch.h>
Packit 032894
Packit 032894
struct args
Packit 032894
{
Packit 032894
  Dwfl *dwfl;
Packit 032894
  Dwarf_Die *cu;
Packit 032894
  Dwarf_Addr dwbias;
Packit 032894
  char **argv;
Packit 032894
  const char *file;
Packit 032894
};
Packit 032894
Packit 032894
static struct args *args;
Packit 032894
Packit 032894
static void
Packit 032894
fail(Dwarf_Off off, const char *name, const char *msg)
Packit 032894
{
Packit 032894
  printf("%s: [%" PRIx64 "] '%s' %s\n", args->file, off, name, msg);
Packit 032894
  exit(-1);
Packit 032894
}
Packit 032894
Packit 032894
static int
Packit 032894
handle_die (Dwarf_Die *die, void *arg)
Packit 032894
{
Packit 032894
  args = arg;
Packit 032894
  Dwarf_Off off = dwarf_dieoffset (die);
Packit 032894
Packit 032894
  const char *name = dwarf_diename (die);
Packit 032894
  if (name == NULL)
Packit 032894
    name = "<no name>";
Packit 032894
Packit 032894
  printf ("[%" PRIx64 "] %s\n", off, name);
Packit 032894
Packit 032894
  Dwarf_Addr lowpc = 0;
Packit 032894
  Dwarf_Addr highpc = 0;
Packit 032894
  if (dwarf_lowpc (die, &lowpc) != 0 && dwarf_hasattr (die, DW_AT_low_pc))
Packit 032894
    fail (off, name, "has DW_AT_low_pc but dwarf_lowpc fails");
Packit 032894
  if (dwarf_highpc (die, &highpc) != 0 && dwarf_hasattr (die, DW_AT_high_pc))
Packit 032894
    fail (off, name, "has DW_AT_high_pc but dwarf_highpc fails");
Packit 032894
Packit 032894
  /* GCC < 4.7 had a bug where no code CUs got a highpc == lowpc.
Packit 032894
     Allow that, because it is not the main purpose of this test.  */
Packit 032894
  if (dwarf_hasattr (die, DW_AT_low_pc)
Packit 032894
      && dwarf_hasattr (die, DW_AT_high_pc)
Packit 032894
      && highpc <= lowpc
Packit 032894
      && ! (dwarf_tag (die) == DW_TAG_compile_unit && highpc == lowpc))
Packit 032894
    {
Packit 032894
      printf("lowpc: %" PRIx64 ", highpc: %" PRIx64 "lx\n", lowpc, highpc);
Packit 032894
      fail (off, name, "highpc <= lowpc");
Packit 032894
    }
Packit 032894
Packit 032894
  return 0;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  int remaining;
Packit 032894
Packit 032894
  /* Set locale.  */
Packit 032894
  (void) setlocale (LC_ALL, "");
Packit 032894
Packit 032894
  struct args a = { .dwfl = NULL, .cu = NULL };
Packit 032894
Packit 032894
  (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
Packit 032894
		     &a.dwfl);
Packit 032894
  assert (a.dwfl != NULL);
Packit 032894
  a.argv = &argv[remaining];
Packit 032894
Packit 032894
  int result = 0;
Packit 032894
Packit 032894
  while ((a.cu = dwfl_nextcu (a.dwfl, a.cu, &a.dwbias)) != NULL)
Packit 032894
    {
Packit 032894
      a.file = dwarf_diename (a.cu);
Packit 032894
      handle_die (a.cu, &a);
Packit 032894
      dwarf_getfuncs (a.cu, &handle_die, &a, 0);
Packit 032894
Packit 032894
      uint8_t unit_type;
Packit 032894
      Dwarf_Die subdie;
Packit 032894
      if (dwarf_cu_info (a.cu->cu, NULL, &unit_type, NULL, &subdie,
Packit 032894
			 NULL, NULL, NULL) != 0)
Packit 032894
	{
Packit 032894
	  Dwarf_Off off = dwarf_dieoffset (a.cu);
Packit 032894
	  fail (off, "dwarf_cu_info", dwarf_errmsg (-1));
Packit 032894
	}
Packit 032894
Packit 032894
      if (unit_type == DW_UT_skeleton)
Packit 032894
	{
Packit 032894
	  const char *name = dwarf_diename (&subdie) ?: "<unknown>";
Packit 032894
	  printf ("Following split subdie: %s\n", name);
Packit 032894
	  struct args b = a;
Packit 032894
	  b.cu = &subdi;;
Packit 032894
	  handle_die (b.cu, &b);
Packit 032894
	  dwarf_getfuncs (b.cu, &handle_die, &b, 0);
Packit 032894
	  printf ("Done subdie: %s\n", name);
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  dwfl_end (a.dwfl);
Packit 032894
Packit 032894
  printf ("\n");
Packit 032894
Packit 032894
  return result;
Packit 032894
}