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