17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*5aefb655Srie  * Common Development and Distribution License (the "License").
6*5aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*5aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
23*5aefb655Srie  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include	<libelf.h>
287c478bd9Sstevel@tonic-gate #include	<sys/reg.h>
297c478bd9Sstevel@tonic-gate #include	<rtld_db.h>
307c478bd9Sstevel@tonic-gate #include	"_rtld_db.h"
317c478bd9Sstevel@tonic-gate #include	"msg.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * On amd64, basically, a PLT entry looks like this:
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *	0x00  ff 25 00 00 00 00  jmpq   *func@got(%rip)  ; jmp GOT[N]
387c478bd9Sstevel@tonic-gate  *	0x06  68 01 00 00 00     pushq  $0x1	       ; push index
397c478bd9Sstevel@tonic-gate  *	0x0b  e9 00 00 00 00     jmpq   .plt0	       ; jmp plt[0]
407c478bd9Sstevel@tonic-gate  *	0x10  ...
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *  The first time around GOT[N] contains address of pushq; this forces
437c478bd9Sstevel@tonic-gate  *	first time resolution to go thru PLT's first entry (which is a call)
447c478bd9Sstevel@tonic-gate  *  The nth time around, the GOT[N] actually contains the resolved
457c478bd9Sstevel@tonic-gate  *	address of the symbol(name), so the jmp is direct
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate /* ARGSUSED 3 */
487c478bd9Sstevel@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)497c478bd9Sstevel@tonic-gate plt64_resolution(rd_agent_t *rap, psaddr_t pc, lwpid_t lwpid,
507c478bd9Sstevel@tonic-gate 	psaddr_t pltbase, rd_plt_info_t *rpi)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	uint32_t	pcrel;
537c478bd9Sstevel@tonic-gate 	psaddr_t	destaddr;
547c478bd9Sstevel@tonic-gate 	psaddr_t	pltoff, pltaddr;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (rtld_db_version >= RD_VERSION3) {
587c478bd9Sstevel@tonic-gate 		rpi->pi_flags = 0;
597c478bd9Sstevel@tonic-gate 		rpi->pi_baddr = 0;
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	pltoff = pc - pltbase;
637c478bd9Sstevel@tonic-gate 	pltaddr = pltbase +
647c478bd9Sstevel@tonic-gate 		((pltoff / M_PLT_ENTSIZE) * M_PLT_ENTSIZE);
657c478bd9Sstevel@tonic-gate 	/*
667c478bd9Sstevel@tonic-gate 	 * This is the target of the jmp instruction
677c478bd9Sstevel@tonic-gate 	 */
687c478bd9Sstevel@tonic-gate 	if (ps_pread(rap->rd_psp, pltaddr + 2, (char *)&pcrel,
697c478bd9Sstevel@tonic-gate 	    sizeof (pcrel)) != PS_OK) {
707c478bd9Sstevel@tonic-gate 		LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_2), EC_ADDR(pltaddr + 2)));
717c478bd9Sstevel@tonic-gate 		return (RD_ERR);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/*
757c478bd9Sstevel@tonic-gate 	 * the offset to the GOT table entry is
767c478bd9Sstevel@tonic-gate 	 * PC-relative.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	destaddr = pcrel + pltaddr + 6;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/*
817c478bd9Sstevel@tonic-gate 	 * Find out what's pointed to by @OFFSET_INTO_GOT
827c478bd9Sstevel@tonic-gate 	 */
837c478bd9Sstevel@tonic-gate 	if (ps_pread(rap->rd_psp, destaddr, (char *)&destaddr,
847c478bd9Sstevel@tonic-gate 	    sizeof (destaddr)) != PS_OK) {
857c478bd9Sstevel@tonic-gate 		LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_2), EC_ADDR(destaddr)));
867c478bd9Sstevel@tonic-gate 		return (RD_ERR);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	if (destaddr == (pltaddr + 6)) {
897c478bd9Sstevel@tonic-gate 		rd_err_e	rerr;
907c478bd9Sstevel@tonic-gate 		/*
917c478bd9Sstevel@tonic-gate 		 * If GOT[ind] points to PLT+6 then this is the first
927c478bd9Sstevel@tonic-gate 		 * time through this PLT.
937c478bd9Sstevel@tonic-gate 		 */
947c478bd9Sstevel@tonic-gate 		if ((rerr = rd_binder_exit_addr(rap, MSG_ORIG(MSG_SYM_RTBIND),
957c478bd9Sstevel@tonic-gate 		    &(rpi->pi_target))) != RD_OK) {
967c478bd9Sstevel@tonic-gate 			return (rerr);
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 		rpi->pi_skip_method = RD_RESOLVE_TARGET_STEP;
997c478bd9Sstevel@tonic-gate 		rpi->pi_nstep = 1;
1007c478bd9Sstevel@tonic-gate 	} else {
1017c478bd9Sstevel@tonic-gate 		/*
1027c478bd9Sstevel@tonic-gate 		 * This is the n'th time through and GOT[ind] points
1037c478bd9Sstevel@tonic-gate 		 * to the final destination.
1047c478bd9Sstevel@tonic-gate 		 */
1057c478bd9Sstevel@tonic-gate 		rpi->pi_skip_method = RD_RESOLVE_STEP;
1067c478bd9Sstevel@tonic-gate 		rpi->pi_nstep = 1;
1077c478bd9Sstevel@tonic-gate 		rpi->pi_target = 0;
1087c478bd9Sstevel@tonic-gate 		if (rtld_db_version >= RD_VERSION3) {
1097c478bd9Sstevel@tonic-gate 			rpi->pi_flags |= RD_FLG_PI_PLTBOUND;
1107c478bd9Sstevel@tonic-gate 			rpi->pi_baddr = destaddr;
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	return (RD_OK);
1157c478bd9Sstevel@tonic-gate }
116