libctf: error-handling fixes
[deliverable/binutils-gdb.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 #include <elf.h>
28 #include "elf-bfd.h"
29
30 #ifndef EOVERFLOW
31 #define EOVERFLOW ERANGE
32 #endif
33
34 #ifndef roundup
35 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
36 #endif
37
38 /* Make sure the ptrtab has enough space for at least one more type.
39
40 We start with 4KiB of ptrtab, enough for a thousand types, then grow it 25%
41 at a time. */
42
43 static int
44 ctf_grow_ptrtab (ctf_dict_t *fp)
45 {
46 size_t new_ptrtab_len = fp->ctf_ptrtab_len;
47
48 /* We allocate one more ptrtab entry than we need, for the initial zero,
49 plus one because the caller will probably allocate a new type. */
50
51 if (fp->ctf_ptrtab == NULL)
52 new_ptrtab_len = 1024;
53 else if ((fp->ctf_typemax + 2) > fp->ctf_ptrtab_len)
54 new_ptrtab_len = fp->ctf_ptrtab_len * 1.25;
55
56 if (new_ptrtab_len != fp->ctf_ptrtab_len)
57 {
58 uint32_t *new_ptrtab;
59
60 if ((new_ptrtab = realloc (fp->ctf_ptrtab,
61 new_ptrtab_len * sizeof (uint32_t))) == NULL)
62 return (ctf_set_errno (fp, ENOMEM));
63
64 fp->ctf_ptrtab = new_ptrtab;
65 memset (fp->ctf_ptrtab + fp->ctf_ptrtab_len, 0,
66 (new_ptrtab_len - fp->ctf_ptrtab_len) * sizeof (uint32_t));
67 fp->ctf_ptrtab_len = new_ptrtab_len;
68 }
69 return 0;
70 }
71
72 /* To create an empty CTF dict, we just declare a zeroed header and call
73 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new dict r/w and
74 initialize the dynamic members. We start assigning type IDs at 1 because
75 type ID 0 is used as a sentinel and a not-found indicator. */
76
77 ctf_dict_t *
78 ctf_create (int *errp)
79 {
80 static const ctf_header_t hdr = { .cth_preamble = { CTF_MAGIC, CTF_VERSION, 0 } };
81
82 ctf_dynhash_t *dthash;
83 ctf_dynhash_t *dvhash;
84 ctf_dynhash_t *structs = NULL, *unions = NULL, *enums = NULL, *names = NULL;
85 ctf_dynhash_t *objthash = NULL, *funchash = NULL;
86 ctf_sect_t cts;
87 ctf_dict_t *fp;
88
89 libctf_init_debug();
90 dthash = ctf_dynhash_create (ctf_hash_integer, ctf_hash_eq_integer,
91 NULL, NULL);
92 if (dthash == NULL)
93 {
94 ctf_set_open_errno (errp, EAGAIN);
95 goto err;
96 }
97
98 dvhash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
99 NULL, NULL);
100 if (dvhash == NULL)
101 {
102 ctf_set_open_errno (errp, EAGAIN);
103 goto err_dt;
104 }
105
106 structs = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
107 NULL, NULL);
108 unions = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
109 NULL, NULL);
110 enums = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
111 NULL, NULL);
112 names = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
113 NULL, NULL);
114 objthash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
115 free, NULL);
116 funchash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
117 free, NULL);
118 if (!structs || !unions || !enums || !names)
119 {
120 ctf_set_open_errno (errp, EAGAIN);
121 goto err_dv;
122 }
123
124 cts.cts_name = _CTF_SECTION;
125 cts.cts_data = &hdr;
126 cts.cts_size = sizeof (hdr);
127 cts.cts_entsize = 1;
128
129 if ((fp = ctf_bufopen_internal (&cts, NULL, NULL, NULL, 1, errp)) == NULL)
130 goto err_dv;
131
132 fp->ctf_structs.ctn_writable = structs;
133 fp->ctf_unions.ctn_writable = unions;
134 fp->ctf_enums.ctn_writable = enums;
135 fp->ctf_names.ctn_writable = names;
136 fp->ctf_objthash = objthash;
137 fp->ctf_funchash = funchash;
138 fp->ctf_dthash = dthash;
139 fp->ctf_dvhash = dvhash;
140 fp->ctf_dtoldid = 0;
141 fp->ctf_snapshots = 1;
142 fp->ctf_snapshot_lu = 0;
143 fp->ctf_flags |= LCTF_DIRTY;
144
145 ctf_set_ctl_hashes (fp);
146 ctf_setmodel (fp, CTF_MODEL_NATIVE);
147 if (ctf_grow_ptrtab (fp) < 0)
148 {
149 ctf_set_open_errno (errp, ctf_errno (fp));
150 ctf_dict_close (fp);
151 return NULL;
152 }
153
154 return fp;
155
156 err_dv:
157 ctf_dynhash_destroy (structs);
158 ctf_dynhash_destroy (unions);
159 ctf_dynhash_destroy (enums);
160 ctf_dynhash_destroy (names);
161 ctf_dynhash_destroy (objthash);
162 ctf_dynhash_destroy (funchash);
163 ctf_dynhash_destroy (dvhash);
164 err_dt:
165 ctf_dynhash_destroy (dthash);
166 err:
167 return NULL;
168 }
169
170 /* Delete data symbols that have been assigned names from the variable section.
171 Must be called from within ctf_serialize, because that is the only place
172 you can safely delete variables without messing up ctf_rollback. */
173
174 static int
175 symtypetab_delete_nonstatic_vars (ctf_dict_t *fp)
176 {
177 ctf_dvdef_t *dvd, *nvd;
178 ctf_id_t type;
179
180 for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd)
181 {
182 nvd = ctf_list_next (dvd);
183
184 if (((type = (ctf_id_t) (uintptr_t)
185 ctf_dynhash_lookup (fp->ctf_objthash, dvd->dvd_name)) > 0)
186 && type == dvd->dvd_type)
187 ctf_dvd_delete (fp, dvd);
188 }
189
190 return 0;
191 }
192
193 /* Determine if a symbol is "skippable" and should never appear in the
194 symtypetab sections. */
195
196 int
197 ctf_symtab_skippable (ctf_link_sym_t *sym)
198 {
199 /* Never skip symbols whose name is not yet known. */
200 if (sym->st_nameidx_set)
201 return 0;
202
203 return (sym->st_name == NULL || sym->st_name[0] == 0
204 || sym->st_shndx == SHN_UNDEF
205 || strcmp (sym->st_name, "_START_") == 0
206 || strcmp (sym->st_name, "_END_") == 0
207 || (sym->st_type == STT_OBJECT && sym->st_shndx == SHN_EXTABS
208 && sym->st_value == 0));
209 }
210
211 /* Symtypetab emission flags. */
212
213 #define CTF_SYMTYPETAB_EMIT_FUNCTION 0x1
214 #define CTF_SYMTYPETAB_EMIT_PAD 0x2
215 #define CTF_SYMTYPETAB_FORCE_INDEXED 0x4
216
217 /* Get the number of symbols in a symbol hash, the count of symbols, the maximum
218 seen, the eventual size, without any padding elements, of the func/data and
219 (if generated) index sections, and the size of accumulated padding elements.
220 The linker-reported set of symbols is found in SYMFP.
221
222 Also figure out if any symbols need to be moved to the variable section, and
223 add them (if not already present). */
224
225 _libctf_nonnull_
226 static int
227 symtypetab_density (ctf_dict_t *fp, ctf_dict_t *symfp, ctf_dynhash_t *symhash,
228 size_t *count, size_t *max, size_t *unpadsize,
229 size_t *padsize, size_t *idxsize, int flags)
230 {
231 ctf_next_t *i = NULL;
232 const void *name;
233 const void *ctf_sym;
234 ctf_dynhash_t *linker_known = NULL;
235 int err;
236 int beyond_max = 0;
237
238 *count = 0;
239 *max = 0;
240 *unpadsize = 0;
241 *idxsize = 0;
242 *padsize = 0;
243
244 if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
245 {
246 /* Make a dynhash citing only symbols reported by the linker of the
247 appropriate type, then traverse all potential-symbols we know the types
248 of, removing them from linker_known as we go. Once this is done, the
249 only symbols remaining in linker_known are symbols we don't know the
250 types of: we must emit pads for those symbols that are below the
251 maximum symbol we will emit (any beyond that are simply skipped). */
252
253 if ((linker_known = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
254 NULL, NULL)) == NULL)
255 return (ctf_set_errno (fp, ENOMEM));
256
257 while ((err = ctf_dynhash_cnext (symfp->ctf_dynsyms, &i,
258 &name, &ctf_sym)) == 0)
259 {
260 ctf_link_sym_t *sym = (ctf_link_sym_t *) ctf_sym;
261
262 if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
263 && sym->st_type != STT_FUNC)
264 || (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
265 && sym->st_type != STT_OBJECT))
266 continue;
267
268 if (ctf_symtab_skippable (sym))
269 continue;
270
271 /* This should only be true briefly before all the names are
272 finalized, long before we get this far. */
273 if (!ctf_assert (fp, !sym->st_nameidx_set))
274 return -1; /* errno is set for us. */
275
276 if (ctf_dynhash_cinsert (linker_known, name, ctf_sym) < 0)
277 {
278 ctf_dynhash_destroy (linker_known);
279 return (ctf_set_errno (fp, ENOMEM));
280 }
281 }
282 if (err != ECTF_NEXT_END)
283 {
284 ctf_err_warn (fp, 0, err, _("iterating over linker-known symbols during "
285 "serialization"));
286 ctf_dynhash_destroy (linker_known);
287 return (ctf_set_errno (fp, err));
288 }
289 }
290
291 while ((err = ctf_dynhash_cnext (symhash, &i, &name, NULL)) == 0)
292 {
293 ctf_link_sym_t *sym;
294
295 if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
296 {
297 /* Linker did not report symbol in symtab. Remove it from the
298 set of known data symbols and continue. */
299 if ((sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, name)) == NULL)
300 {
301 ctf_dynhash_remove (symhash, name);
302 continue;
303 }
304
305 /* We don't remove skippable symbols from the symhash because we don't
306 want them to be migrated into variables. */
307 if (ctf_symtab_skippable (sym))
308 continue;
309
310 if ((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
311 && sym->st_type != STT_FUNC)
312 {
313 ctf_err_warn (fp, 1, 0, _("Symbol %x added to CTF as a function "
314 "but is of type %x\n"),
315 sym->st_symidx, sym->st_type);
316 ctf_dynhash_remove (symhash, name);
317 continue;
318 }
319 else if (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
320 && sym->st_type != STT_OBJECT)
321 {
322 ctf_err_warn (fp, 1, 0, _("Symbol %x added to CTF as a data "
323 "object but is of type %x\n"),
324 sym->st_symidx, sym->st_type);
325 ctf_dynhash_remove (symhash, name);
326 continue;
327 }
328
329 ctf_dynhash_remove (linker_known, name);
330 }
331 *unpadsize += sizeof (uint32_t);
332 (*count)++;
333
334 if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
335 {
336 if (*max < sym->st_symidx)
337 *max = sym->st_symidx;
338 }
339 else
340 (*max)++;
341 }
342 if (err != ECTF_NEXT_END)
343 {
344 ctf_err_warn (fp, 0, err, _("iterating over CTF symtypetab during "
345 "serialization"));
346 ctf_dynhash_destroy (linker_known);
347 return (ctf_set_errno (fp, err));
348 }
349
350 if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
351 {
352 while ((err = ctf_dynhash_cnext (linker_known, &i, NULL, &ctf_sym)) == 0)
353 {
354 ctf_link_sym_t *sym = (ctf_link_sym_t *) ctf_sym;
355
356 if (sym->st_symidx > *max)
357 beyond_max++;
358 }
359 if (err != ECTF_NEXT_END)
360 {
361 ctf_err_warn (fp, 0, err, _("iterating over linker-known symbols "
362 "during CTF serialization"));
363 ctf_dynhash_destroy (linker_known);
364 return (ctf_set_errno (fp, err));
365 }
366 }
367
368 *idxsize = *count * sizeof (uint32_t);
369 if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
370 *padsize = (ctf_dynhash_elements (linker_known) - beyond_max) * sizeof (uint32_t);
371
372 ctf_dynhash_destroy (linker_known);
373 return 0;
374 }
375
376 /* Emit an objt or func symtypetab into DP in a particular order defined by an
377 array of ctf_link_sym_t or symbol names passed in. The index has NIDX
378 elements in it: unindexed output would terminate at symbol OUTMAX and is in
379 any case no larger than SIZE bytes. Some index elements are expected to be
380 skipped: see symtypetab_density. The linker-reported set of symbols (if any)
381 is found in SYMFP. */
382 static int
383 emit_symtypetab (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp,
384 ctf_link_sym_t **idx, const char **nameidx, uint32_t nidx,
385 uint32_t outmax, int size, int flags)
386 {
387 uint32_t i;
388 uint32_t *dpp = dp;
389 ctf_dynhash_t *symhash;
390
391 ctf_dprintf ("Emitting table of size %i, outmax %u, %u symtypetab entries, "
392 "flags %i\n", size, outmax, nidx, flags);
393
394 /* Empty table? Nothing to do. */
395 if (size == 0)
396 return 0;
397
398 if (flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
399 symhash = fp->ctf_funchash;
400 else
401 symhash = fp->ctf_objthash;
402
403 for (i = 0; i < nidx; i++)
404 {
405 const char *sym_name;
406 void *type;
407
408 /* If we have a linker-reported set of symbols, we may be given that set
409 to work from, or a set of symbol names. In both cases we want to look
410 at the corresponding linker-reported symbol (if any). */
411 if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
412 {
413 ctf_link_sym_t *this_link_sym;
414
415 if (idx)
416 this_link_sym = idx[i];
417 else
418 this_link_sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, nameidx[i]);
419
420 /* Unreported symbol number. No pad, no nothing. */
421 if (!this_link_sym)
422 continue;
423
424 /* Symbol of the wrong type, or skippable? This symbol is not in this
425 table. */
426 if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
427 && this_link_sym->st_type != STT_FUNC)
428 || (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
429 && this_link_sym->st_type != STT_OBJECT))
430 continue;
431
432 if (ctf_symtab_skippable (this_link_sym))
433 continue;
434
435 sym_name = this_link_sym->st_name;
436
437 /* Linker reports symbol of a different type to the symbol we actually
438 added? Skip the symbol. No pad, since the symbol doesn't actually
439 belong in this table at all. (Warned about in
440 symtypetab_density.) */
441 if ((this_link_sym->st_type == STT_FUNC)
442 && (ctf_dynhash_lookup (fp->ctf_objthash, sym_name)))
443 continue;
444
445 if ((this_link_sym->st_type == STT_OBJECT)
446 && (ctf_dynhash_lookup (fp->ctf_funchash, sym_name)))
447 continue;
448 }
449 else
450 sym_name = nameidx[i];
451
452 /* Symbol in index but no type set? Silently skip and (optionally)
453 pad. (In force-indexed mode, this is also where we track symbols of
454 the wrong type for this round of insertion.) */
455 if ((type = ctf_dynhash_lookup (symhash, sym_name)) == NULL)
456 {
457 if (flags & CTF_SYMTYPETAB_EMIT_PAD)
458 *dpp++ = 0;
459 continue;
460 }
461
462 if (!ctf_assert (fp, (((char *) dpp) - (char *) dp) < size))
463 return -1; /* errno is set for us. */
464
465 *dpp++ = (ctf_id_t) (uintptr_t) type;
466
467 /* When emitting unindexed output, all later symbols are pads: stop
468 early. */
469 if ((flags & CTF_SYMTYPETAB_EMIT_PAD) && idx[i]->st_symidx == outmax)
470 break;
471 }
472
473 return 0;
474 }
475
476 /* Emit an objt or func symtypetab index into DP in a paticular order defined by
477 an array of symbol names passed in. Stop at NIDX. The linker-reported set
478 of symbols (if any) is found in SYMFP. */
479 static int
480 emit_symtypetab_index (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp,
481 const char **idx, uint32_t nidx, int size, int flags)
482 {
483 uint32_t i;
484 uint32_t *dpp = dp;
485 ctf_dynhash_t *symhash;
486
487 ctf_dprintf ("Emitting index of size %i, %u entries reported by linker, "
488 "flags %i\n", size, nidx, flags);
489
490 /* Empty table? Nothing to do. */
491 if (size == 0)
492 return 0;
493
494 if (flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
495 symhash = fp->ctf_funchash;
496 else
497 symhash = fp->ctf_objthash;
498
499 /* Indexes should always be unpadded. */
500 if (!ctf_assert (fp, !(flags & CTF_SYMTYPETAB_EMIT_PAD)))
501 return -1; /* errno is set for us. */
502
503 for (i = 0; i < nidx; i++)
504 {
505 const char *sym_name;
506 void *type;
507
508 if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
509 {
510 ctf_link_sym_t *this_link_sym;
511
512 this_link_sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, idx[i]);
513
514 /* This is an index: unreported symbols should never appear in it. */
515 if (!ctf_assert (fp, this_link_sym != NULL))
516 return -1; /* errno is set for us. */
517
518 /* Symbol of the wrong type, or skippable? This symbol is not in this
519 table. */
520 if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
521 && this_link_sym->st_type != STT_FUNC)
522 || (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
523 && this_link_sym->st_type != STT_OBJECT))
524 continue;
525
526 if (ctf_symtab_skippable (this_link_sym))
527 continue;
528
529 sym_name = this_link_sym->st_name;
530
531 /* Linker reports symbol of a different type to the symbol we actually
532 added? Skip the symbol. */
533 if ((this_link_sym->st_type == STT_FUNC)
534 && (ctf_dynhash_lookup (fp->ctf_objthash, sym_name)))
535 continue;
536
537 if ((this_link_sym->st_type == STT_OBJECT)
538 && (ctf_dynhash_lookup (fp->ctf_funchash, sym_name)))
539 continue;
540 }
541 else
542 sym_name = idx[i];
543
544 /* Symbol in index and reported by linker, but no type set? Silently skip
545 and (optionally) pad. (In force-indexed mode, this is also where we
546 track symbols of the wrong type for this round of insertion.) */
547 if ((type = ctf_dynhash_lookup (symhash, sym_name)) == NULL)
548 continue;
549
550 ctf_str_add_ref (fp, sym_name, dpp++);
551
552 if (!ctf_assert (fp, (((char *) dpp) - (char *) dp) <= size))
553 return -1; /* errno is set for us. */
554 }
555
556 return 0;
557 }
558
559 static unsigned char *
560 ctf_copy_smembers (ctf_dict_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
561 {
562 ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
563 ctf_member_t ctm;
564
565 for (; dmd != NULL; dmd = ctf_list_next (dmd))
566 {
567 ctf_member_t *copied;
568
569 ctm.ctm_name = 0;
570 ctm.ctm_type = (uint32_t) dmd->dmd_type;
571 ctm.ctm_offset = (uint32_t) dmd->dmd_offset;
572
573 memcpy (t, &ctm, sizeof (ctm));
574 copied = (ctf_member_t *) t;
575 if (dmd->dmd_name)
576 ctf_str_add_ref (fp, dmd->dmd_name, &copied->ctm_name);
577
578 t += sizeof (ctm);
579 }
580
581 return t;
582 }
583
584 static unsigned char *
585 ctf_copy_lmembers (ctf_dict_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
586 {
587 ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
588 ctf_lmember_t ctlm;
589
590 for (; dmd != NULL; dmd = ctf_list_next (dmd))
591 {
592 ctf_lmember_t *copied;
593
594 ctlm.ctlm_name = 0;
595 ctlm.ctlm_type = (uint32_t) dmd->dmd_type;
596 ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI (dmd->dmd_offset);
597 ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO (dmd->dmd_offset);
598
599 memcpy (t, &ctlm, sizeof (ctlm));
600 copied = (ctf_lmember_t *) t;
601 if (dmd->dmd_name)
602 ctf_str_add_ref (fp, dmd->dmd_name, &copied->ctlm_name);
603
604 t += sizeof (ctlm);
605 }
606
607 return t;
608 }
609
610 static unsigned char *
611 ctf_copy_emembers (ctf_dict_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
612 {
613 ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
614 ctf_enum_t cte;
615
616 for (; dmd != NULL; dmd = ctf_list_next (dmd))
617 {
618 ctf_enum_t *copied;
619
620 cte.cte_value = dmd->dmd_value;
621 memcpy (t, &cte, sizeof (cte));
622 copied = (ctf_enum_t *) t;
623 ctf_str_add_ref (fp, dmd->dmd_name, &copied->cte_name);
624 t += sizeof (cte);
625 }
626
627 return t;
628 }
629
630 /* Sort a newly-constructed static variable array. */
631
632 typedef struct ctf_sort_var_arg_cb
633 {
634 ctf_dict_t *fp;
635 ctf_strs_t *strtab;
636 } ctf_sort_var_arg_cb_t;
637
638 static int
639 ctf_sort_var (const void *one_, const void *two_, void *arg_)
640 {
641 const ctf_varent_t *one = one_;
642 const ctf_varent_t *two = two_;
643 ctf_sort_var_arg_cb_t *arg = arg_;
644
645 return (strcmp (ctf_strraw_explicit (arg->fp, one->ctv_name, arg->strtab),
646 ctf_strraw_explicit (arg->fp, two->ctv_name, arg->strtab)));
647 }
648
649 /* Compatibility: just update the threshold for ctf_discard. */
650 int
651 ctf_update (ctf_dict_t *fp)
652 {
653 if (!(fp->ctf_flags & LCTF_RDWR))
654 return (ctf_set_errno (fp, ECTF_RDONLY));
655
656 fp->ctf_dtoldid = fp->ctf_typemax;
657 return 0;
658 }
659
660 /* If the specified CTF dict is writable and has been modified, reload this dict
661 with the updated type definitions, ready for serialization. In order to make
662 this code and the rest of libctf as simple as possible, we perform updates by
663 taking the dynamic type definitions and creating an in-memory CTF dict
664 containing the definitions, and then call ctf_simple_open_internal() on it.
665 We perform one extra trick here for the benefit of callers and to keep our
666 code simple: ctf_simple_open_internal() will return a new ctf_dict_t, but we
667 want to keep the fp constant for the caller, so after
668 ctf_simple_open_internal() returns, we use memcpy to swap the interior of the
669 old and new ctf_dict_t's, and then free the old. */
670 int
671 ctf_serialize (ctf_dict_t *fp)
672 {
673 ctf_dict_t ofp, *nfp;
674 ctf_header_t hdr, *hdrp;
675 ctf_dtdef_t *dtd;
676 ctf_dvdef_t *dvd;
677 ctf_varent_t *dvarents;
678 ctf_strs_writable_t strtab;
679 ctf_dict_t *symfp = fp;
680
681 unsigned char *t;
682 unsigned long i;
683 int symflags = 0;
684 size_t buf_size, type_size, objt_size, func_size;
685 size_t objt_unpadsize, func_unpadsize, objt_padsize, func_padsize;
686 size_t funcidx_size, objtidx_size;
687 size_t nvars, nfuncs, nobjts, maxobjt, maxfunc;
688 size_t ndynsyms = 0;
689 const char **sym_name_order = NULL;
690 unsigned char *buf = NULL, *newbuf;
691 int err;
692
693 if (!(fp->ctf_flags & LCTF_RDWR))
694 return (ctf_set_errno (fp, ECTF_RDONLY));
695
696 /* Update required? */
697 if (!(fp->ctf_flags & LCTF_DIRTY))
698 return 0;
699
700 /* Fill in an initial CTF header. We will leave the label, object,
701 and function sections empty and only output a header, type section,
702 and string table. The type section begins at a 4-byte aligned
703 boundary past the CTF header itself (at relative offset zero). The flag
704 indicating a new-style function info section (an array of CTF_K_FUNCTION
705 type IDs in the types section) is flipped on. */
706
707 memset (&hdr, 0, sizeof (hdr));
708 hdr.cth_magic = CTF_MAGIC;
709 hdr.cth_version = CTF_VERSION;
710
711 /* This is a new-format func info section, and the symtab and strtab come out
712 of the dynsym and dynstr these days. */
713 hdr.cth_flags = (CTF_F_NEWFUNCINFO | CTF_F_DYNSTR);
714
715 /* Iterate through the dynamic type definition list and compute the
716 size of the CTF type section we will need to generate. */
717
718 for (type_size = 0, dtd = ctf_list_next (&fp->ctf_dtdefs);
719 dtd != NULL; dtd = ctf_list_next (dtd))
720 {
721 uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
722 uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
723
724 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
725 type_size += sizeof (ctf_stype_t);
726 else
727 type_size += sizeof (ctf_type_t);
728
729 switch (kind)
730 {
731 case CTF_K_INTEGER:
732 case CTF_K_FLOAT:
733 type_size += sizeof (uint32_t);
734 break;
735 case CTF_K_ARRAY:
736 type_size += sizeof (ctf_array_t);
737 break;
738 case CTF_K_SLICE:
739 type_size += sizeof (ctf_slice_t);
740 break;
741 case CTF_K_FUNCTION:
742 type_size += sizeof (uint32_t) * (vlen + (vlen & 1));
743 break;
744 case CTF_K_STRUCT:
745 case CTF_K_UNION:
746 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
747 type_size += sizeof (ctf_member_t) * vlen;
748 else
749 type_size += sizeof (ctf_lmember_t) * vlen;
750 break;
751 case CTF_K_ENUM:
752 type_size += sizeof (ctf_enum_t) * vlen;
753 break;
754 }
755 }
756
757 /* Symbol table stuff is done only if the linker has told this dict about
758 potential symbols (usually the case for parent dicts only). The linker
759 will report symbols to the parent dict in a parent/child link, as usual
760 with all linker-related matters. */
761
762 if (!fp->ctf_dynsyms && fp->ctf_parent && fp->ctf_parent->ctf_dynsyms)
763 symfp = fp->ctf_parent;
764
765 /* No linker-reported symbols at all: ctf_link_shuffle_syms was never called.
766 This must be an unsorted, indexed dict. Otherwise, this is a sorted
767 dict, and the header flags indicate as much. */
768 if (!symfp->ctf_dynsyms)
769 symflags = CTF_SYMTYPETAB_FORCE_INDEXED;
770 else
771 hdr.cth_flags |= CTF_F_IDXSORTED;
772
773 /* Work out the sizes of the object and function sections, and work out the
774 number of pad (unassigned) symbols in each, and the overall size of the
775 sections. */
776
777 if (symtypetab_density (fp, symfp, fp->ctf_objthash, &nobjts, &maxobjt,
778 &objt_unpadsize, &objt_padsize, &objtidx_size,
779 symflags) < 0)
780 return -1; /* errno is set for us. */
781
782 ctf_dprintf ("Object symtypetab: %i objects, max %i, unpadded size %i, "
783 "%i bytes of pads, index size %i\n", (int) nobjts, (int) maxobjt,
784 (int) objt_unpadsize, (int) objt_padsize, (int) objtidx_size);
785
786 if (symtypetab_density (fp, symfp, fp->ctf_funchash, &nfuncs, &maxfunc,
787 &func_unpadsize, &func_padsize, &funcidx_size,
788 symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0)
789 return -1; /* errno is set for us. */
790
791 ctf_dprintf ("Function symtypetab: %i functions, max %i, unpadded size %i, "
792 "%i bytes of pads, index size %i\n", (int) nfuncs, (int) maxfunc,
793 (int) func_unpadsize, (int) func_padsize, (int) funcidx_size);
794
795 /* If the linker has reported any symbols at all, those symbols that the
796 linker has not reported are now removed from the ctf_objthash and
797 ctf_funchash. Delete entries from the variable section that duplicate
798 newly-added data symbols. There's no need to migrate new ones in, because
799 linker invocations (even ld -r) can only introduce new symbols, not remove
800 symbols that already exist, and the compiler always emits both a variable
801 and a data symbol simultaneously. */
802
803 if (symtypetab_delete_nonstatic_vars (fp) < 0)
804 return -1;
805
806 /* It is worth indexing each section if it would save space to do so, due to
807 reducing the number of pads sufficiently. A pad is the same size as a
808 single index entry: but index sections compress relatively poorly compared
809 to constant pads, so it takes a lot of contiguous padding to equal one
810 index section entry. It would be nice to be able to *verify* whether we
811 would save space after compression rather than guessing, but this seems
812 difficult, since it would require complete reserialization. Regardless, if
813 the linker has not reported any symbols (e.g. if this is not a final link
814 but just an ld -r), we must emit things in indexed fashion just as the
815 compiler does. */
816
817 objt_size = objt_unpadsize;
818 if (!(symflags & CTF_SYMTYPETAB_FORCE_INDEXED)
819 && ((objt_padsize + objt_unpadsize) * CTF_INDEX_PAD_THRESHOLD
820 > objt_padsize))
821 {
822 objt_size += objt_padsize;
823 objtidx_size = 0;
824 }
825
826 func_size = func_unpadsize;
827 if (!(symflags & CTF_SYMTYPETAB_FORCE_INDEXED)
828 && ((func_padsize + func_unpadsize) * CTF_INDEX_PAD_THRESHOLD
829 > func_padsize))
830 {
831 func_size += func_padsize;
832 funcidx_size = 0;
833 }
834
835 /* Computing the number of entries in the CTF variable section is much
836 simpler. */
837
838 for (nvars = 0, dvd = ctf_list_next (&fp->ctf_dvdefs);
839 dvd != NULL; dvd = ctf_list_next (dvd), nvars++);
840
841 /* Compute the size of the CTF buffer we need, sans only the string table,
842 then allocate a new buffer and memcpy the finished header to the start of
843 the buffer. (We will adjust this later with strtab length info.) */
844
845 hdr.cth_lbloff = hdr.cth_objtoff = 0;
846 hdr.cth_funcoff = hdr.cth_objtoff + objt_size;
847 hdr.cth_objtidxoff = hdr.cth_funcoff + func_size;
848 hdr.cth_funcidxoff = hdr.cth_objtidxoff + objtidx_size;
849 hdr.cth_varoff = hdr.cth_funcidxoff + funcidx_size;
850 hdr.cth_typeoff = hdr.cth_varoff + (nvars * sizeof (ctf_varent_t));
851 hdr.cth_stroff = hdr.cth_typeoff + type_size;
852 hdr.cth_strlen = 0;
853
854 buf_size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
855
856 if ((buf = malloc (buf_size)) == NULL)
857 return (ctf_set_errno (fp, EAGAIN));
858
859 memcpy (buf, &hdr, sizeof (ctf_header_t));
860 t = (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_objtoff;
861
862 hdrp = (ctf_header_t *) buf;
863 if ((fp->ctf_flags & LCTF_CHILD) && (fp->ctf_parname != NULL))
864 ctf_str_add_ref (fp, fp->ctf_parname, &hdrp->cth_parname);
865 if (fp->ctf_cuname != NULL)
866 ctf_str_add_ref (fp, fp->ctf_cuname, &hdrp->cth_cuname);
867
868 /* Sort the linker's symbols into name order if need be: if
869 ctf_link_shuffle_syms has not been called at all, just use all the symbols
870 that were added to this dict, and don't bother sorting them since this is
871 probably an ld -r and will likely just be consumed by ld again, with no
872 ctf_lookup_by_symbol()s ever done on it. */
873
874 if ((objtidx_size != 0) || (funcidx_size != 0))
875 {
876 ctf_next_t *i = NULL;
877 void *symname;
878 const char **walk;
879 int err;
880
881 if (symfp->ctf_dynsyms)
882 ndynsyms = ctf_dynhash_elements (symfp->ctf_dynsyms);
883 else
884 ndynsyms = ctf_dynhash_elements (symfp->ctf_objthash)
885 + ctf_dynhash_elements (symfp->ctf_funchash);
886
887 if ((sym_name_order = calloc (ndynsyms, sizeof (const char *))) == NULL)
888 goto oom;
889
890 walk = sym_name_order;
891
892 if (symfp->ctf_dynsyms)
893 {
894 while ((err = ctf_dynhash_next_sorted (symfp->ctf_dynsyms, &i, &symname,
895 NULL, ctf_dynhash_sort_by_name,
896 NULL)) == 0)
897 *walk++ = (const char *) symname;
898 if (err != ECTF_NEXT_END)
899 goto symerr;
900 }
901 else
902 {
903 while ((err = ctf_dynhash_next (symfp->ctf_objthash, &i, &symname,
904 NULL)) == 0)
905 *walk++ = (const char *) symname;
906 if (err != ECTF_NEXT_END)
907 goto symerr;
908
909 while ((err = ctf_dynhash_next (symfp->ctf_funchash, &i, &symname,
910 NULL)) == 0)
911 *walk++ = (const char *) symname;
912 if (err != ECTF_NEXT_END)
913 goto symerr;
914 }
915 }
916
917 /* Emit the object and function sections, and if necessary their indexes.
918 Emission is done in symtab order if there is no index, and in index
919 (name) order otherwise. */
920
921 if ((objtidx_size == 0) && symfp->ctf_dynsymidx)
922 {
923 ctf_dprintf ("Emitting unindexed objt symtypetab\n");
924 if (emit_symtypetab (fp, symfp, (uint32_t *) t, symfp->ctf_dynsymidx,
925 NULL, symfp->ctf_dynsymmax + 1, maxobjt, objt_size,
926 symflags | CTF_SYMTYPETAB_EMIT_PAD) < 0)
927 goto err; /* errno is set for us. */
928 }
929 else
930 {
931 ctf_dprintf ("Emitting indexed objt symtypetab\n");
932 if (emit_symtypetab (fp, symfp, (uint32_t *) t, NULL, sym_name_order,
933 ndynsyms, maxobjt, objt_size, symflags) < 0)
934 goto err; /* errno is set for us. */
935 }
936
937 t += objt_size;
938
939 if ((funcidx_size == 0) && symfp->ctf_dynsymidx)
940 {
941 ctf_dprintf ("Emitting unindexed func symtypetab\n");
942 if (emit_symtypetab (fp, symfp, (uint32_t *) t, symfp->ctf_dynsymidx,
943 NULL, symfp->ctf_dynsymmax + 1, maxfunc,
944 func_size, symflags | CTF_SYMTYPETAB_EMIT_FUNCTION
945 | CTF_SYMTYPETAB_EMIT_PAD) < 0)
946 goto err; /* errno is set for us. */
947 }
948 else
949 {
950 ctf_dprintf ("Emitting indexed func symtypetab\n");
951 if (emit_symtypetab (fp, symfp, (uint32_t *) t, NULL, sym_name_order,
952 ndynsyms, maxfunc, func_size,
953 symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0)
954 goto err; /* errno is set for us. */
955 }
956
957 t += func_size;
958
959 if (objtidx_size > 0)
960 if (emit_symtypetab_index (fp, symfp, (uint32_t *) t, sym_name_order,
961 ndynsyms, objtidx_size, symflags) < 0)
962 goto err;
963
964 t += objtidx_size;
965
966 if (funcidx_size > 0)
967 if (emit_symtypetab_index (fp, symfp, (uint32_t *) t, sym_name_order,
968 ndynsyms, funcidx_size,
969 symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0)
970 goto err;
971
972 t += funcidx_size;
973 free (sym_name_order);
974 sym_name_order = NULL;
975
976 /* Work over the variable list, translating everything into ctf_varent_t's and
977 prepping the string table. */
978
979 dvarents = (ctf_varent_t *) t;
980 for (i = 0, dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL;
981 dvd = ctf_list_next (dvd), i++)
982 {
983 ctf_varent_t *var = &dvarents[i];
984
985 ctf_str_add_ref (fp, dvd->dvd_name, &var->ctv_name);
986 var->ctv_type = (uint32_t) dvd->dvd_type;
987 }
988 assert (i == nvars);
989
990 t += sizeof (ctf_varent_t) * nvars;
991
992 assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_typeoff);
993
994 /* We now take a final lap through the dynamic type definition list and copy
995 the appropriate type records to the output buffer, noting down the
996 strings as we go. */
997
998 for (dtd = ctf_list_next (&fp->ctf_dtdefs);
999 dtd != NULL; dtd = ctf_list_next (dtd))
1000 {
1001 uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1002 uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
1003
1004 ctf_array_t cta;
1005 uint32_t encoding;
1006 size_t len;
1007 ctf_stype_t *copied;
1008 const char *name;
1009
1010 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
1011 len = sizeof (ctf_stype_t);
1012 else
1013 len = sizeof (ctf_type_t);
1014
1015 memcpy (t, &dtd->dtd_data, len);
1016 copied = (ctf_stype_t *) t; /* name is at the start: constant offset. */
1017 if (copied->ctt_name
1018 && (name = ctf_strraw (fp, copied->ctt_name)) != NULL)
1019 ctf_str_add_ref (fp, name, &copied->ctt_name);
1020 t += len;
1021
1022 switch (kind)
1023 {
1024 case CTF_K_INTEGER:
1025 case CTF_K_FLOAT:
1026 if (kind == CTF_K_INTEGER)
1027 {
1028 encoding = CTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format,
1029 dtd->dtd_u.dtu_enc.cte_offset,
1030 dtd->dtd_u.dtu_enc.cte_bits);
1031 }
1032 else
1033 {
1034 encoding = CTF_FP_DATA (dtd->dtd_u.dtu_enc.cte_format,
1035 dtd->dtd_u.dtu_enc.cte_offset,
1036 dtd->dtd_u.dtu_enc.cte_bits);
1037 }
1038 memcpy (t, &encoding, sizeof (encoding));
1039 t += sizeof (encoding);
1040 break;
1041
1042 case CTF_K_SLICE:
1043 memcpy (t, &dtd->dtd_u.dtu_slice, sizeof (struct ctf_slice));
1044 t += sizeof (struct ctf_slice);
1045 break;
1046
1047 case CTF_K_ARRAY:
1048 cta.cta_contents = (uint32_t) dtd->dtd_u.dtu_arr.ctr_contents;
1049 cta.cta_index = (uint32_t) dtd->dtd_u.dtu_arr.ctr_index;
1050 cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
1051 memcpy (t, &cta, sizeof (cta));
1052 t += sizeof (cta);
1053 break;
1054
1055 case CTF_K_FUNCTION:
1056 {
1057 uint32_t *argv = (uint32_t *) (uintptr_t) t;
1058 uint32_t argc;
1059
1060 for (argc = 0; argc < vlen; argc++)
1061 *argv++ = dtd->dtd_u.dtu_argv[argc];
1062
1063 if (vlen & 1)
1064 *argv++ = 0; /* Pad to 4-byte boundary. */
1065
1066 t = (unsigned char *) argv;
1067 break;
1068 }
1069
1070 case CTF_K_STRUCT:
1071 case CTF_K_UNION:
1072 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
1073 t = ctf_copy_smembers (fp, dtd, t);
1074 else
1075 t = ctf_copy_lmembers (fp, dtd, t);
1076 break;
1077
1078 case CTF_K_ENUM:
1079 t = ctf_copy_emembers (fp, dtd, t);
1080 break;
1081 }
1082 }
1083 assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_stroff);
1084
1085 /* Construct the final string table and fill out all the string refs with the
1086 final offsets. Then purge the refs list, because we're about to move this
1087 strtab onto the end of the buf, invalidating all the offsets. */
1088 strtab = ctf_str_write_strtab (fp);
1089 ctf_str_purge_refs (fp);
1090
1091 if (strtab.cts_strs == NULL)
1092 goto oom;
1093
1094 /* Now the string table is constructed, we can sort the buffer of
1095 ctf_varent_t's. */
1096 ctf_sort_var_arg_cb_t sort_var_arg = { fp, (ctf_strs_t *) &strtab };
1097 ctf_qsort_r (dvarents, nvars, sizeof (ctf_varent_t), ctf_sort_var,
1098 &sort_var_arg);
1099
1100 if ((newbuf = ctf_realloc (fp, buf, buf_size + strtab.cts_len)) == NULL)
1101 {
1102 free (strtab.cts_strs);
1103 goto oom;
1104 }
1105 buf = newbuf;
1106 memcpy (buf + buf_size, strtab.cts_strs, strtab.cts_len);
1107 hdrp = (ctf_header_t *) buf;
1108 hdrp->cth_strlen = strtab.cts_len;
1109 buf_size += hdrp->cth_strlen;
1110 free (strtab.cts_strs);
1111
1112 /* Finally, we are ready to ctf_simple_open() the new dict. If this is
1113 successful, we then switch nfp and fp and free the old dict. */
1114
1115 if ((nfp = ctf_simple_open_internal ((char *) buf, buf_size, NULL, 0,
1116 0, NULL, 0, fp->ctf_syn_ext_strtab,
1117 1, &err)) == NULL)
1118 {
1119 free (buf);
1120 return (ctf_set_errno (fp, err));
1121 }
1122
1123 (void) ctf_setmodel (nfp, ctf_getmodel (fp));
1124
1125 nfp->ctf_parent = fp->ctf_parent;
1126 nfp->ctf_parent_unreffed = fp->ctf_parent_unreffed;
1127 nfp->ctf_refcnt = fp->ctf_refcnt;
1128 nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
1129 if (nfp->ctf_dynbase == NULL)
1130 nfp->ctf_dynbase = buf; /* Make sure buf is freed on close. */
1131 nfp->ctf_dthash = fp->ctf_dthash;
1132 nfp->ctf_dtdefs = fp->ctf_dtdefs;
1133 nfp->ctf_dvhash = fp->ctf_dvhash;
1134 nfp->ctf_dvdefs = fp->ctf_dvdefs;
1135 nfp->ctf_dtoldid = fp->ctf_dtoldid;
1136 nfp->ctf_add_processing = fp->ctf_add_processing;
1137 nfp->ctf_snapshots = fp->ctf_snapshots + 1;
1138 nfp->ctf_specific = fp->ctf_specific;
1139 nfp->ctf_nfuncidx = fp->ctf_nfuncidx;
1140 nfp->ctf_nobjtidx = fp->ctf_nobjtidx;
1141 nfp->ctf_objthash = fp->ctf_objthash;
1142 nfp->ctf_funchash = fp->ctf_funchash;
1143 nfp->ctf_dynsyms = fp->ctf_dynsyms;
1144 nfp->ctf_ptrtab = fp->ctf_ptrtab;
1145 nfp->ctf_dynsymidx = fp->ctf_dynsymidx;
1146 nfp->ctf_dynsymmax = fp->ctf_dynsymmax;
1147 nfp->ctf_ptrtab_len = fp->ctf_ptrtab_len;
1148 nfp->ctf_link_inputs = fp->ctf_link_inputs;
1149 nfp->ctf_link_outputs = fp->ctf_link_outputs;
1150 nfp->ctf_errs_warnings = fp->ctf_errs_warnings;
1151 nfp->ctf_funcidx_names = fp->ctf_funcidx_names;
1152 nfp->ctf_objtidx_names = fp->ctf_objtidx_names;
1153 nfp->ctf_funcidx_sxlate = fp->ctf_funcidx_sxlate;
1154 nfp->ctf_objtidx_sxlate = fp->ctf_objtidx_sxlate;
1155 nfp->ctf_str_prov_offset = fp->ctf_str_prov_offset;
1156 nfp->ctf_syn_ext_strtab = fp->ctf_syn_ext_strtab;
1157 nfp->ctf_in_flight_dynsyms = fp->ctf_in_flight_dynsyms;
1158 nfp->ctf_link_in_cu_mapping = fp->ctf_link_in_cu_mapping;
1159 nfp->ctf_link_out_cu_mapping = fp->ctf_link_out_cu_mapping;
1160 nfp->ctf_link_type_mapping = fp->ctf_link_type_mapping;
1161 nfp->ctf_link_memb_name_changer = fp->ctf_link_memb_name_changer;
1162 nfp->ctf_link_memb_name_changer_arg = fp->ctf_link_memb_name_changer_arg;
1163 nfp->ctf_link_variable_filter = fp->ctf_link_variable_filter;
1164 nfp->ctf_link_variable_filter_arg = fp->ctf_link_variable_filter_arg;
1165 nfp->ctf_link_flags = fp->ctf_link_flags;
1166 nfp->ctf_dedup_atoms = fp->ctf_dedup_atoms;
1167 nfp->ctf_dedup_atoms_alloc = fp->ctf_dedup_atoms_alloc;
1168 memcpy (&nfp->ctf_dedup, &fp->ctf_dedup, sizeof (fp->ctf_dedup));
1169
1170 nfp->ctf_snapshot_lu = fp->ctf_snapshots;
1171
1172 memcpy (&nfp->ctf_lookups, fp->ctf_lookups, sizeof (fp->ctf_lookups));
1173 nfp->ctf_structs = fp->ctf_structs;
1174 nfp->ctf_unions = fp->ctf_unions;
1175 nfp->ctf_enums = fp->ctf_enums;
1176 nfp->ctf_names = fp->ctf_names;
1177
1178 fp->ctf_dthash = NULL;
1179 ctf_str_free_atoms (nfp);
1180 nfp->ctf_str_atoms = fp->ctf_str_atoms;
1181 nfp->ctf_prov_strtab = fp->ctf_prov_strtab;
1182 fp->ctf_str_atoms = NULL;
1183 fp->ctf_prov_strtab = NULL;
1184 memset (&fp->ctf_dtdefs, 0, sizeof (ctf_list_t));
1185 memset (&fp->ctf_errs_warnings, 0, sizeof (ctf_list_t));
1186 fp->ctf_add_processing = NULL;
1187 fp->ctf_ptrtab = NULL;
1188 fp->ctf_funcidx_names = NULL;
1189 fp->ctf_objtidx_names = NULL;
1190 fp->ctf_funcidx_sxlate = NULL;
1191 fp->ctf_objtidx_sxlate = NULL;
1192 fp->ctf_objthash = NULL;
1193 fp->ctf_funchash = NULL;
1194 fp->ctf_dynsyms = NULL;
1195 fp->ctf_dynsymidx = NULL;
1196 fp->ctf_link_inputs = NULL;
1197 fp->ctf_link_outputs = NULL;
1198 fp->ctf_syn_ext_strtab = NULL;
1199 fp->ctf_link_in_cu_mapping = NULL;
1200 fp->ctf_link_out_cu_mapping = NULL;
1201 fp->ctf_link_type_mapping = NULL;
1202 fp->ctf_dedup_atoms = NULL;
1203 fp->ctf_dedup_atoms_alloc = NULL;
1204 fp->ctf_parent_unreffed = 1;
1205
1206 fp->ctf_dvhash = NULL;
1207 memset (&fp->ctf_dvdefs, 0, sizeof (ctf_list_t));
1208 memset (fp->ctf_lookups, 0, sizeof (fp->ctf_lookups));
1209 memset (&fp->ctf_in_flight_dynsyms, 0, sizeof (fp->ctf_in_flight_dynsyms));
1210 memset (&fp->ctf_dedup, 0, sizeof (fp->ctf_dedup));
1211 fp->ctf_structs.ctn_writable = NULL;
1212 fp->ctf_unions.ctn_writable = NULL;
1213 fp->ctf_enums.ctn_writable = NULL;
1214 fp->ctf_names.ctn_writable = NULL;
1215
1216 memcpy (&ofp, fp, sizeof (ctf_dict_t));
1217 memcpy (fp, nfp, sizeof (ctf_dict_t));
1218 memcpy (nfp, &ofp, sizeof (ctf_dict_t));
1219
1220 nfp->ctf_refcnt = 1; /* Force nfp to be freed. */
1221 ctf_dict_close (nfp);
1222
1223 return 0;
1224
1225 symerr:
1226 ctf_err_warn (fp, 0, err, _("error serializing symtypetabs"));
1227 goto err;
1228 oom:
1229 free (buf);
1230 free (sym_name_order);
1231 return (ctf_set_errno (fp, EAGAIN));
1232 err:
1233 free (buf);
1234 free (sym_name_order);
1235 return -1; /* errno is set for us. */
1236 }
1237
1238 ctf_names_t *
1239 ctf_name_table (ctf_dict_t *fp, int kind)
1240 {
1241 switch (kind)
1242 {
1243 case CTF_K_STRUCT:
1244 return &fp->ctf_structs;
1245 case CTF_K_UNION:
1246 return &fp->ctf_unions;
1247 case CTF_K_ENUM:
1248 return &fp->ctf_enums;
1249 default:
1250 return &fp->ctf_names;
1251 }
1252 }
1253
1254 int
1255 ctf_dtd_insert (ctf_dict_t *fp, ctf_dtdef_t *dtd, int flag, int kind)
1256 {
1257 const char *name;
1258 if (ctf_dynhash_insert (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type,
1259 dtd) < 0)
1260 {
1261 ctf_set_errno (fp, ENOMEM);
1262 return -1;
1263 }
1264
1265 if (flag == CTF_ADD_ROOT && dtd->dtd_data.ctt_name
1266 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL)
1267 {
1268 if (ctf_dynhash_insert (ctf_name_table (fp, kind)->ctn_writable,
1269 (char *) name, (void *) (uintptr_t)
1270 dtd->dtd_type) < 0)
1271 {
1272 ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t)
1273 dtd->dtd_type);
1274 ctf_set_errno (fp, ENOMEM);
1275 return -1;
1276 }
1277 }
1278 ctf_list_append (&fp->ctf_dtdefs, dtd);
1279 return 0;
1280 }
1281
1282 void
1283 ctf_dtd_delete (ctf_dict_t *fp, ctf_dtdef_t *dtd)
1284 {
1285 ctf_dmdef_t *dmd, *nmd;
1286 int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1287 int name_kind = kind;
1288 const char *name;
1289
1290 ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type);
1291
1292 switch (kind)
1293 {
1294 case CTF_K_STRUCT:
1295 case CTF_K_UNION:
1296 case CTF_K_ENUM:
1297 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
1298 dmd != NULL; dmd = nmd)
1299 {
1300 if (dmd->dmd_name != NULL)
1301 free (dmd->dmd_name);
1302 nmd = ctf_list_next (dmd);
1303 free (dmd);
1304 }
1305 break;
1306 case CTF_K_FUNCTION:
1307 free (dtd->dtd_u.dtu_argv);
1308 break;
1309 case CTF_K_FORWARD:
1310 name_kind = dtd->dtd_data.ctt_type;
1311 break;
1312 }
1313
1314 if (dtd->dtd_data.ctt_name
1315 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL
1316 && LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info))
1317 {
1318 ctf_dynhash_remove (ctf_name_table (fp, name_kind)->ctn_writable,
1319 name);
1320 ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
1321 }
1322
1323 ctf_list_delete (&fp->ctf_dtdefs, dtd);
1324 free (dtd);
1325 }
1326
1327 ctf_dtdef_t *
1328 ctf_dtd_lookup (const ctf_dict_t *fp, ctf_id_t type)
1329 {
1330 return (ctf_dtdef_t *)
1331 ctf_dynhash_lookup (fp->ctf_dthash, (void *) (uintptr_t) type);
1332 }
1333
1334 ctf_dtdef_t *
1335 ctf_dynamic_type (const ctf_dict_t *fp, ctf_id_t id)
1336 {
1337 ctf_id_t idx;
1338
1339 if (!(fp->ctf_flags & LCTF_RDWR))
1340 return NULL;
1341
1342 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, id))
1343 fp = fp->ctf_parent;
1344
1345 idx = LCTF_TYPE_TO_INDEX(fp, id);
1346
1347 if ((unsigned long) idx <= fp->ctf_typemax)
1348 return ctf_dtd_lookup (fp, id);
1349 return NULL;
1350 }
1351
1352 int
1353 ctf_dvd_insert (ctf_dict_t *fp, ctf_dvdef_t *dvd)
1354 {
1355 if (ctf_dynhash_insert (fp->ctf_dvhash, dvd->dvd_name, dvd) < 0)
1356 {
1357 ctf_set_errno (fp, ENOMEM);
1358 return -1;
1359 }
1360 ctf_list_append (&fp->ctf_dvdefs, dvd);
1361 return 0;
1362 }
1363
1364 void
1365 ctf_dvd_delete (ctf_dict_t *fp, ctf_dvdef_t *dvd)
1366 {
1367 ctf_dynhash_remove (fp->ctf_dvhash, dvd->dvd_name);
1368 free (dvd->dvd_name);
1369
1370 ctf_list_delete (&fp->ctf_dvdefs, dvd);
1371 free (dvd);
1372 }
1373
1374 ctf_dvdef_t *
1375 ctf_dvd_lookup (const ctf_dict_t *fp, const char *name)
1376 {
1377 return (ctf_dvdef_t *) ctf_dynhash_lookup (fp->ctf_dvhash, name);
1378 }
1379
1380 /* Discard all of the dynamic type definitions and variable definitions that
1381 have been added to the dict since the last call to ctf_update(). We locate
1382 such types by scanning the dtd list and deleting elements that have type IDs
1383 greater than ctf_dtoldid, which is set by ctf_update(), above, and by
1384 scanning the variable list and deleting elements that have update IDs equal
1385 to the current value of the last-update snapshot count (indicating that they
1386 were added after the most recent call to ctf_update()). */
1387 int
1388 ctf_discard (ctf_dict_t *fp)
1389 {
1390 ctf_snapshot_id_t last_update =
1391 { fp->ctf_dtoldid,
1392 fp->ctf_snapshot_lu + 1 };
1393
1394 /* Update required? */
1395 if (!(fp->ctf_flags & LCTF_DIRTY))
1396 return 0;
1397
1398 return (ctf_rollback (fp, last_update));
1399 }
1400
1401 ctf_snapshot_id_t
1402 ctf_snapshot (ctf_dict_t *fp)
1403 {
1404 ctf_snapshot_id_t snapid;
1405 snapid.dtd_id = fp->ctf_typemax;
1406 snapid.snapshot_id = fp->ctf_snapshots++;
1407 return snapid;
1408 }
1409
1410 /* Like ctf_discard(), only discards everything after a particular ID. */
1411 int
1412 ctf_rollback (ctf_dict_t *fp, ctf_snapshot_id_t id)
1413 {
1414 ctf_dtdef_t *dtd, *ntd;
1415 ctf_dvdef_t *dvd, *nvd;
1416
1417 if (!(fp->ctf_flags & LCTF_RDWR))
1418 return (ctf_set_errno (fp, ECTF_RDONLY));
1419
1420 if (fp->ctf_snapshot_lu >= id.snapshot_id)
1421 return (ctf_set_errno (fp, ECTF_OVERROLLBACK));
1422
1423 for (dtd = ctf_list_next (&fp->ctf_dtdefs); dtd != NULL; dtd = ntd)
1424 {
1425 int kind;
1426 const char *name;
1427
1428 ntd = ctf_list_next (dtd);
1429
1430 if (LCTF_TYPE_TO_INDEX (fp, dtd->dtd_type) <= id.dtd_id)
1431 continue;
1432
1433 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1434 if (kind == CTF_K_FORWARD)
1435 kind = dtd->dtd_data.ctt_type;
1436
1437 if (dtd->dtd_data.ctt_name
1438 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL
1439 && LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info))
1440 {
1441 ctf_dynhash_remove (ctf_name_table (fp, kind)->ctn_writable,
1442 name);
1443 ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
1444 }
1445
1446 ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type);
1447 ctf_dtd_delete (fp, dtd);
1448 }
1449
1450 for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd)
1451 {
1452 nvd = ctf_list_next (dvd);
1453
1454 if (dvd->dvd_snapshots <= id.snapshot_id)
1455 continue;
1456
1457 ctf_dvd_delete (fp, dvd);
1458 }
1459
1460 fp->ctf_typemax = id.dtd_id;
1461 fp->ctf_snapshots = id.snapshot_id;
1462
1463 if (fp->ctf_snapshots == fp->ctf_snapshot_lu)
1464 fp->ctf_flags &= ~LCTF_DIRTY;
1465
1466 return 0;
1467 }
1468
1469 static ctf_id_t
1470 ctf_add_generic (ctf_dict_t *fp, uint32_t flag, const char *name, int kind,
1471 ctf_dtdef_t **rp)
1472 {
1473 ctf_dtdef_t *dtd;
1474 ctf_id_t type;
1475
1476 if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
1477 return (ctf_set_errno (fp, EINVAL));
1478
1479 if (!(fp->ctf_flags & LCTF_RDWR))
1480 return (ctf_set_errno (fp, ECTF_RDONLY));
1481
1482 if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) >= CTF_MAX_TYPE)
1483 return (ctf_set_errno (fp, ECTF_FULL));
1484
1485 if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) == (CTF_MAX_PTYPE - 1))
1486 return (ctf_set_errno (fp, ECTF_FULL));
1487
1488 /* Make sure ptrtab always grows to be big enough for all types. */
1489 if (ctf_grow_ptrtab (fp) < 0)
1490 return CTF_ERR; /* errno is set for us. */
1491
1492 if ((dtd = malloc (sizeof (ctf_dtdef_t))) == NULL)
1493 return (ctf_set_errno (fp, EAGAIN));
1494
1495 type = ++fp->ctf_typemax;
1496 type = LCTF_INDEX_TO_TYPE (fp, type, (fp->ctf_flags & LCTF_CHILD));
1497
1498 memset (dtd, 0, sizeof (ctf_dtdef_t));
1499 dtd->dtd_data.ctt_name = ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name);
1500 dtd->dtd_type = type;
1501
1502 if (dtd->dtd_data.ctt_name == 0 && name != NULL && name[0] != '\0')
1503 {
1504 free (dtd);
1505 return (ctf_set_errno (fp, EAGAIN));
1506 }
1507
1508 if (ctf_dtd_insert (fp, dtd, flag, kind) < 0)
1509 {
1510 free (dtd);
1511 return CTF_ERR; /* errno is set for us. */
1512 }
1513 fp->ctf_flags |= LCTF_DIRTY;
1514
1515 *rp = dtd;
1516 return type;
1517 }
1518
1519 /* When encoding integer sizes, we want to convert a byte count in the range
1520 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
1521 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
1522 static size_t
1523 clp2 (size_t x)
1524 {
1525 x--;
1526
1527 x |= (x >> 1);
1528 x |= (x >> 2);
1529 x |= (x >> 4);
1530 x |= (x >> 8);
1531 x |= (x >> 16);
1532
1533 return (x + 1);
1534 }
1535
1536 ctf_id_t
1537 ctf_add_encoded (ctf_dict_t *fp, uint32_t flag,
1538 const char *name, const ctf_encoding_t *ep, uint32_t kind)
1539 {
1540 ctf_dtdef_t *dtd;
1541 ctf_id_t type;
1542
1543 if (ep == NULL)
1544 return (ctf_set_errno (fp, EINVAL));
1545
1546 if ((type = ctf_add_generic (fp, flag, name, kind, &dtd)) == CTF_ERR)
1547 return CTF_ERR; /* errno is set for us. */
1548
1549 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
1550 dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
1551 / CHAR_BIT);
1552 dtd->dtd_u.dtu_enc = *ep;
1553
1554 return type;
1555 }
1556
1557 ctf_id_t
1558 ctf_add_reftype (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, uint32_t kind)
1559 {
1560 ctf_dtdef_t *dtd;
1561 ctf_id_t type;
1562 ctf_dict_t *tmp = fp;
1563 int child = fp->ctf_flags & LCTF_CHILD;
1564
1565 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
1566 return (ctf_set_errno (fp, EINVAL));
1567
1568 if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL)
1569 return CTF_ERR; /* errno is set for us. */
1570
1571 if ((type = ctf_add_generic (fp, flag, NULL, kind, &dtd)) == CTF_ERR)
1572 return CTF_ERR; /* errno is set for us. */
1573
1574 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
1575 dtd->dtd_data.ctt_type = (uint32_t) ref;
1576
1577 if (kind != CTF_K_POINTER)
1578 return type;
1579
1580 /* If we are adding a pointer, update the ptrtab, both the directly pointed-to
1581 type and (if an anonymous typedef node is being pointed at) the type that
1582 points at too. Note that ctf_typemax is at this point one higher than we
1583 want to check against, because it's just been incremented for the addition
1584 of this type. */
1585
1586 uint32_t type_idx = LCTF_TYPE_TO_INDEX (fp, type);
1587 uint32_t ref_idx = LCTF_TYPE_TO_INDEX (fp, ref);
1588
1589 if (LCTF_TYPE_ISCHILD (fp, ref) == child
1590 && ref_idx < fp->ctf_typemax)
1591 {
1592 fp->ctf_ptrtab[ref_idx] = type_idx;
1593
1594 ctf_id_t refref_idx = LCTF_TYPE_TO_INDEX (fp, dtd->dtd_data.ctt_type);
1595
1596 if (tmp == fp
1597 && (LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) == CTF_K_TYPEDEF)
1598 && strcmp (ctf_strptr (fp, dtd->dtd_data.ctt_name), "") == 0
1599 && refref_idx < fp->ctf_typemax)
1600 fp->ctf_ptrtab[refref_idx] = type_idx;
1601 }
1602
1603 return type;
1604 }
1605
1606 ctf_id_t
1607 ctf_add_slice (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref,
1608 const ctf_encoding_t *ep)
1609 {
1610 ctf_dtdef_t *dtd;
1611 ctf_id_t resolved_ref = ref;
1612 ctf_id_t type;
1613 int kind;
1614 const ctf_type_t *tp;
1615 ctf_dict_t *tmp = fp;
1616
1617 if (ep == NULL)
1618 return (ctf_set_errno (fp, EINVAL));
1619
1620 if ((ep->cte_bits > 255) || (ep->cte_offset > 255))
1621 return (ctf_set_errno (fp, ECTF_SLICEOVERFLOW));
1622
1623 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
1624 return (ctf_set_errno (fp, EINVAL));
1625
1626 if (ref != 0 && ((tp = ctf_lookup_by_id (&tmp, ref)) == NULL))
1627 return CTF_ERR; /* errno is set for us. */
1628
1629 /* Make sure we ultimately point to an integral type. We also allow slices to
1630 point to the unimplemented type, for now, because the compiler can emit
1631 such slices, though they're not very much use. */
1632
1633 resolved_ref = ctf_type_resolve_unsliced (tmp, ref);
1634 kind = ctf_type_kind_unsliced (tmp, resolved_ref);
1635
1636 if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) &&
1637 (kind != CTF_K_ENUM)
1638 && (ref != 0))
1639 return (ctf_set_errno (fp, ECTF_NOTINTFP));
1640
1641 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_SLICE, &dtd)) == CTF_ERR)
1642 return CTF_ERR; /* errno is set for us. */
1643
1644 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0);
1645 dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
1646 / CHAR_BIT);
1647 dtd->dtd_u.dtu_slice.cts_type = (uint32_t) ref;
1648 dtd->dtd_u.dtu_slice.cts_bits = ep->cte_bits;
1649 dtd->dtd_u.dtu_slice.cts_offset = ep->cte_offset;
1650
1651 return type;
1652 }
1653
1654 ctf_id_t
1655 ctf_add_integer (ctf_dict_t *fp, uint32_t flag,
1656 const char *name, const ctf_encoding_t *ep)
1657 {
1658 return (ctf_add_encoded (fp, flag, name, ep, CTF_K_INTEGER));
1659 }
1660
1661 ctf_id_t
1662 ctf_add_float (ctf_dict_t *fp, uint32_t flag,
1663 const char *name, const ctf_encoding_t *ep)
1664 {
1665 return (ctf_add_encoded (fp, flag, name, ep, CTF_K_FLOAT));
1666 }
1667
1668 ctf_id_t
1669 ctf_add_pointer (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref)
1670 {
1671 return (ctf_add_reftype (fp, flag, ref, CTF_K_POINTER));
1672 }
1673
1674 ctf_id_t
1675 ctf_add_array (ctf_dict_t *fp, uint32_t flag, const ctf_arinfo_t *arp)
1676 {
1677 ctf_dtdef_t *dtd;
1678 ctf_id_t type;
1679 ctf_dict_t *tmp = fp;
1680
1681 if (arp == NULL)
1682 return (ctf_set_errno (fp, EINVAL));
1683
1684 if (arp->ctr_contents != 0
1685 && ctf_lookup_by_id (&tmp, arp->ctr_contents) == NULL)
1686 return CTF_ERR; /* errno is set for us. */
1687
1688 tmp = fp;
1689 if (ctf_lookup_by_id (&tmp, arp->ctr_index) == NULL)
1690 return CTF_ERR; /* errno is set for us. */
1691
1692 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_ARRAY, &dtd)) == CTF_ERR)
1693 return CTF_ERR; /* errno is set for us. */
1694
1695 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ARRAY, flag, 0);
1696 dtd->dtd_data.ctt_size = 0;
1697 dtd->dtd_u.dtu_arr = *arp;
1698
1699 return type;
1700 }
1701
1702 int
1703 ctf_set_array (ctf_dict_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
1704 {
1705 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type);
1706
1707 if (!(fp->ctf_flags & LCTF_RDWR))
1708 return (ctf_set_errno (fp, ECTF_RDONLY));
1709
1710 if (dtd == NULL
1711 || LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
1712 return (ctf_set_errno (fp, ECTF_BADID));
1713
1714 fp->ctf_flags |= LCTF_DIRTY;
1715 dtd->dtd_u.dtu_arr = *arp;
1716
1717 return 0;
1718 }
1719
1720 ctf_id_t
1721 ctf_add_function (ctf_dict_t *fp, uint32_t flag,
1722 const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
1723 {
1724 ctf_dtdef_t *dtd;
1725 ctf_id_t type;
1726 uint32_t vlen;
1727 uint32_t *vdat = NULL;
1728 ctf_dict_t *tmp = fp;
1729 size_t i;
1730
1731 if (!(fp->ctf_flags & LCTF_RDWR))
1732 return (ctf_set_errno (fp, ECTF_RDONLY));
1733
1734 if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0
1735 || (ctc->ctc_argc != 0 && argv == NULL))
1736 return (ctf_set_errno (fp, EINVAL));
1737
1738 vlen = ctc->ctc_argc;
1739 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1740 vlen++; /* Add trailing zero to indicate varargs (see below). */
1741
1742 if (ctc->ctc_return != 0
1743 && ctf_lookup_by_id (&tmp, ctc->ctc_return) == NULL)
1744 return CTF_ERR; /* errno is set for us. */
1745
1746 if (vlen > CTF_MAX_VLEN)
1747 return (ctf_set_errno (fp, EOVERFLOW));
1748
1749 if (vlen != 0 && (vdat = malloc (sizeof (ctf_id_t) * vlen)) == NULL)
1750 return (ctf_set_errno (fp, EAGAIN));
1751
1752 for (i = 0; i < ctc->ctc_argc; i++)
1753 {
1754 tmp = fp;
1755 if (argv[i] != 0 && ctf_lookup_by_id (&tmp, argv[i]) == NULL)
1756 {
1757 free (vdat);
1758 return CTF_ERR; /* errno is set for us. */
1759 }
1760 vdat[i] = (uint32_t) argv[i];
1761 }
1762
1763 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_FUNCTION,
1764 &dtd)) == CTF_ERR)
1765 {
1766 free (vdat);
1767 return CTF_ERR; /* errno is set for us. */
1768 }
1769
1770 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FUNCTION, flag, vlen);
1771 dtd->dtd_data.ctt_type = (uint32_t) ctc->ctc_return;
1772
1773 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1774 vdat[vlen - 1] = 0; /* Add trailing zero to indicate varargs. */
1775 dtd->dtd_u.dtu_argv = vdat;
1776
1777 return type;
1778 }
1779
1780 ctf_id_t
1781 ctf_add_struct_sized (ctf_dict_t *fp, uint32_t flag, const char *name,
1782 size_t size)
1783 {
1784 ctf_dtdef_t *dtd;
1785 ctf_id_t type = 0;
1786
1787 /* Promote root-visible forwards to structs. */
1788 if (name != NULL)
1789 type = ctf_lookup_by_rawname (fp, CTF_K_STRUCT, name);
1790
1791 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1792 dtd = ctf_dtd_lookup (fp, type);
1793 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_STRUCT,
1794 &dtd)) == CTF_ERR)
1795 return CTF_ERR; /* errno is set for us. */
1796
1797 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_STRUCT, flag, 0);
1798
1799 if (size > CTF_MAX_SIZE)
1800 {
1801 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1802 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
1803 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
1804 }
1805 else
1806 dtd->dtd_data.ctt_size = (uint32_t) size;
1807
1808 return type;
1809 }
1810
1811 ctf_id_t
1812 ctf_add_struct (ctf_dict_t *fp, uint32_t flag, const char *name)
1813 {
1814 return (ctf_add_struct_sized (fp, flag, name, 0));
1815 }
1816
1817 ctf_id_t
1818 ctf_add_union_sized (ctf_dict_t *fp, uint32_t flag, const char *name,
1819 size_t size)
1820 {
1821 ctf_dtdef_t *dtd;
1822 ctf_id_t type = 0;
1823
1824 /* Promote root-visible forwards to unions. */
1825 if (name != NULL)
1826 type = ctf_lookup_by_rawname (fp, CTF_K_UNION, name);
1827
1828 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1829 dtd = ctf_dtd_lookup (fp, type);
1830 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_UNION,
1831 &dtd)) == CTF_ERR)
1832 return CTF_ERR; /* errno is set for us */
1833
1834 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_UNION, flag, 0);
1835
1836 if (size > CTF_MAX_SIZE)
1837 {
1838 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1839 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
1840 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
1841 }
1842 else
1843 dtd->dtd_data.ctt_size = (uint32_t) size;
1844
1845 return type;
1846 }
1847
1848 ctf_id_t
1849 ctf_add_union (ctf_dict_t *fp, uint32_t flag, const char *name)
1850 {
1851 return (ctf_add_union_sized (fp, flag, name, 0));
1852 }
1853
1854 ctf_id_t
1855 ctf_add_enum (ctf_dict_t *fp, uint32_t flag, const char *name)
1856 {
1857 ctf_dtdef_t *dtd;
1858 ctf_id_t type = 0;
1859
1860 /* Promote root-visible forwards to enums. */
1861 if (name != NULL)
1862 type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
1863
1864 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
1865 dtd = ctf_dtd_lookup (fp, type);
1866 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_ENUM,
1867 &dtd)) == CTF_ERR)
1868 return CTF_ERR; /* errno is set for us. */
1869
1870 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ENUM, flag, 0);
1871 dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
1872
1873 return type;
1874 }
1875
1876 ctf_id_t
1877 ctf_add_enum_encoded (ctf_dict_t *fp, uint32_t flag, const char *name,
1878 const ctf_encoding_t *ep)
1879 {
1880 ctf_id_t type = 0;
1881
1882 /* First, create the enum if need be, using most of the same machinery as
1883 ctf_add_enum(), to ensure that we do not allow things past that are not
1884 enums or forwards to them. (This includes other slices: you cannot slice a
1885 slice, which would be a useless thing to do anyway.) */
1886
1887 if (name != NULL)
1888 type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
1889
1890 if (type != 0)
1891 {
1892 if ((ctf_type_kind (fp, type) != CTF_K_FORWARD) &&
1893 (ctf_type_kind_unsliced (fp, type) != CTF_K_ENUM))
1894 return (ctf_set_errno (fp, ECTF_NOTINTFP));
1895 }
1896 else if ((type = ctf_add_enum (fp, flag, name)) == CTF_ERR)
1897 return CTF_ERR; /* errno is set for us. */
1898
1899 /* Now attach a suitable slice to it. */
1900
1901 return ctf_add_slice (fp, flag, type, ep);
1902 }
1903
1904 ctf_id_t
1905 ctf_add_forward (ctf_dict_t *fp, uint32_t flag, const char *name,
1906 uint32_t kind)
1907 {
1908 ctf_dtdef_t *dtd;
1909 ctf_id_t type = 0;
1910
1911 if (!ctf_forwardable_kind (kind))
1912 return (ctf_set_errno (fp, ECTF_NOTSUE));
1913
1914 /* If the type is already defined or exists as a forward tag, just
1915 return the ctf_id_t of the existing definition. */
1916
1917 if (name != NULL)
1918 type = ctf_lookup_by_rawname (fp, kind, name);
1919
1920 if (type)
1921 return type;
1922
1923 if ((type = ctf_add_generic (fp, flag, name, kind, &dtd)) == CTF_ERR)
1924 return CTF_ERR; /* errno is set for us. */
1925
1926 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FORWARD, flag, 0);
1927 dtd->dtd_data.ctt_type = kind;
1928
1929 return type;
1930 }
1931
1932 ctf_id_t
1933 ctf_add_typedef (ctf_dict_t *fp, uint32_t flag, const char *name,
1934 ctf_id_t ref)
1935 {
1936 ctf_dtdef_t *dtd;
1937 ctf_id_t type;
1938 ctf_dict_t *tmp = fp;
1939
1940 if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
1941 return (ctf_set_errno (fp, EINVAL));
1942
1943 if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL)
1944 return CTF_ERR; /* errno is set for us. */
1945
1946 if ((type = ctf_add_generic (fp, flag, name, CTF_K_TYPEDEF,
1947 &dtd)) == CTF_ERR)
1948 return CTF_ERR; /* errno is set for us. */
1949
1950 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_TYPEDEF, flag, 0);
1951 dtd->dtd_data.ctt_type = (uint32_t) ref;
1952
1953 return type;
1954 }
1955
1956 ctf_id_t
1957 ctf_add_volatile (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref)
1958 {
1959 return (ctf_add_reftype (fp, flag, ref, CTF_K_VOLATILE));
1960 }
1961
1962 ctf_id_t
1963 ctf_add_const (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref)
1964 {
1965 return (ctf_add_reftype (fp, flag, ref, CTF_K_CONST));
1966 }
1967
1968 ctf_id_t
1969 ctf_add_restrict (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref)
1970 {
1971 return (ctf_add_reftype (fp, flag, ref, CTF_K_RESTRICT));
1972 }
1973
1974 int
1975 ctf_add_enumerator (ctf_dict_t *fp, ctf_id_t enid, const char *name,
1976 int value)
1977 {
1978 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, enid);
1979 ctf_dmdef_t *dmd;
1980
1981 uint32_t kind, vlen, root;
1982 char *s;
1983
1984 if (name == NULL)
1985 return (ctf_set_errno (fp, EINVAL));
1986
1987 if (!(fp->ctf_flags & LCTF_RDWR))
1988 return (ctf_set_errno (fp, ECTF_RDONLY));
1989
1990 if (dtd == NULL)
1991 return (ctf_set_errno (fp, ECTF_BADID));
1992
1993 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
1994 root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info);
1995 vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
1996
1997 if (kind != CTF_K_ENUM)
1998 return (ctf_set_errno (fp, ECTF_NOTENUM));
1999
2000 if (vlen == CTF_MAX_VLEN)
2001 return (ctf_set_errno (fp, ECTF_DTFULL));
2002
2003 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
2004 dmd != NULL; dmd = ctf_list_next (dmd))
2005 {
2006 if (strcmp (dmd->dmd_name, name) == 0)
2007 return (ctf_set_errno (fp, ECTF_DUPLICATE));
2008 }
2009
2010 if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
2011 return (ctf_set_errno (fp, EAGAIN));
2012
2013 if ((s = strdup (name)) == NULL)
2014 {
2015 free (dmd);
2016 return (ctf_set_errno (fp, EAGAIN));
2017 }
2018
2019 dmd->dmd_name = s;
2020 dmd->dmd_type = CTF_ERR;
2021 dmd->dmd_offset = 0;
2022 dmd->dmd_value = value;
2023
2024 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1);
2025 ctf_list_append (&dtd->dtd_u.dtu_members, dmd);
2026
2027 fp->ctf_flags |= LCTF_DIRTY;
2028
2029 return 0;
2030 }
2031
2032 int
2033 ctf_add_member_offset (ctf_dict_t *fp, ctf_id_t souid, const char *name,
2034 ctf_id_t type, unsigned long bit_offset)
2035 {
2036 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, souid);
2037 ctf_dmdef_t *dmd;
2038
2039 ssize_t msize, malign, ssize;
2040 uint32_t kind, vlen, root;
2041 char *s = NULL;
2042
2043 if (!(fp->ctf_flags & LCTF_RDWR))
2044 return (ctf_set_errno (fp, ECTF_RDONLY));
2045
2046 if (dtd == NULL)
2047 return (ctf_set_errno (fp, ECTF_BADID));
2048
2049 if (name != NULL && name[0] == '\0')
2050 name = NULL;
2051
2052 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
2053 root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info);
2054 vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
2055
2056 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
2057 return (ctf_set_errno (fp, ECTF_NOTSOU));
2058
2059 if (vlen == CTF_MAX_VLEN)
2060 return (ctf_set_errno (fp, ECTF_DTFULL));
2061
2062 if (name != NULL)
2063 {
2064 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
2065 dmd != NULL; dmd = ctf_list_next (dmd))
2066 {
2067 if (dmd->dmd_name != NULL && strcmp (dmd->dmd_name, name) == 0)
2068 return (ctf_set_errno (fp, ECTF_DUPLICATE));
2069 }
2070 }
2071
2072 if ((msize = ctf_type_size (fp, type)) < 0 ||
2073 (malign = ctf_type_align (fp, type)) < 0)
2074 {
2075 /* The unimplemented type, and any type that resolves to it, has no size
2076 and no alignment: it can correspond to any number of compiler-inserted
2077 types. */
2078
2079 if (ctf_errno (fp) == ECTF_NONREPRESENTABLE)
2080 {
2081 msize = 0;
2082 malign = 0;
2083 ctf_set_errno (fp, 0);
2084 }
2085 else
2086 return -1; /* errno is set for us. */
2087 }
2088
2089 if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
2090 return (ctf_set_errno (fp, EAGAIN));
2091
2092 if (name != NULL && (s = strdup (name)) == NULL)
2093 {
2094 free (dmd);
2095 return (ctf_set_errno (fp, EAGAIN));
2096 }
2097
2098 dmd->dmd_name = s;
2099 dmd->dmd_type = type;
2100 dmd->dmd_value = -1;
2101
2102 if (kind == CTF_K_STRUCT && vlen != 0)
2103 {
2104 if (bit_offset == (unsigned long) - 1)
2105 {
2106 /* Natural alignment. */
2107
2108 ctf_dmdef_t *lmd = ctf_list_prev (&dtd->dtd_u.dtu_members);
2109 ctf_id_t ltype = ctf_type_resolve (fp, lmd->dmd_type);
2110 size_t off = lmd->dmd_offset;
2111
2112 ctf_encoding_t linfo;
2113 ssize_t lsize;
2114
2115 /* Propagate any error from ctf_type_resolve. If the last member was
2116 of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we
2117 cannot insert right after such a member without explicit offset
2118 specification, because its alignment and size is not known. */
2119 if (ltype == CTF_ERR)
2120 {
2121 free (dmd);
2122 return -1; /* errno is set for us. */
2123 }
2124
2125 if (ctf_type_encoding (fp, ltype, &linfo) == 0)
2126 off += linfo.cte_bits;
2127 else if ((lsize = ctf_type_size (fp, ltype)) > 0)
2128 off += lsize * CHAR_BIT;
2129
2130 /* Round up the offset of the end of the last member to
2131 the next byte boundary, convert 'off' to bytes, and
2132 then round it up again to the next multiple of the
2133 alignment required by the new member. Finally,
2134 convert back to bits and store the result in
2135 dmd_offset. Technically we could do more efficient
2136 packing if the new member is a bit-field, but we're
2137 the "compiler" and ANSI says we can do as we choose. */
2138
2139 off = roundup (off, CHAR_BIT) / CHAR_BIT;
2140 off = roundup (off, MAX (malign, 1));
2141 dmd->dmd_offset = off * CHAR_BIT;
2142 ssize = off + msize;
2143 }
2144 else
2145 {
2146 /* Specified offset in bits. */
2147
2148 dmd->dmd_offset = bit_offset;
2149 ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL);
2150 ssize = MAX (ssize, ((signed) bit_offset / CHAR_BIT) + msize);
2151 }
2152 }
2153 else
2154 {
2155 dmd->dmd_offset = 0;
2156 ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL);
2157 ssize = MAX (ssize, msize);
2158 }
2159
2160 if ((size_t) ssize > CTF_MAX_SIZE)
2161 {
2162 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
2163 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (ssize);
2164 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (ssize);
2165 }
2166 else
2167 dtd->dtd_data.ctt_size = (uint32_t) ssize;
2168
2169 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1);
2170 ctf_list_append (&dtd->dtd_u.dtu_members, dmd);
2171
2172 fp->ctf_flags |= LCTF_DIRTY;
2173 return 0;
2174 }
2175
2176 int
2177 ctf_add_member_encoded (ctf_dict_t *fp, ctf_id_t souid, const char *name,
2178 ctf_id_t type, unsigned long bit_offset,
2179 const ctf_encoding_t encoding)
2180 {
2181 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type);
2182 int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
2183 int otype = type;
2184
2185 if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) && (kind != CTF_K_ENUM))
2186 return (ctf_set_errno (fp, ECTF_NOTINTFP));
2187
2188 if ((type = ctf_add_slice (fp, CTF_ADD_NONROOT, otype, &encoding)) == CTF_ERR)
2189 return -1; /* errno is set for us. */
2190
2191 return ctf_add_member_offset (fp, souid, name, type, bit_offset);
2192 }
2193
2194 int
2195 ctf_add_member (ctf_dict_t *fp, ctf_id_t souid, const char *name,
2196 ctf_id_t type)
2197 {
2198 return ctf_add_member_offset (fp, souid, name, type, (unsigned long) - 1);
2199 }
2200
2201 int
2202 ctf_add_variable (ctf_dict_t *fp, const char *name, ctf_id_t ref)
2203 {
2204 ctf_dvdef_t *dvd;
2205 ctf_dict_t *tmp = fp;
2206
2207 if (!(fp->ctf_flags & LCTF_RDWR))
2208 return (ctf_set_errno (fp, ECTF_RDONLY));
2209
2210 if (ctf_dvd_lookup (fp, name) != NULL)
2211 return (ctf_set_errno (fp, ECTF_DUPLICATE));
2212
2213 if (ctf_lookup_by_id (&tmp, ref) == NULL)
2214 return -1; /* errno is set for us. */
2215
2216 /* Make sure this type is representable. */
2217 if ((ctf_type_resolve (fp, ref) == CTF_ERR)
2218 && (ctf_errno (fp) == ECTF_NONREPRESENTABLE))
2219 return -1;
2220
2221 if ((dvd = malloc (sizeof (ctf_dvdef_t))) == NULL)
2222 return (ctf_set_errno (fp, EAGAIN));
2223
2224 if (name != NULL && (dvd->dvd_name = strdup (name)) == NULL)
2225 {
2226 free (dvd);
2227 return (ctf_set_errno (fp, EAGAIN));
2228 }
2229 dvd->dvd_type = ref;
2230 dvd->dvd_snapshots = fp->ctf_snapshots;
2231
2232 if (ctf_dvd_insert (fp, dvd) < 0)
2233 {
2234 free (dvd->dvd_name);
2235 free (dvd);
2236 return -1; /* errno is set for us. */
2237 }
2238
2239 fp->ctf_flags |= LCTF_DIRTY;
2240 return 0;
2241 }
2242
2243 int
2244 ctf_add_funcobjt_sym (ctf_dict_t *fp, int is_function, const char *name, ctf_id_t id)
2245 {
2246 ctf_dict_t *tmp = fp;
2247 char *dupname;
2248 ctf_dynhash_t *h = is_function ? fp->ctf_funchash : fp->ctf_objthash;
2249
2250 if (!(fp->ctf_flags & LCTF_RDWR))
2251 return (ctf_set_errno (fp, ECTF_RDONLY));
2252
2253 if (ctf_dynhash_lookup (fp->ctf_objthash, name) != NULL ||
2254 ctf_dynhash_lookup (fp->ctf_funchash, name) != NULL)
2255 return (ctf_set_errno (fp, ECTF_DUPLICATE));
2256
2257 if (ctf_lookup_by_id (&tmp, id) == NULL)
2258 return -1; /* errno is set for us. */
2259
2260 if (is_function && ctf_type_kind (fp, id) != CTF_K_FUNCTION)
2261 return (ctf_set_errno (fp, ECTF_NOTFUNC));
2262
2263 if ((dupname = strdup (name)) == NULL)
2264 return (ctf_set_errno (fp, ENOMEM));
2265
2266 if (ctf_dynhash_insert (h, dupname, (void *) (uintptr_t) id) < 0)
2267 {
2268 free (dupname);
2269 return (ctf_set_errno (fp, ENOMEM));
2270 }
2271 return 0;
2272 }
2273
2274 int
2275 ctf_add_objt_sym (ctf_dict_t *fp, const char *name, ctf_id_t id)
2276 {
2277 return (ctf_add_funcobjt_sym (fp, 0, name, id));
2278 }
2279
2280 int
2281 ctf_add_func_sym (ctf_dict_t *fp, const char *name, ctf_id_t id)
2282 {
2283 return (ctf_add_funcobjt_sym (fp, 1, name, id));
2284 }
2285
2286 typedef struct ctf_bundle
2287 {
2288 ctf_dict_t *ctb_dict; /* CTF dict handle. */
2289 ctf_id_t ctb_type; /* CTF type identifier. */
2290 ctf_dtdef_t *ctb_dtd; /* CTF dynamic type definition (if any). */
2291 } ctf_bundle_t;
2292
2293 static int
2294 enumcmp (const char *name, int value, void *arg)
2295 {
2296 ctf_bundle_t *ctb = arg;
2297 int bvalue;
2298
2299 if (ctf_enum_value (ctb->ctb_dict, ctb->ctb_type, name, &bvalue) < 0)
2300 {
2301 ctf_err_warn (ctb->ctb_dict, 0, 0,
2302 _("conflict due to enum %s iteration error"), name);
2303 return 1;
2304 }
2305 if (value != bvalue)
2306 {
2307 ctf_err_warn (ctb->ctb_dict, 1, ECTF_CONFLICT,
2308 _("conflict due to enum value change: %i versus %i"),
2309 value, bvalue);
2310 return 1;
2311 }
2312 return 0;
2313 }
2314
2315 static int
2316 enumadd (const char *name, int value, void *arg)
2317 {
2318 ctf_bundle_t *ctb = arg;
2319
2320 return (ctf_add_enumerator (ctb->ctb_dict, ctb->ctb_type,
2321 name, value) < 0);
2322 }
2323
2324 static int
2325 membcmp (const char *name, ctf_id_t type _libctf_unused_, unsigned long offset,
2326 void *arg)
2327 {
2328 ctf_bundle_t *ctb = arg;
2329 ctf_membinfo_t ctm;
2330
2331 /* Don't check nameless members (e.g. anonymous structs/unions) against each
2332 other. */
2333 if (name[0] == 0)
2334 return 0;
2335
2336 if (ctf_member_info (ctb->ctb_dict, ctb->ctb_type, name, &ctm) < 0)
2337 {
2338 ctf_err_warn (ctb->ctb_dict, 0, 0,
2339 _("conflict due to struct member %s iteration error"),
2340 name);
2341 return 1;
2342 }
2343 if (ctm.ctm_offset != offset)
2344 {
2345 ctf_err_warn (ctb->ctb_dict, 1, ECTF_CONFLICT,
2346 _("conflict due to struct member %s offset change: "
2347 "%lx versus %lx"),
2348 name, ctm.ctm_offset, offset);
2349 return 1;
2350 }
2351 return 0;
2352 }
2353
2354 static int
2355 membadd (const char *name, ctf_id_t type, unsigned long offset, void *arg)
2356 {
2357 ctf_bundle_t *ctb = arg;
2358 ctf_dmdef_t *dmd;
2359 char *s = NULL;
2360
2361 if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
2362 return (ctf_set_errno (ctb->ctb_dict, EAGAIN));
2363
2364 if (name != NULL && (s = strdup (name)) == NULL)
2365 {
2366 free (dmd);
2367 return (ctf_set_errno (ctb->ctb_dict, EAGAIN));
2368 }
2369
2370 /* For now, dmd_type is copied as the src_fp's type; it is reset to an
2371 equivalent dst_fp type by a final loop in ctf_add_type(), below. */
2372 dmd->dmd_name = s;
2373 dmd->dmd_type = type;
2374 dmd->dmd_offset = offset;
2375 dmd->dmd_value = -1;
2376
2377 ctf_list_append (&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
2378
2379 ctb->ctb_dict->ctf_flags |= LCTF_DIRTY;
2380 return 0;
2381 }
2382
2383 /* The ctf_add_type routine is used to copy a type from a source CTF dictionary
2384 to a dynamic destination dictionary. This routine operates recursively by
2385 following the source type's links and embedded member types. If the
2386 destination dict already contains a named type which has the same attributes,
2387 then we succeed and return this type but no changes occur. */
2388 static ctf_id_t
2389 ctf_add_type_internal (ctf_dict_t *dst_fp, ctf_dict_t *src_fp, ctf_id_t src_type,
2390 ctf_dict_t *proc_tracking_fp)
2391 {
2392 ctf_id_t dst_type = CTF_ERR;
2393 uint32_t dst_kind = CTF_K_UNKNOWN;
2394 ctf_dict_t *tmp_fp = dst_fp;
2395 ctf_id_t tmp;
2396
2397 const char *name;
2398 uint32_t kind, forward_kind, flag, vlen;
2399
2400 const ctf_type_t *src_tp, *dst_tp;
2401 ctf_bundle_t src, dst;
2402 ctf_encoding_t src_en, dst_en;
2403 ctf_arinfo_t src_ar, dst_ar;
2404
2405 ctf_funcinfo_t ctc;
2406
2407 ctf_id_t orig_src_type = src_type;
2408
2409 if (!(dst_fp->ctf_flags & LCTF_RDWR))
2410 return (ctf_set_errno (dst_fp, ECTF_RDONLY));
2411
2412 if ((src_tp = ctf_lookup_by_id (&src_fp, src_type)) == NULL)
2413 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
2414
2415 if ((ctf_type_resolve (src_fp, src_type) == CTF_ERR)
2416 && (ctf_errno (src_fp) == ECTF_NONREPRESENTABLE))
2417 return (ctf_set_errno (dst_fp, ECTF_NONREPRESENTABLE));
2418
2419 name = ctf_strptr (src_fp, src_tp->ctt_name);
2420 kind = LCTF_INFO_KIND (src_fp, src_tp->ctt_info);
2421 flag = LCTF_INFO_ISROOT (src_fp, src_tp->ctt_info);
2422 vlen = LCTF_INFO_VLEN (src_fp, src_tp->ctt_info);
2423
2424 /* If this is a type we are currently in the middle of adding, hand it
2425 straight back. (This lets us handle self-referential structures without
2426 considering forwards and empty structures the same as their completed
2427 forms.) */
2428
2429 tmp = ctf_type_mapping (src_fp, src_type, &tmp_fp);
2430
2431 if (tmp != 0)
2432 {
2433 if (ctf_dynhash_lookup (proc_tracking_fp->ctf_add_processing,
2434 (void *) (uintptr_t) src_type))
2435 return tmp;
2436
2437 /* If this type has already been added from this dictionary, and is the
2438 same kind and (if a struct or union) has the same number of members,
2439 hand it straight back. */
2440
2441 if (ctf_type_kind_unsliced (tmp_fp, tmp) == (int) kind)
2442 {
2443 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION
2444 || kind == CTF_K_ENUM)
2445 {
2446 if ((dst_tp = ctf_lookup_by_id (&tmp_fp, dst_type)) != NULL)
2447 if (vlen == LCTF_INFO_VLEN (tmp_fp, dst_tp->ctt_info))
2448 return tmp;
2449 }
2450 else
2451 return tmp;
2452 }
2453 }
2454
2455 forward_kind = kind;
2456 if (kind == CTF_K_FORWARD)
2457 forward_kind = src_tp->ctt_type;
2458
2459 /* If the source type has a name and is a root type (visible at the top-level
2460 scope), lookup the name in the destination dictionary and verify that it is
2461 of the same kind before we do anything else. */
2462
2463 if ((flag & CTF_ADD_ROOT) && name[0] != '\0'
2464 && (tmp = ctf_lookup_by_rawname (dst_fp, forward_kind, name)) != 0)
2465 {
2466 dst_type = tmp;
2467 dst_kind = ctf_type_kind_unsliced (dst_fp, dst_type);
2468 }
2469
2470 /* If an identically named dst_type exists, fail with ECTF_CONFLICT
2471 unless dst_type is a forward declaration and src_type is a struct,
2472 union, or enum (i.e. the definition of the previous forward decl).
2473
2474 We also allow addition in the opposite order (addition of a forward when a
2475 struct, union, or enum already exists), which is a NOP and returns the
2476 already-present struct, union, or enum. */
2477
2478 if (dst_type != CTF_ERR && dst_kind != kind)
2479 {
2480 if (kind == CTF_K_FORWARD
2481 && (dst_kind == CTF_K_ENUM || dst_kind == CTF_K_STRUCT
2482 || dst_kind == CTF_K_UNION))
2483 {
2484 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
2485 return dst_type;
2486 }
2487
2488 if (dst_kind != CTF_K_FORWARD
2489 || (kind != CTF_K_ENUM && kind != CTF_K_STRUCT
2490 && kind != CTF_K_UNION))
2491 {
2492 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT,
2493 _("ctf_add_type: conflict for type %s: "
2494 "kinds differ, new: %i; old (ID %lx): %i"),
2495 name, kind, dst_type, dst_kind);
2496 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
2497 }
2498 }
2499
2500 /* We take special action for an integer, float, or slice since it is
2501 described not only by its name but also its encoding. For integers,
2502 bit-fields exploit this degeneracy. */
2503
2504 if (kind == CTF_K_INTEGER || kind == CTF_K_FLOAT || kind == CTF_K_SLICE)
2505 {
2506 if (ctf_type_encoding (src_fp, src_type, &src_en) != 0)
2507 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
2508
2509 if (dst_type != CTF_ERR)
2510 {
2511 ctf_dict_t *fp = dst_fp;
2512
2513 if ((dst_tp = ctf_lookup_by_id (&fp, dst_type)) == NULL)
2514 return CTF_ERR;
2515
2516 if (ctf_type_encoding (dst_fp, dst_type, &dst_en) != 0)
2517 return CTF_ERR; /* errno set for us. */
2518
2519 if (LCTF_INFO_ISROOT (fp, dst_tp->ctt_info) & CTF_ADD_ROOT)
2520 {
2521 /* The type that we found in the hash is also root-visible. If
2522 the two types match then use the existing one; otherwise,
2523 declare a conflict. Note: slices are not certain to match
2524 even if there is no conflict: we must check the contained type
2525 too. */
2526
2527 if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0)
2528 {
2529 if (kind != CTF_K_SLICE)
2530 {
2531 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
2532 return dst_type;
2533 }
2534 }
2535 else
2536 {
2537 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
2538 }
2539 }
2540 else
2541 {
2542 /* We found a non-root-visible type in the hash. If its encoding
2543 is the same, we can reuse it, unless it is a slice. */
2544
2545 if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0)
2546 {
2547 if (kind != CTF_K_SLICE)
2548 {
2549 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
2550 return dst_type;
2551 }
2552 }
2553 }
2554 }
2555 }
2556
2557 src.ctb_dict = src_fp;
2558 src.ctb_type = src_type;
2559 src.ctb_dtd = NULL;
2560
2561 dst.ctb_dict = dst_fp;
2562 dst.ctb_type = dst_type;
2563 dst.ctb_dtd = NULL;
2564
2565 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add
2566 a new type with the same properties as src_type to dst_fp. If dst_type is
2567 not CTF_ERR, then we verify that dst_type has the same attributes as
2568 src_type. We recurse for embedded references. Before we start, we note
2569 that we are processing this type, to prevent infinite recursion: we do not
2570 re-process any type that appears in this list. The list is emptied
2571 wholesale at the end of processing everything in this recursive stack. */
2572
2573 if (ctf_dynhash_insert (proc_tracking_fp->ctf_add_processing,
2574 (void *) (uintptr_t) src_type, (void *) 1) < 0)
2575 return ctf_set_errno (dst_fp, ENOMEM);
2576
2577 switch (kind)
2578 {
2579 case CTF_K_INTEGER:
2580 /* If we found a match we will have either returned it or declared a
2581 conflict. */
2582 dst_type = ctf_add_integer (dst_fp, flag, name, &src_en);
2583 break;
2584
2585 case CTF_K_FLOAT:
2586 /* If we found a match we will have either returned it or declared a
2587 conflict. */
2588 dst_type = ctf_add_float (dst_fp, flag, name, &src_en);
2589 break;
2590
2591 case CTF_K_SLICE:
2592 /* We have checked for conflicting encodings: now try to add the
2593 contained type. */
2594 src_type = ctf_type_reference (src_fp, src_type);
2595 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type,
2596 proc_tracking_fp);
2597
2598 if (src_type == CTF_ERR)
2599 return CTF_ERR; /* errno is set for us. */
2600
2601 dst_type = ctf_add_slice (dst_fp, flag, src_type, &src_en);
2602 break;
2603
2604 case CTF_K_POINTER:
2605 case CTF_K_VOLATILE:
2606 case CTF_K_CONST:
2607 case CTF_K_RESTRICT:
2608 src_type = ctf_type_reference (src_fp, src_type);
2609 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type,
2610 proc_tracking_fp);
2611
2612 if (src_type == CTF_ERR)
2613 return CTF_ERR; /* errno is set for us. */
2614
2615 dst_type = ctf_add_reftype (dst_fp, flag, src_type, kind);
2616 break;
2617
2618 case CTF_K_ARRAY:
2619 if (ctf_array_info (src_fp, src_type, &src_ar) != 0)
2620 return (ctf_set_errno (dst_fp, ctf_errno (src_fp)));
2621
2622 src_ar.ctr_contents =
2623 ctf_add_type_internal (dst_fp, src_fp, src_ar.ctr_contents,
2624 proc_tracking_fp);
2625 src_ar.ctr_index = ctf_add_type_internal (dst_fp, src_fp,
2626 src_ar.ctr_index,
2627 proc_tracking_fp);
2628 src_ar.ctr_nelems = src_ar.ctr_nelems;
2629
2630 if (src_ar.ctr_contents == CTF_ERR || src_ar.ctr_index == CTF_ERR)
2631 return CTF_ERR; /* errno is set for us. */
2632
2633 if (dst_type != CTF_ERR)
2634 {
2635 if (ctf_array_info (dst_fp, dst_type, &dst_ar) != 0)
2636 return CTF_ERR; /* errno is set for us. */
2637
2638 if (memcmp (&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
2639 {
2640 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT,
2641 _("conflict for type %s against ID %lx: array info "
2642 "differs, old %lx/%lx/%x; new: %lx/%lx/%x"),
2643 name, dst_type, src_ar.ctr_contents,
2644 src_ar.ctr_index, src_ar.ctr_nelems,
2645 dst_ar.ctr_contents, dst_ar.ctr_index,
2646 dst_ar.ctr_nelems);
2647 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
2648 }
2649 }
2650 else
2651 dst_type = ctf_add_array (dst_fp, flag, &src_ar);
2652 break;
2653
2654 case CTF_K_FUNCTION:
2655 ctc.ctc_return = ctf_add_type_internal (dst_fp, src_fp,
2656 src_tp->ctt_type,
2657 proc_tracking_fp);
2658 ctc.ctc_argc = 0;
2659 ctc.ctc_flags = 0;
2660
2661 if (ctc.ctc_return == CTF_ERR)
2662 return CTF_ERR; /* errno is set for us. */
2663
2664 dst_type = ctf_add_function (dst_fp, flag, &ctc, NULL);
2665 break;
2666
2667 case CTF_K_STRUCT:
2668 case CTF_K_UNION:
2669 {
2670 ctf_dmdef_t *dmd;
2671 int errs = 0;
2672 size_t size;
2673 ssize_t ssize;
2674 ctf_dtdef_t *dtd;
2675
2676 /* Technically to match a struct or union we need to check both
2677 ways (src members vs. dst, dst members vs. src) but we make
2678 this more optimal by only checking src vs. dst and comparing
2679 the total size of the structure (which we must do anyway)
2680 which covers the possibility of dst members not in src.
2681 This optimization can be defeated for unions, but is so
2682 pathological as to render it irrelevant for our purposes. */
2683
2684 if (dst_type != CTF_ERR && kind != CTF_K_FORWARD
2685 && dst_kind != CTF_K_FORWARD)
2686 {
2687 if (ctf_type_size (src_fp, src_type) !=
2688 ctf_type_size (dst_fp, dst_type))
2689 {
2690 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT,
2691 _("conflict for type %s against ID %lx: union "
2692 "size differs, old %li, new %li"), name,
2693 dst_type, (long) ctf_type_size (src_fp, src_type),
2694 (long) ctf_type_size (dst_fp, dst_type));
2695 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
2696 }
2697
2698 if (ctf_member_iter (src_fp, src_type, membcmp, &dst))
2699 {
2700 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT,
2701 _("conflict for type %s against ID %lx: members "
2702 "differ, see above"), name, dst_type);
2703 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
2704 }
2705
2706 break;
2707 }
2708
2709 /* Unlike the other cases, copying structs and unions is done
2710 manually so as to avoid repeated lookups in ctf_add_member
2711 and to ensure the exact same member offsets as in src_type. */
2712
2713 dst_type = ctf_add_generic (dst_fp, flag, name, kind, &dtd);
2714 if (dst_type == CTF_ERR)
2715 return CTF_ERR; /* errno is set for us. */
2716
2717 dst.ctb_type = dst_type;
2718 dst.ctb_dtd = dtd;
2719
2720 /* Pre-emptively add this struct to the type mapping so that
2721 structures that refer to themselves work. */
2722 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type);
2723
2724 if (ctf_member_iter (src_fp, src_type, membadd, &dst) != 0)
2725 errs++; /* Increment errs and fail at bottom of case. */
2726
2727 if ((ssize = ctf_type_size (src_fp, src_type)) < 0)
2728 return CTF_ERR; /* errno is set for us. */
2729
2730 size = (size_t) ssize;
2731 if (size > CTF_MAX_SIZE)
2732 {
2733 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
2734 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
2735 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
2736 }
2737 else
2738 dtd->dtd_data.ctt_size = (uint32_t) size;
2739
2740 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, vlen);
2741
2742 /* Make a final pass through the members changing each dmd_type (a
2743 src_fp type) to an equivalent type in dst_fp. We pass through all
2744 members, leaving any that fail set to CTF_ERR, unless they fail
2745 because they are marking a member of type not representable in this
2746 version of CTF, in which case we just want to silently omit them:
2747 no consumer can do anything with them anyway. */
2748 for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
2749 dmd != NULL; dmd = ctf_list_next (dmd))
2750 {
2751 ctf_dict_t *dst = dst_fp;
2752 ctf_id_t memb_type;
2753
2754 memb_type = ctf_type_mapping (src_fp, dmd->dmd_type, &dst);
2755 if (memb_type == 0)
2756 {
2757 if ((dmd->dmd_type =
2758 ctf_add_type_internal (dst_fp, src_fp, dmd->dmd_type,
2759 proc_tracking_fp)) == CTF_ERR)
2760 {
2761 if (ctf_errno (dst_fp) != ECTF_NONREPRESENTABLE)
2762 errs++;
2763 }
2764 }
2765 else
2766 dmd->dmd_type = memb_type;
2767 }
2768
2769 if (errs)
2770 return CTF_ERR; /* errno is set for us. */
2771 break;
2772 }
2773
2774 case CTF_K_ENUM:
2775 if (dst_type != CTF_ERR && kind != CTF_K_FORWARD
2776 && dst_kind != CTF_K_FORWARD)
2777 {
2778 if (ctf_enum_iter (src_fp, src_type, enumcmp, &dst)
2779 || ctf_enum_iter (dst_fp, dst_type, enumcmp, &src))
2780 {
2781 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT,
2782 _("conflict for enum %s against ID %lx: members "
2783 "differ, see above"), name, dst_type);
2784 return (ctf_set_errno (dst_fp, ECTF_CONFLICT));
2785 }
2786 }
2787 else
2788 {
2789 dst_type = ctf_add_enum (dst_fp, flag, name);
2790 if ((dst.ctb_type = dst_type) == CTF_ERR
2791 || ctf_enum_iter (src_fp, src_type, enumadd, &dst))
2792 return CTF_ERR; /* errno is set for us */
2793 }
2794 break;
2795
2796 case CTF_K_FORWARD:
2797 if (dst_type == CTF_ERR)
2798 dst_type = ctf_add_forward (dst_fp, flag, name, forward_kind);
2799 break;
2800
2801 case CTF_K_TYPEDEF:
2802 src_type = ctf_type_reference (src_fp, src_type);
2803 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type,
2804 proc_tracking_fp);
2805
2806 if (src_type == CTF_ERR)
2807 return CTF_ERR; /* errno is set for us. */
2808
2809 /* If dst_type is not CTF_ERR at this point, we should check if
2810 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with
2811 ECTF_CONFLICT. However, this causes problems with bitness typedefs
2812 that vary based on things like if 32-bit then pid_t is int otherwise
2813 long. We therefore omit this check and assume that if the identically
2814 named typedef already exists in dst_fp, it is correct or
2815 equivalent. */
2816
2817 if (dst_type == CTF_ERR)
2818 dst_type = ctf_add_typedef (dst_fp, flag, name, src_type);
2819
2820 break;
2821
2822 default:
2823 return (ctf_set_errno (dst_fp, ECTF_CORRUPT));
2824 }
2825
2826 if (dst_type != CTF_ERR)
2827 ctf_add_type_mapping (src_fp, orig_src_type, dst_fp, dst_type);
2828 return dst_type;
2829 }
2830
2831 ctf_id_t
2832 ctf_add_type (ctf_dict_t *dst_fp, ctf_dict_t *src_fp, ctf_id_t src_type)
2833 {
2834 ctf_id_t id;
2835
2836 if (!src_fp->ctf_add_processing)
2837 src_fp->ctf_add_processing = ctf_dynhash_create (ctf_hash_integer,
2838 ctf_hash_eq_integer,
2839 NULL, NULL);
2840
2841 /* We store the hash on the source, because it contains only source type IDs:
2842 but callers will invariably expect errors to appear on the dest. */
2843 if (!src_fp->ctf_add_processing)
2844 return (ctf_set_errno (dst_fp, ENOMEM));
2845
2846 id = ctf_add_type_internal (dst_fp, src_fp, src_type, src_fp);
2847 ctf_dynhash_empty (src_fp->ctf_add_processing);
2848
2849 return id;
2850 }
2851
2852 /* Write the compressed CTF data stream to the specified gzFile descriptor. */
2853 int
2854 ctf_gzwrite (ctf_dict_t *fp, gzFile fd)
2855 {
2856 const unsigned char *buf;
2857 ssize_t resid;
2858 ssize_t len;
2859
2860 resid = sizeof (ctf_header_t);
2861 buf = (unsigned char *) fp->ctf_header;
2862 while (resid != 0)
2863 {
2864 if ((len = gzwrite (fd, buf, resid)) <= 0)
2865 return (ctf_set_errno (fp, errno));
2866 resid -= len;
2867 buf += len;
2868 }
2869
2870 resid = fp->ctf_size;
2871 buf = fp->ctf_buf;
2872 while (resid != 0)
2873 {
2874 if ((len = gzwrite (fd, buf, resid)) <= 0)
2875 return (ctf_set_errno (fp, errno));
2876 resid -= len;
2877 buf += len;
2878 }
2879
2880 return 0;
2881 }
2882
2883 /* Compress the specified CTF data stream and write it to the specified file
2884 descriptor. */
2885 int
2886 ctf_compress_write (ctf_dict_t *fp, int fd)
2887 {
2888 unsigned char *buf;
2889 unsigned char *bp;
2890 ctf_header_t h;
2891 ctf_header_t *hp = &h;
2892 ssize_t header_len = sizeof (ctf_header_t);
2893 ssize_t compress_len;
2894 ssize_t len;
2895 int rc;
2896 int err = 0;
2897
2898 if (ctf_serialize (fp) < 0)
2899 return -1; /* errno is set for us. */
2900
2901 memcpy (hp, fp->ctf_header, header_len);
2902 hp->cth_flags |= CTF_F_COMPRESS;
2903 compress_len = compressBound (fp->ctf_size);
2904
2905 if ((buf = malloc (compress_len)) == NULL)
2906 {
2907 ctf_err_warn (fp, 0, 0, _("ctf_compress_write: cannot allocate %li bytes"),
2908 (unsigned long) compress_len);
2909 return (ctf_set_errno (fp, ECTF_ZALLOC));
2910 }
2911
2912 if ((rc = compress (buf, (uLongf *) &compress_len,
2913 fp->ctf_buf, fp->ctf_size)) != Z_OK)
2914 {
2915 err = ctf_set_errno (fp, ECTF_COMPRESS);
2916 ctf_err_warn (fp, 0, 0, _("zlib deflate err: %s"), zError (rc));
2917 goto ret;
2918 }
2919
2920 while (header_len > 0)
2921 {
2922 if ((len = write (fd, hp, header_len)) < 0)
2923 {
2924 err = ctf_set_errno (fp, errno);
2925 ctf_err_warn (fp, 0, 0, _("ctf_compress_write: error writing header"));
2926 goto ret;
2927 }
2928 header_len -= len;
2929 hp += len;
2930 }
2931
2932 bp = buf;
2933 while (compress_len > 0)
2934 {
2935 if ((len = write (fd, bp, compress_len)) < 0)
2936 {
2937 err = ctf_set_errno (fp, errno);
2938 ctf_err_warn (fp, 0, 0, _("ctf_compress_write: error writing"));
2939 goto ret;
2940 }
2941 compress_len -= len;
2942 bp += len;
2943 }
2944
2945 ret:
2946 free (buf);
2947 return err;
2948 }
2949
2950 /* Optionally compress the specified CTF data stream and return it as a new
2951 dynamically-allocated string. */
2952 unsigned char *
2953 ctf_write_mem (ctf_dict_t *fp, size_t *size, size_t threshold)
2954 {
2955 unsigned char *buf;
2956 unsigned char *bp;
2957 ctf_header_t *hp;
2958 ssize_t header_len = sizeof (ctf_header_t);
2959 ssize_t compress_len;
2960 int rc;
2961
2962 if (ctf_serialize (fp) < 0)
2963 return NULL; /* errno is set for us. */
2964
2965 compress_len = compressBound (fp->ctf_size);
2966 if (fp->ctf_size < threshold)
2967 compress_len = fp->ctf_size;
2968 if ((buf = malloc (compress_len
2969 + sizeof (struct ctf_header))) == NULL)
2970 {
2971 ctf_set_errno (fp, ENOMEM);
2972 ctf_err_warn (fp, 0, 0, _("ctf_write_mem: cannot allocate %li bytes"),
2973 (unsigned long) (compress_len + sizeof (struct ctf_header)));
2974 return NULL;
2975 }
2976
2977 hp = (ctf_header_t *) buf;
2978 memcpy (hp, fp->ctf_header, header_len);
2979 bp = buf + sizeof (struct ctf_header);
2980 *size = sizeof (struct ctf_header);
2981
2982 if (fp->ctf_size < threshold)
2983 {
2984 hp->cth_flags &= ~CTF_F_COMPRESS;
2985 memcpy (bp, fp->ctf_buf, fp->ctf_size);
2986 *size += fp->ctf_size;
2987 }
2988 else
2989 {
2990 hp->cth_flags |= CTF_F_COMPRESS;
2991 if ((rc = compress (bp, (uLongf *) &compress_len,
2992 fp->ctf_buf, fp->ctf_size)) != Z_OK)
2993 {
2994 ctf_set_errno (fp, ECTF_COMPRESS);
2995 ctf_err_warn (fp, 0, 0, _("zlib deflate err: %s"), zError (rc));
2996 free (buf);
2997 return NULL;
2998 }
2999 *size += compress_len;
3000 }
3001 return buf;
3002 }
3003
3004 /* Write the uncompressed CTF data stream to the specified file descriptor. */
3005 int
3006 ctf_write (ctf_dict_t *fp, int fd)
3007 {
3008 const unsigned char *buf;
3009 ssize_t resid;
3010 ssize_t len;
3011
3012 if (ctf_serialize (fp) < 0)
3013 return -1; /* errno is set for us. */
3014
3015 resid = sizeof (ctf_header_t);
3016 buf = (unsigned char *) fp->ctf_header;
3017 while (resid != 0)
3018 {
3019 if ((len = write (fd, buf, resid)) <= 0)
3020 {
3021 ctf_err_warn (fp, 0, errno, _("ctf_write: error writing header"));
3022 return (ctf_set_errno (fp, errno));
3023 }
3024 resid -= len;
3025 buf += len;
3026 }
3027
3028 resid = fp->ctf_size;
3029 buf = fp->ctf_buf;
3030 while (resid != 0)
3031 {
3032 if ((len = write (fd, buf, resid)) <= 0)
3033 {
3034 ctf_err_warn (fp, 0, errno, _("ctf_write: error writing"));
3035 return (ctf_set_errno (fp, errno));
3036 }
3037 resid -= len;
3038 buf += len;
3039 }
3040
3041 return 0;
3042 }
This page took 0.087361 seconds and 5 git commands to generate.