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