xref: /linux-6.15/include/uapi/linux/elf.h (revision 049d19bb)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _UAPI_LINUX_ELF_H
3 #define _UAPI_LINUX_ELF_H
4 
5 #include <linux/types.h>
6 #include <linux/elf-em.h>
7 
8 /* 32-bit ELF base types. */
9 typedef __u32	Elf32_Addr;
10 typedef __u16	Elf32_Half;
11 typedef __u32	Elf32_Off;
12 typedef __s32	Elf32_Sword;
13 typedef __u32	Elf32_Word;
14 
15 /* 64-bit ELF base types. */
16 typedef __u64	Elf64_Addr;
17 typedef __u16	Elf64_Half;
18 typedef __s16	Elf64_SHalf;
19 typedef __u64	Elf64_Off;
20 typedef __s32	Elf64_Sword;
21 typedef __u32	Elf64_Word;
22 typedef __u64	Elf64_Xword;
23 typedef __s64	Elf64_Sxword;
24 
25 /* These constants are for the segment types stored in the image headers */
26 #define PT_NULL    0
27 #define PT_LOAD    1
28 #define PT_DYNAMIC 2
29 #define PT_INTERP  3
30 #define PT_NOTE    4
31 #define PT_SHLIB   5
32 #define PT_PHDR    6
33 #define PT_TLS     7               /* Thread local storage segment */
34 #define PT_LOOS    0x60000000      /* OS-specific */
35 #define PT_HIOS    0x6fffffff      /* OS-specific */
36 #define PT_LOPROC  0x70000000
37 #define PT_HIPROC  0x7fffffff
38 #define PT_GNU_EH_FRAME	(PT_LOOS + 0x474e550)
39 #define PT_GNU_STACK	(PT_LOOS + 0x474e551)
40 #define PT_GNU_RELRO	(PT_LOOS + 0x474e552)
41 #define PT_GNU_PROPERTY	(PT_LOOS + 0x474e553)
42 
43 
44 /* ARM MTE memory tag segment type */
45 #define PT_AARCH64_MEMTAG_MTE	(PT_LOPROC + 0x2)
46 
47 /*
48  * Extended Numbering
49  *
50  * If the real number of program header table entries is larger than
51  * or equal to PN_XNUM(0xffff), it is set to sh_info field of the
52  * section header at index 0, and PN_XNUM is set to e_phnum
53  * field. Otherwise, the section header at index 0 is zero
54  * initialized, if it exists.
55  *
56  * Specifications are available in:
57  *
58  * - Oracle: Linker and Libraries.
59  *   Part No: 817–1984–19, August 2011.
60  *   https://docs.oracle.com/cd/E18752_01/pdf/817-1984.pdf
61  *
62  * - System V ABI AMD64 Architecture Processor Supplement
63  *   Draft Version 0.99.4,
64  *   January 13, 2010.
65  *   http://www.cs.washington.edu/education/courses/cse351/12wi/supp-docs/abi.pdf
66  */
67 #define PN_XNUM 0xffff
68 
69 /* These constants define the different elf file types */
70 #define ET_NONE   0
71 #define ET_REL    1
72 #define ET_EXEC   2
73 #define ET_DYN    3
74 #define ET_CORE   4
75 #define ET_LOPROC 0xff00
76 #define ET_HIPROC 0xffff
77 
78 /* This is the info that is needed to parse the dynamic section of the file */
79 #define DT_NULL		0
80 #define DT_NEEDED	1
81 #define DT_PLTRELSZ	2
82 #define DT_PLTGOT	3
83 #define DT_HASH		4
84 #define DT_STRTAB	5
85 #define DT_SYMTAB	6
86 #define DT_RELA		7
87 #define DT_RELASZ	8
88 #define DT_RELAENT	9
89 #define DT_STRSZ	10
90 #define DT_SYMENT	11
91 #define DT_INIT		12
92 #define DT_FINI		13
93 #define DT_SONAME	14
94 #define DT_RPATH	15
95 #define DT_SYMBOLIC	16
96 #define DT_REL	        17
97 #define DT_RELSZ	18
98 #define DT_RELENT	19
99 #define DT_PLTREL	20
100 #define DT_DEBUG	21
101 #define DT_TEXTREL	22
102 #define DT_JMPREL	23
103 #define DT_ENCODING	32
104 #define OLD_DT_LOOS	0x60000000
105 #define DT_LOOS		0x6000000d
106 #define DT_HIOS		0x6ffff000
107 #define DT_VALRNGLO	0x6ffffd00
108 #define DT_VALRNGHI	0x6ffffdff
109 #define DT_ADDRRNGLO	0x6ffffe00
110 #define DT_GNU_HASH	0x6ffffef5
111 #define DT_ADDRRNGHI	0x6ffffeff
112 #define DT_VERSYM	0x6ffffff0
113 #define DT_RELACOUNT	0x6ffffff9
114 #define DT_RELCOUNT	0x6ffffffa
115 #define DT_FLAGS_1	0x6ffffffb
116 #define DT_VERDEF	0x6ffffffc
117 #define	DT_VERDEFNUM	0x6ffffffd
118 #define DT_VERNEED	0x6ffffffe
119 #define	DT_VERNEEDNUM	0x6fffffff
120 #define OLD_DT_HIOS     0x6fffffff
121 #define DT_LOPROC	0x70000000
122 #define DT_HIPROC	0x7fffffff
123 
124 /* This info is needed when parsing the symbol table */
125 #define STB_LOCAL  0
126 #define STB_GLOBAL 1
127 #define STB_WEAK   2
128 
129 #define STN_UNDEF 0
130 
131 #define STT_NOTYPE  0
132 #define STT_OBJECT  1
133 #define STT_FUNC    2
134 #define STT_SECTION 3
135 #define STT_FILE    4
136 #define STT_COMMON  5
137 #define STT_TLS     6
138 
139 #define VER_FLG_BASE 0x1
140 #define VER_FLG_WEAK 0x2
141 
142 #define ELF_ST_BIND(x)		((x) >> 4)
143 #define ELF_ST_TYPE(x)		((x) & 0xf)
144 #define ELF32_ST_BIND(x)	ELF_ST_BIND(x)
145 #define ELF32_ST_TYPE(x)	ELF_ST_TYPE(x)
146 #define ELF64_ST_BIND(x)	ELF_ST_BIND(x)
147 #define ELF64_ST_TYPE(x)	ELF_ST_TYPE(x)
148 
149 typedef struct {
150   Elf32_Sword d_tag;
151   union {
152     Elf32_Sword	d_val;
153     Elf32_Addr	d_ptr;
154   } d_un;
155 } Elf32_Dyn;
156 
157 typedef struct {
158   Elf64_Sxword d_tag;		/* entry tag value */
159   union {
160     Elf64_Xword d_val;
161     Elf64_Addr d_ptr;
162   } d_un;
163 } Elf64_Dyn;
164 
165 /* The following are used with relocations */
166 #define ELF32_R_SYM(x) ((x) >> 8)
167 #define ELF32_R_TYPE(x) ((x) & 0xff)
168 
169 #define ELF64_R_SYM(i)			((i) >> 32)
170 #define ELF64_R_TYPE(i)			((i) & 0xffffffff)
171 
172 typedef struct elf32_rel {
173   Elf32_Addr	r_offset;
174   Elf32_Word	r_info;
175 } Elf32_Rel;
176 
177 typedef struct elf64_rel {
178   Elf64_Addr r_offset;	/* Location at which to apply the action */
179   Elf64_Xword r_info;	/* index and type of relocation */
180 } Elf64_Rel;
181 
182 typedef struct elf32_rela {
183   Elf32_Addr	r_offset;
184   Elf32_Word	r_info;
185   Elf32_Sword	r_addend;
186 } Elf32_Rela;
187 
188 typedef struct elf64_rela {
189   Elf64_Addr r_offset;	/* Location at which to apply the action */
190   Elf64_Xword r_info;	/* index and type of relocation */
191   Elf64_Sxword r_addend;	/* Constant addend used to compute value */
192 } Elf64_Rela;
193 
194 typedef struct elf32_sym {
195   Elf32_Word	st_name;
196   Elf32_Addr	st_value;
197   Elf32_Word	st_size;
198   unsigned char	st_info;
199   unsigned char	st_other;
200   Elf32_Half	st_shndx;
201 } Elf32_Sym;
202 
203 typedef struct elf64_sym {
204   Elf64_Word st_name;		/* Symbol name, index in string tbl */
205   unsigned char	st_info;	/* Type and binding attributes */
206   unsigned char	st_other;	/* No defined meaning, 0 */
207   Elf64_Half st_shndx;		/* Associated section index */
208   Elf64_Addr st_value;		/* Value of the symbol */
209   Elf64_Xword st_size;		/* Associated symbol size */
210 } Elf64_Sym;
211 
212 
213 #define EI_NIDENT	16
214 
215 typedef struct elf32_hdr {
216   unsigned char	e_ident[EI_NIDENT];
217   Elf32_Half	e_type;
218   Elf32_Half	e_machine;
219   Elf32_Word	e_version;
220   Elf32_Addr	e_entry;  /* Entry point */
221   Elf32_Off	e_phoff;
222   Elf32_Off	e_shoff;
223   Elf32_Word	e_flags;
224   Elf32_Half	e_ehsize;
225   Elf32_Half	e_phentsize;
226   Elf32_Half	e_phnum;
227   Elf32_Half	e_shentsize;
228   Elf32_Half	e_shnum;
229   Elf32_Half	e_shstrndx;
230 } Elf32_Ehdr;
231 
232 typedef struct elf64_hdr {
233   unsigned char	e_ident[EI_NIDENT];	/* ELF "magic number" */
234   Elf64_Half e_type;
235   Elf64_Half e_machine;
236   Elf64_Word e_version;
237   Elf64_Addr e_entry;		/* Entry point virtual address */
238   Elf64_Off e_phoff;		/* Program header table file offset */
239   Elf64_Off e_shoff;		/* Section header table file offset */
240   Elf64_Word e_flags;
241   Elf64_Half e_ehsize;
242   Elf64_Half e_phentsize;
243   Elf64_Half e_phnum;
244   Elf64_Half e_shentsize;
245   Elf64_Half e_shnum;
246   Elf64_Half e_shstrndx;
247 } Elf64_Ehdr;
248 
249 /* These constants define the permissions on sections in the program
250    header, p_flags. */
251 #define PF_R		0x4
252 #define PF_W		0x2
253 #define PF_X		0x1
254 
255 typedef struct elf32_phdr {
256   Elf32_Word	p_type;
257   Elf32_Off	p_offset;
258   Elf32_Addr	p_vaddr;
259   Elf32_Addr	p_paddr;
260   Elf32_Word	p_filesz;
261   Elf32_Word	p_memsz;
262   Elf32_Word	p_flags;
263   Elf32_Word	p_align;
264 } Elf32_Phdr;
265 
266 typedef struct elf64_phdr {
267   Elf64_Word p_type;
268   Elf64_Word p_flags;
269   Elf64_Off p_offset;		/* Segment file offset */
270   Elf64_Addr p_vaddr;		/* Segment virtual address */
271   Elf64_Addr p_paddr;		/* Segment physical address */
272   Elf64_Xword p_filesz;		/* Segment size in file */
273   Elf64_Xword p_memsz;		/* Segment size in memory */
274   Elf64_Xword p_align;		/* Segment alignment, file & memory */
275 } Elf64_Phdr;
276 
277 /* sh_type */
278 #define SHT_NULL	0
279 #define SHT_PROGBITS	1
280 #define SHT_SYMTAB	2
281 #define SHT_STRTAB	3
282 #define SHT_RELA	4
283 #define SHT_HASH	5
284 #define SHT_DYNAMIC	6
285 #define SHT_NOTE	7
286 #define SHT_NOBITS	8
287 #define SHT_REL		9
288 #define SHT_SHLIB	10
289 #define SHT_DYNSYM	11
290 #define SHT_NUM		12
291 #define SHT_LOPROC	0x70000000
292 #define SHT_HIPROC	0x7fffffff
293 #define SHT_LOUSER	0x80000000
294 #define SHT_HIUSER	0xffffffff
295 
296 /* sh_flags */
297 #define SHF_WRITE		0x1
298 #define SHF_ALLOC		0x2
299 #define SHF_EXECINSTR		0x4
300 #define SHF_RELA_LIVEPATCH	0x00100000
301 #define SHF_RO_AFTER_INIT	0x00200000
302 #define SHF_MASKPROC		0xf0000000
303 
304 /* special section indexes */
305 #define SHN_UNDEF	0
306 #define SHN_LORESERVE	0xff00
307 #define SHN_LOPROC	0xff00
308 #define SHN_HIPROC	0xff1f
309 #define SHN_LIVEPATCH	0xff20
310 #define SHN_ABS		0xfff1
311 #define SHN_COMMON	0xfff2
312 #define SHN_HIRESERVE	0xffff
313 
314 typedef struct elf32_shdr {
315   Elf32_Word	sh_name;
316   Elf32_Word	sh_type;
317   Elf32_Word	sh_flags;
318   Elf32_Addr	sh_addr;
319   Elf32_Off	sh_offset;
320   Elf32_Word	sh_size;
321   Elf32_Word	sh_link;
322   Elf32_Word	sh_info;
323   Elf32_Word	sh_addralign;
324   Elf32_Word	sh_entsize;
325 } Elf32_Shdr;
326 
327 typedef struct elf64_shdr {
328   Elf64_Word sh_name;		/* Section name, index in string tbl */
329   Elf64_Word sh_type;		/* Type of section */
330   Elf64_Xword sh_flags;		/* Miscellaneous section attributes */
331   Elf64_Addr sh_addr;		/* Section virtual addr at execution */
332   Elf64_Off sh_offset;		/* Section file offset */
333   Elf64_Xword sh_size;		/* Size of section in bytes */
334   Elf64_Word sh_link;		/* Index of another section */
335   Elf64_Word sh_info;		/* Additional section information */
336   Elf64_Xword sh_addralign;	/* Section alignment */
337   Elf64_Xword sh_entsize;	/* Entry size if section holds table */
338 } Elf64_Shdr;
339 
340 #define	EI_MAG0		0		/* e_ident[] indexes */
341 #define	EI_MAG1		1
342 #define	EI_MAG2		2
343 #define	EI_MAG3		3
344 #define	EI_CLASS	4
345 #define	EI_DATA		5
346 #define	EI_VERSION	6
347 #define	EI_OSABI	7
348 #define	EI_PAD		8
349 
350 #define	ELFMAG0		0x7f		/* EI_MAG */
351 #define	ELFMAG1		'E'
352 #define	ELFMAG2		'L'
353 #define	ELFMAG3		'F'
354 #define	ELFMAG		"\177ELF"
355 #define	SELFMAG		4
356 
357 #define	ELFCLASSNONE	0		/* EI_CLASS */
358 #define	ELFCLASS32	1
359 #define	ELFCLASS64	2
360 #define	ELFCLASSNUM	3
361 
362 #define ELFDATANONE	0		/* e_ident[EI_DATA] */
363 #define ELFDATA2LSB	1
364 #define ELFDATA2MSB	2
365 
366 #define EV_NONE		0		/* e_version, EI_VERSION */
367 #define EV_CURRENT	1
368 #define EV_NUM		2
369 
370 #define ELFOSABI_NONE	0
371 #define ELFOSABI_LINUX	3
372 
373 #ifndef ELF_OSABI
374 #define ELF_OSABI ELFOSABI_NONE
375 #endif
376 
377 /*
378  * Notes used in ET_CORE. Architectures export some of the arch register sets
379  * using the corresponding note types via the PTRACE_GETREGSET and
380  * PTRACE_SETREGSET requests.
381  * The note name for these types is "LINUX", except NT_PRFPREG that is named
382  * "CORE".
383  */
384 #define NT_PRSTATUS	1
385 #define NT_PRFPREG	2
386 #define NT_PRPSINFO	3
387 #define NT_TASKSTRUCT	4
388 #define NT_AUXV		6
389 /*
390  * Note to userspace developers: size of NT_SIGINFO note may increase
391  * in the future to accomodate more fields, don't assume it is fixed!
392  */
393 #define NT_SIGINFO      0x53494749
394 #define NT_FILE         0x46494c45
395 #define NT_PRXFPREG     0x46e62b7f      /* copied from gdb5.1/include/elf/common.h */
396 #define NT_PPC_VMX	0x100		/* PowerPC Altivec/VMX registers */
397 #define NT_PPC_SPE	0x101		/* PowerPC SPE/EVR registers */
398 #define NT_PPC_VSX	0x102		/* PowerPC VSX registers */
399 #define NT_PPC_TAR	0x103		/* Target Address Register */
400 #define NT_PPC_PPR	0x104		/* Program Priority Register */
401 #define NT_PPC_DSCR	0x105		/* Data Stream Control Register */
402 #define NT_PPC_EBB	0x106		/* Event Based Branch Registers */
403 #define NT_PPC_PMU	0x107		/* Performance Monitor Registers */
404 #define NT_PPC_TM_CGPR	0x108		/* TM checkpointed GPR Registers */
405 #define NT_PPC_TM_CFPR	0x109		/* TM checkpointed FPR Registers */
406 #define NT_PPC_TM_CVMX	0x10a		/* TM checkpointed VMX Registers */
407 #define NT_PPC_TM_CVSX	0x10b		/* TM checkpointed VSX Registers */
408 #define NT_PPC_TM_SPR	0x10c		/* TM Special Purpose Registers */
409 #define NT_PPC_TM_CTAR	0x10d		/* TM checkpointed Target Address Register */
410 #define NT_PPC_TM_CPPR	0x10e		/* TM checkpointed Program Priority Register */
411 #define NT_PPC_TM_CDSCR	0x10f		/* TM checkpointed Data Stream Control Register */
412 #define NT_PPC_PKEY	0x110		/* Memory Protection Keys registers */
413 #define NT_PPC_DEXCR	0x111		/* PowerPC DEXCR registers */
414 #define NT_PPC_HASHKEYR	0x112		/* PowerPC HASHKEYR register */
415 #define NT_386_TLS	0x200		/* i386 TLS slots (struct user_desc) */
416 #define NT_386_IOPERM	0x201		/* x86 io permission bitmap (1=deny) */
417 #define NT_X86_XSTATE	0x202		/* x86 extended state using xsave */
418 /* Old binutils treats 0x203 as a CET state */
419 #define NT_X86_SHSTK	0x204		/* x86 SHSTK state */
420 #define NT_X86_XSAVE_LAYOUT	0x205	/* XSAVE layout description */
421 #define NT_S390_HIGH_GPRS	0x300	/* s390 upper register halves */
422 #define NT_S390_TIMER	0x301		/* s390 timer register */
423 #define NT_S390_TODCMP	0x302		/* s390 TOD clock comparator register */
424 #define NT_S390_TODPREG	0x303		/* s390 TOD programmable register */
425 #define NT_S390_CTRS	0x304		/* s390 control registers */
426 #define NT_S390_PREFIX	0x305		/* s390 prefix register */
427 #define NT_S390_LAST_BREAK	0x306	/* s390 breaking event address */
428 #define NT_S390_SYSTEM_CALL	0x307	/* s390 system call restart data */
429 #define NT_S390_TDB	0x308		/* s390 transaction diagnostic block */
430 #define NT_S390_VXRS_LOW	0x309	/* s390 vector registers 0-15 upper half */
431 #define NT_S390_VXRS_HIGH	0x30a	/* s390 vector registers 16-31 */
432 #define NT_S390_GS_CB	0x30b		/* s390 guarded storage registers */
433 #define NT_S390_GS_BC	0x30c		/* s390 guarded storage broadcast control block */
434 #define NT_S390_RI_CB	0x30d		/* s390 runtime instrumentation */
435 #define NT_S390_PV_CPU_DATA	0x30e	/* s390 protvirt cpu dump data */
436 #define NT_ARM_VFP	0x400		/* ARM VFP/NEON registers */
437 #define NT_ARM_TLS	0x401		/* ARM TLS register */
438 #define NT_ARM_HW_BREAK	0x402		/* ARM hardware breakpoint registers */
439 #define NT_ARM_HW_WATCH	0x403		/* ARM hardware watchpoint registers */
440 #define NT_ARM_SYSTEM_CALL	0x404	/* ARM system call number */
441 #define NT_ARM_SVE	0x405		/* ARM Scalable Vector Extension registers */
442 #define NT_ARM_PAC_MASK		0x406	/* ARM pointer authentication code masks */
443 #define NT_ARM_PACA_KEYS	0x407	/* ARM pointer authentication address keys */
444 #define NT_ARM_PACG_KEYS	0x408	/* ARM pointer authentication generic key */
445 #define NT_ARM_TAGGED_ADDR_CTRL	0x409	/* arm64 tagged address control (prctl()) */
446 #define NT_ARM_PAC_ENABLED_KEYS	0x40a	/* arm64 ptr auth enabled keys (prctl()) */
447 #define NT_ARM_SSVE	0x40b		/* ARM Streaming SVE registers */
448 #define NT_ARM_ZA	0x40c		/* ARM SME ZA registers */
449 #define NT_ARM_ZT	0x40d		/* ARM SME ZT registers */
450 #define NT_ARM_FPMR	0x40e		/* ARM floating point mode register */
451 #define NT_ARM_POE	0x40f		/* ARM POE registers */
452 #define NT_ARM_GCS	0x410		/* ARM GCS state */
453 #define NT_ARC_V2	0x600		/* ARCv2 accumulator/extra registers */
454 #define NT_VMCOREDD	0x700		/* Vmcore Device Dump Note */
455 #define NT_MIPS_DSP	0x800		/* MIPS DSP ASE registers */
456 #define NT_MIPS_FP_MODE	0x801		/* MIPS floating-point mode */
457 #define NT_MIPS_MSA	0x802		/* MIPS SIMD registers */
458 #define NT_RISCV_CSR	0x900		/* RISC-V Control and Status Registers */
459 #define NT_RISCV_VECTOR	0x901		/* RISC-V vector registers */
460 #define NT_RISCV_TAGGED_ADDR_CTRL 0x902	/* RISC-V tagged address control (prctl()) */
461 #define NT_LOONGARCH_CPUCFG	0xa00	/* LoongArch CPU config registers */
462 #define NT_LOONGARCH_CSR	0xa01	/* LoongArch control and status registers */
463 #define NT_LOONGARCH_LSX	0xa02	/* LoongArch Loongson SIMD Extension registers */
464 #define NT_LOONGARCH_LASX	0xa03	/* LoongArch Loongson Advanced SIMD Extension registers */
465 #define NT_LOONGARCH_LBT	0xa04	/* LoongArch Loongson Binary Translation registers */
466 #define NT_LOONGARCH_HW_BREAK	0xa05   /* LoongArch hardware breakpoint registers */
467 #define NT_LOONGARCH_HW_WATCH	0xa06   /* LoongArch hardware watchpoint registers */
468 
469 /* Note types with note name "GNU" */
470 #define NT_GNU_PROPERTY_TYPE_0	5
471 
472 /* Note header in a PT_NOTE section */
473 typedef struct elf32_note {
474   Elf32_Word	n_namesz;	/* Name size */
475   Elf32_Word	n_descsz;	/* Content size */
476   Elf32_Word	n_type;		/* Content type */
477 } Elf32_Nhdr;
478 
479 /* Note header in a PT_NOTE section */
480 typedef struct elf64_note {
481   Elf64_Word n_namesz;	/* Name size */
482   Elf64_Word n_descsz;	/* Content size */
483   Elf64_Word n_type;	/* Content type */
484 } Elf64_Nhdr;
485 
486 /* .note.gnu.property types for EM_AARCH64: */
487 #define GNU_PROPERTY_AARCH64_FEATURE_1_AND	0xc0000000
488 
489 /* Bits for GNU_PROPERTY_AARCH64_FEATURE_1_BTI */
490 #define GNU_PROPERTY_AARCH64_FEATURE_1_BTI	(1U << 0)
491 
492 #endif /* _UAPI_LINUX_ELF_H */
493