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