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;
129 fp
->ctf_flags
|= LCTF_DIRTY
;
131 ctf_set_ctl_hashes (fp
);
132 ctf_setmodel (fp
, CTF_MODEL_NATIVE
);
133 if (ctf_grow_ptrtab (fp
) < 0)
135 ctf_set_open_errno (errp
, ctf_errno (fp
));
143 ctf_dynhash_destroy (structs
);
144 ctf_dynhash_destroy (unions
);
145 ctf_dynhash_destroy (enums
);
146 ctf_dynhash_destroy (names
);
147 ctf_dynhash_destroy (dvhash
);
149 ctf_dynhash_destroy (dthash
);
154 static unsigned char *
155 ctf_copy_smembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
157 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
160 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
162 ctf_member_t
*copied
;
165 ctm
.ctm_type
= (uint32_t) dmd
->dmd_type
;
166 ctm
.ctm_offset
= (uint32_t) dmd
->dmd_offset
;
168 memcpy (t
, &ctm
, sizeof (ctm
));
169 copied
= (ctf_member_t
*) t
;
171 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctm_name
);
179 static unsigned char *
180 ctf_copy_lmembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
182 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
185 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
187 ctf_lmember_t
*copied
;
190 ctlm
.ctlm_type
= (uint32_t) dmd
->dmd_type
;
191 ctlm
.ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI (dmd
->dmd_offset
);
192 ctlm
.ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO (dmd
->dmd_offset
);
194 memcpy (t
, &ctlm
, sizeof (ctlm
));
195 copied
= (ctf_lmember_t
*) t
;
197 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctlm_name
);
205 static unsigned char *
206 ctf_copy_emembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
208 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
211 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
215 cte
.cte_value
= dmd
->dmd_value
;
216 memcpy (t
, &cte
, sizeof (cte
));
217 copied
= (ctf_enum_t
*) t
;
218 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->cte_name
);
225 /* Sort a newly-constructed static variable array. */
227 typedef struct ctf_sort_var_arg_cb
231 } ctf_sort_var_arg_cb_t
;
234 ctf_sort_var (const void *one_
, const void *two_
, void *arg_
)
236 const ctf_varent_t
*one
= one_
;
237 const ctf_varent_t
*two
= two_
;
238 ctf_sort_var_arg_cb_t
*arg
= arg_
;
240 return (strcmp (ctf_strraw_explicit (arg
->fp
, one
->ctv_name
, arg
->strtab
),
241 ctf_strraw_explicit (arg
->fp
, two
->ctv_name
, arg
->strtab
)));
244 /* Compatibility: just update the threshold for ctf_discard. */
246 ctf_update (ctf_file_t
*fp
)
248 if (!(fp
->ctf_flags
& LCTF_RDWR
))
249 return (ctf_set_errno (fp
, ECTF_RDONLY
));
251 fp
->ctf_dtoldid
= fp
->ctf_typemax
;
255 /* If the specified CTF container is writable and has been modified, reload this
256 container with the updated type definitions, ready for serialization. In
257 order to make this code and the rest of libctf as simple as possible, we
258 perform updates by taking the dynamic type definitions and creating an
259 in-memory CTF file containing the definitions, and then call
260 ctf_simple_open_internal() on it. We perform one extra trick here for the
261 benefit of callers and to keep our code simple: ctf_simple_open_internal()
262 will return a new ctf_file_t, but we want to keep the fp constant for the
263 caller, so after ctf_simple_open_internal() returns, we use memcpy to swap
264 the interior of the old and new ctf_file_t's, and then free the old. */
266 ctf_serialize (ctf_file_t
*fp
)
268 ctf_file_t ofp
, *nfp
;
269 ctf_header_t hdr
, *hdrp
;
272 ctf_varent_t
*dvarents
;
273 ctf_strs_writable_t strtab
;
277 size_t buf_size
, type_size
, nvars
;
278 unsigned char *buf
, *newbuf
;
281 if (!(fp
->ctf_flags
& LCTF_RDWR
))
282 return (ctf_set_errno (fp
, ECTF_RDONLY
));
284 /* Update required? */
285 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
288 /* Fill in an initial CTF header. We will leave the label, object,
289 and function sections empty and only output a header, type section,
290 and string table. The type section begins at a 4-byte aligned
291 boundary past the CTF header itself (at relative offset zero). */
293 memset (&hdr
, 0, sizeof (hdr
));
294 hdr
.cth_magic
= CTF_MAGIC
;
295 hdr
.cth_version
= CTF_VERSION
;
297 /* Iterate through the dynamic type definition list and compute the
298 size of the CTF type section we will need to generate. */
300 for (type_size
= 0, dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
301 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
303 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
304 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
306 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
307 type_size
+= sizeof (ctf_stype_t
);
309 type_size
+= sizeof (ctf_type_t
);
315 type_size
+= sizeof (uint32_t);
318 type_size
+= sizeof (ctf_array_t
);
321 type_size
+= sizeof (ctf_slice_t
);
324 type_size
+= sizeof (uint32_t) * (vlen
+ (vlen
& 1));
328 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
329 type_size
+= sizeof (ctf_member_t
) * vlen
;
331 type_size
+= sizeof (ctf_lmember_t
) * vlen
;
334 type_size
+= sizeof (ctf_enum_t
) * vlen
;
339 /* Computing the number of entries in the CTF variable section is much
342 for (nvars
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
);
343 dvd
!= NULL
; dvd
= ctf_list_next (dvd
), nvars
++);
345 /* Compute the size of the CTF buffer we need, sans only the string table,
346 then allocate a new buffer and memcpy the finished header to the start of
347 the buffer. (We will adjust this later with strtab length info.) */
349 hdr
.cth_typeoff
= hdr
.cth_varoff
+ (nvars
* sizeof (ctf_varent_t
));
350 hdr
.cth_stroff
= hdr
.cth_typeoff
+ type_size
;
353 buf_size
= sizeof (ctf_header_t
) + hdr
.cth_stroff
+ hdr
.cth_strlen
;
355 if ((buf
= malloc (buf_size
)) == NULL
)
356 return (ctf_set_errno (fp
, EAGAIN
));
358 memcpy (buf
, &hdr
, sizeof (ctf_header_t
));
359 t
= (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_varoff
;
361 hdrp
= (ctf_header_t
*) buf
;
362 if ((fp
->ctf_flags
& LCTF_CHILD
) && (fp
->ctf_parname
!= NULL
))
363 ctf_str_add_ref (fp
, fp
->ctf_parname
, &hdrp
->cth_parname
);
364 if (fp
->ctf_cuname
!= NULL
)
365 ctf_str_add_ref (fp
, fp
->ctf_cuname
, &hdrp
->cth_cuname
);
367 /* Work over the variable list, translating everything into ctf_varent_t's and
368 prepping the string table. */
370 dvarents
= (ctf_varent_t
*) t
;
371 for (i
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
;
372 dvd
= ctf_list_next (dvd
), i
++)
374 ctf_varent_t
*var
= &dvarents
[i
];
376 ctf_str_add_ref (fp
, dvd
->dvd_name
, &var
->ctv_name
);
377 var
->ctv_type
= (uint32_t) dvd
->dvd_type
;
381 t
+= sizeof (ctf_varent_t
) * nvars
;
383 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_typeoff
);
385 /* We now take a final lap through the dynamic type definition list and copy
386 the appropriate type records to the output buffer, noting down the
389 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
390 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
392 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
393 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
401 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
402 len
= sizeof (ctf_stype_t
);
404 len
= sizeof (ctf_type_t
);
406 memcpy (t
, &dtd
->dtd_data
, len
);
407 copied
= (ctf_stype_t
*) t
; /* name is at the start: constant offset. */
409 && (name
= ctf_strraw (fp
, copied
->ctt_name
)) != NULL
)
410 ctf_str_add_ref (fp
, name
, &copied
->ctt_name
);
417 if (kind
== CTF_K_INTEGER
)
419 encoding
= CTF_INT_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
420 dtd
->dtd_u
.dtu_enc
.cte_offset
,
421 dtd
->dtd_u
.dtu_enc
.cte_bits
);
425 encoding
= CTF_FP_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
426 dtd
->dtd_u
.dtu_enc
.cte_offset
,
427 dtd
->dtd_u
.dtu_enc
.cte_bits
);
429 memcpy (t
, &encoding
, sizeof (encoding
));
430 t
+= sizeof (encoding
);
434 memcpy (t
, &dtd
->dtd_u
.dtu_slice
, sizeof (struct ctf_slice
));
435 t
+= sizeof (struct ctf_slice
);
439 cta
.cta_contents
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_contents
;
440 cta
.cta_index
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_index
;
441 cta
.cta_nelems
= dtd
->dtd_u
.dtu_arr
.ctr_nelems
;
442 memcpy (t
, &cta
, sizeof (cta
));
448 uint32_t *argv
= (uint32_t *) (uintptr_t) t
;
451 for (argc
= 0; argc
< vlen
; argc
++)
452 *argv
++ = dtd
->dtd_u
.dtu_argv
[argc
];
455 *argv
++ = 0; /* Pad to 4-byte boundary. */
457 t
= (unsigned char *) argv
;
463 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
464 t
= ctf_copy_smembers (fp
, dtd
, t
);
466 t
= ctf_copy_lmembers (fp
, dtd
, t
);
470 t
= ctf_copy_emembers (fp
, dtd
, t
);
474 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_stroff
);
476 /* Construct the final string table and fill out all the string refs with the
477 final offsets. Then purge the refs list, because we're about to move this
478 strtab onto the end of the buf, invalidating all the offsets. */
479 strtab
= ctf_str_write_strtab (fp
);
480 ctf_str_purge_refs (fp
);
482 if (strtab
.cts_strs
== NULL
)
485 return (ctf_set_errno (fp
, EAGAIN
));
488 /* Now the string table is constructed, we can sort the buffer of
490 ctf_sort_var_arg_cb_t sort_var_arg
= { fp
, (ctf_strs_t
*) &strtab
};
491 ctf_qsort_r (dvarents
, nvars
, sizeof (ctf_varent_t
), ctf_sort_var
,
494 if ((newbuf
= ctf_realloc (fp
, buf
, buf_size
+ strtab
.cts_len
)) == NULL
)
497 free (strtab
.cts_strs
);
498 return (ctf_set_errno (fp
, EAGAIN
));
501 memcpy (buf
+ buf_size
, strtab
.cts_strs
, strtab
.cts_len
);
502 hdrp
= (ctf_header_t
*) buf
;
503 hdrp
->cth_strlen
= strtab
.cts_len
;
504 buf_size
+= hdrp
->cth_strlen
;
505 free (strtab
.cts_strs
);
507 /* Finally, we are ready to ctf_simple_open() the new container. If this
508 is successful, we then switch nfp and fp and free the old container. */
510 if ((nfp
= ctf_simple_open_internal ((char *) buf
, buf_size
, NULL
, 0,
511 0, NULL
, 0, fp
->ctf_syn_ext_strtab
,
515 return (ctf_set_errno (fp
, err
));
518 (void) ctf_setmodel (nfp
, ctf_getmodel (fp
));
520 nfp
->ctf_parent
= fp
->ctf_parent
;
521 nfp
->ctf_parent_unreffed
= fp
->ctf_parent_unreffed
;
522 nfp
->ctf_refcnt
= fp
->ctf_refcnt
;
523 nfp
->ctf_flags
|= fp
->ctf_flags
& ~LCTF_DIRTY
;
524 if (nfp
->ctf_dynbase
== NULL
)
525 nfp
->ctf_dynbase
= buf
; /* Make sure buf is freed on close. */
526 nfp
->ctf_dthash
= fp
->ctf_dthash
;
527 nfp
->ctf_dtdefs
= fp
->ctf_dtdefs
;
528 nfp
->ctf_dvhash
= fp
->ctf_dvhash
;
529 nfp
->ctf_dvdefs
= fp
->ctf_dvdefs
;
530 nfp
->ctf_dtoldid
= fp
->ctf_dtoldid
;
531 nfp
->ctf_add_processing
= fp
->ctf_add_processing
;
532 nfp
->ctf_snapshots
= fp
->ctf_snapshots
+ 1;
533 nfp
->ctf_specific
= fp
->ctf_specific
;
534 nfp
->ctf_ptrtab
= fp
->ctf_ptrtab
;
535 nfp
->ctf_ptrtab_len
= fp
->ctf_ptrtab_len
;
536 nfp
->ctf_link_inputs
= fp
->ctf_link_inputs
;
537 nfp
->ctf_link_outputs
= fp
->ctf_link_outputs
;
538 nfp
->ctf_errs_warnings
= fp
->ctf_errs_warnings
;
539 nfp
->ctf_str_prov_offset
= fp
->ctf_str_prov_offset
;
540 nfp
->ctf_syn_ext_strtab
= fp
->ctf_syn_ext_strtab
;
541 nfp
->ctf_link_in_cu_mapping
= fp
->ctf_link_in_cu_mapping
;
542 nfp
->ctf_link_out_cu_mapping
= fp
->ctf_link_out_cu_mapping
;
543 nfp
->ctf_link_type_mapping
= fp
->ctf_link_type_mapping
;
544 nfp
->ctf_link_memb_name_changer
= fp
->ctf_link_memb_name_changer
;
545 nfp
->ctf_link_memb_name_changer_arg
= fp
->ctf_link_memb_name_changer_arg
;
546 nfp
->ctf_link_variable_filter
= fp
->ctf_link_variable_filter
;
547 nfp
->ctf_link_variable_filter_arg
= fp
->ctf_link_variable_filter_arg
;
548 nfp
->ctf_link_flags
= fp
->ctf_link_flags
;
549 nfp
->ctf_dedup_atoms
= fp
->ctf_dedup_atoms
;
550 nfp
->ctf_dedup_atoms_alloc
= fp
->ctf_dedup_atoms_alloc
;
551 memcpy (&nfp
->ctf_dedup
, &fp
->ctf_dedup
, sizeof (fp
->ctf_dedup
));
553 nfp
->ctf_snapshot_lu
= fp
->ctf_snapshots
;
555 memcpy (&nfp
->ctf_lookups
, fp
->ctf_lookups
, sizeof (fp
->ctf_lookups
));
556 nfp
->ctf_structs
= fp
->ctf_structs
;
557 nfp
->ctf_unions
= fp
->ctf_unions
;
558 nfp
->ctf_enums
= fp
->ctf_enums
;
559 nfp
->ctf_names
= fp
->ctf_names
;
561 fp
->ctf_dthash
= NULL
;
562 ctf_str_free_atoms (nfp
);
563 nfp
->ctf_str_atoms
= fp
->ctf_str_atoms
;
564 nfp
->ctf_prov_strtab
= fp
->ctf_prov_strtab
;
565 fp
->ctf_str_atoms
= NULL
;
566 fp
->ctf_prov_strtab
= NULL
;
567 memset (&fp
->ctf_dtdefs
, 0, sizeof (ctf_list_t
));
568 memset (&fp
->ctf_errs_warnings
, 0, sizeof (ctf_list_t
));
569 fp
->ctf_add_processing
= NULL
;
570 fp
->ctf_ptrtab
= NULL
;
571 fp
->ctf_link_inputs
= NULL
;
572 fp
->ctf_link_outputs
= NULL
;
573 fp
->ctf_syn_ext_strtab
= NULL
;
574 fp
->ctf_link_in_cu_mapping
= NULL
;
575 fp
->ctf_link_out_cu_mapping
= NULL
;
576 fp
->ctf_link_type_mapping
= NULL
;
577 fp
->ctf_dedup_atoms
= NULL
;
578 fp
->ctf_dedup_atoms_alloc
= NULL
;
579 fp
->ctf_parent_unreffed
= 1;
581 fp
->ctf_dvhash
= NULL
;
582 memset (&fp
->ctf_dvdefs
, 0, sizeof (ctf_list_t
));
583 memset (fp
->ctf_lookups
, 0, sizeof (fp
->ctf_lookups
));
584 memset (&fp
->ctf_dedup
, 0, sizeof (fp
->ctf_dedup
));
585 fp
->ctf_structs
.ctn_writable
= NULL
;
586 fp
->ctf_unions
.ctn_writable
= NULL
;
587 fp
->ctf_enums
.ctn_writable
= NULL
;
588 fp
->ctf_names
.ctn_writable
= NULL
;
590 memcpy (&ofp
, fp
, sizeof (ctf_file_t
));
591 memcpy (fp
, nfp
, sizeof (ctf_file_t
));
592 memcpy (nfp
, &ofp
, sizeof (ctf_file_t
));
594 nfp
->ctf_refcnt
= 1; /* Force nfp to be freed. */
595 ctf_file_close (nfp
);
601 ctf_name_table (ctf_file_t
*fp
, int kind
)
606 return &fp
->ctf_structs
;
608 return &fp
->ctf_unions
;
610 return &fp
->ctf_enums
;
612 return &fp
->ctf_names
;
617 ctf_dtd_insert (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, int flag
, int kind
)
620 if (ctf_dynhash_insert (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
,
624 if (flag
== CTF_ADD_ROOT
&& dtd
->dtd_data
.ctt_name
625 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
)
627 if (ctf_dynhash_insert (ctf_name_table (fp
, kind
)->ctn_writable
,
628 (char *) name
, (void *) (uintptr_t)
631 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t)
636 ctf_list_append (&fp
->ctf_dtdefs
, dtd
);
641 ctf_dtd_delete (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
)
643 ctf_dmdef_t
*dmd
, *nmd
;
644 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
645 int name_kind
= kind
;
648 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
);
655 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
656 dmd
!= NULL
; dmd
= nmd
)
658 if (dmd
->dmd_name
!= NULL
)
659 free (dmd
->dmd_name
);
660 nmd
= ctf_list_next (dmd
);
665 free (dtd
->dtd_u
.dtu_argv
);
668 name_kind
= dtd
->dtd_data
.ctt_type
;
672 if (dtd
->dtd_data
.ctt_name
673 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
674 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
676 ctf_dynhash_remove (ctf_name_table (fp
, name_kind
)->ctn_writable
,
678 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
681 ctf_list_delete (&fp
->ctf_dtdefs
, dtd
);
686 ctf_dtd_lookup (const ctf_file_t
*fp
, ctf_id_t type
)
688 return (ctf_dtdef_t
*)
689 ctf_dynhash_lookup (fp
->ctf_dthash
, (void *) (uintptr_t) type
);
693 ctf_dynamic_type (const ctf_file_t
*fp
, ctf_id_t id
)
697 if (!(fp
->ctf_flags
& LCTF_RDWR
))
700 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, id
))
703 idx
= LCTF_TYPE_TO_INDEX(fp
, id
);
705 if ((unsigned long) idx
<= fp
->ctf_typemax
)
706 return ctf_dtd_lookup (fp
, id
);
711 ctf_dvd_insert (ctf_file_t
*fp
, ctf_dvdef_t
*dvd
)
713 if (ctf_dynhash_insert (fp
->ctf_dvhash
, dvd
->dvd_name
, dvd
) < 0)
715 ctf_list_append (&fp
->ctf_dvdefs
, dvd
);
720 ctf_dvd_delete (ctf_file_t
*fp
, ctf_dvdef_t
*dvd
)
722 ctf_dynhash_remove (fp
->ctf_dvhash
, dvd
->dvd_name
);
723 free (dvd
->dvd_name
);
725 ctf_list_delete (&fp
->ctf_dvdefs
, dvd
);
730 ctf_dvd_lookup (const ctf_file_t
*fp
, const char *name
)
732 return (ctf_dvdef_t
*) ctf_dynhash_lookup (fp
->ctf_dvhash
, name
);
735 /* Discard all of the dynamic type definitions and variable definitions that
736 have been added to the container since the last call to ctf_update(). We
737 locate such types by scanning the dtd list and deleting elements that have
738 type IDs greater than ctf_dtoldid, which is set by ctf_update(), above, and
739 by scanning the variable list and deleting elements that have update IDs
740 equal to the current value of the last-update snapshot count (indicating that
741 they were added after the most recent call to ctf_update()). */
743 ctf_discard (ctf_file_t
*fp
)
745 ctf_snapshot_id_t last_update
=
747 fp
->ctf_snapshot_lu
+ 1 };
749 /* Update required? */
750 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
753 return (ctf_rollback (fp
, last_update
));
757 ctf_snapshot (ctf_file_t
*fp
)
759 ctf_snapshot_id_t snapid
;
760 snapid
.dtd_id
= fp
->ctf_typemax
;
761 snapid
.snapshot_id
= fp
->ctf_snapshots
++;
765 /* Like ctf_discard(), only discards everything after a particular ID. */
767 ctf_rollback (ctf_file_t
*fp
, ctf_snapshot_id_t id
)
769 ctf_dtdef_t
*dtd
, *ntd
;
770 ctf_dvdef_t
*dvd
, *nvd
;
772 if (!(fp
->ctf_flags
& LCTF_RDWR
))
773 return (ctf_set_errno (fp
, ECTF_RDONLY
));
775 if (fp
->ctf_snapshot_lu
>= id
.snapshot_id
)
776 return (ctf_set_errno (fp
, ECTF_OVERROLLBACK
));
778 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
); dtd
!= NULL
; dtd
= ntd
)
783 ntd
= ctf_list_next (dtd
);
785 if (LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_type
) <= id
.dtd_id
)
788 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
789 if (kind
== CTF_K_FORWARD
)
790 kind
= dtd
->dtd_data
.ctt_type
;
792 if (dtd
->dtd_data
.ctt_name
793 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
794 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
796 ctf_dynhash_remove (ctf_name_table (fp
, kind
)->ctn_writable
,
798 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
801 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
);
802 ctf_dtd_delete (fp
, dtd
);
805 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
; dvd
= nvd
)
807 nvd
= ctf_list_next (dvd
);
809 if (dvd
->dvd_snapshots
<= id
.snapshot_id
)
812 ctf_dvd_delete (fp
, dvd
);
815 fp
->ctf_typemax
= id
.dtd_id
;
816 fp
->ctf_snapshots
= id
.snapshot_id
;
818 if (fp
->ctf_snapshots
== fp
->ctf_snapshot_lu
)
819 fp
->ctf_flags
&= ~LCTF_DIRTY
;
825 ctf_add_generic (ctf_file_t
*fp
, uint32_t flag
, const char *name
, int kind
,
831 if (flag
!= CTF_ADD_NONROOT
&& flag
!= CTF_ADD_ROOT
)
832 return (ctf_set_errno (fp
, EINVAL
));
834 if (!(fp
->ctf_flags
& LCTF_RDWR
))
835 return (ctf_set_errno (fp
, ECTF_RDONLY
));
837 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) >= CTF_MAX_TYPE
)
838 return (ctf_set_errno (fp
, ECTF_FULL
));
840 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) == (CTF_MAX_PTYPE
- 1))
841 return (ctf_set_errno (fp
, ECTF_FULL
));
843 /* Make sure ptrtab always grows to be big enough for all types. */
844 if (ctf_grow_ptrtab (fp
) < 0)
845 return CTF_ERR
; /* errno is set for us. */
847 if ((dtd
= malloc (sizeof (ctf_dtdef_t
))) == NULL
)
848 return (ctf_set_errno (fp
, EAGAIN
));
850 type
= ++fp
->ctf_typemax
;
851 type
= LCTF_INDEX_TO_TYPE (fp
, type
, (fp
->ctf_flags
& LCTF_CHILD
));
853 memset (dtd
, 0, sizeof (ctf_dtdef_t
));
854 dtd
->dtd_data
.ctt_name
= ctf_str_add_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
855 dtd
->dtd_type
= type
;
857 if (dtd
->dtd_data
.ctt_name
== 0 && name
!= NULL
&& name
[0] != '\0')
860 return (ctf_set_errno (fp
, EAGAIN
));
863 if (ctf_dtd_insert (fp
, dtd
, flag
, kind
) < 0)
866 return CTF_ERR
; /* errno is set for us. */
868 fp
->ctf_flags
|= LCTF_DIRTY
;
874 /* When encoding integer sizes, we want to convert a byte count in the range
875 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
876 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
892 ctf_add_encoded (ctf_file_t
*fp
, uint32_t flag
,
893 const char *name
, const ctf_encoding_t
*ep
, uint32_t kind
)
899 return (ctf_set_errno (fp
, EINVAL
));
901 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, &dtd
)) == CTF_ERR
)
902 return CTF_ERR
; /* errno is set for us. */
904 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
905 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
907 dtd
->dtd_u
.dtu_enc
= *ep
;
913 ctf_add_reftype (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
, uint32_t kind
)
917 ctf_file_t
*tmp
= fp
;
918 int child
= fp
->ctf_flags
& LCTF_CHILD
;
920 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
921 return (ctf_set_errno (fp
, EINVAL
));
923 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
924 return CTF_ERR
; /* errno is set for us. */
926 if ((type
= ctf_add_generic (fp
, flag
, NULL
, kind
, &dtd
)) == CTF_ERR
)
927 return CTF_ERR
; /* errno is set for us. */
929 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
930 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
932 if (kind
!= CTF_K_POINTER
)
935 /* If we are adding a pointer, update the ptrtab, both the directly pointed-to
936 type and (if an anonymous typedef node is being pointed at) the type that
937 points at too. Note that ctf_typemax is at this point one higher than we
938 want to check against, because it's just been incremented for the addition
941 uint32_t type_idx
= LCTF_TYPE_TO_INDEX (fp
, type
);
942 uint32_t ref_idx
= LCTF_TYPE_TO_INDEX (fp
, ref
);
944 if (LCTF_TYPE_ISCHILD (fp
, ref
) == child
945 && ref_idx
< fp
->ctf_typemax
)
947 fp
->ctf_ptrtab
[ref_idx
] = type_idx
;
949 ctf_id_t refref_idx
= LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_data
.ctt_type
);
952 && (LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) == CTF_K_TYPEDEF
)
953 && strcmp (ctf_strptr (fp
, dtd
->dtd_data
.ctt_name
), "") == 0
954 && refref_idx
< fp
->ctf_typemax
)
955 fp
->ctf_ptrtab
[refref_idx
] = type_idx
;
962 ctf_add_slice (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
,
963 const ctf_encoding_t
*ep
)
966 ctf_id_t resolved_ref
= ref
;
969 const ctf_type_t
*tp
;
970 ctf_file_t
*tmp
= fp
;
973 return (ctf_set_errno (fp
, EINVAL
));
975 if ((ep
->cte_bits
> 255) || (ep
->cte_offset
> 255))
976 return (ctf_set_errno (fp
, ECTF_SLICEOVERFLOW
));
978 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
979 return (ctf_set_errno (fp
, EINVAL
));
981 if (ref
!= 0 && ((tp
= ctf_lookup_by_id (&tmp
, ref
)) == NULL
))
982 return CTF_ERR
; /* errno is set for us. */
984 /* Make sure we ultimately point to an integral type. We also allow slices to
985 point to the unimplemented type, for now, because the compiler can emit
986 such slices, though they're not very much use. */
988 resolved_ref
= ctf_type_resolve_unsliced (tmp
, ref
);
989 kind
= ctf_type_kind_unsliced (tmp
, resolved_ref
);
991 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) &&
994 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
996 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_SLICE
, &dtd
)) == CTF_ERR
)
997 return CTF_ERR
; /* errno is set for us. */
999 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_SLICE
, flag
, 0);
1000 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
1002 dtd
->dtd_u
.dtu_slice
.cts_type
= (uint32_t) ref
;
1003 dtd
->dtd_u
.dtu_slice
.cts_bits
= ep
->cte_bits
;
1004 dtd
->dtd_u
.dtu_slice
.cts_offset
= ep
->cte_offset
;
1010 ctf_add_integer (ctf_file_t
*fp
, uint32_t flag
,
1011 const char *name
, const ctf_encoding_t
*ep
)
1013 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_INTEGER
));
1017 ctf_add_float (ctf_file_t
*fp
, uint32_t flag
,
1018 const char *name
, const ctf_encoding_t
*ep
)
1020 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_FLOAT
));
1024 ctf_add_pointer (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1026 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_POINTER
));
1030 ctf_add_array (ctf_file_t
*fp
, uint32_t flag
, const ctf_arinfo_t
*arp
)
1034 ctf_file_t
*tmp
= fp
;
1037 return (ctf_set_errno (fp
, EINVAL
));
1039 if (arp
->ctr_contents
!= 0
1040 && ctf_lookup_by_id (&tmp
, arp
->ctr_contents
) == NULL
)
1041 return CTF_ERR
; /* errno is set for us. */
1044 if (ctf_lookup_by_id (&tmp
, arp
->ctr_index
) == NULL
)
1045 return CTF_ERR
; /* errno is set for us. */
1047 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_ARRAY
, &dtd
)) == CTF_ERR
)
1048 return CTF_ERR
; /* errno is set for us. */
1050 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ARRAY
, flag
, 0);
1051 dtd
->dtd_data
.ctt_size
= 0;
1052 dtd
->dtd_u
.dtu_arr
= *arp
;
1058 ctf_set_array (ctf_file_t
*fp
, ctf_id_t type
, const ctf_arinfo_t
*arp
)
1060 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1062 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1063 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1066 || LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) != CTF_K_ARRAY
)
1067 return (ctf_set_errno (fp
, ECTF_BADID
));
1069 fp
->ctf_flags
|= LCTF_DIRTY
;
1070 dtd
->dtd_u
.dtu_arr
= *arp
;
1076 ctf_add_function (ctf_file_t
*fp
, uint32_t flag
,
1077 const ctf_funcinfo_t
*ctc
, const ctf_id_t
*argv
)
1082 uint32_t *vdat
= NULL
;
1083 ctf_file_t
*tmp
= fp
;
1086 if (ctc
== NULL
|| (ctc
->ctc_flags
& ~CTF_FUNC_VARARG
) != 0
1087 || (ctc
->ctc_argc
!= 0 && argv
== NULL
))
1088 return (ctf_set_errno (fp
, EINVAL
));
1090 vlen
= ctc
->ctc_argc
;
1091 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
1092 vlen
++; /* Add trailing zero to indicate varargs (see below). */
1094 if (ctc
->ctc_return
!= 0
1095 && ctf_lookup_by_id (&tmp
, ctc
->ctc_return
) == NULL
)
1096 return CTF_ERR
; /* errno is set for us. */
1098 if (vlen
> CTF_MAX_VLEN
)
1099 return (ctf_set_errno (fp
, EOVERFLOW
));
1101 if (vlen
!= 0 && (vdat
= malloc (sizeof (ctf_id_t
) * vlen
)) == NULL
)
1102 return (ctf_set_errno (fp
, EAGAIN
));
1104 for (i
= 0; i
< ctc
->ctc_argc
; i
++)
1107 if (argv
[i
] != 0 && ctf_lookup_by_id (&tmp
, argv
[i
]) == NULL
)
1110 return CTF_ERR
; /* errno is set for us. */
1112 vdat
[i
] = (uint32_t) argv
[i
];
1115 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_FUNCTION
,
1119 return CTF_ERR
; /* errno is set for us. */
1122 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FUNCTION
, flag
, vlen
);
1123 dtd
->dtd_data
.ctt_type
= (uint32_t) ctc
->ctc_return
;
1125 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
1126 vdat
[vlen
- 1] = 0; /* Add trailing zero to indicate varargs. */
1127 dtd
->dtd_u
.dtu_argv
= vdat
;
1133 ctf_add_struct_sized (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1139 /* Promote root-visible forwards to structs. */
1141 type
= ctf_lookup_by_rawname (fp
, CTF_K_STRUCT
, name
);
1143 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1144 dtd
= ctf_dtd_lookup (fp
, type
);
1145 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_STRUCT
,
1147 return CTF_ERR
; /* errno is set for us. */
1149 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_STRUCT
, flag
, 0);
1151 if (size
> CTF_MAX_SIZE
)
1153 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1154 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1155 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1158 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1164 ctf_add_struct (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1166 return (ctf_add_struct_sized (fp
, flag
, name
, 0));
1170 ctf_add_union_sized (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1176 /* Promote root-visible forwards to unions. */
1178 type
= ctf_lookup_by_rawname (fp
, CTF_K_UNION
, name
);
1180 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1181 dtd
= ctf_dtd_lookup (fp
, type
);
1182 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_UNION
,
1184 return CTF_ERR
; /* errno is set for us */
1186 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_UNION
, flag
, 0);
1188 if (size
> CTF_MAX_SIZE
)
1190 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1191 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1192 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1195 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1201 ctf_add_union (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1203 return (ctf_add_union_sized (fp
, flag
, name
, 0));
1207 ctf_add_enum (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1212 /* Promote root-visible forwards to enums. */
1214 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
1216 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1217 dtd
= ctf_dtd_lookup (fp
, type
);
1218 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_ENUM
,
1220 return CTF_ERR
; /* errno is set for us. */
1222 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ENUM
, flag
, 0);
1223 dtd
->dtd_data
.ctt_size
= fp
->ctf_dmodel
->ctd_int
;
1229 ctf_add_enum_encoded (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1230 const ctf_encoding_t
*ep
)
1234 /* First, create the enum if need be, using most of the same machinery as
1235 ctf_add_enum(), to ensure that we do not allow things past that are not
1236 enums or forwards to them. (This includes other slices: you cannot slice a
1237 slice, which would be a useless thing to do anyway.) */
1240 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
1244 if ((ctf_type_kind (fp
, type
) != CTF_K_FORWARD
) &&
1245 (ctf_type_kind_unsliced (fp
, type
) != CTF_K_ENUM
))
1246 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1248 else if ((type
= ctf_add_enum (fp
, flag
, name
)) == CTF_ERR
)
1249 return CTF_ERR
; /* errno is set for us. */
1251 /* Now attach a suitable slice to it. */
1253 return ctf_add_slice (fp
, flag
, type
, ep
);
1257 ctf_add_forward (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1263 if (!ctf_forwardable_kind (kind
))
1264 return (ctf_set_errno (fp
, ECTF_NOTSUE
));
1266 /* If the type is already defined or exists as a forward tag, just
1267 return the ctf_id_t of the existing definition. */
1270 type
= ctf_lookup_by_rawname (fp
, kind
, name
);
1275 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, &dtd
)) == CTF_ERR
)
1276 return CTF_ERR
; /* errno is set for us. */
1278 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FORWARD
, flag
, 0);
1279 dtd
->dtd_data
.ctt_type
= kind
;
1285 ctf_add_typedef (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1290 ctf_file_t
*tmp
= fp
;
1292 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
1293 return (ctf_set_errno (fp
, EINVAL
));
1295 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1296 return CTF_ERR
; /* errno is set for us. */
1298 if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_TYPEDEF
,
1300 return CTF_ERR
; /* errno is set for us. */
1302 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_TYPEDEF
, flag
, 0);
1303 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
1309 ctf_add_volatile (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1311 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_VOLATILE
));
1315 ctf_add_const (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1317 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_CONST
));
1321 ctf_add_restrict (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1323 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_RESTRICT
));
1327 ctf_add_enumerator (ctf_file_t
*fp
, ctf_id_t enid
, const char *name
,
1330 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, enid
);
1333 uint32_t kind
, vlen
, root
;
1337 return (ctf_set_errno (fp
, EINVAL
));
1339 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1340 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1343 return (ctf_set_errno (fp
, ECTF_BADID
));
1345 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1346 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1347 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1349 if (kind
!= CTF_K_ENUM
)
1350 return (ctf_set_errno (fp
, ECTF_NOTENUM
));
1352 if (vlen
== CTF_MAX_VLEN
)
1353 return (ctf_set_errno (fp
, ECTF_DTFULL
));
1355 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1356 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1358 if (strcmp (dmd
->dmd_name
, name
) == 0)
1359 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1362 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
1363 return (ctf_set_errno (fp
, EAGAIN
));
1365 if ((s
= strdup (name
)) == NULL
)
1368 return (ctf_set_errno (fp
, EAGAIN
));
1372 dmd
->dmd_type
= CTF_ERR
;
1373 dmd
->dmd_offset
= 0;
1374 dmd
->dmd_value
= value
;
1376 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1377 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
1379 fp
->ctf_flags
|= LCTF_DIRTY
;
1385 ctf_add_member_offset (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1386 ctf_id_t type
, unsigned long bit_offset
)
1388 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, souid
);
1391 ssize_t msize
, malign
, ssize
;
1392 uint32_t kind
, vlen
, root
;
1395 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1396 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1399 return (ctf_set_errno (fp
, ECTF_BADID
));
1401 if (name
!= NULL
&& name
[0] == '\0')
1404 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1405 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1406 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1408 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
1409 return (ctf_set_errno (fp
, ECTF_NOTSOU
));
1411 if (vlen
== CTF_MAX_VLEN
)
1412 return (ctf_set_errno (fp
, ECTF_DTFULL
));
1416 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1417 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1419 if (dmd
->dmd_name
!= NULL
&& strcmp (dmd
->dmd_name
, name
) == 0)
1420 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1424 if ((msize
= ctf_type_size (fp
, type
)) < 0 ||
1425 (malign
= ctf_type_align (fp
, type
)) < 0)
1427 /* The unimplemented type, and any type that resolves to it, has no size
1428 and no alignment: it can correspond to any number of compiler-inserted
1431 if (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
)
1435 ctf_set_errno (fp
, 0);
1438 return -1; /* errno is set for us. */
1441 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
1442 return (ctf_set_errno (fp
, EAGAIN
));
1444 if (name
!= NULL
&& (s
= strdup (name
)) == NULL
)
1447 return (ctf_set_errno (fp
, EAGAIN
));
1451 dmd
->dmd_type
= type
;
1452 dmd
->dmd_value
= -1;
1454 if (kind
== CTF_K_STRUCT
&& vlen
!= 0)
1456 if (bit_offset
== (unsigned long) - 1)
1458 /* Natural alignment. */
1460 ctf_dmdef_t
*lmd
= ctf_list_prev (&dtd
->dtd_u
.dtu_members
);
1461 ctf_id_t ltype
= ctf_type_resolve (fp
, lmd
->dmd_type
);
1462 size_t off
= lmd
->dmd_offset
;
1464 ctf_encoding_t linfo
;
1467 /* Propagate any error from ctf_type_resolve. If the last member was
1468 of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we
1469 cannot insert right after such a member without explicit offset
1470 specification, because its alignment and size is not known. */
1471 if (ltype
== CTF_ERR
)
1474 return -1; /* errno is set for us. */
1477 if (ctf_type_encoding (fp
, ltype
, &linfo
) == 0)
1478 off
+= linfo
.cte_bits
;
1479 else if ((lsize
= ctf_type_size (fp
, ltype
)) > 0)
1480 off
+= lsize
* CHAR_BIT
;
1482 /* Round up the offset of the end of the last member to
1483 the next byte boundary, convert 'off' to bytes, and
1484 then round it up again to the next multiple of the
1485 alignment required by the new member. Finally,
1486 convert back to bits and store the result in
1487 dmd_offset. Technically we could do more efficient
1488 packing if the new member is a bit-field, but we're
1489 the "compiler" and ANSI says we can do as we choose. */
1491 off
= roundup (off
, CHAR_BIT
) / CHAR_BIT
;
1492 off
= roundup (off
, MAX (malign
, 1));
1493 dmd
->dmd_offset
= off
* CHAR_BIT
;
1494 ssize
= off
+ msize
;
1498 /* Specified offset in bits. */
1500 dmd
->dmd_offset
= bit_offset
;
1501 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1502 ssize
= MAX (ssize
, ((signed) bit_offset
/ CHAR_BIT
) + msize
);
1507 dmd
->dmd_offset
= 0;
1508 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1509 ssize
= MAX (ssize
, msize
);
1512 if ((size_t) ssize
> CTF_MAX_SIZE
)
1514 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1515 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (ssize
);
1516 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (ssize
);
1519 dtd
->dtd_data
.ctt_size
= (uint32_t) ssize
;
1521 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1522 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
1524 fp
->ctf_flags
|= LCTF_DIRTY
;
1529 ctf_add_member_encoded (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1530 ctf_id_t type
, unsigned long bit_offset
,
1531 const ctf_encoding_t encoding
)
1533 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1534 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1537 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) && (kind
!= CTF_K_ENUM
))
1538 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1540 if ((type
= ctf_add_slice (fp
, CTF_ADD_NONROOT
, otype
, &encoding
)) == CTF_ERR
)
1541 return -1; /* errno is set for us. */
1543 return ctf_add_member_offset (fp
, souid
, name
, type
, bit_offset
);
1547 ctf_add_member (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1550 return ctf_add_member_offset (fp
, souid
, name
, type
, (unsigned long) - 1);
1554 ctf_add_variable (ctf_file_t
*fp
, const char *name
, ctf_id_t ref
)
1557 ctf_file_t
*tmp
= fp
;
1559 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1560 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1562 if (ctf_dvd_lookup (fp
, name
) != NULL
)
1563 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1565 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1566 return -1; /* errno is set for us. */
1568 /* Make sure this type is representable. */
1569 if ((ctf_type_resolve (fp
, ref
) == CTF_ERR
)
1570 && (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
))
1573 if ((dvd
= malloc (sizeof (ctf_dvdef_t
))) == NULL
)
1574 return (ctf_set_errno (fp
, EAGAIN
));
1576 if (name
!= NULL
&& (dvd
->dvd_name
= strdup (name
)) == NULL
)
1579 return (ctf_set_errno (fp
, EAGAIN
));
1581 dvd
->dvd_type
= ref
;
1582 dvd
->dvd_snapshots
= fp
->ctf_snapshots
;
1584 if (ctf_dvd_insert (fp
, dvd
) < 0)
1586 free (dvd
->dvd_name
);
1588 return -1; /* errno is set for us. */
1591 fp
->ctf_flags
|= LCTF_DIRTY
;
1596 enumcmp (const char *name
, int value
, void *arg
)
1598 ctf_bundle_t
*ctb
= arg
;
1601 if (ctf_enum_value (ctb
->ctb_file
, ctb
->ctb_type
, name
, &bvalue
) < 0)
1603 ctf_dprintf ("Conflict due to member %s iteration error: %s.\n", name
,
1604 ctf_errmsg (ctf_errno (ctb
->ctb_file
)));
1607 if (value
!= bvalue
)
1609 ctf_dprintf ("Conflict due to value change: %i versus %i\n",
1617 enumadd (const char *name
, int value
, void *arg
)
1619 ctf_bundle_t
*ctb
= arg
;
1621 return (ctf_add_enumerator (ctb
->ctb_file
, ctb
->ctb_type
,
1626 membcmp (const char *name
, ctf_id_t type _libctf_unused_
, unsigned long offset
,
1629 ctf_bundle_t
*ctb
= arg
;
1632 /* Don't check nameless members (e.g. anonymous structs/unions) against each
1637 if (ctf_member_info (ctb
->ctb_file
, ctb
->ctb_type
, name
, &ctm
) < 0)
1639 ctf_dprintf ("Conflict due to member %s iteration error: %s.\n", name
,
1640 ctf_errmsg (ctf_errno (ctb
->ctb_file
)));
1643 if (ctm
.ctm_offset
!= offset
)
1645 ctf_dprintf ("Conflict due to member %s offset change: "
1646 "%lx versus %lx\n", name
, ctm
.ctm_offset
, offset
);
1653 membadd (const char *name
, ctf_id_t type
, unsigned long offset
, void *arg
)
1655 ctf_bundle_t
*ctb
= arg
;
1659 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
1660 return (ctf_set_errno (ctb
->ctb_file
, EAGAIN
));
1662 if (name
!= NULL
&& (s
= strdup (name
)) == NULL
)
1665 return (ctf_set_errno (ctb
->ctb_file
, EAGAIN
));
1668 /* For now, dmd_type is copied as the src_fp's type; it is reset to an
1669 equivalent dst_fp type by a final loop in ctf_add_type(), below. */
1671 dmd
->dmd_type
= type
;
1672 dmd
->dmd_offset
= offset
;
1673 dmd
->dmd_value
= -1;
1675 ctf_list_append (&ctb
->ctb_dtd
->dtd_u
.dtu_members
, dmd
);
1677 ctb
->ctb_file
->ctf_flags
|= LCTF_DIRTY
;
1681 /* The ctf_add_type routine is used to copy a type from a source CTF container
1682 to a dynamic destination container. This routine operates recursively by
1683 following the source type's links and embedded member types. If the
1684 destination container already contains a named type which has the same
1685 attributes, then we succeed and return this type but no changes occur. */
1687 ctf_add_type_internal (ctf_file_t
*dst_fp
, ctf_file_t
*src_fp
, ctf_id_t src_type
,
1688 ctf_file_t
*proc_tracking_fp
)
1690 ctf_id_t dst_type
= CTF_ERR
;
1691 uint32_t dst_kind
= CTF_K_UNKNOWN
;
1692 ctf_file_t
*tmp_fp
= dst_fp
;
1696 uint32_t kind
, forward_kind
, flag
, vlen
;
1698 const ctf_type_t
*src_tp
, *dst_tp
;
1699 ctf_bundle_t src
, dst
;
1700 ctf_encoding_t src_en
, dst_en
;
1701 ctf_arinfo_t src_ar
, dst_ar
;
1705 ctf_id_t orig_src_type
= src_type
;
1707 if (!(dst_fp
->ctf_flags
& LCTF_RDWR
))
1708 return (ctf_set_errno (dst_fp
, ECTF_RDONLY
));
1710 if ((src_tp
= ctf_lookup_by_id (&src_fp
, src_type
)) == NULL
)
1711 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1713 if ((ctf_type_resolve (src_fp
, src_type
) == CTF_ERR
)
1714 && (ctf_errno (src_fp
) == ECTF_NONREPRESENTABLE
))
1715 return (ctf_set_errno (dst_fp
, ECTF_NONREPRESENTABLE
));
1717 name
= ctf_strptr (src_fp
, src_tp
->ctt_name
);
1718 kind
= LCTF_INFO_KIND (src_fp
, src_tp
->ctt_info
);
1719 flag
= LCTF_INFO_ISROOT (src_fp
, src_tp
->ctt_info
);
1720 vlen
= LCTF_INFO_VLEN (src_fp
, src_tp
->ctt_info
);
1722 /* If this is a type we are currently in the middle of adding, hand it
1723 straight back. (This lets us handle self-referential structures without
1724 considering forwards and empty structures the same as their completed
1727 tmp
= ctf_type_mapping (src_fp
, src_type
, &tmp_fp
);
1731 if (ctf_dynhash_lookup (proc_tracking_fp
->ctf_add_processing
,
1732 (void *) (uintptr_t) src_type
))
1735 /* If this type has already been added from this container, and is the same
1736 kind and (if a struct or union) has the same number of members, hand it
1739 if (ctf_type_kind_unsliced (tmp_fp
, tmp
) == (int) kind
)
1741 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
1742 || kind
== CTF_K_ENUM
)
1744 if ((dst_tp
= ctf_lookup_by_id (&tmp_fp
, dst_type
)) != NULL
)
1745 if (vlen
== LCTF_INFO_VLEN (tmp_fp
, dst_tp
->ctt_info
))
1753 forward_kind
= kind
;
1754 if (kind
== CTF_K_FORWARD
)
1755 forward_kind
= src_tp
->ctt_type
;
1757 /* If the source type has a name and is a root type (visible at the
1758 top-level scope), lookup the name in the destination container and
1759 verify that it is of the same kind before we do anything else. */
1761 if ((flag
& CTF_ADD_ROOT
) && name
[0] != '\0'
1762 && (tmp
= ctf_lookup_by_rawname (dst_fp
, forward_kind
, name
)) != 0)
1765 dst_kind
= ctf_type_kind_unsliced (dst_fp
, dst_type
);
1768 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
1769 unless dst_type is a forward declaration and src_type is a struct,
1770 union, or enum (i.e. the definition of the previous forward decl).
1772 We also allow addition in the opposite order (addition of a forward when a
1773 struct, union, or enum already exists), which is a NOP and returns the
1774 already-present struct, union, or enum. */
1776 if (dst_type
!= CTF_ERR
&& dst_kind
!= kind
)
1778 if (kind
== CTF_K_FORWARD
1779 && (dst_kind
== CTF_K_ENUM
|| dst_kind
== CTF_K_STRUCT
1780 || dst_kind
== CTF_K_UNION
))
1782 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1786 if (dst_kind
!= CTF_K_FORWARD
1787 || (kind
!= CTF_K_ENUM
&& kind
!= CTF_K_STRUCT
1788 && kind
!= CTF_K_UNION
))
1790 ctf_dprintf ("Conflict for type %s: kinds differ, new: %i; "
1791 "old (ID %lx): %i\n", name
, kind
, dst_type
, dst_kind
);
1792 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1796 /* We take special action for an integer, float, or slice since it is
1797 described not only by its name but also its encoding. For integers,
1798 bit-fields exploit this degeneracy. */
1800 if (kind
== CTF_K_INTEGER
|| kind
== CTF_K_FLOAT
|| kind
== CTF_K_SLICE
)
1802 if (ctf_type_encoding (src_fp
, src_type
, &src_en
) != 0)
1803 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1805 if (dst_type
!= CTF_ERR
)
1807 ctf_file_t
*fp
= dst_fp
;
1809 if ((dst_tp
= ctf_lookup_by_id (&fp
, dst_type
)) == NULL
)
1812 if (ctf_type_encoding (dst_fp
, dst_type
, &dst_en
) != 0)
1813 return CTF_ERR
; /* errno set for us. */
1815 if (LCTF_INFO_ISROOT (fp
, dst_tp
->ctt_info
) & CTF_ADD_ROOT
)
1817 /* The type that we found in the hash is also root-visible. If
1818 the two types match then use the existing one; otherwise,
1819 declare a conflict. Note: slices are not certain to match
1820 even if there is no conflict: we must check the contained type
1823 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1825 if (kind
!= CTF_K_SLICE
)
1827 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1833 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1838 /* We found a non-root-visible type in the hash. If its encoding
1839 is the same, we can reuse it, unless it is a slice. */
1841 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1843 if (kind
!= CTF_K_SLICE
)
1845 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1853 src
.ctb_file
= src_fp
;
1854 src
.ctb_type
= src_type
;
1857 dst
.ctb_file
= dst_fp
;
1858 dst
.ctb_type
= dst_type
;
1861 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add
1862 a new type with the same properties as src_type to dst_fp. If dst_type is
1863 not CTF_ERR, then we verify that dst_type has the same attributes as
1864 src_type. We recurse for embedded references. Before we start, we note
1865 that we are processing this type, to prevent infinite recursion: we do not
1866 re-process any type that appears in this list. The list is emptied
1867 wholesale at the end of processing everything in this recursive stack. */
1869 if (ctf_dynhash_insert (proc_tracking_fp
->ctf_add_processing
,
1870 (void *) (uintptr_t) src_type
, (void *) 1) < 0)
1871 return ctf_set_errno (dst_fp
, ENOMEM
);
1876 /* If we found a match we will have either returned it or declared a
1878 dst_type
= ctf_add_integer (dst_fp
, flag
, name
, &src_en
);
1882 /* If we found a match we will have either returned it or declared a
1884 dst_type
= ctf_add_float (dst_fp
, flag
, name
, &src_en
);
1888 /* We have checked for conflicting encodings: now try to add the
1890 src_type
= ctf_type_reference (src_fp
, src_type
);
1891 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1894 if (src_type
== CTF_ERR
)
1895 return CTF_ERR
; /* errno is set for us. */
1897 dst_type
= ctf_add_slice (dst_fp
, flag
, src_type
, &src_en
);
1901 case CTF_K_VOLATILE
:
1903 case CTF_K_RESTRICT
:
1904 src_type
= ctf_type_reference (src_fp
, src_type
);
1905 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1908 if (src_type
== CTF_ERR
)
1909 return CTF_ERR
; /* errno is set for us. */
1911 dst_type
= ctf_add_reftype (dst_fp
, flag
, src_type
, kind
);
1915 if (ctf_array_info (src_fp
, src_type
, &src_ar
) != 0)
1916 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1918 src_ar
.ctr_contents
=
1919 ctf_add_type_internal (dst_fp
, src_fp
, src_ar
.ctr_contents
,
1921 src_ar
.ctr_index
= ctf_add_type_internal (dst_fp
, src_fp
,
1924 src_ar
.ctr_nelems
= src_ar
.ctr_nelems
;
1926 if (src_ar
.ctr_contents
== CTF_ERR
|| src_ar
.ctr_index
== CTF_ERR
)
1927 return CTF_ERR
; /* errno is set for us. */
1929 if (dst_type
!= CTF_ERR
)
1931 if (ctf_array_info (dst_fp
, dst_type
, &dst_ar
) != 0)
1932 return CTF_ERR
; /* errno is set for us. */
1934 if (memcmp (&src_ar
, &dst_ar
, sizeof (ctf_arinfo_t
)))
1936 ctf_dprintf ("Conflict for type %s against ID %lx: "
1937 "array info differs, old %lx/%lx/%x; "
1938 "new: %lx/%lx/%x\n", name
, dst_type
,
1939 src_ar
.ctr_contents
, src_ar
.ctr_index
,
1940 src_ar
.ctr_nelems
, dst_ar
.ctr_contents
,
1941 dst_ar
.ctr_index
, dst_ar
.ctr_nelems
);
1942 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1946 dst_type
= ctf_add_array (dst_fp
, flag
, &src_ar
);
1949 case CTF_K_FUNCTION
:
1950 ctc
.ctc_return
= ctf_add_type_internal (dst_fp
, src_fp
,
1956 if (ctc
.ctc_return
== CTF_ERR
)
1957 return CTF_ERR
; /* errno is set for us. */
1959 dst_type
= ctf_add_function (dst_fp
, flag
, &ctc
, NULL
);
1971 /* Technically to match a struct or union we need to check both
1972 ways (src members vs. dst, dst members vs. src) but we make
1973 this more optimal by only checking src vs. dst and comparing
1974 the total size of the structure (which we must do anyway)
1975 which covers the possibility of dst members not in src.
1976 This optimization can be defeated for unions, but is so
1977 pathological as to render it irrelevant for our purposes. */
1979 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
1980 && dst_kind
!= CTF_K_FORWARD
)
1982 if (ctf_type_size (src_fp
, src_type
) !=
1983 ctf_type_size (dst_fp
, dst_type
))
1985 ctf_dprintf ("Conflict for type %s against ID %lx: "
1986 "union size differs, old %li, new %li\n",
1988 (long) ctf_type_size (src_fp
, src_type
),
1989 (long) ctf_type_size (dst_fp
, dst_type
));
1990 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1993 if (ctf_member_iter (src_fp
, src_type
, membcmp
, &dst
))
1995 ctf_dprintf ("Conflict for type %s against ID %lx: "
1996 "members differ, see above\n", name
, dst_type
);
1997 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2003 /* Unlike the other cases, copying structs and unions is done
2004 manually so as to avoid repeated lookups in ctf_add_member
2005 and to ensure the exact same member offsets as in src_type. */
2007 dst_type
= ctf_add_generic (dst_fp
, flag
, name
, kind
, &dtd
);
2008 if (dst_type
== CTF_ERR
)
2009 return CTF_ERR
; /* errno is set for us. */
2011 dst
.ctb_type
= dst_type
;
2014 /* Pre-emptively add this struct to the type mapping so that
2015 structures that refer to themselves work. */
2016 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
2018 if (ctf_member_iter (src_fp
, src_type
, membadd
, &dst
) != 0)
2019 errs
++; /* Increment errs and fail at bottom of case. */
2021 if ((ssize
= ctf_type_size (src_fp
, src_type
)) < 0)
2022 return CTF_ERR
; /* errno is set for us. */
2024 size
= (size_t) ssize
;
2025 if (size
> CTF_MAX_SIZE
)
2027 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
2028 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
2029 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
2032 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
2034 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, vlen
);
2036 /* Make a final pass through the members changing each dmd_type (a
2037 src_fp type) to an equivalent type in dst_fp. We pass through all
2038 members, leaving any that fail set to CTF_ERR, unless they fail
2039 because they are marking a member of type not representable in this
2040 version of CTF, in which case we just want to silently omit them:
2041 no consumer can do anything with them anyway. */
2042 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
2043 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
2045 ctf_file_t
*dst
= dst_fp
;
2048 memb_type
= ctf_type_mapping (src_fp
, dmd
->dmd_type
, &dst
);
2051 if ((dmd
->dmd_type
=
2052 ctf_add_type_internal (dst_fp
, src_fp
, dmd
->dmd_type
,
2053 proc_tracking_fp
)) == CTF_ERR
)
2055 if (ctf_errno (dst_fp
) != ECTF_NONREPRESENTABLE
)
2060 dmd
->dmd_type
= memb_type
;
2064 return CTF_ERR
; /* errno is set for us. */
2069 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
2070 && dst_kind
!= CTF_K_FORWARD
)
2072 if (ctf_enum_iter (src_fp
, src_type
, enumcmp
, &dst
)
2073 || ctf_enum_iter (dst_fp
, dst_type
, enumcmp
, &src
))
2075 ctf_dprintf ("Conflict for enum %s against ID %lx: "
2076 "members differ, see above\n", name
, dst_type
);
2077 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2082 dst_type
= ctf_add_enum (dst_fp
, flag
, name
);
2083 if ((dst
.ctb_type
= dst_type
) == CTF_ERR
2084 || ctf_enum_iter (src_fp
, src_type
, enumadd
, &dst
))
2085 return CTF_ERR
; /* errno is set for us */
2090 if (dst_type
== CTF_ERR
)
2091 dst_type
= ctf_add_forward (dst_fp
, flag
, name
, forward_kind
);
2095 src_type
= ctf_type_reference (src_fp
, src_type
);
2096 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
2099 if (src_type
== CTF_ERR
)
2100 return CTF_ERR
; /* errno is set for us. */
2102 /* If dst_type is not CTF_ERR at this point, we should check if
2103 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
2104 ECTF_CONFLICT. However, this causes problems with bitness typedefs
2105 that vary based on things like if 32-bit then pid_t is int otherwise
2106 long. We therefore omit this check and assume that if the identically
2107 named typedef already exists in dst_fp, it is correct or
2110 if (dst_type
== CTF_ERR
)
2111 dst_type
= ctf_add_typedef (dst_fp
, flag
, name
, src_type
);
2116 return (ctf_set_errno (dst_fp
, ECTF_CORRUPT
));
2119 if (dst_type
!= CTF_ERR
)
2120 ctf_add_type_mapping (src_fp
, orig_src_type
, dst_fp
, dst_type
);
2125 ctf_add_type (ctf_file_t
*dst_fp
, ctf_file_t
*src_fp
, ctf_id_t src_type
)
2129 if (!src_fp
->ctf_add_processing
)
2130 src_fp
->ctf_add_processing
= ctf_dynhash_create (ctf_hash_integer
,
2131 ctf_hash_eq_integer
,
2134 /* We store the hash on the source, because it contains only source type IDs:
2135 but callers will invariably expect errors to appear on the dest. */
2136 if (!src_fp
->ctf_add_processing
)
2137 return (ctf_set_errno (dst_fp
, ENOMEM
));
2139 id
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
, src_fp
);
2140 ctf_dynhash_empty (src_fp
->ctf_add_processing
);
2145 /* Write the compressed CTF data stream to the specified gzFile descriptor. */
2147 ctf_gzwrite (ctf_file_t
*fp
, gzFile fd
)
2149 const unsigned char *buf
;
2153 resid
= sizeof (ctf_header_t
);
2154 buf
= (unsigned char *) fp
->ctf_header
;
2157 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
2158 return (ctf_set_errno (fp
, errno
));
2163 resid
= fp
->ctf_size
;
2167 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
2168 return (ctf_set_errno (fp
, errno
));
2176 /* Compress the specified CTF data stream and write it to the specified file
2179 ctf_compress_write (ctf_file_t
*fp
, int fd
)
2184 ctf_header_t
*hp
= &h
;
2185 ssize_t header_len
= sizeof (ctf_header_t
);
2186 ssize_t compress_len
;
2191 if (ctf_serialize (fp
) < 0)
2192 return -1; /* errno is set for us. */
2194 memcpy (hp
, fp
->ctf_header
, header_len
);
2195 hp
->cth_flags
|= CTF_F_COMPRESS
;
2196 compress_len
= compressBound (fp
->ctf_size
);
2198 if ((buf
= malloc (compress_len
)) == NULL
)
2199 return (ctf_set_errno (fp
, ECTF_ZALLOC
));
2201 if ((rc
= compress (buf
, (uLongf
*) &compress_len
,
2202 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
2204 ctf_dprintf ("zlib deflate err: %s\n", zError (rc
));
2205 err
= ctf_set_errno (fp
, ECTF_COMPRESS
);
2209 while (header_len
> 0)
2211 if ((len
= write (fd
, hp
, header_len
)) < 0)
2213 err
= ctf_set_errno (fp
, errno
);
2221 while (compress_len
> 0)
2223 if ((len
= write (fd
, bp
, compress_len
)) < 0)
2225 err
= ctf_set_errno (fp
, errno
);
2228 compress_len
-= len
;
2237 /* Optionally compress the specified CTF data stream and return it as a new
2238 dynamically-allocated string. */
2240 ctf_write_mem (ctf_file_t
*fp
, size_t *size
, size_t threshold
)
2245 ssize_t header_len
= sizeof (ctf_header_t
);
2246 ssize_t compress_len
;
2249 if (ctf_serialize (fp
) < 0)
2250 return NULL
; /* errno is set for us. */
2252 compress_len
= compressBound (fp
->ctf_size
);
2253 if (fp
->ctf_size
< threshold
)
2254 compress_len
= fp
->ctf_size
;
2255 if ((buf
= malloc (compress_len
2256 + sizeof (struct ctf_header
))) == NULL
)
2258 ctf_set_errno (fp
, ENOMEM
);
2262 hp
= (ctf_header_t
*) buf
;
2263 memcpy (hp
, fp
->ctf_header
, header_len
);
2264 bp
= buf
+ sizeof (struct ctf_header
);
2265 *size
= sizeof (struct ctf_header
);
2267 if (fp
->ctf_size
< threshold
)
2269 hp
->cth_flags
&= ~CTF_F_COMPRESS
;
2270 memcpy (bp
, fp
->ctf_buf
, fp
->ctf_size
);
2271 *size
+= fp
->ctf_size
;
2275 hp
->cth_flags
|= CTF_F_COMPRESS
;
2276 if ((rc
= compress (bp
, (uLongf
*) &compress_len
,
2277 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
2279 ctf_dprintf ("zlib deflate err: %s\n", zError (rc
));
2280 ctf_set_errno (fp
, ECTF_COMPRESS
);
2284 *size
+= compress_len
;
2289 /* Write the uncompressed CTF data stream to the specified file descriptor. */
2291 ctf_write (ctf_file_t
*fp
, int fd
)
2293 const unsigned char *buf
;
2297 if (ctf_serialize (fp
) < 0)
2298 return -1; /* errno is set for us. */
2300 resid
= sizeof (ctf_header_t
);
2301 buf
= (unsigned char *) fp
->ctf_header
;
2304 if ((len
= write (fd
, buf
, resid
)) <= 0)
2305 return (ctf_set_errno (fp
, errno
));
2310 resid
= fp
->ctf_size
;
2314 if ((len
= write (fd
, buf
, resid
)) <= 0)
2315 return (ctf_set_errno (fp
, errno
));