TacOS  0.2
 Tout Structures de données Fichiers Fonctions Variables Définitions de type Énumérations Macros
elf.h
Aller à la documentation de ce fichier.
1 
29 #ifndef _ELF_H_
30 #define _ELF_H_
31 
32 typedef unsigned long int Elf32_Addr;
33 typedef unsigned short int Elf32_Half;
34 typedef unsigned long int Elf32_Off;
35 typedef signed long int Elf32_Sword;
36 typedef unsigned long int Elf32_Word;
37 
38 
39 #define EI_NIDENT 16
40 
46 typedef struct {
47  unsigned char e_ident[EI_NIDENT];
48  Elf32_Half e_type;
49  Elf32_Half e_machine;
50  Elf32_Word e_version;
51  Elf32_Addr e_entry;
52  Elf32_Off e_phoff;
53  Elf32_Off e_shoff;
54  Elf32_Word e_flags;
55  Elf32_Half e_ehsize;
56  Elf32_Half e_phentsize;
57  Elf32_Half e_phnum;
58  Elf32_Half e_shentsize;
59  Elf32_Half e_shnum;
60  Elf32_Half e_shstrndx;
61 } Elf32_Ehdr;
62 
63 /* Indices pour e_ident: */
64 #define EI_MAG0 0
65 #define EI_MAG1 1
66 #define EI_MAG2 2
67 #define EI_MAG3 3
68 #define EI_CLASS 4
69  #define ELFCLASSNONE 0
70  #define ELFCLASS32 1
71  #define ELFCLASS64 2
73 #define EI_DATA 5
74  #define ELFDATANONE 0
75  #define ELFDATA2LSB 1
76  #define ELFDATA2MSB 2
78 #define EI_VERSION 6
79 #define EI_PAD 7
80 
81 
82 /* Valeurs pour e_type: */
83 #define ET_NONE 0
84 #define ET_REL 1
85 #define ET_EXE 2
86 #define ET_DYN 3
87 #define ET_CORE 4
88 #define ET_LOPROC 0xff00
89 #define ET_HIPROC 0xffff
91 /* Valeurs pour e_machine: */
92 #define EM_NONE 0
93 #define EM_M32 1
94 #define EM_SPARC 2
95 #define EM_386 3
96 #define EM_68K 4
97 #define EM_88K 5
98 #define EM_860 7
99 #define EM_MIPS 8
101 /* Valeurs pour e_version: */
102 #define EV_NONE 0
103 #define EV_CURRENT 1
104 
105 /*******************************
106  *
107  * PROGRAM HEADER
108  *
109  *******************************/
110 
111 /* Indices des sections spéciales */
112 #define SHN_UNDEF 0
113 #define SHN_LORESERVE 0xff00
114 #define SHN_LOPROC 0xff00
115 #define SHN_HIPROC 0xff1f
116 #define SHN_ABS 0xfff1
117 #define SHN_COMMON 0xfff2
118 #define SHN_HIRESERVE 0xffff
119 
120 typedef struct{
121  Elf32_Word p_type;
122  Elf32_Off p_offset;
123  Elf32_Addr p_vaddr;
124  Elf32_Addr p_paddr;
125  Elf32_Word p_filesz;
126  Elf32_Word p_memsz;
127  Elf32_Word p_flags;
128  Elf32_Word p_align;
129 }Elf32_Phdr;
130 
131 /* Valeurs prises par p_type: */
132 #define PT_NULL 0
133 #define PT_LOAD 1
134 #define PT_DYNAMIC 2
135 #define PT_INTERP 3
136 #define PT_NOTE 4
137 #define PT_SHLIB 5
138 #define PT_PHDR 6
139 #define PT_LOPROC 0x7000000
140 #define PT_HIPROC 0x7FFFFFF
142 /* Valeurs prises par p_flags */
143 #define PF_X 0x1
144 #define PF_W 0x2
145 #define PF_R 0x4
146 
147 /********************************
148  *
149  * SECTION HEADER
150  *
151  ********************************/
152 
153 typedef struct {
154  Elf32_Word sh_name;
155  Elf32_Word sh_type;
156  Elf32_Word sh_flags;
157  Elf32_Addr sh_addr;
158  Elf32_Off sh_offset;
159  Elf32_Word sh_size;
160  Elf32_Word sh_link;
161  Elf32_Word sh_info;
162  Elf32_Word sh_addralign;
163  Elf32_Word sh_entsize;
164 }Elf32_Shdr;
165 
166 /* Valeurs pour sh_type */
167 #define SHT_NULL 0
168 #define SHT_PROGBITS 1
169 #define SHT_SYMTAB 2
170 #define SHT_STRTAB 3
171 #define SHT_RELA 4
172 #define SHT_HASH 5
173 #define SHT_DYNAMIC 6
174 #define SHT_NOTE 7
175 #define SHT_NOBITS 8
176 #define SHT_REL 9
177 #define SHT_SHLIB 10
178 #define SHT_DYNSYM 11
179 #define SHT_LOPROC 0x70000000
180 #define SHT_HIPROC 0x7fffffff
181 #define SHT_LOUSER 0x80000000
182 #define SHT_HIUSER 0x8fffffff
183 
184 /* Masques pour sh_flag */
185 #define SHF_WRITE 1
186 #define SHF_ALLOC 2
187 #define SHF_EXECINSTR 4
188 #define SHF_MASKPROC 0xf0000000
189 
190 /***************************
191  *
192  * SYMBOL TABLE
193  *
194  ***************************/
195 
196 typedef struct {
197  Elf32_Word st_name;
198  Elf32_Addr st_value;
199  Elf32_Word st_size;
200  unsigned char st_info;
201  unsigned char st_other;
202  Elf32_Half st_shndx;
203 }Elf32_Sym;
204 
205 
206 
207 /* Macros pour st_info */
208 #define ELF32_ST_BIND(i) ((i)>>4)
209 #define ELF32_ST_TYPE(i) ((i)&0xf)
210 #define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
211 
212 /* Valeurs pour ELF32_ST_BIND */
213 #define STB_LOCAL 0
214 #define STB_GLOBAL 1
215 #define STB_WEAK 2
216 #define STB_LOPROC 13
217 #define STB_HIPROC 15
218 
219 /* valeurs pour ELF32_ST_TYPE */
220 #define STT_NOTYPE 0
221 #define STT_OBJECT 1
222 #define STT_FUNC 2
223 #define STT_SECTION 3
224 #define STT_FILE 4
225 #define STT_LOPROC 13
226 #define STT_HIPROC 15
227 
228 /***************************
229  *
230  * RELOCATIONS
231  *
232  ***************************/
233 
234 typedef struct {
235  Elf32_Addr r_offset;
236  Elf32_Word r_info;
237 } Elf32_Rel;
238 
239 typedef struct {
240  Elf32_Addr r_offset;
241  Elf32_Word r_info;
242  Elf32_Word r_addend;
243 } Elf32_Rela;
244 
245 /* Macros pour r_info */
246 #define ELF32_R_SYM(i) ((i)>>8)
247 #define ELF32_R_TYPE(i) ((unsigned char)(i))
248 #define ELF32_R_INFO(s,t) (((s)<<8)+(unsigned char)(t))
249 
250 /* Valeurs pour le type contenu dans r_info */
251 #define R_386_NONE 0
252 #define R_386_32 1
253 #define R_386_PC32 2
254 #define R_386_GOT32 3
255 #define R_386_PLT32 4
256 #define R_386_COPY 5
257 #define R_386_GLOB_DAT 6
258 #define R_386_JMP_SLOT 7
259 #define R_386_RELATIVE 8
260 #define R_386_GOTOFF 9
261 #define R_386_GOTPC 10
262 
263 /***************************
264  *
265  * ELF FILE
266  *
267  ***************************/
268 
272 typedef struct {
273  char* name;
275  Elf32_Ehdr* elf_header;
276 
277  Elf32_Phdr* pheaders;
278  Elf32_Shdr* sheaders;
279 
280  int nb_symbols;
281  Elf32_Sym* sym_table;
282 
283  char* string_table;
284  char* symbol_string_table;
285 
286 }Elf32_File;
295 int load_efl_header(Elf32_Ehdr* elf_header, int fd);
296 
308 int load_program_header(Elf32_Phdr* program_header, Elf32_Ehdr* elf_header, int index, int fd);
309 
321 int load_section_header(Elf32_Shdr* section_header, Elf32_Ehdr* elf_header, int index, int fd);
322 
332 unsigned long int elf_size(int fd);
333 
343 int load_elf(int fd, void* dest);
344 
352 Elf32_File* load_elf_file(int fd);
353 
354 
363 Elf32_Sym* find_symbol(Elf32_File* file, const char* symbol);
364 
368 void elf_info(char* name);
369 
370 
371 #endif /* _ELF_H_ */
Elf32_Addr p_paddr
Definition: elf.h:124
Elf32_Word sh_flags
Definition: elf.h:156
Elf32_Off e_shoff
Definition: elf.h:53
Elf32_Word e_version
Definition: elf.h:50
Elf32_Sym * find_symbol(Elf32_File *file, const char *symbol)
Trouve un symbole dans un fichier.
Definition: elf.c:236
Definition: elf.h:46
Elf32_Word e_flags
Definition: elf.h:54
Elf32_Addr st_value
Definition: elf.h:198
Definition: elf.h:153
Elf32_Half e_type
Definition: elf.h:48
unsigned long int elf_size(int fd)
Calcul la taille que prendra l'exécutable dans la mémoire La fonction évalue l'adresse de départ en m...
Definition: elf.c:78
Elf32_Half e_shnum
Definition: elf.h:59
Elf32_Word sh_link
Definition: elf.h:160
Structure qui caractérise un binaire elf.
Definition: elf.h:272
Definition: elf.h:196
Elf32_Half e_phentsize
Definition: elf.h:56
char * name
Definition: elf.h:273
Elf32_Word sh_addralign
Definition: elf.h:162
Definition: elf.h:239
Elf32_Word sh_info
Definition: elf.h:161
Elf32_Word sh_size
Definition: elf.h:159
int load_section_header(Elf32_Shdr *section_header, Elf32_Ehdr *elf_header, int index, int fd)
charge un section header depuis un fichier elf Charge le section header n°index depuis le fichier ...
Definition: elf.h:234
Elf32_Addr sh_addr
Definition: elf.h:157
Elf32_Word p_memsz
Definition: elf.h:126
Elf32_Word p_align
Definition: elf.h:128
Elf32_Half e_shstrndx
Definition: elf.h:60
Elf32_Addr p_vaddr
Definition: elf.h:123
Elf32_Word p_filesz
Definition: elf.h:125
Elf32_Word sh_entsize
Definition: elf.h:163
int load_elf(int fd, void *dest)
charge un fichier elf Charge les données utiles de l'exécutable en mémoire, de façon à ce que ce soit...
Definition: elf.c:108
unsigned char st_other
Definition: elf.h:201
Elf32_Off e_phoff
Definition: elf.h:52
Elf32_Addr e_entry
Definition: elf.h:51
Elf32_Word sh_name
Definition: elf.h:154
int load_efl_header(Elf32_Ehdr *elf_header, int fd)
charge le header principal d'un fichier elf
Elf32_Word st_name
Definition: elf.h:197
Elf32_Half e_shentsize
Definition: elf.h:58
Elf32_Word p_type
Definition: elf.h:121
int load_program_header(Elf32_Phdr *program_header, Elf32_Ehdr *elf_header, int index, int fd)
charge un program header depuis un fichier elf Charge le program header n°index depuis le fichier ...
Definition: elf.c:66
void elf_info(char *name)
Affiche des information sur le fichier elf.
Elf32_Half st_shndx
Definition: elf.h:202
Elf32_Off p_offset
Definition: elf.h:122
Elf32_File * load_elf_file(int fd)
charge un fichier elf dans la structure Elf32_File
Definition: elf.c:150
Elf32_Half e_phnum
Definition: elf.h:57
Elf32_Word sh_type
Definition: elf.h:155
Elf32_Half e_machine
Definition: elf.h:49
Elf32_Half e_ehsize
Definition: elf.h:55
Elf32_Off sh_offset
Definition: elf.h:158
Elf32_Word p_flags
Definition: elf.h:127
unsigned char st_info
Definition: elf.h:200
Definition: elf.h:120