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