16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
66ff6d951SJohn Birrell  * (the "License").  You may not use this file except in compliance
76ff6d951SJohn Birrell  * with the License.
86ff6d951SJohn Birrell  *
96ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
106ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
116ff6d951SJohn Birrell  * See the License for the specific language governing permissions
126ff6d951SJohn Birrell  * and limitations under the License.
136ff6d951SJohn Birrell  *
146ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
156ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
166ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
176ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
186ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
196ff6d951SJohn Birrell  *
206ff6d951SJohn Birrell  * CDDL HEADER END
216ff6d951SJohn Birrell  */
226ff6d951SJohn Birrell /*
236ff6d951SJohn Birrell  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
246ff6d951SJohn Birrell  * Use is subject to license terms.
256ff6d951SJohn Birrell  */
26*8e648814SRui Paulo /*
27*8e648814SRui Paulo  * Copyright (c) 2013 by Delphix. All rights reserved.
28*8e648814SRui Paulo  * Copyright (c) 2013 Joyent, Inc. All rights reserved.
29*8e648814SRui Paulo  */
306ff6d951SJohn Birrell 
316ff6d951SJohn Birrell #include <sys/types.h>
326ff6d951SJohn Birrell #include <strings.h>
336ff6d951SJohn Birrell #include <stdlib.h>
346ff6d951SJohn Birrell #include <assert.h>
356ff6d951SJohn Birrell 
366ff6d951SJohn Birrell #include <dt_impl.h>
376ff6d951SJohn Birrell #include <dt_parser.h>
386ff6d951SJohn Birrell #include <dt_as.h>
396ff6d951SJohn Birrell 
406ff6d951SJohn Birrell void
dt_irlist_create(dt_irlist_t * dlp)416ff6d951SJohn Birrell dt_irlist_create(dt_irlist_t *dlp)
426ff6d951SJohn Birrell {
436ff6d951SJohn Birrell 	bzero(dlp, sizeof (dt_irlist_t));
446ff6d951SJohn Birrell 	dlp->dl_label = 1;
456ff6d951SJohn Birrell }
466ff6d951SJohn Birrell 
476ff6d951SJohn Birrell void
dt_irlist_destroy(dt_irlist_t * dlp)486ff6d951SJohn Birrell dt_irlist_destroy(dt_irlist_t *dlp)
496ff6d951SJohn Birrell {
506ff6d951SJohn Birrell 	dt_irnode_t *dip, *nip;
516ff6d951SJohn Birrell 
526ff6d951SJohn Birrell 	for (dip = dlp->dl_list; dip != NULL; dip = nip) {
536ff6d951SJohn Birrell 		nip = dip->di_next;
546ff6d951SJohn Birrell 		free(dip);
556ff6d951SJohn Birrell 	}
566ff6d951SJohn Birrell }
576ff6d951SJohn Birrell 
586ff6d951SJohn Birrell void
dt_irlist_append(dt_irlist_t * dlp,dt_irnode_t * dip)596ff6d951SJohn Birrell dt_irlist_append(dt_irlist_t *dlp, dt_irnode_t *dip)
606ff6d951SJohn Birrell {
616ff6d951SJohn Birrell 	if (dlp->dl_last != NULL)
626ff6d951SJohn Birrell 		dlp->dl_last->di_next = dip;
636ff6d951SJohn Birrell 	else
646ff6d951SJohn Birrell 		dlp->dl_list = dip;
656ff6d951SJohn Birrell 
666ff6d951SJohn Birrell 	dlp->dl_last = dip;
676ff6d951SJohn Birrell 
686ff6d951SJohn Birrell 	if (dip->di_label == DT_LBL_NONE || dip->di_instr != DIF_INSTR_NOP)
696ff6d951SJohn Birrell 		dlp->dl_len++; /* don't count forward refs in instr count */
706ff6d951SJohn Birrell }
716ff6d951SJohn Birrell 
726ff6d951SJohn Birrell uint_t
dt_irlist_label(dt_irlist_t * dlp)736ff6d951SJohn Birrell dt_irlist_label(dt_irlist_t *dlp)
746ff6d951SJohn Birrell {
756ff6d951SJohn Birrell 	return (dlp->dl_label++);
766ff6d951SJohn Birrell }
776ff6d951SJohn Birrell 
786ff6d951SJohn Birrell /*ARGSUSED*/
796ff6d951SJohn Birrell static int
dt_countvar(dt_idhash_t * dhp,dt_ident_t * idp,void * data)806ff6d951SJohn Birrell dt_countvar(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
816ff6d951SJohn Birrell {
826ff6d951SJohn Birrell 	size_t *np = data;
836ff6d951SJohn Birrell 
846ff6d951SJohn Birrell 	if (idp->di_flags & (DT_IDFLG_DIFR | DT_IDFLG_DIFW))
856ff6d951SJohn Birrell 		(*np)++; /* include variable in vartab */
866ff6d951SJohn Birrell 
876ff6d951SJohn Birrell 	return (0);
886ff6d951SJohn Birrell }
896ff6d951SJohn Birrell 
906ff6d951SJohn Birrell /*ARGSUSED*/
916ff6d951SJohn Birrell static int
dt_copyvar(dt_idhash_t * dhp,dt_ident_t * idp,void * data)926ff6d951SJohn Birrell dt_copyvar(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
936ff6d951SJohn Birrell {
946ff6d951SJohn Birrell 	dt_pcb_t *pcb = data;
956ff6d951SJohn Birrell 	dtrace_difv_t *dvp;
966ff6d951SJohn Birrell 	ssize_t stroff;
976ff6d951SJohn Birrell 	dt_node_t dn;
986ff6d951SJohn Birrell 
996ff6d951SJohn Birrell 	if (!(idp->di_flags & (DT_IDFLG_DIFR | DT_IDFLG_DIFW)))
1006ff6d951SJohn Birrell 		return (0); /* omit variable from vartab */
1016ff6d951SJohn Birrell 
1026ff6d951SJohn Birrell 	dvp = &pcb->pcb_difo->dtdo_vartab[pcb->pcb_asvidx++];
1036ff6d951SJohn Birrell 	stroff = dt_strtab_insert(pcb->pcb_strtab, idp->di_name);
1046ff6d951SJohn Birrell 
1056ff6d951SJohn Birrell 	if (stroff == -1L)
1066ff6d951SJohn Birrell 		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
1076ff6d951SJohn Birrell 	if (stroff > DIF_STROFF_MAX)
1086ff6d951SJohn Birrell 		longjmp(pcb->pcb_jmpbuf, EDT_STR2BIG);
1096ff6d951SJohn Birrell 
1106ff6d951SJohn Birrell 	dvp->dtdv_name = (uint_t)stroff;
1116ff6d951SJohn Birrell 	dvp->dtdv_id = idp->di_id;
1126ff6d951SJohn Birrell 	dvp->dtdv_flags = 0;
1136ff6d951SJohn Birrell 
1146ff6d951SJohn Birrell 	dvp->dtdv_kind = (idp->di_kind == DT_IDENT_ARRAY) ?
1156ff6d951SJohn Birrell 	    DIFV_KIND_ARRAY : DIFV_KIND_SCALAR;
1166ff6d951SJohn Birrell 
1176ff6d951SJohn Birrell 	if (idp->di_flags & DT_IDFLG_LOCAL)
1186ff6d951SJohn Birrell 		dvp->dtdv_scope = DIFV_SCOPE_LOCAL;
1196ff6d951SJohn Birrell 	else if (idp->di_flags & DT_IDFLG_TLS)
1206ff6d951SJohn Birrell 		dvp->dtdv_scope = DIFV_SCOPE_THREAD;
1216ff6d951SJohn Birrell 	else
1226ff6d951SJohn Birrell 		dvp->dtdv_scope = DIFV_SCOPE_GLOBAL;
1236ff6d951SJohn Birrell 
1246ff6d951SJohn Birrell 	if (idp->di_flags & DT_IDFLG_DIFR)
1256ff6d951SJohn Birrell 		dvp->dtdv_flags |= DIFV_F_REF;
1266ff6d951SJohn Birrell 	if (idp->di_flags & DT_IDFLG_DIFW)
1276ff6d951SJohn Birrell 		dvp->dtdv_flags |= DIFV_F_MOD;
1286ff6d951SJohn Birrell 
1296ff6d951SJohn Birrell 	bzero(&dn, sizeof (dn));
130*8e648814SRui Paulo 	dt_node_type_assign(&dn, idp->di_ctfp, idp->di_type, B_FALSE);
1316ff6d951SJohn Birrell 	dt_node_diftype(pcb->pcb_hdl, &dn, &dvp->dtdv_type);
1326ff6d951SJohn Birrell 
1336ff6d951SJohn Birrell 	idp->di_flags &= ~(DT_IDFLG_DIFR | DT_IDFLG_DIFW);
1346ff6d951SJohn Birrell 	return (0);
1356ff6d951SJohn Birrell }
1366ff6d951SJohn Birrell 
1376ff6d951SJohn Birrell static ssize_t
dt_copystr(const char * s,size_t n,size_t off,dt_pcb_t * pcb)1386ff6d951SJohn Birrell dt_copystr(const char *s, size_t n, size_t off, dt_pcb_t *pcb)
1396ff6d951SJohn Birrell {
1406ff6d951SJohn Birrell 	bcopy(s, pcb->pcb_difo->dtdo_strtab + off, n);
1416ff6d951SJohn Birrell 	return (n);
1426ff6d951SJohn Birrell }
1436ff6d951SJohn Birrell 
1446ff6d951SJohn Birrell /*
1456ff6d951SJohn Birrell  * Rewrite the xlate/xlarg instruction at dtdo_buf[i] so that the instruction's
1466ff6d951SJohn Birrell  * xltab index reflects the offset 'xi' of the assigned dtdo_xlmtab[] location.
1476ff6d951SJohn Birrell  * We track the cumulative references to translators and members in the pcb's
1486ff6d951SJohn Birrell  * pcb_asxrefs[] array, a two-dimensional array of bitmaps indexed by the
1496ff6d951SJohn Birrell  * global translator id and then by the corresponding translator member id.
1506ff6d951SJohn Birrell  */
1516ff6d951SJohn Birrell static void
dt_as_xlate(dt_pcb_t * pcb,dtrace_difo_t * dp,uint_t i,uint_t xi,dt_node_t * dnp)1526ff6d951SJohn Birrell dt_as_xlate(dt_pcb_t *pcb, dtrace_difo_t *dp,
1536ff6d951SJohn Birrell     uint_t i, uint_t xi, dt_node_t *dnp)
1546ff6d951SJohn Birrell {
1556ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = pcb->pcb_hdl;
1566ff6d951SJohn Birrell 	dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
1576ff6d951SJohn Birrell 
1586ff6d951SJohn Birrell 	assert(i < dp->dtdo_len);
1596ff6d951SJohn Birrell 	assert(xi < dp->dtdo_xlmlen);
1606ff6d951SJohn Birrell 
1616ff6d951SJohn Birrell 	assert(dnp->dn_kind == DT_NODE_MEMBER);
1626ff6d951SJohn Birrell 	assert(dnp->dn_membexpr->dn_kind == DT_NODE_XLATOR);
1636ff6d951SJohn Birrell 
1646ff6d951SJohn Birrell 	assert(dxp->dx_id < dtp->dt_xlatorid);
1656ff6d951SJohn Birrell 	assert(dnp->dn_membid < dxp->dx_nmembers);
1666ff6d951SJohn Birrell 
1676ff6d951SJohn Birrell 	if (pcb->pcb_asxrefs == NULL) {
1686ff6d951SJohn Birrell 		pcb->pcb_asxreflen = dtp->dt_xlatorid;
1696ff6d951SJohn Birrell 		pcb->pcb_asxrefs =
1706ff6d951SJohn Birrell 		    dt_zalloc(dtp, sizeof (ulong_t *) * pcb->pcb_asxreflen);
1716ff6d951SJohn Birrell 		if (pcb->pcb_asxrefs == NULL)
1726ff6d951SJohn Birrell 			longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
1736ff6d951SJohn Birrell 	}
1746ff6d951SJohn Birrell 
1756ff6d951SJohn Birrell 	if (pcb->pcb_asxrefs[dxp->dx_id] == NULL) {
1766ff6d951SJohn Birrell 		pcb->pcb_asxrefs[dxp->dx_id] =
1776ff6d951SJohn Birrell 		    dt_zalloc(dtp, BT_SIZEOFMAP(dxp->dx_nmembers));
1786ff6d951SJohn Birrell 		if (pcb->pcb_asxrefs[dxp->dx_id] == NULL)
1796ff6d951SJohn Birrell 			longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
1806ff6d951SJohn Birrell 	}
1816ff6d951SJohn Birrell 
1826ff6d951SJohn Birrell 	dp->dtdo_buf[i] = DIF_INSTR_XLATE(
1836ff6d951SJohn Birrell 	    DIF_INSTR_OP(dp->dtdo_buf[i]), xi, DIF_INSTR_RD(dp->dtdo_buf[i]));
1846ff6d951SJohn Birrell 
1856ff6d951SJohn Birrell 	BT_SET(pcb->pcb_asxrefs[dxp->dx_id], dnp->dn_membid);
1866ff6d951SJohn Birrell 	dp->dtdo_xlmtab[xi] = dnp;
1876ff6d951SJohn Birrell }
1886ff6d951SJohn Birrell 
1896ff6d951SJohn Birrell static void
dt_as_undef(const dt_ident_t * idp,uint_t offset)1906ff6d951SJohn Birrell dt_as_undef(const dt_ident_t *idp, uint_t offset)
1916ff6d951SJohn Birrell {
1926ff6d951SJohn Birrell 	const char *kind, *mark = (idp->di_flags & DT_IDFLG_USER) ? "``" : "`";
1936ff6d951SJohn Birrell 	const dtrace_syminfo_t *dts = idp->di_data;
1946ff6d951SJohn Birrell 
1956ff6d951SJohn Birrell 	if (idp->di_flags & DT_IDFLG_USER)
1966ff6d951SJohn Birrell 		kind = "user";
1976ff6d951SJohn Birrell 	else if (idp->di_flags & DT_IDFLG_PRIM)
1986ff6d951SJohn Birrell 		kind = "primary kernel";
1996ff6d951SJohn Birrell 	else
2006ff6d951SJohn Birrell 		kind = "loadable kernel";
2016ff6d951SJohn Birrell 
2026ff6d951SJohn Birrell 	yylineno = idp->di_lineno;
2036ff6d951SJohn Birrell 
2046ff6d951SJohn Birrell 	xyerror(D_ASRELO, "relocation remains against %s symbol %s%s%s (offset "
2056ff6d951SJohn Birrell 	    "0x%x)\n", kind, dts->dts_object, mark, dts->dts_name, offset);
2066ff6d951SJohn Birrell }
2076ff6d951SJohn Birrell 
2086ff6d951SJohn Birrell dtrace_difo_t *
dt_as(dt_pcb_t * pcb)2096ff6d951SJohn Birrell dt_as(dt_pcb_t *pcb)
2106ff6d951SJohn Birrell {
2116ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = pcb->pcb_hdl;
2126ff6d951SJohn Birrell 	dt_irlist_t *dlp = &pcb->pcb_ir;
2136ff6d951SJohn Birrell 	uint_t *labels = NULL;
2146ff6d951SJohn Birrell 	dt_irnode_t *dip;
2156ff6d951SJohn Birrell 	dtrace_difo_t *dp;
2166ff6d951SJohn Birrell 	dt_ident_t *idp;
2176ff6d951SJohn Birrell 
2186ff6d951SJohn Birrell 	size_t n = 0;
2196ff6d951SJohn Birrell 	uint_t i;
2206ff6d951SJohn Birrell 
2216ff6d951SJohn Birrell 	uint_t kmask, kbits, umask, ubits;
2226ff6d951SJohn Birrell 	uint_t krel = 0, urel = 0, xlrefs = 0;
2236ff6d951SJohn Birrell 
2246ff6d951SJohn Birrell 	/*
2256ff6d951SJohn Birrell 	 * Select bitmasks based upon the desired symbol linking policy.  We
2266ff6d951SJohn Birrell 	 * test (di_extern->di_flags & xmask) == xbits to determine if the
2276ff6d951SJohn Birrell 	 * symbol should have a relocation entry generated in the loop below.
2286ff6d951SJohn Birrell 	 *
2296ff6d951SJohn Birrell 	 * DT_LINK_KERNEL = kernel symbols static, user symbols dynamic
2306ff6d951SJohn Birrell 	 * DT_LINK_PRIMARY = primary kernel symbols static, others dynamic
2316ff6d951SJohn Birrell 	 * DT_LINK_DYNAMIC = all symbols dynamic
2326ff6d951SJohn Birrell 	 * DT_LINK_STATIC = all symbols static
2336ff6d951SJohn Birrell 	 *
2346ff6d951SJohn Birrell 	 * By 'static' we mean that we use the symbol's value at compile-time
2356ff6d951SJohn Birrell 	 * in the final DIF.  By 'dynamic' we mean that we create a relocation
2366ff6d951SJohn Birrell 	 * table entry for the symbol's value so it can be relocated later.
2376ff6d951SJohn Birrell 	 */
2386ff6d951SJohn Birrell 	switch (dtp->dt_linkmode) {
2396ff6d951SJohn Birrell 	case DT_LINK_KERNEL:
2406ff6d951SJohn Birrell 		kmask = 0;
2416ff6d951SJohn Birrell 		kbits = -1u;
2426ff6d951SJohn Birrell 		umask = DT_IDFLG_USER;
2436ff6d951SJohn Birrell 		ubits = DT_IDFLG_USER;
2446ff6d951SJohn Birrell 		break;
2456ff6d951SJohn Birrell 	case DT_LINK_PRIMARY:
2466ff6d951SJohn Birrell 		kmask = DT_IDFLG_USER | DT_IDFLG_PRIM;
2476ff6d951SJohn Birrell 		kbits = 0;
2486ff6d951SJohn Birrell 		umask = DT_IDFLG_USER;
2496ff6d951SJohn Birrell 		ubits = DT_IDFLG_USER;
2506ff6d951SJohn Birrell 		break;
2516ff6d951SJohn Birrell 	case DT_LINK_DYNAMIC:
2526ff6d951SJohn Birrell 		kmask = DT_IDFLG_USER;
2536ff6d951SJohn Birrell 		kbits = 0;
2546ff6d951SJohn Birrell 		umask = DT_IDFLG_USER;
2556ff6d951SJohn Birrell 		ubits = DT_IDFLG_USER;
2566ff6d951SJohn Birrell 		break;
2576ff6d951SJohn Birrell 	case DT_LINK_STATIC:
2586ff6d951SJohn Birrell 		kmask = umask = 0;
2596ff6d951SJohn Birrell 		kbits = ubits = -1u;
2606ff6d951SJohn Birrell 		break;
2616ff6d951SJohn Birrell 	default:
2626ff6d951SJohn Birrell 		xyerror(D_UNKNOWN, "internal error -- invalid link mode %u\n",
2636ff6d951SJohn Birrell 		    dtp->dt_linkmode);
2646ff6d951SJohn Birrell 	}
2656ff6d951SJohn Birrell 
2666ff6d951SJohn Birrell 	assert(pcb->pcb_difo == NULL);
2676ff6d951SJohn Birrell 	pcb->pcb_difo = dt_zalloc(dtp, sizeof (dtrace_difo_t));
2686ff6d951SJohn Birrell 
2696ff6d951SJohn Birrell 	if ((dp = pcb->pcb_difo) == NULL)
2706ff6d951SJohn Birrell 		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
2716ff6d951SJohn Birrell 
2726ff6d951SJohn Birrell 	dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * dlp->dl_len);
2736ff6d951SJohn Birrell 
2746ff6d951SJohn Birrell 	if (dp->dtdo_buf == NULL)
2756ff6d951SJohn Birrell 		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
2766ff6d951SJohn Birrell 
2776ff6d951SJohn Birrell 	if ((labels = dt_alloc(dtp, sizeof (uint_t) * dlp->dl_label)) == NULL)
2786ff6d951SJohn Birrell 		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
2796ff6d951SJohn Birrell 
2806ff6d951SJohn Birrell 	/*
2816ff6d951SJohn Birrell 	 * Make an initial pass through the instruction list, filling in the
2826ff6d951SJohn Birrell 	 * instruction buffer with valid instructions and skipping labeled nops.
2836ff6d951SJohn Birrell 	 * While doing this, we also fill in our labels[] translation table
2846ff6d951SJohn Birrell 	 * and we count up the number of relocation table entries we will need.
2856ff6d951SJohn Birrell 	 */
2866ff6d951SJohn Birrell 	for (i = 0, dip = dlp->dl_list; dip != NULL; dip = dip->di_next) {
2876ff6d951SJohn Birrell 		if (dip->di_label != DT_LBL_NONE)
2886ff6d951SJohn Birrell 			labels[dip->di_label] = i;
2896ff6d951SJohn Birrell 
2906ff6d951SJohn Birrell 		if (dip->di_label == DT_LBL_NONE ||
2916ff6d951SJohn Birrell 		    dip->di_instr != DIF_INSTR_NOP)
2926ff6d951SJohn Birrell 			dp->dtdo_buf[i++] = dip->di_instr;
2936ff6d951SJohn Birrell 
2946ff6d951SJohn Birrell 		if (dip->di_extern == NULL)
2956ff6d951SJohn Birrell 			continue; /* no external references needed */
2966ff6d951SJohn Birrell 
2976ff6d951SJohn Birrell 		switch (DIF_INSTR_OP(dip->di_instr)) {
2986ff6d951SJohn Birrell 		case DIF_OP_SETX:
2996ff6d951SJohn Birrell 			idp = dip->di_extern;
3006ff6d951SJohn Birrell 			if ((idp->di_flags & kmask) == kbits)
3016ff6d951SJohn Birrell 				krel++;
3026ff6d951SJohn Birrell 			else if ((idp->di_flags & umask) == ubits)
3036ff6d951SJohn Birrell 				urel++;
3046ff6d951SJohn Birrell 			break;
3056ff6d951SJohn Birrell 		case DIF_OP_XLATE:
3066ff6d951SJohn Birrell 		case DIF_OP_XLARG:
3076ff6d951SJohn Birrell 			xlrefs++;
3086ff6d951SJohn Birrell 			break;
3096ff6d951SJohn Birrell 		default:
3106ff6d951SJohn Birrell 			xyerror(D_UNKNOWN, "unexpected assembler relocation "
3116ff6d951SJohn Birrell 			    "for opcode 0x%x\n", DIF_INSTR_OP(dip->di_instr));
3126ff6d951SJohn Birrell 		}
3136ff6d951SJohn Birrell 	}
3146ff6d951SJohn Birrell 
3156ff6d951SJohn Birrell 	assert(i == dlp->dl_len);
3166ff6d951SJohn Birrell 	dp->dtdo_len = dlp->dl_len;
3176ff6d951SJohn Birrell 
3186ff6d951SJohn Birrell 	/*
3196ff6d951SJohn Birrell 	 * Make a second pass through the instructions, relocating each branch
3206ff6d951SJohn Birrell 	 * label to the index of the final instruction in the buffer and noting
3216ff6d951SJohn Birrell 	 * any other instruction-specific DIFO flags such as dtdo_destructive.
3226ff6d951SJohn Birrell 	 */
3236ff6d951SJohn Birrell 	for (i = 0; i < dp->dtdo_len; i++) {
3246ff6d951SJohn Birrell 		dif_instr_t instr = dp->dtdo_buf[i];
3256ff6d951SJohn Birrell 		uint_t op = DIF_INSTR_OP(instr);
3266ff6d951SJohn Birrell 
3276ff6d951SJohn Birrell 		if (op == DIF_OP_CALL) {
3286ff6d951SJohn Birrell 			if (DIF_INSTR_SUBR(instr) == DIF_SUBR_COPYOUT ||
3296ff6d951SJohn Birrell 			    DIF_INSTR_SUBR(instr) == DIF_SUBR_COPYOUTSTR)
3306ff6d951SJohn Birrell 				dp->dtdo_destructive = 1;
3316ff6d951SJohn Birrell 			continue;
3326ff6d951SJohn Birrell 		}
3336ff6d951SJohn Birrell 
3346ff6d951SJohn Birrell 		if (op >= DIF_OP_BA && op <= DIF_OP_BLEU) {
3356ff6d951SJohn Birrell 			assert(DIF_INSTR_LABEL(instr) < dlp->dl_label);
3366ff6d951SJohn Birrell 			dp->dtdo_buf[i] = DIF_INSTR_BRANCH(op,
3376ff6d951SJohn Birrell 			    labels[DIF_INSTR_LABEL(instr)]);
3386ff6d951SJohn Birrell 		}
3396ff6d951SJohn Birrell 	}
3406ff6d951SJohn Birrell 
3416ff6d951SJohn Birrell 	dt_free(dtp, labels);
3426ff6d951SJohn Birrell 	pcb->pcb_asvidx = 0;
3436ff6d951SJohn Birrell 
3446ff6d951SJohn Birrell 	/*
3456ff6d951SJohn Birrell 	 * Allocate memory for the appropriate number of variable records and
3466ff6d951SJohn Birrell 	 * then fill in each variable record.  As we populate the variable
3476ff6d951SJohn Birrell 	 * table we insert the corresponding variable names into the strtab.
3486ff6d951SJohn Birrell 	 */
3496ff6d951SJohn Birrell 	(void) dt_idhash_iter(dtp->dt_tls, dt_countvar, &n);
3506ff6d951SJohn Birrell 	(void) dt_idhash_iter(dtp->dt_globals, dt_countvar, &n);
3516ff6d951SJohn Birrell 	(void) dt_idhash_iter(pcb->pcb_locals, dt_countvar, &n);
3526ff6d951SJohn Birrell 
3536ff6d951SJohn Birrell 	if (n != 0) {
3546ff6d951SJohn Birrell 		dp->dtdo_vartab = dt_alloc(dtp, n * sizeof (dtrace_difv_t));
3556ff6d951SJohn Birrell 		dp->dtdo_varlen = (uint32_t)n;
3566ff6d951SJohn Birrell 
3576ff6d951SJohn Birrell 		if (dp->dtdo_vartab == NULL)
3586ff6d951SJohn Birrell 			longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
3596ff6d951SJohn Birrell 
3606ff6d951SJohn Birrell 		(void) dt_idhash_iter(dtp->dt_tls, dt_copyvar, pcb);
3616ff6d951SJohn Birrell 		(void) dt_idhash_iter(dtp->dt_globals, dt_copyvar, pcb);
3626ff6d951SJohn Birrell 		(void) dt_idhash_iter(pcb->pcb_locals, dt_copyvar, pcb);
3636ff6d951SJohn Birrell 	}
3646ff6d951SJohn Birrell 
3656ff6d951SJohn Birrell 	/*
3666ff6d951SJohn Birrell 	 * Allocate memory for the appropriate number of relocation table
3676ff6d951SJohn Birrell 	 * entries based upon our kernel and user counts from the first pass.
3686ff6d951SJohn Birrell 	 */
3696ff6d951SJohn Birrell 	if (krel != 0) {
3706ff6d951SJohn Birrell 		dp->dtdo_kreltab = dt_alloc(dtp,
3716ff6d951SJohn Birrell 		    krel * sizeof (dof_relodesc_t));
3726ff6d951SJohn Birrell 		dp->dtdo_krelen = krel;
3736ff6d951SJohn Birrell 
3746ff6d951SJohn Birrell 		if (dp->dtdo_kreltab == NULL)
3756ff6d951SJohn Birrell 			longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
3766ff6d951SJohn Birrell 	}
3776ff6d951SJohn Birrell 
3786ff6d951SJohn Birrell 	if (urel != 0) {
3796ff6d951SJohn Birrell 		dp->dtdo_ureltab = dt_alloc(dtp,
3806ff6d951SJohn Birrell 		    urel * sizeof (dof_relodesc_t));
3816ff6d951SJohn Birrell 		dp->dtdo_urelen = urel;
3826ff6d951SJohn Birrell 
3836ff6d951SJohn Birrell 		if (dp->dtdo_ureltab == NULL)
3846ff6d951SJohn Birrell 			longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
3856ff6d951SJohn Birrell 	}
3866ff6d951SJohn Birrell 
3876ff6d951SJohn Birrell 	if (xlrefs != 0) {
3886ff6d951SJohn Birrell 		dp->dtdo_xlmtab = dt_zalloc(dtp, sizeof (dt_node_t *) * xlrefs);
3896ff6d951SJohn Birrell 		dp->dtdo_xlmlen = xlrefs;
3906ff6d951SJohn Birrell 
3916ff6d951SJohn Birrell 		if (dp->dtdo_xlmtab == NULL)
3926ff6d951SJohn Birrell 			longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
3936ff6d951SJohn Birrell 	}
3946ff6d951SJohn Birrell 
3956ff6d951SJohn Birrell 	/*
3966ff6d951SJohn Birrell 	 * If any relocations are needed, make another pass through the
3976ff6d951SJohn Birrell 	 * instruction list and fill in the relocation table entries.
3986ff6d951SJohn Birrell 	 */
3996ff6d951SJohn Birrell 	if (krel + urel + xlrefs != 0) {
4006ff6d951SJohn Birrell 		uint_t knodef = pcb->pcb_cflags & DTRACE_C_KNODEF;
4016ff6d951SJohn Birrell 		uint_t unodef = pcb->pcb_cflags & DTRACE_C_UNODEF;
4026ff6d951SJohn Birrell 
4036ff6d951SJohn Birrell 		dof_relodesc_t *krp = dp->dtdo_kreltab;
4046ff6d951SJohn Birrell 		dof_relodesc_t *urp = dp->dtdo_ureltab;
4056ff6d951SJohn Birrell 		dt_node_t **xlp = dp->dtdo_xlmtab;
4066ff6d951SJohn Birrell 
4076ff6d951SJohn Birrell 		i = 0; /* dtdo_buf[] index */
4086ff6d951SJohn Birrell 
4096ff6d951SJohn Birrell 		for (dip = dlp->dl_list; dip != NULL; dip = dip->di_next) {
4106ff6d951SJohn Birrell 			dof_relodesc_t *rp;
4116ff6d951SJohn Birrell 			ssize_t soff;
4126ff6d951SJohn Birrell 			uint_t nodef;
4136ff6d951SJohn Birrell 
4146ff6d951SJohn Birrell 			if (dip->di_label != DT_LBL_NONE &&
4156ff6d951SJohn Birrell 			    dip->di_instr == DIF_INSTR_NOP)
4166ff6d951SJohn Birrell 				continue; /* skip label declarations */
4176ff6d951SJohn Birrell 
4186ff6d951SJohn Birrell 			i++; /* advance dtdo_buf[] index */
4196ff6d951SJohn Birrell 
4206ff6d951SJohn Birrell 			if (DIF_INSTR_OP(dip->di_instr) == DIF_OP_XLATE ||
4216ff6d951SJohn Birrell 			    DIF_INSTR_OP(dip->di_instr) == DIF_OP_XLARG) {
4226ff6d951SJohn Birrell 				assert(dp->dtdo_buf[i - 1] == dip->di_instr);
4236ff6d951SJohn Birrell 				dt_as_xlate(pcb, dp, i - 1, (uint_t)
4246ff6d951SJohn Birrell 				    (xlp++ - dp->dtdo_xlmtab), dip->di_extern);
4256ff6d951SJohn Birrell 				continue;
4266ff6d951SJohn Birrell 			}
4276ff6d951SJohn Birrell 
4286ff6d951SJohn Birrell 			if ((idp = dip->di_extern) == NULL)
4296ff6d951SJohn Birrell 				continue; /* no relocation entry needed */
4306ff6d951SJohn Birrell 
4316ff6d951SJohn Birrell 			if ((idp->di_flags & kmask) == kbits) {
4326ff6d951SJohn Birrell 				nodef = knodef;
4336ff6d951SJohn Birrell 				rp = krp++;
4346ff6d951SJohn Birrell 			} else if ((idp->di_flags & umask) == ubits) {
4356ff6d951SJohn Birrell 				nodef = unodef;
4366ff6d951SJohn Birrell 				rp = urp++;
4376ff6d951SJohn Birrell 			} else
4386ff6d951SJohn Birrell 				continue;
4396ff6d951SJohn Birrell 
4406ff6d951SJohn Birrell 			if (!nodef)
4416ff6d951SJohn Birrell 				dt_as_undef(idp, i);
4426ff6d951SJohn Birrell 
4436ff6d951SJohn Birrell 			assert(DIF_INSTR_OP(dip->di_instr) == DIF_OP_SETX);
4446ff6d951SJohn Birrell 			soff = dt_strtab_insert(pcb->pcb_strtab, idp->di_name);
4456ff6d951SJohn Birrell 
4466ff6d951SJohn Birrell 			if (soff == -1L)
4476ff6d951SJohn Birrell 				longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
4486ff6d951SJohn Birrell 			if (soff > DIF_STROFF_MAX)
4496ff6d951SJohn Birrell 				longjmp(pcb->pcb_jmpbuf, EDT_STR2BIG);
4506ff6d951SJohn Birrell 
4516ff6d951SJohn Birrell 			rp->dofr_name = (dof_stridx_t)soff;
4526ff6d951SJohn Birrell 			rp->dofr_type = DOF_RELO_SETX;
4536ff6d951SJohn Birrell 			rp->dofr_offset = DIF_INSTR_INTEGER(dip->di_instr) *
4546ff6d951SJohn Birrell 			    sizeof (uint64_t);
4556ff6d951SJohn Birrell 			rp->dofr_data = 0;
4566ff6d951SJohn Birrell 		}
4576ff6d951SJohn Birrell 
4586ff6d951SJohn Birrell 		assert(krp == dp->dtdo_kreltab + dp->dtdo_krelen);
4596ff6d951SJohn Birrell 		assert(urp == dp->dtdo_ureltab + dp->dtdo_urelen);
4606ff6d951SJohn Birrell 		assert(xlp == dp->dtdo_xlmtab + dp->dtdo_xlmlen);
4616ff6d951SJohn Birrell 		assert(i == dp->dtdo_len);
4626ff6d951SJohn Birrell 	}
4636ff6d951SJohn Birrell 
4646ff6d951SJohn Birrell 	/*
4656ff6d951SJohn Birrell 	 * Allocate memory for the compiled string table and then copy the
4666ff6d951SJohn Birrell 	 * chunks from the string table into the final string buffer.
4676ff6d951SJohn Birrell 	 */
4686ff6d951SJohn Birrell 	if ((n = dt_strtab_size(pcb->pcb_strtab)) != 0) {
4696ff6d951SJohn Birrell 		if ((dp->dtdo_strtab = dt_alloc(dtp, n)) == NULL)
4706ff6d951SJohn Birrell 			longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
4716ff6d951SJohn Birrell 
4726ff6d951SJohn Birrell 		(void) dt_strtab_write(pcb->pcb_strtab,
4736ff6d951SJohn Birrell 		    (dt_strtab_write_f *)dt_copystr, pcb);
4746ff6d951SJohn Birrell 		dp->dtdo_strlen = (uint32_t)n;
4756ff6d951SJohn Birrell 	}
4766ff6d951SJohn Birrell 
4776ff6d951SJohn Birrell 	/*
4786ff6d951SJohn Birrell 	 * Allocate memory for the compiled integer table and then copy the
4796ff6d951SJohn Birrell 	 * integer constants from the table into the final integer buffer.
4806ff6d951SJohn Birrell 	 */
4816ff6d951SJohn Birrell 	if ((n = dt_inttab_size(pcb->pcb_inttab)) != 0) {
4826ff6d951SJohn Birrell 		if ((dp->dtdo_inttab = dt_alloc(dtp,
4836ff6d951SJohn Birrell 		    n * sizeof (uint64_t))) == NULL)
4846ff6d951SJohn Birrell 			longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
4856ff6d951SJohn Birrell 
4866ff6d951SJohn Birrell 		dt_inttab_write(pcb->pcb_inttab, dp->dtdo_inttab);
4876ff6d951SJohn Birrell 		dp->dtdo_intlen = (uint32_t)n;
4886ff6d951SJohn Birrell 	}
4896ff6d951SJohn Birrell 
4906ff6d951SJohn Birrell 	/*
4916ff6d951SJohn Birrell 	 * Fill in the DIFO return type from the type associated with the
4926ff6d951SJohn Birrell 	 * node saved in pcb_dret, and then clear pcb_difo and pcb_dret
4936ff6d951SJohn Birrell 	 * now that the assembler has completed successfully.
4946ff6d951SJohn Birrell 	 */
4956ff6d951SJohn Birrell 	dt_node_diftype(dtp, pcb->pcb_dret, &dp->dtdo_rtype);
4966ff6d951SJohn Birrell 	pcb->pcb_difo = NULL;
4976ff6d951SJohn Birrell 	pcb->pcb_dret = NULL;
4986ff6d951SJohn Birrell 
4996ff6d951SJohn Birrell 	if (pcb->pcb_cflags & DTRACE_C_DIFV)
5006ff6d951SJohn Birrell 		dt_dis(dp, stderr);
5016ff6d951SJohn Birrell 
5026ff6d951SJohn Birrell 	return (dp);
5036ff6d951SJohn Birrell }
504