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