mips_reloc.c revision 1.42 1 /* $NetBSD: mips_reloc.c,v 1.42 2003/09/24 09:59:45 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, because it can screw up
214 * link ordering rules if the symbol is defined in
215 * more than one module. For now, if there is a
216 * definition, we fail the test above and force a full
217 * symbol lookup. Unfortunately, a similar problem
218 * also seems to happen with references to data items,
219 * and we can't fix that here. - mycroft, 2003/09/24
220 */
221 *got = sym->st_value + (Elf_Addr)obj->relocbase;
222 } else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
223 /* Symbols with index SHN_ABS are not relocated. */
224 if (sym->st_shndx != SHN_ABS)
225 *got = sym->st_value +
226 (Elf_Addr)obj->relocbase;
227 } else {
228 def = _rtld_find_symdef(i, obj, &defobj, false);
229 if (def == NULL)
230 return -1;
231 *got = def->st_value + (Elf_Addr)defobj->relocbase;
232 }
233
234 rdbg((" --> now %x", *got));
235 ++sym;
236 ++got;
237 }
238
239 got = obj->pltgot;
240 for (rel = obj->rel; rel < obj->rellim; rel++) {
241 Elf_Addr *where, tmp;
242 unsigned long symnum;
243
244 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
245 symnum = ELF_R_SYM(rel->r_info);
246
247 switch (ELF_R_TYPE(rel->r_info)) {
248 case R_TYPE(NONE):
249 break;
250
251 case R_TYPE(REL32):
252 /* 32-bit PC-relative reference */
253 def = obj->symtab + symnum;
254
255 if (symnum >= obj->gotsym) {
256 tmp = *where;
257 tmp += got[obj->local_gotno + symnum - obj->gotsym];
258 *where = tmp;
259
260 rdbg(("REL32/G %s in %s --> %p in %s",
261 obj->strtab + def->st_name, obj->path,
262 (void *)tmp, obj->path));
263 break;
264 } else {
265 /*
266 * XXX: ABI DIFFERENCE!
267 *
268 * Old NetBSD binutils would generate shared
269 * libs with section-relative relocations being
270 * already adjusted for the start address of
271 * the section.
272 *
273 * New binutils, OTOH, generate shared libs
274 * with the same relocations being based at
275 * zero, so we need to add in the start address
276 * of the section.
277 *
278 * --rkb, Oct 6, 2001
279 */
280 tmp = *where;
281
282 if (def->st_info ==
283 ELF_ST_INFO(STB_LOCAL, STT_SECTION)
284 #ifdef SUPPORT_OLD_BROKEN_LD
285 && !broken
286 #endif
287 )
288 tmp += (Elf_Addr)def->st_value;
289
290 tmp += (Elf_Addr)obj->relocbase;
291 *where = tmp;
292
293 rdbg(("REL32/L %s in %s --> %p in %s",
294 obj->strtab + def->st_name, obj->path,
295 (void *)tmp, obj->path));
296 }
297 break;
298
299 default:
300 rdbg(("sym = %lu, type = %lu, offset = %p, "
301 "contents = %p, symbol = %s",
302 symnum, (u_long)ELF_R_TYPE(rel->r_info),
303 (void *)rel->r_offset, (void *)load_ptr(where),
304 obj->strtab + obj->symtab[symnum].st_name));
305 _rtld_error("%s: Unsupported relocation type %ld "
306 "in non-PLT relocations\n",
307 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
308 return -1;
309 }
310 }
311
312 return 0;
313 }
314
315 int
316 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
317 {
318 /* PLT fixups were done above in the GOT relocation. */
319 return 0;
320 }
321
322 caddr_t
323 _rtld_bind(Elf_Word a0, Elf_Addr a1, Elf_Addr a2, Elf_Addr a3)
324 {
325 Elf_Addr *got = (Elf_Addr *)(a2 - 0x7ff0);
326 const Obj_Entry *obj = (Obj_Entry *)(got[1] & 0x7fffffff);
327 const Elf_Sym *def;
328 const Obj_Entry *defobj;
329 Elf_Addr new_value;
330
331 def = _rtld_find_symdef(a0, obj, &defobj, true);
332 if (def == NULL)
333 _rtld_die();
334
335 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
336 rdbg(("bind now/fixup in %s --> new=%p",
337 defobj->strtab + def->st_name, (void *)new_value));
338 got[obj->local_gotno + a0 - obj->gotsym] = new_value;
339 return ((caddr_t)new_value);
340 }
341