summaryrefslogtreecommitdiff
path: root/dwarflint/check_linkage_external_die.cc
blob: 760b95947c8c12117af60eee0fdd90b6baf3e16d (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Check that every die that has a linkage_name is also external.
   Copyright (C) 2011 Red Hat, Inc.
   This file is part of Red Hat elfutils.

   Red Hat elfutils 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; version 2 of the License.

   Red Hat 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 Red Hat elfutils; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.

   Red Hat elfutils is an included package of the Open Invention Network.
   An included package of the Open Invention Network is a package for which
   Open Invention Network licensees cross-license their patents.  No patent
   license is granted, either expressly or impliedly, by designation as an
   included package.  Should you wish to participate in the Open Invention
   Network licensing program, please visit www.openinventionnetwork.com
   <http://www.openinventionnetwork.com>.  */

#include "check_die_tree.hh"
#include "pri.hh"
#include "messages.hh"


#include "../libelf/gelf.h"
#include "../libdw/libdw.h"

using elfutils::dwarf;

namespace
{
  class check_linkage_external_die
    : public die_check
  {
  private:
    std::map<std::string, bool> _m_symbols;

  public:
    static checkdescriptor const *descriptor ()
    {
      static checkdescriptor cd
	(checkdescriptor::create ("check_linkage_external_die")
	 .inherit<highlevel_check<check_linkage_external_die> > ()
	 .description (
"Check that each DIE that has a linkage_name also has an external attribute.\n"
		       ));
      return &cd;
    }

    check_linkage_external_die (highlevel_check_i *check,
				checkstack &, dwarflint &)
    {
      // Extract all symbol table names for objects and functions
      // and store whether they are global or not in _m_symbols.
      Dwarf *dwarf = check->c_dw;
      Elf *elf = dwarf_getelf (dwarf);
      Elf_Scn *scn = NULL;
      while ((scn = elf_nextscn (elf, scn)) != NULL)
	{
	  GElf_Shdr shdr_mem;
	  GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
	  if (shdr != NULL && (shdr->sh_type == SHT_DYNSYM
			       || shdr->sh_type == SHT_SYMTAB))
	    {
	      Elf_Data *data = elf_getdata (scn, NULL);
	      size_t shstrndx;
	      elf_getshdrstrndx (elf, &shstrndx);
	      unsigned int syms = shdr->sh_size / shdr->sh_entsize;
	      for (unsigned int cnt = 0; cnt < syms; ++cnt)
		{
		  GElf_Sym sym_mem;
		  GElf_Sym *sym = gelf_getsym (data, cnt, &sym_mem);
		  if (sym != NULL
		      && (GELF_ST_TYPE (sym->st_info) == STT_OBJECT
			  || GELF_ST_TYPE (sym->st_info) == STT_FUNC))
		    {
		      const char *name;
		      name = elf_strptr (elf, shdr->sh_link, sym->st_name);
		      if (name != NULL)
			{
			  // Regard anything not explicitly marked as local
			  // a global symbol, it could be STB_GLOBAL,
			  // STB_WEAK, STB_GNU_UNIQUE, ...
			  unsigned int binding = GELF_ST_BIND (sym->st_info);
			  bool global = binding != STB_LOCAL;
			  using namespace std;
			  _m_symbols.insert (pair<string, bool>
					     (string (name), global));
			}
		    }
		}
	    }
	}
    }

    static bool is_external (all_dies_iterator<dwarf> const &it)
    {
      bool candidates = true;
      dwarf::debug_info_entry entry = *it;
      do
	{
	  dwarf::debug_info_entry::attributes_type attrs = entry.attributes ();
	  if (attrs.find (DW_AT_external) != attrs.end ())
	    return true;

	  dwarf::debug_info_entry::attributes_type::const_iterator origin
	    = attrs.find (DW_AT_abstract_origin);
	  if (origin == attrs.end ())
	    origin = attrs.find (DW_AT_specification);

	  if (origin != attrs.end ())
	    entry = *(*origin).second.reference ();
	  else
	    candidates = false;
	}
      while (candidates);

      return false;
    }

    virtual void
    die (all_dies_iterator<dwarf> const &it)
    {
      dwarf::debug_info_entry const &entry = *it;
      dwarf::debug_info_entry::attributes_type attrs = entry.attributes ();
      dwarf::debug_info_entry::attributes_type::const_iterator linkage_name
	= attrs.find (DW_AT_linkage_name);
      if (linkage_name == attrs.end ())
	linkage_name = attrs.find (DW_AT_MIPS_linkage_name);
      if (linkage_name != attrs.end ())
	{
	  using namespace std;
	  const char *name = (*linkage_name).second.string ();
	  map<string, bool>::iterator s = _m_symbols.find (string (name));
	  if (s == _m_symbols.end ())
	    {
	      // No symbol in table, OK, if not a defining or const object.
	      // GNU extension, anonymous structs can have a linkage_name.
	      if (attrs.find (DW_AT_declaration) == attrs.end ()
		  && attrs.find (DW_AT_const_value) == attrs.end ()
		  && ((entry.tag () != DW_TAG_structure_type
		      && entry.tag () != DW_TAG_enumeration_type)
		      || attrs.find (DW_AT_name) != attrs.end ()))
		{
		  wr_message (to_where (entry),
			      mc_impact_3 | mc_acc_suboptimal | mc_die_other)
		    .id (descriptor ())
		    << elfutils::dwarf::tags::name (entry.tag ())
		    << " has linkage_name attribute `"
		    << name << "', which is not in string table,"
		    << " but DIE is not marked as a declaration"
		    << " or const value."
		    << std::endl;
		}
	    }
	  else if ((*s).second == false)
	    {
	      // Local symbol in table, OK if not a defining object
	      // and marked external. Which means it comes from an
	      // external symbol table.
	      if (attrs.find (DW_AT_declaration) == attrs.end ()
		  && is_external (it))
		{
		  wr_message (to_where (entry),
			      mc_impact_3 | mc_acc_suboptimal | mc_die_other)
		    .id (descriptor ())
		    << elfutils::dwarf::tags::name (entry.tag ())
		    << " has linkage_name attribute `"
		    << name << "', which is a local symbol."
		    << std::endl;
		}
	    }
	  else if (! is_external (it))
	    {
	      // Global symbol in symbol table, not marked external.
	      // Always bad.
	      wr_message (to_where (entry),
			  mc_impact_3 | mc_acc_suboptimal | mc_die_other)
		.id (descriptor ())
		<< elfutils::dwarf::tags::name (entry.tag ())
		<< " has linkage_name attribute, but no external attribute."
		<< std::endl;
	    }
	}
    }
  };

  reg_die_check<check_linkage_external_die> reg;
}