Fix two latent Rust bugs
[deliverable/binutils-gdb.git] / gdb / dwarf2 / attribute.c
CommitLineData
162dce55
TT
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"
0826b30a
TT
29#include "dwarf2/stringify.h"
30#include "complaints.h"
162dce55
TT
31
32/* See attribute.h. */
33
34CORE_ADDR
cd6c91b4 35attribute::value_as_address () const
162dce55
TT
36{
37 CORE_ADDR addr;
38
cd6c91b4
TT
39 if (form != DW_FORM_addr && form != DW_FORM_addrx
40 && form != DW_FORM_GNU_addr_index)
162dce55
TT
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. */
cd6c91b4 54 addr = DW_UNSND (this);
162dce55
TT
55 }
56 else
cd6c91b4 57 addr = DW_ADDR (this);
162dce55
TT
58
59 return addr;
60}
61
62/* See attribute.h. */
63
4fc6c0d5
TT
64bool
65attribute::form_is_block () const
162dce55 66{
4fc6c0d5
TT
67 return (form == DW_FORM_block1
68 || form == DW_FORM_block2
69 || form == DW_FORM_block4
70 || form == DW_FORM_block
71 || form == DW_FORM_exprloc);
162dce55
TT
72}
73
74/* See attribute.h. */
75
cd6c91b4
TT
76bool
77attribute::form_is_section_offset () const
162dce55 78{
cd6c91b4
TT
79 return (form == DW_FORM_data4
80 || form == DW_FORM_data8
41144253 81 || form == DW_FORM_sec_offset
82 || form == DW_FORM_loclistx);
162dce55
TT
83}
84
85/* See attribute.h. */
86
cd6c91b4
TT
87bool
88attribute::form_is_constant () const
162dce55 89{
cd6c91b4 90 switch (form)
162dce55
TT
91 {
92 case DW_FORM_sdata:
93 case DW_FORM_udata:
94 case DW_FORM_data1:
95 case DW_FORM_data2:
96 case DW_FORM_data4:
97 case DW_FORM_data8:
98 case DW_FORM_implicit_const:
cd6c91b4 99 return true;
162dce55 100 default:
cd6c91b4 101 return false;
162dce55
TT
102 }
103}
104
105/* DW_ADDR is always stored already as sect_offset; despite for the forms
106 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
107
cd6c91b4
TT
108bool
109attribute::form_is_ref () const
162dce55 110{
cd6c91b4 111 switch (form)
162dce55
TT
112 {
113 case DW_FORM_ref_addr:
114 case DW_FORM_ref1:
115 case DW_FORM_ref2:
116 case DW_FORM_ref4:
117 case DW_FORM_ref8:
118 case DW_FORM_ref_udata:
119 case DW_FORM_GNU_ref_alt:
cd6c91b4 120 return true;
162dce55 121 default:
cd6c91b4 122 return false;
162dce55
TT
123 }
124}
0826b30a
TT
125
126/* See attribute.h. */
127
128sect_offset
129attribute::get_ref_die_offset () const
130{
131 if (form_is_ref ())
132 return (sect_offset) DW_UNSND (this);
133
134 complaint (_("unsupported die ref attribute form: '%s'"),
135 dwarf_form_name (form));
136 return {};
137}
138
139/* See attribute.h. */
140
141LONGEST
142attribute::constant_value (int default_value) const
143{
144 if (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
145 return DW_SND (this);
146 else if (form == DW_FORM_udata
147 || form == DW_FORM_data1
148 || form == DW_FORM_data2
149 || form == DW_FORM_data4
150 || form == DW_FORM_data8)
151 return DW_UNSND (this);
152 else
153 {
154 /* For DW_FORM_data16 see attribute::form_is_constant. */
155 complaint (_("Attribute value is not a constant (%s)"),
156 dwarf_form_name (form));
157 return default_value;
158 }
159}
This page took 0.052523 seconds and 4 git commands to generate.