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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/* Pedantic checking of DWARF files
Copyright (C) 2009,2010,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>. */
// The tables here capture attribute/allowed forms depending on DWARF
// version. Apart from standardized DWARF formats, e.g. DWARF3+GNU is
// a version of its own.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <map>
#include <cassert>
#include <string.h>
#include "../libdw/c++/dwarf"
#include "dwarf_version.hh"
#include "dwarf_2.hh"
#include "dwarf_3.hh"
#include "dwarf_4.hh"
#include "dwarf_gnu.hh"
#include "dwarf_mips.hh"
#include "check_debug_info.hh"
global_opt<void_option>
opt_nognu ("Don't use GNU extension.", "nognu");
dw_class_set::dw_class_set (dw_class a, dw_class b, dw_class c,
dw_class d, dw_class e)
{
#define ADD(V) if (V != max_dw_class) (*this)[V] = true
ADD (a);
ADD (b);
ADD (c);
ADD (d);
ADD (e);
#undef ADD
}
form::form (int a_name, dw_class_set a_classes,
form_width_t a_width, storage_class_t a_storclass,
form_bitness_t a_bitness)
: _m_name (a_name)
, _m_classes (a_classes)
, _m_width (a_width)
, _m_storclass (a_storclass)
, _m_bitness (a_bitness)
{}
form::form (int a_name, dw_class_set a_classes,
form_width_special_t a_width, storage_class_t a_storclass,
form_bitness_t a_bitness)
: _m_name (a_name)
, _m_classes (a_classes)
, _m_width (a_width)
, _m_storclass (a_storclass)
, _m_bitness (a_bitness)
{}
dw_class
dwarf_version::form_class (form const *form, attribute const *attribute) const
{
assert (form != NULL);
assert (attribute != NULL);
dw_class_set result = form->classes ();
result &= attribute->classes ();
if (result.count () > 1)
{
dw_class ret = this->ambiguous_class (form, attribute, result);
assert (ret < max_dw_class);
assert (result[ret]);
return ret;
}
else if (result.count () == 1)
return static_cast<dw_class> (ffsl (result.to_ulong ()) - 1);
else
return max_dw_class;
}
form_width_t
form::width (cu_head const *cu_head) const
{
switch (_m_width)
{
case fw_offset:
case fw_address:
if (unlikely (cu_head == NULL))
return fw_unknown;
if (_m_width == fw_offset)
return static_cast<form_width_t> (cu_head->offset_size);
else
return static_cast<form_width_t> (cu_head->address_size);
default:
return static_cast<form_width_t> (_m_width);
}
}
std::ostream &
operator << (std::ostream &os, form const &obj)
{
return os << elfutils::dwarf::forms::identifier (obj.name ());
}
namespace
{
dw_class_set
include_indirect (dw_class_set a_classes)
{
a_classes.set (cl_indirect);
return a_classes;
}
}
attribute::attribute (int a_name, dw_class_set const &a_classes)
: _m_name (a_name)
, _m_classes (include_indirect (a_classes))
{}
std::ostream &
operator << (std::ostream &os, attribute const &obj)
{
return os << elfutils::dwarf::attributes::identifier (obj.name ());
}
bool
dwarf_version::form_allowed (int form) const
{
return get_form (form) != NULL;
}
bool
dwarf_version::form_allowed (attribute const *attr, form const *form) const
{
dw_class_set const &attr_classes = attr->classes ();
dw_class_set const &form_classes = form->classes ();
return (attr_classes & form_classes).any ();
}
sibling_form_suitable_t
sibling_form_suitable (dwarf_version const *ver, form const *form)
{
if (!ver->form_allowed (ver->get_attribute (DW_AT_sibling), form))
return sfs_invalid;
else if (form->name () == DW_FORM_ref_addr)
return sfs_long;
else
return sfs_ok;
}
namespace
{
class dwarf_version_union
: public dwarf_version
{
dwarf_version const *_m_source;
dwarf_version const *_m_extension;
public:
dwarf_version_union (dwarf_version const *source,
dwarf_version const *extension)
: _m_source (source)
, _m_extension (extension)
{
}
template<class T>
T const *
lookfor (int name, T const*(dwarf_version::*getter) (int) const) const
{
if (T const *emt = (_m_extension->*getter) (name))
return emt;
else
return (_m_source->*getter) (name);
}
form const *
get_form (int form_name) const
{
return lookfor (form_name, &dwarf_version::get_form);
}
attribute const *
get_attribute (int attribute_name) const
{
return lookfor (attribute_name, &dwarf_version::get_attribute);
}
dw_class
ambiguous_class (form const *form,
attribute const *attribute,
dw_class_set const &candidates) const
{
dw_class ret = _m_extension->ambiguous_class (form, attribute, candidates);
if (ret == max_dw_class)
ret = _m_source->ambiguous_class (form, attribute, candidates);
return ret;
}
bool
form_allowed (attribute const *attr, form const *form) const
{
// In GNU mode any combination of new attribute/old form goes,
// in strict mode only the latest.
if (opt_nognu)
return _m_extension->form_allowed (attr, form);
else
return (_m_source->form_allowed (attr, form)
|| _m_extension->form_allowed (attr, form));
}
};
}
dwarf_version const *
dwarf_version::extend (dwarf_version const *source,
dwarf_version const *extension)
{
assert (source != NULL);
assert (extension != NULL);
// this leaks, but we don't really care. These objects have to live
// the whole execution time anyway.
return new dwarf_version_union (source, extension);
}
namespace
{
dwarf_version const *get_ext ()
{
// xxx The GNU toolchain commonly uses DW_AT_MIPS_linkage_name,
// which is part of the MIPS extensions. So that's what we
// return. I wonder how to solve this "right". We cannot simply
// request DW_AT_producer/DW_AT_language values here, since we
// need the version to know how to read these attributes in the
// first place.
if (opt_nognu)
return dwarf_mips_ext ();
else
return dwarf_version::extend (dwarf_mips_ext (), dwarf_gnu_ext ());
}
}
dwarf_version const *
dwarf_version::get (unsigned version)
{
static dwarf_version const *ext = get_ext ();
switch (version)
{
case 2:
{
static dwarf_version const *dw = extend (dwarf_2 (), ext);
return dw;
}
case 3:
{
static dwarf_version const *dw = extend (dwarf_3 (), ext);
return dw;
}
case 4:
{
static dwarf_version const *dw = extend (dwarf_4 (), ext);
return dw;
}
default:
return NULL;
};
}
dwarf_version const *
dwarf_version::get_latest ()
{
return get (4);
}
|