MentOS  0.8.0
The Mentoring Operating System
elf.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include "process/process.h"
9 #include "stdint.h"
10 
14 
16 #define PT_NULL 0
24 #define PT_LOAD 1
26 #define PT_DYNAMIC 2
32 #define PT_INTERP 3
34 #define PT_NOTE 4
36 #define PT_SHLIB 5
43 #define PT_PHDR 6
49 #define PT_EH_FRAME 0x6474E550
52 #define PT_GNU_STACK 0x6474E551
57 #define PT_GNU_RELRO 0x6474E552
59 #define PT_LOPROC 0x70000000
61 #define PT_HIPROC 0x7FFFFFFF
62 
64 
66 #define EI_NIDENT 16
67 
69 typedef struct elf_header {
99 
101 typedef struct elf_program_header {
121 
123 typedef struct elf_section_header {
145 
147 typedef struct elf_symbol {
161 
163 typedef struct elf_rel_t {
169 
171 typedef struct elf_rela_t {
179 
181 enum Elf_Ident {
182  EI_MAG0 = 0,
183  EI_MAG1 = 1,
184  EI_MAG2 = 2,
185  EI_MAG3 = 3,
186  EI_CLASS = 4,
187  EI_DATA = 5,
189  EI_OSABI = 7,
191  EI_PAD = 9
192 };
193 
194 #define ELFMAG0 0x7F
195 #define ELFMAG1 'E'
196 #define ELFMAG2 'L'
197 #define ELFMAG3 'F'
198 
199 #define ELFDATA2LSB 1
200 #define ELFCLASS32 1
201 
203 typedef enum Elf_Type {
204  ET_NONE = 0,
205  ET_REL = 1,
206  ET_EXEC = 2
208 
209 #define EM_386 3
210 #define EV_CURRENT 1
211 
214 typedef enum ShT_Types {
215  SHT_NULL = 0,
219  SHT_RELA = 4,
221  SHT_REL = 9,
223 
227  SHF_WRITE = 0x01,
228  SHF_ALLOC = 0x02
229 };
230 
232 #define ELF32_ST_BIND(INFO) ((INFO) >> 4)
234 #define ELF32_ST_TYPE(INFO) ((INFO)&0x0F)
235 
238  STB_LOCAL = 0,
240  STB_WEAK = 2
241 };
242 
244 enum StT_Types {
247  STT_FUNC = 2
248 };
249 
255 int elf_load_file(task_struct *task, vfs_file_t *file, uint32_t *entry);
256 
261 int elf_check_file_type(vfs_file_t *file, Elf_Type type);
262 
267 
272 
276 const char *elf_type_to_string(int type);
277 
281 const char *elf_section_header_type_to_string(int type);
282 
286 const char *elf_symbol_type_to_string(int type);
287 
291 const char *elf_symbol_bind_to_string(int bind);
int elf_check_magic_number(elf_header_t *hdr)
Checks the correctness of the ELF header magic number.
Definition: elf.c:398
struct elf_section_header elf_section_header_t
A section header with all kinds of useful information.
int elf_check_file_header(elf_header_t *hdr)
Checks the correctness of the ELF header.
Definition: elf.c:369
const char * elf_type_to_string(int type)
Transforms the passed ELF type to string.
Definition: elf.c:422
Elf_Ident
Fields index of ELF_IDENT.
Definition: elf.h:181
@ EI_ABIVERSION
OS Specific.
Definition: elf.h:190
@ EI_MAG1
'E'
Definition: elf.h:183
@ EI_DATA
Set to either 1 or 2 to signify little or big endianness.
Definition: elf.h:187
@ EI_MAG0
0x7F
Definition: elf.h:182
@ EI_PAD
Padding.
Definition: elf.h:191
@ EI_MAG3
'F'
Definition: elf.h:185
@ EI_CLASS
Architecture (32/64)
Definition: elf.h:186
@ EI_OSABI
OS Specific.
Definition: elf.h:189
@ EI_VERSION
ELF Version.
Definition: elf.h:188
@ EI_MAG2
'L'
Definition: elf.h:184
struct elf_symbol elf_symbol_t
A symbol itself.
int elf_check_file_type(vfs_file_t *file, Elf_Type type)
Checks if the file is a valid ELF.
Definition: elf.c:345
ShT_Attributes
ShT_Attributes corresponds to the field sh_flags, but are bit flags rather than stand-alone values.
Definition: elf.h:226
@ SHF_ALLOC
Exists in memory.
Definition: elf.h:228
@ SHF_WRITE
Writable section.
Definition: elf.h:227
const char * elf_section_header_type_to_string(int type)
Transforms the passed ELF section header type to string.
Definition: elf.c:438
struct elf_rel_t elf_rel_t
Holds information about relocation object (that do not need an addend).
struct elf_rela_t elf_rela_t
Holds information about relocation object (that need an addend).
StT_Bindings
Provides possible symbol bindings.
Definition: elf.h:237
@ STB_GLOBAL
Global scope.
Definition: elf.h:239
@ STB_LOCAL
Local scope.
Definition: elf.h:238
@ STB_WEAK
Weak, (ie. attribute((weak)))
Definition: elf.h:240
const char * elf_symbol_type_to_string(int type)
Transforms the passed ELF symbol type to string.
Definition: elf.c:449
struct elf_program_header elf_program_header_t
The elf program header, holding program layout information.
struct elf_header elf_header_t
The elf starting section.
int elf_load_file(task_struct *task, vfs_file_t *file, uint32_t *entry)
Loads an ELF file into the memory of task.
Definition: elf.c:287
ShT_Types
Defines a number of different types of sections, which correspond to values stored in the field sh_ty...
Definition: elf.h:214
@ SHT_RELA
Relocation (w/ addend)
Definition: elf.h:219
@ SHT_PROGBITS
Program information.
Definition: elf.h:216
@ SHT_STRTAB
String table.
Definition: elf.h:218
@ SHT_REL
Relocation (no addend)
Definition: elf.h:221
@ SHT_SYMTAB
Symbol table.
Definition: elf.h:217
@ SHT_NOBITS
Not present in file.
Definition: elf.h:220
@ SHT_NULL
Null section.
Definition: elf.h:215
#define EI_NIDENT
Elf header ident size.
Definition: elf.h:66
Elf_Type
Type of ELF files.
Definition: elf.h:203
@ ET_NONE
Unkown Type.
Definition: elf.h:204
@ ET_REL
Relocatable File.
Definition: elf.h:205
@ ET_EXEC
Executable File.
Definition: elf.h:206
const char * elf_symbol_bind_to_string(int bind)
Transforms the passed ELF symbol bind to string.
Definition: elf.c:457
StT_Types
Provides a number of possible symbol types.
Definition: elf.h:244
@ STT_FUNC
Methods or functions.
Definition: elf.h:247
@ STT_NOTYPE
No type.
Definition: elf.h:245
@ STT_OBJECT
Variables, arrays, etc.
Definition: elf.h:246
Process data structures and functions.
Standard integer data-types.
unsigned short uint16_t
Define the unsigned 16-bit integer.
Definition: stdint.h:24
int int32_t
Define the signed 32-bit integer.
Definition: stdint.h:15
unsigned int uint32_t
Define the unsigned 32-bit integer.
Definition: stdint.h:18
unsigned char uint8_t
Define the unsigned 8-bit integer.
Definition: stdint.h:30
The elf starting section.
Definition: elf.h:69
uint16_t phnum
Number of entries in the program header table.
Definition: elf.h:91
uint8_t ident[EI_NIDENT]
Elf header identity bits.
Definition: elf.h:71
uint16_t shstrndx
Section header table index of sect name string table.
Definition: elf.h:97
uint16_t type
Identifies object file type.
Definition: elf.h:73
uint32_t shoff
Points to the start of the section header table.
Definition: elf.h:83
uint16_t machine
Specifies target instruction set architecture.
Definition: elf.h:75
uint32_t version
Set to 1 for the original version of ELF.
Definition: elf.h:77
uint32_t phoff
Points to the start of the program header table.
Definition: elf.h:81
uint32_t entry
This is the memory address of the entry point from where the process starts executing.
Definition: elf.h:79
uint16_t ehsize
Size of ELF header, in bytes.
Definition: elf.h:87
uint16_t shentsize
Size of an entry in the section header table.
Definition: elf.h:93
uint16_t shnum
Number of entries in the section header table.
Definition: elf.h:95
uint32_t flags
Processor-specific flags.
Definition: elf.h:85
uint16_t phentsize
Size of an entry in the program header table.
Definition: elf.h:89
The elf program header, holding program layout information.
Definition: elf.h:101
uint32_t paddr
Definition: elf.h:110
uint32_t align
Definition: elf.h:119
uint32_t vaddr
Virtual address of the segment in memory.
Definition: elf.h:107
uint32_t flags
Segment-dependent flags.
Definition: elf.h:116
uint32_t offset
Offset of the segment in the file image.
Definition: elf.h:105
uint32_t type
Identifies the type of the segment.
Definition: elf.h:103
uint32_t memsz
Size in bytes of the segment in memory. May be 0.
Definition: elf.h:114
uint32_t filesz
Size in bytes of the segment in the file image. May be 0.
Definition: elf.h:112
Holds information about relocation object (that do not need an addend).
Definition: elf.h:163
uint32_t r_info
TODO: Comment.
Definition: elf.h:167
uint32_t r_offset
TODO: Comment.
Definition: elf.h:165
Holds information about relocation object (that need an addend).
Definition: elf.h:171
int32_t r_addend
TODO: Comment.
Definition: elf.h:177
uint32_t r_offset
TODO: Comment.
Definition: elf.h:173
uint32_t r_info
TODO: Comment.
Definition: elf.h:175
A section header with all kinds of useful information.
Definition: elf.h:123
uint32_t info
TODO: Comment.
Definition: elf.h:139
uint32_t offset
TODO: Comment.
Definition: elf.h:133
uint32_t name
TODO: Comment.
Definition: elf.h:125
uint32_t addralign
TODO: Comment.
Definition: elf.h:141
uint32_t link
TODO: Comment.
Definition: elf.h:137
uint32_t size
TODO: Comment.
Definition: elf.h:135
uint32_t addr
TODO: Comment.
Definition: elf.h:131
uint32_t flags
TODO: Comment.
Definition: elf.h:129
uint32_t type
TODO: Comment.
Definition: elf.h:127
uint32_t entsize
TODO: Comment.
Definition: elf.h:143
A symbol itself.
Definition: elf.h:147
uint8_t info
TODO: Comment.
Definition: elf.h:155
uint8_t other
TODO: Comment.
Definition: elf.h:157
uint32_t name
TODO: Comment.
Definition: elf.h:149
uint32_t value
TODO: Comment.
Definition: elf.h:151
uint16_t ndx
TODO: Comment.
Definition: elf.h:159
uint32_t size
TODO: Comment.
Definition: elf.h:153
this is our task object. Every process in the system has this, and it holds a lot of information....
Definition: process.h:82
Data structure that contains information about the mounted filesystems.
Definition: vfs_types.h:112