ppc_reloc.c revision 1.29 1 /* $NetBSD: ppc_reloc.c,v 1.29 2002/09/26 02:52:05 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 void _rtld_powerpc_pltcall __P((Elf_Word));
42 void _rtld_powerpc_pltresolve __P((Elf_Word, Elf_Word));
43
44 #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
45 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
46 #define l(x) ((u_int32_t)(x) & 0xffff)
47
48 void _rtld_bind_start(void);
49 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
50 caddr_t _rtld_bind __P((const Obj_Entry *, Elf_Word));
51
52 /*
53 * Setup the plt glue routines.
54 */
55 #define PLTCALL_SIZE 20
56 #define PLTRESOLVE_SIZE 24
57
58 void
59 _rtld_setup_pltgot(obj)
60 const Obj_Entry *obj;
61 {
62 Elf_Word *pltcall, *pltresolve;
63 Elf_Word *jmptab;
64 int N = obj->pltrelalim - obj->pltrela;
65
66 /* Entries beyond 8192 take twice as much space. */
67 if (N > 8192)
68 N += N-8192;
69
70 pltcall = obj->pltgot;
71 jmptab = pltcall + 18 + N * 2;
72
73 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
74 pltcall[1] |= ha(jmptab);
75 pltcall[2] |= l(jmptab);
76
77 pltresolve = obj->pltgot + 8;
78
79 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
80 pltresolve[0] |= ha(_rtld_bind_start);
81 pltresolve[1] |= l(_rtld_bind_start);
82 pltresolve[3] |= ha(obj);
83 pltresolve[4] |= l(obj);
84
85 __syncicache(pltcall, 72 + N * 8);
86 }
87
88 void
89 _rtld_relocate_nonplt_self(dynp, relocbase)
90 Elf_Dyn *dynp;
91 Elf_Addr relocbase;
92 {
93 const Elf_Rela *rela = 0, *relalim;
94 Elf_Addr relasz = 0;
95 Elf_Addr *where;
96
97 for (; dynp->d_tag != DT_NULL; dynp++) {
98 switch (dynp->d_tag) {
99 case DT_RELA:
100 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
101 break;
102 case DT_RELASZ:
103 relasz = dynp->d_un.d_val;
104 break;
105 }
106 }
107 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
108 for (; rela < relalim; rela++) {
109 where = (Elf_Addr *)(relocbase + rela->r_offset);
110 *where = (Elf_Addr)(relocbase + rela->r_addend);
111 }
112 }
113
114 int
115 _rtld_relocate_nonplt_objects(obj, self)
116 const Obj_Entry *obj;
117 bool self;
118 {
119 const Elf_Rela *rela;
120
121 if (self)
122 return 0;
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(obj)
196 const Obj_Entry *obj;
197 {
198 const Elf_Rela *rela;
199 int reloff;
200
201 for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
202 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
203 int distance;
204 Elf_Addr *pltresolve;
205
206 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
207
208 pltresolve = obj->pltgot + 8;
209
210 if (reloff < 32768) {
211 /* li r11,reloff */
212 *where++ = 0x39600000 | reloff;
213 } else {
214 /* lis r11,ha(reloff) */
215 /* addi r11,l(reloff) */
216 *where++ = 0x3d600000 | ha(reloff);
217 *where++ = 0x396b0000 | l(reloff);
218 }
219 /* b pltresolve */
220 distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
221 *where++ = 0x48000000 | (distance & 0x03fffffc);
222 /* __syncicache(where - 12, 12); */
223 }
224
225 return 0;
226 }
227
228 caddr_t
229 _rtld_bind(obj, reloff)
230 const Obj_Entry *obj;
231 Elf_Word reloff;
232 {
233 const Elf_Rela *rela = obj->pltrela + reloff;
234 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
235 Elf_Addr value;
236 const Elf_Sym *def;
237 const Obj_Entry *defobj;
238 int distance;
239
240 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
241
242 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
243 if (def == NULL)
244 _rtld_die();
245
246 value = (Elf_Addr)(defobj->relocbase + def->st_value);
247 distance = value - (Elf_Addr)where;
248 rdbg(("bind now/fixup in %s --> new=%p",
249 defobj->strtab + def->st_name, (void *)value));
250
251 if (abs(distance) < 32*1024*1024) { /* inside 32MB? */
252 /* b value # branch directly */
253 *where = 0x48000000 | (distance & 0x03fffffc);
254 __syncicache(where, 4);
255 } else {
256 Elf_Addr *pltcall, *jmptab;
257 int N = obj->pltrelalim - obj->pltrela;
258
259 /* Entries beyond 8192 take twice as much space. */
260 if (N > 8192)
261 N += N-8192;
262
263 pltcall = obj->pltgot;
264 jmptab = pltcall + 18 + N * 2;
265
266 jmptab[reloff] = value;
267
268 if (reloff < 32768) {
269 /* li r11,reloff */
270 *where++ = 0x39600000 | reloff;
271 } else {
272 /* lis r11,ha(reloff) */
273 /* addi r11,l(reloff) */
274 *where++ = 0x3d600000 | ha(reloff);
275 *where++ = 0x396b0000 | l(reloff);
276 }
277 /* b pltcall */
278 distance = (Elf_Addr)pltcall - (Elf_Addr)where;
279 *where++ = 0x48000000 | (distance & 0x03fffffc);
280 __syncicache(where - 12, 12);
281 }
282
283 return (caddr_t)value;
284 }
285