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