2 Copyright (C) 2019-2025 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/>. */
25 #define EOVERFLOW ERANGE
29 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
32 /* The initial size of a dynamic type's vlen in members. Arbitrary: the bigger
33 this is, the less allocation needs to be done for small structure
34 initialization, and the more memory is wasted for small structures during CTF
35 construction. No effect on generated CTF or ctf_open()ed CTF. */
36 #define INITIAL_VLEN 16
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 Equally, if the ptrtab is small -- perhaps due to ctf_open of a small
52 dict -- boost it by quite a lot at first, so we don't need to keep
55 if (fp
->ctf_ptrtab
== NULL
|| fp
->ctf_ptrtab_len
< 1024)
56 new_ptrtab_len
= 1024;
57 else if ((fp
->ctf_typemax
+ 2) > fp
->ctf_ptrtab_len
)
58 new_ptrtab_len
= fp
->ctf_ptrtab_len
* 1.25;
60 if (new_ptrtab_len
!= fp
->ctf_ptrtab_len
)
64 if ((new_ptrtab
= realloc (fp
->ctf_ptrtab
,
65 new_ptrtab_len
* sizeof (uint32_t))) == NULL
)
66 return (ctf_set_errno (fp
, ENOMEM
));
68 fp
->ctf_ptrtab
= new_ptrtab
;
69 memset (fp
->ctf_ptrtab
+ fp
->ctf_ptrtab_len
, 0,
70 (new_ptrtab_len
- fp
->ctf_ptrtab_len
) * sizeof (uint32_t));
71 fp
->ctf_ptrtab_len
= new_ptrtab_len
;
76 /* Make sure a vlen has enough space: expand it otherwise. Unlike the ptrtab,
77 which grows quite slowly, the vlen grows in big jumps because it is quite
78 expensive to expand: the caller has to scan the old vlen for string refs
79 first and remove them, then re-add them afterwards. The initial size is
80 more or less arbitrary. */
82 ctf_grow_vlen (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, size_t vlen
)
84 unsigned char *old
= dtd
->dtd_vlen
;
86 if (dtd
->dtd_vlen_alloc
> vlen
)
89 if ((dtd
->dtd_vlen
= realloc (dtd
->dtd_vlen
,
90 dtd
->dtd_vlen_alloc
* 2)) == NULL
)
93 return (ctf_set_errno (fp
, ENOMEM
));
95 memset (dtd
->dtd_vlen
+ dtd
->dtd_vlen_alloc
, 0, dtd
->dtd_vlen_alloc
);
96 dtd
->dtd_vlen_alloc
*= 2;
100 /* To create an empty CTF dict, we just declare a zeroed header and call
101 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new dict r/w and
102 initialize the dynamic members. We start assigning type IDs at 1 because
103 type ID 0 is used as a sentinel and a not-found indicator. */
106 ctf_create (int *errp
)
108 static const ctf_header_t hdr
= { .cth_preamble
= { CTF_MAGIC
, CTF_VERSION
, 0 } };
110 ctf_dynhash_t
*structs
= NULL
, *unions
= NULL
, *enums
= NULL
, *names
= NULL
;
116 structs
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
118 unions
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
120 enums
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
122 names
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
124 if (!structs
|| !unions
|| !enums
|| !names
)
126 ctf_set_open_errno (errp
, EAGAIN
);
130 cts
.cts_name
= _CTF_SECTION
;
132 cts
.cts_size
= sizeof (hdr
);
135 if ((fp
= ctf_bufopen (&cts
, NULL
, NULL
, errp
)) == NULL
)
138 /* These hashes will have been initialized with a starting size of zero,
139 which is surely wrong. Use ones with slightly larger sizes. */
140 ctf_dynhash_destroy (fp
->ctf_structs
);
141 ctf_dynhash_destroy (fp
->ctf_unions
);
142 ctf_dynhash_destroy (fp
->ctf_enums
);
143 ctf_dynhash_destroy (fp
->ctf_names
);
144 fp
->ctf_structs
= structs
;
145 fp
->ctf_unions
= unions
;
146 fp
->ctf_enums
= enums
;
147 fp
->ctf_names
= names
;
149 fp
->ctf_snapshot_lu
= 0;
151 /* Make sure the ptrtab starts out at a reasonable size. */
153 ctf_set_ctl_hashes (fp
);
154 if (ctf_grow_ptrtab (fp
) < 0)
156 ctf_set_open_errno (errp
, ctf_errno (fp
));
164 ctf_dynhash_destroy (structs
);
165 ctf_dynhash_destroy (unions
);
166 ctf_dynhash_destroy (enums
);
167 ctf_dynhash_destroy (names
);
171 /* Compatibility: just update the threshold for ctf_discard. */
173 ctf_update (ctf_dict_t
*fp
)
175 fp
->ctf_dtoldid
= fp
->ctf_typemax
;
180 ctf_name_table (ctf_dict_t
*fp
, int kind
)
185 return fp
->ctf_structs
;
187 return fp
->ctf_unions
;
189 return fp
->ctf_enums
;
191 return fp
->ctf_names
;
196 ctf_dtd_insert (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, int flag
, int kind
)
199 if (ctf_dynhash_insert (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
,
201 return ctf_set_errno (fp
, ENOMEM
);
203 if (flag
== CTF_ADD_ROOT
&& dtd
->dtd_data
.ctt_name
204 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
)
206 if (ctf_dynhash_insert (ctf_name_table (fp
, kind
),
207 (char *) name
, (void *) (uintptr_t)
210 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t)
212 return ctf_set_errno (fp
, ENOMEM
);
215 ctf_list_append (&fp
->ctf_dtdefs
, dtd
);
220 ctf_dtd_delete (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
)
222 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
223 size_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
224 int name_kind
= kind
;
227 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
);
234 ctf_lmember_t
*memb
= (ctf_lmember_t
*) dtd
->dtd_vlen
;
237 for (i
= 0; i
< vlen
; i
++)
238 ctf_str_remove_ref (fp
, ctf_strraw (fp
, memb
[i
].ctlm_name
),
244 ctf_enum_t
*en
= (ctf_enum_t
*) dtd
->dtd_vlen
;
247 for (i
= 0; i
< vlen
; i
++)
248 ctf_str_remove_ref (fp
, ctf_strraw (fp
, en
[i
].cte_name
),
253 name_kind
= dtd
->dtd_data
.ctt_type
;
256 free (dtd
->dtd_vlen
);
257 dtd
->dtd_vlen_alloc
= 0;
259 if (dtd
->dtd_data
.ctt_name
260 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
)
262 if (LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
263 ctf_dynhash_remove (ctf_name_table (fp
, name_kind
), name
);
264 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
267 ctf_list_delete (&fp
->ctf_dtdefs
, dtd
);
272 ctf_dtd_lookup (const ctf_dict_t
*fp
, ctf_id_t type
)
274 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, type
))
277 return (ctf_dtdef_t
*)
278 ctf_dynhash_lookup (fp
->ctf_dthash
, (void *) (uintptr_t) type
);
282 ctf_dynamic_type (const ctf_dict_t
*fp
, ctf_id_t id
)
286 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, id
))
289 idx
= LCTF_TYPE_TO_INDEX(fp
, id
);
291 if ((unsigned long) idx
<= fp
->ctf_typemax
)
292 return ctf_dtd_lookup (fp
, id
);
297 ctf_static_type (const ctf_dict_t
*fp
, ctf_id_t id
)
301 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, id
))
304 idx
= LCTF_TYPE_TO_INDEX(fp
, id
);
306 return ((unsigned long) idx
<= fp
->ctf_stypes
);
310 ctf_dvd_insert (ctf_dict_t
*fp
, ctf_dvdef_t
*dvd
)
312 if (ctf_dynhash_insert (fp
->ctf_dvhash
, dvd
->dvd_name
, dvd
) < 0)
313 return ctf_set_errno (fp
, ENOMEM
);
314 ctf_list_append (&fp
->ctf_dvdefs
, dvd
);
319 ctf_dvd_delete (ctf_dict_t
*fp
, ctf_dvdef_t
*dvd
)
321 ctf_dynhash_remove (fp
->ctf_dvhash
, dvd
->dvd_name
);
322 free (dvd
->dvd_name
);
324 ctf_list_delete (&fp
->ctf_dvdefs
, dvd
);
329 ctf_dvd_lookup (const ctf_dict_t
*fp
, const char *name
)
331 return (ctf_dvdef_t
*) ctf_dynhash_lookup (fp
->ctf_dvhash
, name
);
334 /* Discard all of the dynamic type definitions and variable definitions that
335 have been added to the dict since the last call to ctf_update(). We locate
336 such types by scanning the dtd list and deleting elements that have type IDs
337 greater than ctf_dtoldid, which is set by ctf_update(), above, and by
338 scanning the variable list and deleting elements that have update IDs equal
339 to the current value of the last-update snapshot count (indicating that they
340 were added after the most recent call to ctf_update()). */
342 ctf_discard (ctf_dict_t
*fp
)
344 ctf_snapshot_id_t last_update
=
346 fp
->ctf_snapshot_lu
+ 1 };
348 return (ctf_rollback (fp
, last_update
));
352 ctf_snapshot (ctf_dict_t
*fp
)
354 ctf_snapshot_id_t snapid
;
355 snapid
.dtd_id
= fp
->ctf_typemax
;
356 snapid
.snapshot_id
= fp
->ctf_snapshots
++;
360 /* Like ctf_discard(), only discards everything after a particular ID. */
362 ctf_rollback (ctf_dict_t
*fp
, ctf_snapshot_id_t id
)
364 ctf_dtdef_t
*dtd
, *ntd
;
365 ctf_dvdef_t
*dvd
, *nvd
;
367 if (id
.snapshot_id
< fp
->ctf_stypes
)
368 return (ctf_set_errno (fp
, ECTF_RDONLY
));
370 if (fp
->ctf_snapshot_lu
>= id
.snapshot_id
)
371 return (ctf_set_errno (fp
, ECTF_OVERROLLBACK
));
373 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
); dtd
!= NULL
; dtd
= ntd
)
378 ntd
= ctf_list_next (dtd
);
380 if (LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_type
) <= id
.dtd_id
)
383 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
384 if (kind
== CTF_K_FORWARD
)
385 kind
= dtd
->dtd_data
.ctt_type
;
387 if (dtd
->dtd_data
.ctt_name
388 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
389 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
391 ctf_dynhash_remove (ctf_name_table (fp
, kind
), name
);
392 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
395 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
);
396 ctf_dtd_delete (fp
, dtd
);
399 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
; dvd
= nvd
)
401 nvd
= ctf_list_next (dvd
);
403 if (dvd
->dvd_snapshots
<= id
.snapshot_id
)
406 ctf_dvd_delete (fp
, dvd
);
409 fp
->ctf_typemax
= id
.dtd_id
;
410 fp
->ctf_snapshots
= id
.snapshot_id
;
415 /* Note: vlen is the amount of space *allocated* for the vlen. It may well not
416 be the amount of space used (yet): the space used is declared in per-kind
417 fashion in the dtd_data's info word. */
419 ctf_add_generic (ctf_dict_t
*fp
, uint32_t flag
, const char *name
, int kind
,
420 size_t vlen
, ctf_dtdef_t
**rp
)
425 if (flag
!= CTF_ADD_NONROOT
&& flag
!= CTF_ADD_ROOT
)
426 return (ctf_set_typed_errno (fp
, EINVAL
));
428 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) >= CTF_MAX_TYPE
)
429 return (ctf_set_typed_errno (fp
, ECTF_FULL
));
431 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) == (CTF_MAX_PTYPE
- 1))
432 return (ctf_set_typed_errno (fp
, ECTF_FULL
));
434 /* Prohibit addition of a root-visible type that is already present
435 in the non-dynamic portion. */
437 if (flag
== CTF_ADD_ROOT
&& name
!= NULL
&& name
[0] != '\0')
441 if (((existing
= ctf_dynhash_lookup_type (ctf_name_table (fp
, kind
),
443 && ctf_static_type (fp
, existing
))
444 return (ctf_set_typed_errno (fp
, ECTF_RDONLY
));
447 /* Make sure ptrtab always grows to be big enough for all types. */
448 if (ctf_grow_ptrtab (fp
) < 0)
449 return CTF_ERR
; /* errno is set for us. */
451 if ((dtd
= calloc (1, sizeof (ctf_dtdef_t
))) == NULL
)
452 return (ctf_set_typed_errno (fp
, EAGAIN
));
454 dtd
->dtd_vlen_alloc
= vlen
;
457 if ((dtd
->dtd_vlen
= calloc (1, vlen
)) == NULL
)
461 dtd
->dtd_vlen
= NULL
;
463 type
= ++fp
->ctf_typemax
;
464 type
= LCTF_INDEX_TO_TYPE (fp
, type
, (fp
->ctf_flags
& LCTF_CHILD
));
466 dtd
->dtd_data
.ctt_name
= ctf_str_add_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
467 dtd
->dtd_type
= type
;
469 if (dtd
->dtd_data
.ctt_name
== 0 && name
!= NULL
&& name
[0] != '\0')
472 if (ctf_dtd_insert (fp
, dtd
, flag
, kind
) < 0)
473 goto err
; /* errno is set for us. */
479 ctf_set_errno (fp
, EAGAIN
);
481 free (dtd
->dtd_vlen
);
486 /* When encoding integer sizes, we want to convert a byte count in the range
487 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
488 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
504 ctf_add_encoded (ctf_dict_t
*fp
, uint32_t flag
,
505 const char *name
, const ctf_encoding_t
*ep
, uint32_t kind
)
512 return (ctf_set_typed_errno (fp
, EINVAL
));
514 if (name
== NULL
|| name
[0] == '\0')
515 return (ctf_set_typed_errno (fp
, ECTF_NONAME
));
517 if (!ctf_assert (fp
, kind
== CTF_K_INTEGER
|| kind
== CTF_K_FLOAT
))
518 return CTF_ERR
; /* errno is set for us. */
520 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, sizeof (uint32_t),
522 return CTF_ERR
; /* errno is set for us. */
524 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
525 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
530 encoding
= CTF_INT_DATA (ep
->cte_format
, ep
->cte_offset
, ep
->cte_bits
);
533 encoding
= CTF_FP_DATA (ep
->cte_format
, ep
->cte_offset
, ep
->cte_bits
);
536 /* ctf_assert is opaque with -fno-inline. This dead code avoids
537 a warning about "encoding" being used uninitialized. */
540 memcpy (dtd
->dtd_vlen
, &encoding
, sizeof (encoding
));
546 ctf_add_reftype (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
, uint32_t kind
)
550 ctf_dict_t
*tmp
= fp
;
551 int child
= fp
->ctf_flags
& LCTF_CHILD
;
553 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
554 return (ctf_set_typed_errno (fp
, EINVAL
));
556 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
557 return CTF_ERR
; /* errno is set for us. */
559 if ((type
= ctf_add_generic (fp
, flag
, NULL
, kind
, 0, &dtd
)) == CTF_ERR
)
560 return CTF_ERR
; /* errno is set for us. */
562 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
563 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
565 if (kind
!= CTF_K_POINTER
)
568 /* If we are adding a pointer, update the ptrtab, pointing at this type from
569 the type it points to. Note that ctf_typemax is at this point one higher
570 than we want to check against, because it's just been incremented for the
571 addition of this type. The pptrtab is lazily-updated as needed, so is not
574 uint32_t type_idx
= LCTF_TYPE_TO_INDEX (fp
, type
);
575 uint32_t ref_idx
= LCTF_TYPE_TO_INDEX (fp
, ref
);
577 if (LCTF_TYPE_ISCHILD (fp
, ref
) == child
578 && ref_idx
< fp
->ctf_typemax
)
579 fp
->ctf_ptrtab
[ref_idx
] = type_idx
;
585 ctf_add_slice (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
,
586 const ctf_encoding_t
*ep
)
590 ctf_id_t resolved_ref
= ref
;
593 const ctf_type_t
*tp
;
594 ctf_dict_t
*tmp
= fp
;
597 return (ctf_set_typed_errno (fp
, EINVAL
));
599 if ((ep
->cte_bits
> 255) || (ep
->cte_offset
> 255))
600 return (ctf_set_typed_errno (fp
, ECTF_SLICEOVERFLOW
));
602 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
603 return (ctf_set_typed_errno (fp
, EINVAL
));
605 if (ref
!= 0 && ((tp
= ctf_lookup_by_id (&tmp
, ref
)) == NULL
))
606 return CTF_ERR
; /* errno is set for us. */
608 /* Make sure we ultimately point to an integral type. We also allow slices to
609 point to the unimplemented type, for now, because the compiler can emit
610 such slices, though they're not very much use. */
612 resolved_ref
= ctf_type_resolve_unsliced (fp
, ref
);
613 kind
= ctf_type_kind_unsliced (fp
, resolved_ref
);
615 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) &&
618 return (ctf_set_typed_errno (fp
, ECTF_NOTINTFP
));
620 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_SLICE
,
621 sizeof (ctf_slice_t
), &dtd
)) == CTF_ERR
)
622 return CTF_ERR
; /* errno is set for us. */
624 memset (&slice
, 0, sizeof (ctf_slice_t
));
626 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_SLICE
, flag
, 0);
627 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
629 slice
.cts_type
= (uint32_t) ref
;
630 slice
.cts_bits
= ep
->cte_bits
;
631 slice
.cts_offset
= ep
->cte_offset
;
632 memcpy (dtd
->dtd_vlen
, &slice
, sizeof (ctf_slice_t
));
638 ctf_add_integer (ctf_dict_t
*fp
, uint32_t flag
,
639 const char *name
, const ctf_encoding_t
*ep
)
641 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_INTEGER
));
645 ctf_add_float (ctf_dict_t
*fp
, uint32_t flag
,
646 const char *name
, const ctf_encoding_t
*ep
)
648 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_FLOAT
));
652 ctf_add_pointer (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
654 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_POINTER
));
658 ctf_add_array (ctf_dict_t
*fp
, uint32_t flag
, const ctf_arinfo_t
*arp
)
663 ctf_dict_t
*tmp
= fp
;
666 return (ctf_set_typed_errno (fp
, EINVAL
));
668 if (arp
->ctr_contents
!= 0
669 && ctf_lookup_by_id (&tmp
, arp
->ctr_contents
) == NULL
)
670 return CTF_ERR
; /* errno is set for us. */
673 if (ctf_lookup_by_id (&tmp
, arp
->ctr_index
) == NULL
)
674 return CTF_ERR
; /* errno is set for us. */
676 if (ctf_type_kind (fp
, arp
->ctr_index
) == CTF_K_FORWARD
)
678 ctf_err_warn (fp
, 1, ECTF_INCOMPLETE
,
679 _("ctf_add_array: index type %lx is incomplete"),
681 return (ctf_set_typed_errno (fp
, ECTF_INCOMPLETE
));
684 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_ARRAY
,
685 sizeof (ctf_array_t
), &dtd
)) == CTF_ERR
)
686 return CTF_ERR
; /* errno is set for us. */
688 memset (&cta
, 0, sizeof (ctf_array_t
));
690 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ARRAY
, flag
, 0);
691 dtd
->dtd_data
.ctt_size
= 0;
692 cta
.cta_contents
= (uint32_t) arp
->ctr_contents
;
693 cta
.cta_index
= (uint32_t) arp
->ctr_index
;
694 cta
.cta_nelems
= arp
->ctr_nelems
;
695 memcpy (dtd
->dtd_vlen
, &cta
, sizeof (ctf_array_t
));
701 ctf_set_array (ctf_dict_t
*fp
, ctf_id_t type
, const ctf_arinfo_t
*arp
)
703 ctf_dict_t
*ofp
= fp
;
704 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
707 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, type
))
710 /* You can only call ctf_set_array on a type you have added, not a
711 type that was read in via ctf_open(). */
712 if (type
< fp
->ctf_stypes
)
713 return (ctf_set_errno (ofp
, ECTF_RDONLY
));
716 || LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) != CTF_K_ARRAY
)
717 return (ctf_set_errno (ofp
, ECTF_BADID
));
719 vlen
= (ctf_array_t
*) dtd
->dtd_vlen
;
720 vlen
->cta_contents
= (uint32_t) arp
->ctr_contents
;
721 vlen
->cta_index
= (uint32_t) arp
->ctr_index
;
722 vlen
->cta_nelems
= arp
->ctr_nelems
;
728 ctf_add_function (ctf_dict_t
*fp
, uint32_t flag
,
729 const ctf_funcinfo_t
*ctc
, const ctf_id_t
*argv
)
735 ctf_dict_t
*tmp
= fp
;
739 if (ctc
== NULL
|| (ctc
->ctc_flags
& ~CTF_FUNC_VARARG
) != 0
740 || (ctc
->ctc_argc
!= 0 && argv
== NULL
))
741 return (ctf_set_typed_errno (fp
, EINVAL
));
743 vlen
= ctc
->ctc_argc
;
744 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
745 vlen
++; /* Add trailing zero to indicate varargs (see below). */
747 if (ctc
->ctc_return
!= 0
748 && ctf_lookup_by_id (&tmp
, ctc
->ctc_return
) == NULL
)
749 return CTF_ERR
; /* errno is set for us. */
751 if (vlen
> CTF_MAX_VLEN
)
752 return (ctf_set_typed_errno (fp
, EOVERFLOW
));
754 /* One word extra allocated for padding for 4-byte alignment if need be.
755 Not reflected in vlen: we don't want to copy anything into it, and
756 it's in addition to (e.g.) the trailing 0 indicating varargs. */
758 initial_vlen
= (sizeof (uint32_t) * (vlen
+ (vlen
& 1)));
759 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_FUNCTION
,
760 initial_vlen
, &dtd
)) == CTF_ERR
)
761 return CTF_ERR
; /* errno is set for us. */
763 vdat
= (uint32_t *) dtd
->dtd_vlen
;
765 for (i
= 0; i
< ctc
->ctc_argc
; i
++)
768 if (argv
[i
] != 0 && ctf_lookup_by_id (&tmp
, argv
[i
]) == NULL
)
769 return CTF_ERR
; /* errno is set for us. */
770 vdat
[i
] = (uint32_t) argv
[i
];
773 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FUNCTION
, flag
, vlen
);
774 dtd
->dtd_data
.ctt_type
= (uint32_t) ctc
->ctc_return
;
776 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
777 vdat
[vlen
- 1] = 0; /* Add trailing zero to indicate varargs. */
783 ctf_add_struct_sized (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
788 size_t initial_vlen
= sizeof (ctf_lmember_t
) * INITIAL_VLEN
;
790 /* Promote root-visible forwards to structs. */
791 if (name
!= NULL
&& flag
== CTF_ADD_ROOT
)
792 type
= ctf_lookup_by_rawname (fp
, CTF_K_STRUCT
, name
);
794 /* Prohibit promotion if this type was ctf_open()ed. */
795 if (type
> 0 && type
< fp
->ctf_stypes
)
796 return (ctf_set_errno (fp
, ECTF_RDONLY
));
798 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
799 dtd
= ctf_dtd_lookup (fp
, type
);
800 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_STRUCT
,
801 initial_vlen
, &dtd
)) == CTF_ERR
)
802 return CTF_ERR
; /* errno is set for us. */
804 /* Forwards won't have any vlen yet. */
805 if (dtd
->dtd_vlen_alloc
== 0)
807 if ((dtd
->dtd_vlen
= calloc (1, initial_vlen
)) == NULL
)
808 return (ctf_set_typed_errno (fp
, ENOMEM
));
809 dtd
->dtd_vlen_alloc
= initial_vlen
;
812 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_STRUCT
, flag
, 0);
813 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
814 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
815 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
821 ctf_add_struct (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
823 return (ctf_add_struct_sized (fp
, flag
, name
, 0));
827 ctf_add_union_sized (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
832 size_t initial_vlen
= sizeof (ctf_lmember_t
) * INITIAL_VLEN
;
834 /* Promote root-visible forwards to unions. */
835 if (name
!= NULL
&& flag
== CTF_ADD_ROOT
)
836 type
= ctf_lookup_by_rawname (fp
, CTF_K_UNION
, name
);
838 /* Prohibit promotion if this type was ctf_open()ed. */
839 if (type
> 0 && type
< fp
->ctf_stypes
)
840 return (ctf_set_errno (fp
, ECTF_RDONLY
));
842 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
843 dtd
= ctf_dtd_lookup (fp
, type
);
844 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_UNION
,
845 initial_vlen
, &dtd
)) == CTF_ERR
)
846 return CTF_ERR
; /* errno is set for us. */
848 /* Forwards won't have any vlen yet. */
849 if (dtd
->dtd_vlen_alloc
== 0)
851 if ((dtd
->dtd_vlen
= calloc (1, initial_vlen
)) == NULL
)
852 return (ctf_set_typed_errno (fp
, ENOMEM
));
853 dtd
->dtd_vlen_alloc
= initial_vlen
;
856 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_UNION
, flag
, 0);
857 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
858 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
859 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
865 ctf_add_union (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
867 return (ctf_add_union_sized (fp
, flag
, name
, 0));
871 ctf_add_enum (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
875 size_t initial_vlen
= sizeof (ctf_enum_t
) * INITIAL_VLEN
;
877 /* Promote root-visible forwards to enums. */
878 if (name
!= NULL
&& flag
== CTF_ADD_ROOT
)
879 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
881 /* Prohibit promotion if this type was ctf_open()ed. */
882 if (type
> 0 && type
< fp
->ctf_stypes
)
883 return (ctf_set_errno (fp
, ECTF_RDONLY
));
885 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
886 dtd
= ctf_dtd_lookup (fp
, type
);
887 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_ENUM
,
888 initial_vlen
, &dtd
)) == CTF_ERR
)
889 return CTF_ERR
; /* errno is set for us. */
891 /* Forwards won't have any vlen yet. */
892 if (dtd
->dtd_vlen_alloc
== 0)
894 if ((dtd
->dtd_vlen
= calloc (1, initial_vlen
)) == NULL
)
895 return (ctf_set_typed_errno (fp
, ENOMEM
));
896 dtd
->dtd_vlen_alloc
= initial_vlen
;
899 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ENUM
, flag
, 0);
900 dtd
->dtd_data
.ctt_size
= fp
->ctf_dmodel
->ctd_int
;
906 ctf_add_enum_encoded (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
907 const ctf_encoding_t
*ep
)
911 /* First, create the enum if need be, using most of the same machinery as
912 ctf_add_enum(), to ensure that we do not allow things past that are not
913 enums or forwards to them. (This includes other slices: you cannot slice a
914 slice, which would be a useless thing to do anyway.) */
917 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
921 if ((ctf_type_kind (fp
, type
) != CTF_K_FORWARD
) &&
922 (ctf_type_kind_unsliced (fp
, type
) != CTF_K_ENUM
))
923 return (ctf_set_typed_errno (fp
, ECTF_NOTINTFP
));
925 else if ((type
= ctf_add_enum (fp
, flag
, name
)) == CTF_ERR
)
926 return CTF_ERR
; /* errno is set for us. */
928 /* Now attach a suitable slice to it. */
930 return ctf_add_slice (fp
, flag
, type
, ep
);
934 ctf_add_forward (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
940 if (!ctf_forwardable_kind (kind
))
941 return (ctf_set_typed_errno (fp
, ECTF_NOTSUE
));
943 if (name
== NULL
|| name
[0] == '\0')
944 return (ctf_set_typed_errno (fp
, ECTF_NONAME
));
946 /* If the type is already defined or exists as a forward tag, just return
947 the ctf_id_t of the existing definition. Since this changes nothing,
948 it's safe to do even on the read-only portion of the dict. */
950 type
= ctf_lookup_by_rawname (fp
, kind
, name
);
955 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, 0, &dtd
)) == CTF_ERR
)
956 return CTF_ERR
; /* errno is set for us. */
958 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FORWARD
, flag
, 0);
959 dtd
->dtd_data
.ctt_type
= kind
;
965 ctf_add_unknown (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
970 /* If a type is already defined with this name, error (if not CTF_K_UNKNOWN)
971 or just return it. */
973 if (name
!= NULL
&& name
[0] != '\0' && flag
== CTF_ADD_ROOT
974 && (type
= ctf_lookup_by_rawname (fp
, CTF_K_UNKNOWN
, name
)))
976 if (ctf_type_kind (fp
, type
) == CTF_K_UNKNOWN
)
980 ctf_err_warn (fp
, 1, ECTF_CONFLICT
,
981 _("ctf_add_unknown: cannot add unknown type "
982 "named %s: type of this name already defined"),
983 name
? name
: _("(unnamed type)"));
984 return (ctf_set_typed_errno (fp
, ECTF_CONFLICT
));
988 if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_UNKNOWN
, 0, &dtd
)) == CTF_ERR
)
989 return CTF_ERR
; /* errno is set for us. */
991 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_UNKNOWN
, flag
, 0);
992 dtd
->dtd_data
.ctt_type
= 0;
998 ctf_add_typedef (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1003 ctf_dict_t
*tmp
= fp
;
1005 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
1006 return (ctf_set_typed_errno (fp
, EINVAL
));
1008 if (name
== NULL
|| name
[0] == '\0')
1009 return (ctf_set_typed_errno (fp
, ECTF_NONAME
));
1011 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1012 return CTF_ERR
; /* errno is set for us. */
1014 if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_TYPEDEF
, 0,
1016 return CTF_ERR
; /* errno is set for us. */
1018 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_TYPEDEF
, flag
, 0);
1019 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
1025 ctf_add_volatile (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1027 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_VOLATILE
));
1031 ctf_add_const (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1033 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_CONST
));
1037 ctf_add_restrict (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1039 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_RESTRICT
));
1043 ctf_add_enumerator (ctf_dict_t
*fp
, ctf_id_t enid
, const char *name
,
1046 ctf_dict_t
*ofp
= fp
;
1048 unsigned char *old_vlen
;
1051 uint32_t kind
, vlen
, root
;
1054 return (ctf_set_errno (fp
, EINVAL
));
1056 if ((enid
= ctf_type_resolve_unsliced (fp
, enid
)) == CTF_ERR
)
1057 return -1; /* errno is set for us. */
1059 dtd
= ctf_dtd_lookup (fp
, enid
);
1060 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, enid
))
1061 fp
= fp
->ctf_parent
;
1063 if (enid
< fp
->ctf_stypes
)
1064 return (ctf_set_errno (ofp
, ECTF_RDONLY
));
1067 return (ctf_set_errno (ofp
, ECTF_BADID
));
1069 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1070 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1071 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1073 /* Enumeration constant names are only added, and only checked for duplicates,
1074 if the enum they are part of is a root-visible type. */
1076 if (root
&& ctf_dynhash_lookup (fp
->ctf_names
, name
))
1078 if (fp
->ctf_flags
& LCTF_STRICT_NO_DUP_ENUMERATORS
)
1079 return (ctf_set_errno (ofp
, ECTF_DUPLICATE
));
1081 if (ctf_track_enumerator (fp
, enid
, name
) < 0)
1082 return (ctf_set_errno (ofp
, ctf_errno (fp
)));
1085 if (kind
!= CTF_K_ENUM
)
1086 return (ctf_set_errno (ofp
, ECTF_NOTENUM
));
1088 if (vlen
== CTF_MAX_VLEN
)
1089 return (ctf_set_errno (ofp
, ECTF_DTFULL
));
1091 old_vlen
= dtd
->dtd_vlen
;
1093 if (ctf_grow_vlen (fp
, dtd
, sizeof (ctf_enum_t
) * (vlen
+ 1)) < 0)
1094 return -1; /* errno is set for us. */
1096 en
= (ctf_enum_t
*) dtd
->dtd_vlen
;
1098 /* Remove refs in the old vlen region and reapply them. */
1100 ctf_str_move_refs (fp
, old_vlen
, sizeof (ctf_enum_t
) * vlen
, dtd
->dtd_vlen
);
1102 /* Check for constant duplication within any given enum: only needed for
1103 non-root-visible types, since the duplicate detection above does the job
1104 for root-visible types just fine. */
1106 if (root
== CTF_ADD_NONROOT
&& (fp
->ctf_flags
& LCTF_STRICT_NO_DUP_ENUMERATORS
))
1110 for (i
= 0; i
< vlen
; i
++)
1111 if (strcmp (ctf_strptr (fp
, en
[i
].cte_name
), name
) == 0)
1112 return (ctf_set_errno (ofp
, ECTF_DUPLICATE
));
1115 en
[vlen
].cte_name
= ctf_str_add_movable_ref (fp
, name
, &en
[vlen
].cte_name
);
1116 en
[vlen
].cte_value
= value
;
1118 if (en
[vlen
].cte_name
== 0 && name
!= NULL
&& name
[0] != '\0')
1119 return (ctf_set_errno (ofp
, ctf_errno (fp
)));
1121 /* Put the newly-added enumerator name into the name table if this type is
1124 if (root
== CTF_ADD_ROOT
)
1126 if (ctf_dynhash_insert (fp
->ctf_names
,
1127 (char *) ctf_strptr (fp
, en
[vlen
].cte_name
),
1128 (void *) (uintptr_t) enid
) < 0)
1129 return ctf_set_errno (fp
, ENOMEM
);
1132 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1138 ctf_add_member_offset (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
1139 ctf_id_t type
, unsigned long bit_offset
)
1141 ctf_dict_t
*ofp
= fp
;
1142 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, souid
);
1144 ssize_t msize
, malign
, ssize
;
1145 uint32_t kind
, vlen
, root
;
1147 int is_incomplete
= 0;
1148 unsigned char *old_vlen
;
1149 ctf_lmember_t
*memb
;
1151 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, souid
))
1153 /* Adding a child type to a parent, even via the child, is prohibited.
1154 Otherwise, climb to the parent and do all work there. */
1156 if (LCTF_TYPE_ISCHILD (fp
, type
))
1157 return (ctf_set_errno (ofp
, ECTF_BADID
));
1159 fp
= fp
->ctf_parent
;
1162 if (souid
< fp
->ctf_stypes
)
1163 return (ctf_set_errno (ofp
, ECTF_RDONLY
));
1166 return (ctf_set_errno (ofp
, ECTF_BADID
));
1168 if (name
!= NULL
&& name
[0] == '\0')
1171 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1172 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1173 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1175 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
1176 return (ctf_set_errno (ofp
, ECTF_NOTSOU
));
1178 if (vlen
== CTF_MAX_VLEN
)
1179 return (ctf_set_errno (ofp
, ECTF_DTFULL
));
1181 old_vlen
= dtd
->dtd_vlen
;
1182 if (ctf_grow_vlen (fp
, dtd
, sizeof (ctf_lmember_t
) * (vlen
+ 1)) < 0)
1183 return (ctf_set_errno (ofp
, ctf_errno (fp
)));
1184 memb
= (ctf_lmember_t
*) dtd
->dtd_vlen
;
1186 /* Remove pending refs in the old vlen region and reapply them. */
1188 ctf_str_move_refs (fp
, old_vlen
, sizeof (ctf_lmember_t
) * vlen
, dtd
->dtd_vlen
);
1192 for (i
= 0; i
< vlen
; i
++)
1193 if (strcmp (ctf_strptr (fp
, memb
[i
].ctlm_name
), name
) == 0)
1194 return (ctf_set_errno (ofp
, ECTF_DUPLICATE
));
1197 if ((msize
= ctf_type_size (fp
, type
)) < 0 ||
1198 (malign
= ctf_type_align (fp
, type
)) < 0)
1200 /* The unimplemented type, and any type that resolves to it, has no size
1201 and no alignment: it can correspond to any number of compiler-inserted
1202 types. We allow incomplete types through since they are routinely
1203 added to the ends of structures, and can even be added elsewhere in
1204 structures by the deduplicator. They are assumed to be zero-size with
1205 no alignment: this is often wrong, but problems can be avoided in this
1206 case by explicitly specifying the size of the structure via the _sized
1207 functions. The deduplicator always does this. */
1211 if (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
)
1212 ctf_set_errno (fp
, 0);
1213 else if (ctf_errno (fp
) == ECTF_INCOMPLETE
)
1216 return -1; /* errno is set for us. */
1219 memb
[vlen
].ctlm_name
= ctf_str_add_movable_ref (fp
, name
, &memb
[vlen
].ctlm_name
);
1220 memb
[vlen
].ctlm_type
= type
;
1221 if (memb
[vlen
].ctlm_name
== 0 && name
!= NULL
&& name
[0] != '\0')
1222 return -1; /* errno is set for us. */
1224 if (kind
== CTF_K_STRUCT
&& vlen
!= 0)
1226 if (bit_offset
== (unsigned long) - 1)
1228 /* Natural alignment. */
1230 ctf_id_t ltype
= ctf_type_resolve (fp
, memb
[vlen
- 1].ctlm_type
);
1231 size_t off
= CTF_LMEM_OFFSET(&memb
[vlen
- 1]);
1233 ctf_encoding_t linfo
;
1236 /* Propagate any error from ctf_type_resolve. If the last member was
1237 of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we
1238 cannot insert right after such a member without explicit offset
1239 specification, because its alignment and size is not known. */
1240 if (ltype
== CTF_ERR
)
1241 return -1; /* errno is set for us. */
1245 ctf_err_warn (ofp
, 1, ECTF_INCOMPLETE
,
1246 _("ctf_add_member_offset: cannot add member %s of "
1247 "incomplete type %lx to struct %lx without "
1248 "specifying explicit offset\n"),
1249 name
? name
: _("(unnamed member)"), type
, souid
);
1250 return (ctf_set_errno (ofp
, ECTF_INCOMPLETE
));
1253 if (ctf_type_encoding (fp
, ltype
, &linfo
) == 0)
1254 off
+= linfo
.cte_bits
;
1255 else if ((lsize
= ctf_type_size (fp
, ltype
)) > 0)
1256 off
+= lsize
* CHAR_BIT
;
1257 else if (lsize
== -1 && ctf_errno (fp
) == ECTF_INCOMPLETE
)
1259 const char *lname
= ctf_strraw (fp
, memb
[vlen
- 1].ctlm_name
);
1261 ctf_err_warn (ofp
, 1, ECTF_INCOMPLETE
,
1262 _("ctf_add_member_offset: cannot add member %s of "
1263 "type %lx to struct %lx without specifying "
1264 "explicit offset after member %s of type %lx, "
1265 "which is an incomplete type\n"),
1266 name
? name
: _("(unnamed member)"), type
, souid
,
1267 lname
? lname
: _("(unnamed member)"), ltype
);
1268 return (ctf_set_errno (ofp
, ECTF_INCOMPLETE
));
1271 /* Round up the offset of the end of the last member to
1272 the next byte boundary, convert 'off' to bytes, and
1273 then round it up again to the next multiple of the
1274 alignment required by the new member. Finally,
1275 convert back to bits and store the result in
1276 dmd_offset. Technically we could do more efficient
1277 packing if the new member is a bit-field, but we're
1278 the "compiler" and ANSI says we can do as we choose. */
1280 off
= roundup (off
, CHAR_BIT
) / CHAR_BIT
;
1281 off
= roundup (off
, MAX (malign
, 1));
1282 memb
[vlen
].ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI (off
* CHAR_BIT
);
1283 memb
[vlen
].ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO (off
* CHAR_BIT
);
1284 ssize
= off
+ msize
;
1288 /* Specified offset in bits. */
1290 memb
[vlen
].ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI (bit_offset
);
1291 memb
[vlen
].ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO (bit_offset
);
1292 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1293 ssize
= MAX (ssize
, ((signed) bit_offset
/ CHAR_BIT
) + msize
);
1298 memb
[vlen
].ctlm_offsethi
= 0;
1299 memb
[vlen
].ctlm_offsetlo
= 0;
1300 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1301 ssize
= MAX (ssize
, msize
);
1304 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1305 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (ssize
);
1306 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (ssize
);
1307 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1313 ctf_add_member_encoded (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
1314 ctf_id_t type
, unsigned long bit_offset
,
1315 const ctf_encoding_t encoding
)
1317 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1322 return (ctf_set_errno (fp
, ECTF_BADID
));
1324 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1326 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) && (kind
!= CTF_K_ENUM
))
1327 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1329 if ((type
= ctf_add_slice (fp
, CTF_ADD_NONROOT
, otype
, &encoding
)) == CTF_ERR
)
1330 return -1; /* errno is set for us. */
1332 return ctf_add_member_offset (fp
, souid
, name
, type
, bit_offset
);
1336 ctf_add_member (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
1339 return ctf_add_member_offset (fp
, souid
, name
, type
, (unsigned long) - 1);
1342 /* Add a variable regardless of whether or not it is already present.
1344 Internal use only. */
1346 ctf_add_variable_forced (ctf_dict_t
*fp
, const char *name
, ctf_id_t ref
)
1349 ctf_dict_t
*tmp
= fp
;
1351 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1352 return -1; /* errno is set for us. */
1354 /* Make sure this type is representable. */
1355 if ((ctf_type_resolve (fp
, ref
) == CTF_ERR
)
1356 && (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
))
1359 if ((dvd
= malloc (sizeof (ctf_dvdef_t
))) == NULL
)
1360 return (ctf_set_errno (fp
, EAGAIN
));
1362 if (name
!= NULL
&& (dvd
->dvd_name
= strdup (name
)) == NULL
)
1365 return (ctf_set_errno (fp
, EAGAIN
));
1367 dvd
->dvd_type
= ref
;
1368 dvd
->dvd_snapshots
= fp
->ctf_snapshots
;
1370 if (ctf_dvd_insert (fp
, dvd
) < 0)
1372 free (dvd
->dvd_name
);
1374 return -1; /* errno is set for us. */
1381 ctf_add_variable (ctf_dict_t
*fp
, const char *name
, ctf_id_t ref
)
1383 if (ctf_lookup_variable_here (fp
, name
) != CTF_ERR
)
1384 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1386 if (ctf_errno (fp
) != ECTF_NOTYPEDAT
)
1387 return -1; /* errno is set for us. */
1389 return ctf_add_variable_forced (fp
, name
, ref
);
1392 /* Add a function or object symbol regardless of whether or not it is already
1393 present (already existing symbols are silently overwritten).
1395 Internal use only. */
1397 ctf_add_funcobjt_sym_forced (ctf_dict_t
*fp
, int is_function
, const char *name
, ctf_id_t id
)
1399 ctf_dict_t
*tmp
= fp
;
1401 ctf_dynhash_t
*h
= is_function
? fp
->ctf_funchash
: fp
->ctf_objthash
;
1403 if (ctf_lookup_by_id (&tmp
, id
) == NULL
)
1404 return -1; /* errno is set for us. */
1406 if (is_function
&& ctf_type_kind (fp
, id
) != CTF_K_FUNCTION
)
1407 return (ctf_set_errno (fp
, ECTF_NOTFUNC
));
1409 if ((dupname
= strdup (name
)) == NULL
)
1410 return (ctf_set_errno (fp
, ENOMEM
));
1412 if (ctf_dynhash_insert (h
, dupname
, (void *) (uintptr_t) id
) < 0)
1415 return (ctf_set_errno (fp
, ENOMEM
));
1421 ctf_add_funcobjt_sym (ctf_dict_t
*fp
, int is_function
, const char *name
, ctf_id_t id
)
1423 if (ctf_lookup_by_sym_or_name (fp
, 0, name
, 0, is_function
) != CTF_ERR
)
1424 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1426 return ctf_add_funcobjt_sym_forced (fp
, is_function
, name
, id
);
1430 ctf_add_objt_sym (ctf_dict_t
*fp
, const char *name
, ctf_id_t id
)
1432 return (ctf_add_funcobjt_sym (fp
, 0, name
, id
));
1436 ctf_add_func_sym (ctf_dict_t
*fp
, const char *name
, ctf_id_t id
)
1438 return (ctf_add_funcobjt_sym (fp
, 1, name
, id
));
1441 /* Add an enumeration constant observed in a given enum type as an identifier.
1442 They appear as names that cite the enum type.
1444 Constants that appear in more than one enum, or which are already the names
1445 of types, appear in ctf_conflicting_enums as well.
1447 This is done for all enumeration types at open time, and for newly-added ones
1448 as well: if the strict-enum flag is turned on, this table must be kept up to
1449 date with enums added in the interim. */
1452 ctf_track_enumerator (ctf_dict_t
*fp
, ctf_id_t type
, const char *cte_name
)
1456 if (ctf_dynhash_lookup_type (fp
->ctf_names
, cte_name
) == 0)
1458 uint32_t name
= ctf_str_add (fp
, cte_name
);
1461 return -1; /* errno is set for us. */
1463 err
= ctf_dynhash_insert_type (fp
, fp
->ctf_names
, type
, name
);
1467 err
= ctf_dynset_insert (fp
->ctf_conflicting_enums
, (void *)
1470 ctf_set_errno (fp
, err
* -1);
1473 return -1; /* errno is set for us. */
1477 typedef struct ctf_bundle
1479 ctf_dict_t
*ctb_dict
; /* CTF dict handle. */
1480 ctf_id_t ctb_type
; /* CTF type identifier. */
1481 ctf_dtdef_t
*ctb_dtd
; /* CTF dynamic type definition (if any). */
1485 enumcmp (const char *name
, int value
, void *arg
)
1487 ctf_bundle_t
*ctb
= arg
;
1490 if (ctf_enum_value (ctb
->ctb_dict
, ctb
->ctb_type
, name
, &bvalue
) < 0)
1492 ctf_err_warn (ctb
->ctb_dict
, 0, 0,
1493 _("conflict due to enum %s iteration error"), name
);
1496 if (value
!= bvalue
)
1498 ctf_err_warn (ctb
->ctb_dict
, 1, ECTF_CONFLICT
,
1499 _("conflict due to enum value change: %i versus %i"),
1507 enumadd (const char *name
, int value
, void *arg
)
1509 ctf_bundle_t
*ctb
= arg
;
1511 return (ctf_add_enumerator (ctb
->ctb_dict
, ctb
->ctb_type
,
1516 membcmp (const char *name
, ctf_id_t type _libctf_unused_
, unsigned long offset
,
1519 ctf_bundle_t
*ctb
= arg
;
1522 /* Don't check nameless members (e.g. anonymous structs/unions) against each
1527 if (ctf_member_info (ctb
->ctb_dict
, ctb
->ctb_type
, name
, &ctm
) < 0)
1529 ctf_err_warn (ctb
->ctb_dict
, 0, 0,
1530 _("conflict due to struct member %s iteration error"),
1534 if (ctm
.ctm_offset
!= offset
)
1536 ctf_err_warn (ctb
->ctb_dict
, 1, ECTF_CONFLICT
,
1537 _("conflict due to struct member %s offset change: "
1539 name
, ctm
.ctm_offset
, offset
);
1545 /* Record the correspondence between a source and ctf_add_type()-added
1546 destination type: both types are translated into parent type IDs if need be,
1547 so they relate to the actual dictionary they are in. Outside controlled
1548 circumstances (like linking) it is probably not useful to do more than
1549 compare these pointers, since there is nothing stopping the user closing the
1550 source dict whenever they want to.
1552 Our OOM handling here is just to not do anything, because this is called deep
1553 enough in the call stack that doing anything useful is painfully difficult:
1554 the worst consequence if we do OOM is a bit of type duplication anyway. */
1557 ctf_add_type_mapping (ctf_dict_t
*src_fp
, ctf_id_t src_type
,
1558 ctf_dict_t
*dst_fp
, ctf_id_t dst_type
)
1560 if (LCTF_TYPE_ISPARENT (src_fp
, src_type
) && src_fp
->ctf_parent
)
1561 src_fp
= src_fp
->ctf_parent
;
1563 src_type
= LCTF_TYPE_TO_INDEX(src_fp
, src_type
);
1565 if (LCTF_TYPE_ISPARENT (dst_fp
, dst_type
) && dst_fp
->ctf_parent
)
1566 dst_fp
= dst_fp
->ctf_parent
;
1568 dst_type
= LCTF_TYPE_TO_INDEX(dst_fp
, dst_type
);
1570 if (dst_fp
->ctf_link_type_mapping
== NULL
)
1572 ctf_hash_fun f
= ctf_hash_type_key
;
1573 ctf_hash_eq_fun e
= ctf_hash_eq_type_key
;
1575 if ((dst_fp
->ctf_link_type_mapping
= ctf_dynhash_create (f
, e
, free
,
1580 ctf_link_type_key_t
*key
;
1581 key
= calloc (1, sizeof (struct ctf_link_type_key
));
1585 key
->cltk_fp
= src_fp
;
1586 key
->cltk_idx
= src_type
;
1588 /* No OOM checking needed, because if this doesn't work the worst we'll do is
1589 add a few more duplicate types (which will probably run out of memory
1591 ctf_dynhash_insert (dst_fp
->ctf_link_type_mapping
, key
,
1592 (void *) (uintptr_t) dst_type
);
1595 /* Look up a type mapping: return 0 if none. The DST_FP is modified to point to
1596 the parent if need be. The ID returned is from the dst_fp's perspective. */
1598 ctf_type_mapping (ctf_dict_t
*src_fp
, ctf_id_t src_type
, ctf_dict_t
**dst_fp
)
1600 ctf_link_type_key_t key
;
1601 ctf_dict_t
*target_fp
= *dst_fp
;
1602 ctf_id_t dst_type
= 0;
1604 if (LCTF_TYPE_ISPARENT (src_fp
, src_type
) && src_fp
->ctf_parent
)
1605 src_fp
= src_fp
->ctf_parent
;
1607 src_type
= LCTF_TYPE_TO_INDEX(src_fp
, src_type
);
1608 key
.cltk_fp
= src_fp
;
1609 key
.cltk_idx
= src_type
;
1611 if (target_fp
->ctf_link_type_mapping
)
1612 dst_type
= (uintptr_t) ctf_dynhash_lookup (target_fp
->ctf_link_type_mapping
,
1617 dst_type
= LCTF_INDEX_TO_TYPE (target_fp
, dst_type
,
1618 target_fp
->ctf_parent
!= NULL
);
1619 *dst_fp
= target_fp
;
1623 if (target_fp
->ctf_parent
)
1624 target_fp
= target_fp
->ctf_parent
;
1628 if (target_fp
->ctf_link_type_mapping
)
1629 dst_type
= (uintptr_t) ctf_dynhash_lookup (target_fp
->ctf_link_type_mapping
,
1633 dst_type
= LCTF_INDEX_TO_TYPE (target_fp
, dst_type
,
1634 target_fp
->ctf_parent
!= NULL
);
1636 *dst_fp
= target_fp
;
1640 /* The ctf_add_type routine is used to copy a type from a source CTF dictionary
1641 to a dynamic destination dictionary. This routine operates recursively by
1642 following the source type's links and embedded member types. If the
1643 destination dict already contains a named type which has the same attributes,
1644 then we succeed and return this type but no changes occur. */
1646 ctf_add_type_internal (ctf_dict_t
*dst_fp
, ctf_dict_t
*src_fp
, ctf_id_t src_type
,
1647 ctf_dict_t
*proc_tracking_fp
)
1649 ctf_id_t dst_type
= CTF_ERR
;
1650 uint32_t dst_kind
= CTF_K_UNKNOWN
;
1651 ctf_dict_t
*tmp_fp
= dst_fp
;
1655 uint32_t kind
, forward_kind
, flag
, vlen
;
1657 const ctf_type_t
*src_tp
, *dst_tp
;
1658 ctf_bundle_t src
, dst
;
1659 ctf_encoding_t src_en
, dst_en
;
1660 ctf_arinfo_t src_ar
, dst_ar
;
1664 ctf_id_t orig_src_type
= src_type
;
1666 if ((src_tp
= ctf_lookup_by_id (&src_fp
, src_type
)) == NULL
)
1667 return (ctf_set_typed_errno (dst_fp
, ctf_errno (src_fp
)));
1669 if ((ctf_type_resolve (src_fp
, src_type
) == CTF_ERR
)
1670 && (ctf_errno (src_fp
) == ECTF_NONREPRESENTABLE
))
1671 return (ctf_set_typed_errno (dst_fp
, ECTF_NONREPRESENTABLE
));
1673 name
= ctf_strptr (src_fp
, src_tp
->ctt_name
);
1674 kind
= LCTF_INFO_KIND (src_fp
, src_tp
->ctt_info
);
1675 flag
= LCTF_INFO_ISROOT (src_fp
, src_tp
->ctt_info
);
1676 vlen
= LCTF_INFO_VLEN (src_fp
, src_tp
->ctt_info
);
1678 /* If this is a type we are currently in the middle of adding, hand it
1679 straight back. (This lets us handle self-referential structures without
1680 considering forwards and empty structures the same as their completed
1683 tmp
= ctf_type_mapping (src_fp
, src_type
, &tmp_fp
);
1687 if (ctf_dynhash_lookup (proc_tracking_fp
->ctf_add_processing
,
1688 (void *) (uintptr_t) src_type
))
1691 /* If this type has already been added from this dictionary, and is the
1692 same kind and (if a struct or union) has the same number of members,
1693 hand it straight back. */
1695 if (ctf_type_kind_unsliced (tmp_fp
, tmp
) == (int) kind
)
1697 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
1698 || kind
== CTF_K_ENUM
)
1700 if ((dst_tp
= ctf_lookup_by_id (&tmp_fp
, dst_type
)) != NULL
)
1701 if (vlen
== LCTF_INFO_VLEN (tmp_fp
, dst_tp
->ctt_info
))
1709 forward_kind
= kind
;
1710 if (kind
== CTF_K_FORWARD
)
1711 forward_kind
= src_tp
->ctt_type
;
1713 /* If the source type has a name and is a root type (visible at the top-level
1714 scope), lookup the name in the destination dictionary and verify that it is
1715 of the same kind before we do anything else. */
1717 if ((flag
& CTF_ADD_ROOT
) && name
[0] != '\0'
1718 && (tmp
= ctf_lookup_by_rawname (dst_fp
, forward_kind
, name
)) != 0)
1721 dst_kind
= ctf_type_kind_unsliced (dst_fp
, dst_type
);
1724 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
1725 unless dst_type is a forward declaration and src_type is a struct,
1726 union, or enum (i.e. the definition of the previous forward decl).
1728 We also allow addition in the opposite order (addition of a forward when a
1729 struct, union, or enum already exists), which is a NOP and returns the
1730 already-present struct, union, or enum. */
1732 if (dst_type
!= CTF_ERR
&& dst_kind
!= kind
)
1734 if (kind
== CTF_K_FORWARD
1735 && (dst_kind
== CTF_K_ENUM
|| dst_kind
== CTF_K_STRUCT
1736 || dst_kind
== CTF_K_UNION
))
1738 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1742 if (dst_kind
!= CTF_K_FORWARD
1743 || (kind
!= CTF_K_ENUM
&& kind
!= CTF_K_STRUCT
1744 && kind
!= CTF_K_UNION
))
1746 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1747 _("ctf_add_type: conflict for type %s: "
1748 "kinds differ, new: %i; old (ID %lx): %i"),
1749 name
, kind
, dst_type
, dst_kind
);
1750 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1754 /* We take special action for an integer, float, or slice since it is
1755 described not only by its name but also its encoding. For integers,
1756 bit-fields exploit this degeneracy. */
1758 if (kind
== CTF_K_INTEGER
|| kind
== CTF_K_FLOAT
|| kind
== CTF_K_SLICE
)
1760 if (ctf_type_encoding (src_fp
, src_type
, &src_en
) != 0)
1761 return (ctf_set_typed_errno (dst_fp
, ctf_errno (src_fp
)));
1763 if (dst_type
!= CTF_ERR
)
1765 ctf_dict_t
*fp
= dst_fp
;
1767 if ((dst_tp
= ctf_lookup_by_id (&fp
, dst_type
)) == NULL
)
1770 if (ctf_type_encoding (dst_fp
, dst_type
, &dst_en
) != 0)
1771 return CTF_ERR
; /* errno set for us. */
1773 if (LCTF_INFO_ISROOT (fp
, dst_tp
->ctt_info
) & CTF_ADD_ROOT
)
1775 /* The type that we found in the hash is also root-visible. If
1776 the two types match then use the existing one; otherwise,
1777 declare a conflict. Note: slices are not certain to match
1778 even if there is no conflict: we must check the contained type
1781 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1783 if (kind
!= CTF_K_SLICE
)
1785 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1791 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1796 /* We found a non-root-visible type in the hash. If its encoding
1797 is the same, we can reuse it, unless it is a slice. */
1799 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1801 if (kind
!= CTF_K_SLICE
)
1803 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1811 src
.ctb_dict
= src_fp
;
1812 src
.ctb_type
= src_type
;
1815 dst
.ctb_dict
= dst_fp
;
1816 dst
.ctb_type
= dst_type
;
1819 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add
1820 a new type with the same properties as src_type to dst_fp. If dst_type is
1821 not CTF_ERR, then we verify that dst_type has the same attributes as
1822 src_type. We recurse for embedded references. Before we start, we note
1823 that we are processing this type, to prevent infinite recursion: we do not
1824 re-process any type that appears in this list. The list is emptied
1825 wholesale at the end of processing everything in this recursive stack. */
1827 if (ctf_dynhash_insert (proc_tracking_fp
->ctf_add_processing
,
1828 (void *) (uintptr_t) src_type
, (void *) 1) < 0)
1829 return ctf_set_typed_errno (dst_fp
, ENOMEM
);
1834 /* If we found a match we will have either returned it or declared a
1836 dst_type
= ctf_add_integer (dst_fp
, flag
, name
, &src_en
);
1840 /* If we found a match we will have either returned it or declared a
1842 dst_type
= ctf_add_float (dst_fp
, flag
, name
, &src_en
);
1846 /* We have checked for conflicting encodings: now try to add the
1848 src_type
= ctf_type_reference (src_fp
, src_type
);
1849 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1852 if (src_type
== CTF_ERR
)
1853 return CTF_ERR
; /* errno is set for us. */
1855 dst_type
= ctf_add_slice (dst_fp
, flag
, src_type
, &src_en
);
1859 case CTF_K_VOLATILE
:
1861 case CTF_K_RESTRICT
:
1862 src_type
= ctf_type_reference (src_fp
, src_type
);
1863 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1866 if (src_type
== CTF_ERR
)
1867 return CTF_ERR
; /* errno is set for us. */
1869 dst_type
= ctf_add_reftype (dst_fp
, flag
, src_type
, kind
);
1873 if (ctf_array_info (src_fp
, src_type
, &src_ar
) != 0)
1874 return (ctf_set_typed_errno (dst_fp
, ctf_errno (src_fp
)));
1876 src_ar
.ctr_contents
=
1877 ctf_add_type_internal (dst_fp
, src_fp
, src_ar
.ctr_contents
,
1879 src_ar
.ctr_index
= ctf_add_type_internal (dst_fp
, src_fp
,
1882 src_ar
.ctr_nelems
= src_ar
.ctr_nelems
;
1884 if (src_ar
.ctr_contents
== CTF_ERR
|| src_ar
.ctr_index
== CTF_ERR
)
1885 return CTF_ERR
; /* errno is set for us. */
1887 if (dst_type
!= CTF_ERR
)
1889 if (ctf_array_info (dst_fp
, dst_type
, &dst_ar
) != 0)
1890 return CTF_ERR
; /* errno is set for us. */
1892 if (memcmp (&src_ar
, &dst_ar
, sizeof (ctf_arinfo_t
)))
1894 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1895 _("conflict for type %s against ID %lx: array info "
1896 "differs, old %lx/%lx/%x; new: %lx/%lx/%x"),
1897 name
, dst_type
, src_ar
.ctr_contents
,
1898 src_ar
.ctr_index
, src_ar
.ctr_nelems
,
1899 dst_ar
.ctr_contents
, dst_ar
.ctr_index
,
1901 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1905 dst_type
= ctf_add_array (dst_fp
, flag
, &src_ar
);
1908 case CTF_K_FUNCTION
:
1909 ctc
.ctc_return
= ctf_add_type_internal (dst_fp
, src_fp
,
1915 if (ctc
.ctc_return
== CTF_ERR
)
1916 return CTF_ERR
; /* errno is set for us. */
1918 dst_type
= ctf_add_function (dst_fp
, flag
, &ctc
, NULL
);
1924 ctf_next_t
*i
= NULL
;
1926 const char *membname
;
1927 ctf_id_t src_membtype
;
1929 /* Technically to match a struct or union we need to check both
1930 ways (src members vs. dst, dst members vs. src) but we make
1931 this more optimal by only checking src vs. dst and comparing
1932 the total size of the structure (which we must do anyway)
1933 which covers the possibility of dst members not in src.
1934 This optimization can be defeated for unions, but is so
1935 pathological as to render it irrelevant for our purposes. */
1937 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
1938 && dst_kind
!= CTF_K_FORWARD
)
1940 if (ctf_type_size (src_fp
, src_type
) !=
1941 ctf_type_size (dst_fp
, dst_type
))
1943 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1944 _("conflict for type %s against ID %lx: union "
1945 "size differs, old %li, new %li"), name
,
1946 dst_type
, (long) ctf_type_size (src_fp
, src_type
),
1947 (long) ctf_type_size (dst_fp
, dst_type
));
1948 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1951 if (ctf_member_iter (src_fp
, src_type
, membcmp
, &dst
))
1953 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1954 _("conflict for type %s against ID %lx: members "
1955 "differ, see above"), name
, dst_type
);
1956 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1962 dst_type
= ctf_add_struct_sized (dst_fp
, flag
, name
,
1963 ctf_type_size (src_fp
, src_type
));
1964 if (dst_type
== CTF_ERR
)
1965 return CTF_ERR
; /* errno is set for us. */
1967 /* Pre-emptively add this struct to the type mapping so that
1968 structures that refer to themselves work. */
1969 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1971 while ((offset
= ctf_member_next (src_fp
, src_type
, &i
, &membname
,
1972 &src_membtype
, 0)) >= 0)
1974 ctf_dict_t
*dst
= dst_fp
;
1975 ctf_id_t dst_membtype
= ctf_type_mapping (src_fp
, src_membtype
, &dst
);
1977 if (dst_membtype
== 0)
1979 dst_membtype
= ctf_add_type_internal (dst_fp
, src_fp
,
1982 if (dst_membtype
== CTF_ERR
)
1984 if (ctf_errno (dst_fp
) != ECTF_NONREPRESENTABLE
)
1986 ctf_next_destroy (i
);
1992 if (ctf_add_member_offset (dst_fp
, dst_type
, membname
,
1993 dst_membtype
, offset
) < 0)
1995 ctf_next_destroy (i
);
1999 if (ctf_errno (src_fp
) != ECTF_NEXT_END
)
2000 return CTF_ERR
; /* errno is set for us. */
2005 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
2006 && dst_kind
!= CTF_K_FORWARD
)
2008 if (ctf_enum_iter (src_fp
, src_type
, enumcmp
, &dst
)
2009 || ctf_enum_iter (dst_fp
, dst_type
, enumcmp
, &src
))
2011 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
2012 _("conflict for enum %s against ID %lx: members "
2013 "differ, see above"), name
, dst_type
);
2014 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
2019 ctf_snapshot_id_t snap
= ctf_snapshot (dst_fp
);
2021 dst_type
= ctf_add_enum (dst_fp
, flag
, name
);
2022 if ((dst
.ctb_type
= dst_type
) == CTF_ERR
2023 || ctf_enum_iter (src_fp
, src_type
, enumadd
, &dst
))
2025 ctf_rollback (dst_fp
, snap
);
2026 return CTF_ERR
; /* errno is set for us */
2032 if (dst_type
== CTF_ERR
)
2033 dst_type
= ctf_add_forward (dst_fp
, flag
, name
, forward_kind
);
2037 src_type
= ctf_type_reference (src_fp
, src_type
);
2038 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
2041 if (src_type
== CTF_ERR
)
2042 return CTF_ERR
; /* errno is set for us. */
2044 /* If dst_type is not CTF_ERR at this point, we should check if
2045 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
2046 ECTF_CONFLICT. However, this causes problems with bitness typedefs
2047 that vary based on things like if 32-bit then pid_t is int otherwise
2048 long. We therefore omit this check and assume that if the identically
2049 named typedef already exists in dst_fp, it is correct or
2052 if (dst_type
== CTF_ERR
)
2053 dst_type
= ctf_add_typedef (dst_fp
, flag
, name
, src_type
);
2058 return (ctf_set_typed_errno (dst_fp
, ECTF_CORRUPT
));
2061 if (dst_type
!= CTF_ERR
)
2062 ctf_add_type_mapping (src_fp
, orig_src_type
, dst_fp
, dst_type
);
2067 ctf_add_type (ctf_dict_t
*dst_fp
, ctf_dict_t
*src_fp
, ctf_id_t src_type
)
2071 if (!src_fp
->ctf_add_processing
)
2072 src_fp
->ctf_add_processing
= ctf_dynhash_create (ctf_hash_integer
,
2073 ctf_hash_eq_integer
,
2076 /* We store the hash on the source, because it contains only source type IDs:
2077 but callers will invariably expect errors to appear on the dest. */
2078 if (!src_fp
->ctf_add_processing
)
2079 return (ctf_set_typed_errno (dst_fp
, ENOMEM
));
2081 id
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
, src_fp
);
2082 ctf_dynhash_empty (src_fp
->ctf_add_processing
);