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