2 Copyright (C) 2019-2020 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>
28 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
31 /* Make sure the ptrtab has enough space for at least one more type.
33 We start with 4KiB of ptrtab, enough for a thousand types, then grow it 25%
37 ctf_grow_ptrtab (ctf_file_t
*fp
)
39 size_t new_ptrtab_len
= fp
->ctf_ptrtab_len
;
41 /* We allocate one more ptrtab entry than we need, for the initial zero,
42 plus one because the caller will probably allocate a new type. */
44 if (fp
->ctf_ptrtab
== NULL
)
45 new_ptrtab_len
= 1024;
46 else if ((fp
->ctf_typemax
+ 2) > fp
->ctf_ptrtab_len
)
47 new_ptrtab_len
= fp
->ctf_ptrtab_len
* 1.25;
49 if (new_ptrtab_len
!= fp
->ctf_ptrtab_len
)
53 if ((new_ptrtab
= realloc (fp
->ctf_ptrtab
,
54 new_ptrtab_len
* sizeof (uint32_t))) == NULL
)
55 return (ctf_set_errno (fp
, ENOMEM
));
57 fp
->ctf_ptrtab
= new_ptrtab
;
58 memset (fp
->ctf_ptrtab
+ fp
->ctf_ptrtab_len
, 0,
59 (new_ptrtab_len
- fp
->ctf_ptrtab_len
) * sizeof (uint32_t));
60 fp
->ctf_ptrtab_len
= new_ptrtab_len
;
65 /* To create an empty CTF container, we just declare a zeroed header and call
66 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w
67 and initialize the dynamic members. We start assigning type IDs at 1 because
68 type ID 0 is used as a sentinel and a not-found indicator. */
71 ctf_create (int *errp
)
73 static const ctf_header_t hdr
= { .cth_preamble
= { CTF_MAGIC
, CTF_VERSION
, 0 } };
75 ctf_dynhash_t
*dthash
;
76 ctf_dynhash_t
*dvhash
;
77 ctf_dynhash_t
*structs
= NULL
, *unions
= NULL
, *enums
= NULL
, *names
= NULL
;
82 dthash
= ctf_dynhash_create (ctf_hash_integer
, ctf_hash_eq_integer
,
86 ctf_set_open_errno (errp
, EAGAIN
);
90 dvhash
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
94 ctf_set_open_errno (errp
, EAGAIN
);
98 structs
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
100 unions
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
102 enums
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
104 names
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
106 if (!structs
|| !unions
|| !enums
|| !names
)
108 ctf_set_open_errno (errp
, EAGAIN
);
112 cts
.cts_name
= _CTF_SECTION
;
114 cts
.cts_size
= sizeof (hdr
);
117 if ((fp
= ctf_bufopen_internal (&cts
, NULL
, NULL
, NULL
, 1, errp
)) == NULL
)
120 fp
->ctf_structs
.ctn_writable
= structs
;
121 fp
->ctf_unions
.ctn_writable
= unions
;
122 fp
->ctf_enums
.ctn_writable
= enums
;
123 fp
->ctf_names
.ctn_writable
= names
;
124 fp
->ctf_dthash
= dthash
;
125 fp
->ctf_dvhash
= dvhash
;
127 fp
->ctf_snapshots
= 1;
128 fp
->ctf_snapshot_lu
= 0;
130 ctf_set_ctl_hashes (fp
);
131 ctf_setmodel (fp
, CTF_MODEL_NATIVE
);
132 if (ctf_grow_ptrtab (fp
) < 0)
134 ctf_set_open_errno (errp
, ctf_errno (fp
));
142 ctf_dynhash_destroy (structs
);
143 ctf_dynhash_destroy (unions
);
144 ctf_dynhash_destroy (enums
);
145 ctf_dynhash_destroy (names
);
146 ctf_dynhash_destroy (dvhash
);
148 ctf_dynhash_destroy (dthash
);
153 static unsigned char *
154 ctf_copy_smembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
156 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
159 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
161 ctf_member_t
*copied
;
164 ctm
.ctm_type
= (uint32_t) dmd
->dmd_type
;
165 ctm
.ctm_offset
= (uint32_t) dmd
->dmd_offset
;
167 memcpy (t
, &ctm
, sizeof (ctm
));
168 copied
= (ctf_member_t
*) t
;
170 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctm_name
);
178 static unsigned char *
179 ctf_copy_lmembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
181 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
184 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
186 ctf_lmember_t
*copied
;
189 ctlm
.ctlm_type
= (uint32_t) dmd
->dmd_type
;
190 ctlm
.ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI (dmd
->dmd_offset
);
191 ctlm
.ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO (dmd
->dmd_offset
);
193 memcpy (t
, &ctlm
, sizeof (ctlm
));
194 copied
= (ctf_lmember_t
*) t
;
196 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctlm_name
);
204 static unsigned char *
205 ctf_copy_emembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
207 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
210 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
214 cte
.cte_value
= dmd
->dmd_value
;
215 memcpy (t
, &cte
, sizeof (cte
));
216 copied
= (ctf_enum_t
*) t
;
217 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->cte_name
);
224 /* Sort a newly-constructed static variable array. */
226 typedef struct ctf_sort_var_arg_cb
230 } ctf_sort_var_arg_cb_t
;
233 ctf_sort_var (const void *one_
, const void *two_
, void *arg_
)
235 const ctf_varent_t
*one
= one_
;
236 const ctf_varent_t
*two
= two_
;
237 ctf_sort_var_arg_cb_t
*arg
= arg_
;
239 return (strcmp (ctf_strraw_explicit (arg
->fp
, one
->ctv_name
, arg
->strtab
),
240 ctf_strraw_explicit (arg
->fp
, two
->ctv_name
, arg
->strtab
)));
243 /* Compatibility: just update the threshold for ctf_discard. */
245 ctf_update (ctf_file_t
*fp
)
247 if (!(fp
->ctf_flags
& LCTF_RDWR
))
248 return (ctf_set_errno (fp
, ECTF_RDONLY
));
250 fp
->ctf_dtoldid
= fp
->ctf_typemax
;
254 /* If the specified CTF container is writable and has been modified, reload this
255 container with the updated type definitions, ready for serialization. In
256 order to make this code and the rest of libctf as simple as possible, we
257 perform updates by taking the dynamic type definitions and creating an
258 in-memory CTF file containing the definitions, and then call
259 ctf_simple_open_internal() on it. We perform one extra trick here for the
260 benefit of callers and to keep our code simple: ctf_simple_open_internal()
261 will return a new ctf_file_t, but we want to keep the fp constant for the
262 caller, so after ctf_simple_open_internal() returns, we use memcpy to swap
263 the interior of the old and new ctf_file_t's, and then free the old. */
265 ctf_serialize (ctf_file_t
*fp
)
267 ctf_file_t ofp
, *nfp
;
268 ctf_header_t hdr
, *hdrp
;
271 ctf_varent_t
*dvarents
;
272 ctf_strs_writable_t strtab
;
276 size_t buf_size
, type_size
, nvars
;
277 unsigned char *buf
, *newbuf
;
280 if (!(fp
->ctf_flags
& LCTF_RDWR
))
281 return (ctf_set_errno (fp
, ECTF_RDONLY
));
283 /* Update required? */
284 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
287 /* Fill in an initial CTF header. We will leave the label, object,
288 and function sections empty and only output a header, type section,
289 and string table. The type section begins at a 4-byte aligned
290 boundary past the CTF header itself (at relative offset zero). */
292 memset (&hdr
, 0, sizeof (hdr
));
293 hdr
.cth_magic
= CTF_MAGIC
;
294 hdr
.cth_version
= CTF_VERSION
;
296 /* Iterate through the dynamic type definition list and compute the
297 size of the CTF type section we will need to generate. */
299 for (type_size
= 0, dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
300 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
302 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
303 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
305 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
306 type_size
+= sizeof (ctf_stype_t
);
308 type_size
+= sizeof (ctf_type_t
);
314 type_size
+= sizeof (uint32_t);
317 type_size
+= sizeof (ctf_array_t
);
320 type_size
+= sizeof (ctf_slice_t
);
323 type_size
+= sizeof (uint32_t) * (vlen
+ (vlen
& 1));
327 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
328 type_size
+= sizeof (ctf_member_t
) * vlen
;
330 type_size
+= sizeof (ctf_lmember_t
) * vlen
;
333 type_size
+= sizeof (ctf_enum_t
) * vlen
;
338 /* Computing the number of entries in the CTF variable section is much
341 for (nvars
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
);
342 dvd
!= NULL
; dvd
= ctf_list_next (dvd
), nvars
++);
344 /* Compute the size of the CTF buffer we need, sans only the string table,
345 then allocate a new buffer and memcpy the finished header to the start of
346 the buffer. (We will adjust this later with strtab length info.) */
348 hdr
.cth_typeoff
= hdr
.cth_varoff
+ (nvars
* sizeof (ctf_varent_t
));
349 hdr
.cth_stroff
= hdr
.cth_typeoff
+ type_size
;
352 buf_size
= sizeof (ctf_header_t
) + hdr
.cth_stroff
+ hdr
.cth_strlen
;
354 if ((buf
= malloc (buf_size
)) == NULL
)
355 return (ctf_set_errno (fp
, EAGAIN
));
357 memcpy (buf
, &hdr
, sizeof (ctf_header_t
));
358 t
= (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_varoff
;
360 hdrp
= (ctf_header_t
*) buf
;
361 if ((fp
->ctf_flags
& LCTF_CHILD
) && (fp
->ctf_parname
!= NULL
))
362 ctf_str_add_ref (fp
, fp
->ctf_parname
, &hdrp
->cth_parname
);
363 if (fp
->ctf_cuname
!= NULL
)
364 ctf_str_add_ref (fp
, fp
->ctf_cuname
, &hdrp
->cth_cuname
);
366 /* Work over the variable list, translating everything into ctf_varent_t's and
367 prepping the string table. */
369 dvarents
= (ctf_varent_t
*) t
;
370 for (i
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
;
371 dvd
= ctf_list_next (dvd
), i
++)
373 ctf_varent_t
*var
= &dvarents
[i
];
375 ctf_str_add_ref (fp
, dvd
->dvd_name
, &var
->ctv_name
);
376 var
->ctv_type
= (uint32_t) dvd
->dvd_type
;
380 t
+= sizeof (ctf_varent_t
) * nvars
;
382 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_typeoff
);
384 /* We now take a final lap through the dynamic type definition list and copy
385 the appropriate type records to the output buffer, noting down the
388 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
389 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
391 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
392 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
400 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
401 len
= sizeof (ctf_stype_t
);
403 len
= sizeof (ctf_type_t
);
405 memcpy (t
, &dtd
->dtd_data
, len
);
406 copied
= (ctf_stype_t
*) t
; /* name is at the start: constant offset. */
408 && (name
= ctf_strraw (fp
, copied
->ctt_name
)) != NULL
)
409 ctf_str_add_ref (fp
, name
, &copied
->ctt_name
);
416 if (kind
== CTF_K_INTEGER
)
418 encoding
= CTF_INT_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
419 dtd
->dtd_u
.dtu_enc
.cte_offset
,
420 dtd
->dtd_u
.dtu_enc
.cte_bits
);
424 encoding
= CTF_FP_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
425 dtd
->dtd_u
.dtu_enc
.cte_offset
,
426 dtd
->dtd_u
.dtu_enc
.cte_bits
);
428 memcpy (t
, &encoding
, sizeof (encoding
));
429 t
+= sizeof (encoding
);
433 memcpy (t
, &dtd
->dtd_u
.dtu_slice
, sizeof (struct ctf_slice
));
434 t
+= sizeof (struct ctf_slice
);
438 cta
.cta_contents
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_contents
;
439 cta
.cta_index
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_index
;
440 cta
.cta_nelems
= dtd
->dtd_u
.dtu_arr
.ctr_nelems
;
441 memcpy (t
, &cta
, sizeof (cta
));
447 uint32_t *argv
= (uint32_t *) (uintptr_t) t
;
450 for (argc
= 0; argc
< vlen
; argc
++)
451 *argv
++ = dtd
->dtd_u
.dtu_argv
[argc
];
454 *argv
++ = 0; /* Pad to 4-byte boundary. */
456 t
= (unsigned char *) argv
;
462 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
463 t
= ctf_copy_smembers (fp
, dtd
, t
);
465 t
= ctf_copy_lmembers (fp
, dtd
, t
);
469 t
= ctf_copy_emembers (fp
, dtd
, t
);
473 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_stroff
);
475 /* Construct the final string table and fill out all the string refs with the
476 final offsets. Then purge the refs list, because we're about to move this
477 strtab onto the end of the buf, invalidating all the offsets. */
478 strtab
= ctf_str_write_strtab (fp
);
479 ctf_str_purge_refs (fp
);
481 if (strtab
.cts_strs
== NULL
)
484 return (ctf_set_errno (fp
, EAGAIN
));
487 /* Now the string table is constructed, we can sort the buffer of
489 ctf_sort_var_arg_cb_t sort_var_arg
= { fp
, (ctf_strs_t
*) &strtab
};
490 ctf_qsort_r (dvarents
, nvars
, sizeof (ctf_varent_t
), ctf_sort_var
,
493 if ((newbuf
= ctf_realloc (fp
, buf
, buf_size
+ strtab
.cts_len
)) == NULL
)
496 free (strtab
.cts_strs
);
497 return (ctf_set_errno (fp
, EAGAIN
));
500 memcpy (buf
+ buf_size
, strtab
.cts_strs
, strtab
.cts_len
);
501 hdrp
= (ctf_header_t
*) buf
;
502 hdrp
->cth_strlen
= strtab
.cts_len
;
503 buf_size
+= hdrp
->cth_strlen
;
504 free (strtab
.cts_strs
);
506 /* Finally, we are ready to ctf_simple_open() the new container. If this
507 is successful, we then switch nfp and fp and free the old container. */
509 if ((nfp
= ctf_simple_open_internal ((char *) buf
, buf_size
, NULL
, 0,
510 0, NULL
, 0, fp
->ctf_syn_ext_strtab
,
514 return (ctf_set_errno (fp
, err
));
517 (void) ctf_setmodel (nfp
, ctf_getmodel (fp
));
518 (void) ctf_import (nfp
, fp
->ctf_parent
);
520 nfp
->ctf_refcnt
= fp
->ctf_refcnt
;
521 nfp
->ctf_flags
|= fp
->ctf_flags
& ~LCTF_DIRTY
;
522 if (nfp
->ctf_dynbase
== NULL
)
523 nfp
->ctf_dynbase
= buf
; /* Make sure buf is freed on close. */
524 nfp
->ctf_dthash
= fp
->ctf_dthash
;
525 nfp
->ctf_dtdefs
= fp
->ctf_dtdefs
;
526 nfp
->ctf_dvhash
= fp
->ctf_dvhash
;
527 nfp
->ctf_dvdefs
= fp
->ctf_dvdefs
;
528 nfp
->ctf_dtoldid
= fp
->ctf_dtoldid
;
529 nfp
->ctf_add_processing
= fp
->ctf_add_processing
;
530 nfp
->ctf_snapshots
= fp
->ctf_snapshots
+ 1;
531 nfp
->ctf_specific
= fp
->ctf_specific
;
532 nfp
->ctf_ptrtab
= fp
->ctf_ptrtab
;
533 nfp
->ctf_ptrtab_len
= fp
->ctf_ptrtab_len
;
534 nfp
->ctf_link_inputs
= fp
->ctf_link_inputs
;
535 nfp
->ctf_link_outputs
= fp
->ctf_link_outputs
;
536 nfp
->ctf_str_prov_offset
= fp
->ctf_str_prov_offset
;
537 nfp
->ctf_syn_ext_strtab
= fp
->ctf_syn_ext_strtab
;
538 nfp
->ctf_link_cu_mapping
= fp
->ctf_link_cu_mapping
;
539 nfp
->ctf_link_type_mapping
= fp
->ctf_link_type_mapping
;
540 nfp
->ctf_link_memb_name_changer
= fp
->ctf_link_memb_name_changer
;
541 nfp
->ctf_link_memb_name_changer_arg
= fp
->ctf_link_memb_name_changer_arg
;
543 nfp
->ctf_snapshot_lu
= fp
->ctf_snapshots
;
545 memcpy (&nfp
->ctf_lookups
, fp
->ctf_lookups
, sizeof (fp
->ctf_lookups
));
546 nfp
->ctf_structs
= fp
->ctf_structs
;
547 nfp
->ctf_unions
= fp
->ctf_unions
;
548 nfp
->ctf_enums
= fp
->ctf_enums
;
549 nfp
->ctf_names
= fp
->ctf_names
;
551 fp
->ctf_dthash
= NULL
;
552 ctf_str_free_atoms (nfp
);
553 nfp
->ctf_str_atoms
= fp
->ctf_str_atoms
;
554 nfp
->ctf_prov_strtab
= fp
->ctf_prov_strtab
;
555 fp
->ctf_str_atoms
= NULL
;
556 fp
->ctf_prov_strtab
= NULL
;
557 memset (&fp
->ctf_dtdefs
, 0, sizeof (ctf_list_t
));
558 fp
->ctf_add_processing
= NULL
;
559 fp
->ctf_ptrtab
= NULL
;
560 fp
->ctf_link_inputs
= NULL
;
561 fp
->ctf_link_outputs
= NULL
;
562 fp
->ctf_syn_ext_strtab
= NULL
;
563 fp
->ctf_link_cu_mapping
= NULL
;
564 fp
->ctf_link_type_mapping
= NULL
;
566 fp
->ctf_dvhash
= NULL
;
567 memset (&fp
->ctf_dvdefs
, 0, sizeof (ctf_list_t
));
568 memset (fp
->ctf_lookups
, 0, sizeof (fp
->ctf_lookups
));
569 fp
->ctf_structs
.ctn_writable
= NULL
;
570 fp
->ctf_unions
.ctn_writable
= NULL
;
571 fp
->ctf_enums
.ctn_writable
= NULL
;
572 fp
->ctf_names
.ctn_writable
= NULL
;
574 memcpy (&ofp
, fp
, sizeof (ctf_file_t
));
575 memcpy (fp
, nfp
, sizeof (ctf_file_t
));
576 memcpy (nfp
, &ofp
, sizeof (ctf_file_t
));
578 nfp
->ctf_refcnt
= 1; /* Force nfp to be freed. */
579 ctf_file_close (nfp
);
585 ctf_name_table (ctf_file_t
*fp
, int kind
)
590 return &fp
->ctf_structs
;
592 return &fp
->ctf_unions
;
594 return &fp
->ctf_enums
;
596 return &fp
->ctf_names
;
601 ctf_dtd_insert (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, int flag
, int kind
)
604 if (ctf_dynhash_insert (fp
->ctf_dthash
, (void *) dtd
->dtd_type
, dtd
) < 0)
607 if (flag
== CTF_ADD_ROOT
&& dtd
->dtd_data
.ctt_name
608 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
)
610 if (ctf_dynhash_insert (ctf_name_table (fp
, kind
)->ctn_writable
,
611 (char *) name
, (void *) dtd
->dtd_type
) < 0)
613 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) dtd
->dtd_type
);
617 ctf_list_append (&fp
->ctf_dtdefs
, dtd
);
622 ctf_dtd_delete (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
)
624 ctf_dmdef_t
*dmd
, *nmd
;
625 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
626 int name_kind
= kind
;
629 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) dtd
->dtd_type
);
636 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
637 dmd
!= NULL
; dmd
= nmd
)
639 if (dmd
->dmd_name
!= NULL
)
640 free (dmd
->dmd_name
);
641 nmd
= ctf_list_next (dmd
);
646 free (dtd
->dtd_u
.dtu_argv
);
649 name_kind
= dtd
->dtd_data
.ctt_type
;
653 if (dtd
->dtd_data
.ctt_name
654 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
655 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
657 ctf_dynhash_remove (ctf_name_table (fp
, name_kind
)->ctn_writable
,
659 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
662 ctf_list_delete (&fp
->ctf_dtdefs
, dtd
);
667 ctf_dtd_lookup (const ctf_file_t
*fp
, ctf_id_t type
)
669 return (ctf_dtdef_t
*) ctf_dynhash_lookup (fp
->ctf_dthash
, (void *) type
);
673 ctf_dynamic_type (const ctf_file_t
*fp
, ctf_id_t id
)
677 if (!(fp
->ctf_flags
& LCTF_RDWR
))
680 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, id
))
683 idx
= LCTF_TYPE_TO_INDEX(fp
, id
);
685 if ((unsigned long) idx
<= fp
->ctf_typemax
)
686 return ctf_dtd_lookup (fp
, id
);
691 ctf_dvd_insert (ctf_file_t
*fp
, ctf_dvdef_t
*dvd
)
693 if (ctf_dynhash_insert (fp
->ctf_dvhash
, dvd
->dvd_name
, dvd
) < 0)
695 ctf_list_append (&fp
->ctf_dvdefs
, dvd
);
700 ctf_dvd_delete (ctf_file_t
*fp
, ctf_dvdef_t
*dvd
)
702 ctf_dynhash_remove (fp
->ctf_dvhash
, dvd
->dvd_name
);
703 free (dvd
->dvd_name
);
705 ctf_list_delete (&fp
->ctf_dvdefs
, dvd
);
710 ctf_dvd_lookup (const ctf_file_t
*fp
, const char *name
)
712 return (ctf_dvdef_t
*) ctf_dynhash_lookup (fp
->ctf_dvhash
, name
);
715 /* Discard all of the dynamic type definitions and variable definitions that
716 have been added to the container since the last call to ctf_update(). We
717 locate such types by scanning the dtd list and deleting elements that have
718 type IDs greater than ctf_dtoldid, which is set by ctf_update(), above, and
719 by scanning the variable list and deleting elements that have update IDs
720 equal to the current value of the last-update snapshot count (indicating that
721 they were added after the most recent call to ctf_update()). */
723 ctf_discard (ctf_file_t
*fp
)
725 ctf_snapshot_id_t last_update
=
727 fp
->ctf_snapshot_lu
+ 1 };
729 /* Update required? */
730 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
733 return (ctf_rollback (fp
, last_update
));
737 ctf_snapshot (ctf_file_t
*fp
)
739 ctf_snapshot_id_t snapid
;
740 snapid
.dtd_id
= fp
->ctf_typemax
;
741 snapid
.snapshot_id
= fp
->ctf_snapshots
++;
745 /* Like ctf_discard(), only discards everything after a particular ID. */
747 ctf_rollback (ctf_file_t
*fp
, ctf_snapshot_id_t id
)
749 ctf_dtdef_t
*dtd
, *ntd
;
750 ctf_dvdef_t
*dvd
, *nvd
;
752 if (!(fp
->ctf_flags
& LCTF_RDWR
))
753 return (ctf_set_errno (fp
, ECTF_RDONLY
));
755 if (fp
->ctf_snapshot_lu
>= id
.snapshot_id
)
756 return (ctf_set_errno (fp
, ECTF_OVERROLLBACK
));
758 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
); dtd
!= NULL
; dtd
= ntd
)
763 ntd
= ctf_list_next (dtd
);
765 if (LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_type
) <= id
.dtd_id
)
768 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
769 if (kind
== CTF_K_FORWARD
)
770 kind
= dtd
->dtd_data
.ctt_type
;
772 if (dtd
->dtd_data
.ctt_name
773 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
774 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
776 ctf_dynhash_remove (ctf_name_table (fp
, kind
)->ctn_writable
,
778 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
781 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) dtd
->dtd_type
);
782 ctf_dtd_delete (fp
, dtd
);
785 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
; dvd
= nvd
)
787 nvd
= ctf_list_next (dvd
);
789 if (dvd
->dvd_snapshots
<= id
.snapshot_id
)
792 ctf_dvd_delete (fp
, dvd
);
795 fp
->ctf_typemax
= id
.dtd_id
;
796 fp
->ctf_snapshots
= id
.snapshot_id
;
798 if (fp
->ctf_snapshots
== fp
->ctf_snapshot_lu
)
799 fp
->ctf_flags
&= ~LCTF_DIRTY
;
805 ctf_add_generic (ctf_file_t
*fp
, uint32_t flag
, const char *name
, int kind
,
811 if (flag
!= CTF_ADD_NONROOT
&& flag
!= CTF_ADD_ROOT
)
812 return (ctf_set_errno (fp
, EINVAL
));
814 if (!(fp
->ctf_flags
& LCTF_RDWR
))
815 return (ctf_set_errno (fp
, ECTF_RDONLY
));
817 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) >= CTF_MAX_TYPE
)
818 return (ctf_set_errno (fp
, ECTF_FULL
));
820 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) == (CTF_MAX_PTYPE
- 1))
821 return (ctf_set_errno (fp
, ECTF_FULL
));
823 /* Make sure ptrtab always grows to be big enough for all types. */
824 if (ctf_grow_ptrtab (fp
) < 0)
825 return CTF_ERR
; /* errno is set for us. */
827 if ((dtd
= malloc (sizeof (ctf_dtdef_t
))) == NULL
)
828 return (ctf_set_errno (fp
, EAGAIN
));
830 type
= ++fp
->ctf_typemax
;
831 type
= LCTF_INDEX_TO_TYPE (fp
, type
, (fp
->ctf_flags
& LCTF_CHILD
));
833 memset (dtd
, 0, sizeof (ctf_dtdef_t
));
834 dtd
->dtd_data
.ctt_name
= ctf_str_add_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
835 dtd
->dtd_type
= type
;
837 if (dtd
->dtd_data
.ctt_name
== 0 && name
!= NULL
&& name
[0] != '\0')
840 return (ctf_set_errno (fp
, EAGAIN
));
843 if (ctf_dtd_insert (fp
, dtd
, flag
, kind
) < 0)
846 return CTF_ERR
; /* errno is set for us. */
848 fp
->ctf_flags
|= LCTF_DIRTY
;
854 /* When encoding integer sizes, we want to convert a byte count in the range
855 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
856 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
872 ctf_add_encoded (ctf_file_t
*fp
, uint32_t flag
,
873 const char *name
, const ctf_encoding_t
*ep
, uint32_t kind
)
879 return (ctf_set_errno (fp
, EINVAL
));
881 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, &dtd
)) == CTF_ERR
)
882 return CTF_ERR
; /* errno is set for us. */
884 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
885 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
887 dtd
->dtd_u
.dtu_enc
= *ep
;
893 ctf_add_reftype (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
, uint32_t kind
)
897 ctf_file_t
*tmp
= fp
;
898 int child
= fp
->ctf_flags
& LCTF_CHILD
;
900 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
901 return (ctf_set_errno (fp
, EINVAL
));
903 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
904 return CTF_ERR
; /* errno is set for us. */
906 if ((type
= ctf_add_generic (fp
, flag
, NULL
, kind
, &dtd
)) == CTF_ERR
)
907 return CTF_ERR
; /* errno is set for us. */
909 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
910 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
912 if (kind
!= CTF_K_POINTER
)
915 /* If we are adding a pointer, update the ptrtab, both the directly pointed-to
916 type and (if an anonymous typedef node is being pointed at) the type that
917 points at too. Note that ctf_typemax is at this point one higher than we
918 want to check against, because it's just been incremented for the addition
921 uint32_t type_idx
= LCTF_TYPE_TO_INDEX (fp
, type
);
922 uint32_t ref_idx
= LCTF_TYPE_TO_INDEX (fp
, ref
);
924 if (LCTF_TYPE_ISCHILD (fp
, ref
) == child
925 && ref_idx
< fp
->ctf_typemax
)
927 fp
->ctf_ptrtab
[ref_idx
] = type_idx
;
929 ctf_id_t refref_idx
= LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_data
.ctt_type
);
932 && (LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) == CTF_K_TYPEDEF
)
933 && strcmp (ctf_strptr (fp
, dtd
->dtd_data
.ctt_name
), "") == 0
934 && refref_idx
< fp
->ctf_typemax
)
935 fp
->ctf_ptrtab
[refref_idx
] = type_idx
;
942 ctf_add_slice (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
,
943 const ctf_encoding_t
*ep
)
948 const ctf_type_t
*tp
;
949 ctf_file_t
*tmp
= fp
;
952 return (ctf_set_errno (fp
, EINVAL
));
954 if ((ep
->cte_bits
> 255) || (ep
->cte_offset
> 255))
955 return (ctf_set_errno (fp
, ECTF_SLICEOVERFLOW
));
957 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
958 return (ctf_set_errno (fp
, EINVAL
));
960 if (ref
!= 0 && ((tp
= ctf_lookup_by_id (&tmp
, ref
)) == NULL
))
961 return CTF_ERR
; /* errno is set for us. */
963 kind
= ctf_type_kind_unsliced (tmp
, ref
);
964 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) &&
967 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
969 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_SLICE
, &dtd
)) == CTF_ERR
)
970 return CTF_ERR
; /* errno is set for us. */
972 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_SLICE
, flag
, 0);
973 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
975 dtd
->dtd_u
.dtu_slice
.cts_type
= (uint32_t) ref
;
976 dtd
->dtd_u
.dtu_slice
.cts_bits
= ep
->cte_bits
;
977 dtd
->dtd_u
.dtu_slice
.cts_offset
= ep
->cte_offset
;
983 ctf_add_integer (ctf_file_t
*fp
, uint32_t flag
,
984 const char *name
, const ctf_encoding_t
*ep
)
986 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_INTEGER
));
990 ctf_add_float (ctf_file_t
*fp
, uint32_t flag
,
991 const char *name
, const ctf_encoding_t
*ep
)
993 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_FLOAT
));
997 ctf_add_pointer (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
999 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_POINTER
));
1003 ctf_add_array (ctf_file_t
*fp
, uint32_t flag
, const ctf_arinfo_t
*arp
)
1007 ctf_file_t
*tmp
= fp
;
1010 return (ctf_set_errno (fp
, EINVAL
));
1012 if (arp
->ctr_contents
!= 0
1013 && ctf_lookup_by_id (&tmp
, arp
->ctr_contents
) == NULL
)
1014 return CTF_ERR
; /* errno is set for us. */
1017 if (ctf_lookup_by_id (&tmp
, arp
->ctr_index
) == NULL
)
1018 return CTF_ERR
; /* errno is set for us. */
1020 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_ARRAY
, &dtd
)) == CTF_ERR
)
1021 return CTF_ERR
; /* errno is set for us. */
1023 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ARRAY
, flag
, 0);
1024 dtd
->dtd_data
.ctt_size
= 0;
1025 dtd
->dtd_u
.dtu_arr
= *arp
;
1031 ctf_set_array (ctf_file_t
*fp
, ctf_id_t type
, const ctf_arinfo_t
*arp
)
1033 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1035 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1036 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1039 || LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) != CTF_K_ARRAY
)
1040 return (ctf_set_errno (fp
, ECTF_BADID
));
1042 fp
->ctf_flags
|= LCTF_DIRTY
;
1043 dtd
->dtd_u
.dtu_arr
= *arp
;
1049 ctf_add_function (ctf_file_t
*fp
, uint32_t flag
,
1050 const ctf_funcinfo_t
*ctc
, const ctf_id_t
*argv
)
1055 uint32_t *vdat
= NULL
;
1056 ctf_file_t
*tmp
= fp
;
1059 if (ctc
== NULL
|| (ctc
->ctc_flags
& ~CTF_FUNC_VARARG
) != 0
1060 || (ctc
->ctc_argc
!= 0 && argv
== NULL
))
1061 return (ctf_set_errno (fp
, EINVAL
));
1063 vlen
= ctc
->ctc_argc
;
1064 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
1065 vlen
++; /* Add trailing zero to indicate varargs (see below). */
1067 if (ctc
->ctc_return
!= 0
1068 && ctf_lookup_by_id (&tmp
, ctc
->ctc_return
) == NULL
)
1069 return CTF_ERR
; /* errno is set for us. */
1071 if (vlen
> CTF_MAX_VLEN
)
1072 return (ctf_set_errno (fp
, EOVERFLOW
));
1074 if (vlen
!= 0 && (vdat
= malloc (sizeof (ctf_id_t
) * vlen
)) == NULL
)
1075 return (ctf_set_errno (fp
, EAGAIN
));
1077 for (i
= 0; i
< ctc
->ctc_argc
; i
++)
1080 if (argv
[i
] != 0 && ctf_lookup_by_id (&tmp
, argv
[i
]) == NULL
)
1083 return CTF_ERR
; /* errno is set for us. */
1085 vdat
[i
] = (uint32_t) argv
[i
];
1088 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_FUNCTION
,
1092 return CTF_ERR
; /* errno is set for us. */
1095 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FUNCTION
, flag
, vlen
);
1096 dtd
->dtd_data
.ctt_type
= (uint32_t) ctc
->ctc_return
;
1098 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
1099 vdat
[vlen
- 1] = 0; /* Add trailing zero to indicate varargs. */
1100 dtd
->dtd_u
.dtu_argv
= vdat
;
1106 ctf_add_struct_sized (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1112 /* Promote root-visible forwards to structs. */
1114 type
= ctf_lookup_by_rawname (fp
, CTF_K_STRUCT
, name
);
1116 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1117 dtd
= ctf_dtd_lookup (fp
, type
);
1118 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_STRUCT
,
1120 return CTF_ERR
; /* errno is set for us. */
1122 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_STRUCT
, flag
, 0);
1124 if (size
> CTF_MAX_SIZE
)
1126 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1127 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1128 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1131 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1137 ctf_add_struct (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1139 return (ctf_add_struct_sized (fp
, flag
, name
, 0));
1143 ctf_add_union_sized (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1149 /* Promote root-visible forwards to unions. */
1151 type
= ctf_lookup_by_rawname (fp
, CTF_K_UNION
, name
);
1153 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1154 dtd
= ctf_dtd_lookup (fp
, type
);
1155 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_UNION
,
1157 return CTF_ERR
; /* errno is set for us */
1159 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_UNION
, flag
, 0);
1161 if (size
> CTF_MAX_SIZE
)
1163 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1164 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1165 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1168 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1174 ctf_add_union (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1176 return (ctf_add_union_sized (fp
, flag
, name
, 0));
1180 ctf_add_enum (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1185 /* Promote root-visible forwards to enums. */
1187 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
1189 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1190 dtd
= ctf_dtd_lookup (fp
, type
);
1191 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_ENUM
,
1193 return CTF_ERR
; /* errno is set for us. */
1195 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ENUM
, flag
, 0);
1196 dtd
->dtd_data
.ctt_size
= fp
->ctf_dmodel
->ctd_int
;
1202 ctf_add_enum_encoded (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1203 const ctf_encoding_t
*ep
)
1207 /* First, create the enum if need be, using most of the same machinery as
1208 ctf_add_enum(), to ensure that we do not allow things past that are not
1209 enums or forwards to them. (This includes other slices: you cannot slice a
1210 slice, which would be a useless thing to do anyway.) */
1213 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
1217 if ((ctf_type_kind (fp
, type
) != CTF_K_FORWARD
) &&
1218 (ctf_type_kind_unsliced (fp
, type
) != CTF_K_ENUM
))
1219 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1221 else if ((type
= ctf_add_enum (fp
, flag
, name
)) == CTF_ERR
)
1222 return CTF_ERR
; /* errno is set for us. */
1224 /* Now attach a suitable slice to it. */
1226 return ctf_add_slice (fp
, flag
, type
, ep
);
1230 ctf_add_forward (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1236 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
&& kind
!= CTF_K_ENUM
)
1237 return (ctf_set_errno (fp
, ECTF_NOTSUE
));
1239 /* If the type is already defined or exists as a forward tag, just
1240 return the ctf_id_t of the existing definition. */
1243 type
= ctf_lookup_by_rawname (fp
, kind
, name
);
1248 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, &dtd
)) == CTF_ERR
)
1249 return CTF_ERR
; /* errno is set for us. */
1251 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FORWARD
, flag
, 0);
1252 dtd
->dtd_data
.ctt_type
= kind
;
1258 ctf_add_typedef (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1263 ctf_file_t
*tmp
= fp
;
1265 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
1266 return (ctf_set_errno (fp
, EINVAL
));
1268 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1269 return CTF_ERR
; /* errno is set for us. */
1271 if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_TYPEDEF
,
1273 return CTF_ERR
; /* errno is set for us. */
1275 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_TYPEDEF
, flag
, 0);
1276 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
1282 ctf_add_volatile (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1284 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_VOLATILE
));
1288 ctf_add_const (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1290 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_CONST
));
1294 ctf_add_restrict (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1296 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_RESTRICT
));
1300 ctf_add_enumerator (ctf_file_t
*fp
, ctf_id_t enid
, const char *name
,
1303 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, enid
);
1306 uint32_t kind
, vlen
, root
;
1310 return (ctf_set_errno (fp
, EINVAL
));
1312 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1313 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1316 return (ctf_set_errno (fp
, ECTF_BADID
));
1318 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1319 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1320 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1322 if (kind
!= CTF_K_ENUM
)
1323 return (ctf_set_errno (fp
, ECTF_NOTENUM
));
1325 if (vlen
== CTF_MAX_VLEN
)
1326 return (ctf_set_errno (fp
, ECTF_DTFULL
));
1328 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1329 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1331 if (strcmp (dmd
->dmd_name
, name
) == 0)
1332 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1335 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
1336 return (ctf_set_errno (fp
, EAGAIN
));
1338 if ((s
= strdup (name
)) == NULL
)
1341 return (ctf_set_errno (fp
, EAGAIN
));
1345 dmd
->dmd_type
= CTF_ERR
;
1346 dmd
->dmd_offset
= 0;
1347 dmd
->dmd_value
= value
;
1349 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1350 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
1352 fp
->ctf_flags
|= LCTF_DIRTY
;
1358 ctf_add_member_offset (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1359 ctf_id_t type
, unsigned long bit_offset
)
1361 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, souid
);
1364 ssize_t msize
, malign
, ssize
;
1365 uint32_t kind
, vlen
, root
;
1368 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1369 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1372 return (ctf_set_errno (fp
, ECTF_BADID
));
1374 if (name
!= NULL
&& name
[0] == '\0')
1377 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1378 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1379 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1381 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
1382 return (ctf_set_errno (fp
, ECTF_NOTSOU
));
1384 if (vlen
== CTF_MAX_VLEN
)
1385 return (ctf_set_errno (fp
, ECTF_DTFULL
));
1389 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1390 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1392 if (dmd
->dmd_name
!= NULL
&& strcmp (dmd
->dmd_name
, name
) == 0)
1393 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1397 if ((msize
= ctf_type_size (fp
, type
)) < 0 ||
1398 (malign
= ctf_type_align (fp
, type
)) < 0)
1400 /* The unimplemented type, and any type that resolves to it, has no size
1401 and no alignment: it can correspond to any number of compiler-inserted
1404 if (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
)
1408 ctf_set_errno (fp
, 0);
1411 return -1; /* errno is set for us. */
1414 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
1415 return (ctf_set_errno (fp
, EAGAIN
));
1417 if (name
!= NULL
&& (s
= strdup (name
)) == NULL
)
1420 return (ctf_set_errno (fp
, EAGAIN
));
1424 dmd
->dmd_type
= type
;
1425 dmd
->dmd_value
= -1;
1427 if (kind
== CTF_K_STRUCT
&& vlen
!= 0)
1429 if (bit_offset
== (unsigned long) - 1)
1431 /* Natural alignment. */
1433 ctf_dmdef_t
*lmd
= ctf_list_prev (&dtd
->dtd_u
.dtu_members
);
1434 ctf_id_t ltype
= ctf_type_resolve (fp
, lmd
->dmd_type
);
1435 size_t off
= lmd
->dmd_offset
;
1437 ctf_encoding_t linfo
;
1440 /* Propagate any error from ctf_type_resolve. If the last member was
1441 of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we
1442 cannot insert right after such a member without explicit offset
1443 specification, because its alignment and size is not known. */
1444 if (ltype
== CTF_ERR
)
1447 return -1; /* errno is set for us. */
1450 if (ctf_type_encoding (fp
, ltype
, &linfo
) == 0)
1451 off
+= linfo
.cte_bits
;
1452 else if ((lsize
= ctf_type_size (fp
, ltype
)) > 0)
1453 off
+= lsize
* CHAR_BIT
;
1455 /* Round up the offset of the end of the last member to
1456 the next byte boundary, convert 'off' to bytes, and
1457 then round it up again to the next multiple of the
1458 alignment required by the new member. Finally,
1459 convert back to bits and store the result in
1460 dmd_offset. Technically we could do more efficient
1461 packing if the new member is a bit-field, but we're
1462 the "compiler" and ANSI says we can do as we choose. */
1464 off
= roundup (off
, CHAR_BIT
) / CHAR_BIT
;
1465 off
= roundup (off
, MAX (malign
, 1));
1466 dmd
->dmd_offset
= off
* CHAR_BIT
;
1467 ssize
= off
+ msize
;
1471 /* Specified offset in bits. */
1473 dmd
->dmd_offset
= bit_offset
;
1474 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1475 ssize
= MAX (ssize
, ((signed) bit_offset
/ CHAR_BIT
) + msize
);
1480 dmd
->dmd_offset
= 0;
1481 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1482 ssize
= MAX (ssize
, msize
);
1485 if ((size_t) ssize
> CTF_MAX_SIZE
)
1487 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1488 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (ssize
);
1489 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (ssize
);
1492 dtd
->dtd_data
.ctt_size
= (uint32_t) ssize
;
1494 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1495 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
1497 fp
->ctf_flags
|= LCTF_DIRTY
;
1502 ctf_add_member_encoded (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1503 ctf_id_t type
, unsigned long bit_offset
,
1504 const ctf_encoding_t encoding
)
1506 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1507 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1510 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) && (kind
!= CTF_K_ENUM
))
1511 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1513 if ((type
= ctf_add_slice (fp
, CTF_ADD_NONROOT
, otype
, &encoding
)) == CTF_ERR
)
1514 return -1; /* errno is set for us. */
1516 return ctf_add_member_offset (fp
, souid
, name
, type
, bit_offset
);
1520 ctf_add_member (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1523 return ctf_add_member_offset (fp
, souid
, name
, type
, (unsigned long) - 1);
1527 ctf_add_variable (ctf_file_t
*fp
, const char *name
, ctf_id_t ref
)
1530 ctf_file_t
*tmp
= fp
;
1532 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1533 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1535 if (ctf_dvd_lookup (fp
, name
) != NULL
)
1536 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1538 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1539 return -1; /* errno is set for us. */
1541 /* Make sure this type is representable. */
1542 if ((ctf_type_resolve (fp
, ref
) == CTF_ERR
)
1543 && (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
))
1546 if ((dvd
= malloc (sizeof (ctf_dvdef_t
))) == NULL
)
1547 return (ctf_set_errno (fp
, EAGAIN
));
1549 if (name
!= NULL
&& (dvd
->dvd_name
= strdup (name
)) == NULL
)
1552 return (ctf_set_errno (fp
, EAGAIN
));
1554 dvd
->dvd_type
= ref
;
1555 dvd
->dvd_snapshots
= fp
->ctf_snapshots
;
1557 if (ctf_dvd_insert (fp
, dvd
) < 0)
1559 free (dvd
->dvd_name
);
1561 return -1; /* errno is set for us. */
1564 fp
->ctf_flags
|= LCTF_DIRTY
;
1569 enumcmp (const char *name
, int value
, void *arg
)
1571 ctf_bundle_t
*ctb
= arg
;
1574 if (ctf_enum_value (ctb
->ctb_file
, ctb
->ctb_type
, name
, &bvalue
) < 0)
1576 ctf_dprintf ("Conflict due to member %s iteration error: %s.\n", name
,
1577 ctf_errmsg (ctf_errno (ctb
->ctb_file
)));
1580 if (value
!= bvalue
)
1582 ctf_dprintf ("Conflict due to value change: %i versus %i\n",
1590 enumadd (const char *name
, int value
, void *arg
)
1592 ctf_bundle_t
*ctb
= arg
;
1594 return (ctf_add_enumerator (ctb
->ctb_file
, ctb
->ctb_type
,
1599 membcmp (const char *name
, ctf_id_t type _libctf_unused_
, unsigned long offset
,
1602 ctf_bundle_t
*ctb
= arg
;
1605 if (ctf_member_info (ctb
->ctb_file
, ctb
->ctb_type
, name
, &ctm
) < 0)
1607 ctf_dprintf ("Conflict due to member %s iteration error: %s.\n", name
,
1608 ctf_errmsg (ctf_errno (ctb
->ctb_file
)));
1611 if (ctm
.ctm_offset
!= offset
)
1613 ctf_dprintf ("Conflict due to member %s offset change: "
1614 "%lx versus %lx\n", name
, ctm
.ctm_offset
, offset
);
1621 membadd (const char *name
, ctf_id_t type
, unsigned long offset
, void *arg
)
1623 ctf_bundle_t
*ctb
= arg
;
1627 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
1628 return (ctf_set_errno (ctb
->ctb_file
, EAGAIN
));
1630 if (name
!= NULL
&& (s
= strdup (name
)) == NULL
)
1633 return (ctf_set_errno (ctb
->ctb_file
, EAGAIN
));
1636 /* For now, dmd_type is copied as the src_fp's type; it is reset to an
1637 equivalent dst_fp type by a final loop in ctf_add_type(), below. */
1639 dmd
->dmd_type
= type
;
1640 dmd
->dmd_offset
= offset
;
1641 dmd
->dmd_value
= -1;
1643 ctf_list_append (&ctb
->ctb_dtd
->dtd_u
.dtu_members
, dmd
);
1645 ctb
->ctb_file
->ctf_flags
|= LCTF_DIRTY
;
1649 /* The ctf_add_type routine is used to copy a type from a source CTF container
1650 to a dynamic destination container. This routine operates recursively by
1651 following the source type's links and embedded member types. If the
1652 destination container already contains a named type which has the same
1653 attributes, then we succeed and return this type but no changes occur. */
1655 ctf_add_type_internal (ctf_file_t
*dst_fp
, ctf_file_t
*src_fp
, ctf_id_t src_type
,
1656 ctf_file_t
*proc_tracking_fp
)
1658 ctf_id_t dst_type
= CTF_ERR
;
1659 uint32_t dst_kind
= CTF_K_UNKNOWN
;
1660 ctf_file_t
*tmp_fp
= dst_fp
;
1664 uint32_t kind
, forward_kind
, flag
, vlen
;
1666 const ctf_type_t
*src_tp
, *dst_tp
;
1667 ctf_bundle_t src
, dst
;
1668 ctf_encoding_t src_en
, dst_en
;
1669 ctf_arinfo_t src_ar
, dst_ar
;
1673 ctf_id_t orig_src_type
= src_type
;
1675 if (!(dst_fp
->ctf_flags
& LCTF_RDWR
))
1676 return (ctf_set_errno (dst_fp
, ECTF_RDONLY
));
1678 if ((src_tp
= ctf_lookup_by_id (&src_fp
, src_type
)) == NULL
)
1679 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1681 if ((ctf_type_resolve (src_fp
, src_type
) == CTF_ERR
)
1682 && (ctf_errno (src_fp
) == ECTF_NONREPRESENTABLE
))
1683 return (ctf_set_errno (dst_fp
, ECTF_NONREPRESENTABLE
));
1685 name
= ctf_strptr (src_fp
, src_tp
->ctt_name
);
1686 kind
= LCTF_INFO_KIND (src_fp
, src_tp
->ctt_info
);
1687 flag
= LCTF_INFO_ISROOT (src_fp
, src_tp
->ctt_info
);
1688 vlen
= LCTF_INFO_VLEN (src_fp
, src_tp
->ctt_info
);
1690 /* If this is a type we are currently in the middle of adding, hand it
1691 straight back. (This lets us handle self-referential structures without
1692 considering forwards and empty structures the same as their completed
1695 tmp
= ctf_type_mapping (src_fp
, src_type
, &tmp_fp
);
1699 if (ctf_dynhash_lookup (proc_tracking_fp
->ctf_add_processing
,
1700 (void *) (uintptr_t) src_type
))
1703 /* If this type has already been added from this container, and is the same
1704 kind and (if a struct or union) has the same number of members, hand it
1707 if (ctf_type_kind_unsliced (tmp_fp
, tmp
) == (int) kind
)
1709 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
1710 || kind
== CTF_K_ENUM
)
1712 if ((dst_tp
= ctf_lookup_by_id (&tmp_fp
, dst_type
)) != NULL
)
1713 if (vlen
== LCTF_INFO_VLEN (tmp_fp
, dst_tp
->ctt_info
))
1721 forward_kind
= kind
;
1722 if (kind
== CTF_K_FORWARD
)
1723 forward_kind
= src_tp
->ctt_type
;
1725 /* If the source type has a name and is a root type (visible at the
1726 top-level scope), lookup the name in the destination container and
1727 verify that it is of the same kind before we do anything else. */
1729 if ((flag
& CTF_ADD_ROOT
) && name
[0] != '\0'
1730 && (tmp
= ctf_lookup_by_rawname (dst_fp
, forward_kind
, name
)) != 0)
1733 dst_kind
= ctf_type_kind_unsliced (dst_fp
, dst_type
);
1736 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
1737 unless dst_type is a forward declaration and src_type is a struct,
1738 union, or enum (i.e. the definition of the previous forward decl).
1740 We also allow addition in the opposite order (addition of a forward when a
1741 struct, union, or enum already exists), which is a NOP and returns the
1742 already-present struct, union, or enum. */
1744 if (dst_type
!= CTF_ERR
&& dst_kind
!= kind
)
1746 if (kind
== CTF_K_FORWARD
1747 && (dst_kind
== CTF_K_ENUM
|| dst_kind
== CTF_K_STRUCT
1748 || dst_kind
== CTF_K_UNION
))
1750 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1754 if (dst_kind
!= CTF_K_FORWARD
1755 || (kind
!= CTF_K_ENUM
&& kind
!= CTF_K_STRUCT
1756 && kind
!= CTF_K_UNION
))
1758 ctf_dprintf ("Conflict for type %s: kinds differ, new: %i; "
1759 "old (ID %lx): %i\n", name
, kind
, dst_type
, dst_kind
);
1760 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1764 /* We take special action for an integer, float, or slice since it is
1765 described not only by its name but also its encoding. For integers,
1766 bit-fields exploit this degeneracy. */
1768 if (kind
== CTF_K_INTEGER
|| kind
== CTF_K_FLOAT
|| kind
== CTF_K_SLICE
)
1770 if (ctf_type_encoding (src_fp
, src_type
, &src_en
) != 0)
1771 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1773 if (dst_type
!= CTF_ERR
)
1775 ctf_file_t
*fp
= dst_fp
;
1777 if ((dst_tp
= ctf_lookup_by_id (&fp
, dst_type
)) == NULL
)
1780 if (ctf_type_encoding (dst_fp
, dst_type
, &dst_en
) != 0)
1781 return CTF_ERR
; /* errno set for us. */
1783 if (LCTF_INFO_ISROOT (fp
, dst_tp
->ctt_info
) & CTF_ADD_ROOT
)
1785 /* The type that we found in the hash is also root-visible. If
1786 the two types match then use the existing one; otherwise,
1787 declare a conflict. Note: slices are not certain to match
1788 even if there is no conflict: we must check the contained type
1791 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1793 if (kind
!= CTF_K_SLICE
)
1795 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1801 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1806 /* We found a non-root-visible type in the hash. If its encoding
1807 is the same, we can reuse it, unless it is a slice. */
1809 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1811 if (kind
!= CTF_K_SLICE
)
1813 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1821 src
.ctb_file
= src_fp
;
1822 src
.ctb_type
= src_type
;
1825 dst
.ctb_file
= dst_fp
;
1826 dst
.ctb_type
= dst_type
;
1829 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add
1830 a new type with the same properties as src_type to dst_fp. If dst_type is
1831 not CTF_ERR, then we verify that dst_type has the same attributes as
1832 src_type. We recurse for embedded references. Before we start, we note
1833 that we are processing this type, to prevent infinite recursion: we do not
1834 re-process any type that appears in this list. The list is emptied
1835 wholesale at the end of processing everything in this recursive stack. */
1837 if (ctf_dynhash_insert (proc_tracking_fp
->ctf_add_processing
,
1838 (void *) (uintptr_t) src_type
, (void *) 1) < 0)
1839 return ctf_set_errno (dst_fp
, ENOMEM
);
1844 /* If we found a match we will have either returned it or declared a
1846 dst_type
= ctf_add_integer (dst_fp
, flag
, name
, &src_en
);
1850 /* If we found a match we will have either returned it or declared a
1852 dst_type
= ctf_add_float (dst_fp
, flag
, name
, &src_en
);
1856 /* We have checked for conflicting encodings: now try to add the
1858 src_type
= ctf_type_reference (src_fp
, src_type
);
1859 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1862 if (src_type
== CTF_ERR
)
1863 return CTF_ERR
; /* errno is set for us. */
1865 dst_type
= ctf_add_slice (dst_fp
, flag
, src_type
, &src_en
);
1869 case CTF_K_VOLATILE
:
1871 case CTF_K_RESTRICT
:
1872 src_type
= ctf_type_reference (src_fp
, src_type
);
1873 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1876 if (src_type
== CTF_ERR
)
1877 return CTF_ERR
; /* errno is set for us. */
1879 dst_type
= ctf_add_reftype (dst_fp
, flag
, src_type
, kind
);
1883 if (ctf_array_info (src_fp
, src_type
, &src_ar
) != 0)
1884 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1886 src_ar
.ctr_contents
=
1887 ctf_add_type_internal (dst_fp
, src_fp
, src_ar
.ctr_contents
,
1889 src_ar
.ctr_index
= ctf_add_type_internal (dst_fp
, src_fp
,
1892 src_ar
.ctr_nelems
= src_ar
.ctr_nelems
;
1894 if (src_ar
.ctr_contents
== CTF_ERR
|| src_ar
.ctr_index
== CTF_ERR
)
1895 return CTF_ERR
; /* errno is set for us. */
1897 if (dst_type
!= CTF_ERR
)
1899 if (ctf_array_info (dst_fp
, dst_type
, &dst_ar
) != 0)
1900 return CTF_ERR
; /* errno is set for us. */
1902 if (memcmp (&src_ar
, &dst_ar
, sizeof (ctf_arinfo_t
)))
1904 ctf_dprintf ("Conflict for type %s against ID %lx: "
1905 "array info differs, old %lx/%lx/%x; "
1906 "new: %lx/%lx/%x\n", name
, dst_type
,
1907 src_ar
.ctr_contents
, src_ar
.ctr_index
,
1908 src_ar
.ctr_nelems
, dst_ar
.ctr_contents
,
1909 dst_ar
.ctr_index
, dst_ar
.ctr_nelems
);
1910 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1914 dst_type
= ctf_add_array (dst_fp
, flag
, &src_ar
);
1917 case CTF_K_FUNCTION
:
1918 ctc
.ctc_return
= ctf_add_type_internal (dst_fp
, src_fp
,
1924 if (ctc
.ctc_return
== CTF_ERR
)
1925 return CTF_ERR
; /* errno is set for us. */
1927 dst_type
= ctf_add_function (dst_fp
, flag
, &ctc
, NULL
);
1939 /* Technically to match a struct or union we need to check both
1940 ways (src members vs. dst, dst members vs. src) but we make
1941 this more optimal by only checking src vs. dst and comparing
1942 the total size of the structure (which we must do anyway)
1943 which covers the possibility of dst members not in src.
1944 This optimization can be defeated for unions, but is so
1945 pathological as to render it irrelevant for our purposes. */
1947 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
1948 && dst_kind
!= CTF_K_FORWARD
)
1950 if (ctf_type_size (src_fp
, src_type
) !=
1951 ctf_type_size (dst_fp
, dst_type
))
1953 ctf_dprintf ("Conflict for type %s against ID %lx: "
1954 "union size differs, old %li, new %li\n",
1956 (long) ctf_type_size (src_fp
, src_type
),
1957 (long) ctf_type_size (dst_fp
, dst_type
));
1958 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1961 if (ctf_member_iter (src_fp
, src_type
, membcmp
, &dst
))
1963 ctf_dprintf ("Conflict for type %s against ID %lx: "
1964 "members differ, see above\n", name
, dst_type
);
1965 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1971 /* Unlike the other cases, copying structs and unions is done
1972 manually so as to avoid repeated lookups in ctf_add_member
1973 and to ensure the exact same member offsets as in src_type. */
1975 dst_type
= ctf_add_generic (dst_fp
, flag
, name
, kind
, &dtd
);
1976 if (dst_type
== CTF_ERR
)
1977 return CTF_ERR
; /* errno is set for us. */
1979 dst
.ctb_type
= dst_type
;
1982 /* Pre-emptively add this struct to the type mapping so that
1983 structures that refer to themselves work. */
1984 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1986 if (ctf_member_iter (src_fp
, src_type
, membadd
, &dst
) != 0)
1987 errs
++; /* Increment errs and fail at bottom of case. */
1989 if ((ssize
= ctf_type_size (src_fp
, src_type
)) < 0)
1990 return CTF_ERR
; /* errno is set for us. */
1992 size
= (size_t) ssize
;
1993 if (size
> CTF_MAX_SIZE
)
1995 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1996 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1997 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
2000 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
2002 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, vlen
);
2004 /* Make a final pass through the members changing each dmd_type (a
2005 src_fp type) to an equivalent type in dst_fp. We pass through all
2006 members, leaving any that fail set to CTF_ERR, unless they fail
2007 because they are marking a member of type not representable in this
2008 version of CTF, in which case we just want to silently omit them:
2009 no consumer can do anything with them anyway. */
2010 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
2011 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
2013 ctf_file_t
*dst
= dst_fp
;
2016 memb_type
= ctf_type_mapping (src_fp
, dmd
->dmd_type
, &dst
);
2019 if ((dmd
->dmd_type
=
2020 ctf_add_type_internal (dst_fp
, src_fp
, dmd
->dmd_type
,
2021 proc_tracking_fp
)) == CTF_ERR
)
2023 if (ctf_errno (dst_fp
) != ECTF_NONREPRESENTABLE
)
2028 dmd
->dmd_type
= memb_type
;
2032 return CTF_ERR
; /* errno is set for us. */
2037 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
2038 && dst_kind
!= CTF_K_FORWARD
)
2040 if (ctf_enum_iter (src_fp
, src_type
, enumcmp
, &dst
)
2041 || ctf_enum_iter (dst_fp
, dst_type
, enumcmp
, &src
))
2043 ctf_dprintf ("Conflict for enum %s against ID %lx: "
2044 "members differ, see above\n", name
, dst_type
);
2045 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2050 dst_type
= ctf_add_enum (dst_fp
, flag
, name
);
2051 if ((dst
.ctb_type
= dst_type
) == CTF_ERR
2052 || ctf_enum_iter (src_fp
, src_type
, enumadd
, &dst
))
2053 return CTF_ERR
; /* errno is set for us */
2058 if (dst_type
== CTF_ERR
)
2059 dst_type
= ctf_add_forward (dst_fp
, flag
, name
, forward_kind
);
2063 src_type
= ctf_type_reference (src_fp
, src_type
);
2064 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
2067 if (src_type
== CTF_ERR
)
2068 return CTF_ERR
; /* errno is set for us. */
2070 /* If dst_type is not CTF_ERR at this point, we should check if
2071 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
2072 ECTF_CONFLICT. However, this causes problems with bitness typedefs
2073 that vary based on things like if 32-bit then pid_t is int otherwise
2074 long. We therefore omit this check and assume that if the identically
2075 named typedef already exists in dst_fp, it is correct or
2078 if (dst_type
== CTF_ERR
)
2079 dst_type
= ctf_add_typedef (dst_fp
, flag
, name
, src_type
);
2084 return (ctf_set_errno (dst_fp
, ECTF_CORRUPT
));
2087 if (dst_type
!= CTF_ERR
)
2088 ctf_add_type_mapping (src_fp
, orig_src_type
, dst_fp
, dst_type
);
2093 ctf_add_type (ctf_file_t
*dst_fp
, ctf_file_t
*src_fp
, ctf_id_t src_type
)
2097 if (!src_fp
->ctf_add_processing
)
2098 src_fp
->ctf_add_processing
= ctf_dynhash_create (ctf_hash_integer
,
2099 ctf_hash_eq_integer
,
2102 /* We store the hash on the source, because it contains only source type IDs:
2103 but callers will invariably expect errors to appear on the dest. */
2104 if (!src_fp
->ctf_add_processing
)
2105 return (ctf_set_errno (dst_fp
, ENOMEM
));
2107 id
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
, src_fp
);
2108 ctf_dynhash_empty (src_fp
->ctf_add_processing
);
2113 /* Write the compressed CTF data stream to the specified gzFile descriptor. */
2115 ctf_gzwrite (ctf_file_t
*fp
, gzFile fd
)
2117 const unsigned char *buf
;
2121 resid
= sizeof (ctf_header_t
);
2122 buf
= (unsigned char *) fp
->ctf_header
;
2125 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
2126 return (ctf_set_errno (fp
, errno
));
2131 resid
= fp
->ctf_size
;
2135 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
2136 return (ctf_set_errno (fp
, errno
));
2144 /* Compress the specified CTF data stream and write it to the specified file
2147 ctf_compress_write (ctf_file_t
*fp
, int fd
)
2152 ctf_header_t
*hp
= &h
;
2153 ssize_t header_len
= sizeof (ctf_header_t
);
2154 ssize_t compress_len
;
2159 if (ctf_serialize (fp
) < 0)
2160 return -1; /* errno is set for us. */
2162 memcpy (hp
, fp
->ctf_header
, header_len
);
2163 hp
->cth_flags
|= CTF_F_COMPRESS
;
2164 compress_len
= compressBound (fp
->ctf_size
);
2166 if ((buf
= malloc (compress_len
)) == NULL
)
2167 return (ctf_set_errno (fp
, ECTF_ZALLOC
));
2169 if ((rc
= compress (buf
, (uLongf
*) &compress_len
,
2170 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
2172 ctf_dprintf ("zlib deflate err: %s\n", zError (rc
));
2173 err
= ctf_set_errno (fp
, ECTF_COMPRESS
);
2177 while (header_len
> 0)
2179 if ((len
= write (fd
, hp
, header_len
)) < 0)
2181 err
= ctf_set_errno (fp
, errno
);
2189 while (compress_len
> 0)
2191 if ((len
= write (fd
, bp
, compress_len
)) < 0)
2193 err
= ctf_set_errno (fp
, errno
);
2196 compress_len
-= len
;
2205 /* Optionally compress the specified CTF data stream and return it as a new
2206 dynamically-allocated string. */
2208 ctf_write_mem (ctf_file_t
*fp
, size_t *size
, size_t threshold
)
2213 ssize_t header_len
= sizeof (ctf_header_t
);
2214 ssize_t compress_len
;
2217 if (ctf_serialize (fp
) < 0)
2218 return NULL
; /* errno is set for us. */
2220 compress_len
= compressBound (fp
->ctf_size
);
2221 if (fp
->ctf_size
< threshold
)
2222 compress_len
= fp
->ctf_size
;
2223 if ((buf
= malloc (compress_len
2224 + sizeof (struct ctf_header
))) == NULL
)
2226 ctf_set_errno (fp
, ENOMEM
);
2230 hp
= (ctf_header_t
*) buf
;
2231 memcpy (hp
, fp
->ctf_header
, header_len
);
2232 bp
= buf
+ sizeof (struct ctf_header
);
2233 *size
= sizeof (struct ctf_header
);
2235 if (fp
->ctf_size
< threshold
)
2237 hp
->cth_flags
&= ~CTF_F_COMPRESS
;
2238 memcpy (bp
, fp
->ctf_buf
, fp
->ctf_size
);
2239 *size
+= fp
->ctf_size
;
2243 hp
->cth_flags
|= CTF_F_COMPRESS
;
2244 if ((rc
= compress (bp
, (uLongf
*) &compress_len
,
2245 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
2247 ctf_dprintf ("zlib deflate err: %s\n", zError (rc
));
2248 ctf_set_errno (fp
, ECTF_COMPRESS
);
2252 *size
+= compress_len
;
2257 /* Write the uncompressed CTF data stream to the specified file descriptor. */
2259 ctf_write (ctf_file_t
*fp
, int fd
)
2261 const unsigned char *buf
;
2265 if (ctf_serialize (fp
) < 0)
2266 return -1; /* errno is set for us. */
2268 resid
= sizeof (ctf_header_t
);
2269 buf
= (unsigned char *) fp
->ctf_header
;
2272 if ((len
= write (fd
, buf
, resid
)) <= 0)
2273 return (ctf_set_errno (fp
, errno
));
2278 resid
= fp
->ctf_size
;
2282 if ((len
= write (fd
, buf
, resid
)) <= 0)
2283 return (ctf_set_errno (fp
, errno
));