/src/elfutils/libdw/dwarf_aggregate_size.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Compute size of an aggregate type from DWARF. |
2 | | Copyright (C) 2010, 2014, 2016 Red Hat, Inc. |
3 | | Copyright (C) 2025, Mark J. Wielaard <mark@klomp.org> |
4 | | This file is part of elfutils. |
5 | | |
6 | | This file is free software; you can redistribute it and/or modify |
7 | | it under the terms of either |
8 | | |
9 | | * the GNU Lesser General Public License as published by the Free |
10 | | Software Foundation; either version 3 of the License, or (at |
11 | | your option) any later version |
12 | | |
13 | | or |
14 | | |
15 | | * the GNU General Public License as published by the Free |
16 | | Software Foundation; either version 2 of the License, or (at |
17 | | your option) any later version |
18 | | |
19 | | or both in parallel, as here. |
20 | | |
21 | | elfutils is distributed in the hope that it will be useful, but |
22 | | WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
24 | | General Public License for more details. |
25 | | |
26 | | You should have received copies of the GNU General Public License and |
27 | | the GNU Lesser General Public License along with this program. If |
28 | | not, see <http://www.gnu.org/licenses/>. */ |
29 | | |
30 | | #ifdef HAVE_CONFIG_H |
31 | | # include <config.h> |
32 | | #endif |
33 | | |
34 | | #include <dwarf.h> |
35 | | #include "libdwP.h" |
36 | | |
37 | | |
38 | | static Dwarf_Die * |
39 | | get_type (Dwarf_Die *die, Dwarf_Attribute *attr_mem, Dwarf_Die *type_mem) |
40 | 0 | { |
41 | 0 | Dwarf_Die *type = INTUSE(dwarf_formref_die) |
42 | 0 | (INTUSE(dwarf_attr_integrate) (die, DW_AT_type, attr_mem), type_mem); |
43 | |
|
44 | 0 | if (type == NULL || INTUSE(dwarf_peel_type) (type, type) != 0) |
45 | 0 | return NULL; |
46 | | |
47 | 0 | return type; |
48 | 0 | } |
49 | | |
50 | | static int aggregate_size (Dwarf_Die *die, Dwarf_Word *size, |
51 | | Dwarf_Die *type_mem, int depth); |
52 | | |
53 | | static int |
54 | | array_size (Dwarf_Die *die, Dwarf_Word *size, |
55 | | Dwarf_Attribute *attr_mem, int depth) |
56 | 0 | { |
57 | 0 | Dwarf_Word eltsize; |
58 | 0 | Dwarf_Die type_mem, aggregate_type_mem; |
59 | 0 | if (aggregate_size (get_type (die, attr_mem, &type_mem), &eltsize, |
60 | 0 | &aggregate_type_mem, depth) != 0) |
61 | 0 | return -1; |
62 | | |
63 | | /* An array can have DW_TAG_subrange_type or DW_TAG_enumeration_type |
64 | | children instead that give the size of each dimension. */ |
65 | | |
66 | 0 | Dwarf_Die child; |
67 | 0 | if (INTUSE(dwarf_child) (die, &child) != 0) |
68 | 0 | return -1; |
69 | | |
70 | 0 | bool any = false; |
71 | 0 | Dwarf_Word count_total = 1; |
72 | 0 | do |
73 | 0 | { |
74 | 0 | Dwarf_Word count; |
75 | 0 | switch (INTUSE(dwarf_tag) (&child)) |
76 | 0 | { |
77 | 0 | case DW_TAG_subrange_type: |
78 | | /* This has either DW_AT_count or DW_AT_upper_bound. */ |
79 | 0 | if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_count, |
80 | 0 | attr_mem) != NULL) |
81 | 0 | { |
82 | 0 | if (INTUSE(dwarf_formudata) (attr_mem, &count) != 0) |
83 | 0 | return -1; |
84 | 0 | } |
85 | 0 | else |
86 | 0 | { |
87 | 0 | bool is_signed = true; |
88 | 0 | if (INTUSE(dwarf_attr) (get_type (&child, attr_mem, &type_mem), |
89 | 0 | DW_AT_encoding, attr_mem) != NULL) |
90 | 0 | { |
91 | 0 | Dwarf_Word encoding; |
92 | 0 | if (INTUSE(dwarf_formudata) (attr_mem, &encoding) == 0) |
93 | 0 | is_signed = (encoding == DW_ATE_signed |
94 | 0 | || encoding == DW_ATE_signed_char); |
95 | 0 | } |
96 | |
|
97 | 0 | Dwarf_Sword upper; |
98 | 0 | Dwarf_Sword lower; |
99 | 0 | if (is_signed) |
100 | 0 | { |
101 | 0 | if (INTUSE(dwarf_formsdata) (INTUSE(dwarf_attr_integrate) |
102 | 0 | (&child, DW_AT_upper_bound, |
103 | 0 | attr_mem), &upper) != 0) |
104 | 0 | return -1; |
105 | 0 | } |
106 | 0 | else |
107 | 0 | { |
108 | 0 | Dwarf_Word unsigned_upper; |
109 | 0 | if (INTUSE(dwarf_formudata) (INTUSE(dwarf_attr_integrate) |
110 | 0 | (&child, DW_AT_upper_bound, |
111 | 0 | attr_mem), &unsigned_upper) != 0) |
112 | 0 | return -1; |
113 | 0 | upper = unsigned_upper; |
114 | 0 | } |
115 | | |
116 | | /* Having DW_AT_lower_bound is optional. */ |
117 | 0 | if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_lower_bound, |
118 | 0 | attr_mem) != NULL) |
119 | 0 | { |
120 | 0 | if (is_signed) |
121 | 0 | { |
122 | 0 | if (INTUSE(dwarf_formsdata) (attr_mem, &lower) != 0) |
123 | 0 | return -1; |
124 | 0 | } |
125 | 0 | else |
126 | 0 | { |
127 | 0 | Dwarf_Word unsigned_lower; |
128 | 0 | if (INTUSE(dwarf_formudata) (attr_mem, &unsigned_lower) != 0) |
129 | 0 | return -1; |
130 | 0 | lower = unsigned_lower; |
131 | 0 | } |
132 | 0 | } |
133 | 0 | else |
134 | 0 | { |
135 | 0 | Dwarf_Word lang; |
136 | 0 | Dwarf_Die cu = CUDIE (die->cu); |
137 | 0 | int res = INTUSE(dwarf_language) (&cu, &lang, NULL); |
138 | 0 | if (res < 0 |
139 | 0 | || INTUSE(dwarf_language_lower_bound) (lang, &lower) != 0) |
140 | 0 | return -1; |
141 | 0 | } |
142 | 0 | if (unlikely (lower > upper)) |
143 | 0 | return -1; |
144 | 0 | count = upper - lower + 1; |
145 | 0 | } |
146 | 0 | break; |
147 | | |
148 | 0 | case DW_TAG_enumeration_type: |
149 | | /* We have to find the DW_TAG_enumerator child with the |
150 | | highest value to know the array's element count. */ |
151 | 0 | count = 0; |
152 | 0 | Dwarf_Die enum_child; |
153 | 0 | int has_children = INTUSE(dwarf_child) (die, &enum_child); |
154 | 0 | if (has_children < 0) |
155 | 0 | return -1; |
156 | 0 | if (has_children > 0) |
157 | 0 | do |
158 | 0 | if (INTUSE(dwarf_tag) (&enum_child) == DW_TAG_enumerator) |
159 | 0 | { |
160 | 0 | Dwarf_Word value; |
161 | 0 | if (INTUSE(dwarf_formudata) (INTUSE(dwarf_attr_integrate) |
162 | 0 | (&enum_child, DW_AT_const_value, |
163 | 0 | attr_mem), &value) != 0) |
164 | 0 | return -1; |
165 | 0 | if (value >= count) |
166 | 0 | count = value + 1; |
167 | 0 | } |
168 | 0 | while (INTUSE(dwarf_siblingof) (&enum_child, &enum_child) > 0); |
169 | 0 | break; |
170 | | |
171 | 0 | default: |
172 | 0 | continue; |
173 | 0 | } |
174 | | |
175 | 0 | count_total *= count; |
176 | |
|
177 | 0 | any = true; |
178 | 0 | } |
179 | 0 | while (INTUSE(dwarf_siblingof) (&child, &child) == 0); |
180 | | |
181 | 0 | if (!any) |
182 | 0 | return -1; |
183 | | |
184 | | /* This is a subrange_type or enumeration_type and we've set COUNT. |
185 | | Now determine the stride for this array. */ |
186 | 0 | Dwarf_Word stride = eltsize; |
187 | 0 | if (INTUSE(dwarf_attr_integrate) (die, DW_AT_byte_stride, |
188 | 0 | attr_mem) != NULL) |
189 | 0 | { |
190 | 0 | if (INTUSE(dwarf_formudata) (attr_mem, &stride) != 0) |
191 | 0 | return -1; |
192 | 0 | } |
193 | 0 | else if (INTUSE(dwarf_attr_integrate) (die, DW_AT_bit_stride, |
194 | 0 | attr_mem) != NULL) |
195 | 0 | { |
196 | 0 | if (INTUSE(dwarf_formudata) (attr_mem, &stride) != 0) |
197 | 0 | return -1; |
198 | 0 | if (stride % 8) /* XXX maybe compute in bits? */ |
199 | 0 | return -1; |
200 | 0 | stride /= 8; |
201 | 0 | } |
202 | | |
203 | 0 | *size = count_total * stride; |
204 | 0 | return 0; |
205 | 0 | } |
206 | | |
207 | | static int |
208 | | aggregate_size (Dwarf_Die *die, Dwarf_Word *size, |
209 | | Dwarf_Die *type_mem, int depth) |
210 | 0 | { |
211 | 0 | Dwarf_Attribute attr_mem; |
212 | | |
213 | | /* Arrays of arrays of subrange types of arrays... Don't recurse too deep. */ |
214 | 0 | #define MAX_DEPTH 256 |
215 | 0 | if (die == NULL || depth++ >= MAX_DEPTH) |
216 | 0 | return -1; |
217 | | |
218 | 0 | if (INTUSE(dwarf_attr_integrate) (die, DW_AT_byte_size, &attr_mem) != NULL) |
219 | 0 | return INTUSE(dwarf_formudata) (&attr_mem, size); |
220 | | |
221 | 0 | switch (INTUSE(dwarf_tag) (die)) |
222 | 0 | { |
223 | 0 | case DW_TAG_subrange_type: |
224 | 0 | { |
225 | 0 | Dwarf_Die aggregate_type_mem; |
226 | 0 | return aggregate_size (get_type (die, &attr_mem, type_mem), |
227 | 0 | size, &aggregate_type_mem, depth); |
228 | 0 | } |
229 | | |
230 | 0 | case DW_TAG_array_type: |
231 | 0 | return array_size (die, size, &attr_mem, depth); |
232 | | |
233 | | /* Assume references and pointers have pointer size if not given an |
234 | | explicit DW_AT_byte_size. */ |
235 | 0 | case DW_TAG_pointer_type: |
236 | 0 | case DW_TAG_reference_type: |
237 | 0 | case DW_TAG_rvalue_reference_type: |
238 | 0 | *size = die->cu->address_size; |
239 | 0 | return 0; |
240 | 0 | } |
241 | | |
242 | | /* Most types must give their size directly. */ |
243 | 0 | return -1; |
244 | 0 | } |
245 | | |
246 | | NEW_VERSION (dwarf_aggregate_size, ELFUTILS_0.161) |
247 | | int |
248 | | dwarf_aggregate_size (Dwarf_Die *die, Dwarf_Word *size) |
249 | 0 | { |
250 | 0 | Dwarf_Die die_mem, type_mem; |
251 | |
|
252 | 0 | if (INTUSE (dwarf_peel_type) (die, &die_mem) != 0) |
253 | 0 | return -1; |
254 | | |
255 | 0 | return aggregate_size (&die_mem, size, &type_mem, 0); |
256 | 0 | } |
257 | | NEW_INTDEF (dwarf_aggregate_size) |
258 | | OLD_VERSION (dwarf_aggregate_size, ELFUTILS_0.144) |