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