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