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