2 Copyright (C) 2019 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>
27 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
30 /* Make sure the ptrtab has enough space for at least one more type.
32 We start with 4KiB of ptrtab, enough for a thousand types, then grow it 25%
36 ctf_grow_ptrtab (ctf_file_t
*fp
)
38 size_t new_ptrtab_len
= fp
->ctf_ptrtab_len
;
40 /* We allocate one more ptrtab entry than we need, for the initial zero,
41 plus one because the caller will probably allocate a new type. */
43 if (fp
->ctf_ptrtab
== NULL
)
44 new_ptrtab_len
= 1024;
45 else if ((fp
->ctf_typemax
+ 2) > fp
->ctf_ptrtab_len
)
46 new_ptrtab_len
= fp
->ctf_ptrtab_len
* 1.25;
48 if (new_ptrtab_len
!= fp
->ctf_ptrtab_len
)
52 if ((new_ptrtab
= realloc (fp
->ctf_ptrtab
,
53 new_ptrtab_len
* sizeof (uint32_t))) == NULL
)
54 return (ctf_set_errno (fp
, ENOMEM
));
56 fp
->ctf_ptrtab
= new_ptrtab
;
57 memset (fp
->ctf_ptrtab
+ fp
->ctf_ptrtab_len
, 0,
58 (new_ptrtab_len
- fp
->ctf_ptrtab_len
) * sizeof (uint32_t));
59 fp
->ctf_ptrtab_len
= new_ptrtab_len
;
64 /* To create an empty CTF container, we just declare a zeroed header and call
65 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w
66 and initialize the dynamic members. We start assigning type IDs at 1 because
67 type ID 0 is used as a sentinel and a not-found indicator. */
70 ctf_create (int *errp
)
72 static const ctf_header_t hdr
= { .cth_preamble
= { CTF_MAGIC
, CTF_VERSION
, 0 } };
74 ctf_dynhash_t
*dthash
;
75 ctf_dynhash_t
*dvhash
;
76 ctf_dynhash_t
*structs
= NULL
, *unions
= NULL
, *enums
= NULL
, *names
= NULL
;
81 dthash
= ctf_dynhash_create (ctf_hash_integer
, ctf_hash_eq_integer
,
85 ctf_set_open_errno (errp
, EAGAIN
);
89 dvhash
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
93 ctf_set_open_errno (errp
, EAGAIN
);
97 structs
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
99 unions
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
101 enums
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
103 names
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
105 if (!structs
|| !unions
|| !enums
|| !names
)
107 ctf_set_open_errno (errp
, EAGAIN
);
111 cts
.cts_name
= _CTF_SECTION
;
113 cts
.cts_size
= sizeof (hdr
);
116 if ((fp
= ctf_bufopen_internal (&cts
, NULL
, NULL
, NULL
, 1, errp
)) == NULL
)
119 fp
->ctf_structs
.ctn_writable
= structs
;
120 fp
->ctf_unions
.ctn_writable
= unions
;
121 fp
->ctf_enums
.ctn_writable
= enums
;
122 fp
->ctf_names
.ctn_writable
= names
;
123 fp
->ctf_dthash
= dthash
;
124 fp
->ctf_dvhash
= dvhash
;
126 fp
->ctf_snapshots
= 1;
127 fp
->ctf_snapshot_lu
= 0;
129 ctf_set_ctl_hashes (fp
);
130 ctf_setmodel (fp
, CTF_MODEL_NATIVE
);
131 if (ctf_grow_ptrtab (fp
) < 0)
133 ctf_set_open_errno (errp
, ctf_errno (fp
));
141 ctf_dynhash_destroy (structs
);
142 ctf_dynhash_destroy (unions
);
143 ctf_dynhash_destroy (enums
);
144 ctf_dynhash_destroy (names
);
145 ctf_dynhash_destroy (dvhash
);
147 ctf_dynhash_destroy (dthash
);
152 static unsigned char *
153 ctf_copy_smembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
155 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
158 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
160 ctf_member_t
*copied
;
163 ctm
.ctm_type
= (uint32_t) dmd
->dmd_type
;
164 ctm
.ctm_offset
= (uint32_t) dmd
->dmd_offset
;
166 memcpy (t
, &ctm
, sizeof (ctm
));
167 copied
= (ctf_member_t
*) t
;
169 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctm_name
);
177 static unsigned char *
178 ctf_copy_lmembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
180 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
183 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
185 ctf_lmember_t
*copied
;
188 ctlm
.ctlm_type
= (uint32_t) dmd
->dmd_type
;
189 ctlm
.ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI (dmd
->dmd_offset
);
190 ctlm
.ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO (dmd
->dmd_offset
);
192 memcpy (t
, &ctlm
, sizeof (ctlm
));
193 copied
= (ctf_lmember_t
*) t
;
195 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctlm_name
);
203 static unsigned char *
204 ctf_copy_emembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
206 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
209 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
213 cte
.cte_value
= dmd
->dmd_value
;
214 memcpy (t
, &cte
, sizeof (cte
));
215 copied
= (ctf_enum_t
*) t
;
216 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->cte_name
);
223 /* Sort a newly-constructed static variable array. */
225 typedef struct ctf_sort_var_arg_cb
229 } ctf_sort_var_arg_cb_t
;
232 ctf_sort_var (const void *one_
, const void *two_
, void *arg_
)
234 const ctf_varent_t
*one
= one_
;
235 const ctf_varent_t
*two
= two_
;
236 ctf_sort_var_arg_cb_t
*arg
= arg_
;
238 return (strcmp (ctf_strraw_explicit (arg
->fp
, one
->ctv_name
, arg
->strtab
),
239 ctf_strraw_explicit (arg
->fp
, two
->ctv_name
, arg
->strtab
)));
242 /* Compatibility: just update the threshold for ctf_discard. */
244 ctf_update (ctf_file_t
*fp
)
246 if (!(fp
->ctf_flags
& LCTF_RDWR
))
247 return (ctf_set_errno (fp
, ECTF_RDONLY
));
249 fp
->ctf_dtoldid
= fp
->ctf_typemax
;
253 /* If the specified CTF container is writable and has been modified, reload this
254 container with the updated type definitions, ready for serialization. In
255 order to make this code and the rest of libctf as simple as possible, we
256 perform updates by taking the dynamic type definitions and creating an
257 in-memory CTF file containing the definitions, and then call
258 ctf_simple_open_internal() on it. We perform one extra trick here for the
259 benefit of callers and to keep our code simple: ctf_simple_open_internal()
260 will return a new ctf_file_t, but we want to keep the fp constant for the
261 caller, so after ctf_simple_open_internal() returns, we use memcpy to swap
262 the interior of the old and new ctf_file_t's, and then free the old. */
264 ctf_serialize (ctf_file_t
*fp
)
266 ctf_file_t ofp
, *nfp
;
267 ctf_header_t hdr
, *hdrp
;
270 ctf_varent_t
*dvarents
;
271 ctf_strs_writable_t strtab
;
275 size_t buf_size
, type_size
, nvars
;
276 unsigned char *buf
, *newbuf
;
279 if (!(fp
->ctf_flags
& LCTF_RDWR
))
280 return (ctf_set_errno (fp
, ECTF_RDONLY
));
282 /* Update required? */
283 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
286 /* Fill in an initial CTF header. We will leave the label, object,
287 and function sections empty and only output a header, type section,
288 and string table. The type section begins at a 4-byte aligned
289 boundary past the CTF header itself (at relative offset zero). */
291 memset (&hdr
, 0, sizeof (hdr
));
292 hdr
.cth_magic
= CTF_MAGIC
;
293 hdr
.cth_version
= CTF_VERSION
;
295 /* Iterate through the dynamic type definition list and compute the
296 size of the CTF type section we will need to generate. */
298 for (type_size
= 0, dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
299 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
301 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
302 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
304 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
305 type_size
+= sizeof (ctf_stype_t
);
307 type_size
+= sizeof (ctf_type_t
);
313 type_size
+= sizeof (uint32_t);
316 type_size
+= sizeof (ctf_array_t
);
319 type_size
+= sizeof (ctf_slice_t
);
322 type_size
+= sizeof (uint32_t) * (vlen
+ (vlen
& 1));
326 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
327 type_size
+= sizeof (ctf_member_t
) * vlen
;
329 type_size
+= sizeof (ctf_lmember_t
) * vlen
;
332 type_size
+= sizeof (ctf_enum_t
) * vlen
;
337 /* Computing the number of entries in the CTF variable section is much
340 for (nvars
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
);
341 dvd
!= NULL
; dvd
= ctf_list_next (dvd
), nvars
++);
343 /* Compute the size of the CTF buffer we need, sans only the string table,
344 then allocate a new buffer and memcpy the finished header to the start of
345 the buffer. (We will adjust this later with strtab length info.) */
347 hdr
.cth_typeoff
= hdr
.cth_varoff
+ (nvars
* sizeof (ctf_varent_t
));
348 hdr
.cth_stroff
= hdr
.cth_typeoff
+ type_size
;
351 buf_size
= sizeof (ctf_header_t
) + hdr
.cth_stroff
+ hdr
.cth_strlen
;
353 if ((buf
= malloc (buf_size
)) == NULL
)
354 return (ctf_set_errno (fp
, EAGAIN
));
356 memcpy (buf
, &hdr
, sizeof (ctf_header_t
));
357 t
= (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_varoff
;
359 hdrp
= (ctf_header_t
*) buf
;
360 if ((fp
->ctf_flags
& LCTF_CHILD
) && (fp
->ctf_parname
!= NULL
))
361 ctf_str_add_ref (fp
, fp
->ctf_parname
, &hdrp
->cth_parname
);
362 if (fp
->ctf_cuname
!= NULL
)
363 ctf_str_add_ref (fp
, fp
->ctf_cuname
, &hdrp
->cth_cuname
);
365 /* Work over the variable list, translating everything into ctf_varent_t's and
366 prepping the string table. */
368 dvarents
= (ctf_varent_t
*) t
;
369 for (i
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
;
370 dvd
= ctf_list_next (dvd
), i
++)
372 ctf_varent_t
*var
= &dvarents
[i
];
374 ctf_str_add_ref (fp
, dvd
->dvd_name
, &var
->ctv_name
);
375 var
->ctv_type
= dvd
->dvd_type
;
379 t
+= sizeof (ctf_varent_t
) * nvars
;
381 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_typeoff
);
383 /* We now take a final lap through the dynamic type definition list and copy
384 the appropriate type records to the output buffer, noting down the
387 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
388 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
390 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
391 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
399 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
400 len
= sizeof (ctf_stype_t
);
402 len
= sizeof (ctf_type_t
);
404 memcpy (t
, &dtd
->dtd_data
, len
);
405 copied
= (ctf_stype_t
*) t
; /* name is at the start: constant offset. */
407 && (name
= ctf_strraw (fp
, copied
->ctt_name
)) != NULL
)
408 ctf_str_add_ref (fp
, name
, &copied
->ctt_name
);
415 if (kind
== CTF_K_INTEGER
)
417 encoding
= CTF_INT_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
418 dtd
->dtd_u
.dtu_enc
.cte_offset
,
419 dtd
->dtd_u
.dtu_enc
.cte_bits
);
423 encoding
= CTF_FP_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
424 dtd
->dtd_u
.dtu_enc
.cte_offset
,
425 dtd
->dtd_u
.dtu_enc
.cte_bits
);
427 memcpy (t
, &encoding
, sizeof (encoding
));
428 t
+= sizeof (encoding
);
432 memcpy (t
, &dtd
->dtd_u
.dtu_slice
, sizeof (struct ctf_slice
));
433 t
+= sizeof (struct ctf_slice
);
437 cta
.cta_contents
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_contents
;
438 cta
.cta_index
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_index
;
439 cta
.cta_nelems
= dtd
->dtd_u
.dtu_arr
.ctr_nelems
;
440 memcpy (t
, &cta
, sizeof (cta
));
446 uint32_t *argv
= (uint32_t *) (uintptr_t) t
;
449 for (argc
= 0; argc
< vlen
; argc
++)
450 *argv
++ = (uint32_t) dtd
->dtd_u
.dtu_argv
[argc
];
453 *argv
++ = 0; /* Pad to 4-byte boundary. */
455 t
= (unsigned char *) argv
;
461 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
462 t
= ctf_copy_smembers (fp
, dtd
, t
);
464 t
= ctf_copy_lmembers (fp
, dtd
, t
);
468 t
= ctf_copy_emembers (fp
, dtd
, t
);
472 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_stroff
);
474 /* Construct the final string table and fill out all the string refs with the
475 final offsets. Then purge the refs list, because we're about to move this
476 strtab onto the end of the buf, invalidating all the offsets. */
477 strtab
= ctf_str_write_strtab (fp
);
478 ctf_str_purge_refs (fp
);
480 if (strtab
.cts_strs
== NULL
)
483 return (ctf_set_errno (fp
, EAGAIN
));
486 /* Now the string table is constructed, we can sort the buffer of
488 ctf_sort_var_arg_cb_t sort_var_arg
= { fp
, (ctf_strs_t
*) &strtab
};
489 ctf_qsort_r (dvarents
, nvars
, sizeof (ctf_varent_t
), ctf_sort_var
,
492 if ((newbuf
= ctf_realloc (fp
, buf
, buf_size
+ strtab
.cts_len
)) == NULL
)
495 ctf_free (strtab
.cts_strs
);
496 return (ctf_set_errno (fp
, EAGAIN
));
499 memcpy (buf
+ buf_size
, strtab
.cts_strs
, strtab
.cts_len
);
500 hdrp
= (ctf_header_t
*) buf
;
501 hdrp
->cth_strlen
= strtab
.cts_len
;
502 buf_size
+= hdrp
->cth_strlen
;
503 ctf_free (strtab
.cts_strs
);
505 /* Finally, we are ready to ctf_simple_open() the new container. If this
506 is successful, we then switch nfp and fp and free the old container. */
508 if ((nfp
= ctf_simple_open_internal ((char *) buf
, buf_size
, NULL
, 0,
509 0, NULL
, 0, fp
->ctf_syn_ext_strtab
,
513 return (ctf_set_errno (fp
, err
));
516 (void) ctf_setmodel (nfp
, ctf_getmodel (fp
));
517 (void) ctf_import (nfp
, fp
->ctf_parent
);
519 nfp
->ctf_refcnt
= fp
->ctf_refcnt
;
520 nfp
->ctf_flags
|= fp
->ctf_flags
& ~LCTF_DIRTY
;
521 if (nfp
->ctf_dynbase
== NULL
)
522 nfp
->ctf_dynbase
= buf
; /* Make sure buf is freed on close. */
523 nfp
->ctf_dthash
= fp
->ctf_dthash
;
524 nfp
->ctf_dtdefs
= fp
->ctf_dtdefs
;
525 nfp
->ctf_dvhash
= fp
->ctf_dvhash
;
526 nfp
->ctf_dvdefs
= fp
->ctf_dvdefs
;
527 nfp
->ctf_dtoldid
= fp
->ctf_dtoldid
;
528 nfp
->ctf_add_processing
= fp
->ctf_add_processing
;
529 nfp
->ctf_snapshots
= fp
->ctf_snapshots
+ 1;
530 nfp
->ctf_specific
= fp
->ctf_specific
;
531 nfp
->ctf_ptrtab
= fp
->ctf_ptrtab
;
532 nfp
->ctf_ptrtab_len
= fp
->ctf_ptrtab_len
;
533 nfp
->ctf_link_inputs
= fp
->ctf_link_inputs
;
534 nfp
->ctf_link_outputs
= fp
->ctf_link_outputs
;
535 nfp
->ctf_str_prov_offset
= fp
->ctf_str_prov_offset
;
536 nfp
->ctf_syn_ext_strtab
= fp
->ctf_syn_ext_strtab
;
537 nfp
->ctf_link_cu_mapping
= fp
->ctf_link_cu_mapping
;
538 nfp
->ctf_link_type_mapping
= fp
->ctf_link_type_mapping
;
539 nfp
->ctf_link_memb_name_changer
= fp
->ctf_link_memb_name_changer
;
540 nfp
->ctf_link_memb_name_changer_arg
= fp
->ctf_link_memb_name_changer_arg
;
542 nfp
->ctf_snapshot_lu
= fp
->ctf_snapshots
;
544 memcpy (&nfp
->ctf_lookups
, fp
->ctf_lookups
, sizeof (fp
->ctf_lookups
));
545 nfp
->ctf_structs
= fp
->ctf_structs
;
546 nfp
->ctf_unions
= fp
->ctf_unions
;
547 nfp
->ctf_enums
= fp
->ctf_enums
;
548 nfp
->ctf_names
= fp
->ctf_names
;
550 fp
->ctf_dthash
= NULL
;
551 ctf_str_free_atoms (nfp
);
552 nfp
->ctf_str_atoms
= fp
->ctf_str_atoms
;
553 nfp
->ctf_prov_strtab
= fp
->ctf_prov_strtab
;
554 fp
->ctf_str_atoms
= NULL
;
555 fp
->ctf_prov_strtab
= NULL
;
556 memset (&fp
->ctf_dtdefs
, 0, sizeof (ctf_list_t
));
557 fp
->ctf_add_processing
= NULL
;
558 fp
->ctf_ptrtab
= NULL
;
559 fp
->ctf_link_inputs
= NULL
;
560 fp
->ctf_link_outputs
= NULL
;
561 fp
->ctf_syn_ext_strtab
= NULL
;
562 fp
->ctf_link_cu_mapping
= NULL
;
563 fp
->ctf_link_type_mapping
= NULL
;
565 fp
->ctf_dvhash
= NULL
;
566 memset (&fp
->ctf_dvdefs
, 0, sizeof (ctf_list_t
));
567 memset (fp
->ctf_lookups
, 0, sizeof (fp
->ctf_lookups
));
568 fp
->ctf_structs
.ctn_writable
= NULL
;
569 fp
->ctf_unions
.ctn_writable
= NULL
;
570 fp
->ctf_enums
.ctn_writable
= NULL
;
571 fp
->ctf_names
.ctn_writable
= NULL
;
573 memcpy (&ofp
, fp
, sizeof (ctf_file_t
));
574 memcpy (fp
, nfp
, sizeof (ctf_file_t
));
575 memcpy (nfp
, &ofp
, sizeof (ctf_file_t
));
577 nfp
->ctf_refcnt
= 1; /* Force nfp to be freed. */
578 ctf_file_close (nfp
);
584 ctf_name_table (ctf_file_t
*fp
, int kind
)
589 return &fp
->ctf_structs
;
591 return &fp
->ctf_unions
;
593 return &fp
->ctf_enums
;
595 return &fp
->ctf_names
;
600 ctf_dtd_insert (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, int kind
)
603 if (ctf_dynhash_insert (fp
->ctf_dthash
, (void *) dtd
->dtd_type
, dtd
) < 0)
606 if (dtd
->dtd_data
.ctt_name
607 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
)
609 if (ctf_dynhash_insert (ctf_name_table (fp
, kind
)->ctn_writable
,
610 (char *) name
, (void *) dtd
->dtd_type
) < 0)
612 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) dtd
->dtd_type
);
616 ctf_list_append (&fp
->ctf_dtdefs
, dtd
);
621 ctf_dtd_delete (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
)
623 ctf_dmdef_t
*dmd
, *nmd
;
624 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
627 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) dtd
->dtd_type
);
634 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
635 dmd
!= NULL
; dmd
= nmd
)
637 if (dmd
->dmd_name
!= NULL
)
638 ctf_free (dmd
->dmd_name
);
639 nmd
= ctf_list_next (dmd
);
644 ctf_free (dtd
->dtd_u
.dtu_argv
);
648 if (dtd
->dtd_data
.ctt_name
649 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
)
651 ctf_dynhash_remove (ctf_name_table (fp
, kind
)->ctn_writable
,
653 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
656 ctf_list_delete (&fp
->ctf_dtdefs
, dtd
);
661 ctf_dtd_lookup (const ctf_file_t
*fp
, ctf_id_t type
)
663 return (ctf_dtdef_t
*) ctf_dynhash_lookup (fp
->ctf_dthash
, (void *) type
);
667 ctf_dynamic_type (const ctf_file_t
*fp
, ctf_id_t id
)
671 if (!(fp
->ctf_flags
& LCTF_RDWR
))
674 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, id
))
677 idx
= LCTF_TYPE_TO_INDEX(fp
, id
);
679 if ((unsigned long) idx
<= fp
->ctf_typemax
)
680 return ctf_dtd_lookup (fp
, id
);
685 ctf_dvd_insert (ctf_file_t
*fp
, ctf_dvdef_t
*dvd
)
687 if (ctf_dynhash_insert (fp
->ctf_dvhash
, dvd
->dvd_name
, dvd
) < 0)
689 ctf_list_append (&fp
->ctf_dvdefs
, dvd
);
694 ctf_dvd_delete (ctf_file_t
*fp
, ctf_dvdef_t
*dvd
)
696 ctf_dynhash_remove (fp
->ctf_dvhash
, dvd
->dvd_name
);
697 ctf_free (dvd
->dvd_name
);
699 ctf_list_delete (&fp
->ctf_dvdefs
, dvd
);
704 ctf_dvd_lookup (const ctf_file_t
*fp
, const char *name
)
706 return (ctf_dvdef_t
*) ctf_dynhash_lookup (fp
->ctf_dvhash
, name
);
709 /* Discard all of the dynamic type definitions and variable definitions that
710 have been added to the container since the last call to ctf_update(). We
711 locate such types by scanning the dtd list and deleting elements that have
712 type IDs greater than ctf_dtoldid, which is set by ctf_update(), above, and
713 by scanning the variable list and deleting elements that have update IDs
714 equal to the current value of the last-update snapshot count (indicating that
715 they were added after the most recent call to ctf_update()). */
717 ctf_discard (ctf_file_t
*fp
)
719 ctf_snapshot_id_t last_update
=
721 fp
->ctf_snapshot_lu
+ 1 };
723 /* Update required? */
724 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
727 return (ctf_rollback (fp
, last_update
));
731 ctf_snapshot (ctf_file_t
*fp
)
733 ctf_snapshot_id_t snapid
;
734 snapid
.dtd_id
= fp
->ctf_typemax
;
735 snapid
.snapshot_id
= fp
->ctf_snapshots
++;
739 /* Like ctf_discard(), only discards everything after a particular ID. */
741 ctf_rollback (ctf_file_t
*fp
, ctf_snapshot_id_t id
)
743 ctf_dtdef_t
*dtd
, *ntd
;
744 ctf_dvdef_t
*dvd
, *nvd
;
746 if (!(fp
->ctf_flags
& LCTF_RDWR
))
747 return (ctf_set_errno (fp
, ECTF_RDONLY
));
749 if (fp
->ctf_snapshot_lu
>= id
.snapshot_id
)
750 return (ctf_set_errno (fp
, ECTF_OVERROLLBACK
));
752 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
); dtd
!= NULL
; dtd
= ntd
)
757 ntd
= ctf_list_next (dtd
);
759 if (LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_type
) <= id
.dtd_id
)
762 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
764 if (dtd
->dtd_data
.ctt_name
765 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
)
767 ctf_dynhash_remove (ctf_name_table (fp
, kind
)->ctn_writable
,
769 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
772 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) dtd
->dtd_type
);
773 ctf_dtd_delete (fp
, dtd
);
776 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
; dvd
= nvd
)
778 nvd
= ctf_list_next (dvd
);
780 if (dvd
->dvd_snapshots
<= id
.snapshot_id
)
783 ctf_dvd_delete (fp
, dvd
);
786 fp
->ctf_typemax
= id
.dtd_id
;
787 fp
->ctf_snapshots
= id
.snapshot_id
;
789 if (fp
->ctf_snapshots
== fp
->ctf_snapshot_lu
)
790 fp
->ctf_flags
&= ~LCTF_DIRTY
;
796 ctf_add_generic (ctf_file_t
*fp
, uint32_t flag
, const char *name
, int kind
,
802 if (flag
!= CTF_ADD_NONROOT
&& flag
!= CTF_ADD_ROOT
)
803 return (ctf_set_errno (fp
, EINVAL
));
805 if (!(fp
->ctf_flags
& LCTF_RDWR
))
806 return (ctf_set_errno (fp
, ECTF_RDONLY
));
808 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) >= CTF_MAX_TYPE
)
809 return (ctf_set_errno (fp
, ECTF_FULL
));
811 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) == (CTF_MAX_PTYPE
- 1))
812 return (ctf_set_errno (fp
, ECTF_FULL
));
814 /* Make sure ptrtab always grows to be big enough for all types. */
815 if (ctf_grow_ptrtab (fp
) < 0)
816 return CTF_ERR
; /* errno is set for us. */
818 if ((dtd
= ctf_alloc (sizeof (ctf_dtdef_t
))) == NULL
)
819 return (ctf_set_errno (fp
, EAGAIN
));
821 type
= ++fp
->ctf_typemax
;
822 type
= LCTF_INDEX_TO_TYPE (fp
, type
, (fp
->ctf_flags
& LCTF_CHILD
));
824 memset (dtd
, 0, sizeof (ctf_dtdef_t
));
825 dtd
->dtd_data
.ctt_name
= ctf_str_add_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
826 dtd
->dtd_type
= type
;
828 if (dtd
->dtd_data
.ctt_name
== 0 && name
!= NULL
&& name
[0] != '\0')
831 return (ctf_set_errno (fp
, EAGAIN
));
834 if (ctf_dtd_insert (fp
, dtd
, kind
) < 0)
837 return CTF_ERR
; /* errno is set for us. */
839 fp
->ctf_flags
|= LCTF_DIRTY
;
845 /* When encoding integer sizes, we want to convert a byte count in the range
846 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
847 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
863 ctf_add_encoded (ctf_file_t
*fp
, uint32_t flag
,
864 const char *name
, const ctf_encoding_t
*ep
, uint32_t kind
)
870 return (ctf_set_errno (fp
, EINVAL
));
872 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, &dtd
)) == CTF_ERR
)
873 return CTF_ERR
; /* errno is set for us. */
875 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
876 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
878 dtd
->dtd_u
.dtu_enc
= *ep
;
884 ctf_add_reftype (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
, uint32_t kind
)
888 ctf_file_t
*tmp
= fp
;
889 int child
= fp
->ctf_flags
& LCTF_CHILD
;
891 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
892 return (ctf_set_errno (fp
, EINVAL
));
894 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
895 return CTF_ERR
; /* errno is set for us. */
897 if ((type
= ctf_add_generic (fp
, flag
, NULL
, kind
, &dtd
)) == CTF_ERR
)
898 return CTF_ERR
; /* errno is set for us. */
900 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
901 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
903 if (kind
!= CTF_K_POINTER
)
906 /* If we are adding a pointer, update the ptrtab, both the directly pointed-to
907 type and (if an anonymous typedef node is being pointed at) the type that
908 points at too. Note that ctf_typemax is at this point one higher than we
909 want to check against, because it's just been incremented for the addition
912 uint32_t type_idx
= LCTF_TYPE_TO_INDEX (fp
, type
);
913 uint32_t ref_idx
= LCTF_TYPE_TO_INDEX (fp
, ref
);
915 if (LCTF_TYPE_ISCHILD (fp
, ref
) == child
916 && ref_idx
< fp
->ctf_typemax
)
918 fp
->ctf_ptrtab
[ref_idx
] = type_idx
;
920 ctf_id_t refref_idx
= LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_data
.ctt_type
);
923 && (LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) == CTF_K_TYPEDEF
)
924 && strcmp (ctf_strptr (fp
, dtd
->dtd_data
.ctt_name
), "") == 0
925 && refref_idx
< fp
->ctf_typemax
)
926 fp
->ctf_ptrtab
[refref_idx
] = type_idx
;
933 ctf_add_slice (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
,
934 const ctf_encoding_t
*ep
)
939 const ctf_type_t
*tp
;
940 ctf_file_t
*tmp
= fp
;
943 return (ctf_set_errno (fp
, EINVAL
));
945 if ((ep
->cte_bits
> 255) || (ep
->cte_offset
> 255))
946 return (ctf_set_errno (fp
, ECTF_SLICEOVERFLOW
));
948 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
949 return (ctf_set_errno (fp
, EINVAL
));
951 if ((tp
= ctf_lookup_by_id (&tmp
, ref
)) == NULL
)
952 return CTF_ERR
; /* errno is set for us. */
954 kind
= ctf_type_kind_unsliced (tmp
, ref
);
955 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) &&
956 (kind
!= CTF_K_ENUM
))
957 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
959 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_SLICE
, &dtd
)) == CTF_ERR
)
960 return CTF_ERR
; /* errno is set for us. */
962 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_SLICE
, flag
, 0);
963 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
965 dtd
->dtd_u
.dtu_slice
.cts_type
= ref
;
966 dtd
->dtd_u
.dtu_slice
.cts_bits
= ep
->cte_bits
;
967 dtd
->dtd_u
.dtu_slice
.cts_offset
= ep
->cte_offset
;
973 ctf_add_integer (ctf_file_t
*fp
, uint32_t flag
,
974 const char *name
, const ctf_encoding_t
*ep
)
976 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_INTEGER
));
980 ctf_add_float (ctf_file_t
*fp
, uint32_t flag
,
981 const char *name
, const ctf_encoding_t
*ep
)
983 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_FLOAT
));
987 ctf_add_pointer (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
989 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_POINTER
));
993 ctf_add_array (ctf_file_t
*fp
, uint32_t flag
, const ctf_arinfo_t
*arp
)
997 ctf_file_t
*tmp
= fp
;
1000 return (ctf_set_errno (fp
, EINVAL
));
1002 if (ctf_lookup_by_id (&tmp
, arp
->ctr_contents
) == NULL
)
1003 return CTF_ERR
; /* errno is set for us. */
1006 if (ctf_lookup_by_id (&tmp
, arp
->ctr_index
) == NULL
)
1007 return CTF_ERR
; /* errno is set for us. */
1009 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_ARRAY
, &dtd
)) == CTF_ERR
)
1010 return CTF_ERR
; /* errno is set for us. */
1012 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ARRAY
, flag
, 0);
1013 dtd
->dtd_data
.ctt_size
= 0;
1014 dtd
->dtd_u
.dtu_arr
= *arp
;
1020 ctf_set_array (ctf_file_t
*fp
, ctf_id_t type
, const ctf_arinfo_t
*arp
)
1022 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1024 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1025 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1028 || LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) != CTF_K_ARRAY
)
1029 return (ctf_set_errno (fp
, ECTF_BADID
));
1031 fp
->ctf_flags
|= LCTF_DIRTY
;
1032 dtd
->dtd_u
.dtu_arr
= *arp
;
1038 ctf_add_function (ctf_file_t
*fp
, uint32_t flag
,
1039 const ctf_funcinfo_t
*ctc
, const ctf_id_t
*argv
)
1044 ctf_id_t
*vdat
= NULL
;
1045 ctf_file_t
*tmp
= fp
;
1048 if (ctc
== NULL
|| (ctc
->ctc_flags
& ~CTF_FUNC_VARARG
) != 0
1049 || (ctc
->ctc_argc
!= 0 && argv
== NULL
))
1050 return (ctf_set_errno (fp
, EINVAL
));
1052 vlen
= ctc
->ctc_argc
;
1053 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
1054 vlen
++; /* Add trailing zero to indicate varargs (see below). */
1056 if (ctf_lookup_by_id (&tmp
, ctc
->ctc_return
) == NULL
)
1057 return CTF_ERR
; /* errno is set for us. */
1059 for (i
= 0; i
< ctc
->ctc_argc
; i
++)
1062 if (ctf_lookup_by_id (&tmp
, argv
[i
]) == NULL
)
1063 return CTF_ERR
; /* errno is set for us. */
1066 if (vlen
> CTF_MAX_VLEN
)
1067 return (ctf_set_errno (fp
, EOVERFLOW
));
1069 if (vlen
!= 0 && (vdat
= ctf_alloc (sizeof (ctf_id_t
) * vlen
)) == NULL
)
1070 return (ctf_set_errno (fp
, EAGAIN
));
1072 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_FUNCTION
,
1076 return CTF_ERR
; /* errno is set for us. */
1079 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FUNCTION
, flag
, vlen
);
1080 dtd
->dtd_data
.ctt_type
= (uint32_t) ctc
->ctc_return
;
1082 memcpy (vdat
, argv
, sizeof (ctf_id_t
) * ctc
->ctc_argc
);
1083 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
1084 vdat
[vlen
- 1] = 0; /* Add trailing zero to indicate varargs. */
1085 dtd
->dtd_u
.dtu_argv
= vdat
;
1091 ctf_add_struct_sized (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1097 /* Promote forwards to structs. */
1100 type
= ctf_lookup_by_rawname (fp
, CTF_K_STRUCT
, name
);
1102 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1103 dtd
= ctf_dtd_lookup (fp
, type
);
1104 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_STRUCT
,
1106 return CTF_ERR
; /* errno is set for us. */
1108 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_STRUCT
, flag
, 0);
1110 if (size
> CTF_MAX_SIZE
)
1112 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1113 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1114 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1117 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1123 ctf_add_struct (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1125 return (ctf_add_struct_sized (fp
, flag
, name
, 0));
1129 ctf_add_union_sized (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1135 /* Promote forwards to unions. */
1137 type
= ctf_lookup_by_rawname (fp
, CTF_K_UNION
, name
);
1139 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1140 dtd
= ctf_dtd_lookup (fp
, type
);
1141 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_UNION
,
1143 return CTF_ERR
; /* errno is set for us */
1145 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_UNION
, flag
, 0);
1147 if (size
> CTF_MAX_SIZE
)
1149 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1150 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1151 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1154 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1160 ctf_add_union (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1162 return (ctf_add_union_sized (fp
, flag
, name
, 0));
1166 ctf_add_enum (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1171 /* Promote forwards to enums. */
1173 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
1175 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1176 dtd
= ctf_dtd_lookup (fp
, type
);
1177 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_ENUM
,
1179 return CTF_ERR
; /* errno is set for us. */
1181 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ENUM
, flag
, 0);
1182 dtd
->dtd_data
.ctt_size
= fp
->ctf_dmodel
->ctd_int
;
1188 ctf_add_enum_encoded (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1189 const ctf_encoding_t
*ep
)
1193 /* First, create the enum if need be, using most of the same machinery as
1194 ctf_add_enum(), to ensure that we do not allow things past that are not
1195 enums or forwards to them. (This includes other slices: you cannot slice a
1196 slice, which would be a useless thing to do anyway.) */
1199 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
1203 if ((ctf_type_kind (fp
, type
) != CTF_K_FORWARD
) &&
1204 (ctf_type_kind_unsliced (fp
, type
) != CTF_K_ENUM
))
1205 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1207 else if ((type
= ctf_add_enum (fp
, flag
, name
)) == CTF_ERR
)
1208 return CTF_ERR
; /* errno is set for us. */
1210 /* Now attach a suitable slice to it. */
1212 return ctf_add_slice (fp
, flag
, type
, ep
);
1216 ctf_add_forward (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1222 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
&& kind
!= CTF_K_ENUM
)
1223 return (ctf_set_errno (fp
, ECTF_NOTSUE
));
1225 /* If the type is already defined or exists as a forward tag, just
1226 return the ctf_id_t of the existing definition. */
1229 type
= ctf_lookup_by_rawname (fp
, kind
, name
);
1231 if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_FORWARD
,&dtd
)) == CTF_ERR
)
1232 return CTF_ERR
; /* errno is set for us. */
1234 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FORWARD
, flag
, 0);
1235 dtd
->dtd_data
.ctt_type
= kind
;
1241 ctf_add_typedef (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1246 ctf_file_t
*tmp
= fp
;
1248 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
1249 return (ctf_set_errno (fp
, EINVAL
));
1251 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1252 return CTF_ERR
; /* errno is set for us. */
1254 if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_TYPEDEF
,
1256 return CTF_ERR
; /* errno is set for us. */
1258 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_TYPEDEF
, flag
, 0);
1259 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
1265 ctf_add_volatile (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1267 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_VOLATILE
));
1271 ctf_add_const (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1273 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_CONST
));
1277 ctf_add_restrict (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1279 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_RESTRICT
));
1283 ctf_add_enumerator (ctf_file_t
*fp
, ctf_id_t enid
, const char *name
,
1286 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, enid
);
1289 uint32_t kind
, vlen
, root
;
1293 return (ctf_set_errno (fp
, EINVAL
));
1295 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1296 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1299 return (ctf_set_errno (fp
, ECTF_BADID
));
1301 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1302 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1303 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1305 if (kind
!= CTF_K_ENUM
)
1306 return (ctf_set_errno (fp
, ECTF_NOTENUM
));
1308 if (vlen
== CTF_MAX_VLEN
)
1309 return (ctf_set_errno (fp
, ECTF_DTFULL
));
1311 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1312 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1314 if (strcmp (dmd
->dmd_name
, name
) == 0)
1315 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1318 if ((dmd
= ctf_alloc (sizeof (ctf_dmdef_t
))) == NULL
)
1319 return (ctf_set_errno (fp
, EAGAIN
));
1321 if ((s
= ctf_strdup (name
)) == NULL
)
1324 return (ctf_set_errno (fp
, EAGAIN
));
1328 dmd
->dmd_type
= CTF_ERR
;
1329 dmd
->dmd_offset
= 0;
1330 dmd
->dmd_value
= value
;
1332 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1333 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
1335 fp
->ctf_flags
|= LCTF_DIRTY
;
1341 ctf_add_member_offset (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1342 ctf_id_t type
, unsigned long bit_offset
)
1344 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, souid
);
1347 ssize_t msize
, malign
, ssize
;
1348 uint32_t kind
, vlen
, root
;
1351 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1352 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1355 return (ctf_set_errno (fp
, ECTF_BADID
));
1357 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1358 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1359 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1361 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
1362 return (ctf_set_errno (fp
, ECTF_NOTSOU
));
1364 if (vlen
== CTF_MAX_VLEN
)
1365 return (ctf_set_errno (fp
, ECTF_DTFULL
));
1369 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1370 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1372 if (dmd
->dmd_name
!= NULL
&& strcmp (dmd
->dmd_name
, name
) == 0)
1373 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1377 if ((msize
= ctf_type_size (fp
, type
)) < 0 ||
1378 (malign
= ctf_type_align (fp
, type
)) < 0)
1379 return -1; /* errno is set for us. */
1381 if ((dmd
= ctf_alloc (sizeof (ctf_dmdef_t
))) == NULL
)
1382 return (ctf_set_errno (fp
, EAGAIN
));
1384 if (name
!= NULL
&& (s
= ctf_strdup (name
)) == NULL
)
1387 return (ctf_set_errno (fp
, EAGAIN
));
1391 dmd
->dmd_type
= type
;
1392 dmd
->dmd_value
= -1;
1394 if (kind
== CTF_K_STRUCT
&& vlen
!= 0)
1396 if (bit_offset
== (unsigned long) - 1)
1398 /* Natural alignment. */
1400 ctf_dmdef_t
*lmd
= ctf_list_prev (&dtd
->dtd_u
.dtu_members
);
1401 ctf_id_t ltype
= ctf_type_resolve (fp
, lmd
->dmd_type
);
1402 size_t off
= lmd
->dmd_offset
;
1404 ctf_encoding_t linfo
;
1407 if (ctf_type_encoding (fp
, ltype
, &linfo
) == 0)
1408 off
+= linfo
.cte_bits
;
1409 else if ((lsize
= ctf_type_size (fp
, ltype
)) > 0)
1410 off
+= lsize
* CHAR_BIT
;
1412 /* Round up the offset of the end of the last member to
1413 the next byte boundary, convert 'off' to bytes, and
1414 then round it up again to the next multiple of the
1415 alignment required by the new member. Finally,
1416 convert back to bits and store the result in
1417 dmd_offset. Technically we could do more efficient
1418 packing if the new member is a bit-field, but we're
1419 the "compiler" and ANSI says we can do as we choose. */
1421 off
= roundup (off
, CHAR_BIT
) / CHAR_BIT
;
1422 off
= roundup (off
, MAX (malign
, 1));
1423 dmd
->dmd_offset
= off
* CHAR_BIT
;
1424 ssize
= off
+ msize
;
1428 /* Specified offset in bits. */
1430 dmd
->dmd_offset
= bit_offset
;
1431 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1432 ssize
= MAX (ssize
, ((signed) bit_offset
/ CHAR_BIT
) + msize
);
1437 dmd
->dmd_offset
= 0;
1438 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1439 ssize
= MAX (ssize
, msize
);
1442 if ((size_t) ssize
> CTF_MAX_SIZE
)
1444 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1445 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (ssize
);
1446 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (ssize
);
1449 dtd
->dtd_data
.ctt_size
= (uint32_t) ssize
;
1451 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1452 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
1454 fp
->ctf_flags
|= LCTF_DIRTY
;
1459 ctf_add_member_encoded (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1460 ctf_id_t type
, unsigned long bit_offset
,
1461 const ctf_encoding_t encoding
)
1463 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1464 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1467 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) && (kind
!= CTF_K_ENUM
))
1468 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1470 if ((type
= ctf_add_slice (fp
, CTF_ADD_NONROOT
, otype
, &encoding
)) == CTF_ERR
)
1471 return -1; /* errno is set for us. */
1473 return ctf_add_member_offset (fp
, souid
, name
, type
, bit_offset
);
1477 ctf_add_member (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1480 return ctf_add_member_offset (fp
, souid
, name
, type
, (unsigned long) - 1);
1484 ctf_add_variable (ctf_file_t
*fp
, const char *name
, ctf_id_t ref
)
1487 ctf_file_t
*tmp
= fp
;
1489 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1490 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1492 if (ctf_dvd_lookup (fp
, name
) != NULL
)
1493 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1495 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1496 return -1; /* errno is set for us. */
1498 /* Make sure this type is representable. */
1499 if ((ctf_type_resolve (fp
, ref
) == CTF_ERR
)
1500 && (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
))
1503 if ((dvd
= ctf_alloc (sizeof (ctf_dvdef_t
))) == NULL
)
1504 return (ctf_set_errno (fp
, EAGAIN
));
1506 if (name
!= NULL
&& (dvd
->dvd_name
= ctf_strdup (name
)) == NULL
)
1509 return (ctf_set_errno (fp
, EAGAIN
));
1511 dvd
->dvd_type
= ref
;
1512 dvd
->dvd_snapshots
= fp
->ctf_snapshots
;
1514 if (ctf_dvd_insert (fp
, dvd
) < 0)
1517 return -1; /* errno is set for us. */
1520 fp
->ctf_flags
|= LCTF_DIRTY
;
1525 enumcmp (const char *name
, int value
, void *arg
)
1527 ctf_bundle_t
*ctb
= arg
;
1530 if (ctf_enum_value (ctb
->ctb_file
, ctb
->ctb_type
, name
, &bvalue
) < 0)
1532 ctf_dprintf ("Conflict due to member %s iteration error: %s.\n", name
,
1533 ctf_errmsg (ctf_errno (ctb
->ctb_file
)));
1536 if (value
!= bvalue
)
1538 ctf_dprintf ("Conflict due to value change: %i versus %i\n",
1546 enumadd (const char *name
, int value
, void *arg
)
1548 ctf_bundle_t
*ctb
= arg
;
1550 return (ctf_add_enumerator (ctb
->ctb_file
, ctb
->ctb_type
,
1555 membcmp (const char *name
, ctf_id_t type _libctf_unused_
, unsigned long offset
,
1558 ctf_bundle_t
*ctb
= arg
;
1561 if (ctf_member_info (ctb
->ctb_file
, ctb
->ctb_type
, name
, &ctm
) < 0)
1563 ctf_dprintf ("Conflict due to member %s iteration error: %s.\n", name
,
1564 ctf_errmsg (ctf_errno (ctb
->ctb_file
)));
1567 if (ctm
.ctm_offset
!= offset
)
1569 ctf_dprintf ("Conflict due to member %s offset change: "
1570 "%lx versus %lx\n", name
, ctm
.ctm_offset
, offset
);
1577 membadd (const char *name
, ctf_id_t type
, unsigned long offset
, void *arg
)
1579 ctf_bundle_t
*ctb
= arg
;
1583 if ((dmd
= ctf_alloc (sizeof (ctf_dmdef_t
))) == NULL
)
1584 return (ctf_set_errno (ctb
->ctb_file
, EAGAIN
));
1586 if (name
!= NULL
&& (s
= ctf_strdup (name
)) == NULL
)
1589 return (ctf_set_errno (ctb
->ctb_file
, EAGAIN
));
1592 /* For now, dmd_type is copied as the src_fp's type; it is reset to an
1593 equivalent dst_fp type by a final loop in ctf_add_type(), below. */
1595 dmd
->dmd_type
= type
;
1596 dmd
->dmd_offset
= offset
;
1597 dmd
->dmd_value
= -1;
1599 ctf_list_append (&ctb
->ctb_dtd
->dtd_u
.dtu_members
, dmd
);
1601 ctb
->ctb_file
->ctf_flags
|= LCTF_DIRTY
;
1605 /* The ctf_add_type routine is used to copy a type from a source CTF container
1606 to a dynamic destination container. This routine operates recursively by
1607 following the source type's links and embedded member types. If the
1608 destination container already contains a named type which has the same
1609 attributes, then we succeed and return this type but no changes occur. */
1611 ctf_add_type_internal (ctf_file_t
*dst_fp
, ctf_file_t
*src_fp
, ctf_id_t src_type
,
1612 ctf_file_t
*proc_tracking_fp
)
1614 ctf_id_t dst_type
= CTF_ERR
;
1615 uint32_t dst_kind
= CTF_K_UNKNOWN
;
1616 ctf_file_t
*tmp_fp
= dst_fp
;
1620 uint32_t kind
, forward_kind
, flag
, vlen
;
1622 const ctf_type_t
*src_tp
, *dst_tp
;
1623 ctf_bundle_t src
, dst
;
1624 ctf_encoding_t src_en
, dst_en
;
1625 ctf_arinfo_t src_ar
, dst_ar
;
1629 ctf_id_t orig_src_type
= src_type
;
1631 if (!(dst_fp
->ctf_flags
& LCTF_RDWR
))
1632 return (ctf_set_errno (dst_fp
, ECTF_RDONLY
));
1634 if ((src_tp
= ctf_lookup_by_id (&src_fp
, src_type
)) == NULL
)
1635 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1637 if ((ctf_type_resolve (src_fp
, src_type
) == CTF_ERR
)
1638 && (ctf_errno (src_fp
) == ECTF_NONREPRESENTABLE
))
1639 return (ctf_set_errno (dst_fp
, ECTF_NONREPRESENTABLE
));
1641 name
= ctf_strptr (src_fp
, src_tp
->ctt_name
);
1642 kind
= LCTF_INFO_KIND (src_fp
, src_tp
->ctt_info
);
1643 flag
= LCTF_INFO_ISROOT (src_fp
, src_tp
->ctt_info
);
1644 vlen
= LCTF_INFO_VLEN (src_fp
, src_tp
->ctt_info
);
1646 /* If this is a type we are currently in the middle of adding, hand it
1647 straight back. (This lets us handle self-referential structures without
1648 considering forwards and empty structures the same as their completed
1651 tmp
= ctf_type_mapping (src_fp
, src_type
, &tmp_fp
);
1655 if (ctf_dynhash_lookup (proc_tracking_fp
->ctf_add_processing
,
1656 (void *) (uintptr_t) src_type
))
1659 /* If this type has already been added from this container, and is the same
1660 kind and (if a struct or union) has the same number of members, hand it
1663 if ((ctf_type_kind_unsliced (tmp_fp
, tmp
) == (int) kind
)
1664 && (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
1665 || kind
== CTF_K_ENUM
))
1667 if ((dst_tp
= ctf_lookup_by_id (&tmp_fp
, dst_type
)) != NULL
)
1668 if (vlen
== LCTF_INFO_VLEN (tmp_fp
, dst_tp
->ctt_info
))
1673 forward_kind
= kind
;
1674 if (kind
== CTF_K_FORWARD
)
1675 forward_kind
= src_tp
->ctt_type
;
1677 /* If the source type has a name and is a root type (visible at the
1678 top-level scope), lookup the name in the destination container and
1679 verify that it is of the same kind before we do anything else. */
1681 if ((flag
& CTF_ADD_ROOT
) && name
[0] != '\0'
1682 && (tmp
= ctf_lookup_by_rawname (dst_fp
, forward_kind
, name
)) != 0)
1685 dst_kind
= ctf_type_kind_unsliced (dst_fp
, dst_type
);
1688 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
1689 unless dst_type is a forward declaration and src_type is a struct,
1690 union, or enum (i.e. the definition of the previous forward decl).
1692 We also allow addition in the opposite order (addition of a forward when a
1693 struct, union, or enum already exists), which is a NOP and returns the
1694 already-present struct, union, or enum. */
1696 if (dst_type
!= CTF_ERR
&& dst_kind
!= kind
)
1698 if (kind
== CTF_K_FORWARD
1699 && (dst_kind
== CTF_K_ENUM
|| dst_kind
== CTF_K_STRUCT
1700 || dst_kind
== CTF_K_UNION
))
1702 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1706 if (dst_kind
!= CTF_K_FORWARD
1707 || (kind
!= CTF_K_ENUM
&& kind
!= CTF_K_STRUCT
1708 && kind
!= CTF_K_UNION
))
1710 ctf_dprintf ("Conflict for type %s: kinds differ, new: %i; "
1711 "old (ID %lx): %i\n", name
, kind
, dst_type
, dst_kind
);
1712 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1716 /* We take special action for an integer, float, or slice since it is
1717 described not only by its name but also its encoding. For integers,
1718 bit-fields exploit this degeneracy. */
1720 if (kind
== CTF_K_INTEGER
|| kind
== CTF_K_FLOAT
|| kind
== CTF_K_SLICE
)
1722 if (ctf_type_encoding (src_fp
, src_type
, &src_en
) != 0)
1723 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1725 if (dst_type
!= CTF_ERR
)
1727 ctf_file_t
*fp
= dst_fp
;
1729 if ((dst_tp
= ctf_lookup_by_id (&fp
, dst_type
)) == NULL
)
1732 if (ctf_type_encoding (dst_fp
, dst_type
, &dst_en
) != 0)
1733 return CTF_ERR
; /* errno set for us. */
1735 if (LCTF_INFO_ISROOT (fp
, dst_tp
->ctt_info
) & CTF_ADD_ROOT
)
1737 /* The type that we found in the hash is also root-visible. If
1738 the two types match then use the existing one; otherwise,
1739 declare a conflict. Note: slices are not certain to match
1740 even if there is no conflict: we must check the contained type
1743 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1745 if (kind
!= CTF_K_SLICE
)
1747 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1753 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1758 /* We found a non-root-visible type in the hash. If its encoding
1759 is the same, we can reuse it, unless it is a slice. */
1761 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1763 if (kind
!= CTF_K_SLICE
)
1765 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1773 src
.ctb_file
= src_fp
;
1774 src
.ctb_type
= src_type
;
1777 dst
.ctb_file
= dst_fp
;
1778 dst
.ctb_type
= dst_type
;
1781 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add
1782 a new type with the same properties as src_type to dst_fp. If dst_type is
1783 not CTF_ERR, then we verify that dst_type has the same attributes as
1784 src_type. We recurse for embedded references. Before we start, we note
1785 that we are processing this type, to prevent infinite recursion: we do not
1786 re-process any type that appears in this list. The list is emptied
1787 wholesale at the end of processing everything in this recursive stack. */
1789 if (ctf_dynhash_insert (proc_tracking_fp
->ctf_add_processing
,
1790 (void *) (uintptr_t) src_type
, (void *) 1) < 0)
1791 return ctf_set_errno (dst_fp
, ENOMEM
);
1796 /* If we found a match we will have either returned it or declared a
1798 dst_type
= ctf_add_integer (dst_fp
, flag
, name
, &src_en
);
1802 /* If we found a match we will have either returned it or declared a
1804 dst_type
= ctf_add_float (dst_fp
, flag
, name
, &src_en
);
1808 /* We have checked for conflicting encodings: now try to add the
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_slice (dst_fp
, flag
, src_type
, &src_en
);
1821 case CTF_K_VOLATILE
:
1823 case CTF_K_RESTRICT
:
1824 src_type
= ctf_type_reference (src_fp
, src_type
);
1825 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1828 if (src_type
== CTF_ERR
)
1829 return CTF_ERR
; /* errno is set for us. */
1831 dst_type
= ctf_add_reftype (dst_fp
, flag
, src_type
, kind
);
1835 if (ctf_array_info (src_fp
, src_type
, &src_ar
) != 0)
1836 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1838 src_ar
.ctr_contents
=
1839 ctf_add_type_internal (dst_fp
, src_fp
, src_ar
.ctr_contents
,
1841 src_ar
.ctr_index
= ctf_add_type_internal (dst_fp
, src_fp
,
1844 src_ar
.ctr_nelems
= src_ar
.ctr_nelems
;
1846 if (src_ar
.ctr_contents
== CTF_ERR
|| src_ar
.ctr_index
== CTF_ERR
)
1847 return CTF_ERR
; /* errno is set for us. */
1849 if (dst_type
!= CTF_ERR
)
1851 if (ctf_array_info (dst_fp
, dst_type
, &dst_ar
) != 0)
1852 return CTF_ERR
; /* errno is set for us. */
1854 if (memcmp (&src_ar
, &dst_ar
, sizeof (ctf_arinfo_t
)))
1856 ctf_dprintf ("Conflict for type %s against ID %lx: "
1857 "array info differs, old %lx/%lx/%x; "
1858 "new: %lx/%lx/%x\n", name
, dst_type
,
1859 src_ar
.ctr_contents
, src_ar
.ctr_index
,
1860 src_ar
.ctr_nelems
, dst_ar
.ctr_contents
,
1861 dst_ar
.ctr_index
, dst_ar
.ctr_nelems
);
1862 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1866 dst_type
= ctf_add_array (dst_fp
, flag
, &src_ar
);
1869 case CTF_K_FUNCTION
:
1870 ctc
.ctc_return
= ctf_add_type_internal (dst_fp
, src_fp
,
1876 if (ctc
.ctc_return
== CTF_ERR
)
1877 return CTF_ERR
; /* errno is set for us. */
1879 dst_type
= ctf_add_function (dst_fp
, flag
, &ctc
, NULL
);
1891 /* Technically to match a struct or union we need to check both
1892 ways (src members vs. dst, dst members vs. src) but we make
1893 this more optimal by only checking src vs. dst and comparing
1894 the total size of the structure (which we must do anyway)
1895 which covers the possibility of dst members not in src.
1896 This optimization can be defeated for unions, but is so
1897 pathological as to render it irrelevant for our purposes. */
1899 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
1900 && dst_kind
!= CTF_K_FORWARD
)
1902 if (ctf_type_size (src_fp
, src_type
) !=
1903 ctf_type_size (dst_fp
, dst_type
))
1905 ctf_dprintf ("Conflict for type %s against ID %lx: "
1906 "union size differs, old %li, new %li\n",
1908 (long) ctf_type_size (src_fp
, src_type
),
1909 (long) ctf_type_size (dst_fp
, dst_type
));
1910 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1913 if (ctf_member_iter (src_fp
, src_type
, membcmp
, &dst
))
1915 ctf_dprintf ("Conflict for type %s against ID %lx: "
1916 "members differ, see above\n", name
, dst_type
);
1917 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1923 /* Unlike the other cases, copying structs and unions is done
1924 manually so as to avoid repeated lookups in ctf_add_member
1925 and to ensure the exact same member offsets as in src_type. */
1927 dst_type
= ctf_add_generic (dst_fp
, flag
, name
, kind
, &dtd
);
1928 if (dst_type
== CTF_ERR
)
1929 return CTF_ERR
; /* errno is set for us. */
1931 dst
.ctb_type
= dst_type
;
1934 /* Pre-emptively add this struct to the type mapping so that
1935 structures that refer to themselves work. */
1936 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1938 if (ctf_member_iter (src_fp
, src_type
, membadd
, &dst
) != 0)
1939 errs
++; /* Increment errs and fail at bottom of case. */
1941 if ((ssize
= ctf_type_size (src_fp
, src_type
)) < 0)
1942 return CTF_ERR
; /* errno is set for us. */
1944 size
= (size_t) ssize
;
1945 if (size
> CTF_MAX_SIZE
)
1947 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1948 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1949 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1952 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1954 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, vlen
);
1956 /* Make a final pass through the members changing each dmd_type (a
1957 src_fp type) to an equivalent type in dst_fp. We pass through all
1958 members, leaving any that fail set to CTF_ERR, unless they fail
1959 because they are marking a member of type not representable in this
1960 version of CTF, in which case we just want to silently omit them:
1961 no consumer can do anything with them anyway. */
1962 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1963 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1965 ctf_file_t
*dst
= dst_fp
;
1968 memb_type
= ctf_type_mapping (src_fp
, dmd
->dmd_type
, &dst
);
1971 if ((dmd
->dmd_type
=
1972 ctf_add_type_internal (dst_fp
, src_fp
, dmd
->dmd_type
,
1973 proc_tracking_fp
)) == CTF_ERR
)
1975 if (ctf_errno (dst_fp
) != ECTF_NONREPRESENTABLE
)
1980 dmd
->dmd_type
= memb_type
;
1984 return CTF_ERR
; /* errno is set for us. */
1989 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
1990 && dst_kind
!= CTF_K_FORWARD
)
1992 if (ctf_enum_iter (src_fp
, src_type
, enumcmp
, &dst
)
1993 || ctf_enum_iter (dst_fp
, dst_type
, enumcmp
, &src
))
1995 ctf_dprintf ("Conflict for enum %s against ID %lx: "
1996 "members differ, see above\n", name
, dst_type
);
1997 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2002 dst_type
= ctf_add_enum (dst_fp
, flag
, name
);
2003 if ((dst
.ctb_type
= dst_type
) == CTF_ERR
2004 || ctf_enum_iter (src_fp
, src_type
, enumadd
, &dst
))
2005 return CTF_ERR
; /* errno is set for us */
2010 if (dst_type
== CTF_ERR
)
2011 dst_type
= ctf_add_forward (dst_fp
, flag
, name
, forward_kind
);
2015 src_type
= ctf_type_reference (src_fp
, src_type
);
2016 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
2019 if (src_type
== CTF_ERR
)
2020 return CTF_ERR
; /* errno is set for us. */
2022 /* If dst_type is not CTF_ERR at this point, we should check if
2023 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
2024 ECTF_CONFLICT. However, this causes problems with bitness typedefs
2025 that vary based on things like if 32-bit then pid_t is int otherwise
2026 long. We therefore omit this check and assume that if the identically
2027 named typedef already exists in dst_fp, it is correct or
2030 if (dst_type
== CTF_ERR
)
2031 dst_type
= ctf_add_typedef (dst_fp
, flag
, name
, src_type
);
2036 return (ctf_set_errno (dst_fp
, ECTF_CORRUPT
));
2039 if (dst_type
!= CTF_ERR
)
2040 ctf_add_type_mapping (src_fp
, orig_src_type
, dst_fp
, dst_type
);
2045 ctf_add_type (ctf_file_t
*dst_fp
, ctf_file_t
*src_fp
, ctf_id_t src_type
)
2049 if (!src_fp
->ctf_add_processing
)
2050 src_fp
->ctf_add_processing
= ctf_dynhash_create (ctf_hash_integer
,
2051 ctf_hash_eq_integer
,
2054 /* We store the hash on the source, because it contains only source type IDs:
2055 but callers will invariably expect errors to appear on the dest. */
2056 if (!src_fp
->ctf_add_processing
)
2057 return (ctf_set_errno (dst_fp
, ENOMEM
));
2059 id
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
, src_fp
);
2060 ctf_dynhash_empty (src_fp
->ctf_add_processing
);
2065 /* Write the compressed CTF data stream to the specified gzFile descriptor. */
2067 ctf_gzwrite (ctf_file_t
*fp
, gzFile fd
)
2069 const unsigned char *buf
;
2073 resid
= sizeof (ctf_header_t
);
2074 buf
= (unsigned char *) fp
->ctf_header
;
2077 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
2078 return (ctf_set_errno (fp
, errno
));
2083 resid
= fp
->ctf_size
;
2087 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
2088 return (ctf_set_errno (fp
, errno
));
2096 /* Compress the specified CTF data stream and write it to the specified file
2099 ctf_compress_write (ctf_file_t
*fp
, int fd
)
2104 ctf_header_t
*hp
= &h
;
2105 ssize_t header_len
= sizeof (ctf_header_t
);
2106 ssize_t compress_len
;
2111 if (ctf_serialize (fp
) < 0)
2112 return -1; /* errno is set for us. */
2114 memcpy (hp
, fp
->ctf_header
, header_len
);
2115 hp
->cth_flags
|= CTF_F_COMPRESS
;
2116 compress_len
= compressBound (fp
->ctf_size
);
2118 if ((buf
= ctf_alloc (compress_len
)) == NULL
)
2119 return (ctf_set_errno (fp
, ECTF_ZALLOC
));
2121 if ((rc
= compress (buf
, (uLongf
*) &compress_len
,
2122 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
2124 ctf_dprintf ("zlib deflate err: %s\n", zError (rc
));
2125 err
= ctf_set_errno (fp
, ECTF_COMPRESS
);
2129 while (header_len
> 0)
2131 if ((len
= write (fd
, hp
, header_len
)) < 0)
2133 err
= ctf_set_errno (fp
, errno
);
2141 while (compress_len
> 0)
2143 if ((len
= write (fd
, bp
, compress_len
)) < 0)
2145 err
= ctf_set_errno (fp
, errno
);
2148 compress_len
-= len
;
2157 /* Optionally compress the specified CTF data stream and return it as a new
2158 dynamically-allocated string. */
2160 ctf_write_mem (ctf_file_t
*fp
, size_t *size
, size_t threshold
)
2165 ssize_t header_len
= sizeof (ctf_header_t
);
2166 ssize_t compress_len
;
2169 if (ctf_serialize (fp
) < 0)
2170 return NULL
; /* errno is set for us. */
2172 compress_len
= compressBound (fp
->ctf_size
);
2173 if (fp
->ctf_size
< threshold
)
2174 compress_len
= fp
->ctf_size
;
2175 if ((buf
= malloc (compress_len
2176 + sizeof (struct ctf_header
))) == NULL
)
2178 ctf_set_errno (fp
, ENOMEM
);
2182 hp
= (ctf_header_t
*) buf
;
2183 memcpy (hp
, fp
->ctf_header
, header_len
);
2184 bp
= buf
+ sizeof (struct ctf_header
);
2185 *size
= sizeof (struct ctf_header
);
2187 if (fp
->ctf_size
< threshold
)
2189 hp
->cth_flags
&= ~CTF_F_COMPRESS
;
2190 memcpy (bp
, fp
->ctf_buf
, fp
->ctf_size
);
2191 *size
+= fp
->ctf_size
;
2195 hp
->cth_flags
|= CTF_F_COMPRESS
;
2196 if ((rc
= compress (bp
, (uLongf
*) &compress_len
,
2197 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
2199 ctf_dprintf ("zlib deflate err: %s\n", zError (rc
));
2200 ctf_set_errno (fp
, ECTF_COMPRESS
);
2204 *size
+= compress_len
;
2209 /* Write the uncompressed CTF data stream to the specified file descriptor. */
2211 ctf_write (ctf_file_t
*fp
, int fd
)
2213 const unsigned char *buf
;
2217 if (ctf_serialize (fp
) < 0)
2218 return -1; /* errno is set for us. */
2220 resid
= sizeof (ctf_header_t
);
2221 buf
= (unsigned char *) fp
->ctf_header
;
2224 if ((len
= write (fd
, buf
, resid
)) <= 0)
2225 return (ctf_set_errno (fp
, errno
));
2230 resid
= fp
->ctf_size
;
2234 if ((len
= write (fd
, buf
, resid
)) <= 0)
2235 return (ctf_set_errno (fp
, errno
));