1/* Test dwarf_get_units finds split DWO CUs.
2   Copyright (C) 2018 Red Hat, Inc.
3   This file is part of elfutils.
4
5   This file is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 3 of the License, or
8   (at your option) any later version.
9
10   elfutils is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
22#include <dwarf.h>
23#include ELFUTILS_HEADER(dw)
24#include <stdio.h>
25#include <inttypes.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29#include <unistd.h>
30
31
32int
33main (int argc, char *argv[])
34{
35  for (int i = 1; i < argc; i++)
36    {
37      printf ("file: %s\n", argv[i]);
38      int fd = open (argv[i], O_RDONLY);
39      Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
40      if (dbg == NULL)
41	{
42	  printf ("%s not usable: %s\n", argv[i], dwarf_errmsg (-1));
43	  return -1;
44	}
45
46      Dwarf_CU *cu = NULL;
47      Dwarf_Die cudie, subdie;
48      uint8_t unit_type;
49      int count = 0;
50      while (dwarf_get_units (dbg, cu, &cu, NULL,
51			      &unit_type, &cudie, &subdie) == 0)
52	{
53	  count++;
54	  printf ("Got cudie unit_type: %" PRIx8 "\n", unit_type);
55
56	  if (unit_type == DW_UT_skeleton)
57	    {
58	      Dwarf_CU *skel_cu = cudie.cu;
59	      Dwarf_CU *split_cu = subdie.cu;
60	      Dwarf_Die skel_die, split_die;
61	      uint64_t skel_id, split_id;
62
63	      printf ("Found a skeleton unit, with split die: %s\n",
64		      dwarf_diename (&subdie));
65
66	      if (dwarf_cu_die (skel_cu, &skel_die, NULL, NULL, NULL, NULL,
67				&skel_id, NULL) == NULL)
68		{
69		  printf ("bad skel_cu: %s\n", dwarf_errmsg (-1));
70		  return -1;
71		}
72
73	      if (dwarf_cu_die (split_cu, &split_die, NULL, NULL, NULL, NULL,
74				&split_id, NULL) == NULL)
75		{
76		  printf ("bad skel_cu: %s\n", dwarf_errmsg (-1));
77		  return -1;
78		}
79
80	      if (skel_id != split_id)
81		{
82		  printf ("Skeleton id and Split id not equal!\n");
83		  return -1;
84		}
85	    }
86	}
87
88      if (count == 0)
89	{
90	  printf ("No units found\n");
91	  return -1;
92	}
93
94      dwarf_end (dbg);
95      close (fd);
96
97      printf ("\n");
98    }
99
100  return 0;
101}
102