xref: /titanic_50/usr/src/common/ctf/ctf_create.c (revision e4586ebf2f01666696316c178da243993b1a0c04)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*e4586ebfSmws 
237c478bd9Sstevel@tonic-gate /*
24*e4586ebfSmws  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/mman.h>
337c478bd9Sstevel@tonic-gate #include <ctf_impl.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * This static string is used as the template for initially populating a
377c478bd9Sstevel@tonic-gate  * dynamic container's string table.  We always store \0 in the first byte,
387c478bd9Sstevel@tonic-gate  * and we use the generic string "PARENT" to mark this container's parent
397c478bd9Sstevel@tonic-gate  * if one is associated with the container using ctf_import().
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT";
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * To create an empty CTF container, we just declare a zeroed header and call
457c478bd9Sstevel@tonic-gate  * ctf_bufopen() on it.  If ctf_bufopen succeeds, we mark the new container r/w
467c478bd9Sstevel@tonic-gate  * and initialize the dynamic members.  We set dtstrlen to 1 to reserve the
477c478bd9Sstevel@tonic-gate  * first byte of the string table for a \0 byte, and we start assigning type
487c478bd9Sstevel@tonic-gate  * IDs at 1 because type ID 0 is used as a sentinel.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate ctf_file_t *
517c478bd9Sstevel@tonic-gate ctf_create(int *errp)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } };
547c478bd9Sstevel@tonic-gate 
55*e4586ebfSmws 	const ulong_t hashlen = 128;
56*e4586ebfSmws 	ctf_dtdef_t **hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *));
577c478bd9Sstevel@tonic-gate 	ctf_sect_t cts;
587c478bd9Sstevel@tonic-gate 	ctf_file_t *fp;
597c478bd9Sstevel@tonic-gate 
60*e4586ebfSmws 	if (hash == NULL)
61*e4586ebfSmws 		return (ctf_set_open_errno(errp, EAGAIN));
62*e4586ebfSmws 
637c478bd9Sstevel@tonic-gate 	cts.cts_name = _CTF_SECTION;
647c478bd9Sstevel@tonic-gate 	cts.cts_type = SHT_PROGBITS;
657c478bd9Sstevel@tonic-gate 	cts.cts_flags = 0;
667c478bd9Sstevel@tonic-gate 	cts.cts_data = &hdr;
677c478bd9Sstevel@tonic-gate 	cts.cts_size = sizeof (hdr);
687c478bd9Sstevel@tonic-gate 	cts.cts_entsize = 1;
697c478bd9Sstevel@tonic-gate 	cts.cts_offset = 0;
707c478bd9Sstevel@tonic-gate 
71*e4586ebfSmws 	if ((fp = ctf_bufopen(&cts, NULL, NULL, errp)) == NULL) {
72*e4586ebfSmws 		ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *));
73*e4586ebfSmws 		return (NULL);
74*e4586ebfSmws 	}
75*e4586ebfSmws 
767c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_RDWR;
77*e4586ebfSmws 	fp->ctf_dthashlen = hashlen;
78*e4586ebfSmws 	bzero(hash, hashlen * sizeof (ctf_dtdef_t *));
79*e4586ebfSmws 	fp->ctf_dthash = hash;
807c478bd9Sstevel@tonic-gate 	fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE);
817c478bd9Sstevel@tonic-gate 	fp->ctf_dtnextid = 1;
827c478bd9Sstevel@tonic-gate 	fp->ctf_dtoldid = 0;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	return (fp);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static uchar_t *
887c478bd9Sstevel@tonic-gate ctf_copy_smembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
917c478bd9Sstevel@tonic-gate 	ctf_member_t ctm;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
947c478bd9Sstevel@tonic-gate 		if (dmd->dmd_name) {
957c478bd9Sstevel@tonic-gate 			ctm.ctm_name = soff;
967c478bd9Sstevel@tonic-gate 			soff += strlen(dmd->dmd_name) + 1;
977c478bd9Sstevel@tonic-gate 		} else
987c478bd9Sstevel@tonic-gate 			ctm.ctm_name = 0;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		ctm.ctm_type = (ushort_t)dmd->dmd_type;
1017c478bd9Sstevel@tonic-gate 		ctm.ctm_offset = (ushort_t)dmd->dmd_offset;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		bcopy(&ctm, t, sizeof (ctm));
1047c478bd9Sstevel@tonic-gate 		t += sizeof (ctm);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	return (t);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static uchar_t *
1117c478bd9Sstevel@tonic-gate ctf_copy_lmembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1147c478bd9Sstevel@tonic-gate 	ctf_lmember_t ctlm;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
1177c478bd9Sstevel@tonic-gate 		if (dmd->dmd_name) {
1187c478bd9Sstevel@tonic-gate 			ctlm.ctlm_name = soff;
1197c478bd9Sstevel@tonic-gate 			soff += strlen(dmd->dmd_name) + 1;
1207c478bd9Sstevel@tonic-gate 		} else
1217c478bd9Sstevel@tonic-gate 			ctlm.ctlm_name = 0;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		ctlm.ctlm_type = (ushort_t)dmd->dmd_type;
1247c478bd9Sstevel@tonic-gate 		ctlm.ctlm_pad = 0;
1257c478bd9Sstevel@tonic-gate 		ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset);
1267c478bd9Sstevel@tonic-gate 		ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		bcopy(&ctlm, t, sizeof (ctlm));
1297c478bd9Sstevel@tonic-gate 		t += sizeof (ctlm);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	return (t);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static uchar_t *
1367c478bd9Sstevel@tonic-gate ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1397c478bd9Sstevel@tonic-gate 	ctf_enum_t cte;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
1427c478bd9Sstevel@tonic-gate 		cte.cte_name = soff;
1437c478bd9Sstevel@tonic-gate 		cte.cte_value = dmd->dmd_value;
1447c478bd9Sstevel@tonic-gate 		soff += strlen(dmd->dmd_name) + 1;
1457c478bd9Sstevel@tonic-gate 		bcopy(&cte, t, sizeof (cte));
1467c478bd9Sstevel@tonic-gate 		t += sizeof (cte);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	return (t);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static uchar_t *
1537c478bd9Sstevel@tonic-gate ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1567c478bd9Sstevel@tonic-gate 	size_t len;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
1597c478bd9Sstevel@tonic-gate 		if (dmd->dmd_name == NULL)
1607c478bd9Sstevel@tonic-gate 			continue; /* skip anonymous members */
1617c478bd9Sstevel@tonic-gate 		len = strlen(dmd->dmd_name) + 1;
1627c478bd9Sstevel@tonic-gate 		bcopy(dmd->dmd_name, s, len);
1637c478bd9Sstevel@tonic-gate 		s += len;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	return (s);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * If the specified CTF container is writable and has been modified, reload
1717c478bd9Sstevel@tonic-gate  * this container with the updated type definitions.  In order to make this
1727c478bd9Sstevel@tonic-gate  * code and the rest of libctf as simple as possible, we perform updates by
1737c478bd9Sstevel@tonic-gate  * taking the dynamic type definitions and creating an in-memory CTF file
1747c478bd9Sstevel@tonic-gate  * containing the definitions, and then call ctf_bufopen() on it.  This not
1757c478bd9Sstevel@tonic-gate  * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest
1767c478bd9Sstevel@tonic-gate  * of the library code with different lookup paths for static and dynamic
1777c478bd9Sstevel@tonic-gate  * type definitions.  We are therefore optimizing greatly for lookup over
1787c478bd9Sstevel@tonic-gate  * update, which we assume will be an uncommon operation.  We perform one
1797c478bd9Sstevel@tonic-gate  * extra trick here for the benefit of callers and to keep our code simple:
1807c478bd9Sstevel@tonic-gate  * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp
1817c478bd9Sstevel@tonic-gate  * constant for the caller, so after ctf_bufopen() returns, we use bcopy to
1827c478bd9Sstevel@tonic-gate  * swap the interior of the old and new ctf_file_t's, and then free the old.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate int
1857c478bd9Sstevel@tonic-gate ctf_update(ctf_file_t *fp)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	ctf_file_t ofp, *nfp;
1887c478bd9Sstevel@tonic-gate 	ctf_header_t hdr;
1897c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
1907c478bd9Sstevel@tonic-gate 	ctf_sect_t cts;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	uchar_t *s, *s0, *t;
1937c478bd9Sstevel@tonic-gate 	size_t size;
1947c478bd9Sstevel@tonic-gate 	void *buf;
1957c478bd9Sstevel@tonic-gate 	int err;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
1987c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_DIRTY))
2017c478bd9Sstevel@tonic-gate 		return (0); /* no update required */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/*
2047c478bd9Sstevel@tonic-gate 	 * Fill in an initial CTF header.  We will leave the label, object,
2057c478bd9Sstevel@tonic-gate 	 * and function sections empty and only output a header, type section,
2067c478bd9Sstevel@tonic-gate 	 * and string table.  The type section begins at a 4-byte aligned
2077c478bd9Sstevel@tonic-gate 	 * boundary past the CTF header itself (at relative offset zero).
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	bzero(&hdr, sizeof (hdr));
2107c478bd9Sstevel@tonic-gate 	hdr.cth_magic = CTF_MAGIC;
2117c478bd9Sstevel@tonic-gate 	hdr.cth_version = CTF_VERSION;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (fp->ctf_flags & LCTF_CHILD)
2147c478bd9Sstevel@tonic-gate 		hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/*
2177c478bd9Sstevel@tonic-gate 	 * Iterate through the dynamic type definition list and compute the
2187c478bd9Sstevel@tonic-gate 	 * size of the CTF type section we will need to generate.
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 	for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs);
2217c478bd9Sstevel@tonic-gate 	    dtd != NULL; dtd = ctf_list_next(dtd)) {
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2247c478bd9Sstevel@tonic-gate 		uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
2277c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_stype_t);
2287c478bd9Sstevel@tonic-gate 		else
2297c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_type_t);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		switch (kind) {
2327c478bd9Sstevel@tonic-gate 		case CTF_K_INTEGER:
2337c478bd9Sstevel@tonic-gate 		case CTF_K_FLOAT:
2347c478bd9Sstevel@tonic-gate 			size += sizeof (uint_t);
2357c478bd9Sstevel@tonic-gate 			break;
2367c478bd9Sstevel@tonic-gate 		case CTF_K_ARRAY:
2377c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_array_t);
2387c478bd9Sstevel@tonic-gate 			break;
2397c478bd9Sstevel@tonic-gate 		case CTF_K_FUNCTION:
2407c478bd9Sstevel@tonic-gate 			size += sizeof (ushort_t) * (vlen + (vlen & 1));
2417c478bd9Sstevel@tonic-gate 			break;
2427c478bd9Sstevel@tonic-gate 		case CTF_K_STRUCT:
2437c478bd9Sstevel@tonic-gate 		case CTF_K_UNION:
2447c478bd9Sstevel@tonic-gate 			if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
2457c478bd9Sstevel@tonic-gate 				size += sizeof (ctf_member_t) * vlen;
2467c478bd9Sstevel@tonic-gate 			else
2477c478bd9Sstevel@tonic-gate 				size += sizeof (ctf_lmember_t) * vlen;
2487c478bd9Sstevel@tonic-gate 			break;
2497c478bd9Sstevel@tonic-gate 		case CTF_K_ENUM:
2507c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_enum_t) * vlen;
2517c478bd9Sstevel@tonic-gate 			break;
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Fill in the string table offset and size, compute the size of the
2577c478bd9Sstevel@tonic-gate 	 * entire CTF buffer we need, and then allocate a new buffer and
2587c478bd9Sstevel@tonic-gate 	 * bcopy the finished header to the start of the buffer.
2597c478bd9Sstevel@tonic-gate 	 */
2607c478bd9Sstevel@tonic-gate 	hdr.cth_stroff = hdr.cth_typeoff + size;
2617c478bd9Sstevel@tonic-gate 	hdr.cth_strlen = fp->ctf_dtstrlen;
2627c478bd9Sstevel@tonic-gate 	size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if ((buf = ctf_data_alloc(size)) == MAP_FAILED)
2657c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	bcopy(&hdr, buf, sizeof (ctf_header_t));
2687c478bd9Sstevel@tonic-gate 	t = (uchar_t *)buf + sizeof (ctf_header_t);
2697c478bd9Sstevel@tonic-gate 	s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE));
2727c478bd9Sstevel@tonic-gate 	s += sizeof (_CTF_STRTAB_TEMPLATE);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	 * We now take a final lap through the dynamic type definition list and
2767c478bd9Sstevel@tonic-gate 	 * copy the appropriate type records and strings to the output buffer.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	for (dtd = ctf_list_next(&fp->ctf_dtdefs);
2797c478bd9Sstevel@tonic-gate 	    dtd != NULL; dtd = ctf_list_next(dtd)) {
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2827c478bd9Sstevel@tonic-gate 		uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		ctf_array_t cta;
2857c478bd9Sstevel@tonic-gate 		uint_t encoding;
2867c478bd9Sstevel@tonic-gate 		size_t len;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		if (dtd->dtd_name != NULL) {
2897c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_name = (uint_t)(s - s0);
2907c478bd9Sstevel@tonic-gate 			len = strlen(dtd->dtd_name) + 1;
2917c478bd9Sstevel@tonic-gate 			bcopy(dtd->dtd_name, s, len);
2927c478bd9Sstevel@tonic-gate 			s += len;
2937c478bd9Sstevel@tonic-gate 		} else
2947c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_name = 0;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
2977c478bd9Sstevel@tonic-gate 			len = sizeof (ctf_stype_t);
2987c478bd9Sstevel@tonic-gate 		else
2997c478bd9Sstevel@tonic-gate 			len = sizeof (ctf_type_t);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		bcopy(&dtd->dtd_data, t, len);
3027c478bd9Sstevel@tonic-gate 		t += len;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 		switch (kind) {
3057c478bd9Sstevel@tonic-gate 		case CTF_K_INTEGER:
3067c478bd9Sstevel@tonic-gate 		case CTF_K_FLOAT:
3077c478bd9Sstevel@tonic-gate 			if (kind == CTF_K_INTEGER) {
3087c478bd9Sstevel@tonic-gate 				encoding = CTF_INT_DATA(
3097c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_format,
3107c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_offset,
3117c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_bits);
3127c478bd9Sstevel@tonic-gate 			} else {
3137c478bd9Sstevel@tonic-gate 				encoding = CTF_FP_DATA(
3147c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_format,
3157c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_offset,
3167c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_bits);
3177c478bd9Sstevel@tonic-gate 			}
3187c478bd9Sstevel@tonic-gate 			bcopy(&encoding, t, sizeof (encoding));
3197c478bd9Sstevel@tonic-gate 			t += sizeof (encoding);
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		case CTF_K_ARRAY:
3237c478bd9Sstevel@tonic-gate 			cta.cta_contents = (ushort_t)
3247c478bd9Sstevel@tonic-gate 			    dtd->dtd_u.dtu_arr.ctr_contents;
3257c478bd9Sstevel@tonic-gate 			cta.cta_index = (ushort_t)
3267c478bd9Sstevel@tonic-gate 			    dtd->dtd_u.dtu_arr.ctr_index;
3277c478bd9Sstevel@tonic-gate 			cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
3287c478bd9Sstevel@tonic-gate 			bcopy(&cta, t, sizeof (cta));
3297c478bd9Sstevel@tonic-gate 			t += sizeof (cta);
3307c478bd9Sstevel@tonic-gate 			break;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		case CTF_K_FUNCTION: {
3337c478bd9Sstevel@tonic-gate 			ushort_t *argv = (ushort_t *)(uintptr_t)t;
3347c478bd9Sstevel@tonic-gate 			uint_t argc;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 			for (argc = 0; argc < vlen; argc++)
3377c478bd9Sstevel@tonic-gate 				*argv++ = (ushort_t)dtd->dtd_u.dtu_argv[argc];
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 			if (vlen & 1)
3407c478bd9Sstevel@tonic-gate 				*argv++ = 0; /* pad to 4-byte boundary */
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 			t = (uchar_t *)argv;
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		case CTF_K_STRUCT:
3477c478bd9Sstevel@tonic-gate 		case CTF_K_UNION:
3487c478bd9Sstevel@tonic-gate 			if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
3497c478bd9Sstevel@tonic-gate 				t = ctf_copy_smembers(dtd, (uint_t)(s - s0), t);
3507c478bd9Sstevel@tonic-gate 			else
3517c478bd9Sstevel@tonic-gate 				t = ctf_copy_lmembers(dtd, (uint_t)(s - s0), t);
3527c478bd9Sstevel@tonic-gate 			s = ctf_copy_membnames(dtd, s);
3537c478bd9Sstevel@tonic-gate 			break;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		case CTF_K_ENUM:
3567c478bd9Sstevel@tonic-gate 			t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t);
3577c478bd9Sstevel@tonic-gate 			s = ctf_copy_membnames(dtd, s);
3587c478bd9Sstevel@tonic-gate 			break;
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/*
3637c478bd9Sstevel@tonic-gate 	 * Finally, we are ready to ctf_bufopen() the new container.  If this
3647c478bd9Sstevel@tonic-gate 	 * is successful, we then switch nfp and fp and free the old container.
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 	ctf_data_protect(buf, size);
3677c478bd9Sstevel@tonic-gate 	cts.cts_name = _CTF_SECTION;
3687c478bd9Sstevel@tonic-gate 	cts.cts_type = SHT_PROGBITS;
3697c478bd9Sstevel@tonic-gate 	cts.cts_flags = 0;
3707c478bd9Sstevel@tonic-gate 	cts.cts_data = buf;
3717c478bd9Sstevel@tonic-gate 	cts.cts_size = size;
3727c478bd9Sstevel@tonic-gate 	cts.cts_entsize = 1;
3737c478bd9Sstevel@tonic-gate 	cts.cts_offset = 0;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if ((nfp = ctf_bufopen(&cts, NULL, NULL, &err)) == NULL) {
3767c478bd9Sstevel@tonic-gate 		ctf_data_free(buf, size);
3777c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, err));
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	(void) ctf_setmodel(nfp, ctf_getmodel(fp));
3817c478bd9Sstevel@tonic-gate 	(void) ctf_import(nfp, fp->ctf_parent);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	nfp->ctf_refcnt = fp->ctf_refcnt;
3847c478bd9Sstevel@tonic-gate 	nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
3857c478bd9Sstevel@tonic-gate 	nfp->ctf_data.cts_data = NULL; /* force ctf_data_free() on close */
386*e4586ebfSmws 	nfp->ctf_dthash = fp->ctf_dthash;
387*e4586ebfSmws 	nfp->ctf_dthashlen = fp->ctf_dthashlen;
3887c478bd9Sstevel@tonic-gate 	nfp->ctf_dtdefs = fp->ctf_dtdefs;
3897c478bd9Sstevel@tonic-gate 	nfp->ctf_dtstrlen = fp->ctf_dtstrlen;
3907c478bd9Sstevel@tonic-gate 	nfp->ctf_dtnextid = fp->ctf_dtnextid;
3917c478bd9Sstevel@tonic-gate 	nfp->ctf_dtoldid = fp->ctf_dtnextid - 1;
3927c478bd9Sstevel@tonic-gate 	nfp->ctf_specific = fp->ctf_specific;
3937c478bd9Sstevel@tonic-gate 
394*e4586ebfSmws 	fp->ctf_dthash = NULL;
395*e4586ebfSmws 	fp->ctf_dthashlen = 0;
3967c478bd9Sstevel@tonic-gate 	bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t));
397*e4586ebfSmws 
3987c478bd9Sstevel@tonic-gate 	bcopy(fp, &ofp, sizeof (ctf_file_t));
3997c478bd9Sstevel@tonic-gate 	bcopy(nfp, fp, sizeof (ctf_file_t));
4007c478bd9Sstevel@tonic-gate 	bcopy(&ofp, nfp, sizeof (ctf_file_t));
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * Initialize the ctf_lookup_by_name top-level dictionary.  We keep an
4047c478bd9Sstevel@tonic-gate 	 * array of type name prefixes and the corresponding ctf_hash to use.
4057c478bd9Sstevel@tonic-gate 	 * NOTE: This code must be kept in sync with the code in ctf_bufopen().
4067c478bd9Sstevel@tonic-gate 	 */
4077c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
4087c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
4097c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
4107c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	nfp->ctf_refcnt = 1; /* force nfp to be freed */
4137c478bd9Sstevel@tonic-gate 	ctf_close(nfp);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	return (0);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
418*e4586ebfSmws void
419*e4586ebfSmws ctf_dtd_insert(ctf_file_t *fp, ctf_dtdef_t *dtd)
4207c478bd9Sstevel@tonic-gate {
421*e4586ebfSmws 	ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
422*e4586ebfSmws 
423*e4586ebfSmws 	dtd->dtd_hash = fp->ctf_dthash[h];
424*e4586ebfSmws 	fp->ctf_dthash[h] = dtd;
425*e4586ebfSmws 	ctf_list_append(&fp->ctf_dtdefs, dtd);
426*e4586ebfSmws }
427*e4586ebfSmws 
428*e4586ebfSmws void
429*e4586ebfSmws ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd)
430*e4586ebfSmws {
431*e4586ebfSmws 	ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
432*e4586ebfSmws 	ctf_dtdef_t *p, **q = &fp->ctf_dthash[h];
4337c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd, *nmd;
4347c478bd9Sstevel@tonic-gate 	size_t len;
4357c478bd9Sstevel@tonic-gate 
436*e4586ebfSmws 	for (p = *q; p != NULL; p = p->dtd_hash) {
437*e4586ebfSmws 		if (p != dtd)
438*e4586ebfSmws 			q = &p->dtd_hash;
439*e4586ebfSmws 		else
440*e4586ebfSmws 			break;
441*e4586ebfSmws 	}
4427c478bd9Sstevel@tonic-gate 
443*e4586ebfSmws 	if (p != NULL)
444*e4586ebfSmws 		*q = p->dtd_hash;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	switch (CTF_INFO_KIND(dtd->dtd_data.ctt_info)) {
4477c478bd9Sstevel@tonic-gate 	case CTF_K_STRUCT:
4487c478bd9Sstevel@tonic-gate 	case CTF_K_UNION:
4497c478bd9Sstevel@tonic-gate 	case CTF_K_ENUM:
4507c478bd9Sstevel@tonic-gate 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
4517c478bd9Sstevel@tonic-gate 		    dmd != NULL; dmd = nmd) {
4527c478bd9Sstevel@tonic-gate 			if (dmd->dmd_name != NULL) {
4537c478bd9Sstevel@tonic-gate 				len = strlen(dmd->dmd_name) + 1;
4547c478bd9Sstevel@tonic-gate 				ctf_free(dmd->dmd_name, len);
4557c478bd9Sstevel@tonic-gate 				fp->ctf_dtstrlen -= len;
4567c478bd9Sstevel@tonic-gate 			}
4577c478bd9Sstevel@tonic-gate 			nmd = ctf_list_next(dmd);
4587c478bd9Sstevel@tonic-gate 			ctf_free(dmd, sizeof (ctf_dmdef_t));
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 		break;
4617c478bd9Sstevel@tonic-gate 	case CTF_K_FUNCTION:
4627c478bd9Sstevel@tonic-gate 		ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
4637c478bd9Sstevel@tonic-gate 		    CTF_INFO_VLEN(dtd->dtd_data.ctt_info));
4647c478bd9Sstevel@tonic-gate 		break;
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (dtd->dtd_name) {
4687c478bd9Sstevel@tonic-gate 		len = strlen(dtd->dtd_name) + 1;
4697c478bd9Sstevel@tonic-gate 		ctf_free(dtd->dtd_name, len);
4707c478bd9Sstevel@tonic-gate 		fp->ctf_dtstrlen -= len;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	ctf_list_delete(&fp->ctf_dtdefs, dtd);
4747c478bd9Sstevel@tonic-gate 	ctf_free(dtd, sizeof (ctf_dtdef_t));
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
477*e4586ebfSmws ctf_dtdef_t *
478*e4586ebfSmws ctf_dtd_lookup(ctf_file_t *fp, ctf_id_t type)
479*e4586ebfSmws {
480*e4586ebfSmws 	ulong_t h = type & (fp->ctf_dthashlen - 1);
481*e4586ebfSmws 	ctf_dtdef_t *dtd;
482*e4586ebfSmws 
483*e4586ebfSmws 	if (fp->ctf_dthash == NULL)
484*e4586ebfSmws 		return (NULL);
485*e4586ebfSmws 
486*e4586ebfSmws 	for (dtd = fp->ctf_dthash[h]; dtd != NULL; dtd = dtd->dtd_hash) {
487*e4586ebfSmws 		if (dtd->dtd_type == type)
488*e4586ebfSmws 			break;
489*e4586ebfSmws 	}
490*e4586ebfSmws 
491*e4586ebfSmws 	return (dtd);
492*e4586ebfSmws }
493*e4586ebfSmws 
494*e4586ebfSmws /*
495*e4586ebfSmws  * Discard all of the dynamic type definitions that have been added to the
496*e4586ebfSmws  * container since the last call to ctf_update().  We locate such types by
497*e4586ebfSmws  * scanning the list and deleting elements that have type IDs greater than
498*e4586ebfSmws  * ctf_dtoldid, which is set by ctf_update(), above.
499*e4586ebfSmws  */
500*e4586ebfSmws int
501*e4586ebfSmws ctf_discard(ctf_file_t *fp)
502*e4586ebfSmws {
503*e4586ebfSmws 	ctf_dtdef_t *dtd, *ntd;
504*e4586ebfSmws 
505*e4586ebfSmws 	if (!(fp->ctf_flags & LCTF_RDWR))
506*e4586ebfSmws 		return (ctf_set_errno(fp, ECTF_RDONLY));
507*e4586ebfSmws 
508*e4586ebfSmws 	if (!(fp->ctf_flags & LCTF_DIRTY))
509*e4586ebfSmws 		return (0); /* no update required */
510*e4586ebfSmws 
511*e4586ebfSmws 	for (dtd = ctf_list_next(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
512*e4586ebfSmws 		if (dtd->dtd_type <= fp->ctf_dtoldid)
513*e4586ebfSmws 			continue; /* skip types that have been committed */
514*e4586ebfSmws 
515*e4586ebfSmws 		ntd = ctf_list_next(dtd);
516*e4586ebfSmws 		ctf_dtd_delete(fp, dtd);
517*e4586ebfSmws 	}
518*e4586ebfSmws 
5197c478bd9Sstevel@tonic-gate 	fp->ctf_dtnextid = fp->ctf_dtoldid + 1;
5207c478bd9Sstevel@tonic-gate 	fp->ctf_flags &= ~LCTF_DIRTY;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	return (0);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate static ctf_id_t
5267c478bd9Sstevel@tonic-gate ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
5297c478bd9Sstevel@tonic-gate 	ctf_id_t type;
5307c478bd9Sstevel@tonic-gate 	char *s = NULL;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
5337c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
5367c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (CTF_INDEX_TO_TYPE(fp->ctf_dtnextid, 1) > CTF_MAX_TYPE)
5397c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_FULL));
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL)
5427c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
5457c478bd9Sstevel@tonic-gate 		ctf_free(dtd, sizeof (ctf_dtdef_t));
5467c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	type = fp->ctf_dtnextid++;
5507c478bd9Sstevel@tonic-gate 	type = CTF_INDEX_TO_TYPE(type, (fp->ctf_flags & LCTF_CHILD));
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	bzero(dtd, sizeof (ctf_dtdef_t));
5537c478bd9Sstevel@tonic-gate 	dtd->dtd_name = s;
5547c478bd9Sstevel@tonic-gate 	dtd->dtd_type = type;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (s != NULL)
5577c478bd9Sstevel@tonic-gate 		fp->ctf_dtstrlen += strlen(s) + 1;
5587c478bd9Sstevel@tonic-gate 
559*e4586ebfSmws 	ctf_dtd_insert(fp, dtd);
5607c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	*rp = dtd;
5637c478bd9Sstevel@tonic-gate 	return (type);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * When encoding integer sizes, we want to convert a byte count in the range
5687c478bd9Sstevel@tonic-gate  * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc).  The clp2() function
5697c478bd9Sstevel@tonic-gate  * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate static size_t
5727c478bd9Sstevel@tonic-gate clp2(size_t x)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	x--;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	x |= (x >> 1);
5777c478bd9Sstevel@tonic-gate 	x |= (x >> 2);
5787c478bd9Sstevel@tonic-gate 	x |= (x >> 4);
5797c478bd9Sstevel@tonic-gate 	x |= (x >> 8);
5807c478bd9Sstevel@tonic-gate 	x |= (x >> 16);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	return (x + 1);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate static ctf_id_t
5867c478bd9Sstevel@tonic-gate ctf_add_encoded(ctf_file_t *fp, uint_t flag,
5877c478bd9Sstevel@tonic-gate     const char *name, const ctf_encoding_t *ep, uint_t kind)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
5907c478bd9Sstevel@tonic-gate 	ctf_id_t type;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (ep == NULL)
5937c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
5967c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
5997c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_size = clp2(P2ROUNDUP(ep->cte_bits, NBBY) / NBBY);
6007c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_enc = *ep;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	return (type);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate static ctf_id_t
6067c478bd9Sstevel@tonic-gate ctf_add_reftype(ctf_file_t *fp, uint_t flag, ctf_id_t ref, uint_t kind)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
6097c478bd9Sstevel@tonic-gate 	ctf_id_t type;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE)
6127c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
6157c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
6187c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_type = (ushort_t)ref;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	return (type);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate ctf_id_t
6247c478bd9Sstevel@tonic-gate ctf_add_integer(ctf_file_t *fp, uint_t flag,
6257c478bd9Sstevel@tonic-gate     const char *name, const ctf_encoding_t *ep)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER));
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate ctf_id_t
6317c478bd9Sstevel@tonic-gate ctf_add_float(ctf_file_t *fp, uint_t flag,
6327c478bd9Sstevel@tonic-gate     const char *name, const ctf_encoding_t *ep)
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT));
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate ctf_id_t
6387c478bd9Sstevel@tonic-gate ctf_add_pointer(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	return (ctf_add_reftype(fp, flag, ref, CTF_K_POINTER));
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate ctf_id_t
6447c478bd9Sstevel@tonic-gate ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
6477c478bd9Sstevel@tonic-gate 	ctf_id_t type;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	if (arp == NULL)
6507c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
6537c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, flag, 0);
6567c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_size = 0;
6577c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_arr = *arp;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	return (type);
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate int
6637c478bd9Sstevel@tonic-gate ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
6647c478bd9Sstevel@tonic-gate {
665*e4586ebfSmws 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
6687c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	if (dtd == NULL || CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
6717c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_BADID));
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
6747c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_arr = *arp;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	return (0);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate ctf_id_t
6807c478bd9Sstevel@tonic-gate ctf_add_function(ctf_file_t *fp, uint_t flag,
6817c478bd9Sstevel@tonic-gate     const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
6847c478bd9Sstevel@tonic-gate 	ctf_id_t type;
6857c478bd9Sstevel@tonic-gate 	uint_t vlen;
6867c478bd9Sstevel@tonic-gate 	ctf_id_t *vdat = NULL;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 ||
6897c478bd9Sstevel@tonic-gate 	    (ctc->ctc_argc != 0 && argv == NULL))
6907c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	vlen = ctc->ctc_argc;
6937c478bd9Sstevel@tonic-gate 	if (ctc->ctc_flags & CTF_FUNC_VARARG)
6947c478bd9Sstevel@tonic-gate 		vlen++; /* add trailing zero to indicate varargs (see below) */
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if (vlen > CTF_MAX_VLEN)
6977c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EOVERFLOW));
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL)
7007c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) {
7037c478bd9Sstevel@tonic-gate 		ctf_free(vdat, sizeof (ctf_id_t) * vlen);
7047c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, flag, vlen);
7087c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_type = (ushort_t)ctc->ctc_return;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc);
7117c478bd9Sstevel@tonic-gate 	if (ctc->ctc_flags & CTF_FUNC_VARARG)
7127c478bd9Sstevel@tonic-gate 		vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */
7137c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_argv = vdat;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	return (type);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate ctf_id_t
7197c478bd9Sstevel@tonic-gate ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name)
7207c478bd9Sstevel@tonic-gate {
721*e4586ebfSmws 	ctf_hash_t *hp = &fp->ctf_structs;
722*e4586ebfSmws 	ctf_helem_t *hep = NULL;
7237c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
7247c478bd9Sstevel@tonic-gate 	ctf_id_t type;
7257c478bd9Sstevel@tonic-gate 
726*e4586ebfSmws 	if (name != NULL)
727*e4586ebfSmws 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
728*e4586ebfSmws 
729*e4586ebfSmws 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
730*e4586ebfSmws 		dtd = ctf_dtd_lookup(fp, type = hep->h_type);
731*e4586ebfSmws 	else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
7327c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, flag, 0);
735*e4586ebfSmws 	dtd->dtd_data.ctt_size = 0;
736*e4586ebfSmws 
7377c478bd9Sstevel@tonic-gate 	return (type);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate ctf_id_t
7417c478bd9Sstevel@tonic-gate ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name)
7427c478bd9Sstevel@tonic-gate {
743*e4586ebfSmws 	ctf_hash_t *hp = &fp->ctf_unions;
744*e4586ebfSmws 	ctf_helem_t *hep = NULL;
7457c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
7467c478bd9Sstevel@tonic-gate 	ctf_id_t type;
7477c478bd9Sstevel@tonic-gate 
748*e4586ebfSmws 	if (name != NULL)
749*e4586ebfSmws 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
750*e4586ebfSmws 
751*e4586ebfSmws 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
752*e4586ebfSmws 		dtd = ctf_dtd_lookup(fp, type = hep->h_type);
753*e4586ebfSmws 	else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
7547c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, flag, 0);
757*e4586ebfSmws 	dtd->dtd_data.ctt_size = 0;
758*e4586ebfSmws 
7597c478bd9Sstevel@tonic-gate 	return (type);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate ctf_id_t
7637c478bd9Sstevel@tonic-gate ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name)
7647c478bd9Sstevel@tonic-gate {
765*e4586ebfSmws 	ctf_hash_t *hp = &fp->ctf_enums;
766*e4586ebfSmws 	ctf_helem_t *hep = NULL;
7677c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
7687c478bd9Sstevel@tonic-gate 	ctf_id_t type;
7697c478bd9Sstevel@tonic-gate 
770*e4586ebfSmws 	if (name != NULL)
771*e4586ebfSmws 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
772*e4586ebfSmws 
773*e4586ebfSmws 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
774*e4586ebfSmws 		dtd = ctf_dtd_lookup(fp, type = hep->h_type);
775*e4586ebfSmws 	else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
7767c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0);
7797c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	return (type);
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate ctf_id_t
7857c478bd9Sstevel@tonic-gate ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind)
7867c478bd9Sstevel@tonic-gate {
787*e4586ebfSmws 	ctf_hash_t *hp;
788*e4586ebfSmws 	ctf_helem_t *hep;
7897c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
7907c478bd9Sstevel@tonic-gate 	ctf_id_t type;
7917c478bd9Sstevel@tonic-gate 
792*e4586ebfSmws 	switch (kind) {
793*e4586ebfSmws 	case CTF_K_STRUCT:
794*e4586ebfSmws 		hp = &fp->ctf_structs;
795*e4586ebfSmws 		break;
796*e4586ebfSmws 	case CTF_K_UNION:
797*e4586ebfSmws 		hp = &fp->ctf_unions;
798*e4586ebfSmws 		break;
799*e4586ebfSmws 	case CTF_K_ENUM:
800*e4586ebfSmws 		hp = &fp->ctf_enums;
801*e4586ebfSmws 		break;
802*e4586ebfSmws 	default:
8037c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOTSUE));
804*e4586ebfSmws 	}
805*e4586ebfSmws 
806*e4586ebfSmws 	/*
807*e4586ebfSmws 	 * If the type is already defined or exists as a forward tag, just
808*e4586ebfSmws 	 * return the ctf_id_t of the existing definition.
809*e4586ebfSmws 	 */
810*e4586ebfSmws 	if (name != NULL && (hep = ctf_hash_lookup(hp,
811*e4586ebfSmws 	    fp, name, strlen(name))) != NULL)
812*e4586ebfSmws 		return (hep->h_type);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
8157c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0);
818*e4586ebfSmws 	dtd->dtd_data.ctt_type = kind;
819*e4586ebfSmws 
8207c478bd9Sstevel@tonic-gate 	return (type);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate ctf_id_t
8247c478bd9Sstevel@tonic-gate ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
8277c478bd9Sstevel@tonic-gate 	ctf_id_t type;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE)
8307c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
8337c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0);
8367c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_type = (ushort_t)ref;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	return (type);
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate ctf_id_t
8427c478bd9Sstevel@tonic-gate ctf_add_volatile(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
8437c478bd9Sstevel@tonic-gate {
8447c478bd9Sstevel@tonic-gate 	return (ctf_add_reftype(fp, flag, ref, CTF_K_VOLATILE));
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate ctf_id_t
8487c478bd9Sstevel@tonic-gate ctf_add_const(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
8497c478bd9Sstevel@tonic-gate {
8507c478bd9Sstevel@tonic-gate 	return (ctf_add_reftype(fp, flag, ref, CTF_K_CONST));
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate ctf_id_t
8547c478bd9Sstevel@tonic-gate ctf_add_restrict(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	return (ctf_add_reftype(fp, flag, ref, CTF_K_RESTRICT));
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate int
8607c478bd9Sstevel@tonic-gate ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
8617c478bd9Sstevel@tonic-gate {
862*e4586ebfSmws 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
8637c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	uint_t kind, vlen, root;
8667c478bd9Sstevel@tonic-gate 	char *s;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	if (name == NULL)
8697c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
8727c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	if (dtd == NULL)
8757c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_BADID));
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
8787c478bd9Sstevel@tonic-gate 	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
8797c478bd9Sstevel@tonic-gate 	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if (kind != CTF_K_ENUM)
8827c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOTENUM));
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	if (vlen == CTF_MAX_VLEN)
8857c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_DTFULL));
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
8887c478bd9Sstevel@tonic-gate 	    dmd != NULL; dmd = ctf_list_next(dmd)) {
8897c478bd9Sstevel@tonic-gate 		if (strcmp(dmd->dmd_name, name) == 0)
8907c478bd9Sstevel@tonic-gate 			return (ctf_set_errno(fp, ECTF_DUPMEMBER));
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
8947c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if ((s = ctf_strdup(name)) == NULL) {
8977c478bd9Sstevel@tonic-gate 		ctf_free(dmd, sizeof (ctf_dmdef_t));
8987c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	dmd->dmd_name = s;
9027c478bd9Sstevel@tonic-gate 	dmd->dmd_type = CTF_ERR;
9037c478bd9Sstevel@tonic-gate 	dmd->dmd_offset = 0;
9047c478bd9Sstevel@tonic-gate 	dmd->dmd_value = value;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
9077c478bd9Sstevel@tonic-gate 	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	fp->ctf_dtstrlen += strlen(s) + 1;
9107c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	return (0);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate int
9167c478bd9Sstevel@tonic-gate ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type)
9177c478bd9Sstevel@tonic-gate {
918*e4586ebfSmws 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
9197c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	ssize_t msize, malign, ssize;
9227c478bd9Sstevel@tonic-gate 	uint_t kind, vlen, root;
9237c478bd9Sstevel@tonic-gate 	char *s = NULL;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
9267c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (dtd == NULL)
9297c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_BADID));
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
9327c478bd9Sstevel@tonic-gate 	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
9337c478bd9Sstevel@tonic-gate 	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
9367c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOTSOU));
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	if (vlen == CTF_MAX_VLEN)
9397c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_DTFULL));
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (name != NULL) {
9427c478bd9Sstevel@tonic-gate 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
9437c478bd9Sstevel@tonic-gate 		    dmd != NULL; dmd = ctf_list_next(dmd)) {
9447c478bd9Sstevel@tonic-gate 			if (dmd->dmd_name != NULL &&
9457c478bd9Sstevel@tonic-gate 			    strcmp(dmd->dmd_name, name) == 0)
9467c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(fp, ECTF_DUPMEMBER));
9477c478bd9Sstevel@tonic-gate 		}
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
9517c478bd9Sstevel@tonic-gate 	    (malign = ctf_type_align(fp, type)) == CTF_ERR)
9527c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
9557c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
9587c478bd9Sstevel@tonic-gate 		ctf_free(dmd, sizeof (ctf_dmdef_t));
9597c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	dmd->dmd_name = s;
9637c478bd9Sstevel@tonic-gate 	dmd->dmd_type = type;
9647c478bd9Sstevel@tonic-gate 	dmd->dmd_value = -1;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_STRUCT && vlen != 0) {
9677c478bd9Sstevel@tonic-gate 		ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
9687c478bd9Sstevel@tonic-gate 		ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
9697c478bd9Sstevel@tonic-gate 		size_t off = lmd->dmd_offset;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		ctf_encoding_t linfo;
9727c478bd9Sstevel@tonic-gate 		ssize_t lsize;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
9757c478bd9Sstevel@tonic-gate 			off += linfo.cte_bits;
9767c478bd9Sstevel@tonic-gate 		else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
9777c478bd9Sstevel@tonic-gate 			off += lsize * NBBY;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 		/*
9807c478bd9Sstevel@tonic-gate 		 * Round up the offset of the end of the last member to the
9817c478bd9Sstevel@tonic-gate 		 * next byte boundary, convert 'off' to bytes, and then round
9827c478bd9Sstevel@tonic-gate 		 * it up again to the next multiple of the alignment required
9837c478bd9Sstevel@tonic-gate 		 * by the new member.  Finally, convert back to bits and store
9847c478bd9Sstevel@tonic-gate 		 * the result in dmd_offset.  Technically we could do more
9857c478bd9Sstevel@tonic-gate 		 * efficient packing if the new member is a bit-field, but
9867c478bd9Sstevel@tonic-gate 		 * we're the "compiler" and ANSI says we can do as we choose.
9877c478bd9Sstevel@tonic-gate 		 */
9887c478bd9Sstevel@tonic-gate 		off = roundup(off, NBBY) / NBBY;
9897c478bd9Sstevel@tonic-gate 		off = roundup(off, MAX(malign, 1));
9907c478bd9Sstevel@tonic-gate 		dmd->dmd_offset = off * NBBY;
9917c478bd9Sstevel@tonic-gate 		ssize = off + msize;
9927c478bd9Sstevel@tonic-gate 	} else {
9937c478bd9Sstevel@tonic-gate 		dmd->dmd_offset = 0;
9947c478bd9Sstevel@tonic-gate 		ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
9957c478bd9Sstevel@tonic-gate 		ssize = MAX(ssize, msize);
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	if (ssize > CTF_MAX_SIZE) {
9997c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
10007c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
10017c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
10027c478bd9Sstevel@tonic-gate 	} else
10037c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_size = (ushort_t)ssize;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
10067c478bd9Sstevel@tonic-gate 	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (s != NULL)
10097c478bd9Sstevel@tonic-gate 		fp->ctf_dtstrlen += strlen(s) + 1;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
10127c478bd9Sstevel@tonic-gate 	return (0);
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate static int
10167c478bd9Sstevel@tonic-gate enumcmp(const char *name, int value, void *arg)
10177c478bd9Sstevel@tonic-gate {
10187c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
10197c478bd9Sstevel@tonic-gate 	int bvalue;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type,
10227c478bd9Sstevel@tonic-gate 	    name, &bvalue) == CTF_ERR || value != bvalue);
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate static int
10267c478bd9Sstevel@tonic-gate enumadd(const char *name, int value, void *arg)
10277c478bd9Sstevel@tonic-gate {
10287c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type,
10317c478bd9Sstevel@tonic-gate 	    name, value) == CTF_ERR);
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10357c478bd9Sstevel@tonic-gate static int
10367c478bd9Sstevel@tonic-gate membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg)
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
10397c478bd9Sstevel@tonic-gate 	ctf_membinfo_t ctm;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	return (ctf_member_info(ctb->ctb_file, ctb->ctb_type,
10427c478bd9Sstevel@tonic-gate 	    name, &ctm) == CTF_ERR || ctm.ctm_offset != offset);
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate static int
10467c478bd9Sstevel@tonic-gate membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg)
10477c478bd9Sstevel@tonic-gate {
10487c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
10497c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd;
10507c478bd9Sstevel@tonic-gate 	char *s = NULL;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
10537c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(ctb->ctb_file, EAGAIN));
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
10567c478bd9Sstevel@tonic-gate 		ctf_free(dmd, sizeof (ctf_dmdef_t));
10577c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(ctb->ctb_file, EAGAIN));
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/*
10617c478bd9Sstevel@tonic-gate 	 * For now, dmd_type is copied as the src_fp's type; it is reset to an
10627c478bd9Sstevel@tonic-gate 	 * equivalent dst_fp type by a final loop in ctf_add_type(), below.
10637c478bd9Sstevel@tonic-gate 	 */
10647c478bd9Sstevel@tonic-gate 	dmd->dmd_name = s;
10657c478bd9Sstevel@tonic-gate 	dmd->dmd_type = type;
10667c478bd9Sstevel@tonic-gate 	dmd->dmd_offset = offset;
10677c478bd9Sstevel@tonic-gate 	dmd->dmd_value = -1;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	if (s != NULL)
10727c478bd9Sstevel@tonic-gate 		ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
10757c478bd9Sstevel@tonic-gate 	return (0);
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate /*
10797c478bd9Sstevel@tonic-gate  * The ctf_add_type routine is used to copy a type from a source CTF container
10807c478bd9Sstevel@tonic-gate  * to a dynamic destination container.  This routine operates recursively by
10817c478bd9Sstevel@tonic-gate  * following the source type's links and embedded member types.  If the
10827c478bd9Sstevel@tonic-gate  * destination container already contains a named type which has the same
10837c478bd9Sstevel@tonic-gate  * attributes, then we succeed and return this type but no changes occur.
10847c478bd9Sstevel@tonic-gate  */
10857c478bd9Sstevel@tonic-gate ctf_id_t
10867c478bd9Sstevel@tonic-gate ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate 	ctf_id_t dst_type = CTF_ERR;
10897c478bd9Sstevel@tonic-gate 	uint_t dst_kind = CTF_K_UNKNOWN;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	const ctf_type_t *tp;
10927c478bd9Sstevel@tonic-gate 	const char *name;
10937c478bd9Sstevel@tonic-gate 	uint_t kind, flag, vlen;
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	ctf_bundle_t src, dst;
10967c478bd9Sstevel@tonic-gate 	ctf_encoding_t src_en, dst_en;
10977c478bd9Sstevel@tonic-gate 	ctf_arinfo_t src_ar, dst_ar;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
11007c478bd9Sstevel@tonic-gate 	ctf_funcinfo_t ctc;
11017c478bd9Sstevel@tonic-gate 	ssize_t size;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	ctf_hash_t *hp;
11047c478bd9Sstevel@tonic-gate 	ctf_helem_t *hep;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	if (!(dst_fp->ctf_flags & LCTF_RDWR))
11077c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ECTF_RDONLY));
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)
11107c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	name = ctf_strptr(src_fp, tp->ctt_name);
11137c478bd9Sstevel@tonic-gate 	kind = LCTF_INFO_KIND(src_fp, tp->ctt_info);
11147c478bd9Sstevel@tonic-gate 	flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info);
11157c478bd9Sstevel@tonic-gate 	vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info);
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	switch (kind) {
11187c478bd9Sstevel@tonic-gate 	case CTF_K_STRUCT:
11197c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_structs;
11207c478bd9Sstevel@tonic-gate 		break;
11217c478bd9Sstevel@tonic-gate 	case CTF_K_UNION:
11227c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_unions;
11237c478bd9Sstevel@tonic-gate 		break;
11247c478bd9Sstevel@tonic-gate 	case CTF_K_ENUM:
11257c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_enums;
11267c478bd9Sstevel@tonic-gate 		break;
11277c478bd9Sstevel@tonic-gate 	default:
11287c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_names;
11297c478bd9Sstevel@tonic-gate 		break;
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	/*
11337c478bd9Sstevel@tonic-gate 	 * If the source type has a name and is a root type (visible at the
11347c478bd9Sstevel@tonic-gate 	 * top-level scope), lookup the name in the destination container and
11357c478bd9Sstevel@tonic-gate 	 * verify that it is of the same kind before we do anything else.
11367c478bd9Sstevel@tonic-gate 	 */
11377c478bd9Sstevel@tonic-gate 	if ((flag & CTF_ADD_ROOT) && name[0] != '\0' &&
11387c478bd9Sstevel@tonic-gate 	    (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) {
11397c478bd9Sstevel@tonic-gate 		dst_type = (ctf_id_t)hep->h_type;
11407c478bd9Sstevel@tonic-gate 		dst_kind = ctf_type_kind(dst_fp, dst_type);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	/*
11447c478bd9Sstevel@tonic-gate 	 * If an identically named dst_type exists, fail with ECTF_CONFLICT
11457c478bd9Sstevel@tonic-gate 	 * unless dst_type is a forward declaration and src_type is a struct,
11467c478bd9Sstevel@tonic-gate 	 * union, or enum (i.e. the definition of the previous forward decl).
11477c478bd9Sstevel@tonic-gate 	 */
11487c478bd9Sstevel@tonic-gate 	if (dst_type != CTF_ERR && dst_kind != kind && (
11497c478bd9Sstevel@tonic-gate 	    dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM &&
11507c478bd9Sstevel@tonic-gate 	    kind != CTF_K_STRUCT && kind != CTF_K_UNION)))
11517c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	/*
11547c478bd9Sstevel@tonic-gate 	 * If the non-empty name was not found in the appropriate hash, search
11557c478bd9Sstevel@tonic-gate 	 * the list of pending dynamic definitions that are not yet committed.
11567c478bd9Sstevel@tonic-gate 	 * If a matching name and kind are found, assume this is the type that
11577c478bd9Sstevel@tonic-gate 	 * we are looking for.  This is necessary to permit ctf_add_type() to
11587c478bd9Sstevel@tonic-gate 	 * operate recursively on entities such as a struct that contains a
11597c478bd9Sstevel@tonic-gate 	 * pointer member that refers to the same struct type.
11607c478bd9Sstevel@tonic-gate 	 */
11617c478bd9Sstevel@tonic-gate 	if (dst_type == CTF_ERR && name[0] != '\0') {
11627c478bd9Sstevel@tonic-gate 		for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL &&
11637c478bd9Sstevel@tonic-gate 		    dtd->dtd_type > dst_fp->ctf_dtoldid;
11647c478bd9Sstevel@tonic-gate 		    dtd = ctf_list_prev(dtd)) {
11657c478bd9Sstevel@tonic-gate 			if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) == kind &&
11667c478bd9Sstevel@tonic-gate 			    dtd->dtd_name != NULL &&
11677c478bd9Sstevel@tonic-gate 			    strcmp(dtd->dtd_name, name) == 0)
11687c478bd9Sstevel@tonic-gate 				return (dtd->dtd_type);
11697c478bd9Sstevel@tonic-gate 		}
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	src.ctb_file = src_fp;
11737c478bd9Sstevel@tonic-gate 	src.ctb_type = src_type;
11747c478bd9Sstevel@tonic-gate 	src.ctb_dtd = NULL;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	dst.ctb_file = dst_fp;
11777c478bd9Sstevel@tonic-gate 	dst.ctb_type = dst_type;
11787c478bd9Sstevel@tonic-gate 	dst.ctb_dtd = NULL;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	/*
11817c478bd9Sstevel@tonic-gate 	 * Now perform kind-specific processing.  If dst_type is CTF_ERR, then
11827c478bd9Sstevel@tonic-gate 	 * we add a new type with the same properties as src_type to dst_fp.
11837c478bd9Sstevel@tonic-gate 	 * If dst_type is not CTF_ERR, then we verify that dst_type has the
11847c478bd9Sstevel@tonic-gate 	 * same attributes as src_type.  We recurse for embedded references.
11857c478bd9Sstevel@tonic-gate 	 */
11867c478bd9Sstevel@tonic-gate 	switch (kind) {
11877c478bd9Sstevel@tonic-gate 	case CTF_K_INTEGER:
11887c478bd9Sstevel@tonic-gate 	case CTF_K_FLOAT:
11897c478bd9Sstevel@tonic-gate 		if (ctf_type_encoding(src_fp, src_type, &src_en) != 0)
11907c478bd9Sstevel@tonic-gate 			return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR) {
11937c478bd9Sstevel@tonic-gate 			if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0)
11947c478bd9Sstevel@tonic-gate 				return (CTF_ERR); /* errno is set for us */
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 			if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t)))
11977c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 		} else if (kind == CTF_K_INTEGER) {
12007c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_integer(dst_fp, flag, name, &src_en);
12017c478bd9Sstevel@tonic-gate 		} else
12027c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_float(dst_fp, flag, name, &src_en);
12037c478bd9Sstevel@tonic-gate 		break;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	case CTF_K_POINTER:
12067c478bd9Sstevel@tonic-gate 	case CTF_K_VOLATILE:
12077c478bd9Sstevel@tonic-gate 	case CTF_K_CONST:
12087c478bd9Sstevel@tonic-gate 	case CTF_K_RESTRICT:
12097c478bd9Sstevel@tonic-gate 		src_type = ctf_type_reference(src_fp, src_type);
12107c478bd9Sstevel@tonic-gate 		src_type = ctf_add_type(dst_fp, src_fp, src_type);
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 		if (src_type == CTF_ERR)
12137c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		dst_type = ctf_add_reftype(dst_fp, flag, src_type, kind);
12167c478bd9Sstevel@tonic-gate 		break;
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	case CTF_K_ARRAY:
12197c478bd9Sstevel@tonic-gate 		if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR)
12207c478bd9Sstevel@tonic-gate 			return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		src_ar.ctr_contents =
12237c478bd9Sstevel@tonic-gate 		    ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents);
12247c478bd9Sstevel@tonic-gate 		src_ar.ctr_index =
12257c478bd9Sstevel@tonic-gate 		    ctf_add_type(dst_fp, src_fp, src_ar.ctr_index);
12267c478bd9Sstevel@tonic-gate 		src_ar.ctr_nelems = src_ar.ctr_nelems;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 		if (src_ar.ctr_contents == CTF_ERR ||
12297c478bd9Sstevel@tonic-gate 		    src_ar.ctr_index == CTF_ERR)
12307c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR) {
12337c478bd9Sstevel@tonic-gate 			if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0)
12347c478bd9Sstevel@tonic-gate 				return (CTF_ERR); /* errno is set for us */
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 			if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
12377c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
12387c478bd9Sstevel@tonic-gate 		} else
12397c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_array(dst_fp, flag, &src_ar);
12407c478bd9Sstevel@tonic-gate 		break;
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	case CTF_K_FUNCTION:
12437c478bd9Sstevel@tonic-gate 		ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type);
12447c478bd9Sstevel@tonic-gate 		ctc.ctc_argc = 0;
12457c478bd9Sstevel@tonic-gate 		ctc.ctc_flags = 0;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 		if (ctc.ctc_return == CTF_ERR)
12487c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 		dst_type = ctf_add_function(dst_fp, flag, &ctc, NULL);
12517c478bd9Sstevel@tonic-gate 		break;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	case CTF_K_STRUCT:
12547c478bd9Sstevel@tonic-gate 	case CTF_K_UNION: {
12557c478bd9Sstevel@tonic-gate 		ctf_dmdef_t *dmd;
12567c478bd9Sstevel@tonic-gate 		int errs = 0;
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 		/*
12597c478bd9Sstevel@tonic-gate 		 * Technically to match a struct or union we need to check both
12607c478bd9Sstevel@tonic-gate 		 * ways (src members vs. dst, dst members vs. src) but we make
12617c478bd9Sstevel@tonic-gate 		 * this more optimal by only checking src vs. dst and comparing
12627c478bd9Sstevel@tonic-gate 		 * the total size of the structure (which we must do anyway)
12637c478bd9Sstevel@tonic-gate 		 * which covers the possibility of dst members not in src.
12647c478bd9Sstevel@tonic-gate 		 * This optimization can be defeated for unions, but is so
12657c478bd9Sstevel@tonic-gate 		 * pathological as to render it irrelevant for our purposes.
12667c478bd9Sstevel@tonic-gate 		 */
12677c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
12687c478bd9Sstevel@tonic-gate 			if (ctf_type_size(src_fp, src_type) !=
12697c478bd9Sstevel@tonic-gate 			    ctf_type_size(dst_fp, dst_type))
12707c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 			if (ctf_member_iter(src_fp, src_type, membcmp, &dst))
12737c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 			break;
12767c478bd9Sstevel@tonic-gate 		}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 		/*
12797c478bd9Sstevel@tonic-gate 		 * Unlike the other cases, copying structs and unions is done
12807c478bd9Sstevel@tonic-gate 		 * manually so as to avoid repeated lookups in ctf_add_member
12817c478bd9Sstevel@tonic-gate 		 * and to ensure the exact same member offsets as in src_type.
12827c478bd9Sstevel@tonic-gate 		 */
12837c478bd9Sstevel@tonic-gate 		dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);
12847c478bd9Sstevel@tonic-gate 		if (dst_type == CTF_ERR)
12857c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 		dst.ctb_type = dst_type;
12887c478bd9Sstevel@tonic-gate 		dst.ctb_dtd = dtd;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)
12917c478bd9Sstevel@tonic-gate 			errs++; /* increment errs and fail at bottom of case */
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 		if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) {
12947c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
12957c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
12967c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
12977c478bd9Sstevel@tonic-gate 		} else
12987c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_size = (ushort_t)size;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen);
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 		/*
13037c478bd9Sstevel@tonic-gate 		 * Make a final pass through the members changing each dmd_type
13047c478bd9Sstevel@tonic-gate 		 * (a src_fp type) to an equivalent type in dst_fp.  We pass
13057c478bd9Sstevel@tonic-gate 		 * through all members, leaving any that fail set to CTF_ERR.
13067c478bd9Sstevel@tonic-gate 		 */
13077c478bd9Sstevel@tonic-gate 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
13087c478bd9Sstevel@tonic-gate 		    dmd != NULL; dmd = ctf_list_next(dmd)) {
13097c478bd9Sstevel@tonic-gate 			if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp,
13107c478bd9Sstevel@tonic-gate 			    dmd->dmd_type)) == CTF_ERR)
13117c478bd9Sstevel@tonic-gate 				errs++;
13127c478bd9Sstevel@tonic-gate 		}
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 		if (errs)
13157c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
13167c478bd9Sstevel@tonic-gate 		break;
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	case CTF_K_ENUM:
13207c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
13217c478bd9Sstevel@tonic-gate 			if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||
13227c478bd9Sstevel@tonic-gate 			    ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))
13237c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
13247c478bd9Sstevel@tonic-gate 		} else {
13257c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_enum(dst_fp, flag, name);
13267c478bd9Sstevel@tonic-gate 			if ((dst.ctb_type = dst_type) == CTF_ERR ||
13277c478bd9Sstevel@tonic-gate 			    ctf_enum_iter(src_fp, src_type, enumadd, &dst))
13287c478bd9Sstevel@tonic-gate 				return (CTF_ERR); /* errno is set for us */
13297c478bd9Sstevel@tonic-gate 		}
13307c478bd9Sstevel@tonic-gate 		break;
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	case CTF_K_FORWARD:
13337c478bd9Sstevel@tonic-gate 		if (dst_type == CTF_ERR) {
13347c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_forward(dst_fp,
13357c478bd9Sstevel@tonic-gate 			    flag, name, CTF_K_STRUCT); /* assume STRUCT */
13367c478bd9Sstevel@tonic-gate 		}
13377c478bd9Sstevel@tonic-gate 		break;
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	case CTF_K_TYPEDEF:
13407c478bd9Sstevel@tonic-gate 		src_type = ctf_type_reference(src_fp, src_type);
13417c478bd9Sstevel@tonic-gate 		src_type = ctf_add_type(dst_fp, src_fp, src_type);
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 		if (src_type == CTF_ERR)
13447c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 		/*
13477c478bd9Sstevel@tonic-gate 		 * If dst_type is not CTF_ERR at this point, we should check if
13487c478bd9Sstevel@tonic-gate 		 * ctf_type_reference(dst_fp, dst_type) != src_type and if so
13497c478bd9Sstevel@tonic-gate 		 * fail with ECTF_CONFLICT.  However, this causes problems with
13507c478bd9Sstevel@tonic-gate 		 * <sys/types.h> typedefs that vary based on things like if
13517c478bd9Sstevel@tonic-gate 		 * _ILP32x then pid_t is int otherwise long.  We therefore omit
13527c478bd9Sstevel@tonic-gate 		 * this check and assume that if the identically named typedef
13537c478bd9Sstevel@tonic-gate 		 * already exists in dst_fp, it is correct or equivalent.
13547c478bd9Sstevel@tonic-gate 		 */
13557c478bd9Sstevel@tonic-gate 		if (dst_type == CTF_ERR) {
13567c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_typedef(dst_fp, flag,
13577c478bd9Sstevel@tonic-gate 			    name, src_type);
13587c478bd9Sstevel@tonic-gate 		}
13597c478bd9Sstevel@tonic-gate 		break;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	default:
13627c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ECTF_CORRUPT));
13637c478bd9Sstevel@tonic-gate 	}
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	return (dst_type);
13667c478bd9Sstevel@tonic-gate }
1367