Path: blob/main/cddl/contrib/opensolaris/tools/ctf/cvt/dwarf.c
39586 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/20/*21* Copyright 2007 Sun Microsystems, Inc. All rights reserved.22* Use is subject to license terms.23*/2425/*26* DWARF to tdata conversion27*28* For the most part, conversion is straightforward, proceeding in two passes.29* On the first pass, we iterate through every die, creating new type nodes as30* necessary. Referenced tdesc_t's are created in an uninitialized state, thus31* allowing type reference pointers to be filled in. If the tdesc_t32* corresponding to a given die can be completely filled out (sizes and offsets33* calculated, and so forth) without using any referenced types, the tdesc_t is34* marked as resolved. Consider an array type. If the type corresponding to35* the array contents has not yet been processed, we will create a blank tdesc36* for the contents type (only the type ID will be filled in, relying upon the37* later portion of the first pass to encounter and complete the referenced38* type). We will then attempt to determine the size of the array. If the39* array has a byte size attribute, we will have completely characterized the40* array type, and will be able to mark it as resolved. The lack of a byte41* size attribute, on the other hand, will prevent us from fully resolving the42* type, as the size will only be calculable with reference to the contents43* type, which has not, as yet, been encountered. The array type will thus be44* left without the resolved flag, and the first pass will continue.45*46* When we begin the second pass, we will have created tdesc_t nodes for every47* type in the section. We will traverse the tree, from the iidescs down,48* processing each unresolved node. As the referenced nodes will have been49* populated, the array type used in our example above will be able to use the50* size of the referenced types (if available) to determine its own type. The51* traversal will be repeated until all types have been resolved or we have52* failed to make progress. When all tdescs have been resolved, the conversion53* is complete.54*55* There are, as always, a few special cases that are handled during the first56* and second passes:57*58* 1. Empty enums - GCC will occasionally emit an enum without any members.59* Later on in the file, it will emit the same enum type, though this time60* with the full complement of members. All references to the memberless61* enum need to be redirected to the full definition. During the first62* pass, each enum is entered in dm_enumhash, along with a pointer to its63* corresponding tdesc_t. If, during the second pass, we encounter a64* memberless enum, we use the hash to locate the full definition. All65* tdescs referencing the empty enum are then redirected.66*67* 2. Forward declarations - If the compiler sees a forward declaration for68* a structure, followed by the definition of that structure, it will emit69* DWARF data for both the forward declaration and the definition. We need70* to resolve the forward declarations when possible, by redirecting71* forward-referencing tdescs to the actual struct/union definitions. This72* redirection is done completely within the first pass. We begin by73* recording all forward declarations in dw_fwdhash. When we define a74* structure, we check to see if there have been any corresponding forward75* declarations. If so, we redirect the tdescs which referenced the forward76* declarations to the structure or union definition.77*78* XXX see if a post traverser will allow the elimination of repeated pass 279* traversals.80*/8182#include <stdio.h>83#include <stdlib.h>84#include <string.h>85#include <strings.h>86#include <errno.h>87#include <libelf.h>88#include <libdwarf.h>89#include <libgen.h>90#include <dwarf.h>9192#include "ctf_headers.h"93#include "ctftools.h"94#include "memory.h"95#include "list.h"96#include "traverse.h"9798/*99* We need to define a couple of our own intrinsics, to smooth out some of the100* differences between the GCC and DevPro DWARF emitters. See the referenced101* routines and the special cases in the file comment for more details.102*103* Type IDs are 32 bits wide. We're going to use the top of that field to104* indicate types that we've created ourselves.105*/106#define TID_FILEMAX 0x3fffffff /* highest tid from file */107#define TID_VOID 0x40000001 /* see die_void() */108#define TID_LONG 0x40000002 /* see die_array() */109110#define TID_MFGTID_BASE 0x40000003 /* first mfg'd tid */111112/*113* To reduce the staggering amount of error-handling code that would otherwise114* be required, the attribute-retrieval routines handle most of their own115* errors. If the following flag is supplied as the value of the `req'116* argument, they will also handle the absence of a requested attribute by117* terminating the program.118*/119#define DW_ATTR_REQ 1120121#define TDESC_HASH_BUCKETS 511122123typedef struct dwarf {124Dwarf_Debug dw_dw; /* for libdwarf */125Dwarf_Error dw_err; /* for libdwarf */126Dwarf_Off dw_maxoff; /* highest legal offset in this cu */127tdata_t *dw_td; /* root of the tdesc/iidesc tree */128hash_t *dw_tidhash; /* hash of tdescs by t_id */129hash_t *dw_fwdhash; /* hash of fwd decls by name */130hash_t *dw_enumhash; /* hash of memberless enums by name */131tdesc_t *dw_void; /* manufactured void type */132tdesc_t *dw_long; /* manufactured long type for arrays */133size_t dw_ptrsz; /* size of a pointer in this file */134tid_t dw_mfgtid_last; /* last mfg'd type ID used */135uint_t dw_nunres; /* count of unresolved types */136char *dw_cuname; /* name of compilation unit */137} dwarf_t;138139static void die_create_one(dwarf_t *, Dwarf_Die);140static void die_create(dwarf_t *, Dwarf_Die);141142static tid_t143mfgtid_next(dwarf_t *dw)144{145return (++dw->dw_mfgtid_last);146}147148static void149tdesc_add(dwarf_t *dw, tdesc_t *tdp)150{151hash_add(dw->dw_tidhash, tdp);152}153154static tdesc_t *155tdesc_lookup(dwarf_t *dw, int tid)156{157tdesc_t tmpl;158void *tdp;159160tmpl.t_id = tid;161162if (hash_find(dw->dw_tidhash, &tmpl, &tdp))163return (tdp);164else165return (NULL);166}167168/*169* Resolve a tdesc down to a node which should have a size. Returns the size,170* zero if the size hasn't yet been determined.171*/172static size_t173tdesc_size(tdesc_t *tdp)174{175for (;;) {176switch (tdp->t_type) {177case INTRINSIC:178case POINTER:179case ARRAY:180case FUNCTION:181case STRUCT:182case UNION:183case ENUM:184return (tdp->t_size);185186case FORWARD:187return (0);188189case TYPEDEF:190case VOLATILE:191case CONST:192case RESTRICT:193tdp = tdp->t_tdesc;194continue;195196case 0: /* not yet defined */197return (0);198199default:200terminate("tdp %u: tdesc_size on unknown type %d\n",201tdp->t_id, tdp->t_type);202}203}204}205206static size_t207tdesc_bitsize(tdesc_t *tdp)208{209for (;;) {210switch (tdp->t_type) {211case INTRINSIC:212return (tdp->t_intr->intr_nbits);213214case ARRAY:215case FUNCTION:216case STRUCT:217case UNION:218case ENUM:219case POINTER:220return (tdp->t_size * NBBY);221222case FORWARD:223return (0);224225case TYPEDEF:226case VOLATILE:227case RESTRICT:228case CONST:229tdp = tdp->t_tdesc;230continue;231232case 0: /* not yet defined */233return (0);234235default:236terminate("tdp %u: tdesc_bitsize on unknown type %d\n",237tdp->t_id, tdp->t_type);238}239}240}241242static tdesc_t *243tdesc_basetype(tdesc_t *tdp)244{245for (;;) {246switch (tdp->t_type) {247case TYPEDEF:248case VOLATILE:249case RESTRICT:250case CONST:251tdp = tdp->t_tdesc;252break;253case 0: /* not yet defined */254return (NULL);255default:256return (tdp);257}258}259}260261static Dwarf_Off262die_off(dwarf_t *dw, Dwarf_Die die)263{264Dwarf_Off off;265266if (dwarf_dieoffset(die, &off, &dw->dw_err) == DW_DLV_OK)267return (off);268269terminate("failed to get offset for die: %s\n",270dwarf_errmsg(dw->dw_err));271/*NOTREACHED*/272return (0);273}274275static Dwarf_Die276die_sibling(dwarf_t *dw, Dwarf_Die die)277{278Dwarf_Die sib;279int rc;280281if ((rc = dwarf_siblingof(dw->dw_dw, die, &sib, &dw->dw_err)) ==282DW_DLV_OK)283return (sib);284else if (rc == DW_DLV_NO_ENTRY)285return (NULL);286287terminate("die %llu: failed to find type sibling: %s\n",288die_off(dw, die), dwarf_errmsg(dw->dw_err));289/*NOTREACHED*/290return (NULL);291}292293static Dwarf_Die294die_child(dwarf_t *dw, Dwarf_Die die)295{296Dwarf_Die child;297int rc;298299if ((rc = dwarf_child(die, &child, &dw->dw_err)) == DW_DLV_OK)300return (child);301else if (rc == DW_DLV_NO_ENTRY)302return (NULL);303304terminate("die %llu: failed to find type child: %s\n",305die_off(dw, die), dwarf_errmsg(dw->dw_err));306/*NOTREACHED*/307return (NULL);308}309310static Dwarf_Half311die_tag(dwarf_t *dw, Dwarf_Die die)312{313Dwarf_Half tag;314315if (dwarf_tag(die, &tag, &dw->dw_err) == DW_DLV_OK)316return (tag);317318terminate("die %llu: failed to get tag for type: %s\n",319die_off(dw, die), dwarf_errmsg(dw->dw_err));320/*NOTREACHED*/321return (0);322}323324static Dwarf_Attribute325die_attr(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, int req)326{327Dwarf_Attribute attr;328int rc;329330if ((rc = dwarf_attr(die, name, &attr, &dw->dw_err)) == DW_DLV_OK) {331return (attr);332} else if (rc == DW_DLV_NO_ENTRY) {333if (req) {334terminate("die %llu: no attr 0x%x\n", die_off(dw, die),335name);336} else {337return (NULL);338}339}340341terminate("die %llu: failed to get attribute for type: %s\n",342die_off(dw, die), dwarf_errmsg(dw->dw_err));343/*NOTREACHED*/344return (NULL);345}346347static int348die_signed(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Signed *valp,349int req)350{351*valp = 0;352if (dwarf_attrval_signed(die, name, valp, &dw->dw_err) != DW_DLV_OK) {353if (req)354terminate("die %llu: failed to get signed: %s\n",355die_off(dw, die), dwarf_errmsg(dw->dw_err));356return (0);357}358359return (1);360}361362static int363die_unsigned(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Unsigned *valp,364int req)365{366*valp = 0;367if (dwarf_attrval_unsigned(die, name, valp, &dw->dw_err) != DW_DLV_OK) {368if (req)369terminate("die %llu: failed to get unsigned: %s\n",370die_off(dw, die), dwarf_errmsg(dw->dw_err));371return (0);372}373374return (1);375}376377static int378die_bool(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Bool *valp, int req)379{380*valp = 0;381382if (dwarf_attrval_flag(die, name, valp, &dw->dw_err) != DW_DLV_OK) {383if (req)384terminate("die %llu: failed to get flag: %s\n",385die_off(dw, die), dwarf_errmsg(dw->dw_err));386return (0);387}388389return (1);390}391392static int393die_string(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, char **strp, int req)394{395const char *str = NULL;396397if (dwarf_attrval_string(die, name, &str, &dw->dw_err) != DW_DLV_OK ||398str == NULL) {399if (req)400terminate("die %llu: failed to get string: %s\n",401die_off(dw, die), dwarf_errmsg(dw->dw_err));402else403*strp = NULL;404return (0);405} else406*strp = xstrdup(str);407408return (1);409}410411static Dwarf_Off412die_attr_ref(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)413{414Dwarf_Off off;415416if (dwarf_attrval_unsigned(die, name, &off, &dw->dw_err) != DW_DLV_OK) {417terminate("die %llu: failed to get ref: %s\n",418die_off(dw, die), dwarf_errmsg(dw->dw_err));419}420421return (off);422}423424static char *425die_name(dwarf_t *dw, Dwarf_Die die)426{427char *str = NULL;428429(void) die_string(dw, die, DW_AT_name, &str, 0);430if (str == NULL)431str = xstrdup("");432433return (str);434}435436static int437die_isdecl(dwarf_t *dw, Dwarf_Die die)438{439Dwarf_Bool val;440441return (die_bool(dw, die, DW_AT_declaration, &val, 0) && val);442}443444static int445die_isglobal(dwarf_t *dw, Dwarf_Die die)446{447Dwarf_Signed vis;448Dwarf_Bool ext;449450/*451* Some compilers (gcc) use DW_AT_external to indicate function452* visibility. Others (Sun) use DW_AT_visibility.453*/454if (die_signed(dw, die, DW_AT_visibility, &vis, 0))455return (vis == DW_VIS_exported);456else457return (die_bool(dw, die, DW_AT_external, &ext, 0) && ext);458}459460static tdesc_t *461die_add(dwarf_t *dw, Dwarf_Off off)462{463tdesc_t *tdp = xcalloc(sizeof (tdesc_t));464465tdp->t_id = off;466467tdesc_add(dw, tdp);468469return (tdp);470}471472static tdesc_t *473die_lookup_pass1(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)474{475Dwarf_Off ref = die_attr_ref(dw, die, name);476tdesc_t *tdp;477478if ((tdp = tdesc_lookup(dw, ref)) != NULL)479return (tdp);480481return (die_add(dw, ref));482}483484static int485die_mem_offset(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name,486Dwarf_Unsigned *valp, int req __unused)487{488Dwarf_Locdesc *loc = NULL;489Dwarf_Signed locnum = 0;490Dwarf_Attribute at;491Dwarf_Half form;492493if (name != DW_AT_data_member_location)494terminate("die %llu: can only process attribute "495"DW_AT_data_member_location\n", die_off(dw, die));496497if ((at = die_attr(dw, die, name, 0)) == NULL)498return (0);499500if (dwarf_whatform(at, &form, &dw->dw_err) != DW_DLV_OK)501return (0);502503switch (form) {504case DW_FORM_sec_offset:505case DW_FORM_block:506case DW_FORM_block1:507case DW_FORM_block2:508case DW_FORM_block4:509/*510* GCC in base and Clang (3.3 or below) generates511* DW_AT_data_member_location attribute with DW_FORM_block*512* form. The attribute contains one DW_OP_plus_uconst513* operator. The member offset stores in the operand.514*/515if (dwarf_loclist(at, &loc, &locnum, &dw->dw_err) != DW_DLV_OK)516return (0);517if (locnum != 1 || loc->ld_s->lr_atom != DW_OP_plus_uconst) {518terminate("die %llu: cannot parse member offset with "519"operator other than DW_OP_plus_uconst\n",520die_off(dw, die));521}522*valp = loc->ld_s->lr_number;523if (loc != NULL) {524dwarf_dealloc(dw->dw_dw, loc->ld_s, DW_DLA_LOC_BLOCK);525dwarf_dealloc(dw->dw_dw, loc, DW_DLA_LOCDESC);526}527break;528529case DW_FORM_data1:530case DW_FORM_data2:531case DW_FORM_data4:532case DW_FORM_data8:533case DW_FORM_udata:534/*535* Clang 3.4 generates DW_AT_data_member_location attribute536* with DW_FORM_data* form (constant class). The attribute537* stores a contant value which is the member offset.538*539* However, note that DW_FORM_data[48] in DWARF version 2 or 3540* could be used as a section offset (offset into .debug_loc in541* this case). Here we assume the attribute always stores a542* constant because we know Clang 3.4 does this and GCC in543* base won't emit DW_FORM_data[48] for this attribute. This544* code will remain correct if future vesrions of Clang and545* GCC conform to DWARF4 standard and only use the form546* DW_FORM_sec_offset for section offset.547*/548if (dwarf_attrval_unsigned(die, name, valp, &dw->dw_err) !=549DW_DLV_OK)550return (0);551break;552553default:554terminate("die %llu: cannot parse member offset with form "555"%u\n", die_off(dw, die), form);556}557558return (1);559}560561static tdesc_t *562tdesc_intr_common(dwarf_t *dw, int tid, const char *name, size_t sz)563{564tdesc_t *tdp;565intr_t *intr;566567intr = xcalloc(sizeof (intr_t));568intr->intr_type = INTR_INT;569intr->intr_signed = 1;570intr->intr_nbits = sz * NBBY;571572tdp = xcalloc(sizeof (tdesc_t));573tdp->t_name = xstrdup(name);574tdp->t_size = sz;575tdp->t_id = tid;576tdp->t_type = INTRINSIC;577tdp->t_intr = intr;578tdp->t_flags = TDESC_F_RESOLVED;579580tdesc_add(dw, tdp);581582return (tdp);583}584585/*586* Manufacture a void type. Used for gcc-emitted stabs, where the lack of a587* type reference implies a reference to a void type. A void *, for example588* will be represented by a pointer die without a DW_AT_type. CTF requires589* that pointer nodes point to something, so we'll create a void for use as590* the target. Note that the DWARF data may already create a void type. Ours591* would then be a duplicate, but it'll be removed in the self-uniquification592* merge performed at the completion of DWARF->tdesc conversion.593*/594static tdesc_t *595tdesc_intr_void(dwarf_t *dw)596{597if (dw->dw_void == NULL)598dw->dw_void = tdesc_intr_common(dw, TID_VOID, "void", 0);599600return (dw->dw_void);601}602603static tdesc_t *604tdesc_intr_long(dwarf_t *dw)605{606if (dw->dw_long == NULL) {607dw->dw_long = tdesc_intr_common(dw, TID_LONG, "long",608dw->dw_ptrsz);609}610611return (dw->dw_long);612}613614/*615* Used for creating bitfield types. We create a copy of an existing intrinsic,616* adjusting the size of the copy to match what the caller requested. The617* caller can then use the copy as the type for a bitfield structure member.618*/619static tdesc_t *620tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz, const char *suffix)621{622tdesc_t *new = xcalloc(sizeof (tdesc_t));623624if (!(old->t_flags & TDESC_F_RESOLVED)) {625terminate("tdp %u: attempt to make a bit field from an "626"unresolved type\n", old->t_id);627}628629xasprintf(&new->t_name, "%s %s", old->t_name, suffix);630new->t_size = old->t_size;631new->t_id = mfgtid_next(dw);632new->t_type = INTRINSIC;633new->t_flags = TDESC_F_RESOLVED;634635new->t_intr = xcalloc(sizeof (intr_t));636bcopy(old->t_intr, new->t_intr, sizeof (intr_t));637new->t_intr->intr_nbits = bitsz;638639tdesc_add(dw, new);640641return (new);642}643644static void645tdesc_array_create(dwarf_t *dw, Dwarf_Die dim, tdesc_t *arrtdp,646tdesc_t *dimtdp)647{648Dwarf_Unsigned uval;649Dwarf_Signed sval;650tdesc_t *ctdp = NULL;651Dwarf_Die dim2;652ardef_t *ar;653654if ((dim2 = die_sibling(dw, dim)) == NULL) {655ctdp = arrtdp;656} else if (die_tag(dw, dim2) == DW_TAG_subrange_type) {657ctdp = xcalloc(sizeof (tdesc_t));658ctdp->t_id = mfgtid_next(dw);659debug(3, "die %llu: creating new type %u for sub-dimension\n",660die_off(dw, dim2), ctdp->t_id);661tdesc_array_create(dw, dim2, arrtdp, ctdp);662} else {663terminate("die %llu: unexpected non-subrange node in array\n",664die_off(dw, dim2));665}666667dimtdp->t_type = ARRAY;668dimtdp->t_ardef = ar = xcalloc(sizeof (ardef_t));669670/*671* Array bounds can be signed or unsigned, but there are several kinds672* of signless forms (data1, data2, etc) that take their sign from the673* routine that is trying to interpret them. That is, data1 can be674* either signed or unsigned, depending on whether you use the signed or675* unsigned accessor function. GCC will use the signless forms to store676* unsigned values which have their high bit set, so we need to try to677* read them first as unsigned to get positive values. We could also678* try signed first, falling back to unsigned if we got a negative679* value.680*/681if (die_unsigned(dw, dim, DW_AT_upper_bound, &uval, 0))682ar->ad_nelems = uval + 1;683else if (die_signed(dw, dim, DW_AT_upper_bound, &sval, 0))684ar->ad_nelems = sval + 1;685else if (die_unsigned(dw, dim, DW_AT_count, &uval, 0))686ar->ad_nelems = uval;687else if (die_signed(dw, dim, DW_AT_count, &sval, 0))688ar->ad_nelems = sval;689else690ar->ad_nelems = 0;691692/*693* Different compilers use different index types. Force the type to be694* a common, known value (long).695*/696ar->ad_idxtype = tdesc_intr_long(dw);697ar->ad_contents = ctdp;698699if (ar->ad_contents->t_size != 0) {700dimtdp->t_size = ar->ad_contents->t_size * ar->ad_nelems;701dimtdp->t_flags |= TDESC_F_RESOLVED;702}703}704705/*706* Create a tdesc from an array node. Some arrays will come with byte size707* attributes, and thus can be resolved immediately. Others don't, and will708* need to wait until the second pass for resolution.709*/710static void711die_array_create(dwarf_t *dw, Dwarf_Die arr, Dwarf_Off off, tdesc_t *tdp)712{713tdesc_t *arrtdp = die_lookup_pass1(dw, arr, DW_AT_type);714Dwarf_Unsigned uval;715Dwarf_Die dim;716717debug(3, "die %llu <%llx>: creating array\n", off, off);718719if ((dim = die_child(dw, arr)) == NULL ||720die_tag(dw, dim) != DW_TAG_subrange_type)721terminate("die %llu: failed to retrieve array bounds\n", off);722723tdesc_array_create(dw, dim, arrtdp, tdp);724725if (die_unsigned(dw, arr, DW_AT_byte_size, &uval, 0)) {726tdesc_t *dimtdp;727int flags;728729tdp->t_size = uval;730731/*732* Ensure that sub-dimensions have sizes too before marking733* as resolved.734*/735flags = TDESC_F_RESOLVED;736for (dimtdp = tdp->t_ardef->ad_contents;737dimtdp->t_type == ARRAY;738dimtdp = dimtdp->t_ardef->ad_contents) {739if (!(dimtdp->t_flags & TDESC_F_RESOLVED)) {740flags = 0;741break;742}743}744745tdp->t_flags |= flags;746}747748debug(3, "die %llu <%llx>: array nelems %u size %u\n", off, off,749tdp->t_ardef->ad_nelems, tdp->t_size);750}751752/*ARGSUSED1*/753static int754die_array_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)755{756dwarf_t *dw = private;757size_t sz;758759if (tdp->t_flags & TDESC_F_RESOLVED)760return (1);761762debug(3, "trying to resolve array %d (cont %d)\n", tdp->t_id,763tdp->t_ardef->ad_contents->t_id);764765if ((sz = tdesc_size(tdp->t_ardef->ad_contents)) == 0 &&766(tdp->t_ardef->ad_contents->t_flags & TDESC_F_RESOLVED) == 0) {767debug(3, "unable to resolve array %s (%d) contents %d\n",768tdesc_name(tdp), tdp->t_id,769tdp->t_ardef->ad_contents->t_id);770771dw->dw_nunres++;772return (1);773}774775tdp->t_size = sz * tdp->t_ardef->ad_nelems;776tdp->t_flags |= TDESC_F_RESOLVED;777778debug(3, "resolved array %d: %u bytes\n", tdp->t_id, tdp->t_size);779780return (1);781}782783/*ARGSUSED1*/784static int785die_array_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused)786{787tdesc_t *cont = tdp->t_ardef->ad_contents;788789if (tdp->t_flags & TDESC_F_RESOLVED)790return (1);791792fprintf(stderr, "Array %d: failed to size contents type %s (%d)\n",793tdp->t_id, tdesc_name(cont), cont->t_id);794795return (1);796}797798/*799* Most enums (those with members) will be resolved during this first pass.800* Others - those without members (see the file comment) - won't be, and will801* need to wait until the second pass when they can be matched with their full802* definitions.803*/804static void805die_enum_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)806{807Dwarf_Die mem;808Dwarf_Unsigned uval;809Dwarf_Signed sval;810811if (die_isdecl(dw, die)) {812tdp->t_type = FORWARD;813return;814}815816debug(3, "die %llu: creating enum\n", off);817818tdp->t_type = ENUM;819820(void) die_unsigned(dw, die, DW_AT_byte_size, &uval, DW_ATTR_REQ);821tdp->t_size = uval;822823if ((mem = die_child(dw, die)) != NULL) {824elist_t **elastp = &tdp->t_emem;825826do {827elist_t *el;828829if (die_tag(dw, mem) != DW_TAG_enumerator) {830/* Nested type declaration */831die_create_one(dw, mem);832continue;833}834835el = xcalloc(sizeof (elist_t));836el->el_name = die_name(dw, mem);837838if (die_signed(dw, mem, DW_AT_const_value, &sval, 0)) {839el->el_number = sval;840} else if (die_unsigned(dw, mem, DW_AT_const_value,841&uval, 0)) {842el->el_number = uval;843} else {844terminate("die %llu: enum %llu: member without "845"value\n", off, die_off(dw, mem));846}847848debug(3, "die %llu: enum %llu: created %s = %d\n", off,849die_off(dw, mem), el->el_name, el->el_number);850851*elastp = el;852elastp = &el->el_next;853854} while ((mem = die_sibling(dw, mem)) != NULL);855856hash_add(dw->dw_enumhash, tdp);857858tdp->t_flags |= TDESC_F_RESOLVED;859860if (tdp->t_name != NULL) {861iidesc_t *ii = xcalloc(sizeof (iidesc_t));862ii->ii_type = II_SOU;863ii->ii_name = xstrdup(tdp->t_name);864ii->ii_dtype = tdp;865866iidesc_add(dw->dw_td->td_iihash, ii);867}868}869}870871static int872die_enum_match(void *arg1, void *arg2)873{874tdesc_t *tdp = arg1, **fullp = arg2;875876if (tdp->t_emem != NULL) {877*fullp = tdp;878return (-1); /* stop the iteration */879}880881return (0);882}883884/*ARGSUSED1*/885static int886die_enum_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)887{888dwarf_t *dw = private;889tdesc_t *full = NULL;890891if (tdp->t_flags & TDESC_F_RESOLVED)892return (1);893894(void) hash_find_iter(dw->dw_enumhash, tdp, die_enum_match, &full);895896/*897* The answer to this one won't change from iteration to iteration,898* so don't even try.899*/900if (full == NULL) {901terminate("tdp %u: enum %s has no members\n", tdp->t_id,902tdesc_name(tdp));903}904905debug(3, "tdp %u: enum %s redirected to %u\n", tdp->t_id,906tdesc_name(tdp), full->t_id);907908tdp->t_flags |= TDESC_F_RESOLVED;909910return (1);911}912913static int914die_fwd_map(void *arg1, void *arg2)915{916tdesc_t *fwd = arg1, *sou = arg2;917918debug(3, "tdp %u: mapped forward %s to sou %u\n", fwd->t_id,919tdesc_name(fwd), sou->t_id);920fwd->t_tdesc = sou;921922return (0);923}924925/*926* Structures and unions will never be resolved during the first pass, as we927* won't be able to fully determine the member sizes. The second pass, which928* have access to sizing information, will be able to complete the resolution.929*/930static void931die_sou_create(dwarf_t *dw, Dwarf_Die str, Dwarf_Off off, tdesc_t *tdp,932int type, const char *typename)933{934Dwarf_Unsigned sz, bitsz, bitoff;935#if BYTE_ORDER == _LITTLE_ENDIAN936Dwarf_Unsigned bysz;937#endif938Dwarf_Die mem;939mlist_t *ml, **mlastp;940iidesc_t *ii;941942tdp->t_type = (die_isdecl(dw, str) ? FORWARD : type);943944debug(3, "die %llu: creating %s %s\n", off,945(tdp->t_type == FORWARD ? "forward decl" : typename),946tdesc_name(tdp));947948if (tdp->t_type == FORWARD) {949hash_add(dw->dw_fwdhash, tdp);950return;951}952953(void) hash_find_iter(dw->dw_fwdhash, tdp, die_fwd_map, tdp);954955(void) die_unsigned(dw, str, DW_AT_byte_size, &sz, DW_ATTR_REQ);956tdp->t_size = sz;957958/*959* GCC allows empty SOUs as an extension.960*/961if ((mem = die_child(dw, str)) == NULL) {962goto out;963}964965mlastp = &tdp->t_members;966967do {968Dwarf_Off memoff = die_off(dw, mem);969Dwarf_Half tag = die_tag(dw, mem);970Dwarf_Unsigned mloff;971972if (tag != DW_TAG_member) {973/* Nested type declaration */974die_create_one(dw, mem);975continue;976}977978debug(3, "die %llu: mem %llu: creating member\n", off, memoff);979980ml = xcalloc(sizeof (mlist_t));981982/*983* This could be a GCC anon struct/union member, so we'll allow984* an empty name, even though nothing can really handle them985* properly. Note that some versions of GCC miss out debug986* info for anon structs, though recent versions are fixed (gcc987* bug 11816).988*/989if ((ml->ml_name = die_name(dw, mem)) == NULL)990ml->ml_name = NULL;991992ml->ml_type = die_lookup_pass1(dw, mem, DW_AT_type);993994if (die_mem_offset(dw, mem, DW_AT_data_member_location,995&mloff, 0)) {996debug(3, "die %llu: got mloff %llx\n", off,997(u_longlong_t)mloff);998ml->ml_offset = mloff * 8;999}10001001if (die_unsigned(dw, mem, DW_AT_bit_size, &bitsz, 0))1002ml->ml_size = bitsz;1003else1004ml->ml_size = tdesc_bitsize(ml->ml_type);10051006if (die_unsigned(dw, mem, DW_AT_data_bit_offset, &bitoff, 0)) {1007ml->ml_offset += bitoff;1008} else if (die_unsigned(dw, mem, DW_AT_bit_offset, &bitoff, 0)) {1009#if BYTE_ORDER == _BIG_ENDIAN1010ml->ml_offset += bitoff;1011#else1012/*1013* Note that Clang 3.4 will sometimes generate1014* member DIE before generating the DIE for the1015* member's type. The code can not handle this1016* properly so that tdesc_bitsize(ml->ml_type) will1017* return 0 because ml->ml_type is unknown. As a1018* result, a wrong member offset will be calculated.1019* To workaround this, we can instead try to1020* retrieve the value of DW_AT_byte_size attribute1021* which stores the byte size of the space occupied1022* by the type. If this attribute exists, its value1023* should equal to tdesc_bitsize(ml->ml_type)/NBBY.1024*/1025if (die_unsigned(dw, mem, DW_AT_byte_size, &bysz, 0) &&1026bysz > 0)1027ml->ml_offset += bysz * NBBY - bitoff -1028ml->ml_size;1029else1030ml->ml_offset += tdesc_bitsize(ml->ml_type) -1031bitoff - ml->ml_size;1032#endif1033}10341035debug(3, "die %llu: mem %llu: created \"%s\" (off %u sz %u)\n",1036off, memoff, ml->ml_name, ml->ml_offset, ml->ml_size);10371038*mlastp = ml;1039mlastp = &ml->ml_next;1040} while ((mem = die_sibling(dw, mem)) != NULL);10411042/*1043* GCC will attempt to eliminate unused types, thus decreasing the1044* size of the emitted dwarf. That is, if you declare a foo_t in your1045* header, include said header in your source file, and neglect to1046* actually use (directly or indirectly) the foo_t in the source file,1047* the foo_t won't make it into the emitted DWARF. So, at least, goes1048* the theory.1049*1050* Occasionally, it'll emit the DW_TAG_structure_type for the foo_t,1051* and then neglect to emit the members. Strangely, the loner struct1052* tag will always be followed by a proper nested declaration of1053* something else. This is clearly a bug, but we're not going to have1054* time to get it fixed before this goo goes back, so we'll have to work1055* around it. If we see a no-membered struct with a nested declaration1056* (i.e. die_child of the struct tag won't be null), we'll ignore it.1057* Being paranoid, we won't simply remove it from the hash. Instead,1058* we'll decline to create an iidesc for it, thus ensuring that this1059* type won't make it into the output file. To be safe, we'll also1060* change the name.1061*/1062if (tdp->t_members == NULL) {1063const char *old = tdesc_name(tdp);1064size_t newsz = 7 + strlen(old) + 1;1065char *new = xmalloc(newsz);1066(void) snprintf(new, newsz, "orphan %s", old);10671068debug(3, "die %llu: worked around %s %s\n", off, typename, old);10691070if (tdp->t_name != NULL)1071free(tdp->t_name);1072tdp->t_name = new;1073return;1074}10751076out:1077if (tdp->t_name != NULL) {1078ii = xcalloc(sizeof (iidesc_t));1079ii->ii_type = II_SOU;1080ii->ii_name = xstrdup(tdp->t_name);1081ii->ii_dtype = tdp;10821083iidesc_add(dw->dw_td->td_iihash, ii);1084}1085}10861087static void1088die_struct_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)1089{1090die_sou_create(dw, die, off, tdp, STRUCT, "struct");1091}10921093static void1094die_union_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)1095{1096die_sou_create(dw, die, off, tdp, UNION, "union");1097}10981099/*ARGSUSED1*/1100static int1101die_sou_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)1102{1103dwarf_t *dw = private;1104mlist_t *ml;1105tdesc_t *mt;11061107if (tdp->t_flags & TDESC_F_RESOLVED)1108return (1);11091110debug(3, "resolving sou %s\n", tdesc_name(tdp));11111112for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {1113if (ml->ml_size == 0) {1114mt = tdesc_basetype(ml->ml_type);11151116if ((ml->ml_size = tdesc_bitsize(mt)) != 0)1117continue;11181119/*1120* For empty members, or GCC/C99 flexible array1121* members, a size of 0 is correct. Structs and unions1122* consisting of flexible array members will also have1123* size 0.1124*/1125if (mt->t_members == NULL)1126continue;1127if (mt->t_type == ARRAY) {1128if (mt->t_ardef->ad_nelems == 0)1129continue;1130mt = tdesc_basetype(mt->t_ardef->ad_contents);1131if ((mt->t_flags & TDESC_F_RESOLVED) != 0 &&1132(mt->t_type == STRUCT ||1133mt->t_type == UNION) &&1134mt->t_members == NULL)1135continue;1136}1137if ((mt->t_flags & TDESC_F_RESOLVED) != 0 &&1138(mt->t_type == STRUCT || mt->t_type == UNION))1139continue;11401141dw->dw_nunres++;1142return (1);1143}11441145if ((mt = tdesc_basetype(ml->ml_type)) == NULL) {1146dw->dw_nunres++;1147return (1);1148}11491150if (ml->ml_size != 0 && mt->t_type == INTRINSIC &&1151mt->t_intr->intr_nbits != ml->ml_size) {1152/*1153* This member is a bitfield, and needs to reference1154* an intrinsic type with the same width. If the1155* currently-referenced type isn't of the same width,1156* we'll copy it, adjusting the width of the copy to1157* the size we'd like.1158*/1159debug(3, "tdp %u: creating bitfield for %d bits\n",1160tdp->t_id, ml->ml_size);11611162ml->ml_type = tdesc_intr_clone(dw, mt, ml->ml_size,1163"bitfield");1164}1165}11661167tdp->t_flags |= TDESC_F_RESOLVED;11681169return (1);1170}11711172/*ARGSUSED1*/1173static int1174die_sou_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused)1175{1176const char *typename = (tdp->t_type == STRUCT ? "struct" : "union");1177mlist_t *ml;11781179if (tdp->t_flags & TDESC_F_RESOLVED)1180return (1);11811182for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {1183if (ml->ml_size == 0) {1184fprintf(stderr, "%s %d <%x>: failed to size member \"%s\" "1185"of type %s (%d <%x>)\n", typename, tdp->t_id,1186tdp->t_id,1187ml->ml_name, tdesc_name(ml->ml_type),1188ml->ml_type->t_id, ml->ml_type->t_id);1189}1190}11911192return (1);1193}11941195static void1196die_funcptr_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)1197{1198Dwarf_Attribute attr;1199Dwarf_Half tag;1200Dwarf_Die arg;1201fndef_t *fn;1202int i;12031204debug(3, "die %llu <%llx>: creating function pointer\n", off, off);12051206/*1207* We'll begin by processing any type definition nodes that may be1208* lurking underneath this one.1209*/1210for (arg = die_child(dw, die); arg != NULL;1211arg = die_sibling(dw, arg)) {1212if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&1213tag != DW_TAG_unspecified_parameters) {1214/* Nested type declaration */1215die_create_one(dw, arg);1216}1217}12181219if (die_isdecl(dw, die)) {1220/*1221* This is a prototype. We don't add prototypes to the1222* tree, so we're going to drop the tdesc. Unfortunately,1223* it has already been added to the tree. Nobody will reference1224* it, though, and it will be leaked.1225*/1226return;1227}12281229fn = xcalloc(sizeof (fndef_t));12301231tdp->t_type = FUNCTION;12321233if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {1234fn->fn_ret = die_lookup_pass1(dw, die, DW_AT_type);1235} else {1236fn->fn_ret = tdesc_intr_void(dw);1237}12381239/*1240* Count the arguments to the function, then read them in.1241*/1242for (fn->fn_nargs = 0, arg = die_child(dw, die); arg != NULL;1243arg = die_sibling(dw, arg)) {1244if ((tag = die_tag(dw, arg)) == DW_TAG_formal_parameter)1245fn->fn_nargs++;1246else if (tag == DW_TAG_unspecified_parameters &&1247fn->fn_nargs > 0)1248fn->fn_vargs = 1;1249}12501251if (fn->fn_nargs != 0) {1252debug(3, "die %llu: adding %d argument%s\n", off, fn->fn_nargs,1253(fn->fn_nargs > 1 ? "s" : ""));12541255fn->fn_args = xcalloc(sizeof (tdesc_t *) * fn->fn_nargs);1256for (i = 0, arg = die_child(dw, die);1257arg != NULL && i < (int) fn->fn_nargs;1258arg = die_sibling(dw, arg)) {1259if (die_tag(dw, arg) != DW_TAG_formal_parameter)1260continue;12611262fn->fn_args[i++] = die_lookup_pass1(dw, arg,1263DW_AT_type);1264}1265}12661267tdp->t_fndef = fn;1268tdp->t_flags |= TDESC_F_RESOLVED;1269}12701271/*1272* GCC and DevPro use different names for the base types. While the terms are1273* the same, they are arranged in a different order. Some terms, such as int,1274* are implied in one, and explicitly named in the other. Given a base type1275* as input, this routine will return a common name, along with an intr_t1276* that reflects said name.1277*/1278static intr_t *1279die_base_name_parse(const char *name, char **newp)1280{1281char buf[256];1282char const *base;1283char *c;1284int nlong = 0, nshort = 0, nchar = 0, nint = 0;1285int sign = 1;1286char fmt = '\0';1287intr_t *intr;12881289if (strlen(name) > sizeof (buf) - 1)1290terminate("base type name \"%s\" is too long\n", name);12911292strncpy(buf, name, sizeof (buf));12931294for (c = strtok(buf, " "); c != NULL; c = strtok(NULL, " ")) {1295if (strcmp(c, "signed") == 0)1296sign = 1;1297else if (strcmp(c, "unsigned") == 0)1298sign = 0;1299else if (strcmp(c, "long") == 0)1300nlong++;1301else if (strcmp(c, "char") == 0) {1302nchar++;1303fmt = 'c';1304} else if (strcmp(c, "short") == 0)1305nshort++;1306else if (strcmp(c, "int") == 0)1307nint++;1308else {1309/*1310* If we don't recognize any of the tokens, we'll tell1311* the caller to fall back to the dwarf-provided1312* encoding information.1313*/1314return (NULL);1315}1316}13171318if (nchar > 1 || nshort > 1 || nint > 1 || nlong > 2)1319return (NULL);13201321if (nchar > 0) {1322if (nlong > 0 || nshort > 0 || nint > 0)1323return (NULL);13241325base = "char";13261327} else if (nshort > 0) {1328if (nlong > 0)1329return (NULL);13301331base = "short";13321333} else if (nlong > 0) {1334base = "long";13351336} else {1337base = "int";1338}13391340intr = xcalloc(sizeof (intr_t));1341intr->intr_type = INTR_INT;1342intr->intr_signed = sign;1343intr->intr_iformat = fmt;13441345snprintf(buf, sizeof (buf), "%s%s%s",1346(sign ? "" : "unsigned "),1347(nlong > 1 ? "long " : ""),1348base);13491350*newp = xstrdup(buf);1351return (intr);1352}13531354typedef struct fp_size_map {1355size_t fsm_typesz[2]; /* size of {32,64} type */1356uint_t fsm_enc[3]; /* CTF_FP_* for {bare,cplx,imagry} type */1357} fp_size_map_t;13581359static const fp_size_map_t fp_encodings[] = {1360{ { 4, 4 }, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },1361{ { 8, 8 }, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },1362#ifdef __sparc1363{ { 16, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },1364#else1365{ { 12, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },1366#endif1367{ { 0, 0 }, { 0, 0, 0 } }1368};13691370static uint_t1371die_base_type2enc(dwarf_t *dw, Dwarf_Off off, Dwarf_Unsigned enc, size_t sz)1372{1373const fp_size_map_t *map = fp_encodings;1374uint_t szidx = dw->dw_ptrsz == sizeof (uint64_t);1375uint_t mult = 1, col = 0;13761377if (enc == DW_ATE_complex_float) {1378mult = 2;1379col = 1;1380} else if (enc == DW_ATE_imaginary_float1381#ifdef illumos1382|| enc == DW_ATE_SUN_imaginary_float1383#endif1384)1385col = 2;13861387while (map->fsm_typesz[szidx] != 0) {1388if (map->fsm_typesz[szidx] * mult == sz)1389return (map->fsm_enc[col]);1390map++;1391}13921393terminate("die %llu: unrecognized real type size %u\n", off, sz);1394/*NOTREACHED*/1395return (0);1396}13971398static intr_t *1399die_base_from_dwarf(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, size_t sz)1400{1401intr_t *intr = xcalloc(sizeof (intr_t));1402Dwarf_Unsigned enc;14031404(void) die_unsigned(dw, base, DW_AT_encoding, &enc, DW_ATTR_REQ);14051406switch (enc) {1407case DW_ATE_unsigned:1408case DW_ATE_address:1409intr->intr_type = INTR_INT;1410break;1411case DW_ATE_unsigned_char:1412intr->intr_type = INTR_INT;1413intr->intr_iformat = 'c';1414break;1415case DW_ATE_signed:1416intr->intr_type = INTR_INT;1417intr->intr_signed = 1;1418break;1419case DW_ATE_signed_char:1420intr->intr_type = INTR_INT;1421intr->intr_signed = 1;1422intr->intr_iformat = 'c';1423break;1424case DW_ATE_boolean:1425intr->intr_type = INTR_INT;1426intr->intr_signed = 1;1427intr->intr_iformat = 'b';1428break;1429case DW_ATE_float:1430case DW_ATE_complex_float:1431case DW_ATE_imaginary_float:1432#ifdef illumos1433case DW_ATE_SUN_imaginary_float:1434case DW_ATE_SUN_interval_float:1435#endif1436intr->intr_type = INTR_REAL;1437intr->intr_signed = 1;1438intr->intr_fformat = die_base_type2enc(dw, off, enc, sz);1439break;1440default:1441terminate("die %llu: unknown base type encoding 0x%llx\n",1442off, enc);1443}14441445return (intr);1446}14471448static void1449die_base_create(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, tdesc_t *tdp)1450{1451Dwarf_Unsigned sz;1452intr_t *intr;1453char *new;14541455debug(3, "die %llu: creating base type\n", off);14561457/*1458* The compilers have their own clever (internally inconsistent) ideas1459* as to what base types should look like. Some times gcc will, for1460* example, use DW_ATE_signed_char for char. Other times, however, it1461* will use DW_ATE_signed. Needless to say, this causes some problems1462* down the road, particularly with merging. We do, however, use the1463* DWARF idea of type sizes, as this allows us to avoid caring about1464* the data model.1465*/1466(void) die_unsigned(dw, base, DW_AT_byte_size, &sz, DW_ATTR_REQ);14671468if (tdp->t_name == NULL)1469terminate("die %llu: base type without name\n", off);14701471/* XXX make a name parser for float too */1472if ((intr = die_base_name_parse(tdp->t_name, &new)) != NULL) {1473/* Found it. We'll use the parsed version */1474debug(3, "die %llu: name \"%s\" remapped to \"%s\"\n", off,1475tdesc_name(tdp), new);14761477free(tdp->t_name);1478tdp->t_name = new;1479} else {1480/*1481* We didn't recognize the type, so we'll create an intr_t1482* based on the DWARF data.1483*/1484debug(3, "die %llu: using dwarf data for base \"%s\"\n", off,1485tdesc_name(tdp));14861487intr = die_base_from_dwarf(dw, base, off, sz);1488}14891490intr->intr_nbits = sz * 8;14911492tdp->t_type = INTRINSIC;1493tdp->t_intr = intr;1494tdp->t_size = sz;14951496tdp->t_flags |= TDESC_F_RESOLVED;1497}14981499static void1500die_through_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp,1501int type, const char *typename)1502{1503Dwarf_Attribute attr;15041505debug(3, "die %llu <%llx>: creating %s type %d\n", off, off, typename, type);15061507tdp->t_type = type;15081509if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {1510tdp->t_tdesc = die_lookup_pass1(dw, die, DW_AT_type);1511} else {1512tdp->t_tdesc = tdesc_intr_void(dw);1513}15141515if (type == POINTER)1516tdp->t_size = dw->dw_ptrsz;15171518tdp->t_flags |= TDESC_F_RESOLVED;15191520if (type == TYPEDEF) {1521iidesc_t *ii = xcalloc(sizeof (iidesc_t));1522ii->ii_type = II_TYPE;1523ii->ii_name = xstrdup(tdp->t_name);1524ii->ii_dtype = tdp;15251526iidesc_add(dw->dw_td->td_iihash, ii);1527}1528}15291530static void1531die_typedef_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)1532{1533die_through_create(dw, die, off, tdp, TYPEDEF, "typedef");1534}15351536static void1537die_const_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)1538{1539die_through_create(dw, die, off, tdp, CONST, "const");1540}15411542static void1543die_pointer_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)1544{1545die_through_create(dw, die, off, tdp, POINTER, "pointer");1546}15471548static void1549die_restrict_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)1550{1551die_through_create(dw, die, off, tdp, RESTRICT, "restrict");1552}15531554static void1555die_volatile_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)1556{1557die_through_create(dw, die, off, tdp, VOLATILE, "volatile");1558}15591560/*ARGSUSED3*/1561static void1562die_function_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused)1563{1564Dwarf_Die arg;1565Dwarf_Half tag;1566iidesc_t *ii;1567char *name;15681569debug(3, "die %llu <%llx>: creating function definition\n", off, off);15701571/*1572* We'll begin by processing any type definition nodes that may be1573* lurking underneath this one.1574*/1575for (arg = die_child(dw, die); arg != NULL;1576arg = die_sibling(dw, arg)) {1577if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&1578tag != DW_TAG_variable) {1579/* Nested type declaration */1580die_create_one(dw, arg);1581}1582}15831584if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) {1585/*1586* We process neither prototypes nor subprograms without1587* names.1588*/1589return;1590}15911592ii = xcalloc(sizeof (iidesc_t));1593ii->ii_type = die_isglobal(dw, die) ? II_GFUN : II_SFUN;1594ii->ii_name = name;1595if (ii->ii_type == II_SFUN)1596ii->ii_owner = xstrdup(dw->dw_cuname);15971598debug(3, "die %llu: function %s is %s\n", off, ii->ii_name,1599(ii->ii_type == II_GFUN ? "global" : "static"));16001601if (die_attr(dw, die, DW_AT_type, 0) != NULL)1602ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);1603else1604ii->ii_dtype = tdesc_intr_void(dw);16051606for (arg = die_child(dw, die); arg != NULL;1607arg = die_sibling(dw, arg)) {1608char *name1;16091610debug(3, "die %llu: looking at sub member at %llu\n",1611off, die_off(dw, die));16121613if (die_tag(dw, arg) != DW_TAG_formal_parameter)1614continue;16151616if ((name1 = die_name(dw, arg)) == NULL) {1617terminate("die %llu: func arg %d has no name\n",1618off, ii->ii_nargs + 1);1619}16201621if (strcmp(name1, "...") == 0) {1622free(name1);1623ii->ii_vargs = 1;1624continue;1625}1626free(name1);16271628ii->ii_nargs++;1629}16301631if (ii->ii_nargs > 0) {1632int i;16331634debug(3, "die %llu: function has %d argument%s\n", off,1635ii->ii_nargs, (ii->ii_nargs == 1 ? "" : "s"));16361637ii->ii_args = xcalloc(sizeof (tdesc_t) * ii->ii_nargs);16381639for (arg = die_child(dw, die), i = 0;1640arg != NULL && i < ii->ii_nargs;1641arg = die_sibling(dw, arg)) {1642if (die_tag(dw, arg) != DW_TAG_formal_parameter)1643continue;16441645ii->ii_args[i++] = die_lookup_pass1(dw, arg,1646DW_AT_type);1647}1648}16491650iidesc_add(dw->dw_td->td_iihash, ii);1651}16521653/*ARGSUSED3*/1654static void1655die_variable_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused)1656{1657iidesc_t *ii;1658char *name;16591660debug(3, "die %llu: creating object definition\n", off);16611662if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL)1663return; /* skip prototypes and nameless objects */16641665ii = xcalloc(sizeof (iidesc_t));1666ii->ii_type = die_isglobal(dw, die) ? II_GVAR : II_SVAR;1667ii->ii_name = name;1668ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);1669if (ii->ii_type == II_SVAR)1670ii->ii_owner = xstrdup(dw->dw_cuname);16711672iidesc_add(dw->dw_td->td_iihash, ii);1673}16741675/*ARGSUSED2*/1676static int1677die_fwd_resolve(tdesc_t *fwd, tdesc_t **fwdp, void *private __unused)1678{1679if (fwd->t_flags & TDESC_F_RESOLVED)1680return (1);16811682if (fwd->t_tdesc != NULL) {1683debug(3, "tdp %u: unforwarded %s\n", fwd->t_id,1684tdesc_name(fwd));1685*fwdp = fwd->t_tdesc;1686}16871688fwd->t_flags |= TDESC_F_RESOLVED;16891690return (1);1691}16921693/*ARGSUSED*/1694static void1695die_lexblk_descend(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off __unused, tdesc_t *tdp __unused)1696{1697Dwarf_Die child = die_child(dw, die);16981699if (child != NULL)1700die_create(dw, child);1701}17021703/*1704* Used to map the die to a routine which can parse it, using the tag to do the1705* mapping. While the processing of most tags entails the creation of a tdesc,1706* there are a few which don't - primarily those which result in the creation of1707* iidescs which refer to existing tdescs.1708*/17091710#define DW_F_NOTDP 0x1 /* Don't create a tdesc for the creator */17111712typedef struct die_creator {1713Dwarf_Half dc_tag;1714uint16_t dc_flags;1715void (*dc_create)(dwarf_t *, Dwarf_Die, Dwarf_Off, tdesc_t *);1716} die_creator_t;17171718static const die_creator_t die_creators[] = {1719{ DW_TAG_array_type, 0, die_array_create },1720{ DW_TAG_enumeration_type, 0, die_enum_create },1721{ DW_TAG_lexical_block, DW_F_NOTDP, die_lexblk_descend },1722{ DW_TAG_pointer_type, 0, die_pointer_create },1723{ DW_TAG_structure_type, 0, die_struct_create },1724{ DW_TAG_subroutine_type, 0, die_funcptr_create },1725{ DW_TAG_typedef, 0, die_typedef_create },1726{ DW_TAG_union_type, 0, die_union_create },1727{ DW_TAG_base_type, 0, die_base_create },1728{ DW_TAG_const_type, 0, die_const_create },1729{ DW_TAG_subprogram, DW_F_NOTDP, die_function_create },1730{ DW_TAG_variable, DW_F_NOTDP, die_variable_create },1731{ DW_TAG_volatile_type, 0, die_volatile_create },1732{ DW_TAG_restrict_type, 0, die_restrict_create },1733{ 0, 0, NULL }1734};17351736static const die_creator_t *1737die_tag2ctor(Dwarf_Half tag)1738{1739const die_creator_t *dc;17401741for (dc = die_creators; dc->dc_create != NULL; dc++) {1742if (dc->dc_tag == tag)1743return (dc);1744}17451746return (NULL);1747}17481749static void1750die_create_one(dwarf_t *dw, Dwarf_Die die)1751{1752Dwarf_Off off = die_off(dw, die);1753const die_creator_t *dc;1754Dwarf_Half tag;1755tdesc_t *tdp;17561757debug(3, "die %llu <%llx>: create_one\n", off, off);17581759if (off > dw->dw_maxoff) {1760terminate("illegal die offset %llu (max %llu)\n", off,1761dw->dw_maxoff);1762}17631764tag = die_tag(dw, die);17651766if ((dc = die_tag2ctor(tag)) == NULL) {1767debug(2, "die %llu: ignoring tag type %x\n", off, tag);1768return;1769}17701771if ((tdp = tdesc_lookup(dw, off)) == NULL &&1772!(dc->dc_flags & DW_F_NOTDP)) {1773tdp = xcalloc(sizeof (tdesc_t));1774tdp->t_id = off;1775tdesc_add(dw, tdp);1776}17771778if (tdp != NULL)1779tdp->t_name = die_name(dw, die);17801781dc->dc_create(dw, die, off, tdp);1782}17831784static void1785die_create(dwarf_t *dw, Dwarf_Die die)1786{1787do {1788die_create_one(dw, die);1789} while ((die = die_sibling(dw, die)) != NULL);1790}17911792static tdtrav_cb_f die_resolvers[] = {1793NULL,1794NULL, /* intrinsic */1795NULL, /* pointer */1796die_array_resolve, /* array */1797NULL, /* function */1798die_sou_resolve, /* struct */1799die_sou_resolve, /* union */1800die_enum_resolve, /* enum */1801die_fwd_resolve, /* forward */1802NULL, /* typedef */1803NULL, /* typedef unres */1804NULL, /* volatile */1805NULL, /* const */1806NULL, /* restrict */1807};18081809static tdtrav_cb_f die_fail_reporters[] = {1810NULL,1811NULL, /* intrinsic */1812NULL, /* pointer */1813die_array_failed, /* array */1814NULL, /* function */1815die_sou_failed, /* struct */1816die_sou_failed, /* union */1817NULL, /* enum */1818NULL, /* forward */1819NULL, /* typedef */1820NULL, /* typedef unres */1821NULL, /* volatile */1822NULL, /* const */1823NULL, /* restrict */1824};18251826static void1827die_resolve(dwarf_t *dw)1828{1829int last = -1;1830int pass = 0;18311832do {1833pass++;1834dw->dw_nunres = 0;18351836(void) iitraverse_hash(dw->dw_td->td_iihash,1837&dw->dw_td->td_curvgen, NULL, NULL, die_resolvers, dw);18381839debug(3, "resolve: pass %d, %u left\n", pass, dw->dw_nunres);18401841if ((int) dw->dw_nunres == last) {1842fprintf(stderr, "%s: failed to resolve the following "1843"types:\n", progname);18441845(void) iitraverse_hash(dw->dw_td->td_iihash,1846&dw->dw_td->td_curvgen, NULL, NULL,1847die_fail_reporters, dw);18481849terminate("failed to resolve types\n");1850}18511852last = dw->dw_nunres;18531854} while (dw->dw_nunres != 0);1855}18561857/*1858* Any object containing a function or object symbol at any scope should also1859* contain DWARF data.1860*/1861static boolean_t1862should_have_dwarf(Elf *elf)1863{1864Elf_Scn *scn = NULL;1865Elf_Data *data = NULL;1866GElf_Shdr shdr;1867GElf_Sym sym;1868uint32_t symdx = 0;1869size_t nsyms = 0;1870boolean_t found = B_FALSE;18711872while ((scn = elf_nextscn(elf, scn)) != NULL) {1873gelf_getshdr(scn, &shdr);18741875if (shdr.sh_type == SHT_SYMTAB) {1876found = B_TRUE;1877break;1878}1879}18801881if (!found)1882terminate("cannot convert stripped objects\n");18831884data = elf_getdata(scn, NULL);1885nsyms = shdr.sh_size / shdr.sh_entsize;18861887for (symdx = 0; symdx < nsyms; symdx++) {1888gelf_getsym(data, symdx, &sym);18891890if ((GELF_ST_TYPE(sym.st_info) == STT_FUNC) ||1891(GELF_ST_TYPE(sym.st_info) == STT_TLS) ||1892(GELF_ST_TYPE(sym.st_info) == STT_OBJECT)) {1893char *name;18941895name = elf_strptr(elf, shdr.sh_link, sym.st_name);18961897/* Studio emits these local symbols regardless */1898if ((strcmp(name, "Bbss.bss") != 0) &&1899(strcmp(name, "Ttbss.bss") != 0) &&1900(strcmp(name, "Ddata.data") != 0) &&1901(strcmp(name, "Ttdata.data") != 0) &&1902(strcmp(name, "Drodata.rodata") != 0))1903return (B_TRUE);1904}1905}19061907return (B_FALSE);1908}19091910/*ARGSUSED*/1911int1912dw_read(tdata_t *td, Elf *elf, char *filename __unused)1913{1914Dwarf_Unsigned abboff, hdrlen, lang, nxthdr;1915Dwarf_Half vers, addrsz, offsz;1916Dwarf_Die cu = 0;1917Dwarf_Die child = 0;1918dwarf_t dw;1919char *prod = NULL;1920int rc;19211922bzero(&dw, sizeof (dwarf_t));1923dw.dw_td = td;1924dw.dw_ptrsz = elf_ptrsz(elf);1925dw.dw_mfgtid_last = TID_MFGTID_BASE;1926dw.dw_tidhash = hash_new(TDESC_HASH_BUCKETS, tdesc_idhash, tdesc_idcmp);1927dw.dw_fwdhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,1928tdesc_namecmp);1929dw.dw_enumhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,1930tdesc_namecmp);19311932if ((rc = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dw.dw_dw,1933&dw.dw_err)) == DW_DLV_NO_ENTRY) {1934if (should_have_dwarf(elf)) {1935errno = ENOENT;1936return (-1);1937} else {1938return (0);1939}1940} else if (rc != DW_DLV_OK) {1941if (dwarf_errno(dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) {1942/*1943* There's no type data in the DWARF section, but1944* libdwarf is too clever to handle that properly.1945*/1946return (0);1947}19481949terminate("failed to initialize DWARF: %s\n",1950dwarf_errmsg(dw.dw_err));1951}19521953if ((rc = dwarf_next_cu_header_b(dw.dw_dw, &hdrlen, &vers, &abboff,1954&addrsz, &offsz, NULL, &nxthdr, &dw.dw_err)) != DW_DLV_OK) {1955if (dw.dw_err.err_error == DW_DLE_NO_ENTRY)1956exit(0);1957else1958terminate("rc = %d %s\n", rc, dwarf_errmsg(dw.dw_err));1959}1960if ((cu = die_sibling(&dw, NULL)) == NULL ||1961(((child = die_child(&dw, cu)) == NULL) &&1962should_have_dwarf(elf))) {1963terminate("file does not contain dwarf type data "1964"(try compiling with -g)\n");1965} else if (child == NULL) {1966return (0);1967}19681969dw.dw_maxoff = nxthdr - 1;19701971if (dw.dw_maxoff > TID_FILEMAX)1972terminate("file contains too many types\n");19731974debug(1, "DWARF version: %d\n", vers);1975if (vers < 2 || vers > 4) {1976terminate("file contains incompatible version %d DWARF code "1977"(version 2, 3 or 4 required)\n", vers);1978}19791980if (die_string(&dw, cu, DW_AT_producer, &prod, 0)) {1981debug(1, "DWARF emitter: %s\n", prod);1982free(prod);1983}19841985if (dwarf_attrval_unsigned(cu, DW_AT_language, &lang, &dw.dw_err) == 0)1986switch (lang) {1987case DW_LANG_C:1988case DW_LANG_C89:1989case DW_LANG_C99:1990case DW_LANG_C11:1991case DW_LANG_C_plus_plus:1992case DW_LANG_C_plus_plus_03:1993case DW_LANG_C_plus_plus_11:1994case DW_LANG_C_plus_plus_14:1995case DW_LANG_Mips_Assembler:1996break;1997default:1998terminate("file contains DWARF for unsupported "1999"language %#x", lang);2000}2001else2002warning("die %llu: failed to get language attribute: %s\n",2003die_off(&dw, cu), dwarf_errmsg(dw.dw_err));20042005if ((dw.dw_cuname = die_name(&dw, cu)) != NULL) {2006char *base = xstrdup(basename(dw.dw_cuname));2007free(dw.dw_cuname);2008dw.dw_cuname = base;20092010debug(1, "CU name: %s\n", dw.dw_cuname);2011}20122013if ((child = die_child(&dw, cu)) != NULL)2014die_create(&dw, child);20152016if ((rc = dwarf_next_cu_header_b(dw.dw_dw, &hdrlen, &vers, &abboff,2017&addrsz, &offsz, NULL, &nxthdr, &dw.dw_err)) != DW_DLV_NO_ENTRY)2018terminate("multiple compilation units not supported\n");20192020(void) dwarf_finish(dw.dw_dw, &dw.dw_err);20212022die_resolve(&dw);20232024cvt_fixups(td, dw.dw_ptrsz);20252026/* leak the dwarf_t */20272028return (0);2029}203020312032