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