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