summaryrefslogtreecommitdiff
path: root/dwarflint/check_dups_abstract_origin.cc
blob: f81d957a6973c9d25a1d9a42b7f24ded3580577e (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Pedantic checking of DWARF files.
   Copyright (C) 2009, 2010, 2011 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 "check_die_tree.hh"
#include "pri.hh"
#include "messages.hh"
#include <map>

using elfutils::dwarf;

namespace
{
  class check_dups_abstract_origin
    : public die_check
  {
  public:
    static checkdescriptor const *descriptor ()
    {
      static checkdescriptor cd
	(checkdescriptor::create ("check_dups_abstract_origin")
	 .description (
"If a given attribute name is present on a DIE, it is "
"suspicious if that attribute name appears on the DIE that's the "
"first DIE's DW_AT_abstract_origin or DW_AT_specification.\n"
" https://bugzilla.redhat.com/show_bug.cgi?id=527430\n"));
      return &cd;
    }

    bool
    duplicate_ok (int tag, int at, int from, int ref_tag, bool same)
    {
      // A call site entry has a DW_AT_low_pc attribute which is the return
      // address after the call and a DW_AT_abstract_origin that is a
      // pointer to the reference it calls directly or indirectly. So
      // both may be available also at the abstract_origin (with different
      // values).
      if (tag == DW_TAG_GNU_call_site
	  && (at == DW_AT_low_pc || at == DW_AT_abstract_origin)
	  && from == DW_AT_abstract_origin
	  && ! same)
	return true;

      // A subprogram that has a concrete out-of-line instance might
      // have an object_pointer different from the original variant
      // of the subprogram. Similar for a subprogram specification,
      // which may refer to the specification die of the object_pointer,
      // while the instance of the subprogram will refer to the
      // actual instance of the object_pointer die.
      if (tag == DW_TAG_subprogram
	  && at == DW_AT_object_pointer
	  && (from == DW_AT_abstract_origin || from == DW_AT_specification)
	  && ref_tag == DW_TAG_subprogram
	  && ! same)
	return true;

      // A subprogram can be defined outside the body of the enclosing
      // class, then file and/or line attributes can differ.
      if (tag == DW_TAG_subprogram
	  && from == DW_AT_specification
	  && (at == DW_AT_decl_line || at == DW_AT_decl_file)
	  && ref_tag == DW_TAG_subprogram
	  && ! same)
	return true;

      // Same for a member variable can be defined outside the body of the
      // enclosing class, then file and/or line attributes can differ.
      if (tag == DW_TAG_variable
	  && from == DW_AT_specification
	  && (at == DW_AT_decl_line || at == DW_AT_decl_file)
	  && ref_tag == DW_TAG_member
	  && ! same)
	return true;


      return false;
    }

    void
    check_die_attr (dwarf::debug_info_entry const &entry,
		    dwarf::attribute const &attr)
    {
      std::map<unsigned int, dwarf::attr_value> m;
      for (dwarf::debug_info_entry::attributes_type::const_iterator
	     at = entry.attributes ().begin ();
	   at != entry.attributes ().end (); ++at)
	m.insert (std::make_pair ((*at).first, (*at).second));

      dwarf::attr_value const &val = attr.second;
      // xxx Referree can't be const&, gives memory errors.
      dwarf::debug_info_entry referree = *val.reference ();

      std::map<unsigned int, dwarf::attr_value>::const_iterator at2;
      for (dwarf::debug_info_entry::attributes_type::const_iterator
	     at = referree.attributes ().begin ();
	   at != referree.attributes ().end (); ++at)
	if ((at2 = m.find ((*at).first)) != m.end ()
	    && ! duplicate_ok (entry.tag (), at2->first, attr.first,
			       referree.tag (), at2->second == (*at).second))
	  wr_message (die_locus (entry), mc_impact_3 | mc_acc_bloat | mc_die_rel)
	    .id (descriptor ())
	    << dwarf::tags::name (entry.tag ())
	    << " attribute " << dwarf::attributes::name (at2->first)
	    << " is duplicated at " << dwarf::attributes::name (attr.first)
	    << " (" << pri::ref (referree) << ")"
	    << (at2->second == (*at).second
		? "." : " with different value.")
	    << std::endl;
    }

    explicit
    check_dups_abstract_origin (highlevel_check_i *, checkstack &, dwarflint &)
    {
      // No state necessary.
    }

    virtual void
    die (all_dies_iterator<dwarf> const &it)
    {
      // Do we have DW_AT_abstract_origin or DW_AT_specification?
      dwarf::debug_info_entry const &entry = *it;
      for (dwarf::debug_info_entry::attributes_type::const_iterator
	     at = entry.attributes ().begin ();
	   at != entry.attributes ().end (); ++at)
	if ((*at).first == DW_AT_abstract_origin
	    || (*at).first == DW_AT_specification)
	  {
	    assert ((*at).second.what_space () == dwarf::VS_reference);
	    check_die_attr (entry, *at);
	  }
    }
  };

  reg_die_check<check_dups_abstract_origin> reg;
}