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 2005 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 <strings.h> 31 #include <stdlib.h> 32 #include <assert.h> 33 34 #include <dt_impl.h> 35 #include <dt_parser.h> 36 #include <dt_as.h> 37 38 void 39 dt_irlist_create(dt_irlist_t *dlp) 40 { 41 bzero(dlp, sizeof (dt_irlist_t)); 42 dlp->dl_label = 1; 43 } 44 45 void 46 dt_irlist_destroy(dt_irlist_t *dlp) 47 { 48 dt_irnode_t *dip, *nip; 49 50 for (dip = dlp->dl_list; dip != NULL; dip = nip) { 51 nip = dip->di_next; 52 free(dip); 53 } 54 } 55 56 void 57 dt_irlist_append(dt_irlist_t *dlp, dt_irnode_t *dip) 58 { 59 if (dlp->dl_last != NULL) 60 dlp->dl_last->di_next = dip; 61 else 62 dlp->dl_list = dip; 63 64 dlp->dl_last = dip; 65 66 if (dip->di_label == DT_LBL_NONE || dip->di_instr != DIF_INSTR_NOP) 67 dlp->dl_len++; /* don't count forward refs in instr count */ 68 } 69 70 uint_t 71 dt_irlist_label(dt_irlist_t *dlp) 72 { 73 return (dlp->dl_label++); 74 } 75 76 /*ARGSUSED*/ 77 static int 78 dt_countvar(dt_idhash_t *dhp, dt_ident_t *idp, void *data) 79 { 80 size_t *np = data; 81 82 if (idp->di_flags & (DT_IDFLG_DIFR | DT_IDFLG_DIFW)) 83 (*np)++; /* include variable in vartab */ 84 85 return (0); 86 } 87 88 /*ARGSUSED*/ 89 static int 90 dt_copyvar(dt_idhash_t *dhp, dt_ident_t *idp, void *data) 91 { 92 dt_pcb_t *pcb = data; 93 dtrace_difv_t *dvp; 94 ssize_t stroff; 95 dt_node_t dn; 96 97 if (!(idp->di_flags & (DT_IDFLG_DIFR | DT_IDFLG_DIFW))) 98 return (0); /* omit variable from vartab */ 99 100 dvp = &pcb->pcb_difo->dtdo_vartab[pcb->pcb_asvidx++]; 101 stroff = dt_strtab_insert(pcb->pcb_strtab, idp->di_name); 102 103 if (stroff == -1L) 104 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 105 if (stroff > DIF_STROFF_MAX) 106 longjmp(pcb->pcb_jmpbuf, EDT_STR2BIG); 107 108 dvp->dtdv_name = (uint_t)stroff; 109 dvp->dtdv_id = idp->di_id; 110 dvp->dtdv_flags = 0; 111 112 dvp->dtdv_kind = (idp->di_kind == DT_IDENT_ARRAY) ? 113 DIFV_KIND_ARRAY : DIFV_KIND_SCALAR; 114 115 if (idp->di_flags & DT_IDFLG_LOCAL) 116 dvp->dtdv_scope = DIFV_SCOPE_LOCAL; 117 else if (idp->di_flags & DT_IDFLG_TLS) 118 dvp->dtdv_scope = DIFV_SCOPE_THREAD; 119 else 120 dvp->dtdv_scope = DIFV_SCOPE_GLOBAL; 121 122 if (idp->di_flags & DT_IDFLG_DIFR) 123 dvp->dtdv_flags |= DIFV_F_REF; 124 if (idp->di_flags & DT_IDFLG_DIFW) 125 dvp->dtdv_flags |= DIFV_F_MOD; 126 127 bzero(&dn, sizeof (dn)); 128 dt_node_type_assign(&dn, idp->di_ctfp, idp->di_type); 129 dt_node_diftype(pcb->pcb_hdl, &dn, &dvp->dtdv_type); 130 131 idp->di_flags &= ~(DT_IDFLG_DIFR | DT_IDFLG_DIFW); 132 return (0); 133 } 134 135 static ssize_t 136 dt_copystr(const char *s, size_t n, size_t off, dt_pcb_t *pcb) 137 { 138 bcopy(s, pcb->pcb_difo->dtdo_strtab + off, n); 139 return (n); 140 } 141 142 /* 143 * Rewrite the xlate/xlarg instruction at dtdo_buf[i] so that the instruction's 144 * xltab index reflects the offset 'xi' of the assigned dtdo_xlmtab[] location. 145 * We track the cumulative references to translators and members in the pcb's 146 * pcb_asxrefs[] array, a two-dimensional array of bitmaps indexed by the 147 * global translator id and then by the corresponding translator member id. 148 */ 149 static void 150 dt_as_xlate(dt_pcb_t *pcb, dtrace_difo_t *dp, 151 uint_t i, uint_t xi, dt_node_t *dnp) 152 { 153 dtrace_hdl_t *dtp = pcb->pcb_hdl; 154 dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator; 155 156 assert(i < dp->dtdo_len); 157 assert(xi < dp->dtdo_xlmlen); 158 159 assert(dnp->dn_kind == DT_NODE_MEMBER); 160 assert(dnp->dn_membexpr->dn_kind == DT_NODE_XLATOR); 161 162 assert(dxp->dx_id < dtp->dt_xlatorid); 163 assert(dnp->dn_membid < dxp->dx_nmembers); 164 165 if (pcb->pcb_asxrefs == NULL) { 166 pcb->pcb_asxreflen = dtp->dt_xlatorid; 167 pcb->pcb_asxrefs = 168 dt_zalloc(dtp, sizeof (ulong_t *) * pcb->pcb_asxreflen); 169 if (pcb->pcb_asxrefs == NULL) 170 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 171 } 172 173 if (pcb->pcb_asxrefs[dxp->dx_id] == NULL) { 174 pcb->pcb_asxrefs[dxp->dx_id] = 175 dt_zalloc(dtp, BT_SIZEOFMAP(dxp->dx_nmembers)); 176 if (pcb->pcb_asxrefs[dxp->dx_id] == NULL) 177 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 178 } 179 180 dp->dtdo_buf[i] = DIF_INSTR_XLATE( 181 DIF_INSTR_OP(dp->dtdo_buf[i]), xi, DIF_INSTR_RD(dp->dtdo_buf[i])); 182 183 BT_SET(pcb->pcb_asxrefs[dxp->dx_id], dnp->dn_membid); 184 dp->dtdo_xlmtab[xi] = dnp; 185 } 186 187 static void 188 dt_as_undef(const dt_ident_t *idp, uint_t offset) 189 { 190 const char *kind, *mark = (idp->di_flags & DT_IDFLG_USER) ? "``" : "`"; 191 const dtrace_syminfo_t *dts = idp->di_data; 192 193 if (idp->di_flags & DT_IDFLG_USER) 194 kind = "user"; 195 else if (idp->di_flags & DT_IDFLG_PRIM) 196 kind = "primary kernel"; 197 else 198 kind = "loadable kernel"; 199 200 yylineno = idp->di_lineno; 201 202 xyerror(D_ASRELO, "relocation remains against %s symbol %s%s%s (offset " 203 "0x%x)\n", kind, dts->dts_object, mark, dts->dts_name, offset); 204 } 205 206 dtrace_difo_t * 207 dt_as(dt_pcb_t *pcb) 208 { 209 dtrace_hdl_t *dtp = pcb->pcb_hdl; 210 dt_irlist_t *dlp = &pcb->pcb_ir; 211 uint_t *labels = NULL; 212 dt_irnode_t *dip; 213 dtrace_difo_t *dp; 214 dt_ident_t *idp; 215 216 size_t n = 0; 217 uint_t i; 218 219 uint_t kmask, kbits, umask, ubits; 220 uint_t krel = 0, urel = 0, xlrefs = 0; 221 222 /* 223 * Select bitmasks based upon the desired symbol linking policy. We 224 * test (di_extern->di_flags & xmask) == xbits to determine if the 225 * symbol should have a relocation entry generated in the loop below. 226 * 227 * DT_LINK_KERNEL = kernel symbols static, user symbols dynamic 228 * DT_LINK_PRIMARY = primary kernel symbols static, others dynamic 229 * DT_LINK_DYNAMIC = all symbols dynamic 230 * DT_LINK_STATIC = all symbols static 231 * 232 * By 'static' we mean that we use the symbol's value at compile-time 233 * in the final DIF. By 'dynamic' we mean that we create a relocation 234 * table entry for the symbol's value so it can be relocated later. 235 */ 236 switch (dtp->dt_linkmode) { 237 case DT_LINK_KERNEL: 238 kmask = 0; 239 kbits = -1u; 240 umask = DT_IDFLG_USER; 241 ubits = DT_IDFLG_USER; 242 break; 243 case DT_LINK_PRIMARY: 244 kmask = DT_IDFLG_USER | DT_IDFLG_PRIM; 245 kbits = 0; 246 umask = DT_IDFLG_USER; 247 ubits = DT_IDFLG_USER; 248 break; 249 case DT_LINK_DYNAMIC: 250 kmask = DT_IDFLG_USER; 251 kbits = 0; 252 umask = DT_IDFLG_USER; 253 ubits = DT_IDFLG_USER; 254 break; 255 case DT_LINK_STATIC: 256 kmask = umask = 0; 257 kbits = ubits = -1u; 258 break; 259 default: 260 xyerror(D_UNKNOWN, "internal error -- invalid link mode %u\n", 261 dtp->dt_linkmode); 262 } 263 264 assert(pcb->pcb_difo == NULL); 265 pcb->pcb_difo = dt_zalloc(dtp, sizeof (dtrace_difo_t)); 266 267 if ((dp = pcb->pcb_difo) == NULL) 268 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 269 270 dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * dlp->dl_len); 271 272 if (dp->dtdo_buf == NULL) 273 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 274 275 if ((labels = dt_alloc(dtp, sizeof (uint_t) * dlp->dl_label)) == NULL) 276 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 277 278 /* 279 * Make an initial pass through the instruction list, filling in the 280 * instruction buffer with valid instructions and skipping labeled nops. 281 * While doing this, we also fill in our labels[] translation table 282 * and we count up the number of relocation table entries we will need. 283 */ 284 for (i = 0, dip = dlp->dl_list; dip != NULL; dip = dip->di_next) { 285 if (dip->di_label != DT_LBL_NONE) 286 labels[dip->di_label] = i; 287 288 if (dip->di_label == DT_LBL_NONE || 289 dip->di_instr != DIF_INSTR_NOP) 290 dp->dtdo_buf[i++] = dip->di_instr; 291 292 if (dip->di_extern == NULL) 293 continue; /* no external references needed */ 294 295 switch (DIF_INSTR_OP(dip->di_instr)) { 296 case DIF_OP_SETX: 297 idp = dip->di_extern; 298 if ((idp->di_flags & kmask) == kbits) 299 krel++; 300 else if ((idp->di_flags & umask) == ubits) 301 urel++; 302 break; 303 case DIF_OP_XLATE: 304 case DIF_OP_XLARG: 305 xlrefs++; 306 break; 307 default: 308 xyerror(D_UNKNOWN, "unexpected assembler relocation " 309 "for opcode 0x%x\n", DIF_INSTR_OP(dip->di_instr)); 310 } 311 } 312 313 assert(i == dlp->dl_len); 314 dp->dtdo_len = dlp->dl_len; 315 316 /* 317 * Make a second pass through the instructions, relocating each branch 318 * label to the index of the final instruction in the buffer and noting 319 * any other instruction-specific DIFO flags such as dtdo_destructive. 320 */ 321 for (i = 0; i < dp->dtdo_len; i++) { 322 dif_instr_t instr = dp->dtdo_buf[i]; 323 uint_t op = DIF_INSTR_OP(instr); 324 325 if (op == DIF_OP_CALL) { 326 if (DIF_INSTR_SUBR(instr) == DIF_SUBR_COPYOUT || 327 DIF_INSTR_SUBR(instr) == DIF_SUBR_COPYOUTSTR) 328 dp->dtdo_destructive = 1; 329 continue; 330 } 331 332 if (op >= DIF_OP_BA && op <= DIF_OP_BLEU) { 333 assert(DIF_INSTR_LABEL(instr) < dlp->dl_label); 334 dp->dtdo_buf[i] = DIF_INSTR_BRANCH(op, 335 labels[DIF_INSTR_LABEL(instr)]); 336 } 337 } 338 339 dt_free(dtp, labels); 340 pcb->pcb_asvidx = 0; 341 342 /* 343 * Allocate memory for the appropriate number of variable records and 344 * then fill in each variable record. As we populate the variable 345 * table we insert the corresponding variable names into the strtab. 346 */ 347 (void) dt_idhash_iter(dtp->dt_tls, dt_countvar, &n); 348 (void) dt_idhash_iter(dtp->dt_globals, dt_countvar, &n); 349 (void) dt_idhash_iter(pcb->pcb_locals, dt_countvar, &n); 350 351 if (n != 0) { 352 dp->dtdo_vartab = dt_alloc(dtp, n * sizeof (dtrace_difv_t)); 353 dp->dtdo_varlen = (uint32_t)n; 354 355 if (dp->dtdo_vartab == NULL) 356 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 357 358 (void) dt_idhash_iter(dtp->dt_tls, dt_copyvar, pcb); 359 (void) dt_idhash_iter(dtp->dt_globals, dt_copyvar, pcb); 360 (void) dt_idhash_iter(pcb->pcb_locals, dt_copyvar, pcb); 361 } 362 363 /* 364 * Allocate memory for the appropriate number of relocation table 365 * entries based upon our kernel and user counts from the first pass. 366 */ 367 if (krel != 0) { 368 dp->dtdo_kreltab = dt_alloc(dtp, 369 krel * sizeof (dof_relodesc_t)); 370 dp->dtdo_krelen = krel; 371 372 if (dp->dtdo_kreltab == NULL) 373 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 374 } 375 376 if (urel != 0) { 377 dp->dtdo_ureltab = dt_alloc(dtp, 378 urel * sizeof (dof_relodesc_t)); 379 dp->dtdo_urelen = urel; 380 381 if (dp->dtdo_ureltab == NULL) 382 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 383 } 384 385 if (xlrefs != 0) { 386 dp->dtdo_xlmtab = dt_zalloc(dtp, sizeof (dt_node_t *) * xlrefs); 387 dp->dtdo_xlmlen = xlrefs; 388 389 if (dp->dtdo_xlmtab == NULL) 390 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 391 } 392 393 /* 394 * If any relocations are needed, make another pass through the 395 * instruction list and fill in the relocation table entries. 396 */ 397 if (krel + urel + xlrefs != 0) { 398 uint_t knodef = pcb->pcb_cflags & DTRACE_C_KNODEF; 399 uint_t unodef = pcb->pcb_cflags & DTRACE_C_UNODEF; 400 401 dof_relodesc_t *krp = dp->dtdo_kreltab; 402 dof_relodesc_t *urp = dp->dtdo_ureltab; 403 dt_node_t **xlp = dp->dtdo_xlmtab; 404 405 i = 0; /* dtdo_buf[] index */ 406 407 for (dip = dlp->dl_list; dip != NULL; dip = dip->di_next) { 408 dof_relodesc_t *rp; 409 ssize_t soff; 410 uint_t nodef; 411 412 if (dip->di_label != DT_LBL_NONE && 413 dip->di_instr == DIF_INSTR_NOP) 414 continue; /* skip label declarations */ 415 416 i++; /* advance dtdo_buf[] index */ 417 418 if (DIF_INSTR_OP(dip->di_instr) == DIF_OP_XLATE || 419 DIF_INSTR_OP(dip->di_instr) == DIF_OP_XLARG) { 420 assert(dp->dtdo_buf[i - 1] == dip->di_instr); 421 dt_as_xlate(pcb, dp, i - 1, (uint_t) 422 (xlp++ - dp->dtdo_xlmtab), dip->di_extern); 423 continue; 424 } 425 426 if ((idp = dip->di_extern) == NULL) 427 continue; /* no relocation entry needed */ 428 429 if ((idp->di_flags & kmask) == kbits) { 430 nodef = knodef; 431 rp = krp++; 432 } else if ((idp->di_flags & umask) == ubits) { 433 nodef = unodef; 434 rp = urp++; 435 } else 436 continue; 437 438 if (!nodef) 439 dt_as_undef(idp, i); 440 441 assert(DIF_INSTR_OP(dip->di_instr) == DIF_OP_SETX); 442 soff = dt_strtab_insert(pcb->pcb_strtab, idp->di_name); 443 444 if (soff == -1L) 445 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 446 if (soff > DIF_STROFF_MAX) 447 longjmp(pcb->pcb_jmpbuf, EDT_STR2BIG); 448 449 rp->dofr_name = (dof_stridx_t)soff; 450 rp->dofr_type = DOF_RELO_SETX; 451 rp->dofr_offset = DIF_INSTR_INTEGER(dip->di_instr) * 452 sizeof (uint64_t); 453 rp->dofr_data = 0; 454 } 455 456 assert(krp == dp->dtdo_kreltab + dp->dtdo_krelen); 457 assert(urp == dp->dtdo_ureltab + dp->dtdo_urelen); 458 assert(xlp == dp->dtdo_xlmtab + dp->dtdo_xlmlen); 459 assert(i == dp->dtdo_len); 460 } 461 462 /* 463 * Allocate memory for the compiled string table and then copy the 464 * chunks from the string table into the final string buffer. 465 */ 466 if ((n = dt_strtab_size(pcb->pcb_strtab)) != 0) { 467 if ((dp->dtdo_strtab = dt_alloc(dtp, n)) == NULL) 468 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 469 470 (void) dt_strtab_write(pcb->pcb_strtab, 471 (dt_strtab_write_f *)dt_copystr, pcb); 472 dp->dtdo_strlen = (uint32_t)n; 473 } 474 475 /* 476 * Allocate memory for the compiled integer table and then copy the 477 * integer constants from the table into the final integer buffer. 478 */ 479 if ((n = dt_inttab_size(pcb->pcb_inttab)) != 0) { 480 if ((dp->dtdo_inttab = dt_alloc(dtp, 481 n * sizeof (uint64_t))) == NULL) 482 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM); 483 484 dt_inttab_write(pcb->pcb_inttab, dp->dtdo_inttab); 485 dp->dtdo_intlen = (uint32_t)n; 486 } 487 488 /* 489 * Fill in the DIFO return type from the type associated with the 490 * node saved in pcb_dret, and then clear pcb_difo and pcb_dret 491 * now that the assembler has completed successfully. 492 */ 493 dt_node_diftype(dtp, pcb->pcb_dret, &dp->dtdo_rtype); 494 pcb->pcb_difo = NULL; 495 pcb->pcb_dret = NULL; 496 497 if (pcb->pcb_cflags & DTRACE_C_DIFV) 498 dt_dis(dp, stderr); 499 500 return (dp); 501 } 502