Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Paul Gortmaker | ecea4ab | 2011-07-22 10:58:34 -0400 | [diff] [blame] | 2 | #include <linux/export.h> |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 3 | #include <linux/sched.h> |
| 4 | #include <linux/personality.h> |
| 5 | #include <linux/binfmts.h> |
| 6 | #include <linux/elf.h> |
Nicolas Pitre | 382e67a | 2017-08-11 00:53:39 -0400 | [diff] [blame] | 7 | #include <linux/elf-fdpic.h> |
David Howells | 9f97da7 | 2012-03-28 18:30:01 +0100 | [diff] [blame] | 8 | #include <asm/system_info.h> |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 9 | |
| 10 | int elf_check_arch(const struct elf32_hdr *x) |
| 11 | { |
| 12 | unsigned int eflags; |
| 13 | |
| 14 | /* Make sure it's an ARM executable */ |
| 15 | if (x->e_machine != EM_ARM) |
| 16 | return 0; |
| 17 | |
| 18 | /* Make sure the entry address is reasonable */ |
| 19 | if (x->e_entry & 1) { |
| 20 | if (!(elf_hwcap & HWCAP_THUMB)) |
| 21 | return 0; |
| 22 | } else if (x->e_entry & 3) |
| 23 | return 0; |
| 24 | |
| 25 | eflags = x->e_flags; |
| 26 | if ((eflags & EF_ARM_EABI_MASK) == EF_ARM_EABI_UNKNOWN) { |
Russell King | d2ed5cb | 2008-11-02 09:16:50 +0000 | [diff] [blame] | 27 | unsigned int flt_fmt; |
| 28 | |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 29 | /* APCS26 is only allowed if the CPU supports it */ |
| 30 | if ((eflags & EF_ARM_APCS_26) && !(elf_hwcap & HWCAP_26BIT)) |
| 31 | return 0; |
| 32 | |
Russell King | d2ed5cb | 2008-11-02 09:16:50 +0000 | [diff] [blame] | 33 | flt_fmt = eflags & (EF_ARM_VFP_FLOAT | EF_ARM_SOFT_FLOAT); |
| 34 | |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 35 | /* VFP requires the supporting code */ |
Russell King | d2ed5cb | 2008-11-02 09:16:50 +0000 | [diff] [blame] | 36 | if (flt_fmt == EF_ARM_VFP_FLOAT && !(elf_hwcap & HWCAP_VFP)) |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | return 1; |
| 40 | } |
| 41 | EXPORT_SYMBOL(elf_check_arch); |
| 42 | |
| 43 | void elf_set_personality(const struct elf32_hdr *x) |
| 44 | { |
| 45 | unsigned int eflags = x->e_flags; |
Nicolas Pitre | 5e14343 | 2011-04-13 04:59:36 +0100 | [diff] [blame] | 46 | unsigned int personality = current->personality & ~PER_MASK; |
| 47 | |
| 48 | /* |
| 49 | * We only support Linux ELF executables, so always set the |
| 50 | * personality to LINUX. |
| 51 | */ |
| 52 | personality |= PER_LINUX; |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * APCS-26 is only valid for OABI executables |
| 56 | */ |
Nicolas Pitre | 5e14343 | 2011-04-13 04:59:36 +0100 | [diff] [blame] | 57 | if ((eflags & EF_ARM_EABI_MASK) == EF_ARM_EABI_UNKNOWN && |
| 58 | (eflags & EF_ARM_APCS_26)) |
| 59 | personality &= ~ADDR_LIMIT_32BIT; |
| 60 | else |
| 61 | personality |= ADDR_LIMIT_32BIT; |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 62 | |
| 63 | set_personality(personality); |
| 64 | |
| 65 | /* |
| 66 | * Since the FPA coprocessor uses CP1 and CP2, and iWMMXt uses CP0 |
| 67 | * and CP1, we only enable access to the iWMMXt coprocessor if the |
| 68 | * binary is EABI or softfloat (and thus, guaranteed not to use |
| 69 | * FPA instructions.) |
| 70 | */ |
| 71 | if (elf_hwcap & HWCAP_IWMMXT && |
| 72 | eflags & (EF_ARM_EABI_MASK | EF_ARM_SOFT_FLOAT)) { |
| 73 | set_thread_flag(TIF_USING_IWMMXT); |
| 74 | } else { |
| 75 | clear_thread_flag(TIF_USING_IWMMXT); |
| 76 | } |
| 77 | } |
| 78 | EXPORT_SYMBOL(elf_set_personality); |
| 79 | |
| 80 | /* |
| 81 | * Set READ_IMPLIES_EXEC if: |
| 82 | * - the binary requires an executable stack |
| 83 | * - we're running on a CPU which doesn't support NX. |
| 84 | */ |
Nicolas Pitre | e71fd63 | 2017-07-22 00:48:09 -0400 | [diff] [blame] | 85 | int arm_elf_read_implies_exec(int executable_stack) |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 86 | { |
Makito SHIOKAWA | 9da616f | 2009-02-19 15:34:59 +0100 | [diff] [blame] | 87 | if (executable_stack != EXSTACK_DISABLE_X) |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 88 | return 1; |
Makito SHIOKAWA | 9da616f | 2009-02-19 15:34:59 +0100 | [diff] [blame] | 89 | if (cpu_architecture() < CPU_ARCH_ARMv6) |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 90 | return 1; |
| 91 | return 0; |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 92 | } |
| 93 | EXPORT_SYMBOL(arm_elf_read_implies_exec); |
Nicolas Pitre | 382e67a | 2017-08-11 00:53:39 -0400 | [diff] [blame] | 94 | |
| 95 | #if defined(CONFIG_MMU) && defined(CONFIG_BINFMT_ELF_FDPIC) |
| 96 | |
| 97 | void elf_fdpic_arch_lay_out_mm(struct elf_fdpic_params *exec_params, |
| 98 | struct elf_fdpic_params *interp_params, |
| 99 | unsigned long *start_stack, |
| 100 | unsigned long *start_brk) |
| 101 | { |
| 102 | elf_set_personality(&exec_params->hdr); |
| 103 | |
| 104 | exec_params->load_addr = 0x8000; |
| 105 | interp_params->load_addr = ELF_ET_DYN_BASE; |
| 106 | *start_stack = TASK_SIZE - SZ_16M; |
| 107 | |
| 108 | if ((exec_params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) == ELF_FDPIC_FLAG_INDEPENDENT) { |
| 109 | exec_params->flags &= ~ELF_FDPIC_FLAG_ARRANGEMENT; |
| 110 | exec_params->flags |= ELF_FDPIC_FLAG_CONSTDISP; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #endif |