xref: /linux-6.15/include/linux/elf.h (revision 606d099c)
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_LOPROC	0x70000000
87 #define DT_HIPROC	0x7fffffff
88 
89 /* This info is needed when parsing the symbol table */
90 #define STB_LOCAL  0
91 #define STB_GLOBAL 1
92 #define STB_WEAK   2
93 
94 #define STT_NOTYPE  0
95 #define STT_OBJECT  1
96 #define STT_FUNC    2
97 #define STT_SECTION 3
98 #define STT_FILE    4
99 #define STT_COMMON  5
100 #define STT_TLS     6
101 
102 #define ELF_ST_BIND(x)		((x) >> 4)
103 #define ELF_ST_TYPE(x)		(((unsigned int) x) & 0xf)
104 #define ELF32_ST_BIND(x)	ELF_ST_BIND(x)
105 #define ELF32_ST_TYPE(x)	ELF_ST_TYPE(x)
106 #define ELF64_ST_BIND(x)	ELF_ST_BIND(x)
107 #define ELF64_ST_TYPE(x)	ELF_ST_TYPE(x)
108 
109 typedef struct dynamic{
110   Elf32_Sword d_tag;
111   union{
112     Elf32_Sword	d_val;
113     Elf32_Addr	d_ptr;
114   } d_un;
115 } Elf32_Dyn;
116 
117 typedef struct {
118   Elf64_Sxword d_tag;		/* entry tag value */
119   union {
120     Elf64_Xword d_val;
121     Elf64_Addr d_ptr;
122   } d_un;
123 } Elf64_Dyn;
124 
125 /* The following are used with relocations */
126 #define ELF32_R_SYM(x) ((x) >> 8)
127 #define ELF32_R_TYPE(x) ((x) & 0xff)
128 
129 #define ELF64_R_SYM(i)			((i) >> 32)
130 #define ELF64_R_TYPE(i)			((i) & 0xffffffff)
131 
132 typedef struct elf32_rel {
133   Elf32_Addr	r_offset;
134   Elf32_Word	r_info;
135 } Elf32_Rel;
136 
137 typedef struct elf64_rel {
138   Elf64_Addr r_offset;	/* Location at which to apply the action */
139   Elf64_Xword r_info;	/* index and type of relocation */
140 } Elf64_Rel;
141 
142 typedef struct elf32_rela{
143   Elf32_Addr	r_offset;
144   Elf32_Word	r_info;
145   Elf32_Sword	r_addend;
146 } Elf32_Rela;
147 
148 typedef struct elf64_rela {
149   Elf64_Addr r_offset;	/* Location at which to apply the action */
150   Elf64_Xword r_info;	/* index and type of relocation */
151   Elf64_Sxword r_addend;	/* Constant addend used to compute value */
152 } Elf64_Rela;
153 
154 typedef struct elf32_sym{
155   Elf32_Word	st_name;
156   Elf32_Addr	st_value;
157   Elf32_Word	st_size;
158   unsigned char	st_info;
159   unsigned char	st_other;
160   Elf32_Half	st_shndx;
161 } Elf32_Sym;
162 
163 typedef struct elf64_sym {
164   Elf64_Word st_name;		/* Symbol name, index in string tbl */
165   unsigned char	st_info;	/* Type and binding attributes */
166   unsigned char	st_other;	/* No defined meaning, 0 */
167   Elf64_Half st_shndx;		/* Associated section index */
168   Elf64_Addr st_value;		/* Value of the symbol */
169   Elf64_Xword st_size;		/* Associated symbol size */
170 } Elf64_Sym;
171 
172 
173 #define EI_NIDENT	16
174 
175 typedef struct elf32_hdr{
176   unsigned char	e_ident[EI_NIDENT];
177   Elf32_Half	e_type;
178   Elf32_Half	e_machine;
179   Elf32_Word	e_version;
180   Elf32_Addr	e_entry;  /* Entry point */
181   Elf32_Off	e_phoff;
182   Elf32_Off	e_shoff;
183   Elf32_Word	e_flags;
184   Elf32_Half	e_ehsize;
185   Elf32_Half	e_phentsize;
186   Elf32_Half	e_phnum;
187   Elf32_Half	e_shentsize;
188   Elf32_Half	e_shnum;
189   Elf32_Half	e_shstrndx;
190 } Elf32_Ehdr;
191 
192 typedef struct elf64_hdr {
193   unsigned char	e_ident[16];		/* ELF "magic number" */
194   Elf64_Half e_type;
195   Elf64_Half e_machine;
196   Elf64_Word e_version;
197   Elf64_Addr e_entry;		/* Entry point virtual address */
198   Elf64_Off e_phoff;		/* Program header table file offset */
199   Elf64_Off e_shoff;		/* Section header table file offset */
200   Elf64_Word e_flags;
201   Elf64_Half e_ehsize;
202   Elf64_Half e_phentsize;
203   Elf64_Half e_phnum;
204   Elf64_Half e_shentsize;
205   Elf64_Half e_shnum;
206   Elf64_Half e_shstrndx;
207 } Elf64_Ehdr;
208 
209 /* These constants define the permissions on sections in the program
210    header, p_flags. */
211 #define PF_R		0x4
212 #define PF_W		0x2
213 #define PF_X		0x1
214 
215 typedef struct elf32_phdr{
216   Elf32_Word	p_type;
217   Elf32_Off	p_offset;
218   Elf32_Addr	p_vaddr;
219   Elf32_Addr	p_paddr;
220   Elf32_Word	p_filesz;
221   Elf32_Word	p_memsz;
222   Elf32_Word	p_flags;
223   Elf32_Word	p_align;
224 } Elf32_Phdr;
225 
226 typedef struct elf64_phdr {
227   Elf64_Word p_type;
228   Elf64_Word p_flags;
229   Elf64_Off p_offset;		/* Segment file offset */
230   Elf64_Addr p_vaddr;		/* Segment virtual address */
231   Elf64_Addr p_paddr;		/* Segment physical address */
232   Elf64_Xword p_filesz;		/* Segment size in file */
233   Elf64_Xword p_memsz;		/* Segment size in memory */
234   Elf64_Xword p_align;		/* Segment alignment, file & memory */
235 } Elf64_Phdr;
236 
237 /* sh_type */
238 #define SHT_NULL	0
239 #define SHT_PROGBITS	1
240 #define SHT_SYMTAB	2
241 #define SHT_STRTAB	3
242 #define SHT_RELA	4
243 #define SHT_HASH	5
244 #define SHT_DYNAMIC	6
245 #define SHT_NOTE	7
246 #define SHT_NOBITS	8
247 #define SHT_REL		9
248 #define SHT_SHLIB	10
249 #define SHT_DYNSYM	11
250 #define SHT_NUM		12
251 #define SHT_LOPROC	0x70000000
252 #define SHT_HIPROC	0x7fffffff
253 #define SHT_LOUSER	0x80000000
254 #define SHT_HIUSER	0xffffffff
255 
256 /* sh_flags */
257 #define SHF_WRITE	0x1
258 #define SHF_ALLOC	0x2
259 #define SHF_EXECINSTR	0x4
260 #define SHF_MASKPROC	0xf0000000
261 
262 /* special section indexes */
263 #define SHN_UNDEF	0
264 #define SHN_LORESERVE	0xff00
265 #define SHN_LOPROC	0xff00
266 #define SHN_HIPROC	0xff1f
267 #define SHN_ABS		0xfff1
268 #define SHN_COMMON	0xfff2
269 #define SHN_HIRESERVE	0xffff
270 
271 typedef struct {
272   Elf32_Word	sh_name;
273   Elf32_Word	sh_type;
274   Elf32_Word	sh_flags;
275   Elf32_Addr	sh_addr;
276   Elf32_Off	sh_offset;
277   Elf32_Word	sh_size;
278   Elf32_Word	sh_link;
279   Elf32_Word	sh_info;
280   Elf32_Word	sh_addralign;
281   Elf32_Word	sh_entsize;
282 } Elf32_Shdr;
283 
284 typedef struct elf64_shdr {
285   Elf64_Word sh_name;		/* Section name, index in string tbl */
286   Elf64_Word sh_type;		/* Type of section */
287   Elf64_Xword sh_flags;		/* Miscellaneous section attributes */
288   Elf64_Addr sh_addr;		/* Section virtual addr at execution */
289   Elf64_Off sh_offset;		/* Section file offset */
290   Elf64_Xword sh_size;		/* Size of section in bytes */
291   Elf64_Word sh_link;		/* Index of another section */
292   Elf64_Word sh_info;		/* Additional section information */
293   Elf64_Xword sh_addralign;	/* Section alignment */
294   Elf64_Xword sh_entsize;	/* Entry size if section holds table */
295 } Elf64_Shdr;
296 
297 #define	EI_MAG0		0		/* e_ident[] indexes */
298 #define	EI_MAG1		1
299 #define	EI_MAG2		2
300 #define	EI_MAG3		3
301 #define	EI_CLASS	4
302 #define	EI_DATA		5
303 #define	EI_VERSION	6
304 #define	EI_OSABI	7
305 #define	EI_PAD		8
306 
307 #define	ELFMAG0		0x7f		/* EI_MAG */
308 #define	ELFMAG1		'E'
309 #define	ELFMAG2		'L'
310 #define	ELFMAG3		'F'
311 #define	ELFMAG		"\177ELF"
312 #define	SELFMAG		4
313 
314 #define	ELFCLASSNONE	0		/* EI_CLASS */
315 #define	ELFCLASS32	1
316 #define	ELFCLASS64	2
317 #define	ELFCLASSNUM	3
318 
319 #define ELFDATANONE	0		/* e_ident[EI_DATA] */
320 #define ELFDATA2LSB	1
321 #define ELFDATA2MSB	2
322 
323 #define EV_NONE		0		/* e_version, EI_VERSION */
324 #define EV_CURRENT	1
325 #define EV_NUM		2
326 
327 #define ELFOSABI_NONE	0
328 #define ELFOSABI_LINUX	3
329 
330 #ifndef ELF_OSABI
331 #define ELF_OSABI ELFOSABI_NONE
332 #endif
333 
334 /* Notes used in ET_CORE */
335 #define NT_PRSTATUS	1
336 #define NT_PRFPREG	2
337 #define NT_PRPSINFO	3
338 #define NT_TASKSTRUCT	4
339 #define NT_AUXV		6
340 #define NT_PRXFPREG     0x46e62b7f      /* copied from gdb5.1/include/elf/common.h */
341 
342 
343 /* Note header in a PT_NOTE section */
344 typedef struct elf32_note {
345   Elf32_Word	n_namesz;	/* Name size */
346   Elf32_Word	n_descsz;	/* Content size */
347   Elf32_Word	n_type;		/* Content type */
348 } Elf32_Nhdr;
349 
350 /* Note header in a PT_NOTE section */
351 typedef struct elf64_note {
352   Elf64_Word n_namesz;	/* Name size */
353   Elf64_Word n_descsz;	/* Content size */
354   Elf64_Word n_type;	/* Content type */
355 } Elf64_Nhdr;
356 
357 #if ELF_CLASS == ELFCLASS32
358 
359 extern Elf32_Dyn _DYNAMIC [];
360 #define elfhdr		elf32_hdr
361 #define elf_phdr	elf32_phdr
362 #define elf_note	elf32_note
363 #define elf_addr_t	Elf32_Off
364 
365 #else
366 
367 extern Elf64_Dyn _DYNAMIC [];
368 #define elfhdr		elf64_hdr
369 #define elf_phdr	elf64_phdr
370 #define elf_note	elf64_note
371 #define elf_addr_t	Elf64_Off
372 
373 #endif
374 
375 #ifndef ARCH_HAVE_EXTRA_ELF_NOTES
376 static inline int arch_notes_size(void) { return 0; }
377 static inline void arch_write_notes(struct file *file) { }
378 
379 #define ELF_CORE_EXTRA_NOTES_SIZE arch_notes_size()
380 #define ELF_CORE_WRITE_EXTRA_NOTES arch_write_notes(file)
381 #endif /* ARCH_HAVE_EXTRA_ELF_NOTES */
382 
383 #endif /* _LINUX_ELF_H */
384