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
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.
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]
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
31 #include <sys/sysmacros.h>
32 #include <sys/param.h>
35 #include <sys/debug.h>
38 * This static string is used as the template for initially populating a
39 * dynamic container's string table. We always store \0 in the first byte,
40 * and we use the generic string "PARENT" to mark this container's parent
41 * if one is associated with the container using ctf_import().
43 static const char _CTF_STRTAB_TEMPLATE
[] = "\0PARENT";
46 * To create an empty CTF container, we just declare a zeroed header and call
47 * ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w
48 * and initialize the dynamic members. We set dtstrlen to 1 to reserve the
49 * first byte of the string table for a \0 byte, and we start assigning type
50 * IDs at 1 because type ID 0 is used as a sentinel.
55 static const ctf_header_t hdr
= { { CTF_MAGIC
, CTF_VERSION
, 0 } };
57 const ulong_t hashlen
= 128;
58 ctf_dtdef_t
**hash
= ctf_alloc(hashlen
* sizeof (ctf_dtdef_t
*));
63 return (ctf_set_open_errno(errp
, EAGAIN
));
65 cts
.cts_name
= _CTF_SECTION
;
66 cts
.cts_type
= SHT_PROGBITS
;
69 cts
.cts_size
= sizeof (hdr
);
73 if ((fp
= ctf_bufopen(&cts
, NULL
, NULL
, errp
)) == NULL
) {
74 ctf_free(hash
, hashlen
* sizeof (ctf_dtdef_t
*));
78 fp
->ctf_flags
|= LCTF_RDWR
;
79 fp
->ctf_dthashlen
= hashlen
;
80 bzero(hash
, hashlen
* sizeof (ctf_dtdef_t
*));
81 fp
->ctf_dthash
= hash
;
82 fp
->ctf_dtstrlen
= sizeof (_CTF_STRTAB_TEMPLATE
);
90 ctf_copy_smembers(ctf_dtdef_t
*dtd
, uint_t soff
, uchar_t
*t
)
92 ctf_dmdef_t
*dmd
= ctf_list_next(&dtd
->dtd_u
.dtu_members
);
95 for (; dmd
!= NULL
; dmd
= ctf_list_next(dmd
)) {
98 soff
+= strlen(dmd
->dmd_name
) + 1;
102 ctm
.ctm_type
= (ushort_t
)dmd
->dmd_type
;
103 ctm
.ctm_offset
= (ushort_t
)dmd
->dmd_offset
;
105 bcopy(&ctm
, t
, sizeof (ctm
));
113 ctf_copy_lmembers(ctf_dtdef_t
*dtd
, uint_t soff
, uchar_t
*t
)
115 ctf_dmdef_t
*dmd
= ctf_list_next(&dtd
->dtd_u
.dtu_members
);
118 for (; dmd
!= NULL
; dmd
= ctf_list_next(dmd
)) {
120 ctlm
.ctlm_name
= soff
;
121 soff
+= strlen(dmd
->dmd_name
) + 1;
125 ctlm
.ctlm_type
= (ushort_t
)dmd
->dmd_type
;
127 ctlm
.ctlm_offsethi
= CTF_OFFSET_TO_LMEMHI(dmd
->dmd_offset
);
128 ctlm
.ctlm_offsetlo
= CTF_OFFSET_TO_LMEMLO(dmd
->dmd_offset
);
130 bcopy(&ctlm
, t
, sizeof (ctlm
));
138 ctf_copy_emembers(ctf_dtdef_t
*dtd
, uint_t soff
, uchar_t
*t
)
140 ctf_dmdef_t
*dmd
= ctf_list_next(&dtd
->dtd_u
.dtu_members
);
143 for (; dmd
!= NULL
; dmd
= ctf_list_next(dmd
)) {
145 cte
.cte_value
= dmd
->dmd_value
;
146 soff
+= strlen(dmd
->dmd_name
) + 1;
147 bcopy(&cte
, t
, sizeof (cte
));
155 ctf_copy_membnames(ctf_dtdef_t
*dtd
, uchar_t
*s
)
157 ctf_dmdef_t
*dmd
= ctf_list_next(&dtd
->dtd_u
.dtu_members
);
160 for (; dmd
!= NULL
; dmd
= ctf_list_next(dmd
)) {
161 if (dmd
->dmd_name
== NULL
)
162 continue; /* skip anonymous members */
163 len
= strlen(dmd
->dmd_name
) + 1;
164 bcopy(dmd
->dmd_name
, s
, len
);
172 * Only types of dyanmic CTF containers contain reference counts. These
173 * containers are marked RD/WR. Because of that we basically make this a no-op
174 * for compatability with non-dynamic CTF sections. This is also a no-op for
175 * types which are not dynamic types. It is the responsibility of the caller to
176 * make sure it is a valid type. We help that caller out on debug builds.
178 * Note that the reference counts are not maintained for types that are not
179 * within this container. In other words if we have a type in a parent, that
180 * will not have its reference count increased. On the flip side, the parent
181 * will not be allowed to remove dynamic types if it has children.
184 ctf_ref_inc(ctf_file_t
*fp
, ctf_id_t tid
)
186 ctf_dtdef_t
*dtd
= ctf_dtd_lookup(fp
, tid
);
191 if (!(fp
->ctf_flags
& LCTF_RDWR
))
198 * Just as with ctf_ref_inc, this is a no-op on non-writeable containers and the
199 * caller should ensure that this is already a valid type.
202 ctf_ref_dec(ctf_file_t
*fp
, ctf_id_t tid
)
204 ctf_dtdef_t
*dtd
= ctf_dtd_lookup(fp
, tid
);
209 if (!(fp
->ctf_flags
& LCTF_RDWR
))
212 ASSERT(dtd
->dtd_ref
>= 1);
217 * If the specified CTF container is writable and has been modified, reload
218 * this container with the updated type definitions. In order to make this
219 * code and the rest of libctf as simple as possible, we perform updates by
220 * taking the dynamic type definitions and creating an in-memory CTF file
221 * containing the definitions, and then call ctf_bufopen() on it. This not
222 * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest
223 * of the library code with different lookup paths for static and dynamic
224 * type definitions. We are therefore optimizing greatly for lookup over
225 * update, which we assume will be an uncommon operation. We perform one
226 * extra trick here for the benefit of callers and to keep our code simple:
227 * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp
228 * constant for the caller, so after ctf_bufopen() returns, we use bcopy to
229 * swap the interior of the old and new ctf_file_t's, and then free the old.
231 * Note that the lists of dynamic types stays around and the resulting container
232 * is still writeable. Furthermore, the reference counts that are on the dtd's
236 ctf_update(ctf_file_t
*fp
)
238 ctf_file_t ofp
, *nfp
;
248 if (!(fp
->ctf_flags
& LCTF_RDWR
))
249 return (ctf_set_errno(fp
, ECTF_RDONLY
));
251 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
252 return (0); /* no update required */
255 * Fill in an initial CTF header. We will leave the label, object,
256 * and function sections empty and only output a header, type section,
257 * and string table. The type section begins at a 4-byte aligned
258 * boundary past the CTF header itself (at relative offset zero).
260 bzero(&hdr
, sizeof (hdr
));
261 hdr
.cth_magic
= CTF_MAGIC
;
262 hdr
.cth_version
= CTF_VERSION
;
264 if (fp
->ctf_flags
& LCTF_CHILD
)
265 hdr
.cth_parname
= 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */
268 * Iterate through the dynamic type definition list and compute the
269 * size of the CTF type section we will need to generate.
271 for (size
= 0, dtd
= ctf_list_next(&fp
->ctf_dtdefs
);
272 dtd
!= NULL
; dtd
= ctf_list_next(dtd
)) {
274 uint_t kind
= CTF_INFO_KIND(dtd
->dtd_data
.ctt_info
);
275 uint_t vlen
= CTF_INFO_VLEN(dtd
->dtd_data
.ctt_info
);
277 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
278 size
+= sizeof (ctf_stype_t
);
280 size
+= sizeof (ctf_type_t
);
285 size
+= sizeof (uint_t
);
288 size
+= sizeof (ctf_array_t
);
291 size
+= sizeof (ushort_t
) * (vlen
+ (vlen
& 1));
295 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
296 size
+= sizeof (ctf_member_t
) * vlen
;
298 size
+= sizeof (ctf_lmember_t
) * vlen
;
301 size
+= sizeof (ctf_enum_t
) * vlen
;
307 * Fill in the string table offset and size, compute the size of the
308 * entire CTF buffer we need, and then allocate a new buffer and
309 * bcopy the finished header to the start of the buffer.
311 hdr
.cth_stroff
= hdr
.cth_typeoff
+ size
;
312 hdr
.cth_strlen
= fp
->ctf_dtstrlen
;
313 size
= sizeof (ctf_header_t
) + hdr
.cth_stroff
+ hdr
.cth_strlen
;
315 if ((buf
= ctf_data_alloc(size
)) == MAP_FAILED
)
316 return (ctf_set_errno(fp
, EAGAIN
));
318 bcopy(&hdr
, buf
, sizeof (ctf_header_t
));
319 t
= (uchar_t
*)buf
+ sizeof (ctf_header_t
);
320 s
= s0
= (uchar_t
*)buf
+ sizeof (ctf_header_t
) + hdr
.cth_stroff
;
322 bcopy(_CTF_STRTAB_TEMPLATE
, s
, sizeof (_CTF_STRTAB_TEMPLATE
));
323 s
+= sizeof (_CTF_STRTAB_TEMPLATE
);
326 * We now take a final lap through the dynamic type definition list and
327 * copy the appropriate type records and strings to the output buffer.
329 for (dtd
= ctf_list_next(&fp
->ctf_dtdefs
);
330 dtd
!= NULL
; dtd
= ctf_list_next(dtd
)) {
332 uint_t kind
= CTF_INFO_KIND(dtd
->dtd_data
.ctt_info
);
333 uint_t vlen
= CTF_INFO_VLEN(dtd
->dtd_data
.ctt_info
);
339 if (dtd
->dtd_name
!= NULL
) {
340 dtd
->dtd_data
.ctt_name
= (uint_t
)(s
- s0
);
341 len
= strlen(dtd
->dtd_name
) + 1;
342 bcopy(dtd
->dtd_name
, s
, len
);
345 dtd
->dtd_data
.ctt_name
= 0;
347 if (dtd
->dtd_data
.ctt_size
!= CTF_LSIZE_SENT
)
348 len
= sizeof (ctf_stype_t
);
350 len
= sizeof (ctf_type_t
);
352 bcopy(&dtd
->dtd_data
, t
, len
);
358 if (kind
== CTF_K_INTEGER
) {
359 encoding
= CTF_INT_DATA(
360 dtd
->dtd_u
.dtu_enc
.cte_format
,
361 dtd
->dtd_u
.dtu_enc
.cte_offset
,
362 dtd
->dtd_u
.dtu_enc
.cte_bits
);
364 encoding
= CTF_FP_DATA(
365 dtd
->dtd_u
.dtu_enc
.cte_format
,
366 dtd
->dtd_u
.dtu_enc
.cte_offset
,
367 dtd
->dtd_u
.dtu_enc
.cte_bits
);
369 bcopy(&encoding
, t
, sizeof (encoding
));
370 t
+= sizeof (encoding
);
374 cta
.cta_contents
= (ushort_t
)
375 dtd
->dtd_u
.dtu_arr
.ctr_contents
;
376 cta
.cta_index
= (ushort_t
)
377 dtd
->dtd_u
.dtu_arr
.ctr_index
;
378 cta
.cta_nelems
= dtd
->dtd_u
.dtu_arr
.ctr_nelems
;
379 bcopy(&cta
, t
, sizeof (cta
));
383 case CTF_K_FUNCTION
: {
384 ushort_t
*argv
= (ushort_t
*)(uintptr_t)t
;
387 for (argc
= 0; argc
< vlen
; argc
++)
388 *argv
++ = (ushort_t
)dtd
->dtd_u
.dtu_argv
[argc
];
391 *argv
++ = 0; /* pad to 4-byte boundary */
399 if (dtd
->dtd_data
.ctt_size
< CTF_LSTRUCT_THRESH
)
400 t
= ctf_copy_smembers(dtd
, (uint_t
)(s
- s0
), t
);
402 t
= ctf_copy_lmembers(dtd
, (uint_t
)(s
- s0
), t
);
403 s
= ctf_copy_membnames(dtd
, s
);
407 t
= ctf_copy_emembers(dtd
, (uint_t
)(s
- s0
), t
);
408 s
= ctf_copy_membnames(dtd
, s
);
414 * Finally, we are ready to ctf_bufopen() the new container. If this
415 * is successful, we then switch nfp and fp and free the old container.
417 ctf_data_protect(buf
, size
);
418 cts
.cts_name
= _CTF_SECTION
;
419 cts
.cts_type
= SHT_PROGBITS
;
426 if ((nfp
= ctf_bufopen(&cts
, NULL
, NULL
, &err
)) == NULL
) {
427 ctf_data_free(buf
, size
);
428 return (ctf_set_errno(fp
, err
));
431 (void) ctf_setmodel(nfp
, ctf_getmodel(fp
));
432 (void) ctf_import(nfp
, fp
->ctf_parent
);
434 nfp
->ctf_refcnt
= fp
->ctf_refcnt
;
435 nfp
->ctf_flags
|= fp
->ctf_flags
& ~LCTF_DIRTY
;
436 nfp
->ctf_data
.cts_data
= NULL
; /* force ctf_data_free() on close */
437 nfp
->ctf_dthash
= fp
->ctf_dthash
;
438 nfp
->ctf_dthashlen
= fp
->ctf_dthashlen
;
439 nfp
->ctf_dtdefs
= fp
->ctf_dtdefs
;
440 nfp
->ctf_dtstrlen
= fp
->ctf_dtstrlen
;
441 nfp
->ctf_dtnextid
= fp
->ctf_dtnextid
;
442 nfp
->ctf_dtoldid
= fp
->ctf_dtnextid
- 1;
443 nfp
->ctf_specific
= fp
->ctf_specific
;
445 fp
->ctf_dthash
= NULL
;
446 fp
->ctf_dthashlen
= 0;
447 bzero(&fp
->ctf_dtdefs
, sizeof (ctf_list_t
));
449 bcopy(fp
, &ofp
, sizeof (ctf_file_t
));
450 bcopy(nfp
, fp
, sizeof (ctf_file_t
));
451 bcopy(&ofp
, nfp
, sizeof (ctf_file_t
));
454 * Initialize the ctf_lookup_by_name top-level dictionary. We keep an
455 * array of type name prefixes and the corresponding ctf_hash to use.
456 * NOTE: This code must be kept in sync with the code in ctf_bufopen().
458 fp
->ctf_lookups
[0].ctl_hash
= &fp
->ctf_structs
;
459 fp
->ctf_lookups
[1].ctl_hash
= &fp
->ctf_unions
;
460 fp
->ctf_lookups
[2].ctl_hash
= &fp
->ctf_enums
;
461 fp
->ctf_lookups
[3].ctl_hash
= &fp
->ctf_names
;
463 nfp
->ctf_refcnt
= 1; /* force nfp to be freed */
470 ctf_dtd_insert(ctf_file_t
*fp
, ctf_dtdef_t
*dtd
)
472 ulong_t h
= dtd
->dtd_type
& (fp
->ctf_dthashlen
- 1);
474 dtd
->dtd_hash
= fp
->ctf_dthash
[h
];
475 fp
->ctf_dthash
[h
] = dtd
;
476 ctf_list_append(&fp
->ctf_dtdefs
, dtd
);
480 ctf_dtd_delete(ctf_file_t
*fp
, ctf_dtdef_t
*dtd
)
482 ulong_t h
= dtd
->dtd_type
& (fp
->ctf_dthashlen
- 1);
483 ctf_dtdef_t
*p
, **q
= &fp
->ctf_dthash
[h
];
484 ctf_dmdef_t
*dmd
, *nmd
;
488 for (p
= *q
; p
!= NULL
; p
= p
->dtd_hash
) {
498 kind
= CTF_INFO_KIND(dtd
->dtd_data
.ctt_info
);
503 for (dmd
= ctf_list_next(&dtd
->dtd_u
.dtu_members
);
504 dmd
!= NULL
; dmd
= nmd
) {
505 if (dmd
->dmd_name
!= NULL
) {
506 len
= strlen(dmd
->dmd_name
) + 1;
507 ctf_free(dmd
->dmd_name
, len
);
508 fp
->ctf_dtstrlen
-= len
;
510 if (kind
!= CTF_K_ENUM
)
511 ctf_ref_dec(fp
, dmd
->dmd_type
);
512 nmd
= ctf_list_next(dmd
);
513 ctf_free(dmd
, sizeof (ctf_dmdef_t
));
517 ctf_ref_dec(fp
, dtd
->dtd_data
.ctt_type
);
518 for (i
= 0; i
< CTF_INFO_VLEN(dtd
->dtd_data
.ctt_info
); i
++)
519 if (dtd
->dtd_u
.dtu_argv
[i
] != 0)
520 ctf_ref_dec(fp
, dtd
->dtd_u
.dtu_argv
[i
]);
521 ctf_free(dtd
->dtd_u
.dtu_argv
, sizeof (ctf_id_t
) *
522 CTF_INFO_VLEN(dtd
->dtd_data
.ctt_info
));
525 ctf_ref_dec(fp
, dtd
->dtd_u
.dtu_arr
.ctr_contents
);
526 ctf_ref_dec(fp
, dtd
->dtd_u
.dtu_arr
.ctr_index
);
529 ctf_ref_dec(fp
, dtd
->dtd_data
.ctt_type
);
535 ctf_ref_dec(fp
, dtd
->dtd_data
.ctt_type
);
540 len
= strlen(dtd
->dtd_name
) + 1;
541 ctf_free(dtd
->dtd_name
, len
);
542 fp
->ctf_dtstrlen
-= len
;
545 ctf_list_delete(&fp
->ctf_dtdefs
, dtd
);
546 ctf_free(dtd
, sizeof (ctf_dtdef_t
));
550 ctf_dtd_lookup(ctf_file_t
*fp
, ctf_id_t type
)
552 ulong_t h
= type
& (fp
->ctf_dthashlen
- 1);
555 if (fp
->ctf_dthash
== NULL
)
558 for (dtd
= fp
->ctf_dthash
[h
]; dtd
!= NULL
; dtd
= dtd
->dtd_hash
) {
559 if (dtd
->dtd_type
== type
)
567 * Discard all of the dynamic type definitions that have been added to the
568 * container since the last call to ctf_update(). We locate such types by
569 * scanning the list and deleting elements that have type IDs greater than
570 * ctf_dtoldid, which is set by ctf_update(), above. Note that to work properly
571 * with our reference counting schemes, we must delete the dynamic list in
575 ctf_discard(ctf_file_t
*fp
)
577 ctf_dtdef_t
*dtd
, *ntd
;
579 if (!(fp
->ctf_flags
& LCTF_RDWR
))
580 return (ctf_set_errno(fp
, ECTF_RDONLY
));
582 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
583 return (0); /* no update required */
585 for (dtd
= ctf_list_prev(&fp
->ctf_dtdefs
); dtd
!= NULL
; dtd
= ntd
) {
586 if (dtd
->dtd_type
<= fp
->ctf_dtoldid
)
587 continue; /* skip types that have been committed */
589 ntd
= ctf_list_prev(dtd
);
590 ctf_dtd_delete(fp
, dtd
);
593 fp
->ctf_dtnextid
= fp
->ctf_dtoldid
+ 1;
594 fp
->ctf_flags
&= ~LCTF_DIRTY
;
600 ctf_add_generic(ctf_file_t
*fp
, uint_t flag
, const char *name
, ctf_dtdef_t
**rp
)
606 if (flag
!= CTF_ADD_NONROOT
&& flag
!= CTF_ADD_ROOT
)
607 return (ctf_set_errno(fp
, EINVAL
));
609 if (!(fp
->ctf_flags
& LCTF_RDWR
))
610 return (ctf_set_errno(fp
, ECTF_RDONLY
));
612 if (CTF_INDEX_TO_TYPE(fp
->ctf_dtnextid
, 1) > CTF_MAX_TYPE
)
613 return (ctf_set_errno(fp
, ECTF_FULL
));
615 if ((dtd
= ctf_alloc(sizeof (ctf_dtdef_t
))) == NULL
)
616 return (ctf_set_errno(fp
, EAGAIN
));
618 if (name
!= NULL
&& (s
= ctf_strdup(name
)) == NULL
) {
619 ctf_free(dtd
, sizeof (ctf_dtdef_t
));
620 return (ctf_set_errno(fp
, EAGAIN
));
623 type
= fp
->ctf_dtnextid
++;
624 type
= CTF_INDEX_TO_TYPE(type
, (fp
->ctf_flags
& LCTF_CHILD
));
626 bzero(dtd
, sizeof (ctf_dtdef_t
));
628 dtd
->dtd_type
= type
;
631 fp
->ctf_dtstrlen
+= strlen(s
) + 1;
633 ctf_dtd_insert(fp
, dtd
);
634 fp
->ctf_flags
|= LCTF_DIRTY
;
641 * When encoding integer sizes, we want to convert a byte count in the range
642 * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
643 * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.
660 ctf_add_encoded(ctf_file_t
*fp
, uint_t flag
,
661 const char *name
, const ctf_encoding_t
*ep
, uint_t kind
)
667 return (ctf_set_errno(fp
, EINVAL
));
669 if ((type
= ctf_add_generic(fp
, flag
, name
, &dtd
)) == CTF_ERR
)
670 return (CTF_ERR
); /* errno is set for us */
672 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(kind
, flag
, 0);
673 dtd
->dtd_data
.ctt_size
= clp2(P2ROUNDUP(ep
->cte_bits
, NBBY
) / NBBY
);
674 dtd
->dtd_u
.dtu_enc
= *ep
;
680 ctf_add_reftype(ctf_file_t
*fp
, uint_t flag
, ctf_id_t ref
, uint_t kind
)
685 if (ref
== CTF_ERR
|| ref
< 0 || ref
> CTF_MAX_TYPE
)
686 return (ctf_set_errno(fp
, EINVAL
));
688 if ((type
= ctf_add_generic(fp
, flag
, NULL
, &dtd
)) == CTF_ERR
)
689 return (CTF_ERR
); /* errno is set for us */
691 ctf_ref_inc(fp
, ref
);
693 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(kind
, flag
, 0);
694 dtd
->dtd_data
.ctt_type
= (ushort_t
)ref
;
700 ctf_add_integer(ctf_file_t
*fp
, uint_t flag
,
701 const char *name
, const ctf_encoding_t
*ep
)
703 return (ctf_add_encoded(fp
, flag
, name
, ep
, CTF_K_INTEGER
));
707 ctf_add_float(ctf_file_t
*fp
, uint_t flag
,
708 const char *name
, const ctf_encoding_t
*ep
)
710 return (ctf_add_encoded(fp
, flag
, name
, ep
, CTF_K_FLOAT
));
714 ctf_add_pointer(ctf_file_t
*fp
, uint_t flag
, ctf_id_t ref
)
716 return (ctf_add_reftype(fp
, flag
, ref
, CTF_K_POINTER
));
720 ctf_add_array(ctf_file_t
*fp
, uint_t flag
, const ctf_arinfo_t
*arp
)
727 return (ctf_set_errno(fp
, EINVAL
));
730 if (ctf_lookup_by_id(&fpd
, arp
->ctr_contents
) == NULL
&&
731 ctf_dtd_lookup(fp
, arp
->ctr_contents
) == NULL
)
732 return (ctf_set_errno(fp
, ECTF_BADID
));
735 if (ctf_lookup_by_id(&fpd
, arp
->ctr_index
) == NULL
&&
736 ctf_dtd_lookup(fp
, arp
->ctr_index
) == NULL
)
737 return (ctf_set_errno(fp
, ECTF_BADID
));
739 if ((type
= ctf_add_generic(fp
, flag
, NULL
, &dtd
)) == CTF_ERR
)
740 return (CTF_ERR
); /* errno is set for us */
742 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(CTF_K_ARRAY
, flag
, 0);
743 dtd
->dtd_data
.ctt_size
= 0;
744 dtd
->dtd_u
.dtu_arr
= *arp
;
745 ctf_ref_inc(fp
, arp
->ctr_contents
);
746 ctf_ref_inc(fp
, arp
->ctr_index
);
752 ctf_set_array(ctf_file_t
*fp
, ctf_id_t type
, const ctf_arinfo_t
*arp
)
755 ctf_dtdef_t
*dtd
= ctf_dtd_lookup(fp
, type
);
757 if (!(fp
->ctf_flags
& LCTF_RDWR
))
758 return (ctf_set_errno(fp
, ECTF_RDONLY
));
760 if (dtd
== NULL
|| CTF_INFO_KIND(dtd
->dtd_data
.ctt_info
) != CTF_K_ARRAY
)
761 return (ctf_set_errno(fp
, ECTF_BADID
));
764 if (ctf_lookup_by_id(&fpd
, arp
->ctr_contents
) == NULL
&&
765 ctf_dtd_lookup(fp
, arp
->ctr_contents
) == NULL
)
766 return (ctf_set_errno(fp
, ECTF_BADID
));
769 if (ctf_lookup_by_id(&fpd
, arp
->ctr_index
) == NULL
&&
770 ctf_dtd_lookup(fp
, arp
->ctr_index
) == NULL
)
771 return (ctf_set_errno(fp
, ECTF_BADID
));
773 ctf_ref_dec(fp
, dtd
->dtd_u
.dtu_arr
.ctr_contents
);
774 ctf_ref_dec(fp
, dtd
->dtd_u
.dtu_arr
.ctr_index
);
775 fp
->ctf_flags
|= LCTF_DIRTY
;
776 dtd
->dtd_u
.dtu_arr
= *arp
;
777 ctf_ref_inc(fp
, arp
->ctr_contents
);
778 ctf_ref_inc(fp
, arp
->ctr_index
);
784 ctf_add_function(ctf_file_t
*fp
, uint_t flag
,
785 const ctf_funcinfo_t
*ctc
, const ctf_id_t
*argv
)
791 ctf_id_t
*vdat
= NULL
;
794 if (ctc
== NULL
|| (ctc
->ctc_flags
& ~CTF_FUNC_VARARG
) != 0 ||
795 (ctc
->ctc_argc
!= 0 && argv
== NULL
))
796 return (ctf_set_errno(fp
, EINVAL
));
798 vlen
= ctc
->ctc_argc
;
799 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
800 vlen
++; /* add trailing zero to indicate varargs (see below) */
802 if (vlen
> CTF_MAX_VLEN
)
803 return (ctf_set_errno(fp
, EOVERFLOW
));
806 if (ctf_lookup_by_id(&fpd
, ctc
->ctc_return
) == NULL
&&
807 ctf_dtd_lookup(fp
, ctc
->ctc_return
) == NULL
)
808 return (ctf_set_errno(fp
, ECTF_BADID
));
810 for (i
= 0; i
< ctc
->ctc_argc
; i
++) {
812 if (ctf_lookup_by_id(&fpd
, argv
[i
]) == NULL
&&
813 ctf_dtd_lookup(fp
, argv
[i
]) == NULL
)
814 return (ctf_set_errno(fp
, ECTF_BADID
));
817 if (vlen
!= 0 && (vdat
= ctf_alloc(sizeof (ctf_id_t
) * vlen
)) == NULL
)
818 return (ctf_set_errno(fp
, EAGAIN
));
820 if ((type
= ctf_add_generic(fp
, flag
, NULL
, &dtd
)) == CTF_ERR
) {
821 ctf_free(vdat
, sizeof (ctf_id_t
) * vlen
);
822 return (CTF_ERR
); /* errno is set for us */
825 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(CTF_K_FUNCTION
, flag
, vlen
);
826 dtd
->dtd_data
.ctt_type
= (ushort_t
)ctc
->ctc_return
;
828 ctf_ref_inc(fp
, ctc
->ctc_return
);
829 for (i
= 0; i
< ctc
->ctc_argc
; i
++)
830 ctf_ref_inc(fp
, argv
[i
]);
832 bcopy(argv
, vdat
, sizeof (ctf_id_t
) * ctc
->ctc_argc
);
833 if (ctc
->ctc_flags
& CTF_FUNC_VARARG
)
834 vdat
[vlen
- 1] = 0; /* add trailing zero to indicate varargs */
835 dtd
->dtd_u
.dtu_argv
= vdat
;
841 ctf_add_struct(ctf_file_t
*fp
, uint_t flag
, const char *name
)
843 ctf_hash_t
*hp
= &fp
->ctf_structs
;
844 ctf_helem_t
*hep
= NULL
;
849 hep
= ctf_hash_lookup(hp
, fp
, name
, strlen(name
));
851 if (hep
!= NULL
&& ctf_type_kind(fp
, hep
->h_type
) == CTF_K_FORWARD
)
852 dtd
= ctf_dtd_lookup(fp
, type
= hep
->h_type
);
853 else if ((type
= ctf_add_generic(fp
, flag
, name
, &dtd
)) == CTF_ERR
)
854 return (CTF_ERR
); /* errno is set for us */
856 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(CTF_K_STRUCT
, flag
, 0);
857 dtd
->dtd_data
.ctt_size
= 0;
863 ctf_add_union(ctf_file_t
*fp
, uint_t flag
, const char *name
)
865 ctf_hash_t
*hp
= &fp
->ctf_unions
;
866 ctf_helem_t
*hep
= NULL
;
871 hep
= ctf_hash_lookup(hp
, fp
, name
, strlen(name
));
873 if (hep
!= NULL
&& ctf_type_kind(fp
, hep
->h_type
) == CTF_K_FORWARD
)
874 dtd
= ctf_dtd_lookup(fp
, type
= hep
->h_type
);
875 else if ((type
= ctf_add_generic(fp
, flag
, name
, &dtd
)) == CTF_ERR
)
876 return (CTF_ERR
); /* errno is set for us */
878 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(CTF_K_UNION
, flag
, 0);
879 dtd
->dtd_data
.ctt_size
= 0;
885 ctf_add_enum(ctf_file_t
*fp
, uint_t flag
, const char *name
)
887 ctf_hash_t
*hp
= &fp
->ctf_enums
;
888 ctf_helem_t
*hep
= NULL
;
893 hep
= ctf_hash_lookup(hp
, fp
, name
, strlen(name
));
895 if (hep
!= NULL
&& ctf_type_kind(fp
, hep
->h_type
) == CTF_K_FORWARD
)
896 dtd
= ctf_dtd_lookup(fp
, type
= hep
->h_type
);
897 else if ((type
= ctf_add_generic(fp
, flag
, name
, &dtd
)) == CTF_ERR
)
898 return (CTF_ERR
); /* errno is set for us */
900 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(CTF_K_ENUM
, flag
, 0);
901 dtd
->dtd_data
.ctt_size
= fp
->ctf_dmodel
->ctd_int
;
907 ctf_add_forward(ctf_file_t
*fp
, uint_t flag
, const char *name
, uint_t kind
)
916 hp
= &fp
->ctf_structs
;
919 hp
= &fp
->ctf_unions
;
925 return (ctf_set_errno(fp
, ECTF_NOTSUE
));
929 * If the type is already defined or exists as a forward tag, just
930 * return the ctf_id_t of the existing definition.
932 if (name
!= NULL
&& (hep
= ctf_hash_lookup(hp
,
933 fp
, name
, strlen(name
))) != NULL
)
934 return (hep
->h_type
);
936 if ((type
= ctf_add_generic(fp
, flag
, name
, &dtd
)) == CTF_ERR
)
937 return (CTF_ERR
); /* errno is set for us */
939 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(CTF_K_FORWARD
, flag
, 0);
940 dtd
->dtd_data
.ctt_type
= kind
;
946 ctf_add_typedef(ctf_file_t
*fp
, uint_t flag
, const char *name
, ctf_id_t ref
)
953 if (ref
== CTF_ERR
|| (ctf_lookup_by_id(&fpd
, ref
) == NULL
&&
954 ctf_dtd_lookup(fp
, ref
) == NULL
))
955 return (ctf_set_errno(fp
, EINVAL
));
957 if ((type
= ctf_add_generic(fp
, flag
, name
, &dtd
)) == CTF_ERR
)
958 return (CTF_ERR
); /* errno is set for us */
960 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(CTF_K_TYPEDEF
, flag
, 0);
961 dtd
->dtd_data
.ctt_type
= (ushort_t
)ref
;
962 ctf_ref_inc(fp
, ref
);
968 ctf_add_volatile(ctf_file_t
*fp
, uint_t flag
, ctf_id_t ref
)
970 return (ctf_add_reftype(fp
, flag
, ref
, CTF_K_VOLATILE
));
974 ctf_add_const(ctf_file_t
*fp
, uint_t flag
, ctf_id_t ref
)
976 return (ctf_add_reftype(fp
, flag
, ref
, CTF_K_CONST
));
980 ctf_add_restrict(ctf_file_t
*fp
, uint_t flag
, ctf_id_t ref
)
982 return (ctf_add_reftype(fp
, flag
, ref
, CTF_K_RESTRICT
));
986 ctf_add_enumerator(ctf_file_t
*fp
, ctf_id_t enid
, const char *name
, int value
)
988 ctf_dtdef_t
*dtd
= ctf_dtd_lookup(fp
, enid
);
991 uint_t kind
, vlen
, root
;
995 return (ctf_set_errno(fp
, EINVAL
));
997 if (!(fp
->ctf_flags
& LCTF_RDWR
))
998 return (ctf_set_errno(fp
, ECTF_RDONLY
));
1001 return (ctf_set_errno(fp
, ECTF_BADID
));
1003 kind
= CTF_INFO_KIND(dtd
->dtd_data
.ctt_info
);
1004 root
= CTF_INFO_ISROOT(dtd
->dtd_data
.ctt_info
);
1005 vlen
= CTF_INFO_VLEN(dtd
->dtd_data
.ctt_info
);
1007 if (kind
!= CTF_K_ENUM
)
1008 return (ctf_set_errno(fp
, ECTF_NOTENUM
));
1010 if (vlen
== CTF_MAX_VLEN
)
1011 return (ctf_set_errno(fp
, ECTF_DTFULL
));
1013 for (dmd
= ctf_list_next(&dtd
->dtd_u
.dtu_members
);
1014 dmd
!= NULL
; dmd
= ctf_list_next(dmd
)) {
1015 if (strcmp(dmd
->dmd_name
, name
) == 0)
1016 return (ctf_set_errno(fp
, ECTF_DUPMEMBER
));
1019 if ((dmd
= ctf_alloc(sizeof (ctf_dmdef_t
))) == NULL
)
1020 return (ctf_set_errno(fp
, EAGAIN
));
1022 if ((s
= ctf_strdup(name
)) == NULL
) {
1023 ctf_free(dmd
, sizeof (ctf_dmdef_t
));
1024 return (ctf_set_errno(fp
, EAGAIN
));
1028 dmd
->dmd_type
= CTF_ERR
;
1029 dmd
->dmd_offset
= 0;
1030 dmd
->dmd_value
= value
;
1032 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(kind
, root
, vlen
+ 1);
1033 ctf_list_append(&dtd
->dtd_u
.dtu_members
, dmd
);
1035 fp
->ctf_dtstrlen
+= strlen(s
) + 1;
1036 fp
->ctf_flags
|= LCTF_DIRTY
;
1042 ctf_add_member(ctf_file_t
*fp
, ctf_id_t souid
, const char *name
, ctf_id_t type
)
1044 ctf_dtdef_t
*dtd
= ctf_dtd_lookup(fp
, souid
);
1047 ssize_t msize
, malign
, ssize
;
1048 uint_t kind
, vlen
, root
;
1051 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1052 return (ctf_set_errno(fp
, ECTF_RDONLY
));
1055 return (ctf_set_errno(fp
, ECTF_BADID
));
1057 kind
= CTF_INFO_KIND(dtd
->dtd_data
.ctt_info
);
1058 root
= CTF_INFO_ISROOT(dtd
->dtd_data
.ctt_info
);
1059 vlen
= CTF_INFO_VLEN(dtd
->dtd_data
.ctt_info
);
1061 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
1062 return (ctf_set_errno(fp
, ECTF_NOTSOU
));
1064 if (vlen
== CTF_MAX_VLEN
)
1065 return (ctf_set_errno(fp
, ECTF_DTFULL
));
1068 for (dmd
= ctf_list_next(&dtd
->dtd_u
.dtu_members
);
1069 dmd
!= NULL
; dmd
= ctf_list_next(dmd
)) {
1070 if (dmd
->dmd_name
!= NULL
&&
1071 strcmp(dmd
->dmd_name
, name
) == 0)
1072 return (ctf_set_errno(fp
, ECTF_DUPMEMBER
));
1076 if ((msize
= ctf_type_size(fp
, type
)) == CTF_ERR
||
1077 (malign
= ctf_type_align(fp
, type
)) == CTF_ERR
)
1078 return (CTF_ERR
); /* errno is set for us */
1080 if ((dmd
= ctf_alloc(sizeof (ctf_dmdef_t
))) == NULL
)
1081 return (ctf_set_errno(fp
, EAGAIN
));
1083 if (name
!= NULL
&& (s
= ctf_strdup(name
)) == NULL
) {
1084 ctf_free(dmd
, sizeof (ctf_dmdef_t
));
1085 return (ctf_set_errno(fp
, EAGAIN
));
1089 dmd
->dmd_type
= type
;
1090 dmd
->dmd_value
= -1;
1092 if (kind
== CTF_K_STRUCT
&& vlen
!= 0) {
1093 ctf_dmdef_t
*lmd
= ctf_list_prev(&dtd
->dtd_u
.dtu_members
);
1094 ctf_id_t ltype
= ctf_type_resolve(fp
, lmd
->dmd_type
);
1095 size_t off
= lmd
->dmd_offset
;
1097 ctf_encoding_t linfo
;
1100 if (ctf_type_encoding(fp
, ltype
, &linfo
) != CTF_ERR
)
1101 off
+= linfo
.cte_bits
;
1102 else if ((lsize
= ctf_type_size(fp
, ltype
)) != CTF_ERR
)
1103 off
+= lsize
* NBBY
;
1106 * Round up the offset of the end of the last member to the
1107 * next byte boundary, convert 'off' to bytes, and then round
1108 * it up again to the next multiple of the alignment required
1109 * by the new member. Finally, convert back to bits and store
1110 * the result in dmd_offset. Technically we could do more
1111 * efficient packing if the new member is a bit-field, but
1112 * we're the "compiler" and ANSI says we can do as we choose.
1114 off
= roundup(off
, NBBY
) / NBBY
;
1115 off
= roundup(off
, MAX(malign
, 1));
1116 dmd
->dmd_offset
= off
* NBBY
;
1117 ssize
= off
+ msize
;
1119 dmd
->dmd_offset
= 0;
1120 ssize
= ctf_get_ctt_size(fp
, &dtd
->dtd_data
, NULL
, NULL
);
1121 ssize
= MAX(ssize
, msize
);
1124 if (ssize
> CTF_MAX_SIZE
) {
1125 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1126 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI(ssize
);
1127 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO(ssize
);
1129 dtd
->dtd_data
.ctt_size
= (ushort_t
)ssize
;
1131 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(kind
, root
, vlen
+ 1);
1132 ctf_list_append(&dtd
->dtd_u
.dtu_members
, dmd
);
1135 fp
->ctf_dtstrlen
+= strlen(s
) + 1;
1137 ctf_ref_inc(fp
, type
);
1138 fp
->ctf_flags
|= LCTF_DIRTY
;
1143 * This removes a type from the dynamic section. This will fail if the type is
1144 * referenced by another type. Note that the CTF ID is never reused currently by
1145 * CTF. Note that if this container is a parent container then we just outright
1146 * refuse to remove the type. There currently is no notion of searching for the
1147 * ctf_dtdef_t in parent containers. If there is, then this constraint could
1148 * become finer grained.
1151 ctf_delete_type(ctf_file_t
*fp
, ctf_id_t type
)
1154 ctf_dtdef_t
*dtd
= ctf_dtd_lookup(fp
, type
);
1156 if (!(fp
->ctf_flags
& LCTF_RDWR
))
1157 return (ctf_set_errno(fp
, ECTF_RDONLY
));
1160 * We want to give as useful an errno as possible. That means that we
1161 * want to distinguish between a type which does not exist and one for
1162 * which the type is not dynamic.
1165 if (ctf_lookup_by_id(&fpd
, type
) == NULL
&&
1166 ctf_dtd_lookup(fp
, type
) == NULL
)
1167 return (CTF_ERR
); /* errno is set for us */
1170 return (ctf_set_errno(fp
, ECTF_NOTDYN
));
1172 if (dtd
->dtd_ref
!= 0 || fp
->ctf_refcnt
> 1)
1173 return (ctf_set_errno(fp
, ECTF_REFERENCED
));
1175 ctf_dtd_delete(fp
, dtd
);
1176 fp
->ctf_flags
|= LCTF_DIRTY
;
1181 enumcmp(const char *name
, int value
, void *arg
)
1183 ctf_bundle_t
*ctb
= arg
;
1186 return (ctf_enum_value(ctb
->ctb_file
, ctb
->ctb_type
,
1187 name
, &bvalue
) == CTF_ERR
|| value
!= bvalue
);
1191 enumadd(const char *name
, int value
, void *arg
)
1193 ctf_bundle_t
*ctb
= arg
;
1195 return (ctf_add_enumerator(ctb
->ctb_file
, ctb
->ctb_type
,
1196 name
, value
) == CTF_ERR
);
1201 membcmp(const char *name
, ctf_id_t type
, ulong_t offset
, void *arg
)
1203 ctf_bundle_t
*ctb
= arg
;
1206 return (ctf_member_info(ctb
->ctb_file
, ctb
->ctb_type
,
1207 name
, &ctm
) == CTF_ERR
|| ctm
.ctm_offset
!= offset
);
1211 membadd(const char *name
, ctf_id_t type
, ulong_t offset
, void *arg
)
1213 ctf_bundle_t
*ctb
= arg
;
1217 if ((dmd
= ctf_alloc(sizeof (ctf_dmdef_t
))) == NULL
)
1218 return (ctf_set_errno(ctb
->ctb_file
, EAGAIN
));
1220 if (name
!= NULL
&& (s
= ctf_strdup(name
)) == NULL
) {
1221 ctf_free(dmd
, sizeof (ctf_dmdef_t
));
1222 return (ctf_set_errno(ctb
->ctb_file
, EAGAIN
));
1226 * For now, dmd_type is copied as the src_fp's type; it is reset to an
1227 * equivalent dst_fp type by a final loop in ctf_add_type(), below.
1230 dmd
->dmd_type
= type
;
1231 dmd
->dmd_offset
= offset
;
1232 dmd
->dmd_value
= -1;
1234 ctf_list_append(&ctb
->ctb_dtd
->dtd_u
.dtu_members
, dmd
);
1237 ctb
->ctb_file
->ctf_dtstrlen
+= strlen(s
) + 1;
1239 ctb
->ctb_file
->ctf_flags
|= LCTF_DIRTY
;
1244 * The ctf_add_type routine is used to copy a type from a source CTF container
1245 * to a dynamic destination container. This routine operates recursively by
1246 * following the source type's links and embedded member types. If the
1247 * destination container already contains a named type which has the same
1248 * attributes, then we succeed and return this type but no changes occur.
1251 ctf_add_type(ctf_file_t
*dst_fp
, ctf_file_t
*src_fp
, ctf_id_t src_type
)
1253 ctf_id_t dst_type
= CTF_ERR
;
1254 uint_t dst_kind
= CTF_K_UNKNOWN
;
1256 const ctf_type_t
*tp
;
1258 uint_t kind
, flag
, vlen
;
1260 ctf_bundle_t src
, dst
;
1261 ctf_encoding_t src_en
, dst_en
;
1262 ctf_arinfo_t src_ar
, dst_ar
;
1271 if (dst_fp
== src_fp
)
1274 if (!(dst_fp
->ctf_flags
& LCTF_RDWR
))
1275 return (ctf_set_errno(dst_fp
, ECTF_RDONLY
));
1277 if ((tp
= ctf_lookup_by_id(&src_fp
, src_type
)) == NULL
)
1278 return (ctf_set_errno(dst_fp
, ctf_errno(src_fp
)));
1280 name
= ctf_strptr(src_fp
, tp
->ctt_name
);
1281 kind
= LCTF_INFO_KIND(src_fp
, tp
->ctt_info
);
1282 flag
= LCTF_INFO_ROOT(src_fp
, tp
->ctt_info
);
1283 vlen
= LCTF_INFO_VLEN(src_fp
, tp
->ctt_info
);
1287 hp
= &dst_fp
->ctf_structs
;
1290 hp
= &dst_fp
->ctf_unions
;
1293 hp
= &dst_fp
->ctf_enums
;
1296 hp
= &dst_fp
->ctf_names
;
1301 * If the source type has a name and is a root type (visible at the
1302 * top-level scope), lookup the name in the destination container and
1303 * verify that it is of the same kind before we do anything else.
1305 if ((flag
& CTF_ADD_ROOT
) && name
[0] != '\0' &&
1306 (hep
= ctf_hash_lookup(hp
, dst_fp
, name
, strlen(name
))) != NULL
) {
1307 dst_type
= (ctf_id_t
)hep
->h_type
;
1308 dst_kind
= ctf_type_kind(dst_fp
, dst_type
);
1312 * If an identically named dst_type exists, fail with ECTF_CONFLICT
1313 * unless dst_type is a forward declaration and src_type is a struct,
1314 * union, or enum (i.e. the definition of the previous forward decl).
1316 if (dst_type
!= CTF_ERR
&& dst_kind
!= kind
&& (
1317 dst_kind
!= CTF_K_FORWARD
|| (kind
!= CTF_K_ENUM
&&
1318 kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)))
1319 return (ctf_set_errno(dst_fp
, ECTF_CONFLICT
));
1322 * If the non-empty name was not found in the appropriate hash, search
1323 * the list of pending dynamic definitions that are not yet committed.
1324 * If a matching name and kind are found, assume this is the type that
1325 * we are looking for. This is necessary to permit ctf_add_type() to
1326 * operate recursively on entities such as a struct that contains a
1327 * pointer member that refers to the same struct type.
1329 if (dst_type
== CTF_ERR
&& name
[0] != '\0') {
1330 for (dtd
= ctf_list_prev(&dst_fp
->ctf_dtdefs
); dtd
!= NULL
&&
1331 dtd
->dtd_type
> dst_fp
->ctf_dtoldid
;
1332 dtd
= ctf_list_prev(dtd
)) {
1333 if (CTF_INFO_KIND(dtd
->dtd_data
.ctt_info
) == kind
&&
1334 dtd
->dtd_name
!= NULL
&&
1335 strcmp(dtd
->dtd_name
, name
) == 0)
1336 return (dtd
->dtd_type
);
1340 src
.ctb_file
= src_fp
;
1341 src
.ctb_type
= src_type
;
1344 dst
.ctb_file
= dst_fp
;
1345 dst
.ctb_type
= dst_type
;
1349 * Now perform kind-specific processing. If dst_type is CTF_ERR, then
1350 * we add a new type with the same properties as src_type to dst_fp.
1351 * If dst_type is not CTF_ERR, then we verify that dst_type has the
1352 * same attributes as src_type. We recurse for embedded references.
1357 if (ctf_type_encoding(src_fp
, src_type
, &src_en
) != 0)
1358 return (ctf_set_errno(dst_fp
, ctf_errno(src_fp
)));
1360 if (dst_type
!= CTF_ERR
) {
1361 if (ctf_type_encoding(dst_fp
, dst_type
, &dst_en
) != 0)
1362 return (CTF_ERR
); /* errno is set for us */
1364 if (bcmp(&src_en
, &dst_en
, sizeof (ctf_encoding_t
)))
1365 return (ctf_set_errno(dst_fp
, ECTF_CONFLICT
));
1367 } else if (kind
== CTF_K_INTEGER
) {
1368 dst_type
= ctf_add_integer(dst_fp
, flag
, name
, &src_en
);
1370 dst_type
= ctf_add_float(dst_fp
, flag
, name
, &src_en
);
1374 case CTF_K_VOLATILE
:
1376 case CTF_K_RESTRICT
:
1377 src_type
= ctf_type_reference(src_fp
, src_type
);
1378 src_type
= ctf_add_type(dst_fp
, src_fp
, src_type
);
1380 if (src_type
== CTF_ERR
)
1381 return (CTF_ERR
); /* errno is set for us */
1383 dst_type
= ctf_add_reftype(dst_fp
, flag
, src_type
, kind
);
1387 if (ctf_array_info(src_fp
, src_type
, &src_ar
) == CTF_ERR
)
1388 return (ctf_set_errno(dst_fp
, ctf_errno(src_fp
)));
1390 src_ar
.ctr_contents
=
1391 ctf_add_type(dst_fp
, src_fp
, src_ar
.ctr_contents
);
1393 ctf_add_type(dst_fp
, src_fp
, src_ar
.ctr_index
);
1394 src_ar
.ctr_nelems
= src_ar
.ctr_nelems
;
1396 if (src_ar
.ctr_contents
== CTF_ERR
||
1397 src_ar
.ctr_index
== CTF_ERR
)
1398 return (CTF_ERR
); /* errno is set for us */
1400 if (dst_type
!= CTF_ERR
) {
1401 if (ctf_array_info(dst_fp
, dst_type
, &dst_ar
) != 0)
1402 return (CTF_ERR
); /* errno is set for us */
1404 if (bcmp(&src_ar
, &dst_ar
, sizeof (ctf_arinfo_t
)))
1405 return (ctf_set_errno(dst_fp
, ECTF_CONFLICT
));
1407 dst_type
= ctf_add_array(dst_fp
, flag
, &src_ar
);
1410 case CTF_K_FUNCTION
:
1411 ctc
.ctc_return
= ctf_add_type(dst_fp
, src_fp
, tp
->ctt_type
);
1415 if (ctc
.ctc_return
== CTF_ERR
)
1416 return (CTF_ERR
); /* errno is set for us */
1418 dst_type
= ctf_add_function(dst_fp
, flag
, &ctc
, NULL
);
1427 * Technically to match a struct or union we need to check both
1428 * ways (src members vs. dst, dst members vs. src) but we make
1429 * this more optimal by only checking src vs. dst and comparing
1430 * the total size of the structure (which we must do anyway)
1431 * which covers the possibility of dst members not in src.
1432 * This optimization can be defeated for unions, but is so
1433 * pathological as to render it irrelevant for our purposes.
1435 if (dst_type
!= CTF_ERR
&& dst_kind
!= CTF_K_FORWARD
) {
1436 if (ctf_type_size(src_fp
, src_type
) !=
1437 ctf_type_size(dst_fp
, dst_type
))
1438 return (ctf_set_errno(dst_fp
, ECTF_CONFLICT
));
1440 if (ctf_member_iter(src_fp
, src_type
, membcmp
, &dst
))
1441 return (ctf_set_errno(dst_fp
, ECTF_CONFLICT
));
1447 * Unlike the other cases, copying structs and unions is done
1448 * manually so as to avoid repeated lookups in ctf_add_member
1449 * and to ensure the exact same member offsets as in src_type.
1451 dst_type
= ctf_add_generic(dst_fp
, flag
, name
, &dtd
);
1452 if (dst_type
== CTF_ERR
)
1453 return (CTF_ERR
); /* errno is set for us */
1455 dst
.ctb_type
= dst_type
;
1458 if (ctf_member_iter(src_fp
, src_type
, membadd
, &dst
) != 0)
1459 errs
++; /* increment errs and fail at bottom of case */
1461 if ((size
= ctf_type_size(src_fp
, src_type
)) > CTF_MAX_SIZE
) {
1462 dtd
->dtd_data
.ctt_size
= CTF_LSIZE_SENT
;
1463 dtd
->dtd_data
.ctt_lsizehi
= CTF_SIZE_TO_LSIZE_HI(size
);
1464 dtd
->dtd_data
.ctt_lsizelo
= CTF_SIZE_TO_LSIZE_LO(size
);
1466 dtd
->dtd_data
.ctt_size
= (ushort_t
)size
;
1468 dtd
->dtd_data
.ctt_info
= CTF_TYPE_INFO(kind
, flag
, vlen
);
1471 * Make a final pass through the members changing each dmd_type
1472 * (a src_fp type) to an equivalent type in dst_fp. We pass
1473 * through all members, leaving any that fail set to CTF_ERR.
1475 for (dmd
= ctf_list_next(&dtd
->dtd_u
.dtu_members
);
1476 dmd
!= NULL
; dmd
= ctf_list_next(dmd
)) {
1477 if ((dmd
->dmd_type
= ctf_add_type(dst_fp
, src_fp
,
1478 dmd
->dmd_type
)) == CTF_ERR
)
1483 return (CTF_ERR
); /* errno is set for us */
1486 * Now that we know that we can't fail, we go through and bump
1487 * all the reference counts on the member types.
1489 for (dmd
= ctf_list_next(&dtd
->dtd_u
.dtu_members
);
1490 dmd
!= NULL
; dmd
= ctf_list_next(dmd
))
1491 ctf_ref_inc(dst_fp
, dmd
->dmd_type
);
1496 if (dst_type
!= CTF_ERR
&& dst_kind
!= CTF_K_FORWARD
) {
1497 if (ctf_enum_iter(src_fp
, src_type
, enumcmp
, &dst
) ||
1498 ctf_enum_iter(dst_fp
, dst_type
, enumcmp
, &src
))
1499 return (ctf_set_errno(dst_fp
, ECTF_CONFLICT
));
1501 dst_type
= ctf_add_enum(dst_fp
, flag
, name
);
1502 if ((dst
.ctb_type
= dst_type
) == CTF_ERR
||
1503 ctf_enum_iter(src_fp
, src_type
, enumadd
, &dst
))
1504 return (CTF_ERR
); /* errno is set for us */
1509 if (dst_type
== CTF_ERR
) {
1510 dst_type
= ctf_add_forward(dst_fp
,
1511 flag
, name
, CTF_K_STRUCT
); /* assume STRUCT */
1516 src_type
= ctf_type_reference(src_fp
, src_type
);
1517 src_type
= ctf_add_type(dst_fp
, src_fp
, src_type
);
1519 if (src_type
== CTF_ERR
)
1520 return (CTF_ERR
); /* errno is set for us */
1523 * If dst_type is not CTF_ERR at this point, we should check if
1524 * ctf_type_reference(dst_fp, dst_type) != src_type and if so
1525 * fail with ECTF_CONFLICT. However, this causes problems with
1526 * <sys/types.h> typedefs that vary based on things like if
1527 * _ILP32x then pid_t is int otherwise long. We therefore omit
1528 * this check and assume that if the identically named typedef
1529 * already exists in dst_fp, it is correct or equivalent.
1531 if (dst_type
== CTF_ERR
) {
1532 dst_type
= ctf_add_typedef(dst_fp
, flag
,
1538 return (ctf_set_errno(dst_fp
, ECTF_CORRUPT
));