ppc_reloc.c revision 1.26 1 /* $NetBSD: ppc_reloc.c,v 1.26 2002/09/24 15:04:48 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) < 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 {
82 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
83 int distance;
84
85 const Elf_Sym *def;
86 const Obj_Entry *defobj;
87 Elf_Addr value;
88
89 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
90
91 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
92 true);
93 if (def == NULL)
94 return (-1);
95
96 value = (Elf_Addr)(defobj->relocbase + def->st_value);
97 distance = value - (Elf_Addr)where;
98
99 if (abs(distance) < 32*1024*1024) { /* inside 32MB? */
100 /* b value # branch directly */
101 *where = 0x48000000 | (distance & 0x03fffffc);
102 __syncicache(where, 4);
103 } else {
104 Elf_Addr *pltcall, *jmptab;
105 int N = obj->pltrelalim - obj->pltrela;
106 int reloff = rela - obj->pltrela;
107
108 if (reloff < 0 || reloff >= 0x8000)
109 return (-1);
110
111 pltcall = obj->pltgot;
112
113 jmptab = pltcall + 18 + N * 2;
114 jmptab[reloff] = value;
115
116 distance = (Elf_Addr)pltcall - (Elf_Addr)(where + 1);
117
118 /* li r11,reloff */
119 /* b pltcall # use pltcall routine */
120 where[0] = 0x39600000 | reloff;
121 where[1] = 0x48000000 | (distance & 0x03fffffc);
122 __syncicache(where, 8);
123 }
124
125 *addrp = (caddr_t)value;
126 return (0);
127 }
128
129 /*
130 * Setup the plt glue routines.
131 */
132 #define PLTCALL_SIZE 20
133 #define PLTRESOLVE_SIZE 24
134
135 void
136 _rtld_setup_pltgot(obj)
137 const Obj_Entry *obj;
138 {
139 Elf_Word *pltcall, *pltresolve;
140 Elf_Word *jmptab;
141 int N = obj->pltrelalim - obj->pltrela;
142
143 pltcall = obj->pltgot;
144
145 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
146 jmptab = pltcall + 18 + N * 2;
147 pltcall[1] |= ha(jmptab);
148 pltcall[2] |= l(jmptab);
149
150 pltresolve = obj->pltgot + 8;
151
152 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
153 pltresolve[0] |= ha(_rtld_bind_start);
154 pltresolve[1] |= l(_rtld_bind_start);
155 pltresolve[3] |= ha(obj);
156 pltresolve[4] |= l(obj);
157
158 __syncicache(pltcall, 72 + N * 8);
159 }
160
161 void
162 _rtld_relocate_nonplt_self(dynp, relocbase)
163 Elf_Dyn *dynp;
164 Elf_Addr relocbase;
165 {
166 const Elf_Rela *rela = 0, *relalim;
167 Elf_Addr relasz = 0;
168 Elf_Addr *where;
169
170 for (; dynp->d_tag != DT_NULL; dynp++) {
171 switch (dynp->d_tag) {
172 case DT_RELA:
173 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
174 break;
175 case DT_RELASZ:
176 relasz = dynp->d_un.d_val;
177 break;
178 }
179 }
180 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
181 for (; rela < relalim; rela++) {
182 where = (Elf_Addr *)(relocbase + rela->r_offset);
183 *where = (Elf_Addr)(relocbase + rela->r_addend);
184 }
185 }
186
187 int
188 _rtld_relocate_nonplt_objects(obj, self)
189 const Obj_Entry *obj;
190 bool self;
191 {
192 const Elf_Rela *rela;
193
194 if (self)
195 return 0;
196
197 for (rela = obj->rela; rela < obj->relalim; rela++) {
198 Elf_Addr *where;
199 const Elf_Sym *def;
200 const Obj_Entry *defobj;
201 Elf_Addr tmp;
202 unsigned long symnum;
203
204 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
205 symnum = ELF_R_SYM(rela->r_info);
206
207 switch (ELF_R_TYPE(rela->r_info)) {
208 #if 1 /* XXX Should not be necessary. */
209 case R_TYPE(JMP_SLOT):
210 #endif
211 case R_TYPE(NONE):
212 break;
213
214 case R_TYPE(32): /* word32 S + A */
215 case R_TYPE(GLOB_DAT): /* word32 S + A */
216 def = _rtld_find_symdef(symnum, obj, &defobj, false);
217 if (def == NULL)
218 return -1;
219
220 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
221 rela->r_addend);
222 if (*where != tmp)
223 *where = tmp;
224 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
225 obj->strtab + obj->symtab[symnum].st_name,
226 obj->path, (void *)*where, defobj->path));
227 break;
228
229 case R_TYPE(RELATIVE): /* word32 B + A */
230 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
231 rdbg(("RELATIVE in %s --> %p", obj->path,
232 (void *)*where));
233 break;
234
235 case R_TYPE(COPY):
236 /*
237 * These are deferred until all other relocations have
238 * been done. All we do here is make sure that the
239 * COPY relocation is not in a shared library. They
240 * are allowed only in executable files.
241 */
242 if (obj->isdynamic) {
243 _rtld_error(
244 "%s: Unexpected R_COPY relocation in shared library",
245 obj->path);
246 return -1;
247 }
248 rdbg(("COPY (avoid in main)"));
249 break;
250
251 default:
252 rdbg(("sym = %lu, type = %lu, offset = %p, "
253 "addend = %p, contents = %p, symbol = %s",
254 symnum, (u_long)ELF_R_TYPE(rela->r_info),
255 (void *)rela->r_offset, (void *)rela->r_addend,
256 (void *)*where,
257 obj->strtab + obj->symtab[symnum].st_name));
258 _rtld_error("%s: Unsupported relocation type %ld "
259 "in non-PLT relocations\n",
260 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
261 return -1;
262 }
263 }
264 return 0;
265 }
266
267 int
268 _rtld_relocate_plt_lazy(obj)
269 const Obj_Entry *obj;
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