mips_reloc.c revision 1.34 1 /* $NetBSD: mips_reloc.c,v 1.34 2002/09/25 03:52:06 mycroft 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
112 i = (got[1] & 0x80000000) ? 2 : 1;
113 /* Relocate the local GOT entries */
114 got += i;
115 for (; i < local_gotno; i++)
116 *got++ += relocbase;
117 sym = symtab + gotsym;
118 /* Now do the global GOT entries */
119 for (i = gotsym; i < symtabno; i++) {
120 *got = sym->st_value + relocbase;
121 ++sym;
122 ++got;
123 }
124
125 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
126 for (; rel < rellim; rel++) {
127 where = (Elf_Addr *)(relocbase + rel->r_offset);
128
129 switch (ELF_R_TYPE(rel->r_info)) {
130 case R_TYPE(NONE):
131 break;
132
133 case R_TYPE(REL32):
134 assert(ELF_R_SYM(rel->r_info) < gotsym);
135 sym = symtab + ELF_R_SYM(rel->r_info);
136 assert(sym->st_info == STT_SECTION);
137 *where += (Elf_Addr)(sym->st_value + relocbase);
138 break;
139
140 default:
141 abort();
142 }
143 }
144 }
145
146 /*
147 * It is possible for the compiler to emit relocations for unaligned data.
148 * We handle this situation with these inlines.
149 */
150 #define RELOC_ALIGNED_P(x) \
151 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
152
153 static __inline Elf_Addr
154 load_ptr(void *where)
155 {
156 Elf_Addr res;
157
158 memcpy(&res, where, sizeof(res));
159
160 return (res);
161 }
162
163 static __inline void
164 store_ptr(void *where, Elf_Addr val)
165 {
166
167 memcpy(where, &val, sizeof(val));
168 }
169
170 int
171 _rtld_relocate_nonplt_objects(obj, self)
172 const Obj_Entry *obj;
173 bool self;
174 {
175 const Elf_Rel *rel;
176 Elf_Addr *got = obj->pltgot;
177 const Elf_Sym *sym, *def;
178 const Obj_Entry *defobj;
179 int i;
180
181 if (self)
182 return 0;
183
184 i = (got[1] & 0x80000000) ? 2 : 1;
185 /* Relocate the local GOT entries */
186 got += i;
187 for (; i < obj->local_gotno; i++)
188 *got++ += (Elf_Addr)obj->relocbase;
189 sym = obj->symtab + obj->gotsym;
190 /* Now do the global GOT entries */
191 for (i = obj->gotsym; i < obj->symtabno; i++) {
192 rdbg((" doing got %d sym %p (%s, %x)", i - obj->gotsym, sym,
193 sym->st_name + obj->strtab, *got));
194
195 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
196 sym->st_shndx == SHN_UNDEF) {
197 /*
198 * XXX DANGER WILL ROBINSON!
199 * You might think this is stupid, as it intentionally
200 * defeats lazy binding -- and you'd be right.
201 * Unfortunately, for lazy binding to work right, we
202 * need to a way to force the GOT slots used for
203 * function pointers to be resolved immediately. This
204 * is supposed to be done automatically by the linker,
205 * by not outputting a PLT slot and setting st_value
206 * to 0, but GNU ld does not do so reliably.
207 */
208 def = _rtld_find_symdef(i, obj, &defobj, true);
209 if (def == NULL)
210 return -1;
211 *got = def->st_value + (Elf_Addr)defobj->relocbase;
212 } else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
213 sym->st_value != 0) {
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 *got = sym->st_value + (Elf_Addr)obj->relocbase;
220 } else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
221 /* Symbols with index SHN_ABS are not relocated. */
222 if (sym->st_shndx != SHN_ABS)
223 *got = sym->st_value +
224 (Elf_Addr)obj->relocbase;
225 } else {
226 def = _rtld_find_symdef(i, obj, &defobj, true);
227 if (def == NULL)
228 return -1;
229 *got = def->st_value + (Elf_Addr)defobj->relocbase;
230 }
231
232 rdbg((" --> now %x", *got));
233 ++sym;
234 ++got;
235 }
236
237 got = obj->pltgot;
238 for (rel = obj->rel; rel < obj->rellim; rel++) {
239 Elf_Addr *where, tmp;
240 unsigned long symnum;
241
242 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
243 symnum = ELF_R_SYM(rel->r_info);
244
245 switch (ELF_R_TYPE(rel->r_info)) {
246 case R_TYPE(NONE):
247 break;
248
249 case R_TYPE(REL32):
250 /* 32-bit PC-relative reference */
251 def = obj->symtab + symnum;
252
253 if (symnum >= obj->gotsym) {
254 tmp = *where;
255 tmp += got[obj->local_gotno + symnum - obj->gotsym];
256 *where = tmp;
257
258 rdbg(("REL32/G %s in %s --> %p in %s",
259 obj->strtab + def->st_name, obj->path,
260 (void *)tmp, obj->path));
261 break;
262 } else {
263 /*
264 * XXX: ABI DIFFERENCE!
265 *
266 * Old NetBSD binutils would generate shared
267 * libs with section-relative relocations being
268 * already adjusted for the start address of
269 * the section.
270 *
271 * New binutils, OTOH, generate shared libs
272 * with the same relocations being based at
273 * zero, so we need to add in the start address
274 * of the section.
275 *
276 * We assume that all section-relative relocs
277 * with contents less than the start of the
278 * section need to be adjusted; this should
279 * work with both old and new shlibs.
280 *
281 * --rkb, Oct 6, 2001
282 */
283 tmp = *where;
284
285 if (def->st_info == STT_SECTION &&
286 tmp < def->st_value)
287 tmp += (Elf_Addr)def->st_value;
288
289 tmp += (Elf_Addr)obj->relocbase;
290 *where = tmp;
291
292 rdbg(("REL32 %s in %s --> %p in %s",
293 obj->strtab + def->st_name, obj->path,
294 (void *)tmp, obj->path));
295 }
296 break;
297
298 default:
299 rdbg(("sym = %lu, type = %lu, offset = %p, "
300 "contents = %p, symbol = %s",
301 symnum, (u_long)ELF_R_TYPE(rel->r_info),
302 (void *)rel->r_offset, (void *)load_ptr(where),
303 obj->strtab + obj->symtab[symnum].st_name));
304 _rtld_error("%s: Unsupported relocation type %ld "
305 "in non-PLT relocations\n",
306 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
307 return -1;
308 }
309 }
310
311 return 0;
312 }
313
314 int
315 _rtld_relocate_plt_lazy(obj)
316 const Obj_Entry *obj;
317 {
318 /* PLT fixups were done above in the GOT relocation. */
319 return 0;
320 }
321