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