1/* DWARF attributes
2
3 Copyright (C) 1994-2025 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 "dwarf2/attribute.h"
28#include "dwarf2/stringify.h"
29#include "complaints.h"
30
31/* See attribute.h. */
32
33unrelocated_addr
34attribute::as_address () const
35{
36 unrelocated_addr addr;
37
38 gdb_assert (!requires_reprocessing);
39
40 if (form != DW_FORM_addr && form != DW_FORM_addrx
41 && form != DW_FORM_GNU_addr_index)
42 {
43 /* Aside from a few clearly defined exceptions, attributes that
44 contain an address must always be in DW_FORM_addr form.
45 Unfortunately, some compilers happen to be violating this
46 requirement by encoding addresses using other forms, such
47 as DW_FORM_data4 for example. For those broken compilers,
48 we try to do our best, without any guarantee of success,
49 to interpret the address correctly. It would also be nice
50 to generate a complaint, but that would require us to maintain
51 a list of legitimate cases where a non-address form is allowed,
52 as well as update callers to pass in at least the CU's DWARF
53 version. This is more overhead than what we're willing to
54 expand for a pretty rare case. */
55 addr = (unrelocated_addr) u.unsnd;
56 }
57 else
58 addr = u.addr;
59
60 return addr;
61}
62
63/* See attribute.h. */
64
65bool
66attribute::form_is_string () const
67{
68 return (form == DW_FORM_strp || form == DW_FORM_line_strp
69 || form == DW_FORM_string
70 || form == DW_FORM_strx
71 || form == DW_FORM_strx1
72 || form == DW_FORM_strx2
73 || form == DW_FORM_strx3
74 || form == DW_FORM_strx4
75 || form == DW_FORM_GNU_str_index
76 || form == DW_FORM_GNU_strp_alt
77 || form == DW_FORM_strp_sup);
78}
79
80/* See attribute.h. */
81
82const char *
83attribute::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
93bool
94attribute::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
106bool
107attribute::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
117bool
118attribute::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
137void
138attribute::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
146LONGEST
147attribute::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
168std::optional<ULONGEST>
169attribute::unsigned_constant () const
170{
171 if (form_is_strictly_signed ())
172 {
173 if (u.snd >= 0)
174 return u.snd;
175 complaint (_("Attribute value is not unsigned (%s)"),
176 dwarf_form_name (form));
177 }
178 else if (form_is_constant ())
179 return u.unsnd;
180
181 /* For DW_FORM_data16 see attribute::form_is_constant. */
182 complaint (_("Attribute value is not a constant (%s)"),
183 dwarf_form_name (form));
184 return {};
185}
186
187/* See attribute.h. */
188
189std::optional<LONGEST>
190attribute::signed_constant () const
191{
192 if (form_is_strictly_signed ())
193 return u.snd;
194
195 switch (form)
196 {
197 case DW_FORM_data8:
198 case DW_FORM_udata:
199 /* Not sure if DW_FORM_udata should be handled or not. Anyway
200 for DW_FORM_data8, there's no need to sign-extend. */
201 return u.snd;
202
203 case DW_FORM_data1:
204 return sign_extend (u.unsnd, 8);
205 case DW_FORM_data2:
206 return sign_extend (u.unsnd, 16);
207 case DW_FORM_data4:
208 return sign_extend (u.unsnd, 32);
209 }
210
211 /* For DW_FORM_data16 see attribute::form_is_constant. */
212 complaint (_("Attribute value is not a constant (%s)"),
213 dwarf_form_name (form));
214 return {};
215}
216
217/* See attribute.h. */
218
219std::optional<LONGEST>
220attribute::confused_constant () const
221{
222 if (form_is_strictly_signed ())
223 return u.snd;
224 else if (form_is_constant ())
225 return u.unsnd;
226
227 /* For DW_FORM_data16 see attribute::form_is_constant. */
228 complaint (_("Attribute value is not a constant (%s)"),
229 dwarf_form_name (form));
230 return {};
231}
232
233/* See attribute.h. */
234
235bool
236attribute::form_is_unsigned () const
237{
238 return (form == DW_FORM_ref_addr
239 || form == DW_FORM_GNU_ref_alt
240 || form == DW_FORM_ref_sup4
241 || form == DW_FORM_ref_sup8
242 || form == DW_FORM_data2
243 || form == DW_FORM_data4
244 || form == DW_FORM_data8
245 || form == DW_FORM_sec_offset
246 || form == DW_FORM_data1
247 || form == DW_FORM_flag
248 || form == DW_FORM_flag_present
249 || form == DW_FORM_udata
250 || form == DW_FORM_rnglistx
251 || form == DW_FORM_loclistx
252 || form == DW_FORM_ref1
253 || form == DW_FORM_ref2
254 || form == DW_FORM_ref4
255 || form == DW_FORM_ref8
256 || form == DW_FORM_ref_udata);
257}
258
259/* See attribute.h. */
260
261bool
262attribute::form_is_strictly_signed () const
263{
264 return form == DW_FORM_sdata || form == DW_FORM_implicit_const;
265}
266
267/* See attribute.h. */
268
269bool
270attribute::form_requires_reprocessing () const
271{
272 return (form == DW_FORM_strx
273 || form == DW_FORM_strx1
274 || form == DW_FORM_strx2
275 || form == DW_FORM_strx3
276 || form == DW_FORM_strx4
277 || form == DW_FORM_GNU_str_index
278 || form == DW_FORM_addrx
279 || form == DW_FORM_GNU_addr_index
280 || form == DW_FORM_rnglistx
281 || form == DW_FORM_loclistx);
282}
283
284/* See attribute.h. */
285
286dwarf_defaulted_attribute
287attribute::defaulted () const
288{
289 std::optional<ULONGEST> value = unsigned_constant ();
290
291 if (value.has_value ())
292 {
293 switch (*value)
294 {
295 case DW_DEFAULTED_no:
296 case DW_DEFAULTED_in_class:
297 case DW_DEFAULTED_out_of_class:
298 return (dwarf_defaulted_attribute) *value;
299
300 default:
301 complaint (_("unrecognized DW_AT_defaulted value (%s)"),
302 plongest (*value));
303 break;
304 }
305 }
306
307 return DW_DEFAULTED_no;
308}
309
310/* See attribute.h. */
311
312dwarf_virtuality_attribute
313attribute::as_virtuality () const
314{
315 std::optional<ULONGEST> value = unsigned_constant ();
316
317 if (value.has_value ())
318 {
319 switch (*value)
320 {
321 case DW_VIRTUALITY_none:
322 case DW_VIRTUALITY_virtual:
323 case DW_VIRTUALITY_pure_virtual:
324 return (dwarf_virtuality_attribute) *value;
325
326 default:
327 complaint (_("unrecognized DW_AT_virtuality value (%s)"),
328 plongest (*value));
329 break;
330 }
331 }
332
333 return DW_VIRTUALITY_none;
334}
335
336/* See attribute.h. */
337
338bool
339attribute::as_boolean () const
340{
341 if (form == DW_FORM_flag_present)
342 return true;
343 else if (form == DW_FORM_flag)
344 return u.unsnd != 0;
345 /* Using signed_constant here will work even for the weird case
346 where a negative value is provided. Probably doesn't matter but
347 also seems harmless. */
348 return signed_constant ().value_or (0) != 0;
349}
350