xref: /freebsd/libexec/rtld-elf/riscv/reloc.c (revision 4b9d6057)
1 /*-
2  * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * This software was developed by SRI International and the University of
6  * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7  * ("CTSRD"), as part of the DARPA CRASH research programme.
8  *
9  * This software was developed by the University of Cambridge Computer
10  * Laboratory as part of the CTSRD Project, with support from the UK Higher
11  * Education Innovation Fund (HEIF).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 
37 #include <stdlib.h>
38 
39 #include "debug.h"
40 #include "rtld.h"
41 #include "rtld_printf.h"
42 
43 /*
44  * It is possible for the compiler to emit relocations for unaligned data.
45  * We handle this situation with these inlines.
46  */
47 #define	RELOC_ALIGNED_P(x) \
48 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
49 
50 uint64_t
51 set_gp(Obj_Entry *obj)
52 {
53 	uint64_t old;
54 	SymLook req;
55 	uint64_t gp;
56 	int res;
57 
58 	__asm __volatile("mv    %0, gp" : "=r"(old));
59 
60 	symlook_init(&req, "__global_pointer$");
61 	req.ventry = NULL;
62 	req.flags = SYMLOOK_EARLY;
63 	res = symlook_obj(&req, obj);
64 
65 	if (res == 0) {
66 		gp = req.sym_out->st_value;
67 		__asm __volatile("mv    gp, %0" :: "r"(gp));
68 	}
69 
70 	return (old);
71 }
72 
73 void
74 init_pltgot(Obj_Entry *obj)
75 {
76 
77 	if (obj->pltgot != NULL) {
78 		obj->pltgot[0] = (Elf_Addr)&_rtld_bind_start;
79 		obj->pltgot[1] = (Elf_Addr)obj;
80 	}
81 }
82 
83 int
84 do_copy_relocations(Obj_Entry *dstobj)
85 {
86 	const Obj_Entry *srcobj, *defobj;
87 	const Elf_Rela *relalim;
88 	const Elf_Rela *rela;
89 	const Elf_Sym *srcsym;
90 	const Elf_Sym *dstsym;
91 	const void *srcaddr;
92 	const char *name;
93 	void *dstaddr;
94 	SymLook req;
95 	size_t size;
96 	int res;
97 
98 	/*
99 	 * COPY relocs are invalid outside of the main program
100 	 */
101 	assert(dstobj->mainprog);
102 
103 	relalim = (const Elf_Rela *)((const char *)dstobj->rela +
104 	    dstobj->relasize);
105 	for (rela = dstobj->rela; rela < relalim; rela++) {
106 		if (ELF_R_TYPE(rela->r_info) != R_RISCV_COPY)
107 			continue;
108 
109 		dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
110 		dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
111 		name = dstobj->strtab + dstsym->st_name;
112 		size = dstsym->st_size;
113 
114 		symlook_init(&req, name);
115 		req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
116 		req.flags = SYMLOOK_EARLY;
117 
118 		for (srcobj = globallist_next(dstobj); srcobj != NULL;
119 		     srcobj = globallist_next(srcobj)) {
120 			res = symlook_obj(&req, srcobj);
121 			if (res == 0) {
122 				srcsym = req.sym_out;
123 				defobj = req.defobj_out;
124 				break;
125 			}
126 		}
127 		if (srcobj == NULL) {
128 			_rtld_error(
129 "Undefined symbol \"%s\" referenced from COPY relocation in %s",
130 			    name, dstobj->path);
131 			return (-1);
132 		}
133 
134 		srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
135 		memcpy(dstaddr, srcaddr, size);
136 	}
137 
138 	return (0);
139 }
140 
141 /*
142  * Process the PLT relocations.
143  */
144 int
145 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
146 {
147 	const Elf_Rela *relalim;
148 	const Elf_Rela *rela;
149 
150 	relalim = (const Elf_Rela *)((const char *)obj->pltrela +
151 	    obj->pltrelasize);
152 	for (rela = obj->pltrela; rela < relalim; rela++) {
153 		Elf_Addr *where;
154 
155 		assert(ELF_R_TYPE(rela->r_info) == R_RISCV_JUMP_SLOT);
156 
157 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
158 		*where += (Elf_Addr)obj->relocbase;
159 	}
160 
161 	return (0);
162 }
163 
164 /*
165  * LD_BIND_NOW was set - force relocation for all jump slots
166  */
167 int
168 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
169 {
170 	const Obj_Entry *defobj;
171 	const Elf_Rela *relalim;
172 	const Elf_Rela *rela;
173 	const Elf_Sym *def;
174 
175 	relalim = (const Elf_Rela *)((const char *)obj->pltrela +
176 	    obj->pltrelasize);
177 	for (rela = obj->pltrela; rela < relalim; rela++) {
178 		Elf_Addr *where;
179 
180 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
181 		switch(ELF_R_TYPE(rela->r_info)) {
182 		case R_RISCV_JUMP_SLOT:
183 			def = find_symdef(ELF_R_SYM(rela->r_info), obj,
184 			    &defobj, SYMLOOK_IN_PLT | flags, NULL, lockstate);
185 			if (def == NULL) {
186 				dbg("reloc_jmpslots: sym not found");
187 				return (-1);
188 			}
189 
190 			*where = (Elf_Addr)(defobj->relocbase + def->st_value);
191 			break;
192 		default:
193 			_rtld_error("Unknown relocation type %x in jmpslot",
194 			    (unsigned int)ELF_R_TYPE(rela->r_info));
195 			return (-1);
196 		}
197 	}
198 
199 	return (0);
200 }
201 
202 int
203 reloc_iresolve(Obj_Entry *obj __unused,
204     struct Struct_RtldLockState *lockstate __unused)
205 {
206 
207 	/* XXX not implemented */
208 	return (0);
209 }
210 
211 int
212 reloc_iresolve_nonplt(Obj_Entry *obj __unused,
213     struct Struct_RtldLockState *lockstate __unused)
214 {
215 
216 	/* XXX not implemented */
217 	return (0);
218 }
219 
220 int
221 reloc_gnu_ifunc(Obj_Entry *obj __unused, int flags __unused,
222    struct Struct_RtldLockState *lockstate __unused)
223 {
224 
225 	/* XXX not implemented */
226 	return (0);
227 }
228 
229 Elf_Addr
230 reloc_jmpslot(Elf_Addr *where, Elf_Addr target,
231     const Obj_Entry *defobj __unused, const Obj_Entry *obj __unused,
232     const Elf_Rel *rel)
233 {
234 
235 	assert(ELF_R_TYPE(rel->r_info) == R_RISCV_JUMP_SLOT);
236 
237 	if (*where != target && !ld_bind_not)
238 		*where = target;
239 	return (target);
240 }
241 
242 /*
243  * Process non-PLT relocations
244  */
245 int
246 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
247     RtldLockState *lockstate)
248 {
249 	const Obj_Entry *defobj;
250 	const Elf_Rela *relalim;
251 	const Elf_Rela *rela;
252 	const Elf_Sym *def;
253 	SymCache *cache;
254 	Elf_Addr *where;
255 	unsigned long symnum;
256 
257 	if ((flags & SYMLOOK_IFUNC) != 0)
258 		/* XXX not implemented */
259 		return (0);
260 
261 	/*
262 	 * The dynamic loader may be called from a thread, we have
263 	 * limited amounts of stack available so we cannot use alloca().
264 	 */
265 	if (obj == obj_rtld)
266 		cache = NULL;
267 	else
268 		cache = calloc(obj->dynsymcount, sizeof(SymCache));
269 		/* No need to check for NULL here */
270 
271 	relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize);
272 	for (rela = obj->rela; rela < relalim; rela++) {
273 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
274 		symnum = ELF_R_SYM(rela->r_info);
275 
276 		switch (ELF_R_TYPE(rela->r_info)) {
277 		case R_RISCV_JUMP_SLOT:
278 			/* This will be handled by the plt/jmpslot routines */
279 			break;
280 		case R_RISCV_NONE:
281 			break;
282 		case R_RISCV_64:
283 			def = find_symdef(symnum, obj, &defobj, flags, cache,
284 			    lockstate);
285 			if (def == NULL)
286 				return (-1);
287 
288 			*where = (Elf_Addr)(defobj->relocbase + def->st_value +
289 			    rela->r_addend);
290 			break;
291 		case R_RISCV_TLS_DTPMOD64:
292 			def = find_symdef(symnum, obj, &defobj, flags, cache,
293 			    lockstate);
294 			if (def == NULL)
295 				return -1;
296 
297 			*where += (Elf_Addr)defobj->tlsindex;
298 			break;
299 		case R_RISCV_COPY:
300 			/*
301 			 * These are deferred until all other relocations have
302 			 * been done. All we do here is make sure that the
303 			 * COPY relocation is not in a shared library. They
304 			 * are allowed only in executable files.
305 			 */
306 			if (!obj->mainprog) {
307 				_rtld_error("%s: Unexpected R_RISCV_COPY "
308 				    "relocation in shared library", obj->path);
309 				return (-1);
310 			}
311 			break;
312 		case R_RISCV_TLS_DTPREL64:
313 			def = find_symdef(symnum, obj, &defobj, flags, cache,
314 			    lockstate);
315 			if (def == NULL)
316 				return (-1);
317 			/*
318 			 * We lazily allocate offsets for static TLS as we
319 			 * see the first relocation that references the
320 			 * TLS block. This allows us to support (small
321 			 * amounts of) static TLS in dynamically loaded
322 			 * modules. If we run out of space, we generate an
323 			 * error.
324 			 */
325 			if (!defobj->tls_static) {
326 				if (!allocate_tls_offset(
327 				    __DECONST(Obj_Entry *, defobj))) {
328 					_rtld_error(
329 					    "%s: No space available for static "
330 					    "Thread Local Storage", obj->path);
331 					return (-1);
332 				}
333 			}
334 
335 			*where += (Elf_Addr)(def->st_value + rela->r_addend
336 			    - TLS_DTV_OFFSET);
337 			break;
338 		case R_RISCV_TLS_TPREL64:
339 			def = find_symdef(symnum, obj, &defobj, flags, cache,
340 			    lockstate);
341 			if (def == NULL)
342 				return (-1);
343 
344 			/*
345 			 * We lazily allocate offsets for static TLS as we
346 			 * see the first relocation that references the
347 			 * TLS block. This allows us to support (small
348 			 * amounts of) static TLS in dynamically loaded
349 			 * modules. If we run out of space, we generate an
350 			 * error.
351 			 */
352 			if (!defobj->tls_static) {
353 				if (!allocate_tls_offset(
354 				    __DECONST(Obj_Entry *, defobj))) {
355 					_rtld_error(
356 					    "%s: No space available for static "
357 					    "Thread Local Storage", obj->path);
358 					return (-1);
359 				}
360 			}
361 
362 			*where = (def->st_value + rela->r_addend +
363 			    defobj->tlsoffset - TLS_TP_OFFSET - TLS_TCB_SIZE);
364 			break;
365 		case R_RISCV_RELATIVE:
366 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
367 			break;
368 		default:
369 			rtld_printf("%s: Unhandled relocation %lu\n",
370 			    obj->path, ELF_R_TYPE(rela->r_info));
371 			return (-1);
372 		}
373 	}
374 
375 	return (0);
376 }
377 
378 void
379 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
380 {
381 
382 }
383 
384 void
385 allocate_initial_tls(Obj_Entry *objs)
386 {
387 
388 	/*
389 	 * Fix the size of the static TLS block by using the maximum
390 	 * offset allocated so far and adding a bit for dynamic modules to
391 	 * use.
392 	 */
393 	tls_static_space = tls_last_offset + tls_last_size +
394 	    ld_static_tls_extra;
395 
396 	_tcb_set(allocate_tls(objs, NULL, TLS_TCB_SIZE, TLS_TCB_ALIGN));
397 }
398 
399 void *
400 __tls_get_addr(tls_index* ti)
401 {
402 	uintptr_t **dtvp;
403 	void *p;
404 
405 	dtvp = &_tcb_get()->tcb_dtv;
406 	p = tls_get_addr_common(dtvp, ti->ti_module, ti->ti_offset);
407 
408 	return ((char*)p + TLS_DTV_OFFSET);
409 }
410