1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include	<proc_service.h>
30*0Sstevel@tonic-gate #include	<link.h>
31*0Sstevel@tonic-gate #include	<rtld_db.h>
32*0Sstevel@tonic-gate #include	<_rtld_db.h>
33*0Sstevel@tonic-gate #include	<msg.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate  * A un-initialized SPARCV9 PLT look like so:
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  * .PLT
39*0Sstevel@tonic-gate  *	sethi	(. - .PLT0), %g1
40*0Sstevel@tonic-gate  *	ba,a	%xcc, .PLT1
41*0Sstevel@tonic-gate  *	nop
42*0Sstevel@tonic-gate  *	nop
43*0Sstevel@tonic-gate  *	nop
44*0Sstevel@tonic-gate  *	nop
45*0Sstevel@tonic-gate  *	nop
46*0Sstevel@tonic-gate  *	nop
47*0Sstevel@tonic-gate  *
48*0Sstevel@tonic-gate  * To test to see if this is an uninitialized PLT we check
49*0Sstevel@tonic-gate  * the second instruction and confirm that it's a branch.
50*0Sstevel@tonic-gate  */
51*0Sstevel@tonic-gate /* ARGSUSED 2 */
52*0Sstevel@tonic-gate rd_err_e
plt64_resolution(rd_agent_t * rap,psaddr_t pc,lwpid_t lwpid,psaddr_t pltbase,rd_plt_info_t * rpi)53*0Sstevel@tonic-gate plt64_resolution(rd_agent_t *rap, psaddr_t pc, lwpid_t lwpid,
54*0Sstevel@tonic-gate 	psaddr_t pltbase, rd_plt_info_t *rpi)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate 	instr_t		instr[8];
57*0Sstevel@tonic-gate 	rd_err_e	rerr;
58*0Sstevel@tonic-gate 	psaddr_t	destaddr = 0;
59*0Sstevel@tonic-gate 	psaddr_t	pltoff;
60*0Sstevel@tonic-gate 	int		pltbound = 0;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	if (rtld_db_version >= RD_VERSION3) {
63*0Sstevel@tonic-gate 		rpi->pi_flags = 0;
64*0Sstevel@tonic-gate 		rpi->pi_baddr = 0;
65*0Sstevel@tonic-gate 	}
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	pltoff = pc - pltbase;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if (pltoff >= (M64_PLT_NEARPLTS * M64_PLT_ENTSIZE)) {
70*0Sstevel@tonic-gate 		psaddr_t	pltptr, pltptrval;
71*0Sstevel@tonic-gate 		psaddr_t	pltaddr;
72*0Sstevel@tonic-gate 		psaddr_t	pltblockoff;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 		/*
75*0Sstevel@tonic-gate 		 * Handle far PLT's
76*0Sstevel@tonic-gate 		 *
77*0Sstevel@tonic-gate 		 * .PLT#
78*0Sstevel@tonic-gate 		 * 0	mov	%o7, %g5
79*0Sstevel@tonic-gate 		 * 1	call	. + 8
80*0Sstevel@tonic-gate 		 * 2	nop
81*0Sstevel@tonic-gate 		 * 3	ldx	[%o7 + (.PLTP# - .PLT#+4)], %g1
82*0Sstevel@tonic-gate 		 * 4	jmpl	%o7 + %g1, %g1
83*0Sstevel@tonic-gate 		 * 5	mov	%g5, %o7
84*0Sstevel@tonic-gate 		 */
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 		pltblockoff = pltoff - (M64_PLT_NEARPLTS * M64_PLT_ENTSIZE);
87*0Sstevel@tonic-gate 		pltblockoff =
88*0Sstevel@tonic-gate 			((pltblockoff / M64_PLT_FBLOCKSZ) * M64_PLT_FBLOCKSZ) +
89*0Sstevel@tonic-gate 			(((pltblockoff % M64_PLT_FBLOCKSZ) / M64_PLT_FENTSIZE) *
90*0Sstevel@tonic-gate 			M64_PLT_FENTSIZE);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 		pltaddr = pltbase + (M64_PLT_NEARPLTS * M64_PLT_ENTSIZE) +
94*0Sstevel@tonic-gate 			pltblockoff;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 		if (ps_pread(rap->rd_psp, pltaddr, (char *)instr,
97*0Sstevel@tonic-gate 		    M64_PLT_FENTSIZE) != PS_OK) {
98*0Sstevel@tonic-gate 			LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_2),
99*0Sstevel@tonic-gate 				EC_ADDR(pltaddr)));
100*0Sstevel@tonic-gate 			return (RD_ERR);
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 		}
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 		if (instr[0] != M_MOVO7TOG5) {
105*0Sstevel@tonic-gate 			LOG(ps_plog(MSG_ORIG(MSG_DB_BADFPLT),
106*0Sstevel@tonic-gate 				EC_ADDR(pltaddr), EC_ADDR(instr[0])));
107*0Sstevel@tonic-gate 			return (RD_ERR);
108*0Sstevel@tonic-gate 		}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 		/*
111*0Sstevel@tonic-gate 		 * the offset is a positive displacement from the
112*0Sstevel@tonic-gate 		 * ldx [%o7 + #], %g1 instruction.  So - we don't
113*0Sstevel@tonic-gate 		 * need to worry about the sign bit :)
114*0Sstevel@tonic-gate 		 */
115*0Sstevel@tonic-gate 		pltptr = instr[3] & S_MASK(12);
116*0Sstevel@tonic-gate 		pltptr += pltaddr + 4;
117*0Sstevel@tonic-gate 		/*
118*0Sstevel@tonic-gate 		 * Load the pltptr to determine whether it is
119*0Sstevel@tonic-gate 		 * pointing to .PLT0 or to the final
120*0Sstevel@tonic-gate 		 * destination.
121*0Sstevel@tonic-gate 		 */
122*0Sstevel@tonic-gate 		if (ps_pread(rap->rd_psp, pltptr, &pltptrval,
123*0Sstevel@tonic-gate 		    sizeof (long long)) != PS_OK) {
124*0Sstevel@tonic-gate 			LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_2),
125*0Sstevel@tonic-gate 				EC_ADDR(pltptr)));
126*0Sstevel@tonic-gate 			return (RD_ERR);
127*0Sstevel@tonic-gate 		}
128*0Sstevel@tonic-gate 		pltptrval += pltaddr + 4;
129*0Sstevel@tonic-gate 		if (pltptrval == pltbase) {
130*0Sstevel@tonic-gate 			if ((rerr = rd_binder_exit_addr(rap,
131*0Sstevel@tonic-gate 			    MSG_ORIG(MSG_SYM_RTBIND),
132*0Sstevel@tonic-gate 			    &(rpi->pi_target))) != RD_OK) {
133*0Sstevel@tonic-gate 				return (rerr);
134*0Sstevel@tonic-gate 			}
135*0Sstevel@tonic-gate 			rpi->pi_skip_method = RD_RESOLVE_TARGET_STEP;
136*0Sstevel@tonic-gate 			rpi->pi_nstep = 1;
137*0Sstevel@tonic-gate 		} else {
138*0Sstevel@tonic-gate 			rpi->pi_skip_method = RD_RESOLVE_STEP;
139*0Sstevel@tonic-gate 			rpi->pi_nstep = 6;
140*0Sstevel@tonic-gate 			rpi->pi_target = 0;
141*0Sstevel@tonic-gate 			if (rtld_db_version >= RD_VERSION3) {
142*0Sstevel@tonic-gate 				destaddr = pltptrval;
143*0Sstevel@tonic-gate 				pltbound++;
144*0Sstevel@tonic-gate 			}
145*0Sstevel@tonic-gate 		}
146*0Sstevel@tonic-gate 	} else  {
147*0Sstevel@tonic-gate 		psaddr_t	pltaddr;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		pltaddr = pltbase +
150*0Sstevel@tonic-gate 			((pltoff / M64_PLT_ENTSIZE) * M64_PLT_ENTSIZE);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 		if (ps_pread(rap->rd_psp, pltaddr, (char *)instr,
153*0Sstevel@tonic-gate 		    M64_PLT_ENTSIZE) != PS_OK) {
154*0Sstevel@tonic-gate 			LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_2),
155*0Sstevel@tonic-gate 				EC_ADDR(pltaddr)));
156*0Sstevel@tonic-gate 			return (RD_ERR);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 		}
159*0Sstevel@tonic-gate 		/*
160*0Sstevel@tonic-gate 		 * ELF64 NEAR PLT's
161*0Sstevel@tonic-gate 		 */
162*0Sstevel@tonic-gate 		if ((instr[0] != M_NOP) &&
163*0Sstevel@tonic-gate 		    ((instr[1] & (~(S_MASK(19)))) == M_BA_A_XCC)) {
164*0Sstevel@tonic-gate 			/*
165*0Sstevel@tonic-gate 			 * Unbound PLT
166*0Sstevel@tonic-gate 			 */
167*0Sstevel@tonic-gate 			if ((rerr = rd_binder_exit_addr(rap,
168*0Sstevel@tonic-gate 			    MSG_ORIG(MSG_SYM_RTBIND),
169*0Sstevel@tonic-gate 			    &(rpi->pi_target))) != RD_OK) {
170*0Sstevel@tonic-gate 				return (rerr);
171*0Sstevel@tonic-gate 			}
172*0Sstevel@tonic-gate 			rpi->pi_skip_method = RD_RESOLVE_TARGET_STEP;
173*0Sstevel@tonic-gate 			rpi->pi_nstep = 1;
174*0Sstevel@tonic-gate 		} else  if ((instr[0] == M_NOP) &&
175*0Sstevel@tonic-gate 		    ((instr[1] & (~(S_MASK(22)))) == M_BA_A)) {
176*0Sstevel@tonic-gate 			/*
177*0Sstevel@tonic-gate 			 * Resolved 64-bit PLT entry format (b+-8mb):
178*0Sstevel@tonic-gate 			 * .PLT
179*0Sstevel@tonic-gate 			 * 0	nop
180*0Sstevel@tonic-gate 			 * 1	ba,a	<dest>
181*0Sstevel@tonic-gate 			 * 2	nop
182*0Sstevel@tonic-gate 			 * 3	nop
183*0Sstevel@tonic-gate 			 * 4	nop
184*0Sstevel@tonic-gate 			 * 5	nop
185*0Sstevel@tonic-gate 			 * 6	nop
186*0Sstevel@tonic-gate 			 * 7	nop
187*0Sstevel@tonic-gate 			 */
188*0Sstevel@tonic-gate 			rpi->pi_skip_method = RD_RESOLVE_STEP;
189*0Sstevel@tonic-gate 			rpi->pi_nstep = 2;
190*0Sstevel@tonic-gate 			rpi->pi_target = 0;
191*0Sstevel@tonic-gate 			if (rtld_db_version >= RD_VERSION3) {
192*0Sstevel@tonic-gate 				uint_t	d22;
193*0Sstevel@tonic-gate 				d22 = instr[1] & S_MASK(22);
194*0Sstevel@tonic-gate 				/* LINTED */
195*0Sstevel@tonic-gate 				destaddr = ((long)pltaddr + 4) +
196*0Sstevel@tonic-gate 					/* LINTED */
197*0Sstevel@tonic-gate 					(((int)d22 << 10) >> 8);
198*0Sstevel@tonic-gate 				pltbound++;
199*0Sstevel@tonic-gate 			}
200*0Sstevel@tonic-gate 		} else if ((instr[0] == M_NOP) &&
201*0Sstevel@tonic-gate 		    ((instr[1] & (~(S_MASK(19)))) == M_BA_A_PT)) {
202*0Sstevel@tonic-gate 			/*
203*0Sstevel@tonic-gate 			 * Resolved 64-bit PLT entry format (b+-2mb):
204*0Sstevel@tonic-gate 			 * .PLT
205*0Sstevel@tonic-gate 			 * 0	nop
206*0Sstevel@tonic-gate 			 * 1	ba,a,pt	%icc, <dest>
207*0Sstevel@tonic-gate 			 * 2	nop
208*0Sstevel@tonic-gate 			 * 3	nop
209*0Sstevel@tonic-gate 			 * 4	nop
210*0Sstevel@tonic-gate 			 * 5	nop
211*0Sstevel@tonic-gate 			 * 6	nop
212*0Sstevel@tonic-gate 			 * 7	nop
213*0Sstevel@tonic-gate 			 */
214*0Sstevel@tonic-gate 			rpi->pi_skip_method = RD_RESOLVE_STEP;
215*0Sstevel@tonic-gate 			rpi->pi_nstep = 2;
216*0Sstevel@tonic-gate 			rpi->pi_target = 0;
217*0Sstevel@tonic-gate 			if (rtld_db_version >= RD_VERSION3) {
218*0Sstevel@tonic-gate 				uint_t	d19;
219*0Sstevel@tonic-gate 				d19 = instr[1] & S_MASK(22);
220*0Sstevel@tonic-gate 				/* LINTED */
221*0Sstevel@tonic-gate 				destaddr = ((long)pltaddr + 4) +
222*0Sstevel@tonic-gate 					/* LINTED */
223*0Sstevel@tonic-gate 					(((int)d19 << 13) >> 11);
224*0Sstevel@tonic-gate 				pltbound++;
225*0Sstevel@tonic-gate 			}
226*0Sstevel@tonic-gate 		} else if ((instr[6] & (~(S_MASK(13)))) == M_JMPL_G5G0) {
227*0Sstevel@tonic-gate 			/*
228*0Sstevel@tonic-gate 			 * Resolved 64-bit PLT entry format (abs-64):
229*0Sstevel@tonic-gate 			 * .PLT
230*0Sstevel@tonic-gate 			 * 0	nop
231*0Sstevel@tonic-gate 			 * 1	sethi	%hh(dest), %g1
232*0Sstevel@tonic-gate 			 * 2	sethi	%lm(dest), %g5
233*0Sstevel@tonic-gate 			 * 3	or	%g1, %hm(dest), %g1
234*0Sstevel@tonic-gate 			 * 4	sllx	%g1, 32, %g1
235*0Sstevel@tonic-gate 			 * 5	or	%g1, %g5, %g5
236*0Sstevel@tonic-gate 			 * 6	jmpl	%g5 + %lo(dest), %g0
237*0Sstevel@tonic-gate 			 * 7	nop
238*0Sstevel@tonic-gate 			 */
239*0Sstevel@tonic-gate 			rpi->pi_skip_method = RD_RESOLVE_STEP;
240*0Sstevel@tonic-gate 			rpi->pi_nstep = 8;
241*0Sstevel@tonic-gate 			rpi->pi_target = 0;
242*0Sstevel@tonic-gate 			if (rtld_db_version >= RD_VERSION3) {
243*0Sstevel@tonic-gate 				uintptr_t	hh_bits;
244*0Sstevel@tonic-gate 				uintptr_t	hm_bits;
245*0Sstevel@tonic-gate 				uintptr_t	lm_bits;
246*0Sstevel@tonic-gate 				uintptr_t	lo_bits;
247*0Sstevel@tonic-gate 				hh_bits = instr[1] & S_MASK(22); /* 63..42 */
248*0Sstevel@tonic-gate 				hm_bits = instr[3] & S_MASK(10); /* 41..32 */
249*0Sstevel@tonic-gate 				lm_bits = instr[2] & S_MASK(22); /* 31..10 */
250*0Sstevel@tonic-gate 				lo_bits = instr[6] & S_MASK(10); /* 09..00 */
251*0Sstevel@tonic-gate 				destaddr = (hh_bits << 42) | (hm_bits << 32) |
252*0Sstevel@tonic-gate 					(lm_bits << 10) | lo_bits;
253*0Sstevel@tonic-gate 				pltbound++;
254*0Sstevel@tonic-gate 			}
255*0Sstevel@tonic-gate 		} else if (instr[3] == M_JMPL) {
256*0Sstevel@tonic-gate 			/*
257*0Sstevel@tonic-gate 			 * Resolved 64-bit PLT entry format (top-32):
258*0Sstevel@tonic-gate 			 *
259*0Sstevel@tonic-gate 			 * .PLT:
260*0Sstevel@tonic-gate 			 * 0	nop
261*0Sstevel@tonic-gate 			 * 1	sethi	%hi(~dest), %g5
262*0Sstevel@tonic-gate 			 * 2	xnor	%g5, %lo(~dest), %g1
263*0Sstevel@tonic-gate 			 * 3	jmpl	%g1, %g0
264*0Sstevel@tonic-gate 			 * 4	nop
265*0Sstevel@tonic-gate 			 * 5	nop
266*0Sstevel@tonic-gate 			 * 6	nop
267*0Sstevel@tonic-gate 			 * 7	nop
268*0Sstevel@tonic-gate 			 */
269*0Sstevel@tonic-gate 			rpi->pi_skip_method = RD_RESOLVE_STEP;
270*0Sstevel@tonic-gate 			rpi->pi_nstep = 5;
271*0Sstevel@tonic-gate 			rpi->pi_target = 0;
272*0Sstevel@tonic-gate 			if (rtld_db_version >= RD_VERSION3) {
273*0Sstevel@tonic-gate 				uintptr_t hi_bits;
274*0Sstevel@tonic-gate 				uintptr_t lo_bits;
275*0Sstevel@tonic-gate 				hi_bits = (instr[1] & S_MASK(22)) << 10;
276*0Sstevel@tonic-gate 				lo_bits = (instr[2] & S_MASK(10));
277*0Sstevel@tonic-gate 				destaddr = hi_bits ^ ~lo_bits;
278*0Sstevel@tonic-gate 				pltbound++;
279*0Sstevel@tonic-gate 			}
280*0Sstevel@tonic-gate 		} else if ((instr[2] & (~(S_MASK(13)))) == M_XNOR_G5G1) {
281*0Sstevel@tonic-gate 			/*
282*0Sstevel@tonic-gate 			 * Resolved 64-bit PLT entry format (top-44):
283*0Sstevel@tonic-gate 			 *
284*0Sstevel@tonic-gate 			 * .PLT:
285*0Sstevel@tonic-gate 			 * 0	nop
286*0Sstevel@tonic-gate 			 * 1	sethi	%h44(~dest), %g5
287*0Sstevel@tonic-gate 			 * 2	xnor	%g5, %m44(~dest), %g1
288*0Sstevel@tonic-gate 			 * 3	slxx	%g1, 12, %g1
289*0Sstevel@tonic-gate 			 * 4	jmpl %g1 + %l44(dest), %g0
290*0Sstevel@tonic-gate 			 * 5	nop
291*0Sstevel@tonic-gate 			 * 6	nop
292*0Sstevel@tonic-gate 			 * 7	nop
293*0Sstevel@tonic-gate 			 */
294*0Sstevel@tonic-gate 			rpi->pi_skip_method = RD_RESOLVE_STEP;
295*0Sstevel@tonic-gate 			rpi->pi_nstep = 6;
296*0Sstevel@tonic-gate 			rpi->pi_target = 0;
297*0Sstevel@tonic-gate 			if (rtld_db_version >= RD_VERSION3) {
298*0Sstevel@tonic-gate 				uintptr_t h44_bits;
299*0Sstevel@tonic-gate 				uintptr_t	m44_bits;
300*0Sstevel@tonic-gate 				uintptr_t	l44_bits;
301*0Sstevel@tonic-gate 				h44_bits = (((long)instr[1] & S_MASK(22))
302*0Sstevel@tonic-gate 					<< 10);
303*0Sstevel@tonic-gate 				m44_bits = (((long)instr[2] & S_MASK(13))
304*0Sstevel@tonic-gate 					<< 41) >> 41;
305*0Sstevel@tonic-gate 				l44_bits = (((long)instr[4] & S_MASK(13))
306*0Sstevel@tonic-gate 					<< 41) >> 41;
307*0Sstevel@tonic-gate 				destaddr = (~(h44_bits ^ m44_bits) << 12)
308*0Sstevel@tonic-gate 					+ l44_bits;
309*0Sstevel@tonic-gate 				pltbound++;
310*0Sstevel@tonic-gate 			}
311*0Sstevel@tonic-gate 		} else
312*0Sstevel@tonic-gate 			rpi->pi_skip_method = RD_RESOLVE_NONE;
313*0Sstevel@tonic-gate 	}
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	if ((rtld_db_version >= RD_VERSION3) && pltbound) {
316*0Sstevel@tonic-gate 		rpi->pi_flags |= RD_FLG_PI_PLTBOUND;
317*0Sstevel@tonic-gate 		rpi->pi_baddr = destaddr;
318*0Sstevel@tonic-gate 	}
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	return (RD_OK);
321*0Sstevel@tonic-gate }
322