mips_reloc.c revision 1.55 1 /* $NetBSD: mips_reloc.c,v 1.55 2009/08/29 13:46:55 jmmv Exp $ */
2
3 /*
4 * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
5 * Portions copyright 2002 Charles M. Hannum <root (at) ihack.net>
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/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: mips_reloc.c,v 1.55 2009/08/29 13:46:55 jmmv Exp $");
34 #endif /* not lint */
35
36 #include <sys/types.h>
37 #include <sys/stat.h>
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "debug.h"
43 #include "rtld.h"
44
45 #define SUPPORT_OLD_BROKEN_LD
46
47 void _rtld_bind_start(void);
48 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
49 caddr_t _rtld_bind(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
50
51 /*
52 * It is possible for the compiler to emit relocations for unaligned data.
53 * We handle this situation with these inlines.
54 */
55 #define RELOC_ALIGNED_P(x) \
56 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
57
58 static inline Elf_Addr
59 load_ptr(void *where)
60 {
61 if (__predict_true(RELOC_ALIGNED_P(where)))
62 return *(Elf_Addr *)where;
63 else {
64 Elf_Addr res;
65
66 (void)memcpy(&res, where, sizeof(res));
67 return res;
68 }
69 }
70
71 static inline void
72 store_ptr(void *where, Elf_Addr val)
73 {
74 if (__predict_true(RELOC_ALIGNED_P(where)))
75 *(Elf_Addr *)where = val;
76 else
77 (void)memcpy(where, &val, sizeof(val));
78 }
79
80
81 void
82 _rtld_setup_pltgot(const Obj_Entry *obj)
83 {
84 obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
85 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
86 obj->pltgot[1] |= (Elf_Addr) obj;
87 }
88
89 void
90 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
91 {
92 const Elf_Rel *rel = 0, *rellim;
93 Elf_Addr relsz = 0;
94 void *where;
95 const Elf_Sym *symtab = NULL, *sym;
96 Elf_Addr *got = NULL;
97 Elf_Word local_gotno = 0, symtabno = 0, gotsym = 0, i;
98
99 for (; dynp->d_tag != DT_NULL; dynp++) {
100 switch (dynp->d_tag) {
101 case DT_REL:
102 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
103 break;
104 case DT_RELSZ:
105 relsz = dynp->d_un.d_val;
106 break;
107 case DT_SYMTAB:
108 symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
109 break;
110 case DT_PLTGOT:
111 got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
112 break;
113 case DT_MIPS_LOCAL_GOTNO:
114 local_gotno = dynp->d_un.d_val;
115 break;
116 case DT_MIPS_SYMTABNO:
117 symtabno = dynp->d_un.d_val;
118 break;
119 case DT_MIPS_GOTSYM:
120 gotsym = dynp->d_un.d_val;
121 break;
122 }
123 }
124
125 i = (got[1] & 0x80000000) ? 2 : 1;
126 /* Relocate the local GOT entries */
127 got += i;
128 for (; i < local_gotno; i++)
129 *got++ += relocbase;
130 sym = symtab + gotsym;
131 /* Now do the global GOT entries */
132 for (i = gotsym; i < symtabno; i++) {
133 *got = sym->st_value + relocbase;
134 ++sym;
135 ++got;
136 }
137
138 rellim = (const Elf_Rel *)((const char *)rel + relsz);
139 for (; rel < rellim; rel++) {
140 where = (void *)(relocbase + rel->r_offset);
141
142 switch (ELF_R_TYPE(rel->r_info)) {
143 case R_TYPE(NONE):
144 break;
145
146 case R_TYPE(REL32):
147 assert(ELF_R_SYM(rel->r_info) < gotsym);
148 sym = symtab + ELF_R_SYM(rel->r_info);
149 assert(ELF_ST_BIND(sym->st_info) == STB_LOCAL);
150 store_ptr(where, load_ptr(where) + relocbase);
151 break;
152
153 default:
154 abort();
155 }
156 }
157 }
158
159 int
160 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
161 {
162 const Elf_Rel *rel;
163 Elf_Addr *got = obj->pltgot;
164 const Elf_Sym *sym, *def;
165 const Obj_Entry *defobj;
166 Elf_Word i;
167 #ifdef SUPPORT_OLD_BROKEN_LD
168 int broken;
169 #endif
170
171 #ifdef SUPPORT_OLD_BROKEN_LD
172 broken = 0;
173 sym = obj->symtab;
174 for (i = 1; i < 12; i++)
175 if (sym[i].st_info == ELF_ST_INFO(STB_LOCAL, STT_NOTYPE))
176 broken = 1;
177 dbg(("%s: broken=%d", obj->path, broken));
178 #endif
179
180 i = (got[1] & 0x80000000) ? 2 : 1;
181 /* Relocate the local GOT entries */
182 got += i;
183 for (; i < obj->local_gotno; i++)
184 *got++ += (Elf_Addr)obj->relocbase;
185 sym = obj->symtab + obj->gotsym;
186 /* Now do the global GOT entries */
187 for (i = obj->gotsym; i < obj->symtabno; i++) {
188 rdbg((" doing got %d sym %p (%s, %x)", i - obj->gotsym, sym,
189 sym->st_name + obj->strtab, *got));
190
191 #ifdef SUPPORT_OLD_BROKEN_LD
192 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
193 broken && sym->st_shndx == SHN_UNDEF) {
194 /*
195 * XXX DANGER WILL ROBINSON!
196 * You might think this is stupid, as it intentionally
197 * defeats lazy binding -- and you'd be right.
198 * Unfortunately, for lazy binding to work right, we
199 * need to a way to force the GOT slots used for
200 * function pointers to be resolved immediately. This
201 * is supposed to be done automatically by the linker,
202 * by not outputting a PLT slot and setting st_value
203 * to 0 if there are non-PLT references, but older
204 * versions of GNU ld do not do this.
205 */
206 def = _rtld_find_symdef(i, obj, &defobj, false);
207 if (def == NULL)
208 return -1;
209 *got = def->st_value + (Elf_Addr)defobj->relocbase;
210 } else
211 #endif
212 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
213 sym->st_value != 0 && sym->st_shndx == SHN_UNDEF) {
214 /*
215 * If there are non-PLT references to the function,
216 * st_value should be 0, forcing us to resolve the
217 * address immediately.
218 *
219 * XXX DANGER WILL ROBINSON!
220 * The linker is not outputting PLT slots for calls to
221 * functions that are defined in the same shared
222 * library. This is a bug, because it can screw up
223 * link ordering rules if the symbol is defined in
224 * more than one module. For now, if there is a
225 * definition, we fail the test above and force a full
226 * symbol lookup. This means that all intra-module
227 * calls are bound immediately. - mycroft, 2003/09/24
228 */
229 *got = sym->st_value + (Elf_Addr)obj->relocbase;
230 } else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
231 /* Symbols with index SHN_ABS are not relocated. */
232 if (sym->st_shndx != SHN_ABS)
233 *got = sym->st_value +
234 (Elf_Addr)obj->relocbase;
235 } else {
236 def = _rtld_find_symdef(i, obj, &defobj, false);
237 if (def == NULL)
238 return -1;
239 *got = def->st_value + (Elf_Addr)defobj->relocbase;
240 }
241
242 rdbg((" --> now %x", *got));
243 ++sym;
244 ++got;
245 }
246
247 got = obj->pltgot;
248 for (rel = obj->rel; rel < obj->rellim; rel++) {
249 void *where;
250 Elf_Addr tmp;
251 unsigned long symnum;
252
253 where = obj->relocbase + rel->r_offset;
254 symnum = ELF_R_SYM(rel->r_info);
255
256 switch (ELF_R_TYPE(rel->r_info)) {
257 case R_TYPE(NONE):
258 break;
259
260 case R_TYPE(REL32):
261 /* 32-bit PC-relative reference */
262 def = obj->symtab + symnum;
263
264 if (symnum >= obj->gotsym) {
265 tmp = load_ptr(where);
266 tmp += got[obj->local_gotno + symnum - obj->gotsym];
267 store_ptr(where, tmp);
268
269 rdbg(("REL32/G %s in %s --> %p in %s",
270 obj->strtab + def->st_name, obj->path,
271 (void *)tmp, obj->path));
272 break;
273 } else {
274 /*
275 * XXX: ABI DIFFERENCE!
276 *
277 * Old NetBSD binutils would generate shared
278 * libs with section-relative relocations being
279 * already adjusted for the start address of
280 * the section.
281 *
282 * New binutils, OTOH, generate shared libs
283 * with the same relocations being based at
284 * zero, so we need to add in the start address
285 * of the section.
286 *
287 * --rkb, Oct 6, 2001
288 */
289 tmp = load_ptr(where);
290
291 if (def->st_info ==
292 ELF_ST_INFO(STB_LOCAL, STT_SECTION)
293 #ifdef SUPPORT_OLD_BROKEN_LD
294 && !broken
295 #endif
296 )
297 tmp += (Elf_Addr)def->st_value;
298
299 tmp += (Elf_Addr)obj->relocbase;
300 store_ptr(where, tmp);
301
302 rdbg(("REL32/L %s in %s --> %p in %s",
303 obj->strtab + def->st_name, obj->path,
304 (void *)tmp, obj->path));
305 }
306 break;
307
308 default:
309 rdbg(("sym = %lu, type = %lu, offset = %p, "
310 "contents = %p, symbol = %s",
311 symnum, (u_long)ELF_R_TYPE(rel->r_info),
312 (void *)rel->r_offset, (void *)load_ptr(where),
313 obj->strtab + obj->symtab[symnum].st_name));
314 _rtld_error("%s: Unsupported relocation type %ld "
315 "in non-PLT relocations",
316 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
317 return -1;
318 }
319 }
320
321 return 0;
322 }
323
324 int
325 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
326 {
327 /* PLT fixups were done above in the GOT relocation. */
328 return 0;
329 }
330
331 static inline int
332 _rtld_relocate_plt_object(const Obj_Entry *obj, Elf_Word sym, Elf_Addr *tp)
333 {
334 Elf_Addr *got = obj->pltgot;
335 const Elf_Sym *def;
336 const Obj_Entry *defobj;
337 Elf_Addr new_value;
338
339 def = _rtld_find_symdef(sym, obj, &defobj, true);
340 if (def == NULL)
341 return -1;
342
343 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
344 rdbg(("bind now/fixup in %s --> new=%p",
345 defobj->strtab + def->st_name, (void *)new_value));
346 got[obj->local_gotno + sym - obj->gotsym] = new_value;
347
348 if (tp)
349 *tp = new_value;
350 return 0;
351 }
352
353 caddr_t
354 _rtld_bind(Elf_Word a0, Elf_Addr a1, Elf_Addr a2, Elf_Addr a3)
355 {
356 Elf_Addr *got = (Elf_Addr *)(a2 - 0x7ff0);
357 const Obj_Entry *obj = (Obj_Entry *)(got[1] & 0x7fffffff);
358 Elf_Addr new_value;
359 int err;
360
361 err = _rtld_relocate_plt_object(obj, a0, &new_value);
362 if (err || new_value == 0)
363 _rtld_die();
364
365 return (caddr_t)new_value;
366 }
367
368 int
369 _rtld_relocate_plt_objects(const Obj_Entry *obj)
370 {
371 const Elf_Sym *sym = obj->symtab + obj->gotsym;
372 Elf_Word i;
373
374 for (i = obj->gotsym; i < obj->symtabno; i++, sym++) {
375 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
376 if (_rtld_relocate_plt_object(obj, i, NULL) < 0)
377 return -1;
378 }
379
380 return 0;
381 }
382