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