xref: /kernel/linux/linux-6.6/arch/arm64/include/asm/elf.h (revision 62306a36)
162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2012 ARM Ltd.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci#ifndef __ASM_ELF_H
662306a36Sopenharmony_ci#define __ASM_ELF_H
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <asm/hwcap.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci/*
1162306a36Sopenharmony_ci * ELF register definitions..
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci#include <asm/ptrace.h>
1462306a36Sopenharmony_ci#include <asm/user.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * AArch64 static relocation types.
1862306a36Sopenharmony_ci */
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* Miscellaneous. */
2162306a36Sopenharmony_ci#define R_ARM_NONE			0
2262306a36Sopenharmony_ci#define R_AARCH64_NONE			256
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/* Data. */
2562306a36Sopenharmony_ci#define R_AARCH64_ABS64			257
2662306a36Sopenharmony_ci#define R_AARCH64_ABS32			258
2762306a36Sopenharmony_ci#define R_AARCH64_ABS16			259
2862306a36Sopenharmony_ci#define R_AARCH64_PREL64		260
2962306a36Sopenharmony_ci#define R_AARCH64_PREL32		261
3062306a36Sopenharmony_ci#define R_AARCH64_PREL16		262
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/* Instructions. */
3362306a36Sopenharmony_ci#define R_AARCH64_MOVW_UABS_G0		263
3462306a36Sopenharmony_ci#define R_AARCH64_MOVW_UABS_G0_NC	264
3562306a36Sopenharmony_ci#define R_AARCH64_MOVW_UABS_G1		265
3662306a36Sopenharmony_ci#define R_AARCH64_MOVW_UABS_G1_NC	266
3762306a36Sopenharmony_ci#define R_AARCH64_MOVW_UABS_G2		267
3862306a36Sopenharmony_ci#define R_AARCH64_MOVW_UABS_G2_NC	268
3962306a36Sopenharmony_ci#define R_AARCH64_MOVW_UABS_G3		269
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define R_AARCH64_MOVW_SABS_G0		270
4262306a36Sopenharmony_ci#define R_AARCH64_MOVW_SABS_G1		271
4362306a36Sopenharmony_ci#define R_AARCH64_MOVW_SABS_G2		272
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#define R_AARCH64_LD_PREL_LO19		273
4662306a36Sopenharmony_ci#define R_AARCH64_ADR_PREL_LO21		274
4762306a36Sopenharmony_ci#define R_AARCH64_ADR_PREL_PG_HI21	275
4862306a36Sopenharmony_ci#define R_AARCH64_ADR_PREL_PG_HI21_NC	276
4962306a36Sopenharmony_ci#define R_AARCH64_ADD_ABS_LO12_NC	277
5062306a36Sopenharmony_ci#define R_AARCH64_LDST8_ABS_LO12_NC	278
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#define R_AARCH64_TSTBR14		279
5362306a36Sopenharmony_ci#define R_AARCH64_CONDBR19		280
5462306a36Sopenharmony_ci#define R_AARCH64_JUMP26		282
5562306a36Sopenharmony_ci#define R_AARCH64_CALL26		283
5662306a36Sopenharmony_ci#define R_AARCH64_LDST16_ABS_LO12_NC	284
5762306a36Sopenharmony_ci#define R_AARCH64_LDST32_ABS_LO12_NC	285
5862306a36Sopenharmony_ci#define R_AARCH64_LDST64_ABS_LO12_NC	286
5962306a36Sopenharmony_ci#define R_AARCH64_LDST128_ABS_LO12_NC	299
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define R_AARCH64_MOVW_PREL_G0		287
6262306a36Sopenharmony_ci#define R_AARCH64_MOVW_PREL_G0_NC	288
6362306a36Sopenharmony_ci#define R_AARCH64_MOVW_PREL_G1		289
6462306a36Sopenharmony_ci#define R_AARCH64_MOVW_PREL_G1_NC	290
6562306a36Sopenharmony_ci#define R_AARCH64_MOVW_PREL_G2		291
6662306a36Sopenharmony_ci#define R_AARCH64_MOVW_PREL_G2_NC	292
6762306a36Sopenharmony_ci#define R_AARCH64_MOVW_PREL_G3		293
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci#define R_AARCH64_RELATIVE		1027
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/*
7262306a36Sopenharmony_ci * These are used to set parameters in the core dumps.
7362306a36Sopenharmony_ci */
7462306a36Sopenharmony_ci#define ELF_CLASS	ELFCLASS64
7562306a36Sopenharmony_ci#ifdef __AARCH64EB__
7662306a36Sopenharmony_ci#define ELF_DATA	ELFDATA2MSB
7762306a36Sopenharmony_ci#else
7862306a36Sopenharmony_ci#define ELF_DATA	ELFDATA2LSB
7962306a36Sopenharmony_ci#endif
8062306a36Sopenharmony_ci#define ELF_ARCH	EM_AARCH64
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci/*
8362306a36Sopenharmony_ci * This yields a string that ld.so will use to load implementation
8462306a36Sopenharmony_ci * specific libraries for optimization.  This is more specific in
8562306a36Sopenharmony_ci * intent than poking at uname or /proc/cpuinfo.
8662306a36Sopenharmony_ci */
8762306a36Sopenharmony_ci#define ELF_PLATFORM_SIZE	16
8862306a36Sopenharmony_ci#ifdef __AARCH64EB__
8962306a36Sopenharmony_ci#define ELF_PLATFORM		("aarch64_be")
9062306a36Sopenharmony_ci#else
9162306a36Sopenharmony_ci#define ELF_PLATFORM		("aarch64")
9262306a36Sopenharmony_ci#endif
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/*
9562306a36Sopenharmony_ci * This is used to ensure we don't load something for the wrong architecture.
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_ci#define elf_check_arch(x)		((x)->e_machine == EM_AARCH64)
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci/*
10062306a36Sopenharmony_ci * An executable for which elf_read_implies_exec() returns TRUE will
10162306a36Sopenharmony_ci * have the READ_IMPLIES_EXEC personality flag set automatically.
10262306a36Sopenharmony_ci *
10362306a36Sopenharmony_ci * The decision process for determining the results are:
10462306a36Sopenharmony_ci *
10562306a36Sopenharmony_ci *                CPU*: | arm32      | arm64      |
10662306a36Sopenharmony_ci * ELF:                 |            |            |
10762306a36Sopenharmony_ci * ---------------------|------------|------------|
10862306a36Sopenharmony_ci * missing PT_GNU_STACK | exec-all   | exec-none  |
10962306a36Sopenharmony_ci * PT_GNU_STACK == RWX  | exec-stack | exec-stack |
11062306a36Sopenharmony_ci * PT_GNU_STACK == RW   | exec-none  | exec-none  |
11162306a36Sopenharmony_ci *
11262306a36Sopenharmony_ci *  exec-all  : all PROT_READ user mappings are executable, except when
11362306a36Sopenharmony_ci *              backed by files on a noexec-filesystem.
11462306a36Sopenharmony_ci *  exec-none : only PROT_EXEC user mappings are executable.
11562306a36Sopenharmony_ci *  exec-stack: only the stack and PROT_EXEC user mappings are executable.
11662306a36Sopenharmony_ci *
11762306a36Sopenharmony_ci *  *all arm64 CPUs support NX, so there is no "lacks NX" column.
11862306a36Sopenharmony_ci *
11962306a36Sopenharmony_ci */
12062306a36Sopenharmony_ci#define compat_elf_read_implies_exec(ex, stk)	(stk == EXSTACK_DEFAULT)
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci#define CORE_DUMP_USE_REGSET
12362306a36Sopenharmony_ci#define ELF_EXEC_PAGESIZE	PAGE_SIZE
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/*
12662306a36Sopenharmony_ci * This is the base location for PIE (ET_DYN with INTERP) loads. On
12762306a36Sopenharmony_ci * 64-bit, this is above 4GB to leave the entire 32-bit address
12862306a36Sopenharmony_ci * space open for things that want to use the area for 32-bit pointers.
12962306a36Sopenharmony_ci */
13062306a36Sopenharmony_ci#ifdef CONFIG_ARM64_FORCE_52BIT
13162306a36Sopenharmony_ci#define ELF_ET_DYN_BASE		(2 * TASK_SIZE_64 / 3)
13262306a36Sopenharmony_ci#else
13362306a36Sopenharmony_ci#define ELF_ET_DYN_BASE		(2 * DEFAULT_MAP_WINDOW_64 / 3)
13462306a36Sopenharmony_ci#endif /* CONFIG_ARM64_FORCE_52BIT */
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#ifndef __ASSEMBLY__
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci#include <uapi/linux/elf.h>
13962306a36Sopenharmony_ci#include <linux/bug.h>
14062306a36Sopenharmony_ci#include <linux/errno.h>
14162306a36Sopenharmony_ci#include <linux/fs.h>
14262306a36Sopenharmony_ci#include <linux/types.h>
14362306a36Sopenharmony_ci#include <asm/processor.h> /* for signal_minsigstksz, used by ARCH_DLINFO */
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_citypedef unsigned long elf_greg_t;
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci#define ELF_NGREG (sizeof(struct user_pt_regs) / sizeof(elf_greg_t))
14862306a36Sopenharmony_ci#define ELF_CORE_COPY_REGS(dest, regs)	\
14962306a36Sopenharmony_ci	*(struct user_pt_regs *)&(dest) = (regs)->user_regs;
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_citypedef elf_greg_t elf_gregset_t[ELF_NGREG];
15262306a36Sopenharmony_citypedef struct user_fpsimd_state elf_fpregset_t;
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci/*
15562306a36Sopenharmony_ci * When the program starts, a1 contains a pointer to a function to be
15662306a36Sopenharmony_ci * registered with atexit, as per the SVR4 ABI.  A value of 0 means we have no
15762306a36Sopenharmony_ci * such handler.
15862306a36Sopenharmony_ci */
15962306a36Sopenharmony_ci#define ELF_PLAT_INIT(_r, load_addr)	(_r)->regs[0] = 0
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci#define SET_PERSONALITY(ex)						\
16262306a36Sopenharmony_ci({									\
16362306a36Sopenharmony_ci	clear_thread_flag(TIF_32BIT);					\
16462306a36Sopenharmony_ci	current->personality &= ~READ_IMPLIES_EXEC;			\
16562306a36Sopenharmony_ci})
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes */
16862306a36Sopenharmony_ci#define ARCH_DLINFO							\
16962306a36Sopenharmony_cido {									\
17062306a36Sopenharmony_ci	NEW_AUX_ENT(AT_SYSINFO_EHDR,					\
17162306a36Sopenharmony_ci		    (elf_addr_t)current->mm->context.vdso);		\
17262306a36Sopenharmony_ci									\
17362306a36Sopenharmony_ci	/*								\
17462306a36Sopenharmony_ci	 * Should always be nonzero unless there's a kernel bug.	\
17562306a36Sopenharmony_ci	 * If we haven't determined a sensible value to give to		\
17662306a36Sopenharmony_ci	 * userspace, omit the entry:					\
17762306a36Sopenharmony_ci	 */								\
17862306a36Sopenharmony_ci	if (likely(signal_minsigstksz))					\
17962306a36Sopenharmony_ci		NEW_AUX_ENT(AT_MINSIGSTKSZ, signal_minsigstksz);	\
18062306a36Sopenharmony_ci	else								\
18162306a36Sopenharmony_ci		NEW_AUX_ENT(AT_IGNORE, 0);				\
18262306a36Sopenharmony_ci} while (0)
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci#define ARCH_HAS_SETUP_ADDITIONAL_PAGES
18562306a36Sopenharmony_cistruct linux_binprm;
18662306a36Sopenharmony_ciextern int arch_setup_additional_pages(struct linux_binprm *bprm,
18762306a36Sopenharmony_ci				       int uses_interp);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci/* 1GB of VA */
19062306a36Sopenharmony_ci#ifdef CONFIG_COMPAT
19162306a36Sopenharmony_ci#define STACK_RND_MASK			(test_thread_flag(TIF_32BIT) ? \
19262306a36Sopenharmony_ci						0x7ff >> (PAGE_SHIFT - 12) : \
19362306a36Sopenharmony_ci						0x3ffff >> (PAGE_SHIFT - 12))
19462306a36Sopenharmony_ci#else
19562306a36Sopenharmony_ci#define STACK_RND_MASK			(0x3ffff >> (PAGE_SHIFT - 12))
19662306a36Sopenharmony_ci#endif
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci#ifdef __AARCH64EB__
19962306a36Sopenharmony_ci#define COMPAT_ELF_PLATFORM		("v8b")
20062306a36Sopenharmony_ci#else
20162306a36Sopenharmony_ci#define COMPAT_ELF_PLATFORM		("v8l")
20262306a36Sopenharmony_ci#endif
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci#ifdef CONFIG_COMPAT
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci/* PIE load location for compat arm. Must match ARM ELF_ET_DYN_BASE. */
20762306a36Sopenharmony_ci#define COMPAT_ELF_ET_DYN_BASE		0x000400000UL
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci/* AArch32 registers. */
21062306a36Sopenharmony_ci#define COMPAT_ELF_NGREG		18
21162306a36Sopenharmony_citypedef unsigned int			compat_elf_greg_t;
21262306a36Sopenharmony_citypedef compat_elf_greg_t		compat_elf_gregset_t[COMPAT_ELF_NGREG];
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci/* AArch32 EABI. */
21562306a36Sopenharmony_ci#define EF_ARM_EABI_MASK		0xff000000
21662306a36Sopenharmony_ciint compat_elf_check_arch(const struct elf32_hdr *);
21762306a36Sopenharmony_ci#define compat_elf_check_arch		compat_elf_check_arch
21862306a36Sopenharmony_ci#define compat_start_thread		compat_start_thread
21962306a36Sopenharmony_ci/*
22062306a36Sopenharmony_ci * Unlike the native SET_PERSONALITY macro, the compat version maintains
22162306a36Sopenharmony_ci * READ_IMPLIES_EXEC across an execve() since this is the behaviour on
22262306a36Sopenharmony_ci * arch/arm/.
22362306a36Sopenharmony_ci */
22462306a36Sopenharmony_ci#define COMPAT_SET_PERSONALITY(ex)					\
22562306a36Sopenharmony_ci({									\
22662306a36Sopenharmony_ci	set_thread_flag(TIF_32BIT);					\
22762306a36Sopenharmony_ci })
22862306a36Sopenharmony_ci#ifdef CONFIG_COMPAT_VDSO
22962306a36Sopenharmony_ci#define COMPAT_ARCH_DLINFO						\
23062306a36Sopenharmony_cido {									\
23162306a36Sopenharmony_ci	/*								\
23262306a36Sopenharmony_ci	 * Note that we use Elf64_Off instead of elf_addr_t because	\
23362306a36Sopenharmony_ci	 * elf_addr_t in compat is defined as Elf32_Addr and casting	\
23462306a36Sopenharmony_ci	 * current->mm->context.vdso to it triggers a cast warning of	\
23562306a36Sopenharmony_ci	 * cast from pointer to integer of different size.		\
23662306a36Sopenharmony_ci	 */								\
23762306a36Sopenharmony_ci	NEW_AUX_ENT(AT_SYSINFO_EHDR,					\
23862306a36Sopenharmony_ci			(Elf64_Off)current->mm->context.vdso);		\
23962306a36Sopenharmony_ci} while (0)
24062306a36Sopenharmony_ci#else
24162306a36Sopenharmony_ci#define COMPAT_ARCH_DLINFO
24262306a36Sopenharmony_ci#endif
24362306a36Sopenharmony_ciextern int aarch32_setup_additional_pages(struct linux_binprm *bprm,
24462306a36Sopenharmony_ci					  int uses_interp);
24562306a36Sopenharmony_ci#define compat_arch_setup_additional_pages \
24662306a36Sopenharmony_ci					aarch32_setup_additional_pages
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci#endif /* CONFIG_COMPAT */
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_cistruct arch_elf_state {
25162306a36Sopenharmony_ci	int flags;
25262306a36Sopenharmony_ci};
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci#define ARM64_ELF_BTI		(1 << 0)
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci#define INIT_ARCH_ELF_STATE {			\
25762306a36Sopenharmony_ci	.flags = 0,				\
25862306a36Sopenharmony_ci}
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_cistatic inline int arch_parse_elf_property(u32 type, const void *data,
26162306a36Sopenharmony_ci					  size_t datasz, bool compat,
26262306a36Sopenharmony_ci					  struct arch_elf_state *arch)
26362306a36Sopenharmony_ci{
26462306a36Sopenharmony_ci	/* No known properties for AArch32 yet */
26562306a36Sopenharmony_ci	if (IS_ENABLED(CONFIG_COMPAT) && compat)
26662306a36Sopenharmony_ci		return 0;
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
26962306a36Sopenharmony_ci		const u32 *p = data;
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci		if (datasz != sizeof(*p))
27262306a36Sopenharmony_ci			return -ENOEXEC;
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci		if (system_supports_bti() &&
27562306a36Sopenharmony_ci		    (*p & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
27662306a36Sopenharmony_ci			arch->flags |= ARM64_ELF_BTI;
27762306a36Sopenharmony_ci	}
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci	return 0;
28062306a36Sopenharmony_ci}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_cistatic inline int arch_elf_pt_proc(void *ehdr, void *phdr,
28362306a36Sopenharmony_ci				   struct file *f, bool is_interp,
28462306a36Sopenharmony_ci				   struct arch_elf_state *state)
28562306a36Sopenharmony_ci{
28662306a36Sopenharmony_ci	return 0;
28762306a36Sopenharmony_ci}
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_cistatic inline int arch_check_elf(void *ehdr, bool has_interp,
29062306a36Sopenharmony_ci				 void *interp_ehdr,
29162306a36Sopenharmony_ci				 struct arch_elf_state *state)
29262306a36Sopenharmony_ci{
29362306a36Sopenharmony_ci	return 0;
29462306a36Sopenharmony_ci}
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci#endif /* !__ASSEMBLY__ */
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci#endif
299