xref: /titanic_50/usr/src/common/ctf/ctf_create.c (revision f3e7f55e73a39377d55a030f124cc86b3b66a9cc)
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  */
270a47c91cSRobert Mustacchi /*
28*f3e7f55eSRobert Mustacchi  * Copyright (c) 2015, Joyent, Inc.
290a47c91cSRobert 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>
350a47c91cSRobert 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 
89*f3e7f55eSRobert Mustacchi ctf_file_t *
ctf_fdcreate(int fd,int * errp)90*f3e7f55eSRobert Mustacchi ctf_fdcreate(int fd, int *errp)
91*f3e7f55eSRobert Mustacchi {
92*f3e7f55eSRobert Mustacchi 	ctf_file_t *fp;
93*f3e7f55eSRobert Mustacchi 	static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } };
94*f3e7f55eSRobert Mustacchi 
95*f3e7f55eSRobert Mustacchi 	const ulong_t hashlen = 128;
96*f3e7f55eSRobert Mustacchi 	ctf_dtdef_t **hash;
97*f3e7f55eSRobert Mustacchi 	ctf_sect_t cts;
98*f3e7f55eSRobert Mustacchi 
99*f3e7f55eSRobert Mustacchi 	if (fd == -1)
100*f3e7f55eSRobert Mustacchi 		return (ctf_create(errp));
101*f3e7f55eSRobert Mustacchi 
102*f3e7f55eSRobert Mustacchi 	hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *));
103*f3e7f55eSRobert Mustacchi 
104*f3e7f55eSRobert Mustacchi 	if (hash == NULL)
105*f3e7f55eSRobert Mustacchi 		return (ctf_set_open_errno(errp, EAGAIN));
106*f3e7f55eSRobert Mustacchi 
107*f3e7f55eSRobert Mustacchi 	cts.cts_name = _CTF_SECTION;
108*f3e7f55eSRobert Mustacchi 	cts.cts_type = SHT_PROGBITS;
109*f3e7f55eSRobert Mustacchi 	cts.cts_flags = 0;
110*f3e7f55eSRobert Mustacchi 	cts.cts_data = &hdr;
111*f3e7f55eSRobert Mustacchi 	cts.cts_size = sizeof (hdr);
112*f3e7f55eSRobert Mustacchi 	cts.cts_entsize = 1;
113*f3e7f55eSRobert Mustacchi 	cts.cts_offset = 0;
114*f3e7f55eSRobert Mustacchi 
115*f3e7f55eSRobert Mustacchi 	if ((fp = ctf_fdcreate_int(fd, errp, &cts)) == NULL) {
116*f3e7f55eSRobert Mustacchi 		ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *));
117*f3e7f55eSRobert Mustacchi 		return (NULL);
118*f3e7f55eSRobert Mustacchi 	}
119*f3e7f55eSRobert Mustacchi 
120*f3e7f55eSRobert Mustacchi 	fp->ctf_flags |= LCTF_RDWR;
121*f3e7f55eSRobert Mustacchi 	fp->ctf_dthashlen = hashlen;
122*f3e7f55eSRobert Mustacchi 	bzero(hash, hashlen * sizeof (ctf_dtdef_t *));
123*f3e7f55eSRobert Mustacchi 	fp->ctf_dthash = hash;
124*f3e7f55eSRobert Mustacchi 	fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE);
125*f3e7f55eSRobert Mustacchi 	fp->ctf_dtnextid = 1;
126*f3e7f55eSRobert Mustacchi 	fp->ctf_dtoldid = 0;
127*f3e7f55eSRobert Mustacchi 
128*f3e7f55eSRobert Mustacchi 	return (fp);
129*f3e7f55eSRobert Mustacchi }
130*f3e7f55eSRobert Mustacchi 
1317c478bd9Sstevel@tonic-gate static uchar_t *
ctf_copy_smembers(ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)1327c478bd9Sstevel@tonic-gate ctf_copy_smembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1357c478bd9Sstevel@tonic-gate 	ctf_member_t ctm;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
1387c478bd9Sstevel@tonic-gate 		if (dmd->dmd_name) {
1397c478bd9Sstevel@tonic-gate 			ctm.ctm_name = soff;
1407c478bd9Sstevel@tonic-gate 			soff += strlen(dmd->dmd_name) + 1;
1417c478bd9Sstevel@tonic-gate 		} else
1427c478bd9Sstevel@tonic-gate 			ctm.ctm_name = 0;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		ctm.ctm_type = (ushort_t)dmd->dmd_type;
1457c478bd9Sstevel@tonic-gate 		ctm.ctm_offset = (ushort_t)dmd->dmd_offset;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		bcopy(&ctm, t, sizeof (ctm));
1487c478bd9Sstevel@tonic-gate 		t += sizeof (ctm);
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_lmembers(ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)1557c478bd9Sstevel@tonic-gate ctf_copy_lmembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1587c478bd9Sstevel@tonic-gate 	ctf_lmember_t ctlm;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
1617c478bd9Sstevel@tonic-gate 		if (dmd->dmd_name) {
1627c478bd9Sstevel@tonic-gate 			ctlm.ctlm_name = soff;
1637c478bd9Sstevel@tonic-gate 			soff += strlen(dmd->dmd_name) + 1;
1647c478bd9Sstevel@tonic-gate 		} else
1657c478bd9Sstevel@tonic-gate 			ctlm.ctlm_name = 0;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		ctlm.ctlm_type = (ushort_t)dmd->dmd_type;
1687c478bd9Sstevel@tonic-gate 		ctlm.ctlm_pad = 0;
1697c478bd9Sstevel@tonic-gate 		ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset);
1707c478bd9Sstevel@tonic-gate 		ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		bcopy(&ctlm, t, sizeof (ctlm));
1737c478bd9Sstevel@tonic-gate 		t += sizeof (ctlm);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	return (t);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate static uchar_t *
ctf_copy_emembers(ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)1807c478bd9Sstevel@tonic-gate ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1837c478bd9Sstevel@tonic-gate 	ctf_enum_t cte;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
1867c478bd9Sstevel@tonic-gate 		cte.cte_name = soff;
1877c478bd9Sstevel@tonic-gate 		cte.cte_value = dmd->dmd_value;
1887c478bd9Sstevel@tonic-gate 		soff += strlen(dmd->dmd_name) + 1;
1897c478bd9Sstevel@tonic-gate 		bcopy(&cte, t, sizeof (cte));
1907c478bd9Sstevel@tonic-gate 		t += sizeof (cte);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	return (t);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static uchar_t *
ctf_copy_membnames(ctf_dtdef_t * dtd,uchar_t * s)1977c478bd9Sstevel@tonic-gate ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
2007c478bd9Sstevel@tonic-gate 	size_t len;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
2037c478bd9Sstevel@tonic-gate 		if (dmd->dmd_name == NULL)
2047c478bd9Sstevel@tonic-gate 			continue; /* skip anonymous members */
2057c478bd9Sstevel@tonic-gate 		len = strlen(dmd->dmd_name) + 1;
2067c478bd9Sstevel@tonic-gate 		bcopy(dmd->dmd_name, s, len);
2077c478bd9Sstevel@tonic-gate 		s += len;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	return (s);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2140a47c91cSRobert Mustacchi  * Only types of dyanmic CTF containers contain reference counts. These
2150a47c91cSRobert Mustacchi  * containers are marked RD/WR. Because of that we basically make this a no-op
2160a47c91cSRobert Mustacchi  * for compatability with non-dynamic CTF sections. This is also a no-op for
2170a47c91cSRobert Mustacchi  * types which are not dynamic types. It is the responsibility of the caller to
2180a47c91cSRobert Mustacchi  * make sure it is a valid type. We help that caller out on debug builds.
2190a47c91cSRobert Mustacchi  *
2200a47c91cSRobert Mustacchi  * Note that the reference counts are not maintained for types that are not
2210a47c91cSRobert Mustacchi  * within this container. In other words if we have a type in a parent, that
2220a47c91cSRobert Mustacchi  * will not have its reference count increased. On the flip side, the parent
2230a47c91cSRobert Mustacchi  * will not be allowed to remove dynamic types if it has children.
2240a47c91cSRobert Mustacchi  */
2250a47c91cSRobert Mustacchi static void
ctf_ref_inc(ctf_file_t * fp,ctf_id_t tid)2260a47c91cSRobert Mustacchi ctf_ref_inc(ctf_file_t *fp, ctf_id_t tid)
2270a47c91cSRobert Mustacchi {
2280a47c91cSRobert Mustacchi 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
2290a47c91cSRobert Mustacchi 
2300a47c91cSRobert Mustacchi 	if (dtd == NULL)
2310a47c91cSRobert Mustacchi 		return;
2320a47c91cSRobert Mustacchi 
2330a47c91cSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
2340a47c91cSRobert Mustacchi 		return;
2350a47c91cSRobert Mustacchi 
2360a47c91cSRobert Mustacchi 	dtd->dtd_ref++;
2370a47c91cSRobert Mustacchi }
2380a47c91cSRobert Mustacchi 
2390a47c91cSRobert Mustacchi /*
2400a47c91cSRobert Mustacchi  * Just as with ctf_ref_inc, this is a no-op on non-writeable containers and the
2410a47c91cSRobert Mustacchi  * caller should ensure that this is already a valid type.
2420a47c91cSRobert Mustacchi  */
2430a47c91cSRobert Mustacchi static void
ctf_ref_dec(ctf_file_t * fp,ctf_id_t tid)2440a47c91cSRobert Mustacchi ctf_ref_dec(ctf_file_t *fp, ctf_id_t tid)
2450a47c91cSRobert Mustacchi {
2460a47c91cSRobert Mustacchi 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
2470a47c91cSRobert Mustacchi 
2480a47c91cSRobert Mustacchi 	if (dtd == NULL)
2490a47c91cSRobert Mustacchi 		return;
2500a47c91cSRobert Mustacchi 
2510a47c91cSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
2520a47c91cSRobert Mustacchi 		return;
2530a47c91cSRobert Mustacchi 
2540a47c91cSRobert Mustacchi 	ASSERT(dtd->dtd_ref >= 1);
2550a47c91cSRobert Mustacchi 	dtd->dtd_ref--;
2560a47c91cSRobert Mustacchi }
2570a47c91cSRobert Mustacchi 
2580a47c91cSRobert Mustacchi /*
2597c478bd9Sstevel@tonic-gate  * If the specified CTF container is writable and has been modified, reload
2607c478bd9Sstevel@tonic-gate  * this container with the updated type definitions.  In order to make this
2617c478bd9Sstevel@tonic-gate  * code and the rest of libctf as simple as possible, we perform updates by
2627c478bd9Sstevel@tonic-gate  * taking the dynamic type definitions and creating an in-memory CTF file
2637c478bd9Sstevel@tonic-gate  * containing the definitions, and then call ctf_bufopen() on it.  This not
2647c478bd9Sstevel@tonic-gate  * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest
2657c478bd9Sstevel@tonic-gate  * of the library code with different lookup paths for static and dynamic
2667c478bd9Sstevel@tonic-gate  * type definitions.  We are therefore optimizing greatly for lookup over
2677c478bd9Sstevel@tonic-gate  * update, which we assume will be an uncommon operation.  We perform one
2687c478bd9Sstevel@tonic-gate  * extra trick here for the benefit of callers and to keep our code simple:
2697c478bd9Sstevel@tonic-gate  * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp
2707c478bd9Sstevel@tonic-gate  * constant for the caller, so after ctf_bufopen() returns, we use bcopy to
2717c478bd9Sstevel@tonic-gate  * swap the interior of the old and new ctf_file_t's, and then free the old.
2720a47c91cSRobert Mustacchi  *
2730a47c91cSRobert Mustacchi  * Note that the lists of dynamic types stays around and the resulting container
2740a47c91cSRobert Mustacchi  * is still writeable. Furthermore, the reference counts that are on the dtd's
2750a47c91cSRobert Mustacchi  * are still valid.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate int
ctf_update(ctf_file_t * fp)2787c478bd9Sstevel@tonic-gate ctf_update(ctf_file_t *fp)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	ctf_file_t ofp, *nfp;
281*f3e7f55eSRobert Mustacchi 	ctf_header_t hdr, *bhdr;
2827c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
283*f3e7f55eSRobert Mustacchi 	ctf_dsdef_t *dsd;
284*f3e7f55eSRobert Mustacchi 	ctf_dldef_t *dld;
285*f3e7f55eSRobert Mustacchi 	ctf_sect_t cts, *symp, *strp;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	uchar_t *s, *s0, *t;
288*f3e7f55eSRobert Mustacchi 	ctf_lblent_t *label;
289*f3e7f55eSRobert Mustacchi 	uint16_t *obj, *func;
290*f3e7f55eSRobert Mustacchi 	size_t size, objsize, funcsize, labelsize, plen;
2917c478bd9Sstevel@tonic-gate 	void *buf;
2927c478bd9Sstevel@tonic-gate 	int err;
293*f3e7f55eSRobert Mustacchi 	ulong_t i;
294*f3e7f55eSRobert Mustacchi 	const char *plabel;
295*f3e7f55eSRobert Mustacchi 	const char *sname;
296*f3e7f55eSRobert Mustacchi 
297*f3e7f55eSRobert Mustacchi 	uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
298*f3e7f55eSRobert Mustacchi 	uintptr_t strbase = (uintptr_t)fp->ctf_strtab.cts_data;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
3017c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_DIRTY))
3047c478bd9Sstevel@tonic-gate 		return (0); /* no update required */
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * Fill in an initial CTF header.  We will leave the label, object,
3087c478bd9Sstevel@tonic-gate 	 * and function sections empty and only output a header, type section,
3097c478bd9Sstevel@tonic-gate 	 * and string table.  The type section begins at a 4-byte aligned
3107c478bd9Sstevel@tonic-gate 	 * boundary past the CTF header itself (at relative offset zero).
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	bzero(&hdr, sizeof (hdr));
3137c478bd9Sstevel@tonic-gate 	hdr.cth_magic = CTF_MAGIC;
3147c478bd9Sstevel@tonic-gate 	hdr.cth_version = CTF_VERSION;
3157c478bd9Sstevel@tonic-gate 
316*f3e7f55eSRobert Mustacchi 	if (fp->ctf_flags & LCTF_CHILD) {
317*f3e7f55eSRobert Mustacchi 		if (fp->ctf_parname == NULL) {
318*f3e7f55eSRobert Mustacchi 			plen = 0;
3197c478bd9Sstevel@tonic-gate 			hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */
320*f3e7f55eSRobert Mustacchi 			plabel = NULL;
321*f3e7f55eSRobert Mustacchi 		} else {
322*f3e7f55eSRobert Mustacchi 			plen = strlen(fp->ctf_parname) + 1;
323*f3e7f55eSRobert Mustacchi 			plabel = ctf_label_topmost(fp->ctf_parent);
324*f3e7f55eSRobert Mustacchi 		}
325*f3e7f55eSRobert Mustacchi 	} else {
326*f3e7f55eSRobert Mustacchi 		plabel = NULL;
327*f3e7f55eSRobert Mustacchi 		plen = 0;
328*f3e7f55eSRobert Mustacchi 	}
329*f3e7f55eSRobert Mustacchi 
330*f3e7f55eSRobert Mustacchi 	/*
331*f3e7f55eSRobert Mustacchi 	 * Iterate over the labels that we have.
332*f3e7f55eSRobert Mustacchi 	 */
333*f3e7f55eSRobert Mustacchi 	for (labelsize = 0, dld = ctf_list_next(&fp->ctf_dldefs);
334*f3e7f55eSRobert Mustacchi 	    dld != NULL; dld = ctf_list_next(dld))
335*f3e7f55eSRobert Mustacchi 		labelsize += sizeof (ctf_lblent_t);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * Iterate through the dynamic type definition list and compute the
3397c478bd9Sstevel@tonic-gate 	 * size of the CTF type section we will need to generate.
3407c478bd9Sstevel@tonic-gate 	 */
3417c478bd9Sstevel@tonic-gate 	for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs);
3427c478bd9Sstevel@tonic-gate 	    dtd != NULL; dtd = ctf_list_next(dtd)) {
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
3457c478bd9Sstevel@tonic-gate 		uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
3487c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_stype_t);
3497c478bd9Sstevel@tonic-gate 		else
3507c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_type_t);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		switch (kind) {
3537c478bd9Sstevel@tonic-gate 		case CTF_K_INTEGER:
3547c478bd9Sstevel@tonic-gate 		case CTF_K_FLOAT:
3557c478bd9Sstevel@tonic-gate 			size += sizeof (uint_t);
3567c478bd9Sstevel@tonic-gate 			break;
3577c478bd9Sstevel@tonic-gate 		case CTF_K_ARRAY:
3587c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_array_t);
3597c478bd9Sstevel@tonic-gate 			break;
3607c478bd9Sstevel@tonic-gate 		case CTF_K_FUNCTION:
3617c478bd9Sstevel@tonic-gate 			size += sizeof (ushort_t) * (vlen + (vlen & 1));
3627c478bd9Sstevel@tonic-gate 			break;
3637c478bd9Sstevel@tonic-gate 		case CTF_K_STRUCT:
3647c478bd9Sstevel@tonic-gate 		case CTF_K_UNION:
3657c478bd9Sstevel@tonic-gate 			if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
3667c478bd9Sstevel@tonic-gate 				size += sizeof (ctf_member_t) * vlen;
3677c478bd9Sstevel@tonic-gate 			else
3687c478bd9Sstevel@tonic-gate 				size += sizeof (ctf_lmember_t) * vlen;
3697c478bd9Sstevel@tonic-gate 			break;
3707c478bd9Sstevel@tonic-gate 		case CTF_K_ENUM:
3717c478bd9Sstevel@tonic-gate 			size += sizeof (ctf_enum_t) * vlen;
3727c478bd9Sstevel@tonic-gate 			break;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
377*f3e7f55eSRobert Mustacchi 	 * An entry for each object must exist in the data section. However, if
378*f3e7f55eSRobert Mustacchi 	 * the symbol is SHN_UNDEF, then it is skipped. For objects, the storage
379*f3e7f55eSRobert Mustacchi 	 * is just the size of the 2-byte id. For functions it's always 2 bytes,
380*f3e7f55eSRobert Mustacchi 	 * plus 2 bytes per argument and the return type.
381*f3e7f55eSRobert Mustacchi 	 */
382*f3e7f55eSRobert Mustacchi 	dsd = ctf_list_next(&fp->ctf_dsdefs);
383*f3e7f55eSRobert Mustacchi 	for (objsize = 0, funcsize = 0, i = 0; i < fp->ctf_nsyms; i++) {
384*f3e7f55eSRobert Mustacchi 		int type;
385*f3e7f55eSRobert Mustacchi 
386*f3e7f55eSRobert Mustacchi 		if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
387*f3e7f55eSRobert Mustacchi 			const Elf32_Sym *symp = (Elf32_Sym *)symbase + i;
388*f3e7f55eSRobert Mustacchi 
389*f3e7f55eSRobert Mustacchi 			type = ELF32_ST_TYPE(symp->st_info);
390*f3e7f55eSRobert Mustacchi 			if (ctf_sym_valid(strbase, type, symp->st_shndx,
391*f3e7f55eSRobert Mustacchi 			    symp->st_value, symp->st_name) == B_FALSE)
392*f3e7f55eSRobert Mustacchi 				continue;
393*f3e7f55eSRobert Mustacchi 		} else {
394*f3e7f55eSRobert Mustacchi 			const Elf64_Sym *symp = (Elf64_Sym *)symbase + i;
395*f3e7f55eSRobert Mustacchi 
396*f3e7f55eSRobert Mustacchi 			type = ELF64_ST_TYPE(symp->st_info);
397*f3e7f55eSRobert Mustacchi 			if (ctf_sym_valid(strbase, type, symp->st_shndx,
398*f3e7f55eSRobert Mustacchi 			    symp->st_value, symp->st_name) == B_FALSE)
399*f3e7f55eSRobert Mustacchi 				continue;
400*f3e7f55eSRobert Mustacchi 		}
401*f3e7f55eSRobert Mustacchi 
402*f3e7f55eSRobert Mustacchi 		while (dsd != NULL && i > dsd->dsd_symidx)
403*f3e7f55eSRobert Mustacchi 			dsd = ctf_list_next(dsd);
404*f3e7f55eSRobert Mustacchi 		if (type == STT_OBJECT) {
405*f3e7f55eSRobert Mustacchi 			objsize += sizeof (uint16_t);
406*f3e7f55eSRobert Mustacchi 		} else {
407*f3e7f55eSRobert Mustacchi 			/* Every function has a uint16_t info no matter what */
408*f3e7f55eSRobert Mustacchi 			if (dsd == NULL || i < dsd->dsd_symidx) {
409*f3e7f55eSRobert Mustacchi 				funcsize += sizeof (uint16_t);
410*f3e7f55eSRobert Mustacchi 			} else {
411*f3e7f55eSRobert Mustacchi 				funcsize += sizeof (uint16_t) *
412*f3e7f55eSRobert Mustacchi 				    (dsd->dsd_nargs + 2);
413*f3e7f55eSRobert Mustacchi 			}
414*f3e7f55eSRobert Mustacchi 		}
415*f3e7f55eSRobert Mustacchi 	}
416*f3e7f55eSRobert Mustacchi 
417*f3e7f55eSRobert Mustacchi 	/*
418*f3e7f55eSRobert Mustacchi 	 * The objtoff and funcoffset must be 2-byte aligned. We're guaranteed
419*f3e7f55eSRobert Mustacchi 	 * that this is always true for the objtoff because labels are always 8
420*f3e7f55eSRobert Mustacchi 	 * bytes large. Similarly, because objects are always two bytes of data,
421*f3e7f55eSRobert Mustacchi 	 * this will always be true for funcoff.
422*f3e7f55eSRobert Mustacchi 	 */
423*f3e7f55eSRobert Mustacchi 	hdr.cth_objtoff = hdr.cth_lbloff + labelsize;
424*f3e7f55eSRobert Mustacchi 	hdr.cth_funcoff = hdr.cth_objtoff + objsize;
425*f3e7f55eSRobert Mustacchi 
426*f3e7f55eSRobert Mustacchi 	/*
427*f3e7f55eSRobert Mustacchi 	 * The type offset must be 4 byte aligned.
428*f3e7f55eSRobert Mustacchi 	 */
429*f3e7f55eSRobert Mustacchi 	hdr.cth_typeoff = hdr.cth_funcoff + funcsize;
430*f3e7f55eSRobert Mustacchi 	if (hdr.cth_typeoff & 3)
431*f3e7f55eSRobert Mustacchi 		hdr.cth_typeoff += 4 - (hdr.cth_typeoff & 3);
432*f3e7f55eSRobert Mustacchi 	ASSERT((hdr.cth_typeoff & 3) == 0);
433*f3e7f55eSRobert Mustacchi 
434*f3e7f55eSRobert Mustacchi 	/*
4357c478bd9Sstevel@tonic-gate 	 * Fill in the string table offset and size, compute the size of the
4367c478bd9Sstevel@tonic-gate 	 * entire CTF buffer we need, and then allocate a new buffer and
4377c478bd9Sstevel@tonic-gate 	 * bcopy the finished header to the start of the buffer.
4387c478bd9Sstevel@tonic-gate 	 */
4397c478bd9Sstevel@tonic-gate 	hdr.cth_stroff = hdr.cth_typeoff + size;
440*f3e7f55eSRobert Mustacchi 	hdr.cth_strlen = fp->ctf_dtstrlen + plen;
4417c478bd9Sstevel@tonic-gate 	size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
442*f3e7f55eSRobert Mustacchi 	ctf_dprintf("lbloff: %u\nobjtoff: %u\nfuncoff: %u\n"
443*f3e7f55eSRobert Mustacchi 	    "typeoff: %u\nstroff: %u\nstrlen: %u\n",
444*f3e7f55eSRobert Mustacchi 	    hdr.cth_lbloff, hdr.cth_objtoff, hdr.cth_funcoff,
445*f3e7f55eSRobert Mustacchi 	    hdr.cth_typeoff, hdr.cth_stroff, hdr.cth_strlen);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if ((buf = ctf_data_alloc(size)) == MAP_FAILED)
4487c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	bcopy(&hdr, buf, sizeof (ctf_header_t));
451*f3e7f55eSRobert Mustacchi 	bhdr = buf;
452*f3e7f55eSRobert Mustacchi 	label = (ctf_lblent_t *)((uintptr_t)buf + sizeof (ctf_header_t));
453*f3e7f55eSRobert Mustacchi 	t = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_typeoff;
4547c478bd9Sstevel@tonic-gate 	s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff;
455*f3e7f55eSRobert Mustacchi 	obj = (uint16_t *)((uintptr_t)buf + sizeof (ctf_header_t) +
456*f3e7f55eSRobert Mustacchi 	    hdr.cth_objtoff);
457*f3e7f55eSRobert Mustacchi 	func = (uint16_t *)((uintptr_t)buf + sizeof (ctf_header_t) +
458*f3e7f55eSRobert Mustacchi 	    hdr.cth_funcoff);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE));
4617c478bd9Sstevel@tonic-gate 	s += sizeof (_CTF_STRTAB_TEMPLATE);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/*
464*f3e7f55eSRobert Mustacchi 	 * We have an actual parent name and we're a child container, therefore
465*f3e7f55eSRobert Mustacchi 	 * we should make sure to note our parent's name here.
466*f3e7f55eSRobert Mustacchi 	 */
467*f3e7f55eSRobert Mustacchi 	if (plen != 0) {
468*f3e7f55eSRobert Mustacchi 		VERIFY(s + plen - s0 <= hdr.cth_strlen);
469*f3e7f55eSRobert Mustacchi 		bcopy(fp->ctf_parname, s, plen);
470*f3e7f55eSRobert Mustacchi 		bhdr->cth_parname = s - s0;
471*f3e7f55eSRobert Mustacchi 		s += plen;
472*f3e7f55eSRobert Mustacchi 	}
473*f3e7f55eSRobert Mustacchi 
474*f3e7f55eSRobert Mustacchi 	/*
475*f3e7f55eSRobert Mustacchi 	 * First pass over the labels and copy them out.
476*f3e7f55eSRobert Mustacchi 	 */
477*f3e7f55eSRobert Mustacchi 	for (dld = ctf_list_next(&fp->ctf_dldefs); dld != NULL;
478*f3e7f55eSRobert Mustacchi 	    dld = ctf_list_next(dld), label++) {
479*f3e7f55eSRobert Mustacchi 		size_t len = strlen(dld->dld_name) + 1;
480*f3e7f55eSRobert Mustacchi 
481*f3e7f55eSRobert Mustacchi 		VERIFY(s + len - s0 <= hdr.cth_strlen);
482*f3e7f55eSRobert Mustacchi 		bcopy(dld->dld_name, s, len);
483*f3e7f55eSRobert Mustacchi 		label->ctl_typeidx = dld->dld_type;
484*f3e7f55eSRobert Mustacchi 		label->ctl_label = s - s0;
485*f3e7f55eSRobert Mustacchi 		s += len;
486*f3e7f55eSRobert Mustacchi 
487*f3e7f55eSRobert Mustacchi 		if (plabel != NULL && strcmp(plabel, dld->dld_name) == 0)
488*f3e7f55eSRobert Mustacchi 			bhdr->cth_parlabel = label->ctl_label;
489*f3e7f55eSRobert Mustacchi 	}
490*f3e7f55eSRobert Mustacchi 
491*f3e7f55eSRobert Mustacchi 	/*
4927c478bd9Sstevel@tonic-gate 	 * We now take a final lap through the dynamic type definition list and
4937c478bd9Sstevel@tonic-gate 	 * copy the appropriate type records and strings to the output buffer.
4947c478bd9Sstevel@tonic-gate 	 */
4957c478bd9Sstevel@tonic-gate 	for (dtd = ctf_list_next(&fp->ctf_dtdefs);
4967c478bd9Sstevel@tonic-gate 	    dtd != NULL; dtd = ctf_list_next(dtd)) {
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
4997c478bd9Sstevel@tonic-gate 		uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		ctf_array_t cta;
5027c478bd9Sstevel@tonic-gate 		uint_t encoding;
5037c478bd9Sstevel@tonic-gate 		size_t len;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		if (dtd->dtd_name != NULL) {
5067c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_name = (uint_t)(s - s0);
5077c478bd9Sstevel@tonic-gate 			len = strlen(dtd->dtd_name) + 1;
508*f3e7f55eSRobert Mustacchi 			VERIFY(s + len - s0 <= hdr.cth_strlen);
5097c478bd9Sstevel@tonic-gate 			bcopy(dtd->dtd_name, s, len);
5107c478bd9Sstevel@tonic-gate 			s += len;
5117c478bd9Sstevel@tonic-gate 		} else
5127c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_name = 0;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
5157c478bd9Sstevel@tonic-gate 			len = sizeof (ctf_stype_t);
5167c478bd9Sstevel@tonic-gate 		else
5177c478bd9Sstevel@tonic-gate 			len = sizeof (ctf_type_t);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		bcopy(&dtd->dtd_data, t, len);
5207c478bd9Sstevel@tonic-gate 		t += len;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 		switch (kind) {
5237c478bd9Sstevel@tonic-gate 		case CTF_K_INTEGER:
5247c478bd9Sstevel@tonic-gate 		case CTF_K_FLOAT:
5257c478bd9Sstevel@tonic-gate 			if (kind == CTF_K_INTEGER) {
5267c478bd9Sstevel@tonic-gate 				encoding = CTF_INT_DATA(
5277c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_format,
5287c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_offset,
5297c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_bits);
5307c478bd9Sstevel@tonic-gate 			} else {
5317c478bd9Sstevel@tonic-gate 				encoding = CTF_FP_DATA(
5327c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_format,
5337c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_offset,
5347c478bd9Sstevel@tonic-gate 				    dtd->dtd_u.dtu_enc.cte_bits);
5357c478bd9Sstevel@tonic-gate 			}
5367c478bd9Sstevel@tonic-gate 			bcopy(&encoding, t, sizeof (encoding));
5377c478bd9Sstevel@tonic-gate 			t += sizeof (encoding);
5387c478bd9Sstevel@tonic-gate 			break;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		case CTF_K_ARRAY:
5417c478bd9Sstevel@tonic-gate 			cta.cta_contents = (ushort_t)
5427c478bd9Sstevel@tonic-gate 			    dtd->dtd_u.dtu_arr.ctr_contents;
5437c478bd9Sstevel@tonic-gate 			cta.cta_index = (ushort_t)
5447c478bd9Sstevel@tonic-gate 			    dtd->dtd_u.dtu_arr.ctr_index;
5457c478bd9Sstevel@tonic-gate 			cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
5467c478bd9Sstevel@tonic-gate 			bcopy(&cta, t, sizeof (cta));
5477c478bd9Sstevel@tonic-gate 			t += sizeof (cta);
5487c478bd9Sstevel@tonic-gate 			break;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		case CTF_K_FUNCTION: {
5517c478bd9Sstevel@tonic-gate 			ushort_t *argv = (ushort_t *)(uintptr_t)t;
5527c478bd9Sstevel@tonic-gate 			uint_t argc;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 			for (argc = 0; argc < vlen; argc++)
5557c478bd9Sstevel@tonic-gate 				*argv++ = (ushort_t)dtd->dtd_u.dtu_argv[argc];
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 			if (vlen & 1)
5587c478bd9Sstevel@tonic-gate 				*argv++ = 0; /* pad to 4-byte boundary */
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 			t = (uchar_t *)argv;
5617c478bd9Sstevel@tonic-gate 			break;
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		case CTF_K_STRUCT:
5657c478bd9Sstevel@tonic-gate 		case CTF_K_UNION:
5667c478bd9Sstevel@tonic-gate 			if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
5677c478bd9Sstevel@tonic-gate 				t = ctf_copy_smembers(dtd, (uint_t)(s - s0), t);
5687c478bd9Sstevel@tonic-gate 			else
5697c478bd9Sstevel@tonic-gate 				t = ctf_copy_lmembers(dtd, (uint_t)(s - s0), t);
5707c478bd9Sstevel@tonic-gate 			s = ctf_copy_membnames(dtd, s);
5717c478bd9Sstevel@tonic-gate 			break;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		case CTF_K_ENUM:
5747c478bd9Sstevel@tonic-gate 			t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t);
5757c478bd9Sstevel@tonic-gate 			s = ctf_copy_membnames(dtd, s);
5767c478bd9Sstevel@tonic-gate 			break;
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/*
581*f3e7f55eSRobert Mustacchi 	 * Now we fill in our dynamic data and function sections. We use the
582*f3e7f55eSRobert Mustacchi 	 * same criteria as above, but also consult the dsd list.
583*f3e7f55eSRobert Mustacchi 	 */
584*f3e7f55eSRobert Mustacchi 	dsd = ctf_list_next(&fp->ctf_dsdefs);
585*f3e7f55eSRobert Mustacchi 	for (i = 0; i < fp->ctf_nsyms; i++) {
586*f3e7f55eSRobert Mustacchi 		int type;
587*f3e7f55eSRobert Mustacchi 		if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
588*f3e7f55eSRobert Mustacchi 			const Elf32_Sym *symp = (Elf32_Sym *)symbase + i;
589*f3e7f55eSRobert Mustacchi 			type = ELF32_ST_TYPE(symp->st_info);
590*f3e7f55eSRobert Mustacchi 
591*f3e7f55eSRobert Mustacchi 			if (ctf_sym_valid(strbase, type, symp->st_shndx,
592*f3e7f55eSRobert Mustacchi 			    symp->st_value, symp->st_name) == B_FALSE)
593*f3e7f55eSRobert Mustacchi 				continue;
594*f3e7f55eSRobert Mustacchi 		} else {
595*f3e7f55eSRobert Mustacchi 			const Elf64_Sym *symp = (Elf64_Sym *)symbase + i;
596*f3e7f55eSRobert Mustacchi 			type = ELF64_ST_TYPE(symp->st_info);
597*f3e7f55eSRobert Mustacchi 			if (ctf_sym_valid(strbase, type, symp->st_shndx,
598*f3e7f55eSRobert Mustacchi 			    symp->st_value, symp->st_name) == B_FALSE)
599*f3e7f55eSRobert Mustacchi 				continue;
600*f3e7f55eSRobert Mustacchi 		}
601*f3e7f55eSRobert Mustacchi 
602*f3e7f55eSRobert Mustacchi 		while (dsd != NULL && i > dsd->dsd_symidx) {
603*f3e7f55eSRobert Mustacchi 			dsd = ctf_list_next(dsd);
604*f3e7f55eSRobert Mustacchi 		}
605*f3e7f55eSRobert Mustacchi 		if (type == STT_OBJECT) {
606*f3e7f55eSRobert Mustacchi 			if (dsd == NULL || i < dsd->dsd_symidx) {
607*f3e7f55eSRobert Mustacchi 				*obj = 0;
608*f3e7f55eSRobert Mustacchi 			} else {
609*f3e7f55eSRobert Mustacchi 				*obj = dsd->dsd_tid;
610*f3e7f55eSRobert Mustacchi 			}
611*f3e7f55eSRobert Mustacchi 			obj++;
612*f3e7f55eSRobert Mustacchi 			VERIFY((uintptr_t)obj <= (uintptr_t)func);
613*f3e7f55eSRobert Mustacchi 		} else {
614*f3e7f55eSRobert Mustacchi 			if (dsd == NULL || i < dsd->dsd_symidx) {
615*f3e7f55eSRobert Mustacchi 				ushort_t data = CTF_TYPE_INFO(CTF_K_UNKNOWN,
616*f3e7f55eSRobert Mustacchi 				    0, 0);
617*f3e7f55eSRobert Mustacchi 				*func = data;
618*f3e7f55eSRobert Mustacchi 				func++;
619*f3e7f55eSRobert Mustacchi 			} else {
620*f3e7f55eSRobert Mustacchi 				int j;
621*f3e7f55eSRobert Mustacchi 				ushort_t data = CTF_TYPE_INFO(CTF_K_FUNCTION, 0,
622*f3e7f55eSRobert Mustacchi 				    dsd->dsd_nargs);
623*f3e7f55eSRobert Mustacchi 
624*f3e7f55eSRobert Mustacchi 				*func = data;
625*f3e7f55eSRobert Mustacchi 				func++;
626*f3e7f55eSRobert Mustacchi 				*func = dsd->dsd_tid;
627*f3e7f55eSRobert Mustacchi 				func++;
628*f3e7f55eSRobert Mustacchi 				for (j = 0; j < dsd->dsd_nargs; j++)
629*f3e7f55eSRobert Mustacchi 					func[j] = dsd->dsd_argc[j];
630*f3e7f55eSRobert Mustacchi 				func += dsd->dsd_nargs;
631*f3e7f55eSRobert Mustacchi 			}
632*f3e7f55eSRobert Mustacchi 		}
633*f3e7f55eSRobert Mustacchi 	}
634*f3e7f55eSRobert Mustacchi 
635*f3e7f55eSRobert Mustacchi 	/*
6367c478bd9Sstevel@tonic-gate 	 * Finally, we are ready to ctf_bufopen() the new container.  If this
6377c478bd9Sstevel@tonic-gate 	 * is successful, we then switch nfp and fp and free the old container.
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	ctf_data_protect(buf, size);
6407c478bd9Sstevel@tonic-gate 	cts.cts_name = _CTF_SECTION;
6417c478bd9Sstevel@tonic-gate 	cts.cts_type = SHT_PROGBITS;
6427c478bd9Sstevel@tonic-gate 	cts.cts_flags = 0;
6437c478bd9Sstevel@tonic-gate 	cts.cts_data = buf;
6447c478bd9Sstevel@tonic-gate 	cts.cts_size = size;
6457c478bd9Sstevel@tonic-gate 	cts.cts_entsize = 1;
6467c478bd9Sstevel@tonic-gate 	cts.cts_offset = 0;
6477c478bd9Sstevel@tonic-gate 
648*f3e7f55eSRobert Mustacchi 	if (fp->ctf_nsyms == 0) {
649*f3e7f55eSRobert Mustacchi 		symp = NULL;
650*f3e7f55eSRobert Mustacchi 		strp = NULL;
651*f3e7f55eSRobert Mustacchi 	} else {
652*f3e7f55eSRobert Mustacchi 		symp = &fp->ctf_symtab;
653*f3e7f55eSRobert Mustacchi 		strp = &fp->ctf_strtab;
654*f3e7f55eSRobert Mustacchi 	}
655*f3e7f55eSRobert Mustacchi 
656*f3e7f55eSRobert Mustacchi 	if ((nfp = ctf_bufopen(&cts, symp, strp, &err)) == NULL) {
6577c478bd9Sstevel@tonic-gate 		ctf_data_free(buf, size);
6587c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, err));
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	(void) ctf_setmodel(nfp, ctf_getmodel(fp));
6627c478bd9Sstevel@tonic-gate 	(void) ctf_import(nfp, fp->ctf_parent);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	nfp->ctf_refcnt = fp->ctf_refcnt;
6657c478bd9Sstevel@tonic-gate 	nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
666e4586ebfSmws 	nfp->ctf_dthash = fp->ctf_dthash;
667e4586ebfSmws 	nfp->ctf_dthashlen = fp->ctf_dthashlen;
6687c478bd9Sstevel@tonic-gate 	nfp->ctf_dtdefs = fp->ctf_dtdefs;
669*f3e7f55eSRobert Mustacchi 	nfp->ctf_dsdefs = fp->ctf_dsdefs;
670*f3e7f55eSRobert Mustacchi 	nfp->ctf_dldefs = fp->ctf_dldefs;
6717c478bd9Sstevel@tonic-gate 	nfp->ctf_dtstrlen = fp->ctf_dtstrlen;
6727c478bd9Sstevel@tonic-gate 	nfp->ctf_dtnextid = fp->ctf_dtnextid;
6737c478bd9Sstevel@tonic-gate 	nfp->ctf_dtoldid = fp->ctf_dtnextid - 1;
6747c478bd9Sstevel@tonic-gate 	nfp->ctf_specific = fp->ctf_specific;
6757c478bd9Sstevel@tonic-gate 
676e4586ebfSmws 	fp->ctf_dthash = NULL;
677e4586ebfSmws 	fp->ctf_dthashlen = 0;
6787c478bd9Sstevel@tonic-gate 	bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t));
679*f3e7f55eSRobert Mustacchi 	bzero(&fp->ctf_dsdefs, sizeof (ctf_list_t));
680*f3e7f55eSRobert Mustacchi 	bzero(&fp->ctf_dldefs, sizeof (ctf_list_t));
681*f3e7f55eSRobert Mustacchi 
682*f3e7f55eSRobert Mustacchi 	/*
683*f3e7f55eSRobert Mustacchi 	 * Because the various containers share the data sections, we don't want
684*f3e7f55eSRobert Mustacchi 	 * to have ctf_close free it all. However, the name of the section is in
685*f3e7f55eSRobert Mustacchi 	 * fact unique to the ctf_sect_t. Thus we save the names of the symbol
686*f3e7f55eSRobert Mustacchi 	 * and string sections around the bzero() and restore them afterwards,
687*f3e7f55eSRobert Mustacchi 	 * ensuring that we don't result in a memory leak.
688*f3e7f55eSRobert Mustacchi 	 */
689*f3e7f55eSRobert Mustacchi 	sname = fp->ctf_symtab.cts_name;
690*f3e7f55eSRobert Mustacchi 	bzero(&fp->ctf_symtab, sizeof (ctf_sect_t));
691*f3e7f55eSRobert Mustacchi 	fp->ctf_symtab.cts_name = sname;
692*f3e7f55eSRobert Mustacchi 
693*f3e7f55eSRobert Mustacchi 	sname = fp->ctf_strtab.cts_name;
694*f3e7f55eSRobert Mustacchi 	bzero(&fp->ctf_strtab, sizeof (ctf_sect_t));
695*f3e7f55eSRobert Mustacchi 	fp->ctf_strtab.cts_name = sname;
696e4586ebfSmws 
6977c478bd9Sstevel@tonic-gate 	bcopy(fp, &ofp, sizeof (ctf_file_t));
6987c478bd9Sstevel@tonic-gate 	bcopy(nfp, fp, sizeof (ctf_file_t));
6997c478bd9Sstevel@tonic-gate 	bcopy(&ofp, nfp, sizeof (ctf_file_t));
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	/*
7027c478bd9Sstevel@tonic-gate 	 * Initialize the ctf_lookup_by_name top-level dictionary.  We keep an
7037c478bd9Sstevel@tonic-gate 	 * array of type name prefixes and the corresponding ctf_hash to use.
7047c478bd9Sstevel@tonic-gate 	 * NOTE: This code must be kept in sync with the code in ctf_bufopen().
7057c478bd9Sstevel@tonic-gate 	 */
7067c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
7077c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
7087c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
7097c478bd9Sstevel@tonic-gate 	fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	nfp->ctf_refcnt = 1; /* force nfp to be freed */
7127c478bd9Sstevel@tonic-gate 	ctf_close(nfp);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	return (0);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
717e4586ebfSmws void
ctf_dtd_insert(ctf_file_t * fp,ctf_dtdef_t * dtd)718e4586ebfSmws ctf_dtd_insert(ctf_file_t *fp, ctf_dtdef_t *dtd)
7197c478bd9Sstevel@tonic-gate {
720e4586ebfSmws 	ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
721e4586ebfSmws 
722e4586ebfSmws 	dtd->dtd_hash = fp->ctf_dthash[h];
723e4586ebfSmws 	fp->ctf_dthash[h] = dtd;
724e4586ebfSmws 	ctf_list_append(&fp->ctf_dtdefs, dtd);
725e4586ebfSmws }
726e4586ebfSmws 
727e4586ebfSmws void
ctf_dtd_delete(ctf_file_t * fp,ctf_dtdef_t * dtd)728e4586ebfSmws ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd)
729e4586ebfSmws {
730e4586ebfSmws 	ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
731e4586ebfSmws 	ctf_dtdef_t *p, **q = &fp->ctf_dthash[h];
7327c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd, *nmd;
7337c478bd9Sstevel@tonic-gate 	size_t len;
7340a47c91cSRobert Mustacchi 	int kind, i;
7357c478bd9Sstevel@tonic-gate 
736e4586ebfSmws 	for (p = *q; p != NULL; p = p->dtd_hash) {
737e4586ebfSmws 		if (p != dtd)
738e4586ebfSmws 			q = &p->dtd_hash;
739e4586ebfSmws 		else
740e4586ebfSmws 			break;
741e4586ebfSmws 	}
7427c478bd9Sstevel@tonic-gate 
743e4586ebfSmws 	if (p != NULL)
744e4586ebfSmws 		*q = p->dtd_hash;
7457c478bd9Sstevel@tonic-gate 
7460a47c91cSRobert Mustacchi 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
7470a47c91cSRobert Mustacchi 	switch (kind) {
7487c478bd9Sstevel@tonic-gate 	case CTF_K_STRUCT:
7497c478bd9Sstevel@tonic-gate 	case CTF_K_UNION:
7507c478bd9Sstevel@tonic-gate 	case CTF_K_ENUM:
7517c478bd9Sstevel@tonic-gate 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
7527c478bd9Sstevel@tonic-gate 		    dmd != NULL; dmd = nmd) {
7537c478bd9Sstevel@tonic-gate 			if (dmd->dmd_name != NULL) {
7547c478bd9Sstevel@tonic-gate 				len = strlen(dmd->dmd_name) + 1;
7557c478bd9Sstevel@tonic-gate 				ctf_free(dmd->dmd_name, len);
7567c478bd9Sstevel@tonic-gate 				fp->ctf_dtstrlen -= len;
7577c478bd9Sstevel@tonic-gate 			}
7580a47c91cSRobert Mustacchi 			if (kind != CTF_K_ENUM)
7590a47c91cSRobert Mustacchi 				ctf_ref_dec(fp, dmd->dmd_type);
7607c478bd9Sstevel@tonic-gate 			nmd = ctf_list_next(dmd);
7617c478bd9Sstevel@tonic-gate 			ctf_free(dmd, sizeof (ctf_dmdef_t));
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 		break;
7647c478bd9Sstevel@tonic-gate 	case CTF_K_FUNCTION:
7650a47c91cSRobert Mustacchi 		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
7660a47c91cSRobert Mustacchi 		for (i = 0; i < CTF_INFO_VLEN(dtd->dtd_data.ctt_info); i++)
7670a47c91cSRobert Mustacchi 			if (dtd->dtd_u.dtu_argv[i] != 0)
7680a47c91cSRobert Mustacchi 				ctf_ref_dec(fp, dtd->dtd_u.dtu_argv[i]);
7697c478bd9Sstevel@tonic-gate 		ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
7707c478bd9Sstevel@tonic-gate 		    CTF_INFO_VLEN(dtd->dtd_data.ctt_info));
7717c478bd9Sstevel@tonic-gate 		break;
7720a47c91cSRobert Mustacchi 	case CTF_K_ARRAY:
7730a47c91cSRobert Mustacchi 		ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
7740a47c91cSRobert Mustacchi 		ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
7750a47c91cSRobert Mustacchi 		break;
7760a47c91cSRobert Mustacchi 	case CTF_K_TYPEDEF:
7770a47c91cSRobert Mustacchi 		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
7780a47c91cSRobert Mustacchi 		break;
7790a47c91cSRobert Mustacchi 	case CTF_K_POINTER:
7800a47c91cSRobert Mustacchi 	case CTF_K_VOLATILE:
7810a47c91cSRobert Mustacchi 	case CTF_K_CONST:
7820a47c91cSRobert Mustacchi 	case CTF_K_RESTRICT:
7830a47c91cSRobert Mustacchi 		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
7840a47c91cSRobert Mustacchi 		break;
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	if (dtd->dtd_name) {
7887c478bd9Sstevel@tonic-gate 		len = strlen(dtd->dtd_name) + 1;
7897c478bd9Sstevel@tonic-gate 		ctf_free(dtd->dtd_name, len);
7907c478bd9Sstevel@tonic-gate 		fp->ctf_dtstrlen -= len;
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	ctf_list_delete(&fp->ctf_dtdefs, dtd);
7947c478bd9Sstevel@tonic-gate 	ctf_free(dtd, sizeof (ctf_dtdef_t));
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate 
797e4586ebfSmws ctf_dtdef_t *
ctf_dtd_lookup(ctf_file_t * fp,ctf_id_t type)798e4586ebfSmws ctf_dtd_lookup(ctf_file_t *fp, ctf_id_t type)
799e4586ebfSmws {
800e4586ebfSmws 	ulong_t h = type & (fp->ctf_dthashlen - 1);
801e4586ebfSmws 	ctf_dtdef_t *dtd;
802e4586ebfSmws 
803e4586ebfSmws 	if (fp->ctf_dthash == NULL)
804e4586ebfSmws 		return (NULL);
805e4586ebfSmws 
806e4586ebfSmws 	for (dtd = fp->ctf_dthash[h]; dtd != NULL; dtd = dtd->dtd_hash) {
807e4586ebfSmws 		if (dtd->dtd_type == type)
808e4586ebfSmws 			break;
809e4586ebfSmws 	}
810e4586ebfSmws 
811e4586ebfSmws 	return (dtd);
812e4586ebfSmws }
813e4586ebfSmws 
814*f3e7f55eSRobert Mustacchi ctf_dsdef_t *
ctf_dsd_lookup(ctf_file_t * fp,ulong_t idx)815*f3e7f55eSRobert Mustacchi ctf_dsd_lookup(ctf_file_t *fp, ulong_t idx)
816*f3e7f55eSRobert Mustacchi {
817*f3e7f55eSRobert Mustacchi 	ctf_dsdef_t *dsd;
818*f3e7f55eSRobert Mustacchi 
819*f3e7f55eSRobert Mustacchi 	for (dsd = ctf_list_next(&fp->ctf_dsdefs); dsd != NULL;
820*f3e7f55eSRobert Mustacchi 	    dsd = ctf_list_next(dsd)) {
821*f3e7f55eSRobert Mustacchi 		if (dsd->dsd_symidx == idx)
822*f3e7f55eSRobert Mustacchi 			return (dsd);
823*f3e7f55eSRobert Mustacchi 	}
824*f3e7f55eSRobert Mustacchi 
825*f3e7f55eSRobert Mustacchi 	return (NULL);
826*f3e7f55eSRobert Mustacchi }
827*f3e7f55eSRobert Mustacchi 
828*f3e7f55eSRobert Mustacchi /*
829*f3e7f55eSRobert Mustacchi  * We order the ctf_dsdef_t by symbol index to make things better for updates.
830*f3e7f55eSRobert Mustacchi  */
831*f3e7f55eSRobert Mustacchi void
ctf_dsd_insert(ctf_file_t * fp,ctf_dsdef_t * dsd)832*f3e7f55eSRobert Mustacchi ctf_dsd_insert(ctf_file_t *fp, ctf_dsdef_t *dsd)
833*f3e7f55eSRobert Mustacchi {
834*f3e7f55eSRobert Mustacchi 	ctf_dsdef_t *i;
835*f3e7f55eSRobert Mustacchi 
836*f3e7f55eSRobert Mustacchi 	for (i = ctf_list_next(&fp->ctf_dsdefs); i != NULL;
837*f3e7f55eSRobert Mustacchi 	    i = ctf_list_next(i)) {
838*f3e7f55eSRobert Mustacchi 		if (i->dsd_symidx > dsd->dsd_symidx)
839*f3e7f55eSRobert Mustacchi 			break;
840*f3e7f55eSRobert Mustacchi 	}
841*f3e7f55eSRobert Mustacchi 
842*f3e7f55eSRobert Mustacchi 	if (i == NULL) {
843*f3e7f55eSRobert Mustacchi 		ctf_list_append(&fp->ctf_dsdefs, dsd);
844*f3e7f55eSRobert Mustacchi 		return;
845*f3e7f55eSRobert Mustacchi 	}
846*f3e7f55eSRobert Mustacchi 
847*f3e7f55eSRobert Mustacchi 	ctf_list_insert_before(&fp->ctf_dsdefs, i, dsd);
848*f3e7f55eSRobert Mustacchi }
849*f3e7f55eSRobert Mustacchi 
850*f3e7f55eSRobert Mustacchi /* ARGSUSED */
851*f3e7f55eSRobert Mustacchi void
ctf_dsd_delete(ctf_file_t * fp,ctf_dsdef_t * dsd)852*f3e7f55eSRobert Mustacchi ctf_dsd_delete(ctf_file_t *fp, ctf_dsdef_t *dsd)
853*f3e7f55eSRobert Mustacchi {
854*f3e7f55eSRobert Mustacchi 	if (dsd->dsd_nargs > 0)
855*f3e7f55eSRobert Mustacchi 		ctf_free(dsd->dsd_argc,
856*f3e7f55eSRobert Mustacchi 		    sizeof (ctf_id_t) * dsd->dsd_nargs);
857*f3e7f55eSRobert Mustacchi 	ctf_list_delete(&fp->ctf_dsdefs, dsd);
858*f3e7f55eSRobert Mustacchi 	ctf_free(dsd, sizeof (ctf_dsdef_t));
859*f3e7f55eSRobert Mustacchi }
860*f3e7f55eSRobert Mustacchi 
861*f3e7f55eSRobert Mustacchi ctf_dldef_t *
ctf_dld_lookup(ctf_file_t * fp,const char * name)862*f3e7f55eSRobert Mustacchi ctf_dld_lookup(ctf_file_t *fp, const char *name)
863*f3e7f55eSRobert Mustacchi {
864*f3e7f55eSRobert Mustacchi 	ctf_dldef_t *dld;
865*f3e7f55eSRobert Mustacchi 
866*f3e7f55eSRobert Mustacchi 	for (dld = ctf_list_next(&fp->ctf_dldefs); dld != NULL;
867*f3e7f55eSRobert Mustacchi 	    dld = ctf_list_next(dld)) {
868*f3e7f55eSRobert Mustacchi 		if (strcmp(name, dld->dld_name) == 0)
869*f3e7f55eSRobert Mustacchi 			return (dld);
870*f3e7f55eSRobert Mustacchi 	}
871*f3e7f55eSRobert Mustacchi 
872*f3e7f55eSRobert Mustacchi 	return (NULL);
873*f3e7f55eSRobert Mustacchi }
874*f3e7f55eSRobert Mustacchi 
875*f3e7f55eSRobert Mustacchi void
ctf_dld_insert(ctf_file_t * fp,ctf_dldef_t * dld,uint_t pos)876*f3e7f55eSRobert Mustacchi ctf_dld_insert(ctf_file_t *fp, ctf_dldef_t *dld, uint_t pos)
877*f3e7f55eSRobert Mustacchi {
878*f3e7f55eSRobert Mustacchi 	ctf_dldef_t *l;
879*f3e7f55eSRobert Mustacchi 
880*f3e7f55eSRobert Mustacchi 	if (pos == 0) {
881*f3e7f55eSRobert Mustacchi 		ctf_list_prepend(&fp->ctf_dldefs, dld);
882*f3e7f55eSRobert Mustacchi 		return;
883*f3e7f55eSRobert Mustacchi 	}
884*f3e7f55eSRobert Mustacchi 
885*f3e7f55eSRobert Mustacchi 	for (l = ctf_list_next(&fp->ctf_dldefs); pos != 0 && dld != NULL;
886*f3e7f55eSRobert Mustacchi 	    l = ctf_list_next(l), pos--)
887*f3e7f55eSRobert Mustacchi 		;
888*f3e7f55eSRobert Mustacchi 
889*f3e7f55eSRobert Mustacchi 	if (l == NULL)
890*f3e7f55eSRobert Mustacchi 		ctf_list_append(&fp->ctf_dldefs, dld);
891*f3e7f55eSRobert Mustacchi 	else
892*f3e7f55eSRobert Mustacchi 		ctf_list_insert_before(&fp->ctf_dsdefs, l, dld);
893*f3e7f55eSRobert Mustacchi }
894*f3e7f55eSRobert Mustacchi 
895*f3e7f55eSRobert Mustacchi void
ctf_dld_delete(ctf_file_t * fp,ctf_dldef_t * dld)896*f3e7f55eSRobert Mustacchi ctf_dld_delete(ctf_file_t *fp, ctf_dldef_t *dld)
897*f3e7f55eSRobert Mustacchi {
898*f3e7f55eSRobert Mustacchi 	ctf_list_delete(&fp->ctf_dldefs, dld);
899*f3e7f55eSRobert Mustacchi 
900*f3e7f55eSRobert Mustacchi 	if (dld->dld_name != NULL) {
901*f3e7f55eSRobert Mustacchi 		size_t len = strlen(dld->dld_name) + 1;
902*f3e7f55eSRobert Mustacchi 		ctf_free(dld->dld_name, len);
903*f3e7f55eSRobert Mustacchi 		fp->ctf_dtstrlen -= len;
904*f3e7f55eSRobert Mustacchi 	}
905*f3e7f55eSRobert Mustacchi 
906*f3e7f55eSRobert Mustacchi 	ctf_free(dld, sizeof (ctf_dldef_t));
907*f3e7f55eSRobert Mustacchi }
908*f3e7f55eSRobert Mustacchi 
909e4586ebfSmws /*
910e4586ebfSmws  * Discard all of the dynamic type definitions that have been added to the
911e4586ebfSmws  * container since the last call to ctf_update().  We locate such types by
912e4586ebfSmws  * scanning the list and deleting elements that have type IDs greater than
9130a47c91cSRobert Mustacchi  * ctf_dtoldid, which is set by ctf_update(), above. Note that to work properly
9140a47c91cSRobert Mustacchi  * with our reference counting schemes, we must delete the dynamic list in
9150a47c91cSRobert Mustacchi  * reverse.
916e4586ebfSmws  */
917e4586ebfSmws int
ctf_discard(ctf_file_t * fp)918e4586ebfSmws ctf_discard(ctf_file_t *fp)
919e4586ebfSmws {
920e4586ebfSmws 	ctf_dtdef_t *dtd, *ntd;
921e4586ebfSmws 
922e4586ebfSmws 	if (!(fp->ctf_flags & LCTF_RDWR))
923e4586ebfSmws 		return (ctf_set_errno(fp, ECTF_RDONLY));
924e4586ebfSmws 
925e4586ebfSmws 	if (!(fp->ctf_flags & LCTF_DIRTY))
926e4586ebfSmws 		return (0); /* no update required */
927e4586ebfSmws 
9280a47c91cSRobert Mustacchi 	for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
929*f3e7f55eSRobert Mustacchi 		ntd = ctf_list_prev(dtd);
930e4586ebfSmws 		if (dtd->dtd_type <= fp->ctf_dtoldid)
931e4586ebfSmws 			continue; /* skip types that have been committed */
932e4586ebfSmws 
933e4586ebfSmws 		ctf_dtd_delete(fp, dtd);
934e4586ebfSmws 	}
935e4586ebfSmws 
9367c478bd9Sstevel@tonic-gate 	fp->ctf_dtnextid = fp->ctf_dtoldid + 1;
9377c478bd9Sstevel@tonic-gate 	fp->ctf_flags &= ~LCTF_DIRTY;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	return (0);
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate static ctf_id_t
ctf_add_generic(ctf_file_t * fp,uint_t flag,const char * name,ctf_dtdef_t ** rp)9437c478bd9Sstevel@tonic-gate ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
9467c478bd9Sstevel@tonic-gate 	ctf_id_t type;
9477c478bd9Sstevel@tonic-gate 	char *s = NULL;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
9507c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
9537c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	if (CTF_INDEX_TO_TYPE(fp->ctf_dtnextid, 1) > CTF_MAX_TYPE)
9567c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_FULL));
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL)
9597c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
9627c478bd9Sstevel@tonic-gate 		ctf_free(dtd, sizeof (ctf_dtdef_t));
9637c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	type = fp->ctf_dtnextid++;
9677c478bd9Sstevel@tonic-gate 	type = CTF_INDEX_TO_TYPE(type, (fp->ctf_flags & LCTF_CHILD));
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	bzero(dtd, sizeof (ctf_dtdef_t));
9707c478bd9Sstevel@tonic-gate 	dtd->dtd_name = s;
9717c478bd9Sstevel@tonic-gate 	dtd->dtd_type = type;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	if (s != NULL)
9747c478bd9Sstevel@tonic-gate 		fp->ctf_dtstrlen += strlen(s) + 1;
9757c478bd9Sstevel@tonic-gate 
976e4586ebfSmws 	ctf_dtd_insert(fp, dtd);
9777c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	*rp = dtd;
9807c478bd9Sstevel@tonic-gate 	return (type);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate 
983*f3e7f55eSRobert Mustacchi ctf_id_t
ctf_add_encoded(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep,uint_t kind)9847c478bd9Sstevel@tonic-gate ctf_add_encoded(ctf_file_t *fp, uint_t flag,
9857c478bd9Sstevel@tonic-gate     const char *name, const ctf_encoding_t *ep, uint_t kind)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
9887c478bd9Sstevel@tonic-gate 	ctf_id_t type;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (ep == NULL)
9917c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
9947c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
997*f3e7f55eSRobert Mustacchi 
998*f3e7f55eSRobert Mustacchi 	/*
999*f3e7f55eSRobert Mustacchi 	 * If the type's size is not an even number of bytes, then we should
1000*f3e7f55eSRobert Mustacchi 	 * round up the type size to the nearest byte.
1001*f3e7f55eSRobert Mustacchi 	 */
1002*f3e7f55eSRobert Mustacchi 	dtd->dtd_data.ctt_size = ep->cte_bits / NBBY;
1003*f3e7f55eSRobert Mustacchi 	if ((ep->cte_bits % NBBY) != 0)
1004*f3e7f55eSRobert Mustacchi 		dtd->dtd_data.ctt_size++;
10057c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_enc = *ep;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	return (type);
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate 
1010*f3e7f55eSRobert Mustacchi ctf_id_t
ctf_add_reftype(ctf_file_t * fp,uint_t flag,const char * name,ctf_id_t ref,uint_t kind)1011*f3e7f55eSRobert Mustacchi ctf_add_reftype(ctf_file_t *fp, uint_t flag,
1012*f3e7f55eSRobert Mustacchi     const char *name, ctf_id_t ref, uint_t kind)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
10157c478bd9Sstevel@tonic-gate 	ctf_id_t type;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE)
10187c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
10197c478bd9Sstevel@tonic-gate 
1020*f3e7f55eSRobert Mustacchi 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
10217c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
10227c478bd9Sstevel@tonic-gate 
10230a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, ref);
10240a47c91cSRobert Mustacchi 
10257c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
10267c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_type = (ushort_t)ref;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	return (type);
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_integer(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep)10327c478bd9Sstevel@tonic-gate ctf_add_integer(ctf_file_t *fp, uint_t flag,
10337c478bd9Sstevel@tonic-gate     const char *name, const ctf_encoding_t *ep)
10347c478bd9Sstevel@tonic-gate {
10357c478bd9Sstevel@tonic-gate 	return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER));
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_float(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep)10397c478bd9Sstevel@tonic-gate ctf_add_float(ctf_file_t *fp, uint_t flag,
10407c478bd9Sstevel@tonic-gate     const char *name, const ctf_encoding_t *ep)
10417c478bd9Sstevel@tonic-gate {
10427c478bd9Sstevel@tonic-gate 	return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT));
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_pointer(ctf_file_t * fp,uint_t flag,const char * name,ctf_id_t ref)1046*f3e7f55eSRobert Mustacchi ctf_add_pointer(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
10477c478bd9Sstevel@tonic-gate {
1048*f3e7f55eSRobert Mustacchi 	return (ctf_add_reftype(fp, flag, name, ref, CTF_K_POINTER));
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_array(ctf_file_t * fp,uint_t flag,const ctf_arinfo_t * arp)10527c478bd9Sstevel@tonic-gate ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
10557c478bd9Sstevel@tonic-gate 	ctf_id_t type;
10560a47c91cSRobert Mustacchi 	ctf_file_t *fpd;
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	if (arp == NULL)
10597c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
10607c478bd9Sstevel@tonic-gate 
10610a47c91cSRobert Mustacchi 	fpd = fp;
10620a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
1063*f3e7f55eSRobert Mustacchi 	    ctf_dtd_lookup(fp, arp->ctr_contents) == NULL) {
1064*f3e7f55eSRobert Mustacchi 		ctf_dprintf("bad contents for array: %ld\n",
1065*f3e7f55eSRobert Mustacchi 		    arp->ctr_contents);
10660a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
1067*f3e7f55eSRobert Mustacchi 	}
10680a47c91cSRobert Mustacchi 
10690a47c91cSRobert Mustacchi 	fpd = fp;
10700a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
1071*f3e7f55eSRobert Mustacchi 	    ctf_dtd_lookup(fp, arp->ctr_index) == NULL) {
1072*f3e7f55eSRobert Mustacchi 		ctf_dprintf("bad index for array: %ld\n", arp->ctr_index);
10730a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
1074*f3e7f55eSRobert Mustacchi 	}
10750a47c91cSRobert Mustacchi 
10767c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
10777c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, flag, 0);
10807c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_size = 0;
10817c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_arr = *arp;
10820a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, arp->ctr_contents);
10830a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, arp->ctr_index);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	return (type);
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate int
ctf_set_array(ctf_file_t * fp,ctf_id_t type,const ctf_arinfo_t * arp)10897c478bd9Sstevel@tonic-gate ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
10907c478bd9Sstevel@tonic-gate {
10910a47c91cSRobert Mustacchi 	ctf_file_t *fpd;
1092e4586ebfSmws 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
10957c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (dtd == NULL || CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
10987c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_BADID));
10997c478bd9Sstevel@tonic-gate 
11000a47c91cSRobert Mustacchi 	fpd = fp;
11010a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
11020a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)
11030a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
11040a47c91cSRobert Mustacchi 
11050a47c91cSRobert Mustacchi 	fpd = fp;
11060a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
11070a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, arp->ctr_index) == NULL)
11080a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
11090a47c91cSRobert Mustacchi 
11100a47c91cSRobert Mustacchi 	ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
11110a47c91cSRobert Mustacchi 	ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
11127c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
11137c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_arr = *arp;
11140a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, arp->ctr_contents);
11150a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, arp->ctr_index);
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	return (0);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_funcptr(ctf_file_t * fp,uint_t flag,const ctf_funcinfo_t * ctc,const ctf_id_t * argv)1121*f3e7f55eSRobert Mustacchi ctf_add_funcptr(ctf_file_t *fp, uint_t flag,
11227c478bd9Sstevel@tonic-gate     const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
11257c478bd9Sstevel@tonic-gate 	ctf_id_t type;
11267c478bd9Sstevel@tonic-gate 	uint_t vlen;
11270a47c91cSRobert Mustacchi 	int i;
11287c478bd9Sstevel@tonic-gate 	ctf_id_t *vdat = NULL;
11290a47c91cSRobert Mustacchi 	ctf_file_t *fpd;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 ||
11327c478bd9Sstevel@tonic-gate 	    (ctc->ctc_argc != 0 && argv == NULL))
11337c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	vlen = ctc->ctc_argc;
11367c478bd9Sstevel@tonic-gate 	if (ctc->ctc_flags & CTF_FUNC_VARARG)
11377c478bd9Sstevel@tonic-gate 		vlen++; /* add trailing zero to indicate varargs (see below) */
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	if (vlen > CTF_MAX_VLEN)
11407c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EOVERFLOW));
11417c478bd9Sstevel@tonic-gate 
11420a47c91cSRobert Mustacchi 	fpd = fp;
11430a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, ctc->ctc_return) == NULL &&
11440a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, ctc->ctc_return) == NULL)
11450a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
11460a47c91cSRobert Mustacchi 
11470a47c91cSRobert Mustacchi 	for (i = 0; i < ctc->ctc_argc; i++) {
11480a47c91cSRobert Mustacchi 		fpd = fp;
11490a47c91cSRobert Mustacchi 		if (ctf_lookup_by_id(&fpd, argv[i]) == NULL &&
11500a47c91cSRobert Mustacchi 		    ctf_dtd_lookup(fp, argv[i]) == NULL)
11510a47c91cSRobert Mustacchi 			return (ctf_set_errno(fp, ECTF_BADID));
11520a47c91cSRobert Mustacchi 	}
11530a47c91cSRobert Mustacchi 
11547c478bd9Sstevel@tonic-gate 	if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL)
11557c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) {
11587c478bd9Sstevel@tonic-gate 		ctf_free(vdat, sizeof (ctf_id_t) * vlen);
11597c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, flag, vlen);
11637c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_type = (ushort_t)ctc->ctc_return;
11647c478bd9Sstevel@tonic-gate 
11650a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, ctc->ctc_return);
11660a47c91cSRobert Mustacchi 	for (i = 0; i < ctc->ctc_argc; i++)
11670a47c91cSRobert Mustacchi 		ctf_ref_inc(fp, argv[i]);
11680a47c91cSRobert Mustacchi 
11697c478bd9Sstevel@tonic-gate 	bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc);
11707c478bd9Sstevel@tonic-gate 	if (ctc->ctc_flags & CTF_FUNC_VARARG)
11717c478bd9Sstevel@tonic-gate 		vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */
11727c478bd9Sstevel@tonic-gate 	dtd->dtd_u.dtu_argv = vdat;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	return (type);
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_struct(ctf_file_t * fp,uint_t flag,const char * name)11787c478bd9Sstevel@tonic-gate ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name)
11797c478bd9Sstevel@tonic-gate {
1180e4586ebfSmws 	ctf_hash_t *hp = &fp->ctf_structs;
1181e4586ebfSmws 	ctf_helem_t *hep = NULL;
1182*f3e7f55eSRobert Mustacchi 	ctf_dtdef_t *dtd = NULL;
1183*f3e7f55eSRobert Mustacchi 	ctf_id_t type = CTF_ERR;
11847c478bd9Sstevel@tonic-gate 
1185e4586ebfSmws 	if (name != NULL)
1186e4586ebfSmws 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1187e4586ebfSmws 
1188*f3e7f55eSRobert Mustacchi 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1189*f3e7f55eSRobert Mustacchi 		type = hep->h_type;
1190*f3e7f55eSRobert Mustacchi 		dtd = ctf_dtd_lookup(fp, type);
1191*f3e7f55eSRobert Mustacchi 		if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1192*f3e7f55eSRobert Mustacchi 			dtd = NULL;
1193*f3e7f55eSRobert Mustacchi 	}
11947c478bd9Sstevel@tonic-gate 
1195*f3e7f55eSRobert Mustacchi 	if (dtd == NULL) {
1196*f3e7f55eSRobert Mustacchi 		type = ctf_add_generic(fp, flag, name, &dtd);
1197*f3e7f55eSRobert Mustacchi 		if (type == CTF_ERR)
1198*f3e7f55eSRobert Mustacchi 			return (CTF_ERR); /* errno is set for us */
1199*f3e7f55eSRobert Mustacchi 	}
1200*f3e7f55eSRobert Mustacchi 
1201*f3e7f55eSRobert Mustacchi 	VERIFY(type != CTF_ERR);
12027c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, flag, 0);
1203e4586ebfSmws 	dtd->dtd_data.ctt_size = 0;
1204e4586ebfSmws 
1205*f3e7f55eSRobert Mustacchi 	/*
1206*f3e7f55eSRobert Mustacchi 	 * Always dirty in case we modified a forward.
1207*f3e7f55eSRobert Mustacchi 	 */
1208*f3e7f55eSRobert Mustacchi 	fp->ctf_flags |= LCTF_DIRTY;
1209*f3e7f55eSRobert Mustacchi 
12107c478bd9Sstevel@tonic-gate 	return (type);
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_union(ctf_file_t * fp,uint_t flag,const char * name)12147c478bd9Sstevel@tonic-gate ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name)
12157c478bd9Sstevel@tonic-gate {
1216e4586ebfSmws 	ctf_hash_t *hp = &fp->ctf_unions;
1217e4586ebfSmws 	ctf_helem_t *hep = NULL;
1218*f3e7f55eSRobert Mustacchi 	ctf_dtdef_t *dtd = NULL;
1219*f3e7f55eSRobert Mustacchi 	ctf_id_t type = CTF_ERR;
12207c478bd9Sstevel@tonic-gate 
1221e4586ebfSmws 	if (name != NULL)
1222e4586ebfSmws 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1223e4586ebfSmws 
1224*f3e7f55eSRobert Mustacchi 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1225*f3e7f55eSRobert Mustacchi 		type = hep->h_type;
1226*f3e7f55eSRobert Mustacchi 		dtd = ctf_dtd_lookup(fp, type);
1227*f3e7f55eSRobert Mustacchi 		if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1228*f3e7f55eSRobert Mustacchi 			dtd = NULL;
1229*f3e7f55eSRobert Mustacchi 	}
12307c478bd9Sstevel@tonic-gate 
1231*f3e7f55eSRobert Mustacchi 	if (dtd == NULL) {
1232*f3e7f55eSRobert Mustacchi 		type = ctf_add_generic(fp, flag, name, &dtd);
1233*f3e7f55eSRobert Mustacchi 		if (type == CTF_ERR)
1234*f3e7f55eSRobert Mustacchi 			return (CTF_ERR); /* errno is set for us */
1235*f3e7f55eSRobert Mustacchi 	}
1236*f3e7f55eSRobert Mustacchi 
1237*f3e7f55eSRobert Mustacchi 	VERIFY(type != CTF_ERR);
12387c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, flag, 0);
1239e4586ebfSmws 	dtd->dtd_data.ctt_size = 0;
1240e4586ebfSmws 
1241*f3e7f55eSRobert Mustacchi 	/*
1242*f3e7f55eSRobert Mustacchi 	 * Always dirty in case we modified a forward.
1243*f3e7f55eSRobert Mustacchi 	 */
1244*f3e7f55eSRobert Mustacchi 	fp->ctf_flags |= LCTF_DIRTY;
1245*f3e7f55eSRobert Mustacchi 
12467c478bd9Sstevel@tonic-gate 	return (type);
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_enum(ctf_file_t * fp,uint_t flag,const char * name)12507c478bd9Sstevel@tonic-gate ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name)
12517c478bd9Sstevel@tonic-gate {
1252e4586ebfSmws 	ctf_hash_t *hp = &fp->ctf_enums;
1253e4586ebfSmws 	ctf_helem_t *hep = NULL;
1254*f3e7f55eSRobert Mustacchi 	ctf_dtdef_t *dtd = NULL;
1255*f3e7f55eSRobert Mustacchi 	ctf_id_t type = CTF_ERR;
12567c478bd9Sstevel@tonic-gate 
1257e4586ebfSmws 	if (name != NULL)
1258e4586ebfSmws 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1259e4586ebfSmws 
1260*f3e7f55eSRobert Mustacchi 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1261*f3e7f55eSRobert Mustacchi 		type = hep->h_type;
1262*f3e7f55eSRobert Mustacchi 		dtd = ctf_dtd_lookup(fp, type);
1263*f3e7f55eSRobert Mustacchi 		if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1264*f3e7f55eSRobert Mustacchi 			dtd = NULL;
1265*f3e7f55eSRobert Mustacchi 	}
12667c478bd9Sstevel@tonic-gate 
1267*f3e7f55eSRobert Mustacchi 	if (dtd == NULL) {
1268*f3e7f55eSRobert Mustacchi 		type = ctf_add_generic(fp, flag, name, &dtd);
1269*f3e7f55eSRobert Mustacchi 		if (type == CTF_ERR)
1270*f3e7f55eSRobert Mustacchi 			return (CTF_ERR); /* errno is set for us */
1271*f3e7f55eSRobert Mustacchi 	}
1272*f3e7f55eSRobert Mustacchi 
1273*f3e7f55eSRobert Mustacchi 	VERIFY(type != CTF_ERR);
12747c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0);
12757c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
12767c478bd9Sstevel@tonic-gate 
1277*f3e7f55eSRobert Mustacchi 	/*
1278*f3e7f55eSRobert Mustacchi 	 * Always dirty in case we modified a forward.
1279*f3e7f55eSRobert Mustacchi 	 */
1280*f3e7f55eSRobert Mustacchi 	fp->ctf_flags |= LCTF_DIRTY;
1281*f3e7f55eSRobert Mustacchi 
12827c478bd9Sstevel@tonic-gate 	return (type);
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_forward(ctf_file_t * fp,uint_t flag,const char * name,uint_t kind)12867c478bd9Sstevel@tonic-gate ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind)
12877c478bd9Sstevel@tonic-gate {
1288e4586ebfSmws 	ctf_hash_t *hp;
1289e4586ebfSmws 	ctf_helem_t *hep;
12907c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
12917c478bd9Sstevel@tonic-gate 	ctf_id_t type;
12927c478bd9Sstevel@tonic-gate 
1293e4586ebfSmws 	switch (kind) {
1294e4586ebfSmws 	case CTF_K_STRUCT:
1295e4586ebfSmws 		hp = &fp->ctf_structs;
1296e4586ebfSmws 		break;
1297e4586ebfSmws 	case CTF_K_UNION:
1298e4586ebfSmws 		hp = &fp->ctf_unions;
1299e4586ebfSmws 		break;
1300e4586ebfSmws 	case CTF_K_ENUM:
1301e4586ebfSmws 		hp = &fp->ctf_enums;
1302e4586ebfSmws 		break;
1303e4586ebfSmws 	default:
13047c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOTSUE));
1305e4586ebfSmws 	}
1306e4586ebfSmws 
1307e4586ebfSmws 	/*
1308e4586ebfSmws 	 * If the type is already defined or exists as a forward tag, just
1309e4586ebfSmws 	 * return the ctf_id_t of the existing definition.
1310e4586ebfSmws 	 */
1311e4586ebfSmws 	if (name != NULL && (hep = ctf_hash_lookup(hp,
1312e4586ebfSmws 	    fp, name, strlen(name))) != NULL)
1313e4586ebfSmws 		return (hep->h_type);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
13167c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0);
1319e4586ebfSmws 	dtd->dtd_data.ctt_type = kind;
1320e4586ebfSmws 
13217c478bd9Sstevel@tonic-gate 	return (type);
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_typedef(ctf_file_t * fp,uint_t flag,const char * name,ctf_id_t ref)13257c478bd9Sstevel@tonic-gate ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
13267c478bd9Sstevel@tonic-gate {
13277c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
13287c478bd9Sstevel@tonic-gate 	ctf_id_t type;
13290a47c91cSRobert Mustacchi 	ctf_file_t *fpd;
13307c478bd9Sstevel@tonic-gate 
13310a47c91cSRobert Mustacchi 	fpd = fp;
13320a47c91cSRobert Mustacchi 	if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL &&
13330a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, ref) == NULL))
13347c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
13377c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0);
13407c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_type = (ushort_t)ref;
13410a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, ref);
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	return (type);
13447c478bd9Sstevel@tonic-gate }
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_volatile(ctf_file_t * fp,uint_t flag,const char * name,ctf_id_t ref)1347*f3e7f55eSRobert Mustacchi ctf_add_volatile(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
13487c478bd9Sstevel@tonic-gate {
1349*f3e7f55eSRobert Mustacchi 	return (ctf_add_reftype(fp, flag, name, ref, CTF_K_VOLATILE));
13507c478bd9Sstevel@tonic-gate }
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_const(ctf_file_t * fp,uint_t flag,const char * name,ctf_id_t ref)1353*f3e7f55eSRobert Mustacchi ctf_add_const(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
13547c478bd9Sstevel@tonic-gate {
1355*f3e7f55eSRobert Mustacchi 	return (ctf_add_reftype(fp, flag, name, ref, CTF_K_CONST));
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_restrict(ctf_file_t * fp,uint_t flag,const char * name,ctf_id_t ref)1359*f3e7f55eSRobert Mustacchi ctf_add_restrict(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
13607c478bd9Sstevel@tonic-gate {
1361*f3e7f55eSRobert Mustacchi 	return (ctf_add_reftype(fp, flag, name, ref, CTF_K_RESTRICT));
13627c478bd9Sstevel@tonic-gate }
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate int
ctf_add_enumerator(ctf_file_t * fp,ctf_id_t enid,const char * name,int value)13657c478bd9Sstevel@tonic-gate ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
13667c478bd9Sstevel@tonic-gate {
1367e4586ebfSmws 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
13687c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd;
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	uint_t kind, vlen, root;
13717c478bd9Sstevel@tonic-gate 	char *s;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	if (name == NULL)
13747c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EINVAL));
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
13777c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	if (dtd == NULL)
13807c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_BADID));
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
13837c478bd9Sstevel@tonic-gate 	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
13847c478bd9Sstevel@tonic-gate 	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	if (kind != CTF_K_ENUM)
13877c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOTENUM));
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if (vlen == CTF_MAX_VLEN)
13907c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_DTFULL));
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
13937c478bd9Sstevel@tonic-gate 	    dmd != NULL; dmd = ctf_list_next(dmd)) {
1394*f3e7f55eSRobert Mustacchi 		if (strcmp(dmd->dmd_name, name) == 0) {
1395*f3e7f55eSRobert Mustacchi 			ctf_dprintf("encountered duplicate member %s\n", name);
13967c478bd9Sstevel@tonic-gate 			return (ctf_set_errno(fp, ECTF_DUPMEMBER));
13977c478bd9Sstevel@tonic-gate 		}
1398*f3e7f55eSRobert Mustacchi 	}
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
14017c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	if ((s = ctf_strdup(name)) == NULL) {
14047c478bd9Sstevel@tonic-gate 		ctf_free(dmd, sizeof (ctf_dmdef_t));
14057c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	dmd->dmd_name = s;
14097c478bd9Sstevel@tonic-gate 	dmd->dmd_type = CTF_ERR;
14107c478bd9Sstevel@tonic-gate 	dmd->dmd_offset = 0;
14117c478bd9Sstevel@tonic-gate 	dmd->dmd_value = value;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
14147c478bd9Sstevel@tonic-gate 	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	fp->ctf_dtstrlen += strlen(s) + 1;
14177c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	return (0);
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate int
ctf_add_member(ctf_file_t * fp,ctf_id_t souid,const char * name,ctf_id_t type,ulong_t offset)1423*f3e7f55eSRobert Mustacchi ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type,
1424*f3e7f55eSRobert Mustacchi     ulong_t offset)
14257c478bd9Sstevel@tonic-gate {
1426e4586ebfSmws 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
14277c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd;
14287c478bd9Sstevel@tonic-gate 
1429*f3e7f55eSRobert Mustacchi 	ulong_t mbitsz;
14307c478bd9Sstevel@tonic-gate 	ssize_t msize, malign, ssize;
14317c478bd9Sstevel@tonic-gate 	uint_t kind, vlen, root;
1432*f3e7f55eSRobert Mustacchi 	int mkind;
14337c478bd9Sstevel@tonic-gate 	char *s = NULL;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	if (!(fp->ctf_flags & LCTF_RDWR))
14367c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_RDONLY));
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	if (dtd == NULL)
14397c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_BADID));
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
14427c478bd9Sstevel@tonic-gate 	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
14437c478bd9Sstevel@tonic-gate 	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
14467c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOTSOU));
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if (vlen == CTF_MAX_VLEN)
14497c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_DTFULL));
14507c478bd9Sstevel@tonic-gate 
1451*f3e7f55eSRobert Mustacchi 	/*
1452*f3e7f55eSRobert Mustacchi 	 * Structures may have members which are anonymous. If they have two of
1453*f3e7f55eSRobert Mustacchi 	 * these, then the duplicate member detection would find it due to the
1454*f3e7f55eSRobert Mustacchi 	 * string of "", so we skip it.
1455*f3e7f55eSRobert Mustacchi 	 */
1456*f3e7f55eSRobert Mustacchi 	if (name != NULL && *name != '\0') {
14577c478bd9Sstevel@tonic-gate 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
14587c478bd9Sstevel@tonic-gate 		    dmd != NULL; dmd = ctf_list_next(dmd)) {
14597c478bd9Sstevel@tonic-gate 			if (dmd->dmd_name != NULL &&
1460*f3e7f55eSRobert Mustacchi 			    strcmp(dmd->dmd_name, name) == 0) {
14617c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(fp, ECTF_DUPMEMBER));
14627c478bd9Sstevel@tonic-gate 			}
14637c478bd9Sstevel@tonic-gate 		}
1464*f3e7f55eSRobert Mustacchi 	}
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
1467*f3e7f55eSRobert Mustacchi 	    (malign = ctf_type_align(fp, type)) == CTF_ERR ||
1468*f3e7f55eSRobert Mustacchi 	    (mkind = ctf_type_kind(fp, type)) == CTF_ERR)
14697c478bd9Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set for us */
14707c478bd9Sstevel@tonic-gate 
1471*f3e7f55eSRobert Mustacchi 	/*
1472*f3e7f55eSRobert Mustacchi 	 * ctf_type_size returns sizes in bytes. However, for bitfields, that
1473*f3e7f55eSRobert Mustacchi 	 * means that it may misrepresent and actually rounds it up to a power
1474*f3e7f55eSRobert Mustacchi 	 * of two and store that in bytes. So instead we have to get the
1475*f3e7f55eSRobert Mustacchi 	 * Integers encoding and rely on that.
1476*f3e7f55eSRobert Mustacchi 	 */
1477*f3e7f55eSRobert Mustacchi 	if (mkind == CTF_K_INTEGER) {
1478*f3e7f55eSRobert Mustacchi 		ctf_encoding_t e;
1479*f3e7f55eSRobert Mustacchi 
1480*f3e7f55eSRobert Mustacchi 		if (ctf_type_encoding(fp, type, &e) == CTF_ERR)
1481*f3e7f55eSRobert Mustacchi 			return (CTF_ERR); /* errno is set for us */
1482*f3e7f55eSRobert Mustacchi 		mbitsz = e.cte_bits;
1483*f3e7f55eSRobert Mustacchi 	} else if (mkind == CTF_K_FORWARD) {
1484*f3e7f55eSRobert Mustacchi 		/*
1485*f3e7f55eSRobert Mustacchi 		 * This is a rather rare case. In general one cannot add a
1486*f3e7f55eSRobert Mustacchi 		 * forward to a structure. However, the CTF tools traditionally
1487*f3e7f55eSRobert Mustacchi 		 * tried to add a forward to the struct cpu as the last member.
1488*f3e7f55eSRobert Mustacchi 		 * Therefore, if we find one here, we're going to verify the
1489*f3e7f55eSRobert Mustacchi 		 * size and make sure it's zero. It's certainly odd, but that's
1490*f3e7f55eSRobert Mustacchi 		 * life.
1491*f3e7f55eSRobert Mustacchi 		 *
1492*f3e7f55eSRobert Mustacchi 		 * Further, if it's not an absolute position being specified,
1493*f3e7f55eSRobert Mustacchi 		 * then we refuse to add it.
1494*f3e7f55eSRobert Mustacchi 		 */
1495*f3e7f55eSRobert Mustacchi 		if (offset == ULONG_MAX)
1496*f3e7f55eSRobert Mustacchi 			return (ctf_set_errno(fp, EINVAL));
1497*f3e7f55eSRobert Mustacchi 		VERIFY(msize == 0);
1498*f3e7f55eSRobert Mustacchi 		mbitsz = msize;
1499*f3e7f55eSRobert Mustacchi 	} else {
1500*f3e7f55eSRobert Mustacchi 		mbitsz = msize * 8;
1501*f3e7f55eSRobert Mustacchi 	}
1502*f3e7f55eSRobert Mustacchi 
15037c478bd9Sstevel@tonic-gate 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
15047c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
15077c478bd9Sstevel@tonic-gate 		ctf_free(dmd, sizeof (ctf_dmdef_t));
15087c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(fp, EAGAIN));
15097c478bd9Sstevel@tonic-gate 	}
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	dmd->dmd_name = s;
15127c478bd9Sstevel@tonic-gate 	dmd->dmd_type = type;
15137c478bd9Sstevel@tonic-gate 	dmd->dmd_value = -1;
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_STRUCT && vlen != 0) {
15167c478bd9Sstevel@tonic-gate 		ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
15177c478bd9Sstevel@tonic-gate 		ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
1518*f3e7f55eSRobert Mustacchi 		size_t off;
15197c478bd9Sstevel@tonic-gate 
1520*f3e7f55eSRobert Mustacchi 		if (offset == ULONG_MAX) {
15217c478bd9Sstevel@tonic-gate 			ctf_encoding_t linfo;
15227c478bd9Sstevel@tonic-gate 			ssize_t lsize;
15237c478bd9Sstevel@tonic-gate 
1524*f3e7f55eSRobert Mustacchi 			off = lmd->dmd_offset;
15257c478bd9Sstevel@tonic-gate 			if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
15267c478bd9Sstevel@tonic-gate 				off += linfo.cte_bits;
15277c478bd9Sstevel@tonic-gate 			else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
15287c478bd9Sstevel@tonic-gate 				off += lsize * NBBY;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 			/*
1531*f3e7f55eSRobert Mustacchi 			 * Round up the offset of the end of the last member to
1532*f3e7f55eSRobert Mustacchi 			 * the next byte boundary, convert 'off' to bytes, and
1533*f3e7f55eSRobert Mustacchi 			 * then round it up again to the next multiple of the
1534*f3e7f55eSRobert Mustacchi 			 * alignment required by the new member.  Finally,
1535*f3e7f55eSRobert Mustacchi 			 * convert back to bits and store the result in
1536*f3e7f55eSRobert Mustacchi 			 * dmd_offset.  Technically we could do more efficient
1537*f3e7f55eSRobert Mustacchi 			 * packing if the new member is a bit-field, but we're
1538*f3e7f55eSRobert Mustacchi 			 * the "compiler" and ANSI says we can do as we choose.
15397c478bd9Sstevel@tonic-gate 			 */
15407c478bd9Sstevel@tonic-gate 			off = roundup(off, NBBY) / NBBY;
15417c478bd9Sstevel@tonic-gate 			off = roundup(off, MAX(malign, 1));
15427c478bd9Sstevel@tonic-gate 			dmd->dmd_offset = off * NBBY;
15437c478bd9Sstevel@tonic-gate 			ssize = off + msize;
15447c478bd9Sstevel@tonic-gate 		} else {
1545*f3e7f55eSRobert Mustacchi 			dmd->dmd_offset = offset;
1546*f3e7f55eSRobert Mustacchi 			ssize = (offset + mbitsz) / NBBY;
1547*f3e7f55eSRobert Mustacchi 		}
1548*f3e7f55eSRobert Mustacchi 	} else {
15497c478bd9Sstevel@tonic-gate 		dmd->dmd_offset = 0;
15507c478bd9Sstevel@tonic-gate 		ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
15517c478bd9Sstevel@tonic-gate 		ssize = MAX(ssize, msize);
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	if (ssize > CTF_MAX_SIZE) {
15557c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
15567c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
15577c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
15587c478bd9Sstevel@tonic-gate 	} else
15597c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_size = (ushort_t)ssize;
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
15627c478bd9Sstevel@tonic-gate 	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	if (s != NULL)
15657c478bd9Sstevel@tonic-gate 		fp->ctf_dtstrlen += strlen(s) + 1;
15667c478bd9Sstevel@tonic-gate 
15670a47c91cSRobert Mustacchi 	ctf_ref_inc(fp, type);
15680a47c91cSRobert Mustacchi 	fp->ctf_flags |= LCTF_DIRTY;
15690a47c91cSRobert Mustacchi 	return (0);
15700a47c91cSRobert Mustacchi }
15710a47c91cSRobert Mustacchi 
15720a47c91cSRobert Mustacchi /*
15730a47c91cSRobert Mustacchi  * This removes a type from the dynamic section. This will fail if the type is
15740a47c91cSRobert Mustacchi  * referenced by another type. Note that the CTF ID is never reused currently by
15750a47c91cSRobert Mustacchi  * CTF. Note that if this container is a parent container then we just outright
15760a47c91cSRobert Mustacchi  * refuse to remove the type. There currently is no notion of searching for the
15770a47c91cSRobert Mustacchi  * ctf_dtdef_t in parent containers. If there is, then this constraint could
15780a47c91cSRobert Mustacchi  * become finer grained.
15790a47c91cSRobert Mustacchi  */
15800a47c91cSRobert Mustacchi int
ctf_delete_type(ctf_file_t * fp,ctf_id_t type)15810a47c91cSRobert Mustacchi ctf_delete_type(ctf_file_t *fp, ctf_id_t type)
15820a47c91cSRobert Mustacchi {
15830a47c91cSRobert Mustacchi 	ctf_file_t *fpd;
15840a47c91cSRobert Mustacchi 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
15850a47c91cSRobert Mustacchi 
15860a47c91cSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
15870a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_RDONLY));
15880a47c91cSRobert Mustacchi 
15890a47c91cSRobert Mustacchi 	/*
15900a47c91cSRobert Mustacchi 	 * We want to give as useful an errno as possible. That means that we
15910a47c91cSRobert Mustacchi 	 * want to distinguish between a type which does not exist and one for
15920a47c91cSRobert Mustacchi 	 * which the type is not dynamic.
15930a47c91cSRobert Mustacchi 	 */
15940a47c91cSRobert Mustacchi 	fpd = fp;
15950a47c91cSRobert Mustacchi 	if (ctf_lookup_by_id(&fpd, type) == NULL &&
15960a47c91cSRobert Mustacchi 	    ctf_dtd_lookup(fp, type) == NULL)
15970a47c91cSRobert Mustacchi 		return (CTF_ERR); /* errno is set for us */
15980a47c91cSRobert Mustacchi 
15990a47c91cSRobert Mustacchi 	if (dtd == NULL)
16000a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_NOTDYN));
16010a47c91cSRobert Mustacchi 
16020a47c91cSRobert Mustacchi 	if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1)
16030a47c91cSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_REFERENCED));
16040a47c91cSRobert Mustacchi 
16050a47c91cSRobert Mustacchi 	ctf_dtd_delete(fp, dtd);
16067c478bd9Sstevel@tonic-gate 	fp->ctf_flags |= LCTF_DIRTY;
16077c478bd9Sstevel@tonic-gate 	return (0);
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate static int
enumcmp(const char * name,int value,void * arg)16117c478bd9Sstevel@tonic-gate enumcmp(const char *name, int value, void *arg)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
16147c478bd9Sstevel@tonic-gate 	int bvalue;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type,
16177c478bd9Sstevel@tonic-gate 	    name, &bvalue) == CTF_ERR || value != bvalue);
16187c478bd9Sstevel@tonic-gate }
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate static int
enumadd(const char * name,int value,void * arg)16217c478bd9Sstevel@tonic-gate enumadd(const char *name, int value, void *arg)
16227c478bd9Sstevel@tonic-gate {
16237c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type,
16267c478bd9Sstevel@tonic-gate 	    name, value) == CTF_ERR);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16307c478bd9Sstevel@tonic-gate static int
membcmp(const char * name,ctf_id_t type,ulong_t offset,void * arg)16317c478bd9Sstevel@tonic-gate membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg)
16327c478bd9Sstevel@tonic-gate {
16337c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
16347c478bd9Sstevel@tonic-gate 	ctf_membinfo_t ctm;
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	return (ctf_member_info(ctb->ctb_file, ctb->ctb_type,
16377c478bd9Sstevel@tonic-gate 	    name, &ctm) == CTF_ERR || ctm.ctm_offset != offset);
16387c478bd9Sstevel@tonic-gate }
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate static int
membadd(const char * name,ctf_id_t type,ulong_t offset,void * arg)16417c478bd9Sstevel@tonic-gate membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg)
16427c478bd9Sstevel@tonic-gate {
16437c478bd9Sstevel@tonic-gate 	ctf_bundle_t *ctb = arg;
16447c478bd9Sstevel@tonic-gate 	ctf_dmdef_t *dmd;
16457c478bd9Sstevel@tonic-gate 	char *s = NULL;
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
16487c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(ctb->ctb_file, EAGAIN));
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
16517c478bd9Sstevel@tonic-gate 		ctf_free(dmd, sizeof (ctf_dmdef_t));
16527c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(ctb->ctb_file, EAGAIN));
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	/*
16567c478bd9Sstevel@tonic-gate 	 * For now, dmd_type is copied as the src_fp's type; it is reset to an
16577c478bd9Sstevel@tonic-gate 	 * equivalent dst_fp type by a final loop in ctf_add_type(), below.
16587c478bd9Sstevel@tonic-gate 	 */
16597c478bd9Sstevel@tonic-gate 	dmd->dmd_name = s;
16607c478bd9Sstevel@tonic-gate 	dmd->dmd_type = type;
16617c478bd9Sstevel@tonic-gate 	dmd->dmd_offset = offset;
16627c478bd9Sstevel@tonic-gate 	dmd->dmd_value = -1;
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (s != NULL)
16677c478bd9Sstevel@tonic-gate 		ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1;
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
16707c478bd9Sstevel@tonic-gate 	return (0);
16717c478bd9Sstevel@tonic-gate }
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate /*
16747c478bd9Sstevel@tonic-gate  * The ctf_add_type routine is used to copy a type from a source CTF container
16757c478bd9Sstevel@tonic-gate  * to a dynamic destination container.  This routine operates recursively by
16767c478bd9Sstevel@tonic-gate  * following the source type's links and embedded member types.  If the
16777c478bd9Sstevel@tonic-gate  * destination container already contains a named type which has the same
16787c478bd9Sstevel@tonic-gate  * attributes, then we succeed and return this type but no changes occur.
16797c478bd9Sstevel@tonic-gate  */
16807c478bd9Sstevel@tonic-gate ctf_id_t
ctf_add_type(ctf_file_t * dst_fp,ctf_file_t * src_fp,ctf_id_t src_type)16817c478bd9Sstevel@tonic-gate ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
16827c478bd9Sstevel@tonic-gate {
16837c478bd9Sstevel@tonic-gate 	ctf_id_t dst_type = CTF_ERR;
16847c478bd9Sstevel@tonic-gate 	uint_t dst_kind = CTF_K_UNKNOWN;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	const ctf_type_t *tp;
16877c478bd9Sstevel@tonic-gate 	const char *name;
16887c478bd9Sstevel@tonic-gate 	uint_t kind, flag, vlen;
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	ctf_bundle_t src, dst;
16917c478bd9Sstevel@tonic-gate 	ctf_encoding_t src_en, dst_en;
16927c478bd9Sstevel@tonic-gate 	ctf_arinfo_t src_ar, dst_ar;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	ctf_dtdef_t *dtd;
16957c478bd9Sstevel@tonic-gate 	ctf_funcinfo_t ctc;
16967c478bd9Sstevel@tonic-gate 	ssize_t size;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	ctf_hash_t *hp;
16997c478bd9Sstevel@tonic-gate 	ctf_helem_t *hep;
17007c478bd9Sstevel@tonic-gate 
17010a47c91cSRobert Mustacchi 	if (dst_fp == src_fp)
17020a47c91cSRobert Mustacchi 		return (src_type);
17030a47c91cSRobert Mustacchi 
17047c478bd9Sstevel@tonic-gate 	if (!(dst_fp->ctf_flags & LCTF_RDWR))
17057c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ECTF_RDONLY));
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)
17087c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	name = ctf_strptr(src_fp, tp->ctt_name);
17117c478bd9Sstevel@tonic-gate 	kind = LCTF_INFO_KIND(src_fp, tp->ctt_info);
17127c478bd9Sstevel@tonic-gate 	flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info);
17137c478bd9Sstevel@tonic-gate 	vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info);
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	switch (kind) {
17167c478bd9Sstevel@tonic-gate 	case CTF_K_STRUCT:
17177c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_structs;
17187c478bd9Sstevel@tonic-gate 		break;
17197c478bd9Sstevel@tonic-gate 	case CTF_K_UNION:
17207c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_unions;
17217c478bd9Sstevel@tonic-gate 		break;
17227c478bd9Sstevel@tonic-gate 	case CTF_K_ENUM:
17237c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_enums;
17247c478bd9Sstevel@tonic-gate 		break;
17257c478bd9Sstevel@tonic-gate 	default:
17267c478bd9Sstevel@tonic-gate 		hp = &dst_fp->ctf_names;
17277c478bd9Sstevel@tonic-gate 		break;
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	/*
17317c478bd9Sstevel@tonic-gate 	 * If the source type has a name and is a root type (visible at the
17327c478bd9Sstevel@tonic-gate 	 * top-level scope), lookup the name in the destination container and
17337c478bd9Sstevel@tonic-gate 	 * verify that it is of the same kind before we do anything else.
17347c478bd9Sstevel@tonic-gate 	 */
17357c478bd9Sstevel@tonic-gate 	if ((flag & CTF_ADD_ROOT) && name[0] != '\0' &&
17367c478bd9Sstevel@tonic-gate 	    (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) {
17377c478bd9Sstevel@tonic-gate 		dst_type = (ctf_id_t)hep->h_type;
17387c478bd9Sstevel@tonic-gate 		dst_kind = ctf_type_kind(dst_fp, dst_type);
17397c478bd9Sstevel@tonic-gate 	}
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	/*
17427c478bd9Sstevel@tonic-gate 	 * If an identically named dst_type exists, fail with ECTF_CONFLICT
17437c478bd9Sstevel@tonic-gate 	 * unless dst_type is a forward declaration and src_type is a struct,
17447c478bd9Sstevel@tonic-gate 	 * union, or enum (i.e. the definition of the previous forward decl).
17457c478bd9Sstevel@tonic-gate 	 */
17467c478bd9Sstevel@tonic-gate 	if (dst_type != CTF_ERR && dst_kind != kind && (
17477c478bd9Sstevel@tonic-gate 	    dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM &&
17487c478bd9Sstevel@tonic-gate 	    kind != CTF_K_STRUCT && kind != CTF_K_UNION)))
17497c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	/*
17527c478bd9Sstevel@tonic-gate 	 * If the non-empty name was not found in the appropriate hash, search
17537c478bd9Sstevel@tonic-gate 	 * the list of pending dynamic definitions that are not yet committed.
17547c478bd9Sstevel@tonic-gate 	 * If a matching name and kind are found, assume this is the type that
17557c478bd9Sstevel@tonic-gate 	 * we are looking for.  This is necessary to permit ctf_add_type() to
17567c478bd9Sstevel@tonic-gate 	 * operate recursively on entities such as a struct that contains a
17577c478bd9Sstevel@tonic-gate 	 * pointer member that refers to the same struct type.
17587c478bd9Sstevel@tonic-gate 	 */
17597c478bd9Sstevel@tonic-gate 	if (dst_type == CTF_ERR && name[0] != '\0') {
17607c478bd9Sstevel@tonic-gate 		for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL &&
17617c478bd9Sstevel@tonic-gate 		    dtd->dtd_type > dst_fp->ctf_dtoldid;
17627c478bd9Sstevel@tonic-gate 		    dtd = ctf_list_prev(dtd)) {
17637c478bd9Sstevel@tonic-gate 			if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) == kind &&
17647c478bd9Sstevel@tonic-gate 			    dtd->dtd_name != NULL &&
17657c478bd9Sstevel@tonic-gate 			    strcmp(dtd->dtd_name, name) == 0)
17667c478bd9Sstevel@tonic-gate 				return (dtd->dtd_type);
17677c478bd9Sstevel@tonic-gate 		}
17687c478bd9Sstevel@tonic-gate 	}
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	src.ctb_file = src_fp;
17717c478bd9Sstevel@tonic-gate 	src.ctb_type = src_type;
17727c478bd9Sstevel@tonic-gate 	src.ctb_dtd = NULL;
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	dst.ctb_file = dst_fp;
17757c478bd9Sstevel@tonic-gate 	dst.ctb_type = dst_type;
17767c478bd9Sstevel@tonic-gate 	dst.ctb_dtd = NULL;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	/*
17797c478bd9Sstevel@tonic-gate 	 * Now perform kind-specific processing.  If dst_type is CTF_ERR, then
17807c478bd9Sstevel@tonic-gate 	 * we add a new type with the same properties as src_type to dst_fp.
17817c478bd9Sstevel@tonic-gate 	 * If dst_type is not CTF_ERR, then we verify that dst_type has the
17827c478bd9Sstevel@tonic-gate 	 * same attributes as src_type.  We recurse for embedded references.
17837c478bd9Sstevel@tonic-gate 	 */
17847c478bd9Sstevel@tonic-gate 	switch (kind) {
17857c478bd9Sstevel@tonic-gate 	case CTF_K_INTEGER:
17867c478bd9Sstevel@tonic-gate 	case CTF_K_FLOAT:
17877c478bd9Sstevel@tonic-gate 		if (ctf_type_encoding(src_fp, src_type, &src_en) != 0)
17887c478bd9Sstevel@tonic-gate 			return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR) {
17917c478bd9Sstevel@tonic-gate 			if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0)
17927c478bd9Sstevel@tonic-gate 				return (CTF_ERR); /* errno is set for us */
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 			if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t)))
17957c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 		} else if (kind == CTF_K_INTEGER) {
17987c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_integer(dst_fp, flag, name, &src_en);
17997c478bd9Sstevel@tonic-gate 		} else
18007c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_float(dst_fp, flag, name, &src_en);
18017c478bd9Sstevel@tonic-gate 		break;
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	case CTF_K_POINTER:
18047c478bd9Sstevel@tonic-gate 	case CTF_K_VOLATILE:
18057c478bd9Sstevel@tonic-gate 	case CTF_K_CONST:
18067c478bd9Sstevel@tonic-gate 	case CTF_K_RESTRICT:
18077c478bd9Sstevel@tonic-gate 		src_type = ctf_type_reference(src_fp, src_type);
18087c478bd9Sstevel@tonic-gate 		src_type = ctf_add_type(dst_fp, src_fp, src_type);
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 		if (src_type == CTF_ERR)
18117c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
18127c478bd9Sstevel@tonic-gate 
1813*f3e7f55eSRobert Mustacchi 		dst_type = ctf_add_reftype(dst_fp, flag, NULL, src_type, kind);
18147c478bd9Sstevel@tonic-gate 		break;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	case CTF_K_ARRAY:
18177c478bd9Sstevel@tonic-gate 		if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR)
18187c478bd9Sstevel@tonic-gate 			return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 		src_ar.ctr_contents =
18217c478bd9Sstevel@tonic-gate 		    ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents);
18227c478bd9Sstevel@tonic-gate 		src_ar.ctr_index =
18237c478bd9Sstevel@tonic-gate 		    ctf_add_type(dst_fp, src_fp, src_ar.ctr_index);
18247c478bd9Sstevel@tonic-gate 		src_ar.ctr_nelems = src_ar.ctr_nelems;
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 		if (src_ar.ctr_contents == CTF_ERR ||
18277c478bd9Sstevel@tonic-gate 		    src_ar.ctr_index == CTF_ERR)
18287c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR) {
18317c478bd9Sstevel@tonic-gate 			if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0)
18327c478bd9Sstevel@tonic-gate 				return (CTF_ERR); /* errno is set for us */
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 			if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
18357c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
18367c478bd9Sstevel@tonic-gate 		} else
18377c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_array(dst_fp, flag, &src_ar);
18387c478bd9Sstevel@tonic-gate 		break;
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	case CTF_K_FUNCTION:
18417c478bd9Sstevel@tonic-gate 		ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type);
18427c478bd9Sstevel@tonic-gate 		ctc.ctc_argc = 0;
18437c478bd9Sstevel@tonic-gate 		ctc.ctc_flags = 0;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 		if (ctc.ctc_return == CTF_ERR)
18467c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
18477c478bd9Sstevel@tonic-gate 
1848*f3e7f55eSRobert Mustacchi 		dst_type = ctf_add_funcptr(dst_fp, flag, &ctc, NULL);
18497c478bd9Sstevel@tonic-gate 		break;
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	case CTF_K_STRUCT:
18527c478bd9Sstevel@tonic-gate 	case CTF_K_UNION: {
18537c478bd9Sstevel@tonic-gate 		ctf_dmdef_t *dmd;
18547c478bd9Sstevel@tonic-gate 		int errs = 0;
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 		/*
18577c478bd9Sstevel@tonic-gate 		 * Technically to match a struct or union we need to check both
18587c478bd9Sstevel@tonic-gate 		 * ways (src members vs. dst, dst members vs. src) but we make
18597c478bd9Sstevel@tonic-gate 		 * this more optimal by only checking src vs. dst and comparing
18607c478bd9Sstevel@tonic-gate 		 * the total size of the structure (which we must do anyway)
18617c478bd9Sstevel@tonic-gate 		 * which covers the possibility of dst members not in src.
18627c478bd9Sstevel@tonic-gate 		 * This optimization can be defeated for unions, but is so
18637c478bd9Sstevel@tonic-gate 		 * pathological as to render it irrelevant for our purposes.
18647c478bd9Sstevel@tonic-gate 		 */
18657c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
18667c478bd9Sstevel@tonic-gate 			if (ctf_type_size(src_fp, src_type) !=
18677c478bd9Sstevel@tonic-gate 			    ctf_type_size(dst_fp, dst_type))
18687c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 			if (ctf_member_iter(src_fp, src_type, membcmp, &dst))
18717c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 			break;
18747c478bd9Sstevel@tonic-gate 		}
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 		/*
18777c478bd9Sstevel@tonic-gate 		 * Unlike the other cases, copying structs and unions is done
18787c478bd9Sstevel@tonic-gate 		 * manually so as to avoid repeated lookups in ctf_add_member
18797c478bd9Sstevel@tonic-gate 		 * and to ensure the exact same member offsets as in src_type.
18807c478bd9Sstevel@tonic-gate 		 */
18817c478bd9Sstevel@tonic-gate 		dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);
18827c478bd9Sstevel@tonic-gate 		if (dst_type == CTF_ERR)
18837c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 		dst.ctb_type = dst_type;
18867c478bd9Sstevel@tonic-gate 		dst.ctb_dtd = dtd;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 		if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)
18897c478bd9Sstevel@tonic-gate 			errs++; /* increment errs and fail at bottom of case */
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 		if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) {
18927c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
18937c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
18947c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
18957c478bd9Sstevel@tonic-gate 		} else
18967c478bd9Sstevel@tonic-gate 			dtd->dtd_data.ctt_size = (ushort_t)size;
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 		dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen);
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 		/*
19017c478bd9Sstevel@tonic-gate 		 * Make a final pass through the members changing each dmd_type
19027c478bd9Sstevel@tonic-gate 		 * (a src_fp type) to an equivalent type in dst_fp.  We pass
19037c478bd9Sstevel@tonic-gate 		 * through all members, leaving any that fail set to CTF_ERR.
19047c478bd9Sstevel@tonic-gate 		 */
19057c478bd9Sstevel@tonic-gate 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
19067c478bd9Sstevel@tonic-gate 		    dmd != NULL; dmd = ctf_list_next(dmd)) {
19077c478bd9Sstevel@tonic-gate 			if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp,
19087c478bd9Sstevel@tonic-gate 			    dmd->dmd_type)) == CTF_ERR)
19097c478bd9Sstevel@tonic-gate 				errs++;
19107c478bd9Sstevel@tonic-gate 		}
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 		if (errs)
19137c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
19140a47c91cSRobert Mustacchi 
19150a47c91cSRobert Mustacchi 		/*
19160a47c91cSRobert Mustacchi 		 * Now that we know that we can't fail, we go through and bump
19170a47c91cSRobert Mustacchi 		 * all the reference counts on the member types.
19180a47c91cSRobert Mustacchi 		 */
19190a47c91cSRobert Mustacchi 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
19200a47c91cSRobert Mustacchi 		    dmd != NULL; dmd = ctf_list_next(dmd))
19210a47c91cSRobert Mustacchi 			ctf_ref_inc(dst_fp, dmd->dmd_type);
19227c478bd9Sstevel@tonic-gate 		break;
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	case CTF_K_ENUM:
19267c478bd9Sstevel@tonic-gate 		if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
19277c478bd9Sstevel@tonic-gate 			if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||
19287c478bd9Sstevel@tonic-gate 			    ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))
19297c478bd9Sstevel@tonic-gate 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
19307c478bd9Sstevel@tonic-gate 		} else {
19317c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_enum(dst_fp, flag, name);
19327c478bd9Sstevel@tonic-gate 			if ((dst.ctb_type = dst_type) == CTF_ERR ||
19337c478bd9Sstevel@tonic-gate 			    ctf_enum_iter(src_fp, src_type, enumadd, &dst))
19347c478bd9Sstevel@tonic-gate 				return (CTF_ERR); /* errno is set for us */
19357c478bd9Sstevel@tonic-gate 		}
19367c478bd9Sstevel@tonic-gate 		break;
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	case CTF_K_FORWARD:
19397c478bd9Sstevel@tonic-gate 		if (dst_type == CTF_ERR) {
19407c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_forward(dst_fp,
19417c478bd9Sstevel@tonic-gate 			    flag, name, CTF_K_STRUCT); /* assume STRUCT */
19427c478bd9Sstevel@tonic-gate 		}
19437c478bd9Sstevel@tonic-gate 		break;
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	case CTF_K_TYPEDEF:
19467c478bd9Sstevel@tonic-gate 		src_type = ctf_type_reference(src_fp, src_type);
19477c478bd9Sstevel@tonic-gate 		src_type = ctf_add_type(dst_fp, src_fp, src_type);
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 		if (src_type == CTF_ERR)
19507c478bd9Sstevel@tonic-gate 			return (CTF_ERR); /* errno is set for us */
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 		/*
19537c478bd9Sstevel@tonic-gate 		 * If dst_type is not CTF_ERR at this point, we should check if
19547c478bd9Sstevel@tonic-gate 		 * ctf_type_reference(dst_fp, dst_type) != src_type and if so
19557c478bd9Sstevel@tonic-gate 		 * fail with ECTF_CONFLICT.  However, this causes problems with
19567c478bd9Sstevel@tonic-gate 		 * <sys/types.h> typedefs that vary based on things like if
19577c478bd9Sstevel@tonic-gate 		 * _ILP32x then pid_t is int otherwise long.  We therefore omit
19587c478bd9Sstevel@tonic-gate 		 * this check and assume that if the identically named typedef
19597c478bd9Sstevel@tonic-gate 		 * already exists in dst_fp, it is correct or equivalent.
19607c478bd9Sstevel@tonic-gate 		 */
19617c478bd9Sstevel@tonic-gate 		if (dst_type == CTF_ERR) {
19627c478bd9Sstevel@tonic-gate 			dst_type = ctf_add_typedef(dst_fp, flag,
19637c478bd9Sstevel@tonic-gate 			    name, src_type);
19647c478bd9Sstevel@tonic-gate 		}
19657c478bd9Sstevel@tonic-gate 		break;
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	default:
19687c478bd9Sstevel@tonic-gate 		return (ctf_set_errno(dst_fp, ECTF_CORRUPT));
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	return (dst_type);
19727c478bd9Sstevel@tonic-gate }
1973*f3e7f55eSRobert Mustacchi 
1974*f3e7f55eSRobert Mustacchi int
ctf_add_function(ctf_file_t * fp,ulong_t idx,const ctf_funcinfo_t * fip,const ctf_id_t * argc)1975*f3e7f55eSRobert Mustacchi ctf_add_function(ctf_file_t *fp, ulong_t idx, const ctf_funcinfo_t *fip,
1976*f3e7f55eSRobert Mustacchi     const ctf_id_t *argc)
1977*f3e7f55eSRobert Mustacchi {
1978*f3e7f55eSRobert Mustacchi 	int i;
1979*f3e7f55eSRobert Mustacchi 	ctf_dsdef_t *dsd;
1980*f3e7f55eSRobert Mustacchi 	ctf_file_t *afp;
1981*f3e7f55eSRobert Mustacchi 	uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
1982*f3e7f55eSRobert Mustacchi 
1983*f3e7f55eSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
1984*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_RDONLY));
1985*f3e7f55eSRobert Mustacchi 
1986*f3e7f55eSRobert Mustacchi 	if (ctf_dsd_lookup(fp, idx) != NULL)
1987*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_CONFLICT));
1988*f3e7f55eSRobert Mustacchi 
1989*f3e7f55eSRobert Mustacchi 	if (symbase == NULL)
1990*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_STRTAB));
1991*f3e7f55eSRobert Mustacchi 
1992*f3e7f55eSRobert Mustacchi 	if (idx > fp->ctf_nsyms)
1993*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_NOTDATA));
1994*f3e7f55eSRobert Mustacchi 
1995*f3e7f55eSRobert Mustacchi 	if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
1996*f3e7f55eSRobert Mustacchi 		const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx;
1997*f3e7f55eSRobert Mustacchi 		if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)
1998*f3e7f55eSRobert Mustacchi 			return (ctf_set_errno(fp, ECTF_NOTFUNC));
1999*f3e7f55eSRobert Mustacchi 	} else {
2000*f3e7f55eSRobert Mustacchi 		const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx;
2001*f3e7f55eSRobert Mustacchi 		if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)
2002*f3e7f55eSRobert Mustacchi 			return (ctf_set_errno(fp, ECTF_NOTFUNC));
2003*f3e7f55eSRobert Mustacchi 	}
2004*f3e7f55eSRobert Mustacchi 
2005*f3e7f55eSRobert Mustacchi 	afp = fp;
2006*f3e7f55eSRobert Mustacchi 	if (ctf_lookup_by_id(&afp, fip->ctc_return) == NULL)
2007*f3e7f55eSRobert Mustacchi 		return (CTF_ERR); /* errno is set for us */
2008*f3e7f55eSRobert Mustacchi 
2009*f3e7f55eSRobert Mustacchi 	for (i = 0; i < fip->ctc_argc; i++) {
2010*f3e7f55eSRobert Mustacchi 		afp = fp;
2011*f3e7f55eSRobert Mustacchi 		if (ctf_lookup_by_id(&afp, argc[i]) == NULL)
2012*f3e7f55eSRobert Mustacchi 			return (CTF_ERR); /* errno is set for us */
2013*f3e7f55eSRobert Mustacchi 	}
2014*f3e7f55eSRobert Mustacchi 
2015*f3e7f55eSRobert Mustacchi 	dsd = ctf_alloc(sizeof (ctf_dsdef_t));
2016*f3e7f55eSRobert Mustacchi 	if (dsd == NULL)
2017*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ENOMEM));
2018*f3e7f55eSRobert Mustacchi 	dsd->dsd_nargs = fip->ctc_argc;
2019*f3e7f55eSRobert Mustacchi 	if (fip->ctc_flags & CTF_FUNC_VARARG)
2020*f3e7f55eSRobert Mustacchi 		dsd->dsd_nargs++;
2021*f3e7f55eSRobert Mustacchi 	if (dsd->dsd_nargs != 0) {
2022*f3e7f55eSRobert Mustacchi 		dsd->dsd_argc = ctf_alloc(sizeof (ctf_id_t) * dsd->dsd_nargs);
2023*f3e7f55eSRobert Mustacchi 		if (dsd->dsd_argc == NULL) {
2024*f3e7f55eSRobert Mustacchi 			ctf_free(dsd, sizeof (ctf_dsdef_t));
2025*f3e7f55eSRobert Mustacchi 			return (ctf_set_errno(fp, ENOMEM));
2026*f3e7f55eSRobert Mustacchi 		}
2027*f3e7f55eSRobert Mustacchi 		bcopy(argc, dsd->dsd_argc, sizeof (ctf_id_t) * fip->ctc_argc);
2028*f3e7f55eSRobert Mustacchi 		if (fip->ctc_flags & CTF_FUNC_VARARG)
2029*f3e7f55eSRobert Mustacchi 			dsd->dsd_argc[fip->ctc_argc] = 0;
2030*f3e7f55eSRobert Mustacchi 	}
2031*f3e7f55eSRobert Mustacchi 	dsd->dsd_symidx = idx;
2032*f3e7f55eSRobert Mustacchi 	dsd->dsd_tid = fip->ctc_return;
2033*f3e7f55eSRobert Mustacchi 
2034*f3e7f55eSRobert Mustacchi 	ctf_dsd_insert(fp, dsd);
2035*f3e7f55eSRobert Mustacchi 	fp->ctf_flags |= LCTF_DIRTY;
2036*f3e7f55eSRobert Mustacchi 
2037*f3e7f55eSRobert Mustacchi 	return (0);
2038*f3e7f55eSRobert Mustacchi }
2039*f3e7f55eSRobert Mustacchi 
2040*f3e7f55eSRobert Mustacchi int
ctf_add_object(ctf_file_t * fp,ulong_t idx,ctf_id_t type)2041*f3e7f55eSRobert Mustacchi ctf_add_object(ctf_file_t *fp, ulong_t idx, ctf_id_t type)
2042*f3e7f55eSRobert Mustacchi {
2043*f3e7f55eSRobert Mustacchi 	ctf_dsdef_t *dsd;
2044*f3e7f55eSRobert Mustacchi 	ctf_file_t *afp;
2045*f3e7f55eSRobert Mustacchi 	uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
2046*f3e7f55eSRobert Mustacchi 
2047*f3e7f55eSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
2048*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_RDONLY));
2049*f3e7f55eSRobert Mustacchi 
2050*f3e7f55eSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
2051*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_RDONLY));
2052*f3e7f55eSRobert Mustacchi 
2053*f3e7f55eSRobert Mustacchi 	if (ctf_dsd_lookup(fp, idx) != NULL)
2054*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_CONFLICT));
2055*f3e7f55eSRobert Mustacchi 
2056*f3e7f55eSRobert Mustacchi 	if (symbase == NULL)
2057*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_STRTAB));
2058*f3e7f55eSRobert Mustacchi 
2059*f3e7f55eSRobert Mustacchi 	if (idx > fp->ctf_nsyms)
2060*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_NOTDATA));
2061*f3e7f55eSRobert Mustacchi 
2062*f3e7f55eSRobert Mustacchi 	if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
2063*f3e7f55eSRobert Mustacchi 		const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx;
2064*f3e7f55eSRobert Mustacchi 		if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT)
2065*f3e7f55eSRobert Mustacchi 			return (ctf_set_errno(fp, ECTF_NOTDATA));
2066*f3e7f55eSRobert Mustacchi 	} else {
2067*f3e7f55eSRobert Mustacchi 		const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx;
2068*f3e7f55eSRobert Mustacchi 		if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT)
2069*f3e7f55eSRobert Mustacchi 			return (ctf_set_errno(fp, ECTF_NOTDATA));
2070*f3e7f55eSRobert Mustacchi 	}
2071*f3e7f55eSRobert Mustacchi 
2072*f3e7f55eSRobert Mustacchi 	afp = fp;
2073*f3e7f55eSRobert Mustacchi 	if (ctf_lookup_by_id(&afp, type) == NULL)
2074*f3e7f55eSRobert Mustacchi 		return (CTF_ERR); /* errno is set for us */
2075*f3e7f55eSRobert Mustacchi 
2076*f3e7f55eSRobert Mustacchi 	dsd = ctf_alloc(sizeof (ctf_dsdef_t));
2077*f3e7f55eSRobert Mustacchi 	if (dsd == NULL)
2078*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ENOMEM));
2079*f3e7f55eSRobert Mustacchi 	dsd->dsd_symidx = idx;
2080*f3e7f55eSRobert Mustacchi 	dsd->dsd_tid = type;
2081*f3e7f55eSRobert Mustacchi 	dsd->dsd_argc = NULL;
2082*f3e7f55eSRobert Mustacchi 
2083*f3e7f55eSRobert Mustacchi 	ctf_dsd_insert(fp, dsd);
2084*f3e7f55eSRobert Mustacchi 	fp->ctf_flags |= LCTF_DIRTY;
2085*f3e7f55eSRobert Mustacchi 
2086*f3e7f55eSRobert Mustacchi 	return (0);
2087*f3e7f55eSRobert Mustacchi }
2088*f3e7f55eSRobert Mustacchi 
2089*f3e7f55eSRobert Mustacchi void
ctf_dataptr(ctf_file_t * fp,const void ** addrp,size_t * sizep)2090*f3e7f55eSRobert Mustacchi ctf_dataptr(ctf_file_t *fp, const void **addrp, size_t *sizep)
2091*f3e7f55eSRobert Mustacchi {
2092*f3e7f55eSRobert Mustacchi 	if (addrp != NULL)
2093*f3e7f55eSRobert Mustacchi 		*addrp = fp->ctf_base;
2094*f3e7f55eSRobert Mustacchi 	if (sizep != NULL)
2095*f3e7f55eSRobert Mustacchi 		*sizep = fp->ctf_size;
2096*f3e7f55eSRobert Mustacchi }
2097*f3e7f55eSRobert Mustacchi 
2098*f3e7f55eSRobert Mustacchi int
ctf_add_label(ctf_file_t * fp,const char * name,ctf_id_t type,uint_t position)2099*f3e7f55eSRobert Mustacchi ctf_add_label(ctf_file_t *fp, const char *name, ctf_id_t type, uint_t position)
2100*f3e7f55eSRobert Mustacchi {
2101*f3e7f55eSRobert Mustacchi 	ctf_file_t *fpd;
2102*f3e7f55eSRobert Mustacchi 	ctf_dldef_t *dld;
2103*f3e7f55eSRobert Mustacchi 
2104*f3e7f55eSRobert Mustacchi 	if (name == NULL)
2105*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, EINVAL));
2106*f3e7f55eSRobert Mustacchi 
2107*f3e7f55eSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
2108*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_RDONLY));
2109*f3e7f55eSRobert Mustacchi 
2110*f3e7f55eSRobert Mustacchi 	fpd = fp;
2111*f3e7f55eSRobert Mustacchi 	if (type != 0 && ctf_lookup_by_id(&fpd, type) == NULL)
2112*f3e7f55eSRobert Mustacchi 		return (CTF_ERR); /* errno is set for us */
2113*f3e7f55eSRobert Mustacchi 
2114*f3e7f55eSRobert Mustacchi 	if (type != 0 && (fp->ctf_flags & LCTF_CHILD) &&
2115*f3e7f55eSRobert Mustacchi 	    CTF_TYPE_ISPARENT(type))
2116*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_NOPARENT));
2117*f3e7f55eSRobert Mustacchi 
2118*f3e7f55eSRobert Mustacchi 	if (ctf_dld_lookup(fp, name) != NULL)
2119*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_LABELEXISTS));
2120*f3e7f55eSRobert Mustacchi 
2121*f3e7f55eSRobert Mustacchi 	if ((dld = ctf_alloc(sizeof (ctf_dldef_t))) == NULL)
2122*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, EAGAIN));
2123*f3e7f55eSRobert Mustacchi 
2124*f3e7f55eSRobert Mustacchi 	if ((dld->dld_name = ctf_strdup(name)) == NULL) {
2125*f3e7f55eSRobert Mustacchi 		ctf_free(dld, sizeof (ctf_dldef_t));
2126*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, EAGAIN));
2127*f3e7f55eSRobert Mustacchi 	}
2128*f3e7f55eSRobert Mustacchi 
2129*f3e7f55eSRobert Mustacchi 	dld->dld_type = type;
2130*f3e7f55eSRobert Mustacchi 	fp->ctf_dtstrlen += strlen(name) + 1;
2131*f3e7f55eSRobert Mustacchi 	ctf_dld_insert(fp, dld, position);
2132*f3e7f55eSRobert Mustacchi 	fp->ctf_flags |= LCTF_DIRTY;
2133*f3e7f55eSRobert Mustacchi 
2134*f3e7f55eSRobert Mustacchi 	return (0);
2135*f3e7f55eSRobert Mustacchi }
2136*f3e7f55eSRobert Mustacchi 
2137*f3e7f55eSRobert Mustacchi /*
2138*f3e7f55eSRobert Mustacchi  * Update the size of a structure or union. Note that we don't allow this to
2139*f3e7f55eSRobert Mustacchi  * shrink the size of a struct or union, only to increase it. This is useful for
2140*f3e7f55eSRobert Mustacchi  * cases when you have a structure whose actual size is larger than the sum of
2141*f3e7f55eSRobert Mustacchi  * its members due to padding for natural alignment.
2142*f3e7f55eSRobert Mustacchi  */
2143*f3e7f55eSRobert Mustacchi int
ctf_set_size(ctf_file_t * fp,ctf_id_t id,const ulong_t newsz)2144*f3e7f55eSRobert Mustacchi ctf_set_size(ctf_file_t *fp, ctf_id_t id, const ulong_t newsz)
2145*f3e7f55eSRobert Mustacchi {
2146*f3e7f55eSRobert Mustacchi 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2147*f3e7f55eSRobert Mustacchi 	uint_t kind;
2148*f3e7f55eSRobert Mustacchi 	size_t oldsz;
2149*f3e7f55eSRobert Mustacchi 
2150*f3e7f55eSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
2151*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_RDONLY));
2152*f3e7f55eSRobert Mustacchi 
2153*f3e7f55eSRobert Mustacchi 	if (dtd == NULL)
2154*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
2155*f3e7f55eSRobert Mustacchi 
2156*f3e7f55eSRobert Mustacchi 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2157*f3e7f55eSRobert Mustacchi 
2158*f3e7f55eSRobert Mustacchi 	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
2159*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_NOTSOU));
2160*f3e7f55eSRobert Mustacchi 
2161*f3e7f55eSRobert Mustacchi 	if ((oldsz = dtd->dtd_data.ctt_size) == CTF_LSIZE_SENT)
2162*f3e7f55eSRobert Mustacchi 		oldsz = CTF_TYPE_LSIZE(&dtd->dtd_data);
2163*f3e7f55eSRobert Mustacchi 
2164*f3e7f55eSRobert Mustacchi 	if (newsz < oldsz)
2165*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, EINVAL));
2166*f3e7f55eSRobert Mustacchi 
2167*f3e7f55eSRobert Mustacchi 	if (newsz > CTF_MAX_SIZE) {
2168*f3e7f55eSRobert Mustacchi 		dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
2169*f3e7f55eSRobert Mustacchi 		dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(newsz);
2170*f3e7f55eSRobert Mustacchi 		dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(newsz);
2171*f3e7f55eSRobert Mustacchi 	} else {
2172*f3e7f55eSRobert Mustacchi 		dtd->dtd_data.ctt_size = (ushort_t)newsz;
2173*f3e7f55eSRobert Mustacchi 	}
2174*f3e7f55eSRobert Mustacchi 
2175*f3e7f55eSRobert Mustacchi 	fp->ctf_flags |= LCTF_DIRTY;
2176*f3e7f55eSRobert Mustacchi 	return (0);
2177*f3e7f55eSRobert Mustacchi }
2178*f3e7f55eSRobert Mustacchi 
2179*f3e7f55eSRobert Mustacchi int
ctf_set_root(ctf_file_t * fp,ctf_id_t id,const boolean_t vis)2180*f3e7f55eSRobert Mustacchi ctf_set_root(ctf_file_t *fp, ctf_id_t id, const boolean_t vis)
2181*f3e7f55eSRobert Mustacchi {
2182*f3e7f55eSRobert Mustacchi 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2183*f3e7f55eSRobert Mustacchi 	uint_t kind, vlen;
2184*f3e7f55eSRobert Mustacchi 
2185*f3e7f55eSRobert Mustacchi 	if (!(fp->ctf_flags & LCTF_RDWR))
2186*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_RDONLY));
2187*f3e7f55eSRobert Mustacchi 
2188*f3e7f55eSRobert Mustacchi 	if (dtd == NULL)
2189*f3e7f55eSRobert Mustacchi 		return (ctf_set_errno(fp, ECTF_BADID));
2190*f3e7f55eSRobert Mustacchi 
2191*f3e7f55eSRobert Mustacchi 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2192*f3e7f55eSRobert Mustacchi 	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
2193*f3e7f55eSRobert Mustacchi 
2194*f3e7f55eSRobert Mustacchi 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, vis, vlen);
2195*f3e7f55eSRobert Mustacchi 	return (0);
2196*f3e7f55eSRobert Mustacchi }
2197