103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes/* Test program for dwarf_lowpc and dwarf_highpc
203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   Copyright (C) 2012 Red Hat, Inc.
303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   This file is part of elfutils.
403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   This file is free software; you can redistribute it and/or modify
603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   it under the terms of the GNU General Public License as published by
703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   the Free Software Foundation; either version 3 of the License, or
803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   (at your option) any later version.
903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   elfutils is distributed in the hope that it will be useful, but
1103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   WITHOUT ANY WARRANTY; without even the implied warranty of
1203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   GNU General Public License for more details.
1403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   You should have received a copy of the GNU General Public License
1603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
1703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <config.h>
1903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <assert.h>
2003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <inttypes.h>
2103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include ELFUTILS_HEADER(dwfl)
2203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <dwarf.h>
2303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <argp.h>
2403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <stdio.h>
2503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <stdio_ext.h>
2603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <locale.h>
2703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <stdlib.h>
2803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <error.h>
2903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <string.h>
3003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <fnmatch.h>
3103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
3203333823c75a1c1887e923828113a1b0fd12020cElliott Hughesstruct args
3303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
3403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwfl *dwfl;
3503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Die *cu;
3603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Addr dwbias;
3703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  char **argv;
3803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  const char *file;
3903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes};
4003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
4103333823c75a1c1887e923828113a1b0fd12020cElliott Hughesstatic struct args *args;
4203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
4303333823c75a1c1887e923828113a1b0fd12020cElliott Hughesstatic void
4403333823c75a1c1887e923828113a1b0fd12020cElliott Hughesfail(Dwarf_Off off, const char *name, const char *msg)
4503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
4603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  printf("%s: [%" PRIx64 "] '%s' %s\n", args->file, off, name, msg);
4703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  exit(-1);
4803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
4903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
5003333823c75a1c1887e923828113a1b0fd12020cElliott Hughesstatic int
5103333823c75a1c1887e923828113a1b0fd12020cElliott Hugheshandle_die (Dwarf_Die *die, void *arg)
5203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
5303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  args = arg;
5403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Off off = dwarf_dieoffset (die);
5503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
5603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  const char *name = dwarf_diename (die);
5703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (name == NULL)
5803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    fail (off, "<no name>", "die without a name");
5903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
6003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Addr lowpc = 0;
6103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Addr highpc = 0;
6203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (dwarf_lowpc (die, &lowpc) != 0 && dwarf_hasattr (die, DW_AT_low_pc))
6303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    fail (off, name, "has DW_AT_low_pc but dwarf_lowpc fails");
6403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (dwarf_highpc (die, &highpc) != 0 && dwarf_hasattr (die, DW_AT_high_pc))
6503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    fail (off, name, "has DW_AT_high_pc but dwarf_highpc fails");
6603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
6703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  /* GCC < 4.7 had a bug where no code CUs got a highpc == lowpc.
6803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes     Allow that, because it is not the main purpose of this test.  */
6903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (dwarf_hasattr (die, DW_AT_low_pc)
7003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      && dwarf_hasattr (die, DW_AT_high_pc)
7103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      && highpc <= lowpc
7203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      && ! (dwarf_tag (die) == DW_TAG_compile_unit && highpc == lowpc))
7303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    {
7403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      printf("lowpc: %" PRIx64 ", highpc: %" PRIx64 "lx\n", lowpc, highpc);
7503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      fail (off, name, "highpc <= lowpc");
7603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    }
7703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
7803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  return 0;
7903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
8003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
8103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
8203333823c75a1c1887e923828113a1b0fd12020cElliott Hughesint
8303333823c75a1c1887e923828113a1b0fd12020cElliott Hughesmain (int argc, char *argv[])
8403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
8503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  int remaining;
8603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
8703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  /* Set locale.  */
8803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  (void) setlocale (LC_ALL, "");
8903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
9003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  struct args a = { .dwfl = NULL, .cu = NULL };
9103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
9203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
9303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes		     &a.dwfl);
9403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  assert (a.dwfl != NULL);
9503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  a.argv = &argv[remaining];
9603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
9703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  int result = 0;
9803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
9903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  while ((a.cu = dwfl_nextcu (a.dwfl, a.cu, &a.dwbias)) != NULL)
10003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    {
10103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      a.file = dwarf_diename (a.cu);
10203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      handle_die (a.cu, &a);
10303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      dwarf_getfuncs (a.cu, &handle_die, &a, 0);
10403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    }
10503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
10603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  dwfl_end (a.dwfl);
10703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
10803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  return result;
10903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
110