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