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      Dwarf_Half version;
50      int count = 0;
51      while (dwarf_get_units (dbg, cu, &cu, &version,
52			      &unit_type, &cudie, &subdie) == 0)
53	{
54	  count++;
55	  if (unit_type == DW_UT_skeleton)
56	    {
57	      Dwarf_Attribute attr;
58	      Dwarf_Word word;
59	      Dwarf_Addr addr;
60
61	      printf ("Split DIE: %s\n", dwarf_diename (&subdie));
62
63	      if (dwarf_attr_integrate (&subdie,
64					DW_AT_GNU_addr_base, &attr) == NULL
65		  && dwarf_attr_integrate (&subdie,
66					   DW_AT_addr_base, &attr) == NULL)
67		printf ("No addr_base");
68	      else if (dwarf_formudata (&attr, &word) != 0)
69		printf ("Bad addr_base: %s\n", dwarf_errmsg (-1));
70	      else
71		printf ("addr_base secoff: 0x%" PRIx64 "\n", word);
72
73	      if (dwarf_attr (&subdie, DW_AT_low_pc, &attr) != NULL)
74		printf ("Unexpected low_pc on split DIE.\n");
75
76	      if (dwarf_attr_integrate (&subdie,
77					DW_AT_low_pc, &attr) == NULL)
78		printf ("No low_pc");
79	      else if (dwarf_formaddr (&attr, &addr) != 0)
80		printf ("Bad low_pc: %s\n", dwarf_errmsg (-1));
81	      else
82		printf ("low_pc addr: 0x%" PRIx64 "\n", addr);
83
84	      if (dwarf_hasattr (&subdie, DW_AT_high_pc))
85		printf ("Unexpected highpc on split DIE\n");
86	      if (dwarf_hasattr (&subdie, DW_AT_ranges))
87		printf ("Unexpected ranges on split DIE\n");
88
89	      if (dwarf_hasattr_integrate (&subdie, DW_AT_high_pc))
90		printf ("Skel has high_pc.\n");
91	      if (dwarf_hasattr_integrate (&subdie, DW_AT_ranges))
92		printf ("Skel has ranges.\n");
93
94	      printf ("\n");
95	    }
96	}
97
98      if (count == 0)
99	{
100	  printf ("No units found\n");
101	  return -1;
102	}
103
104      dwarf_end (dbg);
105      close (fd);
106    }
107
108  return 0;
109}
110