gdb: Make GMP a required dependency for building GDB
[binutils-gdb.git] / libctf / ctf-create.c
blobe236606a249a1a1b05497379a2be9c59a358ceb3
1 /* CTF file creation.
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
9 version.
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/>. */
20 #include <ctf-impl.h>
21 #include <sys/param.h>
22 #include <assert.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <zlib.h>
27 #ifndef EOVERFLOW
28 #define EOVERFLOW ERANGE
29 #endif
31 #ifndef roundup
32 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
33 #endif
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%
38 at a time. */
40 static int
41 ctf_grow_ptrtab (ctf_file_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)
55 uint32_t *new_ptrtab;
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;
66 return 0;
69 /* To create an empty CTF container, we just declare a zeroed header and call
70 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w
71 and 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. */
74 ctf_file_t *
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;
82 ctf_sect_t cts;
83 ctf_file_t *fp;
85 libctf_init_debug();
86 dthash = ctf_dynhash_create (ctf_hash_integer, ctf_hash_eq_integer,
87 NULL, NULL);
88 if (dthash == NULL)
90 ctf_set_open_errno (errp, EAGAIN);
91 goto err;
94 dvhash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
95 NULL, NULL);
96 if (dvhash == NULL)
98 ctf_set_open_errno (errp, EAGAIN);
99 goto err_dt;
102 structs = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
103 NULL, NULL);
104 unions = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
105 NULL, NULL);
106 enums = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
107 NULL, NULL);
108 names = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
109 NULL, NULL);
110 if (!structs || !unions || !enums || !names)
112 ctf_set_open_errno (errp, EAGAIN);
113 goto err_dv;
116 cts.cts_name = _CTF_SECTION;
117 cts.cts_data = &hdr;
118 cts.cts_size = sizeof (hdr);
119 cts.cts_entsize = 1;
121 if ((fp = ctf_bufopen_internal (&cts, NULL, NULL, NULL, 1, errp)) == NULL)
122 goto err_dv;
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;
130 fp->ctf_dtoldid = 0;
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));
140 ctf_file_close (fp);
141 return NULL;
144 return fp;
146 err_dv:
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);
152 err_dt:
153 ctf_dynhash_destroy (dthash);
154 err:
155 return NULL;
158 static unsigned char *
159 ctf_copy_smembers (ctf_file_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
161 ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
162 ctf_member_t ctm;
164 for (; dmd != NULL; dmd = ctf_list_next (dmd))
166 ctf_member_t *copied;
168 ctm.ctm_name = 0;
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;
174 if (dmd->dmd_name)
175 ctf_str_add_ref (fp, dmd->dmd_name, &copied->ctm_name);
177 t += sizeof (ctm);
180 return t;
183 static unsigned char *
184 ctf_copy_lmembers (ctf_file_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
186 ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
187 ctf_lmember_t ctlm;
189 for (; dmd != NULL; dmd = ctf_list_next (dmd))
191 ctf_lmember_t *copied;
193 ctlm.ctlm_name = 0;
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;
200 if (dmd->dmd_name)
201 ctf_str_add_ref (fp, dmd->dmd_name, &copied->ctlm_name);
203 t += sizeof (ctlm);
206 return t;
209 static unsigned char *
210 ctf_copy_emembers (ctf_file_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
212 ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
213 ctf_enum_t cte;
215 for (; dmd != NULL; dmd = ctf_list_next (dmd))
217 ctf_enum_t *copied;
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);
223 t += sizeof (cte);
226 return t;
229 /* Sort a newly-constructed static variable array. */
231 typedef struct ctf_sort_var_arg_cb
233 ctf_file_t *fp;
234 ctf_strs_t *strtab;
235 } ctf_sort_var_arg_cb_t;
237 static int
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_file_t *fp)
252 if (!(fp->ctf_flags & LCTF_RDWR))
253 return (ctf_set_errno (fp, ECTF_RDONLY));
255 fp->ctf_dtoldid = fp->ctf_typemax;
256 return 0;
259 /* If the specified CTF container is writable and has been modified, reload this
260 container with the updated type definitions, ready for serialization. In
261 order to make this code and the rest of libctf as simple as possible, we
262 perform updates by taking the dynamic type definitions and creating an
263 in-memory CTF file containing the definitions, and then call
264 ctf_simple_open_internal() on it. We perform one extra trick here for the
265 benefit of callers and to keep our code simple: ctf_simple_open_internal()
266 will return a new ctf_file_t, but we want to keep the fp constant for the
267 caller, so after ctf_simple_open_internal() returns, we use memcpy to swap
268 the interior of the old and new ctf_file_t's, and then free the old. */
270 ctf_serialize (ctf_file_t *fp)
272 ctf_file_t ofp, *nfp;
273 ctf_header_t hdr, *hdrp;
274 ctf_dtdef_t *dtd;
275 ctf_dvdef_t *dvd;
276 ctf_varent_t *dvarents;
277 ctf_strs_writable_t strtab;
279 unsigned char *t;
280 unsigned long i;
281 size_t buf_size, type_size, nvars;
282 unsigned char *buf, *newbuf;
283 int err;
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))
290 return 0;
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 /* Iterate through the dynamic type definition list and compute the
302 size of the CTF type section we will need to generate. */
304 for (type_size = 0, dtd = ctf_list_next (&fp->ctf_dtdefs);
305 dtd != NULL; dtd = ctf_list_next (dtd))
307 uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
308 uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
310 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
311 type_size += sizeof (ctf_stype_t);
312 else
313 type_size += sizeof (ctf_type_t);
315 switch (kind)
317 case CTF_K_INTEGER:
318 case CTF_K_FLOAT:
319 type_size += sizeof (uint32_t);
320 break;
321 case CTF_K_ARRAY:
322 type_size += sizeof (ctf_array_t);
323 break;
324 case CTF_K_SLICE:
325 type_size += sizeof (ctf_slice_t);
326 break;
327 case CTF_K_FUNCTION:
328 type_size += sizeof (uint32_t) * (vlen + (vlen & 1));
329 break;
330 case CTF_K_STRUCT:
331 case CTF_K_UNION:
332 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
333 type_size += sizeof (ctf_member_t) * vlen;
334 else
335 type_size += sizeof (ctf_lmember_t) * vlen;
336 break;
337 case CTF_K_ENUM:
338 type_size += sizeof (ctf_enum_t) * vlen;
339 break;
343 /* Computing the number of entries in the CTF variable section is much
344 simpler. */
346 for (nvars = 0, dvd = ctf_list_next (&fp->ctf_dvdefs);
347 dvd != NULL; dvd = ctf_list_next (dvd), nvars++);
349 /* Compute the size of the CTF buffer we need, sans only the string table,
350 then allocate a new buffer and memcpy the finished header to the start of
351 the buffer. (We will adjust this later with strtab length info.) */
353 hdr.cth_typeoff = hdr.cth_varoff + (nvars * sizeof (ctf_varent_t));
354 hdr.cth_stroff = hdr.cth_typeoff + type_size;
355 hdr.cth_strlen = 0;
357 buf_size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
359 if ((buf = malloc (buf_size)) == NULL)
360 return (ctf_set_errno (fp, EAGAIN));
362 memcpy (buf, &hdr, sizeof (ctf_header_t));
363 t = (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_varoff;
365 hdrp = (ctf_header_t *) buf;
366 if ((fp->ctf_flags & LCTF_CHILD) && (fp->ctf_parname != NULL))
367 ctf_str_add_ref (fp, fp->ctf_parname, &hdrp->cth_parname);
368 if (fp->ctf_cuname != NULL)
369 ctf_str_add_ref (fp, fp->ctf_cuname, &hdrp->cth_cuname);
371 /* Work over the variable list, translating everything into ctf_varent_t's and
372 prepping the string table. */
374 dvarents = (ctf_varent_t *) t;
375 for (i = 0, dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL;
376 dvd = ctf_list_next (dvd), i++)
378 ctf_varent_t *var = &dvarents[i];
380 ctf_str_add_ref (fp, dvd->dvd_name, &var->ctv_name);
381 var->ctv_type = (uint32_t) dvd->dvd_type;
383 assert (i == nvars);
385 t += sizeof (ctf_varent_t) * nvars;
387 assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_typeoff);
389 /* We now take a final lap through the dynamic type definition list and copy
390 the appropriate type records to the output buffer, noting down the
391 strings as we go. */
393 for (dtd = ctf_list_next (&fp->ctf_dtdefs);
394 dtd != NULL; dtd = ctf_list_next (dtd))
396 uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
397 uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
399 ctf_array_t cta;
400 uint32_t encoding;
401 size_t len;
402 ctf_stype_t *copied;
403 const char *name;
405 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
406 len = sizeof (ctf_stype_t);
407 else
408 len = sizeof (ctf_type_t);
410 memcpy (t, &dtd->dtd_data, len);
411 copied = (ctf_stype_t *) t; /* name is at the start: constant offset. */
412 if (copied->ctt_name
413 && (name = ctf_strraw (fp, copied->ctt_name)) != NULL)
414 ctf_str_add_ref (fp, name, &copied->ctt_name);
415 t += len;
417 switch (kind)
419 case CTF_K_INTEGER:
420 case CTF_K_FLOAT:
421 if (kind == CTF_K_INTEGER)
423 encoding = CTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format,
424 dtd->dtd_u.dtu_enc.cte_offset,
425 dtd->dtd_u.dtu_enc.cte_bits);
427 else
429 encoding = CTF_FP_DATA (dtd->dtd_u.dtu_enc.cte_format,
430 dtd->dtd_u.dtu_enc.cte_offset,
431 dtd->dtd_u.dtu_enc.cte_bits);
433 memcpy (t, &encoding, sizeof (encoding));
434 t += sizeof (encoding);
435 break;
437 case CTF_K_SLICE:
438 memcpy (t, &dtd->dtd_u.dtu_slice, sizeof (struct ctf_slice));
439 t += sizeof (struct ctf_slice);
440 break;
442 case CTF_K_ARRAY:
443 cta.cta_contents = (uint32_t) dtd->dtd_u.dtu_arr.ctr_contents;
444 cta.cta_index = (uint32_t) dtd->dtd_u.dtu_arr.ctr_index;
445 cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
446 memcpy (t, &cta, sizeof (cta));
447 t += sizeof (cta);
448 break;
450 case CTF_K_FUNCTION:
452 uint32_t *argv = (uint32_t *) (uintptr_t) t;
453 uint32_t argc;
455 for (argc = 0; argc < vlen; argc++)
456 *argv++ = dtd->dtd_u.dtu_argv[argc];
458 if (vlen & 1)
459 *argv++ = 0; /* Pad to 4-byte boundary. */
461 t = (unsigned char *) argv;
462 break;
465 case CTF_K_STRUCT:
466 case CTF_K_UNION:
467 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
468 t = ctf_copy_smembers (fp, dtd, t);
469 else
470 t = ctf_copy_lmembers (fp, dtd, t);
471 break;
473 case CTF_K_ENUM:
474 t = ctf_copy_emembers (fp, dtd, t);
475 break;
478 assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_stroff);
480 /* Construct the final string table and fill out all the string refs with the
481 final offsets. Then purge the refs list, because we're about to move this
482 strtab onto the end of the buf, invalidating all the offsets. */
483 strtab = ctf_str_write_strtab (fp);
484 ctf_str_purge_refs (fp);
486 if (strtab.cts_strs == NULL)
488 free (buf);
489 return (ctf_set_errno (fp, EAGAIN));
492 /* Now the string table is constructed, we can sort the buffer of
493 ctf_varent_t's. */
494 ctf_sort_var_arg_cb_t sort_var_arg = { fp, (ctf_strs_t *) &strtab };
495 ctf_qsort_r (dvarents, nvars, sizeof (ctf_varent_t), ctf_sort_var,
496 &sort_var_arg);
498 if ((newbuf = ctf_realloc (fp, buf, buf_size + strtab.cts_len)) == NULL)
500 free (buf);
501 free (strtab.cts_strs);
502 return (ctf_set_errno (fp, EAGAIN));
504 buf = newbuf;
505 memcpy (buf + buf_size, strtab.cts_strs, strtab.cts_len);
506 hdrp = (ctf_header_t *) buf;
507 hdrp->cth_strlen = strtab.cts_len;
508 buf_size += hdrp->cth_strlen;
509 free (strtab.cts_strs);
511 /* Finally, we are ready to ctf_simple_open() the new container. If this
512 is successful, we then switch nfp and fp and free the old container. */
514 if ((nfp = ctf_simple_open_internal ((char *) buf, buf_size, NULL, 0,
515 0, NULL, 0, fp->ctf_syn_ext_strtab,
516 1, &err)) == NULL)
518 free (buf);
519 return (ctf_set_errno (fp, err));
522 (void) ctf_setmodel (nfp, ctf_getmodel (fp));
524 nfp->ctf_parent = fp->ctf_parent;
525 nfp->ctf_parent_unreffed = fp->ctf_parent_unreffed;
526 nfp->ctf_refcnt = fp->ctf_refcnt;
527 nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
528 if (nfp->ctf_dynbase == NULL)
529 nfp->ctf_dynbase = buf; /* Make sure buf is freed on close. */
530 nfp->ctf_dthash = fp->ctf_dthash;
531 nfp->ctf_dtdefs = fp->ctf_dtdefs;
532 nfp->ctf_dvhash = fp->ctf_dvhash;
533 nfp->ctf_dvdefs = fp->ctf_dvdefs;
534 nfp->ctf_dtoldid = fp->ctf_dtoldid;
535 nfp->ctf_add_processing = fp->ctf_add_processing;
536 nfp->ctf_snapshots = fp->ctf_snapshots + 1;
537 nfp->ctf_specific = fp->ctf_specific;
538 nfp->ctf_ptrtab = fp->ctf_ptrtab;
539 nfp->ctf_ptrtab_len = fp->ctf_ptrtab_len;
540 nfp->ctf_link_inputs = fp->ctf_link_inputs;
541 nfp->ctf_link_outputs = fp->ctf_link_outputs;
542 nfp->ctf_errs_warnings = fp->ctf_errs_warnings;
543 nfp->ctf_str_prov_offset = fp->ctf_str_prov_offset;
544 nfp->ctf_syn_ext_strtab = fp->ctf_syn_ext_strtab;
545 nfp->ctf_link_in_cu_mapping = fp->ctf_link_in_cu_mapping;
546 nfp->ctf_link_out_cu_mapping = fp->ctf_link_out_cu_mapping;
547 nfp->ctf_link_type_mapping = fp->ctf_link_type_mapping;
548 nfp->ctf_link_memb_name_changer = fp->ctf_link_memb_name_changer;
549 nfp->ctf_link_memb_name_changer_arg = fp->ctf_link_memb_name_changer_arg;
550 nfp->ctf_link_variable_filter = fp->ctf_link_variable_filter;
551 nfp->ctf_link_variable_filter_arg = fp->ctf_link_variable_filter_arg;
552 nfp->ctf_link_flags = fp->ctf_link_flags;
553 nfp->ctf_dedup_atoms = fp->ctf_dedup_atoms;
554 nfp->ctf_dedup_atoms_alloc = fp->ctf_dedup_atoms_alloc;
555 memcpy (&nfp->ctf_dedup, &fp->ctf_dedup, sizeof (fp->ctf_dedup));
557 nfp->ctf_snapshot_lu = fp->ctf_snapshots;
559 memcpy (&nfp->ctf_lookups, fp->ctf_lookups, sizeof (fp->ctf_lookups));
560 nfp->ctf_structs = fp->ctf_structs;
561 nfp->ctf_unions = fp->ctf_unions;
562 nfp->ctf_enums = fp->ctf_enums;
563 nfp->ctf_names = fp->ctf_names;
565 fp->ctf_dthash = NULL;
566 ctf_str_free_atoms (nfp);
567 nfp->ctf_str_atoms = fp->ctf_str_atoms;
568 nfp->ctf_prov_strtab = fp->ctf_prov_strtab;
569 fp->ctf_str_atoms = NULL;
570 fp->ctf_prov_strtab = NULL;
571 memset (&fp->ctf_dtdefs, 0, sizeof (ctf_list_t));
572 memset (&fp->ctf_errs_warnings, 0, sizeof (ctf_list_t));
573 fp->ctf_add_processing = NULL;
574 fp->ctf_ptrtab = NULL;
575 fp->ctf_link_inputs = NULL;
576 fp->ctf_link_outputs = NULL;
577 fp->ctf_syn_ext_strtab = NULL;
578 fp->ctf_link_in_cu_mapping = NULL;
579 fp->ctf_link_out_cu_mapping = NULL;
580 fp->ctf_link_type_mapping = NULL;
581 fp->ctf_dedup_atoms = NULL;
582 fp->ctf_dedup_atoms_alloc = NULL;
583 fp->ctf_parent_unreffed = 1;
585 fp->ctf_dvhash = NULL;
586 memset (&fp->ctf_dvdefs, 0, sizeof (ctf_list_t));
587 memset (fp->ctf_lookups, 0, sizeof (fp->ctf_lookups));
588 memset (&fp->ctf_dedup, 0, sizeof (fp->ctf_dedup));
589 fp->ctf_structs.ctn_writable = NULL;
590 fp->ctf_unions.ctn_writable = NULL;
591 fp->ctf_enums.ctn_writable = NULL;
592 fp->ctf_names.ctn_writable = NULL;
594 memcpy (&ofp, fp, sizeof (ctf_file_t));
595 memcpy (fp, nfp, sizeof (ctf_file_t));
596 memcpy (nfp, &ofp, sizeof (ctf_file_t));
598 nfp->ctf_refcnt = 1; /* Force nfp to be freed. */
599 ctf_file_close (nfp);
601 return 0;
604 ctf_names_t *
605 ctf_name_table (ctf_file_t *fp, int kind)
607 switch (kind)
609 case CTF_K_STRUCT:
610 return &fp->ctf_structs;
611 case CTF_K_UNION:
612 return &fp->ctf_unions;
613 case CTF_K_ENUM:
614 return &fp->ctf_enums;
615 default:
616 return &fp->ctf_names;
621 ctf_dtd_insert (ctf_file_t *fp, ctf_dtdef_t *dtd, int flag, int kind)
623 const char *name;
624 if (ctf_dynhash_insert (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type,
625 dtd) < 0)
626 return -1;
628 if (flag == CTF_ADD_ROOT && dtd->dtd_data.ctt_name
629 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL)
631 if (ctf_dynhash_insert (ctf_name_table (fp, kind)->ctn_writable,
632 (char *) name, (void *) (uintptr_t)
633 dtd->dtd_type) < 0)
635 ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t)
636 dtd->dtd_type);
637 return -1;
640 ctf_list_append (&fp->ctf_dtdefs, dtd);
641 return 0;
644 void
645 ctf_dtd_delete (ctf_file_t *fp, ctf_dtdef_t *dtd)
647 ctf_dmdef_t *dmd, *nmd;
648 int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
649 int name_kind = kind;
650 const char *name;
652 ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type);
654 switch (kind)
656 case CTF_K_STRUCT:
657 case CTF_K_UNION:
658 case CTF_K_ENUM:
659 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
660 dmd != NULL; dmd = nmd)
662 if (dmd->dmd_name != NULL)
663 free (dmd->dmd_name);
664 nmd = ctf_list_next (dmd);
665 free (dmd);
667 break;
668 case CTF_K_FUNCTION:
669 free (dtd->dtd_u.dtu_argv);
670 break;
671 case CTF_K_FORWARD:
672 name_kind = dtd->dtd_data.ctt_type;
673 break;
676 if (dtd->dtd_data.ctt_name
677 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL
678 && LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info))
680 ctf_dynhash_remove (ctf_name_table (fp, name_kind)->ctn_writable,
681 name);
682 ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
685 ctf_list_delete (&fp->ctf_dtdefs, dtd);
686 free (dtd);
689 ctf_dtdef_t *
690 ctf_dtd_lookup (const ctf_file_t *fp, ctf_id_t type)
692 return (ctf_dtdef_t *)
693 ctf_dynhash_lookup (fp->ctf_dthash, (void *) (uintptr_t) type);
696 ctf_dtdef_t *
697 ctf_dynamic_type (const ctf_file_t *fp, ctf_id_t id)
699 ctf_id_t idx;
701 if (!(fp->ctf_flags & LCTF_RDWR))
702 return NULL;
704 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, id))
705 fp = fp->ctf_parent;
707 idx = LCTF_TYPE_TO_INDEX(fp, id);
709 if ((unsigned long) idx <= fp->ctf_typemax)
710 return ctf_dtd_lookup (fp, id);
711 return NULL;
715 ctf_dvd_insert (ctf_file_t *fp, ctf_dvdef_t *dvd)
717 if (ctf_dynhash_insert (fp->ctf_dvhash, dvd->dvd_name, dvd) < 0)
718 return -1;
719 ctf_list_append (&fp->ctf_dvdefs, dvd);
720 return 0;
723 void
724 ctf_dvd_delete (ctf_file_t *fp, ctf_dvdef_t *dvd)
726 ctf_dynhash_remove (fp->ctf_dvhash, dvd->dvd_name);
727 free (dvd->dvd_name);
729 ctf_list_delete (&fp->ctf_dvdefs, dvd);
730 free (dvd);
733 ctf_dvdef_t *
734 ctf_dvd_lookup (const ctf_file_t *fp, const char *name)
736 return (ctf_dvdef_t *) ctf_dynhash_lookup (fp->ctf_dvhash, name);
739 /* Discard all of the dynamic type definitions and variable definitions that
740 have been added to the container since the last call to ctf_update(). We
741 locate such types by scanning the dtd list and deleting elements that have
742 type IDs greater than ctf_dtoldid, which is set by ctf_update(), above, and
743 by scanning the variable list and deleting elements that have update IDs
744 equal to the current value of the last-update snapshot count (indicating that
745 they were added after the most recent call to ctf_update()). */
747 ctf_discard (ctf_file_t *fp)
749 ctf_snapshot_id_t last_update =
750 { fp->ctf_dtoldid,
751 fp->ctf_snapshot_lu + 1 };
753 /* Update required? */
754 if (!(fp->ctf_flags & LCTF_DIRTY))
755 return 0;
757 return (ctf_rollback (fp, last_update));
760 ctf_snapshot_id_t
761 ctf_snapshot (ctf_file_t *fp)
763 ctf_snapshot_id_t snapid;
764 snapid.dtd_id = fp->ctf_typemax;
765 snapid.snapshot_id = fp->ctf_snapshots++;
766 return snapid;
769 /* Like ctf_discard(), only discards everything after a particular ID. */
771 ctf_rollback (ctf_file_t *fp, ctf_snapshot_id_t id)
773 ctf_dtdef_t *dtd, *ntd;
774 ctf_dvdef_t *dvd, *nvd;
776 if (!(fp->ctf_flags & LCTF_RDWR))
777 return (ctf_set_errno (fp, ECTF_RDONLY));
779 if (fp->ctf_snapshot_lu >= id.snapshot_id)
780 return (ctf_set_errno (fp, ECTF_OVERROLLBACK));
782 for (dtd = ctf_list_next (&fp->ctf_dtdefs); dtd != NULL; dtd = ntd)
784 int kind;
785 const char *name;
787 ntd = ctf_list_next (dtd);
789 if (LCTF_TYPE_TO_INDEX (fp, dtd->dtd_type) <= id.dtd_id)
790 continue;
792 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
793 if (kind == CTF_K_FORWARD)
794 kind = dtd->dtd_data.ctt_type;
796 if (dtd->dtd_data.ctt_name
797 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL
798 && LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info))
800 ctf_dynhash_remove (ctf_name_table (fp, kind)->ctn_writable,
801 name);
802 ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
805 ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type);
806 ctf_dtd_delete (fp, dtd);
809 for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd)
811 nvd = ctf_list_next (dvd);
813 if (dvd->dvd_snapshots <= id.snapshot_id)
814 continue;
816 ctf_dvd_delete (fp, dvd);
819 fp->ctf_typemax = id.dtd_id;
820 fp->ctf_snapshots = id.snapshot_id;
822 if (fp->ctf_snapshots == fp->ctf_snapshot_lu)
823 fp->ctf_flags &= ~LCTF_DIRTY;
825 return 0;
828 static ctf_id_t
829 ctf_add_generic (ctf_file_t *fp, uint32_t flag, const char *name, int kind,
830 ctf_dtdef_t **rp)
832 ctf_dtdef_t *dtd;
833 ctf_id_t type;
835 if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
836 return (ctf_set_errno (fp, EINVAL));
838 if (!(fp->ctf_flags & LCTF_RDWR))
839 return (ctf_set_errno (fp, ECTF_RDONLY));
841 if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) >= CTF_MAX_TYPE)
842 return (ctf_set_errno (fp, ECTF_FULL));
844 if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) == (CTF_MAX_PTYPE - 1))
845 return (ctf_set_errno (fp, ECTF_FULL));
847 /* Make sure ptrtab always grows to be big enough for all types. */
848 if (ctf_grow_ptrtab (fp) < 0)
849 return CTF_ERR; /* errno is set for us. */
851 if ((dtd = malloc (sizeof (ctf_dtdef_t))) == NULL)
852 return (ctf_set_errno (fp, EAGAIN));
854 type = ++fp->ctf_typemax;
855 type = LCTF_INDEX_TO_TYPE (fp, type, (fp->ctf_flags & LCTF_CHILD));
857 memset (dtd, 0, sizeof (ctf_dtdef_t));
858 dtd->dtd_data.ctt_name = ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name);
859 dtd->dtd_type = type;
861 if (dtd->dtd_data.ctt_name == 0 && name != NULL && name[0] != '\0')
863 free (dtd);
864 return (ctf_set_errno (fp, EAGAIN));
867 if (ctf_dtd_insert (fp, dtd, flag, kind) < 0)
869 free (dtd);
870 return CTF_ERR; /* errno is set for us. */
872 fp->ctf_flags |= LCTF_DIRTY;
874 *rp = dtd;
875 return type;
878 /* When encoding integer sizes, we want to convert a byte count in the range
879 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
880 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
881 static size_t
882 clp2 (size_t x)
884 x--;
886 x |= (x >> 1);
887 x |= (x >> 2);
888 x |= (x >> 4);
889 x |= (x >> 8);
890 x |= (x >> 16);
892 return (x + 1);
895 ctf_id_t
896 ctf_add_encoded (ctf_file_t *fp, uint32_t flag,
897 const char *name, const ctf_encoding_t *ep, uint32_t kind)
899 ctf_dtdef_t *dtd;
900 ctf_id_t type;
902 if (ep == NULL)
903 return (ctf_set_errno (fp, EINVAL));
905 if ((type = ctf_add_generic (fp, flag, name, kind, &dtd)) == CTF_ERR)
906 return CTF_ERR; /* errno is set for us. */
908 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
909 dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
910 / CHAR_BIT);
911 dtd->dtd_u.dtu_enc = *ep;
913 return type;
916 ctf_id_t
917 ctf_add_reftype (ctf_file_t *fp, uint32_t flag, ctf_id_t ref, uint32_t kind)
919 ctf_dtdef_t *dtd;
920 ctf_id_t type;
921 ctf_file_t *tmp = fp;
922 int child = fp->ctf_flags & LCTF_CHILD;
924 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
925 return (ctf_set_errno (fp, EINVAL));
927 if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL)
928 return CTF_ERR; /* errno is set for us. */
930 if ((type = ctf_add_generic (fp, flag, NULL, kind, &dtd)) == CTF_ERR)
931 return CTF_ERR; /* errno is set for us. */
933 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
934 dtd->dtd_data.ctt_type = (uint32_t) ref;
936 if (kind != CTF_K_POINTER)
937 return type;
939 /* If we are adding a pointer, update the ptrtab, both the directly pointed-to
940 type and (if an anonymous typedef node is being pointed at) the type that
941 points at too. Note that ctf_typemax is at this point one higher than we
942 want to check against, because it's just been incremented for the addition
943 of this type. */
945 uint32_t type_idx = LCTF_TYPE_TO_INDEX (fp, type);
946 uint32_t ref_idx = LCTF_TYPE_TO_INDEX (fp, ref);
948 if (LCTF_TYPE_ISCHILD (fp, ref) == child
949 && ref_idx < fp->ctf_typemax)
951 fp->ctf_ptrtab[ref_idx] = type_idx;
953 ctf_id_t refref_idx = LCTF_TYPE_TO_INDEX (fp, dtd->dtd_data.ctt_type);
955 if (tmp == fp
956 && (LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) == CTF_K_TYPEDEF)
957 && strcmp (ctf_strptr (fp, dtd->dtd_data.ctt_name), "") == 0
958 && refref_idx < fp->ctf_typemax)
959 fp->ctf_ptrtab[refref_idx] = type_idx;
962 return type;
965 ctf_id_t
966 ctf_add_slice (ctf_file_t *fp, uint32_t flag, ctf_id_t ref,
967 const ctf_encoding_t *ep)
969 ctf_dtdef_t *dtd;
970 ctf_id_t resolved_ref = ref;
971 ctf_id_t type;
972 int kind;
973 const ctf_type_t *tp;
974 ctf_file_t *tmp = fp;
976 if (ep == NULL)
977 return (ctf_set_errno (fp, EINVAL));
979 if ((ep->cte_bits > 255) || (ep->cte_offset > 255))
980 return (ctf_set_errno (fp, ECTF_SLICEOVERFLOW));
982 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
983 return (ctf_set_errno (fp, EINVAL));
985 if (ref != 0 && ((tp = ctf_lookup_by_id (&tmp, ref)) == NULL))
986 return CTF_ERR; /* errno is set for us. */
988 /* Make sure we ultimately point to an integral type. We also allow slices to
989 point to the unimplemented type, for now, because the compiler can emit
990 such slices, though they're not very much use. */
992 resolved_ref = ctf_type_resolve_unsliced (tmp, ref);
993 kind = ctf_type_kind_unsliced (tmp, resolved_ref);
995 if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) &&
996 (kind != CTF_K_ENUM)
997 && (ref != 0))
998 return (ctf_set_errno (fp, ECTF_NOTINTFP));
1000 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_SLICE, &dtd)) == CTF_ERR)
1001 return CTF_ERR; /* errno is set for us. */
1003 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0);
1004 dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
1005 / CHAR_BIT);
1006 dtd->dtd_u.dtu_slice.cts_type = (uint32_t) ref;
1007 dtd->dtd_u.dtu_slice.cts_bits = ep->cte_bits;
1008 dtd->dtd_u.dtu_slice.cts_offset = ep->cte_offset;
1010 return type;
1013 ctf_id_t
1014 ctf_add_integer (ctf_file_t *fp, uint32_t flag,
1015 const char *name, const ctf_encoding_t *ep)
1017 return (ctf_add_encoded (fp, flag, name, ep, CTF_K_INTEGER));
1020 ctf_id_t
1021 ctf_add_float (ctf_file_t *fp, uint32_t flag,
1022 const char *name, const ctf_encoding_t *ep)
1024 return (ctf_add_encoded (fp, flag, name, ep, CTF_K_FLOAT));
1027 ctf_id_t
1028 ctf_add_pointer (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1030 return (ctf_add_reftype (fp, flag, ref, CTF_K_POINTER));
1033 ctf_id_t
1034 ctf_add_array (ctf_file_t *fp, uint32_t flag, const ctf_arinfo_t *arp)
1036 ctf_dtdef_t *dtd;
1037 ctf_id_t type;
1038 ctf_file_t *tmp = fp;
1040 if (arp == NULL)
1041 return (ctf_set_errno (fp, EINVAL));
1043 if (arp->ctr_contents != 0
1044 && ctf_lookup_by_id (&tmp, arp->ctr_contents) == NULL)
1045 return CTF_ERR; /* errno is set for us. */
1047 tmp = fp;
1048 if (ctf_lookup_by_id (&tmp, arp->ctr_index) == NULL)
1049 return CTF_ERR; /* errno is set for us. */
1051 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_ARRAY, &dtd)) == CTF_ERR)
1052 return CTF_ERR; /* errno is set for us. */
1054 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ARRAY, flag, 0);
1055 dtd->dtd_data.ctt_size = 0;
1056 dtd->dtd_u.dtu_arr = *arp;
1058 return type;
1062 ctf_set_array (ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
1064 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type);
1066 if (!(fp->ctf_flags & LCTF_RDWR))
1067 return (ctf_set_errno (fp, ECTF_RDONLY));
1069 if (dtd == NULL
1070 || LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
1071 return (ctf_set_errno (fp, ECTF_BADID));
1073 fp->ctf_flags |= LCTF_DIRTY;
1074 dtd->dtd_u.dtu_arr = *arp;
1076 return 0;
1079 ctf_id_t
1080 ctf_add_function (ctf_file_t *fp, uint32_t flag,
1081 const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
1083 ctf_dtdef_t *dtd;
1084 ctf_id_t type;
1085 uint32_t vlen;
1086 uint32_t *vdat = NULL;
1087 ctf_file_t *tmp = fp;
1088 size_t i;
1090 if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0
1091 || (ctc->ctc_argc != 0 && argv == NULL))
1092 return (ctf_set_errno (fp, EINVAL));
1094 vlen = ctc->ctc_argc;
1095 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1096 vlen++; /* Add trailing zero to indicate varargs (see below). */
1098 if (ctc->ctc_return != 0
1099 && ctf_lookup_by_id (&tmp, ctc->ctc_return) == NULL)
1100 return CTF_ERR; /* errno is set for us. */
1102 if (vlen > CTF_MAX_VLEN)
1103 return (ctf_set_errno (fp, EOVERFLOW));
1105 if (vlen != 0 && (vdat = malloc (sizeof (ctf_id_t) * vlen)) == NULL)
1106 return (ctf_set_errno (fp, EAGAIN));
1108 for (i = 0; i < ctc->ctc_argc; i++)
1110 tmp = fp;
1111 if (argv[i] != 0 && ctf_lookup_by_id (&tmp, argv[i]) == NULL)
1113 free (vdat);
1114 return CTF_ERR; /* errno is set for us. */
1116 vdat[i] = (uint32_t) argv[i];
1119 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_FUNCTION,
1120 &dtd)) == CTF_ERR)
1122 free (vdat);
1123 return CTF_ERR; /* errno is set for us. */
1126 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FUNCTION, flag, vlen);
1127 dtd->dtd_data.ctt_type = (uint32_t) ctc->ctc_return;
1129 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1130 vdat[vlen - 1] = 0; /* Add trailing zero to indicate varargs. */
1131 dtd->dtd_u.dtu_argv = vdat;
1133 return type;
1136 ctf_id_t
1137 ctf_add_struct_sized (ctf_file_t *fp, uint32_t flag, const char *name,
1138 size_t size)
1140 ctf_dtdef_t *dtd;
1141 ctf_id_t type = 0;
1143 /* Promote root-visible forwards to structs. */
1144 if (name != NULL)
1145 type = ctf_lookup_by_rawname (fp, CTF_K_STRUCT, name);
1147 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1148 dtd = ctf_dtd_lookup (fp, type);
1149 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_STRUCT,
1150 &dtd)) == CTF_ERR)
1151 return CTF_ERR; /* errno is set for us. */
1153 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_STRUCT, flag, 0);
1155 if (size > CTF_MAX_SIZE)
1157 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1158 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
1159 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
1161 else
1162 dtd->dtd_data.ctt_size = (uint32_t) size;
1164 return type;
1167 ctf_id_t
1168 ctf_add_struct (ctf_file_t *fp, uint32_t flag, const char *name)
1170 return (ctf_add_struct_sized (fp, flag, name, 0));
1173 ctf_id_t
1174 ctf_add_union_sized (ctf_file_t *fp, uint32_t flag, const char *name,
1175 size_t size)
1177 ctf_dtdef_t *dtd;
1178 ctf_id_t type = 0;
1180 /* Promote root-visible forwards to unions. */
1181 if (name != NULL)
1182 type = ctf_lookup_by_rawname (fp, CTF_K_UNION, name);
1184 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1185 dtd = ctf_dtd_lookup (fp, type);
1186 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_UNION,
1187 &dtd)) == CTF_ERR)
1188 return CTF_ERR; /* errno is set for us */
1190 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_UNION, flag, 0);
1192 if (size > CTF_MAX_SIZE)
1194 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1195 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
1196 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
1198 else
1199 dtd->dtd_data.ctt_size = (uint32_t) size;
1201 return type;
1204 ctf_id_t
1205 ctf_add_union (ctf_file_t *fp, uint32_t flag, const char *name)
1207 return (ctf_add_union_sized (fp, flag, name, 0));
1210 ctf_id_t
1211 ctf_add_enum (ctf_file_t *fp, uint32_t flag, const char *name)
1213 ctf_dtdef_t *dtd;
1214 ctf_id_t type = 0;
1216 /* Promote root-visible forwards to enums. */
1217 if (name != NULL)
1218 type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
1220 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1221 dtd = ctf_dtd_lookup (fp, type);
1222 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_ENUM,
1223 &dtd)) == CTF_ERR)
1224 return CTF_ERR; /* errno is set for us. */
1226 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ENUM, flag, 0);
1227 dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
1229 return type;
1232 ctf_id_t
1233 ctf_add_enum_encoded (ctf_file_t *fp, uint32_t flag, const char *name,
1234 const ctf_encoding_t *ep)
1236 ctf_id_t type = 0;
1238 /* First, create the enum if need be, using most of the same machinery as
1239 ctf_add_enum(), to ensure that we do not allow things past that are not
1240 enums or forwards to them. (This includes other slices: you cannot slice a
1241 slice, which would be a useless thing to do anyway.) */
1243 if (name != NULL)
1244 type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
1246 if (type != 0)
1248 if ((ctf_type_kind (fp, type) != CTF_K_FORWARD) &&
1249 (ctf_type_kind_unsliced (fp, type) != CTF_K_ENUM))
1250 return (ctf_set_errno (fp, ECTF_NOTINTFP));
1252 else if ((type = ctf_add_enum (fp, flag, name)) == CTF_ERR)
1253 return CTF_ERR; /* errno is set for us. */
1255 /* Now attach a suitable slice to it. */
1257 return ctf_add_slice (fp, flag, type, ep);
1260 ctf_id_t
1261 ctf_add_forward (ctf_file_t *fp, uint32_t flag, const char *name,
1262 uint32_t kind)
1264 ctf_dtdef_t *dtd;
1265 ctf_id_t type = 0;
1267 if (!ctf_forwardable_kind (kind))
1268 return (ctf_set_errno (fp, ECTF_NOTSUE));
1270 /* If the type is already defined or exists as a forward tag, just
1271 return the ctf_id_t of the existing definition. */
1273 if (name != NULL)
1274 type = ctf_lookup_by_rawname (fp, kind, name);
1276 if (type)
1277 return type;
1279 if ((type = ctf_add_generic (fp, flag, name, kind, &dtd)) == CTF_ERR)
1280 return CTF_ERR; /* errno is set for us. */
1282 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FORWARD, flag, 0);
1283 dtd->dtd_data.ctt_type = kind;
1285 return type;
1288 ctf_id_t
1289 ctf_add_typedef (ctf_file_t *fp, uint32_t flag, const char *name,
1290 ctf_id_t ref)
1292 ctf_dtdef_t *dtd;
1293 ctf_id_t type;
1294 ctf_file_t *tmp = fp;
1296 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
1297 return (ctf_set_errno (fp, EINVAL));
1299 if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL)
1300 return CTF_ERR; /* errno is set for us. */
1302 if ((type = ctf_add_generic (fp, flag, name, CTF_K_TYPEDEF,
1303 &dtd)) == CTF_ERR)
1304 return CTF_ERR; /* errno is set for us. */
1306 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_TYPEDEF, flag, 0);
1307 dtd->dtd_data.ctt_type = (uint32_t) ref;
1309 return type;
1312 ctf_id_t
1313 ctf_add_volatile (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1315 return (ctf_add_reftype (fp, flag, ref, CTF_K_VOLATILE));
1318 ctf_id_t
1319 ctf_add_const (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1321 return (ctf_add_reftype (fp, flag, ref, CTF_K_CONST));
1324 ctf_id_t
1325 ctf_add_restrict (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1327 return (ctf_add_reftype (fp, flag, ref, CTF_K_RESTRICT));
1331 ctf_add_enumerator (ctf_file_t *fp, ctf_id_t enid, const char *name,
1332 int value)
1334 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, enid);
1335 ctf_dmdef_t *dmd;
1337 uint32_t kind, vlen, root;
1338 char *s;
1340 if (name == NULL)
1341 return (ctf_set_errno (fp, EINVAL));
1343 if (!(fp->ctf_flags & LCTF_RDWR))
1344 return (ctf_set_errno (fp, ECTF_RDONLY));
1346 if (dtd == NULL)
1347 return (ctf_set_errno (fp, ECTF_BADID));
1349 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1350 root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info);
1351 vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
1353 if (kind != CTF_K_ENUM)
1354 return (ctf_set_errno (fp, ECTF_NOTENUM));
1356 if (vlen == CTF_MAX_VLEN)
1357 return (ctf_set_errno (fp, ECTF_DTFULL));
1359 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
1360 dmd != NULL; dmd = ctf_list_next (dmd))
1362 if (strcmp (dmd->dmd_name, name) == 0)
1363 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1366 if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
1367 return (ctf_set_errno (fp, EAGAIN));
1369 if ((s = strdup (name)) == NULL)
1371 free (dmd);
1372 return (ctf_set_errno (fp, EAGAIN));
1375 dmd->dmd_name = s;
1376 dmd->dmd_type = CTF_ERR;
1377 dmd->dmd_offset = 0;
1378 dmd->dmd_value = value;
1380 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1);
1381 ctf_list_append (&dtd->dtd_u.dtu_members, dmd);
1383 fp->ctf_flags |= LCTF_DIRTY;
1385 return 0;
1389 ctf_add_member_offset (ctf_file_t *fp, ctf_id_t souid, const char *name,
1390 ctf_id_t type, unsigned long bit_offset)
1392 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, souid);
1393 ctf_dmdef_t *dmd;
1395 ssize_t msize, malign, ssize;
1396 uint32_t kind, vlen, root;
1397 char *s = NULL;
1399 if (!(fp->ctf_flags & LCTF_RDWR))
1400 return (ctf_set_errno (fp, ECTF_RDONLY));
1402 if (dtd == NULL)
1403 return (ctf_set_errno (fp, ECTF_BADID));
1405 if (name != NULL && name[0] == '\0')
1406 name = NULL;
1408 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1409 root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info);
1410 vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
1412 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
1413 return (ctf_set_errno (fp, ECTF_NOTSOU));
1415 if (vlen == CTF_MAX_VLEN)
1416 return (ctf_set_errno (fp, ECTF_DTFULL));
1418 if (name != NULL)
1420 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
1421 dmd != NULL; dmd = ctf_list_next (dmd))
1423 if (dmd->dmd_name != NULL && strcmp (dmd->dmd_name, name) == 0)
1424 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1428 if ((msize = ctf_type_size (fp, type)) < 0 ||
1429 (malign = ctf_type_align (fp, type)) < 0)
1431 /* The unimplemented type, and any type that resolves to it, has no size
1432 and no alignment: it can correspond to any number of compiler-inserted
1433 types. */
1435 if (ctf_errno (fp) == ECTF_NONREPRESENTABLE)
1437 msize = 0;
1438 malign = 0;
1439 ctf_set_errno (fp, 0);
1441 else
1442 return -1; /* errno is set for us. */
1445 if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
1446 return (ctf_set_errno (fp, EAGAIN));
1448 if (name != NULL && (s = strdup (name)) == NULL)
1450 free (dmd);
1451 return (ctf_set_errno (fp, EAGAIN));
1454 dmd->dmd_name = s;
1455 dmd->dmd_type = type;
1456 dmd->dmd_value = -1;
1458 if (kind == CTF_K_STRUCT && vlen != 0)
1460 if (bit_offset == (unsigned long) - 1)
1462 /* Natural alignment. */
1464 ctf_dmdef_t *lmd = ctf_list_prev (&dtd->dtd_u.dtu_members);
1465 ctf_id_t ltype = ctf_type_resolve (fp, lmd->dmd_type);
1466 size_t off = lmd->dmd_offset;
1468 ctf_encoding_t linfo;
1469 ssize_t lsize;
1471 /* Propagate any error from ctf_type_resolve. If the last member was
1472 of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we
1473 cannot insert right after such a member without explicit offset
1474 specification, because its alignment and size is not known. */
1475 if (ltype == CTF_ERR)
1477 free (dmd);
1478 return -1; /* errno is set for us. */
1481 if (ctf_type_encoding (fp, ltype, &linfo) == 0)
1482 off += linfo.cte_bits;
1483 else if ((lsize = ctf_type_size (fp, ltype)) > 0)
1484 off += lsize * CHAR_BIT;
1486 /* Round up the offset of the end of the last member to
1487 the next byte boundary, convert 'off' to bytes, and
1488 then round it up again to the next multiple of the
1489 alignment required by the new member. Finally,
1490 convert back to bits and store the result in
1491 dmd_offset. Technically we could do more efficient
1492 packing if the new member is a bit-field, but we're
1493 the "compiler" and ANSI says we can do as we choose. */
1495 off = roundup (off, CHAR_BIT) / CHAR_BIT;
1496 off = roundup (off, MAX (malign, 1));
1497 dmd->dmd_offset = off * CHAR_BIT;
1498 ssize = off + msize;
1500 else
1502 /* Specified offset in bits. */
1504 dmd->dmd_offset = bit_offset;
1505 ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL);
1506 ssize = MAX (ssize, ((signed) bit_offset / CHAR_BIT) + msize);
1509 else
1511 dmd->dmd_offset = 0;
1512 ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL);
1513 ssize = MAX (ssize, msize);
1516 if ((size_t) ssize > CTF_MAX_SIZE)
1518 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1519 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (ssize);
1520 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (ssize);
1522 else
1523 dtd->dtd_data.ctt_size = (uint32_t) ssize;
1525 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1);
1526 ctf_list_append (&dtd->dtd_u.dtu_members, dmd);
1528 fp->ctf_flags |= LCTF_DIRTY;
1529 return 0;
1533 ctf_add_member_encoded (ctf_file_t *fp, ctf_id_t souid, const char *name,
1534 ctf_id_t type, unsigned long bit_offset,
1535 const ctf_encoding_t encoding)
1537 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type);
1538 int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1539 int otype = type;
1541 if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) && (kind != CTF_K_ENUM))
1542 return (ctf_set_errno (fp, ECTF_NOTINTFP));
1544 if ((type = ctf_add_slice (fp, CTF_ADD_NONROOT, otype, &encoding)) == CTF_ERR)
1545 return -1; /* errno is set for us. */
1547 return ctf_add_member_offset (fp, souid, name, type, bit_offset);
1551 ctf_add_member (ctf_file_t *fp, ctf_id_t souid, const char *name,
1552 ctf_id_t type)
1554 return ctf_add_member_offset (fp, souid, name, type, (unsigned long) - 1);
1558 ctf_add_variable (ctf_file_t *fp, const char *name, ctf_id_t ref)
1560 ctf_dvdef_t *dvd;
1561 ctf_file_t *tmp = fp;
1563 if (!(fp->ctf_flags & LCTF_RDWR))
1564 return (ctf_set_errno (fp, ECTF_RDONLY));
1566 if (ctf_dvd_lookup (fp, name) != NULL)
1567 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1569 if (ctf_lookup_by_id (&tmp, ref) == NULL)
1570 return -1; /* errno is set for us. */
1572 /* Make sure this type is representable. */
1573 if ((ctf_type_resolve (fp, ref) == CTF_ERR)
1574 && (ctf_errno (fp) == ECTF_NONREPRESENTABLE))
1575 return -1;
1577 if ((dvd = malloc (sizeof (ctf_dvdef_t))) == NULL)
1578 return (ctf_set_errno (fp, EAGAIN));
1580 if (name != NULL && (dvd->dvd_name = strdup (name)) == NULL)
1582 free (dvd);
1583 return (ctf_set_errno (fp, EAGAIN));
1585 dvd->dvd_type = ref;
1586 dvd->dvd_snapshots = fp->ctf_snapshots;
1588 if (ctf_dvd_insert (fp, dvd) < 0)
1590 free (dvd->dvd_name);
1591 free (dvd);
1592 return -1; /* errno is set for us. */
1595 fp->ctf_flags |= LCTF_DIRTY;
1596 return 0;
1599 typedef struct ctf_bundle
1601 ctf_file_t *ctb_file; /* CTF container handle. */
1602 ctf_id_t ctb_type; /* CTF type identifier. */
1603 ctf_dtdef_t *ctb_dtd; /* CTF dynamic type definition (if any). */
1604 } ctf_bundle_t;
1606 static int
1607 enumcmp (const char *name, int value, void *arg)
1609 ctf_bundle_t *ctb = arg;
1610 int bvalue;
1612 if (ctf_enum_value (ctb->ctb_file, ctb->ctb_type, name, &bvalue) < 0)
1614 ctf_err_warn (ctb->ctb_file, 0, 0,
1615 _("conflict due to enum %s iteration error"), name);
1616 return 1;
1618 if (value != bvalue)
1620 ctf_err_warn (ctb->ctb_file, 1, ECTF_CONFLICT,
1621 _("conflict due to enum value change: %i versus %i"),
1622 value, bvalue);
1623 return 1;
1625 return 0;
1628 static int
1629 enumadd (const char *name, int value, void *arg)
1631 ctf_bundle_t *ctb = arg;
1633 return (ctf_add_enumerator (ctb->ctb_file, ctb->ctb_type,
1634 name, value) < 0);
1637 static int
1638 membcmp (const char *name, ctf_id_t type _libctf_unused_, unsigned long offset,
1639 void *arg)
1641 ctf_bundle_t *ctb = arg;
1642 ctf_membinfo_t ctm;
1644 /* Don't check nameless members (e.g. anonymous structs/unions) against each
1645 other. */
1646 if (name[0] == 0)
1647 return 0;
1649 if (ctf_member_info (ctb->ctb_file, ctb->ctb_type, name, &ctm) < 0)
1651 ctf_err_warn (ctb->ctb_file, 0, 0,
1652 _("conflict due to struct member %s iteration error"),
1653 name);
1654 return 1;
1656 if (ctm.ctm_offset != offset)
1658 ctf_err_warn (ctb->ctb_file, 1, ECTF_CONFLICT,
1659 _("conflict due to struct member %s offset change: "
1660 "%lx versus %lx"),
1661 name, ctm.ctm_offset, offset);
1662 return 1;
1664 return 0;
1667 static int
1668 membadd (const char *name, ctf_id_t type, unsigned long offset, void *arg)
1670 ctf_bundle_t *ctb = arg;
1671 ctf_dmdef_t *dmd;
1672 char *s = NULL;
1674 if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
1675 return (ctf_set_errno (ctb->ctb_file, EAGAIN));
1677 if (name != NULL && (s = strdup (name)) == NULL)
1679 free (dmd);
1680 return (ctf_set_errno (ctb->ctb_file, EAGAIN));
1683 /* For now, dmd_type is copied as the src_fp's type; it is reset to an
1684 equivalent dst_fp type by a final loop in ctf_add_type(), below. */
1685 dmd->dmd_name = s;
1686 dmd->dmd_type = type;
1687 dmd->dmd_offset = offset;
1688 dmd->dmd_value = -1;
1690 ctf_list_append (&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
1692 ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
1693 return 0;
1696 /* The ctf_add_type routine is used to copy a type from a source CTF container
1697 to a dynamic destination container. This routine operates recursively by
1698 following the source type's links and embedded member types. If the
1699 destination container already contains a named type which has the same
1700 attributes, then we succeed and return this type but no changes occur. */
1701 static ctf_id_t
1702 ctf_add_type_internal (ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type,
1703 ctf_file_t *proc_tracking_fp)
1705 ctf_id_t dst_type = CTF_ERR;
1706 uint32_t dst_kind = CTF_K_UNKNOWN;
1707 ctf_file_t *tmp_fp = dst_fp;
1708 ctf_id_t tmp;
1710 const char *name;
1711 uint32_t kind, forward_kind, flag, vlen;
1713 const ctf_type_t *src_tp, *dst_tp;
1714 ctf_bundle_t src, dst;
1715 ctf_encoding_t src_en, dst_en;
1716 ctf_arinfo_t src_ar, dst_ar;
1718 ctf_funcinfo_t ctc;
1720 ctf_id_t orig_src_type = src_type;
1722 if (!(dst_fp->ctf_flags & LCTF_RDWR))
1723 return (ctf_set_errno (dst_fp, ECTF_RDONLY));
1725 if ((src_tp = ctf_lookup_by_id (&src_fp, src_type)) == NULL)
1726 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
1728 if ((ctf_type_resolve (src_fp, src_type) == CTF_ERR)
1729 && (ctf_errno (src_fp) == ECTF_NONREPRESENTABLE))
1730 return (ctf_set_errno (dst_fp, ECTF_NONREPRESENTABLE));
1732 name = ctf_strptr (src_fp, src_tp->ctt_name);
1733 kind = LCTF_INFO_KIND (src_fp, src_tp->ctt_info);
1734 flag = LCTF_INFO_ISROOT (src_fp, src_tp->ctt_info);
1735 vlen = LCTF_INFO_VLEN (src_fp, src_tp->ctt_info);
1737 /* If this is a type we are currently in the middle of adding, hand it
1738 straight back. (This lets us handle self-referential structures without
1739 considering forwards and empty structures the same as their completed
1740 forms.) */
1742 tmp = ctf_type_mapping (src_fp, src_type, &tmp_fp);
1744 if (tmp != 0)
1746 if (ctf_dynhash_lookup (proc_tracking_fp->ctf_add_processing,
1747 (void *) (uintptr_t) src_type))
1748 return tmp;
1750 /* If this type has already been added from this container, and is the same
1751 kind and (if a struct or union) has the same number of members, hand it
1752 straight back. */
1754 if (ctf_type_kind_unsliced (tmp_fp, tmp) == (int) kind)
1756 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION
1757 || kind == CTF_K_ENUM)
1759 if ((dst_tp = ctf_lookup_by_id (&tmp_fp, dst_type)) != NULL)
1760 if (vlen == LCTF_INFO_VLEN (tmp_fp, dst_tp->ctt_info))
1761 return tmp;
1763 else
1764 return tmp;
1768 forward_kind = kind;
1769 if (kind == CTF_K_FORWARD)
1770 forward_kind = src_tp->ctt_type;
1772 /* If the source type has a name and is a root type (visible at the
1773 top-level scope), lookup the name in the destination container and
1774 verify that it is of the same kind before we do anything else. */
1776 if ((flag & CTF_ADD_ROOT) && name[0] != '\0'
1777 && (tmp = ctf_lookup_by_rawname (dst_fp, forward_kind, name)) != 0)
1779 dst_type = tmp;
1780 dst_kind = ctf_type_kind_unsliced (dst_fp, dst_type);
1783 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
1784 unless dst_type is a forward declaration and src_type is a struct,
1785 union, or enum (i.e. the definition of the previous forward decl).
1787 We also allow addition in the opposite order (addition of a forward when a
1788 struct, union, or enum already exists), which is a NOP and returns the
1789 already-present struct, union, or enum. */
1791 if (dst_type != CTF_ERR && dst_kind != kind)
1793 if (kind == CTF_K_FORWARD
1794 && (dst_kind == CTF_K_ENUM || dst_kind == CTF_K_STRUCT
1795 || dst_kind == CTF_K_UNION))
1797 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
1798 return dst_type;
1801 if (dst_kind != CTF_K_FORWARD
1802 || (kind != CTF_K_ENUM && kind != CTF_K_STRUCT
1803 && kind != CTF_K_UNION))
1805 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT,
1806 _("ctf_add_file(): conflict for type %s: "
1807 "kinds differ, new: %i; old (ID %lx): %i"),
1808 name, kind, dst_type, dst_kind);
1809 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1813 /* We take special action for an integer, float, or slice since it is
1814 described not only by its name but also its encoding. For integers,
1815 bit-fields exploit this degeneracy. */
1817 if (kind == CTF_K_INTEGER || kind == CTF_K_FLOAT || kind == CTF_K_SLICE)
1819 if (ctf_type_encoding (src_fp, src_type, &src_en) != 0)
1820 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
1822 if (dst_type != CTF_ERR)
1824 ctf_file_t *fp = dst_fp;
1826 if ((dst_tp = ctf_lookup_by_id (&fp, dst_type)) == NULL)
1827 return CTF_ERR;
1829 if (ctf_type_encoding (dst_fp, dst_type, &dst_en) != 0)
1830 return CTF_ERR; /* errno set for us. */
1832 if (LCTF_INFO_ISROOT (fp, dst_tp->ctt_info) & CTF_ADD_ROOT)
1834 /* The type that we found in the hash is also root-visible. If
1835 the two types match then use the existing one; otherwise,
1836 declare a conflict. Note: slices are not certain to match
1837 even if there is no conflict: we must check the contained type
1838 too. */
1840 if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0)
1842 if (kind != CTF_K_SLICE)
1844 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
1845 return dst_type;
1848 else
1850 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1853 else
1855 /* We found a non-root-visible type in the hash. If its encoding
1856 is the same, we can reuse it, unless it is a slice. */
1858 if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0)
1860 if (kind != CTF_K_SLICE)
1862 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
1863 return dst_type;
1870 src.ctb_file = src_fp;
1871 src.ctb_type = src_type;
1872 src.ctb_dtd = NULL;
1874 dst.ctb_file = dst_fp;
1875 dst.ctb_type = dst_type;
1876 dst.ctb_dtd = NULL;
1878 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add
1879 a new type with the same properties as src_type to dst_fp. If dst_type is
1880 not CTF_ERR, then we verify that dst_type has the same attributes as
1881 src_type. We recurse for embedded references. Before we start, we note
1882 that we are processing this type, to prevent infinite recursion: we do not
1883 re-process any type that appears in this list. The list is emptied
1884 wholesale at the end of processing everything in this recursive stack. */
1886 if (ctf_dynhash_insert (proc_tracking_fp->ctf_add_processing,
1887 (void *) (uintptr_t) src_type, (void *) 1) < 0)
1888 return ctf_set_errno (dst_fp, ENOMEM);
1890 switch (kind)
1892 case CTF_K_INTEGER:
1893 /* If we found a match we will have either returned it or declared a
1894 conflict. */
1895 dst_type = ctf_add_integer (dst_fp, flag, name, &src_en);
1896 break;
1898 case CTF_K_FLOAT:
1899 /* If we found a match we will have either returned it or declared a
1900 conflict. */
1901 dst_type = ctf_add_float (dst_fp, flag, name, &src_en);
1902 break;
1904 case CTF_K_SLICE:
1905 /* We have checked for conflicting encodings: now try to add the
1906 contained type. */
1907 src_type = ctf_type_reference (src_fp, src_type);
1908 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type,
1909 proc_tracking_fp);
1911 if (src_type == CTF_ERR)
1912 return CTF_ERR; /* errno is set for us. */
1914 dst_type = ctf_add_slice (dst_fp, flag, src_type, &src_en);
1915 break;
1917 case CTF_K_POINTER:
1918 case CTF_K_VOLATILE:
1919 case CTF_K_CONST:
1920 case CTF_K_RESTRICT:
1921 src_type = ctf_type_reference (src_fp, src_type);
1922 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type,
1923 proc_tracking_fp);
1925 if (src_type == CTF_ERR)
1926 return CTF_ERR; /* errno is set for us. */
1928 dst_type = ctf_add_reftype (dst_fp, flag, src_type, kind);
1929 break;
1931 case CTF_K_ARRAY:
1932 if (ctf_array_info (src_fp, src_type, &src_ar) != 0)
1933 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
1935 src_ar.ctr_contents =
1936 ctf_add_type_internal (dst_fp, src_fp, src_ar.ctr_contents,
1937 proc_tracking_fp);
1938 src_ar.ctr_index = ctf_add_type_internal (dst_fp, src_fp,
1939 src_ar.ctr_index,
1940 proc_tracking_fp);
1941 src_ar.ctr_nelems = src_ar.ctr_nelems;
1943 if (src_ar.ctr_contents == CTF_ERR || src_ar.ctr_index == CTF_ERR)
1944 return CTF_ERR; /* errno is set for us. */
1946 if (dst_type != CTF_ERR)
1948 if (ctf_array_info (dst_fp, dst_type, &dst_ar) != 0)
1949 return CTF_ERR; /* errno is set for us. */
1951 if (memcmp (&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
1953 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT,
1954 _("conflict for type %s against ID %lx: array info "
1955 "differs, old %lx/%lx/%x; new: %lx/%lx/%x"),
1956 name, dst_type, src_ar.ctr_contents,
1957 src_ar.ctr_index, src_ar.ctr_nelems,
1958 dst_ar.ctr_contents, dst_ar.ctr_index,
1959 dst_ar.ctr_nelems);
1960 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1963 else
1964 dst_type = ctf_add_array (dst_fp, flag, &src_ar);
1965 break;
1967 case CTF_K_FUNCTION:
1968 ctc.ctc_return = ctf_add_type_internal (dst_fp, src_fp,
1969 src_tp->ctt_type,
1970 proc_tracking_fp);
1971 ctc.ctc_argc = 0;
1972 ctc.ctc_flags = 0;
1974 if (ctc.ctc_return == CTF_ERR)
1975 return CTF_ERR; /* errno is set for us. */
1977 dst_type = ctf_add_function (dst_fp, flag, &ctc, NULL);
1978 break;
1980 case CTF_K_STRUCT:
1981 case CTF_K_UNION:
1983 ctf_dmdef_t *dmd;
1984 int errs = 0;
1985 size_t size;
1986 ssize_t ssize;
1987 ctf_dtdef_t *dtd;
1989 /* Technically to match a struct or union we need to check both
1990 ways (src members vs. dst, dst members vs. src) but we make
1991 this more optimal by only checking src vs. dst and comparing
1992 the total size of the structure (which we must do anyway)
1993 which covers the possibility of dst members not in src.
1994 This optimization can be defeated for unions, but is so
1995 pathological as to render it irrelevant for our purposes. */
1997 if (dst_type != CTF_ERR && kind != CTF_K_FORWARD
1998 && dst_kind != CTF_K_FORWARD)
2000 if (ctf_type_size (src_fp, src_type) !=
2001 ctf_type_size (dst_fp, dst_type))
2003 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT,
2004 _("conflict for type %s against ID %lx: union "
2005 "size differs, old %li, new %li"), name,
2006 dst_type, (long) ctf_type_size (src_fp, src_type),
2007 (long) ctf_type_size (dst_fp, dst_type));
2008 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
2011 if (ctf_member_iter (src_fp, src_type, membcmp, &dst))
2013 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT,
2014 _("conflict for type %s against ID %lx: members "
2015 "differ, see above"), name, dst_type);
2016 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
2019 break;
2022 /* Unlike the other cases, copying structs and unions is done
2023 manually so as to avoid repeated lookups in ctf_add_member
2024 and to ensure the exact same member offsets as in src_type. */
2026 dst_type = ctf_add_generic (dst_fp, flag, name, kind, &dtd);
2027 if (dst_type == CTF_ERR)
2028 return CTF_ERR; /* errno is set for us. */
2030 dst.ctb_type = dst_type;
2031 dst.ctb_dtd = dtd;
2033 /* Pre-emptively add this struct to the type mapping so that
2034 structures that refer to themselves work. */
2035 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
2037 if (ctf_member_iter (src_fp, src_type, membadd, &dst) != 0)
2038 errs++; /* Increment errs and fail at bottom of case. */
2040 if ((ssize = ctf_type_size (src_fp, src_type)) < 0)
2041 return CTF_ERR; /* errno is set for us. */
2043 size = (size_t) ssize;
2044 if (size > CTF_MAX_SIZE)
2046 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
2047 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
2048 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
2050 else
2051 dtd->dtd_data.ctt_size = (uint32_t) size;
2053 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, vlen);
2055 /* Make a final pass through the members changing each dmd_type (a
2056 src_fp type) to an equivalent type in dst_fp. We pass through all
2057 members, leaving any that fail set to CTF_ERR, unless they fail
2058 because they are marking a member of type not representable in this
2059 version of CTF, in which case we just want to silently omit them:
2060 no consumer can do anything with them anyway. */
2061 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
2062 dmd != NULL; dmd = ctf_list_next (dmd))
2064 ctf_file_t *dst = dst_fp;
2065 ctf_id_t memb_type;
2067 memb_type = ctf_type_mapping (src_fp, dmd->dmd_type, &dst);
2068 if (memb_type == 0)
2070 if ((dmd->dmd_type =
2071 ctf_add_type_internal (dst_fp, src_fp, dmd->dmd_type,
2072 proc_tracking_fp)) == CTF_ERR)
2074 if (ctf_errno (dst_fp) != ECTF_NONREPRESENTABLE)
2075 errs++;
2078 else
2079 dmd->dmd_type = memb_type;
2082 if (errs)
2083 return CTF_ERR; /* errno is set for us. */
2084 break;
2087 case CTF_K_ENUM:
2088 if (dst_type != CTF_ERR && kind != CTF_K_FORWARD
2089 && dst_kind != CTF_K_FORWARD)
2091 if (ctf_enum_iter (src_fp, src_type, enumcmp, &dst)
2092 || ctf_enum_iter (dst_fp, dst_type, enumcmp, &src))
2094 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT,
2095 _("conflict for enum %s against ID %lx: members "
2096 "differ, see above"), name, dst_type);
2097 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
2100 else
2102 dst_type = ctf_add_enum (dst_fp, flag, name);
2103 if ((dst.ctb_type = dst_type) == CTF_ERR
2104 || ctf_enum_iter (src_fp, src_type, enumadd, &dst))
2105 return CTF_ERR; /* errno is set for us */
2107 break;
2109 case CTF_K_FORWARD:
2110 if (dst_type == CTF_ERR)
2111 dst_type = ctf_add_forward (dst_fp, flag, name, forward_kind);
2112 break;
2114 case CTF_K_TYPEDEF:
2115 src_type = ctf_type_reference (src_fp, src_type);
2116 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type,
2117 proc_tracking_fp);
2119 if (src_type == CTF_ERR)
2120 return CTF_ERR; /* errno is set for us. */
2122 /* If dst_type is not CTF_ERR at this point, we should check if
2123 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
2124 ECTF_CONFLICT. However, this causes problems with bitness typedefs
2125 that vary based on things like if 32-bit then pid_t is int otherwise
2126 long. We therefore omit this check and assume that if the identically
2127 named typedef already exists in dst_fp, it is correct or
2128 equivalent. */
2130 if (dst_type == CTF_ERR)
2131 dst_type = ctf_add_typedef (dst_fp, flag, name, src_type);
2133 break;
2135 default:
2136 return (ctf_set_errno (dst_fp, ECTF_CORRUPT));
2139 if (dst_type != CTF_ERR)
2140 ctf_add_type_mapping (src_fp, orig_src_type, dst_fp, dst_type);
2141 return dst_type;
2144 ctf_id_t
2145 ctf_add_type (ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
2147 ctf_id_t id;
2149 if (!src_fp->ctf_add_processing)
2150 src_fp->ctf_add_processing = ctf_dynhash_create (ctf_hash_integer,
2151 ctf_hash_eq_integer,
2152 NULL, NULL);
2154 /* We store the hash on the source, because it contains only source type IDs:
2155 but callers will invariably expect errors to appear on the dest. */
2156 if (!src_fp->ctf_add_processing)
2157 return (ctf_set_errno (dst_fp, ENOMEM));
2159 id = ctf_add_type_internal (dst_fp, src_fp, src_type, src_fp);
2160 ctf_dynhash_empty (src_fp->ctf_add_processing);
2162 return id;
2165 /* Write the compressed CTF data stream to the specified gzFile descriptor. */
2167 ctf_gzwrite (ctf_file_t *fp, gzFile fd)
2169 const unsigned char *buf;
2170 ssize_t resid;
2171 ssize_t len;
2173 resid = sizeof (ctf_header_t);
2174 buf = (unsigned char *) fp->ctf_header;
2175 while (resid != 0)
2177 if ((len = gzwrite (fd, buf, resid)) <= 0)
2178 return (ctf_set_errno (fp, errno));
2179 resid -= len;
2180 buf += len;
2183 resid = fp->ctf_size;
2184 buf = fp->ctf_buf;
2185 while (resid != 0)
2187 if ((len = gzwrite (fd, buf, resid)) <= 0)
2188 return (ctf_set_errno (fp, errno));
2189 resid -= len;
2190 buf += len;
2193 return 0;
2196 /* Compress the specified CTF data stream and write it to the specified file
2197 descriptor. */
2199 ctf_compress_write (ctf_file_t *fp, int fd)
2201 unsigned char *buf;
2202 unsigned char *bp;
2203 ctf_header_t h;
2204 ctf_header_t *hp = &h;
2205 ssize_t header_len = sizeof (ctf_header_t);
2206 ssize_t compress_len;
2207 ssize_t len;
2208 int rc;
2209 int err = 0;
2211 if (ctf_serialize (fp) < 0)
2212 return -1; /* errno is set for us. */
2214 memcpy (hp, fp->ctf_header, header_len);
2215 hp->cth_flags |= CTF_F_COMPRESS;
2216 compress_len = compressBound (fp->ctf_size);
2218 if ((buf = malloc (compress_len)) == NULL)
2220 ctf_err_warn (fp, 0, 0, _("ctf_compress_write: cannot allocate %li bytes"),
2221 (unsigned long) compress_len);
2222 return (ctf_set_errno (fp, ECTF_ZALLOC));
2225 if ((rc = compress (buf, (uLongf *) &compress_len,
2226 fp->ctf_buf, fp->ctf_size)) != Z_OK)
2228 err = ctf_set_errno (fp, ECTF_COMPRESS);
2229 ctf_err_warn (fp, 0, 0, _("zlib deflate err: %s"), zError (rc));
2230 goto ret;
2233 while (header_len > 0)
2235 if ((len = write (fd, hp, header_len)) < 0)
2237 err = ctf_set_errno (fp, errno);
2238 ctf_err_warn (fp, 0, 0, _("ctf_compress_write: error writing header"));
2239 goto ret;
2241 header_len -= len;
2242 hp += len;
2245 bp = buf;
2246 while (compress_len > 0)
2248 if ((len = write (fd, bp, compress_len)) < 0)
2250 err = ctf_set_errno (fp, errno);
2251 ctf_err_warn (fp, 0, 0, _("ctf_compress_write: error writing"));
2252 goto ret;
2254 compress_len -= len;
2255 bp += len;
2258 ret:
2259 free (buf);
2260 return err;
2263 /* Optionally compress the specified CTF data stream and return it as a new
2264 dynamically-allocated string. */
2265 unsigned char *
2266 ctf_write_mem (ctf_file_t *fp, size_t *size, size_t threshold)
2268 unsigned char *buf;
2269 unsigned char *bp;
2270 ctf_header_t *hp;
2271 ssize_t header_len = sizeof (ctf_header_t);
2272 ssize_t compress_len;
2273 int rc;
2275 if (ctf_serialize (fp) < 0)
2276 return NULL; /* errno is set for us. */
2278 compress_len = compressBound (fp->ctf_size);
2279 if (fp->ctf_size < threshold)
2280 compress_len = fp->ctf_size;
2281 if ((buf = malloc (compress_len
2282 + sizeof (struct ctf_header))) == NULL)
2284 ctf_set_errno (fp, ENOMEM);
2285 ctf_err_warn (fp, 0, 0, _("ctf_write_mem: cannot allocate %li bytes"),
2286 (unsigned long) (compress_len + sizeof (struct ctf_header)));
2287 return NULL;
2290 hp = (ctf_header_t *) buf;
2291 memcpy (hp, fp->ctf_header, header_len);
2292 bp = buf + sizeof (struct ctf_header);
2293 *size = sizeof (struct ctf_header);
2295 if (fp->ctf_size < threshold)
2297 hp->cth_flags &= ~CTF_F_COMPRESS;
2298 memcpy (bp, fp->ctf_buf, fp->ctf_size);
2299 *size += fp->ctf_size;
2301 else
2303 hp->cth_flags |= CTF_F_COMPRESS;
2304 if ((rc = compress (bp, (uLongf *) &compress_len,
2305 fp->ctf_buf, fp->ctf_size)) != Z_OK)
2307 ctf_set_errno (fp, ECTF_COMPRESS);
2308 ctf_err_warn (fp, 0, 0, _("zlib deflate err: %s"), zError (rc));
2309 free (buf);
2310 return NULL;
2312 *size += compress_len;
2314 return buf;
2317 /* Write the uncompressed CTF data stream to the specified file descriptor. */
2319 ctf_write (ctf_file_t *fp, int fd)
2321 const unsigned char *buf;
2322 ssize_t resid;
2323 ssize_t len;
2325 if (ctf_serialize (fp) < 0)
2326 return -1; /* errno is set for us. */
2328 resid = sizeof (ctf_header_t);
2329 buf = (unsigned char *) fp->ctf_header;
2330 while (resid != 0)
2332 if ((len = write (fd, buf, resid)) <= 0)
2334 ctf_err_warn (fp, 0, errno, _("ctf_write: error writing header"));
2335 return (ctf_set_errno (fp, errno));
2337 resid -= len;
2338 buf += len;
2341 resid = fp->ctf_size;
2342 buf = fp->ctf_buf;
2343 while (resid != 0)
2345 if ((len = write (fd, buf, resid)) <= 0)
2347 ctf_err_warn (fp, 0, errno, _("ctf_write: error writing"));
2348 return (ctf_set_errno (fp, errno));
2350 resid -= len;
2351 buf += len;
2354 return 0;