ppc_reloc.c revision 1.23 1 /* $NetBSD: ppc_reloc.c,v 1.23 2002/09/12 20:21:01 mycroft 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 <sys/stat.h>
36 #include <machine/cpu.h>
37
38 #include "debug.h"
39 #include "rtld.h"
40
41 caddr_t _rtld_bind_powerpc __P((const Obj_Entry *, Elf_Word));
42 void _rtld_powerpc_pltcall __P((Elf_Word));
43 void _rtld_powerpc_pltresolve __P((Elf_Word, Elf_Word));
44
45 #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
46 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
47 #define l(x) ((u_int32_t)(x) & 0xffff)
48
49 void _rtld_bind_start(void);
50 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
51
52 /*
53 * Bind a pltgot slot indexed by reloff.
54 */
55 caddr_t
56 _rtld_bind_powerpc(obj, reloff)
57 const Obj_Entry *obj;
58 Elf_Word reloff;
59 {
60 const Elf_Rela *rela;
61 caddr_t addr;
62
63 if (reloff < 0 || reloff >= 0x8000) {
64 dbg(("_rtld_bind_powerpc: broken reloff %x", reloff));
65 _rtld_die();
66 }
67
68 rela = obj->pltrela + reloff;
69
70 if (_rtld_relocate_plt_object(obj, rela, &addr, true) < 0)
71 _rtld_die();
72
73 return addr;
74 }
75
76 int
77 _rtld_relocate_plt_object(
78 const Obj_Entry *obj,
79 const Elf_Rela *rela,
80 caddr_t *addrp,
81 bool dodebug)
82 {
83 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
84 int distance;
85
86 const Elf_Sym *def;
87 const Obj_Entry *defobj;
88 Elf_Addr value;
89
90 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
91
92 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
93 true);
94 if (def == NULL)
95 return (-1);
96
97 value = (Elf_Addr)(defobj->relocbase + def->st_value);
98 distance = value - (Elf_Addr)where;
99
100 if (abs(distance) < 32*1024*1024) { /* inside 32MB? */
101 /* b value # branch directly */
102 *where = 0x48000000 | (distance & 0x03fffffc);
103 __syncicache(where, 4);
104 } else {
105 Elf_Addr *pltcall, *jmptab;
106 int N = obj->pltrelalim - obj->pltrela;
107 int reloff = rela - obj->pltrela;
108
109 if (reloff < 0 || reloff >= 0x8000)
110 return (-1);
111
112 pltcall = obj->pltgot;
113
114 jmptab = pltcall + 18 + N * 2;
115 jmptab[reloff] = value;
116
117 distance = (Elf_Addr)pltcall - (Elf_Addr)(where + 1);
118
119 /* li r11,reloff */
120 /* b pltcall # use pltcall routine */
121 where[0] = 0x39600000 | reloff;
122 where[1] = 0x48000000 | (distance & 0x03fffffc);
123 __syncicache(where, 8);
124 }
125
126 *addrp = (caddr_t)value;
127 return (0);
128 }
129
130 /*
131 * Setup the plt glue routines.
132 */
133 #define PLTCALL_SIZE 20
134 #define PLTRESOLVE_SIZE 24
135
136 void
137 _rtld_setup_pltgot(obj)
138 const Obj_Entry *obj;
139 {
140 Elf_Word *pltcall, *pltresolve;
141 Elf_Word *jmptab;
142 int N = obj->pltrelalim - obj->pltrela;
143
144 pltcall = obj->pltgot;
145
146 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
147 jmptab = pltcall + 18 + N * 2;
148 pltcall[1] |= ha(jmptab);
149 pltcall[2] |= l(jmptab);
150
151 pltresolve = obj->pltgot + 8;
152
153 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
154 pltresolve[0] |= ha(_rtld_bind_start);
155 pltresolve[1] |= l(_rtld_bind_start);
156 pltresolve[3] |= ha(obj);
157 pltresolve[4] |= l(obj);
158
159 __syncicache(pltcall, 72 + N * 8);
160 }
161
162 void
163 _rtld_relocate_nonplt_self(dynp, relocbase)
164 Elf_Dyn *dynp;
165 Elf_Addr relocbase;
166 {
167 const Elf_Rela *rela = 0, *relalim;
168 Elf_Addr relasz = 0;
169 Elf_Addr *where;
170
171 for (; dynp->d_tag != DT_NULL; dynp++) {
172 switch (dynp->d_tag) {
173 case DT_RELA:
174 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
175 break;
176 case DT_RELASZ:
177 relasz = dynp->d_un.d_val;
178 break;
179 }
180 }
181 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
182 for (; rela < relalim; rela++) {
183 where = (Elf_Addr *)(relocbase + rela->r_offset);
184 *where = (Elf_Addr)(relocbase + rela->r_addend);
185 }
186 }
187
188 int
189 _rtld_relocate_nonplt_objects(obj, self, dodebug)
190 const Obj_Entry *obj;
191 bool self;
192 bool dodebug;
193 {
194 const Elf_Rela *rela;
195
196 if (self)
197 return 0;
198
199 for (rela = obj->rela; rela < obj->relalim; rela++) {
200 Elf_Addr *where;
201 const Elf_Sym *def;
202 const Obj_Entry *defobj;
203 Elf_Addr tmp;
204 unsigned long symnum;
205
206 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
207 symnum = ELF_R_SYM(rela->r_info);
208
209 switch (ELF_R_TYPE(rela->r_info)) {
210 case R_TYPE(NONE):
211 break;
212
213 case R_TYPE(32): /* word32 S + A */
214 case R_TYPE(GLOB_DAT): /* word32 S + A */
215 def = _rtld_find_symdef(symnum, obj, &defobj, false);
216 if (def == NULL)
217 return -1;
218
219 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
220 rela->r_addend);
221 if (*where != tmp)
222 *where = tmp;
223 rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
224 obj->strtab + obj->symtab[symnum].st_name,
225 obj->path, (void *)*where, defobj->path));
226 break;
227
228 case R_TYPE(RELATIVE): /* word32 B + A */
229 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
230 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
231 (void *)*where));
232 break;
233
234 case R_TYPE(COPY):
235 /*
236 * These are deferred until all other relocations have
237 * been done. All we do here is make sure that the
238 * COPY relocation is not in a shared library. They
239 * are allowed only in executable files.
240 */
241 if (obj->isdynamic) {
242 _rtld_error(
243 "%s: Unexpected R_COPY relocation in shared library",
244 obj->path);
245 return -1;
246 }
247 rdbg(dodebug, ("COPY (avoid in main)"));
248 break;
249
250 default:
251 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
252 "addend = %p, contents = %p, symbol = %s",
253 symnum, (u_long)ELF_R_TYPE(rela->r_info),
254 (void *)rela->r_offset, (void *)rela->r_addend,
255 (void *)*where,
256 obj->strtab + obj->symtab[symnum].st_name));
257 _rtld_error("%s: Unsupported relocation type %ld "
258 "in non-PLT relocations\n",
259 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
260 return -1;
261 }
262 }
263 return 0;
264 }
265
266 int
267 _rtld_relocate_plt_lazy(obj, dodebug)
268 const Obj_Entry *obj;
269 bool dodebug;
270 {
271 const Elf_Rela *rela;
272
273 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
274 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
275 int distance;
276 Elf_Addr *pltresolve;
277 int reloff = rela - obj->pltrela;
278
279 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
280
281 if (reloff < 0 || reloff >= 0x8000)
282 return (-1);
283
284 pltresolve = obj->pltgot + 8;
285
286 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
287
288 /* li r11,reloff */
289 /* b pltresolve */
290 where[0] = 0x39600000 | reloff;
291 where[1] = 0x48000000 | (distance & 0x03fffffc);
292 /* __syncicache(where, 8); */
293 }
294
295 return 0;
296 }
297