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