xref: /openbmc/linux/arch/arm/kernel/elf.c (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/export.h>
3 #include <linux/sched.h>
4 #include <linux/personality.h>
5 #include <linux/binfmts.h>
6 #include <linux/elf.h>
7 #include <asm/system_info.h>
8 
9 int elf_check_arch(const struct elf32_hdr *x)
10 {
11 	unsigned int eflags;
12 
13 	/* Make sure it's an ARM executable */
14 	if (x->e_machine != EM_ARM)
15 		return 0;
16 
17 	/* Make sure the entry address is reasonable */
18 	if (x->e_entry & 1) {
19 		if (!(elf_hwcap & HWCAP_THUMB))
20 			return 0;
21 	} else if (x->e_entry & 3)
22 		return 0;
23 
24 	eflags = x->e_flags;
25 	if ((eflags & EF_ARM_EABI_MASK) == EF_ARM_EABI_UNKNOWN) {
26 		unsigned int flt_fmt;
27 
28 		/* APCS26 is only allowed if the CPU supports it */
29 		if ((eflags & EF_ARM_APCS_26) && !(elf_hwcap & HWCAP_26BIT))
30 			return 0;
31 
32 		flt_fmt = eflags & (EF_ARM_VFP_FLOAT | EF_ARM_SOFT_FLOAT);
33 
34 		/* VFP requires the supporting code */
35 		if (flt_fmt == EF_ARM_VFP_FLOAT && !(elf_hwcap & HWCAP_VFP))
36 			return 0;
37 	}
38 	return 1;
39 }
40 EXPORT_SYMBOL(elf_check_arch);
41 
42 void elf_set_personality(const struct elf32_hdr *x)
43 {
44 	unsigned int eflags = x->e_flags;
45 	unsigned int personality = current->personality & ~PER_MASK;
46 
47 	/*
48 	 * We only support Linux ELF executables, so always set the
49 	 * personality to LINUX.
50 	 */
51 	personality |= PER_LINUX;
52 
53 	/*
54 	 * APCS-26 is only valid for OABI executables
55 	 */
56 	if ((eflags & EF_ARM_EABI_MASK) == EF_ARM_EABI_UNKNOWN &&
57 	    (eflags & EF_ARM_APCS_26))
58 		personality &= ~ADDR_LIMIT_32BIT;
59 	else
60 		personality |= ADDR_LIMIT_32BIT;
61 
62 	set_personality(personality);
63 
64 	/*
65 	 * Since the FPA coprocessor uses CP1 and CP2, and iWMMXt uses CP0
66 	 * and CP1, we only enable access to the iWMMXt coprocessor if the
67 	 * binary is EABI or softfloat (and thus, guaranteed not to use
68 	 * FPA instructions.)
69 	 */
70 	if (elf_hwcap & HWCAP_IWMMXT &&
71 	    eflags & (EF_ARM_EABI_MASK | EF_ARM_SOFT_FLOAT)) {
72 		set_thread_flag(TIF_USING_IWMMXT);
73 	} else {
74 		clear_thread_flag(TIF_USING_IWMMXT);
75 	}
76 }
77 EXPORT_SYMBOL(elf_set_personality);
78 
79 /*
80  * Set READ_IMPLIES_EXEC if:
81  *  - the binary requires an executable stack
82  *  - we're running on a CPU which doesn't support NX.
83  */
84 int arm_elf_read_implies_exec(const struct elf32_hdr *x, int executable_stack)
85 {
86 	if (executable_stack != EXSTACK_DISABLE_X)
87 		return 1;
88 	if (cpu_architecture() < CPU_ARCH_ARMv6)
89 		return 1;
90 	return 0;
91 }
92 EXPORT_SYMBOL(arm_elf_read_implies_exec);
93