blob: 9b05c6a0dceac54615e1dfa472bf28ca946616ac [file] [log] [blame]
Russell King8ec53662008-09-07 17:16:54 +01001#include <linux/module.h>
2#include <linux/sched.h>
3#include <linux/personality.h>
4#include <linux/binfmts.h>
5#include <linux/elf.h>
6
7int elf_check_arch(const struct elf32_hdr *x)
8{
9 unsigned int eflags;
10
11 /* Make sure it's an ARM executable */
12 if (x->e_machine != EM_ARM)
13 return 0;
14
15 /* Make sure the entry address is reasonable */
16 if (x->e_entry & 1) {
17 if (!(elf_hwcap & HWCAP_THUMB))
18 return 0;
19 } else if (x->e_entry & 3)
20 return 0;
21
22 eflags = x->e_flags;
23 if ((eflags & EF_ARM_EABI_MASK) == EF_ARM_EABI_UNKNOWN) {
Russell Kingd2ed5cb2008-11-02 09:16:50 +000024 unsigned int flt_fmt;
25
Russell King8ec53662008-09-07 17:16:54 +010026 /* APCS26 is only allowed if the CPU supports it */
27 if ((eflags & EF_ARM_APCS_26) && !(elf_hwcap & HWCAP_26BIT))
28 return 0;
29
Russell Kingd2ed5cb2008-11-02 09:16:50 +000030 flt_fmt = eflags & (EF_ARM_VFP_FLOAT | EF_ARM_SOFT_FLOAT);
31
Russell King8ec53662008-09-07 17:16:54 +010032 /* VFP requires the supporting code */
Russell Kingd2ed5cb2008-11-02 09:16:50 +000033 if (flt_fmt == EF_ARM_VFP_FLOAT && !(elf_hwcap & HWCAP_VFP))
Russell King8ec53662008-09-07 17:16:54 +010034 return 0;
35 }
36 return 1;
37}
38EXPORT_SYMBOL(elf_check_arch);
39
40void elf_set_personality(const struct elf32_hdr *x)
41{
42 unsigned int eflags = x->e_flags;
Nicolas Pitre5e143432011-04-13 04:59:36 +010043 unsigned int personality = current->personality & ~PER_MASK;
44
45 /*
46 * We only support Linux ELF executables, so always set the
47 * personality to LINUX.
48 */
49 personality |= PER_LINUX;
Russell King8ec53662008-09-07 17:16:54 +010050
51 /*
52 * APCS-26 is only valid for OABI executables
53 */
Nicolas Pitre5e143432011-04-13 04:59:36 +010054 if ((eflags & EF_ARM_EABI_MASK) == EF_ARM_EABI_UNKNOWN &&
55 (eflags & EF_ARM_APCS_26))
56 personality &= ~ADDR_LIMIT_32BIT;
57 else
58 personality |= ADDR_LIMIT_32BIT;
Russell King8ec53662008-09-07 17:16:54 +010059
60 set_personality(personality);
61
62 /*
63 * Since the FPA coprocessor uses CP1 and CP2, and iWMMXt uses CP0
64 * and CP1, we only enable access to the iWMMXt coprocessor if the
65 * binary is EABI or softfloat (and thus, guaranteed not to use
66 * FPA instructions.)
67 */
68 if (elf_hwcap & HWCAP_IWMMXT &&
69 eflags & (EF_ARM_EABI_MASK | EF_ARM_SOFT_FLOAT)) {
70 set_thread_flag(TIF_USING_IWMMXT);
71 } else {
72 clear_thread_flag(TIF_USING_IWMMXT);
73 }
74}
75EXPORT_SYMBOL(elf_set_personality);
76
77/*
78 * Set READ_IMPLIES_EXEC if:
79 * - the binary requires an executable stack
80 * - we're running on a CPU which doesn't support NX.
81 */
82int arm_elf_read_implies_exec(const struct elf32_hdr *x, int executable_stack)
83{
Makito SHIOKAWA9da616f2009-02-19 15:34:59 +010084 if (executable_stack != EXSTACK_DISABLE_X)
Russell King8ec53662008-09-07 17:16:54 +010085 return 1;
Makito SHIOKAWA9da616f2009-02-19 15:34:59 +010086 if (cpu_architecture() < CPU_ARCH_ARMv6)
Russell King8ec53662008-09-07 17:16:54 +010087 return 1;
88 return 0;
Russell King8ec53662008-09-07 17:16:54 +010089}
90EXPORT_SYMBOL(arm_elf_read_implies_exec);