mips_reloc.c revision 1.50 1 1.50 skrll /* $NetBSD: mips_reloc.c,v 1.50 2006/03/04 08:58:46 skrll Exp $ */
2 1.1 mhitch
3 1.1 mhitch /*
4 1.1 mhitch * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
5 1.37 mycroft * Portions copyright 2002 Charles M. Hannum <root (at) ihack.net>
6 1.1 mhitch * All rights reserved.
7 1.1 mhitch *
8 1.1 mhitch * Redistribution and use in source and binary forms, with or without
9 1.1 mhitch * modification, are permitted provided that the following conditions
10 1.1 mhitch * are met:
11 1.1 mhitch * 1. Redistributions of source code must retain the above copyright
12 1.1 mhitch * notice, this list of conditions and the following disclaimer.
13 1.1 mhitch * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 mhitch * notice, this list of conditions and the following disclaimer in the
15 1.1 mhitch * documentation and/or other materials provided with the distribution.
16 1.1 mhitch * 3. The name of the author may not be used to endorse or promote products
17 1.1 mhitch * derived from this software without specific prior written permission.
18 1.1 mhitch *
19 1.1 mhitch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 mhitch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 mhitch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 mhitch * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 mhitch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 mhitch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 mhitch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 mhitch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 mhitch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 mhitch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 mhitch */
30 1.1 mhitch
31 1.48 skrll #include <sys/cdefs.h>
32 1.48 skrll #ifndef lint
33 1.50 skrll __RCSID("$NetBSD: mips_reloc.c,v 1.50 2006/03/04 08:58:46 skrll Exp $");
34 1.48 skrll #endif /* not lint */
35 1.48 skrll
36 1.1 mhitch #include <sys/types.h>
37 1.3 mycroft #include <sys/stat.h>
38 1.40 mrg
39 1.40 mrg #include <stdlib.h>
40 1.33 thorpej #include <string.h>
41 1.1 mhitch
42 1.1 mhitch #include "debug.h"
43 1.1 mhitch #include "rtld.h"
44 1.1 mhitch
45 1.36 mycroft #define SUPPORT_OLD_BROKEN_LD
46 1.36 mycroft
47 1.22 mycroft void _rtld_bind_start(void);
48 1.17 mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
49 1.36 mycroft caddr_t _rtld_bind(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
50 1.6 mycroft
51 1.45 simonb /*
52 1.45 simonb * It is possible for the compiler to emit relocations for unaligned data.
53 1.45 simonb * We handle this situation with these inlines.
54 1.45 simonb */
55 1.45 simonb #define RELOC_ALIGNED_P(x) \
56 1.45 simonb (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
57 1.45 simonb
58 1.49 perry static inline Elf_Addr
59 1.45 simonb load_ptr(void *where)
60 1.45 simonb {
61 1.45 simonb Elf_Addr res;
62 1.45 simonb
63 1.45 simonb memcpy(&res, where, sizeof(res));
64 1.45 simonb
65 1.45 simonb return res;
66 1.45 simonb }
67 1.45 simonb
68 1.49 perry static inline void
69 1.45 simonb store_ptr(void *where, Elf_Addr val)
70 1.45 simonb {
71 1.45 simonb
72 1.45 simonb memcpy(where, &val, sizeof(val));
73 1.45 simonb }
74 1.45 simonb
75 1.45 simonb
76 1.1 mhitch void
77 1.39 skrll _rtld_setup_pltgot(const Obj_Entry *obj)
78 1.1 mhitch {
79 1.6 mycroft obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
80 1.6 mycroft /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
81 1.6 mycroft obj->pltgot[1] |= (Elf_Addr) obj;
82 1.8 mycroft }
83 1.8 mycroft
84 1.17 mycroft void
85 1.39 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
86 1.17 mycroft {
87 1.17 mycroft const Elf_Rel *rel = 0, *rellim;
88 1.17 mycroft Elf_Addr relsz = 0;
89 1.17 mycroft Elf_Addr *where;
90 1.47 he const Elf_Sym *symtab = NULL, *sym;
91 1.47 he Elf_Addr *got = NULL;
92 1.47 he Elf_Word local_gotno = 0, symtabno = 0, gotsym = 0;
93 1.18 mycroft int i;
94 1.17 mycroft
95 1.17 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
96 1.17 mycroft switch (dynp->d_tag) {
97 1.17 mycroft case DT_REL:
98 1.17 mycroft rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
99 1.17 mycroft break;
100 1.17 mycroft case DT_RELSZ:
101 1.17 mycroft relsz = dynp->d_un.d_val;
102 1.17 mycroft break;
103 1.17 mycroft case DT_SYMTAB:
104 1.17 mycroft symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
105 1.17 mycroft break;
106 1.18 mycroft case DT_PLTGOT:
107 1.18 mycroft got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
108 1.18 mycroft break;
109 1.18 mycroft case DT_MIPS_LOCAL_GOTNO:
110 1.18 mycroft local_gotno = dynp->d_un.d_val;
111 1.18 mycroft break;
112 1.18 mycroft case DT_MIPS_SYMTABNO:
113 1.18 mycroft symtabno = dynp->d_un.d_val;
114 1.18 mycroft break;
115 1.18 mycroft case DT_MIPS_GOTSYM:
116 1.18 mycroft gotsym = dynp->d_un.d_val;
117 1.18 mycroft break;
118 1.17 mycroft }
119 1.17 mycroft }
120 1.34 mycroft
121 1.34 mycroft i = (got[1] & 0x80000000) ? 2 : 1;
122 1.34 mycroft /* Relocate the local GOT entries */
123 1.34 mycroft got += i;
124 1.34 mycroft for (; i < local_gotno; i++)
125 1.34 mycroft *got++ += relocbase;
126 1.34 mycroft sym = symtab + gotsym;
127 1.34 mycroft /* Now do the global GOT entries */
128 1.34 mycroft for (i = gotsym; i < symtabno; i++) {
129 1.34 mycroft *got = sym->st_value + relocbase;
130 1.34 mycroft ++sym;
131 1.34 mycroft ++got;
132 1.34 mycroft }
133 1.34 mycroft
134 1.17 mycroft rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
135 1.17 mycroft for (; rel < rellim; rel++) {
136 1.17 mycroft where = (Elf_Addr *)(relocbase + rel->r_offset);
137 1.17 mycroft
138 1.17 mycroft switch (ELF_R_TYPE(rel->r_info)) {
139 1.17 mycroft case R_TYPE(NONE):
140 1.17 mycroft break;
141 1.17 mycroft
142 1.17 mycroft case R_TYPE(REL32):
143 1.34 mycroft assert(ELF_R_SYM(rel->r_info) < gotsym);
144 1.18 mycroft sym = symtab + ELF_R_SYM(rel->r_info);
145 1.46 skrll assert(ELF_ST_BIND(sym->st_info) == STB_LOCAL);
146 1.45 simonb if (__predict_true(RELOC_ALIGNED_P(where)))
147 1.46 skrll *where += relocbase;
148 1.45 simonb else
149 1.46 skrll store_ptr(where, load_ptr(where) + relocbase);
150 1.17 mycroft break;
151 1.17 mycroft
152 1.17 mycroft default:
153 1.17 mycroft abort();
154 1.17 mycroft }
155 1.17 mycroft }
156 1.17 mycroft }
157 1.17 mycroft
158 1.8 mycroft int
159 1.39 skrll _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
160 1.8 mycroft {
161 1.9 mycroft const Elf_Rel *rel;
162 1.18 mycroft Elf_Addr *got = obj->pltgot;
163 1.19 mycroft const Elf_Sym *sym, *def;
164 1.18 mycroft const Obj_Entry *defobj;
165 1.18 mycroft int i;
166 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
167 1.36 mycroft int broken;
168 1.36 mycroft #endif
169 1.36 mycroft
170 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
171 1.36 mycroft broken = 0;
172 1.36 mycroft sym = obj->symtab;
173 1.36 mycroft for (i = 1; i < 12; i++)
174 1.36 mycroft if (sym[i].st_info == ELF_ST_INFO(STB_LOCAL, STT_NOTYPE))
175 1.36 mycroft broken = 1;
176 1.36 mycroft dbg(("%s: broken=%d", obj->path, broken));
177 1.36 mycroft #endif
178 1.17 mycroft
179 1.34 mycroft i = (got[1] & 0x80000000) ? 2 : 1;
180 1.34 mycroft /* Relocate the local GOT entries */
181 1.34 mycroft got += i;
182 1.34 mycroft for (; i < obj->local_gotno; i++)
183 1.34 mycroft *got++ += (Elf_Addr)obj->relocbase;
184 1.34 mycroft sym = obj->symtab + obj->gotsym;
185 1.34 mycroft /* Now do the global GOT entries */
186 1.34 mycroft for (i = obj->gotsym; i < obj->symtabno; i++) {
187 1.34 mycroft rdbg((" doing got %d sym %p (%s, %x)", i - obj->gotsym, sym,
188 1.34 mycroft sym->st_name + obj->strtab, *got));
189 1.34 mycroft
190 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
191 1.34 mycroft if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
192 1.36 mycroft broken && sym->st_shndx == SHN_UNDEF) {
193 1.34 mycroft /*
194 1.34 mycroft * XXX DANGER WILL ROBINSON!
195 1.34 mycroft * You might think this is stupid, as it intentionally
196 1.34 mycroft * defeats lazy binding -- and you'd be right.
197 1.34 mycroft * Unfortunately, for lazy binding to work right, we
198 1.34 mycroft * need to a way to force the GOT slots used for
199 1.34 mycroft * function pointers to be resolved immediately. This
200 1.34 mycroft * is supposed to be done automatically by the linker,
201 1.34 mycroft * by not outputting a PLT slot and setting st_value
202 1.36 mycroft * to 0 if there are non-PLT references, but older
203 1.36 mycroft * versions of GNU ld do not do this.
204 1.34 mycroft */
205 1.38 skrll def = _rtld_find_symdef(i, obj, &defobj, false);
206 1.34 mycroft if (def == NULL)
207 1.34 mycroft return -1;
208 1.34 mycroft *got = def->st_value + (Elf_Addr)defobj->relocbase;
209 1.36 mycroft } else
210 1.36 mycroft #endif
211 1.36 mycroft if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
212 1.41 mycroft sym->st_value != 0 && sym->st_shndx == SHN_UNDEF) {
213 1.34 mycroft /*
214 1.34 mycroft * If there are non-PLT references to the function,
215 1.34 mycroft * st_value should be 0, forcing us to resolve the
216 1.34 mycroft * address immediately.
217 1.41 mycroft *
218 1.41 mycroft * XXX DANGER WILL ROBINSON!
219 1.41 mycroft * The linker is not outputting PLT slots for calls to
220 1.41 mycroft * functions that are defined in the same shared
221 1.42 mycroft * library. This is a bug, because it can screw up
222 1.42 mycroft * link ordering rules if the symbol is defined in
223 1.42 mycroft * more than one module. For now, if there is a
224 1.42 mycroft * definition, we fail the test above and force a full
225 1.44 mycroft * symbol lookup. This means that all intra-module
226 1.44 mycroft * calls are bound immediately. - mycroft, 2003/09/24
227 1.34 mycroft */
228 1.34 mycroft *got = sym->st_value + (Elf_Addr)obj->relocbase;
229 1.34 mycroft } else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
230 1.34 mycroft /* Symbols with index SHN_ABS are not relocated. */
231 1.34 mycroft if (sym->st_shndx != SHN_ABS)
232 1.34 mycroft *got = sym->st_value +
233 1.34 mycroft (Elf_Addr)obj->relocbase;
234 1.34 mycroft } else {
235 1.38 skrll def = _rtld_find_symdef(i, obj, &defobj, false);
236 1.34 mycroft if (def == NULL)
237 1.34 mycroft return -1;
238 1.34 mycroft *got = def->st_value + (Elf_Addr)defobj->relocbase;
239 1.34 mycroft }
240 1.34 mycroft
241 1.34 mycroft rdbg((" --> now %x", *got));
242 1.34 mycroft ++sym;
243 1.34 mycroft ++got;
244 1.34 mycroft }
245 1.34 mycroft
246 1.34 mycroft got = obj->pltgot;
247 1.9 mycroft for (rel = obj->rel; rel < obj->rellim; rel++) {
248 1.33 thorpej Elf_Addr *where, tmp;
249 1.10 mycroft unsigned long symnum;
250 1.9 mycroft
251 1.9 mycroft where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
252 1.10 mycroft symnum = ELF_R_SYM(rel->r_info);
253 1.9 mycroft
254 1.9 mycroft switch (ELF_R_TYPE(rel->r_info)) {
255 1.9 mycroft case R_TYPE(NONE):
256 1.9 mycroft break;
257 1.9 mycroft
258 1.9 mycroft case R_TYPE(REL32):
259 1.9 mycroft /* 32-bit PC-relative reference */
260 1.10 mycroft def = obj->symtab + symnum;
261 1.9 mycroft
262 1.34 mycroft if (symnum >= obj->gotsym) {
263 1.45 simonb if (__predict_true(RELOC_ALIGNED_P(where)))
264 1.45 simonb tmp = *where;
265 1.45 simonb else
266 1.45 simonb tmp = load_ptr(where);
267 1.34 mycroft tmp += got[obj->local_gotno + symnum - obj->gotsym];
268 1.45 simonb if (__predict_true(RELOC_ALIGNED_P(where)))
269 1.45 simonb *where = tmp;
270 1.45 simonb else
271 1.45 simonb store_ptr(where, tmp);
272 1.34 mycroft
273 1.34 mycroft rdbg(("REL32/G %s in %s --> %p in %s",
274 1.34 mycroft obj->strtab + def->st_name, obj->path,
275 1.34 mycroft (void *)tmp, obj->path));
276 1.34 mycroft break;
277 1.34 mycroft } else {
278 1.9 mycroft /*
279 1.9 mycroft * XXX: ABI DIFFERENCE!
280 1.9 mycroft *
281 1.9 mycroft * Old NetBSD binutils would generate shared
282 1.9 mycroft * libs with section-relative relocations being
283 1.9 mycroft * already adjusted for the start address of
284 1.9 mycroft * the section.
285 1.9 mycroft *
286 1.9 mycroft * New binutils, OTOH, generate shared libs
287 1.9 mycroft * with the same relocations being based at
288 1.9 mycroft * zero, so we need to add in the start address
289 1.9 mycroft * of the section.
290 1.9 mycroft *
291 1.9 mycroft * --rkb, Oct 6, 2001
292 1.9 mycroft */
293 1.45 simonb if (__predict_true(RELOC_ALIGNED_P(where)))
294 1.45 simonb tmp = *where;
295 1.45 simonb else
296 1.45 simonb tmp = load_ptr(where);
297 1.34 mycroft
298 1.35 mycroft if (def->st_info ==
299 1.36 mycroft ELF_ST_INFO(STB_LOCAL, STT_SECTION)
300 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
301 1.36 mycroft && !broken
302 1.36 mycroft #endif
303 1.36 mycroft )
304 1.34 mycroft tmp += (Elf_Addr)def->st_value;
305 1.9 mycroft
306 1.34 mycroft tmp += (Elf_Addr)obj->relocbase;
307 1.45 simonb if (__predict_true(RELOC_ALIGNED_P(where)))
308 1.45 simonb *where = tmp;
309 1.45 simonb else
310 1.45 simonb store_ptr(where, tmp);
311 1.9 mycroft
312 1.35 mycroft rdbg(("REL32/L %s in %s --> %p in %s",
313 1.9 mycroft obj->strtab + def->st_name, obj->path,
314 1.33 thorpej (void *)tmp, obj->path));
315 1.9 mycroft }
316 1.9 mycroft break;
317 1.9 mycroft
318 1.9 mycroft default:
319 1.23 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
320 1.9 mycroft "contents = %p, symbol = %s",
321 1.10 mycroft symnum, (u_long)ELF_R_TYPE(rel->r_info),
322 1.33 thorpej (void *)rel->r_offset, (void *)load_ptr(where),
323 1.10 mycroft obj->strtab + obj->symtab[symnum].st_name));
324 1.9 mycroft _rtld_error("%s: Unsupported relocation type %ld "
325 1.9 mycroft "in non-PLT relocations\n",
326 1.9 mycroft obj->path, (u_long) ELF_R_TYPE(rel->r_info));
327 1.9 mycroft return -1;
328 1.8 mycroft }
329 1.18 mycroft }
330 1.18 mycroft
331 1.8 mycroft return 0;
332 1.8 mycroft }
333 1.8 mycroft
334 1.8 mycroft int
335 1.39 skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
336 1.13 mycroft {
337 1.31 mycroft /* PLT fixups were done above in the GOT relocation. */
338 1.28 mycroft return 0;
339 1.36 mycroft }
340 1.36 mycroft
341 1.50 skrll static inline int
342 1.50 skrll _rtld_relocate_plt_object(const Obj_Entry *obj, Elf_Word sym, Elf_Addr *tp)
343 1.36 mycroft {
344 1.50 skrll Elf_Addr *got = obj->pltgot;
345 1.36 mycroft const Elf_Sym *def;
346 1.36 mycroft const Obj_Entry *defobj;
347 1.36 mycroft Elf_Addr new_value;
348 1.36 mycroft
349 1.50 skrll def = _rtld_find_symdef(sym, obj, &defobj, true);
350 1.36 mycroft if (def == NULL)
351 1.50 skrll return -1;
352 1.36 mycroft
353 1.36 mycroft new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
354 1.36 mycroft rdbg(("bind now/fixup in %s --> new=%p",
355 1.36 mycroft defobj->strtab + def->st_name, (void *)new_value));
356 1.50 skrll got[obj->local_gotno + sym - obj->gotsym] = new_value;
357 1.50 skrll
358 1.50 skrll if (tp)
359 1.50 skrll *tp = new_value;
360 1.50 skrll return 0;
361 1.50 skrll }
362 1.50 skrll
363 1.50 skrll
364 1.50 skrll
365 1.50 skrll
366 1.50 skrll
367 1.50 skrll caddr_t
368 1.50 skrll _rtld_bind(Elf_Word a0, Elf_Addr a1, Elf_Addr a2, Elf_Addr a3)
369 1.50 skrll {
370 1.50 skrll Elf_Addr *got = (Elf_Addr *)(a2 - 0x7ff0);
371 1.50 skrll const Obj_Entry *obj = (Obj_Entry *)(got[1] & 0x7fffffff);
372 1.50 skrll Elf_Addr new_value;
373 1.50 skrll int err;
374 1.50 skrll
375 1.50 skrll err = _rtld_relocate_plt_object(obj, a0, &new_value);
376 1.50 skrll if (err)
377 1.50 skrll _rtld_die();
378 1.50 skrll
379 1.50 skrll return (caddr_t)new_value;
380 1.50 skrll }
381 1.50 skrll
382 1.50 skrll int
383 1.50 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
384 1.50 skrll {
385 1.50 skrll const Elf_Sym *sym = obj->symtab + obj->gotsym;
386 1.50 skrll int i;
387 1.50 skrll
388 1.50 skrll for (i = obj->gotsym; i < obj->symtabno; i++, sym++) {
389 1.50 skrll if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
390 1.50 skrll if (_rtld_relocate_plt_object(obj, i, NULL) < 0)
391 1.50 skrll return -1;
392 1.50 skrll }
393 1.50 skrll
394 1.50 skrll return 0;
395 1.1 mhitch }
396