xref: /titanic_52/usr/src/tools/ctf/cvt/dwarf.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54d232658Sjohnlev  * Common Development and Distribution License (the "License").
64d232658Sjohnlev  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * DWARF to tdata conversion
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * For the most part, conversion is straightforward, proceeding in two passes.
327c478bd9Sstevel@tonic-gate  * On the first pass, we iterate through every die, creating new type nodes as
337c478bd9Sstevel@tonic-gate  * necessary.  Referenced tdesc_t's are created in an uninitialized state, thus
347c478bd9Sstevel@tonic-gate  * allowing type reference pointers to be filled in.  If the tdesc_t
357c478bd9Sstevel@tonic-gate  * corresponding to a given die can be completely filled out (sizes and offsets
367c478bd9Sstevel@tonic-gate  * calculated, and so forth) without using any referenced types, the tdesc_t is
377c478bd9Sstevel@tonic-gate  * marked as resolved.  Consider an array type.  If the type corresponding to
387c478bd9Sstevel@tonic-gate  * the array contents has not yet been processed, we will create a blank tdesc
397c478bd9Sstevel@tonic-gate  * for the contents type (only the type ID will be filled in, relying upon the
407c478bd9Sstevel@tonic-gate  * later portion of the first pass to encounter and complete the referenced
417c478bd9Sstevel@tonic-gate  * type).  We will then attempt to determine the size of the array.  If the
427c478bd9Sstevel@tonic-gate  * array has a byte size attribute, we will have completely characterized the
437c478bd9Sstevel@tonic-gate  * array type, and will be able to mark it as resolved.  The lack of a byte
447c478bd9Sstevel@tonic-gate  * size attribute, on the other hand, will prevent us from fully resolving the
457c478bd9Sstevel@tonic-gate  * type, as the size will only be calculable with reference to the contents
467c478bd9Sstevel@tonic-gate  * type, which has not, as yet, been encountered.  The array type will thus be
477c478bd9Sstevel@tonic-gate  * left without the resolved flag, and the first pass will continue.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * When we begin the second pass, we will have created tdesc_t nodes for every
507c478bd9Sstevel@tonic-gate  * type in the section.  We will traverse the tree, from the iidescs down,
517c478bd9Sstevel@tonic-gate  * processing each unresolved node.  As the referenced nodes will have been
527c478bd9Sstevel@tonic-gate  * populated, the array type used in our example above will be able to use the
537c478bd9Sstevel@tonic-gate  * size of the referenced types (if available) to determine its own type.  The
547c478bd9Sstevel@tonic-gate  * traversal will be repeated until all types have been resolved or we have
557c478bd9Sstevel@tonic-gate  * failed to make progress.  When all tdescs have been resolved, the conversion
567c478bd9Sstevel@tonic-gate  * is complete.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * There are, as always, a few special cases that are handled during the first
597c478bd9Sstevel@tonic-gate  * and second passes:
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  *  1. Empty enums - GCC will occasionally emit an enum without any members.
627c478bd9Sstevel@tonic-gate  *     Later on in the file, it will emit the same enum type, though this time
637c478bd9Sstevel@tonic-gate  *     with the full complement of members.  All references to the memberless
647c478bd9Sstevel@tonic-gate  *     enum need to be redirected to the full definition.  During the first
657c478bd9Sstevel@tonic-gate  *     pass, each enum is entered in dm_enumhash, along with a pointer to its
667c478bd9Sstevel@tonic-gate  *     corresponding tdesc_t.  If, during the second pass, we encounter a
677c478bd9Sstevel@tonic-gate  *     memberless enum, we use the hash to locate the full definition.  All
687c478bd9Sstevel@tonic-gate  *     tdescs referencing the empty enum are then redirected.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  *  2. Forward declarations - If the compiler sees a forward declaration for
717c478bd9Sstevel@tonic-gate  *     a structure, followed by the definition of that structure, it will emit
727c478bd9Sstevel@tonic-gate  *     DWARF data for both the forward declaration and the definition.  We need
737c478bd9Sstevel@tonic-gate  *     to resolve the forward declarations when possible, by redirecting
747c478bd9Sstevel@tonic-gate  *     forward-referencing tdescs to the actual struct/union definitions.  This
757c478bd9Sstevel@tonic-gate  *     redirection is done completely within the first pass.  We begin by
767c478bd9Sstevel@tonic-gate  *     recording all forward declarations in dw_fwdhash.  When we define a
777c478bd9Sstevel@tonic-gate  *     structure, we check to see if there have been any corresponding forward
787c478bd9Sstevel@tonic-gate  *     declarations.  If so, we redirect the tdescs which referenced the forward
797c478bd9Sstevel@tonic-gate  *     declarations to the structure or union definition.
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  * XXX see if a post traverser will allow the elimination of repeated pass 2
827c478bd9Sstevel@tonic-gate  * traversals.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #include <stdio.h>
867c478bd9Sstevel@tonic-gate #include <stdlib.h>
877c478bd9Sstevel@tonic-gate #include <strings.h>
887c478bd9Sstevel@tonic-gate #include <errno.h>
897c478bd9Sstevel@tonic-gate #include <libelf.h>
907c478bd9Sstevel@tonic-gate #include <libdwarf.h>
917c478bd9Sstevel@tonic-gate #include <libgen.h>
927c478bd9Sstevel@tonic-gate #include <dwarf.h>
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #include "ctf_headers.h"
957c478bd9Sstevel@tonic-gate #include "ctftools.h"
967c478bd9Sstevel@tonic-gate #include "memory.h"
977c478bd9Sstevel@tonic-gate #include "list.h"
987c478bd9Sstevel@tonic-gate #include "traverse.h"
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /* The version of DWARF which we support. */
1017c478bd9Sstevel@tonic-gate #define	DWARF_VERSION	2
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * We need to define a couple of our own intrinsics, to smooth out some of the
1057c478bd9Sstevel@tonic-gate  * differences between the GCC and DevPro DWARF emitters.  See the referenced
1067c478bd9Sstevel@tonic-gate  * routines and the special cases in the file comment for more details.
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  * Type IDs are 32 bits wide.  We're going to use the top of that field to
1097c478bd9Sstevel@tonic-gate  * indicate types that we've created ourselves.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate #define	TID_FILEMAX		0x3fffffff	/* highest tid from file */
1127c478bd9Sstevel@tonic-gate #define	TID_VOID		0x40000001	/* see die_void() */
1137c478bd9Sstevel@tonic-gate #define	TID_LONG		0x40000002	/* see die_array() */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #define	TID_MFGTID_BASE		0x40000003	/* first mfg'd tid */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * To reduce the staggering amount of error-handling code that would otherwise
1197c478bd9Sstevel@tonic-gate  * be required, the attribute-retrieval routines handle most of their own
1207c478bd9Sstevel@tonic-gate  * errors.  If the following flag is supplied as the value of the `req'
1217c478bd9Sstevel@tonic-gate  * argument, they will also handle the absence of a requested attribute by
1227c478bd9Sstevel@tonic-gate  * terminating the program.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate #define	DW_ATTR_REQ	1
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #define	TDESC_HASH_BUCKETS	511
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate typedef struct dwarf {
1297c478bd9Sstevel@tonic-gate 	Dwarf_Debug dw_dw;		/* for libdwarf */
1307c478bd9Sstevel@tonic-gate 	Dwarf_Error dw_err;		/* for libdwarf */
1317c478bd9Sstevel@tonic-gate 	Dwarf_Unsigned dw_maxoff;	/* highest legal offset in this cu */
1327c478bd9Sstevel@tonic-gate 	tdata_t *dw_td;			/* root of the tdesc/iidesc tree */
1337c478bd9Sstevel@tonic-gate 	hash_t *dw_tidhash;		/* hash of tdescs by t_id */
1347c478bd9Sstevel@tonic-gate 	hash_t *dw_fwdhash;		/* hash of fwd decls by name */
1357c478bd9Sstevel@tonic-gate 	hash_t *dw_enumhash;		/* hash of memberless enums by name */
1367c478bd9Sstevel@tonic-gate 	tdesc_t *dw_void;		/* manufactured void type */
1377c478bd9Sstevel@tonic-gate 	tdesc_t *dw_long;		/* manufactured long type for arrays */
1387c478bd9Sstevel@tonic-gate 	size_t dw_ptrsz;		/* size of a pointer in this file */
1397c478bd9Sstevel@tonic-gate 	tid_t dw_mfgtid_last;		/* last mfg'd type ID used */
1407c478bd9Sstevel@tonic-gate 	uint_t dw_nunres;		/* count of unresolved types */
1417c478bd9Sstevel@tonic-gate 	char *dw_cuname;		/* name of compilation unit */
1427c478bd9Sstevel@tonic-gate } dwarf_t;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static void die_create_one(dwarf_t *, Dwarf_Die);
1457c478bd9Sstevel@tonic-gate static void die_create(dwarf_t *, Dwarf_Die);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static tid_t
1487c478bd9Sstevel@tonic-gate mfgtid_next(dwarf_t *dw)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	return (++dw->dw_mfgtid_last);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static void
1547c478bd9Sstevel@tonic-gate tdesc_add(dwarf_t *dw, tdesc_t *tdp)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	hash_add(dw->dw_tidhash, tdp);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static tdesc_t *
1607c478bd9Sstevel@tonic-gate tdesc_lookup(dwarf_t *dw, int tid)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	tdesc_t tmpl, *tdp;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	tmpl.t_id = tid;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (hash_find(dw->dw_tidhash, &tmpl, (void **)&tdp))
1677c478bd9Sstevel@tonic-gate 		return (tdp);
1687c478bd9Sstevel@tonic-gate 	else
1697c478bd9Sstevel@tonic-gate 		return (NULL);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * Resolve a tdesc down to a node which should have a size.  Returns the size,
1747c478bd9Sstevel@tonic-gate  * zero if the size hasn't yet been determined.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate static size_t
1777c478bd9Sstevel@tonic-gate tdesc_size(tdesc_t *tdp)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	for (;;) {
1807c478bd9Sstevel@tonic-gate 		switch (tdp->t_type) {
1817c478bd9Sstevel@tonic-gate 		case INTRINSIC:
1827c478bd9Sstevel@tonic-gate 		case POINTER:
1837c478bd9Sstevel@tonic-gate 		case ARRAY:
1847c478bd9Sstevel@tonic-gate 		case FUNCTION:
1857c478bd9Sstevel@tonic-gate 		case STRUCT:
1867c478bd9Sstevel@tonic-gate 		case UNION:
1877c478bd9Sstevel@tonic-gate 		case ENUM:
1887c478bd9Sstevel@tonic-gate 			return (tdp->t_size);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		case FORWARD:
1917c478bd9Sstevel@tonic-gate 			return (0);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		case TYPEDEF:
1947c478bd9Sstevel@tonic-gate 		case VOLATILE:
1957c478bd9Sstevel@tonic-gate 		case CONST:
1967c478bd9Sstevel@tonic-gate 		case RESTRICT:
1977c478bd9Sstevel@tonic-gate 			tdp = tdp->t_tdesc;
1987c478bd9Sstevel@tonic-gate 			continue;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		case 0: /* not yet defined */
2017c478bd9Sstevel@tonic-gate 			return (0);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		default:
2047c478bd9Sstevel@tonic-gate 			terminate("tdp %u: tdesc_size on unknown type %d\n",
2057c478bd9Sstevel@tonic-gate 			    tdp->t_id, tdp->t_type);
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate static size_t
2117c478bd9Sstevel@tonic-gate tdesc_bitsize(tdesc_t *tdp)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	for (;;) {
2147c478bd9Sstevel@tonic-gate 		switch (tdp->t_type) {
2157c478bd9Sstevel@tonic-gate 		case INTRINSIC:
2167c478bd9Sstevel@tonic-gate 			return (tdp->t_intr->intr_nbits);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		case ARRAY:
2197c478bd9Sstevel@tonic-gate 		case FUNCTION:
2207c478bd9Sstevel@tonic-gate 		case STRUCT:
2217c478bd9Sstevel@tonic-gate 		case UNION:
2227c478bd9Sstevel@tonic-gate 		case ENUM:
2237c478bd9Sstevel@tonic-gate 		case POINTER:
224626fe1bfSsethg 			return (tdp->t_size * NBBY);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		case FORWARD:
2277c478bd9Sstevel@tonic-gate 			return (0);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		case TYPEDEF:
2307c478bd9Sstevel@tonic-gate 		case VOLATILE:
2317c478bd9Sstevel@tonic-gate 		case RESTRICT:
2327c478bd9Sstevel@tonic-gate 		case CONST:
2337c478bd9Sstevel@tonic-gate 			tdp = tdp->t_tdesc;
2347c478bd9Sstevel@tonic-gate 			continue;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		case 0: /* not yet defined */
2377c478bd9Sstevel@tonic-gate 			return (0);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		default:
2407c478bd9Sstevel@tonic-gate 			terminate("tdp %u: tdesc_bitsize on unknown type %d\n",
2417c478bd9Sstevel@tonic-gate 			    tdp->t_id, tdp->t_type);
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate static tdesc_t *
2477c478bd9Sstevel@tonic-gate tdesc_basetype(tdesc_t *tdp)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	for (;;) {
2507c478bd9Sstevel@tonic-gate 		switch (tdp->t_type) {
2517c478bd9Sstevel@tonic-gate 		case TYPEDEF:
2527c478bd9Sstevel@tonic-gate 		case VOLATILE:
2537c478bd9Sstevel@tonic-gate 		case RESTRICT:
2547c478bd9Sstevel@tonic-gate 		case CONST:
2557c478bd9Sstevel@tonic-gate 			tdp = tdp->t_tdesc;
2567c478bd9Sstevel@tonic-gate 			break;
2577c478bd9Sstevel@tonic-gate 		case 0: /* not yet defined */
2587c478bd9Sstevel@tonic-gate 			return (NULL);
2597c478bd9Sstevel@tonic-gate 		default:
2607c478bd9Sstevel@tonic-gate 			return (tdp);
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate static Dwarf_Off
2667c478bd9Sstevel@tonic-gate die_off(dwarf_t *dw, Dwarf_Die die)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	Dwarf_Off off;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (dwarf_dieoffset(die, &off, &dw->dw_err) == DW_DLV_OK)
2717c478bd9Sstevel@tonic-gate 		return (off);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	terminate("failed to get offset for die: %s\n",
2747c478bd9Sstevel@tonic-gate 	    dwarf_errmsg(dw->dw_err));
2757c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2767c478bd9Sstevel@tonic-gate 	return (0);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate static Dwarf_Die
2807c478bd9Sstevel@tonic-gate die_sibling(dwarf_t *dw, Dwarf_Die die)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	Dwarf_Die sib;
2837c478bd9Sstevel@tonic-gate 	int rc;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if ((rc = dwarf_siblingof(dw->dw_dw, die, &sib, &dw->dw_err)) ==
2867c478bd9Sstevel@tonic-gate 	    DW_DLV_OK)
2877c478bd9Sstevel@tonic-gate 		return (sib);
2887c478bd9Sstevel@tonic-gate 	else if (rc == DW_DLV_NO_ENTRY)
2897c478bd9Sstevel@tonic-gate 		return (NULL);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	terminate("die %llu: failed to find type sibling: %s\n",
2927c478bd9Sstevel@tonic-gate 	    die_off(dw, die), dwarf_errmsg(dw->dw_err));
2937c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2947c478bd9Sstevel@tonic-gate 	return (NULL);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static Dwarf_Die
2987c478bd9Sstevel@tonic-gate die_child(dwarf_t *dw, Dwarf_Die die)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	Dwarf_Die child;
3017c478bd9Sstevel@tonic-gate 	int rc;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if ((rc = dwarf_child(die, &child, &dw->dw_err)) == DW_DLV_OK)
3047c478bd9Sstevel@tonic-gate 		return (child);
3057c478bd9Sstevel@tonic-gate 	else if (rc == DW_DLV_NO_ENTRY)
3067c478bd9Sstevel@tonic-gate 		return (NULL);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	terminate("die %llu: failed to find type child: %s\n",
3097c478bd9Sstevel@tonic-gate 	    die_off(dw, die), dwarf_errmsg(dw->dw_err));
3107c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3117c478bd9Sstevel@tonic-gate 	return (NULL);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate static Dwarf_Half
3157c478bd9Sstevel@tonic-gate die_tag(dwarf_t *dw, Dwarf_Die die)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	Dwarf_Half tag;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (dwarf_tag(die, &tag, &dw->dw_err) == DW_DLV_OK)
3207c478bd9Sstevel@tonic-gate 		return (tag);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	terminate("die %llu: failed to get tag for type: %s\n",
3237c478bd9Sstevel@tonic-gate 	    die_off(dw, die), dwarf_errmsg(dw->dw_err));
3247c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3257c478bd9Sstevel@tonic-gate 	return (0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate static Dwarf_Attribute
3297c478bd9Sstevel@tonic-gate die_attr(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, int req)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	Dwarf_Attribute attr;
3327c478bd9Sstevel@tonic-gate 	int rc;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if ((rc = dwarf_attr(die, name, &attr, &dw->dw_err)) == DW_DLV_OK) {
3357c478bd9Sstevel@tonic-gate 		return (attr);
3367c478bd9Sstevel@tonic-gate 	} else if (rc == DW_DLV_NO_ENTRY) {
3377c478bd9Sstevel@tonic-gate 		if (req) {
3387c478bd9Sstevel@tonic-gate 			terminate("die %llu: no attr 0x%x\n", die_off(dw, die),
3397c478bd9Sstevel@tonic-gate 			    name);
3407c478bd9Sstevel@tonic-gate 		} else {
3417c478bd9Sstevel@tonic-gate 			return (NULL);
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	terminate("die %llu: failed to get attribute for type: %s\n",
3467c478bd9Sstevel@tonic-gate 	    die_off(dw, die), dwarf_errmsg(dw->dw_err));
3477c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3487c478bd9Sstevel@tonic-gate 	return (NULL);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static Dwarf_Half
3527c478bd9Sstevel@tonic-gate die_attr_form(dwarf_t *dw, Dwarf_Attribute attr)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	Dwarf_Half form;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (dwarf_whatform(attr, &form, &dw->dw_err) == DW_DLV_OK)
3577c478bd9Sstevel@tonic-gate 		return (form);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	terminate("failed to get attribute form for type: %s\n",
3607c478bd9Sstevel@tonic-gate 	    dwarf_errmsg(dw->dw_err));
3617c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3627c478bd9Sstevel@tonic-gate 	return (0);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate static int
3667c478bd9Sstevel@tonic-gate die_signed(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Signed *valp,
3677c478bd9Sstevel@tonic-gate     int req)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	Dwarf_Attribute attr;
3707c478bd9Sstevel@tonic-gate 	Dwarf_Signed val;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	if ((attr = die_attr(dw, die, name, req)) == NULL)
3737c478bd9Sstevel@tonic-gate 		return (0); /* die_attr will terminate for us if necessary */
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (dwarf_formsdata(attr, &val, &dw->dw_err) != DW_DLV_OK) {
3767c478bd9Sstevel@tonic-gate 		terminate("die %llu: failed to get signed (form 0x%x)\n",
3777c478bd9Sstevel@tonic-gate 		    die_off(dw, die), die_attr_form(dw, attr));
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	*valp = val;
3837c478bd9Sstevel@tonic-gate 	return (1);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate static int
3877c478bd9Sstevel@tonic-gate die_unsigned(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Unsigned *valp,
3887c478bd9Sstevel@tonic-gate     int req)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	Dwarf_Attribute attr;
3917c478bd9Sstevel@tonic-gate 	Dwarf_Unsigned val;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if ((attr = die_attr(dw, die, name, req)) == NULL)
3947c478bd9Sstevel@tonic-gate 		return (0); /* die_attr will terminate for us if necessary */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (dwarf_formudata(attr, &val, &dw->dw_err) != DW_DLV_OK) {
3977c478bd9Sstevel@tonic-gate 		terminate("die %llu: failed to get unsigned (form 0x%x)\n",
3987c478bd9Sstevel@tonic-gate 		    die_off(dw, die), die_attr_form(dw, attr));
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	*valp = val;
4047c478bd9Sstevel@tonic-gate 	return (1);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate static int
4087c478bd9Sstevel@tonic-gate die_bool(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Bool *valp, int req)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	Dwarf_Attribute attr;
4117c478bd9Sstevel@tonic-gate 	Dwarf_Bool val;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	if ((attr = die_attr(dw, die, name, req)) == NULL)
4147c478bd9Sstevel@tonic-gate 		return (0); /* die_attr will terminate for us if necessary */
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (dwarf_formflag(attr, &val, &dw->dw_err) != DW_DLV_OK) {
4177c478bd9Sstevel@tonic-gate 		terminate("die %llu: failed to get bool (form 0x%x)\n",
4187c478bd9Sstevel@tonic-gate 		    die_off(dw, die), die_attr_form(dw, attr));
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	*valp = val;
4247c478bd9Sstevel@tonic-gate 	return (1);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate static int
4287c478bd9Sstevel@tonic-gate die_string(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, char **strp, int req)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	Dwarf_Attribute attr;
4317c478bd9Sstevel@tonic-gate 	char *str;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if ((attr = die_attr(dw, die, name, req)) == NULL)
4347c478bd9Sstevel@tonic-gate 		return (0); /* die_attr will terminate for us if necessary */
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (dwarf_formstring(attr, &str, &dw->dw_err) != DW_DLV_OK) {
4377c478bd9Sstevel@tonic-gate 		terminate("die %llu: failed to get string (form 0x%x)\n",
4387c478bd9Sstevel@tonic-gate 		    die_off(dw, die), die_attr_form(dw, attr));
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	*strp = xstrdup(str);
4427c478bd9Sstevel@tonic-gate 	dwarf_dealloc(dw->dw_dw, str, DW_DLA_STRING);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	return (1);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate static Dwarf_Off
4487c478bd9Sstevel@tonic-gate die_attr_ref(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	Dwarf_Attribute attr;
4517c478bd9Sstevel@tonic-gate 	Dwarf_Off off;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	attr = die_attr(dw, die, name, DW_ATTR_REQ);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (dwarf_formref(attr, &off, &dw->dw_err) != DW_DLV_OK) {
4567c478bd9Sstevel@tonic-gate 		terminate("die %llu: failed to get ref (form 0x%x)\n",
4577c478bd9Sstevel@tonic-gate 		    die_off(dw, die), die_attr_form(dw, attr));
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	return (off);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate static char *
4667c478bd9Sstevel@tonic-gate die_name(dwarf_t *dw, Dwarf_Die die)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	char *str = NULL;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	(void) die_string(dw, die, DW_AT_name, &str, 0);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	return (str);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate static int
4767c478bd9Sstevel@tonic-gate die_isdecl(dwarf_t *dw, Dwarf_Die die)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	Dwarf_Bool val;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	return (die_bool(dw, die, DW_AT_declaration, &val, 0) && val);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate static int
4847c478bd9Sstevel@tonic-gate die_isglobal(dwarf_t *dw, Dwarf_Die die)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	Dwarf_Signed vis;
4877c478bd9Sstevel@tonic-gate 	Dwarf_Bool ext;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/*
4907c478bd9Sstevel@tonic-gate 	 * Some compilers (gcc) use DW_AT_external to indicate function
4917c478bd9Sstevel@tonic-gate 	 * visibility.  Others (Sun) use DW_AT_visibility.
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	if (die_signed(dw, die, DW_AT_visibility, &vis, 0))
4947c478bd9Sstevel@tonic-gate 		return (vis == DW_VIS_exported);
4957c478bd9Sstevel@tonic-gate 	else
4967c478bd9Sstevel@tonic-gate 		return (die_bool(dw, die, DW_AT_external, &ext, 0) && ext);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate static tdesc_t *
5007c478bd9Sstevel@tonic-gate die_add(dwarf_t *dw, Dwarf_Off off)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	tdesc_t *tdp = xcalloc(sizeof (tdesc_t));
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	tdp->t_id = off;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	tdesc_add(dw, tdp);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	return (tdp);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate static tdesc_t *
5127c478bd9Sstevel@tonic-gate die_lookup_pass1(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	Dwarf_Off ref = die_attr_ref(dw, die, name);
5157c478bd9Sstevel@tonic-gate 	tdesc_t *tdp;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	if ((tdp = tdesc_lookup(dw, ref)) != NULL)
5187c478bd9Sstevel@tonic-gate 		return (tdp);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	return (die_add(dw, ref));
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate static int
5247c478bd9Sstevel@tonic-gate die_mem_offset(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name,
5257c478bd9Sstevel@tonic-gate     Dwarf_Unsigned *valp, int req)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	Dwarf_Attribute attr;
5287c478bd9Sstevel@tonic-gate 	Dwarf_Locdesc *loc;
5297c478bd9Sstevel@tonic-gate 	Dwarf_Signed locnum;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if ((attr = die_attr(dw, die, name, req)) == NULL)
5327c478bd9Sstevel@tonic-gate 		return (0); /* die_attr will terminate for us if necessary */
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	if (dwarf_loclist(attr, &loc, &locnum, &dw->dw_err) != DW_DLV_OK) {
5357c478bd9Sstevel@tonic-gate 		terminate("die %llu: failed to get mem offset location list\n",
5367c478bd9Sstevel@tonic-gate 		    die_off(dw, die));
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (locnum != 1 || loc->ld_s->lr_atom != DW_OP_plus_uconst) {
5427c478bd9Sstevel@tonic-gate 		terminate("die %llu: cannot parse member offset\n",
5437c478bd9Sstevel@tonic-gate 		    die_off(dw, die));
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	*valp = loc->ld_s->lr_number;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	dwarf_dealloc(dw->dw_dw, loc->ld_s, DW_DLA_LOC_BLOCK);
5497c478bd9Sstevel@tonic-gate 	dwarf_dealloc(dw->dw_dw, loc, DW_DLA_LOCDESC);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	return (1);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate static tdesc_t *
5557c478bd9Sstevel@tonic-gate tdesc_intr_common(dwarf_t *dw, int tid, const char *name, size_t sz)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 	tdesc_t *tdp;
5587c478bd9Sstevel@tonic-gate 	intr_t *intr;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	intr = xcalloc(sizeof (intr_t));
5617c478bd9Sstevel@tonic-gate 	intr->intr_type = INTR_INT;
5627c478bd9Sstevel@tonic-gate 	intr->intr_signed = 1;
5637c478bd9Sstevel@tonic-gate 	intr->intr_nbits = sz * NBBY;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	tdp = xcalloc(sizeof (tdesc_t));
5667c478bd9Sstevel@tonic-gate 	tdp->t_name = xstrdup(name);
5677c478bd9Sstevel@tonic-gate 	tdp->t_size = sz;
5687c478bd9Sstevel@tonic-gate 	tdp->t_id = tid;
5697c478bd9Sstevel@tonic-gate 	tdp->t_type = INTRINSIC;
5707c478bd9Sstevel@tonic-gate 	tdp->t_intr = intr;
5717c478bd9Sstevel@tonic-gate 	tdp->t_flags = TDESC_F_RESOLVED;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	tdesc_add(dw, tdp);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	return (tdp);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate /*
5797c478bd9Sstevel@tonic-gate  * Manufacture a void type.  Used for gcc-emitted stabs, where the lack of a
5807c478bd9Sstevel@tonic-gate  * type reference implies a reference to a void type.  A void *, for example
5817c478bd9Sstevel@tonic-gate  * will be represented by a pointer die without a DW_AT_type.  CTF requires
5827c478bd9Sstevel@tonic-gate  * that pointer nodes point to something, so we'll create a void for use as
5837c478bd9Sstevel@tonic-gate  * the target.  Note that the DWARF data may already create a void type.  Ours
5847c478bd9Sstevel@tonic-gate  * would then be a duplicate, but it'll be removed in the self-uniquification
5857c478bd9Sstevel@tonic-gate  * merge performed at the completion of DWARF->tdesc conversion.
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate static tdesc_t *
5887c478bd9Sstevel@tonic-gate tdesc_intr_void(dwarf_t *dw)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	if (dw->dw_void == NULL)
5917c478bd9Sstevel@tonic-gate 		dw->dw_void = tdesc_intr_common(dw, TID_VOID, "void", 0);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	return (dw->dw_void);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate static tdesc_t *
5977c478bd9Sstevel@tonic-gate tdesc_intr_long(dwarf_t *dw)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 	if (dw->dw_long == NULL) {
6007c478bd9Sstevel@tonic-gate 		dw->dw_long = tdesc_intr_common(dw, TID_LONG, "long",
6017c478bd9Sstevel@tonic-gate 		    dw->dw_ptrsz);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	return (dw->dw_long);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate  * Used for creating bitfield types.  We create a copy of an existing intrinsic,
6097c478bd9Sstevel@tonic-gate  * adjusting the size of the copy to match what the caller requested.  The
6107c478bd9Sstevel@tonic-gate  * caller can then use the copy as the type for a bitfield structure member.
6117c478bd9Sstevel@tonic-gate  */
6127c478bd9Sstevel@tonic-gate static tdesc_t *
6137c478bd9Sstevel@tonic-gate tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	tdesc_t *new = xcalloc(sizeof (tdesc_t));
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (!(old->t_flags & TDESC_F_RESOLVED)) {
6187c478bd9Sstevel@tonic-gate 		terminate("tdp %u: attempt to make a bit field from an "
6197c478bd9Sstevel@tonic-gate 		    "unresolved type\n", old->t_id);
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	new->t_name = xstrdup(old->t_name);
6237c478bd9Sstevel@tonic-gate 	new->t_size = old->t_size;
6247c478bd9Sstevel@tonic-gate 	new->t_id = mfgtid_next(dw);
6257c478bd9Sstevel@tonic-gate 	new->t_type = INTRINSIC;
6267c478bd9Sstevel@tonic-gate 	new->t_flags = TDESC_F_RESOLVED;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	new->t_intr = xcalloc(sizeof (intr_t));
6297c478bd9Sstevel@tonic-gate 	bcopy(old->t_intr, new->t_intr, sizeof (intr_t));
6307c478bd9Sstevel@tonic-gate 	new->t_intr->intr_nbits = bitsz;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	tdesc_add(dw, new);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	return (new);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate static void
6387c478bd9Sstevel@tonic-gate tdesc_array_create(dwarf_t *dw, Dwarf_Die dim, tdesc_t *arrtdp,
6397c478bd9Sstevel@tonic-gate     tdesc_t *dimtdp)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	Dwarf_Unsigned uval;
6427c478bd9Sstevel@tonic-gate 	Dwarf_Signed sval;
6437c478bd9Sstevel@tonic-gate 	tdesc_t *ctdp;
6447c478bd9Sstevel@tonic-gate 	Dwarf_Die dim2;
6457c478bd9Sstevel@tonic-gate 	ardef_t *ar;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	if ((dim2 = die_sibling(dw, dim)) == NULL) {
6487c478bd9Sstevel@tonic-gate 		ctdp = arrtdp;
6497c478bd9Sstevel@tonic-gate 	} else if (die_tag(dw, dim2) == DW_TAG_subrange_type) {
6507c478bd9Sstevel@tonic-gate 		ctdp = xcalloc(sizeof (tdesc_t));
6517c478bd9Sstevel@tonic-gate 		ctdp->t_id = mfgtid_next(dw);
6527c478bd9Sstevel@tonic-gate 		debug(3, "die %llu: creating new type %u for sub-dimension\n",
6537c478bd9Sstevel@tonic-gate 		    die_off(dw, dim2), ctdp->t_id);
6547c478bd9Sstevel@tonic-gate 		tdesc_array_create(dw, dim2, arrtdp, ctdp);
6557c478bd9Sstevel@tonic-gate 	} else {
6567c478bd9Sstevel@tonic-gate 		terminate("die %llu: unexpected non-subrange node in array\n",
6577c478bd9Sstevel@tonic-gate 		    die_off(dw, dim2));
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	dimtdp->t_type = ARRAY;
6617c478bd9Sstevel@tonic-gate 	dimtdp->t_ardef = ar = xcalloc(sizeof (ardef_t));
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/*
6647c478bd9Sstevel@tonic-gate 	 * Array bounds can be signed or unsigned, but there are several kinds
6657c478bd9Sstevel@tonic-gate 	 * of signless forms (data1, data2, etc) that take their sign from the
6667c478bd9Sstevel@tonic-gate 	 * routine that is trying to interpret them.  That is, data1 can be
6677c478bd9Sstevel@tonic-gate 	 * either signed or unsigned, depending on whether you use the signed or
6687c478bd9Sstevel@tonic-gate 	 * unsigned accessor function.  GCC will use the signless forms to store
6697c478bd9Sstevel@tonic-gate 	 * unsigned values which have their high bit set, so we need to try to
6707c478bd9Sstevel@tonic-gate 	 * read them first as unsigned to get positive values.  We could also
6717c478bd9Sstevel@tonic-gate 	 * try signed first, falling back to unsigned if we got a negative
6727c478bd9Sstevel@tonic-gate 	 * value.
6737c478bd9Sstevel@tonic-gate 	 */
6747c478bd9Sstevel@tonic-gate 	if (die_unsigned(dw, dim, DW_AT_upper_bound, &uval, 0))
6757c478bd9Sstevel@tonic-gate 		ar->ad_nelems = uval + 1;
6767c478bd9Sstevel@tonic-gate 	else if (die_signed(dw, dim, DW_AT_upper_bound, &sval, 0))
6777c478bd9Sstevel@tonic-gate 		ar->ad_nelems = sval + 1;
6787c478bd9Sstevel@tonic-gate 	else
6797c478bd9Sstevel@tonic-gate 		ar->ad_nelems = 0;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	/*
6827c478bd9Sstevel@tonic-gate 	 * Different compilers use different index types.  Force the type to be
6837c478bd9Sstevel@tonic-gate 	 * a common, known value (long).
6847c478bd9Sstevel@tonic-gate 	 */
6857c478bd9Sstevel@tonic-gate 	ar->ad_idxtype = tdesc_intr_long(dw);
6867c478bd9Sstevel@tonic-gate 	ar->ad_contents = ctdp;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if (ar->ad_contents->t_size != 0) {
6897c478bd9Sstevel@tonic-gate 		dimtdp->t_size = ar->ad_contents->t_size * ar->ad_nelems;
6907c478bd9Sstevel@tonic-gate 		dimtdp->t_flags |= TDESC_F_RESOLVED;
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate /*
6957c478bd9Sstevel@tonic-gate  * Create a tdesc from an array node.  Some arrays will come with byte size
6967c478bd9Sstevel@tonic-gate  * attributes, and thus can be resolved immediately.  Others don't, and will
6977c478bd9Sstevel@tonic-gate  * need to wait until the second pass for resolution.
6987c478bd9Sstevel@tonic-gate  */
6997c478bd9Sstevel@tonic-gate static void
7007c478bd9Sstevel@tonic-gate die_array_create(dwarf_t *dw, Dwarf_Die arr, Dwarf_Off off, tdesc_t *tdp)
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate 	tdesc_t *arrtdp = die_lookup_pass1(dw, arr, DW_AT_type);
7037c478bd9Sstevel@tonic-gate 	Dwarf_Unsigned uval;
7047c478bd9Sstevel@tonic-gate 	Dwarf_Die dim;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: creating array\n", off);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if ((dim = die_child(dw, arr)) == NULL ||
7097c478bd9Sstevel@tonic-gate 	    die_tag(dw, dim) != DW_TAG_subrange_type)
7107c478bd9Sstevel@tonic-gate 		terminate("die %llu: failed to retrieve array bounds\n", off);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	tdesc_array_create(dw, dim, arrtdp, tdp);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if (die_unsigned(dw, arr, DW_AT_byte_size, &uval, 0)) {
7157c478bd9Sstevel@tonic-gate 		tdesc_t *dimtdp;
7167c478bd9Sstevel@tonic-gate 		int flags;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		tdp->t_size = uval;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 		/*
7217c478bd9Sstevel@tonic-gate 		 * Ensure that sub-dimensions have sizes too before marking
7227c478bd9Sstevel@tonic-gate 		 * as resolved.
7237c478bd9Sstevel@tonic-gate 		 */
7247c478bd9Sstevel@tonic-gate 		flags = TDESC_F_RESOLVED;
7257c478bd9Sstevel@tonic-gate 		for (dimtdp = tdp->t_ardef->ad_contents;
7267c478bd9Sstevel@tonic-gate 		    dimtdp->t_type == ARRAY;
7277c478bd9Sstevel@tonic-gate 		    dimtdp = dimtdp->t_ardef->ad_contents) {
7287c478bd9Sstevel@tonic-gate 			if (!(dimtdp->t_flags & TDESC_F_RESOLVED)) {
7297c478bd9Sstevel@tonic-gate 				flags = 0;
7307c478bd9Sstevel@tonic-gate 				break;
7317c478bd9Sstevel@tonic-gate 			}
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		tdp->t_flags |= flags;
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: array nelems %u size %u\n", off,
7387c478bd9Sstevel@tonic-gate 	    tdp->t_ardef->ad_nelems, tdp->t_size);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
7427c478bd9Sstevel@tonic-gate static int
7437c478bd9Sstevel@tonic-gate die_array_resolve(tdesc_t *tdp, tdesc_t **tdpp, void *private)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	dwarf_t *dw = private;
7467c478bd9Sstevel@tonic-gate 	size_t sz;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	if (tdp->t_flags & TDESC_F_RESOLVED)
7497c478bd9Sstevel@tonic-gate 		return (1);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	debug(3, "trying to resolve array %d (cont %d)\n", tdp->t_id,
7527c478bd9Sstevel@tonic-gate 	    tdp->t_ardef->ad_contents->t_id);
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if ((sz = tdesc_size(tdp->t_ardef->ad_contents)) == 0) {
7557c478bd9Sstevel@tonic-gate 		debug(3, "unable to resolve array %s (%d) contents %d\n",
7564d232658Sjohnlev 		    tdesc_name(tdp), tdp->t_id,
7577c478bd9Sstevel@tonic-gate 		    tdp->t_ardef->ad_contents->t_id);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		dw->dw_nunres++;
7607c478bd9Sstevel@tonic-gate 		return (1);
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	tdp->t_size = sz * tdp->t_ardef->ad_nelems;
7647c478bd9Sstevel@tonic-gate 	tdp->t_flags |= TDESC_F_RESOLVED;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	debug(3, "resolved array %d: %u bytes\n", tdp->t_id, tdp->t_size);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	return (1);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
7727c478bd9Sstevel@tonic-gate static int
7737c478bd9Sstevel@tonic-gate die_array_failed(tdesc_t *tdp, tdesc_t **tdpp, void *private)
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	tdesc_t *cont = tdp->t_ardef->ad_contents;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	if (tdp->t_flags & TDESC_F_RESOLVED)
7787c478bd9Sstevel@tonic-gate 		return (1);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Array %d: failed to size contents type %s (%d)\n",
7814d232658Sjohnlev 	    tdp->t_id, tdesc_name(cont), cont->t_id);
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	return (1);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate  * Most enums (those with members) will be resolved during this first pass.
7887c478bd9Sstevel@tonic-gate  * Others - those without members (see the file comment) - won't be, and will
7897c478bd9Sstevel@tonic-gate  * need to wait until the second pass when they can be matched with their full
7907c478bd9Sstevel@tonic-gate  * definitions.
7917c478bd9Sstevel@tonic-gate  */
7927c478bd9Sstevel@tonic-gate static void
7937c478bd9Sstevel@tonic-gate die_enum_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	Dwarf_Die mem;
7967c478bd9Sstevel@tonic-gate 	Dwarf_Unsigned uval;
7977c478bd9Sstevel@tonic-gate 	Dwarf_Signed sval;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: creating enum\n", off);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	tdp->t_type = ENUM;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	(void) die_unsigned(dw, die, DW_AT_byte_size, &uval, DW_ATTR_REQ);
8047c478bd9Sstevel@tonic-gate 	tdp->t_size = uval;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	if ((mem = die_child(dw, die)) != NULL) {
8077c478bd9Sstevel@tonic-gate 		elist_t **elastp = &tdp->t_emem;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		do {
8107c478bd9Sstevel@tonic-gate 			elist_t *el;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 			if (die_tag(dw, mem) != DW_TAG_enumerator) {
8137c478bd9Sstevel@tonic-gate 				/* Nested type declaration */
8147c478bd9Sstevel@tonic-gate 				die_create_one(dw, mem);
8157c478bd9Sstevel@tonic-gate 				continue;
8167c478bd9Sstevel@tonic-gate 			}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 			el = xcalloc(sizeof (elist_t));
8197c478bd9Sstevel@tonic-gate 			el->el_name = die_name(dw, mem);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 			if (die_signed(dw, mem, DW_AT_const_value, &sval, 0)) {
8227c478bd9Sstevel@tonic-gate 				el->el_number = sval;
8237c478bd9Sstevel@tonic-gate 			} else if (die_unsigned(dw, mem, DW_AT_const_value,
8247c478bd9Sstevel@tonic-gate 			    &uval, 0)) {
8257c478bd9Sstevel@tonic-gate 				el->el_number = uval;
8267c478bd9Sstevel@tonic-gate 			} else {
8277c478bd9Sstevel@tonic-gate 				terminate("die %llu: enum %llu: member without "
8287c478bd9Sstevel@tonic-gate 				    "value\n", off, die_off(dw, mem));
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 			debug(3, "die %llu: enum %llu: created %s = %d\n", off,
8327c478bd9Sstevel@tonic-gate 			    die_off(dw, mem), el->el_name, el->el_number);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 			*elastp = el;
8357c478bd9Sstevel@tonic-gate 			elastp = &el->el_next;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		} while ((mem = die_sibling(dw, mem)) != NULL);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		hash_add(dw->dw_enumhash, tdp);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		tdp->t_flags |= TDESC_F_RESOLVED;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 		if (tdp->t_name != NULL) {
8447c478bd9Sstevel@tonic-gate 			iidesc_t *ii = xcalloc(sizeof (iidesc_t));
8457c478bd9Sstevel@tonic-gate 			ii->ii_type = II_SOU;
8467c478bd9Sstevel@tonic-gate 			ii->ii_name = xstrdup(tdp->t_name);
8477c478bd9Sstevel@tonic-gate 			ii->ii_dtype = tdp;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 			iidesc_add(dw->dw_td->td_iihash, ii);
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate static int
8557c478bd9Sstevel@tonic-gate die_enum_match(void *arg1, void *arg2)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 	tdesc_t *tdp = arg1, **fullp = arg2;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	if (tdp->t_emem != NULL) {
8607c478bd9Sstevel@tonic-gate 		*fullp = tdp;
8617c478bd9Sstevel@tonic-gate 		return (-1); /* stop the iteration */
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	return (0);
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
8687c478bd9Sstevel@tonic-gate static int
8697c478bd9Sstevel@tonic-gate die_enum_resolve(tdesc_t *tdp, tdesc_t **tdpp, void *private)
8707c478bd9Sstevel@tonic-gate {
8717c478bd9Sstevel@tonic-gate 	dwarf_t *dw = private;
8727c478bd9Sstevel@tonic-gate 	tdesc_t *full = NULL;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	if (tdp->t_flags & TDESC_F_RESOLVED)
8757c478bd9Sstevel@tonic-gate 		return (1);
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	(void) hash_find_iter(dw->dw_enumhash, tdp, die_enum_match, &full);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	/*
8807c478bd9Sstevel@tonic-gate 	 * The answer to this one won't change from iteration to iteration,
8817c478bd9Sstevel@tonic-gate 	 * so don't even try.
8827c478bd9Sstevel@tonic-gate 	 */
8837c478bd9Sstevel@tonic-gate 	if (full == NULL) {
8844d232658Sjohnlev 		terminate("tdp %u: enum %s has no members\n", tdp->t_id,
8854d232658Sjohnlev 		    tdesc_name(tdp));
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	debug(3, "tdp %u: enum %s redirected to %u\n", tdp->t_id,
8894d232658Sjohnlev 	    tdesc_name(tdp), full->t_id);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	tdp->t_flags |= TDESC_F_RESOLVED;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	return (1);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate static int
8977c478bd9Sstevel@tonic-gate die_fwd_map(void *arg1, void *arg2)
8987c478bd9Sstevel@tonic-gate {
8997c478bd9Sstevel@tonic-gate 	tdesc_t *fwd = arg1, *sou = arg2;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	debug(3, "tdp %u: mapped forward %s to sou %u\n", fwd->t_id,
9024d232658Sjohnlev 	    tdesc_name(fwd), sou->t_id);
9037c478bd9Sstevel@tonic-gate 	fwd->t_tdesc = sou;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	return (0);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
9097c478bd9Sstevel@tonic-gate  * Structures and unions will never be resolved during the first pass, as we
9107c478bd9Sstevel@tonic-gate  * won't be able to fully determine the member sizes.  The second pass, which
9117c478bd9Sstevel@tonic-gate  * have access to sizing information, will be able to complete the resolution.
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate static void
9147c478bd9Sstevel@tonic-gate die_sou_create(dwarf_t *dw, Dwarf_Die str, Dwarf_Off off, tdesc_t *tdp,
9157c478bd9Sstevel@tonic-gate     int type, const char *typename)
9167c478bd9Sstevel@tonic-gate {
9177c478bd9Sstevel@tonic-gate 	Dwarf_Unsigned sz, bitsz, bitoff;
9187c478bd9Sstevel@tonic-gate 	Dwarf_Die mem;
9197c478bd9Sstevel@tonic-gate 	mlist_t *ml, **mlastp;
9207c478bd9Sstevel@tonic-gate 	iidesc_t *ii;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	tdp->t_type = (die_isdecl(dw, str) ? FORWARD : type);
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: creating %s %s\n", off,
9257c478bd9Sstevel@tonic-gate 	    (tdp->t_type == FORWARD ? "forward decl" : typename),
9264d232658Sjohnlev 	    tdesc_name(tdp));
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (tdp->t_type == FORWARD) {
9297c478bd9Sstevel@tonic-gate 		hash_add(dw->dw_fwdhash, tdp);
9307c478bd9Sstevel@tonic-gate 		return;
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	(void) hash_find_iter(dw->dw_fwdhash, tdp, die_fwd_map, tdp);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	(void) die_unsigned(dw, str, DW_AT_byte_size, &sz, DW_ATTR_REQ);
9367c478bd9Sstevel@tonic-gate 	tdp->t_size = sz;
9377c478bd9Sstevel@tonic-gate 
938*ae115bc7Smrj 	/*
939*ae115bc7Smrj 	 * GCC allows empty SOUs as an extension.
940*ae115bc7Smrj 	 */
9417c478bd9Sstevel@tonic-gate 	if ((mem = die_child(dw, str)) == NULL)
942*ae115bc7Smrj 		goto out;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	mlastp = &tdp->t_members;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	do {
9477c478bd9Sstevel@tonic-gate 		Dwarf_Off memoff = die_off(dw, mem);
9487c478bd9Sstevel@tonic-gate 		Dwarf_Half tag = die_tag(dw, mem);
9497c478bd9Sstevel@tonic-gate 		Dwarf_Unsigned mloff;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 		if (tag != DW_TAG_member) {
9527c478bd9Sstevel@tonic-gate 			/* Nested type declaration */
9537c478bd9Sstevel@tonic-gate 			die_create_one(dw, mem);
9547c478bd9Sstevel@tonic-gate 			continue;
9557c478bd9Sstevel@tonic-gate 		}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 		debug(3, "die %llu: mem %llu: creating member\n", off, memoff);
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		ml = xcalloc(sizeof (mlist_t));
9607c478bd9Sstevel@tonic-gate 
961e824d57fSjohnlev 		/*
962e824d57fSjohnlev 		 * This could be a GCC anon struct/union member, so we'll allow
963e824d57fSjohnlev 		 * an empty name, even though nothing can really handle them
964e824d57fSjohnlev 		 * properly.  Note that some versions of GCC miss out debug
965e824d57fSjohnlev 		 * info for anon structs, though recent versions are fixed (gcc
966e824d57fSjohnlev 		 * bug 11816).
967e824d57fSjohnlev 		 */
968e824d57fSjohnlev 		if ((ml->ml_name = die_name(dw, mem)) == NULL)
969e824d57fSjohnlev 			ml->ml_name = "";
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		ml->ml_type = die_lookup_pass1(dw, mem, DW_AT_type);
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 		if (die_mem_offset(dw, mem, DW_AT_data_member_location,
9747c478bd9Sstevel@tonic-gate 		    &mloff, 0)) {
9757c478bd9Sstevel@tonic-gate 			debug(3, "die %llu: got mloff %llx\n", off,
9767c478bd9Sstevel@tonic-gate 			    (u_longlong_t)mloff);
9777c478bd9Sstevel@tonic-gate 			ml->ml_offset = mloff * 8;
9787c478bd9Sstevel@tonic-gate 		}
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 		if (die_unsigned(dw, mem, DW_AT_bit_size, &bitsz, 0))
9817c478bd9Sstevel@tonic-gate 			ml->ml_size = bitsz;
9827c478bd9Sstevel@tonic-gate 		else
9837c478bd9Sstevel@tonic-gate 			ml->ml_size = tdesc_bitsize(ml->ml_type);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		if (die_unsigned(dw, mem, DW_AT_bit_offset, &bitoff, 0)) {
9867c478bd9Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
9877c478bd9Sstevel@tonic-gate 			ml->ml_offset += bitoff;
9887c478bd9Sstevel@tonic-gate #else
989626fe1bfSsethg 			ml->ml_offset += tdesc_bitsize(ml->ml_type) - bitoff -
990626fe1bfSsethg 			    ml->ml_size;
9917c478bd9Sstevel@tonic-gate #endif
9927c478bd9Sstevel@tonic-gate 		}
9937c478bd9Sstevel@tonic-gate 
994e824d57fSjohnlev 		debug(3, "die %llu: mem %llu: created \"%s\" (off %u sz %u)\n",
9957c478bd9Sstevel@tonic-gate 		    off, memoff, ml->ml_name, ml->ml_offset, ml->ml_size);
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 		*mlastp = ml;
9987c478bd9Sstevel@tonic-gate 		mlastp = &ml->ml_next;
9997c478bd9Sstevel@tonic-gate 	} while ((mem = die_sibling(dw, mem)) != NULL);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	/*
10027c478bd9Sstevel@tonic-gate 	 * GCC will attempt to eliminate unused types, thus decreasing the
10037c478bd9Sstevel@tonic-gate 	 * size of the emitted dwarf.  That is, if you declare a foo_t in your
10047c478bd9Sstevel@tonic-gate 	 * header, include said header in your source file, and neglect to
10057c478bd9Sstevel@tonic-gate 	 * actually use (directly or indirectly) the foo_t in the source file,
10067c478bd9Sstevel@tonic-gate 	 * the foo_t won't make it into the emitted DWARF.  So, at least, goes
10077c478bd9Sstevel@tonic-gate 	 * the theory.
10087c478bd9Sstevel@tonic-gate 	 *
10097c478bd9Sstevel@tonic-gate 	 * Occasionally, it'll emit the DW_TAG_structure_type for the foo_t,
10107c478bd9Sstevel@tonic-gate 	 * and then neglect to emit the members.  Strangely, the loner struct
10117c478bd9Sstevel@tonic-gate 	 * tag will always be followed by a proper nested declaration of
10127c478bd9Sstevel@tonic-gate 	 * something else.  This is clearly a bug, but we're not going to have
10137c478bd9Sstevel@tonic-gate 	 * time to get it fixed before this goo goes back, so we'll have to work
10147c478bd9Sstevel@tonic-gate 	 * around it.  If we see a no-membered struct with a nested declaration
10157c478bd9Sstevel@tonic-gate 	 * (i.e. die_child of the struct tag won't be null), we'll ignore it.
10167c478bd9Sstevel@tonic-gate 	 * Being paranoid, we won't simply remove it from the hash.  Instead,
10177c478bd9Sstevel@tonic-gate 	 * we'll decline to create an iidesc for it, thus ensuring that this
10187c478bd9Sstevel@tonic-gate 	 * type won't make it into the output file.  To be safe, we'll also
10197c478bd9Sstevel@tonic-gate 	 * change the name.
10207c478bd9Sstevel@tonic-gate 	 */
10217c478bd9Sstevel@tonic-gate 	if (tdp->t_members == NULL) {
10224d232658Sjohnlev 		const char *old = tdesc_name(tdp);
10237c478bd9Sstevel@tonic-gate 		size_t newsz = 7 + strlen(old) + 1;
10247c478bd9Sstevel@tonic-gate 		char *new = xmalloc(newsz);
10257c478bd9Sstevel@tonic-gate 		(void) snprintf(new, newsz, "orphan %s", old);
10267c478bd9Sstevel@tonic-gate 
10274d232658Sjohnlev 		debug(3, "die %llu: worked around %s %s\n", off, typename, old);
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 		if (tdp->t_name != NULL)
10307c478bd9Sstevel@tonic-gate 			free(tdp->t_name);
10317c478bd9Sstevel@tonic-gate 		tdp->t_name = new;
1032*ae115bc7Smrj 		return;
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 
1035*ae115bc7Smrj out:
1036*ae115bc7Smrj 	if (tdp->t_name != NULL) {
10377c478bd9Sstevel@tonic-gate 		ii = xcalloc(sizeof (iidesc_t));
10387c478bd9Sstevel@tonic-gate 		ii->ii_type = II_SOU;
10397c478bd9Sstevel@tonic-gate 		ii->ii_name = xstrdup(tdp->t_name);
10407c478bd9Sstevel@tonic-gate 		ii->ii_dtype = tdp;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 		iidesc_add(dw->dw_td->td_iihash, ii);
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate static void
10477c478bd9Sstevel@tonic-gate die_struct_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
10487c478bd9Sstevel@tonic-gate {
10497c478bd9Sstevel@tonic-gate 	die_sou_create(dw, die, off, tdp, STRUCT, "struct");
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate static void
10537c478bd9Sstevel@tonic-gate die_union_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	die_sou_create(dw, die, off, tdp, UNION, "union");
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
10597c478bd9Sstevel@tonic-gate static int
10607c478bd9Sstevel@tonic-gate die_sou_resolve(tdesc_t *tdp, tdesc_t **tdpp, void *private)
10617c478bd9Sstevel@tonic-gate {
10627c478bd9Sstevel@tonic-gate 	dwarf_t *dw = private;
10637c478bd9Sstevel@tonic-gate 	mlist_t *ml;
10647c478bd9Sstevel@tonic-gate 	tdesc_t *mt;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	if (tdp->t_flags & TDESC_F_RESOLVED)
10677c478bd9Sstevel@tonic-gate 		return (1);
10687c478bd9Sstevel@tonic-gate 
10694d232658Sjohnlev 	debug(3, "resolving sou %s\n", tdesc_name(tdp));
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {
10727c478bd9Sstevel@tonic-gate 		if (ml->ml_size == 0) {
1073*ae115bc7Smrj 			mt = tdesc_basetype(ml->ml_type);
1074*ae115bc7Smrj 
1075*ae115bc7Smrj 			if ((ml->ml_size = tdesc_bitsize(mt)) != 0)
1076*ae115bc7Smrj 				continue;
1077*ae115bc7Smrj 
1078*ae115bc7Smrj 			/*
1079*ae115bc7Smrj 			 * For empty members, or GCC/C99 flexible array
1080*ae115bc7Smrj 			 * members, a size of 0 is correct.
1081*ae115bc7Smrj 			 */
1082*ae115bc7Smrj 			if (mt->t_members == NULL)
1083*ae115bc7Smrj 				continue;
1084*ae115bc7Smrj 			if (mt->t_type == ARRAY && mt->t_ardef->ad_nelems == 0)
1085*ae115bc7Smrj 				continue;
1086*ae115bc7Smrj 
10877c478bd9Sstevel@tonic-gate 			dw->dw_nunres++;
10887c478bd9Sstevel@tonic-gate 			return (1);
10897c478bd9Sstevel@tonic-gate 		}
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		if ((mt = tdesc_basetype(ml->ml_type)) == NULL) {
10927c478bd9Sstevel@tonic-gate 			dw->dw_nunres++;
10937c478bd9Sstevel@tonic-gate 			return (1);
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		if (ml->ml_size != 0 && mt->t_type == INTRINSIC &&
10977c478bd9Sstevel@tonic-gate 		    mt->t_intr->intr_nbits != ml->ml_size) {
10987c478bd9Sstevel@tonic-gate 			/*
10997c478bd9Sstevel@tonic-gate 			 * This member is a bitfield, and needs to reference
11007c478bd9Sstevel@tonic-gate 			 * an intrinsic type with the same width.  If the
11017c478bd9Sstevel@tonic-gate 			 * currently-referenced type isn't of the same width,
11027c478bd9Sstevel@tonic-gate 			 * we'll copy it, adjusting the width of the copy to
11037c478bd9Sstevel@tonic-gate 			 * the size we'd like.
11047c478bd9Sstevel@tonic-gate 			 */
11057c478bd9Sstevel@tonic-gate 			debug(3, "tdp %u: creating bitfield for %d bits\n",
11067c478bd9Sstevel@tonic-gate 			    tdp->t_id, ml->ml_size);
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 			ml->ml_type = tdesc_intr_clone(dw, mt, ml->ml_size);
11097c478bd9Sstevel@tonic-gate 		}
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	tdp->t_flags |= TDESC_F_RESOLVED;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	return (1);
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
11187c478bd9Sstevel@tonic-gate static int
11197c478bd9Sstevel@tonic-gate die_sou_failed(tdesc_t *tdp, tdesc_t **tdpp, void *private)
11207c478bd9Sstevel@tonic-gate {
11217c478bd9Sstevel@tonic-gate 	const char *typename = (tdp->t_type == STRUCT ? "struct" : "union");
11227c478bd9Sstevel@tonic-gate 	mlist_t *ml;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	if (tdp->t_flags & TDESC_F_RESOLVED)
11257c478bd9Sstevel@tonic-gate 		return (1);
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {
11287c478bd9Sstevel@tonic-gate 		if (ml->ml_size == 0) {
1129e824d57fSjohnlev 			fprintf(stderr, "%s %d: failed to size member \"%s\" "
1130e824d57fSjohnlev 			    "of type %s (%d)\n", typename, tdp->t_id,
1131e824d57fSjohnlev 			    ml->ml_name, tdesc_name(ml->ml_type),
1132e824d57fSjohnlev 			    ml->ml_type->t_id);
11337c478bd9Sstevel@tonic-gate 		}
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	return (1);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate static void
11407c478bd9Sstevel@tonic-gate die_funcptr_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
11417c478bd9Sstevel@tonic-gate {
11427c478bd9Sstevel@tonic-gate 	Dwarf_Attribute attr;
11437c478bd9Sstevel@tonic-gate 	Dwarf_Half tag;
11447c478bd9Sstevel@tonic-gate 	Dwarf_Die arg;
11457c478bd9Sstevel@tonic-gate 	fndef_t *fn;
11467c478bd9Sstevel@tonic-gate 	int i;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: creating function pointer\n", off);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	/*
11517c478bd9Sstevel@tonic-gate 	 * We'll begin by processing any type definition nodes that may be
11527c478bd9Sstevel@tonic-gate 	 * lurking underneath this one.
11537c478bd9Sstevel@tonic-gate 	 */
11547c478bd9Sstevel@tonic-gate 	for (arg = die_child(dw, die); arg != NULL;
11557c478bd9Sstevel@tonic-gate 	    arg = die_sibling(dw, arg)) {
11567c478bd9Sstevel@tonic-gate 		if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
11577c478bd9Sstevel@tonic-gate 		    tag != DW_TAG_unspecified_parameters) {
11587c478bd9Sstevel@tonic-gate 			/* Nested type declaration */
11597c478bd9Sstevel@tonic-gate 			die_create_one(dw, arg);
11607c478bd9Sstevel@tonic-gate 		}
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	if (die_isdecl(dw, die)) {
11647c478bd9Sstevel@tonic-gate 		/*
11657c478bd9Sstevel@tonic-gate 		 * This is a prototype.  We don't add prototypes to the
11667c478bd9Sstevel@tonic-gate 		 * tree, so we're going to drop the tdesc.  Unfortunately,
11677c478bd9Sstevel@tonic-gate 		 * it has already been added to the tree.  Nobody will reference
11687c478bd9Sstevel@tonic-gate 		 * it, though, and it will be leaked.
11697c478bd9Sstevel@tonic-gate 		 */
11707c478bd9Sstevel@tonic-gate 		return;
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	fn = xcalloc(sizeof (fndef_t));
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	tdp->t_type = FUNCTION;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
11787c478bd9Sstevel@tonic-gate 		dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
11797c478bd9Sstevel@tonic-gate 		fn->fn_ret = die_lookup_pass1(dw, die, DW_AT_type);
11807c478bd9Sstevel@tonic-gate 	} else {
11817c478bd9Sstevel@tonic-gate 		fn->fn_ret = tdesc_intr_void(dw);
11827c478bd9Sstevel@tonic-gate 	}
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	/*
11857c478bd9Sstevel@tonic-gate 	 * Count the arguments to the function, then read them in.
11867c478bd9Sstevel@tonic-gate 	 */
11877c478bd9Sstevel@tonic-gate 	for (fn->fn_nargs = 0, arg = die_child(dw, die); arg != NULL;
11887c478bd9Sstevel@tonic-gate 	    arg = die_sibling(dw, arg)) {
11897c478bd9Sstevel@tonic-gate 		if ((tag = die_tag(dw, arg)) == DW_TAG_formal_parameter)
11907c478bd9Sstevel@tonic-gate 			fn->fn_nargs++;
11917c478bd9Sstevel@tonic-gate 		else if (tag == DW_TAG_unspecified_parameters &&
11927c478bd9Sstevel@tonic-gate 		    fn->fn_nargs > 0)
11937c478bd9Sstevel@tonic-gate 			fn->fn_vargs = 1;
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	if (fn->fn_nargs != 0) {
11977c478bd9Sstevel@tonic-gate 		debug(3, "die %llu: adding %d argument%s\n", off, fn->fn_nargs,
11987c478bd9Sstevel@tonic-gate 		    (fn->fn_nargs > 1 ? "s" : ""));
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 		fn->fn_args = xcalloc(sizeof (tdesc_t *) * fn->fn_nargs);
12017c478bd9Sstevel@tonic-gate 		for (i = 0, arg = die_child(dw, die);
12027c478bd9Sstevel@tonic-gate 		    arg != NULL && i < fn->fn_nargs;
12037c478bd9Sstevel@tonic-gate 		    arg = die_sibling(dw, arg)) {
12047c478bd9Sstevel@tonic-gate 			if (die_tag(dw, arg) != DW_TAG_formal_parameter)
12057c478bd9Sstevel@tonic-gate 				continue;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 			fn->fn_args[i++] = die_lookup_pass1(dw, arg,
12087c478bd9Sstevel@tonic-gate 			    DW_AT_type);
12097c478bd9Sstevel@tonic-gate 		}
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	tdp->t_fndef = fn;
12137c478bd9Sstevel@tonic-gate 	tdp->t_flags |= TDESC_F_RESOLVED;
12147c478bd9Sstevel@tonic-gate }
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate /*
12177c478bd9Sstevel@tonic-gate  * GCC and DevPro use different names for the base types.  While the terms are
12187c478bd9Sstevel@tonic-gate  * the same, they are arranged in a different order.  Some terms, such as int,
12197c478bd9Sstevel@tonic-gate  * are implied in one, and explicitly named in the other.  Given a base type
12207c478bd9Sstevel@tonic-gate  * as input, this routine will return a common name, along with an intr_t
12217c478bd9Sstevel@tonic-gate  * that reflects said name.
12227c478bd9Sstevel@tonic-gate  */
12237c478bd9Sstevel@tonic-gate static intr_t *
12247c478bd9Sstevel@tonic-gate die_base_name_parse(const char *name, char **newp)
12257c478bd9Sstevel@tonic-gate {
12267c478bd9Sstevel@tonic-gate 	char buf[100];
12277c478bd9Sstevel@tonic-gate 	char *base, *c;
12287c478bd9Sstevel@tonic-gate 	int nlong = 0, nshort = 0, nchar = 0, nint = 0;
12297c478bd9Sstevel@tonic-gate 	int sign = 1;
12307c478bd9Sstevel@tonic-gate 	char fmt = '\0';
12317c478bd9Sstevel@tonic-gate 	intr_t *intr;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	if (strlen(name) > sizeof (buf) - 1)
12347c478bd9Sstevel@tonic-gate 		terminate("base type name \"%s\" is too long\n", name);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	strncpy(buf, name, sizeof (buf));
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	for (c = strtok(buf, " "); c != NULL; c = strtok(NULL, " ")) {
12397c478bd9Sstevel@tonic-gate 		if (strcmp(c, "signed") == 0)
12407c478bd9Sstevel@tonic-gate 			sign = 1;
12417c478bd9Sstevel@tonic-gate 		else if (strcmp(c, "unsigned") == 0)
12427c478bd9Sstevel@tonic-gate 			sign = 0;
12437c478bd9Sstevel@tonic-gate 		else if (strcmp(c, "long") == 0)
12447c478bd9Sstevel@tonic-gate 			nlong++;
12457c478bd9Sstevel@tonic-gate 		else if (strcmp(c, "char") == 0) {
12467c478bd9Sstevel@tonic-gate 			nchar++;
12477c478bd9Sstevel@tonic-gate 			fmt = 'c';
12487c478bd9Sstevel@tonic-gate 		} else if (strcmp(c, "short") == 0)
12497c478bd9Sstevel@tonic-gate 			nshort++;
12507c478bd9Sstevel@tonic-gate 		else if (strcmp(c, "int") == 0)
12517c478bd9Sstevel@tonic-gate 			nint++;
12527c478bd9Sstevel@tonic-gate 		else {
12537c478bd9Sstevel@tonic-gate 			/*
12547c478bd9Sstevel@tonic-gate 			 * If we don't recognize any of the tokens, we'll tell
12557c478bd9Sstevel@tonic-gate 			 * the caller to fall back to the dwarf-provided
12567c478bd9Sstevel@tonic-gate 			 * encoding information.
12577c478bd9Sstevel@tonic-gate 			 */
12587c478bd9Sstevel@tonic-gate 			return (NULL);
12597c478bd9Sstevel@tonic-gate 		}
12607c478bd9Sstevel@tonic-gate 	}
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	if (nchar > 1 || nshort > 1 || nint > 1 || nlong > 2)
12637c478bd9Sstevel@tonic-gate 		return (NULL);
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	if (nchar > 0) {
12667c478bd9Sstevel@tonic-gate 		if (nlong > 0 || nshort > 0 || nint > 0)
12677c478bd9Sstevel@tonic-gate 			return (NULL);
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 		base = "char";
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	} else if (nshort > 0) {
12727c478bd9Sstevel@tonic-gate 		if (nlong > 0)
12737c478bd9Sstevel@tonic-gate 			return (NULL);
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 		base = "short";
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	} else if (nlong > 0) {
12787c478bd9Sstevel@tonic-gate 		base = "long";
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	} else {
12817c478bd9Sstevel@tonic-gate 		base = "int";
12827c478bd9Sstevel@tonic-gate 	}
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	intr = xcalloc(sizeof (intr_t));
12857c478bd9Sstevel@tonic-gate 	intr->intr_type = INTR_INT;
12867c478bd9Sstevel@tonic-gate 	intr->intr_signed = sign;
12877c478bd9Sstevel@tonic-gate 	intr->intr_iformat = fmt;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof (buf), "%s%s%s",
12907c478bd9Sstevel@tonic-gate 	    (sign ? "" : "unsigned "),
12917c478bd9Sstevel@tonic-gate 	    (nlong > 1 ? "long " : ""),
12927c478bd9Sstevel@tonic-gate 	    base);
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	*newp = xstrdup(buf);
12957c478bd9Sstevel@tonic-gate 	return (intr);
12967c478bd9Sstevel@tonic-gate }
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate typedef struct fp_size_map {
12997c478bd9Sstevel@tonic-gate 	size_t fsm_typesz[2];	/* size of {32,64} type */
13007c478bd9Sstevel@tonic-gate 	uint_t fsm_enc[3];	/* CTF_FP_* for {bare,cplx,imagry} type */
13017c478bd9Sstevel@tonic-gate } fp_size_map_t;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate static const fp_size_map_t fp_encodings[] = {
13047c478bd9Sstevel@tonic-gate 	{ { 4, 4 }, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
13057c478bd9Sstevel@tonic-gate 	{ { 8, 8 }, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
130689518a1cSdmick #ifdef __sparc
130789518a1cSdmick 	{ { 16, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
130889518a1cSdmick #else
13097c478bd9Sstevel@tonic-gate 	{ { 12, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
131089518a1cSdmick #endif
13117c478bd9Sstevel@tonic-gate 	{ { 0, 0 } }
13127c478bd9Sstevel@tonic-gate };
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate static uint_t
13157c478bd9Sstevel@tonic-gate die_base_type2enc(dwarf_t *dw, Dwarf_Off off, Dwarf_Signed enc, size_t sz)
13167c478bd9Sstevel@tonic-gate {
13177c478bd9Sstevel@tonic-gate 	const fp_size_map_t *map = fp_encodings;
13187c478bd9Sstevel@tonic-gate 	uint_t szidx = dw->dw_ptrsz == sizeof (uint64_t);
13197c478bd9Sstevel@tonic-gate 	uint_t mult = 1, col = 0;
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	if (enc == DW_ATE_complex_float) {
13227c478bd9Sstevel@tonic-gate 		mult = 2;
13237c478bd9Sstevel@tonic-gate 		col = 1;
13247c478bd9Sstevel@tonic-gate 	} else if (enc == DW_ATE_imaginary_float ||
13257c478bd9Sstevel@tonic-gate 	    enc == DW_ATE_SUN_imaginary_float)
13267c478bd9Sstevel@tonic-gate 		col = 2;
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	while (map->fsm_typesz[szidx] != 0) {
13297c478bd9Sstevel@tonic-gate 		if (map->fsm_typesz[szidx] * mult == sz)
13307c478bd9Sstevel@tonic-gate 			return (map->fsm_enc[col]);
13317c478bd9Sstevel@tonic-gate 		map++;
13327c478bd9Sstevel@tonic-gate 	}
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	terminate("die %llu: unrecognized real type size %u\n", off, sz);
13357c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
13367c478bd9Sstevel@tonic-gate 	return (0);
13377c478bd9Sstevel@tonic-gate }
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate static intr_t *
13407c478bd9Sstevel@tonic-gate die_base_from_dwarf(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, size_t sz)
13417c478bd9Sstevel@tonic-gate {
13427c478bd9Sstevel@tonic-gate 	intr_t *intr = xcalloc(sizeof (intr_t));
13437c478bd9Sstevel@tonic-gate 	Dwarf_Signed enc;
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	(void) die_signed(dw, base, DW_AT_encoding, &enc, DW_ATTR_REQ);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	switch (enc) {
13487c478bd9Sstevel@tonic-gate 	case DW_ATE_unsigned:
13497c478bd9Sstevel@tonic-gate 	case DW_ATE_address:
13507c478bd9Sstevel@tonic-gate 		intr->intr_type = INTR_INT;
13517c478bd9Sstevel@tonic-gate 		break;
13527c478bd9Sstevel@tonic-gate 	case DW_ATE_unsigned_char:
13537c478bd9Sstevel@tonic-gate 		intr->intr_type = INTR_INT;
13547c478bd9Sstevel@tonic-gate 		intr->intr_iformat = 'c';
13557c478bd9Sstevel@tonic-gate 		break;
13567c478bd9Sstevel@tonic-gate 	case DW_ATE_signed:
13577c478bd9Sstevel@tonic-gate 		intr->intr_type = INTR_INT;
13587c478bd9Sstevel@tonic-gate 		intr->intr_signed = 1;
13597c478bd9Sstevel@tonic-gate 		break;
13607c478bd9Sstevel@tonic-gate 	case DW_ATE_signed_char:
13617c478bd9Sstevel@tonic-gate 		intr->intr_type = INTR_INT;
13627c478bd9Sstevel@tonic-gate 		intr->intr_signed = 1;
13637c478bd9Sstevel@tonic-gate 		intr->intr_iformat = 'c';
13647c478bd9Sstevel@tonic-gate 		break;
13657c478bd9Sstevel@tonic-gate 	case DW_ATE_boolean:
13667c478bd9Sstevel@tonic-gate 		intr->intr_type = INTR_INT;
13677c478bd9Sstevel@tonic-gate 		intr->intr_signed = 1;
13687c478bd9Sstevel@tonic-gate 		intr->intr_iformat = 'b';
13697c478bd9Sstevel@tonic-gate 		break;
13707c478bd9Sstevel@tonic-gate 	case DW_ATE_float:
13717c478bd9Sstevel@tonic-gate 	case DW_ATE_complex_float:
13727c478bd9Sstevel@tonic-gate 	case DW_ATE_imaginary_float:
13737c478bd9Sstevel@tonic-gate 	case DW_ATE_SUN_imaginary_float:
13747c478bd9Sstevel@tonic-gate 	case DW_ATE_SUN_interval_float:
13757c478bd9Sstevel@tonic-gate 		intr->intr_type = INTR_REAL;
13767c478bd9Sstevel@tonic-gate 		intr->intr_signed = 1;
13777c478bd9Sstevel@tonic-gate 		intr->intr_fformat = die_base_type2enc(dw, off, enc, sz);
13787c478bd9Sstevel@tonic-gate 		break;
13797c478bd9Sstevel@tonic-gate 	default:
13807c478bd9Sstevel@tonic-gate 		terminate("die %llu: unknown base type encoding 0x%llx\n",
13817c478bd9Sstevel@tonic-gate 		    off, enc);
13827c478bd9Sstevel@tonic-gate 	}
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	return (intr);
13857c478bd9Sstevel@tonic-gate }
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate static void
13887c478bd9Sstevel@tonic-gate die_base_create(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, tdesc_t *tdp)
13897c478bd9Sstevel@tonic-gate {
13907c478bd9Sstevel@tonic-gate 	Dwarf_Unsigned sz;
13917c478bd9Sstevel@tonic-gate 	intr_t *intr;
13927c478bd9Sstevel@tonic-gate 	char *new;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: creating base type\n", off);
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	/*
13977c478bd9Sstevel@tonic-gate 	 * The compilers have their own clever (internally inconsistent) ideas
13987c478bd9Sstevel@tonic-gate 	 * as to what base types should look like.  Some times gcc will, for
13997c478bd9Sstevel@tonic-gate 	 * example, use DW_ATE_signed_char for char.  Other times, however, it
14007c478bd9Sstevel@tonic-gate 	 * will use DW_ATE_signed.  Needless to say, this causes some problems
14017c478bd9Sstevel@tonic-gate 	 * down the road, particularly with merging.  We do, however, use the
14027c478bd9Sstevel@tonic-gate 	 * DWARF idea of type sizes, as this allows us to avoid caring about
14037c478bd9Sstevel@tonic-gate 	 * the data model.
14047c478bd9Sstevel@tonic-gate 	 */
14057c478bd9Sstevel@tonic-gate 	(void) die_unsigned(dw, base, DW_AT_byte_size, &sz, DW_ATTR_REQ);
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	if (tdp->t_name == NULL)
14087c478bd9Sstevel@tonic-gate 		terminate("die %llu: base type without name\n", off);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/* XXX make a name parser for float too */
14117c478bd9Sstevel@tonic-gate 	if ((intr = die_base_name_parse(tdp->t_name, &new)) != NULL) {
14127c478bd9Sstevel@tonic-gate 		/* Found it.  We'll use the parsed version */
14137c478bd9Sstevel@tonic-gate 		debug(3, "die %llu: name \"%s\" remapped to \"%s\"\n", off,
14144d232658Sjohnlev 		    tdesc_name(tdp), new);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 		free(tdp->t_name);
14177c478bd9Sstevel@tonic-gate 		tdp->t_name = new;
14187c478bd9Sstevel@tonic-gate 	} else {
14197c478bd9Sstevel@tonic-gate 		/*
14207c478bd9Sstevel@tonic-gate 		 * We didn't recognize the type, so we'll create an intr_t
14217c478bd9Sstevel@tonic-gate 		 * based on the DWARF data.
14227c478bd9Sstevel@tonic-gate 		 */
14237c478bd9Sstevel@tonic-gate 		debug(3, "die %llu: using dwarf data for base \"%s\"\n", off,
14244d232658Sjohnlev 		    tdesc_name(tdp));
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 		intr = die_base_from_dwarf(dw, base, off, sz);
14277c478bd9Sstevel@tonic-gate 	}
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	intr->intr_nbits = sz * 8;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	tdp->t_type = INTRINSIC;
14327c478bd9Sstevel@tonic-gate 	tdp->t_intr = intr;
14337c478bd9Sstevel@tonic-gate 	tdp->t_size = sz;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	tdp->t_flags |= TDESC_F_RESOLVED;
14367c478bd9Sstevel@tonic-gate }
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate static void
14397c478bd9Sstevel@tonic-gate die_through_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp,
14407c478bd9Sstevel@tonic-gate     int type, const char *typename)
14417c478bd9Sstevel@tonic-gate {
14427c478bd9Sstevel@tonic-gate 	Dwarf_Attribute attr;
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: creating %s\n", off, typename);
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	tdp->t_type = type;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
14497c478bd9Sstevel@tonic-gate 		dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
14507c478bd9Sstevel@tonic-gate 		tdp->t_tdesc = die_lookup_pass1(dw, die, DW_AT_type);
14517c478bd9Sstevel@tonic-gate 	} else {
14527c478bd9Sstevel@tonic-gate 		tdp->t_tdesc = tdesc_intr_void(dw);
14537c478bd9Sstevel@tonic-gate 	}
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	if (type == POINTER)
14567c478bd9Sstevel@tonic-gate 		tdp->t_size = dw->dw_ptrsz;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	tdp->t_flags |= TDESC_F_RESOLVED;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	if (type == TYPEDEF) {
14617c478bd9Sstevel@tonic-gate 		iidesc_t *ii = xcalloc(sizeof (iidesc_t));
14627c478bd9Sstevel@tonic-gate 		ii->ii_type = II_TYPE;
14637c478bd9Sstevel@tonic-gate 		ii->ii_name = xstrdup(tdp->t_name);
14647c478bd9Sstevel@tonic-gate 		ii->ii_dtype = tdp;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 		iidesc_add(dw->dw_td->td_iihash, ii);
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate }
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate static void
14717c478bd9Sstevel@tonic-gate die_typedef_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
14727c478bd9Sstevel@tonic-gate {
14737c478bd9Sstevel@tonic-gate 	die_through_create(dw, die, off, tdp, TYPEDEF, "typedef");
14747c478bd9Sstevel@tonic-gate }
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate static void
14777c478bd9Sstevel@tonic-gate die_const_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
14787c478bd9Sstevel@tonic-gate {
14797c478bd9Sstevel@tonic-gate 	die_through_create(dw, die, off, tdp, CONST, "const");
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate static void
14837c478bd9Sstevel@tonic-gate die_pointer_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
14847c478bd9Sstevel@tonic-gate {
14857c478bd9Sstevel@tonic-gate 	die_through_create(dw, die, off, tdp, POINTER, "pointer");
14867c478bd9Sstevel@tonic-gate }
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate static void
14897c478bd9Sstevel@tonic-gate die_restrict_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
14907c478bd9Sstevel@tonic-gate {
14917c478bd9Sstevel@tonic-gate 	die_through_create(dw, die, off, tdp, RESTRICT, "restrict");
14927c478bd9Sstevel@tonic-gate }
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate static void
14957c478bd9Sstevel@tonic-gate die_volatile_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
14967c478bd9Sstevel@tonic-gate {
14977c478bd9Sstevel@tonic-gate 	die_through_create(dw, die, off, tdp, VOLATILE, "volatile");
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
15017c478bd9Sstevel@tonic-gate static void
15027c478bd9Sstevel@tonic-gate die_function_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
15037c478bd9Sstevel@tonic-gate {
15047c478bd9Sstevel@tonic-gate 	Dwarf_Die arg;
15057c478bd9Sstevel@tonic-gate 	Dwarf_Half tag;
15067c478bd9Sstevel@tonic-gate 	iidesc_t *ii;
15077c478bd9Sstevel@tonic-gate 	char *name;
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: creating function definition\n", off);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	/*
15127c478bd9Sstevel@tonic-gate 	 * We'll begin by processing any type definition nodes that may be
15137c478bd9Sstevel@tonic-gate 	 * lurking underneath this one.
15147c478bd9Sstevel@tonic-gate 	 */
15157c478bd9Sstevel@tonic-gate 	for (arg = die_child(dw, die); arg != NULL;
15167c478bd9Sstevel@tonic-gate 	    arg = die_sibling(dw, arg)) {
15177c478bd9Sstevel@tonic-gate 		if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
15187c478bd9Sstevel@tonic-gate 		    tag != DW_TAG_variable) {
15197c478bd9Sstevel@tonic-gate 			/* Nested type declaration */
15207c478bd9Sstevel@tonic-gate 			die_create_one(dw, arg);
15217c478bd9Sstevel@tonic-gate 		}
15227c478bd9Sstevel@tonic-gate 	}
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) {
15257c478bd9Sstevel@tonic-gate 		/*
15267c478bd9Sstevel@tonic-gate 		 * We process neither prototypes nor subprograms without
15277c478bd9Sstevel@tonic-gate 		 * names.
15287c478bd9Sstevel@tonic-gate 		 */
15297c478bd9Sstevel@tonic-gate 		return;
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	ii = xcalloc(sizeof (iidesc_t));
15337c478bd9Sstevel@tonic-gate 	ii->ii_type = die_isglobal(dw, die) ? II_GFUN : II_SFUN;
15347c478bd9Sstevel@tonic-gate 	ii->ii_name = name;
15357c478bd9Sstevel@tonic-gate 	if (ii->ii_type == II_SFUN)
15367c478bd9Sstevel@tonic-gate 		ii->ii_owner = xstrdup(dw->dw_cuname);
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: function %s is %s\n", off, ii->ii_name,
15397c478bd9Sstevel@tonic-gate 	    (ii->ii_type == II_GFUN ? "global" : "static"));
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if (die_attr(dw, die, DW_AT_type, 0) != NULL)
15427c478bd9Sstevel@tonic-gate 		ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
15437c478bd9Sstevel@tonic-gate 	else
15447c478bd9Sstevel@tonic-gate 		ii->ii_dtype = tdesc_intr_void(dw);
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	for (arg = die_child(dw, die); arg != NULL;
15477c478bd9Sstevel@tonic-gate 	    arg = die_sibling(dw, arg)) {
15487c478bd9Sstevel@tonic-gate 		char *name;
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 		debug(3, "die %llu: looking at sub member at %llu\n",
15517c478bd9Sstevel@tonic-gate 		    off, die_off(dw, die));
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 		if (die_tag(dw, arg) != DW_TAG_formal_parameter)
15547c478bd9Sstevel@tonic-gate 			continue;
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 		if ((name = die_name(dw, arg)) == NULL) {
15577c478bd9Sstevel@tonic-gate 			terminate("die %llu: func arg %d has no name\n",
15587c478bd9Sstevel@tonic-gate 			    off, ii->ii_nargs + 1);
15597c478bd9Sstevel@tonic-gate 		}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 		if (strcmp(name, "...") == 0) {
15627c478bd9Sstevel@tonic-gate 			free(name);
15637c478bd9Sstevel@tonic-gate 			ii->ii_vargs = 1;
15647c478bd9Sstevel@tonic-gate 			continue;
15657c478bd9Sstevel@tonic-gate 		}
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		ii->ii_nargs++;
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 	if (ii->ii_nargs > 0) {
15717c478bd9Sstevel@tonic-gate 		int i;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 		debug(3, "die %llu: function has %d argument%s\n", off,
15747c478bd9Sstevel@tonic-gate 		    ii->ii_nargs, (ii->ii_nargs == 1 ? "" : "s"));
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 		ii->ii_args = xcalloc(sizeof (tdesc_t) * ii->ii_nargs);
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 		for (arg = die_child(dw, die), i = 0;
15797c478bd9Sstevel@tonic-gate 		    arg != NULL && i < ii->ii_nargs;
15807c478bd9Sstevel@tonic-gate 		    arg = die_sibling(dw, arg)) {
15817c478bd9Sstevel@tonic-gate 			if (die_tag(dw, arg) != DW_TAG_formal_parameter)
15827c478bd9Sstevel@tonic-gate 				continue;
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 			ii->ii_args[i++] = die_lookup_pass1(dw, arg,
15857c478bd9Sstevel@tonic-gate 			    DW_AT_type);
15867c478bd9Sstevel@tonic-gate 		}
15877c478bd9Sstevel@tonic-gate 	}
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	iidesc_add(dw->dw_td->td_iihash, ii);
15907c478bd9Sstevel@tonic-gate }
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
15937c478bd9Sstevel@tonic-gate static void
15947c478bd9Sstevel@tonic-gate die_variable_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
15957c478bd9Sstevel@tonic-gate {
15967c478bd9Sstevel@tonic-gate 	iidesc_t *ii;
15977c478bd9Sstevel@tonic-gate 	char *name;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: creating object definition\n", off);
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL)
16027c478bd9Sstevel@tonic-gate 		return; /* skip prototypes and nameless objects */
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	ii = xcalloc(sizeof (iidesc_t));
16057c478bd9Sstevel@tonic-gate 	ii->ii_type = die_isglobal(dw, die) ? II_GVAR : II_SVAR;
16067c478bd9Sstevel@tonic-gate 	ii->ii_name = name;
16077c478bd9Sstevel@tonic-gate 	ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
16087c478bd9Sstevel@tonic-gate 	if (ii->ii_type == II_SVAR)
16097c478bd9Sstevel@tonic-gate 		ii->ii_owner = xstrdup(dw->dw_cuname);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	iidesc_add(dw->dw_td->td_iihash, ii);
16127c478bd9Sstevel@tonic-gate }
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
16157c478bd9Sstevel@tonic-gate static int
16167c478bd9Sstevel@tonic-gate die_fwd_resolve(tdesc_t *fwd, tdesc_t **fwdp, void *private)
16177c478bd9Sstevel@tonic-gate {
16187c478bd9Sstevel@tonic-gate 	if (fwd->t_flags & TDESC_F_RESOLVED)
16197c478bd9Sstevel@tonic-gate 		return (1);
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	if (fwd->t_tdesc != NULL) {
16224d232658Sjohnlev 		debug(3, "tdp %u: unforwarded %s\n", fwd->t_id,
16234d232658Sjohnlev 		    tdesc_name(fwd));
16247c478bd9Sstevel@tonic-gate 		*fwdp = fwd->t_tdesc;
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	fwd->t_flags |= TDESC_F_RESOLVED;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	return (1);
16307c478bd9Sstevel@tonic-gate }
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16337c478bd9Sstevel@tonic-gate static void
16347c478bd9Sstevel@tonic-gate die_lexblk_descend(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
16357c478bd9Sstevel@tonic-gate {
16367c478bd9Sstevel@tonic-gate 	Dwarf_Die child = die_child(dw, die);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	if (child != NULL)
16397c478bd9Sstevel@tonic-gate 		die_create(dw, child);
16407c478bd9Sstevel@tonic-gate }
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate /*
16437c478bd9Sstevel@tonic-gate  * Used to map the die to a routine which can parse it, using the tag to do the
16447c478bd9Sstevel@tonic-gate  * mapping.  While the processing of most tags entails the creation of a tdesc,
16457c478bd9Sstevel@tonic-gate  * there are a few which don't - primarily those which result in the creation of
16467c478bd9Sstevel@tonic-gate  * iidescs which refer to existing tdescs.
16477c478bd9Sstevel@tonic-gate  */
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate #define	DW_F_NOTDP	0x1	/* Don't create a tdesc for the creator */
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate typedef struct die_creator {
16527c478bd9Sstevel@tonic-gate 	Dwarf_Half dc_tag;
16537c478bd9Sstevel@tonic-gate 	uint16_t dc_flags;
16547c478bd9Sstevel@tonic-gate 	void (*dc_create)(dwarf_t *, Dwarf_Die, Dwarf_Off, tdesc_t *);
16557c478bd9Sstevel@tonic-gate } die_creator_t;
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate static const die_creator_t die_creators[] = {
16587c478bd9Sstevel@tonic-gate 	{ DW_TAG_array_type,		0,		die_array_create },
16597c478bd9Sstevel@tonic-gate 	{ DW_TAG_enumeration_type,	0,		die_enum_create },
16607c478bd9Sstevel@tonic-gate 	{ DW_TAG_lexical_block,		DW_F_NOTDP,	die_lexblk_descend },
16617c478bd9Sstevel@tonic-gate 	{ DW_TAG_pointer_type,		0,		die_pointer_create },
16627c478bd9Sstevel@tonic-gate 	{ DW_TAG_structure_type,	0,		die_struct_create },
16637c478bd9Sstevel@tonic-gate 	{ DW_TAG_subroutine_type,	0,		die_funcptr_create },
16647c478bd9Sstevel@tonic-gate 	{ DW_TAG_typedef,		0,		die_typedef_create },
16657c478bd9Sstevel@tonic-gate 	{ DW_TAG_union_type,		0,		die_union_create },
16667c478bd9Sstevel@tonic-gate 	{ DW_TAG_base_type,		0,		die_base_create },
16677c478bd9Sstevel@tonic-gate 	{ DW_TAG_const_type,		0,		die_const_create },
16687c478bd9Sstevel@tonic-gate 	{ DW_TAG_subprogram,		DW_F_NOTDP,	die_function_create },
16697c478bd9Sstevel@tonic-gate 	{ DW_TAG_variable,		DW_F_NOTDP,	die_variable_create },
16707c478bd9Sstevel@tonic-gate 	{ DW_TAG_volatile_type,		0,		die_volatile_create },
16717c478bd9Sstevel@tonic-gate 	{ DW_TAG_restrict_type,		0,		die_restrict_create },
16727c478bd9Sstevel@tonic-gate 	{ 0, NULL }
16737c478bd9Sstevel@tonic-gate };
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate static const die_creator_t *
16767c478bd9Sstevel@tonic-gate die_tag2ctor(Dwarf_Half tag)
16777c478bd9Sstevel@tonic-gate {
16787c478bd9Sstevel@tonic-gate 	const die_creator_t *dc;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	for (dc = die_creators; dc->dc_create != NULL; dc++) {
16817c478bd9Sstevel@tonic-gate 		if (dc->dc_tag == tag)
16827c478bd9Sstevel@tonic-gate 			return (dc);
16837c478bd9Sstevel@tonic-gate 	}
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	return (NULL);
16867c478bd9Sstevel@tonic-gate }
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate static void
16897c478bd9Sstevel@tonic-gate die_create_one(dwarf_t *dw, Dwarf_Die die)
16907c478bd9Sstevel@tonic-gate {
16917c478bd9Sstevel@tonic-gate 	Dwarf_Off off = die_off(dw, die);
16927c478bd9Sstevel@tonic-gate 	const die_creator_t *dc;
16937c478bd9Sstevel@tonic-gate 	Dwarf_Half tag;
16947c478bd9Sstevel@tonic-gate 	tdesc_t *tdp;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	debug(3, "die %llu: create_one\n", off);
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	if (off > dw->dw_maxoff) {
16997c478bd9Sstevel@tonic-gate 		terminate("illegal die offset %llu (max %llu)\n", off,
17007c478bd9Sstevel@tonic-gate 		    dw->dw_maxoff);
17017c478bd9Sstevel@tonic-gate 	}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	tag = die_tag(dw, die);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	if ((dc = die_tag2ctor(tag)) == NULL) {
17067c478bd9Sstevel@tonic-gate 		debug(2, "die %llu: ignoring tag type %x\n", off, tag);
17077c478bd9Sstevel@tonic-gate 		return;
17087c478bd9Sstevel@tonic-gate 	}
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	if ((tdp = tdesc_lookup(dw, off)) == NULL &&
17117c478bd9Sstevel@tonic-gate 	    !(dc->dc_flags & DW_F_NOTDP)) {
17127c478bd9Sstevel@tonic-gate 		tdp = xcalloc(sizeof (tdesc_t));
17137c478bd9Sstevel@tonic-gate 		tdp->t_id = off;
17147c478bd9Sstevel@tonic-gate 		tdesc_add(dw, tdp);
17157c478bd9Sstevel@tonic-gate 	}
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	if (tdp != NULL)
17187c478bd9Sstevel@tonic-gate 		tdp->t_name = die_name(dw, die);
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	dc->dc_create(dw, die, off, tdp);
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate static void
17247c478bd9Sstevel@tonic-gate die_create(dwarf_t *dw, Dwarf_Die die)
17257c478bd9Sstevel@tonic-gate {
17267c478bd9Sstevel@tonic-gate 	do {
17277c478bd9Sstevel@tonic-gate 		die_create_one(dw, die);
17287c478bd9Sstevel@tonic-gate 	} while ((die = die_sibling(dw, die)) != NULL);
17297c478bd9Sstevel@tonic-gate }
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate static tdtrav_cb_f die_resolvers[] = {
17327c478bd9Sstevel@tonic-gate 	NULL,
17337c478bd9Sstevel@tonic-gate 	NULL,			/* intrinsic */
17347c478bd9Sstevel@tonic-gate 	NULL,			/* pointer */
17357c478bd9Sstevel@tonic-gate 	die_array_resolve,	/* array */
17367c478bd9Sstevel@tonic-gate 	NULL,			/* function */
17377c478bd9Sstevel@tonic-gate 	die_sou_resolve,	/* struct */
17387c478bd9Sstevel@tonic-gate 	die_sou_resolve,	/* union */
17397c478bd9Sstevel@tonic-gate 	die_enum_resolve,	/* enum */
17407c478bd9Sstevel@tonic-gate 	die_fwd_resolve,	/* forward */
17417c478bd9Sstevel@tonic-gate 	NULL,			/* typedef */
17427c478bd9Sstevel@tonic-gate 	NULL,			/* typedef unres */
17437c478bd9Sstevel@tonic-gate 	NULL,			/* volatile */
17447c478bd9Sstevel@tonic-gate 	NULL,			/* const */
17457c478bd9Sstevel@tonic-gate 	NULL,			/* restrict */
17467c478bd9Sstevel@tonic-gate };
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate static tdtrav_cb_f die_fail_reporters[] = {
17497c478bd9Sstevel@tonic-gate 	NULL,
17507c478bd9Sstevel@tonic-gate 	NULL,			/* intrinsic */
17517c478bd9Sstevel@tonic-gate 	NULL,			/* pointer */
17527c478bd9Sstevel@tonic-gate 	die_array_failed,	/* array */
17537c478bd9Sstevel@tonic-gate 	NULL,			/* function */
17547c478bd9Sstevel@tonic-gate 	die_sou_failed,		/* struct */
17557c478bd9Sstevel@tonic-gate 	die_sou_failed,		/* union */
17567c478bd9Sstevel@tonic-gate 	NULL,			/* enum */
17577c478bd9Sstevel@tonic-gate 	NULL,			/* forward */
17587c478bd9Sstevel@tonic-gate 	NULL,			/* typedef */
17597c478bd9Sstevel@tonic-gate 	NULL,			/* typedef unres */
17607c478bd9Sstevel@tonic-gate 	NULL,			/* volatile */
17617c478bd9Sstevel@tonic-gate 	NULL,			/* const */
17627c478bd9Sstevel@tonic-gate 	NULL,			/* restrict */
17637c478bd9Sstevel@tonic-gate };
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate static void
17667c478bd9Sstevel@tonic-gate die_resolve(dwarf_t *dw)
17677c478bd9Sstevel@tonic-gate {
17687c478bd9Sstevel@tonic-gate 	int last = -1;
17697c478bd9Sstevel@tonic-gate 	int pass = 0;
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	do {
17727c478bd9Sstevel@tonic-gate 		pass++;
17737c478bd9Sstevel@tonic-gate 		dw->dw_nunres = 0;
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 		(void) iitraverse_hash(dw->dw_td->td_iihash,
17767c478bd9Sstevel@tonic-gate 		    &dw->dw_td->td_curvgen, NULL, NULL, die_resolvers, dw);
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 		debug(3, "resolve: pass %d, %u left\n", pass, dw->dw_nunres);
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 		if (dw->dw_nunres == last) {
17817c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: failed to resolve the following "
17827c478bd9Sstevel@tonic-gate 			    "types:\n", progname);
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 			(void) iitraverse_hash(dw->dw_td->td_iihash,
17857c478bd9Sstevel@tonic-gate 			    &dw->dw_td->td_curvgen, NULL, NULL,
17867c478bd9Sstevel@tonic-gate 			    die_fail_reporters, dw);
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 			terminate("failed to resolve types\n");
17897c478bd9Sstevel@tonic-gate 		}
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 		last = dw->dw_nunres;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	} while (dw->dw_nunres != 0);
17947c478bd9Sstevel@tonic-gate }
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17977c478bd9Sstevel@tonic-gate int
17987c478bd9Sstevel@tonic-gate dw_read(tdata_t *td, Elf *elf, const char *filename)
17997c478bd9Sstevel@tonic-gate {
18007c478bd9Sstevel@tonic-gate 	Dwarf_Unsigned abboff, hdrlen, nxthdr;
18017c478bd9Sstevel@tonic-gate 	Dwarf_Half vers, addrsz;
18027c478bd9Sstevel@tonic-gate 	Dwarf_Die cu, child;
18037c478bd9Sstevel@tonic-gate 	dwarf_t dw;
18047c478bd9Sstevel@tonic-gate 	char *prod = NULL;
18057c478bd9Sstevel@tonic-gate 	int rc;
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	bzero(&dw, sizeof (dwarf_t));
18087c478bd9Sstevel@tonic-gate 	dw.dw_td = td;
18097c478bd9Sstevel@tonic-gate 	dw.dw_ptrsz = elf_ptrsz(elf);
18107c478bd9Sstevel@tonic-gate 	dw.dw_mfgtid_last = TID_MFGTID_BASE;
18117c478bd9Sstevel@tonic-gate 	dw.dw_tidhash = hash_new(TDESC_HASH_BUCKETS, tdesc_idhash, tdesc_idcmp);
18127c478bd9Sstevel@tonic-gate 	dw.dw_fwdhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
18137c478bd9Sstevel@tonic-gate 	    tdesc_namecmp);
18147c478bd9Sstevel@tonic-gate 	dw.dw_enumhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
18157c478bd9Sstevel@tonic-gate 	    tdesc_namecmp);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	if ((rc = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dw.dw_dw,
18187c478bd9Sstevel@tonic-gate 	    &dw.dw_err)) == DW_DLV_NO_ENTRY) {
18197c478bd9Sstevel@tonic-gate 		errno = ENOENT;
18207c478bd9Sstevel@tonic-gate 		return (-1);
18217c478bd9Sstevel@tonic-gate 	} else if (rc != DW_DLV_OK) {
18227c478bd9Sstevel@tonic-gate 		if (dwarf_errno(dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) {
18237c478bd9Sstevel@tonic-gate 			/*
18247c478bd9Sstevel@tonic-gate 			 * There's no type data in the DWARF section, but
18257c478bd9Sstevel@tonic-gate 			 * libdwarf is too clever to handle that properly.
18267c478bd9Sstevel@tonic-gate 			 */
18277c478bd9Sstevel@tonic-gate 			return (0);
18287c478bd9Sstevel@tonic-gate 		}
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 		terminate("failed to initialize DWARF: %s\n",
18317c478bd9Sstevel@tonic-gate 		    dwarf_errmsg(dw.dw_err));
18327c478bd9Sstevel@tonic-gate 	}
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff,
18357c478bd9Sstevel@tonic-gate 	    &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_OK ||
18367c478bd9Sstevel@tonic-gate 	    (cu = die_sibling(&dw, NULL)) == NULL ||
18377c478bd9Sstevel@tonic-gate 	    (child = die_child(&dw, cu)) == NULL)
18387c478bd9Sstevel@tonic-gate 		terminate("file does not contain dwarf type data "
18397c478bd9Sstevel@tonic-gate 		    "(try compiling with -g)\n");
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	dw.dw_maxoff = nxthdr - 1;
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	if (dw.dw_maxoff > TID_FILEMAX)
18447c478bd9Sstevel@tonic-gate 		terminate("file contains too many types\n");
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	debug(1, "DWARF version: %d\n", vers);
18477c478bd9Sstevel@tonic-gate 	if (vers != DWARF_VERSION) {
18487c478bd9Sstevel@tonic-gate 		terminate("file contains incompatible version %d DWARF code "
18497c478bd9Sstevel@tonic-gate 		    "(version 2 required)\n", vers);
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	if (die_string(&dw, cu, DW_AT_producer, &prod, 0)) {
18537c478bd9Sstevel@tonic-gate 		debug(1, "DWARF emitter: %s\n", prod);
18547c478bd9Sstevel@tonic-gate 		free(prod);
18557c478bd9Sstevel@tonic-gate 	}
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	if ((dw.dw_cuname = die_name(&dw, cu)) != NULL) {
18587c478bd9Sstevel@tonic-gate 		char *base = xstrdup(basename(dw.dw_cuname));
18597c478bd9Sstevel@tonic-gate 		free(dw.dw_cuname);
18607c478bd9Sstevel@tonic-gate 		dw.dw_cuname = base;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 		debug(1, "CU name: %s\n", dw.dw_cuname);
18637c478bd9Sstevel@tonic-gate 	}
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	die_create(&dw, child);
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff,
18687c478bd9Sstevel@tonic-gate 	    &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_NO_ENTRY)
18697c478bd9Sstevel@tonic-gate 		terminate("multiple compilation units not supported\n");
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	(void) dwarf_finish(dw.dw_dw, &dw.dw_err);
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	die_resolve(&dw);
18747c478bd9Sstevel@tonic-gate 
1875e824d57fSjohnlev 	cvt_fixups(td, dw.dw_ptrsz);
1876e824d57fSjohnlev 
18777c478bd9Sstevel@tonic-gate 	/* leak the dwarf_t */
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 	return (0);
18807c478bd9Sstevel@tonic-gate }
1881