xref: /freebsd-src/cddl/contrib/opensolaris/lib/libdtrace/common/dt_dof.c (revision f9edb08480901b8c7d85837d72f8702008b0a773)
16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License (the "License").
66ff6d951SJohn Birrell  * You may not use this file except in compliance with the License.
76ff6d951SJohn Birrell  *
86ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
106ff6d951SJohn Birrell  * See the License for the specific language governing permissions
116ff6d951SJohn Birrell  * and limitations under the License.
126ff6d951SJohn Birrell  *
136ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
146ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
166ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
176ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
186ff6d951SJohn Birrell  *
196ff6d951SJohn Birrell  * CDDL HEADER END
206ff6d951SJohn Birrell  */
216ff6d951SJohn Birrell 
226ff6d951SJohn Birrell /*
231670a1c2SRui Paulo  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2454727873SPedro F. Giffuni  * Copyright (c) 2011 by Delphix. All rights reserved.
2523e4da43SPedro F. Giffuni  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
266ff6d951SJohn Birrell  */
276ff6d951SJohn Birrell 
286ff6d951SJohn Birrell #include <sys/types.h>
29bc96366cSSteven Hartland #ifdef illumos
306ff6d951SJohn Birrell #include <sys/sysmacros.h>
312c981f99SJohn Birrell #endif
326ff6d951SJohn Birrell 
336ff6d951SJohn Birrell #include <strings.h>
34bc96366cSSteven Hartland #ifdef illumos
356ff6d951SJohn Birrell #include <alloca.h>
362c981f99SJohn Birrell #endif
376ff6d951SJohn Birrell #include <assert.h>
386ff6d951SJohn Birrell #include <stdlib.h>
396ff6d951SJohn Birrell #include <errno.h>
406ff6d951SJohn Birrell #include <limits.h>
416ff6d951SJohn Birrell 
426ff6d951SJohn Birrell #include <dt_impl.h>
436ff6d951SJohn Birrell #include <dt_strtab.h>
446ff6d951SJohn Birrell #include <dt_program.h>
456ff6d951SJohn Birrell #include <dt_provider.h>
466ff6d951SJohn Birrell #include <dt_xlator.h>
476ff6d951SJohn Birrell #include <dt_dof.h>
486ff6d951SJohn Birrell 
496ff6d951SJohn Birrell void
dt_dof_init(dtrace_hdl_t * dtp)506ff6d951SJohn Birrell dt_dof_init(dtrace_hdl_t *dtp)
516ff6d951SJohn Birrell {
526ff6d951SJohn Birrell 	dt_dof_t *ddo = &dtp->dt_dof;
536ff6d951SJohn Birrell 
546ff6d951SJohn Birrell 	ddo->ddo_hdl = dtp;
556ff6d951SJohn Birrell 	ddo->ddo_nsecs = 0;
566ff6d951SJohn Birrell 	ddo->ddo_strsec = DOF_SECIDX_NONE;
576ff6d951SJohn Birrell 	ddo->ddo_xlimport = NULL;
586ff6d951SJohn Birrell 	ddo->ddo_xlexport = NULL;
596ff6d951SJohn Birrell 
606ff6d951SJohn Birrell 	dt_buf_create(dtp, &ddo->ddo_secs, "section headers", 0);
616ff6d951SJohn Birrell 	dt_buf_create(dtp, &ddo->ddo_strs, "string table", 0);
626ff6d951SJohn Birrell 	dt_buf_create(dtp, &ddo->ddo_ldata, "loadable data", 0);
636ff6d951SJohn Birrell 	dt_buf_create(dtp, &ddo->ddo_udata, "unloadable data", 0);
646ff6d951SJohn Birrell 
656ff6d951SJohn Birrell 	dt_buf_create(dtp, &ddo->ddo_probes, "probe data", 0);
666ff6d951SJohn Birrell 	dt_buf_create(dtp, &ddo->ddo_args, "probe args", 0);
676ff6d951SJohn Birrell 	dt_buf_create(dtp, &ddo->ddo_offs, "probe offs", 0);
686ff6d951SJohn Birrell 	dt_buf_create(dtp, &ddo->ddo_enoffs, "probe is-enabled offs", 0);
696ff6d951SJohn Birrell 	dt_buf_create(dtp, &ddo->ddo_rels, "probe rels", 0);
706ff6d951SJohn Birrell 
716ff6d951SJohn Birrell 	dt_buf_create(dtp, &ddo->ddo_xlms, "xlate members", 0);
726ff6d951SJohn Birrell }
736ff6d951SJohn Birrell 
746ff6d951SJohn Birrell void
dt_dof_fini(dtrace_hdl_t * dtp)756ff6d951SJohn Birrell dt_dof_fini(dtrace_hdl_t *dtp)
766ff6d951SJohn Birrell {
776ff6d951SJohn Birrell 	dt_dof_t *ddo = &dtp->dt_dof;
786ff6d951SJohn Birrell 
796ff6d951SJohn Birrell 	dt_free(dtp, ddo->ddo_xlimport);
806ff6d951SJohn Birrell 	dt_free(dtp, ddo->ddo_xlexport);
816ff6d951SJohn Birrell 
826ff6d951SJohn Birrell 	dt_buf_destroy(dtp, &ddo->ddo_secs);
836ff6d951SJohn Birrell 	dt_buf_destroy(dtp, &ddo->ddo_strs);
846ff6d951SJohn Birrell 	dt_buf_destroy(dtp, &ddo->ddo_ldata);
856ff6d951SJohn Birrell 	dt_buf_destroy(dtp, &ddo->ddo_udata);
866ff6d951SJohn Birrell 
876ff6d951SJohn Birrell 	dt_buf_destroy(dtp, &ddo->ddo_probes);
886ff6d951SJohn Birrell 	dt_buf_destroy(dtp, &ddo->ddo_args);
896ff6d951SJohn Birrell 	dt_buf_destroy(dtp, &ddo->ddo_offs);
906ff6d951SJohn Birrell 	dt_buf_destroy(dtp, &ddo->ddo_enoffs);
916ff6d951SJohn Birrell 	dt_buf_destroy(dtp, &ddo->ddo_rels);
926ff6d951SJohn Birrell 
936ff6d951SJohn Birrell 	dt_buf_destroy(dtp, &ddo->ddo_xlms);
946ff6d951SJohn Birrell }
956ff6d951SJohn Birrell 
966ff6d951SJohn Birrell static int
dt_dof_reset(dtrace_hdl_t * dtp,dtrace_prog_t * pgp)976ff6d951SJohn Birrell dt_dof_reset(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
986ff6d951SJohn Birrell {
996ff6d951SJohn Birrell 	dt_dof_t *ddo = &dtp->dt_dof;
1006ff6d951SJohn Birrell 	uint_t i, nx = dtp->dt_xlatorid;
1016ff6d951SJohn Birrell 
1026ff6d951SJohn Birrell 	assert(ddo->ddo_hdl == dtp);
1036ff6d951SJohn Birrell 	ddo->ddo_pgp = pgp;
1046ff6d951SJohn Birrell 
1056ff6d951SJohn Birrell 	ddo->ddo_nsecs = 0;
1066ff6d951SJohn Birrell 	ddo->ddo_strsec = DOF_SECIDX_NONE;
1076ff6d951SJohn Birrell 
1086ff6d951SJohn Birrell 	dt_free(dtp, ddo->ddo_xlimport);
1096ff6d951SJohn Birrell 	dt_free(dtp, ddo->ddo_xlexport);
1106ff6d951SJohn Birrell 
1116ff6d951SJohn Birrell 	ddo->ddo_xlimport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
1126ff6d951SJohn Birrell 	ddo->ddo_xlexport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
1136ff6d951SJohn Birrell 
1146ff6d951SJohn Birrell 	if (nx != 0 && (ddo->ddo_xlimport == NULL || ddo->ddo_xlexport == NULL))
1156ff6d951SJohn Birrell 		return (-1); /* errno is set for us */
1166ff6d951SJohn Birrell 
1176ff6d951SJohn Birrell 	for (i = 0; i < nx; i++) {
1186ff6d951SJohn Birrell 		ddo->ddo_xlimport[i] = DOF_SECIDX_NONE;
1196ff6d951SJohn Birrell 		ddo->ddo_xlexport[i] = DOF_SECIDX_NONE;
1206ff6d951SJohn Birrell 	}
1216ff6d951SJohn Birrell 
1226ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_secs);
1236ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_strs);
1246ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_ldata);
1256ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_udata);
1266ff6d951SJohn Birrell 
1276ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_probes);
1286ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_args);
1296ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_offs);
1306ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_enoffs);
1316ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_rels);
1326ff6d951SJohn Birrell 
1336ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_xlms);
1346ff6d951SJohn Birrell 	return (0);
1356ff6d951SJohn Birrell }
1366ff6d951SJohn Birrell 
1376ff6d951SJohn Birrell /*
1386ff6d951SJohn Birrell  * Add a loadable DOF section to the file using the specified data buffer and
1396ff6d951SJohn Birrell  * the specified DOF section attributes.  DOF_SECF_LOAD must be set in flags.
1406ff6d951SJohn Birrell  * If 'data' is NULL, the caller is responsible for manipulating the ldata buf.
1416ff6d951SJohn Birrell  */
1426ff6d951SJohn Birrell static dof_secidx_t
dof_add_lsect(dt_dof_t * ddo,const void * data,uint32_t type,uint32_t align,uint32_t flags,uint32_t entsize,uint64_t size)1436ff6d951SJohn Birrell dof_add_lsect(dt_dof_t *ddo, const void *data, uint32_t type,
1446ff6d951SJohn Birrell     uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
1456ff6d951SJohn Birrell {
1466ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
1476ff6d951SJohn Birrell 	dof_sec_t s;
1486ff6d951SJohn Birrell 
1496ff6d951SJohn Birrell 	s.dofs_type = type;
1506ff6d951SJohn Birrell 	s.dofs_align = align;
1516ff6d951SJohn Birrell 	s.dofs_flags = flags | DOF_SECF_LOAD;
1526ff6d951SJohn Birrell 	s.dofs_entsize = entsize;
1536ff6d951SJohn Birrell 	s.dofs_offset = dt_buf_offset(&ddo->ddo_ldata, align);
1546ff6d951SJohn Birrell 	s.dofs_size = size;
1556ff6d951SJohn Birrell 
1566ff6d951SJohn Birrell 	dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
1576ff6d951SJohn Birrell 
1586ff6d951SJohn Birrell 	if (data != NULL)
1596ff6d951SJohn Birrell 		dt_buf_write(dtp, &ddo->ddo_ldata, data, size, align);
1606ff6d951SJohn Birrell 
1616ff6d951SJohn Birrell 	return (ddo->ddo_nsecs++);
1626ff6d951SJohn Birrell }
1636ff6d951SJohn Birrell 
1646ff6d951SJohn Birrell /*
1656ff6d951SJohn Birrell  * Add an unloadable DOF section to the file using the specified data buffer
1666ff6d951SJohn Birrell  * and DOF section attributes.  DOF_SECF_LOAD must *not* be set in flags.
1676ff6d951SJohn Birrell  * If 'data' is NULL, the caller is responsible for manipulating the udata buf.
1686ff6d951SJohn Birrell  */
1696ff6d951SJohn Birrell static dof_secidx_t
dof_add_usect(dt_dof_t * ddo,const void * data,uint32_t type,uint32_t align,uint32_t flags,uint32_t entsize,uint64_t size)1706ff6d951SJohn Birrell dof_add_usect(dt_dof_t *ddo, const void *data, uint32_t type,
1716ff6d951SJohn Birrell     uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
1726ff6d951SJohn Birrell {
1736ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
1746ff6d951SJohn Birrell 	dof_sec_t s;
1756ff6d951SJohn Birrell 
1766ff6d951SJohn Birrell 	s.dofs_type = type;
1776ff6d951SJohn Birrell 	s.dofs_align = align;
1786ff6d951SJohn Birrell 	s.dofs_flags = flags & ~DOF_SECF_LOAD;
1796ff6d951SJohn Birrell 	s.dofs_entsize = entsize;
1806ff6d951SJohn Birrell 	s.dofs_offset = dt_buf_offset(&ddo->ddo_udata, align);
1816ff6d951SJohn Birrell 	s.dofs_size = size;
1826ff6d951SJohn Birrell 
1836ff6d951SJohn Birrell 	dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
1846ff6d951SJohn Birrell 
1856ff6d951SJohn Birrell 	if (data != NULL)
1866ff6d951SJohn Birrell 		dt_buf_write(dtp, &ddo->ddo_udata, data, size, align);
1876ff6d951SJohn Birrell 
1886ff6d951SJohn Birrell 	return (ddo->ddo_nsecs++);
1896ff6d951SJohn Birrell }
1906ff6d951SJohn Birrell 
1916ff6d951SJohn Birrell /*
1926ff6d951SJohn Birrell  * Add a string to the global string table associated with the DOF.  The offset
1936ff6d951SJohn Birrell  * of the string is returned as an index into the string table.
1946ff6d951SJohn Birrell  */
1956ff6d951SJohn Birrell static dof_stridx_t
dof_add_string(dt_dof_t * ddo,const char * s)1966ff6d951SJohn Birrell dof_add_string(dt_dof_t *ddo, const char *s)
1976ff6d951SJohn Birrell {
1986ff6d951SJohn Birrell 	dt_buf_t *bp = &ddo->ddo_strs;
1996ff6d951SJohn Birrell 	dof_stridx_t i = dt_buf_len(bp);
2006ff6d951SJohn Birrell 
2016ff6d951SJohn Birrell 	if (i != 0 && (s == NULL || *s == '\0'))
2026ff6d951SJohn Birrell 		return (0); /* string table has \0 at offset 0 */
2036ff6d951SJohn Birrell 
2046ff6d951SJohn Birrell 	dt_buf_write(ddo->ddo_hdl, bp, s, strlen(s) + 1, sizeof (char));
2056ff6d951SJohn Birrell 	return (i);
2066ff6d951SJohn Birrell }
2076ff6d951SJohn Birrell 
2086ff6d951SJohn Birrell static dof_attr_t
dof_attr(const dtrace_attribute_t * ap)2096ff6d951SJohn Birrell dof_attr(const dtrace_attribute_t *ap)
2106ff6d951SJohn Birrell {
2116ff6d951SJohn Birrell 	return (DOF_ATTR(ap->dtat_name, ap->dtat_data, ap->dtat_class));
2126ff6d951SJohn Birrell }
2136ff6d951SJohn Birrell 
2146ff6d951SJohn Birrell static dof_secidx_t
dof_add_difo(dt_dof_t * ddo,const dtrace_difo_t * dp)2156ff6d951SJohn Birrell dof_add_difo(dt_dof_t *ddo, const dtrace_difo_t *dp)
2166ff6d951SJohn Birrell {
2176ff6d951SJohn Birrell 	dof_secidx_t dsecs[5]; /* enough for all possible DIFO sections */
2186ff6d951SJohn Birrell 	uint_t nsecs = 0;
2196ff6d951SJohn Birrell 
2206ff6d951SJohn Birrell 	dof_difohdr_t *dofd;
2216ff6d951SJohn Birrell 	dof_relohdr_t dofr;
2226ff6d951SJohn Birrell 	dof_secidx_t relsec;
2236ff6d951SJohn Birrell 
2246ff6d951SJohn Birrell 	dof_secidx_t strsec = DOF_SECIDX_NONE;
2256ff6d951SJohn Birrell 	dof_secidx_t intsec = DOF_SECIDX_NONE;
2266ff6d951SJohn Birrell 	dof_secidx_t hdrsec = DOF_SECIDX_NONE;
2276ff6d951SJohn Birrell 
2286ff6d951SJohn Birrell 	if (dp->dtdo_buf != NULL) {
2296ff6d951SJohn Birrell 		dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_buf,
2306ff6d951SJohn Birrell 		    DOF_SECT_DIF, sizeof (dif_instr_t), 0,
2316ff6d951SJohn Birrell 		    sizeof (dif_instr_t), sizeof (dif_instr_t) * dp->dtdo_len);
2326ff6d951SJohn Birrell 	}
2336ff6d951SJohn Birrell 
2346ff6d951SJohn Birrell 	if (dp->dtdo_inttab != NULL) {
2356ff6d951SJohn Birrell 		dsecs[nsecs++] = intsec = dof_add_lsect(ddo, dp->dtdo_inttab,
2366ff6d951SJohn Birrell 		    DOF_SECT_INTTAB, sizeof (uint64_t), 0,
2376ff6d951SJohn Birrell 		    sizeof (uint64_t), sizeof (uint64_t) * dp->dtdo_intlen);
2386ff6d951SJohn Birrell 	}
2396ff6d951SJohn Birrell 
2406ff6d951SJohn Birrell 	if (dp->dtdo_strtab != NULL) {
2416ff6d951SJohn Birrell 		dsecs[nsecs++] = strsec = dof_add_lsect(ddo, dp->dtdo_strtab,
2426ff6d951SJohn Birrell 		    DOF_SECT_STRTAB, sizeof (char), 0, 0, dp->dtdo_strlen);
2436ff6d951SJohn Birrell 	}
2446ff6d951SJohn Birrell 
2456ff6d951SJohn Birrell 	if (dp->dtdo_vartab != NULL) {
2466ff6d951SJohn Birrell 		dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_vartab,
2476ff6d951SJohn Birrell 		    DOF_SECT_VARTAB, sizeof (uint_t), 0, sizeof (dtrace_difv_t),
2486ff6d951SJohn Birrell 		    sizeof (dtrace_difv_t) * dp->dtdo_varlen);
2496ff6d951SJohn Birrell 	}
2506ff6d951SJohn Birrell 
2516ff6d951SJohn Birrell 	if (dp->dtdo_xlmtab != NULL) {
2526ff6d951SJohn Birrell 		dof_xlref_t *xlt, *xlp;
2536ff6d951SJohn Birrell 		dt_node_t **pnp;
2546ff6d951SJohn Birrell 
2556ff6d951SJohn Birrell 		xlt = alloca(sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
2566ff6d951SJohn Birrell 		pnp = dp->dtdo_xlmtab;
2576ff6d951SJohn Birrell 
2586ff6d951SJohn Birrell 		/*
2596ff6d951SJohn Birrell 		 * dtdo_xlmtab contains pointers to the translator members.
2606ff6d951SJohn Birrell 		 * The translator itself is in sect ddo_xlimport[dxp->dx_id].
2616ff6d951SJohn Birrell 		 * The XLMEMBERS entries are in order by their dn_membid, so
2626ff6d951SJohn Birrell 		 * the member section offset is the population count of bits
2636ff6d951SJohn Birrell 		 * in ddo_pgp->dp_xlrefs[] up to and not including dn_membid.
2646ff6d951SJohn Birrell 		 */
2656ff6d951SJohn Birrell 		for (xlp = xlt; xlp < xlt + dp->dtdo_xlmlen; xlp++) {
2666ff6d951SJohn Birrell 			dt_node_t *dnp = *pnp++;
2676ff6d951SJohn Birrell 			dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
2686ff6d951SJohn Birrell 
2696ff6d951SJohn Birrell 			xlp->dofxr_xlator = ddo->ddo_xlimport[dxp->dx_id];
2706ff6d951SJohn Birrell 			xlp->dofxr_member = dt_popcb(
2716ff6d951SJohn Birrell 			    ddo->ddo_pgp->dp_xrefs[dxp->dx_id], dnp->dn_membid);
2726ff6d951SJohn Birrell 			xlp->dofxr_argn = (uint32_t)dxp->dx_arg;
2736ff6d951SJohn Birrell 		}
2746ff6d951SJohn Birrell 
2756ff6d951SJohn Birrell 		dsecs[nsecs++] = dof_add_lsect(ddo, xlt, DOF_SECT_XLTAB,
2766ff6d951SJohn Birrell 		    sizeof (dof_secidx_t), 0, sizeof (dof_xlref_t),
2776ff6d951SJohn Birrell 		    sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
2786ff6d951SJohn Birrell 	}
2796ff6d951SJohn Birrell 
2806ff6d951SJohn Birrell 	/*
2816ff6d951SJohn Birrell 	 * Copy the return type and the array of section indices that form the
2826ff6d951SJohn Birrell 	 * DIFO into a single dof_difohdr_t and then add DOF_SECT_DIFOHDR.
2836ff6d951SJohn Birrell 	 */
2846ff6d951SJohn Birrell 	assert(nsecs <= sizeof (dsecs) / sizeof (dsecs[0]));
2856ff6d951SJohn Birrell 	dofd = alloca(sizeof (dtrace_diftype_t) + sizeof (dsecs));
2866ff6d951SJohn Birrell 	bcopy(&dp->dtdo_rtype, &dofd->dofd_rtype, sizeof (dtrace_diftype_t));
2876ff6d951SJohn Birrell 	bcopy(dsecs, &dofd->dofd_links, sizeof (dof_secidx_t) * nsecs);
2886ff6d951SJohn Birrell 
2896ff6d951SJohn Birrell 	hdrsec = dof_add_lsect(ddo, dofd, DOF_SECT_DIFOHDR,
2906ff6d951SJohn Birrell 	    sizeof (dof_secidx_t), 0, 0,
2916ff6d951SJohn Birrell 	    sizeof (dtrace_diftype_t) + sizeof (dof_secidx_t) * nsecs);
2926ff6d951SJohn Birrell 
2936ff6d951SJohn Birrell 	/*
2946ff6d951SJohn Birrell 	 * Add any other sections related to dtrace_difo_t.  These are not
2956ff6d951SJohn Birrell 	 * referenced in dof_difohdr_t because they are not used by emulation.
2966ff6d951SJohn Birrell 	 */
2976ff6d951SJohn Birrell 	if (dp->dtdo_kreltab != NULL) {
2986ff6d951SJohn Birrell 		relsec = dof_add_lsect(ddo, dp->dtdo_kreltab, DOF_SECT_RELTAB,
2996ff6d951SJohn Birrell 		    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
3006ff6d951SJohn Birrell 		    sizeof (dof_relodesc_t) * dp->dtdo_krelen);
3016ff6d951SJohn Birrell 
3026ff6d951SJohn Birrell 		/*
3036ff6d951SJohn Birrell 		 * This code assumes the target of all relocations is the
3046ff6d951SJohn Birrell 		 * integer table 'intsec' (DOF_SECT_INTTAB).  If other sections
3056ff6d951SJohn Birrell 		 * need relocation in the future this will need to change.
3066ff6d951SJohn Birrell 		 */
3076ff6d951SJohn Birrell 		dofr.dofr_strtab = strsec;
3086ff6d951SJohn Birrell 		dofr.dofr_relsec = relsec;
3096ff6d951SJohn Birrell 		dofr.dofr_tgtsec = intsec;
3106ff6d951SJohn Birrell 
3116ff6d951SJohn Birrell 		(void) dof_add_lsect(ddo, &dofr, DOF_SECT_KRELHDR,
3126ff6d951SJohn Birrell 		    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
3136ff6d951SJohn Birrell 	}
3146ff6d951SJohn Birrell 
3156ff6d951SJohn Birrell 	if (dp->dtdo_ureltab != NULL) {
3166ff6d951SJohn Birrell 		relsec = dof_add_lsect(ddo, dp->dtdo_ureltab, DOF_SECT_RELTAB,
3176ff6d951SJohn Birrell 		    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
3186ff6d951SJohn Birrell 		    sizeof (dof_relodesc_t) * dp->dtdo_urelen);
3196ff6d951SJohn Birrell 
3206ff6d951SJohn Birrell 		/*
3216ff6d951SJohn Birrell 		 * This code assumes the target of all relocations is the
3226ff6d951SJohn Birrell 		 * integer table 'intsec' (DOF_SECT_INTTAB).  If other sections
3236ff6d951SJohn Birrell 		 * need relocation in the future this will need to change.
3246ff6d951SJohn Birrell 		 */
3256ff6d951SJohn Birrell 		dofr.dofr_strtab = strsec;
3266ff6d951SJohn Birrell 		dofr.dofr_relsec = relsec;
3276ff6d951SJohn Birrell 		dofr.dofr_tgtsec = intsec;
3286ff6d951SJohn Birrell 
3296ff6d951SJohn Birrell 		(void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
3306ff6d951SJohn Birrell 		    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
3316ff6d951SJohn Birrell 	}
3326ff6d951SJohn Birrell 
3336ff6d951SJohn Birrell 	return (hdrsec);
3346ff6d951SJohn Birrell }
3356ff6d951SJohn Birrell 
3366ff6d951SJohn Birrell static void
dof_add_translator(dt_dof_t * ddo,const dt_xlator_t * dxp,uint_t type)3376ff6d951SJohn Birrell dof_add_translator(dt_dof_t *ddo, const dt_xlator_t *dxp, uint_t type)
3386ff6d951SJohn Birrell {
3396ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
3406ff6d951SJohn Birrell 	dof_xlmember_t dofxm;
3416ff6d951SJohn Birrell 	dof_xlator_t dofxl;
3426ff6d951SJohn Birrell 	dof_secidx_t *xst;
3436ff6d951SJohn Birrell 
3446ff6d951SJohn Birrell 	char buf[DT_TYPE_NAMELEN];
3456ff6d951SJohn Birrell 	dt_node_t *dnp;
3466ff6d951SJohn Birrell 	uint_t i = 0;
3476ff6d951SJohn Birrell 
3486ff6d951SJohn Birrell 	assert(type == DOF_SECT_XLIMPORT || type == DOF_SECT_XLEXPORT);
3496ff6d951SJohn Birrell 	xst = type == DOF_SECT_XLIMPORT ? ddo->ddo_xlimport : ddo->ddo_xlexport;
3506ff6d951SJohn Birrell 
3516ff6d951SJohn Birrell 	if (xst[dxp->dx_id] != DOF_SECIDX_NONE)
3526ff6d951SJohn Birrell 		return; /* translator has already been emitted */
3536ff6d951SJohn Birrell 
3546ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_xlms);
3556ff6d951SJohn Birrell 
3566ff6d951SJohn Birrell 	/*
3576ff6d951SJohn Birrell 	 * Generate an array of dof_xlmember_t's into ddo_xlms.  If we are
3586ff6d951SJohn Birrell 	 * importing the translator, add only those members referenced by the
3596ff6d951SJohn Birrell 	 * program and set the dofxm_difo reference of each member to NONE.  If
3606ff6d951SJohn Birrell 	 * we're exporting the translator, add all members and a DIFO for each.
3616ff6d951SJohn Birrell 	 */
3626ff6d951SJohn Birrell 	for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list, i++) {
3636ff6d951SJohn Birrell 		if (type == DOF_SECT_XLIMPORT) {
3646ff6d951SJohn Birrell 			if (!BT_TEST(ddo->ddo_pgp->dp_xrefs[dxp->dx_id], i))
3656ff6d951SJohn Birrell 				continue; /* member is not referenced */
3666ff6d951SJohn Birrell 			dofxm.dofxm_difo = DOF_SECIDX_NONE;
3676ff6d951SJohn Birrell 		} else {
3686ff6d951SJohn Birrell 			dofxm.dofxm_difo = dof_add_difo(ddo,
3696ff6d951SJohn Birrell 			    dxp->dx_membdif[dnp->dn_membid]);
3706ff6d951SJohn Birrell 		}
3716ff6d951SJohn Birrell 
3726ff6d951SJohn Birrell 		dofxm.dofxm_name = dof_add_string(ddo, dnp->dn_membname);
3736ff6d951SJohn Birrell 		dt_node_diftype(dtp, dnp, &dofxm.dofxm_type);
3746ff6d951SJohn Birrell 
3756ff6d951SJohn Birrell 		dt_buf_write(dtp, &ddo->ddo_xlms,
3766ff6d951SJohn Birrell 		    &dofxm, sizeof (dofxm), sizeof (uint32_t));
3776ff6d951SJohn Birrell 	}
3786ff6d951SJohn Birrell 
3796ff6d951SJohn Birrell 	dofxl.dofxl_members = dof_add_lsect(ddo, NULL, DOF_SECT_XLMEMBERS,
3806ff6d951SJohn Birrell 	    sizeof (uint32_t), 0, sizeof (dofxm), dt_buf_len(&ddo->ddo_xlms));
3816ff6d951SJohn Birrell 
3826ff6d951SJohn Birrell 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_xlms, sizeof (uint32_t));
3836ff6d951SJohn Birrell 
3846ff6d951SJohn Birrell 	dofxl.dofxl_strtab = ddo->ddo_strsec;
3856ff6d951SJohn Birrell 	dofxl.dofxl_argv = dof_add_string(ddo, ctf_type_name(
3866ff6d951SJohn Birrell 	    dxp->dx_src_ctfp, dxp->dx_src_type, buf, sizeof (buf)));
3876ff6d951SJohn Birrell 	dofxl.dofxl_argc = 1;
3886ff6d951SJohn Birrell 	dofxl.dofxl_type = dof_add_string(ddo, ctf_type_name(
3896ff6d951SJohn Birrell 	    dxp->dx_dst_ctfp, dxp->dx_dst_type, buf, sizeof (buf)));
3906ff6d951SJohn Birrell 	dofxl.dofxl_attr = dof_attr(&dxp->dx_souid.di_attr);
3916ff6d951SJohn Birrell 
3926ff6d951SJohn Birrell 	xst[dxp->dx_id] = dof_add_lsect(ddo, &dofxl, type,
3936ff6d951SJohn Birrell 	    sizeof (uint32_t), 0, 0, sizeof (dofxl));
3946ff6d951SJohn Birrell }
3956ff6d951SJohn Birrell 
3966ff6d951SJohn Birrell /*ARGSUSED*/
3976ff6d951SJohn Birrell static int
dof_add_probe(dt_idhash_t * dhp,dt_ident_t * idp,void * data)3986ff6d951SJohn Birrell dof_add_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
3996ff6d951SJohn Birrell {
4006ff6d951SJohn Birrell 	dt_dof_t *ddo = data;
4016ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
4026ff6d951SJohn Birrell 	dt_probe_t *prp = idp->di_data;
4036ff6d951SJohn Birrell 
4046ff6d951SJohn Birrell 	dof_probe_t dofpr;
4056ff6d951SJohn Birrell 	dof_relodesc_t dofr;
4066ff6d951SJohn Birrell 	dt_probe_instance_t *pip;
4076ff6d951SJohn Birrell 	dt_node_t *dnp;
4086ff6d951SJohn Birrell 
4096ff6d951SJohn Birrell 	char buf[DT_TYPE_NAMELEN];
4106ff6d951SJohn Birrell 	uint_t i;
4116ff6d951SJohn Birrell 
4126ff6d951SJohn Birrell 	dofpr.dofpr_addr = 0;
4136ff6d951SJohn Birrell 	dofpr.dofpr_name = dof_add_string(ddo, prp->pr_name);
4146ff6d951SJohn Birrell 	dofpr.dofpr_nargv = dt_buf_len(&ddo->ddo_strs);
4156ff6d951SJohn Birrell 
4166ff6d951SJohn Birrell 	for (dnp = prp->pr_nargs; dnp != NULL; dnp = dnp->dn_list) {
4176ff6d951SJohn Birrell 		(void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
4186ff6d951SJohn Birrell 		    dnp->dn_type, buf, sizeof (buf)));
4196ff6d951SJohn Birrell 	}
4206ff6d951SJohn Birrell 
4216ff6d951SJohn Birrell 	dofpr.dofpr_xargv = dt_buf_len(&ddo->ddo_strs);
4226ff6d951SJohn Birrell 
4236ff6d951SJohn Birrell 	for (dnp = prp->pr_xargs; dnp != NULL; dnp = dnp->dn_list) {
4246ff6d951SJohn Birrell 		(void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
4256ff6d951SJohn Birrell 		    dnp->dn_type, buf, sizeof (buf)));
4266ff6d951SJohn Birrell 	}
4276ff6d951SJohn Birrell 
4286ff6d951SJohn Birrell 	dofpr.dofpr_argidx = dt_buf_len(&ddo->ddo_args) / sizeof (uint8_t);
4296ff6d951SJohn Birrell 
4306ff6d951SJohn Birrell 	for (i = 0; i < prp->pr_xargc; i++) {
4316ff6d951SJohn Birrell 		dt_buf_write(dtp, &ddo->ddo_args, &prp->pr_mapping[i],
4326ff6d951SJohn Birrell 		    sizeof (uint8_t), sizeof (uint8_t));
4336ff6d951SJohn Birrell 	}
4346ff6d951SJohn Birrell 
4356ff6d951SJohn Birrell 	dofpr.dofpr_nargc = prp->pr_nargc;
4366ff6d951SJohn Birrell 	dofpr.dofpr_xargc = prp->pr_xargc;
4376ff6d951SJohn Birrell 	dofpr.dofpr_pad1 = 0;
4386ff6d951SJohn Birrell 	dofpr.dofpr_pad2 = 0;
4396ff6d951SJohn Birrell 
4406ff6d951SJohn Birrell 	for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
4416ff6d951SJohn Birrell 		dt_dprintf("adding probe for %s:%s\n", pip->pi_fname,
4426ff6d951SJohn Birrell 		    prp->pr_name);
4436ff6d951SJohn Birrell 
4446ff6d951SJohn Birrell 		dofpr.dofpr_func = dof_add_string(ddo, pip->pi_fname);
4456ff6d951SJohn Birrell 
4466ff6d951SJohn Birrell 		/*
4476ff6d951SJohn Birrell 		 * There should be one probe offset or is-enabled probe offset
4486ff6d951SJohn Birrell 		 * or else this probe instance won't have been created. The
4496ff6d951SJohn Birrell 		 * kernel will reject DOF which has a probe with no offsets.
4506ff6d951SJohn Birrell 		 */
4516ff6d951SJohn Birrell 		assert(pip->pi_noffs + pip->pi_nenoffs > 0);
4526ff6d951SJohn Birrell 
4536ff6d951SJohn Birrell 		dofpr.dofpr_offidx =
4546ff6d951SJohn Birrell 		    dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t);
4556ff6d951SJohn Birrell 		dofpr.dofpr_noffs = pip->pi_noffs;
4566ff6d951SJohn Birrell 		dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs,
4576ff6d951SJohn Birrell 		    pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
4586ff6d951SJohn Birrell 
4596ff6d951SJohn Birrell 		dofpr.dofpr_enoffidx =
4606ff6d951SJohn Birrell 		    dt_buf_len(&ddo->ddo_enoffs) / sizeof (uint32_t);
4616ff6d951SJohn Birrell 		dofpr.dofpr_nenoffs = pip->pi_nenoffs;
4626ff6d951SJohn Birrell 		dt_buf_write(dtp, &ddo->ddo_enoffs, pip->pi_enoffs,
4636ff6d951SJohn Birrell 		    pip->pi_nenoffs * sizeof (uint32_t), sizeof (uint32_t));
4646ff6d951SJohn Birrell 
4656ff6d951SJohn Birrell 		dofr.dofr_name = dof_add_string(ddo, pip->pi_rname);
466*e801af6fSMark Johnston 		dofr.dofr_type = DOF_RELO_DOFREL;
4676ff6d951SJohn Birrell 		dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes);
4686ff6d951SJohn Birrell 		dofr.dofr_data = 0;
4696ff6d951SJohn Birrell 
4706ff6d951SJohn Birrell 		dt_buf_write(dtp, &ddo->ddo_rels, &dofr,
4716ff6d951SJohn Birrell 		    sizeof (dofr), sizeof (uint64_t));
4726ff6d951SJohn Birrell 
4736ff6d951SJohn Birrell 		dt_buf_write(dtp, &ddo->ddo_probes, &dofpr,
4746ff6d951SJohn Birrell 		    sizeof (dofpr), sizeof (uint64_t));
4756ff6d951SJohn Birrell 	}
4766ff6d951SJohn Birrell 
4776ff6d951SJohn Birrell 	return (0);
4786ff6d951SJohn Birrell }
4796ff6d951SJohn Birrell 
48023e4da43SPedro F. Giffuni static int
dof_add_provider(dt_dof_t * ddo,const dt_provider_t * pvp)4816ff6d951SJohn Birrell dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp)
4826ff6d951SJohn Birrell {
4836ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
4846ff6d951SJohn Birrell 	dof_provider_t dofpv;
4856ff6d951SJohn Birrell 	dof_relohdr_t dofr;
4866ff6d951SJohn Birrell 	dof_secidx_t *dofs;
4876ff6d951SJohn Birrell 	ulong_t xr, nxr;
4886ff6d951SJohn Birrell 	size_t sz;
4896ff6d951SJohn Birrell 	id_t i;
4906ff6d951SJohn Birrell 
49123e4da43SPedro F. Giffuni 	if (pvp->pv_flags & DT_PROVIDER_IMPL) {
49223e4da43SPedro F. Giffuni 		/*
49323e4da43SPedro F. Giffuni 		 * ignore providers that are exported by dtrace(7D)
49423e4da43SPedro F. Giffuni 		 */
49523e4da43SPedro F. Giffuni 		return (0);
49623e4da43SPedro F. Giffuni 	}
4976ff6d951SJohn Birrell 
4986ff6d951SJohn Birrell 	nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax);
4996ff6d951SJohn Birrell 	dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1));
5006ff6d951SJohn Birrell 	xr = 1; /* reserve dofs[0] for the provider itself */
5016ff6d951SJohn Birrell 
5026ff6d951SJohn Birrell 	/*
5036ff6d951SJohn Birrell 	 * For each translator referenced by the provider (pv_xrefs), emit an
5046ff6d951SJohn Birrell 	 * exported translator section for it if one hasn't been created yet.
5056ff6d951SJohn Birrell 	 */
5066ff6d951SJohn Birrell 	for (i = 0; i < pvp->pv_xrmax; i++) {
5076ff6d951SJohn Birrell 		if (BT_TEST(pvp->pv_xrefs, i) &&
5086ff6d951SJohn Birrell 		    dtp->dt_xlatemode == DT_XL_DYNAMIC) {
5096ff6d951SJohn Birrell 			dof_add_translator(ddo,
5106ff6d951SJohn Birrell 			    dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT);
5116ff6d951SJohn Birrell 			dofs[xr++] = ddo->ddo_xlexport[i];
5126ff6d951SJohn Birrell 		}
5136ff6d951SJohn Birrell 	}
5146ff6d951SJohn Birrell 
5156ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_probes);
5166ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_args);
5176ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_offs);
5186ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_enoffs);
5196ff6d951SJohn Birrell 	dt_buf_reset(dtp, &ddo->ddo_rels);
5206ff6d951SJohn Birrell 
5216ff6d951SJohn Birrell 	(void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo);
5226ff6d951SJohn Birrell 
52323e4da43SPedro F. Giffuni 	if (dt_buf_len(&ddo->ddo_probes) == 0)
52423e4da43SPedro F. Giffuni 		return (dt_set_errno(dtp, EDT_NOPROBES));
52523e4da43SPedro F. Giffuni 
5266ff6d951SJohn Birrell 	dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES,
5276ff6d951SJohn Birrell 	    sizeof (uint64_t), 0, sizeof (dof_probe_t),
5286ff6d951SJohn Birrell 	    dt_buf_len(&ddo->ddo_probes));
5296ff6d951SJohn Birrell 
5306ff6d951SJohn Birrell 	dt_buf_concat(dtp, &ddo->ddo_ldata,
5316ff6d951SJohn Birrell 	    &ddo->ddo_probes, sizeof (uint64_t));
5326ff6d951SJohn Birrell 
5336ff6d951SJohn Birrell 	dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS,
5346ff6d951SJohn Birrell 	    sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args));
5356ff6d951SJohn Birrell 
5366ff6d951SJohn Birrell 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t));
5376ff6d951SJohn Birrell 
5386ff6d951SJohn Birrell 	dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS,
5396ff6d951SJohn Birrell 	    sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs));
5406ff6d951SJohn Birrell 
5416ff6d951SJohn Birrell 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t));
5426ff6d951SJohn Birrell 
5436ff6d951SJohn Birrell 	if ((sz = dt_buf_len(&ddo->ddo_enoffs)) != 0) {
5446ff6d951SJohn Birrell 		dofpv.dofpv_prenoffs = dof_add_lsect(ddo, NULL,
5456ff6d951SJohn Birrell 		    DOF_SECT_PRENOFFS, sizeof (uint_t), 0, sizeof (uint_t), sz);
5466ff6d951SJohn Birrell 	} else {
5476ff6d951SJohn Birrell 		dofpv.dofpv_prenoffs = DOF_SECT_NONE;
5486ff6d951SJohn Birrell 	}
5496ff6d951SJohn Birrell 
5506ff6d951SJohn Birrell 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_enoffs, sizeof (uint_t));
5516ff6d951SJohn Birrell 
5526ff6d951SJohn Birrell 	dofpv.dofpv_strtab = ddo->ddo_strsec;
5536ff6d951SJohn Birrell 	dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name);
5546ff6d951SJohn Birrell 
5556ff6d951SJohn Birrell 	dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider);
5566ff6d951SJohn Birrell 	dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod);
5576ff6d951SJohn Birrell 	dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func);
5586ff6d951SJohn Birrell 	dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name);
5596ff6d951SJohn Birrell 	dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args);
5606ff6d951SJohn Birrell 
5616ff6d951SJohn Birrell 	dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER,
5626ff6d951SJohn Birrell 	    sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t));
5636ff6d951SJohn Birrell 
5646ff6d951SJohn Birrell 	dofr.dofr_strtab = dofpv.dofpv_strtab;
5656ff6d951SJohn Birrell 	dofr.dofr_tgtsec = dofpv.dofpv_probes;
5666ff6d951SJohn Birrell 	dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB,
5676ff6d951SJohn Birrell 	    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
5686ff6d951SJohn Birrell 	    dt_buf_len(&ddo->ddo_rels));
5696ff6d951SJohn Birrell 
5706ff6d951SJohn Birrell 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t));
5716ff6d951SJohn Birrell 
5726ff6d951SJohn Birrell 	(void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
5736ff6d951SJohn Birrell 	    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
5746ff6d951SJohn Birrell 
5756ff6d951SJohn Birrell 	if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) {
5766ff6d951SJohn Birrell 		(void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT,
5776ff6d951SJohn Birrell 		    sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t),
5786ff6d951SJohn Birrell 		    sizeof (dof_secidx_t) * (nxr + 1));
5796ff6d951SJohn Birrell 	}
58023e4da43SPedro F. Giffuni 
58123e4da43SPedro F. Giffuni 	return (0);
5826ff6d951SJohn Birrell }
5836ff6d951SJohn Birrell 
5846ff6d951SJohn Birrell static int
dof_hdr(dtrace_hdl_t * dtp,uint8_t dofversion,dof_hdr_t * hp)5856ff6d951SJohn Birrell dof_hdr(dtrace_hdl_t *dtp, uint8_t dofversion, dof_hdr_t *hp)
5866ff6d951SJohn Birrell {
5876ff6d951SJohn Birrell 	/*
5886ff6d951SJohn Birrell 	 * If our config values cannot fit in a uint8_t, we can't generate a
5896ff6d951SJohn Birrell 	 * DOF header since the values won't fit.  This can only happen if the
5906ff6d951SJohn Birrell 	 * user forcibly compiles a program with an artificial configuration.
5916ff6d951SJohn Birrell 	 */
5926ff6d951SJohn Birrell 	if (dtp->dt_conf.dtc_difversion > UINT8_MAX ||
5936ff6d951SJohn Birrell 	    dtp->dt_conf.dtc_difintregs > UINT8_MAX ||
5946ff6d951SJohn Birrell 	    dtp->dt_conf.dtc_diftupregs > UINT8_MAX)
5956ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EOVERFLOW));
5966ff6d951SJohn Birrell 
5976ff6d951SJohn Birrell 	bzero(hp, sizeof (dof_hdr_t));
5986ff6d951SJohn Birrell 
5996ff6d951SJohn Birrell 	hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
6006ff6d951SJohn Birrell 	hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
6016ff6d951SJohn Birrell 	hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
6026ff6d951SJohn Birrell 	hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
6036ff6d951SJohn Birrell 
6046ff6d951SJohn Birrell 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
6056ff6d951SJohn Birrell 		hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64;
6066ff6d951SJohn Birrell 	else
6076ff6d951SJohn Birrell 		hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32;
6086ff6d951SJohn Birrell 
6096ff6d951SJohn Birrell 	hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
6106ff6d951SJohn Birrell 	hp->dofh_ident[DOF_ID_VERSION] = dofversion;
6116ff6d951SJohn Birrell 	hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion;
6126ff6d951SJohn Birrell 	hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs;
6136ff6d951SJohn Birrell 	hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs;
6146ff6d951SJohn Birrell 
6156ff6d951SJohn Birrell 	hp->dofh_hdrsize = sizeof (dof_hdr_t);
6166ff6d951SJohn Birrell 	hp->dofh_secsize = sizeof (dof_sec_t);
6176ff6d951SJohn Birrell 	hp->dofh_secoff = sizeof (dof_hdr_t);
6186ff6d951SJohn Birrell 
6196ff6d951SJohn Birrell 	return (0);
6206ff6d951SJohn Birrell }
6216ff6d951SJohn Birrell 
6226ff6d951SJohn Birrell void *
dtrace_dof_create(dtrace_hdl_t * dtp,dtrace_prog_t * pgp,uint_t flags)6236ff6d951SJohn Birrell dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags)
6246ff6d951SJohn Birrell {
6256ff6d951SJohn Birrell 	dt_dof_t *ddo = &dtp->dt_dof;
6266ff6d951SJohn Birrell 
6276ff6d951SJohn Birrell 	const dtrace_ecbdesc_t *edp, *last;
6286ff6d951SJohn Birrell 	const dtrace_probedesc_t *pdp;
6296ff6d951SJohn Birrell 	const dtrace_actdesc_t *ap;
6306ff6d951SJohn Birrell 	const dt_stmt_t *stp;
6316ff6d951SJohn Birrell 
6326ff6d951SJohn Birrell 	uint_t maxacts = 0;
6336ff6d951SJohn Birrell 	uint_t maxfmt = 0;
6346ff6d951SJohn Birrell 
6356ff6d951SJohn Birrell 	dt_provider_t *pvp;
6366ff6d951SJohn Birrell 	dt_xlator_t *dxp;
6376ff6d951SJohn Birrell 	dof_actdesc_t *dofa;
6386ff6d951SJohn Birrell 	dof_sec_t *sp;
6396ff6d951SJohn Birrell 	size_t ssize, lsize;
6406ff6d951SJohn Birrell 	dof_hdr_t h;
6416ff6d951SJohn Birrell 
6426ff6d951SJohn Birrell 	dt_buf_t dof;
6436ff6d951SJohn Birrell 	char *fmt;
6446ff6d951SJohn Birrell 	uint_t i;
6456ff6d951SJohn Birrell 
6466ff6d951SJohn Birrell 	if (flags & ~DTRACE_D_MASK) {
6476ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, EINVAL);
6486ff6d951SJohn Birrell 		return (NULL);
6496ff6d951SJohn Birrell 	}
6506ff6d951SJohn Birrell 
6516ff6d951SJohn Birrell 	flags |= dtp->dt_dflags;
6526ff6d951SJohn Birrell 
6536ff6d951SJohn Birrell 	if (dof_hdr(dtp, pgp->dp_dofversion, &h) != 0)
6546ff6d951SJohn Birrell 		return (NULL);
6556ff6d951SJohn Birrell 
6566ff6d951SJohn Birrell 	if (dt_dof_reset(dtp, pgp) != 0)
6576ff6d951SJohn Birrell 		return (NULL);
6586ff6d951SJohn Birrell 
6596ff6d951SJohn Birrell 	/*
6606ff6d951SJohn Birrell 	 * Iterate through the statement list computing the maximum number of
6616ff6d951SJohn Birrell 	 * actions and the maximum format string for allocating local buffers.
6626ff6d951SJohn Birrell 	 */
6636ff6d951SJohn Birrell 	for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
6646ff6d951SJohn Birrell 	    stp != NULL; stp = dt_list_next(stp), last = edp) {
6656ff6d951SJohn Birrell 
6666ff6d951SJohn Birrell 		dtrace_stmtdesc_t *sdp = stp->ds_desc;
6676ff6d951SJohn Birrell 		dtrace_actdesc_t *ap = sdp->dtsd_action;
6686ff6d951SJohn Birrell 
6696ff6d951SJohn Birrell 		if (sdp->dtsd_fmtdata != NULL) {
6706ff6d951SJohn Birrell 			i = dtrace_printf_format(dtp,
6716ff6d951SJohn Birrell 			    sdp->dtsd_fmtdata, NULL, 0);
6726ff6d951SJohn Birrell 			maxfmt = MAX(maxfmt, i);
6736ff6d951SJohn Birrell 		}
6746ff6d951SJohn Birrell 
6756ff6d951SJohn Birrell 		if ((edp = sdp->dtsd_ecbdesc) == last)
6766ff6d951SJohn Birrell 			continue; /* same ecb as previous statement */
6776ff6d951SJohn Birrell 
6786ff6d951SJohn Birrell 		for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next)
6796ff6d951SJohn Birrell 			i++;
6806ff6d951SJohn Birrell 
6816ff6d951SJohn Birrell 		maxacts = MAX(maxacts, i);
6826ff6d951SJohn Birrell 	}
6836ff6d951SJohn Birrell 
6846ff6d951SJohn Birrell 	dofa = alloca(sizeof (dof_actdesc_t) * maxacts);
6856ff6d951SJohn Birrell 	fmt = alloca(maxfmt + 1);
6866ff6d951SJohn Birrell 
6876ff6d951SJohn Birrell 	ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0);
6886ff6d951SJohn Birrell 	(void) dof_add_string(ddo, "");
6896ff6d951SJohn Birrell 
6906ff6d951SJohn Birrell 	/*
6916ff6d951SJohn Birrell 	 * If there are references to dynamic translators in the program, add
6926ff6d951SJohn Birrell 	 * an imported translator table entry for each referenced translator.
6936ff6d951SJohn Birrell 	 */
6946ff6d951SJohn Birrell 	if (pgp->dp_xrefslen != 0) {
6956ff6d951SJohn Birrell 		for (dxp = dt_list_next(&dtp->dt_xlators);
6966ff6d951SJohn Birrell 		    dxp != NULL; dxp = dt_list_next(dxp)) {
6976ff6d951SJohn Birrell 			if (dxp->dx_id < pgp->dp_xrefslen &&
6986ff6d951SJohn Birrell 			    pgp->dp_xrefs[dxp->dx_id] != NULL)
6996ff6d951SJohn Birrell 				dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT);
7006ff6d951SJohn Birrell 		}
7016ff6d951SJohn Birrell 	}
7026ff6d951SJohn Birrell 
7036ff6d951SJohn Birrell 	/*
7046ff6d951SJohn Birrell 	 * Now iterate through the statement list, creating the DOF section
7056ff6d951SJohn Birrell 	 * headers and data for each one and adding them to our buffers.
7066ff6d951SJohn Birrell 	 */
7076ff6d951SJohn Birrell 	for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
7086ff6d951SJohn Birrell 	    stp != NULL; stp = dt_list_next(stp), last = edp) {
7096ff6d951SJohn Birrell 
7106ff6d951SJohn Birrell 		dof_secidx_t probesec = DOF_SECIDX_NONE;
7116ff6d951SJohn Birrell 		dof_secidx_t prdsec = DOF_SECIDX_NONE;
7126ff6d951SJohn Birrell 		dof_secidx_t actsec = DOF_SECIDX_NONE;
7136ff6d951SJohn Birrell 
7146ff6d951SJohn Birrell 		const dt_stmt_t *next = stp;
7156ff6d951SJohn Birrell 		dtrace_stmtdesc_t *sdp = stp->ds_desc;
7166ff6d951SJohn Birrell 		dof_stridx_t strndx = 0;
7176ff6d951SJohn Birrell 		dof_probedesc_t dofp;
7186ff6d951SJohn Birrell 		dof_ecbdesc_t dofe;
7196ff6d951SJohn Birrell 		uint_t i;
7206ff6d951SJohn Birrell 
7216ff6d951SJohn Birrell 		if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)
7226ff6d951SJohn Birrell 			continue; /* same ecb as previous statement */
7236ff6d951SJohn Birrell 
7246ff6d951SJohn Birrell 		pdp = &edp->dted_probe;
7256ff6d951SJohn Birrell 
7266ff6d951SJohn Birrell 		/*
7276ff6d951SJohn Birrell 		 * Add a DOF_SECT_PROBEDESC for the ECB's probe description,
7286ff6d951SJohn Birrell 		 * and copy the probe description strings into the string table.
7296ff6d951SJohn Birrell 		 */
7306ff6d951SJohn Birrell 		dofp.dofp_strtab = ddo->ddo_strsec;
7316ff6d951SJohn Birrell 		dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider);
7326ff6d951SJohn Birrell 		dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod);
7336ff6d951SJohn Birrell 		dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func);
7346ff6d951SJohn Birrell 		dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name);
7356ff6d951SJohn Birrell 		dofp.dofp_id = pdp->dtpd_id;
7366ff6d951SJohn Birrell 
7376ff6d951SJohn Birrell 		probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC,
7386ff6d951SJohn Birrell 		    sizeof (dof_secidx_t), 0,
7396ff6d951SJohn Birrell 		    sizeof (dof_probedesc_t), sizeof (dof_probedesc_t));
7406ff6d951SJohn Birrell 
7416ff6d951SJohn Birrell 		/*
7426ff6d951SJohn Birrell 		 * If there is a predicate DIFO associated with the ecbdesc,
7436ff6d951SJohn Birrell 		 * write out the DIFO sections and save the DIFO section index.
7446ff6d951SJohn Birrell 		 */
7456ff6d951SJohn Birrell 		if (edp->dted_pred.dtpdd_difo != NULL)
7466ff6d951SJohn Birrell 			prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo);
7476ff6d951SJohn Birrell 
7486ff6d951SJohn Birrell 		/*
7496ff6d951SJohn Birrell 		 * Now iterate through the action list generating DIFOs as
7506ff6d951SJohn Birrell 		 * referenced therein and adding action descriptions to 'dofa'.
7516ff6d951SJohn Birrell 		 */
7526ff6d951SJohn Birrell 		for (i = 0, ap = edp->dted_action;
7536ff6d951SJohn Birrell 		    ap != NULL; ap = ap->dtad_next, i++) {
7546ff6d951SJohn Birrell 
7556ff6d951SJohn Birrell 			if (ap->dtad_difo != NULL) {
7566ff6d951SJohn Birrell 				dofa[i].dofa_difo =
7576ff6d951SJohn Birrell 				    dof_add_difo(ddo, ap->dtad_difo);
7586ff6d951SJohn Birrell 			} else
7596ff6d951SJohn Birrell 				dofa[i].dofa_difo = DOF_SECIDX_NONE;
7606ff6d951SJohn Birrell 
7616ff6d951SJohn Birrell 			/*
76254727873SPedro F. Giffuni 			 * If the first action in a statement has string data,
76354727873SPedro F. Giffuni 			 * add the string to the global string table.  This can
76454727873SPedro F. Giffuni 			 * be due either to a printf() format string
76554727873SPedro F. Giffuni 			 * (dtsd_fmtdata) or a print() type string
76654727873SPedro F. Giffuni 			 * (dtsd_strdata).
7676ff6d951SJohn Birrell 			 */
7686ff6d951SJohn Birrell 			if (sdp != NULL && ap == sdp->dtsd_action) {
7696ff6d951SJohn Birrell 				if (sdp->dtsd_fmtdata != NULL) {
7706ff6d951SJohn Birrell 					(void) dtrace_printf_format(dtp,
7716ff6d951SJohn Birrell 					    sdp->dtsd_fmtdata, fmt, maxfmt + 1);
7726ff6d951SJohn Birrell 					strndx = dof_add_string(ddo, fmt);
77354727873SPedro F. Giffuni 				} else if (sdp->dtsd_strdata != NULL) {
77454727873SPedro F. Giffuni 					strndx = dof_add_string(ddo,
77554727873SPedro F. Giffuni 					    sdp->dtsd_strdata);
77654727873SPedro F. Giffuni 				} else {
7776ff6d951SJohn Birrell 					strndx = 0; /* use dtad_arg instead */
77854727873SPedro F. Giffuni 				}
7796ff6d951SJohn Birrell 
7806ff6d951SJohn Birrell 				if ((next = dt_list_next(next)) != NULL)
7816ff6d951SJohn Birrell 					sdp = next->ds_desc;
7826ff6d951SJohn Birrell 				else
7836ff6d951SJohn Birrell 					sdp = NULL;
7846ff6d951SJohn Birrell 			}
7856ff6d951SJohn Birrell 
7866ff6d951SJohn Birrell 			if (strndx != 0) {
7876ff6d951SJohn Birrell 				dofa[i].dofa_arg = strndx;
7886ff6d951SJohn Birrell 				dofa[i].dofa_strtab = ddo->ddo_strsec;
7896ff6d951SJohn Birrell 			} else {
7906ff6d951SJohn Birrell 				dofa[i].dofa_arg = ap->dtad_arg;
7916ff6d951SJohn Birrell 				dofa[i].dofa_strtab = DOF_SECIDX_NONE;
7926ff6d951SJohn Birrell 			}
7936ff6d951SJohn Birrell 
7946ff6d951SJohn Birrell 			dofa[i].dofa_kind = ap->dtad_kind;
7956ff6d951SJohn Birrell 			dofa[i].dofa_ntuple = ap->dtad_ntuple;
7966ff6d951SJohn Birrell 			dofa[i].dofa_uarg = ap->dtad_uarg;
7976ff6d951SJohn Birrell 		}
7986ff6d951SJohn Birrell 
7996ff6d951SJohn Birrell 		if (i > 0) {
8006ff6d951SJohn Birrell 			actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC,
8016ff6d951SJohn Birrell 			    sizeof (uint64_t), 0, sizeof (dof_actdesc_t),
8026ff6d951SJohn Birrell 			    sizeof (dof_actdesc_t) * i);
8036ff6d951SJohn Birrell 		}
8046ff6d951SJohn Birrell 
8056ff6d951SJohn Birrell 		/*
8066ff6d951SJohn Birrell 		 * Now finally, add the DOF_SECT_ECBDESC referencing all the
8076ff6d951SJohn Birrell 		 * previously created sub-sections.
8086ff6d951SJohn Birrell 		 */
8096ff6d951SJohn Birrell 		dofe.dofe_probes = probesec;
8106ff6d951SJohn Birrell 		dofe.dofe_pred = prdsec;
8116ff6d951SJohn Birrell 		dofe.dofe_actions = actsec;
8126ff6d951SJohn Birrell 		dofe.dofe_pad = 0;
8136ff6d951SJohn Birrell 		dofe.dofe_uarg = edp->dted_uarg;
8146ff6d951SJohn Birrell 
8156ff6d951SJohn Birrell 		(void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC,
8166ff6d951SJohn Birrell 		    sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t));
8176ff6d951SJohn Birrell 	}
8186ff6d951SJohn Birrell 
8196ff6d951SJohn Birrell 	/*
8206ff6d951SJohn Birrell 	 * If any providers are user-defined, output DOF sections corresponding
8216ff6d951SJohn Birrell 	 * to the providers and the probes and arguments that they define.
8226ff6d951SJohn Birrell 	 */
8236ff6d951SJohn Birrell 	if (flags & DTRACE_D_PROBES) {
8246ff6d951SJohn Birrell 		for (pvp = dt_list_next(&dtp->dt_provlist);
82523e4da43SPedro F. Giffuni 		    pvp != NULL; pvp = dt_list_next(pvp)) {
82623e4da43SPedro F. Giffuni 			if (dof_add_provider(ddo, pvp) != 0)
82723e4da43SPedro F. Giffuni 				return (NULL);
82823e4da43SPedro F. Giffuni 		}
8296ff6d951SJohn Birrell 	}
8306ff6d951SJohn Birrell 
8316ff6d951SJohn Birrell 	/*
8326ff6d951SJohn Birrell 	 * If we're not stripping unloadable sections, generate compiler
8336ff6d951SJohn Birrell 	 * comments and any other unloadable miscellany.
8346ff6d951SJohn Birrell 	 */
8356ff6d951SJohn Birrell 	if (!(flags & DTRACE_D_STRIP)) {
8366ff6d951SJohn Birrell 		(void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS,
8376ff6d951SJohn Birrell 		    sizeof (char), 0, 0, strlen(_dtrace_version) + 1);
8386ff6d951SJohn Birrell 		(void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME,
8396ff6d951SJohn Birrell 		    sizeof (char), 0, 0, sizeof (struct utsname));
8406ff6d951SJohn Birrell 	}
8416ff6d951SJohn Birrell 
8426ff6d951SJohn Birrell 	/*
8436ff6d951SJohn Birrell 	 * Compute and fill in the appropriate values for the dof_hdr_t's
8446ff6d951SJohn Birrell 	 * dofh_secnum, dofh_loadsz, and dofh_filez values.
8456ff6d951SJohn Birrell 	 */
8466ff6d951SJohn Birrell 	h.dofh_secnum = ddo->ddo_nsecs;
8476ff6d951SJohn Birrell 	ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs);
8486ff6d951SJohn Birrell 
8496ff6d951SJohn Birrell 	h.dofh_loadsz = ssize +
8506ff6d951SJohn Birrell 	    dt_buf_len(&ddo->ddo_ldata) +
8516ff6d951SJohn Birrell 	    dt_buf_len(&ddo->ddo_strs);
8526ff6d951SJohn Birrell 
8536ff6d951SJohn Birrell 	if (dt_buf_len(&ddo->ddo_udata) != 0) {
8546ff6d951SJohn Birrell 		lsize = roundup(h.dofh_loadsz, sizeof (uint64_t));
8556ff6d951SJohn Birrell 		h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata);
8566ff6d951SJohn Birrell 	} else {
8576ff6d951SJohn Birrell 		lsize = h.dofh_loadsz;
8586ff6d951SJohn Birrell 		h.dofh_filesz = lsize;
8596ff6d951SJohn Birrell 	}
8606ff6d951SJohn Birrell 
8616ff6d951SJohn Birrell 	/*
8626ff6d951SJohn Birrell 	 * Set the global DOF_SECT_STRTAB's offset to be after the header,
8636ff6d951SJohn Birrell 	 * section headers, and other loadable data.  Since we're going to
8646ff6d951SJohn Birrell 	 * iterate over the buffer data directly, we must check for errors.
8656ff6d951SJohn Birrell 	 */
8666ff6d951SJohn Birrell 	if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) {
8676ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, i);
8686ff6d951SJohn Birrell 		return (NULL);
8696ff6d951SJohn Birrell 	}
8706ff6d951SJohn Birrell 
8716ff6d951SJohn Birrell 	sp = dt_buf_ptr(&ddo->ddo_secs);
8726ff6d951SJohn Birrell 	assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB);
8731670a1c2SRui Paulo 	assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs);
8746ff6d951SJohn Birrell 
8756ff6d951SJohn Birrell 	sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata);
8766ff6d951SJohn Birrell 	sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs);
8776ff6d951SJohn Birrell 
8786ff6d951SJohn Birrell 	/*
8796ff6d951SJohn Birrell 	 * Now relocate all the other section headers by adding the appropriate
8806ff6d951SJohn Birrell 	 * delta to their respective dofs_offset values.
8816ff6d951SJohn Birrell 	 */
8826ff6d951SJohn Birrell 	for (i = 0; i < ddo->ddo_nsecs; i++, sp++) {
8836ff6d951SJohn Birrell 		if (i == ddo->ddo_strsec)
8846ff6d951SJohn Birrell 			continue; /* already relocated above */
8856ff6d951SJohn Birrell 
8866ff6d951SJohn Birrell 		if (sp->dofs_flags & DOF_SECF_LOAD)
8876ff6d951SJohn Birrell 			sp->dofs_offset += ssize;
8886ff6d951SJohn Birrell 		else
8896ff6d951SJohn Birrell 			sp->dofs_offset += lsize;
8906ff6d951SJohn Birrell 	}
8916ff6d951SJohn Birrell 
8926ff6d951SJohn Birrell 	/*
8936ff6d951SJohn Birrell 	 * Finally, assemble the complete in-memory DOF buffer by writing the
8946ff6d951SJohn Birrell 	 * header and then concatenating all our buffers.  dt_buf_concat() will
8956ff6d951SJohn Birrell 	 * propagate any errors and cause dt_buf_claim() to return NULL.
8966ff6d951SJohn Birrell 	 */
8976ff6d951SJohn Birrell 	dt_buf_create(dtp, &dof, "dof", h.dofh_filesz);
8986ff6d951SJohn Birrell 
8996ff6d951SJohn Birrell 	dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t));
9006ff6d951SJohn Birrell 	dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t));
9016ff6d951SJohn Birrell 	dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t));
9026ff6d951SJohn Birrell 	dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char));
9036ff6d951SJohn Birrell 	dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t));
9046ff6d951SJohn Birrell 
9056ff6d951SJohn Birrell 	return (dt_buf_claim(dtp, &dof));
9066ff6d951SJohn Birrell }
9076ff6d951SJohn Birrell 
9086ff6d951SJohn Birrell void
dtrace_dof_destroy(dtrace_hdl_t * dtp,void * dof)9096ff6d951SJohn Birrell dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof)
9106ff6d951SJohn Birrell {
9116ff6d951SJohn Birrell 	dt_free(dtp, dof);
9126ff6d951SJohn Birrell }
9136ff6d951SJohn Birrell 
9146ff6d951SJohn Birrell void *
dtrace_getopt_dof(dtrace_hdl_t * dtp)9156ff6d951SJohn Birrell dtrace_getopt_dof(dtrace_hdl_t *dtp)
9166ff6d951SJohn Birrell {
9176ff6d951SJohn Birrell 	dof_hdr_t *dof;
9186ff6d951SJohn Birrell 	dof_sec_t *sec;
9196ff6d951SJohn Birrell 	dof_optdesc_t *dofo;
9206ff6d951SJohn Birrell 	int i, nopts = 0, len = sizeof (dof_hdr_t) +
9216ff6d951SJohn Birrell 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t));
9226ff6d951SJohn Birrell 
9236ff6d951SJohn Birrell 	for (i = 0; i < DTRACEOPT_MAX; i++) {
9246ff6d951SJohn Birrell 		if (dtp->dt_options[i] != DTRACEOPT_UNSET)
9256ff6d951SJohn Birrell 			nopts++;
9266ff6d951SJohn Birrell 	}
9276ff6d951SJohn Birrell 
9286ff6d951SJohn Birrell 	len += sizeof (dof_optdesc_t) * nopts;
9296ff6d951SJohn Birrell 
9306ff6d951SJohn Birrell 	if ((dof = dt_zalloc(dtp, len)) == NULL ||
9316ff6d951SJohn Birrell 	    dof_hdr(dtp, DOF_VERSION, dof) != 0) {
9326ff6d951SJohn Birrell 		dt_free(dtp, dof);
9336ff6d951SJohn Birrell 		return (NULL);
9346ff6d951SJohn Birrell 	}
9356ff6d951SJohn Birrell 
9366ff6d951SJohn Birrell 	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
9376ff6d951SJohn Birrell 	dof->dofh_loadsz = len;
9386ff6d951SJohn Birrell 	dof->dofh_filesz = len;
9396ff6d951SJohn Birrell 
9406ff6d951SJohn Birrell 	/*
9416ff6d951SJohn Birrell 	 * Fill in the option section header...
9426ff6d951SJohn Birrell 	 */
9436ff6d951SJohn Birrell 	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
9446ff6d951SJohn Birrell 	sec->dofs_type = DOF_SECT_OPTDESC;
9456ff6d951SJohn Birrell 	sec->dofs_align = sizeof (uint64_t);
9466ff6d951SJohn Birrell 	sec->dofs_flags = DOF_SECF_LOAD;
9476ff6d951SJohn Birrell 	sec->dofs_entsize = sizeof (dof_optdesc_t);
9486ff6d951SJohn Birrell 
9496ff6d951SJohn Birrell 	dofo = (dof_optdesc_t *)((uintptr_t)sec +
9506ff6d951SJohn Birrell 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
9516ff6d951SJohn Birrell 
9526ff6d951SJohn Birrell 	sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;
9536ff6d951SJohn Birrell 	sec->dofs_size = sizeof (dof_optdesc_t) * nopts;
9546ff6d951SJohn Birrell 
9556ff6d951SJohn Birrell 	for (i = 0; i < DTRACEOPT_MAX; i++) {
9566ff6d951SJohn Birrell 		if (dtp->dt_options[i] == DTRACEOPT_UNSET)
9576ff6d951SJohn Birrell 			continue;
9586ff6d951SJohn Birrell 
9596ff6d951SJohn Birrell 		dofo->dofo_option = i;
9606ff6d951SJohn Birrell 		dofo->dofo_strtab = DOF_SECIDX_NONE;
9616ff6d951SJohn Birrell 		dofo->dofo_value = dtp->dt_options[i];
9626ff6d951SJohn Birrell 		dofo++;
9636ff6d951SJohn Birrell 	}
9646ff6d951SJohn Birrell 
9656ff6d951SJohn Birrell 	return (dof);
9666ff6d951SJohn Birrell }
9676ff6d951SJohn Birrell 
9686ff6d951SJohn Birrell void *
dtrace_geterr_dof(dtrace_hdl_t * dtp)9696ff6d951SJohn Birrell dtrace_geterr_dof(dtrace_hdl_t *dtp)
9706ff6d951SJohn Birrell {
9716ff6d951SJohn Birrell 	if (dtp->dt_errprog != NULL)
9726ff6d951SJohn Birrell 		return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));
9736ff6d951SJohn Birrell 
9746ff6d951SJohn Birrell 	(void) dt_set_errno(dtp, EDT_BADERROR);
9756ff6d951SJohn Birrell 	return (NULL);
9766ff6d951SJohn Birrell }
977