4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011 by Delphix. All rights reserved.
27 #include <sys/types.h>
29 #include <sys/sysmacros.h>
42 #include <dt_strtab.h>
43 #include <dt_program.h>
44 #include <dt_provider.h>
45 #include <dt_xlator.h>
49 dt_dof_init(dtrace_hdl_t *dtp)
51 dt_dof_t *ddo = &dtp->dt_dof;
55 ddo->ddo_strsec = DOF_SECIDX_NONE;
56 ddo->ddo_xlimport = NULL;
57 ddo->ddo_xlexport = NULL;
59 dt_buf_create(dtp, &ddo->ddo_secs, "section headers", 0);
60 dt_buf_create(dtp, &ddo->ddo_strs, "string table", 0);
61 dt_buf_create(dtp, &ddo->ddo_ldata, "loadable data", 0);
62 dt_buf_create(dtp, &ddo->ddo_udata, "unloadable data", 0);
64 dt_buf_create(dtp, &ddo->ddo_probes, "probe data", 0);
65 dt_buf_create(dtp, &ddo->ddo_args, "probe args", 0);
66 dt_buf_create(dtp, &ddo->ddo_offs, "probe offs", 0);
67 dt_buf_create(dtp, &ddo->ddo_enoffs, "probe is-enabled offs", 0);
68 dt_buf_create(dtp, &ddo->ddo_rels, "probe rels", 0);
70 dt_buf_create(dtp, &ddo->ddo_xlms, "xlate members", 0);
74 dt_dof_fini(dtrace_hdl_t *dtp)
76 dt_dof_t *ddo = &dtp->dt_dof;
78 dt_free(dtp, ddo->ddo_xlimport);
79 dt_free(dtp, ddo->ddo_xlexport);
81 dt_buf_destroy(dtp, &ddo->ddo_secs);
82 dt_buf_destroy(dtp, &ddo->ddo_strs);
83 dt_buf_destroy(dtp, &ddo->ddo_ldata);
84 dt_buf_destroy(dtp, &ddo->ddo_udata);
86 dt_buf_destroy(dtp, &ddo->ddo_probes);
87 dt_buf_destroy(dtp, &ddo->ddo_args);
88 dt_buf_destroy(dtp, &ddo->ddo_offs);
89 dt_buf_destroy(dtp, &ddo->ddo_enoffs);
90 dt_buf_destroy(dtp, &ddo->ddo_rels);
92 dt_buf_destroy(dtp, &ddo->ddo_xlms);
96 dt_dof_reset(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
98 dt_dof_t *ddo = &dtp->dt_dof;
99 uint_t i, nx = dtp->dt_xlatorid;
101 assert(ddo->ddo_hdl == dtp);
105 ddo->ddo_strsec = DOF_SECIDX_NONE;
107 dt_free(dtp, ddo->ddo_xlimport);
108 dt_free(dtp, ddo->ddo_xlexport);
110 ddo->ddo_xlimport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
111 ddo->ddo_xlexport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
113 if (nx != 0 && (ddo->ddo_xlimport == NULL || ddo->ddo_xlexport == NULL))
114 return (-1); /* errno is set for us */
116 for (i = 0; i < nx; i++) {
117 ddo->ddo_xlimport[i] = DOF_SECIDX_NONE;
118 ddo->ddo_xlexport[i] = DOF_SECIDX_NONE;
121 dt_buf_reset(dtp, &ddo->ddo_secs);
122 dt_buf_reset(dtp, &ddo->ddo_strs);
123 dt_buf_reset(dtp, &ddo->ddo_ldata);
124 dt_buf_reset(dtp, &ddo->ddo_udata);
126 dt_buf_reset(dtp, &ddo->ddo_probes);
127 dt_buf_reset(dtp, &ddo->ddo_args);
128 dt_buf_reset(dtp, &ddo->ddo_offs);
129 dt_buf_reset(dtp, &ddo->ddo_enoffs);
130 dt_buf_reset(dtp, &ddo->ddo_rels);
132 dt_buf_reset(dtp, &ddo->ddo_xlms);
137 * Add a loadable DOF section to the file using the specified data buffer and
138 * the specified DOF section attributes. DOF_SECF_LOAD must be set in flags.
139 * If 'data' is NULL, the caller is responsible for manipulating the ldata buf.
142 dof_add_lsect(dt_dof_t *ddo, const void *data, uint32_t type,
143 uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
145 dtrace_hdl_t *dtp = ddo->ddo_hdl;
149 s.dofs_align = align;
150 s.dofs_flags = flags | DOF_SECF_LOAD;
151 s.dofs_entsize = entsize;
152 s.dofs_offset = dt_buf_offset(&ddo->ddo_ldata, align);
155 dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
158 dt_buf_write(dtp, &ddo->ddo_ldata, data, size, align);
160 return (ddo->ddo_nsecs++);
164 * Add an unloadable DOF section to the file using the specified data buffer
165 * and DOF section attributes. DOF_SECF_LOAD must *not* be set in flags.
166 * If 'data' is NULL, the caller is responsible for manipulating the udata buf.
169 dof_add_usect(dt_dof_t *ddo, const void *data, uint32_t type,
170 uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
172 dtrace_hdl_t *dtp = ddo->ddo_hdl;
176 s.dofs_align = align;
177 s.dofs_flags = flags & ~DOF_SECF_LOAD;
178 s.dofs_entsize = entsize;
179 s.dofs_offset = dt_buf_offset(&ddo->ddo_udata, align);
182 dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
185 dt_buf_write(dtp, &ddo->ddo_udata, data, size, align);
187 return (ddo->ddo_nsecs++);
191 * Add a string to the global string table associated with the DOF. The offset
192 * of the string is returned as an index into the string table.
195 dof_add_string(dt_dof_t *ddo, const char *s)
197 dt_buf_t *bp = &ddo->ddo_strs;
198 dof_stridx_t i = dt_buf_len(bp);
200 if (i != 0 && (s == NULL || *s == '\0'))
201 return (0); /* string table has \0 at offset 0 */
203 dt_buf_write(ddo->ddo_hdl, bp, s, strlen(s) + 1, sizeof (char));
208 dof_attr(const dtrace_attribute_t *ap)
210 return (DOF_ATTR(ap->dtat_name, ap->dtat_data, ap->dtat_class));
214 dof_add_difo(dt_dof_t *ddo, const dtrace_difo_t *dp)
216 dof_secidx_t dsecs[5]; /* enough for all possible DIFO sections */
223 dof_secidx_t strsec = DOF_SECIDX_NONE;
224 dof_secidx_t intsec = DOF_SECIDX_NONE;
225 dof_secidx_t hdrsec = DOF_SECIDX_NONE;
227 if (dp->dtdo_buf != NULL) {
228 dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_buf,
229 DOF_SECT_DIF, sizeof (dif_instr_t), 0,
230 sizeof (dif_instr_t), sizeof (dif_instr_t) * dp->dtdo_len);
233 if (dp->dtdo_inttab != NULL) {
234 dsecs[nsecs++] = intsec = dof_add_lsect(ddo, dp->dtdo_inttab,
235 DOF_SECT_INTTAB, sizeof (uint64_t), 0,
236 sizeof (uint64_t), sizeof (uint64_t) * dp->dtdo_intlen);
239 if (dp->dtdo_strtab != NULL) {
240 dsecs[nsecs++] = strsec = dof_add_lsect(ddo, dp->dtdo_strtab,
241 DOF_SECT_STRTAB, sizeof (char), 0, 0, dp->dtdo_strlen);
244 if (dp->dtdo_vartab != NULL) {
245 dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_vartab,
246 DOF_SECT_VARTAB, sizeof (uint_t), 0, sizeof (dtrace_difv_t),
247 sizeof (dtrace_difv_t) * dp->dtdo_varlen);
250 if (dp->dtdo_xlmtab != NULL) {
251 dof_xlref_t *xlt, *xlp;
254 xlt = alloca(sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
255 pnp = dp->dtdo_xlmtab;
258 * dtdo_xlmtab contains pointers to the translator members.
259 * The translator itself is in sect ddo_xlimport[dxp->dx_id].
260 * The XLMEMBERS entries are in order by their dn_membid, so
261 * the member section offset is the population count of bits
262 * in ddo_pgp->dp_xlrefs[] up to and not including dn_membid.
264 for (xlp = xlt; xlp < xlt + dp->dtdo_xlmlen; xlp++) {
265 dt_node_t *dnp = *pnp++;
266 dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
268 xlp->dofxr_xlator = ddo->ddo_xlimport[dxp->dx_id];
269 xlp->dofxr_member = dt_popcb(
270 ddo->ddo_pgp->dp_xrefs[dxp->dx_id], dnp->dn_membid);
271 xlp->dofxr_argn = (uint32_t)dxp->dx_arg;
274 dsecs[nsecs++] = dof_add_lsect(ddo, xlt, DOF_SECT_XLTAB,
275 sizeof (dof_secidx_t), 0, sizeof (dof_xlref_t),
276 sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
280 * Copy the return type and the array of section indices that form the
281 * DIFO into a single dof_difohdr_t and then add DOF_SECT_DIFOHDR.
283 assert(nsecs <= sizeof (dsecs) / sizeof (dsecs[0]));
284 dofd = alloca(sizeof (dtrace_diftype_t) + sizeof (dsecs));
285 bcopy(&dp->dtdo_rtype, &dofd->dofd_rtype, sizeof (dtrace_diftype_t));
286 bcopy(dsecs, &dofd->dofd_links, sizeof (dof_secidx_t) * nsecs);
288 hdrsec = dof_add_lsect(ddo, dofd, DOF_SECT_DIFOHDR,
289 sizeof (dof_secidx_t), 0, 0,
290 sizeof (dtrace_diftype_t) + sizeof (dof_secidx_t) * nsecs);
293 * Add any other sections related to dtrace_difo_t. These are not
294 * referenced in dof_difohdr_t because they are not used by emulation.
296 if (dp->dtdo_kreltab != NULL) {
297 relsec = dof_add_lsect(ddo, dp->dtdo_kreltab, DOF_SECT_RELTAB,
298 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
299 sizeof (dof_relodesc_t) * dp->dtdo_krelen);
302 * This code assumes the target of all relocations is the
303 * integer table 'intsec' (DOF_SECT_INTTAB). If other sections
304 * need relocation in the future this will need to change.
306 dofr.dofr_strtab = strsec;
307 dofr.dofr_relsec = relsec;
308 dofr.dofr_tgtsec = intsec;
310 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_KRELHDR,
311 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
314 if (dp->dtdo_ureltab != NULL) {
315 relsec = dof_add_lsect(ddo, dp->dtdo_ureltab, DOF_SECT_RELTAB,
316 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
317 sizeof (dof_relodesc_t) * dp->dtdo_urelen);
320 * This code assumes the target of all relocations is the
321 * integer table 'intsec' (DOF_SECT_INTTAB). If other sections
322 * need relocation in the future this will need to change.
324 dofr.dofr_strtab = strsec;
325 dofr.dofr_relsec = relsec;
326 dofr.dofr_tgtsec = intsec;
328 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
329 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
336 dof_add_translator(dt_dof_t *ddo, const dt_xlator_t *dxp, uint_t type)
338 dtrace_hdl_t *dtp = ddo->ddo_hdl;
339 dof_xlmember_t dofxm;
343 char buf[DT_TYPE_NAMELEN];
347 assert(type == DOF_SECT_XLIMPORT || type == DOF_SECT_XLEXPORT);
348 xst = type == DOF_SECT_XLIMPORT ? ddo->ddo_xlimport : ddo->ddo_xlexport;
350 if (xst[dxp->dx_id] != DOF_SECIDX_NONE)
351 return; /* translator has already been emitted */
353 dt_buf_reset(dtp, &ddo->ddo_xlms);
356 * Generate an array of dof_xlmember_t's into ddo_xlms. If we are
357 * importing the translator, add only those members referenced by the
358 * program and set the dofxm_difo reference of each member to NONE. If
359 * we're exporting the translator, add all members and a DIFO for each.
361 for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list, i++) {
362 if (type == DOF_SECT_XLIMPORT) {
363 if (!BT_TEST(ddo->ddo_pgp->dp_xrefs[dxp->dx_id], i))
364 continue; /* member is not referenced */
365 dofxm.dofxm_difo = DOF_SECIDX_NONE;
367 dofxm.dofxm_difo = dof_add_difo(ddo,
368 dxp->dx_membdif[dnp->dn_membid]);
371 dofxm.dofxm_name = dof_add_string(ddo, dnp->dn_membname);
372 dt_node_diftype(dtp, dnp, &dofxm.dofxm_type);
374 dt_buf_write(dtp, &ddo->ddo_xlms,
375 &dofxm, sizeof (dofxm), sizeof (uint32_t));
378 dofxl.dofxl_members = dof_add_lsect(ddo, NULL, DOF_SECT_XLMEMBERS,
379 sizeof (uint32_t), 0, sizeof (dofxm), dt_buf_len(&ddo->ddo_xlms));
381 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_xlms, sizeof (uint32_t));
383 dofxl.dofxl_strtab = ddo->ddo_strsec;
384 dofxl.dofxl_argv = dof_add_string(ddo, ctf_type_name(
385 dxp->dx_src_ctfp, dxp->dx_src_type, buf, sizeof (buf)));
386 dofxl.dofxl_argc = 1;
387 dofxl.dofxl_type = dof_add_string(ddo, ctf_type_name(
388 dxp->dx_dst_ctfp, dxp->dx_dst_type, buf, sizeof (buf)));
389 dofxl.dofxl_attr = dof_attr(&dxp->dx_souid.di_attr);
391 xst[dxp->dx_id] = dof_add_lsect(ddo, &dofxl, type,
392 sizeof (uint32_t), 0, 0, sizeof (dofxl));
397 dof_add_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
399 dt_dof_t *ddo = data;
400 dtrace_hdl_t *dtp = ddo->ddo_hdl;
401 dt_probe_t *prp = idp->di_data;
405 dt_probe_instance_t *pip;
408 char buf[DT_TYPE_NAMELEN];
411 dofpr.dofpr_addr = 0;
412 dofpr.dofpr_name = dof_add_string(ddo, prp->pr_name);
413 dofpr.dofpr_nargv = dt_buf_len(&ddo->ddo_strs);
415 for (dnp = prp->pr_nargs; dnp != NULL; dnp = dnp->dn_list) {
416 (void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
417 dnp->dn_type, buf, sizeof (buf)));
420 dofpr.dofpr_xargv = dt_buf_len(&ddo->ddo_strs);
422 for (dnp = prp->pr_xargs; dnp != NULL; dnp = dnp->dn_list) {
423 (void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
424 dnp->dn_type, buf, sizeof (buf)));
427 dofpr.dofpr_argidx = dt_buf_len(&ddo->ddo_args) / sizeof (uint8_t);
429 for (i = 0; i < prp->pr_xargc; i++) {
430 dt_buf_write(dtp, &ddo->ddo_args, &prp->pr_mapping[i],
431 sizeof (uint8_t), sizeof (uint8_t));
434 dofpr.dofpr_nargc = prp->pr_nargc;
435 dofpr.dofpr_xargc = prp->pr_xargc;
436 dofpr.dofpr_pad1 = 0;
437 dofpr.dofpr_pad2 = 0;
439 for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
440 dt_dprintf("adding probe for %s:%s\n", pip->pi_fname,
443 dofpr.dofpr_func = dof_add_string(ddo, pip->pi_fname);
446 * There should be one probe offset or is-enabled probe offset
447 * or else this probe instance won't have been created. The
448 * kernel will reject DOF which has a probe with no offsets.
450 assert(pip->pi_noffs + pip->pi_nenoffs > 0);
453 dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t);
454 dofpr.dofpr_noffs = pip->pi_noffs;
455 dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs,
456 pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
458 dofpr.dofpr_enoffidx =
459 dt_buf_len(&ddo->ddo_enoffs) / sizeof (uint32_t);
460 dofpr.dofpr_nenoffs = pip->pi_nenoffs;
461 dt_buf_write(dtp, &ddo->ddo_enoffs, pip->pi_enoffs,
462 pip->pi_nenoffs * sizeof (uint32_t), sizeof (uint32_t));
465 * If pi_rname isn't set, the relocation will be against the
466 * function name. If it is, the relocation will be against
467 * pi_rname. This will be used if the function is scoped
468 * locally so an alternate symbol is added for the purpose
469 * of this relocation.
471 if (pip->pi_rname[0] == '\0')
472 dofr.dofr_name = dofpr.dofpr_func;
474 dofr.dofr_name = dof_add_string(ddo, pip->pi_rname);
475 dofr.dofr_type = DOF_RELO_SETX;
476 dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes);
479 dt_buf_write(dtp, &ddo->ddo_rels, &dofr,
480 sizeof (dofr), sizeof (uint64_t));
482 dt_buf_write(dtp, &ddo->ddo_probes, &dofpr,
483 sizeof (dofpr), sizeof (uint64_t));
490 dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp)
492 dtrace_hdl_t *dtp = ddo->ddo_hdl;
493 dof_provider_t dofpv;
500 if (pvp->pv_flags & DT_PROVIDER_IMPL)
501 return; /* ignore providers that are exported by dtrace(7D) */
503 nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax);
504 dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1));
505 xr = 1; /* reserve dofs[0] for the provider itself */
508 * For each translator referenced by the provider (pv_xrefs), emit an
509 * exported translator section for it if one hasn't been created yet.
511 for (i = 0; i < pvp->pv_xrmax; i++) {
512 if (BT_TEST(pvp->pv_xrefs, i) &&
513 dtp->dt_xlatemode == DT_XL_DYNAMIC) {
514 dof_add_translator(ddo,
515 dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT);
516 dofs[xr++] = ddo->ddo_xlexport[i];
520 dt_buf_reset(dtp, &ddo->ddo_probes);
521 dt_buf_reset(dtp, &ddo->ddo_args);
522 dt_buf_reset(dtp, &ddo->ddo_offs);
523 dt_buf_reset(dtp, &ddo->ddo_enoffs);
524 dt_buf_reset(dtp, &ddo->ddo_rels);
526 (void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo);
528 dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES,
529 sizeof (uint64_t), 0, sizeof (dof_probe_t),
530 dt_buf_len(&ddo->ddo_probes));
532 dt_buf_concat(dtp, &ddo->ddo_ldata,
533 &ddo->ddo_probes, sizeof (uint64_t));
535 dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS,
536 sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args));
538 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t));
540 dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS,
541 sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs));
543 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t));
545 if ((sz = dt_buf_len(&ddo->ddo_enoffs)) != 0) {
546 dofpv.dofpv_prenoffs = dof_add_lsect(ddo, NULL,
547 DOF_SECT_PRENOFFS, sizeof (uint_t), 0, sizeof (uint_t), sz);
549 dofpv.dofpv_prenoffs = DOF_SECT_NONE;
552 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_enoffs, sizeof (uint_t));
554 dofpv.dofpv_strtab = ddo->ddo_strsec;
555 dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name);
557 dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider);
558 dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod);
559 dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func);
560 dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name);
561 dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args);
563 dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER,
564 sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t));
566 dofr.dofr_strtab = dofpv.dofpv_strtab;
567 dofr.dofr_tgtsec = dofpv.dofpv_probes;
568 dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB,
569 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
570 dt_buf_len(&ddo->ddo_rels));
572 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t));
574 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
575 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
577 if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) {
578 (void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT,
579 sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t),
580 sizeof (dof_secidx_t) * (nxr + 1));
585 dof_hdr(dtrace_hdl_t *dtp, uint8_t dofversion, dof_hdr_t *hp)
588 * If our config values cannot fit in a uint8_t, we can't generate a
589 * DOF header since the values won't fit. This can only happen if the
590 * user forcibly compiles a program with an artificial configuration.
592 if (dtp->dt_conf.dtc_difversion > UINT8_MAX ||
593 dtp->dt_conf.dtc_difintregs > UINT8_MAX ||
594 dtp->dt_conf.dtc_diftupregs > UINT8_MAX)
595 return (dt_set_errno(dtp, EOVERFLOW));
597 bzero(hp, sizeof (dof_hdr_t));
599 hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
600 hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
601 hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
602 hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
604 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
605 hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64;
607 hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32;
609 hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
610 hp->dofh_ident[DOF_ID_VERSION] = dofversion;
611 hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion;
612 hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs;
613 hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs;
615 hp->dofh_hdrsize = sizeof (dof_hdr_t);
616 hp->dofh_secsize = sizeof (dof_sec_t);
617 hp->dofh_secoff = sizeof (dof_hdr_t);
623 dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags)
625 dt_dof_t *ddo = &dtp->dt_dof;
627 const dtrace_ecbdesc_t *edp, *last;
628 const dtrace_probedesc_t *pdp;
629 const dtrace_actdesc_t *ap;
630 const dt_stmt_t *stp;
646 if (flags & ~DTRACE_D_MASK) {
647 (void) dt_set_errno(dtp, EINVAL);
651 flags |= dtp->dt_dflags;
653 if (dof_hdr(dtp, pgp->dp_dofversion, &h) != 0)
656 if (dt_dof_reset(dtp, pgp) != 0)
660 * Iterate through the statement list computing the maximum number of
661 * actions and the maximum format string for allocating local buffers.
663 for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
664 stp != NULL; stp = dt_list_next(stp), last = edp) {
666 dtrace_stmtdesc_t *sdp = stp->ds_desc;
667 dtrace_actdesc_t *ap = sdp->dtsd_action;
669 if (sdp->dtsd_fmtdata != NULL) {
670 i = dtrace_printf_format(dtp,
671 sdp->dtsd_fmtdata, NULL, 0);
672 maxfmt = MAX(maxfmt, i);
675 if ((edp = sdp->dtsd_ecbdesc) == last)
676 continue; /* same ecb as previous statement */
678 for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next)
681 maxacts = MAX(maxacts, i);
684 dofa = alloca(sizeof (dof_actdesc_t) * maxacts);
685 fmt = alloca(maxfmt + 1);
687 ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0);
688 (void) dof_add_string(ddo, "");
691 * If there are references to dynamic translators in the program, add
692 * an imported translator table entry for each referenced translator.
694 if (pgp->dp_xrefslen != 0) {
695 for (dxp = dt_list_next(&dtp->dt_xlators);
696 dxp != NULL; dxp = dt_list_next(dxp)) {
697 if (dxp->dx_id < pgp->dp_xrefslen &&
698 pgp->dp_xrefs[dxp->dx_id] != NULL)
699 dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT);
704 * Now iterate through the statement list, creating the DOF section
705 * headers and data for each one and adding them to our buffers.
707 for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
708 stp != NULL; stp = dt_list_next(stp), last = edp) {
710 dof_secidx_t probesec = DOF_SECIDX_NONE;
711 dof_secidx_t prdsec = DOF_SECIDX_NONE;
712 dof_secidx_t actsec = DOF_SECIDX_NONE;
714 const dt_stmt_t *next = stp;
715 dtrace_stmtdesc_t *sdp = stp->ds_desc;
716 dof_stridx_t strndx = 0;
717 dof_probedesc_t dofp;
721 if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)
722 continue; /* same ecb as previous statement */
724 pdp = &edp->dted_probe;
727 * Add a DOF_SECT_PROBEDESC for the ECB's probe description,
728 * and copy the probe description strings into the string table.
730 dofp.dofp_strtab = ddo->ddo_strsec;
731 dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider);
732 dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod);
733 dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func);
734 dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name);
735 dofp.dofp_id = pdp->dtpd_id;
737 probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC,
738 sizeof (dof_secidx_t), 0,
739 sizeof (dof_probedesc_t), sizeof (dof_probedesc_t));
742 * If there is a predicate DIFO associated with the ecbdesc,
743 * write out the DIFO sections and save the DIFO section index.
745 if (edp->dted_pred.dtpdd_difo != NULL)
746 prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo);
749 * Now iterate through the action list generating DIFOs as
750 * referenced therein and adding action descriptions to 'dofa'.
752 for (i = 0, ap = edp->dted_action;
753 ap != NULL; ap = ap->dtad_next, i++) {
755 if (ap->dtad_difo != NULL) {
757 dof_add_difo(ddo, ap->dtad_difo);
759 dofa[i].dofa_difo = DOF_SECIDX_NONE;
762 * If the first action in a statement has string data,
763 * add the string to the global string table. This can
764 * be due either to a printf() format string
765 * (dtsd_fmtdata) or a print() type string
768 if (sdp != NULL && ap == sdp->dtsd_action) {
769 if (sdp->dtsd_fmtdata != NULL) {
770 (void) dtrace_printf_format(dtp,
771 sdp->dtsd_fmtdata, fmt, maxfmt + 1);
772 strndx = dof_add_string(ddo, fmt);
773 } else if (sdp->dtsd_strdata != NULL) {
774 strndx = dof_add_string(ddo,
777 strndx = 0; /* use dtad_arg instead */
780 if ((next = dt_list_next(next)) != NULL)
787 dofa[i].dofa_arg = strndx;
788 dofa[i].dofa_strtab = ddo->ddo_strsec;
790 dofa[i].dofa_arg = ap->dtad_arg;
791 dofa[i].dofa_strtab = DOF_SECIDX_NONE;
794 dofa[i].dofa_kind = ap->dtad_kind;
795 dofa[i].dofa_ntuple = ap->dtad_ntuple;
796 dofa[i].dofa_uarg = ap->dtad_uarg;
800 actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC,
801 sizeof (uint64_t), 0, sizeof (dof_actdesc_t),
802 sizeof (dof_actdesc_t) * i);
806 * Now finally, add the DOF_SECT_ECBDESC referencing all the
807 * previously created sub-sections.
809 dofe.dofe_probes = probesec;
810 dofe.dofe_pred = prdsec;
811 dofe.dofe_actions = actsec;
813 dofe.dofe_uarg = edp->dted_uarg;
815 (void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC,
816 sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t));
820 * If any providers are user-defined, output DOF sections corresponding
821 * to the providers and the probes and arguments that they define.
823 if (flags & DTRACE_D_PROBES) {
824 for (pvp = dt_list_next(&dtp->dt_provlist);
825 pvp != NULL; pvp = dt_list_next(pvp))
826 dof_add_provider(ddo, pvp);
830 * If we're not stripping unloadable sections, generate compiler
831 * comments and any other unloadable miscellany.
833 if (!(flags & DTRACE_D_STRIP)) {
834 (void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS,
835 sizeof (char), 0, 0, strlen(_dtrace_version) + 1);
836 (void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME,
837 sizeof (char), 0, 0, sizeof (struct utsname));
841 * Compute and fill in the appropriate values for the dof_hdr_t's
842 * dofh_secnum, dofh_loadsz, and dofh_filez values.
844 h.dofh_secnum = ddo->ddo_nsecs;
845 ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs);
847 h.dofh_loadsz = ssize +
848 dt_buf_len(&ddo->ddo_ldata) +
849 dt_buf_len(&ddo->ddo_strs);
851 if (dt_buf_len(&ddo->ddo_udata) != 0) {
852 lsize = roundup(h.dofh_loadsz, sizeof (uint64_t));
853 h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata);
855 lsize = h.dofh_loadsz;
856 h.dofh_filesz = lsize;
860 * Set the global DOF_SECT_STRTAB's offset to be after the header,
861 * section headers, and other loadable data. Since we're going to
862 * iterate over the buffer data directly, we must check for errors.
864 if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) {
865 (void) dt_set_errno(dtp, i);
869 sp = dt_buf_ptr(&ddo->ddo_secs);
870 assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB);
871 assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs);
873 sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata);
874 sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs);
877 * Now relocate all the other section headers by adding the appropriate
878 * delta to their respective dofs_offset values.
880 for (i = 0; i < ddo->ddo_nsecs; i++, sp++) {
881 if (i == ddo->ddo_strsec)
882 continue; /* already relocated above */
884 if (sp->dofs_flags & DOF_SECF_LOAD)
885 sp->dofs_offset += ssize;
887 sp->dofs_offset += lsize;
891 * Finally, assemble the complete in-memory DOF buffer by writing the
892 * header and then concatenating all our buffers. dt_buf_concat() will
893 * propagate any errors and cause dt_buf_claim() to return NULL.
895 dt_buf_create(dtp, &dof, "dof", h.dofh_filesz);
897 dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t));
898 dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t));
899 dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t));
900 dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char));
901 dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t));
903 return (dt_buf_claim(dtp, &dof));
907 dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof)
913 dtrace_getopt_dof(dtrace_hdl_t *dtp)
918 int i, nopts = 0, len = sizeof (dof_hdr_t) +
919 roundup(sizeof (dof_sec_t), sizeof (uint64_t));
921 for (i = 0; i < DTRACEOPT_MAX; i++) {
922 if (dtp->dt_options[i] != DTRACEOPT_UNSET)
926 len += sizeof (dof_optdesc_t) * nopts;
928 if ((dof = dt_zalloc(dtp, len)) == NULL ||
929 dof_hdr(dtp, DOF_VERSION, dof) != 0) {
934 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */
935 dof->dofh_loadsz = len;
936 dof->dofh_filesz = len;
939 * Fill in the option section header...
941 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
942 sec->dofs_type = DOF_SECT_OPTDESC;
943 sec->dofs_align = sizeof (uint64_t);
944 sec->dofs_flags = DOF_SECF_LOAD;
945 sec->dofs_entsize = sizeof (dof_optdesc_t);
947 dofo = (dof_optdesc_t *)((uintptr_t)sec +
948 roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
950 sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;
951 sec->dofs_size = sizeof (dof_optdesc_t) * nopts;
953 for (i = 0; i < DTRACEOPT_MAX; i++) {
954 if (dtp->dt_options[i] == DTRACEOPT_UNSET)
957 dofo->dofo_option = i;
958 dofo->dofo_strtab = DOF_SECIDX_NONE;
959 dofo->dofo_value = dtp->dt_options[i];
967 dtrace_geterr_dof(dtrace_hdl_t *dtp)
969 if (dtp->dt_errprog != NULL)
970 return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));
972 (void) dt_set_errno(dtp, EDT_BADERROR);