2 Copyright (C) 2019 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include <sys/param.h>
27 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
30 /* To create an empty CTF container, we just declare a zeroed header and call
31 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w
32 and initialize the dynamic members. We start assigning type IDs at 1 because
33 type ID 0 is used as a sentinel and a not-found indicator. */
36 ctf_create (int *errp
)
38 static const ctf_header_t hdr
= { .cth_preamble
= { CTF_MAGIC
, CTF_VERSION
, 0 } };
40 ctf_dynhash_t
*dthash
;
41 ctf_dynhash_t
*dvhash
;
42 ctf_dynhash_t
*dtbyname
;
47 dthash
= ctf_dynhash_create (ctf_hash_integer
, ctf_hash_eq_integer
,
51 ctf_set_open_errno (errp
, EAGAIN
);
55 dvhash
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
59 ctf_set_open_errno (errp
, EAGAIN
);
63 dtbyname
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
67 ctf_set_open_errno (errp
, EAGAIN
);
71 cts
.cts_name
= _CTF_SECTION
;
73 cts
.cts_size
= sizeof (hdr
);
76 if ((fp
= ctf_bufopen (&cts
, NULL
, NULL
, errp
)) == NULL
)
79 fp
->ctf_flags
|= LCTF_RDWR
;
80 fp
->ctf_dtbyname
= dtbyname
;
81 fp
->ctf_dthash
= dthash
;
82 fp
->ctf_dvhash
= dvhash
;
85 fp
->ctf_snapshots
= 1;
86 fp
->ctf_snapshot_lu
= 0;
91 ctf_dynhash_destroy (dtbyname
);
93 ctf_dynhash_destroy (dvhash
);
95 ctf_dynhash_destroy (dthash
);
100 static unsigned char *
101 ctf_copy_smembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
103 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
106 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
108 ctf_member_t
*copied
;
111 ctm
.ctm_type
= (uint32_t) dmd
->dmd_type
;
112 ctm
.ctm_offset
= (uint32_t) dmd
->dmd_offset
;
114 memcpy (t
, &ctm
, sizeof (ctm
));
115 copied
= (ctf_member_t
*) t
;
117 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctm_name
);
125 static unsigned char *
126 ctf_copy_lmembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
128 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
131 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
133 ctf_lmember_t
*copied
;
136 ctlm
.ctlm_type
= (uint32_t) dmd
->dmd_type
;
137 ctlm
.ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI (dmd
->dmd_offset
);
138 ctlm
.ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO (dmd
->dmd_offset
);
140 memcpy (t
, &ctlm
, sizeof (ctlm
));
141 copied
= (ctf_lmember_t
*) t
;
143 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->ctlm_name
);
151 static unsigned char *
152 ctf_copy_emembers (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
, unsigned char *t
)
154 ctf_dmdef_t
*dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
157 for (; dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
161 cte
.cte_value
= dmd
->dmd_value
;
162 memcpy (t
, &cte
, sizeof (cte
));
163 copied
= (ctf_enum_t
*) t
;
164 ctf_str_add_ref (fp
, dmd
->dmd_name
, &copied
->cte_name
);
171 /* Sort a newly-constructed static variable array. */
174 ctf_sort_var (const void *one_
, const void *two_
, void *strtab_
)
176 const ctf_varent_t
*one
= one_
;
177 const ctf_varent_t
*two
= two_
;
178 const char *strtab
= strtab_
;
179 const char *n1
= strtab
+ CTF_NAME_OFFSET (one
->ctv_name
);
180 const char *n2
= strtab
+ CTF_NAME_OFFSET (two
->ctv_name
);
182 return (strcmp (n1
, n2
));
185 /* If the specified CTF container is writable and has been modified, reload this
186 container with the updated type definitions. In order to make this code and
187 the rest of libctf as simple as possible, we perform updates by taking the
188 dynamic type definitions and creating an in-memory CTF file containing the
189 definitions, and then call ctf_simple_open() on it. This not only leverages
190 ctf_simple_open(), but also avoids having to bifurcate the rest of the library
191 code with different lookup paths for static and dynamic type definitions. We
192 are therefore optimizing greatly for lookup over update, which we assume will
193 be an uncommon operation. We perform one extra trick here for the benefit of
194 callers and to keep our code simple: ctf_simple_open() will return a new
195 ctf_file_t, but we want to keep the fp constant for the caller, so after
196 ctf_simple_open() returns, we use memcpy to swap the interior of the old and
197 new ctf_file_t's, and then free the old. */
199 ctf_update (ctf_file_t
*fp
)
201 ctf_file_t ofp
, *nfp
;
202 ctf_header_t hdr
, *hdrp
;
205 ctf_varent_t
*dvarents
;
206 ctf_strs_writable_t strtab
;
210 size_t buf_size
, type_size
, nvars
;
211 unsigned char *buf
, *newbuf
;
214 if (!(fp
->ctf_flags
& LCTF_RDWR
))
215 return (ctf_set_errno (fp
, ECTF_RDONLY
));
217 /* Update required? */
218 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
221 /* Fill in an initial CTF header. We will leave the label, object,
222 and function sections empty and only output a header, type section,
223 and string table. The type section begins at a 4-byte aligned
224 boundary past the CTF header itself (at relative offset zero). */
226 memset (&hdr
, 0, sizeof (hdr
));
227 hdr
.cth_magic
= CTF_MAGIC
;
228 hdr
.cth_version
= CTF_VERSION
;
230 /* Iterate through the dynamic type definition list and compute the
231 size of the CTF type section we will need to generate. */
233 for (type_size
= 0, dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
234 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
236 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
237 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
239 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
240 type_size
+= sizeof (ctf_stype_t
);
242 type_size
+= sizeof (ctf_type_t
);
248 type_size
+= sizeof (uint32_t);
251 type_size
+= sizeof (ctf_array_t
);
254 type_size
+= sizeof (ctf_slice_t
);
257 type_size
+= sizeof (uint32_t) * (vlen
+ (vlen
& 1));
261 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
262 type_size
+= sizeof (ctf_member_t
) * vlen
;
264 type_size
+= sizeof (ctf_lmember_t
) * vlen
;
267 type_size
+= sizeof (ctf_enum_t
) * vlen
;
272 /* Computing the number of entries in the CTF variable section is much
275 for (nvars
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
);
276 dvd
!= NULL
; dvd
= ctf_list_next (dvd
), nvars
++);
278 /* Compute the size of the CTF buffer we need, sans only the string table,
279 then allocate a new buffer and memcpy the finished header to the start of
280 the buffer. (We will adjust this later with strtab length info.) */
282 hdr
.cth_typeoff
= hdr
.cth_varoff
+ (nvars
* sizeof (ctf_varent_t
));
283 hdr
.cth_stroff
= hdr
.cth_typeoff
+ type_size
;
286 buf_size
= sizeof (ctf_header_t
) + hdr
.cth_stroff
+ hdr
.cth_strlen
;
288 if ((buf
= malloc (buf_size
)) == NULL
)
289 return (ctf_set_errno (fp
, EAGAIN
));
291 memcpy (buf
, &hdr
, sizeof (ctf_header_t
));
292 t
= (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_varoff
;
294 hdrp
= (ctf_header_t
*) buf
;
295 if ((fp
->ctf_flags
& LCTF_CHILD
) && (fp
->ctf_parname
!= NULL
))
296 ctf_str_add_ref (fp
, fp
->ctf_parname
, &hdrp
->cth_parname
);
297 if (fp
->ctf_cuname
!= NULL
)
298 ctf_str_add_ref (fp
, fp
->ctf_cuname
, &hdrp
->cth_cuname
);
300 /* Work over the variable list, translating everything into ctf_varent_t's and
301 prepping the string table. */
303 dvarents
= (ctf_varent_t
*) t
;
304 for (i
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
;
305 dvd
= ctf_list_next (dvd
), i
++)
307 ctf_varent_t
*var
= &dvarents
[i
];
309 ctf_str_add_ref (fp
, dvd
->dvd_name
, &var
->ctv_name
);
310 var
->ctv_type
= dvd
->dvd_type
;
314 t
+= sizeof (ctf_varent_t
) * nvars
;
316 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_typeoff
);
318 /* We now take a final lap through the dynamic type definition list and copy
319 the appropriate type records to the output buffer, noting down the
322 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
323 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
325 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
326 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
333 dtd
->dtd_data
.ctt_name
= 0;
335 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
336 len
= sizeof (ctf_stype_t
);
338 len
= sizeof (ctf_type_t
);
340 memcpy (t
, &dtd
->dtd_data
, len
);
341 copied
= (ctf_stype_t
*) t
; /* name is at the start: constant offset. */
343 ctf_str_add_ref (fp
, dtd
->dtd_name
, &copied
->ctt_name
);
350 if (kind
== CTF_K_INTEGER
)
352 encoding
= CTF_INT_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
353 dtd
->dtd_u
.dtu_enc
.cte_offset
,
354 dtd
->dtd_u
.dtu_enc
.cte_bits
);
358 encoding
= CTF_FP_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
359 dtd
->dtd_u
.dtu_enc
.cte_offset
,
360 dtd
->dtd_u
.dtu_enc
.cte_bits
);
362 memcpy (t
, &encoding
, sizeof (encoding
));
363 t
+= sizeof (encoding
);
367 memcpy (t
, &dtd
->dtd_u
.dtu_slice
, sizeof (struct ctf_slice
));
368 t
+= sizeof (struct ctf_slice
);
372 cta
.cta_contents
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_contents
;
373 cta
.cta_index
= (uint32_t) dtd
->dtd_u
.dtu_arr
.ctr_index
;
374 cta
.cta_nelems
= dtd
->dtd_u
.dtu_arr
.ctr_nelems
;
375 memcpy (t
, &cta
, sizeof (cta
));
381 uint32_t *argv
= (uint32_t *) (uintptr_t) t
;
384 for (argc
= 0; argc
< vlen
; argc
++)
385 *argv
++ = (uint32_t) dtd
->dtd_u
.dtu_argv
[argc
];
388 *argv
++ = 0; /* Pad to 4-byte boundary. */
390 t
= (unsigned char *) argv
;
396 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
397 t
= ctf_copy_smembers (fp
, dtd
, t
);
399 t
= ctf_copy_lmembers (fp
, dtd
, t
);
403 t
= ctf_copy_emembers (fp
, dtd
, t
);
407 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_stroff
);
409 /* Construct the final string table and fill out all the string refs with the
410 final offsets. Then purge the refs list, because we're about to move this
411 strtab onto the end of the buf, invalidating all the offsets. */
412 strtab
= ctf_str_write_strtab (fp
);
413 ctf_str_purge_refs (fp
);
415 /* Now the string table is constructed, we can sort the buffer of
417 ctf_qsort_r (dvarents
, nvars
, sizeof (ctf_varent_t
), ctf_sort_var
,
420 if ((newbuf
= ctf_realloc (fp
, buf
, buf_size
+ strtab
.cts_len
)) == NULL
)
423 ctf_free (strtab
.cts_strs
);
424 return (ctf_set_errno (fp
, EAGAIN
));
427 memcpy (buf
+ buf_size
, strtab
.cts_strs
, strtab
.cts_len
);
428 hdrp
= (ctf_header_t
*) buf
;
429 hdrp
->cth_strlen
= strtab
.cts_len
;
430 buf_size
+= hdrp
->cth_strlen
;
431 ctf_free (strtab
.cts_strs
);
433 /* Finally, we are ready to ctf_simple_open() the new container. If this
434 is successful, we then switch nfp and fp and free the old container. */
436 if ((nfp
= ctf_simple_open ((char *) buf
, buf_size
, NULL
, 0, 0, NULL
,
440 return (ctf_set_errno (fp
, err
));
443 (void) ctf_setmodel (nfp
, ctf_getmodel (fp
));
444 (void) ctf_import (nfp
, fp
->ctf_parent
);
446 nfp
->ctf_refcnt
= fp
->ctf_refcnt
;
447 nfp
->ctf_flags
|= fp
->ctf_flags
& ~LCTF_DIRTY
;
448 if (nfp
->ctf_dynbase
== NULL
)
449 nfp
->ctf_dynbase
= buf
; /* Make sure buf is freed on close. */
450 nfp
->ctf_dthash
= fp
->ctf_dthash
;
451 nfp
->ctf_dtdefs
= fp
->ctf_dtdefs
;
452 nfp
->ctf_dtbyname
= fp
->ctf_dtbyname
;
453 nfp
->ctf_dvhash
= fp
->ctf_dvhash
;
454 nfp
->ctf_dvdefs
= fp
->ctf_dvdefs
;
455 nfp
->ctf_dtnextid
= fp
->ctf_dtnextid
;
456 nfp
->ctf_dtoldid
= fp
->ctf_dtnextid
- 1;
457 nfp
->ctf_snapshots
= fp
->ctf_snapshots
+ 1;
458 nfp
->ctf_specific
= fp
->ctf_specific
;
460 nfp
->ctf_snapshot_lu
= fp
->ctf_snapshots
;
462 fp
->ctf_dtbyname
= NULL
;
463 fp
->ctf_dthash
= NULL
;
464 ctf_str_free_atoms (nfp
);
465 nfp
->ctf_str_atoms
= fp
->ctf_str_atoms
;
466 fp
->ctf_str_atoms
= NULL
;
467 memset (&fp
->ctf_dtdefs
, 0, sizeof (ctf_list_t
));
469 fp
->ctf_dvhash
= NULL
;
470 memset (&fp
->ctf_dvdefs
, 0, sizeof (ctf_list_t
));
472 memcpy (&ofp
, fp
, sizeof (ctf_file_t
));
473 memcpy (fp
, nfp
, sizeof (ctf_file_t
));
474 memcpy (nfp
, &ofp
, sizeof (ctf_file_t
));
476 /* Initialize the ctf_lookup_by_name top-level dictionary. We keep an
477 array of type name prefixes and the corresponding ctf_dynhash to use.
478 NOTE: This code must be kept in sync with the code in ctf_bufopen(). */
480 fp
->ctf_lookups
[0].ctl_hash
= fp
->ctf_structs
;
481 fp
->ctf_lookups
[1].ctl_hash
= fp
->ctf_unions
;
482 fp
->ctf_lookups
[2].ctl_hash
= fp
->ctf_enums
;
483 fp
->ctf_lookups
[3].ctl_hash
= fp
->ctf_names
;
485 nfp
->ctf_refcnt
= 1; /* Force nfp to be freed. */
486 ctf_file_close (nfp
);
492 ctf_prefixed_name (int kind
, const char *name
)
499 prefixed
= ctf_strdup ("struct ");
502 prefixed
= ctf_strdup ("union ");
505 prefixed
= ctf_strdup ("enum ");
508 prefixed
= ctf_strdup ("");
511 prefixed
= ctf_str_append (prefixed
, name
);
516 ctf_dtd_insert (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
)
518 if (ctf_dynhash_insert (fp
->ctf_dthash
, (void *) dtd
->dtd_type
, dtd
) < 0)
523 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
524 if (ctf_dynhash_insert (fp
->ctf_dtbyname
,
525 ctf_prefixed_name (kind
, dtd
->dtd_name
),
529 ctf_list_append (&fp
->ctf_dtdefs
, dtd
);
534 ctf_dtd_delete (ctf_file_t
*fp
, ctf_dtdef_t
*dtd
)
536 ctf_dmdef_t
*dmd
, *nmd
;
537 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
539 ctf_dynhash_remove (fp
->ctf_dthash
, (void *) dtd
->dtd_type
);
546 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
547 dmd
!= NULL
; dmd
= nmd
)
549 if (dmd
->dmd_name
!= NULL
)
550 ctf_free (dmd
->dmd_name
);
551 nmd
= ctf_list_next (dmd
);
556 ctf_free (dtd
->dtd_u
.dtu_argv
);
564 name
= ctf_prefixed_name (kind
, dtd
->dtd_name
);
565 ctf_dynhash_remove (fp
->ctf_dtbyname
, name
);
567 ctf_free (dtd
->dtd_name
);
570 ctf_list_delete (&fp
->ctf_dtdefs
, dtd
);
575 ctf_dtd_lookup (const ctf_file_t
*fp
, ctf_id_t type
)
577 return (ctf_dtdef_t
*) ctf_dynhash_lookup (fp
->ctf_dthash
, (void *) type
);
581 ctf_dtd_lookup_type_by_name (ctf_file_t
*fp
, int kind
, const char *name
)
584 char *decorated
= ctf_prefixed_name (kind
, name
);
586 dtd
= (ctf_dtdef_t
*) ctf_dynhash_lookup (fp
->ctf_dtbyname
, decorated
);
590 return dtd
->dtd_type
;
596 ctf_dynamic_type (const ctf_file_t
*fp
, ctf_id_t id
)
600 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, id
))
603 idx
= LCTF_TYPE_TO_INDEX(fp
, id
);
605 if (((unsigned long) idx
> fp
->ctf_typemax
) &&
606 ((unsigned long) idx
< fp
->ctf_dtnextid
))
607 return ctf_dtd_lookup (fp
, id
);
612 ctf_dvd_insert (ctf_file_t
*fp
, ctf_dvdef_t
*dvd
)
614 if (ctf_dynhash_insert (fp
->ctf_dvhash
, dvd
->dvd_name
, dvd
) < 0)
616 ctf_list_append (&fp
->ctf_dvdefs
, dvd
);
621 ctf_dvd_delete (ctf_file_t
*fp
, ctf_dvdef_t
*dvd
)
623 ctf_dynhash_remove (fp
->ctf_dvhash
, dvd
->dvd_name
);
624 ctf_free (dvd
->dvd_name
);
626 ctf_list_delete (&fp
->ctf_dvdefs
, dvd
);
631 ctf_dvd_lookup (const ctf_file_t
*fp
, const char *name
)
633 return (ctf_dvdef_t
*) ctf_dynhash_lookup (fp
->ctf_dvhash
, name
);
636 /* Discard all of the dynamic type definitions and variable definitions that
637 have been added to the container since the last call to ctf_update(). We
638 locate such types by scanning the dtd list and deleting elements that have
639 type IDs greater than ctf_dtoldid, which is set by ctf_update(), above, and
640 by scanning the variable list and deleting elements that have update IDs
641 equal to the current value of the last-update snapshot count (indicating that
642 they were added after the most recent call to ctf_update()). */
644 ctf_discard (ctf_file_t
*fp
)
646 ctf_snapshot_id_t last_update
=
648 fp
->ctf_snapshot_lu
+ 1 };
650 /* Update required? */
651 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
654 return (ctf_rollback (fp
, last_update
));
658 ctf_snapshot (ctf_file_t
*fp
)
660 ctf_snapshot_id_t snapid
;
661 snapid
.dtd_id
= fp
->ctf_dtnextid
- 1;
662 snapid
.snapshot_id
= fp
->ctf_snapshots
++;
666 /* Like ctf_discard(), only discards everything after a particular ID. */
668 ctf_rollback (ctf_file_t
*fp
, ctf_snapshot_id_t id
)
670 ctf_dtdef_t
*dtd
, *ntd
;
671 ctf_dvdef_t
*dvd
, *nvd
;
673 if (!(fp
->ctf_flags
& LCTF_RDWR
))
674 return (ctf_set_errno (fp
, ECTF_RDONLY
));
676 if (fp
->ctf_dtoldid
> id
.dtd_id
)
677 return (ctf_set_errno (fp
, ECTF_OVERROLLBACK
));
679 if (fp
->ctf_snapshot_lu
>= id
.snapshot_id
)
680 return (ctf_set_errno (fp
, ECTF_OVERROLLBACK
));
682 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
); dtd
!= NULL
; dtd
= ntd
)
684 ntd
= ctf_list_next (dtd
);
686 if (LCTF_TYPE_TO_INDEX (fp
, dtd
->dtd_type
) <= id
.dtd_id
)
689 ctf_dtd_delete (fp
, dtd
);
692 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
; dvd
= nvd
)
694 nvd
= ctf_list_next (dvd
);
696 if (dvd
->dvd_snapshots
<= id
.snapshot_id
)
699 ctf_dvd_delete (fp
, dvd
);
702 fp
->ctf_dtnextid
= id
.dtd_id
+ 1;
703 fp
->ctf_snapshots
= id
.snapshot_id
;
705 if (fp
->ctf_snapshots
== fp
->ctf_snapshot_lu
)
706 fp
->ctf_flags
&= ~LCTF_DIRTY
;
712 ctf_add_generic (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
719 if (flag
!= CTF_ADD_NONROOT
&& flag
!= CTF_ADD_ROOT
)
720 return (ctf_set_errno (fp
, EINVAL
));
722 if (!(fp
->ctf_flags
& LCTF_RDWR
))
723 return (ctf_set_errno (fp
, ECTF_RDONLY
));
725 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_dtnextid
, 1) > CTF_MAX_TYPE
)
726 return (ctf_set_errno (fp
, ECTF_FULL
));
728 if (LCTF_INDEX_TO_TYPE (fp
, fp
->ctf_dtnextid
, 1) == CTF_MAX_PTYPE
)
729 return (ctf_set_errno (fp
, ECTF_FULL
));
731 if ((dtd
= ctf_alloc (sizeof (ctf_dtdef_t
))) == NULL
)
732 return (ctf_set_errno (fp
, EAGAIN
));
734 if (name
!= NULL
&& (s
= ctf_strdup (name
)) == NULL
)
737 return (ctf_set_errno (fp
, EAGAIN
));
740 type
= fp
->ctf_dtnextid
++;
741 type
= LCTF_INDEX_TO_TYPE (fp
, type
, (fp
->ctf_flags
& LCTF_CHILD
));
743 memset (dtd
, 0, sizeof (ctf_dtdef_t
));
745 dtd
->dtd_type
= type
;
747 if (ctf_dtd_insert (fp
, dtd
) < 0)
750 return CTF_ERR
; /* errno is set for us. */
752 fp
->ctf_flags
|= LCTF_DIRTY
;
758 /* When encoding integer sizes, we want to convert a byte count in the range
759 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
760 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
776 ctf_add_encoded (ctf_file_t
*fp
, uint32_t flag
,
777 const char *name
, const ctf_encoding_t
*ep
, uint32_t kind
)
783 return (ctf_set_errno (fp
, EINVAL
));
785 if ((type
= ctf_add_generic (fp
, flag
, name
, &dtd
)) == CTF_ERR
)
786 return CTF_ERR
; /* errno is set for us. */
788 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
789 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
791 dtd
->dtd_u
.dtu_enc
= *ep
;
797 ctf_add_reftype (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
, uint32_t kind
)
801 ctf_file_t
*tmp
= fp
;
803 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
804 return (ctf_set_errno (fp
, EINVAL
));
806 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
807 return CTF_ERR
; /* errno is set for us. */
809 if ((type
= ctf_add_generic (fp
, flag
, NULL
, &dtd
)) == CTF_ERR
)
810 return CTF_ERR
; /* errno is set for us. */
812 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, 0);
813 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
819 ctf_add_slice (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
,
820 const ctf_encoding_t
*ep
)
825 const ctf_type_t
*tp
;
826 ctf_file_t
*tmp
= fp
;
829 return (ctf_set_errno (fp
, EINVAL
));
831 if ((ep
->cte_bits
> 255) || (ep
->cte_offset
> 255))
832 return (ctf_set_errno (fp
, ECTF_SLICEOVERFLOW
));
834 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
835 return (ctf_set_errno (fp
, EINVAL
));
837 if ((tp
= ctf_lookup_by_id (&tmp
, ref
)) == NULL
)
838 return CTF_ERR
; /* errno is set for us. */
840 kind
= ctf_type_kind_unsliced (tmp
, ref
);
841 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) &&
842 (kind
!= CTF_K_ENUM
))
843 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
845 if ((type
= ctf_add_generic (fp
, flag
, NULL
, &dtd
)) == CTF_ERR
)
846 return CTF_ERR
; /* errno is set for us. */
848 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_SLICE
, flag
, 0);
849 dtd
->dtd_data
.ctt_size
= clp2 (P2ROUNDUP (ep
->cte_bits
, CHAR_BIT
)
851 dtd
->dtd_u
.dtu_slice
.cts_type
= ref
;
852 dtd
->dtd_u
.dtu_slice
.cts_bits
= ep
->cte_bits
;
853 dtd
->dtd_u
.dtu_slice
.cts_offset
= ep
->cte_offset
;
859 ctf_add_integer (ctf_file_t
*fp
, uint32_t flag
,
860 const char *name
, const ctf_encoding_t
*ep
)
862 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_INTEGER
));
866 ctf_add_float (ctf_file_t
*fp
, uint32_t flag
,
867 const char *name
, const ctf_encoding_t
*ep
)
869 return (ctf_add_encoded (fp
, flag
, name
, ep
, CTF_K_FLOAT
));
873 ctf_add_pointer (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
875 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_POINTER
));
879 ctf_add_array (ctf_file_t
*fp
, uint32_t flag
, const ctf_arinfo_t
*arp
)
883 ctf_file_t
*tmp
= fp
;
886 return (ctf_set_errno (fp
, EINVAL
));
888 if (ctf_lookup_by_id (&tmp
, arp
->ctr_contents
) == NULL
)
889 return CTF_ERR
; /* errno is set for us. */
892 if (ctf_lookup_by_id (&tmp
, arp
->ctr_index
) == NULL
)
893 return CTF_ERR
; /* errno is set for us. */
895 if ((type
= ctf_add_generic (fp
, flag
, NULL
, &dtd
)) == CTF_ERR
)
896 return CTF_ERR
; /* errno is set for us. */
898 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ARRAY
, flag
, 0);
899 dtd
->dtd_data
.ctt_size
= 0;
900 dtd
->dtd_u
.dtu_arr
= *arp
;
906 ctf_set_array (ctf_file_t
*fp
, ctf_id_t type
, const ctf_arinfo_t
*arp
)
908 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
910 if (!(fp
->ctf_flags
& LCTF_RDWR
))
911 return (ctf_set_errno (fp
, ECTF_RDONLY
));
914 || LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
) != CTF_K_ARRAY
)
915 return (ctf_set_errno (fp
, ECTF_BADID
));
917 fp
->ctf_flags
|= LCTF_DIRTY
;
918 dtd
->dtd_u
.dtu_arr
= *arp
;
924 ctf_add_function (ctf_file_t
*fp
, uint32_t flag
,
925 const ctf_funcinfo_t
*ctc
, const ctf_id_t
*argv
)
930 ctf_id_t
*vdat
= NULL
;
931 ctf_file_t
*tmp
= fp
;
934 if (ctc
== NULL
|| (ctc
->ctc_flags
& ~CTF_FUNC_VARARG
) != 0
935 || (ctc
->ctc_argc
!= 0 && argv
== NULL
))
936 return (ctf_set_errno (fp
, EINVAL
));
938 vlen
= ctc
->ctc_argc
;
939 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
940 vlen
++; /* Add trailing zero to indicate varargs (see below). */
942 if (ctf_lookup_by_id (&tmp
, ctc
->ctc_return
) == NULL
)
943 return CTF_ERR
; /* errno is set for us. */
945 for (i
= 0; i
< ctc
->ctc_argc
; i
++)
948 if (ctf_lookup_by_id (&tmp
, argv
[i
]) == NULL
)
949 return CTF_ERR
; /* errno is set for us. */
952 if (vlen
> CTF_MAX_VLEN
)
953 return (ctf_set_errno (fp
, EOVERFLOW
));
955 if (vlen
!= 0 && (vdat
= ctf_alloc (sizeof (ctf_id_t
) * vlen
)) == NULL
)
956 return (ctf_set_errno (fp
, EAGAIN
));
958 if ((type
= ctf_add_generic (fp
, flag
, NULL
, &dtd
)) == CTF_ERR
)
961 return CTF_ERR
; /* errno is set for us. */
964 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FUNCTION
, flag
, vlen
);
965 dtd
->dtd_data
.ctt_type
= (uint32_t) ctc
->ctc_return
;
967 memcpy (vdat
, argv
, sizeof (ctf_id_t
) * ctc
->ctc_argc
);
968 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
969 vdat
[vlen
- 1] = 0; /* Add trailing zero to indicate varargs. */
970 dtd
->dtd_u
.dtu_argv
= vdat
;
976 ctf_add_struct_sized (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
979 ctf_hash_t
*hp
= fp
->ctf_structs
;
983 /* Promote forwards to structs. */
987 type
= ctf_hash_lookup_type (hp
, fp
, name
);
989 type
= ctf_dtd_lookup_type_by_name (fp
, CTF_K_STRUCT
, name
);
992 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
993 dtd
= ctf_dtd_lookup (fp
, type
);
994 else if ((type
= ctf_add_generic (fp
, flag
, name
, &dtd
)) == CTF_ERR
)
995 return CTF_ERR
; /* errno is set for us. */
997 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_STRUCT
, flag
, 0);
999 if (size
> CTF_MAX_SIZE
)
1001 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1002 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1003 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1006 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1012 ctf_add_struct (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1014 return (ctf_add_struct_sized (fp
, flag
, name
, 0));
1018 ctf_add_union_sized (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1021 ctf_hash_t
*hp
= fp
->ctf_unions
;
1025 /* Promote forwards to unions. */
1028 type
= ctf_hash_lookup_type (hp
, fp
, name
);
1030 type
= ctf_dtd_lookup_type_by_name (fp
, CTF_K_UNION
, name
);
1033 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1034 dtd
= ctf_dtd_lookup (fp
, type
);
1035 else if ((type
= ctf_add_generic (fp
, flag
, name
, &dtd
)) == CTF_ERR
)
1036 return CTF_ERR
; /* errno is set for us */
1038 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_UNION
, flag
, 0);
1040 if (size
> CTF_MAX_SIZE
)
1042 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1043 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1044 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1047 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1053 ctf_add_union (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1055 return (ctf_add_union_sized (fp
, flag
, name
, 0));
1059 ctf_add_enum (ctf_file_t
*fp
, uint32_t flag
, const char *name
)
1061 ctf_hash_t
*hp
= fp
->ctf_enums
;
1065 /* Promote forwards to enums. */
1068 type
= ctf_hash_lookup_type (hp
, fp
, name
);
1070 type
= ctf_dtd_lookup_type_by_name (fp
, CTF_K_ENUM
, name
);
1073 if (type
!= 0 && ctf_type_kind (fp
, type
) == CTF_K_FORWARD
)
1074 dtd
= ctf_dtd_lookup (fp
, type
);
1075 else if ((type
= ctf_add_generic (fp
, flag
, name
, &dtd
)) == CTF_ERR
)
1076 return CTF_ERR
; /* errno is set for us. */
1078 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_ENUM
, flag
, 0);
1079 dtd
->dtd_data
.ctt_size
= fp
->ctf_dmodel
->ctd_int
;
1085 ctf_add_enum_encoded (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1086 const ctf_encoding_t
*ep
)
1088 ctf_hash_t
*hp
= fp
->ctf_enums
;
1091 /* First, create the enum if need be, using most of the same machinery as
1092 ctf_add_enum(), to ensure that we do not allow things past that are not
1093 enums or forwards to them. (This includes other slices: you cannot slice a
1094 slice, which would be a useless thing to do anyway.) */
1098 type
= ctf_hash_lookup_type (hp
, fp
, name
);
1100 type
= ctf_dtd_lookup_type_by_name (fp
, CTF_K_ENUM
, name
);
1105 if ((ctf_type_kind (fp
, type
) != CTF_K_FORWARD
) &&
1106 (ctf_type_kind_unsliced (fp
, type
) != CTF_K_ENUM
))
1107 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1109 else if ((type
= ctf_add_enum (fp
, flag
, name
)) == CTF_ERR
)
1110 return CTF_ERR
; /* errno is set for us. */
1112 /* Now attach a suitable slice to it. */
1114 return ctf_add_slice (fp
, flag
, type
, ep
);
1118 ctf_add_forward (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1128 hp
= fp
->ctf_structs
;
1131 hp
= fp
->ctf_unions
;
1137 return (ctf_set_errno (fp
, ECTF_NOTSUE
));
1140 /* If the type is already defined or exists as a forward tag, just
1141 return the ctf_id_t of the existing definition. */
1145 if (((type
= ctf_hash_lookup_type (hp
, fp
, name
)) != 0)
1146 || (type
= ctf_dtd_lookup_type_by_name (fp
, kind
, name
)) != 0)
1150 if ((type
= ctf_add_generic (fp
, flag
, name
, &dtd
)) == CTF_ERR
)
1151 return CTF_ERR
; /* errno is set for us. */
1153 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_FORWARD
, flag
, 0);
1154 dtd
->dtd_data
.ctt_type
= kind
;
1160 ctf_add_typedef (ctf_file_t
*fp
, uint32_t flag
, const char *name
,
1165 ctf_file_t
*tmp
= fp
;
1167 if (ref
== CTF_ERR
|| ref
> CTF_MAX_TYPE
)
1168 return (ctf_set_errno (fp
, EINVAL
));
1170 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1171 return CTF_ERR
; /* errno is set for us. */
1173 if ((type
= ctf_add_generic (fp
, flag
, name
, &dtd
)) == CTF_ERR
)
1174 return CTF_ERR
; /* errno is set for us. */
1176 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (CTF_K_TYPEDEF
, flag
, 0);
1177 dtd
->dtd_data
.ctt_type
= (uint32_t) ref
;
1183 ctf_add_volatile (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1185 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_VOLATILE
));
1189 ctf_add_const (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1191 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_CONST
));
1195 ctf_add_restrict (ctf_file_t
*fp
, uint32_t flag
, ctf_id_t ref
)
1197 return (ctf_add_reftype (fp
, flag
, ref
, CTF_K_RESTRICT
));
1201 ctf_add_enumerator (ctf_file_t
*fp
, ctf_id_t enid
, const char *name
,
1204 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, enid
);
1207 uint32_t kind
, vlen
, root
;
1211 return (ctf_set_errno (fp
, EINVAL
));
1213 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1214 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1217 return (ctf_set_errno (fp
, ECTF_BADID
));
1219 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1220 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1221 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1223 if (kind
!= CTF_K_ENUM
)
1224 return (ctf_set_errno (fp
, ECTF_NOTENUM
));
1226 if (vlen
== CTF_MAX_VLEN
)
1227 return (ctf_set_errno (fp
, ECTF_DTFULL
));
1229 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1230 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1232 if (strcmp (dmd
->dmd_name
, name
) == 0)
1233 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1236 if ((dmd
= ctf_alloc (sizeof (ctf_dmdef_t
))) == NULL
)
1237 return (ctf_set_errno (fp
, EAGAIN
));
1239 if ((s
= ctf_strdup (name
)) == NULL
)
1242 return (ctf_set_errno (fp
, EAGAIN
));
1246 dmd
->dmd_type
= CTF_ERR
;
1247 dmd
->dmd_offset
= 0;
1248 dmd
->dmd_value
= value
;
1250 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1251 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
1253 fp
->ctf_flags
|= LCTF_DIRTY
;
1259 ctf_add_member_offset (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1260 ctf_id_t type
, unsigned long bit_offset
)
1262 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, souid
);
1265 ssize_t msize
, malign
, ssize
;
1266 uint32_t kind
, vlen
, root
;
1269 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1270 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1273 return (ctf_set_errno (fp
, ECTF_BADID
));
1275 kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1276 root
= LCTF_INFO_ISROOT (fp
, dtd
->dtd_data
.ctt_info
);
1277 vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
1279 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
1280 return (ctf_set_errno (fp
, ECTF_NOTSOU
));
1282 if (vlen
== CTF_MAX_VLEN
)
1283 return (ctf_set_errno (fp
, ECTF_DTFULL
));
1287 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1288 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1290 if (dmd
->dmd_name
!= NULL
&& strcmp (dmd
->dmd_name
, name
) == 0)
1291 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1295 if ((msize
= ctf_type_size (fp
, type
)) < 0 ||
1296 (malign
= ctf_type_align (fp
, type
)) < 0)
1297 return -1; /* errno is set for us. */
1299 if ((dmd
= ctf_alloc (sizeof (ctf_dmdef_t
))) == NULL
)
1300 return (ctf_set_errno (fp
, EAGAIN
));
1302 if (name
!= NULL
&& (s
= ctf_strdup (name
)) == NULL
)
1305 return (ctf_set_errno (fp
, EAGAIN
));
1309 dmd
->dmd_type
= type
;
1310 dmd
->dmd_value
= -1;
1312 if (kind
== CTF_K_STRUCT
&& vlen
!= 0)
1314 if (bit_offset
== (unsigned long) - 1)
1316 /* Natural alignment. */
1318 ctf_dmdef_t
*lmd
= ctf_list_prev (&dtd
->dtd_u
.dtu_members
);
1319 ctf_id_t ltype
= ctf_type_resolve (fp
, lmd
->dmd_type
);
1320 size_t off
= lmd
->dmd_offset
;
1322 ctf_encoding_t linfo
;
1325 if (ctf_type_encoding (fp
, ltype
, &linfo
) == 0)
1326 off
+= linfo
.cte_bits
;
1327 else if ((lsize
= ctf_type_size (fp
, ltype
)) > 0)
1328 off
+= lsize
* CHAR_BIT
;
1330 /* Round up the offset of the end of the last member to
1331 the next byte boundary, convert 'off' to bytes, and
1332 then round it up again to the next multiple of the
1333 alignment required by the new member. Finally,
1334 convert back to bits and store the result in
1335 dmd_offset. Technically we could do more efficient
1336 packing if the new member is a bit-field, but we're
1337 the "compiler" and ANSI says we can do as we choose. */
1339 off
= roundup (off
, CHAR_BIT
) / CHAR_BIT
;
1340 off
= roundup (off
, MAX (malign
, 1));
1341 dmd
->dmd_offset
= off
* CHAR_BIT
;
1342 ssize
= off
+ msize
;
1346 /* Specified offset in bits. */
1348 dmd
->dmd_offset
= bit_offset
;
1349 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1350 ssize
= MAX (ssize
, ((signed) bit_offset
/ CHAR_BIT
) + msize
);
1355 dmd
->dmd_offset
= 0;
1356 ssize
= ctf_get_ctt_size (fp
, &dtd
->dtd_data
, NULL
, NULL
);
1357 ssize
= MAX (ssize
, msize
);
1360 if ((size_t) ssize
> CTF_MAX_SIZE
)
1362 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1363 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (ssize
);
1364 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (ssize
);
1367 dtd
->dtd_data
.ctt_size
= (uint32_t) ssize
;
1369 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, root
, vlen
+ 1);
1370 ctf_list_append (&dtd
->dtd_u
.dtu_members
, dmd
);
1372 fp
->ctf_flags
|= LCTF_DIRTY
;
1377 ctf_add_member_encoded (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1378 ctf_id_t type
, unsigned long bit_offset
,
1379 const ctf_encoding_t encoding
)
1381 ctf_dtdef_t
*dtd
= ctf_dtd_lookup (fp
, type
);
1382 int kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
1385 if ((kind
!= CTF_K_INTEGER
) && (kind
!= CTF_K_FLOAT
) && (kind
!= CTF_K_ENUM
))
1386 return (ctf_set_errno (fp
, ECTF_NOTINTFP
));
1388 if ((type
= ctf_add_slice (fp
, CTF_ADD_NONROOT
, otype
, &encoding
)) == CTF_ERR
)
1389 return -1; /* errno is set for us. */
1391 return ctf_add_member_offset (fp
, souid
, name
, type
, bit_offset
);
1395 ctf_add_member (ctf_file_t
*fp
, ctf_id_t souid
, const char *name
,
1398 return ctf_add_member_offset (fp
, souid
, name
, type
, (unsigned long) - 1);
1402 ctf_add_variable (ctf_file_t
*fp
, const char *name
, ctf_id_t ref
)
1405 ctf_file_t
*tmp
= fp
;
1407 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1408 return (ctf_set_errno (fp
, ECTF_RDONLY
));
1410 if (ctf_dvd_lookup (fp
, name
) != NULL
)
1411 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1413 if (ctf_lookup_by_id (&tmp
, ref
) == NULL
)
1414 return -1; /* errno is set for us. */
1416 if ((dvd
= ctf_alloc (sizeof (ctf_dvdef_t
))) == NULL
)
1417 return (ctf_set_errno (fp
, EAGAIN
));
1419 if (name
!= NULL
&& (dvd
->dvd_name
= ctf_strdup (name
)) == NULL
)
1422 return (ctf_set_errno (fp
, EAGAIN
));
1424 dvd
->dvd_type
= ref
;
1425 dvd
->dvd_snapshots
= fp
->ctf_snapshots
;
1427 if (ctf_dvd_insert (fp
, dvd
) < 0)
1430 return -1; /* errno is set for us. */
1433 fp
->ctf_flags
|= LCTF_DIRTY
;
1438 enumcmp (const char *name
, int value
, void *arg
)
1440 ctf_bundle_t
*ctb
= arg
;
1443 if (ctf_enum_value (ctb
->ctb_file
, ctb
->ctb_type
, name
, &bvalue
) < 0)
1445 ctf_dprintf ("Conflict due to member %s iteration error.\n", name
);
1448 if (value
!= bvalue
)
1450 ctf_dprintf ("Conflict due to value change: %i versus %i\n",
1458 enumadd (const char *name
, int value
, void *arg
)
1460 ctf_bundle_t
*ctb
= arg
;
1462 return (ctf_add_enumerator (ctb
->ctb_file
, ctb
->ctb_type
,
1467 membcmp (const char *name
, ctf_id_t type _libctf_unused_
, unsigned long offset
,
1470 ctf_bundle_t
*ctb
= arg
;
1473 if (ctf_member_info (ctb
->ctb_file
, ctb
->ctb_type
, name
, &ctm
) < 0)
1475 ctf_dprintf ("Conflict due to member %s iteration error.\n", name
);
1478 if (ctm
.ctm_offset
!= offset
)
1480 ctf_dprintf ("Conflict due to member %s offset change: "
1481 "%lx versus %lx\n", name
, ctm
.ctm_offset
, offset
);
1488 membadd (const char *name
, ctf_id_t type
, unsigned long offset
, void *arg
)
1490 ctf_bundle_t
*ctb
= arg
;
1494 if ((dmd
= ctf_alloc (sizeof (ctf_dmdef_t
))) == NULL
)
1495 return (ctf_set_errno (ctb
->ctb_file
, EAGAIN
));
1497 if (name
!= NULL
&& (s
= ctf_strdup (name
)) == NULL
)
1500 return (ctf_set_errno (ctb
->ctb_file
, EAGAIN
));
1503 /* For now, dmd_type is copied as the src_fp's type; it is reset to an
1504 equivalent dst_fp type by a final loop in ctf_add_type(), below. */
1506 dmd
->dmd_type
= type
;
1507 dmd
->dmd_offset
= offset
;
1508 dmd
->dmd_value
= -1;
1510 ctf_list_append (&ctb
->ctb_dtd
->dtd_u
.dtu_members
, dmd
);
1512 ctb
->ctb_file
->ctf_flags
|= LCTF_DIRTY
;
1516 /* The ctf_add_type routine is used to copy a type from a source CTF container
1517 to a dynamic destination container. This routine operates recursively by
1518 following the source type's links and embedded member types. If the
1519 destination container already contains a named type which has the same
1520 attributes, then we succeed and return this type but no changes occur. */
1522 ctf_add_type (ctf_file_t
*dst_fp
, ctf_file_t
*src_fp
, ctf_id_t src_type
)
1524 ctf_id_t dst_type
= CTF_ERR
;
1525 uint32_t dst_kind
= CTF_K_UNKNOWN
;
1529 uint32_t kind
, flag
, vlen
;
1531 const ctf_type_t
*src_tp
, *dst_tp
;
1532 ctf_bundle_t src
, dst
;
1533 ctf_encoding_t src_en
, dst_en
;
1534 ctf_arinfo_t src_ar
, dst_ar
;
1541 if (!(dst_fp
->ctf_flags
& LCTF_RDWR
))
1542 return (ctf_set_errno (dst_fp
, ECTF_RDONLY
));
1544 if ((src_tp
= ctf_lookup_by_id (&src_fp
, src_type
)) == NULL
)
1545 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1547 name
= ctf_strptr (src_fp
, src_tp
->ctt_name
);
1548 kind
= LCTF_INFO_KIND (src_fp
, src_tp
->ctt_info
);
1549 flag
= LCTF_INFO_ISROOT (src_fp
, src_tp
->ctt_info
);
1550 vlen
= LCTF_INFO_VLEN (src_fp
, src_tp
->ctt_info
);
1555 hp
= dst_fp
->ctf_structs
;
1558 hp
= dst_fp
->ctf_unions
;
1561 hp
= dst_fp
->ctf_enums
;
1564 hp
= dst_fp
->ctf_names
;
1568 /* If the source type has a name and is a root type (visible at the
1569 top-level scope), lookup the name in the destination container and
1570 verify that it is of the same kind before we do anything else. */
1572 if ((flag
& CTF_ADD_ROOT
) && name
[0] != '\0'
1573 && (tmp
= ctf_hash_lookup_type (hp
, dst_fp
, name
)) != 0)
1576 dst_kind
= ctf_type_kind_unsliced (dst_fp
, dst_type
);
1579 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
1580 unless dst_type is a forward declaration and src_type is a struct,
1581 union, or enum (i.e. the definition of the previous forward decl). */
1583 if (dst_type
!= CTF_ERR
&& dst_kind
!= kind
1584 && (dst_kind
!= CTF_K_FORWARD
1585 || (kind
!= CTF_K_ENUM
&& kind
!= CTF_K_STRUCT
1586 && kind
!= CTF_K_UNION
)))
1588 ctf_dprintf ("Conflict for type %s: kinds differ, new: %i; "
1589 "old (ID %lx): %i\n", name
, kind
, dst_type
, dst_kind
);
1590 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1593 /* We take special action for an integer, float, or slice since it is
1594 described not only by its name but also its encoding. For integers,
1595 bit-fields exploit this degeneracy. */
1597 if (kind
== CTF_K_INTEGER
|| kind
== CTF_K_FLOAT
|| kind
== CTF_K_SLICE
)
1599 if (ctf_type_encoding (src_fp
, src_type
, &src_en
) != 0)
1600 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1602 if (dst_type
!= CTF_ERR
)
1604 ctf_file_t
*fp
= dst_fp
;
1606 if ((dst_tp
= ctf_lookup_by_id (&fp
, dst_type
)) == NULL
)
1609 if (LCTF_INFO_ISROOT (fp
, dst_tp
->ctt_info
) & CTF_ADD_ROOT
)
1611 /* The type that we found in the hash is also root-visible. If
1612 the two types match then use the existing one; otherwise,
1613 declare a conflict. Note: slices are not certain to match
1614 even if there is no conflict: we must check the contained type
1617 if (ctf_type_encoding (dst_fp
, dst_type
, &dst_en
) != 0)
1618 return CTF_ERR
; /* errno set for us. */
1620 if (memcmp (&src_en
, &dst_en
, sizeof (ctf_encoding_t
)) == 0)
1622 if (kind
!= CTF_K_SLICE
)
1627 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1632 /* We found a non-root-visible type in the hash. We reset
1633 dst_type to ensure that we continue to look for a possible
1634 conflict in the pending list. */
1641 /* If the non-empty name was not found in the appropriate hash, search
1642 the list of pending dynamic definitions that are not yet committed.
1643 If a matching name and kind are found, assume this is the type that
1644 we are looking for. This is necessary to permit ctf_add_type() to
1645 operate recursively on entities such as a struct that contains a
1646 pointer member that refers to the same struct type. */
1648 if (dst_type
== CTF_ERR
&& name
[0] != '\0')
1650 for (dtd
= ctf_list_prev (&dst_fp
->ctf_dtdefs
); dtd
!= NULL
1651 && LCTF_TYPE_TO_INDEX (src_fp
, dtd
->dtd_type
) > dst_fp
->ctf_dtoldid
;
1652 dtd
= ctf_list_prev (dtd
))
1654 if (LCTF_INFO_KIND (src_fp
, dtd
->dtd_data
.ctt_info
) == kind
1655 && dtd
->dtd_name
!= NULL
&& strcmp (dtd
->dtd_name
, name
) == 0)
1657 int sroot
; /* Is the src root-visible? */
1658 int droot
; /* Is the dst root-visible? */
1659 int match
; /* Do the encodings match? */
1661 if (kind
!= CTF_K_INTEGER
&& kind
!= CTF_K_FLOAT
&& kind
!= CTF_K_SLICE
)
1662 return dtd
->dtd_type
;
1664 sroot
= (flag
& CTF_ADD_ROOT
);
1665 droot
= (LCTF_INFO_ISROOT (dst_fp
,
1667 ctt_info
) & CTF_ADD_ROOT
);
1669 match
= (memcmp (&src_en
, &dtd
->dtd_u
.dtu_enc
,
1670 sizeof (ctf_encoding_t
)) == 0);
1672 /* If the types share the same encoding then return the id of the
1673 first unless one type is root-visible and the other is not; in
1674 that case the new type must get a new id if a match is never
1675 found. Note: slices are not certain to match even if there is
1676 no conflict: we must check the contained type too. */
1678 if (match
&& sroot
== droot
)
1680 if (kind
!= CTF_K_SLICE
)
1681 return dtd
->dtd_type
;
1683 else if (!match
&& sroot
&& droot
)
1685 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1691 src
.ctb_file
= src_fp
;
1692 src
.ctb_type
= src_type
;
1695 dst
.ctb_file
= dst_fp
;
1696 dst
.ctb_type
= dst_type
;
1699 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then
1700 we add a new type with the same properties as src_type to dst_fp.
1701 If dst_type is not CTF_ERR, then we verify that dst_type has the
1702 same attributes as src_type. We recurse for embedded references. */
1706 /* If we found a match we will have either returned it or declared a
1708 dst_type
= ctf_add_integer (dst_fp
, flag
, name
, &src_en
);
1712 /* If we found a match we will have either returned it or declared a
1714 dst_type
= ctf_add_float (dst_fp
, flag
, name
, &src_en
);
1718 /* We have checked for conflicting encodings: now try to add the
1720 src_type
= ctf_type_reference (src_fp
, src_type
);
1721 dst_type
= ctf_add_type (dst_fp
, src_fp
, src_type
);
1723 if (src_type
== CTF_ERR
)
1724 return CTF_ERR
; /* errno is set for us. */
1726 dst_type
= ctf_add_slice (dst_fp
, flag
, src_type
, &src_en
);
1730 case CTF_K_VOLATILE
:
1732 case CTF_K_RESTRICT
:
1733 src_type
= ctf_type_reference (src_fp
, src_type
);
1734 src_type
= ctf_add_type (dst_fp
, src_fp
, src_type
);
1736 if (src_type
== CTF_ERR
)
1737 return CTF_ERR
; /* errno is set for us. */
1739 dst_type
= ctf_add_reftype (dst_fp
, flag
, src_type
, kind
);
1743 if (ctf_array_info (src_fp
, src_type
, &src_ar
) != 0)
1744 return (ctf_set_errno (dst_fp
, ctf_errno (src_fp
)));
1746 src_ar
.ctr_contents
=
1747 ctf_add_type (dst_fp
, src_fp
, src_ar
.ctr_contents
);
1748 src_ar
.ctr_index
= ctf_add_type (dst_fp
, src_fp
, src_ar
.ctr_index
);
1749 src_ar
.ctr_nelems
= src_ar
.ctr_nelems
;
1751 if (src_ar
.ctr_contents
== CTF_ERR
|| src_ar
.ctr_index
== CTF_ERR
)
1752 return CTF_ERR
; /* errno is set for us. */
1754 if (dst_type
!= CTF_ERR
)
1756 if (ctf_array_info (dst_fp
, dst_type
, &dst_ar
) != 0)
1757 return CTF_ERR
; /* errno is set for us. */
1759 if (memcmp (&src_ar
, &dst_ar
, sizeof (ctf_arinfo_t
)))
1761 ctf_dprintf ("Conflict for type %s against ID %lx: "
1762 "array info differs, old %lx/%lx/%x; "
1763 "new: %lx/%lx/%x\n", name
, dst_type
,
1764 src_ar
.ctr_contents
, src_ar
.ctr_index
,
1765 src_ar
.ctr_nelems
, dst_ar
.ctr_contents
,
1766 dst_ar
.ctr_index
, dst_ar
.ctr_nelems
);
1767 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1771 dst_type
= ctf_add_array (dst_fp
, flag
, &src_ar
);
1774 case CTF_K_FUNCTION
:
1775 ctc
.ctc_return
= ctf_add_type (dst_fp
, src_fp
, src_tp
->ctt_type
);
1779 if (ctc
.ctc_return
== CTF_ERR
)
1780 return CTF_ERR
; /* errno is set for us. */
1782 dst_type
= ctf_add_function (dst_fp
, flag
, &ctc
, NULL
);
1793 /* Technically to match a struct or union we need to check both
1794 ways (src members vs. dst, dst members vs. src) but we make
1795 this more optimal by only checking src vs. dst and comparing
1796 the total size of the structure (which we must do anyway)
1797 which covers the possibility of dst members not in src.
1798 This optimization can be defeated for unions, but is so
1799 pathological as to render it irrelevant for our purposes. */
1801 if (dst_type
!= CTF_ERR
&& dst_kind
!= CTF_K_FORWARD
)
1803 if (ctf_type_size (src_fp
, src_type
) !=
1804 ctf_type_size (dst_fp
, dst_type
))
1806 ctf_dprintf ("Conflict for type %s against ID %lx: "
1807 "union size differs, old %li, new %li\n",
1809 (long) ctf_type_size (src_fp
, src_type
),
1810 (long) ctf_type_size (dst_fp
, dst_type
));
1811 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1814 if (ctf_member_iter (src_fp
, src_type
, membcmp
, &dst
))
1816 ctf_dprintf ("Conflict for type %s against ID %lx: "
1817 "members differ, see above\n", name
, dst_type
);
1818 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1824 /* Unlike the other cases, copying structs and unions is done
1825 manually so as to avoid repeated lookups in ctf_add_member
1826 and to ensure the exact same member offsets as in src_type. */
1828 dst_type
= ctf_add_generic (dst_fp
, flag
, name
, &dtd
);
1829 if (dst_type
== CTF_ERR
)
1830 return CTF_ERR
; /* errno is set for us. */
1832 dst
.ctb_type
= dst_type
;
1835 if (ctf_member_iter (src_fp
, src_type
, membadd
, &dst
) != 0)
1836 errs
++; /* Increment errs and fail at bottom of case. */
1838 if ((ssize
= ctf_type_size (src_fp
, src_type
)) < 0)
1839 return CTF_ERR
; /* errno is set for us. */
1841 size
= (size_t) ssize
;
1842 if (size
> CTF_MAX_SIZE
)
1844 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1845 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI (size
);
1846 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO (size
);
1849 dtd
->dtd_data
.ctt_size
= (uint32_t) size
;
1851 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO (kind
, flag
, vlen
);
1853 /* Make a final pass through the members changing each dmd_type (a
1854 src_fp type) to an equivalent type in dst_fp. We pass through all
1855 members, leaving any that fail set to CTF_ERR. */
1856 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1857 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1859 if ((dmd
->dmd_type
= ctf_add_type (dst_fp
, src_fp
,
1860 dmd
->dmd_type
)) == CTF_ERR
)
1865 return CTF_ERR
; /* errno is set for us. */
1870 if (dst_type
!= CTF_ERR
&& dst_kind
!= CTF_K_FORWARD
)
1872 if (ctf_enum_iter (src_fp
, src_type
, enumcmp
, &dst
)
1873 || ctf_enum_iter (dst_fp
, dst_type
, enumcmp
, &src
))
1875 ctf_dprintf ("Conflict for enum %s against ID %lx: "
1876 "members differ, see above\n", name
, dst_type
);
1877 return (ctf_set_errno (dst_fp
, ECTF_CONFLICT
));
1882 dst_type
= ctf_add_enum (dst_fp
, flag
, name
);
1883 if ((dst
.ctb_type
= dst_type
) == CTF_ERR
1884 || ctf_enum_iter (src_fp
, src_type
, enumadd
, &dst
))
1885 return CTF_ERR
; /* errno is set for us */
1890 if (dst_type
== CTF_ERR
)
1892 dst_type
= ctf_add_forward (dst_fp
, flag
,
1893 name
, CTF_K_STRUCT
); /* Assume STRUCT. */
1898 src_type
= ctf_type_reference (src_fp
, src_type
);
1899 src_type
= ctf_add_type (dst_fp
, src_fp
, src_type
);
1901 if (src_type
== CTF_ERR
)
1902 return CTF_ERR
; /* errno is set for us. */
1904 /* If dst_type is not CTF_ERR at this point, we should check if
1905 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
1906 ECTF_CONFLICT. However, this causes problems with bitness typedefs
1907 that vary based on things like if 32-bit then pid_t is int otherwise
1908 long. We therefore omit this check and assume that if the identically
1909 named typedef already exists in dst_fp, it is correct or
1912 if (dst_type
== CTF_ERR
)
1914 dst_type
= ctf_add_typedef (dst_fp
, flag
, name
, src_type
);
1919 return (ctf_set_errno (dst_fp
, ECTF_CORRUPT
));
1925 /* Write the compressed CTF data stream to the specified gzFile descriptor. */
1927 ctf_gzwrite (ctf_file_t
*fp
, gzFile fd
)
1929 const unsigned char *buf
;
1933 resid
= sizeof (ctf_header_t
);
1934 buf
= (unsigned char *) fp
->ctf_header
;
1937 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
1938 return (ctf_set_errno (fp
, errno
));
1943 resid
= fp
->ctf_size
;
1947 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
1948 return (ctf_set_errno (fp
, errno
));
1956 /* Compress the specified CTF data stream and write it to the specified file
1959 ctf_compress_write (ctf_file_t
*fp
, int fd
)
1964 ctf_header_t
*hp
= &h
;
1965 ssize_t header_len
= sizeof (ctf_header_t
);
1966 ssize_t compress_len
;
1967 size_t max_compress_len
= compressBound (fp
->ctf_size
);
1972 memcpy (hp
, fp
->ctf_header
, header_len
);
1973 hp
->cth_flags
|= CTF_F_COMPRESS
;
1975 if ((buf
= ctf_alloc (max_compress_len
)) == NULL
)
1976 return (ctf_set_errno (fp
, ECTF_ZALLOC
));
1978 compress_len
= max_compress_len
;
1979 if ((rc
= compress (buf
, (uLongf
*) &compress_len
,
1980 fp
->ctf_buf
, fp
->ctf_size
)) != Z_OK
)
1982 ctf_dprintf ("zlib deflate err: %s\n", zError (rc
));
1983 err
= ctf_set_errno (fp
, ECTF_COMPRESS
);
1988 while (header_len
> 0)
1990 if ((len
= write (fd
, hp
, header_len
)) < 0)
1992 err
= ctf_set_errno (fp
, errno
);
2000 while (compress_len
> 0)
2002 if ((len
= write (fd
, bp
, compress_len
)) < 0)
2004 err
= ctf_set_errno (fp
, errno
);
2007 compress_len
-= len
;
2016 /* Write the uncompressed CTF data stream to the specified file descriptor. */
2018 ctf_write (ctf_file_t
*fp
, int fd
)
2020 const unsigned char *buf
;
2024 resid
= sizeof (ctf_header_t
);
2025 buf
= (unsigned char *) fp
->ctf_header
;
2028 if ((len
= write (fd
, buf
, resid
)) <= 0)
2029 return (ctf_set_errno (fp
, errno
));
2034 resid
= fp
->ctf_size
;
2038 if ((len
= write (fd
, buf
, resid
)) <= 0)
2039 return (ctf_set_errno (fp
, errno
));