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