mdreloc.c revision 1.47 1 1.47 riastrad /* $NetBSD: mdreloc.c,v 1.47 2024/07/22 23:10:35 riastradh Exp $ */
2 1.26 skrll
3 1.26 skrll #include <sys/cdefs.h>
4 1.26 skrll #ifndef lint
5 1.47 riastrad __RCSID("$NetBSD: mdreloc.c,v 1.47 2024/07/22 23:10:35 riastradh Exp $");
6 1.26 skrll #endif /* not lint */
7 1.11 junyoung
8 1.47 riastrad /*
9 1.47 riastrad * Arm (32-bit) ELF relocations.
10 1.47 riastrad *
11 1.47 riastrad * Reference:
12 1.47 riastrad *
13 1.47 riastrad * [AAELF32] ELF for the Arm Architecture, 2022Q3. Arm Ltd.
14 1.47 riastrad * https://github.com/ARM-software/abi-aa/blob/2982a9f3b512a5bfdc9e3fea5d3b298f9165c36b/aaelf32/aaelf32.rst
15 1.47 riastrad */
16 1.47 riastrad
17 1.1 mycroft #include <sys/types.h>
18 1.23 mrg #include <string.h>
19 1.1 mycroft
20 1.1 mycroft #include "debug.h"
21 1.1 mycroft #include "rtld.h"
22 1.1 mycroft
23 1.13 mycroft void _rtld_bind_start(void);
24 1.12 mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
25 1.22 skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
26 1.12 mycroft
27 1.1 mycroft void
28 1.1 mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
29 1.1 mycroft {
30 1.1 mycroft obj->pltgot[1] = (Elf_Addr) obj;
31 1.1 mycroft obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
32 1.1 mycroft }
33 1.2 mycroft
34 1.12 mycroft void
35 1.22 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
36 1.12 mycroft {
37 1.12 mycroft const Elf_Rel *rel = 0, *rellim;
38 1.12 mycroft Elf_Addr relsz = 0;
39 1.12 mycroft Elf_Addr *where;
40 1.12 mycroft
41 1.12 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
42 1.12 mycroft switch (dynp->d_tag) {
43 1.12 mycroft case DT_REL:
44 1.12 mycroft rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
45 1.12 mycroft break;
46 1.12 mycroft case DT_RELSZ:
47 1.12 mycroft relsz = dynp->d_un.d_val;
48 1.12 mycroft break;
49 1.12 mycroft }
50 1.12 mycroft }
51 1.29 lukem rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
52 1.12 mycroft for (; rel < rellim; rel++) {
53 1.12 mycroft where = (Elf_Addr *)(relocbase + rel->r_offset);
54 1.12 mycroft *where += (Elf_Addr)relocbase;
55 1.12 mycroft }
56 1.12 mycroft }
57 1.12 mycroft
58 1.16 thorpej /*
59 1.16 thorpej * It is possible for the compiler to emit relocations for unaligned data.
60 1.16 thorpej * We handle this situation with these inlines.
61 1.16 thorpej */
62 1.16 thorpej #define RELOC_ALIGNED_P(x) \
63 1.16 thorpej (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
64 1.16 thorpej
65 1.27 perry static inline Elf_Addr
66 1.16 thorpej load_ptr(void *where)
67 1.16 thorpej {
68 1.16 thorpej Elf_Addr res;
69 1.16 thorpej
70 1.16 thorpej memcpy(&res, where, sizeof(res));
71 1.16 thorpej
72 1.16 thorpej return (res);
73 1.16 thorpej }
74 1.16 thorpej
75 1.27 perry static inline void
76 1.16 thorpej store_ptr(void *where, Elf_Addr val)
77 1.16 thorpej {
78 1.16 thorpej
79 1.16 thorpej memcpy(where, &val, sizeof(val));
80 1.16 thorpej }
81 1.16 thorpej
82 1.2 mycroft int
83 1.34 joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
84 1.2 mycroft {
85 1.3 mycroft const Elf_Rel *rel;
86 1.39 joerg const Elf_Sym *def = NULL;
87 1.39 joerg const Obj_Entry *defobj = NULL;
88 1.39 joerg unsigned long last_symnum = ULONG_MAX;
89 1.12 mycroft
90 1.3 mycroft for (rel = obj->rel; rel < obj->rellim; rel++) {
91 1.3 mycroft Elf_Addr *where;
92 1.3 mycroft Elf_Addr tmp;
93 1.4 mycroft unsigned long symnum;
94 1.3 mycroft
95 1.3 mycroft where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
96 1.39 joerg
97 1.39 joerg switch (ELF_R_TYPE(rel->r_info)) {
98 1.39 joerg case R_TYPE(PC24): /* word32 S - P + A */
99 1.39 joerg case R_TYPE(ABS32): /* word32 B + S + A */
100 1.39 joerg case R_TYPE(GLOB_DAT): /* word32 B + S */
101 1.39 joerg case R_TYPE(TLS_DTPOFF32):
102 1.39 joerg case R_TYPE(TLS_DTPMOD32):
103 1.39 joerg case R_TYPE(TLS_TPOFF32):
104 1.39 joerg symnum = ELF_R_SYM(rel->r_info);
105 1.39 joerg if (last_symnum != symnum) {
106 1.39 joerg last_symnum = symnum;
107 1.39 joerg def = _rtld_find_symdef(symnum, obj, &defobj,
108 1.39 joerg false);
109 1.39 joerg if (def == NULL)
110 1.39 joerg return -1;
111 1.39 joerg }
112 1.39 joerg break;
113 1.39 joerg
114 1.39 joerg default:
115 1.39 joerg break;
116 1.39 joerg }
117 1.3 mycroft
118 1.3 mycroft switch (ELF_R_TYPE(rel->r_info)) {
119 1.3 mycroft case R_TYPE(NONE):
120 1.3 mycroft break;
121 1.2 mycroft
122 1.2 mycroft #if 1 /* XXX should not occur */
123 1.3 mycroft case R_TYPE(PC24): { /* word32 S - P + A */
124 1.3 mycroft Elf32_Sword addend;
125 1.2 mycroft
126 1.3 mycroft /*
127 1.3 mycroft * Extract addend and sign-extend if needed.
128 1.3 mycroft */
129 1.17 thorpej addend = *where;
130 1.3 mycroft if (addend & 0x00800000)
131 1.3 mycroft addend |= 0xff000000;
132 1.3 mycroft tmp = (Elf_Addr)obj->relocbase + def->st_value
133 1.3 mycroft - (Elf_Addr)where + (addend << 2);
134 1.3 mycroft if ((tmp & 0xfe000000) != 0xfe000000 &&
135 1.3 mycroft (tmp & 0xfe000000) != 0) {
136 1.3 mycroft _rtld_error(
137 1.3 mycroft "%s: R_ARM_PC24 relocation @ %p to %s failed "
138 1.3 mycroft "(displacement %ld (%#lx) out of range)",
139 1.3 mycroft obj->path, where,
140 1.41 joerg obj->strtab + obj->symtab[
141 1.41 joerg ELF_R_SYM(rel->r_info)].st_name,
142 1.5 mycroft (long) tmp, (long) tmp);
143 1.3 mycroft return -1;
144 1.3 mycroft }
145 1.3 mycroft tmp >>= 2;
146 1.17 thorpej *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
147 1.14 mycroft rdbg(("PC24 %s in %s --> %p @ %p in %s",
148 1.41 joerg obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
149 1.41 joerg .st_name, obj->path, (void *)*where, where,
150 1.41 joerg defobj->path));
151 1.3 mycroft break;
152 1.2 mycroft }
153 1.2 mycroft #endif
154 1.2 mycroft
155 1.3 mycroft case R_TYPE(ABS32): /* word32 B + S + A */
156 1.15 mycroft case R_TYPE(GLOB_DAT): /* word32 B + S */
157 1.16 thorpej if (__predict_true(RELOC_ALIGNED_P(where))) {
158 1.16 thorpej tmp = *where + (Elf_Addr)defobj->relocbase +
159 1.16 thorpej def->st_value;
160 1.24 rearnsha /* Set the Thumb bit, if needed. */
161 1.25 mycroft if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
162 1.24 rearnsha tmp |= 1;
163 1.16 thorpej *where = tmp;
164 1.16 thorpej } else {
165 1.16 thorpej tmp = load_ptr(where) +
166 1.16 thorpej (Elf_Addr)defobj->relocbase +
167 1.16 thorpej def->st_value;
168 1.24 rearnsha /* Set the Thumb bit, if needed. */
169 1.25 mycroft if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
170 1.24 rearnsha tmp |= 1;
171 1.16 thorpej store_ptr(where, tmp);
172 1.16 thorpej }
173 1.15 mycroft rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
174 1.41 joerg obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
175 1.41 joerg .st_name, obj->path, (void *)tmp, where,
176 1.41 joerg defobj->path));
177 1.3 mycroft break;
178 1.3 mycroft
179 1.44 joerg case R_TYPE(IRELATIVE):
180 1.44 joerg /* IFUNC relocations are handled in _rtld_call_ifunc */
181 1.44 joerg if (obj->ifunc_remaining_nonplt == 0)
182 1.44 joerg obj->ifunc_remaining_nonplt = obj->rellim - rel;
183 1.44 joerg /* FALL-THROUGH */
184 1.44 joerg
185 1.3 mycroft case R_TYPE(RELATIVE): /* word32 B + A */
186 1.16 thorpej if (__predict_true(RELOC_ALIGNED_P(where))) {
187 1.16 thorpej tmp = *where + (Elf_Addr)obj->relocbase;
188 1.16 thorpej *where = tmp;
189 1.16 thorpej } else {
190 1.16 thorpej tmp = load_ptr(where) +
191 1.16 thorpej (Elf_Addr)obj->relocbase;
192 1.16 thorpej store_ptr(where, tmp);
193 1.16 thorpej }
194 1.14 mycroft rdbg(("RELATIVE in %s --> %p", obj->path,
195 1.16 thorpej (void *)tmp));
196 1.3 mycroft break;
197 1.3 mycroft
198 1.3 mycroft case R_TYPE(COPY):
199 1.3 mycroft /*
200 1.3 mycroft * These are deferred until all other relocations have
201 1.3 mycroft * been done. All we do here is make sure that the
202 1.3 mycroft * COPY relocation is not in a shared library. They
203 1.3 mycroft * are allowed only in executable files.
204 1.3 mycroft */
205 1.8 mycroft if (obj->isdynamic) {
206 1.3 mycroft _rtld_error(
207 1.2 mycroft "%s: Unexpected R_COPY relocation in shared library",
208 1.3 mycroft obj->path);
209 1.3 mycroft return -1;
210 1.3 mycroft }
211 1.14 mycroft rdbg(("COPY (avoid in main)"));
212 1.3 mycroft break;
213 1.3 mycroft
214 1.36 matt case R_TYPE(TLS_DTPOFF32):
215 1.36 matt tmp = (Elf_Addr)(def->st_value);
216 1.36 matt if (__predict_true(RELOC_ALIGNED_P(where)))
217 1.36 matt *where = tmp;
218 1.36 matt else
219 1.36 matt store_ptr(where, tmp);
220 1.36 matt
221 1.36 matt rdbg(("TLS_DTPOFF32 %s in %s --> %p",
222 1.41 joerg obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
223 1.41 joerg .st_name, obj->path, (void *)tmp));
224 1.36 matt
225 1.36 matt break;
226 1.36 matt case R_TYPE(TLS_DTPMOD32):
227 1.36 matt tmp = (Elf_Addr)(defobj->tlsindex);
228 1.36 matt if (__predict_true(RELOC_ALIGNED_P(where)))
229 1.36 matt *where = tmp;
230 1.36 matt else
231 1.36 matt store_ptr(where, tmp);
232 1.36 matt
233 1.36 matt rdbg(("TLS_DTPMOD32 %s in %s --> %p",
234 1.41 joerg obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
235 1.41 joerg .st_name, obj->path, (void *)tmp));
236 1.36 matt
237 1.36 matt break;
238 1.36 matt
239 1.36 matt case R_TYPE(TLS_TPOFF32):
240 1.46 joerg if (!defobj->tls_static &&
241 1.46 joerg _rtld_tls_offset_allocate(__UNCONST(defobj)))
242 1.36 matt return -1;
243 1.36 matt
244 1.45 joerg if (__predict_true(RELOC_ALIGNED_P(where)))
245 1.45 joerg tmp = *where;
246 1.45 joerg else
247 1.45 joerg tmp = load_ptr(where);
248 1.45 joerg tmp += (Elf_Addr)def->st_value + defobj->tlsoffset + sizeof(struct tls_tcb);
249 1.36 matt if (__predict_true(RELOC_ALIGNED_P(where)))
250 1.36 matt *where = tmp;
251 1.36 matt else
252 1.36 matt store_ptr(where, tmp);
253 1.36 matt rdbg(("TLS_TPOFF32 %s in %s --> %p",
254 1.41 joerg obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
255 1.41 joerg .st_name, obj->path, (void *)tmp));
256 1.36 matt break;
257 1.36 matt
258 1.3 mycroft default:
259 1.14 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
260 1.40 joerg "contents = %p",
261 1.39 joerg (u_long)ELF_R_SYM(rel->r_info),
262 1.39 joerg (u_long)ELF_R_TYPE(rel->r_info),
263 1.40 joerg (void *)rel->r_offset, (void *)load_ptr(where)));
264 1.3 mycroft _rtld_error("%s: Unsupported relocation type %ld "
265 1.30 jmmv "in non-PLT relocations",
266 1.3 mycroft obj->path, (u_long) ELF_R_TYPE(rel->r_info));
267 1.2 mycroft return -1;
268 1.2 mycroft }
269 1.2 mycroft }
270 1.2 mycroft return 0;
271 1.2 mycroft }
272 1.6 mycroft
273 1.6 mycroft int
274 1.42 joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
275 1.6 mycroft {
276 1.6 mycroft const Elf_Rel *rel;
277 1.6 mycroft
278 1.42 joerg for (rel = obj->pltrellim; rel-- > obj->pltrel; ) {
279 1.6 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
280 1.6 mycroft
281 1.42 joerg assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT) ||
282 1.42 joerg ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE));
283 1.42 joerg
284 1.42 joerg if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE))
285 1.42 joerg obj->ifunc_remaining = obj->pltrellim - rel;
286 1.6 mycroft
287 1.6 mycroft /* Just relocate the GOT slots pointing into the PLT */
288 1.6 mycroft *where += (Elf_Addr)obj->relocbase;
289 1.14 mycroft rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
290 1.6 mycroft }
291 1.6 mycroft
292 1.6 mycroft return 0;
293 1.6 mycroft }
294 1.6 mycroft
295 1.28 matt static int
296 1.28 matt _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
297 1.28 matt Elf_Addr *tp)
298 1.6 mycroft {
299 1.18 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
300 1.6 mycroft Elf_Addr new_value;
301 1.6 mycroft const Elf_Sym *def;
302 1.6 mycroft const Obj_Entry *defobj;
303 1.31 christos unsigned long info = rel->r_info;
304 1.6 mycroft
305 1.33 skrll assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT));
306 1.6 mycroft
307 1.31 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
308 1.31 christos if (__predict_false(def == NULL))
309 1.28 matt return -1;
310 1.31 christos if (__predict_false(def == &_rtld_sym_zero))
311 1.31 christos return 0;
312 1.6 mycroft
313 1.38 joerg if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
314 1.38 joerg if (tp == NULL)
315 1.38 joerg return 0;
316 1.38 joerg new_value = _rtld_resolve_ifunc(defobj, def);
317 1.38 joerg } else {
318 1.38 joerg new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
319 1.38 joerg }
320 1.24 rearnsha /* Set the Thumb bit, if needed. */
321 1.25 mycroft if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
322 1.24 rearnsha new_value |= 1;
323 1.14 mycroft rdbg(("bind now/fixup in %s --> old=%p new=%p",
324 1.6 mycroft defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
325 1.6 mycroft if (*where != new_value)
326 1.6 mycroft *where = new_value;
327 1.28 matt if (tp)
328 1.28 matt *tp = new_value;
329 1.28 matt
330 1.28 matt return 0;
331 1.28 matt }
332 1.28 matt
333 1.28 matt caddr_t
334 1.28 matt _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
335 1.28 matt {
336 1.29 lukem const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel + reloff);
337 1.32 skrll Elf_Addr new_value = 0; /* XXX gcc */
338 1.28 matt int err;
339 1.28 matt
340 1.35 joerg _rtld_shared_enter();
341 1.28 matt err = _rtld_relocate_plt_object(obj, rel, &new_value);
342 1.31 christos if (err)
343 1.28 matt _rtld_die();
344 1.35 joerg _rtld_shared_exit();
345 1.6 mycroft
346 1.18 mycroft return (caddr_t)new_value;
347 1.21 skrll }
348 1.21 skrll int
349 1.21 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
350 1.21 skrll {
351 1.21 skrll const Elf_Rel *rel;
352 1.28 matt int err = 0;
353 1.21 skrll
354 1.21 skrll for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
355 1.28 matt err = _rtld_relocate_plt_object(obj, rel, NULL);
356 1.28 matt if (err)
357 1.28 matt break;
358 1.21 skrll }
359 1.28 matt
360 1.28 matt return err;
361 1.6 mycroft }
362