ppc_reloc.c revision 1.14 1 /* $NetBSD: ppc_reloc.c,v 1.14 2002/09/05 20:08:18 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 caddr_t _rtld_bind_powerpc __P((Obj_Entry *, Elf_Word));
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 /*
50 * Bind a pltgot slot indexed by reloff.
51 */
52 caddr_t
53 _rtld_bind_powerpc(obj, reloff)
54 Obj_Entry *obj;
55 Elf_Word reloff;
56 {
57 const Elf_Rela *rela;
58 caddr_t addr;
59
60 if (reloff < 0 || reloff >= 0x8000) {
61 dbg(("_rtld_bind_powerpc: broken reloff %x", reloff));
62 _rtld_die();
63 }
64
65 rela = obj->pltrela + reloff;
66
67 if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
68 _rtld_die();
69
70 return addr;
71 }
72
73 /*
74 * Make a pltgot slot.
75 * Initial value is "pltresolve" unless bind_now is true.
76 */
77 int
78 _rtld_relocate_plt_object(
79 Obj_Entry *obj,
80 const Elf_Rela *rela,
81 caddr_t *addrp,
82 bool bind_now,
83 bool dodebug)
84 {
85 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
86 int distance;
87
88 if (bind_now) {
89 const Elf_Sym *def;
90 const Obj_Entry *defobj;
91 Elf_Addr value;
92
93 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
94
95 def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
96 if (def == NULL)
97 return (-1);
98
99 value = (Elf_Addr)(defobj->relocbase + def->st_value);
100 distance = value - (Elf_Addr)where;
101
102 if (abs(distance) < 32*1024*1024) { /* inside 32MB? */
103 /* b value # branch directly */
104 *where = 0x48000000 | (distance & 0x03fffffc);
105 __syncicache(where, 4);
106 } else {
107 Elf_Addr *pltcall, *jmptab;
108 int N = obj->pltrelalim - obj->pltrela;
109 int reloff = rela - obj->pltrela;
110
111 if (reloff < 0 || reloff >= 0x8000)
112 return (-1);
113
114 pltcall = obj->pltgot;
115
116 jmptab = pltcall + 18 + N * 2;
117 jmptab[reloff] = value;
118
119 distance = (Elf_Addr)pltcall - (Elf_Addr)(where + 1);
120
121 /* li r11,reloff */
122 /* b pltcall # use pltcall routine */
123 where[0] = 0x39600000 | reloff;
124 where[1] = 0x48000000 | (distance & 0x03fffffc);
125 __syncicache(where, 8);
126 }
127
128 if (addrp != NULL)
129 *addrp = (caddr_t)value;
130
131 return (0);
132 } else {
133 Elf_Addr *pltresolve;
134 int reloff = rela - obj->pltrela;
135
136 if (reloff < 0 || reloff >= 0x8000)
137 return (-1);
138
139 pltresolve = obj->pltgot + 8;
140
141 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
142
143 /* li r11,reloff */
144 /* b pltresolve */
145 where[0] = 0x39600000 | reloff;
146 where[1] = 0x48000000 | (distance & 0x03fffffc);
147 /* __syncicache(where, 8); */
148 }
149
150 return 0;
151 }
152
153 /*
154 * Setup the plt glue routines.
155 */
156 #define PLTCALL_SIZE 20
157 #define PLTRESOLVE_SIZE 24
158
159 void
160 _rtld_setup_pltgot(obj)
161 const Obj_Entry *obj;
162 {
163 Elf_Word *pltcall, *pltresolve;
164 Elf_Word *jmptab;
165 int N = obj->pltrelalim - obj->pltrela;
166
167 pltcall = obj->pltgot;
168
169 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
170 jmptab = pltcall + 18 + N * 2;
171 pltcall[1] |= ha(jmptab);
172 pltcall[2] |= l(jmptab);
173
174 pltresolve = obj->pltgot + 8;
175
176 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
177 pltresolve[0] |= ha(_rtld_bind_start);
178 pltresolve[1] |= l(_rtld_bind_start);
179 pltresolve[3] |= ha(obj);
180 pltresolve[4] |= l(obj);
181
182 __syncicache(pltcall, 72 + N * 8);
183 }
184
185 int
186 _rtld_relocate_nonplt_objects(obj, dodebug)
187 Obj_Entry *obj;
188 bool dodebug;
189 {
190 const Elf_Rela *rela;
191
192 for (rela = obj->rela; rela < obj->relalim; rela++) {
193 Elf_Addr *where;
194 const Elf_Sym *def;
195 const Obj_Entry *defobj;
196 Elf_Addr tmp;
197
198 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
199
200 switch (ELF_R_TYPE(rela->r_info)) {
201 case R_TYPE(NONE):
202 break;
203
204 case R_TYPE(32): /* word32 S + A */
205 case R_TYPE(GLOB_DAT): /* word32 S + A */
206 def = _rtld_find_symdef(rela->r_info, obj, &defobj,
207 false);
208 if (def == NULL)
209 return -1;
210
211 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
212 rela->r_addend);
213 if (*where != tmp)
214 *where = tmp;
215 rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
216 defobj->strtab + def->st_name, obj->path,
217 (void *)*where, defobj->path));
218 break;
219
220 case R_TYPE(RELATIVE): /* word32 B + A */
221 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
222 if (*where != tmp)
223 *where = tmp;
224 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
225 (void *)*where));
226 break;
227
228 case R_TYPE(COPY):
229 /*
230 * These are deferred until all other relocations have
231 * been done. All we do here is make sure that the
232 * COPY relocation is not in a shared library. They
233 * are allowed only in executable files.
234 */
235 if (!obj->mainprog) {
236 _rtld_error(
237 "%s: Unexpected R_COPY relocation in shared library",
238 obj->path);
239 return -1;
240 }
241 rdbg(dodebug, ("COPY (avoid in main)"));
242 break;
243
244 default:
245 def = _rtld_find_symdef(rela->r_info, obj, &defobj,
246 true);
247 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
248 "addend = %p, contents = %p, symbol = %s",
249 (u_long)ELF_R_SYM(rela->r_info),
250 (u_long)ELF_R_TYPE(rela->r_info),
251 (void *)rela->r_offset, (void *)rela->r_addend,
252 (void *)*where,
253 def ? defobj->strtab + def->st_name : "??"));
254 _rtld_error("%s: Unsupported relocation type %ld "
255 "in non-PLT relocations\n",
256 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
257 return -1;
258 }
259 }
260 return 0;
261 }
262