mips_reloc.c revision 1.17 1 1.17 mycroft /* $NetBSD: mips_reloc.c,v 1.17 2002/09/12 17:08:32 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.1 mhitch Elf_Addr *got = obj->pltgot;
66 1.1 mhitch const Elf_Sym *sym = obj->symtab;
67 1.1 mhitch const Elf_Sym *def;
68 1.1 mhitch const Obj_Entry *defobj;
69 1.1 mhitch int i;
70 1.1 mhitch
71 1.1 mhitch i = (got[1] & 0x80000000) ? 2 : 1;
72 1.1 mhitch /* Relocate the local GOT entries */
73 1.1 mhitch while (i < obj->local_gotno)
74 1.1 mhitch got[i++] += (Elf_Word)obj->relocbase;
75 1.1 mhitch got += obj->local_gotno;
76 1.1 mhitch sym += obj->gotsym;
77 1.1 mhitch /* Now do the global GOT entries */
78 1.17 mycroft for (i = obj->gotsym; i < obj->symtabno; i++) {
79 1.17 mycroft rdbg(1, (" doing got %d sym %p (%s, %x)", i - obj->gotsym,
80 1.17 mycroft sym, sym->st_name + obj->strtab, *got));
81 1.4 rafal
82 1.17 mycroft def = _rtld_find_symdef(i, obj, &defobj, true);
83 1.4 rafal if (def == NULL)
84 1.4 rafal _rtld_error(
85 1.8 mycroft "%s: Undefined PLT symbol \"%s\" (section type = %ld, symnum = %ld)",
86 1.4 rafal obj->path, sym->st_name + obj->strtab,
87 1.17 mycroft (u_long) ELF_ST_TYPE(sym->st_info), (u_long) i);
88 1.5 rafal else {
89 1.5 rafal if (sym->st_shndx == SHN_UNDEF) {
90 1.1 mhitch #if 0 /* These don't seem to work? */
91 1.1 mhitch
92 1.8 mycroft if (ELF_ST_TYPE(sym->st_info) == STT_FUNC) {
93 1.5 rafal if (sym->st_value)
94 1.5 rafal *got = sym->st_value +
95 1.5 rafal (Elf_Word)obj->relocbase;
96 1.5 rafal else
97 1.5 rafal *got = def->st_value +
98 1.5 rafal (Elf_Word)defobj->relocbase;
99 1.5 rafal } else
100 1.5 rafal #endif
101 1.5 rafal *got = def->st_value +
102 1.5 rafal (Elf_Word)defobj->relocbase;
103 1.5 rafal } else if (sym->st_shndx == SHN_COMMON) {
104 1.5 rafal *got = def->st_value +
105 1.5 rafal (Elf_Word)defobj->relocbase;
106 1.8 mycroft } else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
107 1.5 rafal *got != sym->st_value) {
108 1.5 rafal *got += (Elf_Word)obj->relocbase;
109 1.8 mycroft } else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
110 1.8 mycroft ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
111 1.5 rafal if (sym->st_shndx == SHN_ABS)
112 1.4 rafal *got = sym->st_value +
113 1.4 rafal (Elf_Word)obj->relocbase;
114 1.5 rafal /* else SGI stuff ignored */
115 1.1 mhitch } else
116 1.1 mhitch *got = def->st_value +
117 1.1 mhitch (Elf_Word)defobj->relocbase;
118 1.5 rafal }
119 1.4 rafal
120 1.1 mhitch ++sym;
121 1.1 mhitch ++got;
122 1.1 mhitch }
123 1.1 mhitch
124 1.6 mycroft obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
125 1.6 mycroft /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
126 1.6 mycroft obj->pltgot[1] |= (Elf_Addr) obj;
127 1.8 mycroft }
128 1.8 mycroft
129 1.17 mycroft void
130 1.17 mycroft _rtld_relocate_nonplt_self(dynp, relocbase)
131 1.17 mycroft Elf_Dyn *dynp;
132 1.17 mycroft Elf_Addr relocbase;
133 1.17 mycroft {
134 1.17 mycroft const Elf_Rel *rel = 0, *rellim;
135 1.17 mycroft Elf_Addr relsz = 0;
136 1.17 mycroft Elf_Addr *where;
137 1.17 mycroft const Elf_Sym *def, *symtab;
138 1.17 mycroft
139 1.17 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
140 1.17 mycroft switch (dynp->d_tag) {
141 1.17 mycroft case DT_REL:
142 1.17 mycroft rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
143 1.17 mycroft break;
144 1.17 mycroft case DT_RELSZ:
145 1.17 mycroft relsz = dynp->d_un.d_val;
146 1.17 mycroft break;
147 1.17 mycroft case DT_SYMTAB:
148 1.17 mycroft symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
149 1.17 mycroft break;
150 1.17 mycroft }
151 1.17 mycroft }
152 1.17 mycroft rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
153 1.17 mycroft for (; rel < rellim; rel++) {
154 1.17 mycroft where = (Elf_Addr *)(relocbase + rel->r_offset);
155 1.17 mycroft
156 1.17 mycroft switch (ELF_R_TYPE(rel->r_info)) {
157 1.17 mycroft case R_TYPE(NONE):
158 1.17 mycroft break;
159 1.17 mycroft
160 1.17 mycroft case R_TYPE(REL32):
161 1.17 mycroft def = symtab + ELF_R_SYM(rel->r_info);
162 1.17 mycroft if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
163 1.17 mycroft ELF_ST_TYPE(def->st_info) == STT_SECTION) {
164 1.17 mycroft *where += (Elf_Addr)def->st_value;
165 1.17 mycroft *where += (Elf_Addr)relocbase;
166 1.17 mycroft } else
167 1.17 mycroft abort();
168 1.17 mycroft break;
169 1.17 mycroft
170 1.17 mycroft default:
171 1.17 mycroft abort();
172 1.17 mycroft }
173 1.17 mycroft }
174 1.17 mycroft }
175 1.17 mycroft
176 1.8 mycroft int
177 1.16 mycroft _rtld_relocate_nonplt_objects(obj, self, dodebug)
178 1.13 mycroft const Obj_Entry *obj;
179 1.16 mycroft bool self;
180 1.8 mycroft bool dodebug;
181 1.8 mycroft {
182 1.9 mycroft const Elf_Rel *rel;
183 1.8 mycroft
184 1.17 mycroft if (self)
185 1.17 mycroft return 0;
186 1.17 mycroft
187 1.9 mycroft for (rel = obj->rel; rel < obj->rellim; rel++) {
188 1.9 mycroft Elf_Addr *where;
189 1.9 mycroft const Elf_Sym *def;
190 1.9 mycroft const Obj_Entry *defobj;
191 1.10 mycroft unsigned long symnum;
192 1.9 mycroft
193 1.9 mycroft where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
194 1.10 mycroft symnum = ELF_R_SYM(rel->r_info);
195 1.9 mycroft
196 1.9 mycroft switch (ELF_R_TYPE(rel->r_info)) {
197 1.9 mycroft case R_TYPE(NONE):
198 1.9 mycroft break;
199 1.9 mycroft
200 1.9 mycroft case R_TYPE(REL32):
201 1.9 mycroft /* 32-bit PC-relative reference */
202 1.10 mycroft def = obj->symtab + symnum;
203 1.9 mycroft
204 1.9 mycroft if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
205 1.9 mycroft (ELF_ST_TYPE(def->st_info) == STT_SECTION ||
206 1.9 mycroft ELF_ST_TYPE(def->st_info) == STT_NOTYPE)) {
207 1.9 mycroft /*
208 1.9 mycroft * XXX: ABI DIFFERENCE!
209 1.9 mycroft *
210 1.9 mycroft * Old NetBSD binutils would generate shared
211 1.9 mycroft * libs with section-relative relocations being
212 1.9 mycroft * already adjusted for the start address of
213 1.9 mycroft * the section.
214 1.9 mycroft *
215 1.9 mycroft * New binutils, OTOH, generate shared libs
216 1.9 mycroft * with the same relocations being based at
217 1.9 mycroft * zero, so we need to add in the start address
218 1.9 mycroft * of the section.
219 1.9 mycroft *
220 1.9 mycroft * We assume that all section-relative relocs
221 1.9 mycroft * with contents less than the start of the
222 1.9 mycroft * section need to be adjusted; this should
223 1.9 mycroft * work with both old and new shlibs.
224 1.9 mycroft *
225 1.9 mycroft * --rkb, Oct 6, 2001
226 1.9 mycroft */
227 1.9 mycroft if (def->st_info == STT_SECTION &&
228 1.17 mycroft *where < def->st_value)
229 1.17 mycroft *where += (Elf_Addr)def->st_value;
230 1.9 mycroft
231 1.9 mycroft *where += (Elf_Addr)obj->relocbase;
232 1.9 mycroft
233 1.9 mycroft rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
234 1.9 mycroft obj->strtab + def->st_name, obj->path,
235 1.9 mycroft (void *)*where, obj->path));
236 1.9 mycroft } else {
237 1.9 mycroft /* XXX maybe do something re: bootstrapping? */
238 1.10 mycroft def = _rtld_find_symdef(symnum, obj, &defobj,
239 1.10 mycroft false);
240 1.9 mycroft if (def == NULL)
241 1.9 mycroft return -1;
242 1.9 mycroft *where += (Elf_Addr)(defobj->relocbase +
243 1.9 mycroft def->st_value);
244 1.9 mycroft rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
245 1.11 mycroft obj->strtab + obj->symtab[symnum].st_name,
246 1.11 mycroft obj->path, (void *)*where, defobj->path));
247 1.9 mycroft }
248 1.9 mycroft break;
249 1.9 mycroft
250 1.9 mycroft default:
251 1.9 mycroft rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
252 1.9 mycroft "contents = %p, symbol = %s",
253 1.10 mycroft symnum, (u_long)ELF_R_TYPE(rel->r_info),
254 1.9 mycroft (void *)rel->r_offset, (void *)*where,
255 1.10 mycroft obj->strtab + obj->symtab[symnum].st_name));
256 1.9 mycroft _rtld_error("%s: Unsupported relocation type %ld "
257 1.9 mycroft "in non-PLT relocations\n",
258 1.9 mycroft obj->path, (u_long) ELF_R_TYPE(rel->r_info));
259 1.9 mycroft return -1;
260 1.8 mycroft }
261 1.8 mycroft }
262 1.8 mycroft return 0;
263 1.8 mycroft }
264 1.8 mycroft
265 1.8 mycroft int
266 1.13 mycroft _rtld_relocate_plt_lazy(obj, dodebug)
267 1.13 mycroft const Obj_Entry *obj;
268 1.13 mycroft bool dodebug;
269 1.13 mycroft {
270 1.13 mycroft const Elf_Rel *rel;
271 1.13 mycroft
272 1.15 mycroft if (!obj->isdynamic)
273 1.13 mycroft return 0;
274 1.13 mycroft
275 1.13 mycroft for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
276 1.13 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
277 1.13 mycroft
278 1.13 mycroft /* Just relocate the GOT slots pointing into the PLT */
279 1.13 mycroft *where += (Elf_Addr)obj->relocbase;
280 1.13 mycroft rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
281 1.13 mycroft (void *)*where));
282 1.13 mycroft }
283 1.13 mycroft
284 1.13 mycroft return 0;
285 1.13 mycroft }
286 1.13 mycroft
287 1.13 mycroft int
288 1.12 mycroft _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
289 1.13 mycroft const Obj_Entry *obj;
290 1.8 mycroft const Elf_Rela *rela;
291 1.8 mycroft caddr_t *addrp;
292 1.8 mycroft bool dodebug;
293 1.8 mycroft {
294 1.8 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
295 1.8 mycroft Elf_Addr new_value;
296 1.8 mycroft
297 1.8 mycroft /* Fully resolve procedure addresses now */
298 1.8 mycroft
299 1.15 mycroft if (obj->isdynamic) {
300 1.8 mycroft /* Just relocate the GOT slots pointing into the PLT */
301 1.8 mycroft new_value = *where + (Elf_Addr)(obj->relocbase);
302 1.8 mycroft rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
303 1.8 mycroft (void *)*where));
304 1.8 mycroft } else {
305 1.8 mycroft return 0;
306 1.8 mycroft }
307 1.8 mycroft if (*where != new_value)
308 1.8 mycroft *where = new_value;
309 1.12 mycroft
310 1.12 mycroft *addrp = (caddr_t)new_value;
311 1.8 mycroft return 0;
312 1.1 mhitch }
313