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