reloc.c revision 1.24 1 1.24 itohy /* $NetBSD: reloc.c,v 1.24 1999/12/08 08:47:10 itohy Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright 1996 John D. Polstra.
5 1.1 cgd * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 1.1 cgd * All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by John Polstra.
19 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
20 1.1 cgd * derived from this software without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd /*
35 1.1 cgd * Dynamic linker for ELF.
36 1.1 cgd *
37 1.1 cgd * John Polstra <jdp (at) polstra.com>.
38 1.1 cgd */
39 1.1 cgd
40 1.1 cgd #include <err.h>
41 1.1 cgd #include <errno.h>
42 1.1 cgd #include <fcntl.h>
43 1.1 cgd #include <stdarg.h>
44 1.1 cgd #include <stdio.h>
45 1.1 cgd #include <stdlib.h>
46 1.1 cgd #include <string.h>
47 1.1 cgd #include <unistd.h>
48 1.1 cgd #include <sys/types.h>
49 1.1 cgd #include <sys/mman.h>
50 1.1 cgd #include <dirent.h>
51 1.1 cgd
52 1.1 cgd #include "debug.h"
53 1.1 cgd #include "rtld.h"
54 1.1 cgd
55 1.14 pk #ifndef RTLD_INHIBIT_COPY_RELOCS
56 1.16 christos static int _rtld_do_copy_relocation __P((const Obj_Entry *, const Elf_RelA *,
57 1.16 christos bool));
58 1.16 christos
59 1.4 christos /*
60 1.4 christos * XXX: These don't work for the alpha and i386; don't know about powerpc
61 1.4 christos * The alpha and the i386 avoid the problem by compiling everything PIC.
62 1.4 christos * These relocation are supposed to be writing the address of the
63 1.4 christos * function to be called on the bss.rel or bss.rela segment, but:
64 1.4 christos * - st_size == 0
65 1.4 christos * - on the i386 at least the call instruction is a direct call
66 1.4 christos * not an indirect call.
67 1.4 christos */
68 1.1 cgd static int
69 1.16 christos _rtld_do_copy_relocation(dstobj, rela, dodebug)
70 1.16 christos const Obj_Entry *dstobj;
71 1.16 christos const Elf_RelA *rela;
72 1.16 christos bool dodebug;
73 1.1 cgd {
74 1.11 christos void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
75 1.11 christos const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
76 1.11 christos const char *name = dstobj->strtab + dstsym->st_name;
77 1.11 christos unsigned long hash = _rtld_elf_hash(name);
78 1.11 christos size_t size = dstsym->st_size;
79 1.11 christos const void *srcaddr;
80 1.11 christos const Elf_Sym *srcsym;
81 1.11 christos Obj_Entry *srcobj;
82 1.11 christos
83 1.11 christos for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
84 1.11 christos if ((srcsym = _rtld_symlook_obj(name, hash, srcobj,
85 1.11 christos false)) != NULL)
86 1.11 christos break;
87 1.11 christos
88 1.11 christos if (srcobj == NULL) {
89 1.11 christos _rtld_error("Undefined symbol \"%s\" referenced from COPY"
90 1.11 christos " relocation in %s", name, dstobj->path);
91 1.14 pk return (-1);
92 1.11 christos }
93 1.11 christos srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
94 1.11 christos (void)memcpy(dstaddr, srcaddr, size);
95 1.18 christos rdbg(dodebug, ("COPY %s %s %s --> src=%p dst=%p *dst= %p size %ld",
96 1.11 christos dstobj->path, srcobj->path, name, (void *)srcaddr,
97 1.18 christos (void *)dstaddr, (void *)*(long *)dstaddr, (long)size));
98 1.14 pk return (0);
99 1.1 cgd }
100 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
101 1.11 christos
102 1.11 christos
103 1.1 cgd /*
104 1.1 cgd * Process the special R_xxx_COPY relocations in the main program. These
105 1.1 cgd * copy data from a shared object into a region in the main program's BSS
106 1.1 cgd * segment.
107 1.1 cgd *
108 1.1 cgd * Returns 0 on success, -1 on failure.
109 1.1 cgd */
110 1.1 cgd int
111 1.16 christos _rtld_do_copy_relocations(dstobj, dodebug)
112 1.16 christos const Obj_Entry *dstobj;
113 1.16 christos bool dodebug;
114 1.1 cgd {
115 1.14 pk #ifndef RTLD_INHIBIT_COPY_RELOCS
116 1.14 pk
117 1.14 pk /* COPY relocations are invalid elsewhere */
118 1.14 pk assert(dstobj->mainprog);
119 1.1 cgd
120 1.11 christos if (dstobj->rel != NULL) {
121 1.11 christos const Elf_Rel *rel;
122 1.11 christos for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
123 1.11 christos if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
124 1.11 christos Elf_RelA ourrela;
125 1.11 christos ourrela.r_info = rel->r_info;
126 1.11 christos ourrela.r_offset = rel->r_offset;
127 1.11 christos ourrela.r_addend = 0;
128 1.11 christos if (_rtld_do_copy_relocation(dstobj,
129 1.11 christos &ourrela, dodebug) < 0)
130 1.14 pk return (-1);
131 1.11 christos }
132 1.11 christos }
133 1.11 christos }
134 1.11 christos if (dstobj->rela != NULL) {
135 1.11 christos const Elf_RelA *rela;
136 1.11 christos for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
137 1.11 christos if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
138 1.11 christos if (_rtld_do_copy_relocation(dstobj, rela,
139 1.11 christos dodebug) < 0)
140 1.14 pk return (-1);
141 1.11 christos }
142 1.11 christos }
143 1.1 cgd }
144 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
145 1.1 cgd
146 1.14 pk return (0);
147 1.1 cgd }
148 1.9 pk
149 1.9 pk
150 1.11 christos #ifndef __sparc__
151 1.11 christos int
152 1.16 christos _rtld_relocate_nonplt_object(obj, rela, dodebug)
153 1.23 mycroft Obj_Entry *obj;
154 1.16 christos const Elf_RelA *rela;
155 1.16 christos bool dodebug;
156 1.9 pk {
157 1.11 christos Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
158 1.11 christos const Elf_Sym *def;
159 1.9 pk const Obj_Entry *defobj;
160 1.20 kleink #if defined(__alpha__) || defined(__i386__) || defined(__m68k__)
161 1.11 christos extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
162 1.11 christos extern Elf_Dyn _DYNAMIC;
163 1.15 scottr #endif
164 1.20 kleink #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
165 1.21 matt defined(__powerpc__) || defined(__vax__)
166 1.11 christos Elf_Addr tmp;
167 1.17 jonathan #endif
168 1.9 pk
169 1.11 christos switch (ELF_R_TYPE(rela->r_info)) {
170 1.9 pk
171 1.11 christos case R_TYPE(NONE):
172 1.11 christos break;
173 1.9 pk
174 1.24 itohy #if defined(__i386__)
175 1.11 christos case R_TYPE(GOT32):
176 1.9 pk
177 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
178 1.11 christos &defobj, false);
179 1.11 christos if (def == NULL)
180 1.11 christos return -1;
181 1.9 pk
182 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
183 1.11 christos if (*where != tmp)
184 1.11 christos *where = tmp;
185 1.16 christos rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
186 1.11 christos defobj->strtab + def->st_name, obj->path,
187 1.16 christos (void *)*where, defobj->path));
188 1.11 christos break;
189 1.9 pk
190 1.11 christos case R_TYPE(PC32):
191 1.11 christos /*
192 1.11 christos * I don't think the dynamic linker should ever see this
193 1.11 christos * type of relocation. But the binutils-2.6 tools sometimes
194 1.11 christos * generate it.
195 1.11 christos */
196 1.9 pk
197 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
198 1.11 christos &defobj, false);
199 1.11 christos if (def == NULL)
200 1.11 christos return -1;
201 1.9 pk
202 1.11 christos *where += (Elf_Addr)(defobj->relocbase + def->st_value) -
203 1.11 christos (Elf_Addr)where;
204 1.16 christos rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
205 1.11 christos defobj->strtab + def->st_name, obj->path,
206 1.16 christos (void *)*where, defobj->path));
207 1.11 christos break;
208 1.11 christos
209 1.11 christos case R_TYPE(32):
210 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
211 1.11 christos &defobj, false);
212 1.11 christos if (def == NULL)
213 1.11 christos return -1;
214 1.9 pk
215 1.11 christos *where += (Elf_Addr)(defobj->relocbase + def->st_value);
216 1.16 christos rdbg(dodebug, ("32 %s in %s --> %p in %s",
217 1.11 christos defobj->strtab + def->st_name, obj->path,
218 1.16 christos (void *)*where, defobj->path));
219 1.11 christos break;
220 1.24 itohy #endif /* __i386__ */
221 1.24 itohy
222 1.24 itohy #if defined(__m68k__)
223 1.24 itohy case R_TYPE(GOT32):
224 1.24 itohy def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
225 1.24 itohy &defobj, false);
226 1.24 itohy if (def == NULL)
227 1.24 itohy return -1;
228 1.24 itohy
229 1.24 itohy tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
230 1.24 itohy rela->r_addend);
231 1.24 itohy if (*where != tmp)
232 1.24 itohy *where = tmp;
233 1.24 itohy rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
234 1.24 itohy defobj->strtab + def->st_name, obj->path,
235 1.24 itohy (void *)*where, defobj->path));
236 1.24 itohy break;
237 1.24 itohy
238 1.24 itohy case R_TYPE(PC32):
239 1.24 itohy def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
240 1.24 itohy &defobj, false);
241 1.24 itohy if (def == NULL)
242 1.24 itohy return -1;
243 1.24 itohy
244 1.24 itohy tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
245 1.24 itohy rela->r_addend) - (Elf_Addr)where;
246 1.24 itohy if (*where != tmp)
247 1.24 itohy *where = tmp;
248 1.24 itohy rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
249 1.24 itohy defobj->strtab + def->st_name, obj->path,
250 1.24 itohy (void *)*where, defobj->path));
251 1.24 itohy break;
252 1.24 itohy
253 1.24 itohy case R_TYPE(32):
254 1.24 itohy def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
255 1.24 itohy &defobj, false);
256 1.24 itohy if (def == NULL)
257 1.24 itohy return -1;
258 1.24 itohy
259 1.24 itohy tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
260 1.24 itohy rela->r_addend);
261 1.24 itohy if (*where != tmp)
262 1.24 itohy *where = tmp;
263 1.24 itohy rdbg(dodebug, ("32 %s in %s --> %p in %s",
264 1.24 itohy defobj->strtab + def->st_name, obj->path,
265 1.24 itohy (void *)*where, defobj->path));
266 1.24 itohy break;
267 1.24 itohy #endif /* __m68k__ */
268 1.4 christos
269 1.21 matt #if defined(__alpha__)
270 1.11 christos case R_TYPE(REFQUAD):
271 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
272 1.11 christos &defobj, false);
273 1.11 christos if (def == NULL)
274 1.11 christos return -1;
275 1.1 cgd
276 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
277 1.11 christos *where + rela->r_addend;
278 1.12 tv if (*where != tmp)
279 1.12 tv *where = tmp;
280 1.16 christos rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
281 1.11 christos defobj->strtab + def->st_name, obj->path,
282 1.16 christos (void *)*where, defobj->path));
283 1.11 christos break;
284 1.4 christos #endif /* __alpha__ */
285 1.1 cgd
286 1.20 kleink #if defined(__alpha__) || defined(__i386__) || defined(__m68k__)
287 1.11 christos case R_TYPE(GLOB_DAT):
288 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
289 1.11 christos &defobj, false);
290 1.11 christos if (def == NULL)
291 1.11 christos return -1;
292 1.1 cgd
293 1.11 christos if (*where != (Elf_Addr)(defobj->relocbase + def->st_value))
294 1.11 christos *where = (Elf_Addr)(defobj->relocbase + def->st_value);
295 1.16 christos rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
296 1.11 christos defobj->strtab + def->st_name, obj->path,
297 1.16 christos (void *)*where, defobj->path));
298 1.11 christos break;
299 1.11 christos
300 1.11 christos case R_TYPE(RELATIVE):
301 1.19 christos if (!dodebug ||
302 1.19 christos (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
303 1.11 christos (caddr_t)where >= (caddr_t)&_DYNAMIC) {
304 1.11 christos *where += (Elf_Addr)obj->relocbase;
305 1.16 christos rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
306 1.16 christos (void *)*where));
307 1.11 christos }
308 1.11 christos else
309 1.16 christos rdbg(dodebug, ("RELATIVE in %s stays at %p",
310 1.16 christos obj->path, (void *)*where));
311 1.11 christos break;
312 1.1 cgd
313 1.11 christos case R_TYPE(COPY):
314 1.11 christos /*
315 1.11 christos * These are deferred until all other relocations have
316 1.11 christos * been done. All we do here is make sure that the COPY
317 1.11 christos * relocation is not in a shared library. They are allowed
318 1.11 christos * only in executable files.
319 1.11 christos */
320 1.11 christos if (!obj->mainprog) {
321 1.11 christos _rtld_error(
322 1.11 christos "%s: Unexpected R_COPY relocation in shared library",
323 1.11 christos obj->path);
324 1.11 christos return -1;
325 1.11 christos }
326 1.16 christos rdbg(dodebug, ("COPY (avoid in main)"));
327 1.11 christos break;
328 1.4 christos #endif /* __i386__ || __alpha__ */
329 1.2 mhitch
330 1.21 matt #if defined(__mips__)
331 1.11 christos case R_TYPE(REL32):
332 1.11 christos /* 32-bit PC-relative reference */
333 1.11 christos def = obj->symtab + ELF_R_SYM(rela->r_info);
334 1.11 christos
335 1.22 simonb if (ELFDEFNNAME(ST_BIND)(def->st_info) == STB_LOCAL &&
336 1.22 simonb (ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_SECTION ||
337 1.22 simonb ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_NOTYPE)) {
338 1.11 christos *where += (Elf_Addr)obj->relocbase;
339 1.16 christos rdbg(dodebug, ("REL32 in %s --> %p", obj->path,
340 1.16 christos (void *)*where));
341 1.11 christos } else {
342 1.11 christos /* XXX maybe do something re: bootstrapping? */
343 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
344 1.11 christos NULL, obj, &defobj, false);
345 1.11 christos if (def == NULL)
346 1.11 christos return -1;
347 1.11 christos *where += (Elf_Addr)(defobj->relocbase + def->st_value);
348 1.16 christos rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
349 1.11 christos defobj->strtab + def->st_name, obj->path,
350 1.16 christos (void *)*where, defobj->path));
351 1.11 christos }
352 1.11 christos break;
353 1.2 mhitch
354 1.11 christos #endif /* __mips__ */
355 1.1 cgd
356 1.21 matt #if defined(__powerpc__) || defined(__vax__)
357 1.11 christos case R_TYPE(32): /* word32 S + A */
358 1.11 christos case R_TYPE(GLOB_DAT): /* word32 S + A */
359 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
360 1.11 christos &defobj, false);
361 1.11 christos if (def == NULL)
362 1.11 christos return -1;
363 1.3 tsubai
364 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
365 1.11 christos rela->r_addend);
366 1.3 tsubai
367 1.11 christos if (*where != tmp)
368 1.11 christos *where = tmp;
369 1.16 christos rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
370 1.11 christos defobj->strtab + def->st_name, obj->path,
371 1.16 christos (void *)*where, defobj->path));
372 1.11 christos break;
373 1.11 christos
374 1.11 christos case R_TYPE(COPY):
375 1.16 christos rdbg(dodebug, ("COPY"));
376 1.11 christos break;
377 1.11 christos
378 1.11 christos case R_TYPE(JMP_SLOT):
379 1.16 christos rdbg(dodebug, ("JMP_SLOT"));
380 1.11 christos break;
381 1.11 christos
382 1.11 christos case R_TYPE(RELATIVE): /* word32 B + A */
383 1.11 christos tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
384 1.21 matt if (*where != tmp)
385 1.21 matt *where = tmp;
386 1.16 christos rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
387 1.16 christos (void *)*where));
388 1.11 christos break;
389 1.21 matt #endif /* __powerpc__ || __vax__ */
390 1.21 matt
391 1.21 matt #if defined(__vax__)
392 1.21 matt case R_TYPE(REL32):
393 1.21 matt def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
394 1.21 matt &defobj, false);
395 1.21 matt if (def == NULL)
396 1.21 matt return -1;
397 1.21 matt
398 1.21 matt *where += (Elf_Addr)(defobj->relocbase + def->st_value
399 1.21 matt + rela->r_addend);
400 1.21 matt rdbg(dodebug, ("32 %s in %s --> %p in %s",
401 1.21 matt defobj->strtab + def->st_name, obj->path,
402 1.21 matt (void *)*where, defobj->path));
403 1.21 matt break;
404 1.21 matt #endif /* __vax__ */
405 1.3 tsubai
406 1.11 christos default:
407 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
408 1.11 christos &defobj, true);
409 1.16 christos rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
410 1.11 christos "addend = %p, contents = %p, symbol = %s",
411 1.11 christos (u_long)ELF_R_SYM(rela->r_info),
412 1.11 christos (u_long)ELF_R_TYPE(rela->r_info),
413 1.11 christos (void *)rela->r_offset, (void *)rela->r_addend,
414 1.11 christos (void *)*where,
415 1.16 christos def ? defobj->strtab + def->st_name : "??"));
416 1.11 christos _rtld_error("%s: Unsupported relocation type %d"
417 1.11 christos "in non-PLT relocations\n",
418 1.11 christos obj->path, ELF_R_TYPE(rela->r_info));
419 1.11 christos return -1;
420 1.11 christos }
421 1.11 christos return 0;
422 1.1 cgd }
423 1.9 pk
424 1.9 pk
425 1.9 pk
426 1.11 christos int
427 1.16 christos _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
428 1.23 mycroft Obj_Entry *obj;
429 1.16 christos const Elf_RelA *rela;
430 1.16 christos caddr_t *addrp;
431 1.16 christos bool bind_now;
432 1.16 christos bool dodebug;
433 1.1 cgd {
434 1.16 christos Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
435 1.16 christos Elf_Addr new_value;
436 1.1 cgd
437 1.11 christos /* Fully resolve procedure addresses now */
438 1.2 mhitch
439 1.3 tsubai #if defined(__powerpc__)
440 1.11 christos return _rtld_reloc_powerpc_plt(obj, rela, bind_now);
441 1.3 tsubai #endif
442 1.3 tsubai
443 1.21 matt #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
444 1.21 matt defined(__vax__)
445 1.11 christos if (bind_now || obj->pltgot == NULL) {
446 1.11 christos const Elf_Sym *def;
447 1.11 christos const Obj_Entry *defobj;
448 1.11 christos
449 1.11 christos assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
450 1.1 cgd
451 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
452 1.11 christos &defobj, true);
453 1.11 christos if (def == NULL)
454 1.11 christos return -1;
455 1.1 cgd
456 1.11 christos new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
457 1.16 christos rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
458 1.11 christos (int)bind_now,
459 1.11 christos defobj->strtab + def->st_name,
460 1.16 christos (void *)*where, (void *)new_value));
461 1.11 christos } else
462 1.21 matt #endif /* __alpha__ || __i386__ || __m68k__ || __vax__ */
463 1.11 christos if (!obj->mainprog) {
464 1.11 christos /* Just relocate the GOT slots pointing into the PLT */
465 1.11 christos new_value = *where + (Elf_Addr)(obj->relocbase);
466 1.16 christos rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
467 1.16 christos (void *)*where));
468 1.11 christos } else {
469 1.11 christos return 0;
470 1.11 christos }
471 1.11 christos /*
472 1.11 christos * Since this page is probably copy-on-write, let's not write
473 1.11 christos * it unless we really really have to.
474 1.11 christos */
475 1.11 christos if (*where != new_value)
476 1.11 christos *where = new_value;
477 1.11 christos if (addrp != NULL)
478 1.11 christos *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
479 1.7 tv return 0;
480 1.1 cgd }
481 1.11 christos #endif /* __sparc__ */
482 1.9 pk
483 1.1 cgd caddr_t
484 1.16 christos _rtld_bind(obj, reloff)
485 1.23 mycroft Obj_Entry *obj;
486 1.16 christos Elf_Word reloff;
487 1.1 cgd {
488 1.11 christos const Elf_RelA *rela;
489 1.11 christos Elf_RelA ourrela;
490 1.11 christos caddr_t addr;
491 1.1 cgd
492 1.11 christos if (obj->pltrel != NULL) {
493 1.11 christos const Elf_Rel *rel;
494 1.1 cgd
495 1.11 christos rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
496 1.11 christos ourrela.r_info = rel->r_info;
497 1.11 christos ourrela.r_offset = rel->r_offset;
498 1.11 christos rela = &ourrela;
499 1.11 christos } else {
500 1.11 christos rela = (const Elf_RelA *)((caddr_t) obj->pltrela + reloff);
501 1.11 christos }
502 1.11 christos
503 1.11 christos if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
504 1.11 christos _rtld_die();
505 1.1 cgd
506 1.11 christos return addr;
507 1.1 cgd }
508 1.9 pk
509 1.1 cgd /*
510 1.1 cgd * Relocate newly-loaded shared objects. The argument is a pointer to
511 1.1 cgd * the Obj_Entry for the first such object. All objects from the first
512 1.1 cgd * to the end of the list of objects are relocated. Returns 0 on success,
513 1.1 cgd * or -1 on failure.
514 1.1 cgd */
515 1.1 cgd int
516 1.16 christos _rtld_relocate_objects(first, bind_now, dodebug)
517 1.16 christos Obj_Entry *first;
518 1.16 christos bool bind_now;
519 1.16 christos bool dodebug;
520 1.1 cgd {
521 1.16 christos Obj_Entry *obj;
522 1.16 christos int ok = 1;
523 1.1 cgd
524 1.11 christos for (obj = first; obj != NULL; obj = obj->next) {
525 1.16 christos if (obj->nbuckets == 0 || obj->nchains == 0 ||
526 1.16 christos obj->buckets == NULL || obj->symtab == NULL ||
527 1.16 christos obj->strtab == NULL) {
528 1.11 christos _rtld_error("%s: Shared object has no run-time"
529 1.11 christos " symbol table", obj->path);
530 1.11 christos return -1;
531 1.11 christos }
532 1.16 christos rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
533 1.11 christos "%ld/%ld plt rel/rela)",
534 1.11 christos obj->path,
535 1.11 christos (long)(obj->rellim - obj->rel),
536 1.11 christos (long)(obj->relalim - obj->rela),
537 1.11 christos (long)(obj->pltrellim - obj->pltrel),
538 1.16 christos (long)(obj->pltrelalim - obj->pltrela)));
539 1.11 christos
540 1.11 christos if (obj->textrel) {
541 1.11 christos /*
542 1.11 christos * There are relocations to the write-protected text
543 1.11 christos * segment.
544 1.11 christos */
545 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
546 1.11 christos PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
547 1.11 christos _rtld_error("%s: Cannot write-enable text "
548 1.11 christos "segment: %s", obj->path, xstrerror(errno));
549 1.11 christos return -1;
550 1.11 christos }
551 1.11 christos }
552 1.11 christos if (obj->rel != NULL) {
553 1.11 christos /* Process the non-PLT relocations. */
554 1.11 christos const Elf_Rel *rel;
555 1.11 christos for (rel = obj->rel; rel < obj->rellim; ++rel) {
556 1.11 christos Elf_RelA ourrela;
557 1.11 christos ourrela.r_info = rel->r_info;
558 1.11 christos ourrela.r_offset = rel->r_offset;
559 1.2 mhitch #if defined(__mips__)
560 1.11 christos /* rel->r_offset is not valid on mips? */
561 1.11 christos if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
562 1.11 christos ourrela.r_addend = 0;
563 1.11 christos else
564 1.11 christos #endif
565 1.11 christos ourrela.r_addend =
566 1.11 christos *(Elf_Word *)(obj->relocbase +
567 1.11 christos rel->r_offset);
568 1.11 christos
569 1.11 christos if (_rtld_relocate_nonplt_object(obj, &ourrela,
570 1.11 christos dodebug) < 0)
571 1.11 christos ok = 0;
572 1.11 christos }
573 1.11 christos }
574 1.11 christos if (obj->rela != NULL) {
575 1.11 christos /* Process the non-PLT relocations. */
576 1.11 christos const Elf_RelA *rela;
577 1.11 christos for (rela = obj->rela; rela < obj->relalim; ++rela) {
578 1.11 christos if (_rtld_relocate_nonplt_object(obj, rela,
579 1.11 christos dodebug) < 0)
580 1.11 christos ok = 0;
581 1.11 christos }
582 1.11 christos }
583 1.11 christos if (obj->textrel) { /* Re-protected the text segment. */
584 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
585 1.11 christos PROT_READ | PROT_EXEC) == -1) {
586 1.11 christos _rtld_error("%s: Cannot write-protect text "
587 1.11 christos "segment: %s", obj->path, xstrerror(errno));
588 1.11 christos return -1;
589 1.11 christos }
590 1.11 christos }
591 1.11 christos /* Process the PLT relocations. */
592 1.11 christos if (obj->pltrel != NULL) {
593 1.11 christos const Elf_Rel *rel;
594 1.11 christos for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
595 1.11 christos Elf_RelA ourrela;
596 1.11 christos ourrela.r_info = rel->r_info;
597 1.11 christos ourrela.r_offset = rel->r_offset;
598 1.11 christos ourrela.r_addend =
599 1.11 christos *(Elf_Word *)(obj->relocbase +
600 1.11 christos rel->r_offset);
601 1.11 christos if (_rtld_relocate_plt_object(obj, &ourrela,
602 1.11 christos NULL, bind_now, dodebug) < 0)
603 1.11 christos ok = 0;
604 1.11 christos }
605 1.11 christos }
606 1.11 christos if (obj->pltrela != NULL) {
607 1.11 christos const Elf_RelA *rela;
608 1.11 christos for (rela = obj->pltrela; rela < obj->pltrelalim;
609 1.11 christos ++rela) {
610 1.11 christos if (_rtld_relocate_plt_object(obj, rela,
611 1.11 christos NULL, bind_now, dodebug) < 0)
612 1.11 christos ok = 0;
613 1.11 christos }
614 1.11 christos }
615 1.11 christos if (!ok)
616 1.11 christos return -1;
617 1.11 christos
618 1.11 christos
619 1.11 christos /* Set some sanity-checking numbers in the Obj_Entry. */
620 1.11 christos obj->magic = RTLD_MAGIC;
621 1.11 christos obj->version = RTLD_VERSION;
622 1.11 christos
623 1.11 christos /* Fill in the dynamic linker entry points. */
624 1.11 christos obj->dlopen = _rtld_dlopen;
625 1.11 christos obj->dlsym = _rtld_dlsym;
626 1.11 christos obj->dlerror = _rtld_dlerror;
627 1.11 christos obj->dlclose = _rtld_dlclose;
628 1.1 cgd
629 1.11 christos /* Set the special PLTGOT entries. */
630 1.11 christos if (obj->pltgot != NULL) {
631 1.20 kleink #if defined(__i386__) || defined(__m68k__)
632 1.11 christos obj->pltgot[1] = (Elf_Addr) obj;
633 1.11 christos obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
634 1.1 cgd #endif
635 1.1 cgd #if defined(__alpha__)
636 1.11 christos /*
637 1.11 christos * This function will be called to perform the
638 1.11 christos * relocation.
639 1.11 christos */
640 1.11 christos obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
641 1.11 christos /* Identify this shared object */
642 1.11 christos obj->pltgot[3] = (Elf_Addr) obj;
643 1.2 mhitch #endif
644 1.2 mhitch #if defined(__mips__)
645 1.11 christos _rtld_relocate_mips_got(obj);
646 1.2 mhitch
647 1.11 christos obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
648 1.11 christos /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
649 1.11 christos obj->pltgot[1] |= (Elf_Addr) obj;
650 1.3 tsubai #endif
651 1.3 tsubai #if defined(__powerpc__)
652 1.11 christos _rtld_setup_powerpc_plt(obj);
653 1.9 pk #endif
654 1.9 pk #if defined(__sparc__)
655 1.11 christos /*
656 1.11 christos * PLTGOT is the PLT on the sparc.
657 1.11 christos * The first entry holds the call the dynamic linker.
658 1.13 pk * We construct a `call' sequence that transfers
659 1.11 christos * to `_rtld_bind_start()'.
660 1.11 christos * The second entry holds the object identification.
661 1.11 christos * Note: each PLT entry is three words long.
662 1.11 christos */
663 1.13 pk #define SAVE 0x9de3bfc0 /* i.e. `save %sp,-64,%sp' */
664 1.13 pk #define CALL 0x40000000
665 1.13 pk #define NOP 0x01000000
666 1.13 pk obj->pltgot[0] = SAVE;
667 1.13 pk obj->pltgot[1] = CALL |
668 1.11 christos ((Elf_Addr)&_rtld_bind_start -
669 1.13 pk (Elf_Addr)&obj->pltgot[1]) >> 2;
670 1.13 pk obj->pltgot[2] = NOP;
671 1.13 pk
672 1.11 christos obj->pltgot[3] = (Elf_Addr) obj;
673 1.21 matt #endif
674 1.21 matt #if defined(__vax__)
675 1.21 matt obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
676 1.21 matt obj->pltgot[1] = (Elf_Addr) obj;
677 1.1 cgd #endif
678 1.11 christos }
679 1.1 cgd }
680 1.1 cgd
681 1.11 christos return 0;
682 1.1 cgd }
683