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