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