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