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