reloc.c revision 1.14 1 1.14 pk /* $NetBSD: reloc.c,v 1.14 1999/02/27 10:44:26 pk 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.4 christos /*
57 1.4 christos * XXX: These don't work for the alpha and i386; don't know about powerpc
58 1.4 christos * The alpha and the i386 avoid the problem by compiling everything PIC.
59 1.4 christos * These relocation are supposed to be writing the address of the
60 1.4 christos * function to be called on the bss.rel or bss.rela segment, but:
61 1.4 christos * - st_size == 0
62 1.4 christos * - on the i386 at least the call instruction is a direct call
63 1.4 christos * not an indirect call.
64 1.4 christos */
65 1.1 cgd static int
66 1.1 cgd _rtld_do_copy_relocation(
67 1.11 christos const Obj_Entry *dstobj,
68 1.11 christos const Elf_RelA *rela,
69 1.11 christos bool dodebug)
70 1.1 cgd {
71 1.11 christos void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
72 1.11 christos const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
73 1.11 christos const char *name = dstobj->strtab + dstsym->st_name;
74 1.11 christos unsigned long hash = _rtld_elf_hash(name);
75 1.11 christos size_t size = dstsym->st_size;
76 1.11 christos const void *srcaddr;
77 1.11 christos const Elf_Sym *srcsym;
78 1.11 christos Obj_Entry *srcobj;
79 1.11 christos
80 1.11 christos for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
81 1.11 christos if ((srcsym = _rtld_symlook_obj(name, hash, srcobj,
82 1.11 christos false)) != NULL)
83 1.11 christos break;
84 1.11 christos
85 1.11 christos if (srcobj == NULL) {
86 1.11 christos _rtld_error("Undefined symbol \"%s\" referenced from COPY"
87 1.11 christos " relocation in %s", name, dstobj->path);
88 1.14 pk return (-1);
89 1.11 christos }
90 1.11 christos srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
91 1.11 christos (void)memcpy(dstaddr, srcaddr, size);
92 1.11 christos rdbg(dodebug, "COPY %s %s %s --> src=%p dst=%p *dst= %p size %d",
93 1.11 christos dstobj->path, srcobj->path, name, (void *)srcaddr,
94 1.11 christos (void *)dstaddr, (void *)*(long *)dstaddr, size);
95 1.14 pk return (0);
96 1.1 cgd }
97 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
98 1.11 christos
99 1.11 christos
100 1.1 cgd /*
101 1.1 cgd * Process the special R_xxx_COPY relocations in the main program. These
102 1.1 cgd * copy data from a shared object into a region in the main program's BSS
103 1.1 cgd * segment.
104 1.1 cgd *
105 1.1 cgd * Returns 0 on success, -1 on failure.
106 1.1 cgd */
107 1.1 cgd int
108 1.1 cgd _rtld_do_copy_relocations(
109 1.11 christos const Obj_Entry *dstobj,
110 1.11 christos bool dodebug)
111 1.1 cgd {
112 1.14 pk #ifndef RTLD_INHIBIT_COPY_RELOCS
113 1.14 pk
114 1.14 pk /* COPY relocations are invalid elsewhere */
115 1.14 pk assert(dstobj->mainprog);
116 1.1 cgd
117 1.11 christos if (dstobj->rel != NULL) {
118 1.11 christos const Elf_Rel *rel;
119 1.11 christos for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
120 1.11 christos if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
121 1.11 christos Elf_RelA ourrela;
122 1.11 christos ourrela.r_info = rel->r_info;
123 1.11 christos ourrela.r_offset = rel->r_offset;
124 1.11 christos ourrela.r_addend = 0;
125 1.11 christos if (_rtld_do_copy_relocation(dstobj,
126 1.11 christos &ourrela, dodebug) < 0)
127 1.14 pk return (-1);
128 1.11 christos }
129 1.11 christos }
130 1.11 christos }
131 1.11 christos if (dstobj->rela != NULL) {
132 1.11 christos const Elf_RelA *rela;
133 1.11 christos for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
134 1.11 christos if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
135 1.11 christos if (_rtld_do_copy_relocation(dstobj, rela,
136 1.11 christos dodebug) < 0)
137 1.14 pk return (-1);
138 1.11 christos }
139 1.11 christos }
140 1.1 cgd }
141 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
142 1.1 cgd
143 1.14 pk return (0);
144 1.1 cgd }
145 1.9 pk
146 1.9 pk
147 1.11 christos #ifndef __sparc__
148 1.11 christos int
149 1.9 pk _rtld_relocate_nonplt_object(
150 1.11 christos const Obj_Entry * obj,
151 1.11 christos const Elf_RelA * rela,
152 1.11 christos bool dodebug)
153 1.9 pk {
154 1.11 christos Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
155 1.11 christos const Elf_Sym *def;
156 1.9 pk const Obj_Entry *defobj;
157 1.11 christos extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
158 1.11 christos extern Elf_Dyn _DYNAMIC;
159 1.11 christos Elf_Addr tmp;
160 1.9 pk
161 1.11 christos switch (ELF_R_TYPE(rela->r_info)) {
162 1.9 pk
163 1.11 christos case R_TYPE(NONE):
164 1.11 christos break;
165 1.9 pk
166 1.11 christos #ifdef __i386__
167 1.11 christos case R_TYPE(GOT32):
168 1.9 pk
169 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
170 1.11 christos &defobj, false);
171 1.11 christos if (def == NULL)
172 1.11 christos return -1;
173 1.9 pk
174 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
175 1.11 christos if (*where != tmp)
176 1.11 christos *where = tmp;
177 1.11 christos rdbg(dodebug, "GOT32 %s in %s --> %p in %s",
178 1.11 christos defobj->strtab + def->st_name, obj->path,
179 1.11 christos (void *)*where, defobj->path);
180 1.11 christos break;
181 1.9 pk
182 1.11 christos case R_TYPE(PC32):
183 1.11 christos /*
184 1.11 christos * I don't think the dynamic linker should ever see this
185 1.11 christos * type of relocation. But the binutils-2.6 tools sometimes
186 1.11 christos * generate it.
187 1.11 christos */
188 1.9 pk
189 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
190 1.11 christos &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.11 christos rdbg(dodebug, "PC32 %s in %s --> %p in %s",
197 1.11 christos defobj->strtab + def->st_name, obj->path,
198 1.11 christos (void *)*where, defobj->path);
199 1.11 christos break;
200 1.11 christos
201 1.11 christos case R_TYPE(32):
202 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
203 1.11 christos &defobj, false);
204 1.11 christos if (def == NULL)
205 1.11 christos return -1;
206 1.9 pk
207 1.11 christos *where += (Elf_Addr)(defobj->relocbase + def->st_value);
208 1.11 christos rdbg(dodebug, "32 %s in %s --> %p in %s",
209 1.11 christos defobj->strtab + def->st_name, obj->path,
210 1.11 christos (void *)*where, defobj->path);
211 1.11 christos break;
212 1.4 christos #endif /* __i386__ */
213 1.4 christos
214 1.1 cgd #ifdef __alpha__
215 1.11 christos case R_TYPE(REFQUAD):
216 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
217 1.11 christos &defobj, false);
218 1.11 christos if (def == NULL)
219 1.11 christos return -1;
220 1.1 cgd
221 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
222 1.11 christos *where + rela->r_addend;
223 1.12 tv if (*where != tmp)
224 1.12 tv *where = tmp;
225 1.11 christos rdbg(dodebug, "REFQUAD %s in %s --> %p in %s",
226 1.11 christos defobj->strtab + def->st_name, obj->path,
227 1.11 christos (void *)*where, defobj->path);
228 1.11 christos break;
229 1.4 christos #endif /* __alpha__ */
230 1.1 cgd
231 1.4 christos #if defined(__i386__) || defined(__alpha__)
232 1.11 christos case R_TYPE(GLOB_DAT):
233 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
234 1.11 christos &defobj, false);
235 1.11 christos if (def == NULL)
236 1.11 christos return -1;
237 1.1 cgd
238 1.11 christos if (*where != (Elf_Addr)(defobj->relocbase + def->st_value))
239 1.11 christos *where = (Elf_Addr)(defobj->relocbase + def->st_value);
240 1.11 christos rdbg(dodebug, "GLOB_DAT %s in %s --> %p in %s",
241 1.11 christos defobj->strtab + def->st_name, obj->path,
242 1.11 christos (void *)*where, defobj->path);
243 1.11 christos break;
244 1.11 christos
245 1.11 christos case R_TYPE(RELATIVE):
246 1.12 tv if ((caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
247 1.11 christos (caddr_t)where >= (caddr_t)&_DYNAMIC) {
248 1.11 christos *where += (Elf_Addr)obj->relocbase;
249 1.11 christos rdbg(dodebug, "RELATIVE in %s --> %p", obj->path,
250 1.11 christos (void *)*where);
251 1.11 christos }
252 1.11 christos else
253 1.11 christos rdbg(dodebug, "RELATIVE in %s stays at %p",
254 1.11 christos obj->path, (void *)*where);
255 1.11 christos break;
256 1.1 cgd
257 1.11 christos case R_TYPE(COPY):
258 1.11 christos /*
259 1.11 christos * These are deferred until all other relocations have
260 1.11 christos * been done. All we do here is make sure that the COPY
261 1.11 christos * relocation is not in a shared library. They are allowed
262 1.11 christos * only in executable files.
263 1.11 christos */
264 1.11 christos if (!obj->mainprog) {
265 1.11 christos _rtld_error(
266 1.11 christos "%s: Unexpected R_COPY relocation in shared library",
267 1.11 christos obj->path);
268 1.11 christos return -1;
269 1.11 christos }
270 1.11 christos rdbg(dodebug, "COPY (avoid in main)");
271 1.11 christos break;
272 1.4 christos #endif /* __i386__ || __alpha__ */
273 1.2 mhitch
274 1.2 mhitch #ifdef __mips__
275 1.11 christos case R_TYPE(REL32):
276 1.11 christos /* 32-bit PC-relative reference */
277 1.11 christos def = obj->symtab + ELF_R_SYM(rela->r_info);
278 1.11 christos
279 1.11 christos if (ELF_SYM_BIND(def->st_info) == Elf_estb_local &&
280 1.11 christos (ELF_SYM_TYPE(def->st_info) == Elf_estt_section ||
281 1.11 christos ELF_SYM_TYPE(def->st_info) == Elf_estt_notype)) {
282 1.11 christos *where += (Elf_Addr)obj->relocbase;
283 1.11 christos rdbg(dodebug, "REL32 in %s --> %p", obj->path,
284 1.11 christos (void *)*where);
285 1.11 christos } else {
286 1.11 christos /* XXX maybe do something re: bootstrapping? */
287 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
288 1.11 christos NULL, obj, &defobj, false);
289 1.11 christos if (def == NULL)
290 1.11 christos return -1;
291 1.11 christos *where += (Elf_Addr)(defobj->relocbase + def->st_value);
292 1.11 christos rdbg(dodebug, "REL32 %s in %s --> %p in %s",
293 1.11 christos defobj->strtab + def->st_name, obj->path,
294 1.11 christos (void *)*where, defobj->path);
295 1.11 christos }
296 1.11 christos break;
297 1.2 mhitch
298 1.11 christos #endif /* __mips__ */
299 1.1 cgd
300 1.3 tsubai #ifdef __powerpc__
301 1.11 christos case R_TYPE(32): /* word32 S + A */
302 1.11 christos case R_TYPE(GLOB_DAT): /* word32 S + A */
303 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
304 1.11 christos &defobj, false);
305 1.11 christos if (def == NULL)
306 1.11 christos return -1;
307 1.3 tsubai
308 1.11 christos tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
309 1.11 christos rela->r_addend);
310 1.3 tsubai
311 1.11 christos if (*where != tmp)
312 1.11 christos *where = tmp;
313 1.11 christos rdbg(dodebug, "32/GLOB_DAT %s in %s --> %p in %s",
314 1.11 christos defobj->strtab + def->st_name, obj->path,
315 1.11 christos (void *)*where, defobj->path);
316 1.11 christos break;
317 1.11 christos
318 1.11 christos case R_TYPE(COPY):
319 1.11 christos rdbg(dodebug, "COPY");
320 1.11 christos break;
321 1.11 christos
322 1.11 christos case R_TYPE(JMP_SLOT):
323 1.11 christos rdbg(dodebug, "JMP_SLOT");
324 1.11 christos break;
325 1.11 christos
326 1.11 christos case R_TYPE(RELATIVE): /* word32 B + A */
327 1.11 christos tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
328 1.11 christos if (obj == &_rtld_objself && *where == tmp;
329 1.11 christos break; /* GOT - already done */
330 1.11 christos
331 1.11 christos *where = tmp;
332 1.11 christos rdbg(dodebug, "RELATIVE in %s --> %p", obj->path,
333 1.11 christos (void *)*where);
334 1.11 christos break;
335 1.4 christos #endif /* __powerpc__ */
336 1.3 tsubai
337 1.11 christos default:
338 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
339 1.11 christos &defobj, true);
340 1.11 christos rdbg(dodebug, "sym = %lu, type = %lu, offset = %p, "
341 1.11 christos "addend = %p, contents = %p, symbol = %s",
342 1.11 christos (u_long)ELF_R_SYM(rela->r_info),
343 1.11 christos (u_long)ELF_R_TYPE(rela->r_info),
344 1.11 christos (void *)rela->r_offset, (void *)rela->r_addend,
345 1.11 christos (void *)*where,
346 1.11 christos def ? defobj->strtab + def->st_name : "??");
347 1.11 christos _rtld_error("%s: Unsupported relocation type %d"
348 1.11 christos "in non-PLT relocations\n",
349 1.11 christos obj->path, ELF_R_TYPE(rela->r_info));
350 1.11 christos return -1;
351 1.11 christos }
352 1.11 christos return 0;
353 1.1 cgd }
354 1.9 pk
355 1.9 pk
356 1.9 pk
357 1.11 christos int
358 1.1 cgd _rtld_relocate_plt_object(
359 1.11 christos const Obj_Entry * obj,
360 1.11 christos const Elf_RelA * rela,
361 1.11 christos caddr_t *addrp,
362 1.11 christos bool bind_now,
363 1.11 christos bool dodebug)
364 1.1 cgd {
365 1.11 christos Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
366 1.11 christos Elf_Addr new_value;
367 1.1 cgd
368 1.11 christos /* Fully resolve procedure addresses now */
369 1.2 mhitch
370 1.3 tsubai #if defined(__powerpc__)
371 1.11 christos return _rtld_reloc_powerpc_plt(obj, rela, bind_now);
372 1.3 tsubai #endif
373 1.3 tsubai
374 1.11 christos #if defined(__alpha__) || defined(__i386__)
375 1.11 christos if (bind_now || obj->pltgot == NULL) {
376 1.11 christos const Elf_Sym *def;
377 1.11 christos const Obj_Entry *defobj;
378 1.11 christos
379 1.11 christos assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
380 1.1 cgd
381 1.11 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
382 1.11 christos &defobj, true);
383 1.11 christos if (def == NULL)
384 1.11 christos return -1;
385 1.1 cgd
386 1.11 christos new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
387 1.11 christos rdbg(dodebug, "bind now %d/fixup in %s --> old=%p new=%p",
388 1.11 christos (int)bind_now,
389 1.11 christos defobj->strtab + def->st_name,
390 1.11 christos (void *)*where, (void *)new_value);
391 1.11 christos } else
392 1.11 christos #endif /* __alpha__ || __i386__ */
393 1.11 christos if (!obj->mainprog) {
394 1.11 christos /* Just relocate the GOT slots pointing into the PLT */
395 1.11 christos new_value = *where + (Elf_Addr)(obj->relocbase);
396 1.11 christos rdbg(dodebug, "fixup !main in %s --> %p", obj->path,
397 1.11 christos (void *)*where);
398 1.11 christos } else {
399 1.11 christos return 0;
400 1.11 christos }
401 1.11 christos /*
402 1.11 christos * Since this page is probably copy-on-write, let's not write
403 1.11 christos * it unless we really really have to.
404 1.11 christos */
405 1.11 christos if (*where != new_value)
406 1.11 christos *where = new_value;
407 1.11 christos if (addrp != NULL)
408 1.11 christos *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
409 1.7 tv return 0;
410 1.1 cgd }
411 1.11 christos #endif /* __sparc__ */
412 1.9 pk
413 1.1 cgd caddr_t
414 1.1 cgd _rtld_bind(
415 1.11 christos const Obj_Entry *obj,
416 1.11 christos Elf_Word reloff)
417 1.1 cgd {
418 1.11 christos const Elf_RelA *rela;
419 1.11 christos Elf_RelA ourrela;
420 1.11 christos caddr_t addr;
421 1.1 cgd
422 1.11 christos if (obj->pltrel != NULL) {
423 1.11 christos const Elf_Rel *rel;
424 1.1 cgd
425 1.11 christos rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
426 1.11 christos ourrela.r_info = rel->r_info;
427 1.11 christos ourrela.r_offset = rel->r_offset;
428 1.11 christos rela = &ourrela;
429 1.11 christos } else {
430 1.11 christos rela = (const Elf_RelA *)((caddr_t) obj->pltrela + reloff);
431 1.11 christos }
432 1.11 christos
433 1.11 christos if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
434 1.11 christos _rtld_die();
435 1.1 cgd
436 1.11 christos return addr;
437 1.1 cgd }
438 1.9 pk
439 1.1 cgd /*
440 1.1 cgd * Relocate newly-loaded shared objects. The argument is a pointer to
441 1.1 cgd * the Obj_Entry for the first such object. All objects from the first
442 1.1 cgd * to the end of the list of objects are relocated. Returns 0 on success,
443 1.1 cgd * or -1 on failure.
444 1.1 cgd */
445 1.1 cgd int
446 1.1 cgd _rtld_relocate_objects(
447 1.11 christos Obj_Entry * first,
448 1.11 christos bool bind_now,
449 1.11 christos bool dodebug)
450 1.1 cgd {
451 1.11 christos Obj_Entry *obj;
452 1.11 christos int ok = 1;
453 1.1 cgd
454 1.11 christos for (obj = first; obj != NULL; obj = obj->next) {
455 1.11 christos if (obj->nbuckets == 0 || obj->nchains == 0
456 1.11 christos || obj->buckets == NULL || obj->symtab == NULL
457 1.11 christos || obj->strtab == NULL) {
458 1.11 christos _rtld_error("%s: Shared object has no run-time"
459 1.11 christos " symbol table", obj->path);
460 1.11 christos return -1;
461 1.11 christos }
462 1.11 christos rdbg(dodebug, " relocating %s (%ld/%ld rel/rela, "
463 1.11 christos "%ld/%ld plt rel/rela)",
464 1.11 christos obj->path,
465 1.11 christos (long)(obj->rellim - obj->rel),
466 1.11 christos (long)(obj->relalim - obj->rela),
467 1.11 christos (long)(obj->pltrellim - obj->pltrel),
468 1.11 christos (long)(obj->pltrelalim - obj->pltrela));
469 1.11 christos
470 1.11 christos if (obj->textrel) {
471 1.11 christos /*
472 1.11 christos * There are relocations to the write-protected text
473 1.11 christos * segment.
474 1.11 christos */
475 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
476 1.11 christos PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
477 1.11 christos _rtld_error("%s: Cannot write-enable text "
478 1.11 christos "segment: %s", obj->path, xstrerror(errno));
479 1.11 christos return -1;
480 1.11 christos }
481 1.11 christos }
482 1.11 christos if (obj->rel != NULL) {
483 1.11 christos /* Process the non-PLT relocations. */
484 1.11 christos const Elf_Rel *rel;
485 1.11 christos for (rel = obj->rel; rel < obj->rellim; ++rel) {
486 1.11 christos Elf_RelA ourrela;
487 1.11 christos ourrela.r_info = rel->r_info;
488 1.11 christos ourrela.r_offset = rel->r_offset;
489 1.2 mhitch #if defined(__mips__)
490 1.11 christos /* rel->r_offset is not valid on mips? */
491 1.11 christos if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
492 1.11 christos ourrela.r_addend = 0;
493 1.11 christos else
494 1.11 christos #endif
495 1.11 christos ourrela.r_addend =
496 1.11 christos *(Elf_Word *)(obj->relocbase +
497 1.11 christos rel->r_offset);
498 1.11 christos
499 1.11 christos if (_rtld_relocate_nonplt_object(obj, &ourrela,
500 1.11 christos dodebug) < 0)
501 1.11 christos ok = 0;
502 1.11 christos }
503 1.11 christos }
504 1.11 christos if (obj->rela != NULL) {
505 1.11 christos /* Process the non-PLT relocations. */
506 1.11 christos const Elf_RelA *rela;
507 1.11 christos for (rela = obj->rela; rela < obj->relalim; ++rela) {
508 1.11 christos if (_rtld_relocate_nonplt_object(obj, rela,
509 1.11 christos dodebug) < 0)
510 1.11 christos ok = 0;
511 1.11 christos }
512 1.11 christos }
513 1.11 christos if (obj->textrel) { /* Re-protected the text segment. */
514 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
515 1.11 christos PROT_READ | PROT_EXEC) == -1) {
516 1.11 christos _rtld_error("%s: Cannot write-protect text "
517 1.11 christos "segment: %s", obj->path, xstrerror(errno));
518 1.11 christos return -1;
519 1.11 christos }
520 1.11 christos }
521 1.11 christos /* Process the PLT relocations. */
522 1.11 christos if (obj->pltrel != NULL) {
523 1.11 christos const Elf_Rel *rel;
524 1.11 christos for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
525 1.11 christos Elf_RelA ourrela;
526 1.11 christos ourrela.r_info = rel->r_info;
527 1.11 christos ourrela.r_offset = rel->r_offset;
528 1.11 christos ourrela.r_addend =
529 1.11 christos *(Elf_Word *)(obj->relocbase +
530 1.11 christos rel->r_offset);
531 1.11 christos if (_rtld_relocate_plt_object(obj, &ourrela,
532 1.11 christos NULL, bind_now, dodebug) < 0)
533 1.11 christos ok = 0;
534 1.11 christos }
535 1.11 christos }
536 1.11 christos if (obj->pltrela != NULL) {
537 1.11 christos const Elf_RelA *rela;
538 1.11 christos for (rela = obj->pltrela; rela < obj->pltrelalim;
539 1.11 christos ++rela) {
540 1.11 christos if (_rtld_relocate_plt_object(obj, rela,
541 1.11 christos NULL, bind_now, dodebug) < 0)
542 1.11 christos ok = 0;
543 1.11 christos }
544 1.11 christos }
545 1.11 christos if (!ok)
546 1.11 christos return -1;
547 1.11 christos
548 1.11 christos
549 1.11 christos /* Set some sanity-checking numbers in the Obj_Entry. */
550 1.11 christos obj->magic = RTLD_MAGIC;
551 1.11 christos obj->version = RTLD_VERSION;
552 1.11 christos
553 1.11 christos /* Fill in the dynamic linker entry points. */
554 1.11 christos obj->dlopen = _rtld_dlopen;
555 1.11 christos obj->dlsym = _rtld_dlsym;
556 1.11 christos obj->dlerror = _rtld_dlerror;
557 1.11 christos obj->dlclose = _rtld_dlclose;
558 1.1 cgd
559 1.11 christos /* Set the special PLTGOT entries. */
560 1.11 christos if (obj->pltgot != NULL) {
561 1.1 cgd #if defined(__i386__)
562 1.11 christos obj->pltgot[1] = (Elf_Addr) obj;
563 1.11 christos obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
564 1.1 cgd #endif
565 1.1 cgd #if defined(__alpha__)
566 1.11 christos /*
567 1.11 christos * This function will be called to perform the
568 1.11 christos * relocation.
569 1.11 christos */
570 1.11 christos obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
571 1.11 christos /* Identify this shared object */
572 1.11 christos obj->pltgot[3] = (Elf_Addr) obj;
573 1.2 mhitch #endif
574 1.2 mhitch #if defined(__mips__)
575 1.11 christos _rtld_relocate_mips_got(obj);
576 1.2 mhitch
577 1.11 christos obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
578 1.11 christos /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
579 1.11 christos obj->pltgot[1] |= (Elf_Addr) obj;
580 1.3 tsubai #endif
581 1.3 tsubai #if defined(__powerpc__)
582 1.11 christos _rtld_setup_powerpc_plt(obj);
583 1.9 pk #endif
584 1.9 pk #if defined(__sparc__)
585 1.11 christos /*
586 1.11 christos * PLTGOT is the PLT on the sparc.
587 1.11 christos * The first entry holds the call the dynamic linker.
588 1.13 pk * We construct a `call' sequence that transfers
589 1.11 christos * to `_rtld_bind_start()'.
590 1.11 christos * The second entry holds the object identification.
591 1.11 christos * Note: each PLT entry is three words long.
592 1.11 christos */
593 1.13 pk #define SAVE 0x9de3bfc0 /* i.e. `save %sp,-64,%sp' */
594 1.13 pk #define CALL 0x40000000
595 1.13 pk #define NOP 0x01000000
596 1.13 pk obj->pltgot[0] = SAVE;
597 1.13 pk obj->pltgot[1] = CALL |
598 1.11 christos ((Elf_Addr)&_rtld_bind_start -
599 1.13 pk (Elf_Addr)&obj->pltgot[1]) >> 2;
600 1.13 pk obj->pltgot[2] = NOP;
601 1.13 pk
602 1.11 christos obj->pltgot[3] = (Elf_Addr) obj;
603 1.1 cgd #endif
604 1.11 christos }
605 1.1 cgd }
606 1.1 cgd
607 1.11 christos return 0;
608 1.1 cgd }
609