mdreloc.c revision 1.44 1 1.44 riastrad /* $NetBSD: mdreloc.c,v 1.44 2024/08/03 21:59:58 riastradh Exp $ */
2 1.21 skrll
3 1.21 skrll #include <sys/cdefs.h>
4 1.21 skrll #ifndef lint
5 1.44 riastrad __RCSID("$NetBSD: mdreloc.c,v 1.44 2024/08/03 21:59:58 riastradh Exp $");
6 1.21 skrll #endif /* not lint */
7 1.11 junyoung
8 1.1 mycroft #include <sys/types.h>
9 1.33 joerg #include <sys/ucontext.h>
10 1.1 mycroft
11 1.1 mycroft #include "debug.h"
12 1.1 mycroft #include "rtld.h"
13 1.1 mycroft
14 1.13 mycroft void _rtld_bind_start(void);
15 1.12 mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
16 1.19 skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
17 1.12 mycroft
18 1.43 martin #define rdbg_symname(obj, rela) \
19 1.43 martin ((obj)->strtab + (obj)->symtab[ELF_R_SYM((rela)->r_info)].st_name)
20 1.43 martin
21 1.1 mycroft void
22 1.1 mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
23 1.1 mycroft {
24 1.1 mycroft obj->pltgot[1] = (Elf_Addr) obj;
25 1.1 mycroft obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
26 1.1 mycroft }
27 1.2 mycroft
28 1.12 mycroft void
29 1.19 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
30 1.12 mycroft {
31 1.12 mycroft const Elf_Rel *rel = 0, *rellim;
32 1.12 mycroft Elf_Addr relsz = 0;
33 1.12 mycroft Elf_Addr *where;
34 1.12 mycroft
35 1.12 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
36 1.12 mycroft switch (dynp->d_tag) {
37 1.12 mycroft case DT_REL:
38 1.12 mycroft rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
39 1.12 mycroft break;
40 1.12 mycroft case DT_RELSZ:
41 1.12 mycroft relsz = dynp->d_un.d_val;
42 1.12 mycroft break;
43 1.12 mycroft }
44 1.12 mycroft }
45 1.22 christos if (rel == 0 || relsz == 0)
46 1.22 christos return;
47 1.27 lukem rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
48 1.12 mycroft for (; rel < rellim; rel++) {
49 1.12 mycroft where = (Elf_Addr *)(relocbase + rel->r_offset);
50 1.12 mycroft *where += (Elf_Addr)relocbase;
51 1.12 mycroft }
52 1.12 mycroft }
53 1.12 mycroft
54 1.2 mycroft int
55 1.32 joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
56 1.2 mycroft {
57 1.3 mycroft const Elf_Rel *rel;
58 1.20 lukem Elf_Addr target = 0;
59 1.38 joerg const Elf_Sym *def = NULL;
60 1.38 joerg const Obj_Entry *defobj = NULL;
61 1.38 joerg unsigned long last_symnum = ULONG_MAX;
62 1.12 mycroft
63 1.3 mycroft for (rel = obj->rel; rel < obj->rellim; rel++) {
64 1.3 mycroft Elf_Addr *where;
65 1.3 mycroft Elf_Addr tmp;
66 1.4 mycroft unsigned long symnum;
67 1.3 mycroft
68 1.3 mycroft where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
69 1.38 joerg
70 1.38 joerg switch (ELF_R_TYPE(rel->r_info)) {
71 1.38 joerg case R_TYPE(PC32):
72 1.38 joerg case R_TYPE(GOT32):
73 1.38 joerg case R_TYPE(32):
74 1.38 joerg case R_TYPE(GLOB_DAT):
75 1.38 joerg case R_TYPE(TLS_TPOFF):
76 1.38 joerg case R_TYPE(TLS_TPOFF32):
77 1.38 joerg case R_TYPE(TLS_DTPMOD32):
78 1.38 joerg case R_TYPE(TLS_DTPOFF32):
79 1.38 joerg symnum = ELF_R_SYM(rel->r_info);
80 1.38 joerg if (symnum != last_symnum) {
81 1.38 joerg last_symnum = symnum;
82 1.38 joerg def = _rtld_find_symdef(symnum, obj, &defobj,
83 1.38 joerg false);
84 1.38 joerg if (def == NULL)
85 1.38 joerg return -1;
86 1.38 joerg }
87 1.38 joerg break;
88 1.38 joerg default:
89 1.38 joerg break;
90 1.38 joerg }
91 1.38 joerg
92 1.3 mycroft
93 1.3 mycroft switch (ELF_R_TYPE(rel->r_info)) {
94 1.3 mycroft case R_TYPE(NONE):
95 1.3 mycroft break;
96 1.2 mycroft
97 1.2 mycroft #if 1 /* XXX should not occur */
98 1.3 mycroft case R_TYPE(PC32):
99 1.23 matt target = (Elf_Addr)(defobj->relocbase + def->st_value);
100 1.3 mycroft
101 1.4 mycroft *where += target - (Elf_Addr)where;
102 1.14 mycroft rdbg(("PC32 %s in %s --> %p in %s",
103 1.43 martin rdbg_symname(obj, rel),
104 1.5 mycroft obj->path, (void *)*where, defobj->path));
105 1.3 mycroft break;
106 1.2 mycroft
107 1.3 mycroft case R_TYPE(GOT32):
108 1.2 mycroft #endif
109 1.3 mycroft case R_TYPE(32):
110 1.3 mycroft case R_TYPE(GLOB_DAT):
111 1.23 matt target = (Elf_Addr)(defobj->relocbase + def->st_value);
112 1.3 mycroft
113 1.4 mycroft tmp = target + *where;
114 1.3 mycroft if (*where != tmp)
115 1.3 mycroft *where = tmp;
116 1.14 mycroft rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
117 1.43 martin rdbg_symname(obj, rel),
118 1.5 mycroft obj->path, (void *)*where, defobj->path));
119 1.3 mycroft break;
120 1.3 mycroft
121 1.41 joerg
122 1.41 joerg case R_TYPE(IRELATIVE):
123 1.41 joerg /* IFUNC relocations are handled in _rtld_call_ifunc */
124 1.41 joerg if (obj->ifunc_remaining_nonplt == 0) {
125 1.41 joerg obj->ifunc_remaining_nonplt =
126 1.41 joerg obj->rellim - rel;
127 1.41 joerg }
128 1.41 joerg /* FALL-THROUGH */
129 1.41 joerg
130 1.3 mycroft case R_TYPE(RELATIVE):
131 1.12 mycroft *where += (Elf_Addr)obj->relocbase;
132 1.14 mycroft rdbg(("RELATIVE in %s --> %p", obj->path,
133 1.12 mycroft (void *)*where));
134 1.3 mycroft break;
135 1.3 mycroft
136 1.3 mycroft case R_TYPE(COPY):
137 1.3 mycroft /*
138 1.3 mycroft * These are deferred until all other relocations have
139 1.3 mycroft * been done. All we do here is make sure that the
140 1.3 mycroft * COPY relocation is not in a shared library. They
141 1.3 mycroft * are allowed only in executable files.
142 1.3 mycroft */
143 1.8 mycroft if (obj->isdynamic) {
144 1.3 mycroft _rtld_error(
145 1.2 mycroft "%s: Unexpected R_COPY relocation in shared library",
146 1.3 mycroft obj->path);
147 1.3 mycroft return -1;
148 1.3 mycroft }
149 1.14 mycroft rdbg(("COPY (avoid in main)"));
150 1.3 mycroft break;
151 1.3 mycroft
152 1.33 joerg case R_TYPE(TLS_TPOFF):
153 1.42 joerg if (!defobj->tls_static &&
154 1.42 joerg _rtld_tls_offset_allocate(__UNCONST(defobj)))
155 1.33 joerg return -1;
156 1.33 joerg
157 1.35 apb *where += (Elf_Addr)(def->st_value - defobj->tlsoffset);
158 1.33 joerg
159 1.33 joerg rdbg(("TLS_TPOFF %s in %s --> %p",
160 1.43 martin rdbg_symname(obj, rel),
161 1.33 joerg obj->path, (void *)*where));
162 1.33 joerg break;
163 1.33 joerg
164 1.35 apb case R_TYPE(TLS_TPOFF32):
165 1.42 joerg if (!defobj->tls_static &&
166 1.42 joerg _rtld_tls_offset_allocate(__UNCONST(defobj)))
167 1.35 apb return -1;
168 1.35 apb
169 1.35 apb *where += (Elf_Addr)(defobj->tlsoffset - def->st_value);
170 1.35 apb rdbg(("TLS_TPOFF32 %s in %s --> %p",
171 1.43 martin rdbg_symname(obj, rel),
172 1.35 apb obj->path, (void *)*where));
173 1.35 apb break;
174 1.35 apb
175 1.33 joerg case R_TYPE(TLS_DTPMOD32):
176 1.33 joerg *where = (Elf_Addr)(defobj->tlsindex);
177 1.33 joerg
178 1.33 joerg rdbg(("TLS_DTPMOD32 %s in %s --> %p",
179 1.43 martin rdbg_symname(obj, rel),
180 1.33 joerg obj->path, (void *)*where));
181 1.33 joerg break;
182 1.33 joerg
183 1.33 joerg case R_TYPE(TLS_DTPOFF32):
184 1.33 joerg *where = (Elf_Addr)(def->st_value);
185 1.33 joerg
186 1.33 joerg rdbg(("TLS_DTPOFF32 %s in %s --> %p",
187 1.43 martin rdbg_symname(obj, rel),
188 1.33 joerg obj->path, (void *)*where));
189 1.33 joerg
190 1.33 joerg break;
191 1.33 joerg
192 1.3 mycroft default:
193 1.14 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
194 1.3 mycroft "contents = %p, symbol = %s",
195 1.38 joerg (u_long)ELF_R_SYM(rel->r_info),
196 1.38 joerg (u_long)ELF_R_TYPE(rel->r_info),
197 1.3 mycroft (void *)rel->r_offset, (void *)*where,
198 1.43 martin rdbg_symname(obj, rel)));
199 1.3 mycroft _rtld_error("%s: Unsupported relocation type %ld "
200 1.28 jmmv "in non-PLT relocations",
201 1.3 mycroft obj->path, (u_long) ELF_R_TYPE(rel->r_info));
202 1.2 mycroft return -1;
203 1.2 mycroft }
204 1.2 mycroft }
205 1.6 mycroft return 0;
206 1.6 mycroft }
207 1.6 mycroft
208 1.6 mycroft int
209 1.39 joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
210 1.6 mycroft {
211 1.6 mycroft const Elf_Rel *rel;
212 1.6 mycroft
213 1.39 joerg for (rel = obj->pltrellim; rel-- > obj->pltrel; ) {
214 1.6 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
215 1.6 mycroft
216 1.39 joerg assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT) ||
217 1.39 joerg ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE));
218 1.39 joerg
219 1.39 joerg if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE))
220 1.39 joerg obj->ifunc_remaining = obj->pltrellim - rel;
221 1.6 mycroft
222 1.6 mycroft /* Just relocate the GOT slots pointing into the PLT */
223 1.6 mycroft *where += (Elf_Addr)obj->relocbase;
224 1.14 mycroft rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
225 1.6 mycroft }
226 1.6 mycroft
227 1.6 mycroft return 0;
228 1.6 mycroft }
229 1.6 mycroft
230 1.26 matt static inline int
231 1.26 matt _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
232 1.26 matt Elf_Addr *tp)
233 1.6 mycroft {
234 1.16 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
235 1.26 matt Elf_Addr target;
236 1.26 matt const Elf_Sym *def;
237 1.6 mycroft const Obj_Entry *defobj;
238 1.29 christos unsigned long info = rel->r_info;
239 1.6 mycroft
240 1.39 joerg if (ELF_R_TYPE(info) == R_TYPE(IRELATIVE))
241 1.39 joerg return 0;
242 1.39 joerg
243 1.29 christos assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
244 1.6 mycroft
245 1.29 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
246 1.29 christos if (__predict_false(def == NULL))
247 1.26 matt return -1;
248 1.29 christos if (__predict_false(def == &_rtld_sym_zero))
249 1.29 christos return 0;
250 1.29 christos
251 1.36 joerg if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
252 1.36 joerg if (tp == NULL)
253 1.36 joerg return 0;
254 1.36 joerg target = _rtld_resolve_ifunc(defobj, def);
255 1.36 joerg } else {
256 1.36 joerg target = (Elf_Addr)(defobj->relocbase + def->st_value);
257 1.36 joerg }
258 1.36 joerg
259 1.26 matt rdbg(("bind now/fixup in %s --> old=%p new=%p",
260 1.44 riastrad defobj->strtab + def->st_name, (void *)*where,
261 1.26 matt (void *)target));
262 1.26 matt if (*where != target)
263 1.26 matt *where = target;
264 1.26 matt if (tp)
265 1.26 matt *tp = target;
266 1.26 matt return 0;
267 1.26 matt }
268 1.26 matt
269 1.26 matt caddr_t
270 1.26 matt _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
271 1.26 matt {
272 1.31 skrll const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel
273 1.31 skrll + reloff);
274 1.26 matt Elf_Addr new_value;
275 1.26 matt int err;
276 1.26 matt
277 1.30 skrll new_value = 0; /* XXX gcc */
278 1.30 skrll
279 1.34 joerg _rtld_shared_enter();
280 1.26 matt err = _rtld_relocate_plt_object(obj, rel, &new_value);
281 1.29 christos if (err)
282 1.16 mycroft _rtld_die();
283 1.34 joerg _rtld_shared_exit();
284 1.6 mycroft
285 1.16 mycroft return (caddr_t)new_value;
286 1.15 junyoung }
287 1.15 junyoung
288 1.15 junyoung int
289 1.15 junyoung _rtld_relocate_plt_objects(const Obj_Entry *obj)
290 1.15 junyoung {
291 1.15 junyoung const Elf_Rel *rel;
292 1.26 matt int err = 0;
293 1.44 riastrad
294 1.15 junyoung for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
295 1.26 matt err = _rtld_relocate_plt_object(obj, rel, NULL);
296 1.26 matt if (err)
297 1.26 matt break;
298 1.15 junyoung }
299 1.26 matt return err;
300 1.2 mycroft }
301 1.33 joerg
302 1.33 joerg /*
303 1.33 joerg * i386 specific GNU variant of __tls_get_addr using register based
304 1.33 joerg * argument passing.
305 1.33 joerg */
306 1.33 joerg #define DTV_MAX_INDEX(dtv) ((size_t)((dtv)[-1]))
307 1.33 joerg
308 1.33 joerg __dso_public __attribute__((__regparm__(1))) void *
309 1.33 joerg ___tls_get_addr(void *arg_)
310 1.33 joerg {
311 1.33 joerg size_t *arg = (size_t *)arg_;
312 1.33 joerg void **dtv;
313 1.33 joerg struct tls_tcb *tcb = __lwp_getprivate_fast();
314 1.33 joerg size_t idx = arg[0], offset = arg[1];
315 1.33 joerg
316 1.33 joerg dtv = tcb->tcb_dtv;
317 1.33 joerg
318 1.33 joerg if (__predict_true(idx < DTV_MAX_INDEX(dtv) && dtv[idx] != NULL))
319 1.33 joerg return (uint8_t *)dtv[idx] + offset;
320 1.33 joerg
321 1.33 joerg return _rtld_tls_get_addr(tcb, idx, offset);
322 1.33 joerg }
323