xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_dof.c (revision 1239:354be041f716)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*1239Sahl  * 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);
650Sstevel@tonic-gate 	dt_buf_create(dtp, &ddo->ddo_rels, "probe rels", 0);
66265Smws 
67265Smws 	dt_buf_create(dtp, &ddo->ddo_xlms, "xlate members", 0);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate void
710Sstevel@tonic-gate dt_dof_fini(dtrace_hdl_t *dtp)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	dt_dof_t *ddo = &dtp->dt_dof;
740Sstevel@tonic-gate 
75265Smws 	dt_free(dtp, ddo->ddo_xlimport);
76265Smws 	dt_free(dtp, ddo->ddo_xlexport);
77265Smws 
780Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_secs);
790Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_strs);
800Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_ldata);
810Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_udata);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_probes);
840Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_args);
850Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_offs);
860Sstevel@tonic-gate 	dt_buf_destroy(dtp, &ddo->ddo_rels);
87265Smws 
88265Smws 	dt_buf_destroy(dtp, &ddo->ddo_xlms);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
91265Smws static int
92265Smws dt_dof_reset(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	dt_dof_t *ddo = &dtp->dt_dof;
95265Smws 	uint_t i, nx = dtp->dt_xlatorid;
96265Smws 
97265Smws 	assert(ddo->ddo_hdl == dtp);
98265Smws 	ddo->ddo_pgp = pgp;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	ddo->ddo_nsecs = 0;
1010Sstevel@tonic-gate 	ddo->ddo_strsec = DOF_SECIDX_NONE;
1020Sstevel@tonic-gate 
103265Smws 	dt_free(dtp, ddo->ddo_xlimport);
104265Smws 	dt_free(dtp, ddo->ddo_xlexport);
105265Smws 
106265Smws 	ddo->ddo_xlimport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
107265Smws 	ddo->ddo_xlexport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
108265Smws 
109265Smws 	if (nx != 0 && (ddo->ddo_xlimport == NULL || ddo->ddo_xlexport == NULL))
110265Smws 		return (-1); /* errno is set for us */
111265Smws 
112265Smws 	for (i = 0; i < nx; i++) {
113265Smws 		ddo->ddo_xlimport[i] = DOF_SECIDX_NONE;
114265Smws 		ddo->ddo_xlexport[i] = DOF_SECIDX_NONE;
115265Smws 	}
116265Smws 
1170Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_secs);
1180Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_strs);
1190Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_ldata);
1200Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_udata);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_probes);
1230Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_args);
1240Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_offs);
1250Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_rels);
126265Smws 
127265Smws 	dt_buf_reset(dtp, &ddo->ddo_xlms);
128265Smws 	return (0);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * Add a loadable DOF section to the file using the specified data buffer and
1330Sstevel@tonic-gate  * the specified DOF section attributes.  DOF_SECF_LOAD must be set in flags.
1340Sstevel@tonic-gate  * If 'data' is NULL, the caller is responsible for manipulating the ldata buf.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate static dof_secidx_t
1370Sstevel@tonic-gate dof_add_lsect(dt_dof_t *ddo, const void *data, uint32_t type,
1380Sstevel@tonic-gate     uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
1410Sstevel@tonic-gate 	dof_sec_t s;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	s.dofs_type = type;
1440Sstevel@tonic-gate 	s.dofs_align = align;
1450Sstevel@tonic-gate 	s.dofs_flags = flags | DOF_SECF_LOAD;
1460Sstevel@tonic-gate 	s.dofs_entsize = entsize;
1470Sstevel@tonic-gate 	s.dofs_offset = dt_buf_offset(&ddo->ddo_ldata, align);
1480Sstevel@tonic-gate 	s.dofs_size = size;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (data != NULL)
1530Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_ldata, data, size, align);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	return (ddo->ddo_nsecs++);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * Add an unloadable DOF section to the file using the specified data buffer
1600Sstevel@tonic-gate  * and DOF section attributes.  DOF_SECF_LOAD must *not* be set in flags.
1610Sstevel@tonic-gate  * If 'data' is NULL, the caller is responsible for manipulating the udata buf.
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate static dof_secidx_t
1640Sstevel@tonic-gate dof_add_usect(dt_dof_t *ddo, const void *data, uint32_t type,
1650Sstevel@tonic-gate     uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
1680Sstevel@tonic-gate 	dof_sec_t s;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	s.dofs_type = type;
1710Sstevel@tonic-gate 	s.dofs_align = align;
1720Sstevel@tonic-gate 	s.dofs_flags = flags & ~DOF_SECF_LOAD;
1730Sstevel@tonic-gate 	s.dofs_entsize = entsize;
1740Sstevel@tonic-gate 	s.dofs_offset = dt_buf_offset(&ddo->ddo_udata, align);
1750Sstevel@tonic-gate 	s.dofs_size = size;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (data != NULL)
1800Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_udata, data, size, align);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	return (ddo->ddo_nsecs++);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Add a string to the global string table associated with the DOF.  The offset
1870Sstevel@tonic-gate  * of the string is returned as an index into the string table.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate static dof_stridx_t
1900Sstevel@tonic-gate dof_add_string(dt_dof_t *ddo, const char *s)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	dt_buf_t *bp = &ddo->ddo_strs;
1930Sstevel@tonic-gate 	dof_stridx_t i = dt_buf_len(bp);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (i != 0 && (s == NULL || *s == '\0'))
1960Sstevel@tonic-gate 		return (0); /* string table has \0 at offset 0 */
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	dt_buf_write(ddo->ddo_hdl, bp, s, strlen(s) + 1, sizeof (char));
1990Sstevel@tonic-gate 	return (i);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate static dof_attr_t
2030Sstevel@tonic-gate dof_attr(const dtrace_attribute_t *ap)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	return (DOF_ATTR(ap->dtat_name, ap->dtat_data, ap->dtat_class));
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static dof_secidx_t
2090Sstevel@tonic-gate dof_add_difo(dt_dof_t *ddo, const dtrace_difo_t *dp)
2100Sstevel@tonic-gate {
211265Smws 	dof_secidx_t dsecs[5]; /* enough for all possible DIFO sections */
2120Sstevel@tonic-gate 	uint_t nsecs = 0;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	dof_difohdr_t *dofd;
2150Sstevel@tonic-gate 	dof_relohdr_t dofr;
2160Sstevel@tonic-gate 	dof_secidx_t relsec;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	dof_secidx_t strsec = DOF_SECIDX_NONE;
2190Sstevel@tonic-gate 	dof_secidx_t intsec = DOF_SECIDX_NONE;
2200Sstevel@tonic-gate 	dof_secidx_t hdrsec = DOF_SECIDX_NONE;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	if (dp->dtdo_buf != NULL) {
2230Sstevel@tonic-gate 		dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_buf,
2240Sstevel@tonic-gate 		    DOF_SECT_DIF, sizeof (dif_instr_t), 0,
2250Sstevel@tonic-gate 		    sizeof (dif_instr_t), sizeof (dif_instr_t) * dp->dtdo_len);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (dp->dtdo_inttab != NULL) {
2290Sstevel@tonic-gate 		dsecs[nsecs++] = intsec = dof_add_lsect(ddo, dp->dtdo_inttab,
2300Sstevel@tonic-gate 		    DOF_SECT_INTTAB, sizeof (uint64_t), 0,
2310Sstevel@tonic-gate 		    sizeof (uint64_t), sizeof (uint64_t) * dp->dtdo_intlen);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (dp->dtdo_strtab != NULL) {
2350Sstevel@tonic-gate 		dsecs[nsecs++] = strsec = dof_add_lsect(ddo, dp->dtdo_strtab,
2360Sstevel@tonic-gate 		    DOF_SECT_STRTAB, sizeof (char), 0, 0, dp->dtdo_strlen);
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (dp->dtdo_vartab != NULL) {
2400Sstevel@tonic-gate 		dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_vartab,
2410Sstevel@tonic-gate 		    DOF_SECT_VARTAB, sizeof (uint_t), 0, sizeof (dtrace_difv_t),
2420Sstevel@tonic-gate 		    sizeof (dtrace_difv_t) * dp->dtdo_varlen);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
245265Smws 	if (dp->dtdo_xlmtab != NULL) {
246265Smws 		dof_xlref_t *xlt, *xlp;
247265Smws 		dt_node_t **pnp;
248265Smws 
249265Smws 		xlt = alloca(sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
250265Smws 		pnp = dp->dtdo_xlmtab;
251265Smws 
252265Smws 		/*
253265Smws 		 * dtdo_xlmtab contains pointers to the translator members.
254265Smws 		 * The translator itself is in sect ddo_xlimport[dxp->dx_id].
255265Smws 		 * The XLMEMBERS entries are in order by their dn_membid, so
256265Smws 		 * the member section offset is the population count of bits
257265Smws 		 * in ddo_pgp->dp_xlrefs[] up to and not including dn_membid.
258265Smws 		 */
259265Smws 		for (xlp = xlt; xlp < xlt + dp->dtdo_xlmlen; xlp++) {
260265Smws 			dt_node_t *dnp = *pnp++;
261265Smws 			dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
262265Smws 
263265Smws 			xlp->dofxr_xlator = ddo->ddo_xlimport[dxp->dx_id];
264265Smws 			xlp->dofxr_member = dt_popcb(
265265Smws 			    ddo->ddo_pgp->dp_xrefs[dxp->dx_id], dnp->dn_membid);
266265Smws 			xlp->dofxr_argn = (uint32_t)dxp->dx_arg;
267265Smws 		}
268265Smws 
269265Smws 		dsecs[nsecs++] = dof_add_lsect(ddo, xlt, DOF_SECT_XLTAB,
270265Smws 		    sizeof (dof_secidx_t), 0, sizeof (dof_xlref_t),
271265Smws 		    sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
272265Smws 	}
273265Smws 
2740Sstevel@tonic-gate 	/*
2750Sstevel@tonic-gate 	 * Copy the return type and the array of section indices that form the
2760Sstevel@tonic-gate 	 * DIFO into a single dof_difohdr_t and then add DOF_SECT_DIFOHDR.
2770Sstevel@tonic-gate 	 */
2780Sstevel@tonic-gate 	assert(nsecs <= sizeof (dsecs) / sizeof (dsecs[0]));
2790Sstevel@tonic-gate 	dofd = alloca(sizeof (dtrace_diftype_t) + sizeof (dsecs));
2800Sstevel@tonic-gate 	bcopy(&dp->dtdo_rtype, &dofd->dofd_rtype, sizeof (dtrace_diftype_t));
2810Sstevel@tonic-gate 	bcopy(dsecs, &dofd->dofd_links, sizeof (dof_secidx_t) * nsecs);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	hdrsec = dof_add_lsect(ddo, dofd, DOF_SECT_DIFOHDR,
2840Sstevel@tonic-gate 	    sizeof (dof_secidx_t), 0, 0,
2850Sstevel@tonic-gate 	    sizeof (dtrace_diftype_t) + sizeof (dof_secidx_t) * nsecs);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/*
2880Sstevel@tonic-gate 	 * Add any other sections related to dtrace_difo_t.  These are not
2890Sstevel@tonic-gate 	 * referenced in dof_difohdr_t because they are not used by emulation.
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 	if (dp->dtdo_kreltab != NULL) {
2920Sstevel@tonic-gate 		relsec = dof_add_lsect(ddo, dp->dtdo_kreltab, DOF_SECT_RELTAB,
2930Sstevel@tonic-gate 		    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
2940Sstevel@tonic-gate 		    sizeof (dof_relodesc_t) * dp->dtdo_krelen);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 		/*
2970Sstevel@tonic-gate 		 * This code assumes the target of all relocations is the
2980Sstevel@tonic-gate 		 * integer table 'intsec' (DOF_SECT_INTTAB).  If other sections
2990Sstevel@tonic-gate 		 * need relocation in the future this will need to change.
3000Sstevel@tonic-gate 		 */
3010Sstevel@tonic-gate 		dofr.dofr_strtab = strsec;
3020Sstevel@tonic-gate 		dofr.dofr_relsec = relsec;
3030Sstevel@tonic-gate 		dofr.dofr_tgtsec = intsec;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		(void) dof_add_lsect(ddo, &dofr, DOF_SECT_KRELHDR,
3060Sstevel@tonic-gate 		    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (dp->dtdo_ureltab != NULL) {
3100Sstevel@tonic-gate 		relsec = dof_add_lsect(ddo, dp->dtdo_ureltab, DOF_SECT_RELTAB,
3110Sstevel@tonic-gate 		    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
3120Sstevel@tonic-gate 		    sizeof (dof_relodesc_t) * dp->dtdo_urelen);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		/*
3150Sstevel@tonic-gate 		 * This code assumes the target of all relocations is the
3160Sstevel@tonic-gate 		 * integer table 'intsec' (DOF_SECT_INTTAB).  If other sections
3170Sstevel@tonic-gate 		 * need relocation in the future this will need to change.
3180Sstevel@tonic-gate 		 */
3190Sstevel@tonic-gate 		dofr.dofr_strtab = strsec;
3200Sstevel@tonic-gate 		dofr.dofr_relsec = relsec;
3210Sstevel@tonic-gate 		dofr.dofr_tgtsec = intsec;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		(void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
3240Sstevel@tonic-gate 		    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	return (hdrsec);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
330265Smws static void
331265Smws dof_add_translator(dt_dof_t *ddo, const dt_xlator_t *dxp, uint_t type)
332265Smws {
333265Smws 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
334265Smws 	dof_xlmember_t dofxm;
335265Smws 	dof_xlator_t dofxl;
336265Smws 	dof_secidx_t *xst;
337265Smws 
338265Smws 	char buf[DT_TYPE_NAMELEN];
339265Smws 	dt_node_t *dnp;
340265Smws 	uint_t i = 0;
341265Smws 
342265Smws 	assert(type == DOF_SECT_XLIMPORT || type == DOF_SECT_XLEXPORT);
343265Smws 	xst = type == DOF_SECT_XLIMPORT ? ddo->ddo_xlimport : ddo->ddo_xlexport;
344265Smws 
345265Smws 	if (xst[dxp->dx_id] != DOF_SECIDX_NONE)
346265Smws 		return; /* translator has already been emitted */
347265Smws 
348265Smws 	dt_buf_reset(dtp, &ddo->ddo_xlms);
349265Smws 
350265Smws 	/*
351265Smws 	 * Generate an array of dof_xlmember_t's into ddo_xlms.  If we are
352265Smws 	 * importing the translator, add only those members referenced by the
353265Smws 	 * program and set the dofxm_difo reference of each member to NONE.  If
354265Smws 	 * we're exporting the translator, add all members and a DIFO for each.
355265Smws 	 */
356265Smws 	for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list, i++) {
357265Smws 		if (type == DOF_SECT_XLIMPORT) {
358265Smws 			if (!BT_TEST(ddo->ddo_pgp->dp_xrefs[dxp->dx_id], i))
359265Smws 				continue; /* member is not referenced */
360265Smws 			dofxm.dofxm_difo = DOF_SECIDX_NONE;
361265Smws 		} else {
362265Smws 			dofxm.dofxm_difo = dof_add_difo(ddo,
363265Smws 			    dxp->dx_membdif[dnp->dn_membid]);
364265Smws 		}
365265Smws 
366265Smws 		dofxm.dofxm_name = dof_add_string(ddo, dnp->dn_membname);
367265Smws 		dt_node_diftype(dtp, dnp, &dofxm.dofxm_type);
368265Smws 
369265Smws 		dt_buf_write(dtp, &ddo->ddo_xlms,
370265Smws 		    &dofxm, sizeof (dofxm), sizeof (uint32_t));
371265Smws 	}
372265Smws 
373265Smws 	dofxl.dofxl_members = dof_add_lsect(ddo, NULL, DOF_SECT_XLMEMBERS,
374265Smws 	    sizeof (uint32_t), 0, sizeof (dofxm), dt_buf_len(&ddo->ddo_xlms));
375265Smws 
376265Smws 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_xlms, sizeof (uint32_t));
377265Smws 
378265Smws 	dofxl.dofxl_strtab = ddo->ddo_strsec;
379265Smws 	dofxl.dofxl_argv = dof_add_string(ddo, ctf_type_name(
380265Smws 	    dxp->dx_src_ctfp, dxp->dx_src_type, buf, sizeof (buf)));
381265Smws 	dofxl.dofxl_argc = 1;
382265Smws 	dofxl.dofxl_type = dof_add_string(ddo, ctf_type_name(
383265Smws 	    dxp->dx_dst_ctfp, dxp->dx_dst_type, buf, sizeof (buf)));
384265Smws 	dofxl.dofxl_attr = dof_attr(&dxp->dx_souid.di_attr);
385265Smws 
386265Smws 	xst[dxp->dx_id] = dof_add_lsect(ddo, &dofxl, type,
387265Smws 	    sizeof (uint32_t), 0, 0, sizeof (dofxl));
388265Smws }
389265Smws 
3900Sstevel@tonic-gate /*ARGSUSED*/
3910Sstevel@tonic-gate static int
3920Sstevel@tonic-gate dof_add_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	dt_dof_t *ddo = data;
3950Sstevel@tonic-gate 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
3960Sstevel@tonic-gate 	dt_probe_t *prp = idp->di_data;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	dof_probe_t dofpr;
3990Sstevel@tonic-gate 	dof_relodesc_t dofr;
4000Sstevel@tonic-gate 	dt_probe_instance_t *pip;
4010Sstevel@tonic-gate 	dt_node_t *dnp;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	char buf[DT_TYPE_NAMELEN];
4040Sstevel@tonic-gate 	uint_t i;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	dofpr.dofpr_addr = 0;
4070Sstevel@tonic-gate 	dofpr.dofpr_name = dof_add_string(ddo, prp->pr_name);
4080Sstevel@tonic-gate 	dofpr.dofpr_nargv = dt_buf_len(&ddo->ddo_strs);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	for (dnp = prp->pr_nargs; dnp != NULL; dnp = dnp->dn_list) {
4110Sstevel@tonic-gate 		(void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
4120Sstevel@tonic-gate 		    dnp->dn_type, buf, sizeof (buf)));
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	dofpr.dofpr_xargv = dt_buf_len(&ddo->ddo_strs);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	for (dnp = prp->pr_xargs; dnp != NULL; dnp = dnp->dn_list) {
4180Sstevel@tonic-gate 		(void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
4190Sstevel@tonic-gate 		    dnp->dn_type, buf, sizeof (buf)));
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	dofpr.dofpr_argidx = dt_buf_len(&ddo->ddo_args) / sizeof (uint8_t);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	for (i = 0; i < prp->pr_xargc; i++) {
4250Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_args, &prp->pr_mapping[i],
4260Sstevel@tonic-gate 		    sizeof (uint8_t), sizeof (uint8_t));
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	dofpr.dofpr_nargc = prp->pr_nargc;
4300Sstevel@tonic-gate 	dofpr.dofpr_xargc = prp->pr_xargc;
4310Sstevel@tonic-gate 	dofpr.dofpr_pad = 0;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
4340Sstevel@tonic-gate 		dofpr.dofpr_func = dof_add_string(ddo, pip->pi_fname);
4350Sstevel@tonic-gate 		dofpr.dofpr_offidx =
4360Sstevel@tonic-gate 		    dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t);
4370Sstevel@tonic-gate 		dofpr.dofpr_noffs = pip->pi_noffs;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs,
4400Sstevel@tonic-gate 		    pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
4410Sstevel@tonic-gate 
442*1239Sahl 		/*
443*1239Sahl 		 * If pi_rname isn't set, the relocation will be against the
444*1239Sahl 		 * function name. If it is, the relocation will be against
445*1239Sahl 		 * pi_rname. This will be used if the function is scoped
446*1239Sahl 		 * locally so an alternate symbol is added for the purpose
447*1239Sahl 		 * of this relocation.
448*1239Sahl 		 */
449*1239Sahl 		if (pip->pi_rname[0] == '\0')
450*1239Sahl 			dofr.dofr_name = dofpr.dofpr_func;
451*1239Sahl 		else
452*1239Sahl 			dofr.dofr_name = dof_add_string(ddo, pip->pi_rname);
4530Sstevel@tonic-gate 		dofr.dofr_type = DOF_RELO_SETX;
4540Sstevel@tonic-gate 		dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes);
4550Sstevel@tonic-gate 		dofr.dofr_data = 0;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_rels, &dofr,
4580Sstevel@tonic-gate 		    sizeof (dofr), sizeof (uint64_t));
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 		dt_buf_write(dtp, &ddo->ddo_probes, &dofpr,
4610Sstevel@tonic-gate 		    sizeof (dofpr), sizeof (uint64_t));
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	return (0);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate static void
4680Sstevel@tonic-gate dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
4710Sstevel@tonic-gate 	dof_provider_t dofpv;
4720Sstevel@tonic-gate 	dof_relohdr_t dofr;
473265Smws 	dof_secidx_t *dofs;
474265Smws 	ulong_t xr, nxr;
475265Smws 	id_t i;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	if (pvp->pv_flags & DT_PROVIDER_IMPL)
4780Sstevel@tonic-gate 		return; /* ignore providers that are exported by dtrace(7D) */
4790Sstevel@tonic-gate 
480265Smws 	nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax);
481265Smws 	dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1));
482265Smws 	xr = 1; /* reserve dofs[0] for the provider itself */
483265Smws 
484265Smws 	/*
485265Smws 	 * For each translator referenced by the provider (pv_xrefs), emit an
486265Smws 	 * exported translator section for it if one hasn't been created yet.
487265Smws 	 */
488265Smws 	for (i = 0; i < pvp->pv_xrmax; i++) {
489265Smws 		if (BT_TEST(pvp->pv_xrefs, i) &&
490265Smws 		    dtp->dt_xlatemode == DT_XL_DYNAMIC) {
491265Smws 			dof_add_translator(ddo,
492265Smws 			    dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT);
493265Smws 			dofs[xr++] = ddo->ddo_xlexport[i];
494265Smws 		}
495265Smws 	}
496265Smws 
4970Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_probes);
4980Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_args);
4990Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_offs);
5000Sstevel@tonic-gate 	dt_buf_reset(dtp, &ddo->ddo_rels);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	(void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES,
5050Sstevel@tonic-gate 	    sizeof (uint64_t), 0, sizeof (dof_probe_t),
5060Sstevel@tonic-gate 	    dt_buf_len(&ddo->ddo_probes));
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	dt_buf_concat(dtp, &ddo->ddo_ldata,
5090Sstevel@tonic-gate 	    &ddo->ddo_probes, sizeof (uint64_t));
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS,
5120Sstevel@tonic-gate 	    sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args));
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t));
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS,
5170Sstevel@tonic-gate 	    sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs));
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t));
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	dofpv.dofpv_strtab = ddo->ddo_strsec;
5220Sstevel@tonic-gate 	dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider);
5250Sstevel@tonic-gate 	dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod);
5260Sstevel@tonic-gate 	dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func);
5270Sstevel@tonic-gate 	dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name);
5280Sstevel@tonic-gate 	dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args);
5290Sstevel@tonic-gate 
530265Smws 	dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER,
5310Sstevel@tonic-gate 	    sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t));
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	dofr.dofr_strtab = dofpv.dofpv_strtab;
5340Sstevel@tonic-gate 	dofr.dofr_tgtsec = dofpv.dofpv_probes;
5350Sstevel@tonic-gate 	dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB,
5360Sstevel@tonic-gate 	    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
5370Sstevel@tonic-gate 	    dt_buf_len(&ddo->ddo_rels));
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t));
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	(void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
5420Sstevel@tonic-gate 	    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
543265Smws 
544265Smws 	if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) {
545265Smws 		(void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT,
546265Smws 		    sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t),
547265Smws 		    sizeof (dof_secidx_t) * (nxr + 1));
548265Smws 	}
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate static int
5520Sstevel@tonic-gate dof_hdr(dtrace_hdl_t *dtp, dof_hdr_t *hp)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	/*
5550Sstevel@tonic-gate 	 * If our config values cannot fit in a uint8_t, we can't generate a
5560Sstevel@tonic-gate 	 * DOF header since the values won't fit.  This can only happen if the
5570Sstevel@tonic-gate 	 * user forcibly compiles a program with an artificial configuration.
5580Sstevel@tonic-gate 	 */
5590Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_difversion > UINT8_MAX ||
5600Sstevel@tonic-gate 	    dtp->dt_conf.dtc_difintregs > UINT8_MAX ||
5610Sstevel@tonic-gate 	    dtp->dt_conf.dtc_diftupregs > UINT8_MAX)
5620Sstevel@tonic-gate 		return (dt_set_errno(dtp, EOVERFLOW));
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	bzero(hp, sizeof (dof_hdr_t));
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
5670Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
5680Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
5690Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
5720Sstevel@tonic-gate 		hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64;
5730Sstevel@tonic-gate 	else
5740Sstevel@tonic-gate 		hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
5770Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_VERSION] = DOF_VERSION_1;
5780Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion;
5790Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs;
5800Sstevel@tonic-gate 	hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	hp->dofh_hdrsize = sizeof (dof_hdr_t);
5830Sstevel@tonic-gate 	hp->dofh_secsize = sizeof (dof_sec_t);
5840Sstevel@tonic-gate 	hp->dofh_secoff = sizeof (dof_hdr_t);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	return (0);
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate void *
5900Sstevel@tonic-gate dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags)
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate 	dt_dof_t *ddo = &dtp->dt_dof;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	const dtrace_ecbdesc_t *edp, *last;
5950Sstevel@tonic-gate 	const dtrace_probedesc_t *pdp;
5960Sstevel@tonic-gate 	const dtrace_actdesc_t *ap;
5970Sstevel@tonic-gate 	const dt_stmt_t *stp;
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	uint_t maxacts = 0;
6000Sstevel@tonic-gate 	uint_t maxfmt = 0;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	dt_provider_t *pvp;
603265Smws 	dt_xlator_t *dxp;
6040Sstevel@tonic-gate 	dof_actdesc_t *dofa;
6050Sstevel@tonic-gate 	dof_sec_t *sp;
6060Sstevel@tonic-gate 	size_t ssize, lsize;
6070Sstevel@tonic-gate 	dof_hdr_t h;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	dt_buf_t dof;
6100Sstevel@tonic-gate 	char *fmt;
6110Sstevel@tonic-gate 	uint_t i;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	if (flags & ~DTRACE_D_MASK) {
6140Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EINVAL);
6150Sstevel@tonic-gate 		return (NULL);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	flags |= dtp->dt_dflags;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	if (dof_hdr(dtp, &h) != 0)
6210Sstevel@tonic-gate 		return (NULL);
6220Sstevel@tonic-gate 
623265Smws 	if (dt_dof_reset(dtp, pgp) != 0)
624265Smws 		return (NULL);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	/*
6270Sstevel@tonic-gate 	 * Iterate through the statement list computing the maximum number of
6280Sstevel@tonic-gate 	 * actions and the maximum format string for allocating local buffers.
6290Sstevel@tonic-gate 	 */
6300Sstevel@tonic-gate 	for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
6310Sstevel@tonic-gate 	    stp != NULL; stp = dt_list_next(stp), last = edp) {
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		dtrace_stmtdesc_t *sdp = stp->ds_desc;
6340Sstevel@tonic-gate 		dtrace_actdesc_t *ap = sdp->dtsd_action;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 		if (sdp->dtsd_fmtdata != NULL) {
6370Sstevel@tonic-gate 			i = dtrace_printf_format(dtp,
6380Sstevel@tonic-gate 			    sdp->dtsd_fmtdata, NULL, 0);
6390Sstevel@tonic-gate 			maxfmt = MAX(maxfmt, i);
6400Sstevel@tonic-gate 		}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		if ((edp = sdp->dtsd_ecbdesc) == last)
6430Sstevel@tonic-gate 			continue; /* same ecb as previous statement */
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next)
6460Sstevel@tonic-gate 			i++;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		maxacts = MAX(maxacts, i);
6490Sstevel@tonic-gate 	}
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	dofa = alloca(sizeof (dof_actdesc_t) * maxacts);
6520Sstevel@tonic-gate 	fmt = alloca(maxfmt + 1);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0);
6550Sstevel@tonic-gate 	(void) dof_add_string(ddo, "");
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	/*
658265Smws 	 * If there are references to dynamic translators in the program, add
659265Smws 	 * an imported translator table entry for each referenced translator.
660265Smws 	 */
661265Smws 	if (pgp->dp_xrefslen != 0) {
662265Smws 		for (dxp = dt_list_next(&dtp->dt_xlators);
663265Smws 		    dxp != NULL; dxp = dt_list_next(dxp)) {
664265Smws 			if (dxp->dx_id < pgp->dp_xrefslen &&
665265Smws 			    pgp->dp_xrefs[dxp->dx_id] != NULL)
666265Smws 				dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT);
667265Smws 		}
668265Smws 	}
669265Smws 
670265Smws 	/*
6710Sstevel@tonic-gate 	 * Now iterate through the statement list, creating the DOF section
6720Sstevel@tonic-gate 	 * headers and data for each one and adding them to our buffers.
6730Sstevel@tonic-gate 	 */
6740Sstevel@tonic-gate 	for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
6750Sstevel@tonic-gate 	    stp != NULL; stp = dt_list_next(stp), last = edp) {
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 		dof_secidx_t probesec = DOF_SECIDX_NONE;
6780Sstevel@tonic-gate 		dof_secidx_t prdsec = DOF_SECIDX_NONE;
6790Sstevel@tonic-gate 		dof_secidx_t actsec = DOF_SECIDX_NONE;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 		const dt_stmt_t *next = stp;
6820Sstevel@tonic-gate 		dtrace_stmtdesc_t *sdp = stp->ds_desc;
6830Sstevel@tonic-gate 		dof_stridx_t strndx = 0;
6840Sstevel@tonic-gate 		dof_probedesc_t dofp;
6850Sstevel@tonic-gate 		dof_ecbdesc_t dofe;
6860Sstevel@tonic-gate 		uint_t i;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)
6890Sstevel@tonic-gate 			continue; /* same ecb as previous statement */
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		pdp = &edp->dted_probe;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 		/*
6940Sstevel@tonic-gate 		 * Add a DOF_SECT_PROBEDESC for the ECB's probe description,
6950Sstevel@tonic-gate 		 * and copy the probe description strings into the string table.
6960Sstevel@tonic-gate 		 */
6970Sstevel@tonic-gate 		dofp.dofp_strtab = ddo->ddo_strsec;
6980Sstevel@tonic-gate 		dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider);
6990Sstevel@tonic-gate 		dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod);
7000Sstevel@tonic-gate 		dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func);
7010Sstevel@tonic-gate 		dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name);
7020Sstevel@tonic-gate 		dofp.dofp_id = pdp->dtpd_id;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC,
7050Sstevel@tonic-gate 		    sizeof (dof_secidx_t), 0,
7060Sstevel@tonic-gate 		    sizeof (dof_probedesc_t), sizeof (dof_probedesc_t));
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 		/*
7090Sstevel@tonic-gate 		 * If there is a predicate DIFO associated with the ecbdesc,
7100Sstevel@tonic-gate 		 * write out the DIFO sections and save the DIFO section index.
7110Sstevel@tonic-gate 		 */
7120Sstevel@tonic-gate 		if (edp->dted_pred.dtpdd_difo != NULL)
7130Sstevel@tonic-gate 			prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 		/*
7160Sstevel@tonic-gate 		 * Now iterate through the action list generating DIFOs as
7170Sstevel@tonic-gate 		 * referenced therein and adding action descriptions to 'dofa'.
7180Sstevel@tonic-gate 		 */
7190Sstevel@tonic-gate 		for (i = 0, ap = edp->dted_action;
7200Sstevel@tonic-gate 		    ap != NULL; ap = ap->dtad_next, i++) {
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 			if (ap->dtad_difo != NULL) {
7230Sstevel@tonic-gate 				dofa[i].dofa_difo =
7240Sstevel@tonic-gate 				    dof_add_difo(ddo, ap->dtad_difo);
7250Sstevel@tonic-gate 			} else
7260Sstevel@tonic-gate 				dofa[i].dofa_difo = DOF_SECIDX_NONE;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 			/*
7290Sstevel@tonic-gate 			 * If the first action in a statement has format data,
7300Sstevel@tonic-gate 			 * add the format string to the global string table.
7310Sstevel@tonic-gate 			 */
7320Sstevel@tonic-gate 			if (sdp != NULL && ap == sdp->dtsd_action) {
7330Sstevel@tonic-gate 				if (sdp->dtsd_fmtdata != NULL) {
7340Sstevel@tonic-gate 					(void) dtrace_printf_format(dtp,
7350Sstevel@tonic-gate 					    sdp->dtsd_fmtdata, fmt, maxfmt + 1);
7360Sstevel@tonic-gate 					strndx = dof_add_string(ddo, fmt);
7370Sstevel@tonic-gate 				} else
7380Sstevel@tonic-gate 					strndx = 0; /* use dtad_arg instead */
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 				if ((next = dt_list_next(next)) != NULL)
7410Sstevel@tonic-gate 					sdp = next->ds_desc;
7420Sstevel@tonic-gate 				else
7430Sstevel@tonic-gate 					sdp = NULL;
7440Sstevel@tonic-gate 			}
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 			if (strndx != 0) {
7470Sstevel@tonic-gate 				dofa[i].dofa_arg = strndx;
7480Sstevel@tonic-gate 				dofa[i].dofa_strtab = ddo->ddo_strsec;
7490Sstevel@tonic-gate 			} else {
7500Sstevel@tonic-gate 				dofa[i].dofa_arg = ap->dtad_arg;
7510Sstevel@tonic-gate 				dofa[i].dofa_strtab = DOF_SECIDX_NONE;
7520Sstevel@tonic-gate 			}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 			dofa[i].dofa_kind = ap->dtad_kind;
7550Sstevel@tonic-gate 			dofa[i].dofa_ntuple = ap->dtad_ntuple;
7560Sstevel@tonic-gate 			dofa[i].dofa_uarg = ap->dtad_uarg;
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 		if (i > 0) {
7600Sstevel@tonic-gate 			actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC,
7610Sstevel@tonic-gate 			    sizeof (uint64_t), 0, sizeof (dof_actdesc_t),
7620Sstevel@tonic-gate 			    sizeof (dof_actdesc_t) * i);
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		/*
7660Sstevel@tonic-gate 		 * Now finally, add the DOF_SECT_ECBDESC referencing all the
7670Sstevel@tonic-gate 		 * previously created sub-sections.
7680Sstevel@tonic-gate 		 */
7690Sstevel@tonic-gate 		dofe.dofe_probes = probesec;
7700Sstevel@tonic-gate 		dofe.dofe_pred = prdsec;
7710Sstevel@tonic-gate 		dofe.dofe_actions = actsec;
7720Sstevel@tonic-gate 		dofe.dofe_pad = 0;
7730Sstevel@tonic-gate 		dofe.dofe_uarg = edp->dted_uarg;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 		(void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC,
7760Sstevel@tonic-gate 		    sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t));
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/*
7800Sstevel@tonic-gate 	 * If any providers are user-defined, output DOF sections corresponding
7810Sstevel@tonic-gate 	 * to the providers and the probes and arguments that they define.
7820Sstevel@tonic-gate 	 */
7830Sstevel@tonic-gate 	if (flags & DTRACE_D_PROBES) {
7840Sstevel@tonic-gate 		for (pvp = dt_list_next(&dtp->dt_provlist);
7850Sstevel@tonic-gate 		    pvp != NULL; pvp = dt_list_next(pvp))
7860Sstevel@tonic-gate 			dof_add_provider(ddo, pvp);
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	/*
7900Sstevel@tonic-gate 	 * If we're not stripping unloadable sections, generate compiler
7910Sstevel@tonic-gate 	 * comments and any other unloadable miscellany.
7920Sstevel@tonic-gate 	 */
7930Sstevel@tonic-gate 	if (!(flags & DTRACE_D_STRIP)) {
7940Sstevel@tonic-gate 		(void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS,
7950Sstevel@tonic-gate 		    sizeof (char), 0, 0, strlen(_dtrace_version) + 1);
7960Sstevel@tonic-gate 		(void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME,
7970Sstevel@tonic-gate 		    sizeof (char), 0, 0, sizeof (struct utsname));
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	/*
8010Sstevel@tonic-gate 	 * Compute and fill in the appropriate values for the dof_hdr_t's
8020Sstevel@tonic-gate 	 * dofh_secnum, dofh_loadsz, and dofh_filez values.
8030Sstevel@tonic-gate 	 */
8040Sstevel@tonic-gate 	h.dofh_secnum = ddo->ddo_nsecs;
8050Sstevel@tonic-gate 	ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs);
8060Sstevel@tonic-gate 	assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	h.dofh_loadsz = ssize +
8090Sstevel@tonic-gate 	    dt_buf_len(&ddo->ddo_ldata) +
8100Sstevel@tonic-gate 	    dt_buf_len(&ddo->ddo_strs);
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	if (dt_buf_len(&ddo->ddo_udata) != 0) {
8130Sstevel@tonic-gate 		lsize = roundup(h.dofh_loadsz, sizeof (uint64_t));
8140Sstevel@tonic-gate 		h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata);
8150Sstevel@tonic-gate 	} else {
8160Sstevel@tonic-gate 		lsize = h.dofh_loadsz;
8170Sstevel@tonic-gate 		h.dofh_filesz = lsize;
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	/*
8210Sstevel@tonic-gate 	 * Set the global DOF_SECT_STRTAB's offset to be after the header,
8220Sstevel@tonic-gate 	 * section headers, and other loadable data.  Since we're going to
8230Sstevel@tonic-gate 	 * iterate over the buffer data directly, we must check for errors.
8240Sstevel@tonic-gate 	 */
8250Sstevel@tonic-gate 	if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) {
8260Sstevel@tonic-gate 		(void) dt_set_errno(dtp, i);
8270Sstevel@tonic-gate 		return (NULL);
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	sp = dt_buf_ptr(&ddo->ddo_secs);
8310Sstevel@tonic-gate 	assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB);
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata);
8340Sstevel@tonic-gate 	sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/*
8370Sstevel@tonic-gate 	 * Now relocate all the other section headers by adding the appropriate
8380Sstevel@tonic-gate 	 * delta to their respective dofs_offset values.
8390Sstevel@tonic-gate 	 */
8400Sstevel@tonic-gate 	for (i = 0; i < ddo->ddo_nsecs; i++, sp++) {
8410Sstevel@tonic-gate 		if (i == ddo->ddo_strsec)
8420Sstevel@tonic-gate 			continue; /* already relocated above */
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 		if (sp->dofs_flags & DOF_SECF_LOAD)
8450Sstevel@tonic-gate 			sp->dofs_offset += ssize;
8460Sstevel@tonic-gate 		else
8470Sstevel@tonic-gate 			sp->dofs_offset += lsize;
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	/*
8510Sstevel@tonic-gate 	 * Finally, assemble the complete in-memory DOF buffer by writing the
8520Sstevel@tonic-gate 	 * header and then concatenating all our buffers.  dt_buf_concat() will
8530Sstevel@tonic-gate 	 * propagate any errors and cause dt_buf_claim() to return NULL.
8540Sstevel@tonic-gate 	 */
8550Sstevel@tonic-gate 	dt_buf_create(dtp, &dof, "dof", h.dofh_filesz);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t));
8580Sstevel@tonic-gate 	dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t));
8590Sstevel@tonic-gate 	dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t));
8600Sstevel@tonic-gate 	dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char));
8610Sstevel@tonic-gate 	dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t));
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	return (dt_buf_claim(dtp, &dof));
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate void
8670Sstevel@tonic-gate dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	dt_free(dtp, dof);
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate void *
8730Sstevel@tonic-gate dtrace_getopt_dof(dtrace_hdl_t *dtp)
8740Sstevel@tonic-gate {
8750Sstevel@tonic-gate 	dof_hdr_t *dof;
8760Sstevel@tonic-gate 	dof_sec_t *sec;
8770Sstevel@tonic-gate 	dof_optdesc_t *dofo;
8780Sstevel@tonic-gate 	int i, nopts = 0, len = sizeof (dof_hdr_t) +
8790Sstevel@tonic-gate 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t));
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	for (i = 0; i < DTRACEOPT_MAX; i++) {
8820Sstevel@tonic-gate 		if (dtp->dt_options[i] != DTRACEOPT_UNSET)
8830Sstevel@tonic-gate 			nopts++;
8840Sstevel@tonic-gate 	}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	len += sizeof (dof_optdesc_t) * nopts;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	if ((dof = dt_zalloc(dtp, len)) == NULL || dof_hdr(dtp, dof) != 0) {
8890Sstevel@tonic-gate 		dt_free(dtp, dof);
8900Sstevel@tonic-gate 		return (NULL);
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
8940Sstevel@tonic-gate 	dof->dofh_loadsz = len;
8950Sstevel@tonic-gate 	dof->dofh_filesz = len;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/*
8980Sstevel@tonic-gate 	 * Fill in the option section header...
8990Sstevel@tonic-gate 	 */
9000Sstevel@tonic-gate 	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
9010Sstevel@tonic-gate 	sec->dofs_type = DOF_SECT_OPTDESC;
9020Sstevel@tonic-gate 	sec->dofs_align = sizeof (uint64_t);
9030Sstevel@tonic-gate 	sec->dofs_flags = DOF_SECF_LOAD;
9040Sstevel@tonic-gate 	sec->dofs_entsize = sizeof (dof_optdesc_t);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	dofo = (dof_optdesc_t *)((uintptr_t)sec +
9070Sstevel@tonic-gate 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;
9100Sstevel@tonic-gate 	sec->dofs_size = sizeof (dof_optdesc_t) * nopts;
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 			continue;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 		dofo->dofo_option = i;
9170Sstevel@tonic-gate 		dofo->dofo_strtab = DOF_SECIDX_NONE;
9180Sstevel@tonic-gate 		dofo->dofo_value = dtp->dt_options[i];
9190Sstevel@tonic-gate 		dofo++;
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	return (dof);
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate void *
9260Sstevel@tonic-gate dtrace_geterr_dof(dtrace_hdl_t *dtp)
9270Sstevel@tonic-gate {
9280Sstevel@tonic-gate 	if (dtp->dt_errprog != NULL)
9290Sstevel@tonic-gate 		return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	(void) dt_set_errno(dtp, EDT_BADERROR);
9320Sstevel@tonic-gate 	return (NULL);
9330Sstevel@tonic-gate }
934