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