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