• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #ifndef ELF_H
13 #define ELF_H
14 
15 #include <errno.h>
16 #include <stdint.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 /*
23  * ELF format according to
24  * https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
25  */
26 
27 #define EI_MAG_SIZE 4
28 
29 #define PT_NULL    0x00000000
30 #define PT_LOAD    0x00000001
31 #define PT_DYNAMIC 0x00000002
32 #define PT_INTERP  0x00000003
33 #define PT_NOTE    0x00000004
34 #define PT_SHLIB   0x00000005
35 #define PT_PHDR    0x00000006
36 #define PT_LOOS    0x60000000
37 #define PT_HIOS    0x6fffffff
38 #define PT_LOPROC  0x70000000
39 #define PT_HIRPOC  0x7fffffff
40 
41 #define PF_ALL 0x7
42 #define PF_X   0x1
43 #define PF_W   0x2
44 #define PF_R   0x4
45 
46 typedef uint64_t u64;
47 typedef uint32_t u32;
48 typedef uint16_t u16;
49 typedef uint8_t u8;
50 
51 typedef int64_t s64;
52 typedef int32_t s32;
53 typedef int16_t s16;
54 typedef int8_t s8;
55 
56 /*
57  * This part of ELF header is endianness-independent.
58  */
59 struct elf_indent {
60     u8 ei_magic[4];
61     u8 ei_class;
62     u8 ei_data;
63     u8 ei_version;
64     u8 ei_osabi;
65     u8 ei_abiversion;
66     u8 ei_pad[7];
67 };
68 
69 /*
70  * ELF header format. One should check the `e_indent` to decide the endianness.
71  */
72 struct elf_header {
73     struct elf_indent e_indent;
74     u16 e_type;
75     u16 e_machine;
76     u32 e_version;
77     u64 e_entry;
78     u64 e_phoff;
79     u64 e_shoff;
80     u32 e_flags;
81     u16 e_ehsize;
82     u16 e_phentsize;
83     u16 e_phnum;
84     u16 e_shentsize;
85     u16 e_shnum;
86     u16 e_shstrndx;
87 };
88 
89 /*
90  * 32-Bit of the elf_header. Check the `e_indent` first to decide.
91  */
92 struct elf_header_32 {
93     struct elf_indent e_indent;
94     u16 e_type;
95     u16 e_machine;
96     u32 e_version;
97     u32 e_entry;
98     u32 e_phoff;
99     u32 e_shoff;
100     u32 e_flags;
101     u16 e_ehsize;
102     u16 e_phentsize;
103     u16 e_phnum;
104     u16 e_shentsize;
105     u16 e_shnum;
106     u16 e_shstrndx;
107 };
108 
109 struct elf_program_header {
110     u32 p_type;
111     u32 p_flags;
112     u64 p_offset;
113     u64 p_vaddr;
114     u64 p_paddr;
115     u64 p_filesz;
116     u64 p_memsz;
117     u64 p_align;
118 };
119 struct elf_program_header_32 {
120     u32 p_type;
121     u32 p_offset;
122     u32 p_vaddr;
123     u32 p_paddr;
124     u32 p_filesz;
125     u32 p_memsz;
126     u32 p_flags;
127     u32 p_align;
128 };
129 
130 struct elf_section_header {
131     u32 sh_name;
132     u32 sh_type;
133     u64 sh_flags;
134     u64 sh_addr;
135     u64 sh_offset;
136     u64 sh_size;
137     u32 sh_link;
138     u32 sh_info;
139     u64 sh_addralign;
140     u64 sh_entsize;
141 };
142 struct elf_section_header_32 {
143     u32 sh_name;
144     u32 sh_type;
145     u32 sh_flags;
146     u32 sh_addr;
147     u32 sh_offset;
148     u32 sh_size;
149     u32 sh_link;
150     u32 sh_info;
151     u32 sh_addralign;
152     u32 sh_entsize;
153 };
154 
155 struct elf_file {
156     struct elf_header header;
157     struct elf_program_header *p_headers;
158     struct elf_section_header *s_headers;
159 };
160 
161 struct elf_info {
162   u64 mem_size;
163   u64 entry;
164   u64 flags;
165   u64 phentsize;
166   u64 phnum;
167   u64 phdr_addr;
168   struct elf_program_header phdr[3]; /* currently 3 segments for procmgr */
169 };
170 
171 struct elf_file *elf_parse_file(const char *code, struct elf_info*info);
172 void elf_free(struct elf_file *elf);
173 
174 #ifdef __cplusplus
175 }
176 #endif
177 #endif /* ELF_H */
178