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