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