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