update readme for more up-to-date information
[lunaix-os.git] / lunaix-os / includes / lunaix / exebi / elf32.h
1 #ifndef __LUNAIX_ELF32_H
2 #define __LUNAIX_ELF32_H
3
4 #include <lunaix/types.h>
5
6 typedef unsigned int elf32_ptr_t;
7 typedef unsigned short elf32_hlf_t;
8 typedef unsigned int elf32_off_t;
9 typedef unsigned int elf32_swd_t;
10 typedef unsigned int elf32_wrd_t;
11
12 #define ET_NONE 0
13 #define ET_EXEC 2
14
15 #define PT_LOAD 1
16 #define PT_INTERP 3
17
18 #define PF_X 0x1
19 #define PF_W 0x2
20 #define PF_R 0x4
21
22 #define EM_NONE 0
23 #define EM_386 3
24
25 #define EV_CURRENT 1
26
27 // [0x7f, 'E', 'L', 'F']
28 #define ELFMAGIC 0x464c457fU
29 #define ELFCLASS32 1
30 #define ELFCLASS64 2
31 #define ELFDATA2LSB 1
32 #define ELFDATA2MSB 2
33
34 #define EI_CLASS 4
35 #define EI_DATA 5
36
37 #define NO_LOADER 0
38 #define DEFAULT_LOADER "usr/ld"
39
40 struct elf32_ehdr
41 {
42     u8_t e_ident[16];
43     elf32_hlf_t e_type;
44     elf32_hlf_t e_machine;
45     elf32_wrd_t e_version;
46     elf32_ptr_t e_entry;
47     elf32_off_t e_phoff;
48     elf32_off_t e_shoff;
49     elf32_wrd_t e_flags;
50     elf32_hlf_t e_ehsize;
51     elf32_hlf_t e_phentsize;
52     elf32_hlf_t e_phnum;
53     elf32_hlf_t e_shentsize;
54     elf32_hlf_t e_shnum;
55     elf32_hlf_t e_shstrndx;
56 };
57
58 struct elf32_phdr
59 {
60     elf32_wrd_t p_type;
61     elf32_off_t p_offset;
62     elf32_ptr_t p_va;
63     elf32_ptr_t p_pa;
64     elf32_wrd_t p_filesz;
65     elf32_wrd_t p_memsz;
66     elf32_wrd_t p_flags;
67     elf32_wrd_t p_align;
68 };
69
70 struct elf32
71 {
72     const void* elf_file;
73     struct elf32_ehdr eheader;
74     struct elf32_phdr* pheaders;
75 };
76
77 #define declare_elf32(elf, elf_vfile)                                          \
78     struct elf32 elf = { .elf_file = elf_vfile, .pheaders = (void*)0 }
79
80 int
81 elf32_check_exec(const struct elf32* elf);
82
83 int
84 elf32_open(struct elf32* elf, const char* path);
85
86 int
87 elf32_openat(struct elf32* elf, const void* elf_vfile);
88
89 int
90 elf32_static_linked(const struct elf32* elf);
91
92 int
93 elf32_close(struct elf32* elf);
94
95 /**
96  * @brief Try to find the PT_INTERP section. If found, copy it's content to
97  * path_out
98  *
99  * @param elf Opened elf32 descriptor
100  * @param path_out
101  * @param len size of path_out buffer
102  * @return int
103  */
104 int
105 elf32_find_loader(const struct elf32* elf, char* path_out, size_t len);
106
107 int
108 elf32_read_ehdr(struct elf32* elf);
109
110 int
111 elf32_read_phdr(struct elf32* elf);
112
113 /**
114  * @brief Estimate how much memeory will be acquired if we load all loadable
115  * sections.、
116  *
117  * @param elf
118  * @return size_t
119  */
120 size_t
121 elf32_loadable_memsz(const struct elf32* elf);
122
123 #define SIZE_EHDR sizeof(struct elf32_ehdr)
124 #define SIZE_PHDR sizeof(struct elf32_phdr)
125
126 #endif /* __LUNAIX_ELF32_H */