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