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
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.
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]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
35 #include <dt_parser.h>
39 dt_irlist_create(dt_irlist_t *dlp)
41 bzero(dlp, sizeof (dt_irlist_t));
46 dt_irlist_destroy(dt_irlist_t *dlp)
48 dt_irnode_t *dip, *nip;
50 for (dip = dlp->dl_list; dip != NULL; dip = nip) {
57 dt_irlist_append(dt_irlist_t *dlp, dt_irnode_t *dip)
59 if (dlp->dl_last != NULL)
60 dlp->dl_last->di_next = dip;
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 */
71 dt_irlist_label(dt_irlist_t *dlp)
73 return (dlp->dl_label++);
78 dt_countvar(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
82 if (idp->di_flags & (DT_IDFLG_DIFR | DT_IDFLG_DIFW))
83 (*np)++; /* include variable in vartab */
90 dt_copyvar(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
97 if (!(idp->di_flags & (DT_IDFLG_DIFR | DT_IDFLG_DIFW)))
98 return (0); /* omit variable from vartab */
100 dvp = &pcb->pcb_difo->dtdo_vartab[pcb->pcb_asvidx++];
101 stroff = dt_strtab_insert(pcb->pcb_strtab, idp->di_name);
104 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
105 if (stroff > DIF_STROFF_MAX)
106 longjmp(pcb->pcb_jmpbuf, EDT_STR2BIG);
108 dvp->dtdv_name = (uint_t)stroff;
109 dvp->dtdv_id = idp->di_id;
112 dvp->dtdv_kind = (idp->di_kind == DT_IDENT_ARRAY) ?
113 DIFV_KIND_ARRAY : DIFV_KIND_SCALAR;
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;
120 dvp->dtdv_scope = DIFV_SCOPE_GLOBAL;
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;
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);
131 idp->di_flags &= ~(DT_IDFLG_DIFR | DT_IDFLG_DIFW);
136 dt_copystr(const char *s, size_t n, size_t off, dt_pcb_t *pcb)
138 bcopy(s, pcb->pcb_difo->dtdo_strtab + off, n);
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.
150 dt_as_xlate(dt_pcb_t *pcb, dtrace_difo_t *dp,
151 uint_t i, uint_t xi, dt_node_t *dnp)
153 dtrace_hdl_t *dtp = pcb->pcb_hdl;
154 dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
156 assert(i < dp->dtdo_len);
157 assert(xi < dp->dtdo_xlmlen);
159 assert(dnp->dn_kind == DT_NODE_MEMBER);
160 assert(dnp->dn_membexpr->dn_kind == DT_NODE_XLATOR);
162 assert(dxp->dx_id < dtp->dt_xlatorid);
163 assert(dnp->dn_membid < dxp->dx_nmembers);
165 if (pcb->pcb_asxrefs == NULL) {
166 pcb->pcb_asxreflen = dtp->dt_xlatorid;
168 dt_zalloc(dtp, sizeof (ulong_t *) * pcb->pcb_asxreflen);
169 if (pcb->pcb_asxrefs == NULL)
170 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
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);
180 dp->dtdo_buf[i] = DIF_INSTR_XLATE(
181 DIF_INSTR_OP(dp->dtdo_buf[i]), xi, DIF_INSTR_RD(dp->dtdo_buf[i]));
183 BT_SET(pcb->pcb_asxrefs[dxp->dx_id], dnp->dn_membid);
184 dp->dtdo_xlmtab[xi] = dnp;
188 dt_as_undef(const dt_ident_t *idp, uint_t offset)
190 const char *kind, *mark = (idp->di_flags & DT_IDFLG_USER) ? "``" : "`";
191 const dtrace_syminfo_t *dts = idp->di_data;
193 if (idp->di_flags & DT_IDFLG_USER)
195 else if (idp->di_flags & DT_IDFLG_PRIM)
196 kind = "primary kernel";
198 kind = "loadable kernel";
200 yylineno = idp->di_lineno;
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);
209 dtrace_hdl_t *dtp = pcb->pcb_hdl;
210 dt_irlist_t *dlp = &pcb->pcb_ir;
211 uint_t *labels = NULL;
219 uint_t kmask, kbits, umask, ubits;
220 uint_t krel = 0, urel = 0, xlrefs = 0;
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.
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
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.
236 switch (dtp->dt_linkmode) {
240 umask = DT_IDFLG_USER;
241 ubits = DT_IDFLG_USER;
243 case DT_LINK_PRIMARY:
244 kmask = DT_IDFLG_USER | DT_IDFLG_PRIM;
246 umask = DT_IDFLG_USER;
247 ubits = DT_IDFLG_USER;
249 case DT_LINK_DYNAMIC:
250 kmask = DT_IDFLG_USER;
252 umask = DT_IDFLG_USER;
253 ubits = DT_IDFLG_USER;
260 xyerror(D_UNKNOWN, "internal error -- invalid link mode %u\n",
264 assert(pcb->pcb_difo == NULL);
265 pcb->pcb_difo = dt_zalloc(dtp, sizeof (dtrace_difo_t));
267 if ((dp = pcb->pcb_difo) == NULL)
268 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
270 dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * dlp->dl_len);
272 if (dp->dtdo_buf == NULL)
273 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
275 if ((labels = dt_alloc(dtp, sizeof (uint_t) * dlp->dl_label)) == NULL)
276 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
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.
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;
288 if (dip->di_label == DT_LBL_NONE ||
289 dip->di_instr != DIF_INSTR_NOP)
290 dp->dtdo_buf[i++] = dip->di_instr;
292 if (dip->di_extern == NULL)
293 continue; /* no external references needed */
295 switch (DIF_INSTR_OP(dip->di_instr)) {
297 idp = dip->di_extern;
298 if ((idp->di_flags & kmask) == kbits)
300 else if ((idp->di_flags & umask) == ubits)
308 xyerror(D_UNKNOWN, "unexpected assembler relocation "
309 "for opcode 0x%x\n", DIF_INSTR_OP(dip->di_instr));
313 assert(i == dlp->dl_len);
314 dp->dtdo_len = dlp->dl_len;
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.
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);
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;
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)]);
339 dt_free(dtp, labels);
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.
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);
352 dp->dtdo_vartab = dt_alloc(dtp, n * sizeof (dtrace_difv_t));
353 dp->dtdo_varlen = (uint32_t)n;
355 if (dp->dtdo_vartab == NULL)
356 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
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);
364 * Allocate memory for the appropriate number of relocation table
365 * entries based upon our kernel and user counts from the first pass.
368 dp->dtdo_kreltab = dt_alloc(dtp,
369 krel * sizeof (dof_relodesc_t));
370 dp->dtdo_krelen = krel;
372 if (dp->dtdo_kreltab == NULL)
373 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
377 dp->dtdo_ureltab = dt_alloc(dtp,
378 urel * sizeof (dof_relodesc_t));
379 dp->dtdo_urelen = urel;
381 if (dp->dtdo_ureltab == NULL)
382 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
386 dp->dtdo_xlmtab = dt_zalloc(dtp, sizeof (dt_node_t *) * xlrefs);
387 dp->dtdo_xlmlen = xlrefs;
389 if (dp->dtdo_xlmtab == NULL)
390 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
394 * If any relocations are needed, make another pass through the
395 * instruction list and fill in the relocation table entries.
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;
401 dof_relodesc_t *krp = dp->dtdo_kreltab;
402 dof_relodesc_t *urp = dp->dtdo_ureltab;
403 dt_node_t **xlp = dp->dtdo_xlmtab;
405 i = 0; /* dtdo_buf[] index */
407 for (dip = dlp->dl_list; dip != NULL; dip = dip->di_next) {
412 if (dip->di_label != DT_LBL_NONE &&
413 dip->di_instr == DIF_INSTR_NOP)
414 continue; /* skip label declarations */
416 i++; /* advance dtdo_buf[] index */
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);
426 if ((idp = dip->di_extern) == NULL)
427 continue; /* no relocation entry needed */
429 if ((idp->di_flags & kmask) == kbits) {
432 } else if ((idp->di_flags & umask) == ubits) {
441 assert(DIF_INSTR_OP(dip->di_instr) == DIF_OP_SETX);
442 soff = dt_strtab_insert(pcb->pcb_strtab, idp->di_name);
445 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
446 if (soff > DIF_STROFF_MAX)
447 longjmp(pcb->pcb_jmpbuf, EDT_STR2BIG);
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) *
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);
463 * Allocate memory for the compiled string table and then copy the
464 * chunks from the string table into the final string buffer.
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);
470 (void) dt_strtab_write(pcb->pcb_strtab,
471 (dt_strtab_write_f *)dt_copystr, pcb);
472 dp->dtdo_strlen = (uint32_t)n;
476 * Allocate memory for the compiled integer table and then copy the
477 * integer constants from the table into the final integer buffer.
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);
484 dt_inttab_write(pcb->pcb_inttab, dp->dtdo_inttab);
485 dp->dtdo_intlen = (uint32_t)n;
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.
493 dt_node_diftype(dtp, pcb->pcb_dret, &dp->dtdo_rtype);
494 pcb->pcb_difo = NULL;
495 pcb->pcb_dret = NULL;
497 if (pcb->pcb_cflags & DTRACE_C_DIFV)