10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51710Sahl * Common Development and Distribution License (the "License").
61710Sahl * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211710Sahl
220Sstevel@tonic-gate /*
23*12902SBryan.Cantrill@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/sysmacros.h>
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <strings.h>
300Sstevel@tonic-gate #include <alloca.h>
310Sstevel@tonic-gate #include <assert.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <limits.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <dt_impl.h>
370Sstevel@tonic-gate #include <dt_strtab.h>
38265Smws #include <dt_program.h>
390Sstevel@tonic-gate #include <dt_provider.h>
40265Smws #include <dt_xlator.h>
410Sstevel@tonic-gate #include <dt_dof.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate void
dt_dof_init(dtrace_hdl_t * dtp)440Sstevel@tonic-gate dt_dof_init(dtrace_hdl_t *dtp)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate dt_dof_t *ddo = &dtp->dt_dof;
470Sstevel@tonic-gate
480Sstevel@tonic-gate ddo->ddo_hdl = dtp;
490Sstevel@tonic-gate ddo->ddo_nsecs = 0;
500Sstevel@tonic-gate ddo->ddo_strsec = DOF_SECIDX_NONE;
51265Smws ddo->ddo_xlimport = NULL;
52265Smws ddo->ddo_xlexport = NULL;
530Sstevel@tonic-gate
540Sstevel@tonic-gate dt_buf_create(dtp, &ddo->ddo_secs, "section headers", 0);
550Sstevel@tonic-gate dt_buf_create(dtp, &ddo->ddo_strs, "string table", 0);
560Sstevel@tonic-gate dt_buf_create(dtp, &ddo->ddo_ldata, "loadable data", 0);
570Sstevel@tonic-gate dt_buf_create(dtp, &ddo->ddo_udata, "unloadable data", 0);
580Sstevel@tonic-gate
590Sstevel@tonic-gate dt_buf_create(dtp, &ddo->ddo_probes, "probe data", 0);
600Sstevel@tonic-gate dt_buf_create(dtp, &ddo->ddo_args, "probe args", 0);
610Sstevel@tonic-gate dt_buf_create(dtp, &ddo->ddo_offs, "probe offs", 0);
621710Sahl dt_buf_create(dtp, &ddo->ddo_enoffs, "probe is-enabled offs", 0);
630Sstevel@tonic-gate dt_buf_create(dtp, &ddo->ddo_rels, "probe rels", 0);
64265Smws
65265Smws dt_buf_create(dtp, &ddo->ddo_xlms, "xlate members", 0);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate void
dt_dof_fini(dtrace_hdl_t * dtp)690Sstevel@tonic-gate dt_dof_fini(dtrace_hdl_t *dtp)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate dt_dof_t *ddo = &dtp->dt_dof;
720Sstevel@tonic-gate
73265Smws dt_free(dtp, ddo->ddo_xlimport);
74265Smws dt_free(dtp, ddo->ddo_xlexport);
75265Smws
760Sstevel@tonic-gate dt_buf_destroy(dtp, &ddo->ddo_secs);
770Sstevel@tonic-gate dt_buf_destroy(dtp, &ddo->ddo_strs);
780Sstevel@tonic-gate dt_buf_destroy(dtp, &ddo->ddo_ldata);
790Sstevel@tonic-gate dt_buf_destroy(dtp, &ddo->ddo_udata);
800Sstevel@tonic-gate
810Sstevel@tonic-gate dt_buf_destroy(dtp, &ddo->ddo_probes);
820Sstevel@tonic-gate dt_buf_destroy(dtp, &ddo->ddo_args);
830Sstevel@tonic-gate dt_buf_destroy(dtp, &ddo->ddo_offs);
841710Sahl dt_buf_destroy(dtp, &ddo->ddo_enoffs);
850Sstevel@tonic-gate dt_buf_destroy(dtp, &ddo->ddo_rels);
86265Smws
87265Smws dt_buf_destroy(dtp, &ddo->ddo_xlms);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
90265Smws static int
dt_dof_reset(dtrace_hdl_t * dtp,dtrace_prog_t * pgp)91265Smws dt_dof_reset(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate dt_dof_t *ddo = &dtp->dt_dof;
94265Smws uint_t i, nx = dtp->dt_xlatorid;
95265Smws
96265Smws assert(ddo->ddo_hdl == dtp);
97265Smws ddo->ddo_pgp = pgp;
980Sstevel@tonic-gate
990Sstevel@tonic-gate ddo->ddo_nsecs = 0;
1000Sstevel@tonic-gate ddo->ddo_strsec = DOF_SECIDX_NONE;
1010Sstevel@tonic-gate
102265Smws dt_free(dtp, ddo->ddo_xlimport);
103265Smws dt_free(dtp, ddo->ddo_xlexport);
104265Smws
105265Smws ddo->ddo_xlimport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
106265Smws ddo->ddo_xlexport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
107265Smws
108265Smws if (nx != 0 && (ddo->ddo_xlimport == NULL || ddo->ddo_xlexport == NULL))
109265Smws return (-1); /* errno is set for us */
110265Smws
111265Smws for (i = 0; i < nx; i++) {
112265Smws ddo->ddo_xlimport[i] = DOF_SECIDX_NONE;
113265Smws ddo->ddo_xlexport[i] = DOF_SECIDX_NONE;
114265Smws }
115265Smws
1160Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_secs);
1170Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_strs);
1180Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_ldata);
1190Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_udata);
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_probes);
1220Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_args);
1230Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_offs);
1241710Sahl dt_buf_reset(dtp, &ddo->ddo_enoffs);
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
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)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
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)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
dof_add_string(dt_dof_t * ddo,const char * s)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
dof_attr(const dtrace_attribute_t * ap)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
dof_add_difo(dt_dof_t * ddo,const dtrace_difo_t * dp)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
dof_add_translator(dt_dof_t * ddo,const dt_xlator_t * dxp,uint_t type)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
dof_add_probe(dt_idhash_t * dhp,dt_ident_t * idp,void * data)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;
4311710Sahl dofpr.dofpr_pad1 = 0;
4321710Sahl dofpr.dofpr_pad2 = 0;
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
4351710Sahl dt_dprintf("adding probe for %s:%s\n", pip->pi_fname,
4361710Sahl prp->pr_name);
4371710Sahl
4380Sstevel@tonic-gate dofpr.dofpr_func = dof_add_string(ddo, pip->pi_fname);
4391710Sahl
4401880Sahl /*
4411880Sahl * There should be one probe offset or is-enabled probe offset
4421880Sahl * or else this probe instance won't have been created. The
4431880Sahl * kernel will reject DOF which has a probe with no offsets.
4441880Sahl */
4451880Sahl assert(pip->pi_noffs + pip->pi_nenoffs > 0);
4461710Sahl
4470Sstevel@tonic-gate dofpr.dofpr_offidx =
4480Sstevel@tonic-gate dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t);
4490Sstevel@tonic-gate dofpr.dofpr_noffs = pip->pi_noffs;
4501710Sahl dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs,
4511710Sahl pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
4520Sstevel@tonic-gate
4531710Sahl dofpr.dofpr_enoffidx =
4541710Sahl dt_buf_len(&ddo->ddo_enoffs) / sizeof (uint32_t);
4551710Sahl dofpr.dofpr_nenoffs = pip->pi_nenoffs;
4561710Sahl dt_buf_write(dtp, &ddo->ddo_enoffs, pip->pi_enoffs,
4571906Sahl pip->pi_nenoffs * sizeof (uint32_t), sizeof (uint32_t));
4580Sstevel@tonic-gate
4591239Sahl /*
4601239Sahl * If pi_rname isn't set, the relocation will be against the
4611239Sahl * function name. If it is, the relocation will be against
4621239Sahl * pi_rname. This will be used if the function is scoped
4631239Sahl * locally so an alternate symbol is added for the purpose
4641239Sahl * of this relocation.
4651239Sahl */
4661239Sahl if (pip->pi_rname[0] == '\0')
4671239Sahl dofr.dofr_name = dofpr.dofpr_func;
4681239Sahl else
4691239Sahl dofr.dofr_name = dof_add_string(ddo, pip->pi_rname);
4700Sstevel@tonic-gate dofr.dofr_type = DOF_RELO_SETX;
4710Sstevel@tonic-gate dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes);
4720Sstevel@tonic-gate dofr.dofr_data = 0;
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate dt_buf_write(dtp, &ddo->ddo_rels, &dofr,
4750Sstevel@tonic-gate sizeof (dofr), sizeof (uint64_t));
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate dt_buf_write(dtp, &ddo->ddo_probes, &dofpr,
4780Sstevel@tonic-gate sizeof (dofpr), sizeof (uint64_t));
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate return (0);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate static void
dof_add_provider(dt_dof_t * ddo,const dt_provider_t * pvp)4850Sstevel@tonic-gate dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate dtrace_hdl_t *dtp = ddo->ddo_hdl;
4880Sstevel@tonic-gate dof_provider_t dofpv;
4890Sstevel@tonic-gate dof_relohdr_t dofr;
490265Smws dof_secidx_t *dofs;
491265Smws ulong_t xr, nxr;
4921880Sahl size_t sz;
493265Smws id_t i;
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate if (pvp->pv_flags & DT_PROVIDER_IMPL)
4960Sstevel@tonic-gate return; /* ignore providers that are exported by dtrace(7D) */
4970Sstevel@tonic-gate
498265Smws nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax);
499265Smws dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1));
500265Smws xr = 1; /* reserve dofs[0] for the provider itself */
501265Smws
502265Smws /*
503265Smws * For each translator referenced by the provider (pv_xrefs), emit an
504265Smws * exported translator section for it if one hasn't been created yet.
505265Smws */
506265Smws for (i = 0; i < pvp->pv_xrmax; i++) {
507265Smws if (BT_TEST(pvp->pv_xrefs, i) &&
508265Smws dtp->dt_xlatemode == DT_XL_DYNAMIC) {
509265Smws dof_add_translator(ddo,
510265Smws dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT);
511265Smws dofs[xr++] = ddo->ddo_xlexport[i];
512265Smws }
513265Smws }
514265Smws
5150Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_probes);
5160Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_args);
5170Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_offs);
5181710Sahl dt_buf_reset(dtp, &ddo->ddo_enoffs);
5190Sstevel@tonic-gate dt_buf_reset(dtp, &ddo->ddo_rels);
5200Sstevel@tonic-gate
5210Sstevel@tonic-gate (void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo);
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES,
5240Sstevel@tonic-gate sizeof (uint64_t), 0, sizeof (dof_probe_t),
5250Sstevel@tonic-gate dt_buf_len(&ddo->ddo_probes));
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate dt_buf_concat(dtp, &ddo->ddo_ldata,
5280Sstevel@tonic-gate &ddo->ddo_probes, sizeof (uint64_t));
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS,
5310Sstevel@tonic-gate sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args));
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t));
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS,
5360Sstevel@tonic-gate sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs));
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t));
5390Sstevel@tonic-gate
5401880Sahl if ((sz = dt_buf_len(&ddo->ddo_enoffs)) != 0) {
5411880Sahl dofpv.dofpv_prenoffs = dof_add_lsect(ddo, NULL,
5421880Sahl DOF_SECT_PRENOFFS, sizeof (uint_t), 0, sizeof (uint_t), sz);
5431880Sahl } else {
5441880Sahl dofpv.dofpv_prenoffs = DOF_SECT_NONE;
5451880Sahl }
5461710Sahl
5471710Sahl dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_enoffs, sizeof (uint_t));
5481710Sahl
5490Sstevel@tonic-gate dofpv.dofpv_strtab = ddo->ddo_strsec;
5500Sstevel@tonic-gate dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name);
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider);
5530Sstevel@tonic-gate dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod);
5540Sstevel@tonic-gate dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func);
5550Sstevel@tonic-gate dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name);
5560Sstevel@tonic-gate dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args);
5570Sstevel@tonic-gate
558265Smws dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER,
5590Sstevel@tonic-gate sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t));
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate dofr.dofr_strtab = dofpv.dofpv_strtab;
5620Sstevel@tonic-gate dofr.dofr_tgtsec = dofpv.dofpv_probes;
5630Sstevel@tonic-gate dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB,
5640Sstevel@tonic-gate sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
5650Sstevel@tonic-gate dt_buf_len(&ddo->ddo_rels));
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t));
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
5700Sstevel@tonic-gate sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
571265Smws
572265Smws if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) {
573265Smws (void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT,
574265Smws sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t),
575265Smws sizeof (dof_secidx_t) * (nxr + 1));
576265Smws }
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate
5790Sstevel@tonic-gate static int
dof_hdr(dtrace_hdl_t * dtp,uint8_t dofversion,dof_hdr_t * hp)5801710Sahl dof_hdr(dtrace_hdl_t *dtp, uint8_t dofversion, dof_hdr_t *hp)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate /*
5830Sstevel@tonic-gate * If our config values cannot fit in a uint8_t, we can't generate a
5840Sstevel@tonic-gate * DOF header since the values won't fit. This can only happen if the
5850Sstevel@tonic-gate * user forcibly compiles a program with an artificial configuration.
5860Sstevel@tonic-gate */
5870Sstevel@tonic-gate if (dtp->dt_conf.dtc_difversion > UINT8_MAX ||
5880Sstevel@tonic-gate dtp->dt_conf.dtc_difintregs > UINT8_MAX ||
5890Sstevel@tonic-gate dtp->dt_conf.dtc_diftupregs > UINT8_MAX)
5900Sstevel@tonic-gate return (dt_set_errno(dtp, EOVERFLOW));
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate bzero(hp, sizeof (dof_hdr_t));
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
5950Sstevel@tonic-gate hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
5960Sstevel@tonic-gate hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
5970Sstevel@tonic-gate hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
6000Sstevel@tonic-gate hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64;
6010Sstevel@tonic-gate else
6020Sstevel@tonic-gate hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32;
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
6051710Sahl hp->dofh_ident[DOF_ID_VERSION] = dofversion;
6060Sstevel@tonic-gate hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion;
6070Sstevel@tonic-gate hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs;
6080Sstevel@tonic-gate hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs;
6090Sstevel@tonic-gate
6100Sstevel@tonic-gate hp->dofh_hdrsize = sizeof (dof_hdr_t);
6110Sstevel@tonic-gate hp->dofh_secsize = sizeof (dof_sec_t);
6120Sstevel@tonic-gate hp->dofh_secoff = sizeof (dof_hdr_t);
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate return (0);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate void *
dtrace_dof_create(dtrace_hdl_t * dtp,dtrace_prog_t * pgp,uint_t flags)6180Sstevel@tonic-gate dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags)
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate dt_dof_t *ddo = &dtp->dt_dof;
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate const dtrace_ecbdesc_t *edp, *last;
6230Sstevel@tonic-gate const dtrace_probedesc_t *pdp;
6240Sstevel@tonic-gate const dtrace_actdesc_t *ap;
6250Sstevel@tonic-gate const dt_stmt_t *stp;
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate uint_t maxacts = 0;
6280Sstevel@tonic-gate uint_t maxfmt = 0;
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate dt_provider_t *pvp;
631265Smws dt_xlator_t *dxp;
6320Sstevel@tonic-gate dof_actdesc_t *dofa;
6330Sstevel@tonic-gate dof_sec_t *sp;
6340Sstevel@tonic-gate size_t ssize, lsize;
6350Sstevel@tonic-gate dof_hdr_t h;
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate dt_buf_t dof;
6380Sstevel@tonic-gate char *fmt;
6390Sstevel@tonic-gate uint_t i;
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate if (flags & ~DTRACE_D_MASK) {
6420Sstevel@tonic-gate (void) dt_set_errno(dtp, EINVAL);
6430Sstevel@tonic-gate return (NULL);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate flags |= dtp->dt_dflags;
6470Sstevel@tonic-gate
6481710Sahl if (dof_hdr(dtp, pgp->dp_dofversion, &h) != 0)
6490Sstevel@tonic-gate return (NULL);
6500Sstevel@tonic-gate
651265Smws if (dt_dof_reset(dtp, pgp) != 0)
652265Smws return (NULL);
6530Sstevel@tonic-gate
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate * Iterate through the statement list computing the maximum number of
6560Sstevel@tonic-gate * actions and the maximum format string for allocating local buffers.
6570Sstevel@tonic-gate */
6580Sstevel@tonic-gate for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
6590Sstevel@tonic-gate stp != NULL; stp = dt_list_next(stp), last = edp) {
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate dtrace_stmtdesc_t *sdp = stp->ds_desc;
6620Sstevel@tonic-gate dtrace_actdesc_t *ap = sdp->dtsd_action;
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate if (sdp->dtsd_fmtdata != NULL) {
6650Sstevel@tonic-gate i = dtrace_printf_format(dtp,
6660Sstevel@tonic-gate sdp->dtsd_fmtdata, NULL, 0);
6670Sstevel@tonic-gate maxfmt = MAX(maxfmt, i);
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate
6700Sstevel@tonic-gate if ((edp = sdp->dtsd_ecbdesc) == last)
6710Sstevel@tonic-gate continue; /* same ecb as previous statement */
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next)
6740Sstevel@tonic-gate i++;
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate maxacts = MAX(maxacts, i);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate dofa = alloca(sizeof (dof_actdesc_t) * maxacts);
6800Sstevel@tonic-gate fmt = alloca(maxfmt + 1);
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0);
6830Sstevel@tonic-gate (void) dof_add_string(ddo, "");
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate /*
686265Smws * If there are references to dynamic translators in the program, add
687265Smws * an imported translator table entry for each referenced translator.
688265Smws */
689265Smws if (pgp->dp_xrefslen != 0) {
690265Smws for (dxp = dt_list_next(&dtp->dt_xlators);
691265Smws dxp != NULL; dxp = dt_list_next(dxp)) {
692265Smws if (dxp->dx_id < pgp->dp_xrefslen &&
693265Smws pgp->dp_xrefs[dxp->dx_id] != NULL)
694265Smws dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT);
695265Smws }
696265Smws }
697265Smws
698265Smws /*
6990Sstevel@tonic-gate * Now iterate through the statement list, creating the DOF section
7000Sstevel@tonic-gate * headers and data for each one and adding them to our buffers.
7010Sstevel@tonic-gate */
7020Sstevel@tonic-gate for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
7030Sstevel@tonic-gate stp != NULL; stp = dt_list_next(stp), last = edp) {
7040Sstevel@tonic-gate
7050Sstevel@tonic-gate dof_secidx_t probesec = DOF_SECIDX_NONE;
7060Sstevel@tonic-gate dof_secidx_t prdsec = DOF_SECIDX_NONE;
7070Sstevel@tonic-gate dof_secidx_t actsec = DOF_SECIDX_NONE;
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate const dt_stmt_t *next = stp;
7100Sstevel@tonic-gate dtrace_stmtdesc_t *sdp = stp->ds_desc;
7110Sstevel@tonic-gate dof_stridx_t strndx = 0;
7120Sstevel@tonic-gate dof_probedesc_t dofp;
7130Sstevel@tonic-gate dof_ecbdesc_t dofe;
7140Sstevel@tonic-gate uint_t i;
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)
7170Sstevel@tonic-gate continue; /* same ecb as previous statement */
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate pdp = &edp->dted_probe;
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate /*
7220Sstevel@tonic-gate * Add a DOF_SECT_PROBEDESC for the ECB's probe description,
7230Sstevel@tonic-gate * and copy the probe description strings into the string table.
7240Sstevel@tonic-gate */
7250Sstevel@tonic-gate dofp.dofp_strtab = ddo->ddo_strsec;
7260Sstevel@tonic-gate dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider);
7270Sstevel@tonic-gate dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod);
7280Sstevel@tonic-gate dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func);
7290Sstevel@tonic-gate dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name);
7300Sstevel@tonic-gate dofp.dofp_id = pdp->dtpd_id;
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC,
7330Sstevel@tonic-gate sizeof (dof_secidx_t), 0,
7340Sstevel@tonic-gate sizeof (dof_probedesc_t), sizeof (dof_probedesc_t));
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate /*
7370Sstevel@tonic-gate * If there is a predicate DIFO associated with the ecbdesc,
7380Sstevel@tonic-gate * write out the DIFO sections and save the DIFO section index.
7390Sstevel@tonic-gate */
7400Sstevel@tonic-gate if (edp->dted_pred.dtpdd_difo != NULL)
7410Sstevel@tonic-gate prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo);
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate /*
7440Sstevel@tonic-gate * Now iterate through the action list generating DIFOs as
7450Sstevel@tonic-gate * referenced therein and adding action descriptions to 'dofa'.
7460Sstevel@tonic-gate */
7470Sstevel@tonic-gate for (i = 0, ap = edp->dted_action;
7480Sstevel@tonic-gate ap != NULL; ap = ap->dtad_next, i++) {
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate if (ap->dtad_difo != NULL) {
7510Sstevel@tonic-gate dofa[i].dofa_difo =
7520Sstevel@tonic-gate dof_add_difo(ddo, ap->dtad_difo);
7530Sstevel@tonic-gate } else
7540Sstevel@tonic-gate dofa[i].dofa_difo = DOF_SECIDX_NONE;
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate /*
7570Sstevel@tonic-gate * If the first action in a statement has format data,
7580Sstevel@tonic-gate * add the format string to the global string table.
7590Sstevel@tonic-gate */
7600Sstevel@tonic-gate if (sdp != NULL && ap == sdp->dtsd_action) {
7610Sstevel@tonic-gate if (sdp->dtsd_fmtdata != NULL) {
7620Sstevel@tonic-gate (void) dtrace_printf_format(dtp,
7630Sstevel@tonic-gate sdp->dtsd_fmtdata, fmt, maxfmt + 1);
7640Sstevel@tonic-gate strndx = dof_add_string(ddo, fmt);
7650Sstevel@tonic-gate } else
7660Sstevel@tonic-gate strndx = 0; /* use dtad_arg instead */
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate if ((next = dt_list_next(next)) != NULL)
7690Sstevel@tonic-gate sdp = next->ds_desc;
7700Sstevel@tonic-gate else
7710Sstevel@tonic-gate sdp = NULL;
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate
7740Sstevel@tonic-gate if (strndx != 0) {
7750Sstevel@tonic-gate dofa[i].dofa_arg = strndx;
7760Sstevel@tonic-gate dofa[i].dofa_strtab = ddo->ddo_strsec;
7770Sstevel@tonic-gate } else {
7780Sstevel@tonic-gate dofa[i].dofa_arg = ap->dtad_arg;
7790Sstevel@tonic-gate dofa[i].dofa_strtab = DOF_SECIDX_NONE;
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate dofa[i].dofa_kind = ap->dtad_kind;
7830Sstevel@tonic-gate dofa[i].dofa_ntuple = ap->dtad_ntuple;
7840Sstevel@tonic-gate dofa[i].dofa_uarg = ap->dtad_uarg;
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate
7870Sstevel@tonic-gate if (i > 0) {
7880Sstevel@tonic-gate actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC,
7890Sstevel@tonic-gate sizeof (uint64_t), 0, sizeof (dof_actdesc_t),
7900Sstevel@tonic-gate sizeof (dof_actdesc_t) * i);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate /*
7940Sstevel@tonic-gate * Now finally, add the DOF_SECT_ECBDESC referencing all the
7950Sstevel@tonic-gate * previously created sub-sections.
7960Sstevel@tonic-gate */
7970Sstevel@tonic-gate dofe.dofe_probes = probesec;
7980Sstevel@tonic-gate dofe.dofe_pred = prdsec;
7990Sstevel@tonic-gate dofe.dofe_actions = actsec;
8000Sstevel@tonic-gate dofe.dofe_pad = 0;
8010Sstevel@tonic-gate dofe.dofe_uarg = edp->dted_uarg;
8020Sstevel@tonic-gate
8030Sstevel@tonic-gate (void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC,
8040Sstevel@tonic-gate sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t));
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate
8070Sstevel@tonic-gate /*
8080Sstevel@tonic-gate * If any providers are user-defined, output DOF sections corresponding
8090Sstevel@tonic-gate * to the providers and the probes and arguments that they define.
8100Sstevel@tonic-gate */
8110Sstevel@tonic-gate if (flags & DTRACE_D_PROBES) {
8120Sstevel@tonic-gate for (pvp = dt_list_next(&dtp->dt_provlist);
8130Sstevel@tonic-gate pvp != NULL; pvp = dt_list_next(pvp))
8140Sstevel@tonic-gate dof_add_provider(ddo, pvp);
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate /*
8180Sstevel@tonic-gate * If we're not stripping unloadable sections, generate compiler
8190Sstevel@tonic-gate * comments and any other unloadable miscellany.
8200Sstevel@tonic-gate */
8210Sstevel@tonic-gate if (!(flags & DTRACE_D_STRIP)) {
8220Sstevel@tonic-gate (void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS,
8230Sstevel@tonic-gate sizeof (char), 0, 0, strlen(_dtrace_version) + 1);
8240Sstevel@tonic-gate (void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME,
8250Sstevel@tonic-gate sizeof (char), 0, 0, sizeof (struct utsname));
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate
8280Sstevel@tonic-gate /*
8290Sstevel@tonic-gate * Compute and fill in the appropriate values for the dof_hdr_t's
8300Sstevel@tonic-gate * dofh_secnum, dofh_loadsz, and dofh_filez values.
8310Sstevel@tonic-gate */
8320Sstevel@tonic-gate h.dofh_secnum = ddo->ddo_nsecs;
8330Sstevel@tonic-gate ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs);
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate h.dofh_loadsz = ssize +
8360Sstevel@tonic-gate dt_buf_len(&ddo->ddo_ldata) +
8370Sstevel@tonic-gate dt_buf_len(&ddo->ddo_strs);
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate if (dt_buf_len(&ddo->ddo_udata) != 0) {
8400Sstevel@tonic-gate lsize = roundup(h.dofh_loadsz, sizeof (uint64_t));
8410Sstevel@tonic-gate h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata);
8420Sstevel@tonic-gate } else {
8430Sstevel@tonic-gate lsize = h.dofh_loadsz;
8440Sstevel@tonic-gate h.dofh_filesz = lsize;
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate /*
8480Sstevel@tonic-gate * Set the global DOF_SECT_STRTAB's offset to be after the header,
8490Sstevel@tonic-gate * section headers, and other loadable data. Since we're going to
8500Sstevel@tonic-gate * iterate over the buffer data directly, we must check for errors.
8510Sstevel@tonic-gate */
8520Sstevel@tonic-gate if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) {
8530Sstevel@tonic-gate (void) dt_set_errno(dtp, i);
8540Sstevel@tonic-gate return (NULL);
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate
8570Sstevel@tonic-gate sp = dt_buf_ptr(&ddo->ddo_secs);
8580Sstevel@tonic-gate assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB);
859*12902SBryan.Cantrill@Sun.COM assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs);
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata);
8620Sstevel@tonic-gate sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs);
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate /*
8650Sstevel@tonic-gate * Now relocate all the other section headers by adding the appropriate
8660Sstevel@tonic-gate * delta to their respective dofs_offset values.
8670Sstevel@tonic-gate */
8680Sstevel@tonic-gate for (i = 0; i < ddo->ddo_nsecs; i++, sp++) {
8690Sstevel@tonic-gate if (i == ddo->ddo_strsec)
8700Sstevel@tonic-gate continue; /* already relocated above */
8710Sstevel@tonic-gate
8720Sstevel@tonic-gate if (sp->dofs_flags & DOF_SECF_LOAD)
8730Sstevel@tonic-gate sp->dofs_offset += ssize;
8740Sstevel@tonic-gate else
8750Sstevel@tonic-gate sp->dofs_offset += lsize;
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate
8780Sstevel@tonic-gate /*
8790Sstevel@tonic-gate * Finally, assemble the complete in-memory DOF buffer by writing the
8800Sstevel@tonic-gate * header and then concatenating all our buffers. dt_buf_concat() will
8810Sstevel@tonic-gate * propagate any errors and cause dt_buf_claim() to return NULL.
8820Sstevel@tonic-gate */
8830Sstevel@tonic-gate dt_buf_create(dtp, &dof, "dof", h.dofh_filesz);
8840Sstevel@tonic-gate
8850Sstevel@tonic-gate dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t));
8860Sstevel@tonic-gate dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t));
8870Sstevel@tonic-gate dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t));
8880Sstevel@tonic-gate dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char));
8890Sstevel@tonic-gate dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t));
8900Sstevel@tonic-gate
8910Sstevel@tonic-gate return (dt_buf_claim(dtp, &dof));
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate void
dtrace_dof_destroy(dtrace_hdl_t * dtp,void * dof)8950Sstevel@tonic-gate dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate dt_free(dtp, dof);
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate void *
dtrace_getopt_dof(dtrace_hdl_t * dtp)9010Sstevel@tonic-gate dtrace_getopt_dof(dtrace_hdl_t *dtp)
9020Sstevel@tonic-gate {
9030Sstevel@tonic-gate dof_hdr_t *dof;
9040Sstevel@tonic-gate dof_sec_t *sec;
9050Sstevel@tonic-gate dof_optdesc_t *dofo;
9060Sstevel@tonic-gate int i, nopts = 0, len = sizeof (dof_hdr_t) +
9070Sstevel@tonic-gate roundup(sizeof (dof_sec_t), sizeof (uint64_t));
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate for (i = 0; i < DTRACEOPT_MAX; i++) {
9100Sstevel@tonic-gate if (dtp->dt_options[i] != DTRACEOPT_UNSET)
9110Sstevel@tonic-gate nopts++;
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate len += sizeof (dof_optdesc_t) * nopts;
9150Sstevel@tonic-gate
9161710Sahl if ((dof = dt_zalloc(dtp, len)) == NULL ||
9171710Sahl dof_hdr(dtp, DOF_VERSION, dof) != 0) {
9180Sstevel@tonic-gate dt_free(dtp, dof);
9190Sstevel@tonic-gate return (NULL);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */
9230Sstevel@tonic-gate dof->dofh_loadsz = len;
9240Sstevel@tonic-gate dof->dofh_filesz = len;
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate /*
9270Sstevel@tonic-gate * Fill in the option section header...
9280Sstevel@tonic-gate */
9290Sstevel@tonic-gate sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
9300Sstevel@tonic-gate sec->dofs_type = DOF_SECT_OPTDESC;
9310Sstevel@tonic-gate sec->dofs_align = sizeof (uint64_t);
9320Sstevel@tonic-gate sec->dofs_flags = DOF_SECF_LOAD;
9330Sstevel@tonic-gate sec->dofs_entsize = sizeof (dof_optdesc_t);
9340Sstevel@tonic-gate
9350Sstevel@tonic-gate dofo = (dof_optdesc_t *)((uintptr_t)sec +
9360Sstevel@tonic-gate roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
9370Sstevel@tonic-gate
9380Sstevel@tonic-gate sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;
9390Sstevel@tonic-gate sec->dofs_size = sizeof (dof_optdesc_t) * nopts;
9400Sstevel@tonic-gate
9410Sstevel@tonic-gate for (i = 0; i < DTRACEOPT_MAX; i++) {
9420Sstevel@tonic-gate if (dtp->dt_options[i] == DTRACEOPT_UNSET)
9430Sstevel@tonic-gate continue;
9440Sstevel@tonic-gate
9450Sstevel@tonic-gate dofo->dofo_option = i;
9460Sstevel@tonic-gate dofo->dofo_strtab = DOF_SECIDX_NONE;
9470Sstevel@tonic-gate dofo->dofo_value = dtp->dt_options[i];
9480Sstevel@tonic-gate dofo++;
9490Sstevel@tonic-gate }
9500Sstevel@tonic-gate
9510Sstevel@tonic-gate return (dof);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate
9540Sstevel@tonic-gate void *
dtrace_geterr_dof(dtrace_hdl_t * dtp)9550Sstevel@tonic-gate dtrace_geterr_dof(dtrace_hdl_t *dtp)
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate if (dtp->dt_errprog != NULL)
9580Sstevel@tonic-gate return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));
9590Sstevel@tonic-gate
9600Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_BADERROR);
9610Sstevel@tonic-gate return (NULL);
9620Sstevel@tonic-gate }
963