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