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