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