reloc.c revision 1.111 1 1.111 joerg /* $NetBSD: reloc.c,v 1.111 2017/08/10 19:03:25 joerg 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.85 skrll #include <sys/cdefs.h>
41 1.85 skrll #ifndef lint
42 1.111 joerg __RCSID("$NetBSD: reloc.c,v 1.111 2017/08/10 19:03:25 joerg Exp $");
43 1.85 skrll #endif /* not lint */
44 1.85 skrll
45 1.1 cgd #include <err.h>
46 1.1 cgd #include <errno.h>
47 1.1 cgd #include <fcntl.h>
48 1.1 cgd #include <stdarg.h>
49 1.1 cgd #include <stdio.h>
50 1.1 cgd #include <stdlib.h>
51 1.1 cgd #include <string.h>
52 1.1 cgd #include <unistd.h>
53 1.1 cgd #include <sys/types.h>
54 1.1 cgd #include <sys/mman.h>
55 1.102 joerg #include <sys/bitops.h>
56 1.1 cgd #include <dirent.h>
57 1.1 cgd
58 1.1 cgd #include "debug.h"
59 1.1 cgd #include "rtld.h"
60 1.1 cgd
61 1.14 pk #ifndef RTLD_INHIBIT_COPY_RELOCS
62 1.80 skrll static int _rtld_do_copy_relocation(const Obj_Entry *, const Elf_Rela *);
63 1.16 christos
64 1.1 cgd static int
65 1.80 skrll _rtld_do_copy_relocation(const Obj_Entry *dstobj, const Elf_Rela *rela)
66 1.1 cgd {
67 1.11 christos void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
68 1.11 christos const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
69 1.11 christos const char *name = dstobj->strtab + dstsym->st_name;
70 1.11 christos unsigned long hash = _rtld_elf_hash(name);
71 1.11 christos size_t size = dstsym->st_size;
72 1.11 christos const void *srcaddr;
73 1.37 matt const Elf_Sym *srcsym = NULL;
74 1.11 christos Obj_Entry *srcobj;
75 1.11 christos
76 1.110 uwe if (__predict_false(size == 0)) {
77 1.110 uwe #if defined(__powerpc__) && !defined(__LP64) /* PR port-macppc/47464 */
78 1.110 uwe if (strcmp(name, "_SDA_BASE_") == 0
79 1.110 uwe || strcmp(name, "_SDA2_BASE_") == 0)
80 1.110 uwe {
81 1.110 uwe rdbg(("COPY %s %s --> ignoring old binutils bug",
82 1.110 uwe dstobj->path, name));
83 1.110 uwe return 0;
84 1.110 uwe }
85 1.110 uwe #endif
86 1.110 uwe #if 0 /* shall we warn? */
87 1.110 uwe xwarnx("%s: zero size COPY relocation for \"%s\"",
88 1.110 uwe dstobj->path, name);
89 1.110 uwe #endif
90 1.110 uwe }
91 1.110 uwe
92 1.104 nonaka for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) {
93 1.104 nonaka srcsym = _rtld_symlook_obj(name, hash, srcobj, 0,
94 1.104 nonaka _rtld_fetch_ventry(dstobj, ELF_R_SYM(rela->r_info)));
95 1.104 nonaka if (srcsym != NULL)
96 1.11 christos break;
97 1.104 nonaka }
98 1.11 christos
99 1.11 christos if (srcobj == NULL) {
100 1.11 christos _rtld_error("Undefined symbol \"%s\" referenced from COPY"
101 1.11 christos " relocation in %s", name, dstobj->path);
102 1.14 pk return (-1);
103 1.11 christos }
104 1.11 christos srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
105 1.11 christos (void)memcpy(dstaddr, srcaddr, size);
106 1.84 petrov rdbg(("COPY %s %s %s --> src=%p dst=%p size %ld",
107 1.98 christos dstobj->path, srcobj->path, name, srcaddr,
108 1.84 petrov (void *)dstaddr, (long)size));
109 1.14 pk return (0);
110 1.1 cgd }
111 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
112 1.11 christos
113 1.11 christos
114 1.1 cgd /*
115 1.1 cgd * Process the special R_xxx_COPY relocations in the main program. These
116 1.1 cgd * copy data from a shared object into a region in the main program's BSS
117 1.1 cgd * segment.
118 1.1 cgd *
119 1.1 cgd * Returns 0 on success, -1 on failure.
120 1.1 cgd */
121 1.1 cgd int
122 1.80 skrll _rtld_do_copy_relocations(const Obj_Entry *dstobj)
123 1.1 cgd {
124 1.14 pk #ifndef RTLD_INHIBIT_COPY_RELOCS
125 1.14 pk
126 1.14 pk /* COPY relocations are invalid elsewhere */
127 1.64 mycroft assert(!dstobj->isdynamic);
128 1.1 cgd
129 1.11 christos if (dstobj->rel != NULL) {
130 1.11 christos const Elf_Rel *rel;
131 1.11 christos for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
132 1.11 christos if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
133 1.35 kleink Elf_Rela ourrela;
134 1.11 christos ourrela.r_info = rel->r_info;
135 1.11 christos ourrela.r_offset = rel->r_offset;
136 1.11 christos ourrela.r_addend = 0;
137 1.11 christos if (_rtld_do_copy_relocation(dstobj,
138 1.66 mycroft &ourrela) < 0)
139 1.14 pk return (-1);
140 1.11 christos }
141 1.11 christos }
142 1.11 christos }
143 1.11 christos if (dstobj->rela != NULL) {
144 1.35 kleink const Elf_Rela *rela;
145 1.11 christos for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
146 1.11 christos if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
147 1.66 mycroft if (_rtld_do_copy_relocation(dstobj, rela) < 0)
148 1.14 pk return (-1);
149 1.11 christos }
150 1.11 christos }
151 1.1 cgd }
152 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
153 1.1 cgd
154 1.14 pk return (0);
155 1.1 cgd }
156 1.9 pk
157 1.1 cgd /*
158 1.1 cgd * Relocate newly-loaded shared objects. The argument is a pointer to
159 1.1 cgd * the Obj_Entry for the first such object. All objects from the first
160 1.1 cgd * to the end of the list of objects are relocated. Returns 0 on success,
161 1.1 cgd * or -1 on failure.
162 1.1 cgd */
163 1.1 cgd int
164 1.80 skrll _rtld_relocate_objects(Obj_Entry *first, bool bind_now)
165 1.1 cgd {
166 1.16 christos Obj_Entry *obj;
167 1.77 mycroft int ok = 1;
168 1.1 cgd
169 1.11 christos for (obj = first; obj != NULL; obj = obj->next) {
170 1.16 christos if (obj->nbuckets == 0 || obj->nchains == 0 ||
171 1.16 christos obj->buckets == NULL || obj->symtab == NULL ||
172 1.16 christos obj->strtab == NULL) {
173 1.11 christos _rtld_error("%s: Shared object has no run-time"
174 1.11 christos " symbol table", obj->path);
175 1.11 christos return -1;
176 1.11 christos }
177 1.102 joerg if (obj->nbuckets == UINT32_MAX) {
178 1.102 joerg _rtld_error("%s: Symbol table too large", obj->path);
179 1.102 joerg return -1;
180 1.102 joerg }
181 1.66 mycroft rdbg((" relocating %s (%ld/%ld rel/rela, %ld/%ld plt rel/rela)",
182 1.11 christos obj->path,
183 1.11 christos (long)(obj->rellim - obj->rel),
184 1.11 christos (long)(obj->relalim - obj->rela),
185 1.11 christos (long)(obj->pltrellim - obj->pltrel),
186 1.16 christos (long)(obj->pltrelalim - obj->pltrela)));
187 1.11 christos
188 1.11 christos if (obj->textrel) {
189 1.108 christos xwarnx("%s: text relocations", obj->path);
190 1.11 christos /*
191 1.11 christos * There are relocations to the write-protected text
192 1.11 christos * segment.
193 1.11 christos */
194 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
195 1.108 christos PROT_READ | PROT_WRITE) == -1) {
196 1.11 christos _rtld_error("%s: Cannot write-enable text "
197 1.11 christos "segment: %s", obj->path, xstrerror(errno));
198 1.11 christos return -1;
199 1.11 christos }
200 1.11 christos }
201 1.71 mycroft dbg(("doing non-PLT relocations"));
202 1.76 junyoung if (_rtld_relocate_nonplt_objects(obj) < 0)
203 1.77 mycroft ok = 0;
204 1.11 christos if (obj->textrel) { /* Re-protected the text segment. */
205 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
206 1.11 christos PROT_READ | PROT_EXEC) == -1) {
207 1.11 christos _rtld_error("%s: Cannot write-protect text "
208 1.11 christos "segment: %s", obj->path, xstrerror(errno));
209 1.11 christos return -1;
210 1.11 christos }
211 1.11 christos }
212 1.71 mycroft dbg(("doing lazy PLT binding"));
213 1.76 junyoung if (_rtld_relocate_plt_lazy(obj) < 0)
214 1.77 mycroft ok = 0;
215 1.103 skrll if (obj->z_now || bind_now) {
216 1.82 skrll dbg(("doing immediate PLT binding"));
217 1.76 junyoung if (_rtld_relocate_plt_objects(obj) < 0)
218 1.77 mycroft ok = 0;
219 1.82 skrll }
220 1.77 mycroft if (!ok)
221 1.77 mycroft return -1;
222 1.77 mycroft
223 1.11 christos /* Set some sanity-checking numbers in the Obj_Entry. */
224 1.11 christos obj->magic = RTLD_MAGIC;
225 1.11 christos obj->version = RTLD_VERSION;
226 1.11 christos
227 1.105 skrll /*
228 1.105 skrll * Fill in the backwards compatibility dynamic linker entry points.
229 1.105 skrll *
230 1.105 skrll * DO NOT ADD TO THIS LIST
231 1.105 skrll */
232 1.100 skrll obj->dlopen = dlopen;
233 1.100 skrll obj->dlsym = dlsym;
234 1.100 skrll obj->dlerror = dlerror;
235 1.100 skrll obj->dlclose = dlclose;
236 1.100 skrll obj->dladdr = dladdr;
237 1.100 skrll
238 1.71 mycroft dbg(("fixing up PLTGOT"));
239 1.11 christos /* Set the special PLTGOT entries. */
240 1.53 mycroft if (obj->pltgot != NULL)
241 1.53 mycroft _rtld_setup_pltgot(obj);
242 1.109 christos #ifdef GNU_RELRO
243 1.109 christos if (obj->relro_size > 0) {
244 1.109 christos if (mprotect(obj->relro_page, obj->relro_size,
245 1.109 christos PROT_READ) == -1) {
246 1.109 christos _rtld_error("%s: Cannot enforce relro "
247 1.109 christos "protection: %s", obj->path,
248 1.109 christos xstrerror(errno));
249 1.109 christos return -1;
250 1.109 christos }
251 1.109 christos }
252 1.109 christos #endif
253 1.1 cgd }
254 1.1 cgd
255 1.11 christos return 0;
256 1.1 cgd }
257 1.107 joerg
258 1.107 joerg Elf_Addr
259 1.107 joerg _rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def)
260 1.107 joerg {
261 1.107 joerg Elf_Addr target;
262 1.107 joerg
263 1.107 joerg _rtld_shared_exit();
264 1.111 joerg target = _rtld_resolve_ifunc2(obj,
265 1.107 joerg (Elf_Addr)obj->relocbase + def->st_value);
266 1.107 joerg _rtld_shared_enter();
267 1.111 joerg return target;
268 1.111 joerg }
269 1.111 joerg
270 1.111 joerg Elf_Addr
271 1.111 joerg _rtld_resolve_ifunc2(const Obj_Entry *obj, Elf_Addr addr)
272 1.111 joerg {
273 1.111 joerg Elf_Addr target;
274 1.111 joerg
275 1.111 joerg target = _rtld_call_function_addr(obj, addr);
276 1.107 joerg
277 1.107 joerg return target;
278 1.107 joerg }
279