reloc.c revision 1.58 1 1.58 mycroft /* $NetBSD: reloc.c,v 1.58 2002/09/05 17:58:02 mycroft 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.35 kleink 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.35 kleink 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.37 matt const Elf_Sym *srcsym = NULL;
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.35 kleink 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.35 kleink 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.58 mycroft #if !defined(__sparc__) && !defined(__x86_64__) && !defined(__mips__)
151 1.34 christos
152 1.11 christos int
153 1.16 christos _rtld_relocate_nonplt_object(obj, rela, dodebug)
154 1.23 mycroft Obj_Entry *obj;
155 1.35 kleink const Elf_Rela *rela;
156 1.16 christos bool dodebug;
157 1.9 pk {
158 1.11 christos Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
159 1.11 christos const Elf_Sym *def;
160 1.9 pk const Obj_Entry *defobj;
161 1.11 christos Elf_Addr tmp;
162 1.9 pk
163 1.11 christos switch (ELF_R_TYPE(rela->r_info)) {
164 1.9 pk
165 1.11 christos case R_TYPE(NONE):
166 1.11 christos break;
167 1.9 pk
168 1.24 itohy #if defined(__i386__)
169 1.11 christos case R_TYPE(GOT32):
170 1.9 pk
171 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
172 1.11 christos if (def == NULL)
173 1.11 christos return -1;
174 1.9 pk
175 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
176 1.11 christos if (*where != tmp)
177 1.11 christos *where = tmp;
178 1.16 christos rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
179 1.11 christos defobj->strtab + def->st_name, obj->path,
180 1.16 christos (void *)*where, defobj->path));
181 1.11 christos break;
182 1.9 pk
183 1.11 christos case R_TYPE(PC32):
184 1.11 christos /*
185 1.11 christos * I don't think the dynamic linker should ever see this
186 1.11 christos * type of relocation. But the binutils-2.6 tools sometimes
187 1.11 christos * generate it.
188 1.11 christos */
189 1.9 pk
190 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
191 1.11 christos if (def == NULL)
192 1.11 christos return -1;
193 1.9 pk
194 1.11 christos *where += (Elf_Addr)(defobj->relocbase + def->st_value) -
195 1.11 christos (Elf_Addr)where;
196 1.16 christos rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
197 1.11 christos defobj->strtab + def->st_name, obj->path,
198 1.16 christos (void *)*where, defobj->path));
199 1.11 christos break;
200 1.11 christos
201 1.11 christos case R_TYPE(32):
202 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
203 1.11 christos if (def == NULL)
204 1.11 christos return -1;
205 1.9 pk
206 1.11 christos *where += (Elf_Addr)(defobj->relocbase + def->st_value);
207 1.16 christos rdbg(dodebug, ("32 %s in %s --> %p in %s",
208 1.11 christos defobj->strtab + def->st_name, obj->path,
209 1.16 christos (void *)*where, defobj->path));
210 1.11 christos break;
211 1.24 itohy #endif /* __i386__ */
212 1.24 itohy
213 1.24 itohy #if defined(__m68k__)
214 1.24 itohy case R_TYPE(GOT32):
215 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
216 1.24 itohy if (def == NULL)
217 1.24 itohy return -1;
218 1.24 itohy
219 1.24 itohy tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
220 1.24 itohy rela->r_addend);
221 1.24 itohy if (*where != tmp)
222 1.24 itohy *where = tmp;
223 1.24 itohy rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
224 1.24 itohy defobj->strtab + def->st_name, obj->path,
225 1.24 itohy (void *)*where, defobj->path));
226 1.24 itohy break;
227 1.24 itohy
228 1.24 itohy case R_TYPE(PC32):
229 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
230 1.24 itohy if (def == NULL)
231 1.24 itohy return -1;
232 1.24 itohy
233 1.24 itohy tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
234 1.24 itohy rela->r_addend) - (Elf_Addr)where;
235 1.24 itohy if (*where != tmp)
236 1.24 itohy *where = tmp;
237 1.24 itohy rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
238 1.24 itohy defobj->strtab + def->st_name, obj->path,
239 1.24 itohy (void *)*where, defobj->path));
240 1.24 itohy break;
241 1.24 itohy
242 1.24 itohy case R_TYPE(32):
243 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
244 1.24 itohy if (def == NULL)
245 1.24 itohy return -1;
246 1.24 itohy
247 1.24 itohy tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
248 1.24 itohy rela->r_addend);
249 1.24 itohy if (*where != tmp)
250 1.24 itohy *where = tmp;
251 1.24 itohy rdbg(dodebug, ("32 %s in %s --> %p in %s",
252 1.24 itohy defobj->strtab + def->st_name, obj->path,
253 1.24 itohy (void *)*where, defobj->path));
254 1.24 itohy break;
255 1.24 itohy #endif /* __m68k__ */
256 1.4 christos
257 1.51 thorpej #if defined(__sh__)
258 1.51 thorpej case R_TYPE(GOT32):
259 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
260 1.51 thorpej if (def == NULL)
261 1.51 thorpej return -1;
262 1.51 thorpej
263 1.51 thorpej tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
264 1.51 thorpej rela->r_addend);
265 1.51 thorpej if (*where != tmp)
266 1.51 thorpej *where = tmp;
267 1.51 thorpej rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
268 1.51 thorpej defobj->strtab + def->st_name, obj->path,
269 1.51 thorpej (void *)*where, defobj->path));
270 1.51 thorpej break;
271 1.51 thorpej
272 1.51 thorpej case R_TYPE(REL32):
273 1.51 thorpej /*
274 1.51 thorpej * I don't think the dynamic linker should ever see this
275 1.51 thorpej * type of relocation, but some versions of Binutils
276 1.51 thorpej * generate it.
277 1.51 thorpej */
278 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
279 1.51 thorpej if (def == NULL)
280 1.51 thorpej return -1;
281 1.51 thorpej
282 1.51 thorpej *where += (Elf_Addr)(defobj->relocbase + def->st_value +
283 1.51 thorpej rela->r_addend) - (Elf_Addr)where;
284 1.51 thorpej rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
285 1.51 thorpej defobj->strtab + def->st_name, obj->path,
286 1.51 thorpej (void *)*where, defobj->path));
287 1.51 thorpej break;
288 1.51 thorpej
289 1.51 thorpej case R_TYPE(DIR32):
290 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
291 1.51 thorpej if (def == NULL)
292 1.51 thorpej return -1;
293 1.51 thorpej
294 1.51 thorpej *where += (Elf_Addr)(defobj->relocbase + def->st_value +
295 1.51 thorpej rela->r_addend);
296 1.51 thorpej rdbg(dodebug, ("32 %s in %s --> %p in %s",
297 1.51 thorpej defobj->strtab + def->st_name, obj->path,
298 1.51 thorpej (void *)*where, defobj->path));
299 1.51 thorpej break;
300 1.51 thorpej #endif /* __sh__ */
301 1.51 thorpej
302 1.21 matt #if defined(__alpha__)
303 1.11 christos case R_TYPE(REFQUAD):
304 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
305 1.11 christos if (def == NULL)
306 1.11 christos return -1;
307 1.1 cgd
308 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
309 1.11 christos *where + rela->r_addend;
310 1.12 tv if (*where != tmp)
311 1.12 tv *where = tmp;
312 1.16 christos rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
313 1.11 christos defobj->strtab + def->st_name, obj->path,
314 1.16 christos (void *)*where, defobj->path));
315 1.11 christos break;
316 1.46 thorpej
317 1.46 thorpej case R_TYPE(RELATIVE):
318 1.46 thorpej {
319 1.46 thorpej extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
320 1.46 thorpej extern Elf_Addr _GOT_END_[];
321 1.46 thorpej
322 1.46 thorpej /* This is the ...iffy hueristic. */
323 1.46 thorpej if (!dodebug ||
324 1.46 thorpej (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
325 1.46 thorpej (caddr_t)where >= (caddr_t)_GOT_END_) {
326 1.48 thorpej *where += (Elf_Addr)obj->relocbase;
327 1.46 thorpej rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
328 1.46 thorpej (void *)*where));
329 1.46 thorpej } else
330 1.46 thorpej rdbg(dodebug, ("RELATIVE in %s stays at %p",
331 1.46 thorpej obj->path, (void *)*where));
332 1.46 thorpej break;
333 1.46 thorpej }
334 1.4 christos #endif /* __alpha__ */
335 1.1 cgd
336 1.51 thorpej #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
337 1.51 thorpej defined(__sh__)
338 1.11 christos case R_TYPE(GLOB_DAT):
339 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
340 1.11 christos if (def == NULL)
341 1.11 christos return -1;
342 1.1 cgd
343 1.47 thorpej tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
344 1.47 thorpej rela->r_addend;
345 1.47 thorpej if (*where != tmp)
346 1.47 thorpej *where = tmp;
347 1.16 christos rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
348 1.11 christos defobj->strtab + def->st_name, obj->path,
349 1.16 christos (void *)*where, defobj->path));
350 1.11 christos break;
351 1.51 thorpej #if defined(__sh__)
352 1.51 thorpej case R_TYPE(RELATIVE):
353 1.51 thorpej if (rela->r_addend)
354 1.51 thorpej *where = (Elf_Addr)obj->relocbase + rela->r_addend;
355 1.51 thorpej else
356 1.51 thorpej *where += (Elf_Addr)obj->relocbase;
357 1.51 thorpej rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
358 1.51 thorpej (void *)*where));
359 1.51 thorpej break;
360 1.51 thorpej #elif !defined(__alpha__)
361 1.11 christos case R_TYPE(RELATIVE):
362 1.46 thorpej *where += (Elf_Addr)obj->relocbase;
363 1.46 thorpej rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
364 1.46 thorpej (void *)*where));
365 1.11 christos break;
366 1.46 thorpej #endif /* ! __alpha__ */
367 1.52 fredette #endif /* __alpha__ || __i386__ || __m68k__ || __sh__ */
368 1.1 cgd
369 1.21 matt #if defined(__powerpc__) || defined(__vax__)
370 1.11 christos case R_TYPE(32): /* word32 S + A */
371 1.11 christos case R_TYPE(GLOB_DAT): /* word32 S + A */
372 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
373 1.11 christos if (def == NULL)
374 1.11 christos return -1;
375 1.3 tsubai
376 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
377 1.11 christos rela->r_addend);
378 1.3 tsubai
379 1.11 christos if (*where != tmp)
380 1.11 christos *where = tmp;
381 1.16 christos rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
382 1.11 christos defobj->strtab + def->st_name, obj->path,
383 1.16 christos (void *)*where, defobj->path));
384 1.11 christos break;
385 1.11 christos
386 1.11 christos case R_TYPE(RELATIVE): /* word32 B + A */
387 1.11 christos tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
388 1.21 matt if (*where != tmp)
389 1.21 matt *where = tmp;
390 1.16 christos rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
391 1.16 christos (void *)*where));
392 1.11 christos break;
393 1.21 matt #endif /* __powerpc__ || __vax__ */
394 1.37 matt
395 1.37 matt #if defined(__arm__)
396 1.37 matt case R_TYPE(GLOB_DAT): /* word32 B + S */
397 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
398 1.37 matt if (def == NULL)
399 1.37 matt return -1;
400 1.37 matt *where = (Elf_Addr)(defobj->relocbase + def->st_value);
401 1.40 matt rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @ %p in %s",
402 1.37 matt defobj->strtab + def->st_name, obj->path,
403 1.40 matt (void *)*where, where, defobj->path));
404 1.37 matt break;
405 1.37 matt
406 1.37 matt case R_TYPE(ABS32): /* word32 B + S + A */
407 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
408 1.37 matt if (def == NULL)
409 1.37 matt return -1;
410 1.40 matt *where += (Elf_Addr)defobj->relocbase + def->st_value;
411 1.40 matt rdbg(dodebug, ("ABS32 %s in %s --> %p @ %p in %s",
412 1.37 matt defobj->strtab + def->st_name, obj->path,
413 1.40 matt (void *)*where, where, defobj->path));
414 1.37 matt break;
415 1.37 matt
416 1.37 matt case R_TYPE(RELATIVE): /* word32 B + A */
417 1.37 matt *where += (Elf_Addr)obj->relocbase;
418 1.40 matt rdbg(dodebug, ("RELATIVE in %s --> %p @ %p", obj->path,
419 1.40 matt (void *)*where, where));
420 1.37 matt break;
421 1.37 matt
422 1.37 matt case R_TYPE(PC24): { /* word32 S - P + A */
423 1.37 matt Elf32_Sword addend;
424 1.37 matt
425 1.37 matt /*
426 1.37 matt * Extract addend and sign-extend if needed.
427 1.37 matt */
428 1.37 matt addend = *where;
429 1.37 matt if (addend & 0x00800000)
430 1.37 matt addend |= 0xff000000;
431 1.37 matt
432 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
433 1.37 matt if (def == NULL)
434 1.37 matt return -1;
435 1.37 matt tmp = (Elf_Addr)obj->relocbase + def->st_value
436 1.37 matt - (Elf_Addr)where + (addend << 2);
437 1.49 thorpej if ((tmp & 0xfe000000) != 0xfe000000 &&
438 1.49 thorpej (tmp & 0xfe000000) != 0) {
439 1.37 matt _rtld_error(
440 1.37 matt "%s: R_ARM_PC24 relocation @ %p to %s failed "
441 1.37 matt "(displacement %ld (%#lx) out of range)",
442 1.37 matt obj->path, where, defobj->strtab + def->st_name,
443 1.37 matt (long) tmp, (long) tmp);
444 1.37 matt return -1;
445 1.37 matt }
446 1.37 matt tmp >>= 2;
447 1.37 matt *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
448 1.40 matt rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
449 1.37 matt defobj->strtab + def->st_name, obj->path,
450 1.40 matt (void *)*where, where, defobj->path));
451 1.37 matt break;
452 1.37 matt }
453 1.52 fredette #endif /* __arm__ */
454 1.52 fredette
455 1.52 fredette #ifdef __hppa__
456 1.52 fredette case R_TYPE(DIR32):
457 1.52 fredette if (ELF_R_SYM(rela->r_info)) {
458 1.52 fredette /*
459 1.52 fredette * This is either a DIR32 against a symbol
460 1.52 fredette * (def->st_name != 0), or against a local
461 1.52 fredette * section (def->st_name == 0).
462 1.52 fredette */
463 1.52 fredette def = obj->symtab + ELF_R_SYM(rela->r_info);
464 1.52 fredette defobj = obj;
465 1.52 fredette if (def->st_name != 0)
466 1.52 fredette /*
467 1.52 fredette * While we're relocating self, _rtld_objlist
468 1.52 fredette * is NULL, so we just pass in self.
469 1.52 fredette */
470 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj,
471 1.55 junyoung &defobj, false);
472 1.52 fredette if (def == NULL)
473 1.52 fredette return -1;
474 1.52 fredette
475 1.52 fredette tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
476 1.52 fredette rela->r_addend);
477 1.52 fredette
478 1.52 fredette if (*where != tmp)
479 1.52 fredette *where = tmp;
480 1.52 fredette rdbg(dodebug, ("DIR32 %s in %s --> %p in %s",
481 1.52 fredette defobj->strtab + def->st_name, obj->path,
482 1.52 fredette (void *)*where, defobj->path));
483 1.52 fredette } else {
484 1.52 fredette extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
485 1.52 fredette extern Elf_Addr _GOT_END_[];
486 1.52 fredette
487 1.52 fredette tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
488 1.52 fredette
489 1.52 fredette /* This is the ...iffy hueristic. */
490 1.52 fredette if (!dodebug ||
491 1.52 fredette (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
492 1.52 fredette (caddr_t)where >= (caddr_t)_GOT_END_) {
493 1.52 fredette if (*where != tmp)
494 1.52 fredette *where = tmp;
495 1.52 fredette rdbg(dodebug, ("DIR32 in %s --> %p", obj->path,
496 1.52 fredette (void *)*where));
497 1.52 fredette } else
498 1.52 fredette rdbg(dodebug, ("DIR32 in %s stays at %p",
499 1.52 fredette obj->path, (void *)*where));
500 1.52 fredette }
501 1.52 fredette break;
502 1.52 fredette
503 1.52 fredette case R_TYPE(PLABEL32):
504 1.52 fredette if (ELF_R_SYM(rela->r_info)) {
505 1.52 fredette /*
506 1.52 fredette * While we're relocating self, _rtld_objlist
507 1.52 fredette * is NULL, so we just pass in self.
508 1.52 fredette */
509 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj,
510 1.55 junyoung false);
511 1.52 fredette if (def == NULL)
512 1.52 fredette return -1;
513 1.52 fredette
514 1.52 fredette tmp = _rtld_function_descriptor_alloc(defobj, def,
515 1.52 fredette rela->r_addend);
516 1.52 fredette if (tmp == (Elf_Addr)-1)
517 1.52 fredette return -1;
518 1.52 fredette
519 1.52 fredette if (*where != tmp)
520 1.52 fredette *where = tmp;
521 1.52 fredette rdbg(dodebug, ("PLABEL32 %s in %s --> %p in %s",
522 1.52 fredette defobj->strtab + def->st_name, obj->path,
523 1.52 fredette (void *)*where, defobj->path));
524 1.52 fredette } else {
525 1.52 fredette /*
526 1.52 fredette * This is a PLABEL for a static function, and the
527 1.52 fredette * dynamic linker has both allocated a PLT entry
528 1.52 fredette * for this function and told us where it is. We
529 1.52 fredette * can safely use the PLT entry as the PLABEL
530 1.52 fredette * because there should be no other PLABEL reloc
531 1.52 fredette * referencing this function. This object should
532 1.52 fredette * also have an IPLT relocation to initialize the
533 1.52 fredette * PLT entry.
534 1.52 fredette *
535 1.52 fredette * The dynamic linker should also have ensured
536 1.52 fredette * that the addend has the next-least-significant
537 1.52 fredette * bit set; the $$dyncall millicode uses this to
538 1.52 fredette * distinguish a PLABEL pointer from a plain
539 1.52 fredette * function pointer.
540 1.52 fredette */
541 1.52 fredette tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
542 1.52 fredette
543 1.52 fredette if (*where != tmp)
544 1.52 fredette *where = tmp;
545 1.52 fredette rdbg(dodebug, ("PLABEL32 in %s --> %p",
546 1.52 fredette obj->path, (void *)*where));
547 1.52 fredette }
548 1.52 fredette break;
549 1.52 fredette #endif /* __hppa__ */
550 1.21 matt
551 1.58 mycroft case R_TYPE(COPY):
552 1.58 mycroft /*
553 1.58 mycroft * These are deferred until all other relocations have
554 1.58 mycroft * been done. All we do here is make sure that the COPY
555 1.58 mycroft * relocation is not in a shared library. They are allowed
556 1.58 mycroft * only in executable files.
557 1.58 mycroft */
558 1.58 mycroft if (!obj->mainprog) {
559 1.58 mycroft _rtld_error(
560 1.58 mycroft "%s: Unexpected R_COPY relocation in shared library",
561 1.58 mycroft obj->path);
562 1.58 mycroft return -1;
563 1.58 mycroft }
564 1.58 mycroft rdbg(dodebug, ("COPY (avoid in main)"));
565 1.58 mycroft break;
566 1.58 mycroft
567 1.11 christos default:
568 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
569 1.16 christos rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
570 1.11 christos "addend = %p, contents = %p, symbol = %s",
571 1.11 christos (u_long)ELF_R_SYM(rela->r_info),
572 1.11 christos (u_long)ELF_R_TYPE(rela->r_info),
573 1.11 christos (void *)rela->r_offset, (void *)rela->r_addend,
574 1.11 christos (void *)*where,
575 1.16 christos def ? defobj->strtab + def->st_name : "??"));
576 1.33 dan _rtld_error("%s: Unsupported relocation type %ld "
577 1.11 christos "in non-PLT relocations\n",
578 1.33 dan obj->path, (u_long) ELF_R_TYPE(rela->r_info));
579 1.11 christos return -1;
580 1.11 christos }
581 1.11 christos return 0;
582 1.1 cgd }
583 1.9 pk
584 1.9 pk
585 1.52 fredette #if !defined(__powerpc__) && !defined(__hppa__)
586 1.9 pk
587 1.11 christos int
588 1.16 christos _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
589 1.23 mycroft Obj_Entry *obj;
590 1.35 kleink const Elf_Rela *rela;
591 1.16 christos caddr_t *addrp;
592 1.16 christos bool bind_now;
593 1.16 christos bool dodebug;
594 1.1 cgd {
595 1.16 christos Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
596 1.16 christos Elf_Addr new_value;
597 1.1 cgd
598 1.11 christos /* Fully resolve procedure addresses now */
599 1.2 mhitch
600 1.11 christos if (bind_now || obj->pltgot == NULL) {
601 1.11 christos const Elf_Sym *def;
602 1.11 christos const Obj_Entry *defobj;
603 1.11 christos
604 1.39 matt #if !defined(__arm__)
605 1.11 christos assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
606 1.39 matt #else
607 1.39 matt assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
608 1.39 matt #endif
609 1.1 cgd
610 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
611 1.11 christos if (def == NULL)
612 1.11 christos return -1;
613 1.1 cgd
614 1.11 christos new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
615 1.51 thorpej #if defined(__sh__) || defined(__vax__)
616 1.29 matt new_value += rela->r_addend;
617 1.29 matt #endif
618 1.16 christos rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
619 1.11 christos (int)bind_now,
620 1.11 christos defobj->strtab + def->st_name,
621 1.16 christos (void *)*where, (void *)new_value));
622 1.58 mycroft } else if (!obj->mainprog) {
623 1.11 christos /* Just relocate the GOT slots pointing into the PLT */
624 1.11 christos new_value = *where + (Elf_Addr)(obj->relocbase);
625 1.16 christos rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
626 1.16 christos (void *)*where));
627 1.11 christos } else {
628 1.11 christos return 0;
629 1.11 christos }
630 1.11 christos /*
631 1.11 christos * Since this page is probably copy-on-write, let's not write
632 1.11 christos * it unless we really really have to.
633 1.11 christos */
634 1.11 christos if (*where != new_value)
635 1.11 christos *where = new_value;
636 1.29 matt if (addrp != NULL) {
637 1.11 christos *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
638 1.29 matt #if defined(__vax__)
639 1.29 matt *addrp -= rela->r_addend;
640 1.29 matt #endif
641 1.29 matt }
642 1.7 tv return 0;
643 1.1 cgd }
644 1.42 mycroft
645 1.52 fredette #endif /* __powerpc__ || __hppa__ */
646 1.42 mycroft
647 1.58 mycroft #endif /* __sparc__ || __x86_64__ || __mips__ */
648 1.9 pk
649 1.1 cgd caddr_t
650 1.16 christos _rtld_bind(obj, reloff)
651 1.23 mycroft Obj_Entry *obj;
652 1.16 christos Elf_Word reloff;
653 1.1 cgd {
654 1.35 kleink const Elf_Rela *rela;
655 1.35 kleink Elf_Rela ourrela;
656 1.11 christos caddr_t addr;
657 1.1 cgd
658 1.11 christos if (obj->pltrel != NULL) {
659 1.11 christos const Elf_Rel *rel;
660 1.1 cgd
661 1.11 christos rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
662 1.11 christos ourrela.r_info = rel->r_info;
663 1.11 christos ourrela.r_offset = rel->r_offset;
664 1.28 matt ourrela.r_addend = 0;
665 1.11 christos rela = &ourrela;
666 1.11 christos } else {
667 1.35 kleink rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
668 1.41 eeh #ifdef __sparc64__
669 1.41 eeh if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
670 1.41 eeh /*
671 1.41 eeh * XXXX
672 1.41 eeh *
673 1.50 eeh * The first four PLT entries are reserved. There
674 1.41 eeh * is some disagreement whether they should have
675 1.41 eeh * associated relocation entries. Both the SPARC
676 1.41 eeh * 32-bit and 64-bit ELF specifications say that
677 1.41 eeh * they should have relocation entries, but the
678 1.41 eeh * 32-bit SPARC binutils do not generate them,
679 1.41 eeh * and now the 64-bit SPARC binutils have stopped
680 1.41 eeh * generating them too.
681 1.41 eeh *
682 1.41 eeh * So, to provide binary compatibility, we will
683 1.41 eeh * check the first entry, if it is reserved it
684 1.41 eeh * should not be of the type JMP_SLOT. If it
685 1.41 eeh * is JMP_SLOT, then the 4 reserved entries were
686 1.41 eeh * not generated and our index is 4 entries too far.
687 1.41 eeh */
688 1.41 eeh rela -= 4;
689 1.41 eeh }
690 1.41 eeh #endif
691 1.11 christos }
692 1.11 christos
693 1.11 christos if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
694 1.11 christos _rtld_die();
695 1.1 cgd
696 1.11 christos return addr;
697 1.1 cgd }
698 1.9 pk
699 1.1 cgd /*
700 1.1 cgd * Relocate newly-loaded shared objects. The argument is a pointer to
701 1.1 cgd * the Obj_Entry for the first such object. All objects from the first
702 1.1 cgd * to the end of the list of objects are relocated. Returns 0 on success,
703 1.1 cgd * or -1 on failure.
704 1.1 cgd */
705 1.1 cgd int
706 1.16 christos _rtld_relocate_objects(first, bind_now, dodebug)
707 1.16 christos Obj_Entry *first;
708 1.16 christos bool bind_now;
709 1.16 christos bool dodebug;
710 1.1 cgd {
711 1.16 christos Obj_Entry *obj;
712 1.16 christos int ok = 1;
713 1.1 cgd
714 1.11 christos for (obj = first; obj != NULL; obj = obj->next) {
715 1.16 christos if (obj->nbuckets == 0 || obj->nchains == 0 ||
716 1.16 christos obj->buckets == NULL || obj->symtab == NULL ||
717 1.16 christos obj->strtab == NULL) {
718 1.11 christos _rtld_error("%s: Shared object has no run-time"
719 1.11 christos " symbol table", obj->path);
720 1.11 christos return -1;
721 1.11 christos }
722 1.16 christos rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
723 1.11 christos "%ld/%ld plt rel/rela)",
724 1.11 christos obj->path,
725 1.11 christos (long)(obj->rellim - obj->rel),
726 1.11 christos (long)(obj->relalim - obj->rela),
727 1.11 christos (long)(obj->pltrellim - obj->pltrel),
728 1.16 christos (long)(obj->pltrelalim - obj->pltrela)));
729 1.11 christos
730 1.11 christos if (obj->textrel) {
731 1.11 christos /*
732 1.11 christos * There are relocations to the write-protected text
733 1.11 christos * segment.
734 1.11 christos */
735 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
736 1.11 christos PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
737 1.11 christos _rtld_error("%s: Cannot write-enable text "
738 1.11 christos "segment: %s", obj->path, xstrerror(errno));
739 1.11 christos return -1;
740 1.11 christos }
741 1.11 christos }
742 1.11 christos if (obj->rel != NULL) {
743 1.11 christos /* Process the non-PLT relocations. */
744 1.11 christos const Elf_Rel *rel;
745 1.11 christos for (rel = obj->rel; rel < obj->rellim; ++rel) {
746 1.35 kleink Elf_Rela ourrela;
747 1.11 christos ourrela.r_info = rel->r_info;
748 1.11 christos ourrela.r_offset = rel->r_offset;
749 1.2 mhitch #if defined(__mips__)
750 1.11 christos /* rel->r_offset is not valid on mips? */
751 1.11 christos if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
752 1.11 christos ourrela.r_addend = 0;
753 1.11 christos else
754 1.11 christos #endif
755 1.11 christos ourrela.r_addend =
756 1.11 christos *(Elf_Word *)(obj->relocbase +
757 1.11 christos rel->r_offset);
758 1.11 christos
759 1.11 christos if (_rtld_relocate_nonplt_object(obj, &ourrela,
760 1.11 christos dodebug) < 0)
761 1.11 christos ok = 0;
762 1.11 christos }
763 1.11 christos }
764 1.11 christos if (obj->rela != NULL) {
765 1.11 christos /* Process the non-PLT relocations. */
766 1.35 kleink const Elf_Rela *rela;
767 1.11 christos for (rela = obj->rela; rela < obj->relalim; ++rela) {
768 1.11 christos if (_rtld_relocate_nonplt_object(obj, rela,
769 1.11 christos dodebug) < 0)
770 1.11 christos ok = 0;
771 1.11 christos }
772 1.11 christos }
773 1.11 christos if (obj->textrel) { /* Re-protected the text segment. */
774 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
775 1.11 christos PROT_READ | PROT_EXEC) == -1) {
776 1.11 christos _rtld_error("%s: Cannot write-protect text "
777 1.11 christos "segment: %s", obj->path, xstrerror(errno));
778 1.11 christos return -1;
779 1.11 christos }
780 1.11 christos }
781 1.11 christos /* Process the PLT relocations. */
782 1.11 christos if (obj->pltrel != NULL) {
783 1.11 christos const Elf_Rel *rel;
784 1.11 christos for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
785 1.35 kleink Elf_Rela ourrela;
786 1.11 christos ourrela.r_info = rel->r_info;
787 1.11 christos ourrela.r_offset = rel->r_offset;
788 1.11 christos ourrela.r_addend =
789 1.11 christos *(Elf_Word *)(obj->relocbase +
790 1.11 christos rel->r_offset);
791 1.11 christos if (_rtld_relocate_plt_object(obj, &ourrela,
792 1.11 christos NULL, bind_now, dodebug) < 0)
793 1.11 christos ok = 0;
794 1.11 christos }
795 1.11 christos }
796 1.11 christos if (obj->pltrela != NULL) {
797 1.35 kleink const Elf_Rela *rela;
798 1.11 christos for (rela = obj->pltrela; rela < obj->pltrelalim;
799 1.11 christos ++rela) {
800 1.50 eeh #ifdef __sparc64__
801 1.50 eeh if (ELF_R_TYPE(rela->r_info) !=
802 1.50 eeh R_TYPE(JMP_SLOT)) {
803 1.50 eeh /*
804 1.50 eeh * XXXX
805 1.50 eeh *
806 1.50 eeh * The first four PLT entries are
807 1.50 eeh * reserved. There is some
808 1.50 eeh * disagreement whether they should
809 1.50 eeh * have associated relocation
810 1.50 eeh * entries. Both the SPARC 32-bit
811 1.50 eeh * and 64-bit ELF specifications say
812 1.50 eeh * that they should have relocation
813 1.50 eeh * entries, but the 32-bit SPARC
814 1.50 eeh * binutils do not generate them,
815 1.50 eeh * and now the 64-bit SPARC binutils
816 1.50 eeh * have stopped generating them too.
817 1.50 eeh *
818 1.50 eeh * To provide binary compatibility, we
819 1.50 eeh * will skip any entries that are not
820 1.50 eeh * of type JMP_SLOT.
821 1.50 eeh */
822 1.50 eeh continue;
823 1.50 eeh }
824 1.50 eeh #endif
825 1.11 christos if (_rtld_relocate_plt_object(obj, rela,
826 1.11 christos NULL, bind_now, dodebug) < 0)
827 1.11 christos ok = 0;
828 1.11 christos }
829 1.11 christos }
830 1.11 christos if (!ok)
831 1.11 christos return -1;
832 1.11 christos
833 1.11 christos
834 1.11 christos /* Set some sanity-checking numbers in the Obj_Entry. */
835 1.11 christos obj->magic = RTLD_MAGIC;
836 1.11 christos obj->version = RTLD_VERSION;
837 1.11 christos
838 1.11 christos /* Fill in the dynamic linker entry points. */
839 1.11 christos obj->dlopen = _rtld_dlopen;
840 1.11 christos obj->dlsym = _rtld_dlsym;
841 1.11 christos obj->dlerror = _rtld_dlerror;
842 1.11 christos obj->dlclose = _rtld_dlclose;
843 1.25 scottb obj->dladdr = _rtld_dladdr;
844 1.1 cgd
845 1.11 christos /* Set the special PLTGOT entries. */
846 1.53 mycroft if (obj->pltgot != NULL)
847 1.53 mycroft _rtld_setup_pltgot(obj);
848 1.1 cgd }
849 1.1 cgd
850 1.11 christos return 0;
851 1.1 cgd }
852