1 /* Compute size of an aggregate type from DWARF.
2 Copyright (C) 2010, 2014 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
38 get_type (Dwarf_Die *die, Dwarf_Attribute *attr_mem, Dwarf_Die *type_mem)
40 Dwarf_Die *type = INTUSE(dwarf_formref_die)
41 (INTUSE(dwarf_attr_integrate) (die, DW_AT_type, attr_mem), type_mem);
43 if (INTUSE(dwarf_peel_type) (type, type) != 0)
50 array_size (Dwarf_Die *die, Dwarf_Word *size,
51 Dwarf_Attribute *attr_mem, Dwarf_Die *type_mem)
54 if (INTUSE(dwarf_aggregate_size) (get_type (die, attr_mem, type_mem),
58 /* An array can have DW_TAG_subrange_type or DW_TAG_enumeration_type
59 children instead that give the size of each dimension. */
62 if (INTUSE(dwarf_child) (die, &child) != 0)
70 switch (INTUSE(dwarf_tag) (&child))
72 case DW_TAG_subrange_type:
73 /* This has either DW_AT_count or DW_AT_upper_bound. */
74 if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_count,
77 if (INTUSE(dwarf_formudata) (attr_mem, &count) != 0)
84 if (INTUSE(dwarf_formsdata) (INTUSE(dwarf_attr_integrate)
85 (&child, DW_AT_upper_bound,
86 attr_mem), &upper) != 0)
89 /* Having DW_AT_lower_bound is optional. */
90 if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_lower_bound,
93 if (INTUSE(dwarf_formsdata) (attr_mem, &lower) != 0)
98 /* Determine default lower bound from language,
99 as per "4.12 Subrange Type Entries". */
100 Dwarf_Die cu = CUDIE (die->cu);
101 switch (INTUSE(dwarf_srclang) (&cu))
107 case DW_LANG_C_plus_plus:
108 case DW_LANG_C_plus_plus_11:
109 case DW_LANG_C_plus_plus_14:
111 case DW_LANG_ObjC_plus_plus:
121 case DW_LANG_Cobol74:
122 case DW_LANG_Cobol85:
123 case DW_LANG_Fortran77:
124 case DW_LANG_Fortran90:
125 case DW_LANG_Fortran95:
126 case DW_LANG_Fortran03:
127 case DW_LANG_Fortran08:
128 case DW_LANG_Pascal83:
129 case DW_LANG_Modula2:
138 if (unlikely (lower > upper))
140 count = upper - lower + 1;
144 case DW_TAG_enumeration_type:
145 /* We have to find the DW_TAG_enumerator child with the
146 highest value to know the array's element count. */
148 Dwarf_Die enum_child;
149 int has_children = INTUSE(dwarf_child) (die, &enum_child);
150 if (has_children < 0)
152 if (has_children > 0)
154 if (INTUSE(dwarf_tag) (&enum_child) == DW_TAG_enumerator)
157 if (INTUSE(dwarf_formudata) (INTUSE(dwarf_attr_integrate)
158 (&enum_child, DW_AT_const_value,
159 attr_mem), &value) != 0)
164 while (INTUSE(dwarf_siblingof) (&enum_child, &enum_child) > 0);
171 /* This is a subrange_type or enumeration_type and we've set COUNT.
172 Now determine the stride for this array dimension. */
173 Dwarf_Word stride = eltsize;
174 if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_byte_stride,
177 if (INTUSE(dwarf_formudata) (attr_mem, &stride) != 0)
180 else if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_bit_stride,
183 if (INTUSE(dwarf_formudata) (attr_mem, &stride) != 0)
185 if (stride % 8) /* XXX maybe compute in bits? */
191 total += stride * count;
193 while (INTUSE(dwarf_siblingof) (&child, &child) == 0);
203 aggregate_size (Dwarf_Die *die, Dwarf_Word *size, Dwarf_Die *type_mem)
205 Dwarf_Attribute attr_mem;
207 if (INTUSE(dwarf_attr_integrate) (die, DW_AT_byte_size, &attr_mem) != NULL)
208 return INTUSE(dwarf_formudata) (&attr_mem, size);
210 switch (INTUSE(dwarf_tag) (die))
212 case DW_TAG_subrange_type:
213 return aggregate_size (get_type (die, &attr_mem, type_mem),
214 size, type_mem); /* Tail call. */
216 case DW_TAG_array_type:
217 return array_size (die, size, &attr_mem, type_mem);
219 /* Assume references and pointers have pointer size if not given an
220 explicit DW_AT_byte_size. */
221 case DW_TAG_pointer_type:
222 case DW_TAG_reference_type:
223 case DW_TAG_rvalue_reference_type:
224 *size = die->cu->address_size;
228 /* Most types must give their size directly. */
233 dwarf_aggregate_size (Dwarf_Die *die, Dwarf_Word *size)
237 if (INTUSE (dwarf_peel_type) (die, die) != 0)
240 return aggregate_size (die, size, &type_mem);
242 INTDEF (dwarf_aggregate_size)
243 OLD_VERSION (dwarf_aggregate_size, ELFUTILS_0.144)
244 NEW_VERSION (dwarf_aggregate_size, ELFUTILS_0.161)