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