1 /* DWARF attributes
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 #include "defs.h"
28 #include "dwarf2/attribute.h"
29 #include "dwarf2/stringify.h"
30 #include "complaints.h"
31
32 /* See attribute.h. */
33
34 CORE_ADDR
as_address()35 attribute::as_address () const
36 {
37 CORE_ADDR addr;
38
39 gdb_assert (!requires_reprocessing);
40
41 if (form != DW_FORM_addr && form != DW_FORM_addrx
42 && form != DW_FORM_GNU_addr_index)
43 {
44 /* Aside from a few clearly defined exceptions, attributes that
45 contain an address must always be in DW_FORM_addr form.
46 Unfortunately, some compilers happen to be violating this
47 requirement by encoding addresses using other forms, such
48 as DW_FORM_data4 for example. For those broken compilers,
49 we try to do our best, without any guarantee of success,
50 to interpret the address correctly. It would also be nice
51 to generate a complaint, but that would require us to maintain
52 a list of legitimate cases where a non-address form is allowed,
53 as well as update callers to pass in at least the CU's DWARF
54 version. This is more overhead than what we're willing to
55 expand for a pretty rare case. */
56 addr = u.unsnd;
57 }
58 else
59 addr = u.addr;
60
61 return addr;
62 }
63
64 /* See attribute.h. */
65
66 bool
form_is_string()67 attribute::form_is_string () const
68 {
69 return (form == DW_FORM_strp || form == DW_FORM_line_strp
70 || form == DW_FORM_string
71 || form == DW_FORM_strx
72 || form == DW_FORM_strx1
73 || form == DW_FORM_strx2
74 || form == DW_FORM_strx3
75 || form == DW_FORM_strx4
76 || form == DW_FORM_GNU_str_index
77 || form == DW_FORM_GNU_strp_alt);
78 }
79
80 /* See attribute.h. */
81
82 const char *
as_string()83 attribute::as_string () const
84 {
85 gdb_assert (!requires_reprocessing);
86 if (form_is_string ())
87 return u.str;
88 return nullptr;
89 }
90
91 /* See attribute.h. */
92
93 bool
form_is_block()94 attribute::form_is_block () const
95 {
96 return (form == DW_FORM_block1
97 || form == DW_FORM_block2
98 || form == DW_FORM_block4
99 || form == DW_FORM_block
100 || form == DW_FORM_exprloc
101 || form == DW_FORM_data16);
102 }
103
104 /* See attribute.h. */
105
106 bool
form_is_section_offset()107 attribute::form_is_section_offset () const
108 {
109 return (form == DW_FORM_data4
110 || form == DW_FORM_data8
111 || form == DW_FORM_sec_offset
112 || form == DW_FORM_loclistx);
113 }
114
115 /* See attribute.h. */
116
117 bool
form_is_constant()118 attribute::form_is_constant () const
119 {
120 switch (form)
121 {
122 case DW_FORM_sdata:
123 case DW_FORM_udata:
124 case DW_FORM_data1:
125 case DW_FORM_data2:
126 case DW_FORM_data4:
127 case DW_FORM_data8:
128 case DW_FORM_implicit_const:
129 return true;
130 default:
131 return false;
132 }
133 }
134
135 /* See attribute.h. */
136
137 void
get_ref_die_offset_complaint()138 attribute::get_ref_die_offset_complaint () const
139 {
140 complaint (_("unsupported die ref attribute form: '%s'"),
141 dwarf_form_name (form));
142 }
143
144 /* See attribute.h. */
145
146 LONGEST
constant_value(int default_value)147 attribute::constant_value (int default_value) const
148 {
149 if (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
150 return u.snd;
151 else if (form == DW_FORM_udata
152 || form == DW_FORM_data1
153 || form == DW_FORM_data2
154 || form == DW_FORM_data4
155 || form == DW_FORM_data8)
156 return u.unsnd;
157 else
158 {
159 /* For DW_FORM_data16 see attribute::form_is_constant. */
160 complaint (_("Attribute value is not a constant (%s)"),
161 dwarf_form_name (form));
162 return default_value;
163 }
164 }
165
166 /* See attribute.h. */
167
168 bool
form_is_unsigned()169 attribute::form_is_unsigned () const
170 {
171 return (form == DW_FORM_ref_addr
172 || form == DW_FORM_GNU_ref_alt
173 || form == DW_FORM_data2
174 || form == DW_FORM_data4
175 || form == DW_FORM_data8
176 || form == DW_FORM_sec_offset
177 || form == DW_FORM_data1
178 || form == DW_FORM_flag
179 || form == DW_FORM_flag_present
180 || form == DW_FORM_udata
181 || form == DW_FORM_rnglistx
182 || form == DW_FORM_loclistx
183 || form == DW_FORM_ref1
184 || form == DW_FORM_ref2
185 || form == DW_FORM_ref4
186 || form == DW_FORM_ref8
187 || form == DW_FORM_ref_udata);
188 }
189
190 /* See attribute.h. */
191
192 bool
form_is_signed()193 attribute::form_is_signed () const
194 {
195 return form == DW_FORM_sdata || form == DW_FORM_implicit_const;
196 }
197
198 /* See attribute.h. */
199
200 bool
form_requires_reprocessing()201 attribute::form_requires_reprocessing () const
202 {
203 return (form == DW_FORM_strx
204 || form == DW_FORM_strx1
205 || form == DW_FORM_strx2
206 || form == DW_FORM_strx3
207 || form == DW_FORM_strx4
208 || form == DW_FORM_GNU_str_index
209 || form == DW_FORM_addrx
210 || form == DW_FORM_GNU_addr_index
211 || form == DW_FORM_rnglistx
212 || form == DW_FORM_loclistx);
213 }
214
215 /* See attribute.h. */
216
217 dwarf_defaulted_attribute
defaulted()218 attribute::defaulted () const
219 {
220 LONGEST value = constant_value (-1);
221
222 switch (value)
223 {
224 case DW_DEFAULTED_no:
225 case DW_DEFAULTED_in_class:
226 case DW_DEFAULTED_out_of_class:
227 return (dwarf_defaulted_attribute) value;
228 }
229
230 /* If the form was not constant, we already complained in
231 constant_value, so there's no need to complain again. */
232 if (form_is_constant ())
233 complaint (_("unrecognized DW_AT_defaulted value (%s)"),
234 plongest (value));
235 return DW_DEFAULTED_no;
236 }
237
238 /* See attribute.h. */
239
240 dwarf_virtuality_attribute
as_virtuality()241 attribute::as_virtuality () const
242 {
243 LONGEST value = constant_value (-1);
244
245 switch (value)
246 {
247 case DW_VIRTUALITY_none:
248 case DW_VIRTUALITY_virtual:
249 case DW_VIRTUALITY_pure_virtual:
250 return (dwarf_virtuality_attribute) value;
251 }
252
253 /* If the form was not constant, we already complained in
254 constant_value, so there's no need to complain again. */
255 if (form_is_constant ())
256 complaint (_("unrecognized DW_AT_virtuality value (%s)"),
257 plongest (value));
258 return DW_VIRTUALITY_none;
259 }
260
261 /* See attribute.h. */
262
263 bool
as_boolean()264 attribute::as_boolean () const
265 {
266 if (form == DW_FORM_flag_present)
267 return true;
268 else if (form == DW_FORM_flag)
269 return u.unsnd != 0;
270 return constant_value (0) != 0;
271 }
272