xref: /netbsd-src/sys/arch/riscv/include/elf_machdep.h (revision 0ad349337a874f932507463a034e8710784222b7)
1*0ad34933Sskrll /* $NetBSD: elf_machdep.h,v 1.10 2024/08/04 08:16:25 skrll Exp $ */
26cf6fe02Smatt 
36cf6fe02Smatt /*-
46cf6fe02Smatt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
56cf6fe02Smatt  * All rights reserved.
66cf6fe02Smatt  *
76cf6fe02Smatt  * This code is derived from software contributed to The NetBSD Foundation
86cf6fe02Smatt  * by Matt Thomas of 3am Software Foundry.
96cf6fe02Smatt  *
106cf6fe02Smatt  * Redistribution and use in source and binary forms, with or without
116cf6fe02Smatt  * modification, are permitted provided that the following conditions
126cf6fe02Smatt  * are met:
136cf6fe02Smatt  * 1. Redistributions of source code must retain the above copyright
146cf6fe02Smatt  *    notice, this list of conditions and the following disclaimer.
156cf6fe02Smatt  * 2. Redistributions in binary form must reproduce the above copyright
166cf6fe02Smatt  *    notice, this list of conditions and the following disclaimer in the
176cf6fe02Smatt  *    documentation and/or other materials provided with the distribution.
186cf6fe02Smatt  *
196cf6fe02Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206cf6fe02Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216cf6fe02Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226cf6fe02Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236cf6fe02Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246cf6fe02Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256cf6fe02Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266cf6fe02Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276cf6fe02Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286cf6fe02Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296cf6fe02Smatt  * POSSIBILITY OF SUCH DAMAGE.
306cf6fe02Smatt  */
316cf6fe02Smatt 
326cf6fe02Smatt #ifndef _RISCV_ELF_MACHDEP_H_
336cf6fe02Smatt #define _RISCV_ELF_MACHDEP_H_
346cf6fe02Smatt 
356cf6fe02Smatt #define	ELF32_MACHDEP_ID		EM_RISCV
368501aed0Smatt #define	ELF64_MACHDEP_ID		EM_RISCV
376cf6fe02Smatt 
386cf6fe02Smatt #define ELF32_MACHDEP_ENDIANNESS	ELFDATA2LSB
396cf6fe02Smatt #define ELF64_MACHDEP_ENDIANNESS	ELFDATA2LSB
406cf6fe02Smatt 
416cf6fe02Smatt #define ELF32_MACHDEP_ID_CASES		\
426cf6fe02Smatt 		case EM_RISCV:		\
436cf6fe02Smatt 			break;
446cf6fe02Smatt 
456cf6fe02Smatt #define	ELF64_MACHDEP_ID_CASES		\
466cf6fe02Smatt 		case EM_RISCV:		\
476cf6fe02Smatt 			break;
486cf6fe02Smatt 
496cf6fe02Smatt #ifdef _LP64
505bd7eba2Smaxv #define	KERN_ELFSIZE		64
516cf6fe02Smatt #define ARCH_ELFSIZE		64	/* MD native binary size */
526cf6fe02Smatt #else
535bd7eba2Smaxv #define	KERN_ELFSIZE		32
546cf6fe02Smatt #define ARCH_ELFSIZE		32	/* MD native binary size */
556cf6fe02Smatt #endif
566cf6fe02Smatt 
576cf6fe02Smatt /* Processor specific flags for the ELF header e_flags field.  */
586cf6fe02Smatt 
596cf6fe02Smatt /* Processor specific relocation types */
606cf6fe02Smatt 
616cf6fe02Smatt #define R_RISCV_NONE		0
629d6a1448Smatt #define R_RISCV_32		1	// A
639d6a1448Smatt #define R_RISCV_64		2
649d6a1448Smatt #define R_RISCV_RELATIVE	3
659d6a1448Smatt #define R_RISCV_COPY		4
669d6a1448Smatt #define R_RISCV_JMP_SLOT	5
679d6a1448Smatt #define R_RISCV_TLS_DTPMOD32	6
6854081762Sskrll #define R_RISCV_TLS_DTPMOD64	7
6954081762Sskrll #define R_RISCV_TLS_DTPREL32	8
709d6a1448Smatt #define R_RISCV_TLS_DTPREL64	9
719d6a1448Smatt #define R_RISCV_TLS_TPREL32	10
72f78f67fdSmatt #define R_RISCV_TLS_TPREL64	11
736cf6fe02Smatt 
749d6a1448Smatt /* The rest are not used by the dynamic linker */
759d6a1448Smatt #define R_RISCV_BRANCH		16	// (A - P) & 0xffff
769d6a1448Smatt #define R_RISCV_JAL		17	// A & 0xff
779d6a1448Smatt #define R_RISCV_CALL		18	// (A - P) & 0xff
789d6a1448Smatt #define R_RISCV_CALL_PLT	19
799d6a1448Smatt #define R_RISCV_GOT_HI20	20
809d6a1448Smatt #define R_RISCV_TLS_GOT_HI20	21
819d6a1448Smatt #define R_RISCV_TLS_GD_HI20	22
829d6a1448Smatt #define R_RISCV_PCREL_HI20	23
839d6a1448Smatt #define R_RISCV_PCREL_LO12_I	24
849d6a1448Smatt #define R_RISCV_PCREL_LO12_S	25
859d6a1448Smatt #define R_RISCV_HI20		26	// A & 0xffff
869d6a1448Smatt #define R_RISCV_LO12_I		27	// (A >> 16) & 0xffff
879d6a1448Smatt #define R_RISCV_LO12_S		28	// (S + A - P) >> 2
889d6a1448Smatt #define R_RISCV_TPREL_HI20	29
8954081762Sskrll #define R_RISCV_TPREL_LO12_I	30
9054081762Sskrll #define R_RISCV_TPREL_LO12_S	31
9154081762Sskrll #define R_RISCV_TPREL_ADD	32
929d6a1448Smatt #define R_RISCV_ADD8		33
939d6a1448Smatt #define R_RISCV_ADD16		34
949d6a1448Smatt #define R_RISCV_ADD32		35
959d6a1448Smatt #define R_RISCV_ADD64		36
969d6a1448Smatt #define R_RISCV_SUB8		37
979d6a1448Smatt #define R_RISCV_SUB16		38
989d6a1448Smatt #define R_RISCV_SUB32		39
999d6a1448Smatt #define R_RISCV_SUB64		40
1009d6a1448Smatt #define R_RISCV_GNU_VTINHERIT	41	// A & 0xffff
1019d6a1448Smatt #define R_RISCV_GNU_VTENTRY	42
1029d6a1448Smatt #define R_RISCV_ALIGN		43
103e4264474Smatt #define R_RISCV_RVC_BRANCH	44
104e4264474Smatt #define R_RISCV_RVC_JUMP	45
10554081762Sskrll #define R_RISCV_RVC_LUI		46
10654081762Sskrll #define R_RISCV_GPREL_I		47
10754081762Sskrll #define R_RISCV_GPREL_S		48
10854081762Sskrll #define R_RISCV_TPREL_I		49
10954081762Sskrll #define R_RISCV_TPREL_S		50
11054081762Sskrll #define R_RISCV_RELAX		51
11154081762Sskrll #define R_RISCV_SUB6		52
11254081762Sskrll #define R_RISCV_SET6		53
11354081762Sskrll #define R_RISCV_SET8		54
11454081762Sskrll #define R_RISCV_SET16		55
11554081762Sskrll #define R_RISCV_SET32		56
11654081762Sskrll #define R_RISCV_32_PCREL	57
1176cf6fe02Smatt 
118f78f67fdSmatt /* These are aliases we can use R_TYPESZ */
119f78f67fdSmatt #define R_RISCV_ADDR32		R_RISCV_32
120f78f67fdSmatt #define R_RISCV_ADDR64		R_RISCV_64
1216cf6fe02Smatt 
1226cf6fe02Smatt #define R_TYPE(name)		R_RISCV_ ## name
1239d6a1448Smatt #if ELFSIZE == 32
124f78f67fdSmatt #define R_TYPESZ(name)		R_RISCV_ ## name ## 32
1259d6a1448Smatt #else
126f78f67fdSmatt #define R_TYPESZ(name)		R_RISCV_ ## name ## 64
1279d6a1448Smatt #endif
1286cf6fe02Smatt 
1296cf6fe02Smatt #ifdef _KERNEL
1306cf6fe02Smatt #ifdef ELFSIZE
1316cf6fe02Smatt #define ELF_MD_PROBE_FUNC	ELFNAME2(cpu_netbsd,probe)
1326cf6fe02Smatt #endif
1336cf6fe02Smatt 
1346cf6fe02Smatt struct exec_package;
1356cf6fe02Smatt 
1366cf6fe02Smatt int cpu_netbsd_elf32_probe(struct lwp *, struct exec_package *, void *, char *,
1376cf6fe02Smatt         vaddr_t *);
1386cf6fe02Smatt 
1396cf6fe02Smatt int cpu_netbsd_elf64_probe(struct lwp *, struct exec_package *, void *, char *,
1406cf6fe02Smatt         vaddr_t *);
1416cf6fe02Smatt 
1426cf6fe02Smatt #endif /* _KERNEL */
1436cf6fe02Smatt 
1446cf6fe02Smatt #endif /* _RISCV_ELF_MACHDEP_H_ */
145