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