2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include <sys/param.h>
31 #define EOVERFLOW ERANGE
35 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
38 /* Make sure the ptrtab has enough space for at least one more type.
40 We start with 4KiB of ptrtab, enough for a thousand types, then grow it 25%
44 ctf_grow_ptrtab (ctf_dict_t
*fp
)
46 size_t new_ptrtab_len
= fp
->ctf_ptrtab_len
;
48 /* We allocate one more ptrtab entry than we need, for the initial zero,
49 plus one because the caller will probably allocate a new type. */
51 if (fp
->ctf_ptrtab
== NULL
)
52 new_ptrtab_len
= 1024;
53 else if ((fp
->ctf_typemax
+ 2) > fp
->ctf_ptrtab_len
)
54 new_ptrtab_len
= fp
->ctf_ptrtab_len
* 1.25;
56 if (new_ptrtab_len
!= fp
->ctf_ptrtab_len
)
60 if ((new_ptrtab
= realloc (fp
->ctf_ptrtab
,
61 new_ptrtab_len
* sizeof (uint32_t))) == NULL
)
62 return (ctf_set_errno (fp
, ENOMEM
));
64 fp
->ctf_ptrtab
= new_ptrtab
;
65 memset (fp
->ctf_ptrtab
+ fp
->ctf_ptrtab_len
, 0,
66 (new_ptrtab_len
- fp
->ctf_ptrtab_len
) * sizeof (uint32_t));
67 fp
->ctf_ptrtab_len
= new_ptrtab_len
;
72 /* To create an empty CTF dict, we just declare a zeroed header and call
73 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new dict r/w and
74 initialize the dynamic members. We start assigning type IDs at 1 because
75 type ID 0 is used as a sentinel and a not-found indicator. */
78 ctf_create (int *errp
)
80 static const ctf_header_t hdr
= { .cth_preamble
= { CTF_MAGIC
, CTF_VERSION
, 0 } };
82 ctf_dynhash_t
*dthash
;
83 ctf_dynhash_t
*dvhash
;
84 ctf_dynhash_t
*structs
= NULL
, *unions
= NULL
, *enums
= NULL
, *names
= NULL
;
85 ctf_dynhash_t
*objthash
= NULL
, *funchash
= NULL
;
90 dthash
= ctf_dynhash_create (ctf_hash_integer
, ctf_hash_eq_integer
,
94 ctf_set_open_errno (errp
, EAGAIN
);
98 dvhash
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
102 ctf_set_open_errno (errp
, EAGAIN
);
106 structs
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
108 unions
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
110 enums
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
112 names
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
114 objthash
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
116 funchash
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
118 if (!structs
|| !unions
|| !enums
|| !names
)
120 ctf_set_open_errno (errp
, EAGAIN
);
124 cts
.cts_name
= _CTF_SECTION
;
126 cts
.cts_size
= sizeof (hdr
);
129 if ((fp
= ctf_bufopen_internal (&cts
, NULL
, NULL
, NULL
, 1, errp
)) == NULL
)
132 fp
->ctf_structs
.ctn_writable
= structs
;
133 fp
->ctf_unions
.ctn_writable
= unions
;
134 fp
->ctf_enums
.ctn_writable
= enums
;
135 fp
->ctf_names
.ctn_writable
= names
;
136 fp
->ctf_objthash
= objthash
;
137 fp
->ctf_funchash
= funchash
;
138 fp
->ctf_dthash
= dthash
;
139 fp
->ctf_dvhash
= dvhash
;
141 fp
->ctf_snapshots
= 1;
142 fp
->ctf_snapshot_lu
= 0;
143 fp
->ctf_flags
|= LCTF_DIRTY
;
145 ctf_set_ctl_hashes (fp
);
146 ctf_setmodel (fp
, CTF_MODEL_NATIVE
);
147 if (ctf_grow_ptrtab (fp
) < 0)
149 ctf_set_open_errno (errp
, ctf_errno (fp
));
157 ctf_dynhash_destroy (structs
);
158 ctf_dynhash_destroy (unions
);
159 ctf_dynhash_destroy (enums
);
160 ctf_dynhash_destroy (names
);
161 ctf_dynhash_destroy (objthash
);
162 ctf_dynhash_destroy (funchash
);
163 ctf_dynhash_destroy (dvhash
);
165 ctf_dynhash_destroy (dthash
);
170 /* Delete data symbols that have been assigned names from the variable section.
171 Must be called from within ctf_serialize, because that is the only place
172 you can safely delete variables without messing up ctf_rollback. */
175 symtypetab_delete_nonstatic_vars (ctf_dict_t
*fp
)
177 ctf_dvdef_t
*dvd
, *nvd
;
180 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
; dvd
= nvd
)
182 nvd
= ctf_list_next (dvd
);
184 if (((type
= (ctf_id_t
) (uintptr_t)
185 ctf_dynhash_lookup (fp
->ctf_objthash
, dvd
->dvd_name
)) > 0)
186 && type
== dvd
->dvd_type
)
187 ctf_dvd_delete (fp
, dvd
);
193 /* Determine if a symbol is "skippable" and should never appear in the
194 symtypetab sections. */
197 ctf_symtab_skippable (ctf_link_sym_t
*sym
)
199 /* Never skip symbols whose name is not yet known. */
200 if (sym
->st_nameidx_set
)
203 return (sym
->st_name
== NULL
|| sym
->st_name
[0] == 0
204 || sym
->st_shndx
== SHN_UNDEF
205 || strcmp (sym
->st_name
, "_START_") == 0
206 || strcmp (sym
->st_name
, "_END_") == 0
207 || (sym
->st_type
== STT_OBJECT
&& sym
->st_shndx
== SHN_EXTABS
208 && sym
->st_value
== 0));
211 /* Symtypetab emission flags. */
213 #define CTF_SYMTYPETAB_EMIT_FUNCTION 0x1
214 #define CTF_SYMTYPETAB_EMIT_PAD 0x2
215 #define CTF_SYMTYPETAB_FORCE_INDEXED 0x4
217 /* Get the number of symbols in a symbol hash, the count of symbols, the maximum
218 seen, the eventual size, without any padding elements, of the func/data and
219 (if generated) index sections, and the size of accumulated padding elements.
220 The linker-reported set of symbols is found in SYMFP.
222 Also figure out if any symbols need to be moved to the variable section, and
223 add them (if not already present). */
227 symtypetab_density (ctf_dict_t
*fp
, ctf_dict_t
*symfp
, ctf_dynhash_t
*symhash
,
228 size_t *count
, size_t *max
, size_t *unpadsize
,
229 size_t *padsize
, size_t *idxsize
, int flags
)
231 ctf_next_t
*i
= NULL
;
234 ctf_dynhash_t
*linker_known
= NULL
;
244 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
246 /* Make a dynhash citing only symbols reported by the linker of the
247 appropriate type, then traverse all potential-symbols we know the types
248 of, removing them from linker_known as we go. Once this is done, the
249 only symbols remaining in linker_known are symbols we don't know the
250 types of: we must emit pads for those symbols that are below the
251 maximum symbol we will emit (any beyond that are simply skipped). */
253 if ((linker_known
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
254 NULL
, NULL
)) == NULL
)
255 return (ctf_set_errno (fp
, ENOMEM
));
257 while ((err
= ctf_dynhash_cnext (symfp
->ctf_dynsyms
, &i
,
258 &name
, &ctf_sym
)) == 0)
260 ctf_link_sym_t
*sym
= (ctf_link_sym_t
*) ctf_sym
;
262 if (((flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
263 && sym
->st_type
!= STT_FUNC
)
264 || (!(flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
265 && sym
->st_type
!= STT_OBJECT
))
268 if (ctf_symtab_skippable (sym
))
271 /* This should only be true briefly before all the names are
272 finalized, long before we get this far. */
273 if (!ctf_assert (fp
, !sym
->st_nameidx_set
))
274 return -1; /* errno is set for us. */
276 if (ctf_dynhash_cinsert (linker_known
, name
, ctf_sym
) < 0)
278 ctf_dynhash_destroy (linker_known
);
279 return (ctf_set_errno (fp
, ENOMEM
));
282 if (err
!= ECTF_NEXT_END
)
284 ctf_err_warn (fp
, 0, err
, _("iterating over linker-known symbols during "
286 ctf_dynhash_destroy (linker_known
);
287 return (ctf_set_errno (fp
, err
));
291 while ((err
= ctf_dynhash_cnext (symhash
, &i
, &name
, NULL
)) == 0)
295 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
297 /* Linker did not report symbol in symtab. Remove it from the
298 set of known data symbols and continue. */
299 if ((sym
= ctf_dynhash_lookup (symfp
->ctf_dynsyms
, name
)) == NULL
)
301 ctf_dynhash_remove (symhash
, name
);
305 /* We don't remove skippable symbols from the symhash because we don't
306 want them to be migrated into variables. */
307 if (ctf_symtab_skippable (sym
))
310 if ((flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
311 && sym
->st_type
!= STT_FUNC
)
313 ctf_err_warn (fp
, 1, 0, _("Symbol %x added to CTF as a function "
314 "but is of type %x\n"),
315 sym
->st_symidx
, sym
->st_type
);
316 ctf_dynhash_remove (symhash
, name
);
319 else if (!(flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
320 && sym
->st_type
!= STT_OBJECT
)
322 ctf_err_warn (fp
, 1, 0, _("Symbol %x added to CTF as a data "
323 "object but is of type %x\n"),
324 sym
->st_symidx
, sym
->st_type
);
325 ctf_dynhash_remove (symhash
, name
);
329 ctf_dynhash_remove (linker_known
, name
);
331 *unpadsize
+= sizeof (uint32_t);
334 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
336 if (*max
< sym
->st_symidx
)
337 *max
= sym
->st_symidx
;
342 if (err
!= ECTF_NEXT_END
)
344 ctf_err_warn (fp
, 0, err
, _("iterating over CTF symtypetab during "
346 ctf_dynhash_destroy (linker_known
);
347 return (ctf_set_errno (fp
, err
));
350 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
352 while ((err
= ctf_dynhash_cnext (linker_known
, &i
, NULL
, &ctf_sym
)) == 0)
354 ctf_link_sym_t
*sym
= (ctf_link_sym_t
*) ctf_sym
;
356 if (sym
->st_symidx
> *max
)
359 if (err
!= ECTF_NEXT_END
)
361 ctf_err_warn (fp
, 0, err
, _("iterating over linker-known symbols "
362 "during CTF serialization"));
363 ctf_dynhash_destroy (linker_known
);
364 return (ctf_set_errno (fp
, err
));
368 *idxsize
= *count
* sizeof (uint32_t);
369 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
370 *padsize
= (ctf_dynhash_elements (linker_known
) - beyond_max
) * sizeof (uint32_t);
372 ctf_dynhash_destroy (linker_known
);
376 /* Emit an objt or func symtypetab into DP in a particular order defined by an
377 array of ctf_link_sym_t or symbol names passed in. The index has NIDX
378 elements in it: unindexed output would terminate at symbol OUTMAX and is in
379 any case no larger than SIZE bytes. Some index elements are expected to be
380 skipped: see symtypetab_density. The linker-reported set of symbols (if any)
381 is found in SYMFP. */
383 emit_symtypetab (ctf_dict_t
*fp
, ctf_dict_t
*symfp
, uint32_t *dp
,
384 ctf_link_sym_t
**idx
, const char **nameidx
, uint32_t nidx
,
385 uint32_t outmax
, int size
, int flags
)
389 ctf_dynhash_t
*symhash
;
391 ctf_dprintf ("Emitting table of size %i, outmax %u, %u symtypetab entries, "
392 "flags %i\n", size
, outmax
, nidx
, flags
);
394 /* Empty table? Nothing to do. */
398 if (flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
399 symhash
= fp
->ctf_funchash
;
401 symhash
= fp
->ctf_objthash
;
403 for (i
= 0; i
< nidx
; i
++)
405 const char *sym_name
;
408 /* If we have a linker-reported set of symbols, we may be given that set
409 to work from, or a set of symbol names. In both cases we want to look
410 at the corresponding linker-reported symbol (if any). */
411 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
413 ctf_link_sym_t
*this_link_sym
;
416 this_link_sym
= idx
[i
];
418 this_link_sym
= ctf_dynhash_lookup (symfp
->ctf_dynsyms
, nameidx
[i
]);
420 /* Unreported symbol number. No pad, no nothing. */
424 /* Symbol of the wrong type, or skippable? This symbol is not in this
426 if (((flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
427 && this_link_sym
->st_type
!= STT_FUNC
)
428 || (!(flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
429 && this_link_sym
->st_type
!= STT_OBJECT
))
432 if (ctf_symtab_skippable (this_link_sym
))
435 sym_name
= this_link_sym
->st_name
;
437 /* Linker reports symbol of a different type to the symbol we actually
438 added? Skip the symbol. No pad, since the symbol doesn't actually
439 belong in this table at all. (Warned about in
440 symtypetab_density.) */
441 if ((this_link_sym
->st_type
== STT_FUNC
)
442 && (ctf_dynhash_lookup (fp
->ctf_objthash
, sym_name
)))
445 if ((this_link_sym
->st_type
== STT_OBJECT
)
446 && (ctf_dynhash_lookup (fp
->ctf_funchash
, sym_name
)))
450 sym_name
= nameidx
[i
];
452 /* Symbol in index but no type set? Silently skip and (optionally)
453 pad. (In force-indexed mode, this is also where we track symbols of
454 the wrong type for this round of insertion.) */
455 if ((type
= ctf_dynhash_lookup (symhash
, sym_name
)) == NULL
)
457 if (flags
& CTF_SYMTYPETAB_EMIT_PAD
)
462 if (!ctf_assert (fp
, (((char *) dpp
) - (char *) dp
) < size
))
463 return -1; /* errno is set for us. */
465 *dpp
++ = (ctf_id_t
) (uintptr_t) type
;
467 /* When emitting unindexed output, all later symbols are pads: stop
469 if ((flags
& CTF_SYMTYPETAB_EMIT_PAD
) && idx
[i
]->st_symidx
== outmax
)
476 /* Emit an objt or func symtypetab index into DP in a paticular order defined by
477 an array of symbol names passed in. Stop at NIDX. The linker-reported set
478 of symbols (if any) is found in SYMFP. */
480 emit_symtypetab_index (ctf_dict_t
*fp
, ctf_dict_t
*symfp
, uint32_t *dp
,
481 const char **idx
, uint32_t nidx
, int size
, int flags
)
485 ctf_dynhash_t
*symhash
;
487 ctf_dprintf ("Emitting index of size %i, %u entries reported by linker, "
488 "flags %i\n", size
, nidx
, flags
);
490 /* Empty table? Nothing to do. */
494 if (flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
495 symhash
= fp
->ctf_funchash
;
497 symhash
= fp
->ctf_objthash
;
499 /* Indexes should always be unpadded. */
500 if (!ctf_assert (fp
, !(flags
& CTF_SYMTYPETAB_EMIT_PAD
)))
501 return -1; /* errno is set for us. */
503 for (i
= 0; i
< nidx
; i
++)
505 const char *sym_name
;
508 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
510 ctf_link_sym_t
*this_link_sym
;
512 this_link_sym
= ctf_dynhash_lookup (symfp
->ctf_dynsyms
, idx
[i
]);
514 /* This is an index: unreported symbols should never appear in it. */
515 if (!ctf_assert (fp
, this_link_sym
!= NULL
))
516 return -1; /* errno is set for us. */
518 /* Symbol of the wrong type, or skippable? This symbol is not in this
520 if (((flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
521 && this_link_sym
->st_type
!= STT_FUNC
)
522 || (!(flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
523 && this_link_sym
->st_type
!= STT_OBJECT
))
526 if (ctf_symtab_skippable (this_link_sym
))
529 sym_name
= this_link_sym
->st_name
;
531 /* Linker reports symbol of a different type to the symbol we actually
532 added? Skip the symbol. */
533 if ((this_link_sym
->st_type
== STT_FUNC
)
534 && (ctf_dynhash_lookup (fp
->ctf_objthash
, sym_name
)))
537 if ((this_link_sym
->st_type
== STT_OBJECT
)
538 && (ctf_dynhash_lookup (fp
->ctf_funchash
, sym_name
)))
544 /* Symbol in index and reported by linker, but no type set? Silently skip
545 and (optionally) pad. (In force-indexed mode, this is also where we
546 track symbols of the wrong type for this round of insertion.) */
547 if ((type
= ctf_dynhash_lookup (symhash
, sym_name
)) == NULL
)
550 ctf_str_add_ref (fp
, sym_name
, dpp
++);
552 if (!ctf_assert (fp
, (((char *) dpp
) - (char *) dp
) <= size
))
553 return -1; /* errno is set for us. */
559 static unsigned char *
560 ctf_copy_smembers (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
562 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
565 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
567 ctf_member_t
*copied
;
570 ctm
.ctm_type
= (uint32_t) dmd
->dmd_type
;
571 ctm
.ctm_offset
= (uint32_t) dmd
->dmd_offset
;
573 memcpy (t
, &ctm
, sizeof (ctm
));
574 copied
= (ctf_member_t
*) t
;
576 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctm_name
);
584 static unsigned char *
585 ctf_copy_lmembers (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
587 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
590 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
592 ctf_lmember_t
*copied
;
595 ctlm
.ctlm_type
= (uint32_t) dmd
->dmd_type
;
596 ctlm
.ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI (dmd
->dmd_offset
);
597 ctlm
.ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO (dmd
->dmd_offset
);
599 memcpy (t
, &ctlm
, sizeof (ctlm
));
600 copied
= (ctf_lmember_t
*) t
;
602 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctlm_name
);
610 static unsigned char *
611 ctf_copy_emembers (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
613 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
616 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
620 cte
.cte_value
= dmd
->dmd_value
;
621 memcpy (t
, &cte
, sizeof (cte
));
622 copied
= (ctf_enum_t
*) t
;
623 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->cte_name
);
630 /* Sort a newly-constructed static variable array. */
632 typedef struct ctf_sort_var_arg_cb
636 } ctf_sort_var_arg_cb_t
;
639 ctf_sort_var (const void *one_
, const void *two_
, void *arg_
)
641 const ctf_varent_t
*one
= one_
;
642 const ctf_varent_t
*two
= two_
;
643 ctf_sort_var_arg_cb_t
*arg
= arg_
;
645 return (strcmp (ctf_strraw_explicit (arg
->fp
, one
->ctv_name
, arg
->strtab
),
646 ctf_strraw_explicit (arg
->fp
, two
->ctv_name
, arg
->strtab
)));
649 /* Compatibility: just update the threshold for ctf_discard. */
651 ctf_update (ctf_dict_t
*fp
)
653 if (!(fp
->ctf_flags
& LCTF_RDWR
))
654 return (ctf_set_errno (fp
, ECTF_RDONLY
));
656 fp
->ctf_dtoldid
= fp
->ctf_typemax
;
660 /* If the specified CTF dict is writable and has been modified, reload this dict
661 with the updated type definitions, ready for serialization. In order to make
662 this code and the rest of libctf as simple as possible, we perform updates by
663 taking the dynamic type definitions and creating an in-memory CTF dict
664 containing the definitions, and then call ctf_simple_open_internal() on it.
665 We perform one extra trick here for the benefit of callers and to keep our
666 code simple: ctf_simple_open_internal() will return a new ctf_dict_t, but we
667 want to keep the fp constant for the caller, so after
668 ctf_simple_open_internal() returns, we use memcpy to swap the interior of the
669 old and new ctf_dict_t's, and then free the old. */
671 ctf_serialize (ctf_dict_t
*fp
)
673 ctf_dict_t ofp
, *nfp
;
674 ctf_header_t hdr
, *hdrp
;
677 ctf_varent_t
*dvarents
;
678 ctf_strs_writable_t strtab
;
679 ctf_dict_t
*symfp
= fp
;
684 size_t buf_size
, type_size
, objt_size
, func_size
;
685 size_t objt_unpadsize
, func_unpadsize
, objt_padsize
, func_padsize
;
686 size_t funcidx_size
, objtidx_size
;
687 size_t nvars
, nfuncs
, nobjts
, maxobjt
, maxfunc
;
689 const char **sym_name_order
= NULL
;
690 unsigned char *buf
= NULL
, *newbuf
;
693 if (!(fp
->ctf_flags
& LCTF_RDWR
))
694 return (ctf_set_errno (fp
, ECTF_RDONLY
));
696 /* Update required? */
697 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
700 /* Fill in an initial CTF header. We will leave the label, object,
701 and function sections empty and only output a header, type section,
702 and string table. The type section begins at a 4-byte aligned
703 boundary past the CTF header itself (at relative offset zero). The flag
704 indicating a new-style function info section (an array of CTF_K_FUNCTION
705 type IDs in the types section) is flipped on. */
707 memset (&hdr
, 0, sizeof (hdr
));
708 hdr
.cth_magic
= CTF_MAGIC
;
709 hdr
.cth_version
= CTF_VERSION
;
711 /* This is a new-format func info section, and the symtab and strtab come out
712 of the dynsym and dynstr these days. */
713 hdr
.cth_flags
= (CTF_F_NEWFUNCINFO
| CTF_F_DYNSTR
);
715 /* Iterate through the dynamic type definition list and compute the
716 size of the CTF type section we will need to generate. */
718 for (type_size
= 0, dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
719 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
721 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
722 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
724 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
725 type_size
+= sizeof (ctf_stype_t
);
727 type_size
+= sizeof (ctf_type_t
);
733 type_size
+= sizeof (uint32_t);
736 type_size
+= sizeof (ctf_array_t
);
739 type_size
+= sizeof (ctf_slice_t
);
742 type_size
+= sizeof (uint32_t) * (vlen
+ (vlen
& 1));
746 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
747 type_size
+= sizeof (ctf_member_t
) * vlen
;
749 type_size
+= sizeof (ctf_lmember_t
) * vlen
;
752 type_size
+= sizeof (ctf_enum_t
) * vlen
;
757 /* Symbol table stuff is done only if the linker has told this dict about
758 potential symbols (usually the case for parent dicts only). The linker
759 will report symbols to the parent dict in a parent/child link, as usual
760 with all linker-related matters. */
762 if (!fp
->ctf_dynsyms
&& fp
->ctf_parent
&& fp
->ctf_parent
->ctf_dynsyms
)
763 symfp
= fp
->ctf_parent
;
765 /* No linker-reported symbols at all: ctf_link_shuffle_syms was never called.
766 This must be an unsorted, indexed dict. Otherwise, this is a sorted
767 dict, and the header flags indicate as much. */
768 if (!symfp
->ctf_dynsyms
)
769 symflags
= CTF_SYMTYPETAB_FORCE_INDEXED
;
771 hdr
.cth_flags
|= CTF_F_IDXSORTED
;
773 /* Work out the sizes of the object and function sections, and work out the
774 number of pad (unassigned) symbols in each, and the overall size of the
777 if (symtypetab_density (fp
, symfp
, fp
->ctf_objthash
, &nobjts
, &maxobjt
,
778 &objt_unpadsize
, &objt_padsize
, &objtidx_size
,
780 return -1; /* errno is set for us. */
782 ctf_dprintf ("Object symtypetab: %i objects, max %i, unpadded size %i, "
783 "%i bytes of pads, index size %i\n", (int) nobjts
, (int) maxobjt
,
784 (int) objt_unpadsize
, (int) objt_padsize
, (int) objtidx_size
);
786 if (symtypetab_density (fp
, symfp
, fp
->ctf_funchash
, &nfuncs
, &maxfunc
,
787 &func_unpadsize
, &func_padsize
, &funcidx_size
,
788 symflags
| CTF_SYMTYPETAB_EMIT_FUNCTION
) < 0)
789 return -1; /* errno is set for us. */
791 ctf_dprintf ("Function symtypetab: %i functions, max %i, unpadded size %i, "
792 "%i bytes of pads, index size %i\n", (int) nfuncs
, (int) maxfunc
,
793 (int) func_unpadsize
, (int) func_padsize
, (int) funcidx_size
);
795 /* If the linker has reported any symbols at all, those symbols that the
796 linker has not reported are now removed from the ctf_objthash and
797 ctf_funchash. Delete entries from the variable section that duplicate
798 newly-added data symbols. There's no need to migrate new ones in, because
799 linker invocations (even ld -r) can only introduce new symbols, not remove
800 symbols that already exist, and the compiler always emits both a variable
801 and a data symbol simultaneously. */
803 if (symtypetab_delete_nonstatic_vars (fp
) < 0)
806 /* It is worth indexing each section if it would save space to do so, due to
807 reducing the number of pads sufficiently. A pad is the same size as a
808 single index entry: but index sections compress relatively poorly compared
809 to constant pads, so it takes a lot of contiguous padding to equal one
810 index section entry. It would be nice to be able to *verify* whether we
811 would save space after compression rather than guessing, but this seems
812 difficult, since it would require complete reserialization. Regardless, if
813 the linker has not reported any symbols (e.g. if this is not a final link
814 but just an ld -r), we must emit things in indexed fashion just as the
817 objt_size
= objt_unpadsize
;
818 if (!(symflags
& CTF_SYMTYPETAB_FORCE_INDEXED
)
819 && ((objt_padsize
+ objt_unpadsize
) * CTF_INDEX_PAD_THRESHOLD
822 objt_size
+= objt_padsize
;
826 func_size
= func_unpadsize
;
827 if (!(symflags
& CTF_SYMTYPETAB_FORCE_INDEXED
)
828 && ((func_padsize
+ func_unpadsize
) * CTF_INDEX_PAD_THRESHOLD
831 func_size
+= func_padsize
;
835 /* Computing the number of entries in the CTF variable section is much
838 for (nvars
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
);
839 dvd
!= NULL
; dvd
= ctf_list_next (dvd
), nvars
++);
841 /* Compute the size of the CTF buffer we need, sans only the string table,
842 then allocate a new buffer and memcpy the finished header to the start of
843 the buffer. (We will adjust this later with strtab length info.) */
845 hdr
.cth_lbloff
= hdr
.cth_objtoff
= 0;
846 hdr
.cth_funcoff
= hdr
.cth_objtoff
+ objt_size
;
847 hdr
.cth_objtidxoff
= hdr
.cth_funcoff
+ func_size
;
848 hdr
.cth_funcidxoff
= hdr
.cth_objtidxoff
+ objtidx_size
;
849 hdr
.cth_varoff
= hdr
.cth_funcidxoff
+ funcidx_size
;
850 hdr
.cth_typeoff
= hdr
.cth_varoff
+ (nvars
* sizeof (ctf_varent_t
));
851 hdr
.cth_stroff
= hdr
.cth_typeoff
+ type_size
;
854 buf_size
= sizeof (ctf_header_t
) + hdr
.cth_stroff
+ hdr
.cth_strlen
;
856 if ((buf
= malloc (buf_size
)) == NULL
)
857 return (ctf_set_errno (fp
, EAGAIN
));
859 memcpy (buf
, &hdr
, sizeof (ctf_header_t
));
860 t
= (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_objtoff
;
862 hdrp
= (ctf_header_t
*) buf
;
863 if ((fp
->ctf_flags
& LCTF_CHILD
) && (fp
->ctf_parname
!= NULL
))
864 ctf_str_add_ref (fp
, fp
->ctf_parname
, &hdrp
->cth_parname
);
865 if (fp
->ctf_cuname
!= NULL
)
866 ctf_str_add_ref (fp
, fp
->ctf_cuname
, &hdrp
->cth_cuname
);
868 /* Sort the linker's symbols into name order if need be: if
869 ctf_link_shuffle_syms has not been called at all, just use all the symbols
870 that were added to this dict, and don't bother sorting them since this is
871 probably an ld -r and will likely just be consumed by ld again, with no
872 ctf_lookup_by_symbol()s ever done on it. */
874 if ((objtidx_size
!= 0) || (funcidx_size
!= 0))
876 ctf_next_t
*i
= NULL
;
881 if (symfp
->ctf_dynsyms
)
882 ndynsyms
= ctf_dynhash_elements (symfp
->ctf_dynsyms
);
884 ndynsyms
= ctf_dynhash_elements (symfp
->ctf_objthash
)
885 + ctf_dynhash_elements (symfp
->ctf_funchash
);
887 if ((sym_name_order
= calloc (ndynsyms
, sizeof (const char *))) == NULL
)
890 walk
= sym_name_order
;
892 if (symfp
->ctf_dynsyms
)
894 while ((err
= ctf_dynhash_next_sorted (symfp
->ctf_dynsyms
, &i
, &symname
,
895 NULL
, ctf_dynhash_sort_by_name
,
897 *walk
++ = (const char *) symname
;
898 if (err
!= ECTF_NEXT_END
)
903 while ((err
= ctf_dynhash_next (symfp
->ctf_objthash
, &i
, &symname
,
905 *walk
++ = (const char *) symname
;
906 if (err
!= ECTF_NEXT_END
)
909 while ((err
= ctf_dynhash_next (symfp
->ctf_funchash
, &i
, &symname
,
911 *walk
++ = (const char *) symname
;
912 if (err
!= ECTF_NEXT_END
)
917 /* Emit the object and function sections, and if necessary their indexes.
918 Emission is done in symtab order if there is no index, and in index
919 (name) order otherwise. */
921 if ((objtidx_size
== 0) && symfp
->ctf_dynsymidx
)
923 ctf_dprintf ("Emitting unindexed objt symtypetab\n");
924 if (emit_symtypetab (fp
, symfp
, (uint32_t *) t
, symfp
->ctf_dynsymidx
,
925 NULL
, symfp
->ctf_dynsymmax
+ 1, maxobjt
, objt_size
,
926 symflags
| CTF_SYMTYPETAB_EMIT_PAD
) < 0)
927 goto err
; /* errno is set for us. */
931 ctf_dprintf ("Emitting indexed objt symtypetab\n");
932 if (emit_symtypetab (fp
, symfp
, (uint32_t *) t
, NULL
, sym_name_order
,
933 ndynsyms
, maxobjt
, objt_size
, symflags
) < 0)
934 goto err
; /* errno is set for us. */
939 if ((funcidx_size
== 0) && symfp
->ctf_dynsymidx
)
941 ctf_dprintf ("Emitting unindexed func symtypetab\n");
942 if (emit_symtypetab (fp
, symfp
, (uint32_t *) t
, symfp
->ctf_dynsymidx
,
943 NULL
, symfp
->ctf_dynsymmax
+ 1, maxfunc
,
944 func_size
, symflags
| CTF_SYMTYPETAB_EMIT_FUNCTION
945 | CTF_SYMTYPETAB_EMIT_PAD
) < 0)
946 goto err
; /* errno is set for us. */
950 ctf_dprintf ("Emitting indexed func symtypetab\n");
951 if (emit_symtypetab (fp
, symfp
, (uint32_t *) t
, NULL
, sym_name_order
,
952 ndynsyms
, maxfunc
, func_size
,
953 symflags
| CTF_SYMTYPETAB_EMIT_FUNCTION
) < 0)
954 goto err
; /* errno is set for us. */
959 if (objtidx_size
> 0)
960 if (emit_symtypetab_index (fp
, symfp
, (uint32_t *) t
, sym_name_order
,
961 ndynsyms
, objtidx_size
, symflags
) < 0)
966 if (funcidx_size
> 0)
967 if (emit_symtypetab_index (fp
, symfp
, (uint32_t *) t
, sym_name_order
,
968 ndynsyms
, funcidx_size
,
969 symflags
| CTF_SYMTYPETAB_EMIT_FUNCTION
) < 0)
973 free (sym_name_order
);
974 sym_name_order
= NULL
;
976 /* Work over the variable list, translating everything into ctf_varent_t's and
977 prepping the string table. */
979 dvarents
= (ctf_varent_t
*) t
;
980 for (i
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
;
981 dvd
= ctf_list_next (dvd
), i
++)
983 ctf_varent_t
*var
= &dvarents
[i
];
985 ctf_str_add_ref (fp
, dvd
->dvd_name
, &var
->ctv_name
);
986 var
->ctv_type
= (uint32_t) dvd
->dvd_type
;
990 t
+= sizeof (ctf_varent_t
) * nvars
;
992 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_typeoff
);
994 /* We now take a final lap through the dynamic type definition list and copy
995 the appropriate type records to the output buffer, noting down the
998 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
999 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
1001 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1002 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1007 ctf_stype_t
*copied
;
1010 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
1011 len
= sizeof (ctf_stype_t
);
1013 len
= sizeof (ctf_type_t
);
1015 memcpy (t
, &dtd
->dtd_data
, len
);
1016 copied
= (ctf_stype_t
*) t
; /* name is at the start: constant offset. */
1017 if (copied
->ctt_name
1018 && (name
= ctf_strraw (fp
, copied
->ctt_name
)) != NULL
)
1019 ctf_str_add_ref (fp
, name
, &copied
->ctt_name
);
1026 if (kind
== CTF_K_INTEGER
)
1028 encoding
= CTF_INT_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
1029 dtd
->dtd_u
.dtu_enc
.cte_offset
,
1030 dtd
->dtd_u
.dtu_enc
.cte_bits
);
1034 encoding
= CTF_FP_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
1035 dtd
->dtd_u
.dtu_enc
.cte_offset
,
1036 dtd
->dtd_u
.dtu_enc
.cte_bits
);
1038 memcpy (t
, &encoding
, sizeof (encoding
));
1039 t
+= sizeof (encoding
);
1043 memcpy (t
, &dtd
->dtd_u
.dtu_slice
, sizeof (struct ctf_slice
));
1044 t
+= sizeof (struct ctf_slice
);
1048 cta
.cta_contents
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_contents
;
1049 cta
.cta_index
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_index
;
1050 cta
.cta_nelems
= dtd
->dtd_u
.dtu_arr
.ctr_nelems
;
1051 memcpy (t
, &cta
, sizeof (cta
));
1055 case CTF_K_FUNCTION
:
1057 uint32_t *argv
= (uint32_t *) (uintptr_t) t
;
1060 for (argc
= 0; argc
< vlen
; argc
++)
1061 *argv
++ = dtd
->dtd_u
.dtu_argv
[argc
];
1064 *argv
++ = 0; /* Pad to 4-byte boundary. */
1066 t
= (unsigned char *) argv
;
1072 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
1073 t
= ctf_copy_smembers (fp
, dtd
, t
);
1075 t
= ctf_copy_lmembers (fp
, dtd
, t
);
1079 t
= ctf_copy_emembers (fp
, dtd
, t
);
1083 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_stroff
);
1085 /* Construct the final string table and fill out all the string refs with the
1086 final offsets. Then purge the refs list, because we're about to move this
1087 strtab onto the end of the buf, invalidating all the offsets. */
1088 strtab
= ctf_str_write_strtab (fp
);
1089 ctf_str_purge_refs (fp
);
1091 if (strtab
.cts_strs
== NULL
)
1094 /* Now the string table is constructed, we can sort the buffer of
1096 ctf_sort_var_arg_cb_t sort_var_arg
= { fp
, (ctf_strs_t
*) &strtab
};
1097 ctf_qsort_r (dvarents
, nvars
, sizeof (ctf_varent_t
), ctf_sort_var
,
1100 if ((newbuf
= ctf_realloc (fp
, buf
, buf_size
+ strtab
.cts_len
)) == NULL
)
1102 free (strtab
.cts_strs
);
1106 memcpy (buf
+ buf_size
, strtab
.cts_strs
, strtab
.cts_len
);
1107 hdrp
= (ctf_header_t
*) buf
;
1108 hdrp
->cth_strlen
= strtab
.cts_len
;
1109 buf_size
+= hdrp
->cth_strlen
;
1110 free (strtab
.cts_strs
);
1112 /* Finally, we are ready to ctf_simple_open() the new dict. If this is
1113 successful, we then switch nfp and fp and free the old dict. */
1115 if ((nfp
= ctf_simple_open_internal ((char *) buf
, buf_size
, NULL
, 0,
1116 0, NULL
, 0, fp
->ctf_syn_ext_strtab
,
1120 return (ctf_set_errno (fp
, err
));
1123 (void) ctf_setmodel (nfp
, ctf_getmodel (fp
));
1125 nfp
->ctf_parent
= fp
->ctf_parent
;
1126 nfp
->ctf_parent_unreffed
= fp
->ctf_parent_unreffed
;
1127 nfp
->ctf_refcnt
= fp
->ctf_refcnt
;
1128 nfp
->ctf_flags
|= fp
->ctf_flags
& ~LCTF_DIRTY
;
1129 if (nfp
->ctf_dynbase
== NULL
)
1130 nfp
->ctf_dynbase
= buf
; /* Make sure buf is freed on close. */
1131 nfp
->ctf_dthash
= fp
->ctf_dthash
;
1132 nfp
->ctf_dtdefs
= fp
->ctf_dtdefs
;
1133 nfp
->ctf_dvhash
= fp
->ctf_dvhash
;
1134 nfp
->ctf_dvdefs
= fp
->ctf_dvdefs
;
1135 nfp
->ctf_dtoldid
= fp
->ctf_dtoldid
;
1136 nfp
->ctf_add_processing
= fp
->ctf_add_processing
;
1137 nfp
->ctf_snapshots
= fp
->ctf_snapshots
+ 1;
1138 nfp
->ctf_specific
= fp
->ctf_specific
;
1139 nfp
->ctf_nfuncidx
= fp
->ctf_nfuncidx
;
1140 nfp
->ctf_nobjtidx
= fp
->ctf_nobjtidx
;
1141 nfp
->ctf_objthash
= fp
->ctf_objthash
;
1142 nfp
->ctf_funchash
= fp
->ctf_funchash
;
1143 nfp
->ctf_dynsyms
= fp
->ctf_dynsyms
;
1144 nfp
->ctf_ptrtab
= fp
->ctf_ptrtab
;
1145 nfp
->ctf_pptrtab
= fp
->ctf_pptrtab
;
1146 nfp
->ctf_dynsymidx
= fp
->ctf_dynsymidx
;
1147 nfp
->ctf_dynsymmax
= fp
->ctf_dynsymmax
;
1148 nfp
->ctf_ptrtab_len
= fp
->ctf_ptrtab_len
;
1149 nfp
->ctf_pptrtab_len
= fp
->ctf_pptrtab_len
;
1150 nfp
->ctf_link_inputs
= fp
->ctf_link_inputs
;
1151 nfp
->ctf_link_outputs
= fp
->ctf_link_outputs
;
1152 nfp
->ctf_errs_warnings
= fp
->ctf_errs_warnings
;
1153 nfp
->ctf_funcidx_names
= fp
->ctf_funcidx_names
;
1154 nfp
->ctf_objtidx_names
= fp
->ctf_objtidx_names
;
1155 nfp
->ctf_funcidx_sxlate
= fp
->ctf_funcidx_sxlate
;
1156 nfp
->ctf_objtidx_sxlate
= fp
->ctf_objtidx_sxlate
;
1157 nfp
->ctf_str_prov_offset
= fp
->ctf_str_prov_offset
;
1158 nfp
->ctf_syn_ext_strtab
= fp
->ctf_syn_ext_strtab
;
1159 nfp
->ctf_pptrtab_typemax
= fp
->ctf_pptrtab_typemax
;
1160 nfp
->ctf_in_flight_dynsyms
= fp
->ctf_in_flight_dynsyms
;
1161 nfp
->ctf_link_in_cu_mapping
= fp
->ctf_link_in_cu_mapping
;
1162 nfp
->ctf_link_out_cu_mapping
= fp
->ctf_link_out_cu_mapping
;
1163 nfp
->ctf_link_type_mapping
= fp
->ctf_link_type_mapping
;
1164 nfp
->ctf_link_memb_name_changer
= fp
->ctf_link_memb_name_changer
;
1165 nfp
->ctf_link_memb_name_changer_arg
= fp
->ctf_link_memb_name_changer_arg
;
1166 nfp
->ctf_link_variable_filter
= fp
->ctf_link_variable_filter
;
1167 nfp
->ctf_link_variable_filter_arg
= fp
->ctf_link_variable_filter_arg
;
1168 nfp
->ctf_symsect_little_endian
= fp
->ctf_symsect_little_endian
;
1169 nfp
->ctf_link_flags
= fp
->ctf_link_flags
;
1170 nfp
->ctf_dedup_atoms
= fp
->ctf_dedup_atoms
;
1171 nfp
->ctf_dedup_atoms_alloc
= fp
->ctf_dedup_atoms_alloc
;
1172 memcpy (&nfp
->ctf_dedup
, &fp
->ctf_dedup
, sizeof (fp
->ctf_dedup
));
1174 nfp
->ctf_snapshot_lu
= fp
->ctf_snapshots
;
1176 memcpy (&nfp
->ctf_lookups
, fp
->ctf_lookups
, sizeof (fp
->ctf_lookups
));
1177 nfp
->ctf_structs
= fp
->ctf_structs
;
1178 nfp
->ctf_unions
= fp
->ctf_unions
;
1179 nfp
->ctf_enums
= fp
->ctf_enums
;
1180 nfp
->ctf_names
= fp
->ctf_names
;
1182 fp
->ctf_dthash
= NULL
;
1183 ctf_str_free_atoms (nfp
);
1184 nfp
->ctf_str_atoms
= fp
->ctf_str_atoms
;
1185 nfp
->ctf_prov_strtab
= fp
->ctf_prov_strtab
;
1186 fp
->ctf_str_atoms
= NULL
;
1187 fp
->ctf_prov_strtab
= NULL
;
1188 memset (&fp
->ctf_dtdefs
, 0, sizeof (ctf_list_t
));
1189 memset (&fp
->ctf_errs_warnings
, 0, sizeof (ctf_list_t
));
1190 fp
->ctf_add_processing
= NULL
;
1191 fp
->ctf_ptrtab
= NULL
;
1192 fp
->ctf_pptrtab
= NULL
;
1193 fp
->ctf_funcidx_names
= NULL
;
1194 fp
->ctf_objtidx_names
= NULL
;
1195 fp
->ctf_funcidx_sxlate
= NULL
;
1196 fp
->ctf_objtidx_sxlate
= NULL
;
1197 fp
->ctf_objthash
= NULL
;
1198 fp
->ctf_funchash
= NULL
;
1199 fp
->ctf_dynsyms
= NULL
;
1200 fp
->ctf_dynsymidx
= NULL
;
1201 fp
->ctf_link_inputs
= NULL
;
1202 fp
->ctf_link_outputs
= NULL
;
1203 fp
->ctf_syn_ext_strtab
= NULL
;
1204 fp
->ctf_link_in_cu_mapping
= NULL
;
1205 fp
->ctf_link_out_cu_mapping
= NULL
;
1206 fp
->ctf_link_type_mapping
= NULL
;
1207 fp
->ctf_dedup_atoms
= NULL
;
1208 fp
->ctf_dedup_atoms_alloc
= NULL
;
1209 fp
->ctf_parent_unreffed
= 1;
1211 fp
->ctf_dvhash
= NULL
;
1212 memset (&fp
->ctf_dvdefs
, 0, sizeof (ctf_list_t
));
1213 memset (fp
->ctf_lookups
, 0, sizeof (fp
->ctf_lookups
));
1214 memset (&fp
->ctf_in_flight_dynsyms
, 0, sizeof (fp
->ctf_in_flight_dynsyms
));
1215 memset (&fp
->ctf_dedup
, 0, sizeof (fp
->ctf_dedup
));
1216 fp
->ctf_structs
.ctn_writable
= NULL
;
1217 fp
->ctf_unions
.ctn_writable
= NULL
;
1218 fp
->ctf_enums
.ctn_writable
= NULL
;
1219 fp
->ctf_names
.ctn_writable
= NULL
;
1221 memcpy (&ofp
, fp
, sizeof (ctf_dict_t
));
1222 memcpy (fp
, nfp
, sizeof (ctf_dict_t
));
1223 memcpy (nfp
, &ofp
, sizeof (ctf_dict_t
));
1225 nfp
->ctf_refcnt
= 1; /* Force nfp to be freed. */
1226 ctf_dict_close (nfp
);
1231 ctf_err_warn (fp
, 0, err
, _("error serializing symtypetabs"));
1235 free (sym_name_order
);
1236 return (ctf_set_errno (fp
, EAGAIN
));
1239 free (sym_name_order
);
1240 return -1; /* errno is set for us. */
1244 ctf_name_table (ctf_dict_t
*fp
, int kind
)
1249 return &fp
->ctf_structs
;
1251 return &fp
->ctf_unions
;
1253 return &fp
->ctf_enums
;
1255 return &fp
->ctf_names
;
1260 ctf_dtd_insert (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, int flag
, int kind
)
1263 if (ctf_dynhash_insert (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
,
1266 ctf_set_errno (fp
, ENOMEM
);
1270 if (flag
== CTF_ADD_ROOT
&& dtd
->dtd_data
.ctt_name
1271 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
)
1273 if (ctf_dynhash_insert (ctf_name_table (fp
, kind
)->ctn_writable
,
1274 (char *) name
, (void *) (uintptr_t)
1277 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t)
1279 ctf_set_errno (fp
, ENOMEM
);
1283 ctf_list_append (&fp
->ctf_dtdefs
, dtd
);
1288 ctf_dtd_delete (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
)
1290 ctf_dmdef_t
*dmd
, *nmd
;
1291 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1292 int name_kind
= kind
;
1295 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
);
1302 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1303 dmd
!= NULL
; dmd
= nmd
)
1305 if (dmd
->dmd_name
!= NULL
)
1306 free (dmd
->dmd_name
);
1307 nmd
= ctf_list_next (dmd
);
1311 case CTF_K_FUNCTION
:
1312 free (dtd
->dtd_u
.dtu_argv
);
1315 name_kind
= dtd
->dtd_data
.ctt_type
;
1319 if (dtd
->dtd_data
.ctt_name
1320 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
1321 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
1323 ctf_dynhash_remove (ctf_name_table (fp
, name_kind
)->ctn_writable
,
1325 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
1328 ctf_list_delete (&fp
->ctf_dtdefs
, dtd
);
1333 ctf_dtd_lookup (const ctf_dict_t
*fp
, ctf_id_t type
)
1335 return (ctf_dtdef_t
*)
1336 ctf_dynhash_lookup (fp
->ctf_dthash
, (void *) (uintptr_t) type
);
1340 ctf_dynamic_type (const ctf_dict_t
*fp
, ctf_id_t id
)
1344 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1347 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, id
))
1348 fp
= fp
->ctf_parent
;
1350 idx
= LCTF_TYPE_TO_INDEX(fp
, id
);
1352 if ((unsigned long) idx
<= fp
->ctf_typemax
)
1353 return ctf_dtd_lookup (fp
, id
);
1358 ctf_dvd_insert (ctf_dict_t
*fp
, ctf_dvdef_t
*dvd
)
1360 if (ctf_dynhash_insert (fp
->ctf_dvhash
, dvd
->dvd_name
, dvd
) < 0)
1362 ctf_set_errno (fp
, ENOMEM
);
1365 ctf_list_append (&fp
->ctf_dvdefs
, dvd
);
1370 ctf_dvd_delete (ctf_dict_t
*fp
, ctf_dvdef_t
*dvd
)
1372 ctf_dynhash_remove (fp
->ctf_dvhash
, dvd
->dvd_name
);
1373 free (dvd
->dvd_name
);
1375 ctf_list_delete (&fp
->ctf_dvdefs
, dvd
);
1380 ctf_dvd_lookup (const ctf_dict_t
*fp
, const char *name
)
1382 return (ctf_dvdef_t
*) ctf_dynhash_lookup (fp
->ctf_dvhash
, name
);
1385 /* Discard all of the dynamic type definitions and variable definitions that
1386 have been added to the dict since the last call to ctf_update(). We locate
1387 such types by scanning the dtd list and deleting elements that have type IDs
1388 greater than ctf_dtoldid, which is set by ctf_update(), above, and by
1389 scanning the variable list and deleting elements that have update IDs equal
1390 to the current value of the last-update snapshot count (indicating that they
1391 were added after the most recent call to ctf_update()). */
1393 ctf_discard (ctf_dict_t
*fp
)
1395 ctf_snapshot_id_t last_update
=
1397 fp
->ctf_snapshot_lu
+ 1 };
1399 /* Update required? */
1400 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
1403 return (ctf_rollback (fp
, last_update
));
1407 ctf_snapshot (ctf_dict_t
*fp
)
1409 ctf_snapshot_id_t snapid
;
1410 snapid
.dtd_id
= fp
->ctf_typemax
;
1411 snapid
.snapshot_id
= fp
->ctf_snapshots
++;
1415 /* Like ctf_discard(), only discards everything after a particular ID. */
1417 ctf_rollback (ctf_dict_t
*fp
, ctf_snapshot_id_t id
)
1419 ctf_dtdef_t
*dtd
, *ntd
;
1420 ctf_dvdef_t
*dvd
, *nvd
;
1422 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1423 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1425 if (fp
->ctf_snapshot_lu
>= id
.snapshot_id
)
1426 return (ctf_set_errno (fp
, ECTF_OVERROLLBACK
));
1428 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
); dtd
!= NULL
; dtd
= ntd
)
1433 ntd
= ctf_list_next (dtd
);
1435 if (LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_type
) <= id
.dtd_id
)
1438 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1439 if (kind
== CTF_K_FORWARD
)
1440 kind
= dtd
->dtd_data
.ctt_type
;
1442 if (dtd
->dtd_data
.ctt_name
1443 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
1444 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
1446 ctf_dynhash_remove (ctf_name_table (fp
, kind
)->ctn_writable
,
1448 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
1451 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
);
1452 ctf_dtd_delete (fp
, dtd
);
1455 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
; dvd
= nvd
)
1457 nvd
= ctf_list_next (dvd
);
1459 if (dvd
->dvd_snapshots
<= id
.snapshot_id
)
1462 ctf_dvd_delete (fp
, dvd
);
1465 fp
->ctf_typemax
= id
.dtd_id
;
1466 fp
->ctf_snapshots
= id
.snapshot_id
;
1468 if (fp
->ctf_snapshots
== fp
->ctf_snapshot_lu
)
1469 fp
->ctf_flags
&= ~LCTF_DIRTY
;
1475 ctf_add_generic (ctf_dict_t
*fp
, uint32_t flag
, const char *name
, int kind
,
1481 if (flag
!= CTF_ADD_NONROOT
&& flag
!= CTF_ADD_ROOT
)
1482 return (ctf_set_errno (fp
, EINVAL
));
1484 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1485 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1487 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) >= CTF_MAX_TYPE
)
1488 return (ctf_set_errno (fp
, ECTF_FULL
));
1490 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) == (CTF_MAX_PTYPE
- 1))
1491 return (ctf_set_errno (fp
, ECTF_FULL
));
1493 /* Make sure ptrtab always grows to be big enough for all types. */
1494 if (ctf_grow_ptrtab (fp
) < 0)
1495 return CTF_ERR
; /* errno is set for us. */
1497 if ((dtd
= malloc (sizeof (ctf_dtdef_t
))) == NULL
)
1498 return (ctf_set_errno (fp
, EAGAIN
));
1500 type
= ++fp
->ctf_typemax
;
1501 type
= LCTF_INDEX_TO_TYPE (fp
, type
, (fp
->ctf_flags
& LCTF_CHILD
));
1503 memset (dtd
, 0, sizeof (ctf_dtdef_t
));
1504 dtd
->dtd_data
.ctt_name
= ctf_str_add_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
1505 dtd
->dtd_type
= type
;
1507 if (dtd
->dtd_data
.ctt_name
== 0 && name
!= NULL
&& name
[0] != '\0')
1510 return (ctf_set_errno (fp
, EAGAIN
));
1513 if (ctf_dtd_insert (fp
, dtd
, flag
, kind
) < 0)
1516 return CTF_ERR
; /* errno is set for us. */
1518 fp
->ctf_flags
|= LCTF_DIRTY
;
1524 /* When encoding integer sizes, we want to convert a byte count in the range
1525 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
1526 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
1542 ctf_add_encoded (ctf_dict_t
*fp
, uint32_t flag
,
1543 const char *name
, const ctf_encoding_t
*ep
, uint32_t kind
)
1549 return (ctf_set_errno (fp
, EINVAL
));
1551 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, &dtd
)) == CTF_ERR
)
1552 return CTF_ERR
; /* errno is set for us. */
1554 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
1555 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
1557 dtd
->dtd_u
.dtu_enc
= *ep
;
1563 ctf_add_reftype (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
, uint32_t kind
)
1567 ctf_dict_t
*tmp
= fp
;
1568 int child
= fp
->ctf_flags
& LCTF_CHILD
;
1570 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
1571 return (ctf_set_errno (fp
, EINVAL
));
1573 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1574 return CTF_ERR
; /* errno is set for us. */
1576 if ((type
= ctf_add_generic (fp
, flag
, NULL
, kind
, &dtd
)) == CTF_ERR
)
1577 return CTF_ERR
; /* errno is set for us. */
1579 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
1580 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
1582 if (kind
!= CTF_K_POINTER
)
1585 /* If we are adding a pointer, update the ptrtab, both the directly pointed-to
1586 type and (if an anonymous typedef node is being pointed at) the type that
1587 points at too. Note that ctf_typemax is at this point one higher than we
1588 want to check against, because it's just been incremented for the addition
1589 of this type. The pptrtab is lazily-updated as needed, so is not touched
1592 uint32_t type_idx
= LCTF_TYPE_TO_INDEX (fp
, type
);
1593 uint32_t ref_idx
= LCTF_TYPE_TO_INDEX (fp
, ref
);
1595 if (LCTF_TYPE_ISCHILD (fp
, ref
) == child
1596 && ref_idx
< fp
->ctf_typemax
)
1598 fp
->ctf_ptrtab
[ref_idx
] = type_idx
;
1600 ctf_id_t refref_idx
= LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_data
.ctt_type
);
1603 && (LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) == CTF_K_TYPEDEF
)
1604 && strcmp (ctf_strptr (fp
, dtd
->dtd_data
.ctt_name
), "") == 0
1605 && refref_idx
< fp
->ctf_typemax
)
1606 fp
->ctf_ptrtab
[refref_idx
] = type_idx
;
1613 ctf_add_slice (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
,
1614 const ctf_encoding_t
*ep
)
1617 ctf_id_t resolved_ref
= ref
;
1620 const ctf_type_t
*tp
;
1621 ctf_dict_t
*tmp
= fp
;
1624 return (ctf_set_errno (fp
, EINVAL
));
1626 if ((ep
->cte_bits
> 255) || (ep
->cte_offset
> 255))
1627 return (ctf_set_errno (fp
, ECTF_SLICEOVERFLOW
));
1629 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
1630 return (ctf_set_errno (fp
, EINVAL
));
1632 if (ref
!= 0 && ((tp
= ctf_lookup_by_id (&tmp
, ref
)) == NULL
))
1633 return CTF_ERR
; /* errno is set for us. */
1635 /* Make sure we ultimately point to an integral type. We also allow slices to
1636 point to the unimplemented type, for now, because the compiler can emit
1637 such slices, though they're not very much use. */
1639 resolved_ref
= ctf_type_resolve_unsliced (tmp
, ref
);
1640 kind
= ctf_type_kind_unsliced (tmp
, resolved_ref
);
1642 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) &&
1643 (kind
!= CTF_K_ENUM
)
1645 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1647 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_SLICE
, &dtd
)) == CTF_ERR
)
1648 return CTF_ERR
; /* errno is set for us. */
1650 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_SLICE
, flag
, 0);
1651 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
1653 dtd
->dtd_u
.dtu_slice
.cts_type
= (uint32_t) ref
;
1654 dtd
->dtd_u
.dtu_slice
.cts_bits
= ep
->cte_bits
;
1655 dtd
->dtd_u
.dtu_slice
.cts_offset
= ep
->cte_offset
;
1661 ctf_add_integer (ctf_dict_t
*fp
, uint32_t flag
,
1662 const char *name
, const ctf_encoding_t
*ep
)
1664 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_INTEGER
));
1668 ctf_add_float (ctf_dict_t
*fp
, uint32_t flag
,
1669 const char *name
, const ctf_encoding_t
*ep
)
1671 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_FLOAT
));
1675 ctf_add_pointer (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1677 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_POINTER
));
1681 ctf_add_array (ctf_dict_t
*fp
, uint32_t flag
, const ctf_arinfo_t
*arp
)
1685 ctf_dict_t
*tmp
= fp
;
1688 return (ctf_set_errno (fp
, EINVAL
));
1690 if (arp
->ctr_contents
!= 0
1691 && ctf_lookup_by_id (&tmp
, arp
->ctr_contents
) == NULL
)
1692 return CTF_ERR
; /* errno is set for us. */
1695 if (ctf_lookup_by_id (&tmp
, arp
->ctr_index
) == NULL
)
1696 return CTF_ERR
; /* errno is set for us. */
1698 if (ctf_type_kind (fp
, arp
->ctr_index
) == CTF_K_FORWARD
)
1700 ctf_err_warn (fp
, 1, ECTF_INCOMPLETE
,
1701 _("ctf_add_array: index type %lx is incomplete"),
1703 return (ctf_set_errno (fp
, ECTF_INCOMPLETE
));
1706 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_ARRAY
, &dtd
)) == CTF_ERR
)
1707 return CTF_ERR
; /* errno is set for us. */
1709 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ARRAY
, flag
, 0);
1710 dtd
->dtd_data
.ctt_size
= 0;
1711 dtd
->dtd_u
.dtu_arr
= *arp
;
1717 ctf_set_array (ctf_dict_t
*fp
, ctf_id_t type
, const ctf_arinfo_t
*arp
)
1719 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1721 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1722 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1725 || LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) != CTF_K_ARRAY
)
1726 return (ctf_set_errno (fp
, ECTF_BADID
));
1728 fp
->ctf_flags
|= LCTF_DIRTY
;
1729 dtd
->dtd_u
.dtu_arr
= *arp
;
1735 ctf_add_function (ctf_dict_t
*fp
, uint32_t flag
,
1736 const ctf_funcinfo_t
*ctc
, const ctf_id_t
*argv
)
1741 uint32_t *vdat
= NULL
;
1742 ctf_dict_t
*tmp
= fp
;
1745 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1746 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1748 if (ctc
== NULL
|| (ctc
->ctc_flags
& ~CTF_FUNC_VARARG
) != 0
1749 || (ctc
->ctc_argc
!= 0 && argv
== NULL
))
1750 return (ctf_set_errno (fp
, EINVAL
));
1752 vlen
= ctc
->ctc_argc
;
1753 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
1754 vlen
++; /* Add trailing zero to indicate varargs (see below). */
1756 if (ctc
->ctc_return
!= 0
1757 && ctf_lookup_by_id (&tmp
, ctc
->ctc_return
) == NULL
)
1758 return CTF_ERR
; /* errno is set for us. */
1760 if (vlen
> CTF_MAX_VLEN
)
1761 return (ctf_set_errno (fp
, EOVERFLOW
));
1763 if (vlen
!= 0 && (vdat
= malloc (sizeof (ctf_id_t
) * vlen
)) == NULL
)
1764 return (ctf_set_errno (fp
, EAGAIN
));
1766 for (i
= 0; i
< ctc
->ctc_argc
; i
++)
1769 if (argv
[i
] != 0 && ctf_lookup_by_id (&tmp
, argv
[i
]) == NULL
)
1772 return CTF_ERR
; /* errno is set for us. */
1774 vdat
[i
] = (uint32_t) argv
[i
];
1777 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_FUNCTION
,
1781 return CTF_ERR
; /* errno is set for us. */
1784 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FUNCTION
, flag
, vlen
);
1785 dtd
->dtd_data
.ctt_type
= (uint32_t) ctc
->ctc_return
;
1787 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
1788 vdat
[vlen
- 1] = 0; /* Add trailing zero to indicate varargs. */
1789 dtd
->dtd_u
.dtu_argv
= vdat
;
1795 ctf_add_struct_sized (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1801 /* Promote root-visible forwards to structs. */
1803 type
= ctf_lookup_by_rawname (fp
, CTF_K_STRUCT
, name
);
1805 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1806 dtd
= ctf_dtd_lookup (fp
, type
);
1807 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_STRUCT
,
1809 return CTF_ERR
; /* errno is set for us. */
1811 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_STRUCT
, flag
, 0);
1813 if (size
> CTF_MAX_SIZE
)
1815 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1816 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1817 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1820 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1826 ctf_add_struct (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
1828 return (ctf_add_struct_sized (fp
, flag
, name
, 0));
1832 ctf_add_union_sized (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1838 /* Promote root-visible forwards to unions. */
1840 type
= ctf_lookup_by_rawname (fp
, CTF_K_UNION
, name
);
1842 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1843 dtd
= ctf_dtd_lookup (fp
, type
);
1844 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_UNION
,
1846 return CTF_ERR
; /* errno is set for us */
1848 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_UNION
, flag
, 0);
1850 if (size
> CTF_MAX_SIZE
)
1852 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1853 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1854 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1857 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1863 ctf_add_union (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
1865 return (ctf_add_union_sized (fp
, flag
, name
, 0));
1869 ctf_add_enum (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
1874 /* Promote root-visible forwards to enums. */
1876 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
1878 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1879 dtd
= ctf_dtd_lookup (fp
, type
);
1880 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_ENUM
,
1882 return CTF_ERR
; /* errno is set for us. */
1884 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ENUM
, flag
, 0);
1885 dtd
->dtd_data
.ctt_size
= fp
->ctf_dmodel
->ctd_int
;
1891 ctf_add_enum_encoded (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1892 const ctf_encoding_t
*ep
)
1896 /* First, create the enum if need be, using most of the same machinery as
1897 ctf_add_enum(), to ensure that we do not allow things past that are not
1898 enums or forwards to them. (This includes other slices: you cannot slice a
1899 slice, which would be a useless thing to do anyway.) */
1902 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
1906 if ((ctf_type_kind (fp
, type
) != CTF_K_FORWARD
) &&
1907 (ctf_type_kind_unsliced (fp
, type
) != CTF_K_ENUM
))
1908 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1910 else if ((type
= ctf_add_enum (fp
, flag
, name
)) == CTF_ERR
)
1911 return CTF_ERR
; /* errno is set for us. */
1913 /* Now attach a suitable slice to it. */
1915 return ctf_add_slice (fp
, flag
, type
, ep
);
1919 ctf_add_forward (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1925 if (!ctf_forwardable_kind (kind
))
1926 return (ctf_set_errno (fp
, ECTF_NOTSUE
));
1928 /* If the type is already defined or exists as a forward tag, just
1929 return the ctf_id_t of the existing definition. */
1932 type
= ctf_lookup_by_rawname (fp
, kind
, name
);
1937 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, &dtd
)) == CTF_ERR
)
1938 return CTF_ERR
; /* errno is set for us. */
1940 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FORWARD
, flag
, 0);
1941 dtd
->dtd_data
.ctt_type
= kind
;
1947 ctf_add_typedef (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1952 ctf_dict_t
*tmp
= fp
;
1954 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
1955 return (ctf_set_errno (fp
, EINVAL
));
1957 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1958 return CTF_ERR
; /* errno is set for us. */
1960 if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_TYPEDEF
,
1962 return CTF_ERR
; /* errno is set for us. */
1964 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_TYPEDEF
, flag
, 0);
1965 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
1971 ctf_add_volatile (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1973 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_VOLATILE
));
1977 ctf_add_const (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1979 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_CONST
));
1983 ctf_add_restrict (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1985 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_RESTRICT
));
1989 ctf_add_enumerator (ctf_dict_t
*fp
, ctf_id_t enid
, const char *name
,
1992 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, enid
);
1995 uint32_t kind
, vlen
, root
;
1999 return (ctf_set_errno (fp
, EINVAL
));
2001 if (!(fp
->ctf_flags
& LCTF_RDWR
))
2002 return (ctf_set_errno (fp
, ECTF_RDONLY
));
2005 return (ctf_set_errno (fp
, ECTF_BADID
));
2007 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
2008 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
2009 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
2011 if (kind
!= CTF_K_ENUM
)
2012 return (ctf_set_errno (fp
, ECTF_NOTENUM
));
2014 if (vlen
== CTF_MAX_VLEN
)
2015 return (ctf_set_errno (fp
, ECTF_DTFULL
));
2017 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
2018 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
2020 if (strcmp (dmd
->dmd_name
, name
) == 0)
2021 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
2024 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
2025 return (ctf_set_errno (fp
, EAGAIN
));
2027 if ((s
= strdup (name
)) == NULL
)
2030 return (ctf_set_errno (fp
, EAGAIN
));
2034 dmd
->dmd_type
= CTF_ERR
;
2035 dmd
->dmd_offset
= 0;
2036 dmd
->dmd_value
= value
;
2038 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
2039 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
2041 fp
->ctf_flags
|= LCTF_DIRTY
;
2047 ctf_add_member_offset (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
2048 ctf_id_t type
, unsigned long bit_offset
)
2050 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, souid
);
2053 ssize_t msize
, malign
, ssize
;
2054 uint32_t kind
, vlen
, root
;
2056 int is_incomplete
= 0;
2058 if (!(fp
->ctf_flags
& LCTF_RDWR
))
2059 return (ctf_set_errno (fp
, ECTF_RDONLY
));
2062 return (ctf_set_errno (fp
, ECTF_BADID
));
2064 if (name
!= NULL
&& name
[0] == '\0')
2067 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
2068 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
2069 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
2071 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
2072 return (ctf_set_errno (fp
, ECTF_NOTSOU
));
2074 if (vlen
== CTF_MAX_VLEN
)
2075 return (ctf_set_errno (fp
, ECTF_DTFULL
));
2079 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
2080 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
2082 if (dmd
->dmd_name
!= NULL
&& strcmp (dmd
->dmd_name
, name
) == 0)
2083 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
2087 if ((msize
= ctf_type_size (fp
, type
)) < 0 ||
2088 (malign
= ctf_type_align (fp
, type
)) < 0)
2090 /* The unimplemented type, and any type that resolves to it, has no size
2091 and no alignment: it can correspond to any number of compiler-inserted
2092 types. We allow incomplete types through since they are routinely
2093 added to the ends of structures, and can even be added elsewhere in
2094 structures by the deduplicator. They are assumed to be zero-size with
2095 no alignment: this is often wrong, but problems can be avoided in this
2096 case by explicitly specifying the size of the structure via the _sized
2097 functions. The deduplicator always does this. */
2101 if (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
)
2102 ctf_set_errno (fp
, 0);
2103 else if (ctf_errno (fp
) == ECTF_INCOMPLETE
)
2106 return -1; /* errno is set for us. */
2109 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
2110 return (ctf_set_errno (fp
, EAGAIN
));
2112 if (name
!= NULL
&& (s
= strdup (name
)) == NULL
)
2115 return (ctf_set_errno (fp
, EAGAIN
));
2119 dmd
->dmd_type
= type
;
2120 dmd
->dmd_value
= -1;
2122 if (kind
== CTF_K_STRUCT
&& vlen
!= 0)
2124 if (bit_offset
== (unsigned long) - 1)
2126 /* Natural alignment. */
2128 ctf_dmdef_t
*lmd
= ctf_list_prev (&dtd
->dtd_u
.dtu_members
);
2129 ctf_id_t ltype
= ctf_type_resolve (fp
, lmd
->dmd_type
);
2130 size_t off
= lmd
->dmd_offset
;
2132 ctf_encoding_t linfo
;
2135 /* Propagate any error from ctf_type_resolve. If the last member was
2136 of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we
2137 cannot insert right after such a member without explicit offset
2138 specification, because its alignment and size is not known. */
2139 if (ltype
== CTF_ERR
)
2142 return -1; /* errno is set for us. */
2147 ctf_err_warn (fp
, 1, ECTF_INCOMPLETE
,
2148 _("ctf_add_member_offset: cannot add member %s of "
2149 "incomplete type %lx to struct %lx without "
2150 "specifying explicit offset\n"),
2151 name
? name
: _("(unnamed member)"), type
, souid
);
2152 return (ctf_set_errno (fp
, ECTF_INCOMPLETE
));
2155 if (ctf_type_encoding (fp
, ltype
, &linfo
) == 0)
2156 off
+= linfo
.cte_bits
;
2157 else if ((lsize
= ctf_type_size (fp
, ltype
)) > 0)
2158 off
+= lsize
* CHAR_BIT
;
2159 else if (lsize
== -1 && ctf_errno (fp
) == ECTF_INCOMPLETE
)
2161 ctf_err_warn (fp
, 1, ECTF_INCOMPLETE
,
2162 _("ctf_add_member_offset: cannot add member %s of "
2163 "type %lx to struct %lx without specifying "
2164 "explicit offset after member %s of type %lx, "
2165 "which is an incomplete type\n"),
2166 name
? name
: _("(unnamed member)"), type
, souid
,
2167 lmd
->dmd_name
? lmd
->dmd_name
2168 : _("(unnamed member)"), ltype
);
2169 return -1; /* errno is set for us. */
2172 /* Round up the offset of the end of the last member to
2173 the next byte boundary, convert 'off' to bytes, and
2174 then round it up again to the next multiple of the
2175 alignment required by the new member. Finally,
2176 convert back to bits and store the result in
2177 dmd_offset. Technically we could do more efficient
2178 packing if the new member is a bit-field, but we're
2179 the "compiler" and ANSI says we can do as we choose. */
2181 off
= roundup (off
, CHAR_BIT
) / CHAR_BIT
;
2182 off
= roundup (off
, MAX (malign
, 1));
2183 dmd
->dmd_offset
= off
* CHAR_BIT
;
2184 ssize
= off
+ msize
;
2188 /* Specified offset in bits. */
2190 dmd
->dmd_offset
= bit_offset
;
2191 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
2192 ssize
= MAX (ssize
, ((signed) bit_offset
/ CHAR_BIT
) + msize
);
2197 dmd
->dmd_offset
= 0;
2198 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
2199 ssize
= MAX (ssize
, msize
);
2202 if ((size_t) ssize
> CTF_MAX_SIZE
)
2204 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
2205 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (ssize
);
2206 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (ssize
);
2209 dtd
->dtd_data
.ctt_size
= (uint32_t) ssize
;
2211 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
2212 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
2214 fp
->ctf_flags
|= LCTF_DIRTY
;
2219 ctf_add_member_encoded (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
2220 ctf_id_t type
, unsigned long bit_offset
,
2221 const ctf_encoding_t encoding
)
2223 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
2224 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
2227 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) && (kind
!= CTF_K_ENUM
))
2228 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
2230 if ((type
= ctf_add_slice (fp
, CTF_ADD_NONROOT
, otype
, &encoding
)) == CTF_ERR
)
2231 return -1; /* errno is set for us. */
2233 return ctf_add_member_offset (fp
, souid
, name
, type
, bit_offset
);
2237 ctf_add_member (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
2240 return ctf_add_member_offset (fp
, souid
, name
, type
, (unsigned long) - 1);
2244 ctf_add_variable (ctf_dict_t
*fp
, const char *name
, ctf_id_t ref
)
2247 ctf_dict_t
*tmp
= fp
;
2249 if (!(fp
->ctf_flags
& LCTF_RDWR
))
2250 return (ctf_set_errno (fp
, ECTF_RDONLY
));
2252 if (ctf_dvd_lookup (fp
, name
) != NULL
)
2253 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
2255 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
2256 return -1; /* errno is set for us. */
2258 /* Make sure this type is representable. */
2259 if ((ctf_type_resolve (fp
, ref
) == CTF_ERR
)
2260 && (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
))
2263 if ((dvd
= malloc (sizeof (ctf_dvdef_t
))) == NULL
)
2264 return (ctf_set_errno (fp
, EAGAIN
));
2266 if (name
!= NULL
&& (dvd
->dvd_name
= strdup (name
)) == NULL
)
2269 return (ctf_set_errno (fp
, EAGAIN
));
2271 dvd
->dvd_type
= ref
;
2272 dvd
->dvd_snapshots
= fp
->ctf_snapshots
;
2274 if (ctf_dvd_insert (fp
, dvd
) < 0)
2276 free (dvd
->dvd_name
);
2278 return -1; /* errno is set for us. */
2281 fp
->ctf_flags
|= LCTF_DIRTY
;
2286 ctf_add_funcobjt_sym (ctf_dict_t
*fp
, int is_function
, const char *name
, ctf_id_t id
)
2288 ctf_dict_t
*tmp
= fp
;
2290 ctf_dynhash_t
*h
= is_function
? fp
->ctf_funchash
: fp
->ctf_objthash
;
2292 if (!(fp
->ctf_flags
& LCTF_RDWR
))
2293 return (ctf_set_errno (fp
, ECTF_RDONLY
));
2295 if (ctf_dynhash_lookup (fp
->ctf_objthash
, name
) != NULL
||
2296 ctf_dynhash_lookup (fp
->ctf_funchash
, name
) != NULL
)
2297 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
2299 if (ctf_lookup_by_id (&tmp
, id
) == NULL
)
2300 return -1; /* errno is set for us. */
2302 if (is_function
&& ctf_type_kind (fp
, id
) != CTF_K_FUNCTION
)
2303 return (ctf_set_errno (fp
, ECTF_NOTFUNC
));
2305 if ((dupname
= strdup (name
)) == NULL
)
2306 return (ctf_set_errno (fp
, ENOMEM
));
2308 if (ctf_dynhash_insert (h
, dupname
, (void *) (uintptr_t) id
) < 0)
2311 return (ctf_set_errno (fp
, ENOMEM
));
2317 ctf_add_objt_sym (ctf_dict_t
*fp
, const char *name
, ctf_id_t id
)
2319 return (ctf_add_funcobjt_sym (fp
, 0, name
, id
));
2323 ctf_add_func_sym (ctf_dict_t
*fp
, const char *name
, ctf_id_t id
)
2325 return (ctf_add_funcobjt_sym (fp
, 1, name
, id
));
2328 typedef struct ctf_bundle
2330 ctf_dict_t
*ctb_dict
; /* CTF dict handle. */
2331 ctf_id_t ctb_type
; /* CTF type identifier. */
2332 ctf_dtdef_t
*ctb_dtd
; /* CTF dynamic type definition (if any). */
2336 enumcmp (const char *name
, int value
, void *arg
)
2338 ctf_bundle_t
*ctb
= arg
;
2341 if (ctf_enum_value (ctb
->ctb_dict
, ctb
->ctb_type
, name
, &bvalue
) < 0)
2343 ctf_err_warn (ctb
->ctb_dict
, 0, 0,
2344 _("conflict due to enum %s iteration error"), name
);
2347 if (value
!= bvalue
)
2349 ctf_err_warn (ctb
->ctb_dict
, 1, ECTF_CONFLICT
,
2350 _("conflict due to enum value change: %i versus %i"),
2358 enumadd (const char *name
, int value
, void *arg
)
2360 ctf_bundle_t
*ctb
= arg
;
2362 return (ctf_add_enumerator (ctb
->ctb_dict
, ctb
->ctb_type
,
2367 membcmp (const char *name
, ctf_id_t type _libctf_unused_
, unsigned long offset
,
2370 ctf_bundle_t
*ctb
= arg
;
2373 /* Don't check nameless members (e.g. anonymous structs/unions) against each
2378 if (ctf_member_info (ctb
->ctb_dict
, ctb
->ctb_type
, name
, &ctm
) < 0)
2380 ctf_err_warn (ctb
->ctb_dict
, 0, 0,
2381 _("conflict due to struct member %s iteration error"),
2385 if (ctm
.ctm_offset
!= offset
)
2387 ctf_err_warn (ctb
->ctb_dict
, 1, ECTF_CONFLICT
,
2388 _("conflict due to struct member %s offset change: "
2390 name
, ctm
.ctm_offset
, offset
);
2397 membadd (const char *name
, ctf_id_t type
, unsigned long offset
, void *arg
)
2399 ctf_bundle_t
*ctb
= arg
;
2403 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
2404 return (ctf_set_errno (ctb
->ctb_dict
, EAGAIN
));
2406 /* Unnamed members in non-dynamic dicts have a name of "", while dynamic dicts
2412 if (name
!= NULL
&& (s
= strdup (name
)) == NULL
)
2415 return (ctf_set_errno (ctb
->ctb_dict
, EAGAIN
));
2418 /* For now, dmd_type is copied as the src_fp's type; it is reset to an
2419 equivalent dst_fp type by a final loop in ctf_add_type(), below. */
2421 dmd
->dmd_type
= type
;
2422 dmd
->dmd_offset
= offset
;
2423 dmd
->dmd_value
= -1;
2425 ctf_list_append (&ctb
->ctb_dtd
->dtd_u
.dtu_members
, dmd
);
2427 ctb
->ctb_dict
->ctf_flags
|= LCTF_DIRTY
;
2431 /* The ctf_add_type routine is used to copy a type from a source CTF dictionary
2432 to a dynamic destination dictionary. This routine operates recursively by
2433 following the source type's links and embedded member types. If the
2434 destination dict already contains a named type which has the same attributes,
2435 then we succeed and return this type but no changes occur. */
2437 ctf_add_type_internal (ctf_dict_t
*dst_fp
, ctf_dict_t
*src_fp
, ctf_id_t src_type
,
2438 ctf_dict_t
*proc_tracking_fp
)
2440 ctf_id_t dst_type
= CTF_ERR
;
2441 uint32_t dst_kind
= CTF_K_UNKNOWN
;
2442 ctf_dict_t
*tmp_fp
= dst_fp
;
2446 uint32_t kind
, forward_kind
, flag
, vlen
;
2448 const ctf_type_t
*src_tp
, *dst_tp
;
2449 ctf_bundle_t src
, dst
;
2450 ctf_encoding_t src_en
, dst_en
;
2451 ctf_arinfo_t src_ar
, dst_ar
;
2455 ctf_id_t orig_src_type
= src_type
;
2457 if (!(dst_fp
->ctf_flags
& LCTF_RDWR
))
2458 return (ctf_set_errno (dst_fp
, ECTF_RDONLY
));
2460 if ((src_tp
= ctf_lookup_by_id (&src_fp
, src_type
)) == NULL
)
2461 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
2463 if ((ctf_type_resolve (src_fp
, src_type
) == CTF_ERR
)
2464 && (ctf_errno (src_fp
) == ECTF_NONREPRESENTABLE
))
2465 return (ctf_set_errno (dst_fp
, ECTF_NONREPRESENTABLE
));
2467 name
= ctf_strptr (src_fp
, src_tp
->ctt_name
);
2468 kind
= LCTF_INFO_KIND (src_fp
, src_tp
->ctt_info
);
2469 flag
= LCTF_INFO_ISROOT (src_fp
, src_tp
->ctt_info
);
2470 vlen
= LCTF_INFO_VLEN (src_fp
, src_tp
->ctt_info
);
2472 /* If this is a type we are currently in the middle of adding, hand it
2473 straight back. (This lets us handle self-referential structures without
2474 considering forwards and empty structures the same as their completed
2477 tmp
= ctf_type_mapping (src_fp
, src_type
, &tmp_fp
);
2481 if (ctf_dynhash_lookup (proc_tracking_fp
->ctf_add_processing
,
2482 (void *) (uintptr_t) src_type
))
2485 /* If this type has already been added from this dictionary, and is the
2486 same kind and (if a struct or union) has the same number of members,
2487 hand it straight back. */
2489 if (ctf_type_kind_unsliced (tmp_fp
, tmp
) == (int) kind
)
2491 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
2492 || kind
== CTF_K_ENUM
)
2494 if ((dst_tp
= ctf_lookup_by_id (&tmp_fp
, dst_type
)) != NULL
)
2495 if (vlen
== LCTF_INFO_VLEN (tmp_fp
, dst_tp
->ctt_info
))
2503 forward_kind
= kind
;
2504 if (kind
== CTF_K_FORWARD
)
2505 forward_kind
= src_tp
->ctt_type
;
2507 /* If the source type has a name and is a root type (visible at the top-level
2508 scope), lookup the name in the destination dictionary and verify that it is
2509 of the same kind before we do anything else. */
2511 if ((flag
& CTF_ADD_ROOT
) && name
[0] != '\0'
2512 && (tmp
= ctf_lookup_by_rawname (dst_fp
, forward_kind
, name
)) != 0)
2515 dst_kind
= ctf_type_kind_unsliced (dst_fp
, dst_type
);
2518 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
2519 unless dst_type is a forward declaration and src_type is a struct,
2520 union, or enum (i.e. the definition of the previous forward decl).
2522 We also allow addition in the opposite order (addition of a forward when a
2523 struct, union, or enum already exists), which is a NOP and returns the
2524 already-present struct, union, or enum. */
2526 if (dst_type
!= CTF_ERR
&& dst_kind
!= kind
)
2528 if (kind
== CTF_K_FORWARD
2529 && (dst_kind
== CTF_K_ENUM
|| dst_kind
== CTF_K_STRUCT
2530 || dst_kind
== CTF_K_UNION
))
2532 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
2536 if (dst_kind
!= CTF_K_FORWARD
2537 || (kind
!= CTF_K_ENUM
&& kind
!= CTF_K_STRUCT
2538 && kind
!= CTF_K_UNION
))
2540 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
2541 _("ctf_add_type: conflict for type %s: "
2542 "kinds differ, new: %i; old (ID %lx): %i"),
2543 name
, kind
, dst_type
, dst_kind
);
2544 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2548 /* We take special action for an integer, float, or slice since it is
2549 described not only by its name but also its encoding. For integers,
2550 bit-fields exploit this degeneracy. */
2552 if (kind
== CTF_K_INTEGER
|| kind
== CTF_K_FLOAT
|| kind
== CTF_K_SLICE
)
2554 if (ctf_type_encoding (src_fp
, src_type
, &src_en
) != 0)
2555 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
2557 if (dst_type
!= CTF_ERR
)
2559 ctf_dict_t
*fp
= dst_fp
;
2561 if ((dst_tp
= ctf_lookup_by_id (&fp
, dst_type
)) == NULL
)
2564 if (ctf_type_encoding (dst_fp
, dst_type
, &dst_en
) != 0)
2565 return CTF_ERR
; /* errno set for us. */
2567 if (LCTF_INFO_ISROOT (fp
, dst_tp
->ctt_info
) & CTF_ADD_ROOT
)
2569 /* The type that we found in the hash is also root-visible. If
2570 the two types match then use the existing one; otherwise,
2571 declare a conflict. Note: slices are not certain to match
2572 even if there is no conflict: we must check the contained type
2575 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
2577 if (kind
!= CTF_K_SLICE
)
2579 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
2585 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2590 /* We found a non-root-visible type in the hash. If its encoding
2591 is the same, we can reuse it, unless it is a slice. */
2593 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
2595 if (kind
!= CTF_K_SLICE
)
2597 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
2605 src
.ctb_dict
= src_fp
;
2606 src
.ctb_type
= src_type
;
2609 dst
.ctb_dict
= dst_fp
;
2610 dst
.ctb_type
= dst_type
;
2613 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add
2614 a new type with the same properties as src_type to dst_fp. If dst_type is
2615 not CTF_ERR, then we verify that dst_type has the same attributes as
2616 src_type. We recurse for embedded references. Before we start, we note
2617 that we are processing this type, to prevent infinite recursion: we do not
2618 re-process any type that appears in this list. The list is emptied
2619 wholesale at the end of processing everything in this recursive stack. */
2621 if (ctf_dynhash_insert (proc_tracking_fp
->ctf_add_processing
,
2622 (void *) (uintptr_t) src_type
, (void *) 1) < 0)
2623 return ctf_set_errno (dst_fp
, ENOMEM
);
2628 /* If we found a match we will have either returned it or declared a
2630 dst_type
= ctf_add_integer (dst_fp
, flag
, name
, &src_en
);
2634 /* If we found a match we will have either returned it or declared a
2636 dst_type
= ctf_add_float (dst_fp
, flag
, name
, &src_en
);
2640 /* We have checked for conflicting encodings: now try to add the
2642 src_type
= ctf_type_reference (src_fp
, src_type
);
2643 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
2646 if (src_type
== CTF_ERR
)
2647 return CTF_ERR
; /* errno is set for us. */
2649 dst_type
= ctf_add_slice (dst_fp
, flag
, src_type
, &src_en
);
2653 case CTF_K_VOLATILE
:
2655 case CTF_K_RESTRICT
:
2656 src_type
= ctf_type_reference (src_fp
, src_type
);
2657 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
2660 if (src_type
== CTF_ERR
)
2661 return CTF_ERR
; /* errno is set for us. */
2663 dst_type
= ctf_add_reftype (dst_fp
, flag
, src_type
, kind
);
2667 if (ctf_array_info (src_fp
, src_type
, &src_ar
) != 0)
2668 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
2670 src_ar
.ctr_contents
=
2671 ctf_add_type_internal (dst_fp
, src_fp
, src_ar
.ctr_contents
,
2673 src_ar
.ctr_index
= ctf_add_type_internal (dst_fp
, src_fp
,
2676 src_ar
.ctr_nelems
= src_ar
.ctr_nelems
;
2678 if (src_ar
.ctr_contents
== CTF_ERR
|| src_ar
.ctr_index
== CTF_ERR
)
2679 return CTF_ERR
; /* errno is set for us. */
2681 if (dst_type
!= CTF_ERR
)
2683 if (ctf_array_info (dst_fp
, dst_type
, &dst_ar
) != 0)
2684 return CTF_ERR
; /* errno is set for us. */
2686 if (memcmp (&src_ar
, &dst_ar
, sizeof (ctf_arinfo_t
)))
2688 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
2689 _("conflict for type %s against ID %lx: array info "
2690 "differs, old %lx/%lx/%x; new: %lx/%lx/%x"),
2691 name
, dst_type
, src_ar
.ctr_contents
,
2692 src_ar
.ctr_index
, src_ar
.ctr_nelems
,
2693 dst_ar
.ctr_contents
, dst_ar
.ctr_index
,
2695 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2699 dst_type
= ctf_add_array (dst_fp
, flag
, &src_ar
);
2702 case CTF_K_FUNCTION
:
2703 ctc
.ctc_return
= ctf_add_type_internal (dst_fp
, src_fp
,
2709 if (ctc
.ctc_return
== CTF_ERR
)
2710 return CTF_ERR
; /* errno is set for us. */
2712 dst_type
= ctf_add_function (dst_fp
, flag
, &ctc
, NULL
);
2724 /* Technically to match a struct or union we need to check both
2725 ways (src members vs. dst, dst members vs. src) but we make
2726 this more optimal by only checking src vs. dst and comparing
2727 the total size of the structure (which we must do anyway)
2728 which covers the possibility of dst members not in src.
2729 This optimization can be defeated for unions, but is so
2730 pathological as to render it irrelevant for our purposes. */
2732 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
2733 && dst_kind
!= CTF_K_FORWARD
)
2735 if (ctf_type_size (src_fp
, src_type
) !=
2736 ctf_type_size (dst_fp
, dst_type
))
2738 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
2739 _("conflict for type %s against ID %lx: union "
2740 "size differs, old %li, new %li"), name
,
2741 dst_type
, (long) ctf_type_size (src_fp
, src_type
),
2742 (long) ctf_type_size (dst_fp
, dst_type
));
2743 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2746 if (ctf_member_iter (src_fp
, src_type
, membcmp
, &dst
))
2748 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
2749 _("conflict for type %s against ID %lx: members "
2750 "differ, see above"), name
, dst_type
);
2751 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2757 /* Unlike the other cases, copying structs and unions is done
2758 manually so as to avoid repeated lookups in ctf_add_member
2759 and to ensure the exact same member offsets as in src_type. */
2761 dst_type
= ctf_add_generic (dst_fp
, flag
, name
, kind
, &dtd
);
2762 if (dst_type
== CTF_ERR
)
2763 return CTF_ERR
; /* errno is set for us. */
2765 dst
.ctb_type
= dst_type
;
2768 /* Pre-emptively add this struct to the type mapping so that
2769 structures that refer to themselves work. */
2770 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
2772 if (ctf_member_iter (src_fp
, src_type
, membadd
, &dst
) != 0)
2773 errs
++; /* Increment errs and fail at bottom of case. */
2775 if ((ssize
= ctf_type_size (src_fp
, src_type
)) < 0)
2776 return CTF_ERR
; /* errno is set for us. */
2778 size
= (size_t) ssize
;
2779 if (size
> CTF_MAX_SIZE
)
2781 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
2782 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
2783 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
2786 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
2788 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, vlen
);
2790 /* Make a final pass through the members changing each dmd_type (a
2791 src_fp type) to an equivalent type in dst_fp. We pass through all
2792 members, leaving any that fail set to CTF_ERR, unless they fail
2793 because they are marking a member of type not representable in this
2794 version of CTF, in which case we just want to silently omit them:
2795 no consumer can do anything with them anyway. */
2796 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
2797 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
2799 ctf_dict_t
*dst
= dst_fp
;
2802 memb_type
= ctf_type_mapping (src_fp
, dmd
->dmd_type
, &dst
);
2805 if ((dmd
->dmd_type
=
2806 ctf_add_type_internal (dst_fp
, src_fp
, dmd
->dmd_type
,
2807 proc_tracking_fp
)) == CTF_ERR
)
2809 if (ctf_errno (dst_fp
) != ECTF_NONREPRESENTABLE
)
2814 dmd
->dmd_type
= memb_type
;
2818 return CTF_ERR
; /* errno is set for us. */
2823 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
2824 && dst_kind
!= CTF_K_FORWARD
)
2826 if (ctf_enum_iter (src_fp
, src_type
, enumcmp
, &dst
)
2827 || ctf_enum_iter (dst_fp
, dst_type
, enumcmp
, &src
))
2829 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
2830 _("conflict for enum %s against ID %lx: members "
2831 "differ, see above"), name
, dst_type
);
2832 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2837 dst_type
= ctf_add_enum (dst_fp
, flag
, name
);
2838 if ((dst
.ctb_type
= dst_type
) == CTF_ERR
2839 || ctf_enum_iter (src_fp
, src_type
, enumadd
, &dst
))
2840 return CTF_ERR
; /* errno is set for us */
2845 if (dst_type
== CTF_ERR
)
2846 dst_type
= ctf_add_forward (dst_fp
, flag
, name
, forward_kind
);
2850 src_type
= ctf_type_reference (src_fp
, src_type
);
2851 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
2854 if (src_type
== CTF_ERR
)
2855 return CTF_ERR
; /* errno is set for us. */
2857 /* If dst_type is not CTF_ERR at this point, we should check if
2858 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
2859 ECTF_CONFLICT. However, this causes problems with bitness typedefs
2860 that vary based on things like if 32-bit then pid_t is int otherwise
2861 long. We therefore omit this check and assume that if the identically
2862 named typedef already exists in dst_fp, it is correct or
2865 if (dst_type
== CTF_ERR
)
2866 dst_type
= ctf_add_typedef (dst_fp
, flag
, name
, src_type
);
2871 return (ctf_set_errno (dst_fp
, ECTF_CORRUPT
));
2874 if (dst_type
!= CTF_ERR
)
2875 ctf_add_type_mapping (src_fp
, orig_src_type
, dst_fp
, dst_type
);
2880 ctf_add_type (ctf_dict_t
*dst_fp
, ctf_dict_t
*src_fp
, ctf_id_t src_type
)
2884 if (!src_fp
->ctf_add_processing
)
2885 src_fp
->ctf_add_processing
= ctf_dynhash_create (ctf_hash_integer
,
2886 ctf_hash_eq_integer
,
2889 /* We store the hash on the source, because it contains only source type IDs:
2890 but callers will invariably expect errors to appear on the dest. */
2891 if (!src_fp
->ctf_add_processing
)
2892 return (ctf_set_errno (dst_fp
, ENOMEM
));
2894 id
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
, src_fp
);
2895 ctf_dynhash_empty (src_fp
->ctf_add_processing
);
2900 /* Write the compressed CTF data stream to the specified gzFile descriptor. */
2902 ctf_gzwrite (ctf_dict_t
*fp
, gzFile fd
)
2904 const unsigned char *buf
;
2908 resid
= sizeof (ctf_header_t
);
2909 buf
= (unsigned char *) fp
->ctf_header
;
2912 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
2913 return (ctf_set_errno (fp
, errno
));
2918 resid
= fp
->ctf_size
;
2922 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
2923 return (ctf_set_errno (fp
, errno
));
2931 /* Compress the specified CTF data stream and write it to the specified file
2934 ctf_compress_write (ctf_dict_t
*fp
, int fd
)
2939 ctf_header_t
*hp
= &h
;
2940 ssize_t header_len
= sizeof (ctf_header_t
);
2941 ssize_t compress_len
;
2946 if (ctf_serialize (fp
) < 0)
2947 return -1; /* errno is set for us. */
2949 memcpy (hp
, fp
->ctf_header
, header_len
);
2950 hp
->cth_flags
|= CTF_F_COMPRESS
;
2951 compress_len
= compressBound (fp
->ctf_size
);
2953 if ((buf
= malloc (compress_len
)) == NULL
)
2955 ctf_err_warn (fp
, 0, 0, _("ctf_compress_write: cannot allocate %li bytes"),
2956 (unsigned long) compress_len
);
2957 return (ctf_set_errno (fp
, ECTF_ZALLOC
));
2960 if ((rc
= compress (buf
, (uLongf
*) &compress_len
,
2961 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
2963 err
= ctf_set_errno (fp
, ECTF_COMPRESS
);
2964 ctf_err_warn (fp
, 0, 0, _("zlib deflate err: %s"), zError (rc
));
2968 while (header_len
> 0)
2970 if ((len
= write (fd
, hp
, header_len
)) < 0)
2972 err
= ctf_set_errno (fp
, errno
);
2973 ctf_err_warn (fp
, 0, 0, _("ctf_compress_write: error writing header"));
2981 while (compress_len
> 0)
2983 if ((len
= write (fd
, bp
, compress_len
)) < 0)
2985 err
= ctf_set_errno (fp
, errno
);
2986 ctf_err_warn (fp
, 0, 0, _("ctf_compress_write: error writing"));
2989 compress_len
-= len
;
2998 /* Optionally compress the specified CTF data stream and return it as a new
2999 dynamically-allocated string. */
3001 ctf_write_mem (ctf_dict_t
*fp
, size_t *size
, size_t threshold
)
3006 ssize_t header_len
= sizeof (ctf_header_t
);
3007 ssize_t compress_len
;
3010 if (ctf_serialize (fp
) < 0)
3011 return NULL
; /* errno is set for us. */
3013 compress_len
= compressBound (fp
->ctf_size
);
3014 if (fp
->ctf_size
< threshold
)
3015 compress_len
= fp
->ctf_size
;
3016 if ((buf
= malloc (compress_len
3017 + sizeof (struct ctf_header
))) == NULL
)
3019 ctf_set_errno (fp
, ENOMEM
);
3020 ctf_err_warn (fp
, 0, 0, _("ctf_write_mem: cannot allocate %li bytes"),
3021 (unsigned long) (compress_len
+ sizeof (struct ctf_header
)));
3025 hp
= (ctf_header_t
*) buf
;
3026 memcpy (hp
, fp
->ctf_header
, header_len
);
3027 bp
= buf
+ sizeof (struct ctf_header
);
3028 *size
= sizeof (struct ctf_header
);
3030 if (fp
->ctf_size
< threshold
)
3032 hp
->cth_flags
&= ~CTF_F_COMPRESS
;
3033 memcpy (bp
, fp
->ctf_buf
, fp
->ctf_size
);
3034 *size
+= fp
->ctf_size
;
3038 hp
->cth_flags
|= CTF_F_COMPRESS
;
3039 if ((rc
= compress (bp
, (uLongf
*) &compress_len
,
3040 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
3042 ctf_set_errno (fp
, ECTF_COMPRESS
);
3043 ctf_err_warn (fp
, 0, 0, _("zlib deflate err: %s"), zError (rc
));
3047 *size
+= compress_len
;
3052 /* Write the uncompressed CTF data stream to the specified file descriptor. */
3054 ctf_write (ctf_dict_t
*fp
, int fd
)
3056 const unsigned char *buf
;
3060 if (ctf_serialize (fp
) < 0)
3061 return -1; /* errno is set for us. */
3063 resid
= sizeof (ctf_header_t
);
3064 buf
= (unsigned char *) fp
->ctf_header
;
3067 if ((len
= write (fd
, buf
, resid
)) <= 0)
3069 ctf_err_warn (fp
, 0, errno
, _("ctf_write: error writing header"));
3070 return (ctf_set_errno (fp
, errno
));
3076 resid
= fp
->ctf_size
;
3080 if ((len
= write (fd
, buf
, resid
)) <= 0)
3082 ctf_err_warn (fp
, 0, errno
, _("ctf_write: error writing"));
3083 return (ctf_set_errno (fp
, errno
));