xref: /titanic_52/usr/src/lib/libdtrace/common/dt_dof.c (revision 930176a00b697a09f5256ff3d4b1550f8e73c3a6)
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 		assert(pip->pi_noffs > 0);
444 
445 		dofpr.dofpr_offidx =
446 		    dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t);
447 		dofpr.dofpr_noffs = pip->pi_noffs;
448 		dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs,
449 		    pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
450 
451 		dofpr.dofpr_enoffidx =
452 		    dt_buf_len(&ddo->ddo_enoffs) / sizeof (uint32_t);
453 		dofpr.dofpr_nenoffs = pip->pi_nenoffs;
454 		dt_buf_write(dtp, &ddo->ddo_enoffs, pip->pi_enoffs,
455 		    pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
456 
457 		/*
458 		 * If pi_rname isn't set, the relocation will be against the
459 		 * function name. If it is, the relocation will be against
460 		 * pi_rname. This will be used if the function is scoped
461 		 * locally so an alternate symbol is added for the purpose
462 		 * of this relocation.
463 		 */
464 		if (pip->pi_rname[0] == '\0')
465 			dofr.dofr_name = dofpr.dofpr_func;
466 		else
467 			dofr.dofr_name = dof_add_string(ddo, pip->pi_rname);
468 		dofr.dofr_type = DOF_RELO_SETX;
469 		dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes);
470 		dofr.dofr_data = 0;
471 
472 		dt_buf_write(dtp, &ddo->ddo_rels, &dofr,
473 		    sizeof (dofr), sizeof (uint64_t));
474 
475 		dt_buf_write(dtp, &ddo->ddo_probes, &dofpr,
476 		    sizeof (dofpr), sizeof (uint64_t));
477 	}
478 
479 	return (0);
480 }
481 
482 static void
483 dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp)
484 {
485 	dtrace_hdl_t *dtp = ddo->ddo_hdl;
486 	dof_provider_t dofpv;
487 	dof_relohdr_t dofr;
488 	dof_secidx_t *dofs;
489 	ulong_t xr, nxr;
490 	id_t i;
491 
492 	if (pvp->pv_flags & DT_PROVIDER_IMPL)
493 		return; /* ignore providers that are exported by dtrace(7D) */
494 
495 	nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax);
496 	dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1));
497 	xr = 1; /* reserve dofs[0] for the provider itself */
498 
499 	/*
500 	 * For each translator referenced by the provider (pv_xrefs), emit an
501 	 * exported translator section for it if one hasn't been created yet.
502 	 */
503 	for (i = 0; i < pvp->pv_xrmax; i++) {
504 		if (BT_TEST(pvp->pv_xrefs, i) &&
505 		    dtp->dt_xlatemode == DT_XL_DYNAMIC) {
506 			dof_add_translator(ddo,
507 			    dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT);
508 			dofs[xr++] = ddo->ddo_xlexport[i];
509 		}
510 	}
511 
512 	dt_buf_reset(dtp, &ddo->ddo_probes);
513 	dt_buf_reset(dtp, &ddo->ddo_args);
514 	dt_buf_reset(dtp, &ddo->ddo_offs);
515 	dt_buf_reset(dtp, &ddo->ddo_enoffs);
516 	dt_buf_reset(dtp, &ddo->ddo_rels);
517 
518 	(void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo);
519 
520 	dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES,
521 	    sizeof (uint64_t), 0, sizeof (dof_probe_t),
522 	    dt_buf_len(&ddo->ddo_probes));
523 
524 	dt_buf_concat(dtp, &ddo->ddo_ldata,
525 	    &ddo->ddo_probes, sizeof (uint64_t));
526 
527 	dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS,
528 	    sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args));
529 
530 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t));
531 
532 	assert(dt_buf_len(&ddo->ddo_offs) > 0);
533 
534 	dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS,
535 	    sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs));
536 
537 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t));
538 
539 	dofpv.dofpv_prenoffs = dof_add_lsect(ddo, NULL, DOF_SECT_PRENOFFS,
540 	    sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_enoffs));
541 
542 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_enoffs, sizeof (uint_t));
543 
544 	dofpv.dofpv_strtab = ddo->ddo_strsec;
545 	dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name);
546 
547 	dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider);
548 	dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod);
549 	dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func);
550 	dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name);
551 	dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args);
552 
553 	dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER,
554 	    sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t));
555 
556 	dofr.dofr_strtab = dofpv.dofpv_strtab;
557 	dofr.dofr_tgtsec = dofpv.dofpv_probes;
558 	dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB,
559 	    sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
560 	    dt_buf_len(&ddo->ddo_rels));
561 
562 	dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t));
563 
564 	(void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
565 	    sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
566 
567 	if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) {
568 		(void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT,
569 		    sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t),
570 		    sizeof (dof_secidx_t) * (nxr + 1));
571 	}
572 }
573 
574 static int
575 dof_hdr(dtrace_hdl_t *dtp, uint8_t dofversion, dof_hdr_t *hp)
576 {
577 	/*
578 	 * If our config values cannot fit in a uint8_t, we can't generate a
579 	 * DOF header since the values won't fit.  This can only happen if the
580 	 * user forcibly compiles a program with an artificial configuration.
581 	 */
582 	if (dtp->dt_conf.dtc_difversion > UINT8_MAX ||
583 	    dtp->dt_conf.dtc_difintregs > UINT8_MAX ||
584 	    dtp->dt_conf.dtc_diftupregs > UINT8_MAX)
585 		return (dt_set_errno(dtp, EOVERFLOW));
586 
587 	bzero(hp, sizeof (dof_hdr_t));
588 
589 	hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
590 	hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
591 	hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
592 	hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
593 
594 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
595 		hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64;
596 	else
597 		hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32;
598 
599 	hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
600 	hp->dofh_ident[DOF_ID_VERSION] = dofversion;
601 	hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion;
602 	hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs;
603 	hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs;
604 
605 	hp->dofh_hdrsize = sizeof (dof_hdr_t);
606 	hp->dofh_secsize = sizeof (dof_sec_t);
607 	hp->dofh_secoff = sizeof (dof_hdr_t);
608 
609 	return (0);
610 }
611 
612 void *
613 dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags)
614 {
615 	dt_dof_t *ddo = &dtp->dt_dof;
616 
617 	const dtrace_ecbdesc_t *edp, *last;
618 	const dtrace_probedesc_t *pdp;
619 	const dtrace_actdesc_t *ap;
620 	const dt_stmt_t *stp;
621 
622 	uint_t maxacts = 0;
623 	uint_t maxfmt = 0;
624 
625 	dt_provider_t *pvp;
626 	dt_xlator_t *dxp;
627 	dof_actdesc_t *dofa;
628 	dof_sec_t *sp;
629 	size_t ssize, lsize;
630 	dof_hdr_t h;
631 
632 	dt_buf_t dof;
633 	char *fmt;
634 	uint_t i;
635 
636 	if (flags & ~DTRACE_D_MASK) {
637 		(void) dt_set_errno(dtp, EINVAL);
638 		return (NULL);
639 	}
640 
641 	flags |= dtp->dt_dflags;
642 
643 	if (dof_hdr(dtp, pgp->dp_dofversion, &h) != 0)
644 		return (NULL);
645 
646 	if (dt_dof_reset(dtp, pgp) != 0)
647 		return (NULL);
648 
649 	/*
650 	 * Iterate through the statement list computing the maximum number of
651 	 * actions and the maximum format string for allocating local buffers.
652 	 */
653 	for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
654 	    stp != NULL; stp = dt_list_next(stp), last = edp) {
655 
656 		dtrace_stmtdesc_t *sdp = stp->ds_desc;
657 		dtrace_actdesc_t *ap = sdp->dtsd_action;
658 
659 		if (sdp->dtsd_fmtdata != NULL) {
660 			i = dtrace_printf_format(dtp,
661 			    sdp->dtsd_fmtdata, NULL, 0);
662 			maxfmt = MAX(maxfmt, i);
663 		}
664 
665 		if ((edp = sdp->dtsd_ecbdesc) == last)
666 			continue; /* same ecb as previous statement */
667 
668 		for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next)
669 			i++;
670 
671 		maxacts = MAX(maxacts, i);
672 	}
673 
674 	dofa = alloca(sizeof (dof_actdesc_t) * maxacts);
675 	fmt = alloca(maxfmt + 1);
676 
677 	ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0);
678 	(void) dof_add_string(ddo, "");
679 
680 	/*
681 	 * If there are references to dynamic translators in the program, add
682 	 * an imported translator table entry for each referenced translator.
683 	 */
684 	if (pgp->dp_xrefslen != 0) {
685 		for (dxp = dt_list_next(&dtp->dt_xlators);
686 		    dxp != NULL; dxp = dt_list_next(dxp)) {
687 			if (dxp->dx_id < pgp->dp_xrefslen &&
688 			    pgp->dp_xrefs[dxp->dx_id] != NULL)
689 				dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT);
690 		}
691 	}
692 
693 	/*
694 	 * Now iterate through the statement list, creating the DOF section
695 	 * headers and data for each one and adding them to our buffers.
696 	 */
697 	for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
698 	    stp != NULL; stp = dt_list_next(stp), last = edp) {
699 
700 		dof_secidx_t probesec = DOF_SECIDX_NONE;
701 		dof_secidx_t prdsec = DOF_SECIDX_NONE;
702 		dof_secidx_t actsec = DOF_SECIDX_NONE;
703 
704 		const dt_stmt_t *next = stp;
705 		dtrace_stmtdesc_t *sdp = stp->ds_desc;
706 		dof_stridx_t strndx = 0;
707 		dof_probedesc_t dofp;
708 		dof_ecbdesc_t dofe;
709 		uint_t i;
710 
711 		if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)
712 			continue; /* same ecb as previous statement */
713 
714 		pdp = &edp->dted_probe;
715 
716 		/*
717 		 * Add a DOF_SECT_PROBEDESC for the ECB's probe description,
718 		 * and copy the probe description strings into the string table.
719 		 */
720 		dofp.dofp_strtab = ddo->ddo_strsec;
721 		dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider);
722 		dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod);
723 		dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func);
724 		dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name);
725 		dofp.dofp_id = pdp->dtpd_id;
726 
727 		probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC,
728 		    sizeof (dof_secidx_t), 0,
729 		    sizeof (dof_probedesc_t), sizeof (dof_probedesc_t));
730 
731 		/*
732 		 * If there is a predicate DIFO associated with the ecbdesc,
733 		 * write out the DIFO sections and save the DIFO section index.
734 		 */
735 		if (edp->dted_pred.dtpdd_difo != NULL)
736 			prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo);
737 
738 		/*
739 		 * Now iterate through the action list generating DIFOs as
740 		 * referenced therein and adding action descriptions to 'dofa'.
741 		 */
742 		for (i = 0, ap = edp->dted_action;
743 		    ap != NULL; ap = ap->dtad_next, i++) {
744 
745 			if (ap->dtad_difo != NULL) {
746 				dofa[i].dofa_difo =
747 				    dof_add_difo(ddo, ap->dtad_difo);
748 			} else
749 				dofa[i].dofa_difo = DOF_SECIDX_NONE;
750 
751 			/*
752 			 * If the first action in a statement has format data,
753 			 * add the format string to the global string table.
754 			 */
755 			if (sdp != NULL && ap == sdp->dtsd_action) {
756 				if (sdp->dtsd_fmtdata != NULL) {
757 					(void) dtrace_printf_format(dtp,
758 					    sdp->dtsd_fmtdata, fmt, maxfmt + 1);
759 					strndx = dof_add_string(ddo, fmt);
760 				} else
761 					strndx = 0; /* use dtad_arg instead */
762 
763 				if ((next = dt_list_next(next)) != NULL)
764 					sdp = next->ds_desc;
765 				else
766 					sdp = NULL;
767 			}
768 
769 			if (strndx != 0) {
770 				dofa[i].dofa_arg = strndx;
771 				dofa[i].dofa_strtab = ddo->ddo_strsec;
772 			} else {
773 				dofa[i].dofa_arg = ap->dtad_arg;
774 				dofa[i].dofa_strtab = DOF_SECIDX_NONE;
775 			}
776 
777 			dofa[i].dofa_kind = ap->dtad_kind;
778 			dofa[i].dofa_ntuple = ap->dtad_ntuple;
779 			dofa[i].dofa_uarg = ap->dtad_uarg;
780 		}
781 
782 		if (i > 0) {
783 			actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC,
784 			    sizeof (uint64_t), 0, sizeof (dof_actdesc_t),
785 			    sizeof (dof_actdesc_t) * i);
786 		}
787 
788 		/*
789 		 * Now finally, add the DOF_SECT_ECBDESC referencing all the
790 		 * previously created sub-sections.
791 		 */
792 		dofe.dofe_probes = probesec;
793 		dofe.dofe_pred = prdsec;
794 		dofe.dofe_actions = actsec;
795 		dofe.dofe_pad = 0;
796 		dofe.dofe_uarg = edp->dted_uarg;
797 
798 		(void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC,
799 		    sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t));
800 	}
801 
802 	/*
803 	 * If any providers are user-defined, output DOF sections corresponding
804 	 * to the providers and the probes and arguments that they define.
805 	 */
806 	if (flags & DTRACE_D_PROBES) {
807 		for (pvp = dt_list_next(&dtp->dt_provlist);
808 		    pvp != NULL; pvp = dt_list_next(pvp))
809 			dof_add_provider(ddo, pvp);
810 	}
811 
812 	/*
813 	 * If we're not stripping unloadable sections, generate compiler
814 	 * comments and any other unloadable miscellany.
815 	 */
816 	if (!(flags & DTRACE_D_STRIP)) {
817 		(void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS,
818 		    sizeof (char), 0, 0, strlen(_dtrace_version) + 1);
819 		(void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME,
820 		    sizeof (char), 0, 0, sizeof (struct utsname));
821 	}
822 
823 	/*
824 	 * Compute and fill in the appropriate values for the dof_hdr_t's
825 	 * dofh_secnum, dofh_loadsz, and dofh_filez values.
826 	 */
827 	h.dofh_secnum = ddo->ddo_nsecs;
828 	ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs);
829 	assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs);
830 
831 	h.dofh_loadsz = ssize +
832 	    dt_buf_len(&ddo->ddo_ldata) +
833 	    dt_buf_len(&ddo->ddo_strs);
834 
835 	if (dt_buf_len(&ddo->ddo_udata) != 0) {
836 		lsize = roundup(h.dofh_loadsz, sizeof (uint64_t));
837 		h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata);
838 	} else {
839 		lsize = h.dofh_loadsz;
840 		h.dofh_filesz = lsize;
841 	}
842 
843 	/*
844 	 * Set the global DOF_SECT_STRTAB's offset to be after the header,
845 	 * section headers, and other loadable data.  Since we're going to
846 	 * iterate over the buffer data directly, we must check for errors.
847 	 */
848 	if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) {
849 		(void) dt_set_errno(dtp, i);
850 		return (NULL);
851 	}
852 
853 	sp = dt_buf_ptr(&ddo->ddo_secs);
854 	assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB);
855 
856 	sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata);
857 	sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs);
858 
859 	/*
860 	 * Now relocate all the other section headers by adding the appropriate
861 	 * delta to their respective dofs_offset values.
862 	 */
863 	for (i = 0; i < ddo->ddo_nsecs; i++, sp++) {
864 		if (i == ddo->ddo_strsec)
865 			continue; /* already relocated above */
866 
867 		if (sp->dofs_flags & DOF_SECF_LOAD)
868 			sp->dofs_offset += ssize;
869 		else
870 			sp->dofs_offset += lsize;
871 	}
872 
873 	/*
874 	 * Finally, assemble the complete in-memory DOF buffer by writing the
875 	 * header and then concatenating all our buffers.  dt_buf_concat() will
876 	 * propagate any errors and cause dt_buf_claim() to return NULL.
877 	 */
878 	dt_buf_create(dtp, &dof, "dof", h.dofh_filesz);
879 
880 	dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t));
881 	dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t));
882 	dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t));
883 	dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char));
884 	dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t));
885 
886 	return (dt_buf_claim(dtp, &dof));
887 }
888 
889 void
890 dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof)
891 {
892 	dt_free(dtp, dof);
893 }
894 
895 void *
896 dtrace_getopt_dof(dtrace_hdl_t *dtp)
897 {
898 	dof_hdr_t *dof;
899 	dof_sec_t *sec;
900 	dof_optdesc_t *dofo;
901 	int i, nopts = 0, len = sizeof (dof_hdr_t) +
902 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t));
903 
904 	for (i = 0; i < DTRACEOPT_MAX; i++) {
905 		if (dtp->dt_options[i] != DTRACEOPT_UNSET)
906 			nopts++;
907 	}
908 
909 	len += sizeof (dof_optdesc_t) * nopts;
910 
911 	if ((dof = dt_zalloc(dtp, len)) == NULL ||
912 	    dof_hdr(dtp, DOF_VERSION, dof) != 0) {
913 		dt_free(dtp, dof);
914 		return (NULL);
915 	}
916 
917 	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
918 	dof->dofh_loadsz = len;
919 	dof->dofh_filesz = len;
920 
921 	/*
922 	 * Fill in the option section header...
923 	 */
924 	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
925 	sec->dofs_type = DOF_SECT_OPTDESC;
926 	sec->dofs_align = sizeof (uint64_t);
927 	sec->dofs_flags = DOF_SECF_LOAD;
928 	sec->dofs_entsize = sizeof (dof_optdesc_t);
929 
930 	dofo = (dof_optdesc_t *)((uintptr_t)sec +
931 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
932 
933 	sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;
934 	sec->dofs_size = sizeof (dof_optdesc_t) * nopts;
935 
936 	for (i = 0; i < DTRACEOPT_MAX; i++) {
937 		if (dtp->dt_options[i] == DTRACEOPT_UNSET)
938 			continue;
939 
940 		dofo->dofo_option = i;
941 		dofo->dofo_strtab = DOF_SECIDX_NONE;
942 		dofo->dofo_value = dtp->dt_options[i];
943 		dofo++;
944 	}
945 
946 	return (dof);
947 }
948 
949 void *
950 dtrace_geterr_dof(dtrace_hdl_t *dtp)
951 {
952 	if (dtp->dt_errprog != NULL)
953 		return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));
954 
955 	(void) dt_set_errno(dtp, EDT_BADERROR);
956 	return (NULL);
957 }
958