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