4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright 2012 Jason King. All rights reserved.
27 * Use is subject to license terms.
31 * Copyright 2015 Joyent, Inc.
35 * CTF DWARF conversion theory.
37 * DWARF data contains a series of compilation units. Each compilation unit
38 * generally refers to an object file or what once was, in the case of linked
39 * binaries and shared objects. Each compilation unit has a series of what DWARF
40 * calls a DIE (Debugging Information Entry). The set of entries that we care
41 * about have type information stored in a series of attributes. Each DIE also
42 * has a tag that identifies the kind of attributes that it has.
44 * A given DIE may itself have children. For example, a DIE that represents a
45 * structure has children which represent members. Whenever we encounter a DIE
46 * that has children or other values or types associated with it, we recursively
47 * process those children first so that way we can then refer to the generated
48 * CTF type id while processing its parent. This reduces the amount of unknowns
49 * and fixups that we need. It also ensures that we don't accidentally add types
50 * that an overzealous compiler might add to the DWARF data but aren't used by
51 * anything in the system.
53 * Once we do a conversion, we store a mapping in an AVL tree that goes from the
54 * DWARF's die offset, which is relative to the given compilation unit), to a
57 * Unfortunately, some compilers actually will emit duplicate entries for a
58 * given type that look similar, but aren't quite. To that end, we go through
59 * and do a variant on a merge once we're done processing a single compilation
60 * unit which deduplicates all of the types that are in the unit.
62 * Finally, if we encounter an object that has multiple compilation units, then
63 * we'll convert all of the compilation units separately and then do a merge, so
64 * that way we can result in one single ctf_file_t that represents everything
70 * Because a given object we've been given to convert may have multiple
71 * compilation units, we break the work into two halves. The first half
72 * processes each compilation unit (potentially in parallel) and then the second
73 * half optionally merges all of the dies in the first half. First, we'll cover
74 * what's involved in converting a single ctf_die_t's dwarf to CTF. This covers
75 * the work done in ctf_dwarf_convert_one().
77 * An individual ctf_die_t, which represents a compilation unit, is converted to
78 * CTF in a series of multiple passes.
80 * Pass 1: During the first pass we walk all of the dies and if we find a
81 * function, variable, struct, union, enum or typedef, we recursively transform
82 * all of its types. We don't recurse or process everything, because we don't
83 * want to add some of the types that compilers may add which are effectively
86 * During pass 1, if we encounter any structures or unions we mark them for
87 * fixing up later. This is necessary because we may not be able to determine
88 * the full size of a structure at the beginning of time. This will happen if
89 * the DWARF attribute DW_AT_byte_size is not present for a member. Because of
90 * this possibility we defer adding members to structures or even converting
91 * them during pass 1 and save that for pass 2. Adding all of the base
92 * structures without any of their members helps deal with any circular
93 * dependencies that we might encounter.
95 * Pass 2: This pass is used to do the first half of fixing up structures and
96 * unions. Rather than walk the entire type space again, we actually walk the
97 * list of structures and unions that we marked for later fixing up. Here, we
98 * iterate over every structure and add members to the underlying ctf_file_t,
99 * but not to the structs themselves. One might wonder why we don't, and the
100 * main reason is that libctf requires a ctf_update() be done before adding the
101 * members to structures or unions.
103 * Pass 3: This pass is used to do the second half of fixing up structures and
104 * unions. During this part we always go through and add members to structures
105 * and unions that we added to the container in the previous pass. In addition,
106 * we set the structure and union's actual size, which may have additional
107 * padding added by the compiler, it isn't simply the last offset. DWARF always
108 * guarantees an attribute exists for this. Importantly no ctf_id_t's change
111 * Pass 4: The next phase is to add CTF entries for all of the symbols and
112 * variables that are present in this die. During pass 1 we added entries to a
113 * map for each variable and function. During this pass, we iterate over the
114 * symbol table and when we encounter a symbol that we have in our lists of
115 * translated information which matches, we then add it to the ctf_file_t.
117 * Pass 5: Here we go and look for any weak symbols and functions and see if
118 * they match anything that we recognize. If so, then we add type information
119 * for them at this point based on the matching type.
121 * Pass 6: This pass is actually a variant on a merge. The traditional merge
122 * process expects there to be no duplicate types. As such, at the end of
123 * conversion, we do a dedup on all of the types in the system. The
124 * deduplication process is described in lib/libctf/common/ctf_merge.c.
126 * Once pass 6 is done, we've finished processing the individual compilation
129 * The following steps reflect the general process of doing a conversion.
131 * 1) Walk the dwarf section and determine the number of compilation units
132 * 2) Create a ctf_die_t for each compilation unit
133 * 3) Add all ctf_die_t's to a workq
134 * 4) Have the workq process each die with ctf_dwarf_convert_one. This itself
135 * is comprised of several steps, which were already enumerated.
136 * 5) If we have multiple dies, we do a ctf merge of all the dies. The mechanics
137 * of the merge are discussed in lib/libctf/common/ctf_merge.c.
138 * 6) Free everything up and return a ctf_file_t to the user. If we only had a
139 * single compilation unit, then we give that to the user. Otherwise, we
140 * return the merged ctf_file_t.
145 * The process has been designed to be amenable to threading. Each compilation
146 * unit has its own type stream, therefore the logical place to divide and
147 * conquer is at the compilation unit. Each ctf_die_t has been built to be able
148 * to be processed independently of the others. It has its own libdwarf handle,
149 * as a given libdwarf handle may only be used by a single thread at a time.
150 * This allows the various ctf_die_t's to be processed in parallel by different
153 * All of the ctf_die_t's are loaded into a workq which allows for a number of
154 * threads to be specified and used as a thread pool to process all of the
155 * queued work. We set the number of threads to use in the workq equal to the
156 * number of threads that the user has specified.
158 * After all of the compilation units have been drained, we use the same number
159 * of threads when performing a merge of multiple compilation units, if they
162 * While all of these different parts do support and allow for multiple threads,
163 * it's important that when only a single thread is specified, that it be the
164 * calling thread. This allows the conversion routines to be used in a context
165 * that doesn't allow additional threads, such as rtld.
167 * Common DWARF Mechanics and Notes
168 * --------------------------------
170 * At this time, we really only support DWARFv2, though support for DWARFv4 is
171 * mostly there. There is no intent to support DWARFv3.
173 * Generally types for something are stored in the DW_AT_type attribute. For
174 * example, a function's return type will be stored in the local DW_AT_type
175 * attribute while the arguments will be in child DIEs. There are also various
176 * times when we don't have any DW_AT_type. In that case, the lack of a type
177 * implies, at least for C, that it's C type is void. Because DWARF doesn't emit
178 * one, we have a synthetic void type that we create and manipulate instead and
179 * pass it off to consumers on an as-needed basis. If nothing has a void type,
180 * it will not be emitted.
182 * Architecture Specific Parts
183 * ---------------------------
185 * The CTF tooling encodes various information about the various architectures
186 * in the system. Importantly, the tool assumes that every architecture has a
187 * data model where long and pointer are the same size. This is currently the
188 * case, as the two data models illumos supports are ILP32 and LP64.
190 * In addition, we encode the mapping of various floating point sizes to various
191 * types for each architecture. If a new architecture is being added, it should
192 * be added to the list. The general design of the ctf conversion tools is to be
193 * architecture independent. eg. any of the tools here should be able to convert
194 * any architecture's DWARF into ctf; however, this has not been rigorously
195 * tested and more importantly, the ctf routines don't currently write out the
196 * data in an endian-aware form, they only use that of the currently running
200 #include <libctf_impl.h>
202 #include <sys/debug.h>
204 #include <libdwarf.h>
210 #define DWARF_VERSION_TWO 2
211 #define DWARF_VARARGS_NAME "..."
214 * Dwarf may refer recursively to other types that we've already processed. To
215 * see if we've already converted them, we look them up in an AVL tree that's
216 * sorted by the DWARF id.
218 typedef struct ctf_dwmap
{
226 typedef struct ctf_dwvar
{
230 boolean_t cdv_global
;
233 typedef struct ctf_dwfunc
{
236 ctf_funcinfo_t cdf_fip
;
238 boolean_t cdf_global
;
241 typedef struct ctf_dwbitf
{
249 * The ctf_die_t represents a single top-level DWARF die unit. While generally,
250 * the typical object file hs only a single die, if we're asked to convert
251 * something that's been linked from multiple sources, multiple dies will exist.
253 typedef struct ctf_die
{
254 Elf
*cd_elf
; /* shared libelf handle */
255 char *cd_name
; /* basename of the DIE */
256 ctf_merge_t
*cd_cmh
; /* merge handle */
257 ctf_list_t cd_vars
; /* List of variables */
258 ctf_list_t cd_funcs
; /* List of functions */
259 ctf_list_t cd_bitfields
; /* Bit field members */
260 Dwarf_Debug cd_dwarf
; /* shared libdwarf handle */
261 Dwarf_Die cd_cu
; /* libdwarf compilation unit */
262 Dwarf_Off cd_cuoff
; /* cu's offset */
263 Dwarf_Off cd_maxoff
; /* maximum offset */
264 ctf_file_t
*cd_ctfp
; /* output CTF file */
265 avl_tree_t cd_map
; /* map die offsets to CTF types */
266 char *cd_errbuf
; /* error message buffer */
267 size_t cd_errlen
; /* error message buffer length */
268 size_t cd_ptrsz
; /* object's pointer size */
269 boolean_t cd_bigend
; /* is it big endian */
270 boolean_t cd_doweaks
; /* should we convert weak symbols? */
271 uint_t cd_mach
; /* machine type */
272 ctf_id_t cd_voidtid
; /* void pointer */
273 ctf_id_t cd_longtid
; /* id for a 'long' */
276 static int ctf_dwarf_offset(ctf_die_t
*, Dwarf_Die
, Dwarf_Off
*);
277 static int ctf_dwarf_convert_die(ctf_die_t
*, Dwarf_Die
);
278 static int ctf_dwarf_convert_type(ctf_die_t
*, Dwarf_Die
, ctf_id_t
*, int);
280 static int ctf_dwarf_function_count(ctf_die_t
*, Dwarf_Die
, ctf_funcinfo_t
*,
282 static int ctf_dwarf_convert_fargs(ctf_die_t
*, Dwarf_Die
, ctf_funcinfo_t
*,
285 typedef int (ctf_dwarf_symtab_f
)(ctf_die_t
*, const GElf_Sym
*, ulong_t
,
286 const char *, const char *, void *);
289 * This is a generic way to set a CTF Conversion backend error depending on what
290 * we were doing. Unless it was one of a specific set of errors that don't
291 * indicate a programming / translation bug, eg. ENOMEM, then we transform it
292 * into a CTF backend error and fill in the error buffer.
295 ctf_dwarf_error(ctf_die_t
*cdp
, ctf_file_t
*cfp
, int err
, const char *fmt
, ...)
300 ssize_t rem
= cdp
->cd_errlen
;
302 err
= ctf_errno(cfp
);
307 ret
= snprintf(cdp
->cd_errbuf
, rem
, "die %s: ", cdp
->cd_name
);
311 rem
= MAX(rem
- ret
, 0);
314 ret
= vsnprintf(cdp
->cd_errbuf
+ off
, rem
, fmt
, ap
);
320 rem
= MAX(rem
- ret
, 0);
321 if (fmt
[strlen(fmt
) - 1] != '\n') {
322 (void) snprintf(cdp
->cd_errbuf
+ off
, rem
,
323 ": %s\n", ctf_errmsg(err
));
326 return (ECTF_CONVBKERR
);
329 cdp
->cd_errbuf
[0] = '\0';
330 return (ECTF_CONVBKERR
);
334 * DWARF often ops to put no explicit type to describe a void type. eg. if we
335 * have a reference type whose DW_AT_type member doesn't exist, then we should
336 * instead assume it points to void. Because this isn't represented, we
337 * instead cause it to come into existence.
340 ctf_dwarf_void(ctf_die_t
*cdp
)
342 if (cdp
->cd_voidtid
== CTF_ERR
) {
343 ctf_encoding_t enc
= { CTF_INT_SIGNED
, 0, 0 };
344 cdp
->cd_voidtid
= ctf_add_integer(cdp
->cd_ctfp
, CTF_ADD_ROOT
,
346 if (cdp
->cd_voidtid
== CTF_ERR
) {
347 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
348 "failed to create void type: %s\n",
349 ctf_errmsg(ctf_errno(cdp
->cd_ctfp
)));
353 return (cdp
->cd_voidtid
);
357 * There are many different forms that an array index may take. However, we just
358 * always force it to be of a type long no matter what. Therefore we use this to
359 * have a single instance of long across everything.
362 ctf_dwarf_long(ctf_die_t
*cdp
)
364 if (cdp
->cd_longtid
== CTF_ERR
) {
367 enc
.cte_format
= CTF_INT_SIGNED
;
369 /* All illumos systems are LP */
370 enc
.cte_bits
= cdp
->cd_ptrsz
* 8;
371 cdp
->cd_longtid
= ctf_add_integer(cdp
->cd_ctfp
, CTF_ADD_NONROOT
,
373 if (cdp
->cd_longtid
== CTF_ERR
) {
374 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
375 "failed to create long type: %s\n",
376 ctf_errmsg(ctf_errno(cdp
->cd_ctfp
)));
381 return (cdp
->cd_longtid
);
385 ctf_dwmap_comp(const void *a
, const void *b
)
387 const ctf_dwmap_t
*ca
= a
;
388 const ctf_dwmap_t
*cb
= b
;
390 if (ca
->cdm_off
> cb
->cdm_off
)
392 if (ca
->cdm_off
< cb
->cdm_off
)
398 ctf_dwmap_add(ctf_die_t
*cdp
, ctf_id_t id
, Dwarf_Die die
, boolean_t fix
)
405 VERIFY(id
> 0 && id
< CTF_MAX_TYPE
);
407 if ((ret
= ctf_dwarf_offset(cdp
, die
, &off
)) != 0)
410 if ((dwmap
= ctf_alloc(sizeof (ctf_dwmap_t
))) == NULL
)
413 dwmap
->cdm_die
= die
;
414 dwmap
->cdm_off
= off
;
416 dwmap
->cdm_fix
= fix
;
418 ctf_dprintf("dwmap: %p %x->%d\n", dwmap
, (uint32_t)off
, id
);
419 VERIFY(avl_find(&cdp
->cd_map
, dwmap
, &index
) == NULL
);
420 avl_insert(&cdp
->cd_map
, dwmap
, index
);
425 ctf_dwarf_attribute(ctf_die_t
*cdp
, Dwarf_Die die
, Dwarf_Half name
,
426 Dwarf_Attribute
*attrp
)
431 if ((ret
= dwarf_attr(die
, name
, attrp
, &derr
)) == DW_DLV_OK
)
433 if (ret
== DW_DLV_NO_ENTRY
) {
437 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
438 "failed to get attribute for type: %s\n",
440 return (ECTF_CONVBKERR
);
444 ctf_dwarf_ref(ctf_die_t
*cdp
, Dwarf_Die die
, Dwarf_Half name
, Dwarf_Off
*refp
)
447 Dwarf_Attribute attr
;
450 if ((ret
= ctf_dwarf_attribute(cdp
, die
, name
, &attr
)) != 0)
453 if (dwarf_formref(attr
, refp
, &derr
) == DW_DLV_OK
) {
454 dwarf_dealloc(cdp
->cd_dwarf
, attr
, DW_DLA_ATTR
);
458 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
459 "failed to get unsigned attribute for type: %s\n",
461 return (ECTF_CONVBKERR
);
465 ctf_dwarf_refdie(ctf_die_t
*cdp
, Dwarf_Die die
, Dwarf_Half name
,
472 if ((ret
= ctf_dwarf_ref(cdp
, die
, name
, &off
)) != 0)
475 off
+= cdp
->cd_cuoff
;
476 if ((ret
= dwarf_offdie(cdp
->cd_dwarf
, off
, diep
, &derr
)) !=
478 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
479 "failed to get die from offset %llu: %s\n",
480 off
, dwarf_errmsg(derr
));
481 return (ECTF_CONVBKERR
);
488 ctf_dwarf_signed(ctf_die_t
*cdp
, Dwarf_Die die
, Dwarf_Half name
,
492 Dwarf_Attribute attr
;
495 if ((ret
= ctf_dwarf_attribute(cdp
, die
, name
, &attr
)) != 0)
498 if (dwarf_formsdata(attr
, valp
, &derr
) == DW_DLV_OK
) {
499 dwarf_dealloc(cdp
->cd_dwarf
, attr
, DW_DLA_ATTR
);
503 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
504 "failed to get unsigned attribute for type: %s\n",
506 return (ECTF_CONVBKERR
);
510 ctf_dwarf_unsigned(ctf_die_t
*cdp
, Dwarf_Die die
, Dwarf_Half name
,
511 Dwarf_Unsigned
*valp
)
514 Dwarf_Attribute attr
;
517 if ((ret
= ctf_dwarf_attribute(cdp
, die
, name
, &attr
)) != 0)
520 if (dwarf_formudata(attr
, valp
, &derr
) == DW_DLV_OK
) {
521 dwarf_dealloc(cdp
->cd_dwarf
, attr
, DW_DLA_ATTR
);
525 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
526 "failed to get unsigned attribute for type: %s\n",
528 return (ECTF_CONVBKERR
);
532 ctf_dwarf_boolean(ctf_die_t
*cdp
, Dwarf_Die die
, Dwarf_Half name
,
536 Dwarf_Attribute attr
;
539 if ((ret
= ctf_dwarf_attribute(cdp
, die
, name
, &attr
)) != 0)
542 if (dwarf_formflag(attr
, val
, &derr
) == DW_DLV_OK
) {
543 dwarf_dealloc(cdp
->cd_dwarf
, attr
, DW_DLA_ATTR
);
547 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
548 "failed to get boolean attribute for type: %s\n",
551 return (ECTF_CONVBKERR
);
555 ctf_dwarf_string(ctf_die_t
*cdp
, Dwarf_Die die
, Dwarf_Half name
, char **strp
)
559 Dwarf_Attribute attr
;
563 if ((ret
= ctf_dwarf_attribute(cdp
, die
, name
, &attr
)) != 0)
566 if (dwarf_formstring(attr
, &s
, &derr
) == DW_DLV_OK
) {
567 if ((*strp
= ctf_strdup(s
)) == NULL
)
571 dwarf_dealloc(cdp
->cd_dwarf
, attr
, DW_DLA_ATTR
);
575 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
576 "failed to get string attribute for type: %s\n",
578 return (ECTF_CONVBKERR
);
582 ctf_dwarf_member_location(ctf_die_t
*cdp
, Dwarf_Die die
, Dwarf_Unsigned
*valp
)
586 Dwarf_Attribute attr
;
590 if ((ret
= ctf_dwarf_attribute(cdp
, die
, DW_AT_data_member_location
,
594 if (dwarf_loclist(attr
, &loc
, &locnum
, &derr
) != DW_DLV_OK
) {
595 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
596 "failed to obtain location list for member offset: %s",
598 dwarf_dealloc(cdp
->cd_dwarf
, attr
, DW_DLA_ATTR
);
599 return (ECTF_CONVBKERR
);
601 dwarf_dealloc(cdp
->cd_dwarf
, attr
, DW_DLA_ATTR
);
603 if (locnum
!= 1 || loc
->ld_s
->lr_atom
!= DW_OP_plus_uconst
) {
604 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
605 "failed to parse location structure for member");
606 dwarf_dealloc(cdp
->cd_dwarf
, loc
->ld_s
, DW_DLA_LOC_BLOCK
);
607 dwarf_dealloc(cdp
->cd_dwarf
, loc
, DW_DLA_LOCDESC
);
608 return (ECTF_CONVBKERR
);
611 *valp
= loc
->ld_s
->lr_number
;
613 dwarf_dealloc(cdp
->cd_dwarf
, loc
->ld_s
, DW_DLA_LOC_BLOCK
);
614 dwarf_dealloc(cdp
->cd_dwarf
, loc
, DW_DLA_LOCDESC
);
620 ctf_dwarf_offset(ctf_die_t
*cdp
, Dwarf_Die die
, Dwarf_Off
*offsetp
)
624 if (dwarf_dieoffset(die
, offsetp
, &derr
) == DW_DLV_OK
)
627 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
628 "failed to get die offset: %s\n",
630 return (ECTF_CONVBKERR
);
634 ctf_dwarf_tag(ctf_die_t
*cdp
, Dwarf_Die die
, Dwarf_Half
*tagp
)
638 if (dwarf_tag(die
, tagp
, &derr
) == DW_DLV_OK
)
641 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
642 "failed to get tag type: %s\n",
644 return (ECTF_CONVBKERR
);
648 ctf_dwarf_sib(ctf_die_t
*cdp
, Dwarf_Die base
, Dwarf_Die
*sibp
)
654 ret
= dwarf_siblingof(cdp
->cd_dwarf
, base
, sibp
, &derr
);
655 if (ret
== DW_DLV_OK
|| ret
== DW_DLV_NO_ENTRY
)
658 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
659 "failed to sibling from die: %s\n",
661 return (ECTF_CONVBKERR
);
665 ctf_dwarf_child(ctf_die_t
*cdp
, Dwarf_Die base
, Dwarf_Die
*childp
)
671 ret
= dwarf_child(base
, childp
, &derr
);
672 if (ret
== DW_DLV_OK
|| ret
== DW_DLV_NO_ENTRY
)
675 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
676 "failed to child from die: %s\n",
678 return (ECTF_CONVBKERR
);
682 * Compilers disagree on what to do to determine if something has global
683 * visiblity. Traditionally gcc has used DW_AT_external to indicate this while
684 * Studio has used DW_AT_visibility. We check DW_AT_visibility first and then
685 * fall back to DW_AT_external. Lack of DW_AT_external implies that it is not.
688 ctf_dwarf_isglobal(ctf_die_t
*cdp
, Dwarf_Die die
, boolean_t
*igp
)
694 if ((ret
= ctf_dwarf_signed(cdp
, die
, DW_AT_visibility
, &vis
)) == 0) {
695 *igp
= vis
== DW_VIS_exported
;
697 } else if (ret
!= ENOENT
) {
701 if ((ret
= ctf_dwarf_boolean(cdp
, die
, DW_AT_external
, &ext
)) != 0) {
708 *igp
= ext
!= 0 ? B_TRUE
: B_FALSE
;
713 ctf_dwarf_die_elfenc(Elf
*elf
, ctf_die_t
*cdp
, char *errbuf
, size_t errlen
)
717 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
718 (void) snprintf(errbuf
, errlen
,
719 "failed to get ELF header: %s\n",
720 elf_errmsg(elf_errno()));
721 return (ECTF_CONVBKERR
);
724 cdp
->cd_mach
= ehdr
.e_machine
;
726 if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
) {
728 VERIFY(ctf_setmodel(cdp
->cd_ctfp
, CTF_MODEL_ILP32
) == 0);
729 } else if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS64
) {
731 VERIFY(ctf_setmodel(cdp
->cd_ctfp
, CTF_MODEL_LP64
) == 0);
733 (void) snprintf(errbuf
, errlen
,
734 "unknown ELF class %d", ehdr
.e_ident
[EI_CLASS
]);
735 return (ECTF_CONVBKERR
);
738 if (ehdr
.e_ident
[EI_DATA
] == ELFDATA2LSB
) {
739 cdp
->cd_bigend
= B_FALSE
;
740 } else if (ehdr
.e_ident
[EI_DATA
] == ELFDATA2MSB
) {
741 cdp
->cd_bigend
= B_TRUE
;
743 (void) snprintf(errbuf
, errlen
,
744 "unknown ELF data encoding: %d", ehdr
.e_ident
[EI_DATA
]);
745 return (ECTF_CONVBKERR
);
751 typedef struct ctf_dwarf_fpent
{
756 typedef struct ctf_dwarf_fpmap
{
758 ctf_dwarf_fpent_t cdf_ents
[4];
761 static const ctf_dwarf_fpmap_t ctf_dwarf_fpmaps
[] = {
763 { 4, { CTF_FP_SINGLE
, CTF_FP_CPLX
, CTF_FP_IMAGRY
} },
764 { 8, { CTF_FP_DOUBLE
, CTF_FP_DCPLX
, CTF_FP_DIMAGRY
} },
765 { 16, { CTF_FP_LDOUBLE
, CTF_FP_LDCPLX
, CTF_FP_LDIMAGRY
} },
769 { 4, { CTF_FP_SINGLE
, CTF_FP_CPLX
, CTF_FP_IMAGRY
} },
770 { 8, { CTF_FP_DOUBLE
, CTF_FP_DCPLX
, CTF_FP_DIMAGRY
} },
771 { 16, { CTF_FP_LDOUBLE
, CTF_FP_LDCPLX
, CTF_FP_LDIMAGRY
} },
775 { 4, { CTF_FP_SINGLE
, CTF_FP_CPLX
, CTF_FP_IMAGRY
} },
776 { 8, { CTF_FP_DOUBLE
, CTF_FP_DCPLX
, CTF_FP_DIMAGRY
} },
777 { 16, { CTF_FP_LDOUBLE
, CTF_FP_LDCPLX
, CTF_FP_LDIMAGRY
} },
781 { 4, { CTF_FP_SINGLE
, CTF_FP_CPLX
, CTF_FP_IMAGRY
} },
782 { 8, { CTF_FP_DOUBLE
, CTF_FP_DCPLX
, CTF_FP_DIMAGRY
} },
783 { 12, { CTF_FP_LDOUBLE
, CTF_FP_LDCPLX
, CTF_FP_LDIMAGRY
} },
787 { 4, { CTF_FP_SINGLE
, CTF_FP_CPLX
, CTF_FP_IMAGRY
} },
788 { 8, { CTF_FP_DOUBLE
, CTF_FP_DCPLX
, CTF_FP_DIMAGRY
} },
789 { 16, { CTF_FP_LDOUBLE
, CTF_FP_LDCPLX
, CTF_FP_LDIMAGRY
} },
796 ctf_dwarf_float_base(ctf_die_t
*cdp
, Dwarf_Signed type
, ctf_encoding_t
*enc
)
798 const ctf_dwarf_fpmap_t
*map
= &ctf_dwarf_fpmaps
[0];
799 const ctf_dwarf_fpent_t
*ent
;
800 uint_t col
= 0, mult
= 1;
802 for (map
= &ctf_dwarf_fpmaps
[0]; map
->cdf_mach
!= EM_NONE
; map
++) {
803 if (map
->cdf_mach
== cdp
->cd_mach
)
807 if (map
->cdf_mach
== EM_NONE
) {
808 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
809 "Unsupported machine type: %d\n", cdp
->cd_mach
);
813 if (type
== DW_ATE_complex_float
) {
816 } else if (type
== DW_ATE_imaginary_float
||
817 type
== DW_ATE_SUN_imaginary_float
) {
821 ent
= &map
->cdf_ents
[0];
822 for (ent
= &map
->cdf_ents
[0]; ent
->cdfe_size
!= 0; ent
++) {
823 if (ent
->cdfe_size
* mult
* 8 == enc
->cte_bits
) {
824 enc
->cte_format
= ent
->cdfe_enc
[col
];
829 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
830 "failed to find valid fp mapping for encoding %d, size %d bits\n",
831 type
, enc
->cte_bits
);
836 ctf_dwarf_dwarf_base(ctf_die_t
*cdp
, Dwarf_Die die
, int *kindp
,
842 if ((ret
= ctf_dwarf_signed(cdp
, die
, DW_AT_encoding
, &type
)) != 0)
846 case DW_ATE_unsigned
:
848 *kindp
= CTF_K_INTEGER
;
851 case DW_ATE_unsigned_char
:
852 *kindp
= CTF_K_INTEGER
;
853 enc
->cte_format
= CTF_INT_CHAR
;
856 *kindp
= CTF_K_INTEGER
;
857 enc
->cte_format
= CTF_INT_SIGNED
;
859 case DW_ATE_signed_char
:
860 *kindp
= CTF_K_INTEGER
;
861 enc
->cte_format
= CTF_INT_SIGNED
| CTF_INT_CHAR
;
864 *kindp
= CTF_K_INTEGER
;
865 enc
->cte_format
= CTF_INT_SIGNED
| CTF_INT_BOOL
;
868 case DW_ATE_complex_float
:
869 case DW_ATE_imaginary_float
:
870 case DW_ATE_SUN_imaginary_float
:
871 case DW_ATE_SUN_interval_float
:
872 *kindp
= CTF_K_FLOAT
;
873 if ((ret
= ctf_dwarf_float_base(cdp
, type
, enc
)) != 0)
877 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
878 "encountered unkown DWARF encoding: %d", type
);
879 return (ECTF_CONVBKERR
);
886 * Different compilers (at least GCC and Studio) use different names for types.
887 * This parses the types and attempts to unify them. If this fails, we just fall
888 * back to using the DWARF itself.
891 ctf_dwarf_parse_base(const char *name
, int *kindp
, ctf_encoding_t
*enc
,
896 int nlong
= 0, nshort
= 0, nchar
= 0, nint
= 0;
899 if (strlen(name
) + 1 > sizeof (buf
))
902 (void) strlcpy(buf
, name
, sizeof (buf
));
903 for (c
= strtok(buf
, " "); c
!= NULL
; c
= strtok(NULL
, " ")) {
904 if (strcmp(c
, "signed") == 0) {
906 } else if (strcmp(c
, "unsigned") == 0) {
908 } else if (strcmp(c
, "long") == 0) {
910 } else if (strcmp(c
, "char") == 0) {
912 } else if (strcmp(c
, "short") == 0) {
914 } else if (strcmp(c
, "int") == 0) {
918 * If we don't recognize any of the tokens, we'll tell
919 * the caller to fall back to the dwarf-provided
920 * encoding information.
926 if (nchar
> 1 || nshort
> 1 || nint
> 1 || nlong
> 2)
930 if (nlong
> 0 || nshort
> 0 || nint
> 0)
933 } else if (nshort
> 0) {
937 } else if (nlong
> 0) {
944 enc
->cte_format
= CTF_INT_CHAR
;
949 enc
->cte_format
|= CTF_INT_SIGNED
;
951 (void) snprintf(buf
, sizeof (buf
), "%s%s%s",
952 (sign
? "" : "unsigned "),
953 (nlong
> 1 ? "long " : ""),
956 *newnamep
= ctf_strdup(buf
);
957 if (*newnamep
== NULL
)
959 *kindp
= CTF_K_INTEGER
;
964 ctf_dwarf_create_base(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_id_t
*idp
, int isroot
,
974 if ((ret
= ctf_dwarf_string(cdp
, die
, DW_AT_name
, &name
)) != 0)
976 if ((ret
= ctf_dwarf_unsigned(cdp
, die
, DW_AT_byte_size
, &sz
)) != 0) {
979 ctf_dprintf("Creating base type %s from off %llu, size: %d\n", name
,
982 bzero(&enc
, sizeof (ctf_encoding_t
));
983 enc
.cte_bits
= sz
* 8;
984 if ((ret
= ctf_dwarf_parse_base(name
, &kind
, &enc
, &nname
)) == 0) {
985 ctf_free(name
, strlen(name
) + 1);
990 ctf_dprintf("falling back to dwarf for base type %s\n", name
);
991 if ((ret
= ctf_dwarf_dwarf_base(cdp
, die
, &kind
, &enc
)) != 0)
995 id
= ctf_add_encoded(cdp
->cd_ctfp
, isroot
, name
, &enc
, kind
);
997 ret
= ctf_errno(cdp
->cd_ctfp
);
1000 ret
= ctf_dwmap_add(cdp
, id
, die
, B_FALSE
);
1003 ctf_free(name
, strlen(name
) + 1);
1008 * Getting a member's offset is a surprisingly intricate dance. It works as
1011 * 1) If we're in DWARFv4, then we either have a DW_AT_data_bit_offset or we
1012 * have a DW_AT_data_member_location. We won't have both. Thus we check first
1013 * for DW_AT_data_bit_offset, and if it exists, we're set.
1015 * Next, if we have a bitfield and we don't ahve a DW_AT_data_bit_offset, then
1016 * we have to grab the data location and use the following dance:
1018 * 2) Gather the set of DW_AT_byte_size, DW_AT_bit_offset, and DW_AT_bit_size.
1019 * Of course, the DW_AT_byte_size may be omitted, even though it isn't always.
1020 * When it's been omitted, we then have to say that the size is that of the
1021 * underlying type, which forces that to be after a ctf_update(). Here, we have
1022 * to do different things based on whether or not we're using big endian or
1023 * little endian to obtain the proper offset.
1026 ctf_dwarf_member_offset(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_id_t mid
,
1030 Dwarf_Unsigned loc
, bitsz
, bytesz
;
1031 Dwarf_Signed bitoff
;
1035 if ((ret
= ctf_dwarf_unsigned(cdp
, die
, DW_AT_data_bit_offset
,
1039 } else if (ret
!= ENOENT
) {
1043 if ((ret
= ctf_dwarf_member_location(cdp
, die
, &loc
)) != 0)
1047 if ((ret
= ctf_dwarf_signed(cdp
, die
, DW_AT_bit_offset
,
1055 /* At this point we have to have DW_AT_bit_size */
1056 if ((ret
= ctf_dwarf_unsigned(cdp
, die
, DW_AT_bit_size
, &bitsz
)) != 0)
1059 if ((ret
= ctf_dwarf_unsigned(cdp
, die
, DW_AT_byte_size
,
1063 if ((tsz
= ctf_type_size(cdp
->cd_ctfp
, mid
)) == CTF_ERR
) {
1064 int e
= ctf_errno(cdp
->cd_ctfp
);
1065 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
1066 "failed to get type size: %s", ctf_errmsg(e
));
1067 return (ECTF_CONVBKERR
);
1073 if (cdp
->cd_bigend
== B_TRUE
) {
1074 *offp
= off
+ bitoff
;
1076 *offp
= off
+ tsz
- bitoff
- bitsz
;
1083 * We need to determine if the member in question is a bitfield. If it is, then
1084 * we need to go through and create a new type that's based on the actual base
1085 * type, but has a different size. We also rename the type as a result to help
1086 * deal with future collisions.
1088 * Here we need to look and see if we have a DW_AT_bit_size value. If we have a
1089 * bit size member and it does not equal the byte size member, then we need to
1090 * create a bitfield type based on this.
1092 * Note: When we support DWARFv4, there may be a chance that we ned to also
1093 * search for the DW_AT_byte_size if we don't have a DW_AT_bit_size member.
1096 ctf_dwarf_member_bitfield(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_id_t
*idp
)
1099 Dwarf_Unsigned bitsz
;
1103 ctf_id_t base
= *idp
;
1106 if ((ret
= ctf_dwarf_unsigned(cdp
, die
, DW_AT_bit_size
, &bitsz
)) != 0) {
1112 ctf_dprintf("Trying to deal with bitfields on %d:%d\n", base
, bitsz
);
1114 * Given that we now have a bitsize, time to go do something about it.
1115 * We're going to create a new type based on the current one, but first
1116 * we need to find the base type. This means we need to traverse any
1117 * typedef's, consts, and volatiles until we get to what should be
1118 * something of type integer or enumeration.
1120 VERIFY(bitsz
< UINT32_MAX
);
1121 dtd
= ctf_dtd_lookup(cdp
->cd_ctfp
, base
);
1122 VERIFY(dtd
!= NULL
);
1123 kind
= CTF_INFO_KIND(dtd
->dtd_data
.ctt_info
);
1124 while (kind
== CTF_K_TYPEDEF
|| kind
== CTF_K_CONST
||
1125 kind
== CTF_K_VOLATILE
) {
1126 dtd
= ctf_dtd_lookup(cdp
->cd_ctfp
, dtd
->dtd_data
.ctt_type
);
1127 VERIFY(dtd
!= NULL
);
1128 kind
= CTF_INFO_KIND(dtd
->dtd_data
.ctt_info
);
1130 ctf_dprintf("got kind %d\n", kind
);
1131 VERIFY(kind
== CTF_K_INTEGER
|| kind
== CTF_K_ENUM
);
1134 * As surprising as it may be, it is strictly possible to create a
1135 * bitfield that is based on an enum. Of course, the C standard leaves
1136 * enums sizing as an ABI concern more or less. To that effect, today on
1137 * all illumos platforms the size of an enum is generally that of an
1138 * int as our supported data models and ABIs all agree on that. So what
1139 * we'll do is fake up a CTF enconding here to use. In this case, we'll
1140 * treat it as an unsigned value of whatever size the underlying enum
1141 * currently has (which is in the ctt_size member of its dynamic type
1144 if (kind
== CTF_K_INTEGER
) {
1145 e
= dtd
->dtd_u
.dtu_enc
;
1147 bzero(&e
, sizeof (ctf_encoding_t
));
1148 e
.cte_bits
= dtd
->dtd_data
.ctt_size
* NBBY
;
1151 for (cdb
= ctf_list_next(&cdp
->cd_bitfields
); cdb
!= NULL
;
1152 cdb
= ctf_list_next(cdb
)) {
1153 if (cdb
->cdb_base
== base
&& cdb
->cdb_nbits
== bitsz
)
1158 * Create a new type if none exists. We name all types in a way that is
1159 * guaranteed not to conflict with the corresponding C type. We do this
1160 * by using the ':' operator.
1167 namesz
= snprintf(NULL
, 0, "%s:%d", dtd
->dtd_name
,
1169 name
= ctf_alloc(namesz
+ 1);
1172 cdb
= ctf_alloc(sizeof (ctf_dwbitf_t
));
1174 ctf_free(name
, namesz
+ 1);
1177 (void) snprintf(name
, namesz
+ 1, "%s:%d", dtd
->dtd_name
,
1180 cdb
->cdb_base
= base
;
1181 cdb
->cdb_nbits
= bitsz
;
1182 cdb
->cdb_id
= ctf_add_integer(cdp
->cd_ctfp
, CTF_ADD_NONROOT
,
1184 if (cdb
->cdb_id
== CTF_ERR
) {
1185 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
1186 "failed to get add bitfield type %s: %s", name
,
1187 ctf_errmsg(ctf_errno(cdp
->cd_ctfp
)));
1188 ctf_free(name
, namesz
+ 1);
1189 ctf_free(cdb
, sizeof (ctf_dwbitf_t
));
1190 return (ECTF_CONVBKERR
);
1192 ctf_free(name
, namesz
+ 1);
1193 ctf_list_append(&cdp
->cd_bitfields
, cdb
);
1202 ctf_dwarf_fixup_sou(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_id_t base
, boolean_t add
)
1205 Dwarf_Die child
, memb
;
1206 Dwarf_Unsigned size
;
1209 kind
= ctf_type_kind(cdp
->cd_ctfp
, base
);
1210 VERIFY(kind
!= CTF_ERR
);
1211 VERIFY(kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
);
1214 * Members are in children. However, gcc also allows empty ones.
1216 if ((ret
= ctf_dwarf_child(cdp
, die
, &child
)) != 0)
1222 while (memb
!= NULL
) {
1223 Dwarf_Die sib
, tdie
;
1227 ulong_t memboff
= 0;
1229 if ((ret
= ctf_dwarf_tag(cdp
, memb
, &tag
)) != 0)
1232 if (tag
!= DW_TAG_member
)
1235 if ((ret
= ctf_dwarf_refdie(cdp
, memb
, DW_AT_type
, &tdie
)) != 0)
1238 if ((ret
= ctf_dwarf_convert_type(cdp
, tdie
, &mid
,
1239 CTF_ADD_NONROOT
)) != 0)
1241 ctf_dprintf("Got back type id: %d\n", mid
);
1244 * If we're not adding a member, just go ahead and return.
1246 if (add
== B_FALSE
) {
1247 if ((ret
= ctf_dwarf_member_bitfield(cdp
, memb
,
1253 if ((ret
= ctf_dwarf_string(cdp
, memb
, DW_AT_name
,
1254 &mname
)) != 0 && ret
!= ENOENT
)
1259 if (kind
== CTF_K_UNION
) {
1261 } else if ((ret
= ctf_dwarf_member_offset(cdp
, memb
, mid
,
1264 ctf_free(mname
, strlen(mname
) + 1);
1268 if ((ret
= ctf_dwarf_member_bitfield(cdp
, memb
, &mid
)) != 0)
1271 ret
= ctf_add_member(cdp
->cd_ctfp
, base
, mname
, mid
, memboff
);
1272 if (ret
== CTF_ERR
) {
1273 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
1274 "failed to add member %s: %s",
1275 mname
, ctf_errmsg(ctf_errno(cdp
->cd_ctfp
)));
1277 ctf_free(mname
, strlen(mname
) + 1);
1278 return (ECTF_CONVBKERR
);
1282 ctf_free(mname
, strlen(mname
) + 1);
1285 if ((ret
= ctf_dwarf_sib(cdp
, memb
, &sib
)) != 0)
1291 * If we're not adding members, then we don't know the final size of the
1292 * structure, so end here.
1297 /* Finally set the size of the structure to the actual byte size */
1298 if ((ret
= ctf_dwarf_unsigned(cdp
, die
, DW_AT_byte_size
, &size
)) != 0)
1301 if ((ctf_set_size(cdp
->cd_ctfp
, base
, nsz
)) == CTF_ERR
) {
1302 int e
= ctf_errno(cdp
->cd_ctfp
);
1303 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
1304 "failed to set type size for %d to 0x%x: %s", base
,
1305 (uint32_t)size
, ctf_errmsg(e
));
1306 return (ECTF_CONVBKERR
);
1313 ctf_dwarf_create_sou(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_id_t
*idp
,
1314 int kind
, int isroot
)
1323 * Deal with the terribly annoying case of anonymous structs and unions.
1324 * If they don't have a name, set the name to the empty string.
1326 if ((ret
= ctf_dwarf_string(cdp
, die
, DW_AT_name
, &name
)) != 0 &&
1333 * We need to check if we just have a declaration here. If we do, then
1334 * instead of creating an actual structure or union, we're just going to
1335 * go ahead and create a forward. During a dedup or merge, the forward
1336 * will be replaced with the real thing.
1338 if ((ret
= ctf_dwarf_boolean(cdp
, die
, DW_AT_declaration
,
1346 base
= ctf_add_forward(cdp
->cd_ctfp
, isroot
, name
, kind
);
1347 } else if (kind
== CTF_K_STRUCT
) {
1348 base
= ctf_add_struct(cdp
->cd_ctfp
, isroot
, name
);
1350 base
= ctf_add_union(cdp
->cd_ctfp
, isroot
, name
);
1352 ctf_dprintf("added sou %s (%d) (%d)\n", name
, kind
, base
);
1354 ctf_free(name
, strlen(name
) + 1);
1355 if (base
== CTF_ERR
)
1356 return (ctf_errno(cdp
->cd_ctfp
));
1360 * If it's just a declaration, we're not going to mark it for fix up or
1364 return (ctf_dwmap_add(cdp
, base
, die
, B_FALSE
));
1365 if ((ret
= ctf_dwmap_add(cdp
, base
, die
, B_TRUE
)) != 0)
1369 * Members are in children. However, gcc also allows empty ones.
1371 if ((ret
= ctf_dwarf_child(cdp
, die
, &child
)) != 0)
1380 ctf_dwarf_create_array_range(ctf_die_t
*cdp
, Dwarf_Die range
, ctf_id_t
*idp
,
1381 ctf_id_t base
, int isroot
)
1389 ctf_dprintf("creating array range\n");
1391 if ((ret
= ctf_dwarf_sib(cdp
, range
, &sib
)) != 0)
1395 if ((ret
= ctf_dwarf_create_array_range(cdp
, sib
, &id
,
1396 base
, CTF_ADD_NONROOT
)) != 0)
1398 ar
.ctr_contents
= id
;
1400 ar
.ctr_contents
= base
;
1403 if ((ar
.ctr_index
= ctf_dwarf_long(cdp
)) == CTF_ERR
)
1404 return (ctf_errno(cdp
->cd_ctfp
));
1407 * Array bounds can be signed or unsigned, but there are several kinds
1408 * of signless forms (data1, data2, etc) that take their sign from the
1409 * routine that is trying to interpret them. That is, data1 can be
1410 * either signed or unsigned, depending on whether you use the signed or
1411 * unsigned accessor function. GCC will use the signless forms to store
1412 * unsigned values which have their high bit set, so we need to try to
1413 * read them first as unsigned to get positive values. We could also
1414 * try signed first, falling back to unsigned if we got a negative
1417 if ((ret
= ctf_dwarf_unsigned(cdp
, range
, DW_AT_upper_bound
,
1419 ar
.ctr_nelems
= val
+ 1;
1420 } else if (ret
!= ENOENT
) {
1422 } else if ((ret
= ctf_dwarf_signed(cdp
, range
, DW_AT_upper_bound
,
1424 ar
.ctr_nelems
= sval
+ 1;
1425 } else if (ret
!= ENOENT
) {
1431 if ((*idp
= ctf_add_array(cdp
->cd_ctfp
, isroot
, &ar
)) == CTF_ERR
)
1432 return (ctf_errno(cdp
->cd_ctfp
));
1438 * Try and create an array type. First, the kind of the array is specified in
1439 * the DW_AT_type entry. Next, the number of entries is stored in a more
1440 * complicated form, we should have a child that has the DW_TAG_subrange type.
1443 ctf_dwarf_create_array(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_id_t
*idp
, int isroot
)
1446 Dwarf_Die tdie
, rdie
;
1450 if ((ret
= ctf_dwarf_refdie(cdp
, die
, DW_AT_type
, &tdie
)) != 0)
1452 if ((ret
= ctf_dwarf_convert_type(cdp
, tdie
, &tid
,
1453 CTF_ADD_NONROOT
)) != 0)
1456 if ((ret
= ctf_dwarf_child(cdp
, die
, &rdie
)) != 0)
1458 if ((ret
= ctf_dwarf_tag(cdp
, rdie
, &rtag
)) != 0)
1460 if (rtag
!= DW_TAG_subrange_type
) {
1461 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
1462 "encountered array without DW_TAG_subrange_type child\n");
1463 return (ECTF_CONVBKERR
);
1467 * The compiler may opt to describe a multi-dimensional array as one
1468 * giant array or it may opt to instead encode it as a series of
1469 * subranges. If it's the latter, then for each subrange we introduce a
1470 * type. We can always use the base type.
1472 if ((ret
= ctf_dwarf_create_array_range(cdp
, rdie
, idp
, tid
,
1475 ctf_dprintf("Got back id %d\n", *idp
);
1476 return (ctf_dwmap_add(cdp
, *idp
, die
, B_FALSE
));
1480 ctf_dwarf_create_reference(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_id_t
*idp
,
1481 int kind
, int isroot
)
1489 if ((ret
= ctf_dwarf_string(cdp
, die
, DW_AT_name
, &name
)) != 0 &&
1492 if (ret
== ENOENT
) {
1496 namelen
= strlen(name
);
1499 ctf_dprintf("reference kind %d %s\n", kind
, name
!= NULL
? name
: "<>");
1501 if ((ret
= ctf_dwarf_refdie(cdp
, die
, DW_AT_type
, &tdie
)) != 0) {
1502 if (ret
!= ENOENT
) {
1503 ctf_free(name
, namelen
);
1506 if ((id
= ctf_dwarf_void(cdp
)) == CTF_ERR
) {
1507 ctf_free(name
, namelen
);
1508 return (ctf_errno(cdp
->cd_ctfp
));
1511 if ((ret
= ctf_dwarf_convert_type(cdp
, tdie
, &id
,
1512 CTF_ADD_NONROOT
)) != 0) {
1513 ctf_free(name
, namelen
);
1518 if ((*idp
= ctf_add_reftype(cdp
->cd_ctfp
, isroot
, name
, id
, kind
)) ==
1520 ctf_free(name
, namelen
);
1521 return (ctf_errno(cdp
->cd_ctfp
));
1524 ctf_free(name
, namelen
);
1525 return (ctf_dwmap_add(cdp
, *idp
, die
, B_FALSE
));
1529 ctf_dwarf_create_enum(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_id_t
*idp
, int isroot
)
1536 if ((ret
= ctf_dwarf_string(cdp
, die
, DW_AT_name
, &name
)) != 0 &&
1541 id
= ctf_add_enum(cdp
->cd_ctfp
, isroot
, name
);
1542 ctf_dprintf("added enum %s (%d)\n", name
, id
);
1544 ctf_free(name
, strlen(name
) + 1);
1546 return (ctf_errno(cdp
->cd_ctfp
));
1548 if ((ret
= ctf_dwmap_add(cdp
, id
, die
, B_FALSE
)) != 0)
1552 if ((ret
= ctf_dwarf_child(cdp
, die
, &child
)) != 0) {
1558 while (child
!= NULL
) {
1561 Dwarf_Unsigned uval
;
1562 Dwarf_Die arg
= child
;
1565 if ((ret
= ctf_dwarf_sib(cdp
, arg
, &child
)) != 0)
1568 if ((ret
= ctf_dwarf_tag(cdp
, arg
, &tag
)) != 0)
1571 if (tag
!= DW_TAG_enumerator
) {
1572 if ((ret
= ctf_dwarf_convert_type(cdp
, arg
, NULL
,
1573 CTF_ADD_NONROOT
)) != 0)
1578 if ((ret
= ctf_dwarf_signed(cdp
, arg
, DW_AT_const_value
,
1581 } else if (ret
!= ENOENT
) {
1583 } else if ((ret
= ctf_dwarf_unsigned(cdp
, arg
,
1584 DW_AT_const_value
, &uval
)) == 0) {
1587 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
1588 "encountered enumration without constant value\n");
1589 return (ECTF_CONVBKERR
);
1593 * DWARF v4 section 5.7 tells us we'll always have names.
1595 if ((ret
= ctf_dwarf_string(cdp
, arg
, DW_AT_name
,
1599 ret
= ctf_add_enumerator(cdp
->cd_ctfp
, id
, name
, eval
);
1600 if (ret
== CTF_ERR
) {
1601 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
1602 "failed to add enumarator %s (%d) to %d\n",
1604 ctf_free(name
, strlen(name
) + 1);
1605 return (ctf_errno(cdp
->cd_ctfp
));
1607 ctf_free(name
, strlen(name
) + 1);
1614 * For a function pointer, walk over and process all of its children, unless we
1615 * encounter one that's just a declaration. In which case, we error on it.
1618 ctf_dwarf_create_fptr(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_id_t
*idp
, int isroot
)
1624 ctf_id_t
*argv
= NULL
;
1626 bzero(&fi
, sizeof (ctf_funcinfo_t
));
1628 if ((ret
= ctf_dwarf_boolean(cdp
, die
, DW_AT_declaration
, &b
)) != 0) {
1633 return (EPROTOTYPE
);
1637 * Return type is in DW_AT_type, if none, it returns void.
1639 if ((ret
= ctf_dwarf_refdie(cdp
, die
, DW_AT_type
, &retdie
)) != 0) {
1642 if ((fi
.ctc_return
= ctf_dwarf_void(cdp
)) == CTF_ERR
)
1643 return (ctf_errno(cdp
->cd_ctfp
));
1645 if ((ret
= ctf_dwarf_convert_type(cdp
, retdie
, &fi
.ctc_return
,
1646 CTF_ADD_NONROOT
)) != 0)
1650 if ((ret
= ctf_dwarf_function_count(cdp
, die
, &fi
, B_TRUE
)) != 0) {
1654 if (fi
.ctc_argc
!= 0) {
1655 argv
= ctf_alloc(sizeof (ctf_id_t
) * fi
.ctc_argc
);
1659 if ((ret
= ctf_dwarf_convert_fargs(cdp
, die
, &fi
, argv
)) != 0) {
1660 ctf_free(argv
, sizeof (ctf_id_t
) * fi
.ctc_argc
);
1665 if ((*idp
= ctf_add_funcptr(cdp
->cd_ctfp
, isroot
, &fi
, argv
)) ==
1667 ctf_free(argv
, sizeof (ctf_id_t
) * fi
.ctc_argc
);
1668 return (ctf_errno(cdp
->cd_ctfp
));
1671 ctf_free(argv
, sizeof (ctf_id_t
) * fi
.ctc_argc
);
1672 return (ctf_dwmap_add(cdp
, *idp
, die
, B_FALSE
));
1676 ctf_dwarf_convert_type(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_id_t
*idp
,
1682 ctf_dwmap_t lookup
, *map
;
1688 if ((ret
= ctf_dwarf_offset(cdp
, die
, &offset
)) != 0)
1691 if (offset
> cdp
->cd_maxoff
) {
1692 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
1693 "die offset %llu beyond maximum for header %llu\n",
1694 offset
, cdp
->cd_maxoff
);
1695 return (ECTF_CONVBKERR
);
1699 * If we've already added an entry for this offset, then we're done.
1701 lookup
.cdm_off
= offset
;
1702 if ((map
= avl_find(&cdp
->cd_map
, &lookup
, NULL
)) != NULL
) {
1707 if ((ret
= ctf_dwarf_tag(cdp
, die
, &tag
)) != 0)
1712 case DW_TAG_base_type
:
1713 ctf_dprintf("base\n");
1714 ret
= ctf_dwarf_create_base(cdp
, die
, idp
, isroot
, offset
);
1716 case DW_TAG_array_type
:
1717 ctf_dprintf("array\n");
1718 ret
= ctf_dwarf_create_array(cdp
, die
, idp
, isroot
);
1720 case DW_TAG_enumeration_type
:
1721 ctf_dprintf("enum\n");
1722 ret
= ctf_dwarf_create_enum(cdp
, die
, idp
, isroot
);
1724 case DW_TAG_pointer_type
:
1725 ctf_dprintf("pointer\n");
1726 ret
= ctf_dwarf_create_reference(cdp
, die
, idp
, CTF_K_POINTER
,
1729 case DW_TAG_structure_type
:
1730 ctf_dprintf("struct\n");
1731 ret
= ctf_dwarf_create_sou(cdp
, die
, idp
, CTF_K_STRUCT
,
1734 case DW_TAG_subroutine_type
:
1735 ctf_dprintf("fptr\n");
1736 ret
= ctf_dwarf_create_fptr(cdp
, die
, idp
, isroot
);
1738 case DW_TAG_typedef
:
1739 ctf_dprintf("typedef\n");
1740 ret
= ctf_dwarf_create_reference(cdp
, die
, idp
, CTF_K_TYPEDEF
,
1743 case DW_TAG_union_type
:
1744 ctf_dprintf("union\n");
1745 ret
= ctf_dwarf_create_sou(cdp
, die
, idp
, CTF_K_UNION
,
1748 case DW_TAG_const_type
:
1749 ctf_dprintf("const\n");
1750 ret
= ctf_dwarf_create_reference(cdp
, die
, idp
, CTF_K_CONST
,
1753 case DW_TAG_volatile_type
:
1754 ctf_dprintf("volatile\n");
1755 ret
= ctf_dwarf_create_reference(cdp
, die
, idp
, CTF_K_VOLATILE
,
1758 case DW_TAG_restrict_type
:
1759 ctf_dprintf("restrict\n");
1760 ret
= ctf_dwarf_create_reference(cdp
, die
, idp
, CTF_K_RESTRICT
,
1764 ctf_dprintf("ignoring tag type %x\n", tag
);
1768 ctf_dprintf("ctf_dwarf_convert_type tag specific handler returned %d\n",
1775 ctf_dwarf_walk_lexical(ctf_die_t
*cdp
, Dwarf_Die die
)
1780 if ((ret
= ctf_dwarf_child(cdp
, die
, &child
)) != 0)
1786 return (ctf_dwarf_convert_die(cdp
, die
));
1790 ctf_dwarf_function_count(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_funcinfo_t
*fip
,
1794 Dwarf_Die child
, sib
, arg
;
1796 if ((ret
= ctf_dwarf_child(cdp
, die
, &child
)) != 0)
1800 while (arg
!= NULL
) {
1803 if ((ret
= ctf_dwarf_tag(cdp
, arg
, &tag
)) != 0)
1807 * We have to check for a varargs type decleration. This will
1808 * happen in one of two ways. If we have a function pointer
1809 * type, then it'll be done with a tag of type
1810 * DW_TAG_unspecified_parameters. However, it only means we have
1811 * a variable number of arguments, if we have more than one
1812 * argument found so far. Otherwise, when we have a function
1813 * type, it instead uses a formal parameter whose name is '...'
1814 * to indicate a variable arguments member.
1816 * Also, if we have a function pointer, then we have to expect
1817 * that we might not get a name at all.
1819 if (tag
== DW_TAG_formal_parameter
&& fptr
== B_FALSE
) {
1821 if ((ret
= ctf_dwarf_string(cdp
, die
, DW_AT_name
,
1824 if (strcmp(name
, DWARF_VARARGS_NAME
) == 0)
1825 fip
->ctc_flags
|= CTF_FUNC_VARARG
;
1828 ctf_free(name
, strlen(name
) + 1);
1829 } else if (tag
== DW_TAG_formal_parameter
) {
1831 } else if (tag
== DW_TAG_unspecified_parameters
&&
1832 fip
->ctc_argc
> 0) {
1833 fip
->ctc_flags
|= CTF_FUNC_VARARG
;
1835 if ((ret
= ctf_dwarf_sib(cdp
, arg
, &sib
)) != 0)
1844 ctf_dwarf_convert_fargs(ctf_die_t
*cdp
, Dwarf_Die die
, ctf_funcinfo_t
*fip
,
1849 Dwarf_Die child
, sib
, arg
;
1851 if ((ret
= ctf_dwarf_child(cdp
, die
, &child
)) != 0)
1855 while (arg
!= NULL
) {
1858 if ((ret
= ctf_dwarf_tag(cdp
, arg
, &tag
)) != 0)
1860 if (tag
== DW_TAG_formal_parameter
) {
1863 if ((ret
= ctf_dwarf_refdie(cdp
, arg
, DW_AT_type
,
1867 if ((ret
= ctf_dwarf_convert_type(cdp
, tdie
, &argv
[i
],
1868 CTF_ADD_ROOT
)) != 0)
1873 * Once we hit argc entries, we're done. This ensures we
1874 * don't accidentally hit a varargs which should be the
1877 if (i
== fip
->ctc_argc
)
1881 if ((ret
= ctf_dwarf_sib(cdp
, arg
, &sib
)) != 0)
1890 ctf_dwarf_convert_function(ctf_die_t
*cdp
, Dwarf_Die die
)
1898 * Functions that don't have a name are generally functions that have
1899 * been inlined and thus most information about them has been lost. If
1900 * we can't get a name, then instead of returning ENOENT, we silently
1901 * swallow the error.
1903 if ((ret
= ctf_dwarf_string(cdp
, die
, DW_AT_name
, &name
)) != 0) {
1909 ctf_dprintf("beginning work on function %s\n", name
);
1910 if ((cdf
= ctf_alloc(sizeof (ctf_dwfunc_t
))) == NULL
) {
1911 ctf_free(name
, strlen(name
) + 1);
1914 bzero(cdf
, sizeof (ctf_dwfunc_t
));
1915 cdf
->cdf_name
= name
;
1917 if ((ret
= ctf_dwarf_refdie(cdp
, die
, DW_AT_type
, &tdie
)) == 0) {
1918 if ((ret
= ctf_dwarf_convert_type(cdp
, tdie
,
1919 &(cdf
->cdf_fip
.ctc_return
), CTF_ADD_ROOT
)) != 0) {
1920 ctf_free(name
, strlen(name
) + 1);
1921 ctf_free(cdf
, sizeof (ctf_dwfunc_t
));
1924 } else if (ret
!= ENOENT
) {
1925 ctf_free(name
, strlen(name
) + 1);
1926 ctf_free(cdf
, sizeof (ctf_dwfunc_t
));
1929 if ((cdf
->cdf_fip
.ctc_return
= ctf_dwarf_void(cdp
)) ==
1931 ctf_free(name
, strlen(name
) + 1);
1932 ctf_free(cdf
, sizeof (ctf_dwfunc_t
));
1933 return (ctf_errno(cdp
->cd_ctfp
));
1938 * A function has a number of children, some of which may not be ones we
1939 * care about. Children that we care about have a type of
1940 * DW_TAG_formal_parameter. We're going to do two passes, the first to
1941 * count the arguments, the second to process them. Afterwards, we
1942 * should be good to go ahead and add this function.
1944 * Note, we already got the return type by going in and grabbing it out
1945 * of the DW_AT_type.
1947 if ((ret
= ctf_dwarf_function_count(cdp
, die
, &cdf
->cdf_fip
,
1949 ctf_free(name
, strlen(name
) + 1);
1950 ctf_free(cdf
, sizeof (ctf_dwfunc_t
));
1954 ctf_dprintf("beginning to convert function arguments %s\n", name
);
1955 if (cdf
->cdf_fip
.ctc_argc
!= 0) {
1956 uint_t argc
= cdf
->cdf_fip
.ctc_argc
;
1957 cdf
->cdf_argv
= ctf_alloc(sizeof (ctf_id_t
) * argc
);
1958 if (cdf
->cdf_argv
== NULL
) {
1959 ctf_free(name
, strlen(name
) + 1);
1960 ctf_free(cdf
, sizeof (ctf_dwfunc_t
));
1963 if ((ret
= ctf_dwarf_convert_fargs(cdp
, die
,
1964 &cdf
->cdf_fip
, cdf
->cdf_argv
)) != 0) {
1965 ctf_free(cdf
->cdf_argv
, sizeof (ctf_id_t
) * argc
);
1966 ctf_free(name
, strlen(name
) + 1);
1967 ctf_free(cdf
, sizeof (ctf_dwfunc_t
));
1971 cdf
->cdf_argv
= NULL
;
1974 if ((ret
= ctf_dwarf_isglobal(cdp
, die
, &cdf
->cdf_global
)) != 0) {
1975 ctf_free(cdf
->cdf_argv
, sizeof (ctf_id_t
) *
1976 cdf
->cdf_fip
.ctc_argc
);
1977 ctf_free(name
, strlen(name
) + 1);
1978 ctf_free(cdf
, sizeof (ctf_dwfunc_t
));
1982 ctf_list_append(&cdp
->cd_funcs
, cdf
);
1987 * Convert variables, but only if they're not prototypes and have names.
1990 ctf_dwarf_convert_variable(ctf_die_t
*cdp
, Dwarf_Die die
)
1999 if ((ret
= ctf_dwarf_boolean(cdp
, die
, DW_AT_declaration
, &b
)) != 0) {
2002 } else if (b
!= 0) {
2006 if ((ret
= ctf_dwarf_string(cdp
, die
, DW_AT_name
, &name
)) != 0 &&
2012 if ((ret
= ctf_dwarf_refdie(cdp
, die
, DW_AT_type
, &tdie
)) != 0) {
2013 ctf_free(name
, strlen(name
) + 1);
2017 if ((ret
= ctf_dwarf_convert_type(cdp
, tdie
, &id
,
2018 CTF_ADD_ROOT
)) != 0)
2021 if ((cdv
= ctf_alloc(sizeof (ctf_dwvar_t
))) == NULL
) {
2022 ctf_free(name
, strlen(name
) + 1);
2026 cdv
->cdv_name
= name
;
2029 if ((ret
= ctf_dwarf_isglobal(cdp
, die
, &cdv
->cdv_global
)) != 0) {
2030 ctf_free(cdv
, sizeof (ctf_dwvar_t
));
2031 ctf_free(name
, strlen(name
) + 1);
2035 ctf_list_append(&cdp
->cd_vars
, cdv
);
2040 * Walk through our set of top-level types and process them.
2043 ctf_dwarf_walk_toplevel(ctf_die_t
*cdp
, Dwarf_Die die
)
2049 if ((ret
= ctf_dwarf_offset(cdp
, die
, &offset
)) != 0)
2052 if (offset
> cdp
->cd_maxoff
) {
2053 (void) snprintf(cdp
->cd_errbuf
, cdp
->cd_errlen
,
2054 "die offset %llu beyond maximum for header %llu\n",
2055 offset
, cdp
->cd_maxoff
);
2056 return (ECTF_CONVBKERR
);
2059 if ((ret
= ctf_dwarf_tag(cdp
, die
, &tag
)) != 0)
2064 case DW_TAG_subprogram
:
2065 ctf_dprintf("top level func\n");
2066 ret
= ctf_dwarf_convert_function(cdp
, die
);
2068 case DW_TAG_variable
:
2069 ctf_dprintf("top level var\n");
2070 ret
= ctf_dwarf_convert_variable(cdp
, die
);
2072 case DW_TAG_lexical_block
:
2073 ctf_dprintf("top level block\n");
2074 ret
= ctf_dwarf_walk_lexical(cdp
, die
);
2076 case DW_TAG_enumeration_type
:
2077 case DW_TAG_structure_type
:
2078 case DW_TAG_typedef
:
2079 case DW_TAG_union_type
:
2080 ctf_dprintf("top level type\n");
2081 ret
= ctf_dwarf_convert_type(cdp
, die
, NULL
, B_TRUE
);
2092 * We're given a node. At this node we need to convert it and then proceed to
2093 * convert any siblings that are associaed with this die.
2096 ctf_dwarf_convert_die(ctf_die_t
*cdp
, Dwarf_Die die
)
2098 while (die
!= NULL
) {
2102 if ((ret
= ctf_dwarf_walk_toplevel(cdp
, die
)) != 0)
2105 if ((ret
= ctf_dwarf_sib(cdp
, die
, &sib
)) != 0)
2113 ctf_dwarf_fixup_die(ctf_die_t
*cdp
, boolean_t addpass
)
2117 for (map
= avl_first(&cdp
->cd_map
); map
!= NULL
;
2118 map
= AVL_NEXT(&cdp
->cd_map
, map
)) {
2120 if (map
->cdm_fix
== B_FALSE
)
2122 if ((ret
= ctf_dwarf_fixup_sou(cdp
, map
->cdm_die
, map
->cdm_id
,
2130 static ctf_dwfunc_t
*
2131 ctf_dwarf_match_func(ctf_die_t
*cdp
, const char *file
, const char *name
,
2136 if (bind
== STB_WEAK
)
2139 /* Nothing we can do if we can't find a name to compare it to. */
2140 if (bind
== STB_LOCAL
&& (file
== NULL
|| cdp
->cd_name
== NULL
))
2143 for (cdf
= ctf_list_next(&cdp
->cd_funcs
); cdf
!= NULL
;
2144 cdf
= ctf_list_next(cdf
)) {
2145 if (bind
== STB_GLOBAL
&& cdf
->cdf_global
== B_FALSE
)
2147 if (bind
== STB_LOCAL
&& cdf
->cdf_global
== B_TRUE
)
2149 if (strcmp(name
, cdf
->cdf_name
) != 0)
2151 if (bind
== STB_LOCAL
&& strcmp(file
, cdp
->cd_name
) != 0)
2158 static ctf_dwvar_t
*
2159 ctf_dwarf_match_var(ctf_die_t
*cdp
, const char *file
, const char *name
,
2164 /* Nothing we can do if we can't find a name to compare it to. */
2165 if (bind
== STB_LOCAL
&& (file
== NULL
|| cdp
->cd_name
== NULL
))
2167 ctf_dprintf("Still considering %s\n", name
);
2169 for (cdv
= ctf_list_next(&cdp
->cd_vars
); cdv
!= NULL
;
2170 cdv
= ctf_list_next(cdv
)) {
2171 if (bind
== STB_GLOBAL
&& cdv
->cdv_global
== B_FALSE
)
2173 if (bind
== STB_LOCAL
&& cdv
->cdv_global
== B_TRUE
)
2175 if (strcmp(name
, cdv
->cdv_name
) != 0)
2177 if (bind
== STB_LOCAL
&& strcmp(file
, cdp
->cd_name
) != 0)
2186 ctf_dwarf_symtab_iter(ctf_die_t
*cdp
, ctf_dwarf_symtab_f
*func
, void *arg
)
2190 ctf_file_t
*fp
= cdp
->cd_ctfp
;
2191 const char *file
= NULL
;
2192 uintptr_t symbase
= (uintptr_t)fp
->ctf_symtab
.cts_data
;
2193 uintptr_t strbase
= (uintptr_t)fp
->ctf_strtab
.cts_data
;
2195 for (i
= 0; i
< fp
->ctf_nsyms
; i
++) {
2199 const GElf_Sym
*gsymp
;
2201 if (fp
->ctf_symtab
.cts_entsize
== sizeof (Elf32_Sym
)) {
2202 const Elf32_Sym
*symp
= (Elf32_Sym
*)symbase
+ i
;
2203 type
= ELF32_ST_TYPE(symp
->st_info
);
2204 if (type
== STT_FILE
) {
2205 file
= (char *)(strbase
+ symp
->st_name
);
2208 if (type
!= STT_OBJECT
&& type
!= STT_FUNC
)
2210 if (ctf_sym_valid(strbase
, type
, symp
->st_shndx
,
2211 symp
->st_value
, symp
->st_name
) == B_FALSE
)
2213 name
= (char *)(strbase
+ symp
->st_name
);
2214 gsym
.st_name
= symp
->st_name
;
2215 gsym
.st_value
= symp
->st_value
;
2216 gsym
.st_size
= symp
->st_size
;
2217 gsym
.st_info
= symp
->st_info
;
2218 gsym
.st_other
= symp
->st_other
;
2219 gsym
.st_shndx
= symp
->st_shndx
;
2222 const Elf64_Sym
*symp
= (Elf64_Sym
*)symbase
+ i
;
2223 type
= ELF64_ST_TYPE(symp
->st_info
);
2224 if (type
== STT_FILE
) {
2225 file
= (char *)(strbase
+ symp
->st_name
);
2228 if (type
!= STT_OBJECT
&& type
!= STT_FUNC
)
2230 if (ctf_sym_valid(strbase
, type
, symp
->st_shndx
,
2231 symp
->st_value
, symp
->st_name
) == B_FALSE
)
2233 name
= (char *)(strbase
+ symp
->st_name
);
2237 ret
= func(cdp
, gsymp
, i
, file
, name
, arg
);
2247 ctf_dwarf_conv_funcvars_cb(ctf_die_t
*cdp
, const GElf_Sym
*symp
, ulong_t idx
,
2248 const char *file
, const char *name
, void *arg
)
2250 int ret
, bind
, type
;
2252 bind
= GELF_ST_BIND(symp
->st_info
);
2253 type
= GELF_ST_TYPE(symp
->st_info
);
2256 * Come back to weak symbols in another pass
2258 if (bind
== STB_WEAK
)
2261 if (type
== STT_OBJECT
) {
2262 ctf_dwvar_t
*cdv
= ctf_dwarf_match_var(cdp
, file
, name
,
2264 ctf_dprintf("match for %s (%d): %p\n", name
, idx
, cdv
);
2267 ret
= ctf_add_object(cdp
->cd_ctfp
, idx
, cdv
->cdv_type
);
2268 ctf_dprintf("added object %s\n", name
);
2270 ctf_dwfunc_t
*cdf
= ctf_dwarf_match_func(cdp
, file
, name
,
2274 ret
= ctf_add_function(cdp
->cd_ctfp
, idx
, &cdf
->cdf_fip
,
2278 if (ret
== CTF_ERR
) {
2279 return (ctf_errno(cdp
->cd_ctfp
));
2286 ctf_dwarf_conv_funcvars(ctf_die_t
*cdp
)
2288 return (ctf_dwarf_symtab_iter(cdp
, ctf_dwarf_conv_funcvars_cb
, NULL
));
2292 * Note, this comment comes from the original version of the CTF tools.
2294 * If we have a weak symbol, attempt to find the strong symbol it will
2295 * resolve to. Note: the code where this actually happens is in
2296 * sym_process() in cmd/sgs/libld/common/syms.c
2298 * Finding the matching symbol is unfortunately not trivial. For a
2299 * symbol to be a candidate, it must:
2301 * - have the same type (function, object)
2302 * - have the same value (address)
2303 * - have the same size
2304 * - not be another weak symbol
2305 * - belong to the same section (checked via section index)
2307 * If such a candidate is global, then we assume we've found it. The
2308 * linker generates the symbol table such that the curfile might be
2309 * incorrect; this is OK for global symbols, since find_iidesc() doesn't
2310 * need to check for the source file for the symbol.
2312 * We might have found a strong local symbol, where the curfile is
2313 * accurate and matches that of the weak symbol. We assume this is a
2316 * If we've got a local symbol with a non-matching curfile, there are
2317 * two possibilities. Either this is a completely different symbol, or
2318 * it's a once-global symbol that was scoped to local via a mapfile. In
2319 * the latter case, curfile is likely inaccurate since the linker does
2320 * not preserve the needed curfile in the order of the symbol table (see
2321 * the comments about locally scoped symbols in libld's update_osym()).
2322 * As we can't tell this case from the former one, we use this symbol
2323 * iff no other matching symbol is found.
2325 * What we really need here is a SUNW section containing weak<->strong
2326 * mappings that we can consume.
2328 typedef struct ctf_dwarf_weak_arg
{
2329 const GElf_Sym
*cweak_symp
;
2330 const char *cweak_file
;
2331 boolean_t cweak_candidate
;
2333 } ctf_dwarf_weak_arg_t
;
2337 ctf_dwarf_conv_check_weak(ctf_die_t
*cdp
, const GElf_Sym
*symp
,
2338 ulong_t idx
, const char *file
, const char *name
, void *arg
)
2340 ctf_dwarf_weak_arg_t
*cweak
= arg
;
2341 const GElf_Sym
*wsymp
= cweak
->cweak_symp
;
2343 ctf_dprintf("comparing weak to %s\n", name
);
2345 if (GELF_ST_BIND(symp
->st_info
) == STB_WEAK
) {
2349 if (GELF_ST_TYPE(wsymp
->st_info
) != GELF_ST_TYPE(symp
->st_info
)) {
2353 if (wsymp
->st_value
!= symp
->st_value
) {
2357 if (wsymp
->st_size
!= symp
->st_size
) {
2361 if (wsymp
->st_shndx
!= symp
->st_shndx
) {
2366 * Check if it's a weak candidate.
2368 if (GELF_ST_BIND(symp
->st_info
) == STB_LOCAL
&&
2369 (file
== NULL
|| cweak
->cweak_file
== NULL
||
2370 strcmp(file
, cweak
->cweak_file
) != 0)) {
2371 cweak
->cweak_candidate
= B_TRUE
;
2372 cweak
->cweak_idx
= idx
;
2377 * Found a match, break.
2379 cweak
->cweak_idx
= idx
;
2384 ctf_dwarf_duplicate_sym(ctf_die_t
*cdp
, ulong_t idx
, ulong_t matchidx
)
2386 ctf_id_t id
= ctf_lookup_by_symbol(cdp
->cd_ctfp
, matchidx
);
2389 * If we matched something that for some reason didn't have type data,
2390 * we don't consider that a fatal error and silently swallow it.
2392 if (id
== CTF_ERR
) {
2393 if (ctf_errno(cdp
->cd_ctfp
) == ECTF_NOTYPEDAT
)
2396 return (ctf_errno(cdp
->cd_ctfp
));
2399 if (ctf_add_object(cdp
->cd_ctfp
, idx
, id
) == CTF_ERR
)
2400 return (ctf_errno(cdp
->cd_ctfp
));
2406 ctf_dwarf_duplicate_func(ctf_die_t
*cdp
, ulong_t idx
, ulong_t matchidx
)
2410 ctf_id_t
*args
= NULL
;
2412 if (ctf_func_info(cdp
->cd_ctfp
, matchidx
, &fip
) == CTF_ERR
) {
2413 if (ctf_errno(cdp
->cd_ctfp
) == ECTF_NOFUNCDAT
)
2416 return (ctf_errno(cdp
->cd_ctfp
));
2419 if (fip
.ctc_argc
!= 0) {
2420 args
= ctf_alloc(sizeof (ctf_id_t
) * fip
.ctc_argc
);
2424 if (ctf_func_args(cdp
->cd_ctfp
, matchidx
, fip
.ctc_argc
, args
) ==
2426 ctf_free(args
, sizeof (ctf_id_t
) * fip
.ctc_argc
);
2427 return (ctf_errno(cdp
->cd_ctfp
));
2431 ret
= ctf_add_function(cdp
->cd_ctfp
, idx
, &fip
, args
);
2433 ctf_free(args
, sizeof (ctf_id_t
) * fip
.ctc_argc
);
2435 return (ctf_errno(cdp
->cd_ctfp
));
2442 ctf_dwarf_conv_weaks_cb(ctf_die_t
*cdp
, const GElf_Sym
*symp
,
2443 ulong_t idx
, const char *file
, const char *name
, void *arg
)
2446 ctf_dwarf_weak_arg_t cweak
;
2449 * We only care about weak symbols.
2451 if (GELF_ST_BIND(symp
->st_info
) != STB_WEAK
)
2454 type
= GELF_ST_TYPE(symp
->st_info
);
2455 ASSERT(type
== STT_OBJECT
|| type
== STT_FUNC
);
2458 * For each weak symbol we encounter, we need to do a second iteration
2459 * to try and find a match. We should probably think about other
2460 * techniques to try and save us time in the future.
2462 cweak
.cweak_symp
= symp
;
2463 cweak
.cweak_file
= file
;
2464 cweak
.cweak_candidate
= B_FALSE
;
2465 cweak
.cweak_idx
= 0;
2467 ctf_dprintf("Trying to find weak equiv for %s\n", name
);
2469 ret
= ctf_dwarf_symtab_iter(cdp
, ctf_dwarf_conv_check_weak
, &cweak
);
2470 VERIFY(ret
== 0 || ret
== 1);
2473 * Nothing was ever found, we're not going to add anything for this
2476 if (ret
== 0 && cweak
.cweak_candidate
== B_FALSE
) {
2477 ctf_dprintf("found no weak match for %s\n", name
);
2482 * Now, finally go and add the type based on the match.
2484 if (type
== STT_OBJECT
) {
2485 ret
= ctf_dwarf_duplicate_sym(cdp
, idx
, cweak
.cweak_idx
);
2487 ret
= ctf_dwarf_duplicate_func(cdp
, idx
, cweak
.cweak_idx
);
2494 ctf_dwarf_conv_weaks(ctf_die_t
*cdp
)
2496 return (ctf_dwarf_symtab_iter(cdp
, ctf_dwarf_conv_weaks_cb
, NULL
));
2501 ctf_dwarf_convert_one(void *arg
, void *unused
)
2505 ctf_die_t
*cdp
= arg
;
2507 ctf_dprintf("converting die: %s\n", cdp
->cd_name
);
2508 ctf_dprintf("max offset: %x\n", cdp
->cd_maxoff
);
2509 VERIFY(cdp
!= NULL
);
2511 ret
= ctf_dwarf_convert_die(cdp
, cdp
->cd_cu
);
2512 ctf_dprintf("ctf_dwarf_convert_die (%s) returned %d\n", cdp
->cd_name
,
2517 if (ctf_update(cdp
->cd_ctfp
) != 0) {
2518 return (ctf_dwarf_error(cdp
, cdp
->cd_ctfp
, 0,
2519 "failed to update output ctf container"));
2522 ret
= ctf_dwarf_fixup_die(cdp
, B_FALSE
);
2523 ctf_dprintf("ctf_dwarf_fixup_die (%s) returned %d\n", cdp
->cd_name
,
2528 if (ctf_update(cdp
->cd_ctfp
) != 0) {
2529 return (ctf_dwarf_error(cdp
, cdp
->cd_ctfp
, 0,
2530 "failed to update output ctf container"));
2533 ret
= ctf_dwarf_fixup_die(cdp
, B_TRUE
);
2534 ctf_dprintf("ctf_dwarf_fixup_die (%s) returned %d\n", cdp
->cd_name
,
2539 if (ctf_update(cdp
->cd_ctfp
) != 0) {
2540 return (ctf_dwarf_error(cdp
, cdp
->cd_ctfp
, 0,
2541 "failed to update output ctf container"));
2545 if ((ret
= ctf_dwarf_conv_funcvars(cdp
)) != 0) {
2546 return (ctf_dwarf_error(cdp
, NULL
, ret
,
2547 "failed to convert strong functions and variables"));
2550 if (ctf_update(cdp
->cd_ctfp
) != 0) {
2551 return (ctf_dwarf_error(cdp
, cdp
->cd_ctfp
, 0,
2552 "failed to update output ctf container"));
2555 if (cdp
->cd_doweaks
== B_TRUE
) {
2556 if ((ret
= ctf_dwarf_conv_weaks(cdp
)) != 0) {
2557 return (ctf_dwarf_error(cdp
, NULL
, ret
,
2558 "failed to convert weak functions and variables"));
2561 if (ctf_update(cdp
->cd_ctfp
) != 0) {
2562 return (ctf_dwarf_error(cdp
, cdp
->cd_ctfp
, 0,
2563 "failed to update output ctf container"));
2567 ctf_phase_dump(cdp
->cd_ctfp
, "pre-dedup");
2568 ctf_dprintf("adding inputs for dedup\n");
2569 if ((ret
= ctf_merge_add(cdp
->cd_cmh
, cdp
->cd_ctfp
)) != 0) {
2570 return (ctf_dwarf_error(cdp
, NULL
, ret
,
2571 "failed to add inputs for merge"));
2574 ctf_dprintf("starting merge\n");
2575 if ((ret
= ctf_merge_dedup(cdp
->cd_cmh
, &dedup
)) != 0) {
2576 return (ctf_dwarf_error(cdp
, NULL
, ret
,
2577 "failed to deduplicate die"));
2579 ctf_close(cdp
->cd_ctfp
);
2580 cdp
->cd_ctfp
= dedup
;
2586 * Note, we expect that if we're returning a ctf_file_t from one of the dies,
2587 * say in the single node case, it's been saved and the entry here has been set
2588 * to NULL, which ctf_close happily ignores.
2591 ctf_dwarf_free_die(ctf_die_t
*cdp
)
2593 ctf_dwfunc_t
*cdf
, *ndf
;
2594 ctf_dwvar_t
*cdv
, *ndv
;
2595 ctf_dwbitf_t
*cdb
, *ndb
;
2600 ctf_dprintf("Beginning to free die: %p\n", cdp
);
2602 ctf_dprintf("Trying to free name: %p\n", cdp
->cd_name
);
2603 if (cdp
->cd_name
!= NULL
)
2604 ctf_free(cdp
->cd_name
, strlen(cdp
->cd_name
) + 1);
2605 ctf_dprintf("Trying to free merge handle: %p\n", cdp
->cd_cmh
);
2606 if (cdp
->cd_cmh
!= NULL
) {
2607 ctf_merge_fini(cdp
->cd_cmh
);
2611 ctf_dprintf("Trying to free functions\n");
2612 for (cdf
= ctf_list_next(&cdp
->cd_funcs
); cdf
!= NULL
; cdf
= ndf
) {
2613 ndf
= ctf_list_next(cdf
);
2614 ctf_free(cdf
->cdf_name
, strlen(cdf
->cdf_name
) + 1);
2615 if (cdf
->cdf_fip
.ctc_argc
!= 0) {
2616 ctf_free(cdf
->cdf_argv
,
2617 sizeof (ctf_id_t
) * cdf
->cdf_fip
.ctc_argc
);
2619 ctf_free(cdf
, sizeof (ctf_dwfunc_t
));
2622 ctf_dprintf("Trying to free variables\n");
2623 for (cdv
= ctf_list_next(&cdp
->cd_vars
); cdv
!= NULL
; cdv
= ndv
) {
2624 ndv
= ctf_list_next(cdv
);
2625 ctf_free(cdv
->cdv_name
, strlen(cdv
->cdv_name
) + 1);
2626 ctf_free(cdv
, sizeof (ctf_dwvar_t
));
2629 ctf_dprintf("Trying to free bitfields\n");
2630 for (cdb
= ctf_list_next(&cdp
->cd_bitfields
); cdb
!= NULL
; cdb
= ndb
) {
2631 ndb
= ctf_list_next(cdb
);
2632 ctf_free(cdb
, sizeof (ctf_dwbitf_t
));
2635 /* How do we clean up die usage? */
2636 ctf_dprintf("Trying to clean up dwarf_t: %p\n", cdp
->cd_dwarf
);
2637 (void) dwarf_finish(cdp
->cd_dwarf
, &derr
);
2638 cdp
->cd_dwarf
= NULL
;
2639 ctf_close(cdp
->cd_ctfp
);
2642 while ((map
= avl_destroy_nodes(&cdp
->cd_map
, &cookie
)) != NULL
) {
2643 ctf_free(map
, sizeof (ctf_dwmap_t
));
2645 avl_destroy(&cdp
->cd_map
);
2646 cdp
->cd_errbuf
= NULL
;
2650 ctf_dwarf_free_dies(ctf_die_t
*cdies
, int ndies
)
2654 ctf_dprintf("Beginning to free dies\n");
2655 for (i
= 0; i
< ndies
; i
++) {
2656 ctf_dwarf_free_die(&cdies
[i
]);
2659 ctf_free(cdies
, sizeof (ctf_die_t
) * ndies
);
2663 ctf_dwarf_count_dies(Dwarf_Debug dw
, Dwarf_Error
*derr
, int *ndies
,
2664 char *errbuf
, size_t errlen
)
2668 Dwarf_Unsigned nexthdr
;
2670 while ((ret
= dwarf_next_cu_header(dw
, NULL
, &vers
, NULL
, NULL
,
2671 &nexthdr
, derr
)) != DW_DLV_NO_ENTRY
) {
2672 if (ret
!= DW_DLV_OK
) {
2673 (void) snprintf(errbuf
, errlen
,
2674 "file does not contain valid DWARF data: %s\n",
2675 dwarf_errmsg(*derr
));
2676 return (ECTF_CONVBKERR
);
2679 if (vers
!= DWARF_VERSION_TWO
) {
2680 (void) snprintf(errbuf
, errlen
,
2681 "unsupported DWARF version: %d\n", vers
);
2682 return (ECTF_CONVBKERR
);
2684 *ndies
= *ndies
+ 1;
2688 (void) snprintf(errbuf
, errlen
,
2689 "file does not contain valid DWARF data: %s\n",
2690 dwarf_errmsg(*derr
));
2691 return (ECTF_CONVBKERR
);
2698 * Iterate over all of the dies and create a ctf_die_t for each of them. This is
2699 * used to determine if we have zero, one, or multiple dies to convert. If we
2700 * have zero, that's an error. If there's only one die, that's the simple case.
2701 * No merge needed and only a single Dwarf_Debug as well.
2704 ctf_dwarf_init_die(int fd
, Elf
*elf
, ctf_die_t
*cdp
, int ndie
, char *errbuf
,
2708 Dwarf_Unsigned hdrlen
, abboff
, nexthdr
;
2710 Dwarf_Unsigned offset
= 0;
2713 while ((ret
= dwarf_next_cu_header(cdp
->cd_dwarf
, &hdrlen
, NULL
,
2714 &abboff
, &addrsz
, &nexthdr
, &derr
)) != DW_DLV_NO_ENTRY
) {
2716 Dwarf_Die cu
, child
;
2718 /* Based on the counting above, we should be good to go */
2719 VERIFY(ret
== DW_DLV_OK
);
2727 * Compilers are apparently inconsistent. Some emit no DWARF for
2728 * empty files and others emit empty compilation unit.
2730 cdp
->cd_voidtid
= CTF_ERR
;
2731 cdp
->cd_longtid
= CTF_ERR
;
2733 cdp
->cd_maxoff
= nexthdr
- 1;
2734 cdp
->cd_ctfp
= ctf_fdcreate(fd
, &ret
);
2735 if (cdp
->cd_ctfp
== NULL
) {
2736 ctf_free(cdp
, sizeof (ctf_die_t
));
2739 avl_create(&cdp
->cd_map
, ctf_dwmap_comp
, sizeof (ctf_dwmap_t
),
2740 offsetof(ctf_dwmap_t
, cdm_avl
));
2741 cdp
->cd_errbuf
= errbuf
;
2742 cdp
->cd_errlen
= errlen
;
2743 bzero(&cdp
->cd_vars
, sizeof (ctf_list_t
));
2744 bzero(&cdp
->cd_funcs
, sizeof (ctf_list_t
));
2745 bzero(&cdp
->cd_bitfields
, sizeof (ctf_list_t
));
2747 if ((ret
= ctf_dwarf_die_elfenc(elf
, cdp
, errbuf
,
2749 avl_destroy(&cdp
->cd_map
);
2750 ctf_free(cdp
, sizeof (ctf_die_t
));
2754 if ((ret
= ctf_dwarf_sib(cdp
, NULL
, &cu
)) != 0) {
2755 avl_destroy(&cdp
->cd_map
);
2756 ctf_free(cdp
, sizeof (ctf_die_t
));
2760 (void) snprintf(errbuf
, errlen
,
2761 "file does not contain DWARF data\n");
2762 avl_destroy(&cdp
->cd_map
);
2763 ctf_free(cdp
, sizeof (ctf_die_t
));
2764 return (ECTF_CONVBKERR
);
2767 if ((ret
= ctf_dwarf_child(cdp
, cu
, &child
)) != 0) {
2768 avl_destroy(&cdp
->cd_map
);
2769 ctf_free(cdp
, sizeof (ctf_die_t
));
2772 if (child
== NULL
) {
2773 (void) snprintf(errbuf
, errlen
,
2774 "file does not contain DWARF data\n");
2775 avl_destroy(&cdp
->cd_map
);
2776 ctf_free(cdp
, sizeof (ctf_die_t
));
2777 return (ECTF_CONVBKERR
);
2780 cdp
->cd_cuoff
= offset
;
2783 if ((cdp
->cd_cmh
= ctf_merge_init(fd
, &ret
)) == NULL
) {
2784 avl_destroy(&cdp
->cd_map
);
2785 ctf_free(cdp
, sizeof (ctf_die_t
));
2789 if (ctf_dwarf_string(cdp
, cu
, DW_AT_name
, &name
) == 0) {
2790 size_t len
= strlen(name
) + 1;
2791 char *b
= basename(name
);
2792 cdp
->cd_name
= strdup(b
);
2793 ctf_free(name
, len
);
2803 ctf_dwarf_convert(int fd
, Elf
*elf
, uint_t nthrs
, int *errp
, ctf_file_t
**fpp
,
2804 char *errmsg
, size_t errlen
)
2806 int err
, ret
, ndies
, i
;
2809 ctf_die_t
*cdies
= NULL
, *cdp
;
2810 workq_t
*wqp
= NULL
;
2817 ret
= dwarf_elf_init(elf
, DW_DLC_READ
, NULL
, NULL
, &dw
, &derr
);
2818 if (ret
!= DW_DLV_OK
) {
2820 * The old CTF tools used to check if we expected DWARF data
2821 * here. In this case, if we actually have some amount of DWARF,
2822 * but no section, for now, just go ahead and create an empty
2825 if (ret
== DW_DLV_NO_ENTRY
||
2826 dwarf_errno(derr
) == DW_DLE_DEBUG_INFO_NULL
) {
2827 *fpp
= ctf_create(errp
);
2828 return (*fpp
!= NULL
? CTF_CONV_SUCCESS
:
2831 (void) snprintf(errmsg
, errlen
,
2832 "failed to initialize DWARF: %s\n",
2833 dwarf_errmsg(derr
));
2834 *errp
= ECTF_CONVBKERR
;
2835 return (CTF_CONV_ERROR
);
2839 ret
= ctf_dwarf_count_dies(dw
, &derr
, &ndies
, errmsg
, errlen
);
2845 (void) dwarf_finish(dw
, &derr
);
2846 cdies
= ctf_alloc(sizeof (ctf_die_t
) * ndies
);
2847 if (cdies
== NULL
) {
2849 return (CTF_CONV_ERROR
);
2852 for (i
= 0; i
< ndies
; i
++) {
2854 ret
= dwarf_elf_init(elf
, DW_DLC_READ
, NULL
, NULL
,
2855 &cdp
->cd_dwarf
, &derr
);
2857 ctf_free(cdies
, sizeof (ctf_die_t
) * ndies
);
2858 (void) snprintf(errmsg
, errlen
,
2859 "failed to initialize DWARF: %s\n",
2860 dwarf_errmsg(derr
));
2861 *errp
= ECTF_CONVBKERR
;
2862 return (CTF_CONV_ERROR
);
2865 ret
= ctf_dwarf_init_die(fd
, elf
, &cdies
[i
], i
, errmsg
, errlen
);
2870 cdp
->cd_doweaks
= ndies
> 1 ? B_FALSE
: B_TRUE
;
2873 ctf_dprintf("found %d DWARF die(s)\n", ndies
);
2876 * If we only have one die, there's no reason to use multiple threads,
2877 * even if the user requested them. After all, they just gave us an
2883 if (workq_init(&wqp
, nthrs
) == -1) {
2888 for (i
= 0; i
< ndies
; i
++) {
2890 ctf_dprintf("adding die %s: %p, %x %x\n", cdp
->cd_name
,
2891 cdp
->cd_cu
, cdp
->cd_cuoff
, cdp
->cd_maxoff
);
2892 if (workq_add(wqp
, cdp
) == -1) {
2898 ret
= workq_work(wqp
, ctf_dwarf_convert_one
, NULL
, errp
);
2899 if (ret
== WORKQ_ERROR
) {
2902 } else if (ret
== WORKQ_UERROR
) {
2903 ctf_dprintf("internal convert failed: %s\n",
2908 ctf_dprintf("Determining next phase: have %d dies\n", ndies
);
2912 cmp
= ctf_merge_init(fd
, &ret
);
2918 ctf_dprintf("setting threads\n");
2919 if ((ret
= ctf_merge_set_nthreads(cmp
, nthrs
)) != 0) {
2920 ctf_merge_fini(cmp
);
2925 ctf_dprintf("adding dies\n");
2926 for (i
= 0; i
< ndies
; i
++) {
2928 if ((ret
= ctf_merge_add(cmp
, cdp
->cd_ctfp
)) != 0) {
2929 ctf_merge_fini(cmp
);
2935 ctf_dprintf("performing merge\n");
2936 ret
= ctf_merge_merge(cmp
, fpp
);
2938 ctf_dprintf("failed merge!\n");
2940 ctf_merge_fini(cmp
);
2944 ctf_merge_fini(cmp
);
2946 ctf_dprintf("successfully converted!\n");
2949 *fpp
= cdies
->cd_ctfp
;
2950 cdies
->cd_ctfp
= NULL
;
2951 ctf_dprintf("successfully converted!\n");
2956 ctf_dwarf_free_dies(cdies
, ndies
);
2957 return (*fpp
!= NULL
? CTF_CONV_SUCCESS
: CTF_CONV_ERROR
);