lin
2025-01-10 9ec4e21f2f615ef95b70a249569906799e36bace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Test program for dwarf_peel_type. Peels type of top-level vars.
   Copyright (C) 2017 Red Hat, Inc.
   This file is part of elfutils.
 
   This file is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
 
   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
 
#include <assert.h>
#include <argp.h>
#include <inttypes.h>
#include <fcntl.h>
#include ELFUTILS_HEADER(dw)
#include ELFUTILS_HEADER(dwfl)
#include <stdio.h>
#include <unistd.h>
#include <dwarf.h>
 
#include "../libdw/known-dwarf.h"
 
static const char *
dwarf_tag_string (unsigned int tag)
{
  switch (tag)
    {
#define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
      DWARF_ALL_KNOWN_DW_TAG
#undef DWARF_ONE_KNOWN_DW_TAG
    default:
      return NULL;
    }
}
 
void
print_var_raw_type (Dwarf_Die *var)
{
  Dwarf_Attribute attr_mem;
  Dwarf_Die type_mem;
  Dwarf_Die *type;
  const char *name = dwarf_diename (var);
 
  type = dwarf_formref_die (dwarf_attr (var, DW_AT_type, &attr_mem),
               &type_mem);
  if (type != NULL)
    {
      /* Test twice, once with a separate result DIE. Then with the
    DIE itself. The resulting tag should be the same. */
      Dwarf_Die result_mem;
      Dwarf_Die *result = &result_mem;
      int res = dwarf_peel_type (type, result);
      if (res < 0)
        printf ("%s error peeling type: %s\n", name, dwarf_errmsg (-1));
      else if (res > 0)
   printf ("%s missing DW_TAG_TYPE, could peel further: %s\n",
       name, dwarf_tag_string (dwarf_tag (result)));
      else
   {
     int tag = dwarf_tag (result);
     printf ("%s raw type %s\n", name, dwarf_tag_string (tag));
     res = dwarf_peel_type (type, type);
     if (res < 0)
       printf ("%s cannot peel type itself: %s\n", name,
           dwarf_errmsg (-1));
     else if (res > 0)
   printf ("%s missing DW_TAG_TYPE, could peel type further: %s\n",
       name, dwarf_tag_string (dwarf_tag (type)));
     else if (dwarf_tag (type) != tag)
       printf ("%s doesn't resolve the same: %s != %s\n", name,
           dwarf_tag_string (tag),
           dwarf_tag_string (dwarf_tag (type)));
   }
    }
  else
    printf ("%s has no type.\n", name);
}
 
int
main (int argc, char *argv[])
{
 
  int remaining;
  Dwfl *dwfl;
  (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
                     &dwfl);
  assert (dwfl != NULL);
 
  Dwarf_Die *cu = NULL;
  Dwarf_Addr dwbias;
  while ((cu = dwfl_nextcu (dwfl, cu, &dwbias)) != NULL)
    {
      Dwarf_Die die_mem;
      Dwarf_Die *die = &die_mem;
      dwarf_child (cu, &die_mem);
 
      while (1)
   {
     if (dwarf_tag (die) == DW_TAG_variable)
       print_var_raw_type (die);
 
     if (dwarf_siblingof (die, &die_mem) != 0)
       break;
   }
    }
 
  dwfl_end (dwfl);
}