16e7de85c157af3dc5f630cbdf185a67e2c6ffd9
[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 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 ctf_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 ctf_free (buf);
495 ctf_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 ctf_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 ctf_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_snapshots = fp->ctf_snapshots + 1;
529 nfp->ctf_specific = fp->ctf_specific;
530 nfp->ctf_ptrtab = fp->ctf_ptrtab;
531 nfp->ctf_ptrtab_len = fp->ctf_ptrtab_len;
532 nfp->ctf_link_inputs = fp->ctf_link_inputs;
533 nfp->ctf_link_outputs = fp->ctf_link_outputs;
534 nfp->ctf_str_prov_offset = fp->ctf_str_prov_offset;
535 nfp->ctf_syn_ext_strtab = fp->ctf_syn_ext_strtab;
536 nfp->ctf_link_cu_mapping = fp->ctf_link_cu_mapping;
537 nfp->ctf_link_type_mapping = fp->ctf_link_type_mapping;
538 nfp->ctf_link_memb_name_changer = fp->ctf_link_memb_name_changer;
539 nfp->ctf_link_memb_name_changer_arg = fp->ctf_link_memb_name_changer_arg;
540
541 nfp->ctf_snapshot_lu = fp->ctf_snapshots;
542
543 memcpy (&nfp->ctf_lookups, fp->ctf_lookups, sizeof (fp->ctf_lookups));
544 nfp->ctf_structs = fp->ctf_structs;
545 nfp->ctf_unions = fp->ctf_unions;
546 nfp->ctf_enums = fp->ctf_enums;
547 nfp->ctf_names = fp->ctf_names;
548
549 fp->ctf_dthash = NULL;
550 ctf_str_free_atoms (nfp);
551 nfp->ctf_str_atoms = fp->ctf_str_atoms;
552 nfp->ctf_prov_strtab = fp->ctf_prov_strtab;
553 fp->ctf_str_atoms = NULL;
554 fp->ctf_prov_strtab = NULL;
555 memset (&fp->ctf_dtdefs, 0, sizeof (ctf_list_t));
556 fp->ctf_ptrtab = NULL;
557 fp->ctf_link_inputs = NULL;
558 fp->ctf_link_outputs = NULL;
559 fp->ctf_syn_ext_strtab = NULL;
560 fp->ctf_link_cu_mapping = NULL;
561 fp->ctf_link_type_mapping = NULL;
562
563 fp->ctf_dvhash = NULL;
564 memset (&fp->ctf_dvdefs, 0, sizeof (ctf_list_t));
565 memset (fp->ctf_lookups, 0, sizeof (fp->ctf_lookups));
566 fp->ctf_structs.ctn_writable = NULL;
567 fp->ctf_unions.ctn_writable = NULL;
568 fp->ctf_enums.ctn_writable = NULL;
569 fp->ctf_names.ctn_writable = NULL;
570
571 memcpy (&ofp, fp, sizeof (ctf_file_t));
572 memcpy (fp, nfp, sizeof (ctf_file_t));
573 memcpy (nfp, &ofp, sizeof (ctf_file_t));
574
575 nfp->ctf_refcnt = 1; /* Force nfp to be freed. */
576 ctf_file_close (nfp);
577
578 return 0;
579 }
580
581 ctf_names_t *
582 ctf_name_table (ctf_file_t *fp, int kind)
583 {
584 switch (kind)
585 {
586 case CTF_K_STRUCT:
587 return &fp->ctf_structs;
588 case CTF_K_UNION:
589 return &fp->ctf_unions;
590 case CTF_K_ENUM:
591 return &fp->ctf_enums;
592 default:
593 return &fp->ctf_names;
594 }
595 }
596
597 int
598 ctf_dtd_insert (ctf_file_t *fp, ctf_dtdef_t *dtd, int kind)
599 {
600 const char *name;
601 if (ctf_dynhash_insert (fp->ctf_dthash, (void *) dtd->dtd_type, dtd) < 0)
602 return -1;
603
604 if (dtd->dtd_data.ctt_name
605 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL)
606 {
607 if (ctf_dynhash_insert (ctf_name_table (fp, kind)->ctn_writable,
608 (char *) name, (void *) dtd->dtd_type) < 0)
609 {
610 ctf_dynhash_remove (fp->ctf_dthash, (void *) dtd->dtd_type);
611 return -1;
612 }
613 }
614 ctf_list_append (&fp->ctf_dtdefs, dtd);
615 return 0;
616 }
617
618 void
619 ctf_dtd_delete (ctf_file_t *fp, ctf_dtdef_t *dtd)
620 {
621 ctf_dmdef_t *dmd, *nmd;
622 int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
623 const char *name;
624
625 ctf_dynhash_remove (fp->ctf_dthash, (void *) dtd->dtd_type);
626
627 switch (kind)
628 {
629 case CTF_K_STRUCT:
630 case CTF_K_UNION:
631 case CTF_K_ENUM:
632 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
633 dmd != NULL; dmd = nmd)
634 {
635 if (dmd->dmd_name != NULL)
636 ctf_free (dmd->dmd_name);
637 nmd = ctf_list_next (dmd);
638 ctf_free (dmd);
639 }
640 break;
641 case CTF_K_FUNCTION:
642 ctf_free (dtd->dtd_u.dtu_argv);
643 break;
644 }
645
646 if (dtd->dtd_data.ctt_name
647 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL)
648 {
649 ctf_dynhash_remove (ctf_name_table (fp, kind)->ctn_writable,
650 name);
651 ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
652 }
653
654 ctf_list_delete (&fp->ctf_dtdefs, dtd);
655 ctf_free (dtd);
656 }
657
658 ctf_dtdef_t *
659 ctf_dtd_lookup (const ctf_file_t *fp, ctf_id_t type)
660 {
661 return (ctf_dtdef_t *) ctf_dynhash_lookup (fp->ctf_dthash, (void *) type);
662 }
663
664 ctf_dtdef_t *
665 ctf_dynamic_type (const ctf_file_t *fp, ctf_id_t id)
666 {
667 ctf_id_t idx;
668
669 if (!(fp->ctf_flags & LCTF_RDWR))
670 return NULL;
671
672 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, id))
673 fp = fp->ctf_parent;
674
675 idx = LCTF_TYPE_TO_INDEX(fp, id);
676
677 if ((unsigned long) idx <= fp->ctf_typemax)
678 return ctf_dtd_lookup (fp, id);
679 return NULL;
680 }
681
682 int
683 ctf_dvd_insert (ctf_file_t *fp, ctf_dvdef_t *dvd)
684 {
685 if (ctf_dynhash_insert (fp->ctf_dvhash, dvd->dvd_name, dvd) < 0)
686 return -1;
687 ctf_list_append (&fp->ctf_dvdefs, dvd);
688 return 0;
689 }
690
691 void
692 ctf_dvd_delete (ctf_file_t *fp, ctf_dvdef_t *dvd)
693 {
694 ctf_dynhash_remove (fp->ctf_dvhash, dvd->dvd_name);
695 ctf_free (dvd->dvd_name);
696
697 ctf_list_delete (&fp->ctf_dvdefs, dvd);
698 ctf_free (dvd);
699 }
700
701 ctf_dvdef_t *
702 ctf_dvd_lookup (const ctf_file_t *fp, const char *name)
703 {
704 return (ctf_dvdef_t *) ctf_dynhash_lookup (fp->ctf_dvhash, name);
705 }
706
707 /* Discard all of the dynamic type definitions and variable definitions that
708 have been added to the container since the last call to ctf_update(). We
709 locate such types by scanning the dtd list and deleting elements that have
710 type IDs greater than ctf_dtoldid, which is set by ctf_update(), above, and
711 by scanning the variable list and deleting elements that have update IDs
712 equal to the current value of the last-update snapshot count (indicating that
713 they were added after the most recent call to ctf_update()). */
714 int
715 ctf_discard (ctf_file_t *fp)
716 {
717 ctf_snapshot_id_t last_update =
718 { fp->ctf_dtoldid,
719 fp->ctf_snapshot_lu + 1 };
720
721 /* Update required? */
722 if (!(fp->ctf_flags & LCTF_DIRTY))
723 return 0;
724
725 return (ctf_rollback (fp, last_update));
726 }
727
728 ctf_snapshot_id_t
729 ctf_snapshot (ctf_file_t *fp)
730 {
731 ctf_snapshot_id_t snapid;
732 snapid.dtd_id = fp->ctf_typemax;
733 snapid.snapshot_id = fp->ctf_snapshots++;
734 return snapid;
735 }
736
737 /* Like ctf_discard(), only discards everything after a particular ID. */
738 int
739 ctf_rollback (ctf_file_t *fp, ctf_snapshot_id_t id)
740 {
741 ctf_dtdef_t *dtd, *ntd;
742 ctf_dvdef_t *dvd, *nvd;
743
744 if (!(fp->ctf_flags & LCTF_RDWR))
745 return (ctf_set_errno (fp, ECTF_RDONLY));
746
747 if (fp->ctf_snapshot_lu >= id.snapshot_id)
748 return (ctf_set_errno (fp, ECTF_OVERROLLBACK));
749
750 for (dtd = ctf_list_next (&fp->ctf_dtdefs); dtd != NULL; dtd = ntd)
751 {
752 int kind;
753 const char *name;
754
755 ntd = ctf_list_next (dtd);
756
757 if (LCTF_TYPE_TO_INDEX (fp, dtd->dtd_type) <= id.dtd_id)
758 continue;
759
760 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
761
762 if (dtd->dtd_data.ctt_name
763 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL)
764 {
765 ctf_dynhash_remove (ctf_name_table (fp, kind)->ctn_writable,
766 name);
767 ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
768 }
769
770 ctf_dynhash_remove (fp->ctf_dthash, (void *) dtd->dtd_type);
771 ctf_dtd_delete (fp, dtd);
772 }
773
774 for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd)
775 {
776 nvd = ctf_list_next (dvd);
777
778 if (dvd->dvd_snapshots <= id.snapshot_id)
779 continue;
780
781 ctf_dvd_delete (fp, dvd);
782 }
783
784 fp->ctf_typemax = id.dtd_id;
785 fp->ctf_snapshots = id.snapshot_id;
786
787 if (fp->ctf_snapshots == fp->ctf_snapshot_lu)
788 fp->ctf_flags &= ~LCTF_DIRTY;
789
790 return 0;
791 }
792
793 static ctf_id_t
794 ctf_add_generic (ctf_file_t *fp, uint32_t flag, const char *name, int kind,
795 ctf_dtdef_t **rp)
796 {
797 ctf_dtdef_t *dtd;
798 ctf_id_t type;
799
800 if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
801 return (ctf_set_errno (fp, EINVAL));
802
803 if (!(fp->ctf_flags & LCTF_RDWR))
804 return (ctf_set_errno (fp, ECTF_RDONLY));
805
806 if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) >= CTF_MAX_TYPE)
807 return (ctf_set_errno (fp, ECTF_FULL));
808
809 if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) == (CTF_MAX_PTYPE - 1))
810 return (ctf_set_errno (fp, ECTF_FULL));
811
812 /* Make sure ptrtab always grows to be big enough for all types. */
813 if (ctf_grow_ptrtab (fp) < 0)
814 return CTF_ERR; /* errno is set for us. */
815
816 if ((dtd = ctf_alloc (sizeof (ctf_dtdef_t))) == NULL)
817 return (ctf_set_errno (fp, EAGAIN));
818
819 type = ++fp->ctf_typemax;
820 type = LCTF_INDEX_TO_TYPE (fp, type, (fp->ctf_flags & LCTF_CHILD));
821
822 memset (dtd, 0, sizeof (ctf_dtdef_t));
823 dtd->dtd_data.ctt_name = ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name);
824 dtd->dtd_type = type;
825
826 if (dtd->dtd_data.ctt_name == 0 && name != NULL && name[0] != '\0')
827 {
828 ctf_free (dtd);
829 return (ctf_set_errno (fp, EAGAIN));
830 }
831
832 if (ctf_dtd_insert (fp, dtd, kind) < 0)
833 {
834 ctf_free (dtd);
835 return CTF_ERR; /* errno is set for us. */
836 }
837 fp->ctf_flags |= LCTF_DIRTY;
838
839 *rp = dtd;
840 return type;
841 }
842
843 /* When encoding integer sizes, we want to convert a byte count in the range
844 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
845 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
846 static size_t
847 clp2 (size_t x)
848 {
849 x--;
850
851 x |= (x >> 1);
852 x |= (x >> 2);
853 x |= (x >> 4);
854 x |= (x >> 8);
855 x |= (x >> 16);
856
857 return (x + 1);
858 }
859
860 static ctf_id_t
861 ctf_add_encoded (ctf_file_t *fp, uint32_t flag,
862 const char *name, const ctf_encoding_t *ep, uint32_t kind)
863 {
864 ctf_dtdef_t *dtd;
865 ctf_id_t type;
866
867 if (ep == NULL)
868 return (ctf_set_errno (fp, EINVAL));
869
870 if ((type = ctf_add_generic (fp, flag, name, kind, &dtd)) == CTF_ERR)
871 return CTF_ERR; /* errno is set for us. */
872
873 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
874 dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
875 / CHAR_BIT);
876 dtd->dtd_u.dtu_enc = *ep;
877
878 return type;
879 }
880
881 static ctf_id_t
882 ctf_add_reftype (ctf_file_t *fp, uint32_t flag, ctf_id_t ref, uint32_t kind)
883 {
884 ctf_dtdef_t *dtd;
885 ctf_id_t type;
886 ctf_file_t *tmp = fp;
887 int child = fp->ctf_flags & LCTF_CHILD;
888
889 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
890 return (ctf_set_errno (fp, EINVAL));
891
892 if (ctf_lookup_by_id (&tmp, ref) == NULL)
893 return CTF_ERR; /* errno is set for us. */
894
895 if ((type = ctf_add_generic (fp, flag, NULL, kind, &dtd)) == CTF_ERR)
896 return CTF_ERR; /* errno is set for us. */
897
898 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
899 dtd->dtd_data.ctt_type = (uint32_t) ref;
900
901 if (kind != CTF_K_POINTER)
902 return type;
903
904 /* If we are adding a pointer, update the ptrtab, both the directly pointed-to
905 type and (if an anonymous typedef node is being pointed at) the type that
906 points at too. Note that ctf_typemax is at this point one higher than we
907 want to check against, because it's just been incremented for the addition
908 of this type. */
909
910 uint32_t type_idx = LCTF_TYPE_TO_INDEX (fp, type);
911 uint32_t ref_idx = LCTF_TYPE_TO_INDEX (fp, ref);
912
913 if (LCTF_TYPE_ISCHILD (fp, ref) == child
914 && ref_idx < fp->ctf_typemax)
915 {
916 fp->ctf_ptrtab[ref_idx] = type_idx;
917
918 ctf_id_t refref_idx = LCTF_TYPE_TO_INDEX (fp, dtd->dtd_data.ctt_type);
919
920 if (tmp == fp
921 && (LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) == CTF_K_TYPEDEF)
922 && strcmp (ctf_strptr (fp, dtd->dtd_data.ctt_name), "") == 0
923 && refref_idx < fp->ctf_typemax)
924 fp->ctf_ptrtab[refref_idx] = type_idx;
925 }
926
927 return type;
928 }
929
930 ctf_id_t
931 ctf_add_slice (ctf_file_t *fp, uint32_t flag, ctf_id_t ref,
932 const ctf_encoding_t *ep)
933 {
934 ctf_dtdef_t *dtd;
935 ctf_id_t type;
936 int kind;
937 const ctf_type_t *tp;
938 ctf_file_t *tmp = fp;
939
940 if (ep == NULL)
941 return (ctf_set_errno (fp, EINVAL));
942
943 if ((ep->cte_bits > 255) || (ep->cte_offset > 255))
944 return (ctf_set_errno (fp, ECTF_SLICEOVERFLOW));
945
946 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
947 return (ctf_set_errno (fp, EINVAL));
948
949 if ((tp = ctf_lookup_by_id (&tmp, ref)) == NULL)
950 return CTF_ERR; /* errno is set for us. */
951
952 kind = ctf_type_kind_unsliced (tmp, ref);
953 if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) &&
954 (kind != CTF_K_ENUM))
955 return (ctf_set_errno (fp, ECTF_NOTINTFP));
956
957 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_SLICE, &dtd)) == CTF_ERR)
958 return CTF_ERR; /* errno is set for us. */
959
960 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0);
961 dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
962 / CHAR_BIT);
963 dtd->dtd_u.dtu_slice.cts_type = ref;
964 dtd->dtd_u.dtu_slice.cts_bits = ep->cte_bits;
965 dtd->dtd_u.dtu_slice.cts_offset = ep->cte_offset;
966
967 return type;
968 }
969
970 ctf_id_t
971 ctf_add_integer (ctf_file_t *fp, uint32_t flag,
972 const char *name, const ctf_encoding_t *ep)
973 {
974 return (ctf_add_encoded (fp, flag, name, ep, CTF_K_INTEGER));
975 }
976
977 ctf_id_t
978 ctf_add_float (ctf_file_t *fp, uint32_t flag,
979 const char *name, const ctf_encoding_t *ep)
980 {
981 return (ctf_add_encoded (fp, flag, name, ep, CTF_K_FLOAT));
982 }
983
984 ctf_id_t
985 ctf_add_pointer (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
986 {
987 return (ctf_add_reftype (fp, flag, ref, CTF_K_POINTER));
988 }
989
990 ctf_id_t
991 ctf_add_array (ctf_file_t *fp, uint32_t flag, const ctf_arinfo_t *arp)
992 {
993 ctf_dtdef_t *dtd;
994 ctf_id_t type;
995 ctf_file_t *tmp = fp;
996
997 if (arp == NULL)
998 return (ctf_set_errno (fp, EINVAL));
999
1000 if (ctf_lookup_by_id (&tmp, arp->ctr_contents) == NULL)
1001 return CTF_ERR; /* errno is set for us. */
1002
1003 tmp = fp;
1004 if (ctf_lookup_by_id (&tmp, arp->ctr_index) == NULL)
1005 return CTF_ERR; /* errno is set for us. */
1006
1007 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_ARRAY, &dtd)) == CTF_ERR)
1008 return CTF_ERR; /* errno is set for us. */
1009
1010 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ARRAY, flag, 0);
1011 dtd->dtd_data.ctt_size = 0;
1012 dtd->dtd_u.dtu_arr = *arp;
1013
1014 return type;
1015 }
1016
1017 int
1018 ctf_set_array (ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
1019 {
1020 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type);
1021
1022 if (!(fp->ctf_flags & LCTF_RDWR))
1023 return (ctf_set_errno (fp, ECTF_RDONLY));
1024
1025 if (dtd == NULL
1026 || LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
1027 return (ctf_set_errno (fp, ECTF_BADID));
1028
1029 fp->ctf_flags |= LCTF_DIRTY;
1030 dtd->dtd_u.dtu_arr = *arp;
1031
1032 return 0;
1033 }
1034
1035 ctf_id_t
1036 ctf_add_function (ctf_file_t *fp, uint32_t flag,
1037 const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
1038 {
1039 ctf_dtdef_t *dtd;
1040 ctf_id_t type;
1041 uint32_t vlen;
1042 ctf_id_t *vdat = NULL;
1043 ctf_file_t *tmp = fp;
1044 size_t i;
1045
1046 if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0
1047 || (ctc->ctc_argc != 0 && argv == NULL))
1048 return (ctf_set_errno (fp, EINVAL));
1049
1050 vlen = ctc->ctc_argc;
1051 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1052 vlen++; /* Add trailing zero to indicate varargs (see below). */
1053
1054 if (ctf_lookup_by_id (&tmp, ctc->ctc_return) == NULL)
1055 return CTF_ERR; /* errno is set for us. */
1056
1057 for (i = 0; i < ctc->ctc_argc; i++)
1058 {
1059 tmp = fp;
1060 if (ctf_lookup_by_id (&tmp, argv[i]) == NULL)
1061 return CTF_ERR; /* errno is set for us. */
1062 }
1063
1064 if (vlen > CTF_MAX_VLEN)
1065 return (ctf_set_errno (fp, EOVERFLOW));
1066
1067 if (vlen != 0 && (vdat = ctf_alloc (sizeof (ctf_id_t) * vlen)) == NULL)
1068 return (ctf_set_errno (fp, EAGAIN));
1069
1070 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_FUNCTION,
1071 &dtd)) == CTF_ERR)
1072 {
1073 ctf_free (vdat);
1074 return CTF_ERR; /* errno is set for us. */
1075 }
1076
1077 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FUNCTION, flag, vlen);
1078 dtd->dtd_data.ctt_type = (uint32_t) ctc->ctc_return;
1079
1080 memcpy (vdat, argv, sizeof (ctf_id_t) * ctc->ctc_argc);
1081 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1082 vdat[vlen - 1] = 0; /* Add trailing zero to indicate varargs. */
1083 dtd->dtd_u.dtu_argv = vdat;
1084
1085 return type;
1086 }
1087
1088 ctf_id_t
1089 ctf_add_struct_sized (ctf_file_t *fp, uint32_t flag, const char *name,
1090 size_t size)
1091 {
1092 ctf_dtdef_t *dtd;
1093 ctf_id_t type = 0;
1094
1095 /* Promote forwards to structs. */
1096
1097 if (name != NULL)
1098 type = ctf_lookup_by_rawname (fp, CTF_K_STRUCT, name);
1099
1100 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1101 dtd = ctf_dtd_lookup (fp, type);
1102 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_STRUCT,
1103 &dtd)) == CTF_ERR)
1104 return CTF_ERR; /* errno is set for us. */
1105
1106 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_STRUCT, flag, 0);
1107
1108 if (size > CTF_MAX_SIZE)
1109 {
1110 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1111 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
1112 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
1113 }
1114 else
1115 dtd->dtd_data.ctt_size = (uint32_t) size;
1116
1117 return type;
1118 }
1119
1120 ctf_id_t
1121 ctf_add_struct (ctf_file_t *fp, uint32_t flag, const char *name)
1122 {
1123 return (ctf_add_struct_sized (fp, flag, name, 0));
1124 }
1125
1126 ctf_id_t
1127 ctf_add_union_sized (ctf_file_t *fp, uint32_t flag, const char *name,
1128 size_t size)
1129 {
1130 ctf_dtdef_t *dtd;
1131 ctf_id_t type = 0;
1132
1133 /* Promote forwards to unions. */
1134 if (name != NULL)
1135 type = ctf_lookup_by_rawname (fp, CTF_K_UNION, name);
1136
1137 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1138 dtd = ctf_dtd_lookup (fp, type);
1139 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_UNION,
1140 &dtd)) == CTF_ERR)
1141 return CTF_ERR; /* errno is set for us */
1142
1143 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_UNION, flag, 0);
1144
1145 if (size > CTF_MAX_SIZE)
1146 {
1147 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1148 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
1149 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
1150 }
1151 else
1152 dtd->dtd_data.ctt_size = (uint32_t) size;
1153
1154 return type;
1155 }
1156
1157 ctf_id_t
1158 ctf_add_union (ctf_file_t *fp, uint32_t flag, const char *name)
1159 {
1160 return (ctf_add_union_sized (fp, flag, name, 0));
1161 }
1162
1163 ctf_id_t
1164 ctf_add_enum (ctf_file_t *fp, uint32_t flag, const char *name)
1165 {
1166 ctf_dtdef_t *dtd;
1167 ctf_id_t type = 0;
1168
1169 /* Promote forwards to enums. */
1170 if (name != NULL)
1171 type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
1172
1173 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1174 dtd = ctf_dtd_lookup (fp, type);
1175 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_ENUM,
1176 &dtd)) == CTF_ERR)
1177 return CTF_ERR; /* errno is set for us. */
1178
1179 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ENUM, flag, 0);
1180 dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
1181
1182 return type;
1183 }
1184
1185 ctf_id_t
1186 ctf_add_enum_encoded (ctf_file_t *fp, uint32_t flag, const char *name,
1187 const ctf_encoding_t *ep)
1188 {
1189 ctf_id_t type = 0;
1190
1191 /* First, create the enum if need be, using most of the same machinery as
1192 ctf_add_enum(), to ensure that we do not allow things past that are not
1193 enums or forwards to them. (This includes other slices: you cannot slice a
1194 slice, which would be a useless thing to do anyway.) */
1195
1196 if (name != NULL)
1197 type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
1198
1199 if (type != 0)
1200 {
1201 if ((ctf_type_kind (fp, type) != CTF_K_FORWARD) &&
1202 (ctf_type_kind_unsliced (fp, type) != CTF_K_ENUM))
1203 return (ctf_set_errno (fp, ECTF_NOTINTFP));
1204 }
1205 else if ((type = ctf_add_enum (fp, flag, name)) == CTF_ERR)
1206 return CTF_ERR; /* errno is set for us. */
1207
1208 /* Now attach a suitable slice to it. */
1209
1210 return ctf_add_slice (fp, flag, type, ep);
1211 }
1212
1213 ctf_id_t
1214 ctf_add_forward (ctf_file_t *fp, uint32_t flag, const char *name,
1215 uint32_t kind)
1216 {
1217 ctf_dtdef_t *dtd;
1218 ctf_id_t type = 0;
1219
1220 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION && kind != CTF_K_ENUM)
1221 return (ctf_set_errno (fp, ECTF_NOTSUE));
1222
1223 /* If the type is already defined or exists as a forward tag, just
1224 return the ctf_id_t of the existing definition. */
1225
1226 if (name != NULL)
1227 type = ctf_lookup_by_rawname (fp, kind, name);
1228
1229 if ((type = ctf_add_generic (fp, flag, name, CTF_K_FORWARD,&dtd)) == CTF_ERR)
1230 return CTF_ERR; /* errno is set for us. */
1231
1232 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FORWARD, flag, 0);
1233 dtd->dtd_data.ctt_type = kind;
1234
1235 return type;
1236 }
1237
1238 ctf_id_t
1239 ctf_add_typedef (ctf_file_t *fp, uint32_t flag, const char *name,
1240 ctf_id_t ref)
1241 {
1242 ctf_dtdef_t *dtd;
1243 ctf_id_t type;
1244 ctf_file_t *tmp = fp;
1245
1246 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
1247 return (ctf_set_errno (fp, EINVAL));
1248
1249 if (ctf_lookup_by_id (&tmp, ref) == NULL)
1250 return CTF_ERR; /* errno is set for us. */
1251
1252 if ((type = ctf_add_generic (fp, flag, name, CTF_K_TYPEDEF,
1253 &dtd)) == CTF_ERR)
1254 return CTF_ERR; /* errno is set for us. */
1255
1256 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_TYPEDEF, flag, 0);
1257 dtd->dtd_data.ctt_type = (uint32_t) ref;
1258
1259 return type;
1260 }
1261
1262 ctf_id_t
1263 ctf_add_volatile (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1264 {
1265 return (ctf_add_reftype (fp, flag, ref, CTF_K_VOLATILE));
1266 }
1267
1268 ctf_id_t
1269 ctf_add_const (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1270 {
1271 return (ctf_add_reftype (fp, flag, ref, CTF_K_CONST));
1272 }
1273
1274 ctf_id_t
1275 ctf_add_restrict (ctf_file_t *fp, uint32_t flag, ctf_id_t ref)
1276 {
1277 return (ctf_add_reftype (fp, flag, ref, CTF_K_RESTRICT));
1278 }
1279
1280 int
1281 ctf_add_enumerator (ctf_file_t *fp, ctf_id_t enid, const char *name,
1282 int value)
1283 {
1284 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, enid);
1285 ctf_dmdef_t *dmd;
1286
1287 uint32_t kind, vlen, root;
1288 char *s;
1289
1290 if (name == NULL)
1291 return (ctf_set_errno (fp, EINVAL));
1292
1293 if (!(fp->ctf_flags & LCTF_RDWR))
1294 return (ctf_set_errno (fp, ECTF_RDONLY));
1295
1296 if (dtd == NULL)
1297 return (ctf_set_errno (fp, ECTF_BADID));
1298
1299 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1300 root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info);
1301 vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
1302
1303 if (kind != CTF_K_ENUM)
1304 return (ctf_set_errno (fp, ECTF_NOTENUM));
1305
1306 if (vlen == CTF_MAX_VLEN)
1307 return (ctf_set_errno (fp, ECTF_DTFULL));
1308
1309 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
1310 dmd != NULL; dmd = ctf_list_next (dmd))
1311 {
1312 if (strcmp (dmd->dmd_name, name) == 0)
1313 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1314 }
1315
1316 if ((dmd = ctf_alloc (sizeof (ctf_dmdef_t))) == NULL)
1317 return (ctf_set_errno (fp, EAGAIN));
1318
1319 if ((s = ctf_strdup (name)) == NULL)
1320 {
1321 ctf_free (dmd);
1322 return (ctf_set_errno (fp, EAGAIN));
1323 }
1324
1325 dmd->dmd_name = s;
1326 dmd->dmd_type = CTF_ERR;
1327 dmd->dmd_offset = 0;
1328 dmd->dmd_value = value;
1329
1330 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1);
1331 ctf_list_append (&dtd->dtd_u.dtu_members, dmd);
1332
1333 fp->ctf_flags |= LCTF_DIRTY;
1334
1335 return 0;
1336 }
1337
1338 int
1339 ctf_add_member_offset (ctf_file_t *fp, ctf_id_t souid, const char *name,
1340 ctf_id_t type, unsigned long bit_offset)
1341 {
1342 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, souid);
1343 ctf_dmdef_t *dmd;
1344
1345 ssize_t msize, malign, ssize;
1346 uint32_t kind, vlen, root;
1347 char *s = NULL;
1348
1349 if (!(fp->ctf_flags & LCTF_RDWR))
1350 return (ctf_set_errno (fp, ECTF_RDONLY));
1351
1352 if (dtd == NULL)
1353 return (ctf_set_errno (fp, ECTF_BADID));
1354
1355 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1356 root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info);
1357 vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
1358
1359 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
1360 return (ctf_set_errno (fp, ECTF_NOTSOU));
1361
1362 if (vlen == CTF_MAX_VLEN)
1363 return (ctf_set_errno (fp, ECTF_DTFULL));
1364
1365 if (name != NULL)
1366 {
1367 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
1368 dmd != NULL; dmd = ctf_list_next (dmd))
1369 {
1370 if (dmd->dmd_name != NULL && strcmp (dmd->dmd_name, name) == 0)
1371 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1372 }
1373 }
1374
1375 if ((msize = ctf_type_size (fp, type)) < 0 ||
1376 (malign = ctf_type_align (fp, type)) < 0)
1377 return -1; /* errno is set for us. */
1378
1379 if ((dmd = ctf_alloc (sizeof (ctf_dmdef_t))) == NULL)
1380 return (ctf_set_errno (fp, EAGAIN));
1381
1382 if (name != NULL && (s = ctf_strdup (name)) == NULL)
1383 {
1384 ctf_free (dmd);
1385 return (ctf_set_errno (fp, EAGAIN));
1386 }
1387
1388 dmd->dmd_name = s;
1389 dmd->dmd_type = type;
1390 dmd->dmd_value = -1;
1391
1392 if (kind == CTF_K_STRUCT && vlen != 0)
1393 {
1394 if (bit_offset == (unsigned long) - 1)
1395 {
1396 /* Natural alignment. */
1397
1398 ctf_dmdef_t *lmd = ctf_list_prev (&dtd->dtd_u.dtu_members);
1399 ctf_id_t ltype = ctf_type_resolve (fp, lmd->dmd_type);
1400 size_t off = lmd->dmd_offset;
1401
1402 ctf_encoding_t linfo;
1403 ssize_t lsize;
1404
1405 if (ctf_type_encoding (fp, ltype, &linfo) == 0)
1406 off += linfo.cte_bits;
1407 else if ((lsize = ctf_type_size (fp, ltype)) > 0)
1408 off += lsize * CHAR_BIT;
1409
1410 /* Round up the offset of the end of the last member to
1411 the next byte boundary, convert 'off' to bytes, and
1412 then round it up again to the next multiple of the
1413 alignment required by the new member. Finally,
1414 convert back to bits and store the result in
1415 dmd_offset. Technically we could do more efficient
1416 packing if the new member is a bit-field, but we're
1417 the "compiler" and ANSI says we can do as we choose. */
1418
1419 off = roundup (off, CHAR_BIT) / CHAR_BIT;
1420 off = roundup (off, MAX (malign, 1));
1421 dmd->dmd_offset = off * CHAR_BIT;
1422 ssize = off + msize;
1423 }
1424 else
1425 {
1426 /* Specified offset in bits. */
1427
1428 dmd->dmd_offset = bit_offset;
1429 ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL);
1430 ssize = MAX (ssize, ((signed) bit_offset / CHAR_BIT) + msize);
1431 }
1432 }
1433 else
1434 {
1435 dmd->dmd_offset = 0;
1436 ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL);
1437 ssize = MAX (ssize, msize);
1438 }
1439
1440 if ((size_t) ssize > CTF_MAX_SIZE)
1441 {
1442 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1443 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (ssize);
1444 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (ssize);
1445 }
1446 else
1447 dtd->dtd_data.ctt_size = (uint32_t) ssize;
1448
1449 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1);
1450 ctf_list_append (&dtd->dtd_u.dtu_members, dmd);
1451
1452 fp->ctf_flags |= LCTF_DIRTY;
1453 return 0;
1454 }
1455
1456 int
1457 ctf_add_member_encoded (ctf_file_t *fp, ctf_id_t souid, const char *name,
1458 ctf_id_t type, unsigned long bit_offset,
1459 const ctf_encoding_t encoding)
1460 {
1461 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type);
1462 int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1463 int otype = type;
1464
1465 if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) && (kind != CTF_K_ENUM))
1466 return (ctf_set_errno (fp, ECTF_NOTINTFP));
1467
1468 if ((type = ctf_add_slice (fp, CTF_ADD_NONROOT, otype, &encoding)) == CTF_ERR)
1469 return -1; /* errno is set for us. */
1470
1471 return ctf_add_member_offset (fp, souid, name, type, bit_offset);
1472 }
1473
1474 int
1475 ctf_add_member (ctf_file_t *fp, ctf_id_t souid, const char *name,
1476 ctf_id_t type)
1477 {
1478 return ctf_add_member_offset (fp, souid, name, type, (unsigned long) - 1);
1479 }
1480
1481 int
1482 ctf_add_variable (ctf_file_t *fp, const char *name, ctf_id_t ref)
1483 {
1484 ctf_dvdef_t *dvd;
1485 ctf_file_t *tmp = fp;
1486
1487 if (!(fp->ctf_flags & LCTF_RDWR))
1488 return (ctf_set_errno (fp, ECTF_RDONLY));
1489
1490 if (ctf_dvd_lookup (fp, name) != NULL)
1491 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1492
1493 if (ctf_lookup_by_id (&tmp, ref) == NULL)
1494 return -1; /* errno is set for us. */
1495
1496 /* Make sure this type is representable. */
1497 if ((ctf_type_resolve (fp, ref) == CTF_ERR)
1498 && (ctf_errno (fp) == ECTF_NONREPRESENTABLE))
1499 return -1;
1500
1501 if ((dvd = ctf_alloc (sizeof (ctf_dvdef_t))) == NULL)
1502 return (ctf_set_errno (fp, EAGAIN));
1503
1504 if (name != NULL && (dvd->dvd_name = ctf_strdup (name)) == NULL)
1505 {
1506 ctf_free (dvd);
1507 return (ctf_set_errno (fp, EAGAIN));
1508 }
1509 dvd->dvd_type = ref;
1510 dvd->dvd_snapshots = fp->ctf_snapshots;
1511
1512 if (ctf_dvd_insert (fp, dvd) < 0)
1513 {
1514 ctf_free (dvd);
1515 return -1; /* errno is set for us. */
1516 }
1517
1518 fp->ctf_flags |= LCTF_DIRTY;
1519 return 0;
1520 }
1521
1522 static int
1523 enumcmp (const char *name, int value, void *arg)
1524 {
1525 ctf_bundle_t *ctb = arg;
1526 int bvalue;
1527
1528 if (ctf_enum_value (ctb->ctb_file, ctb->ctb_type, name, &bvalue) < 0)
1529 {
1530 ctf_dprintf ("Conflict due to member %s iteration error.\n", name);
1531 return 1;
1532 }
1533 if (value != bvalue)
1534 {
1535 ctf_dprintf ("Conflict due to value change: %i versus %i\n",
1536 value, bvalue);
1537 return 1;
1538 }
1539 return 0;
1540 }
1541
1542 static int
1543 enumadd (const char *name, int value, void *arg)
1544 {
1545 ctf_bundle_t *ctb = arg;
1546
1547 return (ctf_add_enumerator (ctb->ctb_file, ctb->ctb_type,
1548 name, value) < 0);
1549 }
1550
1551 static int
1552 membcmp (const char *name, ctf_id_t type _libctf_unused_, unsigned long offset,
1553 void *arg)
1554 {
1555 ctf_bundle_t *ctb = arg;
1556 ctf_membinfo_t ctm;
1557
1558 if (ctf_member_info (ctb->ctb_file, ctb->ctb_type, name, &ctm) < 0)
1559 {
1560 ctf_dprintf ("Conflict due to member %s iteration error.\n", name);
1561 return 1;
1562 }
1563 if (ctm.ctm_offset != offset)
1564 {
1565 ctf_dprintf ("Conflict due to member %s offset change: "
1566 "%lx versus %lx\n", name, ctm.ctm_offset, offset);
1567 return 1;
1568 }
1569 return 0;
1570 }
1571
1572 static int
1573 membadd (const char *name, ctf_id_t type, unsigned long offset, void *arg)
1574 {
1575 ctf_bundle_t *ctb = arg;
1576 ctf_dmdef_t *dmd;
1577 char *s = NULL;
1578
1579 if ((dmd = ctf_alloc (sizeof (ctf_dmdef_t))) == NULL)
1580 return (ctf_set_errno (ctb->ctb_file, EAGAIN));
1581
1582 if (name != NULL && (s = ctf_strdup (name)) == NULL)
1583 {
1584 ctf_free (dmd);
1585 return (ctf_set_errno (ctb->ctb_file, EAGAIN));
1586 }
1587
1588 /* For now, dmd_type is copied as the src_fp's type; it is reset to an
1589 equivalent dst_fp type by a final loop in ctf_add_type(), below. */
1590 dmd->dmd_name = s;
1591 dmd->dmd_type = type;
1592 dmd->dmd_offset = offset;
1593 dmd->dmd_value = -1;
1594
1595 ctf_list_append (&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
1596
1597 ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
1598 return 0;
1599 }
1600
1601 /* The ctf_add_type routine is used to copy a type from a source CTF container
1602 to a dynamic destination container. This routine operates recursively by
1603 following the source type's links and embedded member types. If the
1604 destination container already contains a named type which has the same
1605 attributes, then we succeed and return this type but no changes occur. */
1606 ctf_id_t
1607 ctf_add_type (ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
1608 {
1609 ctf_id_t dst_type = CTF_ERR;
1610 uint32_t dst_kind = CTF_K_UNKNOWN;
1611 ctf_id_t tmp;
1612
1613 const char *name;
1614 uint32_t kind, forward_kind, flag, vlen;
1615
1616 const ctf_type_t *src_tp, *dst_tp;
1617 ctf_bundle_t src, dst;
1618 ctf_encoding_t src_en, dst_en;
1619 ctf_arinfo_t src_ar, dst_ar;
1620
1621 ctf_dtdef_t *dtd;
1622 ctf_funcinfo_t ctc;
1623
1624 ctf_id_t orig_src_type = src_type;
1625
1626 if (!(dst_fp->ctf_flags & LCTF_RDWR))
1627 return (ctf_set_errno (dst_fp, ECTF_RDONLY));
1628
1629 if ((src_tp = ctf_lookup_by_id (&src_fp, src_type)) == NULL)
1630 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
1631
1632 if ((ctf_type_resolve (src_fp, src_type) == CTF_ERR)
1633 && (ctf_errno (src_fp) == ECTF_NONREPRESENTABLE))
1634 return (ctf_set_errno (dst_fp, ECTF_NONREPRESENTABLE));
1635
1636 name = ctf_strptr (src_fp, src_tp->ctt_name);
1637 kind = LCTF_INFO_KIND (src_fp, src_tp->ctt_info);
1638 flag = LCTF_INFO_ISROOT (src_fp, src_tp->ctt_info);
1639 vlen = LCTF_INFO_VLEN (src_fp, src_tp->ctt_info);
1640
1641 forward_kind = kind;
1642 if (kind == CTF_K_FORWARD)
1643 forward_kind = src_tp->ctt_type;
1644
1645 /* If the source type has a name and is a root type (visible at the
1646 top-level scope), lookup the name in the destination container and
1647 verify that it is of the same kind before we do anything else. */
1648
1649 if ((flag & CTF_ADD_ROOT) && name[0] != '\0'
1650 && (tmp = ctf_lookup_by_rawname (dst_fp, forward_kind, name)) != 0)
1651 {
1652 dst_type = tmp;
1653 dst_kind = ctf_type_kind_unsliced (dst_fp, dst_type);
1654 }
1655
1656 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
1657 unless dst_type is a forward declaration and src_type is a struct,
1658 union, or enum (i.e. the definition of the previous forward decl).
1659
1660 We also allow addition in the opposite order (addition of a forward when a
1661 struct, union, or enum already exists), which is a NOP and returns the
1662 already-present struct, union, or enum. */
1663
1664 if (dst_type != CTF_ERR && dst_kind != kind)
1665 {
1666 if (kind == CTF_K_FORWARD
1667 && (dst_kind == CTF_K_ENUM || dst_kind == CTF_K_STRUCT
1668 || dst_kind == CTF_K_UNION))
1669 {
1670 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
1671 return dst_type;
1672 }
1673
1674 if (dst_kind != CTF_K_FORWARD
1675 || (kind != CTF_K_ENUM && kind != CTF_K_STRUCT
1676 && kind != CTF_K_UNION))
1677 {
1678 ctf_dprintf ("Conflict for type %s: kinds differ, new: %i; "
1679 "old (ID %lx): %i\n", name, kind, dst_type, dst_kind);
1680 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1681 }
1682 }
1683
1684 /* We take special action for an integer, float, or slice since it is
1685 described not only by its name but also its encoding. For integers,
1686 bit-fields exploit this degeneracy. */
1687
1688 if (kind == CTF_K_INTEGER || kind == CTF_K_FLOAT || kind == CTF_K_SLICE)
1689 {
1690 if (ctf_type_encoding (src_fp, src_type, &src_en) != 0)
1691 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
1692
1693 if (dst_type != CTF_ERR)
1694 {
1695 ctf_file_t *fp = dst_fp;
1696
1697 if ((dst_tp = ctf_lookup_by_id (&fp, dst_type)) == NULL)
1698 return CTF_ERR;
1699
1700 if (LCTF_INFO_ISROOT (fp, dst_tp->ctt_info) & CTF_ADD_ROOT)
1701 {
1702 /* The type that we found in the hash is also root-visible. If
1703 the two types match then use the existing one; otherwise,
1704 declare a conflict. Note: slices are not certain to match
1705 even if there is no conflict: we must check the contained type
1706 too. */
1707
1708 if (ctf_type_encoding (dst_fp, dst_type, &dst_en) != 0)
1709 return CTF_ERR; /* errno set for us. */
1710
1711 if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0)
1712 {
1713 if (kind != CTF_K_SLICE)
1714 {
1715 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
1716 return dst_type;
1717 }
1718 }
1719 else
1720 {
1721 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1722 }
1723 }
1724 else
1725 {
1726 /* We found a non-root-visible type in the hash. We reset
1727 dst_type to ensure that we continue to look for a possible
1728 conflict in the pending list. */
1729
1730 dst_type = CTF_ERR;
1731 }
1732 }
1733 }
1734
1735 /* If the non-empty name was not found in the appropriate hash, search
1736 the list of pending dynamic definitions that are not yet committed.
1737 If a matching name and kind are found, assume this is the type that
1738 we are looking for. This is necessary to permit ctf_add_type() to
1739 operate recursively on entities such as a struct that contains a
1740 pointer member that refers to the same struct type. */
1741
1742 if (dst_type == CTF_ERR && name[0] != '\0')
1743 {
1744 for (dtd = ctf_list_prev (&dst_fp->ctf_dtdefs); dtd != NULL
1745 && LCTF_TYPE_TO_INDEX (src_fp, dtd->dtd_type) > dst_fp->ctf_dtoldid;
1746 dtd = ctf_list_prev (dtd))
1747 {
1748 const char *ctt_name;
1749
1750 if (LCTF_INFO_KIND (src_fp, dtd->dtd_data.ctt_info) == kind
1751 && dtd->dtd_data.ctt_name
1752 && ((ctt_name = ctf_strraw (src_fp, dtd->dtd_data.ctt_name)) != NULL)
1753 && strcmp (ctt_name, name) == 0)
1754 {
1755 int sroot; /* Is the src root-visible? */
1756 int droot; /* Is the dst root-visible? */
1757 int match; /* Do the encodings match? */
1758
1759 if (kind != CTF_K_INTEGER && kind != CTF_K_FLOAT && kind != CTF_K_SLICE)
1760 {
1761 ctf_add_type_mapping (src_fp, src_type, dst_fp, dtd->dtd_type);
1762 return dtd->dtd_type;
1763 }
1764
1765 sroot = (flag & CTF_ADD_ROOT);
1766 droot = (LCTF_INFO_ISROOT (dst_fp,
1767 dtd->dtd_data.
1768 ctt_info) & CTF_ADD_ROOT);
1769
1770 match = (memcmp (&src_en, &dtd->dtd_u.dtu_enc,
1771 sizeof (ctf_encoding_t)) == 0);
1772
1773 /* If the types share the same encoding then return the id of the
1774 first unless one type is root-visible and the other is not; in
1775 that case the new type must get a new id if a match is never
1776 found. Note: slices are not certain to match even if there is
1777 no conflict: we must check the contained type too. */
1778
1779 if (match && sroot == droot)
1780 {
1781 if (kind != CTF_K_SLICE)
1782 {
1783 ctf_add_type_mapping (src_fp, src_type, dst_fp, dtd->dtd_type);
1784 return dtd->dtd_type;
1785 }
1786 }
1787 else if (!match && sroot && droot)
1788 {
1789 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1790 }
1791 }
1792 }
1793 }
1794
1795 src.ctb_file = src_fp;
1796 src.ctb_type = src_type;
1797 src.ctb_dtd = NULL;
1798
1799 dst.ctb_file = dst_fp;
1800 dst.ctb_type = dst_type;
1801 dst.ctb_dtd = NULL;
1802
1803 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then
1804 we add a new type with the same properties as src_type to dst_fp.
1805 If dst_type is not CTF_ERR, then we verify that dst_type has the
1806 same attributes as src_type. We recurse for embedded references. */
1807 switch (kind)
1808 {
1809 case CTF_K_INTEGER:
1810 /* If we found a match we will have either returned it or declared a
1811 conflict. */
1812 dst_type = ctf_add_integer (dst_fp, flag, name, &src_en);
1813 break;
1814
1815 case CTF_K_FLOAT:
1816 /* If we found a match we will have either returned it or declared a
1817 conflict. */
1818 dst_type = ctf_add_float (dst_fp, flag, name, &src_en);
1819 break;
1820
1821 case CTF_K_SLICE:
1822 /* We have checked for conflicting encodings: now try to add the
1823 contained type. */
1824 src_type = ctf_type_reference (src_fp, src_type);
1825 dst_type = ctf_add_type (dst_fp, src_fp, src_type);
1826
1827 if (src_type == CTF_ERR)
1828 return CTF_ERR; /* errno is set for us. */
1829
1830 dst_type = ctf_add_slice (dst_fp, flag, src_type, &src_en);
1831 break;
1832
1833 case CTF_K_POINTER:
1834 case CTF_K_VOLATILE:
1835 case CTF_K_CONST:
1836 case CTF_K_RESTRICT:
1837 src_type = ctf_type_reference (src_fp, src_type);
1838 src_type = ctf_add_type (dst_fp, src_fp, src_type);
1839
1840 if (src_type == CTF_ERR)
1841 return CTF_ERR; /* errno is set for us. */
1842
1843 dst_type = ctf_add_reftype (dst_fp, flag, src_type, kind);
1844 break;
1845
1846 case CTF_K_ARRAY:
1847 if (ctf_array_info (src_fp, src_type, &src_ar) != 0)
1848 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
1849
1850 src_ar.ctr_contents =
1851 ctf_add_type (dst_fp, src_fp, src_ar.ctr_contents);
1852 src_ar.ctr_index = ctf_add_type (dst_fp, src_fp, src_ar.ctr_index);
1853 src_ar.ctr_nelems = src_ar.ctr_nelems;
1854
1855 if (src_ar.ctr_contents == CTF_ERR || src_ar.ctr_index == CTF_ERR)
1856 return CTF_ERR; /* errno is set for us. */
1857
1858 if (dst_type != CTF_ERR)
1859 {
1860 if (ctf_array_info (dst_fp, dst_type, &dst_ar) != 0)
1861 return CTF_ERR; /* errno is set for us. */
1862
1863 if (memcmp (&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
1864 {
1865 ctf_dprintf ("Conflict for type %s against ID %lx: "
1866 "array info differs, old %lx/%lx/%x; "
1867 "new: %lx/%lx/%x\n", name, dst_type,
1868 src_ar.ctr_contents, src_ar.ctr_index,
1869 src_ar.ctr_nelems, dst_ar.ctr_contents,
1870 dst_ar.ctr_index, dst_ar.ctr_nelems);
1871 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1872 }
1873 }
1874 else
1875 dst_type = ctf_add_array (dst_fp, flag, &src_ar);
1876 break;
1877
1878 case CTF_K_FUNCTION:
1879 ctc.ctc_return = ctf_add_type (dst_fp, src_fp, src_tp->ctt_type);
1880 ctc.ctc_argc = 0;
1881 ctc.ctc_flags = 0;
1882
1883 if (ctc.ctc_return == CTF_ERR)
1884 return CTF_ERR; /* errno is set for us. */
1885
1886 dst_type = ctf_add_function (dst_fp, flag, &ctc, NULL);
1887 break;
1888
1889 case CTF_K_STRUCT:
1890 case CTF_K_UNION:
1891 {
1892 ctf_dmdef_t *dmd;
1893 int errs = 0;
1894 size_t size;
1895 ssize_t ssize;
1896
1897 /* Technically to match a struct or union we need to check both
1898 ways (src members vs. dst, dst members vs. src) but we make
1899 this more optimal by only checking src vs. dst and comparing
1900 the total size of the structure (which we must do anyway)
1901 which covers the possibility of dst members not in src.
1902 This optimization can be defeated for unions, but is so
1903 pathological as to render it irrelevant for our purposes. */
1904
1905 if (dst_type != CTF_ERR && 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 if (ctf_member_iter (src_fp, src_type, membadd, &dst) != 0)
1940 errs++; /* Increment errs and fail at bottom of case. */
1941
1942 if ((ssize = ctf_type_size (src_fp, src_type)) < 0)
1943 return CTF_ERR; /* errno is set for us. */
1944
1945 size = (size_t) ssize;
1946 if (size > CTF_MAX_SIZE)
1947 {
1948 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1949 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
1950 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
1951 }
1952 else
1953 dtd->dtd_data.ctt_size = (uint32_t) size;
1954
1955 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, vlen);
1956
1957 /* Make a final pass through the members changing each dmd_type (a
1958 src_fp type) to an equivalent type in dst_fp. We pass through all
1959 members, leaving any that fail set to CTF_ERR, unless they fail
1960 because they are marking a member of type not representable in this
1961 version of CTF, in which case we just want to silently omit them:
1962 no consumer can do anything with them anyway. */
1963 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
1964 dmd != NULL; dmd = ctf_list_next (dmd))
1965 {
1966 if ((dmd->dmd_type = ctf_add_type (dst_fp, src_fp,
1967 dmd->dmd_type)) == CTF_ERR)
1968 {
1969 if (ctf_errno (dst_fp) != ECTF_NONREPRESENTABLE)
1970 errs++;
1971 }
1972 }
1973
1974 if (errs)
1975 return CTF_ERR; /* errno is set for us. */
1976 break;
1977 }
1978
1979 case CTF_K_ENUM:
1980 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD)
1981 {
1982 if (ctf_enum_iter (src_fp, src_type, enumcmp, &dst)
1983 || ctf_enum_iter (dst_fp, dst_type, enumcmp, &src))
1984 {
1985 ctf_dprintf ("Conflict for enum %s against ID %lx: "
1986 "members differ, see above\n", name, dst_type);
1987 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
1988 }
1989 }
1990 else
1991 {
1992 dst_type = ctf_add_enum (dst_fp, flag, name);
1993 if ((dst.ctb_type = dst_type) == CTF_ERR
1994 || ctf_enum_iter (src_fp, src_type, enumadd, &dst))
1995 return CTF_ERR; /* errno is set for us */
1996 }
1997 break;
1998
1999 case CTF_K_FORWARD:
2000 if (dst_type == CTF_ERR)
2001 dst_type = ctf_add_forward (dst_fp, flag, name, forward_kind);
2002 break;
2003
2004 case CTF_K_TYPEDEF:
2005 src_type = ctf_type_reference (src_fp, src_type);
2006 src_type = ctf_add_type (dst_fp, src_fp, src_type);
2007
2008 if (src_type == CTF_ERR)
2009 return CTF_ERR; /* errno is set for us. */
2010
2011 /* If dst_type is not CTF_ERR at this point, we should check if
2012 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
2013 ECTF_CONFLICT. However, this causes problems with bitness typedefs
2014 that vary based on things like if 32-bit then pid_t is int otherwise
2015 long. We therefore omit this check and assume that if the identically
2016 named typedef already exists in dst_fp, it is correct or
2017 equivalent. */
2018
2019 if (dst_type == CTF_ERR)
2020 {
2021 dst_type = ctf_add_typedef (dst_fp, flag, name, src_type);
2022 }
2023 break;
2024
2025 default:
2026 return (ctf_set_errno (dst_fp, ECTF_CORRUPT));
2027 }
2028
2029 if (dst_type != CTF_ERR)
2030 ctf_add_type_mapping (src_fp, orig_src_type, dst_fp, dst_type);
2031 return dst_type;
2032 }
2033
2034 /* Write the compressed CTF data stream to the specified gzFile descriptor. */
2035 int
2036 ctf_gzwrite (ctf_file_t *fp, gzFile fd)
2037 {
2038 const unsigned char *buf;
2039 ssize_t resid;
2040 ssize_t len;
2041
2042 resid = sizeof (ctf_header_t);
2043 buf = (unsigned char *) fp->ctf_header;
2044 while (resid != 0)
2045 {
2046 if ((len = gzwrite (fd, buf, resid)) <= 0)
2047 return (ctf_set_errno (fp, errno));
2048 resid -= len;
2049 buf += len;
2050 }
2051
2052 resid = fp->ctf_size;
2053 buf = fp->ctf_buf;
2054 while (resid != 0)
2055 {
2056 if ((len = gzwrite (fd, buf, resid)) <= 0)
2057 return (ctf_set_errno (fp, errno));
2058 resid -= len;
2059 buf += len;
2060 }
2061
2062 return 0;
2063 }
2064
2065 /* Compress the specified CTF data stream and write it to the specified file
2066 descriptor. */
2067 int
2068 ctf_compress_write (ctf_file_t *fp, int fd)
2069 {
2070 unsigned char *buf;
2071 unsigned char *bp;
2072 ctf_header_t h;
2073 ctf_header_t *hp = &h;
2074 ssize_t header_len = sizeof (ctf_header_t);
2075 ssize_t compress_len;
2076 ssize_t len;
2077 int rc;
2078 int err = 0;
2079
2080 if (ctf_serialize (fp) < 0)
2081 return -1; /* errno is set for us. */
2082
2083 memcpy (hp, fp->ctf_header, header_len);
2084 hp->cth_flags |= CTF_F_COMPRESS;
2085 compress_len = compressBound (fp->ctf_size);
2086
2087 if ((buf = ctf_alloc (compress_len)) == NULL)
2088 return (ctf_set_errno (fp, ECTF_ZALLOC));
2089
2090 if ((rc = compress (buf, (uLongf *) &compress_len,
2091 fp->ctf_buf, fp->ctf_size)) != Z_OK)
2092 {
2093 ctf_dprintf ("zlib deflate err: %s\n", zError (rc));
2094 err = ctf_set_errno (fp, ECTF_COMPRESS);
2095 goto ret;
2096 }
2097
2098 while (header_len > 0)
2099 {
2100 if ((len = write (fd, hp, header_len)) < 0)
2101 {
2102 err = ctf_set_errno (fp, errno);
2103 goto ret;
2104 }
2105 header_len -= len;
2106 hp += len;
2107 }
2108
2109 bp = buf;
2110 while (compress_len > 0)
2111 {
2112 if ((len = write (fd, bp, compress_len)) < 0)
2113 {
2114 err = ctf_set_errno (fp, errno);
2115 goto ret;
2116 }
2117 compress_len -= len;
2118 bp += len;
2119 }
2120
2121 ret:
2122 ctf_free (buf);
2123 return err;
2124 }
2125
2126 /* Optionally compress the specified CTF data stream and return it as a new
2127 dynamically-allocated string. */
2128 unsigned char *
2129 ctf_write_mem (ctf_file_t *fp, size_t *size, size_t threshold)
2130 {
2131 unsigned char *buf;
2132 unsigned char *bp;
2133 ctf_header_t *hp;
2134 ssize_t header_len = sizeof (ctf_header_t);
2135 ssize_t compress_len;
2136 int rc;
2137
2138 if (ctf_serialize (fp) < 0)
2139 return NULL; /* errno is set for us. */
2140
2141 compress_len = compressBound (fp->ctf_size);
2142 if (fp->ctf_size < threshold)
2143 compress_len = fp->ctf_size;
2144 if ((buf = malloc (compress_len
2145 + sizeof (struct ctf_header))) == NULL)
2146 {
2147 ctf_set_errno (fp, ENOMEM);
2148 return NULL;
2149 }
2150
2151 hp = (ctf_header_t *) buf;
2152 memcpy (hp, fp->ctf_header, header_len);
2153 bp = buf + sizeof (struct ctf_header);
2154 *size = sizeof (struct ctf_header);
2155
2156 if (fp->ctf_size < threshold)
2157 {
2158 hp->cth_flags &= ~CTF_F_COMPRESS;
2159 memcpy (bp, fp->ctf_buf, fp->ctf_size);
2160 *size += fp->ctf_size;
2161 }
2162 else
2163 {
2164 hp->cth_flags |= CTF_F_COMPRESS;
2165 if ((rc = compress (bp, (uLongf *) &compress_len,
2166 fp->ctf_buf, fp->ctf_size)) != Z_OK)
2167 {
2168 ctf_dprintf ("zlib deflate err: %s\n", zError (rc));
2169 ctf_set_errno (fp, ECTF_COMPRESS);
2170 ctf_free (buf);
2171 return NULL;
2172 }
2173 *size += compress_len;
2174 }
2175 return buf;
2176 }
2177
2178 /* Write the uncompressed CTF data stream to the specified file descriptor. */
2179 int
2180 ctf_write (ctf_file_t *fp, int fd)
2181 {
2182 const unsigned char *buf;
2183 ssize_t resid;
2184 ssize_t len;
2185
2186 if (ctf_serialize (fp) < 0)
2187 return -1; /* errno is set for us. */
2188
2189 resid = sizeof (ctf_header_t);
2190 buf = (unsigned char *) fp->ctf_header;
2191 while (resid != 0)
2192 {
2193 if ((len = write (fd, buf, resid)) <= 0)
2194 return (ctf_set_errno (fp, errno));
2195 resid -= len;
2196 buf += len;
2197 }
2198
2199 resid = fp->ctf_size;
2200 buf = fp->ctf_buf;
2201 while (resid != 0)
2202 {
2203 if ((len = write (fd, buf, resid)) <= 0)
2204 return (ctf_set_errno (fp, errno));
2205 resid -= len;
2206 buf += len;
2207 }
2208
2209 return 0;
2210 }
This page took 0.073195 seconds and 3 git commands to generate.