1/*
2 * ELF data structures and values
3 */
4
5typedef unsigned short	Elf32_Half;
6typedef unsigned int	Elf32_Word;
7typedef signed int	Elf32_Sword;
8typedef unsigned int	Elf32_Off;
9typedef unsigned int	Elf32_Addr;
10typedef unsigned char	Elf_Char;
11
12typedef unsigned short	Elf64_Half;
13typedef unsigned int	Elf64_Word;
14typedef signed int	Elf64_Sword;
15typedef unsigned long long Elf64_Off;
16typedef unsigned long long Elf64_Addr;
17typedef unsigned long long Elf64_Xword;
18typedef signed long long   Elf64_Sxword;
19
20
21/*
22 * File Header
23 */
24
25#define EI_NIDENT	16
26
27#define EI_MINHDR	24	/* enough for fields up to e_version */
28
29typedef struct {
30    Elf_Char	e_ident[EI_NIDENT];
31    Elf32_Half	e_type;
32    Elf32_Half	e_machine;
33    Elf32_Word	e_version;
34    Elf32_Addr	e_entry;
35    Elf32_Off	e_phoff;
36    Elf32_Off	e_shoff;
37    Elf32_Word	e_flags;
38    Elf32_Half	e_ehsize;
39    Elf32_Half	e_phentsize;
40    Elf32_Half	e_phnum;
41    Elf32_Half	e_shentsize;
42    Elf32_Half	e_shnum;
43    Elf32_Half	e_shstrndx;
44} Elf32_Ehdr;
45
46typedef struct {
47    Elf_Char	e_ident[EI_NIDENT];
48    Elf64_Half e_type;
49    Elf64_Half e_machine;
50    Elf64_Word e_version;
51    Elf64_Addr e_entry;
52    Elf64_Off e_phoff;
53    Elf64_Off e_shoff;
54    Elf64_Word e_flags;
55    Elf64_Half e_ehsize;
56    Elf64_Half e_phentsize;
57    Elf64_Half e_phnum;
58    Elf64_Half e_shentsize;
59    Elf64_Half e_shnum;
60    Elf64_Half e_shstrndx;
61} Elf64_Ehdr;
62
63/* The constant below should refer to whichever structure is bigger! */
64#define ELF_EHDRSIZE (sizeof(Elf64_Ehdr))
65
66
67
68/* e_indent */
69#define EI_MAG0		0		/* File identification byte 0 index */
70#define EI_MAG1		1		/* File identification byte 1 index */
71#define EI_MAG2		2		/* File identification byte 2 index */
72#define EI_MAG3		3		/* File identification byte 3 index */
73#define EI_CLASS	4		/* File class */
74#define  ELFCLASSNONE	 0		 /* Invalid class */
75#define  ELFCLASS32	 1		 /* 32-bit objects */
76#define  ELFCLASS64	 2		 /* 64-bit objects */
77#define EI_DATA		5		/* Data encoding */
78#define  ELFDATANONE	 0		 /* Invalid data encoding */
79#define  ELFDATA2LSB	 1		 /* 2's complement, little endian */
80#define  ELFDATA2MSB	 2		 /* 2's complement, big endian */
81#define EI_VERSION	6		/* File version */
82#define EI_PAD		7		/* Start of padding bytes */
83
84#define ELFMAG0		0x7F		/* Magic number byte 0 */
85#define ELFMAG1		'E'		/* Magic number byte 1 */
86#define ELFMAG2		'L'		/* Magic number byte 2 */
87#define ELFMAG3		'F'		/* Magic number byte 3 */
88
89/* e_type */
90#define ET_NONE		0		/* No file type */
91#define ET_REL		1		/* Relocatable file */
92#define ET_EXEC		2		/* Executable file */
93#define ET_DYN		3		/* Shared object file */
94#define ET_CORE		4		/* Core file */
95#define ET_LOPROC	0xFF00		/* Processor-specific */
96#define ET_HIPROC	0xFFFF		/* Processor-specific */
97
98/* e_machine */
99#define EM_NONE		0		/* No machine */
100#define EM_M32		1		/* AT&T WE 32100 */
101#define EM_SPARC	2		/* SUN SPARC */
102#define EM_386		3		/* Intel 80386 */
103#define EM_68K		4		/* Motorola m68k family */
104#define EM_88K		5		/* Motorola m88k family */
105#define EM_860		7		/* Intel 80860 */
106#define EM_MIPS		8		/* MIPS R3000  */
107#define EM_PPC		20		/* PowerPC */
108#define EM_PPC64	21		/* PowerPC (64-bit) */
109#define EM_ARM		40		/* ARM */
110
111/* e_version */
112#define EV_NONE		0		/* Invalid ELF version */
113#define EV_CURRENT	1		/* Current version */
114
115
116/*
117 * Program Header
118 */
119typedef struct {
120  Elf32_Word	p_type;			/* Identifies program segment type */
121  Elf32_Off	p_offset;		/* Segment file offset */
122  Elf32_Addr	p_vaddr;		/* Segment virtual address */
123  Elf32_Addr	p_paddr;		/* Segment physical address */
124  Elf32_Word	p_filesz;		/* Segment size in file */
125  Elf32_Word	p_memsz;		/* Segment size in memory */
126  Elf32_Word	p_flags;		/* Segment flags */
127  Elf32_Word	p_align;		/* Segment alignment, file & memory */
128} Elf32_Phdr;
129
130typedef struct {
131    Elf64_Word p_type;
132    Elf64_Word p_flags;
133    Elf64_Off p_offset;
134    Elf64_Addr p_vaddr;
135    Elf64_Addr p_paddr;
136    Elf64_Xword p_filesz;
137    Elf64_Xword p_memsz;
138    Elf64_Xword p_align;
139} Elf64_Phdr;
140
141
142/* p_type */
143#define	PT_NULL		0		/* Program header table entry unused */
144#define PT_LOAD		1		/* Loadable program segment */
145#define PT_DYNAMIC	2		/* Dynamic linking information */
146#define PT_INTERP	3		/* Program interpreter */
147#define PT_NOTE		4		/* Auxiliary information */
148#define PT_SHLIB	5		/* Reserved, unspecified semantics */
149#define PT_PHDR		6		/* Entry for header table itself */
150#define PT_LOPROC	0x70000000	/* Processor-specific */
151#define PT_HIPROC	0x7FFFFFFF	/* Processor-specific */
152
153/* p_flags */
154#define PF_X		(1 << 0)	/* Segment is executable */
155#define PF_W		(1 << 1)	/* Segment is writable */
156#define PF_R		(1 << 2)	/* Segment is readable */
157#define PF_MASKPROC	0xF0000000	/* Processor-specific reserved bits */
158
159
160/*
161 * Section Header
162 */
163typedef struct {
164    Elf32_Word	sh_name;
165    Elf32_Word	sh_type;
166    Elf32_Word	sh_flags;
167    Elf32_Addr	sh_addr;
168    Elf32_Off	sh_offset;
169    Elf32_Word	sh_size;
170    Elf32_Word	sh_link;
171    Elf32_Word	sh_info;
172    Elf32_Word	sh_addralign;
173    Elf32_Word	sh_entsize;
174} Elf32_Shdr;
175
176/* sh_type */
177#define SHT_NULL	0		/* Section header table entry unused */
178#define SHT_PROGBITS	1		/* Program specific (private) data */
179#define SHT_SYMTAB	2		/* Link editing symbol table */
180#define SHT_STRTAB	3		/* A string table */
181#define SHT_RELA	4		/* Relocation entries with addends */
182#define SHT_HASH	5		/* A symbol hash table */
183#define SHT_DYNAMIC	6		/* Information for dynamic linking */
184#define SHT_NOTE	7		/* Information that marks file */
185#define SHT_NOBITS	8		/* Section occupies no space in file */
186#define SHT_REL		9		/* Relocation entries, no addends */
187#define SHT_SHLIB	10		/* Reserved, unspecified semantics */
188#define SHT_DYNSYM	11		/* Dynamic linking symbol table */
189#define SHT_LOPROC	0x70000000	/* Processor-specific semantics, lo */
190#define SHT_HIPROC	0x7FFFFFFF	/* Processor-specific semantics, hi */
191#define SHT_LOUSER	0x80000000	/* Application-specific semantics */
192#define SHT_HIUSER	0x8FFFFFFF	/* Application-specific semantics */
193
194/* sh_flags */
195#define SHF_WRITE	(1 << 0)	/* Writable data during execution */
196#define SHF_ALLOC	(1 << 1)	/* Occupies memory during execution */
197#define SHF_EXECINSTR	(1 << 2)	/* Executable machine instructions */
198#define SHF_MASKPROC	0xF0000000	/* Processor-specific semantics */
199
200
201/*
202 * Symbol Table
203 */
204typedef struct {
205    Elf32_Word	st_name;
206    Elf32_Addr	st_value;
207    Elf32_Word	st_size;
208    Elf_Char	st_info;
209    Elf_Char	st_other;
210    Elf32_Half	st_shndx;
211} Elf32_Sym;
212
213
214#define ELF_ST_BIND(val)		(((unsigned int)(val)) >> 4)
215#define ELF_ST_TYPE(val)		((val) & 0xF)
216#define ELF_ST_INFO(bind,type)		(((bind) << 4) | ((type) & 0xF))
217
218/* symbol binding */
219#define STB_LOCAL	0		/* Symbol not visible outside obj */
220#define STB_GLOBAL	1		/* Symbol visible outside obj */
221#define STB_WEAK	2		/* Like globals, lower precedence */
222#define STB_LOPROC	13		/* Application-specific semantics */
223#define STB_HIPROC	15		/* Application-specific semantics */
224
225/* symbol type */
226#define STT_NOTYPE	0		/* Symbol type is unspecified */
227#define STT_OBJECT	1		/* Symbol is a data object */
228#define STT_FUNC	2		/* Symbol is a code object */
229#define STT_SECTION	3		/* Symbol associated with a section */
230#define STT_FILE	4		/* Symbol gives a file name */
231#define STT_LOPROC	13		/* Application-specific semantics */
232#define STT_HIPROC	15		/* Application-specific semantics */
233
234/* special values st_shndx */
235#define SHN_UNDEF	0		/* Undefined section reference */
236#define SHN_LORESERV	0xFF00		/* Begin range of reserved indices */
237#define SHN_LOPROC	0xFF00		/* Begin range of appl-specific */
238#define SHN_HIPROC	0xFF1F		/* End range of appl-specific */
239#define SHN_ABS		0xFFF1		/* Associated symbol is absolute */
240#define SHN_COMMON	0xFFF2		/* Associated symbol is in common */
241#define SHN_HIRESERVE	0xFFFF		/* End range of reserved indices */
242