1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27 /*
28 * Copyright 2020 Joyent, Inc.
29 */
30
31 #include <sys/sysmacros.h>
32 #include <sys/param.h>
33 #include <sys/mman.h>
34 #include <ctf_impl.h>
35 #include <sys/debug.h>
36
37 /*
38 * SSIZE_MAX is not available in the kernel, so we define it here rather than
39 * accidentally inject into headers where it's not wanted.
40 */
41 #ifndef SSIZE_MAX
42 #define SSIZE_MAX (LONG_MAX)
43 #endif
44
45 /*
46 * This static string is used as the template for initially populating a
47 * dynamic container's string table. We always store \0 in the first byte,
48 * and we use the generic string "PARENT" to mark this container's parent
49 * if one is associated with the container using ctf_import().
50 */
51 static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT";
52
53 /*
54 * To create an empty CTF container, we just declare a zeroed header and call
55 * ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w
56 * and initialize the dynamic members. We set dtstrlen to 1 to reserve the
57 * first byte of the string table for a \0 byte, and we start assigning type
58 * IDs at 1 because type ID 0 is used as a sentinel.
59 */
60 ctf_file_t *
61 ctf_create(int *errp)
62 {
63 static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } };
64
65 const ulong_t hashlen = 128;
66 ctf_dtdef_t **hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *));
67 ctf_sect_t cts;
68 ctf_file_t *fp;
69
70 if (hash == NULL)
71 return (ctf_set_open_errno(errp, EAGAIN));
72
73 cts.cts_name = _CTF_SECTION;
74 cts.cts_type = SHT_PROGBITS;
75 cts.cts_flags = 0;
76 cts.cts_data = &hdr;
77 cts.cts_size = sizeof (hdr);
78 cts.cts_entsize = 1;
79 cts.cts_offset = 0;
80
81 if ((fp = ctf_bufopen(&cts, NULL, NULL, errp)) == NULL) {
82 ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *));
83 return (NULL);
84 }
85
86 fp->ctf_flags |= LCTF_RDWR;
87 fp->ctf_dthashlen = hashlen;
88 bzero(hash, hashlen * sizeof (ctf_dtdef_t *));
89 fp->ctf_dthash = hash;
90 fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE);
91 fp->ctf_dtnextid = 1;
92 fp->ctf_dtoldid = 0;
93
94 return (fp);
95 }
96
97 ctf_file_t *
98 ctf_fdcreate(int fd, int *errp)
99 {
100 ctf_file_t *fp;
101 static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } };
102
103 const ulong_t hashlen = 128;
104 ctf_dtdef_t **hash;
105 ctf_sect_t cts;
106
107 if (fd == -1)
108 return (ctf_create(errp));
109
110 hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *));
111
112 if (hash == NULL)
113 return (ctf_set_open_errno(errp, EAGAIN));
114
115 cts.cts_name = _CTF_SECTION;
116 cts.cts_type = SHT_PROGBITS;
117 cts.cts_flags = 0;
118 cts.cts_data = &hdr;
119 cts.cts_size = sizeof (hdr);
120 cts.cts_entsize = 1;
121 cts.cts_offset = 0;
122
123 if ((fp = ctf_fdcreate_int(fd, errp, &cts)) == NULL) {
124 ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *));
125 return (NULL);
126 }
127
128 fp->ctf_flags |= LCTF_RDWR;
129 fp->ctf_dthashlen = hashlen;
130 bzero(hash, hashlen * sizeof (ctf_dtdef_t *));
131 fp->ctf_dthash = hash;
132 fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE);
133 fp->ctf_dtnextid = 1;
134 fp->ctf_dtoldid = 0;
135
136 return (fp);
137 }
138
139 static uchar_t *
140 ctf_copy_smembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
141 {
142 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
143 ctf_member_t ctm;
144
145 for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
146 if (dmd->dmd_name) {
147 ctm.ctm_name = soff;
148 soff += strlen(dmd->dmd_name) + 1;
149 } else
150 ctm.ctm_name = 0;
151
152 ctm.ctm_type = (ushort_t)dmd->dmd_type;
153 ctm.ctm_offset = (ushort_t)dmd->dmd_offset;
154
155 bcopy(&ctm, t, sizeof (ctm));
156 t += sizeof (ctm);
157 }
158
159 return (t);
160 }
161
162 static uchar_t *
163 ctf_copy_lmembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
164 {
165 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
166 ctf_lmember_t ctlm;
167
168 for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
169 if (dmd->dmd_name) {
170 ctlm.ctlm_name = soff;
171 soff += strlen(dmd->dmd_name) + 1;
172 } else
173 ctlm.ctlm_name = 0;
174
175 ctlm.ctlm_type = (ushort_t)dmd->dmd_type;
176 ctlm.ctlm_pad = 0;
177 ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset);
178 ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset);
179
180 bcopy(&ctlm, t, sizeof (ctlm));
181 t += sizeof (ctlm);
182 }
183
184 return (t);
185 }
186
187 static uchar_t *
188 ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
189 {
190 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
191 ctf_enum_t cte;
192
193 for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
194 cte.cte_name = soff;
195 cte.cte_value = dmd->dmd_value;
196 soff += strlen(dmd->dmd_name) + 1;
197 bcopy(&cte, t, sizeof (cte));
198 t += sizeof (cte);
199 }
200
201 return (t);
202 }
203
204 static uchar_t *
205 ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s)
206 {
207 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
208 size_t len;
209
210 for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
211 if (dmd->dmd_name == NULL)
212 continue; /* skip anonymous members */
213 len = strlen(dmd->dmd_name) + 1;
214 bcopy(dmd->dmd_name, s, len);
215 s += len;
216 }
217
218 return (s);
219 }
220
221 /*
222 * Only types of dyanmic CTF containers contain reference counts. These
223 * containers are marked RD/WR. Because of that we basically make this a no-op
224 * for compatability with non-dynamic CTF sections. This is also a no-op for
225 * types which are not dynamic types. It is the responsibility of the caller to
226 * make sure it is a valid type. We help that caller out on debug builds.
227 *
228 * Note that the reference counts are not maintained for types that are not
229 * within this container. In other words if we have a type in a parent, that
230 * will not have its reference count increased. On the flip side, the parent
231 * will not be allowed to remove dynamic types if it has children.
232 */
233 static void
234 ctf_ref_inc(ctf_file_t *fp, ctf_id_t tid)
235 {
236 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
237
238 if (dtd == NULL)
239 return;
240
241 if (!(fp->ctf_flags & LCTF_RDWR))
242 return;
243
244 dtd->dtd_ref++;
245 }
246
247 /*
248 * Just as with ctf_ref_inc, this is a no-op on non-writeable containers and the
249 * caller should ensure that this is already a valid type.
250 */
251 static void
252 ctf_ref_dec(ctf_file_t *fp, ctf_id_t tid)
253 {
254 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
255
256 if (dtd == NULL)
257 return;
258
259 if (!(fp->ctf_flags & LCTF_RDWR))
260 return;
261
262 ASSERT(dtd->dtd_ref >= 1);
263 dtd->dtd_ref--;
264 }
265
266 /*
267 * If the specified CTF container is writable and has been modified, reload
268 * this container with the updated type definitions. In order to make this
269 * code and the rest of libctf as simple as possible, we perform updates by
270 * taking the dynamic type definitions and creating an in-memory CTF file
271 * containing the definitions, and then call ctf_bufopen() on it. This not
272 * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest
273 * of the library code with different lookup paths for static and dynamic
274 * type definitions. We are therefore optimizing greatly for lookup over
275 * update, which we assume will be an uncommon operation. We perform one
276 * extra trick here for the benefit of callers and to keep our code simple:
277 * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp
278 * constant for the caller, so after ctf_bufopen() returns, we use bcopy to
279 * swap the interior of the old and new ctf_file_t's, and then free the old.
280 *
281 * Note that the lists of dynamic types stays around and the resulting container
282 * is still writeable. Furthermore, the reference counts that are on the dtd's
283 * are still valid.
284 */
285 int
286 ctf_update(ctf_file_t *fp)
287 {
288 ctf_file_t ofp, *nfp;
289 ctf_header_t hdr, *bhdr;
290 ctf_dtdef_t *dtd;
291 ctf_dsdef_t *dsd;
292 ctf_dldef_t *dld;
293 ctf_sect_t cts, *symp, *strp;
294
295 uchar_t *s, *s0, *t;
296 ctf_lblent_t *label;
297 uint16_t *obj, *func;
298 size_t size, objsize, funcsize, labelsize, plen;
299 void *buf;
300 int err;
301 ulong_t i;
302 const char *plabel;
303 const char *sname;
304
305 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
306 uintptr_t strbase = (uintptr_t)fp->ctf_strtab.cts_data;
307
308 if (!(fp->ctf_flags & LCTF_RDWR))
309 return (ctf_set_errno(fp, ECTF_RDONLY));
310
311 if (!(fp->ctf_flags & LCTF_DIRTY))
312 return (0); /* no update required */
313
314 /*
315 * Fill in an initial CTF header. We will leave the label, object,
316 * and function sections empty and only output a header, type section,
317 * and string table. The type section begins at a 4-byte aligned
318 * boundary past the CTF header itself (at relative offset zero).
319 */
320 bzero(&hdr, sizeof (hdr));
321 hdr.cth_magic = CTF_MAGIC;
322 hdr.cth_version = CTF_VERSION;
323
324 if (fp->ctf_flags & LCTF_CHILD) {
325 if (fp->ctf_parname == NULL) {
326 plen = 0;
327 hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */
328 plabel = NULL;
329 } else {
330 plen = strlen(fp->ctf_parname) + 1;
331 plabel = ctf_label_topmost(fp->ctf_parent);
332 }
333 } else {
334 plabel = NULL;
335 plen = 0;
336 }
337
338 /*
339 * Iterate over the labels that we have.
340 */
341 for (labelsize = 0, dld = ctf_list_next(&fp->ctf_dldefs);
342 dld != NULL; dld = ctf_list_next(dld))
343 labelsize += sizeof (ctf_lblent_t);
344
345 /*
346 * Iterate through the dynamic type definition list and compute the
347 * size of the CTF type section we will need to generate.
348 */
349 for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs);
350 dtd != NULL; dtd = ctf_list_next(dtd)) {
351
352 uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
353 uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
354
355 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
356 size += sizeof (ctf_stype_t);
357 else
358 size += sizeof (ctf_type_t);
359
360 switch (kind) {
361 case CTF_K_INTEGER:
362 case CTF_K_FLOAT:
363 size += sizeof (uint_t);
364 break;
365 case CTF_K_ARRAY:
366 size += sizeof (ctf_array_t);
367 break;
368 case CTF_K_FUNCTION:
369 size += sizeof (ushort_t) * (vlen + (vlen & 1));
370 break;
371 case CTF_K_STRUCT:
372 case CTF_K_UNION:
373 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
374 size += sizeof (ctf_member_t) * vlen;
375 else
376 size += sizeof (ctf_lmember_t) * vlen;
377 break;
378 case CTF_K_ENUM:
379 size += sizeof (ctf_enum_t) * vlen;
380 break;
381 }
382 }
383
384 /*
385 * An entry for each object must exist in the data section. However, if
386 * the symbol is SHN_UNDEF, then it is skipped. For objects, the storage
387 * is just the size of the 2-byte id. For functions it's always 2 bytes,
388 * plus 2 bytes per argument and the return type.
389 */
390 dsd = ctf_list_next(&fp->ctf_dsdefs);
391 for (objsize = 0, funcsize = 0, i = 0; i < fp->ctf_nsyms; i++) {
392 int type;
393
394 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
395 const Elf32_Sym *symp = (Elf32_Sym *)symbase + i;
396
397 type = ELF32_ST_TYPE(symp->st_info);
398 if (ctf_sym_valid(strbase, type, symp->st_shndx,
399 symp->st_value, symp->st_name) == B_FALSE)
400 continue;
401 } else {
402 const Elf64_Sym *symp = (Elf64_Sym *)symbase + i;
403
404 type = ELF64_ST_TYPE(symp->st_info);
405 if (ctf_sym_valid(strbase, type, symp->st_shndx,
406 symp->st_value, symp->st_name) == B_FALSE)
407 continue;
408 }
409
410 while (dsd != NULL && i > dsd->dsd_symidx)
411 dsd = ctf_list_next(dsd);
412 if (type == STT_OBJECT) {
413 objsize += sizeof (uint16_t);
414 } else {
415 /* Every function has a uint16_t info no matter what */
416 if (dsd == NULL || i < dsd->dsd_symidx) {
417 funcsize += sizeof (uint16_t);
418 } else {
419 funcsize += sizeof (uint16_t) *
420 (dsd->dsd_nargs + 2);
421 }
422 }
423 }
424
425 /*
426 * The objtoff and funcoffset must be 2-byte aligned. We're guaranteed
427 * that this is always true for the objtoff because labels are always 8
428 * bytes large. Similarly, because objects are always two bytes of data,
429 * this will always be true for funcoff.
430 */
431 hdr.cth_objtoff = hdr.cth_lbloff + labelsize;
432 hdr.cth_funcoff = hdr.cth_objtoff + objsize;
433
434 /*
435 * The type offset must be 4 byte aligned.
436 */
437 hdr.cth_typeoff = hdr.cth_funcoff + funcsize;
438 if (hdr.cth_typeoff & 3)
439 hdr.cth_typeoff += 4 - (hdr.cth_typeoff & 3);
440 ASSERT((hdr.cth_typeoff & 3) == 0);
441
442 /*
443 * Fill in the string table offset and size, compute the size of the
444 * entire CTF buffer we need, and then allocate a new buffer and
445 * bcopy the finished header to the start of the buffer.
446 */
447 hdr.cth_stroff = hdr.cth_typeoff + size;
448 hdr.cth_strlen = fp->ctf_dtstrlen + plen;
449 size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
450 ctf_dprintf("lbloff: %u\nobjtoff: %u\nfuncoff: %u\n"
451 "typeoff: %u\nstroff: %u\nstrlen: %u\n",
452 hdr.cth_lbloff, hdr.cth_objtoff, hdr.cth_funcoff,
453 hdr.cth_typeoff, hdr.cth_stroff, hdr.cth_strlen);
454
455 if ((buf = ctf_data_alloc(size)) == MAP_FAILED)
456 return (ctf_set_errno(fp, EAGAIN));
457
458 bcopy(&hdr, buf, sizeof (ctf_header_t));
459 bhdr = buf;
460 label = (ctf_lblent_t *)((uintptr_t)buf + sizeof (ctf_header_t));
461 t = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_typeoff;
462 s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff;
463 obj = (uint16_t *)((uintptr_t)buf + sizeof (ctf_header_t) +
464 hdr.cth_objtoff);
465 func = (uint16_t *)((uintptr_t)buf + sizeof (ctf_header_t) +
466 hdr.cth_funcoff);
467
468 bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE));
469 s += sizeof (_CTF_STRTAB_TEMPLATE);
470
471 /*
472 * We have an actual parent name and we're a child container, therefore
473 * we should make sure to note our parent's name here.
474 */
475 if (plen != 0) {
476 VERIFY(s + plen - s0 <= hdr.cth_strlen);
477 bcopy(fp->ctf_parname, s, plen);
478 bhdr->cth_parname = s - s0;
479 s += plen;
480 }
481
482 /*
483 * First pass over the labels and copy them out.
484 */
485 for (dld = ctf_list_next(&fp->ctf_dldefs); dld != NULL;
486 dld = ctf_list_next(dld), label++) {
487 size_t len = strlen(dld->dld_name) + 1;
488
489 VERIFY(s + len - s0 <= hdr.cth_strlen);
490 bcopy(dld->dld_name, s, len);
491 label->ctl_typeidx = dld->dld_type;
492 label->ctl_label = s - s0;
493 s += len;
494
495 if (plabel != NULL && strcmp(plabel, dld->dld_name) == 0)
496 bhdr->cth_parlabel = label->ctl_label;
497 }
498
499 /*
500 * We now take a final lap through the dynamic type definition list and
501 * copy the appropriate type records and strings to the output buffer.
502 */
503 for (dtd = ctf_list_next(&fp->ctf_dtdefs);
504 dtd != NULL; dtd = ctf_list_next(dtd)) {
505
506 uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
507 uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
508
509 ctf_array_t cta;
510 uint_t encoding;
511 size_t len;
512
513 if (dtd->dtd_name != NULL) {
514 dtd->dtd_data.ctt_name = (uint_t)(s - s0);
515 len = strlen(dtd->dtd_name) + 1;
516 VERIFY(s + len - s0 <= hdr.cth_strlen);
517 bcopy(dtd->dtd_name, s, len);
518 s += len;
519 } else
520 dtd->dtd_data.ctt_name = 0;
521
522 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
523 len = sizeof (ctf_stype_t);
524 else
525 len = sizeof (ctf_type_t);
526
527 bcopy(&dtd->dtd_data, t, len);
528 t += len;
529
530 switch (kind) {
531 case CTF_K_INTEGER:
532 case CTF_K_FLOAT:
533 if (kind == CTF_K_INTEGER) {
534 encoding = CTF_INT_DATA(
535 dtd->dtd_u.dtu_enc.cte_format,
536 dtd->dtd_u.dtu_enc.cte_offset,
537 dtd->dtd_u.dtu_enc.cte_bits);
538 } else {
539 encoding = CTF_FP_DATA(
540 dtd->dtd_u.dtu_enc.cte_format,
541 dtd->dtd_u.dtu_enc.cte_offset,
542 dtd->dtd_u.dtu_enc.cte_bits);
543 }
544 bcopy(&encoding, t, sizeof (encoding));
545 t += sizeof (encoding);
546 break;
547
548 case CTF_K_ARRAY:
549 cta.cta_contents = (ushort_t)
550 dtd->dtd_u.dtu_arr.ctr_contents;
551 cta.cta_index = (ushort_t)
552 dtd->dtd_u.dtu_arr.ctr_index;
553 cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
554 bcopy(&cta, t, sizeof (cta));
555 t += sizeof (cta);
556 break;
557
558 case CTF_K_FUNCTION: {
559 ushort_t *argv = (ushort_t *)(uintptr_t)t;
560 uint_t argc;
561
562 for (argc = 0; argc < vlen; argc++)
563 *argv++ = (ushort_t)dtd->dtd_u.dtu_argv[argc];
564
565 if (vlen & 1)
566 *argv++ = 0; /* pad to 4-byte boundary */
567
568 t = (uchar_t *)argv;
569 break;
570 }
571
572 case CTF_K_STRUCT:
573 case CTF_K_UNION:
574 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
575 t = ctf_copy_smembers(dtd, (uint_t)(s - s0), t);
576 else
577 t = ctf_copy_lmembers(dtd, (uint_t)(s - s0), t);
578 s = ctf_copy_membnames(dtd, s);
579 break;
580
581 case CTF_K_ENUM:
582 t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t);
583 s = ctf_copy_membnames(dtd, s);
584 break;
585 }
586 }
587
588 /*
589 * Now we fill in our dynamic data and function sections. We use the
590 * same criteria as above, but also consult the dsd list.
591 */
592 dsd = ctf_list_next(&fp->ctf_dsdefs);
593 for (i = 0; i < fp->ctf_nsyms; i++) {
594 int type;
595 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
596 const Elf32_Sym *symp = (Elf32_Sym *)symbase + i;
597 type = ELF32_ST_TYPE(symp->st_info);
598
599 if (ctf_sym_valid(strbase, type, symp->st_shndx,
600 symp->st_value, symp->st_name) == B_FALSE)
601 continue;
602 } else {
603 const Elf64_Sym *symp = (Elf64_Sym *)symbase + i;
604 type = ELF64_ST_TYPE(symp->st_info);
605 if (ctf_sym_valid(strbase, type, symp->st_shndx,
606 symp->st_value, symp->st_name) == B_FALSE)
607 continue;
608 }
609
610 while (dsd != NULL && i > dsd->dsd_symidx) {
611 dsd = ctf_list_next(dsd);
612 }
613 if (type == STT_OBJECT) {
614 if (dsd == NULL || i < dsd->dsd_symidx) {
615 *obj = 0;
616 } else {
617 *obj = dsd->dsd_tid;
618 }
619 obj++;
620 VERIFY((uintptr_t)obj <= (uintptr_t)func);
621 } else {
622 if (dsd == NULL || i < dsd->dsd_symidx) {
623 ushort_t data = CTF_TYPE_INFO(CTF_K_UNKNOWN,
624 0, 0);
625 *func = data;
626 func++;
627 } else {
628 int j;
629 ushort_t data = CTF_TYPE_INFO(CTF_K_FUNCTION, 0,
630 dsd->dsd_nargs);
631
632 *func = data;
633 func++;
634 *func = dsd->dsd_tid;
635 func++;
636 for (j = 0; j < dsd->dsd_nargs; j++)
637 func[j] = dsd->dsd_argc[j];
638 func += dsd->dsd_nargs;
639 }
640 }
641 }
642
643 /*
644 * Finally, we are ready to ctf_bufopen() the new container. If this
645 * is successful, we then switch nfp and fp and free the old container.
646 */
647 ctf_data_protect(buf, size);
648 cts.cts_name = _CTF_SECTION;
649 cts.cts_type = SHT_PROGBITS;
650 cts.cts_flags = 0;
651 cts.cts_data = buf;
652 cts.cts_size = size;
653 cts.cts_entsize = 1;
654 cts.cts_offset = 0;
655
656 if (fp->ctf_nsyms == 0) {
657 symp = NULL;
658 strp = NULL;
659 } else {
660 symp = &fp->ctf_symtab;
661 strp = &fp->ctf_strtab;
662 }
663
664 if ((nfp = ctf_bufopen(&cts, symp, strp, &err)) == NULL) {
665 ctf_data_free(buf, size);
666 return (ctf_set_errno(fp, err));
667 }
668
669 (void) ctf_setmodel(nfp, ctf_getmodel(fp));
670 (void) ctf_import(nfp, fp->ctf_parent);
671
672 nfp->ctf_refcnt = fp->ctf_refcnt;
673 nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
674 nfp->ctf_dthash = fp->ctf_dthash;
675 nfp->ctf_dthashlen = fp->ctf_dthashlen;
676 nfp->ctf_dtdefs = fp->ctf_dtdefs;
677 nfp->ctf_dsdefs = fp->ctf_dsdefs;
678 nfp->ctf_dldefs = fp->ctf_dldefs;
679 nfp->ctf_dtstrlen = fp->ctf_dtstrlen;
680 nfp->ctf_dtnextid = fp->ctf_dtnextid;
681 nfp->ctf_dtoldid = fp->ctf_dtnextid - 1;
682 nfp->ctf_specific = fp->ctf_specific;
683
684 fp->ctf_dthash = NULL;
685 fp->ctf_dthashlen = 0;
686 bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t));
687 bzero(&fp->ctf_dsdefs, sizeof (ctf_list_t));
688 bzero(&fp->ctf_dldefs, sizeof (ctf_list_t));
689
690 /*
691 * Because the various containers share the data sections, we don't want
692 * to have ctf_close free it all. However, the name of the section is in
693 * fact unique to the ctf_sect_t. Thus we save the names of the symbol
694 * and string sections around the bzero() and restore them afterwards,
695 * ensuring that we don't result in a memory leak.
696 */
697 sname = fp->ctf_symtab.cts_name;
698 bzero(&fp->ctf_symtab, sizeof (ctf_sect_t));
699 fp->ctf_symtab.cts_name = sname;
700
701 sname = fp->ctf_strtab.cts_name;
702 bzero(&fp->ctf_strtab, sizeof (ctf_sect_t));
703 fp->ctf_strtab.cts_name = sname;
704
705 bcopy(fp, &ofp, sizeof (ctf_file_t));
706 bcopy(nfp, fp, sizeof (ctf_file_t));
707 bcopy(&ofp, nfp, sizeof (ctf_file_t));
708
709 /*
710 * Initialize the ctf_lookup_by_name top-level dictionary. We keep an
711 * array of type name prefixes and the corresponding ctf_hash to use.
712 * NOTE: This code must be kept in sync with the code in ctf_bufopen().
713 */
714 fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
715 fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
716 fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
717 fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
718
719 nfp->ctf_refcnt = 1; /* force nfp to be freed */
720 ctf_close(nfp);
721
722 return (0);
723 }
724
725 void
726 ctf_dtd_insert(ctf_file_t *fp, ctf_dtdef_t *dtd)
727 {
728 ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
729
730 dtd->dtd_hash = fp->ctf_dthash[h];
731 fp->ctf_dthash[h] = dtd;
732 ctf_list_append(&fp->ctf_dtdefs, dtd);
733 }
734
735 void
736 ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd)
737 {
738 ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
739 ctf_dtdef_t *p, **q = &fp->ctf_dthash[h];
740 ctf_dmdef_t *dmd, *nmd;
741 size_t len;
742 int kind, i;
743
744 for (p = *q; p != NULL; p = p->dtd_hash) {
745 if (p != dtd)
746 q = &p->dtd_hash;
747 else
748 break;
749 }
750
751 if (p != NULL)
752 *q = p->dtd_hash;
753
754 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
755 switch (kind) {
756 case CTF_K_STRUCT:
757 case CTF_K_UNION:
758 case CTF_K_ENUM:
759 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
760 dmd != NULL; dmd = nmd) {
761 if (dmd->dmd_name != NULL) {
762 len = strlen(dmd->dmd_name) + 1;
763 ctf_free(dmd->dmd_name, len);
764 fp->ctf_dtstrlen -= len;
765 }
766 if (kind != CTF_K_ENUM)
767 ctf_ref_dec(fp, dmd->dmd_type);
768 nmd = ctf_list_next(dmd);
769 ctf_free(dmd, sizeof (ctf_dmdef_t));
770 }
771 break;
772 case CTF_K_FUNCTION:
773 ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
774 for (i = 0; i < CTF_INFO_VLEN(dtd->dtd_data.ctt_info); i++)
775 if (dtd->dtd_u.dtu_argv[i] != 0)
776 ctf_ref_dec(fp, dtd->dtd_u.dtu_argv[i]);
777 ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
778 CTF_INFO_VLEN(dtd->dtd_data.ctt_info));
779 break;
780 case CTF_K_ARRAY:
781 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
782 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
783 break;
784 case CTF_K_TYPEDEF:
785 ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
786 break;
787 case CTF_K_POINTER:
788 case CTF_K_VOLATILE:
789 case CTF_K_CONST:
790 case CTF_K_RESTRICT:
791 ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
792 break;
793 }
794
795 if (dtd->dtd_name) {
796 len = strlen(dtd->dtd_name) + 1;
797 ctf_free(dtd->dtd_name, len);
798 fp->ctf_dtstrlen -= len;
799 }
800
801 ctf_list_delete(&fp->ctf_dtdefs, dtd);
802 ctf_free(dtd, sizeof (ctf_dtdef_t));
803 }
804
805 ctf_dtdef_t *
806 ctf_dtd_lookup(ctf_file_t *fp, ctf_id_t type)
807 {
808 ulong_t h = type & (fp->ctf_dthashlen - 1);
809 ctf_dtdef_t *dtd;
810
811 if (fp->ctf_dthash == NULL)
812 return (NULL);
813
814 for (dtd = fp->ctf_dthash[h]; dtd != NULL; dtd = dtd->dtd_hash) {
815 if (dtd->dtd_type == type)
816 break;
817 }
818
819 return (dtd);
820 }
821
822 ctf_dsdef_t *
823 ctf_dsd_lookup(ctf_file_t *fp, ulong_t idx)
824 {
825 ctf_dsdef_t *dsd;
826
827 for (dsd = ctf_list_next(&fp->ctf_dsdefs); dsd != NULL;
828 dsd = ctf_list_next(dsd)) {
829 if (dsd->dsd_symidx == idx)
830 return (dsd);
831 }
832
833 return (NULL);
834 }
835
836 /*
837 * We order the ctf_dsdef_t by symbol index to make things better for updates.
838 */
839 void
840 ctf_dsd_insert(ctf_file_t *fp, ctf_dsdef_t *dsd)
841 {
842 ctf_dsdef_t *i;
843
844 for (i = ctf_list_next(&fp->ctf_dsdefs); i != NULL;
845 i = ctf_list_next(i)) {
846 if (i->dsd_symidx > dsd->dsd_symidx)
847 break;
848 }
849
850 if (i == NULL) {
851 ctf_list_append(&fp->ctf_dsdefs, dsd);
852 return;
853 }
854
855 ctf_list_insert_before(&fp->ctf_dsdefs, i, dsd);
856 }
857
858 /* ARGSUSED */
859 void
860 ctf_dsd_delete(ctf_file_t *fp, ctf_dsdef_t *dsd)
861 {
862 if (dsd->dsd_nargs > 0)
863 ctf_free(dsd->dsd_argc,
864 sizeof (ctf_id_t) * dsd->dsd_nargs);
865 ctf_list_delete(&fp->ctf_dsdefs, dsd);
866 ctf_free(dsd, sizeof (ctf_dsdef_t));
867 }
868
869 ctf_dldef_t *
870 ctf_dld_lookup(ctf_file_t *fp, const char *name)
871 {
872 ctf_dldef_t *dld;
873
874 for (dld = ctf_list_next(&fp->ctf_dldefs); dld != NULL;
875 dld = ctf_list_next(dld)) {
876 if (strcmp(name, dld->dld_name) == 0)
877 return (dld);
878 }
879
880 return (NULL);
881 }
882
883 void
884 ctf_dld_insert(ctf_file_t *fp, ctf_dldef_t *dld, uint_t pos)
885 {
886 ctf_dldef_t *l;
887
888 if (pos == 0) {
889 ctf_list_prepend(&fp->ctf_dldefs, dld);
890 return;
891 }
892
893 for (l = ctf_list_next(&fp->ctf_dldefs); pos != 0 && dld != NULL;
894 l = ctf_list_next(l), pos--)
895 ;
896
897 if (l == NULL)
898 ctf_list_append(&fp->ctf_dldefs, dld);
899 else
900 ctf_list_insert_before(&fp->ctf_dsdefs, l, dld);
901 }
902
903 void
904 ctf_dld_delete(ctf_file_t *fp, ctf_dldef_t *dld)
905 {
906 ctf_list_delete(&fp->ctf_dldefs, dld);
907
908 if (dld->dld_name != NULL) {
909 size_t len = strlen(dld->dld_name) + 1;
910 ctf_free(dld->dld_name, len);
911 fp->ctf_dtstrlen -= len;
912 }
913
914 ctf_free(dld, sizeof (ctf_dldef_t));
915 }
916
917 /*
918 * Discard all of the dynamic type definitions that have been added to the
919 * container since the last call to ctf_update(). We locate such types by
920 * scanning the list and deleting elements that have type IDs greater than
921 * ctf_dtoldid, which is set by ctf_update(), above. Note that to work properly
922 * with our reference counting schemes, we must delete the dynamic list in
923 * reverse.
924 */
925 int
926 ctf_discard(ctf_file_t *fp)
927 {
928 ctf_dtdef_t *dtd, *ntd;
929
930 if (!(fp->ctf_flags & LCTF_RDWR))
931 return (ctf_set_errno(fp, ECTF_RDONLY));
932
933 if (!(fp->ctf_flags & LCTF_DIRTY))
934 return (0); /* no update required */
935
936 for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
937 ntd = ctf_list_prev(dtd);
938 if (dtd->dtd_type <= fp->ctf_dtoldid)
939 continue; /* skip types that have been committed */
940
941 ctf_dtd_delete(fp, dtd);
942 }
943
944 fp->ctf_dtnextid = fp->ctf_dtoldid + 1;
945 fp->ctf_flags &= ~LCTF_DIRTY;
946
947 return (0);
948 }
949
950 static ctf_id_t
951 ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp)
952 {
953 ctf_dtdef_t *dtd;
954 ctf_id_t type;
955 char *s = NULL;
956
957 if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
958 return (ctf_set_errno(fp, EINVAL));
959
960 if (!(fp->ctf_flags & LCTF_RDWR))
961 return (ctf_set_errno(fp, ECTF_RDONLY));
962
963 if (CTF_INDEX_TO_TYPE(fp->ctf_dtnextid, 1) > CTF_MAX_TYPE)
964 return (ctf_set_errno(fp, ECTF_FULL));
965
966 if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL)
967 return (ctf_set_errno(fp, EAGAIN));
968
969 if (name != NULL && (s = ctf_strdup(name)) == NULL) {
970 ctf_free(dtd, sizeof (ctf_dtdef_t));
971 return (ctf_set_errno(fp, EAGAIN));
972 }
973
974 type = fp->ctf_dtnextid++;
975 type = CTF_INDEX_TO_TYPE(type, (fp->ctf_flags & LCTF_CHILD));
976
977 bzero(dtd, sizeof (ctf_dtdef_t));
978 dtd->dtd_name = s;
979 dtd->dtd_type = type;
980
981 if (s != NULL)
982 fp->ctf_dtstrlen += strlen(s) + 1;
983
984 ctf_dtd_insert(fp, dtd);
985 fp->ctf_flags |= LCTF_DIRTY;
986
987 *rp = dtd;
988 return (type);
989 }
990
991 ctf_id_t
992 ctf_add_encoded(ctf_file_t *fp, uint_t flag,
993 const char *name, const ctf_encoding_t *ep, uint_t kind)
994 {
995 ctf_dtdef_t *dtd;
996 ctf_id_t type;
997
998 if (ep == NULL)
999 return (ctf_set_errno(fp, EINVAL));
1000
1001 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1002 return (CTF_ERR); /* errno is set for us */
1003
1004 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
1005
1006 /*
1007 * If the type's size is not an even number of bytes, then we should
1008 * round up the type size to the nearest byte.
1009 */
1010 dtd->dtd_data.ctt_size = ep->cte_bits / NBBY;
1011 if ((ep->cte_bits % NBBY) != 0)
1012 dtd->dtd_data.ctt_size++;
1013 dtd->dtd_u.dtu_enc = *ep;
1014
1015 return (type);
1016 }
1017
1018 ctf_id_t
1019 ctf_add_reftype(ctf_file_t *fp, uint_t flag,
1020 const char *name, ctf_id_t ref, uint_t kind)
1021 {
1022 ctf_dtdef_t *dtd;
1023 ctf_id_t type;
1024
1025 if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE)
1026 return (ctf_set_errno(fp, EINVAL));
1027
1028 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1029 return (CTF_ERR); /* errno is set for us */
1030
1031 ctf_ref_inc(fp, ref);
1032
1033 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
1034 dtd->dtd_data.ctt_type = (ushort_t)ref;
1035
1036 return (type);
1037 }
1038
1039 ctf_id_t
1040 ctf_add_integer(ctf_file_t *fp, uint_t flag,
1041 const char *name, const ctf_encoding_t *ep)
1042 {
1043 return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER));
1044 }
1045
1046 ctf_id_t
1047 ctf_add_float(ctf_file_t *fp, uint_t flag,
1048 const char *name, const ctf_encoding_t *ep)
1049 {
1050 return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT));
1051 }
1052
1053 ctf_id_t
1054 ctf_add_pointer(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1055 {
1056 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_POINTER));
1057 }
1058
1059 ctf_id_t
1060 ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp)
1061 {
1062 ctf_dtdef_t *dtd;
1063 ctf_id_t type;
1064 ctf_file_t *fpd;
1065
1066 if (arp == NULL)
1067 return (ctf_set_errno(fp, EINVAL));
1068
1069 fpd = fp;
1070 if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
1071 ctf_dtd_lookup(fp, arp->ctr_contents) == NULL) {
1072 ctf_dprintf("bad contents for array: %ld\n",
1073 arp->ctr_contents);
1074 return (ctf_set_errno(fp, ECTF_BADID));
1075 }
1076
1077 fpd = fp;
1078 if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
1079 ctf_dtd_lookup(fp, arp->ctr_index) == NULL) {
1080 ctf_dprintf("bad index for array: %ld\n", arp->ctr_index);
1081 return (ctf_set_errno(fp, ECTF_BADID));
1082 }
1083
1084 if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
1085 return (CTF_ERR); /* errno is set for us */
1086
1087 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, flag, 0);
1088 dtd->dtd_data.ctt_size = 0;
1089 dtd->dtd_u.dtu_arr = *arp;
1090 ctf_ref_inc(fp, arp->ctr_contents);
1091 ctf_ref_inc(fp, arp->ctr_index);
1092
1093 return (type);
1094 }
1095
1096 int
1097 ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
1098 {
1099 ctf_file_t *fpd;
1100 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
1101
1102 if (!(fp->ctf_flags & LCTF_RDWR))
1103 return (ctf_set_errno(fp, ECTF_RDONLY));
1104
1105 if (dtd == NULL || CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
1106 return (ctf_set_errno(fp, ECTF_BADID));
1107
1108 fpd = fp;
1109 if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
1110 ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)
1111 return (ctf_set_errno(fp, ECTF_BADID));
1112
1113 fpd = fp;
1114 if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
1115 ctf_dtd_lookup(fp, arp->ctr_index) == NULL)
1116 return (ctf_set_errno(fp, ECTF_BADID));
1117
1118 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
1119 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
1120 fp->ctf_flags |= LCTF_DIRTY;
1121 dtd->dtd_u.dtu_arr = *arp;
1122 ctf_ref_inc(fp, arp->ctr_contents);
1123 ctf_ref_inc(fp, arp->ctr_index);
1124
1125 return (0);
1126 }
1127
1128 ctf_id_t
1129 ctf_add_funcptr(ctf_file_t *fp, uint_t flag,
1130 const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
1131 {
1132 ctf_dtdef_t *dtd;
1133 ctf_id_t type;
1134 uint_t vlen;
1135 int i;
1136 ctf_id_t *vdat = NULL;
1137 ctf_file_t *fpd;
1138
1139 if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 ||
1140 (ctc->ctc_argc != 0 && argv == NULL))
1141 return (ctf_set_errno(fp, EINVAL));
1142
1143 vlen = ctc->ctc_argc;
1144 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1145 vlen++; /* add trailing zero to indicate varargs (see below) */
1146
1147 if (vlen > CTF_MAX_VLEN)
1148 return (ctf_set_errno(fp, EOVERFLOW));
1149
1150 fpd = fp;
1151 if (ctf_lookup_by_id(&fpd, ctc->ctc_return) == NULL &&
1152 ctf_dtd_lookup(fp, ctc->ctc_return) == NULL)
1153 return (ctf_set_errno(fp, ECTF_BADID));
1154
1155 for (i = 0; i < ctc->ctc_argc; i++) {
1156 fpd = fp;
1157 if (ctf_lookup_by_id(&fpd, argv[i]) == NULL &&
1158 ctf_dtd_lookup(fp, argv[i]) == NULL)
1159 return (ctf_set_errno(fp, ECTF_BADID));
1160 }
1161
1162 if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL)
1163 return (ctf_set_errno(fp, EAGAIN));
1164
1165 if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) {
1166 ctf_free(vdat, sizeof (ctf_id_t) * vlen);
1167 return (CTF_ERR); /* errno is set for us */
1168 }
1169
1170 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, flag, vlen);
1171 dtd->dtd_data.ctt_type = (ushort_t)ctc->ctc_return;
1172
1173 ctf_ref_inc(fp, ctc->ctc_return);
1174 for (i = 0; i < ctc->ctc_argc; i++)
1175 ctf_ref_inc(fp, argv[i]);
1176
1177 bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc);
1178 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1179 vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */
1180 dtd->dtd_u.dtu_argv = vdat;
1181
1182 return (type);
1183 }
1184
1185 ctf_id_t
1186 ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name)
1187 {
1188 ctf_hash_t *hp = &fp->ctf_structs;
1189 ctf_helem_t *hep = NULL;
1190 ctf_dtdef_t *dtd = NULL;
1191 ctf_id_t type = CTF_ERR;
1192
1193 if (name != NULL)
1194 hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1195
1196 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1197 type = hep->h_type;
1198 dtd = ctf_dtd_lookup(fp, type);
1199 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1200 dtd = NULL;
1201 }
1202
1203 if (dtd == NULL) {
1204 type = ctf_add_generic(fp, flag, name, &dtd);
1205 if (type == CTF_ERR)
1206 return (CTF_ERR); /* errno is set for us */
1207 }
1208
1209 VERIFY(type != CTF_ERR);
1210 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, flag, 0);
1211 dtd->dtd_data.ctt_size = 0;
1212
1213 /*
1214 * Always dirty in case we modified a forward.
1215 */
1216 fp->ctf_flags |= LCTF_DIRTY;
1217
1218 return (type);
1219 }
1220
1221 ctf_id_t
1222 ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name)
1223 {
1224 ctf_hash_t *hp = &fp->ctf_unions;
1225 ctf_helem_t *hep = NULL;
1226 ctf_dtdef_t *dtd = NULL;
1227 ctf_id_t type = CTF_ERR;
1228
1229 if (name != NULL)
1230 hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1231
1232 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1233 type = hep->h_type;
1234 dtd = ctf_dtd_lookup(fp, type);
1235 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1236 dtd = NULL;
1237 }
1238
1239 if (dtd == NULL) {
1240 type = ctf_add_generic(fp, flag, name, &dtd);
1241 if (type == CTF_ERR)
1242 return (CTF_ERR); /* errno is set for us */
1243 }
1244
1245 VERIFY(type != CTF_ERR);
1246 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, flag, 0);
1247 dtd->dtd_data.ctt_size = 0;
1248
1249 /*
1250 * Always dirty in case we modified a forward.
1251 */
1252 fp->ctf_flags |= LCTF_DIRTY;
1253
1254 return (type);
1255 }
1256
1257 /*
1258 * If size is 0, we use the standard integer size. This is almost always the
1259 * case, except for packed enums.
1260 */
1261 ctf_id_t
1262 ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name, size_t size)
1263 {
1264 ctf_hash_t *hp = &fp->ctf_enums;
1265 ctf_helem_t *hep = NULL;
1266 ctf_dtdef_t *dtd = NULL;
1267 ctf_id_t type = CTF_ERR;
1268
1269 /* Check we could return something valid in ctf_type_size. */
1270 if (size > SSIZE_MAX)
1271 return (ctf_set_errno(fp, EINVAL));
1272
1273 if (name != NULL)
1274 hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1275
1276 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1277 type = hep->h_type;
1278 dtd = ctf_dtd_lookup(fp, type);
1279 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1280 dtd = NULL;
1281 }
1282
1283 if (dtd == NULL) {
1284 type = ctf_add_generic(fp, flag, name, &dtd);
1285 if (type == CTF_ERR)
1286 return (CTF_ERR); /* errno is set for us */
1287 }
1288
1289 VERIFY(type != CTF_ERR);
1290 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0);
1291
1292 ctf_set_ctt_size(&dtd->dtd_data, size == 0 ?
1293 fp->ctf_dmodel->ctd_int : size);
1294
1295 /*
1296 * Always dirty in case we modified a forward.
1297 */
1298 fp->ctf_flags |= LCTF_DIRTY;
1299
1300 return (type);
1301 }
1302
1303 ctf_id_t
1304 ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind)
1305 {
1306 ctf_hash_t *hp;
1307 ctf_helem_t *hep;
1308 ctf_dtdef_t *dtd;
1309 ctf_id_t type;
1310
1311 switch (kind) {
1312 case CTF_K_STRUCT:
1313 hp = &fp->ctf_structs;
1314 break;
1315 case CTF_K_UNION:
1316 hp = &fp->ctf_unions;
1317 break;
1318 case CTF_K_ENUM:
1319 hp = &fp->ctf_enums;
1320 break;
1321 default:
1322 return (ctf_set_errno(fp, ECTF_NOTSUE));
1323 }
1324
1325 /*
1326 * If the type is already defined or exists as a forward tag, just
1327 * return the ctf_id_t of the existing definition.
1328 */
1329 if (name != NULL && (hep = ctf_hash_lookup(hp,
1330 fp, name, strlen(name))) != NULL)
1331 return (hep->h_type);
1332
1333 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1334 return (CTF_ERR); /* errno is set for us */
1335
1336 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0);
1337 dtd->dtd_data.ctt_type = kind;
1338
1339 return (type);
1340 }
1341
1342 ctf_id_t
1343 ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1344 {
1345 ctf_dtdef_t *dtd;
1346 ctf_id_t type;
1347 ctf_file_t *fpd;
1348
1349 fpd = fp;
1350 if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL &&
1351 ctf_dtd_lookup(fp, ref) == NULL))
1352 return (ctf_set_errno(fp, EINVAL));
1353
1354 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1355 return (CTF_ERR); /* errno is set for us */
1356
1357 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0);
1358 dtd->dtd_data.ctt_type = (ushort_t)ref;
1359 ctf_ref_inc(fp, ref);
1360
1361 return (type);
1362 }
1363
1364 ctf_id_t
1365 ctf_add_volatile(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1366 {
1367 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_VOLATILE));
1368 }
1369
1370 ctf_id_t
1371 ctf_add_const(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1372 {
1373 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_CONST));
1374 }
1375
1376 ctf_id_t
1377 ctf_add_restrict(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1378 {
1379 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_RESTRICT));
1380 }
1381
1382 int
1383 ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
1384 {
1385 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
1386 ctf_dmdef_t *dmd;
1387
1388 uint_t kind, vlen, root;
1389 char *s;
1390
1391 if (name == NULL)
1392 return (ctf_set_errno(fp, EINVAL));
1393
1394 if (!(fp->ctf_flags & LCTF_RDWR))
1395 return (ctf_set_errno(fp, ECTF_RDONLY));
1396
1397 if (dtd == NULL)
1398 return (ctf_set_errno(fp, ECTF_BADID));
1399
1400 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1401 root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
1402 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
1403
1404 if (kind != CTF_K_ENUM)
1405 return (ctf_set_errno(fp, ECTF_NOTENUM));
1406
1407 if (vlen == CTF_MAX_VLEN)
1408 return (ctf_set_errno(fp, ECTF_DTFULL));
1409
1410 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1411 dmd != NULL; dmd = ctf_list_next(dmd)) {
1412 if (strcmp(dmd->dmd_name, name) == 0) {
1413 ctf_dprintf("encountered duplicate member %s\n", name);
1414 return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1415 }
1416 }
1417
1418 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1419 return (ctf_set_errno(fp, EAGAIN));
1420
1421 if ((s = ctf_strdup(name)) == NULL) {
1422 ctf_free(dmd, sizeof (ctf_dmdef_t));
1423 return (ctf_set_errno(fp, EAGAIN));
1424 }
1425
1426 dmd->dmd_name = s;
1427 dmd->dmd_type = CTF_ERR;
1428 dmd->dmd_offset = 0;
1429 dmd->dmd_value = value;
1430
1431 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1432 ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1433
1434 fp->ctf_dtstrlen += strlen(s) + 1;
1435 fp->ctf_flags |= LCTF_DIRTY;
1436
1437 return (0);
1438 }
1439
1440 int
1441 ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type,
1442 ulong_t offset)
1443 {
1444 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
1445 ctf_dmdef_t *dmd;
1446
1447 ulong_t mbitsz;
1448 ssize_t msize, malign, ssize;
1449 uint_t kind, vlen, root;
1450 int mkind;
1451 char *s = NULL;
1452
1453 if (!(fp->ctf_flags & LCTF_RDWR))
1454 return (ctf_set_errno(fp, ECTF_RDONLY));
1455
1456 if (dtd == NULL)
1457 return (ctf_set_errno(fp, ECTF_BADID));
1458
1459 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1460 root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
1461 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
1462
1463 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
1464 return (ctf_set_errno(fp, ECTF_NOTSOU));
1465
1466 if (vlen == CTF_MAX_VLEN)
1467 return (ctf_set_errno(fp, ECTF_DTFULL));
1468
1469 /*
1470 * Structures may have members which are anonymous. If they have two of
1471 * these, then the duplicate member detection would find it due to the
1472 * string of "", so we skip it.
1473 */
1474 if (name != NULL && *name != '\0') {
1475 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1476 dmd != NULL; dmd = ctf_list_next(dmd)) {
1477 if (dmd->dmd_name != NULL &&
1478 strcmp(dmd->dmd_name, name) == 0) {
1479 return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1480 }
1481 }
1482 }
1483
1484 if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
1485 (malign = ctf_type_align(fp, type)) == CTF_ERR ||
1486 (mkind = ctf_type_kind(fp, type)) == CTF_ERR)
1487 return (CTF_ERR); /* errno is set for us */
1488
1489 /*
1490 * ctf_type_size returns sizes in bytes. However, for bitfields, that
1491 * means that it may misrepresent and actually rounds it up to a power
1492 * of two and store that in bytes. So instead we have to get the
1493 * Integers encoding and rely on that.
1494 */
1495 if (mkind == CTF_K_INTEGER) {
1496 ctf_encoding_t e;
1497
1498 if (ctf_type_encoding(fp, type, &e) == CTF_ERR)
1499 return (CTF_ERR); /* errno is set for us */
1500 mbitsz = e.cte_bits;
1501 } else if (mkind == CTF_K_FORWARD) {
1502 /*
1503 * This is a rather rare case. In general one cannot add a
1504 * forward to a structure. However, the CTF tools traditionally
1505 * tried to add a forward to the struct cpu as the last member.
1506 * Therefore, if we find one here, we're going to verify the
1507 * size and make sure it's zero. It's certainly odd, but that's
1508 * life.
1509 *
1510 * Further, if it's not an absolute position being specified,
1511 * then we refuse to add it.
1512 */
1513 if (offset == ULONG_MAX)
1514 return (ctf_set_errno(fp, EINVAL));
1515 VERIFY(msize == 0);
1516 mbitsz = msize;
1517 } else {
1518 mbitsz = msize * 8;
1519 }
1520
1521 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1522 return (ctf_set_errno(fp, EAGAIN));
1523
1524 if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1525 ctf_free(dmd, sizeof (ctf_dmdef_t));
1526 return (ctf_set_errno(fp, EAGAIN));
1527 }
1528
1529 dmd->dmd_name = s;
1530 dmd->dmd_type = type;
1531 dmd->dmd_value = -1;
1532
1533 if (kind == CTF_K_STRUCT && vlen != 0) {
1534 ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
1535 ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
1536 size_t off;
1537
1538 if (offset == ULONG_MAX) {
1539 ctf_encoding_t linfo;
1540 ssize_t lsize;
1541
1542 off = lmd->dmd_offset;
1543 if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
1544 off += linfo.cte_bits;
1545 else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
1546 off += lsize * NBBY;
1547
1548 /*
1549 * Round up the offset of the end of the last member to
1550 * the next byte boundary, convert 'off' to bytes, and
1551 * then round it up again to the next multiple of the
1552 * alignment required by the new member. Finally,
1553 * convert back to bits and store the result in
1554 * dmd_offset. Technically we could do more efficient
1555 * packing if the new member is a bit-field, but we're
1556 * the "compiler" and ANSI says we can do as we choose.
1557 */
1558 off = roundup(off, NBBY) / NBBY;
1559 off = roundup(off, MAX(malign, 1));
1560 dmd->dmd_offset = off * NBBY;
1561 ssize = off + msize;
1562 } else {
1563 dmd->dmd_offset = offset;
1564 ssize = (offset + mbitsz) / NBBY;
1565 }
1566 } else {
1567 dmd->dmd_offset = 0;
1568 ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
1569 ssize = MAX(ssize, msize);
1570 }
1571
1572 ctf_set_ctt_size(&dtd->dtd_data, ssize);
1573
1574 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1575 ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1576
1577 if (s != NULL)
1578 fp->ctf_dtstrlen += strlen(s) + 1;
1579
1580 ctf_ref_inc(fp, type);
1581 fp->ctf_flags |= LCTF_DIRTY;
1582 return (0);
1583 }
1584
1585 /*
1586 * This removes a type from the dynamic section. This will fail if the type is
1587 * referenced by another type. Note that the CTF ID is never reused currently by
1588 * CTF. Note that if this container is a parent container then we just outright
1589 * refuse to remove the type. There currently is no notion of searching for the
1590 * ctf_dtdef_t in parent containers. If there is, then this constraint could
1591 * become finer grained.
1592 */
1593 int
1594 ctf_delete_type(ctf_file_t *fp, ctf_id_t type)
1595 {
1596 ctf_file_t *fpd;
1597 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
1598
1599 if (!(fp->ctf_flags & LCTF_RDWR))
1600 return (ctf_set_errno(fp, ECTF_RDONLY));
1601
1602 /*
1603 * We want to give as useful an errno as possible. That means that we
1604 * want to distinguish between a type which does not exist and one for
1605 * which the type is not dynamic.
1606 */
1607 fpd = fp;
1608 if (ctf_lookup_by_id(&fpd, type) == NULL &&
1609 ctf_dtd_lookup(fp, type) == NULL)
1610 return (CTF_ERR); /* errno is set for us */
1611
1612 if (dtd == NULL)
1613 return (ctf_set_errno(fp, ECTF_NOTDYN));
1614
1615 if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1)
1616 return (ctf_set_errno(fp, ECTF_REFERENCED));
1617
1618 ctf_dtd_delete(fp, dtd);
1619 fp->ctf_flags |= LCTF_DIRTY;
1620 return (0);
1621 }
1622
1623 static int
1624 enumcmp(const char *name, int value, void *arg)
1625 {
1626 ctf_bundle_t *ctb = arg;
1627 int bvalue;
1628
1629 return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type,
1630 name, &bvalue) == CTF_ERR || value != bvalue);
1631 }
1632
1633 static int
1634 enumadd(const char *name, int value, void *arg)
1635 {
1636 ctf_bundle_t *ctb = arg;
1637
1638 return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type,
1639 name, value) == CTF_ERR);
1640 }
1641
1642 /*ARGSUSED*/
1643 static int
1644 membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1645 {
1646 ctf_bundle_t *ctb = arg;
1647 ctf_membinfo_t ctm;
1648
1649 return (ctf_member_info(ctb->ctb_file, ctb->ctb_type,
1650 name, &ctm) == CTF_ERR || ctm.ctm_offset != offset);
1651 }
1652
1653 static int
1654 membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1655 {
1656 ctf_bundle_t *ctb = arg;
1657 ctf_dmdef_t *dmd;
1658 char *s = NULL;
1659
1660 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1661 return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1662
1663 if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1664 ctf_free(dmd, sizeof (ctf_dmdef_t));
1665 return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1666 }
1667
1668 /*
1669 * For now, dmd_type is copied as the src_fp's type; it is reset to an
1670 * equivalent dst_fp type by a final loop in ctf_add_type(), below.
1671 */
1672 dmd->dmd_name = s;
1673 dmd->dmd_type = type;
1674 dmd->dmd_offset = offset;
1675 dmd->dmd_value = -1;
1676
1677 ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
1678
1679 if (s != NULL)
1680 ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1;
1681
1682 ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
1683 return (0);
1684 }
1685
1686 /*
1687 * The ctf_add_type routine is used to copy a type from a source CTF container
1688 * to a dynamic destination container. This routine operates recursively by
1689 * following the source type's links and embedded member types. If the
1690 * destination container already contains a named type which has the same
1691 * attributes, then we succeed and return this type but no changes occur.
1692 */
1693 ctf_id_t
1694 ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
1695 {
1696 ctf_id_t dst_type = CTF_ERR;
1697 uint_t dst_kind = CTF_K_UNKNOWN;
1698
1699 const ctf_type_t *tp;
1700 const char *name;
1701 uint_t kind, flag, vlen;
1702
1703 ctf_bundle_t src, dst;
1704 ctf_encoding_t src_en, dst_en;
1705 ctf_arinfo_t src_ar, dst_ar;
1706
1707 ctf_dtdef_t *dtd;
1708 ctf_funcinfo_t ctc;
1709
1710 ctf_hash_t *hp;
1711 ctf_helem_t *hep;
1712
1713 if (dst_fp == src_fp)
1714 return (src_type);
1715
1716 if (!(dst_fp->ctf_flags & LCTF_RDWR))
1717 return (ctf_set_errno(dst_fp, ECTF_RDONLY));
1718
1719 if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)
1720 return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1721
1722 name = ctf_strptr(src_fp, tp->ctt_name);
1723 kind = LCTF_INFO_KIND(src_fp, tp->ctt_info);
1724 flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info);
1725 vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info);
1726
1727 switch (kind) {
1728 case CTF_K_STRUCT:
1729 hp = &dst_fp->ctf_structs;
1730 break;
1731 case CTF_K_UNION:
1732 hp = &dst_fp->ctf_unions;
1733 break;
1734 case CTF_K_ENUM:
1735 hp = &dst_fp->ctf_enums;
1736 break;
1737 default:
1738 hp = &dst_fp->ctf_names;
1739 break;
1740 }
1741
1742 /*
1743 * If the source type has a name and is a root type (visible at the
1744 * top-level scope), lookup the name in the destination container and
1745 * verify that it is of the same kind before we do anything else.
1746 */
1747 if ((flag & CTF_ADD_ROOT) && name[0] != '\0' &&
1748 (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) {
1749 dst_type = (ctf_id_t)hep->h_type;
1750 dst_kind = ctf_type_kind(dst_fp, dst_type);
1751 }
1752
1753 /*
1754 * If an identically named dst_type exists, fail with ECTF_CONFLICT
1755 * unless dst_type is a forward declaration and src_type is a struct,
1756 * union, or enum (i.e. the definition of the previous forward decl).
1757 */
1758 if (dst_type != CTF_ERR && dst_kind != kind && (
1759 dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM &&
1760 kind != CTF_K_STRUCT && kind != CTF_K_UNION)))
1761 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1762
1763 /*
1764 * If the non-empty name was not found in the appropriate hash, search
1765 * the list of pending dynamic definitions that are not yet committed.
1766 * If a matching name and kind are found, assume this is the type that
1767 * we are looking for. This is necessary to permit ctf_add_type() to
1768 * operate recursively on entities such as a struct that contains a
1769 * pointer member that refers to the same struct type.
1770 */
1771 if (dst_type == CTF_ERR && name[0] != '\0') {
1772 for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL &&
1773 dtd->dtd_type > dst_fp->ctf_dtoldid;
1774 dtd = ctf_list_prev(dtd)) {
1775 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) == kind &&
1776 dtd->dtd_name != NULL &&
1777 strcmp(dtd->dtd_name, name) == 0)
1778 return (dtd->dtd_type);
1779 }
1780 }
1781
1782 src.ctb_file = src_fp;
1783 src.ctb_type = src_type;
1784 src.ctb_dtd = NULL;
1785
1786 dst.ctb_file = dst_fp;
1787 dst.ctb_type = dst_type;
1788 dst.ctb_dtd = NULL;
1789
1790 /*
1791 * Now perform kind-specific processing. If dst_type is CTF_ERR, then
1792 * we add a new type with the same properties as src_type to dst_fp.
1793 * If dst_type is not CTF_ERR, then we verify that dst_type has the
1794 * same attributes as src_type. We recurse for embedded references.
1795 */
1796 switch (kind) {
1797 case CTF_K_INTEGER:
1798 case CTF_K_FLOAT:
1799 if (ctf_type_encoding(src_fp, src_type, &src_en) != 0)
1800 return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1801
1802 if (dst_type != CTF_ERR) {
1803 if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0)
1804 return (CTF_ERR); /* errno is set for us */
1805
1806 if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t)))
1807 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1808
1809 } else if (kind == CTF_K_INTEGER) {
1810 dst_type = ctf_add_integer(dst_fp, flag, name, &src_en);
1811 } else
1812 dst_type = ctf_add_float(dst_fp, flag, name, &src_en);
1813 break;
1814
1815 case CTF_K_POINTER:
1816 case CTF_K_VOLATILE:
1817 case CTF_K_CONST:
1818 case CTF_K_RESTRICT:
1819 src_type = ctf_type_reference(src_fp, src_type);
1820 src_type = ctf_add_type(dst_fp, src_fp, src_type);
1821
1822 if (src_type == CTF_ERR)
1823 return (CTF_ERR); /* errno is set for us */
1824
1825 dst_type = ctf_add_reftype(dst_fp, flag, NULL, src_type, kind);
1826 break;
1827
1828 case CTF_K_ARRAY:
1829 if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR)
1830 return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1831
1832 src_ar.ctr_contents =
1833 ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents);
1834 src_ar.ctr_index =
1835 ctf_add_type(dst_fp, src_fp, src_ar.ctr_index);
1836 src_ar.ctr_nelems = src_ar.ctr_nelems;
1837
1838 if (src_ar.ctr_contents == CTF_ERR ||
1839 src_ar.ctr_index == CTF_ERR)
1840 return (CTF_ERR); /* errno is set for us */
1841
1842 if (dst_type != CTF_ERR) {
1843 if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0)
1844 return (CTF_ERR); /* errno is set for us */
1845
1846 if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
1847 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1848 } else
1849 dst_type = ctf_add_array(dst_fp, flag, &src_ar);
1850 break;
1851
1852 case CTF_K_FUNCTION:
1853 ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type);
1854 ctc.ctc_argc = 0;
1855 ctc.ctc_flags = 0;
1856
1857 if (ctc.ctc_return == CTF_ERR)
1858 return (CTF_ERR); /* errno is set for us */
1859
1860 dst_type = ctf_add_funcptr(dst_fp, flag, &ctc, NULL);
1861 break;
1862
1863 case CTF_K_STRUCT:
1864 case CTF_K_UNION: {
1865 ctf_dmdef_t *dmd;
1866 int errs = 0;
1867
1868 /*
1869 * Technically to match a struct or union we need to check both
1870 * ways (src members vs. dst, dst members vs. src) but we make
1871 * this more optimal by only checking src vs. dst and comparing
1872 * the total size of the structure (which we must do anyway)
1873 * which covers the possibility of dst members not in src.
1874 * This optimization can be defeated for unions, but is so
1875 * pathological as to render it irrelevant for our purposes.
1876 */
1877 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1878 if (ctf_type_size(src_fp, src_type) !=
1879 ctf_type_size(dst_fp, dst_type))
1880 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1881
1882 if (ctf_member_iter(src_fp, src_type, membcmp, &dst))
1883 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1884
1885 break;
1886 }
1887
1888 /*
1889 * Unlike the other cases, copying structs and unions is done
1890 * manually so as to avoid repeated lookups in ctf_add_member
1891 * and to ensure the exact same member offsets as in src_type.
1892 */
1893 dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);
1894 if (dst_type == CTF_ERR)
1895 return (CTF_ERR); /* errno is set for us */
1896
1897 dst.ctb_type = dst_type;
1898 dst.ctb_dtd = dtd;
1899
1900 if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)
1901 errs++; /* increment errs and fail at bottom of case */
1902
1903 ctf_set_ctt_size(&dtd->dtd_data,
1904 ctf_type_size(src_fp, src_type));
1905
1906 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen);
1907
1908 /*
1909 * Make a final pass through the members changing each dmd_type
1910 * (a src_fp type) to an equivalent type in dst_fp. We pass
1911 * through all members, leaving any that fail set to CTF_ERR.
1912 */
1913 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1914 dmd != NULL; dmd = ctf_list_next(dmd)) {
1915 if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp,
1916 dmd->dmd_type)) == CTF_ERR)
1917 errs++;
1918 }
1919
1920 if (errs)
1921 return (CTF_ERR); /* errno is set for us */
1922
1923 /*
1924 * Now that we know that we can't fail, we go through and bump
1925 * all the reference counts on the member types.
1926 */
1927 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1928 dmd != NULL; dmd = ctf_list_next(dmd))
1929 ctf_ref_inc(dst_fp, dmd->dmd_type);
1930 break;
1931 }
1932
1933 case CTF_K_ENUM:
1934 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1935 if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||
1936 ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))
1937 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1938 } else {
1939 ssize_t size = ctf_type_size(src_fp, src_type);
1940
1941 if (size == CTF_ERR)
1942 return (CTF_ERR); /* errno is set for us */
1943
1944 dst_type = ctf_add_enum(dst_fp, flag, name, size);
1945 if ((dst.ctb_type = dst_type) == CTF_ERR ||
1946 ctf_enum_iter(src_fp, src_type, enumadd, &dst))
1947 return (CTF_ERR); /* errno is set for us */
1948 }
1949 break;
1950
1951 case CTF_K_FORWARD:
1952 if (dst_type == CTF_ERR) {
1953 dst_type = ctf_add_forward(dst_fp,
1954 flag, name, CTF_K_STRUCT); /* assume STRUCT */
1955 }
1956 break;
1957
1958 case CTF_K_TYPEDEF:
1959 src_type = ctf_type_reference(src_fp, src_type);
1960 src_type = ctf_add_type(dst_fp, src_fp, src_type);
1961
1962 if (src_type == CTF_ERR)
1963 return (CTF_ERR); /* errno is set for us */
1964
1965 /*
1966 * If dst_type is not CTF_ERR at this point, we should check if
1967 * ctf_type_reference(dst_fp, dst_type) != src_type and if so
1968 * fail with ECTF_CONFLICT. However, this causes problems with
1969 * <sys/types.h> typedefs that vary based on things like if
1970 * _ILP32x then pid_t is int otherwise long. We therefore omit
1971 * this check and assume that if the identically named typedef
1972 * already exists in dst_fp, it is correct or equivalent.
1973 */
1974 if (dst_type == CTF_ERR) {
1975 dst_type = ctf_add_typedef(dst_fp, flag,
1976 name, src_type);
1977 }
1978 break;
1979
1980 default:
1981 return (ctf_set_errno(dst_fp, ECTF_CORRUPT));
1982 }
1983
1984 return (dst_type);
1985 }
1986
1987 int
1988 ctf_add_function(ctf_file_t *fp, ulong_t idx, const ctf_funcinfo_t *fip,
1989 const ctf_id_t *argc)
1990 {
1991 int i;
1992 ctf_dsdef_t *dsd;
1993 ctf_file_t *afp;
1994 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
1995
1996 if (!(fp->ctf_flags & LCTF_RDWR))
1997 return (ctf_set_errno(fp, ECTF_RDONLY));
1998
1999 if (ctf_dsd_lookup(fp, idx) != NULL)
2000 return (ctf_set_errno(fp, ECTF_CONFLICT));
2001
2002 if (symbase == (uintptr_t)NULL)
2003 return (ctf_set_errno(fp, ECTF_STRTAB));
2004
2005 if (idx > fp->ctf_nsyms)
2006 return (ctf_set_errno(fp, ECTF_NOTDATA));
2007
2008 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
2009 const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx;
2010 if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)
2011 return (ctf_set_errno(fp, ECTF_NOTFUNC));
2012 } else {
2013 const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx;
2014 if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)
2015 return (ctf_set_errno(fp, ECTF_NOTFUNC));
2016 }
2017
2018 afp = fp;
2019 if (ctf_lookup_by_id(&afp, fip->ctc_return) == NULL)
2020 return (CTF_ERR); /* errno is set for us */
2021
2022 for (i = 0; i < fip->ctc_argc; i++) {
2023 afp = fp;
2024 if (ctf_lookup_by_id(&afp, argc[i]) == NULL)
2025 return (CTF_ERR); /* errno is set for us */
2026 }
2027
2028 dsd = ctf_alloc(sizeof (ctf_dsdef_t));
2029 if (dsd == NULL)
2030 return (ctf_set_errno(fp, ENOMEM));
2031 dsd->dsd_nargs = fip->ctc_argc;
2032 if (fip->ctc_flags & CTF_FUNC_VARARG)
2033 dsd->dsd_nargs++;
2034 if (dsd->dsd_nargs != 0) {
2035 dsd->dsd_argc = ctf_alloc(sizeof (ctf_id_t) * dsd->dsd_nargs);
2036 if (dsd->dsd_argc == NULL) {
2037 ctf_free(dsd, sizeof (ctf_dsdef_t));
2038 return (ctf_set_errno(fp, ENOMEM));
2039 }
2040 bcopy(argc, dsd->dsd_argc, sizeof (ctf_id_t) * fip->ctc_argc);
2041 if (fip->ctc_flags & CTF_FUNC_VARARG)
2042 dsd->dsd_argc[fip->ctc_argc] = 0;
2043 }
2044 dsd->dsd_symidx = idx;
2045 dsd->dsd_tid = fip->ctc_return;
2046
2047 ctf_dsd_insert(fp, dsd);
2048 fp->ctf_flags |= LCTF_DIRTY;
2049
2050 return (0);
2051 }
2052
2053 int
2054 ctf_add_object(ctf_file_t *fp, ulong_t idx, ctf_id_t type)
2055 {
2056 ctf_dsdef_t *dsd;
2057 ctf_file_t *afp;
2058 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
2059
2060 if (!(fp->ctf_flags & LCTF_RDWR))
2061 return (ctf_set_errno(fp, ECTF_RDONLY));
2062
2063 if (!(fp->ctf_flags & LCTF_RDWR))
2064 return (ctf_set_errno(fp, ECTF_RDONLY));
2065
2066 if (ctf_dsd_lookup(fp, idx) != NULL)
2067 return (ctf_set_errno(fp, ECTF_CONFLICT));
2068
2069 if (symbase == (uintptr_t)NULL)
2070 return (ctf_set_errno(fp, ECTF_STRTAB));
2071
2072 if (idx > fp->ctf_nsyms)
2073 return (ctf_set_errno(fp, ECTF_NOTDATA));
2074
2075 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
2076 const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx;
2077 if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT)
2078 return (ctf_set_errno(fp, ECTF_NOTDATA));
2079 } else {
2080 const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx;
2081 if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT)
2082 return (ctf_set_errno(fp, ECTF_NOTDATA));
2083 }
2084
2085 afp = fp;
2086 if (ctf_lookup_by_id(&afp, type) == NULL)
2087 return (CTF_ERR); /* errno is set for us */
2088
2089 dsd = ctf_alloc(sizeof (ctf_dsdef_t));
2090 if (dsd == NULL)
2091 return (ctf_set_errno(fp, ENOMEM));
2092 dsd->dsd_symidx = idx;
2093 dsd->dsd_tid = type;
2094 dsd->dsd_argc = NULL;
2095
2096 ctf_dsd_insert(fp, dsd);
2097 fp->ctf_flags |= LCTF_DIRTY;
2098
2099 return (0);
2100 }
2101
2102 void
2103 ctf_dataptr(ctf_file_t *fp, const void **addrp, size_t *sizep)
2104 {
2105 if (addrp != NULL)
2106 *addrp = fp->ctf_base;
2107 if (sizep != NULL)
2108 *sizep = fp->ctf_size;
2109 }
2110
2111 int
2112 ctf_add_label(ctf_file_t *fp, const char *name, ctf_id_t type, uint_t position)
2113 {
2114 ctf_file_t *fpd;
2115 ctf_dldef_t *dld;
2116
2117 if (name == NULL)
2118 return (ctf_set_errno(fp, EINVAL));
2119
2120 if (!(fp->ctf_flags & LCTF_RDWR))
2121 return (ctf_set_errno(fp, ECTF_RDONLY));
2122
2123 fpd = fp;
2124 if (type != 0 && ctf_lookup_by_id(&fpd, type) == NULL)
2125 return (CTF_ERR); /* errno is set for us */
2126
2127 if (type != 0 && (fp->ctf_flags & LCTF_CHILD) &&
2128 CTF_TYPE_ISPARENT(type))
2129 return (ctf_set_errno(fp, ECTF_NOPARENT));
2130
2131 if (ctf_dld_lookup(fp, name) != NULL)
2132 return (ctf_set_errno(fp, ECTF_LABELEXISTS));
2133
2134 if ((dld = ctf_alloc(sizeof (ctf_dldef_t))) == NULL)
2135 return (ctf_set_errno(fp, EAGAIN));
2136
2137 if ((dld->dld_name = ctf_strdup(name)) == NULL) {
2138 ctf_free(dld, sizeof (ctf_dldef_t));
2139 return (ctf_set_errno(fp, EAGAIN));
2140 }
2141
2142 ctf_dprintf("adding label %s, %ld\n", name, type);
2143 dld->dld_type = type;
2144 fp->ctf_dtstrlen += strlen(name) + 1;
2145 ctf_dld_insert(fp, dld, position);
2146 fp->ctf_flags |= LCTF_DIRTY;
2147
2148 return (0);
2149 }
2150
2151 /*
2152 * Update the size of a structure or union. Note that we don't allow this to
2153 * shrink the size of a struct or union, only to increase it. This is useful for
2154 * cases when you have a structure whose actual size is larger than the sum of
2155 * its members due to padding for natural alignment.
2156 */
2157 int
2158 ctf_set_size(ctf_file_t *fp, ctf_id_t id, const ulong_t newsz)
2159 {
2160 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2161 uint_t kind;
2162 size_t oldsz;
2163
2164 if (!(fp->ctf_flags & LCTF_RDWR))
2165 return (ctf_set_errno(fp, ECTF_RDONLY));
2166
2167 if (dtd == NULL)
2168 return (ctf_set_errno(fp, ECTF_BADID));
2169
2170 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2171
2172 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
2173 return (ctf_set_errno(fp, ECTF_NOTSOU));
2174
2175 if ((oldsz = dtd->dtd_data.ctt_size) == CTF_LSIZE_SENT)
2176 oldsz = CTF_TYPE_LSIZE(&dtd->dtd_data);
2177
2178 if (newsz < oldsz)
2179 return (ctf_set_errno(fp, EINVAL));
2180
2181 ctf_set_ctt_size(&dtd->dtd_data, newsz);
2182
2183 fp->ctf_flags |= LCTF_DIRTY;
2184 return (0);
2185 }
2186
2187 int
2188 ctf_set_root(ctf_file_t *fp, ctf_id_t id, const boolean_t vis)
2189 {
2190 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2191 uint_t kind, vlen;
2192
2193 if (!(fp->ctf_flags & LCTF_RDWR))
2194 return (ctf_set_errno(fp, ECTF_RDONLY));
2195
2196 if (dtd == NULL)
2197 return (ctf_set_errno(fp, ECTF_BADID));
2198
2199 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2200 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
2201
2202 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, vis, vlen);
2203 return (0);
2204 }