xref: /titanic_44/usr/src/common/ctf/ctf_create.c (revision 0a47c91c895e274dd0990009919e30e984364a8b)
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  */
22e4586ebfSmws 
237c478bd9Sstevel@tonic-gate /*
24e4586ebfSmws  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
27*0a47c91cSRobert Mustacchi /*
28*0a47c91cSRobert Mustacchi  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
29*0a47c91cSRobert Mustacchi  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/mman.h>
347c478bd9Sstevel@tonic-gate #include <ctf_impl.h>
35*0a47c91cSRobert Mustacchi #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * This static string is used as the template for initially populating a
397c478bd9Sstevel@tonic-gate  * dynamic container's string table.  We always store \0 in the first byte,
407c478bd9Sstevel@tonic-gate  * and we use the generic string "PARENT" to mark this container's parent
417c478bd9Sstevel@tonic-gate  * if one is associated with the container using ctf_import().
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT";
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * To create an empty CTF container, we just declare a zeroed header and call
477c478bd9Sstevel@tonic-gate  * ctf_bufopen() on it.  If ctf_bufopen succeeds, we mark the new container r/w
487c478bd9Sstevel@tonic-gate  * and initialize the dynamic members.  We set dtstrlen to 1 to reserve the
497c478bd9Sstevel@tonic-gate  * first byte of the string table for a \0 byte, and we start assigning type
507c478bd9Sstevel@tonic-gate  * IDs at 1 because type ID 0 is used as a sentinel.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate ctf_file_t *
ctf_create(int * errp)537c478bd9Sstevel@tonic-gate ctf_create(int *errp)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } };
567c478bd9Sstevel@tonic-gate 
57e4586ebfSmws 	const ulong_t hashlen = 128;
58e4586ebfSmws 	ctf_dtdef_t **hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *));
597c478bd9Sstevel@tonic-gate 	ctf_sect_t cts;
607c478bd9Sstevel@tonic-gate 	ctf_file_t *fp;
617c478bd9Sstevel@tonic-gate 
62e4586ebfSmws 	if (hash == NULL)
63e4586ebfSmws 		return (ctf_set_open_errno(errp, EAGAIN));
64e4586ebfSmws 
657c478bd9Sstevel@tonic-gate 	cts.cts_name = _CTF_SECTION;
667c478bd9Sstevel@tonic-gate 	cts.cts_type = SHT_PROGBITS;
677c478bd9Sstevel@tonic-gate 	cts.cts_flags = 0;
687c478bd9Sstevel@tonic-gate 	cts.cts_data = &hdr;
697c478bd9Sstevel@tonic-gate 	cts.cts_size = sizeof (hdr);
707c478bd9Sstevel@tonic-gate 	cts.cts_entsize = 1;
717c478bd9Sstevel@tonic-gate 	cts.cts_offset = 0;
727c478bd9Sstevel@tonic-gate 
73e4586ebfSmws 	if ((fp = ctf_bufopen(&cts, NULL, NULL, errp)) == NULL) {
74e4586ebfSmws 		ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *));
75e4586ebfSmws 		return (NULL);
76e4586ebfSmws 	}
77e4586ebfSmws 
787c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_RDWR;
79e4586ebfSmws 	fp->ctf_dthashlen = hashlen;
80e4586ebfSmws 	bzero(hash, hashlen * sizeof (ctf_dtdef_t *));
81e4586ebfSmws 	fp->ctf_dthash = hash;
827c478bd9Sstevel@tonic-gate 	fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE);
837c478bd9Sstevel@tonic-gate 	fp->ctf_dtnextid = 1;
847c478bd9Sstevel@tonic-gate 	fp->ctf_dtoldid = 0;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	return (fp);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static uchar_t *
ctf_copy_smembers(ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)907c478bd9Sstevel@tonic-gate ctf_copy_smembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
937c478bd9Sstevel@tonic-gate 	ctf_member_t ctm;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
967c478bd9Sstevel@tonic-gate 		if (dmd->dmd_name) {
977c478bd9Sstevel@tonic-gate 			ctm.ctm_name = soff;
987c478bd9Sstevel@tonic-gate 			soff += strlen(dmd->dmd_name) + 1;
997c478bd9Sstevel@tonic-gate 		} else
1007c478bd9Sstevel@tonic-gate 			ctm.ctm_name = 0;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 		ctm.ctm_type = (ushort_t)dmd->dmd_type;
1037c478bd9Sstevel@tonic-gate 		ctm.ctm_offset = (ushort_t)dmd->dmd_offset;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		bcopy(&ctm, t, sizeof (ctm));
1067c478bd9Sstevel@tonic-gate 		t += sizeof (ctm);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	return (t);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static uchar_t *
ctf_copy_lmembers(ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)1137c478bd9Sstevel@tonic-gate ctf_copy_lmembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1167c478bd9Sstevel@tonic-gate 	ctf_lmember_t ctlm;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
1197c478bd9Sstevel@tonic-gate 		if (dmd->dmd_name) {
1207c478bd9Sstevel@tonic-gate 			ctlm.ctlm_name = soff;
1217c478bd9Sstevel@tonic-gate 			soff += strlen(dmd->dmd_name) + 1;
1227c478bd9Sstevel@tonic-gate 		} else
1237c478bd9Sstevel@tonic-gate 			ctlm.ctlm_name = 0;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		ctlm.ctlm_type = (ushort_t)dmd->dmd_type;
1267c478bd9Sstevel@tonic-gate 		ctlm.ctlm_pad = 0;
1277c478bd9Sstevel@tonic-gate 		ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset);
1287c478bd9Sstevel@tonic-gate 		ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		bcopy(&ctlm, t, sizeof (ctlm));
1317c478bd9Sstevel@tonic-gate 		t += sizeof (ctlm);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	return (t);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static uchar_t *
ctf_copy_emembers(ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)1387c478bd9Sstevel@tonic-gate ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1417c478bd9Sstevel@tonic-gate 	ctf_enum_t cte;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
1447c478bd9Sstevel@tonic-gate 		cte.cte_name = soff;
1457c478bd9Sstevel@tonic-gate 		cte.cte_value = dmd->dmd_value;
1467c478bd9Sstevel@tonic-gate 		soff += strlen(dmd->dmd_name) + 1;
1477c478bd9Sstevel@tonic-gate 		bcopy(&cte, t, sizeof (cte));
1487c478bd9Sstevel@tonic-gate 		t += sizeof (cte);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	return (t);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static uchar_t *
ctf_copy_membnames(ctf_dtdef_t * dtd,uchar_t * s)1557c478bd9Sstevel@tonic-gate ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1587c478bd9Sstevel@tonic-gate 	size_t len;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
1617c478bd9Sstevel@tonic-gate 		if (dmd->dmd_name == NULL)
1627c478bd9Sstevel@tonic-gate 			continue; /* skip anonymous members */
1637c478bd9Sstevel@tonic-gate 		len = strlen(dmd->dmd_name) + 1;
1647c478bd9Sstevel@tonic-gate 		bcopy(dmd->dmd_name, s, len);
1657c478bd9Sstevel@tonic-gate 		s += len;
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	return (s);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
172*0a47c91cSRobert Mustacchi  * Only types of dyanmic CTF containers contain reference counts. These
173*0a47c91cSRobert Mustacchi  * containers are marked RD/WR. Because of that we basically make this a no-op
174*0a47c91cSRobert Mustacchi  * for compatability with non-dynamic CTF sections. This is also a no-op for
175*0a47c91cSRobert Mustacchi  * types which are not dynamic types. It is the responsibility of the caller to
176*0a47c91cSRobert Mustacchi  * make sure it is a valid type. We help that caller out on debug builds.
177*0a47c91cSRobert Mustacchi  *
178*0a47c91cSRobert Mustacchi  * Note that the reference counts are not maintained for types that are not
179*0a47c91cSRobert Mustacchi  * within this container. In other words if we have a type in a parent, that
180*0a47c91cSRobert Mustacchi  * will not have its reference count increased. On the flip side, the parent
181*0a47c91cSRobert Mustacchi  * will not be allowed to remove dynamic types if it has children.
182*0a47c91cSRobert Mustacchi  */
183*0a47c91cSRobert Mustacchi static void
ctf_ref_inc(ctf_file_t * fp,ctf_id_t tid)184*0a47c91cSRobert Mustacchi ctf_ref_inc(ctf_file_t *fp, ctf_id_t tid)
185*0a47c91cSRobert Mustacchi {
186*0a47c91cSRobert Mustacchi 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
187*0a47c91cSRobert Mustacchi 
188*0a47c91cSRobert Mustacchi 	if (dtd == NULL)
189*0a47c91cSRobert Mustacchi 		return;
190*0a47c91cSRobert Mustacchi 
191*0a47c91cSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
192*0a47c91cSRobert Mustacchi 		return;
193*0a47c91cSRobert Mustacchi 
194*0a47c91cSRobert Mustacchi 	dtd->dtd_ref++;
195*0a47c91cSRobert Mustacchi }
196*0a47c91cSRobert Mustacchi 
197*0a47c91cSRobert Mustacchi /*
198*0a47c91cSRobert Mustacchi  * Just as with ctf_ref_inc, this is a no-op on non-writeable containers and the
199*0a47c91cSRobert Mustacchi  * caller should ensure that this is already a valid type.
200*0a47c91cSRobert Mustacchi  */
201*0a47c91cSRobert Mustacchi static void
ctf_ref_dec(ctf_file_t * fp,ctf_id_t tid)202*0a47c91cSRobert Mustacchi ctf_ref_dec(ctf_file_t *fp, ctf_id_t tid)
203*0a47c91cSRobert Mustacchi {
204*0a47c91cSRobert Mustacchi 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
205*0a47c91cSRobert Mustacchi 
206*0a47c91cSRobert Mustacchi 	if (dtd == NULL)
207*0a47c91cSRobert Mustacchi 		return;
208*0a47c91cSRobert Mustacchi 
209*0a47c91cSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
210*0a47c91cSRobert Mustacchi 		return;
211*0a47c91cSRobert Mustacchi 
212*0a47c91cSRobert Mustacchi 	ASSERT(dtd->dtd_ref >= 1);
213*0a47c91cSRobert Mustacchi 	dtd->dtd_ref--;
214*0a47c91cSRobert Mustacchi }
215*0a47c91cSRobert Mustacchi 
216*0a47c91cSRobert Mustacchi /*
2177c478bd9Sstevel@tonic-gate  * If the specified CTF container is writable and has been modified, reload
2187c478bd9Sstevel@tonic-gate  * this container with the updated type definitions.  In order to make this
2197c478bd9Sstevel@tonic-gate  * code and the rest of libctf as simple as possible, we perform updates by
2207c478bd9Sstevel@tonic-gate  * taking the dynamic type definitions and creating an in-memory CTF file
2217c478bd9Sstevel@tonic-gate  * containing the definitions, and then call ctf_bufopen() on it.  This not
2227c478bd9Sstevel@tonic-gate  * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest
2237c478bd9Sstevel@tonic-gate  * of the library code with different lookup paths for static and dynamic
2247c478bd9Sstevel@tonic-gate  * type definitions.  We are therefore optimizing greatly for lookup over
2257c478bd9Sstevel@tonic-gate  * update, which we assume will be an uncommon operation.  We perform one
2267c478bd9Sstevel@tonic-gate  * extra trick here for the benefit of callers and to keep our code simple:
2277c478bd9Sstevel@tonic-gate  * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp
2287c478bd9Sstevel@tonic-gate  * constant for the caller, so after ctf_bufopen() returns, we use bcopy to
2297c478bd9Sstevel@tonic-gate  * swap the interior of the old and new ctf_file_t's, and then free the old.
230*0a47c91cSRobert Mustacchi  *
231*0a47c91cSRobert Mustacchi  * Note that the lists of dynamic types stays around and the resulting container
232*0a47c91cSRobert Mustacchi  * is still writeable. Furthermore, the reference counts that are on the dtd's
233*0a47c91cSRobert Mustacchi  * are still valid.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate int
ctf_update(ctf_file_t * fp)2367c478bd9Sstevel@tonic-gate ctf_update(ctf_file_t *fp)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	ctf_file_t ofp, *nfp;
2397c478bd9Sstevel@tonic-gate 	ctf_header_t hdr;
2407c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
2417c478bd9Sstevel@tonic-gate 	ctf_sect_t cts;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	uchar_t *s, *s0, *t;
2447c478bd9Sstevel@tonic-gate 	size_t size;
2457c478bd9Sstevel@tonic-gate 	void *buf;
2467c478bd9Sstevel@tonic-gate 	int err;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
2497c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_DIRTY))
2527c478bd9Sstevel@tonic-gate 		return (0); /* no update required */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * Fill in an initial CTF header.  We will leave the label, object,
2567c478bd9Sstevel@tonic-gate 	 * and function sections empty and only output a header, type section,
2577c478bd9Sstevel@tonic-gate 	 * and string table.  The type section begins at a 4-byte aligned
2587c478bd9Sstevel@tonic-gate 	 * boundary past the CTF header itself (at relative offset zero).
2597c478bd9Sstevel@tonic-gate 	 */
2607c478bd9Sstevel@tonic-gate 	bzero(&hdr, sizeof (hdr));
2617c478bd9Sstevel@tonic-gate 	hdr.cth_magic = CTF_MAGIC;
2627c478bd9Sstevel@tonic-gate 	hdr.cth_version = CTF_VERSION;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (fp->ctf_flags & LCTF_CHILD)
2657c478bd9Sstevel@tonic-gate 		hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/*
2687c478bd9Sstevel@tonic-gate 	 * Iterate through the dynamic type definition list and compute the
2697c478bd9Sstevel@tonic-gate 	 * size of the CTF type section we will need to generate.
2707c478bd9Sstevel@tonic-gate 	 */
2717c478bd9Sstevel@tonic-gate 	for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs);
2727c478bd9Sstevel@tonic-gate 	    dtd != NULL; dtd = ctf_list_next(dtd)) {
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2757c478bd9Sstevel@tonic-gate 		uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
2787c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_stype_t);
2797c478bd9Sstevel@tonic-gate 		else
2807c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_type_t);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		switch (kind) {
2837c478bd9Sstevel@tonic-gate 		case CTF_K_INTEGER:
2847c478bd9Sstevel@tonic-gate 		case CTF_K_FLOAT:
2857c478bd9Sstevel@tonic-gate 			size += sizeof (uint_t);
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 		case CTF_K_ARRAY:
2887c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_array_t);
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 		case CTF_K_FUNCTION:
2917c478bd9Sstevel@tonic-gate 			size += sizeof (ushort_t) * (vlen + (vlen & 1));
2927c478bd9Sstevel@tonic-gate 			break;
2937c478bd9Sstevel@tonic-gate 		case CTF_K_STRUCT:
2947c478bd9Sstevel@tonic-gate 		case CTF_K_UNION:
2957c478bd9Sstevel@tonic-gate 			if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
2967c478bd9Sstevel@tonic-gate 				size += sizeof (ctf_member_t) * vlen;
2977c478bd9Sstevel@tonic-gate 			else
2987c478bd9Sstevel@tonic-gate 				size += sizeof (ctf_lmember_t) * vlen;
2997c478bd9Sstevel@tonic-gate 			break;
3007c478bd9Sstevel@tonic-gate 		case CTF_K_ENUM:
3017c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_enum_t) * vlen;
3027c478bd9Sstevel@tonic-gate 			break;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * Fill in the string table offset and size, compute the size of the
3087c478bd9Sstevel@tonic-gate 	 * entire CTF buffer we need, and then allocate a new buffer and
3097c478bd9Sstevel@tonic-gate 	 * bcopy the finished header to the start of the buffer.
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	hdr.cth_stroff = hdr.cth_typeoff + size;
3127c478bd9Sstevel@tonic-gate 	hdr.cth_strlen = fp->ctf_dtstrlen;
3137c478bd9Sstevel@tonic-gate 	size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if ((buf = ctf_data_alloc(size)) == MAP_FAILED)
3167c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	bcopy(&hdr, buf, sizeof (ctf_header_t));
3197c478bd9Sstevel@tonic-gate 	t = (uchar_t *)buf + sizeof (ctf_header_t);
3207c478bd9Sstevel@tonic-gate 	s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE));
3237c478bd9Sstevel@tonic-gate 	s += sizeof (_CTF_STRTAB_TEMPLATE);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	/*
3267c478bd9Sstevel@tonic-gate 	 * We now take a final lap through the dynamic type definition list and
3277c478bd9Sstevel@tonic-gate 	 * copy the appropriate type records and strings to the output buffer.
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 	for (dtd = ctf_list_next(&fp->ctf_dtdefs);
3307c478bd9Sstevel@tonic-gate 	    dtd != NULL; dtd = ctf_list_next(dtd)) {
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
3337c478bd9Sstevel@tonic-gate 		uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		ctf_array_t cta;
3367c478bd9Sstevel@tonic-gate 		uint_t encoding;
3377c478bd9Sstevel@tonic-gate 		size_t len;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		if (dtd->dtd_name != NULL) {
3407c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_name = (uint_t)(s - s0);
3417c478bd9Sstevel@tonic-gate 			len = strlen(dtd->dtd_name) + 1;
3427c478bd9Sstevel@tonic-gate 			bcopy(dtd->dtd_name, s, len);
3437c478bd9Sstevel@tonic-gate 			s += len;
3447c478bd9Sstevel@tonic-gate 		} else
3457c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_name = 0;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
3487c478bd9Sstevel@tonic-gate 			len = sizeof (ctf_stype_t);
3497c478bd9Sstevel@tonic-gate 		else
3507c478bd9Sstevel@tonic-gate 			len = sizeof (ctf_type_t);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		bcopy(&dtd->dtd_data, t, len);
3537c478bd9Sstevel@tonic-gate 		t += len;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		switch (kind) {
3567c478bd9Sstevel@tonic-gate 		case CTF_K_INTEGER:
3577c478bd9Sstevel@tonic-gate 		case CTF_K_FLOAT:
3587c478bd9Sstevel@tonic-gate 			if (kind == CTF_K_INTEGER) {
3597c478bd9Sstevel@tonic-gate 				encoding = CTF_INT_DATA(
3607c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_format,
3617c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_offset,
3627c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_bits);
3637c478bd9Sstevel@tonic-gate 			} else {
3647c478bd9Sstevel@tonic-gate 				encoding = CTF_FP_DATA(
3657c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_format,
3667c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_offset,
3677c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_bits);
3687c478bd9Sstevel@tonic-gate 			}
3697c478bd9Sstevel@tonic-gate 			bcopy(&encoding, t, sizeof (encoding));
3707c478bd9Sstevel@tonic-gate 			t += sizeof (encoding);
3717c478bd9Sstevel@tonic-gate 			break;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		case CTF_K_ARRAY:
3747c478bd9Sstevel@tonic-gate 			cta.cta_contents = (ushort_t)
3757c478bd9Sstevel@tonic-gate 			    dtd->dtd_u.dtu_arr.ctr_contents;
3767c478bd9Sstevel@tonic-gate 			cta.cta_index = (ushort_t)
3777c478bd9Sstevel@tonic-gate 			    dtd->dtd_u.dtu_arr.ctr_index;
3787c478bd9Sstevel@tonic-gate 			cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
3797c478bd9Sstevel@tonic-gate 			bcopy(&cta, t, sizeof (cta));
3807c478bd9Sstevel@tonic-gate 			t += sizeof (cta);
3817c478bd9Sstevel@tonic-gate 			break;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		case CTF_K_FUNCTION: {
3847c478bd9Sstevel@tonic-gate 			ushort_t *argv = (ushort_t *)(uintptr_t)t;
3857c478bd9Sstevel@tonic-gate 			uint_t argc;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 			for (argc = 0; argc < vlen; argc++)
3887c478bd9Sstevel@tonic-gate 				*argv++ = (ushort_t)dtd->dtd_u.dtu_argv[argc];
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 			if (vlen & 1)
3917c478bd9Sstevel@tonic-gate 				*argv++ = 0; /* pad to 4-byte boundary */
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 			t = (uchar_t *)argv;
3947c478bd9Sstevel@tonic-gate 			break;
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		case CTF_K_STRUCT:
3987c478bd9Sstevel@tonic-gate 		case CTF_K_UNION:
3997c478bd9Sstevel@tonic-gate 			if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
4007c478bd9Sstevel@tonic-gate 				t = ctf_copy_smembers(dtd, (uint_t)(s - s0), t);
4017c478bd9Sstevel@tonic-gate 			else
4027c478bd9Sstevel@tonic-gate 				t = ctf_copy_lmembers(dtd, (uint_t)(s - s0), t);
4037c478bd9Sstevel@tonic-gate 			s = ctf_copy_membnames(dtd, s);
4047c478bd9Sstevel@tonic-gate 			break;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		case CTF_K_ENUM:
4077c478bd9Sstevel@tonic-gate 			t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t);
4087c478bd9Sstevel@tonic-gate 			s = ctf_copy_membnames(dtd, s);
4097c478bd9Sstevel@tonic-gate 			break;
4107c478bd9Sstevel@tonic-gate 		}
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/*
4147c478bd9Sstevel@tonic-gate 	 * Finally, we are ready to ctf_bufopen() the new container.  If this
4157c478bd9Sstevel@tonic-gate 	 * is successful, we then switch nfp and fp and free the old container.
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 	ctf_data_protect(buf, size);
4187c478bd9Sstevel@tonic-gate 	cts.cts_name = _CTF_SECTION;
4197c478bd9Sstevel@tonic-gate 	cts.cts_type = SHT_PROGBITS;
4207c478bd9Sstevel@tonic-gate 	cts.cts_flags = 0;
4217c478bd9Sstevel@tonic-gate 	cts.cts_data = buf;
4227c478bd9Sstevel@tonic-gate 	cts.cts_size = size;
4237c478bd9Sstevel@tonic-gate 	cts.cts_entsize = 1;
4247c478bd9Sstevel@tonic-gate 	cts.cts_offset = 0;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if ((nfp = ctf_bufopen(&cts, NULL, NULL, &err)) == NULL) {
4277c478bd9Sstevel@tonic-gate 		ctf_data_free(buf, size);
4287c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, err));
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	(void) ctf_setmodel(nfp, ctf_getmodel(fp));
4327c478bd9Sstevel@tonic-gate 	(void) ctf_import(nfp, fp->ctf_parent);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	nfp->ctf_refcnt = fp->ctf_refcnt;
4357c478bd9Sstevel@tonic-gate 	nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
4367c478bd9Sstevel@tonic-gate 	nfp->ctf_data.cts_data = NULL; /* force ctf_data_free() on close */
437e4586ebfSmws 	nfp->ctf_dthash = fp->ctf_dthash;
438e4586ebfSmws 	nfp->ctf_dthashlen = fp->ctf_dthashlen;
4397c478bd9Sstevel@tonic-gate 	nfp->ctf_dtdefs = fp->ctf_dtdefs;
4407c478bd9Sstevel@tonic-gate 	nfp->ctf_dtstrlen = fp->ctf_dtstrlen;
4417c478bd9Sstevel@tonic-gate 	nfp->ctf_dtnextid = fp->ctf_dtnextid;
4427c478bd9Sstevel@tonic-gate 	nfp->ctf_dtoldid = fp->ctf_dtnextid - 1;
4437c478bd9Sstevel@tonic-gate 	nfp->ctf_specific = fp->ctf_specific;
4447c478bd9Sstevel@tonic-gate 
445e4586ebfSmws 	fp->ctf_dthash = NULL;
446e4586ebfSmws 	fp->ctf_dthashlen = 0;
4477c478bd9Sstevel@tonic-gate 	bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t));
448e4586ebfSmws 
4497c478bd9Sstevel@tonic-gate 	bcopy(fp, &ofp, sizeof (ctf_file_t));
4507c478bd9Sstevel@tonic-gate 	bcopy(nfp, fp, sizeof (ctf_file_t));
4517c478bd9Sstevel@tonic-gate 	bcopy(&ofp, nfp, sizeof (ctf_file_t));
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/*
4547c478bd9Sstevel@tonic-gate 	 * Initialize the ctf_lookup_by_name top-level dictionary.  We keep an
4557c478bd9Sstevel@tonic-gate 	 * array of type name prefixes and the corresponding ctf_hash to use.
4567c478bd9Sstevel@tonic-gate 	 * NOTE: This code must be kept in sync with the code in ctf_bufopen().
4577c478bd9Sstevel@tonic-gate 	 */
4587c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
4597c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
4607c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
4617c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	nfp->ctf_refcnt = 1; /* force nfp to be freed */
4647c478bd9Sstevel@tonic-gate 	ctf_close(nfp);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	return (0);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
469e4586ebfSmws void
ctf_dtd_insert(ctf_file_t * fp,ctf_dtdef_t * dtd)470e4586ebfSmws ctf_dtd_insert(ctf_file_t *fp, ctf_dtdef_t *dtd)
4717c478bd9Sstevel@tonic-gate {
472e4586ebfSmws 	ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
473e4586ebfSmws 
474e4586ebfSmws 	dtd->dtd_hash = fp->ctf_dthash[h];
475e4586ebfSmws 	fp->ctf_dthash[h] = dtd;
476e4586ebfSmws 	ctf_list_append(&fp->ctf_dtdefs, dtd);
477e4586ebfSmws }
478e4586ebfSmws 
479e4586ebfSmws void
ctf_dtd_delete(ctf_file_t * fp,ctf_dtdef_t * dtd)480e4586ebfSmws ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd)
481e4586ebfSmws {
482e4586ebfSmws 	ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
483e4586ebfSmws 	ctf_dtdef_t *p, **q = &fp->ctf_dthash[h];
4847c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd, *nmd;
4857c478bd9Sstevel@tonic-gate 	size_t len;
486*0a47c91cSRobert Mustacchi 	int kind, i;
4877c478bd9Sstevel@tonic-gate 
488e4586ebfSmws 	for (p = *q; p != NULL; p = p->dtd_hash) {
489e4586ebfSmws 		if (p != dtd)
490e4586ebfSmws 			q = &p->dtd_hash;
491e4586ebfSmws 		else
492e4586ebfSmws 			break;
493e4586ebfSmws 	}
4947c478bd9Sstevel@tonic-gate 
495e4586ebfSmws 	if (p != NULL)
496e4586ebfSmws 		*q = p->dtd_hash;
4977c478bd9Sstevel@tonic-gate 
498*0a47c91cSRobert Mustacchi 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
499*0a47c91cSRobert Mustacchi 	switch (kind) {
5007c478bd9Sstevel@tonic-gate 	case CTF_K_STRUCT:
5017c478bd9Sstevel@tonic-gate 	case CTF_K_UNION:
5027c478bd9Sstevel@tonic-gate 	case CTF_K_ENUM:
5037c478bd9Sstevel@tonic-gate 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
5047c478bd9Sstevel@tonic-gate 		    dmd != NULL; dmd = nmd) {
5057c478bd9Sstevel@tonic-gate 			if (dmd->dmd_name != NULL) {
5067c478bd9Sstevel@tonic-gate 				len = strlen(dmd->dmd_name) + 1;
5077c478bd9Sstevel@tonic-gate 				ctf_free(dmd->dmd_name, len);
5087c478bd9Sstevel@tonic-gate 				fp->ctf_dtstrlen -= len;
5097c478bd9Sstevel@tonic-gate 			}
510*0a47c91cSRobert Mustacchi 			if (kind != CTF_K_ENUM)
511*0a47c91cSRobert Mustacchi 				ctf_ref_dec(fp, dmd->dmd_type);
5127c478bd9Sstevel@tonic-gate 			nmd = ctf_list_next(dmd);
5137c478bd9Sstevel@tonic-gate 			ctf_free(dmd, sizeof (ctf_dmdef_t));
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 		break;
5167c478bd9Sstevel@tonic-gate 	case CTF_K_FUNCTION:
517*0a47c91cSRobert Mustacchi 		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
518*0a47c91cSRobert Mustacchi 		for (i = 0; i < CTF_INFO_VLEN(dtd->dtd_data.ctt_info); i++)
519*0a47c91cSRobert Mustacchi 			if (dtd->dtd_u.dtu_argv[i] != 0)
520*0a47c91cSRobert Mustacchi 				ctf_ref_dec(fp, dtd->dtd_u.dtu_argv[i]);
5217c478bd9Sstevel@tonic-gate 		ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
5227c478bd9Sstevel@tonic-gate 		    CTF_INFO_VLEN(dtd->dtd_data.ctt_info));
5237c478bd9Sstevel@tonic-gate 		break;
524*0a47c91cSRobert Mustacchi 	case CTF_K_ARRAY:
525*0a47c91cSRobert Mustacchi 		ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
526*0a47c91cSRobert Mustacchi 		ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
527*0a47c91cSRobert Mustacchi 		break;
528*0a47c91cSRobert Mustacchi 	case CTF_K_TYPEDEF:
529*0a47c91cSRobert Mustacchi 		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
530*0a47c91cSRobert Mustacchi 		break;
531*0a47c91cSRobert Mustacchi 	case CTF_K_POINTER:
532*0a47c91cSRobert Mustacchi 	case CTF_K_VOLATILE:
533*0a47c91cSRobert Mustacchi 	case CTF_K_CONST:
534*0a47c91cSRobert Mustacchi 	case CTF_K_RESTRICT:
535*0a47c91cSRobert Mustacchi 		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
536*0a47c91cSRobert Mustacchi 		break;
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	if (dtd->dtd_name) {
5407c478bd9Sstevel@tonic-gate 		len = strlen(dtd->dtd_name) + 1;
5417c478bd9Sstevel@tonic-gate 		ctf_free(dtd->dtd_name, len);
5427c478bd9Sstevel@tonic-gate 		fp->ctf_dtstrlen -= len;
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	ctf_list_delete(&fp->ctf_dtdefs, dtd);
5467c478bd9Sstevel@tonic-gate 	ctf_free(dtd, sizeof (ctf_dtdef_t));
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
549e4586ebfSmws ctf_dtdef_t *
ctf_dtd_lookup(ctf_file_t * fp,ctf_id_t type)550e4586ebfSmws ctf_dtd_lookup(ctf_file_t *fp, ctf_id_t type)
551e4586ebfSmws {
552e4586ebfSmws 	ulong_t h = type & (fp->ctf_dthashlen - 1);
553e4586ebfSmws 	ctf_dtdef_t *dtd;
554e4586ebfSmws 
555e4586ebfSmws 	if (fp->ctf_dthash == NULL)
556e4586ebfSmws 		return (NULL);
557e4586ebfSmws 
558e4586ebfSmws 	for (dtd = fp->ctf_dthash[h]; dtd != NULL; dtd = dtd->dtd_hash) {
559e4586ebfSmws 		if (dtd->dtd_type == type)
560e4586ebfSmws 			break;
561e4586ebfSmws 	}
562e4586ebfSmws 
563e4586ebfSmws 	return (dtd);
564e4586ebfSmws }
565e4586ebfSmws 
566e4586ebfSmws /*
567e4586ebfSmws  * Discard all of the dynamic type definitions that have been added to the
568e4586ebfSmws  * container since the last call to ctf_update().  We locate such types by
569e4586ebfSmws  * scanning the list and deleting elements that have type IDs greater than
570*0a47c91cSRobert Mustacchi  * ctf_dtoldid, which is set by ctf_update(), above. Note that to work properly
571*0a47c91cSRobert Mustacchi  * with our reference counting schemes, we must delete the dynamic list in
572*0a47c91cSRobert Mustacchi  * reverse.
573e4586ebfSmws  */
574e4586ebfSmws int
ctf_discard(ctf_file_t * fp)575e4586ebfSmws ctf_discard(ctf_file_t *fp)
576e4586ebfSmws {
577e4586ebfSmws 	ctf_dtdef_t *dtd, *ntd;
578e4586ebfSmws 
579e4586ebfSmws 	if (!(fp->ctf_flags & LCTF_RDWR))
580e4586ebfSmws 		return (ctf_set_errno(fp, ECTF_RDONLY));
581e4586ebfSmws 
582e4586ebfSmws 	if (!(fp->ctf_flags & LCTF_DIRTY))
583e4586ebfSmws 		return (0); /* no update required */
584e4586ebfSmws 
585*0a47c91cSRobert Mustacchi 	for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
586e4586ebfSmws 		if (dtd->dtd_type <= fp->ctf_dtoldid)
587e4586ebfSmws 			continue; /* skip types that have been committed */
588e4586ebfSmws 
589*0a47c91cSRobert Mustacchi 		ntd = ctf_list_prev(dtd);
590e4586ebfSmws 		ctf_dtd_delete(fp, dtd);
591e4586ebfSmws 	}
592e4586ebfSmws 
5937c478bd9Sstevel@tonic-gate 	fp->ctf_dtnextid = fp->ctf_dtoldid + 1;
5947c478bd9Sstevel@tonic-gate 	fp->ctf_flags &= ~LCTF_DIRTY;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	return (0);
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate static ctf_id_t
ctf_add_generic(ctf_file_t * fp,uint_t flag,const char * name,ctf_dtdef_t ** rp)6007c478bd9Sstevel@tonic-gate ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
6037c478bd9Sstevel@tonic-gate 	ctf_id_t type;
6047c478bd9Sstevel@tonic-gate 	char *s = NULL;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
6077c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
6107c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if (CTF_INDEX_TO_TYPE(fp->ctf_dtnextid, 1) > CTF_MAX_TYPE)
6137c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_FULL));
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL)
6167c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
6197c478bd9Sstevel@tonic-gate 		ctf_free(dtd, sizeof (ctf_dtdef_t));
6207c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	type = fp->ctf_dtnextid++;
6247c478bd9Sstevel@tonic-gate 	type = CTF_INDEX_TO_TYPE(type, (fp->ctf_flags & LCTF_CHILD));
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	bzero(dtd, sizeof (ctf_dtdef_t));
6277c478bd9Sstevel@tonic-gate 	dtd->dtd_name = s;
6287c478bd9Sstevel@tonic-gate 	dtd->dtd_type = type;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if (s != NULL)
6317c478bd9Sstevel@tonic-gate 		fp->ctf_dtstrlen += strlen(s) + 1;
6327c478bd9Sstevel@tonic-gate 
633e4586ebfSmws 	ctf_dtd_insert(fp, dtd);
6347c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	*rp = dtd;
6377c478bd9Sstevel@tonic-gate 	return (type);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate /*
6417c478bd9Sstevel@tonic-gate  * When encoding integer sizes, we want to convert a byte count in the range
6427c478bd9Sstevel@tonic-gate  * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc).  The clp2() function
6437c478bd9Sstevel@tonic-gate  * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate static size_t
clp2(size_t x)6467c478bd9Sstevel@tonic-gate clp2(size_t x)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	x--;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	x |= (x >> 1);
6517c478bd9Sstevel@tonic-gate 	x |= (x >> 2);
6527c478bd9Sstevel@tonic-gate 	x |= (x >> 4);
6537c478bd9Sstevel@tonic-gate 	x |= (x >> 8);
6547c478bd9Sstevel@tonic-gate 	x |= (x >> 16);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	return (x + 1);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate static ctf_id_t
ctf_add_encoded(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep,uint_t kind)6607c478bd9Sstevel@tonic-gate ctf_add_encoded(ctf_file_t *fp, uint_t flag,
6617c478bd9Sstevel@tonic-gate     const char *name, const ctf_encoding_t *ep, uint_t kind)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
6647c478bd9Sstevel@tonic-gate 	ctf_id_t type;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (ep == NULL)
6677c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
6707c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
6737c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_size = clp2(P2ROUNDUP(ep->cte_bits, NBBY) / NBBY);
6747c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_enc = *ep;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	return (type);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate static ctf_id_t
ctf_add_reftype(ctf_file_t * fp,uint_t flag,ctf_id_t ref,uint_t kind)6807c478bd9Sstevel@tonic-gate ctf_add_reftype(ctf_file_t *fp, uint_t flag, ctf_id_t ref, uint_t kind)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
6837c478bd9Sstevel@tonic-gate 	ctf_id_t type;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE)
6867c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
6897c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
6907c478bd9Sstevel@tonic-gate 
691*0a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, ref);
692*0a47c91cSRobert Mustacchi 
6937c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
6947c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_type = (ushort_t)ref;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	return (type);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_integer(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep)7007c478bd9Sstevel@tonic-gate ctf_add_integer(ctf_file_t *fp, uint_t flag,
7017c478bd9Sstevel@tonic-gate     const char *name, const ctf_encoding_t *ep)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER));
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_float(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep)7077c478bd9Sstevel@tonic-gate ctf_add_float(ctf_file_t *fp, uint_t flag,
7087c478bd9Sstevel@tonic-gate     const char *name, const ctf_encoding_t *ep)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT));
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_pointer(ctf_file_t * fp,uint_t flag,ctf_id_t ref)7147c478bd9Sstevel@tonic-gate ctf_add_pointer(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	return (ctf_add_reftype(fp, flag, ref, CTF_K_POINTER));
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_array(ctf_file_t * fp,uint_t flag,const ctf_arinfo_t * arp)7207c478bd9Sstevel@tonic-gate ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
7237c478bd9Sstevel@tonic-gate 	ctf_id_t type;
724*0a47c91cSRobert Mustacchi 	ctf_file_t *fpd;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (arp == NULL)
7277c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
7287c478bd9Sstevel@tonic-gate 
729*0a47c91cSRobert Mustacchi 	fpd = fp;
730*0a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
731*0a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)
732*0a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
733*0a47c91cSRobert Mustacchi 
734*0a47c91cSRobert Mustacchi 	fpd = fp;
735*0a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
736*0a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, arp->ctr_index) == NULL)
737*0a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
738*0a47c91cSRobert Mustacchi 
7397c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
7407c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, flag, 0);
7437c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_size = 0;
7447c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_arr = *arp;
745*0a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, arp->ctr_contents);
746*0a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, arp->ctr_index);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	return (type);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate int
ctf_set_array(ctf_file_t * fp,ctf_id_t type,const ctf_arinfo_t * arp)7527c478bd9Sstevel@tonic-gate ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
7537c478bd9Sstevel@tonic-gate {
754*0a47c91cSRobert Mustacchi 	ctf_file_t *fpd;
755e4586ebfSmws 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
7587c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	if (dtd == NULL || CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
7617c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_BADID));
7627c478bd9Sstevel@tonic-gate 
763*0a47c91cSRobert Mustacchi 	fpd = fp;
764*0a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
765*0a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)
766*0a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
767*0a47c91cSRobert Mustacchi 
768*0a47c91cSRobert Mustacchi 	fpd = fp;
769*0a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
770*0a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, arp->ctr_index) == NULL)
771*0a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
772*0a47c91cSRobert Mustacchi 
773*0a47c91cSRobert Mustacchi 	ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
774*0a47c91cSRobert Mustacchi 	ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
7757c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
7767c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_arr = *arp;
777*0a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, arp->ctr_contents);
778*0a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, arp->ctr_index);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	return (0);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_function(ctf_file_t * fp,uint_t flag,const ctf_funcinfo_t * ctc,const ctf_id_t * argv)7847c478bd9Sstevel@tonic-gate ctf_add_function(ctf_file_t *fp, uint_t flag,
7857c478bd9Sstevel@tonic-gate     const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
7887c478bd9Sstevel@tonic-gate 	ctf_id_t type;
7897c478bd9Sstevel@tonic-gate 	uint_t vlen;
790*0a47c91cSRobert Mustacchi 	int i;
7917c478bd9Sstevel@tonic-gate 	ctf_id_t *vdat = NULL;
792*0a47c91cSRobert Mustacchi 	ctf_file_t *fpd;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 ||
7957c478bd9Sstevel@tonic-gate 	    (ctc->ctc_argc != 0 && argv == NULL))
7967c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	vlen = ctc->ctc_argc;
7997c478bd9Sstevel@tonic-gate 	if (ctc->ctc_flags & CTF_FUNC_VARARG)
8007c478bd9Sstevel@tonic-gate 		vlen++; /* add trailing zero to indicate varargs (see below) */
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if (vlen > CTF_MAX_VLEN)
8037c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EOVERFLOW));
8047c478bd9Sstevel@tonic-gate 
805*0a47c91cSRobert Mustacchi 	fpd = fp;
806*0a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, ctc->ctc_return) == NULL &&
807*0a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, ctc->ctc_return) == NULL)
808*0a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
809*0a47c91cSRobert Mustacchi 
810*0a47c91cSRobert Mustacchi 	for (i = 0; i < ctc->ctc_argc; i++) {
811*0a47c91cSRobert Mustacchi 		fpd = fp;
812*0a47c91cSRobert Mustacchi 		if (ctf_lookup_by_id(&fpd, argv[i]) == NULL &&
813*0a47c91cSRobert Mustacchi 		    ctf_dtd_lookup(fp, argv[i]) == NULL)
814*0a47c91cSRobert Mustacchi 			return (ctf_set_errno(fp, ECTF_BADID));
815*0a47c91cSRobert Mustacchi 	}
816*0a47c91cSRobert Mustacchi 
8177c478bd9Sstevel@tonic-gate 	if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL)
8187c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) {
8217c478bd9Sstevel@tonic-gate 		ctf_free(vdat, sizeof (ctf_id_t) * vlen);
8227c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, flag, vlen);
8267c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_type = (ushort_t)ctc->ctc_return;
8277c478bd9Sstevel@tonic-gate 
828*0a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, ctc->ctc_return);
829*0a47c91cSRobert Mustacchi 	for (i = 0; i < ctc->ctc_argc; i++)
830*0a47c91cSRobert Mustacchi 		ctf_ref_inc(fp, argv[i]);
831*0a47c91cSRobert Mustacchi 
8327c478bd9Sstevel@tonic-gate 	bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc);
8337c478bd9Sstevel@tonic-gate 	if (ctc->ctc_flags & CTF_FUNC_VARARG)
8347c478bd9Sstevel@tonic-gate 		vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */
8357c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_argv = vdat;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	return (type);
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_struct(ctf_file_t * fp,uint_t flag,const char * name)8417c478bd9Sstevel@tonic-gate ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name)
8427c478bd9Sstevel@tonic-gate {
843e4586ebfSmws 	ctf_hash_t *hp = &fp->ctf_structs;
844e4586ebfSmws 	ctf_helem_t *hep = NULL;
8457c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
8467c478bd9Sstevel@tonic-gate 	ctf_id_t type;
8477c478bd9Sstevel@tonic-gate 
848e4586ebfSmws 	if (name != NULL)
849e4586ebfSmws 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
850e4586ebfSmws 
851e4586ebfSmws 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
852e4586ebfSmws 		dtd = ctf_dtd_lookup(fp, type = hep->h_type);
853e4586ebfSmws 	else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
8547c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, flag, 0);
857e4586ebfSmws 	dtd->dtd_data.ctt_size = 0;
858e4586ebfSmws 
8597c478bd9Sstevel@tonic-gate 	return (type);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_union(ctf_file_t * fp,uint_t flag,const char * name)8637c478bd9Sstevel@tonic-gate ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name)
8647c478bd9Sstevel@tonic-gate {
865e4586ebfSmws 	ctf_hash_t *hp = &fp->ctf_unions;
866e4586ebfSmws 	ctf_helem_t *hep = NULL;
8677c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
8687c478bd9Sstevel@tonic-gate 	ctf_id_t type;
8697c478bd9Sstevel@tonic-gate 
870e4586ebfSmws 	if (name != NULL)
871e4586ebfSmws 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
872e4586ebfSmws 
873e4586ebfSmws 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
874e4586ebfSmws 		dtd = ctf_dtd_lookup(fp, type = hep->h_type);
875e4586ebfSmws 	else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
8767c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, flag, 0);
879e4586ebfSmws 	dtd->dtd_data.ctt_size = 0;
880e4586ebfSmws 
8817c478bd9Sstevel@tonic-gate 	return (type);
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_enum(ctf_file_t * fp,uint_t flag,const char * name)8857c478bd9Sstevel@tonic-gate ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name)
8867c478bd9Sstevel@tonic-gate {
887e4586ebfSmws 	ctf_hash_t *hp = &fp->ctf_enums;
888e4586ebfSmws 	ctf_helem_t *hep = NULL;
8897c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
8907c478bd9Sstevel@tonic-gate 	ctf_id_t type;
8917c478bd9Sstevel@tonic-gate 
892e4586ebfSmws 	if (name != NULL)
893e4586ebfSmws 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
894e4586ebfSmws 
895e4586ebfSmws 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
896e4586ebfSmws 		dtd = ctf_dtd_lookup(fp, type = hep->h_type);
897e4586ebfSmws 	else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
8987c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0);
9017c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	return (type);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_forward(ctf_file_t * fp,uint_t flag,const char * name,uint_t kind)9077c478bd9Sstevel@tonic-gate ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind)
9087c478bd9Sstevel@tonic-gate {
909e4586ebfSmws 	ctf_hash_t *hp;
910e4586ebfSmws 	ctf_helem_t *hep;
9117c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
9127c478bd9Sstevel@tonic-gate 	ctf_id_t type;
9137c478bd9Sstevel@tonic-gate 
914e4586ebfSmws 	switch (kind) {
915e4586ebfSmws 	case CTF_K_STRUCT:
916e4586ebfSmws 		hp = &fp->ctf_structs;
917e4586ebfSmws 		break;
918e4586ebfSmws 	case CTF_K_UNION:
919e4586ebfSmws 		hp = &fp->ctf_unions;
920e4586ebfSmws 		break;
921e4586ebfSmws 	case CTF_K_ENUM:
922e4586ebfSmws 		hp = &fp->ctf_enums;
923e4586ebfSmws 		break;
924e4586ebfSmws 	default:
9257c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOTSUE));
926e4586ebfSmws 	}
927e4586ebfSmws 
928e4586ebfSmws 	/*
929e4586ebfSmws 	 * If the type is already defined or exists as a forward tag, just
930e4586ebfSmws 	 * return the ctf_id_t of the existing definition.
931e4586ebfSmws 	 */
932e4586ebfSmws 	if (name != NULL && (hep = ctf_hash_lookup(hp,
933e4586ebfSmws 	    fp, name, strlen(name))) != NULL)
934e4586ebfSmws 		return (hep->h_type);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
9377c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0);
940e4586ebfSmws 	dtd->dtd_data.ctt_type = kind;
941e4586ebfSmws 
9427c478bd9Sstevel@tonic-gate 	return (type);
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_typedef(ctf_file_t * fp,uint_t flag,const char * name,ctf_id_t ref)9467c478bd9Sstevel@tonic-gate ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
9477c478bd9Sstevel@tonic-gate {
9487c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
9497c478bd9Sstevel@tonic-gate 	ctf_id_t type;
950*0a47c91cSRobert Mustacchi 	ctf_file_t *fpd;
9517c478bd9Sstevel@tonic-gate 
952*0a47c91cSRobert Mustacchi 	fpd = fp;
953*0a47c91cSRobert Mustacchi 	if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL &&
954*0a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, ref) == NULL))
9557c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
9587c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0);
9617c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_type = (ushort_t)ref;
962*0a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, ref);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	return (type);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_volatile(ctf_file_t * fp,uint_t flag,ctf_id_t ref)9687c478bd9Sstevel@tonic-gate ctf_add_volatile(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	return (ctf_add_reftype(fp, flag, ref, CTF_K_VOLATILE));
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_const(ctf_file_t * fp,uint_t flag,ctf_id_t ref)9747c478bd9Sstevel@tonic-gate ctf_add_const(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
9757c478bd9Sstevel@tonic-gate {
9767c478bd9Sstevel@tonic-gate 	return (ctf_add_reftype(fp, flag, ref, CTF_K_CONST));
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_restrict(ctf_file_t * fp,uint_t flag,ctf_id_t ref)9807c478bd9Sstevel@tonic-gate ctf_add_restrict(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate 	return (ctf_add_reftype(fp, flag, ref, CTF_K_RESTRICT));
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate int
ctf_add_enumerator(ctf_file_t * fp,ctf_id_t enid,const char * name,int value)9867c478bd9Sstevel@tonic-gate ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
9877c478bd9Sstevel@tonic-gate {
988e4586ebfSmws 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
9897c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	uint_t kind, vlen, root;
9927c478bd9Sstevel@tonic-gate 	char *s;
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	if (name == NULL)
9957c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
9987c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	if (dtd == NULL)
10017c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_BADID));
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
10047c478bd9Sstevel@tonic-gate 	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
10057c478bd9Sstevel@tonic-gate 	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	if (kind != CTF_K_ENUM)
10087c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOTENUM));
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (vlen == CTF_MAX_VLEN)
10117c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_DTFULL));
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
10147c478bd9Sstevel@tonic-gate 	    dmd != NULL; dmd = ctf_list_next(dmd)) {
10157c478bd9Sstevel@tonic-gate 		if (strcmp(dmd->dmd_name, name) == 0)
10167c478bd9Sstevel@tonic-gate 			return (ctf_set_errno(fp, ECTF_DUPMEMBER));
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
10207c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	if ((s = ctf_strdup(name)) == NULL) {
10237c478bd9Sstevel@tonic-gate 		ctf_free(dmd, sizeof (ctf_dmdef_t));
10247c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	dmd->dmd_name = s;
10287c478bd9Sstevel@tonic-gate 	dmd->dmd_type = CTF_ERR;
10297c478bd9Sstevel@tonic-gate 	dmd->dmd_offset = 0;
10307c478bd9Sstevel@tonic-gate 	dmd->dmd_value = value;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
10337c478bd9Sstevel@tonic-gate 	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	fp->ctf_dtstrlen += strlen(s) + 1;
10367c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	return (0);
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate int
ctf_add_member(ctf_file_t * fp,ctf_id_t souid,const char * name,ctf_id_t type)10427c478bd9Sstevel@tonic-gate ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type)
10437c478bd9Sstevel@tonic-gate {
1044e4586ebfSmws 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
10457c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	ssize_t msize, malign, ssize;
10487c478bd9Sstevel@tonic-gate 	uint_t kind, vlen, root;
10497c478bd9Sstevel@tonic-gate 	char *s = NULL;
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
10527c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if (dtd == NULL)
10557c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_BADID));
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
10587c478bd9Sstevel@tonic-gate 	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
10597c478bd9Sstevel@tonic-gate 	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
10627c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOTSOU));
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	if (vlen == CTF_MAX_VLEN)
10657c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_DTFULL));
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	if (name != NULL) {
10687c478bd9Sstevel@tonic-gate 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
10697c478bd9Sstevel@tonic-gate 		    dmd != NULL; dmd = ctf_list_next(dmd)) {
10707c478bd9Sstevel@tonic-gate 			if (dmd->dmd_name != NULL &&
10717c478bd9Sstevel@tonic-gate 			    strcmp(dmd->dmd_name, name) == 0)
10727c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(fp, ECTF_DUPMEMBER));
10737c478bd9Sstevel@tonic-gate 		}
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
10777c478bd9Sstevel@tonic-gate 	    (malign = ctf_type_align(fp, type)) == CTF_ERR)
10787c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
10817c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
10847c478bd9Sstevel@tonic-gate 		ctf_free(dmd, sizeof (ctf_dmdef_t));
10857c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	dmd->dmd_name = s;
10897c478bd9Sstevel@tonic-gate 	dmd->dmd_type = type;
10907c478bd9Sstevel@tonic-gate 	dmd->dmd_value = -1;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_STRUCT && vlen != 0) {
10937c478bd9Sstevel@tonic-gate 		ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
10947c478bd9Sstevel@tonic-gate 		ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
10957c478bd9Sstevel@tonic-gate 		size_t off = lmd->dmd_offset;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 		ctf_encoding_t linfo;
10987c478bd9Sstevel@tonic-gate 		ssize_t lsize;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
11017c478bd9Sstevel@tonic-gate 			off += linfo.cte_bits;
11027c478bd9Sstevel@tonic-gate 		else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
11037c478bd9Sstevel@tonic-gate 			off += lsize * NBBY;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		/*
11067c478bd9Sstevel@tonic-gate 		 * Round up the offset of the end of the last member to the
11077c478bd9Sstevel@tonic-gate 		 * next byte boundary, convert 'off' to bytes, and then round
11087c478bd9Sstevel@tonic-gate 		 * it up again to the next multiple of the alignment required
11097c478bd9Sstevel@tonic-gate 		 * by the new member.  Finally, convert back to bits and store
11107c478bd9Sstevel@tonic-gate 		 * the result in dmd_offset.  Technically we could do more
11117c478bd9Sstevel@tonic-gate 		 * efficient packing if the new member is a bit-field, but
11127c478bd9Sstevel@tonic-gate 		 * we're the "compiler" and ANSI says we can do as we choose.
11137c478bd9Sstevel@tonic-gate 		 */
11147c478bd9Sstevel@tonic-gate 		off = roundup(off, NBBY) / NBBY;
11157c478bd9Sstevel@tonic-gate 		off = roundup(off, MAX(malign, 1));
11167c478bd9Sstevel@tonic-gate 		dmd->dmd_offset = off * NBBY;
11177c478bd9Sstevel@tonic-gate 		ssize = off + msize;
11187c478bd9Sstevel@tonic-gate 	} else {
11197c478bd9Sstevel@tonic-gate 		dmd->dmd_offset = 0;
11207c478bd9Sstevel@tonic-gate 		ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
11217c478bd9Sstevel@tonic-gate 		ssize = MAX(ssize, msize);
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	if (ssize > CTF_MAX_SIZE) {
11257c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
11267c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
11277c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
11287c478bd9Sstevel@tonic-gate 	} else
11297c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_size = (ushort_t)ssize;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
11327c478bd9Sstevel@tonic-gate 	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	if (s != NULL)
11357c478bd9Sstevel@tonic-gate 		fp->ctf_dtstrlen += strlen(s) + 1;
11367c478bd9Sstevel@tonic-gate 
1137*0a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, type);
1138*0a47c91cSRobert Mustacchi 	fp->ctf_flags |= LCTF_DIRTY;
1139*0a47c91cSRobert Mustacchi 	return (0);
1140*0a47c91cSRobert Mustacchi }
1141*0a47c91cSRobert Mustacchi 
1142*0a47c91cSRobert Mustacchi /*
1143*0a47c91cSRobert Mustacchi  * This removes a type from the dynamic section. This will fail if the type is
1144*0a47c91cSRobert Mustacchi  * referenced by another type. Note that the CTF ID is never reused currently by
1145*0a47c91cSRobert Mustacchi  * CTF. Note that if this container is a parent container then we just outright
1146*0a47c91cSRobert Mustacchi  * refuse to remove the type. There currently is no notion of searching for the
1147*0a47c91cSRobert Mustacchi  * ctf_dtdef_t in parent containers. If there is, then this constraint could
1148*0a47c91cSRobert Mustacchi  * become finer grained.
1149*0a47c91cSRobert Mustacchi  */
1150*0a47c91cSRobert Mustacchi int
ctf_delete_type(ctf_file_t * fp,ctf_id_t type)1151*0a47c91cSRobert Mustacchi ctf_delete_type(ctf_file_t *fp, ctf_id_t type)
1152*0a47c91cSRobert Mustacchi {
1153*0a47c91cSRobert Mustacchi 	ctf_file_t *fpd;
1154*0a47c91cSRobert Mustacchi 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
1155*0a47c91cSRobert Mustacchi 
1156*0a47c91cSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
1157*0a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_RDONLY));
1158*0a47c91cSRobert Mustacchi 
1159*0a47c91cSRobert Mustacchi 	/*
1160*0a47c91cSRobert Mustacchi 	 * We want to give as useful an errno as possible. That means that we
1161*0a47c91cSRobert Mustacchi 	 * want to distinguish between a type which does not exist and one for
1162*0a47c91cSRobert Mustacchi 	 * which the type is not dynamic.
1163*0a47c91cSRobert Mustacchi 	 */
1164*0a47c91cSRobert Mustacchi 	fpd = fp;
1165*0a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, type) == NULL &&
1166*0a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, type) == NULL)
1167*0a47c91cSRobert Mustacchi 		return (CTF_ERR); /* errno is set for us */
1168*0a47c91cSRobert Mustacchi 
1169*0a47c91cSRobert Mustacchi 	if (dtd == NULL)
1170*0a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_NOTDYN));
1171*0a47c91cSRobert Mustacchi 
1172*0a47c91cSRobert Mustacchi 	if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1)
1173*0a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_REFERENCED));
1174*0a47c91cSRobert Mustacchi 
1175*0a47c91cSRobert Mustacchi 	ctf_dtd_delete(fp, dtd);
11767c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
11777c478bd9Sstevel@tonic-gate 	return (0);
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate static int
enumcmp(const char * name,int value,void * arg)11817c478bd9Sstevel@tonic-gate enumcmp(const char *name, int value, void *arg)
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
11847c478bd9Sstevel@tonic-gate 	int bvalue;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type,
11877c478bd9Sstevel@tonic-gate 	    name, &bvalue) == CTF_ERR || value != bvalue);
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate static int
enumadd(const char * name,int value,void * arg)11917c478bd9Sstevel@tonic-gate enumadd(const char *name, int value, void *arg)
11927c478bd9Sstevel@tonic-gate {
11937c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type,
11967c478bd9Sstevel@tonic-gate 	    name, value) == CTF_ERR);
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12007c478bd9Sstevel@tonic-gate static int
membcmp(const char * name,ctf_id_t type,ulong_t offset,void * arg)12017c478bd9Sstevel@tonic-gate membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg)
12027c478bd9Sstevel@tonic-gate {
12037c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
12047c478bd9Sstevel@tonic-gate 	ctf_membinfo_t ctm;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	return (ctf_member_info(ctb->ctb_file, ctb->ctb_type,
12077c478bd9Sstevel@tonic-gate 	    name, &ctm) == CTF_ERR || ctm.ctm_offset != offset);
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate static int
membadd(const char * name,ctf_id_t type,ulong_t offset,void * arg)12117c478bd9Sstevel@tonic-gate membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg)
12127c478bd9Sstevel@tonic-gate {
12137c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
12147c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd;
12157c478bd9Sstevel@tonic-gate 	char *s = NULL;
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
12187c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(ctb->ctb_file, EAGAIN));
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
12217c478bd9Sstevel@tonic-gate 		ctf_free(dmd, sizeof (ctf_dmdef_t));
12227c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(ctb->ctb_file, EAGAIN));
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	/*
12267c478bd9Sstevel@tonic-gate 	 * For now, dmd_type is copied as the src_fp's type; it is reset to an
12277c478bd9Sstevel@tonic-gate 	 * equivalent dst_fp type by a final loop in ctf_add_type(), below.
12287c478bd9Sstevel@tonic-gate 	 */
12297c478bd9Sstevel@tonic-gate 	dmd->dmd_name = s;
12307c478bd9Sstevel@tonic-gate 	dmd->dmd_type = type;
12317c478bd9Sstevel@tonic-gate 	dmd->dmd_offset = offset;
12327c478bd9Sstevel@tonic-gate 	dmd->dmd_value = -1;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	if (s != NULL)
12377c478bd9Sstevel@tonic-gate 		ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1;
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
12407c478bd9Sstevel@tonic-gate 	return (0);
12417c478bd9Sstevel@tonic-gate }
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate /*
12447c478bd9Sstevel@tonic-gate  * The ctf_add_type routine is used to copy a type from a source CTF container
12457c478bd9Sstevel@tonic-gate  * to a dynamic destination container.  This routine operates recursively by
12467c478bd9Sstevel@tonic-gate  * following the source type's links and embedded member types.  If the
12477c478bd9Sstevel@tonic-gate  * destination container already contains a named type which has the same
12487c478bd9Sstevel@tonic-gate  * attributes, then we succeed and return this type but no changes occur.
12497c478bd9Sstevel@tonic-gate  */
12507c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_type(ctf_file_t * dst_fp,ctf_file_t * src_fp,ctf_id_t src_type)12517c478bd9Sstevel@tonic-gate ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
12527c478bd9Sstevel@tonic-gate {
12537c478bd9Sstevel@tonic-gate 	ctf_id_t dst_type = CTF_ERR;
12547c478bd9Sstevel@tonic-gate 	uint_t dst_kind = CTF_K_UNKNOWN;
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	const ctf_type_t *tp;
12577c478bd9Sstevel@tonic-gate 	const char *name;
12587c478bd9Sstevel@tonic-gate 	uint_t kind, flag, vlen;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	ctf_bundle_t src, dst;
12617c478bd9Sstevel@tonic-gate 	ctf_encoding_t src_en, dst_en;
12627c478bd9Sstevel@tonic-gate 	ctf_arinfo_t src_ar, dst_ar;
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
12657c478bd9Sstevel@tonic-gate 	ctf_funcinfo_t ctc;
12667c478bd9Sstevel@tonic-gate 	ssize_t size;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	ctf_hash_t *hp;
12697c478bd9Sstevel@tonic-gate 	ctf_helem_t *hep;
12707c478bd9Sstevel@tonic-gate 
1271*0a47c91cSRobert Mustacchi 	if (dst_fp == src_fp)
1272*0a47c91cSRobert Mustacchi 		return (src_type);
1273*0a47c91cSRobert Mustacchi 
12747c478bd9Sstevel@tonic-gate 	if (!(dst_fp->ctf_flags & LCTF_RDWR))
12757c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ECTF_RDONLY));
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)
12787c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	name = ctf_strptr(src_fp, tp->ctt_name);
12817c478bd9Sstevel@tonic-gate 	kind = LCTF_INFO_KIND(src_fp, tp->ctt_info);
12827c478bd9Sstevel@tonic-gate 	flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info);
12837c478bd9Sstevel@tonic-gate 	vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info);
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	switch (kind) {
12867c478bd9Sstevel@tonic-gate 	case CTF_K_STRUCT:
12877c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_structs;
12887c478bd9Sstevel@tonic-gate 		break;
12897c478bd9Sstevel@tonic-gate 	case CTF_K_UNION:
12907c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_unions;
12917c478bd9Sstevel@tonic-gate 		break;
12927c478bd9Sstevel@tonic-gate 	case CTF_K_ENUM:
12937c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_enums;
12947c478bd9Sstevel@tonic-gate 		break;
12957c478bd9Sstevel@tonic-gate 	default:
12967c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_names;
12977c478bd9Sstevel@tonic-gate 		break;
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	/*
13017c478bd9Sstevel@tonic-gate 	 * If the source type has a name and is a root type (visible at the
13027c478bd9Sstevel@tonic-gate 	 * top-level scope), lookup the name in the destination container and
13037c478bd9Sstevel@tonic-gate 	 * verify that it is of the same kind before we do anything else.
13047c478bd9Sstevel@tonic-gate 	 */
13057c478bd9Sstevel@tonic-gate 	if ((flag & CTF_ADD_ROOT) && name[0] != '\0' &&
13067c478bd9Sstevel@tonic-gate 	    (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) {
13077c478bd9Sstevel@tonic-gate 		dst_type = (ctf_id_t)hep->h_type;
13087c478bd9Sstevel@tonic-gate 		dst_kind = ctf_type_kind(dst_fp, dst_type);
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	/*
13127c478bd9Sstevel@tonic-gate 	 * If an identically named dst_type exists, fail with ECTF_CONFLICT
13137c478bd9Sstevel@tonic-gate 	 * unless dst_type is a forward declaration and src_type is a struct,
13147c478bd9Sstevel@tonic-gate 	 * union, or enum (i.e. the definition of the previous forward decl).
13157c478bd9Sstevel@tonic-gate 	 */
13167c478bd9Sstevel@tonic-gate 	if (dst_type != CTF_ERR && dst_kind != kind && (
13177c478bd9Sstevel@tonic-gate 	    dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM &&
13187c478bd9Sstevel@tonic-gate 	    kind != CTF_K_STRUCT && kind != CTF_K_UNION)))
13197c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	/*
13227c478bd9Sstevel@tonic-gate 	 * If the non-empty name was not found in the appropriate hash, search
13237c478bd9Sstevel@tonic-gate 	 * the list of pending dynamic definitions that are not yet committed.
13247c478bd9Sstevel@tonic-gate 	 * If a matching name and kind are found, assume this is the type that
13257c478bd9Sstevel@tonic-gate 	 * we are looking for.  This is necessary to permit ctf_add_type() to
13267c478bd9Sstevel@tonic-gate 	 * operate recursively on entities such as a struct that contains a
13277c478bd9Sstevel@tonic-gate 	 * pointer member that refers to the same struct type.
13287c478bd9Sstevel@tonic-gate 	 */
13297c478bd9Sstevel@tonic-gate 	if (dst_type == CTF_ERR && name[0] != '\0') {
13307c478bd9Sstevel@tonic-gate 		for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL &&
13317c478bd9Sstevel@tonic-gate 		    dtd->dtd_type > dst_fp->ctf_dtoldid;
13327c478bd9Sstevel@tonic-gate 		    dtd = ctf_list_prev(dtd)) {
13337c478bd9Sstevel@tonic-gate 			if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) == kind &&
13347c478bd9Sstevel@tonic-gate 			    dtd->dtd_name != NULL &&
13357c478bd9Sstevel@tonic-gate 			    strcmp(dtd->dtd_name, name) == 0)
13367c478bd9Sstevel@tonic-gate 				return (dtd->dtd_type);
13377c478bd9Sstevel@tonic-gate 		}
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	src.ctb_file = src_fp;
13417c478bd9Sstevel@tonic-gate 	src.ctb_type = src_type;
13427c478bd9Sstevel@tonic-gate 	src.ctb_dtd = NULL;
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	dst.ctb_file = dst_fp;
13457c478bd9Sstevel@tonic-gate 	dst.ctb_type = dst_type;
13467c478bd9Sstevel@tonic-gate 	dst.ctb_dtd = NULL;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	/*
13497c478bd9Sstevel@tonic-gate 	 * Now perform kind-specific processing.  If dst_type is CTF_ERR, then
13507c478bd9Sstevel@tonic-gate 	 * we add a new type with the same properties as src_type to dst_fp.
13517c478bd9Sstevel@tonic-gate 	 * If dst_type is not CTF_ERR, then we verify that dst_type has the
13527c478bd9Sstevel@tonic-gate 	 * same attributes as src_type.  We recurse for embedded references.
13537c478bd9Sstevel@tonic-gate 	 */
13547c478bd9Sstevel@tonic-gate 	switch (kind) {
13557c478bd9Sstevel@tonic-gate 	case CTF_K_INTEGER:
13567c478bd9Sstevel@tonic-gate 	case CTF_K_FLOAT:
13577c478bd9Sstevel@tonic-gate 		if (ctf_type_encoding(src_fp, src_type, &src_en) != 0)
13587c478bd9Sstevel@tonic-gate 			return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR) {
13617c478bd9Sstevel@tonic-gate 			if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0)
13627c478bd9Sstevel@tonic-gate 				return (CTF_ERR); /* errno is set for us */
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 			if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t)))
13657c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 		} else if (kind == CTF_K_INTEGER) {
13687c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_integer(dst_fp, flag, name, &src_en);
13697c478bd9Sstevel@tonic-gate 		} else
13707c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_float(dst_fp, flag, name, &src_en);
13717c478bd9Sstevel@tonic-gate 		break;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	case CTF_K_POINTER:
13747c478bd9Sstevel@tonic-gate 	case CTF_K_VOLATILE:
13757c478bd9Sstevel@tonic-gate 	case CTF_K_CONST:
13767c478bd9Sstevel@tonic-gate 	case CTF_K_RESTRICT:
13777c478bd9Sstevel@tonic-gate 		src_type = ctf_type_reference(src_fp, src_type);
13787c478bd9Sstevel@tonic-gate 		src_type = ctf_add_type(dst_fp, src_fp, src_type);
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 		if (src_type == CTF_ERR)
13817c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 		dst_type = ctf_add_reftype(dst_fp, flag, src_type, kind);
13847c478bd9Sstevel@tonic-gate 		break;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	case CTF_K_ARRAY:
13877c478bd9Sstevel@tonic-gate 		if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR)
13887c478bd9Sstevel@tonic-gate 			return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 		src_ar.ctr_contents =
13917c478bd9Sstevel@tonic-gate 		    ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents);
13927c478bd9Sstevel@tonic-gate 		src_ar.ctr_index =
13937c478bd9Sstevel@tonic-gate 		    ctf_add_type(dst_fp, src_fp, src_ar.ctr_index);
13947c478bd9Sstevel@tonic-gate 		src_ar.ctr_nelems = src_ar.ctr_nelems;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 		if (src_ar.ctr_contents == CTF_ERR ||
13977c478bd9Sstevel@tonic-gate 		    src_ar.ctr_index == CTF_ERR)
13987c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR) {
14017c478bd9Sstevel@tonic-gate 			if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0)
14027c478bd9Sstevel@tonic-gate 				return (CTF_ERR); /* errno is set for us */
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 			if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
14057c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
14067c478bd9Sstevel@tonic-gate 		} else
14077c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_array(dst_fp, flag, &src_ar);
14087c478bd9Sstevel@tonic-gate 		break;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	case CTF_K_FUNCTION:
14117c478bd9Sstevel@tonic-gate 		ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type);
14127c478bd9Sstevel@tonic-gate 		ctc.ctc_argc = 0;
14137c478bd9Sstevel@tonic-gate 		ctc.ctc_flags = 0;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 		if (ctc.ctc_return == CTF_ERR)
14167c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 		dst_type = ctf_add_function(dst_fp, flag, &ctc, NULL);
14197c478bd9Sstevel@tonic-gate 		break;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	case CTF_K_STRUCT:
14227c478bd9Sstevel@tonic-gate 	case CTF_K_UNION: {
14237c478bd9Sstevel@tonic-gate 		ctf_dmdef_t *dmd;
14247c478bd9Sstevel@tonic-gate 		int errs = 0;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 		/*
14277c478bd9Sstevel@tonic-gate 		 * Technically to match a struct or union we need to check both
14287c478bd9Sstevel@tonic-gate 		 * ways (src members vs. dst, dst members vs. src) but we make
14297c478bd9Sstevel@tonic-gate 		 * this more optimal by only checking src vs. dst and comparing
14307c478bd9Sstevel@tonic-gate 		 * the total size of the structure (which we must do anyway)
14317c478bd9Sstevel@tonic-gate 		 * which covers the possibility of dst members not in src.
14327c478bd9Sstevel@tonic-gate 		 * This optimization can be defeated for unions, but is so
14337c478bd9Sstevel@tonic-gate 		 * pathological as to render it irrelevant for our purposes.
14347c478bd9Sstevel@tonic-gate 		 */
14357c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
14367c478bd9Sstevel@tonic-gate 			if (ctf_type_size(src_fp, src_type) !=
14377c478bd9Sstevel@tonic-gate 			    ctf_type_size(dst_fp, dst_type))
14387c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 			if (ctf_member_iter(src_fp, src_type, membcmp, &dst))
14417c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 			break;
14447c478bd9Sstevel@tonic-gate 		}
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 		/*
14477c478bd9Sstevel@tonic-gate 		 * Unlike the other cases, copying structs and unions is done
14487c478bd9Sstevel@tonic-gate 		 * manually so as to avoid repeated lookups in ctf_add_member
14497c478bd9Sstevel@tonic-gate 		 * and to ensure the exact same member offsets as in src_type.
14507c478bd9Sstevel@tonic-gate 		 */
14517c478bd9Sstevel@tonic-gate 		dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);
14527c478bd9Sstevel@tonic-gate 		if (dst_type == CTF_ERR)
14537c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 		dst.ctb_type = dst_type;
14567c478bd9Sstevel@tonic-gate 		dst.ctb_dtd = dtd;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)
14597c478bd9Sstevel@tonic-gate 			errs++; /* increment errs and fail at bottom of case */
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 		if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) {
14627c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
14637c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
14647c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
14657c478bd9Sstevel@tonic-gate 		} else
14667c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_size = (ushort_t)size;
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen);
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 		/*
14717c478bd9Sstevel@tonic-gate 		 * Make a final pass through the members changing each dmd_type
14727c478bd9Sstevel@tonic-gate 		 * (a src_fp type) to an equivalent type in dst_fp.  We pass
14737c478bd9Sstevel@tonic-gate 		 * through all members, leaving any that fail set to CTF_ERR.
14747c478bd9Sstevel@tonic-gate 		 */
14757c478bd9Sstevel@tonic-gate 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
14767c478bd9Sstevel@tonic-gate 		    dmd != NULL; dmd = ctf_list_next(dmd)) {
14777c478bd9Sstevel@tonic-gate 			if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp,
14787c478bd9Sstevel@tonic-gate 			    dmd->dmd_type)) == CTF_ERR)
14797c478bd9Sstevel@tonic-gate 				errs++;
14807c478bd9Sstevel@tonic-gate 		}
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 		if (errs)
14837c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
1484*0a47c91cSRobert Mustacchi 
1485*0a47c91cSRobert Mustacchi 		/*
1486*0a47c91cSRobert Mustacchi 		 * Now that we know that we can't fail, we go through and bump
1487*0a47c91cSRobert Mustacchi 		 * all the reference counts on the member types.
1488*0a47c91cSRobert Mustacchi 		 */
1489*0a47c91cSRobert Mustacchi 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1490*0a47c91cSRobert Mustacchi 		    dmd != NULL; dmd = ctf_list_next(dmd))
1491*0a47c91cSRobert Mustacchi 			ctf_ref_inc(dst_fp, dmd->dmd_type);
14927c478bd9Sstevel@tonic-gate 		break;
14937c478bd9Sstevel@tonic-gate 	}
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	case CTF_K_ENUM:
14967c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
14977c478bd9Sstevel@tonic-gate 			if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||
14987c478bd9Sstevel@tonic-gate 			    ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))
14997c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
15007c478bd9Sstevel@tonic-gate 		} else {
15017c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_enum(dst_fp, flag, name);
15027c478bd9Sstevel@tonic-gate 			if ((dst.ctb_type = dst_type) == CTF_ERR ||
15037c478bd9Sstevel@tonic-gate 			    ctf_enum_iter(src_fp, src_type, enumadd, &dst))
15047c478bd9Sstevel@tonic-gate 				return (CTF_ERR); /* errno is set for us */
15057c478bd9Sstevel@tonic-gate 		}
15067c478bd9Sstevel@tonic-gate 		break;
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	case CTF_K_FORWARD:
15097c478bd9Sstevel@tonic-gate 		if (dst_type == CTF_ERR) {
15107c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_forward(dst_fp,
15117c478bd9Sstevel@tonic-gate 			    flag, name, CTF_K_STRUCT); /* assume STRUCT */
15127c478bd9Sstevel@tonic-gate 		}
15137c478bd9Sstevel@tonic-gate 		break;
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	case CTF_K_TYPEDEF:
15167c478bd9Sstevel@tonic-gate 		src_type = ctf_type_reference(src_fp, src_type);
15177c478bd9Sstevel@tonic-gate 		src_type = ctf_add_type(dst_fp, src_fp, src_type);
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 		if (src_type == CTF_ERR)
15207c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 		/*
15237c478bd9Sstevel@tonic-gate 		 * If dst_type is not CTF_ERR at this point, we should check if
15247c478bd9Sstevel@tonic-gate 		 * ctf_type_reference(dst_fp, dst_type) != src_type and if so
15257c478bd9Sstevel@tonic-gate 		 * fail with ECTF_CONFLICT.  However, this causes problems with
15267c478bd9Sstevel@tonic-gate 		 * <sys/types.h> typedefs that vary based on things like if
15277c478bd9Sstevel@tonic-gate 		 * _ILP32x then pid_t is int otherwise long.  We therefore omit
15287c478bd9Sstevel@tonic-gate 		 * this check and assume that if the identically named typedef
15297c478bd9Sstevel@tonic-gate 		 * already exists in dst_fp, it is correct or equivalent.
15307c478bd9Sstevel@tonic-gate 		 */
15317c478bd9Sstevel@tonic-gate 		if (dst_type == CTF_ERR) {
15327c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_typedef(dst_fp, flag,
15337c478bd9Sstevel@tonic-gate 			    name, src_type);
15347c478bd9Sstevel@tonic-gate 		}
15357c478bd9Sstevel@tonic-gate 		break;
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	default:
15387c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ECTF_CORRUPT));
15397c478bd9Sstevel@tonic-gate 	}
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	return (dst_type);
15427c478bd9Sstevel@tonic-gate }
1543