xref: /linux-6.15/include/linux/elf.h (revision 40efcb05)
1 #ifndef _LINUX_ELF_H
2 #define _LINUX_ELF_H
3 
4 #include <linux/types.h>
5 #include <linux/elf-em.h>
6 #include <asm/elf.h>
7 
8 struct file;
9 
10 #ifndef elf_read_implies_exec
11   /* Executables for which elf_read_implies_exec() returns TRUE will
12      have the READ_IMPLIES_EXEC personality flag set automatically.
13      Override in asm/elf.h as needed.  */
14 # define elf_read_implies_exec(ex, have_pt_gnu_stack)	0
15 #endif
16 
17 /* 32-bit ELF base types. */
18 typedef __u32	Elf32_Addr;
19 typedef __u16	Elf32_Half;
20 typedef __u32	Elf32_Off;
21 typedef __s32	Elf32_Sword;
22 typedef __u32	Elf32_Word;
23 
24 /* 64-bit ELF base types. */
25 typedef __u64	Elf64_Addr;
26 typedef __u16	Elf64_Half;
27 typedef __s16	Elf64_SHalf;
28 typedef __u64	Elf64_Off;
29 typedef __s32	Elf64_Sword;
30 typedef __u32	Elf64_Word;
31 typedef __u64	Elf64_Xword;
32 typedef __s64	Elf64_Sxword;
33 
34 /* These constants are for the segment types stored in the image headers */
35 #define PT_NULL    0
36 #define PT_LOAD    1
37 #define PT_DYNAMIC 2
38 #define PT_INTERP  3
39 #define PT_NOTE    4
40 #define PT_SHLIB   5
41 #define PT_PHDR    6
42 #define PT_TLS     7               /* Thread local storage segment */
43 #define PT_LOOS    0x60000000      /* OS-specific */
44 #define PT_HIOS    0x6fffffff      /* OS-specific */
45 #define PT_LOPROC  0x70000000
46 #define PT_HIPROC  0x7fffffff
47 #define PT_GNU_EH_FRAME		0x6474e550
48 
49 #define PT_GNU_STACK	(PT_LOOS + 0x474e551)
50 
51 /* These constants define the different elf file types */
52 #define ET_NONE   0
53 #define ET_REL    1
54 #define ET_EXEC   2
55 #define ET_DYN    3
56 #define ET_CORE   4
57 #define ET_LOPROC 0xff00
58 #define ET_HIPROC 0xffff
59 
60 /* This is the info that is needed to parse the dynamic section of the file */
61 #define DT_NULL		0
62 #define DT_NEEDED	1
63 #define DT_PLTRELSZ	2
64 #define DT_PLTGOT	3
65 #define DT_HASH		4
66 #define DT_STRTAB	5
67 #define DT_SYMTAB	6
68 #define DT_RELA		7
69 #define DT_RELASZ	8
70 #define DT_RELAENT	9
71 #define DT_STRSZ	10
72 #define DT_SYMENT	11
73 #define DT_INIT		12
74 #define DT_FINI		13
75 #define DT_SONAME	14
76 #define DT_RPATH 	15
77 #define DT_SYMBOLIC	16
78 #define DT_REL	        17
79 #define DT_RELSZ	18
80 #define DT_RELENT	19
81 #define DT_PLTREL	20
82 #define DT_DEBUG	21
83 #define DT_TEXTREL	22
84 #define DT_JMPREL	23
85 #define DT_ENCODING	32
86 #define OLD_DT_LOOS	0x60000000
87 #define DT_LOOS		0x6000000d
88 #define DT_HIOS		0x6ffff000
89 #define DT_VALRNGLO	0x6ffffd00
90 #define DT_VALRNGHI	0x6ffffdff
91 #define DT_ADDRRNGLO	0x6ffffe00
92 #define DT_ADDRRNGHI	0x6ffffeff
93 #define DT_VERSYM	0x6ffffff0
94 #define DT_RELACOUNT	0x6ffffff9
95 #define DT_RELCOUNT	0x6ffffffa
96 #define DT_FLAGS_1	0x6ffffffb
97 #define DT_VERDEF	0x6ffffffc
98 #define	DT_VERDEFNUM	0x6ffffffd
99 #define DT_VERNEED	0x6ffffffe
100 #define	DT_VERNEEDNUM	0x6fffffff
101 #define OLD_DT_HIOS     0x6fffffff
102 #define DT_LOPROC	0x70000000
103 #define DT_HIPROC	0x7fffffff
104 
105 /* This info is needed when parsing the symbol table */
106 #define STB_LOCAL  0
107 #define STB_GLOBAL 1
108 #define STB_WEAK   2
109 
110 #define STT_NOTYPE  0
111 #define STT_OBJECT  1
112 #define STT_FUNC    2
113 #define STT_SECTION 3
114 #define STT_FILE    4
115 #define STT_COMMON  5
116 #define STT_TLS     6
117 
118 #define ELF_ST_BIND(x)		((x) >> 4)
119 #define ELF_ST_TYPE(x)		(((unsigned int) x) & 0xf)
120 #define ELF32_ST_BIND(x)	ELF_ST_BIND(x)
121 #define ELF32_ST_TYPE(x)	ELF_ST_TYPE(x)
122 #define ELF64_ST_BIND(x)	ELF_ST_BIND(x)
123 #define ELF64_ST_TYPE(x)	ELF_ST_TYPE(x)
124 
125 typedef struct dynamic{
126   Elf32_Sword d_tag;
127   union{
128     Elf32_Sword	d_val;
129     Elf32_Addr	d_ptr;
130   } d_un;
131 } Elf32_Dyn;
132 
133 typedef struct {
134   Elf64_Sxword d_tag;		/* entry tag value */
135   union {
136     Elf64_Xword d_val;
137     Elf64_Addr d_ptr;
138   } d_un;
139 } Elf64_Dyn;
140 
141 /* The following are used with relocations */
142 #define ELF32_R_SYM(x) ((x) >> 8)
143 #define ELF32_R_TYPE(x) ((x) & 0xff)
144 
145 #define ELF64_R_SYM(i)			((i) >> 32)
146 #define ELF64_R_TYPE(i)			((i) & 0xffffffff)
147 
148 typedef struct elf32_rel {
149   Elf32_Addr	r_offset;
150   Elf32_Word	r_info;
151 } Elf32_Rel;
152 
153 typedef struct elf64_rel {
154   Elf64_Addr r_offset;	/* Location at which to apply the action */
155   Elf64_Xword r_info;	/* index and type of relocation */
156 } Elf64_Rel;
157 
158 typedef struct elf32_rela{
159   Elf32_Addr	r_offset;
160   Elf32_Word	r_info;
161   Elf32_Sword	r_addend;
162 } Elf32_Rela;
163 
164 typedef struct elf64_rela {
165   Elf64_Addr r_offset;	/* Location at which to apply the action */
166   Elf64_Xword r_info;	/* index and type of relocation */
167   Elf64_Sxword r_addend;	/* Constant addend used to compute value */
168 } Elf64_Rela;
169 
170 typedef struct elf32_sym{
171   Elf32_Word	st_name;
172   Elf32_Addr	st_value;
173   Elf32_Word	st_size;
174   unsigned char	st_info;
175   unsigned char	st_other;
176   Elf32_Half	st_shndx;
177 } Elf32_Sym;
178 
179 typedef struct elf64_sym {
180   Elf64_Word st_name;		/* Symbol name, index in string tbl */
181   unsigned char	st_info;	/* Type and binding attributes */
182   unsigned char	st_other;	/* No defined meaning, 0 */
183   Elf64_Half st_shndx;		/* Associated section index */
184   Elf64_Addr st_value;		/* Value of the symbol */
185   Elf64_Xword st_size;		/* Associated symbol size */
186 } Elf64_Sym;
187 
188 
189 #define EI_NIDENT	16
190 
191 typedef struct elf32_hdr{
192   unsigned char	e_ident[EI_NIDENT];
193   Elf32_Half	e_type;
194   Elf32_Half	e_machine;
195   Elf32_Word	e_version;
196   Elf32_Addr	e_entry;  /* Entry point */
197   Elf32_Off	e_phoff;
198   Elf32_Off	e_shoff;
199   Elf32_Word	e_flags;
200   Elf32_Half	e_ehsize;
201   Elf32_Half	e_phentsize;
202   Elf32_Half	e_phnum;
203   Elf32_Half	e_shentsize;
204   Elf32_Half	e_shnum;
205   Elf32_Half	e_shstrndx;
206 } Elf32_Ehdr;
207 
208 typedef struct elf64_hdr {
209   unsigned char	e_ident[16];		/* ELF "magic number" */
210   Elf64_Half e_type;
211   Elf64_Half e_machine;
212   Elf64_Word e_version;
213   Elf64_Addr e_entry;		/* Entry point virtual address */
214   Elf64_Off e_phoff;		/* Program header table file offset */
215   Elf64_Off e_shoff;		/* Section header table file offset */
216   Elf64_Word e_flags;
217   Elf64_Half e_ehsize;
218   Elf64_Half e_phentsize;
219   Elf64_Half e_phnum;
220   Elf64_Half e_shentsize;
221   Elf64_Half e_shnum;
222   Elf64_Half e_shstrndx;
223 } Elf64_Ehdr;
224 
225 /* These constants define the permissions on sections in the program
226    header, p_flags. */
227 #define PF_R		0x4
228 #define PF_W		0x2
229 #define PF_X		0x1
230 
231 typedef struct elf32_phdr{
232   Elf32_Word	p_type;
233   Elf32_Off	p_offset;
234   Elf32_Addr	p_vaddr;
235   Elf32_Addr	p_paddr;
236   Elf32_Word	p_filesz;
237   Elf32_Word	p_memsz;
238   Elf32_Word	p_flags;
239   Elf32_Word	p_align;
240 } Elf32_Phdr;
241 
242 typedef struct elf64_phdr {
243   Elf64_Word p_type;
244   Elf64_Word p_flags;
245   Elf64_Off p_offset;		/* Segment file offset */
246   Elf64_Addr p_vaddr;		/* Segment virtual address */
247   Elf64_Addr p_paddr;		/* Segment physical address */
248   Elf64_Xword p_filesz;		/* Segment size in file */
249   Elf64_Xword p_memsz;		/* Segment size in memory */
250   Elf64_Xword p_align;		/* Segment alignment, file & memory */
251 } Elf64_Phdr;
252 
253 /* sh_type */
254 #define SHT_NULL	0
255 #define SHT_PROGBITS	1
256 #define SHT_SYMTAB	2
257 #define SHT_STRTAB	3
258 #define SHT_RELA	4
259 #define SHT_HASH	5
260 #define SHT_DYNAMIC	6
261 #define SHT_NOTE	7
262 #define SHT_NOBITS	8
263 #define SHT_REL		9
264 #define SHT_SHLIB	10
265 #define SHT_DYNSYM	11
266 #define SHT_NUM		12
267 #define SHT_LOPROC	0x70000000
268 #define SHT_HIPROC	0x7fffffff
269 #define SHT_LOUSER	0x80000000
270 #define SHT_HIUSER	0xffffffff
271 
272 /* sh_flags */
273 #define SHF_WRITE	0x1
274 #define SHF_ALLOC	0x2
275 #define SHF_EXECINSTR	0x4
276 #define SHF_MASKPROC	0xf0000000
277 
278 /* special section indexes */
279 #define SHN_UNDEF	0
280 #define SHN_LORESERVE	0xff00
281 #define SHN_LOPROC	0xff00
282 #define SHN_HIPROC	0xff1f
283 #define SHN_ABS		0xfff1
284 #define SHN_COMMON	0xfff2
285 #define SHN_HIRESERVE	0xffff
286 
287 typedef struct {
288   Elf32_Word	sh_name;
289   Elf32_Word	sh_type;
290   Elf32_Word	sh_flags;
291   Elf32_Addr	sh_addr;
292   Elf32_Off	sh_offset;
293   Elf32_Word	sh_size;
294   Elf32_Word	sh_link;
295   Elf32_Word	sh_info;
296   Elf32_Word	sh_addralign;
297   Elf32_Word	sh_entsize;
298 } Elf32_Shdr;
299 
300 typedef struct elf64_shdr {
301   Elf64_Word sh_name;		/* Section name, index in string tbl */
302   Elf64_Word sh_type;		/* Type of section */
303   Elf64_Xword sh_flags;		/* Miscellaneous section attributes */
304   Elf64_Addr sh_addr;		/* Section virtual addr at execution */
305   Elf64_Off sh_offset;		/* Section file offset */
306   Elf64_Xword sh_size;		/* Size of section in bytes */
307   Elf64_Word sh_link;		/* Index of another section */
308   Elf64_Word sh_info;		/* Additional section information */
309   Elf64_Xword sh_addralign;	/* Section alignment */
310   Elf64_Xword sh_entsize;	/* Entry size if section holds table */
311 } Elf64_Shdr;
312 
313 #define	EI_MAG0		0		/* e_ident[] indexes */
314 #define	EI_MAG1		1
315 #define	EI_MAG2		2
316 #define	EI_MAG3		3
317 #define	EI_CLASS	4
318 #define	EI_DATA		5
319 #define	EI_VERSION	6
320 #define	EI_OSABI	7
321 #define	EI_PAD		8
322 
323 #define	ELFMAG0		0x7f		/* EI_MAG */
324 #define	ELFMAG1		'E'
325 #define	ELFMAG2		'L'
326 #define	ELFMAG3		'F'
327 #define	ELFMAG		"\177ELF"
328 #define	SELFMAG		4
329 
330 #define	ELFCLASSNONE	0		/* EI_CLASS */
331 #define	ELFCLASS32	1
332 #define	ELFCLASS64	2
333 #define	ELFCLASSNUM	3
334 
335 #define ELFDATANONE	0		/* e_ident[EI_DATA] */
336 #define ELFDATA2LSB	1
337 #define ELFDATA2MSB	2
338 
339 #define EV_NONE		0		/* e_version, EI_VERSION */
340 #define EV_CURRENT	1
341 #define EV_NUM		2
342 
343 #define ELFOSABI_NONE	0
344 #define ELFOSABI_LINUX	3
345 
346 #ifndef ELF_OSABI
347 #define ELF_OSABI ELFOSABI_NONE
348 #endif
349 
350 /* Notes used in ET_CORE */
351 #define NT_PRSTATUS	1
352 #define NT_PRFPREG	2
353 #define NT_PRPSINFO	3
354 #define NT_TASKSTRUCT	4
355 #define NT_AUXV		6
356 #define NT_PRXFPREG     0x46e62b7f      /* copied from gdb5.1/include/elf/common.h */
357 #define NT_PPC_VMX	0x100		/* PowerPC Altivec/VMX registers */
358 
359 
360 /* Note header in a PT_NOTE section */
361 typedef struct elf32_note {
362   Elf32_Word	n_namesz;	/* Name size */
363   Elf32_Word	n_descsz;	/* Content size */
364   Elf32_Word	n_type;		/* Content type */
365 } Elf32_Nhdr;
366 
367 /* Note header in a PT_NOTE section */
368 typedef struct elf64_note {
369   Elf64_Word n_namesz;	/* Name size */
370   Elf64_Word n_descsz;	/* Content size */
371   Elf64_Word n_type;	/* Content type */
372 } Elf64_Nhdr;
373 
374 #if ELF_CLASS == ELFCLASS32
375 
376 extern Elf32_Dyn _DYNAMIC [];
377 #define elfhdr		elf32_hdr
378 #define elf_phdr	elf32_phdr
379 #define elf_note	elf32_note
380 #define elf_addr_t	Elf32_Off
381 
382 #else
383 
384 extern Elf64_Dyn _DYNAMIC [];
385 #define elfhdr		elf64_hdr
386 #define elf_phdr	elf64_phdr
387 #define elf_note	elf64_note
388 #define elf_addr_t	Elf64_Off
389 
390 #endif
391 
392 /* Optional callbacks to write extra ELF notes. */
393 #ifndef ARCH_HAVE_EXTRA_ELF_NOTES
394 static inline int elf_coredump_extra_notes_size(void) { return 0; }
395 static inline int elf_coredump_extra_notes_write(struct file *file,
396 			loff_t *foffset) { return 0; }
397 #else
398 extern int elf_coredump_extra_notes_size(void);
399 extern int elf_coredump_extra_notes_write(struct file *file, loff_t *foffset);
400 #endif
401 
402 #endif /* _LINUX_ELF_H */
403