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