mips_reloc.c revision 1.33 1 /* $NetBSD: mips_reloc.c,v 1.33 2002/09/14 23:53:21 thorpej Exp $ */
2
3 /*
4 * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
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 OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <string.h>
34
35 #include "debug.h"
36 #include "rtld.h"
37
38 void _rtld_bind_start(void);
39 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
40 caddr_t _rtld_bind_mips(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
41
42 caddr_t
43 _rtld_bind_mips(a0, a1, a2, a3)
44 Elf_Word a0;
45 Elf_Addr a1, a2, a3;
46 {
47 Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
48 const Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
49 const Elf_Sym *def;
50 const Obj_Entry *defobj;
51 Elf_Addr new_value;
52
53 def = _rtld_find_symdef(a0, obj, &defobj, true);
54 if (def == NULL)
55 _rtld_die();
56
57 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
58 rdbg(("bind now/fixup in %s --> new=%p",
59 defobj->strtab + def->st_name, (void *)new_value));
60 u[obj->local_gotno + a0 - obj->gotsym] = new_value;
61 return ((caddr_t)new_value);
62 }
63
64 void
65 _rtld_setup_pltgot(obj)
66 const Obj_Entry *obj;
67 {
68 obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
69 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
70 obj->pltgot[1] |= (Elf_Addr) obj;
71 }
72
73 void
74 _rtld_relocate_nonplt_self(dynp, relocbase)
75 Elf_Dyn *dynp;
76 Elf_Addr relocbase;
77 {
78 const Elf_Rel *rel = 0, *rellim;
79 Elf_Addr relsz = 0;
80 Elf_Addr *where;
81 const Elf_Sym *symtab, *sym;
82 Elf_Addr *got;
83 Elf_Word local_gotno, symtabno, gotsym;
84 int i;
85
86 for (; dynp->d_tag != DT_NULL; dynp++) {
87 switch (dynp->d_tag) {
88 case DT_REL:
89 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
90 break;
91 case DT_RELSZ:
92 relsz = dynp->d_un.d_val;
93 break;
94 case DT_SYMTAB:
95 symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
96 break;
97 case DT_PLTGOT:
98 got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
99 break;
100 case DT_MIPS_LOCAL_GOTNO:
101 local_gotno = dynp->d_un.d_val;
102 break;
103 case DT_MIPS_SYMTABNO:
104 symtabno = dynp->d_un.d_val;
105 break;
106 case DT_MIPS_GOTSYM:
107 gotsym = dynp->d_un.d_val;
108 break;
109 }
110 }
111 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
112 for (; rel < rellim; rel++) {
113 where = (Elf_Addr *)(relocbase + rel->r_offset);
114
115 switch (ELF_R_TYPE(rel->r_info)) {
116 case R_TYPE(NONE):
117 break;
118
119 case R_TYPE(REL32):
120 sym = symtab + ELF_R_SYM(rel->r_info);
121 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL &&
122 ELF_ST_TYPE(sym->st_info) == STT_SECTION)
123 *where += (Elf_Addr)(sym->st_value + relocbase);
124 else
125 abort();
126 break;
127
128 default:
129 abort();
130 }
131 }
132
133 i = (got[1] & 0x80000000) ? 2 : 1;
134 /* Relocate the local GOT entries */
135 got += i;
136 for (; i < local_gotno; i++)
137 *got++ += relocbase;
138 sym = symtab + gotsym;
139 /* Now do the global GOT entries */
140 for (i = gotsym; i < symtabno; i++) {
141 *got = sym->st_value + relocbase;
142 ++sym;
143 ++got;
144 }
145 }
146
147 /*
148 * It is possible for the compiler to emit relocations for unaligned data.
149 * We handle this situation with these inlines.
150 */
151 #define RELOC_ALIGNED_P(x) \
152 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
153
154 static __inline Elf_Addr
155 load_ptr(void *where)
156 {
157 Elf_Addr res;
158
159 memcpy(&res, where, sizeof(res));
160
161 return (res);
162 }
163
164 static __inline void
165 store_ptr(void *where, Elf_Addr val)
166 {
167
168 memcpy(where, &val, sizeof(val));
169 }
170
171 int
172 _rtld_relocate_nonplt_objects(obj, self)
173 const Obj_Entry *obj;
174 bool self;
175 {
176 const Elf_Rel *rel;
177 Elf_Addr *got = obj->pltgot;
178 const Elf_Sym *sym, *def;
179 const Obj_Entry *defobj;
180 int i;
181
182 if (self)
183 return 0;
184
185 for (rel = obj->rel; rel < obj->rellim; rel++) {
186 Elf_Addr *where, tmp;
187 unsigned long symnum;
188
189 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
190 symnum = ELF_R_SYM(rel->r_info);
191
192 switch (ELF_R_TYPE(rel->r_info)) {
193 case R_TYPE(NONE):
194 break;
195
196 case R_TYPE(REL32):
197 /* 32-bit PC-relative reference */
198 def = obj->symtab + symnum;
199
200 if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
201 (ELF_ST_TYPE(def->st_info) == STT_SECTION ||
202 ELF_ST_TYPE(def->st_info) == STT_NOTYPE)) {
203 /*
204 * XXX: ABI DIFFERENCE!
205 *
206 * Old NetBSD binutils would generate shared
207 * libs with section-relative relocations being
208 * already adjusted for the start address of
209 * the section.
210 *
211 * New binutils, OTOH, generate shared libs
212 * with the same relocations being based at
213 * zero, so we need to add in the start address
214 * of the section.
215 *
216 * We assume that all section-relative relocs
217 * with contents less than the start of the
218 * section need to be adjusted; this should
219 * work with both old and new shlibs.
220 *
221 * --rkb, Oct 6, 2001
222 */
223 if (__predict_true(RELOC_ALIGNED_P(where))) {
224 tmp = *where;
225
226 if (def->st_info == STT_SECTION &&
227 tmp < def->st_value)
228 tmp += (Elf_Addr)def->st_value;
229
230 tmp += (Elf_Addr)obj->relocbase;
231 *where = tmp;
232 } else {
233 tmp = load_ptr(where);
234
235 if (def->st_info == STT_SECTION &&
236 tmp < def->st_value)
237 tmp += (Elf_Addr)def->st_value;
238
239 tmp += (Elf_Addr)obj->relocbase;
240 store_ptr(where, tmp);
241 }
242
243 rdbg(("REL32 %s in %s --> %p in %s",
244 obj->strtab + def->st_name, obj->path,
245 (void *)tmp, obj->path));
246 } else {
247 def = _rtld_find_symdef(symnum, obj, &defobj,
248 false);
249 if (def == NULL)
250 return -1;
251 if (__predict_true(RELOC_ALIGNED_P(where))) {
252 tmp = *where +
253 (Elf_Addr)(defobj->relocbase +
254 def->st_value);
255 *where = tmp;
256 } else {
257 tmp = load_ptr(where) +
258 (Elf_Addr)(defobj->relocbase +
259 def->st_value);
260 store_ptr(where, tmp);
261 }
262 rdbg(("REL32 %s in %s --> %p in %s",
263 obj->strtab + obj->symtab[symnum].st_name,
264 obj->path, (void *)tmp, defobj->path));
265 }
266 break;
267
268 default:
269 rdbg(("sym = %lu, type = %lu, offset = %p, "
270 "contents = %p, symbol = %s",
271 symnum, (u_long)ELF_R_TYPE(rel->r_info),
272 (void *)rel->r_offset, (void *)load_ptr(where),
273 obj->strtab + obj->symtab[symnum].st_name));
274 _rtld_error("%s: Unsupported relocation type %ld "
275 "in non-PLT relocations\n",
276 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
277 return -1;
278 }
279 }
280
281 i = (got[1] & 0x80000000) ? 2 : 1;
282 /* Relocate the local GOT entries */
283 got += i;
284 for (; i < obj->local_gotno; i++)
285 *got++ += (Elf_Addr)obj->relocbase;
286 sym = obj->symtab + obj->gotsym;
287 /* Now do the global GOT entries */
288 for (i = obj->gotsym; i < obj->symtabno; i++) {
289 rdbg((" doing got %d sym %p (%s, %x)", i - obj->gotsym, sym,
290 sym->st_name + obj->strtab, *got));
291
292 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
293 sym->st_value != 0)
294 /* The symbol table contains the address of the PLT
295 slot. However, sometimes a PLT slot is not
296 allocated, so we have to check st_value first. */
297 *got = sym->st_value + (Elf_Addr)obj->relocbase;
298 else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
299 ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
300 /* Symbols with index SHN_ABS are not relocated. */
301 if (sym->st_shndx != SHN_ABS)
302 *got = sym->st_value +
303 (Elf_Addr)obj->relocbase;
304 } else {
305 def = _rtld_find_symdef(i, obj, &defobj, true);
306 if (def == NULL)
307 return -1;
308 *got = def->st_value + (Elf_Addr)defobj->relocbase;
309 }
310 ++sym;
311 ++got;
312 }
313
314 return 0;
315 }
316
317 int
318 _rtld_relocate_plt_lazy(obj)
319 const Obj_Entry *obj;
320 {
321 /* PLT fixups were done above in the GOT relocation. */
322 return 0;
323 }
324