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