1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2012 ARM Ltd. 4 */ 5 #ifndef __ASM_ELF_H 6 #define __ASM_ELF_H 7 8 #include <asm/hwcap.h> 9 10 /* 11 * ELF register definitions.. 12 */ 13 #include <asm/ptrace.h> 14 #include <asm/user.h> 15 16 /* 17 * AArch64 static relocation types. 18 */ 19 20 /* Miscellaneous. */ 21 #define R_ARM_NONE 0 22 #define R_AARCH64_NONE 256 23 24 /* Data. */ 25 #define R_AARCH64_ABS64 257 26 #define R_AARCH64_ABS32 258 27 #define R_AARCH64_ABS16 259 28 #define R_AARCH64_PREL64 260 29 #define R_AARCH64_PREL32 261 30 #define R_AARCH64_PREL16 262 31 32 /* Instructions. */ 33 #define R_AARCH64_MOVW_UABS_G0 263 34 #define R_AARCH64_MOVW_UABS_G0_NC 264 35 #define R_AARCH64_MOVW_UABS_G1 265 36 #define R_AARCH64_MOVW_UABS_G1_NC 266 37 #define R_AARCH64_MOVW_UABS_G2 267 38 #define R_AARCH64_MOVW_UABS_G2_NC 268 39 #define R_AARCH64_MOVW_UABS_G3 269 40 41 #define R_AARCH64_MOVW_SABS_G0 270 42 #define R_AARCH64_MOVW_SABS_G1 271 43 #define R_AARCH64_MOVW_SABS_G2 272 44 45 #define R_AARCH64_LD_PREL_LO19 273 46 #define R_AARCH64_ADR_PREL_LO21 274 47 #define R_AARCH64_ADR_PREL_PG_HI21 275 48 #define R_AARCH64_ADR_PREL_PG_HI21_NC 276 49 #define R_AARCH64_ADD_ABS_LO12_NC 277 50 #define R_AARCH64_LDST8_ABS_LO12_NC 278 51 52 #define R_AARCH64_TSTBR14 279 53 #define R_AARCH64_CONDBR19 280 54 #define R_AARCH64_JUMP26 282 55 #define R_AARCH64_CALL26 283 56 #define R_AARCH64_LDST16_ABS_LO12_NC 284 57 #define R_AARCH64_LDST32_ABS_LO12_NC 285 58 #define R_AARCH64_LDST64_ABS_LO12_NC 286 59 #define R_AARCH64_LDST128_ABS_LO12_NC 299 60 61 #define R_AARCH64_MOVW_PREL_G0 287 62 #define R_AARCH64_MOVW_PREL_G0_NC 288 63 #define R_AARCH64_MOVW_PREL_G1 289 64 #define R_AARCH64_MOVW_PREL_G1_NC 290 65 #define R_AARCH64_MOVW_PREL_G2 291 66 #define R_AARCH64_MOVW_PREL_G2_NC 292 67 #define R_AARCH64_MOVW_PREL_G3 293 68 69 #define R_AARCH64_RELATIVE 1027 70 71 /* 72 * These are used to set parameters in the core dumps. 73 */ 74 #define ELF_CLASS ELFCLASS64 75 #define ELF_DATA ELFDATA2LSB 76 #define ELF_ARCH EM_AARCH64 77 78 /* 79 * This yields a string that ld.so will use to load implementation 80 * specific libraries for optimization. This is more specific in 81 * intent than poking at uname or /proc/cpuinfo. 82 */ 83 #define ELF_PLATFORM_SIZE 16 84 #define ELF_PLATFORM ("aarch64") 85 86 /* 87 * This is used to ensure we don't load something for the wrong architecture. 88 */ 89 #define elf_check_arch(x) ((x)->e_machine == EM_AARCH64) 90 91 /* 92 * An executable for which elf_read_implies_exec() returns TRUE will 93 * have the READ_IMPLIES_EXEC personality flag set automatically. 94 * 95 * The decision process for determining the results are: 96 * 97 * CPU*: | arm32 | arm64 | 98 * ELF: | | | 99 * ---------------------|------------|------------| 100 * missing PT_GNU_STACK | exec-all | exec-none | 101 * PT_GNU_STACK == RWX | exec-stack | exec-stack | 102 * PT_GNU_STACK == RW | exec-none | exec-none | 103 * 104 * exec-all : all PROT_READ user mappings are executable, except when 105 * backed by files on a noexec-filesystem. 106 * exec-none : only PROT_EXEC user mappings are executable. 107 * exec-stack: only the stack and PROT_EXEC user mappings are executable. 108 * 109 * *all arm64 CPUs support NX, so there is no "lacks NX" column. 110 * 111 */ 112 #define compat_elf_read_implies_exec(ex, stk) (stk == EXSTACK_DEFAULT) 113 114 #define CORE_DUMP_USE_REGSET 115 #define ELF_EXEC_PAGESIZE PAGE_SIZE 116 117 /* 118 * This is the base location for PIE (ET_DYN with INTERP) loads. On 119 * 64-bit, this is above 4GB to leave the entire 32-bit address 120 * space open for things that want to use the area for 32-bit pointers. 121 */ 122 #ifdef CONFIG_ARM64_FORCE_52BIT 123 #define ELF_ET_DYN_BASE (2 * TASK_SIZE_64 / 3) 124 #else 125 #define ELF_ET_DYN_BASE (2 * DEFAULT_MAP_WINDOW_64 / 3) 126 #endif /* CONFIG_ARM64_FORCE_52BIT */ 127 128 #ifndef __ASSEMBLER__ 129 130 #include <uapi/linux/elf.h> 131 #include <linux/bug.h> 132 #include <linux/errno.h> 133 #include <linux/fs.h> 134 #include <linux/types.h> 135 #include <asm/processor.h> /* for signal_minsigstksz, used by ARCH_DLINFO */ 136 137 typedef unsigned long elf_greg_t; 138 139 #define ELF_NGREG (sizeof(struct user_pt_regs) / sizeof(elf_greg_t)) 140 #define ELF_CORE_COPY_REGS(dest, regs) \ 141 *(struct user_pt_regs *)&(dest) = (regs)->user_regs; 142 143 typedef elf_greg_t elf_gregset_t[ELF_NGREG]; 144 typedef struct user_fpsimd_state elf_fpregset_t; 145 146 /* 147 * When the program starts, a1 contains a pointer to a function to be 148 * registered with atexit, as per the SVR4 ABI. A value of 0 means we have no 149 * such handler. 150 */ 151 #define ELF_PLAT_INIT(_r, load_addr) (_r)->regs[0] = 0 152 153 #define SET_PERSONALITY(ex) \ 154 ({ \ 155 clear_thread_flag(TIF_32BIT); \ 156 current->personality &= ~READ_IMPLIES_EXEC; \ 157 }) 158 159 /* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes */ 160 #define ARCH_DLINFO \ 161 do { \ 162 NEW_AUX_ENT(AT_SYSINFO_EHDR, \ 163 (elf_addr_t)current->mm->context.vdso); \ 164 \ 165 /* \ 166 * Should always be nonzero unless there's a kernel bug. \ 167 * If we haven't determined a sensible value to give to \ 168 * userspace, omit the entry: \ 169 */ \ 170 if (likely(signal_minsigstksz)) \ 171 NEW_AUX_ENT(AT_MINSIGSTKSZ, signal_minsigstksz); \ 172 else \ 173 NEW_AUX_ENT(AT_IGNORE, 0); \ 174 } while (0) 175 176 #define ARCH_HAS_SETUP_ADDITIONAL_PAGES 177 struct linux_binprm; 178 extern int arch_setup_additional_pages(struct linux_binprm *bprm, 179 int uses_interp); 180 181 /* 1GB of VA */ 182 #ifdef CONFIG_COMPAT 183 #define STACK_RND_MASK (test_thread_flag(TIF_32BIT) ? \ 184 0x7ff >> (PAGE_SHIFT - 12) : \ 185 0x3ffff >> (PAGE_SHIFT - 12)) 186 #else 187 #define STACK_RND_MASK (0x3ffff >> (PAGE_SHIFT - 12)) 188 #endif 189 190 #define COMPAT_ELF_PLATFORM ("v8l") 191 192 /* AArch32 registers. */ 193 #define COMPAT_ELF_NGREG 18 194 typedef unsigned int compat_elf_greg_t; 195 typedef compat_elf_greg_t compat_elf_gregset_t[COMPAT_ELF_NGREG]; 196 197 #ifdef CONFIG_COMPAT 198 199 /* PIE load location for compat arm. Must match ARM ELF_ET_DYN_BASE. */ 200 #define COMPAT_ELF_ET_DYN_BASE 0x000400000UL 201 202 /* AArch32 EABI. */ 203 #define EF_ARM_EABI_MASK 0xff000000 204 int compat_elf_check_arch(const struct elf32_hdr *); 205 #define compat_elf_check_arch compat_elf_check_arch 206 #define compat_start_thread compat_start_thread 207 /* 208 * Unlike the native SET_PERSONALITY macro, the compat version maintains 209 * READ_IMPLIES_EXEC across an execve() since this is the behaviour on 210 * arch/arm/. 211 */ 212 #define COMPAT_SET_PERSONALITY(ex) \ 213 ({ \ 214 set_thread_flag(TIF_32BIT); \ 215 }) 216 #ifdef CONFIG_COMPAT_VDSO 217 #define COMPAT_ARCH_DLINFO \ 218 do { \ 219 /* \ 220 * Note that we use Elf64_Off instead of elf_addr_t because \ 221 * elf_addr_t in compat is defined as Elf32_Addr and casting \ 222 * current->mm->context.vdso to it triggers a cast warning of \ 223 * cast from pointer to integer of different size. \ 224 */ \ 225 NEW_AUX_ENT(AT_SYSINFO_EHDR, \ 226 (Elf64_Off)current->mm->context.vdso); \ 227 } while (0) 228 #else 229 #define COMPAT_ARCH_DLINFO 230 #endif 231 extern int aarch32_setup_additional_pages(struct linux_binprm *bprm, 232 int uses_interp); 233 #define compat_arch_setup_additional_pages \ 234 aarch32_setup_additional_pages 235 236 #endif /* CONFIG_COMPAT */ 237 238 struct arch_elf_state { 239 int flags; 240 }; 241 242 #define ARM64_ELF_BTI (1 << 0) 243 244 #define INIT_ARCH_ELF_STATE { \ 245 .flags = 0, \ 246 } 247 248 static inline int arch_parse_elf_property(u32 type, const void *data, 249 size_t datasz, bool compat, 250 struct arch_elf_state *arch) 251 { 252 /* No known properties for AArch32 yet */ 253 if (IS_ENABLED(CONFIG_COMPAT) && compat) 254 return 0; 255 256 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) { 257 const u32 *p = data; 258 259 if (datasz != sizeof(*p)) 260 return -ENOEXEC; 261 262 if (system_supports_bti() && 263 (*p & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) 264 arch->flags |= ARM64_ELF_BTI; 265 } 266 267 return 0; 268 } 269 270 static inline int arch_elf_pt_proc(void *ehdr, void *phdr, 271 struct file *f, bool is_interp, 272 struct arch_elf_state *state) 273 { 274 return 0; 275 } 276 277 static inline int arch_check_elf(void *ehdr, bool has_interp, 278 void *interp_ehdr, 279 struct arch_elf_state *state) 280 { 281 return 0; 282 } 283 284 #endif /* !__ASSEMBLER__ */ 285 286 #endif 287