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