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