elf.h 8.0 KB

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