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