1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
28*7c478bd9Sstevel@tonic-gate #include	<proc_service.h>
29*7c478bd9Sstevel@tonic-gate #include	<link.h>
30*7c478bd9Sstevel@tonic-gate #include	<rtld_db.h>
31*7c478bd9Sstevel@tonic-gate #include	<_rtld_db.h>
32*7c478bd9Sstevel@tonic-gate #include	<msg.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate /*
36*7c478bd9Sstevel@tonic-gate  * A un-initialized PLT look like so:
37*7c478bd9Sstevel@tonic-gate  *
38*7c478bd9Sstevel@tonic-gate  * .PLT
39*7c478bd9Sstevel@tonic-gate  *	sethi	(.-.PLT0), %g1
40*7c478bd9Sstevel@tonic-gate  *	ba,a	.PLT0
41*7c478bd9Sstevel@tonic-gate  *	nop
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  * To test to see if this is an uninitialized PLT we check
44*7c478bd9Sstevel@tonic-gate  * the second instruction and confirm that it's a branch.
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate /* ARGSUSED 2 */
47*7c478bd9Sstevel@tonic-gate rd_err_e
plt32_resolution(rd_agent_t * rap,psaddr_t pc,lwpid_t lwpid,psaddr_t pltbase,rd_plt_info_t * rpi)48*7c478bd9Sstevel@tonic-gate plt32_resolution(rd_agent_t *rap, psaddr_t pc, lwpid_t lwpid,
49*7c478bd9Sstevel@tonic-gate 	psaddr_t pltbase, rd_plt_info_t *rpi)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	unsigned int	instr[4];
52*7c478bd9Sstevel@tonic-gate 	rd_err_e	rerr;
53*7c478bd9Sstevel@tonic-gate 	psaddr_t	destaddr = 0;
54*7c478bd9Sstevel@tonic-gate 	psaddr_t	pltoff, pltaddr;
55*7c478bd9Sstevel@tonic-gate 	int		pltbound = 0;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	pltoff = pc - pltbase;
58*7c478bd9Sstevel@tonic-gate 	pltaddr = pltbase +
59*7c478bd9Sstevel@tonic-gate 		((pltoff / M32_PLT_ENTSIZE) * M32_PLT_ENTSIZE);
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	if (ps_pread(rap->rd_psp, pltaddr, (char *)instr,
62*7c478bd9Sstevel@tonic-gate 	    M32_PLT_ENTSIZE) != PS_OK) {
63*7c478bd9Sstevel@tonic-gate 		LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_2), EC_ADDR(pltaddr)));
64*7c478bd9Sstevel@tonic-gate 		return (RD_ERR);
65*7c478bd9Sstevel@tonic-gate 	}
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if (rtld_db_version >= RD_VERSION3) {
68*7c478bd9Sstevel@tonic-gate 		rpi->pi_flags = 0;
69*7c478bd9Sstevel@tonic-gate 		rpi->pi_baddr = 0;
70*7c478bd9Sstevel@tonic-gate 	}
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	if ((instr[0] != M_NOP) &&
73*7c478bd9Sstevel@tonic-gate 	    ((instr[1] & (~(S_MASK(22)))) == M_BA_A)) {
74*7c478bd9Sstevel@tonic-gate 		/*
75*7c478bd9Sstevel@tonic-gate 		 * Unbound PLT
76*7c478bd9Sstevel@tonic-gate 		 */
77*7c478bd9Sstevel@tonic-gate 		if ((rerr = rd_binder_exit_addr(rap, MSG_ORIG(MSG_SYM_RTBIND),
78*7c478bd9Sstevel@tonic-gate 		    &(rpi->pi_target))) != RD_OK) {
79*7c478bd9Sstevel@tonic-gate 			return (rerr);
80*7c478bd9Sstevel@tonic-gate 		}
81*7c478bd9Sstevel@tonic-gate 		rpi->pi_skip_method = RD_RESOLVE_TARGET_STEP;
82*7c478bd9Sstevel@tonic-gate 		rpi->pi_nstep = 1;
83*7c478bd9Sstevel@tonic-gate 	} else if ((instr[2] & (~(S_MASK(13)))) == M_JMPL) {
84*7c478bd9Sstevel@tonic-gate 		/*
85*7c478bd9Sstevel@tonic-gate 		 * Resolved 32-bit PLT entry format (full-32):
86*7c478bd9Sstevel@tonic-gate 		 *
87*7c478bd9Sstevel@tonic-gate 		 * .PLT:
88*7c478bd9Sstevel@tonic-gate 		 * 0	sethi	(.-PLT0), %g1
89*7c478bd9Sstevel@tonic-gate 		 * 1	sethi	%hi(dest), %g1
90*7c478bd9Sstevel@tonic-gate 		 * 2	jmpl	%g1 + lo(dest), %g0
91*7c478bd9Sstevel@tonic-gate 		 * 3	nop
92*7c478bd9Sstevel@tonic-gate 		 */
93*7c478bd9Sstevel@tonic-gate 		rpi->pi_skip_method = RD_RESOLVE_STEP;
94*7c478bd9Sstevel@tonic-gate 		rpi->pi_nstep = 4;
95*7c478bd9Sstevel@tonic-gate 		rpi->pi_target = 0;
96*7c478bd9Sstevel@tonic-gate 		if (rtld_db_version >= RD_VERSION3) {
97*7c478bd9Sstevel@tonic-gate 			uint_t		hi_bits;
98*7c478bd9Sstevel@tonic-gate 			uint_t		lo_bits;
99*7c478bd9Sstevel@tonic-gate 			hi_bits = instr[1] & S_MASK(22); /* 31..10 */
100*7c478bd9Sstevel@tonic-gate 			lo_bits = instr[2] & S_MASK(10); /* 09..00 */
101*7c478bd9Sstevel@tonic-gate 			destaddr = (hi_bits << 10) | lo_bits;
102*7c478bd9Sstevel@tonic-gate 			pltbound++;
103*7c478bd9Sstevel@tonic-gate 		}
104*7c478bd9Sstevel@tonic-gate 	} else  if ((instr[0] == M_NOP) &&
105*7c478bd9Sstevel@tonic-gate 	    ((instr[1] & (~(S_MASK(22)))) == M_BA_A)) {
106*7c478bd9Sstevel@tonic-gate 		/*
107*7c478bd9Sstevel@tonic-gate 		 * Resolved 32-bit PLT entry format (b+-8mb):
108*7c478bd9Sstevel@tonic-gate 		 * .PLT
109*7c478bd9Sstevel@tonic-gate 		 * 0	nop
110*7c478bd9Sstevel@tonic-gate 		 * 1	ba,a	<dest>
111*7c478bd9Sstevel@tonic-gate 		 * 2	nop
112*7c478bd9Sstevel@tonic-gate 		 * 3	nop
113*7c478bd9Sstevel@tonic-gate 		 */
114*7c478bd9Sstevel@tonic-gate 		rpi->pi_skip_method = RD_RESOLVE_STEP;
115*7c478bd9Sstevel@tonic-gate 		rpi->pi_nstep = 2;
116*7c478bd9Sstevel@tonic-gate 		rpi->pi_target = 0;
117*7c478bd9Sstevel@tonic-gate 		if (rtld_db_version >= RD_VERSION3) {
118*7c478bd9Sstevel@tonic-gate 			uint_t	d22;
119*7c478bd9Sstevel@tonic-gate 			d22 = instr[1] & S_MASK(22);
120*7c478bd9Sstevel@tonic-gate 			destaddr = ((int)pltaddr + 4) +
121*7c478bd9Sstevel@tonic-gate 				(((int)d22 << 10) >> 8);
122*7c478bd9Sstevel@tonic-gate 			pltbound++;
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate 	} else if ((instr[0] == M_NOP) &&
125*7c478bd9Sstevel@tonic-gate 	    ((instr[1] & (~(S_MASK(19)))) == M_BA_A_PT)) {
126*7c478bd9Sstevel@tonic-gate 		/*
127*7c478bd9Sstevel@tonic-gate 		 * Resolved 32-bit PLT entry format (b+-2mb):
128*7c478bd9Sstevel@tonic-gate 		 * .PLT
129*7c478bd9Sstevel@tonic-gate 		 * 0	nop
130*7c478bd9Sstevel@tonic-gate 		 * 1	ba,a,pt	%icc, <dest>
131*7c478bd9Sstevel@tonic-gate 		 * 2	nop
132*7c478bd9Sstevel@tonic-gate 		 * 3	nop
133*7c478bd9Sstevel@tonic-gate 		 */
134*7c478bd9Sstevel@tonic-gate 		rpi->pi_skip_method = RD_RESOLVE_STEP;
135*7c478bd9Sstevel@tonic-gate 		rpi->pi_nstep = 2;
136*7c478bd9Sstevel@tonic-gate 		rpi->pi_target = 0;
137*7c478bd9Sstevel@tonic-gate 		if (rtld_db_version >= RD_VERSION3) {
138*7c478bd9Sstevel@tonic-gate 			uint_t	d19;
139*7c478bd9Sstevel@tonic-gate 			d19 = instr[1] & S_MASK(22);
140*7c478bd9Sstevel@tonic-gate 			destaddr = ((int)pltaddr + 4) +
141*7c478bd9Sstevel@tonic-gate 				(((int)d19 << 13) >> 11);
142*7c478bd9Sstevel@tonic-gate 			pltbound++;
143*7c478bd9Sstevel@tonic-gate 		}
144*7c478bd9Sstevel@tonic-gate 	} else
145*7c478bd9Sstevel@tonic-gate 		rpi->pi_skip_method = RD_RESOLVE_NONE;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	if ((rtld_db_version >= RD_VERSION3) && pltbound) {
148*7c478bd9Sstevel@tonic-gate 		rpi->pi_flags |= RD_FLG_PI_PLTBOUND;
149*7c478bd9Sstevel@tonic-gate 		rpi->pi_baddr = destaddr;
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 	return (RD_OK);
152*7c478bd9Sstevel@tonic-gate }
153