mdreloc.c revision 1.36 1 1.36 riastrad /* $NetBSD: mdreloc.c,v 1.36 2025/04/16 17:40:37 riastradh Exp $ */
2 1.36 riastrad
3 1.36 riastrad /*
4 1.36 riastrad * Copyright 1996 John D. Polstra.
5 1.36 riastrad * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 1.36 riastrad * All rights reserved.
7 1.36 riastrad *
8 1.36 riastrad * Redistribution and use in source and binary forms, with or without
9 1.36 riastrad * modification, are permitted provided that the following conditions
10 1.36 riastrad * are met:
11 1.36 riastrad * 1. Redistributions of source code must retain the above copyright
12 1.36 riastrad * notice, this list of conditions and the following disclaimer.
13 1.36 riastrad * 2. Redistributions in binary form must reproduce the above copyright
14 1.36 riastrad * notice, this list of conditions and the following disclaimer in the
15 1.36 riastrad * documentation and/or other materials provided with the distribution.
16 1.36 riastrad * 3. All advertising materials mentioning features or use of this software
17 1.36 riastrad * must display the following acknowledgement:
18 1.36 riastrad * This product includes software developed by John Polstra.
19 1.36 riastrad * 4. The name of the author may not be used to endorse or promote products
20 1.36 riastrad * derived from this software without specific prior written permission.
21 1.36 riastrad *
22 1.36 riastrad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.36 riastrad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.36 riastrad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.36 riastrad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.36 riastrad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.36 riastrad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.36 riastrad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.36 riastrad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.36 riastrad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.36 riastrad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.36 riastrad */
33 1.19 skrll
34 1.19 skrll #include <sys/cdefs.h>
35 1.19 skrll #ifndef lint
36 1.36 riastrad __RCSID("$NetBSD: mdreloc.c,v 1.36 2025/04/16 17:40:37 riastradh Exp $");
37 1.19 skrll #endif /* not lint */
38 1.10 junyoung
39 1.1 mycroft #include <sys/types.h>
40 1.1 mycroft
41 1.1 mycroft #include "debug.h"
42 1.1 mycroft #include "rtld.h"
43 1.1 mycroft
44 1.35 christos #include <machine/lwp_private.h>
45 1.35 christos
46 1.12 mycroft void _rtld_bind_start(void);
47 1.11 mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
48 1.17 skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
49 1.18 skrll static inline int _rtld_relocate_plt_object(const Obj_Entry *,
50 1.18 skrll const Elf_Rela *, Elf_Addr *);
51 1.18 skrll
52 1.11 mycroft
53 1.1 mycroft void
54 1.1 mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
55 1.1 mycroft {
56 1.1 mycroft obj->pltgot[1] = (Elf_Addr) obj;
57 1.1 mycroft obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
58 1.1 mycroft }
59 1.2 mycroft
60 1.11 mycroft void
61 1.17 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
62 1.11 mycroft {
63 1.11 mycroft const Elf_Rela *rela = 0, *relalim;
64 1.11 mycroft Elf_Addr relasz = 0;
65 1.11 mycroft Elf_Addr *where;
66 1.11 mycroft
67 1.11 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
68 1.11 mycroft switch (dynp->d_tag) {
69 1.11 mycroft case DT_RELA:
70 1.11 mycroft rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
71 1.11 mycroft break;
72 1.11 mycroft case DT_RELASZ:
73 1.11 mycroft relasz = dynp->d_un.d_val;
74 1.11 mycroft break;
75 1.11 mycroft }
76 1.11 mycroft }
77 1.23 lukem relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
78 1.11 mycroft for (; rela < relalim; rela++) {
79 1.11 mycroft where = (Elf_Addr *)(relocbase + rela->r_offset);
80 1.11 mycroft *where += (Elf_Addr)relocbase;
81 1.11 mycroft }
82 1.11 mycroft }
83 1.11 mycroft
84 1.2 mycroft int
85 1.27 joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
86 1.2 mycroft {
87 1.3 mycroft const Elf_Rela *rela;
88 1.32 joerg const Elf_Sym *def = NULL;
89 1.32 joerg const Obj_Entry *defobj = NULL;
90 1.32 joerg unsigned long last_symnum = ULONG_MAX;
91 1.2 mycroft
92 1.3 mycroft for (rela = obj->rela; rela < obj->relalim; rela++) {
93 1.3 mycroft Elf_Addr *where;
94 1.3 mycroft Elf_Addr tmp;
95 1.4 mycroft unsigned long symnum;
96 1.3 mycroft
97 1.3 mycroft where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
98 1.32 joerg
99 1.32 joerg switch (ELF_R_TYPE(rela->r_info)) {
100 1.32 joerg case R_TYPE(PC32):
101 1.32 joerg case R_TYPE(GOT32):
102 1.32 joerg case R_TYPE(32):
103 1.32 joerg case R_TYPE(GLOB_DAT):
104 1.32 joerg case R_TYPE(TLS_DTPMOD32):
105 1.32 joerg case R_TYPE(TLS_DTPREL32):
106 1.32 joerg case R_TYPE(TLS_TPREL32):
107 1.32 joerg symnum = ELF_R_SYM(rela->r_info);
108 1.32 joerg if (last_symnum != symnum) {
109 1.32 joerg last_symnum = symnum;
110 1.32 joerg def = _rtld_find_symdef(symnum, obj, &defobj,
111 1.32 joerg false);
112 1.32 joerg if (def == NULL)
113 1.32 joerg return -1;
114 1.32 joerg }
115 1.32 joerg break;
116 1.32 joerg default:
117 1.32 joerg break;
118 1.32 joerg }
119 1.3 mycroft
120 1.3 mycroft switch (ELF_R_TYPE(rela->r_info)) {
121 1.3 mycroft case R_TYPE(NONE):
122 1.3 mycroft break;
123 1.2 mycroft
124 1.2 mycroft #if 1 /* XXX should not occur */
125 1.3 mycroft case R_TYPE(PC32):
126 1.3 mycroft tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
127 1.3 mycroft rela->r_addend) - (Elf_Addr)where;
128 1.3 mycroft if (*where != tmp)
129 1.3 mycroft *where = tmp;
130 1.13 mycroft rdbg(("PC32 %s in %s --> %p in %s",
131 1.5 mycroft obj->strtab + obj->symtab[symnum].st_name,
132 1.5 mycroft obj->path, (void *)*where, defobj->path));
133 1.3 mycroft break;
134 1.2 mycroft
135 1.3 mycroft case R_TYPE(GOT32):
136 1.2 mycroft #endif
137 1.3 mycroft case R_TYPE(32):
138 1.3 mycroft case R_TYPE(GLOB_DAT):
139 1.3 mycroft tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
140 1.3 mycroft rela->r_addend);
141 1.3 mycroft if (*where != tmp)
142 1.3 mycroft *where = tmp;
143 1.13 mycroft rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
144 1.5 mycroft obj->strtab + obj->symtab[symnum].st_name,
145 1.5 mycroft obj->path, (void *)*where, defobj->path));
146 1.3 mycroft break;
147 1.3 mycroft
148 1.3 mycroft case R_TYPE(RELATIVE):
149 1.3 mycroft *where += (Elf_Addr)obj->relocbase;
150 1.13 mycroft rdbg(("RELATIVE in %s --> %p", obj->path,
151 1.3 mycroft (void *)*where));
152 1.3 mycroft break;
153 1.3 mycroft
154 1.3 mycroft case R_TYPE(COPY):
155 1.3 mycroft /*
156 1.3 mycroft * These are deferred until all other relocations have
157 1.3 mycroft * been done. All we do here is make sure that the
158 1.3 mycroft * COPY relocation is not in a shared library. They
159 1.3 mycroft * are allowed only in executable files.
160 1.3 mycroft */
161 1.8 mycroft if (obj->isdynamic) {
162 1.3 mycroft _rtld_error(
163 1.2 mycroft "%s: Unexpected R_COPY relocation in shared library",
164 1.3 mycroft obj->path);
165 1.3 mycroft return -1;
166 1.3 mycroft }
167 1.13 mycroft rdbg(("COPY (avoid in main)"));
168 1.3 mycroft break;
169 1.3 mycroft
170 1.29 joerg case R_TYPE(TLS_DTPMOD32):
171 1.29 joerg *where = (Elf_Addr)defobj->tlsindex;
172 1.29 joerg rdbg(("DTPMOD32 %s in %s --> %p in %s",
173 1.29 joerg obj->strtab + obj->symtab[symnum].st_name,
174 1.29 joerg obj->path, (void *)*where, defobj->path));
175 1.29 joerg break;
176 1.29 joerg
177 1.29 joerg case R_TYPE(TLS_DTPREL32):
178 1.29 joerg *where = (Elf_Addr)(def->st_value + rela->r_addend
179 1.29 joerg - TLS_DTV_OFFSET);
180 1.29 joerg rdbg(("DTPREL32 %s in %s --> %p in %s",
181 1.29 joerg obj->strtab + obj->symtab[symnum].st_name,
182 1.29 joerg obj->path, (void *)*where, defobj->path));
183 1.29 joerg break;
184 1.29 joerg
185 1.29 joerg case R_TYPE(TLS_TPREL32):
186 1.34 joerg if (!defobj->tls_static &&
187 1.34 joerg _rtld_tls_offset_allocate(__UNCONST(defobj)))
188 1.29 joerg return -1;
189 1.29 joerg
190 1.29 joerg *where = (Elf_Addr)(def->st_value + rela->r_addend
191 1.29 joerg + defobj->tlsoffset - TLS_TP_OFFSET);
192 1.29 joerg rdbg(("TPREL32 %s in %s --> %p in %s",
193 1.29 joerg obj->strtab + obj->symtab[symnum].st_name,
194 1.29 joerg obj->path, (void *)*where, defobj->path));
195 1.29 joerg break;
196 1.29 joerg
197 1.3 mycroft default:
198 1.13 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
199 1.3 mycroft "addend = %p, contents = %p, symbol = %s",
200 1.32 joerg (u_long)ELF_R_SYM(rela->r_info),
201 1.32 joerg (u_long)ELF_R_TYPE(rela->r_info),
202 1.3 mycroft (void *)rela->r_offset, (void *)rela->r_addend,
203 1.3 mycroft (void *)*where,
204 1.4 mycroft obj->strtab + obj->symtab[symnum].st_name));
205 1.3 mycroft _rtld_error("%s: Unsupported relocation type %ld "
206 1.24 jmmv "in non-PLT relocations",
207 1.3 mycroft obj->path, (u_long) ELF_R_TYPE(rela->r_info));
208 1.2 mycroft return -1;
209 1.2 mycroft }
210 1.2 mycroft }
211 1.2 mycroft return 0;
212 1.2 mycroft }
213 1.6 mycroft
214 1.6 mycroft int
215 1.33 joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
216 1.6 mycroft {
217 1.6 mycroft const Elf_Rela *rela;
218 1.6 mycroft
219 1.16 mycroft if (!obj->relocbase)
220 1.6 mycroft return 0;
221 1.6 mycroft
222 1.6 mycroft for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
223 1.6 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
224 1.6 mycroft
225 1.6 mycroft assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
226 1.6 mycroft
227 1.6 mycroft /* Just relocate the GOT slots pointing into the PLT */
228 1.6 mycroft *where += (Elf_Addr)obj->relocbase;
229 1.13 mycroft rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
230 1.6 mycroft }
231 1.6 mycroft
232 1.6 mycroft return 0;
233 1.6 mycroft }
234 1.6 mycroft
235 1.18 skrll static inline int
236 1.26 skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
237 1.26 skrll Elf_Addr *tp)
238 1.6 mycroft {
239 1.6 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
240 1.6 mycroft Elf_Addr new_value;
241 1.6 mycroft const Elf_Sym *def;
242 1.6 mycroft const Obj_Entry *defobj;
243 1.25 christos unsigned long info = rela->r_info;
244 1.6 mycroft
245 1.25 christos assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
246 1.6 mycroft
247 1.25 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
248 1.25 christos if (__predict_false(def == NULL))
249 1.18 skrll return -1;
250 1.25 christos if (__predict_false(def == &_rtld_sym_zero))
251 1.25 christos return 0;
252 1.6 mycroft
253 1.18 skrll assert(rela->r_addend == 0);
254 1.30 joerg if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
255 1.30 joerg if (tp == NULL)
256 1.30 joerg return 0;
257 1.30 joerg new_value = _rtld_resolve_ifunc(defobj, def);
258 1.30 joerg } else {
259 1.30 joerg new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
260 1.30 joerg rela->r_addend);
261 1.30 joerg }
262 1.13 mycroft rdbg(("bind now/fixup in %s --> old=%p new=%p",
263 1.6 mycroft defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
264 1.6 mycroft if (*where != new_value)
265 1.6 mycroft *where = new_value;
266 1.6 mycroft
267 1.18 skrll if (tp)
268 1.18 skrll *tp = new_value - rela->r_addend;
269 1.18 skrll
270 1.18 skrll return 0;
271 1.18 skrll }
272 1.18 skrll
273 1.18 skrll caddr_t
274 1.18 skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
275 1.18 skrll {
276 1.23 lukem const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
277 1.18 skrll Elf_Addr result;
278 1.18 skrll int err;
279 1.18 skrll
280 1.20 mrg result = 0; /* XXX gcc */
281 1.20 mrg
282 1.28 joerg _rtld_shared_enter();
283 1.18 skrll err = _rtld_relocate_plt_object(obj, rela, &result);
284 1.25 christos if (err)
285 1.18 skrll _rtld_die();
286 1.28 joerg _rtld_shared_exit();
287 1.18 skrll
288 1.18 skrll return (caddr_t)result;
289 1.18 skrll }
290 1.18 skrll
291 1.18 skrll int
292 1.18 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
293 1.18 skrll {
294 1.18 skrll const Elf_Rela *rela;
295 1.18 skrll
296 1.18 skrll for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
297 1.18 skrll if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
298 1.18 skrll return -1;
299 1.18 skrll
300 1.18 skrll return 0;
301 1.6 mycroft }
302