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