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