07c53a22368d196f7aa4e93845da5cd9ccc11b46
[platform/upstream/elfutils.git] / libdw / dwarf_aggregate_size.c
1 /* Compute size of an aggregate type from DWARF.
2    Copyright (C) 2010 Red Hat, Inc.
3    This file is part of elfutils.
4
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of either
7
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
11
12    or
13
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
17
18    or both in parallel, as here.
19
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.
24
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/>.  */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include <dwarf.h>
34 #include "libdwP.h"
35
36
37 static Dwarf_Die *
38 get_type (Dwarf_Die *die, Dwarf_Attribute *attr_mem, Dwarf_Die *type_mem)
39 {
40   return INTUSE(dwarf_formref_die)
41     (INTUSE(dwarf_attr_integrate) (die, DW_AT_type, attr_mem), type_mem);
42 }
43
44 static int
45 array_size (Dwarf_Die *die, Dwarf_Word *size,
46             Dwarf_Attribute *attr_mem, Dwarf_Die *type_mem)
47 {
48   Dwarf_Word eltsize;
49   if (INTUSE(dwarf_aggregate_size) (get_type (die, attr_mem, type_mem),
50                                     &eltsize) != 0)
51       return -1;
52
53   /* An array can have DW_TAG_subrange_type or DW_TAG_enumeration_type
54      children instead that give the size of each dimension.  */
55
56   Dwarf_Die child;
57   if (INTUSE(dwarf_child) (die, &child) != 0)
58     return -1;
59
60   bool any = false;
61   Dwarf_Word total = 0;
62   do
63     {
64       Dwarf_Word count;
65       switch (INTUSE(dwarf_tag) (&child))
66         {
67         case DW_TAG_subrange_type:
68           /* This has either DW_AT_count or DW_AT_upper_bound.  */
69           if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_count,
70                                             attr_mem) != NULL)
71             {
72               if (INTUSE(dwarf_formudata) (attr_mem, &count) != 0)
73                 return -1;
74             }
75           else
76             {
77               Dwarf_Sword upper;
78               Dwarf_Sword lower;
79               if (INTUSE(dwarf_formsdata) (INTUSE(dwarf_attr_integrate)
80                                            (&child, DW_AT_upper_bound,
81                                             attr_mem), &upper) != 0)
82                 return -1;
83
84               /* Having DW_AT_lower_bound is optional.  */
85               if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_lower_bound,
86                                                 attr_mem) != NULL)
87                 {
88                   if (INTUSE(dwarf_formsdata) (attr_mem, &lower) != 0)
89                     return -1;
90                 }
91               else
92                 {
93                   /* Determine default lower bound from language,
94                      as per "4.12 Subrange Type Entries".  */
95                   Dwarf_Die cu = CUDIE (die->cu);
96                   switch (INTUSE(dwarf_srclang) (&cu))
97                     {
98                     case DW_LANG_C:
99                     case DW_LANG_C89:
100                     case DW_LANG_C99:
101                     case DW_LANG_C_plus_plus:
102                     case DW_LANG_ObjC:
103                     case DW_LANG_ObjC_plus_plus:
104                     case DW_LANG_Java:
105                     case DW_LANG_D:
106                     case DW_LANG_UPC:
107                       lower = 0;
108                       break;
109
110                     case DW_LANG_Ada83:
111                     case DW_LANG_Ada95:
112                     case DW_LANG_Cobol74:
113                     case DW_LANG_Cobol85:
114                     case DW_LANG_Fortran77:
115                     case DW_LANG_Fortran90:
116                     case DW_LANG_Fortran95:
117                     case DW_LANG_Pascal83:
118                     case DW_LANG_Modula2:
119                     case DW_LANG_PL1:
120                       lower = 1;
121                       break;
122
123                     default:
124                       return -1;
125                     }
126                 }
127               if (unlikely (lower > upper))
128                 return -1;
129               count = upper - lower + 1;
130             }
131           break;
132
133         case DW_TAG_enumeration_type:
134           /* We have to find the DW_TAG_enumerator child with the
135              highest value to know the array's element count.  */
136           count = 0;
137           Dwarf_Die enum_child;
138           int has_children = INTUSE(dwarf_child) (die, &enum_child);
139           if (has_children < 0)
140             return -1;
141           if (has_children > 0)
142             do
143               if (INTUSE(dwarf_tag) (&enum_child) == DW_TAG_enumerator)
144                 {
145                   Dwarf_Word value;
146                   if (INTUSE(dwarf_formudata) (INTUSE(dwarf_attr_integrate)
147                                                (&enum_child, DW_AT_const_value,
148                                                 attr_mem), &value) != 0)
149                     return -1;
150                   if (value >= count)
151                     count = value + 1;
152                 }
153             while (INTUSE(dwarf_siblingof) (&enum_child, &enum_child) > 0);
154           break;
155
156         default:
157           continue;
158         }
159
160       /* This is a subrange_type or enumeration_type and we've set COUNT.
161          Now determine the stride for this array dimension.  */
162       Dwarf_Word stride = eltsize;
163       if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_byte_stride,
164                                         attr_mem) != NULL)
165         {
166           if (INTUSE(dwarf_formudata) (attr_mem, &stride) != 0)
167             return -1;
168         }
169       else if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_bit_stride,
170                                              attr_mem) != NULL)
171         {
172           if (INTUSE(dwarf_formudata) (attr_mem, &stride) != 0)
173             return -1;
174           if (stride % 8)       /* XXX maybe compute in bits? */
175             return -1;
176           stride /= 8;
177         }
178
179       any = true;
180       total += stride * count;
181     }
182   while (INTUSE(dwarf_siblingof) (&child, &child) == 0);
183
184   if (!any)
185     return -1;
186
187   *size = total;
188   return 0;
189 }
190
191 static int
192 aggregate_size (Dwarf_Die *die, Dwarf_Word *size, Dwarf_Die *type_mem)
193 {
194   Dwarf_Attribute attr_mem;
195
196   if (INTUSE(dwarf_attr_integrate) (die, DW_AT_byte_size, &attr_mem) != NULL)
197     return INTUSE(dwarf_formudata) (&attr_mem, size);
198
199   switch (INTUSE(dwarf_tag) (die))
200     {
201     case DW_TAG_typedef:
202     case DW_TAG_subrange_type:
203       return aggregate_size (get_type (die, &attr_mem, type_mem),
204                              size, type_mem); /* Tail call.  */
205
206     case DW_TAG_array_type:
207       return array_size (die, size, &attr_mem, type_mem);
208     }
209
210   /* Most types must give their size directly.  */
211   return -1;
212 }
213
214 int
215 dwarf_aggregate_size (die, size)
216      Dwarf_Die *die;
217      Dwarf_Word *size;
218 {
219   Dwarf_Die type_mem;
220   return aggregate_size (die, size, &type_mem);
221 }
222 INTDEF (dwarf_aggregate_size)