1292691Sbr/*-
2292691Sbr * Copyright (c) 1999, 2000 John D. Polstra.
3292691Sbr * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
4292691Sbr * All rights reserved.
5292691Sbr *
6292691Sbr * Portions of this software were developed by SRI International and the
7292691Sbr * University of Cambridge Computer Laboratory under DARPA/AFRL contract
8292691Sbr * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9292691Sbr *
10292691Sbr * Portions of this software were developed by the University of Cambridge
11292691Sbr * Computer Laboratory as part of the CTSRD Project, with support from the
12292691Sbr * UK Higher Education Innovation Fund (HEIF).
13292691Sbr *
14292691Sbr * Redistribution and use in source and binary forms, with or without
15292691Sbr * modification, are permitted provided that the following conditions
16292691Sbr * are met:
17292691Sbr * 1. Redistributions of source code must retain the above copyright
18292691Sbr *    notice, this list of conditions and the following disclaimer.
19292691Sbr * 2. Redistributions in binary form must reproduce the above copyright
20292691Sbr *    notice, this list of conditions and the following disclaimer in the
21292691Sbr *    documentation and/or other materials provided with the distribution.
22292691Sbr *
23292691Sbr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24292691Sbr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25292691Sbr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26292691Sbr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27292691Sbr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28292691Sbr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29292691Sbr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30292691Sbr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31292691Sbr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32292691Sbr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33292691Sbr * SUCH DAMAGE.
34292691Sbr *
35292691Sbr * $FreeBSD: releng/11.0/libexec/rtld-elf/riscv/rtld_machdep.h 294623 2016-01-23 11:46:52Z br $
36292691Sbr */
37292691Sbr
38292691Sbr#ifndef RTLD_MACHDEP_H
39292691Sbr#define RTLD_MACHDEP_H	1
40292691Sbr
41292691Sbr#include <sys/types.h>
42292691Sbr#include <machine/atomic.h>
43292691Sbr
44292691Sbrstruct Struct_Obj_Entry;
45292691Sbr
46292691Sbruint64_t set_gp(struct Struct_Obj_Entry *obj);
47292691Sbr
48292691Sbr/* Return the address of the .dynamic section in the dynamic linker. */
49292691Sbr#define rtld_dynamic(obj)                                               \
50292691Sbr({                                                                      \
51292691Sbr	Elf_Addr _dynamic_addr;                                         \
52292691Sbr	__asm __volatile("lla       %0, _DYNAMIC" : "=r"(_dynamic_addr));   \
53292691Sbr	(const Elf_Dyn *)_dynamic_addr;                                 \
54292691Sbr})
55292691Sbr#define RTLD_IS_DYNAMIC() (1)
56292691Sbr
57292691SbrElf_Addr reloc_jmpslot(Elf_Addr *where, Elf_Addr target,
58292691Sbr		       const struct Struct_Obj_Entry *defobj,
59292691Sbr		       const struct Struct_Obj_Entry *obj,
60292691Sbr		       const Elf_Rel *rel);
61292691Sbr
62292691Sbr#define make_function_pointer(def, defobj) \
63292691Sbr	((defobj)->relocbase + (def)->st_value)
64292691Sbr
65292691Sbr#define call_initfini_pointer(obj, target)				\
66292691Sbr({									\
67292691Sbr	uint64_t old0;							\
68292691Sbr	old0 = set_gp(obj);						\
69292691Sbr	(((InitFunc)(target))());					\
70292691Sbr	__asm __volatile("mv    gp, %0" :: "r"(old0));			\
71292691Sbr})
72292691Sbr
73292691Sbr#define call_init_pointer(obj, target)					\
74292691Sbr({									\
75292691Sbr	uint64_t old1;							\
76292691Sbr	old1 = set_gp(obj);						\
77292691Sbr	(((InitArrFunc)(target))(main_argc, main_argv, environ));	\
78292691Sbr	__asm __volatile("mv    gp, %0" :: "r"(old1));			\
79292691Sbr})
80292691Sbr
81292691Sbr/*
82292691Sbr * Lazy binding entry point, called via PLT.
83292691Sbr */
84292691Sbrvoid _rtld_bind_start(void);
85292691Sbr
86292691Sbr/*
87292691Sbr * TLS
88292691Sbr */
89292691Sbr#define	TLS_TP_OFFSET	0x0
90292691Sbr#define	TLS_DTV_OFFSET	0x800
91292691Sbr#define	TLS_TCB_SIZE	16
92292691Sbr
93292691Sbr#define round(size, align) \
94292691Sbr    (((size) + (align) - 1) & ~((align) - 1))
95292691Sbr#define calculate_first_tls_offset(size, align) \
96292691Sbr    round(16, align)
97292691Sbr#define calculate_tls_offset(prev_offset, prev_size, size, align) \
98292691Sbr    round(prev_offset + prev_size, align)
99292691Sbr#define calculate_tls_end(off, size)    ((off) + (size))
100292691Sbr
101292691Sbrtypedef struct {
102292691Sbr	unsigned long ti_module;
103292691Sbr	unsigned long ti_offset;
104292691Sbr} tls_index;
105292691Sbr
106292691Sbrextern void *__tls_get_addr(tls_index* ti);
107292691Sbr
108292691Sbr#define	RTLD_DEFAULT_STACK_PF_EXEC	PF_X
109292691Sbr#define	RTLD_DEFAULT_STACK_EXEC		PROT_EXEC
110292691Sbr
111294623Sbr#define	md_abi_variant_hook(x)
112294623Sbr
113292691Sbr#endif
114