xref: /titanic_51/usr/src/lib/libctf/common/ctf_dwarf.c (revision 110570ceb08d1961475cc4922fdf73e6ffc65924)
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  * Copyright 2012 Jason King.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 /*
31  * Copyright 2018 Joyent, Inc.
32  */
33 
34 /*
35  * CTF DWARF conversion theory.
36  *
37  * DWARF data contains a series of compilation units. Each compilation unit
38  * generally refers to an object file or what once was, in the case of linked
39  * binaries and shared objects. Each compilation unit has a series of what DWARF
40  * calls a DIE (Debugging Information Entry). The set of entries that we care
41  * about have type information stored in a series of attributes. Each DIE also
42  * has a tag that identifies the kind of attributes that it has.
43  *
44  * A given DIE may itself have children. For example, a DIE that represents a
45  * structure has children which represent members. Whenever we encounter a DIE
46  * that has children or other values or types associated with it, we recursively
47  * process those children first so that way we can then refer to the generated
48  * CTF type id while processing its parent. This reduces the amount of unknowns
49  * and fixups that we need. It also ensures that we don't accidentally add types
50  * that an overzealous compiler might add to the DWARF data but aren't used by
51  * anything in the system.
52  *
53  * Once we do a conversion, we store a mapping in an AVL tree that goes from the
54  * DWARF's die offset, which is relative to the given compilation unit, to a
55  * ctf_id_t.
56  *
57  * Unfortunately, some compilers actually will emit duplicate entries for a
58  * given type that look similar, but aren't quite. To that end, we go through
59  * and do a variant on a merge once we're done processing a single compilation
60  * unit which deduplicates all of the types that are in the unit.
61  *
62  * Finally, if we encounter an object that has multiple compilation units, then
63  * we'll convert all of the compilation units separately and then do a merge, so
64  * that way we can result in one single ctf_file_t that represents everything
65  * for the object.
66  *
67  * Conversion Steps
68  * ----------------
69  *
70  * Because a given object we've been given to convert may have multiple
71  * compilation units, we break the work into two halves. The first half
72  * processes each compilation unit (potentially in parallel) and then the second
73  * half optionally merges all of the dies in the first half. First, we'll cover
74  * what's involved in converting a single ctf_cu_t's dwarf to CTF. This covers
75  * the work done in ctf_dwarf_convert_one().
76  *
77  * An individual ctf_cu_t, which represents a compilation unit, is converted to
78  * CTF in a series of multiple passes.
79  *
80  * Pass 1: During the first pass we walk all of the top-level dies and if we
81  * find a function, variable, struct, union, enum or typedef, we recursively
82  * transform all of its types. We don't recurse or process everything, because
83  * we don't want to add some of the types that compilers may add which are
84  * effectively unused.
85  *
86  * During pass 1, if we encounter any structures or unions we mark them for
87  * fixing up later. This is necessary because we may not be able to determine
88  * the full size of a structure at the beginning of time. This will happen if
89  * the DWARF attribute DW_AT_byte_size is not present for a member. Because of
90  * this possibility we defer adding members to structures or even converting
91  * them during pass 1 and save that for pass 2. Adding all of the base
92  * structures without any of their members helps deal with any circular
93  * dependencies that we might encounter.
94  *
95  * Pass 2: This pass is used to do the first half of fixing up structures and
96  * unions. Rather than walk the entire type space again, we actually walk the
97  * list of structures and unions that we marked for later fixing up. Here, we
98  * iterate over every structure and add members to the underlying ctf_file_t,
99  * but not to the structs themselves. One might wonder why we don't, and the
100  * main reason is that libctf requires a ctf_update() be done before adding the
101  * members to structures or unions.
102  *
103  * Pass 3: This pass is used to do the second half of fixing up structures and
104  * unions. During this part we always go through and add members to structures
105  * and unions that we added to the container in the previous pass. In addition,
106  * we set the structure and union's actual size, which may have additional
107  * padding added by the compiler, it isn't simply the last offset. DWARF always
108  * guarantees an attribute exists for this. Importantly no ctf_id_t's change
109  * during pass 2.
110  *
111  * Pass 4: The next phase is to add CTF entries for all of the symbols and
112  * variables that are present in this die. During pass 1 we added entries to a
113  * map for each variable and function. During this pass, we iterate over the
114  * symbol table and when we encounter a symbol that we have in our lists of
115  * translated information which matches, we then add it to the ctf_file_t.
116  *
117  * Pass 5: Here we go and look for any weak symbols and functions and see if
118  * they match anything that we recognize. If so, then we add type information
119  * for them at this point based on the matching type.
120  *
121  * Pass 6: This pass is actually a variant on a merge. The traditional merge
122  * process expects there to be no duplicate types. As such, at the end of
123  * conversion, we do a dedup on all of the types in the system. The
124  * deduplication process is described in lib/libctf/common/ctf_merge.c.
125  *
126  * Once pass 6 is done, we've finished processing the individual compilation
127  * unit.
128  *
129  * The following steps reflect the general process of doing a conversion.
130  *
131  * 1) Walk the dwarf section and determine the number of compilation units
132  * 2) Create a ctf_cu_t for each compilation unit
133  * 3) Add all ctf_cu_t's to a workq
134  * 4) Have the workq process each die with ctf_dwarf_convert_one. This itself
135  *    is comprised of several steps, which were already enumerated.
136  * 5) If we have multiple cu's, we do a ctf merge of all the dies. The mechanics
137  *    of the merge are discussed in lib/libctf/common/ctf_merge.c.
138  * 6) Free everything up and return a ctf_file_t to the user. If we only had a
139  *    single compilation unit, then we give that to the user. Otherwise, we
140  *    return the merged ctf_file_t.
141  *
142  * Threading
143  * ---------
144  *
145  * The process has been designed to be amenable to threading. Each compilation
146  * unit has its own type stream, therefore the logical place to divide and
147  * conquer is at the compilation unit. Each ctf_cu_t has been built to be able
148  * to be processed independently of the others. It has its own libdwarf handle,
149  * as a given libdwarf handle may only be used by a single thread at a time.
150  * This allows the various ctf_cu_t's to be processed in parallel by different
151  * threads.
152  *
153  * All of the ctf_cu_t's are loaded into a workq which allows for a number of
154  * threads to be specified and used as a thread pool to process all of the
155  * queued work. We set the number of threads to use in the workq equal to the
156  * number of threads that the user has specified.
157  *
158  * After all of the compilation units have been drained, we use the same number
159  * of threads when performing a merge of multiple compilation units, if they
160  * exist.
161  *
162  * While all of these different parts do support and allow for multiple threads,
163  * it's important that when only a single thread is specified, that it be the
164  * calling thread. This allows the conversion routines to be used in a context
165  * that doesn't allow additional threads, such as rtld.
166  *
167  * Common DWARF Mechanics and Notes
168  * --------------------------------
169  *
170  * At this time, we really only support DWARFv2, though support for DWARFv4 is
171  * mostly there. There is no intent to support DWARFv3.
172  *
173  * Generally types for something are stored in the DW_AT_type attribute. For
174  * example, a function's return type will be stored in the local DW_AT_type
175  * attribute while the arguments will be in child DIEs. There are also various
176  * times when we don't have any DW_AT_type. In that case, the lack of a type
177  * implies, at least for C, that its C type is void. Because DWARF doesn't emit
178  * one, we have a synthetic void type that we create and manipulate instead and
179  * pass it off to consumers on an as-needed basis. If nothing has a void type,
180  * it will not be emitted.
181  *
182  * Architecture Specific Parts
183  * ---------------------------
184  *
185  * The CTF tooling encodes various information about the various architectures
186  * in the system. Importantly, the tool assumes that every architecture has a
187  * data model where long and pointer are the same size. This is currently the
188  * case, as the two data models illumos supports are ILP32 and LP64.
189  *
190  * In addition, we encode the mapping of various floating point sizes to various
191  * types for each architecture. If a new architecture is being added, it should
192  * be added to the list. The general design of the ctf conversion tools is to be
193  * architecture independent. eg. any of the tools here should be able to convert
194  * any architecture's DWARF into ctf; however, this has not been rigorously
195  * tested and more importantly, the ctf routines don't currently write out the
196  * data in an endian-aware form, they only use that of the currently running
197  * library.
198  */
199 
200 #include <libctf_impl.h>
201 #include <sys/avl.h>
202 #include <sys/debug.h>
203 #include <gelf.h>
204 #include <libdwarf.h>
205 #include <dwarf.h>
206 #include <libgen.h>
207 #include <workq.h>
208 #include <errno.h>
209 
210 #define	DWARF_VERSION_TWO	2
211 #define	DWARF_VARARGS_NAME	"..."
212 
213 /*
214  * Dwarf may refer recursively to other types that we've already processed. To
215  * see if we've already converted them, we look them up in an AVL tree that's
216  * sorted by the DWARF id.
217  */
218 typedef struct ctf_dwmap {
219 	avl_node_t	cdm_avl;
220 	Dwarf_Off	cdm_off;
221 	Dwarf_Die	cdm_die;
222 	ctf_id_t	cdm_id;
223 	boolean_t	cdm_fix;
224 } ctf_dwmap_t;
225 
226 typedef struct ctf_dwvar {
227 	ctf_list_t	cdv_list;
228 	char		*cdv_name;
229 	ctf_id_t	cdv_type;
230 	boolean_t	cdv_global;
231 } ctf_dwvar_t;
232 
233 typedef struct ctf_dwfunc {
234 	ctf_list_t	cdf_list;
235 	char		*cdf_name;
236 	ctf_funcinfo_t	cdf_fip;
237 	ctf_id_t	*cdf_argv;
238 	boolean_t	cdf_global;
239 } ctf_dwfunc_t;
240 
241 typedef struct ctf_dwbitf {
242 	ctf_list_t	cdb_list;
243 	ctf_id_t	cdb_base;
244 	uint_t		cdb_nbits;
245 	ctf_id_t	cdb_id;
246 } ctf_dwbitf_t;
247 
248 /*
249  * The ctf_cu_t represents a single top-level DWARF die unit. While generally,
250  * the typical object file has only a single die, if we're asked to convert
251  * something that's been linked from multiple sources, multiple dies will exist.
252  */
253 typedef struct ctf_die {
254 	Elf		*cu_elf;	/* shared libelf handle */
255 	char		*cu_name;	/* basename of the DIE */
256 	ctf_merge_t	*cu_cmh;	/* merge handle */
257 	ctf_list_t	cu_vars;	/* List of variables */
258 	ctf_list_t	cu_funcs;	/* List of functions */
259 	ctf_list_t	cu_bitfields;	/* Bit field members */
260 	Dwarf_Debug	cu_dwarf;	/* libdwarf handle */
261 	Dwarf_Die	cu_cu;		/* libdwarf compilation unit */
262 	Dwarf_Off	cu_cuoff;	/* cu's offset */
263 	Dwarf_Off	cu_maxoff;	/* maximum offset */
264 	ctf_file_t	*cu_ctfp;	/* output CTF file */
265 	avl_tree_t	cu_map;		/* map die offsets to CTF types */
266 	char		*cu_errbuf;	/* error message buffer */
267 	size_t		cu_errlen;	/* error message buffer length */
268 	size_t		cu_ptrsz;	/* object's pointer size */
269 	boolean_t	cu_bigend;	/* is it big endian */
270 	boolean_t	cu_doweaks;	/* should we convert weak symbols? */
271 	uint_t		cu_mach;	/* machine type */
272 	ctf_id_t	cu_voidtid;	/* void pointer */
273 	ctf_id_t	cu_longtid;	/* id for a 'long' */
274 } ctf_cu_t;
275 
276 static int ctf_dwarf_offset(ctf_cu_t *, Dwarf_Die, Dwarf_Off *);
277 static int ctf_dwarf_convert_die(ctf_cu_t *, Dwarf_Die);
278 static int ctf_dwarf_convert_type(ctf_cu_t *, Dwarf_Die, ctf_id_t *, int);
279 
280 static int ctf_dwarf_function_count(ctf_cu_t *, Dwarf_Die, ctf_funcinfo_t *,
281     boolean_t);
282 static int ctf_dwarf_convert_fargs(ctf_cu_t *, Dwarf_Die, ctf_funcinfo_t *,
283     ctf_id_t *);
284 
285 typedef int (ctf_dwarf_symtab_f)(ctf_cu_t *, const GElf_Sym *, ulong_t,
286     const char *, const char *, void *);
287 
288 /*
289  * This is a generic way to set a CTF Conversion backend error depending on what
290  * we were doing. Unless it was one of a specific set of errors that don't
291  * indicate a programming / translation bug, eg. ENOMEM, then we transform it
292  * into a CTF backend error and fill in the error buffer.
293  */
294 static int
295 ctf_dwarf_error(ctf_cu_t *cup, ctf_file_t *cfp, int err, const char *fmt, ...)
296 {
297 	va_list ap;
298 	int ret;
299 	size_t off = 0;
300 	ssize_t rem = cup->cu_errlen;
301 	if (cfp != NULL)
302 		err = ctf_errno(cfp);
303 
304 	if (err == ENOMEM)
305 		return (err);
306 
307 	ret = snprintf(cup->cu_errbuf, rem, "die %s: ", cup->cu_name);
308 	if (ret < 0)
309 		goto err;
310 	off += ret;
311 	rem = MAX(rem - ret, 0);
312 
313 	va_start(ap, fmt);
314 	ret = vsnprintf(cup->cu_errbuf + off, rem, fmt, ap);
315 	va_end(ap);
316 	if (ret < 0)
317 		goto err;
318 
319 	off += ret;
320 	rem = MAX(rem - ret, 0);
321 	if (fmt[strlen(fmt) - 1] != '\n') {
322 		(void) snprintf(cup->cu_errbuf + off, rem,
323 		    ": %s\n", ctf_errmsg(err));
324 	}
325 	va_end(ap);
326 	return (ECTF_CONVBKERR);
327 
328 err:
329 	cup->cu_errbuf[0] = '\0';
330 	return (ECTF_CONVBKERR);
331 }
332 
333 /*
334  * DWARF often opts to put no explicit type to describe a void type. eg. if we
335  * have a reference type whose DW_AT_type member doesn't exist, then we should
336  * instead assume it points to void. Because this isn't represented, we
337  * instead cause it to come into existence.
338  */
339 static ctf_id_t
340 ctf_dwarf_void(ctf_cu_t *cup)
341 {
342 	if (cup->cu_voidtid == CTF_ERR) {
343 		ctf_encoding_t enc = { CTF_INT_SIGNED, 0, 0 };
344 		cup->cu_voidtid = ctf_add_integer(cup->cu_ctfp, CTF_ADD_ROOT,
345 		    "void", &enc);
346 		if (cup->cu_voidtid == CTF_ERR) {
347 			(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
348 			    "failed to create void type: %s\n",
349 			    ctf_errmsg(ctf_errno(cup->cu_ctfp)));
350 		}
351 	}
352 
353 	return (cup->cu_voidtid);
354 }
355 
356 /*
357  * There are many different forms that an array index may take. However, we just
358  * always force it to be of a type long no matter what. Therefore we use this to
359  * have a single instance of long across everything.
360  */
361 static ctf_id_t
362 ctf_dwarf_long(ctf_cu_t *cup)
363 {
364 	if (cup->cu_longtid == CTF_ERR) {
365 		ctf_encoding_t enc;
366 
367 		enc.cte_format = CTF_INT_SIGNED;
368 		enc.cte_offset = 0;
369 		/* All illumos systems are LP */
370 		enc.cte_bits = cup->cu_ptrsz * 8;
371 		cup->cu_longtid = ctf_add_integer(cup->cu_ctfp, CTF_ADD_NONROOT,
372 		    "long", &enc);
373 		if (cup->cu_longtid == CTF_ERR) {
374 			(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
375 			    "failed to create long type: %s\n",
376 			    ctf_errmsg(ctf_errno(cup->cu_ctfp)));
377 		}
378 
379 	}
380 
381 	return (cup->cu_longtid);
382 }
383 
384 static int
385 ctf_dwmap_comp(const void *a, const void *b)
386 {
387 	const ctf_dwmap_t *ca = a;
388 	const ctf_dwmap_t *cb = b;
389 
390 	if (ca->cdm_off > cb->cdm_off)
391 		return (1);
392 	if (ca->cdm_off < cb->cdm_off)
393 		return (-1);
394 	return (0);
395 }
396 
397 static int
398 ctf_dwmap_add(ctf_cu_t *cup, ctf_id_t id, Dwarf_Die die, boolean_t fix)
399 {
400 	int ret;
401 	avl_index_t index;
402 	ctf_dwmap_t *dwmap;
403 	Dwarf_Off off;
404 
405 	VERIFY(id > 0 && id < CTF_MAX_TYPE);
406 
407 	if ((ret = ctf_dwarf_offset(cup, die, &off)) != 0)
408 		return (ret);
409 
410 	if ((dwmap = ctf_alloc(sizeof (ctf_dwmap_t))) == NULL)
411 		return (ENOMEM);
412 
413 	dwmap->cdm_die = die;
414 	dwmap->cdm_off = off;
415 	dwmap->cdm_id = id;
416 	dwmap->cdm_fix = fix;
417 
418 	ctf_dprintf("dwmap: %p %" DW_PR_DUx "->%d\n", dwmap, off, id);
419 	VERIFY(avl_find(&cup->cu_map, dwmap, &index) == NULL);
420 	avl_insert(&cup->cu_map, dwmap, index);
421 	return (0);
422 }
423 
424 static int
425 ctf_dwarf_attribute(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name,
426     Dwarf_Attribute *attrp)
427 {
428 	int ret;
429 	Dwarf_Error derr;
430 
431 	if ((ret = dwarf_attr(die, name, attrp, &derr)) == DW_DLV_OK)
432 		return (0);
433 	if (ret == DW_DLV_NO_ENTRY) {
434 		*attrp = NULL;
435 		return (ENOENT);
436 	}
437 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
438 	    "failed to get attribute for type: %s\n",
439 	    dwarf_errmsg(derr));
440 	return (ECTF_CONVBKERR);
441 }
442 
443 static int
444 ctf_dwarf_ref(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name, Dwarf_Off *refp)
445 {
446 	int ret;
447 	Dwarf_Attribute attr;
448 	Dwarf_Error derr;
449 
450 	if ((ret = ctf_dwarf_attribute(cup, die, name, &attr)) != 0)
451 		return (ret);
452 
453 	if (dwarf_formref(attr, refp, &derr) == DW_DLV_OK) {
454 		dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
455 		return (0);
456 	}
457 
458 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
459 	    "failed to get unsigned attribute for type: %s\n",
460 	    dwarf_errmsg(derr));
461 	return (ECTF_CONVBKERR);
462 }
463 
464 static int
465 ctf_dwarf_refdie(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name,
466     Dwarf_Die *diep)
467 {
468 	int ret;
469 	Dwarf_Off off;
470 	Dwarf_Error derr;
471 
472 	if ((ret = ctf_dwarf_ref(cup, die, name, &off)) != 0)
473 		return (ret);
474 
475 	off += cup->cu_cuoff;
476 	if ((ret = dwarf_offdie(cup->cu_dwarf, off, diep, &derr)) !=
477 	    DW_DLV_OK) {
478 		(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
479 		    "failed to get die from offset %" DW_PR_DUu ": %s\n",
480 		    off, dwarf_errmsg(derr));
481 		return (ECTF_CONVBKERR);
482 	}
483 
484 	return (0);
485 }
486 
487 static int
488 ctf_dwarf_signed(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name,
489     Dwarf_Signed *valp)
490 {
491 	int ret;
492 	Dwarf_Attribute attr;
493 	Dwarf_Error derr;
494 
495 	if ((ret = ctf_dwarf_attribute(cup, die, name, &attr)) != 0)
496 		return (ret);
497 
498 	if (dwarf_formsdata(attr, valp, &derr) == DW_DLV_OK) {
499 		dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
500 		return (0);
501 	}
502 
503 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
504 	    "failed to get unsigned attribute for type: %s\n",
505 	    dwarf_errmsg(derr));
506 	return (ECTF_CONVBKERR);
507 }
508 
509 static int
510 ctf_dwarf_unsigned(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name,
511     Dwarf_Unsigned *valp)
512 {
513 	int ret;
514 	Dwarf_Attribute attr;
515 	Dwarf_Error derr;
516 
517 	if ((ret = ctf_dwarf_attribute(cup, die, name, &attr)) != 0)
518 		return (ret);
519 
520 	if (dwarf_formudata(attr, valp, &derr) == DW_DLV_OK) {
521 		dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
522 		return (0);
523 	}
524 
525 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
526 	    "failed to get unsigned attribute for type: %s\n",
527 	    dwarf_errmsg(derr));
528 	return (ECTF_CONVBKERR);
529 }
530 
531 static int
532 ctf_dwarf_boolean(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name,
533     Dwarf_Bool *val)
534 {
535 	int ret;
536 	Dwarf_Attribute attr;
537 	Dwarf_Error derr;
538 
539 	if ((ret = ctf_dwarf_attribute(cup, die, name, &attr)) != 0)
540 		return (ret);
541 
542 	if (dwarf_formflag(attr, val, &derr) == DW_DLV_OK) {
543 		dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
544 		return (0);
545 	}
546 
547 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
548 	    "failed to get boolean attribute for type: %s\n",
549 	    dwarf_errmsg(derr));
550 
551 	return (ECTF_CONVBKERR);
552 }
553 
554 static int
555 ctf_dwarf_string(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name, char **strp)
556 {
557 	int ret;
558 	char *s;
559 	Dwarf_Attribute attr;
560 	Dwarf_Error derr;
561 
562 	*strp = NULL;
563 	if ((ret = ctf_dwarf_attribute(cup, die, name, &attr)) != 0)
564 		return (ret);
565 
566 	if (dwarf_formstring(attr, &s, &derr) == DW_DLV_OK) {
567 		if ((*strp = ctf_strdup(s)) == NULL)
568 			ret = ENOMEM;
569 		else
570 			ret = 0;
571 		dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
572 		return (ret);
573 	}
574 
575 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
576 	    "failed to get string attribute for type: %s\n",
577 	    dwarf_errmsg(derr));
578 	return (ECTF_CONVBKERR);
579 }
580 
581 static int
582 ctf_dwarf_member_location(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Unsigned *valp)
583 {
584 	int ret;
585 	Dwarf_Error derr;
586 	Dwarf_Attribute attr;
587 	Dwarf_Locdesc *loc;
588 	Dwarf_Signed locnum;
589 
590 	if ((ret = ctf_dwarf_attribute(cup, die, DW_AT_data_member_location,
591 	    &attr)) != 0)
592 		return (ret);
593 
594 	if (dwarf_loclist(attr, &loc, &locnum, &derr) != DW_DLV_OK) {
595 		(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
596 		    "failed to obtain location list for member offset: %s",
597 		    dwarf_errmsg(derr));
598 		dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
599 		return (ECTF_CONVBKERR);
600 	}
601 	dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
602 
603 	if (locnum != 1 || loc->ld_s->lr_atom != DW_OP_plus_uconst) {
604 		(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
605 		    "failed to parse location structure for member");
606 		dwarf_dealloc(cup->cu_dwarf, loc->ld_s, DW_DLA_LOC_BLOCK);
607 		dwarf_dealloc(cup->cu_dwarf, loc, DW_DLA_LOCDESC);
608 		return (ECTF_CONVBKERR);
609 	}
610 
611 	*valp = loc->ld_s->lr_number;
612 
613 	dwarf_dealloc(cup->cu_dwarf, loc->ld_s, DW_DLA_LOC_BLOCK);
614 	dwarf_dealloc(cup->cu_dwarf, loc, DW_DLA_LOCDESC);
615 	return (0);
616 }
617 
618 
619 static int
620 ctf_dwarf_offset(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Off *offsetp)
621 {
622 	Dwarf_Error derr;
623 
624 	if (dwarf_dieoffset(die, offsetp, &derr) == DW_DLV_OK)
625 		return (0);
626 
627 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
628 	    "failed to get die offset: %s\n",
629 	    dwarf_errmsg(derr));
630 	return (ECTF_CONVBKERR);
631 }
632 
633 /* simpler variant for debugging output */
634 static Dwarf_Off
635 ctf_die_offset(Dwarf_Die die)
636 {
637 	Dwarf_Off off = -1;
638 	Dwarf_Error derr;
639 
640 	(void) dwarf_dieoffset(die, &off, &derr);
641 	return (off);
642 }
643 
644 static int
645 ctf_dwarf_tag(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half *tagp)
646 {
647 	Dwarf_Error derr;
648 
649 	if (dwarf_tag(die, tagp, &derr) == DW_DLV_OK)
650 		return (0);
651 
652 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
653 	    "failed to get tag type: %s\n",
654 	    dwarf_errmsg(derr));
655 	return (ECTF_CONVBKERR);
656 }
657 
658 static int
659 ctf_dwarf_sib(ctf_cu_t *cup, Dwarf_Die base, Dwarf_Die *sibp)
660 {
661 	Dwarf_Error derr;
662 	int ret;
663 
664 	*sibp = NULL;
665 	ret = dwarf_siblingof(cup->cu_dwarf, base, sibp, &derr);
666 	if (ret == DW_DLV_OK || ret == DW_DLV_NO_ENTRY)
667 		return (0);
668 
669 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
670 	    "failed to sibling from die: %s\n",
671 	    dwarf_errmsg(derr));
672 	return (ECTF_CONVBKERR);
673 }
674 
675 static int
676 ctf_dwarf_child(ctf_cu_t *cup, Dwarf_Die base, Dwarf_Die *childp)
677 {
678 	Dwarf_Error derr;
679 	int ret;
680 
681 	*childp = NULL;
682 	ret = dwarf_child(base, childp, &derr);
683 	if (ret == DW_DLV_OK || ret == DW_DLV_NO_ENTRY)
684 		return (0);
685 
686 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
687 	    "failed to child from die: %s\n",
688 	    dwarf_errmsg(derr));
689 	return (ECTF_CONVBKERR);
690 }
691 
692 /*
693  * Compilers disagree on what to do to determine if something has global
694  * visiblity. Traditionally gcc has used DW_AT_external to indicate this while
695  * Studio has used DW_AT_visibility. We check DW_AT_visibility first and then
696  * fall back to DW_AT_external. Lack of DW_AT_external implies that it is not.
697  */
698 static int
699 ctf_dwarf_isglobal(ctf_cu_t *cup, Dwarf_Die die, boolean_t *igp)
700 {
701 	int ret;
702 	Dwarf_Signed vis;
703 	Dwarf_Bool ext;
704 
705 	if ((ret = ctf_dwarf_signed(cup, die, DW_AT_visibility, &vis)) == 0) {
706 		*igp = vis == DW_VIS_exported;
707 		return (0);
708 	} else if (ret != ENOENT) {
709 		return (ret);
710 	}
711 
712 	if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_external, &ext)) != 0) {
713 		if (ret == ENOENT) {
714 			*igp = B_FALSE;
715 			return (0);
716 		}
717 		return (ret);
718 	}
719 	*igp = ext != 0 ? B_TRUE : B_FALSE;
720 	return (0);
721 }
722 
723 static int
724 ctf_dwarf_die_elfenc(Elf *elf, ctf_cu_t *cup, char *errbuf, size_t errlen)
725 {
726 	GElf_Ehdr ehdr;
727 
728 	if (gelf_getehdr(elf, &ehdr) == NULL) {
729 		(void) snprintf(errbuf, errlen,
730 		    "failed to get ELF header: %s\n",
731 		    elf_errmsg(elf_errno()));
732 		return (ECTF_CONVBKERR);
733 	}
734 
735 	cup->cu_mach = ehdr.e_machine;
736 
737 	if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
738 		cup->cu_ptrsz = 4;
739 		VERIFY(ctf_setmodel(cup->cu_ctfp, CTF_MODEL_ILP32) == 0);
740 	} else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
741 		cup->cu_ptrsz = 8;
742 		VERIFY(ctf_setmodel(cup->cu_ctfp, CTF_MODEL_LP64) == 0);
743 	} else {
744 		(void) snprintf(errbuf, errlen,
745 		    "unknown ELF class %d", ehdr.e_ident[EI_CLASS]);
746 		return (ECTF_CONVBKERR);
747 	}
748 
749 	if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) {
750 		cup->cu_bigend = B_FALSE;
751 	} else if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
752 		cup->cu_bigend = B_TRUE;
753 	} else {
754 		(void) snprintf(errbuf, errlen,
755 		    "unknown ELF data encoding: %hhu", ehdr.e_ident[EI_DATA]);
756 		return (ECTF_CONVBKERR);
757 	}
758 
759 	return (0);
760 }
761 
762 typedef struct ctf_dwarf_fpent {
763 	size_t	cdfe_size;
764 	uint_t	cdfe_enc[3];
765 } ctf_dwarf_fpent_t;
766 
767 typedef struct ctf_dwarf_fpmap {
768 	uint_t			cdf_mach;
769 	ctf_dwarf_fpent_t	cdf_ents[4];
770 } ctf_dwarf_fpmap_t;
771 
772 static const ctf_dwarf_fpmap_t ctf_dwarf_fpmaps[] = {
773 	{ EM_SPARC, {
774 		{ 4, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
775 		{ 8, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
776 		{ 16, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
777 		{ 0, { 0 } }
778 	} },
779 	{ EM_SPARC32PLUS, {
780 		{ 4, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
781 		{ 8, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
782 		{ 16, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
783 		{ 0, { 0 } }
784 	} },
785 	{ EM_SPARCV9, {
786 		{ 4, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
787 		{ 8, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
788 		{ 16, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
789 		{ 0, { 0 } }
790 	} },
791 	{ EM_386, {
792 		{ 4, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
793 		{ 8, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
794 		{ 12, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
795 		{ 0, { 0 } }
796 	} },
797 	{ EM_X86_64, {
798 		{ 4, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
799 		{ 8, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
800 		{ 16, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
801 		{ 0, { 0 } }
802 	} },
803 	{ EM_NONE }
804 };
805 
806 static int
807 ctf_dwarf_float_base(ctf_cu_t *cup, Dwarf_Signed type, ctf_encoding_t *enc)
808 {
809 	const ctf_dwarf_fpmap_t *map = &ctf_dwarf_fpmaps[0];
810 	const ctf_dwarf_fpent_t *ent;
811 	uint_t col = 0, mult = 1;
812 
813 	for (map = &ctf_dwarf_fpmaps[0]; map->cdf_mach != EM_NONE; map++) {
814 		if (map->cdf_mach == cup->cu_mach)
815 			break;
816 	}
817 
818 	if (map->cdf_mach == EM_NONE) {
819 		(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
820 		    "Unsupported machine type: %d\n", cup->cu_mach);
821 		return (ENOTSUP);
822 	}
823 
824 	if (type == DW_ATE_complex_float) {
825 		mult = 2;
826 		col = 1;
827 	} else if (type == DW_ATE_imaginary_float ||
828 	    type == DW_ATE_SUN_imaginary_float) {
829 		col = 2;
830 	}
831 
832 	ent = &map->cdf_ents[0];
833 	for (ent = &map->cdf_ents[0]; ent->cdfe_size != 0; ent++) {
834 		if (ent->cdfe_size * mult * 8 == enc->cte_bits) {
835 			enc->cte_format = ent->cdfe_enc[col];
836 			return (0);
837 		}
838 	}
839 
840 	(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
841 	    "failed to find valid fp mapping for encoding %d, size %d bits\n",
842 	    type, enc->cte_bits);
843 	return (EINVAL);
844 }
845 
846 static int
847 ctf_dwarf_dwarf_base(ctf_cu_t *cup, Dwarf_Die die, int *kindp,
848     ctf_encoding_t *enc)
849 {
850 	int ret;
851 	Dwarf_Signed type;
852 
853 	if ((ret = ctf_dwarf_signed(cup, die, DW_AT_encoding, &type)) != 0)
854 		return (ret);
855 
856 	switch (type) {
857 	case DW_ATE_unsigned:
858 	case DW_ATE_address:
859 		*kindp = CTF_K_INTEGER;
860 		enc->cte_format = 0;
861 		break;
862 	case DW_ATE_unsigned_char:
863 		*kindp = CTF_K_INTEGER;
864 		enc->cte_format = CTF_INT_CHAR;
865 		break;
866 	case DW_ATE_signed:
867 		*kindp = CTF_K_INTEGER;
868 		enc->cte_format = CTF_INT_SIGNED;
869 		break;
870 	case DW_ATE_signed_char:
871 		*kindp = CTF_K_INTEGER;
872 		enc->cte_format = CTF_INT_SIGNED | CTF_INT_CHAR;
873 		break;
874 	case DW_ATE_boolean:
875 		*kindp = CTF_K_INTEGER;
876 		enc->cte_format = CTF_INT_SIGNED | CTF_INT_BOOL;
877 		break;
878 	case DW_ATE_float:
879 	case DW_ATE_complex_float:
880 	case DW_ATE_imaginary_float:
881 	case DW_ATE_SUN_imaginary_float:
882 	case DW_ATE_SUN_interval_float:
883 		*kindp = CTF_K_FLOAT;
884 		if ((ret = ctf_dwarf_float_base(cup, type, enc)) != 0)
885 			return (ret);
886 		break;
887 	default:
888 		(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
889 		    "encountered unkown DWARF encoding: %d", type);
890 		return (ECTF_CONVBKERR);
891 	}
892 
893 	return (0);
894 }
895 
896 /*
897  * Different compilers (at least GCC and Studio) use different names for types.
898  * This parses the types and attempts to unify them. If this fails, we just fall
899  * back to using the DWARF itself.
900  */
901 static int
902 ctf_dwarf_parse_base(const char *name, int *kindp, ctf_encoding_t *enc,
903     char **newnamep)
904 {
905 	char buf[256];
906 	char *base, *c, *last;
907 	int nlong = 0, nshort = 0, nchar = 0, nint = 0;
908 	int sign = 1;
909 
910 	if (strlen(name) + 1 > sizeof (buf))
911 		return (EINVAL);
912 
913 	(void) strlcpy(buf, name, sizeof (buf));
914 	for (c = strtok_r(buf, " ", &last); c != NULL;
915 	    c = strtok_r(NULL, " ", &last)) {
916 		if (strcmp(c, "signed") == 0) {
917 			sign = 1;
918 		} else if (strcmp(c, "unsigned") == 0) {
919 			sign = 0;
920 		} else if (strcmp(c, "long") == 0) {
921 			nlong++;
922 		} else if (strcmp(c, "char") == 0) {
923 			nchar++;
924 		} else if (strcmp(c, "short") == 0) {
925 			nshort++;
926 		} else if (strcmp(c, "int") == 0) {
927 			nint++;
928 		} else {
929 			/*
930 			 * If we don't recognize any of the tokens, we'll tell
931 			 * the caller to fall back to the dwarf-provided
932 			 * encoding information.
933 			 */
934 			return (EINVAL);
935 		}
936 	}
937 
938 	if (nchar > 1 || nshort > 1 || nint > 1 || nlong > 2)
939 		return (EINVAL);
940 
941 	if (nchar > 0) {
942 		if (nlong > 0 || nshort > 0 || nint > 0)
943 			return (EINVAL);
944 		base = "char";
945 	} else if (nshort > 0) {
946 		if (nlong > 0)
947 			return (EINVAL);
948 		base = "short";
949 	} else if (nlong > 0) {
950 		base = "long";
951 	} else {
952 		base = "int";
953 	}
954 
955 	if (nchar > 0)
956 		enc->cte_format = CTF_INT_CHAR;
957 	else
958 		enc->cte_format = 0;
959 
960 	if (sign > 0)
961 		enc->cte_format |= CTF_INT_SIGNED;
962 
963 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
964 	    (sign ? "" : "unsigned "),
965 	    (nlong > 1 ? "long " : ""),
966 	    base);
967 
968 	*newnamep = ctf_strdup(buf);
969 	if (*newnamep == NULL)
970 		return (ENOMEM);
971 	*kindp = CTF_K_INTEGER;
972 	return (0);
973 }
974 
975 static int
976 ctf_dwarf_create_base(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot,
977     Dwarf_Off off)
978 {
979 	int ret;
980 	char *name, *nname;
981 	Dwarf_Unsigned sz;
982 	int kind;
983 	ctf_encoding_t enc;
984 	ctf_id_t id;
985 
986 	if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0)
987 		return (ret);
988 	if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_byte_size, &sz)) != 0) {
989 		goto out;
990 	}
991 	ctf_dprintf("Creating base type %s from off %llu, size: %d\n", name,
992 	    off, sz);
993 
994 	bzero(&enc, sizeof (ctf_encoding_t));
995 	enc.cte_bits = sz * 8;
996 	if ((ret = ctf_dwarf_parse_base(name, &kind, &enc, &nname)) == 0) {
997 		ctf_free(name, strlen(name) + 1);
998 		name = nname;
999 	} else {
1000 		if (ret != EINVAL)
1001 			return (ret);
1002 		ctf_dprintf("falling back to dwarf for base type %s\n", name);
1003 		if ((ret = ctf_dwarf_dwarf_base(cup, die, &kind, &enc)) != 0)
1004 			return (ret);
1005 	}
1006 
1007 	id = ctf_add_encoded(cup->cu_ctfp, isroot, name, &enc, kind);
1008 	if (id == CTF_ERR) {
1009 		ret = ctf_errno(cup->cu_ctfp);
1010 	} else {
1011 		*idp = id;
1012 		ret = ctf_dwmap_add(cup, id, die, B_FALSE);
1013 	}
1014 out:
1015 	ctf_free(name, strlen(name) + 1);
1016 	return (ret);
1017 }
1018 
1019 /*
1020  * Getting a member's offset is a surprisingly intricate dance. It works as
1021  * follows:
1022  *
1023  * 1) If we're in DWARFv4, then we either have a DW_AT_data_bit_offset or we
1024  * have a DW_AT_data_member_location. We won't have both. Thus we check first
1025  * for DW_AT_data_bit_offset, and if it exists, we're set.
1026  *
1027  * Next, if we have a bitfield and we don't have a DW_AT_data_bit_offset, then
1028  * we have to grab the data location and use the following dance:
1029  *
1030  * 2) Gather the set of DW_AT_byte_size, DW_AT_bit_offset, and DW_AT_bit_size.
1031  * Of course, the DW_AT_byte_size may be omitted, even though it isn't always.
1032  * When it's been omitted, we then have to say that the size is that of the
1033  * underlying type, which forces that to be after a ctf_update(). Here, we have
1034  * to do different things based on whether or not we're using big endian or
1035  * little endian to obtain the proper offset.
1036  */
1037 static int
1038 ctf_dwarf_member_offset(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t mid,
1039     ulong_t *offp)
1040 {
1041 	int ret;
1042 	Dwarf_Unsigned loc, bitsz, bytesz;
1043 	Dwarf_Signed bitoff;
1044 	size_t off;
1045 	ssize_t tsz;
1046 
1047 	if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_data_bit_offset,
1048 	    &loc)) == 0) {
1049 		*offp = loc;
1050 		return (0);
1051 	} else if (ret != ENOENT) {
1052 		return (ret);
1053 	}
1054 
1055 	if ((ret = ctf_dwarf_member_location(cup, die, &loc)) != 0)
1056 		return (ret);
1057 	off = loc * 8;
1058 
1059 	if ((ret = ctf_dwarf_signed(cup, die, DW_AT_bit_offset,
1060 	    &bitoff)) != 0) {
1061 		if (ret != ENOENT)
1062 			return (ret);
1063 		*offp = off;
1064 		return (0);
1065 	}
1066 
1067 	/* At this point we have to have DW_AT_bit_size */
1068 	if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_bit_size, &bitsz)) != 0)
1069 		return (ret);
1070 
1071 	if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_byte_size,
1072 	    &bytesz)) != 0) {
1073 		if (ret != ENOENT)
1074 			return (ret);
1075 		if ((tsz = ctf_type_size(cup->cu_ctfp, mid)) == CTF_ERR) {
1076 			int e = ctf_errno(cup->cu_ctfp);
1077 			(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1078 			    "failed to get type size: %s", ctf_errmsg(e));
1079 			return (ECTF_CONVBKERR);
1080 		}
1081 	} else {
1082 		tsz = bytesz;
1083 	}
1084 	tsz *= 8;
1085 	if (cup->cu_bigend == B_TRUE) {
1086 		*offp = off + bitoff;
1087 	} else {
1088 		*offp = off + tsz - bitoff - bitsz;
1089 	}
1090 
1091 	return (0);
1092 }
1093 
1094 /*
1095  * We need to determine if the member in question is a bitfield. If it is, then
1096  * we need to go through and create a new type that's based on the actual base
1097  * type, but has a different size. We also rename the type as a result to help
1098  * deal with future collisions.
1099  *
1100  * Here we need to look and see if we have a DW_AT_bit_size value. If we have a
1101  * bit size member and it does not equal the byte size member, then we need to
1102  * create a bitfield type based on this.
1103  *
1104  * Note: When we support DWARFv4, there may be a chance that we need to also
1105  * search for the DW_AT_byte_size if we don't have a DW_AT_bit_size member.
1106  */
1107 static int
1108 ctf_dwarf_member_bitfield(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp)
1109 {
1110 	int ret;
1111 	Dwarf_Unsigned bitsz;
1112 	ctf_encoding_t e;
1113 	ctf_dwbitf_t *cdb;
1114 	ctf_dtdef_t *dtd;
1115 	ctf_id_t base = *idp;
1116 	int kind;
1117 
1118 	if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_bit_size, &bitsz)) != 0) {
1119 		if (ret == ENOENT)
1120 			return (0);
1121 		return (ret);
1122 	}
1123 
1124 	ctf_dprintf("Trying to deal with bitfields on %d:%d\n", base, bitsz);
1125 	/*
1126 	 * Given that we now have a bitsize, time to go do something about it.
1127 	 * We're going to create a new type based on the current one, but first
1128 	 * we need to find the base type. This means we need to traverse any
1129 	 * typedef's, consts, and volatiles until we get to what should be
1130 	 * something of type integer or enumeration.
1131 	 */
1132 	VERIFY(bitsz < UINT32_MAX);
1133 	dtd = ctf_dtd_lookup(cup->cu_ctfp, base);
1134 	VERIFY(dtd != NULL);
1135 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1136 	while (kind == CTF_K_TYPEDEF || kind == CTF_K_CONST ||
1137 	    kind == CTF_K_VOLATILE) {
1138 		dtd = ctf_dtd_lookup(cup->cu_ctfp, dtd->dtd_data.ctt_type);
1139 		VERIFY(dtd != NULL);
1140 		kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1141 	}
1142 	ctf_dprintf("got kind %d\n", kind);
1143 	VERIFY(kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
1144 
1145 	/*
1146 	 * As surprising as it may be, it is strictly possible to create a
1147 	 * bitfield that is based on an enum. Of course, the C standard leaves
1148 	 * enums sizing as an ABI concern more or less. To that effect, today on
1149 	 * all illumos platforms the size of an enum is generally that of an
1150 	 * int as our supported data models and ABIs all agree on that. So what
1151 	 * we'll do is fake up a CTF encoding here to use. In this case, we'll
1152 	 * treat it as an unsigned value of whatever size the underlying enum
1153 	 * currently has (which is in the ctt_size member of its dynamic type
1154 	 * data).
1155 	 */
1156 	if (kind == CTF_K_INTEGER) {
1157 		e = dtd->dtd_u.dtu_enc;
1158 	} else {
1159 		bzero(&e, sizeof (ctf_encoding_t));
1160 		e.cte_bits = dtd->dtd_data.ctt_size * NBBY;
1161 	}
1162 
1163 	for (cdb = ctf_list_next(&cup->cu_bitfields); cdb != NULL;
1164 	    cdb = ctf_list_next(cdb)) {
1165 		if (cdb->cdb_base == base && cdb->cdb_nbits == bitsz)
1166 			break;
1167 	}
1168 
1169 	/*
1170 	 * Create a new type if none exists. We name all types in a way that is
1171 	 * guaranteed not to conflict with the corresponding C type. We do this
1172 	 * by using the ':' operator.
1173 	 */
1174 	if (cdb == NULL) {
1175 		size_t namesz;
1176 		char *name;
1177 
1178 		e.cte_bits = bitsz;
1179 		namesz = snprintf(NULL, 0, "%s:%d", dtd->dtd_name,
1180 		    (uint32_t)bitsz);
1181 		name = ctf_alloc(namesz + 1);
1182 		if (name == NULL)
1183 			return (ENOMEM);
1184 		cdb = ctf_alloc(sizeof (ctf_dwbitf_t));
1185 		if (cdb == NULL) {
1186 			ctf_free(name, namesz + 1);
1187 			return (ENOMEM);
1188 		}
1189 		(void) snprintf(name, namesz + 1, "%s:%d", dtd->dtd_name,
1190 		    (uint32_t)bitsz);
1191 
1192 		cdb->cdb_base = base;
1193 		cdb->cdb_nbits = bitsz;
1194 		cdb->cdb_id = ctf_add_integer(cup->cu_ctfp, CTF_ADD_NONROOT,
1195 		    name, &e);
1196 		if (cdb->cdb_id == CTF_ERR) {
1197 			(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1198 			    "failed to get add bitfield type %s: %s", name,
1199 			    ctf_errmsg(ctf_errno(cup->cu_ctfp)));
1200 			ctf_free(name, namesz + 1);
1201 			ctf_free(cdb, sizeof (ctf_dwbitf_t));
1202 			return (ECTF_CONVBKERR);
1203 		}
1204 		ctf_free(name, namesz + 1);
1205 		ctf_list_append(&cup->cu_bitfields, cdb);
1206 	}
1207 
1208 	*idp = cdb->cdb_id;
1209 
1210 	return (0);
1211 }
1212 
1213 static int
1214 ctf_dwarf_fixup_sou(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t base, boolean_t add)
1215 {
1216 	int ret, kind;
1217 	Dwarf_Die child, memb;
1218 	Dwarf_Unsigned size;
1219 	ulong_t nsz;
1220 
1221 	kind = ctf_type_kind(cup->cu_ctfp, base);
1222 	VERIFY(kind != CTF_ERR);
1223 	VERIFY(kind == CTF_K_STRUCT || kind == CTF_K_UNION);
1224 
1225 	/*
1226 	 * Members are in children. However, gcc also allows empty ones.
1227 	 */
1228 	if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1229 		return (ret);
1230 	if (child == NULL)
1231 		return (0);
1232 
1233 	memb = child;
1234 	while (memb != NULL) {
1235 		Dwarf_Die sib, tdie;
1236 		Dwarf_Half tag;
1237 		ctf_id_t mid;
1238 		char *mname;
1239 		ulong_t memboff = 0;
1240 
1241 		if ((ret = ctf_dwarf_tag(cup, memb, &tag)) != 0)
1242 			return (ret);
1243 
1244 		if (tag != DW_TAG_member)
1245 			continue;
1246 
1247 		if ((ret = ctf_dwarf_refdie(cup, memb, DW_AT_type, &tdie)) != 0)
1248 			return (ret);
1249 
1250 		if ((ret = ctf_dwarf_convert_type(cup, tdie, &mid,
1251 		    CTF_ADD_NONROOT)) != 0)
1252 			return (ret);
1253 		ctf_dprintf("Got back type id: %d\n", mid);
1254 
1255 		/*
1256 		 * If we're not adding a member, just go ahead and return.
1257 		 */
1258 		if (add == B_FALSE) {
1259 			if ((ret = ctf_dwarf_member_bitfield(cup, memb,
1260 			    &mid)) != 0)
1261 				return (ret);
1262 			goto next;
1263 		}
1264 
1265 		if ((ret = ctf_dwarf_string(cup, memb, DW_AT_name,
1266 		    &mname)) != 0 && ret != ENOENT)
1267 			return (ret);
1268 		if (ret == ENOENT)
1269 			mname = NULL;
1270 
1271 		if (kind == CTF_K_UNION) {
1272 			memboff = 0;
1273 		} else if ((ret = ctf_dwarf_member_offset(cup, memb, mid,
1274 		    &memboff)) != 0) {
1275 			if (mname != NULL)
1276 				ctf_free(mname, strlen(mname) + 1);
1277 			return (ret);
1278 		}
1279 
1280 		if ((ret = ctf_dwarf_member_bitfield(cup, memb, &mid)) != 0)
1281 			return (ret);
1282 
1283 		ret = ctf_add_member(cup->cu_ctfp, base, mname, mid, memboff);
1284 		if (ret == CTF_ERR) {
1285 			(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1286 			    "failed to add member %s: %s",
1287 			    mname, ctf_errmsg(ctf_errno(cup->cu_ctfp)));
1288 			if (mname != NULL)
1289 				ctf_free(mname, strlen(mname) + 1);
1290 			return (ECTF_CONVBKERR);
1291 		}
1292 
1293 		if (mname != NULL)
1294 			ctf_free(mname, strlen(mname) + 1);
1295 
1296 next:
1297 		if ((ret = ctf_dwarf_sib(cup, memb, &sib)) != 0)
1298 			return (ret);
1299 		memb = sib;
1300 	}
1301 
1302 	/*
1303 	 * If we're not adding members, then we don't know the final size of the
1304 	 * structure, so end here.
1305 	 */
1306 	if (add == B_FALSE)
1307 		return (0);
1308 
1309 	/* Finally set the size of the structure to the actual byte size */
1310 	if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_byte_size, &size)) != 0)
1311 		return (ret);
1312 	nsz = size;
1313 	if ((ctf_set_size(cup->cu_ctfp, base, nsz)) == CTF_ERR) {
1314 		int e = ctf_errno(cup->cu_ctfp);
1315 		(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1316 		    "failed to set type size for %d to 0x%x: %s", base,
1317 		    (uint32_t)size, ctf_errmsg(e));
1318 		return (ECTF_CONVBKERR);
1319 	}
1320 
1321 	return (0);
1322 }
1323 
1324 static int
1325 ctf_dwarf_create_sou(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp,
1326     int kind, int isroot)
1327 {
1328 	int ret;
1329 	char *name;
1330 	ctf_id_t base;
1331 	Dwarf_Die child;
1332 	Dwarf_Bool decl;
1333 
1334 	/*
1335 	 * Deal with the terribly annoying case of anonymous structs and unions.
1336 	 * If they don't have a name, set the name to the empty string.
1337 	 */
1338 	if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
1339 	    ret != ENOENT)
1340 		return (ret);
1341 	if (ret == ENOENT)
1342 		name = NULL;
1343 
1344 	/*
1345 	 * We need to check if we just have a declaration here. If we do, then
1346 	 * instead of creating an actual structure or union, we're just going to
1347 	 * go ahead and create a forward. During a dedup or merge, the forward
1348 	 * will be replaced with the real thing.
1349 	 */
1350 	if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_declaration,
1351 	    &decl)) != 0) {
1352 		if (ret != ENOENT)
1353 			return (ret);
1354 		decl = 0;
1355 	}
1356 
1357 	if (decl != 0) {
1358 		base = ctf_add_forward(cup->cu_ctfp, isroot, name, kind);
1359 	} else if (kind == CTF_K_STRUCT) {
1360 		base = ctf_add_struct(cup->cu_ctfp, isroot, name);
1361 	} else {
1362 		base = ctf_add_union(cup->cu_ctfp, isroot, name);
1363 	}
1364 	ctf_dprintf("added sou %s (%d) (%d)\n", name, kind, base);
1365 	if (name != NULL)
1366 		ctf_free(name, strlen(name) + 1);
1367 	if (base == CTF_ERR)
1368 		return (ctf_errno(cup->cu_ctfp));
1369 	*idp = base;
1370 
1371 	/*
1372 	 * If it's just a declaration, we're not going to mark it for fix up or
1373 	 * do anything else.
1374 	 */
1375 	if (decl == B_TRUE)
1376 		return (ctf_dwmap_add(cup, base, die, B_FALSE));
1377 	if ((ret = ctf_dwmap_add(cup, base, die, B_TRUE)) != 0)
1378 		return (ret);
1379 
1380 	/*
1381 	 * Members are in children. However, gcc also allows empty ones.
1382 	 */
1383 	if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1384 		return (ret);
1385 	if (child == NULL)
1386 		return (0);
1387 
1388 	return (0);
1389 }
1390 
1391 static int
1392 ctf_dwarf_create_array_range(ctf_cu_t *cup, Dwarf_Die range, ctf_id_t *idp,
1393     ctf_id_t base, int isroot)
1394 {
1395 	int ret;
1396 	Dwarf_Die sib;
1397 	Dwarf_Unsigned val;
1398 	Dwarf_Signed sval;
1399 	ctf_arinfo_t ar;
1400 
1401 	ctf_dprintf("creating array range\n");
1402 
1403 	if ((ret = ctf_dwarf_sib(cup, range, &sib)) != 0)
1404 		return (ret);
1405 	if (sib != NULL) {
1406 		ctf_id_t id;
1407 		if ((ret = ctf_dwarf_create_array_range(cup, sib, &id,
1408 		    base, CTF_ADD_NONROOT)) != 0)
1409 			return (ret);
1410 		ar.ctr_contents = id;
1411 	} else {
1412 		ar.ctr_contents = base;
1413 	}
1414 
1415 	if ((ar.ctr_index = ctf_dwarf_long(cup)) == CTF_ERR)
1416 		return (ctf_errno(cup->cu_ctfp));
1417 
1418 	/*
1419 	 * Array bounds can be signed or unsigned, but there are several kinds
1420 	 * of signless forms (data1, data2, etc) that take their sign from the
1421 	 * routine that is trying to interpret them.  That is, data1 can be
1422 	 * either signed or unsigned, depending on whether you use the signed or
1423 	 * unsigned accessor function.  GCC will use the signless forms to store
1424 	 * unsigned values which have their high bit set, so we need to try to
1425 	 * read them first as unsigned to get positive values.  We could also
1426 	 * try signed first, falling back to unsigned if we got a negative
1427 	 * value.
1428 	 */
1429 	if ((ret = ctf_dwarf_unsigned(cup, range, DW_AT_upper_bound,
1430 	    &val)) == 0) {
1431 		ar.ctr_nelems = val + 1;
1432 	} else if (ret != ENOENT) {
1433 		return (ret);
1434 	} else if ((ret = ctf_dwarf_signed(cup, range, DW_AT_upper_bound,
1435 	    &sval)) == 0) {
1436 		ar.ctr_nelems = sval + 1;
1437 	} else if (ret != ENOENT) {
1438 		return (ret);
1439 	} else {
1440 		ar.ctr_nelems = 0;
1441 	}
1442 
1443 	if ((*idp = ctf_add_array(cup->cu_ctfp, isroot, &ar)) == CTF_ERR)
1444 		return (ctf_errno(cup->cu_ctfp));
1445 
1446 	return (0);
1447 }
1448 
1449 /*
1450  * Try and create an array type. First, the kind of the array is specified in
1451  * the DW_AT_type entry. Next, the number of entries is stored in a more
1452  * complicated form, we should have a child that has the DW_TAG_subrange type.
1453  */
1454 static int
1455 ctf_dwarf_create_array(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot)
1456 {
1457 	int ret;
1458 	Dwarf_Die tdie, rdie;
1459 	ctf_id_t tid;
1460 	Dwarf_Half rtag;
1461 
1462 	if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0)
1463 		return (ret);
1464 	if ((ret = ctf_dwarf_convert_type(cup, tdie, &tid,
1465 	    CTF_ADD_NONROOT)) != 0)
1466 		return (ret);
1467 
1468 	if ((ret = ctf_dwarf_child(cup, die, &rdie)) != 0)
1469 		return (ret);
1470 	if ((ret = ctf_dwarf_tag(cup, rdie, &rtag)) != 0)
1471 		return (ret);
1472 	if (rtag != DW_TAG_subrange_type) {
1473 		(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1474 		    "encountered array without DW_TAG_subrange_type child\n");
1475 		return (ECTF_CONVBKERR);
1476 	}
1477 
1478 	/*
1479 	 * The compiler may opt to describe a multi-dimensional array as one
1480 	 * giant array or it may opt to instead encode it as a series of
1481 	 * subranges. If it's the latter, then for each subrange we introduce a
1482 	 * type. We can always use the base type.
1483 	 */
1484 	if ((ret = ctf_dwarf_create_array_range(cup, rdie, idp, tid,
1485 	    isroot)) != 0)
1486 		return (ret);
1487 	ctf_dprintf("Got back id %d\n", *idp);
1488 	return (ctf_dwmap_add(cup, *idp, die, B_FALSE));
1489 }
1490 
1491 static int
1492 ctf_dwarf_create_reference(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp,
1493     int kind, int isroot)
1494 {
1495 	int ret;
1496 	ctf_id_t id;
1497 	Dwarf_Die tdie;
1498 	char *name;
1499 	size_t namelen;
1500 
1501 	if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
1502 	    ret != ENOENT)
1503 		return (ret);
1504 	if (ret == ENOENT) {
1505 		name = NULL;
1506 		namelen = 0;
1507 	} else {
1508 		namelen = strlen(name);
1509 	}
1510 
1511 	ctf_dprintf("reference kind %d %s\n", kind, name != NULL ? name : "<>");
1512 
1513 	if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0) {
1514 		if (ret != ENOENT) {
1515 			ctf_free(name, namelen);
1516 			return (ret);
1517 		}
1518 		if ((id = ctf_dwarf_void(cup)) == CTF_ERR) {
1519 			ctf_free(name, namelen);
1520 			return (ctf_errno(cup->cu_ctfp));
1521 		}
1522 	} else {
1523 		if ((ret = ctf_dwarf_convert_type(cup, tdie, &id,
1524 		    CTF_ADD_NONROOT)) != 0) {
1525 			ctf_free(name, namelen);
1526 			return (ret);
1527 		}
1528 	}
1529 
1530 	if ((*idp = ctf_add_reftype(cup->cu_ctfp, isroot, name, id, kind)) ==
1531 	    CTF_ERR) {
1532 		ctf_free(name, namelen);
1533 		return (ctf_errno(cup->cu_ctfp));
1534 	}
1535 
1536 	ctf_free(name, namelen);
1537 	return (ctf_dwmap_add(cup, *idp, die, B_FALSE));
1538 }
1539 
1540 static int
1541 ctf_dwarf_create_enum(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot)
1542 {
1543 	int ret;
1544 	ctf_id_t id;
1545 	Dwarf_Die child;
1546 	char *name;
1547 
1548 	if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
1549 	    ret != ENOENT)
1550 		return (ret);
1551 	if (ret == ENOENT)
1552 		name = NULL;
1553 	id = ctf_add_enum(cup->cu_ctfp, isroot, name);
1554 	ctf_dprintf("added enum %s (%d)\n", name, id);
1555 	if (name != NULL)
1556 		ctf_free(name, strlen(name) + 1);
1557 	if (id == CTF_ERR)
1558 		return (ctf_errno(cup->cu_ctfp));
1559 	*idp = id;
1560 	if ((ret = ctf_dwmap_add(cup, id, die, B_FALSE)) != 0)
1561 		return (ret);
1562 
1563 	if ((ret = ctf_dwarf_child(cup, die, &child)) != 0) {
1564 		if (ret == ENOENT)
1565 			ret = 0;
1566 		return (ret);
1567 	}
1568 
1569 	while (child != NULL) {
1570 		Dwarf_Half tag;
1571 		Dwarf_Signed sval;
1572 		Dwarf_Unsigned uval;
1573 		Dwarf_Die arg = child;
1574 		int eval;
1575 
1576 		if ((ret = ctf_dwarf_sib(cup, arg, &child)) != 0)
1577 			return (ret);
1578 
1579 		if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0)
1580 			return (ret);
1581 
1582 		if (tag != DW_TAG_enumerator) {
1583 			if ((ret = ctf_dwarf_convert_type(cup, arg, NULL,
1584 			    CTF_ADD_NONROOT)) != 0)
1585 				return (ret);
1586 			continue;
1587 		}
1588 
1589 		/*
1590 		 * DWARF v4 section 5.7 tells us we'll always have names.
1591 		 */
1592 		if ((ret = ctf_dwarf_string(cup, arg, DW_AT_name, &name)) != 0)
1593 			return (ret);
1594 
1595 		/*
1596 		 * We have to be careful here: newer GCCs generate DWARF where
1597 		 * an unsigned value will happily pass ctf_dwarf_signed().
1598 		 * Since negative values will fail ctf_dwarf_unsigned(), we try
1599 		 * that first to make sure we get the right value.
1600 		 */
1601 		if ((ret = ctf_dwarf_unsigned(cup, arg, DW_AT_const_value,
1602 		    &uval)) == 0) {
1603 			eval = (int)uval;
1604 		} else if ((ret = ctf_dwarf_signed(cup, arg, DW_AT_const_value,
1605 		    &sval)) == 0) {
1606 			eval = sval;
1607 		}
1608 
1609 		if (ret != 0) {
1610 			if (ret != ENOENT)
1611 				return (ret);
1612 
1613 			(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1614 			    "encountered enumeration without constant value\n");
1615 			return (ECTF_CONVBKERR);
1616 		}
1617 
1618 		ret = ctf_add_enumerator(cup->cu_ctfp, id, name, eval);
1619 		if (ret == CTF_ERR) {
1620 			(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1621 			    "failed to add enumarator %s (%d) to %d\n",
1622 			    name, eval, id);
1623 			ctf_free(name, strlen(name) + 1);
1624 			return (ctf_errno(cup->cu_ctfp));
1625 		}
1626 		ctf_free(name, strlen(name) + 1);
1627 	}
1628 
1629 	return (0);
1630 }
1631 
1632 /*
1633  * For a function pointer, walk over and process all of its children, unless we
1634  * encounter one that's just a declaration. In which case, we error on it.
1635  */
1636 static int
1637 ctf_dwarf_create_fptr(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot)
1638 {
1639 	int ret;
1640 	Dwarf_Bool b;
1641 	ctf_funcinfo_t fi;
1642 	Dwarf_Die retdie;
1643 	ctf_id_t *argv = NULL;
1644 
1645 	bzero(&fi, sizeof (ctf_funcinfo_t));
1646 
1647 	if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_declaration, &b)) != 0) {
1648 		if (ret != ENOENT)
1649 			return (ret);
1650 	} else {
1651 		if (b != 0)
1652 			return (EPROTOTYPE);
1653 	}
1654 
1655 	/*
1656 	 * Return type is in DW_AT_type, if none, it returns void.
1657 	 */
1658 	if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &retdie)) != 0) {
1659 		if (ret != ENOENT)
1660 			return (ret);
1661 		if ((fi.ctc_return = ctf_dwarf_void(cup)) == CTF_ERR)
1662 			return (ctf_errno(cup->cu_ctfp));
1663 	} else {
1664 		if ((ret = ctf_dwarf_convert_type(cup, retdie, &fi.ctc_return,
1665 		    CTF_ADD_NONROOT)) != 0)
1666 			return (ret);
1667 	}
1668 
1669 	if ((ret = ctf_dwarf_function_count(cup, die, &fi, B_TRUE)) != 0) {
1670 		return (ret);
1671 	}
1672 
1673 	if (fi.ctc_argc != 0) {
1674 		argv = ctf_alloc(sizeof (ctf_id_t) * fi.ctc_argc);
1675 		if (argv == NULL)
1676 			return (ENOMEM);
1677 
1678 		if ((ret = ctf_dwarf_convert_fargs(cup, die, &fi, argv)) != 0) {
1679 			ctf_free(argv, sizeof (ctf_id_t) * fi.ctc_argc);
1680 			return (ret);
1681 		}
1682 	}
1683 
1684 	if ((*idp = ctf_add_funcptr(cup->cu_ctfp, isroot, &fi, argv)) ==
1685 	    CTF_ERR) {
1686 		ctf_free(argv, sizeof (ctf_id_t) * fi.ctc_argc);
1687 		return (ctf_errno(cup->cu_ctfp));
1688 	}
1689 
1690 	ctf_free(argv, sizeof (ctf_id_t) * fi.ctc_argc);
1691 	return (ctf_dwmap_add(cup, *idp, die, B_FALSE));
1692 }
1693 
1694 static int
1695 ctf_dwarf_convert_type(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp,
1696     int isroot)
1697 {
1698 	int ret;
1699 	Dwarf_Off offset;
1700 	Dwarf_Half tag;
1701 	ctf_dwmap_t lookup, *map;
1702 	ctf_id_t id;
1703 
1704 	if (idp == NULL)
1705 		idp = &id;
1706 
1707 	if ((ret = ctf_dwarf_offset(cup, die, &offset)) != 0)
1708 		return (ret);
1709 
1710 	if (offset > cup->cu_maxoff) {
1711 		(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1712 		    "die offset %llu beyond maximum for header %llu\n",
1713 		    offset, cup->cu_maxoff);
1714 		return (ECTF_CONVBKERR);
1715 	}
1716 
1717 	/*
1718 	 * If we've already added an entry for this offset, then we're done.
1719 	 */
1720 	lookup.cdm_off = offset;
1721 	if ((map = avl_find(&cup->cu_map, &lookup, NULL)) != NULL) {
1722 		*idp = map->cdm_id;
1723 		return (0);
1724 	}
1725 
1726 	if ((ret = ctf_dwarf_tag(cup, die, &tag)) != 0)
1727 		return (ret);
1728 
1729 	ret = ENOTSUP;
1730 	switch (tag) {
1731 	case DW_TAG_base_type:
1732 		ctf_dprintf("base\n");
1733 		ret = ctf_dwarf_create_base(cup, die, idp, isroot, offset);
1734 		break;
1735 	case DW_TAG_array_type:
1736 		ctf_dprintf("array\n");
1737 		ret = ctf_dwarf_create_array(cup, die, idp, isroot);
1738 		break;
1739 	case DW_TAG_enumeration_type:
1740 		ctf_dprintf("enum\n");
1741 		ret = ctf_dwarf_create_enum(cup, die, idp, isroot);
1742 		break;
1743 	case DW_TAG_pointer_type:
1744 		ctf_dprintf("pointer\n");
1745 		ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_POINTER,
1746 		    isroot);
1747 		break;
1748 	case DW_TAG_structure_type:
1749 		ctf_dprintf("struct\n");
1750 		ret = ctf_dwarf_create_sou(cup, die, idp, CTF_K_STRUCT,
1751 		    isroot);
1752 		break;
1753 	case DW_TAG_subroutine_type:
1754 		ctf_dprintf("fptr\n");
1755 		ret = ctf_dwarf_create_fptr(cup, die, idp, isroot);
1756 		break;
1757 	case DW_TAG_typedef:
1758 		ctf_dprintf("typedef\n");
1759 		ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_TYPEDEF,
1760 		    isroot);
1761 		break;
1762 	case DW_TAG_union_type:
1763 		ctf_dprintf("union\n");
1764 		ret = ctf_dwarf_create_sou(cup, die, idp, CTF_K_UNION,
1765 		    isroot);
1766 		break;
1767 	case DW_TAG_const_type:
1768 		ctf_dprintf("const\n");
1769 		ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_CONST,
1770 		    isroot);
1771 		break;
1772 	case DW_TAG_volatile_type:
1773 		ctf_dprintf("volatile\n");
1774 		ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_VOLATILE,
1775 		    isroot);
1776 		break;
1777 	case DW_TAG_restrict_type:
1778 		ctf_dprintf("restrict\n");
1779 		ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_RESTRICT,
1780 		    isroot);
1781 		break;
1782 	default:
1783 		ctf_dprintf("ignoring tag type %x\n", tag);
1784 		ret = 0;
1785 		break;
1786 	}
1787 	ctf_dprintf("ctf_dwarf_convert_type tag specific handler returned %d\n",
1788 	    ret);
1789 
1790 	return (ret);
1791 }
1792 
1793 static int
1794 ctf_dwarf_walk_lexical(ctf_cu_t *cup, Dwarf_Die die)
1795 {
1796 	int ret;
1797 	Dwarf_Die child;
1798 
1799 	if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1800 		return (ret);
1801 
1802 	if (child == NULL)
1803 		return (0);
1804 
1805 	return (ctf_dwarf_convert_die(cup, die));
1806 }
1807 
1808 static int
1809 ctf_dwarf_function_count(ctf_cu_t *cup, Dwarf_Die die, ctf_funcinfo_t *fip,
1810     boolean_t fptr)
1811 {
1812 	int ret;
1813 	Dwarf_Die child, sib, arg;
1814 
1815 	if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1816 		return (ret);
1817 
1818 	arg = child;
1819 	while (arg != NULL) {
1820 		Dwarf_Half tag;
1821 
1822 		if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0)
1823 			return (ret);
1824 
1825 		/*
1826 		 * We have to check for a varargs type decleration. This will
1827 		 * happen in one of two ways. If we have a function pointer
1828 		 * type, then it'll be done with a tag of type
1829 		 * DW_TAG_unspecified_parameters. However, it only means we have
1830 		 * a variable number of arguments, if we have more than one
1831 		 * argument found so far. Otherwise, when we have a function
1832 		 * type, it instead uses a formal parameter whose name is '...'
1833 		 * to indicate a variable arguments member.
1834 		 *
1835 		 * Also, if we have a function pointer, then we have to expect
1836 		 * that we might not get a name at all.
1837 		 */
1838 		if (tag == DW_TAG_formal_parameter && fptr == B_FALSE) {
1839 			char *name;
1840 			if ((ret = ctf_dwarf_string(cup, die, DW_AT_name,
1841 			    &name)) != 0)
1842 				return (ret);
1843 			if (strcmp(name, DWARF_VARARGS_NAME) == 0)
1844 				fip->ctc_flags |= CTF_FUNC_VARARG;
1845 			else
1846 				fip->ctc_argc++;
1847 			ctf_free(name, strlen(name) + 1);
1848 		} else if (tag == DW_TAG_formal_parameter) {
1849 			fip->ctc_argc++;
1850 		} else if (tag == DW_TAG_unspecified_parameters &&
1851 		    fip->ctc_argc > 0) {
1852 			fip->ctc_flags |= CTF_FUNC_VARARG;
1853 		}
1854 		if ((ret = ctf_dwarf_sib(cup, arg, &sib)) != 0)
1855 			return (ret);
1856 		arg = sib;
1857 	}
1858 
1859 	return (0);
1860 }
1861 
1862 static int
1863 ctf_dwarf_convert_fargs(ctf_cu_t *cup, Dwarf_Die die, ctf_funcinfo_t *fip,
1864     ctf_id_t *argv)
1865 {
1866 	int ret;
1867 	int i = 0;
1868 	Dwarf_Die child, sib, arg;
1869 
1870 	if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1871 		return (ret);
1872 
1873 	arg = child;
1874 	while (arg != NULL) {
1875 		Dwarf_Half tag;
1876 
1877 		if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0)
1878 			return (ret);
1879 		if (tag == DW_TAG_formal_parameter) {
1880 			Dwarf_Die tdie;
1881 
1882 			if ((ret = ctf_dwarf_refdie(cup, arg, DW_AT_type,
1883 			    &tdie)) != 0)
1884 				return (ret);
1885 
1886 			if ((ret = ctf_dwarf_convert_type(cup, tdie, &argv[i],
1887 			    CTF_ADD_ROOT)) != 0)
1888 				return (ret);
1889 			i++;
1890 
1891 			/*
1892 			 * Once we hit argc entries, we're done. This ensures we
1893 			 * don't accidentally hit a varargs which should be the
1894 			 * last entry.
1895 			 */
1896 			if (i == fip->ctc_argc)
1897 				break;
1898 		}
1899 
1900 		if ((ret = ctf_dwarf_sib(cup, arg, &sib)) != 0)
1901 			return (ret);
1902 		arg = sib;
1903 	}
1904 
1905 	return (0);
1906 }
1907 
1908 static int
1909 ctf_dwarf_convert_function(ctf_cu_t *cup, Dwarf_Die die)
1910 {
1911 	int ret;
1912 	char *name;
1913 	ctf_dwfunc_t *cdf;
1914 	Dwarf_Die tdie;
1915 
1916 	/*
1917 	 * Functions that don't have a name are generally functions that have
1918 	 * been inlined and thus most information about them has been lost. If
1919 	 * we can't get a name, then instead of returning ENOENT, we silently
1920 	 * swallow the error.
1921 	 */
1922 	if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0) {
1923 		if (ret == ENOENT)
1924 			return (0);
1925 		return (ret);
1926 	}
1927 
1928 	ctf_dprintf("beginning work on function %s\n", name);
1929 	if ((cdf = ctf_alloc(sizeof (ctf_dwfunc_t))) == NULL) {
1930 		ctf_free(name, strlen(name) + 1);
1931 		return (ENOMEM);
1932 	}
1933 	bzero(cdf, sizeof (ctf_dwfunc_t));
1934 	cdf->cdf_name = name;
1935 
1936 	if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) == 0) {
1937 		if ((ret = ctf_dwarf_convert_type(cup, tdie,
1938 		    &(cdf->cdf_fip.ctc_return), CTF_ADD_ROOT)) != 0) {
1939 			ctf_free(name, strlen(name) + 1);
1940 			ctf_free(cdf, sizeof (ctf_dwfunc_t));
1941 			return (ret);
1942 		}
1943 	} else if (ret != ENOENT) {
1944 		ctf_free(name, strlen(name) + 1);
1945 		ctf_free(cdf, sizeof (ctf_dwfunc_t));
1946 		return (ret);
1947 	} else {
1948 		if ((cdf->cdf_fip.ctc_return = ctf_dwarf_void(cup)) ==
1949 		    CTF_ERR) {
1950 			ctf_free(name, strlen(name) + 1);
1951 			ctf_free(cdf, sizeof (ctf_dwfunc_t));
1952 			return (ctf_errno(cup->cu_ctfp));
1953 		}
1954 	}
1955 
1956 	/*
1957 	 * A function has a number of children, some of which may not be ones we
1958 	 * care about. Children that we care about have a type of
1959 	 * DW_TAG_formal_parameter. We're going to do two passes, the first to
1960 	 * count the arguments, the second to process them. Afterwards, we
1961 	 * should be good to go ahead and add this function.
1962 	 *
1963 	 * Note, we already got the return type by going in and grabbing it out
1964 	 * of the DW_AT_type.
1965 	 */
1966 	if ((ret = ctf_dwarf_function_count(cup, die, &cdf->cdf_fip,
1967 	    B_FALSE)) != 0) {
1968 		ctf_free(name, strlen(name) + 1);
1969 		ctf_free(cdf, sizeof (ctf_dwfunc_t));
1970 		return (ret);
1971 	}
1972 
1973 	ctf_dprintf("beginning to convert function arguments %s\n", name);
1974 	if (cdf->cdf_fip.ctc_argc != 0) {
1975 		uint_t argc = cdf->cdf_fip.ctc_argc;
1976 		cdf->cdf_argv = ctf_alloc(sizeof (ctf_id_t) * argc);
1977 		if (cdf->cdf_argv == NULL) {
1978 			ctf_free(name, strlen(name) + 1);
1979 			ctf_free(cdf, sizeof (ctf_dwfunc_t));
1980 			return (ENOMEM);
1981 		}
1982 		if ((ret = ctf_dwarf_convert_fargs(cup, die,
1983 		    &cdf->cdf_fip, cdf->cdf_argv)) != 0) {
1984 			ctf_free(cdf->cdf_argv, sizeof (ctf_id_t) * argc);
1985 			ctf_free(name, strlen(name) + 1);
1986 			ctf_free(cdf, sizeof (ctf_dwfunc_t));
1987 			return (ret);
1988 		}
1989 	} else {
1990 		cdf->cdf_argv = NULL;
1991 	}
1992 
1993 	if ((ret = ctf_dwarf_isglobal(cup, die, &cdf->cdf_global)) != 0) {
1994 		ctf_free(cdf->cdf_argv, sizeof (ctf_id_t) *
1995 		    cdf->cdf_fip.ctc_argc);
1996 		ctf_free(name, strlen(name) + 1);
1997 		ctf_free(cdf, sizeof (ctf_dwfunc_t));
1998 		return (ret);
1999 	}
2000 
2001 	ctf_list_append(&cup->cu_funcs, cdf);
2002 	return (ret);
2003 }
2004 
2005 /*
2006  * Convert variables, but only if they're not prototypes and have names.
2007  */
2008 static int
2009 ctf_dwarf_convert_variable(ctf_cu_t *cup, Dwarf_Die die)
2010 {
2011 	int ret;
2012 	char *name;
2013 	Dwarf_Bool b;
2014 	Dwarf_Die tdie;
2015 	ctf_id_t id;
2016 	ctf_dwvar_t *cdv;
2017 
2018 	/* Skip "Non-Defining Declarations" */
2019 	if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_declaration, &b)) == 0) {
2020 		if (b != 0)
2021 			return (0);
2022 	} else if (ret != ENOENT) {
2023 		return (ret);
2024 	}
2025 
2026 	/*
2027 	 * If we find a DIE of "Declarations Completing Non-Defining
2028 	 * Declarations", we will use the referenced type's DIE.  This isn't
2029 	 * quite correct, e.g. DW_AT_decl_line will be the forward declaration
2030 	 * not this site.  It's sufficient for what we need, however: in
2031 	 * particular, we should find DW_AT_external as needed there.
2032 	 */
2033 	if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_specification,
2034 	    &tdie)) == 0) {
2035 		Dwarf_Off offset;
2036 		if ((ret = ctf_dwarf_offset(cup, tdie, &offset)) != 0)
2037 			return (ret);
2038 		ctf_dprintf("die 0x%llx DW_AT_specification -> die 0x%llx\n",
2039 		    ctf_die_offset(die), ctf_die_offset(tdie));
2040 		die = tdie;
2041 	} else if (ret != ENOENT) {
2042 		return (ret);
2043 	}
2044 
2045 	if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
2046 	    ret != ENOENT)
2047 		return (ret);
2048 	if (ret == ENOENT)
2049 		return (0);
2050 
2051 	if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0) {
2052 		ctf_free(name, strlen(name) + 1);
2053 		return (ret);
2054 	}
2055 
2056 	if ((ret = ctf_dwarf_convert_type(cup, tdie, &id,
2057 	    CTF_ADD_ROOT)) != 0)
2058 		return (ret);
2059 
2060 	if ((cdv = ctf_alloc(sizeof (ctf_dwvar_t))) == NULL) {
2061 		ctf_free(name, strlen(name) + 1);
2062 		return (ENOMEM);
2063 	}
2064 
2065 	cdv->cdv_name = name;
2066 	cdv->cdv_type = id;
2067 
2068 	if ((ret = ctf_dwarf_isglobal(cup, die, &cdv->cdv_global)) != 0) {
2069 		ctf_free(cdv, sizeof (ctf_dwvar_t));
2070 		ctf_free(name, strlen(name) + 1);
2071 		return (ret);
2072 	}
2073 
2074 	ctf_list_append(&cup->cu_vars, cdv);
2075 	return (0);
2076 }
2077 
2078 /*
2079  * Walk through our set of top-level types and process them.
2080  */
2081 static int
2082 ctf_dwarf_walk_toplevel(ctf_cu_t *cup, Dwarf_Die die)
2083 {
2084 	int ret;
2085 	Dwarf_Off offset;
2086 	Dwarf_Half tag;
2087 
2088 	if ((ret = ctf_dwarf_offset(cup, die, &offset)) != 0)
2089 		return (ret);
2090 
2091 	if (offset > cup->cu_maxoff) {
2092 		(void) snprintf(cup->cu_errbuf, cup->cu_errlen,
2093 		    "die offset %llu beyond maximum for header %llu\n",
2094 		    offset, cup->cu_maxoff);
2095 		return (ECTF_CONVBKERR);
2096 	}
2097 
2098 	if ((ret = ctf_dwarf_tag(cup, die, &tag)) != 0)
2099 		return (ret);
2100 
2101 	ret = 0;
2102 	switch (tag) {
2103 	case DW_TAG_subprogram:
2104 		ctf_dprintf("top level func\n");
2105 		ret = ctf_dwarf_convert_function(cup, die);
2106 		break;
2107 	case DW_TAG_variable:
2108 		ctf_dprintf("top level var\n");
2109 		ret = ctf_dwarf_convert_variable(cup, die);
2110 		break;
2111 	case DW_TAG_lexical_block:
2112 		ctf_dprintf("top level block\n");
2113 		ret = ctf_dwarf_walk_lexical(cup, die);
2114 		break;
2115 	case DW_TAG_enumeration_type:
2116 	case DW_TAG_structure_type:
2117 	case DW_TAG_typedef:
2118 	case DW_TAG_union_type:
2119 		ctf_dprintf("top level type\n");
2120 		ret = ctf_dwarf_convert_type(cup, die, NULL, B_TRUE);
2121 		break;
2122 	default:
2123 		break;
2124 	}
2125 
2126 	return (ret);
2127 }
2128 
2129 
2130 /*
2131  * We're given a node. At this node we need to convert it and then proceed to
2132  * convert any siblings that are associaed with this die.
2133  */
2134 static int
2135 ctf_dwarf_convert_die(ctf_cu_t *cup, Dwarf_Die die)
2136 {
2137 	while (die != NULL) {
2138 		int ret;
2139 		Dwarf_Die sib;
2140 
2141 		if ((ret = ctf_dwarf_walk_toplevel(cup, die)) != 0)
2142 			return (ret);
2143 
2144 		if ((ret = ctf_dwarf_sib(cup, die, &sib)) != 0)
2145 			return (ret);
2146 		die = sib;
2147 	}
2148 	return (0);
2149 }
2150 
2151 static int
2152 ctf_dwarf_fixup_die(ctf_cu_t *cup, boolean_t addpass)
2153 {
2154 	ctf_dwmap_t *map;
2155 
2156 	for (map = avl_first(&cup->cu_map); map != NULL;
2157 	    map = AVL_NEXT(&cup->cu_map, map)) {
2158 		int ret;
2159 		if (map->cdm_fix == B_FALSE)
2160 			continue;
2161 		if ((ret = ctf_dwarf_fixup_sou(cup, map->cdm_die, map->cdm_id,
2162 		    addpass)) != 0)
2163 			return (ret);
2164 	}
2165 
2166 	return (0);
2167 }
2168 
2169 static ctf_dwfunc_t *
2170 ctf_dwarf_match_func(ctf_cu_t *cup, const char *file, const char *name,
2171     int bind)
2172 {
2173 	ctf_dwfunc_t *cdf;
2174 
2175 	if (bind == STB_WEAK)
2176 		return (NULL);
2177 
2178 	/* Nothing we can do if we can't find a name to compare it to. */
2179 	if (bind == STB_LOCAL && (file == NULL || cup->cu_name == NULL))
2180 		return (NULL);
2181 
2182 	for (cdf = ctf_list_next(&cup->cu_funcs); cdf != NULL;
2183 	    cdf = ctf_list_next(cdf)) {
2184 		if (bind == STB_GLOBAL && cdf->cdf_global == B_FALSE)
2185 			continue;
2186 		if (bind == STB_LOCAL && cdf->cdf_global == B_TRUE)
2187 			continue;
2188 		if (strcmp(name, cdf->cdf_name) != 0)
2189 			continue;
2190 		if (bind == STB_LOCAL && strcmp(file, cup->cu_name) != 0)
2191 			continue;
2192 		return (cdf);
2193 	}
2194 
2195 	return (NULL);
2196 }
2197 static ctf_dwvar_t *
2198 ctf_dwarf_match_var(ctf_cu_t *cup, const char *file, const char *name,
2199     int bind)
2200 {
2201 	ctf_dwvar_t *cdv;
2202 
2203 	/* Nothing we can do if we can't find a name to compare it to. */
2204 	if (bind == STB_LOCAL && (file == NULL || cup->cu_name == NULL))
2205 		return (NULL);
2206 	ctf_dprintf("Still considering %s\n", name);
2207 
2208 	for (cdv = ctf_list_next(&cup->cu_vars); cdv != NULL;
2209 	    cdv = ctf_list_next(cdv)) {
2210 		if (bind == STB_GLOBAL && cdv->cdv_global == B_FALSE)
2211 			continue;
2212 		if (bind == STB_LOCAL && cdv->cdv_global == B_TRUE)
2213 			continue;
2214 		if (strcmp(name, cdv->cdv_name) != 0)
2215 			continue;
2216 		if (bind == STB_LOCAL && strcmp(file, cup->cu_name) != 0)
2217 			continue;
2218 		return (cdv);
2219 	}
2220 
2221 	return (NULL);
2222 }
2223 
2224 static int
2225 ctf_dwarf_symtab_iter(ctf_cu_t *cup, ctf_dwarf_symtab_f *func, void *arg)
2226 {
2227 	int ret;
2228 	ulong_t i;
2229 	ctf_file_t *fp = cup->cu_ctfp;
2230 	const char *file = NULL;
2231 	uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
2232 	uintptr_t strbase = (uintptr_t)fp->ctf_strtab.cts_data;
2233 
2234 	for (i = 0; i < fp->ctf_nsyms; i++) {
2235 		const char *name;
2236 		int type;
2237 		GElf_Sym gsym;
2238 		const GElf_Sym *gsymp;
2239 
2240 		if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
2241 			const Elf32_Sym *symp = (Elf32_Sym *)symbase + i;
2242 			type = ELF32_ST_TYPE(symp->st_info);
2243 			if (type == STT_FILE) {
2244 				file = (char *)(strbase + symp->st_name);
2245 				continue;
2246 			}
2247 			if (type != STT_OBJECT && type != STT_FUNC)
2248 				continue;
2249 			if (ctf_sym_valid(strbase, type, symp->st_shndx,
2250 			    symp->st_value, symp->st_name) == B_FALSE)
2251 				continue;
2252 			name = (char *)(strbase + symp->st_name);
2253 			gsym.st_name = symp->st_name;
2254 			gsym.st_value = symp->st_value;
2255 			gsym.st_size = symp->st_size;
2256 			gsym.st_info = symp->st_info;
2257 			gsym.st_other = symp->st_other;
2258 			gsym.st_shndx = symp->st_shndx;
2259 			gsymp = &gsym;
2260 		} else {
2261 			const Elf64_Sym *symp = (Elf64_Sym *)symbase + i;
2262 			type = ELF64_ST_TYPE(symp->st_info);
2263 			if (type == STT_FILE) {
2264 				file = (char *)(strbase + symp->st_name);
2265 				continue;
2266 			}
2267 			if (type != STT_OBJECT && type != STT_FUNC)
2268 				continue;
2269 			if (ctf_sym_valid(strbase, type, symp->st_shndx,
2270 			    symp->st_value, symp->st_name) == B_FALSE)
2271 				continue;
2272 			name = (char *)(strbase + symp->st_name);
2273 			gsymp = symp;
2274 		}
2275 
2276 		ret = func(cup, gsymp, i, file, name, arg);
2277 		if (ret != 0)
2278 			return (ret);
2279 	}
2280 
2281 	return (0);
2282 }
2283 
2284 static int
2285 ctf_dwarf_conv_funcvars_cb(ctf_cu_t *cup, const GElf_Sym *symp, ulong_t idx,
2286     const char *file, const char *name, void *arg)
2287 {
2288 	int ret, bind, type;
2289 
2290 	bind = GELF_ST_BIND(symp->st_info);
2291 	type = GELF_ST_TYPE(symp->st_info);
2292 
2293 	/*
2294 	 * Come back to weak symbols in another pass
2295 	 */
2296 	if (bind == STB_WEAK)
2297 		return (0);
2298 
2299 	if (type == STT_OBJECT) {
2300 		ctf_dwvar_t *cdv = ctf_dwarf_match_var(cup, file, name,
2301 		    bind);
2302 		ctf_dprintf("match for %s (%d): %p\n", name, idx, cdv);
2303 		if (cdv == NULL)
2304 			return (0);
2305 		ret = ctf_add_object(cup->cu_ctfp, idx, cdv->cdv_type);
2306 		ctf_dprintf("added object %s\n", name);
2307 	} else {
2308 		ctf_dwfunc_t *cdf = ctf_dwarf_match_func(cup, file, name,
2309 		    bind);
2310 		if (cdf == NULL)
2311 			return (0);
2312 		ret = ctf_add_function(cup->cu_ctfp, idx, &cdf->cdf_fip,
2313 		    cdf->cdf_argv);
2314 	}
2315 
2316 	if (ret == CTF_ERR) {
2317 		return (ctf_errno(cup->cu_ctfp));
2318 	}
2319 
2320 	return (0);
2321 }
2322 
2323 static int
2324 ctf_dwarf_conv_funcvars(ctf_cu_t *cup)
2325 {
2326 	return (ctf_dwarf_symtab_iter(cup, ctf_dwarf_conv_funcvars_cb, NULL));
2327 }
2328 
2329 /*
2330  * If we have a weak symbol, attempt to find the strong symbol it will resolve
2331  * to.  Note: the code where this actually happens is in sym_process() in
2332  * cmd/sgs/libld/common/syms.c
2333  *
2334  * Finding the matching symbol is unfortunately not trivial.  For a symbol to be
2335  * a candidate, it must:
2336  *
2337  * - have the same type (function, object)
2338  * - have the same value (address)
2339  * - have the same size
2340  * - not be another weak symbol
2341  * - belong to the same section (checked via section index)
2342  *
2343  * To perform this check, we first iterate over the symbol table. For each weak
2344  * symbol that we encounter, we then do a second walk over the symbol table,
2345  * calling ctf_dwarf_conv_check_weak(). If a symbol matches the above, then it's
2346  * either a local or global symbol. If we find a global symbol then we go with
2347  * it and stop searching for additional matches.
2348  *
2349  * If instead, we find a local symbol, things are more complicated. The first
2350  * thing we do is to try and see if we have file information about both symbols
2351  * (STT_FILE). If they both have file information and it matches, then we treat
2352  * that as a good match and stop searching for additional matches.
2353  *
2354  * Otherwise, this means we have a non-matching file and a local symbol. We
2355  * treat this as a candidate and if we find a better match (one of the two cases
2356  * above), use that instead. There are two different ways this can happen.
2357  * Either this is a completely different symbol, or it's a once-global symbol
2358  * that was scoped to local via a mapfile.  In the former case, curfile is
2359  * likely inaccurate since the linker does not preserve the needed curfile in
2360  * the order of the symbol table (see the comments about locally scoped symbols
2361  * in libld's update_osym()).  As we can't tell this case from the former one,
2362  * we use this symbol iff no other matching symbol is found.
2363  *
2364  * What we really need here is a SUNW section containing weak<->strong mappings
2365  * that we can consume.
2366  */
2367 typedef struct ctf_dwarf_weak_arg {
2368 	const GElf_Sym *cweak_symp;
2369 	const char *cweak_file;
2370 	boolean_t cweak_candidate;
2371 	ulong_t cweak_idx;
2372 } ctf_dwarf_weak_arg_t;
2373 
2374 static int
2375 ctf_dwarf_conv_check_weak(ctf_cu_t *cup, const GElf_Sym *symp,
2376     ulong_t idx, const char *file, const char *name, void *arg)
2377 {
2378 	ctf_dwarf_weak_arg_t *cweak = arg;
2379 	const GElf_Sym *wsymp = cweak->cweak_symp;
2380 
2381 	ctf_dprintf("comparing weak to %s\n", name);
2382 
2383 	if (GELF_ST_BIND(symp->st_info) == STB_WEAK) {
2384 		return (0);
2385 	}
2386 
2387 	if (GELF_ST_TYPE(wsymp->st_info) != GELF_ST_TYPE(symp->st_info)) {
2388 		return (0);
2389 	}
2390 
2391 	if (wsymp->st_value != symp->st_value) {
2392 		return (0);
2393 	}
2394 
2395 	if (wsymp->st_size != symp->st_size) {
2396 		return (0);
2397 	}
2398 
2399 	if (wsymp->st_shndx != symp->st_shndx) {
2400 		return (0);
2401 	}
2402 
2403 	/*
2404 	 * Check if it's a weak candidate.
2405 	 */
2406 	if (GELF_ST_BIND(symp->st_info) == STB_LOCAL &&
2407 	    (file == NULL || cweak->cweak_file == NULL ||
2408 	    strcmp(file, cweak->cweak_file) != 0)) {
2409 		cweak->cweak_candidate = B_TRUE;
2410 		cweak->cweak_idx = idx;
2411 		return (0);
2412 	}
2413 
2414 	/*
2415 	 * Found a match, break.
2416 	 */
2417 	cweak->cweak_idx = idx;
2418 	return (1);
2419 }
2420 
2421 static int
2422 ctf_dwarf_duplicate_sym(ctf_cu_t *cup, ulong_t idx, ulong_t matchidx)
2423 {
2424 	ctf_id_t id = ctf_lookup_by_symbol(cup->cu_ctfp, matchidx);
2425 
2426 	/*
2427 	 * If we matched something that for some reason didn't have type data,
2428 	 * we don't consider that a fatal error and silently swallow it.
2429 	 */
2430 	if (id == CTF_ERR) {
2431 		if (ctf_errno(cup->cu_ctfp) == ECTF_NOTYPEDAT)
2432 			return (0);
2433 		else
2434 			return (ctf_errno(cup->cu_ctfp));
2435 	}
2436 
2437 	if (ctf_add_object(cup->cu_ctfp, idx, id) == CTF_ERR)
2438 		return (ctf_errno(cup->cu_ctfp));
2439 
2440 	return (0);
2441 }
2442 
2443 static int
2444 ctf_dwarf_duplicate_func(ctf_cu_t *cup, ulong_t idx, ulong_t matchidx)
2445 {
2446 	int ret;
2447 	ctf_funcinfo_t fip;
2448 	ctf_id_t *args = NULL;
2449 
2450 	if (ctf_func_info(cup->cu_ctfp, matchidx, &fip) == CTF_ERR) {
2451 		if (ctf_errno(cup->cu_ctfp) == ECTF_NOFUNCDAT)
2452 			return (0);
2453 		else
2454 			return (ctf_errno(cup->cu_ctfp));
2455 	}
2456 
2457 	if (fip.ctc_argc != 0) {
2458 		args = ctf_alloc(sizeof (ctf_id_t) * fip.ctc_argc);
2459 		if (args == NULL)
2460 			return (ENOMEM);
2461 
2462 		if (ctf_func_args(cup->cu_ctfp, matchidx, fip.ctc_argc, args) ==
2463 		    CTF_ERR) {
2464 			ctf_free(args, sizeof (ctf_id_t) * fip.ctc_argc);
2465 			return (ctf_errno(cup->cu_ctfp));
2466 		}
2467 	}
2468 
2469 	ret = ctf_add_function(cup->cu_ctfp, idx, &fip, args);
2470 	if (args != NULL)
2471 		ctf_free(args, sizeof (ctf_id_t) * fip.ctc_argc);
2472 	if (ret == CTF_ERR)
2473 		return (ctf_errno(cup->cu_ctfp));
2474 
2475 	return (0);
2476 }
2477 
2478 static int
2479 ctf_dwarf_conv_weaks_cb(ctf_cu_t *cup, const GElf_Sym *symp,
2480     ulong_t idx, const char *file, const char *name, void *arg)
2481 {
2482 	int ret, type;
2483 	ctf_dwarf_weak_arg_t cweak;
2484 
2485 	/*
2486 	 * We only care about weak symbols.
2487 	 */
2488 	if (GELF_ST_BIND(symp->st_info) != STB_WEAK)
2489 		return (0);
2490 
2491 	type = GELF_ST_TYPE(symp->st_info);
2492 	ASSERT(type == STT_OBJECT || type == STT_FUNC);
2493 
2494 	/*
2495 	 * For each weak symbol we encounter, we need to do a second iteration
2496 	 * to try and find a match. We should probably think about other
2497 	 * techniques to try and save us time in the future.
2498 	 */
2499 	cweak.cweak_symp = symp;
2500 	cweak.cweak_file = file;
2501 	cweak.cweak_candidate = B_FALSE;
2502 	cweak.cweak_idx = 0;
2503 
2504 	ctf_dprintf("Trying to find weak equiv for %s\n", name);
2505 
2506 	ret = ctf_dwarf_symtab_iter(cup, ctf_dwarf_conv_check_weak, &cweak);
2507 	VERIFY(ret == 0 || ret == 1);
2508 
2509 	/*
2510 	 * Nothing was ever found, we're not going to add anything for this
2511 	 * entry.
2512 	 */
2513 	if (ret == 0 && cweak.cweak_candidate == B_FALSE) {
2514 		ctf_dprintf("found no weak match for %s\n", name);
2515 		return (0);
2516 	}
2517 
2518 	/*
2519 	 * Now, finally go and add the type based on the match.
2520 	 */
2521 	if (type == STT_OBJECT) {
2522 		ret = ctf_dwarf_duplicate_sym(cup, idx, cweak.cweak_idx);
2523 	} else {
2524 		ret = ctf_dwarf_duplicate_func(cup, idx, cweak.cweak_idx);
2525 	}
2526 
2527 	return (ret);
2528 }
2529 
2530 static int
2531 ctf_dwarf_conv_weaks(ctf_cu_t *cup)
2532 {
2533 	return (ctf_dwarf_symtab_iter(cup, ctf_dwarf_conv_weaks_cb, NULL));
2534 }
2535 
2536 /* ARGSUSED */
2537 static int
2538 ctf_dwarf_convert_one(void *arg, void *unused)
2539 {
2540 	int ret;
2541 	ctf_file_t *dedup;
2542 	ctf_cu_t *cup = arg;
2543 
2544 	ctf_dprintf("converting die: %s\n", cup->cu_name);
2545 	ctf_dprintf("max offset: %x\n", cup->cu_maxoff);
2546 	VERIFY(cup != NULL);
2547 
2548 	ret = ctf_dwarf_convert_die(cup, cup->cu_cu);
2549 	ctf_dprintf("ctf_dwarf_convert_die (%s) returned %d\n", cup->cu_name,
2550 	    ret);
2551 	if (ret != 0) {
2552 		return (ret);
2553 	}
2554 	if (ctf_update(cup->cu_ctfp) != 0) {
2555 		return (ctf_dwarf_error(cup, cup->cu_ctfp, 0,
2556 		    "failed to update output ctf container"));
2557 	}
2558 
2559 	ret = ctf_dwarf_fixup_die(cup, B_FALSE);
2560 	ctf_dprintf("ctf_dwarf_fixup_die (%s) returned %d\n", cup->cu_name,
2561 	    ret);
2562 	if (ret != 0) {
2563 		return (ret);
2564 	}
2565 	if (ctf_update(cup->cu_ctfp) != 0) {
2566 		return (ctf_dwarf_error(cup, cup->cu_ctfp, 0,
2567 		    "failed to update output ctf container"));
2568 	}
2569 
2570 	ret = ctf_dwarf_fixup_die(cup, B_TRUE);
2571 	ctf_dprintf("ctf_dwarf_fixup_die (%s) returned %d\n", cup->cu_name,
2572 	    ret);
2573 	if (ret != 0) {
2574 		return (ret);
2575 	}
2576 	if (ctf_update(cup->cu_ctfp) != 0) {
2577 		return (ctf_dwarf_error(cup, cup->cu_ctfp, 0,
2578 		    "failed to update output ctf container"));
2579 	}
2580 
2581 
2582 	if ((ret = ctf_dwarf_conv_funcvars(cup)) != 0) {
2583 		return (ctf_dwarf_error(cup, NULL, ret,
2584 		    "failed to convert strong functions and variables"));
2585 	}
2586 
2587 	if (ctf_update(cup->cu_ctfp) != 0) {
2588 		return (ctf_dwarf_error(cup, cup->cu_ctfp, 0,
2589 		    "failed to update output ctf container"));
2590 	}
2591 
2592 	if (cup->cu_doweaks == B_TRUE) {
2593 		if ((ret = ctf_dwarf_conv_weaks(cup)) != 0) {
2594 			return (ctf_dwarf_error(cup, NULL, ret,
2595 			    "failed to convert weak functions and variables"));
2596 		}
2597 
2598 		if (ctf_update(cup->cu_ctfp) != 0) {
2599 			return (ctf_dwarf_error(cup, cup->cu_ctfp, 0,
2600 			    "failed to update output ctf container"));
2601 		}
2602 	}
2603 
2604 	ctf_phase_dump(cup->cu_ctfp, "pre-dedup");
2605 	ctf_dprintf("adding inputs for dedup\n");
2606 	if ((ret = ctf_merge_add(cup->cu_cmh, cup->cu_ctfp)) != 0) {
2607 		return (ctf_dwarf_error(cup, NULL, ret,
2608 		    "failed to add inputs for merge"));
2609 	}
2610 
2611 	ctf_dprintf("starting merge\n");
2612 	if ((ret = ctf_merge_dedup(cup->cu_cmh, &dedup)) != 0) {
2613 		return (ctf_dwarf_error(cup, NULL, ret,
2614 		    "failed to deduplicate die"));
2615 	}
2616 	ctf_close(cup->cu_ctfp);
2617 	cup->cu_ctfp = dedup;
2618 
2619 	return (0);
2620 }
2621 
2622 /*
2623  * Note, we expect that if we're returning a ctf_file_t from one of the dies,
2624  * say in the single node case, it's been saved and the entry here has been set
2625  * to NULL, which ctf_close happily ignores.
2626  */
2627 static void
2628 ctf_dwarf_free_die(ctf_cu_t *cup)
2629 {
2630 	ctf_dwfunc_t *cdf, *ndf;
2631 	ctf_dwvar_t *cdv, *ndv;
2632 	ctf_dwbitf_t *cdb, *ndb;
2633 	ctf_dwmap_t *map;
2634 	void *cookie;
2635 	Dwarf_Error derr;
2636 
2637 	ctf_dprintf("Beginning to free die: %p\n", cup);
2638 	cup->cu_elf = NULL;
2639 	ctf_dprintf("Trying to free name: %p\n", cup->cu_name);
2640 	if (cup->cu_name != NULL)
2641 		ctf_free(cup->cu_name, strlen(cup->cu_name) + 1);
2642 	ctf_dprintf("Trying to free merge handle: %p\n", cup->cu_cmh);
2643 	if (cup->cu_cmh != NULL) {
2644 		ctf_merge_fini(cup->cu_cmh);
2645 		cup->cu_cmh = NULL;
2646 	}
2647 
2648 	ctf_dprintf("Trying to free functions\n");
2649 	for (cdf = ctf_list_next(&cup->cu_funcs); cdf != NULL; cdf = ndf) {
2650 		ndf = ctf_list_next(cdf);
2651 		ctf_free(cdf->cdf_name, strlen(cdf->cdf_name) + 1);
2652 		if (cdf->cdf_fip.ctc_argc != 0) {
2653 			ctf_free(cdf->cdf_argv,
2654 			    sizeof (ctf_id_t) * cdf->cdf_fip.ctc_argc);
2655 		}
2656 		ctf_free(cdf, sizeof (ctf_dwfunc_t));
2657 	}
2658 
2659 	ctf_dprintf("Trying to free variables\n");
2660 	for (cdv = ctf_list_next(&cup->cu_vars); cdv != NULL; cdv = ndv) {
2661 		ndv = ctf_list_next(cdv);
2662 		ctf_free(cdv->cdv_name, strlen(cdv->cdv_name) + 1);
2663 		ctf_free(cdv, sizeof (ctf_dwvar_t));
2664 	}
2665 
2666 	ctf_dprintf("Trying to free bitfields\n");
2667 	for (cdb = ctf_list_next(&cup->cu_bitfields); cdb != NULL; cdb = ndb) {
2668 		ndb = ctf_list_next(cdb);
2669 		ctf_free(cdb, sizeof (ctf_dwbitf_t));
2670 	}
2671 
2672 	ctf_dprintf("Trying to clean up dwarf_t: %p\n", cup->cu_dwarf);
2673 	(void) dwarf_finish(cup->cu_dwarf, &derr);
2674 	cup->cu_dwarf = NULL;
2675 	ctf_close(cup->cu_ctfp);
2676 
2677 	cookie = NULL;
2678 	while ((map = avl_destroy_nodes(&cup->cu_map, &cookie)) != NULL) {
2679 		ctf_free(map, sizeof (ctf_dwmap_t));
2680 	}
2681 	avl_destroy(&cup->cu_map);
2682 	cup->cu_errbuf = NULL;
2683 }
2684 
2685 static void
2686 ctf_dwarf_free_dies(ctf_cu_t *cdies, int ndies)
2687 {
2688 	int i;
2689 
2690 	ctf_dprintf("Beginning to free dies\n");
2691 	for (i = 0; i < ndies; i++) {
2692 		ctf_dwarf_free_die(&cdies[i]);
2693 	}
2694 
2695 	ctf_free(cdies, sizeof (ctf_cu_t) * ndies);
2696 }
2697 
2698 static int
2699 ctf_dwarf_count_dies(Dwarf_Debug dw, Dwarf_Error *derr, int *ndies,
2700     char *errbuf, size_t errlen)
2701 {
2702 	int ret;
2703 	Dwarf_Half vers;
2704 	Dwarf_Unsigned nexthdr;
2705 
2706 	while ((ret = dwarf_next_cu_header(dw, NULL, &vers, NULL, NULL,
2707 	    &nexthdr, derr)) != DW_DLV_NO_ENTRY) {
2708 		if (ret != DW_DLV_OK) {
2709 			(void) snprintf(errbuf, errlen,
2710 			    "file does not contain valid DWARF data: %s\n",
2711 			    dwarf_errmsg(*derr));
2712 			return (ECTF_CONVBKERR);
2713 		}
2714 
2715 		if (vers != DWARF_VERSION_TWO) {
2716 			(void) snprintf(errbuf, errlen,
2717 			    "unsupported DWARF version: %d\n", vers);
2718 			return (ECTF_CONVBKERR);
2719 		}
2720 		*ndies = *ndies + 1;
2721 	}
2722 
2723 	if (*ndies == 0) {
2724 		(void) snprintf(errbuf, errlen,
2725 		    "file does not contain valid DWARF data: %s\n",
2726 		    dwarf_errmsg(*derr));
2727 		return (ECTF_CONVBKERR);
2728 	}
2729 
2730 	return (0);
2731 }
2732 
2733 static int
2734 ctf_dwarf_init_die(int fd, Elf *elf, ctf_cu_t *cup, int ndie, char *errbuf,
2735     size_t errlen)
2736 {
2737 	int ret;
2738 	Dwarf_Unsigned hdrlen, abboff, nexthdr;
2739 	Dwarf_Half addrsz;
2740 	Dwarf_Unsigned offset = 0;
2741 	Dwarf_Error derr;
2742 
2743 	while ((ret = dwarf_next_cu_header(cup->cu_dwarf, &hdrlen, NULL,
2744 	    &abboff, &addrsz, &nexthdr, &derr)) != DW_DLV_NO_ENTRY) {
2745 		char *name;
2746 		Dwarf_Die cu, child;
2747 
2748 		/* Based on the counting above, we should be good to go */
2749 		VERIFY(ret == DW_DLV_OK);
2750 		if (ndie > 0) {
2751 			ndie--;
2752 			offset = nexthdr;
2753 			continue;
2754 		}
2755 
2756 		/*
2757 		 * Compilers are apparently inconsistent. Some emit no DWARF for
2758 		 * empty files and others emit empty compilation unit.
2759 		 */
2760 		cup->cu_voidtid = CTF_ERR;
2761 		cup->cu_longtid = CTF_ERR;
2762 		cup->cu_elf = elf;
2763 		cup->cu_maxoff = nexthdr - 1;
2764 		cup->cu_ctfp = ctf_fdcreate(fd, &ret);
2765 		if (cup->cu_ctfp == NULL) {
2766 			ctf_free(cup, sizeof (ctf_cu_t));
2767 			return (ret);
2768 		}
2769 		avl_create(&cup->cu_map, ctf_dwmap_comp, sizeof (ctf_dwmap_t),
2770 		    offsetof(ctf_dwmap_t, cdm_avl));
2771 		cup->cu_errbuf = errbuf;
2772 		cup->cu_errlen = errlen;
2773 		bzero(&cup->cu_vars, sizeof (ctf_list_t));
2774 		bzero(&cup->cu_funcs, sizeof (ctf_list_t));
2775 		bzero(&cup->cu_bitfields, sizeof (ctf_list_t));
2776 
2777 		if ((ret = ctf_dwarf_die_elfenc(elf, cup, errbuf,
2778 		    errlen)) != 0) {
2779 			avl_destroy(&cup->cu_map);
2780 			ctf_free(cup, sizeof (ctf_cu_t));
2781 			return (ret);
2782 		}
2783 
2784 		if ((ret = ctf_dwarf_sib(cup, NULL, &cu)) != 0) {
2785 			avl_destroy(&cup->cu_map);
2786 			ctf_free(cup, sizeof (ctf_cu_t));
2787 			return (ret);
2788 		}
2789 		if (cu == NULL) {
2790 			(void) snprintf(errbuf, errlen,
2791 			    "file does not contain DWARF data\n");
2792 			avl_destroy(&cup->cu_map);
2793 			ctf_free(cup, sizeof (ctf_cu_t));
2794 			return (ECTF_CONVBKERR);
2795 		}
2796 
2797 		if ((ret = ctf_dwarf_child(cup, cu, &child)) != 0) {
2798 			avl_destroy(&cup->cu_map);
2799 			ctf_free(cup, sizeof (ctf_cu_t));
2800 			return (ret);
2801 		}
2802 		if (child == NULL) {
2803 			(void) snprintf(errbuf, errlen,
2804 			    "file does not contain DWARF data\n");
2805 			avl_destroy(&cup->cu_map);
2806 			ctf_free(cup, sizeof (ctf_cu_t));
2807 			return (ECTF_CONVBKERR);
2808 		}
2809 
2810 		cup->cu_cuoff = offset;
2811 		cup->cu_cu = child;
2812 
2813 		if ((cup->cu_cmh = ctf_merge_init(fd, &ret)) == NULL) {
2814 			avl_destroy(&cup->cu_map);
2815 			ctf_free(cup, sizeof (ctf_cu_t));
2816 			return (ret);
2817 		}
2818 
2819 		if (ctf_dwarf_string(cup, cu, DW_AT_name, &name) == 0) {
2820 			size_t len = strlen(name) + 1;
2821 			char *b = basename(name);
2822 			cup->cu_name = strdup(b);
2823 			ctf_free(name, len);
2824 		}
2825 		break;
2826 	}
2827 
2828 	return (0);
2829 }
2830 
2831 
2832 ctf_conv_status_t
2833 ctf_dwarf_convert(int fd, Elf *elf, uint_t nthrs, int *errp, ctf_file_t **fpp,
2834     char *errmsg, size_t errlen)
2835 {
2836 	int err, ret, ndies, i;
2837 	Dwarf_Debug dw;
2838 	Dwarf_Error derr;
2839 	ctf_cu_t *cdies = NULL, *cup;
2840 	workq_t *wqp = NULL;
2841 
2842 	if (errp == NULL)
2843 		errp = &err;
2844 	*errp = 0;
2845 	*fpp = NULL;
2846 
2847 	ret = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dw, &derr);
2848 	if (ret != DW_DLV_OK) {
2849 		/*
2850 		 * We may want to expect DWARF data here and fail conversion if
2851 		 * it's missing. In this case, if we actually have some amount
2852 		 * of DWARF, but no section, for now, just go ahead and create
2853 		 * an empty CTF file.
2854 		 */
2855 		if (ret == DW_DLV_NO_ENTRY ||
2856 		    dwarf_errno(derr) == DW_DLE_DEBUG_INFO_NULL) {
2857 			*fpp = ctf_create(errp);
2858 			return (*fpp != NULL ? CTF_CONV_SUCCESS :
2859 			    CTF_CONV_ERROR);
2860 		}
2861 		(void) snprintf(errmsg, errlen,
2862 		    "failed to initialize DWARF: %s\n",
2863 		    dwarf_errmsg(derr));
2864 		*errp = ECTF_CONVBKERR;
2865 		return (CTF_CONV_ERROR);
2866 	}
2867 
2868 	/*
2869 	 * Iterate over all of the compilation units and create a ctf_cu_t for
2870 	 * each of them.  This is used to determine if we have zero, one, or
2871 	 * multiple dies to convert. If we have zero, that's an error. If
2872 	 * there's only one die, that's the simple case.  No merge needed and
2873 	 * only a single Dwarf_Debug as well.
2874 	 */
2875 	ndies = 0;
2876 	ret = ctf_dwarf_count_dies(dw, &derr, &ndies, errmsg, errlen);
2877 	if (ret != 0) {
2878 		*errp = ret;
2879 		goto out;
2880 	}
2881 
2882 	(void) dwarf_finish(dw, &derr);
2883 	cdies = ctf_alloc(sizeof (ctf_cu_t) * ndies);
2884 	if (cdies == NULL) {
2885 		*errp = ENOMEM;
2886 		return (CTF_CONV_ERROR);
2887 	}
2888 
2889 	for (i = 0; i < ndies; i++) {
2890 		cup = &cdies[i];
2891 		ret = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL,
2892 		    &cup->cu_dwarf, &derr);
2893 		if (ret != 0) {
2894 			ctf_free(cdies, sizeof (ctf_cu_t) * ndies);
2895 			(void) snprintf(errmsg, errlen,
2896 			    "failed to initialize DWARF: %s\n",
2897 			    dwarf_errmsg(derr));
2898 			*errp = ECTF_CONVBKERR;
2899 			return (CTF_CONV_ERROR);
2900 		}
2901 
2902 		ret = ctf_dwarf_init_die(fd, elf, &cdies[i], i, errmsg, errlen);
2903 		if (ret != 0) {
2904 			*errp = ret;
2905 			goto out;
2906 		}
2907 		cup->cu_doweaks = ndies > 1 ? B_FALSE : B_TRUE;
2908 	}
2909 
2910 	ctf_dprintf("found %d DWARF die(s)\n", ndies);
2911 
2912 	/*
2913 	 * If we only have one compilation unit, there's no reason to use
2914 	 * multiple threads, even if the user requested them. After all, they
2915 	 * just gave us an upper bound.
2916 	 */
2917 	if (ndies == 1)
2918 		nthrs = 1;
2919 
2920 	if (workq_init(&wqp, nthrs) == -1) {
2921 		*errp = errno;
2922 		goto out;
2923 	}
2924 
2925 	for (i = 0; i < ndies; i++) {
2926 		cup = &cdies[i];
2927 		ctf_dprintf("adding die %s: %p, %x %x\n", cup->cu_name,
2928 		    cup->cu_cu, cup->cu_cuoff, cup->cu_maxoff);
2929 		if (workq_add(wqp, cup) == -1) {
2930 			*errp = errno;
2931 			goto out;
2932 		}
2933 	}
2934 
2935 	ret = workq_work(wqp, ctf_dwarf_convert_one, NULL, errp);
2936 	if (ret == WORKQ_ERROR) {
2937 		*errp = errno;
2938 		goto out;
2939 	} else if (ret == WORKQ_UERROR) {
2940 		ctf_dprintf("internal convert failed: %s\n",
2941 		    ctf_errmsg(*errp));
2942 		goto out;
2943 	}
2944 
2945 	ctf_dprintf("Determining next phase: have %d dies\n", ndies);
2946 	if (ndies != 1) {
2947 		ctf_merge_t *cmp;
2948 
2949 		cmp = ctf_merge_init(fd, &ret);
2950 		if (cmp == NULL) {
2951 			*errp = ret;
2952 			goto out;
2953 		}
2954 
2955 		ctf_dprintf("setting threads\n");
2956 		if ((ret = ctf_merge_set_nthreads(cmp, nthrs)) != 0) {
2957 			ctf_merge_fini(cmp);
2958 			*errp = ret;
2959 			goto out;
2960 		}
2961 
2962 		ctf_dprintf("adding dies\n");
2963 		for (i = 0; i < ndies; i++) {
2964 			cup = &cdies[i];
2965 			if ((ret = ctf_merge_add(cmp, cup->cu_ctfp)) != 0) {
2966 				ctf_merge_fini(cmp);
2967 				*errp = ret;
2968 				goto out;
2969 			}
2970 		}
2971 
2972 		ctf_dprintf("performing merge\n");
2973 		ret = ctf_merge_merge(cmp, fpp);
2974 		if (ret != 0) {
2975 			ctf_dprintf("failed merge!\n");
2976 			*fpp = NULL;
2977 			ctf_merge_fini(cmp);
2978 			*errp = ret;
2979 			goto out;
2980 		}
2981 		ctf_merge_fini(cmp);
2982 		*errp = 0;
2983 		ctf_dprintf("successfully converted!\n");
2984 	} else {
2985 		*errp = 0;
2986 		*fpp = cdies->cu_ctfp;
2987 		cdies->cu_ctfp = NULL;
2988 		ctf_dprintf("successfully converted!\n");
2989 	}
2990 
2991 out:
2992 	workq_fini(wqp);
2993 	ctf_dwarf_free_dies(cdies, ndies);
2994 	return (*fpp != NULL ? CTF_CONV_SUCCESS : CTF_CONV_ERROR);
2995 }
2996