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