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