1// Elf.h
2
3#ifndef _ELF_H
4#define _ELF_H
5
6// types
7typedef uint32		Elf32_Addr;
8typedef uint16		Elf32_Half;
9typedef uint32		Elf32_Off;
10typedef int32		Elf32_Sword;
11typedef uint32		Elf32_Word;
12
13// e_ident indices
14#define EI_MAG0		0
15#define EI_MAG1		1
16#define EI_MAG2		2
17#define EI_MAG3		3
18#define EI_CLASS	4
19#define EI_DATA		5
20#define EI_VERSION	6
21#define EI_PAD		7
22#define EI_NIDENT	16
23
24// ELF magic
25#define ELFMAG0		0x7f
26#define ELFMAG1		'E'
27#define ELFMAG2		'L'
28#define ELFMAG3		'F'
29
30// class
31#define ELFCLASSNONE	0
32#define ELFCLASS32		1
33#define ELFCLASS64		2
34
35// version
36#define EV_NONE			0
37#define EV_CURRENT		1
38
39// object file header
40typedef struct {
41	unsigned char	e_ident[EI_NIDENT];
42	Elf32_Half		e_type;
43	Elf32_Half		e_machine;
44	Elf32_Word		e_version;
45	Elf32_Addr		e_entry;
46	Elf32_Off		e_phoff;
47	Elf32_Off		e_shoff;
48	Elf32_Word		e_flags;
49	Elf32_Half		e_ehsize;
50	Elf32_Half		e_phentsize;
51	Elf32_Half		e_phnum;
52	Elf32_Half		e_shentsize;
53	Elf32_Half		e_shnum;
54	Elf32_Half		e_shstrndx;
55} Elf32_Ehdr;
56
57// e_ident EI_CLASS and EI_DATA values
58#define ELFCLASSNONE	0
59#define ELFCLASS32		1
60#define ELFCLASS64		2
61#define ELFDATANONE		0
62#define ELFDATA2LSB		1
63#define ELFDATA2MSB		2
64
65// special section header indices
66#define SHN_UNDEF		0
67#define SHN_LORESERVE	0xff00
68#define SHN_LOPROC		0xff00
69#define SHN_HIPROC		0xff1f
70#define SHN_ABS			0xfff1
71#define SHN_COMMON		0xfff2
72#define SHN_HIRESERVE	0xffff
73
74// program header
75typedef struct {
76	Elf32_Word	p_type;
77	Elf32_Off	p_offset;
78	Elf32_Addr	p_vaddr;
79	Elf32_Addr	p_paddr;
80	Elf32_Word	p_filesz;
81	Elf32_Word	p_memsz;
82	Elf32_Word	p_flags;
83	Elf32_Word	p_align;
84} Elf32_Phdr;
85
86// p_type
87#define PT_NULL		0
88#define PT_LOAD		1
89#define PT_DYNAMIC	2
90#define PT_INTERP	3
91#define PT_NOTE		4
92#define PT_SHLIB	5
93#define PT_PHDIR	6
94#define PT_LOPROC	0x70000000
95#define PT_HIPROC	0x7fffffff
96
97// section header
98typedef struct {
99	Elf32_Word	sh_name;
100	Elf32_Word	sh_type;
101	Elf32_Word	sh_flags;
102	Elf32_Addr	sh_addr;
103	Elf32_Off	sh_offset;
104	Elf32_Word	sh_size;
105	Elf32_Word	sh_link;
106	Elf32_Word	sh_info;
107	Elf32_Word	sh_addralign;
108	Elf32_Word	sh_entsize;
109} Elf32_Shdr;
110
111// sh_type values
112#define SHT_NULL		0
113#define SHT_PROGBITS	1
114#define SHT_SYMTAB		2
115#define SHT_STRTAB		3
116#define SHT_RELA		4
117#define SHT_HASH		5
118#define SHT_DYNAMIC		6
119#define SHT_NOTE		7
120#define SHT_NOBITS		8
121#define SHT_REL			9
122#define SHT_SHLIB		10
123#define SHT_DYNSYM		11
124#define SHT_LOPROC		0x70000000
125#define SHT_HIPROC		0x7fffffff
126#define SHT_LOUSER		0x80000000
127#define SHT_HIUSER		0xffffffff
128
129// relocation entry
130typedef struct {
131	Elf32_Addr	r_offset;
132	Elf32_Word	r_info;
133} Elf32_Rel;
134
135// relocation entry with addend
136typedef struct {
137	Elf32_Addr	r_offset;
138	Elf32_Word	r_info;
139	Elf32_Sword	r_addend;
140} Elf32_Rela;
141
142// r_info accessors
143#define ELF32_R_SYM(i)		((i) >> 8)
144#define ELF32_R_TYPE(i)		((unsigned char)(i))
145#define ELF32_R_INFO(s, t)	((s) << 8 + (unsigned char)(t))
146
147// relocation types (i368 specific)
148#define R_386_NONE		0
149#define R_386_32		1
150#define R_386_PC32		2
151#define R_386_GOT32		3
152#define R_386_PLT32		4
153#define R_386_COPY		5
154#define R_386_GLOB_DAT	6
155#define R_386_JMP_SLOT	7
156#define R_386_RELATIVE	8
157#define R_386_GOTOFF	9
158#define R_386_GOTPC		10
159
160// symbol table entry
161typedef struct {
162	Elf32_Word	st_name;
163	Elf32_Addr	st_value;
164	Elf32_Word	st_size;
165	Elf32_Word	st_info;
166	Elf32_Word	st_other;
167	Elf32_Word	st_shndx;
168} Elf32_Sym;
169
170// st_info accessors
171#define ELF32_ST_BIND(i)	((i) >> 4)
172#define ELF32_ST_TYPE(i)	((i) & 0xf)
173#define ELF32_ST_INFO(b, t)	((b) << 4 + (t) & 0xf)
174
175// symbol binding
176#define STB_LOCAL		0
177#define STB_GLOBAL		1
178#define STB_WEAK		2
179#define STB_LOPROC		13
180#define STB_HIPROC		15
181
182// symbol types
183#define STT_NOTYPE		0
184#define STT_OBJECT		1
185#define STT_FUNC		2
186#define STT_SECTION		3
187#define STT_FILE		4
188#define STT_LOPROC		13
189#define STT_HIPROC		15
190
191
192#endif	// _ELF_H
193