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