5d9c559194aa20d8afe3efaefdab4a7a52abd461
[deliverable/binutils-gdb.git] / gdb / dwarf2 / attribute.c
1 /* DWARF attributes
2
3 Copyright (C) 1994-2020 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
35 attribute::as_address () const
36 {
37 CORE_ADDR addr;
38
39 if (form != DW_FORM_addr && form != DW_FORM_addrx
40 && form != DW_FORM_GNU_addr_index)
41 {
42 /* Aside from a few clearly defined exceptions, attributes that
43 contain an address must always be in DW_FORM_addr form.
44 Unfortunately, some compilers happen to be violating this
45 requirement by encoding addresses using other forms, such
46 as DW_FORM_data4 for example. For those broken compilers,
47 we try to do our best, without any guarantee of success,
48 to interpret the address correctly. It would also be nice
49 to generate a complaint, but that would require us to maintain
50 a list of legitimate cases where a non-address form is allowed,
51 as well as update callers to pass in at least the CU's DWARF
52 version. This is more overhead than what we're willing to
53 expand for a pretty rare case. */
54 addr = u.unsnd;
55 }
56 else
57 addr = u.addr;
58
59 return addr;
60 }
61
62 /* See attribute.h. */
63
64 bool
65 attribute::form_is_string () const
66 {
67 return (form == DW_FORM_strp || form == DW_FORM_line_strp
68 || form == DW_FORM_string
69 || form == DW_FORM_strx
70 || form == DW_FORM_strx1
71 || form == DW_FORM_strx2
72 || form == DW_FORM_strx3
73 || form == DW_FORM_strx4
74 || form == DW_FORM_GNU_str_index
75 || form == DW_FORM_GNU_strp_alt);
76 }
77
78 /* See attribute.h. */
79
80 const char *
81 attribute::as_string () const
82 {
83 if (form_is_string ())
84 return u.str;
85 return nullptr;
86 }
87
88 /* See attribute.h. */
89
90 bool
91 attribute::form_is_block () const
92 {
93 return (form == DW_FORM_block1
94 || form == DW_FORM_block2
95 || form == DW_FORM_block4
96 || form == DW_FORM_block
97 || form == DW_FORM_exprloc
98 || form == DW_FORM_data16);
99 }
100
101 /* See attribute.h. */
102
103 bool
104 attribute::form_is_section_offset () const
105 {
106 return (form == DW_FORM_data4
107 || form == DW_FORM_data8
108 || form == DW_FORM_sec_offset
109 || form == DW_FORM_loclistx);
110 }
111
112 /* See attribute.h. */
113
114 bool
115 attribute::form_is_constant () const
116 {
117 switch (form)
118 {
119 case DW_FORM_sdata:
120 case DW_FORM_udata:
121 case DW_FORM_data1:
122 case DW_FORM_data2:
123 case DW_FORM_data4:
124 case DW_FORM_data8:
125 case DW_FORM_implicit_const:
126 return true;
127 default:
128 return false;
129 }
130 }
131
132 /* See attribute.h. */
133
134 void
135 attribute::get_ref_die_offset_complaint () const
136 {
137 complaint (_("unsupported die ref attribute form: '%s'"),
138 dwarf_form_name (form));
139 }
140
141 /* See attribute.h. */
142
143 LONGEST
144 attribute::constant_value (int default_value) const
145 {
146 if (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
147 return u.snd;
148 else if (form == DW_FORM_udata
149 || form == DW_FORM_data1
150 || form == DW_FORM_data2
151 || form == DW_FORM_data4
152 || form == DW_FORM_data8)
153 return u.unsnd;
154 else
155 {
156 /* For DW_FORM_data16 see attribute::form_is_constant. */
157 complaint (_("Attribute value is not a constant (%s)"),
158 dwarf_form_name (form));
159 return default_value;
160 }
161 }
162
163 /* See attribute.h. */
164
165 bool
166 attribute::form_is_unsigned () const
167 {
168 return (form == DW_FORM_ref_addr
169 || form == DW_FORM_GNU_ref_alt
170 || form == DW_FORM_data2
171 || form == DW_FORM_data4
172 || form == DW_FORM_data8
173 || form == DW_FORM_sec_offset
174 || form == DW_FORM_data1
175 || form == DW_FORM_flag
176 || form == DW_FORM_flag_present
177 || form == DW_FORM_udata
178 || form == DW_FORM_rnglistx
179 || form == DW_FORM_ref1
180 || form == DW_FORM_ref2
181 || form == DW_FORM_ref4
182 || form == DW_FORM_ref8
183 || form == DW_FORM_ref_udata);
184 }
185
186 /* See attribute.h. */
187
188 bool
189 attribute::form_requires_reprocessing () const
190 {
191 return (form == DW_FORM_strx1
192 || form == DW_FORM_strx2
193 || form == DW_FORM_strx3
194 || form == DW_FORM_strx4
195 || form == DW_FORM_GNU_str_index
196 || form == DW_FORM_addrx
197 || form == DW_FORM_GNU_addr_index);
198 }
This page took 0.036928 seconds and 4 git commands to generate.