reloc.c revision 1.116 1 1.116 kamil /* $NetBSD: reloc.c,v 1.116 2020/02/29 04:24:33 kamil 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.116 kamil __RCSID("$NetBSD: reloc.c,v 1.116 2020/02/29 04:24:33 kamil 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.115 kamil Elf_Hash hash;
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.115 kamil hash.sysv = _rtld_sysv_hash(name);
77 1.115 kamil hash.gnu = _rtld_gnu_hash(name);
78 1.115 kamil
79 1.110 uwe if (__predict_false(size == 0)) {
80 1.110 uwe #if defined(__powerpc__) && !defined(__LP64) /* PR port-macppc/47464 */
81 1.110 uwe if (strcmp(name, "_SDA_BASE_") == 0
82 1.110 uwe || strcmp(name, "_SDA2_BASE_") == 0)
83 1.110 uwe {
84 1.110 uwe rdbg(("COPY %s %s --> ignoring old binutils bug",
85 1.110 uwe dstobj->path, name));
86 1.110 uwe return 0;
87 1.110 uwe }
88 1.110 uwe #endif
89 1.110 uwe #if 0 /* shall we warn? */
90 1.110 uwe xwarnx("%s: zero size COPY relocation for \"%s\"",
91 1.110 uwe dstobj->path, name);
92 1.110 uwe #endif
93 1.110 uwe }
94 1.110 uwe
95 1.104 nonaka for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) {
96 1.115 kamil srcsym = _rtld_symlook_obj(name, &hash, srcobj, 0,
97 1.104 nonaka _rtld_fetch_ventry(dstobj, ELF_R_SYM(rela->r_info)));
98 1.104 nonaka if (srcsym != NULL)
99 1.11 christos break;
100 1.104 nonaka }
101 1.11 christos
102 1.11 christos if (srcobj == NULL) {
103 1.11 christos _rtld_error("Undefined symbol \"%s\" referenced from COPY"
104 1.11 christos " relocation in %s", name, dstobj->path);
105 1.14 pk return (-1);
106 1.11 christos }
107 1.11 christos srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
108 1.84 petrov rdbg(("COPY %s %s %s --> src=%p dst=%p size %ld",
109 1.98 christos dstobj->path, srcobj->path, name, srcaddr,
110 1.84 petrov (void *)dstaddr, (long)size));
111 1.114 christos (void)memcpy(dstaddr, srcaddr, size);
112 1.14 pk return (0);
113 1.1 cgd }
114 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
115 1.11 christos
116 1.11 christos
117 1.1 cgd /*
118 1.1 cgd * Process the special R_xxx_COPY relocations in the main program. These
119 1.1 cgd * copy data from a shared object into a region in the main program's BSS
120 1.1 cgd * segment.
121 1.1 cgd *
122 1.1 cgd * Returns 0 on success, -1 on failure.
123 1.1 cgd */
124 1.1 cgd int
125 1.80 skrll _rtld_do_copy_relocations(const Obj_Entry *dstobj)
126 1.1 cgd {
127 1.14 pk #ifndef RTLD_INHIBIT_COPY_RELOCS
128 1.14 pk
129 1.14 pk /* COPY relocations are invalid elsewhere */
130 1.64 mycroft assert(!dstobj->isdynamic);
131 1.1 cgd
132 1.11 christos if (dstobj->rel != NULL) {
133 1.11 christos const Elf_Rel *rel;
134 1.11 christos for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
135 1.11 christos if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
136 1.35 kleink Elf_Rela ourrela;
137 1.11 christos ourrela.r_info = rel->r_info;
138 1.11 christos ourrela.r_offset = rel->r_offset;
139 1.11 christos ourrela.r_addend = 0;
140 1.11 christos if (_rtld_do_copy_relocation(dstobj,
141 1.66 mycroft &ourrela) < 0)
142 1.14 pk return (-1);
143 1.11 christos }
144 1.11 christos }
145 1.11 christos }
146 1.11 christos if (dstobj->rela != NULL) {
147 1.35 kleink const Elf_Rela *rela;
148 1.11 christos for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
149 1.11 christos if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
150 1.66 mycroft if (_rtld_do_copy_relocation(dstobj, rela) < 0)
151 1.14 pk return (-1);
152 1.11 christos }
153 1.11 christos }
154 1.1 cgd }
155 1.114 christos #ifdef GNU_RELRO
156 1.114 christos if (_rtld_relro(dstobj, true) == -1)
157 1.114 christos return -1;
158 1.114 christos #endif
159 1.14 pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
160 1.1 cgd
161 1.14 pk return (0);
162 1.1 cgd }
163 1.9 pk
164 1.1 cgd /*
165 1.1 cgd * Relocate newly-loaded shared objects. The argument is a pointer to
166 1.1 cgd * the Obj_Entry for the first such object. All objects from the first
167 1.1 cgd * to the end of the list of objects are relocated. Returns 0 on success,
168 1.1 cgd * or -1 on failure.
169 1.1 cgd */
170 1.1 cgd int
171 1.80 skrll _rtld_relocate_objects(Obj_Entry *first, bool bind_now)
172 1.1 cgd {
173 1.16 christos Obj_Entry *obj;
174 1.77 mycroft int ok = 1;
175 1.1 cgd
176 1.11 christos for (obj = first; obj != NULL; obj = obj->next) {
177 1.116 kamil if ((!obj->sysv_hash && !obj->gnu_hash) ||
178 1.116 kamil obj->symtab == NULL || obj->strtab == NULL) {
179 1.11 christos _rtld_error("%s: Shared object has no run-time"
180 1.11 christos " symbol table", obj->path);
181 1.11 christos return -1;
182 1.11 christos }
183 1.102 joerg if (obj->nbuckets == UINT32_MAX) {
184 1.102 joerg _rtld_error("%s: Symbol table too large", obj->path);
185 1.102 joerg return -1;
186 1.102 joerg }
187 1.66 mycroft rdbg((" relocating %s (%ld/%ld rel/rela, %ld/%ld plt rel/rela)",
188 1.11 christos obj->path,
189 1.11 christos (long)(obj->rellim - obj->rel),
190 1.11 christos (long)(obj->relalim - obj->rela),
191 1.11 christos (long)(obj->pltrellim - obj->pltrel),
192 1.16 christos (long)(obj->pltrelalim - obj->pltrela)));
193 1.11 christos
194 1.11 christos if (obj->textrel) {
195 1.108 christos xwarnx("%s: text relocations", obj->path);
196 1.11 christos /*
197 1.11 christos * There are relocations to the write-protected text
198 1.11 christos * segment.
199 1.11 christos */
200 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
201 1.108 christos PROT_READ | PROT_WRITE) == -1) {
202 1.11 christos _rtld_error("%s: Cannot write-enable text "
203 1.11 christos "segment: %s", obj->path, xstrerror(errno));
204 1.11 christos return -1;
205 1.11 christos }
206 1.11 christos }
207 1.71 mycroft dbg(("doing non-PLT relocations"));
208 1.76 junyoung if (_rtld_relocate_nonplt_objects(obj) < 0)
209 1.77 mycroft ok = 0;
210 1.11 christos if (obj->textrel) { /* Re-protected the text segment. */
211 1.11 christos if (mprotect(obj->mapbase, obj->textsize,
212 1.11 christos PROT_READ | PROT_EXEC) == -1) {
213 1.11 christos _rtld_error("%s: Cannot write-protect text "
214 1.11 christos "segment: %s", obj->path, xstrerror(errno));
215 1.11 christos return -1;
216 1.11 christos }
217 1.11 christos }
218 1.71 mycroft dbg(("doing lazy PLT binding"));
219 1.76 junyoung if (_rtld_relocate_plt_lazy(obj) < 0)
220 1.77 mycroft ok = 0;
221 1.103 skrll if (obj->z_now || bind_now) {
222 1.82 skrll dbg(("doing immediate PLT binding"));
223 1.76 junyoung if (_rtld_relocate_plt_objects(obj) < 0)
224 1.77 mycroft ok = 0;
225 1.82 skrll }
226 1.77 mycroft if (!ok)
227 1.77 mycroft return -1;
228 1.77 mycroft
229 1.71 mycroft dbg(("fixing up PLTGOT"));
230 1.11 christos /* Set the special PLTGOT entries. */
231 1.53 mycroft if (obj->pltgot != NULL)
232 1.53 mycroft _rtld_setup_pltgot(obj);
233 1.109 christos #ifdef GNU_RELRO
234 1.114 christos if (_rtld_relro(obj, false) == -1)
235 1.114 christos return -1;
236 1.109 christos #endif
237 1.1 cgd }
238 1.11 christos return 0;
239 1.1 cgd }
240 1.107 joerg
241 1.107 joerg Elf_Addr
242 1.107 joerg _rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def)
243 1.107 joerg {
244 1.107 joerg Elf_Addr target;
245 1.107 joerg
246 1.107 joerg _rtld_shared_exit();
247 1.111 joerg target = _rtld_resolve_ifunc2(obj,
248 1.107 joerg (Elf_Addr)obj->relocbase + def->st_value);
249 1.107 joerg _rtld_shared_enter();
250 1.111 joerg return target;
251 1.111 joerg }
252 1.111 joerg
253 1.111 joerg Elf_Addr
254 1.111 joerg _rtld_resolve_ifunc2(const Obj_Entry *obj, Elf_Addr addr)
255 1.111 joerg {
256 1.111 joerg Elf_Addr target;
257 1.111 joerg
258 1.111 joerg target = _rtld_call_function_addr(obj, addr);
259 1.107 joerg
260 1.107 joerg return target;
261 1.107 joerg }
262 1.112 joerg
263 1.112 joerg #ifdef RTLD_COMMON_CALL_IFUNC_RELA
264 1.112 joerg # ifdef __sparc__
265 1.112 joerg # include <machine/elf_support.h>
266 1.112 joerg # endif
267 1.112 joerg
268 1.112 joerg void
269 1.112 joerg _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
270 1.112 joerg {
271 1.112 joerg const Elf_Rela *rela;
272 1.112 joerg Elf_Addr *where;
273 1.112 joerg #ifdef __sparc__
274 1.112 joerg Elf_Word *where2;
275 1.112 joerg #endif
276 1.112 joerg Elf_Addr target;
277 1.112 joerg
278 1.112 joerg while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
279 1.112 joerg rela = obj->pltrelalim - obj->ifunc_remaining--;
280 1.112 joerg #ifdef __sparc__
281 1.112 joerg #define PLT_IRELATIVE R_TYPE(JMP_IREL)
282 1.112 joerg #else
283 1.112 joerg #define PLT_IRELATIVE R_TYPE(IRELATIVE)
284 1.112 joerg #endif
285 1.112 joerg if (ELF_R_TYPE(rela->r_info) != PLT_IRELATIVE)
286 1.112 joerg continue;
287 1.112 joerg #ifdef __sparc__
288 1.112 joerg where2 = (Elf_Word *)(obj->relocbase + rela->r_offset);
289 1.112 joerg #else
290 1.112 joerg where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
291 1.112 joerg #endif
292 1.112 joerg target = (Elf_Addr)(obj->relocbase + rela->r_addend);
293 1.112 joerg _rtld_exclusive_exit(mask);
294 1.112 joerg target = _rtld_resolve_ifunc2(obj, target);
295 1.112 joerg _rtld_exclusive_enter(mask);
296 1.112 joerg #ifdef __sparc__
297 1.112 joerg sparc_write_branch(where2 + 1, (void *)target);
298 1.112 joerg #else
299 1.112 joerg if (*where != target)
300 1.112 joerg *where = target;
301 1.112 joerg #endif
302 1.112 joerg }
303 1.112 joerg
304 1.112 joerg while (obj->ifunc_remaining_nonplt > 0 && _rtld_objgen == cur_objgen) {
305 1.112 joerg rela = obj->relalim - obj->ifunc_remaining_nonplt--;
306 1.112 joerg if (ELF_R_TYPE(rela->r_info) != R_TYPE(IRELATIVE))
307 1.112 joerg continue;
308 1.112 joerg where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
309 1.112 joerg target = (Elf_Addr)(obj->relocbase + rela->r_addend);
310 1.112 joerg _rtld_exclusive_exit(mask);
311 1.112 joerg target = _rtld_resolve_ifunc2(obj, target);
312 1.112 joerg _rtld_exclusive_enter(mask);
313 1.112 joerg if (*where != target)
314 1.112 joerg *where = target;
315 1.112 joerg }
316 1.112 joerg }
317 1.112 joerg #endif
318 1.112 joerg
319 1.112 joerg #ifdef RTLD_COMMON_CALL_IFUNC_REL
320 1.112 joerg void
321 1.112 joerg _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
322 1.112 joerg {
323 1.112 joerg const Elf_Rel *rel;
324 1.112 joerg Elf_Addr *where, target;
325 1.112 joerg
326 1.112 joerg while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
327 1.112 joerg rel = obj->pltrellim - obj->ifunc_remaining;
328 1.112 joerg --obj->ifunc_remaining;
329 1.112 joerg if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE)) {
330 1.112 joerg where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
331 1.112 joerg _rtld_exclusive_exit(mask);
332 1.112 joerg target = _rtld_resolve_ifunc2(obj, *where);
333 1.112 joerg _rtld_exclusive_enter(mask);
334 1.112 joerg if (*where != target)
335 1.112 joerg *where = target;
336 1.112 joerg }
337 1.112 joerg }
338 1.112 joerg
339 1.112 joerg while (obj->ifunc_remaining_nonplt > 0 && _rtld_objgen == cur_objgen) {
340 1.112 joerg rel = obj->rellim - obj->ifunc_remaining_nonplt--;
341 1.112 joerg if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE)) {
342 1.112 joerg where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
343 1.112 joerg _rtld_exclusive_exit(mask);
344 1.112 joerg target = _rtld_resolve_ifunc2(obj, *where);
345 1.112 joerg _rtld_exclusive_enter(mask);
346 1.112 joerg if (*where != target)
347 1.112 joerg *where = target;
348 1.112 joerg }
349 1.112 joerg }
350 1.112 joerg }
351 1.112 joerg #endif
352