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