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 EOVERFLOW ERANGE
32 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
35 /* Make sure the ptrtab has enough space for at least one more type.
37 We start with 4KiB of ptrtab, enough for a thousand types, then grow it 25%
41 ctf_grow_ptrtab (ctf_dict_t
*fp
)
43 size_t new_ptrtab_len
= fp
->ctf_ptrtab_len
;
45 /* We allocate one more ptrtab entry than we need, for the initial zero,
46 plus one because the caller will probably allocate a new type. */
48 if (fp
->ctf_ptrtab
== NULL
)
49 new_ptrtab_len
= 1024;
50 else if ((fp
->ctf_typemax
+ 2) > fp
->ctf_ptrtab_len
)
51 new_ptrtab_len
= fp
->ctf_ptrtab_len
* 1.25;
53 if (new_ptrtab_len
!= fp
->ctf_ptrtab_len
)
57 if ((new_ptrtab
= realloc (fp
->ctf_ptrtab
,
58 new_ptrtab_len
* sizeof (uint32_t))) == NULL
)
59 return (ctf_set_errno (fp
, ENOMEM
));
61 fp
->ctf_ptrtab
= new_ptrtab
;
62 memset (fp
->ctf_ptrtab
+ fp
->ctf_ptrtab_len
, 0,
63 (new_ptrtab_len
- fp
->ctf_ptrtab_len
) * sizeof (uint32_t));
64 fp
->ctf_ptrtab_len
= new_ptrtab_len
;
69 /* To create an empty CTF dict, we just declare a zeroed header and call
70 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new dict r/w and
71 initialize the dynamic members. We start assigning type IDs at 1 because
72 type ID 0 is used as a sentinel and a not-found indicator. */
75 ctf_create (int *errp
)
77 static const ctf_header_t hdr
= { .cth_preamble
= { CTF_MAGIC
, CTF_VERSION
, 0 } };
79 ctf_dynhash_t
*dthash
;
80 ctf_dynhash_t
*dvhash
;
81 ctf_dynhash_t
*structs
= NULL
, *unions
= NULL
, *enums
= NULL
, *names
= NULL
;
86 dthash
= ctf_dynhash_create (ctf_hash_integer
, ctf_hash_eq_integer
,
90 ctf_set_open_errno (errp
, EAGAIN
);
94 dvhash
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
98 ctf_set_open_errno (errp
, EAGAIN
);
102 structs
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
104 unions
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
106 enums
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
108 names
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
110 if (!structs
|| !unions
|| !enums
|| !names
)
112 ctf_set_open_errno (errp
, EAGAIN
);
116 cts
.cts_name
= _CTF_SECTION
;
118 cts
.cts_size
= sizeof (hdr
);
121 if ((fp
= ctf_bufopen_internal (&cts
, NULL
, NULL
, NULL
, 1, errp
)) == NULL
)
124 fp
->ctf_structs
.ctn_writable
= structs
;
125 fp
->ctf_unions
.ctn_writable
= unions
;
126 fp
->ctf_enums
.ctn_writable
= enums
;
127 fp
->ctf_names
.ctn_writable
= names
;
128 fp
->ctf_dthash
= dthash
;
129 fp
->ctf_dvhash
= dvhash
;
131 fp
->ctf_snapshots
= 1;
132 fp
->ctf_snapshot_lu
= 0;
133 fp
->ctf_flags
|= LCTF_DIRTY
;
135 ctf_set_ctl_hashes (fp
);
136 ctf_setmodel (fp
, CTF_MODEL_NATIVE
);
137 if (ctf_grow_ptrtab (fp
) < 0)
139 ctf_set_open_errno (errp
, ctf_errno (fp
));
147 ctf_dynhash_destroy (structs
);
148 ctf_dynhash_destroy (unions
);
149 ctf_dynhash_destroy (enums
);
150 ctf_dynhash_destroy (names
);
151 ctf_dynhash_destroy (dvhash
);
153 ctf_dynhash_destroy (dthash
);
158 static unsigned char *
159 ctf_copy_smembers (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
161 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
164 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
166 ctf_member_t
*copied
;
169 ctm
.ctm_type
= (uint32_t) dmd
->dmd_type
;
170 ctm
.ctm_offset
= (uint32_t) dmd
->dmd_offset
;
172 memcpy (t
, &ctm
, sizeof (ctm
));
173 copied
= (ctf_member_t
*) t
;
175 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctm_name
);
183 static unsigned char *
184 ctf_copy_lmembers (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
186 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
189 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
191 ctf_lmember_t
*copied
;
194 ctlm
.ctlm_type
= (uint32_t) dmd
->dmd_type
;
195 ctlm
.ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI (dmd
->dmd_offset
);
196 ctlm
.ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO (dmd
->dmd_offset
);
198 memcpy (t
, &ctlm
, sizeof (ctlm
));
199 copied
= (ctf_lmember_t
*) t
;
201 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctlm_name
);
209 static unsigned char *
210 ctf_copy_emembers (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
212 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
215 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
219 cte
.cte_value
= dmd
->dmd_value
;
220 memcpy (t
, &cte
, sizeof (cte
));
221 copied
= (ctf_enum_t
*) t
;
222 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->cte_name
);
229 /* Sort a newly-constructed static variable array. */
231 typedef struct ctf_sort_var_arg_cb
235 } ctf_sort_var_arg_cb_t
;
238 ctf_sort_var (const void *one_
, const void *two_
, void *arg_
)
240 const ctf_varent_t
*one
= one_
;
241 const ctf_varent_t
*two
= two_
;
242 ctf_sort_var_arg_cb_t
*arg
= arg_
;
244 return (strcmp (ctf_strraw_explicit (arg
->fp
, one
->ctv_name
, arg
->strtab
),
245 ctf_strraw_explicit (arg
->fp
, two
->ctv_name
, arg
->strtab
)));
248 /* Compatibility: just update the threshold for ctf_discard. */
250 ctf_update (ctf_dict_t
*fp
)
252 if (!(fp
->ctf_flags
& LCTF_RDWR
))
253 return (ctf_set_errno (fp
, ECTF_RDONLY
));
255 fp
->ctf_dtoldid
= fp
->ctf_typemax
;
259 /* If the specified CTF dict is writable and has been modified, reload this dict
260 with the updated type definitions, ready for serialization. In order to make
261 this code and the rest of libctf as simple as possible, we perform updates by
262 taking the dynamic type definitions and creating an in-memory CTF dict
263 containing the definitions, and then call ctf_simple_open_internal() on it.
264 We perform one extra trick here for the benefit of callers and to keep our
265 code simple: ctf_simple_open_internal() will return a new ctf_dict_t, but we
266 want to keep the fp constant for the caller, so after
267 ctf_simple_open_internal() returns, we use memcpy to swap the interior of the
268 old and new ctf_dict_t's, and then free the old. */
270 ctf_serialize (ctf_dict_t
*fp
)
272 ctf_dict_t ofp
, *nfp
;
273 ctf_header_t hdr
, *hdrp
;
276 ctf_varent_t
*dvarents
;
277 ctf_strs_writable_t strtab
;
281 size_t buf_size
, type_size
, nvars
;
282 unsigned char *buf
, *newbuf
;
285 if (!(fp
->ctf_flags
& LCTF_RDWR
))
286 return (ctf_set_errno (fp
, ECTF_RDONLY
));
288 /* Update required? */
289 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
292 /* Fill in an initial CTF header. We will leave the label, object,
293 and function sections empty and only output a header, type section,
294 and string table. The type section begins at a 4-byte aligned
295 boundary past the CTF header itself (at relative offset zero). */
297 memset (&hdr
, 0, sizeof (hdr
));
298 hdr
.cth_magic
= CTF_MAGIC
;
299 hdr
.cth_version
= CTF_VERSION
;
301 hdr
.cth_flags
= CTF_F_DYNSTR
;
303 /* Iterate through the dynamic type definition list and compute the
304 size of the CTF type section we will need to generate. */
306 for (type_size
= 0, dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
307 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
309 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
310 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
312 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
313 type_size
+= sizeof (ctf_stype_t
);
315 type_size
+= sizeof (ctf_type_t
);
321 type_size
+= sizeof (uint32_t);
324 type_size
+= sizeof (ctf_array_t
);
327 type_size
+= sizeof (ctf_slice_t
);
330 type_size
+= sizeof (uint32_t) * (vlen
+ (vlen
& 1));
334 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
335 type_size
+= sizeof (ctf_member_t
) * vlen
;
337 type_size
+= sizeof (ctf_lmember_t
) * vlen
;
340 type_size
+= sizeof (ctf_enum_t
) * vlen
;
345 /* Computing the number of entries in the CTF variable section is much
348 for (nvars
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
);
349 dvd
!= NULL
; dvd
= ctf_list_next (dvd
), nvars
++);
351 /* Compute the size of the CTF buffer we need, sans only the string table,
352 then allocate a new buffer and memcpy the finished header to the start of
353 the buffer. (We will adjust this later with strtab length info.) */
355 hdr
.cth_typeoff
= hdr
.cth_varoff
+ (nvars
* sizeof (ctf_varent_t
));
356 hdr
.cth_stroff
= hdr
.cth_typeoff
+ type_size
;
359 buf_size
= sizeof (ctf_header_t
) + hdr
.cth_stroff
+ hdr
.cth_strlen
;
361 if ((buf
= malloc (buf_size
)) == NULL
)
362 return (ctf_set_errno (fp
, EAGAIN
));
364 memcpy (buf
, &hdr
, sizeof (ctf_header_t
));
365 t
= (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_varoff
;
367 hdrp
= (ctf_header_t
*) buf
;
368 if ((fp
->ctf_flags
& LCTF_CHILD
) && (fp
->ctf_parname
!= NULL
))
369 ctf_str_add_ref (fp
, fp
->ctf_parname
, &hdrp
->cth_parname
);
370 if (fp
->ctf_cuname
!= NULL
)
371 ctf_str_add_ref (fp
, fp
->ctf_cuname
, &hdrp
->cth_cuname
);
373 /* Work over the variable list, translating everything into ctf_varent_t's and
374 prepping the string table. */
376 dvarents
= (ctf_varent_t
*) t
;
377 for (i
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
;
378 dvd
= ctf_list_next (dvd
), i
++)
380 ctf_varent_t
*var
= &dvarents
[i
];
382 ctf_str_add_ref (fp
, dvd
->dvd_name
, &var
->ctv_name
);
383 var
->ctv_type
= (uint32_t) dvd
->dvd_type
;
387 t
+= sizeof (ctf_varent_t
) * nvars
;
389 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_typeoff
);
391 /* We now take a final lap through the dynamic type definition list and copy
392 the appropriate type records to the output buffer, noting down the
395 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
396 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
398 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
399 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
407 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
408 len
= sizeof (ctf_stype_t
);
410 len
= sizeof (ctf_type_t
);
412 memcpy (t
, &dtd
->dtd_data
, len
);
413 copied
= (ctf_stype_t
*) t
; /* name is at the start: constant offset. */
415 && (name
= ctf_strraw (fp
, copied
->ctt_name
)) != NULL
)
416 ctf_str_add_ref (fp
, name
, &copied
->ctt_name
);
423 if (kind
== CTF_K_INTEGER
)
425 encoding
= CTF_INT_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
426 dtd
->dtd_u
.dtu_enc
.cte_offset
,
427 dtd
->dtd_u
.dtu_enc
.cte_bits
);
431 encoding
= CTF_FP_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
432 dtd
->dtd_u
.dtu_enc
.cte_offset
,
433 dtd
->dtd_u
.dtu_enc
.cte_bits
);
435 memcpy (t
, &encoding
, sizeof (encoding
));
436 t
+= sizeof (encoding
);
440 memcpy (t
, &dtd
->dtd_u
.dtu_slice
, sizeof (struct ctf_slice
));
441 t
+= sizeof (struct ctf_slice
);
445 cta
.cta_contents
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_contents
;
446 cta
.cta_index
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_index
;
447 cta
.cta_nelems
= dtd
->dtd_u
.dtu_arr
.ctr_nelems
;
448 memcpy (t
, &cta
, sizeof (cta
));
454 uint32_t *argv
= (uint32_t *) (uintptr_t) t
;
457 for (argc
= 0; argc
< vlen
; argc
++)
458 *argv
++ = dtd
->dtd_u
.dtu_argv
[argc
];
461 *argv
++ = 0; /* Pad to 4-byte boundary. */
463 t
= (unsigned char *) argv
;
469 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
470 t
= ctf_copy_smembers (fp
, dtd
, t
);
472 t
= ctf_copy_lmembers (fp
, dtd
, t
);
476 t
= ctf_copy_emembers (fp
, dtd
, t
);
480 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_stroff
);
482 /* Construct the final string table and fill out all the string refs with the
483 final offsets. Then purge the refs list, because we're about to move this
484 strtab onto the end of the buf, invalidating all the offsets. */
485 strtab
= ctf_str_write_strtab (fp
);
486 ctf_str_purge_refs (fp
);
488 if (strtab
.cts_strs
== NULL
)
491 return (ctf_set_errno (fp
, EAGAIN
));
494 /* Now the string table is constructed, we can sort the buffer of
496 ctf_sort_var_arg_cb_t sort_var_arg
= { fp
, (ctf_strs_t
*) &strtab
};
497 ctf_qsort_r (dvarents
, nvars
, sizeof (ctf_varent_t
), ctf_sort_var
,
500 if ((newbuf
= ctf_realloc (fp
, buf
, buf_size
+ strtab
.cts_len
)) == NULL
)
503 free (strtab
.cts_strs
);
504 return (ctf_set_errno (fp
, EAGAIN
));
507 memcpy (buf
+ buf_size
, strtab
.cts_strs
, strtab
.cts_len
);
508 hdrp
= (ctf_header_t
*) buf
;
509 hdrp
->cth_strlen
= strtab
.cts_len
;
510 buf_size
+= hdrp
->cth_strlen
;
511 free (strtab
.cts_strs
);
513 /* Finally, we are ready to ctf_simple_open() the new dict. If this is
514 successful, we then switch nfp and fp and free the old dict. */
516 if ((nfp
= ctf_simple_open_internal ((char *) buf
, buf_size
, NULL
, 0,
517 0, NULL
, 0, fp
->ctf_syn_ext_strtab
,
521 return (ctf_set_errno (fp
, err
));
524 (void) ctf_setmodel (nfp
, ctf_getmodel (fp
));
526 nfp
->ctf_parent
= fp
->ctf_parent
;
527 nfp
->ctf_parent_unreffed
= fp
->ctf_parent_unreffed
;
528 nfp
->ctf_refcnt
= fp
->ctf_refcnt
;
529 nfp
->ctf_flags
|= fp
->ctf_flags
& ~LCTF_DIRTY
;
530 if (nfp
->ctf_dynbase
== NULL
)
531 nfp
->ctf_dynbase
= buf
; /* Make sure buf is freed on close. */
532 nfp
->ctf_dthash
= fp
->ctf_dthash
;
533 nfp
->ctf_dtdefs
= fp
->ctf_dtdefs
;
534 nfp
->ctf_dvhash
= fp
->ctf_dvhash
;
535 nfp
->ctf_dvdefs
= fp
->ctf_dvdefs
;
536 nfp
->ctf_dtoldid
= fp
->ctf_dtoldid
;
537 nfp
->ctf_add_processing
= fp
->ctf_add_processing
;
538 nfp
->ctf_snapshots
= fp
->ctf_snapshots
+ 1;
539 nfp
->ctf_specific
= fp
->ctf_specific
;
540 nfp
->ctf_ptrtab
= fp
->ctf_ptrtab
;
541 nfp
->ctf_ptrtab_len
= fp
->ctf_ptrtab_len
;
542 nfp
->ctf_link_inputs
= fp
->ctf_link_inputs
;
543 nfp
->ctf_link_outputs
= fp
->ctf_link_outputs
;
544 nfp
->ctf_errs_warnings
= fp
->ctf_errs_warnings
;
545 nfp
->ctf_str_prov_offset
= fp
->ctf_str_prov_offset
;
546 nfp
->ctf_syn_ext_strtab
= fp
->ctf_syn_ext_strtab
;
547 nfp
->ctf_link_in_cu_mapping
= fp
->ctf_link_in_cu_mapping
;
548 nfp
->ctf_link_out_cu_mapping
= fp
->ctf_link_out_cu_mapping
;
549 nfp
->ctf_link_type_mapping
= fp
->ctf_link_type_mapping
;
550 nfp
->ctf_link_memb_name_changer
= fp
->ctf_link_memb_name_changer
;
551 nfp
->ctf_link_memb_name_changer_arg
= fp
->ctf_link_memb_name_changer_arg
;
552 nfp
->ctf_link_variable_filter
= fp
->ctf_link_variable_filter
;
553 nfp
->ctf_link_variable_filter_arg
= fp
->ctf_link_variable_filter_arg
;
554 nfp
->ctf_link_flags
= fp
->ctf_link_flags
;
555 nfp
->ctf_dedup_atoms
= fp
->ctf_dedup_atoms
;
556 nfp
->ctf_dedup_atoms_alloc
= fp
->ctf_dedup_atoms_alloc
;
557 memcpy (&nfp
->ctf_dedup
, &fp
->ctf_dedup
, sizeof (fp
->ctf_dedup
));
559 nfp
->ctf_snapshot_lu
= fp
->ctf_snapshots
;
561 memcpy (&nfp
->ctf_lookups
, fp
->ctf_lookups
, sizeof (fp
->ctf_lookups
));
562 nfp
->ctf_structs
= fp
->ctf_structs
;
563 nfp
->ctf_unions
= fp
->ctf_unions
;
564 nfp
->ctf_enums
= fp
->ctf_enums
;
565 nfp
->ctf_names
= fp
->ctf_names
;
567 fp
->ctf_dthash
= NULL
;
568 ctf_str_free_atoms (nfp
);
569 nfp
->ctf_str_atoms
= fp
->ctf_str_atoms
;
570 nfp
->ctf_prov_strtab
= fp
->ctf_prov_strtab
;
571 fp
->ctf_str_atoms
= NULL
;
572 fp
->ctf_prov_strtab
= NULL
;
573 memset (&fp
->ctf_dtdefs
, 0, sizeof (ctf_list_t
));
574 memset (&fp
->ctf_errs_warnings
, 0, sizeof (ctf_list_t
));
575 fp
->ctf_add_processing
= NULL
;
576 fp
->ctf_ptrtab
= NULL
;
577 fp
->ctf_link_inputs
= NULL
;
578 fp
->ctf_link_outputs
= NULL
;
579 fp
->ctf_syn_ext_strtab
= NULL
;
580 fp
->ctf_link_in_cu_mapping
= NULL
;
581 fp
->ctf_link_out_cu_mapping
= NULL
;
582 fp
->ctf_link_type_mapping
= NULL
;
583 fp
->ctf_dedup_atoms
= NULL
;
584 fp
->ctf_dedup_atoms_alloc
= NULL
;
585 fp
->ctf_parent_unreffed
= 1;
587 fp
->ctf_dvhash
= NULL
;
588 memset (&fp
->ctf_dvdefs
, 0, sizeof (ctf_list_t
));
589 memset (fp
->ctf_lookups
, 0, sizeof (fp
->ctf_lookups
));
590 memset (&fp
->ctf_dedup
, 0, sizeof (fp
->ctf_dedup
));
591 fp
->ctf_structs
.ctn_writable
= NULL
;
592 fp
->ctf_unions
.ctn_writable
= NULL
;
593 fp
->ctf_enums
.ctn_writable
= NULL
;
594 fp
->ctf_names
.ctn_writable
= NULL
;
596 memcpy (&ofp
, fp
, sizeof (ctf_dict_t
));
597 memcpy (fp
, nfp
, sizeof (ctf_dict_t
));
598 memcpy (nfp
, &ofp
, sizeof (ctf_dict_t
));
600 nfp
->ctf_refcnt
= 1; /* Force nfp to be freed. */
601 ctf_dict_close (nfp
);
607 ctf_name_table (ctf_dict_t
*fp
, int kind
)
612 return &fp
->ctf_structs
;
614 return &fp
->ctf_unions
;
616 return &fp
->ctf_enums
;
618 return &fp
->ctf_names
;
623 ctf_dtd_insert (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
, int flag
, int kind
)
626 if (ctf_dynhash_insert (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
,
630 if (flag
== CTF_ADD_ROOT
&& dtd
->dtd_data
.ctt_name
631 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
)
633 if (ctf_dynhash_insert (ctf_name_table (fp
, kind
)->ctn_writable
,
634 (char *) name
, (void *) (uintptr_t)
637 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t)
642 ctf_list_append (&fp
->ctf_dtdefs
, dtd
);
647 ctf_dtd_delete (ctf_dict_t
*fp
, ctf_dtdef_t
*dtd
)
649 ctf_dmdef_t
*dmd
, *nmd
;
650 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
651 int name_kind
= kind
;
654 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
);
661 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
662 dmd
!= NULL
; dmd
= nmd
)
664 if (dmd
->dmd_name
!= NULL
)
665 free (dmd
->dmd_name
);
666 nmd
= ctf_list_next (dmd
);
671 free (dtd
->dtd_u
.dtu_argv
);
674 name_kind
= dtd
->dtd_data
.ctt_type
;
678 if (dtd
->dtd_data
.ctt_name
679 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
680 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
682 ctf_dynhash_remove (ctf_name_table (fp
, name_kind
)->ctn_writable
,
684 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
687 ctf_list_delete (&fp
->ctf_dtdefs
, dtd
);
692 ctf_dtd_lookup (const ctf_dict_t
*fp
, ctf_id_t type
)
694 return (ctf_dtdef_t
*)
695 ctf_dynhash_lookup (fp
->ctf_dthash
, (void *) (uintptr_t) type
);
699 ctf_dynamic_type (const ctf_dict_t
*fp
, ctf_id_t id
)
703 if (!(fp
->ctf_flags
& LCTF_RDWR
))
706 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, id
))
709 idx
= LCTF_TYPE_TO_INDEX(fp
, id
);
711 if ((unsigned long) idx
<= fp
->ctf_typemax
)
712 return ctf_dtd_lookup (fp
, id
);
717 ctf_dvd_insert (ctf_dict_t
*fp
, ctf_dvdef_t
*dvd
)
719 if (ctf_dynhash_insert (fp
->ctf_dvhash
, dvd
->dvd_name
, dvd
) < 0)
721 ctf_list_append (&fp
->ctf_dvdefs
, dvd
);
726 ctf_dvd_delete (ctf_dict_t
*fp
, ctf_dvdef_t
*dvd
)
728 ctf_dynhash_remove (fp
->ctf_dvhash
, dvd
->dvd_name
);
729 free (dvd
->dvd_name
);
731 ctf_list_delete (&fp
->ctf_dvdefs
, dvd
);
736 ctf_dvd_lookup (const ctf_dict_t
*fp
, const char *name
)
738 return (ctf_dvdef_t
*) ctf_dynhash_lookup (fp
->ctf_dvhash
, name
);
741 /* Discard all of the dynamic type definitions and variable definitions that
742 have been added to the dict since the last call to ctf_update(). We locate
743 such types by scanning the dtd list and deleting elements that have type IDs
744 greater than ctf_dtoldid, which is set by ctf_update(), above, and by
745 scanning the variable list and deleting elements that have update IDs equal
746 to the current value of the last-update snapshot count (indicating that they
747 were added after the most recent call to ctf_update()). */
749 ctf_discard (ctf_dict_t
*fp
)
751 ctf_snapshot_id_t last_update
=
753 fp
->ctf_snapshot_lu
+ 1 };
755 /* Update required? */
756 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
759 return (ctf_rollback (fp
, last_update
));
763 ctf_snapshot (ctf_dict_t
*fp
)
765 ctf_snapshot_id_t snapid
;
766 snapid
.dtd_id
= fp
->ctf_typemax
;
767 snapid
.snapshot_id
= fp
->ctf_snapshots
++;
771 /* Like ctf_discard(), only discards everything after a particular ID. */
773 ctf_rollback (ctf_dict_t
*fp
, ctf_snapshot_id_t id
)
775 ctf_dtdef_t
*dtd
, *ntd
;
776 ctf_dvdef_t
*dvd
, *nvd
;
778 if (!(fp
->ctf_flags
& LCTF_RDWR
))
779 return (ctf_set_errno (fp
, ECTF_RDONLY
));
781 if (fp
->ctf_snapshot_lu
>= id
.snapshot_id
)
782 return (ctf_set_errno (fp
, ECTF_OVERROLLBACK
));
784 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
); dtd
!= NULL
; dtd
= ntd
)
789 ntd
= ctf_list_next (dtd
);
791 if (LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_type
) <= id
.dtd_id
)
794 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
795 if (kind
== CTF_K_FORWARD
)
796 kind
= dtd
->dtd_data
.ctt_type
;
798 if (dtd
->dtd_data
.ctt_name
799 && (name
= ctf_strraw (fp
, dtd
->dtd_data
.ctt_name
)) != NULL
800 && LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
))
802 ctf_dynhash_remove (ctf_name_table (fp
, kind
)->ctn_writable
,
804 ctf_str_remove_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
807 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) (uintptr_t) dtd
->dtd_type
);
808 ctf_dtd_delete (fp
, dtd
);
811 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
; dvd
= nvd
)
813 nvd
= ctf_list_next (dvd
);
815 if (dvd
->dvd_snapshots
<= id
.snapshot_id
)
818 ctf_dvd_delete (fp
, dvd
);
821 fp
->ctf_typemax
= id
.dtd_id
;
822 fp
->ctf_snapshots
= id
.snapshot_id
;
824 if (fp
->ctf_snapshots
== fp
->ctf_snapshot_lu
)
825 fp
->ctf_flags
&= ~LCTF_DIRTY
;
831 ctf_add_generic (ctf_dict_t
*fp
, uint32_t flag
, const char *name
, int kind
,
837 if (flag
!= CTF_ADD_NONROOT
&& flag
!= CTF_ADD_ROOT
)
838 return (ctf_set_errno (fp
, EINVAL
));
840 if (!(fp
->ctf_flags
& LCTF_RDWR
))
841 return (ctf_set_errno (fp
, ECTF_RDONLY
));
843 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) >= CTF_MAX_TYPE
)
844 return (ctf_set_errno (fp
, ECTF_FULL
));
846 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_typemax
, 1) == (CTF_MAX_PTYPE
- 1))
847 return (ctf_set_errno (fp
, ECTF_FULL
));
849 /* Make sure ptrtab always grows to be big enough for all types. */
850 if (ctf_grow_ptrtab (fp
) < 0)
851 return CTF_ERR
; /* errno is set for us. */
853 if ((dtd
= malloc (sizeof (ctf_dtdef_t
))) == NULL
)
854 return (ctf_set_errno (fp
, EAGAIN
));
856 type
= ++fp
->ctf_typemax
;
857 type
= LCTF_INDEX_TO_TYPE (fp
, type
, (fp
->ctf_flags
& LCTF_CHILD
));
859 memset (dtd
, 0, sizeof (ctf_dtdef_t
));
860 dtd
->dtd_data
.ctt_name
= ctf_str_add_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
861 dtd
->dtd_type
= type
;
863 if (dtd
->dtd_data
.ctt_name
== 0 && name
!= NULL
&& name
[0] != '\0')
866 return (ctf_set_errno (fp
, EAGAIN
));
869 if (ctf_dtd_insert (fp
, dtd
, flag
, kind
) < 0)
872 return CTF_ERR
; /* errno is set for us. */
874 fp
->ctf_flags
|= LCTF_DIRTY
;
880 /* When encoding integer sizes, we want to convert a byte count in the range
881 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
882 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
898 ctf_add_encoded (ctf_dict_t
*fp
, uint32_t flag
,
899 const char *name
, const ctf_encoding_t
*ep
, uint32_t kind
)
905 return (ctf_set_errno (fp
, EINVAL
));
907 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, &dtd
)) == CTF_ERR
)
908 return CTF_ERR
; /* errno is set for us. */
910 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
911 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
913 dtd
->dtd_u
.dtu_enc
= *ep
;
919 ctf_add_reftype (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
, uint32_t kind
)
923 ctf_dict_t
*tmp
= fp
;
924 int child
= fp
->ctf_flags
& LCTF_CHILD
;
926 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
927 return (ctf_set_errno (fp
, EINVAL
));
929 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
930 return CTF_ERR
; /* errno is set for us. */
932 if ((type
= ctf_add_generic (fp
, flag
, NULL
, kind
, &dtd
)) == CTF_ERR
)
933 return CTF_ERR
; /* errno is set for us. */
935 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
936 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
938 if (kind
!= CTF_K_POINTER
)
941 /* If we are adding a pointer, update the ptrtab, both the directly pointed-to
942 type and (if an anonymous typedef node is being pointed at) the type that
943 points at too. Note that ctf_typemax is at this point one higher than we
944 want to check against, because it's just been incremented for the addition
947 uint32_t type_idx
= LCTF_TYPE_TO_INDEX (fp
, type
);
948 uint32_t ref_idx
= LCTF_TYPE_TO_INDEX (fp
, ref
);
950 if (LCTF_TYPE_ISCHILD (fp
, ref
) == child
951 && ref_idx
< fp
->ctf_typemax
)
953 fp
->ctf_ptrtab
[ref_idx
] = type_idx
;
955 ctf_id_t refref_idx
= LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_data
.ctt_type
);
958 && (LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) == CTF_K_TYPEDEF
)
959 && strcmp (ctf_strptr (fp
, dtd
->dtd_data
.ctt_name
), "") == 0
960 && refref_idx
< fp
->ctf_typemax
)
961 fp
->ctf_ptrtab
[refref_idx
] = type_idx
;
968 ctf_add_slice (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
,
969 const ctf_encoding_t
*ep
)
972 ctf_id_t resolved_ref
= ref
;
975 const ctf_type_t
*tp
;
976 ctf_dict_t
*tmp
= fp
;
979 return (ctf_set_errno (fp
, EINVAL
));
981 if ((ep
->cte_bits
> 255) || (ep
->cte_offset
> 255))
982 return (ctf_set_errno (fp
, ECTF_SLICEOVERFLOW
));
984 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
985 return (ctf_set_errno (fp
, EINVAL
));
987 if (ref
!= 0 && ((tp
= ctf_lookup_by_id (&tmp
, ref
)) == NULL
))
988 return CTF_ERR
; /* errno is set for us. */
990 /* Make sure we ultimately point to an integral type. We also allow slices to
991 point to the unimplemented type, for now, because the compiler can emit
992 such slices, though they're not very much use. */
994 resolved_ref
= ctf_type_resolve_unsliced (tmp
, ref
);
995 kind
= ctf_type_kind_unsliced (tmp
, resolved_ref
);
997 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) &&
1000 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1002 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_SLICE
, &dtd
)) == CTF_ERR
)
1003 return CTF_ERR
; /* errno is set for us. */
1005 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_SLICE
, flag
, 0);
1006 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
1008 dtd
->dtd_u
.dtu_slice
.cts_type
= (uint32_t) ref
;
1009 dtd
->dtd_u
.dtu_slice
.cts_bits
= ep
->cte_bits
;
1010 dtd
->dtd_u
.dtu_slice
.cts_offset
= ep
->cte_offset
;
1016 ctf_add_integer (ctf_dict_t
*fp
, uint32_t flag
,
1017 const char *name
, const ctf_encoding_t
*ep
)
1019 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_INTEGER
));
1023 ctf_add_float (ctf_dict_t
*fp
, uint32_t flag
,
1024 const char *name
, const ctf_encoding_t
*ep
)
1026 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_FLOAT
));
1030 ctf_add_pointer (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1032 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_POINTER
));
1036 ctf_add_array (ctf_dict_t
*fp
, uint32_t flag
, const ctf_arinfo_t
*arp
)
1040 ctf_dict_t
*tmp
= fp
;
1043 return (ctf_set_errno (fp
, EINVAL
));
1045 if (arp
->ctr_contents
!= 0
1046 && ctf_lookup_by_id (&tmp
, arp
->ctr_contents
) == NULL
)
1047 return CTF_ERR
; /* errno is set for us. */
1050 if (ctf_lookup_by_id (&tmp
, arp
->ctr_index
) == NULL
)
1051 return CTF_ERR
; /* errno is set for us. */
1053 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_ARRAY
, &dtd
)) == CTF_ERR
)
1054 return CTF_ERR
; /* errno is set for us. */
1056 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ARRAY
, flag
, 0);
1057 dtd
->dtd_data
.ctt_size
= 0;
1058 dtd
->dtd_u
.dtu_arr
= *arp
;
1064 ctf_set_array (ctf_dict_t
*fp
, ctf_id_t type
, const ctf_arinfo_t
*arp
)
1066 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1068 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1069 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1072 || LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) != CTF_K_ARRAY
)
1073 return (ctf_set_errno (fp
, ECTF_BADID
));
1075 fp
->ctf_flags
|= LCTF_DIRTY
;
1076 dtd
->dtd_u
.dtu_arr
= *arp
;
1082 ctf_add_function (ctf_dict_t
*fp
, uint32_t flag
,
1083 const ctf_funcinfo_t
*ctc
, const ctf_id_t
*argv
)
1088 uint32_t *vdat
= NULL
;
1089 ctf_dict_t
*tmp
= fp
;
1092 if (ctc
== NULL
|| (ctc
->ctc_flags
& ~CTF_FUNC_VARARG
) != 0
1093 || (ctc
->ctc_argc
!= 0 && argv
== NULL
))
1094 return (ctf_set_errno (fp
, EINVAL
));
1096 vlen
= ctc
->ctc_argc
;
1097 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
1098 vlen
++; /* Add trailing zero to indicate varargs (see below). */
1100 if (ctc
->ctc_return
!= 0
1101 && ctf_lookup_by_id (&tmp
, ctc
->ctc_return
) == NULL
)
1102 return CTF_ERR
; /* errno is set for us. */
1104 if (vlen
> CTF_MAX_VLEN
)
1105 return (ctf_set_errno (fp
, EOVERFLOW
));
1107 if (vlen
!= 0 && (vdat
= malloc (sizeof (ctf_id_t
) * vlen
)) == NULL
)
1108 return (ctf_set_errno (fp
, EAGAIN
));
1110 for (i
= 0; i
< ctc
->ctc_argc
; i
++)
1113 if (argv
[i
] != 0 && ctf_lookup_by_id (&tmp
, argv
[i
]) == NULL
)
1116 return CTF_ERR
; /* errno is set for us. */
1118 vdat
[i
] = (uint32_t) argv
[i
];
1121 if ((type
= ctf_add_generic (fp
, flag
, NULL
, CTF_K_FUNCTION
,
1125 return CTF_ERR
; /* errno is set for us. */
1128 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FUNCTION
, flag
, vlen
);
1129 dtd
->dtd_data
.ctt_type
= (uint32_t) ctc
->ctc_return
;
1131 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
1132 vdat
[vlen
- 1] = 0; /* Add trailing zero to indicate varargs. */
1133 dtd
->dtd_u
.dtu_argv
= vdat
;
1139 ctf_add_struct_sized (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1145 /* Promote root-visible forwards to structs. */
1147 type
= ctf_lookup_by_rawname (fp
, CTF_K_STRUCT
, name
);
1149 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1150 dtd
= ctf_dtd_lookup (fp
, type
);
1151 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_STRUCT
,
1153 return CTF_ERR
; /* errno is set for us. */
1155 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_STRUCT
, flag
, 0);
1157 if (size
> CTF_MAX_SIZE
)
1159 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1160 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1161 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1164 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1170 ctf_add_struct (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
1172 return (ctf_add_struct_sized (fp
, flag
, name
, 0));
1176 ctf_add_union_sized (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1182 /* Promote root-visible forwards to unions. */
1184 type
= ctf_lookup_by_rawname (fp
, CTF_K_UNION
, name
);
1186 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1187 dtd
= ctf_dtd_lookup (fp
, type
);
1188 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_UNION
,
1190 return CTF_ERR
; /* errno is set for us */
1192 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_UNION
, flag
, 0);
1194 if (size
> CTF_MAX_SIZE
)
1196 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1197 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1198 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1201 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1207 ctf_add_union (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
1209 return (ctf_add_union_sized (fp
, flag
, name
, 0));
1213 ctf_add_enum (ctf_dict_t
*fp
, uint32_t flag
, const char *name
)
1218 /* Promote root-visible forwards to enums. */
1220 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
1222 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1223 dtd
= ctf_dtd_lookup (fp
, type
);
1224 else if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_ENUM
,
1226 return CTF_ERR
; /* errno is set for us. */
1228 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ENUM
, flag
, 0);
1229 dtd
->dtd_data
.ctt_size
= fp
->ctf_dmodel
->ctd_int
;
1235 ctf_add_enum_encoded (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1236 const ctf_encoding_t
*ep
)
1240 /* First, create the enum if need be, using most of the same machinery as
1241 ctf_add_enum(), to ensure that we do not allow things past that are not
1242 enums or forwards to them. (This includes other slices: you cannot slice a
1243 slice, which would be a useless thing to do anyway.) */
1246 type
= ctf_lookup_by_rawname (fp
, CTF_K_ENUM
, name
);
1250 if ((ctf_type_kind (fp
, type
) != CTF_K_FORWARD
) &&
1251 (ctf_type_kind_unsliced (fp
, type
) != CTF_K_ENUM
))
1252 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1254 else if ((type
= ctf_add_enum (fp
, flag
, name
)) == CTF_ERR
)
1255 return CTF_ERR
; /* errno is set for us. */
1257 /* Now attach a suitable slice to it. */
1259 return ctf_add_slice (fp
, flag
, type
, ep
);
1263 ctf_add_forward (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1269 if (!ctf_forwardable_kind (kind
))
1270 return (ctf_set_errno (fp
, ECTF_NOTSUE
));
1272 /* If the type is already defined or exists as a forward tag, just
1273 return the ctf_id_t of the existing definition. */
1276 type
= ctf_lookup_by_rawname (fp
, kind
, name
);
1281 if ((type
= ctf_add_generic (fp
, flag
, name
, kind
, &dtd
)) == CTF_ERR
)
1282 return CTF_ERR
; /* errno is set for us. */
1284 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FORWARD
, flag
, 0);
1285 dtd
->dtd_data
.ctt_type
= kind
;
1291 ctf_add_typedef (ctf_dict_t
*fp
, uint32_t flag
, const char *name
,
1296 ctf_dict_t
*tmp
= fp
;
1298 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
1299 return (ctf_set_errno (fp
, EINVAL
));
1301 if (ref
!= 0 && ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1302 return CTF_ERR
; /* errno is set for us. */
1304 if ((type
= ctf_add_generic (fp
, flag
, name
, CTF_K_TYPEDEF
,
1306 return CTF_ERR
; /* errno is set for us. */
1308 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_TYPEDEF
, flag
, 0);
1309 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
1315 ctf_add_volatile (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1317 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_VOLATILE
));
1321 ctf_add_const (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1323 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_CONST
));
1327 ctf_add_restrict (ctf_dict_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1329 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_RESTRICT
));
1333 ctf_add_enumerator (ctf_dict_t
*fp
, ctf_id_t enid
, const char *name
,
1336 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, enid
);
1339 uint32_t kind
, vlen
, root
;
1343 return (ctf_set_errno (fp
, EINVAL
));
1345 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1346 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1349 return (ctf_set_errno (fp
, ECTF_BADID
));
1351 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1352 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1353 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1355 if (kind
!= CTF_K_ENUM
)
1356 return (ctf_set_errno (fp
, ECTF_NOTENUM
));
1358 if (vlen
== CTF_MAX_VLEN
)
1359 return (ctf_set_errno (fp
, ECTF_DTFULL
));
1361 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1362 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1364 if (strcmp (dmd
->dmd_name
, name
) == 0)
1365 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1368 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
1369 return (ctf_set_errno (fp
, EAGAIN
));
1371 if ((s
= strdup (name
)) == NULL
)
1374 return (ctf_set_errno (fp
, EAGAIN
));
1378 dmd
->dmd_type
= CTF_ERR
;
1379 dmd
->dmd_offset
= 0;
1380 dmd
->dmd_value
= value
;
1382 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1383 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
1385 fp
->ctf_flags
|= LCTF_DIRTY
;
1391 ctf_add_member_offset (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
1392 ctf_id_t type
, unsigned long bit_offset
)
1394 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, souid
);
1397 ssize_t msize
, malign
, ssize
;
1398 uint32_t kind
, vlen
, root
;
1401 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1402 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1405 return (ctf_set_errno (fp
, ECTF_BADID
));
1407 if (name
!= NULL
&& name
[0] == '\0')
1410 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1411 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1412 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1414 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
1415 return (ctf_set_errno (fp
, ECTF_NOTSOU
));
1417 if (vlen
== CTF_MAX_VLEN
)
1418 return (ctf_set_errno (fp
, ECTF_DTFULL
));
1422 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1423 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1425 if (dmd
->dmd_name
!= NULL
&& strcmp (dmd
->dmd_name
, name
) == 0)
1426 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1430 if ((msize
= ctf_type_size (fp
, type
)) < 0 ||
1431 (malign
= ctf_type_align (fp
, type
)) < 0)
1433 /* The unimplemented type, and any type that resolves to it, has no size
1434 and no alignment: it can correspond to any number of compiler-inserted
1437 if (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
)
1441 ctf_set_errno (fp
, 0);
1444 return -1; /* errno is set for us. */
1447 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
1448 return (ctf_set_errno (fp
, EAGAIN
));
1450 if (name
!= NULL
&& (s
= strdup (name
)) == NULL
)
1453 return (ctf_set_errno (fp
, EAGAIN
));
1457 dmd
->dmd_type
= type
;
1458 dmd
->dmd_value
= -1;
1460 if (kind
== CTF_K_STRUCT
&& vlen
!= 0)
1462 if (bit_offset
== (unsigned long) - 1)
1464 /* Natural alignment. */
1466 ctf_dmdef_t
*lmd
= ctf_list_prev (&dtd
->dtd_u
.dtu_members
);
1467 ctf_id_t ltype
= ctf_type_resolve (fp
, lmd
->dmd_type
);
1468 size_t off
= lmd
->dmd_offset
;
1470 ctf_encoding_t linfo
;
1473 /* Propagate any error from ctf_type_resolve. If the last member was
1474 of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we
1475 cannot insert right after such a member without explicit offset
1476 specification, because its alignment and size is not known. */
1477 if (ltype
== CTF_ERR
)
1480 return -1; /* errno is set for us. */
1483 if (ctf_type_encoding (fp
, ltype
, &linfo
) == 0)
1484 off
+= linfo
.cte_bits
;
1485 else if ((lsize
= ctf_type_size (fp
, ltype
)) > 0)
1486 off
+= lsize
* CHAR_BIT
;
1488 /* Round up the offset of the end of the last member to
1489 the next byte boundary, convert 'off' to bytes, and
1490 then round it up again to the next multiple of the
1491 alignment required by the new member. Finally,
1492 convert back to bits and store the result in
1493 dmd_offset. Technically we could do more efficient
1494 packing if the new member is a bit-field, but we're
1495 the "compiler" and ANSI says we can do as we choose. */
1497 off
= roundup (off
, CHAR_BIT
) / CHAR_BIT
;
1498 off
= roundup (off
, MAX (malign
, 1));
1499 dmd
->dmd_offset
= off
* CHAR_BIT
;
1500 ssize
= off
+ msize
;
1504 /* Specified offset in bits. */
1506 dmd
->dmd_offset
= bit_offset
;
1507 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1508 ssize
= MAX (ssize
, ((signed) bit_offset
/ CHAR_BIT
) + msize
);
1513 dmd
->dmd_offset
= 0;
1514 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1515 ssize
= MAX (ssize
, msize
);
1518 if ((size_t) ssize
> CTF_MAX_SIZE
)
1520 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1521 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (ssize
);
1522 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (ssize
);
1525 dtd
->dtd_data
.ctt_size
= (uint32_t) ssize
;
1527 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1528 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
1530 fp
->ctf_flags
|= LCTF_DIRTY
;
1535 ctf_add_member_encoded (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
1536 ctf_id_t type
, unsigned long bit_offset
,
1537 const ctf_encoding_t encoding
)
1539 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1540 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1543 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) && (kind
!= CTF_K_ENUM
))
1544 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1546 if ((type
= ctf_add_slice (fp
, CTF_ADD_NONROOT
, otype
, &encoding
)) == CTF_ERR
)
1547 return -1; /* errno is set for us. */
1549 return ctf_add_member_offset (fp
, souid
, name
, type
, bit_offset
);
1553 ctf_add_member (ctf_dict_t
*fp
, ctf_id_t souid
, const char *name
,
1556 return ctf_add_member_offset (fp
, souid
, name
, type
, (unsigned long) - 1);
1560 ctf_add_variable (ctf_dict_t
*fp
, const char *name
, ctf_id_t ref
)
1563 ctf_dict_t
*tmp
= fp
;
1565 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1566 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1568 if (ctf_dvd_lookup (fp
, name
) != NULL
)
1569 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1571 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1572 return -1; /* errno is set for us. */
1574 /* Make sure this type is representable. */
1575 if ((ctf_type_resolve (fp
, ref
) == CTF_ERR
)
1576 && (ctf_errno (fp
) == ECTF_NONREPRESENTABLE
))
1579 if ((dvd
= malloc (sizeof (ctf_dvdef_t
))) == NULL
)
1580 return (ctf_set_errno (fp
, EAGAIN
));
1582 if (name
!= NULL
&& (dvd
->dvd_name
= strdup (name
)) == NULL
)
1585 return (ctf_set_errno (fp
, EAGAIN
));
1587 dvd
->dvd_type
= ref
;
1588 dvd
->dvd_snapshots
= fp
->ctf_snapshots
;
1590 if (ctf_dvd_insert (fp
, dvd
) < 0)
1592 free (dvd
->dvd_name
);
1594 return -1; /* errno is set for us. */
1597 fp
->ctf_flags
|= LCTF_DIRTY
;
1601 typedef struct ctf_bundle
1603 ctf_dict_t
*ctb_dict
; /* CTF dict handle. */
1604 ctf_id_t ctb_type
; /* CTF type identifier. */
1605 ctf_dtdef_t
*ctb_dtd
; /* CTF dynamic type definition (if any). */
1609 enumcmp (const char *name
, int value
, void *arg
)
1611 ctf_bundle_t
*ctb
= arg
;
1614 if (ctf_enum_value (ctb
->ctb_dict
, ctb
->ctb_type
, name
, &bvalue
) < 0)
1616 ctf_err_warn (ctb
->ctb_dict
, 0, 0,
1617 _("conflict due to enum %s iteration error"), name
);
1620 if (value
!= bvalue
)
1622 ctf_err_warn (ctb
->ctb_dict
, 1, ECTF_CONFLICT
,
1623 _("conflict due to enum value change: %i versus %i"),
1631 enumadd (const char *name
, int value
, void *arg
)
1633 ctf_bundle_t
*ctb
= arg
;
1635 return (ctf_add_enumerator (ctb
->ctb_dict
, ctb
->ctb_type
,
1640 membcmp (const char *name
, ctf_id_t type _libctf_unused_
, unsigned long offset
,
1643 ctf_bundle_t
*ctb
= arg
;
1646 /* Don't check nameless members (e.g. anonymous structs/unions) against each
1651 if (ctf_member_info (ctb
->ctb_dict
, ctb
->ctb_type
, name
, &ctm
) < 0)
1653 ctf_err_warn (ctb
->ctb_dict
, 0, 0,
1654 _("conflict due to struct member %s iteration error"),
1658 if (ctm
.ctm_offset
!= offset
)
1660 ctf_err_warn (ctb
->ctb_dict
, 1, ECTF_CONFLICT
,
1661 _("conflict due to struct member %s offset change: "
1663 name
, ctm
.ctm_offset
, offset
);
1670 membadd (const char *name
, ctf_id_t type
, unsigned long offset
, void *arg
)
1672 ctf_bundle_t
*ctb
= arg
;
1676 if ((dmd
= malloc (sizeof (ctf_dmdef_t
))) == NULL
)
1677 return (ctf_set_errno (ctb
->ctb_dict
, EAGAIN
));
1679 if (name
!= NULL
&& (s
= strdup (name
)) == NULL
)
1682 return (ctf_set_errno (ctb
->ctb_dict
, EAGAIN
));
1685 /* For now, dmd_type is copied as the src_fp's type; it is reset to an
1686 equivalent dst_fp type by a final loop in ctf_add_type(), below. */
1688 dmd
->dmd_type
= type
;
1689 dmd
->dmd_offset
= offset
;
1690 dmd
->dmd_value
= -1;
1692 ctf_list_append (&ctb
->ctb_dtd
->dtd_u
.dtu_members
, dmd
);
1694 ctb
->ctb_dict
->ctf_flags
|= LCTF_DIRTY
;
1698 /* The ctf_add_type routine is used to copy a type from a source CTF dictionary
1699 to a dynamic destination dictionary. This routine operates recursively by
1700 following the source type's links and embedded member types. If the
1701 destination dict already contains a named type which has the same attributes,
1702 then we succeed and return this type but no changes occur. */
1704 ctf_add_type_internal (ctf_dict_t
*dst_fp
, ctf_dict_t
*src_fp
, ctf_id_t src_type
,
1705 ctf_dict_t
*proc_tracking_fp
)
1707 ctf_id_t dst_type
= CTF_ERR
;
1708 uint32_t dst_kind
= CTF_K_UNKNOWN
;
1709 ctf_dict_t
*tmp_fp
= dst_fp
;
1713 uint32_t kind
, forward_kind
, flag
, vlen
;
1715 const ctf_type_t
*src_tp
, *dst_tp
;
1716 ctf_bundle_t src
, dst
;
1717 ctf_encoding_t src_en
, dst_en
;
1718 ctf_arinfo_t src_ar
, dst_ar
;
1722 ctf_id_t orig_src_type
= src_type
;
1724 if (!(dst_fp
->ctf_flags
& LCTF_RDWR
))
1725 return (ctf_set_errno (dst_fp
, ECTF_RDONLY
));
1727 if ((src_tp
= ctf_lookup_by_id (&src_fp
, src_type
)) == NULL
)
1728 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1730 if ((ctf_type_resolve (src_fp
, src_type
) == CTF_ERR
)
1731 && (ctf_errno (src_fp
) == ECTF_NONREPRESENTABLE
))
1732 return (ctf_set_errno (dst_fp
, ECTF_NONREPRESENTABLE
));
1734 name
= ctf_strptr (src_fp
, src_tp
->ctt_name
);
1735 kind
= LCTF_INFO_KIND (src_fp
, src_tp
->ctt_info
);
1736 flag
= LCTF_INFO_ISROOT (src_fp
, src_tp
->ctt_info
);
1737 vlen
= LCTF_INFO_VLEN (src_fp
, src_tp
->ctt_info
);
1739 /* If this is a type we are currently in the middle of adding, hand it
1740 straight back. (This lets us handle self-referential structures without
1741 considering forwards and empty structures the same as their completed
1744 tmp
= ctf_type_mapping (src_fp
, src_type
, &tmp_fp
);
1748 if (ctf_dynhash_lookup (proc_tracking_fp
->ctf_add_processing
,
1749 (void *) (uintptr_t) src_type
))
1752 /* If this type has already been added from this dictionary, and is the
1753 same kind and (if a struct or union) has the same number of members,
1754 hand it straight back. */
1756 if (ctf_type_kind_unsliced (tmp_fp
, tmp
) == (int) kind
)
1758 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
1759 || kind
== CTF_K_ENUM
)
1761 if ((dst_tp
= ctf_lookup_by_id (&tmp_fp
, dst_type
)) != NULL
)
1762 if (vlen
== LCTF_INFO_VLEN (tmp_fp
, dst_tp
->ctt_info
))
1770 forward_kind
= kind
;
1771 if (kind
== CTF_K_FORWARD
)
1772 forward_kind
= src_tp
->ctt_type
;
1774 /* If the source type has a name and is a root type (visible at the top-level
1775 scope), lookup the name in the destination dictionary and verify that it is
1776 of the same kind before we do anything else. */
1778 if ((flag
& CTF_ADD_ROOT
) && name
[0] != '\0'
1779 && (tmp
= ctf_lookup_by_rawname (dst_fp
, forward_kind
, name
)) != 0)
1782 dst_kind
= ctf_type_kind_unsliced (dst_fp
, dst_type
);
1785 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
1786 unless dst_type is a forward declaration and src_type is a struct,
1787 union, or enum (i.e. the definition of the previous forward decl).
1789 We also allow addition in the opposite order (addition of a forward when a
1790 struct, union, or enum already exists), which is a NOP and returns the
1791 already-present struct, union, or enum. */
1793 if (dst_type
!= CTF_ERR
&& dst_kind
!= kind
)
1795 if (kind
== CTF_K_FORWARD
1796 && (dst_kind
== CTF_K_ENUM
|| dst_kind
== CTF_K_STRUCT
1797 || dst_kind
== CTF_K_UNION
))
1799 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1803 if (dst_kind
!= CTF_K_FORWARD
1804 || (kind
!= CTF_K_ENUM
&& kind
!= CTF_K_STRUCT
1805 && kind
!= CTF_K_UNION
))
1807 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1808 _("ctf_add_type: conflict for type %s: "
1809 "kinds differ, new: %i; old (ID %lx): %i"),
1810 name
, kind
, dst_type
, dst_kind
);
1811 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1815 /* We take special action for an integer, float, or slice since it is
1816 described not only by its name but also its encoding. For integers,
1817 bit-fields exploit this degeneracy. */
1819 if (kind
== CTF_K_INTEGER
|| kind
== CTF_K_FLOAT
|| kind
== CTF_K_SLICE
)
1821 if (ctf_type_encoding (src_fp
, src_type
, &src_en
) != 0)
1822 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1824 if (dst_type
!= CTF_ERR
)
1826 ctf_dict_t
*fp
= dst_fp
;
1828 if ((dst_tp
= ctf_lookup_by_id (&fp
, dst_type
)) == NULL
)
1831 if (ctf_type_encoding (dst_fp
, dst_type
, &dst_en
) != 0)
1832 return CTF_ERR
; /* errno set for us. */
1834 if (LCTF_INFO_ISROOT (fp
, dst_tp
->ctt_info
) & CTF_ADD_ROOT
)
1836 /* The type that we found in the hash is also root-visible. If
1837 the two types match then use the existing one; otherwise,
1838 declare a conflict. Note: slices are not certain to match
1839 even if there is no conflict: we must check the contained type
1842 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1844 if (kind
!= CTF_K_SLICE
)
1846 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1852 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1857 /* We found a non-root-visible type in the hash. If its encoding
1858 is the same, we can reuse it, unless it is a slice. */
1860 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1862 if (kind
!= CTF_K_SLICE
)
1864 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
1872 src
.ctb_dict
= src_fp
;
1873 src
.ctb_type
= src_type
;
1876 dst
.ctb_dict
= dst_fp
;
1877 dst
.ctb_type
= dst_type
;
1880 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add
1881 a new type with the same properties as src_type to dst_fp. If dst_type is
1882 not CTF_ERR, then we verify that dst_type has the same attributes as
1883 src_type. We recurse for embedded references. Before we start, we note
1884 that we are processing this type, to prevent infinite recursion: we do not
1885 re-process any type that appears in this list. The list is emptied
1886 wholesale at the end of processing everything in this recursive stack. */
1888 if (ctf_dynhash_insert (proc_tracking_fp
->ctf_add_processing
,
1889 (void *) (uintptr_t) src_type
, (void *) 1) < 0)
1890 return ctf_set_errno (dst_fp
, ENOMEM
);
1895 /* If we found a match we will have either returned it or declared a
1897 dst_type
= ctf_add_integer (dst_fp
, flag
, name
, &src_en
);
1901 /* If we found a match we will have either returned it or declared a
1903 dst_type
= ctf_add_float (dst_fp
, flag
, name
, &src_en
);
1907 /* We have checked for conflicting encodings: now try to add the
1909 src_type
= ctf_type_reference (src_fp
, src_type
);
1910 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1913 if (src_type
== CTF_ERR
)
1914 return CTF_ERR
; /* errno is set for us. */
1916 dst_type
= ctf_add_slice (dst_fp
, flag
, src_type
, &src_en
);
1920 case CTF_K_VOLATILE
:
1922 case CTF_K_RESTRICT
:
1923 src_type
= ctf_type_reference (src_fp
, src_type
);
1924 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
1927 if (src_type
== CTF_ERR
)
1928 return CTF_ERR
; /* errno is set for us. */
1930 dst_type
= ctf_add_reftype (dst_fp
, flag
, src_type
, kind
);
1934 if (ctf_array_info (src_fp
, src_type
, &src_ar
) != 0)
1935 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1937 src_ar
.ctr_contents
=
1938 ctf_add_type_internal (dst_fp
, src_fp
, src_ar
.ctr_contents
,
1940 src_ar
.ctr_index
= ctf_add_type_internal (dst_fp
, src_fp
,
1943 src_ar
.ctr_nelems
= src_ar
.ctr_nelems
;
1945 if (src_ar
.ctr_contents
== CTF_ERR
|| src_ar
.ctr_index
== CTF_ERR
)
1946 return CTF_ERR
; /* errno is set for us. */
1948 if (dst_type
!= CTF_ERR
)
1950 if (ctf_array_info (dst_fp
, dst_type
, &dst_ar
) != 0)
1951 return CTF_ERR
; /* errno is set for us. */
1953 if (memcmp (&src_ar
, &dst_ar
, sizeof (ctf_arinfo_t
)))
1955 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
1956 _("conflict for type %s against ID %lx: array info "
1957 "differs, old %lx/%lx/%x; new: %lx/%lx/%x"),
1958 name
, dst_type
, src_ar
.ctr_contents
,
1959 src_ar
.ctr_index
, src_ar
.ctr_nelems
,
1960 dst_ar
.ctr_contents
, dst_ar
.ctr_index
,
1962 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1966 dst_type
= ctf_add_array (dst_fp
, flag
, &src_ar
);
1969 case CTF_K_FUNCTION
:
1970 ctc
.ctc_return
= ctf_add_type_internal (dst_fp
, src_fp
,
1976 if (ctc
.ctc_return
== CTF_ERR
)
1977 return CTF_ERR
; /* errno is set for us. */
1979 dst_type
= ctf_add_function (dst_fp
, flag
, &ctc
, NULL
);
1991 /* Technically to match a struct or union we need to check both
1992 ways (src members vs. dst, dst members vs. src) but we make
1993 this more optimal by only checking src vs. dst and comparing
1994 the total size of the structure (which we must do anyway)
1995 which covers the possibility of dst members not in src.
1996 This optimization can be defeated for unions, but is so
1997 pathological as to render it irrelevant for our purposes. */
1999 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
2000 && dst_kind
!= CTF_K_FORWARD
)
2002 if (ctf_type_size (src_fp
, src_type
) !=
2003 ctf_type_size (dst_fp
, dst_type
))
2005 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
2006 _("conflict for type %s against ID %lx: union "
2007 "size differs, old %li, new %li"), name
,
2008 dst_type
, (long) ctf_type_size (src_fp
, src_type
),
2009 (long) ctf_type_size (dst_fp
, dst_type
));
2010 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2013 if (ctf_member_iter (src_fp
, src_type
, membcmp
, &dst
))
2015 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
2016 _("conflict for type %s against ID %lx: members "
2017 "differ, see above"), name
, dst_type
);
2018 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2024 /* Unlike the other cases, copying structs and unions is done
2025 manually so as to avoid repeated lookups in ctf_add_member
2026 and to ensure the exact same member offsets as in src_type. */
2028 dst_type
= ctf_add_generic (dst_fp
, flag
, name
, kind
, &dtd
);
2029 if (dst_type
== CTF_ERR
)
2030 return CTF_ERR
; /* errno is set for us. */
2032 dst
.ctb_type
= dst_type
;
2035 /* Pre-emptively add this struct to the type mapping so that
2036 structures that refer to themselves work. */
2037 ctf_add_type_mapping (src_fp
, src_type
, dst_fp
, dst_type
);
2039 if (ctf_member_iter (src_fp
, src_type
, membadd
, &dst
) != 0)
2040 errs
++; /* Increment errs and fail at bottom of case. */
2042 if ((ssize
= ctf_type_size (src_fp
, src_type
)) < 0)
2043 return CTF_ERR
; /* errno is set for us. */
2045 size
= (size_t) ssize
;
2046 if (size
> CTF_MAX_SIZE
)
2048 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
2049 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
2050 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
2053 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
2055 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, vlen
);
2057 /* Make a final pass through the members changing each dmd_type (a
2058 src_fp type) to an equivalent type in dst_fp. We pass through all
2059 members, leaving any that fail set to CTF_ERR, unless they fail
2060 because they are marking a member of type not representable in this
2061 version of CTF, in which case we just want to silently omit them:
2062 no consumer can do anything with them anyway. */
2063 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
2064 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
2066 ctf_dict_t
*dst
= dst_fp
;
2069 memb_type
= ctf_type_mapping (src_fp
, dmd
->dmd_type
, &dst
);
2072 if ((dmd
->dmd_type
=
2073 ctf_add_type_internal (dst_fp
, src_fp
, dmd
->dmd_type
,
2074 proc_tracking_fp
)) == CTF_ERR
)
2076 if (ctf_errno (dst_fp
) != ECTF_NONREPRESENTABLE
)
2081 dmd
->dmd_type
= memb_type
;
2085 return CTF_ERR
; /* errno is set for us. */
2090 if (dst_type
!= CTF_ERR
&& kind
!= CTF_K_FORWARD
2091 && dst_kind
!= CTF_K_FORWARD
)
2093 if (ctf_enum_iter (src_fp
, src_type
, enumcmp
, &dst
)
2094 || ctf_enum_iter (dst_fp
, dst_type
, enumcmp
, &src
))
2096 ctf_err_warn (dst_fp
, 1, ECTF_CONFLICT
,
2097 _("conflict for enum %s against ID %lx: members "
2098 "differ, see above"), name
, dst_type
);
2099 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
2104 dst_type
= ctf_add_enum (dst_fp
, flag
, name
);
2105 if ((dst
.ctb_type
= dst_type
) == CTF_ERR
2106 || ctf_enum_iter (src_fp
, src_type
, enumadd
, &dst
))
2107 return CTF_ERR
; /* errno is set for us */
2112 if (dst_type
== CTF_ERR
)
2113 dst_type
= ctf_add_forward (dst_fp
, flag
, name
, forward_kind
);
2117 src_type
= ctf_type_reference (src_fp
, src_type
);
2118 src_type
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
,
2121 if (src_type
== CTF_ERR
)
2122 return CTF_ERR
; /* errno is set for us. */
2124 /* If dst_type is not CTF_ERR at this point, we should check if
2125 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
2126 ECTF_CONFLICT. However, this causes problems with bitness typedefs
2127 that vary based on things like if 32-bit then pid_t is int otherwise
2128 long. We therefore omit this check and assume that if the identically
2129 named typedef already exists in dst_fp, it is correct or
2132 if (dst_type
== CTF_ERR
)
2133 dst_type
= ctf_add_typedef (dst_fp
, flag
, name
, src_type
);
2138 return (ctf_set_errno (dst_fp
, ECTF_CORRUPT
));
2141 if (dst_type
!= CTF_ERR
)
2142 ctf_add_type_mapping (src_fp
, orig_src_type
, dst_fp
, dst_type
);
2147 ctf_add_type (ctf_dict_t
*dst_fp
, ctf_dict_t
*src_fp
, ctf_id_t src_type
)
2151 if (!src_fp
->ctf_add_processing
)
2152 src_fp
->ctf_add_processing
= ctf_dynhash_create (ctf_hash_integer
,
2153 ctf_hash_eq_integer
,
2156 /* We store the hash on the source, because it contains only source type IDs:
2157 but callers will invariably expect errors to appear on the dest. */
2158 if (!src_fp
->ctf_add_processing
)
2159 return (ctf_set_errno (dst_fp
, ENOMEM
));
2161 id
= ctf_add_type_internal (dst_fp
, src_fp
, src_type
, src_fp
);
2162 ctf_dynhash_empty (src_fp
->ctf_add_processing
);
2167 /* Write the compressed CTF data stream to the specified gzFile descriptor. */
2169 ctf_gzwrite (ctf_dict_t
*fp
, gzFile fd
)
2171 const unsigned char *buf
;
2175 resid
= sizeof (ctf_header_t
);
2176 buf
= (unsigned char *) fp
->ctf_header
;
2179 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
2180 return (ctf_set_errno (fp
, errno
));
2185 resid
= fp
->ctf_size
;
2189 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
2190 return (ctf_set_errno (fp
, errno
));
2198 /* Compress the specified CTF data stream and write it to the specified file
2201 ctf_compress_write (ctf_dict_t
*fp
, int fd
)
2206 ctf_header_t
*hp
= &h
;
2207 ssize_t header_len
= sizeof (ctf_header_t
);
2208 ssize_t compress_len
;
2213 if (ctf_serialize (fp
) < 0)
2214 return -1; /* errno is set for us. */
2216 memcpy (hp
, fp
->ctf_header
, header_len
);
2217 hp
->cth_flags
|= CTF_F_COMPRESS
;
2218 compress_len
= compressBound (fp
->ctf_size
);
2220 if ((buf
= malloc (compress_len
)) == NULL
)
2222 ctf_err_warn (fp
, 0, 0, _("ctf_compress_write: cannot allocate %li bytes"),
2223 (unsigned long) compress_len
);
2224 return (ctf_set_errno (fp
, ECTF_ZALLOC
));
2227 if ((rc
= compress (buf
, (uLongf
*) &compress_len
,
2228 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
2230 err
= ctf_set_errno (fp
, ECTF_COMPRESS
);
2231 ctf_err_warn (fp
, 0, 0, _("zlib deflate err: %s"), zError (rc
));
2235 while (header_len
> 0)
2237 if ((len
= write (fd
, hp
, header_len
)) < 0)
2239 err
= ctf_set_errno (fp
, errno
);
2240 ctf_err_warn (fp
, 0, 0, _("ctf_compress_write: error writing header"));
2248 while (compress_len
> 0)
2250 if ((len
= write (fd
, bp
, compress_len
)) < 0)
2252 err
= ctf_set_errno (fp
, errno
);
2253 ctf_err_warn (fp
, 0, 0, _("ctf_compress_write: error writing"));
2256 compress_len
-= len
;
2265 /* Optionally compress the specified CTF data stream and return it as a new
2266 dynamically-allocated string. */
2268 ctf_write_mem (ctf_dict_t
*fp
, size_t *size
, size_t threshold
)
2273 ssize_t header_len
= sizeof (ctf_header_t
);
2274 ssize_t compress_len
;
2277 if (ctf_serialize (fp
) < 0)
2278 return NULL
; /* errno is set for us. */
2280 compress_len
= compressBound (fp
->ctf_size
);
2281 if (fp
->ctf_size
< threshold
)
2282 compress_len
= fp
->ctf_size
;
2283 if ((buf
= malloc (compress_len
2284 + sizeof (struct ctf_header
))) == NULL
)
2286 ctf_set_errno (fp
, ENOMEM
);
2287 ctf_err_warn (fp
, 0, 0, _("ctf_write_mem: cannot allocate %li bytes"),
2288 (unsigned long) (compress_len
+ sizeof (struct ctf_header
)));
2292 hp
= (ctf_header_t
*) buf
;
2293 memcpy (hp
, fp
->ctf_header
, header_len
);
2294 bp
= buf
+ sizeof (struct ctf_header
);
2295 *size
= sizeof (struct ctf_header
);
2297 if (fp
->ctf_size
< threshold
)
2299 hp
->cth_flags
&= ~CTF_F_COMPRESS
;
2300 memcpy (bp
, fp
->ctf_buf
, fp
->ctf_size
);
2301 *size
+= fp
->ctf_size
;
2305 hp
->cth_flags
|= CTF_F_COMPRESS
;
2306 if ((rc
= compress (bp
, (uLongf
*) &compress_len
,
2307 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
2309 ctf_set_errno (fp
, ECTF_COMPRESS
);
2310 ctf_err_warn (fp
, 0, 0, _("zlib deflate err: %s"), zError (rc
));
2314 *size
+= compress_len
;
2319 /* Write the uncompressed CTF data stream to the specified file descriptor. */
2321 ctf_write (ctf_dict_t
*fp
, int fd
)
2323 const unsigned char *buf
;
2327 if (ctf_serialize (fp
) < 0)
2328 return -1; /* errno is set for us. */
2330 resid
= sizeof (ctf_header_t
);
2331 buf
= (unsigned char *) fp
->ctf_header
;
2334 if ((len
= write (fd
, buf
, resid
)) <= 0)
2336 ctf_err_warn (fp
, 0, errno
, _("ctf_write: error writing header"));
2337 return (ctf_set_errno (fp
, errno
));
2343 resid
= fp
->ctf_size
;
2347 if ((len
= write (fd
, buf
, resid
)) <= 0)
2349 ctf_err_warn (fp
, 0, errno
, _("ctf_write: error writing"));
2350 return (ctf_set_errno (fp
, errno
));