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