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