libctf: sort out potential refcount loops
[binutils-gdb.git] / libctf / ctf-create.c
1 /* CTF file creation.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3
4 This file is part of libctf.
5
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.
10
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.
15
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/>. */
19
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>
26
27 #ifndef roundup
28 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
29 #endif
30
31 /* Make sure the ptrtab has enough space for at least one more type.
32
33 We start with 4KiB of ptrtab, enough for a thousand types, then grow it 25%
34 at a time. */
35
36 static int
37 ctf_grow_ptrtab (ctf_file_t *fp)
38 {
39 size_t new_ptrtab_len = fp->ctf_ptrtab_len;
40
41 /* We allocate one more ptrtab entry than we need, for the initial zero,
42 plus one because the caller will probably allocate a new type. */
43
44 if (fp->ctf_ptrtab == NULL)
45 new_ptrtab_len = 1024;
46 else if ((fp->ctf_typemax + 2) > fp->ctf_ptrtab_len)
47 new_ptrtab_len = fp->ctf_ptrtab_len * 1.25;
48
49 if (new_ptrtab_len != fp->ctf_ptrtab_len)
50 {
51 uint32_t *new_ptrtab;
52
53 if ((new_ptrtab = realloc (fp->ctf_ptrtab,
54 new_ptrtab_len * sizeof (uint32_t))) == NULL)
55 return (ctf_set_errno (fp, ENOMEM));
56
57 fp->ctf_ptrtab = new_ptrtab;
58 memset (fp->ctf_ptrtab + fp->ctf_ptrtab_len, 0,
59 (new_ptrtab_len - fp->ctf_ptrtab_len) * sizeof (uint32_t));
60 fp->ctf_ptrtab_len = new_ptrtab_len;
61 }
62 return 0;
63 }
64
65 /* To create an empty CTF container, we just declare a zeroed header and call
66 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w
67 and initialize the dynamic members. We start assigning type IDs at 1 because
68 type ID 0 is used as a sentinel and a not-found indicator. */
69
70 ctf_file_t *
71 ctf_create (int *errp)
72 {
73 static const ctf_header_t hdr = { .cth_preamble = { CTF_MAGIC, CTF_VERSION, 0 } };
74
75 ctf_dynhash_t *dthash;
76 ctf_dynhash_t *dvhash;
77 ctf_dynhash_t *structs = NULL, *unions = NULL, *enums = NULL, *names = NULL;
78 ctf_sect_t cts;
79 ctf_file_t *fp;
80
81 libctf_init_debug();
82 dthash = ctf_dynhash_create (ctf_hash_integer, ctf_hash_eq_integer,
83 NULL, NULL);
84 if (dthash == NULL)
85 {
86 ctf_set_open_errno (errp, EAGAIN);
87 goto err;
88 }
89
90 dvhash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
91 NULL, NULL);
92 if (dvhash == NULL)
93 {
94 ctf_set_open_errno (errp, EAGAIN);
95 goto err_dt;
96 }
97
98 structs = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
99 NULL, NULL);
100 unions = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
101 NULL, NULL);
102 enums = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
103 NULL, NULL);
104 names = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
105 NULL, NULL);
106 if (!structs || !unions || !enums || !names)
107 {
108 ctf_set_open_errno (errp, EAGAIN);
109 goto err_dv;
110 }
111
112 cts.cts_name = _CTF_SECTION;
113 cts.cts_data = &hdr;
114 cts.cts_size = sizeof (hdr);
115 cts.cts_entsize = 1;
116
117 if ((fp = ctf_bufopen_internal (&cts, NULL, NULL, NULL, 1, errp)) == NULL)
118 goto err_dv;
119
120 fp->ctf_structs.ctn_writable = structs;
121 fp->ctf_unions.ctn_writable = unions;
122 fp->ctf_enums.ctn_writable = enums;
123 fp->ctf_names.ctn_writable = names;
124 fp->ctf_dthash = dthash;
125 fp->ctf_dvhash = dvhash;
126 fp->ctf_dtoldid = 0;
127 fp->ctf_snapshots = 1;
128 fp->ctf_snapshot_lu = 0;
129 fp->ctf_flags |= LCTF_DIRTY;
130
131 ctf_set_ctl_hashes (fp);
132 ctf_setmodel (fp, CTF_MODEL_NATIVE);
133 if (ctf_grow_ptrtab (fp) < 0)
134 {
135 ctf_set_open_errno (errp, ctf_errno (fp));
136 ctf_file_close (fp);
137 return NULL;
138 }
139
140 return fp;
141
142 err_dv:
143 ctf_dynhash_destroy (structs);
144 ctf_dynhash_destroy (unions);
145 ctf_dynhash_destroy (enums);
146 ctf_dynhash_destroy (names);
147 ctf_dynhash_destroy (dvhash);
148 err_dt:
149 ctf_dynhash_destroy (dthash);
150 err:
151 return NULL;
152 }
153
154 static unsigned char *
155 ctf_copy_smembers (ctf_file_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
156 {
157 ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
158 ctf_member_t ctm;
159
160 for (; dmd != NULL; dmd = ctf_list_next (dmd))
161 {
162 ctf_member_t *copied;
163
164 ctm.ctm_name = 0;
165 ctm.ctm_type = (uint32_t) dmd->dmd_type;
166 ctm.ctm_offset = (uint32_t) dmd->dmd_offset;
167
168 memcpy (t, &ctm, sizeof (ctm));
169 copied = (ctf_member_t *) t;
170 if (dmd->dmd_name)
171 ctf_str_add_ref (fp, dmd->dmd_name, &copied->ctm_name);
172
173 t += sizeof (ctm);
174 }
175
176 return t;
177 }
178
179 static unsigned char *
180 ctf_copy_lmembers (ctf_file_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
181 {
182 ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
183 ctf_lmember_t ctlm;
184
185 for (; dmd != NULL; dmd = ctf_list_next (dmd))
186 {
187 ctf_lmember_t *copied;
188
189 ctlm.ctlm_name = 0;
190 ctlm.ctlm_type = (uint32_t) dmd->dmd_type;
191 ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI (dmd->dmd_offset);
192 ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO (dmd->dmd_offset);
193
194 memcpy (t, &ctlm, sizeof (ctlm));
195 copied = (ctf_lmember_t *) t;
196 if (dmd->dmd_name)
197 ctf_str_add_ref (fp, dmd->dmd_name, &copied->ctlm_name);
198
199 t += sizeof (ctlm);
200 }
201
202 return t;
203 }
204
205 static unsigned char *
206 ctf_copy_emembers (ctf_file_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
207 {
208 ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
209 ctf_enum_t cte;
210
211 for (; dmd != NULL; dmd = ctf_list_next (dmd))
212 {
213 ctf_enum_t *copied;
214
215 cte.cte_value = dmd->dmd_value;
216 memcpy (t, &cte, sizeof (cte));
217 copied = (ctf_enum_t *) t;
218 ctf_str_add_ref (fp, dmd->dmd_name, &copied->cte_name);
219 t += sizeof (cte);
220 }
221
222 return t;
223 }
224
225 /* Sort a newly-constructed static variable array. */
226
227 typedef struct ctf_sort_var_arg_cb
228 {
229 ctf_file_t *fp;
230 ctf_strs_t *strtab;
231 } ctf_sort_var_arg_cb_t;
232
233 static int
234 ctf_sort_var (const void *one_, const void *two_, void *arg_)
235 {
236 const ctf_varent_t *one = one_;
237 const ctf_varent_t *two = two_;
238 ctf_sort_var_arg_cb_t *arg = arg_;
239
240 return (strcmp (ctf_strraw_explicit (arg->fp, one->ctv_name, arg->strtab),
241 ctf_strraw_explicit (arg->fp, two->ctv_name, arg->strtab)));
242 }
243
244 /* Compatibility: just update the threshold for ctf_discard. */
245 int
246 ctf_update (ctf_file_t *fp)
247 {
248 if (!(fp->ctf_flags & LCTF_RDWR))
249 return (ctf_set_errno (fp, ECTF_RDONLY));
250
251 fp->ctf_dtoldid = fp->ctf_typemax;
252 return 0;
253 }
254
255 /* If the specified CTF container is writable and has been modified, reload this
256 container with the updated type definitions, ready for serialization. In
257 order to make this code and the rest of libctf as simple as possible, we
258 perform updates by taking the dynamic type definitions and creating an
259 in-memory CTF file containing the definitions, and then call
260 ctf_simple_open_internal() on it. We perform one extra trick here for the
261 benefit of callers and to keep our code simple: ctf_simple_open_internal()
262 will return a new ctf_file_t, but we want to keep the fp constant for the
263 caller, so after ctf_simple_open_internal() returns, we use memcpy to swap
264 the interior of the old and new ctf_file_t's, and then free the old. */
265 int
266 ctf_serialize (ctf_file_t *fp)
267 {
268 ctf_file_t ofp, *nfp;
269 ctf_header_t hdr, *hdrp;
270 ctf_dtdef_t *dtd;
271 ctf_dvdef_t *dvd;
272 ctf_varent_t *dvarents;
273 ctf_strs_writable_t strtab;
274
275 unsigned char *t;
276 unsigned long i;
277 size_t buf_size, type_size, nvars;
278 unsigned char *buf, *newbuf;
279 int err;
280
281 if (!(fp->ctf_flags & LCTF_RDWR))
282 return (ctf_set_errno (fp, ECTF_RDONLY));
283
284 /* Update required? */
285 if (!(fp->ctf_flags & LCTF_DIRTY))
286 return 0;
287
288 /* Fill in an initial CTF header. We will leave the label, object,
289 and function sections empty and only output a header, type section,
290 and string table. The type section begins at a 4-byte aligned
291 boundary past the CTF header itself (at relative offset zero). */
292
293 memset (&hdr, 0, sizeof (hdr));
294 hdr.cth_magic = CTF_MAGIC;
295 hdr.cth_version = CTF_VERSION;
296
297 /* Iterate through the dynamic type definition list and compute the
298 size of the CTF type section we will need to generate. */
299
300 for (type_size = 0, dtd = ctf_list_next (&fp->ctf_dtdefs);
301 dtd != NULL; dtd = ctf_list_next (dtd))
302 {
303 uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
304 uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
305
306 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
307 type_size += sizeof (ctf_stype_t);
308 else
309 type_size += sizeof (ctf_type_t);
310
311 switch (kind)
312 {
313 case CTF_K_INTEGER:
314 case CTF_K_FLOAT:
315 type_size += sizeof (uint32_t);
316 break;
317 case CTF_K_ARRAY:
318 type_size += sizeof (ctf_array_t);
319 break;
320 case CTF_K_SLICE:
321 type_size += sizeof (ctf_slice_t);
322 break;
323 case CTF_K_FUNCTION:
324 type_size += sizeof (uint32_t) * (vlen + (vlen & 1));
325 break;
326 case CTF_K_STRUCT:
327 case CTF_K_UNION:
328 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
329 type_size += sizeof (ctf_member_t) * vlen;
330 else
331 type_size += sizeof (ctf_lmember_t) * vlen;
332 break;
333 case CTF_K_ENUM:
334 type_size += sizeof (ctf_enum_t) * vlen;
335 break;
336 }
337 }
338
339 /* Computing the number of entries in the CTF variable section is much
340 simpler. */
341
342 for (nvars = 0, dvd = ctf_list_next (&fp->ctf_dvdefs);
343 dvd != NULL; dvd = ctf_list_next (dvd), nvars++);
344
345 /* Compute the size of the CTF buffer we need, sans only the string table,
346 then allocate a new buffer and memcpy the finished header to the start of
347 the buffer. (We will adjust this later with strtab length info.) */
348
349 hdr.cth_typeoff = hdr.cth_varoff + (nvars * sizeof (ctf_varent_t));
350 hdr.cth_stroff = hdr.cth_typeoff + type_size;
351 hdr.cth_strlen = 0;
352
353 buf_size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
354
355 if ((buf = malloc (buf_size)) == NULL)
356 return (ctf_set_errno (fp, EAGAIN));
357
358 memcpy (buf, &hdr, sizeof (ctf_header_t));
359 t = (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_varoff;
360
361 hdrp = (ctf_header_t *) buf;
362 if ((fp->ctf_flags & LCTF_CHILD) && (fp->ctf_parname != NULL))
363 ctf_str_add_ref (fp, fp->ctf_parname, &hdrp->cth_parname);
364 if (fp->ctf_cuname != NULL)
365 ctf_str_add_ref (fp, fp->ctf_cuname, &hdrp->cth_cuname);
366
367 /* Work over the variable list, translating everything into ctf_varent_t's and
368 prepping the string table. */
369
370 dvarents = (ctf_varent_t *) t;
371 for (i = 0, dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL;
372 dvd = ctf_list_next (dvd), i++)
373 {
374 ctf_varent_t *var = &dvarents[i];
375
376 ctf_str_add_ref (fp, dvd->dvd_name, &var->ctv_name);
377 var->ctv_type = (uint32_t) dvd->dvd_type;
378 }
379 assert (i == nvars);
380
381 t += sizeof (ctf_varent_t) * nvars;
382
383 assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_typeoff);
384
385 /* We now take a final lap through the dynamic type definition list and copy
386 the appropriate type records to the output buffer, noting down the
387 strings as we go. */
388
389 for (dtd = ctf_list_next (&fp->ctf_dtdefs);
390 dtd != NULL; dtd = ctf_list_next (dtd))
391 {
392 uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
393 uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
394
395 ctf_array_t cta;
396 uint32_t encoding;
397 size_t len;
398 ctf_stype_t *copied;
399 const char *name;
400
401 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
402 len = sizeof (ctf_stype_t);
403 else
404 len = sizeof (ctf_type_t);
405
406 memcpy (t, &dtd->dtd_data, len);
407 copied = (ctf_stype_t *) t; /* name is at the start: constant offset. */
408 if (copied->ctt_name
409 && (name = ctf_strraw (fp, copied->ctt_name)) != NULL)
410 ctf_str_add_ref (fp, name, &copied->ctt_name);
411 t += len;
412
413 switch (kind)
414 {
415 case CTF_K_INTEGER:
416 case CTF_K_FLOAT:
417 if (kind == CTF_K_INTEGER)
418 {
419 encoding = CTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format,
420 dtd->dtd_u.dtu_enc.cte_offset,
421 dtd->dtd_u.dtu_enc.cte_bits);
422 }
423 else
424 {
425 encoding = CTF_FP_DATA (dtd->dtd_u.dtu_enc.cte_format,
426 dtd->dtd_u.dtu_enc.cte_offset,
427 dtd->dtd_u.dtu_enc.cte_bits);
428 }
429 memcpy (t, &encoding, sizeof (encoding));
430 t += sizeof (encoding);
431 break;
432
433 case CTF_K_SLICE:
434 memcpy (t, &dtd->dtd_u.dtu_slice, sizeof (struct ctf_slice));
435 t += sizeof (struct ctf_slice);
436 break;
437
438 case CTF_K_ARRAY:
439 cta.cta_contents = (uint32_t) dtd->dtd_u.dtu_arr.ctr_contents;
440 cta.cta_index = (uint32_t) dtd->dtd_u.dtu_arr.ctr_index;
441 cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
442 memcpy (t, &cta, sizeof (cta));
443 t += sizeof (cta);
444 break;
445
446 case CTF_K_FUNCTION:
447 {
448 uint32_t *argv = (uint32_t *) (uintptr_t) t;
449 uint32_t argc;
450
451 for (argc = 0; argc < vlen; argc++)
452 *argv++ = dtd->dtd_u.dtu_argv[argc];
453
454 if (vlen & 1)
455 *argv++ = 0; /* Pad to 4-byte boundary. */
456
457 t = (unsigned char *) argv;
458 break;
459 }
460
461 case CTF_K_STRUCT:
462 case CTF_K_UNION:
463 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
464 t = ctf_copy_smembers (fp, dtd, t);
465 else
466 t = ctf_copy_lmembers (fp, dtd, t);
467 break;
468
469 case CTF_K_ENUM:
470 t = ctf_copy_emembers (fp, dtd, t);
471 break;
472 }
473 }
474 assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_stroff);
475
476 /* Construct the final string table and fill out all the string refs with the
477 final offsets. Then purge the refs list, because we're about to move this
478 strtab onto the end of the buf, invalidating all the offsets. */
479 strtab = ctf_str_write_strtab (fp);
480 ctf_str_purge_refs (fp);
481
482 if (strtab.cts_strs == NULL)
483 {
484 free (buf);
485 return (ctf_set_errno (fp, EAGAIN));
486 }
487
488 /* Now the string table is constructed, we can sort the buffer of
489 ctf_varent_t's. */
490 ctf_sort_var_arg_cb_t sort_var_arg = { fp, (ctf_strs_t *) &strtab };
491 ctf_qsort_r (dvarents, nvars, sizeof (ctf_varent_t), ctf_sort_var,
492 &sort_var_arg);
493
494 if ((newbuf = ctf_realloc (fp, buf, buf_size + strtab.cts_len)) == NULL)
495 {
496 free (buf);
497 free (strtab.cts_strs);
498 return (ctf_set_errno (fp, EAGAIN));
499 }
500 buf = newbuf;
501 memcpy (buf + buf_size, strtab.cts_strs, strtab.cts_len);
502 hdrp = (ctf_header_t *) buf;
503 hdrp->cth_strlen = strtab.cts_len;
504 buf_size += hdrp->cth_strlen;
505 free (strtab.cts_strs);
506
507 /* Finally, we are ready to ctf_simple_open() the new container. If this
508 is successful, we then switch nfp and fp and free the old container. */
509
510 if ((nfp = ctf_simple_open_internal ((char *) buf, buf_size, NULL, 0,
511 0, NULL, 0, fp->ctf_syn_ext_strtab,
512 1, &err)) == NULL)
513 {
514 free (buf);
515 return (ctf_set_errno (fp, err));
516 }
517
518 (void) ctf_setmodel (nfp, ctf_getmodel (fp));
519
520 nfp->ctf_parent = fp->ctf_parent;
521 nfp->ctf_parent_unreffed = fp->ctf_parent_unreffed;
522 nfp->ctf_refcnt = fp->ctf_refcnt;
523 nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
524 if (nfp->ctf_dynbase == NULL)
525 nfp->ctf_dynbase = buf; /* Make sure buf is freed on close. */
526 nfp->ctf_dthash = fp->ctf_dthash;
527 nfp->ctf_dtdefs = fp->ctf_dtdefs;
528 nfp->ctf_dvhash = fp->ctf_dvhash;
529 nfp->ctf_dvdefs = fp->ctf_dvdefs;
530 nfp->ctf_dtoldid = fp->ctf_dtoldid;
531 nfp->ctf_add_processing = fp->ctf_add_processing;
532 nfp->ctf_snapshots = fp->ctf_snapshots + 1;
533 nfp->ctf_specific = fp->ctf_specific;
534 nfp->ctf_ptrtab = fp->ctf_ptrtab;
535 nfp->ctf_ptrtab_len = fp->ctf_ptrtab_len;
536 nfp->ctf_link_inputs = fp->ctf_link_inputs;
537 nfp->ctf_link_outputs = fp->ctf_link_outputs;
538 nfp->ctf_errs_warnings = fp->ctf_errs_warnings;
539 nfp->ctf_str_prov_offset = fp->ctf_str_prov_offset;
540 nfp->ctf_syn_ext_strtab = fp->ctf_syn_ext_strtab;
541 nfp->ctf_link_cu_mapping = fp->ctf_link_cu_mapping;
542 nfp->ctf_link_type_mapping = fp->ctf_link_type_mapping;
543 nfp->ctf_link_memb_name_changer = fp->ctf_link_memb_name_changer;
544 nfp->ctf_link_memb_name_changer_arg = fp->ctf_link_memb_name_changer_arg;
545
546 nfp->ctf_snapshot_lu = fp->ctf_snapshots;
547
548 memcpy (&nfp->ctf_lookups, fp->ctf_lookups, sizeof (fp->ctf_lookups));
549 nfp->ctf_structs = fp->ctf_structs;
550 nfp->ctf_unions = fp->ctf_unions;
551 nfp->ctf_enums = fp->ctf_enums;
552 nfp->ctf_names = fp->ctf_names;
553
554 fp->ctf_dthash = NULL;
555 ctf_str_free_atoms (nfp);
556 nfp->ctf_str_atoms = fp->ctf_str_atoms;
557 nfp->ctf_prov_strtab = fp->ctf_prov_strtab;
558 fp->ctf_str_atoms = NULL;
559 fp->ctf_prov_strtab = NULL;
560 memset (&fp->ctf_dtdefs, 0, sizeof (ctf_list_t));
561 memset (&fp->ctf_errs_warnings, 0, sizeof (ctf_list_t));
562 fp->ctf_add_processing = NULL;
563 fp->ctf_ptrtab = NULL;
564 fp->ctf_link_inputs = NULL;
565 fp->ctf_link_outputs = NULL;
566 fp->ctf_syn_ext_strtab = NULL;
567 fp->ctf_link_cu_mapping = NULL;
568 fp->ctf_link_type_mapping = NULL;
569 fp->ctf_parent_unreffed = 1;
570
571 fp->ctf_dvhash = NULL;
572 memset (&fp->ctf_dvdefs, 0, sizeof (ctf_list_t));
573 memset (fp->ctf_lookups, 0, sizeof (fp->ctf_lookups));
574 fp->ctf_structs.ctn_writable = NULL;
575 fp->ctf_unions.ctn_writable = NULL;
576 fp->ctf_enums.ctn_writable = NULL;
577 fp->ctf_names.ctn_writable = NULL;
578
579 memcpy (&ofp, fp, sizeof (ctf_file_t));
580 memcpy (fp, nfp, sizeof (ctf_file_t));
581 memcpy (nfp, &ofp, sizeof (ctf_file_t));
582
583 nfp->ctf_refcnt = 1; /* Force nfp to be freed. */
584 ctf_file_close (nfp);
585
586 return 0;
587 }
588
589 ctf_names_t *
590 ctf_name_table (ctf_file_t *fp, int kind)
591 {
592 switch (kind)
593 {
594 case CTF_K_STRUCT:
595 return &fp->ctf_structs;
596 case CTF_K_UNION:
597 return &fp->ctf_unions;
598 case CTF_K_ENUM:
599 return &fp->ctf_enums;
600 default:
601 return &fp->ctf_names;
602 }
603 }
604
605 int
606 ctf_dtd_insert (ctf_file_t *fp, ctf_dtdef_t *dtd, int flag, int kind)
607 {
608 const char *name;
609 if (ctf_dynhash_insert (fp->ctf_dthash, (void *) dtd->dtd_type, dtd) < 0)
610 return -1;
611
612 if (flag == CTF_ADD_ROOT && dtd->dtd_data.ctt_name
613 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL)
614 {
615 if (ctf_dynhash_insert (ctf_name_table (fp, kind)->ctn_writable,
616 (char *) name, (void *) dtd->dtd_type) < 0)
617 {
618 ctf_dynhash_remove (fp->ctf_dthash, (void *) dtd->dtd_type);
619 return -1;
620 }
621 }
622 ctf_list_append (&fp->ctf_dtdefs, dtd);
623 return 0;
624 }
625
626 void
627 ctf_dtd_delete (ctf_file_t *fp, ctf_dtdef_t *dtd)
628 {
629 ctf_dmdef_t *dmd, *nmd;
630 int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
631 int name_kind = kind;
632 const char *name;
633
634 ctf_dynhash_remove (fp->ctf_dthash, (void *) dtd->dtd_type);
635
636 switch (kind)
637 {
638 case CTF_K_STRUCT:
639 case CTF_K_UNION:
640 case CTF_K_ENUM:
641 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
642 dmd != NULL; dmd = nmd)
643 {
644 if (dmd->dmd_name != NULL)
645 free (dmd->dmd_name);
646 nmd = ctf_list_next (dmd);
647 free (dmd);
648 }
649 break;
650 case CTF_K_FUNCTION:
651 free (dtd->dtd_u.dtu_argv);
652 break;
653 case CTF_K_FORWARD:
654 name_kind = dtd->dtd_data.ctt_type;
655 break;
656 }
657
658 if (dtd->dtd_data.ctt_name
659 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL
660 && LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info))
661 {
662 ctf_dynhash_remove (ctf_name_table (fp, name_kind)->ctn_writable,
663 name);
664 ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
665 }
666
667 ctf_list_delete (&fp->ctf_dtdefs, dtd);
668 free (dtd);
669 }
670
671 ctf_dtdef_t *
672 ctf_dtd_lookup (const ctf_file_t *fp, ctf_id_t type)
673 {
674 return (ctf_dtdef_t *) ctf_dynhash_lookup (fp->ctf_dthash, (void *) type);
675 }
676
677 ctf_dtdef_t *
678 ctf_dynamic_type (const ctf_file_t *fp, ctf_id_t id)
679 {
680 ctf_id_t idx;
681
682 if (!(fp->ctf_flags & LCTF_RDWR))
683 return NULL;
684
685 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, id))
686 fp = fp->ctf_parent;
687
688 idx = LCTF_TYPE_TO_INDEX(fp, id);
689
690 if ((unsigned long) idx <= fp->ctf_typemax)
691 return ctf_dtd_lookup (fp, id);
692 return NULL;
693 }
694
695 int
696 ctf_dvd_insert (ctf_file_t *fp, ctf_dvdef_t *dvd)
697 {
698 if (ctf_dynhash_insert (fp->ctf_dvhash, dvd->dvd_name, dvd) < 0)
699 return -1;
700 ctf_list_append (&fp->ctf_dvdefs, dvd);
701 return 0;
702 }
703
704 void
705 ctf_dvd_delete (ctf_file_t *fp, ctf_dvdef_t *dvd)
706 {
707 ctf_dynhash_remove (fp->ctf_dvhash, dvd->dvd_name);
708 free (dvd->dvd_name);
709
710 ctf_list_delete (&fp->ctf_dvdefs, dvd);
711 free (dvd);
712 }
713
714 ctf_dvdef_t *
715 ctf_dvd_lookup (const ctf_file_t *fp, const char *name)
716 {
717 return (ctf_dvdef_t *) ctf_dynhash_lookup (fp->ctf_dvhash, name);
718 }
719
720 /* Discard all of the dynamic type definitions and variable definitions that
721 have been added to the container since the last call to ctf_update(). We
722 locate such types by scanning the dtd list and deleting elements that have
723 type IDs greater than ctf_dtoldid, which is set by ctf_update(), above, and
724 by scanning the variable list and deleting elements that have update IDs
725 equal to the current value of the last-update snapshot count (indicating that
726 they were added after the most recent call to ctf_update()). */
727 int
728 ctf_discard (ctf_file_t *fp)
729 {
730 ctf_snapshot_id_t last_update =
731 { fp->ctf_dtoldid,
732 fp->ctf_snapshot_lu + 1 };
733
734 /* Update required? */
735 if (!(fp->ctf_flags & LCTF_DIRTY))
736 return 0;
737
738 return (ctf_rollback (fp, last_update));
739 }
740
741 ctf_snapshot_id_t
742 ctf_snapshot (ctf_file_t *fp)
743 {
744 ctf_snapshot_id_t snapid;
745 snapid.dtd_id = fp->ctf_typemax;
746 snapid.snapshot_id = fp->ctf_snapshots++;
747 return snapid;
748 }
749
750 /* Like ctf_discard(), only discards everything after a particular ID. */
751 int
752 ctf_rollback (ctf_file_t *fp, ctf_snapshot_id_t id)
753 {
754 ctf_dtdef_t *dtd, *ntd;
755 ctf_dvdef_t *dvd, *nvd;
756
757 if (!(fp->ctf_flags & LCTF_RDWR))
758 return (ctf_set_errno (fp, ECTF_RDONLY));
759
760 if (fp->ctf_snapshot_lu >= id.snapshot_id)
761 return (ctf_set_errno (fp, ECTF_OVERROLLBACK));
762
763 for (dtd = ctf_list_next (&fp->ctf_dtdefs); dtd != NULL; dtd = ntd)
764 {
765 int kind;
766 const char *name;
767
768 ntd = ctf_list_next (dtd);
769
770 if (LCTF_TYPE_TO_INDEX (fp, dtd->dtd_type) <= id.dtd_id)
771 continue;
772
773 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
774 if (kind == CTF_K_FORWARD)
775 kind = dtd->dtd_data.ctt_type;
776
777 if (dtd->dtd_data.ctt_name
778 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL
779 && LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info))
780 {
781 ctf_dynhash_remove (ctf_name_table (fp, kind)->ctn_writable,
782 name);
783 ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
784 }
785
786 ctf_dynhash_remove (fp->ctf_dthash, (void *) dtd->dtd_type);
787 ctf_dtd_delete (fp, dtd);
788 }
789
790 for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd)
791 {
792 nvd = ctf_list_next (dvd);
793
794 if (dvd->dvd_snapshots <= id.snapshot_id)
795 continue;
796
797 ctf_dvd_delete (fp, dvd);
798 }
799
800 fp->ctf_typemax = id.dtd_id;
801 fp->ctf_snapshots = id.snapshot_id;
802
803 if (fp->ctf_snapshots == fp->ctf_snapshot_lu)
804 fp->ctf_flags &= ~LCTF_DIRTY;
805
806 return 0;
807 }
808
809 static ctf_id_t
810 ctf_add_generic (ctf_file_t *fp, uint32_t flag, const char *name, int kind,
811 ctf_dtdef_t **rp)
812 {
813 ctf_dtdef_t *dtd;
814 ctf_id_t type;
815
816 if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
817 return (ctf_set_errno (fp, EINVAL));
818
819 if (!(fp->ctf_flags & LCTF_RDWR))
820 return (ctf_set_errno (fp, ECTF_RDONLY));
821
822 if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) >= CTF_MAX_TYPE)
823 return (ctf_set_errno (fp, ECTF_FULL));
824
825 if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) == (CTF_MAX_PTYPE - 1))
826 return (ctf_set_errno (fp, ECTF_FULL));
827
828 /* Make sure ptrtab always grows to be big enough for all types. */
829 if (ctf_grow_ptrtab (fp) < 0)
830 return CTF_ERR; /* errno is set for us. */
831
832 if ((dtd = malloc (sizeof (ctf_dtdef_t))) == NULL)
833 return (ctf_set_errno (fp, EAGAIN));
834
835 type = ++fp->ctf_typemax;
836 type = LCTF_INDEX_TO_TYPE (fp, type, (fp->ctf_flags & LCTF_CHILD));
837
838 memset (dtd, 0, sizeof (ctf_dtdef_t));
839 dtd->dtd_data.ctt_name = ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name);
840 dtd->dtd_type = type;
841
842 if (dtd->dtd_data.ctt_name == 0 && name != NULL && name[0] != '\0')
843 {
844 free (dtd);
845 return (ctf_set_errno (fp, EAGAIN));
846 }
847
848 if (ctf_dtd_insert (fp, dtd, flag, kind) < 0)
849 {
850 free (dtd);
851 return CTF_ERR; /* errno is set for us. */
852 }
853 fp->ctf_flags |= LCTF_DIRTY;
854
855 *rp = dtd;
856 return type;
857 }
858
859 /* When encoding integer sizes, we want to convert a byte count in the range
860 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
861 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
862 static size_t
863 clp2 (size_t x)
864 {
865 x--;
866
867 x |= (x >> 1);
868 x |= (x >> 2);
869 x |= (x >> 4);
870 x |= (x >> 8);
871 x |= (x >> 16);
872
873 return (x + 1);
874 }
875
876 static ctf_id_t
877 ctf_add_encoded (ctf_file_t *fp, uint32_t flag,
878 const char *name, const ctf_encoding_t *ep, uint32_t kind)
879 {
880 ctf_dtdef_t *dtd;
881 ctf_id_t type;
882
883 if (ep == NULL)
884 return (ctf_set_errno (fp, EINVAL));
885
886 if ((type = ctf_add_generic (fp, flag, name, kind, &dtd)) == CTF_ERR)
887 return CTF_ERR; /* errno is set for us. */
888
889 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
890 dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
891 / CHAR_BIT);
892 dtd->dtd_u.dtu_enc = *ep;
893
894 return type;
895 }
896
897 static ctf_id_t
898 ctf_add_reftype (ctf_file_t *fp, uint32_t flag, ctf_id_t ref, uint32_t kind)
899 {
900 ctf_dtdef_t *dtd;
901 ctf_id_t type;
902 ctf_file_t *tmp = fp;
903 int child = fp->ctf_flags & LCTF_CHILD;
904
905 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
906 return (ctf_set_errno (fp, EINVAL));
907
908 if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL)
909 return CTF_ERR; /* errno is set for us. */
910
911 if ((type = ctf_add_generic (fp, flag, NULL, kind, &dtd)) == CTF_ERR)
912 return CTF_ERR; /* errno is set for us. */
913
914 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
915 dtd->dtd_data.ctt_type = (uint32_t) ref;
916
917 if (kind != CTF_K_POINTER)
918 return type;
919
920 /* If we are adding a pointer, update the ptrtab, both the directly pointed-to
921 type and (if an anonymous typedef node is being pointed at) the type that
922 points at too. Note that ctf_typemax is at this point one higher than we
923 want to check against, because it's just been incremented for the addition
924 of this type. */
925
926 uint32_t type_idx = LCTF_TYPE_TO_INDEX (fp, type);
927 uint32_t ref_idx = LCTF_TYPE_TO_INDEX (fp, ref);
928
929 if (LCTF_TYPE_ISCHILD (fp, ref) == child
930 && ref_idx < fp->ctf_typemax)
931 {
932 fp->ctf_ptrtab[ref_idx] = type_idx;
933
934 ctf_id_t refref_idx = LCTF_TYPE_TO_INDEX (fp, dtd->dtd_data.ctt_type);
935
936 if (tmp == fp
937 && (LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) == CTF_K_TYPEDEF)
938 && strcmp (ctf_strptr (fp, dtd->dtd_data.ctt_name), "") == 0
939 && refref_idx < fp->ctf_typemax)
940 fp->ctf_ptrtab[refref_idx] = type_idx;
941 }
942
943 return type;
944 }
945
946 ctf_id_t
947 ctf_add_slice (ctf_file_t *fp, uint32_t flag, ctf_id_t ref,
948 const ctf_encoding_t *ep)
949 {
950 ctf_dtdef_t *dtd;
951 ctf_id_t resolved_ref = ref;
952 ctf_id_t type;
953 int kind;
954 const ctf_type_t *tp;
955 ctf_file_t *tmp = fp;
956
957 if (ep == NULL)
958 return (ctf_set_errno (fp, EINVAL));
959
960 if ((ep->cte_bits > 255) || (ep->cte_offset > 255))
961 return (ctf_set_errno (fp, ECTF_SLICEOVERFLOW));
962
963 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
964 return (ctf_set_errno (fp, EINVAL));
965
966 if (ref != 0 && ((tp = ctf_lookup_by_id (&tmp, ref)) == NULL))
967 return CTF_ERR; /* errno is set for us. */
968
969 /* Make sure we ultimately point to an integral type. We also allow slices to
970 point to the unimplemented type, for now, because the compiler can emit
971 such slices, though they're not very much use. */
972
973 resolved_ref = ctf_type_resolve_unsliced (tmp, ref);
974 kind = ctf_type_kind_unsliced (tmp, resolved_ref);
975
976 if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) &&
977 (kind != CTF_K_ENUM)
978 && (ref != 0))
979 return (ctf_set_errno (fp, ECTF_NOTINTFP));
980
981 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_SLICE, &dtd)) == CTF_ERR)
982 return CTF_ERR; /* errno is set for us. */
983
984 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0);
985 dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
986 / CHAR_BIT);
987 dtd->dtd_u.dtu_slice.cts_type = (uint32_t) ref;
988 dtd->dtd_u.dtu_slice.cts_bits = ep->cte_bits;
989 dtd->dtd_u.dtu_slice.cts_offset = ep->cte_offset;
990
991 return type;
992 }
993
994 ctf_id_t
995 ctf_add_integer (ctf_file_t *fp, uint32_t flag,
996 const char *name, const ctf_encoding_t *ep)
997 {
998 return (ctf_add_encoded (fp, flag, name, ep, CTF_K_INTEGER));
999 }
1000
1001 ctf_id_t
1002 ctf_add_float (ctf_file_t *fp, uint32_t flag,
1003 const char *name, const ctf_encoding_t *ep)
1004 {
1005 return (ctf_add_encoded (fp, flag, name, ep, CTF_K_FLOAT));
1006 }
1007
1008 ctf_id_t
1009 ctf_add_pointer (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1010 {
1011 return (ctf_add_reftype (fp, flag, ref, CTF_K_POINTER));
1012 }
1013
1014 ctf_id_t
1015 ctf_add_array (ctf_file_t *fp, uint32_t flag, const ctf_arinfo_t *arp)
1016 {
1017 ctf_dtdef_t *dtd;
1018 ctf_id_t type;
1019 ctf_file_t *tmp = fp;
1020
1021 if (arp == NULL)
1022 return (ctf_set_errno (fp, EINVAL));
1023
1024 if (arp->ctr_contents != 0
1025 && ctf_lookup_by_id (&tmp, arp->ctr_contents) == NULL)
1026 return CTF_ERR; /* errno is set for us. */
1027
1028 tmp = fp;
1029 if (ctf_lookup_by_id (&tmp, arp->ctr_index) == NULL)
1030 return CTF_ERR; /* errno is set for us. */
1031
1032 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_ARRAY, &dtd)) == CTF_ERR)
1033 return CTF_ERR; /* errno is set for us. */
1034
1035 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ARRAY, flag, 0);
1036 dtd->dtd_data.ctt_size = 0;
1037 dtd->dtd_u.dtu_arr = *arp;
1038
1039 return type;
1040 }
1041
1042 int
1043 ctf_set_array (ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
1044 {
1045 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type);
1046
1047 if (!(fp->ctf_flags & LCTF_RDWR))
1048 return (ctf_set_errno (fp, ECTF_RDONLY));
1049
1050 if (dtd == NULL
1051 || LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
1052 return (ctf_set_errno (fp, ECTF_BADID));
1053
1054 fp->ctf_flags |= LCTF_DIRTY;
1055 dtd->dtd_u.dtu_arr = *arp;
1056
1057 return 0;
1058 }
1059
1060 ctf_id_t
1061 ctf_add_function (ctf_file_t *fp, uint32_t flag,
1062 const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
1063 {
1064 ctf_dtdef_t *dtd;
1065 ctf_id_t type;
1066 uint32_t vlen;
1067 uint32_t *vdat = NULL;
1068 ctf_file_t *tmp = fp;
1069 size_t i;
1070
1071 if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0
1072 || (ctc->ctc_argc != 0 && argv == NULL))
1073 return (ctf_set_errno (fp, EINVAL));
1074
1075 vlen = ctc->ctc_argc;
1076 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1077 vlen++; /* Add trailing zero to indicate varargs (see below). */
1078
1079 if (ctc->ctc_return != 0
1080 && ctf_lookup_by_id (&tmp, ctc->ctc_return) == NULL)
1081 return CTF_ERR; /* errno is set for us. */
1082
1083 if (vlen > CTF_MAX_VLEN)
1084 return (ctf_set_errno (fp, EOVERFLOW));
1085
1086 if (vlen != 0 && (vdat = malloc (sizeof (ctf_id_t) * vlen)) == NULL)
1087 return (ctf_set_errno (fp, EAGAIN));
1088
1089 for (i = 0; i < ctc->ctc_argc; i++)
1090 {
1091 tmp = fp;
1092 if (argv[i] != 0 && ctf_lookup_by_id (&tmp, argv[i]) == NULL)
1093 {
1094 free (vdat);
1095 return CTF_ERR; /* errno is set for us. */
1096 }
1097 vdat[i] = (uint32_t) argv[i];
1098 }
1099
1100 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_FUNCTION,
1101 &dtd)) == CTF_ERR)
1102 {
1103 free (vdat);
1104 return CTF_ERR; /* errno is set for us. */
1105 }
1106
1107 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FUNCTION, flag, vlen);
1108 dtd->dtd_data.ctt_type = (uint32_t) ctc->ctc_return;
1109
1110 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1111 vdat[vlen - 1] = 0; /* Add trailing zero to indicate varargs. */
1112 dtd->dtd_u.dtu_argv = vdat;
1113
1114 return type;
1115 }
1116
1117 ctf_id_t
1118 ctf_add_struct_sized (ctf_file_t *fp, uint32_t flag, const char *name,
1119 size_t size)
1120 {
1121 ctf_dtdef_t *dtd;
1122 ctf_id_t type = 0;
1123
1124 /* Promote root-visible forwards to structs. */
1125 if (name != NULL)
1126 type = ctf_lookup_by_rawname (fp, CTF_K_STRUCT, name);
1127
1128 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1129 dtd = ctf_dtd_lookup (fp, type);
1130 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_STRUCT,
1131 &dtd)) == CTF_ERR)
1132 return CTF_ERR; /* errno is set for us. */
1133
1134 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_STRUCT, flag, 0);
1135
1136 if (size > CTF_MAX_SIZE)
1137 {
1138 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1139 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
1140 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
1141 }
1142 else
1143 dtd->dtd_data.ctt_size = (uint32_t) size;
1144
1145 return type;
1146 }
1147
1148 ctf_id_t
1149 ctf_add_struct (ctf_file_t *fp, uint32_t flag, const char *name)
1150 {
1151 return (ctf_add_struct_sized (fp, flag, name, 0));
1152 }
1153
1154 ctf_id_t
1155 ctf_add_union_sized (ctf_file_t *fp, uint32_t flag, const char *name,
1156 size_t size)
1157 {
1158 ctf_dtdef_t *dtd;
1159 ctf_id_t type = 0;
1160
1161 /* Promote root-visible forwards to unions. */
1162 if (name != NULL)
1163 type = ctf_lookup_by_rawname (fp, CTF_K_UNION, name);
1164
1165 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1166 dtd = ctf_dtd_lookup (fp, type);
1167 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_UNION,
1168 &dtd)) == CTF_ERR)
1169 return CTF_ERR; /* errno is set for us */
1170
1171 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_UNION, flag, 0);
1172
1173 if (size > CTF_MAX_SIZE)
1174 {
1175 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1176 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
1177 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
1178 }
1179 else
1180 dtd->dtd_data.ctt_size = (uint32_t) size;
1181
1182 return type;
1183 }
1184
1185 ctf_id_t
1186 ctf_add_union (ctf_file_t *fp, uint32_t flag, const char *name)
1187 {
1188 return (ctf_add_union_sized (fp, flag, name, 0));
1189 }
1190
1191 ctf_id_t
1192 ctf_add_enum (ctf_file_t *fp, uint32_t flag, const char *name)
1193 {
1194 ctf_dtdef_t *dtd;
1195 ctf_id_t type = 0;
1196
1197 /* Promote root-visible forwards to enums. */
1198 if (name != NULL)
1199 type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
1200
1201 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1202 dtd = ctf_dtd_lookup (fp, type);
1203 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_ENUM,
1204 &dtd)) == CTF_ERR)
1205 return CTF_ERR; /* errno is set for us. */
1206
1207 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ENUM, flag, 0);
1208 dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
1209
1210 return type;
1211 }
1212
1213 ctf_id_t
1214 ctf_add_enum_encoded (ctf_file_t *fp, uint32_t flag, const char *name,
1215 const ctf_encoding_t *ep)
1216 {
1217 ctf_id_t type = 0;
1218
1219 /* First, create the enum if need be, using most of the same machinery as
1220 ctf_add_enum(), to ensure that we do not allow things past that are not
1221 enums or forwards to them. (This includes other slices: you cannot slice a
1222 slice, which would be a useless thing to do anyway.) */
1223
1224 if (name != NULL)
1225 type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
1226
1227 if (type != 0)
1228 {
1229 if ((ctf_type_kind (fp, type) != CTF_K_FORWARD) &&
1230 (ctf_type_kind_unsliced (fp, type) != CTF_K_ENUM))
1231 return (ctf_set_errno (fp, ECTF_NOTINTFP));
1232 }
1233 else if ((type = ctf_add_enum (fp, flag, name)) == CTF_ERR)
1234 return CTF_ERR; /* errno is set for us. */
1235
1236 /* Now attach a suitable slice to it. */
1237
1238 return ctf_add_slice (fp, flag, type, ep);
1239 }
1240
1241 ctf_id_t
1242 ctf_add_forward (ctf_file_t *fp, uint32_t flag, const char *name,
1243 uint32_t kind)
1244 {
1245 ctf_dtdef_t *dtd;
1246 ctf_id_t type = 0;
1247
1248 if (!ctf_forwardable_kind (kind))
1249 return (ctf_set_errno (fp, ECTF_NOTSUE));
1250
1251 /* If the type is already defined or exists as a forward tag, just
1252 return the ctf_id_t of the existing definition. */
1253
1254 if (name != NULL)
1255 type = ctf_lookup_by_rawname (fp, kind, name);
1256
1257 if (type)
1258 return type;
1259
1260 if ((type = ctf_add_generic (fp, flag, name, kind, &dtd)) == CTF_ERR)
1261 return CTF_ERR; /* errno is set for us. */
1262
1263 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FORWARD, flag, 0);
1264 dtd->dtd_data.ctt_type = kind;
1265
1266 return type;
1267 }
1268
1269 ctf_id_t
1270 ctf_add_typedef (ctf_file_t *fp, uint32_t flag, const char *name,
1271 ctf_id_t ref)
1272 {
1273 ctf_dtdef_t *dtd;
1274 ctf_id_t type;
1275 ctf_file_t *tmp = fp;
1276
1277 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
1278 return (ctf_set_errno (fp, EINVAL));
1279
1280 if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL)
1281 return CTF_ERR; /* errno is set for us. */
1282
1283 if ((type = ctf_add_generic (fp, flag, name, CTF_K_TYPEDEF,
1284 &dtd)) == CTF_ERR)
1285 return CTF_ERR; /* errno is set for us. */
1286
1287 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_TYPEDEF, flag, 0);
1288 dtd->dtd_data.ctt_type = (uint32_t) ref;
1289
1290 return type;
1291 }
1292
1293 ctf_id_t
1294 ctf_add_volatile (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1295 {
1296 return (ctf_add_reftype (fp, flag, ref, CTF_K_VOLATILE));
1297 }
1298
1299 ctf_id_t
1300 ctf_add_const (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1301 {
1302 return (ctf_add_reftype (fp, flag, ref, CTF_K_CONST));
1303 }
1304
1305 ctf_id_t
1306 ctf_add_restrict (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1307 {
1308 return (ctf_add_reftype (fp, flag, ref, CTF_K_RESTRICT));
1309 }
1310
1311 int
1312 ctf_add_enumerator (ctf_file_t *fp, ctf_id_t enid, const char *name,
1313 int value)
1314 {
1315 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, enid);
1316 ctf_dmdef_t *dmd;
1317
1318 uint32_t kind, vlen, root;
1319 char *s;
1320
1321 if (name == NULL)
1322 return (ctf_set_errno (fp, EINVAL));
1323
1324 if (!(fp->ctf_flags & LCTF_RDWR))
1325 return (ctf_set_errno (fp, ECTF_RDONLY));
1326
1327 if (dtd == NULL)
1328 return (ctf_set_errno (fp, ECTF_BADID));
1329
1330 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1331 root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info);
1332 vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
1333
1334 if (kind != CTF_K_ENUM)
1335 return (ctf_set_errno (fp, ECTF_NOTENUM));
1336
1337 if (vlen == CTF_MAX_VLEN)
1338 return (ctf_set_errno (fp, ECTF_DTFULL));
1339
1340 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
1341 dmd != NULL; dmd = ctf_list_next (dmd))
1342 {
1343 if (strcmp (dmd->dmd_name, name) == 0)
1344 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1345 }
1346
1347 if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
1348 return (ctf_set_errno (fp, EAGAIN));
1349
1350 if ((s = strdup (name)) == NULL)
1351 {
1352 free (dmd);
1353 return (ctf_set_errno (fp, EAGAIN));
1354 }
1355
1356 dmd->dmd_name = s;
1357 dmd->dmd_type = CTF_ERR;
1358 dmd->dmd_offset = 0;
1359 dmd->dmd_value = value;
1360
1361 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1);
1362 ctf_list_append (&dtd->dtd_u.dtu_members, dmd);
1363
1364 fp->ctf_flags |= LCTF_DIRTY;
1365
1366 return 0;
1367 }
1368
1369 int
1370 ctf_add_member_offset (ctf_file_t *fp, ctf_id_t souid, const char *name,
1371 ctf_id_t type, unsigned long bit_offset)
1372 {
1373 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, souid);
1374 ctf_dmdef_t *dmd;
1375
1376 ssize_t msize, malign, ssize;
1377 uint32_t kind, vlen, root;
1378 char *s = NULL;
1379
1380 if (!(fp->ctf_flags & LCTF_RDWR))
1381 return (ctf_set_errno (fp, ECTF_RDONLY));
1382
1383 if (dtd == NULL)
1384 return (ctf_set_errno (fp, ECTF_BADID));
1385
1386 if (name != NULL && name[0] == '\0')
1387 name = NULL;
1388
1389 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1390 root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info);
1391 vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
1392
1393 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
1394 return (ctf_set_errno (fp, ECTF_NOTSOU));
1395
1396 if (vlen == CTF_MAX_VLEN)
1397 return (ctf_set_errno (fp, ECTF_DTFULL));
1398
1399 if (name != NULL)
1400 {
1401 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
1402 dmd != NULL; dmd = ctf_list_next (dmd))
1403 {
1404 if (dmd->dmd_name != NULL && strcmp (dmd->dmd_name, name) == 0)
1405 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1406 }
1407 }
1408
1409 if ((msize = ctf_type_size (fp, type)) < 0 ||
1410 (malign = ctf_type_align (fp, type)) < 0)
1411 {
1412 /* The unimplemented type, and any type that resolves to it, has no size
1413 and no alignment: it can correspond to any number of compiler-inserted
1414 types. */
1415
1416 if (ctf_errno (fp) == ECTF_NONREPRESENTABLE)
1417 {
1418 msize = 0;
1419 malign = 0;
1420 ctf_set_errno (fp, 0);
1421 }
1422 else
1423 return -1; /* errno is set for us. */
1424 }
1425
1426 if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
1427 return (ctf_set_errno (fp, EAGAIN));
1428
1429 if (name != NULL && (s = strdup (name)) == NULL)
1430 {
1431 free (dmd);
1432 return (ctf_set_errno (fp, EAGAIN));
1433 }
1434
1435 dmd->dmd_name = s;
1436 dmd->dmd_type = type;
1437 dmd->dmd_value = -1;
1438
1439 if (kind == CTF_K_STRUCT && vlen != 0)
1440 {
1441 if (bit_offset == (unsigned long) - 1)
1442 {
1443 /* Natural alignment. */
1444
1445 ctf_dmdef_t *lmd = ctf_list_prev (&dtd->dtd_u.dtu_members);
1446 ctf_id_t ltype = ctf_type_resolve (fp, lmd->dmd_type);
1447 size_t off = lmd->dmd_offset;
1448
1449 ctf_encoding_t linfo;
1450 ssize_t lsize;
1451
1452 /* Propagate any error from ctf_type_resolve. If the last member was
1453 of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we
1454 cannot insert right after such a member without explicit offset
1455 specification, because its alignment and size is not known. */
1456 if (ltype == CTF_ERR)
1457 {
1458 free (dmd);
1459 return -1; /* errno is set for us. */
1460 }
1461
1462 if (ctf_type_encoding (fp, ltype, &linfo) == 0)
1463 off += linfo.cte_bits;
1464 else if ((lsize = ctf_type_size (fp, ltype)) > 0)
1465 off += lsize * CHAR_BIT;
1466
1467 /* Round up the offset of the end of the last member to
1468 the next byte boundary, convert 'off' to bytes, and
1469 then round it up again to the next multiple of the
1470 alignment required by the new member. Finally,
1471 convert back to bits and store the result in
1472 dmd_offset. Technically we could do more efficient
1473 packing if the new member is a bit-field, but we're
1474 the "compiler" and ANSI says we can do as we choose. */
1475
1476 off = roundup (off, CHAR_BIT) / CHAR_BIT;
1477 off = roundup (off, MAX (malign, 1));
1478 dmd->dmd_offset = off * CHAR_BIT;
1479 ssize = off + msize;
1480 }
1481 else
1482 {
1483 /* Specified offset in bits. */
1484
1485 dmd->dmd_offset = bit_offset;
1486 ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL);
1487 ssize = MAX (ssize, ((signed) bit_offset / CHAR_BIT) + msize);
1488 }
1489 }
1490 else
1491 {
1492 dmd->dmd_offset = 0;
1493 ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL);
1494 ssize = MAX (ssize, msize);
1495 }
1496
1497 if ((size_t) ssize > CTF_MAX_SIZE)
1498 {
1499 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1500 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (ssize);
1501 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (ssize);
1502 }
1503 else
1504 dtd->dtd_data.ctt_size = (uint32_t) ssize;
1505
1506 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1);
1507 ctf_list_append (&dtd->dtd_u.dtu_members, dmd);
1508
1509 fp->ctf_flags |= LCTF_DIRTY;
1510 return 0;
1511 }
1512
1513 int
1514 ctf_add_member_encoded (ctf_file_t *fp, ctf_id_t souid, const char *name,
1515 ctf_id_t type, unsigned long bit_offset,
1516 const ctf_encoding_t encoding)
1517 {
1518 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type);
1519 int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1520 int otype = type;
1521
1522 if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) && (kind != CTF_K_ENUM))
1523 return (ctf_set_errno (fp, ECTF_NOTINTFP));
1524
1525 if ((type = ctf_add_slice (fp, CTF_ADD_NONROOT, otype, &encoding)) == CTF_ERR)
1526 return -1; /* errno is set for us. */
1527
1528 return ctf_add_member_offset (fp, souid, name, type, bit_offset);
1529 }
1530
1531 int
1532 ctf_add_member (ctf_file_t *fp, ctf_id_t souid, const char *name,
1533 ctf_id_t type)
1534 {
1535 return ctf_add_member_offset (fp, souid, name, type, (unsigned long) - 1);
1536 }
1537
1538 int
1539 ctf_add_variable (ctf_file_t *fp, const char *name, ctf_id_t ref)
1540 {
1541 ctf_dvdef_t *dvd;
1542 ctf_file_t *tmp = fp;
1543
1544 if (!(fp->ctf_flags & LCTF_RDWR))
1545 return (ctf_set_errno (fp, ECTF_RDONLY));
1546
1547 if (ctf_dvd_lookup (fp, name) != NULL)
1548 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1549
1550 if (ctf_lookup_by_id (&tmp, ref) == NULL)
1551 return -1; /* errno is set for us. */
1552
1553 /* Make sure this type is representable. */
1554 if ((ctf_type_resolve (fp, ref) == CTF_ERR)
1555 && (ctf_errno (fp) == ECTF_NONREPRESENTABLE))
1556 return -1;
1557
1558 if ((dvd = malloc (sizeof (ctf_dvdef_t))) == NULL)
1559 return (ctf_set_errno (fp, EAGAIN));
1560
1561 if (name != NULL && (dvd->dvd_name = strdup (name)) == NULL)
1562 {
1563 free (dvd);
1564 return (ctf_set_errno (fp, EAGAIN));
1565 }
1566 dvd->dvd_type = ref;
1567 dvd->dvd_snapshots = fp->ctf_snapshots;
1568
1569 if (ctf_dvd_insert (fp, dvd) < 0)
1570 {
1571 free (dvd->dvd_name);
1572 free (dvd);
1573 return -1; /* errno is set for us. */
1574 }
1575
1576 fp->ctf_flags |= LCTF_DIRTY;
1577 return 0;
1578 }
1579
1580 static int
1581 enumcmp (const char *name, int value, void *arg)
1582 {
1583 ctf_bundle_t *ctb = arg;
1584 int bvalue;
1585
1586 if (ctf_enum_value (ctb->ctb_file, ctb->ctb_type, name, &bvalue) < 0)
1587 {
1588 ctf_dprintf ("Conflict due to member %s iteration error: %s.\n", name,
1589 ctf_errmsg (ctf_errno (ctb->ctb_file)));
1590 return 1;
1591 }
1592 if (value != bvalue)
1593 {
1594 ctf_dprintf ("Conflict due to value change: %i versus %i\n",
1595 value, bvalue);
1596 return 1;
1597 }
1598 return 0;
1599 }
1600
1601 static int
1602 enumadd (const char *name, int value, void *arg)
1603 {
1604 ctf_bundle_t *ctb = arg;
1605
1606 return (ctf_add_enumerator (ctb->ctb_file, ctb->ctb_type,
1607 name, value) < 0);
1608 }
1609
1610 static int
1611 membcmp (const char *name, ctf_id_t type _libctf_unused_, unsigned long offset,
1612 void *arg)
1613 {
1614 ctf_bundle_t *ctb = arg;
1615 ctf_membinfo_t ctm;
1616
1617 /* Don't check nameless members (e.g. anonymous structs/unions) against each
1618 other. */
1619 if (name[0] == 0)
1620 return 0;
1621
1622 if (ctf_member_info (ctb->ctb_file, ctb->ctb_type, name, &ctm) < 0)
1623 {
1624 ctf_dprintf ("Conflict due to member %s iteration error: %s.\n", name,
1625 ctf_errmsg (ctf_errno (ctb->ctb_file)));
1626 return 1;
1627 }
1628 if (ctm.ctm_offset != offset)
1629 {
1630 ctf_dprintf ("Conflict due to member %s offset change: "
1631 "%lx versus %lx\n", name, ctm.ctm_offset, offset);
1632 return 1;
1633 }
1634 return 0;
1635 }
1636
1637 static int
1638 membadd (const char *name, ctf_id_t type, unsigned long offset, void *arg)
1639 {
1640 ctf_bundle_t *ctb = arg;
1641 ctf_dmdef_t *dmd;
1642 char *s = NULL;
1643
1644 if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
1645 return (ctf_set_errno (ctb->ctb_file, EAGAIN));
1646
1647 if (name != NULL && (s = strdup (name)) == NULL)
1648 {
1649 free (dmd);
1650 return (ctf_set_errno (ctb->ctb_file, EAGAIN));
1651 }
1652
1653 /* For now, dmd_type is copied as the src_fp's type; it is reset to an
1654 equivalent dst_fp type by a final loop in ctf_add_type(), below. */
1655 dmd->dmd_name = s;
1656 dmd->dmd_type = type;
1657 dmd->dmd_offset = offset;
1658 dmd->dmd_value = -1;
1659
1660 ctf_list_append (&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
1661
1662 ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
1663 return 0;
1664 }
1665
1666 /* The ctf_add_type routine is used to copy a type from a source CTF container
1667 to a dynamic destination container. This routine operates recursively by
1668 following the source type's links and embedded member types. If the
1669 destination container already contains a named type which has the same
1670 attributes, then we succeed and return this type but no changes occur. */
1671 static ctf_id_t
1672 ctf_add_type_internal (ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type,
1673 ctf_file_t *proc_tracking_fp)
1674 {
1675 ctf_id_t dst_type = CTF_ERR;
1676 uint32_t dst_kind = CTF_K_UNKNOWN;
1677 ctf_file_t *tmp_fp = dst_fp;
1678 ctf_id_t tmp;
1679
1680 const char *name;
1681 uint32_t kind, forward_kind, flag, vlen;
1682
1683 const ctf_type_t *src_tp, *dst_tp;
1684 ctf_bundle_t src, dst;
1685 ctf_encoding_t src_en, dst_en;
1686 ctf_arinfo_t src_ar, dst_ar;
1687
1688 ctf_funcinfo_t ctc;
1689
1690 ctf_id_t orig_src_type = src_type;
1691
1692 if (!(dst_fp->ctf_flags & LCTF_RDWR))
1693 return (ctf_set_errno (dst_fp, ECTF_RDONLY));
1694
1695 if ((src_tp = ctf_lookup_by_id (&src_fp, src_type)) == NULL)
1696 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
1697
1698 if ((ctf_type_resolve (src_fp, src_type) == CTF_ERR)
1699 && (ctf_errno (src_fp) == ECTF_NONREPRESENTABLE))
1700 return (ctf_set_errno (dst_fp, ECTF_NONREPRESENTABLE));
1701
1702 name = ctf_strptr (src_fp, src_tp->ctt_name);
1703 kind = LCTF_INFO_KIND (src_fp, src_tp->ctt_info);
1704 flag = LCTF_INFO_ISROOT (src_fp, src_tp->ctt_info);
1705 vlen = LCTF_INFO_VLEN (src_fp, src_tp->ctt_info);
1706
1707 /* If this is a type we are currently in the middle of adding, hand it
1708 straight back. (This lets us handle self-referential structures without
1709 considering forwards and empty structures the same as their completed
1710 forms.) */
1711
1712 tmp = ctf_type_mapping (src_fp, src_type, &tmp_fp);
1713
1714 if (tmp != 0)
1715 {
1716 if (ctf_dynhash_lookup (proc_tracking_fp->ctf_add_processing,
1717 (void *) (uintptr_t) src_type))
1718 return tmp;
1719
1720 /* If this type has already been added from this container, and is the same
1721 kind and (if a struct or union) has the same number of members, hand it
1722 straight back. */
1723
1724 if (ctf_type_kind_unsliced (tmp_fp, tmp) == (int) kind)
1725 {
1726 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION
1727 || kind == CTF_K_ENUM)
1728 {
1729 if ((dst_tp = ctf_lookup_by_id (&tmp_fp, dst_type)) != NULL)
1730 if (vlen == LCTF_INFO_VLEN (tmp_fp, dst_tp->ctt_info))
1731 return tmp;
1732 }
1733 else
1734 return tmp;
1735 }
1736 }
1737
1738 forward_kind = kind;
1739 if (kind == CTF_K_FORWARD)
1740 forward_kind = src_tp->ctt_type;
1741
1742 /* If the source type has a name and is a root type (visible at the
1743 top-level scope), lookup the name in the destination container and
1744 verify that it is of the same kind before we do anything else. */
1745
1746 if ((flag & CTF_ADD_ROOT) && name[0] != '\0'
1747 && (tmp = ctf_lookup_by_rawname (dst_fp, forward_kind, name)) != 0)
1748 {
1749 dst_type = tmp;
1750 dst_kind = ctf_type_kind_unsliced (dst_fp, dst_type);
1751 }
1752
1753 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
1754 unless dst_type is a forward declaration and src_type is a struct,
1755 union, or enum (i.e. the definition of the previous forward decl).
1756
1757 We also allow addition in the opposite order (addition of a forward when a
1758 struct, union, or enum already exists), which is a NOP and returns the
1759 already-present struct, union, or enum. */
1760
1761 if (dst_type != CTF_ERR && dst_kind != kind)
1762 {
1763 if (kind == CTF_K_FORWARD
1764 && (dst_kind == CTF_K_ENUM || dst_kind == CTF_K_STRUCT
1765 || dst_kind == CTF_K_UNION))
1766 {
1767 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
1768 return dst_type;
1769 }
1770
1771 if (dst_kind != CTF_K_FORWARD
1772 || (kind != CTF_K_ENUM && kind != CTF_K_STRUCT
1773 && kind != CTF_K_UNION))
1774 {
1775 ctf_dprintf ("Conflict for type %s: kinds differ, new: %i; "
1776 "old (ID %lx): %i\n", name, kind, dst_type, dst_kind);
1777 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1778 }
1779 }
1780
1781 /* We take special action for an integer, float, or slice since it is
1782 described not only by its name but also its encoding. For integers,
1783 bit-fields exploit this degeneracy. */
1784
1785 if (kind == CTF_K_INTEGER || kind == CTF_K_FLOAT || kind == CTF_K_SLICE)
1786 {
1787 if (ctf_type_encoding (src_fp, src_type, &src_en) != 0)
1788 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
1789
1790 if (dst_type != CTF_ERR)
1791 {
1792 ctf_file_t *fp = dst_fp;
1793
1794 if ((dst_tp = ctf_lookup_by_id (&fp, dst_type)) == NULL)
1795 return CTF_ERR;
1796
1797 if (ctf_type_encoding (dst_fp, dst_type, &dst_en) != 0)
1798 return CTF_ERR; /* errno set for us. */
1799
1800 if (LCTF_INFO_ISROOT (fp, dst_tp->ctt_info) & CTF_ADD_ROOT)
1801 {
1802 /* The type that we found in the hash is also root-visible. If
1803 the two types match then use the existing one; otherwise,
1804 declare a conflict. Note: slices are not certain to match
1805 even if there is no conflict: we must check the contained type
1806 too. */
1807
1808 if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0)
1809 {
1810 if (kind != CTF_K_SLICE)
1811 {
1812 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
1813 return dst_type;
1814 }
1815 }
1816 else
1817 {
1818 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1819 }
1820 }
1821 else
1822 {
1823 /* We found a non-root-visible type in the hash. If its encoding
1824 is the same, we can reuse it, unless it is a slice. */
1825
1826 if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0)
1827 {
1828 if (kind != CTF_K_SLICE)
1829 {
1830 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
1831 return dst_type;
1832 }
1833 }
1834 }
1835 }
1836 }
1837
1838 src.ctb_file = src_fp;
1839 src.ctb_type = src_type;
1840 src.ctb_dtd = NULL;
1841
1842 dst.ctb_file = dst_fp;
1843 dst.ctb_type = dst_type;
1844 dst.ctb_dtd = NULL;
1845
1846 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add
1847 a new type with the same properties as src_type to dst_fp. If dst_type is
1848 not CTF_ERR, then we verify that dst_type has the same attributes as
1849 src_type. We recurse for embedded references. Before we start, we note
1850 that we are processing this type, to prevent infinite recursion: we do not
1851 re-process any type that appears in this list. The list is emptied
1852 wholesale at the end of processing everything in this recursive stack. */
1853
1854 if (ctf_dynhash_insert (proc_tracking_fp->ctf_add_processing,
1855 (void *) (uintptr_t) src_type, (void *) 1) < 0)
1856 return ctf_set_errno (dst_fp, ENOMEM);
1857
1858 switch (kind)
1859 {
1860 case CTF_K_INTEGER:
1861 /* If we found a match we will have either returned it or declared a
1862 conflict. */
1863 dst_type = ctf_add_integer (dst_fp, flag, name, &src_en);
1864 break;
1865
1866 case CTF_K_FLOAT:
1867 /* If we found a match we will have either returned it or declared a
1868 conflict. */
1869 dst_type = ctf_add_float (dst_fp, flag, name, &src_en);
1870 break;
1871
1872 case CTF_K_SLICE:
1873 /* We have checked for conflicting encodings: now try to add the
1874 contained type. */
1875 src_type = ctf_type_reference (src_fp, src_type);
1876 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type,
1877 proc_tracking_fp);
1878
1879 if (src_type == CTF_ERR)
1880 return CTF_ERR; /* errno is set for us. */
1881
1882 dst_type = ctf_add_slice (dst_fp, flag, src_type, &src_en);
1883 break;
1884
1885 case CTF_K_POINTER:
1886 case CTF_K_VOLATILE:
1887 case CTF_K_CONST:
1888 case CTF_K_RESTRICT:
1889 src_type = ctf_type_reference (src_fp, src_type);
1890 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type,
1891 proc_tracking_fp);
1892
1893 if (src_type == CTF_ERR)
1894 return CTF_ERR; /* errno is set for us. */
1895
1896 dst_type = ctf_add_reftype (dst_fp, flag, src_type, kind);
1897 break;
1898
1899 case CTF_K_ARRAY:
1900 if (ctf_array_info (src_fp, src_type, &src_ar) != 0)
1901 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
1902
1903 src_ar.ctr_contents =
1904 ctf_add_type_internal (dst_fp, src_fp, src_ar.ctr_contents,
1905 proc_tracking_fp);
1906 src_ar.ctr_index = ctf_add_type_internal (dst_fp, src_fp,
1907 src_ar.ctr_index,
1908 proc_tracking_fp);
1909 src_ar.ctr_nelems = src_ar.ctr_nelems;
1910
1911 if (src_ar.ctr_contents == CTF_ERR || src_ar.ctr_index == CTF_ERR)
1912 return CTF_ERR; /* errno is set for us. */
1913
1914 if (dst_type != CTF_ERR)
1915 {
1916 if (ctf_array_info (dst_fp, dst_type, &dst_ar) != 0)
1917 return CTF_ERR; /* errno is set for us. */
1918
1919 if (memcmp (&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
1920 {
1921 ctf_dprintf ("Conflict for type %s against ID %lx: "
1922 "array info differs, old %lx/%lx/%x; "
1923 "new: %lx/%lx/%x\n", name, dst_type,
1924 src_ar.ctr_contents, src_ar.ctr_index,
1925 src_ar.ctr_nelems, dst_ar.ctr_contents,
1926 dst_ar.ctr_index, dst_ar.ctr_nelems);
1927 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1928 }
1929 }
1930 else
1931 dst_type = ctf_add_array (dst_fp, flag, &src_ar);
1932 break;
1933
1934 case CTF_K_FUNCTION:
1935 ctc.ctc_return = ctf_add_type_internal (dst_fp, src_fp,
1936 src_tp->ctt_type,
1937 proc_tracking_fp);
1938 ctc.ctc_argc = 0;
1939 ctc.ctc_flags = 0;
1940
1941 if (ctc.ctc_return == CTF_ERR)
1942 return CTF_ERR; /* errno is set for us. */
1943
1944 dst_type = ctf_add_function (dst_fp, flag, &ctc, NULL);
1945 break;
1946
1947 case CTF_K_STRUCT:
1948 case CTF_K_UNION:
1949 {
1950 ctf_dmdef_t *dmd;
1951 int errs = 0;
1952 size_t size;
1953 ssize_t ssize;
1954 ctf_dtdef_t *dtd;
1955
1956 /* Technically to match a struct or union we need to check both
1957 ways (src members vs. dst, dst members vs. src) but we make
1958 this more optimal by only checking src vs. dst and comparing
1959 the total size of the structure (which we must do anyway)
1960 which covers the possibility of dst members not in src.
1961 This optimization can be defeated for unions, but is so
1962 pathological as to render it irrelevant for our purposes. */
1963
1964 if (dst_type != CTF_ERR && kind != CTF_K_FORWARD
1965 && dst_kind != CTF_K_FORWARD)
1966 {
1967 if (ctf_type_size (src_fp, src_type) !=
1968 ctf_type_size (dst_fp, dst_type))
1969 {
1970 ctf_dprintf ("Conflict for type %s against ID %lx: "
1971 "union size differs, old %li, new %li\n",
1972 name, dst_type,
1973 (long) ctf_type_size (src_fp, src_type),
1974 (long) ctf_type_size (dst_fp, dst_type));
1975 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1976 }
1977
1978 if (ctf_member_iter (src_fp, src_type, membcmp, &dst))
1979 {
1980 ctf_dprintf ("Conflict for type %s against ID %lx: "
1981 "members differ, see above\n", name, dst_type);
1982 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1983 }
1984
1985 break;
1986 }
1987
1988 /* Unlike the other cases, copying structs and unions is done
1989 manually so as to avoid repeated lookups in ctf_add_member
1990 and to ensure the exact same member offsets as in src_type. */
1991
1992 dst_type = ctf_add_generic (dst_fp, flag, name, kind, &dtd);
1993 if (dst_type == CTF_ERR)
1994 return CTF_ERR; /* errno is set for us. */
1995
1996 dst.ctb_type = dst_type;
1997 dst.ctb_dtd = dtd;
1998
1999 /* Pre-emptively add this struct to the type mapping so that
2000 structures that refer to themselves work. */
2001 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
2002
2003 if (ctf_member_iter (src_fp, src_type, membadd, &dst) != 0)
2004 errs++; /* Increment errs and fail at bottom of case. */
2005
2006 if ((ssize = ctf_type_size (src_fp, src_type)) < 0)
2007 return CTF_ERR; /* errno is set for us. */
2008
2009 size = (size_t) ssize;
2010 if (size > CTF_MAX_SIZE)
2011 {
2012 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
2013 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
2014 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
2015 }
2016 else
2017 dtd->dtd_data.ctt_size = (uint32_t) size;
2018
2019 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, vlen);
2020
2021 /* Make a final pass through the members changing each dmd_type (a
2022 src_fp type) to an equivalent type in dst_fp. We pass through all
2023 members, leaving any that fail set to CTF_ERR, unless they fail
2024 because they are marking a member of type not representable in this
2025 version of CTF, in which case we just want to silently omit them:
2026 no consumer can do anything with them anyway. */
2027 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
2028 dmd != NULL; dmd = ctf_list_next (dmd))
2029 {
2030 ctf_file_t *dst = dst_fp;
2031 ctf_id_t memb_type;
2032
2033 memb_type = ctf_type_mapping (src_fp, dmd->dmd_type, &dst);
2034 if (memb_type == 0)
2035 {
2036 if ((dmd->dmd_type =
2037 ctf_add_type_internal (dst_fp, src_fp, dmd->dmd_type,
2038 proc_tracking_fp)) == CTF_ERR)
2039 {
2040 if (ctf_errno (dst_fp) != ECTF_NONREPRESENTABLE)
2041 errs++;
2042 }
2043 }
2044 else
2045 dmd->dmd_type = memb_type;
2046 }
2047
2048 if (errs)
2049 return CTF_ERR; /* errno is set for us. */
2050 break;
2051 }
2052
2053 case CTF_K_ENUM:
2054 if (dst_type != CTF_ERR && kind != CTF_K_FORWARD
2055 && dst_kind != CTF_K_FORWARD)
2056 {
2057 if (ctf_enum_iter (src_fp, src_type, enumcmp, &dst)
2058 || ctf_enum_iter (dst_fp, dst_type, enumcmp, &src))
2059 {
2060 ctf_dprintf ("Conflict for enum %s against ID %lx: "
2061 "members differ, see above\n", name, dst_type);
2062 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
2063 }
2064 }
2065 else
2066 {
2067 dst_type = ctf_add_enum (dst_fp, flag, name);
2068 if ((dst.ctb_type = dst_type) == CTF_ERR
2069 || ctf_enum_iter (src_fp, src_type, enumadd, &dst))
2070 return CTF_ERR; /* errno is set for us */
2071 }
2072 break;
2073
2074 case CTF_K_FORWARD:
2075 if (dst_type == CTF_ERR)
2076 dst_type = ctf_add_forward (dst_fp, flag, name, forward_kind);
2077 break;
2078
2079 case CTF_K_TYPEDEF:
2080 src_type = ctf_type_reference (src_fp, src_type);
2081 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type,
2082 proc_tracking_fp);
2083
2084 if (src_type == CTF_ERR)
2085 return CTF_ERR; /* errno is set for us. */
2086
2087 /* If dst_type is not CTF_ERR at this point, we should check if
2088 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
2089 ECTF_CONFLICT. However, this causes problems with bitness typedefs
2090 that vary based on things like if 32-bit then pid_t is int otherwise
2091 long. We therefore omit this check and assume that if the identically
2092 named typedef already exists in dst_fp, it is correct or
2093 equivalent. */
2094
2095 if (dst_type == CTF_ERR)
2096 dst_type = ctf_add_typedef (dst_fp, flag, name, src_type);
2097
2098 break;
2099
2100 default:
2101 return (ctf_set_errno (dst_fp, ECTF_CORRUPT));
2102 }
2103
2104 if (dst_type != CTF_ERR)
2105 ctf_add_type_mapping (src_fp, orig_src_type, dst_fp, dst_type);
2106 return dst_type;
2107 }
2108
2109 ctf_id_t
2110 ctf_add_type (ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
2111 {
2112 ctf_id_t id;
2113
2114 if (!src_fp->ctf_add_processing)
2115 src_fp->ctf_add_processing = ctf_dynhash_create (ctf_hash_integer,
2116 ctf_hash_eq_integer,
2117 NULL, NULL);
2118
2119 /* We store the hash on the source, because it contains only source type IDs:
2120 but callers will invariably expect errors to appear on the dest. */
2121 if (!src_fp->ctf_add_processing)
2122 return (ctf_set_errno (dst_fp, ENOMEM));
2123
2124 id = ctf_add_type_internal (dst_fp, src_fp, src_type, src_fp);
2125 ctf_dynhash_empty (src_fp->ctf_add_processing);
2126
2127 return id;
2128 }
2129
2130 /* Write the compressed CTF data stream to the specified gzFile descriptor. */
2131 int
2132 ctf_gzwrite (ctf_file_t *fp, gzFile fd)
2133 {
2134 const unsigned char *buf;
2135 ssize_t resid;
2136 ssize_t len;
2137
2138 resid = sizeof (ctf_header_t);
2139 buf = (unsigned char *) fp->ctf_header;
2140 while (resid != 0)
2141 {
2142 if ((len = gzwrite (fd, buf, resid)) <= 0)
2143 return (ctf_set_errno (fp, errno));
2144 resid -= len;
2145 buf += len;
2146 }
2147
2148 resid = fp->ctf_size;
2149 buf = fp->ctf_buf;
2150 while (resid != 0)
2151 {
2152 if ((len = gzwrite (fd, buf, resid)) <= 0)
2153 return (ctf_set_errno (fp, errno));
2154 resid -= len;
2155 buf += len;
2156 }
2157
2158 return 0;
2159 }
2160
2161 /* Compress the specified CTF data stream and write it to the specified file
2162 descriptor. */
2163 int
2164 ctf_compress_write (ctf_file_t *fp, int fd)
2165 {
2166 unsigned char *buf;
2167 unsigned char *bp;
2168 ctf_header_t h;
2169 ctf_header_t *hp = &h;
2170 ssize_t header_len = sizeof (ctf_header_t);
2171 ssize_t compress_len;
2172 ssize_t len;
2173 int rc;
2174 int err = 0;
2175
2176 if (ctf_serialize (fp) < 0)
2177 return -1; /* errno is set for us. */
2178
2179 memcpy (hp, fp->ctf_header, header_len);
2180 hp->cth_flags |= CTF_F_COMPRESS;
2181 compress_len = compressBound (fp->ctf_size);
2182
2183 if ((buf = malloc (compress_len)) == NULL)
2184 return (ctf_set_errno (fp, ECTF_ZALLOC));
2185
2186 if ((rc = compress (buf, (uLongf *) &compress_len,
2187 fp->ctf_buf, fp->ctf_size)) != Z_OK)
2188 {
2189 ctf_dprintf ("zlib deflate err: %s\n", zError (rc));
2190 err = ctf_set_errno (fp, ECTF_COMPRESS);
2191 goto ret;
2192 }
2193
2194 while (header_len > 0)
2195 {
2196 if ((len = write (fd, hp, header_len)) < 0)
2197 {
2198 err = ctf_set_errno (fp, errno);
2199 goto ret;
2200 }
2201 header_len -= len;
2202 hp += len;
2203 }
2204
2205 bp = buf;
2206 while (compress_len > 0)
2207 {
2208 if ((len = write (fd, bp, compress_len)) < 0)
2209 {
2210 err = ctf_set_errno (fp, errno);
2211 goto ret;
2212 }
2213 compress_len -= len;
2214 bp += len;
2215 }
2216
2217 ret:
2218 free (buf);
2219 return err;
2220 }
2221
2222 /* Optionally compress the specified CTF data stream and return it as a new
2223 dynamically-allocated string. */
2224 unsigned char *
2225 ctf_write_mem (ctf_file_t *fp, size_t *size, size_t threshold)
2226 {
2227 unsigned char *buf;
2228 unsigned char *bp;
2229 ctf_header_t *hp;
2230 ssize_t header_len = sizeof (ctf_header_t);
2231 ssize_t compress_len;
2232 int rc;
2233
2234 if (ctf_serialize (fp) < 0)
2235 return NULL; /* errno is set for us. */
2236
2237 compress_len = compressBound (fp->ctf_size);
2238 if (fp->ctf_size < threshold)
2239 compress_len = fp->ctf_size;
2240 if ((buf = malloc (compress_len
2241 + sizeof (struct ctf_header))) == NULL)
2242 {
2243 ctf_set_errno (fp, ENOMEM);
2244 return NULL;
2245 }
2246
2247 hp = (ctf_header_t *) buf;
2248 memcpy (hp, fp->ctf_header, header_len);
2249 bp = buf + sizeof (struct ctf_header);
2250 *size = sizeof (struct ctf_header);
2251
2252 if (fp->ctf_size < threshold)
2253 {
2254 hp->cth_flags &= ~CTF_F_COMPRESS;
2255 memcpy (bp, fp->ctf_buf, fp->ctf_size);
2256 *size += fp->ctf_size;
2257 }
2258 else
2259 {
2260 hp->cth_flags |= CTF_F_COMPRESS;
2261 if ((rc = compress (bp, (uLongf *) &compress_len,
2262 fp->ctf_buf, fp->ctf_size)) != Z_OK)
2263 {
2264 ctf_dprintf ("zlib deflate err: %s\n", zError (rc));
2265 ctf_set_errno (fp, ECTF_COMPRESS);
2266 free (buf);
2267 return NULL;
2268 }
2269 *size += compress_len;
2270 }
2271 return buf;
2272 }
2273
2274 /* Write the uncompressed CTF data stream to the specified file descriptor. */
2275 int
2276 ctf_write (ctf_file_t *fp, int fd)
2277 {
2278 const unsigned char *buf;
2279 ssize_t resid;
2280 ssize_t len;
2281
2282 if (ctf_serialize (fp) < 0)
2283 return -1; /* errno is set for us. */
2284
2285 resid = sizeof (ctf_header_t);
2286 buf = (unsigned char *) fp->ctf_header;
2287 while (resid != 0)
2288 {
2289 if ((len = write (fd, buf, resid)) <= 0)
2290 return (ctf_set_errno (fp, errno));
2291 resid -= len;
2292 buf += len;
2293 }
2294
2295 resid = fp->ctf_size;
2296 buf = fp->ctf_buf;
2297 while (resid != 0)
2298 {
2299 if ((len = write (fd, buf, resid)) <= 0)
2300 return (ctf_set_errno (fp, errno));
2301 resid -= len;
2302 buf += len;
2303 }
2304
2305 return 0;
2306 }