xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_dof.c (revision 1880:1982c224f2bd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51710Sahl  * Common Development and Distribution License (the "License").
61710Sahl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211710Sahl 
220Sstevel@tonic-gate /*
231239Sahl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <strings.h>
330Sstevel@tonic-gate #include <alloca.h>
340Sstevel@tonic-gate #include <assert.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <dt_impl.h>
400Sstevel@tonic-gate #include <dt_strtab.h>
41265Smws #include <dt_program.h>
420Sstevel@tonic-gate #include <dt_provider.h>
43265Smws #include <dt_xlator.h>
440Sstevel@tonic-gate #include <dt_dof.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate void
470Sstevel@tonic-gate dt_dof_init(dtrace_hdl_t *dtp)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	dt_dof_t *ddo = &dtp->dt_dof;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	ddo->ddo_hdl = dtp;
520Sstevel@tonic-gate 	ddo->ddo_nsecs = 0;
530Sstevel@tonic-gate 	ddo->ddo_strsec = DOF_SECIDX_NONE;
54265Smws 	ddo->ddo_xlimport = NULL;
55265Smws 	ddo->ddo_xlexport = NULL;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	dt_buf_create(dtp, &ddo->ddo_secs, "section headers", 0);
580Sstevel@tonic-gate 	dt_buf_create(dtp, &ddo->ddo_strs, "string table", 0);
590Sstevel@tonic-gate 	dt_buf_create(dtp, &ddo->ddo_ldata, "loadable data", 0);
600Sstevel@tonic-gate 	dt_buf_create(dtp, &ddo->ddo_udata, "unloadable data", 0);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	dt_buf_create(dtp, &ddo->ddo_probes, "probe data", 0);
630Sstevel@tonic-gate 	dt_buf_create(dtp, &ddo->ddo_args, "probe args", 0);
640Sstevel@tonic-gate 	dt_buf_create(dtp, &ddo->ddo_offs, "probe offs", 0);
651710Sahl 	dt_buf_create(dtp, &ddo->ddo_enoffs, "probe is-enabled offs", 0);
660Sstevel@tonic-gate 	dt_buf_create(dtp, &ddo->ddo_rels, "probe rels", 0);
67265Smws 
68265Smws 	dt_buf_create(dtp, &ddo->ddo_xlms, "xlate members", 0);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate void
720Sstevel@tonic-gate dt_dof_fini(dtrace_hdl_t *dtp)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	dt_dof_t *ddo = &dtp->dt_dof;
750Sstevel@tonic-gate 
76265Smws 	dt_free(dtp, ddo->ddo_xlimport);
77265Smws 	dt_free(dtp, ddo->ddo_xlexport);
78265Smws 
790Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_secs);
800Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_strs);
810Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_ldata);
820Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_udata);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_probes);
850Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_args);
860Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_offs);
871710Sahl 	dt_buf_destroy(dtp, &ddo->ddo_enoffs);
880Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_rels);
89265Smws 
90265Smws 	dt_buf_destroy(dtp, &ddo->ddo_xlms);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
93265Smws static int
94265Smws dt_dof_reset(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	dt_dof_t *ddo = &dtp->dt_dof;
97265Smws 	uint_t i, nx = dtp->dt_xlatorid;
98265Smws 
99265Smws 	assert(ddo->ddo_hdl == dtp);
100265Smws 	ddo->ddo_pgp = pgp;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	ddo->ddo_nsecs = 0;
1030Sstevel@tonic-gate 	ddo->ddo_strsec = DOF_SECIDX_NONE;
1040Sstevel@tonic-gate 
105265Smws 	dt_free(dtp, ddo->ddo_xlimport);
106265Smws 	dt_free(dtp, ddo->ddo_xlexport);
107265Smws 
108265Smws 	ddo->ddo_xlimport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
109265Smws 	ddo->ddo_xlexport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
110265Smws 
111265Smws 	if (nx != 0 && (ddo->ddo_xlimport == NULL || ddo->ddo_xlexport == NULL))
112265Smws 		return (-1); /* errno is set for us */
113265Smws 
114265Smws 	for (i = 0; i < nx; i++) {
115265Smws 		ddo->ddo_xlimport[i] = DOF_SECIDX_NONE;
116265Smws 		ddo->ddo_xlexport[i] = DOF_SECIDX_NONE;
117265Smws 	}
118265Smws 
1190Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_secs);
1200Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_strs);
1210Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_ldata);
1220Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_udata);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_probes);
1250Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_args);
1260Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_offs);
1271710Sahl 	dt_buf_reset(dtp, &ddo->ddo_enoffs);
1280Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_rels);
129265Smws 
130265Smws 	dt_buf_reset(dtp, &ddo->ddo_xlms);
131265Smws 	return (0);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Add a loadable DOF section to the file using the specified data buffer and
1360Sstevel@tonic-gate  * the specified DOF section attributes.  DOF_SECF_LOAD must be set in flags.
1370Sstevel@tonic-gate  * If 'data' is NULL, the caller is responsible for manipulating the ldata buf.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate static dof_secidx_t
1400Sstevel@tonic-gate dof_add_lsect(dt_dof_t *ddo, const void *data, uint32_t type,
1410Sstevel@tonic-gate     uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
1440Sstevel@tonic-gate 	dof_sec_t s;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	s.dofs_type = type;
1470Sstevel@tonic-gate 	s.dofs_align = align;
1480Sstevel@tonic-gate 	s.dofs_flags = flags | DOF_SECF_LOAD;
1490Sstevel@tonic-gate 	s.dofs_entsize = entsize;
1500Sstevel@tonic-gate 	s.dofs_offset = dt_buf_offset(&ddo->ddo_ldata, align);
1510Sstevel@tonic-gate 	s.dofs_size = size;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (data != NULL)
1560Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_ldata, data, size, align);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	return (ddo->ddo_nsecs++);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate  * Add an unloadable DOF section to the file using the specified data buffer
1630Sstevel@tonic-gate  * and DOF section attributes.  DOF_SECF_LOAD must *not* be set in flags.
1640Sstevel@tonic-gate  * If 'data' is NULL, the caller is responsible for manipulating the udata buf.
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate static dof_secidx_t
1670Sstevel@tonic-gate dof_add_usect(dt_dof_t *ddo, const void *data, uint32_t type,
1680Sstevel@tonic-gate     uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
1710Sstevel@tonic-gate 	dof_sec_t s;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	s.dofs_type = type;
1740Sstevel@tonic-gate 	s.dofs_align = align;
1750Sstevel@tonic-gate 	s.dofs_flags = flags & ~DOF_SECF_LOAD;
1760Sstevel@tonic-gate 	s.dofs_entsize = entsize;
1770Sstevel@tonic-gate 	s.dofs_offset = dt_buf_offset(&ddo->ddo_udata, align);
1780Sstevel@tonic-gate 	s.dofs_size = size;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (data != NULL)
1830Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_udata, data, size, align);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	return (ddo->ddo_nsecs++);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * Add a string to the global string table associated with the DOF.  The offset
1900Sstevel@tonic-gate  * of the string is returned as an index into the string table.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate static dof_stridx_t
1930Sstevel@tonic-gate dof_add_string(dt_dof_t *ddo, const char *s)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	dt_buf_t *bp = &ddo->ddo_strs;
1960Sstevel@tonic-gate 	dof_stridx_t i = dt_buf_len(bp);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (i != 0 && (s == NULL || *s == '\0'))
1990Sstevel@tonic-gate 		return (0); /* string table has \0 at offset 0 */
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	dt_buf_write(ddo->ddo_hdl, bp, s, strlen(s) + 1, sizeof (char));
2020Sstevel@tonic-gate 	return (i);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate static dof_attr_t
2060Sstevel@tonic-gate dof_attr(const dtrace_attribute_t *ap)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	return (DOF_ATTR(ap->dtat_name, ap->dtat_data, ap->dtat_class));
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate static dof_secidx_t
2120Sstevel@tonic-gate dof_add_difo(dt_dof_t *ddo, const dtrace_difo_t *dp)
2130Sstevel@tonic-gate {
214265Smws 	dof_secidx_t dsecs[5]; /* enough for all possible DIFO sections */
2150Sstevel@tonic-gate 	uint_t nsecs = 0;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	dof_difohdr_t *dofd;
2180Sstevel@tonic-gate 	dof_relohdr_t dofr;
2190Sstevel@tonic-gate 	dof_secidx_t relsec;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	dof_secidx_t strsec = DOF_SECIDX_NONE;
2220Sstevel@tonic-gate 	dof_secidx_t intsec = DOF_SECIDX_NONE;
2230Sstevel@tonic-gate 	dof_secidx_t hdrsec = DOF_SECIDX_NONE;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (dp->dtdo_buf != NULL) {
2260Sstevel@tonic-gate 		dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_buf,
2270Sstevel@tonic-gate 		    DOF_SECT_DIF, sizeof (dif_instr_t), 0,
2280Sstevel@tonic-gate 		    sizeof (dif_instr_t), sizeof (dif_instr_t) * dp->dtdo_len);
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (dp->dtdo_inttab != NULL) {
2320Sstevel@tonic-gate 		dsecs[nsecs++] = intsec = dof_add_lsect(ddo, dp->dtdo_inttab,
2330Sstevel@tonic-gate 		    DOF_SECT_INTTAB, sizeof (uint64_t), 0,
2340Sstevel@tonic-gate 		    sizeof (uint64_t), sizeof (uint64_t) * dp->dtdo_intlen);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (dp->dtdo_strtab != NULL) {
2380Sstevel@tonic-gate 		dsecs[nsecs++] = strsec = dof_add_lsect(ddo, dp->dtdo_strtab,
2390Sstevel@tonic-gate 		    DOF_SECT_STRTAB, sizeof (char), 0, 0, dp->dtdo_strlen);
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (dp->dtdo_vartab != NULL) {
2430Sstevel@tonic-gate 		dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_vartab,
2440Sstevel@tonic-gate 		    DOF_SECT_VARTAB, sizeof (uint_t), 0, sizeof (dtrace_difv_t),
2450Sstevel@tonic-gate 		    sizeof (dtrace_difv_t) * dp->dtdo_varlen);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
248265Smws 	if (dp->dtdo_xlmtab != NULL) {
249265Smws 		dof_xlref_t *xlt, *xlp;
250265Smws 		dt_node_t **pnp;
251265Smws 
252265Smws 		xlt = alloca(sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
253265Smws 		pnp = dp->dtdo_xlmtab;
254265Smws 
255265Smws 		/*
256265Smws 		 * dtdo_xlmtab contains pointers to the translator members.
257265Smws 		 * The translator itself is in sect ddo_xlimport[dxp->dx_id].
258265Smws 		 * The XLMEMBERS entries are in order by their dn_membid, so
259265Smws 		 * the member section offset is the population count of bits
260265Smws 		 * in ddo_pgp->dp_xlrefs[] up to and not including dn_membid.
261265Smws 		 */
262265Smws 		for (xlp = xlt; xlp < xlt + dp->dtdo_xlmlen; xlp++) {
263265Smws 			dt_node_t *dnp = *pnp++;
264265Smws 			dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
265265Smws 
266265Smws 			xlp->dofxr_xlator = ddo->ddo_xlimport[dxp->dx_id];
267265Smws 			xlp->dofxr_member = dt_popcb(
268265Smws 			    ddo->ddo_pgp->dp_xrefs[dxp->dx_id], dnp->dn_membid);
269265Smws 			xlp->dofxr_argn = (uint32_t)dxp->dx_arg;
270265Smws 		}
271265Smws 
272265Smws 		dsecs[nsecs++] = dof_add_lsect(ddo, xlt, DOF_SECT_XLTAB,
273265Smws 		    sizeof (dof_secidx_t), 0, sizeof (dof_xlref_t),
274265Smws 		    sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
275265Smws 	}
276265Smws 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * Copy the return type and the array of section indices that form the
2790Sstevel@tonic-gate 	 * DIFO into a single dof_difohdr_t and then add DOF_SECT_DIFOHDR.
2800Sstevel@tonic-gate 	 */
2810Sstevel@tonic-gate 	assert(nsecs <= sizeof (dsecs) / sizeof (dsecs[0]));
2820Sstevel@tonic-gate 	dofd = alloca(sizeof (dtrace_diftype_t) + sizeof (dsecs));
2830Sstevel@tonic-gate 	bcopy(&dp->dtdo_rtype, &dofd->dofd_rtype, sizeof (dtrace_diftype_t));
2840Sstevel@tonic-gate 	bcopy(dsecs, &dofd->dofd_links, sizeof (dof_secidx_t) * nsecs);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	hdrsec = dof_add_lsect(ddo, dofd, DOF_SECT_DIFOHDR,
2870Sstevel@tonic-gate 	    sizeof (dof_secidx_t), 0, 0,
2880Sstevel@tonic-gate 	    sizeof (dtrace_diftype_t) + sizeof (dof_secidx_t) * nsecs);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/*
2910Sstevel@tonic-gate 	 * Add any other sections related to dtrace_difo_t.  These are not
2920Sstevel@tonic-gate 	 * referenced in dof_difohdr_t because they are not used by emulation.
2930Sstevel@tonic-gate 	 */
2940Sstevel@tonic-gate 	if (dp->dtdo_kreltab != NULL) {
2950Sstevel@tonic-gate 		relsec = dof_add_lsect(ddo, dp->dtdo_kreltab, DOF_SECT_RELTAB,
2960Sstevel@tonic-gate 		    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
2970Sstevel@tonic-gate 		    sizeof (dof_relodesc_t) * dp->dtdo_krelen);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		/*
3000Sstevel@tonic-gate 		 * This code assumes the target of all relocations is the
3010Sstevel@tonic-gate 		 * integer table 'intsec' (DOF_SECT_INTTAB).  If other sections
3020Sstevel@tonic-gate 		 * need relocation in the future this will need to change.
3030Sstevel@tonic-gate 		 */
3040Sstevel@tonic-gate 		dofr.dofr_strtab = strsec;
3050Sstevel@tonic-gate 		dofr.dofr_relsec = relsec;
3060Sstevel@tonic-gate 		dofr.dofr_tgtsec = intsec;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		(void) dof_add_lsect(ddo, &dofr, DOF_SECT_KRELHDR,
3090Sstevel@tonic-gate 		    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if (dp->dtdo_ureltab != NULL) {
3130Sstevel@tonic-gate 		relsec = dof_add_lsect(ddo, dp->dtdo_ureltab, DOF_SECT_RELTAB,
3140Sstevel@tonic-gate 		    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
3150Sstevel@tonic-gate 		    sizeof (dof_relodesc_t) * dp->dtdo_urelen);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		/*
3180Sstevel@tonic-gate 		 * This code assumes the target of all relocations is the
3190Sstevel@tonic-gate 		 * integer table 'intsec' (DOF_SECT_INTTAB).  If other sections
3200Sstevel@tonic-gate 		 * need relocation in the future this will need to change.
3210Sstevel@tonic-gate 		 */
3220Sstevel@tonic-gate 		dofr.dofr_strtab = strsec;
3230Sstevel@tonic-gate 		dofr.dofr_relsec = relsec;
3240Sstevel@tonic-gate 		dofr.dofr_tgtsec = intsec;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 		(void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
3270Sstevel@tonic-gate 		    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	return (hdrsec);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
333265Smws static void
334265Smws dof_add_translator(dt_dof_t *ddo, const dt_xlator_t *dxp, uint_t type)
335265Smws {
336265Smws 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
337265Smws 	dof_xlmember_t dofxm;
338265Smws 	dof_xlator_t dofxl;
339265Smws 	dof_secidx_t *xst;
340265Smws 
341265Smws 	char buf[DT_TYPE_NAMELEN];
342265Smws 	dt_node_t *dnp;
343265Smws 	uint_t i = 0;
344265Smws 
345265Smws 	assert(type == DOF_SECT_XLIMPORT || type == DOF_SECT_XLEXPORT);
346265Smws 	xst = type == DOF_SECT_XLIMPORT ? ddo->ddo_xlimport : ddo->ddo_xlexport;
347265Smws 
348265Smws 	if (xst[dxp->dx_id] != DOF_SECIDX_NONE)
349265Smws 		return; /* translator has already been emitted */
350265Smws 
351265Smws 	dt_buf_reset(dtp, &ddo->ddo_xlms);
352265Smws 
353265Smws 	/*
354265Smws 	 * Generate an array of dof_xlmember_t's into ddo_xlms.  If we are
355265Smws 	 * importing the translator, add only those members referenced by the
356265Smws 	 * program and set the dofxm_difo reference of each member to NONE.  If
357265Smws 	 * we're exporting the translator, add all members and a DIFO for each.
358265Smws 	 */
359265Smws 	for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list, i++) {
360265Smws 		if (type == DOF_SECT_XLIMPORT) {
361265Smws 			if (!BT_TEST(ddo->ddo_pgp->dp_xrefs[dxp->dx_id], i))
362265Smws 				continue; /* member is not referenced */
363265Smws 			dofxm.dofxm_difo = DOF_SECIDX_NONE;
364265Smws 		} else {
365265Smws 			dofxm.dofxm_difo = dof_add_difo(ddo,
366265Smws 			    dxp->dx_membdif[dnp->dn_membid]);
367265Smws 		}
368265Smws 
369265Smws 		dofxm.dofxm_name = dof_add_string(ddo, dnp->dn_membname);
370265Smws 		dt_node_diftype(dtp, dnp, &dofxm.dofxm_type);
371265Smws 
372265Smws 		dt_buf_write(dtp, &ddo->ddo_xlms,
373265Smws 		    &dofxm, sizeof (dofxm), sizeof (uint32_t));
374265Smws 	}
375265Smws 
376265Smws 	dofxl.dofxl_members = dof_add_lsect(ddo, NULL, DOF_SECT_XLMEMBERS,
377265Smws 	    sizeof (uint32_t), 0, sizeof (dofxm), dt_buf_len(&ddo->ddo_xlms));
378265Smws 
379265Smws 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_xlms, sizeof (uint32_t));
380265Smws 
381265Smws 	dofxl.dofxl_strtab = ddo->ddo_strsec;
382265Smws 	dofxl.dofxl_argv = dof_add_string(ddo, ctf_type_name(
383265Smws 	    dxp->dx_src_ctfp, dxp->dx_src_type, buf, sizeof (buf)));
384265Smws 	dofxl.dofxl_argc = 1;
385265Smws 	dofxl.dofxl_type = dof_add_string(ddo, ctf_type_name(
386265Smws 	    dxp->dx_dst_ctfp, dxp->dx_dst_type, buf, sizeof (buf)));
387265Smws 	dofxl.dofxl_attr = dof_attr(&dxp->dx_souid.di_attr);
388265Smws 
389265Smws 	xst[dxp->dx_id] = dof_add_lsect(ddo, &dofxl, type,
390265Smws 	    sizeof (uint32_t), 0, 0, sizeof (dofxl));
391265Smws }
392265Smws 
3930Sstevel@tonic-gate /*ARGSUSED*/
3940Sstevel@tonic-gate static int
3950Sstevel@tonic-gate dof_add_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate 	dt_dof_t *ddo = data;
3980Sstevel@tonic-gate 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
3990Sstevel@tonic-gate 	dt_probe_t *prp = idp->di_data;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	dof_probe_t dofpr;
4020Sstevel@tonic-gate 	dof_relodesc_t dofr;
4030Sstevel@tonic-gate 	dt_probe_instance_t *pip;
4040Sstevel@tonic-gate 	dt_node_t *dnp;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	char buf[DT_TYPE_NAMELEN];
4070Sstevel@tonic-gate 	uint_t i;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	dofpr.dofpr_addr = 0;
4100Sstevel@tonic-gate 	dofpr.dofpr_name = dof_add_string(ddo, prp->pr_name);
4110Sstevel@tonic-gate 	dofpr.dofpr_nargv = dt_buf_len(&ddo->ddo_strs);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	for (dnp = prp->pr_nargs; dnp != NULL; dnp = dnp->dn_list) {
4140Sstevel@tonic-gate 		(void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
4150Sstevel@tonic-gate 		    dnp->dn_type, buf, sizeof (buf)));
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	dofpr.dofpr_xargv = dt_buf_len(&ddo->ddo_strs);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	for (dnp = prp->pr_xargs; dnp != NULL; dnp = dnp->dn_list) {
4210Sstevel@tonic-gate 		(void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
4220Sstevel@tonic-gate 		    dnp->dn_type, buf, sizeof (buf)));
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	dofpr.dofpr_argidx = dt_buf_len(&ddo->ddo_args) / sizeof (uint8_t);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	for (i = 0; i < prp->pr_xargc; i++) {
4280Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_args, &prp->pr_mapping[i],
4290Sstevel@tonic-gate 		    sizeof (uint8_t), sizeof (uint8_t));
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	dofpr.dofpr_nargc = prp->pr_nargc;
4330Sstevel@tonic-gate 	dofpr.dofpr_xargc = prp->pr_xargc;
4341710Sahl 	dofpr.dofpr_pad1 = 0;
4351710Sahl 	dofpr.dofpr_pad2 = 0;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
4381710Sahl 		dt_dprintf("adding probe for %s:%s\n", pip->pi_fname,
4391710Sahl 		    prp->pr_name);
4401710Sahl 
4410Sstevel@tonic-gate 		dofpr.dofpr_func = dof_add_string(ddo, pip->pi_fname);
4421710Sahl 
443*1880Sahl 		/*
444*1880Sahl 		 * There should be one probe offset or is-enabled probe offset
445*1880Sahl 		 * or else this probe instance won't have been created. The
446*1880Sahl 		 * kernel will reject DOF which has a probe with no offsets.
447*1880Sahl 		 */
448*1880Sahl 		assert(pip->pi_noffs + pip->pi_nenoffs > 0);
4491710Sahl 
4500Sstevel@tonic-gate 		dofpr.dofpr_offidx =
4510Sstevel@tonic-gate 		    dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t);
4520Sstevel@tonic-gate 		dofpr.dofpr_noffs = pip->pi_noffs;
4531710Sahl 		dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs,
4541710Sahl 		    pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
4550Sstevel@tonic-gate 
4561710Sahl 		dofpr.dofpr_enoffidx =
4571710Sahl 		    dt_buf_len(&ddo->ddo_enoffs) / sizeof (uint32_t);
4581710Sahl 		dofpr.dofpr_nenoffs = pip->pi_nenoffs;
4591710Sahl 		dt_buf_write(dtp, &ddo->ddo_enoffs, pip->pi_enoffs,
4600Sstevel@tonic-gate 		    pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
4610Sstevel@tonic-gate 
4621239Sahl 		/*
4631239Sahl 		 * If pi_rname isn't set, the relocation will be against the
4641239Sahl 		 * function name. If it is, the relocation will be against
4651239Sahl 		 * pi_rname. This will be used if the function is scoped
4661239Sahl 		 * locally so an alternate symbol is added for the purpose
4671239Sahl 		 * of this relocation.
4681239Sahl 		 */
4691239Sahl 		if (pip->pi_rname[0] == '\0')
4701239Sahl 			dofr.dofr_name = dofpr.dofpr_func;
4711239Sahl 		else
4721239Sahl 			dofr.dofr_name = dof_add_string(ddo, pip->pi_rname);
4730Sstevel@tonic-gate 		dofr.dofr_type = DOF_RELO_SETX;
4740Sstevel@tonic-gate 		dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes);
4750Sstevel@tonic-gate 		dofr.dofr_data = 0;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_rels, &dofr,
4780Sstevel@tonic-gate 		    sizeof (dofr), sizeof (uint64_t));
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_probes, &dofpr,
4810Sstevel@tonic-gate 		    sizeof (dofpr), sizeof (uint64_t));
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	return (0);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate static void
4880Sstevel@tonic-gate dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
4910Sstevel@tonic-gate 	dof_provider_t dofpv;
4920Sstevel@tonic-gate 	dof_relohdr_t dofr;
493265Smws 	dof_secidx_t *dofs;
494265Smws 	ulong_t xr, nxr;
495*1880Sahl 	size_t sz;
496265Smws 	id_t i;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	if (pvp->pv_flags & DT_PROVIDER_IMPL)
4990Sstevel@tonic-gate 		return; /* ignore providers that are exported by dtrace(7D) */
5000Sstevel@tonic-gate 
501265Smws 	nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax);
502265Smws 	dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1));
503265Smws 	xr = 1; /* reserve dofs[0] for the provider itself */
504265Smws 
505265Smws 	/*
506265Smws 	 * For each translator referenced by the provider (pv_xrefs), emit an
507265Smws 	 * exported translator section for it if one hasn't been created yet.
508265Smws 	 */
509265Smws 	for (i = 0; i < pvp->pv_xrmax; i++) {
510265Smws 		if (BT_TEST(pvp->pv_xrefs, i) &&
511265Smws 		    dtp->dt_xlatemode == DT_XL_DYNAMIC) {
512265Smws 			dof_add_translator(ddo,
513265Smws 			    dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT);
514265Smws 			dofs[xr++] = ddo->ddo_xlexport[i];
515265Smws 		}
516265Smws 	}
517265Smws 
5180Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_probes);
5190Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_args);
5200Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_offs);
5211710Sahl 	dt_buf_reset(dtp, &ddo->ddo_enoffs);
5220Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_rels);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	(void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES,
5270Sstevel@tonic-gate 	    sizeof (uint64_t), 0, sizeof (dof_probe_t),
5280Sstevel@tonic-gate 	    dt_buf_len(&ddo->ddo_probes));
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	dt_buf_concat(dtp, &ddo->ddo_ldata,
5310Sstevel@tonic-gate 	    &ddo->ddo_probes, sizeof (uint64_t));
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS,
5340Sstevel@tonic-gate 	    sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args));
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t));
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS,
5390Sstevel@tonic-gate 	    sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs));
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t));
5420Sstevel@tonic-gate 
543*1880Sahl 	if ((sz = dt_buf_len(&ddo->ddo_enoffs)) != 0) {
544*1880Sahl 		dofpv.dofpv_prenoffs = dof_add_lsect(ddo, NULL,
545*1880Sahl 		    DOF_SECT_PRENOFFS, sizeof (uint_t), 0, sizeof (uint_t), sz);
546*1880Sahl 	} else {
547*1880Sahl 		dofpv.dofpv_prenoffs = DOF_SECT_NONE;
548*1880Sahl 	}
5491710Sahl 
5501710Sahl 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_enoffs, sizeof (uint_t));
5511710Sahl 
5520Sstevel@tonic-gate 	dofpv.dofpv_strtab = ddo->ddo_strsec;
5530Sstevel@tonic-gate 	dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider);
5560Sstevel@tonic-gate 	dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod);
5570Sstevel@tonic-gate 	dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func);
5580Sstevel@tonic-gate 	dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name);
5590Sstevel@tonic-gate 	dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args);
5600Sstevel@tonic-gate 
561265Smws 	dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER,
5620Sstevel@tonic-gate 	    sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t));
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	dofr.dofr_strtab = dofpv.dofpv_strtab;
5650Sstevel@tonic-gate 	dofr.dofr_tgtsec = dofpv.dofpv_probes;
5660Sstevel@tonic-gate 	dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB,
5670Sstevel@tonic-gate 	    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
5680Sstevel@tonic-gate 	    dt_buf_len(&ddo->ddo_rels));
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t));
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	(void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
5730Sstevel@tonic-gate 	    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
574265Smws 
575265Smws 	if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) {
576265Smws 		(void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT,
577265Smws 		    sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t),
578265Smws 		    sizeof (dof_secidx_t) * (nxr + 1));
579265Smws 	}
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate static int
5831710Sahl dof_hdr(dtrace_hdl_t *dtp, uint8_t dofversion, dof_hdr_t *hp)
5840Sstevel@tonic-gate {
5850Sstevel@tonic-gate 	/*
5860Sstevel@tonic-gate 	 * If our config values cannot fit in a uint8_t, we can't generate a
5870Sstevel@tonic-gate 	 * DOF header since the values won't fit.  This can only happen if the
5880Sstevel@tonic-gate 	 * user forcibly compiles a program with an artificial configuration.
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_difversion > UINT8_MAX ||
5910Sstevel@tonic-gate 	    dtp->dt_conf.dtc_difintregs > UINT8_MAX ||
5920Sstevel@tonic-gate 	    dtp->dt_conf.dtc_diftupregs > UINT8_MAX)
5930Sstevel@tonic-gate 		return (dt_set_errno(dtp, EOVERFLOW));
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	bzero(hp, sizeof (dof_hdr_t));
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
5980Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
5990Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
6000Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
6030Sstevel@tonic-gate 		hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64;
6040Sstevel@tonic-gate 	else
6050Sstevel@tonic-gate 		hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
6081710Sahl 	hp->dofh_ident[DOF_ID_VERSION] = dofversion;
6090Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion;
6100Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs;
6110Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	hp->dofh_hdrsize = sizeof (dof_hdr_t);
6140Sstevel@tonic-gate 	hp->dofh_secsize = sizeof (dof_sec_t);
6150Sstevel@tonic-gate 	hp->dofh_secoff = sizeof (dof_hdr_t);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	return (0);
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate void *
6210Sstevel@tonic-gate dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags)
6220Sstevel@tonic-gate {
6230Sstevel@tonic-gate 	dt_dof_t *ddo = &dtp->dt_dof;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	const dtrace_ecbdesc_t *edp, *last;
6260Sstevel@tonic-gate 	const dtrace_probedesc_t *pdp;
6270Sstevel@tonic-gate 	const dtrace_actdesc_t *ap;
6280Sstevel@tonic-gate 	const dt_stmt_t *stp;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	uint_t maxacts = 0;
6310Sstevel@tonic-gate 	uint_t maxfmt = 0;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	dt_provider_t *pvp;
634265Smws 	dt_xlator_t *dxp;
6350Sstevel@tonic-gate 	dof_actdesc_t *dofa;
6360Sstevel@tonic-gate 	dof_sec_t *sp;
6370Sstevel@tonic-gate 	size_t ssize, lsize;
6380Sstevel@tonic-gate 	dof_hdr_t h;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	dt_buf_t dof;
6410Sstevel@tonic-gate 	char *fmt;
6420Sstevel@tonic-gate 	uint_t i;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	if (flags & ~DTRACE_D_MASK) {
6450Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EINVAL);
6460Sstevel@tonic-gate 		return (NULL);
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	flags |= dtp->dt_dflags;
6500Sstevel@tonic-gate 
6511710Sahl 	if (dof_hdr(dtp, pgp->dp_dofversion, &h) != 0)
6520Sstevel@tonic-gate 		return (NULL);
6530Sstevel@tonic-gate 
654265Smws 	if (dt_dof_reset(dtp, pgp) != 0)
655265Smws 		return (NULL);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	/*
6580Sstevel@tonic-gate 	 * Iterate through the statement list computing the maximum number of
6590Sstevel@tonic-gate 	 * actions and the maximum format string for allocating local buffers.
6600Sstevel@tonic-gate 	 */
6610Sstevel@tonic-gate 	for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
6620Sstevel@tonic-gate 	    stp != NULL; stp = dt_list_next(stp), last = edp) {
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 		dtrace_stmtdesc_t *sdp = stp->ds_desc;
6650Sstevel@tonic-gate 		dtrace_actdesc_t *ap = sdp->dtsd_action;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 		if (sdp->dtsd_fmtdata != NULL) {
6680Sstevel@tonic-gate 			i = dtrace_printf_format(dtp,
6690Sstevel@tonic-gate 			    sdp->dtsd_fmtdata, NULL, 0);
6700Sstevel@tonic-gate 			maxfmt = MAX(maxfmt, i);
6710Sstevel@tonic-gate 		}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		if ((edp = sdp->dtsd_ecbdesc) == last)
6740Sstevel@tonic-gate 			continue; /* same ecb as previous statement */
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next)
6770Sstevel@tonic-gate 			i++;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		maxacts = MAX(maxacts, i);
6800Sstevel@tonic-gate 	}
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	dofa = alloca(sizeof (dof_actdesc_t) * maxacts);
6830Sstevel@tonic-gate 	fmt = alloca(maxfmt + 1);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0);
6860Sstevel@tonic-gate 	(void) dof_add_string(ddo, "");
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	/*
689265Smws 	 * If there are references to dynamic translators in the program, add
690265Smws 	 * an imported translator table entry for each referenced translator.
691265Smws 	 */
692265Smws 	if (pgp->dp_xrefslen != 0) {
693265Smws 		for (dxp = dt_list_next(&dtp->dt_xlators);
694265Smws 		    dxp != NULL; dxp = dt_list_next(dxp)) {
695265Smws 			if (dxp->dx_id < pgp->dp_xrefslen &&
696265Smws 			    pgp->dp_xrefs[dxp->dx_id] != NULL)
697265Smws 				dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT);
698265Smws 		}
699265Smws 	}
700265Smws 
701265Smws 	/*
7020Sstevel@tonic-gate 	 * Now iterate through the statement list, creating the DOF section
7030Sstevel@tonic-gate 	 * headers and data for each one and adding them to our buffers.
7040Sstevel@tonic-gate 	 */
7050Sstevel@tonic-gate 	for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
7060Sstevel@tonic-gate 	    stp != NULL; stp = dt_list_next(stp), last = edp) {
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 		dof_secidx_t probesec = DOF_SECIDX_NONE;
7090Sstevel@tonic-gate 		dof_secidx_t prdsec = DOF_SECIDX_NONE;
7100Sstevel@tonic-gate 		dof_secidx_t actsec = DOF_SECIDX_NONE;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		const dt_stmt_t *next = stp;
7130Sstevel@tonic-gate 		dtrace_stmtdesc_t *sdp = stp->ds_desc;
7140Sstevel@tonic-gate 		dof_stridx_t strndx = 0;
7150Sstevel@tonic-gate 		dof_probedesc_t dofp;
7160Sstevel@tonic-gate 		dof_ecbdesc_t dofe;
7170Sstevel@tonic-gate 		uint_t i;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)
7200Sstevel@tonic-gate 			continue; /* same ecb as previous statement */
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 		pdp = &edp->dted_probe;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 		/*
7250Sstevel@tonic-gate 		 * Add a DOF_SECT_PROBEDESC for the ECB's probe description,
7260Sstevel@tonic-gate 		 * and copy the probe description strings into the string table.
7270Sstevel@tonic-gate 		 */
7280Sstevel@tonic-gate 		dofp.dofp_strtab = ddo->ddo_strsec;
7290Sstevel@tonic-gate 		dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider);
7300Sstevel@tonic-gate 		dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod);
7310Sstevel@tonic-gate 		dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func);
7320Sstevel@tonic-gate 		dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name);
7330Sstevel@tonic-gate 		dofp.dofp_id = pdp->dtpd_id;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC,
7360Sstevel@tonic-gate 		    sizeof (dof_secidx_t), 0,
7370Sstevel@tonic-gate 		    sizeof (dof_probedesc_t), sizeof (dof_probedesc_t));
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 		/*
7400Sstevel@tonic-gate 		 * If there is a predicate DIFO associated with the ecbdesc,
7410Sstevel@tonic-gate 		 * write out the DIFO sections and save the DIFO section index.
7420Sstevel@tonic-gate 		 */
7430Sstevel@tonic-gate 		if (edp->dted_pred.dtpdd_difo != NULL)
7440Sstevel@tonic-gate 			prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 		/*
7470Sstevel@tonic-gate 		 * Now iterate through the action list generating DIFOs as
7480Sstevel@tonic-gate 		 * referenced therein and adding action descriptions to 'dofa'.
7490Sstevel@tonic-gate 		 */
7500Sstevel@tonic-gate 		for (i = 0, ap = edp->dted_action;
7510Sstevel@tonic-gate 		    ap != NULL; ap = ap->dtad_next, i++) {
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 			if (ap->dtad_difo != NULL) {
7540Sstevel@tonic-gate 				dofa[i].dofa_difo =
7550Sstevel@tonic-gate 				    dof_add_difo(ddo, ap->dtad_difo);
7560Sstevel@tonic-gate 			} else
7570Sstevel@tonic-gate 				dofa[i].dofa_difo = DOF_SECIDX_NONE;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 			/*
7600Sstevel@tonic-gate 			 * If the first action in a statement has format data,
7610Sstevel@tonic-gate 			 * add the format string to the global string table.
7620Sstevel@tonic-gate 			 */
7630Sstevel@tonic-gate 			if (sdp != NULL && ap == sdp->dtsd_action) {
7640Sstevel@tonic-gate 				if (sdp->dtsd_fmtdata != NULL) {
7650Sstevel@tonic-gate 					(void) dtrace_printf_format(dtp,
7660Sstevel@tonic-gate 					    sdp->dtsd_fmtdata, fmt, maxfmt + 1);
7670Sstevel@tonic-gate 					strndx = dof_add_string(ddo, fmt);
7680Sstevel@tonic-gate 				} else
7690Sstevel@tonic-gate 					strndx = 0; /* use dtad_arg instead */
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 				if ((next = dt_list_next(next)) != NULL)
7720Sstevel@tonic-gate 					sdp = next->ds_desc;
7730Sstevel@tonic-gate 				else
7740Sstevel@tonic-gate 					sdp = NULL;
7750Sstevel@tonic-gate 			}
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 			if (strndx != 0) {
7780Sstevel@tonic-gate 				dofa[i].dofa_arg = strndx;
7790Sstevel@tonic-gate 				dofa[i].dofa_strtab = ddo->ddo_strsec;
7800Sstevel@tonic-gate 			} else {
7810Sstevel@tonic-gate 				dofa[i].dofa_arg = ap->dtad_arg;
7820Sstevel@tonic-gate 				dofa[i].dofa_strtab = DOF_SECIDX_NONE;
7830Sstevel@tonic-gate 			}
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 			dofa[i].dofa_kind = ap->dtad_kind;
7860Sstevel@tonic-gate 			dofa[i].dofa_ntuple = ap->dtad_ntuple;
7870Sstevel@tonic-gate 			dofa[i].dofa_uarg = ap->dtad_uarg;
7880Sstevel@tonic-gate 		}
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 		if (i > 0) {
7910Sstevel@tonic-gate 			actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC,
7920Sstevel@tonic-gate 			    sizeof (uint64_t), 0, sizeof (dof_actdesc_t),
7930Sstevel@tonic-gate 			    sizeof (dof_actdesc_t) * i);
7940Sstevel@tonic-gate 		}
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 		/*
7970Sstevel@tonic-gate 		 * Now finally, add the DOF_SECT_ECBDESC referencing all the
7980Sstevel@tonic-gate 		 * previously created sub-sections.
7990Sstevel@tonic-gate 		 */
8000Sstevel@tonic-gate 		dofe.dofe_probes = probesec;
8010Sstevel@tonic-gate 		dofe.dofe_pred = prdsec;
8020Sstevel@tonic-gate 		dofe.dofe_actions = actsec;
8030Sstevel@tonic-gate 		dofe.dofe_pad = 0;
8040Sstevel@tonic-gate 		dofe.dofe_uarg = edp->dted_uarg;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 		(void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC,
8070Sstevel@tonic-gate 		    sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t));
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * If any providers are user-defined, output DOF sections corresponding
8120Sstevel@tonic-gate 	 * to the providers and the probes and arguments that they define.
8130Sstevel@tonic-gate 	 */
8140Sstevel@tonic-gate 	if (flags & DTRACE_D_PROBES) {
8150Sstevel@tonic-gate 		for (pvp = dt_list_next(&dtp->dt_provlist);
8160Sstevel@tonic-gate 		    pvp != NULL; pvp = dt_list_next(pvp))
8170Sstevel@tonic-gate 			dof_add_provider(ddo, pvp);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	/*
8210Sstevel@tonic-gate 	 * If we're not stripping unloadable sections, generate compiler
8220Sstevel@tonic-gate 	 * comments and any other unloadable miscellany.
8230Sstevel@tonic-gate 	 */
8240Sstevel@tonic-gate 	if (!(flags & DTRACE_D_STRIP)) {
8250Sstevel@tonic-gate 		(void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS,
8260Sstevel@tonic-gate 		    sizeof (char), 0, 0, strlen(_dtrace_version) + 1);
8270Sstevel@tonic-gate 		(void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME,
8280Sstevel@tonic-gate 		    sizeof (char), 0, 0, sizeof (struct utsname));
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	/*
8320Sstevel@tonic-gate 	 * Compute and fill in the appropriate values for the dof_hdr_t's
8330Sstevel@tonic-gate 	 * dofh_secnum, dofh_loadsz, and dofh_filez values.
8340Sstevel@tonic-gate 	 */
8350Sstevel@tonic-gate 	h.dofh_secnum = ddo->ddo_nsecs;
8360Sstevel@tonic-gate 	ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs);
8370Sstevel@tonic-gate 	assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs);
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	h.dofh_loadsz = ssize +
8400Sstevel@tonic-gate 	    dt_buf_len(&ddo->ddo_ldata) +
8410Sstevel@tonic-gate 	    dt_buf_len(&ddo->ddo_strs);
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	if (dt_buf_len(&ddo->ddo_udata) != 0) {
8440Sstevel@tonic-gate 		lsize = roundup(h.dofh_loadsz, sizeof (uint64_t));
8450Sstevel@tonic-gate 		h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata);
8460Sstevel@tonic-gate 	} else {
8470Sstevel@tonic-gate 		lsize = h.dofh_loadsz;
8480Sstevel@tonic-gate 		h.dofh_filesz = lsize;
8490Sstevel@tonic-gate 	}
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	/*
8520Sstevel@tonic-gate 	 * Set the global DOF_SECT_STRTAB's offset to be after the header,
8530Sstevel@tonic-gate 	 * section headers, and other loadable data.  Since we're going to
8540Sstevel@tonic-gate 	 * iterate over the buffer data directly, we must check for errors.
8550Sstevel@tonic-gate 	 */
8560Sstevel@tonic-gate 	if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) {
8570Sstevel@tonic-gate 		(void) dt_set_errno(dtp, i);
8580Sstevel@tonic-gate 		return (NULL);
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	sp = dt_buf_ptr(&ddo->ddo_secs);
8620Sstevel@tonic-gate 	assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata);
8650Sstevel@tonic-gate 	sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs);
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	/*
8680Sstevel@tonic-gate 	 * Now relocate all the other section headers by adding the appropriate
8690Sstevel@tonic-gate 	 * delta to their respective dofs_offset values.
8700Sstevel@tonic-gate 	 */
8710Sstevel@tonic-gate 	for (i = 0; i < ddo->ddo_nsecs; i++, sp++) {
8720Sstevel@tonic-gate 		if (i == ddo->ddo_strsec)
8730Sstevel@tonic-gate 			continue; /* already relocated above */
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		if (sp->dofs_flags & DOF_SECF_LOAD)
8760Sstevel@tonic-gate 			sp->dofs_offset += ssize;
8770Sstevel@tonic-gate 		else
8780Sstevel@tonic-gate 			sp->dofs_offset += lsize;
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	/*
8820Sstevel@tonic-gate 	 * Finally, assemble the complete in-memory DOF buffer by writing the
8830Sstevel@tonic-gate 	 * header and then concatenating all our buffers.  dt_buf_concat() will
8840Sstevel@tonic-gate 	 * propagate any errors and cause dt_buf_claim() to return NULL.
8850Sstevel@tonic-gate 	 */
8860Sstevel@tonic-gate 	dt_buf_create(dtp, &dof, "dof", h.dofh_filesz);
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t));
8890Sstevel@tonic-gate 	dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t));
8900Sstevel@tonic-gate 	dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t));
8910Sstevel@tonic-gate 	dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char));
8920Sstevel@tonic-gate 	dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t));
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	return (dt_buf_claim(dtp, &dof));
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate void
8980Sstevel@tonic-gate dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof)
8990Sstevel@tonic-gate {
9000Sstevel@tonic-gate 	dt_free(dtp, dof);
9010Sstevel@tonic-gate }
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate void *
9040Sstevel@tonic-gate dtrace_getopt_dof(dtrace_hdl_t *dtp)
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate 	dof_hdr_t *dof;
9070Sstevel@tonic-gate 	dof_sec_t *sec;
9080Sstevel@tonic-gate 	dof_optdesc_t *dofo;
9090Sstevel@tonic-gate 	int i, nopts = 0, len = sizeof (dof_hdr_t) +
9100Sstevel@tonic-gate 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t));
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	for (i = 0; i < DTRACEOPT_MAX; i++) {
9130Sstevel@tonic-gate 		if (dtp->dt_options[i] != DTRACEOPT_UNSET)
9140Sstevel@tonic-gate 			nopts++;
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	len += sizeof (dof_optdesc_t) * nopts;
9180Sstevel@tonic-gate 
9191710Sahl 	if ((dof = dt_zalloc(dtp, len)) == NULL ||
9201710Sahl 	    dof_hdr(dtp, DOF_VERSION, dof) != 0) {
9210Sstevel@tonic-gate 		dt_free(dtp, dof);
9220Sstevel@tonic-gate 		return (NULL);
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
9260Sstevel@tonic-gate 	dof->dofh_loadsz = len;
9270Sstevel@tonic-gate 	dof->dofh_filesz = len;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	/*
9300Sstevel@tonic-gate 	 * Fill in the option section header...
9310Sstevel@tonic-gate 	 */
9320Sstevel@tonic-gate 	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
9330Sstevel@tonic-gate 	sec->dofs_type = DOF_SECT_OPTDESC;
9340Sstevel@tonic-gate 	sec->dofs_align = sizeof (uint64_t);
9350Sstevel@tonic-gate 	sec->dofs_flags = DOF_SECF_LOAD;
9360Sstevel@tonic-gate 	sec->dofs_entsize = sizeof (dof_optdesc_t);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	dofo = (dof_optdesc_t *)((uintptr_t)sec +
9390Sstevel@tonic-gate 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;
9420Sstevel@tonic-gate 	sec->dofs_size = sizeof (dof_optdesc_t) * nopts;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	for (i = 0; i < DTRACEOPT_MAX; i++) {
9450Sstevel@tonic-gate 		if (dtp->dt_options[i] == DTRACEOPT_UNSET)
9460Sstevel@tonic-gate 			continue;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		dofo->dofo_option = i;
9490Sstevel@tonic-gate 		dofo->dofo_strtab = DOF_SECIDX_NONE;
9500Sstevel@tonic-gate 		dofo->dofo_value = dtp->dt_options[i];
9510Sstevel@tonic-gate 		dofo++;
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	return (dof);
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate void *
9580Sstevel@tonic-gate dtrace_geterr_dof(dtrace_hdl_t *dtp)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate 	if (dtp->dt_errprog != NULL)
9610Sstevel@tonic-gate 		return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	(void) dt_set_errno(dtp, EDT_BADERROR);
9640Sstevel@tonic-gate 	return (NULL);
9650Sstevel@tonic-gate }
966