mips_reloc.c revision 1.38 1 1.38 skrll /* $NetBSD: mips_reloc.c,v 1.38 2003/07/17 13:56:33 skrll Exp $ */
2 1.1 mhitch
3 1.1 mhitch /*
4 1.1 mhitch * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
5 1.37 mycroft * Portions copyright 2002 Charles M. Hannum <root (at) ihack.net>
6 1.1 mhitch * All rights reserved.
7 1.1 mhitch *
8 1.1 mhitch * Redistribution and use in source and binary forms, with or without
9 1.1 mhitch * modification, are permitted provided that the following conditions
10 1.1 mhitch * are met:
11 1.1 mhitch * 1. Redistributions of source code must retain the above copyright
12 1.1 mhitch * notice, this list of conditions and the following disclaimer.
13 1.1 mhitch * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 mhitch * notice, this list of conditions and the following disclaimer in the
15 1.1 mhitch * documentation and/or other materials provided with the distribution.
16 1.1 mhitch * 3. The name of the author may not be used to endorse or promote products
17 1.1 mhitch * derived from this software without specific prior written permission.
18 1.1 mhitch *
19 1.1 mhitch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 mhitch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 mhitch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 mhitch * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 mhitch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 mhitch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 mhitch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 mhitch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 mhitch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 mhitch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 mhitch */
30 1.1 mhitch
31 1.1 mhitch #include <sys/types.h>
32 1.3 mycroft #include <sys/stat.h>
33 1.33 thorpej #include <string.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.36 mycroft #define SUPPORT_OLD_BROKEN_LD
39 1.36 mycroft
40 1.22 mycroft void _rtld_bind_start(void);
41 1.17 mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
42 1.36 mycroft caddr_t _rtld_bind(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
43 1.6 mycroft
44 1.1 mhitch void
45 1.6 mycroft _rtld_setup_pltgot(obj)
46 1.12 mycroft const Obj_Entry *obj;
47 1.1 mhitch {
48 1.6 mycroft obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
49 1.6 mycroft /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
50 1.6 mycroft obj->pltgot[1] |= (Elf_Addr) obj;
51 1.8 mycroft }
52 1.8 mycroft
53 1.17 mycroft void
54 1.17 mycroft _rtld_relocate_nonplt_self(dynp, relocbase)
55 1.17 mycroft Elf_Dyn *dynp;
56 1.17 mycroft Elf_Addr relocbase;
57 1.17 mycroft {
58 1.17 mycroft const Elf_Rel *rel = 0, *rellim;
59 1.17 mycroft Elf_Addr relsz = 0;
60 1.17 mycroft Elf_Addr *where;
61 1.18 mycroft const Elf_Sym *symtab, *sym;
62 1.18 mycroft Elf_Addr *got;
63 1.18 mycroft Elf_Word local_gotno, symtabno, gotsym;
64 1.18 mycroft int i;
65 1.17 mycroft
66 1.17 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
67 1.17 mycroft switch (dynp->d_tag) {
68 1.17 mycroft case DT_REL:
69 1.17 mycroft rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
70 1.17 mycroft break;
71 1.17 mycroft case DT_RELSZ:
72 1.17 mycroft relsz = dynp->d_un.d_val;
73 1.17 mycroft break;
74 1.17 mycroft case DT_SYMTAB:
75 1.17 mycroft symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
76 1.17 mycroft break;
77 1.18 mycroft case DT_PLTGOT:
78 1.18 mycroft got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
79 1.18 mycroft break;
80 1.18 mycroft case DT_MIPS_LOCAL_GOTNO:
81 1.18 mycroft local_gotno = dynp->d_un.d_val;
82 1.18 mycroft break;
83 1.18 mycroft case DT_MIPS_SYMTABNO:
84 1.18 mycroft symtabno = dynp->d_un.d_val;
85 1.18 mycroft break;
86 1.18 mycroft case DT_MIPS_GOTSYM:
87 1.18 mycroft gotsym = dynp->d_un.d_val;
88 1.18 mycroft break;
89 1.17 mycroft }
90 1.17 mycroft }
91 1.34 mycroft
92 1.34 mycroft i = (got[1] & 0x80000000) ? 2 : 1;
93 1.34 mycroft /* Relocate the local GOT entries */
94 1.34 mycroft got += i;
95 1.34 mycroft for (; i < local_gotno; i++)
96 1.34 mycroft *got++ += relocbase;
97 1.34 mycroft sym = symtab + gotsym;
98 1.34 mycroft /* Now do the global GOT entries */
99 1.34 mycroft for (i = gotsym; i < symtabno; i++) {
100 1.34 mycroft *got = sym->st_value + relocbase;
101 1.34 mycroft ++sym;
102 1.34 mycroft ++got;
103 1.34 mycroft }
104 1.34 mycroft
105 1.17 mycroft rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
106 1.17 mycroft for (; rel < rellim; rel++) {
107 1.17 mycroft where = (Elf_Addr *)(relocbase + rel->r_offset);
108 1.17 mycroft
109 1.17 mycroft switch (ELF_R_TYPE(rel->r_info)) {
110 1.17 mycroft case R_TYPE(NONE):
111 1.17 mycroft break;
112 1.17 mycroft
113 1.17 mycroft case R_TYPE(REL32):
114 1.34 mycroft assert(ELF_R_SYM(rel->r_info) < gotsym);
115 1.18 mycroft sym = symtab + ELF_R_SYM(rel->r_info);
116 1.35 mycroft assert(sym->st_info ==
117 1.35 mycroft ELF_ST_INFO(STB_LOCAL, STT_SECTION));
118 1.34 mycroft *where += (Elf_Addr)(sym->st_value + relocbase);
119 1.17 mycroft break;
120 1.17 mycroft
121 1.17 mycroft default:
122 1.17 mycroft abort();
123 1.17 mycroft }
124 1.17 mycroft }
125 1.17 mycroft }
126 1.17 mycroft
127 1.33 thorpej /*
128 1.33 thorpej * It is possible for the compiler to emit relocations for unaligned data.
129 1.33 thorpej * We handle this situation with these inlines.
130 1.33 thorpej */
131 1.33 thorpej #define RELOC_ALIGNED_P(x) \
132 1.33 thorpej (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
133 1.33 thorpej
134 1.33 thorpej static __inline Elf_Addr
135 1.33 thorpej load_ptr(void *where)
136 1.33 thorpej {
137 1.33 thorpej Elf_Addr res;
138 1.33 thorpej
139 1.33 thorpej memcpy(&res, where, sizeof(res));
140 1.33 thorpej
141 1.33 thorpej return (res);
142 1.33 thorpej }
143 1.33 thorpej
144 1.33 thorpej static __inline void
145 1.33 thorpej store_ptr(void *where, Elf_Addr val)
146 1.33 thorpej {
147 1.33 thorpej
148 1.33 thorpej memcpy(where, &val, sizeof(val));
149 1.33 thorpej }
150 1.33 thorpej
151 1.8 mycroft int
152 1.36 mycroft _rtld_relocate_nonplt_objects(obj)
153 1.13 mycroft const Obj_Entry *obj;
154 1.8 mycroft {
155 1.9 mycroft const Elf_Rel *rel;
156 1.18 mycroft Elf_Addr *got = obj->pltgot;
157 1.19 mycroft const Elf_Sym *sym, *def;
158 1.18 mycroft const Obj_Entry *defobj;
159 1.18 mycroft int i;
160 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
161 1.36 mycroft int broken;
162 1.36 mycroft #endif
163 1.36 mycroft
164 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
165 1.36 mycroft broken = 0;
166 1.36 mycroft sym = obj->symtab;
167 1.36 mycroft for (i = 1; i < 12; i++)
168 1.36 mycroft if (sym[i].st_info == ELF_ST_INFO(STB_LOCAL, STT_NOTYPE))
169 1.36 mycroft broken = 1;
170 1.36 mycroft dbg(("%s: broken=%d", obj->path, broken));
171 1.36 mycroft #endif
172 1.17 mycroft
173 1.34 mycroft i = (got[1] & 0x80000000) ? 2 : 1;
174 1.34 mycroft /* Relocate the local GOT entries */
175 1.34 mycroft got += i;
176 1.34 mycroft for (; i < obj->local_gotno; i++)
177 1.34 mycroft *got++ += (Elf_Addr)obj->relocbase;
178 1.34 mycroft sym = obj->symtab + obj->gotsym;
179 1.34 mycroft /* Now do the global GOT entries */
180 1.34 mycroft for (i = obj->gotsym; i < obj->symtabno; i++) {
181 1.34 mycroft rdbg((" doing got %d sym %p (%s, %x)", i - obj->gotsym, sym,
182 1.34 mycroft sym->st_name + obj->strtab, *got));
183 1.34 mycroft
184 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
185 1.34 mycroft if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
186 1.36 mycroft broken && sym->st_shndx == SHN_UNDEF) {
187 1.34 mycroft /*
188 1.34 mycroft * XXX DANGER WILL ROBINSON!
189 1.34 mycroft * You might think this is stupid, as it intentionally
190 1.34 mycroft * defeats lazy binding -- and you'd be right.
191 1.34 mycroft * Unfortunately, for lazy binding to work right, we
192 1.34 mycroft * need to a way to force the GOT slots used for
193 1.34 mycroft * function pointers to be resolved immediately. This
194 1.34 mycroft * is supposed to be done automatically by the linker,
195 1.34 mycroft * by not outputting a PLT slot and setting st_value
196 1.36 mycroft * to 0 if there are non-PLT references, but older
197 1.36 mycroft * versions of GNU ld do not do this.
198 1.34 mycroft */
199 1.38 skrll def = _rtld_find_symdef(i, obj, &defobj, false);
200 1.34 mycroft if (def == NULL)
201 1.34 mycroft return -1;
202 1.34 mycroft *got = def->st_value + (Elf_Addr)defobj->relocbase;
203 1.36 mycroft } else
204 1.36 mycroft #endif
205 1.36 mycroft if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
206 1.34 mycroft sym->st_value != 0) {
207 1.34 mycroft /*
208 1.34 mycroft * If there are non-PLT references to the function,
209 1.34 mycroft * st_value should be 0, forcing us to resolve the
210 1.34 mycroft * address immediately.
211 1.34 mycroft */
212 1.34 mycroft *got = sym->st_value + (Elf_Addr)obj->relocbase;
213 1.34 mycroft } else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
214 1.34 mycroft /* Symbols with index SHN_ABS are not relocated. */
215 1.34 mycroft if (sym->st_shndx != SHN_ABS)
216 1.34 mycroft *got = sym->st_value +
217 1.34 mycroft (Elf_Addr)obj->relocbase;
218 1.34 mycroft } else {
219 1.38 skrll def = _rtld_find_symdef(i, obj, &defobj, false);
220 1.34 mycroft if (def == NULL)
221 1.34 mycroft return -1;
222 1.34 mycroft *got = def->st_value + (Elf_Addr)defobj->relocbase;
223 1.34 mycroft }
224 1.34 mycroft
225 1.34 mycroft rdbg((" --> now %x", *got));
226 1.34 mycroft ++sym;
227 1.34 mycroft ++got;
228 1.34 mycroft }
229 1.34 mycroft
230 1.34 mycroft got = obj->pltgot;
231 1.9 mycroft for (rel = obj->rel; rel < obj->rellim; rel++) {
232 1.33 thorpej Elf_Addr *where, tmp;
233 1.10 mycroft unsigned long symnum;
234 1.9 mycroft
235 1.9 mycroft where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
236 1.10 mycroft symnum = ELF_R_SYM(rel->r_info);
237 1.9 mycroft
238 1.9 mycroft switch (ELF_R_TYPE(rel->r_info)) {
239 1.9 mycroft case R_TYPE(NONE):
240 1.9 mycroft break;
241 1.9 mycroft
242 1.9 mycroft case R_TYPE(REL32):
243 1.9 mycroft /* 32-bit PC-relative reference */
244 1.10 mycroft def = obj->symtab + symnum;
245 1.9 mycroft
246 1.34 mycroft if (symnum >= obj->gotsym) {
247 1.34 mycroft tmp = *where;
248 1.34 mycroft tmp += got[obj->local_gotno + symnum - obj->gotsym];
249 1.34 mycroft *where = tmp;
250 1.34 mycroft
251 1.34 mycroft rdbg(("REL32/G %s in %s --> %p in %s",
252 1.34 mycroft obj->strtab + def->st_name, obj->path,
253 1.34 mycroft (void *)tmp, obj->path));
254 1.34 mycroft break;
255 1.34 mycroft } else {
256 1.9 mycroft /*
257 1.9 mycroft * XXX: ABI DIFFERENCE!
258 1.9 mycroft *
259 1.9 mycroft * Old NetBSD binutils would generate shared
260 1.9 mycroft * libs with section-relative relocations being
261 1.9 mycroft * already adjusted for the start address of
262 1.9 mycroft * the section.
263 1.9 mycroft *
264 1.9 mycroft * New binutils, OTOH, generate shared libs
265 1.9 mycroft * with the same relocations being based at
266 1.9 mycroft * zero, so we need to add in the start address
267 1.9 mycroft * of the section.
268 1.9 mycroft *
269 1.9 mycroft * --rkb, Oct 6, 2001
270 1.9 mycroft */
271 1.34 mycroft tmp = *where;
272 1.34 mycroft
273 1.35 mycroft if (def->st_info ==
274 1.36 mycroft ELF_ST_INFO(STB_LOCAL, STT_SECTION)
275 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
276 1.36 mycroft && !broken
277 1.36 mycroft #endif
278 1.36 mycroft )
279 1.34 mycroft tmp += (Elf_Addr)def->st_value;
280 1.9 mycroft
281 1.34 mycroft tmp += (Elf_Addr)obj->relocbase;
282 1.34 mycroft *where = tmp;
283 1.9 mycroft
284 1.35 mycroft rdbg(("REL32/L %s in %s --> %p in %s",
285 1.9 mycroft obj->strtab + def->st_name, obj->path,
286 1.33 thorpej (void *)tmp, obj->path));
287 1.9 mycroft }
288 1.9 mycroft break;
289 1.9 mycroft
290 1.9 mycroft default:
291 1.23 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
292 1.9 mycroft "contents = %p, symbol = %s",
293 1.10 mycroft symnum, (u_long)ELF_R_TYPE(rel->r_info),
294 1.33 thorpej (void *)rel->r_offset, (void *)load_ptr(where),
295 1.10 mycroft obj->strtab + obj->symtab[symnum].st_name));
296 1.9 mycroft _rtld_error("%s: Unsupported relocation type %ld "
297 1.9 mycroft "in non-PLT relocations\n",
298 1.9 mycroft obj->path, (u_long) ELF_R_TYPE(rel->r_info));
299 1.9 mycroft return -1;
300 1.8 mycroft }
301 1.18 mycroft }
302 1.18 mycroft
303 1.8 mycroft return 0;
304 1.8 mycroft }
305 1.8 mycroft
306 1.8 mycroft int
307 1.23 mycroft _rtld_relocate_plt_lazy(obj)
308 1.13 mycroft const Obj_Entry *obj;
309 1.13 mycroft {
310 1.31 mycroft /* PLT fixups were done above in the GOT relocation. */
311 1.28 mycroft return 0;
312 1.36 mycroft }
313 1.36 mycroft
314 1.36 mycroft caddr_t
315 1.36 mycroft _rtld_bind(a0, a1, a2, a3)
316 1.36 mycroft Elf_Word a0;
317 1.36 mycroft Elf_Addr a1, a2, a3;
318 1.36 mycroft {
319 1.36 mycroft Elf_Addr *got = (Elf_Addr *)(a2 - 0x7ff0);
320 1.36 mycroft const Obj_Entry *obj = (Obj_Entry *)(got[1] & 0x7fffffff);
321 1.36 mycroft const Elf_Sym *def;
322 1.36 mycroft const Obj_Entry *defobj;
323 1.36 mycroft Elf_Addr new_value;
324 1.36 mycroft
325 1.36 mycroft def = _rtld_find_symdef(a0, obj, &defobj, true);
326 1.36 mycroft if (def == NULL)
327 1.36 mycroft _rtld_die();
328 1.36 mycroft
329 1.36 mycroft new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
330 1.36 mycroft rdbg(("bind now/fixup in %s --> new=%p",
331 1.36 mycroft defobj->strtab + def->st_name, (void *)new_value));
332 1.36 mycroft got[obj->local_gotno + a0 - obj->gotsym] = new_value;
333 1.36 mycroft return ((caddr_t)new_value);
334 1.1 mhitch }
335