reloc.c revision 1.67 1 1.67 mycroft /* $NetBSD: reloc.c,v 1.67 2002/09/13 03:09:38 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.66 mycroft static int _rtld_do_copy_relocation __P((const Obj_Entry *, const Elf_Rela *));
57 1.16 christos
58 1.4 christos /*
59 1.4 christos * XXX: These don't work for the alpha and i386; don't know about powerpc
60 1.4 christos * The alpha and the i386 avoid the problem by compiling everything PIC.
61 1.4 christos * These relocation are supposed to be writing the address of the
62 1.4 christos * function to be called on the bss.rel or bss.rela segment, but:
63 1.4 christos * - st_size == 0
64 1.4 christos * - on the i386 at least the call instruction is a direct call
65 1.4 christos * not an indirect call.
66 1.4 christos */
67 1.1 cgd static int
68 1.66 mycroft _rtld_do_copy_relocation(dstobj, rela)
69 1.16 christos const Obj_Entry *dstobj;
70 1.35 kleink const Elf_Rela *rela;
71 1.1 cgd {
72 1.11 christos void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
73 1.11 christos const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
74 1.11 christos const char *name = dstobj->strtab + dstsym->st_name;
75 1.11 christos unsigned long hash = _rtld_elf_hash(name);
76 1.11 christos size_t size = dstsym->st_size;
77 1.11 christos const void *srcaddr;
78 1.37 matt const Elf_Sym *srcsym = NULL;
79 1.11 christos Obj_Entry *srcobj;
80 1.11 christos
81 1.11 christos for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
82 1.11 christos if ((srcsym = _rtld_symlook_obj(name, hash, srcobj,
83 1.11 christos false)) != NULL)
84 1.11 christos break;
85 1.11 christos
86 1.11 christos if (srcobj == NULL) {
87 1.11 christos _rtld_error("Undefined symbol \"%s\" referenced from COPY"
88 1.11 christos " relocation in %s", name, dstobj->path);
89 1.14 pk return (-1);
90 1.11 christos }
91 1.11 christos srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
92 1.11 christos (void)memcpy(dstaddr, srcaddr, size);
93 1.66 mycroft rdbg(("COPY %s %s %s --> src=%p dst=%p *dst= %p size %ld",
94 1.11 christos dstobj->path, srcobj->path, name, (void *)srcaddr,
95 1.18 christos (void *)dstaddr, (void *)*(long *)dstaddr, (long)size));
96 1.14 pk return (0);
97 1.1 cgd }
98 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
99 1.11 christos
100 1.11 christos
101 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.66 mycroft _rtld_do_copy_relocations(dstobj)
110 1.16 christos const Obj_Entry *dstobj;
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.64 mycroft assert(!dstobj->isdynamic);
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.35 kleink 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.66 mycroft &ourrela) < 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.35 kleink 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.66 mycroft if (_rtld_do_copy_relocation(dstobj, rela) < 0)
136 1.14 pk return (-1);
137 1.11 christos }
138 1.11 christos }
139 1.1 cgd }
140 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
141 1.1 cgd
142 1.14 pk return (0);
143 1.1 cgd }
144 1.9 pk
145 1.67 mycroft #if !defined(__mips__)
146 1.1 cgd caddr_t
147 1.16 christos _rtld_bind(obj, reloff)
148 1.63 mycroft const Obj_Entry *obj;
149 1.16 christos Elf_Word reloff;
150 1.1 cgd {
151 1.35 kleink const Elf_Rela *rela;
152 1.35 kleink Elf_Rela ourrela;
153 1.11 christos caddr_t addr;
154 1.1 cgd
155 1.11 christos if (obj->pltrel != NULL) {
156 1.11 christos const Elf_Rel *rel;
157 1.1 cgd
158 1.11 christos rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
159 1.11 christos ourrela.r_info = rel->r_info;
160 1.11 christos ourrela.r_offset = rel->r_offset;
161 1.28 matt ourrela.r_addend = 0;
162 1.11 christos rela = &ourrela;
163 1.11 christos } else {
164 1.35 kleink rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
165 1.41 eeh #ifdef __sparc64__
166 1.41 eeh if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
167 1.41 eeh /*
168 1.41 eeh * XXXX
169 1.41 eeh *
170 1.50 eeh * The first four PLT entries are reserved. There
171 1.41 eeh * is some disagreement whether they should have
172 1.41 eeh * associated relocation entries. Both the SPARC
173 1.41 eeh * 32-bit and 64-bit ELF specifications say that
174 1.41 eeh * they should have relocation entries, but the
175 1.41 eeh * 32-bit SPARC binutils do not generate them,
176 1.41 eeh * and now the 64-bit SPARC binutils have stopped
177 1.41 eeh * generating them too.
178 1.41 eeh *
179 1.41 eeh * So, to provide binary compatibility, we will
180 1.41 eeh * check the first entry, if it is reserved it
181 1.41 eeh * should not be of the type JMP_SLOT. If it
182 1.41 eeh * is JMP_SLOT, then the 4 reserved entries were
183 1.41 eeh * not generated and our index is 4 entries too far.
184 1.41 eeh */
185 1.41 eeh rela -= 4;
186 1.41 eeh }
187 1.41 eeh #endif
188 1.11 christos }
189 1.11 christos
190 1.66 mycroft if (_rtld_relocate_plt_object(obj, rela, &addr) < 0)
191 1.11 christos _rtld_die();
192 1.1 cgd
193 1.11 christos return addr;
194 1.1 cgd }
195 1.67 mycroft #endif
196 1.9 pk
197 1.1 cgd /*
198 1.1 cgd * Relocate newly-loaded shared objects. The argument is a pointer to
199 1.1 cgd * the Obj_Entry for the first such object. All objects from the first
200 1.1 cgd * to the end of the list of objects are relocated. Returns 0 on success,
201 1.1 cgd * or -1 on failure.
202 1.1 cgd */
203 1.1 cgd int
204 1.66 mycroft _rtld_relocate_objects(first, bind_now, self)
205 1.16 christos Obj_Entry *first;
206 1.16 christos bool bind_now;
207 1.65 mycroft bool self;
208 1.1 cgd {
209 1.16 christos Obj_Entry *obj;
210 1.16 christos int ok = 1;
211 1.1 cgd
212 1.11 christos for (obj = first; obj != NULL; obj = obj->next) {
213 1.16 christos if (obj->nbuckets == 0 || obj->nchains == 0 ||
214 1.16 christos obj->buckets == NULL || obj->symtab == NULL ||
215 1.16 christos obj->strtab == NULL) {
216 1.11 christos _rtld_error("%s: Shared object has no run-time"
217 1.11 christos " symbol table", obj->path);
218 1.11 christos return -1;
219 1.11 christos }
220 1.66 mycroft rdbg((" relocating %s (%ld/%ld rel/rela, %ld/%ld plt rel/rela)",
221 1.11 christos obj->path,
222 1.11 christos (long)(obj->rellim - obj->rel),
223 1.11 christos (long)(obj->relalim - obj->rela),
224 1.11 christos (long)(obj->pltrellim - obj->pltrel),
225 1.16 christos (long)(obj->pltrelalim - obj->pltrela)));
226 1.11 christos
227 1.11 christos if (obj->textrel) {
228 1.11 christos /*
229 1.11 christos * There are relocations to the write-protected text
230 1.11 christos * segment.
231 1.11 christos */
232 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
233 1.11 christos PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
234 1.11 christos _rtld_error("%s: Cannot write-enable text "
235 1.11 christos "segment: %s", obj->path, xstrerror(errno));
236 1.11 christos return -1;
237 1.11 christos }
238 1.11 christos }
239 1.66 mycroft if (_rtld_relocate_nonplt_objects(obj, self) < 0)
240 1.60 mycroft ok = 0;
241 1.11 christos if (obj->textrel) { /* Re-protected the text segment. */
242 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
243 1.11 christos PROT_READ | PROT_EXEC) == -1) {
244 1.11 christos _rtld_error("%s: Cannot write-protect text "
245 1.11 christos "segment: %s", obj->path, xstrerror(errno));
246 1.11 christos return -1;
247 1.11 christos }
248 1.11 christos }
249 1.66 mycroft if (_rtld_relocate_plt_lazy(obj) < 0)
250 1.62 mycroft ok = 0;
251 1.62 mycroft #if 0
252 1.62 mycroft if (bind_now)
253 1.66 mycroft if (_rtld_relocate_plt_object(obj) < 0)
254 1.62 mycroft ok = 0;
255 1.50 eeh #endif
256 1.11 christos if (!ok)
257 1.11 christos return -1;
258 1.11 christos
259 1.11 christos
260 1.11 christos /* Set some sanity-checking numbers in the Obj_Entry. */
261 1.11 christos obj->magic = RTLD_MAGIC;
262 1.11 christos obj->version = RTLD_VERSION;
263 1.11 christos
264 1.11 christos /* Fill in the dynamic linker entry points. */
265 1.11 christos obj->dlopen = _rtld_dlopen;
266 1.11 christos obj->dlsym = _rtld_dlsym;
267 1.11 christos obj->dlerror = _rtld_dlerror;
268 1.11 christos obj->dlclose = _rtld_dlclose;
269 1.25 scottb obj->dladdr = _rtld_dladdr;
270 1.1 cgd
271 1.11 christos /* Set the special PLTGOT entries. */
272 1.53 mycroft if (obj->pltgot != NULL)
273 1.53 mycroft _rtld_setup_pltgot(obj);
274 1.1 cgd }
275 1.1 cgd
276 1.11 christos return 0;
277 1.1 cgd }
278