reloc.c revision 1.60 1 1.60 mycroft /* $NetBSD: reloc.c,v 1.60 2002/09/05 20:08:14 mycroft Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright 1996 John D. Polstra.
5 1.1 cgd * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 1.1 cgd * All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by John Polstra.
19 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
20 1.1 cgd * derived from this software without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd /*
35 1.1 cgd * Dynamic linker for ELF.
36 1.1 cgd *
37 1.1 cgd * John Polstra <jdp (at) polstra.com>.
38 1.1 cgd */
39 1.1 cgd
40 1.1 cgd #include <err.h>
41 1.1 cgd #include <errno.h>
42 1.1 cgd #include <fcntl.h>
43 1.1 cgd #include <stdarg.h>
44 1.1 cgd #include <stdio.h>
45 1.1 cgd #include <stdlib.h>
46 1.1 cgd #include <string.h>
47 1.1 cgd #include <unistd.h>
48 1.1 cgd #include <sys/types.h>
49 1.1 cgd #include <sys/mman.h>
50 1.1 cgd #include <dirent.h>
51 1.1 cgd
52 1.1 cgd #include "debug.h"
53 1.1 cgd #include "rtld.h"
54 1.1 cgd
55 1.14 pk #ifndef RTLD_INHIBIT_COPY_RELOCS
56 1.35 kleink static int _rtld_do_copy_relocation __P((const Obj_Entry *, const Elf_Rela *,
57 1.16 christos bool));
58 1.16 christos
59 1.4 christos /*
60 1.4 christos * XXX: These don't work for the alpha and i386; don't know about powerpc
61 1.4 christos * The alpha and the i386 avoid the problem by compiling everything PIC.
62 1.4 christos * These relocation are supposed to be writing the address of the
63 1.4 christos * function to be called on the bss.rel or bss.rela segment, but:
64 1.4 christos * - st_size == 0
65 1.4 christos * - on the i386 at least the call instruction is a direct call
66 1.4 christos * not an indirect call.
67 1.4 christos */
68 1.1 cgd static int
69 1.16 christos _rtld_do_copy_relocation(dstobj, rela, dodebug)
70 1.16 christos const Obj_Entry *dstobj;
71 1.35 kleink const Elf_Rela *rela;
72 1.16 christos bool dodebug;
73 1.1 cgd {
74 1.11 christos void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
75 1.11 christos const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
76 1.11 christos const char *name = dstobj->strtab + dstsym->st_name;
77 1.11 christos unsigned long hash = _rtld_elf_hash(name);
78 1.11 christos size_t size = dstsym->st_size;
79 1.11 christos const void *srcaddr;
80 1.37 matt const Elf_Sym *srcsym = NULL;
81 1.11 christos Obj_Entry *srcobj;
82 1.11 christos
83 1.11 christos for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
84 1.11 christos if ((srcsym = _rtld_symlook_obj(name, hash, srcobj,
85 1.11 christos false)) != NULL)
86 1.11 christos break;
87 1.11 christos
88 1.11 christos if (srcobj == NULL) {
89 1.11 christos _rtld_error("Undefined symbol \"%s\" referenced from COPY"
90 1.11 christos " relocation in %s", name, dstobj->path);
91 1.14 pk return (-1);
92 1.11 christos }
93 1.11 christos srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
94 1.11 christos (void)memcpy(dstaddr, srcaddr, size);
95 1.18 christos rdbg(dodebug, ("COPY %s %s %s --> src=%p dst=%p *dst= %p size %ld",
96 1.11 christos dstobj->path, srcobj->path, name, (void *)srcaddr,
97 1.18 christos (void *)dstaddr, (void *)*(long *)dstaddr, (long)size));
98 1.14 pk return (0);
99 1.1 cgd }
100 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
101 1.11 christos
102 1.11 christos
103 1.1 cgd /*
104 1.1 cgd * Process the special R_xxx_COPY relocations in the main program. These
105 1.1 cgd * copy data from a shared object into a region in the main program's BSS
106 1.1 cgd * segment.
107 1.1 cgd *
108 1.1 cgd * Returns 0 on success, -1 on failure.
109 1.1 cgd */
110 1.1 cgd int
111 1.16 christos _rtld_do_copy_relocations(dstobj, dodebug)
112 1.16 christos const Obj_Entry *dstobj;
113 1.16 christos bool dodebug;
114 1.1 cgd {
115 1.14 pk #ifndef RTLD_INHIBIT_COPY_RELOCS
116 1.14 pk
117 1.14 pk /* COPY relocations are invalid elsewhere */
118 1.14 pk assert(dstobj->mainprog);
119 1.1 cgd
120 1.11 christos if (dstobj->rel != NULL) {
121 1.11 christos const Elf_Rel *rel;
122 1.11 christos for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
123 1.11 christos if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
124 1.35 kleink Elf_Rela ourrela;
125 1.11 christos ourrela.r_info = rel->r_info;
126 1.11 christos ourrela.r_offset = rel->r_offset;
127 1.11 christos ourrela.r_addend = 0;
128 1.11 christos if (_rtld_do_copy_relocation(dstobj,
129 1.11 christos &ourrela, dodebug) < 0)
130 1.14 pk return (-1);
131 1.11 christos }
132 1.11 christos }
133 1.11 christos }
134 1.11 christos if (dstobj->rela != NULL) {
135 1.35 kleink const Elf_Rela *rela;
136 1.11 christos for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
137 1.11 christos if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
138 1.11 christos if (_rtld_do_copy_relocation(dstobj, rela,
139 1.11 christos dodebug) < 0)
140 1.14 pk return (-1);
141 1.11 christos }
142 1.11 christos }
143 1.1 cgd }
144 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
145 1.1 cgd
146 1.14 pk return (0);
147 1.1 cgd }
148 1.9 pk
149 1.9 pk
150 1.58 mycroft #if !defined(__sparc__) && !defined(__x86_64__) && !defined(__mips__)
151 1.9 pk
152 1.52 fredette #if !defined(__powerpc__) && !defined(__hppa__)
153 1.9 pk
154 1.11 christos int
155 1.16 christos _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
156 1.23 mycroft Obj_Entry *obj;
157 1.35 kleink const Elf_Rela *rela;
158 1.16 christos caddr_t *addrp;
159 1.16 christos bool bind_now;
160 1.16 christos bool dodebug;
161 1.1 cgd {
162 1.16 christos Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
163 1.16 christos Elf_Addr new_value;
164 1.1 cgd
165 1.11 christos /* Fully resolve procedure addresses now */
166 1.2 mhitch
167 1.11 christos if (bind_now || obj->pltgot == NULL) {
168 1.11 christos const Elf_Sym *def;
169 1.11 christos const Obj_Entry *defobj;
170 1.11 christos
171 1.39 matt #if !defined(__arm__)
172 1.11 christos assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
173 1.39 matt #else
174 1.39 matt assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
175 1.39 matt #endif
176 1.1 cgd
177 1.55 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
178 1.11 christos if (def == NULL)
179 1.11 christos return -1;
180 1.1 cgd
181 1.11 christos new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
182 1.51 thorpej #if defined(__sh__) || defined(__vax__)
183 1.29 matt new_value += rela->r_addend;
184 1.29 matt #endif
185 1.16 christos rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
186 1.11 christos (int)bind_now,
187 1.11 christos defobj->strtab + def->st_name,
188 1.16 christos (void *)*where, (void *)new_value));
189 1.58 mycroft } else if (!obj->mainprog) {
190 1.11 christos /* Just relocate the GOT slots pointing into the PLT */
191 1.11 christos new_value = *where + (Elf_Addr)(obj->relocbase);
192 1.16 christos rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
193 1.16 christos (void *)*where));
194 1.11 christos } else {
195 1.11 christos return 0;
196 1.11 christos }
197 1.11 christos /*
198 1.11 christos * Since this page is probably copy-on-write, let's not write
199 1.11 christos * it unless we really really have to.
200 1.11 christos */
201 1.11 christos if (*where != new_value)
202 1.11 christos *where = new_value;
203 1.29 matt if (addrp != NULL) {
204 1.11 christos *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
205 1.29 matt #if defined(__vax__)
206 1.29 matt *addrp -= rela->r_addend;
207 1.29 matt #endif
208 1.29 matt }
209 1.7 tv return 0;
210 1.1 cgd }
211 1.42 mycroft
212 1.52 fredette #endif /* __powerpc__ || __hppa__ */
213 1.42 mycroft
214 1.58 mycroft #endif /* __sparc__ || __x86_64__ || __mips__ */
215 1.9 pk
216 1.1 cgd caddr_t
217 1.16 christos _rtld_bind(obj, reloff)
218 1.23 mycroft Obj_Entry *obj;
219 1.16 christos Elf_Word reloff;
220 1.1 cgd {
221 1.35 kleink const Elf_Rela *rela;
222 1.35 kleink Elf_Rela ourrela;
223 1.11 christos caddr_t addr;
224 1.1 cgd
225 1.11 christos if (obj->pltrel != NULL) {
226 1.11 christos const Elf_Rel *rel;
227 1.1 cgd
228 1.11 christos rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
229 1.11 christos ourrela.r_info = rel->r_info;
230 1.11 christos ourrela.r_offset = rel->r_offset;
231 1.28 matt ourrela.r_addend = 0;
232 1.11 christos rela = &ourrela;
233 1.11 christos } else {
234 1.35 kleink rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
235 1.41 eeh #ifdef __sparc64__
236 1.41 eeh if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
237 1.41 eeh /*
238 1.41 eeh * XXXX
239 1.41 eeh *
240 1.50 eeh * The first four PLT entries are reserved. There
241 1.41 eeh * is some disagreement whether they should have
242 1.41 eeh * associated relocation entries. Both the SPARC
243 1.41 eeh * 32-bit and 64-bit ELF specifications say that
244 1.41 eeh * they should have relocation entries, but the
245 1.41 eeh * 32-bit SPARC binutils do not generate them,
246 1.41 eeh * and now the 64-bit SPARC binutils have stopped
247 1.41 eeh * generating them too.
248 1.41 eeh *
249 1.41 eeh * So, to provide binary compatibility, we will
250 1.41 eeh * check the first entry, if it is reserved it
251 1.41 eeh * should not be of the type JMP_SLOT. If it
252 1.41 eeh * is JMP_SLOT, then the 4 reserved entries were
253 1.41 eeh * not generated and our index is 4 entries too far.
254 1.41 eeh */
255 1.41 eeh rela -= 4;
256 1.41 eeh }
257 1.41 eeh #endif
258 1.11 christos }
259 1.11 christos
260 1.11 christos if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
261 1.11 christos _rtld_die();
262 1.1 cgd
263 1.11 christos return addr;
264 1.1 cgd }
265 1.9 pk
266 1.1 cgd /*
267 1.1 cgd * Relocate newly-loaded shared objects. The argument is a pointer to
268 1.1 cgd * the Obj_Entry for the first such object. All objects from the first
269 1.1 cgd * to the end of the list of objects are relocated. Returns 0 on success,
270 1.1 cgd * or -1 on failure.
271 1.1 cgd */
272 1.1 cgd int
273 1.16 christos _rtld_relocate_objects(first, bind_now, dodebug)
274 1.16 christos Obj_Entry *first;
275 1.16 christos bool bind_now;
276 1.16 christos bool dodebug;
277 1.1 cgd {
278 1.16 christos Obj_Entry *obj;
279 1.16 christos int ok = 1;
280 1.1 cgd
281 1.11 christos for (obj = first; obj != NULL; obj = obj->next) {
282 1.16 christos if (obj->nbuckets == 0 || obj->nchains == 0 ||
283 1.16 christos obj->buckets == NULL || obj->symtab == NULL ||
284 1.16 christos obj->strtab == NULL) {
285 1.11 christos _rtld_error("%s: Shared object has no run-time"
286 1.11 christos " symbol table", obj->path);
287 1.11 christos return -1;
288 1.11 christos }
289 1.16 christos rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
290 1.11 christos "%ld/%ld plt rel/rela)",
291 1.11 christos obj->path,
292 1.11 christos (long)(obj->rellim - obj->rel),
293 1.11 christos (long)(obj->relalim - obj->rela),
294 1.11 christos (long)(obj->pltrellim - obj->pltrel),
295 1.16 christos (long)(obj->pltrelalim - obj->pltrela)));
296 1.11 christos
297 1.11 christos if (obj->textrel) {
298 1.11 christos /*
299 1.11 christos * There are relocations to the write-protected text
300 1.11 christos * segment.
301 1.11 christos */
302 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
303 1.11 christos PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
304 1.11 christos _rtld_error("%s: Cannot write-enable text "
305 1.11 christos "segment: %s", obj->path, xstrerror(errno));
306 1.11 christos return -1;
307 1.11 christos }
308 1.11 christos }
309 1.60 mycroft if (_rtld_relocate_nonplt_objects(obj, dodebug) < 0)
310 1.60 mycroft ok = 0;
311 1.11 christos if (obj->textrel) { /* Re-protected the text segment. */
312 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
313 1.11 christos PROT_READ | PROT_EXEC) == -1) {
314 1.11 christos _rtld_error("%s: Cannot write-protect text "
315 1.11 christos "segment: %s", obj->path, xstrerror(errno));
316 1.11 christos return -1;
317 1.11 christos }
318 1.11 christos }
319 1.11 christos /* Process the PLT relocations. */
320 1.11 christos if (obj->pltrel != NULL) {
321 1.11 christos const Elf_Rel *rel;
322 1.11 christos for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
323 1.35 kleink Elf_Rela ourrela;
324 1.11 christos ourrela.r_info = rel->r_info;
325 1.11 christos ourrela.r_offset = rel->r_offset;
326 1.11 christos ourrela.r_addend =
327 1.11 christos *(Elf_Word *)(obj->relocbase +
328 1.11 christos rel->r_offset);
329 1.11 christos if (_rtld_relocate_plt_object(obj, &ourrela,
330 1.11 christos NULL, bind_now, dodebug) < 0)
331 1.11 christos ok = 0;
332 1.11 christos }
333 1.11 christos }
334 1.11 christos if (obj->pltrela != NULL) {
335 1.35 kleink const Elf_Rela *rela;
336 1.11 christos for (rela = obj->pltrela; rela < obj->pltrelalim;
337 1.11 christos ++rela) {
338 1.50 eeh #ifdef __sparc64__
339 1.50 eeh if (ELF_R_TYPE(rela->r_info) !=
340 1.50 eeh R_TYPE(JMP_SLOT)) {
341 1.50 eeh /*
342 1.50 eeh * XXXX
343 1.50 eeh *
344 1.50 eeh * The first four PLT entries are
345 1.50 eeh * reserved. There is some
346 1.50 eeh * disagreement whether they should
347 1.50 eeh * have associated relocation
348 1.50 eeh * entries. Both the SPARC 32-bit
349 1.50 eeh * and 64-bit ELF specifications say
350 1.50 eeh * that they should have relocation
351 1.50 eeh * entries, but the 32-bit SPARC
352 1.50 eeh * binutils do not generate them,
353 1.50 eeh * and now the 64-bit SPARC binutils
354 1.50 eeh * have stopped generating them too.
355 1.50 eeh *
356 1.50 eeh * To provide binary compatibility, we
357 1.50 eeh * will skip any entries that are not
358 1.50 eeh * of type JMP_SLOT.
359 1.50 eeh */
360 1.50 eeh continue;
361 1.50 eeh }
362 1.50 eeh #endif
363 1.11 christos if (_rtld_relocate_plt_object(obj, rela,
364 1.11 christos NULL, bind_now, dodebug) < 0)
365 1.11 christos ok = 0;
366 1.11 christos }
367 1.11 christos }
368 1.11 christos if (!ok)
369 1.11 christos return -1;
370 1.11 christos
371 1.11 christos
372 1.11 christos /* Set some sanity-checking numbers in the Obj_Entry. */
373 1.11 christos obj->magic = RTLD_MAGIC;
374 1.11 christos obj->version = RTLD_VERSION;
375 1.11 christos
376 1.11 christos /* Fill in the dynamic linker entry points. */
377 1.11 christos obj->dlopen = _rtld_dlopen;
378 1.11 christos obj->dlsym = _rtld_dlsym;
379 1.11 christos obj->dlerror = _rtld_dlerror;
380 1.11 christos obj->dlclose = _rtld_dlclose;
381 1.25 scottb obj->dladdr = _rtld_dladdr;
382 1.1 cgd
383 1.11 christos /* Set the special PLTGOT entries. */
384 1.53 mycroft if (obj->pltgot != NULL)
385 1.53 mycroft _rtld_setup_pltgot(obj);
386 1.1 cgd }
387 1.1 cgd
388 1.11 christos return 0;
389 1.1 cgd }
390