1/*
2 * CDDL HEADER START
3 *
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
7 * with the License.
8 *
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.
13 *
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]
19 *
20 * CDDL HEADER END
21 */
22/*
23 *	Copyright (c) 1991,1992 by Sun Microsystems, Inc.
24 */
25#pragma ident	"%Z%%M%	%I%	%E% SMI"
26
27#include	"machdep.h"
28#if	defined(lint)
29#include	<sys/types.h>
30#include	"sgs.h"
31#include	"_a.out.h"
32#else
33
34	.file	"boot_a.out.s"
35	.seg	".text"
36#endif
37
38/*
39 * We got here because the initial call to a function resolved to a procedure
40 * linkage table entry.  That entry did a branch to the first PLT entry, which
41 * in turn did a call to aout_rtbndr (refer aout_plt_init()).
42 *
43 * the code sequence that got us here was:
44 *
45 * PLT entry for foo():
46 *	save	%sp, -0x60, %sp			! patched first
47 *	call	.PLT0				! patched second
48 *	sethi	%hi(XXXXXXX), %g0		! unchanged
49 *
50 * Therefore on entry, %i7 has the address of the call, which will be added
51 * to the offset to the plt entry in %g1 to calculate the plt entry address
52 * we must also subtract 4 for because the address of PLT0 points to the
53 * save instruction before the call
54 *
55 * the plt entry is rewritten:
56 *
57 * PLT entry for foo():
58 *	sethi	%hi(entry_pt), %g1
59 *	jmpl	%g1 + %lo(entry_pt), %g0
60 */
61
62#if	defined(lint)
63
64void
65aout_rtbndr(caddr_t pc)
66{
67	(void) aout_bndr(pc);
68}
69
70#else
71	.global	aout_rtbndr
72	.type   aout_rtbndr, #function
73	.align	4
74
75aout_rtbndr:
76	save	%sp, -80, %sp
77	call	aout_bndr		! returns function address in %o0
78	add	%i7, -0x4, %o0		! %o0 now has address of PLT0
79	mov	%o0, %g1		! save address of routine binded
80	restore				! how many restores needed ? 2
81	jmp	%g1			! jump to it
82	restore
83	nop
84	.size	aout_rtbndr, . - aout_rtbndr
85
86#endif
87
88
89/*
90 * After the first call to a plt, aout_bndr() will have determined the true
91 * address of the function being bound.  The plt is now rewritten so that
92 * any subsequent calls go directly to the bound function.
93 *
94 * the new plt entry is:
95 *
96 *	sethi	%hi(function address), %g1	! patched first
97 *	jmpl	%g1 + %lo(function address, %g0	! patched second
98 */
99
100#if	defined(lint)
101
102void
103aout_plt_write(caddr_t pc, unsigned long symval)
104{
105	/* LINTED */
106	*(unsigned long *)(pc) = (M_SETHIG1 | (symval >> (32 - 22)));
107	/* LINTED */
108	*(unsigned long *)(pc + 4) = (M_JMPL | (symval & S_MASK(10)));
109
110}
111
112#else
113	.global	aout_plt_write
114	.type	aout_plt_write, #function
115	.align	4
116
117aout_plt_write:
118	srl	%o1, 10, %o2		! Get high part of function address
119	sethi	%hi(M_SETHIG1), %o3	! Get sethi instruction
120	or	%o3, %o2, %o3		! Add sethi and function address
121	st	%o3, [%o0]		! Store instruction in plt[0]
122	iflush  %o0
123	stbar
124	sethi	%hi(M_JMPL), %o3	! Get jmpl instruction
125	and	%o1, 0x3ff, %o2		! Lower part of function address
126	or	%o3, %o2, %o3		!	is or'ed into instruction
127	st	%o3, [%o0 + 4]		! Store instruction in plt[1]
128	retl
129	iflush	%o0 + 4
130	.size	aout_plt_write, . - aout_plt_write
131
132#endif
133