Blame tests/addrcfi.c

Packit Service 97d2fb
/* Test program for CFI handling.
Packit Service 97d2fb
   Copyright (C) 2009-2010, 2013, 2015 Red Hat, Inc.
Packit Service 97d2fb
   This file is part of elfutils.
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
#include <assert.h>
Packit Service 97d2fb
#include <inttypes.h>
Packit Service 97d2fb
#include ELFUTILS_HEADER(dwfl)
Packit Service 97d2fb
#include <dwarf.h>
Packit Service 97d2fb
#include <argp.h>
Packit Service 97d2fb
#include <stdio.h>
Packit Service 97d2fb
#include <stdio_ext.h>
Packit Service 97d2fb
#include <locale.h>
Packit Service 97d2fb
#include <stdlib.h>
Packit Service 97d2fb
#include <string.h>
Packit Service 97d2fb
Packit Service 97d2fb
#include "../libdw/known-dwarf.h"
Packit Service 97d2fb
Packit Service 97d2fb
static const char *
Packit Service 97d2fb
op_name (unsigned int code)
Packit Service 97d2fb
{
Packit Service 97d2fb
  static const char *const known[] =
Packit Service 97d2fb
    {
Packit Service 97d2fb
#define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
Packit Service 97d2fb
      DWARF_ALL_KNOWN_DW_OP
Packit Service 97d2fb
#undef DWARF_ONE_KNOWN_DW_OP
Packit Service 97d2fb
    };
Packit Service 97d2fb
Packit Service 97d2fb
  if (likely (code < sizeof (known) / sizeof (known[0])))
Packit Service 97d2fb
    return known[code];
Packit Service 97d2fb
Packit Service 97d2fb
  return NULL;
Packit Service 97d2fb
}
Packit Service 97d2fb
Packit Service 97d2fb
static void
Packit Service 97d2fb
print_detail (int result, const Dwarf_Op *ops, size_t nops, Dwarf_Addr bias)
Packit Service 97d2fb
{
Packit Service 97d2fb
  if (result < 0)
Packit Service 97d2fb
    printf ("indeterminate (%s)\n", dwarf_errmsg (-1));
Packit Service 97d2fb
  else if (nops == 0)
Packit Service 97d2fb
    printf ("%s\n", ops == NULL ? "same_value" : "undefined");
Packit Service 97d2fb
  else
Packit Service 97d2fb
    {
Packit Service 97d2fb
      printf ("%s expression:", result == 0 ? "location" : "value");
Packit Service 97d2fb
      for (size_t i = 0; i < nops; ++i)
Packit Service 97d2fb
	{
Packit Service 97d2fb
	  printf (" %s", op_name(ops[i].atom));
Packit Service 97d2fb
	  if (ops[i].number2 == 0)
Packit Service 97d2fb
	    {
Packit Service 97d2fb
	      if (ops[i].atom == DW_OP_addr)
Packit Service 97d2fb
		printf ("(%#" PRIx64 ")", ops[i].number + bias);
Packit Service 97d2fb
	      else if (ops[i].number != 0)
Packit Service 97d2fb
		printf ("(%" PRId64 ")", ops[i].number);
Packit Service 97d2fb
	    }
Packit Service 97d2fb
	  else
Packit Service 97d2fb
	    printf ("(%" PRId64 ",%" PRId64 ")",
Packit Service 97d2fb
		    ops[i].number, ops[i].number2);
Packit Service 97d2fb
	}
Packit Service 97d2fb
      puts ("");
Packit Service 97d2fb
    }
Packit Service 97d2fb
}
Packit Service 97d2fb
Packit Service 97d2fb
struct stuff
Packit Service 97d2fb
{
Packit Service 97d2fb
  Dwarf_Frame *frame;
Packit Service 97d2fb
  Dwarf_Addr bias;
Packit Service 97d2fb
};
Packit Service 97d2fb
Packit Service 97d2fb
static int
Packit Service 97d2fb
print_register (void *arg,
Packit Service 97d2fb
		int regno,
Packit Service 97d2fb
		const char *setname,
Packit Service 97d2fb
		const char *prefix,
Packit Service 97d2fb
		const char *regname,
Packit Service 97d2fb
		int bits __attribute__ ((unused)),
Packit Service 97d2fb
		int type __attribute__ ((unused)))
Packit Service 97d2fb
{
Packit Service 97d2fb
  struct stuff *stuff = arg;
Packit Service 97d2fb
Packit Service 97d2fb
  printf ("\t%s reg%u (%s%s): ", setname, regno, prefix, regname);
Packit Service 97d2fb
Packit Service 97d2fb
  Dwarf_Op ops_mem[3];
Packit Service 97d2fb
  Dwarf_Op *ops;
Packit Service 97d2fb
  size_t nops;
Packit Service 97d2fb
  int result = dwarf_frame_register (stuff->frame, regno, ops_mem, &ops, &nops;;
Packit Service 97d2fb
  print_detail (result, ops, nops, stuff->bias);
Packit Service 97d2fb
Packit Service 97d2fb
  return DWARF_CB_OK;
Packit Service 97d2fb
}
Packit Service 97d2fb
Packit Service 97d2fb
static int
Packit Service 97d2fb
handle_cfi (Dwfl *dwfl, const char *which, Dwarf_CFI *cfi,
Packit Service 97d2fb
	    GElf_Addr pc, struct stuff *stuff)
Packit Service 97d2fb
{
Packit Service 97d2fb
  if (cfi == NULL)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      printf ("handle_cfi no CFI (%s): %s\n", which, dwarf_errmsg (-1));
Packit Service 97d2fb
      return -1;
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  int result = dwarf_cfi_addrframe (cfi, pc - stuff->bias, &stuff->frame);
Packit Service 97d2fb
  if (result != 0)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      printf ("dwarf_cfi_addrframe (%s): %s\n", which, dwarf_errmsg (-1));
Packit Service 97d2fb
      return 1;
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  Dwarf_Addr start = pc;
Packit Service 97d2fb
  Dwarf_Addr end = pc;
Packit Service 97d2fb
  bool signalp;
Packit Service 97d2fb
  int ra_regno = dwarf_frame_info (stuff->frame, &start, &end, &signalp);
Packit Service 97d2fb
  if (ra_regno >= 0)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      start += stuff->bias;
Packit Service 97d2fb
      end += stuff->bias;
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  printf ("%s has %#" PRIx64 " => [%#" PRIx64 ", %#" PRIx64 "):\n",
Packit Service 97d2fb
	  which, pc, start, end);
Packit Service 97d2fb
Packit Service 97d2fb
  if (ra_regno < 0)
Packit Service 97d2fb
    printf ("\treturn address register unavailable (%s)\n",
Packit Service 97d2fb
	    dwarf_errmsg (0));
Packit Service 97d2fb
  else
Packit Service 97d2fb
    printf ("\treturn address in reg%u%s\n",
Packit Service 97d2fb
	    ra_regno, signalp ? " (signal frame)" : "");
Packit Service 97d2fb
Packit Service 97d2fb
  // Point cfa_ops to dummy to match print_detail expectations.
Packit Service 97d2fb
  // (nops == 0 && cfa_ops != NULL => "undefined")
Packit Service 97d2fb
  Dwarf_Op dummy;
Packit Service 97d2fb
  Dwarf_Op *cfa_ops = &dummy;
Packit Service 97d2fb
  size_t cfa_nops;
Packit Service 97d2fb
  result = dwarf_frame_cfa (stuff->frame, &cfa_ops, &cfa_nops);
Packit Service 97d2fb
Packit Service 97d2fb
  printf ("\tCFA ");
Packit Service 97d2fb
  print_detail (result, cfa_ops, cfa_nops, stuff->bias);
Packit Service 97d2fb
Packit Service 97d2fb
  (void) dwfl_module_register_names (dwfl_addrmodule (dwfl, pc),
Packit Service 97d2fb
				     &print_register, stuff);
Packit Service 97d2fb
Packit Service 97d2fb
  return 0;
Packit Service 97d2fb
}
Packit Service 97d2fb
Packit Service 97d2fb
static int
Packit Service 97d2fb
handle_address (GElf_Addr pc, Dwfl *dwfl)
Packit Service 97d2fb
{
Packit Service 97d2fb
  Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc);
Packit Service 97d2fb
Packit Service 97d2fb
  struct stuff stuff;
Packit Service 97d2fb
  stuff.frame = NULL;
Packit Service 97d2fb
  stuff.bias = 0;
Packit Service 97d2fb
  int res = handle_cfi (dwfl, ".eh_frame",
Packit Service 97d2fb
			dwfl_module_eh_cfi (mod, &stuff.bias), pc, &stuff);
Packit Service 97d2fb
  free (stuff.frame);
Packit Service 97d2fb
Packit Service 97d2fb
  stuff.frame = NULL;
Packit Service 97d2fb
  stuff.bias = 0;
Packit Service 97d2fb
  res &= handle_cfi (dwfl, ".debug_frame",
Packit Service 97d2fb
		     dwfl_module_dwarf_cfi (mod, &stuff.bias), pc, &stuff);
Packit Service 97d2fb
  free (stuff.frame);
Packit Service 97d2fb
Packit Service 97d2fb
  return res;
Packit Service 97d2fb
}
Packit Service 97d2fb
Packit Service 97d2fb
int
Packit Service 97d2fb
main (int argc, char *argv[])
Packit Service 97d2fb
{
Packit Service 97d2fb
  int remaining;
Packit Service 97d2fb
Packit Service 97d2fb
  /* Set locale.  */
