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