ppc_reloc.c revision 1.37 1 /* $NetBSD: ppc_reloc.c,v 1.37 2005/08/20 19:01:17 skrll Exp $ */
2
3 /*-
4 * Copyright (C) 1998 Tsubai Masanari
5 * Portions copyright 2002 Charles M. Hannum <root (at) ihack.net>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: ppc_reloc.c,v 1.37 2005/08/20 19:01:17 skrll Exp $");
34 #endif /* not lint */
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <machine/cpu.h>
43
44 #include "debug.h"
45 #include "rtld.h"
46
47 void _rtld_powerpc_pltcall(Elf_Word);
48 void _rtld_powerpc_pltresolve(Elf_Word, Elf_Word);
49
50 #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
51 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
52 #define l(x) ((u_int32_t)(x) & 0xffff)
53
54 void _rtld_bind_start(void);
55 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
56 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
57 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
58 const Elf_Rela *, int, Elf_Addr *);
59
60 /*
61 * Setup the plt glue routines.
62 */
63 #define PLTCALL_SIZE 20
64 #define PLTRESOLVE_SIZE 24
65
66 void
67 _rtld_setup_pltgot(const Obj_Entry *obj)
68 {
69 Elf_Word *pltcall, *pltresolve;
70 Elf_Word *jmptab;
71 int N = obj->pltrelalim - obj->pltrela;
72
73 /* Entries beyond 8192 take twice as much space. */
74 if (N > 8192)
75 N += N-8192;
76
77 pltcall = obj->pltgot;
78 jmptab = pltcall + 18 + N * 2;
79
80 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
81 pltcall[1] |= ha(jmptab);
82 pltcall[2] |= l(jmptab);
83
84 pltresolve = obj->pltgot + 8;
85
86 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
87 pltresolve[0] |= ha(_rtld_bind_start);
88 pltresolve[1] |= l(_rtld_bind_start);
89 pltresolve[3] |= ha(obj);
90 pltresolve[4] |= l(obj);
91
92 __syncicache(pltcall, 72 + N * 8);
93 }
94
95 void
96 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
97 {
98 const Elf_Rela *rela = 0, *relalim;
99 Elf_Addr relasz = 0;
100 Elf_Addr *where;
101
102 for (; dynp->d_tag != DT_NULL; dynp++) {
103 switch (dynp->d_tag) {
104 case DT_RELA:
105 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
106 break;
107 case DT_RELASZ:
108 relasz = dynp->d_un.d_val;
109 break;
110 }
111 }
112 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
113 for (; rela < relalim; rela++) {
114 where = (Elf_Addr *)(relocbase + rela->r_offset);
115 *where = (Elf_Addr)(relocbase + rela->r_addend);
116 }
117 }
118
119 int
120 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
121 {
122 const Elf_Rela *rela;
123
124 for (rela = obj->rela; rela < obj->relalim; rela++) {
125 Elf_Addr *where;
126 const Elf_Sym *def;
127 const Obj_Entry *defobj;
128 Elf_Addr tmp;
129 unsigned long symnum;
130
131 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
132 symnum = ELF_R_SYM(rela->r_info);
133
134 switch (ELF_R_TYPE(rela->r_info)) {
135 #if 1 /* XXX Should not be necessary. */
136 case R_TYPE(JMP_SLOT):
137 #endif
138 case R_TYPE(NONE):
139 break;
140
141 case R_TYPE(32): /* word32 S + A */
142 case R_TYPE(GLOB_DAT): /* word32 S + A */
143 def = _rtld_find_symdef(symnum, obj, &defobj, false);
144 if (def == NULL)
145 return -1;
146
147 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
148 rela->r_addend);
149 if (*where != tmp)
150 *where = tmp;
151 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
152 obj->strtab + obj->symtab[symnum].st_name,
153 obj->path, (void *)*where, defobj->path));
154 break;
155
156 case R_TYPE(RELATIVE): /* word32 B + A */
157 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
158 rdbg(("RELATIVE in %s --> %p", obj->path,
159 (void *)*where));
160 break;
161
162 case R_TYPE(COPY):
163 /*
164 * These are deferred until all other relocations have
165 * been done. All we do here is make sure that the
166 * COPY relocation is not in a shared library. They
167 * are allowed only in executable files.
168 */
169 if (obj->isdynamic) {
170 _rtld_error(
171 "%s: Unexpected R_COPY relocation in shared library",
172 obj->path);
173 return -1;
174 }
175 rdbg(("COPY (avoid in main)"));
176 break;
177
178 default:
179 rdbg(("sym = %lu, type = %lu, offset = %p, "
180 "addend = %p, contents = %p, symbol = %s",
181 symnum, (u_long)ELF_R_TYPE(rela->r_info),
182 (void *)rela->r_offset, (void *)rela->r_addend,
183 (void *)*where,
184 obj->strtab + obj->symtab[symnum].st_name));
185 _rtld_error("%s: Unsupported relocation type %ld "
186 "in non-PLT relocations\n",
187 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
188 return -1;
189 }
190 }
191 return 0;
192 }
193
194 int
195 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
196 {
197 const Elf_Rela *rela;
198 int reloff;
199
200 for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
201 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
202 int distance;
203 Elf_Addr *pltresolve;
204
205 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
206
207 pltresolve = obj->pltgot + 8;
208
209 if (reloff < 32768) {
210 /* li r11,reloff */
211 *where++ = 0x39600000 | reloff;
212 } else {
213 /* lis r11,ha(reloff) */
214 /* addi r11,l(reloff) */
215 *where++ = 0x3d600000 | ha(reloff);
216 *where++ = 0x396b0000 | l(reloff);
217 }
218 /* b pltresolve */
219 distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
220 *where++ = 0x48000000 | (distance & 0x03fffffc);
221 /* __syncicache(where - 12, 12); */
222 }
223
224 return 0;
225 }
226
227 static inline int
228 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
229 {
230 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
231 Elf_Addr value;
232 const Elf_Sym *def;
233 const Obj_Entry *defobj;
234 int distance;
235
236 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
237
238 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
239 if (def == NULL)
240 return -1;
241
242 value = (Elf_Addr)(defobj->relocbase + def->st_value);
243 distance = value - (Elf_Addr)where;
244 rdbg(("bind now/fixup in %s --> new=%p",
245 defobj->strtab + def->st_name, (void *)value));
246
247 if (abs(distance) < 32*1024*1024) { /* inside 32MB? */
248 /* b value # branch directly */
249 *where = 0x48000000 | (distance & 0x03fffffc);
250 __syncicache(where, 4);
251 } else {
252 Elf_Addr *pltcall, *jmptab;
253 int N = obj->pltrelalim - obj->pltrela;
254
255 /* Entries beyond 8192 take twice as much space. */
256 if (N > 8192)
257 N += N-8192;
258
259 pltcall = obj->pltgot;
260 jmptab = pltcall + 18 + N * 2;
261
262 jmptab[reloff] = value;
263
264 if (reloff < 32768) {
265 /* li r11,reloff */
266 *where++ = 0x39600000 | reloff;
267 } else {
268 /* lis r11,ha(reloff) */
269 /* addi r11,l(reloff) */
270 *where++ = 0x3d600000 | ha(reloff);
271 *where++ = 0x396b0000 | l(reloff);
272 }
273 /* b pltcall */
274 distance = (Elf_Addr)pltcall - (Elf_Addr)where;
275 *where++ = 0x48000000 | (distance & 0x03fffffc);
276 __syncicache(where - 12, 12);
277 }
278
279 if (tp)
280 *tp = value;
281 return 0;
282 }
283
284 caddr_t
285 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
286 {
287 const Elf_Rela *rela = obj->pltrela + reloff;
288 Elf_Addr new_value;
289 int err;
290
291 err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
292 if (err)
293 _rtld_die();
294
295 return (caddr_t)new_value;
296 }
297
298 int
299 _rtld_relocate_plt_objects(const Obj_Entry *obj)
300 {
301 const Elf_Rela *rela;
302 int reloff;
303
304 for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
305 if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
306 return -1;
307 }
308 return 0;
309 }
310