xref: /titanic_51/usr/src/tools/ctf/cvt/dwarf.c (revision f95bda13672da09ae973b76683e47b094188903a)
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 #ifdef __sparc
1289 	{ { 16, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
1290 #else
1291 	{ { 12, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
1292 #endif
1293 	{ { 0, 0 } }
1294 };
1295 
1296 static uint_t
1297 die_base_type2enc(dwarf_t *dw, Dwarf_Off off, Dwarf_Signed enc, size_t sz)
1298 {
1299 	const fp_size_map_t *map = fp_encodings;
1300 	uint_t szidx = dw->dw_ptrsz == sizeof (uint64_t);
1301 	uint_t mult = 1, col = 0;
1302 
1303 	if (enc == DW_ATE_complex_float) {
1304 		mult = 2;
1305 		col = 1;
1306 	} else if (enc == DW_ATE_imaginary_float ||
1307 	    enc == DW_ATE_SUN_imaginary_float)
1308 		col = 2;
1309 
1310 	while (map->fsm_typesz[szidx] != 0) {
1311 		if (map->fsm_typesz[szidx] * mult == sz)
1312 			return (map->fsm_enc[col]);
1313 		map++;
1314 	}
1315 
1316 	terminate("die %llu: unrecognized real type size %u\n", off, sz);
1317 	/*NOTREACHED*/
1318 	return (0);
1319 }
1320 
1321 static intr_t *
1322 die_base_from_dwarf(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, size_t sz)
1323 {
1324 	intr_t *intr = xcalloc(sizeof (intr_t));
1325 	Dwarf_Signed enc;
1326 
1327 	(void) die_signed(dw, base, DW_AT_encoding, &enc, DW_ATTR_REQ);
1328 
1329 	switch (enc) {
1330 	case DW_ATE_unsigned:
1331 	case DW_ATE_address:
1332 		intr->intr_type = INTR_INT;
1333 		break;
1334 	case DW_ATE_unsigned_char:
1335 		intr->intr_type = INTR_INT;
1336 		intr->intr_iformat = 'c';
1337 		break;
1338 	case DW_ATE_signed:
1339 		intr->intr_type = INTR_INT;
1340 		intr->intr_signed = 1;
1341 		break;
1342 	case DW_ATE_signed_char:
1343 		intr->intr_type = INTR_INT;
1344 		intr->intr_signed = 1;
1345 		intr->intr_iformat = 'c';
1346 		break;
1347 	case DW_ATE_boolean:
1348 		intr->intr_type = INTR_INT;
1349 		intr->intr_signed = 1;
1350 		intr->intr_iformat = 'b';
1351 		break;
1352 	case DW_ATE_float:
1353 	case DW_ATE_complex_float:
1354 	case DW_ATE_imaginary_float:
1355 	case DW_ATE_SUN_imaginary_float:
1356 	case DW_ATE_SUN_interval_float:
1357 		intr->intr_type = INTR_REAL;
1358 		intr->intr_signed = 1;
1359 		intr->intr_fformat = die_base_type2enc(dw, off, enc, sz);
1360 		break;
1361 	default:
1362 		terminate("die %llu: unknown base type encoding 0x%llx\n",
1363 		    off, enc);
1364 	}
1365 
1366 	return (intr);
1367 }
1368 
1369 static void
1370 die_base_create(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, tdesc_t *tdp)
1371 {
1372 	Dwarf_Unsigned sz;
1373 	intr_t *intr;
1374 	char *new;
1375 
1376 	debug(3, "die %llu: creating base type\n", off);
1377 
1378 	/*
1379 	 * The compilers have their own clever (internally inconsistent) ideas
1380 	 * as to what base types should look like.  Some times gcc will, for
1381 	 * example, use DW_ATE_signed_char for char.  Other times, however, it
1382 	 * will use DW_ATE_signed.  Needless to say, this causes some problems
1383 	 * down the road, particularly with merging.  We do, however, use the
1384 	 * DWARF idea of type sizes, as this allows us to avoid caring about
1385 	 * the data model.
1386 	 */
1387 	(void) die_unsigned(dw, base, DW_AT_byte_size, &sz, DW_ATTR_REQ);
1388 
1389 	if (tdp->t_name == NULL)
1390 		terminate("die %llu: base type without name\n", off);
1391 
1392 	/* XXX make a name parser for float too */
1393 	if ((intr = die_base_name_parse(tdp->t_name, &new)) != NULL) {
1394 		/* Found it.  We'll use the parsed version */
1395 		debug(3, "die %llu: name \"%s\" remapped to \"%s\"\n", off,
1396 		    tdp->t_name, new);
1397 
1398 		free(tdp->t_name);
1399 		tdp->t_name = new;
1400 	} else {
1401 		/*
1402 		 * We didn't recognize the type, so we'll create an intr_t
1403 		 * based on the DWARF data.
1404 		 */
1405 		debug(3, "die %llu: using dwarf data for base \"%s\"\n", off,
1406 		    tdp->t_name);
1407 
1408 		intr = die_base_from_dwarf(dw, base, off, sz);
1409 	}
1410 
1411 	intr->intr_nbits = sz * 8;
1412 
1413 	tdp->t_type = INTRINSIC;
1414 	tdp->t_intr = intr;
1415 	tdp->t_size = sz;
1416 
1417 	tdp->t_flags |= TDESC_F_RESOLVED;
1418 }
1419 
1420 static void
1421 die_through_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp,
1422     int type, const char *typename)
1423 {
1424 	Dwarf_Attribute attr;
1425 
1426 	debug(3, "die %llu: creating %s\n", off, typename);
1427 
1428 	tdp->t_type = type;
1429 
1430 	if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
1431 		dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
1432 		tdp->t_tdesc = die_lookup_pass1(dw, die, DW_AT_type);
1433 	} else {
1434 		tdp->t_tdesc = tdesc_intr_void(dw);
1435 	}
1436 
1437 	if (type == POINTER)
1438 		tdp->t_size = dw->dw_ptrsz;
1439 
1440 	tdp->t_flags |= TDESC_F_RESOLVED;
1441 
1442 	if (type == TYPEDEF) {
1443 		iidesc_t *ii = xcalloc(sizeof (iidesc_t));
1444 		ii->ii_type = II_TYPE;
1445 		ii->ii_name = xstrdup(tdp->t_name);
1446 		ii->ii_dtype = tdp;
1447 
1448 		iidesc_add(dw->dw_td->td_iihash, ii);
1449 	}
1450 }
1451 
1452 static void
1453 die_typedef_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1454 {
1455 	die_through_create(dw, die, off, tdp, TYPEDEF, "typedef");
1456 }
1457 
1458 static void
1459 die_const_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1460 {
1461 	die_through_create(dw, die, off, tdp, CONST, "const");
1462 }
1463 
1464 static void
1465 die_pointer_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1466 {
1467 	die_through_create(dw, die, off, tdp, POINTER, "pointer");
1468 }
1469 
1470 static void
1471 die_restrict_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1472 {
1473 	die_through_create(dw, die, off, tdp, RESTRICT, "restrict");
1474 }
1475 
1476 static void
1477 die_volatile_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1478 {
1479 	die_through_create(dw, die, off, tdp, VOLATILE, "volatile");
1480 }
1481 
1482 /*ARGSUSED3*/
1483 static void
1484 die_function_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1485 {
1486 	Dwarf_Die arg;
1487 	Dwarf_Half tag;
1488 	iidesc_t *ii;
1489 	char *name;
1490 
1491 	debug(3, "die %llu: creating function definition\n", off);
1492 
1493 	/*
1494 	 * We'll begin by processing any type definition nodes that may be
1495 	 * lurking underneath this one.
1496 	 */
1497 	for (arg = die_child(dw, die); arg != NULL;
1498 	    arg = die_sibling(dw, arg)) {
1499 		if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
1500 		    tag != DW_TAG_variable) {
1501 			/* Nested type declaration */
1502 			die_create_one(dw, arg);
1503 		}
1504 	}
1505 
1506 	if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) {
1507 		/*
1508 		 * We process neither prototypes nor subprograms without
1509 		 * names.
1510 		 */
1511 		return;
1512 	}
1513 
1514 	ii = xcalloc(sizeof (iidesc_t));
1515 	ii->ii_type = die_isglobal(dw, die) ? II_GFUN : II_SFUN;
1516 	ii->ii_name = name;
1517 	if (ii->ii_type == II_SFUN)
1518 		ii->ii_owner = xstrdup(dw->dw_cuname);
1519 
1520 	debug(3, "die %llu: function %s is %s\n", off, ii->ii_name,
1521 	    (ii->ii_type == II_GFUN ? "global" : "static"));
1522 
1523 	if (die_attr(dw, die, DW_AT_type, 0) != NULL)
1524 		ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
1525 	else
1526 		ii->ii_dtype = tdesc_intr_void(dw);
1527 
1528 	for (arg = die_child(dw, die); arg != NULL;
1529 	    arg = die_sibling(dw, arg)) {
1530 		char *name;
1531 
1532 		debug(3, "die %llu: looking at sub member at %llu\n",
1533 		    off, die_off(dw, die));
1534 
1535 		if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1536 			continue;
1537 
1538 		if ((name = die_name(dw, arg)) == NULL) {
1539 			terminate("die %llu: func arg %d has no name\n",
1540 			    off, ii->ii_nargs + 1);
1541 		}
1542 
1543 		if (strcmp(name, "...") == 0) {
1544 			free(name);
1545 			ii->ii_vargs = 1;
1546 			continue;
1547 		}
1548 
1549 		ii->ii_nargs++;
1550 	}
1551 
1552 	if (ii->ii_nargs > 0) {
1553 		int i;
1554 
1555 		debug(3, "die %llu: function has %d argument%s\n", off,
1556 		    ii->ii_nargs, (ii->ii_nargs == 1 ? "" : "s"));
1557 
1558 		ii->ii_args = xcalloc(sizeof (tdesc_t) * ii->ii_nargs);
1559 
1560 		for (arg = die_child(dw, die), i = 0;
1561 		    arg != NULL && i < ii->ii_nargs;
1562 		    arg = die_sibling(dw, arg)) {
1563 			if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1564 				continue;
1565 
1566 			ii->ii_args[i++] = die_lookup_pass1(dw, arg,
1567 			    DW_AT_type);
1568 		}
1569 	}
1570 
1571 	iidesc_add(dw->dw_td->td_iihash, ii);
1572 }
1573 
1574 /*ARGSUSED3*/
1575 static void
1576 die_variable_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1577 {
1578 	iidesc_t *ii;
1579 	char *name;
1580 
1581 	debug(3, "die %llu: creating object definition\n", off);
1582 
1583 	if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL)
1584 		return; /* skip prototypes and nameless objects */
1585 
1586 	ii = xcalloc(sizeof (iidesc_t));
1587 	ii->ii_type = die_isglobal(dw, die) ? II_GVAR : II_SVAR;
1588 	ii->ii_name = name;
1589 	ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
1590 	if (ii->ii_type == II_SVAR)
1591 		ii->ii_owner = xstrdup(dw->dw_cuname);
1592 
1593 	iidesc_add(dw->dw_td->td_iihash, ii);
1594 }
1595 
1596 /*ARGSUSED2*/
1597 static int
1598 die_fwd_resolve(tdesc_t *fwd, tdesc_t **fwdp, void *private)
1599 {
1600 	if (fwd->t_flags & TDESC_F_RESOLVED)
1601 		return (1);
1602 
1603 	if (fwd->t_tdesc != NULL) {
1604 		debug(3, "tdp %u: unforwarded %s\n", fwd->t_id, fwd->t_name);
1605 		*fwdp = fwd->t_tdesc;
1606 	}
1607 
1608 	fwd->t_flags |= TDESC_F_RESOLVED;
1609 
1610 	return (1);
1611 }
1612 
1613 /*ARGSUSED*/
1614 static void
1615 die_lexblk_descend(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1616 {
1617 	Dwarf_Die child = die_child(dw, die);
1618 
1619 	if (child != NULL)
1620 		die_create(dw, child);
1621 }
1622 
1623 /*
1624  * Used to map the die to a routine which can parse it, using the tag to do the
1625  * mapping.  While the processing of most tags entails the creation of a tdesc,
1626  * there are a few which don't - primarily those which result in the creation of
1627  * iidescs which refer to existing tdescs.
1628  */
1629 
1630 #define	DW_F_NOTDP	0x1	/* Don't create a tdesc for the creator */
1631 
1632 typedef struct die_creator {
1633 	Dwarf_Half dc_tag;
1634 	uint16_t dc_flags;
1635 	void (*dc_create)(dwarf_t *, Dwarf_Die, Dwarf_Off, tdesc_t *);
1636 } die_creator_t;
1637 
1638 static const die_creator_t die_creators[] = {
1639 	{ DW_TAG_array_type,		0,		die_array_create },
1640 	{ DW_TAG_enumeration_type,	0,		die_enum_create },
1641 	{ DW_TAG_lexical_block,		DW_F_NOTDP,	die_lexblk_descend },
1642 	{ DW_TAG_pointer_type,		0,		die_pointer_create },
1643 	{ DW_TAG_structure_type,	0,		die_struct_create },
1644 	{ DW_TAG_subroutine_type,	0,		die_funcptr_create },
1645 	{ DW_TAG_typedef,		0,		die_typedef_create },
1646 	{ DW_TAG_union_type,		0,		die_union_create },
1647 	{ DW_TAG_base_type,		0,		die_base_create },
1648 	{ DW_TAG_const_type,		0,		die_const_create },
1649 	{ DW_TAG_subprogram,		DW_F_NOTDP,	die_function_create },
1650 	{ DW_TAG_variable,		DW_F_NOTDP,	die_variable_create },
1651 	{ DW_TAG_volatile_type,		0,		die_volatile_create },
1652 	{ DW_TAG_restrict_type,		0,		die_restrict_create },
1653 	{ 0, NULL }
1654 };
1655 
1656 static const die_creator_t *
1657 die_tag2ctor(Dwarf_Half tag)
1658 {
1659 	const die_creator_t *dc;
1660 
1661 	for (dc = die_creators; dc->dc_create != NULL; dc++) {
1662 		if (dc->dc_tag == tag)
1663 			return (dc);
1664 	}
1665 
1666 	return (NULL);
1667 }
1668 
1669 static void
1670 die_create_one(dwarf_t *dw, Dwarf_Die die)
1671 {
1672 	Dwarf_Off off = die_off(dw, die);
1673 	const die_creator_t *dc;
1674 	Dwarf_Half tag;
1675 	tdesc_t *tdp;
1676 
1677 	debug(3, "die %llu: create_one\n", off);
1678 
1679 	if (off > dw->dw_maxoff) {
1680 		terminate("illegal die offset %llu (max %llu)\n", off,
1681 		    dw->dw_maxoff);
1682 	}
1683 
1684 	tag = die_tag(dw, die);
1685 
1686 	if ((dc = die_tag2ctor(tag)) == NULL) {
1687 		debug(2, "die %llu: ignoring tag type %x\n", off, tag);
1688 		return;
1689 	}
1690 
1691 	if ((tdp = tdesc_lookup(dw, off)) == NULL &&
1692 	    !(dc->dc_flags & DW_F_NOTDP)) {
1693 		tdp = xcalloc(sizeof (tdesc_t));
1694 		tdp->t_id = off;
1695 		tdesc_add(dw, tdp);
1696 	}
1697 
1698 	if (tdp != NULL)
1699 		tdp->t_name = die_name(dw, die);
1700 
1701 	dc->dc_create(dw, die, off, tdp);
1702 }
1703 
1704 static void
1705 die_create(dwarf_t *dw, Dwarf_Die die)
1706 {
1707 	do {
1708 		die_create_one(dw, die);
1709 	} while ((die = die_sibling(dw, die)) != NULL);
1710 }
1711 
1712 static tdtrav_cb_f die_resolvers[] = {
1713 	NULL,
1714 	NULL,			/* intrinsic */
1715 	NULL,			/* pointer */
1716 	die_array_resolve,	/* array */
1717 	NULL,			/* function */
1718 	die_sou_resolve,	/* struct */
1719 	die_sou_resolve,	/* union */
1720 	die_enum_resolve,	/* enum */
1721 	die_fwd_resolve,	/* forward */
1722 	NULL,			/* typedef */
1723 	NULL,			/* typedef unres */
1724 	NULL,			/* volatile */
1725 	NULL,			/* const */
1726 	NULL,			/* restrict */
1727 };
1728 
1729 static tdtrav_cb_f die_fail_reporters[] = {
1730 	NULL,
1731 	NULL,			/* intrinsic */
1732 	NULL,			/* pointer */
1733 	die_array_failed,	/* array */
1734 	NULL,			/* function */
1735 	die_sou_failed,		/* struct */
1736 	die_sou_failed,		/* union */
1737 	NULL,			/* enum */
1738 	NULL,			/* forward */
1739 	NULL,			/* typedef */
1740 	NULL,			/* typedef unres */
1741 	NULL,			/* volatile */
1742 	NULL,			/* const */
1743 	NULL,			/* restrict */
1744 };
1745 
1746 static void
1747 die_resolve(dwarf_t *dw)
1748 {
1749 	int last = -1;
1750 	int pass = 0;
1751 
1752 	do {
1753 		pass++;
1754 		dw->dw_nunres = 0;
1755 
1756 		(void) iitraverse_hash(dw->dw_td->td_iihash,
1757 		    &dw->dw_td->td_curvgen, NULL, NULL, die_resolvers, dw);
1758 
1759 		debug(3, "resolve: pass %d, %u left\n", pass, dw->dw_nunres);
1760 
1761 		if (dw->dw_nunres == last) {
1762 			fprintf(stderr, "%s: failed to resolve the following "
1763 			    "types:\n", progname);
1764 
1765 			(void) iitraverse_hash(dw->dw_td->td_iihash,
1766 			    &dw->dw_td->td_curvgen, NULL, NULL,
1767 			    die_fail_reporters, dw);
1768 
1769 			terminate("failed to resolve types\n");
1770 		}
1771 
1772 		last = dw->dw_nunres;
1773 
1774 	} while (dw->dw_nunres != 0);
1775 }
1776 
1777 static size_t
1778 elf_ptrsz(Elf *elf)
1779 {
1780 	GElf_Ehdr ehdr;
1781 
1782 	if (gelf_getehdr(elf, &ehdr) == NULL) {
1783 		terminate("failed to read ELF header: %s\n",
1784 		    elf_errmsg(elf_errno()));
1785 	}
1786 
1787 	if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
1788 		return (4);
1789 	else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
1790 		return (8);
1791 	else
1792 		terminate("unknown ELF class %d\n", ehdr.e_ident[EI_CLASS]);
1793 
1794 	/*NOTREACHED*/
1795 	return (0);
1796 }
1797 
1798 /*ARGSUSED*/
1799 int
1800 dw_read(tdata_t *td, Elf *elf, const char *filename)
1801 {
1802 	Dwarf_Unsigned abboff, hdrlen, nxthdr;
1803 	Dwarf_Half vers, addrsz;
1804 	Dwarf_Die cu, child;
1805 	dwarf_t dw;
1806 	char *prod = NULL;
1807 	int rc;
1808 
1809 	bzero(&dw, sizeof (dwarf_t));
1810 	dw.dw_td = td;
1811 	dw.dw_ptrsz = elf_ptrsz(elf);
1812 	dw.dw_mfgtid_last = TID_MFGTID_BASE;
1813 	dw.dw_tidhash = hash_new(TDESC_HASH_BUCKETS, tdesc_idhash, tdesc_idcmp);
1814 	dw.dw_fwdhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
1815 	    tdesc_namecmp);
1816 	dw.dw_enumhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
1817 	    tdesc_namecmp);
1818 
1819 	if ((rc = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dw.dw_dw,
1820 	    &dw.dw_err)) == DW_DLV_NO_ENTRY) {
1821 		errno = ENOENT;
1822 		return (-1);
1823 	} else if (rc != DW_DLV_OK) {
1824 		if (dwarf_errno(dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) {
1825 			/*
1826 			 * There's no type data in the DWARF section, but
1827 			 * libdwarf is too clever to handle that properly.
1828 			 */
1829 			return (0);
1830 		}
1831 
1832 		terminate("failed to initialize DWARF: %s\n",
1833 		    dwarf_errmsg(dw.dw_err));
1834 	}
1835 
1836 	if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff,
1837 	    &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_OK ||
1838 	    (cu = die_sibling(&dw, NULL)) == NULL ||
1839 	    (child = die_child(&dw, cu)) == NULL)
1840 		terminate("file does not contain dwarf type data "
1841 		    "(try compiling with -g)\n");
1842 
1843 	dw.dw_maxoff = nxthdr - 1;
1844 
1845 	if (dw.dw_maxoff > TID_FILEMAX)
1846 		terminate("file contains too many types\n");
1847 
1848 	debug(1, "DWARF version: %d\n", vers);
1849 	if (vers != DWARF_VERSION) {
1850 		terminate("file contains incompatible version %d DWARF code "
1851 		    "(version 2 required)\n", vers);
1852 	}
1853 
1854 	if (die_string(&dw, cu, DW_AT_producer, &prod, 0)) {
1855 		debug(1, "DWARF emitter: %s\n", prod);
1856 		free(prod);
1857 	}
1858 
1859 	if ((dw.dw_cuname = die_name(&dw, cu)) != NULL) {
1860 		char *base = xstrdup(basename(dw.dw_cuname));
1861 		free(dw.dw_cuname);
1862 		dw.dw_cuname = base;
1863 
1864 		debug(1, "CU name: %s\n", dw.dw_cuname);
1865 	}
1866 
1867 	die_create(&dw, child);
1868 
1869 	if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff,
1870 	    &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_NO_ENTRY)
1871 		terminate("multiple compilation units not supported\n");
1872 
1873 	(void) dwarf_finish(dw.dw_dw, &dw.dw_err);
1874 
1875 	die_resolve(&dw);
1876 
1877 	/* leak the dwarf_t */
1878 
1879 	return (0);
1880 }
1881