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