xref: /titanic_44/usr/src/lib/libdtrace/common/dt_dof.c (revision e429788e241121c1f81089f762558027000ea25f)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/sysmacros.h>
31 
32 #include <strings.h>
33 #include <alloca.h>
34 #include <assert.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <limits.h>
38 
39 #include <dt_impl.h>
40 #include <dt_strtab.h>
41 #include <dt_program.h>
42 #include <dt_provider.h>
43 #include <dt_xlator.h>
44 #include <dt_dof.h>
45 
46 void
47 dt_dof_init(dtrace_hdl_t *dtp)
48 {
49 	dt_dof_t *ddo = &dtp->dt_dof;
50 
51 	ddo->ddo_hdl = dtp;
52 	ddo->ddo_nsecs = 0;
53 	ddo->ddo_strsec = DOF_SECIDX_NONE;
54 	ddo->ddo_xlimport = NULL;
55 	ddo->ddo_xlexport = NULL;
56 
57 	dt_buf_create(dtp, &ddo->ddo_secs, "section headers", 0);
58 	dt_buf_create(dtp, &ddo->ddo_strs, "string table", 0);
59 	dt_buf_create(dtp, &ddo->ddo_ldata, "loadable data", 0);
60 	dt_buf_create(dtp, &ddo->ddo_udata, "unloadable data", 0);
61 
62 	dt_buf_create(dtp, &ddo->ddo_probes, "probe data", 0);
63 	dt_buf_create(dtp, &ddo->ddo_args, "probe args", 0);
64 	dt_buf_create(dtp, &ddo->ddo_offs, "probe offs", 0);
65 	dt_buf_create(dtp, &ddo->ddo_enoffs, "probe is-enabled offs", 0);
66 	dt_buf_create(dtp, &ddo->ddo_rels, "probe rels", 0);
67 
68 	dt_buf_create(dtp, &ddo->ddo_xlms, "xlate members", 0);
69 }
70 
71 void
72 dt_dof_fini(dtrace_hdl_t *dtp)
73 {
74 	dt_dof_t *ddo = &dtp->dt_dof;
75 
76 	dt_free(dtp, ddo->ddo_xlimport);
77 	dt_free(dtp, ddo->ddo_xlexport);
78 
79 	dt_buf_destroy(dtp, &ddo->ddo_secs);
80 	dt_buf_destroy(dtp, &ddo->ddo_strs);
81 	dt_buf_destroy(dtp, &ddo->ddo_ldata);
82 	dt_buf_destroy(dtp, &ddo->ddo_udata);
83 
84 	dt_buf_destroy(dtp, &ddo->ddo_probes);
85 	dt_buf_destroy(dtp, &ddo->ddo_args);
86 	dt_buf_destroy(dtp, &ddo->ddo_offs);
87 	dt_buf_destroy(dtp, &ddo->ddo_enoffs);
88 	dt_buf_destroy(dtp, &ddo->ddo_rels);
89 
90 	dt_buf_destroy(dtp, &ddo->ddo_xlms);
91 }
92 
93 static int
94 dt_dof_reset(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
95 {
96 	dt_dof_t *ddo = &dtp->dt_dof;
97 	uint_t i, nx = dtp->dt_xlatorid;
98 
99 	assert(ddo->ddo_hdl == dtp);
100 	ddo->ddo_pgp = pgp;
101 
102 	ddo->ddo_nsecs = 0;
103 	ddo->ddo_strsec = DOF_SECIDX_NONE;
104 
105 	dt_free(dtp, ddo->ddo_xlimport);
106 	dt_free(dtp, ddo->ddo_xlexport);
107 
108 	ddo->ddo_xlimport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
109 	ddo->ddo_xlexport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
110 
111 	if (nx != 0 && (ddo->ddo_xlimport == NULL || ddo->ddo_xlexport == NULL))
112 		return (-1); /* errno is set for us */
113 
114 	for (i = 0; i < nx; i++) {
115 		ddo->ddo_xlimport[i] = DOF_SECIDX_NONE;
116 		ddo->ddo_xlexport[i] = DOF_SECIDX_NONE;
117 	}
118 
119 	dt_buf_reset(dtp, &ddo->ddo_secs);
120 	dt_buf_reset(dtp, &ddo->ddo_strs);
121 	dt_buf_reset(dtp, &ddo->ddo_ldata);
122 	dt_buf_reset(dtp, &ddo->ddo_udata);
123 
124 	dt_buf_reset(dtp, &ddo->ddo_probes);
125 	dt_buf_reset(dtp, &ddo->ddo_args);
126 	dt_buf_reset(dtp, &ddo->ddo_offs);
127 	dt_buf_reset(dtp, &ddo->ddo_enoffs);
128 	dt_buf_reset(dtp, &ddo->ddo_rels);
129 
130 	dt_buf_reset(dtp, &ddo->ddo_xlms);
131 	return (0);
132 }
133 
134 /*
135  * Add a loadable DOF section to the file using the specified data buffer and
136  * the specified DOF section attributes.  DOF_SECF_LOAD must be set in flags.
137  * If 'data' is NULL, the caller is responsible for manipulating the ldata buf.
138  */
139 static dof_secidx_t
140 dof_add_lsect(dt_dof_t *ddo, const void *data, uint32_t type,
141     uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
142 {
143 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
144 	dof_sec_t s;
145 
146 	s.dofs_type = type;
147 	s.dofs_align = align;
148 	s.dofs_flags = flags | DOF_SECF_LOAD;
149 	s.dofs_entsize = entsize;
150 	s.dofs_offset = dt_buf_offset(&ddo->ddo_ldata, align);
151 	s.dofs_size = size;
152 
153 	dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
154 
155 	if (data != NULL)
156 		dt_buf_write(dtp, &ddo->ddo_ldata, data, size, align);
157 
158 	return (ddo->ddo_nsecs++);
159 }
160 
161 /*
162  * Add an unloadable DOF section to the file using the specified data buffer
163  * and DOF section attributes.  DOF_SECF_LOAD must *not* be set in flags.
164  * If 'data' is NULL, the caller is responsible for manipulating the udata buf.
165  */
166 static dof_secidx_t
167 dof_add_usect(dt_dof_t *ddo, const void *data, uint32_t type,
168     uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
169 {
170 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
171 	dof_sec_t s;
172 
173 	s.dofs_type = type;
174 	s.dofs_align = align;
175 	s.dofs_flags = flags & ~DOF_SECF_LOAD;
176 	s.dofs_entsize = entsize;
177 	s.dofs_offset = dt_buf_offset(&ddo->ddo_udata, align);
178 	s.dofs_size = size;
179 
180 	dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
181 
182 	if (data != NULL)
183 		dt_buf_write(dtp, &ddo->ddo_udata, data, size, align);
184 
185 	return (ddo->ddo_nsecs++);
186 }
187 
188 /*
189  * Add a string to the global string table associated with the DOF.  The offset
190  * of the string is returned as an index into the string table.
191  */
192 static dof_stridx_t
193 dof_add_string(dt_dof_t *ddo, const char *s)
194 {
195 	dt_buf_t *bp = &ddo->ddo_strs;
196 	dof_stridx_t i = dt_buf_len(bp);
197 
198 	if (i != 0 && (s == NULL || *s == '\0'))
199 		return (0); /* string table has \0 at offset 0 */
200 
201 	dt_buf_write(ddo->ddo_hdl, bp, s, strlen(s) + 1, sizeof (char));
202 	return (i);
203 }
204 
205 static dof_attr_t
206 dof_attr(const dtrace_attribute_t *ap)
207 {
208 	return (DOF_ATTR(ap->dtat_name, ap->dtat_data, ap->dtat_class));
209 }
210 
211 static dof_secidx_t
212 dof_add_difo(dt_dof_t *ddo, const dtrace_difo_t *dp)
213 {
214 	dof_secidx_t dsecs[5]; /* enough for all possible DIFO sections */
215 	uint_t nsecs = 0;
216 
217 	dof_difohdr_t *dofd;
218 	dof_relohdr_t dofr;
219 	dof_secidx_t relsec;
220 
221 	dof_secidx_t strsec = DOF_SECIDX_NONE;
222 	dof_secidx_t intsec = DOF_SECIDX_NONE;
223 	dof_secidx_t hdrsec = DOF_SECIDX_NONE;
224 
225 	if (dp->dtdo_buf != NULL) {
226 		dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_buf,
227 		    DOF_SECT_DIF, sizeof (dif_instr_t), 0,
228 		    sizeof (dif_instr_t), sizeof (dif_instr_t) * dp->dtdo_len);
229 	}
230 
231 	if (dp->dtdo_inttab != NULL) {
232 		dsecs[nsecs++] = intsec = dof_add_lsect(ddo, dp->dtdo_inttab,
233 		    DOF_SECT_INTTAB, sizeof (uint64_t), 0,
234 		    sizeof (uint64_t), sizeof (uint64_t) * dp->dtdo_intlen);
235 	}
236 
237 	if (dp->dtdo_strtab != NULL) {
238 		dsecs[nsecs++] = strsec = dof_add_lsect(ddo, dp->dtdo_strtab,
239 		    DOF_SECT_STRTAB, sizeof (char), 0, 0, dp->dtdo_strlen);
240 	}
241 
242 	if (dp->dtdo_vartab != NULL) {
243 		dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_vartab,
244 		    DOF_SECT_VARTAB, sizeof (uint_t), 0, sizeof (dtrace_difv_t),
245 		    sizeof (dtrace_difv_t) * dp->dtdo_varlen);
246 	}
247 
248 	if (dp->dtdo_xlmtab != NULL) {
249 		dof_xlref_t *xlt, *xlp;
250 		dt_node_t **pnp;
251 
252 		xlt = alloca(sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
253 		pnp = dp->dtdo_xlmtab;
254 
255 		/*
256 		 * dtdo_xlmtab contains pointers to the translator members.
257 		 * The translator itself is in sect ddo_xlimport[dxp->dx_id].
258 		 * The XLMEMBERS entries are in order by their dn_membid, so
259 		 * the member section offset is the population count of bits
260 		 * in ddo_pgp->dp_xlrefs[] up to and not including dn_membid.
261 		 */
262 		for (xlp = xlt; xlp < xlt + dp->dtdo_xlmlen; xlp++) {
263 			dt_node_t *dnp = *pnp++;
264 			dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
265 
266 			xlp->dofxr_xlator = ddo->ddo_xlimport[dxp->dx_id];
267 			xlp->dofxr_member = dt_popcb(
268 			    ddo->ddo_pgp->dp_xrefs[dxp->dx_id], dnp->dn_membid);
269 			xlp->dofxr_argn = (uint32_t)dxp->dx_arg;
270 		}
271 
272 		dsecs[nsecs++] = dof_add_lsect(ddo, xlt, DOF_SECT_XLTAB,
273 		    sizeof (dof_secidx_t), 0, sizeof (dof_xlref_t),
274 		    sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
275 	}
276 
277 	/*
278 	 * Copy the return type and the array of section indices that form the
279 	 * DIFO into a single dof_difohdr_t and then add DOF_SECT_DIFOHDR.
280 	 */
281 	assert(nsecs <= sizeof (dsecs) / sizeof (dsecs[0]));
282 	dofd = alloca(sizeof (dtrace_diftype_t) + sizeof (dsecs));
283 	bcopy(&dp->dtdo_rtype, &dofd->dofd_rtype, sizeof (dtrace_diftype_t));
284 	bcopy(dsecs, &dofd->dofd_links, sizeof (dof_secidx_t) * nsecs);
285 
286 	hdrsec = dof_add_lsect(ddo, dofd, DOF_SECT_DIFOHDR,
287 	    sizeof (dof_secidx_t), 0, 0,
288 	    sizeof (dtrace_diftype_t) + sizeof (dof_secidx_t) * nsecs);
289 
290 	/*
291 	 * Add any other sections related to dtrace_difo_t.  These are not
292 	 * referenced in dof_difohdr_t because they are not used by emulation.
293 	 */
294 	if (dp->dtdo_kreltab != NULL) {
295 		relsec = dof_add_lsect(ddo, dp->dtdo_kreltab, DOF_SECT_RELTAB,
296 		    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
297 		    sizeof (dof_relodesc_t) * dp->dtdo_krelen);
298 
299 		/*
300 		 * This code assumes the target of all relocations is the
301 		 * integer table 'intsec' (DOF_SECT_INTTAB).  If other sections
302 		 * need relocation in the future this will need to change.
303 		 */
304 		dofr.dofr_strtab = strsec;
305 		dofr.dofr_relsec = relsec;
306 		dofr.dofr_tgtsec = intsec;
307 
308 		(void) dof_add_lsect(ddo, &dofr, DOF_SECT_KRELHDR,
309 		    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
310 	}
311 
312 	if (dp->dtdo_ureltab != NULL) {
313 		relsec = dof_add_lsect(ddo, dp->dtdo_ureltab, DOF_SECT_RELTAB,
314 		    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
315 		    sizeof (dof_relodesc_t) * dp->dtdo_urelen);
316 
317 		/*
318 		 * This code assumes the target of all relocations is the
319 		 * integer table 'intsec' (DOF_SECT_INTTAB).  If other sections
320 		 * need relocation in the future this will need to change.
321 		 */
322 		dofr.dofr_strtab = strsec;
323 		dofr.dofr_relsec = relsec;
324 		dofr.dofr_tgtsec = intsec;
325 
326 		(void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
327 		    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
328 	}
329 
330 	return (hdrsec);
331 }
332 
333 static void
334 dof_add_translator(dt_dof_t *ddo, const dt_xlator_t *dxp, uint_t type)
335 {
336 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
337 	dof_xlmember_t dofxm;
338 	dof_xlator_t dofxl;
339 	dof_secidx_t *xst;
340 
341 	char buf[DT_TYPE_NAMELEN];
342 	dt_node_t *dnp;
343 	uint_t i = 0;
344 
345 	assert(type == DOF_SECT_XLIMPORT || type == DOF_SECT_XLEXPORT);
346 	xst = type == DOF_SECT_XLIMPORT ? ddo->ddo_xlimport : ddo->ddo_xlexport;
347 
348 	if (xst[dxp->dx_id] != DOF_SECIDX_NONE)
349 		return; /* translator has already been emitted */
350 
351 	dt_buf_reset(dtp, &ddo->ddo_xlms);
352 
353 	/*
354 	 * Generate an array of dof_xlmember_t's into ddo_xlms.  If we are
355 	 * importing the translator, add only those members referenced by the
356 	 * program and set the dofxm_difo reference of each member to NONE.  If
357 	 * we're exporting the translator, add all members and a DIFO for each.
358 	 */
359 	for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list, i++) {
360 		if (type == DOF_SECT_XLIMPORT) {
361 			if (!BT_TEST(ddo->ddo_pgp->dp_xrefs[dxp->dx_id], i))
362 				continue; /* member is not referenced */
363 			dofxm.dofxm_difo = DOF_SECIDX_NONE;
364 		} else {
365 			dofxm.dofxm_difo = dof_add_difo(ddo,
366 			    dxp->dx_membdif[dnp->dn_membid]);
367 		}
368 
369 		dofxm.dofxm_name = dof_add_string(ddo, dnp->dn_membname);
370 		dt_node_diftype(dtp, dnp, &dofxm.dofxm_type);
371 
372 		dt_buf_write(dtp, &ddo->ddo_xlms,
373 		    &dofxm, sizeof (dofxm), sizeof (uint32_t));
374 	}
375 
376 	dofxl.dofxl_members = dof_add_lsect(ddo, NULL, DOF_SECT_XLMEMBERS,
377 	    sizeof (uint32_t), 0, sizeof (dofxm), dt_buf_len(&ddo->ddo_xlms));
378 
379 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_xlms, sizeof (uint32_t));
380 
381 	dofxl.dofxl_strtab = ddo->ddo_strsec;
382 	dofxl.dofxl_argv = dof_add_string(ddo, ctf_type_name(
383 	    dxp->dx_src_ctfp, dxp->dx_src_type, buf, sizeof (buf)));
384 	dofxl.dofxl_argc = 1;
385 	dofxl.dofxl_type = dof_add_string(ddo, ctf_type_name(
386 	    dxp->dx_dst_ctfp, dxp->dx_dst_type, buf, sizeof (buf)));
387 	dofxl.dofxl_attr = dof_attr(&dxp->dx_souid.di_attr);
388 
389 	xst[dxp->dx_id] = dof_add_lsect(ddo, &dofxl, type,
390 	    sizeof (uint32_t), 0, 0, sizeof (dofxl));
391 }
392 
393 /*ARGSUSED*/
394 static int
395 dof_add_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
396 {
397 	dt_dof_t *ddo = data;
398 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
399 	dt_probe_t *prp = idp->di_data;
400 
401 	dof_probe_t dofpr;
402 	dof_relodesc_t dofr;
403 	dt_probe_instance_t *pip;
404 	dt_node_t *dnp;
405 
406 	char buf[DT_TYPE_NAMELEN];
407 	uint_t i;
408 
409 	dofpr.dofpr_addr = 0;
410 	dofpr.dofpr_name = dof_add_string(ddo, prp->pr_name);
411 	dofpr.dofpr_nargv = dt_buf_len(&ddo->ddo_strs);
412 
413 	for (dnp = prp->pr_nargs; dnp != NULL; dnp = dnp->dn_list) {
414 		(void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
415 		    dnp->dn_type, buf, sizeof (buf)));
416 	}
417 
418 	dofpr.dofpr_xargv = dt_buf_len(&ddo->ddo_strs);
419 
420 	for (dnp = prp->pr_xargs; dnp != NULL; dnp = dnp->dn_list) {
421 		(void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
422 		    dnp->dn_type, buf, sizeof (buf)));
423 	}
424 
425 	dofpr.dofpr_argidx = dt_buf_len(&ddo->ddo_args) / sizeof (uint8_t);
426 
427 	for (i = 0; i < prp->pr_xargc; i++) {
428 		dt_buf_write(dtp, &ddo->ddo_args, &prp->pr_mapping[i],
429 		    sizeof (uint8_t), sizeof (uint8_t));
430 	}
431 
432 	dofpr.dofpr_nargc = prp->pr_nargc;
433 	dofpr.dofpr_xargc = prp->pr_xargc;
434 	dofpr.dofpr_pad1 = 0;
435 	dofpr.dofpr_pad2 = 0;
436 
437 	for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
438 		dt_dprintf("adding probe for %s:%s\n", pip->pi_fname,
439 		    prp->pr_name);
440 
441 		dofpr.dofpr_func = dof_add_string(ddo, pip->pi_fname);
442 
443 		/*
444 		 * There should be one probe offset or is-enabled probe offset
445 		 * or else this probe instance won't have been created. The
446 		 * kernel will reject DOF which has a probe with no offsets.
447 		 */
448 		assert(pip->pi_noffs + pip->pi_nenoffs > 0);
449 
450 		dofpr.dofpr_offidx =
451 		    dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t);
452 		dofpr.dofpr_noffs = pip->pi_noffs;
453 		dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs,
454 		    pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
455 
456 		dofpr.dofpr_enoffidx =
457 		    dt_buf_len(&ddo->ddo_enoffs) / sizeof (uint32_t);
458 		dofpr.dofpr_nenoffs = pip->pi_nenoffs;
459 		dt_buf_write(dtp, &ddo->ddo_enoffs, pip->pi_enoffs,
460 		    pip->pi_nenoffs * sizeof (uint32_t), sizeof (uint32_t));
461 
462 		/*
463 		 * If pi_rname isn't set, the relocation will be against the
464 		 * function name. If it is, the relocation will be against
465 		 * pi_rname. This will be used if the function is scoped
466 		 * locally so an alternate symbol is added for the purpose
467 		 * of this relocation.
468 		 */
469 		if (pip->pi_rname[0] == '\0')
470 			dofr.dofr_name = dofpr.dofpr_func;
471 		else
472 			dofr.dofr_name = dof_add_string(ddo, pip->pi_rname);
473 		dofr.dofr_type = DOF_RELO_SETX;
474 		dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes);
475 		dofr.dofr_data = 0;
476 
477 		dt_buf_write(dtp, &ddo->ddo_rels, &dofr,
478 		    sizeof (dofr), sizeof (uint64_t));
479 
480 		dt_buf_write(dtp, &ddo->ddo_probes, &dofpr,
481 		    sizeof (dofpr), sizeof (uint64_t));
482 	}
483 
484 	return (0);
485 }
486 
487 static void
488 dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp)
489 {
490 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
491 	dof_provider_t dofpv;
492 	dof_relohdr_t dofr;
493 	dof_secidx_t *dofs;
494 	ulong_t xr, nxr;
495 	size_t sz;
496 	id_t i;
497 
498 	if (pvp->pv_flags & DT_PROVIDER_IMPL)
499 		return; /* ignore providers that are exported by dtrace(7D) */
500 
501 	nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax);
502 	dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1));
503 	xr = 1; /* reserve dofs[0] for the provider itself */
504 
505 	/*
506 	 * For each translator referenced by the provider (pv_xrefs), emit an
507 	 * exported translator section for it if one hasn't been created yet.
508 	 */
509 	for (i = 0; i < pvp->pv_xrmax; i++) {
510 		if (BT_TEST(pvp->pv_xrefs, i) &&
511 		    dtp->dt_xlatemode == DT_XL_DYNAMIC) {
512 			dof_add_translator(ddo,
513 			    dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT);
514 			dofs[xr++] = ddo->ddo_xlexport[i];
515 		}
516 	}
517 
518 	dt_buf_reset(dtp, &ddo->ddo_probes);
519 	dt_buf_reset(dtp, &ddo->ddo_args);
520 	dt_buf_reset(dtp, &ddo->ddo_offs);
521 	dt_buf_reset(dtp, &ddo->ddo_enoffs);
522 	dt_buf_reset(dtp, &ddo->ddo_rels);
523 
524 	(void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo);
525 
526 	dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES,
527 	    sizeof (uint64_t), 0, sizeof (dof_probe_t),
528 	    dt_buf_len(&ddo->ddo_probes));
529 
530 	dt_buf_concat(dtp, &ddo->ddo_ldata,
531 	    &ddo->ddo_probes, sizeof (uint64_t));
532 
533 	dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS,
534 	    sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args));
535 
536 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t));
537 
538 	dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS,
539 	    sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs));
540 
541 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t));
542 
543 	if ((sz = dt_buf_len(&ddo->ddo_enoffs)) != 0) {
544 		dofpv.dofpv_prenoffs = dof_add_lsect(ddo, NULL,
545 		    DOF_SECT_PRENOFFS, sizeof (uint_t), 0, sizeof (uint_t), sz);
546 	} else {
547 		dofpv.dofpv_prenoffs = DOF_SECT_NONE;
548 	}
549 
550 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_enoffs, sizeof (uint_t));
551 
552 	dofpv.dofpv_strtab = ddo->ddo_strsec;
553 	dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name);
554 
555 	dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider);
556 	dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod);
557 	dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func);
558 	dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name);
559 	dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args);
560 
561 	dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER,
562 	    sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t));
563 
564 	dofr.dofr_strtab = dofpv.dofpv_strtab;
565 	dofr.dofr_tgtsec = dofpv.dofpv_probes;
566 	dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB,
567 	    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
568 	    dt_buf_len(&ddo->ddo_rels));
569 
570 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t));
571 
572 	(void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
573 	    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
574 
575 	if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) {
576 		(void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT,
577 		    sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t),
578 		    sizeof (dof_secidx_t) * (nxr + 1));
579 	}
580 }
581 
582 static int
583 dof_hdr(dtrace_hdl_t *dtp, uint8_t dofversion, dof_hdr_t *hp)
584 {
585 	/*
586 	 * If our config values cannot fit in a uint8_t, we can't generate a
587 	 * DOF header since the values won't fit.  This can only happen if the
588 	 * user forcibly compiles a program with an artificial configuration.
589 	 */
590 	if (dtp->dt_conf.dtc_difversion > UINT8_MAX ||
591 	    dtp->dt_conf.dtc_difintregs > UINT8_MAX ||
592 	    dtp->dt_conf.dtc_diftupregs > UINT8_MAX)
593 		return (dt_set_errno(dtp, EOVERFLOW));
594 
595 	bzero(hp, sizeof (dof_hdr_t));
596 
597 	hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
598 	hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
599 	hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
600 	hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
601 
602 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
603 		hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64;
604 	else
605 		hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32;
606 
607 	hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
608 	hp->dofh_ident[DOF_ID_VERSION] = dofversion;
609 	hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion;
610 	hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs;
611 	hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs;
612 
613 	hp->dofh_hdrsize = sizeof (dof_hdr_t);
614 	hp->dofh_secsize = sizeof (dof_sec_t);
615 	hp->dofh_secoff = sizeof (dof_hdr_t);
616 
617 	return (0);
618 }
619 
620 void *
621 dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags)
622 {
623 	dt_dof_t *ddo = &dtp->dt_dof;
624 
625 	const dtrace_ecbdesc_t *edp, *last;
626 	const dtrace_probedesc_t *pdp;
627 	const dtrace_actdesc_t *ap;
628 	const dt_stmt_t *stp;
629 
630 	uint_t maxacts = 0;
631 	uint_t maxfmt = 0;
632 
633 	dt_provider_t *pvp;
634 	dt_xlator_t *dxp;
635 	dof_actdesc_t *dofa;
636 	dof_sec_t *sp;
637 	size_t ssize, lsize;
638 	dof_hdr_t h;
639 
640 	dt_buf_t dof;
641 	char *fmt;
642 	uint_t i;
643 
644 	if (flags & ~DTRACE_D_MASK) {
645 		(void) dt_set_errno(dtp, EINVAL);
646 		return (NULL);
647 	}
648 
649 	flags |= dtp->dt_dflags;
650 
651 	if (dof_hdr(dtp, pgp->dp_dofversion, &h) != 0)
652 		return (NULL);
653 
654 	if (dt_dof_reset(dtp, pgp) != 0)
655 		return (NULL);
656 
657 	/*
658 	 * Iterate through the statement list computing the maximum number of
659 	 * actions and the maximum format string for allocating local buffers.
660 	 */
661 	for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
662 	    stp != NULL; stp = dt_list_next(stp), last = edp) {
663 
664 		dtrace_stmtdesc_t *sdp = stp->ds_desc;
665 		dtrace_actdesc_t *ap = sdp->dtsd_action;
666 
667 		if (sdp->dtsd_fmtdata != NULL) {
668 			i = dtrace_printf_format(dtp,
669 			    sdp->dtsd_fmtdata, NULL, 0);
670 			maxfmt = MAX(maxfmt, i);
671 		}
672 
673 		if ((edp = sdp->dtsd_ecbdesc) == last)
674 			continue; /* same ecb as previous statement */
675 
676 		for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next)
677 			i++;
678 
679 		maxacts = MAX(maxacts, i);
680 	}
681 
682 	dofa = alloca(sizeof (dof_actdesc_t) * maxacts);
683 	fmt = alloca(maxfmt + 1);
684 
685 	ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0);
686 	(void) dof_add_string(ddo, "");
687 
688 	/*
689 	 * If there are references to dynamic translators in the program, add
690 	 * an imported translator table entry for each referenced translator.
691 	 */
692 	if (pgp->dp_xrefslen != 0) {
693 		for (dxp = dt_list_next(&dtp->dt_xlators);
694 		    dxp != NULL; dxp = dt_list_next(dxp)) {
695 			if (dxp->dx_id < pgp->dp_xrefslen &&
696 			    pgp->dp_xrefs[dxp->dx_id] != NULL)
697 				dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT);
698 		}
699 	}
700 
701 	/*
702 	 * Now iterate through the statement list, creating the DOF section
703 	 * headers and data for each one and adding them to our buffers.
704 	 */
705 	for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
706 	    stp != NULL; stp = dt_list_next(stp), last = edp) {
707 
708 		dof_secidx_t probesec = DOF_SECIDX_NONE;
709 		dof_secidx_t prdsec = DOF_SECIDX_NONE;
710 		dof_secidx_t actsec = DOF_SECIDX_NONE;
711 
712 		const dt_stmt_t *next = stp;
713 		dtrace_stmtdesc_t *sdp = stp->ds_desc;
714 		dof_stridx_t strndx = 0;
715 		dof_probedesc_t dofp;
716 		dof_ecbdesc_t dofe;
717 		uint_t i;
718 
719 		if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)
720 			continue; /* same ecb as previous statement */
721 
722 		pdp = &edp->dted_probe;
723 
724 		/*
725 		 * Add a DOF_SECT_PROBEDESC for the ECB's probe description,
726 		 * and copy the probe description strings into the string table.
727 		 */
728 		dofp.dofp_strtab = ddo->ddo_strsec;
729 		dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider);
730 		dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod);
731 		dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func);
732 		dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name);
733 		dofp.dofp_id = pdp->dtpd_id;
734 
735 		probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC,
736 		    sizeof (dof_secidx_t), 0,
737 		    sizeof (dof_probedesc_t), sizeof (dof_probedesc_t));
738 
739 		/*
740 		 * If there is a predicate DIFO associated with the ecbdesc,
741 		 * write out the DIFO sections and save the DIFO section index.
742 		 */
743 		if (edp->dted_pred.dtpdd_difo != NULL)
744 			prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo);
745 
746 		/*
747 		 * Now iterate through the action list generating DIFOs as
748 		 * referenced therein and adding action descriptions to 'dofa'.
749 		 */
750 		for (i = 0, ap = edp->dted_action;
751 		    ap != NULL; ap = ap->dtad_next, i++) {
752 
753 			if (ap->dtad_difo != NULL) {
754 				dofa[i].dofa_difo =
755 				    dof_add_difo(ddo, ap->dtad_difo);
756 			} else
757 				dofa[i].dofa_difo = DOF_SECIDX_NONE;
758 
759 			/*
760 			 * If the first action in a statement has format data,
761 			 * add the format string to the global string table.
762 			 */
763 			if (sdp != NULL && ap == sdp->dtsd_action) {
764 				if (sdp->dtsd_fmtdata != NULL) {
765 					(void) dtrace_printf_format(dtp,
766 					    sdp->dtsd_fmtdata, fmt, maxfmt + 1);
767 					strndx = dof_add_string(ddo, fmt);
768 				} else
769 					strndx = 0; /* use dtad_arg instead */
770 
771 				if ((next = dt_list_next(next)) != NULL)
772 					sdp = next->ds_desc;
773 				else
774 					sdp = NULL;
775 			}
776 
777 			if (strndx != 0) {
778 				dofa[i].dofa_arg = strndx;
779 				dofa[i].dofa_strtab = ddo->ddo_strsec;
780 			} else {
781 				dofa[i].dofa_arg = ap->dtad_arg;
782 				dofa[i].dofa_strtab = DOF_SECIDX_NONE;
783 			}
784 
785 			dofa[i].dofa_kind = ap->dtad_kind;
786 			dofa[i].dofa_ntuple = ap->dtad_ntuple;
787 			dofa[i].dofa_uarg = ap->dtad_uarg;
788 		}
789 
790 		if (i > 0) {
791 			actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC,
792 			    sizeof (uint64_t), 0, sizeof (dof_actdesc_t),
793 			    sizeof (dof_actdesc_t) * i);
794 		}
795 
796 		/*
797 		 * Now finally, add the DOF_SECT_ECBDESC referencing all the
798 		 * previously created sub-sections.
799 		 */
800 		dofe.dofe_probes = probesec;
801 		dofe.dofe_pred = prdsec;
802 		dofe.dofe_actions = actsec;
803 		dofe.dofe_pad = 0;
804 		dofe.dofe_uarg = edp->dted_uarg;
805 
806 		(void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC,
807 		    sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t));
808 	}
809 
810 	/*
811 	 * If any providers are user-defined, output DOF sections corresponding
812 	 * to the providers and the probes and arguments that they define.
813 	 */
814 	if (flags & DTRACE_D_PROBES) {
815 		for (pvp = dt_list_next(&dtp->dt_provlist);
816 		    pvp != NULL; pvp = dt_list_next(pvp))
817 			dof_add_provider(ddo, pvp);
818 	}
819 
820 	/*
821 	 * If we're not stripping unloadable sections, generate compiler
822 	 * comments and any other unloadable miscellany.
823 	 */
824 	if (!(flags & DTRACE_D_STRIP)) {
825 		(void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS,
826 		    sizeof (char), 0, 0, strlen(_dtrace_version) + 1);
827 		(void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME,
828 		    sizeof (char), 0, 0, sizeof (struct utsname));
829 	}
830 
831 	/*
832 	 * Compute and fill in the appropriate values for the dof_hdr_t's
833 	 * dofh_secnum, dofh_loadsz, and dofh_filez values.
834 	 */
835 	h.dofh_secnum = ddo->ddo_nsecs;
836 	ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs);
837 	assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs);
838 
839 	h.dofh_loadsz = ssize +
840 	    dt_buf_len(&ddo->ddo_ldata) +
841 	    dt_buf_len(&ddo->ddo_strs);
842 
843 	if (dt_buf_len(&ddo->ddo_udata) != 0) {
844 		lsize = roundup(h.dofh_loadsz, sizeof (uint64_t));
845 		h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata);
846 	} else {
847 		lsize = h.dofh_loadsz;
848 		h.dofh_filesz = lsize;
849 	}
850 
851 	/*
852 	 * Set the global DOF_SECT_STRTAB's offset to be after the header,
853 	 * section headers, and other loadable data.  Since we're going to
854 	 * iterate over the buffer data directly, we must check for errors.
855 	 */
856 	if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) {
857 		(void) dt_set_errno(dtp, i);
858 		return (NULL);
859 	}
860 
861 	sp = dt_buf_ptr(&ddo->ddo_secs);
862 	assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB);
863 
864 	sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata);
865 	sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs);
866 
867 	/*
868 	 * Now relocate all the other section headers by adding the appropriate
869 	 * delta to their respective dofs_offset values.
870 	 */
871 	for (i = 0; i < ddo->ddo_nsecs; i++, sp++) {
872 		if (i == ddo->ddo_strsec)
873 			continue; /* already relocated above */
874 
875 		if (sp->dofs_flags & DOF_SECF_LOAD)
876 			sp->dofs_offset += ssize;
877 		else
878 			sp->dofs_offset += lsize;
879 	}
880 
881 	/*
882 	 * Finally, assemble the complete in-memory DOF buffer by writing the
883 	 * header and then concatenating all our buffers.  dt_buf_concat() will
884 	 * propagate any errors and cause dt_buf_claim() to return NULL.
885 	 */
886 	dt_buf_create(dtp, &dof, "dof", h.dofh_filesz);
887 
888 	dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t));
889 	dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t));
890 	dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t));
891 	dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char));
892 	dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t));
893 
894 	return (dt_buf_claim(dtp, &dof));
895 }
896 
897 void
898 dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof)
899 {
900 	dt_free(dtp, dof);
901 }
902 
903 void *
904 dtrace_getopt_dof(dtrace_hdl_t *dtp)
905 {
906 	dof_hdr_t *dof;
907 	dof_sec_t *sec;
908 	dof_optdesc_t *dofo;
909 	int i, nopts = 0, len = sizeof (dof_hdr_t) +
910 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t));
911 
912 	for (i = 0; i < DTRACEOPT_MAX; i++) {
913 		if (dtp->dt_options[i] != DTRACEOPT_UNSET)
914 			nopts++;
915 	}
916 
917 	len += sizeof (dof_optdesc_t) * nopts;
918 
919 	if ((dof = dt_zalloc(dtp, len)) == NULL ||
920 	    dof_hdr(dtp, DOF_VERSION, dof) != 0) {
921 		dt_free(dtp, dof);
922 		return (NULL);
923 	}
924 
925 	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
926 	dof->dofh_loadsz = len;
927 	dof->dofh_filesz = len;
928 
929 	/*
930 	 * Fill in the option section header...
931 	 */
932 	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
933 	sec->dofs_type = DOF_SECT_OPTDESC;
934 	sec->dofs_align = sizeof (uint64_t);
935 	sec->dofs_flags = DOF_SECF_LOAD;
936 	sec->dofs_entsize = sizeof (dof_optdesc_t);
937 
938 	dofo = (dof_optdesc_t *)((uintptr_t)sec +
939 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
940 
941 	sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;
942 	sec->dofs_size = sizeof (dof_optdesc_t) * nopts;
943 
944 	for (i = 0; i < DTRACEOPT_MAX; i++) {
945 		if (dtp->dt_options[i] == DTRACEOPT_UNSET)
946 			continue;
947 
948 		dofo->dofo_option = i;
949 		dofo->dofo_strtab = DOF_SECIDX_NONE;
950 		dofo->dofo_value = dtp->dt_options[i];
951 		dofo++;
952 	}
953 
954 	return (dof);
955 }
956 
957 void *
958 dtrace_geterr_dof(dtrace_hdl_t *dtp)
959 {
960 	if (dtp->dt_errprog != NULL)
961 		return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));
962 
963 	(void) dt_set_errno(dtp, EDT_BADERROR);
964 	return (NULL);
965 }
966