My Kernel v0.1.0
elf32.h
1/*
2 * @file elf32.h
3 * @brief Standard ELF32 structures definitions
4 *
5 * @see https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-46512.html
6 *
7 * TODO: relocation
8 */
9
10#ifndef ELF32_H
11#define ELF32_H
12
13#include <stdint.h>
14
15typedef uint16_t elf32_half;
16typedef uint32_t elf32_word;
17typedef int32_t elf32_sword;
18typedef uint32_t elf32_off;
19typedef uint32_t elf32_addr;
20
22struct elf32_ehdr {
23 uint8_t e_ident[16];
24 elf32_half e_type;
25 elf32_half e_machine;
26 elf32_word e_version;
27 elf32_word e_entry;
28 elf32_off e_phoff;
29 elf32_off e_shoff;
30 elf32_word e_flags;
31 elf32_half e_ehsize;
32 elf32_half e_phentsize;
33 elf32_half e_phnum;
34 elf32_half e_shentsize;
35 elf32_half e_shnum;
36 elf32_half e_shstrndx;
38};
39
41typedef enum {
42 EI_MAG0 = 0, // 0x7F
43 EI_MAG1 = 1, // 'E'
44 EI_MAG2 = 2, // 'L'
45 EI_MAG3 = 3, // 'F'
46 EI_CLASS = 4, // Architecture (32/64)
47 EI_DATA = 5, // Byte Order
48 EI_VERSION = 6, // ELF Version
49 EI_OSABI = 7, // OS Specific
50 EI_ABIVERSION = 8, // OS Specific
51 EI_PAD = 9, // Padding
52 EI_NIDENT = 16,
53} e_ident;
54
56typedef enum {
57 ET_NONE = 0,
58 ET_REL = 1,
59 ET_EXEC = 2,
60 ET_DYN = 3,
61 ET_CORE = 4
62} e_type;
63
65typedef enum {
66 EI_MAG_0 = 0x7f,
67 EI_MAG_1 = 'E',
68 EI_MAG_2 = 'L',
69 EI_MAG_3 = 'F',
70} ei_mag;
71
73typedef enum {
74 EI_CLASS_32 = 1,
75 EI_CLASS_64 = 2,
76} ei_class;
77
79typedef enum {
80 EI_DATA_LSB = 1,
81 EI_DATA_MSB = 2,
82} ei_data;
83
85typedef enum {
86 EM_NONE = 0,
87 EM_386 = 3,
88} e_machine;
89
90/***/
91typedef enum {
92 EV_NONE = 0,
93 EV_CURRENT = 1,
94} e_version;
95
103 elf32_word sh_name;
104 elf32_word sh_type;
105 elf32_word sh_flags;
106 elf32_addr sh_addr;
107 elf32_off sh_offset;
108 elf32_word sh_size;
109 elf32_word sh_link;
110 elf32_word sh_info;
111 elf32_word sh_addralign;
113 elf32_word sh_entsize;
114};
115
117typedef enum {
118 SHN_UNDEF = 0,
119} sh_index;
120
121typedef enum {
122 SHT_NULL = 0,
123 SHT_PROGBITS = 1,
124 SHT_SYMTAB = 2,
125 SHT_STRTAB = 3,
126 SHT_RELA = 4,
127 SHT_NOBITS = 8,
128 SHT_REL = 9,
129} sh_type;
130
131typedef enum {
132 SHF_WRITE = 0x1,
133 SHF_ALLOC = 0x2,
134 SHF_EXECINSTR = 0x4,
135 SHF_MASKPROC = 0xf0000000,
136} sh_flags;
137
142struct elf32_sym {
143 elf32_word st_name;
144 elf32_addr st_value;
145 elf32_word st_size;
146 unsigned char st_info;
147 unsigned char st_other;
148 elf32_half st_shndx;
149};
150
151#define ELF32_ST_BIND(info) ((info) >> 4)
152#define ELF32_ST_TYPE(info) ((info)&0xf)
153#define ELF32_ST_INFO(bind, type) (((bind) << 4) | ((type)&0xf))
154
155typedef enum {
156 STB_LOCAL = 0,
157 STB_GLOBAL = 1,
158 STB_WEAK = 2,
160} st_bind;
161
162typedef enum {
163 STT_NOTYPE = 0,
164 STT_OBJECT = 1,
165 STT_FUNC = 2,
166 STT_SECTION = 3,
167 STT_FILE = 4,
168} st_type;
169
183 elf32_word p_type;
184 elf32_off p_offset;
185 elf32_addr p_vaddr;
186 elf32_addr p_paddr;
187 elf32_word p_filesz;
188 elf32_word p_memsz;
189 elf32_word p_pflags;
190 elf32_word p_align;
191};
192
194typedef enum {
195 PT_NULL = 0,
196 PT_LOAD = 1,
197 PT_DYNAMIC = 2,
198 PT_INTERP = 3,
199 PT_NOTE = 4,
200 PT_SHLIB = 5,
201 PT_PHDR = 6,
202} p_type;
203
205typedef enum {
206 PF_X = 0x1,
207 PF_W = 0x2,
208 PF_R = 0x4,
209} p_flags;
210
211#endif /* ELF32_H */
ELF file header.
Definition: elf32.h:22
elf32_off e_shoff
Offset to the section header table.
Definition: elf32.h:29
elf32_half e_machine
Machine type.
Definition: elf32.h:25
elf32_half e_phentsize
Size of a entry inside the program header table.
Definition: elf32.h:32
elf32_word e_flags
Processor specific flags.
Definition: elf32.h:30
elf32_half e_phnum
Number of entries inside the program header table.
Definition: elf32.h:33
elf32_half e_shentsize
Size of a section table entry.
Definition: elf32.h:34
elf32_half e_ehsize
The size of this header.
Definition: elf32.h:31
elf32_half e_shstrndx
Section header index of the string table for section header names.
Definition: elf32.h:36
elf32_word e_version
Must contain EV_CURRENT.
Definition: elf32.h:26
elf32_half e_shnum
Number of sections.
Definition: elf32.h:35
elf32_word e_entry
Virtual address of the program's entrypoint.
Definition: elf32.h:27
elf32_half e_type
Object file type.
Definition: elf32.h:24
elf32_off e_phoff
Offset to the program header table.
Definition: elf32.h:28
ELF Program header entry (segment)
Definition: elf32.h:182
elf32_addr p_paddr
Segment's physical address in memory.
Definition: elf32.h:186
elf32_word p_memsz
Size of this segment inside memory.
Definition: elf32.h:188
elf32_off p_offset
Offset to the segment's first byte in the file.
Definition: elf32.h:184
elf32_addr p_vaddr
Segment's virtual address in memory.
Definition: elf32.h:185
elf32_word p_align
Alignment requirement for the virtual load address.
Definition: elf32.h:190
elf32_word p_type
The segment's type.
Definition: elf32.h:183
elf32_word p_filesz
Size of this segment inside the file.
Definition: elf32.h:187
elf32_word p_pflags
A combination of p_pflags.
Definition: elf32.h:189
ELF section header.
Definition: elf32.h:102
elf32_addr sh_addr
Offset to the section's location inside memory.
Definition: elf32.h:106
elf32_word sh_name
Offset to this section's name inside the name table.
Definition: elf32.h:103
elf32_word sh_type
Section type.
Definition: elf32.h:104
elf32_word sh_flags
1-bit flag that describes the section's attributes
Definition: elf32.h:105
elf32_word sh_size
Size of the section.
Definition: elf32.h:108
elf32_word sh_addralign
Alignment requirement for the section's starting address.
Definition: elf32.h:111
elf32_off sh_offset
Offset to the section's location inside the file.
Definition: elf32.h:107
Symbol table entry.
Definition: elf32.h:142
elf32_half st_shndx
Index of the section containing the symbol.
Definition: elf32.h:148
elf32_word st_name
Symbol name's offset inside the symbol string table.
Definition: elf32.h:143
elf32_addr st_value
Can be an absolute value, address, etc (context)
Definition: elf32.h:144
unsigned char st_other
No meaning. Always 0.
Definition: elf32.h:147
unsigned char st_info
Symbol's type and binding attributes.
Definition: elf32.h:146
elf32_word st_size
Data object's size (if any)
Definition: elf32.h:145