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