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