reloc.c revision 1.52 1 1.52 fredette /* $NetBSD: reloc.c,v 1.52 2002/07/10 15:12:35 fredette 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.36 fvdl #if !defined(__sparc__) && !defined(__x86_64__)
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.52 fredette #if defined(__alpha__) || defined(__arm__) || defined(__hppa__) || \
162 1.52 fredette defined(__i386__) || defined(__m68k__) || defined(__powerpc__) || \
163 1.52 fredette defined(__sh__) || defined(__vax__)
164 1.11 christos Elf_Addr tmp;
165 1.17 jonathan #endif
166 1.9 pk
167 1.11 christos switch (ELF_R_TYPE(rela->r_info)) {
168 1.9 pk
169 1.11 christos case R_TYPE(NONE):
170 1.11 christos break;
171 1.9 pk
172 1.24 itohy #if defined(__i386__)
173 1.11 christos case R_TYPE(GOT32):
174 1.9 pk
175 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
176 1.11 christos &defobj, false);
177 1.11 christos if (def == NULL)
178 1.11 christos return -1;
179 1.9 pk
180 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
181 1.11 christos if (*where != tmp)
182 1.11 christos *where = tmp;
183 1.16 christos rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
184 1.11 christos defobj->strtab + def->st_name, obj->path,
185 1.16 christos (void *)*where, defobj->path));
186 1.11 christos break;
187 1.9 pk
188 1.11 christos case R_TYPE(PC32):
189 1.11 christos /*
190 1.11 christos * I don't think the dynamic linker should ever see this
191 1.11 christos * type of relocation. But the binutils-2.6 tools sometimes
192 1.11 christos * generate it.
193 1.11 christos */
194 1.9 pk
195 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
196 1.11 christos &defobj, false);
197 1.11 christos if (def == NULL)
198 1.11 christos return -1;
199 1.9 pk
200 1.11 christos *where += (Elf_Addr)(defobj->relocbase + def->st_value) -
201 1.11 christos (Elf_Addr)where;
202 1.16 christos rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
203 1.11 christos defobj->strtab + def->st_name, obj->path,
204 1.16 christos (void *)*where, defobj->path));
205 1.11 christos break;
206 1.11 christos
207 1.11 christos case R_TYPE(32):
208 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
209 1.11 christos &defobj, false);
210 1.11 christos if (def == NULL)
211 1.11 christos return -1;
212 1.9 pk
213 1.11 christos *where += (Elf_Addr)(defobj->relocbase + def->st_value);
214 1.16 christos rdbg(dodebug, ("32 %s in %s --> %p in %s",
215 1.11 christos defobj->strtab + def->st_name, obj->path,
216 1.16 christos (void *)*where, defobj->path));
217 1.11 christos break;
218 1.24 itohy #endif /* __i386__ */
219 1.24 itohy
220 1.24 itohy #if defined(__m68k__)
221 1.24 itohy case R_TYPE(GOT32):
222 1.24 itohy def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
223 1.24 itohy &defobj, false);
224 1.24 itohy if (def == NULL)
225 1.24 itohy return -1;
226 1.24 itohy
227 1.24 itohy tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
228 1.24 itohy rela->r_addend);
229 1.24 itohy if (*where != tmp)
230 1.24 itohy *where = tmp;
231 1.24 itohy rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
232 1.24 itohy defobj->strtab + def->st_name, obj->path,
233 1.24 itohy (void *)*where, defobj->path));
234 1.24 itohy break;
235 1.24 itohy
236 1.24 itohy case R_TYPE(PC32):
237 1.24 itohy def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
238 1.24 itohy &defobj, false);
239 1.24 itohy if (def == NULL)
240 1.24 itohy return -1;
241 1.24 itohy
242 1.24 itohy tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
243 1.24 itohy rela->r_addend) - (Elf_Addr)where;
244 1.24 itohy if (*where != tmp)
245 1.24 itohy *where = tmp;
246 1.24 itohy rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
247 1.24 itohy defobj->strtab + def->st_name, obj->path,
248 1.24 itohy (void *)*where, defobj->path));
249 1.24 itohy break;
250 1.24 itohy
251 1.24 itohy case R_TYPE(32):
252 1.24 itohy def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
253 1.24 itohy &defobj, false);
254 1.24 itohy if (def == NULL)
255 1.24 itohy return -1;
256 1.24 itohy
257 1.24 itohy tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
258 1.24 itohy rela->r_addend);
259 1.24 itohy if (*where != tmp)
260 1.24 itohy *where = tmp;
261 1.24 itohy rdbg(dodebug, ("32 %s in %s --> %p in %s",
262 1.24 itohy defobj->strtab + def->st_name, obj->path,
263 1.24 itohy (void *)*where, defobj->path));
264 1.24 itohy break;
265 1.24 itohy #endif /* __m68k__ */
266 1.4 christos
267 1.51 thorpej #if defined(__sh__)
268 1.51 thorpej case R_TYPE(GOT32):
269 1.51 thorpej def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
270 1.51 thorpej &defobj, false);
271 1.51 thorpej if (def == NULL)
272 1.51 thorpej return -1;
273 1.51 thorpej
274 1.51 thorpej tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
275 1.51 thorpej rela->r_addend);
276 1.51 thorpej if (*where != tmp)
277 1.51 thorpej *where = tmp;
278 1.51 thorpej rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
279 1.51 thorpej defobj->strtab + def->st_name, obj->path,
280 1.51 thorpej (void *)*where, defobj->path));
281 1.51 thorpej break;
282 1.51 thorpej
283 1.51 thorpej case R_TYPE(REL32):
284 1.51 thorpej /*
285 1.51 thorpej * I don't think the dynamic linker should ever see this
286 1.51 thorpej * type of relocation, but some versions of Binutils
287 1.51 thorpej * generate it.
288 1.51 thorpej */
289 1.51 thorpej def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
290 1.51 thorpej &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) - (Elf_Addr)where;
296 1.51 thorpej rdbg(dodebug, ("PC32 %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
301 1.51 thorpej case R_TYPE(DIR32):
302 1.51 thorpej def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
303 1.51 thorpej &defobj, false);
304 1.51 thorpej if (def == NULL)
305 1.51 thorpej return -1;
306 1.51 thorpej
307 1.51 thorpej *where += (Elf_Addr)(defobj->relocbase + def->st_value +
308 1.51 thorpej rela->r_addend);
309 1.51 thorpej rdbg(dodebug, ("32 %s in %s --> %p in %s",
310 1.51 thorpej defobj->strtab + def->st_name, obj->path,
311 1.51 thorpej (void *)*where, defobj->path));
312 1.51 thorpej break;
313 1.51 thorpej #endif /* __sh__ */
314 1.51 thorpej
315 1.21 matt #if defined(__alpha__)
316 1.11 christos case R_TYPE(REFQUAD):
317 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
318 1.11 christos &defobj, false);
319 1.11 christos if (def == NULL)
320 1.11 christos return -1;
321 1.1 cgd
322 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
323 1.11 christos *where + rela->r_addend;
324 1.12 tv if (*where != tmp)
325 1.12 tv *where = tmp;
326 1.16 christos rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
327 1.11 christos defobj->strtab + def->st_name, obj->path,
328 1.16 christos (void *)*where, defobj->path));
329 1.11 christos break;
330 1.46 thorpej
331 1.46 thorpej case R_TYPE(RELATIVE):
332 1.46 thorpej {
333 1.46 thorpej extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
334 1.46 thorpej extern Elf_Addr _GOT_END_[];
335 1.46 thorpej
336 1.46 thorpej /* This is the ...iffy hueristic. */
337 1.46 thorpej if (!dodebug ||
338 1.46 thorpej (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
339 1.46 thorpej (caddr_t)where >= (caddr_t)_GOT_END_) {
340 1.48 thorpej *where += (Elf_Addr)obj->relocbase;
341 1.46 thorpej rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
342 1.46 thorpej (void *)*where));
343 1.46 thorpej } else
344 1.46 thorpej rdbg(dodebug, ("RELATIVE in %s stays at %p",
345 1.46 thorpej obj->path, (void *)*where));
346 1.46 thorpej break;
347 1.46 thorpej }
348 1.4 christos #endif /* __alpha__ */
349 1.1 cgd
350 1.51 thorpej #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
351 1.51 thorpej defined(__sh__)
352 1.11 christos case R_TYPE(GLOB_DAT):
353 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
354 1.11 christos &defobj, false);
355 1.11 christos if (def == NULL)
356 1.11 christos return -1;
357 1.1 cgd
358 1.47 thorpej tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
359 1.47 thorpej rela->r_addend;
360 1.47 thorpej if (*where != tmp)
361 1.47 thorpej *where = tmp;
362 1.16 christos rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
363 1.11 christos defobj->strtab + def->st_name, obj->path,
364 1.16 christos (void *)*where, defobj->path));
365 1.11 christos break;
366 1.51 thorpej #if defined(__sh__)
367 1.51 thorpej case R_TYPE(RELATIVE):
368 1.51 thorpej if (rela->r_addend)
369 1.51 thorpej *where = (Elf_Addr)obj->relocbase + rela->r_addend;
370 1.51 thorpej else
371 1.51 thorpej *where += (Elf_Addr)obj->relocbase;
372 1.51 thorpej rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
373 1.51 thorpej (void *)*where));
374 1.51 thorpej break;
375 1.51 thorpej #elif !defined(__alpha__)
376 1.11 christos case R_TYPE(RELATIVE):
377 1.46 thorpej *where += (Elf_Addr)obj->relocbase;
378 1.46 thorpej rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
379 1.46 thorpej (void *)*where));
380 1.11 christos break;
381 1.46 thorpej #endif /* ! __alpha__ */
382 1.52 fredette #endif /* __alpha__ || __i386__ || __m68k__ || __sh__ */
383 1.1 cgd
384 1.52 fredette #if defined(__alpha__) || defined(__hppa__) || defined(__i386__) || \
385 1.52 fredette defined(__m68k__) || defined(__sh__)
386 1.11 christos case R_TYPE(COPY):
387 1.11 christos /*
388 1.11 christos * These are deferred until all other relocations have
389 1.11 christos * been done. All we do here is make sure that the COPY
390 1.11 christos * relocation is not in a shared library. They are allowed
391 1.11 christos * only in executable files.
392 1.11 christos */
393 1.11 christos if (!obj->mainprog) {
394 1.11 christos _rtld_error(
395 1.11 christos "%s: Unexpected R_COPY relocation in shared library",
396 1.11 christos obj->path);
397 1.11 christos return -1;
398 1.11 christos }
399 1.16 christos rdbg(dodebug, ("COPY (avoid in main)"));
400 1.11 christos break;
401 1.52 fredette #endif /* __alpha__ || __hppa__ || __i386__ || __m68k__ || __sh__ */
402 1.2 mhitch
403 1.21 matt #if defined(__mips__)
404 1.11 christos case R_TYPE(REL32):
405 1.11 christos /* 32-bit PC-relative reference */
406 1.11 christos def = obj->symtab + ELF_R_SYM(rela->r_info);
407 1.11 christos
408 1.22 simonb if (ELFDEFNNAME(ST_BIND)(def->st_info) == STB_LOCAL &&
409 1.22 simonb (ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_SECTION ||
410 1.22 simonb ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_NOTYPE)) {
411 1.43 rafal /*
412 1.43 rafal * XXX: ABI DIFFERENCE!
413 1.43 rafal *
414 1.43 rafal * Old NetBSD binutils would generate shared libs
415 1.43 rafal * with section-relative relocations being already
416 1.43 rafal * adjusted for the start address of the section.
417 1.43 rafal *
418 1.43 rafal * New binutils, OTOH, generate shared libs with
419 1.43 rafal * the same relocations being based at zero, so we
420 1.43 rafal * need to add in the start address of the section.
421 1.43 rafal *
422 1.43 rafal * We assume that all section-relative relocs with
423 1.43 rafal * contents less than the start of the section need
424 1.43 rafal * to be adjusted; this should work with both old
425 1.43 rafal * and new shlibs.
426 1.43 rafal *
427 1.43 rafal * --rkb, Oct 6, 2001
428 1.43 rafal */
429 1.43 rafal if (def->st_info == STT_SECTION &&
430 1.43 rafal (*where < def->st_value))
431 1.43 rafal *where += (Elf_Addr) def->st_value;
432 1.43 rafal
433 1.11 christos *where += (Elf_Addr)obj->relocbase;
434 1.43 rafal
435 1.43 rafal rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
436 1.43 rafal obj->strtab + def->st_name, obj->path,
437 1.43 rafal (void *)*where, obj->path));
438 1.11 christos } else {
439 1.11 christos /* XXX maybe do something re: bootstrapping? */
440 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
441 1.11 christos NULL, obj, &defobj, false);
442 1.11 christos if (def == NULL)
443 1.11 christos return -1;
444 1.11 christos *where += (Elf_Addr)(defobj->relocbase + def->st_value);
445 1.16 christos rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
446 1.11 christos defobj->strtab + def->st_name, obj->path,
447 1.16 christos (void *)*where, defobj->path));
448 1.11 christos }
449 1.11 christos break;
450 1.2 mhitch
451 1.11 christos #endif /* __mips__ */
452 1.1 cgd
453 1.21 matt #if defined(__powerpc__) || defined(__vax__)
454 1.11 christos case R_TYPE(32): /* word32 S + A */
455 1.11 christos case R_TYPE(GLOB_DAT): /* word32 S + A */
456 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
457 1.11 christos &defobj, false);
458 1.11 christos if (def == NULL)
459 1.11 christos return -1;
460 1.3 tsubai
461 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
462 1.11 christos rela->r_addend);
463 1.3 tsubai
464 1.11 christos if (*where != tmp)
465 1.11 christos *where = tmp;
466 1.16 christos rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
467 1.11 christos defobj->strtab + def->st_name, obj->path,
468 1.16 christos (void *)*where, defobj->path));
469 1.11 christos break;
470 1.11 christos
471 1.11 christos case R_TYPE(COPY):
472 1.16 christos rdbg(dodebug, ("COPY"));
473 1.11 christos break;
474 1.11 christos
475 1.11 christos case R_TYPE(JMP_SLOT):
476 1.16 christos rdbg(dodebug, ("JMP_SLOT"));
477 1.11 christos break;
478 1.11 christos
479 1.11 christos case R_TYPE(RELATIVE): /* word32 B + A */
480 1.11 christos tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
481 1.21 matt if (*where != tmp)
482 1.21 matt *where = tmp;
483 1.16 christos rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
484 1.16 christos (void *)*where));
485 1.11 christos break;
486 1.21 matt #endif /* __powerpc__ || __vax__ */
487 1.26 kleink
488 1.26 kleink #if defined(__powerpc__)
489 1.26 kleink case R_TYPE(16_LO): /* #lo(S + A) */
490 1.26 kleink tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
491 1.26 kleink if (*(Elf32_Half *)where != tmp)
492 1.26 kleink *(Elf32_Half *)where = tmp;
493 1.26 kleink rdbg(dodebug, ("16_LO in %s --> %p", obj->path,
494 1.26 kleink (void *)*where));
495 1.26 kleink break;
496 1.26 kleink
497 1.26 kleink case R_TYPE(16_HI): /* #hi(S + A) */
498 1.26 kleink tmp = (Elf_Addr)(obj->relocbase + rela->r_addend) >> 16;
499 1.26 kleink if (*(Elf32_Half *)where != tmp)
500 1.26 kleink *(Elf32_Half *)where = tmp;
501 1.26 kleink rdbg(dodebug, ("16_HI in %s --> %p", obj->path,
502 1.26 kleink (void *)*where));
503 1.26 kleink break;
504 1.26 kleink
505 1.26 kleink case R_TYPE(16_HA): /* #ha(S + A) */
506 1.26 kleink tmp = (Elf_Addr)(obj->relocbase + rela->r_addend + 0x8000)
507 1.26 kleink >> 16;
508 1.26 kleink if (*(Elf32_Half *)where != tmp)
509 1.26 kleink *(Elf32_Half *)where = tmp;
510 1.26 kleink rdbg(dodebug, ("16_HA in %s --> %p", obj->path,
511 1.26 kleink (void *)*where));
512 1.26 kleink break;
513 1.26 kleink #endif /* __powerpc__ */
514 1.37 matt
515 1.37 matt #if defined(__arm__)
516 1.37 matt case R_TYPE(GLOB_DAT): /* word32 B + S */
517 1.37 matt def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
518 1.37 matt &defobj, false);
519 1.37 matt if (def == NULL)
520 1.37 matt return -1;
521 1.37 matt *where = (Elf_Addr)(defobj->relocbase + def->st_value);
522 1.40 matt rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @ %p in %s",
523 1.37 matt defobj->strtab + def->st_name, obj->path,
524 1.40 matt (void *)*where, where, defobj->path));
525 1.37 matt break;
526 1.37 matt
527 1.37 matt case R_TYPE(COPY):
528 1.37 matt rdbg(dodebug, ("COPY"));
529 1.37 matt break;
530 1.37 matt
531 1.37 matt case R_TYPE(JUMP_SLOT):
532 1.37 matt rdbg(dodebug, ("JUMP_SLOT"));
533 1.37 matt break;
534 1.37 matt
535 1.37 matt case R_TYPE(ABS32): /* word32 B + S + A */
536 1.37 matt def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
537 1.37 matt &defobj, false);
538 1.37 matt if (def == NULL)
539 1.37 matt return -1;
540 1.40 matt *where += (Elf_Addr)defobj->relocbase + def->st_value;
541 1.40 matt rdbg(dodebug, ("ABS32 %s in %s --> %p @ %p in %s",
542 1.37 matt defobj->strtab + def->st_name, obj->path,
543 1.40 matt (void *)*where, where, defobj->path));
544 1.37 matt break;
545 1.37 matt
546 1.37 matt case R_TYPE(RELATIVE): /* word32 B + A */
547 1.37 matt *where += (Elf_Addr)obj->relocbase;
548 1.40 matt rdbg(dodebug, ("RELATIVE in %s --> %p @ %p", obj->path,
549 1.40 matt (void *)*where, where));
550 1.37 matt break;
551 1.37 matt
552 1.37 matt case R_TYPE(PC24): { /* word32 S - P + A */
553 1.37 matt Elf32_Sword addend;
554 1.37 matt
555 1.37 matt /*
556 1.37 matt * Extract addend and sign-extend if needed.
557 1.37 matt */
558 1.37 matt addend = *where;
559 1.37 matt if (addend & 0x00800000)
560 1.37 matt addend |= 0xff000000;
561 1.37 matt
562 1.37 matt def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
563 1.37 matt &defobj, false);
564 1.37 matt if (def == NULL)
565 1.37 matt return -1;
566 1.37 matt tmp = (Elf_Addr)obj->relocbase + def->st_value
567 1.37 matt - (Elf_Addr)where + (addend << 2);
568 1.49 thorpej if ((tmp & 0xfe000000) != 0xfe000000 &&
569 1.49 thorpej (tmp & 0xfe000000) != 0) {
570 1.37 matt _rtld_error(
571 1.37 matt "%s: R_ARM_PC24 relocation @ %p to %s failed "
572 1.37 matt "(displacement %ld (%#lx) out of range)",
573 1.37 matt obj->path, where, defobj->strtab + def->st_name,
574 1.37 matt (long) tmp, (long) tmp);
575 1.37 matt return -1;
576 1.37 matt }
577 1.37 matt tmp >>= 2;
578 1.37 matt *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
579 1.40 matt rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
580 1.37 matt defobj->strtab + def->st_name, obj->path,
581 1.40 matt (void *)*where, where, defobj->path));
582 1.37 matt break;
583 1.37 matt }
584 1.52 fredette #endif /* __arm__ */
585 1.52 fredette
586 1.52 fredette #ifdef __hppa__
587 1.52 fredette case R_TYPE(DIR32):
588 1.52 fredette if (ELF_R_SYM(rela->r_info)) {
589 1.52 fredette /*
590 1.52 fredette * This is either a DIR32 against a symbol
591 1.52 fredette * (def->st_name != 0), or against a local
592 1.52 fredette * section (def->st_name == 0).
593 1.52 fredette */
594 1.52 fredette def = obj->symtab + ELF_R_SYM(rela->r_info);
595 1.52 fredette defobj = obj;
596 1.52 fredette if (def->st_name != 0)
597 1.52 fredette /*
598 1.52 fredette * While we're relocating self, _rtld_objlist
599 1.52 fredette * is NULL, so we just pass in self.
600 1.52 fredette */
601 1.52 fredette def = _rtld_find_symdef((_rtld_objlist == NULL ?
602 1.52 fredette obj : _rtld_objlist), rela->r_info,
603 1.52 fredette NULL, obj, &defobj, false);
604 1.52 fredette if (def == NULL)
605 1.52 fredette return -1;
606 1.52 fredette
607 1.52 fredette tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
608 1.52 fredette rela->r_addend);
609 1.52 fredette
610 1.52 fredette if (*where != tmp)
611 1.52 fredette *where = tmp;
612 1.52 fredette rdbg(dodebug, ("DIR32 %s in %s --> %p in %s",
613 1.52 fredette defobj->strtab + def->st_name, obj->path,
614 1.52 fredette (void *)*where, defobj->path));
615 1.52 fredette } else {
616 1.52 fredette extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
617 1.52 fredette extern Elf_Addr _GOT_END_[];
618 1.52 fredette
619 1.52 fredette tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
620 1.52 fredette
621 1.52 fredette /* This is the ...iffy hueristic. */
622 1.52 fredette if (!dodebug ||
623 1.52 fredette (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
624 1.52 fredette (caddr_t)where >= (caddr_t)_GOT_END_) {
625 1.52 fredette if (*where != tmp)
626 1.52 fredette *where = tmp;
627 1.52 fredette rdbg(dodebug, ("DIR32 in %s --> %p", obj->path,
628 1.52 fredette (void *)*where));
629 1.52 fredette } else
630 1.52 fredette rdbg(dodebug, ("DIR32 in %s stays at %p",
631 1.52 fredette obj->path, (void *)*where));
632 1.52 fredette }
633 1.52 fredette break;
634 1.52 fredette
635 1.52 fredette case R_TYPE(PLABEL32):
636 1.52 fredette if (ELF_R_SYM(rela->r_info)) {
637 1.52 fredette /*
638 1.52 fredette * While we're relocating self, _rtld_objlist
639 1.52 fredette * is NULL, so we just pass in self.
640 1.52 fredette */
641 1.52 fredette def = _rtld_find_symdef((_rtld_objlist == NULL ?
642 1.52 fredette obj : _rtld_objlist), rela->r_info,
643 1.52 fredette NULL, obj, &defobj, false);
644 1.52 fredette if (def == NULL)
645 1.52 fredette return -1;
646 1.52 fredette
647 1.52 fredette tmp = _rtld_function_descriptor_alloc(defobj, def,
648 1.52 fredette rela->r_addend);
649 1.52 fredette if (tmp == (Elf_Addr)-1)
650 1.52 fredette return -1;
651 1.52 fredette
652 1.52 fredette if (*where != tmp)
653 1.52 fredette *where = tmp;
654 1.52 fredette rdbg(dodebug, ("PLABEL32 %s in %s --> %p in %s",
655 1.52 fredette defobj->strtab + def->st_name, obj->path,
656 1.52 fredette (void *)*where, defobj->path));
657 1.52 fredette } else {
658 1.52 fredette /*
659 1.52 fredette * This is a PLABEL for a static function, and the
660 1.52 fredette * dynamic linker has both allocated a PLT entry
661 1.52 fredette * for this function and told us where it is. We
662 1.52 fredette * can safely use the PLT entry as the PLABEL
663 1.52 fredette * because there should be no other PLABEL reloc
664 1.52 fredette * referencing this function. This object should
665 1.52 fredette * also have an IPLT relocation to initialize the
666 1.52 fredette * PLT entry.
667 1.52 fredette *
668 1.52 fredette * The dynamic linker should also have ensured
669 1.52 fredette * that the addend has the next-least-significant
670 1.52 fredette * bit set; the $$dyncall millicode uses this to
671 1.52 fredette * distinguish a PLABEL pointer from a plain
672 1.52 fredette * function pointer.
673 1.52 fredette */
674 1.52 fredette tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
675 1.52 fredette
676 1.52 fredette if (*where != tmp)
677 1.52 fredette *where = tmp;
678 1.52 fredette rdbg(dodebug, ("PLABEL32 in %s --> %p",
679 1.52 fredette obj->path, (void *)*where));
680 1.52 fredette }
681 1.52 fredette break;
682 1.52 fredette #endif /* __hppa__ */
683 1.21 matt
684 1.11 christos default:
685 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
686 1.11 christos &defobj, true);
687 1.16 christos rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
688 1.11 christos "addend = %p, contents = %p, symbol = %s",
689 1.11 christos (u_long)ELF_R_SYM(rela->r_info),
690 1.11 christos (u_long)ELF_R_TYPE(rela->r_info),
691 1.11 christos (void *)rela->r_offset, (void *)rela->r_addend,
692 1.11 christos (void *)*where,
693 1.16 christos def ? defobj->strtab + def->st_name : "??"));
694 1.33 dan _rtld_error("%s: Unsupported relocation type %ld "
695 1.11 christos "in non-PLT relocations\n",
696 1.33 dan obj->path, (u_long) ELF_R_TYPE(rela->r_info));
697 1.11 christos return -1;
698 1.11 christos }
699 1.11 christos return 0;
700 1.1 cgd }
701 1.9 pk
702 1.9 pk
703 1.52 fredette #if !defined(__powerpc__) && !defined(__hppa__)
704 1.9 pk
705 1.11 christos int
706 1.16 christos _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
707 1.23 mycroft Obj_Entry *obj;
708 1.35 kleink const Elf_Rela *rela;
709 1.16 christos caddr_t *addrp;
710 1.16 christos bool bind_now;
711 1.16 christos bool dodebug;
712 1.1 cgd {
713 1.16 christos Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
714 1.16 christos Elf_Addr new_value;
715 1.1 cgd
716 1.11 christos /* Fully resolve procedure addresses now */
717 1.2 mhitch
718 1.39 matt #if defined(__alpha__) || defined(__arm__) || defined(__i386__) || \
719 1.51 thorpej defined(__m68k__) || defined(__sh__) || defined(__vax__)
720 1.11 christos if (bind_now || obj->pltgot == NULL) {
721 1.11 christos const Elf_Sym *def;
722 1.11 christos const Obj_Entry *defobj;
723 1.11 christos
724 1.39 matt #if !defined(__arm__)
725 1.11 christos assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
726 1.39 matt #else
727 1.39 matt assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
728 1.39 matt #endif
729 1.1 cgd
730 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
731 1.11 christos &defobj, true);
732 1.11 christos if (def == NULL)
733 1.11 christos return -1;
734 1.1 cgd
735 1.11 christos new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
736 1.51 thorpej #if defined(__sh__) || defined(__vax__)
737 1.29 matt new_value += rela->r_addend;
738 1.29 matt #endif
739 1.16 christos rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
740 1.11 christos (int)bind_now,
741 1.11 christos defobj->strtab + def->st_name,
742 1.16 christos (void *)*where, (void *)new_value));
743 1.11 christos } else
744 1.51 thorpej #endif /* __alpha__ || __i386__ || __m68k__ || __sh__ || __vax__ */
745 1.11 christos if (!obj->mainprog) {
746 1.11 christos /* Just relocate the GOT slots pointing into the PLT */
747 1.11 christos new_value = *where + (Elf_Addr)(obj->relocbase);
748 1.16 christos rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
749 1.16 christos (void *)*where));
750 1.11 christos } else {
751 1.11 christos return 0;
752 1.11 christos }
753 1.11 christos /*
754 1.11 christos * Since this page is probably copy-on-write, let's not write
755 1.11 christos * it unless we really really have to.
756 1.11 christos */
757 1.11 christos if (*where != new_value)
758 1.11 christos *where = new_value;
759 1.29 matt if (addrp != NULL) {
760 1.11 christos *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
761 1.29 matt #if defined(__vax__)
762 1.29 matt *addrp -= rela->r_addend;
763 1.29 matt #endif
764 1.29 matt }
765 1.7 tv return 0;
766 1.1 cgd }
767 1.42 mycroft
768 1.52 fredette #endif /* __powerpc__ || __hppa__ */
769 1.42 mycroft
770 1.42 mycroft #endif /* __sparc__ || __x86_64__ */
771 1.9 pk
772 1.1 cgd caddr_t
773 1.16 christos _rtld_bind(obj, reloff)
774 1.23 mycroft Obj_Entry *obj;
775 1.16 christos Elf_Word reloff;
776 1.1 cgd {
777 1.35 kleink const Elf_Rela *rela;
778 1.35 kleink Elf_Rela ourrela;
779 1.11 christos caddr_t addr;
780 1.1 cgd
781 1.11 christos if (obj->pltrel != NULL) {
782 1.11 christos const Elf_Rel *rel;
783 1.1 cgd
784 1.11 christos rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
785 1.11 christos ourrela.r_info = rel->r_info;
786 1.11 christos ourrela.r_offset = rel->r_offset;
787 1.28 matt ourrela.r_addend = 0;
788 1.11 christos rela = &ourrela;
789 1.11 christos } else {
790 1.35 kleink rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
791 1.41 eeh #ifdef __sparc64__
792 1.41 eeh if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
793 1.41 eeh /*
794 1.41 eeh * XXXX
795 1.41 eeh *
796 1.50 eeh * The first four PLT entries are reserved. There
797 1.41 eeh * is some disagreement whether they should have
798 1.41 eeh * associated relocation entries. Both the SPARC
799 1.41 eeh * 32-bit and 64-bit ELF specifications say that
800 1.41 eeh * they should have relocation entries, but the
801 1.41 eeh * 32-bit SPARC binutils do not generate them,
802 1.41 eeh * and now the 64-bit SPARC binutils have stopped
803 1.41 eeh * generating them too.
804 1.41 eeh *
805 1.41 eeh * So, to provide binary compatibility, we will
806 1.41 eeh * check the first entry, if it is reserved it
807 1.41 eeh * should not be of the type JMP_SLOT. If it
808 1.41 eeh * is JMP_SLOT, then the 4 reserved entries were
809 1.41 eeh * not generated and our index is 4 entries too far.
810 1.41 eeh */
811 1.41 eeh rela -= 4;
812 1.41 eeh }
813 1.41 eeh #endif
814 1.11 christos }
815 1.11 christos
816 1.11 christos if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
817 1.11 christos _rtld_die();
818 1.1 cgd
819 1.11 christos return addr;
820 1.1 cgd }
821 1.9 pk
822 1.1 cgd /*
823 1.1 cgd * Relocate newly-loaded shared objects. The argument is a pointer to
824 1.1 cgd * the Obj_Entry for the first such object. All objects from the first
825 1.1 cgd * to the end of the list of objects are relocated. Returns 0 on success,
826 1.1 cgd * or -1 on failure.
827 1.1 cgd */
828 1.1 cgd int
829 1.16 christos _rtld_relocate_objects(first, bind_now, dodebug)
830 1.16 christos Obj_Entry *first;
831 1.16 christos bool bind_now;
832 1.16 christos bool dodebug;
833 1.1 cgd {
834 1.16 christos Obj_Entry *obj;
835 1.16 christos int ok = 1;
836 1.1 cgd
837 1.11 christos for (obj = first; obj != NULL; obj = obj->next) {
838 1.16 christos if (obj->nbuckets == 0 || obj->nchains == 0 ||
839 1.16 christos obj->buckets == NULL || obj->symtab == NULL ||
840 1.16 christos obj->strtab == NULL) {
841 1.11 christos _rtld_error("%s: Shared object has no run-time"
842 1.11 christos " symbol table", obj->path);
843 1.11 christos return -1;
844 1.11 christos }
845 1.16 christos rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
846 1.11 christos "%ld/%ld plt rel/rela)",
847 1.11 christos obj->path,
848 1.11 christos (long)(obj->rellim - obj->rel),
849 1.11 christos (long)(obj->relalim - obj->rela),
850 1.11 christos (long)(obj->pltrellim - obj->pltrel),
851 1.16 christos (long)(obj->pltrelalim - obj->pltrela)));
852 1.11 christos
853 1.11 christos if (obj->textrel) {
854 1.11 christos /*
855 1.11 christos * There are relocations to the write-protected text
856 1.11 christos * segment.
857 1.11 christos */
858 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
859 1.11 christos PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
860 1.11 christos _rtld_error("%s: Cannot write-enable text "
861 1.11 christos "segment: %s", obj->path, xstrerror(errno));
862 1.11 christos return -1;
863 1.11 christos }
864 1.11 christos }
865 1.11 christos if (obj->rel != NULL) {
866 1.11 christos /* Process the non-PLT relocations. */
867 1.11 christos const Elf_Rel *rel;
868 1.11 christos for (rel = obj->rel; rel < obj->rellim; ++rel) {
869 1.35 kleink Elf_Rela ourrela;
870 1.11 christos ourrela.r_info = rel->r_info;
871 1.11 christos ourrela.r_offset = rel->r_offset;
872 1.2 mhitch #if defined(__mips__)
873 1.11 christos /* rel->r_offset is not valid on mips? */
874 1.11 christos if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
875 1.11 christos ourrela.r_addend = 0;
876 1.11 christos else
877 1.11 christos #endif
878 1.11 christos ourrela.r_addend =
879 1.11 christos *(Elf_Word *)(obj->relocbase +
880 1.11 christos rel->r_offset);
881 1.11 christos
882 1.11 christos if (_rtld_relocate_nonplt_object(obj, &ourrela,
883 1.11 christos dodebug) < 0)
884 1.11 christos ok = 0;
885 1.11 christos }
886 1.11 christos }
887 1.11 christos if (obj->rela != NULL) {
888 1.11 christos /* Process the non-PLT relocations. */
889 1.35 kleink const Elf_Rela *rela;
890 1.11 christos for (rela = obj->rela; rela < obj->relalim; ++rela) {
891 1.11 christos if (_rtld_relocate_nonplt_object(obj, rela,
892 1.11 christos dodebug) < 0)
893 1.11 christos ok = 0;
894 1.11 christos }
895 1.11 christos }
896 1.11 christos if (obj->textrel) { /* Re-protected the text segment. */
897 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
898 1.11 christos PROT_READ | PROT_EXEC) == -1) {
899 1.11 christos _rtld_error("%s: Cannot write-protect text "
900 1.11 christos "segment: %s", obj->path, xstrerror(errno));
901 1.11 christos return -1;
902 1.11 christos }
903 1.11 christos }
904 1.11 christos /* Process the PLT relocations. */
905 1.11 christos if (obj->pltrel != NULL) {
906 1.11 christos const Elf_Rel *rel;
907 1.11 christos for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
908 1.35 kleink Elf_Rela ourrela;
909 1.11 christos ourrela.r_info = rel->r_info;
910 1.11 christos ourrela.r_offset = rel->r_offset;
911 1.11 christos ourrela.r_addend =
912 1.11 christos *(Elf_Word *)(obj->relocbase +
913 1.11 christos rel->r_offset);
914 1.11 christos if (_rtld_relocate_plt_object(obj, &ourrela,
915 1.11 christos NULL, bind_now, dodebug) < 0)
916 1.11 christos ok = 0;
917 1.11 christos }
918 1.11 christos }
919 1.11 christos if (obj->pltrela != NULL) {
920 1.35 kleink const Elf_Rela *rela;
921 1.11 christos for (rela = obj->pltrela; rela < obj->pltrelalim;
922 1.11 christos ++rela) {
923 1.50 eeh #ifdef __sparc64__
924 1.50 eeh if (ELF_R_TYPE(rela->r_info) !=
925 1.50 eeh R_TYPE(JMP_SLOT)) {
926 1.50 eeh /*
927 1.50 eeh * XXXX
928 1.50 eeh *
929 1.50 eeh * The first four PLT entries are
930 1.50 eeh * reserved. There is some
931 1.50 eeh * disagreement whether they should
932 1.50 eeh * have associated relocation
933 1.50 eeh * entries. Both the SPARC 32-bit
934 1.50 eeh * and 64-bit ELF specifications say
935 1.50 eeh * that they should have relocation
936 1.50 eeh * entries, but the 32-bit SPARC
937 1.50 eeh * binutils do not generate them,
938 1.50 eeh * and now the 64-bit SPARC binutils
939 1.50 eeh * have stopped generating them too.
940 1.50 eeh *
941 1.50 eeh * To provide binary compatibility, we
942 1.50 eeh * will skip any entries that are not
943 1.50 eeh * of type JMP_SLOT.
944 1.50 eeh */
945 1.50 eeh continue;
946 1.50 eeh }
947 1.50 eeh #endif
948 1.11 christos if (_rtld_relocate_plt_object(obj, rela,
949 1.11 christos NULL, bind_now, dodebug) < 0)
950 1.11 christos ok = 0;
951 1.11 christos }
952 1.11 christos }
953 1.11 christos if (!ok)
954 1.11 christos return -1;
955 1.11 christos
956 1.11 christos
957 1.11 christos /* Set some sanity-checking numbers in the Obj_Entry. */
958 1.11 christos obj->magic = RTLD_MAGIC;
959 1.11 christos obj->version = RTLD_VERSION;
960 1.11 christos
961 1.11 christos /* Fill in the dynamic linker entry points. */
962 1.11 christos obj->dlopen = _rtld_dlopen;
963 1.11 christos obj->dlsym = _rtld_dlsym;
964 1.11 christos obj->dlerror = _rtld_dlerror;
965 1.11 christos obj->dlclose = _rtld_dlclose;
966 1.25 scottb obj->dladdr = _rtld_dladdr;
967 1.1 cgd
968 1.11 christos /* Set the special PLTGOT entries. */
969 1.11 christos if (obj->pltgot != NULL) {
970 1.38 matt #if defined(__arm__) || defined(__i386__) || defined(__m68k__) || \
971 1.51 thorpej defined(__sh__) || defined(__x86_64__)
972 1.11 christos obj->pltgot[1] = (Elf_Addr) obj;
973 1.11 christos obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
974 1.1 cgd #endif
975 1.1 cgd #if defined(__alpha__)
976 1.45 thorpej _rtld_setup_alpha_pltgot(obj, dodebug);
977 1.52 fredette #endif
978 1.52 fredette #if defined(__hppa__)
979 1.52 fredette _rtld_setup_hppa_pltgot(obj);
980 1.2 mhitch #endif
981 1.2 mhitch #if defined(__mips__)
982 1.11 christos _rtld_relocate_mips_got(obj);
983 1.2 mhitch
984 1.11 christos obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
985 1.11 christos /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
986 1.11 christos obj->pltgot[1] |= (Elf_Addr) obj;
987 1.3 tsubai #endif
988 1.3 tsubai #if defined(__powerpc__)
989 1.11 christos _rtld_setup_powerpc_plt(obj);
990 1.9 pk #endif
991 1.9 pk #if defined(__sparc__)
992 1.31 mycroft #if defined(__arch64__) || defined(__sparc_v9__)
993 1.27 eeh /*
994 1.27 eeh * On sparc64 we got troubles.
995 1.27 eeh *
996 1.27 eeh * Instructions are 4 bytes long.
997 1.27 eeh * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
998 1.27 eeh * array entries.
999 1.27 eeh * Each PLT entry jumps to PLT0 to enter the dynamic
1000 1.27 eeh * linker.
1001 1.27 eeh * Loading an arbitrary 64-bit pointer takes 6
1002 1.27 eeh * instructions and 2 registers.
1003 1.27 eeh *
1004 1.27 eeh * Somehow we need to issue a save to get a new stack
1005 1.27 eeh * frame, load the address of the dynamic linker, and
1006 1.27 eeh * jump there, in 8 instructions or less.
1007 1.27 eeh *
1008 1.27 eeh * Oh, we need to fill out both PLT0 and PLT1.
1009 1.27 eeh */
1010 1.27 eeh {
1011 1.31 mycroft Elf_Word *entry = (Elf_Word *)obj->pltgot;
1012 1.31 mycroft extern void _rtld_install_plt __P((Elf_Word *,
1013 1.27 eeh Elf_Addr));
1014 1.27 eeh extern void _rtld_bind_start_0 __P((long,
1015 1.27 eeh long));
1016 1.27 eeh extern void _rtld_bind_start_1 __P((long,
1017 1.27 eeh long));
1018 1.27 eeh
1019 1.27 eeh /* Install in entries 0 and 1 */
1020 1.27 eeh _rtld_install_plt(&entry[0],
1021 1.27 eeh (Elf_Addr)&_rtld_bind_start_0);
1022 1.27 eeh _rtld_install_plt(&entry[8],
1023 1.27 eeh (Elf_Addr)&_rtld_bind_start_1);
1024 1.27 eeh
1025 1.27 eeh /*
1026 1.27 eeh * Install the object reference in first slot
1027 1.27 eeh * of entry 2.
1028 1.27 eeh */
1029 1.30 eeh obj->pltgot[8] = (Elf_Addr) obj;
1030 1.27 eeh }
1031 1.27 eeh #else
1032 1.11 christos /*
1033 1.11 christos * PLTGOT is the PLT on the sparc.
1034 1.11 christos * The first entry holds the call the dynamic linker.
1035 1.13 pk * We construct a `call' sequence that transfers
1036 1.11 christos * to `_rtld_bind_start()'.
1037 1.11 christos * The second entry holds the object identification.
1038 1.11 christos * Note: each PLT entry is three words long.
1039 1.11 christos */
1040 1.13 pk #define SAVE 0x9de3bfc0 /* i.e. `save %sp,-64,%sp' */
1041 1.13 pk #define CALL 0x40000000
1042 1.13 pk #define NOP 0x01000000
1043 1.13 pk obj->pltgot[0] = SAVE;
1044 1.13 pk obj->pltgot[1] = CALL |
1045 1.11 christos ((Elf_Addr)&_rtld_bind_start -
1046 1.13 pk (Elf_Addr)&obj->pltgot[1]) >> 2;
1047 1.13 pk obj->pltgot[2] = NOP;
1048 1.13 pk
1049 1.11 christos obj->pltgot[3] = (Elf_Addr) obj;
1050 1.28 matt #endif /* __arch64__ */
1051 1.28 matt #endif /* __sparc__ */
1052 1.21 matt #if defined(__vax__)
1053 1.32 matt obj->pltgot[1] = (Elf_Addr) obj;
1054 1.28 matt obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
1055 1.1 cgd #endif
1056 1.11 christos }
1057 1.1 cgd }
1058 1.1 cgd
1059 1.11 christos return 0;
1060 1.1 cgd }
1061