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