MOS Source Code
Loading...
Searching...
No Matches
elf.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include "mos/tasks/process.h"
7
8#include <elf.h>
9#include <mos/types.h>
10
17
18#if MOS_LITTLE_ENDIAN
19#define ELF_ENDIANNESS_MOS_DEFAULT ELF_ENDIANNESS_LSB
20#else
21#define ELF_ENDIANNESS_MOS_DEFAULT ELF_ENDIANNESS_MSB
22#endif
23
24typedef struct
25{
26 char magic[4];
27 u32 bits : 8;
32 u8 __padding[7];
34
35MOS_STATIC_ASSERT(sizeof(elf_identity_t) == 16, "elf_identity_t has wrong size");
36
37typedef struct
38{
40 int object_type : 16;
41 int machine_type : 16;
42
44
46 size_t ph_offset;
47 size_t sh_offset;
48
51
52 struct
53 {
54 u16 entry_size, count;
55 } __packed ph, sh;
56
59
60typedef enum
61{
62 ELF_PT_NULL = 0, // Unused entry
63 ELF_PT_LOAD = 1, // Loadable segment
64 ELF_PT_DYNAMIC = 2, // Dynamic linking information
65 ELF_PT_INTERP = 3, // Interpreter information
66 ELF_PT_NOTE = 4, // Auxiliary information
67 ELF_PT_SHLIB = 5, // reserved
68 ELF_PT_PHDR = 6, // Segment containing program header table
69 ELF_PT_TLS = 7, // Thread-local storage segment
70
72
73 ELF_PT_OS_LOW = 0x60000000, // reserved
74 ELF_PT_OS_HIGH = 0x6fffffff, // reserved
75 ELF_PT_PROCESSOR_LO = 0x70000000, // reserved
76 ELF_PT_PROCESSOR_HI = 0x7fffffff, // reserved
78
79typedef enum
80{
81 ELF_PF_X = 1 << 0, // Executable
82 ELF_PF_W = 1 << 1, // Writable
83 ELF_PF_R = 1 << 2, // Readable
85
86typedef struct
87{
89 elf_ph_flags p_flags; // Segment independent flags (64-bit only)
90 ptr_t data_offset; // Offset of the segment in the file
91 ptr_t vaddr; // Virtual address of the segment
92 ptr_t _reserved; // reserved
93 ptr_t size_in_file; // Size of the segment in the file (may be 0)
94 ptr_t size_in_mem; // Size of the segment in memory (may be 0)
97
98#define AUXV_VEC_SIZE 16
99
100typedef struct
101{
102 int count;
103 Elf64_auxv_t vector[AUXV_VEC_SIZE];
104} auxv_vec_t;
105
106typedef struct
107{
108 const char *invocation;
110 int argc;
111 const char **argv;
112
113 int envc;
114 const char **envp;
116
118__nodiscard bool elf_fill_process(process_t *proc, file_t *file, const char *path, const char *const argv[], const char *const envp[]);
120process_t *elf_create_process(const char *path, process_t *parent, const char *const argv[], const char *const envp[], const stdio_t *ios);
elf_program_header_type
Definition elf.h:61
@ ELF_PT_PROCESSOR_LO
Definition elf.h:75
@ ELF_PT_PHDR
Definition elf.h:68
@ ELF_PT_DYNAMIC
Definition elf.h:64
@ ELF_PT_LOAD
Definition elf.h:63
@ ELF_PT_OS_HIGH
Definition elf.h:74
@ _ELF_PT_COUNT
Definition elf.h:71
@ ELF_PT_OS_LOW
Definition elf.h:73
@ ELF_PT_PROCESSOR_HI
Definition elf.h:76
@ ELF_PT_NOTE
Definition elf.h:66
@ ELF_PT_SHLIB
Definition elf.h:67
@ ELF_PT_INTERP
Definition elf.h:65
@ ELF_PT_NULL
Definition elf.h:62
@ ELF_PT_TLS
Definition elf.h:69
elf_endianness
Definition elf.h:12
@ ELF_ENDIANNESS_INVALID
Definition elf.h:13
@ ELF_ENDIANNESS_MSB
Definition elf.h:15
@ ELF_ENDIANNESS_LSB
Definition elf.h:14
__nodiscard bool elf_fill_process(process_t *proc, file_t *file, const char *path, const char *const argv[], const char *const envp[])
Definition elf.c:353
process_t * elf_create_process(const char *path, process_t *parent, const char *const argv[], const char *const envp[], const stdio_t *ios)
Definition elf.c:408
__nodiscard bool elf_read_and_verify_executable(file_t *file, elf_header_t *header)
Definition elf.c:338
__nodiscard bool elf_do_fill_process(process_t *proc, file_t *file, elf_header_t elf, elf_startup_info_t *info)
Definition elf.c:230
elf_ph_flags
Definition elf.h:80
@ ELF_PF_X
Definition elf.h:81
@ ELF_PF_W
Definition elf.h:82
@ ELF_PF_R
Definition elf.h:83
#define AUXV_VEC_SIZE
Definition elf.h:98
const char ** argv
Definition kmain.c:44
#define __packed
Definition mos_global.h:29
#define __nodiscard
Definition mos_global.h:35
#define MOS_STATIC_ASSERT
Definition mos_global.h:14
int count
Definition elf.h:102
elf_identity_t identity
Definition elf.h:39
int machine_type
Definition elf.h:41
u16 header_size
Definition elf.h:50
u32 version
Definition elf.h:43
int object_type
Definition elf.h:40
u16 count
Definition elf.h:54
u16 sh_strtab_index
Definition elf.h:57
size_t sh_offset
Definition elf.h:47
size_t ph_offset
Definition elf.h:46
u32 flags
Definition elf.h:49
ptr_t entry_point
Definition elf.h:45
u32 version
Definition elf.h:29
u32 endianness
Definition elf.h:28
u32 osabi
Definition elf.h:30
u8 abiversion
Definition elf.h:31
u32 bits
Definition elf.h:27
elf_program_header_type header_type
Definition elf.h:88
ptr_t size_in_mem
Definition elf.h:94
ptr_t vaddr
Definition elf.h:91
elf_ph_flags p_flags
Definition elf.h:89
ptr_t required_alignment
Definition elf.h:95
ptr_t _reserved
Definition elf.h:92
ptr_t size_in_file
Definition elf.h:93
ptr_t data_offset
Definition elf.h:90
auxv_vec_t auxv
Definition elf.h:109
const char ** envp
Definition elf.h:114
const char ** argv
Definition elf.h:111
const char * invocation
Definition elf.h:108
A wrapper type for the standard I/O streams.
Definition process.h:17
unsigned int u32
Definition types.h:21
unsigned short u16
Definition types.h:20
unsigned long ptr_t
Definition types.h:25
unsigned char u8
Definition types.h:19