mdreloc.c revision 1.49 1 1.49 riastrad /* $NetBSD: mdreloc.c,v 1.49 2024/08/03 21:59:59 riastradh Exp $ */
2 1.4 thorpej
3 1.4 thorpej /*
4 1.4 thorpej * Copyright (c) 2001 Wasabi Systems, Inc.
5 1.4 thorpej * All rights reserved.
6 1.4 thorpej *
7 1.4 thorpej * Written by Frank van der Linden for Wasabi Systems, Inc.
8 1.4 thorpej *
9 1.4 thorpej * Redistribution and use in source and binary forms, with or without
10 1.4 thorpej * modification, are permitted provided that the following conditions
11 1.4 thorpej * are met:
12 1.4 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.4 thorpej * notice, this list of conditions and the following disclaimer.
14 1.4 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.4 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.4 thorpej * documentation and/or other materials provided with the distribution.
17 1.4 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.4 thorpej * must display the following acknowledgement:
19 1.4 thorpej * This product includes software developed for the NetBSD Project by
20 1.4 thorpej * Wasabi Systems, Inc.
21 1.4 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.4 thorpej * or promote products derived from this software without specific prior
23 1.4 thorpej * written permission.
24 1.4 thorpej *
25 1.4 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.4 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.4 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.4 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.4 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.4 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.4 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.4 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.4 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.4 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.4 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.5 thorpej */
37 1.5 thorpej
38 1.5 thorpej /*
39 1.5 thorpej * Copyright 1996 John D. Polstra.
40 1.5 thorpej * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
41 1.5 thorpej * All rights reserved.
42 1.5 thorpej *
43 1.5 thorpej * Redistribution and use in source and binary forms, with or without
44 1.5 thorpej * modification, are permitted provided that the following conditions
45 1.5 thorpej * are met:
46 1.5 thorpej * 1. Redistributions of source code must retain the above copyright
47 1.5 thorpej * notice, this list of conditions and the following disclaimer.
48 1.5 thorpej * 2. Redistributions in binary form must reproduce the above copyright
49 1.5 thorpej * notice, this list of conditions and the following disclaimer in the
50 1.5 thorpej * documentation and/or other materials provided with the distribution.
51 1.5 thorpej * 3. All advertising materials mentioning features or use of this software
52 1.5 thorpej * must display the following acknowledgement:
53 1.5 thorpej * This product includes software developed by John Polstra.
54 1.5 thorpej * 4. The name of the author may not be used to endorse or promote products
55 1.5 thorpej * derived from this software without specific prior written permission.
56 1.5 thorpej *
57 1.5 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 1.5 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 1.5 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 1.5 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 1.5 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 1.5 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 1.5 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 1.5 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 1.5 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 1.5 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 1.4 thorpej */
68 1.4 thorpej
69 1.26 skrll #include <sys/cdefs.h>
70 1.26 skrll #ifndef lint
71 1.49 riastrad __RCSID("$NetBSD: mdreloc.c,v 1.49 2024/08/03 21:59:59 riastradh Exp $");
72 1.26 skrll #endif /* not lint */
73 1.26 skrll
74 1.1 fvdl #include <sys/types.h>
75 1.1 fvdl #include <sys/mman.h>
76 1.1 fvdl #include <errno.h>
77 1.1 fvdl #include <fcntl.h>
78 1.1 fvdl #include <stdarg.h>
79 1.1 fvdl #include <stdio.h>
80 1.1 fvdl #include <stdlib.h>
81 1.1 fvdl #include <string.h>
82 1.1 fvdl #include <unistd.h>
83 1.1 fvdl #include <elf.h>
84 1.1 fvdl
85 1.1 fvdl #include "debug.h"
86 1.1 fvdl #include "rtld.h"
87 1.16 mycroft
88 1.16 mycroft void _rtld_bind_start(void);
89 1.21 fvdl void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
90 1.23 skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
91 1.25 skrll static inline int _rtld_relocate_plt_object(const Obj_Entry *,
92 1.25 skrll const Elf_Rela *, Elf_Addr *);
93 1.1 fvdl
94 1.44 christos #define rdbg_symname(obj, rela) \
95 1.43 christos ((obj)->strtab + (obj)->symtab[ELF_R_SYM((rela)->r_info)].st_name)
96 1.43 christos
97 1.8 mycroft void
98 1.8 mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
99 1.8 mycroft {
100 1.8 mycroft obj->pltgot[1] = (Elf_Addr) obj;
101 1.8 mycroft obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
102 1.21 fvdl }
103 1.21 fvdl
104 1.21 fvdl void
105 1.23 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
106 1.21 fvdl {
107 1.21 fvdl const Elf_Rela *rela = 0, *relalim;
108 1.21 fvdl Elf_Addr relasz = 0;
109 1.21 fvdl Elf_Addr *where;
110 1.21 fvdl
111 1.21 fvdl for (; dynp->d_tag != DT_NULL; dynp++) {
112 1.21 fvdl switch (dynp->d_tag) {
113 1.21 fvdl case DT_RELA:
114 1.21 fvdl rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
115 1.21 fvdl break;
116 1.21 fvdl case DT_RELASZ:
117 1.21 fvdl relasz = dynp->d_un.d_val;
118 1.21 fvdl break;
119 1.21 fvdl }
120 1.21 fvdl }
121 1.21 fvdl /*
122 1.21 fvdl * Assume only 64-bit relocations here, which should always
123 1.21 fvdl * be true for the dynamic linker.
124 1.21 fvdl */
125 1.34 lukem relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
126 1.21 fvdl for (; rela < relalim; rela++) {
127 1.21 fvdl where = (Elf_Addr *)(relocbase + rela->r_offset);
128 1.21 fvdl *where = (Elf_Addr)(relocbase + rela->r_addend);
129 1.21 fvdl }
130 1.8 mycroft }
131 1.8 mycroft
132 1.8 mycroft int
133 1.38 joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
134 1.1 fvdl {
135 1.9 mycroft const Elf_Rela *rela;
136 1.28 skrll const Elf_Sym *def = NULL;
137 1.42 joerg const Obj_Entry *defobj = NULL;
138 1.42 joerg unsigned long last_symnum = ULONG_MAX;
139 1.1 fvdl
140 1.9 mycroft for (rela = obj->rela; rela < obj->relalim; rela++) {
141 1.9 mycroft Elf64_Addr *where64;
142 1.9 mycroft Elf32_Addr *where32;
143 1.9 mycroft Elf64_Addr tmp64;
144 1.9 mycroft Elf32_Addr tmp32;
145 1.42 joerg unsigned long symnum;
146 1.1 fvdl
147 1.9 mycroft where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
148 1.9 mycroft where32 = (Elf32_Addr *)where64;
149 1.42 joerg
150 1.42 joerg switch (ELF_R_TYPE(rela->r_info)) {
151 1.42 joerg case R_TYPE(32): /* word32 S + A, truncate */
152 1.42 joerg case R_TYPE(32S): /* word32 S + A, signed truncate */
153 1.42 joerg case R_TYPE(GOT32): /* word32 G + A (XXX can we see these?) */
154 1.42 joerg case R_TYPE(64): /* word64 S + A */
155 1.42 joerg case R_TYPE(PC32): /* word32 S + A - P */
156 1.42 joerg case R_TYPE(GLOB_DAT): /* word64 S */
157 1.42 joerg case R_TYPE(TPOFF64):
158 1.42 joerg case R_TYPE(DTPMOD64):
159 1.42 joerg case R_TYPE(DTPOFF64):
160 1.42 joerg symnum = ELF_R_SYM(rela->r_info);
161 1.42 joerg if (last_symnum != symnum) {
162 1.42 joerg last_symnum = symnum;
163 1.42 joerg def = _rtld_find_symdef(symnum, obj, &defobj,
164 1.42 joerg false);
165 1.42 joerg if (def == NULL)
166 1.42 joerg return -1;
167 1.42 joerg }
168 1.42 joerg break;
169 1.42 joerg default:
170 1.42 joerg break;
171 1.42 joerg }
172 1.1 fvdl
173 1.9 mycroft switch (ELF_R_TYPE(rela->r_info)) {
174 1.9 mycroft case R_TYPE(NONE):
175 1.9 mycroft break;
176 1.9 mycroft
177 1.9 mycroft case R_TYPE(32): /* word32 S + A, truncate */
178 1.9 mycroft case R_TYPE(32S): /* word32 S + A, signed truncate */
179 1.9 mycroft case R_TYPE(GOT32): /* word32 G + A (XXX can we see these?) */
180 1.9 mycroft tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
181 1.9 mycroft def->st_value + rela->r_addend);
182 1.9 mycroft
183 1.9 mycroft if (*where32 != tmp32)
184 1.9 mycroft *where32 = tmp32;
185 1.17 mycroft rdbg(("32/32S %s in %s --> %p in %s",
186 1.44 christos rdbg_symname(obj, rela),
187 1.39 joerg obj->path, (void *)(uintptr_t)*where32,
188 1.11 mycroft defobj->path));
189 1.9 mycroft break;
190 1.9 mycroft case R_TYPE(64): /* word64 S + A */
191 1.9 mycroft tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
192 1.9 mycroft rela->r_addend);
193 1.9 mycroft
194 1.9 mycroft if (*where64 != tmp64)
195 1.9 mycroft *where64 = tmp64;
196 1.17 mycroft rdbg(("64 %s in %s --> %p in %s",
197 1.44 christos rdbg_symname(obj, rela),
198 1.11 mycroft obj->path, (void *)*where64, defobj->path));
199 1.9 mycroft break;
200 1.9 mycroft case R_TYPE(PC32): /* word32 S + A - P */
201 1.9 mycroft tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
202 1.9 mycroft def->st_value + rela->r_addend -
203 1.9 mycroft (Elf64_Addr)where64);
204 1.9 mycroft if (*where32 != tmp32)
205 1.9 mycroft *where32 = tmp32;
206 1.17 mycroft rdbg(("PC32 %s in %s --> %p in %s",
207 1.44 christos rdbg_symname(obj, rela),
208 1.11 mycroft obj->path, (void *)(unsigned long)*where32,
209 1.11 mycroft defobj->path));
210 1.9 mycroft break;
211 1.9 mycroft case R_TYPE(GLOB_DAT): /* word64 S */
212 1.9 mycroft tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
213 1.9 mycroft
214 1.9 mycroft if (*where64 != tmp64)
215 1.9 mycroft *where64 = tmp64;
216 1.17 mycroft rdbg(("64 %s in %s --> %p in %s",
217 1.44 christos rdbg_symname(obj, rela),
218 1.11 mycroft obj->path, (void *)*where64, defobj->path));
219 1.9 mycroft break;
220 1.9 mycroft case R_TYPE(RELATIVE): /* word64 B + A */
221 1.9 mycroft tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
222 1.9 mycroft if (*where64 != tmp64)
223 1.9 mycroft *where64 = tmp64;
224 1.17 mycroft rdbg(("RELATIVE in %s --> %p", obj->path,
225 1.9 mycroft (void *)*where64));
226 1.9 mycroft break;
227 1.9 mycroft
228 1.39 joerg case R_TYPE(TPOFF64):
229 1.48 joerg if (!defobj->tls_static &&
230 1.48 joerg _rtld_tls_offset_allocate(__UNCONST(defobj)))
231 1.39 joerg return -1;
232 1.39 joerg
233 1.39 joerg *where64 = (Elf64_Addr)(def->st_value -
234 1.39 joerg defobj->tlsoffset + rela->r_addend);
235 1.39 joerg
236 1.39 joerg rdbg(("TPOFF64 %s in %s --> %p",
237 1.44 christos rdbg_symname(obj, rela),
238 1.39 joerg obj->path, (void *)*where64));
239 1.39 joerg
240 1.39 joerg break;
241 1.39 joerg
242 1.39 joerg case R_TYPE(DTPMOD64):
243 1.39 joerg *where64 = (Elf64_Addr)defobj->tlsindex;
244 1.39 joerg
245 1.39 joerg rdbg(("DTPMOD64 %s in %s --> %p",
246 1.44 christos rdbg_symname(obj, rela),
247 1.39 joerg obj->path, (void *)*where64));
248 1.39 joerg
249 1.39 joerg break;
250 1.39 joerg
251 1.39 joerg case R_TYPE(DTPOFF64):
252 1.39 joerg *where64 = (Elf64_Addr)(def->st_value + rela->r_addend);
253 1.39 joerg
254 1.39 joerg rdbg(("DTPOFF64 %s in %s --> %p",
255 1.44 christos rdbg_symname(obj, rela),
256 1.39 joerg obj->path, (void *)*where64));
257 1.39 joerg
258 1.39 joerg break;
259 1.39 joerg
260 1.9 mycroft case R_TYPE(COPY):
261 1.17 mycroft rdbg(("COPY"));
262 1.9 mycroft break;
263 1.9 mycroft
264 1.47 joerg case R_TYPE(IRELATIVE):
265 1.47 joerg /* IFUNC relocations are handled in _rtld_call_ifunc */
266 1.47 joerg if (obj->ifunc_remaining_nonplt == 0) {
267 1.47 joerg obj->ifunc_remaining_nonplt =
268 1.47 joerg obj->relalim - rela;
269 1.47 joerg }
270 1.47 joerg break;
271 1.47 joerg
272 1.9 mycroft default:
273 1.17 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
274 1.9 mycroft "addend = %p, contents = %p, symbol = %s",
275 1.42 joerg (u_long)ELF_R_SYM(rela->r_info),
276 1.42 joerg (u_long)ELF_R_TYPE(rela->r_info),
277 1.9 mycroft (void *)rela->r_offset, (void *)rela->r_addend,
278 1.44 christos (void *)*where64, rdbg_symname(obj, rela)));
279 1.9 mycroft _rtld_error("%s: Unsupported relocation type %ld "
280 1.35 jmmv "in non-PLT relocations",
281 1.9 mycroft obj->path, (u_long) ELF_R_TYPE(rela->r_info));
282 1.1 fvdl return -1;
283 1.9 mycroft }
284 1.1 fvdl }
285 1.12 mycroft return 0;
286 1.12 mycroft }
287 1.12 mycroft
288 1.12 mycroft int
289 1.45 joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
290 1.12 mycroft {
291 1.12 mycroft const Elf_Rela *rela;
292 1.12 mycroft
293 1.45 joerg for (rela = obj->pltrelalim; rela-- > obj->pltrela; ) {
294 1.12 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
295 1.12 mycroft
296 1.45 joerg assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT) ||
297 1.45 joerg ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE));
298 1.45 joerg
299 1.45 joerg if (ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE))
300 1.45 joerg obj->ifunc_remaining = obj->pltrelalim - rela;
301 1.12 mycroft
302 1.12 mycroft /* Just relocate the GOT slots pointing into the PLT */
303 1.12 mycroft *where += (Elf_Addr)obj->relocbase;
304 1.17 mycroft rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
305 1.12 mycroft }
306 1.12 mycroft
307 1.12 mycroft return 0;
308 1.12 mycroft }
309 1.12 mycroft
310 1.24 skrll static inline int
311 1.24 skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
312 1.12 mycroft {
313 1.12 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
314 1.12 mycroft Elf_Addr new_value;
315 1.12 mycroft const Elf_Sym *def;
316 1.12 mycroft const Obj_Entry *defobj;
317 1.36 christos unsigned long info = rela->r_info;
318 1.12 mycroft
319 1.45 joerg if (ELF_R_TYPE(info) == R_TYPE(IRELATIVE))
320 1.45 joerg return 0;
321 1.45 joerg
322 1.37 skrll assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT));
323 1.12 mycroft
324 1.36 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
325 1.36 christos if (__predict_false(def == NULL))
326 1.24 skrll return -1;
327 1.36 christos if (__predict_false(def == &_rtld_sym_zero))
328 1.36 christos return 0;
329 1.36 christos
330 1.41 joerg if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
331 1.41 joerg if (tp == NULL)
332 1.41 joerg return 0;
333 1.41 joerg new_value = _rtld_resolve_ifunc(defobj, def);
334 1.41 joerg } else {
335 1.41 joerg new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
336 1.41 joerg rela->r_addend);
337 1.41 joerg }
338 1.41 joerg
339 1.49 riastrad rdbg(("bind now/fixup in %s --> old=%p new=%p",
340 1.31 christos defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
341 1.12 mycroft if (*where != new_value)
342 1.12 mycroft *where = new_value;
343 1.12 mycroft
344 1.24 skrll if (tp)
345 1.24 skrll *tp = new_value - rela->r_addend;
346 1.24 skrll
347 1.24 skrll return 0;
348 1.24 skrll }
349 1.24 skrll
350 1.24 skrll caddr_t
351 1.24 skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
352 1.24 skrll {
353 1.24 skrll const Elf_Rela *rela = obj->pltrela + reloff;
354 1.24 skrll Elf_Addr new_value;
355 1.33 lukem int error;
356 1.24 skrll
357 1.27 dan new_value = 0; /* XXX GCC4 */
358 1.27 dan
359 1.40 joerg _rtld_shared_enter();
360 1.33 lukem error = _rtld_relocate_plt_object(obj, rela, &new_value);
361 1.36 christos if (error)
362 1.24 skrll _rtld_die();
363 1.40 joerg _rtld_shared_exit();
364 1.24 skrll
365 1.24 skrll return (caddr_t)new_value;
366 1.24 skrll }
367 1.24 skrll
368 1.24 skrll int
369 1.24 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
370 1.24 skrll {
371 1.24 skrll const Elf_Rela *rela;
372 1.24 skrll
373 1.24 skrll for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
374 1.24 skrll if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
375 1.24 skrll return -1;
376 1.24 skrll
377 1.24 skrll return 0;
378 1.1 fvdl }
379