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