mips_reloc.c revision 1.18 1 /* $NetBSD: mips_reloc.c,v 1.18 2002/09/12 18:21:18 mycroft Exp $ */
2
3 /*
4 * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31 #include <stdarg.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34
35 #include "debug.h"
36 #include "rtld.h"
37
38 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
39 caddr_t _rtld_bind_mips(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
40
41 caddr_t
42 _rtld_bind_mips(a0, a1, a2, a3)
43 Elf_Word a0;
44 Elf_Addr a1, a2, a3;
45 {
46 Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
47 const Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
48 const Elf_Sym *def;
49 const Obj_Entry *defobj;
50
51 def = _rtld_find_symdef(a0, obj, &defobj, true);
52 if (def) {
53 u[obj->local_gotno + a0 - obj->gotsym] = (Elf_Addr)
54 (def->st_value + defobj->relocbase);
55 return((caddr_t)(def->st_value + defobj->relocbase));
56 }
57
58 return(NULL); /* XXX */
59 }
60
61 void
62 _rtld_setup_pltgot(obj)
63 const Obj_Entry *obj;
64 {
65 obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
66 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
67 obj->pltgot[1] |= (Elf_Addr) obj;
68 }
69
70 void
71 _rtld_relocate_nonplt_self(dynp, relocbase)
72 Elf_Dyn *dynp;
73 Elf_Addr relocbase;
74 {
75 const Elf_Rel *rel = 0, *rellim;
76 Elf_Addr relsz = 0;
77 Elf_Addr *where;
78 const Elf_Sym *symtab, *sym;
79 Elf_Addr *got;
80 Elf_Word local_gotno, symtabno, gotsym;
81 int i;
82
83 for (; dynp->d_tag != DT_NULL; dynp++) {
84 switch (dynp->d_tag) {
85 case DT_REL:
86 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
87 break;
88 case DT_RELSZ:
89 relsz = dynp->d_un.d_val;
90 break;
91 case DT_SYMTAB:
92 symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
93 break;
94 case DT_PLTGOT:
95 got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
96 break;
97 case DT_MIPS_LOCAL_GOTNO:
98 local_gotno = dynp->d_un.d_val;
99 break;
100 case DT_MIPS_SYMTABNO:
101 symtabno = dynp->d_un.d_val;
102 break;
103 case DT_MIPS_GOTSYM:
104 gotsym = dynp->d_un.d_val;
105 break;
106 }
107 }
108 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
109 for (; rel < rellim; rel++) {
110 where = (Elf_Addr *)(relocbase + rel->r_offset);
111
112 switch (ELF_R_TYPE(rel->r_info)) {
113 case R_TYPE(NONE):
114 break;
115
116 case R_TYPE(REL32):
117 sym = symtab + ELF_R_SYM(rel->r_info);
118 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL &&
119 ELF_ST_TYPE(sym->st_info) == STT_SECTION)
120 *where += (Elf_Addr)(sym->st_value + relocbase);
121 else
122 abort();
123 break;
124
125 default:
126 abort();
127 }
128 }
129
130 i = (got[1] & 0x80000000) ? 2 : 1;
131 /* Relocate the local GOT entries */
132 while (i < local_gotno)
133 got[i++] += relocbase;
134 got += local_gotno;
135 sym = symtab + gotsym;
136 /* Now do the global GOT entries */
137 for (i = gotsym; i < symtabno; i++) {
138 if (sym->st_shndx == SHN_UNDEF ||
139 sym->st_shndx == SHN_COMMON) {
140 *got = sym->st_value + relocbase;
141 } else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
142 *got != sym->st_value) {
143 *got += relocbase;
144 } else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
145 ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
146 if (sym->st_shndx == SHN_ABS)
147 *got = sym->st_value + relocbase;
148 /* else SGI stuff ignored */
149 } else
150 *got = sym->st_value + relocbase;
151 ++sym;
152 ++got;
153 }
154 }
155
156 int
157 _rtld_relocate_nonplt_objects(obj, self, dodebug)
158 const Obj_Entry *obj;
159 bool self;
160 bool dodebug;
161 {
162 const Elf_Rel *rel;
163 Elf_Addr *got = obj->pltgot;
164 const Elf_Sym *sym = obj->symtab;
165 const Elf_Sym *def;
166 const Obj_Entry *defobj;
167 int i;
168
169 if (self)
170 return 0;
171
172 for (rel = obj->rel; rel < obj->rellim; rel++) {
173 Elf_Addr *where;
174 const Elf_Sym *def;
175 const Obj_Entry *defobj;
176 unsigned long symnum;
177
178 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
179 symnum = ELF_R_SYM(rel->r_info);
180
181 switch (ELF_R_TYPE(rel->r_info)) {
182 case R_TYPE(NONE):
183 break;
184
185 case R_TYPE(REL32):
186 /* 32-bit PC-relative reference */
187 def = obj->symtab + symnum;
188
189 if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
190 (ELF_ST_TYPE(def->st_info) == STT_SECTION ||
191 ELF_ST_TYPE(def->st_info) == STT_NOTYPE)) {
192 /*
193 * XXX: ABI DIFFERENCE!
194 *
195 * Old NetBSD binutils would generate shared
196 * libs with section-relative relocations being
197 * already adjusted for the start address of
198 * the section.
199 *
200 * New binutils, OTOH, generate shared libs
201 * with the same relocations being based at
202 * zero, so we need to add in the start address
203 * of the section.
204 *
205 * We assume that all section-relative relocs
206 * with contents less than the start of the
207 * section need to be adjusted; this should
208 * work with both old and new shlibs.
209 *
210 * --rkb, Oct 6, 2001
211 */
212 if (def->st_info == STT_SECTION &&
213 *where < def->st_value)
214 *where += (Elf_Addr)def->st_value;
215
216 *where += (Elf_Addr)obj->relocbase;
217
218 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
219 obj->strtab + def->st_name, obj->path,
220 (void *)*where, obj->path));
221 } else {
222 /* XXX maybe do something re: bootstrapping? */
223 def = _rtld_find_symdef(symnum, obj, &defobj,
224 false);
225 if (def == NULL)
226 return -1;
227 *where += (Elf_Addr)(defobj->relocbase +
228 def->st_value);
229 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
230 obj->strtab + obj->symtab[symnum].st_name,
231 obj->path, (void *)*where, defobj->path));
232 }
233 break;
234
235 default:
236 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
237 "contents = %p, symbol = %s",
238 symnum, (u_long)ELF_R_TYPE(rel->r_info),
239 (void *)rel->r_offset, (void *)*where,
240 obj->strtab + obj->symtab[symnum].st_name));
241 _rtld_error("%s: Unsupported relocation type %ld "
242 "in non-PLT relocations\n",
243 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
244 return -1;
245 }
246 }
247
248 i = (got[1] & 0x80000000) ? 2 : 1;
249 /* Relocate the local GOT entries */
250 while (i < obj->local_gotno)
251 got[i++] += (Elf_Word)obj->relocbase;
252 got += obj->local_gotno;
253 sym += obj->gotsym;
254 /* Now do the global GOT entries */
255 for (i = obj->gotsym; i < obj->symtabno; i++) {
256 rdbg(1, (" doing got %d sym %p (%s, %x)", i - obj->gotsym,
257 sym, sym->st_name + obj->strtab, *got));
258
259 def = _rtld_find_symdef(i, obj, &defobj, true);
260 if (def == NULL)
261 _rtld_error(
262 "%s: Undefined PLT symbol \"%s\" (section type = %ld, symnum = %ld)",
263 obj->path, sym->st_name + obj->strtab,
264 (u_long) ELF_ST_TYPE(sym->st_info), (u_long) i);
265 else {
266 if (sym->st_shndx == SHN_UNDEF) {
267 #if 0 /* These don't seem to work? */
268
269 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC) {
270 if (sym->st_value)
271 *got = sym->st_value +
272 (Elf_Word)obj->relocbase;
273 else
274 *got = def->st_value +
275 (Elf_Word)defobj->relocbase;
276 } else
277 #endif
278 *got = def->st_value +
279 (Elf_Word)defobj->relocbase;
280 } else if (sym->st_shndx == SHN_COMMON) {
281 *got = def->st_value +
282 (Elf_Word)defobj->relocbase;
283 } else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
284 *got != sym->st_value) {
285 *got += (Elf_Word)obj->relocbase;
286 } else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
287 ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
288 if (sym->st_shndx == SHN_ABS)
289 *got = sym->st_value +
290 (Elf_Word)obj->relocbase;
291 /* else SGI stuff ignored */
292 } else
293 *got = def->st_value +
294 (Elf_Word)defobj->relocbase;
295 }
296
297 ++sym;
298 ++got;
299 }
300
301 return 0;
302 }
303
304 int
305 _rtld_relocate_plt_lazy(obj, dodebug)
306 const Obj_Entry *obj;
307 bool dodebug;
308 {
309 const Elf_Rel *rel;
310
311 if (!obj->isdynamic)
312 return 0;
313
314 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
315 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
316
317 /* Just relocate the GOT slots pointing into the PLT */
318 *where += (Elf_Addr)obj->relocbase;
319 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
320 (void *)*where));
321 }
322
323 return 0;
324 }
325
326 int
327 _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
328 const Obj_Entry *obj;
329 const Elf_Rela *rela;
330 caddr_t *addrp;
331 bool dodebug;
332 {
333 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
334 Elf_Addr new_value;
335
336 /* Fully resolve procedure addresses now */
337
338 if (obj->isdynamic) {
339 /* Just relocate the GOT slots pointing into the PLT */
340 new_value = *where + (Elf_Addr)(obj->relocbase);
341 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
342 (void *)*where));
343 } else {
344 return 0;
345 }
346 if (*where != new_value)
347 *where = new_value;
348
349 *addrp = (caddr_t)new_value;
350 return 0;
351 }
352