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