Move primary cache code to libminixfs.
[minix.git] / libexec / ld.elf_so / arch / powerpc / ppc_reloc.c
blob88b93a76edb483b897fe0441400a8d6de12f511e
1 /* $NetBSD: ppc_reloc.c,v 1.46 2011/01/16 01:22:29 matt Exp $ */
3 /*-
4 * Copyright (C) 1998 Tsubai Masanari
5 * Portions copyright 2002 Charles M. Hannum <root@ihack.net>
6 * All rights reserved.
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.
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.
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: ppc_reloc.c,v 1.46 2011/01/16 01:22:29 matt Exp $");
34 #endif /* not lint */
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <machine/cpu.h>
43 #include "debug.h"
44 #include "rtld.h"
46 void _rtld_powerpc_pltcall(Elf_Word);
47 void _rtld_powerpc_pltresolve(Elf_Word, Elf_Word);
49 #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
50 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
51 #define l(x) ((u_int32_t)(x) & 0xffff)
53 void _rtld_bind_bssplt_start(void);
54 void _rtld_bind_secureplt_start(void);
55 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
56 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
57 static int _rtld_relocate_plt_object(const Obj_Entry *,
58 const Elf_Rela *, int, Elf_Addr *);
61 * The PPC PLT format consists of three sections:
62 * (1) The "pltcall" and "pltresolve" glue code. This is always 18 words.
63 * (2) The code part of the PLT entries. There are 2 words per entry for
64 * up to 8192 entries, then 4 words per entry for any additional entries.
65 * (3) The data part of the PLT entries, comprising a jump table.
66 * This section is half the size of the second section (ie. 1 or 2 words
67 * per entry).
71 * Setup the plt glue routines (for bss-plt).
73 #define PLTCALL_SIZE 20
74 #define PLTRESOLVE_SIZE 24
76 void
77 _rtld_setup_pltgot(const Obj_Entry *obj)
80 * Secure-PLT is much more sane.
82 if (obj->gotptr != NULL) {
83 obj->gotptr[1] = (Elf_Addr) _rtld_bind_secureplt_start;
84 obj->gotptr[2] = (Elf_Addr) obj;
85 } else {
86 Elf_Word *pltcall, *pltresolve;
87 Elf_Word *jmptab;
88 int N = obj->pltrelalim - obj->pltrela;
90 /* Entries beyond 8192 take twice as much space. */
91 if (N > 8192)
92 N += N-8192;
94 pltcall = obj->pltgot;
95 jmptab = pltcall + 18 + N * 2;
97 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
98 pltcall[1] |= ha(jmptab);
99 pltcall[2] |= l(jmptab);
101 pltresolve = obj->pltgot + 8;
103 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
104 pltresolve[0] |= ha(_rtld_bind_bssplt_start);
105 pltresolve[1] |= l(_rtld_bind_bssplt_start);
106 pltresolve[3] |= ha(obj);
107 pltresolve[4] |= l(obj);
110 * Invalidate the icache for only the code part of the PLT
111 * (and not the jump table at the end).
113 __syncicache(pltcall, (char *)jmptab - (char *)pltcall);
117 void
118 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
120 const Elf_Rela *rela = 0, *relalim;
121 Elf_Addr relasz = 0;
122 Elf_Addr *where;
124 for (; dynp->d_tag != DT_NULL; dynp++) {
125 switch (dynp->d_tag) {
126 case DT_RELA:
127 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
128 break;
129 case DT_RELASZ:
130 relasz = dynp->d_un.d_val;
131 break;
134 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
135 for (; rela < relalim; rela++) {
136 where = (Elf_Addr *)(relocbase + rela->r_offset);
137 *where = (Elf_Addr)(relocbase + rela->r_addend);
142 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
144 const Elf_Rela *rela;
146 for (rela = obj->rela; rela < obj->relalim; rela++) {
147 Elf_Addr *where;
148 const Elf_Sym *def;
149 const Obj_Entry *defobj;
150 Elf_Addr tmp;
151 unsigned long symnum;
153 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
154 symnum = ELF_R_SYM(rela->r_info);
156 switch (ELF_R_TYPE(rela->r_info)) {
157 #if 1 /* XXX Should not be necessary. */
158 case R_TYPE(JMP_SLOT):
159 #endif
160 case R_TYPE(NONE):
161 break;
163 case R_TYPE(32): /* word32 S + A */
164 case R_TYPE(GLOB_DAT): /* word32 S + A */
165 def = _rtld_find_symdef(symnum, obj, &defobj, false);
166 if (def == NULL)
167 return -1;
169 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
170 rela->r_addend);
171 if (*where != tmp)
172 *where = tmp;
173 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
174 obj->strtab + obj->symtab[symnum].st_name,
175 obj->path, (void *)*where, defobj->path));
176 break;
178 case R_TYPE(RELATIVE): /* word32 B + A */
179 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
180 rdbg(("RELATIVE in %s --> %p", obj->path,
181 (void *)*where));
182 break;
184 case R_TYPE(COPY):
186 * These are deferred until all other relocations have
187 * been done. All we do here is make sure that the
188 * COPY relocation is not in a shared library. They
189 * are allowed only in executable files.
191 if (obj->isdynamic) {
192 _rtld_error(
193 "%s: Unexpected R_COPY relocation in shared library",
194 obj->path);
195 return -1;
197 rdbg(("COPY (avoid in main)"));
198 break;
200 default:
201 rdbg(("sym = %lu, type = %lu, offset = %p, "
202 "addend = %p, contents = %p, symbol = %s",
203 symnum, (u_long)ELF_R_TYPE(rela->r_info),
204 (void *)rela->r_offset, (void *)rela->r_addend,
205 (void *)*where,
206 obj->strtab + obj->symtab[symnum].st_name));
207 _rtld_error("%s: Unsupported relocation type %ld "
208 "in non-PLT relocations",
209 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
210 return -1;
213 return 0;
217 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
219 Elf_Addr * const pltresolve = obj->pltgot + 8;
220 const Elf_Rela *rela;
221 int reloff;
223 for (rela = obj->pltrela, reloff = 0;
224 rela < obj->pltrelalim;
225 rela++, reloff++) {
226 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
228 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
230 if (obj->gotptr != NULL) {
232 * For now, simply treat then as relative.
234 *where += (Elf_Addr)obj->relocbase;
235 } else {
236 int distance;
238 if (reloff < 32768) {
239 /* li r11,reloff */
240 *where++ = 0x39600000 | reloff;
241 } else {
242 /* lis r11,ha(reloff) */
243 /* addi r11,l(reloff) */
244 *where++ = 0x3d600000 | ha(reloff);
245 *where++ = 0x396b0000 | l(reloff);
247 /* b pltresolve */
248 distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
249 *where++ = 0x48000000 | (distance & 0x03fffffc);
252 * Icache invalidation is not done for each entry here
253 * because we sync the entire code part of the PLT once
254 * in _rtld_setup_pltgot() after all the entries have been
255 * initialized.
257 /* __syncicache(where - 3, 12); */
261 return 0;
264 static int
265 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
267 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
268 Elf_Addr value;
269 const Elf_Sym *def;
270 const Obj_Entry *defobj;
271 int distance;
272 unsigned long info = rela->r_info;
274 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
276 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
277 if (__predict_false(def == NULL))
278 return -1;
279 if (__predict_false(def == &_rtld_sym_zero))
280 return 0;
282 value = (Elf_Addr)(defobj->relocbase + def->st_value);
283 distance = value - (Elf_Addr)where;
284 rdbg(("bind now/fixup in %s --> new=%p",
285 defobj->strtab + def->st_name, (void *)value));
287 if (obj->gotptr != NULL) {
289 * For Secure-PLT we simply replace the entry in GOT with the address
290 * of the routine.
292 assert(where >= (Elf_Word *)obj->pltgot);
293 assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
294 *where = value;
295 } else if (abs(distance) < 32*1024*1024) { /* inside 32MB? */
296 /* b value # branch directly */
297 *where = 0x48000000 | (distance & 0x03fffffc);
298 __syncicache(where, 4);
299 } else {
300 Elf_Addr *pltcall, *jmptab;
301 int N = obj->pltrelalim - obj->pltrela;
303 /* Entries beyond 8192 take twice as much space. */
304 if (N > 8192)
305 N += N-8192;
307 pltcall = obj->pltgot;
308 jmptab = pltcall + 18 + N * 2;
310 jmptab[reloff] = value;
312 if (reloff < 32768) {
313 /* li r11,reloff */
314 *where++ = 0x39600000 | reloff;
315 } else {
316 #ifdef notyet
317 /* lis r11,ha(value) */
318 /* addi r11,l(value) */
319 /* mtctr r11 */
320 /* bctr */
321 *where++ = 0x3d600000 | ha(value);
322 *where++ = 0x396b0000 | l(value);
323 *where++ = 0x7d6903a6;
324 *where++ = 0x4e800420;
325 #else
326 /* lis r11,ha(reloff) */
327 /* addi r11,l(reloff) */
328 *where++ = 0x3d600000 | ha(reloff);
329 *where++ = 0x396b0000 | l(reloff);
330 #endif
332 /* b pltcall */
333 distance = (Elf_Addr)pltcall - (Elf_Addr)where;
334 *where++ = 0x48000000 | (distance & 0x03fffffc);
335 __syncicache(where - 3, 12);
338 if (tp)
339 *tp = value;
340 return 0;
343 caddr_t
344 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
346 const Elf_Rela *rela = (const void *)((const char *)obj->pltrela + reloff);
347 Elf_Addr new_value;
348 int err;
350 new_value = 0; /* XXX gcc */
352 err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
353 if (err)
354 _rtld_die();
356 return (caddr_t)new_value;
360 _rtld_relocate_plt_objects(const Obj_Entry *obj)
362 const Elf_Rela *rela;
363 int reloff;
365 for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
366 if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
367 return -1;
369 return 0;