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