ppc_reloc.c revision 1.1 1 /* $NetBSD: ppc_reloc.c,v 1.1 1998/11/24 11:34:31 tsubai Exp $ */
2
3 /*-
4 * Copyright (C) 1998 Tsubai Masanari
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <machine/cpu.h>
36
37 #include "debug.h"
38 #include "rtld.h"
39
40 extern caddr_t _rtld_bind_powerpc(const Obj_Entry *, Elf_Word);
41 extern void _rtld_powerpc_pltcall(Elf_Word);
42 extern void _rtld_powerpc_pltresolve(Elf_Word, Elf_Word);
43
44 static Elf_Addr _rtld_bind_pltgot(const Obj_Entry *, const Elf_RelA *);
45
46 #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
47 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
48 #define l(x) ((u_int32_t)(x) & 0xffff)
49
50 /*
51 * Bind a pltgot slot indexed by reloff.
52 */
53 caddr_t
54 _rtld_bind_powerpc(obj, reloff)
55 const Obj_Entry *obj;
56 Elf_Word reloff;
57 {
58 Elf_Addr addr;
59 const Elf_RelA *rela;
60
61 if (reloff < 0 || reloff >= 0x8000) {
62 dbg("_rtld_bind_powerpc: broken reloff %x", reloff);
63 _rtld_die();
64 }
65
66 rela = obj->pltrela + reloff;
67 addr = _rtld_bind_pltgot(obj, rela);
68 if (addr == 0)
69 _rtld_die();
70
71 return (caddr_t)addr;
72 }
73
74 /*
75 * Make a pltgot slot.
76 * Initial value is "pltresolve" unless bind_now is true.
77 */
78 int
79 _rtld_reloc_powerpc_plt(
80 const Obj_Entry *obj,
81 const Elf_RelA *rela,
82 bool bind_now)
83 {
84 if (bind_now) {
85 if (_rtld_bind_pltgot(obj, rela) == 0)
86 return -1;
87 } else {
88 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
89 char *pltresolve = (void *)&obj->pltgot[8];
90 int index = rela - obj->pltrela;
91 int distance;
92
93 if (index < 0 || index >= 0x8000)
94 return -1;
95 distance = pltresolve - (char *)(where + 1);
96
97 /* li r11,index */
98 /* b pltresolve */
99 where[0] = 0x39600000 | index;
100 where[1] = 0x48000000 | (distance & 0x03fffffc);
101 /* syncicache(where, 8); */
102 }
103 return 0;
104 }
105
106 /*
107 * Bind a pltgot entry to the symbol value.
108 */
109 Elf_Addr
110 _rtld_bind_pltgot(obj, rela)
111 const Obj_Entry *obj;
112 const Elf_RelA *rela;
113 {
114 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
115 const Elf_Sym *def;
116 const Obj_Entry *defobj;
117 Elf_Addr targ_addr;
118 int distance;
119
120 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
121
122 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
123 &defobj, true);
124 dbg("sym='%s'", def ? defobj->strtab + def->st_name : "??");
125 if (def == NULL) {
126 dbg("symbol not found");
127 return 0;
128 }
129
130 targ_addr = (Elf_Addr)(defobj->relocbase + def->st_value);
131 distance = targ_addr - (Elf_Addr)where;
132
133 if (abs(distance) < 32*1024*1024) { /* inside 32MB? */
134 /* b targ_addr # branch directly */
135 *where = 0x48000000 | (distance & 0x03fffffc);
136 syncicache(where, 4);
137 } else {
138 Elf_Addr *pltcall, *jmptab;
139 int N = obj->pltrelalim - obj->pltrela;
140 int reloff = rela - obj->pltrela;
141
142 if (reloff < 0 || reloff >= 0x8000)
143 return 0;
144
145 pltcall = obj->pltgot;
146
147 jmptab = pltcall + 18 + N * 2;
148 jmptab[reloff] = targ_addr;
149
150 distance = (Elf_Addr)pltcall - (Elf_Addr)(where + 1);
151
152 /* li r11,reloff */
153 /* b pltcall # use pltcall routine */
154 where[0] = 0x39600000 | reloff;
155 where[1] = 0x48000000 | (distance & 0x03fffffc);
156 syncicache(where, 8);
157 }
158
159 return targ_addr;
160 }
161
162 /*
163 * Setup the plt glue routines.
164 */
165 #define PLTCALL_SIZE 20
166 #define PLTRESOLVE_SIZE 24
167
168 void
169 _rtld_setup_powerpc_plt(obj)
170 const Obj_Entry * obj;
171 {
172 Elf_Word *pltcall, *pltresolve;
173 Elf_Word *jmptab;
174 int N = obj->pltrelalim - obj->pltrela;
175
176 pltcall = obj->pltgot;
177
178 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
179
180 jmptab = pltcall + 18 + N * 2;
181 pltcall[1] |= ha(jmptab);
182 pltcall[2] |= l(jmptab);
183
184 pltresolve = &pltcall[8];
185
186 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
187 pltresolve[0] |= ha(_rtld_bind_start);
188 pltresolve[1] |= l(_rtld_bind_start);
189 pltresolve[3] |= ha(obj);
190 pltresolve[4] |= l(obj);
191
192 syncicache(pltcall, 72 + N * 8);
193 }
194