Packit Service 97d2fb
  (void) setlocale (LC_ALL, "");
Packit Service 97d2fb
Packit Service 97d2fb
  Dwfl *dwfl = NULL;
Packit Service 97d2fb
  (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining, &dwfl);
Packit Service 97d2fb
  assert (dwfl != NULL);
Packit Service 97d2fb
Packit Service 97d2fb
  int result = 0;
Packit Service 97d2fb
Packit Service 97d2fb
  /* Now handle the addresses.  In case none are given on the command
Packit Service 97d2fb
     line, read from stdin.  */
Packit Service 97d2fb
  if (remaining == argc)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      /* We use no threads here which can interfere with handling a stream.  */
Packit Service 97d2fb
      (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
Packit Service 97d2fb
Packit Service 97d2fb
      char *buf = NULL;
Packit Service 97d2fb
      size_t len = 0;
Packit Service 97d2fb
      while (!feof_unlocked (stdin))
Packit Service 97d2fb
	{
Packit Service 97d2fb
	  if (getline (&buf, &len, stdin) < 0)
Packit Service 97d2fb
	    break;
Packit Service 97d2fb
Packit Service 97d2fb
	  char *endp;
Packit Service 97d2fb
	  uintmax_t addr = strtoumax (buf, &endp, 0);
Packit Service 97d2fb
	  if (endp != buf)
Packit Service 97d2fb
	    result |= handle_address (addr, dwfl);
Packit Service 97d2fb
	  else
Packit Service 97d2fb
	    result = 1;
Packit Service 97d2fb
	}
Packit Service 97d2fb
Packit Service 97d2fb
      free (buf);
Packit Service 97d2fb
    }
Packit Service 97d2fb
  else
Packit Service 97d2fb
    {
Packit Service 97d2fb
      do
Packit Service 97d2fb
	{
Packit Service 97d2fb
	  char *endp;
Packit Service 97d2fb
	  uintmax_t addr = strtoumax (argv[remaining], &endp, 0);
Packit Service 97d2fb
	  if (endp != argv[remaining])
Packit Service 97d2fb
	    result |= handle_address (addr, dwfl);
Packit Service 97d2fb
	  else
Packit Service 97d2fb
	    result = 1;
Packit Service 97d2fb
	}
Packit Service 97d2fb
      while (++remaining < argc);
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  dwfl_end (dwfl);
Packit Service 97d2fb
Packit Service 97d2fb
  return result;
Packit Service 97d2fb
}