2 Copyright (C) 2019-2024 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>
26 #define EOVERFLOW ERANGE
30 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
33 /* The initial size of a dynamic type's vlen in members. Arbitrary: the bigger
34 this is, the less allocation needs to be done for small structure
35 initialization, and the more memory is wasted for small structures during CTF
36 construction. No effect on generated CTF or ctf_open()ed CTF. */
37 #define INITIAL_VLEN 16
39 /* Make sure the ptrtab has enough space for at least one more type.
41 We start with 4KiB of ptrtab, enough for a thousand types, then grow it 25%
45 ctf_grow_ptrtab (ctf_dict_t
*fp
)
47 size_t new_ptrtab_len
= fp
->ctf_ptrtab_len
;
49 /* We allocate one more ptrtab entry than we need, for the initial zero,
50 plus one because the caller will probably allocate a new type. */
52 if (fp
->ctf_ptrtab
== NULL
)
53 new_ptrtab_len
= 1024;
54 else if ((fp
->ctf_typemax
+ 2) > fp
->ctf_ptrtab_len
)
55 new_ptrtab_len
= fp
->ctf_ptrtab_len
* 1.25;
57 if (new_ptrtab_len
!= fp
->ctf_ptrtab_len
)
61 if ((new_ptrtab
= realloc (fp
->ctf_ptrtab
,
62 new_ptrtab_len
* sizeof (uint32_t))) == NULL
)
63 return (ctf_set_errno (fp
, ENOMEM
));
65 fp
->ctf_ptrtab
= new_ptrtab
;
66 memset (fp
->ctf_ptrtab
+ fp
->ctf_ptrtab_len
, 0,
67 (new_ptrtab_len
- fp
->ctf_ptrtab_len
) * sizeof (uint32_t));
68 fp
->ctf_ptrtab_len
= new_ptrtab_len
;
73 /* Make sure a vlen has enough space: expand it otherwise. Unlike the ptrtab,
74 which grows quite slowly, the vlen grows in big jumps because it is quite
75 expensive to expand: the caller has to scan the old vlen for string refs
76 first and remove them, then re-add them afterwards. The initial size is
77 more or less arbitrary. */
79 ctf_grow_vlen (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, size_t vlen
)
81 unsigned char *old
= dtd
->dtd_vlen
;
83 if (dtd
->dtd_vlen_alloc
> vlen
)
86 if ((dtd
->dtd_vlen
= realloc (dtd
->dtd_vlen
,
87 dtd
->dtd_vlen_alloc
* 2)) == NULL
)
90 return (ctf_set_errno (fp
, ENOMEM
));
92 memset (dtd
->dtd_vlen
+ dtd
->dtd_vlen_alloc
, 0, dtd
->dtd_vlen_alloc
);
93 dtd
->dtd_vlen_alloc
*= 2;
97 /* To create an empty CTF dict, we just declare a zeroed header and call
98 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new dict r/w and
99 initialize the dynamic members. We start assigning type IDs at 1 because
100 type ID 0 is used as a sentinel and a not-found indicator. */
103 ctf_create (int *errp
)
105 static const ctf_header_t hdr
= { .cth_preamble
= { CTF_MAGIC
, CTF_VERSION
, 0 } };
107 ctf_dynhash_t
*dthash
;
108 ctf_dynhash_t
*dvhash
;
109 ctf_dynhash_t
*structs
= NULL
, *unions
= NULL
, *enums
= NULL
, *names
= NULL
;
110 ctf_dynhash_t
*objthash
= NULL
, *funchash
= NULL
;
115 dthash
= ctf_dynhash_create (ctf_hash_integer
, ctf_hash_eq_integer
,
119 ctf_set_open_errno (errp
, EAGAIN
);
123 dvhash
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
127 ctf_set_open_errno (errp
, EAGAIN
);
131 structs
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
133 unions
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
135 enums
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
137 names
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
139 objthash
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
141 funchash
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
143 if (!structs
|| !unions
|| !enums
|| !names
)
145 ctf_set_open_errno (errp
, EAGAIN
);
149 cts
.cts_name
= _CTF_SECTION
;
151 cts
.cts_size
= sizeof (hdr
);
154 if ((fp
= ctf_bufopen_internal (&cts
, NULL
, NULL
, NULL
, 1, errp
)) == NULL
)
157 fp
->ctf_structs
.ctn_writable
= structs
;
158 fp
->ctf_unions
.ctn_writable
= unions
;
159 fp
->ctf_enums
.ctn_writable
= enums
;
160 fp
->ctf_names
.ctn_writable
= names
;
161 fp
->ctf_objthash
= objthash
;
162 fp
->ctf_funchash
= funchash
;
163 fp
->ctf_dthash
= dthash
;
164 fp
->ctf_dvhash
= dvhash
;
166 fp
->ctf_snapshots
= 1;
167 fp
->ctf_snapshot_lu
= 0;
168 fp
->ctf_flags
|= LCTF_DIRTY
;
170 ctf_set_ctl_hashes (fp
);
171 ctf_setmodel (fp
, CTF_MODEL_NATIVE
);
172 if (ctf_grow_ptrtab (fp
) < 0)
174 ctf_set_open_errno (errp
, ctf_errno (fp
));
182 ctf_dynhash_destroy (structs
);
183 ctf_dynhash_destroy (unions
);
184 ctf_dynhash_destroy (enums
);
185 ctf_dynhash_destroy (names
);
186 ctf_dynhash_destroy (objthash
);
187 ctf_dynhash_destroy (funchash
);
188 ctf_dynhash_destroy (dvhash
);
190 ctf_dynhash_destroy (dthash
);
195 /* Compatibility: just update the threshold for ctf_discard. */
197 ctf_update (ctf_dict_t
*fp
)
199 if (!(fp
->ctf_flags
& LCTF_RDWR
))
200 return (ctf_set_errno (fp
, ECTF_RDONLY
));
202 fp
->ctf_dtoldid
= fp
->ctf_typemax
;
207 ctf_name_table (ctf_dict_t
*fp
, int kind
)
212 return &fp
->ctf_structs
;
214 return &fp
->ctf_unions
;
216 return &fp
->ctf_enums
;
218 return &fp
->ctf_names
;
223 ctf_dtd_insert (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, int flag
, int kind
)
226 if (ctf_dynhash_insert (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
,
228 return ctf_set_errno (fp
, ENOMEM
);
230 if (flag
== CTF_ADD_ROOT
&& dtd
->dtd_data
.ctt_name
231 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
)
233 if (ctf_dynhash_insert (ctf_name_table (fp
, kind
)->ctn_writable
,
234 (char *) name
, (void *) (uintptr_t)
237 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t)
239 return ctf_set_errno (fp
, ENOMEM
);
242 ctf_list_append (&fp
->ctf_dtdefs
, dtd
);
247 ctf_dtd_delete (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
)
249 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
250 size_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
251 int name_kind
= kind
;
254 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
);
261 ctf_lmember_t
*memb
= (ctf_lmember_t
*) dtd
->dtd_vlen
;
264 for (i
= 0; i
< vlen
; i
++)
265 ctf_str_remove_ref (fp
, ctf_strraw (fp
, memb
[i
].ctlm_name
),
271 ctf_enum_t
*en
= (ctf_enum_t
*) dtd
->dtd_vlen
;
274 for (i
= 0; i
< vlen
; i
++)
275 ctf_str_remove_ref (fp
, ctf_strraw (fp
, en
[i
].cte_name
),
280 name_kind
= dtd
->dtd_data
.ctt_type
;
283 free (dtd
->dtd_vlen
);
284 dtd
->dtd_vlen_alloc
= 0;
286 if (dtd
->dtd_data
.ctt_name
287 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
288 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
290 ctf_dynhash_remove (ctf_name_table (fp
, name_kind
)->ctn_writable
,
292 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
295 ctf_list_delete (&fp
->ctf_dtdefs
, dtd
);
300 ctf_dtd_lookup (const ctf_dict_t
*fp
, ctf_id_t type
)
302 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, type
))
305 return (ctf_dtdef_t
*)
306 ctf_dynhash_lookup (fp
->ctf_dthash
, (void *) (uintptr_t) type
);
310 ctf_dynamic_type (const ctf_dict_t
*fp
, ctf_id_t id
)
314 if (!(fp
->ctf_flags
& LCTF_RDWR
))
317 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, id
))
320 idx
= LCTF_TYPE_TO_INDEX(fp
, id
);
322 if ((unsigned long) idx
<= fp
->ctf_typemax
)
323 return ctf_dtd_lookup (fp
, id
);
328 ctf_dvd_insert (ctf_dict_t
*fp
, ctf_dvdef_t
*dvd
)
330 if (ctf_dynhash_insert (fp
->ctf_dvhash
, dvd
->dvd_name
, dvd
) < 0)
331 return ctf_set_errno (fp
, ENOMEM
);
332 ctf_list_append (&fp
->ctf_dvdefs
, dvd
);
337 ctf_dvd_delete (ctf_dict_t
*fp
, ctf_dvdef_t
*dvd
)
339 ctf_dynhash_remove (fp
->ctf_dvhash
, dvd
->dvd_name
);
340 free (dvd
->dvd_name
);
342 ctf_list_delete (&fp
->ctf_dvdefs
, dvd
);
347 ctf_dvd_lookup (const ctf_dict_t
*fp
, const char *name
)
349 return (ctf_dvdef_t
*) ctf_dynhash_lookup (fp
->ctf_dvhash
, name
);
352 /* Discard all of the dynamic type definitions and variable definitions that
353 have been added to the dict since the last call to ctf_update(). We locate
354 such types by scanning the dtd list and deleting elements that have type IDs
355 greater than ctf_dtoldid, which is set by ctf_update(), above, and by
356 scanning the variable list and deleting elements that have update IDs equal
357 to the current value of the last-update snapshot count (indicating that they
358 were added after the most recent call to ctf_update()). */
360 ctf_discard (ctf_dict_t
*fp
)
362 ctf_snapshot_id_t last_update
=
364 fp
->ctf_snapshot_lu
+ 1 };
366 /* Update required? */
367 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
370 return (ctf_rollback (fp
, last_update
));
374 ctf_snapshot (ctf_dict_t
*fp
)
376 ctf_snapshot_id_t snapid
;
377 snapid
.dtd_id
= fp
->ctf_typemax
;
378 snapid
.snapshot_id
= fp
->ctf_snapshots
++;
382 /* Like ctf_discard(), only discards everything after a particular ID. */
384 ctf_rollback (ctf_dict_t
*fp
, ctf_snapshot_id_t id
)
386 ctf_dtdef_t
*dtd
, *ntd
;
387 ctf_dvdef_t
*dvd
, *nvd
;
389 if (!(fp
->ctf_flags
& LCTF_RDWR
))
390 return (ctf_set_errno (fp
, ECTF_RDONLY
));
392 if (fp
->ctf_snapshot_lu
>= id
.snapshot_id
)
393 return (ctf_set_errno (fp
, ECTF_OVERROLLBACK
));
395 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
); dtd
!= NULL
; dtd
= ntd
)
400 ntd
= ctf_list_next (dtd
);
402 if (LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_type
) <= id
.dtd_id
)
405 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
406 if (kind
== CTF_K_FORWARD
)
407 kind
= dtd
->dtd_data
.ctt_type
;
409 if (dtd
->dtd_data
.ctt_name
410 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
411 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
413 ctf_dynhash_remove (ctf_name_table (fp
, kind
)->ctn_writable
,
415 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
418 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
);
419 ctf_dtd_delete (fp
, dtd
);
422 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
; dvd
= nvd
)
424 nvd
= ctf_list_next (dvd
);
426 if (dvd
->dvd_snapshots
<= id
.snapshot_id
)
429 ctf_dvd_delete (fp
, dvd
);
432 fp
->ctf_typemax
= id
.dtd_id
;
433 fp
->ctf_snapshots
= id
.snapshot_id
;
435 if (fp
->ctf_snapshots
== fp
->ctf_snapshot_lu
)
436 fp
->ctf_flags
&= ~LCTF_DIRTY
;
441 /* Note: vlen is the amount of space *allocated* for the vlen. It may well not
442 be the amount of space used (yet): the space used is declared in per-kind
443 fashion in the dtd_data's info word. */
445 ctf_add_generic (ctf_dict_t
*fp
, uint32_t flag
, const char *name
, int kind
,
446 size_t vlen
, ctf_dtdef_t
**rp
)
451 if (flag
!= CTF_ADD_NONROOT
&& flag
!= CTF_ADD_ROOT
)
452 return (ctf_set_typed_errno (fp
, EINVAL
));
454 if (!(fp
->ctf_flags
& LCTF_RDWR
))
455 return (ctf_set_typed_errno (fp
, ECTF_RDONLY
));
457 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) >= CTF_MAX_TYPE
)
458 return (ctf_set_typed_errno (fp
, ECTF_FULL
));
460 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) == (CTF_MAX_PTYPE
- 1))
461 return (ctf_set_typed_errno (fp
, ECTF_FULL
));
463 /* Make sure ptrtab always grows to be big enough for all types. */
464 if (ctf_grow_ptrtab (fp
) < 0)
465 return CTF_ERR
; /* errno is set for us. */
467 if ((dtd
= calloc (1, sizeof (ctf_dtdef_t
))) == NULL
)
468 return (ctf_set_typed_errno (fp
, EAGAIN
));
470 dtd
->dtd_vlen_alloc
= vlen
;
473 if ((dtd
->dtd_vlen
= calloc (1, vlen
)) == NULL
)
477 dtd
->dtd_vlen
= NULL
;
479 type
= ++fp
->ctf_typemax
;
480 type
= LCTF_INDEX_TO_TYPE (fp
, type
, (fp
->ctf_flags
& LCTF_CHILD
));
482 dtd
->dtd_data
.ctt_name
= ctf_str_add_pending (fp
, name
,
483 &dtd
->dtd_data
.ctt_name
);
484 dtd
->dtd_type
= type
;
486 if (dtd
->dtd_data
.ctt_name
== 0 && name
!= NULL
&& name
[0] != '\0')
489 if (ctf_dtd_insert (fp
, dtd
, flag
, kind
) < 0)
490 goto err
; /* errno is set for us. */
492 fp
->ctf_flags
|= LCTF_DIRTY
;
498 ctf_set_errno (fp
, EAGAIN
);
500 free (dtd
->dtd_vlen
);
505 /* When encoding integer sizes, we want to convert a byte count in the range
506 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
507 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
523 ctf_add_encoded (ctf_dict_t
*fp
, uint32_t flag
,
524 const char *name
, const ctf_encoding_t
*ep
, uint32_t kind
)
531 return (ctf_set_typed_errno (fp
, EINVAL
));
533 if (name
== NULL
|| name
[0] == '\0')
534 return (ctf_set_typed_errno (fp
, ECTF_NONAME
));
536 if (!ctf_assert (fp
, kind
== CTF_K_INTEGER
|| kind
== CTF_K_FLOAT
))
537 return CTF_ERR
; /* errno is set for us. */
539 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, sizeof (uint32_t),
541 return CTF_ERR
; /* errno is set for us. */
543 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
544 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
549 encoding
= CTF_INT_DATA (ep
->cte_format
, ep
->cte_offset
, ep
->cte_bits
);
552 encoding
= CTF_FP_DATA (ep
->cte_format
, ep
->cte_offset
, ep
->cte_bits
);
555 /* ctf_assert is opaque with -fno-inline. This dead code avoids
556 a warning about "encoding" being used uninitialized. */
559 memcpy (dtd
->dtd_vlen
, &encoding
, sizeof (encoding
));
565 ctf_add_reftype (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
, uint32_t kind
)
569 ctf_dict_t
*tmp
= fp
;
570 int child
= fp
->ctf_flags
& LCTF_CHILD
;
572 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
573 return (ctf_set_typed_errno (fp
, EINVAL
));
575 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
576 return CTF_ERR
; /* errno is set for us. */
578 if ((type
= ctf_add_generic (fp
, flag
, NULL
, kind
, 0, &dtd
)) == CTF_ERR
)
579 return CTF_ERR
; /* errno is set for us. */
581 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
582 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
584 if (kind
!= CTF_K_POINTER
)
587 /* If we are adding a pointer, update the ptrtab, pointing at this type from
588 the type it points to. Note that ctf_typemax is at this point one higher
589 than we want to check against, because it's just been incremented for the
590 addition of this type. The pptrtab is lazily-updated as needed, so is not
593 uint32_t type_idx
= LCTF_TYPE_TO_INDEX (fp
, type
);
594 uint32_t ref_idx
= LCTF_TYPE_TO_INDEX (fp
, ref
);
596 if (LCTF_TYPE_ISCHILD (fp
, ref
) == child
597 && ref_idx
< fp
->ctf_typemax
)
598 fp
->ctf_ptrtab
[ref_idx
] = type_idx
;
604 ctf_add_slice (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
,
605 const ctf_encoding_t
*ep
)
609 ctf_id_t resolved_ref
= ref
;
612 const ctf_type_t
*tp
;
613 ctf_dict_t
*tmp
= fp
;
616 return (ctf_set_typed_errno (fp
, EINVAL
));
618 if ((ep
->cte_bits
> 255) || (ep
->cte_offset
> 255))
619 return (ctf_set_typed_errno (fp
, ECTF_SLICEOVERFLOW
));
621 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
622 return (ctf_set_typed_errno (fp
, EINVAL
));
624 if (ref
!= 0 && ((tp
= ctf_lookup_by_id (&tmp
, ref
)) == NULL
))
625 return CTF_ERR
; /* errno is set for us. */
627 /* Make sure we ultimately point to an integral type. We also allow slices to
628 point to the unimplemented type, for now, because the compiler can emit
629 such slices, though they're not very much use. */
631 resolved_ref
= ctf_type_resolve_unsliced (fp
, ref
);
632 kind
= ctf_type_kind_unsliced (fp
, resolved_ref
);
634 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) &&
637 return (ctf_set_typed_errno (fp
, ECTF_NOTINTFP
));
639 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_SLICE
,
640 sizeof (ctf_slice_t
), &dtd
)) == CTF_ERR
)
641 return CTF_ERR
; /* errno is set for us. */
643 memset (&slice
, 0, sizeof (ctf_slice_t
));
645 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_SLICE
, flag
, 0);
646 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
648 slice
.cts_type
= (uint32_t) ref
;
649 slice
.cts_bits
= ep
->cte_bits
;
650 slice
.cts_offset
= ep
->cte_offset
;
651 memcpy (dtd
->dtd_vlen
, &slice
, sizeof (ctf_slice_t
));
657 ctf_add_integer (ctf_dict_t
*fp
, uint32_t flag
,
658 const char *name
, const ctf_encoding_t
*ep
)
660 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_INTEGER
));
664 ctf_add_float (ctf_dict_t
*fp
, uint32_t flag
,
665 const char *name
, const ctf_encoding_t
*ep
)
667 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_FLOAT
));
671 ctf_add_pointer (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
673 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_POINTER
));
677 ctf_add_array (ctf_dict_t
*fp
, uint32_t flag
, const ctf_arinfo_t
*arp
)
682 ctf_dict_t
*tmp
= fp
;
685 return (ctf_set_typed_errno (fp
, EINVAL
));
687 if (arp
->ctr_contents
!= 0
688 && ctf_lookup_by_id (&tmp
, arp
->ctr_contents
) == NULL
)
689 return CTF_ERR
; /* errno is set for us. */
692 if (ctf_lookup_by_id (&tmp
, arp
->ctr_index
) == NULL
)
693 return CTF_ERR
; /* errno is set for us. */
695 if (ctf_type_kind (fp
, arp
->ctr_index
) == CTF_K_FORWARD
)
697 ctf_err_warn (fp
, 1, ECTF_INCOMPLETE
,
698 _("ctf_add_array: index type %lx is incomplete"),
700 return (ctf_set_typed_errno (fp
, ECTF_INCOMPLETE
));
703 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_ARRAY
,
704 sizeof (ctf_array_t
), &dtd
)) == CTF_ERR
)
705 return CTF_ERR
; /* errno is set for us. */
707 memset (&cta
, 0, sizeof (ctf_array_t
));
709 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ARRAY
, flag
, 0);
710 dtd
->dtd_data
.ctt_size
= 0;
711 cta
.cta_contents
= (uint32_t) arp
->ctr_contents
;
712 cta
.cta_index
= (uint32_t) arp
->ctr_index
;
713 cta
.cta_nelems
= arp
->ctr_nelems
;
714 memcpy (dtd
->dtd_vlen
, &cta
, sizeof (ctf_array_t
));
720 ctf_set_array (ctf_dict_t
*fp
, ctf_id_t type
, const ctf_arinfo_t
*arp
)
722 ctf_dict_t
*ofp
= fp
;
723 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
726 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, type
))
729 if (!(ofp
->ctf_flags
& LCTF_RDWR
))
730 return (ctf_set_errno (ofp
, ECTF_RDONLY
));
732 if (!(fp
->ctf_flags
& LCTF_RDWR
))
733 return (ctf_set_errno (ofp
, ECTF_RDONLY
));
736 || LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) != CTF_K_ARRAY
)
737 return (ctf_set_errno (ofp
, ECTF_BADID
));
739 vlen
= (ctf_array_t
*) dtd
->dtd_vlen
;
740 fp
->ctf_flags
|= LCTF_DIRTY
;
741 vlen
->cta_contents
= (uint32_t) arp
->ctr_contents
;
742 vlen
->cta_index
= (uint32_t) arp
->ctr_index
;
743 vlen
->cta_nelems
= arp
->ctr_nelems
;
749 ctf_add_function (ctf_dict_t
*fp
, uint32_t flag
,
750 const ctf_funcinfo_t
*ctc
, const ctf_id_t
*argv
)
756 ctf_dict_t
*tmp
= fp
;
760 if (!(fp
->ctf_flags
& LCTF_RDWR
))
761 return (ctf_set_typed_errno (fp
, ECTF_RDONLY
));
763 if (ctc
== NULL
|| (ctc
->ctc_flags
& ~CTF_FUNC_VARARG
) != 0
764 || (ctc
->ctc_argc
!= 0 && argv
== NULL
))
765 return (ctf_set_typed_errno (fp
, EINVAL
));
767 vlen
= ctc
->ctc_argc
;
768 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
769 vlen
++; /* Add trailing zero to indicate varargs (see below). */
771 if (ctc
->ctc_return
!= 0
772 && ctf_lookup_by_id (&tmp
, ctc
->ctc_return
) == NULL
)
773 return CTF_ERR
; /* errno is set for us. */
775 if (vlen
> CTF_MAX_VLEN
)
776 return (ctf_set_typed_errno (fp
, EOVERFLOW
));
778 /* One word extra allocated for padding for 4-byte alignment if need be.
779 Not reflected in vlen: we don't want to copy anything into it, and
780 it's in addition to (e.g.) the trailing 0 indicating varargs. */
782 initial_vlen
= (sizeof (uint32_t) * (vlen
+ (vlen
& 1)));
783 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_FUNCTION
,
784 initial_vlen
, &dtd
)) == CTF_ERR
)
785 return CTF_ERR
; /* errno is set for us. */
787 vdat
= (uint32_t *) dtd
->dtd_vlen
;
789 for (i
= 0; i
< ctc
->ctc_argc
; i
++)
792 if (argv
[i
] != 0 && ctf_lookup_by_id (&tmp
, argv
[i
]) == NULL
)
793 return CTF_ERR
; /* errno is set for us. */
794 vdat
[i
] = (uint32_t) argv
[i
];
797 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FUNCTION
, flag
, vlen
);
798 dtd
->dtd_data
.ctt_type
= (uint32_t) ctc
->ctc_return
;
800 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
801 vdat
[vlen
- 1] = 0; /* Add trailing zero to indicate varargs. */
807 ctf_add_struct_sized (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
812 size_t initial_vlen
= sizeof (ctf_lmember_t
) * INITIAL_VLEN
;
814 /* Promote root-visible forwards to structs. */
816 type
= ctf_lookup_by_rawname (fp
, CTF_K_STRUCT
, name
);
818 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
819 dtd
= ctf_dtd_lookup (fp
, type
);
820 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_STRUCT
,
821 initial_vlen
, &dtd
)) == CTF_ERR
)
822 return CTF_ERR
; /* errno is set for us. */
824 /* Forwards won't have any vlen yet. */
825 if (dtd
->dtd_vlen_alloc
== 0)
827 if ((dtd
->dtd_vlen
= calloc (1, initial_vlen
)) == NULL
)
828 return (ctf_set_typed_errno (fp
, ENOMEM
));
829 dtd
->dtd_vlen_alloc
= initial_vlen
;
832 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_STRUCT
, flag
, 0);
833 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
834 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
835 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
841 ctf_add_struct (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
843 return (ctf_add_struct_sized (fp
, flag
, name
, 0));
847 ctf_add_union_sized (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
852 size_t initial_vlen
= sizeof (ctf_lmember_t
) * INITIAL_VLEN
;
854 /* Promote root-visible forwards to unions. */
856 type
= ctf_lookup_by_rawname (fp
, CTF_K_UNION
, name
);
858 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
859 dtd
= ctf_dtd_lookup (fp
, type
);
860 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_UNION
,
861 initial_vlen
, &dtd
)) == CTF_ERR
)
862 return CTF_ERR
; /* errno is set for us */
864 /* Forwards won't have any vlen yet. */
865 if (dtd
->dtd_vlen_alloc
== 0)
867 if ((dtd
->dtd_vlen
= calloc (1, initial_vlen
)) == NULL
)
868 return (ctf_set_typed_errno (fp
, ENOMEM
));
869 dtd
->dtd_vlen_alloc
= initial_vlen
;
872 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_UNION
, flag
, 0);
873 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
874 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
875 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
881 ctf_add_union (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
883 return (ctf_add_union_sized (fp
, flag
, name
, 0));
887 ctf_add_enum (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
891 size_t initial_vlen
= sizeof (ctf_enum_t
) * INITIAL_VLEN
;
893 /* Promote root-visible forwards to enums. */
895 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
897 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
898 dtd
= ctf_dtd_lookup (fp
, type
);
899 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_ENUM
,
900 initial_vlen
, &dtd
)) == CTF_ERR
)
901 return CTF_ERR
; /* errno is set for us. */
903 /* Forwards won't have any vlen yet. */
904 if (dtd
->dtd_vlen_alloc
== 0)
906 if ((dtd
->dtd_vlen
= calloc (1, initial_vlen
)) == NULL
)
907 return (ctf_set_typed_errno (fp
, ENOMEM
));
908 dtd
->dtd_vlen_alloc
= initial_vlen
;
911 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ENUM
, flag
, 0);
912 dtd
->dtd_data
.ctt_size
= fp
->ctf_dmodel
->ctd_int
;
918 ctf_add_enum_encoded (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
919 const ctf_encoding_t
*ep
)
923 /* First, create the enum if need be, using most of the same machinery as
924 ctf_add_enum(), to ensure that we do not allow things past that are not
925 enums or forwards to them. (This includes other slices: you cannot slice a
926 slice, which would be a useless thing to do anyway.) */
929 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
933 if ((ctf_type_kind (fp
, type
) != CTF_K_FORWARD
) &&
934 (ctf_type_kind_unsliced (fp
, type
) != CTF_K_ENUM
))
935 return (ctf_set_typed_errno (fp
, ECTF_NOTINTFP
));
937 else if ((type
= ctf_add_enum (fp
, flag
, name
)) == CTF_ERR
)
938 return CTF_ERR
; /* errno is set for us. */
940 /* Now attach a suitable slice to it. */
942 return ctf_add_slice (fp
, flag
, type
, ep
);
946 ctf_add_forward (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
952 if (!ctf_forwardable_kind (kind
))
953 return (ctf_set_typed_errno (fp
, ECTF_NOTSUE
));
955 if (name
== NULL
|| name
[0] == '\0')
956 return (ctf_set_typed_errno (fp
, ECTF_NONAME
));
958 /* If the type is already defined or exists as a forward tag, just
959 return the ctf_id_t of the existing definition. */
961 type
= ctf_lookup_by_rawname (fp
, kind
, name
);
966 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, 0, &dtd
)) == CTF_ERR
)
967 return CTF_ERR
; /* errno is set for us. */
969 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FORWARD
, flag
, 0);
970 dtd
->dtd_data
.ctt_type
= kind
;
976 ctf_add_unknown (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
981 /* If a type is already defined with this name, error (if not CTF_K_UNKNOWN)
982 or just return it. */
984 if (name
!= NULL
&& name
[0] != '\0' && flag
== CTF_ADD_ROOT
985 && (type
= ctf_lookup_by_rawname (fp
, CTF_K_UNKNOWN
, name
)))
987 if (ctf_type_kind (fp
, type
) == CTF_K_UNKNOWN
)
991 ctf_err_warn (fp
, 1, ECTF_CONFLICT
,
992 _("ctf_add_unknown: cannot add unknown type "
993 "named %s: type of this name already defined"),
994 name
? name
: _("(unnamed type)"));
995 return (ctf_set_typed_errno (fp
, ECTF_CONFLICT
));
999 if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_UNKNOWN
, 0, &dtd
)) == CTF_ERR
)
1000 return CTF_ERR
; /* errno is set for us. */
1002 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_UNKNOWN
, flag
, 0);
1003 dtd
->dtd_data
.ctt_type
= 0;
1009 ctf_add_typedef (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1014 ctf_dict_t
*tmp
= fp
;
1016 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
1017 return (ctf_set_typed_errno (fp
, EINVAL
));
1019 if (name
== NULL
|| name
[0] == '\0')
1020 return (ctf_set_typed_errno (fp
, ECTF_NONAME
));
1022 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1023 return CTF_ERR
; /* errno is set for us. */
1025 if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_TYPEDEF
, 0,
1027 return CTF_ERR
; /* errno is set for us. */
1029 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_TYPEDEF
, flag
, 0);
1030 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
1036 ctf_add_volatile (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1038 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_VOLATILE
));
1042 ctf_add_const (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1044 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_CONST
));
1048 ctf_add_restrict (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1050 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_RESTRICT
));
1054 ctf_add_enumerator (ctf_dict_t
*fp
, ctf_id_t enid
, const char *name
,
1057 ctf_dict_t
*ofp
= fp
;
1058 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, enid
);
1059 unsigned char *old_vlen
;
1063 uint32_t kind
, vlen
, root
;
1066 return (ctf_set_errno (fp
, EINVAL
));
1068 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, enid
))
1069 fp
= fp
->ctf_parent
;
1071 if (!(ofp
->ctf_flags
& LCTF_RDWR
))
1072 return (ctf_set_errno (ofp
, ECTF_RDONLY
));
1074 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1075 return (ctf_set_errno (ofp
, ECTF_RDONLY
));
1078 return (ctf_set_errno (ofp
, ECTF_BADID
));
1080 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1081 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1082 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1084 if (kind
!= CTF_K_ENUM
)
1085 return (ctf_set_errno (ofp
, ECTF_NOTENUM
));
1087 if (vlen
== CTF_MAX_VLEN
)
1088 return (ctf_set_errno (ofp
, ECTF_DTFULL
));
1090 old_vlen
= dtd
->dtd_vlen
;
1091 if (ctf_grow_vlen (fp
, dtd
, sizeof (ctf_enum_t
) * (vlen
+ 1)) < 0)
1092 return -1; /* errno is set for us. */
1093 en
= (ctf_enum_t
*) dtd
->dtd_vlen
;
1095 if (dtd
->dtd_vlen
!= old_vlen
)
1097 ptrdiff_t move
= (signed char *) dtd
->dtd_vlen
- (signed char *) old_vlen
;
1099 /* Remove pending refs in the old vlen region and reapply them. */
1101 for (i
= 0; i
< vlen
; i
++)
1102 ctf_str_move_pending (fp
, &en
[i
].cte_name
, move
);
1105 for (i
= 0; i
< vlen
; i
++)
1106 if (strcmp (ctf_strptr (fp
, en
[i
].cte_name
), name
) == 0)
1107 return (ctf_set_errno (ofp
, ECTF_DUPLICATE
));
1109 en
[i
].cte_name
= ctf_str_add_pending (fp
, name
, &en
[i
].cte_name
);
1110 en
[i
].cte_value
= value
;
1112 if (en
[i
].cte_name
== 0 && name
!= NULL
&& name
[0] != '\0')
1113 return (ctf_set_errno (ofp
, ctf_errno (fp
)));
1115 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1117 fp
->ctf_flags
|= LCTF_DIRTY
;
1123 ctf_add_member_offset (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
1124 ctf_id_t type
, unsigned long bit_offset
)
1126 ctf_dict_t
*ofp
= fp
;
1127 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, souid
);
1129 ssize_t msize
, malign
, ssize
;
1130 uint32_t kind
, vlen
, root
;
1132 int is_incomplete
= 0;
1133 unsigned char *old_vlen
;
1134 ctf_lmember_t
*memb
;
1136 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, souid
))
1138 /* Adding a child type to a parent, even via the child, is prohibited.
1139 Otherwise, climb to the parent and do all work there. */
1141 if (LCTF_TYPE_ISCHILD (fp
, type
))
1142 return (ctf_set_errno (ofp
, ECTF_BADID
));
1144 fp
= fp
->ctf_parent
;
1147 if (!(ofp
->ctf_flags
& LCTF_RDWR
))
1148 return (ctf_set_errno (ofp
, ECTF_RDONLY
));
1150 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1151 return (ctf_set_errno (ofp
, ECTF_RDONLY
));
1154 return (ctf_set_errno (ofp
, ECTF_BADID
));
1156 if (name
!= NULL
&& name
[0] == '\0')
1159 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1160 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1161 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1163 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
1164 return (ctf_set_errno (ofp
, ECTF_NOTSOU
));
1166 if (vlen
== CTF_MAX_VLEN
)
1167 return (ctf_set_errno (ofp
, ECTF_DTFULL
));
1169 old_vlen
= dtd
->dtd_vlen
;
1170 if (ctf_grow_vlen (fp
, dtd
, sizeof (ctf_lmember_t
) * (vlen
+ 1)) < 0)
1171 return (ctf_set_errno (ofp
, ctf_errno (fp
)));
1172 memb
= (ctf_lmember_t
*) dtd
->dtd_vlen
;
1174 if (dtd
->dtd_vlen
!= old_vlen
)
1176 ptrdiff_t move
= (signed char *) dtd
->dtd_vlen
- (signed char *) old_vlen
;
1178 /* Remove pending refs in the old vlen region and reapply them. */
1180 for (i
= 0; i
< vlen
; i
++)
1181 ctf_str_move_pending (fp
, &memb
[i
].ctlm_name
, move
);
1186 for (i
= 0; i
< vlen
; i
++)
1187 if (strcmp (ctf_strptr (fp
, memb
[i
].ctlm_name
), name
) == 0)
1188 return (ctf_set_errno (ofp
, ECTF_DUPLICATE
));
1191 if ((msize
= ctf_type_size (fp
, type
)) < 0 ||
1192 (malign
= ctf_type_align (fp
, type
)) < 0)
1194 /* The unimplemented type, and any type that resolves to it, has no size
1195 and no alignment: it can correspond to any number of compiler-inserted
1196 types. We allow incomplete types through since they are routinely
1197 added to the ends of structures, and can even be added elsewhere in
1198 structures by the deduplicator. They are assumed to be zero-size with
1199 no alignment: this is often wrong, but problems can be avoided in this
1200 case by explicitly specifying the size of the structure via the _sized
1201 functions. The deduplicator always does this. */
1205 if (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
)
1206 ctf_set_errno (fp
, 0);
1207 else if (ctf_errno (fp
) == ECTF_INCOMPLETE
)
1210 return -1; /* errno is set for us. */
1213 memb
[vlen
].ctlm_name
= ctf_str_add_pending (fp
, name
, &memb
[vlen
].ctlm_name
);
1214 memb
[vlen
].ctlm_type
= type
;
1215 if (memb
[vlen
].ctlm_name
== 0 && name
!= NULL
&& name
[0] != '\0')
1216 return -1; /* errno is set for us. */
1218 if (kind
== CTF_K_STRUCT
&& vlen
!= 0)
1220 if (bit_offset
== (unsigned long) - 1)
1222 /* Natural alignment. */
1224 ctf_id_t ltype
= ctf_type_resolve (fp
, memb
[vlen
- 1].ctlm_type
);
1225 size_t off
= CTF_LMEM_OFFSET(&memb
[vlen
- 1]);
1227 ctf_encoding_t linfo
;
1230 /* Propagate any error from ctf_type_resolve. If the last member was
1231 of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we
1232 cannot insert right after such a member without explicit offset
1233 specification, because its alignment and size is not known. */
1234 if (ltype
== CTF_ERR
)
1235 return -1; /* errno is set for us. */
1239 ctf_err_warn (ofp
, 1, ECTF_INCOMPLETE
,
1240 _("ctf_add_member_offset: cannot add member %s of "
1241 "incomplete type %lx to struct %lx without "
1242 "specifying explicit offset\n"),
1243 name
? name
: _("(unnamed member)"), type
, souid
);
1244 return (ctf_set_errno (ofp
, ECTF_INCOMPLETE
));
1247 if (ctf_type_encoding (fp
, ltype
, &linfo
) == 0)
1248 off
+= linfo
.cte_bits
;
1249 else if ((lsize
= ctf_type_size (fp
, ltype
)) > 0)
1250 off
+= lsize
* CHAR_BIT
;
1251 else if (lsize
== -1 && ctf_errno (fp
) == ECTF_INCOMPLETE
)
1253 const char *lname
= ctf_strraw (fp
, memb
[vlen
- 1].ctlm_name
);
1255 ctf_err_warn (ofp
, 1, ECTF_INCOMPLETE
,
1256 _("ctf_add_member_offset: cannot add member %s of "
1257 "type %lx to struct %lx without specifying "
1258 "explicit offset after member %s of type %lx, "
1259 "which is an incomplete type\n"),
1260 name
? name
: _("(unnamed member)"), type
, souid
,
1261 lname
? lname
: _("(unnamed member)"), ltype
);
1262 return (ctf_set_errno (ofp
, ECTF_INCOMPLETE
));
1265 /* Round up the offset of the end of the last member to
1266 the next byte boundary, convert 'off' to bytes, and
1267 then round it up again to the next multiple of the
1268 alignment required by the new member. Finally,
1269 convert back to bits and store the result in
1270 dmd_offset. Technically we could do more efficient
1271 packing if the new member is a bit-field, but we're
1272 the "compiler" and ANSI says we can do as we choose. */
1274 off
= roundup (off
, CHAR_BIT
) / CHAR_BIT
;
1275 off
= roundup (off
, MAX (malign
, 1));
1276 memb
[vlen
].ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI (off
* CHAR_BIT
);
1277 memb
[vlen
].ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO (off
* CHAR_BIT
);
1278 ssize
= off
+ msize
;
1282 /* Specified offset in bits. */
1284 memb
[vlen
].ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI (bit_offset
);
1285 memb
[vlen
].ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO (bit_offset
);
1286 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1287 ssize
= MAX (ssize
, ((signed) bit_offset
/ CHAR_BIT
) + msize
);
1292 memb
[vlen
].ctlm_offsethi
= 0;
1293 memb
[vlen
].ctlm_offsetlo
= 0;
1294 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1295 ssize
= MAX (ssize
, msize
);
1298 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1299 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (ssize
);
1300 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (ssize
);
1301 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1303 fp
->ctf_flags
|= LCTF_DIRTY
;
1308 ctf_add_member_encoded (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
1309 ctf_id_t type
, unsigned long bit_offset
,
1310 const ctf_encoding_t encoding
)
1312 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1317 return (ctf_set_errno (fp
, ECTF_BADID
));
1319 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1321 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) && (kind
!= CTF_K_ENUM
))
1322 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1324 if ((type
= ctf_add_slice (fp
, CTF_ADD_NONROOT
, otype
, &encoding
)) == CTF_ERR
)
1325 return -1; /* errno is set for us. */
1327 return ctf_add_member_offset (fp
, souid
, name
, type
, bit_offset
);
1331 ctf_add_member (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
1334 return ctf_add_member_offset (fp
, souid
, name
, type
, (unsigned long) - 1);
1338 ctf_add_variable (ctf_dict_t
*fp
, const char *name
, ctf_id_t ref
)
1341 ctf_dict_t
*tmp
= fp
;
1343 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1344 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1346 if (ctf_dvd_lookup (fp
, name
) != NULL
)
1347 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1349 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1350 return -1; /* errno is set for us. */
1352 /* Make sure this type is representable. */
1353 if ((ctf_type_resolve (fp
, ref
) == CTF_ERR
)
1354 && (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
))
1357 if ((dvd
= malloc (sizeof (ctf_dvdef_t
))) == NULL
)
1358 return (ctf_set_errno (fp
, EAGAIN
));
1360 if (name
!= NULL
&& (dvd
->dvd_name
= strdup (name
)) == NULL
)
1363 return (ctf_set_errno (fp
, EAGAIN
));
1365 dvd
->dvd_type
= ref
;
1366 dvd
->dvd_snapshots
= fp
->ctf_snapshots
;
1368 if (ctf_dvd_insert (fp
, dvd
) < 0)
1370 free (dvd
->dvd_name
);
1372 return -1; /* errno is set for us. */
1375 fp
->ctf_flags
|= LCTF_DIRTY
;
1380 ctf_add_funcobjt_sym (ctf_dict_t
*fp
, int is_function
, const char *name
, ctf_id_t id
)
1382 ctf_dict_t
*tmp
= fp
;
1384 ctf_dynhash_t
*h
= is_function
? fp
->ctf_funchash
: fp
->ctf_objthash
;
1386 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1387 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1389 if (ctf_dynhash_lookup (fp
->ctf_objthash
, name
) != NULL
||
1390 ctf_dynhash_lookup (fp
->ctf_funchash
, name
) != NULL
)
1391 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1393 if (ctf_lookup_by_id (&tmp
, id
) == NULL
)
1394 return -1; /* errno is set for us. */
1396 if (is_function
&& ctf_type_kind (fp
, id
) != CTF_K_FUNCTION
)
1397 return (ctf_set_errno (fp
, ECTF_NOTFUNC
));
1399 if ((dupname
= strdup (name
)) == NULL
)
1400 return (ctf_set_errno (fp
, ENOMEM
));
1402 if (ctf_dynhash_insert (h
, dupname
, (void *) (uintptr_t) id
) < 0)
1405 return (ctf_set_errno (fp
, ENOMEM
));
1411 ctf_add_objt_sym (ctf_dict_t
*fp
, const char *name
, ctf_id_t id
)
1413 return (ctf_add_funcobjt_sym (fp
, 0, name
, id
));
1417 ctf_add_func_sym (ctf_dict_t
*fp
, const char *name
, ctf_id_t id
)
1419 return (ctf_add_funcobjt_sym (fp
, 1, name
, id
));
1422 typedef struct ctf_bundle
1424 ctf_dict_t
*ctb_dict
; /* CTF dict handle. */
1425 ctf_id_t ctb_type
; /* CTF type identifier. */
1426 ctf_dtdef_t
*ctb_dtd
; /* CTF dynamic type definition (if any). */
1430 enumcmp (const char *name
, int value
, void *arg
)
1432 ctf_bundle_t
*ctb
= arg
;
1435 if (ctf_enum_value (ctb
->ctb_dict
, ctb
->ctb_type
, name
, &bvalue
) < 0)
1437 ctf_err_warn (ctb
->ctb_dict
, 0, 0,
1438 _("conflict due to enum %s iteration error"), name
);
1441 if (value
!= bvalue
)
1443 ctf_err_warn (ctb
->ctb_dict
, 1, ECTF_CONFLICT
,
1444 _("conflict due to enum value change: %i versus %i"),
1452 enumadd (const char *name
, int value
, void *arg
)
1454 ctf_bundle_t
*ctb
= arg
;
1456 return (ctf_add_enumerator (ctb
->ctb_dict
, ctb
->ctb_type
,
1461 membcmp (const char *name
, ctf_id_t type _libctf_unused_
, unsigned long offset
,
1464 ctf_bundle_t
*ctb
= arg
;
1467 /* Don't check nameless members (e.g. anonymous structs/unions) against each
1472 if (ctf_member_info (ctb
->ctb_dict
, ctb
->ctb_type
, name
, &ctm
) < 0)
1474 ctf_err_warn (ctb
->ctb_dict
, 0, 0,
1475 _("conflict due to struct member %s iteration error"),
1479 if (ctm
.ctm_offset
!= offset
)
1481 ctf_err_warn (ctb
->ctb_dict
, 1, ECTF_CONFLICT
,
1482 _("conflict due to struct member %s offset change: "
1484 name
, ctm
.ctm_offset
, offset
);
1490 /* Record the correspondence between a source and ctf_add_type()-added
1491 destination type: both types are translated into parent type IDs if need be,
1492 so they relate to the actual dictionary they are in. Outside controlled
1493 circumstances (like linking) it is probably not useful to do more than
1494 compare these pointers, since there is nothing stopping the user closing the
1495 source dict whenever they want to.
1497 Our OOM handling here is just to not do anything, because this is called deep
1498 enough in the call stack that doing anything useful is painfully difficult:
1499 the worst consequence if we do OOM is a bit of type duplication anyway. */
1502 ctf_add_type_mapping (ctf_dict_t
*src_fp
, ctf_id_t src_type
,
1503 ctf_dict_t
*dst_fp
, ctf_id_t dst_type
)
1505 if (LCTF_TYPE_ISPARENT (src_fp
, src_type
) && src_fp
->ctf_parent
)
1506 src_fp
= src_fp
->ctf_parent
;
1508 src_type
= LCTF_TYPE_TO_INDEX(src_fp
, src_type
);
1510 if (LCTF_TYPE_ISPARENT (dst_fp
, dst_type
) && dst_fp
->ctf_parent
)
1511 dst_fp
= dst_fp
->ctf_parent
;
1513 dst_type
= LCTF_TYPE_TO_INDEX(dst_fp
, dst_type
);
1515 if (dst_fp
->ctf_link_type_mapping
== NULL
)
1517 ctf_hash_fun f
= ctf_hash_type_key
;
1518 ctf_hash_eq_fun e
= ctf_hash_eq_type_key
;
1520 if ((dst_fp
->ctf_link_type_mapping
= ctf_dynhash_create (f
, e
, free
,
1525 ctf_link_type_key_t
*key
;
1526 key
= calloc (1, sizeof (struct ctf_link_type_key
));
1530 key
->cltk_fp
= src_fp
;
1531 key
->cltk_idx
= src_type
;
1533 /* No OOM checking needed, because if this doesn't work the worst we'll do is
1534 add a few more duplicate types (which will probably run out of memory
1536 ctf_dynhash_insert (dst_fp
->ctf_link_type_mapping
, key
,
1537 (void *) (uintptr_t) dst_type
);
1540 /* Look up a type mapping: return 0 if none. The DST_FP is modified to point to
1541 the parent if need be. The ID returned is from the dst_fp's perspective. */
1543 ctf_type_mapping (ctf_dict_t
*src_fp
, ctf_id_t src_type
, ctf_dict_t
**dst_fp
)
1545 ctf_link_type_key_t key
;
1546 ctf_dict_t
*target_fp
= *dst_fp
;
1547 ctf_id_t dst_type
= 0;
1549 if (LCTF_TYPE_ISPARENT (src_fp
, src_type
) && src_fp
->ctf_parent
)
1550 src_fp
= src_fp
->ctf_parent
;
1552 src_type
= LCTF_TYPE_TO_INDEX(src_fp
, src_type
);
1553 key
.cltk_fp
= src_fp
;
1554 key
.cltk_idx
= src_type
;
1556 if (target_fp
->ctf_link_type_mapping
)
1557 dst_type
= (uintptr_t) ctf_dynhash_lookup (target_fp
->ctf_link_type_mapping
,
1562 dst_type
= LCTF_INDEX_TO_TYPE (target_fp
, dst_type
,
1563 target_fp
->ctf_parent
!= NULL
);
1564 *dst_fp
= target_fp
;
1568 if (target_fp
->ctf_parent
)
1569 target_fp
= target_fp
->ctf_parent
;
1573 if (target_fp
->ctf_link_type_mapping
)
1574 dst_type
= (uintptr_t) ctf_dynhash_lookup (target_fp
->ctf_link_type_mapping
,
1578 dst_type
= LCTF_INDEX_TO_TYPE (target_fp
, dst_type
,
1579 target_fp
->ctf_parent
!= NULL
);
1581 *dst_fp
= target_fp
;
1585 /* The ctf_add_type routine is used to copy a type from a source CTF dictionary
1586 to a dynamic destination dictionary. This routine operates recursively by
1587 following the source type's links and embedded member types. If the
1588 destination dict already contains a named type which has the same attributes,
1589 then we succeed and return this type but no changes occur. */
1591 ctf_add_type_internal (ctf_dict_t
*dst_fp
, ctf_dict_t
*src_fp
, ctf_id_t src_type
,
1592 ctf_dict_t
*proc_tracking_fp
)
1594 ctf_id_t dst_type
= CTF_ERR
;
1595 uint32_t dst_kind
= CTF_K_UNKNOWN
;
1596 ctf_dict_t
*tmp_fp
= dst_fp
;
1600 uint32_t kind
, forward_kind
, flag
, vlen
;
1602 const ctf_type_t
*src_tp
, *dst_tp
;
1603 ctf_bundle_t src
, dst
;
1604 ctf_encoding_t src_en
, dst_en
;
1605 ctf_arinfo_t src_ar
, dst_ar
;
1609 ctf_id_t orig_src_type
= src_type
;
1611 if (!(dst_fp
->ctf_flags
& LCTF_RDWR
))
1612 return (ctf_set_typed_errno (dst_fp
, ECTF_RDONLY
));
1614 if ((src_tp
= ctf_lookup_by_id (&src_fp
, src_type
)) == NULL
)
1615 return (ctf_set_typed_errno (dst_fp
, ctf_errno (src_fp
)));
1617 if ((ctf_type_resolve (src_fp
, src_type
) == CTF_ERR
)
1618 && (ctf_errno (src_fp
) == ECTF_NONREPRESENTABLE
))
1619 return (ctf_set_typed_errno (dst_fp
, ECTF_NONREPRESENTABLE
));
1621 name
= ctf_strptr (src_fp
, src_tp
->ctt_name
);
1622 kind
= LCTF_INFO_KIND (src_fp
, src_tp
->ctt_info
);
1623 flag
= LCTF_INFO_ISROOT (src_fp
, src_tp
->ctt_info
);
1624 vlen
= LCTF_INFO_VLEN (src_fp
, src_tp
->ctt_info
);
1626 /* If this is a type we are currently in the middle of adding, hand it
1627 straight back. (This lets us handle self-referential structures without
1628 considering forwards and empty structures the same as their completed
1631 tmp
= ctf_type_mapping (src_fp
, src_type
, &tmp_fp
);
1635 if (ctf_dynhash_lookup (proc_tracking_fp
->ctf_add_processing
,
1636 (void *) (uintptr_t) src_type
))
1639 /* If this type has already been added from this dictionary, and is the
1640 same kind and (if a struct or union) has the same number of members,
1641 hand it straight back. */
1643 if (ctf_type_kind_unsliced (tmp_fp
, tmp
) == (int) kind
)
1645 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
1646 || kind
== CTF_K_ENUM
)
1648 if ((dst_tp
= ctf_lookup_by_id (&tmp_fp
, dst_type
)) != NULL
)
1649 if (vlen
== LCTF_INFO_VLEN (tmp_fp
, dst_tp
->ctt_info
))
1657 forward_kind
= kind
;
1658 if (kind
== CTF_K_FORWARD
)
1659 forward_kind
= src_tp
->ctt_type
;
1661 /* If the source type has a name and is a root type (visible at the top-level
1662 scope), lookup the name in the destination dictionary and verify that it is
1663 of the same kind before we do anything else. */
1665 if ((flag
& CTF_ADD_ROOT
) && name
[0] != '\0'
1666 && (tmp
= ctf_lookup_by_rawname (dst_fp
, forward_kind
, name
)) != 0)
1669 dst_kind
= ctf_type_kind_unsliced (dst_fp
, dst_type
);
1672 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
1673 unless dst_type is a forward declaration and src_type is a struct,
1674 union, or enum (i.e. the definition of the previous forward decl).
1676 We also allow addition in the opposite order (addition of a forward when a
1677 struct, union, or enum already exists), which is a NOP and returns the
1678 already-present struct, union, or enum. */
1680 if (dst_type
!= CTF_ERR
&& dst_kind
!= kind
)
1682 if (kind
== CTF_K_FORWARD
1683 && (dst_kind
== CTF_K_ENUM
|| dst_kind
== CTF_K_STRUCT
1684 || dst_kind
== CTF_K_UNION
))
1686 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1690 if (dst_kind
!= CTF_K_FORWARD
1691 || (kind
!= CTF_K_ENUM
&& kind
!= CTF_K_STRUCT
1692 && kind
!= CTF_K_UNION
))
1694 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1695 _("ctf_add_type: conflict for type %s: "
1696 "kinds differ, new: %i; old (ID %lx): %i"),
1697 name
, kind
, dst_type
, dst_kind
);
1698 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1702 /* We take special action for an integer, float, or slice since it is
1703 described not only by its name but also its encoding. For integers,
1704 bit-fields exploit this degeneracy. */
1706 if (kind
== CTF_K_INTEGER
|| kind
== CTF_K_FLOAT
|| kind
== CTF_K_SLICE
)
1708 if (ctf_type_encoding (src_fp
, src_type
, &src_en
) != 0)
1709 return (ctf_set_typed_errno (dst_fp
, ctf_errno (src_fp
)));
1711 if (dst_type
!= CTF_ERR
)
1713 ctf_dict_t
*fp
= dst_fp
;
1715 if ((dst_tp
= ctf_lookup_by_id (&fp
, dst_type
)) == NULL
)
1718 if (ctf_type_encoding (dst_fp
, dst_type
, &dst_en
) != 0)
1719 return CTF_ERR
; /* errno set for us. */
1721 if (LCTF_INFO_ISROOT (fp
, dst_tp
->ctt_info
) & CTF_ADD_ROOT
)
1723 /* The type that we found in the hash is also root-visible. If
1724 the two types match then use the existing one; otherwise,
1725 declare a conflict. Note: slices are not certain to match
1726 even if there is no conflict: we must check the contained type
1729 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1731 if (kind
!= CTF_K_SLICE
)
1733 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1739 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1744 /* We found a non-root-visible type in the hash. If its encoding
1745 is the same, we can reuse it, unless it is a slice. */
1747 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1749 if (kind
!= CTF_K_SLICE
)
1751 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1759 src
.ctb_dict
= src_fp
;
1760 src
.ctb_type
= src_type
;
1763 dst
.ctb_dict
= dst_fp
;
1764 dst
.ctb_type
= dst_type
;
1767 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add
1768 a new type with the same properties as src_type to dst_fp. If dst_type is
1769 not CTF_ERR, then we verify that dst_type has the same attributes as
1770 src_type. We recurse for embedded references. Before we start, we note
1771 that we are processing this type, to prevent infinite recursion: we do not
1772 re-process any type that appears in this list. The list is emptied
1773 wholesale at the end of processing everything in this recursive stack. */
1775 if (ctf_dynhash_insert (proc_tracking_fp
->ctf_add_processing
,
1776 (void *) (uintptr_t) src_type
, (void *) 1) < 0)
1777 return ctf_set_typed_errno (dst_fp
, ENOMEM
);
1782 /* If we found a match we will have either returned it or declared a
1784 dst_type
= ctf_add_integer (dst_fp
, flag
, name
, &src_en
);
1788 /* If we found a match we will have either returned it or declared a
1790 dst_type
= ctf_add_float (dst_fp
, flag
, name
, &src_en
);
1794 /* We have checked for conflicting encodings: now try to add the
1796 src_type
= ctf_type_reference (src_fp
, src_type
);
1797 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1800 if (src_type
== CTF_ERR
)
1801 return CTF_ERR
; /* errno is set for us. */
1803 dst_type
= ctf_add_slice (dst_fp
, flag
, src_type
, &src_en
);
1807 case CTF_K_VOLATILE
:
1809 case CTF_K_RESTRICT
:
1810 src_type
= ctf_type_reference (src_fp
, src_type
);
1811 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1814 if (src_type
== CTF_ERR
)
1815 return CTF_ERR
; /* errno is set for us. */
1817 dst_type
= ctf_add_reftype (dst_fp
, flag
, src_type
, kind
);
1821 if (ctf_array_info (src_fp
, src_type
, &src_ar
) != 0)
1822 return (ctf_set_typed_errno (dst_fp
, ctf_errno (src_fp
)));
1824 src_ar
.ctr_contents
=
1825 ctf_add_type_internal (dst_fp
, src_fp
, src_ar
.ctr_contents
,
1827 src_ar
.ctr_index
= ctf_add_type_internal (dst_fp
, src_fp
,
1830 src_ar
.ctr_nelems
= src_ar
.ctr_nelems
;
1832 if (src_ar
.ctr_contents
== CTF_ERR
|| src_ar
.ctr_index
== CTF_ERR
)
1833 return CTF_ERR
; /* errno is set for us. */
1835 if (dst_type
!= CTF_ERR
)
1837 if (ctf_array_info (dst_fp
, dst_type
, &dst_ar
) != 0)
1838 return CTF_ERR
; /* errno is set for us. */
1840 if (memcmp (&src_ar
, &dst_ar
, sizeof (ctf_arinfo_t
)))
1842 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1843 _("conflict for type %s against ID %lx: array info "
1844 "differs, old %lx/%lx/%x; new: %lx/%lx/%x"),
1845 name
, dst_type
, src_ar
.ctr_contents
,
1846 src_ar
.ctr_index
, src_ar
.ctr_nelems
,
1847 dst_ar
.ctr_contents
, dst_ar
.ctr_index
,
1849 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1853 dst_type
= ctf_add_array (dst_fp
, flag
, &src_ar
);
1856 case CTF_K_FUNCTION
:
1857 ctc
.ctc_return
= ctf_add_type_internal (dst_fp
, src_fp
,
1863 if (ctc
.ctc_return
== CTF_ERR
)
1864 return CTF_ERR
; /* errno is set for us. */
1866 dst_type
= ctf_add_function (dst_fp
, flag
, &ctc
, NULL
);
1872 ctf_next_t
*i
= NULL
;
1874 const char *membname
;
1875 ctf_id_t src_membtype
;
1877 /* Technically to match a struct or union we need to check both
1878 ways (src members vs. dst, dst members vs. src) but we make
1879 this more optimal by only checking src vs. dst and comparing
1880 the total size of the structure (which we must do anyway)
1881 which covers the possibility of dst members not in src.
1882 This optimization can be defeated for unions, but is so
1883 pathological as to render it irrelevant for our purposes. */
1885 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
1886 && dst_kind
!= CTF_K_FORWARD
)
1888 if (ctf_type_size (src_fp
, src_type
) !=
1889 ctf_type_size (dst_fp
, dst_type
))
1891 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1892 _("conflict for type %s against ID %lx: union "
1893 "size differs, old %li, new %li"), name
,
1894 dst_type
, (long) ctf_type_size (src_fp
, src_type
),
1895 (long) ctf_type_size (dst_fp
, dst_type
));
1896 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1899 if (ctf_member_iter (src_fp
, src_type
, membcmp
, &dst
))
1901 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1902 _("conflict for type %s against ID %lx: members "
1903 "differ, see above"), name
, dst_type
);
1904 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1910 dst_type
= ctf_add_struct_sized (dst_fp
, flag
, name
,
1911 ctf_type_size (src_fp
, src_type
));
1912 if (dst_type
== CTF_ERR
)
1913 return CTF_ERR
; /* errno is set for us. */
1915 /* Pre-emptively add this struct to the type mapping so that
1916 structures that refer to themselves work. */
1917 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1919 while ((offset
= ctf_member_next (src_fp
, src_type
, &i
, &membname
,
1920 &src_membtype
, 0)) >= 0)
1922 ctf_dict_t
*dst
= dst_fp
;
1923 ctf_id_t dst_membtype
= ctf_type_mapping (src_fp
, src_membtype
, &dst
);
1925 if (dst_membtype
== 0)
1927 dst_membtype
= ctf_add_type_internal (dst_fp
, src_fp
,
1930 if (dst_membtype
== CTF_ERR
)
1932 if (ctf_errno (dst_fp
) != ECTF_NONREPRESENTABLE
)
1934 ctf_next_destroy (i
);
1940 if (ctf_add_member_offset (dst_fp
, dst_type
, membname
,
1941 dst_membtype
, offset
) < 0)
1943 ctf_next_destroy (i
);
1947 if (ctf_errno (src_fp
) != ECTF_NEXT_END
)
1948 return CTF_ERR
; /* errno is set for us. */
1953 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
1954 && dst_kind
!= CTF_K_FORWARD
)
1956 if (ctf_enum_iter (src_fp
, src_type
, enumcmp
, &dst
)
1957 || ctf_enum_iter (dst_fp
, dst_type
, enumcmp
, &src
))
1959 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1960 _("conflict for enum %s against ID %lx: members "
1961 "differ, see above"), name
, dst_type
);
1962 return (ctf_set_typed_errno (dst_fp
, ECTF_CONFLICT
));
1967 dst_type
= ctf_add_enum (dst_fp
, flag
, name
);
1968 if ((dst
.ctb_type
= dst_type
) == CTF_ERR
1969 || ctf_enum_iter (src_fp
, src_type
, enumadd
, &dst
))
1970 return CTF_ERR
; /* errno is set for us */
1975 if (dst_type
== CTF_ERR
)
1976 dst_type
= ctf_add_forward (dst_fp
, flag
, name
, forward_kind
);
1980 src_type
= ctf_type_reference (src_fp
, src_type
);
1981 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1984 if (src_type
== CTF_ERR
)
1985 return CTF_ERR
; /* errno is set for us. */
1987 /* If dst_type is not CTF_ERR at this point, we should check if
1988 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
1989 ECTF_CONFLICT. However, this causes problems with bitness typedefs
1990 that vary based on things like if 32-bit then pid_t is int otherwise
1991 long. We therefore omit this check and assume that if the identically
1992 named typedef already exists in dst_fp, it is correct or
1995 if (dst_type
== CTF_ERR
)
1996 dst_type
= ctf_add_typedef (dst_fp
, flag
, name
, src_type
);
2001 return (ctf_set_typed_errno (dst_fp
, ECTF_CORRUPT
));
2004 if (dst_type
!= CTF_ERR
)
2005 ctf_add_type_mapping (src_fp
, orig_src_type
, dst_fp
, dst_type
);
2010 ctf_add_type (ctf_dict_t
*dst_fp
, ctf_dict_t
*src_fp
, ctf_id_t src_type
)
2014 if (!src_fp
->ctf_add_processing
)
2015 src_fp
->ctf_add_processing
= ctf_dynhash_create (ctf_hash_integer
,
2016 ctf_hash_eq_integer
,
2019 /* We store the hash on the source, because it contains only source type IDs:
2020 but callers will invariably expect errors to appear on the dest. */
2021 if (!src_fp
->ctf_add_processing
)
2022 return (ctf_set_typed_errno (dst_fp
, ENOMEM
));
2024 id
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
, src_fp
);
2025 ctf_dynhash_empty (src_fp
->ctf_add_processing
);