dwarf.c revision 1.26.8.1 1 1.1 darran /*
2 1.1 darran * CDDL HEADER START
3 1.1 darran *
4 1.1 darran * The contents of this file are subject to the terms of the
5 1.1 darran * Common Development and Distribution License (the "License").
6 1.1 darran * You may not use this file except in compliance with the License.
7 1.1 darran *
8 1.1 darran * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 1.1 darran * or http://www.opensolaris.org/os/licensing.
10 1.1 darran * See the License for the specific language governing permissions
11 1.1 darran * and limitations under the License.
12 1.1 darran *
13 1.1 darran * When distributing Covered Code, include this CDDL HEADER in each
14 1.1 darran * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 1.1 darran * If applicable, add the following below this CDDL HEADER, with the
16 1.1 darran * fields enclosed by brackets "[]" replaced with your own identifying
17 1.1 darran * information: Portions Copyright [yyyy] [name of copyright owner]
18 1.1 darran *
19 1.1 darran * CDDL HEADER END
20 1.1 darran */
21 1.1 darran /*
22 1.1 darran * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 1.1 darran * Use is subject to license terms.
24 1.1 darran */
25 1.1 darran
26 1.1 darran /*
27 1.1 darran * DWARF to tdata conversion
28 1.1 darran *
29 1.1 darran * For the most part, conversion is straightforward, proceeding in two passes.
30 1.1 darran * On the first pass, we iterate through every die, creating new type nodes as
31 1.1 darran * necessary. Referenced tdesc_t's are created in an uninitialized state, thus
32 1.1 darran * allowing type reference pointers to be filled in. If the tdesc_t
33 1.1 darran * corresponding to a given die can be completely filled out (sizes and offsets
34 1.1 darran * calculated, and so forth) without using any referenced types, the tdesc_t is
35 1.1 darran * marked as resolved. Consider an array type. If the type corresponding to
36 1.1 darran * the array contents has not yet been processed, we will create a blank tdesc
37 1.1 darran * for the contents type (only the type ID will be filled in, relying upon the
38 1.1 darran * later portion of the first pass to encounter and complete the referenced
39 1.1 darran * type). We will then attempt to determine the size of the array. If the
40 1.1 darran * array has a byte size attribute, we will have completely characterized the
41 1.1 darran * array type, and will be able to mark it as resolved. The lack of a byte
42 1.1 darran * size attribute, on the other hand, will prevent us from fully resolving the
43 1.1 darran * type, as the size will only be calculable with reference to the contents
44 1.1 darran * type, which has not, as yet, been encountered. The array type will thus be
45 1.1 darran * left without the resolved flag, and the first pass will continue.
46 1.1 darran *
47 1.1 darran * When we begin the second pass, we will have created tdesc_t nodes for every
48 1.1 darran * type in the section. We will traverse the tree, from the iidescs down,
49 1.1 darran * processing each unresolved node. As the referenced nodes will have been
50 1.1 darran * populated, the array type used in our example above will be able to use the
51 1.1 darran * size of the referenced types (if available) to determine its own type. The
52 1.1 darran * traversal will be repeated until all types have been resolved or we have
53 1.1 darran * failed to make progress. When all tdescs have been resolved, the conversion
54 1.1 darran * is complete.
55 1.1 darran *
56 1.1 darran * There are, as always, a few special cases that are handled during the first
57 1.1 darran * and second passes:
58 1.1 darran *
59 1.1 darran * 1. Empty enums - GCC will occasionally emit an enum without any members.
60 1.1 darran * Later on in the file, it will emit the same enum type, though this time
61 1.1 darran * with the full complement of members. All references to the memberless
62 1.1 darran * enum need to be redirected to the full definition. During the first
63 1.1 darran * pass, each enum is entered in dm_enumhash, along with a pointer to its
64 1.1 darran * corresponding tdesc_t. If, during the second pass, we encounter a
65 1.1 darran * memberless enum, we use the hash to locate the full definition. All
66 1.1 darran * tdescs referencing the empty enum are then redirected.
67 1.1 darran *
68 1.1 darran * 2. Forward declarations - If the compiler sees a forward declaration for
69 1.1 darran * a structure, followed by the definition of that structure, it will emit
70 1.1 darran * DWARF data for both the forward declaration and the definition. We need
71 1.1 darran * to resolve the forward declarations when possible, by redirecting
72 1.1 darran * forward-referencing tdescs to the actual struct/union definitions. This
73 1.1 darran * redirection is done completely within the first pass. We begin by
74 1.1 darran * recording all forward declarations in dw_fwdhash. When we define a
75 1.1 darran * structure, we check to see if there have been any corresponding forward
76 1.1 darran * declarations. If so, we redirect the tdescs which referenced the forward
77 1.1 darran * declarations to the structure or union definition.
78 1.1 darran *
79 1.1 darran * XXX see if a post traverser will allow the elimination of repeated pass 2
80 1.1 darran * traversals.
81 1.1 darran */
82 1.1 darran
83 1.4 darran #if HAVE_NBTOOL_CONFIG_H
84 1.4 darran # include "nbtool_config.h"
85 1.4 darran #endif
86 1.4 darran
87 1.1 darran #include <stdio.h>
88 1.1 darran #include <stdlib.h>
89 1.2 darran #include <string.h>
90 1.1 darran #include <strings.h>
91 1.1 darran #include <errno.h>
92 1.1 darran #include <libelf.h>
93 1.1 darran #include <libdwarf.h>
94 1.1 darran #include <libgen.h>
95 1.1 darran #include <dwarf.h>
96 1.1 darran
97 1.1 darran #include "ctf_headers.h"
98 1.1 darran #include "ctftools.h"
99 1.1 darran #include "memory.h"
100 1.1 darran #include "list.h"
101 1.1 darran #include "traverse.h"
102 1.1 darran
103 1.1 darran /*
104 1.1 darran * We need to define a couple of our own intrinsics, to smooth out some of the
105 1.1 darran * differences between the GCC and DevPro DWARF emitters. See the referenced
106 1.1 darran * routines and the special cases in the file comment for more details.
107 1.1 darran *
108 1.1 darran * Type IDs are 32 bits wide. We're going to use the top of that field to
109 1.1 darran * indicate types that we've created ourselves.
110 1.1 darran */
111 1.1 darran #define TID_FILEMAX 0x3fffffff /* highest tid from file */
112 1.1 darran #define TID_VOID 0x40000001 /* see die_void() */
113 1.1 darran #define TID_LONG 0x40000002 /* see die_array() */
114 1.1 darran
115 1.1 darran #define TID_MFGTID_BASE 0x40000003 /* first mfg'd tid */
116 1.1 darran
117 1.1 darran /*
118 1.1 darran * To reduce the staggering amount of error-handling code that would otherwise
119 1.1 darran * be required, the attribute-retrieval routines handle most of their own
120 1.1 darran * errors. If the following flag is supplied as the value of the `req'
121 1.1 darran * argument, they will also handle the absence of a requested attribute by
122 1.1 darran * terminating the program.
123 1.1 darran */
124 1.1 darran #define DW_ATTR_REQ 1
125 1.1 darran
126 1.1 darran #define TDESC_HASH_BUCKETS 511
127 1.1 darran
128 1.1 darran typedef struct dwarf {
129 1.1 darran Dwarf_Debug dw_dw; /* for libdwarf */
130 1.1 darran Dwarf_Error dw_err; /* for libdwarf */
131 1.2 darran Dwarf_Off dw_maxoff; /* highest legal offset in this cu */
132 1.1 darran tdata_t *dw_td; /* root of the tdesc/iidesc tree */
133 1.1 darran hash_t *dw_tidhash; /* hash of tdescs by t_id */
134 1.1 darran hash_t *dw_fwdhash; /* hash of fwd decls by name */
135 1.1 darran hash_t *dw_enumhash; /* hash of memberless enums by name */
136 1.1 darran tdesc_t *dw_void; /* manufactured void type */
137 1.1 darran tdesc_t *dw_long; /* manufactured long type for arrays */
138 1.1 darran size_t dw_ptrsz; /* size of a pointer in this file */
139 1.1 darran tid_t dw_mfgtid_last; /* last mfg'd type ID used */
140 1.1 darran uint_t dw_nunres; /* count of unresolved types */
141 1.1 darran char *dw_cuname; /* name of compilation unit */
142 1.1 darran } dwarf_t;
143 1.1 darran
144 1.1 darran static void die_create_one(dwarf_t *, Dwarf_Die);
145 1.1 darran static void die_create(dwarf_t *, Dwarf_Die);
146 1.1 darran
147 1.1 darran static tid_t
148 1.1 darran mfgtid_next(dwarf_t *dw)
149 1.1 darran {
150 1.1 darran return (++dw->dw_mfgtid_last);
151 1.1 darran }
152 1.1 darran
153 1.1 darran static void
154 1.1 darran tdesc_add(dwarf_t *dw, tdesc_t *tdp)
155 1.1 darran {
156 1.1 darran hash_add(dw->dw_tidhash, tdp);
157 1.1 darran }
158 1.1 darran
159 1.1 darran static tdesc_t *
160 1.1 darran tdesc_lookup(dwarf_t *dw, int tid)
161 1.1 darran {
162 1.2 darran tdesc_t tmpl;
163 1.2 darran void *tdp;
164 1.1 darran
165 1.1 darran tmpl.t_id = tid;
166 1.1 darran
167 1.2 darran if (hash_find(dw->dw_tidhash, &tmpl, &tdp))
168 1.1 darran return (tdp);
169 1.1 darran else
170 1.1 darran return (NULL);
171 1.1 darran }
172 1.1 darran
173 1.1 darran /*
174 1.1 darran * Resolve a tdesc down to a node which should have a size. Returns the size,
175 1.1 darran * zero if the size hasn't yet been determined.
176 1.1 darran */
177 1.1 darran static size_t
178 1.1 darran tdesc_size(tdesc_t *tdp)
179 1.1 darran {
180 1.1 darran for (;;) {
181 1.1 darran switch (tdp->t_type) {
182 1.1 darran case INTRINSIC:
183 1.1 darran case POINTER:
184 1.20 christos case REFERENCE:
185 1.1 darran case ARRAY:
186 1.1 darran case FUNCTION:
187 1.1 darran case STRUCT:
188 1.1 darran case UNION:
189 1.22 christos case CLASS:
190 1.1 darran case ENUM:
191 1.1 darran return (tdp->t_size);
192 1.1 darran
193 1.1 darran case FORWARD:
194 1.20 christos debug(3, "type is forward for %#x\n", tdp->t_id);
195 1.1 darran return (0);
196 1.1 darran
197 1.1 darran case TYPEDEF:
198 1.1 darran case VOLATILE:
199 1.1 darran case CONST:
200 1.1 darran case RESTRICT:
201 1.1 darran tdp = tdp->t_tdesc;
202 1.1 darran continue;
203 1.1 darran
204 1.1 darran case 0: /* not yet defined */
205 1.20 christos debug(3, "type is undefined for %#x\n", tdp->t_id);
206 1.1 darran return (0);
207 1.1 darran
208 1.1 darran default:
209 1.20 christos terminate("tdp %u: tdesc_size on unknown type %#x\n",
210 1.1 darran tdp->t_id, tdp->t_type);
211 1.1 darran }
212 1.1 darran }
213 1.1 darran }
214 1.1 darran
215 1.1 darran static size_t
216 1.1 darran tdesc_bitsize(tdesc_t *tdp)
217 1.1 darran {
218 1.1 darran for (;;) {
219 1.1 darran switch (tdp->t_type) {
220 1.1 darran case INTRINSIC:
221 1.1 darran return (tdp->t_intr->intr_nbits);
222 1.1 darran
223 1.1 darran case ARRAY:
224 1.1 darran case FUNCTION:
225 1.1 darran case STRUCT:
226 1.1 darran case UNION:
227 1.22 christos case CLASS:
228 1.1 darran case ENUM:
229 1.1 darran case POINTER:
230 1.20 christos case REFERENCE:
231 1.1 darran return (tdp->t_size * NBBY);
232 1.1 darran
233 1.1 darran case FORWARD:
234 1.20 christos debug(3, "bitsize is forward for %d\n", tdp->t_id);
235 1.1 darran return (0);
236 1.1 darran
237 1.1 darran case TYPEDEF:
238 1.1 darran case VOLATILE:
239 1.1 darran case RESTRICT:
240 1.1 darran case CONST:
241 1.1 darran tdp = tdp->t_tdesc;
242 1.1 darran continue;
243 1.1 darran
244 1.1 darran case 0: /* not yet defined */
245 1.20 christos debug(3, "bitsize is undefined for %d\n", tdp->t_id);
246 1.1 darran return (0);
247 1.1 darran
248 1.1 darran default:
249 1.1 darran terminate("tdp %u: tdesc_bitsize on unknown type %d\n",
250 1.1 darran tdp->t_id, tdp->t_type);
251 1.1 darran }
252 1.1 darran }
253 1.1 darran }
254 1.1 darran
255 1.1 darran static tdesc_t *
256 1.1 darran tdesc_basetype(tdesc_t *tdp)
257 1.1 darran {
258 1.1 darran for (;;) {
259 1.1 darran switch (tdp->t_type) {
260 1.1 darran case TYPEDEF:
261 1.1 darran case VOLATILE:
262 1.1 darran case RESTRICT:
263 1.1 darran case CONST:
264 1.1 darran tdp = tdp->t_tdesc;
265 1.1 darran break;
266 1.1 darran case 0: /* not yet defined */
267 1.1 darran return (NULL);
268 1.1 darran default:
269 1.1 darran return (tdp);
270 1.1 darran }
271 1.1 darran }
272 1.1 darran }
273 1.1 darran
274 1.1 darran static Dwarf_Off
275 1.1 darran die_off(dwarf_t *dw, Dwarf_Die die)
276 1.1 darran {
277 1.1 darran Dwarf_Off off;
278 1.1 darran
279 1.1 darran if (dwarf_dieoffset(die, &off, &dw->dw_err) == DW_DLV_OK)
280 1.1 darran return (off);
281 1.1 darran
282 1.1 darran terminate("failed to get offset for die: %s\n",
283 1.8 christos dwarf_errmsg(dw->dw_err));
284 1.1 darran /*NOTREACHED*/
285 1.1 darran return (0);
286 1.1 darran }
287 1.1 darran
288 1.1 darran static Dwarf_Die
289 1.1 darran die_sibling(dwarf_t *dw, Dwarf_Die die)
290 1.1 darran {
291 1.1 darran Dwarf_Die sib;
292 1.1 darran int rc;
293 1.1 darran
294 1.1 darran if ((rc = dwarf_siblingof(dw->dw_dw, die, &sib, &dw->dw_err)) ==
295 1.1 darran DW_DLV_OK)
296 1.1 darran return (sib);
297 1.1 darran else if (rc == DW_DLV_NO_ENTRY)
298 1.1 darran return (NULL);
299 1.1 darran
300 1.12 christos terminate("die %ju: failed to find type sibling: %s\n",
301 1.12 christos (uintmax_t)die_off(dw, die), dwarf_errmsg(dw->dw_err));
302 1.1 darran /*NOTREACHED*/
303 1.1 darran return (NULL);
304 1.1 darran }
305 1.1 darran
306 1.1 darran static Dwarf_Die
307 1.1 darran die_child(dwarf_t *dw, Dwarf_Die die)
308 1.1 darran {
309 1.1 darran Dwarf_Die child;
310 1.1 darran int rc;
311 1.1 darran
312 1.1 darran if ((rc = dwarf_child(die, &child, &dw->dw_err)) == DW_DLV_OK)
313 1.1 darran return (child);
314 1.1 darran else if (rc == DW_DLV_NO_ENTRY)
315 1.1 darran return (NULL);
316 1.1 darran
317 1.12 christos terminate("die %ju: failed to find type child: %s\n",
318 1.12 christos (uintmax_t)die_off(dw, die), dwarf_errmsg(dw->dw_err));
319 1.1 darran /*NOTREACHED*/
320 1.1 darran return (NULL);
321 1.1 darran }
322 1.1 darran
323 1.1 darran static Dwarf_Half
324 1.1 darran die_tag(dwarf_t *dw, Dwarf_Die die)
325 1.1 darran {
326 1.1 darran Dwarf_Half tag;
327 1.1 darran
328 1.1 darran if (dwarf_tag(die, &tag, &dw->dw_err) == DW_DLV_OK)
329 1.1 darran return (tag);
330 1.1 darran
331 1.12 christos terminate("die %ju: failed to get tag for type: %s\n",
332 1.12 christos (uintmax_t)die_off(dw, die), dwarf_errmsg(dw->dw_err));
333 1.1 darran /*NOTREACHED*/
334 1.1 darran return (0);
335 1.1 darran }
336 1.1 darran
337 1.1 darran static Dwarf_Attribute
338 1.1 darran die_attr(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, int req)
339 1.1 darran {
340 1.1 darran Dwarf_Attribute attr;
341 1.1 darran int rc;
342 1.1 darran
343 1.1 darran if ((rc = dwarf_attr(die, name, &attr, &dw->dw_err)) == DW_DLV_OK) {
344 1.1 darran return (attr);
345 1.1 darran } else if (rc == DW_DLV_NO_ENTRY) {
346 1.1 darran if (req) {
347 1.12 christos terminate("die %ju: no attr 0x%x\n",
348 1.12 christos (uintmax_t)die_off(dw, die),
349 1.1 darran name);
350 1.1 darran } else {
351 1.1 darran return (NULL);
352 1.1 darran }
353 1.1 darran }
354 1.1 darran
355 1.12 christos terminate("die %ju: failed to get attribute for type: %s\n",
356 1.12 christos (uintmax_t)die_off(dw, die), dwarf_errmsg(dw->dw_err));
357 1.1 darran /*NOTREACHED*/
358 1.1 darran return (NULL);
359 1.1 darran }
360 1.1 darran
361 1.1 darran static int
362 1.1 darran die_signed(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Signed *valp,
363 1.1 darran int req)
364 1.1 darran {
365 1.2 darran *valp = 0;
366 1.8 christos if (dwarf_attrval_signed(die, name, valp, &dw->dw_err) != DW_DLV_OK) {
367 1.2 darran if (req)
368 1.12 christos terminate("die %ju: failed to get signed: %s\n",
369 1.12 christos (uintmax_t)die_off(dw, die),
370 1.12 christos dwarf_errmsg(dw->dw_err));
371 1.2 darran return (0);
372 1.1 darran }
373 1.1 darran
374 1.1 darran return (1);
375 1.1 darran }
376 1.1 darran
377 1.1 darran static int
378 1.1 darran die_unsigned(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Unsigned *valp,
379 1.1 darran int req)
380 1.1 darran {
381 1.2 darran *valp = 0;
382 1.8 christos if (dwarf_attrval_unsigned(die, name, valp, &dw->dw_err) != DW_DLV_OK) {
383 1.2 darran if (req)
384 1.12 christos terminate("die %ju: failed to get unsigned: %s\n",
385 1.12 christos (uintmax_t)die_off(dw, die),
386 1.12 christos dwarf_errmsg(dw->dw_err));
387 1.2 darran return (0);
388 1.1 darran }
389 1.1 darran
390 1.1 darran return (1);
391 1.1 darran }
392 1.1 darran
393 1.1 darran static int
394 1.1 darran die_bool(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Bool *valp, int req)
395 1.1 darran {
396 1.2 darran *valp = 0;
397 1.1 darran
398 1.8 christos if (dwarf_attrval_flag(die, name, valp, &dw->dw_err) != DW_DLV_OK) {
399 1.2 darran if (req)
400 1.12 christos terminate("die %ju: failed to get flag: %s\n",
401 1.12 christos (uintmax_t)die_off(dw, die),
402 1.12 christos dwarf_errmsg(dw->dw_err));
403 1.2 darran return (0);
404 1.1 darran }
405 1.1 darran
406 1.1 darran return (1);
407 1.1 darran }
408 1.1 darran
409 1.1 darran static int
410 1.1 darran die_string(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, char **strp, int req)
411 1.1 darran {
412 1.2 darran const char *str = NULL;
413 1.1 darran
414 1.8 christos if (dwarf_attrval_string(die, name, &str, &dw->dw_err) != DW_DLV_OK ||
415 1.2 darran str == NULL) {
416 1.2 darran if (req)
417 1.12 christos terminate("die %ju: failed to get string: %s\n",
418 1.12 christos (uintmax_t)die_off(dw, die),
419 1.12 christos dwarf_errmsg(dw->dw_err));
420 1.2 darran else
421 1.2 darran *strp = NULL;
422 1.2 darran return (0);
423 1.2 darran } else
424 1.2 darran *strp = xstrdup(str);
425 1.1 darran
426 1.1 darran return (1);
427 1.1 darran }
428 1.1 darran
429 1.1 darran static Dwarf_Off
430 1.1 darran die_attr_ref(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
431 1.1 darran {
432 1.25 chs Dwarf_Off off;
433 1.1 darran
434 1.8 christos if (dwarf_attrval_unsigned(die, name, &off, &dw->dw_err) != DW_DLV_OK) {
435 1.12 christos terminate("die %ju: failed to get ref: %s\n",
436 1.12 christos (uintmax_t)die_off(dw, die), dwarf_errmsg(dw->dw_err));
437 1.1 darran }
438 1.1 darran
439 1.1 darran return (off);
440 1.1 darran }
441 1.1 darran
442 1.1 darran static char *
443 1.1 darran die_name(dwarf_t *dw, Dwarf_Die die)
444 1.1 darran {
445 1.1 darran char *str = NULL;
446 1.1 darran
447 1.1 darran (void) die_string(dw, die, DW_AT_name, &str, 0);
448 1.8 christos if (str == NULL)
449 1.8 christos str = xstrdup("");
450 1.1 darran
451 1.1 darran return (str);
452 1.1 darran }
453 1.1 darran
454 1.1 darran static int
455 1.1 darran die_isdecl(dwarf_t *dw, Dwarf_Die die)
456 1.1 darran {
457 1.1 darran Dwarf_Bool val;
458 1.1 darran
459 1.1 darran return (die_bool(dw, die, DW_AT_declaration, &val, 0) && val);
460 1.1 darran }
461 1.1 darran
462 1.1 darran static int
463 1.1 darran die_isglobal(dwarf_t *dw, Dwarf_Die die)
464 1.1 darran {
465 1.1 darran Dwarf_Signed vis;
466 1.1 darran Dwarf_Bool ext;
467 1.1 darran
468 1.1 darran /*
469 1.1 darran * Some compilers (gcc) use DW_AT_external to indicate function
470 1.1 darran * visibility. Others (Sun) use DW_AT_visibility.
471 1.1 darran */
472 1.1 darran if (die_signed(dw, die, DW_AT_visibility, &vis, 0))
473 1.1 darran return (vis == DW_VIS_exported);
474 1.1 darran else
475 1.1 darran return (die_bool(dw, die, DW_AT_external, &ext, 0) && ext);
476 1.1 darran }
477 1.1 darran
478 1.1 darran static tdesc_t *
479 1.1 darran die_add(dwarf_t *dw, Dwarf_Off off)
480 1.1 darran {
481 1.1 darran tdesc_t *tdp = xcalloc(sizeof (tdesc_t));
482 1.1 darran
483 1.1 darran tdp->t_id = off;
484 1.1 darran
485 1.1 darran tdesc_add(dw, tdp);
486 1.1 darran
487 1.1 darran return (tdp);
488 1.1 darran }
489 1.1 darran
490 1.1 darran static tdesc_t *
491 1.1 darran die_lookup_pass1(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
492 1.1 darran {
493 1.1 darran Dwarf_Off ref = die_attr_ref(dw, die, name);
494 1.1 darran tdesc_t *tdp;
495 1.1 darran
496 1.1 darran if ((tdp = tdesc_lookup(dw, ref)) != NULL)
497 1.1 darran return (tdp);
498 1.1 darran
499 1.1 darran return (die_add(dw, ref));
500 1.1 darran }
501 1.1 darran
502 1.1 darran static int
503 1.1 darran die_mem_offset(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name,
504 1.2 darran Dwarf_Unsigned *valp, int req __unused)
505 1.1 darran {
506 1.2 darran Dwarf_Locdesc *loc = NULL;
507 1.2 darran Dwarf_Signed locnum = 0;
508 1.8 christos Dwarf_Attribute at;
509 1.8 christos Dwarf_Half form;
510 1.1 darran
511 1.8 christos if (name != DW_AT_data_member_location)
512 1.12 christos terminate("die %ju: can only process attribute "
513 1.12 christos "DW_AT_data_member_location\n",
514 1.12 christos (uintmax_t)die_off(dw, die));
515 1.8 christos
516 1.8 christos if ((at = die_attr(dw, die, name, 0)) == NULL)
517 1.2 darran return (0);
518 1.1 darran
519 1.8 christos if (dwarf_whatform(at, &form, &dw->dw_err) != DW_DLV_OK)
520 1.8 christos return (0);
521 1.1 darran
522 1.8 christos switch (form) {
523 1.8 christos case DW_FORM_sec_offset:
524 1.8 christos case DW_FORM_block:
525 1.8 christos case DW_FORM_block1:
526 1.8 christos case DW_FORM_block2:
527 1.8 christos case DW_FORM_block4:
528 1.8 christos /*
529 1.8 christos * GCC in base and Clang (3.3 or below) generates
530 1.8 christos * DW_AT_data_member_location attribute with DW_FORM_block*
531 1.8 christos * form. The attribute contains one DW_OP_plus_uconst
532 1.8 christos * operator. The member offset stores in the operand.
533 1.8 christos */
534 1.8 christos if (dwarf_loclist(at, &loc, &locnum, &dw->dw_err) != DW_DLV_OK)
535 1.8 christos return (0);
536 1.8 christos if (locnum != 1 || loc->ld_s->lr_atom != DW_OP_plus_uconst) {
537 1.12 christos terminate("die %ju: cannot parse member offset with "
538 1.8 christos "operator other than DW_OP_plus_uconst\n",
539 1.12 christos (uintmax_t)die_off(dw, die));
540 1.8 christos }
541 1.8 christos *valp = loc->ld_s->lr_number;
542 1.8 christos if (loc != NULL) {
543 1.8 christos dwarf_dealloc(dw->dw_dw, loc->ld_s, DW_DLA_LOC_BLOCK);
544 1.8 christos dwarf_dealloc(dw->dw_dw, loc, DW_DLA_LOCDESC);
545 1.8 christos }
546 1.8 christos break;
547 1.1 darran
548 1.8 christos case DW_FORM_data1:
549 1.8 christos case DW_FORM_data2:
550 1.8 christos case DW_FORM_data4:
551 1.8 christos case DW_FORM_data8:
552 1.8 christos case DW_FORM_udata:
553 1.8 christos /*
554 1.8 christos * Clang 3.4 generates DW_AT_data_member_location attribute
555 1.8 christos * with DW_FORM_data* form (constant class). The attribute
556 1.8 christos * stores a contant value which is the member offset.
557 1.8 christos *
558 1.8 christos * However, note that DW_FORM_data[48] in DWARF version 2 or 3
559 1.8 christos * could be used as a section offset (offset into .debug_loc in
560 1.8 christos * this case). Here we assume the attribute always stores a
561 1.8 christos * constant because we know Clang 3.4 does this and GCC in
562 1.8 christos * base won't emit DW_FORM_data[48] for this attribute. This
563 1.8 christos * code will remain correct if future vesrions of Clang and
564 1.8 christos * GCC conform to DWARF4 standard and only use the form
565 1.8 christos * DW_FORM_sec_offset for section offset.
566 1.8 christos */
567 1.8 christos if (dwarf_attrval_unsigned(die, name, valp, &dw->dw_err) !=
568 1.8 christos DW_DLV_OK)
569 1.8 christos return (0);
570 1.8 christos break;
571 1.8 christos
572 1.8 christos default:
573 1.12 christos terminate("die %ju: cannot parse member offset with form "
574 1.12 christos "%u\n", (uintmax_t)die_off(dw, die), form);
575 1.8 christos }
576 1.1 darran
577 1.1 darran return (1);
578 1.1 darran }
579 1.1 darran
580 1.1 darran static tdesc_t *
581 1.1 darran tdesc_intr_common(dwarf_t *dw, int tid, const char *name, size_t sz)
582 1.1 darran {
583 1.1 darran tdesc_t *tdp;
584 1.1 darran intr_t *intr;
585 1.1 darran
586 1.1 darran intr = xcalloc(sizeof (intr_t));
587 1.1 darran intr->intr_type = INTR_INT;
588 1.1 darran intr->intr_signed = 1;
589 1.1 darran intr->intr_nbits = sz * NBBY;
590 1.1 darran
591 1.1 darran tdp = xcalloc(sizeof (tdesc_t));
592 1.1 darran tdp->t_name = xstrdup(name);
593 1.1 darran tdp->t_size = sz;
594 1.1 darran tdp->t_id = tid;
595 1.1 darran tdp->t_type = INTRINSIC;
596 1.1 darran tdp->t_intr = intr;
597 1.1 darran tdp->t_flags = TDESC_F_RESOLVED;
598 1.1 darran
599 1.1 darran tdesc_add(dw, tdp);
600 1.1 darran
601 1.1 darran return (tdp);
602 1.1 darran }
603 1.1 darran
604 1.1 darran /*
605 1.1 darran * Manufacture a void type. Used for gcc-emitted stabs, where the lack of a
606 1.1 darran * type reference implies a reference to a void type. A void *, for example
607 1.1 darran * will be represented by a pointer die without a DW_AT_type. CTF requires
608 1.1 darran * that pointer nodes point to something, so we'll create a void for use as
609 1.1 darran * the target. Note that the DWARF data may already create a void type. Ours
610 1.1 darran * would then be a duplicate, but it'll be removed in the self-uniquification
611 1.1 darran * merge performed at the completion of DWARF->tdesc conversion.
612 1.1 darran */
613 1.1 darran static tdesc_t *
614 1.1 darran tdesc_intr_void(dwarf_t *dw)
615 1.1 darran {
616 1.1 darran if (dw->dw_void == NULL)
617 1.1 darran dw->dw_void = tdesc_intr_common(dw, TID_VOID, "void", 0);
618 1.1 darran
619 1.1 darran return (dw->dw_void);
620 1.1 darran }
621 1.1 darran
622 1.1 darran static tdesc_t *
623 1.1 darran tdesc_intr_long(dwarf_t *dw)
624 1.1 darran {
625 1.1 darran if (dw->dw_long == NULL) {
626 1.1 darran dw->dw_long = tdesc_intr_common(dw, TID_LONG, "long",
627 1.1 darran dw->dw_ptrsz);
628 1.1 darran }
629 1.1 darran
630 1.1 darran return (dw->dw_long);
631 1.1 darran }
632 1.1 darran
633 1.1 darran /*
634 1.1 darran * Used for creating bitfield types. We create a copy of an existing intrinsic,
635 1.1 darran * adjusting the size of the copy to match what the caller requested. The
636 1.1 darran * caller can then use the copy as the type for a bitfield structure member.
637 1.1 darran */
638 1.1 darran static tdesc_t *
639 1.26.8.1 perseant tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz, const char *suffix)
640 1.1 darran {
641 1.1 darran tdesc_t *new = xcalloc(sizeof (tdesc_t));
642 1.1 darran
643 1.1 darran if (!(old->t_flags & TDESC_F_RESOLVED)) {
644 1.1 darran terminate("tdp %u: attempt to make a bit field from an "
645 1.1 darran "unresolved type\n", old->t_id);
646 1.1 darran }
647 1.1 darran
648 1.26.8.1 perseant xasprintf(&new->t_name, "%s %s", old->t_name, suffix);
649 1.1 darran new->t_size = old->t_size;
650 1.1 darran new->t_id = mfgtid_next(dw);
651 1.1 darran new->t_type = INTRINSIC;
652 1.1 darran new->t_flags = TDESC_F_RESOLVED;
653 1.1 darran
654 1.1 darran new->t_intr = xcalloc(sizeof (intr_t));
655 1.1 darran bcopy(old->t_intr, new->t_intr, sizeof (intr_t));
656 1.1 darran new->t_intr->intr_nbits = bitsz;
657 1.1 darran
658 1.1 darran tdesc_add(dw, new);
659 1.1 darran
660 1.1 darran return (new);
661 1.1 darran }
662 1.1 darran
663 1.1 darran static void
664 1.1 darran tdesc_array_create(dwarf_t *dw, Dwarf_Die dim, tdesc_t *arrtdp,
665 1.1 darran tdesc_t *dimtdp)
666 1.1 darran {
667 1.1 darran Dwarf_Unsigned uval;
668 1.1 darran Dwarf_Signed sval;
669 1.2 darran tdesc_t *ctdp = NULL;
670 1.1 darran Dwarf_Die dim2;
671 1.1 darran ardef_t *ar;
672 1.1 darran
673 1.1 darran if ((dim2 = die_sibling(dw, dim)) == NULL) {
674 1.1 darran ctdp = arrtdp;
675 1.1 darran } else if (die_tag(dw, dim2) == DW_TAG_subrange_type) {
676 1.1 darran ctdp = xcalloc(sizeof (tdesc_t));
677 1.1 darran ctdp->t_id = mfgtid_next(dw);
678 1.20 christos debug(3, "die %ju: creating new type %#x for sub-dimension\n",
679 1.12 christos (uintmax_t)die_off(dw, dim2), ctdp->t_id);
680 1.1 darran tdesc_array_create(dw, dim2, arrtdp, ctdp);
681 1.1 darran } else {
682 1.12 christos terminate("die %ju: unexpected non-subrange node in array\n",
683 1.12 christos (uintmax_t)die_off(dw, dim2));
684 1.1 darran }
685 1.1 darran
686 1.1 darran dimtdp->t_type = ARRAY;
687 1.1 darran dimtdp->t_ardef = ar = xcalloc(sizeof (ardef_t));
688 1.1 darran
689 1.1 darran /*
690 1.1 darran * Array bounds can be signed or unsigned, but there are several kinds
691 1.1 darran * of signless forms (data1, data2, etc) that take their sign from the
692 1.1 darran * routine that is trying to interpret them. That is, data1 can be
693 1.1 darran * either signed or unsigned, depending on whether you use the signed or
694 1.1 darran * unsigned accessor function. GCC will use the signless forms to store
695 1.1 darran * unsigned values which have their high bit set, so we need to try to
696 1.1 darran * read them first as unsigned to get positive values. We could also
697 1.1 darran * try signed first, falling back to unsigned if we got a negative
698 1.1 darran * value.
699 1.1 darran */
700 1.1 darran if (die_unsigned(dw, dim, DW_AT_upper_bound, &uval, 0))
701 1.1 darran ar->ad_nelems = uval + 1;
702 1.1 darran else if (die_signed(dw, dim, DW_AT_upper_bound, &sval, 0))
703 1.1 darran ar->ad_nelems = sval + 1;
704 1.18 joerg else if (die_unsigned(dw, dim, DW_AT_count, &uval, 0))
705 1.26.8.1 perseant ar->ad_nelems = uval;
706 1.17 joerg else if (die_signed(dw, dim, DW_AT_count, &sval, 0))
707 1.26.8.1 perseant ar->ad_nelems = sval;
708 1.1 darran else
709 1.1 darran ar->ad_nelems = 0;
710 1.1 darran
711 1.1 darran /*
712 1.1 darran * Different compilers use different index types. Force the type to be
713 1.1 darran * a common, known value (long).
714 1.1 darran */
715 1.1 darran ar->ad_idxtype = tdesc_intr_long(dw);
716 1.1 darran ar->ad_contents = ctdp;
717 1.20 christos debug(3, "die %ju: hi mom sibling type %#x for dimension\n",
718 1.20 christos (uintmax_t)die_off(dw, dim), ctdp->t_id);
719 1.1 darran
720 1.1 darran if (ar->ad_contents->t_size != 0) {
721 1.1 darran dimtdp->t_size = ar->ad_contents->t_size * ar->ad_nelems;
722 1.1 darran dimtdp->t_flags |= TDESC_F_RESOLVED;
723 1.1 darran }
724 1.1 darran }
725 1.1 darran
726 1.1 darran /*
727 1.1 darran * Create a tdesc from an array node. Some arrays will come with byte size
728 1.1 darran * attributes, and thus can be resolved immediately. Others don't, and will
729 1.1 darran * need to wait until the second pass for resolution.
730 1.1 darran */
731 1.1 darran static void
732 1.1 darran die_array_create(dwarf_t *dw, Dwarf_Die arr, Dwarf_Off off, tdesc_t *tdp)
733 1.1 darran {
734 1.1 darran tdesc_t *arrtdp = die_lookup_pass1(dw, arr, DW_AT_type);
735 1.1 darran Dwarf_Unsigned uval;
736 1.1 darran Dwarf_Die dim;
737 1.1 darran
738 1.12 christos debug(3, "die %ju <%jx>: creating array\n",
739 1.12 christos (uintmax_t)off, (uintmax_t)off);
740 1.1 darran
741 1.1 darran if ((dim = die_child(dw, arr)) == NULL ||
742 1.1 darran die_tag(dw, dim) != DW_TAG_subrange_type)
743 1.12 christos terminate("die %ju: failed to retrieve array bounds\n",
744 1.12 christos (uintmax_t)off);
745 1.1 darran
746 1.20 christos if (arrtdp->t_type == 0) {
747 1.20 christos /*
748 1.20 christos * Add the die that contains the type of the array elements
749 1.20 christos * to the the ones we process; XXX: no public API for that?
750 1.20 christos */
751 1.20 christos extern Dwarf_Die _dwarf_die_find(Dwarf_Die, Dwarf_Unsigned);
752 1.20 christos Dwarf_Die elem = _dwarf_die_find(arr, arrtdp->t_id);
753 1.20 christos if (elem != NULL)
754 1.20 christos die_create_one(dw, elem);
755 1.20 christos }
756 1.20 christos
757 1.1 darran tdesc_array_create(dw, dim, arrtdp, tdp);
758 1.1 darran
759 1.1 darran if (die_unsigned(dw, arr, DW_AT_byte_size, &uval, 0)) {
760 1.1 darran tdesc_t *dimtdp;
761 1.1 darran int flags;
762 1.1 darran
763 1.1 darran tdp->t_size = uval;
764 1.1 darran
765 1.1 darran /*
766 1.1 darran * Ensure that sub-dimensions have sizes too before marking
767 1.1 darran * as resolved.
768 1.1 darran */
769 1.1 darran flags = TDESC_F_RESOLVED;
770 1.1 darran for (dimtdp = tdp->t_ardef->ad_contents;
771 1.1 darran dimtdp->t_type == ARRAY;
772 1.1 darran dimtdp = dimtdp->t_ardef->ad_contents) {
773 1.1 darran if (!(dimtdp->t_flags & TDESC_F_RESOLVED)) {
774 1.1 darran flags = 0;
775 1.1 darran break;
776 1.1 darran }
777 1.1 darran }
778 1.1 darran
779 1.1 darran tdp->t_flags |= flags;
780 1.1 darran }
781 1.1 darran
782 1.12 christos debug(3, "die %ju <%jx>: array nelems %u size %u\n", (uintmax_t)off,
783 1.12 christos (uintmax_t)off, tdp->t_ardef->ad_nelems, tdp->t_size);
784 1.1 darran }
785 1.1 darran
786 1.1 darran /*ARGSUSED1*/
787 1.1 darran static int
788 1.2 darran die_array_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
789 1.1 darran {
790 1.1 darran dwarf_t *dw = private;
791 1.1 darran size_t sz;
792 1.1 darran
793 1.1 darran if (tdp->t_flags & TDESC_F_RESOLVED)
794 1.1 darran return (1);
795 1.1 darran
796 1.21 christos debug(3, "trying to resolve array %#x (cont %#x/%d)\n", tdp->t_id,
797 1.20 christos tdp->t_ardef->ad_contents->t_id,
798 1.20 christos tdp->t_ardef->ad_contents->t_size);
799 1.1 darran
800 1.19 joerg if ((sz = tdesc_size(tdp->t_ardef->ad_contents)) == 0 &&
801 1.19 joerg (tdp->t_ardef->ad_contents->t_flags & TDESC_F_RESOLVED) == 0) {
802 1.20 christos debug(3, "unable to resolve array %s (%#x) contents %#x\n",
803 1.1 darran tdesc_name(tdp), tdp->t_id,
804 1.1 darran tdp->t_ardef->ad_contents->t_id);
805 1.1 darran
806 1.1 darran dw->dw_nunres++;
807 1.1 darran return (1);
808 1.1 darran }
809 1.1 darran
810 1.1 darran tdp->t_size = sz * tdp->t_ardef->ad_nelems;
811 1.1 darran tdp->t_flags |= TDESC_F_RESOLVED;
812 1.1 darran
813 1.20 christos debug(3, "resolved array %#x: %u bytes\n", tdp->t_id, tdp->t_size);
814 1.1 darran
815 1.1 darran return (1);
816 1.1 darran }
817 1.1 darran
818 1.1 darran /*ARGSUSED1*/
819 1.1 darran static int
820 1.2 darran die_array_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused)
821 1.1 darran {
822 1.1 darran tdesc_t *cont = tdp->t_ardef->ad_contents;
823 1.1 darran
824 1.1 darran if (tdp->t_flags & TDESC_F_RESOLVED)
825 1.1 darran return (1);
826 1.1 darran
827 1.1 darran fprintf(stderr, "Array %d: failed to size contents type %s (%d)\n",
828 1.1 darran tdp->t_id, tdesc_name(cont), cont->t_id);
829 1.1 darran
830 1.1 darran return (1);
831 1.1 darran }
832 1.1 darran
833 1.1 darran /*
834 1.1 darran * Most enums (those with members) will be resolved during this first pass.
835 1.1 darran * Others - those without members (see the file comment) - won't be, and will
836 1.1 darran * need to wait until the second pass when they can be matched with their full
837 1.1 darran * definitions.
838 1.1 darran */
839 1.1 darran static void
840 1.1 darran die_enum_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
841 1.1 darran {
842 1.1 darran Dwarf_Die mem;
843 1.1 darran Dwarf_Unsigned uval;
844 1.1 darran Dwarf_Signed sval;
845 1.1 darran
846 1.25 chs if (die_isdecl(dw, die)) {
847 1.25 chs tdp->t_type = FORWARD;
848 1.25 chs return;
849 1.25 chs }
850 1.25 chs
851 1.12 christos debug(3, "die %ju: creating enum\n", (uintmax_t)off);
852 1.1 darran
853 1.25 chs tdp->t_type = ENUM;
854 1.1 darran
855 1.1 darran (void) die_unsigned(dw, die, DW_AT_byte_size, &uval, DW_ATTR_REQ);
856 1.1 darran tdp->t_size = uval;
857 1.1 darran
858 1.1 darran if ((mem = die_child(dw, die)) != NULL) {
859 1.1 darran elist_t **elastp = &tdp->t_emem;
860 1.1 darran
861 1.1 darran do {
862 1.1 darran elist_t *el;
863 1.1 darran
864 1.1 darran if (die_tag(dw, mem) != DW_TAG_enumerator) {
865 1.1 darran /* Nested type declaration */
866 1.1 darran die_create_one(dw, mem);
867 1.1 darran continue;
868 1.1 darran }
869 1.1 darran
870 1.1 darran el = xcalloc(sizeof (elist_t));
871 1.1 darran el->el_name = die_name(dw, mem);
872 1.1 darran
873 1.1 darran if (die_signed(dw, mem, DW_AT_const_value, &sval, 0)) {
874 1.1 darran el->el_number = sval;
875 1.1 darran } else if (die_unsigned(dw, mem, DW_AT_const_value,
876 1.1 darran &uval, 0)) {
877 1.1 darran el->el_number = uval;
878 1.1 darran } else {
879 1.12 christos terminate("die %ju: enum %ju: member without "
880 1.12 christos "value\n", (uintmax_t)off,
881 1.12 christos (uintmax_t)die_off(dw, mem));
882 1.1 darran }
883 1.1 darran
884 1.12 christos debug(3, "die %ju: enum %ju: created %s = %d\n",
885 1.12 christos (uintmax_t)off, (uintmax_t)die_off(dw, mem),
886 1.12 christos el->el_name, el->el_number);
887 1.1 darran
888 1.1 darran *elastp = el;
889 1.1 darran elastp = &el->el_next;
890 1.1 darran
891 1.1 darran } while ((mem = die_sibling(dw, mem)) != NULL);
892 1.1 darran
893 1.1 darran hash_add(dw->dw_enumhash, tdp);
894 1.1 darran
895 1.1 darran tdp->t_flags |= TDESC_F_RESOLVED;
896 1.1 darran
897 1.1 darran if (tdp->t_name != NULL) {
898 1.1 darran iidesc_t *ii = xcalloc(sizeof (iidesc_t));
899 1.1 darran ii->ii_type = II_SOU;
900 1.1 darran ii->ii_name = xstrdup(tdp->t_name);
901 1.1 darran ii->ii_dtype = tdp;
902 1.1 darran
903 1.1 darran iidesc_add(dw->dw_td->td_iihash, ii);
904 1.1 darran }
905 1.1 darran }
906 1.1 darran }
907 1.1 darran
908 1.1 darran static int
909 1.1 darran die_enum_match(void *arg1, void *arg2)
910 1.1 darran {
911 1.1 darran tdesc_t *tdp = arg1, **fullp = arg2;
912 1.1 darran
913 1.1 darran if (tdp->t_emem != NULL) {
914 1.1 darran *fullp = tdp;
915 1.1 darran return (-1); /* stop the iteration */
916 1.1 darran }
917 1.1 darran
918 1.1 darran return (0);
919 1.1 darran }
920 1.1 darran
921 1.1 darran /*ARGSUSED1*/
922 1.1 darran static int
923 1.2 darran die_enum_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
924 1.1 darran {
925 1.1 darran dwarf_t *dw = private;
926 1.1 darran tdesc_t *full = NULL;
927 1.1 darran
928 1.1 darran if (tdp->t_flags & TDESC_F_RESOLVED)
929 1.1 darran return (1);
930 1.1 darran
931 1.1 darran (void) hash_find_iter(dw->dw_enumhash, tdp, die_enum_match, &full);
932 1.1 darran
933 1.1 darran /*
934 1.1 darran * The answer to this one won't change from iteration to iteration,
935 1.1 darran * so don't even try.
936 1.1 darran */
937 1.1 darran if (full == NULL) {
938 1.1 darran terminate("tdp %u: enum %s has no members\n", tdp->t_id,
939 1.1 darran tdesc_name(tdp));
940 1.1 darran }
941 1.1 darran
942 1.1 darran debug(3, "tdp %u: enum %s redirected to %u\n", tdp->t_id,
943 1.1 darran tdesc_name(tdp), full->t_id);
944 1.1 darran
945 1.1 darran tdp->t_flags |= TDESC_F_RESOLVED;
946 1.1 darran
947 1.1 darran return (1);
948 1.1 darran }
949 1.1 darran
950 1.1 darran static int
951 1.1 darran die_fwd_map(void *arg1, void *arg2)
952 1.1 darran {
953 1.1 darran tdesc_t *fwd = arg1, *sou = arg2;
954 1.1 darran
955 1.1 darran debug(3, "tdp %u: mapped forward %s to sou %u\n", fwd->t_id,
956 1.1 darran tdesc_name(fwd), sou->t_id);
957 1.1 darran fwd->t_tdesc = sou;
958 1.1 darran
959 1.1 darran return (0);
960 1.1 darran }
961 1.1 darran
962 1.1 darran /*
963 1.1 darran * Structures and unions will never be resolved during the first pass, as we
964 1.1 darran * won't be able to fully determine the member sizes. The second pass, which
965 1.1 darran * have access to sizing information, will be able to complete the resolution.
966 1.1 darran */
967 1.1 darran static void
968 1.1 darran die_sou_create(dwarf_t *dw, Dwarf_Die str, Dwarf_Off off, tdesc_t *tdp,
969 1.1 darran int type, const char *typename)
970 1.1 darran {
971 1.25 chs Dwarf_Unsigned sz, bitsz, bitoff;
972 1.14 christos #if BYTE_ORDER == LITTLE_ENDIAN
973 1.8 christos Dwarf_Unsigned bysz;
974 1.8 christos #endif
975 1.1 darran Dwarf_Die mem;
976 1.1 darran mlist_t *ml, **mlastp;
977 1.1 darran iidesc_t *ii;
978 1.1 darran
979 1.1 darran tdp->t_type = (die_isdecl(dw, str) ? FORWARD : type);
980 1.1 darran
981 1.20 christos debug(3, "die %ju: creating %s %s <%d>\n", (uintmax_t)off,
982 1.1 darran (tdp->t_type == FORWARD ? "forward decl" : typename),
983 1.20 christos tdesc_name(tdp), tdp->t_id);
984 1.1 darran
985 1.1 darran if (tdp->t_type == FORWARD) {
986 1.1 darran hash_add(dw->dw_fwdhash, tdp);
987 1.1 darran return;
988 1.1 darran }
989 1.1 darran
990 1.1 darran (void) hash_find_iter(dw->dw_fwdhash, tdp, die_fwd_map, tdp);
991 1.1 darran
992 1.1 darran (void) die_unsigned(dw, str, DW_AT_byte_size, &sz, DW_ATTR_REQ);
993 1.1 darran tdp->t_size = sz;
994 1.1 darran
995 1.1 darran /*
996 1.1 darran * GCC allows empty SOUs as an extension.
997 1.1 darran */
998 1.2 darran if ((mem = die_child(dw, str)) == NULL) {
999 1.1 darran goto out;
1000 1.2 darran }
1001 1.1 darran
1002 1.1 darran mlastp = &tdp->t_members;
1003 1.1 darran
1004 1.1 darran do {
1005 1.1 darran Dwarf_Off memoff = die_off(dw, mem);
1006 1.1 darran Dwarf_Half tag = die_tag(dw, mem);
1007 1.1 darran Dwarf_Unsigned mloff;
1008 1.1 darran
1009 1.1 darran if (tag != DW_TAG_member) {
1010 1.1 darran /* Nested type declaration */
1011 1.1 darran die_create_one(dw, mem);
1012 1.1 darran continue;
1013 1.1 darran }
1014 1.1 darran
1015 1.12 christos debug(3, "die %ju: mem %ju: creating member\n",
1016 1.12 christos (uintmax_t)off, (uintmax_t)memoff);
1017 1.1 darran
1018 1.1 darran ml = xcalloc(sizeof (mlist_t));
1019 1.1 darran
1020 1.1 darran /*
1021 1.1 darran * This could be a GCC anon struct/union member, so we'll allow
1022 1.1 darran * an empty name, even though nothing can really handle them
1023 1.1 darran * properly. Note that some versions of GCC miss out debug
1024 1.1 darran * info for anon structs, though recent versions are fixed (gcc
1025 1.1 darran * bug 11816).
1026 1.1 darran */
1027 1.1 darran if ((ml->ml_name = die_name(dw, mem)) == NULL)
1028 1.2 darran ml->ml_name = NULL;
1029 1.1 darran
1030 1.1 darran ml->ml_type = die_lookup_pass1(dw, mem, DW_AT_type);
1031 1.1 darran
1032 1.1 darran if (die_mem_offset(dw, mem, DW_AT_data_member_location,
1033 1.1 darran &mloff, 0)) {
1034 1.12 christos debug(3, "die %ju: got mloff 0x%jx\n", (uintmax_t)off,
1035 1.12 christos (uintmax_t)mloff);
1036 1.1 darran ml->ml_offset = mloff * 8;
1037 1.1 darran }
1038 1.1 darran
1039 1.1 darran if (die_unsigned(dw, mem, DW_AT_bit_size, &bitsz, 0))
1040 1.1 darran ml->ml_size = bitsz;
1041 1.1 darran else
1042 1.1 darran ml->ml_size = tdesc_bitsize(ml->ml_type);
1043 1.1 darran
1044 1.26.8.1 perseant if (die_unsigned(dw, mem, DW_AT_data_bit_offset, &bitoff, 0)) {
1045 1.26.8.1 perseant ml->ml_offset += bitoff;
1046 1.26.8.1 perseant } else if (die_unsigned(dw, mem, DW_AT_bit_offset, &bitoff, 0)) {
1047 1.14 christos #if BYTE_ORDER == BIG_ENDIAN
1048 1.1 darran ml->ml_offset += bitoff;
1049 1.1 darran #else
1050 1.8 christos /*
1051 1.8 christos * Note that Clang 3.4 will sometimes generate
1052 1.8 christos * member DIE before generating the DIE for the
1053 1.8 christos * member's type. The code can not handle this
1054 1.8 christos * properly so that tdesc_bitsize(ml->ml_type) will
1055 1.8 christos * return 0 because ml->ml_type is unknown. As a
1056 1.8 christos * result, a wrong member offset will be calculated.
1057 1.8 christos * To workaround this, we can instead try to
1058 1.8 christos * retrieve the value of DW_AT_byte_size attribute
1059 1.8 christos * which stores the byte size of the space occupied
1060 1.8 christos * by the type. If this attribute exists, its value
1061 1.8 christos * should equal to tdesc_bitsize(ml->ml_type)/NBBY.
1062 1.8 christos */
1063 1.8 christos if (die_unsigned(dw, mem, DW_AT_byte_size, &bysz, 0) &&
1064 1.8 christos bysz > 0)
1065 1.8 christos ml->ml_offset += bysz * NBBY - bitoff -
1066 1.8 christos ml->ml_size;
1067 1.8 christos else
1068 1.8 christos ml->ml_offset += tdesc_bitsize(ml->ml_type) -
1069 1.8 christos bitoff - ml->ml_size;
1070 1.1 darran #endif
1071 1.1 darran }
1072 1.1 darran
1073 1.12 christos debug(3, "die %ju: mem %ju: created \"%s\" (off %u sz %u)\n",
1074 1.12 christos (uintmax_t)off, (uintmax_t)memoff, ml->ml_name,
1075 1.12 christos ml->ml_offset, ml->ml_size);
1076 1.1 darran
1077 1.1 darran *mlastp = ml;
1078 1.1 darran mlastp = &ml->ml_next;
1079 1.1 darran } while ((mem = die_sibling(dw, mem)) != NULL);
1080 1.1 darran
1081 1.1 darran /*
1082 1.1 darran * GCC will attempt to eliminate unused types, thus decreasing the
1083 1.1 darran * size of the emitted dwarf. That is, if you declare a foo_t in your
1084 1.1 darran * header, include said header in your source file, and neglect to
1085 1.1 darran * actually use (directly or indirectly) the foo_t in the source file,
1086 1.1 darran * the foo_t won't make it into the emitted DWARF. So, at least, goes
1087 1.1 darran * the theory.
1088 1.1 darran *
1089 1.1 darran * Occasionally, it'll emit the DW_TAG_structure_type for the foo_t,
1090 1.1 darran * and then neglect to emit the members. Strangely, the loner struct
1091 1.1 darran * tag will always be followed by a proper nested declaration of
1092 1.1 darran * something else. This is clearly a bug, but we're not going to have
1093 1.1 darran * time to get it fixed before this goo goes back, so we'll have to work
1094 1.1 darran * around it. If we see a no-membered struct with a nested declaration
1095 1.1 darran * (i.e. die_child of the struct tag won't be null), we'll ignore it.
1096 1.1 darran * Being paranoid, we won't simply remove it from the hash. Instead,
1097 1.1 darran * we'll decline to create an iidesc for it, thus ensuring that this
1098 1.1 darran * type won't make it into the output file. To be safe, we'll also
1099 1.1 darran * change the name.
1100 1.1 darran */
1101 1.1 darran if (tdp->t_members == NULL) {
1102 1.1 darran const char *old = tdesc_name(tdp);
1103 1.1 darran size_t newsz = 7 + strlen(old) + 1;
1104 1.1 darran char *new = xmalloc(newsz);
1105 1.1 darran (void) snprintf(new, newsz, "orphan %s", old);
1106 1.1 darran
1107 1.12 christos debug(3, "die %ju: worked around %s %s\n", (uintmax_t)off,
1108 1.12 christos typename, old);
1109 1.1 darran
1110 1.1 darran if (tdp->t_name != NULL)
1111 1.1 darran free(tdp->t_name);
1112 1.1 darran tdp->t_name = new;
1113 1.1 darran return;
1114 1.1 darran }
1115 1.1 darran
1116 1.1 darran out:
1117 1.1 darran if (tdp->t_name != NULL) {
1118 1.1 darran ii = xcalloc(sizeof (iidesc_t));
1119 1.1 darran ii->ii_type = II_SOU;
1120 1.1 darran ii->ii_name = xstrdup(tdp->t_name);
1121 1.1 darran ii->ii_dtype = tdp;
1122 1.1 darran
1123 1.1 darran iidesc_add(dw->dw_td->td_iihash, ii);
1124 1.1 darran }
1125 1.1 darran }
1126 1.1 darran
1127 1.1 darran static void
1128 1.1 darran die_struct_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1129 1.1 darran {
1130 1.1 darran die_sou_create(dw, die, off, tdp, STRUCT, "struct");
1131 1.1 darran }
1132 1.1 darran
1133 1.1 darran static void
1134 1.1 darran die_union_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1135 1.1 darran {
1136 1.1 darran die_sou_create(dw, die, off, tdp, UNION, "union");
1137 1.1 darran }
1138 1.1 darran
1139 1.22 christos static void
1140 1.22 christos die_class_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1141 1.22 christos {
1142 1.22 christos die_sou_create(dw, die, off, tdp, CLASS, "class");
1143 1.22 christos }
1144 1.22 christos
1145 1.1 darran /*ARGSUSED1*/
1146 1.1 darran static int
1147 1.2 darran die_sou_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
1148 1.1 darran {
1149 1.1 darran dwarf_t *dw = private;
1150 1.1 darran mlist_t *ml;
1151 1.1 darran tdesc_t *mt;
1152 1.1 darran
1153 1.1 darran if (tdp->t_flags & TDESC_F_RESOLVED)
1154 1.1 darran return (1);
1155 1.1 darran
1156 1.8 christos debug(3, "resolving sou %s\n", tdesc_name(tdp));
1157 1.1 darran
1158 1.1 darran for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {
1159 1.1 darran if (ml->ml_size == 0) {
1160 1.1 darran mt = tdesc_basetype(ml->ml_type);
1161 1.1 darran
1162 1.9 christos if (mt == NULL)
1163 1.9 christos continue;
1164 1.9 christos
1165 1.1 darran if ((ml->ml_size = tdesc_bitsize(mt)) != 0)
1166 1.1 darran continue;
1167 1.1 darran
1168 1.1 darran /*
1169 1.1 darran * For empty members, or GCC/C99 flexible array
1170 1.19 joerg * members, a size of 0 is correct. Structs and unions
1171 1.19 joerg * consisting of flexible array members will also have
1172 1.19 joerg * size 0.
1173 1.1 darran */
1174 1.1 darran if (mt->t_members == NULL)
1175 1.1 darran continue;
1176 1.26.8.1 perseant if (mt->t_type == ARRAY) {
1177 1.26.8.1 perseant if (mt->t_ardef->ad_nelems == 0)
1178 1.26.8.1 perseant continue;
1179 1.26.8.1 perseant mt = tdesc_basetype(mt->t_ardef->ad_contents);
1180 1.26.8.1 perseant if ((mt->t_flags & TDESC_F_RESOLVED) != 0 &&
1181 1.26.8.1 perseant (mt->t_type == STRUCT ||
1182 1.26.8.1 perseant mt->t_type == UNION) &&
1183 1.26.8.1 perseant mt->t_members == NULL)
1184 1.26.8.1 perseant continue;
1185 1.26.8.1 perseant }
1186 1.19 joerg if ((mt->t_flags & TDESC_F_RESOLVED) != 0 &&
1187 1.22 christos (mt->t_type == STRUCT || mt->t_type == UNION ||
1188 1.22 christos mt->t_type == CLASS))
1189 1.19 joerg continue;
1190 1.1 darran
1191 1.9 christos if (mt->t_type == STRUCT &&
1192 1.9 christos mt->t_members != NULL &&
1193 1.9 christos mt->t_members->ml_type->t_type == ARRAY &&
1194 1.9 christos mt->t_members->ml_type->t_ardef->ad_nelems == 0) {
1195 1.9 christos /* struct with zero sized array */
1196 1.9 christos continue;
1197 1.9 christos }
1198 1.9 christos
1199 1.11 christos /*
1200 1.11 christos * anonymous union members are OK.
1201 1.11 christos * XXX: we should consistently use NULL, instead of ""
1202 1.11 christos */
1203 1.11 christos if (mt->t_type == UNION &&
1204 1.11 christos (mt->t_name == NULL || mt->t_name[0] == '\0'))
1205 1.11 christos continue;
1206 1.11 christos
1207 1.23 christos /*
1208 1.23 christos * XXX: Gcc-5.4 DW_TAG_typedef without DW_AT_type;
1209 1.23 christos * assume pointer
1210 1.23 christos */
1211 1.23 christos if (mt->t_id == TID_VOID) {
1212 1.23 christos ml->ml_size = dw->dw_ptrsz;
1213 1.23 christos continue;
1214 1.23 christos }
1215 1.23 christos
1216 1.23 christos fprintf(stderr, "%s unresolved type=%d (%s) tid=%#x\n",
1217 1.23 christos tdesc_name(tdp), mt->t_type, tdesc_name(mt),
1218 1.23 christos mt->t_id);
1219 1.1 darran dw->dw_nunres++;
1220 1.1 darran return (1);
1221 1.1 darran }
1222 1.1 darran
1223 1.1 darran if ((mt = tdesc_basetype(ml->ml_type)) == NULL) {
1224 1.1 darran dw->dw_nunres++;
1225 1.1 darran return (1);
1226 1.1 darran }
1227 1.1 darran
1228 1.1 darran if (ml->ml_size != 0 && mt->t_type == INTRINSIC &&
1229 1.25 chs mt->t_intr->intr_nbits != ml->ml_size) {
1230 1.1 darran /*
1231 1.1 darran * This member is a bitfield, and needs to reference
1232 1.1 darran * an intrinsic type with the same width. If the
1233 1.1 darran * currently-referenced type isn't of the same width,
1234 1.1 darran * we'll copy it, adjusting the width of the copy to
1235 1.1 darran * the size we'd like.
1236 1.1 darran */
1237 1.1 darran debug(3, "tdp %u: creating bitfield for %d bits\n",
1238 1.1 darran tdp->t_id, ml->ml_size);
1239 1.1 darran
1240 1.26.8.1 perseant ml->ml_type = tdesc_intr_clone(dw, mt, ml->ml_size,
1241 1.26.8.1 perseant "bitfield");
1242 1.1 darran }
1243 1.1 darran }
1244 1.1 darran
1245 1.1 darran tdp->t_flags |= TDESC_F_RESOLVED;
1246 1.1 darran
1247 1.1 darran return (1);
1248 1.1 darran }
1249 1.1 darran
1250 1.1 darran /*ARGSUSED1*/
1251 1.1 darran static int
1252 1.2 darran die_sou_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused)
1253 1.1 darran {
1254 1.1 darran const char *typename = (tdp->t_type == STRUCT ? "struct" : "union");
1255 1.1 darran mlist_t *ml;
1256 1.1 darran
1257 1.1 darran if (tdp->t_flags & TDESC_F_RESOLVED)
1258 1.1 darran return (1);
1259 1.1 darran
1260 1.1 darran for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {
1261 1.1 darran if (ml->ml_size == 0) {
1262 1.2 darran fprintf(stderr, "%s %d <%x>: failed to size member \"%s\" "
1263 1.2 darran "of type %s (%d <%x>)\n", typename, tdp->t_id,
1264 1.2 darran tdp->t_id,
1265 1.1 darran ml->ml_name, tdesc_name(ml->ml_type),
1266 1.2 darran ml->ml_type->t_id, ml->ml_type->t_id);
1267 1.1 darran }
1268 1.1 darran }
1269 1.1 darran
1270 1.1 darran return (1);
1271 1.1 darran }
1272 1.1 darran
1273 1.1 darran static void
1274 1.1 darran die_funcptr_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1275 1.1 darran {
1276 1.1 darran Dwarf_Attribute attr;
1277 1.1 darran Dwarf_Half tag;
1278 1.1 darran Dwarf_Die arg;
1279 1.1 darran fndef_t *fn;
1280 1.1 darran int i;
1281 1.1 darran
1282 1.12 christos debug(3, "die %ju <0x%jx>: creating function pointer\n",
1283 1.12 christos (uintmax_t)off, (uintmax_t)off);
1284 1.1 darran
1285 1.1 darran /*
1286 1.1 darran * We'll begin by processing any type definition nodes that may be
1287 1.1 darran * lurking underneath this one.
1288 1.1 darran */
1289 1.1 darran for (arg = die_child(dw, die); arg != NULL;
1290 1.1 darran arg = die_sibling(dw, arg)) {
1291 1.1 darran if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
1292 1.1 darran tag != DW_TAG_unspecified_parameters) {
1293 1.1 darran /* Nested type declaration */
1294 1.1 darran die_create_one(dw, arg);
1295 1.1 darran }
1296 1.1 darran }
1297 1.1 darran
1298 1.1 darran if (die_isdecl(dw, die)) {
1299 1.1 darran /*
1300 1.1 darran * This is a prototype. We don't add prototypes to the
1301 1.1 darran * tree, so we're going to drop the tdesc. Unfortunately,
1302 1.1 darran * it has already been added to the tree. Nobody will reference
1303 1.1 darran * it, though, and it will be leaked.
1304 1.1 darran */
1305 1.1 darran return;
1306 1.1 darran }
1307 1.1 darran
1308 1.1 darran fn = xcalloc(sizeof (fndef_t));
1309 1.1 darran
1310 1.1 darran tdp->t_type = FUNCTION;
1311 1.1 darran
1312 1.1 darran if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
1313 1.1 darran fn->fn_ret = die_lookup_pass1(dw, die, DW_AT_type);
1314 1.1 darran } else {
1315 1.1 darran fn->fn_ret = tdesc_intr_void(dw);
1316 1.1 darran }
1317 1.1 darran
1318 1.1 darran /*
1319 1.1 darran * Count the arguments to the function, then read them in.
1320 1.1 darran */
1321 1.1 darran for (fn->fn_nargs = 0, arg = die_child(dw, die); arg != NULL;
1322 1.1 darran arg = die_sibling(dw, arg)) {
1323 1.1 darran if ((tag = die_tag(dw, arg)) == DW_TAG_formal_parameter)
1324 1.1 darran fn->fn_nargs++;
1325 1.1 darran else if (tag == DW_TAG_unspecified_parameters &&
1326 1.1 darran fn->fn_nargs > 0)
1327 1.1 darran fn->fn_vargs = 1;
1328 1.1 darran }
1329 1.1 darran
1330 1.1 darran if (fn->fn_nargs != 0) {
1331 1.12 christos debug(3, "die %ju: adding %d argument%s\n", (uintmax_t)off,
1332 1.12 christos fn->fn_nargs, (fn->fn_nargs > 1 ? "s" : ""));
1333 1.1 darran
1334 1.1 darran fn->fn_args = xcalloc(sizeof (tdesc_t *) * fn->fn_nargs);
1335 1.1 darran for (i = 0, arg = die_child(dw, die);
1336 1.2 darran arg != NULL && i < (int) fn->fn_nargs;
1337 1.1 darran arg = die_sibling(dw, arg)) {
1338 1.1 darran if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1339 1.1 darran continue;
1340 1.1 darran
1341 1.1 darran fn->fn_args[i++] = die_lookup_pass1(dw, arg,
1342 1.1 darran DW_AT_type);
1343 1.1 darran }
1344 1.1 darran }
1345 1.1 darran
1346 1.1 darran tdp->t_fndef = fn;
1347 1.1 darran tdp->t_flags |= TDESC_F_RESOLVED;
1348 1.1 darran }
1349 1.1 darran
1350 1.1 darran /*
1351 1.1 darran * GCC and DevPro use different names for the base types. While the terms are
1352 1.1 darran * the same, they are arranged in a different order. Some terms, such as int,
1353 1.1 darran * are implied in one, and explicitly named in the other. Given a base type
1354 1.1 darran * as input, this routine will return a common name, along with an intr_t
1355 1.1 darran * that reflects said name.
1356 1.1 darran */
1357 1.1 darran static intr_t *
1358 1.1 darran die_base_name_parse(const char *name, char **newp)
1359 1.1 darran {
1360 1.16 christos char buf[1024];
1361 1.2 darran char const *base;
1362 1.2 darran char *c;
1363 1.1 darran int nlong = 0, nshort = 0, nchar = 0, nint = 0;
1364 1.1 darran int sign = 1;
1365 1.1 darran char fmt = '\0';
1366 1.1 darran intr_t *intr;
1367 1.1 darran
1368 1.1 darran if (strlen(name) > sizeof (buf) - 1)
1369 1.1 darran terminate("base type name \"%s\" is too long\n", name);
1370 1.1 darran
1371 1.1 darran strncpy(buf, name, sizeof (buf));
1372 1.1 darran
1373 1.1 darran for (c = strtok(buf, " "); c != NULL; c = strtok(NULL, " ")) {
1374 1.1 darran if (strcmp(c, "signed") == 0)
1375 1.1 darran sign = 1;
1376 1.1 darran else if (strcmp(c, "unsigned") == 0)
1377 1.1 darran sign = 0;
1378 1.1 darran else if (strcmp(c, "long") == 0)
1379 1.1 darran nlong++;
1380 1.1 darran else if (strcmp(c, "char") == 0) {
1381 1.1 darran nchar++;
1382 1.1 darran fmt = 'c';
1383 1.1 darran } else if (strcmp(c, "short") == 0)
1384 1.1 darran nshort++;
1385 1.1 darran else if (strcmp(c, "int") == 0)
1386 1.1 darran nint++;
1387 1.1 darran else {
1388 1.1 darran /*
1389 1.1 darran * If we don't recognize any of the tokens, we'll tell
1390 1.1 darran * the caller to fall back to the dwarf-provided
1391 1.1 darran * encoding information.
1392 1.1 darran */
1393 1.1 darran return (NULL);
1394 1.1 darran }
1395 1.1 darran }
1396 1.1 darran
1397 1.1 darran if (nchar > 1 || nshort > 1 || nint > 1 || nlong > 2)
1398 1.1 darran return (NULL);
1399 1.1 darran
1400 1.1 darran if (nchar > 0) {
1401 1.1 darran if (nlong > 0 || nshort > 0 || nint > 0)
1402 1.1 darran return (NULL);
1403 1.1 darran
1404 1.1 darran base = "char";
1405 1.1 darran
1406 1.1 darran } else if (nshort > 0) {
1407 1.1 darran if (nlong > 0)
1408 1.1 darran return (NULL);
1409 1.1 darran
1410 1.1 darran base = "short";
1411 1.1 darran
1412 1.1 darran } else if (nlong > 0) {
1413 1.1 darran base = "long";
1414 1.1 darran
1415 1.1 darran } else {
1416 1.1 darran base = "int";
1417 1.1 darran }
1418 1.1 darran
1419 1.1 darran intr = xcalloc(sizeof (intr_t));
1420 1.1 darran intr->intr_type = INTR_INT;
1421 1.1 darran intr->intr_signed = sign;
1422 1.1 darran intr->intr_iformat = fmt;
1423 1.1 darran
1424 1.1 darran snprintf(buf, sizeof (buf), "%s%s%s",
1425 1.1 darran (sign ? "" : "unsigned "),
1426 1.1 darran (nlong > 1 ? "long " : ""),
1427 1.1 darran base);
1428 1.1 darran
1429 1.1 darran *newp = xstrdup(buf);
1430 1.1 darran return (intr);
1431 1.1 darran }
1432 1.1 darran
1433 1.26 simonb /*
1434 1.26 simonb * Return the CTF float encoding type. The logic is all floating
1435 1.26 simonb * point types of 4 bytes or less are "float", 8 bytes or less are
1436 1.26 simonb * "double" and 16 bytes or less are "long double". Anything bigger
1437 1.26 simonb * will error.
1438 1.26 simonb */
1439 1.26 simonb #define FLOAT_SIZE_SINGLE 4
1440 1.26 simonb #define FLOAT_SIZE_DOUBLE 8
1441 1.26 simonb #define FLOAT_SIZE_LONG_DOUBLE 16
1442 1.26 simonb
1443 1.1 darran typedef struct fp_size_map {
1444 1.26 simonb size_t fsm_typesz; /* size of type */
1445 1.1 darran uint_t fsm_enc[3]; /* CTF_FP_* for {bare,cplx,imagry} type */
1446 1.1 darran } fp_size_map_t;
1447 1.1 darran
1448 1.1 darran static const fp_size_map_t fp_encodings[] = {
1449 1.26 simonb { FLOAT_SIZE_SINGLE, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
1450 1.26 simonb { FLOAT_SIZE_DOUBLE, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
1451 1.26 simonb { FLOAT_SIZE_LONG_DOUBLE,
1452 1.26 simonb { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
1453 1.26 simonb { 0, { 0, 0, 0 } }
1454 1.1 darran };
1455 1.1 darran
1456 1.1 darran static uint_t
1457 1.26.8.1 perseant die_base_type2enc(dwarf_t *dw, Dwarf_Off off, Dwarf_Unsigned enc, size_t sz)
1458 1.1 darran {
1459 1.1 darran const fp_size_map_t *map = fp_encodings;
1460 1.1 darran uint_t mult = 1, col = 0;
1461 1.1 darran
1462 1.26 simonb switch (enc) {
1463 1.26 simonb case DW_ATE_complex_float:
1464 1.26 simonb #if defined(DW_ATE_SUN_interval_float)
1465 1.26 simonb case DW_ATE_SUN_interval_float:
1466 1.26 simonb #endif
1467 1.1 darran mult = 2;
1468 1.1 darran col = 1;
1469 1.26 simonb break;
1470 1.26 simonb case DW_ATE_imaginary_float:
1471 1.26 simonb #if defined(DW_ATE_SUN_imaginary_float)
1472 1.26 simonb case DW_ATE_SUN_imaginary_float:
1473 1.2 darran #endif
1474 1.1 darran col = 2;
1475 1.26 simonb break;
1476 1.26 simonb }
1477 1.1 darran
1478 1.26 simonb while (map->fsm_typesz != 0) {
1479 1.26 simonb if (sz <= map->fsm_typesz * mult)
1480 1.1 darran return (map->fsm_enc[col]);
1481 1.1 darran map++;
1482 1.1 darran }
1483 1.1 darran
1484 1.12 christos terminate("die %ju: unrecognized real type size %ju\n",
1485 1.12 christos (uintmax_t)off, (uintmax_t)sz);
1486 1.1 darran /*NOTREACHED*/
1487 1.1 darran return (0);
1488 1.1 darran }
1489 1.1 darran
1490 1.1 darran static intr_t *
1491 1.1 darran die_base_from_dwarf(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, size_t sz)
1492 1.1 darran {
1493 1.1 darran intr_t *intr = xcalloc(sizeof (intr_t));
1494 1.26.8.1 perseant Dwarf_Unsigned enc;
1495 1.1 darran
1496 1.26.8.1 perseant (void) die_unsigned(dw, base, DW_AT_encoding, &enc, DW_ATTR_REQ);
1497 1.1 darran
1498 1.1 darran switch (enc) {
1499 1.1 darran case DW_ATE_unsigned:
1500 1.1 darran case DW_ATE_address:
1501 1.1 darran intr->intr_type = INTR_INT;
1502 1.1 darran break;
1503 1.1 darran case DW_ATE_unsigned_char:
1504 1.1 darran intr->intr_type = INTR_INT;
1505 1.1 darran intr->intr_iformat = 'c';
1506 1.1 darran break;
1507 1.1 darran case DW_ATE_signed:
1508 1.1 darran intr->intr_type = INTR_INT;
1509 1.1 darran intr->intr_signed = 1;
1510 1.1 darran break;
1511 1.1 darran case DW_ATE_signed_char:
1512 1.1 darran intr->intr_type = INTR_INT;
1513 1.1 darran intr->intr_signed = 1;
1514 1.1 darran intr->intr_iformat = 'c';
1515 1.1 darran break;
1516 1.1 darran case DW_ATE_boolean:
1517 1.1 darran intr->intr_type = INTR_INT;
1518 1.1 darran intr->intr_signed = 1;
1519 1.1 darran intr->intr_iformat = 'b';
1520 1.1 darran break;
1521 1.1 darran case DW_ATE_float:
1522 1.1 darran case DW_ATE_complex_float:
1523 1.1 darran case DW_ATE_imaginary_float:
1524 1.26.8.1 perseant #if defined(DW_ATE_SUN_imaginary_float)
1525 1.1 darran case DW_ATE_SUN_imaginary_float:
1526 1.26.8.1 perseant #endif
1527 1.26.8.1 perseant #if defined(DW_ATE_SUN_interval_float)
1528 1.1 darran case DW_ATE_SUN_interval_float:
1529 1.2 darran #endif
1530 1.1 darran intr->intr_type = INTR_REAL;
1531 1.1 darran intr->intr_signed = 1;
1532 1.1 darran intr->intr_fformat = die_base_type2enc(dw, off, enc, sz);
1533 1.1 darran break;
1534 1.24 christos case DW_ATE_UTF:
1535 1.24 christos // XXX: c++ char16_t/char32_t; we don't deal with it.
1536 1.24 christos intr->intr_type = INTR_INT;
1537 1.24 christos intr->intr_signed = 1;
1538 1.24 christos intr->intr_iformat = 'v';
1539 1.24 christos break;
1540 1.1 darran default:
1541 1.12 christos terminate("die %ju: unknown base type encoding 0x%jx\n",
1542 1.12 christos (uintmax_t)off, (uintmax_t)enc);
1543 1.1 darran }
1544 1.1 darran
1545 1.1 darran return (intr);
1546 1.1 darran }
1547 1.1 darran
1548 1.1 darran static void
1549 1.1 darran die_base_create(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, tdesc_t *tdp)
1550 1.1 darran {
1551 1.1 darran Dwarf_Unsigned sz;
1552 1.1 darran intr_t *intr;
1553 1.1 darran char *new;
1554 1.1 darran
1555 1.12 christos debug(3, "die %ju: creating base type\n", (uintmax_t)off);
1556 1.1 darran
1557 1.1 darran /*
1558 1.1 darran * The compilers have their own clever (internally inconsistent) ideas
1559 1.1 darran * as to what base types should look like. Some times gcc will, for
1560 1.1 darran * example, use DW_ATE_signed_char for char. Other times, however, it
1561 1.1 darran * will use DW_ATE_signed. Needless to say, this causes some problems
1562 1.1 darran * down the road, particularly with merging. We do, however, use the
1563 1.1 darran * DWARF idea of type sizes, as this allows us to avoid caring about
1564 1.1 darran * the data model.
1565 1.1 darran */
1566 1.1 darran (void) die_unsigned(dw, base, DW_AT_byte_size, &sz, DW_ATTR_REQ);
1567 1.1 darran
1568 1.1 darran if (tdp->t_name == NULL)
1569 1.12 christos terminate("die %ju: base type without name\n", (uintmax_t)off);
1570 1.1 darran
1571 1.1 darran /* XXX make a name parser for float too */
1572 1.1 darran if ((intr = die_base_name_parse(tdp->t_name, &new)) != NULL) {
1573 1.1 darran /* Found it. We'll use the parsed version */
1574 1.12 christos debug(3, "die %ju: name \"%s\" remapped to \"%s\"\n",
1575 1.12 christos (uintmax_t)off, tdesc_name(tdp), new);
1576 1.1 darran
1577 1.1 darran free(tdp->t_name);
1578 1.1 darran tdp->t_name = new;
1579 1.1 darran } else {
1580 1.1 darran /*
1581 1.1 darran * We didn't recognize the type, so we'll create an intr_t
1582 1.1 darran * based on the DWARF data.
1583 1.1 darran */
1584 1.12 christos debug(3, "die %ju: using dwarf data for base \"%s\"\n",
1585 1.12 christos (uintmax_t)off, tdesc_name(tdp));
1586 1.1 darran
1587 1.1 darran intr = die_base_from_dwarf(dw, base, off, sz);
1588 1.1 darran }
1589 1.1 darran
1590 1.1 darran intr->intr_nbits = sz * 8;
1591 1.1 darran
1592 1.1 darran tdp->t_type = INTRINSIC;
1593 1.1 darran tdp->t_intr = intr;
1594 1.1 darran tdp->t_size = sz;
1595 1.1 darran
1596 1.1 darran tdp->t_flags |= TDESC_F_RESOLVED;
1597 1.1 darran }
1598 1.1 darran
1599 1.1 darran static void
1600 1.1 darran die_through_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp,
1601 1.1 darran int type, const char *typename)
1602 1.1 darran {
1603 1.1 darran Dwarf_Attribute attr;
1604 1.1 darran
1605 1.12 christos debug(3, "die %ju <0x%jx>: creating %s type %d\n", (uintmax_t)off,
1606 1.12 christos (uintmax_t)off, typename, type);
1607 1.1 darran
1608 1.1 darran tdp->t_type = type;
1609 1.1 darran
1610 1.1 darran if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
1611 1.1 darran tdp->t_tdesc = die_lookup_pass1(dw, die, DW_AT_type);
1612 1.1 darran } else {
1613 1.1 darran tdp->t_tdesc = tdesc_intr_void(dw);
1614 1.1 darran }
1615 1.1 darran
1616 1.20 christos if (type == POINTER || type == REFERENCE)
1617 1.1 darran tdp->t_size = dw->dw_ptrsz;
1618 1.1 darran
1619 1.1 darran tdp->t_flags |= TDESC_F_RESOLVED;
1620 1.1 darran
1621 1.1 darran if (type == TYPEDEF) {
1622 1.1 darran iidesc_t *ii = xcalloc(sizeof (iidesc_t));
1623 1.1 darran ii->ii_type = II_TYPE;
1624 1.1 darran ii->ii_name = xstrdup(tdp->t_name);
1625 1.1 darran ii->ii_dtype = tdp;
1626 1.1 darran
1627 1.1 darran iidesc_add(dw->dw_td->td_iihash, ii);
1628 1.1 darran }
1629 1.1 darran }
1630 1.1 darran
1631 1.1 darran static void
1632 1.1 darran die_typedef_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1633 1.1 darran {
1634 1.1 darran die_through_create(dw, die, off, tdp, TYPEDEF, "typedef");
1635 1.1 darran }
1636 1.1 darran
1637 1.1 darran static void
1638 1.1 darran die_const_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1639 1.1 darran {
1640 1.1 darran die_through_create(dw, die, off, tdp, CONST, "const");
1641 1.1 darran }
1642 1.1 darran
1643 1.1 darran static void
1644 1.1 darran die_pointer_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1645 1.1 darran {
1646 1.1 darran die_through_create(dw, die, off, tdp, POINTER, "pointer");
1647 1.1 darran }
1648 1.1 darran
1649 1.1 darran static void
1650 1.20 christos die_reference_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1651 1.20 christos {
1652 1.20 christos die_through_create(dw, die, off, tdp, REFERENCE, "reference");
1653 1.20 christos }
1654 1.20 christos
1655 1.20 christos static void
1656 1.1 darran die_restrict_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1657 1.1 darran {
1658 1.1 darran die_through_create(dw, die, off, tdp, RESTRICT, "restrict");
1659 1.1 darran }
1660 1.1 darran
1661 1.1 darran static void
1662 1.1 darran die_volatile_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1663 1.1 darran {
1664 1.1 darran die_through_create(dw, die, off, tdp, VOLATILE, "volatile");
1665 1.1 darran }
1666 1.1 darran
1667 1.1 darran /*ARGSUSED3*/
1668 1.1 darran static void
1669 1.2 darran die_function_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused)
1670 1.1 darran {
1671 1.1 darran Dwarf_Die arg;
1672 1.1 darran Dwarf_Half tag;
1673 1.1 darran iidesc_t *ii;
1674 1.1 darran char *name;
1675 1.1 darran
1676 1.12 christos debug(3, "die %ju <0x%jx>: creating function definition\n",
1677 1.12 christos (uintmax_t)off, (uintmax_t)off);
1678 1.1 darran
1679 1.1 darran /*
1680 1.1 darran * We'll begin by processing any type definition nodes that may be
1681 1.1 darran * lurking underneath this one.
1682 1.1 darran */
1683 1.1 darran for (arg = die_child(dw, die); arg != NULL;
1684 1.1 darran arg = die_sibling(dw, arg)) {
1685 1.1 darran if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
1686 1.1 darran tag != DW_TAG_variable) {
1687 1.1 darran /* Nested type declaration */
1688 1.1 darran die_create_one(dw, arg);
1689 1.1 darran }
1690 1.1 darran }
1691 1.1 darran
1692 1.1 darran if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) {
1693 1.1 darran /*
1694 1.1 darran * We process neither prototypes nor subprograms without
1695 1.1 darran * names.
1696 1.1 darran */
1697 1.1 darran return;
1698 1.1 darran }
1699 1.1 darran
1700 1.1 darran ii = xcalloc(sizeof (iidesc_t));
1701 1.1 darran ii->ii_type = die_isglobal(dw, die) ? II_GFUN : II_SFUN;
1702 1.1 darran ii->ii_name = name;
1703 1.1 darran if (ii->ii_type == II_SFUN)
1704 1.1 darran ii->ii_owner = xstrdup(dw->dw_cuname);
1705 1.1 darran
1706 1.12 christos debug(3, "die %ju: function %s is %s\n", (uintmax_t)off, ii->ii_name,
1707 1.1 darran (ii->ii_type == II_GFUN ? "global" : "static"));
1708 1.1 darran
1709 1.1 darran if (die_attr(dw, die, DW_AT_type, 0) != NULL)
1710 1.1 darran ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
1711 1.1 darran else
1712 1.1 darran ii->ii_dtype = tdesc_intr_void(dw);
1713 1.1 darran
1714 1.1 darran for (arg = die_child(dw, die); arg != NULL;
1715 1.1 darran arg = die_sibling(dw, arg)) {
1716 1.2 darran char *name1;
1717 1.1 darran
1718 1.12 christos debug(3, "die %ju: looking at sub member at %ju\n",
1719 1.12 christos (uintmax_t)off, (uintmax_t)die_off(dw, die));
1720 1.1 darran
1721 1.1 darran if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1722 1.1 darran continue;
1723 1.1 darran
1724 1.2 darran if ((name1 = die_name(dw, arg)) == NULL) {
1725 1.12 christos terminate("die %ju: func arg %d has no name\n",
1726 1.13 christos (uintmax_t)off, ii->ii_nargs + 1);
1727 1.1 darran }
1728 1.1 darran
1729 1.2 darran if (strcmp(name1, "...") == 0) {
1730 1.2 darran free(name1);
1731 1.1 darran ii->ii_vargs = 1;
1732 1.1 darran continue;
1733 1.1 darran }
1734 1.26.8.1 perseant free(name1);
1735 1.1 darran
1736 1.1 darran ii->ii_nargs++;
1737 1.1 darran }
1738 1.1 darran
1739 1.1 darran if (ii->ii_nargs > 0) {
1740 1.1 darran int i;
1741 1.1 darran
1742 1.12 christos debug(3, "die %ju: function has %d argument%s\n",
1743 1.13 christos (uintmax_t)off, ii->ii_nargs, ii->ii_nargs == 1 ? "" : "s");
1744 1.1 darran
1745 1.1 darran ii->ii_args = xcalloc(sizeof (tdesc_t) * ii->ii_nargs);
1746 1.1 darran
1747 1.1 darran for (arg = die_child(dw, die), i = 0;
1748 1.1 darran arg != NULL && i < ii->ii_nargs;
1749 1.1 darran arg = die_sibling(dw, arg)) {
1750 1.1 darran if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1751 1.1 darran continue;
1752 1.1 darran
1753 1.1 darran ii->ii_args[i++] = die_lookup_pass1(dw, arg,
1754 1.1 darran DW_AT_type);
1755 1.1 darran }
1756 1.1 darran }
1757 1.1 darran
1758 1.1 darran iidesc_add(dw->dw_td->td_iihash, ii);
1759 1.1 darran }
1760 1.1 darran
1761 1.1 darran /*ARGSUSED3*/
1762 1.1 darran static void
1763 1.2 darran die_variable_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused)
1764 1.1 darran {
1765 1.1 darran iidesc_t *ii;
1766 1.1 darran char *name;
1767 1.1 darran
1768 1.13 christos debug(3, "die %ju: creating object definition\n", (uintmax_t)off);
1769 1.1 darran
1770 1.1 darran if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL)
1771 1.1 darran return; /* skip prototypes and nameless objects */
1772 1.1 darran
1773 1.1 darran ii = xcalloc(sizeof (iidesc_t));
1774 1.1 darran ii->ii_type = die_isglobal(dw, die) ? II_GVAR : II_SVAR;
1775 1.1 darran ii->ii_name = name;
1776 1.1 darran ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
1777 1.1 darran if (ii->ii_type == II_SVAR)
1778 1.1 darran ii->ii_owner = xstrdup(dw->dw_cuname);
1779 1.1 darran
1780 1.1 darran iidesc_add(dw->dw_td->td_iihash, ii);
1781 1.1 darran }
1782 1.1 darran
1783 1.1 darran /*ARGSUSED2*/
1784 1.1 darran static int
1785 1.2 darran die_fwd_resolve(tdesc_t *fwd, tdesc_t **fwdp, void *private __unused)
1786 1.1 darran {
1787 1.1 darran if (fwd->t_flags & TDESC_F_RESOLVED)
1788 1.1 darran return (1);
1789 1.1 darran
1790 1.1 darran if (fwd->t_tdesc != NULL) {
1791 1.1 darran debug(3, "tdp %u: unforwarded %s\n", fwd->t_id,
1792 1.1 darran tdesc_name(fwd));
1793 1.1 darran *fwdp = fwd->t_tdesc;
1794 1.1 darran }
1795 1.1 darran
1796 1.1 darran fwd->t_flags |= TDESC_F_RESOLVED;
1797 1.1 darran
1798 1.1 darran return (1);
1799 1.1 darran }
1800 1.1 darran
1801 1.1 darran /*ARGSUSED*/
1802 1.1 darran static void
1803 1.2 darran die_lexblk_descend(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off __unused, tdesc_t *tdp __unused)
1804 1.1 darran {
1805 1.1 darran Dwarf_Die child = die_child(dw, die);
1806 1.1 darran
1807 1.1 darran if (child != NULL)
1808 1.1 darran die_create(dw, child);
1809 1.1 darran }
1810 1.1 darran
1811 1.1 darran /*
1812 1.1 darran * Used to map the die to a routine which can parse it, using the tag to do the
1813 1.1 darran * mapping. While the processing of most tags entails the creation of a tdesc,
1814 1.1 darran * there are a few which don't - primarily those which result in the creation of
1815 1.1 darran * iidescs which refer to existing tdescs.
1816 1.1 darran */
1817 1.1 darran
1818 1.1 darran #define DW_F_NOTDP 0x1 /* Don't create a tdesc for the creator */
1819 1.1 darran
1820 1.1 darran typedef struct die_creator {
1821 1.1 darran Dwarf_Half dc_tag;
1822 1.1 darran uint16_t dc_flags;
1823 1.1 darran void (*dc_create)(dwarf_t *, Dwarf_Die, Dwarf_Off, tdesc_t *);
1824 1.1 darran } die_creator_t;
1825 1.1 darran
1826 1.1 darran static const die_creator_t die_creators[] = {
1827 1.1 darran { DW_TAG_array_type, 0, die_array_create },
1828 1.1 darran { DW_TAG_enumeration_type, 0, die_enum_create },
1829 1.1 darran { DW_TAG_lexical_block, DW_F_NOTDP, die_lexblk_descend },
1830 1.1 darran { DW_TAG_pointer_type, 0, die_pointer_create },
1831 1.20 christos { DW_TAG_reference_type, 0, die_reference_create },
1832 1.1 darran { DW_TAG_structure_type, 0, die_struct_create },
1833 1.1 darran { DW_TAG_subroutine_type, 0, die_funcptr_create },
1834 1.1 darran { DW_TAG_typedef, 0, die_typedef_create },
1835 1.1 darran { DW_TAG_union_type, 0, die_union_create },
1836 1.22 christos { DW_TAG_class_type, 0, die_class_create },
1837 1.1 darran { DW_TAG_base_type, 0, die_base_create },
1838 1.1 darran { DW_TAG_const_type, 0, die_const_create },
1839 1.1 darran { DW_TAG_subprogram, DW_F_NOTDP, die_function_create },
1840 1.1 darran { DW_TAG_variable, DW_F_NOTDP, die_variable_create },
1841 1.1 darran { DW_TAG_volatile_type, 0, die_volatile_create },
1842 1.1 darran { DW_TAG_restrict_type, 0, die_restrict_create },
1843 1.2 darran { 0, 0, NULL }
1844 1.1 darran };
1845 1.1 darran
1846 1.1 darran static const die_creator_t *
1847 1.1 darran die_tag2ctor(Dwarf_Half tag)
1848 1.1 darran {
1849 1.1 darran const die_creator_t *dc;
1850 1.1 darran
1851 1.1 darran for (dc = die_creators; dc->dc_create != NULL; dc++) {
1852 1.1 darran if (dc->dc_tag == tag)
1853 1.1 darran return (dc);
1854 1.1 darran }
1855 1.1 darran
1856 1.1 darran return (NULL);
1857 1.1 darran }
1858 1.1 darran
1859 1.1 darran static void
1860 1.1 darran die_create_one(dwarf_t *dw, Dwarf_Die die)
1861 1.1 darran {
1862 1.1 darran Dwarf_Off off = die_off(dw, die);
1863 1.1 darran const die_creator_t *dc;
1864 1.1 darran Dwarf_Half tag;
1865 1.1 darran tdesc_t *tdp;
1866 1.1 darran
1867 1.13 christos debug(3, "die %ju <0x%jx>: create_one\n", (uintmax_t)off,
1868 1.13 christos (uintmax_t)off);
1869 1.1 darran
1870 1.1 darran if (off > dw->dw_maxoff) {
1871 1.13 christos terminate("illegal die offset %ju (max %ju)\n", (uintmax_t)off,
1872 1.1 darran dw->dw_maxoff);
1873 1.1 darran }
1874 1.1 darran
1875 1.1 darran tag = die_tag(dw, die);
1876 1.1 darran
1877 1.1 darran if ((dc = die_tag2ctor(tag)) == NULL) {
1878 1.13 christos debug(2, "die %ju: ignoring tag type %x\n", (uintmax_t)off,
1879 1.12 christos tag);
1880 1.1 darran return;
1881 1.1 darran }
1882 1.1 darran
1883 1.1 darran if ((tdp = tdesc_lookup(dw, off)) == NULL &&
1884 1.1 darran !(dc->dc_flags & DW_F_NOTDP)) {
1885 1.1 darran tdp = xcalloc(sizeof (tdesc_t));
1886 1.1 darran tdp->t_id = off;
1887 1.1 darran tdesc_add(dw, tdp);
1888 1.1 darran }
1889 1.1 darran
1890 1.1 darran if (tdp != NULL)
1891 1.1 darran tdp->t_name = die_name(dw, die);
1892 1.1 darran
1893 1.1 darran dc->dc_create(dw, die, off, tdp);
1894 1.1 darran }
1895 1.1 darran
1896 1.1 darran static void
1897 1.1 darran die_create(dwarf_t *dw, Dwarf_Die die)
1898 1.1 darran {
1899 1.1 darran do {
1900 1.1 darran die_create_one(dw, die);
1901 1.1 darran } while ((die = die_sibling(dw, die)) != NULL);
1902 1.1 darran }
1903 1.1 darran
1904 1.1 darran static tdtrav_cb_f die_resolvers[] = {
1905 1.1 darran NULL,
1906 1.1 darran NULL, /* intrinsic */
1907 1.1 darran NULL, /* pointer */
1908 1.20 christos NULL, /* reference */
1909 1.1 darran die_array_resolve, /* array */
1910 1.1 darran NULL, /* function */
1911 1.1 darran die_sou_resolve, /* struct */
1912 1.1 darran die_sou_resolve, /* union */
1913 1.22 christos die_sou_resolve, /* class */
1914 1.1 darran die_enum_resolve, /* enum */
1915 1.1 darran die_fwd_resolve, /* forward */
1916 1.1 darran NULL, /* typedef */
1917 1.1 darran NULL, /* typedef unres */
1918 1.1 darran NULL, /* volatile */
1919 1.1 darran NULL, /* const */
1920 1.1 darran NULL, /* restrict */
1921 1.1 darran };
1922 1.1 darran
1923 1.1 darran static tdtrav_cb_f die_fail_reporters[] = {
1924 1.1 darran NULL,
1925 1.1 darran NULL, /* intrinsic */
1926 1.1 darran NULL, /* pointer */
1927 1.20 christos NULL, /* reference */
1928 1.1 darran die_array_failed, /* array */
1929 1.1 darran NULL, /* function */
1930 1.1 darran die_sou_failed, /* struct */
1931 1.1 darran die_sou_failed, /* union */
1932 1.22 christos die_sou_failed, /* class */
1933 1.1 darran NULL, /* enum */
1934 1.1 darran NULL, /* forward */
1935 1.1 darran NULL, /* typedef */
1936 1.1 darran NULL, /* typedef unres */
1937 1.1 darran NULL, /* volatile */
1938 1.1 darran NULL, /* const */
1939 1.1 darran NULL, /* restrict */
1940 1.1 darran };
1941 1.1 darran
1942 1.1 darran static void
1943 1.1 darran die_resolve(dwarf_t *dw)
1944 1.1 darran {
1945 1.1 darran int last = -1;
1946 1.1 darran int pass = 0;
1947 1.1 darran
1948 1.1 darran do {
1949 1.1 darran pass++;
1950 1.1 darran dw->dw_nunres = 0;
1951 1.1 darran
1952 1.1 darran (void) iitraverse_hash(dw->dw_td->td_iihash,
1953 1.1 darran &dw->dw_td->td_curvgen, NULL, NULL, die_resolvers, dw);
1954 1.1 darran
1955 1.1 darran debug(3, "resolve: pass %d, %u left\n", pass, dw->dw_nunres);
1956 1.1 darran
1957 1.2 darran if ((int) dw->dw_nunres == last) {
1958 1.1 darran fprintf(stderr, "%s: failed to resolve the following "
1959 1.1 darran "types:\n", progname);
1960 1.1 darran
1961 1.1 darran (void) iitraverse_hash(dw->dw_td->td_iihash,
1962 1.1 darran &dw->dw_td->td_curvgen, NULL, NULL,
1963 1.1 darran die_fail_reporters, dw);
1964 1.1 darran
1965 1.1 darran terminate("failed to resolve types\n");
1966 1.1 darran }
1967 1.1 darran
1968 1.1 darran last = dw->dw_nunres;
1969 1.1 darran
1970 1.1 darran } while (dw->dw_nunres != 0);
1971 1.1 darran }
1972 1.1 darran
1973 1.8 christos /*
1974 1.8 christos * Any object containing a function or object symbol at any scope should also
1975 1.8 christos * contain DWARF data.
1976 1.8 christos */
1977 1.8 christos static boolean_t
1978 1.8 christos should_have_dwarf(Elf *elf)
1979 1.8 christos {
1980 1.8 christos Elf_Scn *scn = NULL;
1981 1.8 christos Elf_Data *data = NULL;
1982 1.8 christos GElf_Shdr shdr;
1983 1.8 christos GElf_Sym sym;
1984 1.8 christos uint32_t symdx = 0;
1985 1.8 christos size_t nsyms = 0;
1986 1.8 christos boolean_t found = B_FALSE;
1987 1.8 christos
1988 1.8 christos while ((scn = elf_nextscn(elf, scn)) != NULL) {
1989 1.8 christos gelf_getshdr(scn, &shdr);
1990 1.8 christos
1991 1.8 christos if (shdr.sh_type == SHT_SYMTAB) {
1992 1.8 christos found = B_TRUE;
1993 1.8 christos break;
1994 1.8 christos }
1995 1.8 christos }
1996 1.8 christos
1997 1.8 christos if (!found)
1998 1.8 christos terminate("cannot convert stripped objects\n");
1999 1.8 christos
2000 1.8 christos data = elf_getdata(scn, NULL);
2001 1.8 christos nsyms = shdr.sh_size / shdr.sh_entsize;
2002 1.8 christos
2003 1.8 christos for (symdx = 0; symdx < nsyms; symdx++) {
2004 1.8 christos gelf_getsym(data, symdx, &sym);
2005 1.8 christos
2006 1.8 christos if ((GELF_ST_TYPE(sym.st_info) == STT_FUNC) ||
2007 1.8 christos (GELF_ST_TYPE(sym.st_info) == STT_TLS) ||
2008 1.8 christos (GELF_ST_TYPE(sym.st_info) == STT_OBJECT)) {
2009 1.8 christos char *name;
2010 1.8 christos
2011 1.8 christos name = elf_strptr(elf, shdr.sh_link, sym.st_name);
2012 1.8 christos
2013 1.8 christos /* Studio emits these local symbols regardless */
2014 1.8 christos if ((strcmp(name, "Bbss.bss") != 0) &&
2015 1.8 christos (strcmp(name, "Ttbss.bss") != 0) &&
2016 1.8 christos (strcmp(name, "Ddata.data") != 0) &&
2017 1.8 christos (strcmp(name, "Ttdata.data") != 0) &&
2018 1.8 christos (strcmp(name, "Drodata.rodata") != 0))
2019 1.8 christos return (B_TRUE);
2020 1.8 christos }
2021 1.8 christos }
2022 1.8 christos
2023 1.8 christos return (B_FALSE);
2024 1.8 christos }
2025 1.8 christos
2026 1.1 darran /*ARGSUSED*/
2027 1.1 darran int
2028 1.2 darran dw_read(tdata_t *td, Elf *elf, char *filename __unused)
2029 1.1 darran {
2030 1.26.8.1 perseant Dwarf_Unsigned hdrlen, lang, nxthdr;
2031 1.12 christos Dwarf_Off abboff;
2032 1.8 christos Dwarf_Half vers, addrsz, offsz;
2033 1.2 darran Dwarf_Die cu = 0;
2034 1.2 darran Dwarf_Die child = 0;
2035 1.1 darran dwarf_t dw;
2036 1.1 darran char *prod = NULL;
2037 1.1 darran int rc;
2038 1.1 darran
2039 1.1 darran bzero(&dw, sizeof (dwarf_t));
2040 1.1 darran dw.dw_td = td;
2041 1.1 darran dw.dw_ptrsz = elf_ptrsz(elf);
2042 1.1 darran dw.dw_mfgtid_last = TID_MFGTID_BASE;
2043 1.1 darran dw.dw_tidhash = hash_new(TDESC_HASH_BUCKETS, tdesc_idhash, tdesc_idcmp);
2044 1.1 darran dw.dw_fwdhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
2045 1.1 darran tdesc_namecmp);
2046 1.1 darran dw.dw_enumhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
2047 1.1 darran tdesc_namecmp);
2048 1.1 darran
2049 1.8 christos if ((rc = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dw.dw_dw,
2050 1.1 darran &dw.dw_err)) == DW_DLV_NO_ENTRY) {
2051 1.8 christos /* The new library does that */
2052 1.8 christos if (dwarf_errno(dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) {
2053 1.8 christos /*
2054 1.8 christos * There's no type data in the DWARF section, but
2055 1.8 christos * libdwarf is too clever to handle that properly.
2056 1.8 christos */
2057 1.8 christos return (0);
2058 1.8 christos }
2059 1.8 christos if (should_have_dwarf(elf)) {
2060 1.8 christos errno = ENOENT;
2061 1.8 christos return (-1);
2062 1.8 christos } else {
2063 1.8 christos return (0);
2064 1.8 christos }
2065 1.1 darran } else if (rc != DW_DLV_OK) {
2066 1.8 christos if (dwarf_errno(dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) {
2067 1.1 darran /*
2068 1.1 darran * There's no type data in the DWARF section, but
2069 1.1 darran * libdwarf is too clever to handle that properly.
2070 1.1 darran */
2071 1.1 darran return (0);
2072 1.1 darran }
2073 1.1 darran
2074 1.1 darran terminate("failed to initialize DWARF: %s\n",
2075 1.8 christos dwarf_errmsg(dw.dw_err));
2076 1.1 darran }
2077 1.1 darran
2078 1.8 christos if ((rc = dwarf_next_cu_header_b(dw.dw_dw, &hdrlen, &vers, &abboff,
2079 1.16 christos &addrsz, &offsz, NULL, &nxthdr, &dw.dw_err)) != DW_DLV_OK) {
2080 1.16 christos if (dwarf_errno(dw.dw_err) == DW_DLE_NO_ENTRY) {
2081 1.16 christos /*
2082 1.16 christos * There's no DWARF section...
2083 1.16 christos */
2084 1.16 christos return (0);
2085 1.16 christos }
2086 1.8 christos terminate("rc = %d %s\n", rc, dwarf_errmsg(dw.dw_err));
2087 1.16 christos }
2088 1.2 darran
2089 1.10 christos if ((cu = die_sibling(&dw, NULL)) == NULL)
2090 1.10 christos goto out;
2091 1.10 christos
2092 1.10 christos if ((child = die_child(&dw, cu)) == NULL) {
2093 1.26.8.1 perseant Dwarf_Unsigned llang;
2094 1.26.8.1 perseant if (die_unsigned(&dw, cu, DW_AT_language, &llang, 0)) {
2095 1.26.8.1 perseant debug(1, "DWARF language: %ju\n", (uintmax_t)llang);
2096 1.10 christos /*
2097 1.10 christos * Assembly languages are typically that.
2098 1.10 christos * They have some dwarf info, but not what
2099 1.10 christos * we expect. They have local symbols for
2100 1.10 christos * example, but they are missing the child info.
2101 1.10 christos */
2102 1.26.8.1 perseant if (llang >= DW_LANG_lo_user)
2103 1.10 christos return 0;
2104 1.10 christos }
2105 1.10 christos if (should_have_dwarf(elf))
2106 1.10 christos goto out;
2107 1.10 christos }
2108 1.10 christos
2109 1.10 christos if (child == NULL)
2110 1.8 christos return (0);
2111 1.1 darran
2112 1.1 darran dw.dw_maxoff = nxthdr - 1;
2113 1.1 darran
2114 1.1 darran if (dw.dw_maxoff > TID_FILEMAX)
2115 1.1 darran terminate("file contains too many types\n");
2116 1.1 darran
2117 1.1 darran debug(1, "DWARF version: %d\n", vers);
2118 1.8 christos if (vers < 2 || vers > 4) {
2119 1.1 darran terminate("file contains incompatible version %d DWARF code "
2120 1.8 christos "(version 2, 3 or 4 required)\n", vers);
2121 1.1 darran }
2122 1.1 darran
2123 1.1 darran if (die_string(&dw, cu, DW_AT_producer, &prod, 0)) {
2124 1.1 darran debug(1, "DWARF emitter: %s\n", prod);
2125 1.1 darran free(prod);
2126 1.1 darran }
2127 1.1 darran
2128 1.26.8.1 perseant if (dwarf_attrval_unsigned(cu, DW_AT_language, &lang, &dw.dw_err) == 0)
2129 1.26.8.1 perseant switch (lang) {
2130 1.26.8.1 perseant case DW_LANG_C:
2131 1.26.8.1 perseant case DW_LANG_C89:
2132 1.26.8.1 perseant case DW_LANG_C99:
2133 1.26.8.1 perseant case DW_LANG_C11:
2134 1.26.8.1 perseant case DW_LANG_C_plus_plus:
2135 1.26.8.1 perseant case DW_LANG_C_plus_plus_03:
2136 1.26.8.1 perseant case DW_LANG_C_plus_plus_11:
2137 1.26.8.1 perseant case DW_LANG_C_plus_plus_14:
2138 1.26.8.1 perseant case DW_LANG_Mips_Assembler:
2139 1.26.8.1 perseant break;
2140 1.26.8.1 perseant default:
2141 1.26.8.1 perseant terminate("file contains DWARF for unsupported "
2142 1.26.8.1 perseant "language %#llx", (unsigned long long)lang);
2143 1.26.8.1 perseant }
2144 1.26.8.1 perseant else
2145 1.26.8.1 perseant warning("die %llu: failed to get language attribute: %s\n",
2146 1.26.8.1 perseant (unsigned long long)die_off(&dw, cu), dwarf_errmsg(dw.dw_err));
2147 1.26.8.1 perseant
2148 1.1 darran if ((dw.dw_cuname = die_name(&dw, cu)) != NULL) {
2149 1.1 darran char *base = xstrdup(basename(dw.dw_cuname));
2150 1.1 darran free(dw.dw_cuname);
2151 1.1 darran dw.dw_cuname = base;
2152 1.1 darran
2153 1.1 darran debug(1, "CU name: %s\n", dw.dw_cuname);
2154 1.1 darran }
2155 1.1 darran
2156 1.2 darran if ((child = die_child(&dw, cu)) != NULL)
2157 1.2 darran die_create(&dw, child);
2158 1.1 darran
2159 1.8 christos if ((rc = dwarf_next_cu_header_b(dw.dw_dw, &hdrlen, &vers, &abboff,
2160 1.8 christos &addrsz, &offsz, NULL, &nxthdr, &dw.dw_err)) != DW_DLV_NO_ENTRY)
2161 1.1 darran terminate("multiple compilation units not supported\n");
2162 1.1 darran
2163 1.8 christos (void) dwarf_finish(dw.dw_dw, &dw.dw_err);
2164 1.1 darran
2165 1.1 darran die_resolve(&dw);
2166 1.1 darran
2167 1.1 darran cvt_fixups(td, dw.dw_ptrsz);
2168 1.1 darran
2169 1.1 darran /* leak the dwarf_t */
2170 1.1 darran
2171 1.1 darran return (0);
2172 1.10 christos out:
2173 1.10 christos terminate("file does not contain dwarf type data "
2174 1.10 christos "(try compiling with -g)\n");
2175 1.15 christos return -1;
2176 1.1 darran }
2177