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