axle OS
x86_32 UNIX-like hobby OS
elf.h
1 #ifndef ELF_H
2 #define ELF_H
3 
4 #include <stdint.h>
5 
6 #define ELF_RELOC_ERR -1
7 
8 #define ELF_IDENT_LENGTH 16
9 typedef struct {
10  uint8_t ident[ELF_IDENT_LENGTH];
11  uint16_t type;
12  uint16_t machine;
13  uint32_t version;
14  uint32_t entry;
15  uint32_t phoff;
16  uint32_t shoff;
17  uint32_t flags;
18  uint16_t ehsize;
19  uint16_t phentsize;
20  uint16_t phnum;
21  uint16_t shentsize;
22  uint16_t shnum;
23  uint16_t shstrndx;
24 } elf_header;
25 
26 typedef struct {
27  uint32_t type; /* Segment type */
28  uint32_t offset; /* Segment file offset */
29  uint32_t vaddr; /* Segment virtual address */
30  uint32_t paddr; /* Segment physical address */
31  uint32_t filesz; /* Segment size in file */
32  uint32_t memsz; /* Segment size in memory */
33  uint32_t flags; /* Segment flags */
34  uint32_t align; /* Segment alignment */
35 } elf_phdr;
36 
37 typedef struct {
38  uint32_t name;
39  uint32_t type;
40  uint32_t flags;
41  uint32_t addr;
42  uint32_t offset;
43  uint32_t size;
44  uint32_t link;
45  uint32_t info;
46  uint32_t addralign;
47  uint32_t entsize;
48 } elf_s_header;
49 
50 typedef struct {
51  uint32_t name;
52  uint32_t value;
53  uint32_t size;
54  uint8_t info;
55  uint8_t other;
56  uint16_t shndx;
57 } elf_sym_tab;
58 
59 #define ELF32_ST_BIND(INFO) ((INFO) >> 4)
60 #define ELF32_ST_TYPE(INFO) ((INFO) & 0x0F)
61 
62 enum elf_sym_tab_bindings {
63  STB_LOCAL = 0, //local scope
64  STB_GLOBAL = 1, //global scope
65  STB_WEAK = 2, //weak (__attribute__((weak)))
66 };
67 
68 enum elf_sym_tab_types {
69  STT_NOTYPE = 0, //no type
70  STT_OBJECT = 1, //variable/array/etc
71  STT_FUNC = 2, //method/function
72 };
73 
74 #define SHN_UNDEF (0x00) //undefined/not present
75 #define SHN_ABS (0xfff1)
76 enum elf_sh_types {
77  SHT_NULL = 0, //null section
78  SHT_PROGBITS = 1, //program information
79  SHT_SYMTAB = 2, //symbol table
80  SHT_STRTAB = 3, //string table
81  SHT_RELA = 4, //relocation (w/ addend)
82  SHT_NOBITS = 8, //not present in file
83  SHT_REL = 9, //relocation (no addend)
84 };
85 
86 enum elf_sh_attr {
87  SHF_WRITE = 0x01, //writable section
88  SHF_ALLOC = 0x02, //exists in memory
89 };
90 
91 enum elf_ident {
92  EI_MAG0 = 0, //0x7F
93  EI_MAG1 = 1, //'E'
94  EI_MAG2 = 2, //'L'
95  EI_MAG3 = 3, //'F'
96  EI_CLASS = 4, //arch (32/64)
97  EI_DATA = 5, //endianness
98  EI_VERSION = 6, //ELF version
99  EI_OSABI = 7, //OS specific
100  EI_ABIVERSION = 8, //OS specific
101  EI_PAD = 9, //padding
102 };
103 
104 #define ELFMAG0 0x7F //ident[EL_MAG0]
105 #define ELFMAG1 'E' //ident[EL_MAG1]
106 #define ELFMAG2 'L' //ident[EL_MAG2]
107 #define ELFMAG3 'F' //ident[EL_MAG3]
108 #define ELFDATA2LSB (1) //little endian
109 #define ELFCLASS32 (1) //32-bit arch
110 
111 enum elf_type {
112  ET_NONE = 0, //unknown type
113  ET_REL = 1, //relocatable file
114  ET_EXEC = 2, //executable file
115 };
116 
117 #define EM_386 (3) //x86 type
118 #define EV_CURRENT (1) //ELF current version
119 
120 typedef struct {
121  uint32_t offset;
122  uint32_t info;
123 } elf_rel;
124 
125 typedef struct {
126  uint32_t offset;
127  uint32_t info;
128  int32_t addend;
129 } elf_rela;
130 
131 #define ELF_R_SYM(INFO) ((INFO) >> 8)
132 #define ELF_R_TYPE(INFO)((uint8_t)(INFO))
133 
134 enum elf_rt_types {
135  R_386_NONE = 0, //no relocation
136  R_386_32 = 1, //symbol + offset
137  R_386_PC32 = 2, //symbol + offset - section offset
138 };
139 
140 #define PT_LOAD 1
141 #define PT_DYNAMIC 2
142 #define PT_INTERP 3
143 
144 
145 void* elf_load_file(char* filename, void* file, uint32_t size);
146 
147 int execve(const char *filename, char *const argv[], char *const envp[]);
148 
149 #endif
Definition: elf.h:26
Definition: size.h:4
Definition: elf.h:37
Definition: elf.h:120
Definition: elf.h:9
Definition: elf.h:125
Definition: elf.h:50